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 4096
92 #define NUM_BLOCKS_DIRTY_STRIPE_REGION 2056
93 #define SECT_PER_MB_SHIFT 11
94 #define MAX_SECTOR_SIZE 4096
96 /* Disk configuration info. */
97 #define IMSM_MAX_DEVICES 255
99 __u8 serial
[MAX_RAID_SERIAL_LEN
];/* 0xD8 - 0xE7 ascii serial number */
100 __u32 total_blocks_lo
; /* 0xE8 - 0xEB total blocks lo */
101 __u32 scsi_id
; /* 0xEC - 0xEF scsi ID */
102 #define SPARE_DISK __cpu_to_le32(0x01) /* Spare */
103 #define CONFIGURED_DISK __cpu_to_le32(0x02) /* Member of some RaidDev */
104 #define FAILED_DISK __cpu_to_le32(0x04) /* Permanent failure */
105 #define JOURNAL_DISK __cpu_to_le32(0x2000000) /* Device marked as Journaling Drive */
106 __u32 status
; /* 0xF0 - 0xF3 */
107 __u32 owner_cfg_num
; /* which config 0,1,2... owns this disk */
108 __u32 total_blocks_hi
; /* 0xF4 - 0xF5 total blocks hi */
109 #define IMSM_DISK_FILLERS 3
110 __u32 filler
[IMSM_DISK_FILLERS
]; /* 0xF5 - 0x107 MPB_DISK_FILLERS for future expansion */
113 /* map selector for map managment
119 /* RAID map configuration infos. */
121 __u32 pba_of_lba0_lo
; /* start address of partition */
122 __u32 blocks_per_member_lo
;/* blocks per member */
123 __u32 num_data_stripes_lo
; /* number of data stripes */
124 __u16 blocks_per_strip
;
125 __u8 map_state
; /* Normal, Uninitialized, Degraded, Failed */
126 #define IMSM_T_STATE_NORMAL 0
127 #define IMSM_T_STATE_UNINITIALIZED 1
128 #define IMSM_T_STATE_DEGRADED 2
129 #define IMSM_T_STATE_FAILED 3
131 #define IMSM_T_RAID0 0
132 #define IMSM_T_RAID1 1
133 #define IMSM_T_RAID5 5 /* since metadata version 1.2.02 ? */
134 __u8 num_members
; /* number of member disks */
135 __u8 num_domains
; /* number of parity domains */
136 __u8 failed_disk_num
; /* valid only when state is degraded */
138 __u32 pba_of_lba0_hi
;
139 __u32 blocks_per_member_hi
;
140 __u32 num_data_stripes_hi
;
141 __u32 filler
[4]; /* expansion area */
142 #define IMSM_ORD_REBUILD (1 << 24)
143 __u32 disk_ord_tbl
[1]; /* disk_ord_tbl[num_members],
144 * top byte contains some flags
146 } __attribute__ ((packed
));
149 __u32 curr_migr_unit
;
150 __u32 checkpoint_id
; /* id to access curr_migr_unit */
151 __u8 migr_state
; /* Normal or Migrating */
153 #define MIGR_REBUILD 1
154 #define MIGR_VERIFY 2 /* analagous to echo check > sync_action */
155 #define MIGR_GEN_MIGR 3
156 #define MIGR_STATE_CHANGE 4
157 #define MIGR_REPAIR 5
158 __u8 migr_type
; /* Initializing, Rebuilding, ... */
159 #define RAIDVOL_CLEAN 0
160 #define RAIDVOL_DIRTY 1
161 #define RAIDVOL_DSRECORD_VALID 2
163 __u8 fs_state
; /* fast-sync state for CnG (0xff == disabled) */
164 __u16 verify_errors
; /* number of mismatches */
165 __u16 bad_blocks
; /* number of bad blocks during verify */
167 struct imsm_map map
[1];
168 /* here comes another one if migr_state */
169 } __attribute__ ((packed
));
172 __u8 volume
[MAX_RAID_SERIAL_LEN
];
175 #define DEV_BOOTABLE __cpu_to_le32(0x01)
176 #define DEV_BOOT_DEVICE __cpu_to_le32(0x02)
177 #define DEV_READ_COALESCING __cpu_to_le32(0x04)
178 #define DEV_WRITE_COALESCING __cpu_to_le32(0x08)
179 #define DEV_LAST_SHUTDOWN_DIRTY __cpu_to_le32(0x10)
180 #define DEV_HIDDEN_AT_BOOT __cpu_to_le32(0x20)
181 #define DEV_CURRENTLY_HIDDEN __cpu_to_le32(0x40)
182 #define DEV_VERIFY_AND_FIX __cpu_to_le32(0x80)
183 #define DEV_MAP_STATE_UNINIT __cpu_to_le32(0x100)
184 #define DEV_NO_AUTO_RECOVERY __cpu_to_le32(0x200)
185 #define DEV_CLONE_N_GO __cpu_to_le32(0x400)
186 #define DEV_CLONE_MAN_SYNC __cpu_to_le32(0x800)
187 #define DEV_CNG_MASTER_DISK_NUM __cpu_to_le32(0x1000)
188 __u32 status
; /* Persistent RaidDev status */
189 __u32 reserved_blocks
; /* Reserved blocks at beginning of volume */
193 __u8 cng_master_disk
;
197 __u16 my_vol_raid_dev_num
; /* Used in Unique volume Id for this RaidDev */
203 /* Unique Volume Id of the NvCache Volume associated with this volume */
204 __u32 nvc_vol_orig_family_num
;
205 __u16 nvc_vol_raid_dev_num
;
208 #define RWH_DISTRIBUTED 1
209 #define RWH_JOURNALING_DRIVE 2
210 __u8 rwh_policy
; /* Raid Write Hole Policy */
211 __u8 jd_serial
[MAX_RAID_SERIAL_LEN
]; /* Journal Drive serial number */
214 #define IMSM_DEV_FILLERS 3
215 __u32 filler
[IMSM_DEV_FILLERS
];
217 } __attribute__ ((packed
));
220 __u8 sig
[MAX_SIGNATURE_LENGTH
]; /* 0x00 - 0x1F */
221 __u32 check_sum
; /* 0x20 - 0x23 MPB Checksum */
222 __u32 mpb_size
; /* 0x24 - 0x27 Size of MPB */
223 __u32 family_num
; /* 0x28 - 0x2B Checksum from first time this config was written */
224 __u32 generation_num
; /* 0x2C - 0x2F Incremented each time this array's MPB is written */
225 __u32 error_log_size
; /* 0x30 - 0x33 in bytes */
226 __u32 attributes
; /* 0x34 - 0x37 */
227 __u8 num_disks
; /* 0x38 Number of configured disks */
228 __u8 num_raid_devs
; /* 0x39 Number of configured volumes */
229 __u8 error_log_pos
; /* 0x3A */
230 __u8 fill
[1]; /* 0x3B */
231 __u32 cache_size
; /* 0x3c - 0x40 in mb */
232 __u32 orig_family_num
; /* 0x40 - 0x43 original family num */
233 __u32 pwr_cycle_count
; /* 0x44 - 0x47 simulated power cycle count for array */
234 __u32 bbm_log_size
; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */
235 __u16 num_raid_devs_created
; /* 0x4C - 0x4D Used for generating unique
236 * volume IDs for raid_dev created in this array
239 __u16 filler1
; /* 0x4E - 0x4F */
240 #define IMSM_FILLERS 34
241 __u32 filler
[IMSM_FILLERS
]; /* 0x50 - 0xD7 RAID_MPB_FILLERS */
242 struct imsm_disk disk
[1]; /* 0xD8 diskTbl[numDisks] */
243 /* here comes imsm_dev[num_raid_devs] */
244 /* here comes BBM logs */
245 } __attribute__ ((packed
));
247 #define BBM_LOG_MAX_ENTRIES 254
248 #define BBM_LOG_MAX_LBA_ENTRY_VAL 256 /* Represents 256 LBAs */
249 #define BBM_LOG_SIGNATURE 0xabadb10c
251 struct bbm_log_block_addr
{
254 } __attribute__ ((__packed__
));
256 struct bbm_log_entry
{
257 __u8 marked_count
; /* Number of blocks marked - 1 */
258 __u8 disk_ordinal
; /* Disk entry within the imsm_super */
259 struct bbm_log_block_addr defective_block_start
;
260 } __attribute__ ((__packed__
));
263 __u32 signature
; /* 0xABADB10C */
265 struct bbm_log_entry marked_block_entries
[BBM_LOG_MAX_ENTRIES
];
266 } __attribute__ ((__packed__
));
268 static char *map_state_str
[] = { "normal", "uninitialized", "degraded", "failed" };
270 #define BLOCKS_PER_KB (1024/512)
272 #define RAID_DISK_RESERVED_BLOCKS_IMSM_HI 2209
274 #define GEN_MIGR_AREA_SIZE 2048 /* General Migration Copy Area size in blocks */
276 #define MIGR_REC_BUF_SECTORS 1 /* size of migr_record i/o buffer in sectors */
277 #define MIGR_REC_SECTOR_POSITION 1 /* migr_record position offset on disk,
278 * MIGR_REC_BUF_SECTORS <= MIGR_REC_SECTOR_POS
281 #define UNIT_SRC_NORMAL 0 /* Source data for curr_migr_unit must
282 * be recovered using srcMap */
283 #define UNIT_SRC_IN_CP_AREA 1 /* Source data for curr_migr_unit has
284 * already been migrated and must
285 * be recovered from checkpoint area */
287 #define PPL_ENTRY_SPACE (128 * 1024) /* Size of the PPL, without the header */
290 __u32 rec_status
; /* Status used to determine how to restart
291 * migration in case it aborts
293 __u32 curr_migr_unit
; /* 0..numMigrUnits-1 */
294 __u32 family_num
; /* Family number of MPB
295 * containing the RaidDev
296 * that is migrating */
297 __u32 ascending_migr
; /* True if migrating in increasing
299 __u32 blocks_per_unit
; /* Num disk blocks per unit of operation */
300 __u32 dest_depth_per_unit
; /* Num member blocks each destMap
302 * advances per unit-of-operation */
303 __u32 ckpt_area_pba
; /* Pba of first block of ckpt copy area */
304 __u32 dest_1st_member_lba
; /* First member lba on first
305 * stripe of destination */
306 __u32 num_migr_units
; /* Total num migration units-of-op */
307 __u32 post_migr_vol_cap
; /* Size of volume after
308 * migration completes */
309 __u32 post_migr_vol_cap_hi
; /* Expansion space for LBA64 */
310 __u32 ckpt_read_disk_num
; /* Which member disk in destSubMap[0] the
311 * migration ckpt record was read from
312 * (for recovered migrations) */
313 } __attribute__ ((__packed__
));
318 * 2: metadata does not match
326 struct md_list
*next
;
329 #define pr_vrb(fmt, arg...) (void) (verbose && pr_err(fmt, ##arg))
331 static __u8
migr_type(struct imsm_dev
*dev
)
333 if (dev
->vol
.migr_type
== MIGR_VERIFY
&&
334 dev
->status
& DEV_VERIFY_AND_FIX
)
337 return dev
->vol
.migr_type
;
340 static void set_migr_type(struct imsm_dev
*dev
, __u8 migr_type
)
342 /* for compatibility with older oroms convert MIGR_REPAIR, into
343 * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
345 if (migr_type
== MIGR_REPAIR
) {
346 dev
->vol
.migr_type
= MIGR_VERIFY
;
347 dev
->status
|= DEV_VERIFY_AND_FIX
;
349 dev
->vol
.migr_type
= migr_type
;
350 dev
->status
&= ~DEV_VERIFY_AND_FIX
;
354 static unsigned int sector_count(__u32 bytes
, unsigned int sector_size
)
356 return ROUND_UP(bytes
, sector_size
) / sector_size
;
359 static unsigned int mpb_sectors(struct imsm_super
*mpb
,
360 unsigned int sector_size
)
362 return sector_count(__le32_to_cpu(mpb
->mpb_size
), sector_size
);
366 struct imsm_dev
*dev
;
367 struct intel_dev
*next
;
372 enum sys_dev_type type
;
375 struct intel_hba
*next
;
382 /* internal representation of IMSM metadata */
385 void *buf
; /* O_DIRECT buffer for reading/writing metadata */
386 struct imsm_super
*anchor
; /* immovable parameters */
389 void *migr_rec_buf
; /* buffer for I/O operations */
390 struct migr_record
*migr_rec
; /* migration record */
392 int clean_migration_record_by_mdmon
; /* when reshape is switched to next
393 array, it indicates that mdmon is allowed to clean migration
395 size_t len
; /* size of the 'buf' allocation */
396 size_t extra_space
; /* extra space in 'buf' that is not used yet */
397 void *next_buf
; /* for realloc'ing buf from the manager */
399 int updates_pending
; /* count of pending updates for mdmon */
400 int current_vol
; /* index of raid device undergoing creation */
401 unsigned long long create_offset
; /* common start for 'current_vol' */
402 __u32 random
; /* random data for seeding new family numbers */
403 struct intel_dev
*devlist
;
404 unsigned int sector_size
; /* sector size of used member drives */
408 __u8 serial
[MAX_RAID_SERIAL_LEN
];
411 struct imsm_disk disk
;
414 struct extent
*e
; /* for determining freespace @ create */
415 int raiddisk
; /* slot to fill in autolayout */
417 } *disks
, *current_disk
;
418 struct dl
*disk_mgmt_list
; /* list of disks to add/remove while mdmon
420 struct dl
*missing
; /* disks removed while we weren't looking */
421 struct bbm_log
*bbm_log
;
422 struct intel_hba
*hba
; /* device path of the raid controller for this metadata */
423 const struct imsm_orom
*orom
; /* platform firmware support */
424 struct intel_super
*next
; /* (temp) list for disambiguating family_num */
425 struct md_bb bb
; /* memory for get_bad_blocks call */
429 struct imsm_disk disk
;
430 #define IMSM_UNKNOWN_OWNER (-1)
432 struct intel_disk
*next
;
436 unsigned long long start
, size
;
439 /* definitions of reshape process types */
440 enum imsm_reshape_type
{
446 /* definition of messages passed to imsm_process_update */
447 enum imsm_update_type
{
448 update_activate_spare
,
452 update_add_remove_disk
,
453 update_reshape_container_disks
,
454 update_reshape_migration
,
456 update_general_migration_checkpoint
,
458 update_prealloc_badblocks_mem
,
462 struct imsm_update_activate_spare
{
463 enum imsm_update_type type
;
467 struct imsm_update_activate_spare
*next
;
473 unsigned long long size
;
480 enum takeover_direction
{
484 struct imsm_update_takeover
{
485 enum imsm_update_type type
;
487 enum takeover_direction direction
;
490 struct imsm_update_reshape
{
491 enum imsm_update_type type
;
495 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
498 struct imsm_update_reshape_migration
{
499 enum imsm_update_type type
;
502 /* fields for array migration changes
509 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
512 struct imsm_update_size_change
{
513 enum imsm_update_type type
;
518 struct imsm_update_general_migration_checkpoint
{
519 enum imsm_update_type type
;
520 __u32 curr_migr_unit
;
524 __u8 serial
[MAX_RAID_SERIAL_LEN
];
527 struct imsm_update_create_array
{
528 enum imsm_update_type type
;
533 struct imsm_update_kill_array
{
534 enum imsm_update_type type
;
538 struct imsm_update_rename_array
{
539 enum imsm_update_type type
;
540 __u8 name
[MAX_RAID_SERIAL_LEN
];
544 struct imsm_update_add_remove_disk
{
545 enum imsm_update_type type
;
548 struct imsm_update_prealloc_bb_mem
{
549 enum imsm_update_type type
;
552 struct imsm_update_rwh_policy
{
553 enum imsm_update_type type
;
558 static const char *_sys_dev_type
[] = {
559 [SYS_DEV_UNKNOWN
] = "Unknown",
560 [SYS_DEV_SAS
] = "SAS",
561 [SYS_DEV_SATA
] = "SATA",
562 [SYS_DEV_NVME
] = "NVMe",
563 [SYS_DEV_VMD
] = "VMD"
566 const char *get_sys_dev_type(enum sys_dev_type type
)
568 if (type
>= SYS_DEV_MAX
)
569 type
= SYS_DEV_UNKNOWN
;
571 return _sys_dev_type
[type
];
574 static struct intel_hba
* alloc_intel_hba(struct sys_dev
*device
)
576 struct intel_hba
*result
= xmalloc(sizeof(*result
));
578 result
->type
= device
->type
;
579 result
->path
= xstrdup(device
->path
);
581 if (result
->path
&& (result
->pci_id
= strrchr(result
->path
, '/')) != NULL
)
587 static struct intel_hba
* find_intel_hba(struct intel_hba
*hba
, struct sys_dev
*device
)
589 struct intel_hba
*result
;
591 for (result
= hba
; result
; result
= result
->next
) {
592 if (result
->type
== device
->type
&& strcmp(result
->path
, device
->path
) == 0)
598 static int attach_hba_to_super(struct intel_super
*super
, struct sys_dev
*device
)
600 struct intel_hba
*hba
;
602 /* check if disk attached to Intel HBA */
603 hba
= find_intel_hba(super
->hba
, device
);
606 /* Check if HBA is already attached to super */
607 if (super
->hba
== NULL
) {
608 super
->hba
= alloc_intel_hba(device
);
613 /* Intel metadata allows for all disks attached to the same type HBA.
614 * Do not support HBA types mixing
616 if (device
->type
!= hba
->type
)
619 /* Multiple same type HBAs can be used if they share the same OROM */
620 const struct imsm_orom
*device_orom
= get_orom_by_device_id(device
->dev_id
);
622 if (device_orom
!= super
->orom
)
628 hba
->next
= alloc_intel_hba(device
);
632 static struct sys_dev
* find_disk_attached_hba(int fd
, const char *devname
)
634 struct sys_dev
*list
, *elem
;
637 if ((list
= find_intel_devices()) == NULL
)
641 disk_path
= (char *) devname
;
643 disk_path
= diskfd_to_devpath(fd
);
648 for (elem
= list
; elem
; elem
= elem
->next
)
649 if (path_attached_to_hba(disk_path
, elem
->path
))
652 if (disk_path
!= devname
)
658 static int find_intel_hba_capability(int fd
, struct intel_super
*super
,
661 static struct supertype
*match_metadata_desc_imsm(char *arg
)
663 struct supertype
*st
;
665 if (strcmp(arg
, "imsm") != 0 &&
666 strcmp(arg
, "default") != 0
670 st
= xcalloc(1, sizeof(*st
));
671 st
->ss
= &super_imsm
;
672 st
->max_devs
= IMSM_MAX_DEVICES
;
673 st
->minor_version
= 0;
678 static __u8
*get_imsm_version(struct imsm_super
*mpb
)
680 return &mpb
->sig
[MPB_SIG_LEN
];
683 /* retrieve a disk directly from the anchor when the anchor is known to be
684 * up-to-date, currently only at load time
686 static struct imsm_disk
*__get_imsm_disk(struct imsm_super
*mpb
, __u8 index
)
688 if (index
>= mpb
->num_disks
)
690 return &mpb
->disk
[index
];
693 /* retrieve the disk description based on a index of the disk
696 static struct dl
*get_imsm_dl_disk(struct intel_super
*super
, __u8 index
)
700 for (d
= super
->disks
; d
; d
= d
->next
)
701 if (d
->index
== index
)
706 /* retrieve a disk from the parsed metadata */
707 static struct imsm_disk
*get_imsm_disk(struct intel_super
*super
, __u8 index
)
711 dl
= get_imsm_dl_disk(super
, index
);
718 /* generate a checksum directly from the anchor when the anchor is known to be
719 * up-to-date, currently only at load or write_super after coalescing
721 static __u32
__gen_imsm_checksum(struct imsm_super
*mpb
)
723 __u32 end
= mpb
->mpb_size
/ sizeof(end
);
724 __u32
*p
= (__u32
*) mpb
;
728 sum
+= __le32_to_cpu(*p
);
732 return sum
- __le32_to_cpu(mpb
->check_sum
);
735 static size_t sizeof_imsm_map(struct imsm_map
*map
)
737 return sizeof(struct imsm_map
) + sizeof(__u32
) * (map
->num_members
- 1);
740 struct imsm_map
*get_imsm_map(struct imsm_dev
*dev
, int second_map
)
742 /* A device can have 2 maps if it is in the middle of a migration.
744 * MAP_0 - we return the first map
745 * MAP_1 - we return the second map if it exists, else NULL
746 * MAP_X - we return the second map if it exists, else the first
748 struct imsm_map
*map
= &dev
->vol
.map
[0];
749 struct imsm_map
*map2
= NULL
;
751 if (dev
->vol
.migr_state
)
752 map2
= (void *)map
+ sizeof_imsm_map(map
);
754 switch (second_map
) {
771 /* return the size of the device.
772 * migr_state increases the returned size if map[0] were to be duplicated
774 static size_t sizeof_imsm_dev(struct imsm_dev
*dev
, int migr_state
)
776 size_t size
= sizeof(*dev
) - sizeof(struct imsm_map
) +
777 sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
779 /* migrating means an additional map */
780 if (dev
->vol
.migr_state
)
781 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_1
));
783 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
788 /* retrieve disk serial number list from a metadata update */
789 static struct disk_info
*get_disk_info(struct imsm_update_create_array
*update
)
792 struct disk_info
*inf
;
794 inf
= u
+ sizeof(*update
) - sizeof(struct imsm_dev
) +
795 sizeof_imsm_dev(&update
->dev
, 0);
800 static struct imsm_dev
*__get_imsm_dev(struct imsm_super
*mpb
, __u8 index
)
806 if (index
>= mpb
->num_raid_devs
)
809 /* devices start after all disks */
810 offset
= ((void *) &mpb
->disk
[mpb
->num_disks
]) - _mpb
;
812 for (i
= 0; i
<= index
; i
++)
814 return _mpb
+ offset
;
816 offset
+= sizeof_imsm_dev(_mpb
+ offset
, 0);
821 static struct imsm_dev
*get_imsm_dev(struct intel_super
*super
, __u8 index
)
823 struct intel_dev
*dv
;
825 if (index
>= super
->anchor
->num_raid_devs
)
827 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
828 if (dv
->index
== index
)
833 static inline unsigned long long __le48_to_cpu(const struct bbm_log_block_addr
836 return ((((__u64
)__le32_to_cpu(addr
->dw1
)) << 16) |
837 __le16_to_cpu(addr
->w1
));
840 static inline struct bbm_log_block_addr
__cpu_to_le48(unsigned long long sec
)
842 struct bbm_log_block_addr addr
;
844 addr
.w1
= __cpu_to_le16((__u16
)(sec
& 0xffff));
845 addr
.dw1
= __cpu_to_le32((__u32
)(sec
>> 16) & 0xffffffff);
849 /* get size of the bbm log */
850 static __u32
get_imsm_bbm_log_size(struct bbm_log
*log
)
852 if (!log
|| log
->entry_count
== 0)
855 return sizeof(log
->signature
) +
856 sizeof(log
->entry_count
) +
857 log
->entry_count
* sizeof(struct bbm_log_entry
);
860 /* check if bad block is not partially stored in bbm log */
861 static int is_stored_in_bbm(struct bbm_log
*log
, const __u8 idx
, const unsigned
862 long long sector
, const int length
, __u32
*pos
)
866 for (i
= *pos
; i
< log
->entry_count
; i
++) {
867 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[i
];
868 unsigned long long bb_start
;
869 unsigned long long bb_end
;
871 bb_start
= __le48_to_cpu(&entry
->defective_block_start
);
872 bb_end
= bb_start
+ (entry
->marked_count
+ 1);
874 if ((entry
->disk_ordinal
== idx
) && (bb_start
>= sector
) &&
875 (bb_end
<= sector
+ length
)) {
883 /* record new bad block in bbm log */
884 static int record_new_badblock(struct bbm_log
*log
, const __u8 idx
, unsigned
885 long long sector
, int length
)
889 struct bbm_log_entry
*entry
= NULL
;
891 while (is_stored_in_bbm(log
, idx
, sector
, length
, &pos
)) {
892 struct bbm_log_entry
*e
= &log
->marked_block_entries
[pos
];
894 if ((e
->marked_count
+ 1 == BBM_LOG_MAX_LBA_ENTRY_VAL
) &&
895 (__le48_to_cpu(&e
->defective_block_start
) == sector
)) {
896 sector
+= BBM_LOG_MAX_LBA_ENTRY_VAL
;
897 length
-= BBM_LOG_MAX_LBA_ENTRY_VAL
;
906 int cnt
= (length
<= BBM_LOG_MAX_LBA_ENTRY_VAL
) ? length
:
907 BBM_LOG_MAX_LBA_ENTRY_VAL
;
908 entry
->defective_block_start
= __cpu_to_le48(sector
);
909 entry
->marked_count
= cnt
- 1;
916 new_bb
= ROUND_UP(length
, BBM_LOG_MAX_LBA_ENTRY_VAL
) /
917 BBM_LOG_MAX_LBA_ENTRY_VAL
;
918 if (log
->entry_count
+ new_bb
> BBM_LOG_MAX_ENTRIES
)
922 int cnt
= (length
<= BBM_LOG_MAX_LBA_ENTRY_VAL
) ? length
:
923 BBM_LOG_MAX_LBA_ENTRY_VAL
;
924 struct bbm_log_entry
*entry
=
925 &log
->marked_block_entries
[log
->entry_count
];
927 entry
->defective_block_start
= __cpu_to_le48(sector
);
928 entry
->marked_count
= cnt
- 1;
929 entry
->disk_ordinal
= idx
;
940 /* clear all bad blocks for given disk */
941 static void clear_disk_badblocks(struct bbm_log
*log
, const __u8 idx
)
945 while (i
< log
->entry_count
) {
946 struct bbm_log_entry
*entries
= log
->marked_block_entries
;
948 if (entries
[i
].disk_ordinal
== idx
) {
949 if (i
< log
->entry_count
- 1)
950 entries
[i
] = entries
[log
->entry_count
- 1];
958 /* clear given bad block */
959 static int clear_badblock(struct bbm_log
*log
, const __u8 idx
, const unsigned
960 long long sector
, const int length
) {
963 while (i
< log
->entry_count
) {
964 struct bbm_log_entry
*entries
= log
->marked_block_entries
;
966 if ((entries
[i
].disk_ordinal
== idx
) &&
967 (__le48_to_cpu(&entries
[i
].defective_block_start
) ==
968 sector
) && (entries
[i
].marked_count
+ 1 == length
)) {
969 if (i
< log
->entry_count
- 1)
970 entries
[i
] = entries
[log
->entry_count
- 1];
980 /* allocate and load BBM log from metadata */
981 static int load_bbm_log(struct intel_super
*super
)
983 struct imsm_super
*mpb
= super
->anchor
;
984 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
986 super
->bbm_log
= xcalloc(1, sizeof(struct bbm_log
));
991 struct bbm_log
*log
= (void *)mpb
+
992 __le32_to_cpu(mpb
->mpb_size
) - bbm_log_size
;
996 if (bbm_log_size
< sizeof(log
->signature
) +
997 sizeof(log
->entry_count
))
1000 entry_count
= __le32_to_cpu(log
->entry_count
);
1001 if ((__le32_to_cpu(log
->signature
) != BBM_LOG_SIGNATURE
) ||
1002 (entry_count
> BBM_LOG_MAX_ENTRIES
))
1006 sizeof(log
->signature
) + sizeof(log
->entry_count
) +
1007 entry_count
* sizeof(struct bbm_log_entry
))
1010 memcpy(super
->bbm_log
, log
, bbm_log_size
);
1012 super
->bbm_log
->signature
= __cpu_to_le32(BBM_LOG_SIGNATURE
);
1013 super
->bbm_log
->entry_count
= 0;
1019 /* checks if bad block is within volume boundaries */
1020 static int is_bad_block_in_volume(const struct bbm_log_entry
*entry
,
1021 const unsigned long long start_sector
,
1022 const unsigned long long size
)
1024 unsigned long long bb_start
;
1025 unsigned long long bb_end
;
1027 bb_start
= __le48_to_cpu(&entry
->defective_block_start
);
1028 bb_end
= bb_start
+ (entry
->marked_count
+ 1);
1030 if (((bb_start
>= start_sector
) && (bb_start
< start_sector
+ size
)) ||
1031 ((bb_end
>= start_sector
) && (bb_end
<= start_sector
+ size
)))
1037 /* get list of bad blocks on a drive for a volume */
1038 static void get_volume_badblocks(const struct bbm_log
*log
, const __u8 idx
,
1039 const unsigned long long start_sector
,
1040 const unsigned long long size
,
1046 for (i
= 0; i
< log
->entry_count
; i
++) {
1047 const struct bbm_log_entry
*ent
=
1048 &log
->marked_block_entries
[i
];
1049 struct md_bb_entry
*bb
;
1051 if ((ent
->disk_ordinal
== idx
) &&
1052 is_bad_block_in_volume(ent
, start_sector
, size
)) {
1054 if (!bbs
->entries
) {
1055 bbs
->entries
= xmalloc(BBM_LOG_MAX_ENTRIES
*
1061 bb
= &bbs
->entries
[count
++];
1062 bb
->sector
= __le48_to_cpu(&ent
->defective_block_start
);
1063 bb
->length
= ent
->marked_count
+ 1;
1071 * == MAP_0 get first map
1072 * == MAP_1 get second map
1073 * == MAP_X than get map according to the current migr_state
1075 static __u32
get_imsm_ord_tbl_ent(struct imsm_dev
*dev
,
1079 struct imsm_map
*map
;
1081 map
= get_imsm_map(dev
, second_map
);
1083 /* top byte identifies disk under rebuild */
1084 return __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
1087 #define ord_to_idx(ord) (((ord) << 8) >> 8)
1088 static __u32
get_imsm_disk_idx(struct imsm_dev
*dev
, int slot
, int second_map
)
1090 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, second_map
);
1092 return ord_to_idx(ord
);
1095 static void set_imsm_ord_tbl_ent(struct imsm_map
*map
, int slot
, __u32 ord
)
1097 map
->disk_ord_tbl
[slot
] = __cpu_to_le32(ord
);
1100 static int get_imsm_disk_slot(struct imsm_map
*map
, unsigned idx
)
1105 for (slot
= 0; slot
< map
->num_members
; slot
++) {
1106 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
1107 if (ord_to_idx(ord
) == idx
)
1114 static int get_imsm_raid_level(struct imsm_map
*map
)
1116 if (map
->raid_level
== 1) {
1117 if (map
->num_members
== 2)
1123 return map
->raid_level
;
1126 static int cmp_extent(const void *av
, const void *bv
)
1128 const struct extent
*a
= av
;
1129 const struct extent
*b
= bv
;
1130 if (a
->start
< b
->start
)
1132 if (a
->start
> b
->start
)
1137 static int count_memberships(struct dl
*dl
, struct intel_super
*super
)
1139 int memberships
= 0;
1142 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1143 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1144 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1146 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
1153 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
);
1155 static int split_ull(unsigned long long n
, __u32
*lo
, __u32
*hi
)
1157 if (lo
== 0 || hi
== 0)
1159 *lo
= __le32_to_cpu((unsigned)n
);
1160 *hi
= __le32_to_cpu((unsigned)(n
>> 32));
1164 static unsigned long long join_u32(__u32 lo
, __u32 hi
)
1166 return (unsigned long long)__le32_to_cpu(lo
) |
1167 (((unsigned long long)__le32_to_cpu(hi
)) << 32);
1170 static unsigned long long total_blocks(struct imsm_disk
*disk
)
1174 return join_u32(disk
->total_blocks_lo
, disk
->total_blocks_hi
);
1177 static unsigned long long pba_of_lba0(struct imsm_map
*map
)
1181 return join_u32(map
->pba_of_lba0_lo
, map
->pba_of_lba0_hi
);
1184 static unsigned long long blocks_per_member(struct imsm_map
*map
)
1188 return join_u32(map
->blocks_per_member_lo
, map
->blocks_per_member_hi
);
1191 static unsigned long long num_data_stripes(struct imsm_map
*map
)
1195 return join_u32(map
->num_data_stripes_lo
, map
->num_data_stripes_hi
);
1198 static void set_total_blocks(struct imsm_disk
*disk
, unsigned long long n
)
1200 split_ull(n
, &disk
->total_blocks_lo
, &disk
->total_blocks_hi
);
1203 static void set_pba_of_lba0(struct imsm_map
*map
, unsigned long long n
)
1205 split_ull(n
, &map
->pba_of_lba0_lo
, &map
->pba_of_lba0_hi
);
1208 static void set_blocks_per_member(struct imsm_map
*map
, unsigned long long n
)
1210 split_ull(n
, &map
->blocks_per_member_lo
, &map
->blocks_per_member_hi
);
1213 static void set_num_data_stripes(struct imsm_map
*map
, unsigned long long n
)
1215 split_ull(n
, &map
->num_data_stripes_lo
, &map
->num_data_stripes_hi
);
1218 static struct extent
*get_extents(struct intel_super
*super
, struct dl
*dl
)
1220 /* find a list of used extents on the given physical device */
1221 struct extent
*rv
, *e
;
1223 int memberships
= count_memberships(dl
, super
);
1226 /* trim the reserved area for spares, so they can join any array
1227 * regardless of whether the OROM has assigned sectors from the
1228 * IMSM_RESERVED_SECTORS region
1230 if (dl
->index
== -1)
1231 reservation
= imsm_min_reserved_sectors(super
);
1233 reservation
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1235 rv
= xcalloc(sizeof(struct extent
), (memberships
+ 1));
1238 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1239 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1240 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1242 if (get_imsm_disk_slot(map
, dl
->index
) >= 0) {
1243 e
->start
= pba_of_lba0(map
);
1244 e
->size
= blocks_per_member(map
);
1248 qsort(rv
, memberships
, sizeof(*rv
), cmp_extent
);
1250 /* determine the start of the metadata
1251 * when no raid devices are defined use the default
1252 * ...otherwise allow the metadata to truncate the value
1253 * as is the case with older versions of imsm
1256 struct extent
*last
= &rv
[memberships
- 1];
1257 unsigned long long remainder
;
1259 remainder
= total_blocks(&dl
->disk
) - (last
->start
+ last
->size
);
1260 /* round down to 1k block to satisfy precision of the kernel
1264 /* make sure remainder is still sane */
1265 if (remainder
< (unsigned)ROUND_UP(super
->len
, 512) >> 9)
1266 remainder
= ROUND_UP(super
->len
, 512) >> 9;
1267 if (reservation
> remainder
)
1268 reservation
= remainder
;
1270 e
->start
= total_blocks(&dl
->disk
) - reservation
;
1275 /* try to determine how much space is reserved for metadata from
1276 * the last get_extents() entry, otherwise fallback to the
1279 static __u32
imsm_reserved_sectors(struct intel_super
*super
, struct dl
*dl
)
1285 /* for spares just return a minimal reservation which will grow
1286 * once the spare is picked up by an array
1288 if (dl
->index
== -1)
1289 return MPB_SECTOR_CNT
;
1291 e
= get_extents(super
, dl
);
1293 return MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1295 /* scroll to last entry */
1296 for (i
= 0; e
[i
].size
; i
++)
1299 rv
= total_blocks(&dl
->disk
) - e
[i
].start
;
1306 static int is_spare(struct imsm_disk
*disk
)
1308 return (disk
->status
& SPARE_DISK
) == SPARE_DISK
;
1311 static int is_configured(struct imsm_disk
*disk
)
1313 return (disk
->status
& CONFIGURED_DISK
) == CONFIGURED_DISK
;
1316 static int is_failed(struct imsm_disk
*disk
)
1318 return (disk
->status
& FAILED_DISK
) == FAILED_DISK
;
1321 static int is_journal(struct imsm_disk
*disk
)
1323 return (disk
->status
& JOURNAL_DISK
) == JOURNAL_DISK
;
1326 /* round array size down to closest MB and ensure it splits evenly
1329 static unsigned long long round_size_to_mb(unsigned long long size
, unsigned int
1333 size
= (size
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
1339 /* try to determine how much space is reserved for metadata from
1340 * the last get_extents() entry on the smallest active disk,
1341 * otherwise fallback to the default
1343 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
)
1347 unsigned long long min_active
;
1349 __u32 rv
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1350 struct dl
*dl
, *dl_min
= NULL
;
1356 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
1359 unsigned long long blocks
= total_blocks(&dl
->disk
);
1360 if (blocks
< min_active
|| min_active
== 0) {
1362 min_active
= blocks
;
1368 /* find last lba used by subarrays on the smallest active disk */
1369 e
= get_extents(super
, dl_min
);
1372 for (i
= 0; e
[i
].size
; i
++)
1375 remainder
= min_active
- e
[i
].start
;
1378 /* to give priority to recovery we should not require full
1379 IMSM_RESERVED_SECTORS from the spare */
1380 rv
= MPB_SECTOR_CNT
+ NUM_BLOCKS_DIRTY_STRIPE_REGION
;
1382 /* if real reservation is smaller use that value */
1383 return (remainder
< rv
) ? remainder
: rv
;
1386 /* Return minimum size of a spare that can be used in this array*/
1387 static unsigned long long min_acceptable_spare_size_imsm(struct supertype
*st
)
1389 struct intel_super
*super
= st
->sb
;
1393 unsigned long long rv
= 0;
1397 /* find first active disk in array */
1399 while (dl
&& (is_failed(&dl
->disk
) || dl
->index
== -1))
1403 /* find last lba used by subarrays */
1404 e
= get_extents(super
, dl
);
1407 for (i
= 0; e
[i
].size
; i
++)
1410 rv
= e
[i
-1].start
+ e
[i
-1].size
;
1413 /* add the amount of space needed for metadata */
1414 rv
= rv
+ imsm_min_reserved_sectors(super
);
1419 static int is_gen_migration(struct imsm_dev
*dev
);
1421 #define IMSM_4K_DIV 8
1423 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
1424 struct imsm_dev
*dev
);
1426 static void print_imsm_dev(struct intel_super
*super
,
1427 struct imsm_dev
*dev
,
1433 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1434 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
1438 printf("[%.16s]:\n", dev
->volume
);
1439 printf(" UUID : %s\n", uuid
);
1440 printf(" RAID Level : %d", get_imsm_raid_level(map
));
1442 printf(" <-- %d", get_imsm_raid_level(map2
));
1444 printf(" Members : %d", map
->num_members
);
1446 printf(" <-- %d", map2
->num_members
);
1448 printf(" Slots : [");
1449 for (i
= 0; i
< map
->num_members
; i
++) {
1450 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
1451 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1456 for (i
= 0; i
< map2
->num_members
; i
++) {
1457 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_1
);
1458 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1463 printf(" Failed disk : ");
1464 if (map
->failed_disk_num
== 0xff)
1467 printf("%i", map
->failed_disk_num
);
1469 slot
= get_imsm_disk_slot(map
, disk_idx
);
1471 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
1472 printf(" This Slot : %d%s\n", slot
,
1473 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
1475 printf(" This Slot : ?\n");
1476 sz
= __le32_to_cpu(dev
->size_high
);
1478 sz
+= __le32_to_cpu(dev
->size_low
);
1479 printf(" Array Size : %llu%s\n", (unsigned long long)sz
,
1480 human_size(sz
* 512));
1481 sz
= blocks_per_member(map
);
1482 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz
,
1483 human_size(sz
* 512));
1484 printf(" Sector Offset : %llu\n",
1486 printf(" Num Stripes : %llu\n",
1487 num_data_stripes(map
));
1488 printf(" Chunk Size : %u KiB",
1489 __le16_to_cpu(map
->blocks_per_strip
) / 2);
1491 printf(" <-- %u KiB",
1492 __le16_to_cpu(map2
->blocks_per_strip
) / 2);
1494 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
1495 printf(" Migrate State : ");
1496 if (dev
->vol
.migr_state
) {
1497 if (migr_type(dev
) == MIGR_INIT
)
1498 printf("initialize\n");
1499 else if (migr_type(dev
) == MIGR_REBUILD
)
1500 printf("rebuild\n");
1501 else if (migr_type(dev
) == MIGR_VERIFY
)
1503 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
1504 printf("general migration\n");
1505 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
1506 printf("state change\n");
1507 else if (migr_type(dev
) == MIGR_REPAIR
)
1510 printf("<unknown:%d>\n", migr_type(dev
));
1513 printf(" Map State : %s", map_state_str
[map
->map_state
]);
1514 if (dev
->vol
.migr_state
) {
1515 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
1517 printf(" <-- %s", map_state_str
[map
->map_state
]);
1518 printf("\n Checkpoint : %u ",
1519 __le32_to_cpu(dev
->vol
.curr_migr_unit
));
1520 if (is_gen_migration(dev
) && (slot
> 1 || slot
< 0))
1523 printf("(%llu)", (unsigned long long)
1524 blocks_per_migr_unit(super
, dev
));
1527 printf(" Dirty State : %s\n", (dev
->vol
.dirty
& RAIDVOL_DIRTY
) ?
1529 printf(" RWH Policy : ");
1530 if (dev
->rwh_policy
== RWH_OFF
)
1532 else if (dev
->rwh_policy
== RWH_DISTRIBUTED
)
1533 printf("PPL distributed\n");
1534 else if (dev
->rwh_policy
== RWH_JOURNALING_DRIVE
)
1535 printf("PPL journaling drive\n");
1537 printf("<unknown:%d>\n", dev
->rwh_policy
);
1540 static void print_imsm_disk(struct imsm_disk
*disk
,
1543 unsigned int sector_size
) {
1544 char str
[MAX_RAID_SERIAL_LEN
+ 1];
1547 if (index
< -1 || !disk
)
1551 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
1553 printf(" Disk%02d Serial : %s\n", index
, str
);
1555 printf(" Disk Serial : %s\n", str
);
1556 printf(" State :%s%s%s%s\n", is_spare(disk
) ? " spare" : "",
1557 is_configured(disk
) ? " active" : "",
1558 is_failed(disk
) ? " failed" : "",
1559 is_journal(disk
) ? " journal" : "");
1560 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
1561 sz
= total_blocks(disk
) - reserved
;
1562 printf(" Usable Size : %llu%s\n",
1563 (unsigned long long)sz
* 512 / sector_size
,
1564 human_size(sz
* 512));
1567 void convert_to_4k_imsm_migr_rec(struct intel_super
*super
)
1569 struct migr_record
*migr_rec
= super
->migr_rec
;
1571 migr_rec
->blocks_per_unit
/= IMSM_4K_DIV
;
1572 migr_rec
->ckpt_area_pba
/= IMSM_4K_DIV
;
1573 migr_rec
->dest_1st_member_lba
/= IMSM_4K_DIV
;
1574 migr_rec
->dest_depth_per_unit
/= IMSM_4K_DIV
;
1575 split_ull((join_u32(migr_rec
->post_migr_vol_cap
,
1576 migr_rec
->post_migr_vol_cap_hi
) / IMSM_4K_DIV
),
1577 &migr_rec
->post_migr_vol_cap
, &migr_rec
->post_migr_vol_cap_hi
);
1580 void convert_to_4k_imsm_disk(struct imsm_disk
*disk
)
1582 set_total_blocks(disk
, (total_blocks(disk
)/IMSM_4K_DIV
));
1585 void convert_to_4k(struct intel_super
*super
)
1587 struct imsm_super
*mpb
= super
->anchor
;
1588 struct imsm_disk
*disk
;
1590 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
1592 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1593 disk
= __get_imsm_disk(mpb
, i
);
1595 convert_to_4k_imsm_disk(disk
);
1597 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1598 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1599 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1601 split_ull((join_u32(dev
->size_low
, dev
->size_high
)/IMSM_4K_DIV
),
1602 &dev
->size_low
, &dev
->size_high
);
1603 dev
->vol
.curr_migr_unit
/= IMSM_4K_DIV
;
1606 set_blocks_per_member(map
, blocks_per_member(map
)/IMSM_4K_DIV
);
1607 map
->blocks_per_strip
/= IMSM_4K_DIV
;
1608 set_pba_of_lba0(map
, pba_of_lba0(map
)/IMSM_4K_DIV
);
1610 if (dev
->vol
.migr_state
) {
1612 map
= get_imsm_map(dev
, MAP_1
);
1613 set_blocks_per_member(map
,
1614 blocks_per_member(map
)/IMSM_4K_DIV
);
1615 map
->blocks_per_strip
/= IMSM_4K_DIV
;
1616 set_pba_of_lba0(map
, pba_of_lba0(map
)/IMSM_4K_DIV
);
1620 struct bbm_log
*log
= (void *)mpb
+
1621 __le32_to_cpu(mpb
->mpb_size
) - bbm_log_size
;
1624 for (i
= 0; i
< log
->entry_count
; i
++) {
1625 struct bbm_log_entry
*entry
=
1626 &log
->marked_block_entries
[i
];
1628 __u8 count
= entry
->marked_count
+ 1;
1629 unsigned long long sector
=
1630 __le48_to_cpu(&entry
->defective_block_start
);
1632 entry
->defective_block_start
=
1633 __cpu_to_le48(sector
/IMSM_4K_DIV
);
1634 entry
->marked_count
= max(count
/IMSM_4K_DIV
, 1) - 1;
1638 mpb
->check_sum
= __gen_imsm_checksum(mpb
);
1641 void examine_migr_rec_imsm(struct intel_super
*super
)
1643 struct migr_record
*migr_rec
= super
->migr_rec
;
1644 struct imsm_super
*mpb
= super
->anchor
;
1647 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1648 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1649 struct imsm_map
*map
;
1652 if (is_gen_migration(dev
) == 0)
1655 printf("\nMigration Record Information:");
1657 /* first map under migration */
1658 map
= get_imsm_map(dev
, MAP_0
);
1660 slot
= get_imsm_disk_slot(map
, super
->disks
->index
);
1661 if (map
== NULL
|| slot
> 1 || slot
< 0) {
1662 printf(" Empty\n ");
1663 printf("Examine one of first two disks in array\n");
1666 printf("\n Status : ");
1667 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
1670 printf("Contains Data\n");
1671 printf(" Current Unit : %u\n",
1672 __le32_to_cpu(migr_rec
->curr_migr_unit
));
1673 printf(" Family : %u\n",
1674 __le32_to_cpu(migr_rec
->family_num
));
1675 printf(" Ascending : %u\n",
1676 __le32_to_cpu(migr_rec
->ascending_migr
));
1677 printf(" Blocks Per Unit : %u\n",
1678 __le32_to_cpu(migr_rec
->blocks_per_unit
));
1679 printf(" Dest. Depth Per Unit : %u\n",
1680 __le32_to_cpu(migr_rec
->dest_depth_per_unit
));
1681 printf(" Checkpoint Area pba : %u\n",
1682 __le32_to_cpu(migr_rec
->ckpt_area_pba
));
1683 printf(" First member lba : %u\n",
1684 __le32_to_cpu(migr_rec
->dest_1st_member_lba
));
1685 printf(" Total Number of Units : %u\n",
1686 __le32_to_cpu(migr_rec
->num_migr_units
));
1687 printf(" Size of volume : %u\n",
1688 __le32_to_cpu(migr_rec
->post_migr_vol_cap
));
1689 printf(" Expansion space for LBA64 : %u\n",
1690 __le32_to_cpu(migr_rec
->post_migr_vol_cap_hi
));
1691 printf(" Record was read from : %u\n",
1692 __le32_to_cpu(migr_rec
->ckpt_read_disk_num
));
1698 void convert_from_4k_imsm_migr_rec(struct intel_super
*super
)
1700 struct migr_record
*migr_rec
= super
->migr_rec
;
1702 migr_rec
->blocks_per_unit
*= IMSM_4K_DIV
;
1703 migr_rec
->ckpt_area_pba
*= IMSM_4K_DIV
;
1704 migr_rec
->dest_1st_member_lba
*= IMSM_4K_DIV
;
1705 migr_rec
->dest_depth_per_unit
*= IMSM_4K_DIV
;
1706 split_ull((join_u32(migr_rec
->post_migr_vol_cap
,
1707 migr_rec
->post_migr_vol_cap_hi
) * IMSM_4K_DIV
),
1708 &migr_rec
->post_migr_vol_cap
,
1709 &migr_rec
->post_migr_vol_cap_hi
);
1712 void convert_from_4k(struct intel_super
*super
)
1714 struct imsm_super
*mpb
= super
->anchor
;
1715 struct imsm_disk
*disk
;
1717 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
1719 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1720 disk
= __get_imsm_disk(mpb
, i
);
1722 set_total_blocks(disk
, (total_blocks(disk
)*IMSM_4K_DIV
));
1725 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1726 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1727 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1729 split_ull((join_u32(dev
->size_low
, dev
->size_high
)*IMSM_4K_DIV
),
1730 &dev
->size_low
, &dev
->size_high
);
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
= count
*IMSM_4K_DIV
- 1;
1766 mpb
->check_sum
= __gen_imsm_checksum(mpb
);
1769 /*******************************************************************************
1770 * function: imsm_check_attributes
1771 * Description: Function checks if features represented by attributes flags
1772 * are supported by mdadm.
1774 * attributes - Attributes read from metadata
1776 * 0 - passed attributes contains unsupported features flags
1777 * 1 - all features are supported
1778 ******************************************************************************/
1779 static int imsm_check_attributes(__u32 attributes
)
1782 __u32 not_supported
= MPB_ATTRIB_SUPPORTED
^0xffffffff;
1784 not_supported
&= ~MPB_ATTRIB_IGNORED
;
1786 not_supported
&= attributes
;
1787 if (not_supported
) {
1788 pr_err("(IMSM): Unsupported attributes : %x\n",
1789 (unsigned)__le32_to_cpu(not_supported
));
1790 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1791 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
1792 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1794 if (not_supported
& MPB_ATTRIB_2TB
) {
1795 dprintf("\t\tMPB_ATTRIB_2TB\n");
1796 not_supported
^= MPB_ATTRIB_2TB
;
1798 if (not_supported
& MPB_ATTRIB_RAID0
) {
1799 dprintf("\t\tMPB_ATTRIB_RAID0\n");
1800 not_supported
^= MPB_ATTRIB_RAID0
;
1802 if (not_supported
& MPB_ATTRIB_RAID1
) {
1803 dprintf("\t\tMPB_ATTRIB_RAID1\n");
1804 not_supported
^= MPB_ATTRIB_RAID1
;
1806 if (not_supported
& MPB_ATTRIB_RAID10
) {
1807 dprintf("\t\tMPB_ATTRIB_RAID10\n");
1808 not_supported
^= MPB_ATTRIB_RAID10
;
1810 if (not_supported
& MPB_ATTRIB_RAID1E
) {
1811 dprintf("\t\tMPB_ATTRIB_RAID1E\n");
1812 not_supported
^= MPB_ATTRIB_RAID1E
;
1814 if (not_supported
& MPB_ATTRIB_RAID5
) {
1815 dprintf("\t\tMPB_ATTRIB_RAID5\n");
1816 not_supported
^= MPB_ATTRIB_RAID5
;
1818 if (not_supported
& MPB_ATTRIB_RAIDCNG
) {
1819 dprintf("\t\tMPB_ATTRIB_RAIDCNG\n");
1820 not_supported
^= MPB_ATTRIB_RAIDCNG
;
1822 if (not_supported
& MPB_ATTRIB_BBM
) {
1823 dprintf("\t\tMPB_ATTRIB_BBM\n");
1824 not_supported
^= MPB_ATTRIB_BBM
;
1826 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1827 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n");
1828 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1830 if (not_supported
& MPB_ATTRIB_EXP_STRIPE_SIZE
) {
1831 dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n");
1832 not_supported
^= MPB_ATTRIB_EXP_STRIPE_SIZE
;
1834 if (not_supported
& MPB_ATTRIB_2TB_DISK
) {
1835 dprintf("\t\tMPB_ATTRIB_2TB_DISK\n");
1836 not_supported
^= MPB_ATTRIB_2TB_DISK
;
1838 if (not_supported
& MPB_ATTRIB_NEVER_USE2
) {
1839 dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n");
1840 not_supported
^= MPB_ATTRIB_NEVER_USE2
;
1842 if (not_supported
& MPB_ATTRIB_NEVER_USE
) {
1843 dprintf("\t\tMPB_ATTRIB_NEVER_USE\n");
1844 not_supported
^= MPB_ATTRIB_NEVER_USE
;
1848 dprintf("(IMSM): Unknown attributes : %x\n", not_supported
);
1856 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
);
1858 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
1860 struct intel_super
*super
= st
->sb
;
1861 struct imsm_super
*mpb
= super
->anchor
;
1862 char str
[MAX_SIGNATURE_LENGTH
];
1867 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
1870 strncpy(str
, (char *)mpb
->sig
, MPB_SIG_LEN
);
1871 str
[MPB_SIG_LEN
-1] = '\0';
1872 printf(" Magic : %s\n", str
);
1873 snprintf(str
, strlen(MPB_VERSION_RAID0
), "%s", get_imsm_version(mpb
));
1874 printf(" Version : %s\n", get_imsm_version(mpb
));
1875 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
1876 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
1877 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
1878 printf(" Attributes : ");
1879 if (imsm_check_attributes(mpb
->attributes
))
1880 printf("All supported\n");
1882 printf("not supported\n");
1883 getinfo_super_imsm(st
, &info
, NULL
);
1884 fname_from_uuid(st
, &info
, nbuf
, ':');
1885 printf(" UUID : %s\n", nbuf
+ 5);
1886 sum
= __le32_to_cpu(mpb
->check_sum
);
1887 printf(" Checksum : %08x %s\n", sum
,
1888 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
1889 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
, super
->sector_size
));
1890 printf(" Disks : %d\n", mpb
->num_disks
);
1891 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
1892 print_imsm_disk(__get_imsm_disk(mpb
, super
->disks
->index
),
1893 super
->disks
->index
, reserved
, super
->sector_size
);
1894 if (get_imsm_bbm_log_size(super
->bbm_log
)) {
1895 struct bbm_log
*log
= super
->bbm_log
;
1898 printf("Bad Block Management Log:\n");
1899 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
1900 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
1901 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
1903 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1905 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1907 super
->current_vol
= i
;
1908 getinfo_super_imsm(st
, &info
, NULL
);
1909 fname_from_uuid(st
, &info
, nbuf
, ':');
1910 print_imsm_dev(super
, dev
, nbuf
+ 5, super
->disks
->index
);
1912 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1913 if (i
== super
->disks
->index
)
1915 print_imsm_disk(__get_imsm_disk(mpb
, i
), i
, reserved
,
1916 super
->sector_size
);
1919 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
1920 if (dl
->index
== -1)
1921 print_imsm_disk(&dl
->disk
, -1, reserved
,
1922 super
->sector_size
);
1924 examine_migr_rec_imsm(super
);
1927 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
1929 /* We just write a generic IMSM ARRAY entry */
1932 struct intel_super
*super
= st
->sb
;
1934 if (!super
->anchor
->num_raid_devs
) {
1935 printf("ARRAY metadata=imsm\n");
1939 getinfo_super_imsm(st
, &info
, NULL
);
1940 fname_from_uuid(st
, &info
, nbuf
, ':');
1941 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
1944 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
1946 /* We just write a generic IMSM ARRAY entry */
1950 struct intel_super
*super
= st
->sb
;
1953 if (!super
->anchor
->num_raid_devs
)
1956 getinfo_super_imsm(st
, &info
, NULL
);
1957 fname_from_uuid(st
, &info
, nbuf
, ':');
1958 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1959 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1961 super
->current_vol
= i
;
1962 getinfo_super_imsm(st
, &info
, NULL
);
1963 fname_from_uuid(st
, &info
, nbuf1
, ':');
1964 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
1965 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
1969 static void export_examine_super_imsm(struct supertype
*st
)
1971 struct intel_super
*super
= st
->sb
;
1972 struct imsm_super
*mpb
= super
->anchor
;
1976 getinfo_super_imsm(st
, &info
, NULL
);
1977 fname_from_uuid(st
, &info
, nbuf
, ':');
1978 printf("MD_METADATA=imsm\n");
1979 printf("MD_LEVEL=container\n");
1980 printf("MD_UUID=%s\n", nbuf
+5);
1981 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
1984 static int copy_metadata_imsm(struct supertype
*st
, int from
, int to
)
1986 /* The second last sector of the device contains
1987 * the "struct imsm_super" metadata.
1988 * This contains mpb_size which is the size in bytes of the
1989 * extended metadata. This is located immediately before
1991 * We want to read all that, plus the last sector which
1992 * may contain a migration record, and write it all
1996 unsigned long long dsize
, offset
;
1998 struct imsm_super
*sb
;
1999 struct intel_super
*super
= st
->sb
;
2000 unsigned int sector_size
= super
->sector_size
;
2001 unsigned int written
= 0;
2003 if (posix_memalign(&buf
, MAX_SECTOR_SIZE
, MAX_SECTOR_SIZE
) != 0)
2006 if (!get_dev_size(from
, NULL
, &dsize
))
2009 if (lseek64(from
, dsize
-(2*sector_size
), 0) < 0)
2011 if ((unsigned int)read(from
, buf
, sector_size
) != sector_size
)
2014 if (strncmp((char*)sb
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0)
2017 sectors
= mpb_sectors(sb
, sector_size
) + 2;
2018 offset
= dsize
- sectors
* sector_size
;
2019 if (lseek64(from
, offset
, 0) < 0 ||
2020 lseek64(to
, offset
, 0) < 0)
2022 while (written
< sectors
* sector_size
) {
2023 int n
= sectors
*sector_size
- written
;
2026 if (read(from
, buf
, n
) != n
)
2028 if (write(to
, buf
, n
) != n
)
2039 static void detail_super_imsm(struct supertype
*st
, char *homehost
)
2044 getinfo_super_imsm(st
, &info
, NULL
);
2045 fname_from_uuid(st
, &info
, nbuf
, ':');
2046 printf("\n UUID : %s\n", nbuf
+ 5);
2049 static void brief_detail_super_imsm(struct supertype
*st
)
2053 getinfo_super_imsm(st
, &info
, NULL
);
2054 fname_from_uuid(st
, &info
, nbuf
, ':');
2055 printf(" UUID=%s", nbuf
+ 5);
2058 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
);
2059 static void fd2devname(int fd
, char *name
);
2061 static int ahci_enumerate_ports(const char *hba_path
, int port_count
, int host_base
, int verbose
)
2063 /* dump an unsorted list of devices attached to AHCI Intel storage
2064 * controller, as well as non-connected ports
2066 int hba_len
= strlen(hba_path
) + 1;
2071 unsigned long port_mask
= (1 << port_count
) - 1;
2073 if (port_count
> (int)sizeof(port_mask
) * 8) {
2075 pr_err("port_count %d out of range\n", port_count
);
2079 /* scroll through /sys/dev/block looking for devices attached to
2082 dir
= opendir("/sys/dev/block");
2086 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2097 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
2099 path
= devt_to_devpath(makedev(major
, minor
));
2102 if (!path_attached_to_hba(path
, hba_path
)) {
2108 /* retrieve the scsi device type */
2109 if (asprintf(&device
, "/sys/dev/block/%d:%d/device/xxxxxxx", major
, minor
) < 0) {
2111 pr_err("failed to allocate 'device'\n");
2115 sprintf(device
, "/sys/dev/block/%d:%d/device/type", major
, minor
);
2116 if (load_sys(device
, buf
, sizeof(buf
)) != 0) {
2118 pr_err("failed to read device type for %s\n",
2124 type
= strtoul(buf
, NULL
, 10);
2126 /* if it's not a disk print the vendor and model */
2127 if (!(type
== 0 || type
== 7 || type
== 14)) {
2130 sprintf(device
, "/sys/dev/block/%d:%d/device/vendor", major
, minor
);
2131 if (load_sys(device
, buf
, sizeof(buf
)) == 0) {
2132 strncpy(vendor
, buf
, sizeof(vendor
));
2133 vendor
[sizeof(vendor
) - 1] = '\0';
2134 c
= (char *) &vendor
[sizeof(vendor
) - 1];
2135 while (isspace(*c
) || *c
== '\0')
2139 sprintf(device
, "/sys/dev/block/%d:%d/device/model", major
, minor
);
2140 if (load_sys(device
, buf
, sizeof(buf
)) == 0) {
2141 strncpy(model
, buf
, sizeof(model
));
2142 model
[sizeof(model
) - 1] = '\0';
2143 c
= (char *) &model
[sizeof(model
) - 1];
2144 while (isspace(*c
) || *c
== '\0')
2148 if (vendor
[0] && model
[0])
2149 sprintf(buf
, "%.64s %.64s", vendor
, model
);
2151 switch (type
) { /* numbers from hald/linux/device.c */
2152 case 1: sprintf(buf
, "tape"); break;
2153 case 2: sprintf(buf
, "printer"); break;
2154 case 3: sprintf(buf
, "processor"); break;
2156 case 5: sprintf(buf
, "cdrom"); break;
2157 case 6: sprintf(buf
, "scanner"); break;
2158 case 8: sprintf(buf
, "media_changer"); break;
2159 case 9: sprintf(buf
, "comm"); break;
2160 case 12: sprintf(buf
, "raid"); break;
2161 default: sprintf(buf
, "unknown");
2167 /* chop device path to 'host%d' and calculate the port number */
2168 c
= strchr(&path
[hba_len
], '/');
2171 pr_err("%s - invalid path name\n", path
+ hba_len
);
2176 if ((sscanf(&path
[hba_len
], "ata%d", &port
) == 1) ||
2177 ((sscanf(&path
[hba_len
], "host%d", &port
) == 1)))
2181 *c
= '/'; /* repair the full string */
2182 pr_err("failed to determine port number for %s\n",
2189 /* mark this port as used */
2190 port_mask
&= ~(1 << port
);
2192 /* print out the device information */
2194 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
2198 fd
= dev_open(ent
->d_name
, O_RDONLY
);
2200 printf(" Port%d : - disk info unavailable -\n", port
);
2202 fd2devname(fd
, buf
);
2203 printf(" Port%d : %s", port
, buf
);
2204 if (imsm_read_serial(fd
, NULL
, (__u8
*) buf
) == 0)
2205 printf(" (%.*s)\n", MAX_RAID_SERIAL_LEN
, buf
);
2220 for (i
= 0; i
< port_count
; i
++)
2221 if (port_mask
& (1 << i
))
2222 printf(" Port%d : - no device attached -\n", i
);
2228 static int print_vmd_attached_devs(struct sys_dev
*hba
)
2236 if (hba
->type
!= SYS_DEV_VMD
)
2239 /* scroll through /sys/dev/block looking for devices attached to
2242 dir
= opendir("/sys/bus/pci/drivers/nvme");
2246 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2249 /* is 'ent' a device? check that the 'subsystem' link exists and
2250 * that its target matches 'bus'
2252 sprintf(path
, "/sys/bus/pci/drivers/nvme/%s/subsystem",
2254 n
= readlink(path
, link
, sizeof(link
));
2255 if (n
< 0 || n
>= (int)sizeof(link
))
2258 c
= strrchr(link
, '/');
2261 if (strncmp("pci", c
+1, strlen("pci")) != 0)
2264 sprintf(path
, "/sys/bus/pci/drivers/nvme/%s", ent
->d_name
);
2266 rp
= realpath(path
, NULL
);
2270 if (path_attached_to_hba(rp
, hba
->path
)) {
2271 printf(" NVMe under VMD : %s\n", rp
);
2280 static void print_found_intel_controllers(struct sys_dev
*elem
)
2282 for (; elem
; elem
= elem
->next
) {
2283 pr_err("found Intel(R) ");
2284 if (elem
->type
== SYS_DEV_SATA
)
2285 fprintf(stderr
, "SATA ");
2286 else if (elem
->type
== SYS_DEV_SAS
)
2287 fprintf(stderr
, "SAS ");
2288 else if (elem
->type
== SYS_DEV_NVME
)
2289 fprintf(stderr
, "NVMe ");
2291 if (elem
->type
== SYS_DEV_VMD
)
2292 fprintf(stderr
, "VMD domain");
2294 fprintf(stderr
, "RAID controller");
2297 fprintf(stderr
, " at %s", elem
->pci_id
);
2298 fprintf(stderr
, ".\n");
2303 static int ahci_get_port_count(const char *hba_path
, int *port_count
)
2310 if ((dir
= opendir(hba_path
)) == NULL
)
2313 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2316 if ((sscanf(ent
->d_name
, "ata%d", &host
) != 1) &&
2317 ((sscanf(ent
->d_name
, "host%d", &host
) != 1)))
2319 if (*port_count
== 0)
2321 else if (host
< host_base
)
2324 if (host
+ 1 > *port_count
+ host_base
)
2325 *port_count
= host
+ 1 - host_base
;
2331 static void print_imsm_capability(const struct imsm_orom
*orom
)
2333 printf(" Platform : Intel(R) ");
2334 if (orom
->capabilities
== 0 && orom
->driver_features
== 0)
2335 printf("Matrix Storage Manager\n");
2337 printf("Rapid Storage Technology%s\n",
2338 imsm_orom_is_enterprise(orom
) ? " enterprise" : "");
2339 if (orom
->major_ver
|| orom
->minor_ver
|| orom
->hotfix_ver
|| orom
->build
)
2340 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
,
2341 orom
->minor_ver
, orom
->hotfix_ver
, orom
->build
);
2342 printf(" RAID Levels :%s%s%s%s%s\n",
2343 imsm_orom_has_raid0(orom
) ? " raid0" : "",
2344 imsm_orom_has_raid1(orom
) ? " raid1" : "",
2345 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
2346 imsm_orom_has_raid10(orom
) ? " raid10" : "",
2347 imsm_orom_has_raid5(orom
) ? " raid5" : "");
2348 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2349 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
2350 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
2351 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
2352 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
2353 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
2354 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
2355 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
2356 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
2357 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
2358 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
2359 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
2360 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
2361 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
2362 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
2363 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
2364 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
2365 printf(" 2TB volumes :%s supported\n",
2366 (orom
->attr
& IMSM_OROM_ATTR_2TB
)?"":" not");
2367 printf(" 2TB disks :%s supported\n",
2368 (orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
)?"":" not");
2369 printf(" Max Disks : %d\n", orom
->tds
);
2370 printf(" Max Volumes : %d per array, %d per %s\n",
2371 orom
->vpa
, orom
->vphba
,
2372 imsm_orom_is_nvme(orom
) ? "platform" : "controller");
2376 static void print_imsm_capability_export(const struct imsm_orom
*orom
)
2378 printf("MD_FIRMWARE_TYPE=imsm\n");
2379 if (orom
->major_ver
|| orom
->minor_ver
|| orom
->hotfix_ver
|| orom
->build
)
2380 printf("IMSM_VERSION=%d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
2381 orom
->hotfix_ver
, orom
->build
);
2382 printf("IMSM_SUPPORTED_RAID_LEVELS=%s%s%s%s%s\n",
2383 imsm_orom_has_raid0(orom
) ? "raid0 " : "",
2384 imsm_orom_has_raid1(orom
) ? "raid1 " : "",
2385 imsm_orom_has_raid1e(orom
) ? "raid1e " : "",
2386 imsm_orom_has_raid5(orom
) ? "raid10 " : "",
2387 imsm_orom_has_raid10(orom
) ? "raid5 " : "");
2388 printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2389 imsm_orom_has_chunk(orom
, 2) ? "2k " : "",
2390 imsm_orom_has_chunk(orom
, 4) ? "4k " : "",
2391 imsm_orom_has_chunk(orom
, 8) ? "8k " : "",
2392 imsm_orom_has_chunk(orom
, 16) ? "16k " : "",
2393 imsm_orom_has_chunk(orom
, 32) ? "32k " : "",
2394 imsm_orom_has_chunk(orom
, 64) ? "64k " : "",
2395 imsm_orom_has_chunk(orom
, 128) ? "128k " : "",
2396 imsm_orom_has_chunk(orom
, 256) ? "256k " : "",
2397 imsm_orom_has_chunk(orom
, 512) ? "512k " : "",
2398 imsm_orom_has_chunk(orom
, 1024*1) ? "1M " : "",
2399 imsm_orom_has_chunk(orom
, 1024*2) ? "2M " : "",
2400 imsm_orom_has_chunk(orom
, 1024*4) ? "4M " : "",
2401 imsm_orom_has_chunk(orom
, 1024*8) ? "8M " : "",
2402 imsm_orom_has_chunk(orom
, 1024*16) ? "16M " : "",
2403 imsm_orom_has_chunk(orom
, 1024*32) ? "32M " : "",
2404 imsm_orom_has_chunk(orom
, 1024*64) ? "64M " : "");
2405 printf("IMSM_2TB_VOLUMES=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB
) ? "yes" : "no");
2406 printf("IMSM_2TB_DISKS=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) ? "yes" : "no");
2407 printf("IMSM_MAX_DISKS=%d\n",orom
->tds
);
2408 printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom
->vpa
);
2409 printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom
->vphba
);
2412 static int detail_platform_imsm(int verbose
, int enumerate_only
, char *controller_path
)
2414 /* There are two components to imsm platform support, the ahci SATA
2415 * controller and the option-rom. To find the SATA controller we
2416 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
2417 * controller with the Intel vendor id is present. This approach
2418 * allows mdadm to leverage the kernel's ahci detection logic, with the
2419 * caveat that if ahci.ko is not loaded mdadm will not be able to
2420 * detect platform raid capabilities. The option-rom resides in a
2421 * platform "Adapter ROM". We scan for its signature to retrieve the
2422 * platform capabilities. If raid support is disabled in the BIOS the
2423 * option-rom capability structure will not be available.
2425 struct sys_dev
*list
, *hba
;
2430 if (enumerate_only
) {
2431 if (check_env("IMSM_NO_PLATFORM"))
2433 list
= find_intel_devices();
2436 for (hba
= list
; hba
; hba
= hba
->next
) {
2437 if (find_imsm_capability(hba
)) {
2447 list
= find_intel_devices();
2450 pr_err("no active Intel(R) RAID controller found.\n");
2452 } else if (verbose
> 0)
2453 print_found_intel_controllers(list
);
2455 for (hba
= list
; hba
; hba
= hba
->next
) {
2456 if (controller_path
&& (compare_paths(hba
->path
, controller_path
) != 0))
2458 if (!find_imsm_capability(hba
)) {
2460 pr_err("imsm capabilities not found for controller: %s (type %s)\n",
2461 hba
->type
== SYS_DEV_VMD
? vmd_domain_to_controller(hba
, buf
) : hba
->path
,
2462 get_sys_dev_type(hba
->type
));
2468 if (controller_path
&& result
== 1) {
2469 pr_err("no active Intel(R) RAID controller found under %s\n",
2474 const struct orom_entry
*entry
;
2476 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
2477 if (entry
->type
== SYS_DEV_VMD
) {
2478 print_imsm_capability(&entry
->orom
);
2479 printf(" 3rd party NVMe :%s supported\n",
2480 imsm_orom_has_tpv_support(&entry
->orom
)?"":" not");
2481 for (hba
= list
; hba
; hba
= hba
->next
) {
2482 if (hba
->type
== SYS_DEV_VMD
) {
2484 printf(" I/O Controller : %s (%s)\n",
2485 vmd_domain_to_controller(hba
, buf
), get_sys_dev_type(hba
->type
));
2486 if (print_vmd_attached_devs(hba
)) {
2488 pr_err("failed to get devices attached to VMD domain.\n");
2497 print_imsm_capability(&entry
->orom
);
2498 if (entry
->type
== SYS_DEV_NVME
) {
2499 for (hba
= list
; hba
; hba
= hba
->next
) {
2500 if (hba
->type
== SYS_DEV_NVME
)
2501 printf(" NVMe Device : %s\n", hba
->path
);
2507 struct devid_list
*devid
;
2508 for (devid
= entry
->devid_list
; devid
; devid
= devid
->next
) {
2509 hba
= device_by_id(devid
->devid
);
2513 printf(" I/O Controller : %s (%s)\n",
2514 hba
->path
, get_sys_dev_type(hba
->type
));
2515 if (hba
->type
== SYS_DEV_SATA
) {
2516 host_base
= ahci_get_port_count(hba
->path
, &port_count
);
2517 if (ahci_enumerate_ports(hba
->path
, port_count
, host_base
, verbose
)) {
2519 pr_err("failed to enumerate ports on SATA controller at %s.\n", hba
->pci_id
);
2530 static int export_detail_platform_imsm(int verbose
, char *controller_path
)
2532 struct sys_dev
*list
, *hba
;
2535 list
= find_intel_devices();
2538 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n");
2543 for (hba
= list
; hba
; hba
= hba
->next
) {
2544 if (controller_path
&& (compare_paths(hba
->path
,controller_path
) != 0))
2546 if (!find_imsm_capability(hba
) && verbose
> 0) {
2548 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n",
2549 hba
->type
== SYS_DEV_VMD
? vmd_domain_to_controller(hba
, buf
) : hba
->path
);
2555 const struct orom_entry
*entry
;
2557 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
2558 if (entry
->type
== SYS_DEV_VMD
) {
2559 for (hba
= list
; hba
; hba
= hba
->next
)
2560 print_imsm_capability_export(&entry
->orom
);
2563 print_imsm_capability_export(&entry
->orom
);
2569 static int match_home_imsm(struct supertype
*st
, char *homehost
)
2571 /* the imsm metadata format does not specify any host
2572 * identification information. We return -1 since we can never
2573 * confirm nor deny whether a given array is "meant" for this
2574 * host. We rely on compare_super and the 'family_num' fields to
2575 * exclude member disks that do not belong, and we rely on
2576 * mdadm.conf to specify the arrays that should be assembled.
2577 * Auto-assembly may still pick up "foreign" arrays.
2583 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
2585 /* The uuid returned here is used for:
2586 * uuid to put into bitmap file (Create, Grow)
2587 * uuid for backup header when saving critical section (Grow)
2588 * comparing uuids when re-adding a device into an array
2589 * In these cases the uuid required is that of the data-array,
2590 * not the device-set.
2591 * uuid to recognise same set when adding a missing device back
2592 * to an array. This is a uuid for the device-set.
2594 * For each of these we can make do with a truncated
2595 * or hashed uuid rather than the original, as long as
2597 * In each case the uuid required is that of the data-array,
2598 * not the device-set.
2600 /* imsm does not track uuid's so we synthesis one using sha1 on
2601 * - The signature (Which is constant for all imsm array, but no matter)
2602 * - the orig_family_num of the container
2603 * - the index number of the volume
2604 * - the 'serial' number of the volume.
2605 * Hopefully these are all constant.
2607 struct intel_super
*super
= st
->sb
;
2610 struct sha1_ctx ctx
;
2611 struct imsm_dev
*dev
= NULL
;
2614 /* some mdadm versions failed to set ->orig_family_num, in which
2615 * case fall back to ->family_num. orig_family_num will be
2616 * fixed up with the first metadata update.
2618 family_num
= super
->anchor
->orig_family_num
;
2619 if (family_num
== 0)
2620 family_num
= super
->anchor
->family_num
;
2621 sha1_init_ctx(&ctx
);
2622 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
2623 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
2624 if (super
->current_vol
>= 0)
2625 dev
= get_imsm_dev(super
, super
->current_vol
);
2627 __u32 vol
= super
->current_vol
;
2628 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
2629 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
2631 sha1_finish_ctx(&ctx
, buf
);
2632 memcpy(uuid
, buf
, 4*4);
2637 get_imsm_numerical_version(struct imsm_super
*mpb
, int *m
, int *p
)
2639 __u8
*v
= get_imsm_version(mpb
);
2640 __u8
*end
= mpb
->sig
+ MAX_SIGNATURE_LENGTH
;
2641 char major
[] = { 0, 0, 0 };
2642 char minor
[] = { 0 ,0, 0 };
2643 char patch
[] = { 0, 0, 0 };
2644 char *ver_parse
[] = { major
, minor
, patch
};
2648 while (*v
!= '\0' && v
< end
) {
2649 if (*v
!= '.' && j
< 2)
2650 ver_parse
[i
][j
++] = *v
;
2658 *m
= strtol(minor
, NULL
, 0);
2659 *p
= strtol(patch
, NULL
, 0);
2663 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
2665 /* migr_strip_size when repairing or initializing parity */
2666 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2667 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2669 switch (get_imsm_raid_level(map
)) {
2674 return 128*1024 >> 9;
2678 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
2680 /* migr_strip_size when rebuilding a degraded disk, no idea why
2681 * this is different than migr_strip_size_resync(), but it's good
2684 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2685 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2687 switch (get_imsm_raid_level(map
)) {
2690 if (map
->num_members
% map
->num_domains
== 0)
2691 return 128*1024 >> 9;
2695 return max((__u32
) 64*1024 >> 9, chunk
);
2697 return 128*1024 >> 9;
2701 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
2703 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2704 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2705 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
2706 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
2708 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
2711 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
2713 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2714 int level
= get_imsm_raid_level(lo
);
2716 if (level
== 1 || level
== 10) {
2717 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2719 return hi
->num_domains
;
2721 return num_stripes_per_unit_resync(dev
);
2724 static __u8
imsm_num_data_members(struct imsm_dev
*dev
, int second_map
)
2726 /* named 'imsm_' because raid0, raid1 and raid10
2727 * counter-intuitively have the same number of data disks
2729 struct imsm_map
*map
= get_imsm_map(dev
, second_map
);
2731 switch (get_imsm_raid_level(map
)) {
2733 return map
->num_members
;
2737 return map
->num_members
/2;
2739 return map
->num_members
- 1;
2741 dprintf("unsupported raid level\n");
2746 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
2748 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2749 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2751 switch(get_imsm_raid_level(map
)) {
2754 return chunk
* map
->num_domains
;
2756 return chunk
* map
->num_members
;
2762 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
2764 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2765 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2766 __u32 strip
= block
/ chunk
;
2768 switch (get_imsm_raid_level(map
)) {
2771 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
2772 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
2774 return vol_stripe
* chunk
+ block
% chunk
;
2776 __u32 stripe
= strip
/ (map
->num_members
- 1);
2778 return stripe
* chunk
+ block
% chunk
;
2785 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
2786 struct imsm_dev
*dev
)
2788 /* calculate the conversion factor between per member 'blocks'
2789 * (md/{resync,rebuild}_start) and imsm migration units, return
2790 * 0 for the 'not migrating' and 'unsupported migration' cases
2792 if (!dev
->vol
.migr_state
)
2795 switch (migr_type(dev
)) {
2796 case MIGR_GEN_MIGR
: {
2797 struct migr_record
*migr_rec
= super
->migr_rec
;
2798 return __le32_to_cpu(migr_rec
->blocks_per_unit
);
2803 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2804 __u32 stripes_per_unit
;
2805 __u32 blocks_per_unit
;
2814 /* yes, this is really the translation of migr_units to
2815 * per-member blocks in the 'resync' case
2817 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
2818 migr_chunk
= migr_strip_blocks_resync(dev
);
2819 disks
= imsm_num_data_members(dev
, MAP_0
);
2820 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
2821 stripe
= __le16_to_cpu(map
->blocks_per_strip
) * disks
;
2822 segment
= blocks_per_unit
/ stripe
;
2823 block_rel
= blocks_per_unit
- segment
* stripe
;
2824 parity_depth
= parity_segment_depth(dev
);
2825 block_map
= map_migr_block(dev
, block_rel
);
2826 return block_map
+ parity_depth
* segment
;
2828 case MIGR_REBUILD
: {
2829 __u32 stripes_per_unit
;
2832 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
2833 migr_chunk
= migr_strip_blocks_rebuild(dev
);
2834 return migr_chunk
* stripes_per_unit
;
2836 case MIGR_STATE_CHANGE
:
2842 static int imsm_level_to_layout(int level
)
2850 return ALGORITHM_LEFT_ASYMMETRIC
;
2857 /*******************************************************************************
2858 * Function: read_imsm_migr_rec
2859 * Description: Function reads imsm migration record from last sector of disk
2861 * fd : disk descriptor
2862 * super : metadata info
2866 ******************************************************************************/
2867 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
)
2870 unsigned int sector_size
= super
->sector_size
;
2871 unsigned long long dsize
;
2873 get_dev_size(fd
, NULL
, &dsize
);
2874 if (lseek64(fd
, dsize
- (sector_size
*MIGR_REC_SECTOR_POSITION
),
2876 pr_err("Cannot seek to anchor block: %s\n",
2880 if ((unsigned int)read(fd
, super
->migr_rec_buf
,
2881 MIGR_REC_BUF_SECTORS
*sector_size
) !=
2882 MIGR_REC_BUF_SECTORS
*sector_size
) {
2883 pr_err("Cannot read migr record block: %s\n",
2888 if (sector_size
== 4096)
2889 convert_from_4k_imsm_migr_rec(super
);
2895 static struct imsm_dev
*imsm_get_device_during_migration(
2896 struct intel_super
*super
)
2899 struct intel_dev
*dv
;
2901 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
2902 if (is_gen_migration(dv
->dev
))
2908 /*******************************************************************************
2909 * Function: load_imsm_migr_rec
2910 * Description: Function reads imsm migration record (it is stored at the last
2913 * super : imsm internal array info
2914 * info : general array info
2918 * -2 : no migration in progress
2919 ******************************************************************************/
2920 static int load_imsm_migr_rec(struct intel_super
*super
, struct mdinfo
*info
)
2927 struct imsm_dev
*dev
;
2928 struct imsm_map
*map
;
2931 /* find map under migration */
2932 dev
= imsm_get_device_during_migration(super
);
2933 /* nothing to load,no migration in progress?
2939 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
2940 /* read only from one of the first two slots */
2941 if ((sd
->disk
.raid_disk
< 0) ||
2942 (sd
->disk
.raid_disk
> 1))
2945 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
2946 fd
= dev_open(nm
, O_RDONLY
);
2952 map
= get_imsm_map(dev
, MAP_0
);
2953 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
2954 /* skip spare and failed disks
2958 /* read only from one of the first two slots */
2960 slot
= get_imsm_disk_slot(map
, dl
->index
);
2961 if (map
== NULL
|| slot
> 1 || slot
< 0)
2963 sprintf(nm
, "%d:%d", dl
->major
, dl
->minor
);
2964 fd
= dev_open(nm
, O_RDONLY
);
2971 retval
= read_imsm_migr_rec(fd
, super
);
2979 /*******************************************************************************
2980 * function: imsm_create_metadata_checkpoint_update
2981 * Description: It creates update for checkpoint change.
2983 * super : imsm internal array info
2984 * u : pointer to prepared update
2987 * If length is equal to 0, input pointer u contains no update
2988 ******************************************************************************/
2989 static int imsm_create_metadata_checkpoint_update(
2990 struct intel_super
*super
,
2991 struct imsm_update_general_migration_checkpoint
**u
)
2994 int update_memory_size
= 0;
2996 dprintf("(enter)\n");
3002 /* size of all update data without anchor */
3003 update_memory_size
=
3004 sizeof(struct imsm_update_general_migration_checkpoint
);
3006 *u
= xcalloc(1, update_memory_size
);
3008 dprintf("error: cannot get memory\n");
3011 (*u
)->type
= update_general_migration_checkpoint
;
3012 (*u
)->curr_migr_unit
= __le32_to_cpu(super
->migr_rec
->curr_migr_unit
);
3013 dprintf("prepared for %u\n", (*u
)->curr_migr_unit
);
3015 return update_memory_size
;
3018 static void imsm_update_metadata_locally(struct supertype
*st
,
3019 void *buf
, int len
);
3021 /*******************************************************************************
3022 * Function: write_imsm_migr_rec
3023 * Description: Function writes imsm migration record
3024 * (at the last sector of disk)
3026 * super : imsm internal array info
3030 ******************************************************************************/
3031 static int write_imsm_migr_rec(struct supertype
*st
)
3033 struct intel_super
*super
= st
->sb
;
3034 unsigned int sector_size
= super
->sector_size
;
3035 unsigned long long dsize
;
3041 struct imsm_update_general_migration_checkpoint
*u
;
3042 struct imsm_dev
*dev
;
3043 struct imsm_map
*map
;
3045 /* find map under migration */
3046 dev
= imsm_get_device_during_migration(super
);
3047 /* if no migration, write buffer anyway to clear migr_record
3048 * on disk based on first available device
3051 dev
= get_imsm_dev(super
, super
->current_vol
< 0 ? 0 :
3052 super
->current_vol
);
3054 map
= get_imsm_map(dev
, MAP_0
);
3056 if (sector_size
== 4096)
3057 convert_to_4k_imsm_migr_rec(super
);
3058 for (sd
= super
->disks
; sd
; sd
= sd
->next
) {
3061 /* skip failed and spare devices */
3064 /* write to 2 first slots only */
3066 slot
= get_imsm_disk_slot(map
, sd
->index
);
3067 if (map
== NULL
|| slot
> 1 || slot
< 0)
3070 sprintf(nm
, "%d:%d", sd
->major
, sd
->minor
);
3071 fd
= dev_open(nm
, O_RDWR
);
3074 get_dev_size(fd
, NULL
, &dsize
);
3075 if (lseek64(fd
, dsize
- (MIGR_REC_SECTOR_POSITION
*sector_size
),
3077 pr_err("Cannot seek to anchor block: %s\n",
3081 if ((unsigned int)write(fd
, super
->migr_rec_buf
,
3082 MIGR_REC_BUF_SECTORS
*sector_size
) !=
3083 MIGR_REC_BUF_SECTORS
*sector_size
) {
3084 pr_err("Cannot write migr record block: %s\n",
3091 if (sector_size
== 4096)
3092 convert_from_4k_imsm_migr_rec(super
);
3093 /* update checkpoint information in metadata */
3094 len
= imsm_create_metadata_checkpoint_update(super
, &u
);
3096 dprintf("imsm: Cannot prepare update\n");
3099 /* update metadata locally */
3100 imsm_update_metadata_locally(st
, u
, len
);
3101 /* and possibly remotely */
3102 if (st
->update_tail
) {
3103 append_metadata_update(st
, u
, len
);
3104 /* during reshape we do all work inside metadata handler
3105 * manage_reshape(), so metadata update has to be triggered
3108 flush_metadata_updates(st
);
3109 st
->update_tail
= &st
->updates
;
3120 /* spare/missing disks activations are not allowe when
3121 * array/container performs reshape operation, because
3122 * all arrays in container works on the same disks set
3124 int imsm_reshape_blocks_arrays_changes(struct intel_super
*super
)
3127 struct intel_dev
*i_dev
;
3128 struct imsm_dev
*dev
;
3130 /* check whole container
3132 for (i_dev
= super
->devlist
; i_dev
; i_dev
= i_dev
->next
) {
3134 if (is_gen_migration(dev
)) {
3135 /* No repair during any migration in container
3143 static unsigned long long imsm_component_size_aligment_check(int level
,
3145 unsigned int sector_size
,
3146 unsigned long long component_size
)
3148 unsigned int component_size_alligment
;
3150 /* check component size aligment
3152 component_size_alligment
= component_size
% (chunk_size
/sector_size
);
3154 dprintf("(Level: %i, chunk_size = %i, component_size = %llu), component_size_alligment = %u\n",
3155 level
, chunk_size
, component_size
,
3156 component_size_alligment
);
3158 if (component_size_alligment
&& (level
!= 1) && (level
!= UnSet
)) {
3159 dprintf("imsm: reported component size alligned from %llu ",
3161 component_size
-= component_size_alligment
;
3162 dprintf_cont("to %llu (%i).\n",
3163 component_size
, component_size_alligment
);
3166 return component_size
;
3169 static unsigned long long get_ppl_sector(struct intel_super
*super
, int dev_idx
)
3171 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
3172 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3174 return pba_of_lba0(map
) +
3175 (num_data_stripes(map
) * map
->blocks_per_strip
);
3178 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
3180 struct intel_super
*super
= st
->sb
;
3181 struct migr_record
*migr_rec
= super
->migr_rec
;
3182 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
3183 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3184 struct imsm_map
*prev_map
= get_imsm_map(dev
, MAP_1
);
3185 struct imsm_map
*map_to_analyse
= map
;
3187 int map_disks
= info
->array
.raid_disks
;
3189 memset(info
, 0, sizeof(*info
));
3191 map_to_analyse
= prev_map
;
3193 dl
= super
->current_disk
;
3195 info
->container_member
= super
->current_vol
;
3196 info
->array
.raid_disks
= map
->num_members
;
3197 info
->array
.level
= get_imsm_raid_level(map_to_analyse
);
3198 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
3199 info
->array
.md_minor
= -1;
3200 info
->array
.ctime
= 0;
3201 info
->array
.utime
= 0;
3202 info
->array
.chunk_size
=
3203 __le16_to_cpu(map_to_analyse
->blocks_per_strip
) << 9;
3204 info
->array
.state
= !(dev
->vol
.dirty
& RAIDVOL_DIRTY
);
3205 info
->custom_array_size
= __le32_to_cpu(dev
->size_high
);
3206 info
->custom_array_size
<<= 32;
3207 info
->custom_array_size
|= __le32_to_cpu(dev
->size_low
);
3208 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
3210 if (is_gen_migration(dev
)) {
3211 info
->reshape_active
= 1;
3212 info
->new_level
= get_imsm_raid_level(map
);
3213 info
->new_layout
= imsm_level_to_layout(info
->new_level
);
3214 info
->new_chunk
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
3215 info
->delta_disks
= map
->num_members
- prev_map
->num_members
;
3216 if (info
->delta_disks
) {
3217 /* this needs to be applied to every array
3220 info
->reshape_active
= CONTAINER_RESHAPE
;
3222 /* We shape information that we give to md might have to be
3223 * modify to cope with md's requirement for reshaping arrays.
3224 * For example, when reshaping a RAID0, md requires it to be
3225 * presented as a degraded RAID4.
3226 * Also if a RAID0 is migrating to a RAID5 we need to specify
3227 * the array as already being RAID5, but the 'before' layout
3228 * is a RAID4-like layout.
3230 switch (info
->array
.level
) {
3232 switch(info
->new_level
) {
3234 /* conversion is happening as RAID4 */
3235 info
->array
.level
= 4;
3236 info
->array
.raid_disks
+= 1;
3239 /* conversion is happening as RAID5 */
3240 info
->array
.level
= 5;
3241 info
->array
.layout
= ALGORITHM_PARITY_N
;
3242 info
->delta_disks
-= 1;
3245 /* FIXME error message */
3246 info
->array
.level
= UnSet
;
3252 info
->new_level
= UnSet
;
3253 info
->new_layout
= UnSet
;
3254 info
->new_chunk
= info
->array
.chunk_size
;
3255 info
->delta_disks
= 0;
3259 info
->disk
.major
= dl
->major
;
3260 info
->disk
.minor
= dl
->minor
;
3261 info
->disk
.number
= dl
->index
;
3262 info
->disk
.raid_disk
= get_imsm_disk_slot(map_to_analyse
,
3266 info
->data_offset
= pba_of_lba0(map_to_analyse
);
3268 if (info
->array
.level
== 5) {
3269 info
->component_size
= num_data_stripes(map_to_analyse
) *
3270 map_to_analyse
->blocks_per_strip
;
3272 info
->component_size
= blocks_per_member(map_to_analyse
);
3275 info
->component_size
= imsm_component_size_aligment_check(
3277 info
->array
.chunk_size
,
3279 info
->component_size
);
3280 info
->bb
.supported
= 1;
3282 memset(info
->uuid
, 0, sizeof(info
->uuid
));
3283 info
->recovery_start
= MaxSector
;
3285 if (info
->array
.level
== 5 && dev
->rwh_policy
== RWH_DISTRIBUTED
) {
3286 info
->consistency_policy
= CONSISTENCY_POLICY_PPL
;
3287 info
->ppl_sector
= get_ppl_sector(super
, super
->current_vol
);
3288 info
->ppl_size
= (PPL_HEADER_SIZE
+ PPL_ENTRY_SPACE
) >> 9;
3289 } else if (info
->array
.level
<= 0) {
3290 info
->consistency_policy
= CONSISTENCY_POLICY_NONE
;
3292 info
->consistency_policy
= CONSISTENCY_POLICY_RESYNC
;
3295 info
->reshape_progress
= 0;
3296 info
->resync_start
= MaxSector
;
3297 if ((map_to_analyse
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
3298 !(info
->array
.state
& 1)) &&
3299 imsm_reshape_blocks_arrays_changes(super
) == 0) {
3300 info
->resync_start
= 0;
3302 if (dev
->vol
.migr_state
) {
3303 switch (migr_type(dev
)) {
3306 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
3308 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
3310 info
->resync_start
= blocks_per_unit
* units
;
3313 case MIGR_GEN_MIGR
: {
3314 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
3316 __u64 units
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
3317 unsigned long long array_blocks
;
3320 if (__le32_to_cpu(migr_rec
->ascending_migr
) &&
3322 (__le32_to_cpu(migr_rec
->num_migr_units
)-1)) &&
3323 (super
->migr_rec
->rec_status
==
3324 __cpu_to_le32(UNIT_SRC_IN_CP_AREA
)))
3327 info
->reshape_progress
= blocks_per_unit
* units
;
3329 dprintf("IMSM: General Migration checkpoint : %llu (%llu) -> read reshape progress : %llu\n",
3330 (unsigned long long)units
,
3331 (unsigned long long)blocks_per_unit
,
3332 info
->reshape_progress
);
3334 used_disks
= imsm_num_data_members(dev
, MAP_1
);
3335 if (used_disks
> 0) {
3336 array_blocks
= blocks_per_member(map
) *
3338 info
->custom_array_size
=
3339 round_size_to_mb(array_blocks
,
3345 /* we could emulate the checkpointing of
3346 * 'sync_action=check' migrations, but for now
3347 * we just immediately complete them
3350 /* this is handled by container_content_imsm() */
3351 case MIGR_STATE_CHANGE
:
3352 /* FIXME handle other migrations */
3354 /* we are not dirty, so... */
3355 info
->resync_start
= MaxSector
;
3359 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
3360 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
3362 info
->array
.major_version
= -1;
3363 info
->array
.minor_version
= -2;
3364 sprintf(info
->text_version
, "/%s/%d", st
->container_devnm
, info
->container_member
);
3365 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
3366 uuid_from_super_imsm(st
, info
->uuid
);
3370 for (i
=0; i
<map_disks
; i
++) {
3372 if (i
< info
->array
.raid_disks
) {
3373 struct imsm_disk
*dsk
;
3374 j
= get_imsm_disk_idx(dev
, i
, MAP_X
);
3375 dsk
= get_imsm_disk(super
, j
);
3376 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
3383 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
3384 int failed
, int look_in_map
);
3386 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
3389 static void manage_second_map(struct intel_super
*super
, struct imsm_dev
*dev
)
3391 if (is_gen_migration(dev
)) {
3394 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
3396 failed
= imsm_count_failed(super
, dev
, MAP_1
);
3397 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_1
);
3398 if (map2
->map_state
!= map_state
) {
3399 map2
->map_state
= map_state
;
3400 super
->updates_pending
++;
3405 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
3409 for (d
= super
->missing
; d
; d
= d
->next
)
3410 if (d
->index
== index
)
3415 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
3417 struct intel_super
*super
= st
->sb
;
3418 struct imsm_disk
*disk
;
3419 int map_disks
= info
->array
.raid_disks
;
3420 int max_enough
= -1;
3422 struct imsm_super
*mpb
;
3424 if (super
->current_vol
>= 0) {
3425 getinfo_super_imsm_volume(st
, info
, map
);
3428 memset(info
, 0, sizeof(*info
));
3430 /* Set raid_disks to zero so that Assemble will always pull in valid
3433 info
->array
.raid_disks
= 0;
3434 info
->array
.level
= LEVEL_CONTAINER
;
3435 info
->array
.layout
= 0;
3436 info
->array
.md_minor
= -1;
3437 info
->array
.ctime
= 0; /* N/A for imsm */
3438 info
->array
.utime
= 0;
3439 info
->array
.chunk_size
= 0;
3441 info
->disk
.major
= 0;
3442 info
->disk
.minor
= 0;
3443 info
->disk
.raid_disk
= -1;
3444 info
->reshape_active
= 0;
3445 info
->array
.major_version
= -1;
3446 info
->array
.minor_version
= -2;
3447 strcpy(info
->text_version
, "imsm");
3448 info
->safe_mode_delay
= 0;
3449 info
->disk
.number
= -1;
3450 info
->disk
.state
= 0;
3452 info
->recovery_start
= MaxSector
;
3453 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
3454 info
->bb
.supported
= 1;
3456 /* do we have the all the insync disks that we expect? */
3457 mpb
= super
->anchor
;
3458 info
->events
= __le32_to_cpu(mpb
->generation_num
);
3460 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3461 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3462 int failed
, enough
, j
, missing
= 0;
3463 struct imsm_map
*map
;
3466 failed
= imsm_count_failed(super
, dev
, MAP_0
);
3467 state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
3468 map
= get_imsm_map(dev
, MAP_0
);
3470 /* any newly missing disks?
3471 * (catches single-degraded vs double-degraded)
3473 for (j
= 0; j
< map
->num_members
; j
++) {
3474 __u32 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
3475 __u32 idx
= ord_to_idx(ord
);
3477 if (!(ord
& IMSM_ORD_REBUILD
) &&
3478 get_imsm_missing(super
, idx
)) {
3484 if (state
== IMSM_T_STATE_FAILED
)
3486 else if (state
== IMSM_T_STATE_DEGRADED
&&
3487 (state
!= map
->map_state
|| missing
))
3489 else /* we're normal, or already degraded */
3491 if (is_gen_migration(dev
) && missing
) {
3492 /* during general migration we need all disks
3493 * that process is running on.
3494 * No new missing disk is allowed.
3498 /* no more checks necessary
3502 /* in the missing/failed disk case check to see
3503 * if at least one array is runnable
3505 max_enough
= max(max_enough
, enough
);
3507 dprintf("enough: %d\n", max_enough
);
3508 info
->container_enough
= max_enough
;
3511 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
3513 disk
= &super
->disks
->disk
;
3514 info
->data_offset
= total_blocks(&super
->disks
->disk
) - reserved
;
3515 info
->component_size
= reserved
;
3516 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
3517 /* we don't change info->disk.raid_disk here because
3518 * this state will be finalized in mdmon after we have
3519 * found the 'most fresh' version of the metadata
3521 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
3522 info
->disk
.state
|= (is_spare(disk
) || is_journal(disk
)) ?
3523 0 : (1 << MD_DISK_SYNC
);
3526 /* only call uuid_from_super_imsm when this disk is part of a populated container,
3527 * ->compare_super may have updated the 'num_raid_devs' field for spares
3529 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
3530 uuid_from_super_imsm(st
, info
->uuid
);
3532 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
3534 /* I don't know how to compute 'map' on imsm, so use safe default */
3537 for (i
= 0; i
< map_disks
; i
++)
3543 /* allocates memory and fills disk in mdinfo structure
3544 * for each disk in array */
3545 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
3547 struct mdinfo
*mddev
;
3548 struct intel_super
*super
= st
->sb
;
3549 struct imsm_disk
*disk
;
3552 if (!super
|| !super
->disks
)
3555 mddev
= xcalloc(1, sizeof(*mddev
));
3559 tmp
= xcalloc(1, sizeof(*tmp
));
3561 tmp
->next
= mddev
->devs
;
3563 tmp
->disk
.number
= count
++;
3564 tmp
->disk
.major
= dl
->major
;
3565 tmp
->disk
.minor
= dl
->minor
;
3566 tmp
->disk
.state
= is_configured(disk
) ?
3567 (1 << MD_DISK_ACTIVE
) : 0;
3568 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
3569 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
3570 tmp
->disk
.raid_disk
= -1;
3576 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
3577 char *update
, char *devname
, int verbose
,
3578 int uuid_set
, char *homehost
)
3580 /* For 'assemble' and 'force' we need to return non-zero if any
3581 * change was made. For others, the return value is ignored.
3582 * Update options are:
3583 * force-one : This device looks a bit old but needs to be included,
3584 * update age info appropriately.
3585 * assemble: clear any 'faulty' flag to allow this device to
3587 * force-array: Array is degraded but being forced, mark it clean
3588 * if that will be needed to assemble it.
3590 * newdev: not used ????
3591 * grow: Array has gained a new device - this is currently for
3593 * resync: mark as dirty so a resync will happen.
3594 * name: update the name - preserving the homehost
3595 * uuid: Change the uuid of the array to match watch is given
3597 * Following are not relevant for this imsm:
3598 * sparc2.2 : update from old dodgey metadata
3599 * super-minor: change the preferred_minor number
3600 * summaries: update redundant counters.
3601 * homehost: update the recorded homehost
3602 * _reshape_progress: record new reshape_progress position.
3605 struct intel_super
*super
= st
->sb
;
3606 struct imsm_super
*mpb
;
3608 /* we can only update container info */
3609 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
3612 mpb
= super
->anchor
;
3614 if (strcmp(update
, "uuid") == 0) {
3615 /* We take this to mean that the family_num should be updated.
3616 * However that is much smaller than the uuid so we cannot really
3617 * allow an explicit uuid to be given. And it is hard to reliably
3619 * So if !uuid_set we know the current uuid is random and just used
3620 * the first 'int' and copy it to the other 3 positions.
3621 * Otherwise we require the 4 'int's to be the same as would be the
3622 * case if we are using a random uuid. So an explicit uuid will be
3623 * accepted as long as all for ints are the same... which shouldn't hurt
3626 info
->uuid
[1] = info
->uuid
[2] = info
->uuid
[3] = info
->uuid
[0];
3629 if (info
->uuid
[0] != info
->uuid
[1] ||
3630 info
->uuid
[1] != info
->uuid
[2] ||
3631 info
->uuid
[2] != info
->uuid
[3])
3637 mpb
->orig_family_num
= info
->uuid
[0];
3638 } else if (strcmp(update
, "assemble") == 0)
3643 /* successful update? recompute checksum */
3645 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
3650 static size_t disks_to_mpb_size(int disks
)
3654 size
= sizeof(struct imsm_super
);
3655 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
3656 size
+= 2 * sizeof(struct imsm_dev
);
3657 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
3658 size
+= (4 - 2) * sizeof(struct imsm_map
);
3659 /* 4 possible disk_ord_tbl's */
3660 size
+= 4 * (disks
- 1) * sizeof(__u32
);
3661 /* maximum bbm log */
3662 size
+= sizeof(struct bbm_log
);
3667 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
,
3668 unsigned long long data_offset
)
3670 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
3673 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
3676 static void free_devlist(struct intel_super
*super
)
3678 struct intel_dev
*dv
;
3680 while (super
->devlist
) {
3681 dv
= super
->devlist
->next
;
3682 free(super
->devlist
->dev
);
3683 free(super
->devlist
);
3684 super
->devlist
= dv
;
3688 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
3690 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
3693 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
)
3697 * 0 same, or first was empty, and second was copied
3698 * 1 second had wrong number
3700 * 3 wrong other info
3702 struct intel_super
*first
= st
->sb
;
3703 struct intel_super
*sec
= tst
->sb
;
3710 /* in platform dependent environment test if the disks
3711 * use the same Intel hba
3712 * If not on Intel hba at all, allow anything.
3714 if (!check_env("IMSM_NO_PLATFORM") && first
->hba
&& sec
->hba
) {
3715 if (first
->hba
->type
!= sec
->hba
->type
) {
3717 "HBAs of devices do not match %s != %s\n",
3718 get_sys_dev_type(first
->hba
->type
),
3719 get_sys_dev_type(sec
->hba
->type
));
3722 if (first
->orom
!= sec
->orom
) {
3724 "HBAs of devices do not match %s != %s\n",
3725 first
->hba
->pci_id
, sec
->hba
->pci_id
);
3730 /* if an anchor does not have num_raid_devs set then it is a free
3733 if (first
->anchor
->num_raid_devs
> 0 &&
3734 sec
->anchor
->num_raid_devs
> 0) {
3735 /* Determine if these disks might ever have been
3736 * related. Further disambiguation can only take place
3737 * in load_super_imsm_all
3739 __u32 first_family
= first
->anchor
->orig_family_num
;
3740 __u32 sec_family
= sec
->anchor
->orig_family_num
;
3742 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
3743 MAX_SIGNATURE_LENGTH
) != 0)
3746 if (first_family
== 0)
3747 first_family
= first
->anchor
->family_num
;
3748 if (sec_family
== 0)
3749 sec_family
= sec
->anchor
->family_num
;
3751 if (first_family
!= sec_family
)
3756 /* if 'first' is a spare promote it to a populated mpb with sec's
3759 if (first
->anchor
->num_raid_devs
== 0 &&
3760 sec
->anchor
->num_raid_devs
> 0) {
3762 struct intel_dev
*dv
;
3763 struct imsm_dev
*dev
;
3765 /* we need to copy raid device info from sec if an allocation
3766 * fails here we don't associate the spare
3768 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++) {
3769 dv
= xmalloc(sizeof(*dv
));
3770 dev
= xmalloc(sizeof_imsm_dev(get_imsm_dev(sec
, i
), 1));
3773 dv
->next
= first
->devlist
;
3774 first
->devlist
= dv
;
3776 if (i
< sec
->anchor
->num_raid_devs
) {
3777 /* allocation failure */
3778 free_devlist(first
);
3779 pr_err("imsm: failed to associate spare\n");
3782 first
->anchor
->num_raid_devs
= sec
->anchor
->num_raid_devs
;
3783 first
->anchor
->orig_family_num
= sec
->anchor
->orig_family_num
;
3784 first
->anchor
->family_num
= sec
->anchor
->family_num
;
3785 memcpy(first
->anchor
->sig
, sec
->anchor
->sig
, MAX_SIGNATURE_LENGTH
);
3786 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++)
3787 imsm_copy_dev(get_imsm_dev(first
, i
), get_imsm_dev(sec
, i
));
3793 static void fd2devname(int fd
, char *name
)
3797 char dname
[PATH_MAX
];
3802 if (fstat(fd
, &st
) != 0)
3804 sprintf(path
, "/sys/dev/block/%d:%d",
3805 major(st
.st_rdev
), minor(st
.st_rdev
));
3807 rv
= readlink(path
, dname
, sizeof(dname
)-1);
3812 nm
= strrchr(dname
, '/');
3815 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
3819 static int nvme_get_serial(int fd
, void *buf
, size_t buf_len
)
3822 char *name
= fd2kname(fd
);
3827 if (strncmp(name
, "nvme", 4) != 0)
3830 snprintf(path
, sizeof(path
) - 1, "/sys/block/%s/device/serial", name
);
3832 return load_sys(path
, buf
, buf_len
);
3835 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
3837 static int imsm_read_serial(int fd
, char *devname
,
3838 __u8 serial
[MAX_RAID_SERIAL_LEN
])
3847 memset(buf
, 0, sizeof(buf
));
3849 rv
= nvme_get_serial(fd
, buf
, sizeof(buf
));
3852 rv
= scsi_get_serial(fd
, buf
, sizeof(buf
));
3854 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
3855 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3856 fd2devname(fd
, (char *) serial
);
3862 pr_err("Failed to retrieve serial for %s\n",
3867 /* trim all whitespace and non-printable characters and convert
3870 for (i
= 0, dest
= buf
; i
< sizeof(buf
) && buf
[i
]; i
++) {
3873 /* ':' is reserved for use in placeholder serial
3874 * numbers for missing disks
3885 /* truncate leading characters */
3886 if (len
> MAX_RAID_SERIAL_LEN
) {
3887 dest
+= len
- MAX_RAID_SERIAL_LEN
;
3888 len
= MAX_RAID_SERIAL_LEN
;
3891 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3892 memcpy(serial
, dest
, len
);
3897 static int serialcmp(__u8
*s1
, __u8
*s2
)
3899 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
3902 static void serialcpy(__u8
*dest
, __u8
*src
)
3904 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
3907 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
3911 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3912 if (serialcmp(dl
->serial
, serial
) == 0)
3918 static struct imsm_disk
*
3919 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
3923 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3924 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3926 if (serialcmp(disk
->serial
, serial
) == 0) {
3937 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3939 struct imsm_disk
*disk
;
3944 __u8 serial
[MAX_RAID_SERIAL_LEN
];
3946 rv
= imsm_read_serial(fd
, devname
, serial
);
3951 dl
= xcalloc(1, sizeof(*dl
));
3954 dl
->major
= major(stb
.st_rdev
);
3955 dl
->minor
= minor(stb
.st_rdev
);
3956 dl
->next
= super
->disks
;
3957 dl
->fd
= keep_fd
? fd
: -1;
3958 assert(super
->disks
== NULL
);
3960 serialcpy(dl
->serial
, serial
);
3963 fd2devname(fd
, name
);
3965 dl
->devname
= xstrdup(devname
);
3967 dl
->devname
= xstrdup(name
);
3969 /* look up this disk's index in the current anchor */
3970 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
3973 /* only set index on disks that are a member of a
3974 * populated contianer, i.e. one with raid_devs
3976 if (is_failed(&dl
->disk
))
3978 else if (is_spare(&dl
->disk
) || is_journal(&dl
->disk
))
3985 /* When migrating map0 contains the 'destination' state while map1
3986 * contains the current state. When not migrating map0 contains the
3987 * current state. This routine assumes that map[0].map_state is set to
3988 * the current array state before being called.
3990 * Migration is indicated by one of the following states
3991 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
3992 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
3993 * map1state=unitialized)
3994 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
3996 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
3997 * map1state=degraded)
3998 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
4001 static void migrate(struct imsm_dev
*dev
, struct intel_super
*super
,
4002 __u8 to_state
, int migr_type
)
4004 struct imsm_map
*dest
;
4005 struct imsm_map
*src
= get_imsm_map(dev
, MAP_0
);
4007 dev
->vol
.migr_state
= 1;
4008 set_migr_type(dev
, migr_type
);
4009 dev
->vol
.curr_migr_unit
= 0;
4010 dest
= get_imsm_map(dev
, MAP_1
);
4012 /* duplicate and then set the target end state in map[0] */
4013 memcpy(dest
, src
, sizeof_imsm_map(src
));
4014 if (migr_type
== MIGR_REBUILD
|| migr_type
== MIGR_GEN_MIGR
) {
4018 for (i
= 0; i
< src
->num_members
; i
++) {
4019 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
4020 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
4024 if (migr_type
== MIGR_GEN_MIGR
)
4025 /* Clear migration record */
4026 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
4028 src
->map_state
= to_state
;
4031 static void end_migration(struct imsm_dev
*dev
, struct intel_super
*super
,
4034 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
4035 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
== 0 ?
4039 /* merge any IMSM_ORD_REBUILD bits that were not successfully
4040 * completed in the last migration.
4042 * FIXME add support for raid-level-migration
4044 if (map_state
!= map
->map_state
&& (is_gen_migration(dev
) == 0) &&
4045 prev
->map_state
!= IMSM_T_STATE_UNINITIALIZED
) {
4046 /* when final map state is other than expected
4047 * merge maps (not for migration)
4051 for (i
= 0; i
< prev
->num_members
; i
++)
4052 for (j
= 0; j
< map
->num_members
; j
++)
4053 /* during online capacity expansion
4054 * disks position can be changed
4055 * if takeover is used
4057 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
4058 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
4059 map
->disk_ord_tbl
[j
] |=
4060 prev
->disk_ord_tbl
[i
];
4063 failed
= imsm_count_failed(super
, dev
, MAP_0
);
4064 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
4067 dev
->vol
.migr_state
= 0;
4068 set_migr_type(dev
, 0);
4069 dev
->vol
.curr_migr_unit
= 0;
4070 map
->map_state
= map_state
;
4073 static int parse_raid_devices(struct intel_super
*super
)
4076 struct imsm_dev
*dev_new
;
4077 size_t len
, len_migr
;
4079 size_t space_needed
= 0;
4080 struct imsm_super
*mpb
= super
->anchor
;
4082 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
4083 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
4084 struct intel_dev
*dv
;
4086 len
= sizeof_imsm_dev(dev_iter
, 0);
4087 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
4089 space_needed
+= len_migr
- len
;
4091 dv
= xmalloc(sizeof(*dv
));
4092 if (max_len
< len_migr
)
4094 if (max_len
> len_migr
)
4095 space_needed
+= max_len
- len_migr
;
4096 dev_new
= xmalloc(max_len
);
4097 imsm_copy_dev(dev_new
, dev_iter
);
4100 dv
->next
= super
->devlist
;
4101 super
->devlist
= dv
;
4104 /* ensure that super->buf is large enough when all raid devices
4107 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
4110 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
,
4111 super
->sector_size
);
4112 if (posix_memalign(&buf
, MAX_SECTOR_SIZE
, len
) != 0)
4115 memcpy(buf
, super
->buf
, super
->len
);
4116 memset(buf
+ super
->len
, 0, len
- super
->len
);
4122 super
->extra_space
+= space_needed
;
4127 /*******************************************************************************
4128 * Function: check_mpb_migr_compatibility
4129 * Description: Function checks for unsupported migration features:
4130 * - migration optimization area (pba_of_lba0)
4131 * - descending reshape (ascending_migr)
4133 * super : imsm metadata information
4135 * 0 : migration is compatible
4136 * -1 : migration is not compatible
4137 ******************************************************************************/
4138 int check_mpb_migr_compatibility(struct intel_super
*super
)
4140 struct imsm_map
*map0
, *map1
;
4141 struct migr_record
*migr_rec
= super
->migr_rec
;
4144 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
4145 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
4148 dev_iter
->vol
.migr_state
== 1 &&
4149 dev_iter
->vol
.migr_type
== MIGR_GEN_MIGR
) {
4150 /* This device is migrating */
4151 map0
= get_imsm_map(dev_iter
, MAP_0
);
4152 map1
= get_imsm_map(dev_iter
, MAP_1
);
4153 if (pba_of_lba0(map0
) != pba_of_lba0(map1
))
4154 /* migration optimization area was used */
4156 if (migr_rec
->ascending_migr
== 0
4157 && migr_rec
->dest_depth_per_unit
> 0)
4158 /* descending reshape not supported yet */
4165 static void __free_imsm(struct intel_super
*super
, int free_disks
);
4167 /* load_imsm_mpb - read matrix metadata
4168 * allocates super->mpb to be freed by free_imsm
4170 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
4172 unsigned long long dsize
;
4173 unsigned long long sectors
;
4174 unsigned int sector_size
= super
->sector_size
;
4176 struct imsm_super
*anchor
;
4179 get_dev_size(fd
, NULL
, &dsize
);
4180 if (dsize
< 2*sector_size
) {
4182 pr_err("%s: device to small for imsm\n",
4187 if (lseek64(fd
, dsize
- (sector_size
* 2), SEEK_SET
) < 0) {
4189 pr_err("Cannot seek to anchor block on %s: %s\n",
4190 devname
, strerror(errno
));
4194 if (posix_memalign((void **)&anchor
, sector_size
, sector_size
) != 0) {
4196 pr_err("Failed to allocate imsm anchor buffer on %s\n", devname
);
4199 if ((unsigned int)read(fd
, anchor
, sector_size
) != sector_size
) {
4201 pr_err("Cannot read anchor block on %s: %s\n",
4202 devname
, strerror(errno
));
4207 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
4209 pr_err("no IMSM anchor on %s\n", devname
);
4214 __free_imsm(super
, 0);
4215 /* reload capability and hba */
4217 /* capability and hba must be updated with new super allocation */
4218 find_intel_hba_capability(fd
, super
, devname
);
4219 super
->len
= ROUND_UP(anchor
->mpb_size
, sector_size
);
4220 if (posix_memalign(&super
->buf
, MAX_SECTOR_SIZE
, super
->len
) != 0) {
4222 pr_err("unable to allocate %zu byte mpb buffer\n",
4227 memcpy(super
->buf
, anchor
, sector_size
);
4229 sectors
= mpb_sectors(anchor
, sector_size
) - 1;
4232 if (posix_memalign(&super
->migr_rec_buf
, MAX_SECTOR_SIZE
,
4233 MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
) != 0) {
4234 pr_err("could not allocate migr_rec buffer\n");
4238 super
->clean_migration_record_by_mdmon
= 0;
4241 check_sum
= __gen_imsm_checksum(super
->anchor
);
4242 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
4244 pr_err("IMSM checksum %x != %x on %s\n",
4246 __le32_to_cpu(super
->anchor
->check_sum
),
4254 /* read the extended mpb */
4255 if (lseek64(fd
, dsize
- (sector_size
* (2 + sectors
)), SEEK_SET
) < 0) {
4257 pr_err("Cannot seek to extended mpb on %s: %s\n",
4258 devname
, strerror(errno
));
4262 if ((unsigned int)read(fd
, super
->buf
+ sector_size
,
4263 super
->len
- sector_size
) != super
->len
- sector_size
) {
4265 pr_err("Cannot read extended mpb on %s: %s\n",
4266 devname
, strerror(errno
));
4270 check_sum
= __gen_imsm_checksum(super
->anchor
);
4271 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
4273 pr_err("IMSM checksum %x != %x on %s\n",
4274 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
4282 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
);
4284 /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
4285 static void clear_hi(struct intel_super
*super
)
4287 struct imsm_super
*mpb
= super
->anchor
;
4289 if (mpb
->attributes
& MPB_ATTRIB_2TB_DISK
)
4291 for (i
= 0; i
< mpb
->num_disks
; ++i
) {
4292 struct imsm_disk
*disk
= &mpb
->disk
[i
];
4293 disk
->total_blocks_hi
= 0;
4295 for (i
= 0; i
< mpb
->num_raid_devs
; ++i
) {
4296 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
4299 for (n
= 0; n
< 2; ++n
) {
4300 struct imsm_map
*map
= get_imsm_map(dev
, n
);
4303 map
->pba_of_lba0_hi
= 0;
4304 map
->blocks_per_member_hi
= 0;
4305 map
->num_data_stripes_hi
= 0;
4311 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
4315 err
= load_imsm_mpb(fd
, super
, devname
);
4318 if (super
->sector_size
== 4096)
4319 convert_from_4k(super
);
4320 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
4323 err
= parse_raid_devices(super
);
4326 err
= load_bbm_log(super
);
4331 static void __free_imsm_disk(struct dl
*d
)
4343 static void free_imsm_disks(struct intel_super
*super
)
4347 while (super
->disks
) {
4349 super
->disks
= d
->next
;
4350 __free_imsm_disk(d
);
4352 while (super
->disk_mgmt_list
) {
4353 d
= super
->disk_mgmt_list
;
4354 super
->disk_mgmt_list
= d
->next
;
4355 __free_imsm_disk(d
);
4357 while (super
->missing
) {
4359 super
->missing
= d
->next
;
4360 __free_imsm_disk(d
);
4365 /* free all the pieces hanging off of a super pointer */
4366 static void __free_imsm(struct intel_super
*super
, int free_disks
)
4368 struct intel_hba
*elem
, *next
;
4374 /* unlink capability description */
4376 if (super
->migr_rec_buf
) {
4377 free(super
->migr_rec_buf
);
4378 super
->migr_rec_buf
= NULL
;
4381 free_imsm_disks(super
);
4382 free_devlist(super
);
4386 free((void *)elem
->path
);
4392 free(super
->bbm_log
);
4396 static void free_imsm(struct intel_super
*super
)
4398 __free_imsm(super
, 1);
4399 free(super
->bb
.entries
);
4403 static void free_super_imsm(struct supertype
*st
)
4405 struct intel_super
*super
= st
->sb
;
4414 static struct intel_super
*alloc_super(void)
4416 struct intel_super
*super
= xcalloc(1, sizeof(*super
));
4418 super
->current_vol
= -1;
4419 super
->create_offset
= ~((unsigned long long) 0);
4421 super
->bb
.entries
= xmalloc(BBM_LOG_MAX_ENTRIES
*
4422 sizeof(struct md_bb_entry
));
4423 if (!super
->bb
.entries
) {
4432 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
4434 static int find_intel_hba_capability(int fd
, struct intel_super
*super
, char *devname
)
4436 struct sys_dev
*hba_name
;
4439 if (fd
< 0 || check_env("IMSM_NO_PLATFORM")) {
4444 hba_name
= find_disk_attached_hba(fd
, NULL
);
4447 pr_err("%s is not attached to Intel(R) RAID controller.\n",
4451 rv
= attach_hba_to_super(super
, hba_name
);
4454 struct intel_hba
*hba
= super
->hba
;
4456 pr_err("%s is attached to Intel(R) %s %s (%s),\n"
4457 " but the container is assigned to Intel(R) %s %s (",
4459 get_sys_dev_type(hba_name
->type
),
4460 hba_name
->type
== SYS_DEV_VMD
? "domain" : "RAID controller",
4461 hba_name
->pci_id
? : "Err!",
4462 get_sys_dev_type(super
->hba
->type
),
4463 hba
->type
== SYS_DEV_VMD
? "domain" : "RAID controller");
4466 fprintf(stderr
, "%s", hba
->pci_id
? : "Err!");
4468 fprintf(stderr
, ", ");
4471 fprintf(stderr
, ").\n"
4472 " Mixing devices attached to different %s is not allowed.\n",
4473 hba_name
->type
== SYS_DEV_VMD
? "VMD domains" : "controllers");
4477 super
->orom
= find_imsm_capability(hba_name
);
4484 /* find_missing - helper routine for load_super_imsm_all that identifies
4485 * disks that have disappeared from the system. This routine relies on
4486 * the mpb being uptodate, which it is at load time.
4488 static int find_missing(struct intel_super
*super
)
4491 struct imsm_super
*mpb
= super
->anchor
;
4493 struct imsm_disk
*disk
;
4495 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4496 disk
= __get_imsm_disk(mpb
, i
);
4497 dl
= serial_to_dl(disk
->serial
, super
);
4501 dl
= xmalloc(sizeof(*dl
));
4505 dl
->devname
= xstrdup("missing");
4507 serialcpy(dl
->serial
, disk
->serial
);
4510 dl
->next
= super
->missing
;
4511 super
->missing
= dl
;
4517 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
4519 struct intel_disk
*idisk
= disk_list
;
4522 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
4524 idisk
= idisk
->next
;
4530 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
4531 struct intel_super
*super
,
4532 struct intel_disk
**disk_list
)
4534 struct imsm_disk
*d
= &super
->disks
->disk
;
4535 struct imsm_super
*mpb
= super
->anchor
;
4538 for (i
= 0; i
< tbl_size
; i
++) {
4539 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
4540 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
4542 if (tbl_mpb
->family_num
== mpb
->family_num
) {
4543 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
4544 dprintf("mpb from %d:%d matches %d:%d\n",
4545 super
->disks
->major
,
4546 super
->disks
->minor
,
4547 table
[i
]->disks
->major
,
4548 table
[i
]->disks
->minor
);
4552 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
4553 is_configured(d
) == is_configured(tbl_d
)) &&
4554 tbl_mpb
->generation_num
< mpb
->generation_num
) {
4555 /* current version of the mpb is a
4556 * better candidate than the one in
4557 * super_table, but copy over "cross
4558 * generational" status
4560 struct intel_disk
*idisk
;
4562 dprintf("mpb from %d:%d replaces %d:%d\n",
4563 super
->disks
->major
,
4564 super
->disks
->minor
,
4565 table
[i
]->disks
->major
,
4566 table
[i
]->disks
->minor
);
4568 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
4569 if (idisk
&& is_failed(&idisk
->disk
))
4570 tbl_d
->status
|= FAILED_DISK
;
4573 struct intel_disk
*idisk
;
4574 struct imsm_disk
*disk
;
4576 /* tbl_mpb is more up to date, but copy
4577 * over cross generational status before
4580 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
4581 if (disk
&& is_failed(disk
))
4582 d
->status
|= FAILED_DISK
;
4584 idisk
= disk_list_get(d
->serial
, *disk_list
);
4587 if (disk
&& is_configured(disk
))
4588 idisk
->disk
.status
|= CONFIGURED_DISK
;
4591 dprintf("mpb from %d:%d prefer %d:%d\n",
4592 super
->disks
->major
,
4593 super
->disks
->minor
,
4594 table
[i
]->disks
->major
,
4595 table
[i
]->disks
->minor
);
4603 table
[tbl_size
++] = super
;
4607 /* update/extend the merged list of imsm_disk records */
4608 for (j
= 0; j
< mpb
->num_disks
; j
++) {
4609 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
4610 struct intel_disk
*idisk
;
4612 idisk
= disk_list_get(disk
->serial
, *disk_list
);
4614 idisk
->disk
.status
|= disk
->status
;
4615 if (is_configured(&idisk
->disk
) ||
4616 is_failed(&idisk
->disk
))
4617 idisk
->disk
.status
&= ~(SPARE_DISK
);
4619 idisk
= xcalloc(1, sizeof(*idisk
));
4620 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
4621 idisk
->disk
= *disk
;
4622 idisk
->next
= *disk_list
;
4626 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
4633 static struct intel_super
*
4634 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
4637 struct imsm_super
*mpb
= super
->anchor
;
4641 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4642 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
4643 struct intel_disk
*idisk
;
4645 idisk
= disk_list_get(disk
->serial
, disk_list
);
4647 if (idisk
->owner
== owner
||
4648 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
4651 dprintf("'%.16s' owner %d != %d\n",
4652 disk
->serial
, idisk
->owner
,
4655 dprintf("unknown disk %x [%d]: %.16s\n",
4656 __le32_to_cpu(mpb
->family_num
), i
,
4662 if (ok_count
== mpb
->num_disks
)
4667 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
4669 struct intel_super
*s
;
4671 for (s
= super_list
; s
; s
= s
->next
) {
4672 if (family_num
!= s
->anchor
->family_num
)
4674 pr_err("Conflict, offlining family %#x on '%s'\n",
4675 __le32_to_cpu(family_num
), s
->disks
->devname
);
4679 static struct intel_super
*
4680 imsm_thunderdome(struct intel_super
**super_list
, int len
)
4682 struct intel_super
*super_table
[len
];
4683 struct intel_disk
*disk_list
= NULL
;
4684 struct intel_super
*champion
, *spare
;
4685 struct intel_super
*s
, **del
;
4690 memset(super_table
, 0, sizeof(super_table
));
4691 for (s
= *super_list
; s
; s
= s
->next
)
4692 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
4694 for (i
= 0; i
< tbl_size
; i
++) {
4695 struct imsm_disk
*d
;
4696 struct intel_disk
*idisk
;
4697 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
4700 d
= &s
->disks
->disk
;
4702 /* 'd' must appear in merged disk list for its
4703 * configuration to be valid
4705 idisk
= disk_list_get(d
->serial
, disk_list
);
4706 if (idisk
&& idisk
->owner
== i
)
4707 s
= validate_members(s
, disk_list
, i
);
4712 dprintf("marking family: %#x from %d:%d offline\n",
4714 super_table
[i
]->disks
->major
,
4715 super_table
[i
]->disks
->minor
);
4719 /* This is where the mdadm implementation differs from the Windows
4720 * driver which has no strict concept of a container. We can only
4721 * assemble one family from a container, so when returning a prodigal
4722 * array member to this system the code will not be able to disambiguate
4723 * the container contents that should be assembled ("foreign" versus
4724 * "local"). It requires user intervention to set the orig_family_num
4725 * to a new value to establish a new container. The Windows driver in
4726 * this situation fixes up the volume name in place and manages the
4727 * foreign array as an independent entity.
4732 for (i
= 0; i
< tbl_size
; i
++) {
4733 struct intel_super
*tbl_ent
= super_table
[i
];
4739 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
4744 if (s
&& !is_spare
) {
4745 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
4747 } else if (!s
&& !is_spare
)
4760 pr_err("Chose family %#x on '%s', assemble conflicts to new container with '--update=uuid'\n",
4761 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
4763 /* collect all dl's onto 'champion', and update them to
4764 * champion's version of the status
4766 for (s
= *super_list
; s
; s
= s
->next
) {
4767 struct imsm_super
*mpb
= champion
->anchor
;
4768 struct dl
*dl
= s
->disks
;
4773 mpb
->attributes
|= s
->anchor
->attributes
& MPB_ATTRIB_2TB_DISK
;
4775 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4776 struct imsm_disk
*disk
;
4778 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
4781 /* only set index on disks that are a member of
4782 * a populated contianer, i.e. one with
4785 if (is_failed(&dl
->disk
))
4787 else if (is_spare(&dl
->disk
))
4793 if (i
>= mpb
->num_disks
) {
4794 struct intel_disk
*idisk
;
4796 idisk
= disk_list_get(dl
->serial
, disk_list
);
4797 if (idisk
&& is_spare(&idisk
->disk
) &&
4798 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
4806 dl
->next
= champion
->disks
;
4807 champion
->disks
= dl
;
4811 /* delete 'champion' from super_list */
4812 for (del
= super_list
; *del
; ) {
4813 if (*del
== champion
) {
4814 *del
= (*del
)->next
;
4817 del
= &(*del
)->next
;
4819 champion
->next
= NULL
;
4823 struct intel_disk
*idisk
= disk_list
;
4825 disk_list
= disk_list
->next
;
4833 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
);
4834 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
4835 int major
, int minor
, int keep_fd
);
4837 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4838 int *max
, int keep_fd
);
4840 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
4841 char *devname
, struct md_list
*devlist
,
4844 struct intel_super
*super_list
= NULL
;
4845 struct intel_super
*super
= NULL
;
4850 /* 'fd' is an opened container */
4851 err
= get_sra_super_block(fd
, &super_list
, devname
, &i
, keep_fd
);
4853 /* get super block from devlist devices */
4854 err
= get_devlist_super_block(devlist
, &super_list
, &i
, keep_fd
);
4857 /* all mpbs enter, maybe one leaves */
4858 super
= imsm_thunderdome(&super_list
, i
);
4864 if (find_missing(super
) != 0) {
4870 /* load migration record */
4871 err
= load_imsm_migr_rec(super
, NULL
);
4873 /* migration is in progress,
4874 * but migr_rec cannot be loaded,
4880 /* Check migration compatibility */
4881 if (err
== 0 && check_mpb_migr_compatibility(super
) != 0) {
4882 pr_err("Unsupported migration detected");
4884 fprintf(stderr
, " on %s\n", devname
);
4886 fprintf(stderr
, " (IMSM).\n");
4895 while (super_list
) {
4896 struct intel_super
*s
= super_list
;
4898 super_list
= super_list
->next
;
4907 strcpy(st
->container_devnm
, fd2devnm(fd
));
4909 st
->container_devnm
[0] = 0;
4910 if (err
== 0 && st
->ss
== NULL
) {
4911 st
->ss
= &super_imsm
;
4912 st
->minor_version
= 0;
4913 st
->max_devs
= IMSM_MAX_DEVICES
;
4919 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4920 int *max
, int keep_fd
)
4922 struct md_list
*tmpdev
;
4926 for (i
= 0, tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
4927 if (tmpdev
->used
!= 1)
4929 if (tmpdev
->container
== 1) {
4931 int fd
= dev_open(tmpdev
->devname
, O_RDONLY
|O_EXCL
);
4933 pr_err("cannot open device %s: %s\n",
4934 tmpdev
->devname
, strerror(errno
));
4938 err
= get_sra_super_block(fd
, super_list
,
4939 tmpdev
->devname
, &lmax
,
4948 int major
= major(tmpdev
->st_rdev
);
4949 int minor
= minor(tmpdev
->st_rdev
);
4950 err
= get_super_block(super_list
,
4967 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
4968 int major
, int minor
, int keep_fd
)
4970 struct intel_super
*s
;
4982 sprintf(nm
, "%d:%d", major
, minor
);
4983 dfd
= dev_open(nm
, O_RDWR
);
4989 get_dev_sector_size(dfd
, NULL
, &s
->sector_size
);
4990 find_intel_hba_capability(dfd
, s
, devname
);
4991 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4993 /* retry the load if we might have raced against mdmon */
4994 if (err
== 3 && devnm
&& mdmon_running(devnm
))
4995 for (retry
= 0; retry
< 3; retry
++) {
4997 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
5003 s
->next
= *super_list
;
5011 if (dfd
>= 0 && !keep_fd
)
5018 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
)
5025 sra
= sysfs_read(fd
, NULL
, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
5029 if (sra
->array
.major_version
!= -1 ||
5030 sra
->array
.minor_version
!= -2 ||
5031 strcmp(sra
->text_version
, "imsm") != 0) {
5036 devnm
= fd2devnm(fd
);
5037 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
5038 if (get_super_block(super_list
, devnm
, devname
,
5039 sd
->disk
.major
, sd
->disk
.minor
, keep_fd
) != 0) {
5050 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
5052 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
, NULL
, 1);
5055 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
5057 struct intel_super
*super
;
5061 if (test_partition(fd
))
5062 /* IMSM not allowed on partitions */
5065 free_super_imsm(st
);
5067 super
= alloc_super();
5068 get_dev_sector_size(fd
, NULL
, &super
->sector_size
);
5071 /* Load hba and capabilities if they exist.
5072 * But do not preclude loading metadata in case capabilities or hba are
5073 * non-compliant and ignore_hw_compat is set.
5075 rv
= find_intel_hba_capability(fd
, super
, devname
);
5076 /* no orom/efi or non-intel hba of the disk */
5077 if (rv
!= 0 && st
->ignore_hw_compat
== 0) {
5079 pr_err("No OROM/EFI properties for %s\n", devname
);
5083 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
5085 /* retry the load if we might have raced against mdmon */
5087 struct mdstat_ent
*mdstat
= NULL
;
5088 char *name
= fd2kname(fd
);
5091 mdstat
= mdstat_by_component(name
);
5093 if (mdstat
&& mdmon_running(mdstat
->devnm
) && getpid() != mdmon_pid(mdstat
->devnm
)) {
5094 for (retry
= 0; retry
< 3; retry
++) {
5096 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
5102 free_mdstat(mdstat
);
5107 pr_err("Failed to load all information sections on %s\n", devname
);
5113 if (st
->ss
== NULL
) {
5114 st
->ss
= &super_imsm
;
5115 st
->minor_version
= 0;
5116 st
->max_devs
= IMSM_MAX_DEVICES
;
5119 /* load migration record */
5120 if (load_imsm_migr_rec(super
, NULL
) == 0) {
5121 /* Check for unsupported migration features */
5122 if (check_mpb_migr_compatibility(super
) != 0) {
5123 pr_err("Unsupported migration detected");
5125 fprintf(stderr
, " on %s\n", devname
);
5127 fprintf(stderr
, " (IMSM).\n");
5135 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
5137 if (info
->level
== 1)
5139 return info
->chunk_size
>> 9;
5142 static unsigned long long info_to_blocks_per_member(mdu_array_info_t
*info
,
5143 unsigned long long size
)
5145 if (info
->level
== 1)
5148 return (size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
5151 static void imsm_update_version_info(struct intel_super
*super
)
5153 /* update the version and attributes */
5154 struct imsm_super
*mpb
= super
->anchor
;
5156 struct imsm_dev
*dev
;
5157 struct imsm_map
*map
;
5160 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5161 dev
= get_imsm_dev(super
, i
);
5162 map
= get_imsm_map(dev
, MAP_0
);
5163 if (__le32_to_cpu(dev
->size_high
) > 0)
5164 mpb
->attributes
|= MPB_ATTRIB_2TB
;
5166 /* FIXME detect when an array spans a port multiplier */
5168 mpb
->attributes
|= MPB_ATTRIB_PM
;
5171 if (mpb
->num_raid_devs
> 1 ||
5172 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
5173 version
= MPB_VERSION_ATTRIBS
;
5174 switch (get_imsm_raid_level(map
)) {
5175 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
5176 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
5177 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
5178 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
5181 if (map
->num_members
>= 5)
5182 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
5183 else if (dev
->status
== DEV_CLONE_N_GO
)
5184 version
= MPB_VERSION_CNG
;
5185 else if (get_imsm_raid_level(map
) == 5)
5186 version
= MPB_VERSION_RAID5
;
5187 else if (map
->num_members
>= 3)
5188 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
5189 else if (get_imsm_raid_level(map
) == 1)
5190 version
= MPB_VERSION_RAID1
;
5192 version
= MPB_VERSION_RAID0
;
5194 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
5198 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
5200 struct imsm_super
*mpb
= super
->anchor
;
5201 char *reason
= NULL
;
5204 if (strlen(name
) > MAX_RAID_SERIAL_LEN
)
5205 reason
= "must be 16 characters or less";
5207 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5208 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
5210 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
5211 reason
= "already exists";
5216 if (reason
&& !quiet
)
5217 pr_err("imsm volume name %s\n", reason
);
5222 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
5223 struct shape
*s
, char *name
,
5224 char *homehost
, int *uuid
,
5225 long long data_offset
)
5227 /* We are creating a volume inside a pre-existing container.
5228 * so st->sb is already set.
5230 struct intel_super
*super
= st
->sb
;
5231 unsigned int sector_size
= super
->sector_size
;
5232 struct imsm_super
*mpb
= super
->anchor
;
5233 struct intel_dev
*dv
;
5234 struct imsm_dev
*dev
;
5235 struct imsm_vol
*vol
;
5236 struct imsm_map
*map
;
5237 int idx
= mpb
->num_raid_devs
;
5239 unsigned long long array_blocks
;
5240 size_t size_old
, size_new
;
5241 unsigned long long num_data_stripes
;
5242 unsigned int data_disks
;
5243 unsigned long long size_per_member
;
5245 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
5246 pr_err("This imsm-container already has the maximum of %d volumes\n", super
->orom
->vpa
);
5250 /* ensure the mpb is large enough for the new data */
5251 size_old
= __le32_to_cpu(mpb
->mpb_size
);
5252 size_new
= disks_to_mpb_size(info
->nr_disks
);
5253 if (size_new
> size_old
) {
5255 size_t size_round
= ROUND_UP(size_new
, sector_size
);
5257 if (posix_memalign(&mpb_new
, sector_size
, size_round
) != 0) {
5258 pr_err("could not allocate new mpb\n");
5261 if (posix_memalign(&super
->migr_rec_buf
, MAX_SECTOR_SIZE
,
5262 MIGR_REC_BUF_SECTORS
*
5263 MAX_SECTOR_SIZE
) != 0) {
5264 pr_err("could not allocate migr_rec buffer\n");
5270 memcpy(mpb_new
, mpb
, size_old
);
5273 super
->anchor
= mpb_new
;
5274 mpb
->mpb_size
= __cpu_to_le32(size_new
);
5275 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
5276 super
->len
= size_round
;
5278 super
->current_vol
= idx
;
5280 /* handle 'failed_disks' by either:
5281 * a) create dummy disk entries in the table if this the first
5282 * volume in the array. We add them here as this is the only
5283 * opportunity to add them. add_to_super_imsm_volume()
5284 * handles the non-failed disks and continues incrementing
5286 * b) validate that 'failed_disks' matches the current number
5287 * of missing disks if the container is populated
5289 if (super
->current_vol
== 0) {
5291 for (i
= 0; i
< info
->failed_disks
; i
++) {
5292 struct imsm_disk
*disk
;
5295 disk
= __get_imsm_disk(mpb
, i
);
5296 disk
->status
= CONFIGURED_DISK
| FAILED_DISK
;
5297 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
5298 snprintf((char *) disk
->serial
, MAX_RAID_SERIAL_LEN
,
5299 "missing:%d", (__u8
)i
);
5301 find_missing(super
);
5306 for (d
= super
->missing
; d
; d
= d
->next
)
5308 if (info
->failed_disks
> missing
) {
5309 pr_err("unable to add 'missing' disk to container\n");
5314 if (!check_name(super
, name
, 0))
5316 dv
= xmalloc(sizeof(*dv
));
5317 dev
= xcalloc(1, sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
5318 strncpy((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
5319 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
5320 info
->layout
, info
->chunk_size
,
5321 s
->size
* BLOCKS_PER_KB
);
5322 data_disks
= get_data_disks(info
->level
, info
->layout
,
5324 array_blocks
= round_size_to_mb(array_blocks
, data_disks
);
5325 size_per_member
= array_blocks
/ data_disks
;
5327 dev
->size_low
= __cpu_to_le32((__u32
) array_blocks
);
5328 dev
->size_high
= __cpu_to_le32((__u32
) (array_blocks
>> 32));
5329 dev
->status
= (DEV_READ_COALESCING
| DEV_WRITE_COALESCING
);
5331 vol
->migr_state
= 0;
5332 set_migr_type(dev
, MIGR_INIT
);
5333 vol
->dirty
= !info
->state
;
5334 vol
->curr_migr_unit
= 0;
5335 map
= get_imsm_map(dev
, MAP_0
);
5336 set_pba_of_lba0(map
, super
->create_offset
);
5337 set_blocks_per_member(map
, info_to_blocks_per_member(info
,
5340 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
5341 map
->failed_disk_num
= ~0;
5342 if (info
->level
> 0)
5343 map
->map_state
= (info
->state
? IMSM_T_STATE_NORMAL
5344 : IMSM_T_STATE_UNINITIALIZED
);
5346 map
->map_state
= info
->failed_disks
? IMSM_T_STATE_FAILED
:
5347 IMSM_T_STATE_NORMAL
;
5350 if (info
->level
== 1 && info
->raid_disks
> 2) {
5353 pr_err("imsm does not support more than 2 disksin a raid1 volume\n");
5357 map
->raid_level
= info
->level
;
5358 if (info
->level
== 10) {
5359 map
->raid_level
= 1;
5360 map
->num_domains
= info
->raid_disks
/ 2;
5361 } else if (info
->level
== 1)
5362 map
->num_domains
= info
->raid_disks
;
5364 map
->num_domains
= 1;
5366 /* info->size is only int so use the 'size' parameter instead */
5367 num_data_stripes
= size_per_member
/ info_to_blocks_per_strip(info
);
5368 num_data_stripes
/= map
->num_domains
;
5369 set_num_data_stripes(map
, num_data_stripes
);
5371 map
->num_members
= info
->raid_disks
;
5372 for (i
= 0; i
< map
->num_members
; i
++) {
5373 /* initialized in add_to_super */
5374 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
5376 mpb
->num_raid_devs
++;
5377 mpb
->num_raid_devs_created
++;
5378 dev
->my_vol_raid_dev_num
= mpb
->num_raid_devs_created
;
5380 if (s
->consistency_policy
<= CONSISTENCY_POLICY_RESYNC
) {
5381 dev
->rwh_policy
= RWH_OFF
;
5382 } else if (s
->consistency_policy
== CONSISTENCY_POLICY_PPL
) {
5383 dev
->rwh_policy
= RWH_DISTRIBUTED
;
5387 pr_err("imsm does not support consistency policy %s\n",
5388 map_num(consistency_policies
, s
->consistency_policy
));
5393 dv
->index
= super
->current_vol
;
5394 dv
->next
= super
->devlist
;
5395 super
->devlist
= dv
;
5397 imsm_update_version_info(super
);
5402 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
5403 struct shape
*s
, char *name
,
5404 char *homehost
, int *uuid
,
5405 unsigned long long data_offset
)
5407 /* This is primarily called by Create when creating a new array.
5408 * We will then get add_to_super called for each component, and then
5409 * write_init_super called to write it out to each device.
5410 * For IMSM, Create can create on fresh devices or on a pre-existing
5412 * To create on a pre-existing array a different method will be called.
5413 * This one is just for fresh drives.
5415 struct intel_super
*super
;
5416 struct imsm_super
*mpb
;
5420 if (data_offset
!= INVALID_SECTORS
) {
5421 pr_err("data-offset not supported by imsm\n");
5426 return init_super_imsm_volume(st
, info
, s
, name
, homehost
, uuid
,
5430 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
5432 mpb_size
= MAX_SECTOR_SIZE
;
5434 super
= alloc_super();
5436 posix_memalign(&super
->buf
, MAX_SECTOR_SIZE
, mpb_size
) != 0) {
5441 pr_err("could not allocate superblock\n");
5444 if (posix_memalign(&super
->migr_rec_buf
, MAX_SECTOR_SIZE
,
5445 MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
) != 0) {
5446 pr_err("could not allocate migr_rec buffer\n");
5451 memset(super
->buf
, 0, mpb_size
);
5453 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5457 /* zeroing superblock */
5461 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
5463 version
= (char *) mpb
->sig
;
5464 strcpy(version
, MPB_SIGNATURE
);
5465 version
+= strlen(MPB_SIGNATURE
);
5466 strcpy(version
, MPB_VERSION_RAID0
);
5471 static int drive_validate_sector_size(struct intel_super
*super
, struct dl
*dl
)
5473 unsigned int member_sector_size
;
5476 pr_err("Invalid file descriptor for %s\n", dl
->devname
);
5480 if (!get_dev_sector_size(dl
->fd
, dl
->devname
, &member_sector_size
))
5482 if (member_sector_size
!= super
->sector_size
)
5487 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
5488 int fd
, char *devname
)
5490 struct intel_super
*super
= st
->sb
;
5491 struct imsm_super
*mpb
= super
->anchor
;
5492 struct imsm_disk
*_disk
;
5493 struct imsm_dev
*dev
;
5494 struct imsm_map
*map
;
5498 dev
= get_imsm_dev(super
, super
->current_vol
);
5499 map
= get_imsm_map(dev
, MAP_0
);
5501 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
5502 pr_err("%s: Cannot add spare devices to IMSM volume\n",
5508 /* we're doing autolayout so grab the pre-marked (in
5509 * validate_geometry) raid_disk
5511 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5512 if (dl
->raiddisk
== dk
->raid_disk
)
5515 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5516 if (dl
->major
== dk
->major
&&
5517 dl
->minor
== dk
->minor
)
5522 pr_err("%s is not a member of the same container\n", devname
);
5526 if (!drive_validate_sector_size(super
, dl
)) {
5527 pr_err("Combining drives of different sector size in one volume is not allowed\n");
5531 /* add a pristine spare to the metadata */
5532 if (dl
->index
< 0) {
5533 dl
->index
= super
->anchor
->num_disks
;
5534 super
->anchor
->num_disks
++;
5536 /* Check the device has not already been added */
5537 slot
= get_imsm_disk_slot(map
, dl
->index
);
5539 (get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
) & IMSM_ORD_REBUILD
) == 0) {
5540 pr_err("%s has been included in this array twice\n",
5544 set_imsm_ord_tbl_ent(map
, dk
->raid_disk
, dl
->index
);
5545 dl
->disk
.status
= CONFIGURED_DISK
;
5547 /* update size of 'missing' disks to be at least as large as the
5548 * largest acitve member (we only have dummy missing disks when
5549 * creating the first volume)
5551 if (super
->current_vol
== 0) {
5552 for (df
= super
->missing
; df
; df
= df
->next
) {
5553 if (total_blocks(&dl
->disk
) > total_blocks(&df
->disk
))
5554 set_total_blocks(&df
->disk
, total_blocks(&dl
->disk
));
5555 _disk
= __get_imsm_disk(mpb
, df
->index
);
5560 /* refresh unset/failed slots to point to valid 'missing' entries */
5561 for (df
= super
->missing
; df
; df
= df
->next
)
5562 for (slot
= 0; slot
< mpb
->num_disks
; slot
++) {
5563 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
5565 if ((ord
& IMSM_ORD_REBUILD
) == 0)
5567 set_imsm_ord_tbl_ent(map
, slot
, df
->index
| IMSM_ORD_REBUILD
);
5568 if (is_gen_migration(dev
)) {
5569 struct imsm_map
*map2
= get_imsm_map(dev
,
5571 int slot2
= get_imsm_disk_slot(map2
, df
->index
);
5572 if (slot2
< map2
->num_members
&& slot2
>= 0) {
5573 __u32 ord2
= get_imsm_ord_tbl_ent(dev
,
5576 if ((unsigned)df
->index
==
5578 set_imsm_ord_tbl_ent(map2
,
5584 dprintf("set slot:%d to missing disk:%d\n", slot
, df
->index
);
5588 /* if we are creating the first raid device update the family number */
5589 if (super
->current_vol
== 0) {
5591 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
5593 _disk
= __get_imsm_disk(mpb
, dl
->index
);
5594 if (!_dev
|| !_disk
) {
5595 pr_err("BUG mpb setup error\n");
5601 sum
+= __gen_imsm_checksum(mpb
);
5602 mpb
->family_num
= __cpu_to_le32(sum
);
5603 mpb
->orig_family_num
= mpb
->family_num
;
5605 super
->current_disk
= dl
;
5610 * Function marks disk as spare and restores disk serial
5611 * in case it was previously marked as failed by takeover operation
5613 * -1 : critical error
5614 * 0 : disk is marked as spare but serial is not set
5617 int mark_spare(struct dl
*disk
)
5619 __u8 serial
[MAX_RAID_SERIAL_LEN
];
5626 if (!imsm_read_serial(disk
->fd
, NULL
, serial
)) {
5627 /* Restore disk serial number, because takeover marks disk
5628 * as failed and adds to serial ':0' before it becomes
5631 serialcpy(disk
->serial
, serial
);
5632 serialcpy(disk
->disk
.serial
, serial
);
5635 disk
->disk
.status
= SPARE_DISK
;
5641 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
5642 int fd
, char *devname
,
5643 unsigned long long data_offset
)
5645 struct intel_super
*super
= st
->sb
;
5647 unsigned long long size
;
5648 unsigned int member_sector_size
;
5653 /* If we are on an RAID enabled platform check that the disk is
5654 * attached to the raid controller.
5655 * We do not need to test disks attachment for container based additions,
5656 * they shall be already tested when container was created/assembled.
5658 rv
= find_intel_hba_capability(fd
, super
, devname
);
5659 /* no orom/efi or non-intel hba of the disk */
5661 dprintf("capability: %p fd: %d ret: %d\n",
5662 super
->orom
, fd
, rv
);
5666 if (super
->current_vol
>= 0)
5667 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
5670 dd
= xcalloc(sizeof(*dd
), 1);
5671 dd
->major
= major(stb
.st_rdev
);
5672 dd
->minor
= minor(stb
.st_rdev
);
5673 dd
->devname
= devname
? xstrdup(devname
) : NULL
;
5676 dd
->action
= DISK_ADD
;
5677 rv
= imsm_read_serial(fd
, devname
, dd
->serial
);
5679 pr_err("failed to retrieve scsi serial, aborting\n");
5685 if (super
->hba
&& ((super
->hba
->type
== SYS_DEV_NVME
) ||
5686 (super
->hba
->type
== SYS_DEV_VMD
))) {
5688 char *devpath
= diskfd_to_devpath(fd
);
5689 char controller_path
[PATH_MAX
];
5692 pr_err("failed to get devpath, aborting\n");
5699 snprintf(controller_path
, PATH_MAX
-1, "%s/device", devpath
);
5702 if (devpath_to_vendor(controller_path
) == 0x8086) {
5704 * If Intel's NVMe drive has serial ended with
5705 * "-A","-B","-1" or "-2" it means that this is "x8"
5706 * device (double drive on single PCIe card).
5707 * User should be warned about potential data loss.
5709 for (i
= MAX_RAID_SERIAL_LEN
-1; i
> 0; i
--) {
5710 /* Skip empty character at the end */
5711 if (dd
->serial
[i
] == 0)
5714 if (((dd
->serial
[i
] == 'A') ||
5715 (dd
->serial
[i
] == 'B') ||
5716 (dd
->serial
[i
] == '1') ||
5717 (dd
->serial
[i
] == '2')) &&
5718 (dd
->serial
[i
-1] == '-'))
5719 pr_err("\tThe action you are about to take may put your data at risk.\n"
5720 "\tPlease note that x8 devices may consist of two separate x4 devices "
5721 "located on a single PCIe port.\n"
5722 "\tRAID 0 is the only supported configuration for this type of x8 device.\n");
5725 } else if (super
->hba
->type
== SYS_DEV_VMD
&& super
->orom
&&
5726 !imsm_orom_has_tpv_support(super
->orom
)) {
5727 pr_err("\tPlatform configuration does not support non-Intel NVMe drives.\n"
5728 "\tPlease refer to Intel(R) RSTe user guide.\n");
5735 get_dev_size(fd
, NULL
, &size
);
5736 get_dev_sector_size(fd
, NULL
, &member_sector_size
);
5738 if (super
->sector_size
== 0) {
5739 /* this a first device, so sector_size is not set yet */
5740 super
->sector_size
= member_sector_size
;
5743 /* clear migr_rec when adding disk to container */
5744 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
);
5745 if (lseek64(fd
, size
- MIGR_REC_SECTOR_POSITION
*member_sector_size
,
5747 if ((unsigned int)write(fd
, super
->migr_rec_buf
,
5748 MIGR_REC_BUF_SECTORS
*member_sector_size
) !=
5749 MIGR_REC_BUF_SECTORS
*member_sector_size
)
5750 perror("Write migr_rec failed");
5754 serialcpy(dd
->disk
.serial
, dd
->serial
);
5755 set_total_blocks(&dd
->disk
, size
);
5756 if (__le32_to_cpu(dd
->disk
.total_blocks_hi
) > 0) {
5757 struct imsm_super
*mpb
= super
->anchor
;
5758 mpb
->attributes
|= MPB_ATTRIB_2TB_DISK
;
5761 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
5762 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
5764 dd
->disk
.scsi_id
= __cpu_to_le32(0);
5766 if (st
->update_tail
) {
5767 dd
->next
= super
->disk_mgmt_list
;
5768 super
->disk_mgmt_list
= dd
;
5770 dd
->next
= super
->disks
;
5772 super
->updates_pending
++;
5778 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
5780 struct intel_super
*super
= st
->sb
;
5783 /* remove from super works only in mdmon - for communication
5784 * manager - monitor. Check if communication memory buffer
5787 if (!st
->update_tail
) {
5788 pr_err("shall be used in mdmon context only\n");
5791 dd
= xcalloc(1, sizeof(*dd
));
5792 dd
->major
= dk
->major
;
5793 dd
->minor
= dk
->minor
;
5796 dd
->action
= DISK_REMOVE
;
5798 dd
->next
= super
->disk_mgmt_list
;
5799 super
->disk_mgmt_list
= dd
;
5804 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
5807 char buf
[MAX_SECTOR_SIZE
];
5808 struct imsm_super anchor
;
5809 } spare_record
__attribute__ ((aligned(MAX_SECTOR_SIZE
)));
5811 /* spare records have their own family number and do not have any defined raid
5814 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
5816 struct imsm_super
*mpb
= super
->anchor
;
5817 struct imsm_super
*spare
= &spare_record
.anchor
;
5821 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
));
5822 spare
->generation_num
= __cpu_to_le32(1UL);
5823 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
5824 spare
->num_disks
= 1;
5825 spare
->num_raid_devs
= 0;
5826 spare
->cache_size
= mpb
->cache_size
;
5827 spare
->pwr_cycle_count
= __cpu_to_le32(1);
5829 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
5830 MPB_SIGNATURE MPB_VERSION_RAID0
);
5832 for (d
= super
->disks
; d
; d
= d
->next
) {
5836 spare
->disk
[0] = d
->disk
;
5837 if (__le32_to_cpu(d
->disk
.total_blocks_hi
) > 0)
5838 spare
->attributes
|= MPB_ATTRIB_2TB_DISK
;
5840 if (super
->sector_size
== 4096)
5841 convert_to_4k_imsm_disk(&spare
->disk
[0]);
5843 sum
= __gen_imsm_checksum(spare
);
5844 spare
->family_num
= __cpu_to_le32(sum
);
5845 spare
->orig_family_num
= 0;
5846 sum
= __gen_imsm_checksum(spare
);
5847 spare
->check_sum
= __cpu_to_le32(sum
);
5849 if (store_imsm_mpb(d
->fd
, spare
)) {
5850 pr_err("failed for device %d:%d %s\n",
5851 d
->major
, d
->minor
, strerror(errno
));
5863 static int write_super_imsm(struct supertype
*st
, int doclose
)
5865 struct intel_super
*super
= st
->sb
;
5866 unsigned int sector_size
= super
->sector_size
;
5867 struct imsm_super
*mpb
= super
->anchor
;
5873 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
5875 int clear_migration_record
= 1;
5878 /* 'generation' is incremented everytime the metadata is written */
5879 generation
= __le32_to_cpu(mpb
->generation_num
);
5881 mpb
->generation_num
= __cpu_to_le32(generation
);
5883 /* fix up cases where previous mdadm releases failed to set
5886 if (mpb
->orig_family_num
== 0)
5887 mpb
->orig_family_num
= mpb
->family_num
;
5889 for (d
= super
->disks
; d
; d
= d
->next
) {
5893 mpb
->disk
[d
->index
] = d
->disk
;
5897 for (d
= super
->missing
; d
; d
= d
->next
) {
5898 mpb
->disk
[d
->index
] = d
->disk
;
5901 mpb
->num_disks
= num_disks
;
5902 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
5904 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5905 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
5906 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
5908 imsm_copy_dev(dev
, dev2
);
5909 mpb_size
+= sizeof_imsm_dev(dev
, 0);
5911 if (is_gen_migration(dev2
))
5912 clear_migration_record
= 0;
5915 bbm_log_size
= get_imsm_bbm_log_size(super
->bbm_log
);
5918 memcpy((void *)mpb
+ mpb_size
, super
->bbm_log
, bbm_log_size
);
5919 mpb
->attributes
|= MPB_ATTRIB_BBM
;
5921 mpb
->attributes
&= ~MPB_ATTRIB_BBM
;
5923 super
->anchor
->bbm_log_size
= __cpu_to_le32(bbm_log_size
);
5924 mpb_size
+= bbm_log_size
;
5925 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5928 assert(super
->len
== 0 || mpb_size
<= super
->len
);
5931 /* recalculate checksum */
5932 sum
= __gen_imsm_checksum(mpb
);
5933 mpb
->check_sum
= __cpu_to_le32(sum
);
5935 if (super
->clean_migration_record_by_mdmon
) {
5936 clear_migration_record
= 1;
5937 super
->clean_migration_record_by_mdmon
= 0;
5939 if (clear_migration_record
)
5940 memset(super
->migr_rec_buf
, 0,
5941 MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
);
5943 if (sector_size
== 4096)
5944 convert_to_4k(super
);
5946 /* write the mpb for disks that compose raid devices */
5947 for (d
= super
->disks
; d
; d
= d
->next
) {
5948 if (d
->index
< 0 || is_failed(&d
->disk
))
5951 if (clear_migration_record
) {
5952 unsigned long long dsize
;
5954 get_dev_size(d
->fd
, NULL
, &dsize
);
5955 if (lseek64(d
->fd
, dsize
- sector_size
,
5957 if ((unsigned int)write(d
->fd
,
5958 super
->migr_rec_buf
,
5959 MIGR_REC_BUF_SECTORS
*sector_size
) !=
5960 MIGR_REC_BUF_SECTORS
*sector_size
)
5961 perror("Write migr_rec failed");
5965 if (store_imsm_mpb(d
->fd
, mpb
))
5967 "failed for device %d:%d (fd: %d)%s\n",
5969 d
->fd
, strerror(errno
));
5978 return write_super_imsm_spares(super
, doclose
);
5983 static int create_array(struct supertype
*st
, int dev_idx
)
5986 struct imsm_update_create_array
*u
;
5987 struct intel_super
*super
= st
->sb
;
5988 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
5989 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
5990 struct disk_info
*inf
;
5991 struct imsm_disk
*disk
;
5994 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
5995 sizeof(*inf
) * map
->num_members
;
5997 u
->type
= update_create_array
;
5998 u
->dev_idx
= dev_idx
;
5999 imsm_copy_dev(&u
->dev
, dev
);
6000 inf
= get_disk_info(u
);
6001 for (i
= 0; i
< map
->num_members
; i
++) {
6002 int idx
= get_imsm_disk_idx(dev
, i
, MAP_X
);
6004 disk
= get_imsm_disk(super
, idx
);
6006 disk
= get_imsm_missing(super
, idx
);
6007 serialcpy(inf
[i
].serial
, disk
->serial
);
6009 append_metadata_update(st
, u
, len
);
6014 static int mgmt_disk(struct supertype
*st
)
6016 struct intel_super
*super
= st
->sb
;
6018 struct imsm_update_add_remove_disk
*u
;
6020 if (!super
->disk_mgmt_list
)
6025 u
->type
= update_add_remove_disk
;
6026 append_metadata_update(st
, u
, len
);
6031 __u32
crc32c_le(__u32 crc
, unsigned char const *p
, size_t len
);
6033 static int write_init_ppl_imsm(struct supertype
*st
, struct mdinfo
*info
, int fd
)
6035 struct intel_super
*super
= st
->sb
;
6037 struct ppl_header
*ppl_hdr
;
6040 ret
= posix_memalign(&buf
, 4096, PPL_HEADER_SIZE
);
6042 pr_err("Failed to allocate PPL header buffer\n");
6046 memset(buf
, 0, PPL_HEADER_SIZE
);
6048 memset(ppl_hdr
->reserved
, 0xff, PPL_HDR_RESERVED
);
6049 ppl_hdr
->signature
= __cpu_to_le32(super
->anchor
->orig_family_num
);
6050 ppl_hdr
->checksum
= __cpu_to_le32(~crc32c_le(~0, buf
, PPL_HEADER_SIZE
));
6052 if (lseek64(fd
, info
->ppl_sector
* 512, SEEK_SET
) < 0) {
6054 perror("Failed to seek to PPL header location");
6057 if (!ret
&& write(fd
, buf
, PPL_HEADER_SIZE
) != PPL_HEADER_SIZE
) {
6059 perror("Write PPL header failed");
6069 static int validate_ppl_imsm(struct supertype
*st
, struct mdinfo
*info
,
6070 struct mdinfo
*disk
)
6072 struct intel_super
*super
= st
->sb
;
6076 struct ppl_header
*ppl_hdr
;
6078 struct imsm_dev
*dev
;
6079 struct imsm_map
*map
;
6082 if (disk
->disk
.raid_disk
< 0)
6085 if (posix_memalign(&buf
, 4096, PPL_HEADER_SIZE
)) {
6086 pr_err("Failed to allocate PPL header buffer\n");
6090 dev
= get_imsm_dev(super
, info
->container_member
);
6091 map
= get_imsm_map(dev
, MAP_X
);
6092 idx
= get_imsm_disk_idx(dev
, disk
->disk
.raid_disk
, MAP_X
);
6093 d
= get_imsm_dl_disk(super
, idx
);
6095 if (!d
|| d
->index
< 0 || is_failed(&d
->disk
))
6098 if (lseek64(d
->fd
, info
->ppl_sector
* 512, SEEK_SET
) < 0) {
6099 perror("Failed to seek to PPL header location");
6104 if (read(d
->fd
, buf
, PPL_HEADER_SIZE
) != PPL_HEADER_SIZE
) {
6105 perror("Read PPL header failed");
6112 crc
= __le32_to_cpu(ppl_hdr
->checksum
);
6113 ppl_hdr
->checksum
= 0;
6115 if (crc
!= ~crc32c_le(~0, buf
, PPL_HEADER_SIZE
)) {
6116 dprintf("Wrong PPL header checksum on %s\n",
6121 if (!ret
&& (__le32_to_cpu(ppl_hdr
->signature
) !=
6122 super
->anchor
->orig_family_num
)) {
6123 dprintf("Wrong PPL header signature on %s\n",
6131 if (ret
== 1 && map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
6132 return st
->ss
->write_init_ppl(st
, info
, d
->fd
);
6137 static int write_init_ppl_imsm_all(struct supertype
*st
, struct mdinfo
*info
)
6139 struct intel_super
*super
= st
->sb
;
6143 if (info
->consistency_policy
!= CONSISTENCY_POLICY_PPL
||
6144 info
->array
.level
!= 5)
6147 for (d
= super
->disks
; d
; d
= d
->next
) {
6148 if (d
->index
< 0 || is_failed(&d
->disk
))
6151 ret
= st
->ss
->write_init_ppl(st
, info
, d
->fd
);
6159 static int write_init_super_imsm(struct supertype
*st
)
6161 struct intel_super
*super
= st
->sb
;
6162 int current_vol
= super
->current_vol
;
6166 getinfo_super_imsm(st
, &info
, NULL
);
6168 /* we are done with current_vol reset it to point st at the container */
6169 super
->current_vol
= -1;
6171 if (st
->update_tail
) {
6172 /* queue the recently created array / added disk
6173 * as a metadata update */
6175 /* determine if we are creating a volume or adding a disk */
6176 if (current_vol
< 0) {
6177 /* in the mgmt (add/remove) disk case we are running
6178 * in mdmon context, so don't close fd's
6182 rv
= write_init_ppl_imsm_all(st
, &info
);
6184 rv
= create_array(st
, current_vol
);
6188 for (d
= super
->disks
; d
; d
= d
->next
)
6189 Kill(d
->devname
, NULL
, 0, -1, 1);
6190 if (current_vol
>= 0)
6191 rv
= write_init_ppl_imsm_all(st
, &info
);
6193 rv
= write_super_imsm(st
, 1);
6199 static int store_super_imsm(struct supertype
*st
, int fd
)
6201 struct intel_super
*super
= st
->sb
;
6202 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
6207 if (super
->sector_size
== 4096)
6208 convert_to_4k(super
);
6209 return store_imsm_mpb(fd
, mpb
);
6212 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
6213 int layout
, int raiddisks
, int chunk
,
6214 unsigned long long size
,
6215 unsigned long long data_offset
,
6217 unsigned long long *freesize
,
6221 unsigned long long ldsize
;
6222 struct intel_super
*super
;
6225 if (level
!= LEVEL_CONTAINER
)
6230 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
6233 pr_err("imsm: Cannot open %s: %s\n",
6234 dev
, strerror(errno
));
6237 if (!get_dev_size(fd
, dev
, &ldsize
)) {
6242 /* capabilities retrieve could be possible
6243 * note that there is no fd for the disks in array.
6245 super
= alloc_super();
6250 if (!get_dev_sector_size(fd
, NULL
, &super
->sector_size
)) {
6256 rv
= find_intel_hba_capability(fd
, super
, verbose
> 0 ? dev
: NULL
);
6260 fd2devname(fd
, str
);
6261 dprintf("fd: %d %s orom: %p rv: %d raiddisk: %d\n",
6262 fd
, str
, super
->orom
, rv
, raiddisks
);
6264 /* no orom/efi or non-intel hba of the disk */
6271 if (raiddisks
> super
->orom
->tds
) {
6273 pr_err("%d exceeds maximum number of platform supported disks: %d\n",
6274 raiddisks
, super
->orom
->tds
);
6278 if ((super
->orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) == 0 &&
6279 (ldsize
>> 9) >> 32 > 0) {
6281 pr_err("%s exceeds maximum platform supported size\n", dev
);
6287 *freesize
= avail_size_imsm(st
, ldsize
>> 9, data_offset
);
6293 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
6295 const unsigned long long base_start
= e
[*idx
].start
;
6296 unsigned long long end
= base_start
+ e
[*idx
].size
;
6299 if (base_start
== end
)
6303 for (i
= *idx
; i
< num_extents
; i
++) {
6304 /* extend overlapping extents */
6305 if (e
[i
].start
>= base_start
&&
6306 e
[i
].start
<= end
) {
6309 if (e
[i
].start
+ e
[i
].size
> end
)
6310 end
= e
[i
].start
+ e
[i
].size
;
6311 } else if (e
[i
].start
> end
) {
6317 return end
- base_start
;
6320 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
6322 /* build a composite disk with all known extents and generate a new
6323 * 'maxsize' given the "all disks in an array must share a common start
6324 * offset" constraint
6326 struct extent
*e
= xcalloc(sum_extents
, sizeof(*e
));
6330 unsigned long long pos
;
6331 unsigned long long start
= 0;
6332 unsigned long long maxsize
;
6333 unsigned long reserve
;
6335 /* coalesce and sort all extents. also, check to see if we need to
6336 * reserve space between member arrays
6339 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6342 for (i
= 0; i
< dl
->extent_cnt
; i
++)
6345 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
6350 while (i
< sum_extents
) {
6351 e
[j
].start
= e
[i
].start
;
6352 e
[j
].size
= find_size(e
, &i
, sum_extents
);
6354 if (e
[j
-1].size
== 0)
6363 unsigned long long esize
;
6365 esize
= e
[i
].start
- pos
;
6366 if (esize
>= maxsize
) {
6371 pos
= e
[i
].start
+ e
[i
].size
;
6373 } while (e
[i
-1].size
);
6379 /* FIXME assumes volume at offset 0 is the first volume in a
6382 if (start_extent
> 0)
6383 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
6387 if (maxsize
< reserve
)
6390 super
->create_offset
= ~((unsigned long long) 0);
6391 if (start
+ reserve
> super
->create_offset
)
6392 return 0; /* start overflows create_offset */
6393 super
->create_offset
= start
+ reserve
;
6395 return maxsize
- reserve
;
6398 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
6400 if (level
< 0 || level
== 6 || level
== 4)
6403 /* if we have an orom prevent invalid raid levels */
6406 case 0: return imsm_orom_has_raid0(orom
);
6409 return imsm_orom_has_raid1e(orom
);
6410 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
6411 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
6412 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
6415 return 1; /* not on an Intel RAID platform so anything goes */
6421 active_arrays_by_format(char *name
, char* hba
, struct md_list
**devlist
,
6422 int dpa
, int verbose
)
6424 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
6425 struct mdstat_ent
*memb
;
6431 for (memb
= mdstat
; memb
; memb
= memb
->next
) {
6432 if (memb
->metadata_version
&&
6433 (strncmp(memb
->metadata_version
, "external:", 9) == 0) &&
6434 (strcmp(&memb
->metadata_version
[9], name
) == 0) &&
6435 !is_subarray(memb
->metadata_version
+9) &&
6437 struct dev_member
*dev
= memb
->members
;
6439 while(dev
&& (fd
< 0)) {
6440 char *path
= xmalloc(strlen(dev
->name
) + strlen("/dev/") + 1);
6441 num
= sprintf(path
, "%s%s", "/dev/", dev
->name
);
6443 fd
= open(path
, O_RDONLY
, 0);
6444 if (num
<= 0 || fd
< 0) {
6445 pr_vrb("Cannot open %s: %s\n",
6446 dev
->name
, strerror(errno
));
6452 if (fd
>= 0 && disk_attached_to_hba(fd
, hba
)) {
6453 struct mdstat_ent
*vol
;
6454 for (vol
= mdstat
; vol
; vol
= vol
->next
) {
6455 if (vol
->active
> 0 &&
6456 vol
->metadata_version
&&
6457 is_container_member(vol
, memb
->devnm
)) {
6462 if (*devlist
&& (found
< dpa
)) {
6463 dv
= xcalloc(1, sizeof(*dv
));
6464 dv
->devname
= xmalloc(strlen(memb
->devnm
) + strlen("/dev/") + 1);
6465 sprintf(dv
->devname
, "%s%s", "/dev/", memb
->devnm
);
6468 dv
->next
= *devlist
;
6476 free_mdstat(mdstat
);
6481 static struct md_list
*
6482 get_loop_devices(void)
6485 struct md_list
*devlist
= NULL
;
6488 for(i
= 0; i
< 12; i
++) {
6489 dv
= xcalloc(1, sizeof(*dv
));
6490 dv
->devname
= xmalloc(40);
6491 sprintf(dv
->devname
, "/dev/loop%d", i
);
6499 static struct md_list
*
6500 get_devices(const char *hba_path
)
6502 struct md_list
*devlist
= NULL
;
6509 devlist
= get_loop_devices();
6512 /* scroll through /sys/dev/block looking for devices attached to
6515 dir
= opendir("/sys/dev/block");
6516 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
6521 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
6523 path
= devt_to_devpath(makedev(major
, minor
));
6526 if (!path_attached_to_hba(path
, hba_path
)) {
6533 fd
= dev_open(ent
->d_name
, O_RDONLY
);
6535 fd2devname(fd
, buf
);
6538 pr_err("cannot open device: %s\n",
6543 dv
= xcalloc(1, sizeof(*dv
));
6544 dv
->devname
= xstrdup(buf
);
6551 devlist
= devlist
->next
;
6561 count_volumes_list(struct md_list
*devlist
, char *homehost
,
6562 int verbose
, int *found
)
6564 struct md_list
*tmpdev
;
6566 struct supertype
*st
;
6568 /* first walk the list of devices to find a consistent set
6569 * that match the criterea, if that is possible.
6570 * We flag the ones we like with 'used'.
6573 st
= match_metadata_desc_imsm("imsm");
6575 pr_vrb("cannot allocate memory for imsm supertype\n");
6579 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
6580 char *devname
= tmpdev
->devname
;
6582 struct supertype
*tst
;
6584 if (tmpdev
->used
> 1)
6586 tst
= dup_super(st
);
6588 pr_vrb("cannot allocate memory for imsm supertype\n");
6591 tmpdev
->container
= 0;
6592 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
6594 dprintf("cannot open device %s: %s\n",
6595 devname
, strerror(errno
));
6597 } else if (!fstat_is_blkdev(dfd
, devname
, &rdev
)) {
6599 } else if (must_be_container(dfd
)) {
6600 struct supertype
*cst
;
6601 cst
= super_by_fd(dfd
, NULL
);
6603 dprintf("cannot recognize container type %s\n",
6606 } else if (tst
->ss
!= st
->ss
) {
6607 dprintf("non-imsm container - ignore it: %s\n",
6610 } else if (!tst
->ss
->load_container
||
6611 tst
->ss
->load_container(tst
, dfd
, NULL
))
6614 tmpdev
->container
= 1;
6617 cst
->ss
->free_super(cst
);
6619 tmpdev
->st_rdev
= rdev
;
6620 if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
6621 dprintf("no RAID superblock on %s\n",
6624 } else if (tst
->ss
->compare_super
== NULL
) {
6625 dprintf("Cannot assemble %s metadata on %s\n",
6626 tst
->ss
->name
, devname
);
6632 if (tmpdev
->used
== 2 || tmpdev
->used
== 4) {
6633 /* Ignore unrecognised devices during auto-assembly */
6638 tst
->ss
->getinfo_super(tst
, &info
, NULL
);
6640 if (st
->minor_version
== -1)
6641 st
->minor_version
= tst
->minor_version
;
6643 if (memcmp(info
.uuid
, uuid_zero
,
6644 sizeof(int[4])) == 0) {
6645 /* this is a floating spare. It cannot define
6646 * an array unless there are no more arrays of
6647 * this type to be found. It can be included
6648 * in an array of this type though.
6654 if (st
->ss
!= tst
->ss
||
6655 st
->minor_version
!= tst
->minor_version
||
6656 st
->ss
->compare_super(st
, tst
) != 0) {
6657 /* Some mismatch. If exactly one array matches this host,
6658 * we can resolve on that one.
6659 * Or, if we are auto assembling, we just ignore the second
6662 dprintf("superblock on %s doesn't match others - assembly aborted\n",
6668 dprintf("found: devname: %s\n", devname
);
6672 tst
->ss
->free_super(tst
);
6676 if ((err
= load_super_imsm_all(st
, -1, &st
->sb
, NULL
, devlist
, 0)) == 0) {
6677 struct mdinfo
*iter
, *head
= st
->ss
->container_content(st
, NULL
);
6678 for (iter
= head
; iter
; iter
= iter
->next
) {
6679 dprintf("content->text_version: %s vol\n",
6680 iter
->text_version
);
6681 if (iter
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
6682 /* do not assemble arrays with unsupported
6684 dprintf("Cannot activate member %s.\n",
6685 iter
->text_version
);
6692 dprintf("No valid super block on device list: err: %d %p\n",
6696 dprintf("no more devices to examine\n");
6699 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
6700 if (tmpdev
->used
== 1 && tmpdev
->found
) {
6702 if (count
< tmpdev
->found
)
6705 count
-= tmpdev
->found
;
6708 if (tmpdev
->used
== 1)
6713 st
->ss
->free_super(st
);
6717 static int __count_volumes(char *hba_path
, int dpa
, int verbose
,
6720 struct sys_dev
*idev
, *intel_devices
= find_intel_devices();
6722 const struct orom_entry
*entry
;
6723 struct devid_list
*dv
, *devid_list
;
6728 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
6729 if (strstr(idev
->path
, hba_path
))
6733 if (!idev
|| !idev
->dev_id
)
6736 entry
= get_orom_entry_by_device_id(idev
->dev_id
);
6738 if (!entry
|| !entry
->devid_list
)
6741 devid_list
= entry
->devid_list
;
6742 for (dv
= devid_list
; dv
; dv
= dv
->next
) {
6743 struct md_list
*devlist
;
6744 struct sys_dev
*device
= NULL
;
6749 device
= device_by_id_and_path(dv
->devid
, hba_path
);
6751 device
= device_by_id(dv
->devid
);
6754 hpath
= device
->path
;
6758 devlist
= get_devices(hpath
);
6759 /* if no intel devices return zero volumes */
6760 if (devlist
== NULL
)
6763 count
+= active_arrays_by_format("imsm", hpath
, &devlist
, dpa
,
6765 dprintf("path: %s active arrays: %d\n", hpath
, count
);
6766 if (devlist
== NULL
)
6770 count
+= count_volumes_list(devlist
,
6774 dprintf("found %d count: %d\n", found
, count
);
6777 dprintf("path: %s total number of volumes: %d\n", hpath
, count
);
6780 struct md_list
*dv
= devlist
;
6781 devlist
= devlist
->next
;
6789 static int count_volumes(struct intel_hba
*hba
, int dpa
, int verbose
)
6793 if (hba
->type
== SYS_DEV_VMD
) {
6794 struct sys_dev
*dev
;
6797 for (dev
= find_intel_devices(); dev
; dev
= dev
->next
) {
6798 if (dev
->type
== SYS_DEV_VMD
)
6799 count
+= __count_volumes(dev
->path
, dpa
,
6804 return __count_volumes(hba
->path
, dpa
, verbose
, 0);
6807 static int imsm_default_chunk(const struct imsm_orom
*orom
)
6809 /* up to 512 if the plaform supports it, otherwise the platform max.
6810 * 128 if no platform detected
6812 int fs
= max(7, orom
? fls(orom
->sss
) : 0);
6814 return min(512, (1 << fs
));
6818 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
6819 int raiddisks
, int *chunk
, unsigned long long size
, int verbose
)
6821 /* check/set platform and metadata limits/defaults */
6822 if (super
->orom
&& raiddisks
> super
->orom
->dpa
) {
6823 pr_vrb("platform supports a maximum of %d disks per array\n",
6828 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
6829 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
6830 pr_vrb("platform does not support raid%d with %d disk%s\n",
6831 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
6835 if (*chunk
== 0 || *chunk
== UnSet
)
6836 *chunk
= imsm_default_chunk(super
->orom
);
6838 if (super
->orom
&& !imsm_orom_has_chunk(super
->orom
, *chunk
)) {
6839 pr_vrb("platform does not support a chunk size of: %d\n", *chunk
);
6843 if (layout
!= imsm_level_to_layout(level
)) {
6845 pr_vrb("imsm raid 5 only supports the left-asymmetric layout\n");
6846 else if (level
== 10)
6847 pr_vrb("imsm raid 10 only supports the n2 layout\n");
6849 pr_vrb("imsm unknown layout %#x for this raid level %d\n",
6854 if (super
->orom
&& (super
->orom
->attr
& IMSM_OROM_ATTR_2TB
) == 0 &&
6855 (calc_array_size(level
, raiddisks
, layout
, *chunk
, size
) >> 32) > 0) {
6856 pr_vrb("platform does not support a volume size over 2TB\n");
6863 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
6864 * FIX ME add ahci details
6866 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
6867 int layout
, int raiddisks
, int *chunk
,
6868 unsigned long long size
,
6869 unsigned long long data_offset
,
6871 unsigned long long *freesize
,
6875 struct intel_super
*super
= st
->sb
;
6876 struct imsm_super
*mpb
;
6878 unsigned long long pos
= 0;
6879 unsigned long long maxsize
;
6883 /* We must have the container info already read in. */
6887 mpb
= super
->anchor
;
6889 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, size
, verbose
)) {
6890 pr_err("RAID gemetry validation failed. Cannot proceed with the action(s).\n");
6894 /* General test: make sure there is space for
6895 * 'raiddisks' device extents of size 'size' at a given
6898 unsigned long long minsize
= size
;
6899 unsigned long long start_offset
= MaxSector
;
6902 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
6903 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6908 e
= get_extents(super
, dl
);
6911 unsigned long long esize
;
6912 esize
= e
[i
].start
- pos
;
6913 if (esize
>= minsize
)
6915 if (found
&& start_offset
== MaxSector
) {
6918 } else if (found
&& pos
!= start_offset
) {
6922 pos
= e
[i
].start
+ e
[i
].size
;
6924 } while (e
[i
-1].size
);
6929 if (dcnt
< raiddisks
) {
6931 pr_err("imsm: Not enough devices with space for this array (%d < %d)\n",
6938 /* This device must be a member of the set */
6939 if (!stat_is_blkdev(dev
, &rdev
))
6941 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6942 if (dl
->major
== (int)major(rdev
) &&
6943 dl
->minor
== (int)minor(rdev
))
6948 pr_err("%s is not in the same imsm set\n", dev
);
6950 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
6951 /* If a volume is present then the current creation attempt
6952 * cannot incorporate new spares because the orom may not
6953 * understand this configuration (all member disks must be
6954 * members of each array in the container).
6956 pr_err("%s is a spare and a volume is already defined for this container\n", dev
);
6957 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
6959 } else if (super
->orom
&& mpb
->num_raid_devs
> 0 &&
6960 mpb
->num_disks
!= raiddisks
) {
6961 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
6965 /* retrieve the largest free space block */
6966 e
= get_extents(super
, dl
);
6971 unsigned long long esize
;
6973 esize
= e
[i
].start
- pos
;
6974 if (esize
>= maxsize
)
6976 pos
= e
[i
].start
+ e
[i
].size
;
6978 } while (e
[i
-1].size
);
6983 pr_err("unable to determine free space for: %s\n",
6987 if (maxsize
< size
) {
6989 pr_err("%s not enough space (%llu < %llu)\n",
6990 dev
, maxsize
, size
);
6994 /* count total number of extents for merge */
6996 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6998 i
+= dl
->extent_cnt
;
7000 maxsize
= merge_extents(super
, i
);
7002 if (!check_env("IMSM_NO_PLATFORM") &&
7003 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
7004 pr_err("attempting to create a second volume with size less then remaining space. Aborting...\n");
7008 if (maxsize
< size
|| maxsize
== 0) {
7011 pr_err("no free space left on device. Aborting...\n");
7013 pr_err("not enough space to create volume of given size (%llu < %llu). Aborting...\n",
7019 *freesize
= maxsize
;
7022 int count
= count_volumes(super
->hba
,
7023 super
->orom
->dpa
, verbose
);
7024 if (super
->orom
->vphba
<= count
) {
7025 pr_vrb("platform does not support more than %d raid volumes.\n",
7026 super
->orom
->vphba
);
7033 static int imsm_get_free_size(struct supertype
*st
, int raiddisks
,
7034 unsigned long long size
, int chunk
,
7035 unsigned long long *freesize
)
7037 struct intel_super
*super
= st
->sb
;
7038 struct imsm_super
*mpb
= super
->anchor
;
7043 unsigned long long maxsize
;
7044 unsigned long long minsize
;
7048 /* find the largest common start free region of the possible disks */
7052 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7058 /* don't activate new spares if we are orom constrained
7059 * and there is already a volume active in the container
7061 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
7064 e
= get_extents(super
, dl
);
7067 for (i
= 1; e
[i
-1].size
; i
++)
7075 maxsize
= merge_extents(super
, extent_cnt
);
7079 minsize
= chunk
* 2;
7081 if (cnt
< raiddisks
||
7082 (super
->orom
&& used
&& used
!= raiddisks
) ||
7083 maxsize
< minsize
||
7085 pr_err("not enough devices with space to create array.\n");
7086 return 0; /* No enough free spaces large enough */
7097 if (!check_env("IMSM_NO_PLATFORM") &&
7098 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
7099 pr_err("attempting to create a second volume with size less then remaining space. Aborting...\n");
7103 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7105 dl
->raiddisk
= cnt
++;
7109 dprintf("imsm: imsm_get_free_size() returns : %llu\n", size
);
7114 static int reserve_space(struct supertype
*st
, int raiddisks
,
7115 unsigned long long size
, int chunk
,
7116 unsigned long long *freesize
)
7118 struct intel_super
*super
= st
->sb
;
7123 rv
= imsm_get_free_size(st
, raiddisks
, size
, chunk
, freesize
);
7126 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7128 dl
->raiddisk
= cnt
++;
7135 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
7136 int raiddisks
, int *chunk
, unsigned long long size
,
7137 unsigned long long data_offset
,
7138 char *dev
, unsigned long long *freesize
,
7139 int consistency_policy
, int verbose
)
7146 * if given unused devices create a container
7147 * if given given devices in a container create a member volume
7149 if (level
== LEVEL_CONTAINER
) {
7150 /* Must be a fresh device to add to a container */
7151 return validate_geometry_imsm_container(st
, level
, layout
,
7161 struct intel_super
*super
= st
->sb
;
7162 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
7163 raiddisks
, chunk
, size
,
7166 /* we are being asked to automatically layout a
7167 * new volume based on the current contents of
7168 * the container. If the the parameters can be
7169 * satisfied reserve_space will record the disks,
7170 * start offset, and size of the volume to be
7171 * created. add_to_super and getinfo_super
7172 * detect when autolayout is in progress.
7174 /* assuming that freesize is always given when array is
7176 if (super
->orom
&& freesize
) {
7178 count
= count_volumes(super
->hba
,
7179 super
->orom
->dpa
, verbose
);
7180 if (super
->orom
->vphba
<= count
) {
7181 pr_vrb("platform does not support more than %d raid volumes.\n",
7182 super
->orom
->vphba
);
7187 return reserve_space(st
, raiddisks
, size
,
7193 /* creating in a given container */
7194 return validate_geometry_imsm_volume(st
, level
, layout
,
7195 raiddisks
, chunk
, size
,
7197 dev
, freesize
, verbose
);
7200 /* This device needs to be a device in an 'imsm' container */
7201 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
7204 pr_err("Cannot create this array on device %s\n",
7209 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
7211 pr_err("Cannot open %s: %s\n",
7212 dev
, strerror(errno
));
7215 /* Well, it is in use by someone, maybe an 'imsm' container. */
7216 cfd
= open_container(fd
);
7220 pr_err("Cannot use %s: It is busy\n",
7224 sra
= sysfs_read(cfd
, NULL
, GET_VERSION
);
7225 if (sra
&& sra
->array
.major_version
== -1 &&
7226 strcmp(sra
->text_version
, "imsm") == 0)
7230 /* This is a member of a imsm container. Load the container
7231 * and try to create a volume
7233 struct intel_super
*super
;
7235 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
, NULL
, 1) == 0) {
7237 strcpy(st
->container_devnm
, fd2devnm(cfd
));
7239 return validate_geometry_imsm_volume(st
, level
, layout
,
7241 size
, data_offset
, dev
,
7248 pr_err("failed container membership check\n");
7254 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
7256 struct intel_super
*super
= st
->sb
;
7258 if (level
&& *level
== UnSet
)
7259 *level
= LEVEL_CONTAINER
;
7261 if (level
&& layout
&& *layout
== UnSet
)
7262 *layout
= imsm_level_to_layout(*level
);
7264 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0))
7265 *chunk
= imsm_default_chunk(super
->orom
);
7268 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
7270 static int kill_subarray_imsm(struct supertype
*st
)
7272 /* remove the subarray currently referenced by ->current_vol */
7274 struct intel_dev
**dp
;
7275 struct intel_super
*super
= st
->sb
;
7276 __u8 current_vol
= super
->current_vol
;
7277 struct imsm_super
*mpb
= super
->anchor
;
7279 if (super
->current_vol
< 0)
7281 super
->current_vol
= -1; /* invalidate subarray cursor */
7283 /* block deletions that would change the uuid of active subarrays
7285 * FIXME when immutable ids are available, but note that we'll
7286 * also need to fixup the invalidated/active subarray indexes in
7289 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7292 if (i
< current_vol
)
7294 sprintf(subarray
, "%u", i
);
7295 if (is_subarray_active(subarray
, st
->devnm
)) {
7296 pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
7303 if (st
->update_tail
) {
7304 struct imsm_update_kill_array
*u
= xmalloc(sizeof(*u
));
7306 u
->type
= update_kill_array
;
7307 u
->dev_idx
= current_vol
;
7308 append_metadata_update(st
, u
, sizeof(*u
));
7313 for (dp
= &super
->devlist
; *dp
;)
7314 if ((*dp
)->index
== current_vol
) {
7317 handle_missing(super
, (*dp
)->dev
);
7318 if ((*dp
)->index
> current_vol
)
7323 /* no more raid devices, all active components are now spares,
7324 * but of course failed are still failed
7326 if (--mpb
->num_raid_devs
== 0) {
7329 for (d
= super
->disks
; d
; d
= d
->next
)
7334 super
->updates_pending
++;
7339 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
7340 char *update
, struct mddev_ident
*ident
)
7342 /* update the subarray currently referenced by ->current_vol */
7343 struct intel_super
*super
= st
->sb
;
7344 struct imsm_super
*mpb
= super
->anchor
;
7346 if (strcmp(update
, "name") == 0) {
7347 char *name
= ident
->name
;
7351 if (is_subarray_active(subarray
, st
->devnm
)) {
7352 pr_err("Unable to update name of active subarray\n");
7356 if (!check_name(super
, name
, 0))
7359 vol
= strtoul(subarray
, &ep
, 10);
7360 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
7363 if (st
->update_tail
) {
7364 struct imsm_update_rename_array
*u
= xmalloc(sizeof(*u
));
7366 u
->type
= update_rename_array
;
7368 strncpy((char *) u
->name
, name
, MAX_RAID_SERIAL_LEN
);
7369 u
->name
[MAX_RAID_SERIAL_LEN
-1] = '\0';
7370 append_metadata_update(st
, u
, sizeof(*u
));
7372 struct imsm_dev
*dev
;
7375 dev
= get_imsm_dev(super
, vol
);
7376 strncpy((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
7377 dev
->volume
[MAX_RAID_SERIAL_LEN
-1] = '\0';
7378 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7379 dev
= get_imsm_dev(super
, i
);
7380 handle_missing(super
, dev
);
7382 super
->updates_pending
++;
7384 } else if (strcmp(update
, "ppl") == 0 ||
7385 strcmp(update
, "no-ppl") == 0) {
7388 int vol
= strtoul(subarray
, &ep
, 10);
7390 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
7393 if (strcmp(update
, "ppl") == 0)
7394 new_policy
= RWH_DISTRIBUTED
;
7396 new_policy
= RWH_OFF
;
7398 if (st
->update_tail
) {
7399 struct imsm_update_rwh_policy
*u
= xmalloc(sizeof(*u
));
7401 u
->type
= update_rwh_policy
;
7403 u
->new_policy
= new_policy
;
7404 append_metadata_update(st
, u
, sizeof(*u
));
7406 struct imsm_dev
*dev
;
7408 dev
= get_imsm_dev(super
, vol
);
7409 dev
->rwh_policy
= new_policy
;
7410 super
->updates_pending
++;
7418 static int is_gen_migration(struct imsm_dev
*dev
)
7423 if (!dev
->vol
.migr_state
)
7426 if (migr_type(dev
) == MIGR_GEN_MIGR
)
7432 static int is_rebuilding(struct imsm_dev
*dev
)
7434 struct imsm_map
*migr_map
;
7436 if (!dev
->vol
.migr_state
)
7439 if (migr_type(dev
) != MIGR_REBUILD
)
7442 migr_map
= get_imsm_map(dev
, MAP_1
);
7444 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
7450 static int is_initializing(struct imsm_dev
*dev
)
7452 struct imsm_map
*migr_map
;
7454 if (!dev
->vol
.migr_state
)
7457 if (migr_type(dev
) != MIGR_INIT
)
7460 migr_map
= get_imsm_map(dev
, MAP_1
);
7462 if (migr_map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
7468 static void update_recovery_start(struct intel_super
*super
,
7469 struct imsm_dev
*dev
,
7470 struct mdinfo
*array
)
7472 struct mdinfo
*rebuild
= NULL
;
7476 if (!is_rebuilding(dev
))
7479 /* Find the rebuild target, but punt on the dual rebuild case */
7480 for (d
= array
->devs
; d
; d
= d
->next
)
7481 if (d
->recovery_start
== 0) {
7488 /* (?) none of the disks are marked with
7489 * IMSM_ORD_REBUILD, so assume they are missing and the
7490 * disk_ord_tbl was not correctly updated
7492 dprintf("failed to locate out-of-sync disk\n");
7496 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
7497 rebuild
->recovery_start
= units
* blocks_per_migr_unit(super
, dev
);
7500 static int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
);
7502 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
7504 /* Given a container loaded by load_super_imsm_all,
7505 * extract information about all the arrays into
7507 * If 'subarray' is given, just extract info about that array.
7509 * For each imsm_dev create an mdinfo, fill it in,
7510 * then look for matching devices in super->disks
7511 * and create appropriate device mdinfo.
7513 struct intel_super
*super
= st
->sb
;
7514 struct imsm_super
*mpb
= super
->anchor
;
7515 struct mdinfo
*rest
= NULL
;
7519 int spare_disks
= 0;
7521 /* do not assemble arrays when not all attributes are supported */
7522 if (imsm_check_attributes(mpb
->attributes
) == 0) {
7524 pr_err("Unsupported attributes in IMSM metadata.Arrays activation is blocked.\n");
7527 /* count spare devices, not used in maps
7529 for (d
= super
->disks
; d
; d
= d
->next
)
7533 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7534 struct imsm_dev
*dev
;
7535 struct imsm_map
*map
;
7536 struct imsm_map
*map2
;
7537 struct mdinfo
*this;
7543 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
7546 dev
= get_imsm_dev(super
, i
);
7547 map
= get_imsm_map(dev
, MAP_0
);
7548 map2
= get_imsm_map(dev
, MAP_1
);
7550 /* do not publish arrays that are in the middle of an
7551 * unsupported migration
7553 if (dev
->vol
.migr_state
&&
7554 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
7555 pr_err("cannot assemble volume '%.16s': unsupported migration in progress\n",
7559 /* do not publish arrays that are not support by controller's
7563 this = xmalloc(sizeof(*this));
7565 super
->current_vol
= i
;
7566 getinfo_super_imsm_volume(st
, this, NULL
);
7568 chunk
= __le16_to_cpu(map
->blocks_per_strip
) >> 1;
7569 /* mdadm does not support all metadata features- set the bit in all arrays state */
7570 if (!validate_geometry_imsm_orom(super
,
7571 get_imsm_raid_level(map
), /* RAID level */
7572 imsm_level_to_layout(get_imsm_raid_level(map
)),
7573 map
->num_members
, /* raid disks */
7574 &chunk
, join_u32(dev
->size_low
, dev
->size_high
),
7576 pr_err("IMSM RAID geometry validation failed. Array %s activation is blocked.\n",
7578 this->array
.state
|=
7579 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
7580 (1<<MD_SB_BLOCK_VOLUME
);
7583 /* if array has bad blocks, set suitable bit in all arrays state */
7585 this->array
.state
|=
7586 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
7587 (1<<MD_SB_BLOCK_VOLUME
);
7589 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
7590 unsigned long long recovery_start
;
7591 struct mdinfo
*info_d
;
7598 idx
= get_imsm_disk_idx(dev
, slot
, MAP_0
);
7599 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
7600 for (d
= super
->disks
; d
; d
= d
->next
)
7601 if (d
->index
== idx
)
7604 recovery_start
= MaxSector
;
7607 if (d
&& is_failed(&d
->disk
))
7609 if (ord
& IMSM_ORD_REBUILD
)
7613 * if we skip some disks the array will be assmebled degraded;
7614 * reset resync start to avoid a dirty-degraded
7615 * situation when performing the intial sync
7617 * FIXME handle dirty degraded
7619 if ((skip
|| recovery_start
== 0) &&
7620 !(dev
->vol
.dirty
& RAIDVOL_DIRTY
))
7621 this->resync_start
= MaxSector
;
7625 info_d
= xcalloc(1, sizeof(*info_d
));
7626 info_d
->next
= this->devs
;
7627 this->devs
= info_d
;
7629 info_d
->disk
.number
= d
->index
;
7630 info_d
->disk
.major
= d
->major
;
7631 info_d
->disk
.minor
= d
->minor
;
7632 info_d
->disk
.raid_disk
= slot
;
7633 info_d
->recovery_start
= recovery_start
;
7635 if (slot
< map2
->num_members
)
7636 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
7638 this->array
.spare_disks
++;
7640 if (slot
< map
->num_members
)
7641 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
7643 this->array
.spare_disks
++;
7645 if (info_d
->recovery_start
== MaxSector
)
7646 this->array
.working_disks
++;
7648 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
7649 info_d
->data_offset
= pba_of_lba0(map
);
7651 if (map
->raid_level
== 5) {
7652 info_d
->component_size
=
7653 num_data_stripes(map
) *
7654 map
->blocks_per_strip
;
7655 info_d
->ppl_sector
= this->ppl_sector
;
7656 info_d
->ppl_size
= this->ppl_size
;
7658 info_d
->component_size
= blocks_per_member(map
);
7660 info_d
->consistency_policy
= this->consistency_policy
;
7662 info_d
->bb
.supported
= 1;
7663 get_volume_badblocks(super
->bbm_log
, ord_to_idx(ord
),
7664 info_d
->data_offset
,
7665 info_d
->component_size
,
7668 /* now that the disk list is up-to-date fixup recovery_start */
7669 update_recovery_start(super
, dev
, this);
7670 this->array
.spare_disks
+= spare_disks
;
7672 /* check for reshape */
7673 if (this->reshape_active
== 1)
7674 recover_backup_imsm(st
, this);
7681 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
7682 int failed
, int look_in_map
)
7684 struct imsm_map
*map
;
7686 map
= get_imsm_map(dev
, look_in_map
);
7689 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
7690 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
7692 switch (get_imsm_raid_level(map
)) {
7694 return IMSM_T_STATE_FAILED
;
7697 if (failed
< map
->num_members
)
7698 return IMSM_T_STATE_DEGRADED
;
7700 return IMSM_T_STATE_FAILED
;
7705 * check to see if any mirrors have failed, otherwise we
7706 * are degraded. Even numbered slots are mirrored on
7710 /* gcc -Os complains that this is unused */
7711 int insync
= insync
;
7713 for (i
= 0; i
< map
->num_members
; i
++) {
7714 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_X
);
7715 int idx
= ord_to_idx(ord
);
7716 struct imsm_disk
*disk
;
7718 /* reset the potential in-sync count on even-numbered
7719 * slots. num_copies is always 2 for imsm raid10
7724 disk
= get_imsm_disk(super
, idx
);
7725 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
7728 /* no in-sync disks left in this mirror the
7732 return IMSM_T_STATE_FAILED
;
7735 return IMSM_T_STATE_DEGRADED
;
7739 return IMSM_T_STATE_DEGRADED
;
7741 return IMSM_T_STATE_FAILED
;
7747 return map
->map_state
;
7750 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
7755 struct imsm_disk
*disk
;
7756 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7757 struct imsm_map
*prev
= get_imsm_map(dev
, MAP_1
);
7758 struct imsm_map
*map_for_loop
;
7763 /* at the beginning of migration we set IMSM_ORD_REBUILD on
7764 * disks that are being rebuilt. New failures are recorded to
7765 * map[0]. So we look through all the disks we started with and
7766 * see if any failures are still present, or if any new ones
7770 if (prev
&& (map
->num_members
< prev
->num_members
))
7771 map_for_loop
= prev
;
7773 for (i
= 0; i
< map_for_loop
->num_members
; i
++) {
7775 /* when MAP_X is passed both maps failures are counted
7778 (look_in_map
== MAP_1
|| look_in_map
== MAP_X
) &&
7779 i
< prev
->num_members
) {
7780 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
7781 idx_1
= ord_to_idx(ord
);
7783 disk
= get_imsm_disk(super
, idx_1
);
7784 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
7787 if ((look_in_map
== MAP_0
|| look_in_map
== MAP_X
) &&
7788 i
< map
->num_members
) {
7789 ord
= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
7790 idx
= ord_to_idx(ord
);
7793 disk
= get_imsm_disk(super
, idx
);
7794 if (!disk
|| is_failed(disk
) ||
7795 ord
& IMSM_ORD_REBUILD
)
7804 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
7807 struct intel_super
*super
= c
->sb
;
7808 struct imsm_super
*mpb
= super
->anchor
;
7809 struct imsm_update_prealloc_bb_mem u
;
7811 if (atoi(inst
) >= mpb
->num_raid_devs
) {
7812 pr_err("subarry index %d, out of range\n", atoi(inst
));
7816 dprintf("imsm: open_new %s\n", inst
);
7817 a
->info
.container_member
= atoi(inst
);
7819 u
.type
= update_prealloc_badblocks_mem
;
7820 imsm_update_metadata_locally(c
, &u
, sizeof(u
));
7825 static int is_resyncing(struct imsm_dev
*dev
)
7827 struct imsm_map
*migr_map
;
7829 if (!dev
->vol
.migr_state
)
7832 if (migr_type(dev
) == MIGR_INIT
||
7833 migr_type(dev
) == MIGR_REPAIR
)
7836 if (migr_type(dev
) == MIGR_GEN_MIGR
)
7839 migr_map
= get_imsm_map(dev
, MAP_1
);
7841 if (migr_map
->map_state
== IMSM_T_STATE_NORMAL
&&
7842 dev
->vol
.migr_type
!= MIGR_GEN_MIGR
)
7848 /* return true if we recorded new information */
7849 static int mark_failure(struct intel_super
*super
,
7850 struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
7854 struct imsm_map
*map
;
7855 char buf
[MAX_RAID_SERIAL_LEN
+3];
7856 unsigned int len
, shift
= 0;
7858 /* new failures are always set in map[0] */
7859 map
= get_imsm_map(dev
, MAP_0
);
7861 slot
= get_imsm_disk_slot(map
, idx
);
7865 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
7866 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
7869 memcpy(buf
, disk
->serial
, MAX_RAID_SERIAL_LEN
);
7870 buf
[MAX_RAID_SERIAL_LEN
] = '\000';
7872 if ((len
= strlen(buf
)) >= MAX_RAID_SERIAL_LEN
)
7873 shift
= len
- MAX_RAID_SERIAL_LEN
+ 1;
7874 strncpy((char *)disk
->serial
, &buf
[shift
], MAX_RAID_SERIAL_LEN
);
7876 disk
->status
|= FAILED_DISK
;
7877 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
7878 /* mark failures in second map if second map exists and this disk
7880 * This is valid for migration, initialization and rebuild
7882 if (dev
->vol
.migr_state
) {
7883 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
7884 int slot2
= get_imsm_disk_slot(map2
, idx
);
7886 if (slot2
< map2
->num_members
&& slot2
>= 0)
7887 set_imsm_ord_tbl_ent(map2
, slot2
,
7888 idx
| IMSM_ORD_REBUILD
);
7890 if (map
->failed_disk_num
== 0xff)
7891 map
->failed_disk_num
= slot
;
7893 clear_disk_badblocks(super
->bbm_log
, ord_to_idx(ord
));
7898 static void mark_missing(struct intel_super
*super
,
7899 struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
7901 mark_failure(super
, dev
, disk
, idx
);
7903 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
7906 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
7907 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
7910 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
7914 if (!super
->missing
)
7917 /* When orom adds replacement for missing disk it does
7918 * not remove entry of missing disk, but just updates map with
7919 * new added disk. So it is not enough just to test if there is
7920 * any missing disk, we have to look if there are any failed disks
7921 * in map to stop migration */
7923 dprintf("imsm: mark missing\n");
7924 /* end process for initialization and rebuild only
7926 if (is_gen_migration(dev
) == 0) {
7930 failed
= imsm_count_failed(super
, dev
, MAP_0
);
7931 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7934 end_migration(dev
, super
, map_state
);
7936 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
7937 mark_missing(super
, dev
, &dl
->disk
, dl
->index
);
7938 super
->updates_pending
++;
7941 static unsigned long long imsm_set_array_size(struct imsm_dev
*dev
,
7944 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
7945 unsigned long long array_blocks
;
7946 struct imsm_map
*map
;
7948 if (used_disks
== 0) {
7949 /* when problems occures
7950 * return current array_blocks value
7952 array_blocks
= __le32_to_cpu(dev
->size_high
);
7953 array_blocks
= array_blocks
<< 32;
7954 array_blocks
+= __le32_to_cpu(dev
->size_low
);
7956 return array_blocks
;
7959 /* set array size in metadata
7961 if (new_size
<= 0) {
7962 /* OLCE size change is caused by added disks
7964 map
= get_imsm_map(dev
, MAP_0
);
7965 array_blocks
= blocks_per_member(map
) * used_disks
;
7967 /* Online Volume Size Change
7968 * Using available free space
7970 array_blocks
= new_size
;
7973 array_blocks
= round_size_to_mb(array_blocks
, used_disks
);
7974 dev
->size_low
= __cpu_to_le32((__u32
)array_blocks
);
7975 dev
->size_high
= __cpu_to_le32((__u32
)(array_blocks
>> 32));
7977 return array_blocks
;
7980 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
7982 static void imsm_progress_container_reshape(struct intel_super
*super
)
7984 /* if no device has a migr_state, but some device has a
7985 * different number of members than the previous device, start
7986 * changing the number of devices in this device to match
7989 struct imsm_super
*mpb
= super
->anchor
;
7990 int prev_disks
= -1;
7994 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7995 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
7996 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7997 struct imsm_map
*map2
;
7998 int prev_num_members
;
8000 if (dev
->vol
.migr_state
)
8003 if (prev_disks
== -1)
8004 prev_disks
= map
->num_members
;
8005 if (prev_disks
== map
->num_members
)
8008 /* OK, this array needs to enter reshape mode.
8009 * i.e it needs a migr_state
8012 copy_map_size
= sizeof_imsm_map(map
);
8013 prev_num_members
= map
->num_members
;
8014 map
->num_members
= prev_disks
;
8015 dev
->vol
.migr_state
= 1;
8016 dev
->vol
.curr_migr_unit
= 0;
8017 set_migr_type(dev
, MIGR_GEN_MIGR
);
8018 for (i
= prev_num_members
;
8019 i
< map
->num_members
; i
++)
8020 set_imsm_ord_tbl_ent(map
, i
, i
);
8021 map2
= get_imsm_map(dev
, MAP_1
);
8022 /* Copy the current map */
8023 memcpy(map2
, map
, copy_map_size
);
8024 map2
->num_members
= prev_num_members
;
8026 imsm_set_array_size(dev
, -1);
8027 super
->clean_migration_record_by_mdmon
= 1;
8028 super
->updates_pending
++;
8032 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
8033 * states are handled in imsm_set_disk() with one exception, when a
8034 * resync is stopped due to a new failure this routine will set the
8035 * 'degraded' state for the array.
8037 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
8039 int inst
= a
->info
.container_member
;
8040 struct intel_super
*super
= a
->container
->sb
;
8041 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8042 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8043 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
8044 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8045 __u32 blocks_per_unit
;
8047 if (dev
->vol
.migr_state
&&
8048 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
8049 /* array state change is blocked due to reshape action
8051 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
8052 * - finish the reshape (if last_checkpoint is big and action != reshape)
8053 * - update curr_migr_unit
8055 if (a
->curr_action
== reshape
) {
8056 /* still reshaping, maybe update curr_migr_unit */
8057 goto mark_checkpoint
;
8059 if (a
->last_checkpoint
== 0 && a
->prev_action
== reshape
) {
8060 /* for some reason we aborted the reshape.
8062 * disable automatic metadata rollback
8063 * user action is required to recover process
8066 struct imsm_map
*map2
=
8067 get_imsm_map(dev
, MAP_1
);
8068 dev
->vol
.migr_state
= 0;
8069 set_migr_type(dev
, 0);
8070 dev
->vol
.curr_migr_unit
= 0;
8072 sizeof_imsm_map(map2
));
8073 super
->updates_pending
++;
8076 if (a
->last_checkpoint
>= a
->info
.component_size
) {
8077 unsigned long long array_blocks
;
8081 used_disks
= imsm_num_data_members(dev
, MAP_0
);
8082 if (used_disks
> 0) {
8084 blocks_per_member(map
) *
8087 round_size_to_mb(array_blocks
,
8089 a
->info
.custom_array_size
= array_blocks
;
8090 /* encourage manager to update array
8094 a
->check_reshape
= 1;
8096 /* finalize online capacity expansion/reshape */
8097 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
8099 mdi
->disk
.raid_disk
,
8102 imsm_progress_container_reshape(super
);
8107 /* before we activate this array handle any missing disks */
8108 if (consistent
== 2)
8109 handle_missing(super
, dev
);
8111 if (consistent
== 2 &&
8112 (!is_resync_complete(&a
->info
) ||
8113 map_state
!= IMSM_T_STATE_NORMAL
||
8114 dev
->vol
.migr_state
))
8117 if (is_resync_complete(&a
->info
)) {
8118 /* complete intialization / resync,
8119 * recovery and interrupted recovery is completed in
8122 if (is_resyncing(dev
)) {
8123 dprintf("imsm: mark resync done\n");
8124 end_migration(dev
, super
, map_state
);
8125 super
->updates_pending
++;
8126 a
->last_checkpoint
= 0;
8128 } else if ((!is_resyncing(dev
) && !failed
) &&
8129 (imsm_reshape_blocks_arrays_changes(super
) == 0)) {
8130 /* mark the start of the init process if nothing is failed */
8131 dprintf("imsm: mark resync start\n");
8132 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
8133 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
8135 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
8136 super
->updates_pending
++;
8140 /* skip checkpointing for general migration,
8141 * it is controlled in mdadm
8143 if (is_gen_migration(dev
))
8144 goto skip_mark_checkpoint
;
8146 /* check if we can update curr_migr_unit from resync_start, recovery_start */
8147 blocks_per_unit
= blocks_per_migr_unit(super
, dev
);
8148 if (blocks_per_unit
) {
8152 units
= a
->last_checkpoint
/ blocks_per_unit
;
8155 /* check that we did not overflow 32-bits, and that
8156 * curr_migr_unit needs updating
8158 if (units32
== units
&&
8160 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
8161 dprintf("imsm: mark checkpoint (%u)\n", units32
);
8162 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
8163 super
->updates_pending
++;
8167 skip_mark_checkpoint
:
8168 /* mark dirty / clean */
8169 if (((dev
->vol
.dirty
& RAIDVOL_DIRTY
) && consistent
) ||
8170 (!(dev
->vol
.dirty
& RAIDVOL_DIRTY
) && !consistent
)) {
8171 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
8173 dev
->vol
.dirty
= RAIDVOL_CLEAN
;
8175 dev
->vol
.dirty
= RAIDVOL_DIRTY
;
8176 if (dev
->rwh_policy
== RWH_DISTRIBUTED
)
8177 dev
->vol
.dirty
|= RAIDVOL_DSRECORD_VALID
;
8179 super
->updates_pending
++;
8185 static int imsm_disk_slot_to_ord(struct active_array
*a
, int slot
)
8187 int inst
= a
->info
.container_member
;
8188 struct intel_super
*super
= a
->container
->sb
;
8189 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8190 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8192 if (slot
> map
->num_members
) {
8193 pr_err("imsm: imsm_disk_slot_to_ord %d out of range 0..%d\n",
8194 slot
, map
->num_members
- 1);
8201 return get_imsm_ord_tbl_ent(dev
, slot
, MAP_0
);
8204 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
8206 int inst
= a
->info
.container_member
;
8207 struct intel_super
*super
= a
->container
->sb
;
8208 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8209 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8210 struct imsm_disk
*disk
;
8212 int recovery_not_finished
= 0;
8217 ord
= imsm_disk_slot_to_ord(a
, n
);
8221 dprintf("imsm: set_disk %d:%x\n", n
, state
);
8222 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
8224 /* check for new failures */
8225 if (state
& DS_FAULTY
) {
8226 if (mark_failure(super
, dev
, disk
, ord_to_idx(ord
)))
8227 super
->updates_pending
++;
8230 /* check if in_sync */
8231 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
8232 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
8234 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
8235 super
->updates_pending
++;
8238 failed
= imsm_count_failed(super
, dev
, MAP_0
);
8239 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8241 /* check if recovery complete, newly degraded, or failed */
8242 dprintf("imsm: Detected transition to state ");
8243 switch (map_state
) {
8244 case IMSM_T_STATE_NORMAL
: /* transition to normal state */
8245 dprintf("normal: ");
8246 if (is_rebuilding(dev
)) {
8247 dprintf_cont("while rebuilding");
8248 /* check if recovery is really finished */
8249 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
8250 if (mdi
->recovery_start
!= MaxSector
) {
8251 recovery_not_finished
= 1;
8254 if (recovery_not_finished
) {
8256 dprintf("Rebuild has not finished yet, state not changed");
8257 if (a
->last_checkpoint
< mdi
->recovery_start
) {
8258 a
->last_checkpoint
= mdi
->recovery_start
;
8259 super
->updates_pending
++;
8263 end_migration(dev
, super
, map_state
);
8264 map
= get_imsm_map(dev
, MAP_0
);
8265 map
->failed_disk_num
= ~0;
8266 super
->updates_pending
++;
8267 a
->last_checkpoint
= 0;
8270 if (is_gen_migration(dev
)) {
8271 dprintf_cont("while general migration");
8272 if (a
->last_checkpoint
>= a
->info
.component_size
)
8273 end_migration(dev
, super
, map_state
);
8275 map
->map_state
= map_state
;
8276 map
= get_imsm_map(dev
, MAP_0
);
8277 map
->failed_disk_num
= ~0;
8278 super
->updates_pending
++;
8282 case IMSM_T_STATE_DEGRADED
: /* transition to degraded state */
8283 dprintf_cont("degraded: ");
8284 if (map
->map_state
!= map_state
&& !dev
->vol
.migr_state
) {
8285 dprintf_cont("mark degraded");
8286 map
->map_state
= map_state
;
8287 super
->updates_pending
++;
8288 a
->last_checkpoint
= 0;
8291 if (is_rebuilding(dev
)) {
8292 dprintf_cont("while rebuilding.");
8293 if (map
->map_state
!= map_state
) {
8294 dprintf_cont(" Map state change");
8295 end_migration(dev
, super
, map_state
);
8296 super
->updates_pending
++;
8300 if (is_gen_migration(dev
)) {
8301 dprintf_cont("while general migration");
8302 if (a
->last_checkpoint
>= a
->info
.component_size
)
8303 end_migration(dev
, super
, map_state
);
8305 map
->map_state
= map_state
;
8306 manage_second_map(super
, dev
);
8308 super
->updates_pending
++;
8311 if (is_initializing(dev
)) {
8312 dprintf_cont("while initialization.");
8313 map
->map_state
= map_state
;
8314 super
->updates_pending
++;
8318 case IMSM_T_STATE_FAILED
: /* transition to failed state */
8319 dprintf_cont("failed: ");
8320 if (is_gen_migration(dev
)) {
8321 dprintf_cont("while general migration");
8322 map
->map_state
= map_state
;
8323 super
->updates_pending
++;
8326 if (map
->map_state
!= map_state
) {
8327 dprintf_cont("mark failed");
8328 end_migration(dev
, super
, map_state
);
8329 super
->updates_pending
++;
8330 a
->last_checkpoint
= 0;
8335 dprintf_cont("state %i\n", map_state
);
8340 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
8343 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
8344 unsigned long long dsize
;
8345 unsigned long long sectors
;
8346 unsigned int sector_size
;
8348 get_dev_sector_size(fd
, NULL
, §or_size
);
8349 get_dev_size(fd
, NULL
, &dsize
);
8351 if (mpb_size
> sector_size
) {
8352 /* -1 to account for anchor */
8353 sectors
= mpb_sectors(mpb
, sector_size
) - 1;
8355 /* write the extended mpb to the sectors preceeding the anchor */
8356 if (lseek64(fd
, dsize
- (sector_size
* (2 + sectors
)),
8360 if ((unsigned long long)write(fd
, buf
+ sector_size
,
8361 sector_size
* sectors
) != sector_size
* sectors
)
8365 /* first block is stored on second to last sector of the disk */
8366 if (lseek64(fd
, dsize
- (sector_size
* 2), SEEK_SET
) < 0)
8369 if ((unsigned int)write(fd
, buf
, sector_size
) != sector_size
)
8375 static void imsm_sync_metadata(struct supertype
*container
)
8377 struct intel_super
*super
= container
->sb
;
8379 dprintf("sync metadata: %d\n", super
->updates_pending
);
8380 if (!super
->updates_pending
)
8383 write_super_imsm(container
, 0);
8385 super
->updates_pending
= 0;
8388 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
8390 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
8391 int i
= get_imsm_disk_idx(dev
, idx
, MAP_X
);
8394 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
8398 if (dl
&& is_failed(&dl
->disk
))
8402 dprintf("found %x:%x\n", dl
->major
, dl
->minor
);
8407 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
8408 struct active_array
*a
, int activate_new
,
8409 struct mdinfo
*additional_test_list
)
8411 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
8412 int idx
= get_imsm_disk_idx(dev
, slot
, MAP_X
);
8413 struct imsm_super
*mpb
= super
->anchor
;
8414 struct imsm_map
*map
;
8415 unsigned long long pos
;
8420 __u32 array_start
= 0;
8421 __u32 array_end
= 0;
8423 struct mdinfo
*test_list
;
8425 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
8426 /* If in this array, skip */
8427 for (d
= a
->info
.devs
; d
; d
= d
->next
)
8428 if (d
->state_fd
>= 0 &&
8429 d
->disk
.major
== dl
->major
&&
8430 d
->disk
.minor
== dl
->minor
) {
8431 dprintf("%x:%x already in array\n",
8432 dl
->major
, dl
->minor
);
8437 test_list
= additional_test_list
;
8439 if (test_list
->disk
.major
== dl
->major
&&
8440 test_list
->disk
.minor
== dl
->minor
) {
8441 dprintf("%x:%x already in additional test list\n",
8442 dl
->major
, dl
->minor
);
8445 test_list
= test_list
->next
;
8450 /* skip in use or failed drives */
8451 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
8453 dprintf("%x:%x status (failed: %d index: %d)\n",
8454 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
8458 /* skip pure spares when we are looking for partially
8459 * assimilated drives
8461 if (dl
->index
== -1 && !activate_new
)
8464 if (!drive_validate_sector_size(super
, dl
))
8467 /* Does this unused device have the requisite free space?
8468 * It needs to be able to cover all member volumes
8470 ex
= get_extents(super
, dl
);
8472 dprintf("cannot get extents\n");
8475 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8476 dev
= get_imsm_dev(super
, i
);
8477 map
= get_imsm_map(dev
, MAP_0
);
8479 /* check if this disk is already a member of
8482 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
8488 array_start
= pba_of_lba0(map
);
8489 array_end
= array_start
+
8490 blocks_per_member(map
) - 1;
8493 /* check that we can start at pba_of_lba0 with
8494 * blocks_per_member of space
8496 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
8500 pos
= ex
[j
].start
+ ex
[j
].size
;
8502 } while (ex
[j
-1].size
);
8509 if (i
< mpb
->num_raid_devs
) {
8510 dprintf("%x:%x does not have %u to %u available\n",
8511 dl
->major
, dl
->minor
, array_start
, array_end
);
8521 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
8523 struct imsm_dev
*dev2
;
8524 struct imsm_map
*map
;
8530 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
8532 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
, MAP_0
);
8533 if (state
== IMSM_T_STATE_FAILED
) {
8534 map
= get_imsm_map(dev2
, MAP_0
);
8537 for (slot
= 0; slot
< map
->num_members
; slot
++) {
8539 * Check if failed disks are deleted from intel
8540 * disk list or are marked to be deleted
8542 idx
= get_imsm_disk_idx(dev2
, slot
, MAP_X
);
8543 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
8545 * Do not rebuild the array if failed disks
8546 * from failed sub-array are not removed from
8550 is_failed(&idisk
->disk
) &&
8551 (idisk
->action
!= DISK_REMOVE
))
8559 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
8560 struct metadata_update
**updates
)
8563 * Find a device with unused free space and use it to replace a
8564 * failed/vacant region in an array. We replace failed regions one a
8565 * array at a time. The result is that a new spare disk will be added
8566 * to the first failed array and after the monitor has finished
8567 * propagating failures the remainder will be consumed.
8569 * FIXME add a capability for mdmon to request spares from another
8573 struct intel_super
*super
= a
->container
->sb
;
8574 int inst
= a
->info
.container_member
;
8575 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8576 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8577 int failed
= a
->info
.array
.raid_disks
;
8578 struct mdinfo
*rv
= NULL
;
8581 struct metadata_update
*mu
;
8583 struct imsm_update_activate_spare
*u
;
8588 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
8589 if ((d
->curr_state
& DS_FAULTY
) &&
8591 /* wait for Removal to happen */
8593 if (d
->state_fd
>= 0)
8597 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
8598 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
8600 if (imsm_reshape_blocks_arrays_changes(super
))
8603 /* Cannot activate another spare if rebuild is in progress already
8605 if (is_rebuilding(dev
)) {
8606 dprintf("imsm: No spare activation allowed. Rebuild in progress already.\n");
8610 if (a
->info
.array
.level
== 4)
8611 /* No repair for takeovered array
8612 * imsm doesn't support raid4
8616 if (imsm_check_degraded(super
, dev
, failed
, MAP_0
) !=
8617 IMSM_T_STATE_DEGRADED
)
8620 if (get_imsm_map(dev
, MAP_0
)->map_state
== IMSM_T_STATE_UNINITIALIZED
) {
8621 dprintf("imsm: No spare activation allowed. Volume is not initialized.\n");
8626 * If there are any failed disks check state of the other volume.
8627 * Block rebuild if the another one is failed until failed disks
8628 * are removed from container.
8631 dprintf("found failed disks in %.*s, check if there anotherfailed sub-array.\n",
8632 MAX_RAID_SERIAL_LEN
, dev
->volume
);
8633 /* check if states of the other volumes allow for rebuild */
8634 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
8636 allowed
= imsm_rebuild_allowed(a
->container
,
8644 /* For each slot, if it is not working, find a spare */
8645 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
8646 for (d
= a
->info
.devs
; d
; d
= d
->next
)
8647 if (d
->disk
.raid_disk
== i
)
8649 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
8650 if (d
&& (d
->state_fd
>= 0))
8654 * OK, this device needs recovery. Try to re-add the
8655 * previous occupant of this slot, if this fails see if
8656 * we can continue the assimilation of a spare that was
8657 * partially assimilated, finally try to activate a new
8660 dl
= imsm_readd(super
, i
, a
);
8662 dl
= imsm_add_spare(super
, i
, a
, 0, rv
);
8664 dl
= imsm_add_spare(super
, i
, a
, 1, rv
);
8668 /* found a usable disk with enough space */
8669 di
= xcalloc(1, sizeof(*di
));
8671 /* dl->index will be -1 in the case we are activating a
8672 * pristine spare. imsm_process_update() will create a
8673 * new index in this case. Once a disk is found to be
8674 * failed in all member arrays it is kicked from the
8677 di
->disk
.number
= dl
->index
;
8679 /* (ab)use di->devs to store a pointer to the device
8682 di
->devs
= (struct mdinfo
*) dl
;
8684 di
->disk
.raid_disk
= i
;
8685 di
->disk
.major
= dl
->major
;
8686 di
->disk
.minor
= dl
->minor
;
8688 di
->recovery_start
= 0;
8689 di
->data_offset
= pba_of_lba0(map
);
8690 di
->component_size
= a
->info
.component_size
;
8691 di
->container_member
= inst
;
8692 di
->bb
.supported
= 1;
8693 if (dev
->rwh_policy
== RWH_DISTRIBUTED
) {
8694 di
->consistency_policy
= CONSISTENCY_POLICY_PPL
;
8695 di
->ppl_sector
= get_ppl_sector(super
, inst
);
8696 di
->ppl_size
= (PPL_HEADER_SIZE
+ PPL_ENTRY_SPACE
) >> 9;
8698 super
->random
= random32();
8702 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
8703 i
, di
->data_offset
);
8707 /* No spares found */
8709 /* Now 'rv' has a list of devices to return.
8710 * Create a metadata_update record to update the
8711 * disk_ord_tbl for the array
8713 mu
= xmalloc(sizeof(*mu
));
8714 mu
->buf
= xcalloc(num_spares
,
8715 sizeof(struct imsm_update_activate_spare
));
8717 mu
->space_list
= NULL
;
8718 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
8719 mu
->next
= *updates
;
8720 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
8722 for (di
= rv
; di
; di
= di
->next
) {
8723 u
->type
= update_activate_spare
;
8724 u
->dl
= (struct dl
*) di
->devs
;
8726 u
->slot
= di
->disk
.raid_disk
;
8737 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
8739 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
8740 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8741 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, MAP_0
);
8742 struct disk_info
*inf
= get_disk_info(u
);
8743 struct imsm_disk
*disk
;
8747 for (i
= 0; i
< map
->num_members
; i
++) {
8748 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, MAP_X
));
8749 for (j
= 0; j
< new_map
->num_members
; j
++)
8750 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
8757 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
8761 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
8762 if (dl
->major
== major
&& dl
->minor
== minor
)
8767 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
8773 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
8774 if (dl
->major
== major
&& dl
->minor
== minor
) {
8777 prev
->next
= dl
->next
;
8779 super
->disks
= dl
->next
;
8781 __free_imsm_disk(dl
);
8782 dprintf("removed %x:%x\n", major
, minor
);
8790 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
8792 static int add_remove_disk_update(struct intel_super
*super
)
8794 int check_degraded
= 0;
8797 /* add/remove some spares to/from the metadata/contrainer */
8798 while (super
->disk_mgmt_list
) {
8799 struct dl
*disk_cfg
;
8801 disk_cfg
= super
->disk_mgmt_list
;
8802 super
->disk_mgmt_list
= disk_cfg
->next
;
8803 disk_cfg
->next
= NULL
;
8805 if (disk_cfg
->action
== DISK_ADD
) {
8806 disk_cfg
->next
= super
->disks
;
8807 super
->disks
= disk_cfg
;
8809 dprintf("added %x:%x\n",
8810 disk_cfg
->major
, disk_cfg
->minor
);
8811 } else if (disk_cfg
->action
== DISK_REMOVE
) {
8812 dprintf("Disk remove action processed: %x.%x\n",
8813 disk_cfg
->major
, disk_cfg
->minor
);
8814 disk
= get_disk_super(super
,
8818 /* store action status */
8819 disk
->action
= DISK_REMOVE
;
8820 /* remove spare disks only */
8821 if (disk
->index
== -1) {
8822 remove_disk_super(super
,
8827 /* release allocate disk structure */
8828 __free_imsm_disk(disk_cfg
);
8831 return check_degraded
;
8834 static int apply_reshape_migration_update(struct imsm_update_reshape_migration
*u
,
8835 struct intel_super
*super
,
8838 struct intel_dev
*id
;
8839 void **tofree
= NULL
;
8842 dprintf("(enter)\n");
8843 if (u
->subdev
< 0 || u
->subdev
> 1) {
8844 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
8847 if (space_list
== NULL
|| *space_list
== NULL
) {
8848 dprintf("imsm: Error: Memory is not allocated\n");
8852 for (id
= super
->devlist
; id
; id
= id
->next
) {
8853 if (id
->index
== (unsigned)u
->subdev
) {
8854 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
8855 struct imsm_map
*map
;
8856 struct imsm_dev
*new_dev
=
8857 (struct imsm_dev
*)*space_list
;
8858 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
8860 struct dl
*new_disk
;
8862 if (new_dev
== NULL
)
8864 *space_list
= **space_list
;
8865 memcpy(new_dev
, dev
, sizeof_imsm_dev(dev
, 0));
8866 map
= get_imsm_map(new_dev
, MAP_0
);
8868 dprintf("imsm: Error: migration in progress");
8872 to_state
= map
->map_state
;
8873 if ((u
->new_level
== 5) && (map
->raid_level
== 0)) {
8875 /* this should not happen */
8876 if (u
->new_disks
[0] < 0) {
8877 map
->failed_disk_num
=
8878 map
->num_members
- 1;
8879 to_state
= IMSM_T_STATE_DEGRADED
;
8881 to_state
= IMSM_T_STATE_NORMAL
;
8883 migrate(new_dev
, super
, to_state
, MIGR_GEN_MIGR
);
8884 if (u
->new_level
> -1)
8885 map
->raid_level
= u
->new_level
;
8886 migr_map
= get_imsm_map(new_dev
, MAP_1
);
8887 if ((u
->new_level
== 5) &&
8888 (migr_map
->raid_level
== 0)) {
8889 int ord
= map
->num_members
- 1;
8890 migr_map
->num_members
--;
8891 if (u
->new_disks
[0] < 0)
8892 ord
|= IMSM_ORD_REBUILD
;
8893 set_imsm_ord_tbl_ent(map
,
8894 map
->num_members
- 1,
8898 tofree
= (void **)dev
;
8900 /* update chunk size
8902 if (u
->new_chunksize
> 0) {
8903 unsigned long long num_data_stripes
;
8905 imsm_num_data_members(dev
, MAP_0
);
8907 if (used_disks
== 0)
8910 map
->blocks_per_strip
=
8911 __cpu_to_le16(u
->new_chunksize
* 2);
8913 (join_u32(dev
->size_low
, dev
->size_high
)
8915 num_data_stripes
/= map
->blocks_per_strip
;
8916 num_data_stripes
/= map
->num_domains
;
8917 set_num_data_stripes(map
, num_data_stripes
);
8922 if (u
->new_level
!= 5 || migr_map
->raid_level
!= 0 ||
8923 migr_map
->raid_level
== map
->raid_level
)
8926 if (u
->new_disks
[0] >= 0) {
8929 new_disk
= get_disk_super(super
,
8930 major(u
->new_disks
[0]),
8931 minor(u
->new_disks
[0]));
8932 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
8933 major(u
->new_disks
[0]),
8934 minor(u
->new_disks
[0]),
8935 new_disk
, new_disk
->index
);
8936 if (new_disk
== NULL
)
8937 goto error_disk_add
;
8939 new_disk
->index
= map
->num_members
- 1;
8940 /* slot to fill in autolayout
8942 new_disk
->raiddisk
= new_disk
->index
;
8943 new_disk
->disk
.status
|= CONFIGURED_DISK
;
8944 new_disk
->disk
.status
&= ~SPARE_DISK
;
8946 goto error_disk_add
;
8949 *tofree
= *space_list
;
8950 /* calculate new size
8952 imsm_set_array_size(new_dev
, -1);
8959 *space_list
= tofree
;
8963 dprintf("Error: imsm: Cannot find disk.\n");
8967 static int apply_size_change_update(struct imsm_update_size_change
*u
,
8968 struct intel_super
*super
)
8970 struct intel_dev
*id
;
8973 dprintf("(enter)\n");
8974 if (u
->subdev
< 0 || u
->subdev
> 1) {
8975 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
8979 for (id
= super
->devlist
; id
; id
= id
->next
) {
8980 if (id
->index
== (unsigned)u
->subdev
) {
8981 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
8982 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8983 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
8984 unsigned long long blocks_per_member
;
8985 unsigned long long num_data_stripes
;
8987 /* calculate new size
8989 blocks_per_member
= u
->new_size
/ used_disks
;
8990 num_data_stripes
= blocks_per_member
/
8991 map
->blocks_per_strip
;
8992 num_data_stripes
/= map
->num_domains
;
8993 dprintf("(size: %llu, blocks per member: %llu, num_data_stipes: %llu)\n",
8994 u
->new_size
, blocks_per_member
,
8996 set_blocks_per_member(map
, blocks_per_member
);
8997 set_num_data_stripes(map
, num_data_stripes
);
8998 imsm_set_array_size(dev
, u
->new_size
);
9008 static int apply_update_activate_spare(struct imsm_update_activate_spare
*u
,
9009 struct intel_super
*super
,
9010 struct active_array
*active_array
)
9012 struct imsm_super
*mpb
= super
->anchor
;
9013 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
9014 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9015 struct imsm_map
*migr_map
;
9016 struct active_array
*a
;
9017 struct imsm_disk
*disk
;
9024 int second_map_created
= 0;
9026 for (; u
; u
= u
->next
) {
9027 victim
= get_imsm_disk_idx(dev
, u
->slot
, MAP_X
);
9032 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
9037 pr_err("error: imsm_activate_spare passed an unknown disk (index: %d)\n",
9042 /* count failures (excluding rebuilds and the victim)
9043 * to determine map[0] state
9046 for (i
= 0; i
< map
->num_members
; i
++) {
9049 disk
= get_imsm_disk(super
,
9050 get_imsm_disk_idx(dev
, i
, MAP_X
));
9051 if (!disk
|| is_failed(disk
))
9055 /* adding a pristine spare, assign a new index */
9056 if (dl
->index
< 0) {
9057 dl
->index
= super
->anchor
->num_disks
;
9058 super
->anchor
->num_disks
++;
9061 disk
->status
|= CONFIGURED_DISK
;
9062 disk
->status
&= ~SPARE_DISK
;
9065 to_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
9066 if (!second_map_created
) {
9067 second_map_created
= 1;
9068 map
->map_state
= IMSM_T_STATE_DEGRADED
;
9069 migrate(dev
, super
, to_state
, MIGR_REBUILD
);
9071 map
->map_state
= to_state
;
9072 migr_map
= get_imsm_map(dev
, MAP_1
);
9073 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
9074 set_imsm_ord_tbl_ent(migr_map
, u
->slot
,
9075 dl
->index
| IMSM_ORD_REBUILD
);
9077 /* update the family_num to mark a new container
9078 * generation, being careful to record the existing
9079 * family_num in orig_family_num to clean up after
9080 * earlier mdadm versions that neglected to set it.
9082 if (mpb
->orig_family_num
== 0)
9083 mpb
->orig_family_num
= mpb
->family_num
;
9084 mpb
->family_num
+= super
->random
;
9086 /* count arrays using the victim in the metadata */
9088 for (a
= active_array
; a
; a
= a
->next
) {
9089 dev
= get_imsm_dev(super
, a
->info
.container_member
);
9090 map
= get_imsm_map(dev
, MAP_0
);
9092 if (get_imsm_disk_slot(map
, victim
) >= 0)
9096 /* delete the victim if it is no longer being
9102 /* We know that 'manager' isn't touching anything,
9103 * so it is safe to delete
9105 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
9106 if ((*dlp
)->index
== victim
)
9109 /* victim may be on the missing list */
9111 for (dlp
= &super
->missing
; *dlp
;
9112 dlp
= &(*dlp
)->next
)
9113 if ((*dlp
)->index
== victim
)
9115 imsm_delete(super
, dlp
, victim
);
9122 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
9123 struct intel_super
*super
,
9126 struct dl
*new_disk
;
9127 struct intel_dev
*id
;
9129 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
9130 int disk_count
= u
->old_raid_disks
;
9131 void **tofree
= NULL
;
9132 int devices_to_reshape
= 1;
9133 struct imsm_super
*mpb
= super
->anchor
;
9135 unsigned int dev_id
;
9137 dprintf("(enter)\n");
9139 /* enable spares to use in array */
9140 for (i
= 0; i
< delta_disks
; i
++) {
9141 new_disk
= get_disk_super(super
,
9142 major(u
->new_disks
[i
]),
9143 minor(u
->new_disks
[i
]));
9144 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
9145 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
9146 new_disk
, new_disk
->index
);
9147 if (new_disk
== NULL
||
9148 (new_disk
->index
>= 0 &&
9149 new_disk
->index
< u
->old_raid_disks
))
9150 goto update_reshape_exit
;
9151 new_disk
->index
= disk_count
++;
9152 /* slot to fill in autolayout
9154 new_disk
->raiddisk
= new_disk
->index
;
9155 new_disk
->disk
.status
|=
9157 new_disk
->disk
.status
&= ~SPARE_DISK
;
9160 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
9161 mpb
->num_raid_devs
);
9162 /* manage changes in volume
9164 for (dev_id
= 0; dev_id
< mpb
->num_raid_devs
; dev_id
++) {
9165 void **sp
= *space_list
;
9166 struct imsm_dev
*newdev
;
9167 struct imsm_map
*newmap
, *oldmap
;
9169 for (id
= super
->devlist
; id
; id
= id
->next
) {
9170 if (id
->index
== dev_id
)
9179 /* Copy the dev, but not (all of) the map */
9180 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
9181 oldmap
= get_imsm_map(id
->dev
, MAP_0
);
9182 newmap
= get_imsm_map(newdev
, MAP_0
);
9183 /* Copy the current map */
9184 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
9185 /* update one device only
9187 if (devices_to_reshape
) {
9188 dprintf("imsm: modifying subdev: %i\n",
9190 devices_to_reshape
--;
9191 newdev
->vol
.migr_state
= 1;
9192 newdev
->vol
.curr_migr_unit
= 0;
9193 set_migr_type(newdev
, MIGR_GEN_MIGR
);
9194 newmap
->num_members
= u
->new_raid_disks
;
9195 for (i
= 0; i
< delta_disks
; i
++) {
9196 set_imsm_ord_tbl_ent(newmap
,
9197 u
->old_raid_disks
+ i
,
9198 u
->old_raid_disks
+ i
);
9200 /* New map is correct, now need to save old map
9202 newmap
= get_imsm_map(newdev
, MAP_1
);
9203 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
9205 imsm_set_array_size(newdev
, -1);
9208 sp
= (void **)id
->dev
;
9213 /* Clear migration record */
9214 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
9217 *space_list
= tofree
;
9220 update_reshape_exit
:
9225 static int apply_takeover_update(struct imsm_update_takeover
*u
,
9226 struct intel_super
*super
,
9229 struct imsm_dev
*dev
= NULL
;
9230 struct intel_dev
*dv
;
9231 struct imsm_dev
*dev_new
;
9232 struct imsm_map
*map
;
9236 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
9237 if (dv
->index
== (unsigned int)u
->subarray
) {
9245 map
= get_imsm_map(dev
, MAP_0
);
9247 if (u
->direction
== R10_TO_R0
) {
9248 unsigned long long num_data_stripes
;
9250 map
->num_domains
= 1;
9251 num_data_stripes
= blocks_per_member(map
);
9252 num_data_stripes
/= map
->blocks_per_strip
;
9253 num_data_stripes
/= map
->num_domains
;
9254 set_num_data_stripes(map
, num_data_stripes
);
9256 /* Number of failed disks must be half of initial disk number */
9257 if (imsm_count_failed(super
, dev
, MAP_0
) !=
9258 (map
->num_members
/ 2))
9261 /* iterate through devices to mark removed disks as spare */
9262 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
9263 if (dm
->disk
.status
& FAILED_DISK
) {
9264 int idx
= dm
->index
;
9265 /* update indexes on the disk list */
9266 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
9267 the index values will end up being correct.... NB */
9268 for (du
= super
->disks
; du
; du
= du
->next
)
9269 if (du
->index
> idx
)
9271 /* mark as spare disk */
9276 map
->num_members
= map
->num_members
/ 2;
9277 map
->map_state
= IMSM_T_STATE_NORMAL
;
9278 map
->num_domains
= 1;
9279 map
->raid_level
= 0;
9280 map
->failed_disk_num
= -1;
9283 if (u
->direction
== R0_TO_R10
) {
9285 /* update slots in current disk list */
9286 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
9290 /* create new *missing* disks */
9291 for (i
= 0; i
< map
->num_members
; i
++) {
9292 space
= *space_list
;
9295 *space_list
= *space
;
9297 memcpy(du
, super
->disks
, sizeof(*du
));
9301 du
->index
= (i
* 2) + 1;
9302 sprintf((char *)du
->disk
.serial
,
9303 " MISSING_%d", du
->index
);
9304 sprintf((char *)du
->serial
,
9305 "MISSING_%d", du
->index
);
9306 du
->next
= super
->missing
;
9307 super
->missing
= du
;
9309 /* create new dev and map */
9310 space
= *space_list
;
9313 *space_list
= *space
;
9314 dev_new
= (void *)space
;
9315 memcpy(dev_new
, dev
, sizeof(*dev
));
9316 /* update new map */
9317 map
= get_imsm_map(dev_new
, MAP_0
);
9318 map
->num_members
= map
->num_members
* 2;
9319 map
->map_state
= IMSM_T_STATE_DEGRADED
;
9320 map
->num_domains
= 2;
9321 map
->raid_level
= 1;
9322 /* replace dev<->dev_new */
9325 /* update disk order table */
9326 for (du
= super
->disks
; du
; du
= du
->next
)
9328 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
9329 for (du
= super
->missing
; du
; du
= du
->next
)
9330 if (du
->index
>= 0) {
9331 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
9332 mark_missing(super
, dv
->dev
, &du
->disk
, du
->index
);
9338 static void imsm_process_update(struct supertype
*st
,
9339 struct metadata_update
*update
)
9342 * crack open the metadata_update envelope to find the update record
9343 * update can be one of:
9344 * update_reshape_container_disks - all the arrays in the container
9345 * are being reshaped to have more devices. We need to mark
9346 * the arrays for general migration and convert selected spares
9347 * into active devices.
9348 * update_activate_spare - a spare device has replaced a failed
9349 * device in an array, update the disk_ord_tbl. If this disk is
9350 * present in all member arrays then also clear the SPARE_DISK
9352 * update_create_array
9354 * update_rename_array
9355 * update_add_remove_disk
9357 struct intel_super
*super
= st
->sb
;
9358 struct imsm_super
*mpb
;
9359 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
9361 /* update requires a larger buf but the allocation failed */
9362 if (super
->next_len
&& !super
->next_buf
) {
9363 super
->next_len
= 0;
9367 if (super
->next_buf
) {
9368 memcpy(super
->next_buf
, super
->buf
, super
->len
);
9370 super
->len
= super
->next_len
;
9371 super
->buf
= super
->next_buf
;
9373 super
->next_len
= 0;
9374 super
->next_buf
= NULL
;
9377 mpb
= super
->anchor
;
9380 case update_general_migration_checkpoint
: {
9381 struct intel_dev
*id
;
9382 struct imsm_update_general_migration_checkpoint
*u
=
9383 (void *)update
->buf
;
9385 dprintf("called for update_general_migration_checkpoint\n");
9387 /* find device under general migration */
9388 for (id
= super
->devlist
; id
; id
= id
->next
) {
9389 if (is_gen_migration(id
->dev
)) {
9390 id
->dev
->vol
.curr_migr_unit
=
9391 __cpu_to_le32(u
->curr_migr_unit
);
9392 super
->updates_pending
++;
9397 case update_takeover
: {
9398 struct imsm_update_takeover
*u
= (void *)update
->buf
;
9399 if (apply_takeover_update(u
, super
, &update
->space_list
)) {
9400 imsm_update_version_info(super
);
9401 super
->updates_pending
++;
9406 case update_reshape_container_disks
: {
9407 struct imsm_update_reshape
*u
= (void *)update
->buf
;
9408 if (apply_reshape_container_disks_update(
9409 u
, super
, &update
->space_list
))
9410 super
->updates_pending
++;
9413 case update_reshape_migration
: {
9414 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
9415 if (apply_reshape_migration_update(
9416 u
, super
, &update
->space_list
))
9417 super
->updates_pending
++;
9420 case update_size_change
: {
9421 struct imsm_update_size_change
*u
= (void *)update
->buf
;
9422 if (apply_size_change_update(u
, super
))
9423 super
->updates_pending
++;
9426 case update_activate_spare
: {
9427 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
9428 if (apply_update_activate_spare(u
, super
, st
->arrays
))
9429 super
->updates_pending
++;
9432 case update_create_array
: {
9433 /* someone wants to create a new array, we need to be aware of
9434 * a few races/collisions:
9435 * 1/ 'Create' called by two separate instances of mdadm
9436 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
9437 * devices that have since been assimilated via
9439 * In the event this update can not be carried out mdadm will
9440 * (FIX ME) notice that its update did not take hold.
9442 struct imsm_update_create_array
*u
= (void *) update
->buf
;
9443 struct intel_dev
*dv
;
9444 struct imsm_dev
*dev
;
9445 struct imsm_map
*map
, *new_map
;
9446 unsigned long long start
, end
;
9447 unsigned long long new_start
, new_end
;
9449 struct disk_info
*inf
;
9452 /* handle racing creates: first come first serve */
9453 if (u
->dev_idx
< mpb
->num_raid_devs
) {
9454 dprintf("subarray %d already defined\n", u
->dev_idx
);
9458 /* check update is next in sequence */
9459 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
9460 dprintf("can not create array %d expected index %d\n",
9461 u
->dev_idx
, mpb
->num_raid_devs
);
9465 new_map
= get_imsm_map(&u
->dev
, MAP_0
);
9466 new_start
= pba_of_lba0(new_map
);
9467 new_end
= new_start
+ blocks_per_member(new_map
);
9468 inf
= get_disk_info(u
);
9470 /* handle activate_spare versus create race:
9471 * check to make sure that overlapping arrays do not include
9474 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
9475 dev
= get_imsm_dev(super
, i
);
9476 map
= get_imsm_map(dev
, MAP_0
);
9477 start
= pba_of_lba0(map
);
9478 end
= start
+ blocks_per_member(map
);
9479 if ((new_start
>= start
&& new_start
<= end
) ||
9480 (start
>= new_start
&& start
<= new_end
))
9485 if (disks_overlap(super
, i
, u
)) {
9486 dprintf("arrays overlap\n");
9491 /* check that prepare update was successful */
9492 if (!update
->space
) {
9493 dprintf("prepare update failed\n");
9497 /* check that all disks are still active before committing
9498 * changes. FIXME: could we instead handle this by creating a
9499 * degraded array? That's probably not what the user expects,
9500 * so better to drop this update on the floor.
9502 for (i
= 0; i
< new_map
->num_members
; i
++) {
9503 dl
= serial_to_dl(inf
[i
].serial
, super
);
9505 dprintf("disk disappeared\n");
9510 super
->updates_pending
++;
9512 /* convert spares to members and fixup ord_tbl */
9513 for (i
= 0; i
< new_map
->num_members
; i
++) {
9514 dl
= serial_to_dl(inf
[i
].serial
, super
);
9515 if (dl
->index
== -1) {
9516 dl
->index
= mpb
->num_disks
;
9518 dl
->disk
.status
|= CONFIGURED_DISK
;
9519 dl
->disk
.status
&= ~SPARE_DISK
;
9521 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
9526 update
->space
= NULL
;
9527 imsm_copy_dev(dev
, &u
->dev
);
9528 dv
->index
= u
->dev_idx
;
9529 dv
->next
= super
->devlist
;
9530 super
->devlist
= dv
;
9531 mpb
->num_raid_devs
++;
9533 imsm_update_version_info(super
);
9536 /* mdmon knows how to release update->space, but not
9537 * ((struct intel_dev *) update->space)->dev
9539 if (update
->space
) {
9545 case update_kill_array
: {
9546 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
9547 int victim
= u
->dev_idx
;
9548 struct active_array
*a
;
9549 struct intel_dev
**dp
;
9550 struct imsm_dev
*dev
;
9552 /* sanity check that we are not affecting the uuid of
9553 * active arrays, or deleting an active array
9555 * FIXME when immutable ids are available, but note that
9556 * we'll also need to fixup the invalidated/active
9557 * subarray indexes in mdstat
9559 for (a
= st
->arrays
; a
; a
= a
->next
)
9560 if (a
->info
.container_member
>= victim
)
9562 /* by definition if mdmon is running at least one array
9563 * is active in the container, so checking
9564 * mpb->num_raid_devs is just extra paranoia
9566 dev
= get_imsm_dev(super
, victim
);
9567 if (a
|| !dev
|| mpb
->num_raid_devs
== 1) {
9568 dprintf("failed to delete subarray-%d\n", victim
);
9572 for (dp
= &super
->devlist
; *dp
;)
9573 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
9576 if ((*dp
)->index
> (unsigned)victim
)
9580 mpb
->num_raid_devs
--;
9581 super
->updates_pending
++;
9584 case update_rename_array
: {
9585 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
9586 char name
[MAX_RAID_SERIAL_LEN
+1];
9587 int target
= u
->dev_idx
;
9588 struct active_array
*a
;
9589 struct imsm_dev
*dev
;
9591 /* sanity check that we are not affecting the uuid of
9594 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
9595 name
[MAX_RAID_SERIAL_LEN
] = '\0';
9596 for (a
= st
->arrays
; a
; a
= a
->next
)
9597 if (a
->info
.container_member
== target
)
9599 dev
= get_imsm_dev(super
, u
->dev_idx
);
9600 if (a
|| !dev
|| !check_name(super
, name
, 1)) {
9601 dprintf("failed to rename subarray-%d\n", target
);
9605 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
9606 super
->updates_pending
++;
9609 case update_add_remove_disk
: {
9610 /* we may be able to repair some arrays if disks are
9611 * being added, check the status of add_remove_disk
9612 * if discs has been added.
9614 if (add_remove_disk_update(super
)) {
9615 struct active_array
*a
;
9617 super
->updates_pending
++;
9618 for (a
= st
->arrays
; a
; a
= a
->next
)
9619 a
->check_degraded
= 1;
9623 case update_prealloc_badblocks_mem
:
9625 case update_rwh_policy
: {
9626 struct imsm_update_rwh_policy
*u
= (void *)update
->buf
;
9627 int target
= u
->dev_idx
;
9628 struct imsm_dev
*dev
= get_imsm_dev(super
, target
);
9630 dprintf("could not find subarray-%d\n", target
);
9634 if (dev
->rwh_policy
!= u
->new_policy
) {
9635 dev
->rwh_policy
= u
->new_policy
;
9636 super
->updates_pending
++;
9641 pr_err("error: unsuported process update type:(type: %d)\n", type
);
9645 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
);
9647 static int imsm_prepare_update(struct supertype
*st
,
9648 struct metadata_update
*update
)
9651 * Allocate space to hold new disk entries, raid-device entries or a new
9652 * mpb if necessary. The manager synchronously waits for updates to
9653 * complete in the monitor, so new mpb buffers allocated here can be
9654 * integrated by the monitor thread without worrying about live pointers
9655 * in the manager thread.
9657 enum imsm_update_type type
;
9658 struct intel_super
*super
= st
->sb
;
9659 unsigned int sector_size
= super
->sector_size
;
9660 struct imsm_super
*mpb
= super
->anchor
;
9664 if (update
->len
< (int)sizeof(type
))
9667 type
= *(enum imsm_update_type
*) update
->buf
;
9670 case update_general_migration_checkpoint
:
9671 if (update
->len
< (int)sizeof(struct imsm_update_general_migration_checkpoint
))
9673 dprintf("called for update_general_migration_checkpoint\n");
9675 case update_takeover
: {
9676 struct imsm_update_takeover
*u
= (void *)update
->buf
;
9677 if (update
->len
< (int)sizeof(*u
))
9679 if (u
->direction
== R0_TO_R10
) {
9680 void **tail
= (void **)&update
->space_list
;
9681 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subarray
);
9682 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9683 int num_members
= map
->num_members
;
9686 /* allocate memory for added disks */
9687 for (i
= 0; i
< num_members
; i
++) {
9688 size
= sizeof(struct dl
);
9689 space
= xmalloc(size
);
9694 /* allocate memory for new device */
9695 size
= sizeof_imsm_dev(super
->devlist
->dev
, 0) +
9696 (num_members
* sizeof(__u32
));
9697 space
= xmalloc(size
);
9701 len
= disks_to_mpb_size(num_members
* 2);
9706 case update_reshape_container_disks
: {
9707 /* Every raid device in the container is about to
9708 * gain some more devices, and we will enter a
9710 * So each 'imsm_map' will be bigger, and the imsm_vol
9711 * will now hold 2 of them.
9712 * Thus we need new 'struct imsm_dev' allocations sized
9713 * as sizeof_imsm_dev but with more devices in both maps.
9715 struct imsm_update_reshape
*u
= (void *)update
->buf
;
9716 struct intel_dev
*dl
;
9717 void **space_tail
= (void**)&update
->space_list
;
9719 if (update
->len
< (int)sizeof(*u
))
9722 dprintf("for update_reshape\n");
9724 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
9725 int size
= sizeof_imsm_dev(dl
->dev
, 1);
9727 if (u
->new_raid_disks
> u
->old_raid_disks
)
9728 size
+= sizeof(__u32
)*2*
9729 (u
->new_raid_disks
- u
->old_raid_disks
);
9736 len
= disks_to_mpb_size(u
->new_raid_disks
);
9737 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
9740 case update_reshape_migration
: {
9741 /* for migration level 0->5 we need to add disks
9742 * so the same as for container operation we will copy
9743 * device to the bigger location.
9744 * in memory prepared device and new disk area are prepared
9745 * for usage in process update
9747 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
9748 struct intel_dev
*id
;
9749 void **space_tail
= (void **)&update
->space_list
;
9752 int current_level
= -1;
9754 if (update
->len
< (int)sizeof(*u
))
9757 dprintf("for update_reshape\n");
9759 /* add space for bigger array in update
9761 for (id
= super
->devlist
; id
; id
= id
->next
) {
9762 if (id
->index
== (unsigned)u
->subdev
) {
9763 size
= sizeof_imsm_dev(id
->dev
, 1);
9764 if (u
->new_raid_disks
> u
->old_raid_disks
)
9765 size
+= sizeof(__u32
)*2*
9766 (u
->new_raid_disks
- u
->old_raid_disks
);
9774 if (update
->space_list
== NULL
)
9777 /* add space for disk in update
9779 size
= sizeof(struct dl
);
9785 /* add spare device to update
9787 for (id
= super
->devlist
; id
; id
= id
->next
)
9788 if (id
->index
== (unsigned)u
->subdev
) {
9789 struct imsm_dev
*dev
;
9790 struct imsm_map
*map
;
9792 dev
= get_imsm_dev(super
, u
->subdev
);
9793 map
= get_imsm_map(dev
, MAP_0
);
9794 current_level
= map
->raid_level
;
9797 if (u
->new_level
== 5 && u
->new_level
!= current_level
) {
9798 struct mdinfo
*spares
;
9800 spares
= get_spares_for_grow(st
);
9808 makedev(dev
->disk
.major
,
9810 dl
= get_disk_super(super
,
9813 dl
->index
= u
->old_raid_disks
;
9819 len
= disks_to_mpb_size(u
->new_raid_disks
);
9820 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
9823 case update_size_change
: {
9824 if (update
->len
< (int)sizeof(struct imsm_update_size_change
))
9828 case update_activate_spare
: {
9829 if (update
->len
< (int)sizeof(struct imsm_update_activate_spare
))
9833 case update_create_array
: {
9834 struct imsm_update_create_array
*u
= (void *) update
->buf
;
9835 struct intel_dev
*dv
;
9836 struct imsm_dev
*dev
= &u
->dev
;
9837 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9839 struct disk_info
*inf
;
9843 if (update
->len
< (int)sizeof(*u
))
9846 inf
= get_disk_info(u
);
9847 len
= sizeof_imsm_dev(dev
, 1);
9848 /* allocate a new super->devlist entry */
9849 dv
= xmalloc(sizeof(*dv
));
9850 dv
->dev
= xmalloc(len
);
9853 /* count how many spares will be converted to members */
9854 for (i
= 0; i
< map
->num_members
; i
++) {
9855 dl
= serial_to_dl(inf
[i
].serial
, super
);
9857 /* hmm maybe it failed?, nothing we can do about
9862 if (count_memberships(dl
, super
) == 0)
9865 len
+= activate
* sizeof(struct imsm_disk
);
9868 case update_kill_array
: {
9869 if (update
->len
< (int)sizeof(struct imsm_update_kill_array
))
9873 case update_rename_array
: {
9874 if (update
->len
< (int)sizeof(struct imsm_update_rename_array
))
9878 case update_add_remove_disk
:
9879 /* no update->len needed */
9881 case update_prealloc_badblocks_mem
:
9882 super
->extra_space
+= sizeof(struct bbm_log
) -
9883 get_imsm_bbm_log_size(super
->bbm_log
);
9885 case update_rwh_policy
: {
9886 if (update
->len
< (int)sizeof(struct imsm_update_rwh_policy
))
9894 /* check if we need a larger metadata buffer */
9895 if (super
->next_buf
)
9896 buf_len
= super
->next_len
;
9898 buf_len
= super
->len
;
9900 if (__le32_to_cpu(mpb
->mpb_size
) + super
->extra_space
+ len
> buf_len
) {
9901 /* ok we need a larger buf than what is currently allocated
9902 * if this allocation fails process_update will notice that
9903 * ->next_len is set and ->next_buf is NULL
9905 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) +
9906 super
->extra_space
+ len
, sector_size
);
9907 if (super
->next_buf
)
9908 free(super
->next_buf
);
9910 super
->next_len
= buf_len
;
9911 if (posix_memalign(&super
->next_buf
, sector_size
, buf_len
) == 0)
9912 memset(super
->next_buf
, 0, buf_len
);
9914 super
->next_buf
= NULL
;
9919 /* must be called while manager is quiesced */
9920 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
9922 struct imsm_super
*mpb
= super
->anchor
;
9924 struct imsm_dev
*dev
;
9925 struct imsm_map
*map
;
9926 unsigned int i
, j
, num_members
;
9928 struct bbm_log
*log
= super
->bbm_log
;
9930 dprintf("deleting device[%d] from imsm_super\n", index
);
9932 /* shift all indexes down one */
9933 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
9934 if (iter
->index
> (int)index
)
9936 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
9937 if (iter
->index
> (int)index
)
9940 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
9941 dev
= get_imsm_dev(super
, i
);
9942 map
= get_imsm_map(dev
, MAP_0
);
9943 num_members
= map
->num_members
;
9944 for (j
= 0; j
< num_members
; j
++) {
9945 /* update ord entries being careful not to propagate
9946 * ord-flags to the first map
9948 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_X
);
9950 if (ord_to_idx(ord
) <= index
)
9953 map
= get_imsm_map(dev
, MAP_0
);
9954 set_imsm_ord_tbl_ent(map
, j
, ord_to_idx(ord
- 1));
9955 map
= get_imsm_map(dev
, MAP_1
);
9957 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
9961 for (i
= 0; i
< log
->entry_count
; i
++) {
9962 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[i
];
9964 if (entry
->disk_ordinal
<= index
)
9966 entry
->disk_ordinal
--;
9970 super
->updates_pending
++;
9972 struct dl
*dl
= *dlp
;
9974 *dlp
= (*dlp
)->next
;
9975 __free_imsm_disk(dl
);
9979 static void close_targets(int *targets
, int new_disks
)
9986 for (i
= 0; i
< new_disks
; i
++) {
9987 if (targets
[i
] >= 0) {
9994 static int imsm_get_allowed_degradation(int level
, int raid_disks
,
9995 struct intel_super
*super
,
9996 struct imsm_dev
*dev
)
10002 struct imsm_map
*map
;
10005 ret_val
= raid_disks
/2;
10006 /* check map if all disks pairs not failed
10009 map
= get_imsm_map(dev
, MAP_0
);
10010 for (i
= 0; i
< ret_val
; i
++) {
10011 int degradation
= 0;
10012 if (get_imsm_disk(super
, i
) == NULL
)
10014 if (get_imsm_disk(super
, i
+ 1) == NULL
)
10016 if (degradation
== 2)
10019 map
= get_imsm_map(dev
, MAP_1
);
10020 /* if there is no second map
10021 * result can be returned
10025 /* check degradation in second map
10027 for (i
= 0; i
< ret_val
; i
++) {
10028 int degradation
= 0;
10029 if (get_imsm_disk(super
, i
) == NULL
)
10031 if (get_imsm_disk(super
, i
+ 1) == NULL
)
10033 if (degradation
== 2)
10047 /*******************************************************************************
10048 * Function: open_backup_targets
10049 * Description: Function opens file descriptors for all devices given in
10052 * info : general array info
10053 * raid_disks : number of disks
10054 * raid_fds : table of device's file descriptors
10055 * super : intel super for raid10 degradation check
10056 * dev : intel device for raid10 degradation check
10060 ******************************************************************************/
10061 int open_backup_targets(struct mdinfo
*info
, int raid_disks
, int *raid_fds
,
10062 struct intel_super
*super
, struct imsm_dev
*dev
)
10068 for (i
= 0; i
< raid_disks
; i
++)
10071 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
10074 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
)) {
10075 dprintf("disk is faulty!!\n");
10079 if (sd
->disk
.raid_disk
>= raid_disks
|| sd
->disk
.raid_disk
< 0)
10082 dn
= map_dev(sd
->disk
.major
,
10083 sd
->disk
.minor
, 1);
10084 raid_fds
[sd
->disk
.raid_disk
] = dev_open(dn
, O_RDWR
);
10085 if (raid_fds
[sd
->disk
.raid_disk
] < 0) {
10086 pr_err("cannot open component\n");
10091 /* check if maximum array degradation level is not exceeded
10093 if ((raid_disks
- opened
) >
10094 imsm_get_allowed_degradation(info
->new_level
, raid_disks
,
10096 pr_err("Not enough disks can be opened.\n");
10097 close_targets(raid_fds
, raid_disks
);
10103 /*******************************************************************************
10104 * Function: validate_container_imsm
10105 * Description: This routine validates container after assemble,
10106 * eg. if devices in container are under the same controller.
10109 * info : linked list with info about devices used in array
10113 ******************************************************************************/
10114 int validate_container_imsm(struct mdinfo
*info
)
10116 if (check_env("IMSM_NO_PLATFORM"))
10119 struct sys_dev
*idev
;
10120 struct sys_dev
*hba
= NULL
;
10121 struct sys_dev
*intel_devices
= find_intel_devices();
10122 char *dev_path
= devt_to_devpath(makedev(info
->disk
.major
,
10123 info
->disk
.minor
));
10125 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
10126 if (dev_path
&& strstr(dev_path
, idev
->path
)) {
10135 pr_err("WARNING - Cannot detect HBA for device %s!\n",
10136 devid2kname(makedev(info
->disk
.major
, info
->disk
.minor
)));
10140 const struct imsm_orom
*orom
= get_orom_by_device_id(hba
->dev_id
);
10141 struct mdinfo
*dev
;
10143 for (dev
= info
->next
; dev
; dev
= dev
->next
) {
10144 dev_path
= devt_to_devpath(makedev(dev
->disk
.major
, dev
->disk
.minor
));
10146 struct sys_dev
*hba2
= NULL
;
10147 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
10148 if (dev_path
&& strstr(dev_path
, idev
->path
)) {
10156 const struct imsm_orom
*orom2
= hba2
== NULL
? NULL
:
10157 get_orom_by_device_id(hba2
->dev_id
);
10159 if (hba2
&& hba
->type
!= hba2
->type
) {
10160 pr_err("WARNING - HBAs of devices do not match %s != %s\n",
10161 get_sys_dev_type(hba
->type
), get_sys_dev_type(hba2
->type
));
10165 if (orom
!= orom2
) {
10166 pr_err("WARNING - IMSM container assembled with disks under different HBAs!\n"
10167 " This operation is not supported and can lead to data loss.\n");
10172 pr_err("WARNING - IMSM container assembled with disks under HBAs without IMSM platform support!\n"
10173 " This operation is not supported and can lead to data loss.\n");
10181 /*******************************************************************************
10182 * Function: imsm_record_badblock
10183 * Description: This routine stores new bad block record in BBM log
10186 * a : array containing a bad block
10187 * slot : disk number containing a bad block
10188 * sector : bad block sector
10189 * length : bad block sectors range
10193 ******************************************************************************/
10194 static int imsm_record_badblock(struct active_array
*a
, int slot
,
10195 unsigned long long sector
, int length
)
10197 struct intel_super
*super
= a
->container
->sb
;
10201 ord
= imsm_disk_slot_to_ord(a
, slot
);
10205 ret
= record_new_badblock(super
->bbm_log
, ord_to_idx(ord
), sector
,
10208 super
->updates_pending
++;
10212 /*******************************************************************************
10213 * Function: imsm_clear_badblock
10214 * Description: This routine clears bad block record from BBM log
10217 * a : array containing a bad block
10218 * slot : disk number containing a bad block
10219 * sector : bad block sector
10220 * length : bad block sectors range
10224 ******************************************************************************/
10225 static int imsm_clear_badblock(struct active_array
*a
, int slot
,
10226 unsigned long long sector
, int length
)
10228 struct intel_super
*super
= a
->container
->sb
;
10232 ord
= imsm_disk_slot_to_ord(a
, slot
);
10236 ret
= clear_badblock(super
->bbm_log
, ord_to_idx(ord
), sector
, length
);
10238 super
->updates_pending
++;
10242 /*******************************************************************************
10243 * Function: imsm_get_badblocks
10244 * Description: This routine get list of bad blocks for an array
10248 * slot : disk number
10250 * bb : structure containing bad blocks
10252 ******************************************************************************/
10253 static struct md_bb
*imsm_get_badblocks(struct active_array
*a
, int slot
)
10255 int inst
= a
->info
.container_member
;
10256 struct intel_super
*super
= a
->container
->sb
;
10257 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
10258 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
10261 ord
= imsm_disk_slot_to_ord(a
, slot
);
10265 get_volume_badblocks(super
->bbm_log
, ord_to_idx(ord
), pba_of_lba0(map
),
10266 blocks_per_member(map
), &super
->bb
);
10270 /*******************************************************************************
10271 * Function: examine_badblocks_imsm
10272 * Description: Prints list of bad blocks on a disk to the standard output
10275 * st : metadata handler
10276 * fd : open file descriptor for device
10277 * devname : device name
10281 ******************************************************************************/
10282 static int examine_badblocks_imsm(struct supertype
*st
, int fd
, char *devname
)
10284 struct intel_super
*super
= st
->sb
;
10285 struct bbm_log
*log
= super
->bbm_log
;
10286 struct dl
*d
= NULL
;
10289 for (d
= super
->disks
; d
; d
= d
->next
) {
10290 if (strcmp(d
->devname
, devname
) == 0)
10294 if ((d
== NULL
) || (d
->index
< 0)) { /* serial mismatch probably */
10295 pr_err("%s doesn't appear to be part of a raid array\n",
10302 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[0];
10304 for (i
= 0; i
< log
->entry_count
; i
++) {
10305 if (entry
[i
].disk_ordinal
== d
->index
) {
10306 unsigned long long sector
= __le48_to_cpu(
10307 &entry
[i
].defective_block_start
);
10308 int cnt
= entry
[i
].marked_count
+ 1;
10311 printf("Bad-blocks on %s:\n", devname
);
10315 printf("%20llu for %d sectors\n", sector
, cnt
);
10321 printf("No bad-blocks list configured on %s\n", devname
);
10325 /*******************************************************************************
10326 * Function: init_migr_record_imsm
10327 * Description: Function inits imsm migration record
10329 * super : imsm internal array info
10330 * dev : device under migration
10331 * info : general array info to find the smallest device
10334 ******************************************************************************/
10335 void init_migr_record_imsm(struct supertype
*st
, struct imsm_dev
*dev
,
10336 struct mdinfo
*info
)
10338 struct intel_super
*super
= st
->sb
;
10339 struct migr_record
*migr_rec
= super
->migr_rec
;
10340 int new_data_disks
;
10341 unsigned long long dsize
, dev_sectors
;
10342 long long unsigned min_dev_sectors
= -1LLU;
10346 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
10347 struct imsm_map
*map_src
= get_imsm_map(dev
, MAP_1
);
10348 unsigned long long num_migr_units
;
10349 unsigned long long array_blocks
;
10351 memset(migr_rec
, 0, sizeof(struct migr_record
));
10352 migr_rec
->family_num
= __cpu_to_le32(super
->anchor
->family_num
);
10354 /* only ascending reshape supported now */
10355 migr_rec
->ascending_migr
= __cpu_to_le32(1);
10357 migr_rec
->dest_depth_per_unit
= GEN_MIGR_AREA_SIZE
/
10358 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
10359 migr_rec
->dest_depth_per_unit
*=
10360 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
10361 new_data_disks
= imsm_num_data_members(dev
, MAP_0
);
10362 migr_rec
->blocks_per_unit
=
10363 __cpu_to_le32(migr_rec
->dest_depth_per_unit
* new_data_disks
);
10364 migr_rec
->dest_depth_per_unit
=
10365 __cpu_to_le32(migr_rec
->dest_depth_per_unit
);
10366 array_blocks
= info
->component_size
* new_data_disks
;
10368 array_blocks
/ __le32_to_cpu(migr_rec
->blocks_per_unit
);
10370 if (array_blocks
% __le32_to_cpu(migr_rec
->blocks_per_unit
))
10372 migr_rec
->num_migr_units
= __cpu_to_le32(num_migr_units
);
10374 migr_rec
->post_migr_vol_cap
= dev
->size_low
;
10375 migr_rec
->post_migr_vol_cap_hi
= dev
->size_high
;
10377 /* Find the smallest dev */
10378 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
10379 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
10380 fd
= dev_open(nm
, O_RDONLY
);
10383 get_dev_size(fd
, NULL
, &dsize
);
10384 dev_sectors
= dsize
/ 512;
10385 if (dev_sectors
< min_dev_sectors
)
10386 min_dev_sectors
= dev_sectors
;
10389 migr_rec
->ckpt_area_pba
= __cpu_to_le32(min_dev_sectors
-
10390 RAID_DISK_RESERVED_BLOCKS_IMSM_HI
);
10392 write_imsm_migr_rec(st
);
10397 /*******************************************************************************
10398 * Function: save_backup_imsm
10399 * Description: Function saves critical data stripes to Migration Copy Area
10400 * and updates the current migration unit status.
10401 * Use restore_stripes() to form a destination stripe,
10402 * and to write it to the Copy Area.
10404 * st : supertype information
10405 * dev : imsm device that backup is saved for
10406 * info : general array info
10407 * buf : input buffer
10408 * length : length of data to backup (blocks_per_unit)
10412 ******************************************************************************/
10413 int save_backup_imsm(struct supertype
*st
,
10414 struct imsm_dev
*dev
,
10415 struct mdinfo
*info
,
10420 struct intel_super
*super
= st
->sb
;
10421 unsigned long long *target_offsets
;
10424 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
10425 int new_disks
= map_dest
->num_members
;
10426 int dest_layout
= 0;
10428 unsigned long long start
;
10429 int data_disks
= imsm_num_data_members(dev
, MAP_0
);
10431 targets
= xmalloc(new_disks
* sizeof(int));
10433 for (i
= 0; i
< new_disks
; i
++)
10436 target_offsets
= xcalloc(new_disks
, sizeof(unsigned long long));
10438 start
= info
->reshape_progress
* 512;
10439 for (i
= 0; i
< new_disks
; i
++) {
10440 target_offsets
[i
] = (unsigned long long)
10441 __le32_to_cpu(super
->migr_rec
->ckpt_area_pba
) * 512;
10442 /* move back copy area adderss, it will be moved forward
10443 * in restore_stripes() using start input variable
10445 target_offsets
[i
] -= start
/data_disks
;
10448 if (open_backup_targets(info
, new_disks
, targets
,
10452 dest_layout
= imsm_level_to_layout(map_dest
->raid_level
);
10453 dest_chunk
= __le16_to_cpu(map_dest
->blocks_per_strip
) * 512;
10455 if (restore_stripes(targets
, /* list of dest devices */
10456 target_offsets
, /* migration record offsets */
10459 map_dest
->raid_level
,
10461 -1, /* source backup file descriptor */
10462 0, /* input buf offset
10463 * always 0 buf is already offseted */
10467 pr_err("Error restoring stripes\n");
10475 close_targets(targets
, new_disks
);
10478 free(target_offsets
);
10483 /*******************************************************************************
10484 * Function: save_checkpoint_imsm
10485 * Description: Function called for current unit status update
10486 * in the migration record. It writes it to disk.
10488 * super : imsm internal array info
10489 * info : general array info
10493 * 2: failure, means no valid migration record
10494 * / no general migration in progress /
10495 ******************************************************************************/
10496 int save_checkpoint_imsm(struct supertype
*st
, struct mdinfo
*info
, int state
)
10498 struct intel_super
*super
= st
->sb
;
10499 unsigned long long blocks_per_unit
;
10500 unsigned long long curr_migr_unit
;
10502 if (load_imsm_migr_rec(super
, info
) != 0) {
10503 dprintf("imsm: ERROR: Cannot read migration record for checkpoint save.\n");
10507 blocks_per_unit
= __le32_to_cpu(super
->migr_rec
->blocks_per_unit
);
10508 if (blocks_per_unit
== 0) {
10509 dprintf("imsm: no migration in progress.\n");
10512 curr_migr_unit
= info
->reshape_progress
/ blocks_per_unit
;
10513 /* check if array is alligned to copy area
10514 * if it is not alligned, add one to current migration unit value
10515 * this can happend on array reshape finish only
10517 if (info
->reshape_progress
% blocks_per_unit
)
10520 super
->migr_rec
->curr_migr_unit
=
10521 __cpu_to_le32(curr_migr_unit
);
10522 super
->migr_rec
->rec_status
= __cpu_to_le32(state
);
10523 super
->migr_rec
->dest_1st_member_lba
=
10524 __cpu_to_le32(curr_migr_unit
*
10525 __le32_to_cpu(super
->migr_rec
->dest_depth_per_unit
));
10526 if (write_imsm_migr_rec(st
) < 0) {
10527 dprintf("imsm: Cannot write migration record outside backup area\n");
10534 /*******************************************************************************
10535 * Function: recover_backup_imsm
10536 * Description: Function recovers critical data from the Migration Copy Area
10537 * while assembling an array.
10539 * super : imsm internal array info
10540 * info : general array info
10542 * 0 : success (or there is no data to recover)
10544 ******************************************************************************/
10545 int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
)
10547 struct intel_super
*super
= st
->sb
;
10548 struct migr_record
*migr_rec
= super
->migr_rec
;
10549 struct imsm_map
*map_dest
;
10550 struct intel_dev
*id
= NULL
;
10551 unsigned long long read_offset
;
10552 unsigned long long write_offset
;
10554 int *targets
= NULL
;
10555 int new_disks
, i
, err
;
10558 unsigned int sector_size
= super
->sector_size
;
10559 unsigned long curr_migr_unit
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
10560 unsigned long num_migr_units
= __le32_to_cpu(migr_rec
->num_migr_units
);
10562 int skipped_disks
= 0;
10564 err
= sysfs_get_str(info
, NULL
, "array_state", (char *)buffer
, 20);
10568 /* recover data only during assemblation */
10569 if (strncmp(buffer
, "inactive", 8) != 0)
10571 /* no data to recover */
10572 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
10574 if (curr_migr_unit
>= num_migr_units
)
10577 /* find device during reshape */
10578 for (id
= super
->devlist
; id
; id
= id
->next
)
10579 if (is_gen_migration(id
->dev
))
10584 map_dest
= get_imsm_map(id
->dev
, MAP_0
);
10585 new_disks
= map_dest
->num_members
;
10587 read_offset
= (unsigned long long)
10588 __le32_to_cpu(migr_rec
->ckpt_area_pba
) * 512;
10590 write_offset
= ((unsigned long long)
10591 __le32_to_cpu(migr_rec
->dest_1st_member_lba
) +
10592 pba_of_lba0(map_dest
)) * 512;
10594 unit_len
= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
10595 if (posix_memalign((void **)&buf
, sector_size
, unit_len
) != 0)
10597 targets
= xcalloc(new_disks
, sizeof(int));
10599 if (open_backup_targets(info
, new_disks
, targets
, super
, id
->dev
)) {
10600 pr_err("Cannot open some devices belonging to array.\n");
10604 for (i
= 0; i
< new_disks
; i
++) {
10605 if (targets
[i
] < 0) {
10609 if (lseek64(targets
[i
], read_offset
, SEEK_SET
) < 0) {
10610 pr_err("Cannot seek to block: %s\n",
10615 if ((unsigned)read(targets
[i
], buf
, unit_len
) != unit_len
) {
10616 pr_err("Cannot read copy area block: %s\n",
10621 if (lseek64(targets
[i
], write_offset
, SEEK_SET
) < 0) {
10622 pr_err("Cannot seek to block: %s\n",
10627 if ((unsigned)write(targets
[i
], buf
, unit_len
) != unit_len
) {
10628 pr_err("Cannot restore block: %s\n",
10635 if (skipped_disks
> imsm_get_allowed_degradation(info
->new_level
,
10639 pr_err("Cannot restore data from backup. Too many failed disks\n");
10643 if (save_checkpoint_imsm(st
, info
, UNIT_SRC_NORMAL
)) {
10644 /* ignore error == 2, this can mean end of reshape here
10646 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL) during restart\n");
10652 for (i
= 0; i
< new_disks
; i
++)
10661 static char disk_by_path
[] = "/dev/disk/by-path/";
10663 static const char *imsm_get_disk_controller_domain(const char *path
)
10665 char disk_path
[PATH_MAX
];
10669 strcpy(disk_path
, disk_by_path
);
10670 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
10671 if (stat(disk_path
, &st
) == 0) {
10672 struct sys_dev
* hba
;
10675 path
= devt_to_devpath(st
.st_rdev
);
10678 hba
= find_disk_attached_hba(-1, path
);
10679 if (hba
&& hba
->type
== SYS_DEV_SAS
)
10681 else if (hba
&& hba
->type
== SYS_DEV_SATA
)
10685 dprintf("path: %s hba: %s attached: %s\n",
10686 path
, (hba
) ? hba
->path
: "NULL", drv
);
10692 static char *imsm_find_array_devnm_by_subdev(int subdev
, char *container
)
10694 static char devnm
[32];
10695 char subdev_name
[20];
10696 struct mdstat_ent
*mdstat
;
10698 sprintf(subdev_name
, "%d", subdev
);
10699 mdstat
= mdstat_by_subdev(subdev_name
, container
);
10703 strcpy(devnm
, mdstat
->devnm
);
10704 free_mdstat(mdstat
);
10708 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
10709 struct geo_params
*geo
,
10710 int *old_raid_disks
,
10713 /* currently we only support increasing the number of devices
10714 * for a container. This increases the number of device for each
10715 * member array. They must all be RAID0 or RAID5.
10718 struct mdinfo
*info
, *member
;
10719 int devices_that_can_grow
= 0;
10721 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): st->devnm = (%s)\n", st
->devnm
);
10723 if (geo
->size
> 0 ||
10724 geo
->level
!= UnSet
||
10725 geo
->layout
!= UnSet
||
10726 geo
->chunksize
!= 0 ||
10727 geo
->raid_disks
== UnSet
) {
10728 dprintf("imsm: Container operation is allowed for raid disks number change only.\n");
10732 if (direction
== ROLLBACK_METADATA_CHANGES
) {
10733 dprintf("imsm: Metadata changes rollback is not supported for container operation.\n");
10737 info
= container_content_imsm(st
, NULL
);
10738 for (member
= info
; member
; member
= member
->next
) {
10741 dprintf("imsm: checking device_num: %i\n",
10742 member
->container_member
);
10744 if (geo
->raid_disks
<= member
->array
.raid_disks
) {
10745 /* we work on container for Online Capacity Expansion
10746 * only so raid_disks has to grow
10748 dprintf("imsm: for container operation raid disks increase is required\n");
10752 if (info
->array
.level
!= 0 && info
->array
.level
!= 5) {
10753 /* we cannot use this container with other raid level
10755 dprintf("imsm: for container operation wrong raid level (%i) detected\n",
10756 info
->array
.level
);
10759 /* check for platform support
10760 * for this raid level configuration
10762 struct intel_super
*super
= st
->sb
;
10763 if (!is_raid_level_supported(super
->orom
,
10764 member
->array
.level
,
10765 geo
->raid_disks
)) {
10766 dprintf("platform does not support raid%d with %d disk%s\n",
10769 geo
->raid_disks
> 1 ? "s" : "");
10772 /* check if component size is aligned to chunk size
10774 if (info
->component_size
%
10775 (info
->array
.chunk_size
/512)) {
10776 dprintf("Component size is not aligned to chunk size\n");
10781 if (*old_raid_disks
&&
10782 info
->array
.raid_disks
!= *old_raid_disks
)
10784 *old_raid_disks
= info
->array
.raid_disks
;
10786 /* All raid5 and raid0 volumes in container
10787 * have to be ready for Online Capacity Expansion
10788 * so they need to be assembled. We have already
10789 * checked that no recovery etc is happening.
10791 result
= imsm_find_array_devnm_by_subdev(member
->container_member
,
10792 st
->container_devnm
);
10793 if (result
== NULL
) {
10794 dprintf("imsm: cannot find array\n");
10797 devices_that_can_grow
++;
10800 if (!member
&& devices_that_can_grow
)
10804 dprintf("Container operation allowed\n");
10806 dprintf("Error: %i\n", ret_val
);
10811 /* Function: get_spares_for_grow
10812 * Description: Allocates memory and creates list of spare devices
10813 * avaliable in container. Checks if spare drive size is acceptable.
10814 * Parameters: Pointer to the supertype structure
10815 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
10818 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
10820 unsigned long long min_size
= min_acceptable_spare_size_imsm(st
);
10821 return container_choose_spares(st
, min_size
, NULL
, NULL
, NULL
, 0);
10824 /******************************************************************************
10825 * function: imsm_create_metadata_update_for_reshape
10826 * Function creates update for whole IMSM container.
10828 ******************************************************************************/
10829 static int imsm_create_metadata_update_for_reshape(
10830 struct supertype
*st
,
10831 struct geo_params
*geo
,
10832 int old_raid_disks
,
10833 struct imsm_update_reshape
**updatep
)
10835 struct intel_super
*super
= st
->sb
;
10836 struct imsm_super
*mpb
= super
->anchor
;
10837 int update_memory_size
;
10838 struct imsm_update_reshape
*u
;
10839 struct mdinfo
*spares
;
10842 struct mdinfo
*dev
;
10844 dprintf("(enter) raid_disks = %i\n", geo
->raid_disks
);
10846 delta_disks
= geo
->raid_disks
- old_raid_disks
;
10848 /* size of all update data without anchor */
10849 update_memory_size
= sizeof(struct imsm_update_reshape
);
10851 /* now add space for spare disks that we need to add. */
10852 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
10854 u
= xcalloc(1, update_memory_size
);
10855 u
->type
= update_reshape_container_disks
;
10856 u
->old_raid_disks
= old_raid_disks
;
10857 u
->new_raid_disks
= geo
->raid_disks
;
10859 /* now get spare disks list
10861 spares
= get_spares_for_grow(st
);
10864 || delta_disks
> spares
->array
.spare_disks
) {
10865 pr_err("imsm: ERROR: Cannot get spare devices for %s.\n", geo
->dev_name
);
10870 /* we have got spares
10871 * update disk list in imsm_disk list table in anchor
10873 dprintf("imsm: %i spares are available.\n\n",
10874 spares
->array
.spare_disks
);
10876 dev
= spares
->devs
;
10877 for (i
= 0; i
< delta_disks
; i
++) {
10882 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
10884 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
10885 dl
->index
= mpb
->num_disks
;
10893 sysfs_free(spares
);
10895 dprintf("imsm: reshape update preparation :");
10896 if (i
== delta_disks
) {
10897 dprintf_cont(" OK\n");
10899 return update_memory_size
;
10902 dprintf_cont(" Error\n");
10907 /******************************************************************************
10908 * function: imsm_create_metadata_update_for_size_change()
10909 * Creates update for IMSM array for array size change.
10911 ******************************************************************************/
10912 static int imsm_create_metadata_update_for_size_change(
10913 struct supertype
*st
,
10914 struct geo_params
*geo
,
10915 struct imsm_update_size_change
**updatep
)
10917 struct intel_super
*super
= st
->sb
;
10918 int update_memory_size
;
10919 struct imsm_update_size_change
*u
;
10921 dprintf("(enter) New size = %llu\n", geo
->size
);
10923 /* size of all update data without anchor */
10924 update_memory_size
= sizeof(struct imsm_update_size_change
);
10926 u
= xcalloc(1, update_memory_size
);
10927 u
->type
= update_size_change
;
10928 u
->subdev
= super
->current_vol
;
10929 u
->new_size
= geo
->size
;
10931 dprintf("imsm: reshape update preparation : OK\n");
10934 return update_memory_size
;
10937 /******************************************************************************
10938 * function: imsm_create_metadata_update_for_migration()
10939 * Creates update for IMSM array.
10941 ******************************************************************************/
10942 static int imsm_create_metadata_update_for_migration(
10943 struct supertype
*st
,
10944 struct geo_params
*geo
,
10945 struct imsm_update_reshape_migration
**updatep
)
10947 struct intel_super
*super
= st
->sb
;
10948 int update_memory_size
;
10949 struct imsm_update_reshape_migration
*u
;
10950 struct imsm_dev
*dev
;
10951 int previous_level
= -1;
10953 dprintf("(enter) New Level = %i\n", geo
->level
);
10955 /* size of all update data without anchor */
10956 update_memory_size
= sizeof(struct imsm_update_reshape_migration
);
10958 u
= xcalloc(1, update_memory_size
);
10959 u
->type
= update_reshape_migration
;
10960 u
->subdev
= super
->current_vol
;
10961 u
->new_level
= geo
->level
;
10962 u
->new_layout
= geo
->layout
;
10963 u
->new_raid_disks
= u
->old_raid_disks
= geo
->raid_disks
;
10964 u
->new_disks
[0] = -1;
10965 u
->new_chunksize
= -1;
10967 dev
= get_imsm_dev(super
, u
->subdev
);
10969 struct imsm_map
*map
;
10971 map
= get_imsm_map(dev
, MAP_0
);
10973 int current_chunk_size
=
10974 __le16_to_cpu(map
->blocks_per_strip
) / 2;
10976 if (geo
->chunksize
!= current_chunk_size
) {
10977 u
->new_chunksize
= geo
->chunksize
/ 1024;
10978 dprintf("imsm: chunk size change from %i to %i\n",
10979 current_chunk_size
, u
->new_chunksize
);
10981 previous_level
= map
->raid_level
;
10984 if (geo
->level
== 5 && previous_level
== 0) {
10985 struct mdinfo
*spares
= NULL
;
10987 u
->new_raid_disks
++;
10988 spares
= get_spares_for_grow(st
);
10989 if (spares
== NULL
|| spares
->array
.spare_disks
< 1) {
10991 sysfs_free(spares
);
10992 update_memory_size
= 0;
10993 pr_err("cannot get spare device for requested migration\n");
10996 sysfs_free(spares
);
10998 dprintf("imsm: reshape update preparation : OK\n");
11001 return update_memory_size
;
11004 static void imsm_update_metadata_locally(struct supertype
*st
,
11005 void *buf
, int len
)
11007 struct metadata_update mu
;
11012 mu
.space_list
= NULL
;
11014 if (imsm_prepare_update(st
, &mu
))
11015 imsm_process_update(st
, &mu
);
11017 while (mu
.space_list
) {
11018 void **space
= mu
.space_list
;
11019 mu
.space_list
= *space
;
11024 /***************************************************************************
11025 * Function: imsm_analyze_change
11026 * Description: Function analyze change for single volume
11027 * and validate if transition is supported
11028 * Parameters: Geometry parameters, supertype structure,
11029 * metadata change direction (apply/rollback)
11030 * Returns: Operation type code on success, -1 if fail
11031 ****************************************************************************/
11032 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
11033 struct geo_params
*geo
,
11036 struct mdinfo info
;
11038 int check_devs
= 0;
11040 /* number of added/removed disks in operation result */
11041 int devNumChange
= 0;
11042 /* imsm compatible layout value for array geometry verification */
11043 int imsm_layout
= -1;
11045 struct imsm_dev
*dev
;
11046 struct intel_super
*super
;
11047 unsigned long long current_size
;
11048 unsigned long long free_size
;
11049 unsigned long long max_size
;
11052 getinfo_super_imsm_volume(st
, &info
, NULL
);
11053 if (geo
->level
!= info
.array
.level
&& geo
->level
>= 0 &&
11054 geo
->level
!= UnSet
) {
11055 switch (info
.array
.level
) {
11057 if (geo
->level
== 5) {
11058 change
= CH_MIGRATION
;
11059 if (geo
->layout
!= ALGORITHM_LEFT_ASYMMETRIC
) {
11060 pr_err("Error. Requested Layout not supported (left-asymmetric layout is supported only)!\n");
11062 goto analyse_change_exit
;
11064 imsm_layout
= geo
->layout
;
11066 devNumChange
= 1; /* parity disk added */
11067 } else if (geo
->level
== 10) {
11068 change
= CH_TAKEOVER
;
11070 devNumChange
= 2; /* two mirrors added */
11071 imsm_layout
= 0x102; /* imsm supported layout */
11076 if (geo
->level
== 0) {
11077 change
= CH_TAKEOVER
;
11079 devNumChange
= -(geo
->raid_disks
/2);
11080 imsm_layout
= 0; /* imsm raid0 layout */
11084 if (change
== -1) {
11085 pr_err("Error. Level Migration from %d to %d not supported!\n",
11086 info
.array
.level
, geo
->level
);
11087 goto analyse_change_exit
;
11090 geo
->level
= info
.array
.level
;
11092 if (geo
->layout
!= info
.array
.layout
&&
11093 (geo
->layout
!= UnSet
&& geo
->layout
!= -1)) {
11094 change
= CH_MIGRATION
;
11095 if (info
.array
.layout
== 0 && info
.array
.level
== 5 &&
11096 geo
->layout
== 5) {
11097 /* reshape 5 -> 4 */
11098 } else if (info
.array
.layout
== 5 && info
.array
.level
== 5 &&
11099 geo
->layout
== 0) {
11100 /* reshape 4 -> 5 */
11104 pr_err("Error. Layout Migration from %d to %d not supported!\n",
11105 info
.array
.layout
, geo
->layout
);
11107 goto analyse_change_exit
;
11110 geo
->layout
= info
.array
.layout
;
11111 if (imsm_layout
== -1)
11112 imsm_layout
= info
.array
.layout
;
11115 if (geo
->chunksize
> 0 && geo
->chunksize
!= UnSet
&&
11116 geo
->chunksize
!= info
.array
.chunk_size
) {
11117 if (info
.array
.level
== 10) {
11118 pr_err("Error. Chunk size change for RAID 10 is not supported.\n");
11120 goto analyse_change_exit
;
11121 } else if (info
.component_size
% (geo
->chunksize
/512)) {
11122 pr_err("New chunk size (%dK) does not evenly divide device size (%lluk). Aborting...\n",
11123 geo
->chunksize
/1024, info
.component_size
/2);
11125 goto analyse_change_exit
;
11127 change
= CH_MIGRATION
;
11129 geo
->chunksize
= info
.array
.chunk_size
;
11132 chunk
= geo
->chunksize
/ 1024;
11135 dev
= get_imsm_dev(super
, super
->current_vol
);
11136 data_disks
= imsm_num_data_members(dev
, MAP_0
);
11137 /* compute current size per disk member
11139 current_size
= info
.custom_array_size
/ data_disks
;
11141 if (geo
->size
> 0 && geo
->size
!= MAX_SIZE
) {
11142 /* align component size
11144 geo
->size
= imsm_component_size_aligment_check(
11145 get_imsm_raid_level(dev
->vol
.map
),
11146 chunk
* 1024, super
->sector_size
,
11148 if (geo
->size
== 0) {
11149 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is 0).\n",
11151 goto analyse_change_exit
;
11155 if (current_size
!= geo
->size
&& geo
->size
> 0) {
11156 if (change
!= -1) {
11157 pr_err("Error. Size change should be the only one at a time.\n");
11159 goto analyse_change_exit
;
11161 if ((super
->current_vol
+ 1) != super
->anchor
->num_raid_devs
) {
11162 pr_err("Error. The last volume in container can be expanded only (%i/%s).\n",
11163 super
->current_vol
, st
->devnm
);
11164 goto analyse_change_exit
;
11166 /* check the maximum available size
11168 rv
= imsm_get_free_size(st
, dev
->vol
.map
->num_members
,
11169 0, chunk
, &free_size
);
11171 /* Cannot find maximum available space
11175 max_size
= free_size
+ current_size
;
11176 /* align component size
11178 max_size
= imsm_component_size_aligment_check(
11179 get_imsm_raid_level(dev
->vol
.map
),
11180 chunk
* 1024, super
->sector_size
,
11183 if (geo
->size
== MAX_SIZE
) {
11184 /* requested size change to the maximum available size
11186 if (max_size
== 0) {
11187 pr_err("Error. Cannot find maximum available space.\n");
11189 goto analyse_change_exit
;
11191 geo
->size
= max_size
;
11194 if (direction
== ROLLBACK_METADATA_CHANGES
) {
11195 /* accept size for rollback only
11198 /* round size due to metadata compatibility
11200 geo
->size
= (geo
->size
>> SECT_PER_MB_SHIFT
)
11201 << SECT_PER_MB_SHIFT
;
11202 dprintf("Prepare update for size change to %llu\n",
11204 if (current_size
>= geo
->size
) {
11205 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is %llu).\n",
11206 current_size
, geo
->size
);
11207 goto analyse_change_exit
;
11209 if (max_size
&& geo
->size
> max_size
) {
11210 pr_err("Error. Requested size is larger than maximum available size (maximum available size is %llu, requested size /rounded/ is %llu).\n",
11211 max_size
, geo
->size
);
11212 goto analyse_change_exit
;
11215 geo
->size
*= data_disks
;
11216 geo
->raid_disks
= dev
->vol
.map
->num_members
;
11217 change
= CH_ARRAY_SIZE
;
11219 if (!validate_geometry_imsm(st
,
11222 geo
->raid_disks
+ devNumChange
,
11224 geo
->size
, INVALID_SECTORS
,
11225 0, 0, info
.consistency_policy
, 1))
11229 struct intel_super
*super
= st
->sb
;
11230 struct imsm_super
*mpb
= super
->anchor
;
11232 if (mpb
->num_raid_devs
> 1) {
11233 pr_err("Error. Cannot perform operation on %s- for this operation it MUST be single array in container\n",
11239 analyse_change_exit
:
11240 if (direction
== ROLLBACK_METADATA_CHANGES
&&
11241 (change
== CH_MIGRATION
|| change
== CH_TAKEOVER
)) {
11242 dprintf("imsm: Metadata changes rollback is not supported for migration and takeover operations.\n");
11248 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
11250 struct intel_super
*super
= st
->sb
;
11251 struct imsm_update_takeover
*u
;
11253 u
= xmalloc(sizeof(struct imsm_update_takeover
));
11255 u
->type
= update_takeover
;
11256 u
->subarray
= super
->current_vol
;
11258 /* 10->0 transition */
11259 if (geo
->level
== 0)
11260 u
->direction
= R10_TO_R0
;
11262 /* 0->10 transition */
11263 if (geo
->level
== 10)
11264 u
->direction
= R0_TO_R10
;
11266 /* update metadata locally */
11267 imsm_update_metadata_locally(st
, u
,
11268 sizeof(struct imsm_update_takeover
));
11269 /* and possibly remotely */
11270 if (st
->update_tail
)
11271 append_metadata_update(st
, u
,
11272 sizeof(struct imsm_update_takeover
));
11279 static int imsm_reshape_super(struct supertype
*st
, unsigned long long size
,
11281 int layout
, int chunksize
, int raid_disks
,
11282 int delta_disks
, char *backup
, char *dev
,
11283 int direction
, int verbose
)
11286 struct geo_params geo
;
11288 dprintf("(enter)\n");
11290 memset(&geo
, 0, sizeof(struct geo_params
));
11292 geo
.dev_name
= dev
;
11293 strcpy(geo
.devnm
, st
->devnm
);
11296 geo
.layout
= layout
;
11297 geo
.chunksize
= chunksize
;
11298 geo
.raid_disks
= raid_disks
;
11299 if (delta_disks
!= UnSet
)
11300 geo
.raid_disks
+= delta_disks
;
11302 dprintf("for level : %i\n", geo
.level
);
11303 dprintf("for raid_disks : %i\n", geo
.raid_disks
);
11305 if (experimental() == 0)
11308 if (strcmp(st
->container_devnm
, st
->devnm
) == 0) {
11309 /* On container level we can only increase number of devices. */
11310 dprintf("imsm: info: Container operation\n");
11311 int old_raid_disks
= 0;
11313 if (imsm_reshape_is_allowed_on_container(
11314 st
, &geo
, &old_raid_disks
, direction
)) {
11315 struct imsm_update_reshape
*u
= NULL
;
11318 len
= imsm_create_metadata_update_for_reshape(
11319 st
, &geo
, old_raid_disks
, &u
);
11322 dprintf("imsm: Cannot prepare update\n");
11323 goto exit_imsm_reshape_super
;
11327 /* update metadata locally */
11328 imsm_update_metadata_locally(st
, u
, len
);
11329 /* and possibly remotely */
11330 if (st
->update_tail
)
11331 append_metadata_update(st
, u
, len
);
11336 pr_err("(imsm) Operation is not allowed on this container\n");
11339 /* On volume level we support following operations
11340 * - takeover: raid10 -> raid0; raid0 -> raid10
11341 * - chunk size migration
11342 * - migration: raid5 -> raid0; raid0 -> raid5
11344 struct intel_super
*super
= st
->sb
;
11345 struct intel_dev
*dev
= super
->devlist
;
11347 dprintf("imsm: info: Volume operation\n");
11348 /* find requested device */
11351 imsm_find_array_devnm_by_subdev(
11352 dev
->index
, st
->container_devnm
);
11353 if (devnm
&& strcmp(devnm
, geo
.devnm
) == 0)
11358 pr_err("Cannot find %s (%s) subarray\n",
11359 geo
.dev_name
, geo
.devnm
);
11360 goto exit_imsm_reshape_super
;
11362 super
->current_vol
= dev
->index
;
11363 change
= imsm_analyze_change(st
, &geo
, direction
);
11366 ret_val
= imsm_takeover(st
, &geo
);
11368 case CH_MIGRATION
: {
11369 struct imsm_update_reshape_migration
*u
= NULL
;
11371 imsm_create_metadata_update_for_migration(
11374 dprintf("imsm: Cannot prepare update\n");
11378 /* update metadata locally */
11379 imsm_update_metadata_locally(st
, u
, len
);
11380 /* and possibly remotely */
11381 if (st
->update_tail
)
11382 append_metadata_update(st
, u
, len
);
11387 case CH_ARRAY_SIZE
: {
11388 struct imsm_update_size_change
*u
= NULL
;
11390 imsm_create_metadata_update_for_size_change(
11393 dprintf("imsm: Cannot prepare update\n");
11397 /* update metadata locally */
11398 imsm_update_metadata_locally(st
, u
, len
);
11399 /* and possibly remotely */
11400 if (st
->update_tail
)
11401 append_metadata_update(st
, u
, len
);
11411 exit_imsm_reshape_super
:
11412 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
11416 #define COMPLETED_OK 0
11417 #define COMPLETED_NONE 1
11418 #define COMPLETED_DELAYED 2
11420 static int read_completed(int fd
, unsigned long long *val
)
11425 ret
= sysfs_fd_get_str(fd
, buf
, 50);
11429 ret
= COMPLETED_OK
;
11430 if (strncmp(buf
, "none", 4) == 0) {
11431 ret
= COMPLETED_NONE
;
11432 } else if (strncmp(buf
, "delayed", 7) == 0) {
11433 ret
= COMPLETED_DELAYED
;
11436 *val
= strtoull(buf
, &ep
, 0);
11437 if (ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))
11443 /*******************************************************************************
11444 * Function: wait_for_reshape_imsm
11445 * Description: Function writes new sync_max value and waits until
11446 * reshape process reach new position
11448 * sra : general array info
11449 * ndata : number of disks in new array's layout
11452 * 1 : there is no reshape in progress,
11454 ******************************************************************************/
11455 int wait_for_reshape_imsm(struct mdinfo
*sra
, int ndata
)
11457 int fd
= sysfs_get_fd(sra
, NULL
, "sync_completed");
11459 unsigned long long completed
;
11460 /* to_complete : new sync_max position */
11461 unsigned long long to_complete
= sra
->reshape_progress
;
11462 unsigned long long position_to_set
= to_complete
/ ndata
;
11465 dprintf("cannot open reshape_position\n");
11470 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
11472 dprintf("cannot read reshape_position (no reshape in progres)\n");
11481 if (completed
> position_to_set
) {
11482 dprintf("wrong next position to set %llu (%llu)\n",
11483 to_complete
, position_to_set
);
11487 dprintf("Position set: %llu\n", position_to_set
);
11488 if (sysfs_set_num(sra
, NULL
, "sync_max",
11489 position_to_set
) != 0) {
11490 dprintf("cannot set reshape position to %llu\n",
11499 int timeout
= 3000;
11501 sysfs_wait(fd
, &timeout
);
11502 if (sysfs_get_str(sra
, NULL
, "sync_action",
11504 strncmp(action
, "reshape", 7) != 0) {
11505 if (strncmp(action
, "idle", 4) == 0)
11511 rc
= read_completed(fd
, &completed
);
11513 dprintf("cannot read reshape_position (in loop)\n");
11516 } else if (rc
== COMPLETED_NONE
)
11518 } while (completed
< position_to_set
);
11524 /*******************************************************************************
11525 * Function: check_degradation_change
11526 * Description: Check that array hasn't become failed.
11528 * info : for sysfs access
11529 * sources : source disks descriptors
11530 * degraded: previous degradation level
11532 * degradation level
11533 ******************************************************************************/
11534 int check_degradation_change(struct mdinfo
*info
,
11538 unsigned long long new_degraded
;
11541 rv
= sysfs_get_ll(info
, NULL
, "degraded", &new_degraded
);
11542 if (rv
== -1 || (new_degraded
!= (unsigned long long)degraded
)) {
11543 /* check each device to ensure it is still working */
11546 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
11547 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
11549 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
11552 if (sysfs_get_str(info
,
11553 sd
, "state", sbuf
, sizeof(sbuf
)) < 0 ||
11554 strstr(sbuf
, "faulty") ||
11555 strstr(sbuf
, "in_sync") == NULL
) {
11556 /* this device is dead */
11557 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
11558 if (sd
->disk
.raid_disk
>= 0 &&
11559 sources
[sd
->disk
.raid_disk
] >= 0) {
11561 sd
->disk
.raid_disk
]);
11562 sources
[sd
->disk
.raid_disk
] =
11571 return new_degraded
;
11574 /*******************************************************************************
11575 * Function: imsm_manage_reshape
11576 * Description: Function finds array under reshape and it manages reshape
11577 * process. It creates stripes backups (if required) and sets
11580 * afd : Backup handle (nattive) - not used
11581 * sra : general array info
11582 * reshape : reshape parameters - not used
11583 * st : supertype structure
11584 * blocks : size of critical section [blocks]
11585 * fds : table of source device descriptor
11586 * offsets : start of array (offest per devices)
11588 * destfd : table of destination device descriptor
11589 * destoffsets : table of destination offsets (per device)
11591 * 1 : success, reshape is done
11593 ******************************************************************************/
11594 static int imsm_manage_reshape(
11595 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
11596 struct supertype
*st
, unsigned long backup_blocks
,
11597 int *fds
, unsigned long long *offsets
,
11598 int dests
, int *destfd
, unsigned long long *destoffsets
)
11601 struct intel_super
*super
= st
->sb
;
11602 struct intel_dev
*dv
;
11603 unsigned int sector_size
= super
->sector_size
;
11604 struct imsm_dev
*dev
= NULL
;
11605 struct imsm_map
*map_src
;
11606 int migr_vol_qan
= 0;
11607 int ndata
, odata
; /* [bytes] */
11608 int chunk
; /* [bytes] */
11609 struct migr_record
*migr_rec
;
11611 unsigned int buf_size
; /* [bytes] */
11612 unsigned long long max_position
; /* array size [bytes] */
11613 unsigned long long next_step
; /* [blocks]/[bytes] */
11614 unsigned long long old_data_stripe_length
;
11615 unsigned long long start_src
; /* [bytes] */
11616 unsigned long long start
; /* [bytes] */
11617 unsigned long long start_buf_shift
; /* [bytes] */
11619 int source_layout
= 0;
11624 if (!fds
|| !offsets
)
11627 /* Find volume during the reshape */
11628 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
11629 if (dv
->dev
->vol
.migr_type
== MIGR_GEN_MIGR
11630 && dv
->dev
->vol
.migr_state
== 1) {
11635 /* Only one volume can migrate at the same time */
11636 if (migr_vol_qan
!= 1) {
11637 pr_err("%s", migr_vol_qan
?
11638 "Number of migrating volumes greater than 1\n" :
11639 "There is no volume during migrationg\n");
11643 map_src
= get_imsm_map(dev
, MAP_1
);
11644 if (map_src
== NULL
)
11647 ndata
= imsm_num_data_members(dev
, MAP_0
);
11648 odata
= imsm_num_data_members(dev
, MAP_1
);
11650 chunk
= __le16_to_cpu(map_src
->blocks_per_strip
) * 512;
11651 old_data_stripe_length
= odata
* chunk
;
11653 migr_rec
= super
->migr_rec
;
11655 /* initialize migration record for start condition */
11656 if (sra
->reshape_progress
== 0)
11657 init_migr_record_imsm(st
, dev
, sra
);
11659 if (__le32_to_cpu(migr_rec
->rec_status
) != UNIT_SRC_NORMAL
) {
11660 dprintf("imsm: cannot restart migration when data are present in copy area.\n");
11663 /* Save checkpoint to update migration record for current
11664 * reshape position (in md). It can be farther than current
11665 * reshape position in metadata.
11667 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
11668 /* ignore error == 2, this can mean end of reshape here
11670 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL, initial save)\n");
11675 /* size for data */
11676 buf_size
= __le32_to_cpu(migr_rec
->blocks_per_unit
) * 512;
11677 /* extend buffer size for parity disk */
11678 buf_size
+= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
11679 /* add space for stripe aligment */
11680 buf_size
+= old_data_stripe_length
;
11681 if (posix_memalign((void **)&buf
, MAX_SECTOR_SIZE
, buf_size
)) {
11682 dprintf("imsm: Cannot allocate checkpoint buffer\n");
11686 max_position
= sra
->component_size
* ndata
;
11687 source_layout
= imsm_level_to_layout(map_src
->raid_level
);
11689 while (__le32_to_cpu(migr_rec
->curr_migr_unit
) <
11690 __le32_to_cpu(migr_rec
->num_migr_units
)) {
11691 /* current reshape position [blocks] */
11692 unsigned long long current_position
=
11693 __le32_to_cpu(migr_rec
->blocks_per_unit
)
11694 * __le32_to_cpu(migr_rec
->curr_migr_unit
);
11695 unsigned long long border
;
11697 /* Check that array hasn't become failed.
11699 degraded
= check_degradation_change(sra
, fds
, degraded
);
11700 if (degraded
> 1) {
11701 dprintf("imsm: Abort reshape due to degradation level (%i)\n", degraded
);
11705 next_step
= __le32_to_cpu(migr_rec
->blocks_per_unit
);
11707 if ((current_position
+ next_step
) > max_position
)
11708 next_step
= max_position
- current_position
;
11710 start
= current_position
* 512;
11712 /* align reading start to old geometry */
11713 start_buf_shift
= start
% old_data_stripe_length
;
11714 start_src
= start
- start_buf_shift
;
11716 border
= (start_src
/ odata
) - (start
/ ndata
);
11718 if (border
<= __le32_to_cpu(migr_rec
->dest_depth_per_unit
)) {
11719 /* save critical stripes to buf
11720 * start - start address of current unit
11721 * to backup [bytes]
11722 * start_src - start address of current unit
11723 * to backup alligned to source array
11726 unsigned long long next_step_filler
;
11727 unsigned long long copy_length
= next_step
* 512;
11729 /* allign copy area length to stripe in old geometry */
11730 next_step_filler
= ((copy_length
+ start_buf_shift
)
11731 % old_data_stripe_length
);
11732 if (next_step_filler
)
11733 next_step_filler
= (old_data_stripe_length
11734 - next_step_filler
);
11735 dprintf("save_stripes() parameters: start = %llu,\tstart_src = %llu,\tnext_step*512 = %llu,\tstart_in_buf_shift = %llu,\tnext_step_filler = %llu\n",
11736 start
, start_src
, copy_length
,
11737 start_buf_shift
, next_step_filler
);
11739 if (save_stripes(fds
, offsets
, map_src
->num_members
,
11740 chunk
, map_src
->raid_level
,
11741 source_layout
, 0, NULL
, start_src
,
11743 next_step_filler
+ start_buf_shift
,
11745 dprintf("imsm: Cannot save stripes to buffer\n");
11748 /* Convert data to destination format and store it
11749 * in backup general migration area
11751 if (save_backup_imsm(st
, dev
, sra
,
11752 buf
+ start_buf_shift
, copy_length
)) {
11753 dprintf("imsm: Cannot save stripes to target devices\n");
11756 if (save_checkpoint_imsm(st
, sra
,
11757 UNIT_SRC_IN_CP_AREA
)) {
11758 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_IN_CP_AREA)\n");
11762 /* set next step to use whole border area */
11763 border
/= next_step
;
11765 next_step
*= border
;
11767 /* When data backed up, checkpoint stored,
11768 * kick the kernel to reshape unit of data
11770 next_step
= next_step
+ sra
->reshape_progress
;
11771 /* limit next step to array max position */
11772 if (next_step
> max_position
)
11773 next_step
= max_position
;
11774 sysfs_set_num(sra
, NULL
, "suspend_lo", sra
->reshape_progress
);
11775 sysfs_set_num(sra
, NULL
, "suspend_hi", next_step
);
11776 sra
->reshape_progress
= next_step
;
11778 /* wait until reshape finish */
11779 if (wait_for_reshape_imsm(sra
, ndata
)) {
11780 dprintf("wait_for_reshape_imsm returned error!\n");
11786 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
11787 /* ignore error == 2, this can mean end of reshape here
11789 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL)\n");
11795 /* clear migr_rec on disks after successful migration */
11798 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
);
11799 for (d
= super
->disks
; d
; d
= d
->next
) {
11800 if (d
->index
< 0 || is_failed(&d
->disk
))
11802 unsigned long long dsize
;
11804 get_dev_size(d
->fd
, NULL
, &dsize
);
11805 if (lseek64(d
->fd
, dsize
- MIGR_REC_SECTOR_POSITION
*sector_size
,
11807 if ((unsigned int)write(d
->fd
, super
->migr_rec_buf
,
11808 MIGR_REC_BUF_SECTORS
*sector_size
) !=
11809 MIGR_REC_BUF_SECTORS
*sector_size
)
11810 perror("Write migr_rec failed");
11814 /* return '1' if done */
11818 /* See Grow.c: abort_reshape() for further explanation */
11819 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
11820 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
11821 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
11826 struct superswitch super_imsm
= {
11827 .examine_super
= examine_super_imsm
,
11828 .brief_examine_super
= brief_examine_super_imsm
,
11829 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
11830 .export_examine_super
= export_examine_super_imsm
,
11831 .detail_super
= detail_super_imsm
,
11832 .brief_detail_super
= brief_detail_super_imsm
,
11833 .write_init_super
= write_init_super_imsm
,
11834 .validate_geometry
= validate_geometry_imsm
,
11835 .add_to_super
= add_to_super_imsm
,
11836 .remove_from_super
= remove_from_super_imsm
,
11837 .detail_platform
= detail_platform_imsm
,
11838 .export_detail_platform
= export_detail_platform_imsm
,
11839 .kill_subarray
= kill_subarray_imsm
,
11840 .update_subarray
= update_subarray_imsm
,
11841 .load_container
= load_container_imsm
,
11842 .default_geometry
= default_geometry_imsm
,
11843 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
11844 .reshape_super
= imsm_reshape_super
,
11845 .manage_reshape
= imsm_manage_reshape
,
11846 .recover_backup
= recover_backup_imsm
,
11847 .copy_metadata
= copy_metadata_imsm
,
11848 .examine_badblocks
= examine_badblocks_imsm
,
11849 .match_home
= match_home_imsm
,
11850 .uuid_from_super
= uuid_from_super_imsm
,
11851 .getinfo_super
= getinfo_super_imsm
,
11852 .getinfo_super_disks
= getinfo_super_disks_imsm
,
11853 .update_super
= update_super_imsm
,
11855 .avail_size
= avail_size_imsm
,
11856 .min_acceptable_spare_size
= min_acceptable_spare_size_imsm
,
11858 .compare_super
= compare_super_imsm
,
11860 .load_super
= load_super_imsm
,
11861 .init_super
= init_super_imsm
,
11862 .store_super
= store_super_imsm
,
11863 .free_super
= free_super_imsm
,
11864 .match_metadata_desc
= match_metadata_desc_imsm
,
11865 .container_content
= container_content_imsm
,
11866 .validate_container
= validate_container_imsm
,
11868 .write_init_ppl
= write_init_ppl_imsm
,
11869 .validate_ppl
= validate_ppl_imsm
,
11875 .open_new
= imsm_open_new
,
11876 .set_array_state
= imsm_set_array_state
,
11877 .set_disk
= imsm_set_disk
,
11878 .sync_metadata
= imsm_sync_metadata
,
11879 .activate_spare
= imsm_activate_spare
,
11880 .process_update
= imsm_process_update
,
11881 .prepare_update
= imsm_prepare_update
,
11882 .record_bad_block
= imsm_record_badblock
,
11883 .clear_bad_block
= imsm_clear_badblock
,
11884 .get_bad_blocks
= imsm_get_badblocks
,