2 * mdadm - Intel(R) Matrix Storage Manager Support
4 * Copyright (C) 2002-2008 Intel Corporation
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20 #define HAVE_STDINT_H 1
24 #include "platform-intel.h"
30 /* MPB == Metadata Parameter Block */
31 #define MPB_SIGNATURE "Intel Raid ISM Cfg Sig. "
32 #define MPB_SIG_LEN (strlen(MPB_SIGNATURE))
33 #define MPB_VERSION_RAID0 "1.0.00"
34 #define MPB_VERSION_RAID1 "1.1.00"
35 #define MPB_VERSION_MANY_VOLUMES_PER_ARRAY "1.2.00"
36 #define MPB_VERSION_3OR4_DISK_ARRAY "1.2.01"
37 #define MPB_VERSION_RAID5 "1.2.02"
38 #define MPB_VERSION_5OR6_DISK_ARRAY "1.2.04"
39 #define MPB_VERSION_CNG "1.2.06"
40 #define MPB_VERSION_ATTRIBS "1.3.00"
41 #define MAX_SIGNATURE_LENGTH 32
42 #define MAX_RAID_SERIAL_LEN 16
45 #define MPB_ATTRIB_RAID0 __cpu_to_le32(0x00000001)
47 #define MPB_ATTRIB_RAID1 __cpu_to_le32(0x00000002)
49 #define MPB_ATTRIB_RAID10 __cpu_to_le32(0x00000004)
51 #define MPB_ATTRIB_RAID1E __cpu_to_le32(0x00000008)
53 #define MPB_ATTRIB_RAID5 __cpu_to_le32(0x00000010)
54 /* supports RAID CNG */
55 #define MPB_ATTRIB_RAIDCNG __cpu_to_le32(0x00000020)
56 /* supports expanded stripe sizes of 256K, 512K and 1MB */
57 #define MPB_ATTRIB_EXP_STRIPE_SIZE __cpu_to_le32(0x00000040)
59 /* The OROM Support RST Caching of Volumes */
60 #define MPB_ATTRIB_NVM __cpu_to_le32(0x02000000)
61 /* The OROM supports creating disks greater than 2TB */
62 #define MPB_ATTRIB_2TB_DISK __cpu_to_le32(0x04000000)
63 /* The OROM supports Bad Block Management */
64 #define MPB_ATTRIB_BBM __cpu_to_le32(0x08000000)
66 /* THe OROM Supports NVM Caching of Volumes */
67 #define MPB_ATTRIB_NEVER_USE2 __cpu_to_le32(0x10000000)
68 /* The OROM supports creating volumes greater than 2TB */
69 #define MPB_ATTRIB_2TB __cpu_to_le32(0x20000000)
70 /* originally for PMP, now it's wasted b/c. Never use this bit! */
71 #define MPB_ATTRIB_NEVER_USE __cpu_to_le32(0x40000000)
72 /* Verify MPB contents against checksum after reading MPB */
73 #define MPB_ATTRIB_CHECKSUM_VERIFY __cpu_to_le32(0x80000000)
75 /* Define all supported attributes that have to be accepted by mdadm
77 #define MPB_ATTRIB_SUPPORTED (MPB_ATTRIB_CHECKSUM_VERIFY | \
79 MPB_ATTRIB_2TB_DISK | \
84 MPB_ATTRIB_EXP_STRIPE_SIZE | \
87 /* Define attributes that are unused but not harmful */
88 #define MPB_ATTRIB_IGNORED (MPB_ATTRIB_NEVER_USE)
90 #define MPB_SECTOR_CNT 2210
91 #define IMSM_RESERVED_SECTORS 8192
92 #define NUM_BLOCKS_DIRTY_STRIPE_REGION 2048
93 #define SECT_PER_MB_SHIFT 11
94 #define MAX_SECTOR_SIZE 4096
95 #define MULTIPLE_PPL_AREA_SIZE_IMSM (1024 * 1024) /* Size of the whole
99 /* Disk configuration info. */
100 #define IMSM_MAX_DEVICES 255
102 __u8 serial
[MAX_RAID_SERIAL_LEN
];/* 0xD8 - 0xE7 ascii serial number */
103 __u32 total_blocks_lo
; /* 0xE8 - 0xEB total blocks lo */
104 __u32 scsi_id
; /* 0xEC - 0xEF scsi ID */
105 #define SPARE_DISK __cpu_to_le32(0x01) /* Spare */
106 #define CONFIGURED_DISK __cpu_to_le32(0x02) /* Member of some RaidDev */
107 #define FAILED_DISK __cpu_to_le32(0x04) /* Permanent failure */
108 #define JOURNAL_DISK __cpu_to_le32(0x2000000) /* Device marked as Journaling Drive */
109 __u32 status
; /* 0xF0 - 0xF3 */
110 __u32 owner_cfg_num
; /* which config 0,1,2... owns this disk */
111 __u32 total_blocks_hi
; /* 0xF4 - 0xF5 total blocks hi */
112 #define IMSM_DISK_FILLERS 3
113 __u32 filler
[IMSM_DISK_FILLERS
]; /* 0xF5 - 0x107 MPB_DISK_FILLERS for future expansion */
116 /* map selector for map managment
122 /* RAID map configuration infos. */
124 __u32 pba_of_lba0_lo
; /* start address of partition */
125 __u32 blocks_per_member_lo
;/* blocks per member */
126 __u32 num_data_stripes_lo
; /* number of data stripes */
127 __u16 blocks_per_strip
;
128 __u8 map_state
; /* Normal, Uninitialized, Degraded, Failed */
129 #define IMSM_T_STATE_NORMAL 0
130 #define IMSM_T_STATE_UNINITIALIZED 1
131 #define IMSM_T_STATE_DEGRADED 2
132 #define IMSM_T_STATE_FAILED 3
134 #define IMSM_T_RAID0 0
135 #define IMSM_T_RAID1 1
136 #define IMSM_T_RAID5 5 /* since metadata version 1.2.02 ? */
137 __u8 num_members
; /* number of member disks */
138 __u8 num_domains
; /* number of parity domains */
139 __u8 failed_disk_num
; /* valid only when state is degraded */
141 __u32 pba_of_lba0_hi
;
142 __u32 blocks_per_member_hi
;
143 __u32 num_data_stripes_hi
;
144 __u32 filler
[4]; /* expansion area */
145 #define IMSM_ORD_REBUILD (1 << 24)
146 __u32 disk_ord_tbl
[1]; /* disk_ord_tbl[num_members],
147 * top byte contains some flags
149 } __attribute__ ((packed
));
152 __u32 curr_migr_unit
;
153 __u32 checkpoint_id
; /* id to access curr_migr_unit */
154 __u8 migr_state
; /* Normal or Migrating */
156 #define MIGR_REBUILD 1
157 #define MIGR_VERIFY 2 /* analagous to echo check > sync_action */
158 #define MIGR_GEN_MIGR 3
159 #define MIGR_STATE_CHANGE 4
160 #define MIGR_REPAIR 5
161 __u8 migr_type
; /* Initializing, Rebuilding, ... */
162 #define RAIDVOL_CLEAN 0
163 #define RAIDVOL_DIRTY 1
164 #define RAIDVOL_DSRECORD_VALID 2
166 __u8 fs_state
; /* fast-sync state for CnG (0xff == disabled) */
167 __u16 verify_errors
; /* number of mismatches */
168 __u16 bad_blocks
; /* number of bad blocks during verify */
170 struct imsm_map map
[1];
171 /* here comes another one if migr_state */
172 } __attribute__ ((packed
));
175 __u8 volume
[MAX_RAID_SERIAL_LEN
];
178 #define DEV_BOOTABLE __cpu_to_le32(0x01)
179 #define DEV_BOOT_DEVICE __cpu_to_le32(0x02)
180 #define DEV_READ_COALESCING __cpu_to_le32(0x04)
181 #define DEV_WRITE_COALESCING __cpu_to_le32(0x08)
182 #define DEV_LAST_SHUTDOWN_DIRTY __cpu_to_le32(0x10)
183 #define DEV_HIDDEN_AT_BOOT __cpu_to_le32(0x20)
184 #define DEV_CURRENTLY_HIDDEN __cpu_to_le32(0x40)
185 #define DEV_VERIFY_AND_FIX __cpu_to_le32(0x80)
186 #define DEV_MAP_STATE_UNINIT __cpu_to_le32(0x100)
187 #define DEV_NO_AUTO_RECOVERY __cpu_to_le32(0x200)
188 #define DEV_CLONE_N_GO __cpu_to_le32(0x400)
189 #define DEV_CLONE_MAN_SYNC __cpu_to_le32(0x800)
190 #define DEV_CNG_MASTER_DISK_NUM __cpu_to_le32(0x1000)
191 __u32 status
; /* Persistent RaidDev status */
192 __u32 reserved_blocks
; /* Reserved blocks at beginning of volume */
196 __u8 cng_master_disk
;
200 __u16 my_vol_raid_dev_num
; /* Used in Unique volume Id for this RaidDev */
206 /* Unique Volume Id of the NvCache Volume associated with this volume */
207 __u32 nvc_vol_orig_family_num
;
208 __u16 nvc_vol_raid_dev_num
;
211 #define RWH_DISTRIBUTED 1
212 #define RWH_JOURNALING_DRIVE 2
213 #define RWH_MULTIPLE_DISTRIBUTED 3
214 #define RWH_MULTIPLE_PPLS_JOURNALING_DRIVE 4
215 #define RWH_MULTIPLE_OFF 5
216 __u8 rwh_policy
; /* Raid Write Hole Policy */
217 __u8 jd_serial
[MAX_RAID_SERIAL_LEN
]; /* Journal Drive serial number */
220 #define IMSM_DEV_FILLERS 3
221 __u32 filler
[IMSM_DEV_FILLERS
];
223 } __attribute__ ((packed
));
226 __u8 sig
[MAX_SIGNATURE_LENGTH
]; /* 0x00 - 0x1F */
227 __u32 check_sum
; /* 0x20 - 0x23 MPB Checksum */
228 __u32 mpb_size
; /* 0x24 - 0x27 Size of MPB */
229 __u32 family_num
; /* 0x28 - 0x2B Checksum from first time this config was written */
230 __u32 generation_num
; /* 0x2C - 0x2F Incremented each time this array's MPB is written */
231 __u32 error_log_size
; /* 0x30 - 0x33 in bytes */
232 __u32 attributes
; /* 0x34 - 0x37 */
233 __u8 num_disks
; /* 0x38 Number of configured disks */
234 __u8 num_raid_devs
; /* 0x39 Number of configured volumes */
235 __u8 error_log_pos
; /* 0x3A */
236 __u8 fill
[1]; /* 0x3B */
237 __u32 cache_size
; /* 0x3c - 0x40 in mb */
238 __u32 orig_family_num
; /* 0x40 - 0x43 original family num */
239 __u32 pwr_cycle_count
; /* 0x44 - 0x47 simulated power cycle count for array */
240 __u32 bbm_log_size
; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */
241 __u16 num_raid_devs_created
; /* 0x4C - 0x4D Used for generating unique
242 * volume IDs for raid_dev created in this array
245 __u16 filler1
; /* 0x4E - 0x4F */
246 #define IMSM_FILLERS 34
247 __u32 filler
[IMSM_FILLERS
]; /* 0x50 - 0xD7 RAID_MPB_FILLERS */
248 struct imsm_disk disk
[1]; /* 0xD8 diskTbl[numDisks] */
249 /* here comes imsm_dev[num_raid_devs] */
250 /* here comes BBM logs */
251 } __attribute__ ((packed
));
253 #define BBM_LOG_MAX_ENTRIES 254
254 #define BBM_LOG_MAX_LBA_ENTRY_VAL 256 /* Represents 256 LBAs */
255 #define BBM_LOG_SIGNATURE 0xabadb10c
257 struct bbm_log_block_addr
{
260 } __attribute__ ((__packed__
));
262 struct bbm_log_entry
{
263 __u8 marked_count
; /* Number of blocks marked - 1 */
264 __u8 disk_ordinal
; /* Disk entry within the imsm_super */
265 struct bbm_log_block_addr defective_block_start
;
266 } __attribute__ ((__packed__
));
269 __u32 signature
; /* 0xABADB10C */
271 struct bbm_log_entry marked_block_entries
[BBM_LOG_MAX_ENTRIES
];
272 } __attribute__ ((__packed__
));
274 static char *map_state_str
[] = { "normal", "uninitialized", "degraded", "failed" };
276 #define BLOCKS_PER_KB (1024/512)
278 #define RAID_DISK_RESERVED_BLOCKS_IMSM_HI 2209
280 #define GEN_MIGR_AREA_SIZE 2048 /* General Migration Copy Area size in blocks */
282 #define MIGR_REC_BUF_SECTORS 1 /* size of migr_record i/o buffer in sectors */
283 #define MIGR_REC_SECTOR_POSITION 1 /* migr_record position offset on disk,
284 * MIGR_REC_BUF_SECTORS <= MIGR_REC_SECTOR_POS
287 #define UNIT_SRC_NORMAL 0 /* Source data for curr_migr_unit must
288 * be recovered using srcMap */
289 #define UNIT_SRC_IN_CP_AREA 1 /* Source data for curr_migr_unit has
290 * already been migrated and must
291 * be recovered from checkpoint area */
293 #define PPL_ENTRY_SPACE (128 * 1024) /* Size of single PPL, without the header */
296 __u32 rec_status
; /* Status used to determine how to restart
297 * migration in case it aborts
299 __u32 curr_migr_unit
; /* 0..numMigrUnits-1 */
300 __u32 family_num
; /* Family number of MPB
301 * containing the RaidDev
302 * that is migrating */
303 __u32 ascending_migr
; /* True if migrating in increasing
305 __u32 blocks_per_unit
; /* Num disk blocks per unit of operation */
306 __u32 dest_depth_per_unit
; /* Num member blocks each destMap
308 * advances per unit-of-operation */
309 __u32 ckpt_area_pba
; /* Pba of first block of ckpt copy area */
310 __u32 dest_1st_member_lba
; /* First member lba on first
311 * stripe of destination */
312 __u32 num_migr_units
; /* Total num migration units-of-op */
313 __u32 post_migr_vol_cap
; /* Size of volume after
314 * migration completes */
315 __u32 post_migr_vol_cap_hi
; /* Expansion space for LBA64 */
316 __u32 ckpt_read_disk_num
; /* Which member disk in destSubMap[0] the
317 * migration ckpt record was read from
318 * (for recovered migrations) */
319 } __attribute__ ((__packed__
));
324 * 2: metadata does not match
332 struct md_list
*next
;
335 #define pr_vrb(fmt, arg...) (void) (verbose && pr_err(fmt, ##arg))
337 static __u8
migr_type(struct imsm_dev
*dev
)
339 if (dev
->vol
.migr_type
== MIGR_VERIFY
&&
340 dev
->status
& DEV_VERIFY_AND_FIX
)
343 return dev
->vol
.migr_type
;
346 static void set_migr_type(struct imsm_dev
*dev
, __u8 migr_type
)
348 /* for compatibility with older oroms convert MIGR_REPAIR, into
349 * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
351 if (migr_type
== MIGR_REPAIR
) {
352 dev
->vol
.migr_type
= MIGR_VERIFY
;
353 dev
->status
|= DEV_VERIFY_AND_FIX
;
355 dev
->vol
.migr_type
= migr_type
;
356 dev
->status
&= ~DEV_VERIFY_AND_FIX
;
360 static unsigned int sector_count(__u32 bytes
, unsigned int sector_size
)
362 return ROUND_UP(bytes
, sector_size
) / sector_size
;
365 static unsigned int mpb_sectors(struct imsm_super
*mpb
,
366 unsigned int sector_size
)
368 return sector_count(__le32_to_cpu(mpb
->mpb_size
), sector_size
);
372 struct imsm_dev
*dev
;
373 struct intel_dev
*next
;
378 enum sys_dev_type type
;
381 struct intel_hba
*next
;
388 /* internal representation of IMSM metadata */
391 void *buf
; /* O_DIRECT buffer for reading/writing metadata */
392 struct imsm_super
*anchor
; /* immovable parameters */
395 void *migr_rec_buf
; /* buffer for I/O operations */
396 struct migr_record
*migr_rec
; /* migration record */
398 int clean_migration_record_by_mdmon
; /* when reshape is switched to next
399 array, it indicates that mdmon is allowed to clean migration
401 size_t len
; /* size of the 'buf' allocation */
402 size_t extra_space
; /* extra space in 'buf' that is not used yet */
403 void *next_buf
; /* for realloc'ing buf from the manager */
405 int updates_pending
; /* count of pending updates for mdmon */
406 int current_vol
; /* index of raid device undergoing creation */
407 unsigned long long create_offset
; /* common start for 'current_vol' */
408 __u32 random
; /* random data for seeding new family numbers */
409 struct intel_dev
*devlist
;
410 unsigned int sector_size
; /* sector size of used member drives */
414 __u8 serial
[MAX_RAID_SERIAL_LEN
];
417 struct imsm_disk disk
;
420 struct extent
*e
; /* for determining freespace @ create */
421 int raiddisk
; /* slot to fill in autolayout */
423 } *disks
, *current_disk
;
424 struct dl
*disk_mgmt_list
; /* list of disks to add/remove while mdmon
426 struct dl
*missing
; /* disks removed while we weren't looking */
427 struct bbm_log
*bbm_log
;
428 struct intel_hba
*hba
; /* device path of the raid controller for this metadata */
429 const struct imsm_orom
*orom
; /* platform firmware support */
430 struct intel_super
*next
; /* (temp) list for disambiguating family_num */
431 struct md_bb bb
; /* memory for get_bad_blocks call */
435 struct imsm_disk disk
;
436 #define IMSM_UNKNOWN_OWNER (-1)
438 struct intel_disk
*next
;
442 unsigned long long start
, size
;
445 /* definitions of reshape process types */
446 enum imsm_reshape_type
{
452 /* definition of messages passed to imsm_process_update */
453 enum imsm_update_type
{
454 update_activate_spare
,
458 update_add_remove_disk
,
459 update_reshape_container_disks
,
460 update_reshape_migration
,
462 update_general_migration_checkpoint
,
464 update_prealloc_badblocks_mem
,
468 struct imsm_update_activate_spare
{
469 enum imsm_update_type type
;
473 struct imsm_update_activate_spare
*next
;
479 unsigned long long size
;
486 enum takeover_direction
{
490 struct imsm_update_takeover
{
491 enum imsm_update_type type
;
493 enum takeover_direction direction
;
496 struct imsm_update_reshape
{
497 enum imsm_update_type type
;
501 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
504 struct imsm_update_reshape_migration
{
505 enum imsm_update_type type
;
508 /* fields for array migration changes
515 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
518 struct imsm_update_size_change
{
519 enum imsm_update_type type
;
524 struct imsm_update_general_migration_checkpoint
{
525 enum imsm_update_type type
;
526 __u32 curr_migr_unit
;
530 __u8 serial
[MAX_RAID_SERIAL_LEN
];
533 struct imsm_update_create_array
{
534 enum imsm_update_type type
;
539 struct imsm_update_kill_array
{
540 enum imsm_update_type type
;
544 struct imsm_update_rename_array
{
545 enum imsm_update_type type
;
546 __u8 name
[MAX_RAID_SERIAL_LEN
];
550 struct imsm_update_add_remove_disk
{
551 enum imsm_update_type type
;
554 struct imsm_update_prealloc_bb_mem
{
555 enum imsm_update_type type
;
558 struct imsm_update_rwh_policy
{
559 enum imsm_update_type type
;
564 static const char *_sys_dev_type
[] = {
565 [SYS_DEV_UNKNOWN
] = "Unknown",
566 [SYS_DEV_SAS
] = "SAS",
567 [SYS_DEV_SATA
] = "SATA",
568 [SYS_DEV_NVME
] = "NVMe",
569 [SYS_DEV_VMD
] = "VMD"
572 const char *get_sys_dev_type(enum sys_dev_type type
)
574 if (type
>= SYS_DEV_MAX
)
575 type
= SYS_DEV_UNKNOWN
;
577 return _sys_dev_type
[type
];
580 static struct intel_hba
* alloc_intel_hba(struct sys_dev
*device
)
582 struct intel_hba
*result
= xmalloc(sizeof(*result
));
584 result
->type
= device
->type
;
585 result
->path
= xstrdup(device
->path
);
587 if (result
->path
&& (result
->pci_id
= strrchr(result
->path
, '/')) != NULL
)
593 static struct intel_hba
* find_intel_hba(struct intel_hba
*hba
, struct sys_dev
*device
)
595 struct intel_hba
*result
;
597 for (result
= hba
; result
; result
= result
->next
) {
598 if (result
->type
== device
->type
&& strcmp(result
->path
, device
->path
) == 0)
604 static int attach_hba_to_super(struct intel_super
*super
, struct sys_dev
*device
)
606 struct intel_hba
*hba
;
608 /* check if disk attached to Intel HBA */
609 hba
= find_intel_hba(super
->hba
, device
);
612 /* Check if HBA is already attached to super */
613 if (super
->hba
== NULL
) {
614 super
->hba
= alloc_intel_hba(device
);
619 /* Intel metadata allows for all disks attached to the same type HBA.
620 * Do not support HBA types mixing
622 if (device
->type
!= hba
->type
)
625 /* Multiple same type HBAs can be used if they share the same OROM */
626 const struct imsm_orom
*device_orom
= get_orom_by_device_id(device
->dev_id
);
628 if (device_orom
!= super
->orom
)
634 hba
->next
= alloc_intel_hba(device
);
638 static struct sys_dev
* find_disk_attached_hba(int fd
, const char *devname
)
640 struct sys_dev
*list
, *elem
;
643 if ((list
= find_intel_devices()) == NULL
)
647 disk_path
= (char *) devname
;
649 disk_path
= diskfd_to_devpath(fd
);
654 for (elem
= list
; elem
; elem
= elem
->next
)
655 if (path_attached_to_hba(disk_path
, elem
->path
))
658 if (disk_path
!= devname
)
664 static int find_intel_hba_capability(int fd
, struct intel_super
*super
,
667 static struct supertype
*match_metadata_desc_imsm(char *arg
)
669 struct supertype
*st
;
671 if (strcmp(arg
, "imsm") != 0 &&
672 strcmp(arg
, "default") != 0
676 st
= xcalloc(1, sizeof(*st
));
677 st
->ss
= &super_imsm
;
678 st
->max_devs
= IMSM_MAX_DEVICES
;
679 st
->minor_version
= 0;
684 static __u8
*get_imsm_version(struct imsm_super
*mpb
)
686 return &mpb
->sig
[MPB_SIG_LEN
];
689 /* retrieve a disk directly from the anchor when the anchor is known to be
690 * up-to-date, currently only at load time
692 static struct imsm_disk
*__get_imsm_disk(struct imsm_super
*mpb
, __u8 index
)
694 if (index
>= mpb
->num_disks
)
696 return &mpb
->disk
[index
];
699 /* retrieve the disk description based on a index of the disk
702 static struct dl
*get_imsm_dl_disk(struct intel_super
*super
, __u8 index
)
706 for (d
= super
->disks
; d
; d
= d
->next
)
707 if (d
->index
== index
)
712 /* retrieve a disk from the parsed metadata */
713 static struct imsm_disk
*get_imsm_disk(struct intel_super
*super
, __u8 index
)
717 dl
= get_imsm_dl_disk(super
, index
);
724 /* generate a checksum directly from the anchor when the anchor is known to be
725 * up-to-date, currently only at load or write_super after coalescing
727 static __u32
__gen_imsm_checksum(struct imsm_super
*mpb
)
729 __u32 end
= mpb
->mpb_size
/ sizeof(end
);
730 __u32
*p
= (__u32
*) mpb
;
734 sum
+= __le32_to_cpu(*p
);
738 return sum
- __le32_to_cpu(mpb
->check_sum
);
741 static size_t sizeof_imsm_map(struct imsm_map
*map
)
743 return sizeof(struct imsm_map
) + sizeof(__u32
) * (map
->num_members
- 1);
746 struct imsm_map
*get_imsm_map(struct imsm_dev
*dev
, int second_map
)
748 /* A device can have 2 maps if it is in the middle of a migration.
750 * MAP_0 - we return the first map
751 * MAP_1 - we return the second map if it exists, else NULL
752 * MAP_X - we return the second map if it exists, else the first
754 struct imsm_map
*map
= &dev
->vol
.map
[0];
755 struct imsm_map
*map2
= NULL
;
757 if (dev
->vol
.migr_state
)
758 map2
= (void *)map
+ sizeof_imsm_map(map
);
760 switch (second_map
) {
777 /* return the size of the device.
778 * migr_state increases the returned size if map[0] were to be duplicated
780 static size_t sizeof_imsm_dev(struct imsm_dev
*dev
, int migr_state
)
782 size_t size
= sizeof(*dev
) - sizeof(struct imsm_map
) +
783 sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
785 /* migrating means an additional map */
786 if (dev
->vol
.migr_state
)
787 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_1
));
789 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
794 /* retrieve disk serial number list from a metadata update */
795 static struct disk_info
*get_disk_info(struct imsm_update_create_array
*update
)
798 struct disk_info
*inf
;
800 inf
= u
+ sizeof(*update
) - sizeof(struct imsm_dev
) +
801 sizeof_imsm_dev(&update
->dev
, 0);
806 static struct imsm_dev
*__get_imsm_dev(struct imsm_super
*mpb
, __u8 index
)
812 if (index
>= mpb
->num_raid_devs
)
815 /* devices start after all disks */
816 offset
= ((void *) &mpb
->disk
[mpb
->num_disks
]) - _mpb
;
818 for (i
= 0; i
<= index
; i
++)
820 return _mpb
+ offset
;
822 offset
+= sizeof_imsm_dev(_mpb
+ offset
, 0);
827 static struct imsm_dev
*get_imsm_dev(struct intel_super
*super
, __u8 index
)
829 struct intel_dev
*dv
;
831 if (index
>= super
->anchor
->num_raid_devs
)
833 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
834 if (dv
->index
== index
)
839 static inline unsigned long long __le48_to_cpu(const struct bbm_log_block_addr
842 return ((((__u64
)__le32_to_cpu(addr
->dw1
)) << 16) |
843 __le16_to_cpu(addr
->w1
));
846 static inline struct bbm_log_block_addr
__cpu_to_le48(unsigned long long sec
)
848 struct bbm_log_block_addr addr
;
850 addr
.w1
= __cpu_to_le16((__u16
)(sec
& 0xffff));
851 addr
.dw1
= __cpu_to_le32((__u32
)(sec
>> 16) & 0xffffffff);
855 /* get size of the bbm log */
856 static __u32
get_imsm_bbm_log_size(struct bbm_log
*log
)
858 if (!log
|| log
->entry_count
== 0)
861 return sizeof(log
->signature
) +
862 sizeof(log
->entry_count
) +
863 log
->entry_count
* sizeof(struct bbm_log_entry
);
866 /* check if bad block is not partially stored in bbm log */
867 static int is_stored_in_bbm(struct bbm_log
*log
, const __u8 idx
, const unsigned
868 long long sector
, const int length
, __u32
*pos
)
872 for (i
= *pos
; i
< log
->entry_count
; i
++) {
873 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[i
];
874 unsigned long long bb_start
;
875 unsigned long long bb_end
;
877 bb_start
= __le48_to_cpu(&entry
->defective_block_start
);
878 bb_end
= bb_start
+ (entry
->marked_count
+ 1);
880 if ((entry
->disk_ordinal
== idx
) && (bb_start
>= sector
) &&
881 (bb_end
<= sector
+ length
)) {
889 /* record new bad block in bbm log */
890 static int record_new_badblock(struct bbm_log
*log
, const __u8 idx
, unsigned
891 long long sector
, int length
)
895 struct bbm_log_entry
*entry
= NULL
;
897 while (is_stored_in_bbm(log
, idx
, sector
, length
, &pos
)) {
898 struct bbm_log_entry
*e
= &log
->marked_block_entries
[pos
];
900 if ((e
->marked_count
+ 1 == BBM_LOG_MAX_LBA_ENTRY_VAL
) &&
901 (__le48_to_cpu(&e
->defective_block_start
) == sector
)) {
902 sector
+= BBM_LOG_MAX_LBA_ENTRY_VAL
;
903 length
-= BBM_LOG_MAX_LBA_ENTRY_VAL
;
912 int cnt
= (length
<= BBM_LOG_MAX_LBA_ENTRY_VAL
) ? length
:
913 BBM_LOG_MAX_LBA_ENTRY_VAL
;
914 entry
->defective_block_start
= __cpu_to_le48(sector
);
915 entry
->marked_count
= cnt
- 1;
922 new_bb
= ROUND_UP(length
, BBM_LOG_MAX_LBA_ENTRY_VAL
) /
923 BBM_LOG_MAX_LBA_ENTRY_VAL
;
924 if (log
->entry_count
+ new_bb
> BBM_LOG_MAX_ENTRIES
)
928 int cnt
= (length
<= BBM_LOG_MAX_LBA_ENTRY_VAL
) ? length
:
929 BBM_LOG_MAX_LBA_ENTRY_VAL
;
930 struct bbm_log_entry
*entry
=
931 &log
->marked_block_entries
[log
->entry_count
];
933 entry
->defective_block_start
= __cpu_to_le48(sector
);
934 entry
->marked_count
= cnt
- 1;
935 entry
->disk_ordinal
= idx
;
946 /* clear all bad blocks for given disk */
947 static void clear_disk_badblocks(struct bbm_log
*log
, const __u8 idx
)
951 while (i
< log
->entry_count
) {
952 struct bbm_log_entry
*entries
= log
->marked_block_entries
;
954 if (entries
[i
].disk_ordinal
== idx
) {
955 if (i
< log
->entry_count
- 1)
956 entries
[i
] = entries
[log
->entry_count
- 1];
964 /* clear given bad block */
965 static int clear_badblock(struct bbm_log
*log
, const __u8 idx
, const unsigned
966 long long sector
, const int length
) {
969 while (i
< log
->entry_count
) {
970 struct bbm_log_entry
*entries
= log
->marked_block_entries
;
972 if ((entries
[i
].disk_ordinal
== idx
) &&
973 (__le48_to_cpu(&entries
[i
].defective_block_start
) ==
974 sector
) && (entries
[i
].marked_count
+ 1 == length
)) {
975 if (i
< log
->entry_count
- 1)
976 entries
[i
] = entries
[log
->entry_count
- 1];
986 /* allocate and load BBM log from metadata */
987 static int load_bbm_log(struct intel_super
*super
)
989 struct imsm_super
*mpb
= super
->anchor
;
990 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
992 super
->bbm_log
= xcalloc(1, sizeof(struct bbm_log
));
997 struct bbm_log
*log
= (void *)mpb
+
998 __le32_to_cpu(mpb
->mpb_size
) - bbm_log_size
;
1002 if (bbm_log_size
< sizeof(log
->signature
) +
1003 sizeof(log
->entry_count
))
1006 entry_count
= __le32_to_cpu(log
->entry_count
);
1007 if ((__le32_to_cpu(log
->signature
) != BBM_LOG_SIGNATURE
) ||
1008 (entry_count
> BBM_LOG_MAX_ENTRIES
))
1012 sizeof(log
->signature
) + sizeof(log
->entry_count
) +
1013 entry_count
* sizeof(struct bbm_log_entry
))
1016 memcpy(super
->bbm_log
, log
, bbm_log_size
);
1018 super
->bbm_log
->signature
= __cpu_to_le32(BBM_LOG_SIGNATURE
);
1019 super
->bbm_log
->entry_count
= 0;
1025 /* checks if bad block is within volume boundaries */
1026 static int is_bad_block_in_volume(const struct bbm_log_entry
*entry
,
1027 const unsigned long long start_sector
,
1028 const unsigned long long size
)
1030 unsigned long long bb_start
;
1031 unsigned long long bb_end
;
1033 bb_start
= __le48_to_cpu(&entry
->defective_block_start
);
1034 bb_end
= bb_start
+ (entry
->marked_count
+ 1);
1036 if (((bb_start
>= start_sector
) && (bb_start
< start_sector
+ size
)) ||
1037 ((bb_end
>= start_sector
) && (bb_end
<= start_sector
+ size
)))
1043 /* get list of bad blocks on a drive for a volume */
1044 static void get_volume_badblocks(const struct bbm_log
*log
, const __u8 idx
,
1045 const unsigned long long start_sector
,
1046 const unsigned long long size
,
1052 for (i
= 0; i
< log
->entry_count
; i
++) {
1053 const struct bbm_log_entry
*ent
=
1054 &log
->marked_block_entries
[i
];
1055 struct md_bb_entry
*bb
;
1057 if ((ent
->disk_ordinal
== idx
) &&
1058 is_bad_block_in_volume(ent
, start_sector
, size
)) {
1060 if (!bbs
->entries
) {
1061 bbs
->entries
= xmalloc(BBM_LOG_MAX_ENTRIES
*
1067 bb
= &bbs
->entries
[count
++];
1068 bb
->sector
= __le48_to_cpu(&ent
->defective_block_start
);
1069 bb
->length
= ent
->marked_count
+ 1;
1077 * == MAP_0 get first map
1078 * == MAP_1 get second map
1079 * == MAP_X than get map according to the current migr_state
1081 static __u32
get_imsm_ord_tbl_ent(struct imsm_dev
*dev
,
1085 struct imsm_map
*map
;
1087 map
= get_imsm_map(dev
, second_map
);
1089 /* top byte identifies disk under rebuild */
1090 return __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
1093 #define ord_to_idx(ord) (((ord) << 8) >> 8)
1094 static __u32
get_imsm_disk_idx(struct imsm_dev
*dev
, int slot
, int second_map
)
1096 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, second_map
);
1098 return ord_to_idx(ord
);
1101 static void set_imsm_ord_tbl_ent(struct imsm_map
*map
, int slot
, __u32 ord
)
1103 map
->disk_ord_tbl
[slot
] = __cpu_to_le32(ord
);
1106 static int get_imsm_disk_slot(struct imsm_map
*map
, unsigned idx
)
1111 for (slot
= 0; slot
< map
->num_members
; slot
++) {
1112 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
1113 if (ord_to_idx(ord
) == idx
)
1120 static int get_imsm_raid_level(struct imsm_map
*map
)
1122 if (map
->raid_level
== 1) {
1123 if (map
->num_members
== 2)
1129 return map
->raid_level
;
1132 static int cmp_extent(const void *av
, const void *bv
)
1134 const struct extent
*a
= av
;
1135 const struct extent
*b
= bv
;
1136 if (a
->start
< b
->start
)
1138 if (a
->start
> b
->start
)
1143 static int count_memberships(struct dl
*dl
, struct intel_super
*super
)
1145 int memberships
= 0;
1148 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1149 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1150 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1152 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
1159 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
);
1161 static int split_ull(unsigned long long n
, __u32
*lo
, __u32
*hi
)
1163 if (lo
== 0 || hi
== 0)
1165 *lo
= __le32_to_cpu((unsigned)n
);
1166 *hi
= __le32_to_cpu((unsigned)(n
>> 32));
1170 static unsigned long long join_u32(__u32 lo
, __u32 hi
)
1172 return (unsigned long long)__le32_to_cpu(lo
) |
1173 (((unsigned long long)__le32_to_cpu(hi
)) << 32);
1176 static unsigned long long total_blocks(struct imsm_disk
*disk
)
1180 return join_u32(disk
->total_blocks_lo
, disk
->total_blocks_hi
);
1183 static unsigned long long pba_of_lba0(struct imsm_map
*map
)
1187 return join_u32(map
->pba_of_lba0_lo
, map
->pba_of_lba0_hi
);
1190 static unsigned long long blocks_per_member(struct imsm_map
*map
)
1194 return join_u32(map
->blocks_per_member_lo
, map
->blocks_per_member_hi
);
1197 static unsigned long long num_data_stripes(struct imsm_map
*map
)
1201 return join_u32(map
->num_data_stripes_lo
, map
->num_data_stripes_hi
);
1204 static unsigned long long imsm_dev_size(struct imsm_dev
*dev
)
1208 return join_u32(dev
->size_low
, dev
->size_high
);
1211 static void set_total_blocks(struct imsm_disk
*disk
, unsigned long long n
)
1213 split_ull(n
, &disk
->total_blocks_lo
, &disk
->total_blocks_hi
);
1216 static void set_pba_of_lba0(struct imsm_map
*map
, unsigned long long n
)
1218 split_ull(n
, &map
->pba_of_lba0_lo
, &map
->pba_of_lba0_hi
);
1221 static void set_blocks_per_member(struct imsm_map
*map
, unsigned long long n
)
1223 split_ull(n
, &map
->blocks_per_member_lo
, &map
->blocks_per_member_hi
);
1226 static void set_num_data_stripes(struct imsm_map
*map
, unsigned long long n
)
1228 split_ull(n
, &map
->num_data_stripes_lo
, &map
->num_data_stripes_hi
);
1231 static void set_imsm_dev_size(struct imsm_dev
*dev
, unsigned long long n
)
1233 split_ull(n
, &dev
->size_low
, &dev
->size_high
);
1236 static unsigned long long per_dev_array_size(struct imsm_map
*map
)
1238 unsigned long long array_size
= 0;
1243 array_size
= num_data_stripes(map
) * map
->blocks_per_strip
;
1244 if (get_imsm_raid_level(map
) == 1 || get_imsm_raid_level(map
) == 10)
1250 static struct extent
*get_extents(struct intel_super
*super
, struct dl
*dl
)
1252 /* find a list of used extents on the given physical device */
1253 struct extent
*rv
, *e
;
1255 int memberships
= count_memberships(dl
, super
);
1258 /* trim the reserved area for spares, so they can join any array
1259 * regardless of whether the OROM has assigned sectors from the
1260 * IMSM_RESERVED_SECTORS region
1262 if (dl
->index
== -1)
1263 reservation
= imsm_min_reserved_sectors(super
);
1265 reservation
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1267 rv
= xcalloc(sizeof(struct extent
), (memberships
+ 1));
1270 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1271 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1272 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1274 if (get_imsm_disk_slot(map
, dl
->index
) >= 0) {
1275 e
->start
= pba_of_lba0(map
);
1276 e
->size
= per_dev_array_size(map
);
1280 qsort(rv
, memberships
, sizeof(*rv
), cmp_extent
);
1282 /* determine the start of the metadata
1283 * when no raid devices are defined use the default
1284 * ...otherwise allow the metadata to truncate the value
1285 * as is the case with older versions of imsm
1288 struct extent
*last
= &rv
[memberships
- 1];
1289 unsigned long long remainder
;
1291 remainder
= total_blocks(&dl
->disk
) - (last
->start
+ last
->size
);
1292 /* round down to 1k block to satisfy precision of the kernel
1296 /* make sure remainder is still sane */
1297 if (remainder
< (unsigned)ROUND_UP(super
->len
, 512) >> 9)
1298 remainder
= ROUND_UP(super
->len
, 512) >> 9;
1299 if (reservation
> remainder
)
1300 reservation
= remainder
;
1302 e
->start
= total_blocks(&dl
->disk
) - reservation
;
1307 /* try to determine how much space is reserved for metadata from
1308 * the last get_extents() entry, otherwise fallback to the
1311 static __u32
imsm_reserved_sectors(struct intel_super
*super
, struct dl
*dl
)
1317 /* for spares just return a minimal reservation which will grow
1318 * once the spare is picked up by an array
1320 if (dl
->index
== -1)
1321 return MPB_SECTOR_CNT
;
1323 e
= get_extents(super
, dl
);
1325 return MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1327 /* scroll to last entry */
1328 for (i
= 0; e
[i
].size
; i
++)
1331 rv
= total_blocks(&dl
->disk
) - e
[i
].start
;
1338 static int is_spare(struct imsm_disk
*disk
)
1340 return (disk
->status
& SPARE_DISK
) == SPARE_DISK
;
1343 static int is_configured(struct imsm_disk
*disk
)
1345 return (disk
->status
& CONFIGURED_DISK
) == CONFIGURED_DISK
;
1348 static int is_failed(struct imsm_disk
*disk
)
1350 return (disk
->status
& FAILED_DISK
) == FAILED_DISK
;
1353 static int is_journal(struct imsm_disk
*disk
)
1355 return (disk
->status
& JOURNAL_DISK
) == JOURNAL_DISK
;
1358 /* round array size down to closest MB and ensure it splits evenly
1361 static unsigned long long round_size_to_mb(unsigned long long size
, unsigned int
1365 size
= (size
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
1371 static int able_to_resync(int raid_level
, int missing_disks
)
1373 int max_missing_disks
= 0;
1375 switch (raid_level
) {
1377 max_missing_disks
= 1;
1380 max_missing_disks
= 0;
1382 return missing_disks
<= max_missing_disks
;
1385 /* try to determine how much space is reserved for metadata from
1386 * the last get_extents() entry on the smallest active disk,
1387 * otherwise fallback to the default
1389 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
)
1393 unsigned long long min_active
;
1395 __u32 rv
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1396 struct dl
*dl
, *dl_min
= NULL
;
1402 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
1405 unsigned long long blocks
= total_blocks(&dl
->disk
);
1406 if (blocks
< min_active
|| min_active
== 0) {
1408 min_active
= blocks
;
1414 /* find last lba used by subarrays on the smallest active disk */
1415 e
= get_extents(super
, dl_min
);
1418 for (i
= 0; e
[i
].size
; i
++)
1421 remainder
= min_active
- e
[i
].start
;
1424 /* to give priority to recovery we should not require full
1425 IMSM_RESERVED_SECTORS from the spare */
1426 rv
= MPB_SECTOR_CNT
+ NUM_BLOCKS_DIRTY_STRIPE_REGION
;
1428 /* if real reservation is smaller use that value */
1429 return (remainder
< rv
) ? remainder
: rv
;
1433 * Return minimum size of a spare and sector size
1434 * that can be used in this array
1436 int get_spare_criteria_imsm(struct supertype
*st
, struct spare_criteria
*c
)
1438 struct intel_super
*super
= st
->sb
;
1442 unsigned long long size
= 0;
1449 /* find first active disk in array */
1451 while (dl
&& (is_failed(&dl
->disk
) || dl
->index
== -1))
1455 /* find last lba used by subarrays */
1456 e
= get_extents(super
, dl
);
1459 for (i
= 0; e
[i
].size
; i
++)
1462 size
= e
[i
-1].start
+ e
[i
-1].size
;
1465 /* add the amount of space needed for metadata */
1466 size
+= imsm_min_reserved_sectors(super
);
1468 c
->min_size
= size
* 512;
1469 c
->sector_size
= super
->sector_size
;
1474 static int is_gen_migration(struct imsm_dev
*dev
);
1476 #define IMSM_4K_DIV 8
1478 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
1479 struct imsm_dev
*dev
);
1481 static void print_imsm_dev(struct intel_super
*super
,
1482 struct imsm_dev
*dev
,
1488 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1489 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
1493 printf("[%.16s]:\n", dev
->volume
);
1494 printf(" UUID : %s\n", uuid
);
1495 printf(" RAID Level : %d", get_imsm_raid_level(map
));
1497 printf(" <-- %d", get_imsm_raid_level(map2
));
1499 printf(" Members : %d", map
->num_members
);
1501 printf(" <-- %d", map2
->num_members
);
1503 printf(" Slots : [");
1504 for (i
= 0; i
< map
->num_members
; i
++) {
1505 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
1506 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1511 for (i
= 0; i
< map2
->num_members
; i
++) {
1512 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_1
);
1513 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1518 printf(" Failed disk : ");
1519 if (map
->failed_disk_num
== 0xff)
1522 printf("%i", map
->failed_disk_num
);
1524 slot
= get_imsm_disk_slot(map
, disk_idx
);
1526 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
1527 printf(" This Slot : %d%s\n", slot
,
1528 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
1530 printf(" This Slot : ?\n");
1531 printf(" Sector Size : %u\n", super
->sector_size
);
1532 sz
= imsm_dev_size(dev
);
1533 printf(" Array Size : %llu%s\n",
1534 (unsigned long long)sz
* 512 / super
->sector_size
,
1535 human_size(sz
* 512));
1536 sz
= blocks_per_member(map
);
1537 printf(" Per Dev Size : %llu%s\n",
1538 (unsigned long long)sz
* 512 / super
->sector_size
,
1539 human_size(sz
* 512));
1540 printf(" Sector Offset : %llu\n",
1542 printf(" Num Stripes : %llu\n",
1543 num_data_stripes(map
));
1544 printf(" Chunk Size : %u KiB",
1545 __le16_to_cpu(map
->blocks_per_strip
) / 2);
1547 printf(" <-- %u KiB",
1548 __le16_to_cpu(map2
->blocks_per_strip
) / 2);
1550 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
1551 printf(" Migrate State : ");
1552 if (dev
->vol
.migr_state
) {
1553 if (migr_type(dev
) == MIGR_INIT
)
1554 printf("initialize\n");
1555 else if (migr_type(dev
) == MIGR_REBUILD
)
1556 printf("rebuild\n");
1557 else if (migr_type(dev
) == MIGR_VERIFY
)
1559 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
1560 printf("general migration\n");
1561 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
1562 printf("state change\n");
1563 else if (migr_type(dev
) == MIGR_REPAIR
)
1566 printf("<unknown:%d>\n", migr_type(dev
));
1569 printf(" Map State : %s", map_state_str
[map
->map_state
]);
1570 if (dev
->vol
.migr_state
) {
1571 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
1573 printf(" <-- %s", map_state_str
[map
->map_state
]);
1574 printf("\n Checkpoint : %u ",
1575 __le32_to_cpu(dev
->vol
.curr_migr_unit
));
1576 if (is_gen_migration(dev
) && (slot
> 1 || slot
< 0))
1579 printf("(%llu)", (unsigned long long)
1580 blocks_per_migr_unit(super
, dev
));
1583 printf(" Dirty State : %s\n", (dev
->vol
.dirty
& RAIDVOL_DIRTY
) ?
1585 printf(" RWH Policy : ");
1586 if (dev
->rwh_policy
== RWH_OFF
|| dev
->rwh_policy
== RWH_MULTIPLE_OFF
)
1588 else if (dev
->rwh_policy
== RWH_DISTRIBUTED
)
1589 printf("PPL distributed\n");
1590 else if (dev
->rwh_policy
== RWH_JOURNALING_DRIVE
)
1591 printf("PPL journaling drive\n");
1592 else if (dev
->rwh_policy
== RWH_MULTIPLE_DISTRIBUTED
)
1593 printf("Multiple distributed PPLs\n");
1594 else if (dev
->rwh_policy
== RWH_MULTIPLE_PPLS_JOURNALING_DRIVE
)
1595 printf("Multiple PPLs on journaling drive\n");
1597 printf("<unknown:%d>\n", dev
->rwh_policy
);
1600 static void print_imsm_disk(struct imsm_disk
*disk
,
1603 unsigned int sector_size
) {
1604 char str
[MAX_RAID_SERIAL_LEN
+ 1];
1607 if (index
< -1 || !disk
)
1611 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
1613 printf(" Disk%02d Serial : %s\n", index
, str
);
1615 printf(" Disk Serial : %s\n", str
);
1616 printf(" State :%s%s%s%s\n", is_spare(disk
) ? " spare" : "",
1617 is_configured(disk
) ? " active" : "",
1618 is_failed(disk
) ? " failed" : "",
1619 is_journal(disk
) ? " journal" : "");
1620 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
1621 sz
= total_blocks(disk
) - reserved
;
1622 printf(" Usable Size : %llu%s\n",
1623 (unsigned long long)sz
* 512 / sector_size
,
1624 human_size(sz
* 512));
1627 void convert_to_4k_imsm_migr_rec(struct intel_super
*super
)
1629 struct migr_record
*migr_rec
= super
->migr_rec
;
1631 migr_rec
->blocks_per_unit
/= IMSM_4K_DIV
;
1632 migr_rec
->ckpt_area_pba
/= IMSM_4K_DIV
;
1633 migr_rec
->dest_1st_member_lba
/= IMSM_4K_DIV
;
1634 migr_rec
->dest_depth_per_unit
/= IMSM_4K_DIV
;
1635 split_ull((join_u32(migr_rec
->post_migr_vol_cap
,
1636 migr_rec
->post_migr_vol_cap_hi
) / IMSM_4K_DIV
),
1637 &migr_rec
->post_migr_vol_cap
, &migr_rec
->post_migr_vol_cap_hi
);
1640 void convert_to_4k_imsm_disk(struct imsm_disk
*disk
)
1642 set_total_blocks(disk
, (total_blocks(disk
)/IMSM_4K_DIV
));
1645 void convert_to_4k(struct intel_super
*super
)
1647 struct imsm_super
*mpb
= super
->anchor
;
1648 struct imsm_disk
*disk
;
1650 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
1652 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1653 disk
= __get_imsm_disk(mpb
, i
);
1655 convert_to_4k_imsm_disk(disk
);
1657 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1658 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1659 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1661 set_imsm_dev_size(dev
, imsm_dev_size(dev
)/IMSM_4K_DIV
);
1662 dev
->vol
.curr_migr_unit
/= IMSM_4K_DIV
;
1665 set_blocks_per_member(map
, blocks_per_member(map
)/IMSM_4K_DIV
);
1666 map
->blocks_per_strip
/= IMSM_4K_DIV
;
1667 set_pba_of_lba0(map
, pba_of_lba0(map
)/IMSM_4K_DIV
);
1669 if (dev
->vol
.migr_state
) {
1671 map
= get_imsm_map(dev
, MAP_1
);
1672 set_blocks_per_member(map
,
1673 blocks_per_member(map
)/IMSM_4K_DIV
);
1674 map
->blocks_per_strip
/= IMSM_4K_DIV
;
1675 set_pba_of_lba0(map
, pba_of_lba0(map
)/IMSM_4K_DIV
);
1679 struct bbm_log
*log
= (void *)mpb
+
1680 __le32_to_cpu(mpb
->mpb_size
) - bbm_log_size
;
1683 for (i
= 0; i
< log
->entry_count
; i
++) {
1684 struct bbm_log_entry
*entry
=
1685 &log
->marked_block_entries
[i
];
1687 __u8 count
= entry
->marked_count
+ 1;
1688 unsigned long long sector
=
1689 __le48_to_cpu(&entry
->defective_block_start
);
1691 entry
->defective_block_start
=
1692 __cpu_to_le48(sector
/IMSM_4K_DIV
);
1693 entry
->marked_count
= max(count
/IMSM_4K_DIV
, 1) - 1;
1697 mpb
->check_sum
= __gen_imsm_checksum(mpb
);
1700 void examine_migr_rec_imsm(struct intel_super
*super
)
1702 struct migr_record
*migr_rec
= super
->migr_rec
;
1703 struct imsm_super
*mpb
= super
->anchor
;
1706 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1707 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1708 struct imsm_map
*map
;
1711 if (is_gen_migration(dev
) == 0)
1714 printf("\nMigration Record Information:");
1716 /* first map under migration */
1717 map
= get_imsm_map(dev
, MAP_0
);
1719 slot
= get_imsm_disk_slot(map
, super
->disks
->index
);
1720 if (map
== NULL
|| slot
> 1 || slot
< 0) {
1721 printf(" Empty\n ");
1722 printf("Examine one of first two disks in array\n");
1725 printf("\n Status : ");
1726 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
1729 printf("Contains Data\n");
1730 printf(" Current Unit : %u\n",
1731 __le32_to_cpu(migr_rec
->curr_migr_unit
));
1732 printf(" Family : %u\n",
1733 __le32_to_cpu(migr_rec
->family_num
));
1734 printf(" Ascending : %u\n",
1735 __le32_to_cpu(migr_rec
->ascending_migr
));
1736 printf(" Blocks Per Unit : %u\n",
1737 __le32_to_cpu(migr_rec
->blocks_per_unit
));
1738 printf(" Dest. Depth Per Unit : %u\n",
1739 __le32_to_cpu(migr_rec
->dest_depth_per_unit
));
1740 printf(" Checkpoint Area pba : %u\n",
1741 __le32_to_cpu(migr_rec
->ckpt_area_pba
));
1742 printf(" First member lba : %u\n",
1743 __le32_to_cpu(migr_rec
->dest_1st_member_lba
));
1744 printf(" Total Number of Units : %u\n",
1745 __le32_to_cpu(migr_rec
->num_migr_units
));
1746 printf(" Size of volume : %u\n",
1747 __le32_to_cpu(migr_rec
->post_migr_vol_cap
));
1748 printf(" Expansion space for LBA64 : %u\n",
1749 __le32_to_cpu(migr_rec
->post_migr_vol_cap_hi
));
1750 printf(" Record was read from : %u\n",
1751 __le32_to_cpu(migr_rec
->ckpt_read_disk_num
));
1757 void convert_from_4k_imsm_migr_rec(struct intel_super
*super
)
1759 struct migr_record
*migr_rec
= super
->migr_rec
;
1761 migr_rec
->blocks_per_unit
*= IMSM_4K_DIV
;
1762 migr_rec
->ckpt_area_pba
*= IMSM_4K_DIV
;
1763 migr_rec
->dest_1st_member_lba
*= IMSM_4K_DIV
;
1764 migr_rec
->dest_depth_per_unit
*= IMSM_4K_DIV
;
1765 split_ull((join_u32(migr_rec
->post_migr_vol_cap
,
1766 migr_rec
->post_migr_vol_cap_hi
) * IMSM_4K_DIV
),
1767 &migr_rec
->post_migr_vol_cap
,
1768 &migr_rec
->post_migr_vol_cap_hi
);
1771 void convert_from_4k(struct intel_super
*super
)
1773 struct imsm_super
*mpb
= super
->anchor
;
1774 struct imsm_disk
*disk
;
1776 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
1778 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1779 disk
= __get_imsm_disk(mpb
, i
);
1781 set_total_blocks(disk
, (total_blocks(disk
)*IMSM_4K_DIV
));
1784 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1785 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1786 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1788 set_imsm_dev_size(dev
, imsm_dev_size(dev
)*IMSM_4K_DIV
);
1789 dev
->vol
.curr_migr_unit
*= IMSM_4K_DIV
;
1792 set_blocks_per_member(map
, blocks_per_member(map
)*IMSM_4K_DIV
);
1793 map
->blocks_per_strip
*= IMSM_4K_DIV
;
1794 set_pba_of_lba0(map
, pba_of_lba0(map
)*IMSM_4K_DIV
);
1796 if (dev
->vol
.migr_state
) {
1798 map
= get_imsm_map(dev
, MAP_1
);
1799 set_blocks_per_member(map
,
1800 blocks_per_member(map
)*IMSM_4K_DIV
);
1801 map
->blocks_per_strip
*= IMSM_4K_DIV
;
1802 set_pba_of_lba0(map
, pba_of_lba0(map
)*IMSM_4K_DIV
);
1806 struct bbm_log
*log
= (void *)mpb
+
1807 __le32_to_cpu(mpb
->mpb_size
) - bbm_log_size
;
1810 for (i
= 0; i
< log
->entry_count
; i
++) {
1811 struct bbm_log_entry
*entry
=
1812 &log
->marked_block_entries
[i
];
1814 __u8 count
= entry
->marked_count
+ 1;
1815 unsigned long long sector
=
1816 __le48_to_cpu(&entry
->defective_block_start
);
1818 entry
->defective_block_start
=
1819 __cpu_to_le48(sector
*IMSM_4K_DIV
);
1820 entry
->marked_count
= count
*IMSM_4K_DIV
- 1;
1824 mpb
->check_sum
= __gen_imsm_checksum(mpb
);
1827 /*******************************************************************************
1828 * function: imsm_check_attributes
1829 * Description: Function checks if features represented by attributes flags
1830 * are supported by mdadm.
1832 * attributes - Attributes read from metadata
1834 * 0 - passed attributes contains unsupported features flags
1835 * 1 - all features are supported
1836 ******************************************************************************/
1837 static int imsm_check_attributes(__u32 attributes
)
1840 __u32 not_supported
= MPB_ATTRIB_SUPPORTED
^0xffffffff;
1842 not_supported
&= ~MPB_ATTRIB_IGNORED
;
1844 not_supported
&= attributes
;
1845 if (not_supported
) {
1846 pr_err("(IMSM): Unsupported attributes : %x\n",
1847 (unsigned)__le32_to_cpu(not_supported
));
1848 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1849 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
1850 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1852 if (not_supported
& MPB_ATTRIB_2TB
) {
1853 dprintf("\t\tMPB_ATTRIB_2TB\n");
1854 not_supported
^= MPB_ATTRIB_2TB
;
1856 if (not_supported
& MPB_ATTRIB_RAID0
) {
1857 dprintf("\t\tMPB_ATTRIB_RAID0\n");
1858 not_supported
^= MPB_ATTRIB_RAID0
;
1860 if (not_supported
& MPB_ATTRIB_RAID1
) {
1861 dprintf("\t\tMPB_ATTRIB_RAID1\n");
1862 not_supported
^= MPB_ATTRIB_RAID1
;
1864 if (not_supported
& MPB_ATTRIB_RAID10
) {
1865 dprintf("\t\tMPB_ATTRIB_RAID10\n");
1866 not_supported
^= MPB_ATTRIB_RAID10
;
1868 if (not_supported
& MPB_ATTRIB_RAID1E
) {
1869 dprintf("\t\tMPB_ATTRIB_RAID1E\n");
1870 not_supported
^= MPB_ATTRIB_RAID1E
;
1872 if (not_supported
& MPB_ATTRIB_RAID5
) {
1873 dprintf("\t\tMPB_ATTRIB_RAID5\n");
1874 not_supported
^= MPB_ATTRIB_RAID5
;
1876 if (not_supported
& MPB_ATTRIB_RAIDCNG
) {
1877 dprintf("\t\tMPB_ATTRIB_RAIDCNG\n");
1878 not_supported
^= MPB_ATTRIB_RAIDCNG
;
1880 if (not_supported
& MPB_ATTRIB_BBM
) {
1881 dprintf("\t\tMPB_ATTRIB_BBM\n");
1882 not_supported
^= MPB_ATTRIB_BBM
;
1884 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1885 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n");
1886 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1888 if (not_supported
& MPB_ATTRIB_EXP_STRIPE_SIZE
) {
1889 dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n");
1890 not_supported
^= MPB_ATTRIB_EXP_STRIPE_SIZE
;
1892 if (not_supported
& MPB_ATTRIB_2TB_DISK
) {
1893 dprintf("\t\tMPB_ATTRIB_2TB_DISK\n");
1894 not_supported
^= MPB_ATTRIB_2TB_DISK
;
1896 if (not_supported
& MPB_ATTRIB_NEVER_USE2
) {
1897 dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n");
1898 not_supported
^= MPB_ATTRIB_NEVER_USE2
;
1900 if (not_supported
& MPB_ATTRIB_NEVER_USE
) {
1901 dprintf("\t\tMPB_ATTRIB_NEVER_USE\n");
1902 not_supported
^= MPB_ATTRIB_NEVER_USE
;
1906 dprintf("(IMSM): Unknown attributes : %x\n", not_supported
);
1914 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
);
1916 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
1918 struct intel_super
*super
= st
->sb
;
1919 struct imsm_super
*mpb
= super
->anchor
;
1920 char str
[MAX_SIGNATURE_LENGTH
];
1925 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
1928 strncpy(str
, (char *)mpb
->sig
, MPB_SIG_LEN
);
1929 str
[MPB_SIG_LEN
-1] = '\0';
1930 printf(" Magic : %s\n", str
);
1931 printf(" Version : %s\n", get_imsm_version(mpb
));
1932 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
1933 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
1934 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
1935 printf(" Attributes : ");
1936 if (imsm_check_attributes(mpb
->attributes
))
1937 printf("All supported\n");
1939 printf("not supported\n");
1940 getinfo_super_imsm(st
, &info
, NULL
);
1941 fname_from_uuid(st
, &info
, nbuf
, ':');
1942 printf(" UUID : %s\n", nbuf
+ 5);
1943 sum
= __le32_to_cpu(mpb
->check_sum
);
1944 printf(" Checksum : %08x %s\n", sum
,
1945 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
1946 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
, super
->sector_size
));
1947 printf(" Disks : %d\n", mpb
->num_disks
);
1948 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
1949 print_imsm_disk(__get_imsm_disk(mpb
, super
->disks
->index
),
1950 super
->disks
->index
, reserved
, super
->sector_size
);
1951 if (get_imsm_bbm_log_size(super
->bbm_log
)) {
1952 struct bbm_log
*log
= super
->bbm_log
;
1955 printf("Bad Block Management Log:\n");
1956 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
1957 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
1958 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
1960 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1962 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1964 super
->current_vol
= i
;
1965 getinfo_super_imsm(st
, &info
, NULL
);
1966 fname_from_uuid(st
, &info
, nbuf
, ':');
1967 print_imsm_dev(super
, dev
, nbuf
+ 5, super
->disks
->index
);
1969 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1970 if (i
== super
->disks
->index
)
1972 print_imsm_disk(__get_imsm_disk(mpb
, i
), i
, reserved
,
1973 super
->sector_size
);
1976 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
1977 if (dl
->index
== -1)
1978 print_imsm_disk(&dl
->disk
, -1, reserved
,
1979 super
->sector_size
);
1981 examine_migr_rec_imsm(super
);
1984 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
1986 /* We just write a generic IMSM ARRAY entry */
1989 struct intel_super
*super
= st
->sb
;
1991 if (!super
->anchor
->num_raid_devs
) {
1992 printf("ARRAY metadata=imsm\n");
1996 getinfo_super_imsm(st
, &info
, NULL
);
1997 fname_from_uuid(st
, &info
, nbuf
, ':');
1998 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
2001 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
2003 /* We just write a generic IMSM ARRAY entry */
2007 struct intel_super
*super
= st
->sb
;
2010 if (!super
->anchor
->num_raid_devs
)
2013 getinfo_super_imsm(st
, &info
, NULL
);
2014 fname_from_uuid(st
, &info
, nbuf
, ':');
2015 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
2016 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
2018 super
->current_vol
= i
;
2019 getinfo_super_imsm(st
, &info
, NULL
);
2020 fname_from_uuid(st
, &info
, nbuf1
, ':');
2021 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
2022 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
2026 static void export_examine_super_imsm(struct supertype
*st
)
2028 struct intel_super
*super
= st
->sb
;
2029 struct imsm_super
*mpb
= super
->anchor
;
2033 getinfo_super_imsm(st
, &info
, NULL
);
2034 fname_from_uuid(st
, &info
, nbuf
, ':');
2035 printf("MD_METADATA=imsm\n");
2036 printf("MD_LEVEL=container\n");
2037 printf("MD_UUID=%s\n", nbuf
+5);
2038 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
2041 static int copy_metadata_imsm(struct supertype
*st
, int from
, int to
)
2043 /* The second last sector of the device contains
2044 * the "struct imsm_super" metadata.
2045 * This contains mpb_size which is the size in bytes of the
2046 * extended metadata. This is located immediately before
2048 * We want to read all that, plus the last sector which
2049 * may contain a migration record, and write it all
2053 unsigned long long dsize
, offset
;
2055 struct imsm_super
*sb
;
2056 struct intel_super
*super
= st
->sb
;
2057 unsigned int sector_size
= super
->sector_size
;
2058 unsigned int written
= 0;
2060 if (posix_memalign(&buf
, MAX_SECTOR_SIZE
, MAX_SECTOR_SIZE
) != 0)
2063 if (!get_dev_size(from
, NULL
, &dsize
))
2066 if (lseek64(from
, dsize
-(2*sector_size
), 0) < 0)
2068 if ((unsigned int)read(from
, buf
, sector_size
) != sector_size
)
2071 if (strncmp((char*)sb
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0)
2074 sectors
= mpb_sectors(sb
, sector_size
) + 2;
2075 offset
= dsize
- sectors
* sector_size
;
2076 if (lseek64(from
, offset
, 0) < 0 ||
2077 lseek64(to
, offset
, 0) < 0)
2079 while (written
< sectors
* sector_size
) {
2080 int n
= sectors
*sector_size
- written
;
2083 if (read(from
, buf
, n
) != n
)
2085 if (write(to
, buf
, n
) != n
)
2096 static void detail_super_imsm(struct supertype
*st
, char *homehost
)
2101 getinfo_super_imsm(st
, &info
, NULL
);
2102 fname_from_uuid(st
, &info
, nbuf
, ':');
2103 printf("\n UUID : %s\n", nbuf
+ 5);
2106 static void brief_detail_super_imsm(struct supertype
*st
)
2110 getinfo_super_imsm(st
, &info
, NULL
);
2111 fname_from_uuid(st
, &info
, nbuf
, ':');
2112 printf(" UUID=%s", nbuf
+ 5);
2115 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
);
2116 static void fd2devname(int fd
, char *name
);
2118 static int ahci_enumerate_ports(const char *hba_path
, int port_count
, int host_base
, int verbose
)
2120 /* dump an unsorted list of devices attached to AHCI Intel storage
2121 * controller, as well as non-connected ports
2123 int hba_len
= strlen(hba_path
) + 1;
2128 unsigned long port_mask
= (1 << port_count
) - 1;
2130 if (port_count
> (int)sizeof(port_mask
) * 8) {
2132 pr_err("port_count %d out of range\n", port_count
);
2136 /* scroll through /sys/dev/block looking for devices attached to
2139 dir
= opendir("/sys/dev/block");
2143 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2154 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
2156 path
= devt_to_devpath(makedev(major
, minor
));
2159 if (!path_attached_to_hba(path
, hba_path
)) {
2165 /* retrieve the scsi device type */
2166 if (asprintf(&device
, "/sys/dev/block/%d:%d/device/xxxxxxx", major
, minor
) < 0) {
2168 pr_err("failed to allocate 'device'\n");
2172 sprintf(device
, "/sys/dev/block/%d:%d/device/type", major
, minor
);
2173 if (load_sys(device
, buf
, sizeof(buf
)) != 0) {
2175 pr_err("failed to read device type for %s\n",
2181 type
= strtoul(buf
, NULL
, 10);
2183 /* if it's not a disk print the vendor and model */
2184 if (!(type
== 0 || type
== 7 || type
== 14)) {
2187 sprintf(device
, "/sys/dev/block/%d:%d/device/vendor", major
, minor
);
2188 if (load_sys(device
, buf
, sizeof(buf
)) == 0) {
2189 strncpy(vendor
, buf
, sizeof(vendor
));
2190 vendor
[sizeof(vendor
) - 1] = '\0';
2191 c
= (char *) &vendor
[sizeof(vendor
) - 1];
2192 while (isspace(*c
) || *c
== '\0')
2196 sprintf(device
, "/sys/dev/block/%d:%d/device/model", major
, minor
);
2197 if (load_sys(device
, buf
, sizeof(buf
)) == 0) {
2198 strncpy(model
, buf
, sizeof(model
));
2199 model
[sizeof(model
) - 1] = '\0';
2200 c
= (char *) &model
[sizeof(model
) - 1];
2201 while (isspace(*c
) || *c
== '\0')
2205 if (vendor
[0] && model
[0])
2206 sprintf(buf
, "%.64s %.64s", vendor
, model
);
2208 switch (type
) { /* numbers from hald/linux/device.c */
2209 case 1: sprintf(buf
, "tape"); break;
2210 case 2: sprintf(buf
, "printer"); break;
2211 case 3: sprintf(buf
, "processor"); break;
2213 case 5: sprintf(buf
, "cdrom"); break;
2214 case 6: sprintf(buf
, "scanner"); break;
2215 case 8: sprintf(buf
, "media_changer"); break;
2216 case 9: sprintf(buf
, "comm"); break;
2217 case 12: sprintf(buf
, "raid"); break;
2218 default: sprintf(buf
, "unknown");
2224 /* chop device path to 'host%d' and calculate the port number */
2225 c
= strchr(&path
[hba_len
], '/');
2228 pr_err("%s - invalid path name\n", path
+ hba_len
);
2233 if ((sscanf(&path
[hba_len
], "ata%d", &port
) == 1) ||
2234 ((sscanf(&path
[hba_len
], "host%d", &port
) == 1)))
2238 *c
= '/'; /* repair the full string */
2239 pr_err("failed to determine port number for %s\n",
2246 /* mark this port as used */
2247 port_mask
&= ~(1 << port
);
2249 /* print out the device information */
2251 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
2255 fd
= dev_open(ent
->d_name
, O_RDONLY
);
2257 printf(" Port%d : - disk info unavailable -\n", port
);
2259 fd2devname(fd
, buf
);
2260 printf(" Port%d : %s", port
, buf
);
2261 if (imsm_read_serial(fd
, NULL
, (__u8
*) buf
) == 0)
2262 printf(" (%.*s)\n", MAX_RAID_SERIAL_LEN
, buf
);
2277 for (i
= 0; i
< port_count
; i
++)
2278 if (port_mask
& (1 << i
))
2279 printf(" Port%d : - no device attached -\n", i
);
2285 static int print_vmd_attached_devs(struct sys_dev
*hba
)
2293 if (hba
->type
!= SYS_DEV_VMD
)
2296 /* scroll through /sys/dev/block looking for devices attached to
2299 dir
= opendir("/sys/bus/pci/drivers/nvme");
2303 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2306 /* is 'ent' a device? check that the 'subsystem' link exists and
2307 * that its target matches 'bus'
2309 sprintf(path
, "/sys/bus/pci/drivers/nvme/%s/subsystem",
2311 n
= readlink(path
, link
, sizeof(link
));
2312 if (n
< 0 || n
>= (int)sizeof(link
))
2315 c
= strrchr(link
, '/');
2318 if (strncmp("pci", c
+1, strlen("pci")) != 0)
2321 sprintf(path
, "/sys/bus/pci/drivers/nvme/%s", ent
->d_name
);
2323 rp
= realpath(path
, NULL
);
2327 if (path_attached_to_hba(rp
, hba
->path
)) {
2328 printf(" NVMe under VMD : %s\n", rp
);
2337 static void print_found_intel_controllers(struct sys_dev
*elem
)
2339 for (; elem
; elem
= elem
->next
) {
2340 pr_err("found Intel(R) ");
2341 if (elem
->type
== SYS_DEV_SATA
)
2342 fprintf(stderr
, "SATA ");
2343 else if (elem
->type
== SYS_DEV_SAS
)
2344 fprintf(stderr
, "SAS ");
2345 else if (elem
->type
== SYS_DEV_NVME
)
2346 fprintf(stderr
, "NVMe ");
2348 if (elem
->type
== SYS_DEV_VMD
)
2349 fprintf(stderr
, "VMD domain");
2351 fprintf(stderr
, "RAID controller");
2354 fprintf(stderr
, " at %s", elem
->pci_id
);
2355 fprintf(stderr
, ".\n");
2360 static int ahci_get_port_count(const char *hba_path
, int *port_count
)
2367 if ((dir
= opendir(hba_path
)) == NULL
)
2370 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2373 if ((sscanf(ent
->d_name
, "ata%d", &host
) != 1) &&
2374 ((sscanf(ent
->d_name
, "host%d", &host
) != 1)))
2376 if (*port_count
== 0)
2378 else if (host
< host_base
)
2381 if (host
+ 1 > *port_count
+ host_base
)
2382 *port_count
= host
+ 1 - host_base
;
2388 static void print_imsm_capability(const struct imsm_orom
*orom
)
2390 printf(" Platform : Intel(R) ");
2391 if (orom
->capabilities
== 0 && orom
->driver_features
== 0)
2392 printf("Matrix Storage Manager\n");
2393 else if (imsm_orom_is_enterprise(orom
) && orom
->major_ver
>= 6)
2394 printf("Virtual RAID on CPU\n");
2396 printf("Rapid Storage Technology%s\n",
2397 imsm_orom_is_enterprise(orom
) ? " enterprise" : "");
2398 if (orom
->major_ver
|| orom
->minor_ver
|| orom
->hotfix_ver
|| orom
->build
)
2399 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
,
2400 orom
->minor_ver
, orom
->hotfix_ver
, orom
->build
);
2401 printf(" RAID Levels :%s%s%s%s%s\n",
2402 imsm_orom_has_raid0(orom
) ? " raid0" : "",
2403 imsm_orom_has_raid1(orom
) ? " raid1" : "",
2404 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
2405 imsm_orom_has_raid10(orom
) ? " raid10" : "",
2406 imsm_orom_has_raid5(orom
) ? " raid5" : "");
2407 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2408 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
2409 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
2410 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
2411 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
2412 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
2413 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
2414 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
2415 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
2416 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
2417 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
2418 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
2419 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
2420 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
2421 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
2422 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
2423 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
2424 printf(" 2TB volumes :%s supported\n",
2425 (orom
->attr
& IMSM_OROM_ATTR_2TB
)?"":" not");
2426 printf(" 2TB disks :%s supported\n",
2427 (orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
)?"":" not");
2428 printf(" Max Disks : %d\n", orom
->tds
);
2429 printf(" Max Volumes : %d per array, %d per %s\n",
2430 orom
->vpa
, orom
->vphba
,
2431 imsm_orom_is_nvme(orom
) ? "platform" : "controller");
2435 static void print_imsm_capability_export(const struct imsm_orom
*orom
)
2437 printf("MD_FIRMWARE_TYPE=imsm\n");
2438 if (orom
->major_ver
|| orom
->minor_ver
|| orom
->hotfix_ver
|| orom
->build
)
2439 printf("IMSM_VERSION=%d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
2440 orom
->hotfix_ver
, orom
->build
);
2441 printf("IMSM_SUPPORTED_RAID_LEVELS=%s%s%s%s%s\n",
2442 imsm_orom_has_raid0(orom
) ? "raid0 " : "",
2443 imsm_orom_has_raid1(orom
) ? "raid1 " : "",
2444 imsm_orom_has_raid1e(orom
) ? "raid1e " : "",
2445 imsm_orom_has_raid5(orom
) ? "raid10 " : "",
2446 imsm_orom_has_raid10(orom
) ? "raid5 " : "");
2447 printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2448 imsm_orom_has_chunk(orom
, 2) ? "2k " : "",
2449 imsm_orom_has_chunk(orom
, 4) ? "4k " : "",
2450 imsm_orom_has_chunk(orom
, 8) ? "8k " : "",
2451 imsm_orom_has_chunk(orom
, 16) ? "16k " : "",
2452 imsm_orom_has_chunk(orom
, 32) ? "32k " : "",
2453 imsm_orom_has_chunk(orom
, 64) ? "64k " : "",
2454 imsm_orom_has_chunk(orom
, 128) ? "128k " : "",
2455 imsm_orom_has_chunk(orom
, 256) ? "256k " : "",
2456 imsm_orom_has_chunk(orom
, 512) ? "512k " : "",
2457 imsm_orom_has_chunk(orom
, 1024*1) ? "1M " : "",
2458 imsm_orom_has_chunk(orom
, 1024*2) ? "2M " : "",
2459 imsm_orom_has_chunk(orom
, 1024*4) ? "4M " : "",
2460 imsm_orom_has_chunk(orom
, 1024*8) ? "8M " : "",
2461 imsm_orom_has_chunk(orom
, 1024*16) ? "16M " : "",
2462 imsm_orom_has_chunk(orom
, 1024*32) ? "32M " : "",
2463 imsm_orom_has_chunk(orom
, 1024*64) ? "64M " : "");
2464 printf("IMSM_2TB_VOLUMES=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB
) ? "yes" : "no");
2465 printf("IMSM_2TB_DISKS=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) ? "yes" : "no");
2466 printf("IMSM_MAX_DISKS=%d\n",orom
->tds
);
2467 printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom
->vpa
);
2468 printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom
->vphba
);
2471 static int detail_platform_imsm(int verbose
, int enumerate_only
, char *controller_path
)
2473 /* There are two components to imsm platform support, the ahci SATA
2474 * controller and the option-rom. To find the SATA controller we
2475 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
2476 * controller with the Intel vendor id is present. This approach
2477 * allows mdadm to leverage the kernel's ahci detection logic, with the
2478 * caveat that if ahci.ko is not loaded mdadm will not be able to
2479 * detect platform raid capabilities. The option-rom resides in a
2480 * platform "Adapter ROM". We scan for its signature to retrieve the
2481 * platform capabilities. If raid support is disabled in the BIOS the
2482 * option-rom capability structure will not be available.
2484 struct sys_dev
*list
, *hba
;
2489 if (enumerate_only
) {
2490 if (check_env("IMSM_NO_PLATFORM"))
2492 list
= find_intel_devices();
2495 for (hba
= list
; hba
; hba
= hba
->next
) {
2496 if (find_imsm_capability(hba
)) {
2506 list
= find_intel_devices();
2509 pr_err("no active Intel(R) RAID controller found.\n");
2511 } else if (verbose
> 0)
2512 print_found_intel_controllers(list
);
2514 for (hba
= list
; hba
; hba
= hba
->next
) {
2515 if (controller_path
&& (compare_paths(hba
->path
, controller_path
) != 0))
2517 if (!find_imsm_capability(hba
)) {
2519 pr_err("imsm capabilities not found for controller: %s (type %s)\n",
2520 hba
->type
== SYS_DEV_VMD
? vmd_domain_to_controller(hba
, buf
) : hba
->path
,
2521 get_sys_dev_type(hba
->type
));
2527 if (controller_path
&& result
== 1) {
2528 pr_err("no active Intel(R) RAID controller found under %s\n",
2533 const struct orom_entry
*entry
;
2535 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
2536 if (entry
->type
== SYS_DEV_VMD
) {
2537 print_imsm_capability(&entry
->orom
);
2538 printf(" 3rd party NVMe :%s supported\n",
2539 imsm_orom_has_tpv_support(&entry
->orom
)?"":" not");
2540 for (hba
= list
; hba
; hba
= hba
->next
) {
2541 if (hba
->type
== SYS_DEV_VMD
) {
2543 printf(" I/O Controller : %s (%s)\n",
2544 vmd_domain_to_controller(hba
, buf
), get_sys_dev_type(hba
->type
));
2545 if (print_vmd_attached_devs(hba
)) {
2547 pr_err("failed to get devices attached to VMD domain.\n");
2556 print_imsm_capability(&entry
->orom
);
2557 if (entry
->type
== SYS_DEV_NVME
) {
2558 for (hba
= list
; hba
; hba
= hba
->next
) {
2559 if (hba
->type
== SYS_DEV_NVME
)
2560 printf(" NVMe Device : %s\n", hba
->path
);
2566 struct devid_list
*devid
;
2567 for (devid
= entry
->devid_list
; devid
; devid
= devid
->next
) {
2568 hba
= device_by_id(devid
->devid
);
2572 printf(" I/O Controller : %s (%s)\n",
2573 hba
->path
, get_sys_dev_type(hba
->type
));
2574 if (hba
->type
== SYS_DEV_SATA
) {
2575 host_base
= ahci_get_port_count(hba
->path
, &port_count
);
2576 if (ahci_enumerate_ports(hba
->path
, port_count
, host_base
, verbose
)) {
2578 pr_err("failed to enumerate ports on SATA controller at %s.\n", hba
->pci_id
);
2589 static int export_detail_platform_imsm(int verbose
, char *controller_path
)
2591 struct sys_dev
*list
, *hba
;
2594 list
= find_intel_devices();
2597 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n");
2602 for (hba
= list
; hba
; hba
= hba
->next
) {
2603 if (controller_path
&& (compare_paths(hba
->path
,controller_path
) != 0))
2605 if (!find_imsm_capability(hba
) && verbose
> 0) {
2607 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n",
2608 hba
->type
== SYS_DEV_VMD
? vmd_domain_to_controller(hba
, buf
) : hba
->path
);
2614 const struct orom_entry
*entry
;
2616 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
2617 if (entry
->type
== SYS_DEV_VMD
) {
2618 for (hba
= list
; hba
; hba
= hba
->next
)
2619 print_imsm_capability_export(&entry
->orom
);
2622 print_imsm_capability_export(&entry
->orom
);
2628 static int match_home_imsm(struct supertype
*st
, char *homehost
)
2630 /* the imsm metadata format does not specify any host
2631 * identification information. We return -1 since we can never
2632 * confirm nor deny whether a given array is "meant" for this
2633 * host. We rely on compare_super and the 'family_num' fields to
2634 * exclude member disks that do not belong, and we rely on
2635 * mdadm.conf to specify the arrays that should be assembled.
2636 * Auto-assembly may still pick up "foreign" arrays.
2642 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
2644 /* The uuid returned here is used for:
2645 * uuid to put into bitmap file (Create, Grow)
2646 * uuid for backup header when saving critical section (Grow)
2647 * comparing uuids when re-adding a device into an array
2648 * In these cases the uuid required is that of the data-array,
2649 * not the device-set.
2650 * uuid to recognise same set when adding a missing device back
2651 * to an array. This is a uuid for the device-set.
2653 * For each of these we can make do with a truncated
2654 * or hashed uuid rather than the original, as long as
2656 * In each case the uuid required is that of the data-array,
2657 * not the device-set.
2659 /* imsm does not track uuid's so we synthesis one using sha1 on
2660 * - The signature (Which is constant for all imsm array, but no matter)
2661 * - the orig_family_num of the container
2662 * - the index number of the volume
2663 * - the 'serial' number of the volume.
2664 * Hopefully these are all constant.
2666 struct intel_super
*super
= st
->sb
;
2669 struct sha1_ctx ctx
;
2670 struct imsm_dev
*dev
= NULL
;
2673 /* some mdadm versions failed to set ->orig_family_num, in which
2674 * case fall back to ->family_num. orig_family_num will be
2675 * fixed up with the first metadata update.
2677 family_num
= super
->anchor
->orig_family_num
;
2678 if (family_num
== 0)
2679 family_num
= super
->anchor
->family_num
;
2680 sha1_init_ctx(&ctx
);
2681 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
2682 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
2683 if (super
->current_vol
>= 0)
2684 dev
= get_imsm_dev(super
, super
->current_vol
);
2686 __u32 vol
= super
->current_vol
;
2687 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
2688 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
2690 sha1_finish_ctx(&ctx
, buf
);
2691 memcpy(uuid
, buf
, 4*4);
2696 get_imsm_numerical_version(struct imsm_super
*mpb
, int *m
, int *p
)
2698 __u8
*v
= get_imsm_version(mpb
);
2699 __u8
*end
= mpb
->sig
+ MAX_SIGNATURE_LENGTH
;
2700 char major
[] = { 0, 0, 0 };
2701 char minor
[] = { 0 ,0, 0 };
2702 char patch
[] = { 0, 0, 0 };
2703 char *ver_parse
[] = { major
, minor
, patch
};
2707 while (*v
!= '\0' && v
< end
) {
2708 if (*v
!= '.' && j
< 2)
2709 ver_parse
[i
][j
++] = *v
;
2717 *m
= strtol(minor
, NULL
, 0);
2718 *p
= strtol(patch
, NULL
, 0);
2722 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
2724 /* migr_strip_size when repairing or initializing parity */
2725 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2726 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2728 switch (get_imsm_raid_level(map
)) {
2733 return 128*1024 >> 9;
2737 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
2739 /* migr_strip_size when rebuilding a degraded disk, no idea why
2740 * this is different than migr_strip_size_resync(), but it's good
2743 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2744 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2746 switch (get_imsm_raid_level(map
)) {
2749 if (map
->num_members
% map
->num_domains
== 0)
2750 return 128*1024 >> 9;
2754 return max((__u32
) 64*1024 >> 9, chunk
);
2756 return 128*1024 >> 9;
2760 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
2762 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2763 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2764 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
2765 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
2767 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
2770 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
2772 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2773 int level
= get_imsm_raid_level(lo
);
2775 if (level
== 1 || level
== 10) {
2776 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2778 return hi
->num_domains
;
2780 return num_stripes_per_unit_resync(dev
);
2783 static __u8
imsm_num_data_members(struct imsm_map
*map
)
2785 /* named 'imsm_' because raid0, raid1 and raid10
2786 * counter-intuitively have the same number of data disks
2788 switch (get_imsm_raid_level(map
)) {
2790 return map
->num_members
;
2794 return map
->num_members
/2;
2796 return map
->num_members
- 1;
2798 dprintf("unsupported raid level\n");
2803 static unsigned long long calc_component_size(struct imsm_map
*map
,
2804 struct imsm_dev
*dev
)
2806 unsigned long long component_size
;
2807 unsigned long long dev_size
= imsm_dev_size(dev
);
2808 unsigned long long calc_dev_size
= 0;
2809 unsigned int member_disks
= imsm_num_data_members(map
);
2811 if (member_disks
== 0)
2814 component_size
= per_dev_array_size(map
);
2815 calc_dev_size
= component_size
* member_disks
;
2817 /* Component size is rounded to 1MB so difference between size from
2818 * metadata and size calculated from num_data_stripes equals up to
2819 * 2048 blocks per each device. If the difference is higher it means
2820 * that array size was expanded and num_data_stripes was not updated.
2822 if ((unsigned int)abs(calc_dev_size
- dev_size
) >
2823 (1 << SECT_PER_MB_SHIFT
) * member_disks
) {
2824 component_size
= dev_size
/ member_disks
;
2825 dprintf("Invalid num_data_stripes in metadata; expected=%llu, found=%llu\n",
2826 component_size
/ map
->blocks_per_strip
,
2827 num_data_stripes(map
));
2830 return component_size
;
2833 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
2835 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2836 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2838 switch(get_imsm_raid_level(map
)) {
2841 return chunk
* map
->num_domains
;
2843 return chunk
* map
->num_members
;
2849 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
2851 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2852 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2853 __u32 strip
= block
/ chunk
;
2855 switch (get_imsm_raid_level(map
)) {
2858 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
2859 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
2861 return vol_stripe
* chunk
+ block
% chunk
;
2863 __u32 stripe
= strip
/ (map
->num_members
- 1);
2865 return stripe
* chunk
+ block
% chunk
;
2872 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
2873 struct imsm_dev
*dev
)
2875 /* calculate the conversion factor between per member 'blocks'
2876 * (md/{resync,rebuild}_start) and imsm migration units, return
2877 * 0 for the 'not migrating' and 'unsupported migration' cases
2879 if (!dev
->vol
.migr_state
)
2882 switch (migr_type(dev
)) {
2883 case MIGR_GEN_MIGR
: {
2884 struct migr_record
*migr_rec
= super
->migr_rec
;
2885 return __le32_to_cpu(migr_rec
->blocks_per_unit
);
2890 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2891 __u32 stripes_per_unit
;
2892 __u32 blocks_per_unit
;
2901 /* yes, this is really the translation of migr_units to
2902 * per-member blocks in the 'resync' case
2904 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
2905 migr_chunk
= migr_strip_blocks_resync(dev
);
2906 disks
= imsm_num_data_members(map
);
2907 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
2908 stripe
= __le16_to_cpu(map
->blocks_per_strip
) * disks
;
2909 segment
= blocks_per_unit
/ stripe
;
2910 block_rel
= blocks_per_unit
- segment
* stripe
;
2911 parity_depth
= parity_segment_depth(dev
);
2912 block_map
= map_migr_block(dev
, block_rel
);
2913 return block_map
+ parity_depth
* segment
;
2915 case MIGR_REBUILD
: {
2916 __u32 stripes_per_unit
;
2919 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
2920 migr_chunk
= migr_strip_blocks_rebuild(dev
);
2921 return migr_chunk
* stripes_per_unit
;
2923 case MIGR_STATE_CHANGE
:
2929 static int imsm_level_to_layout(int level
)
2937 return ALGORITHM_LEFT_ASYMMETRIC
;
2944 /*******************************************************************************
2945 * Function: read_imsm_migr_rec
2946 * Description: Function reads imsm migration record from last sector of disk
2948 * fd : disk descriptor
2949 * super : metadata info
2953 ******************************************************************************/
2954 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
)
2957 unsigned int sector_size
= super
->sector_size
;
2958 unsigned long long dsize
;
2960 get_dev_size(fd
, NULL
, &dsize
);
2961 if (lseek64(fd
, dsize
- (sector_size
*MIGR_REC_SECTOR_POSITION
),
2963 pr_err("Cannot seek to anchor block: %s\n",
2967 if ((unsigned int)read(fd
, super
->migr_rec_buf
,
2968 MIGR_REC_BUF_SECTORS
*sector_size
) !=
2969 MIGR_REC_BUF_SECTORS
*sector_size
) {
2970 pr_err("Cannot read migr record block: %s\n",
2975 if (sector_size
== 4096)
2976 convert_from_4k_imsm_migr_rec(super
);
2982 static struct imsm_dev
*imsm_get_device_during_migration(
2983 struct intel_super
*super
)
2986 struct intel_dev
*dv
;
2988 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
2989 if (is_gen_migration(dv
->dev
))
2995 /*******************************************************************************
2996 * Function: load_imsm_migr_rec
2997 * Description: Function reads imsm migration record (it is stored at the last
3000 * super : imsm internal array info
3001 * info : general array info
3005 * -2 : no migration in progress
3006 ******************************************************************************/
3007 static int load_imsm_migr_rec(struct intel_super
*super
, struct mdinfo
*info
)
3014 struct imsm_dev
*dev
;
3015 struct imsm_map
*map
;
3018 /* find map under migration */
3019 dev
= imsm_get_device_during_migration(super
);
3020 /* nothing to load,no migration in progress?
3026 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
3027 /* read only from one of the first two slots */
3028 if ((sd
->disk
.raid_disk
< 0) ||
3029 (sd
->disk
.raid_disk
> 1))
3032 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
3033 fd
= dev_open(nm
, O_RDONLY
);
3039 map
= get_imsm_map(dev
, MAP_0
);
3040 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
3041 /* skip spare and failed disks
3045 /* read only from one of the first two slots */
3047 slot
= get_imsm_disk_slot(map
, dl
->index
);
3048 if (map
== NULL
|| slot
> 1 || slot
< 0)
3050 sprintf(nm
, "%d:%d", dl
->major
, dl
->minor
);
3051 fd
= dev_open(nm
, O_RDONLY
);
3058 retval
= read_imsm_migr_rec(fd
, super
);
3066 /*******************************************************************************
3067 * function: imsm_create_metadata_checkpoint_update
3068 * Description: It creates update for checkpoint change.
3070 * super : imsm internal array info
3071 * u : pointer to prepared update
3074 * If length is equal to 0, input pointer u contains no update
3075 ******************************************************************************/
3076 static int imsm_create_metadata_checkpoint_update(
3077 struct intel_super
*super
,
3078 struct imsm_update_general_migration_checkpoint
**u
)
3081 int update_memory_size
= 0;
3083 dprintf("(enter)\n");
3089 /* size of all update data without anchor */
3090 update_memory_size
=
3091 sizeof(struct imsm_update_general_migration_checkpoint
);
3093 *u
= xcalloc(1, update_memory_size
);
3095 dprintf("error: cannot get memory\n");
3098 (*u
)->type
= update_general_migration_checkpoint
;
3099 (*u
)->curr_migr_unit
= __le32_to_cpu(super
->migr_rec
->curr_migr_unit
);
3100 dprintf("prepared for %u\n", (*u
)->curr_migr_unit
);
3102 return update_memory_size
;
3105 static void imsm_update_metadata_locally(struct supertype
*st
,
3106 void *buf
, int len
);
3108 /*******************************************************************************
3109 * Function: write_imsm_migr_rec
3110 * Description: Function writes imsm migration record
3111 * (at the last sector of disk)
3113 * super : imsm internal array info
3117 ******************************************************************************/
3118 static int write_imsm_migr_rec(struct supertype
*st
)
3120 struct intel_super
*super
= st
->sb
;
3121 unsigned int sector_size
= super
->sector_size
;
3122 unsigned long long dsize
;
3128 struct imsm_update_general_migration_checkpoint
*u
;
3129 struct imsm_dev
*dev
;
3130 struct imsm_map
*map
;
3132 /* find map under migration */
3133 dev
= imsm_get_device_during_migration(super
);
3134 /* if no migration, write buffer anyway to clear migr_record
3135 * on disk based on first available device
3138 dev
= get_imsm_dev(super
, super
->current_vol
< 0 ? 0 :
3139 super
->current_vol
);
3141 map
= get_imsm_map(dev
, MAP_0
);
3143 if (sector_size
== 4096)
3144 convert_to_4k_imsm_migr_rec(super
);
3145 for (sd
= super
->disks
; sd
; sd
= sd
->next
) {
3148 /* skip failed and spare devices */
3151 /* write to 2 first slots only */
3153 slot
= get_imsm_disk_slot(map
, sd
->index
);
3154 if (map
== NULL
|| slot
> 1 || slot
< 0)
3157 sprintf(nm
, "%d:%d", sd
->major
, sd
->minor
);
3158 fd
= dev_open(nm
, O_RDWR
);
3161 get_dev_size(fd
, NULL
, &dsize
);
3162 if (lseek64(fd
, dsize
- (MIGR_REC_SECTOR_POSITION
*sector_size
),
3164 pr_err("Cannot seek to anchor block: %s\n",
3168 if ((unsigned int)write(fd
, super
->migr_rec_buf
,
3169 MIGR_REC_BUF_SECTORS
*sector_size
) !=
3170 MIGR_REC_BUF_SECTORS
*sector_size
) {
3171 pr_err("Cannot write migr record block: %s\n",
3178 if (sector_size
== 4096)
3179 convert_from_4k_imsm_migr_rec(super
);
3180 /* update checkpoint information in metadata */
3181 len
= imsm_create_metadata_checkpoint_update(super
, &u
);
3183 dprintf("imsm: Cannot prepare update\n");
3186 /* update metadata locally */
3187 imsm_update_metadata_locally(st
, u
, len
);
3188 /* and possibly remotely */
3189 if (st
->update_tail
) {
3190 append_metadata_update(st
, u
, len
);
3191 /* during reshape we do all work inside metadata handler
3192 * manage_reshape(), so metadata update has to be triggered
3195 flush_metadata_updates(st
);
3196 st
->update_tail
= &st
->updates
;
3207 /* spare/missing disks activations are not allowe when
3208 * array/container performs reshape operation, because
3209 * all arrays in container works on the same disks set
3211 int imsm_reshape_blocks_arrays_changes(struct intel_super
*super
)
3214 struct intel_dev
*i_dev
;
3215 struct imsm_dev
*dev
;
3217 /* check whole container
3219 for (i_dev
= super
->devlist
; i_dev
; i_dev
= i_dev
->next
) {
3221 if (is_gen_migration(dev
)) {
3222 /* No repair during any migration in container
3230 static unsigned long long imsm_component_size_alignment_check(int level
,
3232 unsigned int sector_size
,
3233 unsigned long long component_size
)
3235 unsigned int component_size_alignment
;
3237 /* check component size alignment
3239 component_size_alignment
= component_size
% (chunk_size
/sector_size
);
3241 dprintf("(Level: %i, chunk_size = %i, component_size = %llu), component_size_alignment = %u\n",
3242 level
, chunk_size
, component_size
,
3243 component_size_alignment
);
3245 if (component_size_alignment
&& (level
!= 1) && (level
!= UnSet
)) {
3246 dprintf("imsm: reported component size aligned from %llu ",
3248 component_size
-= component_size_alignment
;
3249 dprintf_cont("to %llu (%i).\n",
3250 component_size
, component_size_alignment
);
3253 return component_size
;
3256 static unsigned long long get_ppl_sector(struct intel_super
*super
, int dev_idx
)
3258 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
3259 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3261 return pba_of_lba0(map
) +
3262 (num_data_stripes(map
) * map
->blocks_per_strip
);
3265 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
3267 struct intel_super
*super
= st
->sb
;
3268 struct migr_record
*migr_rec
= super
->migr_rec
;
3269 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
3270 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3271 struct imsm_map
*prev_map
= get_imsm_map(dev
, MAP_1
);
3272 struct imsm_map
*map_to_analyse
= map
;
3274 int map_disks
= info
->array
.raid_disks
;
3276 memset(info
, 0, sizeof(*info
));
3278 map_to_analyse
= prev_map
;
3280 dl
= super
->current_disk
;
3282 info
->container_member
= super
->current_vol
;
3283 info
->array
.raid_disks
= map
->num_members
;
3284 info
->array
.level
= get_imsm_raid_level(map_to_analyse
);
3285 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
3286 info
->array
.md_minor
= -1;
3287 info
->array
.ctime
= 0;
3288 info
->array
.utime
= 0;
3289 info
->array
.chunk_size
=
3290 __le16_to_cpu(map_to_analyse
->blocks_per_strip
) << 9;
3291 info
->array
.state
= !(dev
->vol
.dirty
& RAIDVOL_DIRTY
);
3292 info
->custom_array_size
= imsm_dev_size(dev
);
3293 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
3295 if (is_gen_migration(dev
)) {
3296 info
->reshape_active
= 1;
3297 info
->new_level
= get_imsm_raid_level(map
);
3298 info
->new_layout
= imsm_level_to_layout(info
->new_level
);
3299 info
->new_chunk
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
3300 info
->delta_disks
= map
->num_members
- prev_map
->num_members
;
3301 if (info
->delta_disks
) {
3302 /* this needs to be applied to every array
3305 info
->reshape_active
= CONTAINER_RESHAPE
;
3307 /* We shape information that we give to md might have to be
3308 * modify to cope with md's requirement for reshaping arrays.
3309 * For example, when reshaping a RAID0, md requires it to be
3310 * presented as a degraded RAID4.
3311 * Also if a RAID0 is migrating to a RAID5 we need to specify
3312 * the array as already being RAID5, but the 'before' layout
3313 * is a RAID4-like layout.
3315 switch (info
->array
.level
) {
3317 switch(info
->new_level
) {
3319 /* conversion is happening as RAID4 */
3320 info
->array
.level
= 4;
3321 info
->array
.raid_disks
+= 1;
3324 /* conversion is happening as RAID5 */
3325 info
->array
.level
= 5;
3326 info
->array
.layout
= ALGORITHM_PARITY_N
;
3327 info
->delta_disks
-= 1;
3330 /* FIXME error message */
3331 info
->array
.level
= UnSet
;
3337 info
->new_level
= UnSet
;
3338 info
->new_layout
= UnSet
;
3339 info
->new_chunk
= info
->array
.chunk_size
;
3340 info
->delta_disks
= 0;
3344 info
->disk
.major
= dl
->major
;
3345 info
->disk
.minor
= dl
->minor
;
3346 info
->disk
.number
= dl
->index
;
3347 info
->disk
.raid_disk
= get_imsm_disk_slot(map_to_analyse
,
3351 info
->data_offset
= pba_of_lba0(map_to_analyse
);
3352 info
->component_size
= calc_component_size(map
, dev
);
3353 info
->component_size
= imsm_component_size_alignment_check(
3355 info
->array
.chunk_size
,
3357 info
->component_size
);
3358 info
->bb
.supported
= 1;
3360 memset(info
->uuid
, 0, sizeof(info
->uuid
));
3361 info
->recovery_start
= MaxSector
;
3363 if (info
->array
.level
== 5 &&
3364 (dev
->rwh_policy
== RWH_DISTRIBUTED
||
3365 dev
->rwh_policy
== RWH_MULTIPLE_DISTRIBUTED
)) {
3366 info
->consistency_policy
= CONSISTENCY_POLICY_PPL
;
3367 info
->ppl_sector
= get_ppl_sector(super
, super
->current_vol
);
3368 if (dev
->rwh_policy
== RWH_MULTIPLE_DISTRIBUTED
)
3369 info
->ppl_size
= MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9;
3371 info
->ppl_size
= (PPL_HEADER_SIZE
+ PPL_ENTRY_SPACE
)
3373 } else if (info
->array
.level
<= 0) {
3374 info
->consistency_policy
= CONSISTENCY_POLICY_NONE
;
3376 info
->consistency_policy
= CONSISTENCY_POLICY_RESYNC
;
3379 info
->reshape_progress
= 0;
3380 info
->resync_start
= MaxSector
;
3381 if ((map_to_analyse
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
3382 !(info
->array
.state
& 1)) &&
3383 imsm_reshape_blocks_arrays_changes(super
) == 0) {
3384 info
->resync_start
= 0;
3386 if (dev
->vol
.migr_state
) {
3387 switch (migr_type(dev
)) {
3390 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
3392 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
3394 info
->resync_start
= blocks_per_unit
* units
;
3397 case MIGR_GEN_MIGR
: {
3398 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
3400 __u64 units
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
3401 unsigned long long array_blocks
;
3404 if (__le32_to_cpu(migr_rec
->ascending_migr
) &&
3406 (__le32_to_cpu(migr_rec
->num_migr_units
)-1)) &&
3407 (super
->migr_rec
->rec_status
==
3408 __cpu_to_le32(UNIT_SRC_IN_CP_AREA
)))
3411 info
->reshape_progress
= blocks_per_unit
* units
;
3413 dprintf("IMSM: General Migration checkpoint : %llu (%llu) -> read reshape progress : %llu\n",
3414 (unsigned long long)units
,
3415 (unsigned long long)blocks_per_unit
,
3416 info
->reshape_progress
);
3418 used_disks
= imsm_num_data_members(prev_map
);
3419 if (used_disks
> 0) {
3420 array_blocks
= per_dev_array_size(map
) *
3422 info
->custom_array_size
=
3423 round_size_to_mb(array_blocks
,
3429 /* we could emulate the checkpointing of
3430 * 'sync_action=check' migrations, but for now
3431 * we just immediately complete them
3434 /* this is handled by container_content_imsm() */
3435 case MIGR_STATE_CHANGE
:
3436 /* FIXME handle other migrations */
3438 /* we are not dirty, so... */
3439 info
->resync_start
= MaxSector
;
3443 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
3444 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
3446 info
->array
.major_version
= -1;
3447 info
->array
.minor_version
= -2;
3448 sprintf(info
->text_version
, "/%s/%d", st
->container_devnm
, info
->container_member
);
3449 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
3450 uuid_from_super_imsm(st
, info
->uuid
);
3454 for (i
=0; i
<map_disks
; i
++) {
3456 if (i
< info
->array
.raid_disks
) {
3457 struct imsm_disk
*dsk
;
3458 j
= get_imsm_disk_idx(dev
, i
, MAP_X
);
3459 dsk
= get_imsm_disk(super
, j
);
3460 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
3467 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
3468 int failed
, int look_in_map
);
3470 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
3473 static void manage_second_map(struct intel_super
*super
, struct imsm_dev
*dev
)
3475 if (is_gen_migration(dev
)) {
3478 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
3480 failed
= imsm_count_failed(super
, dev
, MAP_1
);
3481 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_1
);
3482 if (map2
->map_state
!= map_state
) {
3483 map2
->map_state
= map_state
;
3484 super
->updates_pending
++;
3489 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
3493 for (d
= super
->missing
; d
; d
= d
->next
)
3494 if (d
->index
== index
)
3499 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
3501 struct intel_super
*super
= st
->sb
;
3502 struct imsm_disk
*disk
;
3503 int map_disks
= info
->array
.raid_disks
;
3504 int max_enough
= -1;
3506 struct imsm_super
*mpb
;
3508 if (super
->current_vol
>= 0) {
3509 getinfo_super_imsm_volume(st
, info
, map
);
3512 memset(info
, 0, sizeof(*info
));
3514 /* Set raid_disks to zero so that Assemble will always pull in valid
3517 info
->array
.raid_disks
= 0;
3518 info
->array
.level
= LEVEL_CONTAINER
;
3519 info
->array
.layout
= 0;
3520 info
->array
.md_minor
= -1;
3521 info
->array
.ctime
= 0; /* N/A for imsm */
3522 info
->array
.utime
= 0;
3523 info
->array
.chunk_size
= 0;
3525 info
->disk
.major
= 0;
3526 info
->disk
.minor
= 0;
3527 info
->disk
.raid_disk
= -1;
3528 info
->reshape_active
= 0;
3529 info
->array
.major_version
= -1;
3530 info
->array
.minor_version
= -2;
3531 strcpy(info
->text_version
, "imsm");
3532 info
->safe_mode_delay
= 0;
3533 info
->disk
.number
= -1;
3534 info
->disk
.state
= 0;
3536 info
->recovery_start
= MaxSector
;
3537 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
3538 info
->bb
.supported
= 1;
3540 /* do we have the all the insync disks that we expect? */
3541 mpb
= super
->anchor
;
3542 info
->events
= __le32_to_cpu(mpb
->generation_num
);
3544 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3545 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3546 int failed
, enough
, j
, missing
= 0;
3547 struct imsm_map
*map
;
3550 failed
= imsm_count_failed(super
, dev
, MAP_0
);
3551 state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
3552 map
= get_imsm_map(dev
, MAP_0
);
3554 /* any newly missing disks?
3555 * (catches single-degraded vs double-degraded)
3557 for (j
= 0; j
< map
->num_members
; j
++) {
3558 __u32 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
3559 __u32 idx
= ord_to_idx(ord
);
3561 if (super
->disks
&& super
->disks
->index
== (int)idx
)
3562 info
->disk
.raid_disk
= j
;
3564 if (!(ord
& IMSM_ORD_REBUILD
) &&
3565 get_imsm_missing(super
, idx
)) {
3571 if (state
== IMSM_T_STATE_FAILED
)
3573 else if (state
== IMSM_T_STATE_DEGRADED
&&
3574 (state
!= map
->map_state
|| missing
))
3576 else /* we're normal, or already degraded */
3578 if (is_gen_migration(dev
) && missing
) {
3579 /* during general migration we need all disks
3580 * that process is running on.
3581 * No new missing disk is allowed.
3585 /* no more checks necessary
3589 /* in the missing/failed disk case check to see
3590 * if at least one array is runnable
3592 max_enough
= max(max_enough
, enough
);
3594 dprintf("enough: %d\n", max_enough
);
3595 info
->container_enough
= max_enough
;
3598 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
3600 disk
= &super
->disks
->disk
;
3601 info
->data_offset
= total_blocks(&super
->disks
->disk
) - reserved
;
3602 info
->component_size
= reserved
;
3603 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
3604 /* we don't change info->disk.raid_disk here because
3605 * this state will be finalized in mdmon after we have
3606 * found the 'most fresh' version of the metadata
3608 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
3609 info
->disk
.state
|= (is_spare(disk
) || is_journal(disk
)) ?
3610 0 : (1 << MD_DISK_SYNC
);
3613 /* only call uuid_from_super_imsm when this disk is part of a populated container,
3614 * ->compare_super may have updated the 'num_raid_devs' field for spares
3616 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
3617 uuid_from_super_imsm(st
, info
->uuid
);
3619 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
3621 /* I don't know how to compute 'map' on imsm, so use safe default */
3624 for (i
= 0; i
< map_disks
; i
++)
3630 /* allocates memory and fills disk in mdinfo structure
3631 * for each disk in array */
3632 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
3634 struct mdinfo
*mddev
;
3635 struct intel_super
*super
= st
->sb
;
3636 struct imsm_disk
*disk
;
3639 if (!super
|| !super
->disks
)
3642 mddev
= xcalloc(1, sizeof(*mddev
));
3646 tmp
= xcalloc(1, sizeof(*tmp
));
3648 tmp
->next
= mddev
->devs
;
3650 tmp
->disk
.number
= count
++;
3651 tmp
->disk
.major
= dl
->major
;
3652 tmp
->disk
.minor
= dl
->minor
;
3653 tmp
->disk
.state
= is_configured(disk
) ?
3654 (1 << MD_DISK_ACTIVE
) : 0;
3655 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
3656 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
3657 tmp
->disk
.raid_disk
= -1;
3663 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
3664 char *update
, char *devname
, int verbose
,
3665 int uuid_set
, char *homehost
)
3667 /* For 'assemble' and 'force' we need to return non-zero if any
3668 * change was made. For others, the return value is ignored.
3669 * Update options are:
3670 * force-one : This device looks a bit old but needs to be included,
3671 * update age info appropriately.
3672 * assemble: clear any 'faulty' flag to allow this device to
3674 * force-array: Array is degraded but being forced, mark it clean
3675 * if that will be needed to assemble it.
3677 * newdev: not used ????
3678 * grow: Array has gained a new device - this is currently for
3680 * resync: mark as dirty so a resync will happen.
3681 * name: update the name - preserving the homehost
3682 * uuid: Change the uuid of the array to match watch is given
3684 * Following are not relevant for this imsm:
3685 * sparc2.2 : update from old dodgey metadata
3686 * super-minor: change the preferred_minor number
3687 * summaries: update redundant counters.
3688 * homehost: update the recorded homehost
3689 * _reshape_progress: record new reshape_progress position.
3692 struct intel_super
*super
= st
->sb
;
3693 struct imsm_super
*mpb
;
3695 /* we can only update container info */
3696 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
3699 mpb
= super
->anchor
;
3701 if (strcmp(update
, "uuid") == 0) {
3702 /* We take this to mean that the family_num should be updated.
3703 * However that is much smaller than the uuid so we cannot really
3704 * allow an explicit uuid to be given. And it is hard to reliably
3706 * So if !uuid_set we know the current uuid is random and just used
3707 * the first 'int' and copy it to the other 3 positions.
3708 * Otherwise we require the 4 'int's to be the same as would be the
3709 * case if we are using a random uuid. So an explicit uuid will be
3710 * accepted as long as all for ints are the same... which shouldn't hurt
3713 info
->uuid
[1] = info
->uuid
[2] = info
->uuid
[3] = info
->uuid
[0];
3716 if (info
->uuid
[0] != info
->uuid
[1] ||
3717 info
->uuid
[1] != info
->uuid
[2] ||
3718 info
->uuid
[2] != info
->uuid
[3])
3724 mpb
->orig_family_num
= info
->uuid
[0];
3725 } else if (strcmp(update
, "assemble") == 0)
3730 /* successful update? recompute checksum */
3732 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
3737 static size_t disks_to_mpb_size(int disks
)
3741 size
= sizeof(struct imsm_super
);
3742 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
3743 size
+= 2 * sizeof(struct imsm_dev
);
3744 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
3745 size
+= (4 - 2) * sizeof(struct imsm_map
);
3746 /* 4 possible disk_ord_tbl's */
3747 size
+= 4 * (disks
- 1) * sizeof(__u32
);
3748 /* maximum bbm log */
3749 size
+= sizeof(struct bbm_log
);
3754 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
,
3755 unsigned long long data_offset
)
3757 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
3760 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
3763 static void free_devlist(struct intel_super
*super
)
3765 struct intel_dev
*dv
;
3767 while (super
->devlist
) {
3768 dv
= super
->devlist
->next
;
3769 free(super
->devlist
->dev
);
3770 free(super
->devlist
);
3771 super
->devlist
= dv
;
3775 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
3777 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
3780 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
)
3784 * 0 same, or first was empty, and second was copied
3785 * 1 second had wrong number
3787 * 3 wrong other info
3789 struct intel_super
*first
= st
->sb
;
3790 struct intel_super
*sec
= tst
->sb
;
3797 /* in platform dependent environment test if the disks
3798 * use the same Intel hba
3799 * If not on Intel hba at all, allow anything.
3801 if (!check_env("IMSM_NO_PLATFORM") && first
->hba
&& sec
->hba
) {
3802 if (first
->hba
->type
!= sec
->hba
->type
) {
3804 "HBAs of devices do not match %s != %s\n",
3805 get_sys_dev_type(first
->hba
->type
),
3806 get_sys_dev_type(sec
->hba
->type
));
3809 if (first
->orom
!= sec
->orom
) {
3811 "HBAs of devices do not match %s != %s\n",
3812 first
->hba
->pci_id
, sec
->hba
->pci_id
);
3817 /* if an anchor does not have num_raid_devs set then it is a free
3820 if (first
->anchor
->num_raid_devs
> 0 &&
3821 sec
->anchor
->num_raid_devs
> 0) {
3822 /* Determine if these disks might ever have been
3823 * related. Further disambiguation can only take place
3824 * in load_super_imsm_all
3826 __u32 first_family
= first
->anchor
->orig_family_num
;
3827 __u32 sec_family
= sec
->anchor
->orig_family_num
;
3829 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
3830 MAX_SIGNATURE_LENGTH
) != 0)
3833 if (first_family
== 0)
3834 first_family
= first
->anchor
->family_num
;
3835 if (sec_family
== 0)
3836 sec_family
= sec
->anchor
->family_num
;
3838 if (first_family
!= sec_family
)
3843 /* if 'first' is a spare promote it to a populated mpb with sec's
3846 if (first
->anchor
->num_raid_devs
== 0 &&
3847 sec
->anchor
->num_raid_devs
> 0) {
3849 struct intel_dev
*dv
;
3850 struct imsm_dev
*dev
;
3852 /* we need to copy raid device info from sec if an allocation
3853 * fails here we don't associate the spare
3855 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++) {
3856 dv
= xmalloc(sizeof(*dv
));
3857 dev
= xmalloc(sizeof_imsm_dev(get_imsm_dev(sec
, i
), 1));
3860 dv
->next
= first
->devlist
;
3861 first
->devlist
= dv
;
3863 if (i
< sec
->anchor
->num_raid_devs
) {
3864 /* allocation failure */
3865 free_devlist(first
);
3866 pr_err("imsm: failed to associate spare\n");
3869 first
->anchor
->num_raid_devs
= sec
->anchor
->num_raid_devs
;
3870 first
->anchor
->orig_family_num
= sec
->anchor
->orig_family_num
;
3871 first
->anchor
->family_num
= sec
->anchor
->family_num
;
3872 memcpy(first
->anchor
->sig
, sec
->anchor
->sig
, MAX_SIGNATURE_LENGTH
);
3873 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++)
3874 imsm_copy_dev(get_imsm_dev(first
, i
), get_imsm_dev(sec
, i
));
3880 static void fd2devname(int fd
, char *name
)
3884 char dname
[PATH_MAX
];
3889 if (fstat(fd
, &st
) != 0)
3891 sprintf(path
, "/sys/dev/block/%d:%d",
3892 major(st
.st_rdev
), minor(st
.st_rdev
));
3894 rv
= readlink(path
, dname
, sizeof(dname
)-1);
3899 nm
= strrchr(dname
, '/');
3902 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
3906 static int nvme_get_serial(int fd
, void *buf
, size_t buf_len
)
3909 char *name
= fd2kname(fd
);
3914 if (strncmp(name
, "nvme", 4) != 0)
3917 snprintf(path
, sizeof(path
) - 1, "/sys/block/%s/device/serial", name
);
3919 return load_sys(path
, buf
, buf_len
);
3922 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
3924 static int imsm_read_serial(int fd
, char *devname
,
3925 __u8 serial
[MAX_RAID_SERIAL_LEN
])
3934 memset(buf
, 0, sizeof(buf
));
3936 rv
= nvme_get_serial(fd
, buf
, sizeof(buf
));
3939 rv
= scsi_get_serial(fd
, buf
, sizeof(buf
));
3941 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
3942 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3943 fd2devname(fd
, (char *) serial
);
3949 pr_err("Failed to retrieve serial for %s\n",
3954 /* trim all whitespace and non-printable characters and convert
3957 for (i
= 0, dest
= buf
; i
< sizeof(buf
) && buf
[i
]; i
++) {
3960 /* ':' is reserved for use in placeholder serial
3961 * numbers for missing disks
3972 /* truncate leading characters */
3973 if (len
> MAX_RAID_SERIAL_LEN
) {
3974 dest
+= len
- MAX_RAID_SERIAL_LEN
;
3975 len
= MAX_RAID_SERIAL_LEN
;
3978 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3979 memcpy(serial
, dest
, len
);
3984 static int serialcmp(__u8
*s1
, __u8
*s2
)
3986 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
3989 static void serialcpy(__u8
*dest
, __u8
*src
)
3991 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
3994 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
3998 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3999 if (serialcmp(dl
->serial
, serial
) == 0)
4005 static struct imsm_disk
*
4006 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
4010 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4011 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
4013 if (serialcmp(disk
->serial
, serial
) == 0) {
4024 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
4026 struct imsm_disk
*disk
;
4031 __u8 serial
[MAX_RAID_SERIAL_LEN
];
4033 rv
= imsm_read_serial(fd
, devname
, serial
);
4038 dl
= xcalloc(1, sizeof(*dl
));
4041 dl
->major
= major(stb
.st_rdev
);
4042 dl
->minor
= minor(stb
.st_rdev
);
4043 dl
->next
= super
->disks
;
4044 dl
->fd
= keep_fd
? fd
: -1;
4045 assert(super
->disks
== NULL
);
4047 serialcpy(dl
->serial
, serial
);
4050 fd2devname(fd
, name
);
4052 dl
->devname
= xstrdup(devname
);
4054 dl
->devname
= xstrdup(name
);
4056 /* look up this disk's index in the current anchor */
4057 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
4060 /* only set index on disks that are a member of a
4061 * populated contianer, i.e. one with raid_devs
4063 if (is_failed(&dl
->disk
))
4065 else if (is_spare(&dl
->disk
) || is_journal(&dl
->disk
))
4072 /* When migrating map0 contains the 'destination' state while map1
4073 * contains the current state. When not migrating map0 contains the
4074 * current state. This routine assumes that map[0].map_state is set to
4075 * the current array state before being called.
4077 * Migration is indicated by one of the following states
4078 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
4079 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
4080 * map1state=unitialized)
4081 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
4083 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
4084 * map1state=degraded)
4085 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
4088 static void migrate(struct imsm_dev
*dev
, struct intel_super
*super
,
4089 __u8 to_state
, int migr_type
)
4091 struct imsm_map
*dest
;
4092 struct imsm_map
*src
= get_imsm_map(dev
, MAP_0
);
4094 dev
->vol
.migr_state
= 1;
4095 set_migr_type(dev
, migr_type
);
4096 dev
->vol
.curr_migr_unit
= 0;
4097 dest
= get_imsm_map(dev
, MAP_1
);
4099 /* duplicate and then set the target end state in map[0] */
4100 memcpy(dest
, src
, sizeof_imsm_map(src
));
4101 if (migr_type
== MIGR_GEN_MIGR
) {
4105 for (i
= 0; i
< src
->num_members
; i
++) {
4106 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
4107 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
4111 if (migr_type
== MIGR_GEN_MIGR
)
4112 /* Clear migration record */
4113 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
4115 src
->map_state
= to_state
;
4118 static void end_migration(struct imsm_dev
*dev
, struct intel_super
*super
,
4121 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
4122 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
== 0 ?
4126 /* merge any IMSM_ORD_REBUILD bits that were not successfully
4127 * completed in the last migration.
4129 * FIXME add support for raid-level-migration
4131 if (map_state
!= map
->map_state
&& (is_gen_migration(dev
) == 0) &&
4132 prev
->map_state
!= IMSM_T_STATE_UNINITIALIZED
) {
4133 /* when final map state is other than expected
4134 * merge maps (not for migration)
4138 for (i
= 0; i
< prev
->num_members
; i
++)
4139 for (j
= 0; j
< map
->num_members
; j
++)
4140 /* during online capacity expansion
4141 * disks position can be changed
4142 * if takeover is used
4144 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
4145 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
4146 map
->disk_ord_tbl
[j
] |=
4147 prev
->disk_ord_tbl
[i
];
4150 failed
= imsm_count_failed(super
, dev
, MAP_0
);
4151 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
4154 dev
->vol
.migr_state
= 0;
4155 set_migr_type(dev
, 0);
4156 dev
->vol
.curr_migr_unit
= 0;
4157 map
->map_state
= map_state
;
4160 static int parse_raid_devices(struct intel_super
*super
)
4163 struct imsm_dev
*dev_new
;
4164 size_t len
, len_migr
;
4166 size_t space_needed
= 0;
4167 struct imsm_super
*mpb
= super
->anchor
;
4169 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
4170 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
4171 struct intel_dev
*dv
;
4173 len
= sizeof_imsm_dev(dev_iter
, 0);
4174 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
4176 space_needed
+= len_migr
- len
;
4178 dv
= xmalloc(sizeof(*dv
));
4179 if (max_len
< len_migr
)
4181 if (max_len
> len_migr
)
4182 space_needed
+= max_len
- len_migr
;
4183 dev_new
= xmalloc(max_len
);
4184 imsm_copy_dev(dev_new
, dev_iter
);
4187 dv
->next
= super
->devlist
;
4188 super
->devlist
= dv
;
4191 /* ensure that super->buf is large enough when all raid devices
4194 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
4197 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
,
4198 super
->sector_size
);
4199 if (posix_memalign(&buf
, MAX_SECTOR_SIZE
, len
) != 0)
4202 memcpy(buf
, super
->buf
, super
->len
);
4203 memset(buf
+ super
->len
, 0, len
- super
->len
);
4209 super
->extra_space
+= space_needed
;
4214 /*******************************************************************************
4215 * Function: check_mpb_migr_compatibility
4216 * Description: Function checks for unsupported migration features:
4217 * - migration optimization area (pba_of_lba0)
4218 * - descending reshape (ascending_migr)
4220 * super : imsm metadata information
4222 * 0 : migration is compatible
4223 * -1 : migration is not compatible
4224 ******************************************************************************/
4225 int check_mpb_migr_compatibility(struct intel_super
*super
)
4227 struct imsm_map
*map0
, *map1
;
4228 struct migr_record
*migr_rec
= super
->migr_rec
;
4231 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
4232 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
4235 dev_iter
->vol
.migr_state
== 1 &&
4236 dev_iter
->vol
.migr_type
== MIGR_GEN_MIGR
) {
4237 /* This device is migrating */
4238 map0
= get_imsm_map(dev_iter
, MAP_0
);
4239 map1
= get_imsm_map(dev_iter
, MAP_1
);
4240 if (pba_of_lba0(map0
) != pba_of_lba0(map1
))
4241 /* migration optimization area was used */
4243 if (migr_rec
->ascending_migr
== 0 &&
4244 migr_rec
->dest_depth_per_unit
> 0)
4245 /* descending reshape not supported yet */
4252 static void __free_imsm(struct intel_super
*super
, int free_disks
);
4254 /* load_imsm_mpb - read matrix metadata
4255 * allocates super->mpb to be freed by free_imsm
4257 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
4259 unsigned long long dsize
;
4260 unsigned long long sectors
;
4261 unsigned int sector_size
= super
->sector_size
;
4263 struct imsm_super
*anchor
;
4266 get_dev_size(fd
, NULL
, &dsize
);
4267 if (dsize
< 2*sector_size
) {
4269 pr_err("%s: device to small for imsm\n",
4274 if (lseek64(fd
, dsize
- (sector_size
* 2), SEEK_SET
) < 0) {
4276 pr_err("Cannot seek to anchor block on %s: %s\n",
4277 devname
, strerror(errno
));
4281 if (posix_memalign((void **)&anchor
, sector_size
, sector_size
) != 0) {
4283 pr_err("Failed to allocate imsm anchor buffer on %s\n", devname
);
4286 if ((unsigned int)read(fd
, anchor
, sector_size
) != sector_size
) {
4288 pr_err("Cannot read anchor block on %s: %s\n",
4289 devname
, strerror(errno
));
4294 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
4296 pr_err("no IMSM anchor on %s\n", devname
);
4301 __free_imsm(super
, 0);
4302 /* reload capability and hba */
4304 /* capability and hba must be updated with new super allocation */
4305 find_intel_hba_capability(fd
, super
, devname
);
4306 super
->len
= ROUND_UP(anchor
->mpb_size
, sector_size
);
4307 if (posix_memalign(&super
->buf
, MAX_SECTOR_SIZE
, super
->len
) != 0) {
4309 pr_err("unable to allocate %zu byte mpb buffer\n",
4314 memcpy(super
->buf
, anchor
, sector_size
);
4316 sectors
= mpb_sectors(anchor
, sector_size
) - 1;
4319 if (posix_memalign(&super
->migr_rec_buf
, MAX_SECTOR_SIZE
,
4320 MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
) != 0) {
4321 pr_err("could not allocate migr_rec buffer\n");
4325 super
->clean_migration_record_by_mdmon
= 0;
4328 check_sum
= __gen_imsm_checksum(super
->anchor
);
4329 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
4331 pr_err("IMSM checksum %x != %x on %s\n",
4333 __le32_to_cpu(super
->anchor
->check_sum
),
4341 /* read the extended mpb */
4342 if (lseek64(fd
, dsize
- (sector_size
* (2 + sectors
)), SEEK_SET
) < 0) {
4344 pr_err("Cannot seek to extended mpb on %s: %s\n",
4345 devname
, strerror(errno
));
4349 if ((unsigned int)read(fd
, super
->buf
+ sector_size
,
4350 super
->len
- sector_size
) != super
->len
- sector_size
) {
4352 pr_err("Cannot read extended mpb on %s: %s\n",
4353 devname
, strerror(errno
));
4357 check_sum
= __gen_imsm_checksum(super
->anchor
);
4358 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
4360 pr_err("IMSM checksum %x != %x on %s\n",
4361 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
4369 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
);
4371 /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
4372 static void clear_hi(struct intel_super
*super
)
4374 struct imsm_super
*mpb
= super
->anchor
;
4376 if (mpb
->attributes
& MPB_ATTRIB_2TB_DISK
)
4378 for (i
= 0; i
< mpb
->num_disks
; ++i
) {
4379 struct imsm_disk
*disk
= &mpb
->disk
[i
];
4380 disk
->total_blocks_hi
= 0;
4382 for (i
= 0; i
< mpb
->num_raid_devs
; ++i
) {
4383 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
4386 for (n
= 0; n
< 2; ++n
) {
4387 struct imsm_map
*map
= get_imsm_map(dev
, n
);
4390 map
->pba_of_lba0_hi
= 0;
4391 map
->blocks_per_member_hi
= 0;
4392 map
->num_data_stripes_hi
= 0;
4398 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
4402 err
= load_imsm_mpb(fd
, super
, devname
);
4405 if (super
->sector_size
== 4096)
4406 convert_from_4k(super
);
4407 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
4410 err
= parse_raid_devices(super
);
4413 err
= load_bbm_log(super
);
4418 static void __free_imsm_disk(struct dl
*d
)
4430 static void free_imsm_disks(struct intel_super
*super
)
4434 while (super
->disks
) {
4436 super
->disks
= d
->next
;
4437 __free_imsm_disk(d
);
4439 while (super
->disk_mgmt_list
) {
4440 d
= super
->disk_mgmt_list
;
4441 super
->disk_mgmt_list
= d
->next
;
4442 __free_imsm_disk(d
);
4444 while (super
->missing
) {
4446 super
->missing
= d
->next
;
4447 __free_imsm_disk(d
);
4452 /* free all the pieces hanging off of a super pointer */
4453 static void __free_imsm(struct intel_super
*super
, int free_disks
)
4455 struct intel_hba
*elem
, *next
;
4461 /* unlink capability description */
4463 if (super
->migr_rec_buf
) {
4464 free(super
->migr_rec_buf
);
4465 super
->migr_rec_buf
= NULL
;
4468 free_imsm_disks(super
);
4469 free_devlist(super
);
4473 free((void *)elem
->path
);
4479 free(super
->bbm_log
);
4483 static void free_imsm(struct intel_super
*super
)
4485 __free_imsm(super
, 1);
4486 free(super
->bb
.entries
);
4490 static void free_super_imsm(struct supertype
*st
)
4492 struct intel_super
*super
= st
->sb
;
4501 static struct intel_super
*alloc_super(void)
4503 struct intel_super
*super
= xcalloc(1, sizeof(*super
));
4505 super
->current_vol
= -1;
4506 super
->create_offset
= ~((unsigned long long) 0);
4508 super
->bb
.entries
= xmalloc(BBM_LOG_MAX_ENTRIES
*
4509 sizeof(struct md_bb_entry
));
4510 if (!super
->bb
.entries
) {
4519 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
4521 static int find_intel_hba_capability(int fd
, struct intel_super
*super
, char *devname
)
4523 struct sys_dev
*hba_name
;
4526 if (fd
>= 0 && test_partition(fd
)) {
4527 pr_err("imsm: %s is a partition, cannot be used in IMSM\n",
4531 if (fd
< 0 || check_env("IMSM_NO_PLATFORM")) {
4536 hba_name
= find_disk_attached_hba(fd
, NULL
);
4539 pr_err("%s is not attached to Intel(R) RAID controller.\n",
4543 rv
= attach_hba_to_super(super
, hba_name
);
4546 struct intel_hba
*hba
= super
->hba
;
4548 pr_err("%s is attached to Intel(R) %s %s (%s),\n"
4549 " but the container is assigned to Intel(R) %s %s (",
4551 get_sys_dev_type(hba_name
->type
),
4552 hba_name
->type
== SYS_DEV_VMD
? "domain" : "RAID controller",
4553 hba_name
->pci_id
? : "Err!",
4554 get_sys_dev_type(super
->hba
->type
),
4555 hba
->type
== SYS_DEV_VMD
? "domain" : "RAID controller");
4558 fprintf(stderr
, "%s", hba
->pci_id
? : "Err!");
4560 fprintf(stderr
, ", ");
4563 fprintf(stderr
, ").\n"
4564 " Mixing devices attached to different controllers is not allowed.\n");
4568 super
->orom
= find_imsm_capability(hba_name
);
4575 /* find_missing - helper routine for load_super_imsm_all that identifies
4576 * disks that have disappeared from the system. This routine relies on
4577 * the mpb being uptodate, which it is at load time.
4579 static int find_missing(struct intel_super
*super
)
4582 struct imsm_super
*mpb
= super
->anchor
;
4584 struct imsm_disk
*disk
;
4586 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4587 disk
= __get_imsm_disk(mpb
, i
);
4588 dl
= serial_to_dl(disk
->serial
, super
);
4592 dl
= xmalloc(sizeof(*dl
));
4596 dl
->devname
= xstrdup("missing");
4598 serialcpy(dl
->serial
, disk
->serial
);
4601 dl
->next
= super
->missing
;
4602 super
->missing
= dl
;
4608 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
4610 struct intel_disk
*idisk
= disk_list
;
4613 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
4615 idisk
= idisk
->next
;
4621 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
4622 struct intel_super
*super
,
4623 struct intel_disk
**disk_list
)
4625 struct imsm_disk
*d
= &super
->disks
->disk
;
4626 struct imsm_super
*mpb
= super
->anchor
;
4629 for (i
= 0; i
< tbl_size
; i
++) {
4630 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
4631 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
4633 if (tbl_mpb
->family_num
== mpb
->family_num
) {
4634 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
4635 dprintf("mpb from %d:%d matches %d:%d\n",
4636 super
->disks
->major
,
4637 super
->disks
->minor
,
4638 table
[i
]->disks
->major
,
4639 table
[i
]->disks
->minor
);
4643 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
4644 is_configured(d
) == is_configured(tbl_d
)) &&
4645 tbl_mpb
->generation_num
< mpb
->generation_num
) {
4646 /* current version of the mpb is a
4647 * better candidate than the one in
4648 * super_table, but copy over "cross
4649 * generational" status
4651 struct intel_disk
*idisk
;
4653 dprintf("mpb from %d:%d replaces %d:%d\n",
4654 super
->disks
->major
,
4655 super
->disks
->minor
,
4656 table
[i
]->disks
->major
,
4657 table
[i
]->disks
->minor
);
4659 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
4660 if (idisk
&& is_failed(&idisk
->disk
))
4661 tbl_d
->status
|= FAILED_DISK
;
4664 struct intel_disk
*idisk
;
4665 struct imsm_disk
*disk
;
4667 /* tbl_mpb is more up to date, but copy
4668 * over cross generational status before
4671 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
4672 if (disk
&& is_failed(disk
))
4673 d
->status
|= FAILED_DISK
;
4675 idisk
= disk_list_get(d
->serial
, *disk_list
);
4678 if (disk
&& is_configured(disk
))
4679 idisk
->disk
.status
|= CONFIGURED_DISK
;
4682 dprintf("mpb from %d:%d prefer %d:%d\n",
4683 super
->disks
->major
,
4684 super
->disks
->minor
,
4685 table
[i
]->disks
->major
,
4686 table
[i
]->disks
->minor
);
4694 table
[tbl_size
++] = super
;
4698 /* update/extend the merged list of imsm_disk records */
4699 for (j
= 0; j
< mpb
->num_disks
; j
++) {
4700 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
4701 struct intel_disk
*idisk
;
4703 idisk
= disk_list_get(disk
->serial
, *disk_list
);
4705 idisk
->disk
.status
|= disk
->status
;
4706 if (is_configured(&idisk
->disk
) ||
4707 is_failed(&idisk
->disk
))
4708 idisk
->disk
.status
&= ~(SPARE_DISK
);
4710 idisk
= xcalloc(1, sizeof(*idisk
));
4711 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
4712 idisk
->disk
= *disk
;
4713 idisk
->next
= *disk_list
;
4717 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
4724 static struct intel_super
*
4725 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
4728 struct imsm_super
*mpb
= super
->anchor
;
4732 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4733 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
4734 struct intel_disk
*idisk
;
4736 idisk
= disk_list_get(disk
->serial
, disk_list
);
4738 if (idisk
->owner
== owner
||
4739 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
4742 dprintf("'%.16s' owner %d != %d\n",
4743 disk
->serial
, idisk
->owner
,
4746 dprintf("unknown disk %x [%d]: %.16s\n",
4747 __le32_to_cpu(mpb
->family_num
), i
,
4753 if (ok_count
== mpb
->num_disks
)
4758 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
4760 struct intel_super
*s
;
4762 for (s
= super_list
; s
; s
= s
->next
) {
4763 if (family_num
!= s
->anchor
->family_num
)
4765 pr_err("Conflict, offlining family %#x on '%s'\n",
4766 __le32_to_cpu(family_num
), s
->disks
->devname
);
4770 static struct intel_super
*
4771 imsm_thunderdome(struct intel_super
**super_list
, int len
)
4773 struct intel_super
*super_table
[len
];
4774 struct intel_disk
*disk_list
= NULL
;
4775 struct intel_super
*champion
, *spare
;
4776 struct intel_super
*s
, **del
;
4781 memset(super_table
, 0, sizeof(super_table
));
4782 for (s
= *super_list
; s
; s
= s
->next
)
4783 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
4785 for (i
= 0; i
< tbl_size
; i
++) {
4786 struct imsm_disk
*d
;
4787 struct intel_disk
*idisk
;
4788 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
4791 d
= &s
->disks
->disk
;
4793 /* 'd' must appear in merged disk list for its
4794 * configuration to be valid
4796 idisk
= disk_list_get(d
->serial
, disk_list
);
4797 if (idisk
&& idisk
->owner
== i
)
4798 s
= validate_members(s
, disk_list
, i
);
4803 dprintf("marking family: %#x from %d:%d offline\n",
4805 super_table
[i
]->disks
->major
,
4806 super_table
[i
]->disks
->minor
);
4810 /* This is where the mdadm implementation differs from the Windows
4811 * driver which has no strict concept of a container. We can only
4812 * assemble one family from a container, so when returning a prodigal
4813 * array member to this system the code will not be able to disambiguate
4814 * the container contents that should be assembled ("foreign" versus
4815 * "local"). It requires user intervention to set the orig_family_num
4816 * to a new value to establish a new container. The Windows driver in
4817 * this situation fixes up the volume name in place and manages the
4818 * foreign array as an independent entity.
4823 for (i
= 0; i
< tbl_size
; i
++) {
4824 struct intel_super
*tbl_ent
= super_table
[i
];
4830 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
4835 if (s
&& !is_spare
) {
4836 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
4838 } else if (!s
&& !is_spare
)
4851 pr_err("Chose family %#x on '%s', assemble conflicts to new container with '--update=uuid'\n",
4852 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
4854 /* collect all dl's onto 'champion', and update them to
4855 * champion's version of the status
4857 for (s
= *super_list
; s
; s
= s
->next
) {
4858 struct imsm_super
*mpb
= champion
->anchor
;
4859 struct dl
*dl
= s
->disks
;
4864 mpb
->attributes
|= s
->anchor
->attributes
& MPB_ATTRIB_2TB_DISK
;
4866 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4867 struct imsm_disk
*disk
;
4869 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
4872 /* only set index on disks that are a member of
4873 * a populated contianer, i.e. one with
4876 if (is_failed(&dl
->disk
))
4878 else if (is_spare(&dl
->disk
))
4884 if (i
>= mpb
->num_disks
) {
4885 struct intel_disk
*idisk
;
4887 idisk
= disk_list_get(dl
->serial
, disk_list
);
4888 if (idisk
&& is_spare(&idisk
->disk
) &&
4889 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
4897 dl
->next
= champion
->disks
;
4898 champion
->disks
= dl
;
4902 /* delete 'champion' from super_list */
4903 for (del
= super_list
; *del
; ) {
4904 if (*del
== champion
) {
4905 *del
= (*del
)->next
;
4908 del
= &(*del
)->next
;
4910 champion
->next
= NULL
;
4914 struct intel_disk
*idisk
= disk_list
;
4916 disk_list
= disk_list
->next
;
4924 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
);
4925 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
4926 int major
, int minor
, int keep_fd
);
4928 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4929 int *max
, int keep_fd
);
4931 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
4932 char *devname
, struct md_list
*devlist
,
4935 struct intel_super
*super_list
= NULL
;
4936 struct intel_super
*super
= NULL
;
4941 /* 'fd' is an opened container */
4942 err
= get_sra_super_block(fd
, &super_list
, devname
, &i
, keep_fd
);
4944 /* get super block from devlist devices */
4945 err
= get_devlist_super_block(devlist
, &super_list
, &i
, keep_fd
);
4948 /* all mpbs enter, maybe one leaves */
4949 super
= imsm_thunderdome(&super_list
, i
);
4955 if (find_missing(super
) != 0) {
4961 /* load migration record */
4962 err
= load_imsm_migr_rec(super
, NULL
);
4964 /* migration is in progress,
4965 * but migr_rec cannot be loaded,
4971 /* Check migration compatibility */
4972 if (err
== 0 && check_mpb_migr_compatibility(super
) != 0) {
4973 pr_err("Unsupported migration detected");
4975 fprintf(stderr
, " on %s\n", devname
);
4977 fprintf(stderr
, " (IMSM).\n");
4986 while (super_list
) {
4987 struct intel_super
*s
= super_list
;
4989 super_list
= super_list
->next
;
4998 strcpy(st
->container_devnm
, fd2devnm(fd
));
5000 st
->container_devnm
[0] = 0;
5001 if (err
== 0 && st
->ss
== NULL
) {
5002 st
->ss
= &super_imsm
;
5003 st
->minor_version
= 0;
5004 st
->max_devs
= IMSM_MAX_DEVICES
;
5010 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
5011 int *max
, int keep_fd
)
5013 struct md_list
*tmpdev
;
5017 for (i
= 0, tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5018 if (tmpdev
->used
!= 1)
5020 if (tmpdev
->container
== 1) {
5022 int fd
= dev_open(tmpdev
->devname
, O_RDONLY
|O_EXCL
);
5024 pr_err("cannot open device %s: %s\n",
5025 tmpdev
->devname
, strerror(errno
));
5029 err
= get_sra_super_block(fd
, super_list
,
5030 tmpdev
->devname
, &lmax
,
5039 int major
= major(tmpdev
->st_rdev
);
5040 int minor
= minor(tmpdev
->st_rdev
);
5041 err
= get_super_block(super_list
,
5058 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
5059 int major
, int minor
, int keep_fd
)
5061 struct intel_super
*s
;
5073 sprintf(nm
, "%d:%d", major
, minor
);
5074 dfd
= dev_open(nm
, O_RDWR
);
5080 get_dev_sector_size(dfd
, NULL
, &s
->sector_size
);
5081 find_intel_hba_capability(dfd
, s
, devname
);
5082 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
5084 /* retry the load if we might have raced against mdmon */
5085 if (err
== 3 && devnm
&& mdmon_running(devnm
))
5086 for (retry
= 0; retry
< 3; retry
++) {
5088 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
5094 s
->next
= *super_list
;
5102 if (dfd
>= 0 && !keep_fd
)
5109 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
)
5116 sra
= sysfs_read(fd
, NULL
, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
5120 if (sra
->array
.major_version
!= -1 ||
5121 sra
->array
.minor_version
!= -2 ||
5122 strcmp(sra
->text_version
, "imsm") != 0) {
5127 devnm
= fd2devnm(fd
);
5128 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
5129 if (get_super_block(super_list
, devnm
, devname
,
5130 sd
->disk
.major
, sd
->disk
.minor
, keep_fd
) != 0) {
5141 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
5143 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
, NULL
, 1);
5146 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
5148 struct intel_super
*super
;
5152 if (test_partition(fd
))
5153 /* IMSM not allowed on partitions */
5156 free_super_imsm(st
);
5158 super
= alloc_super();
5159 get_dev_sector_size(fd
, NULL
, &super
->sector_size
);
5162 /* Load hba and capabilities if they exist.
5163 * But do not preclude loading metadata in case capabilities or hba are
5164 * non-compliant and ignore_hw_compat is set.
5166 rv
= find_intel_hba_capability(fd
, super
, devname
);
5167 /* no orom/efi or non-intel hba of the disk */
5168 if (rv
!= 0 && st
->ignore_hw_compat
== 0) {
5170 pr_err("No OROM/EFI properties for %s\n", devname
);
5174 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
5176 /* retry the load if we might have raced against mdmon */
5178 struct mdstat_ent
*mdstat
= NULL
;
5179 char *name
= fd2kname(fd
);
5182 mdstat
= mdstat_by_component(name
);
5184 if (mdstat
&& mdmon_running(mdstat
->devnm
) && getpid() != mdmon_pid(mdstat
->devnm
)) {
5185 for (retry
= 0; retry
< 3; retry
++) {
5187 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
5193 free_mdstat(mdstat
);
5198 pr_err("Failed to load all information sections on %s\n", devname
);
5204 if (st
->ss
== NULL
) {
5205 st
->ss
= &super_imsm
;
5206 st
->minor_version
= 0;
5207 st
->max_devs
= IMSM_MAX_DEVICES
;
5210 /* load migration record */
5211 if (load_imsm_migr_rec(super
, NULL
) == 0) {
5212 /* Check for unsupported migration features */
5213 if (check_mpb_migr_compatibility(super
) != 0) {
5214 pr_err("Unsupported migration detected");
5216 fprintf(stderr
, " on %s\n", devname
);
5218 fprintf(stderr
, " (IMSM).\n");
5226 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
5228 if (info
->level
== 1)
5230 return info
->chunk_size
>> 9;
5233 static unsigned long long info_to_blocks_per_member(mdu_array_info_t
*info
,
5234 unsigned long long size
)
5236 if (info
->level
== 1)
5239 return (size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
5242 static void imsm_update_version_info(struct intel_super
*super
)
5244 /* update the version and attributes */
5245 struct imsm_super
*mpb
= super
->anchor
;
5247 struct imsm_dev
*dev
;
5248 struct imsm_map
*map
;
5251 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5252 dev
= get_imsm_dev(super
, i
);
5253 map
= get_imsm_map(dev
, MAP_0
);
5254 if (__le32_to_cpu(dev
->size_high
) > 0)
5255 mpb
->attributes
|= MPB_ATTRIB_2TB
;
5257 /* FIXME detect when an array spans a port multiplier */
5259 mpb
->attributes
|= MPB_ATTRIB_PM
;
5262 if (mpb
->num_raid_devs
> 1 ||
5263 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
5264 version
= MPB_VERSION_ATTRIBS
;
5265 switch (get_imsm_raid_level(map
)) {
5266 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
5267 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
5268 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
5269 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
5272 if (map
->num_members
>= 5)
5273 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
5274 else if (dev
->status
== DEV_CLONE_N_GO
)
5275 version
= MPB_VERSION_CNG
;
5276 else if (get_imsm_raid_level(map
) == 5)
5277 version
= MPB_VERSION_RAID5
;
5278 else if (map
->num_members
>= 3)
5279 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
5280 else if (get_imsm_raid_level(map
) == 1)
5281 version
= MPB_VERSION_RAID1
;
5283 version
= MPB_VERSION_RAID0
;
5285 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
5289 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
5291 struct imsm_super
*mpb
= super
->anchor
;
5292 char *reason
= NULL
;
5294 size_t len
= strlen(name
);
5298 while (isspace(start
[len
- 1]))
5300 while (*start
&& isspace(*start
))
5302 memmove(name
, start
, len
+ 1);
5305 if (len
> MAX_RAID_SERIAL_LEN
)
5306 reason
= "must be 16 characters or less";
5308 reason
= "must be a non-empty string";
5310 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5311 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
5313 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
5314 reason
= "already exists";
5319 if (reason
&& !quiet
)
5320 pr_err("imsm volume name %s\n", reason
);
5325 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
5326 struct shape
*s
, char *name
,
5327 char *homehost
, int *uuid
,
5328 long long data_offset
)
5330 /* We are creating a volume inside a pre-existing container.
5331 * so st->sb is already set.
5333 struct intel_super
*super
= st
->sb
;
5334 unsigned int sector_size
= super
->sector_size
;
5335 struct imsm_super
*mpb
= super
->anchor
;
5336 struct intel_dev
*dv
;
5337 struct imsm_dev
*dev
;
5338 struct imsm_vol
*vol
;
5339 struct imsm_map
*map
;
5340 int idx
= mpb
->num_raid_devs
;
5343 unsigned long long array_blocks
;
5344 size_t size_old
, size_new
;
5345 unsigned long long num_data_stripes
;
5346 unsigned int data_disks
;
5347 unsigned long long size_per_member
;
5349 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
5350 pr_err("This imsm-container already has the maximum of %d volumes\n", super
->orom
->vpa
);
5354 /* ensure the mpb is large enough for the new data */
5355 size_old
= __le32_to_cpu(mpb
->mpb_size
);
5356 size_new
= disks_to_mpb_size(info
->nr_disks
);
5357 if (size_new
> size_old
) {
5359 size_t size_round
= ROUND_UP(size_new
, sector_size
);
5361 if (posix_memalign(&mpb_new
, sector_size
, size_round
) != 0) {
5362 pr_err("could not allocate new mpb\n");
5365 if (posix_memalign(&super
->migr_rec_buf
, MAX_SECTOR_SIZE
,
5366 MIGR_REC_BUF_SECTORS
*
5367 MAX_SECTOR_SIZE
) != 0) {
5368 pr_err("could not allocate migr_rec buffer\n");
5374 memcpy(mpb_new
, mpb
, size_old
);
5377 super
->anchor
= mpb_new
;
5378 mpb
->mpb_size
= __cpu_to_le32(size_new
);
5379 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
5380 super
->len
= size_round
;
5382 super
->current_vol
= idx
;
5384 /* handle 'failed_disks' by either:
5385 * a) create dummy disk entries in the table if this the first
5386 * volume in the array. We add them here as this is the only
5387 * opportunity to add them. add_to_super_imsm_volume()
5388 * handles the non-failed disks and continues incrementing
5390 * b) validate that 'failed_disks' matches the current number
5391 * of missing disks if the container is populated
5393 if (super
->current_vol
== 0) {
5395 for (i
= 0; i
< info
->failed_disks
; i
++) {
5396 struct imsm_disk
*disk
;
5399 disk
= __get_imsm_disk(mpb
, i
);
5400 disk
->status
= CONFIGURED_DISK
| FAILED_DISK
;
5401 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
5402 snprintf((char *) disk
->serial
, MAX_RAID_SERIAL_LEN
,
5403 "missing:%d", (__u8
)i
);
5405 find_missing(super
);
5410 for (d
= super
->missing
; d
; d
= d
->next
)
5412 if (info
->failed_disks
> missing
) {
5413 pr_err("unable to add 'missing' disk to container\n");
5418 if (!check_name(super
, name
, 0))
5420 dv
= xmalloc(sizeof(*dv
));
5421 dev
= xcalloc(1, sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
5423 * Explicitly allow truncating to not confuse gcc's
5424 * -Werror=stringop-truncation
5426 namelen
= min((int) strlen(name
), MAX_RAID_SERIAL_LEN
);
5427 memcpy(dev
->volume
, name
, namelen
);
5428 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
5429 info
->layout
, info
->chunk_size
,
5430 s
->size
* BLOCKS_PER_KB
);
5431 data_disks
= get_data_disks(info
->level
, info
->layout
,
5433 array_blocks
= round_size_to_mb(array_blocks
, data_disks
);
5434 size_per_member
= array_blocks
/ data_disks
;
5436 set_imsm_dev_size(dev
, array_blocks
);
5437 dev
->status
= (DEV_READ_COALESCING
| DEV_WRITE_COALESCING
);
5439 vol
->migr_state
= 0;
5440 set_migr_type(dev
, MIGR_INIT
);
5441 vol
->dirty
= !info
->state
;
5442 vol
->curr_migr_unit
= 0;
5443 map
= get_imsm_map(dev
, MAP_0
);
5444 set_pba_of_lba0(map
, super
->create_offset
);
5445 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
5446 map
->failed_disk_num
= ~0;
5447 if (info
->level
> 0)
5448 map
->map_state
= (info
->state
? IMSM_T_STATE_NORMAL
5449 : IMSM_T_STATE_UNINITIALIZED
);
5451 map
->map_state
= info
->failed_disks
? IMSM_T_STATE_FAILED
:
5452 IMSM_T_STATE_NORMAL
;
5455 if (info
->level
== 1 && info
->raid_disks
> 2) {
5458 pr_err("imsm does not support more than 2 disksin a raid1 volume\n");
5462 map
->raid_level
= info
->level
;
5463 if (info
->level
== 10) {
5464 map
->raid_level
= 1;
5465 map
->num_domains
= info
->raid_disks
/ 2;
5466 } else if (info
->level
== 1)
5467 map
->num_domains
= info
->raid_disks
;
5469 map
->num_domains
= 1;
5471 /* info->size is only int so use the 'size' parameter instead */
5472 num_data_stripes
= size_per_member
/ info_to_blocks_per_strip(info
);
5473 num_data_stripes
/= map
->num_domains
;
5474 set_num_data_stripes(map
, num_data_stripes
);
5476 size_per_member
+= NUM_BLOCKS_DIRTY_STRIPE_REGION
;
5477 set_blocks_per_member(map
, info_to_blocks_per_member(info
,
5481 map
->num_members
= info
->raid_disks
;
5482 for (i
= 0; i
< map
->num_members
; i
++) {
5483 /* initialized in add_to_super */
5484 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
5486 mpb
->num_raid_devs
++;
5487 mpb
->num_raid_devs_created
++;
5488 dev
->my_vol_raid_dev_num
= mpb
->num_raid_devs_created
;
5490 if (s
->consistency_policy
<= CONSISTENCY_POLICY_RESYNC
) {
5491 dev
->rwh_policy
= RWH_MULTIPLE_OFF
;
5492 } else if (s
->consistency_policy
== CONSISTENCY_POLICY_PPL
) {
5493 dev
->rwh_policy
= RWH_MULTIPLE_DISTRIBUTED
;
5497 pr_err("imsm does not support consistency policy %s\n",
5498 map_num(consistency_policies
, s
->consistency_policy
));
5503 dv
->index
= super
->current_vol
;
5504 dv
->next
= super
->devlist
;
5505 super
->devlist
= dv
;
5507 imsm_update_version_info(super
);
5512 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
5513 struct shape
*s
, char *name
,
5514 char *homehost
, int *uuid
,
5515 unsigned long long data_offset
)
5517 /* This is primarily called by Create when creating a new array.
5518 * We will then get add_to_super called for each component, and then
5519 * write_init_super called to write it out to each device.
5520 * For IMSM, Create can create on fresh devices or on a pre-existing
5522 * To create on a pre-existing array a different method will be called.
5523 * This one is just for fresh drives.
5525 struct intel_super
*super
;
5526 struct imsm_super
*mpb
;
5530 if (data_offset
!= INVALID_SECTORS
) {
5531 pr_err("data-offset not supported by imsm\n");
5536 return init_super_imsm_volume(st
, info
, s
, name
, homehost
, uuid
,
5540 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
5542 mpb_size
= MAX_SECTOR_SIZE
;
5544 super
= alloc_super();
5546 posix_memalign(&super
->buf
, MAX_SECTOR_SIZE
, mpb_size
) != 0) {
5551 pr_err("could not allocate superblock\n");
5554 if (posix_memalign(&super
->migr_rec_buf
, MAX_SECTOR_SIZE
,
5555 MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
) != 0) {
5556 pr_err("could not allocate migr_rec buffer\n");
5561 memset(super
->buf
, 0, mpb_size
);
5563 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5567 /* zeroing superblock */
5571 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
5573 version
= (char *) mpb
->sig
;
5574 strcpy(version
, MPB_SIGNATURE
);
5575 version
+= strlen(MPB_SIGNATURE
);
5576 strcpy(version
, MPB_VERSION_RAID0
);
5581 static int drive_validate_sector_size(struct intel_super
*super
, struct dl
*dl
)
5583 unsigned int member_sector_size
;
5586 pr_err("Invalid file descriptor for %s\n", dl
->devname
);
5590 if (!get_dev_sector_size(dl
->fd
, dl
->devname
, &member_sector_size
))
5592 if (member_sector_size
!= super
->sector_size
)
5597 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
5598 int fd
, char *devname
)
5600 struct intel_super
*super
= st
->sb
;
5601 struct imsm_super
*mpb
= super
->anchor
;
5602 struct imsm_disk
*_disk
;
5603 struct imsm_dev
*dev
;
5604 struct imsm_map
*map
;
5608 dev
= get_imsm_dev(super
, super
->current_vol
);
5609 map
= get_imsm_map(dev
, MAP_0
);
5611 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
5612 pr_err("%s: Cannot add spare devices to IMSM volume\n",
5618 /* we're doing autolayout so grab the pre-marked (in
5619 * validate_geometry) raid_disk
5621 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5622 if (dl
->raiddisk
== dk
->raid_disk
)
5625 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5626 if (dl
->major
== dk
->major
&&
5627 dl
->minor
== dk
->minor
)
5632 pr_err("%s is not a member of the same container\n", devname
);
5636 if (mpb
->num_disks
== 0)
5637 if (!get_dev_sector_size(dl
->fd
, dl
->devname
,
5638 &super
->sector_size
))
5641 if (!drive_validate_sector_size(super
, dl
)) {
5642 pr_err("Combining drives of different sector size in one volume is not allowed\n");
5646 /* add a pristine spare to the metadata */
5647 if (dl
->index
< 0) {
5648 dl
->index
= super
->anchor
->num_disks
;
5649 super
->anchor
->num_disks
++;
5651 /* Check the device has not already been added */
5652 slot
= get_imsm_disk_slot(map
, dl
->index
);
5654 (get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
) & IMSM_ORD_REBUILD
) == 0) {
5655 pr_err("%s has been included in this array twice\n",
5659 set_imsm_ord_tbl_ent(map
, dk
->raid_disk
, dl
->index
);
5660 dl
->disk
.status
= CONFIGURED_DISK
;
5662 /* update size of 'missing' disks to be at least as large as the
5663 * largest acitve member (we only have dummy missing disks when
5664 * creating the first volume)
5666 if (super
->current_vol
== 0) {
5667 for (df
= super
->missing
; df
; df
= df
->next
) {
5668 if (total_blocks(&dl
->disk
) > total_blocks(&df
->disk
))
5669 set_total_blocks(&df
->disk
, total_blocks(&dl
->disk
));
5670 _disk
= __get_imsm_disk(mpb
, df
->index
);
5675 /* refresh unset/failed slots to point to valid 'missing' entries */
5676 for (df
= super
->missing
; df
; df
= df
->next
)
5677 for (slot
= 0; slot
< mpb
->num_disks
; slot
++) {
5678 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
5680 if ((ord
& IMSM_ORD_REBUILD
) == 0)
5682 set_imsm_ord_tbl_ent(map
, slot
, df
->index
| IMSM_ORD_REBUILD
);
5683 if (is_gen_migration(dev
)) {
5684 struct imsm_map
*map2
= get_imsm_map(dev
,
5686 int slot2
= get_imsm_disk_slot(map2
, df
->index
);
5687 if (slot2
< map2
->num_members
&& slot2
>= 0) {
5688 __u32 ord2
= get_imsm_ord_tbl_ent(dev
,
5691 if ((unsigned)df
->index
==
5693 set_imsm_ord_tbl_ent(map2
,
5699 dprintf("set slot:%d to missing disk:%d\n", slot
, df
->index
);
5703 /* if we are creating the first raid device update the family number */
5704 if (super
->current_vol
== 0) {
5706 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
5708 _disk
= __get_imsm_disk(mpb
, dl
->index
);
5709 if (!_dev
|| !_disk
) {
5710 pr_err("BUG mpb setup error\n");
5716 sum
+= __gen_imsm_checksum(mpb
);
5717 mpb
->family_num
= __cpu_to_le32(sum
);
5718 mpb
->orig_family_num
= mpb
->family_num
;
5720 super
->current_disk
= dl
;
5725 * Function marks disk as spare and restores disk serial
5726 * in case it was previously marked as failed by takeover operation
5728 * -1 : critical error
5729 * 0 : disk is marked as spare but serial is not set
5732 int mark_spare(struct dl
*disk
)
5734 __u8 serial
[MAX_RAID_SERIAL_LEN
];
5741 if (!imsm_read_serial(disk
->fd
, NULL
, serial
)) {
5742 /* Restore disk serial number, because takeover marks disk
5743 * as failed and adds to serial ':0' before it becomes
5746 serialcpy(disk
->serial
, serial
);
5747 serialcpy(disk
->disk
.serial
, serial
);
5750 disk
->disk
.status
= SPARE_DISK
;
5756 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
5757 int fd
, char *devname
,
5758 unsigned long long data_offset
)
5760 struct intel_super
*super
= st
->sb
;
5762 unsigned long long size
;
5763 unsigned int member_sector_size
;
5768 /* If we are on an RAID enabled platform check that the disk is
5769 * attached to the raid controller.
5770 * We do not need to test disks attachment for container based additions,
5771 * they shall be already tested when container was created/assembled.
5773 rv
= find_intel_hba_capability(fd
, super
, devname
);
5774 /* no orom/efi or non-intel hba of the disk */
5776 dprintf("capability: %p fd: %d ret: %d\n",
5777 super
->orom
, fd
, rv
);
5781 if (super
->current_vol
>= 0)
5782 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
5785 dd
= xcalloc(sizeof(*dd
), 1);
5786 dd
->major
= major(stb
.st_rdev
);
5787 dd
->minor
= minor(stb
.st_rdev
);
5788 dd
->devname
= devname
? xstrdup(devname
) : NULL
;
5791 dd
->action
= DISK_ADD
;
5792 rv
= imsm_read_serial(fd
, devname
, dd
->serial
);
5794 pr_err("failed to retrieve scsi serial, aborting\n");
5800 if (super
->hba
&& ((super
->hba
->type
== SYS_DEV_NVME
) ||
5801 (super
->hba
->type
== SYS_DEV_VMD
))) {
5803 char *devpath
= diskfd_to_devpath(fd
);
5804 char controller_path
[PATH_MAX
];
5807 pr_err("failed to get devpath, aborting\n");
5814 snprintf(controller_path
, PATH_MAX
-1, "%s/device", devpath
);
5817 if (devpath_to_vendor(controller_path
) == 0x8086) {
5819 * If Intel's NVMe drive has serial ended with
5820 * "-A","-B","-1" or "-2" it means that this is "x8"
5821 * device (double drive on single PCIe card).
5822 * User should be warned about potential data loss.
5824 for (i
= MAX_RAID_SERIAL_LEN
-1; i
> 0; i
--) {
5825 /* Skip empty character at the end */
5826 if (dd
->serial
[i
] == 0)
5829 if (((dd
->serial
[i
] == 'A') ||
5830 (dd
->serial
[i
] == 'B') ||
5831 (dd
->serial
[i
] == '1') ||
5832 (dd
->serial
[i
] == '2')) &&
5833 (dd
->serial
[i
-1] == '-'))
5834 pr_err("\tThe action you are about to take may put your data at risk.\n"
5835 "\tPlease note that x8 devices may consist of two separate x4 devices "
5836 "located on a single PCIe port.\n"
5837 "\tRAID 0 is the only supported configuration for this type of x8 device.\n");
5840 } else if (super
->hba
->type
== SYS_DEV_VMD
&& super
->orom
&&
5841 !imsm_orom_has_tpv_support(super
->orom
)) {
5842 pr_err("\tPlatform configuration does not support non-Intel NVMe drives.\n"
5843 "\tPlease refer to Intel(R) RSTe/VROC user guide.\n");
5850 get_dev_size(fd
, NULL
, &size
);
5851 get_dev_sector_size(fd
, NULL
, &member_sector_size
);
5853 if (super
->sector_size
== 0) {
5854 /* this a first device, so sector_size is not set yet */
5855 super
->sector_size
= member_sector_size
;
5858 /* clear migr_rec when adding disk to container */
5859 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
);
5860 if (lseek64(fd
, size
- MIGR_REC_SECTOR_POSITION
*member_sector_size
,
5862 if ((unsigned int)write(fd
, super
->migr_rec_buf
,
5863 MIGR_REC_BUF_SECTORS
*member_sector_size
) !=
5864 MIGR_REC_BUF_SECTORS
*member_sector_size
)
5865 perror("Write migr_rec failed");
5869 serialcpy(dd
->disk
.serial
, dd
->serial
);
5870 set_total_blocks(&dd
->disk
, size
);
5871 if (__le32_to_cpu(dd
->disk
.total_blocks_hi
) > 0) {
5872 struct imsm_super
*mpb
= super
->anchor
;
5873 mpb
->attributes
|= MPB_ATTRIB_2TB_DISK
;
5876 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
5877 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
5879 dd
->disk
.scsi_id
= __cpu_to_le32(0);
5881 if (st
->update_tail
) {
5882 dd
->next
= super
->disk_mgmt_list
;
5883 super
->disk_mgmt_list
= dd
;
5885 dd
->next
= super
->disks
;
5887 super
->updates_pending
++;
5893 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
5895 struct intel_super
*super
= st
->sb
;
5898 /* remove from super works only in mdmon - for communication
5899 * manager - monitor. Check if communication memory buffer
5902 if (!st
->update_tail
) {
5903 pr_err("shall be used in mdmon context only\n");
5906 dd
= xcalloc(1, sizeof(*dd
));
5907 dd
->major
= dk
->major
;
5908 dd
->minor
= dk
->minor
;
5911 dd
->action
= DISK_REMOVE
;
5913 dd
->next
= super
->disk_mgmt_list
;
5914 super
->disk_mgmt_list
= dd
;
5919 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
5922 char buf
[MAX_SECTOR_SIZE
];
5923 struct imsm_super anchor
;
5924 } spare_record
__attribute__ ((aligned(MAX_SECTOR_SIZE
)));
5926 /* spare records have their own family number and do not have any defined raid
5929 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
5931 struct imsm_super
*mpb
= super
->anchor
;
5932 struct imsm_super
*spare
= &spare_record
.anchor
;
5936 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
));
5937 spare
->generation_num
= __cpu_to_le32(1UL);
5938 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
5939 spare
->num_disks
= 1;
5940 spare
->num_raid_devs
= 0;
5941 spare
->cache_size
= mpb
->cache_size
;
5942 spare
->pwr_cycle_count
= __cpu_to_le32(1);
5944 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
5945 MPB_SIGNATURE MPB_VERSION_RAID0
);
5947 for (d
= super
->disks
; d
; d
= d
->next
) {
5951 spare
->disk
[0] = d
->disk
;
5952 if (__le32_to_cpu(d
->disk
.total_blocks_hi
) > 0)
5953 spare
->attributes
|= MPB_ATTRIB_2TB_DISK
;
5955 if (super
->sector_size
== 4096)
5956 convert_to_4k_imsm_disk(&spare
->disk
[0]);
5958 sum
= __gen_imsm_checksum(spare
);
5959 spare
->family_num
= __cpu_to_le32(sum
);
5960 spare
->orig_family_num
= 0;
5961 sum
= __gen_imsm_checksum(spare
);
5962 spare
->check_sum
= __cpu_to_le32(sum
);
5964 if (store_imsm_mpb(d
->fd
, spare
)) {
5965 pr_err("failed for device %d:%d %s\n",
5966 d
->major
, d
->minor
, strerror(errno
));
5978 static int write_super_imsm(struct supertype
*st
, int doclose
)
5980 struct intel_super
*super
= st
->sb
;
5981 unsigned int sector_size
= super
->sector_size
;
5982 struct imsm_super
*mpb
= super
->anchor
;
5988 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
5990 int clear_migration_record
= 1;
5993 /* 'generation' is incremented everytime the metadata is written */
5994 generation
= __le32_to_cpu(mpb
->generation_num
);
5996 mpb
->generation_num
= __cpu_to_le32(generation
);
5998 /* fix up cases where previous mdadm releases failed to set
6001 if (mpb
->orig_family_num
== 0)
6002 mpb
->orig_family_num
= mpb
->family_num
;
6004 for (d
= super
->disks
; d
; d
= d
->next
) {
6008 mpb
->disk
[d
->index
] = d
->disk
;
6012 for (d
= super
->missing
; d
; d
= d
->next
) {
6013 mpb
->disk
[d
->index
] = d
->disk
;
6016 mpb
->num_disks
= num_disks
;
6017 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
6019 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6020 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
6021 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
6023 imsm_copy_dev(dev
, dev2
);
6024 mpb_size
+= sizeof_imsm_dev(dev
, 0);
6026 if (is_gen_migration(dev2
))
6027 clear_migration_record
= 0;
6030 bbm_log_size
= get_imsm_bbm_log_size(super
->bbm_log
);
6033 memcpy((void *)mpb
+ mpb_size
, super
->bbm_log
, bbm_log_size
);
6034 mpb
->attributes
|= MPB_ATTRIB_BBM
;
6036 mpb
->attributes
&= ~MPB_ATTRIB_BBM
;
6038 super
->anchor
->bbm_log_size
= __cpu_to_le32(bbm_log_size
);
6039 mpb_size
+= bbm_log_size
;
6040 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
6043 assert(super
->len
== 0 || mpb_size
<= super
->len
);
6046 /* recalculate checksum */
6047 sum
= __gen_imsm_checksum(mpb
);
6048 mpb
->check_sum
= __cpu_to_le32(sum
);
6050 if (super
->clean_migration_record_by_mdmon
) {
6051 clear_migration_record
= 1;
6052 super
->clean_migration_record_by_mdmon
= 0;
6054 if (clear_migration_record
)
6055 memset(super
->migr_rec_buf
, 0,
6056 MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
);
6058 if (sector_size
== 4096)
6059 convert_to_4k(super
);
6061 /* write the mpb for disks that compose raid devices */
6062 for (d
= super
->disks
; d
; d
= d
->next
) {
6063 if (d
->index
< 0 || is_failed(&d
->disk
))
6066 if (clear_migration_record
) {
6067 unsigned long long dsize
;
6069 get_dev_size(d
->fd
, NULL
, &dsize
);
6070 if (lseek64(d
->fd
, dsize
- sector_size
,
6072 if ((unsigned int)write(d
->fd
,
6073 super
->migr_rec_buf
,
6074 MIGR_REC_BUF_SECTORS
*sector_size
) !=
6075 MIGR_REC_BUF_SECTORS
*sector_size
)
6076 perror("Write migr_rec failed");
6080 if (store_imsm_mpb(d
->fd
, mpb
))
6082 "failed for device %d:%d (fd: %d)%s\n",
6084 d
->fd
, strerror(errno
));
6093 return write_super_imsm_spares(super
, doclose
);
6098 static int create_array(struct supertype
*st
, int dev_idx
)
6101 struct imsm_update_create_array
*u
;
6102 struct intel_super
*super
= st
->sb
;
6103 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
6104 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
6105 struct disk_info
*inf
;
6106 struct imsm_disk
*disk
;
6109 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
6110 sizeof(*inf
) * map
->num_members
;
6112 u
->type
= update_create_array
;
6113 u
->dev_idx
= dev_idx
;
6114 imsm_copy_dev(&u
->dev
, dev
);
6115 inf
= get_disk_info(u
);
6116 for (i
= 0; i
< map
->num_members
; i
++) {
6117 int idx
= get_imsm_disk_idx(dev
, i
, MAP_X
);
6119 disk
= get_imsm_disk(super
, idx
);
6121 disk
= get_imsm_missing(super
, idx
);
6122 serialcpy(inf
[i
].serial
, disk
->serial
);
6124 append_metadata_update(st
, u
, len
);
6129 static int mgmt_disk(struct supertype
*st
)
6131 struct intel_super
*super
= st
->sb
;
6133 struct imsm_update_add_remove_disk
*u
;
6135 if (!super
->disk_mgmt_list
)
6140 u
->type
= update_add_remove_disk
;
6141 append_metadata_update(st
, u
, len
);
6146 __u32
crc32c_le(__u32 crc
, unsigned char const *p
, size_t len
);
6148 static int write_ppl_header(unsigned long long ppl_sector
, int fd
, void *buf
)
6150 struct ppl_header
*ppl_hdr
= buf
;
6153 ppl_hdr
->checksum
= __cpu_to_le32(~crc32c_le(~0, buf
, PPL_HEADER_SIZE
));
6155 if (lseek64(fd
, ppl_sector
* 512, SEEK_SET
) < 0) {
6157 perror("Failed to seek to PPL header location");
6161 if (write(fd
, buf
, PPL_HEADER_SIZE
) != PPL_HEADER_SIZE
) {
6163 perror("Write PPL header failed");
6172 static int write_init_ppl_imsm(struct supertype
*st
, struct mdinfo
*info
, int fd
)
6174 struct intel_super
*super
= st
->sb
;
6176 struct ppl_header
*ppl_hdr
;
6179 /* first clear entire ppl space */
6180 ret
= zero_disk_range(fd
, info
->ppl_sector
, info
->ppl_size
);
6184 ret
= posix_memalign(&buf
, MAX_SECTOR_SIZE
, PPL_HEADER_SIZE
);
6186 pr_err("Failed to allocate PPL header buffer\n");
6190 memset(buf
, 0, PPL_HEADER_SIZE
);
6192 memset(ppl_hdr
->reserved
, 0xff, PPL_HDR_RESERVED
);
6193 ppl_hdr
->signature
= __cpu_to_le32(super
->anchor
->orig_family_num
);
6195 if (info
->mismatch_cnt
) {
6197 * We are overwriting an invalid ppl. Make one entry with wrong
6198 * checksum to prevent the kernel from skipping resync.
6200 ppl_hdr
->entries_count
= __cpu_to_le32(1);
6201 ppl_hdr
->entries
[0].checksum
= ~0;
6204 ret
= write_ppl_header(info
->ppl_sector
, fd
, buf
);
6210 static int is_rebuilding(struct imsm_dev
*dev
);
6212 static int validate_ppl_imsm(struct supertype
*st
, struct mdinfo
*info
,
6213 struct mdinfo
*disk
)
6215 struct intel_super
*super
= st
->sb
;
6217 void *buf_orig
, *buf
, *buf_prev
= NULL
;
6219 struct ppl_header
*ppl_hdr
= NULL
;
6221 struct imsm_dev
*dev
;
6224 unsigned long long ppl_offset
= 0;
6225 unsigned long long prev_gen_num
= 0;
6227 if (disk
->disk
.raid_disk
< 0)
6230 dev
= get_imsm_dev(super
, info
->container_member
);
6231 idx
= get_imsm_disk_idx(dev
, disk
->disk
.raid_disk
, MAP_0
);
6232 d
= get_imsm_dl_disk(super
, idx
);
6234 if (!d
|| d
->index
< 0 || is_failed(&d
->disk
))
6237 if (posix_memalign(&buf_orig
, MAX_SECTOR_SIZE
, PPL_HEADER_SIZE
* 2)) {
6238 pr_err("Failed to allocate PPL header buffer\n");
6244 while (ppl_offset
< MULTIPLE_PPL_AREA_SIZE_IMSM
) {
6247 dprintf("Checking potential PPL at offset: %llu\n", ppl_offset
);
6249 if (lseek64(d
->fd
, info
->ppl_sector
* 512 + ppl_offset
,
6251 perror("Failed to seek to PPL header location");
6256 if (read(d
->fd
, buf
, PPL_HEADER_SIZE
) != PPL_HEADER_SIZE
) {
6257 perror("Read PPL header failed");
6264 crc
= __le32_to_cpu(ppl_hdr
->checksum
);
6265 ppl_hdr
->checksum
= 0;
6267 if (crc
!= ~crc32c_le(~0, buf
, PPL_HEADER_SIZE
)) {
6268 dprintf("Wrong PPL header checksum on %s\n",
6273 if (prev_gen_num
> __le64_to_cpu(ppl_hdr
->generation
)) {
6274 /* previous was newest, it was already checked */
6278 if ((__le32_to_cpu(ppl_hdr
->signature
) !=
6279 super
->anchor
->orig_family_num
)) {
6280 dprintf("Wrong PPL header signature on %s\n",
6287 prev_gen_num
= __le64_to_cpu(ppl_hdr
->generation
);
6289 ppl_offset
+= PPL_HEADER_SIZE
;
6290 for (i
= 0; i
< __le32_to_cpu(ppl_hdr
->entries_count
); i
++)
6292 __le32_to_cpu(ppl_hdr
->entries
[i
].pp_size
);
6295 buf_prev
= buf
+ PPL_HEADER_SIZE
;
6307 * Update metadata to use mutliple PPLs area (1MB).
6308 * This is done once for all RAID members
6310 if (info
->consistency_policy
== CONSISTENCY_POLICY_PPL
&&
6311 info
->ppl_size
!= (MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9)) {
6313 struct mdinfo
*member_dev
;
6315 sprintf(subarray
, "%d", info
->container_member
);
6317 if (mdmon_running(st
->container_devnm
))
6318 st
->update_tail
= &st
->updates
;
6320 if (st
->ss
->update_subarray(st
, subarray
, "ppl", NULL
)) {
6321 pr_err("Failed to update subarray %s\n",
6324 if (st
->update_tail
)
6325 flush_metadata_updates(st
);
6327 st
->ss
->sync_metadata(st
);
6328 info
->ppl_size
= (MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9);
6329 for (member_dev
= info
->devs
; member_dev
;
6330 member_dev
= member_dev
->next
)
6331 member_dev
->ppl_size
=
6332 (MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9);
6337 struct imsm_map
*map
= get_imsm_map(dev
, MAP_X
);
6339 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
6340 (map
->map_state
== IMSM_T_STATE_NORMAL
&&
6341 !(dev
->vol
.dirty
& RAIDVOL_DIRTY
)) ||
6342 (is_rebuilding(dev
) &&
6343 dev
->vol
.curr_migr_unit
== 0 &&
6344 get_imsm_disk_idx(dev
, disk
->disk
.raid_disk
, MAP_1
) != idx
))
6345 ret
= st
->ss
->write_init_ppl(st
, info
, d
->fd
);
6347 info
->mismatch_cnt
++;
6348 } else if (ret
== 0 &&
6349 ppl_hdr
->entries_count
== 0 &&
6350 is_rebuilding(dev
) &&
6351 info
->resync_start
== 0) {
6353 * The header has no entries - add a single empty entry and
6354 * rewrite the header to prevent the kernel from going into
6355 * resync after an interrupted rebuild.
6357 ppl_hdr
->entries_count
= __cpu_to_le32(1);
6358 ret
= write_ppl_header(info
->ppl_sector
, d
->fd
, buf
);
6366 static int write_init_ppl_imsm_all(struct supertype
*st
, struct mdinfo
*info
)
6368 struct intel_super
*super
= st
->sb
;
6372 if (info
->consistency_policy
!= CONSISTENCY_POLICY_PPL
||
6373 info
->array
.level
!= 5)
6376 for (d
= super
->disks
; d
; d
= d
->next
) {
6377 if (d
->index
< 0 || is_failed(&d
->disk
))
6380 ret
= st
->ss
->write_init_ppl(st
, info
, d
->fd
);
6388 static int write_init_super_imsm(struct supertype
*st
)
6390 struct intel_super
*super
= st
->sb
;
6391 int current_vol
= super
->current_vol
;
6395 getinfo_super_imsm(st
, &info
, NULL
);
6397 /* we are done with current_vol reset it to point st at the container */
6398 super
->current_vol
= -1;
6400 if (st
->update_tail
) {
6401 /* queue the recently created array / added disk
6402 * as a metadata update */
6404 /* determine if we are creating a volume or adding a disk */
6405 if (current_vol
< 0) {
6406 /* in the mgmt (add/remove) disk case we are running
6407 * in mdmon context, so don't close fd's
6411 rv
= write_init_ppl_imsm_all(st
, &info
);
6413 rv
= create_array(st
, current_vol
);
6417 for (d
= super
->disks
; d
; d
= d
->next
)
6418 Kill(d
->devname
, NULL
, 0, -1, 1);
6419 if (current_vol
>= 0)
6420 rv
= write_init_ppl_imsm_all(st
, &info
);
6422 rv
= write_super_imsm(st
, 1);
6428 static int store_super_imsm(struct supertype
*st
, int fd
)
6430 struct intel_super
*super
= st
->sb
;
6431 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
6436 if (super
->sector_size
== 4096)
6437 convert_to_4k(super
);
6438 return store_imsm_mpb(fd
, mpb
);
6441 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
6442 int layout
, int raiddisks
, int chunk
,
6443 unsigned long long size
,
6444 unsigned long long data_offset
,
6446 unsigned long long *freesize
,
6450 unsigned long long ldsize
;
6451 struct intel_super
*super
;
6454 if (level
!= LEVEL_CONTAINER
)
6459 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
6462 pr_err("imsm: Cannot open %s: %s\n",
6463 dev
, strerror(errno
));
6466 if (!get_dev_size(fd
, dev
, &ldsize
)) {
6471 /* capabilities retrieve could be possible
6472 * note that there is no fd for the disks in array.
6474 super
= alloc_super();
6479 if (!get_dev_sector_size(fd
, NULL
, &super
->sector_size
)) {
6485 rv
= find_intel_hba_capability(fd
, super
, verbose
> 0 ? dev
: NULL
);
6489 fd2devname(fd
, str
);
6490 dprintf("fd: %d %s orom: %p rv: %d raiddisk: %d\n",
6491 fd
, str
, super
->orom
, rv
, raiddisks
);
6493 /* no orom/efi or non-intel hba of the disk */
6500 if (raiddisks
> super
->orom
->tds
) {
6502 pr_err("%d exceeds maximum number of platform supported disks: %d\n",
6503 raiddisks
, super
->orom
->tds
);
6507 if ((super
->orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) == 0 &&
6508 (ldsize
>> 9) >> 32 > 0) {
6510 pr_err("%s exceeds maximum platform supported size\n", dev
);
6516 *freesize
= avail_size_imsm(st
, ldsize
>> 9, data_offset
);
6522 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
6524 const unsigned long long base_start
= e
[*idx
].start
;
6525 unsigned long long end
= base_start
+ e
[*idx
].size
;
6528 if (base_start
== end
)
6532 for (i
= *idx
; i
< num_extents
; i
++) {
6533 /* extend overlapping extents */
6534 if (e
[i
].start
>= base_start
&&
6535 e
[i
].start
<= end
) {
6538 if (e
[i
].start
+ e
[i
].size
> end
)
6539 end
= e
[i
].start
+ e
[i
].size
;
6540 } else if (e
[i
].start
> end
) {
6546 return end
- base_start
;
6549 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
6551 /* build a composite disk with all known extents and generate a new
6552 * 'maxsize' given the "all disks in an array must share a common start
6553 * offset" constraint
6555 struct extent
*e
= xcalloc(sum_extents
, sizeof(*e
));
6559 unsigned long long pos
;
6560 unsigned long long start
= 0;
6561 unsigned long long maxsize
;
6562 unsigned long reserve
;
6564 /* coalesce and sort all extents. also, check to see if we need to
6565 * reserve space between member arrays
6568 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6571 for (i
= 0; i
< dl
->extent_cnt
; i
++)
6574 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
6579 while (i
< sum_extents
) {
6580 e
[j
].start
= e
[i
].start
;
6581 e
[j
].size
= find_size(e
, &i
, sum_extents
);
6583 if (e
[j
-1].size
== 0)
6592 unsigned long long esize
;
6594 esize
= e
[i
].start
- pos
;
6595 if (esize
>= maxsize
) {
6600 pos
= e
[i
].start
+ e
[i
].size
;
6602 } while (e
[i
-1].size
);
6608 /* FIXME assumes volume at offset 0 is the first volume in a
6611 if (start_extent
> 0)
6612 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
6616 if (maxsize
< reserve
)
6619 super
->create_offset
= ~((unsigned long long) 0);
6620 if (start
+ reserve
> super
->create_offset
)
6621 return 0; /* start overflows create_offset */
6622 super
->create_offset
= start
+ reserve
;
6624 return maxsize
- reserve
;
6627 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
6629 if (level
< 0 || level
== 6 || level
== 4)
6632 /* if we have an orom prevent invalid raid levels */
6635 case 0: return imsm_orom_has_raid0(orom
);
6638 return imsm_orom_has_raid1e(orom
);
6639 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
6640 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
6641 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
6644 return 1; /* not on an Intel RAID platform so anything goes */
6650 active_arrays_by_format(char *name
, char* hba
, struct md_list
**devlist
,
6651 int dpa
, int verbose
)
6653 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
6654 struct mdstat_ent
*memb
;
6660 for (memb
= mdstat
; memb
; memb
= memb
->next
) {
6661 if (memb
->metadata_version
&&
6662 (strncmp(memb
->metadata_version
, "external:", 9) == 0) &&
6663 (strcmp(&memb
->metadata_version
[9], name
) == 0) &&
6664 !is_subarray(memb
->metadata_version
+9) &&
6666 struct dev_member
*dev
= memb
->members
;
6668 while(dev
&& (fd
< 0)) {
6669 char *path
= xmalloc(strlen(dev
->name
) + strlen("/dev/") + 1);
6670 num
= sprintf(path
, "%s%s", "/dev/", dev
->name
);
6672 fd
= open(path
, O_RDONLY
, 0);
6673 if (num
<= 0 || fd
< 0) {
6674 pr_vrb("Cannot open %s: %s\n",
6675 dev
->name
, strerror(errno
));
6681 if (fd
>= 0 && disk_attached_to_hba(fd
, hba
)) {
6682 struct mdstat_ent
*vol
;
6683 for (vol
= mdstat
; vol
; vol
= vol
->next
) {
6684 if (vol
->active
> 0 &&
6685 vol
->metadata_version
&&
6686 is_container_member(vol
, memb
->devnm
)) {
6691 if (*devlist
&& (found
< dpa
)) {
6692 dv
= xcalloc(1, sizeof(*dv
));
6693 dv
->devname
= xmalloc(strlen(memb
->devnm
) + strlen("/dev/") + 1);
6694 sprintf(dv
->devname
, "%s%s", "/dev/", memb
->devnm
);
6697 dv
->next
= *devlist
;
6705 free_mdstat(mdstat
);
6710 static struct md_list
*
6711 get_loop_devices(void)
6714 struct md_list
*devlist
= NULL
;
6717 for(i
= 0; i
< 12; i
++) {
6718 dv
= xcalloc(1, sizeof(*dv
));
6719 dv
->devname
= xmalloc(40);
6720 sprintf(dv
->devname
, "/dev/loop%d", i
);
6728 static struct md_list
*
6729 get_devices(const char *hba_path
)
6731 struct md_list
*devlist
= NULL
;
6738 devlist
= get_loop_devices();
6741 /* scroll through /sys/dev/block looking for devices attached to
6744 dir
= opendir("/sys/dev/block");
6745 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
6750 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
6752 path
= devt_to_devpath(makedev(major
, minor
));
6755 if (!path_attached_to_hba(path
, hba_path
)) {
6762 fd
= dev_open(ent
->d_name
, O_RDONLY
);
6764 fd2devname(fd
, buf
);
6767 pr_err("cannot open device: %s\n",
6772 dv
= xcalloc(1, sizeof(*dv
));
6773 dv
->devname
= xstrdup(buf
);
6780 devlist
= devlist
->next
;
6790 count_volumes_list(struct md_list
*devlist
, char *homehost
,
6791 int verbose
, int *found
)
6793 struct md_list
*tmpdev
;
6795 struct supertype
*st
;
6797 /* first walk the list of devices to find a consistent set
6798 * that match the criterea, if that is possible.
6799 * We flag the ones we like with 'used'.
6802 st
= match_metadata_desc_imsm("imsm");
6804 pr_vrb("cannot allocate memory for imsm supertype\n");
6808 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
6809 char *devname
= tmpdev
->devname
;
6811 struct supertype
*tst
;
6813 if (tmpdev
->used
> 1)
6815 tst
= dup_super(st
);
6817 pr_vrb("cannot allocate memory for imsm supertype\n");
6820 tmpdev
->container
= 0;
6821 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
6823 dprintf("cannot open device %s: %s\n",
6824 devname
, strerror(errno
));
6826 } else if (!fstat_is_blkdev(dfd
, devname
, &rdev
)) {
6828 } else if (must_be_container(dfd
)) {
6829 struct supertype
*cst
;
6830 cst
= super_by_fd(dfd
, NULL
);
6832 dprintf("cannot recognize container type %s\n",
6835 } else if (tst
->ss
!= st
->ss
) {
6836 dprintf("non-imsm container - ignore it: %s\n",
6839 } else if (!tst
->ss
->load_container
||
6840 tst
->ss
->load_container(tst
, dfd
, NULL
))
6843 tmpdev
->container
= 1;
6846 cst
->ss
->free_super(cst
);
6848 tmpdev
->st_rdev
= rdev
;
6849 if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
6850 dprintf("no RAID superblock on %s\n",
6853 } else if (tst
->ss
->compare_super
== NULL
) {
6854 dprintf("Cannot assemble %s metadata on %s\n",
6855 tst
->ss
->name
, devname
);
6861 if (tmpdev
->used
== 2 || tmpdev
->used
== 4) {
6862 /* Ignore unrecognised devices during auto-assembly */
6867 tst
->ss
->getinfo_super(tst
, &info
, NULL
);
6869 if (st
->minor_version
== -1)
6870 st
->minor_version
= tst
->minor_version
;
6872 if (memcmp(info
.uuid
, uuid_zero
,
6873 sizeof(int[4])) == 0) {
6874 /* this is a floating spare. It cannot define
6875 * an array unless there are no more arrays of
6876 * this type to be found. It can be included
6877 * in an array of this type though.
6883 if (st
->ss
!= tst
->ss
||
6884 st
->minor_version
!= tst
->minor_version
||
6885 st
->ss
->compare_super(st
, tst
) != 0) {
6886 /* Some mismatch. If exactly one array matches this host,
6887 * we can resolve on that one.
6888 * Or, if we are auto assembling, we just ignore the second
6891 dprintf("superblock on %s doesn't match others - assembly aborted\n",
6897 dprintf("found: devname: %s\n", devname
);
6901 tst
->ss
->free_super(tst
);
6905 if ((err
= load_super_imsm_all(st
, -1, &st
->sb
, NULL
, devlist
, 0)) == 0) {
6906 struct mdinfo
*iter
, *head
= st
->ss
->container_content(st
, NULL
);
6907 for (iter
= head
; iter
; iter
= iter
->next
) {
6908 dprintf("content->text_version: %s vol\n",
6909 iter
->text_version
);
6910 if (iter
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
6911 /* do not assemble arrays with unsupported
6913 dprintf("Cannot activate member %s.\n",
6914 iter
->text_version
);
6921 dprintf("No valid super block on device list: err: %d %p\n",
6925 dprintf("no more devices to examine\n");
6928 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
6929 if (tmpdev
->used
== 1 && tmpdev
->found
) {
6931 if (count
< tmpdev
->found
)
6934 count
-= tmpdev
->found
;
6937 if (tmpdev
->used
== 1)
6942 st
->ss
->free_super(st
);
6946 static int __count_volumes(char *hba_path
, int dpa
, int verbose
,
6949 struct sys_dev
*idev
, *intel_devices
= find_intel_devices();
6951 const struct orom_entry
*entry
;
6952 struct devid_list
*dv
, *devid_list
;
6957 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
6958 if (strstr(idev
->path
, hba_path
))
6962 if (!idev
|| !idev
->dev_id
)
6965 entry
= get_orom_entry_by_device_id(idev
->dev_id
);
6967 if (!entry
|| !entry
->devid_list
)
6970 devid_list
= entry
->devid_list
;
6971 for (dv
= devid_list
; dv
; dv
= dv
->next
) {
6972 struct md_list
*devlist
;
6973 struct sys_dev
*device
= NULL
;
6978 device
= device_by_id_and_path(dv
->devid
, hba_path
);
6980 device
= device_by_id(dv
->devid
);
6983 hpath
= device
->path
;
6987 devlist
= get_devices(hpath
);
6988 /* if no intel devices return zero volumes */
6989 if (devlist
== NULL
)
6992 count
+= active_arrays_by_format("imsm", hpath
, &devlist
, dpa
,
6994 dprintf("path: %s active arrays: %d\n", hpath
, count
);
6995 if (devlist
== NULL
)
6999 count
+= count_volumes_list(devlist
,
7003 dprintf("found %d count: %d\n", found
, count
);
7006 dprintf("path: %s total number of volumes: %d\n", hpath
, count
);
7009 struct md_list
*dv
= devlist
;
7010 devlist
= devlist
->next
;
7018 static int count_volumes(struct intel_hba
*hba
, int dpa
, int verbose
)
7022 if (hba
->type
== SYS_DEV_VMD
) {
7023 struct sys_dev
*dev
;
7026 for (dev
= find_intel_devices(); dev
; dev
= dev
->next
) {
7027 if (dev
->type
== SYS_DEV_VMD
)
7028 count
+= __count_volumes(dev
->path
, dpa
,
7033 return __count_volumes(hba
->path
, dpa
, verbose
, 0);
7036 static int imsm_default_chunk(const struct imsm_orom
*orom
)
7038 /* up to 512 if the plaform supports it, otherwise the platform max.
7039 * 128 if no platform detected
7041 int fs
= max(7, orom
? fls(orom
->sss
) : 0);
7043 return min(512, (1 << fs
));
7047 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
7048 int raiddisks
, int *chunk
, unsigned long long size
, int verbose
)
7050 /* check/set platform and metadata limits/defaults */
7051 if (super
->orom
&& raiddisks
> super
->orom
->dpa
) {
7052 pr_vrb("platform supports a maximum of %d disks per array\n",
7057 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
7058 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
7059 pr_vrb("platform does not support raid%d with %d disk%s\n",
7060 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
7064 if (*chunk
== 0 || *chunk
== UnSet
)
7065 *chunk
= imsm_default_chunk(super
->orom
);
7067 if (super
->orom
&& !imsm_orom_has_chunk(super
->orom
, *chunk
)) {
7068 pr_vrb("platform does not support a chunk size of: %d\n", *chunk
);
7072 if (layout
!= imsm_level_to_layout(level
)) {
7074 pr_vrb("imsm raid 5 only supports the left-asymmetric layout\n");
7075 else if (level
== 10)
7076 pr_vrb("imsm raid 10 only supports the n2 layout\n");
7078 pr_vrb("imsm unknown layout %#x for this raid level %d\n",
7083 if (super
->orom
&& (super
->orom
->attr
& IMSM_OROM_ATTR_2TB
) == 0 &&
7084 (calc_array_size(level
, raiddisks
, layout
, *chunk
, size
) >> 32) > 0) {
7085 pr_vrb("platform does not support a volume size over 2TB\n");
7092 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
7093 * FIX ME add ahci details
7095 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
7096 int layout
, int raiddisks
, int *chunk
,
7097 unsigned long long size
,
7098 unsigned long long data_offset
,
7100 unsigned long long *freesize
,
7104 struct intel_super
*super
= st
->sb
;
7105 struct imsm_super
*mpb
;
7107 unsigned long long pos
= 0;
7108 unsigned long long maxsize
;
7112 /* We must have the container info already read in. */
7116 mpb
= super
->anchor
;
7118 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, size
, verbose
)) {
7119 pr_err("RAID geometry validation failed. Cannot proceed with the action(s).\n");
7123 /* General test: make sure there is space for
7124 * 'raiddisks' device extents of size 'size' at a given
7127 unsigned long long minsize
= size
;
7128 unsigned long long start_offset
= MaxSector
;
7131 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
7132 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7137 e
= get_extents(super
, dl
);
7140 unsigned long long esize
;
7141 esize
= e
[i
].start
- pos
;
7142 if (esize
>= minsize
)
7144 if (found
&& start_offset
== MaxSector
) {
7147 } else if (found
&& pos
!= start_offset
) {
7151 pos
= e
[i
].start
+ e
[i
].size
;
7153 } while (e
[i
-1].size
);
7158 if (dcnt
< raiddisks
) {
7160 pr_err("imsm: Not enough devices with space for this array (%d < %d)\n",
7167 /* This device must be a member of the set */
7168 if (!stat_is_blkdev(dev
, &rdev
))
7170 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7171 if (dl
->major
== (int)major(rdev
) &&
7172 dl
->minor
== (int)minor(rdev
))
7177 pr_err("%s is not in the same imsm set\n", dev
);
7179 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
7180 /* If a volume is present then the current creation attempt
7181 * cannot incorporate new spares because the orom may not
7182 * understand this configuration (all member disks must be
7183 * members of each array in the container).
7185 pr_err("%s is a spare and a volume is already defined for this container\n", dev
);
7186 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
7188 } else if (super
->orom
&& mpb
->num_raid_devs
> 0 &&
7189 mpb
->num_disks
!= raiddisks
) {
7190 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
7194 /* retrieve the largest free space block */
7195 e
= get_extents(super
, dl
);
7200 unsigned long long esize
;
7202 esize
= e
[i
].start
- pos
;
7203 if (esize
>= maxsize
)
7205 pos
= e
[i
].start
+ e
[i
].size
;
7207 } while (e
[i
-1].size
);
7212 pr_err("unable to determine free space for: %s\n",
7216 if (maxsize
< size
) {
7218 pr_err("%s not enough space (%llu < %llu)\n",
7219 dev
, maxsize
, size
);
7223 /* count total number of extents for merge */
7225 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7227 i
+= dl
->extent_cnt
;
7229 maxsize
= merge_extents(super
, i
);
7231 if (!check_env("IMSM_NO_PLATFORM") &&
7232 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
7233 pr_err("attempting to create a second volume with size less then remaining space. Aborting...\n");
7237 if (maxsize
< size
|| maxsize
== 0) {
7240 pr_err("no free space left on device. Aborting...\n");
7242 pr_err("not enough space to create volume of given size (%llu < %llu). Aborting...\n",
7248 *freesize
= maxsize
;
7251 int count
= count_volumes(super
->hba
,
7252 super
->orom
->dpa
, verbose
);
7253 if (super
->orom
->vphba
<= count
) {
7254 pr_vrb("platform does not support more than %d raid volumes.\n",
7255 super
->orom
->vphba
);
7262 static int imsm_get_free_size(struct supertype
*st
, int raiddisks
,
7263 unsigned long long size
, int chunk
,
7264 unsigned long long *freesize
)
7266 struct intel_super
*super
= st
->sb
;
7267 struct imsm_super
*mpb
= super
->anchor
;
7272 unsigned long long maxsize
;
7273 unsigned long long minsize
;
7277 /* find the largest common start free region of the possible disks */
7281 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7287 /* don't activate new spares if we are orom constrained
7288 * and there is already a volume active in the container
7290 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
7293 e
= get_extents(super
, dl
);
7296 for (i
= 1; e
[i
-1].size
; i
++)
7304 maxsize
= merge_extents(super
, extent_cnt
);
7308 minsize
= chunk
* 2;
7310 if (cnt
< raiddisks
||
7311 (super
->orom
&& used
&& used
!= raiddisks
) ||
7312 maxsize
< minsize
||
7314 pr_err("not enough devices with space to create array.\n");
7315 return 0; /* No enough free spaces large enough */
7326 if (!check_env("IMSM_NO_PLATFORM") &&
7327 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
7328 pr_err("attempting to create a second volume with size less then remaining space. Aborting...\n");
7332 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7334 dl
->raiddisk
= cnt
++;
7338 dprintf("imsm: imsm_get_free_size() returns : %llu\n", size
);
7343 static int reserve_space(struct supertype
*st
, int raiddisks
,
7344 unsigned long long size
, int chunk
,
7345 unsigned long long *freesize
)
7347 struct intel_super
*super
= st
->sb
;
7352 rv
= imsm_get_free_size(st
, raiddisks
, size
, chunk
, freesize
);
7355 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7357 dl
->raiddisk
= cnt
++;
7364 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
7365 int raiddisks
, int *chunk
, unsigned long long size
,
7366 unsigned long long data_offset
,
7367 char *dev
, unsigned long long *freesize
,
7368 int consistency_policy
, int verbose
)
7375 * if given unused devices create a container
7376 * if given given devices in a container create a member volume
7378 if (level
== LEVEL_CONTAINER
) {
7379 /* Must be a fresh device to add to a container */
7380 return validate_geometry_imsm_container(st
, level
, layout
,
7388 if (size
&& ((size
< 1024) || (*chunk
!= UnSet
&&
7389 size
< (unsigned long long) *chunk
))) {
7390 pr_err("Given size must be greater than 1M and chunk size.\n");
7391 /* Depends on algorithm in Create.c :
7392 * if container was given (dev == NULL) return -1,
7393 * if block device was given ( dev != NULL) return 0.
7395 return dev
? -1 : 0;
7400 struct intel_super
*super
= st
->sb
;
7401 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
7402 raiddisks
, chunk
, size
,
7405 /* we are being asked to automatically layout a
7406 * new volume based on the current contents of
7407 * the container. If the the parameters can be
7408 * satisfied reserve_space will record the disks,
7409 * start offset, and size of the volume to be
7410 * created. add_to_super and getinfo_super
7411 * detect when autolayout is in progress.
7413 /* assuming that freesize is always given when array is
7415 if (super
->orom
&& freesize
) {
7417 count
= count_volumes(super
->hba
,
7418 super
->orom
->dpa
, verbose
);
7419 if (super
->orom
->vphba
<= count
) {
7420 pr_vrb("platform does not support more than %d raid volumes.\n",
7421 super
->orom
->vphba
);
7426 return reserve_space(st
, raiddisks
, size
,
7432 /* creating in a given container */
7433 return validate_geometry_imsm_volume(st
, level
, layout
,
7434 raiddisks
, chunk
, size
,
7436 dev
, freesize
, verbose
);
7439 /* This device needs to be a device in an 'imsm' container */
7440 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
7443 pr_err("Cannot create this array on device %s\n",
7448 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
7450 pr_err("Cannot open %s: %s\n",
7451 dev
, strerror(errno
));
7454 /* Well, it is in use by someone, maybe an 'imsm' container. */
7455 cfd
= open_container(fd
);
7459 pr_err("Cannot use %s: It is busy\n",
7463 sra
= sysfs_read(cfd
, NULL
, GET_VERSION
);
7464 if (sra
&& sra
->array
.major_version
== -1 &&
7465 strcmp(sra
->text_version
, "imsm") == 0)
7469 /* This is a member of a imsm container. Load the container
7470 * and try to create a volume
7472 struct intel_super
*super
;
7474 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
, NULL
, 1) == 0) {
7476 strcpy(st
->container_devnm
, fd2devnm(cfd
));
7478 return validate_geometry_imsm_volume(st
, level
, layout
,
7480 size
, data_offset
, dev
,
7487 pr_err("failed container membership check\n");
7493 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
7495 struct intel_super
*super
= st
->sb
;
7497 if (level
&& *level
== UnSet
)
7498 *level
= LEVEL_CONTAINER
;
7500 if (level
&& layout
&& *layout
== UnSet
)
7501 *layout
= imsm_level_to_layout(*level
);
7503 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0))
7504 *chunk
= imsm_default_chunk(super
->orom
);
7507 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
7509 static int kill_subarray_imsm(struct supertype
*st
)
7511 /* remove the subarray currently referenced by ->current_vol */
7513 struct intel_dev
**dp
;
7514 struct intel_super
*super
= st
->sb
;
7515 __u8 current_vol
= super
->current_vol
;
7516 struct imsm_super
*mpb
= super
->anchor
;
7518 if (super
->current_vol
< 0)
7520 super
->current_vol
= -1; /* invalidate subarray cursor */
7522 /* block deletions that would change the uuid of active subarrays
7524 * FIXME when immutable ids are available, but note that we'll
7525 * also need to fixup the invalidated/active subarray indexes in
7528 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7531 if (i
< current_vol
)
7533 sprintf(subarray
, "%u", i
);
7534 if (is_subarray_active(subarray
, st
->devnm
)) {
7535 pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
7542 if (st
->update_tail
) {
7543 struct imsm_update_kill_array
*u
= xmalloc(sizeof(*u
));
7545 u
->type
= update_kill_array
;
7546 u
->dev_idx
= current_vol
;
7547 append_metadata_update(st
, u
, sizeof(*u
));
7552 for (dp
= &super
->devlist
; *dp
;)
7553 if ((*dp
)->index
== current_vol
) {
7556 handle_missing(super
, (*dp
)->dev
);
7557 if ((*dp
)->index
> current_vol
)
7562 /* no more raid devices, all active components are now spares,
7563 * but of course failed are still failed
7565 if (--mpb
->num_raid_devs
== 0) {
7568 for (d
= super
->disks
; d
; d
= d
->next
)
7573 super
->updates_pending
++;
7578 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
7579 char *update
, struct mddev_ident
*ident
)
7581 /* update the subarray currently referenced by ->current_vol */
7582 struct intel_super
*super
= st
->sb
;
7583 struct imsm_super
*mpb
= super
->anchor
;
7585 if (strcmp(update
, "name") == 0) {
7586 char *name
= ident
->name
;
7590 if (is_subarray_active(subarray
, st
->devnm
)) {
7591 pr_err("Unable to update name of active subarray\n");
7595 if (!check_name(super
, name
, 0))
7598 vol
= strtoul(subarray
, &ep
, 10);
7599 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
7602 if (st
->update_tail
) {
7603 struct imsm_update_rename_array
*u
= xmalloc(sizeof(*u
));
7605 u
->type
= update_rename_array
;
7607 strncpy((char *) u
->name
, name
, MAX_RAID_SERIAL_LEN
);
7608 u
->name
[MAX_RAID_SERIAL_LEN
-1] = '\0';
7609 append_metadata_update(st
, u
, sizeof(*u
));
7611 struct imsm_dev
*dev
;
7614 dev
= get_imsm_dev(super
, vol
);
7615 memset(dev
->volume
, '\0', MAX_RAID_SERIAL_LEN
);
7616 namelen
= min((int)strlen(name
), MAX_RAID_SERIAL_LEN
);
7617 memcpy(dev
->volume
, name
, namelen
);
7618 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7619 dev
= get_imsm_dev(super
, i
);
7620 handle_missing(super
, dev
);
7622 super
->updates_pending
++;
7624 } else if (strcmp(update
, "ppl") == 0 ||
7625 strcmp(update
, "no-ppl") == 0) {
7628 int vol
= strtoul(subarray
, &ep
, 10);
7630 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
7633 if (strcmp(update
, "ppl") == 0)
7634 new_policy
= RWH_MULTIPLE_DISTRIBUTED
;
7636 new_policy
= RWH_MULTIPLE_OFF
;
7638 if (st
->update_tail
) {
7639 struct imsm_update_rwh_policy
*u
= xmalloc(sizeof(*u
));
7641 u
->type
= update_rwh_policy
;
7643 u
->new_policy
= new_policy
;
7644 append_metadata_update(st
, u
, sizeof(*u
));
7646 struct imsm_dev
*dev
;
7648 dev
= get_imsm_dev(super
, vol
);
7649 dev
->rwh_policy
= new_policy
;
7650 super
->updates_pending
++;
7658 static int is_gen_migration(struct imsm_dev
*dev
)
7663 if (!dev
->vol
.migr_state
)
7666 if (migr_type(dev
) == MIGR_GEN_MIGR
)
7672 static int is_rebuilding(struct imsm_dev
*dev
)
7674 struct imsm_map
*migr_map
;
7676 if (!dev
->vol
.migr_state
)
7679 if (migr_type(dev
) != MIGR_REBUILD
)
7682 migr_map
= get_imsm_map(dev
, MAP_1
);
7684 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
7690 static int is_initializing(struct imsm_dev
*dev
)
7692 struct imsm_map
*migr_map
;
7694 if (!dev
->vol
.migr_state
)
7697 if (migr_type(dev
) != MIGR_INIT
)
7700 migr_map
= get_imsm_map(dev
, MAP_1
);
7702 if (migr_map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
7708 static void update_recovery_start(struct intel_super
*super
,
7709 struct imsm_dev
*dev
,
7710 struct mdinfo
*array
)
7712 struct mdinfo
*rebuild
= NULL
;
7716 if (!is_rebuilding(dev
))
7719 /* Find the rebuild target, but punt on the dual rebuild case */
7720 for (d
= array
->devs
; d
; d
= d
->next
)
7721 if (d
->recovery_start
== 0) {
7728 /* (?) none of the disks are marked with
7729 * IMSM_ORD_REBUILD, so assume they are missing and the
7730 * disk_ord_tbl was not correctly updated
7732 dprintf("failed to locate out-of-sync disk\n");
7736 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
7737 rebuild
->recovery_start
= units
* blocks_per_migr_unit(super
, dev
);
7740 static int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
);
7742 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
7744 /* Given a container loaded by load_super_imsm_all,
7745 * extract information about all the arrays into
7747 * If 'subarray' is given, just extract info about that array.
7749 * For each imsm_dev create an mdinfo, fill it in,
7750 * then look for matching devices in super->disks
7751 * and create appropriate device mdinfo.
7753 struct intel_super
*super
= st
->sb
;
7754 struct imsm_super
*mpb
= super
->anchor
;
7755 struct mdinfo
*rest
= NULL
;
7759 int spare_disks
= 0;
7761 /* do not assemble arrays when not all attributes are supported */
7762 if (imsm_check_attributes(mpb
->attributes
) == 0) {
7764 pr_err("Unsupported attributes in IMSM metadata.Arrays activation is blocked.\n");
7767 /* count spare devices, not used in maps
7769 for (d
= super
->disks
; d
; d
= d
->next
)
7773 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7774 struct imsm_dev
*dev
;
7775 struct imsm_map
*map
;
7776 struct imsm_map
*map2
;
7777 struct mdinfo
*this;
7784 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
7787 dev
= get_imsm_dev(super
, i
);
7788 map
= get_imsm_map(dev
, MAP_0
);
7789 map2
= get_imsm_map(dev
, MAP_1
);
7790 level
= get_imsm_raid_level(map
);
7792 /* do not publish arrays that are in the middle of an
7793 * unsupported migration
7795 if (dev
->vol
.migr_state
&&
7796 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
7797 pr_err("cannot assemble volume '%.16s': unsupported migration in progress\n",
7801 /* do not publish arrays that are not support by controller's
7805 this = xmalloc(sizeof(*this));
7807 super
->current_vol
= i
;
7808 getinfo_super_imsm_volume(st
, this, NULL
);
7810 chunk
= __le16_to_cpu(map
->blocks_per_strip
) >> 1;
7811 /* mdadm does not support all metadata features- set the bit in all arrays state */
7812 if (!validate_geometry_imsm_orom(super
,
7813 level
, /* RAID level */
7814 imsm_level_to_layout(level
),
7815 map
->num_members
, /* raid disks */
7816 &chunk
, imsm_dev_size(dev
),
7818 pr_err("IMSM RAID geometry validation failed. Array %s activation is blocked.\n",
7820 this->array
.state
|=
7821 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
7822 (1<<MD_SB_BLOCK_VOLUME
);
7825 /* if array has bad blocks, set suitable bit in all arrays state */
7827 this->array
.state
|=
7828 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
7829 (1<<MD_SB_BLOCK_VOLUME
);
7831 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
7832 unsigned long long recovery_start
;
7833 struct mdinfo
*info_d
;
7841 idx
= get_imsm_disk_idx(dev
, slot
, MAP_0
);
7842 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
7843 for (d
= super
->disks
; d
; d
= d
->next
)
7844 if (d
->index
== idx
)
7847 recovery_start
= MaxSector
;
7850 if (d
&& is_failed(&d
->disk
))
7852 if (!skip
&& (ord
& IMSM_ORD_REBUILD
))
7856 * if we skip some disks the array will be assmebled degraded;
7857 * reset resync start to avoid a dirty-degraded
7858 * situation when performing the intial sync
7863 if (!(dev
->vol
.dirty
& RAIDVOL_DIRTY
)) {
7864 if ((!able_to_resync(level
, missing
) ||
7865 recovery_start
== 0))
7866 this->resync_start
= MaxSector
;
7869 * FIXME handle dirty degraded
7876 info_d
= xcalloc(1, sizeof(*info_d
));
7877 info_d
->next
= this->devs
;
7878 this->devs
= info_d
;
7880 info_d
->disk
.number
= d
->index
;
7881 info_d
->disk
.major
= d
->major
;
7882 info_d
->disk
.minor
= d
->minor
;
7883 info_d
->disk
.raid_disk
= slot
;
7884 info_d
->recovery_start
= recovery_start
;
7886 if (slot
< map2
->num_members
)
7887 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
7889 this->array
.spare_disks
++;
7891 if (slot
< map
->num_members
)
7892 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
7894 this->array
.spare_disks
++;
7896 if (info_d
->recovery_start
== MaxSector
)
7897 this->array
.working_disks
++;
7899 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
7900 info_d
->data_offset
= pba_of_lba0(map
);
7901 info_d
->component_size
= calc_component_size(map
, dev
);
7903 if (map
->raid_level
== 5) {
7904 info_d
->ppl_sector
= this->ppl_sector
;
7905 info_d
->ppl_size
= this->ppl_size
;
7906 if (this->consistency_policy
== CONSISTENCY_POLICY_PPL
&&
7907 recovery_start
== 0)
7908 this->resync_start
= 0;
7911 info_d
->bb
.supported
= 1;
7912 get_volume_badblocks(super
->bbm_log
, ord_to_idx(ord
),
7913 info_d
->data_offset
,
7914 info_d
->component_size
,
7917 /* now that the disk list is up-to-date fixup recovery_start */
7918 update_recovery_start(super
, dev
, this);
7919 this->array
.spare_disks
+= spare_disks
;
7921 /* check for reshape */
7922 if (this->reshape_active
== 1)
7923 recover_backup_imsm(st
, this);
7930 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
7931 int failed
, int look_in_map
)
7933 struct imsm_map
*map
;
7935 map
= get_imsm_map(dev
, look_in_map
);
7938 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
7939 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
7941 switch (get_imsm_raid_level(map
)) {
7943 return IMSM_T_STATE_FAILED
;
7946 if (failed
< map
->num_members
)
7947 return IMSM_T_STATE_DEGRADED
;
7949 return IMSM_T_STATE_FAILED
;
7954 * check to see if any mirrors have failed, otherwise we
7955 * are degraded. Even numbered slots are mirrored on
7959 /* gcc -Os complains that this is unused */
7960 int insync
= insync
;
7962 for (i
= 0; i
< map
->num_members
; i
++) {
7963 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_X
);
7964 int idx
= ord_to_idx(ord
);
7965 struct imsm_disk
*disk
;
7967 /* reset the potential in-sync count on even-numbered
7968 * slots. num_copies is always 2 for imsm raid10
7973 disk
= get_imsm_disk(super
, idx
);
7974 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
7977 /* no in-sync disks left in this mirror the
7981 return IMSM_T_STATE_FAILED
;
7984 return IMSM_T_STATE_DEGRADED
;
7988 return IMSM_T_STATE_DEGRADED
;
7990 return IMSM_T_STATE_FAILED
;
7996 return map
->map_state
;
7999 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
8004 struct imsm_disk
*disk
;
8005 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8006 struct imsm_map
*prev
= get_imsm_map(dev
, MAP_1
);
8007 struct imsm_map
*map_for_loop
;
8012 /* at the beginning of migration we set IMSM_ORD_REBUILD on
8013 * disks that are being rebuilt. New failures are recorded to
8014 * map[0]. So we look through all the disks we started with and
8015 * see if any failures are still present, or if any new ones
8019 if (prev
&& (map
->num_members
< prev
->num_members
))
8020 map_for_loop
= prev
;
8022 for (i
= 0; i
< map_for_loop
->num_members
; i
++) {
8024 /* when MAP_X is passed both maps failures are counted
8027 (look_in_map
== MAP_1
|| look_in_map
== MAP_X
) &&
8028 i
< prev
->num_members
) {
8029 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
8030 idx_1
= ord_to_idx(ord
);
8032 disk
= get_imsm_disk(super
, idx_1
);
8033 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
8036 if ((look_in_map
== MAP_0
|| look_in_map
== MAP_X
) &&
8037 i
< map
->num_members
) {
8038 ord
= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
8039 idx
= ord_to_idx(ord
);
8042 disk
= get_imsm_disk(super
, idx
);
8043 if (!disk
|| is_failed(disk
) ||
8044 ord
& IMSM_ORD_REBUILD
)
8053 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
8056 struct intel_super
*super
= c
->sb
;
8057 struct imsm_super
*mpb
= super
->anchor
;
8058 struct imsm_update_prealloc_bb_mem u
;
8060 if (atoi(inst
) >= mpb
->num_raid_devs
) {
8061 pr_err("subarry index %d, out of range\n", atoi(inst
));
8065 dprintf("imsm: open_new %s\n", inst
);
8066 a
->info
.container_member
= atoi(inst
);
8068 u
.type
= update_prealloc_badblocks_mem
;
8069 imsm_update_metadata_locally(c
, &u
, sizeof(u
));
8074 static int is_resyncing(struct imsm_dev
*dev
)
8076 struct imsm_map
*migr_map
;
8078 if (!dev
->vol
.migr_state
)
8081 if (migr_type(dev
) == MIGR_INIT
||
8082 migr_type(dev
) == MIGR_REPAIR
)
8085 if (migr_type(dev
) == MIGR_GEN_MIGR
)
8088 migr_map
= get_imsm_map(dev
, MAP_1
);
8090 if (migr_map
->map_state
== IMSM_T_STATE_NORMAL
&&
8091 dev
->vol
.migr_type
!= MIGR_GEN_MIGR
)
8097 /* return true if we recorded new information */
8098 static int mark_failure(struct intel_super
*super
,
8099 struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
8103 struct imsm_map
*map
;
8104 char buf
[MAX_RAID_SERIAL_LEN
+3];
8105 unsigned int len
, shift
= 0;
8107 /* new failures are always set in map[0] */
8108 map
= get_imsm_map(dev
, MAP_0
);
8110 slot
= get_imsm_disk_slot(map
, idx
);
8114 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
8115 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
8118 memcpy(buf
, disk
->serial
, MAX_RAID_SERIAL_LEN
);
8119 buf
[MAX_RAID_SERIAL_LEN
] = '\000';
8121 if ((len
= strlen(buf
)) >= MAX_RAID_SERIAL_LEN
)
8122 shift
= len
- MAX_RAID_SERIAL_LEN
+ 1;
8123 memcpy(disk
->serial
, &buf
[shift
], len
+ 1 - shift
);
8125 disk
->status
|= FAILED_DISK
;
8126 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
8127 /* mark failures in second map if second map exists and this disk
8129 * This is valid for migration, initialization and rebuild
8131 if (dev
->vol
.migr_state
) {
8132 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
8133 int slot2
= get_imsm_disk_slot(map2
, idx
);
8135 if (slot2
< map2
->num_members
&& slot2
>= 0)
8136 set_imsm_ord_tbl_ent(map2
, slot2
,
8137 idx
| IMSM_ORD_REBUILD
);
8139 if (map
->failed_disk_num
== 0xff)
8140 map
->failed_disk_num
= slot
;
8142 clear_disk_badblocks(super
->bbm_log
, ord_to_idx(ord
));
8147 static void mark_missing(struct intel_super
*super
,
8148 struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
8150 mark_failure(super
, dev
, disk
, idx
);
8152 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
8155 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
8156 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
8159 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
8163 if (!super
->missing
)
8166 /* When orom adds replacement for missing disk it does
8167 * not remove entry of missing disk, but just updates map with
8168 * new added disk. So it is not enough just to test if there is
8169 * any missing disk, we have to look if there are any failed disks
8170 * in map to stop migration */
8172 dprintf("imsm: mark missing\n");
8173 /* end process for initialization and rebuild only
8175 if (is_gen_migration(dev
) == 0) {
8176 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
8180 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8181 struct imsm_map
*map1
;
8182 int i
, ord
, ord_map1
;
8185 for (i
= 0; i
< map
->num_members
; i
++) {
8186 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
8187 if (!(ord
& IMSM_ORD_REBUILD
))
8190 map1
= get_imsm_map(dev
, MAP_1
);
8194 ord_map1
= __le32_to_cpu(map1
->disk_ord_tbl
[i
]);
8195 if (ord_map1
& IMSM_ORD_REBUILD
)
8200 map_state
= imsm_check_degraded(super
, dev
,
8202 end_migration(dev
, super
, map_state
);
8206 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
8207 mark_missing(super
, dev
, &dl
->disk
, dl
->index
);
8208 super
->updates_pending
++;
8211 static unsigned long long imsm_set_array_size(struct imsm_dev
*dev
,
8214 unsigned long long array_blocks
;
8215 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8216 int used_disks
= imsm_num_data_members(map
);
8218 if (used_disks
== 0) {
8219 /* when problems occures
8220 * return current array_blocks value
8222 array_blocks
= imsm_dev_size(dev
);
8224 return array_blocks
;
8227 /* set array size in metadata
8230 /* OLCE size change is caused by added disks
8232 array_blocks
= per_dev_array_size(map
) * used_disks
;
8234 /* Online Volume Size Change
8235 * Using available free space
8237 array_blocks
= new_size
;
8239 array_blocks
= round_size_to_mb(array_blocks
, used_disks
);
8240 set_imsm_dev_size(dev
, array_blocks
);
8242 return array_blocks
;
8245 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
8247 static void imsm_progress_container_reshape(struct intel_super
*super
)
8249 /* if no device has a migr_state, but some device has a
8250 * different number of members than the previous device, start
8251 * changing the number of devices in this device to match
8254 struct imsm_super
*mpb
= super
->anchor
;
8255 int prev_disks
= -1;
8259 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8260 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
8261 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8262 struct imsm_map
*map2
;
8263 int prev_num_members
;
8265 if (dev
->vol
.migr_state
)
8268 if (prev_disks
== -1)
8269 prev_disks
= map
->num_members
;
8270 if (prev_disks
== map
->num_members
)
8273 /* OK, this array needs to enter reshape mode.
8274 * i.e it needs a migr_state
8277 copy_map_size
= sizeof_imsm_map(map
);
8278 prev_num_members
= map
->num_members
;
8279 map
->num_members
= prev_disks
;
8280 dev
->vol
.migr_state
= 1;
8281 dev
->vol
.curr_migr_unit
= 0;
8282 set_migr_type(dev
, MIGR_GEN_MIGR
);
8283 for (i
= prev_num_members
;
8284 i
< map
->num_members
; i
++)
8285 set_imsm_ord_tbl_ent(map
, i
, i
);
8286 map2
= get_imsm_map(dev
, MAP_1
);
8287 /* Copy the current map */
8288 memcpy(map2
, map
, copy_map_size
);
8289 map2
->num_members
= prev_num_members
;
8291 imsm_set_array_size(dev
, -1);
8292 super
->clean_migration_record_by_mdmon
= 1;
8293 super
->updates_pending
++;
8297 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
8298 * states are handled in imsm_set_disk() with one exception, when a
8299 * resync is stopped due to a new failure this routine will set the
8300 * 'degraded' state for the array.
8302 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
8304 int inst
= a
->info
.container_member
;
8305 struct intel_super
*super
= a
->container
->sb
;
8306 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8307 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8308 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
8309 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8310 __u32 blocks_per_unit
;
8312 if (dev
->vol
.migr_state
&&
8313 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
8314 /* array state change is blocked due to reshape action
8316 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
8317 * - finish the reshape (if last_checkpoint is big and action != reshape)
8318 * - update curr_migr_unit
8320 if (a
->curr_action
== reshape
) {
8321 /* still reshaping, maybe update curr_migr_unit */
8322 goto mark_checkpoint
;
8324 if (a
->last_checkpoint
== 0 && a
->prev_action
== reshape
) {
8325 /* for some reason we aborted the reshape.
8327 * disable automatic metadata rollback
8328 * user action is required to recover process
8331 struct imsm_map
*map2
=
8332 get_imsm_map(dev
, MAP_1
);
8333 dev
->vol
.migr_state
= 0;
8334 set_migr_type(dev
, 0);
8335 dev
->vol
.curr_migr_unit
= 0;
8337 sizeof_imsm_map(map2
));
8338 super
->updates_pending
++;
8341 if (a
->last_checkpoint
>= a
->info
.component_size
) {
8342 unsigned long long array_blocks
;
8346 used_disks
= imsm_num_data_members(map
);
8347 if (used_disks
> 0) {
8349 per_dev_array_size(map
) *
8352 round_size_to_mb(array_blocks
,
8354 a
->info
.custom_array_size
= array_blocks
;
8355 /* encourage manager to update array
8359 a
->check_reshape
= 1;
8361 /* finalize online capacity expansion/reshape */
8362 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
8364 mdi
->disk
.raid_disk
,
8367 imsm_progress_container_reshape(super
);
8372 /* before we activate this array handle any missing disks */
8373 if (consistent
== 2)
8374 handle_missing(super
, dev
);
8376 if (consistent
== 2 &&
8377 (!is_resync_complete(&a
->info
) ||
8378 map_state
!= IMSM_T_STATE_NORMAL
||
8379 dev
->vol
.migr_state
))
8382 if (is_resync_complete(&a
->info
)) {
8383 /* complete intialization / resync,
8384 * recovery and interrupted recovery is completed in
8387 if (is_resyncing(dev
)) {
8388 dprintf("imsm: mark resync done\n");
8389 end_migration(dev
, super
, map_state
);
8390 super
->updates_pending
++;
8391 a
->last_checkpoint
= 0;
8393 } else if ((!is_resyncing(dev
) && !failed
) &&
8394 (imsm_reshape_blocks_arrays_changes(super
) == 0)) {
8395 /* mark the start of the init process if nothing is failed */
8396 dprintf("imsm: mark resync start\n");
8397 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
8398 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
8400 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
8401 super
->updates_pending
++;
8405 /* skip checkpointing for general migration,
8406 * it is controlled in mdadm
8408 if (is_gen_migration(dev
))
8409 goto skip_mark_checkpoint
;
8411 /* check if we can update curr_migr_unit from resync_start, recovery_start */
8412 blocks_per_unit
= blocks_per_migr_unit(super
, dev
);
8413 if (blocks_per_unit
) {
8417 units
= a
->last_checkpoint
/ blocks_per_unit
;
8420 /* check that we did not overflow 32-bits, and that
8421 * curr_migr_unit needs updating
8423 if (units32
== units
&&
8425 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
8426 dprintf("imsm: mark checkpoint (%u)\n", units32
);
8427 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
8428 super
->updates_pending
++;
8432 skip_mark_checkpoint
:
8433 /* mark dirty / clean */
8434 if (((dev
->vol
.dirty
& RAIDVOL_DIRTY
) && consistent
) ||
8435 (!(dev
->vol
.dirty
& RAIDVOL_DIRTY
) && !consistent
)) {
8436 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
8438 dev
->vol
.dirty
= RAIDVOL_CLEAN
;
8440 dev
->vol
.dirty
= RAIDVOL_DIRTY
;
8441 if (dev
->rwh_policy
== RWH_DISTRIBUTED
||
8442 dev
->rwh_policy
== RWH_MULTIPLE_DISTRIBUTED
)
8443 dev
->vol
.dirty
|= RAIDVOL_DSRECORD_VALID
;
8445 super
->updates_pending
++;
8451 static int imsm_disk_slot_to_ord(struct active_array
*a
, int slot
)
8453 int inst
= a
->info
.container_member
;
8454 struct intel_super
*super
= a
->container
->sb
;
8455 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8456 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8458 if (slot
> map
->num_members
) {
8459 pr_err("imsm: imsm_disk_slot_to_ord %d out of range 0..%d\n",
8460 slot
, map
->num_members
- 1);
8467 return get_imsm_ord_tbl_ent(dev
, slot
, MAP_0
);
8470 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
8472 int inst
= a
->info
.container_member
;
8473 struct intel_super
*super
= a
->container
->sb
;
8474 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8475 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8476 struct imsm_disk
*disk
;
8478 int recovery_not_finished
= 0;
8482 int rebuild_done
= 0;
8485 ord
= get_imsm_ord_tbl_ent(dev
, n
, MAP_X
);
8489 dprintf("imsm: set_disk %d:%x\n", n
, state
);
8490 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
8492 /* check for new failures */
8493 if (state
& DS_FAULTY
) {
8494 if (mark_failure(super
, dev
, disk
, ord_to_idx(ord
)))
8495 super
->updates_pending
++;
8498 /* check if in_sync */
8499 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
8500 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
8502 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
8504 super
->updates_pending
++;
8507 failed
= imsm_count_failed(super
, dev
, MAP_0
);
8508 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8510 /* check if recovery complete, newly degraded, or failed */
8511 dprintf("imsm: Detected transition to state ");
8512 switch (map_state
) {
8513 case IMSM_T_STATE_NORMAL
: /* transition to normal state */
8514 dprintf("normal: ");
8515 if (is_rebuilding(dev
)) {
8516 dprintf_cont("while rebuilding");
8517 /* check if recovery is really finished */
8518 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
8519 if (mdi
->recovery_start
!= MaxSector
) {
8520 recovery_not_finished
= 1;
8523 if (recovery_not_finished
) {
8525 dprintf("Rebuild has not finished yet, state not changed");
8526 if (a
->last_checkpoint
< mdi
->recovery_start
) {
8527 a
->last_checkpoint
= mdi
->recovery_start
;
8528 super
->updates_pending
++;
8532 end_migration(dev
, super
, map_state
);
8533 map
= get_imsm_map(dev
, MAP_0
);
8534 map
->failed_disk_num
= ~0;
8535 super
->updates_pending
++;
8536 a
->last_checkpoint
= 0;
8539 if (is_gen_migration(dev
)) {
8540 dprintf_cont("while general migration");
8541 if (a
->last_checkpoint
>= a
->info
.component_size
)
8542 end_migration(dev
, super
, map_state
);
8544 map
->map_state
= map_state
;
8545 map
= get_imsm_map(dev
, MAP_0
);
8546 map
->failed_disk_num
= ~0;
8547 super
->updates_pending
++;
8551 case IMSM_T_STATE_DEGRADED
: /* transition to degraded state */
8552 dprintf_cont("degraded: ");
8553 if (map
->map_state
!= map_state
&& !dev
->vol
.migr_state
) {
8554 dprintf_cont("mark degraded");
8555 map
->map_state
= map_state
;
8556 super
->updates_pending
++;
8557 a
->last_checkpoint
= 0;
8560 if (is_rebuilding(dev
)) {
8561 dprintf_cont("while rebuilding.");
8562 if (map
->map_state
!= map_state
) {
8563 dprintf_cont(" Map state change");
8564 end_migration(dev
, super
, map_state
);
8565 super
->updates_pending
++;
8566 } else if (!rebuild_done
) {
8570 /* check if recovery is really finished */
8571 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
8572 if (mdi
->recovery_start
!= MaxSector
) {
8573 recovery_not_finished
= 1;
8576 if (recovery_not_finished
) {
8578 dprintf("Rebuild has not finished yet, state not changed");
8579 if (a
->last_checkpoint
< mdi
->recovery_start
) {
8580 a
->last_checkpoint
=
8581 mdi
->recovery_start
;
8582 super
->updates_pending
++;
8587 dprintf_cont(" Rebuild done, still degraded");
8588 dev
->vol
.migr_state
= 0;
8589 set_migr_type(dev
, 0);
8590 dev
->vol
.curr_migr_unit
= 0;
8592 for (i
= 0; i
< map
->num_members
; i
++) {
8593 int idx
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
8595 if (idx
& IMSM_ORD_REBUILD
)
8596 map
->failed_disk_num
= i
;
8598 super
->updates_pending
++;
8601 if (is_gen_migration(dev
)) {
8602 dprintf_cont("while general migration");
8603 if (a
->last_checkpoint
>= a
->info
.component_size
)
8604 end_migration(dev
, super
, map_state
);
8606 map
->map_state
= map_state
;
8607 manage_second_map(super
, dev
);
8609 super
->updates_pending
++;
8612 if (is_initializing(dev
)) {
8613 dprintf_cont("while initialization.");
8614 map
->map_state
= map_state
;
8615 super
->updates_pending
++;
8619 case IMSM_T_STATE_FAILED
: /* transition to failed state */
8620 dprintf_cont("failed: ");
8621 if (is_gen_migration(dev
)) {
8622 dprintf_cont("while general migration");
8623 map
->map_state
= map_state
;
8624 super
->updates_pending
++;
8627 if (map
->map_state
!= map_state
) {
8628 dprintf_cont("mark failed");
8629 end_migration(dev
, super
, map_state
);
8630 super
->updates_pending
++;
8631 a
->last_checkpoint
= 0;
8636 dprintf_cont("state %i\n", map_state
);
8641 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
8644 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
8645 unsigned long long dsize
;
8646 unsigned long long sectors
;
8647 unsigned int sector_size
;
8649 get_dev_sector_size(fd
, NULL
, §or_size
);
8650 get_dev_size(fd
, NULL
, &dsize
);
8652 if (mpb_size
> sector_size
) {
8653 /* -1 to account for anchor */
8654 sectors
= mpb_sectors(mpb
, sector_size
) - 1;
8656 /* write the extended mpb to the sectors preceeding the anchor */
8657 if (lseek64(fd
, dsize
- (sector_size
* (2 + sectors
)),
8661 if ((unsigned long long)write(fd
, buf
+ sector_size
,
8662 sector_size
* sectors
) != sector_size
* sectors
)
8666 /* first block is stored on second to last sector of the disk */
8667 if (lseek64(fd
, dsize
- (sector_size
* 2), SEEK_SET
) < 0)
8670 if ((unsigned int)write(fd
, buf
, sector_size
) != sector_size
)
8676 static void imsm_sync_metadata(struct supertype
*container
)
8678 struct intel_super
*super
= container
->sb
;
8680 dprintf("sync metadata: %d\n", super
->updates_pending
);
8681 if (!super
->updates_pending
)
8684 write_super_imsm(container
, 0);
8686 super
->updates_pending
= 0;
8689 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
8691 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
8692 int i
= get_imsm_disk_idx(dev
, idx
, MAP_X
);
8695 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
8699 if (dl
&& is_failed(&dl
->disk
))
8703 dprintf("found %x:%x\n", dl
->major
, dl
->minor
);
8708 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
8709 struct active_array
*a
, int activate_new
,
8710 struct mdinfo
*additional_test_list
)
8712 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
8713 int idx
= get_imsm_disk_idx(dev
, slot
, MAP_X
);
8714 struct imsm_super
*mpb
= super
->anchor
;
8715 struct imsm_map
*map
;
8716 unsigned long long pos
;
8721 __u32 array_start
= 0;
8722 __u32 array_end
= 0;
8724 struct mdinfo
*test_list
;
8726 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
8727 /* If in this array, skip */
8728 for (d
= a
->info
.devs
; d
; d
= d
->next
)
8729 if (d
->state_fd
>= 0 &&
8730 d
->disk
.major
== dl
->major
&&
8731 d
->disk
.minor
== dl
->minor
) {
8732 dprintf("%x:%x already in array\n",
8733 dl
->major
, dl
->minor
);
8738 test_list
= additional_test_list
;
8740 if (test_list
->disk
.major
== dl
->major
&&
8741 test_list
->disk
.minor
== dl
->minor
) {
8742 dprintf("%x:%x already in additional test list\n",
8743 dl
->major
, dl
->minor
);
8746 test_list
= test_list
->next
;
8751 /* skip in use or failed drives */
8752 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
8754 dprintf("%x:%x status (failed: %d index: %d)\n",
8755 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
8759 /* skip pure spares when we are looking for partially
8760 * assimilated drives
8762 if (dl
->index
== -1 && !activate_new
)
8765 if (!drive_validate_sector_size(super
, dl
))
8768 /* Does this unused device have the requisite free space?
8769 * It needs to be able to cover all member volumes
8771 ex
= get_extents(super
, dl
);
8773 dprintf("cannot get extents\n");
8776 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8777 dev
= get_imsm_dev(super
, i
);
8778 map
= get_imsm_map(dev
, MAP_0
);
8780 /* check if this disk is already a member of
8783 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
8789 array_start
= pba_of_lba0(map
);
8790 array_end
= array_start
+
8791 per_dev_array_size(map
) - 1;
8794 /* check that we can start at pba_of_lba0 with
8795 * num_data_stripes*blocks_per_stripe of space
8797 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
8801 pos
= ex
[j
].start
+ ex
[j
].size
;
8803 } while (ex
[j
-1].size
);
8810 if (i
< mpb
->num_raid_devs
) {
8811 dprintf("%x:%x does not have %u to %u available\n",
8812 dl
->major
, dl
->minor
, array_start
, array_end
);
8822 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
8824 struct imsm_dev
*dev2
;
8825 struct imsm_map
*map
;
8831 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
8833 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
, MAP_0
);
8834 if (state
== IMSM_T_STATE_FAILED
) {
8835 map
= get_imsm_map(dev2
, MAP_0
);
8838 for (slot
= 0; slot
< map
->num_members
; slot
++) {
8840 * Check if failed disks are deleted from intel
8841 * disk list or are marked to be deleted
8843 idx
= get_imsm_disk_idx(dev2
, slot
, MAP_X
);
8844 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
8846 * Do not rebuild the array if failed disks
8847 * from failed sub-array are not removed from
8851 is_failed(&idisk
->disk
) &&
8852 (idisk
->action
!= DISK_REMOVE
))
8860 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
8861 struct metadata_update
**updates
)
8864 * Find a device with unused free space and use it to replace a
8865 * failed/vacant region in an array. We replace failed regions one a
8866 * array at a time. The result is that a new spare disk will be added
8867 * to the first failed array and after the monitor has finished
8868 * propagating failures the remainder will be consumed.
8870 * FIXME add a capability for mdmon to request spares from another
8874 struct intel_super
*super
= a
->container
->sb
;
8875 int inst
= a
->info
.container_member
;
8876 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8877 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8878 int failed
= a
->info
.array
.raid_disks
;
8879 struct mdinfo
*rv
= NULL
;
8882 struct metadata_update
*mu
;
8884 struct imsm_update_activate_spare
*u
;
8889 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
8890 if ((d
->curr_state
& DS_FAULTY
) &&
8892 /* wait for Removal to happen */
8894 if (d
->state_fd
>= 0)
8898 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
8899 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
8901 if (imsm_reshape_blocks_arrays_changes(super
))
8904 /* Cannot activate another spare if rebuild is in progress already
8906 if (is_rebuilding(dev
)) {
8907 dprintf("imsm: No spare activation allowed. Rebuild in progress already.\n");
8911 if (a
->info
.array
.level
== 4)
8912 /* No repair for takeovered array
8913 * imsm doesn't support raid4
8917 if (imsm_check_degraded(super
, dev
, failed
, MAP_0
) !=
8918 IMSM_T_STATE_DEGRADED
)
8921 if (get_imsm_map(dev
, MAP_0
)->map_state
== IMSM_T_STATE_UNINITIALIZED
) {
8922 dprintf("imsm: No spare activation allowed. Volume is not initialized.\n");
8927 * If there are any failed disks check state of the other volume.
8928 * Block rebuild if the another one is failed until failed disks
8929 * are removed from container.
8932 dprintf("found failed disks in %.*s, check if there anotherfailed sub-array.\n",
8933 MAX_RAID_SERIAL_LEN
, dev
->volume
);
8934 /* check if states of the other volumes allow for rebuild */
8935 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
8937 allowed
= imsm_rebuild_allowed(a
->container
,
8945 /* For each slot, if it is not working, find a spare */
8946 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
8947 for (d
= a
->info
.devs
; d
; d
= d
->next
)
8948 if (d
->disk
.raid_disk
== i
)
8950 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
8951 if (d
&& (d
->state_fd
>= 0))
8955 * OK, this device needs recovery. Try to re-add the
8956 * previous occupant of this slot, if this fails see if
8957 * we can continue the assimilation of a spare that was
8958 * partially assimilated, finally try to activate a new
8961 dl
= imsm_readd(super
, i
, a
);
8963 dl
= imsm_add_spare(super
, i
, a
, 0, rv
);
8965 dl
= imsm_add_spare(super
, i
, a
, 1, rv
);
8969 /* found a usable disk with enough space */
8970 di
= xcalloc(1, sizeof(*di
));
8972 /* dl->index will be -1 in the case we are activating a
8973 * pristine spare. imsm_process_update() will create a
8974 * new index in this case. Once a disk is found to be
8975 * failed in all member arrays it is kicked from the
8978 di
->disk
.number
= dl
->index
;
8980 /* (ab)use di->devs to store a pointer to the device
8983 di
->devs
= (struct mdinfo
*) dl
;
8985 di
->disk
.raid_disk
= i
;
8986 di
->disk
.major
= dl
->major
;
8987 di
->disk
.minor
= dl
->minor
;
8989 di
->recovery_start
= 0;
8990 di
->data_offset
= pba_of_lba0(map
);
8991 di
->component_size
= a
->info
.component_size
;
8992 di
->container_member
= inst
;
8993 di
->bb
.supported
= 1;
8994 if (a
->info
.consistency_policy
== CONSISTENCY_POLICY_PPL
) {
8995 di
->ppl_sector
= get_ppl_sector(super
, inst
);
8996 di
->ppl_size
= MULTIPLE_PPL_AREA_SIZE_IMSM
>> 9;
8998 super
->random
= random32();
9002 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
9003 i
, di
->data_offset
);
9007 /* No spares found */
9009 /* Now 'rv' has a list of devices to return.
9010 * Create a metadata_update record to update the
9011 * disk_ord_tbl for the array
9013 mu
= xmalloc(sizeof(*mu
));
9014 mu
->buf
= xcalloc(num_spares
,
9015 sizeof(struct imsm_update_activate_spare
));
9017 mu
->space_list
= NULL
;
9018 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
9019 mu
->next
= *updates
;
9020 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
9022 for (di
= rv
; di
; di
= di
->next
) {
9023 u
->type
= update_activate_spare
;
9024 u
->dl
= (struct dl
*) di
->devs
;
9026 u
->slot
= di
->disk
.raid_disk
;
9037 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
9039 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
9040 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9041 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, MAP_0
);
9042 struct disk_info
*inf
= get_disk_info(u
);
9043 struct imsm_disk
*disk
;
9047 for (i
= 0; i
< map
->num_members
; i
++) {
9048 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, MAP_X
));
9049 for (j
= 0; j
< new_map
->num_members
; j
++)
9050 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
9057 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
9061 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
9062 if (dl
->major
== major
&& dl
->minor
== minor
)
9067 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
9073 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
9074 if (dl
->major
== major
&& dl
->minor
== minor
) {
9077 prev
->next
= dl
->next
;
9079 super
->disks
= dl
->next
;
9081 __free_imsm_disk(dl
);
9082 dprintf("removed %x:%x\n", major
, minor
);
9090 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
9092 static int add_remove_disk_update(struct intel_super
*super
)
9094 int check_degraded
= 0;
9097 /* add/remove some spares to/from the metadata/contrainer */
9098 while (super
->disk_mgmt_list
) {
9099 struct dl
*disk_cfg
;
9101 disk_cfg
= super
->disk_mgmt_list
;
9102 super
->disk_mgmt_list
= disk_cfg
->next
;
9103 disk_cfg
->next
= NULL
;
9105 if (disk_cfg
->action
== DISK_ADD
) {
9106 disk_cfg
->next
= super
->disks
;
9107 super
->disks
= disk_cfg
;
9109 dprintf("added %x:%x\n",
9110 disk_cfg
->major
, disk_cfg
->minor
);
9111 } else if (disk_cfg
->action
== DISK_REMOVE
) {
9112 dprintf("Disk remove action processed: %x.%x\n",
9113 disk_cfg
->major
, disk_cfg
->minor
);
9114 disk
= get_disk_super(super
,
9118 /* store action status */
9119 disk
->action
= DISK_REMOVE
;
9120 /* remove spare disks only */
9121 if (disk
->index
== -1) {
9122 remove_disk_super(super
,
9127 /* release allocate disk structure */
9128 __free_imsm_disk(disk_cfg
);
9131 return check_degraded
;
9134 static int apply_reshape_migration_update(struct imsm_update_reshape_migration
*u
,
9135 struct intel_super
*super
,
9138 struct intel_dev
*id
;
9139 void **tofree
= NULL
;
9142 dprintf("(enter)\n");
9143 if (u
->subdev
< 0 || u
->subdev
> 1) {
9144 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
9147 if (space_list
== NULL
|| *space_list
== NULL
) {
9148 dprintf("imsm: Error: Memory is not allocated\n");
9152 for (id
= super
->devlist
; id
; id
= id
->next
) {
9153 if (id
->index
== (unsigned)u
->subdev
) {
9154 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
9155 struct imsm_map
*map
;
9156 struct imsm_dev
*new_dev
=
9157 (struct imsm_dev
*)*space_list
;
9158 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
9160 struct dl
*new_disk
;
9162 if (new_dev
== NULL
)
9164 *space_list
= **space_list
;
9165 memcpy(new_dev
, dev
, sizeof_imsm_dev(dev
, 0));
9166 map
= get_imsm_map(new_dev
, MAP_0
);
9168 dprintf("imsm: Error: migration in progress");
9172 to_state
= map
->map_state
;
9173 if ((u
->new_level
== 5) && (map
->raid_level
== 0)) {
9175 /* this should not happen */
9176 if (u
->new_disks
[0] < 0) {
9177 map
->failed_disk_num
=
9178 map
->num_members
- 1;
9179 to_state
= IMSM_T_STATE_DEGRADED
;
9181 to_state
= IMSM_T_STATE_NORMAL
;
9183 migrate(new_dev
, super
, to_state
, MIGR_GEN_MIGR
);
9184 if (u
->new_level
> -1)
9185 map
->raid_level
= u
->new_level
;
9186 migr_map
= get_imsm_map(new_dev
, MAP_1
);
9187 if ((u
->new_level
== 5) &&
9188 (migr_map
->raid_level
== 0)) {
9189 int ord
= map
->num_members
- 1;
9190 migr_map
->num_members
--;
9191 if (u
->new_disks
[0] < 0)
9192 ord
|= IMSM_ORD_REBUILD
;
9193 set_imsm_ord_tbl_ent(map
,
9194 map
->num_members
- 1,
9198 tofree
= (void **)dev
;
9200 /* update chunk size
9202 if (u
->new_chunksize
> 0) {
9203 unsigned long long num_data_stripes
;
9204 struct imsm_map
*dest_map
=
9205 get_imsm_map(dev
, MAP_0
);
9207 imsm_num_data_members(dest_map
);
9209 if (used_disks
== 0)
9212 map
->blocks_per_strip
=
9213 __cpu_to_le16(u
->new_chunksize
* 2);
9215 imsm_dev_size(dev
) / used_disks
;
9216 num_data_stripes
/= map
->blocks_per_strip
;
9217 num_data_stripes
/= map
->num_domains
;
9218 set_num_data_stripes(map
, num_data_stripes
);
9221 /* ensure blocks_per_member has valid value
9223 set_blocks_per_member(map
,
9224 per_dev_array_size(map
) +
9225 NUM_BLOCKS_DIRTY_STRIPE_REGION
);
9229 if (u
->new_level
!= 5 || migr_map
->raid_level
!= 0 ||
9230 migr_map
->raid_level
== map
->raid_level
)
9233 if (u
->new_disks
[0] >= 0) {
9236 new_disk
= get_disk_super(super
,
9237 major(u
->new_disks
[0]),
9238 minor(u
->new_disks
[0]));
9239 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
9240 major(u
->new_disks
[0]),
9241 minor(u
->new_disks
[0]),
9242 new_disk
, new_disk
->index
);
9243 if (new_disk
== NULL
)
9244 goto error_disk_add
;
9246 new_disk
->index
= map
->num_members
- 1;
9247 /* slot to fill in autolayout
9249 new_disk
->raiddisk
= new_disk
->index
;
9250 new_disk
->disk
.status
|= CONFIGURED_DISK
;
9251 new_disk
->disk
.status
&= ~SPARE_DISK
;
9253 goto error_disk_add
;
9256 *tofree
= *space_list
;
9257 /* calculate new size
9259 imsm_set_array_size(new_dev
, -1);
9266 *space_list
= tofree
;
9270 dprintf("Error: imsm: Cannot find disk.\n");
9274 static int apply_size_change_update(struct imsm_update_size_change
*u
,
9275 struct intel_super
*super
)
9277 struct intel_dev
*id
;
9280 dprintf("(enter)\n");
9281 if (u
->subdev
< 0 || u
->subdev
> 1) {
9282 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
9286 for (id
= super
->devlist
; id
; id
= id
->next
) {
9287 if (id
->index
== (unsigned)u
->subdev
) {
9288 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
9289 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9290 int used_disks
= imsm_num_data_members(map
);
9291 unsigned long long blocks_per_member
;
9292 unsigned long long num_data_stripes
;
9293 unsigned long long new_size_per_disk
;
9295 if (used_disks
== 0)
9298 /* calculate new size
9300 new_size_per_disk
= u
->new_size
/ used_disks
;
9301 blocks_per_member
= new_size_per_disk
+
9302 NUM_BLOCKS_DIRTY_STRIPE_REGION
;
9303 num_data_stripes
= new_size_per_disk
/
9304 map
->blocks_per_strip
;
9305 num_data_stripes
/= map
->num_domains
;
9306 dprintf("(size: %llu, blocks per member: %llu, num_data_stipes: %llu)\n",
9307 u
->new_size
, new_size_per_disk
,
9309 set_blocks_per_member(map
, blocks_per_member
);
9310 set_num_data_stripes(map
, num_data_stripes
);
9311 imsm_set_array_size(dev
, u
->new_size
);
9321 static int apply_update_activate_spare(struct imsm_update_activate_spare
*u
,
9322 struct intel_super
*super
,
9323 struct active_array
*active_array
)
9325 struct imsm_super
*mpb
= super
->anchor
;
9326 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
9327 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9328 struct imsm_map
*migr_map
;
9329 struct active_array
*a
;
9330 struct imsm_disk
*disk
;
9337 int second_map_created
= 0;
9339 for (; u
; u
= u
->next
) {
9340 victim
= get_imsm_disk_idx(dev
, u
->slot
, MAP_X
);
9345 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
9350 pr_err("error: imsm_activate_spare passed an unknown disk (index: %d)\n",
9355 /* count failures (excluding rebuilds and the victim)
9356 * to determine map[0] state
9359 for (i
= 0; i
< map
->num_members
; i
++) {
9362 disk
= get_imsm_disk(super
,
9363 get_imsm_disk_idx(dev
, i
, MAP_X
));
9364 if (!disk
|| is_failed(disk
))
9368 /* adding a pristine spare, assign a new index */
9369 if (dl
->index
< 0) {
9370 dl
->index
= super
->anchor
->num_disks
;
9371 super
->anchor
->num_disks
++;
9374 disk
->status
|= CONFIGURED_DISK
;
9375 disk
->status
&= ~SPARE_DISK
;
9378 to_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
9379 if (!second_map_created
) {
9380 second_map_created
= 1;
9381 map
->map_state
= IMSM_T_STATE_DEGRADED
;
9382 migrate(dev
, super
, to_state
, MIGR_REBUILD
);
9384 map
->map_state
= to_state
;
9385 migr_map
= get_imsm_map(dev
, MAP_1
);
9386 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
9387 set_imsm_ord_tbl_ent(migr_map
, u
->slot
,
9388 dl
->index
| IMSM_ORD_REBUILD
);
9390 /* update the family_num to mark a new container
9391 * generation, being careful to record the existing
9392 * family_num in orig_family_num to clean up after
9393 * earlier mdadm versions that neglected to set it.
9395 if (mpb
->orig_family_num
== 0)
9396 mpb
->orig_family_num
= mpb
->family_num
;
9397 mpb
->family_num
+= super
->random
;
9399 /* count arrays using the victim in the metadata */
9401 for (a
= active_array
; a
; a
= a
->next
) {
9402 dev
= get_imsm_dev(super
, a
->info
.container_member
);
9403 map
= get_imsm_map(dev
, MAP_0
);
9405 if (get_imsm_disk_slot(map
, victim
) >= 0)
9409 /* delete the victim if it is no longer being
9415 /* We know that 'manager' isn't touching anything,
9416 * so it is safe to delete
9418 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
9419 if ((*dlp
)->index
== victim
)
9422 /* victim may be on the missing list */
9424 for (dlp
= &super
->missing
; *dlp
;
9425 dlp
= &(*dlp
)->next
)
9426 if ((*dlp
)->index
== victim
)
9428 imsm_delete(super
, dlp
, victim
);
9435 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
9436 struct intel_super
*super
,
9439 struct dl
*new_disk
;
9440 struct intel_dev
*id
;
9442 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
9443 int disk_count
= u
->old_raid_disks
;
9444 void **tofree
= NULL
;
9445 int devices_to_reshape
= 1;
9446 struct imsm_super
*mpb
= super
->anchor
;
9448 unsigned int dev_id
;
9450 dprintf("(enter)\n");
9452 /* enable spares to use in array */
9453 for (i
= 0; i
< delta_disks
; i
++) {
9454 new_disk
= get_disk_super(super
,
9455 major(u
->new_disks
[i
]),
9456 minor(u
->new_disks
[i
]));
9457 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
9458 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
9459 new_disk
, new_disk
->index
);
9460 if (new_disk
== NULL
||
9461 (new_disk
->index
>= 0 &&
9462 new_disk
->index
< u
->old_raid_disks
))
9463 goto update_reshape_exit
;
9464 new_disk
->index
= disk_count
++;
9465 /* slot to fill in autolayout
9467 new_disk
->raiddisk
= new_disk
->index
;
9468 new_disk
->disk
.status
|=
9470 new_disk
->disk
.status
&= ~SPARE_DISK
;
9473 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
9474 mpb
->num_raid_devs
);
9475 /* manage changes in volume
9477 for (dev_id
= 0; dev_id
< mpb
->num_raid_devs
; dev_id
++) {
9478 void **sp
= *space_list
;
9479 struct imsm_dev
*newdev
;
9480 struct imsm_map
*newmap
, *oldmap
;
9482 for (id
= super
->devlist
; id
; id
= id
->next
) {
9483 if (id
->index
== dev_id
)
9492 /* Copy the dev, but not (all of) the map */
9493 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
9494 oldmap
= get_imsm_map(id
->dev
, MAP_0
);
9495 newmap
= get_imsm_map(newdev
, MAP_0
);
9496 /* Copy the current map */
9497 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
9498 /* update one device only
9500 if (devices_to_reshape
) {
9501 dprintf("imsm: modifying subdev: %i\n",
9503 devices_to_reshape
--;
9504 newdev
->vol
.migr_state
= 1;
9505 newdev
->vol
.curr_migr_unit
= 0;
9506 set_migr_type(newdev
, MIGR_GEN_MIGR
);
9507 newmap
->num_members
= u
->new_raid_disks
;
9508 for (i
= 0; i
< delta_disks
; i
++) {
9509 set_imsm_ord_tbl_ent(newmap
,
9510 u
->old_raid_disks
+ i
,
9511 u
->old_raid_disks
+ i
);
9513 /* New map is correct, now need to save old map
9515 newmap
= get_imsm_map(newdev
, MAP_1
);
9516 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
9518 imsm_set_array_size(newdev
, -1);
9521 sp
= (void **)id
->dev
;
9526 /* Clear migration record */
9527 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
9530 *space_list
= tofree
;
9533 update_reshape_exit
:
9538 static int apply_takeover_update(struct imsm_update_takeover
*u
,
9539 struct intel_super
*super
,
9542 struct imsm_dev
*dev
= NULL
;
9543 struct intel_dev
*dv
;
9544 struct imsm_dev
*dev_new
;
9545 struct imsm_map
*map
;
9549 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
9550 if (dv
->index
== (unsigned int)u
->subarray
) {
9558 map
= get_imsm_map(dev
, MAP_0
);
9560 if (u
->direction
== R10_TO_R0
) {
9561 unsigned long long num_data_stripes
;
9563 /* Number of failed disks must be half of initial disk number */
9564 if (imsm_count_failed(super
, dev
, MAP_0
) !=
9565 (map
->num_members
/ 2))
9568 /* iterate through devices to mark removed disks as spare */
9569 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
9570 if (dm
->disk
.status
& FAILED_DISK
) {
9571 int idx
= dm
->index
;
9572 /* update indexes on the disk list */
9573 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
9574 the index values will end up being correct.... NB */
9575 for (du
= super
->disks
; du
; du
= du
->next
)
9576 if (du
->index
> idx
)
9578 /* mark as spare disk */
9583 map
->num_members
= map
->num_members
/ 2;
9584 map
->map_state
= IMSM_T_STATE_NORMAL
;
9585 map
->num_domains
= 1;
9586 map
->raid_level
= 0;
9587 map
->failed_disk_num
= -1;
9588 num_data_stripes
= imsm_dev_size(dev
) / 2;
9589 num_data_stripes
/= map
->blocks_per_strip
;
9590 set_num_data_stripes(map
, num_data_stripes
);
9593 if (u
->direction
== R0_TO_R10
) {
9595 unsigned long long num_data_stripes
;
9597 /* update slots in current disk list */
9598 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
9602 /* create new *missing* disks */
9603 for (i
= 0; i
< map
->num_members
; i
++) {
9604 space
= *space_list
;
9607 *space_list
= *space
;
9609 memcpy(du
, super
->disks
, sizeof(*du
));
9613 du
->index
= (i
* 2) + 1;
9614 sprintf((char *)du
->disk
.serial
,
9615 " MISSING_%d", du
->index
);
9616 sprintf((char *)du
->serial
,
9617 "MISSING_%d", du
->index
);
9618 du
->next
= super
->missing
;
9619 super
->missing
= du
;
9621 /* create new dev and map */
9622 space
= *space_list
;
9625 *space_list
= *space
;
9626 dev_new
= (void *)space
;
9627 memcpy(dev_new
, dev
, sizeof(*dev
));
9628 /* update new map */
9629 map
= get_imsm_map(dev_new
, MAP_0
);
9630 map
->num_members
= map
->num_members
* 2;
9631 map
->map_state
= IMSM_T_STATE_DEGRADED
;
9632 map
->num_domains
= 2;
9633 map
->raid_level
= 1;
9634 num_data_stripes
= imsm_dev_size(dev
) / 2;
9635 num_data_stripes
/= map
->blocks_per_strip
;
9636 num_data_stripes
/= map
->num_domains
;
9637 set_num_data_stripes(map
, num_data_stripes
);
9639 /* replace dev<->dev_new */
9642 /* update disk order table */
9643 for (du
= super
->disks
; du
; du
= du
->next
)
9645 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
9646 for (du
= super
->missing
; du
; du
= du
->next
)
9647 if (du
->index
>= 0) {
9648 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
9649 mark_missing(super
, dv
->dev
, &du
->disk
, du
->index
);
9655 static void imsm_process_update(struct supertype
*st
,
9656 struct metadata_update
*update
)
9659 * crack open the metadata_update envelope to find the update record
9660 * update can be one of:
9661 * update_reshape_container_disks - all the arrays in the container
9662 * are being reshaped to have more devices. We need to mark
9663 * the arrays for general migration and convert selected spares
9664 * into active devices.
9665 * update_activate_spare - a spare device has replaced a failed
9666 * device in an array, update the disk_ord_tbl. If this disk is
9667 * present in all member arrays then also clear the SPARE_DISK
9669 * update_create_array
9671 * update_rename_array
9672 * update_add_remove_disk
9674 struct intel_super
*super
= st
->sb
;
9675 struct imsm_super
*mpb
;
9676 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
9678 /* update requires a larger buf but the allocation failed */
9679 if (super
->next_len
&& !super
->next_buf
) {
9680 super
->next_len
= 0;
9684 if (super
->next_buf
) {
9685 memcpy(super
->next_buf
, super
->buf
, super
->len
);
9687 super
->len
= super
->next_len
;
9688 super
->buf
= super
->next_buf
;
9690 super
->next_len
= 0;
9691 super
->next_buf
= NULL
;
9694 mpb
= super
->anchor
;
9697 case update_general_migration_checkpoint
: {
9698 struct intel_dev
*id
;
9699 struct imsm_update_general_migration_checkpoint
*u
=
9700 (void *)update
->buf
;
9702 dprintf("called for update_general_migration_checkpoint\n");
9704 /* find device under general migration */
9705 for (id
= super
->devlist
; id
; id
= id
->next
) {
9706 if (is_gen_migration(id
->dev
)) {
9707 id
->dev
->vol
.curr_migr_unit
=
9708 __cpu_to_le32(u
->curr_migr_unit
);
9709 super
->updates_pending
++;
9714 case update_takeover
: {
9715 struct imsm_update_takeover
*u
= (void *)update
->buf
;
9716 if (apply_takeover_update(u
, super
, &update
->space_list
)) {
9717 imsm_update_version_info(super
);
9718 super
->updates_pending
++;
9723 case update_reshape_container_disks
: {
9724 struct imsm_update_reshape
*u
= (void *)update
->buf
;
9725 if (apply_reshape_container_disks_update(
9726 u
, super
, &update
->space_list
))
9727 super
->updates_pending
++;
9730 case update_reshape_migration
: {
9731 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
9732 if (apply_reshape_migration_update(
9733 u
, super
, &update
->space_list
))
9734 super
->updates_pending
++;
9737 case update_size_change
: {
9738 struct imsm_update_size_change
*u
= (void *)update
->buf
;
9739 if (apply_size_change_update(u
, super
))
9740 super
->updates_pending
++;
9743 case update_activate_spare
: {
9744 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
9745 if (apply_update_activate_spare(u
, super
, st
->arrays
))
9746 super
->updates_pending
++;
9749 case update_create_array
: {
9750 /* someone wants to create a new array, we need to be aware of
9751 * a few races/collisions:
9752 * 1/ 'Create' called by two separate instances of mdadm
9753 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
9754 * devices that have since been assimilated via
9756 * In the event this update can not be carried out mdadm will
9757 * (FIX ME) notice that its update did not take hold.
9759 struct imsm_update_create_array
*u
= (void *) update
->buf
;
9760 struct intel_dev
*dv
;
9761 struct imsm_dev
*dev
;
9762 struct imsm_map
*map
, *new_map
;
9763 unsigned long long start
, end
;
9764 unsigned long long new_start
, new_end
;
9766 struct disk_info
*inf
;
9769 /* handle racing creates: first come first serve */
9770 if (u
->dev_idx
< mpb
->num_raid_devs
) {
9771 dprintf("subarray %d already defined\n", u
->dev_idx
);
9775 /* check update is next in sequence */
9776 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
9777 dprintf("can not create array %d expected index %d\n",
9778 u
->dev_idx
, mpb
->num_raid_devs
);
9782 new_map
= get_imsm_map(&u
->dev
, MAP_0
);
9783 new_start
= pba_of_lba0(new_map
);
9784 new_end
= new_start
+ per_dev_array_size(new_map
);
9785 inf
= get_disk_info(u
);
9787 /* handle activate_spare versus create race:
9788 * check to make sure that overlapping arrays do not include
9791 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
9792 dev
= get_imsm_dev(super
, i
);
9793 map
= get_imsm_map(dev
, MAP_0
);
9794 start
= pba_of_lba0(map
);
9795 end
= start
+ per_dev_array_size(map
);
9796 if ((new_start
>= start
&& new_start
<= end
) ||
9797 (start
>= new_start
&& start
<= new_end
))
9802 if (disks_overlap(super
, i
, u
)) {
9803 dprintf("arrays overlap\n");
9808 /* check that prepare update was successful */
9809 if (!update
->space
) {
9810 dprintf("prepare update failed\n");
9814 /* check that all disks are still active before committing
9815 * changes. FIXME: could we instead handle this by creating a
9816 * degraded array? That's probably not what the user expects,
9817 * so better to drop this update on the floor.
9819 for (i
= 0; i
< new_map
->num_members
; i
++) {
9820 dl
= serial_to_dl(inf
[i
].serial
, super
);
9822 dprintf("disk disappeared\n");
9827 super
->updates_pending
++;
9829 /* convert spares to members and fixup ord_tbl */
9830 for (i
= 0; i
< new_map
->num_members
; i
++) {
9831 dl
= serial_to_dl(inf
[i
].serial
, super
);
9832 if (dl
->index
== -1) {
9833 dl
->index
= mpb
->num_disks
;
9835 dl
->disk
.status
|= CONFIGURED_DISK
;
9836 dl
->disk
.status
&= ~SPARE_DISK
;
9838 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
9843 update
->space
= NULL
;
9844 imsm_copy_dev(dev
, &u
->dev
);
9845 dv
->index
= u
->dev_idx
;
9846 dv
->next
= super
->devlist
;
9847 super
->devlist
= dv
;
9848 mpb
->num_raid_devs
++;
9850 imsm_update_version_info(super
);
9853 /* mdmon knows how to release update->space, but not
9854 * ((struct intel_dev *) update->space)->dev
9856 if (update
->space
) {
9862 case update_kill_array
: {
9863 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
9864 int victim
= u
->dev_idx
;
9865 struct active_array
*a
;
9866 struct intel_dev
**dp
;
9867 struct imsm_dev
*dev
;
9869 /* sanity check that we are not affecting the uuid of
9870 * active arrays, or deleting an active array
9872 * FIXME when immutable ids are available, but note that
9873 * we'll also need to fixup the invalidated/active
9874 * subarray indexes in mdstat
9876 for (a
= st
->arrays
; a
; a
= a
->next
)
9877 if (a
->info
.container_member
>= victim
)
9879 /* by definition if mdmon is running at least one array
9880 * is active in the container, so checking
9881 * mpb->num_raid_devs is just extra paranoia
9883 dev
= get_imsm_dev(super
, victim
);
9884 if (a
|| !dev
|| mpb
->num_raid_devs
== 1) {
9885 dprintf("failed to delete subarray-%d\n", victim
);
9889 for (dp
= &super
->devlist
; *dp
;)
9890 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
9893 if ((*dp
)->index
> (unsigned)victim
)
9897 mpb
->num_raid_devs
--;
9898 super
->updates_pending
++;
9901 case update_rename_array
: {
9902 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
9903 char name
[MAX_RAID_SERIAL_LEN
+1];
9904 int target
= u
->dev_idx
;
9905 struct active_array
*a
;
9906 struct imsm_dev
*dev
;
9908 /* sanity check that we are not affecting the uuid of
9911 memset(name
, 0, sizeof(name
));
9912 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
9913 name
[MAX_RAID_SERIAL_LEN
] = '\0';
9914 for (a
= st
->arrays
; a
; a
= a
->next
)
9915 if (a
->info
.container_member
== target
)
9917 dev
= get_imsm_dev(super
, u
->dev_idx
);
9918 if (a
|| !dev
|| !check_name(super
, name
, 1)) {
9919 dprintf("failed to rename subarray-%d\n", target
);
9923 memcpy(dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
9924 super
->updates_pending
++;
9927 case update_add_remove_disk
: {
9928 /* we may be able to repair some arrays if disks are
9929 * being added, check the status of add_remove_disk
9930 * if discs has been added.
9932 if (add_remove_disk_update(super
)) {
9933 struct active_array
*a
;
9935 super
->updates_pending
++;
9936 for (a
= st
->arrays
; a
; a
= a
->next
)
9937 a
->check_degraded
= 1;
9941 case update_prealloc_badblocks_mem
:
9943 case update_rwh_policy
: {
9944 struct imsm_update_rwh_policy
*u
= (void *)update
->buf
;
9945 int target
= u
->dev_idx
;
9946 struct imsm_dev
*dev
= get_imsm_dev(super
, target
);
9948 dprintf("could not find subarray-%d\n", target
);
9952 if (dev
->rwh_policy
!= u
->new_policy
) {
9953 dev
->rwh_policy
= u
->new_policy
;
9954 super
->updates_pending
++;
9959 pr_err("error: unsuported process update type:(type: %d)\n", type
);
9963 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
);
9965 static int imsm_prepare_update(struct supertype
*st
,
9966 struct metadata_update
*update
)
9969 * Allocate space to hold new disk entries, raid-device entries or a new
9970 * mpb if necessary. The manager synchronously waits for updates to
9971 * complete in the monitor, so new mpb buffers allocated here can be
9972 * integrated by the monitor thread without worrying about live pointers
9973 * in the manager thread.
9975 enum imsm_update_type type
;
9976 struct intel_super
*super
= st
->sb
;
9977 unsigned int sector_size
= super
->sector_size
;
9978 struct imsm_super
*mpb
= super
->anchor
;
9982 if (update
->len
< (int)sizeof(type
))
9985 type
= *(enum imsm_update_type
*) update
->buf
;
9988 case update_general_migration_checkpoint
:
9989 if (update
->len
< (int)sizeof(struct imsm_update_general_migration_checkpoint
))
9991 dprintf("called for update_general_migration_checkpoint\n");
9993 case update_takeover
: {
9994 struct imsm_update_takeover
*u
= (void *)update
->buf
;
9995 if (update
->len
< (int)sizeof(*u
))
9997 if (u
->direction
== R0_TO_R10
) {
9998 void **tail
= (void **)&update
->space_list
;
9999 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subarray
);
10000 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
10001 int num_members
= map
->num_members
;
10004 /* allocate memory for added disks */
10005 for (i
= 0; i
< num_members
; i
++) {
10006 size
= sizeof(struct dl
);
10007 space
= xmalloc(size
);
10012 /* allocate memory for new device */
10013 size
= sizeof_imsm_dev(super
->devlist
->dev
, 0) +
10014 (num_members
* sizeof(__u32
));
10015 space
= xmalloc(size
);
10019 len
= disks_to_mpb_size(num_members
* 2);
10024 case update_reshape_container_disks
: {
10025 /* Every raid device in the container is about to
10026 * gain some more devices, and we will enter a
10028 * So each 'imsm_map' will be bigger, and the imsm_vol
10029 * will now hold 2 of them.
10030 * Thus we need new 'struct imsm_dev' allocations sized
10031 * as sizeof_imsm_dev but with more devices in both maps.
10033 struct imsm_update_reshape
*u
= (void *)update
->buf
;
10034 struct intel_dev
*dl
;
10035 void **space_tail
= (void**)&update
->space_list
;
10037 if (update
->len
< (int)sizeof(*u
))
10040 dprintf("for update_reshape\n");
10042 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
10043 int size
= sizeof_imsm_dev(dl
->dev
, 1);
10045 if (u
->new_raid_disks
> u
->old_raid_disks
)
10046 size
+= sizeof(__u32
)*2*
10047 (u
->new_raid_disks
- u
->old_raid_disks
);
10051 *space_tail
= NULL
;
10054 len
= disks_to_mpb_size(u
->new_raid_disks
);
10055 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
10058 case update_reshape_migration
: {
10059 /* for migration level 0->5 we need to add disks
10060 * so the same as for container operation we will copy
10061 * device to the bigger location.
10062 * in memory prepared device and new disk area are prepared
10063 * for usage in process update
10065 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
10066 struct intel_dev
*id
;
10067 void **space_tail
= (void **)&update
->space_list
;
10070 int current_level
= -1;
10072 if (update
->len
< (int)sizeof(*u
))
10075 dprintf("for update_reshape\n");
10077 /* add space for bigger array in update
10079 for (id
= super
->devlist
; id
; id
= id
->next
) {
10080 if (id
->index
== (unsigned)u
->subdev
) {
10081 size
= sizeof_imsm_dev(id
->dev
, 1);
10082 if (u
->new_raid_disks
> u
->old_raid_disks
)
10083 size
+= sizeof(__u32
)*2*
10084 (u
->new_raid_disks
- u
->old_raid_disks
);
10088 *space_tail
= NULL
;
10092 if (update
->space_list
== NULL
)
10095 /* add space for disk in update
10097 size
= sizeof(struct dl
);
10101 *space_tail
= NULL
;
10103 /* add spare device to update
10105 for (id
= super
->devlist
; id
; id
= id
->next
)
10106 if (id
->index
== (unsigned)u
->subdev
) {
10107 struct imsm_dev
*dev
;
10108 struct imsm_map
*map
;
10110 dev
= get_imsm_dev(super
, u
->subdev
);
10111 map
= get_imsm_map(dev
, MAP_0
);
10112 current_level
= map
->raid_level
;
10115 if (u
->new_level
== 5 && u
->new_level
!= current_level
) {
10116 struct mdinfo
*spares
;
10118 spares
= get_spares_for_grow(st
);
10121 struct mdinfo
*dev
;
10123 dev
= spares
->devs
;
10126 makedev(dev
->disk
.major
,
10128 dl
= get_disk_super(super
,
10131 dl
->index
= u
->old_raid_disks
;
10134 sysfs_free(spares
);
10137 len
= disks_to_mpb_size(u
->new_raid_disks
);
10138 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
10141 case update_size_change
: {
10142 if (update
->len
< (int)sizeof(struct imsm_update_size_change
))
10146 case update_activate_spare
: {
10147 if (update
->len
< (int)sizeof(struct imsm_update_activate_spare
))
10151 case update_create_array
: {
10152 struct imsm_update_create_array
*u
= (void *) update
->buf
;
10153 struct intel_dev
*dv
;
10154 struct imsm_dev
*dev
= &u
->dev
;
10155 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
10157 struct disk_info
*inf
;
10161 if (update
->len
< (int)sizeof(*u
))
10164 inf
= get_disk_info(u
);
10165 len
= sizeof_imsm_dev(dev
, 1);
10166 /* allocate a new super->devlist entry */
10167 dv
= xmalloc(sizeof(*dv
));
10168 dv
->dev
= xmalloc(len
);
10169 update
->space
= dv
;
10171 /* count how many spares will be converted to members */
10172 for (i
= 0; i
< map
->num_members
; i
++) {
10173 dl
= serial_to_dl(inf
[i
].serial
, super
);
10175 /* hmm maybe it failed?, nothing we can do about
10180 if (count_memberships(dl
, super
) == 0)
10183 len
+= activate
* sizeof(struct imsm_disk
);
10186 case update_kill_array
: {
10187 if (update
->len
< (int)sizeof(struct imsm_update_kill_array
))
10191 case update_rename_array
: {
10192 if (update
->len
< (int)sizeof(struct imsm_update_rename_array
))
10196 case update_add_remove_disk
:
10197 /* no update->len needed */
10199 case update_prealloc_badblocks_mem
:
10200 super
->extra_space
+= sizeof(struct bbm_log
) -
10201 get_imsm_bbm_log_size(super
->bbm_log
);
10203 case update_rwh_policy
: {
10204 if (update
->len
< (int)sizeof(struct imsm_update_rwh_policy
))
10212 /* check if we need a larger metadata buffer */
10213 if (super
->next_buf
)
10214 buf_len
= super
->next_len
;
10216 buf_len
= super
->len
;
10218 if (__le32_to_cpu(mpb
->mpb_size
) + super
->extra_space
+ len
> buf_len
) {
10219 /* ok we need a larger buf than what is currently allocated
10220 * if this allocation fails process_update will notice that
10221 * ->next_len is set and ->next_buf is NULL
10223 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) +
10224 super
->extra_space
+ len
, sector_size
);
10225 if (super
->next_buf
)
10226 free(super
->next_buf
);
10228 super
->next_len
= buf_len
;
10229 if (posix_memalign(&super
->next_buf
, sector_size
, buf_len
) == 0)
10230 memset(super
->next_buf
, 0, buf_len
);
10232 super
->next_buf
= NULL
;
10237 /* must be called while manager is quiesced */
10238 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
10240 struct imsm_super
*mpb
= super
->anchor
;
10242 struct imsm_dev
*dev
;
10243 struct imsm_map
*map
;
10244 unsigned int i
, j
, num_members
;
10245 __u32 ord
, ord_map0
;
10246 struct bbm_log
*log
= super
->bbm_log
;
10248 dprintf("deleting device[%d] from imsm_super\n", index
);
10250 /* shift all indexes down one */
10251 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
10252 if (iter
->index
> (int)index
)
10254 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
10255 if (iter
->index
> (int)index
)
10258 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
10259 dev
= get_imsm_dev(super
, i
);
10260 map
= get_imsm_map(dev
, MAP_0
);
10261 num_members
= map
->num_members
;
10262 for (j
= 0; j
< num_members
; j
++) {
10263 /* update ord entries being careful not to propagate
10264 * ord-flags to the first map
10266 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_X
);
10267 ord_map0
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
10269 if (ord_to_idx(ord
) <= index
)
10272 map
= get_imsm_map(dev
, MAP_0
);
10273 set_imsm_ord_tbl_ent(map
, j
, ord_map0
- 1);
10274 map
= get_imsm_map(dev
, MAP_1
);
10276 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
10280 for (i
= 0; i
< log
->entry_count
; i
++) {
10281 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[i
];
10283 if (entry
->disk_ordinal
<= index
)
10285 entry
->disk_ordinal
--;
10289 super
->updates_pending
++;
10291 struct dl
*dl
= *dlp
;
10293 *dlp
= (*dlp
)->next
;
10294 __free_imsm_disk(dl
);
10298 static void close_targets(int *targets
, int new_disks
)
10305 for (i
= 0; i
< new_disks
; i
++) {
10306 if (targets
[i
] >= 0) {
10313 static int imsm_get_allowed_degradation(int level
, int raid_disks
,
10314 struct intel_super
*super
,
10315 struct imsm_dev
*dev
)
10321 struct imsm_map
*map
;
10324 ret_val
= raid_disks
/2;
10325 /* check map if all disks pairs not failed
10328 map
= get_imsm_map(dev
, MAP_0
);
10329 for (i
= 0; i
< ret_val
; i
++) {
10330 int degradation
= 0;
10331 if (get_imsm_disk(super
, i
) == NULL
)
10333 if (get_imsm_disk(super
, i
+ 1) == NULL
)
10335 if (degradation
== 2)
10338 map
= get_imsm_map(dev
, MAP_1
);
10339 /* if there is no second map
10340 * result can be returned
10344 /* check degradation in second map
10346 for (i
= 0; i
< ret_val
; i
++) {
10347 int degradation
= 0;
10348 if (get_imsm_disk(super
, i
) == NULL
)
10350 if (get_imsm_disk(super
, i
+ 1) == NULL
)
10352 if (degradation
== 2)
10366 /*******************************************************************************
10367 * Function: open_backup_targets
10368 * Description: Function opens file descriptors for all devices given in
10371 * info : general array info
10372 * raid_disks : number of disks
10373 * raid_fds : table of device's file descriptors
10374 * super : intel super for raid10 degradation check
10375 * dev : intel device for raid10 degradation check
10379 ******************************************************************************/
10380 int open_backup_targets(struct mdinfo
*info
, int raid_disks
, int *raid_fds
,
10381 struct intel_super
*super
, struct imsm_dev
*dev
)
10387 for (i
= 0; i
< raid_disks
; i
++)
10390 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
10393 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
)) {
10394 dprintf("disk is faulty!!\n");
10398 if (sd
->disk
.raid_disk
>= raid_disks
|| sd
->disk
.raid_disk
< 0)
10401 dn
= map_dev(sd
->disk
.major
,
10402 sd
->disk
.minor
, 1);
10403 raid_fds
[sd
->disk
.raid_disk
] = dev_open(dn
, O_RDWR
);
10404 if (raid_fds
[sd
->disk
.raid_disk
] < 0) {
10405 pr_err("cannot open component\n");
10410 /* check if maximum array degradation level is not exceeded
10412 if ((raid_disks
- opened
) >
10413 imsm_get_allowed_degradation(info
->new_level
, raid_disks
,
10415 pr_err("Not enough disks can be opened.\n");
10416 close_targets(raid_fds
, raid_disks
);
10422 /*******************************************************************************
10423 * Function: validate_container_imsm
10424 * Description: This routine validates container after assemble,
10425 * eg. if devices in container are under the same controller.
10428 * info : linked list with info about devices used in array
10432 ******************************************************************************/
10433 int validate_container_imsm(struct mdinfo
*info
)
10435 if (check_env("IMSM_NO_PLATFORM"))
10438 struct sys_dev
*idev
;
10439 struct sys_dev
*hba
= NULL
;
10440 struct sys_dev
*intel_devices
= find_intel_devices();
10441 char *dev_path
= devt_to_devpath(makedev(info
->disk
.major
,
10442 info
->disk
.minor
));
10444 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
10445 if (dev_path
&& strstr(dev_path
, idev
->path
)) {
10454 pr_err("WARNING - Cannot detect HBA for device %s!\n",
10455 devid2kname(makedev(info
->disk
.major
, info
->disk
.minor
)));
10459 const struct imsm_orom
*orom
= get_orom_by_device_id(hba
->dev_id
);
10460 struct mdinfo
*dev
;
10462 for (dev
= info
->next
; dev
; dev
= dev
->next
) {
10463 dev_path
= devt_to_devpath(makedev(dev
->disk
.major
, dev
->disk
.minor
));
10465 struct sys_dev
*hba2
= NULL
;
10466 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
10467 if (dev_path
&& strstr(dev_path
, idev
->path
)) {
10475 const struct imsm_orom
*orom2
= hba2
== NULL
? NULL
:
10476 get_orom_by_device_id(hba2
->dev_id
);
10478 if (hba2
&& hba
->type
!= hba2
->type
) {
10479 pr_err("WARNING - HBAs of devices do not match %s != %s\n",
10480 get_sys_dev_type(hba
->type
), get_sys_dev_type(hba2
->type
));
10484 if (orom
!= orom2
) {
10485 pr_err("WARNING - IMSM container assembled with disks under different HBAs!\n"
10486 " This operation is not supported and can lead to data loss.\n");
10491 pr_err("WARNING - IMSM container assembled with disks under HBAs without IMSM platform support!\n"
10492 " This operation is not supported and can lead to data loss.\n");
10500 /*******************************************************************************
10501 * Function: imsm_record_badblock
10502 * Description: This routine stores new bad block record in BBM log
10505 * a : array containing a bad block
10506 * slot : disk number containing a bad block
10507 * sector : bad block sector
10508 * length : bad block sectors range
10512 ******************************************************************************/
10513 static int imsm_record_badblock(struct active_array
*a
, int slot
,
10514 unsigned long long sector
, int length
)
10516 struct intel_super
*super
= a
->container
->sb
;
10520 ord
= imsm_disk_slot_to_ord(a
, slot
);
10524 ret
= record_new_badblock(super
->bbm_log
, ord_to_idx(ord
), sector
,
10527 super
->updates_pending
++;
10531 /*******************************************************************************
10532 * Function: imsm_clear_badblock
10533 * Description: This routine clears bad block record from BBM log
10536 * a : array containing a bad block
10537 * slot : disk number containing a bad block
10538 * sector : bad block sector
10539 * length : bad block sectors range
10543 ******************************************************************************/
10544 static int imsm_clear_badblock(struct active_array
*a
, int slot
,
10545 unsigned long long sector
, int length
)
10547 struct intel_super
*super
= a
->container
->sb
;
10551 ord
= imsm_disk_slot_to_ord(a
, slot
);
10555 ret
= clear_badblock(super
->bbm_log
, ord_to_idx(ord
), sector
, length
);
10557 super
->updates_pending
++;
10561 /*******************************************************************************
10562 * Function: imsm_get_badblocks
10563 * Description: This routine get list of bad blocks for an array
10567 * slot : disk number
10569 * bb : structure containing bad blocks
10571 ******************************************************************************/
10572 static struct md_bb
*imsm_get_badblocks(struct active_array
*a
, int slot
)
10574 int inst
= a
->info
.container_member
;
10575 struct intel_super
*super
= a
->container
->sb
;
10576 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
10577 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
10580 ord
= imsm_disk_slot_to_ord(a
, slot
);
10584 get_volume_badblocks(super
->bbm_log
, ord_to_idx(ord
), pba_of_lba0(map
),
10585 per_dev_array_size(map
), &super
->bb
);
10589 /*******************************************************************************
10590 * Function: examine_badblocks_imsm
10591 * Description: Prints list of bad blocks on a disk to the standard output
10594 * st : metadata handler
10595 * fd : open file descriptor for device
10596 * devname : device name
10600 ******************************************************************************/
10601 static int examine_badblocks_imsm(struct supertype
*st
, int fd
, char *devname
)
10603 struct intel_super
*super
= st
->sb
;
10604 struct bbm_log
*log
= super
->bbm_log
;
10605 struct dl
*d
= NULL
;
10608 for (d
= super
->disks
; d
; d
= d
->next
) {
10609 if (strcmp(d
->devname
, devname
) == 0)
10613 if ((d
== NULL
) || (d
->index
< 0)) { /* serial mismatch probably */
10614 pr_err("%s doesn't appear to be part of a raid array\n",
10621 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[0];
10623 for (i
= 0; i
< log
->entry_count
; i
++) {
10624 if (entry
[i
].disk_ordinal
== d
->index
) {
10625 unsigned long long sector
= __le48_to_cpu(
10626 &entry
[i
].defective_block_start
);
10627 int cnt
= entry
[i
].marked_count
+ 1;
10630 printf("Bad-blocks on %s:\n", devname
);
10634 printf("%20llu for %d sectors\n", sector
, cnt
);
10640 printf("No bad-blocks list configured on %s\n", devname
);
10644 /*******************************************************************************
10645 * Function: init_migr_record_imsm
10646 * Description: Function inits imsm migration record
10648 * super : imsm internal array info
10649 * dev : device under migration
10650 * info : general array info to find the smallest device
10653 ******************************************************************************/
10654 void init_migr_record_imsm(struct supertype
*st
, struct imsm_dev
*dev
,
10655 struct mdinfo
*info
)
10657 struct intel_super
*super
= st
->sb
;
10658 struct migr_record
*migr_rec
= super
->migr_rec
;
10659 int new_data_disks
;
10660 unsigned long long dsize
, dev_sectors
;
10661 long long unsigned min_dev_sectors
= -1LLU;
10665 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
10666 struct imsm_map
*map_src
= get_imsm_map(dev
, MAP_1
);
10667 unsigned long long num_migr_units
;
10668 unsigned long long array_blocks
;
10670 memset(migr_rec
, 0, sizeof(struct migr_record
));
10671 migr_rec
->family_num
= __cpu_to_le32(super
->anchor
->family_num
);
10673 /* only ascending reshape supported now */
10674 migr_rec
->ascending_migr
= __cpu_to_le32(1);
10676 migr_rec
->dest_depth_per_unit
= GEN_MIGR_AREA_SIZE
/
10677 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
10678 migr_rec
->dest_depth_per_unit
*=
10679 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
10680 new_data_disks
= imsm_num_data_members(map_dest
);
10681 migr_rec
->blocks_per_unit
=
10682 __cpu_to_le32(migr_rec
->dest_depth_per_unit
* new_data_disks
);
10683 migr_rec
->dest_depth_per_unit
=
10684 __cpu_to_le32(migr_rec
->dest_depth_per_unit
);
10685 array_blocks
= info
->component_size
* new_data_disks
;
10687 array_blocks
/ __le32_to_cpu(migr_rec
->blocks_per_unit
);
10689 if (array_blocks
% __le32_to_cpu(migr_rec
->blocks_per_unit
))
10691 migr_rec
->num_migr_units
= __cpu_to_le32(num_migr_units
);
10693 migr_rec
->post_migr_vol_cap
= dev
->size_low
;
10694 migr_rec
->post_migr_vol_cap_hi
= dev
->size_high
;
10696 /* Find the smallest dev */
10697 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
10698 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
10699 fd
= dev_open(nm
, O_RDONLY
);
10702 get_dev_size(fd
, NULL
, &dsize
);
10703 dev_sectors
= dsize
/ 512;
10704 if (dev_sectors
< min_dev_sectors
)
10705 min_dev_sectors
= dev_sectors
;
10708 migr_rec
->ckpt_area_pba
= __cpu_to_le32(min_dev_sectors
-
10709 RAID_DISK_RESERVED_BLOCKS_IMSM_HI
);
10711 write_imsm_migr_rec(st
);
10716 /*******************************************************************************
10717 * Function: save_backup_imsm
10718 * Description: Function saves critical data stripes to Migration Copy Area
10719 * and updates the current migration unit status.
10720 * Use restore_stripes() to form a destination stripe,
10721 * and to write it to the Copy Area.
10723 * st : supertype information
10724 * dev : imsm device that backup is saved for
10725 * info : general array info
10726 * buf : input buffer
10727 * length : length of data to backup (blocks_per_unit)
10731 ******************************************************************************/
10732 int save_backup_imsm(struct supertype
*st
,
10733 struct imsm_dev
*dev
,
10734 struct mdinfo
*info
,
10739 struct intel_super
*super
= st
->sb
;
10740 unsigned long long *target_offsets
;
10743 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
10744 int new_disks
= map_dest
->num_members
;
10745 int dest_layout
= 0;
10747 unsigned long long start
;
10748 int data_disks
= imsm_num_data_members(map_dest
);
10750 targets
= xmalloc(new_disks
* sizeof(int));
10752 for (i
= 0; i
< new_disks
; i
++)
10755 target_offsets
= xcalloc(new_disks
, sizeof(unsigned long long));
10757 start
= info
->reshape_progress
* 512;
10758 for (i
= 0; i
< new_disks
; i
++) {
10759 target_offsets
[i
] = (unsigned long long)
10760 __le32_to_cpu(super
->migr_rec
->ckpt_area_pba
) * 512;
10761 /* move back copy area adderss, it will be moved forward
10762 * in restore_stripes() using start input variable
10764 target_offsets
[i
] -= start
/data_disks
;
10767 if (open_backup_targets(info
, new_disks
, targets
,
10771 dest_layout
= imsm_level_to_layout(map_dest
->raid_level
);
10772 dest_chunk
= __le16_to_cpu(map_dest
->blocks_per_strip
) * 512;
10774 if (restore_stripes(targets
, /* list of dest devices */
10775 target_offsets
, /* migration record offsets */
10778 map_dest
->raid_level
,
10780 -1, /* source backup file descriptor */
10781 0, /* input buf offset
10782 * always 0 buf is already offseted */
10786 pr_err("Error restoring stripes\n");
10794 close_targets(targets
, new_disks
);
10797 free(target_offsets
);
10802 /*******************************************************************************
10803 * Function: save_checkpoint_imsm
10804 * Description: Function called for current unit status update
10805 * in the migration record. It writes it to disk.
10807 * super : imsm internal array info
10808 * info : general array info
10812 * 2: failure, means no valid migration record
10813 * / no general migration in progress /
10814 ******************************************************************************/
10815 int save_checkpoint_imsm(struct supertype
*st
, struct mdinfo
*info
, int state
)
10817 struct intel_super
*super
= st
->sb
;
10818 unsigned long long blocks_per_unit
;
10819 unsigned long long curr_migr_unit
;
10821 if (load_imsm_migr_rec(super
, info
) != 0) {
10822 dprintf("imsm: ERROR: Cannot read migration record for checkpoint save.\n");
10826 blocks_per_unit
= __le32_to_cpu(super
->migr_rec
->blocks_per_unit
);
10827 if (blocks_per_unit
== 0) {
10828 dprintf("imsm: no migration in progress.\n");
10831 curr_migr_unit
= info
->reshape_progress
/ blocks_per_unit
;
10832 /* check if array is alligned to copy area
10833 * if it is not alligned, add one to current migration unit value
10834 * this can happend on array reshape finish only
10836 if (info
->reshape_progress
% blocks_per_unit
)
10839 super
->migr_rec
->curr_migr_unit
=
10840 __cpu_to_le32(curr_migr_unit
);
10841 super
->migr_rec
->rec_status
= __cpu_to_le32(state
);
10842 super
->migr_rec
->dest_1st_member_lba
=
10843 __cpu_to_le32(curr_migr_unit
*
10844 __le32_to_cpu(super
->migr_rec
->dest_depth_per_unit
));
10845 if (write_imsm_migr_rec(st
) < 0) {
10846 dprintf("imsm: Cannot write migration record outside backup area\n");
10853 /*******************************************************************************
10854 * Function: recover_backup_imsm
10855 * Description: Function recovers critical data from the Migration Copy Area
10856 * while assembling an array.
10858 * super : imsm internal array info
10859 * info : general array info
10861 * 0 : success (or there is no data to recover)
10863 ******************************************************************************/
10864 int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
)
10866 struct intel_super
*super
= st
->sb
;
10867 struct migr_record
*migr_rec
= super
->migr_rec
;
10868 struct imsm_map
*map_dest
;
10869 struct intel_dev
*id
= NULL
;
10870 unsigned long long read_offset
;
10871 unsigned long long write_offset
;
10873 int *targets
= NULL
;
10874 int new_disks
, i
, err
;
10877 unsigned int sector_size
= super
->sector_size
;
10878 unsigned long curr_migr_unit
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
10879 unsigned long num_migr_units
= __le32_to_cpu(migr_rec
->num_migr_units
);
10881 int skipped_disks
= 0;
10883 err
= sysfs_get_str(info
, NULL
, "array_state", (char *)buffer
, 20);
10887 /* recover data only during assemblation */
10888 if (strncmp(buffer
, "inactive", 8) != 0)
10890 /* no data to recover */
10891 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
10893 if (curr_migr_unit
>= num_migr_units
)
10896 /* find device during reshape */
10897 for (id
= super
->devlist
; id
; id
= id
->next
)
10898 if (is_gen_migration(id
->dev
))
10903 map_dest
= get_imsm_map(id
->dev
, MAP_0
);
10904 new_disks
= map_dest
->num_members
;
10906 read_offset
= (unsigned long long)
10907 __le32_to_cpu(migr_rec
->ckpt_area_pba
) * 512;
10909 write_offset
= ((unsigned long long)
10910 __le32_to_cpu(migr_rec
->dest_1st_member_lba
) +
10911 pba_of_lba0(map_dest
)) * 512;
10913 unit_len
= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
10914 if (posix_memalign((void **)&buf
, sector_size
, unit_len
) != 0)
10916 targets
= xcalloc(new_disks
, sizeof(int));
10918 if (open_backup_targets(info
, new_disks
, targets
, super
, id
->dev
)) {
10919 pr_err("Cannot open some devices belonging to array.\n");
10923 for (i
= 0; i
< new_disks
; i
++) {
10924 if (targets
[i
] < 0) {
10928 if (lseek64(targets
[i
], read_offset
, SEEK_SET
) < 0) {
10929 pr_err("Cannot seek to block: %s\n",
10934 if ((unsigned)read(targets
[i
], buf
, unit_len
) != unit_len
) {
10935 pr_err("Cannot read copy area block: %s\n",
10940 if (lseek64(targets
[i
], write_offset
, SEEK_SET
) < 0) {
10941 pr_err("Cannot seek to block: %s\n",
10946 if ((unsigned)write(targets
[i
], buf
, unit_len
) != unit_len
) {
10947 pr_err("Cannot restore block: %s\n",
10954 if (skipped_disks
> imsm_get_allowed_degradation(info
->new_level
,
10958 pr_err("Cannot restore data from backup. Too many failed disks\n");
10962 if (save_checkpoint_imsm(st
, info
, UNIT_SRC_NORMAL
)) {
10963 /* ignore error == 2, this can mean end of reshape here
10965 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL) during restart\n");
10971 for (i
= 0; i
< new_disks
; i
++)
10980 static char disk_by_path
[] = "/dev/disk/by-path/";
10982 static const char *imsm_get_disk_controller_domain(const char *path
)
10984 char disk_path
[PATH_MAX
];
10988 strcpy(disk_path
, disk_by_path
);
10989 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
10990 if (stat(disk_path
, &st
) == 0) {
10991 struct sys_dev
* hba
;
10994 path
= devt_to_devpath(st
.st_rdev
);
10997 hba
= find_disk_attached_hba(-1, path
);
10998 if (hba
&& hba
->type
== SYS_DEV_SAS
)
11000 else if (hba
&& hba
->type
== SYS_DEV_SATA
)
11002 else if (hba
&& hba
->type
== SYS_DEV_VMD
)
11004 else if (hba
&& hba
->type
== SYS_DEV_NVME
)
11008 dprintf("path: %s hba: %s attached: %s\n",
11009 path
, (hba
) ? hba
->path
: "NULL", drv
);
11015 static char *imsm_find_array_devnm_by_subdev(int subdev
, char *container
)
11017 static char devnm
[32];
11018 char subdev_name
[20];
11019 struct mdstat_ent
*mdstat
;
11021 sprintf(subdev_name
, "%d", subdev
);
11022 mdstat
= mdstat_by_subdev(subdev_name
, container
);
11026 strcpy(devnm
, mdstat
->devnm
);
11027 free_mdstat(mdstat
);
11031 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
11032 struct geo_params
*geo
,
11033 int *old_raid_disks
,
11036 /* currently we only support increasing the number of devices
11037 * for a container. This increases the number of device for each
11038 * member array. They must all be RAID0 or RAID5.
11041 struct mdinfo
*info
, *member
;
11042 int devices_that_can_grow
= 0;
11044 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): st->devnm = (%s)\n", st
->devnm
);
11046 if (geo
->size
> 0 ||
11047 geo
->level
!= UnSet
||
11048 geo
->layout
!= UnSet
||
11049 geo
->chunksize
!= 0 ||
11050 geo
->raid_disks
== UnSet
) {
11051 dprintf("imsm: Container operation is allowed for raid disks number change only.\n");
11055 if (direction
== ROLLBACK_METADATA_CHANGES
) {
11056 dprintf("imsm: Metadata changes rollback is not supported for container operation.\n");
11060 info
= container_content_imsm(st
, NULL
);
11061 for (member
= info
; member
; member
= member
->next
) {
11064 dprintf("imsm: checking device_num: %i\n",
11065 member
->container_member
);
11067 if (geo
->raid_disks
<= member
->array
.raid_disks
) {
11068 /* we work on container for Online Capacity Expansion
11069 * only so raid_disks has to grow
11071 dprintf("imsm: for container operation raid disks increase is required\n");
11075 if (info
->array
.level
!= 0 && info
->array
.level
!= 5) {
11076 /* we cannot use this container with other raid level
11078 dprintf("imsm: for container operation wrong raid level (%i) detected\n",
11079 info
->array
.level
);
11082 /* check for platform support
11083 * for this raid level configuration
11085 struct intel_super
*super
= st
->sb
;
11086 if (!is_raid_level_supported(super
->orom
,
11087 member
->array
.level
,
11088 geo
->raid_disks
)) {
11089 dprintf("platform does not support raid%d with %d disk%s\n",
11092 geo
->raid_disks
> 1 ? "s" : "");
11095 /* check if component size is aligned to chunk size
11097 if (info
->component_size
%
11098 (info
->array
.chunk_size
/512)) {
11099 dprintf("Component size is not aligned to chunk size\n");
11104 if (*old_raid_disks
&&
11105 info
->array
.raid_disks
!= *old_raid_disks
)
11107 *old_raid_disks
= info
->array
.raid_disks
;
11109 /* All raid5 and raid0 volumes in container
11110 * have to be ready for Online Capacity Expansion
11111 * so they need to be assembled. We have already
11112 * checked that no recovery etc is happening.
11114 result
= imsm_find_array_devnm_by_subdev(member
->container_member
,
11115 st
->container_devnm
);
11116 if (result
== NULL
) {
11117 dprintf("imsm: cannot find array\n");
11120 devices_that_can_grow
++;
11123 if (!member
&& devices_that_can_grow
)
11127 dprintf("Container operation allowed\n");
11129 dprintf("Error: %i\n", ret_val
);
11134 /* Function: get_spares_for_grow
11135 * Description: Allocates memory and creates list of spare devices
11136 * avaliable in container. Checks if spare drive size is acceptable.
11137 * Parameters: Pointer to the supertype structure
11138 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
11141 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
11143 struct spare_criteria sc
;
11145 get_spare_criteria_imsm(st
, &sc
);
11146 return container_choose_spares(st
, &sc
, NULL
, NULL
, NULL
, 0);
11149 /******************************************************************************
11150 * function: imsm_create_metadata_update_for_reshape
11151 * Function creates update for whole IMSM container.
11153 ******************************************************************************/
11154 static int imsm_create_metadata_update_for_reshape(
11155 struct supertype
*st
,
11156 struct geo_params
*geo
,
11157 int old_raid_disks
,
11158 struct imsm_update_reshape
**updatep
)
11160 struct intel_super
*super
= st
->sb
;
11161 struct imsm_super
*mpb
= super
->anchor
;
11162 int update_memory_size
;
11163 struct imsm_update_reshape
*u
;
11164 struct mdinfo
*spares
;
11167 struct mdinfo
*dev
;
11169 dprintf("(enter) raid_disks = %i\n", geo
->raid_disks
);
11171 delta_disks
= geo
->raid_disks
- old_raid_disks
;
11173 /* size of all update data without anchor */
11174 update_memory_size
= sizeof(struct imsm_update_reshape
);
11176 /* now add space for spare disks that we need to add. */
11177 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
11179 u
= xcalloc(1, update_memory_size
);
11180 u
->type
= update_reshape_container_disks
;
11181 u
->old_raid_disks
= old_raid_disks
;
11182 u
->new_raid_disks
= geo
->raid_disks
;
11184 /* now get spare disks list
11186 spares
= get_spares_for_grow(st
);
11188 if (spares
== NULL
|| delta_disks
> spares
->array
.spare_disks
) {
11189 pr_err("imsm: ERROR: Cannot get spare devices for %s.\n", geo
->dev_name
);
11194 /* we have got spares
11195 * update disk list in imsm_disk list table in anchor
11197 dprintf("imsm: %i spares are available.\n\n",
11198 spares
->array
.spare_disks
);
11200 dev
= spares
->devs
;
11201 for (i
= 0; i
< delta_disks
; i
++) {
11206 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
11208 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
11209 dl
->index
= mpb
->num_disks
;
11217 sysfs_free(spares
);
11219 dprintf("imsm: reshape update preparation :");
11220 if (i
== delta_disks
) {
11221 dprintf_cont(" OK\n");
11223 return update_memory_size
;
11226 dprintf_cont(" Error\n");
11231 /******************************************************************************
11232 * function: imsm_create_metadata_update_for_size_change()
11233 * Creates update for IMSM array for array size change.
11235 ******************************************************************************/
11236 static int imsm_create_metadata_update_for_size_change(
11237 struct supertype
*st
,
11238 struct geo_params
*geo
,
11239 struct imsm_update_size_change
**updatep
)
11241 struct intel_super
*super
= st
->sb
;
11242 int update_memory_size
;
11243 struct imsm_update_size_change
*u
;
11245 dprintf("(enter) New size = %llu\n", geo
->size
);
11247 /* size of all update data without anchor */
11248 update_memory_size
= sizeof(struct imsm_update_size_change
);
11250 u
= xcalloc(1, update_memory_size
);
11251 u
->type
= update_size_change
;
11252 u
->subdev
= super
->current_vol
;
11253 u
->new_size
= geo
->size
;
11255 dprintf("imsm: reshape update preparation : OK\n");
11258 return update_memory_size
;
11261 /******************************************************************************
11262 * function: imsm_create_metadata_update_for_migration()
11263 * Creates update for IMSM array.
11265 ******************************************************************************/
11266 static int imsm_create_metadata_update_for_migration(
11267 struct supertype
*st
,
11268 struct geo_params
*geo
,
11269 struct imsm_update_reshape_migration
**updatep
)
11271 struct intel_super
*super
= st
->sb
;
11272 int update_memory_size
;
11273 struct imsm_update_reshape_migration
*u
;
11274 struct imsm_dev
*dev
;
11275 int previous_level
= -1;
11277 dprintf("(enter) New Level = %i\n", geo
->level
);
11279 /* size of all update data without anchor */
11280 update_memory_size
= sizeof(struct imsm_update_reshape_migration
);
11282 u
= xcalloc(1, update_memory_size
);
11283 u
->type
= update_reshape_migration
;
11284 u
->subdev
= super
->current_vol
;
11285 u
->new_level
= geo
->level
;
11286 u
->new_layout
= geo
->layout
;
11287 u
->new_raid_disks
= u
->old_raid_disks
= geo
->raid_disks
;
11288 u
->new_disks
[0] = -1;
11289 u
->new_chunksize
= -1;
11291 dev
= get_imsm_dev(super
, u
->subdev
);
11293 struct imsm_map
*map
;
11295 map
= get_imsm_map(dev
, MAP_0
);
11297 int current_chunk_size
=
11298 __le16_to_cpu(map
->blocks_per_strip
) / 2;
11300 if (geo
->chunksize
!= current_chunk_size
) {
11301 u
->new_chunksize
= geo
->chunksize
/ 1024;
11302 dprintf("imsm: chunk size change from %i to %i\n",
11303 current_chunk_size
, u
->new_chunksize
);
11305 previous_level
= map
->raid_level
;
11308 if (geo
->level
== 5 && previous_level
== 0) {
11309 struct mdinfo
*spares
= NULL
;
11311 u
->new_raid_disks
++;
11312 spares
= get_spares_for_grow(st
);
11313 if (spares
== NULL
|| spares
->array
.spare_disks
< 1) {
11315 sysfs_free(spares
);
11316 update_memory_size
= 0;
11317 pr_err("cannot get spare device for requested migration\n");
11320 sysfs_free(spares
);
11322 dprintf("imsm: reshape update preparation : OK\n");
11325 return update_memory_size
;
11328 static void imsm_update_metadata_locally(struct supertype
*st
,
11329 void *buf
, int len
)
11331 struct metadata_update mu
;
11336 mu
.space_list
= NULL
;
11338 if (imsm_prepare_update(st
, &mu
))
11339 imsm_process_update(st
, &mu
);
11341 while (mu
.space_list
) {
11342 void **space
= mu
.space_list
;
11343 mu
.space_list
= *space
;
11348 /***************************************************************************
11349 * Function: imsm_analyze_change
11350 * Description: Function analyze change for single volume
11351 * and validate if transition is supported
11352 * Parameters: Geometry parameters, supertype structure,
11353 * metadata change direction (apply/rollback)
11354 * Returns: Operation type code on success, -1 if fail
11355 ****************************************************************************/
11356 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
11357 struct geo_params
*geo
,
11360 struct mdinfo info
;
11362 int check_devs
= 0;
11364 /* number of added/removed disks in operation result */
11365 int devNumChange
= 0;
11366 /* imsm compatible layout value for array geometry verification */
11367 int imsm_layout
= -1;
11369 struct imsm_dev
*dev
;
11370 struct imsm_map
*map
;
11371 struct intel_super
*super
;
11372 unsigned long long current_size
;
11373 unsigned long long free_size
;
11374 unsigned long long max_size
;
11377 getinfo_super_imsm_volume(st
, &info
, NULL
);
11378 if (geo
->level
!= info
.array
.level
&& geo
->level
>= 0 &&
11379 geo
->level
!= UnSet
) {
11380 switch (info
.array
.level
) {
11382 if (geo
->level
== 5) {
11383 change
= CH_MIGRATION
;
11384 if (geo
->layout
!= ALGORITHM_LEFT_ASYMMETRIC
) {
11385 pr_err("Error. Requested Layout not supported (left-asymmetric layout is supported only)!\n");
11387 goto analyse_change_exit
;
11389 imsm_layout
= geo
->layout
;
11391 devNumChange
= 1; /* parity disk added */
11392 } else if (geo
->level
== 10) {
11393 change
= CH_TAKEOVER
;
11395 devNumChange
= 2; /* two mirrors added */
11396 imsm_layout
= 0x102; /* imsm supported layout */
11401 if (geo
->level
== 0) {
11402 change
= CH_TAKEOVER
;
11404 devNumChange
= -(geo
->raid_disks
/2);
11405 imsm_layout
= 0; /* imsm raid0 layout */
11409 if (change
== -1) {
11410 pr_err("Error. Level Migration from %d to %d not supported!\n",
11411 info
.array
.level
, geo
->level
);
11412 goto analyse_change_exit
;
11415 geo
->level
= info
.array
.level
;
11417 if (geo
->layout
!= info
.array
.layout
&&
11418 (geo
->layout
!= UnSet
&& geo
->layout
!= -1)) {
11419 change
= CH_MIGRATION
;
11420 if (info
.array
.layout
== 0 && info
.array
.level
== 5 &&
11421 geo
->layout
== 5) {
11422 /* reshape 5 -> 4 */
11423 } else if (info
.array
.layout
== 5 && info
.array
.level
== 5 &&
11424 geo
->layout
== 0) {
11425 /* reshape 4 -> 5 */
11429 pr_err("Error. Layout Migration from %d to %d not supported!\n",
11430 info
.array
.layout
, geo
->layout
);
11432 goto analyse_change_exit
;
11435 geo
->layout
= info
.array
.layout
;
11436 if (imsm_layout
== -1)
11437 imsm_layout
= info
.array
.layout
;
11440 if (geo
->chunksize
> 0 && geo
->chunksize
!= UnSet
&&
11441 geo
->chunksize
!= info
.array
.chunk_size
) {
11442 if (info
.array
.level
== 10) {
11443 pr_err("Error. Chunk size change for RAID 10 is not supported.\n");
11445 goto analyse_change_exit
;
11446 } else if (info
.component_size
% (geo
->chunksize
/512)) {
11447 pr_err("New chunk size (%dK) does not evenly divide device size (%lluk). Aborting...\n",
11448 geo
->chunksize
/1024, info
.component_size
/2);
11450 goto analyse_change_exit
;
11452 change
= CH_MIGRATION
;
11454 geo
->chunksize
= info
.array
.chunk_size
;
11457 chunk
= geo
->chunksize
/ 1024;
11460 dev
= get_imsm_dev(super
, super
->current_vol
);
11461 map
= get_imsm_map(dev
, MAP_0
);
11462 data_disks
= imsm_num_data_members(map
);
11463 /* compute current size per disk member
11465 current_size
= info
.custom_array_size
/ data_disks
;
11467 if (geo
->size
> 0 && geo
->size
!= MAX_SIZE
) {
11468 /* align component size
11470 geo
->size
= imsm_component_size_alignment_check(
11471 get_imsm_raid_level(dev
->vol
.map
),
11472 chunk
* 1024, super
->sector_size
,
11474 if (geo
->size
== 0) {
11475 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is 0).\n",
11477 goto analyse_change_exit
;
11481 if (current_size
!= geo
->size
&& geo
->size
> 0) {
11482 if (change
!= -1) {
11483 pr_err("Error. Size change should be the only one at a time.\n");
11485 goto analyse_change_exit
;
11487 if ((super
->current_vol
+ 1) != super
->anchor
->num_raid_devs
) {
11488 pr_err("Error. The last volume in container can be expanded only (%i/%s).\n",
11489 super
->current_vol
, st
->devnm
);
11490 goto analyse_change_exit
;
11492 /* check the maximum available size
11494 rv
= imsm_get_free_size(st
, dev
->vol
.map
->num_members
,
11495 0, chunk
, &free_size
);
11497 /* Cannot find maximum available space
11501 max_size
= free_size
+ current_size
;
11502 /* align component size
11504 max_size
= imsm_component_size_alignment_check(
11505 get_imsm_raid_level(dev
->vol
.map
),
11506 chunk
* 1024, super
->sector_size
,
11509 if (geo
->size
== MAX_SIZE
) {
11510 /* requested size change to the maximum available size
11512 if (max_size
== 0) {
11513 pr_err("Error. Cannot find maximum available space.\n");
11515 goto analyse_change_exit
;
11517 geo
->size
= max_size
;
11520 if (direction
== ROLLBACK_METADATA_CHANGES
) {
11521 /* accept size for rollback only
11524 /* round size due to metadata compatibility
11526 geo
->size
= (geo
->size
>> SECT_PER_MB_SHIFT
)
11527 << SECT_PER_MB_SHIFT
;
11528 dprintf("Prepare update for size change to %llu\n",
11530 if (current_size
>= geo
->size
) {
11531 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is %llu).\n",
11532 current_size
, geo
->size
);
11533 goto analyse_change_exit
;
11535 if (max_size
&& geo
->size
> max_size
) {
11536 pr_err("Error. Requested size is larger than maximum available size (maximum available size is %llu, requested size /rounded/ is %llu).\n",
11537 max_size
, geo
->size
);
11538 goto analyse_change_exit
;
11541 geo
->size
*= data_disks
;
11542 geo
->raid_disks
= dev
->vol
.map
->num_members
;
11543 change
= CH_ARRAY_SIZE
;
11545 if (!validate_geometry_imsm(st
,
11548 geo
->raid_disks
+ devNumChange
,
11550 geo
->size
, INVALID_SECTORS
,
11551 0, 0, info
.consistency_policy
, 1))
11555 struct intel_super
*super
= st
->sb
;
11556 struct imsm_super
*mpb
= super
->anchor
;
11558 if (mpb
->num_raid_devs
> 1) {
11559 pr_err("Error. Cannot perform operation on %s- for this operation it MUST be single array in container\n",
11565 analyse_change_exit
:
11566 if (direction
== ROLLBACK_METADATA_CHANGES
&&
11567 (change
== CH_MIGRATION
|| change
== CH_TAKEOVER
)) {
11568 dprintf("imsm: Metadata changes rollback is not supported for migration and takeover operations.\n");
11574 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
11576 struct intel_super
*super
= st
->sb
;
11577 struct imsm_update_takeover
*u
;
11579 u
= xmalloc(sizeof(struct imsm_update_takeover
));
11581 u
->type
= update_takeover
;
11582 u
->subarray
= super
->current_vol
;
11584 /* 10->0 transition */
11585 if (geo
->level
== 0)
11586 u
->direction
= R10_TO_R0
;
11588 /* 0->10 transition */
11589 if (geo
->level
== 10)
11590 u
->direction
= R0_TO_R10
;
11592 /* update metadata locally */
11593 imsm_update_metadata_locally(st
, u
,
11594 sizeof(struct imsm_update_takeover
));
11595 /* and possibly remotely */
11596 if (st
->update_tail
)
11597 append_metadata_update(st
, u
,
11598 sizeof(struct imsm_update_takeover
));
11605 static int imsm_reshape_super(struct supertype
*st
, unsigned long long size
,
11607 int layout
, int chunksize
, int raid_disks
,
11608 int delta_disks
, char *backup
, char *dev
,
11609 int direction
, int verbose
)
11612 struct geo_params geo
;
11614 dprintf("(enter)\n");
11616 memset(&geo
, 0, sizeof(struct geo_params
));
11618 geo
.dev_name
= dev
;
11619 strcpy(geo
.devnm
, st
->devnm
);
11622 geo
.layout
= layout
;
11623 geo
.chunksize
= chunksize
;
11624 geo
.raid_disks
= raid_disks
;
11625 if (delta_disks
!= UnSet
)
11626 geo
.raid_disks
+= delta_disks
;
11628 dprintf("for level : %i\n", geo
.level
);
11629 dprintf("for raid_disks : %i\n", geo
.raid_disks
);
11631 if (strcmp(st
->container_devnm
, st
->devnm
) == 0) {
11632 /* On container level we can only increase number of devices. */
11633 dprintf("imsm: info: Container operation\n");
11634 int old_raid_disks
= 0;
11636 if (imsm_reshape_is_allowed_on_container(
11637 st
, &geo
, &old_raid_disks
, direction
)) {
11638 struct imsm_update_reshape
*u
= NULL
;
11641 len
= imsm_create_metadata_update_for_reshape(
11642 st
, &geo
, old_raid_disks
, &u
);
11645 dprintf("imsm: Cannot prepare update\n");
11646 goto exit_imsm_reshape_super
;
11650 /* update metadata locally */
11651 imsm_update_metadata_locally(st
, u
, len
);
11652 /* and possibly remotely */
11653 if (st
->update_tail
)
11654 append_metadata_update(st
, u
, len
);
11659 pr_err("(imsm) Operation is not allowed on this container\n");
11662 /* On volume level we support following operations
11663 * - takeover: raid10 -> raid0; raid0 -> raid10
11664 * - chunk size migration
11665 * - migration: raid5 -> raid0; raid0 -> raid5
11667 struct intel_super
*super
= st
->sb
;
11668 struct intel_dev
*dev
= super
->devlist
;
11670 dprintf("imsm: info: Volume operation\n");
11671 /* find requested device */
11674 imsm_find_array_devnm_by_subdev(
11675 dev
->index
, st
->container_devnm
);
11676 if (devnm
&& strcmp(devnm
, geo
.devnm
) == 0)
11681 pr_err("Cannot find %s (%s) subarray\n",
11682 geo
.dev_name
, geo
.devnm
);
11683 goto exit_imsm_reshape_super
;
11685 super
->current_vol
= dev
->index
;
11686 change
= imsm_analyze_change(st
, &geo
, direction
);
11689 ret_val
= imsm_takeover(st
, &geo
);
11691 case CH_MIGRATION
: {
11692 struct imsm_update_reshape_migration
*u
= NULL
;
11694 imsm_create_metadata_update_for_migration(
11697 dprintf("imsm: Cannot prepare update\n");
11701 /* update metadata locally */
11702 imsm_update_metadata_locally(st
, u
, len
);
11703 /* and possibly remotely */
11704 if (st
->update_tail
)
11705 append_metadata_update(st
, u
, len
);
11710 case CH_ARRAY_SIZE
: {
11711 struct imsm_update_size_change
*u
= NULL
;
11713 imsm_create_metadata_update_for_size_change(
11716 dprintf("imsm: Cannot prepare update\n");
11720 /* update metadata locally */
11721 imsm_update_metadata_locally(st
, u
, len
);
11722 /* and possibly remotely */
11723 if (st
->update_tail
)
11724 append_metadata_update(st
, u
, len
);
11734 exit_imsm_reshape_super
:
11735 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
11739 #define COMPLETED_OK 0
11740 #define COMPLETED_NONE 1
11741 #define COMPLETED_DELAYED 2
11743 static int read_completed(int fd
, unsigned long long *val
)
11748 ret
= sysfs_fd_get_str(fd
, buf
, 50);
11752 ret
= COMPLETED_OK
;
11753 if (strncmp(buf
, "none", 4) == 0) {
11754 ret
= COMPLETED_NONE
;
11755 } else if (strncmp(buf
, "delayed", 7) == 0) {
11756 ret
= COMPLETED_DELAYED
;
11759 *val
= strtoull(buf
, &ep
, 0);
11760 if (ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))
11766 /*******************************************************************************
11767 * Function: wait_for_reshape_imsm
11768 * Description: Function writes new sync_max value and waits until
11769 * reshape process reach new position
11771 * sra : general array info
11772 * ndata : number of disks in new array's layout
11775 * 1 : there is no reshape in progress,
11777 ******************************************************************************/
11778 int wait_for_reshape_imsm(struct mdinfo
*sra
, int ndata
)
11780 int fd
= sysfs_get_fd(sra
, NULL
, "sync_completed");
11782 unsigned long long completed
;
11783 /* to_complete : new sync_max position */
11784 unsigned long long to_complete
= sra
->reshape_progress
;
11785 unsigned long long position_to_set
= to_complete
/ ndata
;
11788 dprintf("cannot open reshape_position\n");
11793 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
11795 dprintf("cannot read reshape_position (no reshape in progres)\n");
11804 if (completed
> position_to_set
) {
11805 dprintf("wrong next position to set %llu (%llu)\n",
11806 to_complete
, position_to_set
);
11810 dprintf("Position set: %llu\n", position_to_set
);
11811 if (sysfs_set_num(sra
, NULL
, "sync_max",
11812 position_to_set
) != 0) {
11813 dprintf("cannot set reshape position to %llu\n",
11822 int timeout
= 3000;
11824 sysfs_wait(fd
, &timeout
);
11825 if (sysfs_get_str(sra
, NULL
, "sync_action",
11827 strncmp(action
, "reshape", 7) != 0) {
11828 if (strncmp(action
, "idle", 4) == 0)
11834 rc
= read_completed(fd
, &completed
);
11836 dprintf("cannot read reshape_position (in loop)\n");
11839 } else if (rc
== COMPLETED_NONE
)
11841 } while (completed
< position_to_set
);
11847 /*******************************************************************************
11848 * Function: check_degradation_change
11849 * Description: Check that array hasn't become failed.
11851 * info : for sysfs access
11852 * sources : source disks descriptors
11853 * degraded: previous degradation level
11855 * degradation level
11856 ******************************************************************************/
11857 int check_degradation_change(struct mdinfo
*info
,
11861 unsigned long long new_degraded
;
11864 rv
= sysfs_get_ll(info
, NULL
, "degraded", &new_degraded
);
11865 if (rv
== -1 || (new_degraded
!= (unsigned long long)degraded
)) {
11866 /* check each device to ensure it is still working */
11869 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
11870 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
11872 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
11875 if (sysfs_get_str(info
,
11876 sd
, "state", sbuf
, sizeof(sbuf
)) < 0 ||
11877 strstr(sbuf
, "faulty") ||
11878 strstr(sbuf
, "in_sync") == NULL
) {
11879 /* this device is dead */
11880 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
11881 if (sd
->disk
.raid_disk
>= 0 &&
11882 sources
[sd
->disk
.raid_disk
] >= 0) {
11884 sd
->disk
.raid_disk
]);
11885 sources
[sd
->disk
.raid_disk
] =
11894 return new_degraded
;
11897 /*******************************************************************************
11898 * Function: imsm_manage_reshape
11899 * Description: Function finds array under reshape and it manages reshape
11900 * process. It creates stripes backups (if required) and sets
11903 * afd : Backup handle (nattive) - not used
11904 * sra : general array info
11905 * reshape : reshape parameters - not used
11906 * st : supertype structure
11907 * blocks : size of critical section [blocks]
11908 * fds : table of source device descriptor
11909 * offsets : start of array (offest per devices)
11911 * destfd : table of destination device descriptor
11912 * destoffsets : table of destination offsets (per device)
11914 * 1 : success, reshape is done
11916 ******************************************************************************/
11917 static int imsm_manage_reshape(
11918 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
11919 struct supertype
*st
, unsigned long backup_blocks
,
11920 int *fds
, unsigned long long *offsets
,
11921 int dests
, int *destfd
, unsigned long long *destoffsets
)
11924 struct intel_super
*super
= st
->sb
;
11925 struct intel_dev
*dv
;
11926 unsigned int sector_size
= super
->sector_size
;
11927 struct imsm_dev
*dev
= NULL
;
11928 struct imsm_map
*map_src
, *map_dest
;
11929 int migr_vol_qan
= 0;
11930 int ndata
, odata
; /* [bytes] */
11931 int chunk
; /* [bytes] */
11932 struct migr_record
*migr_rec
;
11934 unsigned int buf_size
; /* [bytes] */
11935 unsigned long long max_position
; /* array size [bytes] */
11936 unsigned long long next_step
; /* [blocks]/[bytes] */
11937 unsigned long long old_data_stripe_length
;
11938 unsigned long long start_src
; /* [bytes] */
11939 unsigned long long start
; /* [bytes] */
11940 unsigned long long start_buf_shift
; /* [bytes] */
11942 int source_layout
= 0;
11947 if (!fds
|| !offsets
)
11950 /* Find volume during the reshape */
11951 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
11952 if (dv
->dev
->vol
.migr_type
== MIGR_GEN_MIGR
&&
11953 dv
->dev
->vol
.migr_state
== 1) {
11958 /* Only one volume can migrate at the same time */
11959 if (migr_vol_qan
!= 1) {
11960 pr_err("%s", migr_vol_qan
?
11961 "Number of migrating volumes greater than 1\n" :
11962 "There is no volume during migrationg\n");
11966 map_dest
= get_imsm_map(dev
, MAP_0
);
11967 map_src
= get_imsm_map(dev
, MAP_1
);
11968 if (map_src
== NULL
)
11971 ndata
= imsm_num_data_members(map_dest
);
11972 odata
= imsm_num_data_members(map_src
);
11974 chunk
= __le16_to_cpu(map_src
->blocks_per_strip
) * 512;
11975 old_data_stripe_length
= odata
* chunk
;
11977 migr_rec
= super
->migr_rec
;
11979 /* initialize migration record for start condition */
11980 if (sra
->reshape_progress
== 0)
11981 init_migr_record_imsm(st
, dev
, sra
);
11983 if (__le32_to_cpu(migr_rec
->rec_status
) != UNIT_SRC_NORMAL
) {
11984 dprintf("imsm: cannot restart migration when data are present in copy area.\n");
11987 /* Save checkpoint to update migration record for current
11988 * reshape position (in md). It can be farther than current
11989 * reshape position in metadata.
11991 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
11992 /* ignore error == 2, this can mean end of reshape here
11994 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL, initial save)\n");
11999 /* size for data */
12000 buf_size
= __le32_to_cpu(migr_rec
->blocks_per_unit
) * 512;
12001 /* extend buffer size for parity disk */
12002 buf_size
+= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
12003 /* add space for stripe alignment */
12004 buf_size
+= old_data_stripe_length
;
12005 if (posix_memalign((void **)&buf
, MAX_SECTOR_SIZE
, buf_size
)) {
12006 dprintf("imsm: Cannot allocate checkpoint buffer\n");
12010 max_position
= sra
->component_size
* ndata
;
12011 source_layout
= imsm_level_to_layout(map_src
->raid_level
);
12013 while (__le32_to_cpu(migr_rec
->curr_migr_unit
) <
12014 __le32_to_cpu(migr_rec
->num_migr_units
)) {
12015 /* current reshape position [blocks] */
12016 unsigned long long current_position
=
12017 __le32_to_cpu(migr_rec
->blocks_per_unit
)
12018 * __le32_to_cpu(migr_rec
->curr_migr_unit
);
12019 unsigned long long border
;
12021 /* Check that array hasn't become failed.
12023 degraded
= check_degradation_change(sra
, fds
, degraded
);
12024 if (degraded
> 1) {
12025 dprintf("imsm: Abort reshape due to degradation level (%i)\n", degraded
);
12029 next_step
= __le32_to_cpu(migr_rec
->blocks_per_unit
);
12031 if ((current_position
+ next_step
) > max_position
)
12032 next_step
= max_position
- current_position
;
12034 start
= current_position
* 512;
12036 /* align reading start to old geometry */
12037 start_buf_shift
= start
% old_data_stripe_length
;
12038 start_src
= start
- start_buf_shift
;
12040 border
= (start_src
/ odata
) - (start
/ ndata
);
12042 if (border
<= __le32_to_cpu(migr_rec
->dest_depth_per_unit
)) {
12043 /* save critical stripes to buf
12044 * start - start address of current unit
12045 * to backup [bytes]
12046 * start_src - start address of current unit
12047 * to backup alligned to source array
12050 unsigned long long next_step_filler
;
12051 unsigned long long copy_length
= next_step
* 512;
12053 /* allign copy area length to stripe in old geometry */
12054 next_step_filler
= ((copy_length
+ start_buf_shift
)
12055 % old_data_stripe_length
);
12056 if (next_step_filler
)
12057 next_step_filler
= (old_data_stripe_length
12058 - next_step_filler
);
12059 dprintf("save_stripes() parameters: start = %llu,\tstart_src = %llu,\tnext_step*512 = %llu,\tstart_in_buf_shift = %llu,\tnext_step_filler = %llu\n",
12060 start
, start_src
, copy_length
,
12061 start_buf_shift
, next_step_filler
);
12063 if (save_stripes(fds
, offsets
, map_src
->num_members
,
12064 chunk
, map_src
->raid_level
,
12065 source_layout
, 0, NULL
, start_src
,
12067 next_step_filler
+ start_buf_shift
,
12069 dprintf("imsm: Cannot save stripes to buffer\n");
12072 /* Convert data to destination format and store it
12073 * in backup general migration area
12075 if (save_backup_imsm(st
, dev
, sra
,
12076 buf
+ start_buf_shift
, copy_length
)) {
12077 dprintf("imsm: Cannot save stripes to target devices\n");
12080 if (save_checkpoint_imsm(st
, sra
,
12081 UNIT_SRC_IN_CP_AREA
)) {
12082 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_IN_CP_AREA)\n");
12086 /* set next step to use whole border area */
12087 border
/= next_step
;
12089 next_step
*= border
;
12091 /* When data backed up, checkpoint stored,
12092 * kick the kernel to reshape unit of data
12094 next_step
= next_step
+ sra
->reshape_progress
;
12095 /* limit next step to array max position */
12096 if (next_step
> max_position
)
12097 next_step
= max_position
;
12098 sysfs_set_num(sra
, NULL
, "suspend_lo", sra
->reshape_progress
);
12099 sysfs_set_num(sra
, NULL
, "suspend_hi", next_step
);
12100 sra
->reshape_progress
= next_step
;
12102 /* wait until reshape finish */
12103 if (wait_for_reshape_imsm(sra
, ndata
)) {
12104 dprintf("wait_for_reshape_imsm returned error!\n");
12110 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
12111 /* ignore error == 2, this can mean end of reshape here
12113 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL)\n");
12119 /* clear migr_rec on disks after successful migration */
12122 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
);
12123 for (d
= super
->disks
; d
; d
= d
->next
) {
12124 if (d
->index
< 0 || is_failed(&d
->disk
))
12126 unsigned long long dsize
;
12128 get_dev_size(d
->fd
, NULL
, &dsize
);
12129 if (lseek64(d
->fd
, dsize
- MIGR_REC_SECTOR_POSITION
*sector_size
,
12131 if ((unsigned int)write(d
->fd
, super
->migr_rec_buf
,
12132 MIGR_REC_BUF_SECTORS
*sector_size
) !=
12133 MIGR_REC_BUF_SECTORS
*sector_size
)
12134 perror("Write migr_rec failed");
12138 /* return '1' if done */
12142 /* See Grow.c: abort_reshape() for further explanation */
12143 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
12144 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
12145 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
12150 struct superswitch super_imsm
= {
12151 .examine_super
= examine_super_imsm
,
12152 .brief_examine_super
= brief_examine_super_imsm
,
12153 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
12154 .export_examine_super
= export_examine_super_imsm
,
12155 .detail_super
= detail_super_imsm
,
12156 .brief_detail_super
= brief_detail_super_imsm
,
12157 .write_init_super
= write_init_super_imsm
,
12158 .validate_geometry
= validate_geometry_imsm
,
12159 .add_to_super
= add_to_super_imsm
,
12160 .remove_from_super
= remove_from_super_imsm
,
12161 .detail_platform
= detail_platform_imsm
,
12162 .export_detail_platform
= export_detail_platform_imsm
,
12163 .kill_subarray
= kill_subarray_imsm
,
12164 .update_subarray
= update_subarray_imsm
,
12165 .load_container
= load_container_imsm
,
12166 .default_geometry
= default_geometry_imsm
,
12167 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
12168 .reshape_super
= imsm_reshape_super
,
12169 .manage_reshape
= imsm_manage_reshape
,
12170 .recover_backup
= recover_backup_imsm
,
12171 .copy_metadata
= copy_metadata_imsm
,
12172 .examine_badblocks
= examine_badblocks_imsm
,
12173 .match_home
= match_home_imsm
,
12174 .uuid_from_super
= uuid_from_super_imsm
,
12175 .getinfo_super
= getinfo_super_imsm
,
12176 .getinfo_super_disks
= getinfo_super_disks_imsm
,
12177 .update_super
= update_super_imsm
,
12179 .avail_size
= avail_size_imsm
,
12180 .get_spare_criteria
= get_spare_criteria_imsm
,
12182 .compare_super
= compare_super_imsm
,
12184 .load_super
= load_super_imsm
,
12185 .init_super
= init_super_imsm
,
12186 .store_super
= store_super_imsm
,
12187 .free_super
= free_super_imsm
,
12188 .match_metadata_desc
= match_metadata_desc_imsm
,
12189 .container_content
= container_content_imsm
,
12190 .validate_container
= validate_container_imsm
,
12192 .write_init_ppl
= write_init_ppl_imsm
,
12193 .validate_ppl
= validate_ppl_imsm
,
12199 .open_new
= imsm_open_new
,
12200 .set_array_state
= imsm_set_array_state
,
12201 .set_disk
= imsm_set_disk
,
12202 .sync_metadata
= imsm_sync_metadata
,
12203 .activate_spare
= imsm_activate_spare
,
12204 .process_update
= imsm_process_update
,
12205 .prepare_update
= imsm_prepare_update
,
12206 .record_bad_block
= imsm_record_badblock
,
12207 .clear_bad_block
= imsm_clear_badblock
,
12208 .get_bad_blocks
= imsm_get_badblocks
,