]>
Commit | Line | Data |
---|---|---|
cdddbdbc DW |
1 | /* |
2 | * mdadm - Intel(R) Matrix Storage Manager Support | |
3 | * | |
a54d5262 | 4 | * Copyright (C) 2002-2008 Intel Corporation |
cdddbdbc DW |
5 | * |
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. | |
9 | * | |
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 | |
13 | * more details. | |
14 | * | |
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. | |
18 | */ | |
19 | ||
51006d85 | 20 | #define HAVE_STDINT_H 1 |
cdddbdbc | 21 | #include "mdadm.h" |
c2a1e7da | 22 | #include "mdmon.h" |
420dafcd | 23 | #include "dlink.h" |
51006d85 | 24 | #include "sha1.h" |
88c32bb1 | 25 | #include "platform-intel.h" |
cdddbdbc DW |
26 | #include <values.h> |
27 | #include <scsi/sg.h> | |
28 | #include <ctype.h> | |
d665cc31 | 29 | #include <dirent.h> |
bf62ed5d | 30 | #include "drive_encryption.h" |
cdddbdbc DW |
31 | |
32 | /* MPB == Metadata Parameter Block */ | |
33 | #define MPB_SIGNATURE "Intel Raid ISM Cfg Sig. " | |
34 | #define MPB_SIG_LEN (strlen(MPB_SIGNATURE)) | |
35 | #define MPB_VERSION_RAID0 "1.0.00" | |
36 | #define MPB_VERSION_RAID1 "1.1.00" | |
fe7ed8cb DW |
37 | #define MPB_VERSION_MANY_VOLUMES_PER_ARRAY "1.2.00" |
38 | #define MPB_VERSION_3OR4_DISK_ARRAY "1.2.01" | |
cdddbdbc | 39 | #define MPB_VERSION_RAID5 "1.2.02" |
fe7ed8cb DW |
40 | #define MPB_VERSION_5OR6_DISK_ARRAY "1.2.04" |
41 | #define MPB_VERSION_CNG "1.2.06" | |
42 | #define MPB_VERSION_ATTRIBS "1.3.00" | |
cdddbdbc DW |
43 | #define MAX_SIGNATURE_LENGTH 32 |
44 | #define MAX_RAID_SERIAL_LEN 16 | |
fe7ed8cb | 45 | |
19482bcc AK |
46 | /* supports RAID0 */ |
47 | #define MPB_ATTRIB_RAID0 __cpu_to_le32(0x00000001) | |
48 | /* supports RAID1 */ | |
49 | #define MPB_ATTRIB_RAID1 __cpu_to_le32(0x00000002) | |
50 | /* supports RAID10 */ | |
51 | #define MPB_ATTRIB_RAID10 __cpu_to_le32(0x00000004) | |
52 | /* supports RAID1E */ | |
53 | #define MPB_ATTRIB_RAID1E __cpu_to_le32(0x00000008) | |
54 | /* supports RAID5 */ | |
55 | #define MPB_ATTRIB_RAID5 __cpu_to_le32(0x00000010) | |
56 | /* supports RAID CNG */ | |
57 | #define MPB_ATTRIB_RAIDCNG __cpu_to_le32(0x00000020) | |
58 | /* supports expanded stripe sizes of 256K, 512K and 1MB */ | |
59 | #define MPB_ATTRIB_EXP_STRIPE_SIZE __cpu_to_le32(0x00000040) | |
60 | ||
61 | /* The OROM Support RST Caching of Volumes */ | |
62 | #define MPB_ATTRIB_NVM __cpu_to_le32(0x02000000) | |
63 | /* The OROM supports creating disks greater than 2TB */ | |
64 | #define MPB_ATTRIB_2TB_DISK __cpu_to_le32(0x04000000) | |
65 | /* The OROM supports Bad Block Management */ | |
66 | #define MPB_ATTRIB_BBM __cpu_to_le32(0x08000000) | |
67 | ||
68 | /* THe OROM Supports NVM Caching of Volumes */ | |
69 | #define MPB_ATTRIB_NEVER_USE2 __cpu_to_le32(0x10000000) | |
70 | /* The OROM supports creating volumes greater than 2TB */ | |
71 | #define MPB_ATTRIB_2TB __cpu_to_le32(0x20000000) | |
72 | /* originally for PMP, now it's wasted b/c. Never use this bit! */ | |
73 | #define MPB_ATTRIB_NEVER_USE __cpu_to_le32(0x40000000) | |
74 | /* Verify MPB contents against checksum after reading MPB */ | |
75 | #define MPB_ATTRIB_CHECKSUM_VERIFY __cpu_to_le32(0x80000000) | |
76 | ||
77 | /* Define all supported attributes that have to be accepted by mdadm | |
78 | */ | |
418f9b36 | 79 | #define MPB_ATTRIB_SUPPORTED (MPB_ATTRIB_CHECKSUM_VERIFY | \ |
19482bcc AK |
80 | MPB_ATTRIB_2TB | \ |
81 | MPB_ATTRIB_2TB_DISK | \ | |
82 | MPB_ATTRIB_RAID0 | \ | |
83 | MPB_ATTRIB_RAID1 | \ | |
84 | MPB_ATTRIB_RAID10 | \ | |
85 | MPB_ATTRIB_RAID5 | \ | |
bbab0940 TM |
86 | MPB_ATTRIB_EXP_STRIPE_SIZE | \ |
87 | MPB_ATTRIB_BBM) | |
418f9b36 N |
88 | |
89 | /* Define attributes that are unused but not harmful */ | |
90 | #define MPB_ATTRIB_IGNORED (MPB_ATTRIB_NEVER_USE) | |
fe7ed8cb | 91 | |
8e59f3d8 | 92 | #define MPB_SECTOR_CNT 2210 |
611d9529 MD |
93 | #define IMSM_RESERVED_SECTORS 8192 |
94 | #define NUM_BLOCKS_DIRTY_STRIPE_REGION 2048 | |
979d38be | 95 | #define SECT_PER_MB_SHIFT 11 |
f36a9ecd | 96 | #define MAX_SECTOR_SIZE 4096 |
c2462068 PB |
97 | #define MULTIPLE_PPL_AREA_SIZE_IMSM (1024 * 1024) /* Size of the whole |
98 | * mutliple PPL area | |
99 | */ | |
cdddbdbc | 100 | |
fbc42556 JR |
101 | /* |
102 | * Internal Write-intent bitmap is stored in the same area where PPL. | |
103 | * Both features are mutually exclusive, so it is not an issue. | |
104 | * The first 8KiB of the area are reserved and shall not be used. | |
105 | */ | |
106 | #define IMSM_BITMAP_AREA_RESERVED_SIZE 8192 | |
107 | ||
108 | #define IMSM_BITMAP_HEADER_OFFSET (IMSM_BITMAP_AREA_RESERVED_SIZE) | |
109 | #define IMSM_BITMAP_HEADER_SIZE MAX_SECTOR_SIZE | |
110 | ||
111 | #define IMSM_BITMAP_START_OFFSET (IMSM_BITMAP_HEADER_OFFSET + IMSM_BITMAP_HEADER_SIZE) | |
112 | #define IMSM_BITMAP_AREA_SIZE (MULTIPLE_PPL_AREA_SIZE_IMSM - IMSM_BITMAP_START_OFFSET) | |
113 | #define IMSM_BITMAP_AND_HEADER_SIZE (IMSM_BITMAP_AREA_SIZE + IMSM_BITMAP_HEADER_SIZE) | |
114 | ||
115 | #define IMSM_DEFAULT_BITMAP_CHUNKSIZE (64 * 1024 * 1024) | |
116 | #define IMSM_DEFAULT_BITMAP_DAEMON_SLEEP 5 | |
117 | ||
761e3bd9 N |
118 | /* |
119 | * This macro let's us ensure that no-one accidentally | |
120 | * changes the size of a struct | |
121 | */ | |
122 | #define ASSERT_SIZE(_struct, size) \ | |
123 | static inline void __assert_size_##_struct(void) \ | |
124 | { \ | |
125 | switch (0) { \ | |
126 | case 0: break; \ | |
127 | case (sizeof(struct _struct) == size): break; \ | |
128 | } \ | |
129 | } | |
130 | ||
cdddbdbc DW |
131 | /* Disk configuration info. */ |
132 | #define IMSM_MAX_DEVICES 255 | |
133 | struct imsm_disk { | |
134 | __u8 serial[MAX_RAID_SERIAL_LEN];/* 0xD8 - 0xE7 ascii serial number */ | |
5551b113 | 135 | __u32 total_blocks_lo; /* 0xE8 - 0xEB total blocks lo */ |
cdddbdbc | 136 | __u32 scsi_id; /* 0xEC - 0xEF scsi ID */ |
f2f27e63 DW |
137 | #define SPARE_DISK __cpu_to_le32(0x01) /* Spare */ |
138 | #define CONFIGURED_DISK __cpu_to_le32(0x02) /* Member of some RaidDev */ | |
139 | #define FAILED_DISK __cpu_to_le32(0x04) /* Permanent failure */ | |
2432ce9b | 140 | #define JOURNAL_DISK __cpu_to_le32(0x2000000) /* Device marked as Journaling Drive */ |
cdddbdbc | 141 | __u32 status; /* 0xF0 - 0xF3 */ |
1011e834 | 142 | __u32 owner_cfg_num; /* which config 0,1,2... owns this disk */ |
5551b113 CA |
143 | __u32 total_blocks_hi; /* 0xF4 - 0xF5 total blocks hi */ |
144 | #define IMSM_DISK_FILLERS 3 | |
145 | __u32 filler[IMSM_DISK_FILLERS]; /* 0xF5 - 0x107 MPB_DISK_FILLERS for future expansion */ | |
cdddbdbc | 146 | }; |
761e3bd9 | 147 | ASSERT_SIZE(imsm_disk, 48) |
cdddbdbc | 148 | |
3b451610 AK |
149 | /* map selector for map managment |
150 | */ | |
238c0a71 AK |
151 | #define MAP_0 0 |
152 | #define MAP_1 1 | |
153 | #define MAP_X -1 | |
3b451610 | 154 | |
cdddbdbc DW |
155 | /* RAID map configuration infos. */ |
156 | struct imsm_map { | |
5551b113 CA |
157 | __u32 pba_of_lba0_lo; /* start address of partition */ |
158 | __u32 blocks_per_member_lo;/* blocks per member */ | |
159 | __u32 num_data_stripes_lo; /* number of data stripes */ | |
cdddbdbc DW |
160 | __u16 blocks_per_strip; |
161 | __u8 map_state; /* Normal, Uninitialized, Degraded, Failed */ | |
162 | #define IMSM_T_STATE_NORMAL 0 | |
163 | #define IMSM_T_STATE_UNINITIALIZED 1 | |
e3bba0e0 DW |
164 | #define IMSM_T_STATE_DEGRADED 2 |
165 | #define IMSM_T_STATE_FAILED 3 | |
cdddbdbc DW |
166 | __u8 raid_level; |
167 | #define IMSM_T_RAID0 0 | |
168 | #define IMSM_T_RAID1 1 | |
169 | #define IMSM_T_RAID5 5 /* since metadata version 1.2.02 ? */ | |
170 | __u8 num_members; /* number of member disks */ | |
fe7ed8cb DW |
171 | __u8 num_domains; /* number of parity domains */ |
172 | __u8 failed_disk_num; /* valid only when state is degraded */ | |
252d23c0 | 173 | __u8 ddf; |
5551b113 CA |
174 | __u32 pba_of_lba0_hi; |
175 | __u32 blocks_per_member_hi; | |
176 | __u32 num_data_stripes_hi; | |
177 | __u32 filler[4]; /* expansion area */ | |
7eef0453 | 178 | #define IMSM_ORD_REBUILD (1 << 24) |
cdddbdbc | 179 | __u32 disk_ord_tbl[1]; /* disk_ord_tbl[num_members], |
7eef0453 DW |
180 | * top byte contains some flags |
181 | */ | |
761e3bd9 N |
182 | }; |
183 | ASSERT_SIZE(imsm_map, 52) | |
cdddbdbc DW |
184 | |
185 | struct imsm_vol { | |
4036e7ee | 186 | __u32 curr_migr_unit_lo; |
fe7ed8cb | 187 | __u32 checkpoint_id; /* id to access curr_migr_unit */ |
cdddbdbc | 188 | __u8 migr_state; /* Normal or Migrating */ |
e3bba0e0 DW |
189 | #define MIGR_INIT 0 |
190 | #define MIGR_REBUILD 1 | |
191 | #define MIGR_VERIFY 2 /* analagous to echo check > sync_action */ | |
192 | #define MIGR_GEN_MIGR 3 | |
193 | #define MIGR_STATE_CHANGE 4 | |
1484e727 | 194 | #define MIGR_REPAIR 5 |
cdddbdbc | 195 | __u8 migr_type; /* Initializing, Rebuilding, ... */ |
2432ce9b AP |
196 | #define RAIDVOL_CLEAN 0 |
197 | #define RAIDVOL_DIRTY 1 | |
198 | #define RAIDVOL_DSRECORD_VALID 2 | |
cdddbdbc | 199 | __u8 dirty; |
fe7ed8cb DW |
200 | __u8 fs_state; /* fast-sync state for CnG (0xff == disabled) */ |
201 | __u16 verify_errors; /* number of mismatches */ | |
202 | __u16 bad_blocks; /* number of bad blocks during verify */ | |
4036e7ee MT |
203 | __u32 curr_migr_unit_hi; |
204 | __u32 filler[3]; | |
cdddbdbc DW |
205 | struct imsm_map map[1]; |
206 | /* here comes another one if migr_state */ | |
761e3bd9 N |
207 | }; |
208 | ASSERT_SIZE(imsm_vol, 84) | |
cdddbdbc DW |
209 | |
210 | struct imsm_dev { | |
fe7ed8cb | 211 | __u8 volume[MAX_RAID_SERIAL_LEN]; |
cdddbdbc DW |
212 | __u32 size_low; |
213 | __u32 size_high; | |
fe7ed8cb DW |
214 | #define DEV_BOOTABLE __cpu_to_le32(0x01) |
215 | #define DEV_BOOT_DEVICE __cpu_to_le32(0x02) | |
216 | #define DEV_READ_COALESCING __cpu_to_le32(0x04) | |
217 | #define DEV_WRITE_COALESCING __cpu_to_le32(0x08) | |
218 | #define DEV_LAST_SHUTDOWN_DIRTY __cpu_to_le32(0x10) | |
219 | #define DEV_HIDDEN_AT_BOOT __cpu_to_le32(0x20) | |
220 | #define DEV_CURRENTLY_HIDDEN __cpu_to_le32(0x40) | |
221 | #define DEV_VERIFY_AND_FIX __cpu_to_le32(0x80) | |
222 | #define DEV_MAP_STATE_UNINIT __cpu_to_le32(0x100) | |
223 | #define DEV_NO_AUTO_RECOVERY __cpu_to_le32(0x200) | |
224 | #define DEV_CLONE_N_GO __cpu_to_le32(0x400) | |
225 | #define DEV_CLONE_MAN_SYNC __cpu_to_le32(0x800) | |
226 | #define DEV_CNG_MASTER_DISK_NUM __cpu_to_le32(0x1000) | |
cdddbdbc DW |
227 | __u32 status; /* Persistent RaidDev status */ |
228 | __u32 reserved_blocks; /* Reserved blocks at beginning of volume */ | |
fe7ed8cb DW |
229 | __u8 migr_priority; |
230 | __u8 num_sub_vols; | |
231 | __u8 tid; | |
232 | __u8 cng_master_disk; | |
233 | __u16 cache_policy; | |
234 | __u8 cng_state; | |
235 | __u8 cng_sub_state; | |
2432ce9b AP |
236 | __u16 my_vol_raid_dev_num; /* Used in Unique volume Id for this RaidDev */ |
237 | ||
238 | /* NVM_EN */ | |
239 | __u8 nv_cache_mode; | |
240 | __u8 nv_cache_flags; | |
241 | ||
242 | /* Unique Volume Id of the NvCache Volume associated with this volume */ | |
243 | __u32 nvc_vol_orig_family_num; | |
244 | __u16 nvc_vol_raid_dev_num; | |
245 | ||
246 | #define RWH_OFF 0 | |
247 | #define RWH_DISTRIBUTED 1 | |
248 | #define RWH_JOURNALING_DRIVE 2 | |
c2462068 PB |
249 | #define RWH_MULTIPLE_DISTRIBUTED 3 |
250 | #define RWH_MULTIPLE_PPLS_JOURNALING_DRIVE 4 | |
251 | #define RWH_MULTIPLE_OFF 5 | |
fbc42556 | 252 | #define RWH_BITMAP 6 |
2432ce9b AP |
253 | __u8 rwh_policy; /* Raid Write Hole Policy */ |
254 | __u8 jd_serial[MAX_RAID_SERIAL_LEN]; /* Journal Drive serial number */ | |
255 | __u8 filler1; | |
256 | ||
257 | #define IMSM_DEV_FILLERS 3 | |
cdddbdbc DW |
258 | __u32 filler[IMSM_DEV_FILLERS]; |
259 | struct imsm_vol vol; | |
761e3bd9 N |
260 | }; |
261 | ASSERT_SIZE(imsm_dev, 164) | |
cdddbdbc DW |
262 | |
263 | struct imsm_super { | |
264 | __u8 sig[MAX_SIGNATURE_LENGTH]; /* 0x00 - 0x1F */ | |
265 | __u32 check_sum; /* 0x20 - 0x23 MPB Checksum */ | |
266 | __u32 mpb_size; /* 0x24 - 0x27 Size of MPB */ | |
267 | __u32 family_num; /* 0x28 - 0x2B Checksum from first time this config was written */ | |
268 | __u32 generation_num; /* 0x2C - 0x2F Incremented each time this array's MPB is written */ | |
604b746f JD |
269 | __u32 error_log_size; /* 0x30 - 0x33 in bytes */ |
270 | __u32 attributes; /* 0x34 - 0x37 */ | |
cdddbdbc DW |
271 | __u8 num_disks; /* 0x38 Number of configured disks */ |
272 | __u8 num_raid_devs; /* 0x39 Number of configured volumes */ | |
604b746f JD |
273 | __u8 error_log_pos; /* 0x3A */ |
274 | __u8 fill[1]; /* 0x3B */ | |
275 | __u32 cache_size; /* 0x3c - 0x40 in mb */ | |
276 | __u32 orig_family_num; /* 0x40 - 0x43 original family num */ | |
277 | __u32 pwr_cycle_count; /* 0x44 - 0x47 simulated power cycle count for array */ | |
278 | __u32 bbm_log_size; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */ | |
2a24dc1b PB |
279 | __u16 num_raid_devs_created; /* 0x4C - 0x4D Used for generating unique |
280 | * volume IDs for raid_dev created in this array | |
281 | * (starts at 1) | |
282 | */ | |
283 | __u16 filler1; /* 0x4E - 0x4F */ | |
e48aed3c AP |
284 | __u64 creation_time; /* 0x50 - 0x57 Array creation time */ |
285 | #define IMSM_FILLERS 32 | |
286 | __u32 filler[IMSM_FILLERS]; /* 0x58 - 0xD7 RAID_MPB_FILLERS */ | |
cdddbdbc DW |
287 | struct imsm_disk disk[1]; /* 0xD8 diskTbl[numDisks] */ |
288 | /* here comes imsm_dev[num_raid_devs] */ | |
604b746f | 289 | /* here comes BBM logs */ |
761e3bd9 N |
290 | }; |
291 | ASSERT_SIZE(imsm_super, 264) | |
cdddbdbc | 292 | |
604b746f | 293 | #define BBM_LOG_MAX_ENTRIES 254 |
8d67477f TM |
294 | #define BBM_LOG_MAX_LBA_ENTRY_VAL 256 /* Represents 256 LBAs */ |
295 | #define BBM_LOG_SIGNATURE 0xabadb10c | |
296 | ||
297 | struct bbm_log_block_addr { | |
298 | __u16 w1; | |
299 | __u32 dw1; | |
300 | } __attribute__ ((__packed__)); | |
604b746f JD |
301 | |
302 | struct bbm_log_entry { | |
8d67477f TM |
303 | __u8 marked_count; /* Number of blocks marked - 1 */ |
304 | __u8 disk_ordinal; /* Disk entry within the imsm_super */ | |
305 | struct bbm_log_block_addr defective_block_start; | |
604b746f JD |
306 | } __attribute__ ((__packed__)); |
307 | ||
308 | struct bbm_log { | |
309 | __u32 signature; /* 0xABADB10C */ | |
310 | __u32 entry_count; | |
8d67477f | 311 | struct bbm_log_entry marked_block_entries[BBM_LOG_MAX_ENTRIES]; |
761e3bd9 N |
312 | }; |
313 | ASSERT_SIZE(bbm_log, 2040) | |
604b746f | 314 | |
cdddbdbc | 315 | static char *map_state_str[] = { "normal", "uninitialized", "degraded", "failed" }; |
cdddbdbc | 316 | |
b53bfba6 TM |
317 | #define BLOCKS_PER_KB (1024/512) |
318 | ||
8e59f3d8 AK |
319 | #define RAID_DISK_RESERVED_BLOCKS_IMSM_HI 2209 |
320 | ||
321 | #define GEN_MIGR_AREA_SIZE 2048 /* General Migration Copy Area size in blocks */ | |
322 | ||
de44e46f PB |
323 | #define MIGR_REC_BUF_SECTORS 1 /* size of migr_record i/o buffer in sectors */ |
324 | #define MIGR_REC_SECTOR_POSITION 1 /* migr_record position offset on disk, | |
325 | * MIGR_REC_BUF_SECTORS <= MIGR_REC_SECTOR_POS | |
17a4eaf9 AK |
326 | */ |
327 | ||
8e59f3d8 AK |
328 | #define UNIT_SRC_NORMAL 0 /* Source data for curr_migr_unit must |
329 | * be recovered using srcMap */ | |
330 | #define UNIT_SRC_IN_CP_AREA 1 /* Source data for curr_migr_unit has | |
331 | * already been migrated and must | |
332 | * be recovered from checkpoint area */ | |
2432ce9b | 333 | |
c2462068 | 334 | #define PPL_ENTRY_SPACE (128 * 1024) /* Size of single PPL, without the header */ |
2432ce9b | 335 | |
8e59f3d8 AK |
336 | struct migr_record { |
337 | __u32 rec_status; /* Status used to determine how to restart | |
338 | * migration in case it aborts | |
339 | * in some fashion */ | |
9f421827 | 340 | __u32 curr_migr_unit_lo; /* 0..numMigrUnits-1 */ |
8e59f3d8 AK |
341 | __u32 family_num; /* Family number of MPB |
342 | * containing the RaidDev | |
343 | * that is migrating */ | |
344 | __u32 ascending_migr; /* True if migrating in increasing | |
345 | * order of lbas */ | |
346 | __u32 blocks_per_unit; /* Num disk blocks per unit of operation */ | |
347 | __u32 dest_depth_per_unit; /* Num member blocks each destMap | |
348 | * member disk | |
349 | * advances per unit-of-operation */ | |
9f421827 PB |
350 | __u32 ckpt_area_pba_lo; /* Pba of first block of ckpt copy area */ |
351 | __u32 dest_1st_member_lba_lo; /* First member lba on first | |
352 | * stripe of destination */ | |
353 | __u32 num_migr_units_lo; /* Total num migration units-of-op */ | |
8e59f3d8 AK |
354 | __u32 post_migr_vol_cap; /* Size of volume after |
355 | * migration completes */ | |
356 | __u32 post_migr_vol_cap_hi; /* Expansion space for LBA64 */ | |
357 | __u32 ckpt_read_disk_num; /* Which member disk in destSubMap[0] the | |
358 | * migration ckpt record was read from | |
359 | * (for recovered migrations) */ | |
9f421827 PB |
360 | __u32 curr_migr_unit_hi; /* 0..numMigrUnits-1 high order 32 bits */ |
361 | __u32 ckpt_area_pba_hi; /* Pba of first block of ckpt copy area | |
362 | * high order 32 bits */ | |
363 | __u32 dest_1st_member_lba_hi; /* First member lba on first stripe of | |
364 | * destination - high order 32 bits */ | |
365 | __u32 num_migr_units_hi; /* Total num migration units-of-op | |
366 | * high order 32 bits */ | |
4036e7ee | 367 | __u32 filler[16]; |
761e3bd9 | 368 | }; |
4036e7ee | 369 | ASSERT_SIZE(migr_record, 128) |
8e59f3d8 | 370 | |
76c152ca MT |
371 | /** |
372 | * enum imsm_status - internal IMSM return values representation. | |
373 | * @STATUS_OK: function succeeded. | |
374 | * @STATUS_ERROR: General error ocurred (not specified). | |
375 | * | |
376 | * Typedefed to imsm_status_t. | |
377 | */ | |
378 | typedef enum imsm_status { | |
379 | IMSM_STATUS_ERROR = -1, | |
380 | IMSM_STATUS_OK = 0, | |
381 | } imsm_status_t; | |
382 | ||
ec50f7b6 LM |
383 | struct md_list { |
384 | /* usage marker: | |
385 | * 1: load metadata | |
386 | * 2: metadata does not match | |
387 | * 4: already checked | |
388 | */ | |
389 | int used; | |
390 | char *devname; | |
391 | int found; | |
392 | int container; | |
393 | dev_t st_rdev; | |
394 | struct md_list *next; | |
395 | }; | |
396 | ||
1484e727 DW |
397 | static __u8 migr_type(struct imsm_dev *dev) |
398 | { | |
399 | if (dev->vol.migr_type == MIGR_VERIFY && | |
400 | dev->status & DEV_VERIFY_AND_FIX) | |
401 | return MIGR_REPAIR; | |
402 | else | |
403 | return dev->vol.migr_type; | |
404 | } | |
405 | ||
406 | static void set_migr_type(struct imsm_dev *dev, __u8 migr_type) | |
407 | { | |
408 | /* for compatibility with older oroms convert MIGR_REPAIR, into | |
409 | * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status | |
410 | */ | |
411 | if (migr_type == MIGR_REPAIR) { | |
412 | dev->vol.migr_type = MIGR_VERIFY; | |
413 | dev->status |= DEV_VERIFY_AND_FIX; | |
414 | } else { | |
415 | dev->vol.migr_type = migr_type; | |
416 | dev->status &= ~DEV_VERIFY_AND_FIX; | |
417 | } | |
418 | } | |
419 | ||
f36a9ecd | 420 | static unsigned int sector_count(__u32 bytes, unsigned int sector_size) |
cdddbdbc | 421 | { |
f36a9ecd | 422 | return ROUND_UP(bytes, sector_size) / sector_size; |
87eb16df | 423 | } |
cdddbdbc | 424 | |
f36a9ecd PB |
425 | static unsigned int mpb_sectors(struct imsm_super *mpb, |
426 | unsigned int sector_size) | |
87eb16df | 427 | { |
f36a9ecd | 428 | return sector_count(__le32_to_cpu(mpb->mpb_size), sector_size); |
cdddbdbc DW |
429 | } |
430 | ||
ba2de7ba DW |
431 | struct intel_dev { |
432 | struct imsm_dev *dev; | |
433 | struct intel_dev *next; | |
f21e18ca | 434 | unsigned index; |
ba2de7ba DW |
435 | }; |
436 | ||
88654014 LM |
437 | struct intel_hba { |
438 | enum sys_dev_type type; | |
439 | char *path; | |
440 | char *pci_id; | |
441 | struct intel_hba *next; | |
442 | }; | |
443 | ||
1a64be56 LM |
444 | enum action { |
445 | DISK_REMOVE = 1, | |
446 | DISK_ADD | |
447 | }; | |
cdddbdbc DW |
448 | /* internal representation of IMSM metadata */ |
449 | struct intel_super { | |
450 | union { | |
949c47a0 DW |
451 | void *buf; /* O_DIRECT buffer for reading/writing metadata */ |
452 | struct imsm_super *anchor; /* immovable parameters */ | |
cdddbdbc | 453 | }; |
8e59f3d8 AK |
454 | union { |
455 | void *migr_rec_buf; /* buffer for I/O operations */ | |
456 | struct migr_record *migr_rec; /* migration record */ | |
457 | }; | |
51d83f5d AK |
458 | int clean_migration_record_by_mdmon; /* when reshape is switched to next |
459 | array, it indicates that mdmon is allowed to clean migration | |
460 | record */ | |
949c47a0 | 461 | size_t len; /* size of the 'buf' allocation */ |
bbab0940 | 462 | size_t extra_space; /* extra space in 'buf' that is not used yet */ |
4d7b1503 DW |
463 | void *next_buf; /* for realloc'ing buf from the manager */ |
464 | size_t next_len; | |
c2c087e6 | 465 | int updates_pending; /* count of pending updates for mdmon */ |
bf5a934a | 466 | int current_vol; /* index of raid device undergoing creation */ |
5551b113 | 467 | unsigned long long create_offset; /* common start for 'current_vol' */ |
148acb7b | 468 | __u32 random; /* random data for seeding new family numbers */ |
ba2de7ba | 469 | struct intel_dev *devlist; |
fa7bb6f8 | 470 | unsigned int sector_size; /* sector size of used member drives */ |
cdddbdbc DW |
471 | struct dl { |
472 | struct dl *next; | |
473 | int index; | |
474 | __u8 serial[MAX_RAID_SERIAL_LEN]; | |
475 | int major, minor; | |
476 | char *devname; | |
b9f594fe | 477 | struct imsm_disk disk; |
cdddbdbc | 478 | int fd; |
0dcecb2e DW |
479 | int extent_cnt; |
480 | struct extent *e; /* for determining freespace @ create */ | |
efb30e7f | 481 | int raiddisk; /* slot to fill in autolayout */ |
1a64be56 | 482 | enum action action; |
ca0748fa | 483 | } *disks, *current_disk; |
1a64be56 LM |
484 | struct dl *disk_mgmt_list; /* list of disks to add/remove while mdmon |
485 | active */ | |
47ee5a45 | 486 | struct dl *missing; /* disks removed while we weren't looking */ |
43dad3d6 | 487 | struct bbm_log *bbm_log; |
88654014 | 488 | struct intel_hba *hba; /* device path of the raid controller for this metadata */ |
88c32bb1 | 489 | const struct imsm_orom *orom; /* platform firmware support */ |
a2b97981 | 490 | struct intel_super *next; /* (temp) list for disambiguating family_num */ |
928f1424 | 491 | struct md_bb bb; /* memory for get_bad_blocks call */ |
a2b97981 DW |
492 | }; |
493 | ||
494 | struct intel_disk { | |
495 | struct imsm_disk disk; | |
496 | #define IMSM_UNKNOWN_OWNER (-1) | |
497 | int owner; | |
498 | struct intel_disk *next; | |
cdddbdbc DW |
499 | }; |
500 | ||
aa19fdd4 MT |
501 | /** |
502 | * struct extent - reserved space details. | |
503 | * @start: start offset. | |
504 | * @size: size of reservation, set to 0 for metadata reservation. | |
505 | * @vol: index of the volume, meaningful if &size is set. | |
506 | */ | |
c2c087e6 DW |
507 | struct extent { |
508 | unsigned long long start, size; | |
aa19fdd4 | 509 | int vol; |
c2c087e6 DW |
510 | }; |
511 | ||
694575e7 KW |
512 | /* definitions of reshape process types */ |
513 | enum imsm_reshape_type { | |
514 | CH_TAKEOVER, | |
b5347799 | 515 | CH_MIGRATION, |
7abc9871 | 516 | CH_ARRAY_SIZE, |
694575e7 KW |
517 | }; |
518 | ||
88758e9d DW |
519 | /* definition of messages passed to imsm_process_update */ |
520 | enum imsm_update_type { | |
521 | update_activate_spare, | |
8273f55e | 522 | update_create_array, |
33414a01 | 523 | update_kill_array, |
aa534678 | 524 | update_rename_array, |
1a64be56 | 525 | update_add_remove_disk, |
78b10e66 | 526 | update_reshape_container_disks, |
48c5303a | 527 | update_reshape_migration, |
2d40f3a1 AK |
528 | update_takeover, |
529 | update_general_migration_checkpoint, | |
f3871fdc | 530 | update_size_change, |
bbab0940 | 531 | update_prealloc_badblocks_mem, |
e6e9dd3f | 532 | update_rwh_policy, |
88758e9d DW |
533 | }; |
534 | ||
535 | struct imsm_update_activate_spare { | |
536 | enum imsm_update_type type; | |
d23fe947 | 537 | struct dl *dl; |
88758e9d DW |
538 | int slot; |
539 | int array; | |
540 | struct imsm_update_activate_spare *next; | |
541 | }; | |
542 | ||
78b10e66 | 543 | struct geo_params { |
4dd2df09 | 544 | char devnm[32]; |
78b10e66 | 545 | char *dev_name; |
d04f65f4 | 546 | unsigned long long size; |
78b10e66 N |
547 | int level; |
548 | int layout; | |
549 | int chunksize; | |
550 | int raid_disks; | |
551 | }; | |
552 | ||
bb025c2f KW |
553 | enum takeover_direction { |
554 | R10_TO_R0, | |
555 | R0_TO_R10 | |
556 | }; | |
557 | struct imsm_update_takeover { | |
558 | enum imsm_update_type type; | |
559 | int subarray; | |
560 | enum takeover_direction direction; | |
561 | }; | |
78b10e66 N |
562 | |
563 | struct imsm_update_reshape { | |
564 | enum imsm_update_type type; | |
565 | int old_raid_disks; | |
566 | int new_raid_disks; | |
48c5303a PC |
567 | |
568 | int new_disks[1]; /* new_raid_disks - old_raid_disks makedev number */ | |
569 | }; | |
570 | ||
571 | struct imsm_update_reshape_migration { | |
572 | enum imsm_update_type type; | |
573 | int old_raid_disks; | |
574 | int new_raid_disks; | |
575 | /* fields for array migration changes | |
576 | */ | |
577 | int subdev; | |
578 | int new_level; | |
579 | int new_layout; | |
4bba0439 | 580 | int new_chunksize; |
48c5303a | 581 | |
d195167d | 582 | int new_disks[1]; /* new_raid_disks - old_raid_disks makedev number */ |
78b10e66 N |
583 | }; |
584 | ||
f3871fdc AK |
585 | struct imsm_update_size_change { |
586 | enum imsm_update_type type; | |
587 | int subdev; | |
588 | long long new_size; | |
589 | }; | |
590 | ||
2d40f3a1 AK |
591 | struct imsm_update_general_migration_checkpoint { |
592 | enum imsm_update_type type; | |
4036e7ee | 593 | __u64 curr_migr_unit; |
2d40f3a1 AK |
594 | }; |
595 | ||
54c2c1ea DW |
596 | struct disk_info { |
597 | __u8 serial[MAX_RAID_SERIAL_LEN]; | |
598 | }; | |
599 | ||
8273f55e DW |
600 | struct imsm_update_create_array { |
601 | enum imsm_update_type type; | |
8273f55e | 602 | int dev_idx; |
6a3e913e | 603 | struct imsm_dev dev; |
8273f55e DW |
604 | }; |
605 | ||
33414a01 DW |
606 | struct imsm_update_kill_array { |
607 | enum imsm_update_type type; | |
608 | int dev_idx; | |
609 | }; | |
610 | ||
aa534678 DW |
611 | struct imsm_update_rename_array { |
612 | enum imsm_update_type type; | |
613 | __u8 name[MAX_RAID_SERIAL_LEN]; | |
614 | int dev_idx; | |
615 | }; | |
616 | ||
1a64be56 | 617 | struct imsm_update_add_remove_disk { |
43dad3d6 DW |
618 | enum imsm_update_type type; |
619 | }; | |
620 | ||
bbab0940 TM |
621 | struct imsm_update_prealloc_bb_mem { |
622 | enum imsm_update_type type; | |
623 | }; | |
624 | ||
e6e9dd3f AP |
625 | struct imsm_update_rwh_policy { |
626 | enum imsm_update_type type; | |
627 | int new_policy; | |
628 | int dev_idx; | |
629 | }; | |
630 | ||
88654014 LM |
631 | static const char *_sys_dev_type[] = { |
632 | [SYS_DEV_UNKNOWN] = "Unknown", | |
633 | [SYS_DEV_SAS] = "SAS", | |
614902f6 | 634 | [SYS_DEV_SATA] = "SATA", |
60f0f54d | 635 | [SYS_DEV_NVME] = "NVMe", |
75350d87 KF |
636 | [SYS_DEV_VMD] = "VMD", |
637 | [SYS_DEV_SATA_VMD] = "SATA VMD" | |
88654014 LM |
638 | }; |
639 | ||
420dafcd N |
640 | static int no_platform = -1; |
641 | ||
642 | static int check_no_platform(void) | |
643 | { | |
644 | static const char search[] = "mdadm.imsm.test=1"; | |
645 | FILE *fp; | |
646 | ||
647 | if (no_platform >= 0) | |
648 | return no_platform; | |
649 | ||
650 | if (check_env("IMSM_NO_PLATFORM")) { | |
651 | no_platform = 1; | |
652 | return 1; | |
653 | } | |
654 | fp = fopen("/proc/cmdline", "r"); | |
655 | if (fp) { | |
656 | char *l = conf_line(fp); | |
657 | char *w = l; | |
658 | ||
cf1577bf MG |
659 | if (l == NULL) { |
660 | fclose(fp); | |
661 | return 0; | |
662 | } | |
663 | ||
420dafcd N |
664 | do { |
665 | if (strcmp(w, search) == 0) | |
666 | no_platform = 1; | |
667 | w = dl_next(w); | |
668 | } while (w != l); | |
669 | free_line(l); | |
670 | fclose(fp); | |
671 | if (no_platform >= 0) | |
672 | return no_platform; | |
673 | } | |
674 | no_platform = 0; | |
675 | return 0; | |
676 | } | |
677 | ||
678 | void imsm_set_no_platform(int v) | |
679 | { | |
680 | no_platform = v; | |
681 | } | |
682 | ||
88654014 LM |
683 | const char *get_sys_dev_type(enum sys_dev_type type) |
684 | { | |
685 | if (type >= SYS_DEV_MAX) | |
686 | type = SYS_DEV_UNKNOWN; | |
687 | ||
688 | return _sys_dev_type[type]; | |
689 | } | |
690 | ||
691 | static struct intel_hba * alloc_intel_hba(struct sys_dev *device) | |
692 | { | |
503975b9 N |
693 | struct intel_hba *result = xmalloc(sizeof(*result)); |
694 | ||
695 | result->type = device->type; | |
696 | result->path = xstrdup(device->path); | |
697 | result->next = NULL; | |
698 | if (result->path && (result->pci_id = strrchr(result->path, '/')) != NULL) | |
699 | result->pci_id++; | |
700 | ||
88654014 LM |
701 | return result; |
702 | } | |
703 | ||
704 | static struct intel_hba * find_intel_hba(struct intel_hba *hba, struct sys_dev *device) | |
705 | { | |
594dc1b8 JS |
706 | struct intel_hba *result; |
707 | ||
88654014 LM |
708 | for (result = hba; result; result = result->next) { |
709 | if (result->type == device->type && strcmp(result->path, device->path) == 0) | |
710 | break; | |
711 | } | |
712 | return result; | |
713 | } | |
714 | ||
b4cf4cba | 715 | static int attach_hba_to_super(struct intel_super *super, struct sys_dev *device) |
88654014 LM |
716 | { |
717 | struct intel_hba *hba; | |
718 | ||
719 | /* check if disk attached to Intel HBA */ | |
720 | hba = find_intel_hba(super->hba, device); | |
721 | if (hba != NULL) | |
722 | return 1; | |
723 | /* Check if HBA is already attached to super */ | |
724 | if (super->hba == NULL) { | |
725 | super->hba = alloc_intel_hba(device); | |
726 | return 1; | |
6b781d33 AP |
727 | } |
728 | ||
729 | hba = super->hba; | |
730 | /* Intel metadata allows for all disks attached to the same type HBA. | |
614902f6 | 731 | * Do not support HBA types mixing |
6b781d33 AP |
732 | */ |
733 | if (device->type != hba->type) | |
88654014 | 734 | return 2; |
6b781d33 AP |
735 | |
736 | /* Multiple same type HBAs can be used if they share the same OROM */ | |
737 | const struct imsm_orom *device_orom = get_orom_by_device_id(device->dev_id); | |
738 | ||
739 | if (device_orom != super->orom) | |
740 | return 2; | |
741 | ||
742 | while (hba->next) | |
743 | hba = hba->next; | |
744 | ||
745 | hba->next = alloc_intel_hba(device); | |
746 | return 1; | |
88654014 LM |
747 | } |
748 | ||
749 | static struct sys_dev* find_disk_attached_hba(int fd, const char *devname) | |
750 | { | |
9bc4ae77 | 751 | struct sys_dev *list, *elem; |
88654014 LM |
752 | char *disk_path; |
753 | ||
754 | if ((list = find_intel_devices()) == NULL) | |
755 | return 0; | |
756 | ||
4389ce73 | 757 | if (!is_fd_valid(fd)) |
88654014 LM |
758 | disk_path = (char *) devname; |
759 | else | |
7c798f87 | 760 | disk_path = diskfd_to_devpath(fd, 1, NULL); |
88654014 | 761 | |
9bc4ae77 | 762 | if (!disk_path) |
88654014 | 763 | return 0; |
88654014 | 764 | |
9bc4ae77 N |
765 | for (elem = list; elem; elem = elem->next) |
766 | if (path_attached_to_hba(disk_path, elem->path)) | |
5d2434d1 | 767 | break; |
9bc4ae77 | 768 | |
88654014 LM |
769 | if (disk_path != devname) |
770 | free(disk_path); | |
88654014 | 771 | |
5d2434d1 | 772 | return elem; |
88654014 LM |
773 | } |
774 | ||
d424212e N |
775 | static int find_intel_hba_capability(int fd, struct intel_super *super, |
776 | char *devname); | |
f2f5c343 | 777 | |
cdddbdbc DW |
778 | static struct supertype *match_metadata_desc_imsm(char *arg) |
779 | { | |
780 | struct supertype *st; | |
781 | ||
782 | if (strcmp(arg, "imsm") != 0 && | |
783 | strcmp(arg, "default") != 0 | |
784 | ) | |
785 | return NULL; | |
786 | ||
503975b9 | 787 | st = xcalloc(1, sizeof(*st)); |
cdddbdbc DW |
788 | st->ss = &super_imsm; |
789 | st->max_devs = IMSM_MAX_DEVICES; | |
790 | st->minor_version = 0; | |
791 | st->sb = NULL; | |
792 | return st; | |
793 | } | |
794 | ||
cdddbdbc DW |
795 | static __u8 *get_imsm_version(struct imsm_super *mpb) |
796 | { | |
797 | return &mpb->sig[MPB_SIG_LEN]; | |
798 | } | |
799 | ||
949c47a0 DW |
800 | /* retrieve a disk directly from the anchor when the anchor is known to be |
801 | * up-to-date, currently only at load time | |
802 | */ | |
803 | static struct imsm_disk *__get_imsm_disk(struct imsm_super *mpb, __u8 index) | |
cdddbdbc | 804 | { |
949c47a0 | 805 | if (index >= mpb->num_disks) |
cdddbdbc DW |
806 | return NULL; |
807 | return &mpb->disk[index]; | |
808 | } | |
809 | ||
95d07a2c LM |
810 | /* retrieve the disk description based on a index of the disk |
811 | * in the sub-array | |
812 | */ | |
813 | static struct dl *get_imsm_dl_disk(struct intel_super *super, __u8 index) | |
949c47a0 | 814 | { |
b9f594fe DW |
815 | struct dl *d; |
816 | ||
817 | for (d = super->disks; d; d = d->next) | |
818 | if (d->index == index) | |
95d07a2c LM |
819 | return d; |
820 | ||
821 | return NULL; | |
822 | } | |
823 | /* retrieve a disk from the parsed metadata */ | |
824 | static struct imsm_disk *get_imsm_disk(struct intel_super *super, __u8 index) | |
825 | { | |
826 | struct dl *dl; | |
827 | ||
828 | dl = get_imsm_dl_disk(super, index); | |
829 | if (dl) | |
830 | return &dl->disk; | |
831 | ||
b9f594fe | 832 | return NULL; |
949c47a0 DW |
833 | } |
834 | ||
835 | /* generate a checksum directly from the anchor when the anchor is known to be | |
836 | * up-to-date, currently only at load or write_super after coalescing | |
837 | */ | |
838 | static __u32 __gen_imsm_checksum(struct imsm_super *mpb) | |
cdddbdbc DW |
839 | { |
840 | __u32 end = mpb->mpb_size / sizeof(end); | |
841 | __u32 *p = (__u32 *) mpb; | |
842 | __u32 sum = 0; | |
843 | ||
5d500228 N |
844 | while (end--) { |
845 | sum += __le32_to_cpu(*p); | |
97f734fd N |
846 | p++; |
847 | } | |
cdddbdbc | 848 | |
5d500228 | 849 | return sum - __le32_to_cpu(mpb->check_sum); |
cdddbdbc DW |
850 | } |
851 | ||
a965f303 DW |
852 | static size_t sizeof_imsm_map(struct imsm_map *map) |
853 | { | |
854 | return sizeof(struct imsm_map) + sizeof(__u32) * (map->num_members - 1); | |
855 | } | |
856 | ||
857 | struct imsm_map *get_imsm_map(struct imsm_dev *dev, int second_map) | |
cdddbdbc | 858 | { |
5e7b0330 AK |
859 | /* A device can have 2 maps if it is in the middle of a migration. |
860 | * If second_map is: | |
238c0a71 AK |
861 | * MAP_0 - we return the first map |
862 | * MAP_1 - we return the second map if it exists, else NULL | |
863 | * MAP_X - we return the second map if it exists, else the first | |
5e7b0330 | 864 | */ |
a965f303 | 865 | struct imsm_map *map = &dev->vol.map[0]; |
9535fc47 | 866 | struct imsm_map *map2 = NULL; |
a965f303 | 867 | |
9535fc47 AK |
868 | if (dev->vol.migr_state) |
869 | map2 = (void *)map + sizeof_imsm_map(map); | |
a965f303 | 870 | |
9535fc47 | 871 | switch (second_map) { |
3b451610 | 872 | case MAP_0: |
9535fc47 | 873 | break; |
3b451610 | 874 | case MAP_1: |
9535fc47 AK |
875 | map = map2; |
876 | break; | |
238c0a71 | 877 | case MAP_X: |
9535fc47 AK |
878 | if (map2) |
879 | map = map2; | |
880 | break; | |
9535fc47 AK |
881 | default: |
882 | map = NULL; | |
883 | } | |
884 | return map; | |
5e7b0330 | 885 | |
a965f303 | 886 | } |
cdddbdbc | 887 | |
3393c6af DW |
888 | /* return the size of the device. |
889 | * migr_state increases the returned size if map[0] were to be duplicated | |
890 | */ | |
891 | static size_t sizeof_imsm_dev(struct imsm_dev *dev, int migr_state) | |
a965f303 DW |
892 | { |
893 | size_t size = sizeof(*dev) - sizeof(struct imsm_map) + | |
238c0a71 | 894 | sizeof_imsm_map(get_imsm_map(dev, MAP_0)); |
cdddbdbc DW |
895 | |
896 | /* migrating means an additional map */ | |
a965f303 | 897 | if (dev->vol.migr_state) |
238c0a71 | 898 | size += sizeof_imsm_map(get_imsm_map(dev, MAP_1)); |
3393c6af | 899 | else if (migr_state) |
238c0a71 | 900 | size += sizeof_imsm_map(get_imsm_map(dev, MAP_0)); |
cdddbdbc DW |
901 | |
902 | return size; | |
903 | } | |
904 | ||
54c2c1ea DW |
905 | /* retrieve disk serial number list from a metadata update */ |
906 | static struct disk_info *get_disk_info(struct imsm_update_create_array *update) | |
907 | { | |
908 | void *u = update; | |
909 | struct disk_info *inf; | |
910 | ||
911 | inf = u + sizeof(*update) - sizeof(struct imsm_dev) + | |
912 | sizeof_imsm_dev(&update->dev, 0); | |
913 | ||
914 | return inf; | |
915 | } | |
54c2c1ea | 916 | |
756a15f3 MG |
917 | /** |
918 | * __get_imsm_dev() - Get device with index from imsm_super. | |
919 | * @mpb: &imsm_super pointer, not NULL. | |
920 | * @index: Device index. | |
921 | * | |
922 | * Function works as non-NULL, aborting in such a case, | |
923 | * when NULL would be returned. | |
924 | * | |
925 | * Device index should be in range 0 up to num_raid_devs. | |
926 | * Function assumes the index was already verified. | |
927 | * Index must be valid, otherwise abort() is called. | |
928 | * | |
929 | * Return: Pointer to corresponding imsm_dev. | |
930 | * | |
931 | */ | |
949c47a0 | 932 | static struct imsm_dev *__get_imsm_dev(struct imsm_super *mpb, __u8 index) |
cdddbdbc DW |
933 | { |
934 | int offset; | |
935 | int i; | |
936 | void *_mpb = mpb; | |
937 | ||
949c47a0 | 938 | if (index >= mpb->num_raid_devs) |
756a15f3 | 939 | goto error; |
cdddbdbc DW |
940 | |
941 | /* devices start after all disks */ | |
942 | offset = ((void *) &mpb->disk[mpb->num_disks]) - _mpb; | |
943 | ||
756a15f3 | 944 | for (i = 0; i <= index; i++, offset += sizeof_imsm_dev(_mpb + offset, 0)) |
cdddbdbc DW |
945 | if (i == index) |
946 | return _mpb + offset; | |
756a15f3 MG |
947 | error: |
948 | pr_err("cannot find imsm_dev with index %u in imsm_super\n", index); | |
949 | abort(); | |
cdddbdbc DW |
950 | } |
951 | ||
756a15f3 MG |
952 | /** |
953 | * get_imsm_dev() - Get device with index from intel_super. | |
954 | * @super: &intel_super pointer, not NULL. | |
955 | * @index: Device index. | |
956 | * | |
957 | * Function works as non-NULL, aborting in such a case, | |
958 | * when NULL would be returned. | |
959 | * | |
960 | * Device index should be in range 0 up to num_raid_devs. | |
961 | * Function assumes the index was already verified. | |
962 | * Index must be valid, otherwise abort() is called. | |
963 | * | |
964 | * Return: Pointer to corresponding imsm_dev. | |
965 | * | |
966 | */ | |
949c47a0 DW |
967 | static struct imsm_dev *get_imsm_dev(struct intel_super *super, __u8 index) |
968 | { | |
ba2de7ba DW |
969 | struct intel_dev *dv; |
970 | ||
949c47a0 | 971 | if (index >= super->anchor->num_raid_devs) |
756a15f3 MG |
972 | goto error; |
973 | ||
ba2de7ba DW |
974 | for (dv = super->devlist; dv; dv = dv->next) |
975 | if (dv->index == index) | |
976 | return dv->dev; | |
756a15f3 MG |
977 | error: |
978 | pr_err("cannot find imsm_dev with index %u in intel_super\n", index); | |
979 | abort(); | |
949c47a0 DW |
980 | } |
981 | ||
8d67477f TM |
982 | static inline unsigned long long __le48_to_cpu(const struct bbm_log_block_addr |
983 | *addr) | |
984 | { | |
985 | return ((((__u64)__le32_to_cpu(addr->dw1)) << 16) | | |
986 | __le16_to_cpu(addr->w1)); | |
987 | } | |
988 | ||
989 | static inline struct bbm_log_block_addr __cpu_to_le48(unsigned long long sec) | |
990 | { | |
991 | struct bbm_log_block_addr addr; | |
992 | ||
993 | addr.w1 = __cpu_to_le16((__u16)(sec & 0xffff)); | |
994 | addr.dw1 = __cpu_to_le32((__u32)(sec >> 16) & 0xffffffff); | |
995 | return addr; | |
996 | } | |
997 | ||
8d67477f TM |
998 | /* get size of the bbm log */ |
999 | static __u32 get_imsm_bbm_log_size(struct bbm_log *log) | |
1000 | { | |
1001 | if (!log || log->entry_count == 0) | |
1002 | return 0; | |
1003 | ||
1004 | return sizeof(log->signature) + | |
1005 | sizeof(log->entry_count) + | |
1006 | log->entry_count * sizeof(struct bbm_log_entry); | |
1007 | } | |
6f50473f TM |
1008 | |
1009 | /* check if bad block is not partially stored in bbm log */ | |
1010 | static int is_stored_in_bbm(struct bbm_log *log, const __u8 idx, const unsigned | |
1011 | long long sector, const int length, __u32 *pos) | |
1012 | { | |
1013 | __u32 i; | |
1014 | ||
1015 | for (i = *pos; i < log->entry_count; i++) { | |
1016 | struct bbm_log_entry *entry = &log->marked_block_entries[i]; | |
1017 | unsigned long long bb_start; | |
1018 | unsigned long long bb_end; | |
1019 | ||
1020 | bb_start = __le48_to_cpu(&entry->defective_block_start); | |
1021 | bb_end = bb_start + (entry->marked_count + 1); | |
1022 | ||
1023 | if ((entry->disk_ordinal == idx) && (bb_start >= sector) && | |
1024 | (bb_end <= sector + length)) { | |
1025 | *pos = i; | |
1026 | return 1; | |
1027 | } | |
1028 | } | |
1029 | return 0; | |
1030 | } | |
1031 | ||
1032 | /* record new bad block in bbm log */ | |
1033 | static int record_new_badblock(struct bbm_log *log, const __u8 idx, unsigned | |
1034 | long long sector, int length) | |
1035 | { | |
1036 | int new_bb = 0; | |
1037 | __u32 pos = 0; | |
1038 | struct bbm_log_entry *entry = NULL; | |
1039 | ||
1040 | while (is_stored_in_bbm(log, idx, sector, length, &pos)) { | |
1041 | struct bbm_log_entry *e = &log->marked_block_entries[pos]; | |
1042 | ||
1043 | if ((e->marked_count + 1 == BBM_LOG_MAX_LBA_ENTRY_VAL) && | |
1044 | (__le48_to_cpu(&e->defective_block_start) == sector)) { | |
1045 | sector += BBM_LOG_MAX_LBA_ENTRY_VAL; | |
1046 | length -= BBM_LOG_MAX_LBA_ENTRY_VAL; | |
1047 | pos = pos + 1; | |
1048 | continue; | |
1049 | } | |
1050 | entry = e; | |
1051 | break; | |
1052 | } | |
1053 | ||
1054 | if (entry) { | |
1055 | int cnt = (length <= BBM_LOG_MAX_LBA_ENTRY_VAL) ? length : | |
1056 | BBM_LOG_MAX_LBA_ENTRY_VAL; | |
1057 | entry->defective_block_start = __cpu_to_le48(sector); | |
1058 | entry->marked_count = cnt - 1; | |
1059 | if (cnt == length) | |
1060 | return 1; | |
1061 | sector += cnt; | |
1062 | length -= cnt; | |
1063 | } | |
1064 | ||
1065 | new_bb = ROUND_UP(length, BBM_LOG_MAX_LBA_ENTRY_VAL) / | |
1066 | BBM_LOG_MAX_LBA_ENTRY_VAL; | |
1067 | if (log->entry_count + new_bb > BBM_LOG_MAX_ENTRIES) | |
1068 | return 0; | |
1069 | ||
1070 | while (length > 0) { | |
1071 | int cnt = (length <= BBM_LOG_MAX_LBA_ENTRY_VAL) ? length : | |
1072 | BBM_LOG_MAX_LBA_ENTRY_VAL; | |
1073 | struct bbm_log_entry *entry = | |
1074 | &log->marked_block_entries[log->entry_count]; | |
1075 | ||
1076 | entry->defective_block_start = __cpu_to_le48(sector); | |
1077 | entry->marked_count = cnt - 1; | |
1078 | entry->disk_ordinal = idx; | |
1079 | ||
1080 | sector += cnt; | |
1081 | length -= cnt; | |
1082 | ||
1083 | log->entry_count++; | |
1084 | } | |
1085 | ||
1086 | return new_bb; | |
1087 | } | |
c07a5a4f | 1088 | |
4c9e8c1e TM |
1089 | /* clear all bad blocks for given disk */ |
1090 | static void clear_disk_badblocks(struct bbm_log *log, const __u8 idx) | |
1091 | { | |
1092 | __u32 i = 0; | |
1093 | ||
1094 | while (i < log->entry_count) { | |
1095 | struct bbm_log_entry *entries = log->marked_block_entries; | |
1096 | ||
1097 | if (entries[i].disk_ordinal == idx) { | |
1098 | if (i < log->entry_count - 1) | |
1099 | entries[i] = entries[log->entry_count - 1]; | |
1100 | log->entry_count--; | |
1101 | } else { | |
1102 | i++; | |
1103 | } | |
1104 | } | |
1105 | } | |
1106 | ||
c07a5a4f TM |
1107 | /* clear given bad block */ |
1108 | static int clear_badblock(struct bbm_log *log, const __u8 idx, const unsigned | |
1109 | long long sector, const int length) { | |
1110 | __u32 i = 0; | |
1111 | ||
1112 | while (i < log->entry_count) { | |
1113 | struct bbm_log_entry *entries = log->marked_block_entries; | |
1114 | ||
1115 | if ((entries[i].disk_ordinal == idx) && | |
1116 | (__le48_to_cpu(&entries[i].defective_block_start) == | |
1117 | sector) && (entries[i].marked_count + 1 == length)) { | |
1118 | if (i < log->entry_count - 1) | |
1119 | entries[i] = entries[log->entry_count - 1]; | |
1120 | log->entry_count--; | |
1121 | break; | |
1122 | } | |
1123 | i++; | |
1124 | } | |
1125 | ||
1126 | return 1; | |
1127 | } | |
8d67477f TM |
1128 | |
1129 | /* allocate and load BBM log from metadata */ | |
1130 | static int load_bbm_log(struct intel_super *super) | |
1131 | { | |
1132 | struct imsm_super *mpb = super->anchor; | |
1133 | __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size); | |
1134 | ||
1135 | super->bbm_log = xcalloc(1, sizeof(struct bbm_log)); | |
1136 | if (!super->bbm_log) | |
1137 | return 1; | |
1138 | ||
1139 | if (bbm_log_size) { | |
1140 | struct bbm_log *log = (void *)mpb + | |
1141 | __le32_to_cpu(mpb->mpb_size) - bbm_log_size; | |
1142 | ||
1143 | __u32 entry_count; | |
1144 | ||
1145 | if (bbm_log_size < sizeof(log->signature) + | |
1146 | sizeof(log->entry_count)) | |
1147 | return 2; | |
1148 | ||
1149 | entry_count = __le32_to_cpu(log->entry_count); | |
1150 | if ((__le32_to_cpu(log->signature) != BBM_LOG_SIGNATURE) || | |
1151 | (entry_count > BBM_LOG_MAX_ENTRIES)) | |
1152 | return 3; | |
1153 | ||
1154 | if (bbm_log_size != | |
1155 | sizeof(log->signature) + sizeof(log->entry_count) + | |
1156 | entry_count * sizeof(struct bbm_log_entry)) | |
1157 | return 4; | |
1158 | ||
1159 | memcpy(super->bbm_log, log, bbm_log_size); | |
1160 | } else { | |
1161 | super->bbm_log->signature = __cpu_to_le32(BBM_LOG_SIGNATURE); | |
1162 | super->bbm_log->entry_count = 0; | |
1163 | } | |
1164 | ||
1165 | return 0; | |
1166 | } | |
1167 | ||
b12796be TM |
1168 | /* checks if bad block is within volume boundaries */ |
1169 | static int is_bad_block_in_volume(const struct bbm_log_entry *entry, | |
1170 | const unsigned long long start_sector, | |
1171 | const unsigned long long size) | |
1172 | { | |
1173 | unsigned long long bb_start; | |
1174 | unsigned long long bb_end; | |
1175 | ||
1176 | bb_start = __le48_to_cpu(&entry->defective_block_start); | |
1177 | bb_end = bb_start + (entry->marked_count + 1); | |
1178 | ||
1179 | if (((bb_start >= start_sector) && (bb_start < start_sector + size)) || | |
1180 | ((bb_end >= start_sector) && (bb_end <= start_sector + size))) | |
1181 | return 1; | |
1182 | ||
1183 | return 0; | |
1184 | } | |
1185 | ||
1186 | /* get list of bad blocks on a drive for a volume */ | |
1187 | static void get_volume_badblocks(const struct bbm_log *log, const __u8 idx, | |
1188 | const unsigned long long start_sector, | |
1189 | const unsigned long long size, | |
1190 | struct md_bb *bbs) | |
1191 | { | |
1192 | __u32 count = 0; | |
1193 | __u32 i; | |
1194 | ||
1195 | for (i = 0; i < log->entry_count; i++) { | |
1196 | const struct bbm_log_entry *ent = | |
1197 | &log->marked_block_entries[i]; | |
1198 | struct md_bb_entry *bb; | |
1199 | ||
1200 | if ((ent->disk_ordinal == idx) && | |
1201 | is_bad_block_in_volume(ent, start_sector, size)) { | |
1202 | ||
1203 | if (!bbs->entries) { | |
1204 | bbs->entries = xmalloc(BBM_LOG_MAX_ENTRIES * | |
1205 | sizeof(*bb)); | |
1206 | if (!bbs->entries) | |
1207 | break; | |
1208 | } | |
1209 | ||
1210 | bb = &bbs->entries[count++]; | |
1211 | bb->sector = __le48_to_cpu(&ent->defective_block_start); | |
1212 | bb->length = ent->marked_count + 1; | |
1213 | } | |
1214 | } | |
1215 | bbs->count = count; | |
1216 | } | |
1217 | ||
98130f40 AK |
1218 | /* |
1219 | * for second_map: | |
238c0a71 AK |
1220 | * == MAP_0 get first map |
1221 | * == MAP_1 get second map | |
1222 | * == MAP_X than get map according to the current migr_state | |
98130f40 AK |
1223 | */ |
1224 | static __u32 get_imsm_ord_tbl_ent(struct imsm_dev *dev, | |
1225 | int slot, | |
1226 | int second_map) | |
7eef0453 DW |
1227 | { |
1228 | struct imsm_map *map; | |
1229 | ||
5e7b0330 | 1230 | map = get_imsm_map(dev, second_map); |
7eef0453 | 1231 | |
ff077194 DW |
1232 | /* top byte identifies disk under rebuild */ |
1233 | return __le32_to_cpu(map->disk_ord_tbl[slot]); | |
1234 | } | |
1235 | ||
1236 | #define ord_to_idx(ord) (((ord) << 8) >> 8) | |
98130f40 | 1237 | static __u32 get_imsm_disk_idx(struct imsm_dev *dev, int slot, int second_map) |
ff077194 | 1238 | { |
98130f40 | 1239 | __u32 ord = get_imsm_ord_tbl_ent(dev, slot, second_map); |
ff077194 DW |
1240 | |
1241 | return ord_to_idx(ord); | |
7eef0453 DW |
1242 | } |
1243 | ||
be73972f DW |
1244 | static void set_imsm_ord_tbl_ent(struct imsm_map *map, int slot, __u32 ord) |
1245 | { | |
1246 | map->disk_ord_tbl[slot] = __cpu_to_le32(ord); | |
1247 | } | |
1248 | ||
76c152ca | 1249 | static int get_imsm_disk_slot(struct imsm_map *map, const unsigned int idx) |
620b1713 DW |
1250 | { |
1251 | int slot; | |
1252 | __u32 ord; | |
1253 | ||
1254 | for (slot = 0; slot < map->num_members; slot++) { | |
1255 | ord = __le32_to_cpu(map->disk_ord_tbl[slot]); | |
1256 | if (ord_to_idx(ord) == idx) | |
1257 | return slot; | |
1258 | } | |
1259 | ||
76c152ca | 1260 | return IMSM_STATUS_ERROR; |
620b1713 DW |
1261 | } |
1262 | ||
cdddbdbc DW |
1263 | static int get_imsm_raid_level(struct imsm_map *map) |
1264 | { | |
1265 | if (map->raid_level == 1) { | |
1266 | if (map->num_members == 2) | |
1267 | return 1; | |
1268 | else | |
1269 | return 10; | |
1270 | } | |
1271 | ||
1272 | return map->raid_level; | |
1273 | } | |
1274 | ||
76c152ca MT |
1275 | /** |
1276 | * get_disk_slot_in_dev() - retrieve disk slot from &imsm_dev. | |
1277 | * @super: &intel_super pointer, not NULL. | |
1278 | * @dev_idx: imsm device index. | |
1279 | * @idx: disk index. | |
1280 | * | |
1281 | * Return: Slot on success, IMSM_STATUS_ERROR otherwise. | |
1282 | */ | |
1283 | static int get_disk_slot_in_dev(struct intel_super *super, const __u8 dev_idx, | |
1284 | const unsigned int idx) | |
1285 | { | |
1286 | struct imsm_dev *dev = get_imsm_dev(super, dev_idx); | |
1287 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
1288 | ||
1289 | return get_imsm_disk_slot(map, idx); | |
1290 | } | |
1291 | ||
c2c087e6 DW |
1292 | static int cmp_extent(const void *av, const void *bv) |
1293 | { | |
1294 | const struct extent *a = av; | |
1295 | const struct extent *b = bv; | |
1296 | if (a->start < b->start) | |
1297 | return -1; | |
1298 | if (a->start > b->start) | |
1299 | return 1; | |
1300 | return 0; | |
1301 | } | |
1302 | ||
0dcecb2e | 1303 | static int count_memberships(struct dl *dl, struct intel_super *super) |
c2c087e6 | 1304 | { |
c2c087e6 | 1305 | int memberships = 0; |
620b1713 | 1306 | int i; |
c2c087e6 | 1307 | |
76c152ca MT |
1308 | for (i = 0; i < super->anchor->num_raid_devs; i++) |
1309 | if (get_disk_slot_in_dev(super, i, dl->index) >= 0) | |
620b1713 | 1310 | memberships++; |
0dcecb2e DW |
1311 | |
1312 | return memberships; | |
1313 | } | |
1314 | ||
b81221b7 CA |
1315 | static __u32 imsm_min_reserved_sectors(struct intel_super *super); |
1316 | ||
486720e0 | 1317 | static int split_ull(unsigned long long n, void *lo, void *hi) |
5551b113 CA |
1318 | { |
1319 | if (lo == 0 || hi == 0) | |
1320 | return 1; | |
486720e0 JS |
1321 | __put_unaligned32(__cpu_to_le32((__u32)n), lo); |
1322 | __put_unaligned32(__cpu_to_le32((n >> 32)), hi); | |
5551b113 CA |
1323 | return 0; |
1324 | } | |
1325 | ||
1326 | static unsigned long long join_u32(__u32 lo, __u32 hi) | |
1327 | { | |
1328 | return (unsigned long long)__le32_to_cpu(lo) | | |
1329 | (((unsigned long long)__le32_to_cpu(hi)) << 32); | |
1330 | } | |
1331 | ||
1332 | static unsigned long long total_blocks(struct imsm_disk *disk) | |
1333 | { | |
1334 | if (disk == NULL) | |
1335 | return 0; | |
1336 | return join_u32(disk->total_blocks_lo, disk->total_blocks_hi); | |
1337 | } | |
1338 | ||
1c275381 MT |
1339 | /** |
1340 | * imsm_num_data_members() - get data drives count for an array. | |
1341 | * @map: Map to analyze. | |
1342 | * | |
1343 | * num_data_members value represents minimal count of drives for level. | |
1344 | * The name of the property could be misleading for RAID5 with asymmetric layout | |
1345 | * because some data required to be calculated from parity. | |
1346 | * The property is extracted from level and num_members value. | |
1347 | * | |
1348 | * Return: num_data_members value on success, zero otherwise. | |
1349 | */ | |
1350 | static __u8 imsm_num_data_members(struct imsm_map *map) | |
1351 | { | |
1352 | switch (get_imsm_raid_level(map)) { | |
1353 | case 0: | |
1354 | return map->num_members; | |
1355 | case 1: | |
1356 | case 10: | |
1357 | return map->num_members / 2; | |
1358 | case 5: | |
1359 | return map->num_members - 1; | |
1360 | default: | |
1361 | dprintf("unsupported raid level\n"); | |
1362 | return 0; | |
1363 | } | |
1364 | } | |
1365 | ||
5551b113 CA |
1366 | static unsigned long long pba_of_lba0(struct imsm_map *map) |
1367 | { | |
1368 | if (map == NULL) | |
1369 | return 0; | |
1370 | return join_u32(map->pba_of_lba0_lo, map->pba_of_lba0_hi); | |
1371 | } | |
1372 | ||
1373 | static unsigned long long blocks_per_member(struct imsm_map *map) | |
1374 | { | |
1375 | if (map == NULL) | |
1376 | return 0; | |
1377 | return join_u32(map->blocks_per_member_lo, map->blocks_per_member_hi); | |
1378 | } | |
1379 | ||
1380 | static unsigned long long num_data_stripes(struct imsm_map *map) | |
1381 | { | |
1382 | if (map == NULL) | |
1383 | return 0; | |
1384 | return join_u32(map->num_data_stripes_lo, map->num_data_stripes_hi); | |
1385 | } | |
1386 | ||
4036e7ee MT |
1387 | static unsigned long long vol_curr_migr_unit(struct imsm_dev *dev) |
1388 | { | |
1389 | if (dev == NULL) | |
1390 | return 0; | |
1391 | ||
1392 | return join_u32(dev->vol.curr_migr_unit_lo, dev->vol.curr_migr_unit_hi); | |
1393 | } | |
1394 | ||
fcc2c9da MD |
1395 | static unsigned long long imsm_dev_size(struct imsm_dev *dev) |
1396 | { | |
1397 | if (dev == NULL) | |
1398 | return 0; | |
1399 | return join_u32(dev->size_low, dev->size_high); | |
1400 | } | |
1401 | ||
9f421827 PB |
1402 | static unsigned long long migr_chkp_area_pba(struct migr_record *migr_rec) |
1403 | { | |
1404 | if (migr_rec == NULL) | |
1405 | return 0; | |
1406 | return join_u32(migr_rec->ckpt_area_pba_lo, | |
1407 | migr_rec->ckpt_area_pba_hi); | |
1408 | } | |
1409 | ||
1410 | static unsigned long long current_migr_unit(struct migr_record *migr_rec) | |
1411 | { | |
1412 | if (migr_rec == NULL) | |
1413 | return 0; | |
1414 | return join_u32(migr_rec->curr_migr_unit_lo, | |
1415 | migr_rec->curr_migr_unit_hi); | |
1416 | } | |
1417 | ||
1418 | static unsigned long long migr_dest_1st_member_lba(struct migr_record *migr_rec) | |
1419 | { | |
1420 | if (migr_rec == NULL) | |
1421 | return 0; | |
1422 | return join_u32(migr_rec->dest_1st_member_lba_lo, | |
1423 | migr_rec->dest_1st_member_lba_hi); | |
1424 | } | |
1425 | ||
1426 | static unsigned long long get_num_migr_units(struct migr_record *migr_rec) | |
1427 | { | |
1428 | if (migr_rec == NULL) | |
1429 | return 0; | |
1430 | return join_u32(migr_rec->num_migr_units_lo, | |
1431 | migr_rec->num_migr_units_hi); | |
1432 | } | |
1433 | ||
5551b113 CA |
1434 | static void set_total_blocks(struct imsm_disk *disk, unsigned long long n) |
1435 | { | |
1436 | split_ull(n, &disk->total_blocks_lo, &disk->total_blocks_hi); | |
1437 | } | |
1438 | ||
1c275381 MT |
1439 | /** |
1440 | * set_num_domains() - Set number of domains for an array. | |
1441 | * @map: Map to be updated. | |
1442 | * | |
1443 | * num_domains property represents copies count of each data drive, thus make | |
1444 | * it meaningful only for RAID1 and RAID10. IMSM supports two domains for | |
1445 | * raid1 and raid10. | |
1446 | */ | |
1447 | static void set_num_domains(struct imsm_map *map) | |
1448 | { | |
1449 | int level = get_imsm_raid_level(map); | |
1450 | ||
1451 | if (level == 1 || level == 10) | |
1452 | map->num_domains = 2; | |
1453 | else | |
1454 | map->num_domains = 1; | |
1455 | } | |
1456 | ||
5551b113 CA |
1457 | static void set_pba_of_lba0(struct imsm_map *map, unsigned long long n) |
1458 | { | |
1459 | split_ull(n, &map->pba_of_lba0_lo, &map->pba_of_lba0_hi); | |
1460 | } | |
1461 | ||
1462 | static void set_blocks_per_member(struct imsm_map *map, unsigned long long n) | |
1463 | { | |
1464 | split_ull(n, &map->blocks_per_member_lo, &map->blocks_per_member_hi); | |
1465 | } | |
1466 | ||
1467 | static void set_num_data_stripes(struct imsm_map *map, unsigned long long n) | |
1468 | { | |
1469 | split_ull(n, &map->num_data_stripes_lo, &map->num_data_stripes_hi); | |
1470 | } | |
1471 | ||
1c275381 MT |
1472 | /** |
1473 | * update_num_data_stripes() - Calculate and update num_data_stripes value. | |
1474 | * @map: map to be updated. | |
1475 | * @dev_size: size of volume. | |
1476 | * | |
1477 | * num_data_stripes value is addictionally divided by num_domains, therefore for | |
1478 | * levels where num_domains is not 1, nds is a part of real value. | |
1479 | */ | |
1480 | static void update_num_data_stripes(struct imsm_map *map, | |
1481 | unsigned long long dev_size) | |
1482 | { | |
1483 | unsigned long long nds = dev_size / imsm_num_data_members(map); | |
1484 | ||
1485 | nds /= map->num_domains; | |
1486 | nds /= map->blocks_per_strip; | |
1487 | set_num_data_stripes(map, nds); | |
1488 | } | |
1489 | ||
4036e7ee MT |
1490 | static void set_vol_curr_migr_unit(struct imsm_dev *dev, unsigned long long n) |
1491 | { | |
1492 | if (dev == NULL) | |
1493 | return; | |
1494 | ||
1495 | split_ull(n, &dev->vol.curr_migr_unit_lo, &dev->vol.curr_migr_unit_hi); | |
1496 | } | |
1497 | ||
fcc2c9da MD |
1498 | static void set_imsm_dev_size(struct imsm_dev *dev, unsigned long long n) |
1499 | { | |
1500 | split_ull(n, &dev->size_low, &dev->size_high); | |
1501 | } | |
1502 | ||
9f421827 PB |
1503 | static void set_migr_chkp_area_pba(struct migr_record *migr_rec, |
1504 | unsigned long long n) | |
1505 | { | |
1506 | split_ull(n, &migr_rec->ckpt_area_pba_lo, &migr_rec->ckpt_area_pba_hi); | |
1507 | } | |
1508 | ||
1509 | static void set_current_migr_unit(struct migr_record *migr_rec, | |
1510 | unsigned long long n) | |
1511 | { | |
1512 | split_ull(n, &migr_rec->curr_migr_unit_lo, | |
1513 | &migr_rec->curr_migr_unit_hi); | |
1514 | } | |
1515 | ||
1516 | static void set_migr_dest_1st_member_lba(struct migr_record *migr_rec, | |
1517 | unsigned long long n) | |
1518 | { | |
1519 | split_ull(n, &migr_rec->dest_1st_member_lba_lo, | |
1520 | &migr_rec->dest_1st_member_lba_hi); | |
1521 | } | |
1522 | ||
1523 | static void set_num_migr_units(struct migr_record *migr_rec, | |
1524 | unsigned long long n) | |
1525 | { | |
1526 | split_ull(n, &migr_rec->num_migr_units_lo, | |
1527 | &migr_rec->num_migr_units_hi); | |
1528 | } | |
1529 | ||
44490938 MD |
1530 | static unsigned long long per_dev_array_size(struct imsm_map *map) |
1531 | { | |
1532 | unsigned long long array_size = 0; | |
1533 | ||
1534 | if (map == NULL) | |
1535 | return array_size; | |
1536 | ||
1537 | array_size = num_data_stripes(map) * map->blocks_per_strip; | |
1538 | if (get_imsm_raid_level(map) == 1 || get_imsm_raid_level(map) == 10) | |
1539 | array_size *= 2; | |
1540 | ||
1541 | return array_size; | |
1542 | } | |
1543 | ||
05501181 PB |
1544 | static struct extent *get_extents(struct intel_super *super, struct dl *dl, |
1545 | int get_minimal_reservation) | |
0dcecb2e DW |
1546 | { |
1547 | /* find a list of used extents on the given physical device */ | |
0dcecb2e | 1548 | int memberships = count_memberships(dl, super); |
aa19fdd4 MT |
1549 | struct extent *rv = xcalloc(memberships + 1, sizeof(struct extent)); |
1550 | struct extent *e = rv; | |
1551 | int i; | |
b276dd33 DW |
1552 | __u32 reservation; |
1553 | ||
1554 | /* trim the reserved area for spares, so they can join any array | |
1555 | * regardless of whether the OROM has assigned sectors from the | |
1556 | * IMSM_RESERVED_SECTORS region | |
1557 | */ | |
05501181 | 1558 | if (dl->index == -1 || get_minimal_reservation) |
b81221b7 | 1559 | reservation = imsm_min_reserved_sectors(super); |
b276dd33 DW |
1560 | else |
1561 | reservation = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS; | |
0dcecb2e | 1562 | |
949c47a0 DW |
1563 | for (i = 0; i < super->anchor->num_raid_devs; i++) { |
1564 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
238c0a71 | 1565 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
c2c087e6 | 1566 | |
620b1713 | 1567 | if (get_imsm_disk_slot(map, dl->index) >= 0) { |
5551b113 | 1568 | e->start = pba_of_lba0(map); |
44490938 | 1569 | e->size = per_dev_array_size(map); |
aa19fdd4 | 1570 | e->vol = i; |
620b1713 | 1571 | e++; |
c2c087e6 DW |
1572 | } |
1573 | } | |
1574 | qsort(rv, memberships, sizeof(*rv), cmp_extent); | |
1575 | ||
1011e834 | 1576 | /* determine the start of the metadata |
14e8215b DW |
1577 | * when no raid devices are defined use the default |
1578 | * ...otherwise allow the metadata to truncate the value | |
1579 | * as is the case with older versions of imsm | |
1580 | */ | |
1581 | if (memberships) { | |
1582 | struct extent *last = &rv[memberships - 1]; | |
5551b113 | 1583 | unsigned long long remainder; |
14e8215b | 1584 | |
5551b113 | 1585 | remainder = total_blocks(&dl->disk) - (last->start + last->size); |
dda5855f DW |
1586 | /* round down to 1k block to satisfy precision of the kernel |
1587 | * 'size' interface | |
1588 | */ | |
1589 | remainder &= ~1UL; | |
1590 | /* make sure remainder is still sane */ | |
f21e18ca | 1591 | if (remainder < (unsigned)ROUND_UP(super->len, 512) >> 9) |
dda5855f | 1592 | remainder = ROUND_UP(super->len, 512) >> 9; |
14e8215b DW |
1593 | if (reservation > remainder) |
1594 | reservation = remainder; | |
1595 | } | |
5551b113 | 1596 | e->start = total_blocks(&dl->disk) - reservation; |
c2c087e6 DW |
1597 | e->size = 0; |
1598 | return rv; | |
1599 | } | |
1600 | ||
14e8215b DW |
1601 | /* try to determine how much space is reserved for metadata from |
1602 | * the last get_extents() entry, otherwise fallback to the | |
1603 | * default | |
1604 | */ | |
1605 | static __u32 imsm_reserved_sectors(struct intel_super *super, struct dl *dl) | |
1606 | { | |
1607 | struct extent *e; | |
1608 | int i; | |
1609 | __u32 rv; | |
1610 | ||
1611 | /* for spares just return a minimal reservation which will grow | |
1612 | * once the spare is picked up by an array | |
1613 | */ | |
1614 | if (dl->index == -1) | |
1615 | return MPB_SECTOR_CNT; | |
1616 | ||
05501181 | 1617 | e = get_extents(super, dl, 0); |
14e8215b DW |
1618 | if (!e) |
1619 | return MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS; | |
1620 | ||
1621 | /* scroll to last entry */ | |
1622 | for (i = 0; e[i].size; i++) | |
1623 | continue; | |
1624 | ||
5551b113 | 1625 | rv = total_blocks(&dl->disk) - e[i].start; |
14e8215b DW |
1626 | |
1627 | free(e); | |
1628 | ||
1629 | return rv; | |
1630 | } | |
1631 | ||
25ed7e59 DW |
1632 | static int is_spare(struct imsm_disk *disk) |
1633 | { | |
1634 | return (disk->status & SPARE_DISK) == SPARE_DISK; | |
1635 | } | |
1636 | ||
1637 | static int is_configured(struct imsm_disk *disk) | |
1638 | { | |
1639 | return (disk->status & CONFIGURED_DISK) == CONFIGURED_DISK; | |
1640 | } | |
1641 | ||
1642 | static int is_failed(struct imsm_disk *disk) | |
1643 | { | |
1644 | return (disk->status & FAILED_DISK) == FAILED_DISK; | |
1645 | } | |
1646 | ||
2432ce9b AP |
1647 | static int is_journal(struct imsm_disk *disk) |
1648 | { | |
1649 | return (disk->status & JOURNAL_DISK) == JOURNAL_DISK; | |
1650 | } | |
1651 | ||
78c8028b MT |
1652 | /** |
1653 | * round_member_size_to_mb()- Round given size to closest MiB. | |
1654 | * @size: size to round in sectors. | |
b53bfba6 | 1655 | */ |
78c8028b | 1656 | static inline unsigned long long round_member_size_to_mb(unsigned long long size) |
b53bfba6 | 1657 | { |
78c8028b MT |
1658 | return (size >> SECT_PER_MB_SHIFT) << SECT_PER_MB_SHIFT; |
1659 | } | |
b53bfba6 | 1660 | |
78c8028b MT |
1661 | /** |
1662 | * round_size_to_mb()- Round given size. | |
1663 | * @array_size: size to round in sectors. | |
1664 | * @disk_count: count of data members. | |
1665 | * | |
1666 | * Get size per each data member and round it to closest MiB to ensure that data | |
1667 | * splits evenly between members. | |
1668 | * | |
1669 | * Return: Array size, rounded down. | |
1670 | */ | |
1671 | static inline unsigned long long round_size_to_mb(unsigned long long array_size, | |
1672 | unsigned int disk_count) | |
1673 | { | |
1674 | return round_member_size_to_mb(array_size / disk_count) * disk_count; | |
b53bfba6 TM |
1675 | } |
1676 | ||
8b9cd157 MK |
1677 | static int able_to_resync(int raid_level, int missing_disks) |
1678 | { | |
1679 | int max_missing_disks = 0; | |
1680 | ||
1681 | switch (raid_level) { | |
1682 | case 10: | |
1683 | max_missing_disks = 1; | |
1684 | break; | |
1685 | default: | |
1686 | max_missing_disks = 0; | |
1687 | } | |
1688 | return missing_disks <= max_missing_disks; | |
1689 | } | |
1690 | ||
b81221b7 CA |
1691 | /* try to determine how much space is reserved for metadata from |
1692 | * the last get_extents() entry on the smallest active disk, | |
1693 | * otherwise fallback to the default | |
1694 | */ | |
1695 | static __u32 imsm_min_reserved_sectors(struct intel_super *super) | |
1696 | { | |
1697 | struct extent *e; | |
1698 | int i; | |
5551b113 CA |
1699 | unsigned long long min_active; |
1700 | __u32 remainder; | |
b81221b7 CA |
1701 | __u32 rv = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS; |
1702 | struct dl *dl, *dl_min = NULL; | |
1703 | ||
1704 | if (!super) | |
1705 | return rv; | |
1706 | ||
1707 | min_active = 0; | |
1708 | for (dl = super->disks; dl; dl = dl->next) { | |
1709 | if (dl->index < 0) | |
1710 | continue; | |
5551b113 CA |
1711 | unsigned long long blocks = total_blocks(&dl->disk); |
1712 | if (blocks < min_active || min_active == 0) { | |
b81221b7 | 1713 | dl_min = dl; |
5551b113 | 1714 | min_active = blocks; |
b81221b7 CA |
1715 | } |
1716 | } | |
1717 | if (!dl_min) | |
1718 | return rv; | |
1719 | ||
1720 | /* find last lba used by subarrays on the smallest active disk */ | |
05501181 | 1721 | e = get_extents(super, dl_min, 0); |
b81221b7 CA |
1722 | if (!e) |
1723 | return rv; | |
1724 | for (i = 0; e[i].size; i++) | |
1725 | continue; | |
1726 | ||
1727 | remainder = min_active - e[i].start; | |
1728 | free(e); | |
1729 | ||
1730 | /* to give priority to recovery we should not require full | |
1731 | IMSM_RESERVED_SECTORS from the spare */ | |
1732 | rv = MPB_SECTOR_CNT + NUM_BLOCKS_DIRTY_STRIPE_REGION; | |
1733 | ||
1734 | /* if real reservation is smaller use that value */ | |
1735 | return (remainder < rv) ? remainder : rv; | |
1736 | } | |
1737 | ||
195d1d76 | 1738 | static bool is_gen_migration(struct imsm_dev *dev); |
d1e02575 | 1739 | |
f36a9ecd PB |
1740 | #define IMSM_4K_DIV 8 |
1741 | ||
c47b0ff6 AK |
1742 | static __u64 blocks_per_migr_unit(struct intel_super *super, |
1743 | struct imsm_dev *dev); | |
1e5c6983 | 1744 | |
c47b0ff6 AK |
1745 | static void print_imsm_dev(struct intel_super *super, |
1746 | struct imsm_dev *dev, | |
1747 | char *uuid, | |
1748 | int disk_idx) | |
cdddbdbc DW |
1749 | { |
1750 | __u64 sz; | |
0d80bb2f | 1751 | int slot, i; |
238c0a71 AK |
1752 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
1753 | struct imsm_map *map2 = get_imsm_map(dev, MAP_1); | |
b10b37b8 | 1754 | __u32 ord; |
cdddbdbc DW |
1755 | |
1756 | printf("\n"); | |
1e7bc0ed | 1757 | printf("[%.16s]:\n", dev->volume); |
ba1b3bc8 | 1758 | printf(" Subarray : %d\n", super->current_vol); |
44470971 | 1759 | printf(" UUID : %s\n", uuid); |
dd8bcb3b AK |
1760 | printf(" RAID Level : %d", get_imsm_raid_level(map)); |
1761 | if (map2) | |
1762 | printf(" <-- %d", get_imsm_raid_level(map2)); | |
1763 | printf("\n"); | |
1764 | printf(" Members : %d", map->num_members); | |
1765 | if (map2) | |
1766 | printf(" <-- %d", map2->num_members); | |
1767 | printf("\n"); | |
0d80bb2f DW |
1768 | printf(" Slots : ["); |
1769 | for (i = 0; i < map->num_members; i++) { | |
238c0a71 | 1770 | ord = get_imsm_ord_tbl_ent(dev, i, MAP_0); |
0d80bb2f DW |
1771 | printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U"); |
1772 | } | |
dd8bcb3b AK |
1773 | printf("]"); |
1774 | if (map2) { | |
1775 | printf(" <-- ["); | |
1776 | for (i = 0; i < map2->num_members; i++) { | |
238c0a71 | 1777 | ord = get_imsm_ord_tbl_ent(dev, i, MAP_1); |
dd8bcb3b AK |
1778 | printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U"); |
1779 | } | |
1780 | printf("]"); | |
1781 | } | |
1782 | printf("\n"); | |
7095bccb AK |
1783 | printf(" Failed disk : "); |
1784 | if (map->failed_disk_num == 0xff) | |
b823c8f9 | 1785 | printf(STR_COMMON_NONE); |
7095bccb AK |
1786 | else |
1787 | printf("%i", map->failed_disk_num); | |
1788 | printf("\n"); | |
620b1713 DW |
1789 | slot = get_imsm_disk_slot(map, disk_idx); |
1790 | if (slot >= 0) { | |
238c0a71 | 1791 | ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X); |
b10b37b8 DW |
1792 | printf(" This Slot : %d%s\n", slot, |
1793 | ord & IMSM_ORD_REBUILD ? " (out-of-sync)" : ""); | |
1794 | } else | |
cdddbdbc | 1795 | printf(" This Slot : ?\n"); |
84918897 | 1796 | printf(" Sector Size : %u\n", super->sector_size); |
fcc2c9da | 1797 | sz = imsm_dev_size(dev); |
84918897 MK |
1798 | printf(" Array Size : %llu%s\n", |
1799 | (unsigned long long)sz * 512 / super->sector_size, | |
cdddbdbc | 1800 | human_size(sz * 512)); |
5551b113 | 1801 | sz = blocks_per_member(map); |
84918897 MK |
1802 | printf(" Per Dev Size : %llu%s\n", |
1803 | (unsigned long long)sz * 512 / super->sector_size, | |
cdddbdbc | 1804 | human_size(sz * 512)); |
5551b113 | 1805 | printf(" Sector Offset : %llu\n", |
7d8935cb | 1806 | pba_of_lba0(map) * 512 / super->sector_size); |
5551b113 CA |
1807 | printf(" Num Stripes : %llu\n", |
1808 | num_data_stripes(map)); | |
dd8bcb3b | 1809 | printf(" Chunk Size : %u KiB", |
cdddbdbc | 1810 | __le16_to_cpu(map->blocks_per_strip) / 2); |
dd8bcb3b AK |
1811 | if (map2) |
1812 | printf(" <-- %u KiB", | |
1813 | __le16_to_cpu(map2->blocks_per_strip) / 2); | |
1814 | printf("\n"); | |
cdddbdbc | 1815 | printf(" Reserved : %d\n", __le32_to_cpu(dev->reserved_blocks)); |
8655a7b1 | 1816 | printf(" Migrate State : "); |
1484e727 DW |
1817 | if (dev->vol.migr_state) { |
1818 | if (migr_type(dev) == MIGR_INIT) | |
8655a7b1 | 1819 | printf("initialize\n"); |
1484e727 | 1820 | else if (migr_type(dev) == MIGR_REBUILD) |
8655a7b1 | 1821 | printf("rebuild\n"); |
1484e727 | 1822 | else if (migr_type(dev) == MIGR_VERIFY) |
8655a7b1 | 1823 | printf("check\n"); |
1484e727 | 1824 | else if (migr_type(dev) == MIGR_GEN_MIGR) |
8655a7b1 | 1825 | printf("general migration\n"); |
1484e727 | 1826 | else if (migr_type(dev) == MIGR_STATE_CHANGE) |
8655a7b1 | 1827 | printf("state change\n"); |
1484e727 | 1828 | else if (migr_type(dev) == MIGR_REPAIR) |
8655a7b1 | 1829 | printf("repair\n"); |
1484e727 | 1830 | else |
8655a7b1 DW |
1831 | printf("<unknown:%d>\n", migr_type(dev)); |
1832 | } else | |
1833 | printf("idle\n"); | |
3393c6af DW |
1834 | printf(" Map State : %s", map_state_str[map->map_state]); |
1835 | if (dev->vol.migr_state) { | |
238c0a71 | 1836 | struct imsm_map *map = get_imsm_map(dev, MAP_1); |
1e5c6983 | 1837 | |
b10b37b8 | 1838 | printf(" <-- %s", map_state_str[map->map_state]); |
4036e7ee | 1839 | printf("\n Checkpoint : %llu ", vol_curr_migr_unit(dev)); |
089f9d79 | 1840 | if (is_gen_migration(dev) && (slot > 1 || slot < 0)) |
464d40e8 LD |
1841 | printf("(N/A)"); |
1842 | else | |
1843 | printf("(%llu)", (unsigned long long) | |
1844 | blocks_per_migr_unit(super, dev)); | |
3393c6af DW |
1845 | } |
1846 | printf("\n"); | |
2432ce9b AP |
1847 | printf(" Dirty State : %s\n", (dev->vol.dirty & RAIDVOL_DIRTY) ? |
1848 | "dirty" : "clean"); | |
1849 | printf(" RWH Policy : "); | |
c2462068 | 1850 | if (dev->rwh_policy == RWH_OFF || dev->rwh_policy == RWH_MULTIPLE_OFF) |
2432ce9b AP |
1851 | printf("off\n"); |
1852 | else if (dev->rwh_policy == RWH_DISTRIBUTED) | |
1853 | printf("PPL distributed\n"); | |
1854 | else if (dev->rwh_policy == RWH_JOURNALING_DRIVE) | |
1855 | printf("PPL journaling drive\n"); | |
c2462068 PB |
1856 | else if (dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED) |
1857 | printf("Multiple distributed PPLs\n"); | |
1858 | else if (dev->rwh_policy == RWH_MULTIPLE_PPLS_JOURNALING_DRIVE) | |
1859 | printf("Multiple PPLs on journaling drive\n"); | |
fbc42556 JR |
1860 | else if (dev->rwh_policy == RWH_BITMAP) |
1861 | printf("Write-intent bitmap\n"); | |
2432ce9b AP |
1862 | else |
1863 | printf("<unknown:%d>\n", dev->rwh_policy); | |
ba1b3bc8 AP |
1864 | |
1865 | printf(" Volume ID : %u\n", dev->my_vol_raid_dev_num); | |
cdddbdbc DW |
1866 | } |
1867 | ||
ef5c214e MK |
1868 | static void print_imsm_disk(struct imsm_disk *disk, |
1869 | int index, | |
1870 | __u32 reserved, | |
1871 | unsigned int sector_size) { | |
1f24f035 | 1872 | char str[MAX_RAID_SERIAL_LEN + 1]; |
cdddbdbc DW |
1873 | __u64 sz; |
1874 | ||
0ec1f4e8 | 1875 | if (index < -1 || !disk) |
e9d82038 DW |
1876 | return; |
1877 | ||
cdddbdbc | 1878 | printf("\n"); |
1f24f035 | 1879 | snprintf(str, MAX_RAID_SERIAL_LEN + 1, "%s", disk->serial); |
0ec1f4e8 DW |
1880 | if (index >= 0) |
1881 | printf(" Disk%02d Serial : %s\n", index, str); | |
1882 | else | |
1883 | printf(" Disk Serial : %s\n", str); | |
2432ce9b AP |
1884 | printf(" State :%s%s%s%s\n", is_spare(disk) ? " spare" : "", |
1885 | is_configured(disk) ? " active" : "", | |
1886 | is_failed(disk) ? " failed" : "", | |
1887 | is_journal(disk) ? " journal" : ""); | |
cdddbdbc | 1888 | printf(" Id : %08x\n", __le32_to_cpu(disk->scsi_id)); |
5551b113 | 1889 | sz = total_blocks(disk) - reserved; |
ef5c214e MK |
1890 | printf(" Usable Size : %llu%s\n", |
1891 | (unsigned long long)sz * 512 / sector_size, | |
cdddbdbc DW |
1892 | human_size(sz * 512)); |
1893 | } | |
1894 | ||
de44e46f PB |
1895 | void convert_to_4k_imsm_migr_rec(struct intel_super *super) |
1896 | { | |
1897 | struct migr_record *migr_rec = super->migr_rec; | |
1898 | ||
1899 | migr_rec->blocks_per_unit /= IMSM_4K_DIV; | |
de44e46f PB |
1900 | migr_rec->dest_depth_per_unit /= IMSM_4K_DIV; |
1901 | split_ull((join_u32(migr_rec->post_migr_vol_cap, | |
1902 | migr_rec->post_migr_vol_cap_hi) / IMSM_4K_DIV), | |
1903 | &migr_rec->post_migr_vol_cap, &migr_rec->post_migr_vol_cap_hi); | |
9f421827 PB |
1904 | set_migr_chkp_area_pba(migr_rec, |
1905 | migr_chkp_area_pba(migr_rec) / IMSM_4K_DIV); | |
1906 | set_migr_dest_1st_member_lba(migr_rec, | |
1907 | migr_dest_1st_member_lba(migr_rec) / IMSM_4K_DIV); | |
de44e46f PB |
1908 | } |
1909 | ||
f36a9ecd PB |
1910 | void convert_to_4k_imsm_disk(struct imsm_disk *disk) |
1911 | { | |
1912 | set_total_blocks(disk, (total_blocks(disk)/IMSM_4K_DIV)); | |
1913 | } | |
1914 | ||
1915 | void convert_to_4k(struct intel_super *super) | |
1916 | { | |
1917 | struct imsm_super *mpb = super->anchor; | |
1918 | struct imsm_disk *disk; | |
1919 | int i; | |
e4467bc7 | 1920 | __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size); |
f36a9ecd PB |
1921 | |
1922 | for (i = 0; i < mpb->num_disks ; i++) { | |
1923 | disk = __get_imsm_disk(mpb, i); | |
1924 | /* disk */ | |
1925 | convert_to_4k_imsm_disk(disk); | |
1926 | } | |
1927 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
1928 | struct imsm_dev *dev = __get_imsm_dev(mpb, i); | |
1929 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
1930 | /* dev */ | |
fcc2c9da | 1931 | set_imsm_dev_size(dev, imsm_dev_size(dev)/IMSM_4K_DIV); |
4036e7ee MT |
1932 | set_vol_curr_migr_unit(dev, |
1933 | vol_curr_migr_unit(dev) / IMSM_4K_DIV); | |
f36a9ecd PB |
1934 | |
1935 | /* map0 */ | |
1936 | set_blocks_per_member(map, blocks_per_member(map)/IMSM_4K_DIV); | |
1937 | map->blocks_per_strip /= IMSM_4K_DIV; | |
1938 | set_pba_of_lba0(map, pba_of_lba0(map)/IMSM_4K_DIV); | |
1939 | ||
1940 | if (dev->vol.migr_state) { | |
1941 | /* map1 */ | |
1942 | map = get_imsm_map(dev, MAP_1); | |
1943 | set_blocks_per_member(map, | |
1944 | blocks_per_member(map)/IMSM_4K_DIV); | |
1945 | map->blocks_per_strip /= IMSM_4K_DIV; | |
1946 | set_pba_of_lba0(map, pba_of_lba0(map)/IMSM_4K_DIV); | |
1947 | } | |
1948 | } | |
e4467bc7 TM |
1949 | if (bbm_log_size) { |
1950 | struct bbm_log *log = (void *)mpb + | |
1951 | __le32_to_cpu(mpb->mpb_size) - bbm_log_size; | |
1952 | __u32 i; | |
1953 | ||
1954 | for (i = 0; i < log->entry_count; i++) { | |
1955 | struct bbm_log_entry *entry = | |
1956 | &log->marked_block_entries[i]; | |
1957 | ||
1958 | __u8 count = entry->marked_count + 1; | |
1959 | unsigned long long sector = | |
1960 | __le48_to_cpu(&entry->defective_block_start); | |
1961 | ||
1962 | entry->defective_block_start = | |
1963 | __cpu_to_le48(sector/IMSM_4K_DIV); | |
1964 | entry->marked_count = max(count/IMSM_4K_DIV, 1) - 1; | |
1965 | } | |
1966 | } | |
f36a9ecd PB |
1967 | |
1968 | mpb->check_sum = __gen_imsm_checksum(mpb); | |
1969 | } | |
1970 | ||
520e69e2 AK |
1971 | void examine_migr_rec_imsm(struct intel_super *super) |
1972 | { | |
1973 | struct migr_record *migr_rec = super->migr_rec; | |
1974 | struct imsm_super *mpb = super->anchor; | |
1975 | int i; | |
1976 | ||
1977 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
1978 | struct imsm_dev *dev = __get_imsm_dev(mpb, i); | |
3136abe5 | 1979 | struct imsm_map *map; |
b4ab44d8 | 1980 | int slot = -1; |
3136abe5 | 1981 | |
195d1d76 | 1982 | if (is_gen_migration(dev) == false) |
520e69e2 AK |
1983 | continue; |
1984 | ||
1985 | printf("\nMigration Record Information:"); | |
3136abe5 | 1986 | |
44bfe6df AK |
1987 | /* first map under migration */ |
1988 | map = get_imsm_map(dev, MAP_0); | |
76c152ca | 1989 | |
3136abe5 AK |
1990 | if (map) |
1991 | slot = get_imsm_disk_slot(map, super->disks->index); | |
089f9d79 | 1992 | if (map == NULL || slot > 1 || slot < 0) { |
520e69e2 AK |
1993 | printf(" Empty\n "); |
1994 | printf("Examine one of first two disks in array\n"); | |
1995 | break; | |
1996 | } | |
1997 | printf("\n Status : "); | |
1998 | if (__le32_to_cpu(migr_rec->rec_status) == UNIT_SRC_NORMAL) | |
1999 | printf("Normal\n"); | |
2000 | else | |
2001 | printf("Contains Data\n"); | |
9f421827 PB |
2002 | printf(" Current Unit : %llu\n", |
2003 | current_migr_unit(migr_rec)); | |
520e69e2 AK |
2004 | printf(" Family : %u\n", |
2005 | __le32_to_cpu(migr_rec->family_num)); | |
2006 | printf(" Ascending : %u\n", | |
2007 | __le32_to_cpu(migr_rec->ascending_migr)); | |
2008 | printf(" Blocks Per Unit : %u\n", | |
2009 | __le32_to_cpu(migr_rec->blocks_per_unit)); | |
2010 | printf(" Dest. Depth Per Unit : %u\n", | |
2011 | __le32_to_cpu(migr_rec->dest_depth_per_unit)); | |
9f421827 PB |
2012 | printf(" Checkpoint Area pba : %llu\n", |
2013 | migr_chkp_area_pba(migr_rec)); | |
2014 | printf(" First member lba : %llu\n", | |
2015 | migr_dest_1st_member_lba(migr_rec)); | |
2016 | printf(" Total Number of Units : %llu\n", | |
2017 | get_num_migr_units(migr_rec)); | |
2018 | printf(" Size of volume : %llu\n", | |
2019 | join_u32(migr_rec->post_migr_vol_cap, | |
2020 | migr_rec->post_migr_vol_cap_hi)); | |
520e69e2 AK |
2021 | printf(" Record was read from : %u\n", |
2022 | __le32_to_cpu(migr_rec->ckpt_read_disk_num)); | |
2023 | ||
2024 | break; | |
2025 | } | |
2026 | } | |
f36a9ecd | 2027 | |
de44e46f PB |
2028 | void convert_from_4k_imsm_migr_rec(struct intel_super *super) |
2029 | { | |
2030 | struct migr_record *migr_rec = super->migr_rec; | |
2031 | ||
2032 | migr_rec->blocks_per_unit *= IMSM_4K_DIV; | |
de44e46f PB |
2033 | migr_rec->dest_depth_per_unit *= IMSM_4K_DIV; |
2034 | split_ull((join_u32(migr_rec->post_migr_vol_cap, | |
2035 | migr_rec->post_migr_vol_cap_hi) * IMSM_4K_DIV), | |
2036 | &migr_rec->post_migr_vol_cap, | |
2037 | &migr_rec->post_migr_vol_cap_hi); | |
9f421827 PB |
2038 | set_migr_chkp_area_pba(migr_rec, |
2039 | migr_chkp_area_pba(migr_rec) * IMSM_4K_DIV); | |
2040 | set_migr_dest_1st_member_lba(migr_rec, | |
2041 | migr_dest_1st_member_lba(migr_rec) * IMSM_4K_DIV); | |
de44e46f PB |
2042 | } |
2043 | ||
f36a9ecd PB |
2044 | void convert_from_4k(struct intel_super *super) |
2045 | { | |
2046 | struct imsm_super *mpb = super->anchor; | |
2047 | struct imsm_disk *disk; | |
2048 | int i; | |
e4467bc7 | 2049 | __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size); |
f36a9ecd PB |
2050 | |
2051 | for (i = 0; i < mpb->num_disks ; i++) { | |
2052 | disk = __get_imsm_disk(mpb, i); | |
2053 | /* disk */ | |
2054 | set_total_blocks(disk, (total_blocks(disk)*IMSM_4K_DIV)); | |
2055 | } | |
2056 | ||
2057 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
2058 | struct imsm_dev *dev = __get_imsm_dev(mpb, i); | |
2059 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
2060 | /* dev */ | |
fcc2c9da | 2061 | set_imsm_dev_size(dev, imsm_dev_size(dev)*IMSM_4K_DIV); |
4036e7ee MT |
2062 | set_vol_curr_migr_unit(dev, |
2063 | vol_curr_migr_unit(dev) * IMSM_4K_DIV); | |
f36a9ecd PB |
2064 | |
2065 | /* map0 */ | |
2066 | set_blocks_per_member(map, blocks_per_member(map)*IMSM_4K_DIV); | |
2067 | map->blocks_per_strip *= IMSM_4K_DIV; | |
2068 | set_pba_of_lba0(map, pba_of_lba0(map)*IMSM_4K_DIV); | |
2069 | ||
2070 | if (dev->vol.migr_state) { | |
2071 | /* map1 */ | |
2072 | map = get_imsm_map(dev, MAP_1); | |
2073 | set_blocks_per_member(map, | |
2074 | blocks_per_member(map)*IMSM_4K_DIV); | |
2075 | map->blocks_per_strip *= IMSM_4K_DIV; | |
2076 | set_pba_of_lba0(map, pba_of_lba0(map)*IMSM_4K_DIV); | |
2077 | } | |
2078 | } | |
e4467bc7 TM |
2079 | if (bbm_log_size) { |
2080 | struct bbm_log *log = (void *)mpb + | |
2081 | __le32_to_cpu(mpb->mpb_size) - bbm_log_size; | |
2082 | __u32 i; | |
2083 | ||
2084 | for (i = 0; i < log->entry_count; i++) { | |
2085 | struct bbm_log_entry *entry = | |
2086 | &log->marked_block_entries[i]; | |
2087 | ||
2088 | __u8 count = entry->marked_count + 1; | |
2089 | unsigned long long sector = | |
2090 | __le48_to_cpu(&entry->defective_block_start); | |
2091 | ||
2092 | entry->defective_block_start = | |
2093 | __cpu_to_le48(sector*IMSM_4K_DIV); | |
2094 | entry->marked_count = count*IMSM_4K_DIV - 1; | |
2095 | } | |
2096 | } | |
f36a9ecd PB |
2097 | |
2098 | mpb->check_sum = __gen_imsm_checksum(mpb); | |
2099 | } | |
2100 | ||
19482bcc AK |
2101 | /******************************************************************************* |
2102 | * function: imsm_check_attributes | |
2103 | * Description: Function checks if features represented by attributes flags | |
1011e834 | 2104 | * are supported by mdadm. |
19482bcc AK |
2105 | * Parameters: |
2106 | * attributes - Attributes read from metadata | |
2107 | * Returns: | |
1011e834 N |
2108 | * 0 - passed attributes contains unsupported features flags |
2109 | * 1 - all features are supported | |
19482bcc AK |
2110 | ******************************************************************************/ |
2111 | static int imsm_check_attributes(__u32 attributes) | |
2112 | { | |
2113 | int ret_val = 1; | |
418f9b36 N |
2114 | __u32 not_supported = MPB_ATTRIB_SUPPORTED^0xffffffff; |
2115 | ||
2116 | not_supported &= ~MPB_ATTRIB_IGNORED; | |
19482bcc AK |
2117 | |
2118 | not_supported &= attributes; | |
2119 | if (not_supported) { | |
e7b84f9d | 2120 | pr_err("(IMSM): Unsupported attributes : %x\n", |
418f9b36 | 2121 | (unsigned)__le32_to_cpu(not_supported)); |
19482bcc AK |
2122 | if (not_supported & MPB_ATTRIB_CHECKSUM_VERIFY) { |
2123 | dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n"); | |
2124 | not_supported ^= MPB_ATTRIB_CHECKSUM_VERIFY; | |
2125 | } | |
2126 | if (not_supported & MPB_ATTRIB_2TB) { | |
2127 | dprintf("\t\tMPB_ATTRIB_2TB\n"); | |
2128 | not_supported ^= MPB_ATTRIB_2TB; | |
2129 | } | |
2130 | if (not_supported & MPB_ATTRIB_RAID0) { | |
2131 | dprintf("\t\tMPB_ATTRIB_RAID0\n"); | |
2132 | not_supported ^= MPB_ATTRIB_RAID0; | |
2133 | } | |
2134 | if (not_supported & MPB_ATTRIB_RAID1) { | |
2135 | dprintf("\t\tMPB_ATTRIB_RAID1\n"); | |
2136 | not_supported ^= MPB_ATTRIB_RAID1; | |
2137 | } | |
2138 | if (not_supported & MPB_ATTRIB_RAID10) { | |
2139 | dprintf("\t\tMPB_ATTRIB_RAID10\n"); | |
2140 | not_supported ^= MPB_ATTRIB_RAID10; | |
2141 | } | |
2142 | if (not_supported & MPB_ATTRIB_RAID1E) { | |
2143 | dprintf("\t\tMPB_ATTRIB_RAID1E\n"); | |
2144 | not_supported ^= MPB_ATTRIB_RAID1E; | |
2145 | } | |
2146 | if (not_supported & MPB_ATTRIB_RAID5) { | |
2147 | dprintf("\t\tMPB_ATTRIB_RAID5\n"); | |
2148 | not_supported ^= MPB_ATTRIB_RAID5; | |
2149 | } | |
2150 | if (not_supported & MPB_ATTRIB_RAIDCNG) { | |
2151 | dprintf("\t\tMPB_ATTRIB_RAIDCNG\n"); | |
2152 | not_supported ^= MPB_ATTRIB_RAIDCNG; | |
2153 | } | |
2154 | if (not_supported & MPB_ATTRIB_BBM) { | |
2155 | dprintf("\t\tMPB_ATTRIB_BBM\n"); | |
2156 | not_supported ^= MPB_ATTRIB_BBM; | |
2157 | } | |
2158 | if (not_supported & MPB_ATTRIB_CHECKSUM_VERIFY) { | |
2159 | dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n"); | |
2160 | not_supported ^= MPB_ATTRIB_CHECKSUM_VERIFY; | |
2161 | } | |
2162 | if (not_supported & MPB_ATTRIB_EXP_STRIPE_SIZE) { | |
2163 | dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n"); | |
2164 | not_supported ^= MPB_ATTRIB_EXP_STRIPE_SIZE; | |
2165 | } | |
2166 | if (not_supported & MPB_ATTRIB_2TB_DISK) { | |
2167 | dprintf("\t\tMPB_ATTRIB_2TB_DISK\n"); | |
2168 | not_supported ^= MPB_ATTRIB_2TB_DISK; | |
2169 | } | |
2170 | if (not_supported & MPB_ATTRIB_NEVER_USE2) { | |
2171 | dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n"); | |
2172 | not_supported ^= MPB_ATTRIB_NEVER_USE2; | |
2173 | } | |
2174 | if (not_supported & MPB_ATTRIB_NEVER_USE) { | |
2175 | dprintf("\t\tMPB_ATTRIB_NEVER_USE\n"); | |
2176 | not_supported ^= MPB_ATTRIB_NEVER_USE; | |
2177 | } | |
2178 | ||
2179 | if (not_supported) | |
1ade5cc1 | 2180 | dprintf("(IMSM): Unknown attributes : %x\n", not_supported); |
19482bcc AK |
2181 | |
2182 | ret_val = 0; | |
2183 | } | |
2184 | ||
2185 | return ret_val; | |
2186 | } | |
2187 | ||
a5d85af7 | 2188 | static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map); |
44470971 | 2189 | |
cdddbdbc DW |
2190 | static void examine_super_imsm(struct supertype *st, char *homehost) |
2191 | { | |
2192 | struct intel_super *super = st->sb; | |
949c47a0 | 2193 | struct imsm_super *mpb = super->anchor; |
cdddbdbc DW |
2194 | char str[MAX_SIGNATURE_LENGTH]; |
2195 | int i; | |
27fd6274 DW |
2196 | struct mdinfo info; |
2197 | char nbuf[64]; | |
cdddbdbc | 2198 | __u32 sum; |
14e8215b | 2199 | __u32 reserved = imsm_reserved_sectors(super, super->disks); |
94827db3 | 2200 | struct dl *dl; |
e48aed3c | 2201 | time_t creation_time; |
27fd6274 | 2202 | |
618f4e6d XN |
2203 | strncpy(str, (char *)mpb->sig, MPB_SIG_LEN); |
2204 | str[MPB_SIG_LEN-1] = '\0'; | |
cdddbdbc | 2205 | printf(" Magic : %s\n", str); |
cdddbdbc | 2206 | printf(" Version : %s\n", get_imsm_version(mpb)); |
148acb7b | 2207 | printf(" Orig Family : %08x\n", __le32_to_cpu(mpb->orig_family_num)); |
cdddbdbc DW |
2208 | printf(" Family : %08x\n", __le32_to_cpu(mpb->family_num)); |
2209 | printf(" Generation : %08x\n", __le32_to_cpu(mpb->generation_num)); | |
e48aed3c AP |
2210 | creation_time = __le64_to_cpu(mpb->creation_time); |
2211 | printf(" Creation Time : %.24s\n", | |
2212 | creation_time ? ctime(&creation_time) : "Unknown"); | |
19482bcc AK |
2213 | printf(" Attributes : "); |
2214 | if (imsm_check_attributes(mpb->attributes)) | |
2215 | printf("All supported\n"); | |
2216 | else | |
2217 | printf("not supported\n"); | |
a5d85af7 | 2218 | getinfo_super_imsm(st, &info, NULL); |
ba65d917 | 2219 | fname_from_uuid(&info, nbuf); |
27fd6274 | 2220 | printf(" UUID : %s\n", nbuf + 5); |
cdddbdbc DW |
2221 | sum = __le32_to_cpu(mpb->check_sum); |
2222 | printf(" Checksum : %08x %s\n", sum, | |
949c47a0 | 2223 | __gen_imsm_checksum(mpb) == sum ? "correct" : "incorrect"); |
f36a9ecd | 2224 | printf(" MPB Sectors : %d\n", mpb_sectors(mpb, super->sector_size)); |
cdddbdbc DW |
2225 | printf(" Disks : %d\n", mpb->num_disks); |
2226 | printf(" RAID Devices : %d\n", mpb->num_raid_devs); | |
ef5c214e MK |
2227 | print_imsm_disk(__get_imsm_disk(mpb, super->disks->index), |
2228 | super->disks->index, reserved, super->sector_size); | |
8d67477f | 2229 | if (get_imsm_bbm_log_size(super->bbm_log)) { |
604b746f JD |
2230 | struct bbm_log *log = super->bbm_log; |
2231 | ||
2232 | printf("\n"); | |
2233 | printf("Bad Block Management Log:\n"); | |
2234 | printf(" Log Size : %d\n", __le32_to_cpu(mpb->bbm_log_size)); | |
2235 | printf(" Signature : %x\n", __le32_to_cpu(log->signature)); | |
2236 | printf(" Entry Count : %d\n", __le32_to_cpu(log->entry_count)); | |
604b746f | 2237 | } |
44470971 DW |
2238 | for (i = 0; i < mpb->num_raid_devs; i++) { |
2239 | struct mdinfo info; | |
2240 | struct imsm_dev *dev = __get_imsm_dev(mpb, i); | |
2241 | ||
2242 | super->current_vol = i; | |
a5d85af7 | 2243 | getinfo_super_imsm(st, &info, NULL); |
ba65d917 | 2244 | fname_from_uuid(&info, nbuf); |
c47b0ff6 | 2245 | print_imsm_dev(super, dev, nbuf + 5, super->disks->index); |
44470971 | 2246 | } |
cdddbdbc DW |
2247 | for (i = 0; i < mpb->num_disks; i++) { |
2248 | if (i == super->disks->index) | |
2249 | continue; | |
ef5c214e MK |
2250 | print_imsm_disk(__get_imsm_disk(mpb, i), i, reserved, |
2251 | super->sector_size); | |
cdddbdbc | 2252 | } |
94827db3 | 2253 | |
0ec1f4e8 DW |
2254 | for (dl = super->disks; dl; dl = dl->next) |
2255 | if (dl->index == -1) | |
ef5c214e MK |
2256 | print_imsm_disk(&dl->disk, -1, reserved, |
2257 | super->sector_size); | |
520e69e2 AK |
2258 | |
2259 | examine_migr_rec_imsm(super); | |
cdddbdbc DW |
2260 | } |
2261 | ||
061f2c6a | 2262 | static void brief_examine_super_imsm(struct supertype *st, int verbose) |
cdddbdbc | 2263 | { |
27fd6274 | 2264 | /* We just write a generic IMSM ARRAY entry */ |
ff54de6e N |
2265 | struct mdinfo info; |
2266 | char nbuf[64]; | |
2267 | ||
a5d85af7 | 2268 | getinfo_super_imsm(st, &info, NULL); |
ba65d917 | 2269 | fname_from_uuid(&info, nbuf); |
4737ae25 N |
2270 | printf("ARRAY metadata=imsm UUID=%s\n", nbuf + 5); |
2271 | } | |
2272 | ||
2273 | static void brief_examine_subarrays_imsm(struct supertype *st, int verbose) | |
2274 | { | |
2275 | /* We just write a generic IMSM ARRAY entry */ | |
2276 | struct mdinfo info; | |
2277 | char nbuf[64]; | |
2278 | char nbuf1[64]; | |
2279 | struct intel_super *super = st->sb; | |
2280 | int i; | |
2281 | ||
2282 | if (!super->anchor->num_raid_devs) | |
2283 | return; | |
2284 | ||
a5d85af7 | 2285 | getinfo_super_imsm(st, &info, NULL); |
ba65d917 | 2286 | fname_from_uuid(&info, nbuf); |
1e7bc0ed DW |
2287 | for (i = 0; i < super->anchor->num_raid_devs; i++) { |
2288 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
2289 | ||
2290 | super->current_vol = i; | |
a5d85af7 | 2291 | getinfo_super_imsm(st, &info, NULL); |
ba65d917 | 2292 | fname_from_uuid(&info, nbuf1); |
b9ce7ab0 | 2293 | printf("ARRAY " DEV_MD_DIR "%.16s container=%s member=%d UUID=%s\n", |
cf8de691 | 2294 | dev->volume, nbuf + 5, i, nbuf1 + 5); |
1e7bc0ed | 2295 | } |
cdddbdbc DW |
2296 | } |
2297 | ||
9d84c8ea DW |
2298 | static void export_examine_super_imsm(struct supertype *st) |
2299 | { | |
2300 | struct intel_super *super = st->sb; | |
2301 | struct imsm_super *mpb = super->anchor; | |
2302 | struct mdinfo info; | |
2303 | char nbuf[64]; | |
2304 | ||
a5d85af7 | 2305 | getinfo_super_imsm(st, &info, NULL); |
ba65d917 | 2306 | fname_from_uuid(&info, nbuf); |
9d84c8ea DW |
2307 | printf("MD_METADATA=imsm\n"); |
2308 | printf("MD_LEVEL=container\n"); | |
2309 | printf("MD_UUID=%s\n", nbuf+5); | |
2310 | printf("MD_DEVICES=%u\n", mpb->num_disks); | |
e48aed3c | 2311 | printf("MD_CREATION_TIME=%llu\n", __le64_to_cpu(mpb->creation_time)); |
9d84c8ea DW |
2312 | } |
2313 | ||
b771faef BK |
2314 | static void detail_super_imsm(struct supertype *st, char *homehost, |
2315 | char *subarray) | |
cdddbdbc | 2316 | { |
3ebe00a1 DW |
2317 | struct mdinfo info; |
2318 | char nbuf[64]; | |
b771faef BK |
2319 | struct intel_super *super = st->sb; |
2320 | int temp_vol = super->current_vol; | |
2321 | ||
2322 | if (subarray) | |
2323 | super->current_vol = strtoul(subarray, NULL, 10); | |
3ebe00a1 | 2324 | |
a5d85af7 | 2325 | getinfo_super_imsm(st, &info, NULL); |
ba65d917 | 2326 | fname_from_uuid(&info, nbuf); |
65884368 | 2327 | printf("\n UUID : %s\n", nbuf + 5); |
b771faef BK |
2328 | |
2329 | super->current_vol = temp_vol; | |
cdddbdbc DW |
2330 | } |
2331 | ||
b771faef | 2332 | static void brief_detail_super_imsm(struct supertype *st, char *subarray) |
cdddbdbc | 2333 | { |
ff54de6e N |
2334 | struct mdinfo info; |
2335 | char nbuf[64]; | |
b771faef BK |
2336 | struct intel_super *super = st->sb; |
2337 | int temp_vol = super->current_vol; | |
2338 | ||
2339 | if (subarray) | |
2340 | super->current_vol = strtoul(subarray, NULL, 10); | |
2341 | ||
a5d85af7 | 2342 | getinfo_super_imsm(st, &info, NULL); |
ba65d917 | 2343 | fname_from_uuid(&info, nbuf); |
ff54de6e | 2344 | printf(" UUID=%s", nbuf + 5); |
b771faef BK |
2345 | |
2346 | super->current_vol = temp_vol; | |
cdddbdbc | 2347 | } |
d665cc31 | 2348 | |
6da53c0e BK |
2349 | static int imsm_read_serial(int fd, char *devname, __u8 *serial, |
2350 | size_t serial_buf_len); | |
d665cc31 DW |
2351 | static void fd2devname(int fd, char *name); |
2352 | ||
bf62ed5d BK |
2353 | void print_encryption_information(int disk_fd, enum sys_dev_type hba_type) |
2354 | { | |
2355 | struct encryption_information information = {0}; | |
2356 | mdadm_status_t status = MDADM_STATUS_SUCCESS; | |
2357 | const char *indent = " "; | |
2358 | ||
2359 | switch (hba_type) { | |
2360 | case SYS_DEV_VMD: | |
2361 | case SYS_DEV_NVME: | |
2362 | status = get_nvme_opal_encryption_information(disk_fd, &information, 1); | |
2363 | break; | |
2364 | case SYS_DEV_SATA: | |
2365 | case SYS_DEV_SATA_VMD: | |
2366 | status = get_ata_encryption_information(disk_fd, &information, 1); | |
2367 | break; | |
2368 | default: | |
2369 | return; | |
2370 | } | |
2371 | ||
2372 | if (status) { | |
2373 | pr_err("Failed to get drive encryption information.\n"); | |
2374 | return; | |
2375 | } | |
2376 | ||
2377 | printf("%sEncryption(Ability|Status): %s|%s\n", indent, | |
2378 | get_encryption_ability_string(information.ability), | |
2379 | get_encryption_status_string(information.status)); | |
2380 | } | |
2381 | ||
2382 | static int ahci_enumerate_ports(struct sys_dev *hba, int port_count, int host_base, int verbose) | |
d665cc31 | 2383 | { |
120dc887 LM |
2384 | /* dump an unsorted list of devices attached to AHCI Intel storage |
2385 | * controller, as well as non-connected ports | |
d665cc31 | 2386 | */ |
bf62ed5d | 2387 | int hba_len = strlen(hba->path) + 1; |
d665cc31 DW |
2388 | struct dirent *ent; |
2389 | DIR *dir; | |
2390 | char *path = NULL; | |
2391 | int err = 0; | |
2392 | unsigned long port_mask = (1 << port_count) - 1; | |
2393 | ||
f21e18ca | 2394 | if (port_count > (int)sizeof(port_mask) * 8) { |
ba728be7 | 2395 | if (verbose > 0) |
e7b84f9d | 2396 | pr_err("port_count %d out of range\n", port_count); |
d665cc31 DW |
2397 | return 2; |
2398 | } | |
2399 | ||
2400 | /* scroll through /sys/dev/block looking for devices attached to | |
2401 | * this hba | |
2402 | */ | |
2403 | dir = opendir("/sys/dev/block"); | |
1a6dd6b9 PB |
2404 | if (!dir) |
2405 | return 1; | |
2406 | ||
2407 | for (ent = readdir(dir); ent; ent = readdir(dir)) { | |
d665cc31 DW |
2408 | int fd; |
2409 | char model[64]; | |
2410 | char vendor[64]; | |
2411 | char buf[1024]; | |
2412 | int major, minor; | |
fcebeb77 | 2413 | char device[PATH_MAX]; |
d665cc31 DW |
2414 | char *c; |
2415 | int port; | |
2416 | int type; | |
2417 | ||
2418 | if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2) | |
2419 | continue; | |
7c798f87 | 2420 | path = devt_to_devpath(makedev(major, minor), 1, NULL); |
d665cc31 DW |
2421 | if (!path) |
2422 | continue; | |
bf62ed5d | 2423 | if (!path_attached_to_hba(path, hba->path)) { |
d665cc31 DW |
2424 | free(path); |
2425 | path = NULL; | |
2426 | continue; | |
2427 | } | |
2428 | ||
fcebeb77 MT |
2429 | /* retrieve the scsi device */ |
2430 | if (!devt_to_devpath(makedev(major, minor), 1, device)) { | |
ba728be7 | 2431 | if (verbose > 0) |
fcebeb77 | 2432 | pr_err("failed to get device\n"); |
d665cc31 DW |
2433 | err = 2; |
2434 | break; | |
2435 | } | |
fcebeb77 | 2436 | if (devpath_to_char(device, "type", buf, sizeof(buf), 0)) { |
d665cc31 | 2437 | err = 2; |
d665cc31 DW |
2438 | break; |
2439 | } | |
2440 | type = strtoul(buf, NULL, 10); | |
2441 | ||
2442 | /* if it's not a disk print the vendor and model */ | |
2443 | if (!(type == 0 || type == 7 || type == 14)) { | |
2444 | vendor[0] = '\0'; | |
2445 | model[0] = '\0'; | |
fcebeb77 MT |
2446 | |
2447 | if (devpath_to_char(device, "vendor", buf, | |
2448 | sizeof(buf), 0) == 0) { | |
d665cc31 DW |
2449 | strncpy(vendor, buf, sizeof(vendor)); |
2450 | vendor[sizeof(vendor) - 1] = '\0'; | |
2451 | c = (char *) &vendor[sizeof(vendor) - 1]; | |
2452 | while (isspace(*c) || *c == '\0') | |
2453 | *c-- = '\0'; | |
2454 | ||
2455 | } | |
fcebeb77 MT |
2456 | |
2457 | if (devpath_to_char(device, "model", buf, | |
2458 | sizeof(buf), 0) == 0) { | |
d665cc31 DW |
2459 | strncpy(model, buf, sizeof(model)); |
2460 | model[sizeof(model) - 1] = '\0'; | |
2461 | c = (char *) &model[sizeof(model) - 1]; | |
2462 | while (isspace(*c) || *c == '\0') | |
2463 | *c-- = '\0'; | |
2464 | } | |
2465 | ||
2466 | if (vendor[0] && model[0]) | |
2467 | sprintf(buf, "%.64s %.64s", vendor, model); | |
2468 | else | |
2469 | switch (type) { /* numbers from hald/linux/device.c */ | |
2470 | case 1: sprintf(buf, "tape"); break; | |
2471 | case 2: sprintf(buf, "printer"); break; | |
2472 | case 3: sprintf(buf, "processor"); break; | |
2473 | case 4: | |
2474 | case 5: sprintf(buf, "cdrom"); break; | |
2475 | case 6: sprintf(buf, "scanner"); break; | |
2476 | case 8: sprintf(buf, "media_changer"); break; | |
2477 | case 9: sprintf(buf, "comm"); break; | |
2478 | case 12: sprintf(buf, "raid"); break; | |
2479 | default: sprintf(buf, "unknown"); | |
2480 | } | |
2481 | } else | |
2482 | buf[0] = '\0'; | |
d665cc31 DW |
2483 | |
2484 | /* chop device path to 'host%d' and calculate the port number */ | |
2485 | c = strchr(&path[hba_len], '/'); | |
4e5e717d | 2486 | if (!c) { |
ba728be7 | 2487 | if (verbose > 0) |
e7b84f9d | 2488 | pr_err("%s - invalid path name\n", path + hba_len); |
4e5e717d AW |
2489 | err = 2; |
2490 | break; | |
2491 | } | |
d665cc31 | 2492 | *c = '\0'; |
0858eccf AP |
2493 | if ((sscanf(&path[hba_len], "ata%d", &port) == 1) || |
2494 | ((sscanf(&path[hba_len], "host%d", &port) == 1))) | |
d665cc31 DW |
2495 | port -= host_base; |
2496 | else { | |
ba728be7 | 2497 | if (verbose > 0) { |
d665cc31 | 2498 | *c = '/'; /* repair the full string */ |
e7b84f9d | 2499 | pr_err("failed to determine port number for %s\n", |
d665cc31 DW |
2500 | path); |
2501 | } | |
2502 | err = 2; | |
2503 | break; | |
2504 | } | |
2505 | ||
2506 | /* mark this port as used */ | |
2507 | port_mask &= ~(1 << port); | |
2508 | ||
2509 | /* print out the device information */ | |
2510 | if (buf[0]) { | |
2511 | printf(" Port%d : - non-disk device (%s) -\n", port, buf); | |
2512 | continue; | |
2513 | } | |
2514 | ||
2515 | fd = dev_open(ent->d_name, O_RDONLY); | |
4389ce73 | 2516 | if (!is_fd_valid(fd)) |
d665cc31 DW |
2517 | printf(" Port%d : - disk info unavailable -\n", port); |
2518 | else { | |
2519 | fd2devname(fd, buf); | |
2520 | printf(" Port%d : %s", port, buf); | |
6da53c0e BK |
2521 | if (imsm_read_serial(fd, NULL, (__u8 *)buf, |
2522 | sizeof(buf)) == 0) | |
2523 | printf(" (%s)\n", buf); | |
d665cc31 | 2524 | else |
664d5325 | 2525 | printf(" ()\n"); |
bf62ed5d BK |
2526 | |
2527 | print_encryption_information(fd, hba->type); | |
4dab422a | 2528 | close(fd); |
d665cc31 | 2529 | } |
d665cc31 DW |
2530 | free(path); |
2531 | path = NULL; | |
2532 | } | |
2533 | if (path) | |
2534 | free(path); | |
2535 | if (dir) | |
2536 | closedir(dir); | |
2537 | if (err == 0) { | |
2538 | int i; | |
2539 | ||
2540 | for (i = 0; i < port_count; i++) | |
2541 | if (port_mask & (1 << i)) | |
2542 | printf(" Port%d : - no device attached -\n", i); | |
2543 | } | |
2544 | ||
2545 | return err; | |
2546 | } | |
2547 | ||
6da53c0e | 2548 | static int print_nvme_info(struct sys_dev *hba) |
60f0f54d PB |
2549 | { |
2550 | struct dirent *ent; | |
2551 | DIR *dir; | |
60f0f54d | 2552 | |
6da53c0e | 2553 | dir = opendir("/sys/block/"); |
b9135011 | 2554 | if (!dir) |
b5eece69 | 2555 | return 1; |
b9135011 JS |
2556 | |
2557 | for (ent = readdir(dir); ent; ent = readdir(dir)) { | |
8662f92d MT |
2558 | char ns_path[PATH_MAX]; |
2559 | char cntrl_path[PATH_MAX]; | |
2560 | char buf[PATH_MAX]; | |
2561 | int fd = -1; | |
60f0f54d | 2562 | |
8662f92d MT |
2563 | if (!strstr(ent->d_name, "nvme")) |
2564 | goto skip; | |
d835518b | 2565 | |
8662f92d | 2566 | fd = open_dev(ent->d_name); |
4389ce73 | 2567 | if (!is_fd_valid(fd)) |
8662f92d | 2568 | goto skip; |
d835518b | 2569 | |
8662f92d MT |
2570 | if (!diskfd_to_devpath(fd, 0, ns_path) || |
2571 | !diskfd_to_devpath(fd, 1, cntrl_path)) | |
2572 | goto skip; | |
2573 | ||
2574 | if (!path_attached_to_hba(cntrl_path, hba->path)) | |
2575 | goto skip; | |
2576 | ||
2577 | if (!imsm_is_nvme_namespace_supported(fd, 0)) | |
2578 | goto skip; | |
2579 | ||
2580 | fd2devname(fd, buf); | |
2581 | if (hba->type == SYS_DEV_VMD) | |
2582 | printf(" NVMe under VMD : %s", buf); | |
2583 | else if (hba->type == SYS_DEV_NVME) | |
2584 | printf(" NVMe Device : %s", buf); | |
2585 | ||
2586 | if (!imsm_read_serial(fd, NULL, (__u8 *)buf, | |
2587 | sizeof(buf))) | |
2588 | printf(" (%s)\n", buf); | |
2589 | else | |
2590 | printf("()\n"); | |
2591 | ||
bf62ed5d BK |
2592 | print_encryption_information(fd, hba->type); |
2593 | ||
8662f92d | 2594 | skip: |
4389ce73 | 2595 | close_fd(&fd); |
60f0f54d PB |
2596 | } |
2597 | ||
b9135011 | 2598 | closedir(dir); |
b5eece69 | 2599 | return 0; |
60f0f54d PB |
2600 | } |
2601 | ||
120dc887 LM |
2602 | static void print_found_intel_controllers(struct sys_dev *elem) |
2603 | { | |
2604 | for (; elem; elem = elem->next) { | |
e7b84f9d | 2605 | pr_err("found Intel(R) "); |
120dc887 LM |
2606 | if (elem->type == SYS_DEV_SATA) |
2607 | fprintf(stderr, "SATA "); | |
155cbb4c LM |
2608 | else if (elem->type == SYS_DEV_SAS) |
2609 | fprintf(stderr, "SAS "); | |
0858eccf AP |
2610 | else if (elem->type == SYS_DEV_NVME) |
2611 | fprintf(stderr, "NVMe "); | |
60f0f54d PB |
2612 | |
2613 | if (elem->type == SYS_DEV_VMD) | |
2614 | fprintf(stderr, "VMD domain"); | |
75350d87 KF |
2615 | else if (elem->type == SYS_DEV_SATA_VMD) |
2616 | fprintf(stderr, "SATA VMD domain"); | |
60f0f54d PB |
2617 | else |
2618 | fprintf(stderr, "RAID controller"); | |
2619 | ||
120dc887 LM |
2620 | if (elem->pci_id) |
2621 | fprintf(stderr, " at %s", elem->pci_id); | |
2622 | fprintf(stderr, ".\n"); | |
2623 | } | |
2624 | fflush(stderr); | |
2625 | } | |
2626 | ||
120dc887 LM |
2627 | static int ahci_get_port_count(const char *hba_path, int *port_count) |
2628 | { | |
2629 | struct dirent *ent; | |
2630 | DIR *dir; | |
2631 | int host_base = -1; | |
2632 | ||
2633 | *port_count = 0; | |
2634 | if ((dir = opendir(hba_path)) == NULL) | |
2635 | return -1; | |
2636 | ||
2637 | for (ent = readdir(dir); ent; ent = readdir(dir)) { | |
2638 | int host; | |
2639 | ||
0858eccf AP |
2640 | if ((sscanf(ent->d_name, "ata%d", &host) != 1) && |
2641 | ((sscanf(ent->d_name, "host%d", &host) != 1))) | |
120dc887 LM |
2642 | continue; |
2643 | if (*port_count == 0) | |
2644 | host_base = host; | |
2645 | else if (host < host_base) | |
2646 | host_base = host; | |
2647 | ||
2648 | if (host + 1 > *port_count + host_base) | |
2649 | *port_count = host + 1 - host_base; | |
2650 | } | |
2651 | closedir(dir); | |
2652 | return host_base; | |
2653 | } | |
2654 | ||
a891a3c2 LM |
2655 | static void print_imsm_capability(const struct imsm_orom *orom) |
2656 | { | |
0858eccf AP |
2657 | printf(" Platform : Intel(R) "); |
2658 | if (orom->capabilities == 0 && orom->driver_features == 0) | |
2659 | printf("Matrix Storage Manager\n"); | |
ab0c6bb9 AP |
2660 | else if (imsm_orom_is_enterprise(orom) && orom->major_ver >= 6) |
2661 | printf("Virtual RAID on CPU\n"); | |
0858eccf AP |
2662 | else |
2663 | printf("Rapid Storage Technology%s\n", | |
2664 | imsm_orom_is_enterprise(orom) ? " enterprise" : ""); | |
8d1114be MG |
2665 | if (orom->major_ver || orom->minor_ver || orom->hotfix_ver || orom->build) { |
2666 | if (imsm_orom_is_vmd_without_efi(orom)) | |
2667 | printf(" Version : %d.%d\n", orom->major_ver, | |
2668 | orom->minor_ver); | |
2669 | else | |
2670 | printf(" Version : %d.%d.%d.%d\n", orom->major_ver, | |
2671 | orom->minor_ver, orom->hotfix_ver, orom->build); | |
2672 | } | |
a891a3c2 LM |
2673 | printf(" RAID Levels :%s%s%s%s%s\n", |
2674 | imsm_orom_has_raid0(orom) ? " raid0" : "", | |
2675 | imsm_orom_has_raid1(orom) ? " raid1" : "", | |
2676 | imsm_orom_has_raid1e(orom) ? " raid1e" : "", | |
2677 | imsm_orom_has_raid10(orom) ? " raid10" : "", | |
2678 | imsm_orom_has_raid5(orom) ? " raid5" : ""); | |
2679 | printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n", | |
2680 | imsm_orom_has_chunk(orom, 2) ? " 2k" : "", | |
2681 | imsm_orom_has_chunk(orom, 4) ? " 4k" : "", | |
2682 | imsm_orom_has_chunk(orom, 8) ? " 8k" : "", | |
2683 | imsm_orom_has_chunk(orom, 16) ? " 16k" : "", | |
2684 | imsm_orom_has_chunk(orom, 32) ? " 32k" : "", | |
2685 | imsm_orom_has_chunk(orom, 64) ? " 64k" : "", | |
2686 | imsm_orom_has_chunk(orom, 128) ? " 128k" : "", | |
2687 | imsm_orom_has_chunk(orom, 256) ? " 256k" : "", | |
2688 | imsm_orom_has_chunk(orom, 512) ? " 512k" : "", | |
2689 | imsm_orom_has_chunk(orom, 1024*1) ? " 1M" : "", | |
2690 | imsm_orom_has_chunk(orom, 1024*2) ? " 2M" : "", | |
2691 | imsm_orom_has_chunk(orom, 1024*4) ? " 4M" : "", | |
2692 | imsm_orom_has_chunk(orom, 1024*8) ? " 8M" : "", | |
2693 | imsm_orom_has_chunk(orom, 1024*16) ? " 16M" : "", | |
2694 | imsm_orom_has_chunk(orom, 1024*32) ? " 32M" : "", | |
2695 | imsm_orom_has_chunk(orom, 1024*64) ? " 64M" : ""); | |
29cd0821 CA |
2696 | printf(" 2TB volumes :%s supported\n", |
2697 | (orom->attr & IMSM_OROM_ATTR_2TB)?"":" not"); | |
2698 | printf(" 2TB disks :%s supported\n", | |
2699 | (orom->attr & IMSM_OROM_ATTR_2TB_DISK)?"":" not"); | |
0e7f69a8 | 2700 | printf(" Max Disks : %d\n", orom->tds); |
0858eccf AP |
2701 | printf(" Max Volumes : %d per array, %d per %s\n", |
2702 | orom->vpa, orom->vphba, | |
2703 | imsm_orom_is_nvme(orom) ? "platform" : "controller"); | |
a891a3c2 LM |
2704 | return; |
2705 | } | |
2706 | ||
e50cf220 MN |
2707 | static void print_imsm_capability_export(const struct imsm_orom *orom) |
2708 | { | |
2709 | printf("MD_FIRMWARE_TYPE=imsm\n"); | |
0858eccf AP |
2710 | if (orom->major_ver || orom->minor_ver || orom->hotfix_ver || orom->build) |
2711 | printf("IMSM_VERSION=%d.%d.%d.%d\n", orom->major_ver, orom->minor_ver, | |
2712 | orom->hotfix_ver, orom->build); | |
e50cf220 MN |
2713 | printf("IMSM_SUPPORTED_RAID_LEVELS=%s%s%s%s%s\n", |
2714 | imsm_orom_has_raid0(orom) ? "raid0 " : "", | |
2715 | imsm_orom_has_raid1(orom) ? "raid1 " : "", | |
2716 | imsm_orom_has_raid1e(orom) ? "raid1e " : "", | |
2717 | imsm_orom_has_raid5(orom) ? "raid10 " : "", | |
2718 | imsm_orom_has_raid10(orom) ? "raid5 " : ""); | |
2719 | printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n", | |
2720 | imsm_orom_has_chunk(orom, 2) ? "2k " : "", | |
2721 | imsm_orom_has_chunk(orom, 4) ? "4k " : "", | |
2722 | imsm_orom_has_chunk(orom, 8) ? "8k " : "", | |
2723 | imsm_orom_has_chunk(orom, 16) ? "16k " : "", | |
2724 | imsm_orom_has_chunk(orom, 32) ? "32k " : "", | |
2725 | imsm_orom_has_chunk(orom, 64) ? "64k " : "", | |
2726 | imsm_orom_has_chunk(orom, 128) ? "128k " : "", | |
2727 | imsm_orom_has_chunk(orom, 256) ? "256k " : "", | |
2728 | imsm_orom_has_chunk(orom, 512) ? "512k " : "", | |
2729 | imsm_orom_has_chunk(orom, 1024*1) ? "1M " : "", | |
2730 | imsm_orom_has_chunk(orom, 1024*2) ? "2M " : "", | |
2731 | imsm_orom_has_chunk(orom, 1024*4) ? "4M " : "", | |
2732 | imsm_orom_has_chunk(orom, 1024*8) ? "8M " : "", | |
2733 | imsm_orom_has_chunk(orom, 1024*16) ? "16M " : "", | |
2734 | imsm_orom_has_chunk(orom, 1024*32) ? "32M " : "", | |
2735 | imsm_orom_has_chunk(orom, 1024*64) ? "64M " : ""); | |
2736 | printf("IMSM_2TB_VOLUMES=%s\n",(orom->attr & IMSM_OROM_ATTR_2TB) ? "yes" : "no"); | |
2737 | printf("IMSM_2TB_DISKS=%s\n",(orom->attr & IMSM_OROM_ATTR_2TB_DISK) ? "yes" : "no"); | |
2738 | printf("IMSM_MAX_DISKS=%d\n",orom->tds); | |
2739 | printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom->vpa); | |
2740 | printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom->vphba); | |
2741 | } | |
2742 | ||
9eafa1de | 2743 | static int detail_platform_imsm(int verbose, int enumerate_only, char *controller_path) |
d665cc31 DW |
2744 | { |
2745 | /* There are two components to imsm platform support, the ahci SATA | |
2746 | * controller and the option-rom. To find the SATA controller we | |
2747 | * simply look in /sys/bus/pci/drivers/ahci to see if an ahci | |
2748 | * controller with the Intel vendor id is present. This approach | |
2749 | * allows mdadm to leverage the kernel's ahci detection logic, with the | |
2750 | * caveat that if ahci.ko is not loaded mdadm will not be able to | |
2751 | * detect platform raid capabilities. The option-rom resides in a | |
2752 | * platform "Adapter ROM". We scan for its signature to retrieve the | |
2753 | * platform capabilities. If raid support is disabled in the BIOS the | |
2754 | * option-rom capability structure will not be available. | |
2755 | */ | |
d665cc31 | 2756 | struct sys_dev *list, *hba; |
d665cc31 DW |
2757 | int host_base = 0; |
2758 | int port_count = 0; | |
9eafa1de | 2759 | int result=1; |
d665cc31 | 2760 | |
5615172f | 2761 | if (enumerate_only) { |
420dafcd | 2762 | if (check_no_platform()) |
5615172f | 2763 | return 0; |
a891a3c2 LM |
2764 | list = find_intel_devices(); |
2765 | if (!list) | |
2766 | return 2; | |
2767 | for (hba = list; hba; hba = hba->next) { | |
6b781d33 AP |
2768 | if (find_imsm_capability(hba)) { |
2769 | result = 0; | |
a891a3c2 LM |
2770 | break; |
2771 | } | |
9eafa1de | 2772 | else |
6b781d33 | 2773 | result = 2; |
a891a3c2 | 2774 | } |
a891a3c2 | 2775 | return result; |
5615172f DW |
2776 | } |
2777 | ||
155cbb4c LM |
2778 | list = find_intel_devices(); |
2779 | if (!list) { | |
ba728be7 | 2780 | if (verbose > 0) |
7a862a02 | 2781 | pr_err("no active Intel(R) RAID controller found.\n"); |
d665cc31 | 2782 | return 2; |
ba728be7 | 2783 | } else if (verbose > 0) |
155cbb4c | 2784 | print_found_intel_controllers(list); |
d665cc31 | 2785 | |
a891a3c2 | 2786 | for (hba = list; hba; hba = hba->next) { |
0858eccf | 2787 | if (controller_path && (compare_paths(hba->path, controller_path) != 0)) |
9eafa1de | 2788 | continue; |
0858eccf | 2789 | if (!find_imsm_capability(hba)) { |
60f0f54d | 2790 | char buf[PATH_MAX]; |
e7b84f9d | 2791 | pr_err("imsm capabilities not found for controller: %s (type %s)\n", |
75350d87 KF |
2792 | hba->type == SYS_DEV_VMD || hba->type == SYS_DEV_SATA_VMD ? |
2793 | vmd_domain_to_controller(hba, buf) : | |
2794 | hba->path, get_sys_dev_type(hba->type)); | |
0858eccf AP |
2795 | continue; |
2796 | } | |
2797 | result = 0; | |
2798 | } | |
2799 | ||
2800 | if (controller_path && result == 1) { | |
2801 | pr_err("no active Intel(R) RAID controller found under %s\n", | |
2802 | controller_path); | |
2803 | return result; | |
2804 | } | |
2805 | ||
5e1d6128 | 2806 | const struct orom_entry *entry; |
0858eccf | 2807 | |
5e1d6128 | 2808 | for (entry = orom_entries; entry; entry = entry->next) { |
60f0f54d | 2809 | if (entry->type == SYS_DEV_VMD) { |
07cb1e57 | 2810 | print_imsm_capability(&entry->orom); |
32716c51 PB |
2811 | printf(" 3rd party NVMe :%s supported\n", |
2812 | imsm_orom_has_tpv_support(&entry->orom)?"":" not"); | |
60f0f54d PB |
2813 | for (hba = list; hba; hba = hba->next) { |
2814 | if (hba->type == SYS_DEV_VMD) { | |
2815 | char buf[PATH_MAX]; | |
60f0f54d PB |
2816 | printf(" I/O Controller : %s (%s)\n", |
2817 | vmd_domain_to_controller(hba, buf), get_sys_dev_type(hba->type)); | |
6da53c0e | 2818 | if (print_nvme_info(hba)) { |
b5eece69 PB |
2819 | if (verbose > 0) |
2820 | pr_err("failed to get devices attached to VMD domain.\n"); | |
2821 | result |= 2; | |
2822 | } | |
60f0f54d PB |
2823 | } |
2824 | } | |
07cb1e57 | 2825 | printf("\n"); |
60f0f54d PB |
2826 | continue; |
2827 | } | |
0858eccf | 2828 | |
60f0f54d PB |
2829 | print_imsm_capability(&entry->orom); |
2830 | if (entry->type == SYS_DEV_NVME) { | |
0858eccf AP |
2831 | for (hba = list; hba; hba = hba->next) { |
2832 | if (hba->type == SYS_DEV_NVME) | |
6da53c0e | 2833 | print_nvme_info(hba); |
0858eccf | 2834 | } |
60f0f54d | 2835 | printf("\n"); |
0858eccf AP |
2836 | continue; |
2837 | } | |
2838 | ||
2839 | struct devid_list *devid; | |
5e1d6128 | 2840 | for (devid = entry->devid_list; devid; devid = devid->next) { |
0858eccf AP |
2841 | hba = device_by_id(devid->devid); |
2842 | if (!hba) | |
2843 | continue; | |
2844 | ||
9eafa1de MN |
2845 | printf(" I/O Controller : %s (%s)\n", |
2846 | hba->path, get_sys_dev_type(hba->type)); | |
75350d87 | 2847 | if (hba->type == SYS_DEV_SATA || hba->type == SYS_DEV_SATA_VMD) { |
9eafa1de | 2848 | host_base = ahci_get_port_count(hba->path, &port_count); |
bf62ed5d | 2849 | if (ahci_enumerate_ports(hba, port_count, host_base, verbose)) { |
9eafa1de | 2850 | if (verbose > 0) |
75350d87 KF |
2851 | pr_err("failed to enumerate ports on %s controller at %s.\n", |
2852 | get_sys_dev_type(hba->type), hba->pci_id); | |
9eafa1de MN |
2853 | result |= 2; |
2854 | } | |
120dc887 LM |
2855 | } |
2856 | } | |
0858eccf | 2857 | printf("\n"); |
d665cc31 | 2858 | } |
155cbb4c | 2859 | |
120dc887 | 2860 | return result; |
d665cc31 | 2861 | } |
e50cf220 | 2862 | |
9eafa1de | 2863 | static int export_detail_platform_imsm(int verbose, char *controller_path) |
e50cf220 | 2864 | { |
e50cf220 MN |
2865 | struct sys_dev *list, *hba; |
2866 | int result=1; | |
2867 | ||
2868 | list = find_intel_devices(); | |
2869 | if (!list) { | |
2870 | if (verbose > 0) | |
2871 | pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n"); | |
2872 | result = 2; | |
e50cf220 MN |
2873 | return result; |
2874 | } | |
2875 | ||
2876 | for (hba = list; hba; hba = hba->next) { | |
9eafa1de MN |
2877 | if (controller_path && (compare_paths(hba->path,controller_path) != 0)) |
2878 | continue; | |
60f0f54d PB |
2879 | if (!find_imsm_capability(hba) && verbose > 0) { |
2880 | char buf[PATH_MAX]; | |
2881 | pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n", | |
75350d87 KF |
2882 | hba->type == SYS_DEV_VMD || hba->type == SYS_DEV_SATA_VMD ? |
2883 | vmd_domain_to_controller(hba, buf) : hba->path); | |
60f0f54d | 2884 | } |
0858eccf | 2885 | else |
e50cf220 | 2886 | result = 0; |
e50cf220 MN |
2887 | } |
2888 | ||
5e1d6128 | 2889 | const struct orom_entry *entry; |
0858eccf | 2890 | |
60f0f54d | 2891 | for (entry = orom_entries; entry; entry = entry->next) { |
75350d87 | 2892 | if (entry->type == SYS_DEV_VMD || entry->type == SYS_DEV_SATA_VMD) { |
60f0f54d PB |
2893 | for (hba = list; hba; hba = hba->next) |
2894 | print_imsm_capability_export(&entry->orom); | |
2895 | continue; | |
2896 | } | |
5e1d6128 | 2897 | print_imsm_capability_export(&entry->orom); |
60f0f54d | 2898 | } |
0858eccf | 2899 | |
e50cf220 MN |
2900 | return result; |
2901 | } | |
2902 | ||
cdddbdbc DW |
2903 | static int match_home_imsm(struct supertype *st, char *homehost) |
2904 | { | |
5115ca67 DW |
2905 | /* the imsm metadata format does not specify any host |
2906 | * identification information. We return -1 since we can never | |
2907 | * confirm nor deny whether a given array is "meant" for this | |
148acb7b | 2908 | * host. We rely on compare_super and the 'family_num' fields to |
5115ca67 DW |
2909 | * exclude member disks that do not belong, and we rely on |
2910 | * mdadm.conf to specify the arrays that should be assembled. | |
2911 | * Auto-assembly may still pick up "foreign" arrays. | |
2912 | */ | |
cdddbdbc | 2913 | |
9362c1c8 | 2914 | return -1; |
cdddbdbc DW |
2915 | } |
2916 | ||
2917 | static void uuid_from_super_imsm(struct supertype *st, int uuid[4]) | |
2918 | { | |
51006d85 N |
2919 | /* The uuid returned here is used for: |
2920 | * uuid to put into bitmap file (Create, Grow) | |
2921 | * uuid for backup header when saving critical section (Grow) | |
2922 | * comparing uuids when re-adding a device into an array | |
2923 | * In these cases the uuid required is that of the data-array, | |
2924 | * not the device-set. | |
2925 | * uuid to recognise same set when adding a missing device back | |
2926 | * to an array. This is a uuid for the device-set. | |
1011e834 | 2927 | * |
51006d85 N |
2928 | * For each of these we can make do with a truncated |
2929 | * or hashed uuid rather than the original, as long as | |
2930 | * everyone agrees. | |
2931 | * In each case the uuid required is that of the data-array, | |
2932 | * not the device-set. | |
43dad3d6 | 2933 | */ |
51006d85 N |
2934 | /* imsm does not track uuid's so we synthesis one using sha1 on |
2935 | * - The signature (Which is constant for all imsm array, but no matter) | |
148acb7b | 2936 | * - the orig_family_num of the container |
51006d85 N |
2937 | * - the index number of the volume |
2938 | * - the 'serial' number of the volume. | |
2939 | * Hopefully these are all constant. | |
2940 | */ | |
2941 | struct intel_super *super = st->sb; | |
43dad3d6 | 2942 | |
51006d85 N |
2943 | char buf[20]; |
2944 | struct sha1_ctx ctx; | |
2945 | struct imsm_dev *dev = NULL; | |
148acb7b | 2946 | __u32 family_num; |
51006d85 | 2947 | |
148acb7b DW |
2948 | /* some mdadm versions failed to set ->orig_family_num, in which |
2949 | * case fall back to ->family_num. orig_family_num will be | |
2950 | * fixed up with the first metadata update. | |
2951 | */ | |
2952 | family_num = super->anchor->orig_family_num; | |
2953 | if (family_num == 0) | |
2954 | family_num = super->anchor->family_num; | |
51006d85 | 2955 | sha1_init_ctx(&ctx); |
92bd8f8d | 2956 | sha1_process_bytes(super->anchor->sig, MPB_SIG_LEN, &ctx); |
148acb7b | 2957 | sha1_process_bytes(&family_num, sizeof(__u32), &ctx); |
51006d85 N |
2958 | if (super->current_vol >= 0) |
2959 | dev = get_imsm_dev(super, super->current_vol); | |
2960 | if (dev) { | |
2961 | __u32 vol = super->current_vol; | |
2962 | sha1_process_bytes(&vol, sizeof(vol), &ctx); | |
2963 | sha1_process_bytes(dev->volume, MAX_RAID_SERIAL_LEN, &ctx); | |
2964 | } | |
2965 | sha1_finish_ctx(&ctx, buf); | |
2966 | memcpy(uuid, buf, 4*4); | |
cdddbdbc DW |
2967 | } |
2968 | ||
1e5c6983 DW |
2969 | static __u32 migr_strip_blocks_resync(struct imsm_dev *dev) |
2970 | { | |
2971 | /* migr_strip_size when repairing or initializing parity */ | |
238c0a71 | 2972 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
1e5c6983 DW |
2973 | __u32 chunk = __le32_to_cpu(map->blocks_per_strip); |
2974 | ||
2975 | switch (get_imsm_raid_level(map)) { | |
2976 | case 5: | |
2977 | case 10: | |
2978 | return chunk; | |
2979 | default: | |
2980 | return 128*1024 >> 9; | |
2981 | } | |
2982 | } | |
2983 | ||
2984 | static __u32 migr_strip_blocks_rebuild(struct imsm_dev *dev) | |
2985 | { | |
2986 | /* migr_strip_size when rebuilding a degraded disk, no idea why | |
2987 | * this is different than migr_strip_size_resync(), but it's good | |
2988 | * to be compatible | |
2989 | */ | |
238c0a71 | 2990 | struct imsm_map *map = get_imsm_map(dev, MAP_1); |
1e5c6983 DW |
2991 | __u32 chunk = __le32_to_cpu(map->blocks_per_strip); |
2992 | ||
2993 | switch (get_imsm_raid_level(map)) { | |
2994 | case 1: | |
2995 | case 10: | |
2996 | if (map->num_members % map->num_domains == 0) | |
2997 | return 128*1024 >> 9; | |
2998 | else | |
2999 | return chunk; | |
3000 | case 5: | |
3001 | return max((__u32) 64*1024 >> 9, chunk); | |
3002 | default: | |
3003 | return 128*1024 >> 9; | |
3004 | } | |
3005 | } | |
3006 | ||
3007 | static __u32 num_stripes_per_unit_resync(struct imsm_dev *dev) | |
3008 | { | |
238c0a71 AK |
3009 | struct imsm_map *lo = get_imsm_map(dev, MAP_0); |
3010 | struct imsm_map *hi = get_imsm_map(dev, MAP_1); | |
1e5c6983 DW |
3011 | __u32 lo_chunk = __le32_to_cpu(lo->blocks_per_strip); |
3012 | __u32 hi_chunk = __le32_to_cpu(hi->blocks_per_strip); | |
3013 | ||
3014 | return max((__u32) 1, hi_chunk / lo_chunk); | |
3015 | } | |
3016 | ||
3017 | static __u32 num_stripes_per_unit_rebuild(struct imsm_dev *dev) | |
3018 | { | |
238c0a71 | 3019 | struct imsm_map *lo = get_imsm_map(dev, MAP_0); |
1e5c6983 DW |
3020 | int level = get_imsm_raid_level(lo); |
3021 | ||
3022 | if (level == 1 || level == 10) { | |
238c0a71 | 3023 | struct imsm_map *hi = get_imsm_map(dev, MAP_1); |
1e5c6983 DW |
3024 | |
3025 | return hi->num_domains; | |
3026 | } else | |
3027 | return num_stripes_per_unit_resync(dev); | |
3028 | } | |
3029 | ||
44490938 MD |
3030 | static unsigned long long calc_component_size(struct imsm_map *map, |
3031 | struct imsm_dev *dev) | |
3032 | { | |
3033 | unsigned long long component_size; | |
3034 | unsigned long long dev_size = imsm_dev_size(dev); | |
a4f7290c | 3035 | long long calc_dev_size = 0; |
44490938 MD |
3036 | unsigned int member_disks = imsm_num_data_members(map); |
3037 | ||
3038 | if (member_disks == 0) | |
3039 | return 0; | |
3040 | ||
3041 | component_size = per_dev_array_size(map); | |
3042 | calc_dev_size = component_size * member_disks; | |
3043 | ||
3044 | /* Component size is rounded to 1MB so difference between size from | |
3045 | * metadata and size calculated from num_data_stripes equals up to | |
3046 | * 2048 blocks per each device. If the difference is higher it means | |
3047 | * that array size was expanded and num_data_stripes was not updated. | |
3048 | */ | |
a4f7290c | 3049 | if (llabs(calc_dev_size - (long long)dev_size) > |
44490938 MD |
3050 | (1 << SECT_PER_MB_SHIFT) * member_disks) { |
3051 | component_size = dev_size / member_disks; | |
3052 | dprintf("Invalid num_data_stripes in metadata; expected=%llu, found=%llu\n", | |
3053 | component_size / map->blocks_per_strip, | |
3054 | num_data_stripes(map)); | |
3055 | } | |
3056 | ||
3057 | return component_size; | |
3058 | } | |
3059 | ||
1e5c6983 DW |
3060 | static __u32 parity_segment_depth(struct imsm_dev *dev) |
3061 | { | |
238c0a71 | 3062 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
1e5c6983 DW |
3063 | __u32 chunk = __le32_to_cpu(map->blocks_per_strip); |
3064 | ||
3065 | switch(get_imsm_raid_level(map)) { | |
3066 | case 1: | |
3067 | case 10: | |
3068 | return chunk * map->num_domains; | |
3069 | case 5: | |
3070 | return chunk * map->num_members; | |
3071 | default: | |
3072 | return chunk; | |
3073 | } | |
3074 | } | |
3075 | ||
3076 | static __u32 map_migr_block(struct imsm_dev *dev, __u32 block) | |
3077 | { | |
238c0a71 | 3078 | struct imsm_map *map = get_imsm_map(dev, MAP_1); |
1e5c6983 DW |
3079 | __u32 chunk = __le32_to_cpu(map->blocks_per_strip); |
3080 | __u32 strip = block / chunk; | |
3081 | ||
3082 | switch (get_imsm_raid_level(map)) { | |
3083 | case 1: | |
3084 | case 10: { | |
3085 | __u32 vol_strip = (strip * map->num_domains) + 1; | |
3086 | __u32 vol_stripe = vol_strip / map->num_members; | |
3087 | ||
3088 | return vol_stripe * chunk + block % chunk; | |
3089 | } case 5: { | |
3090 | __u32 stripe = strip / (map->num_members - 1); | |
3091 | ||
3092 | return stripe * chunk + block % chunk; | |
3093 | } | |
3094 | default: | |
3095 | return 0; | |
3096 | } | |
3097 | } | |
3098 | ||
c47b0ff6 AK |
3099 | static __u64 blocks_per_migr_unit(struct intel_super *super, |
3100 | struct imsm_dev *dev) | |
1e5c6983 DW |
3101 | { |
3102 | /* calculate the conversion factor between per member 'blocks' | |
3103 | * (md/{resync,rebuild}_start) and imsm migration units, return | |
3104 | * 0 for the 'not migrating' and 'unsupported migration' cases | |
3105 | */ | |
3106 | if (!dev->vol.migr_state) | |
3107 | return 0; | |
3108 | ||
3109 | switch (migr_type(dev)) { | |
c47b0ff6 AK |
3110 | case MIGR_GEN_MIGR: { |
3111 | struct migr_record *migr_rec = super->migr_rec; | |
3112 | return __le32_to_cpu(migr_rec->blocks_per_unit); | |
3113 | } | |
1e5c6983 DW |
3114 | case MIGR_VERIFY: |
3115 | case MIGR_REPAIR: | |
3116 | case MIGR_INIT: { | |
238c0a71 | 3117 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
1e5c6983 DW |
3118 | __u32 stripes_per_unit; |
3119 | __u32 blocks_per_unit; | |
3120 | __u32 parity_depth; | |
3121 | __u32 migr_chunk; | |
3122 | __u32 block_map; | |
3123 | __u32 block_rel; | |
3124 | __u32 segment; | |
3125 | __u32 stripe; | |
3126 | __u8 disks; | |
3127 | ||
3128 | /* yes, this is really the translation of migr_units to | |
3129 | * per-member blocks in the 'resync' case | |
3130 | */ | |
3131 | stripes_per_unit = num_stripes_per_unit_resync(dev); | |
3132 | migr_chunk = migr_strip_blocks_resync(dev); | |
9529d343 | 3133 | disks = imsm_num_data_members(map); |
1e5c6983 | 3134 | blocks_per_unit = stripes_per_unit * migr_chunk * disks; |
7b1ab482 | 3135 | stripe = __le16_to_cpu(map->blocks_per_strip) * disks; |
1e5c6983 DW |
3136 | segment = blocks_per_unit / stripe; |
3137 | block_rel = blocks_per_unit - segment * stripe; | |
3138 | parity_depth = parity_segment_depth(dev); | |
3139 | block_map = map_migr_block(dev, block_rel); | |
3140 | return block_map + parity_depth * segment; | |
3141 | } | |
3142 | case MIGR_REBUILD: { | |
3143 | __u32 stripes_per_unit; | |
3144 | __u32 migr_chunk; | |
3145 | ||
3146 | stripes_per_unit = num_stripes_per_unit_rebuild(dev); | |
3147 | migr_chunk = migr_strip_blocks_rebuild(dev); | |
3148 | return migr_chunk * stripes_per_unit; | |
3149 | } | |
1e5c6983 DW |
3150 | case MIGR_STATE_CHANGE: |
3151 | default: | |
3152 | return 0; | |
3153 | } | |
3154 | } | |
3155 | ||
c2c087e6 DW |
3156 | static int imsm_level_to_layout(int level) |
3157 | { | |
3158 | switch (level) { | |
3159 | case 0: | |
3160 | case 1: | |
3161 | return 0; | |
3162 | case 5: | |
3163 | case 6: | |
a380c027 | 3164 | return ALGORITHM_LEFT_ASYMMETRIC; |
c2c087e6 | 3165 | case 10: |
c92a2527 | 3166 | return 0x102; |
c2c087e6 | 3167 | } |
a18a888e | 3168 | return UnSet; |
c2c087e6 DW |
3169 | } |
3170 | ||
8e59f3d8 AK |
3171 | /******************************************************************************* |
3172 | * Function: read_imsm_migr_rec | |
3173 | * Description: Function reads imsm migration record from last sector of disk | |
3174 | * Parameters: | |
3175 | * fd : disk descriptor | |
3176 | * super : metadata info | |
3177 | * Returns: | |
3178 | * 0 : success, | |
3179 | * -1 : fail | |
3180 | ******************************************************************************/ | |
3181 | static int read_imsm_migr_rec(int fd, struct intel_super *super) | |
3182 | { | |
3183 | int ret_val = -1; | |
de44e46f | 3184 | unsigned int sector_size = super->sector_size; |
8e59f3d8 AK |
3185 | unsigned long long dsize; |
3186 | ||
3187 | get_dev_size(fd, NULL, &dsize); | |
de44e46f PB |
3188 | if (lseek64(fd, dsize - (sector_size*MIGR_REC_SECTOR_POSITION), |
3189 | SEEK_SET) < 0) { | |
e7b84f9d N |
3190 | pr_err("Cannot seek to anchor block: %s\n", |
3191 | strerror(errno)); | |
8e59f3d8 AK |
3192 | goto out; |
3193 | } | |
466070ad | 3194 | if ((unsigned int)read(fd, super->migr_rec_buf, |
de44e46f PB |
3195 | MIGR_REC_BUF_SECTORS*sector_size) != |
3196 | MIGR_REC_BUF_SECTORS*sector_size) { | |
e7b84f9d N |
3197 | pr_err("Cannot read migr record block: %s\n", |
3198 | strerror(errno)); | |
8e59f3d8 AK |
3199 | goto out; |
3200 | } | |
3201 | ret_val = 0; | |
de44e46f PB |
3202 | if (sector_size == 4096) |
3203 | convert_from_4k_imsm_migr_rec(super); | |
8e59f3d8 AK |
3204 | |
3205 | out: | |
3206 | return ret_val; | |
3207 | } | |
3208 | ||
3136abe5 AK |
3209 | static struct imsm_dev *imsm_get_device_during_migration( |
3210 | struct intel_super *super) | |
3211 | { | |
3212 | ||
3213 | struct intel_dev *dv; | |
3214 | ||
3215 | for (dv = super->devlist; dv; dv = dv->next) { | |
3216 | if (is_gen_migration(dv->dev)) | |
3217 | return dv->dev; | |
3218 | } | |
3219 | return NULL; | |
3220 | } | |
3221 | ||
8e59f3d8 AK |
3222 | /******************************************************************************* |
3223 | * Function: load_imsm_migr_rec | |
3224 | * Description: Function reads imsm migration record (it is stored at the last | |
3225 | * sector of disk) | |
3226 | * Parameters: | |
3227 | * super : imsm internal array info | |
8e59f3d8 AK |
3228 | * Returns: |
3229 | * 0 : success | |
3230 | * -1 : fail | |
4c965cc9 | 3231 | * -2 : no migration in progress |
8e59f3d8 | 3232 | ******************************************************************************/ |
2f86fda3 | 3233 | static int load_imsm_migr_rec(struct intel_super *super) |
8e59f3d8 | 3234 | { |
594dc1b8 | 3235 | struct dl *dl; |
8e59f3d8 AK |
3236 | char nm[30]; |
3237 | int retval = -1; | |
3238 | int fd = -1; | |
3136abe5 | 3239 | struct imsm_dev *dev; |
594dc1b8 | 3240 | struct imsm_map *map; |
b4ab44d8 | 3241 | int slot = -1; |
2f86fda3 | 3242 | int keep_fd = 1; |
3136abe5 AK |
3243 | |
3244 | /* find map under migration */ | |
3245 | dev = imsm_get_device_during_migration(super); | |
3246 | /* nothing to load,no migration in progress? | |
3247 | */ | |
3248 | if (dev == NULL) | |
4c965cc9 | 3249 | return -2; |
8e59f3d8 | 3250 | |
2f86fda3 MT |
3251 | map = get_imsm_map(dev, MAP_0); |
3252 | if (!map) | |
3253 | return -1; | |
3136abe5 | 3254 | |
2f86fda3 MT |
3255 | for (dl = super->disks; dl; dl = dl->next) { |
3256 | /* skip spare and failed disks | |
3257 | */ | |
3258 | if (dl->index < 0) | |
3259 | continue; | |
3260 | /* read only from one of the first two slots | |
3261 | */ | |
3262 | slot = get_imsm_disk_slot(map, dl->index); | |
3263 | if (slot > 1 || slot < 0) | |
3264 | continue; | |
3265 | ||
4389ce73 | 3266 | if (!is_fd_valid(dl->fd)) { |
8e59f3d8 AK |
3267 | sprintf(nm, "%d:%d", dl->major, dl->minor); |
3268 | fd = dev_open(nm, O_RDONLY); | |
4389ce73 MT |
3269 | |
3270 | if (is_fd_valid(fd)) { | |
2f86fda3 | 3271 | keep_fd = 0; |
8e59f3d8 | 3272 | break; |
2f86fda3 MT |
3273 | } |
3274 | } else { | |
3275 | fd = dl->fd; | |
3276 | break; | |
8e59f3d8 AK |
3277 | } |
3278 | } | |
2f86fda3 | 3279 | |
4389ce73 | 3280 | if (!is_fd_valid(fd)) |
2f86fda3 | 3281 | return retval; |
8e59f3d8 | 3282 | retval = read_imsm_migr_rec(fd, super); |
2f86fda3 | 3283 | if (!keep_fd) |
8e59f3d8 | 3284 | close(fd); |
2f86fda3 | 3285 | |
8e59f3d8 AK |
3286 | return retval; |
3287 | } | |
3288 | ||
c17608ea AK |
3289 | /******************************************************************************* |
3290 | * function: imsm_create_metadata_checkpoint_update | |
3291 | * Description: It creates update for checkpoint change. | |
3292 | * Parameters: | |
3293 | * super : imsm internal array info | |
3294 | * u : pointer to prepared update | |
3295 | * Returns: | |
3296 | * Uptate length. | |
3297 | * If length is equal to 0, input pointer u contains no update | |
3298 | ******************************************************************************/ | |
3299 | static int imsm_create_metadata_checkpoint_update( | |
3300 | struct intel_super *super, | |
3301 | struct imsm_update_general_migration_checkpoint **u) | |
3302 | { | |
3303 | ||
3304 | int update_memory_size = 0; | |
3305 | ||
1ade5cc1 | 3306 | dprintf("(enter)\n"); |
c17608ea AK |
3307 | |
3308 | if (u == NULL) | |
3309 | return 0; | |
3310 | *u = NULL; | |
3311 | ||
3312 | /* size of all update data without anchor */ | |
3313 | update_memory_size = | |
3314 | sizeof(struct imsm_update_general_migration_checkpoint); | |
3315 | ||
503975b9 | 3316 | *u = xcalloc(1, update_memory_size); |
c17608ea | 3317 | if (*u == NULL) { |
1ade5cc1 | 3318 | dprintf("error: cannot get memory\n"); |
c17608ea AK |
3319 | return 0; |
3320 | } | |
3321 | (*u)->type = update_general_migration_checkpoint; | |
9f421827 | 3322 | (*u)->curr_migr_unit = current_migr_unit(super->migr_rec); |
83b3de77 | 3323 | dprintf("prepared for %llu\n", (unsigned long long)(*u)->curr_migr_unit); |
c17608ea AK |
3324 | |
3325 | return update_memory_size; | |
3326 | } | |
3327 | ||
c17608ea AK |
3328 | static void imsm_update_metadata_locally(struct supertype *st, |
3329 | void *buf, int len); | |
3330 | ||
687629c2 AK |
3331 | /******************************************************************************* |
3332 | * Function: write_imsm_migr_rec | |
3333 | * Description: Function writes imsm migration record | |
3334 | * (at the last sector of disk) | |
3335 | * Parameters: | |
3336 | * super : imsm internal array info | |
3337 | * Returns: | |
3338 | * 0 : success | |
3339 | * -1 : if fail | |
3340 | ******************************************************************************/ | |
3341 | static int write_imsm_migr_rec(struct supertype *st) | |
3342 | { | |
3343 | struct intel_super *super = st->sb; | |
de44e46f | 3344 | unsigned int sector_size = super->sector_size; |
687629c2 | 3345 | unsigned long long dsize; |
687629c2 AK |
3346 | int retval = -1; |
3347 | struct dl *sd; | |
c17608ea AK |
3348 | int len; |
3349 | struct imsm_update_general_migration_checkpoint *u; | |
3136abe5 | 3350 | struct imsm_dev *dev; |
594dc1b8 | 3351 | struct imsm_map *map; |
3136abe5 AK |
3352 | |
3353 | /* find map under migration */ | |
3354 | dev = imsm_get_device_during_migration(super); | |
3355 | /* if no migration, write buffer anyway to clear migr_record | |
3356 | * on disk based on first available device | |
3357 | */ | |
3358 | if (dev == NULL) | |
3359 | dev = get_imsm_dev(super, super->current_vol < 0 ? 0 : | |
3360 | super->current_vol); | |
3361 | ||
44bfe6df | 3362 | map = get_imsm_map(dev, MAP_0); |
687629c2 | 3363 | |
de44e46f PB |
3364 | if (sector_size == 4096) |
3365 | convert_to_4k_imsm_migr_rec(super); | |
687629c2 | 3366 | for (sd = super->disks ; sd ; sd = sd->next) { |
b4ab44d8 | 3367 | int slot = -1; |
3136abe5 AK |
3368 | |
3369 | /* skip failed and spare devices */ | |
3370 | if (sd->index < 0) | |
3371 | continue; | |
687629c2 | 3372 | /* write to 2 first slots only */ |
3136abe5 AK |
3373 | if (map) |
3374 | slot = get_imsm_disk_slot(map, sd->index); | |
089f9d79 | 3375 | if (map == NULL || slot > 1 || slot < 0) |
687629c2 | 3376 | continue; |
3136abe5 | 3377 | |
2f86fda3 MT |
3378 | get_dev_size(sd->fd, NULL, &dsize); |
3379 | if (lseek64(sd->fd, dsize - (MIGR_REC_SECTOR_POSITION * | |
3380 | sector_size), | |
de44e46f | 3381 | SEEK_SET) < 0) { |
e7b84f9d N |
3382 | pr_err("Cannot seek to anchor block: %s\n", |
3383 | strerror(errno)); | |
687629c2 AK |
3384 | goto out; |
3385 | } | |
2f86fda3 | 3386 | if ((unsigned int)write(sd->fd, super->migr_rec_buf, |
de44e46f PB |
3387 | MIGR_REC_BUF_SECTORS*sector_size) != |
3388 | MIGR_REC_BUF_SECTORS*sector_size) { | |
e7b84f9d N |
3389 | pr_err("Cannot write migr record block: %s\n", |
3390 | strerror(errno)); | |
687629c2 AK |
3391 | goto out; |
3392 | } | |
687629c2 | 3393 | } |
de44e46f PB |
3394 | if (sector_size == 4096) |
3395 | convert_from_4k_imsm_migr_rec(super); | |
c17608ea AK |
3396 | /* update checkpoint information in metadata */ |
3397 | len = imsm_create_metadata_checkpoint_update(super, &u); | |
c17608ea AK |
3398 | if (len <= 0) { |
3399 | dprintf("imsm: Cannot prepare update\n"); | |
3400 | goto out; | |
3401 | } | |
3402 | /* update metadata locally */ | |
3403 | imsm_update_metadata_locally(st, u, len); | |
3404 | /* and possibly remotely */ | |
3405 | if (st->update_tail) { | |
3406 | append_metadata_update(st, u, len); | |
3407 | /* during reshape we do all work inside metadata handler | |
3408 | * manage_reshape(), so metadata update has to be triggered | |
3409 | * insida it | |
3410 | */ | |
3411 | flush_metadata_updates(st); | |
3412 | st->update_tail = &st->updates; | |
3413 | } else | |
3414 | free(u); | |
687629c2 AK |
3415 | |
3416 | retval = 0; | |
3417 | out: | |
687629c2 AK |
3418 | return retval; |
3419 | } | |
3420 | ||
e2962bfc AK |
3421 | /* spare/missing disks activations are not allowe when |
3422 | * array/container performs reshape operation, because | |
3423 | * all arrays in container works on the same disks set | |
3424 | */ | |
3425 | int imsm_reshape_blocks_arrays_changes(struct intel_super *super) | |
3426 | { | |
3427 | int rv = 0; | |
3428 | struct intel_dev *i_dev; | |
3429 | struct imsm_dev *dev; | |
3430 | ||
3431 | /* check whole container | |
3432 | */ | |
3433 | for (i_dev = super->devlist; i_dev; i_dev = i_dev->next) { | |
3434 | dev = i_dev->dev; | |
3ad25638 | 3435 | if (is_gen_migration(dev)) { |
e2962bfc AK |
3436 | /* No repair during any migration in container |
3437 | */ | |
3438 | rv = 1; | |
3439 | break; | |
3440 | } | |
3441 | } | |
3442 | return rv; | |
3443 | } | |
3e684231 | 3444 | static unsigned long long imsm_component_size_alignment_check(int level, |
c41e00b2 | 3445 | int chunk_size, |
f36a9ecd | 3446 | unsigned int sector_size, |
c41e00b2 AK |
3447 | unsigned long long component_size) |
3448 | { | |
3e684231 | 3449 | unsigned int component_size_alignment; |
c41e00b2 | 3450 | |
3e684231 | 3451 | /* check component size alignment |
c41e00b2 | 3452 | */ |
3e684231 | 3453 | component_size_alignment = component_size % (chunk_size/sector_size); |
c41e00b2 | 3454 | |
3e684231 | 3455 | dprintf("(Level: %i, chunk_size = %i, component_size = %llu), component_size_alignment = %u\n", |
c41e00b2 | 3456 | level, chunk_size, component_size, |
3e684231 | 3457 | component_size_alignment); |
c41e00b2 | 3458 | |
3e684231 MZ |
3459 | if (component_size_alignment && (level != 1) && (level != UnSet)) { |
3460 | dprintf("imsm: reported component size aligned from %llu ", | |
c41e00b2 | 3461 | component_size); |
3e684231 | 3462 | component_size -= component_size_alignment; |
1ade5cc1 | 3463 | dprintf_cont("to %llu (%i).\n", |
3e684231 | 3464 | component_size, component_size_alignment); |
c41e00b2 AK |
3465 | } |
3466 | ||
3467 | return component_size; | |
3468 | } | |
e2962bfc | 3469 | |
fbc42556 JR |
3470 | /******************************************************************************* |
3471 | * Function: get_bitmap_header_sector | |
3472 | * Description: Returns the sector where the bitmap header is placed. | |
3473 | * Parameters: | |
3474 | * st : supertype information | |
3475 | * dev_idx : index of the device with bitmap | |
3476 | * | |
3477 | * Returns: | |
3478 | * The sector where the bitmap header is placed | |
3479 | ******************************************************************************/ | |
3480 | static unsigned long long get_bitmap_header_sector(struct intel_super *super, | |
3481 | int dev_idx) | |
3482 | { | |
3483 | struct imsm_dev *dev = get_imsm_dev(super, dev_idx); | |
3484 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
3485 | ||
3486 | if (!super->sector_size) { | |
3487 | dprintf("sector size is not set\n"); | |
3488 | return 0; | |
3489 | } | |
3490 | ||
3491 | return pba_of_lba0(map) + calc_component_size(map, dev) + | |
3492 | (IMSM_BITMAP_HEADER_OFFSET / super->sector_size); | |
3493 | } | |
3494 | ||
3495 | /******************************************************************************* | |
3496 | * Function: get_bitmap_sector | |
3497 | * Description: Returns the sector where the bitmap is placed. | |
3498 | * Parameters: | |
3499 | * st : supertype information | |
3500 | * dev_idx : index of the device with bitmap | |
3501 | * | |
3502 | * Returns: | |
3503 | * The sector where the bitmap is placed | |
3504 | ******************************************************************************/ | |
3505 | static unsigned long long get_bitmap_sector(struct intel_super *super, | |
3506 | int dev_idx) | |
3507 | { | |
3508 | if (!super->sector_size) { | |
3509 | dprintf("sector size is not set\n"); | |
3510 | return 0; | |
3511 | } | |
3512 | ||
3513 | return get_bitmap_header_sector(super, dev_idx) + | |
3514 | (IMSM_BITMAP_HEADER_SIZE / super->sector_size); | |
3515 | } | |
3516 | ||
2432ce9b AP |
3517 | static unsigned long long get_ppl_sector(struct intel_super *super, int dev_idx) |
3518 | { | |
3519 | struct imsm_dev *dev = get_imsm_dev(super, dev_idx); | |
3520 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
3521 | ||
3522 | return pba_of_lba0(map) + | |
3523 | (num_data_stripes(map) * map->blocks_per_strip); | |
3524 | } | |
3525 | ||
a5d85af7 | 3526 | static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info, char *dmap) |
bf5a934a DW |
3527 | { |
3528 | struct intel_super *super = st->sb; | |
c47b0ff6 | 3529 | struct migr_record *migr_rec = super->migr_rec; |
949c47a0 | 3530 | struct imsm_dev *dev = get_imsm_dev(super, super->current_vol); |
238c0a71 AK |
3531 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
3532 | struct imsm_map *prev_map = get_imsm_map(dev, MAP_1); | |
b335e593 | 3533 | struct imsm_map *map_to_analyse = map; |
efb30e7f | 3534 | struct dl *dl; |
a5d85af7 | 3535 | int map_disks = info->array.raid_disks; |
bf5a934a | 3536 | |
95eeceeb | 3537 | memset(info, 0, sizeof(*info)); |
b335e593 AK |
3538 | if (prev_map) |
3539 | map_to_analyse = prev_map; | |
3540 | ||
ca0748fa | 3541 | dl = super->current_disk; |
9894ec0d | 3542 | |
bf5a934a | 3543 | info->container_member = super->current_vol; |
cd0430a1 | 3544 | info->array.raid_disks = map->num_members; |
b335e593 | 3545 | info->array.level = get_imsm_raid_level(map_to_analyse); |
bf5a934a DW |
3546 | info->array.layout = imsm_level_to_layout(info->array.level); |
3547 | info->array.md_minor = -1; | |
3548 | info->array.ctime = 0; | |
3549 | info->array.utime = 0; | |
b335e593 AK |
3550 | info->array.chunk_size = |
3551 | __le16_to_cpu(map_to_analyse->blocks_per_strip) << 9; | |
2432ce9b | 3552 | info->array.state = !(dev->vol.dirty & RAIDVOL_DIRTY); |
fcc2c9da | 3553 | info->custom_array_size = imsm_dev_size(dev); |
3ad25638 AK |
3554 | info->recovery_blocked = imsm_reshape_blocks_arrays_changes(st->sb); |
3555 | ||
3f510843 | 3556 | if (is_gen_migration(dev)) { |
195d1d76 PP |
3557 | /* |
3558 | * device prev_map should be added if it is in the middle | |
3559 | * of migration | |
3560 | */ | |
3561 | assert(prev_map); | |
3562 | ||
3f83228a | 3563 | info->reshape_active = 1; |
b335e593 AK |
3564 | info->new_level = get_imsm_raid_level(map); |
3565 | info->new_layout = imsm_level_to_layout(info->new_level); | |
3566 | info->new_chunk = __le16_to_cpu(map->blocks_per_strip) << 9; | |
3f83228a | 3567 | info->delta_disks = map->num_members - prev_map->num_members; |
493f5dd6 N |
3568 | if (info->delta_disks) { |
3569 | /* this needs to be applied to every array | |
3570 | * in the container. | |
3571 | */ | |
81219e70 | 3572 | info->reshape_active = CONTAINER_RESHAPE; |
493f5dd6 | 3573 | } |
3f83228a N |
3574 | /* We shape information that we give to md might have to be |
3575 | * modify to cope with md's requirement for reshaping arrays. | |
3576 | * For example, when reshaping a RAID0, md requires it to be | |
3577 | * presented as a degraded RAID4. | |
3578 | * Also if a RAID0 is migrating to a RAID5 we need to specify | |
3579 | * the array as already being RAID5, but the 'before' layout | |
3580 | * is a RAID4-like layout. | |
3581 | */ | |
3582 | switch (info->array.level) { | |
3583 | case 0: | |
3584 | switch(info->new_level) { | |
3585 | case 0: | |
3586 | /* conversion is happening as RAID4 */ | |
3587 | info->array.level = 4; | |
3588 | info->array.raid_disks += 1; | |
3589 | break; | |
3590 | case 5: | |
3591 | /* conversion is happening as RAID5 */ | |
3592 | info->array.level = 5; | |
3593 | info->array.layout = ALGORITHM_PARITY_N; | |
3f83228a N |
3594 | info->delta_disks -= 1; |
3595 | break; | |
3596 | default: | |
3597 | /* FIXME error message */ | |
3598 | info->array.level = UnSet; | |
3599 | break; | |
3600 | } | |
3601 | break; | |
3602 | } | |
b335e593 AK |
3603 | } else { |
3604 | info->new_level = UnSet; | |
3605 | info->new_layout = UnSet; | |
3606 | info->new_chunk = info->array.chunk_size; | |
3f83228a | 3607 | info->delta_disks = 0; |
b335e593 | 3608 | } |
ca0748fa | 3609 | |
efb30e7f DW |
3610 | if (dl) { |
3611 | info->disk.major = dl->major; | |
3612 | info->disk.minor = dl->minor; | |
ca0748fa | 3613 | info->disk.number = dl->index; |
656b6b5a N |
3614 | info->disk.raid_disk = get_imsm_disk_slot(map_to_analyse, |
3615 | dl->index); | |
efb30e7f | 3616 | } |
bf5a934a | 3617 | |
5551b113 | 3618 | info->data_offset = pba_of_lba0(map_to_analyse); |
44490938 | 3619 | info->component_size = calc_component_size(map, dev); |
3e684231 | 3620 | info->component_size = imsm_component_size_alignment_check( |
c41e00b2 AK |
3621 | info->array.level, |
3622 | info->array.chunk_size, | |
f36a9ecd | 3623 | super->sector_size, |
c41e00b2 | 3624 | info->component_size); |
5e46202e | 3625 | info->bb.supported = 1; |
139dae11 | 3626 | |
301406c9 | 3627 | memset(info->uuid, 0, sizeof(info->uuid)); |
921d9e16 | 3628 | info->recovery_start = MaxSector; |
bf5a934a | 3629 | |
c2462068 PB |
3630 | if (info->array.level == 5 && |
3631 | (dev->rwh_policy == RWH_DISTRIBUTED || | |
3632 | dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)) { | |
2432ce9b AP |
3633 | info->consistency_policy = CONSISTENCY_POLICY_PPL; |
3634 | info->ppl_sector = get_ppl_sector(super, super->current_vol); | |
c2462068 PB |
3635 | if (dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED) |
3636 | info->ppl_size = MULTIPLE_PPL_AREA_SIZE_IMSM >> 9; | |
3637 | else | |
3638 | info->ppl_size = (PPL_HEADER_SIZE + PPL_ENTRY_SPACE) | |
3639 | >> 9; | |
2432ce9b AP |
3640 | } else if (info->array.level <= 0) { |
3641 | info->consistency_policy = CONSISTENCY_POLICY_NONE; | |
3642 | } else { | |
fbc42556 JR |
3643 | if (dev->rwh_policy == RWH_BITMAP) { |
3644 | info->bitmap_offset = get_bitmap_sector(super, super->current_vol); | |
3645 | info->consistency_policy = CONSISTENCY_POLICY_BITMAP; | |
3646 | } else { | |
3647 | info->consistency_policy = CONSISTENCY_POLICY_RESYNC; | |
3648 | } | |
2432ce9b AP |
3649 | } |
3650 | ||
d2e6d5d6 | 3651 | info->reshape_progress = 0; |
b6796ce1 | 3652 | info->resync_start = MaxSector; |
b9172665 | 3653 | if ((map_to_analyse->map_state == IMSM_T_STATE_UNINITIALIZED || |
2432ce9b | 3654 | !(info->array.state & 1)) && |
b9172665 | 3655 | imsm_reshape_blocks_arrays_changes(super) == 0) { |
301406c9 | 3656 | info->resync_start = 0; |
b6796ce1 AK |
3657 | } |
3658 | if (dev->vol.migr_state) { | |
1e5c6983 DW |
3659 | switch (migr_type(dev)) { |
3660 | case MIGR_REPAIR: | |
3661 | case MIGR_INIT: { | |
c47b0ff6 AK |
3662 | __u64 blocks_per_unit = blocks_per_migr_unit(super, |
3663 | dev); | |
4036e7ee | 3664 | __u64 units = vol_curr_migr_unit(dev); |
1e5c6983 DW |
3665 | |
3666 | info->resync_start = blocks_per_unit * units; | |
3667 | break; | |
3668 | } | |
d2e6d5d6 | 3669 | case MIGR_GEN_MIGR: { |
c47b0ff6 AK |
3670 | __u64 blocks_per_unit = blocks_per_migr_unit(super, |
3671 | dev); | |
9f421827 | 3672 | __u64 units = current_migr_unit(migr_rec); |
04fa9523 | 3673 | int used_disks; |
d2e6d5d6 | 3674 | |
befb629b AK |
3675 | if (__le32_to_cpu(migr_rec->ascending_migr) && |
3676 | (units < | |
9f421827 | 3677 | (get_num_migr_units(migr_rec)-1)) && |
befb629b AK |
3678 | (super->migr_rec->rec_status == |
3679 | __cpu_to_le32(UNIT_SRC_IN_CP_AREA))) | |
3680 | units++; | |
3681 | ||
d2e6d5d6 | 3682 | info->reshape_progress = blocks_per_unit * units; |
6289d1e0 | 3683 | |
7a862a02 | 3684 | dprintf("IMSM: General Migration checkpoint : %llu (%llu) -> read reshape progress : %llu\n", |
19986c72 MB |
3685 | (unsigned long long)units, |
3686 | (unsigned long long)blocks_per_unit, | |
3687 | info->reshape_progress); | |
75156c46 | 3688 | |
9529d343 | 3689 | used_disks = imsm_num_data_members(prev_map); |
75156c46 | 3690 | if (used_disks > 0) { |
895ffd99 | 3691 | info->custom_array_size = per_dev_array_size(map) * |
75156c46 | 3692 | used_disks; |
75156c46 | 3693 | } |
d2e6d5d6 | 3694 | } |
1e5c6983 DW |
3695 | case MIGR_VERIFY: |
3696 | /* we could emulate the checkpointing of | |
3697 | * 'sync_action=check' migrations, but for now | |
3698 | * we just immediately complete them | |
3699 | */ | |
3700 | case MIGR_REBUILD: | |
3701 | /* this is handled by container_content_imsm() */ | |
1e5c6983 DW |
3702 | case MIGR_STATE_CHANGE: |
3703 | /* FIXME handle other migrations */ | |
3704 | default: | |
3705 | /* we are not dirty, so... */ | |
3706 | info->resync_start = MaxSector; | |
3707 | } | |
b6796ce1 | 3708 | } |
301406c9 DW |
3709 | |
3710 | strncpy(info->name, (char *) dev->volume, MAX_RAID_SERIAL_LEN); | |
3711 | info->name[MAX_RAID_SERIAL_LEN] = 0; | |
bf5a934a | 3712 | |
f35f2525 N |
3713 | info->array.major_version = -1; |
3714 | info->array.minor_version = -2; | |
4dd2df09 | 3715 | sprintf(info->text_version, "/%s/%d", st->container_devnm, info->container_member); |
a67dd8cc | 3716 | info->safe_mode_delay = 4000; /* 4 secs like the Matrix driver */ |
51006d85 | 3717 | uuid_from_super_imsm(st, info->uuid); |
a5d85af7 N |
3718 | |
3719 | if (dmap) { | |
3720 | int i, j; | |
3721 | for (i=0; i<map_disks; i++) { | |
3722 | dmap[i] = 0; | |
3723 | if (i < info->array.raid_disks) { | |
3724 | struct imsm_disk *dsk; | |
238c0a71 | 3725 | j = get_imsm_disk_idx(dev, i, MAP_X); |
a5d85af7 N |
3726 | dsk = get_imsm_disk(super, j); |
3727 | if (dsk && (dsk->status & CONFIGURED_DISK)) | |
3728 | dmap[i] = 1; | |
3729 | } | |
3730 | } | |
3731 | } | |
81ac8b4d | 3732 | } |
bf5a934a | 3733 | |
3b451610 AK |
3734 | static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev, |
3735 | int failed, int look_in_map); | |
3736 | ||
3737 | static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev, | |
3738 | int look_in_map); | |
3739 | ||
3740 | static void manage_second_map(struct intel_super *super, struct imsm_dev *dev) | |
3741 | { | |
3742 | if (is_gen_migration(dev)) { | |
3743 | int failed; | |
3744 | __u8 map_state; | |
3745 | struct imsm_map *map2 = get_imsm_map(dev, MAP_1); | |
3746 | ||
3747 | failed = imsm_count_failed(super, dev, MAP_1); | |
238c0a71 | 3748 | map_state = imsm_check_degraded(super, dev, failed, MAP_1); |
3b451610 AK |
3749 | if (map2->map_state != map_state) { |
3750 | map2->map_state = map_state; | |
3751 | super->updates_pending++; | |
3752 | } | |
3753 | } | |
3754 | } | |
97b4d0e9 DW |
3755 | |
3756 | static struct imsm_disk *get_imsm_missing(struct intel_super *super, __u8 index) | |
3757 | { | |
3758 | struct dl *d; | |
3759 | ||
3760 | for (d = super->missing; d; d = d->next) | |
3761 | if (d->index == index) | |
3762 | return &d->disk; | |
3763 | return NULL; | |
3764 | } | |
3765 | ||
a5d85af7 | 3766 | static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map) |
4f5bc454 DW |
3767 | { |
3768 | struct intel_super *super = st->sb; | |
4f5bc454 | 3769 | struct imsm_disk *disk; |
a5d85af7 | 3770 | int map_disks = info->array.raid_disks; |
476b00bd | 3771 | int max_enough = -1; |
ab3cb6b3 N |
3772 | int i; |
3773 | struct imsm_super *mpb; | |
4f5bc454 | 3774 | |
bf5a934a | 3775 | if (super->current_vol >= 0) { |
a5d85af7 | 3776 | getinfo_super_imsm_volume(st, info, map); |
bf5a934a DW |
3777 | return; |
3778 | } | |
95eeceeb | 3779 | memset(info, 0, sizeof(*info)); |
d23fe947 DW |
3780 | |
3781 | /* Set raid_disks to zero so that Assemble will always pull in valid | |
3782 | * spares | |
3783 | */ | |
3784 | info->array.raid_disks = 0; | |
cdddbdbc DW |
3785 | info->array.level = LEVEL_CONTAINER; |
3786 | info->array.layout = 0; | |
3787 | info->array.md_minor = -1; | |
1011e834 | 3788 | info->array.ctime = 0; /* N/A for imsm */ |
cdddbdbc DW |
3789 | info->array.utime = 0; |
3790 | info->array.chunk_size = 0; | |
3791 | ||
3792 | info->disk.major = 0; | |
3793 | info->disk.minor = 0; | |
cdddbdbc | 3794 | info->disk.raid_disk = -1; |
c2c087e6 | 3795 | info->reshape_active = 0; |
f35f2525 N |
3796 | info->array.major_version = -1; |
3797 | info->array.minor_version = -2; | |
c2c087e6 | 3798 | strcpy(info->text_version, "imsm"); |
a67dd8cc | 3799 | info->safe_mode_delay = 0; |
c2c087e6 DW |
3800 | info->disk.number = -1; |
3801 | info->disk.state = 0; | |
c5afc314 | 3802 | info->name[0] = 0; |
921d9e16 | 3803 | info->recovery_start = MaxSector; |
3ad25638 | 3804 | info->recovery_blocked = imsm_reshape_blocks_arrays_changes(st->sb); |
5e46202e | 3805 | info->bb.supported = 1; |
c2c087e6 | 3806 | |
97b4d0e9 | 3807 | /* do we have the all the insync disks that we expect? */ |
ab3cb6b3 | 3808 | mpb = super->anchor; |
b7d81a38 | 3809 | info->events = __le32_to_cpu(mpb->generation_num); |
97b4d0e9 | 3810 | |
ab3cb6b3 N |
3811 | for (i = 0; i < mpb->num_raid_devs; i++) { |
3812 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
476b00bd | 3813 | int failed, enough, j, missing = 0; |
ab3cb6b3 | 3814 | struct imsm_map *map; |
476b00bd | 3815 | __u8 state; |
97b4d0e9 | 3816 | |
476b00bd MT |
3817 | failed = imsm_count_failed(super, dev, MAP_0); |
3818 | state = imsm_check_degraded(super, dev, failed, MAP_0); | |
238c0a71 | 3819 | map = get_imsm_map(dev, MAP_0); |
ab3cb6b3 N |
3820 | |
3821 | /* any newly missing disks? | |
3822 | * (catches single-degraded vs double-degraded) | |
3823 | */ | |
3824 | for (j = 0; j < map->num_members; j++) { | |
238c0a71 | 3825 | __u32 ord = get_imsm_ord_tbl_ent(dev, j, MAP_0); |
ab3cb6b3 N |
3826 | __u32 idx = ord_to_idx(ord); |
3827 | ||
20dc76d1 MT |
3828 | if (super->disks && super->disks->index == (int)idx) |
3829 | info->disk.raid_disk = j; | |
3830 | ||
ab3cb6b3 N |
3831 | if (!(ord & IMSM_ORD_REBUILD) && |
3832 | get_imsm_missing(super, idx)) { | |
476b00bd | 3833 | missing = 1; |
ab3cb6b3 N |
3834 | break; |
3835 | } | |
97b4d0e9 | 3836 | } |
476b00bd MT |
3837 | |
3838 | if (state == IMSM_T_STATE_FAILED) | |
3839 | enough = -1; | |
3840 | else if (state == IMSM_T_STATE_DEGRADED && | |
3841 | (state != map->map_state || missing)) | |
3842 | enough = 0; | |
3843 | else /* we're normal, or already degraded */ | |
3844 | enough = 1; | |
3845 | if (is_gen_migration(dev) && missing) { | |
3846 | /* during general migration we need all disks | |
3847 | * that process is running on. | |
3848 | * No new missing disk is allowed. | |
3849 | */ | |
3850 | max_enough = -1; | |
3851 | enough = -1; | |
3852 | /* no more checks necessary | |
3853 | */ | |
3854 | break; | |
3855 | } | |
3856 | /* in the missing/failed disk case check to see | |
3857 | * if at least one array is runnable | |
3858 | */ | |
3859 | max_enough = max(max_enough, enough); | |
ab3cb6b3 | 3860 | } |
97b4d0e9 | 3861 | |
476b00bd MT |
3862 | info->container_enough = max_enough; |
3863 | ||
4a04ec6c | 3864 | if (super->disks) { |
14e8215b DW |
3865 | __u32 reserved = imsm_reserved_sectors(super, super->disks); |
3866 | ||
b9f594fe | 3867 | disk = &super->disks->disk; |
5551b113 | 3868 | info->data_offset = total_blocks(&super->disks->disk) - reserved; |
14e8215b | 3869 | info->component_size = reserved; |
25ed7e59 | 3870 | info->disk.state = is_configured(disk) ? (1 << MD_DISK_ACTIVE) : 0; |
df474657 DW |
3871 | /* we don't change info->disk.raid_disk here because |
3872 | * this state will be finalized in mdmon after we have | |
3873 | * found the 'most fresh' version of the metadata | |
3874 | */ | |
25ed7e59 | 3875 | info->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0; |
2432ce9b AP |
3876 | info->disk.state |= (is_spare(disk) || is_journal(disk)) ? |
3877 | 0 : (1 << MD_DISK_SYNC); | |
cdddbdbc | 3878 | } |
a575e2a7 DW |
3879 | |
3880 | /* only call uuid_from_super_imsm when this disk is part of a populated container, | |
3881 | * ->compare_super may have updated the 'num_raid_devs' field for spares | |
3882 | */ | |
3883 | if (info->disk.state & (1 << MD_DISK_SYNC) || super->anchor->num_raid_devs) | |
36ba7d48 | 3884 | uuid_from_super_imsm(st, info->uuid); |
22e263f6 AC |
3885 | else |
3886 | memcpy(info->uuid, uuid_zero, sizeof(uuid_zero)); | |
a5d85af7 N |
3887 | |
3888 | /* I don't know how to compute 'map' on imsm, so use safe default */ | |
3889 | if (map) { | |
3890 | int i; | |
3891 | for (i = 0; i < map_disks; i++) | |
3892 | map[i] = 1; | |
3893 | } | |
3894 | ||
cdddbdbc DW |
3895 | } |
3896 | ||
5c4cd5da AC |
3897 | /* allocates memory and fills disk in mdinfo structure |
3898 | * for each disk in array */ | |
3899 | struct mdinfo *getinfo_super_disks_imsm(struct supertype *st) | |
3900 | { | |
594dc1b8 | 3901 | struct mdinfo *mddev; |
5c4cd5da AC |
3902 | struct intel_super *super = st->sb; |
3903 | struct imsm_disk *disk; | |
3904 | int count = 0; | |
3905 | struct dl *dl; | |
3906 | if (!super || !super->disks) | |
3907 | return NULL; | |
3908 | dl = super->disks; | |
503975b9 | 3909 | mddev = xcalloc(1, sizeof(*mddev)); |
5c4cd5da AC |
3910 | while (dl) { |
3911 | struct mdinfo *tmp; | |
3912 | disk = &dl->disk; | |
503975b9 | 3913 | tmp = xcalloc(1, sizeof(*tmp)); |
5c4cd5da AC |
3914 | if (mddev->devs) |
3915 | tmp->next = mddev->devs; | |
3916 | mddev->devs = tmp; | |
3917 | tmp->disk.number = count++; | |
3918 | tmp->disk.major = dl->major; | |
3919 | tmp->disk.minor = dl->minor; | |
3920 | tmp->disk.state = is_configured(disk) ? | |
3921 | (1 << MD_DISK_ACTIVE) : 0; | |
3922 | tmp->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0; | |
3923 | tmp->disk.state |= is_spare(disk) ? 0 : (1 << MD_DISK_SYNC); | |
3924 | tmp->disk.raid_disk = -1; | |
3925 | dl = dl->next; | |
3926 | } | |
3927 | return mddev; | |
3928 | } | |
3929 | ||
cdddbdbc | 3930 | static int update_super_imsm(struct supertype *st, struct mdinfo *info, |
03312b52 MK |
3931 | enum update_opt update, char *devname, |
3932 | int verbose, int uuid_set, char *homehost) | |
cdddbdbc | 3933 | { |
f352c545 DW |
3934 | /* For 'assemble' and 'force' we need to return non-zero if any |
3935 | * change was made. For others, the return value is ignored. | |
3936 | * Update options are: | |
3937 | * force-one : This device looks a bit old but needs to be included, | |
3938 | * update age info appropriately. | |
3939 | * assemble: clear any 'faulty' flag to allow this device to | |
3940 | * be assembled. | |
3941 | * force-array: Array is degraded but being forced, mark it clean | |
3942 | * if that will be needed to assemble it. | |
3943 | * | |
3944 | * newdev: not used ???? | |
3945 | * grow: Array has gained a new device - this is currently for | |
3946 | * linear only | |
3947 | * resync: mark as dirty so a resync will happen. | |
3948 | * name: update the name - preserving the homehost | |
6e46bf34 | 3949 | * uuid: Change the uuid of the array to match watch is given |
f352c545 DW |
3950 | * |
3951 | * Following are not relevant for this imsm: | |
3952 | * sparc2.2 : update from old dodgey metadata | |
3953 | * super-minor: change the preferred_minor number | |
3954 | * summaries: update redundant counters. | |
f352c545 DW |
3955 | * homehost: update the recorded homehost |
3956 | * _reshape_progress: record new reshape_progress position. | |
3957 | */ | |
6e46bf34 DW |
3958 | int rv = 1; |
3959 | struct intel_super *super = st->sb; | |
3960 | struct imsm_super *mpb; | |
f352c545 | 3961 | |
6e46bf34 DW |
3962 | /* we can only update container info */ |
3963 | if (!super || super->current_vol >= 0 || !super->anchor) | |
3964 | return 1; | |
3965 | ||
3966 | mpb = super->anchor; | |
3967 | ||
03312b52 | 3968 | switch (update) { |
4345e135 | 3969 | case UOPT_UUID: |
81a5b4f5 N |
3970 | /* We take this to mean that the family_num should be updated. |
3971 | * However that is much smaller than the uuid so we cannot really | |
3972 | * allow an explicit uuid to be given. And it is hard to reliably | |
3973 | * know if one was. | |
3974 | * So if !uuid_set we know the current uuid is random and just used | |
3975 | * the first 'int' and copy it to the other 3 positions. | |
3976 | * Otherwise we require the 4 'int's to be the same as would be the | |
3977 | * case if we are using a random uuid. So an explicit uuid will be | |
3978 | * accepted as long as all for ints are the same... which shouldn't hurt | |
6e46bf34 | 3979 | */ |
81a5b4f5 N |
3980 | if (!uuid_set) { |
3981 | info->uuid[1] = info->uuid[2] = info->uuid[3] = info->uuid[0]; | |
6e46bf34 | 3982 | rv = 0; |
81a5b4f5 N |
3983 | } else { |
3984 | if (info->uuid[0] != info->uuid[1] || | |
3985 | info->uuid[1] != info->uuid[2] || | |
3986 | info->uuid[2] != info->uuid[3]) | |
3987 | rv = -1; | |
3988 | else | |
3989 | rv = 0; | |
6e46bf34 | 3990 | } |
81a5b4f5 N |
3991 | if (rv == 0) |
3992 | mpb->orig_family_num = info->uuid[0]; | |
4345e135 MK |
3993 | break; |
3994 | case UOPT_SPEC_ASSEMBLE: | |
6e46bf34 | 3995 | rv = 0; |
4345e135 MK |
3996 | break; |
3997 | default: | |
1e2b2765 | 3998 | rv = -1; |
4345e135 MK |
3999 | break; |
4000 | } | |
f352c545 | 4001 | |
6e46bf34 DW |
4002 | /* successful update? recompute checksum */ |
4003 | if (rv == 0) | |
4004 | mpb->check_sum = __le32_to_cpu(__gen_imsm_checksum(mpb)); | |
f352c545 DW |
4005 | |
4006 | return rv; | |
cdddbdbc DW |
4007 | } |
4008 | ||
c2c087e6 | 4009 | static size_t disks_to_mpb_size(int disks) |
cdddbdbc | 4010 | { |
c2c087e6 | 4011 | size_t size; |
cdddbdbc | 4012 | |
c2c087e6 DW |
4013 | size = sizeof(struct imsm_super); |
4014 | size += (disks - 1) * sizeof(struct imsm_disk); | |
4015 | size += 2 * sizeof(struct imsm_dev); | |
4016 | /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */ | |
4017 | size += (4 - 2) * sizeof(struct imsm_map); | |
4018 | /* 4 possible disk_ord_tbl's */ | |
4019 | size += 4 * (disks - 1) * sizeof(__u32); | |
bbab0940 TM |
4020 | /* maximum bbm log */ |
4021 | size += sizeof(struct bbm_log); | |
c2c087e6 DW |
4022 | |
4023 | return size; | |
4024 | } | |
4025 | ||
387fcd59 N |
4026 | static __u64 avail_size_imsm(struct supertype *st, __u64 devsize, |
4027 | unsigned long long data_offset) | |
c2c087e6 DW |
4028 | { |
4029 | if (devsize < (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS)) | |
4030 | return 0; | |
4031 | ||
4032 | return devsize - (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS); | |
cdddbdbc DW |
4033 | } |
4034 | ||
ba2de7ba DW |
4035 | static void free_devlist(struct intel_super *super) |
4036 | { | |
4037 | struct intel_dev *dv; | |
4038 | ||
4039 | while (super->devlist) { | |
4040 | dv = super->devlist->next; | |
4041 | free(super->devlist->dev); | |
4042 | free(super->devlist); | |
4043 | super->devlist = dv; | |
4044 | } | |
4045 | } | |
4046 | ||
4047 | static void imsm_copy_dev(struct imsm_dev *dest, struct imsm_dev *src) | |
4048 | { | |
4049 | memcpy(dest, src, sizeof_imsm_dev(src, 0)); | |
4050 | } | |
4051 | ||
c7b8547c MT |
4052 | static int compare_super_imsm(struct supertype *st, struct supertype *tst, |
4053 | int verbose) | |
cdddbdbc | 4054 | { |
601ffa78 | 4055 | /* return: |
cdddbdbc | 4056 | * 0 same, or first was empty, and second was copied |
601ffa78 | 4057 | * 1 sb are different |
cdddbdbc DW |
4058 | */ |
4059 | struct intel_super *first = st->sb; | |
4060 | struct intel_super *sec = tst->sb; | |
4061 | ||
5d500228 N |
4062 | if (!first) { |
4063 | st->sb = tst->sb; | |
4064 | tst->sb = NULL; | |
4065 | return 0; | |
4066 | } | |
601ffa78 | 4067 | |
8603ea6f LM |
4068 | /* in platform dependent environment test if the disks |
4069 | * use the same Intel hba | |
601ffa78 OS |
4070 | * if not on Intel hba at all, allow anything. |
4071 | * doesn't check HBAs if num_raid_devs is not set, as it means | |
4072 | * it is a free floating spare, and all spares regardless of HBA type | |
4073 | * will fall into separate container during the assembly | |
8603ea6f | 4074 | */ |
601ffa78 | 4075 | if (first->hba && sec->hba && first->anchor->num_raid_devs != 0) { |
6b781d33 | 4076 | if (first->hba->type != sec->hba->type) { |
c7b8547c MT |
4077 | if (verbose) |
4078 | pr_err("HBAs of devices do not match %s != %s\n", | |
4079 | get_sys_dev_type(first->hba->type), | |
4080 | get_sys_dev_type(sec->hba->type)); | |
601ffa78 | 4081 | return 1; |
6b781d33 AP |
4082 | } |
4083 | if (first->orom != sec->orom) { | |
c7b8547c MT |
4084 | if (verbose) |
4085 | pr_err("HBAs of devices do not match %s != %s\n", | |
4086 | first->hba->pci_id, sec->hba->pci_id); | |
601ffa78 | 4087 | return 1; |
8603ea6f LM |
4088 | } |
4089 | } | |
cdddbdbc | 4090 | |
d23fe947 DW |
4091 | if (first->anchor->num_raid_devs > 0 && |
4092 | sec->anchor->num_raid_devs > 0) { | |
a2b97981 DW |
4093 | /* Determine if these disks might ever have been |
4094 | * related. Further disambiguation can only take place | |
4095 | * in load_super_imsm_all | |
4096 | */ | |
4097 | __u32 first_family = first->anchor->orig_family_num; | |
4098 | __u32 sec_family = sec->anchor->orig_family_num; | |
4099 | ||
f796af5d DW |
4100 | if (memcmp(first->anchor->sig, sec->anchor->sig, |
4101 | MAX_SIGNATURE_LENGTH) != 0) | |
601ffa78 | 4102 | return 1; |
f796af5d | 4103 | |
a2b97981 DW |
4104 | if (first_family == 0) |
4105 | first_family = first->anchor->family_num; | |
4106 | if (sec_family == 0) | |
4107 | sec_family = sec->anchor->family_num; | |
4108 | ||
4109 | if (first_family != sec_family) | |
601ffa78 | 4110 | return 1; |
f796af5d | 4111 | |
d23fe947 | 4112 | } |
cdddbdbc | 4113 | |
601ffa78 OS |
4114 | /* if an anchor does not have num_raid_devs set then it is a free |
4115 | * floating spare. don't assosiate spare with any array, as during assembly | |
4116 | * spares shall fall into separate container, from which they can be moved | |
4117 | * when necessary | |
4118 | */ | |
4119 | if (first->anchor->num_raid_devs ^ sec->anchor->num_raid_devs) | |
4120 | return 1; | |
3e372e5a | 4121 | |
cdddbdbc DW |
4122 | return 0; |
4123 | } | |
4124 | ||
0030e8d6 DW |
4125 | static void fd2devname(int fd, char *name) |
4126 | { | |
0030e8d6 | 4127 | char *nm; |
0030e8d6 | 4128 | |
7c798f87 MT |
4129 | nm = fd2kname(fd); |
4130 | if (!nm) | |
0030e8d6 | 4131 | return; |
9587c373 | 4132 | |
7c798f87 | 4133 | snprintf(name, MAX_RAID_SERIAL_LEN, "/dev/%s", nm); |
0030e8d6 DW |
4134 | } |
4135 | ||
21e9380b AP |
4136 | static int nvme_get_serial(int fd, void *buf, size_t buf_len) |
4137 | { | |
fcebeb77 | 4138 | char path[PATH_MAX]; |
21e9380b AP |
4139 | char *name = fd2kname(fd); |
4140 | ||
4141 | if (!name) | |
4142 | return 1; | |
4143 | ||
4144 | if (strncmp(name, "nvme", 4) != 0) | |
4145 | return 1; | |
4146 | ||
fcebeb77 MT |
4147 | if (!diskfd_to_devpath(fd, 1, path)) |
4148 | return 1; | |
21e9380b | 4149 | |
fcebeb77 | 4150 | return devpath_to_char(path, "serial", buf, buf_len, 0); |
21e9380b AP |
4151 | } |
4152 | ||
cdddbdbc DW |
4153 | extern int scsi_get_serial(int fd, void *buf, size_t buf_len); |
4154 | ||
4155 | static int imsm_read_serial(int fd, char *devname, | |
6da53c0e | 4156 | __u8 *serial, size_t serial_buf_len) |
cdddbdbc | 4157 | { |
21e9380b | 4158 | char buf[50]; |
cdddbdbc | 4159 | int rv; |
6da53c0e | 4160 | size_t len; |
316e2bf4 DW |
4161 | char *dest; |
4162 | char *src; | |
21e9380b AP |
4163 | unsigned int i; |
4164 | ||
4165 | memset(buf, 0, sizeof(buf)); | |
cdddbdbc | 4166 | |
d1cd231a KT |
4167 | if (check_env("IMSM_DEVNAME_AS_SERIAL")) { |
4168 | memset(serial, 0, serial_buf_len); | |
4169 | fd2devname(fd, (char *) serial); | |
4170 | return 0; | |
4171 | } | |
4172 | ||
21e9380b | 4173 | rv = nvme_get_serial(fd, buf, sizeof(buf)); |
cdddbdbc | 4174 | |
21e9380b AP |
4175 | if (rv) |
4176 | rv = scsi_get_serial(fd, buf, sizeof(buf)); | |
f9ba0ff1 | 4177 | |
cdddbdbc DW |
4178 | if (rv != 0) { |
4179 | if (devname) | |
e7b84f9d N |
4180 | pr_err("Failed to retrieve serial for %s\n", |
4181 | devname); | |
cdddbdbc DW |
4182 | return rv; |
4183 | } | |
4184 | ||
316e2bf4 DW |
4185 | /* trim all whitespace and non-printable characters and convert |
4186 | * ':' to ';' | |
4187 | */ | |
21e9380b AP |
4188 | for (i = 0, dest = buf; i < sizeof(buf) && buf[i]; i++) { |
4189 | src = &buf[i]; | |
316e2bf4 DW |
4190 | if (*src > 0x20) { |
4191 | /* ':' is reserved for use in placeholder serial | |
4192 | * numbers for missing disks | |
4193 | */ | |
4194 | if (*src == ':') | |
4195 | *dest++ = ';'; | |
4196 | else | |
4197 | *dest++ = *src; | |
4198 | } | |
4199 | } | |
21e9380b AP |
4200 | len = dest - buf; |
4201 | dest = buf; | |
316e2bf4 | 4202 | |
6da53c0e BK |
4203 | if (len > serial_buf_len) { |
4204 | /* truncate leading characters */ | |
4205 | dest += len - serial_buf_len; | |
4206 | len = serial_buf_len; | |
316e2bf4 | 4207 | } |
5c3db629 | 4208 | |
6da53c0e | 4209 | memset(serial, 0, serial_buf_len); |
316e2bf4 | 4210 | memcpy(serial, dest, len); |
cdddbdbc DW |
4211 | |
4212 | return 0; | |
4213 | } | |
4214 | ||
1f24f035 DW |
4215 | static int serialcmp(__u8 *s1, __u8 *s2) |
4216 | { | |
4217 | return strncmp((char *) s1, (char *) s2, MAX_RAID_SERIAL_LEN); | |
4218 | } | |
4219 | ||
4220 | static void serialcpy(__u8 *dest, __u8 *src) | |
4221 | { | |
4222 | strncpy((char *) dest, (char *) src, MAX_RAID_SERIAL_LEN); | |
4223 | } | |
4224 | ||
54c2c1ea DW |
4225 | static struct dl *serial_to_dl(__u8 *serial, struct intel_super *super) |
4226 | { | |
4227 | struct dl *dl; | |
4228 | ||
4229 | for (dl = super->disks; dl; dl = dl->next) | |
4230 | if (serialcmp(dl->serial, serial) == 0) | |
4231 | break; | |
4232 | ||
4233 | return dl; | |
4234 | } | |
4235 | ||
a2b97981 DW |
4236 | static struct imsm_disk * |
4237 | __serial_to_disk(__u8 *serial, struct imsm_super *mpb, int *idx) | |
4238 | { | |
4239 | int i; | |
4240 | ||
4241 | for (i = 0; i < mpb->num_disks; i++) { | |
4242 | struct imsm_disk *disk = __get_imsm_disk(mpb, i); | |
4243 | ||
4244 | if (serialcmp(disk->serial, serial) == 0) { | |
4245 | if (idx) | |
4246 | *idx = i; | |
4247 | return disk; | |
4248 | } | |
4249 | } | |
4250 | ||
4251 | return NULL; | |
4252 | } | |
4253 | ||
cdddbdbc DW |
4254 | static int |
4255 | load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd) | |
4256 | { | |
a2b97981 | 4257 | struct imsm_disk *disk; |
cdddbdbc DW |
4258 | struct dl *dl; |
4259 | struct stat stb; | |
cdddbdbc | 4260 | int rv; |
a2b97981 | 4261 | char name[40]; |
d23fe947 DW |
4262 | __u8 serial[MAX_RAID_SERIAL_LEN]; |
4263 | ||
6da53c0e | 4264 | rv = imsm_read_serial(fd, devname, serial, MAX_RAID_SERIAL_LEN); |
d23fe947 DW |
4265 | |
4266 | if (rv != 0) | |
4267 | return 2; | |
4268 | ||
503975b9 | 4269 | dl = xcalloc(1, sizeof(*dl)); |
cdddbdbc | 4270 | |
a2b97981 DW |
4271 | fstat(fd, &stb); |
4272 | dl->major = major(stb.st_rdev); | |
4273 | dl->minor = minor(stb.st_rdev); | |
4274 | dl->next = super->disks; | |
4275 | dl->fd = keep_fd ? fd : -1; | |
4276 | assert(super->disks == NULL); | |
4277 | super->disks = dl; | |
4278 | serialcpy(dl->serial, serial); | |
4279 | dl->index = -2; | |
4280 | dl->e = NULL; | |
4281 | fd2devname(fd, name); | |
4282 | if (devname) | |
503975b9 | 4283 | dl->devname = xstrdup(devname); |
a2b97981 | 4284 | else |
503975b9 | 4285 | dl->devname = xstrdup(name); |
cdddbdbc | 4286 | |
d23fe947 | 4287 | /* look up this disk's index in the current anchor */ |
a2b97981 DW |
4288 | disk = __serial_to_disk(dl->serial, super->anchor, &dl->index); |
4289 | if (disk) { | |
4290 | dl->disk = *disk; | |
4291 | /* only set index on disks that are a member of a | |
4292 | * populated contianer, i.e. one with raid_devs | |
4293 | */ | |
4294 | if (is_failed(&dl->disk)) | |
3f6efecc | 4295 | dl->index = -2; |
2432ce9b | 4296 | else if (is_spare(&dl->disk) || is_journal(&dl->disk)) |
a2b97981 | 4297 | dl->index = -1; |
3f6efecc DW |
4298 | } |
4299 | ||
949c47a0 DW |
4300 | return 0; |
4301 | } | |
4302 | ||
0c046afd DW |
4303 | /* When migrating map0 contains the 'destination' state while map1 |
4304 | * contains the current state. When not migrating map0 contains the | |
4305 | * current state. This routine assumes that map[0].map_state is set to | |
4306 | * the current array state before being called. | |
4307 | * | |
4308 | * Migration is indicated by one of the following states | |
4309 | * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed) | |
e3bba0e0 | 4310 | * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal |
0c046afd | 4311 | * map1state=unitialized) |
1484e727 | 4312 | * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal |
0c046afd | 4313 | * map1state=normal) |
e3bba0e0 | 4314 | * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal |
0c046afd | 4315 | * map1state=degraded) |
8e59f3d8 AK |
4316 | * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal |
4317 | * map1state=normal) | |
0c046afd | 4318 | */ |
8e59f3d8 AK |
4319 | static void migrate(struct imsm_dev *dev, struct intel_super *super, |
4320 | __u8 to_state, int migr_type) | |
3393c6af | 4321 | { |
0c046afd | 4322 | struct imsm_map *dest; |
238c0a71 | 4323 | struct imsm_map *src = get_imsm_map(dev, MAP_0); |
3393c6af | 4324 | |
0c046afd | 4325 | dev->vol.migr_state = 1; |
1484e727 | 4326 | set_migr_type(dev, migr_type); |
4036e7ee | 4327 | set_vol_curr_migr_unit(dev, 0); |
238c0a71 | 4328 | dest = get_imsm_map(dev, MAP_1); |
0c046afd | 4329 | |
0556e1a2 | 4330 | /* duplicate and then set the target end state in map[0] */ |
3393c6af | 4331 | memcpy(dest, src, sizeof_imsm_map(src)); |
fb12a745 | 4332 | if (migr_type == MIGR_GEN_MIGR) { |
0556e1a2 DW |
4333 | __u32 ord; |
4334 | int i; | |
4335 | ||
4336 | for (i = 0; i < src->num_members; i++) { | |
4337 | ord = __le32_to_cpu(src->disk_ord_tbl[i]); | |
4338 | set_imsm_ord_tbl_ent(src, i, ord_to_idx(ord)); | |
4339 | } | |
4340 | } | |
4341 | ||
8e59f3d8 AK |
4342 | if (migr_type == MIGR_GEN_MIGR) |
4343 | /* Clear migration record */ | |
4344 | memset(super->migr_rec, 0, sizeof(struct migr_record)); | |
4345 | ||
0c046afd | 4346 | src->map_state = to_state; |
949c47a0 | 4347 | } |
f8f603f1 | 4348 | |
809da78e AK |
4349 | static void end_migration(struct imsm_dev *dev, struct intel_super *super, |
4350 | __u8 map_state) | |
f8f603f1 | 4351 | { |
238c0a71 AK |
4352 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
4353 | struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state == 0 ? | |
4354 | MAP_0 : MAP_1); | |
28bce06f | 4355 | int i, j; |
0556e1a2 DW |
4356 | |
4357 | /* merge any IMSM_ORD_REBUILD bits that were not successfully | |
4358 | * completed in the last migration. | |
4359 | * | |
28bce06f | 4360 | * FIXME add support for raid-level-migration |
0556e1a2 | 4361 | */ |
195d1d76 | 4362 | if (map_state != map->map_state && (is_gen_migration(dev) == false) && |
089f9d79 | 4363 | prev->map_state != IMSM_T_STATE_UNINITIALIZED) { |
809da78e AK |
4364 | /* when final map state is other than expected |
4365 | * merge maps (not for migration) | |
4366 | */ | |
4367 | int failed; | |
4368 | ||
4369 | for (i = 0; i < prev->num_members; i++) | |
4370 | for (j = 0; j < map->num_members; j++) | |
4371 | /* during online capacity expansion | |
4372 | * disks position can be changed | |
4373 | * if takeover is used | |
4374 | */ | |
4375 | if (ord_to_idx(map->disk_ord_tbl[j]) == | |
4376 | ord_to_idx(prev->disk_ord_tbl[i])) { | |
4377 | map->disk_ord_tbl[j] |= | |
4378 | prev->disk_ord_tbl[i]; | |
4379 | break; | |
4380 | } | |
4381 | failed = imsm_count_failed(super, dev, MAP_0); | |
4382 | map_state = imsm_check_degraded(super, dev, failed, MAP_0); | |
4383 | } | |
f8f603f1 DW |
4384 | |
4385 | dev->vol.migr_state = 0; | |
ea672ee1 | 4386 | set_migr_type(dev, 0); |
4036e7ee | 4387 | set_vol_curr_migr_unit(dev, 0); |
f8f603f1 DW |
4388 | map->map_state = map_state; |
4389 | } | |
949c47a0 DW |
4390 | |
4391 | static int parse_raid_devices(struct intel_super *super) | |
4392 | { | |
4393 | int i; | |
4394 | struct imsm_dev *dev_new; | |
4d7b1503 | 4395 | size_t len, len_migr; |
401d313b | 4396 | size_t max_len = 0; |
4d7b1503 DW |
4397 | size_t space_needed = 0; |
4398 | struct imsm_super *mpb = super->anchor; | |
949c47a0 DW |
4399 | |
4400 | for (i = 0; i < super->anchor->num_raid_devs; i++) { | |
4401 | struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i); | |
ba2de7ba | 4402 | struct intel_dev *dv; |
949c47a0 | 4403 | |
4d7b1503 DW |
4404 | len = sizeof_imsm_dev(dev_iter, 0); |
4405 | len_migr = sizeof_imsm_dev(dev_iter, 1); | |
4406 | if (len_migr > len) | |
4407 | space_needed += len_migr - len; | |
ca9de185 | 4408 | |
503975b9 | 4409 | dv = xmalloc(sizeof(*dv)); |
401d313b AK |
4410 | if (max_len < len_migr) |
4411 | max_len = len_migr; | |
4412 | if (max_len > len_migr) | |
4413 | space_needed += max_len - len_migr; | |
503975b9 | 4414 | dev_new = xmalloc(max_len); |
949c47a0 | 4415 | imsm_copy_dev(dev_new, dev_iter); |
ba2de7ba DW |
4416 | dv->dev = dev_new; |
4417 | dv->index = i; | |
4418 | dv->next = super->devlist; | |
4419 | super->devlist = dv; | |
949c47a0 | 4420 | } |
cdddbdbc | 4421 | |
4d7b1503 DW |
4422 | /* ensure that super->buf is large enough when all raid devices |
4423 | * are migrating | |
4424 | */ | |
4425 | if (__le32_to_cpu(mpb->mpb_size) + space_needed > super->len) { | |
4426 | void *buf; | |
4427 | ||
f36a9ecd PB |
4428 | len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + space_needed, |
4429 | super->sector_size); | |
4430 | if (posix_memalign(&buf, MAX_SECTOR_SIZE, len) != 0) | |
4d7b1503 DW |
4431 | return 1; |
4432 | ||
1f45a8ad DW |
4433 | memcpy(buf, super->buf, super->len); |
4434 | memset(buf + super->len, 0, len - super->len); | |
4d7b1503 DW |
4435 | free(super->buf); |
4436 | super->buf = buf; | |
4437 | super->len = len; | |
4438 | } | |
ca9de185 | 4439 | |
bbab0940 TM |
4440 | super->extra_space += space_needed; |
4441 | ||
cdddbdbc DW |
4442 | return 0; |
4443 | } | |
4444 | ||
e2f41b2c AK |
4445 | /******************************************************************************* |
4446 | * Function: check_mpb_migr_compatibility | |
4447 | * Description: Function checks for unsupported migration features: | |
4448 | * - migration optimization area (pba_of_lba0) | |
4449 | * - descending reshape (ascending_migr) | |
4450 | * Parameters: | |
4451 | * super : imsm metadata information | |
4452 | * Returns: | |
4453 | * 0 : migration is compatible | |
4454 | * -1 : migration is not compatible | |
4455 | ******************************************************************************/ | |
4456 | int check_mpb_migr_compatibility(struct intel_super *super) | |
4457 | { | |
4458 | struct imsm_map *map0, *map1; | |
4459 | struct migr_record *migr_rec = super->migr_rec; | |
4460 | int i; | |
4461 | ||
4462 | for (i = 0; i < super->anchor->num_raid_devs; i++) { | |
4463 | struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i); | |
4464 | ||
756a15f3 | 4465 | if (dev_iter->vol.migr_state == 1 && |
e2f41b2c AK |
4466 | dev_iter->vol.migr_type == MIGR_GEN_MIGR) { |
4467 | /* This device is migrating */ | |
238c0a71 AK |
4468 | map0 = get_imsm_map(dev_iter, MAP_0); |
4469 | map1 = get_imsm_map(dev_iter, MAP_1); | |
5551b113 | 4470 | if (pba_of_lba0(map0) != pba_of_lba0(map1)) |
e2f41b2c AK |
4471 | /* migration optimization area was used */ |
4472 | return -1; | |
fc54fe7a JS |
4473 | if (migr_rec->ascending_migr == 0 && |
4474 | migr_rec->dest_depth_per_unit > 0) | |
e2f41b2c AK |
4475 | /* descending reshape not supported yet */ |
4476 | return -1; | |
4477 | } | |
4478 | } | |
4479 | return 0; | |
4480 | } | |
4481 | ||
d23fe947 | 4482 | static void __free_imsm(struct intel_super *super, int free_disks); |
9ca2c81c | 4483 | |
cdddbdbc | 4484 | /* load_imsm_mpb - read matrix metadata |
f2f5c343 | 4485 | * allocates super->mpb to be freed by free_imsm |
cdddbdbc DW |
4486 | */ |
4487 | static int load_imsm_mpb(int fd, struct intel_super *super, char *devname) | |
4488 | { | |
4489 | unsigned long long dsize; | |
cdddbdbc | 4490 | unsigned long long sectors; |
f36a9ecd | 4491 | unsigned int sector_size = super->sector_size; |
cdddbdbc | 4492 | struct stat; |
6416d527 | 4493 | struct imsm_super *anchor; |
cdddbdbc DW |
4494 | __u32 check_sum; |
4495 | ||
cdddbdbc | 4496 | get_dev_size(fd, NULL, &dsize); |
f36a9ecd | 4497 | if (dsize < 2*sector_size) { |
64436f06 | 4498 | if (devname) |
e7b84f9d N |
4499 | pr_err("%s: device to small for imsm\n", |
4500 | devname); | |
64436f06 N |
4501 | return 1; |
4502 | } | |
cdddbdbc | 4503 | |
f36a9ecd | 4504 | if (lseek64(fd, dsize - (sector_size * 2), SEEK_SET) < 0) { |
cdddbdbc | 4505 | if (devname) |
e7b84f9d N |
4506 | pr_err("Cannot seek to anchor block on %s: %s\n", |
4507 | devname, strerror(errno)); | |
cdddbdbc DW |
4508 | return 1; |
4509 | } | |
4510 | ||
f36a9ecd | 4511 | if (posix_memalign((void **)&anchor, sector_size, sector_size) != 0) { |
ad97895e | 4512 | if (devname) |
7a862a02 | 4513 | pr_err("Failed to allocate imsm anchor buffer on %s\n", devname); |
ad97895e DW |
4514 | return 1; |
4515 | } | |
466070ad | 4516 | if ((unsigned int)read(fd, anchor, sector_size) != sector_size) { |
cdddbdbc | 4517 | if (devname) |
e7b84f9d N |
4518 | pr_err("Cannot read anchor block on %s: %s\n", |
4519 | devname, strerror(errno)); | |
6416d527 | 4520 | free(anchor); |
cdddbdbc DW |
4521 | return 1; |
4522 | } | |
4523 | ||
6416d527 | 4524 | if (strncmp((char *) anchor->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) { |
cdddbdbc | 4525 | if (devname) |
e7b84f9d | 4526 | pr_err("no IMSM anchor on %s\n", devname); |
6416d527 | 4527 | free(anchor); |
cdddbdbc DW |
4528 | return 2; |
4529 | } | |
4530 | ||
d23fe947 | 4531 | __free_imsm(super, 0); |
f2f5c343 LM |
4532 | /* reload capability and hba */ |
4533 | ||
4534 | /* capability and hba must be updated with new super allocation */ | |
d424212e | 4535 | find_intel_hba_capability(fd, super, devname); |
f36a9ecd PB |
4536 | super->len = ROUND_UP(anchor->mpb_size, sector_size); |
4537 | if (posix_memalign(&super->buf, MAX_SECTOR_SIZE, super->len) != 0) { | |
cdddbdbc | 4538 | if (devname) |
e7b84f9d N |
4539 | pr_err("unable to allocate %zu byte mpb buffer\n", |
4540 | super->len); | |
6416d527 | 4541 | free(anchor); |
cdddbdbc DW |
4542 | return 2; |
4543 | } | |
f36a9ecd | 4544 | memcpy(super->buf, anchor, sector_size); |
cdddbdbc | 4545 | |
f36a9ecd | 4546 | sectors = mpb_sectors(anchor, sector_size) - 1; |
6416d527 | 4547 | free(anchor); |
8e59f3d8 | 4548 | |
85337573 AO |
4549 | if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE, |
4550 | MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE) != 0) { | |
1ade5cc1 | 4551 | pr_err("could not allocate migr_rec buffer\n"); |
8e59f3d8 | 4552 | free(super->buf); |
50cd06b4 | 4553 | super->buf = NULL; |
8e59f3d8 AK |
4554 | return 2; |
4555 | } | |
51d83f5d | 4556 | super->clean_migration_record_by_mdmon = 0; |
8e59f3d8 | 4557 | |
949c47a0 | 4558 | if (!sectors) { |
ecf45690 DW |
4559 | check_sum = __gen_imsm_checksum(super->anchor); |
4560 | if (check_sum != __le32_to_cpu(super->anchor->check_sum)) { | |
4561 | if (devname) | |
e7b84f9d N |
4562 | pr_err("IMSM checksum %x != %x on %s\n", |
4563 | check_sum, | |
4564 | __le32_to_cpu(super->anchor->check_sum), | |
4565 | devname); | |
ecf45690 DW |
4566 | return 2; |
4567 | } | |
4568 | ||
a2b97981 | 4569 | return 0; |
949c47a0 | 4570 | } |
cdddbdbc DW |
4571 | |
4572 | /* read the extended mpb */ | |
f36a9ecd | 4573 | if (lseek64(fd, dsize - (sector_size * (2 + sectors)), SEEK_SET) < 0) { |
cdddbdbc | 4574 | if (devname) |
e7b84f9d N |
4575 | pr_err("Cannot seek to extended mpb on %s: %s\n", |
4576 | devname, strerror(errno)); | |
cdddbdbc DW |
4577 | return 1; |
4578 | } | |
4579 | ||
f36a9ecd PB |
4580 | if ((unsigned int)read(fd, super->buf + sector_size, |
4581 | super->len - sector_size) != super->len - sector_size) { | |
cdddbdbc | 4582 | if (devname) |
e7b84f9d N |
4583 | pr_err("Cannot read extended mpb on %s: %s\n", |
4584 | devname, strerror(errno)); | |
cdddbdbc DW |
4585 | return 2; |
4586 | } | |
4587 | ||
949c47a0 DW |
4588 | check_sum = __gen_imsm_checksum(super->anchor); |
4589 | if (check_sum != __le32_to_cpu(super->anchor->check_sum)) { | |
cdddbdbc | 4590 | if (devname) |
e7b84f9d N |
4591 | pr_err("IMSM checksum %x != %x on %s\n", |
4592 | check_sum, __le32_to_cpu(super->anchor->check_sum), | |
4593 | devname); | |
db575f3b | 4594 | return 3; |
cdddbdbc DW |
4595 | } |
4596 | ||
a2b97981 DW |
4597 | return 0; |
4598 | } | |
4599 | ||
8e59f3d8 AK |
4600 | static int read_imsm_migr_rec(int fd, struct intel_super *super); |
4601 | ||
97f81ee2 CA |
4602 | /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */ |
4603 | static void clear_hi(struct intel_super *super) | |
4604 | { | |
4605 | struct imsm_super *mpb = super->anchor; | |
4606 | int i, n; | |
4607 | if (mpb->attributes & MPB_ATTRIB_2TB_DISK) | |
4608 | return; | |
4609 | for (i = 0; i < mpb->num_disks; ++i) { | |
4610 | struct imsm_disk *disk = &mpb->disk[i]; | |
4611 | disk->total_blocks_hi = 0; | |
4612 | } | |
4613 | for (i = 0; i < mpb->num_raid_devs; ++i) { | |
4614 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
97f81ee2 CA |
4615 | for (n = 0; n < 2; ++n) { |
4616 | struct imsm_map *map = get_imsm_map(dev, n); | |
4617 | if (!map) | |
4618 | continue; | |
4619 | map->pba_of_lba0_hi = 0; | |
4620 | map->blocks_per_member_hi = 0; | |
4621 | map->num_data_stripes_hi = 0; | |
4622 | } | |
4623 | } | |
4624 | } | |
4625 | ||
a2b97981 DW |
4626 | static int |
4627 | load_and_parse_mpb(int fd, struct intel_super *super, char *devname, int keep_fd) | |
4628 | { | |
4629 | int err; | |
4630 | ||
4631 | err = load_imsm_mpb(fd, super, devname); | |
4632 | if (err) | |
4633 | return err; | |
f36a9ecd PB |
4634 | if (super->sector_size == 4096) |
4635 | convert_from_4k(super); | |
a2b97981 DW |
4636 | err = load_imsm_disk(fd, super, devname, keep_fd); |
4637 | if (err) | |
4638 | return err; | |
4639 | err = parse_raid_devices(super); | |
8d67477f TM |
4640 | if (err) |
4641 | return err; | |
4642 | err = load_bbm_log(super); | |
97f81ee2 | 4643 | clear_hi(super); |
a2b97981 | 4644 | return err; |
cdddbdbc DW |
4645 | } |
4646 | ||
4389ce73 | 4647 | static void __free_imsm_disk(struct dl *d, int do_close) |
ae6aad82 | 4648 | { |
4389ce73 MT |
4649 | if (do_close) |
4650 | close_fd(&d->fd); | |
ae6aad82 DW |
4651 | if (d->devname) |
4652 | free(d->devname); | |
0dcecb2e DW |
4653 | if (d->e) |
4654 | free(d->e); | |
ae6aad82 DW |
4655 | free(d); |
4656 | ||
4657 | } | |
1a64be56 | 4658 | |
cdddbdbc DW |
4659 | static void free_imsm_disks(struct intel_super *super) |
4660 | { | |
47ee5a45 | 4661 | struct dl *d; |
cdddbdbc | 4662 | |
47ee5a45 DW |
4663 | while (super->disks) { |
4664 | d = super->disks; | |
cdddbdbc | 4665 | super->disks = d->next; |
3a85bf0e | 4666 | __free_imsm_disk(d, 1); |
cdddbdbc | 4667 | } |
cb82edca AK |
4668 | while (super->disk_mgmt_list) { |
4669 | d = super->disk_mgmt_list; | |
4670 | super->disk_mgmt_list = d->next; | |
3a85bf0e | 4671 | __free_imsm_disk(d, 1); |
cb82edca | 4672 | } |
47ee5a45 DW |
4673 | while (super->missing) { |
4674 | d = super->missing; | |
4675 | super->missing = d->next; | |
3a85bf0e | 4676 | __free_imsm_disk(d, 1); |
47ee5a45 DW |
4677 | } |
4678 | ||
cdddbdbc DW |
4679 | } |
4680 | ||
9ca2c81c | 4681 | /* free all the pieces hanging off of a super pointer */ |
d23fe947 | 4682 | static void __free_imsm(struct intel_super *super, int free_disks) |
cdddbdbc | 4683 | { |
88654014 LM |
4684 | struct intel_hba *elem, *next; |
4685 | ||
9ca2c81c | 4686 | if (super->buf) { |
949c47a0 | 4687 | free(super->buf); |
9ca2c81c DW |
4688 | super->buf = NULL; |
4689 | } | |
f2f5c343 LM |
4690 | /* unlink capability description */ |
4691 | super->orom = NULL; | |
8e59f3d8 AK |
4692 | if (super->migr_rec_buf) { |
4693 | free(super->migr_rec_buf); | |
4694 | super->migr_rec_buf = NULL; | |
4695 | } | |
d23fe947 DW |
4696 | if (free_disks) |
4697 | free_imsm_disks(super); | |
ba2de7ba | 4698 | free_devlist(super); |
88654014 LM |
4699 | elem = super->hba; |
4700 | while (elem) { | |
4701 | if (elem->path) | |
4702 | free((void *)elem->path); | |
4703 | next = elem->next; | |
4704 | free(elem); | |
4705 | elem = next; | |
88c32bb1 | 4706 | } |
8d67477f TM |
4707 | if (super->bbm_log) |
4708 | free(super->bbm_log); | |
88654014 | 4709 | super->hba = NULL; |
cdddbdbc DW |
4710 | } |
4711 | ||
9ca2c81c DW |
4712 | static void free_imsm(struct intel_super *super) |
4713 | { | |
d23fe947 | 4714 | __free_imsm(super, 1); |
928f1424 | 4715 | free(super->bb.entries); |
9ca2c81c DW |
4716 | free(super); |
4717 | } | |
cdddbdbc DW |
4718 | |
4719 | static void free_super_imsm(struct supertype *st) | |
4720 | { | |
4721 | struct intel_super *super = st->sb; | |
4722 | ||
4723 | if (!super) | |
4724 | return; | |
4725 | ||
4726 | free_imsm(super); | |
4727 | st->sb = NULL; | |
4728 | } | |
4729 | ||
49133e57 | 4730 | static struct intel_super *alloc_super(void) |
c2c087e6 | 4731 | { |
503975b9 | 4732 | struct intel_super *super = xcalloc(1, sizeof(*super)); |
c2c087e6 | 4733 | |
503975b9 N |
4734 | super->current_vol = -1; |
4735 | super->create_offset = ~((unsigned long long) 0); | |
928f1424 TM |
4736 | |
4737 | super->bb.entries = xmalloc(BBM_LOG_MAX_ENTRIES * | |
4738 | sizeof(struct md_bb_entry)); | |
4739 | if (!super->bb.entries) { | |
4740 | free(super); | |
4741 | return NULL; | |
4742 | } | |
4743 | ||
c2c087e6 DW |
4744 | return super; |
4745 | } | |
4746 | ||
f0f5a016 LM |
4747 | /* |
4748 | * find and allocate hba and OROM/EFI based on valid fd of RAID component device | |
4749 | */ | |
d424212e | 4750 | static int find_intel_hba_capability(int fd, struct intel_super *super, char *devname) |
f0f5a016 LM |
4751 | { |
4752 | struct sys_dev *hba_name; | |
4753 | int rv = 0; | |
4754 | ||
4389ce73 | 4755 | if (is_fd_valid(fd) && test_partition(fd)) { |
3a30e28e MT |
4756 | pr_err("imsm: %s is a partition, cannot be used in IMSM\n", |
4757 | devname); | |
4758 | return 1; | |
4759 | } | |
420dafcd | 4760 | if (!is_fd_valid(fd) || check_no_platform()) { |
f2f5c343 | 4761 | super->orom = NULL; |
f0f5a016 LM |
4762 | super->hba = NULL; |
4763 | return 0; | |
4764 | } | |
4765 | hba_name = find_disk_attached_hba(fd, NULL); | |
4766 | if (!hba_name) { | |
d424212e | 4767 | if (devname) |
e7b84f9d N |
4768 | pr_err("%s is not attached to Intel(R) RAID controller.\n", |
4769 | devname); | |
f0f5a016 LM |
4770 | return 1; |
4771 | } | |
4772 | rv = attach_hba_to_super(super, hba_name); | |
4773 | if (rv == 2) { | |
d424212e N |
4774 | if (devname) { |
4775 | struct intel_hba *hba = super->hba; | |
f0f5a016 | 4776 | |
60f0f54d PB |
4777 | pr_err("%s is attached to Intel(R) %s %s (%s),\n" |
4778 | " but the container is assigned to Intel(R) %s %s (", | |
d424212e | 4779 | devname, |
614902f6 | 4780 | get_sys_dev_type(hba_name->type), |
75350d87 KF |
4781 | hba_name->type == SYS_DEV_VMD || hba_name->type == SYS_DEV_SATA_VMD ? |
4782 | "domain" : "RAID controller", | |
f0f5a016 | 4783 | hba_name->pci_id ? : "Err!", |
60f0f54d | 4784 | get_sys_dev_type(super->hba->type), |
75350d87 KF |
4785 | hba->type == SYS_DEV_VMD || hba_name->type == SYS_DEV_SATA_VMD ? |
4786 | "domain" : "RAID controller"); | |
f0f5a016 | 4787 | |
f0f5a016 LM |
4788 | while (hba) { |
4789 | fprintf(stderr, "%s", hba->pci_id ? : "Err!"); | |
4790 | if (hba->next) | |
4791 | fprintf(stderr, ", "); | |
4792 | hba = hba->next; | |
4793 | } | |
6b781d33 | 4794 | fprintf(stderr, ").\n" |
cca67208 | 4795 | " Mixing devices attached to different controllers is not allowed.\n"); |
f0f5a016 | 4796 | } |
f0f5a016 LM |
4797 | return 2; |
4798 | } | |
6b781d33 | 4799 | super->orom = find_imsm_capability(hba_name); |
f2f5c343 LM |
4800 | if (!super->orom) |
4801 | return 3; | |
614902f6 | 4802 | |
f0f5a016 LM |
4803 | return 0; |
4804 | } | |
4805 | ||
47ee5a45 DW |
4806 | /* find_missing - helper routine for load_super_imsm_all that identifies |
4807 | * disks that have disappeared from the system. This routine relies on | |
4808 | * the mpb being uptodate, which it is at load time. | |
4809 | */ | |
4810 | static int find_missing(struct intel_super *super) | |
4811 | { | |
4812 | int i; | |
4813 | struct imsm_super *mpb = super->anchor; | |
4814 | struct dl *dl; | |
4815 | struct imsm_disk *disk; | |
47ee5a45 DW |
4816 | |
4817 | for (i = 0; i < mpb->num_disks; i++) { | |
4818 | disk = __get_imsm_disk(mpb, i); | |
54c2c1ea | 4819 | dl = serial_to_dl(disk->serial, super); |
47ee5a45 DW |
4820 | if (dl) |
4821 | continue; | |
47ee5a45 | 4822 | |
503975b9 | 4823 | dl = xmalloc(sizeof(*dl)); |
47ee5a45 DW |
4824 | dl->major = 0; |
4825 | dl->minor = 0; | |
4826 | dl->fd = -1; | |
503975b9 | 4827 | dl->devname = xstrdup("missing"); |
47ee5a45 DW |
4828 | dl->index = i; |
4829 | serialcpy(dl->serial, disk->serial); | |
4830 | dl->disk = *disk; | |
689c9bf3 | 4831 | dl->e = NULL; |
47ee5a45 DW |
4832 | dl->next = super->missing; |
4833 | super->missing = dl; | |
4834 | } | |
4835 | ||
4836 | return 0; | |
4837 | } | |
4838 | ||
a2b97981 DW |
4839 | static struct intel_disk *disk_list_get(__u8 *serial, struct intel_disk *disk_list) |
4840 | { | |
4841 | struct intel_disk *idisk = disk_list; | |
4842 | ||
4843 | while (idisk) { | |
4844 | if (serialcmp(idisk->disk.serial, serial) == 0) | |
4845 | break; | |
4846 | idisk = idisk->next; | |
4847 | } | |
4848 | ||
4849 | return idisk; | |
4850 | } | |
4851 | ||
4852 | static int __prep_thunderdome(struct intel_super **table, int tbl_size, | |
4853 | struct intel_super *super, | |
4854 | struct intel_disk **disk_list) | |
4855 | { | |
4856 | struct imsm_disk *d = &super->disks->disk; | |
4857 | struct imsm_super *mpb = super->anchor; | |
4858 | int i, j; | |
4859 | ||
4860 | for (i = 0; i < tbl_size; i++) { | |
4861 | struct imsm_super *tbl_mpb = table[i]->anchor; | |
4862 | struct imsm_disk *tbl_d = &table[i]->disks->disk; | |
4863 | ||
4864 | if (tbl_mpb->family_num == mpb->family_num) { | |
4865 | if (tbl_mpb->check_sum == mpb->check_sum) { | |
1ade5cc1 N |
4866 | dprintf("mpb from %d:%d matches %d:%d\n", |
4867 | super->disks->major, | |
a2b97981 DW |
4868 | super->disks->minor, |
4869 | table[i]->disks->major, | |
4870 | table[i]->disks->minor); | |
4871 | break; | |
4872 | } | |
4873 | ||
4874 | if (((is_configured(d) && !is_configured(tbl_d)) || | |
4875 | is_configured(d) == is_configured(tbl_d)) && | |
4876 | tbl_mpb->generation_num < mpb->generation_num) { | |
4877 | /* current version of the mpb is a | |
4878 | * better candidate than the one in | |
4879 | * super_table, but copy over "cross | |
4880 | * generational" status | |
4881 | */ | |
4882 | struct intel_disk *idisk; | |
4883 | ||
1ade5cc1 N |
4884 | dprintf("mpb from %d:%d replaces %d:%d\n", |
4885 | super->disks->major, | |
a2b97981 DW |
4886 | super->disks->minor, |
4887 | table[i]->disks->major, | |
4888 | table[i]->disks->minor); | |
4889 | ||
4890 | idisk = disk_list_get(tbl_d->serial, *disk_list); | |
4891 | if (idisk && is_failed(&idisk->disk)) | |
4892 | tbl_d->status |= FAILED_DISK; | |
4893 | break; | |
4894 | } else { | |
4895 | struct intel_disk *idisk; | |
4896 | struct imsm_disk *disk; | |
4897 | ||
4898 | /* tbl_mpb is more up to date, but copy | |
4899 | * over cross generational status before | |
4900 | * returning | |
4901 | */ | |
4902 | disk = __serial_to_disk(d->serial, mpb, NULL); | |
4903 | if (disk && is_failed(disk)) | |
4904 | d->status |= FAILED_DISK; | |
4905 | ||
4906 | idisk = disk_list_get(d->serial, *disk_list); | |
4907 | if (idisk) { | |
4908 | idisk->owner = i; | |
4909 | if (disk && is_configured(disk)) | |
4910 | idisk->disk.status |= CONFIGURED_DISK; | |
4911 | } | |
4912 | ||
1ade5cc1 N |
4913 | dprintf("mpb from %d:%d prefer %d:%d\n", |
4914 | super->disks->major, | |
a2b97981 DW |
4915 | super->disks->minor, |
4916 | table[i]->disks->major, | |
4917 | table[i]->disks->minor); | |
4918 | ||
4919 | return tbl_size; | |
4920 | } | |
4921 | } | |
4922 | } | |
4923 | ||
4924 | if (i >= tbl_size) | |
4925 | table[tbl_size++] = super; | |
4926 | else | |
4927 | table[i] = super; | |
4928 | ||
4929 | /* update/extend the merged list of imsm_disk records */ | |
4930 | for (j = 0; j < mpb->num_disks; j++) { | |
4931 | struct imsm_disk *disk = __get_imsm_disk(mpb, j); | |
4932 | struct intel_disk *idisk; | |
4933 | ||
4934 | idisk = disk_list_get(disk->serial, *disk_list); | |
4935 | if (idisk) { | |
4936 | idisk->disk.status |= disk->status; | |
4937 | if (is_configured(&idisk->disk) || | |
4938 | is_failed(&idisk->disk)) | |
4939 | idisk->disk.status &= ~(SPARE_DISK); | |
4940 | } else { | |
503975b9 | 4941 | idisk = xcalloc(1, sizeof(*idisk)); |
a2b97981 DW |
4942 | idisk->owner = IMSM_UNKNOWN_OWNER; |
4943 | idisk->disk = *disk; | |
4944 | idisk->next = *disk_list; | |
4945 | *disk_list = idisk; | |
4946 | } | |
4947 | ||
4948 | if (serialcmp(idisk->disk.serial, d->serial) == 0) | |
4949 | idisk->owner = i; | |
4950 | } | |
4951 | ||
4952 | return tbl_size; | |
4953 | } | |
4954 | ||
4955 | static struct intel_super * | |
4956 | validate_members(struct intel_super *super, struct intel_disk *disk_list, | |
4957 | const int owner) | |
4958 | { | |
4959 | struct imsm_super *mpb = super->anchor; | |
4960 | int ok_count = 0; | |
4961 | int i; | |
4962 | ||
4963 | for (i = 0; i < mpb->num_disks; i++) { | |
4964 | struct imsm_disk *disk = __get_imsm_disk(mpb, i); | |
4965 | struct intel_disk *idisk; | |
4966 | ||
4967 | idisk = disk_list_get(disk->serial, disk_list); | |
4968 | if (idisk) { | |
4969 | if (idisk->owner == owner || | |
4970 | idisk->owner == IMSM_UNKNOWN_OWNER) | |
4971 | ok_count++; | |
4972 | else | |
1ade5cc1 N |
4973 | dprintf("'%.16s' owner %d != %d\n", |
4974 | disk->serial, idisk->owner, | |
a2b97981 DW |
4975 | owner); |
4976 | } else { | |
1ade5cc1 N |
4977 | dprintf("unknown disk %x [%d]: %.16s\n", |
4978 | __le32_to_cpu(mpb->family_num), i, | |
a2b97981 DW |
4979 | disk->serial); |
4980 | break; | |
4981 | } | |
4982 | } | |
4983 | ||
4984 | if (ok_count == mpb->num_disks) | |
4985 | return super; | |
4986 | return NULL; | |
4987 | } | |
4988 | ||
4989 | static void show_conflicts(__u32 family_num, struct intel_super *super_list) | |
4990 | { | |
4991 | struct intel_super *s; | |
4992 | ||
4993 | for (s = super_list; s; s = s->next) { | |
4994 | if (family_num != s->anchor->family_num) | |
4995 | continue; | |
e12b3daa | 4996 | pr_err("Conflict, offlining family %#x on '%s'\n", |
a2b97981 DW |
4997 | __le32_to_cpu(family_num), s->disks->devname); |
4998 | } | |
4999 | } | |
5000 | ||
5001 | static struct intel_super * | |
5002 | imsm_thunderdome(struct intel_super **super_list, int len) | |
5003 | { | |
5004 | struct intel_super *super_table[len]; | |
5005 | struct intel_disk *disk_list = NULL; | |
5006 | struct intel_super *champion, *spare; | |
5007 | struct intel_super *s, **del; | |
5008 | int tbl_size = 0; | |
5009 | int conflict; | |
5010 | int i; | |
5011 | ||
5012 | memset(super_table, 0, sizeof(super_table)); | |
5013 | for (s = *super_list; s; s = s->next) | |
5014 | tbl_size = __prep_thunderdome(super_table, tbl_size, s, &disk_list); | |
5015 | ||
5016 | for (i = 0; i < tbl_size; i++) { | |
5017 | struct imsm_disk *d; | |
5018 | struct intel_disk *idisk; | |
5019 | struct imsm_super *mpb = super_table[i]->anchor; | |
5020 | ||
5021 | s = super_table[i]; | |
5022 | d = &s->disks->disk; | |
5023 | ||
5024 | /* 'd' must appear in merged disk list for its | |
5025 | * configuration to be valid | |
5026 | */ | |
5027 | idisk = disk_list_get(d->serial, disk_list); | |
5028 | if (idisk && idisk->owner == i) | |
5029 | s = validate_members(s, disk_list, i); | |
5030 | else | |
5031 | s = NULL; | |
5032 | ||
5033 | if (!s) | |
1ade5cc1 N |
5034 | dprintf("marking family: %#x from %d:%d offline\n", |
5035 | mpb->family_num, | |
a2b97981 DW |
5036 | super_table[i]->disks->major, |
5037 | super_table[i]->disks->minor); | |
5038 | super_table[i] = s; | |
5039 | } | |
5040 | ||
5041 | /* This is where the mdadm implementation differs from the Windows | |
5042 | * driver which has no strict concept of a container. We can only | |
5043 | * assemble one family from a container, so when returning a prodigal | |
5044 | * array member to this system the code will not be able to disambiguate | |
5045 | * the container contents that should be assembled ("foreign" versus | |
5046 | * "local"). It requires user intervention to set the orig_family_num | |
5047 | * to a new value to establish a new container. The Windows driver in | |
5048 | * this situation fixes up the volume name in place and manages the | |
5049 | * foreign array as an independent entity. | |
5050 | */ | |
5051 | s = NULL; | |
5052 | spare = NULL; | |
5053 | conflict = 0; | |
5054 | for (i = 0; i < tbl_size; i++) { | |
5055 | struct intel_super *tbl_ent = super_table[i]; | |
5056 | int is_spare = 0; | |
5057 | ||
5058 | if (!tbl_ent) | |
5059 | continue; | |
5060 | ||
5061 | if (tbl_ent->anchor->num_raid_devs == 0) { | |
5062 | spare = tbl_ent; | |
5063 | is_spare = 1; | |
5064 | } | |
5065 | ||
5066 | if (s && !is_spare) { | |
5067 | show_conflicts(tbl_ent->anchor->family_num, *super_list); | |
5068 | conflict++; | |
5069 | } else if (!s && !is_spare) | |
5070 | s = tbl_ent; | |
5071 | } | |
5072 | ||
5073 | if (!s) | |
5074 | s = spare; | |
5075 | if (!s) { | |
5076 | champion = NULL; | |
5077 | goto out; | |
5078 | } | |
5079 | champion = s; | |
5080 | ||
5081 | if (conflict) | |
7a862a02 | 5082 | pr_err("Chose family %#x on '%s', assemble conflicts to new container with '--update=uuid'\n", |
a2b97981 DW |
5083 | __le32_to_cpu(s->anchor->family_num), s->disks->devname); |
5084 | ||
5085 | /* collect all dl's onto 'champion', and update them to | |
5086 | * champion's version of the status | |
5087 | */ | |
5088 | for (s = *super_list; s; s = s->next) { | |
5089 | struct imsm_super *mpb = champion->anchor; | |
5090 | struct dl *dl = s->disks; | |
5091 | ||
5092 | if (s == champion) | |
5093 | continue; | |
5094 | ||
5d7b407a CA |
5095 | mpb->attributes |= s->anchor->attributes & MPB_ATTRIB_2TB_DISK; |
5096 | ||
a2b97981 DW |
5097 | for (i = 0; i < mpb->num_disks; i++) { |
5098 | struct imsm_disk *disk; | |
5099 | ||
5100 | disk = __serial_to_disk(dl->serial, mpb, &dl->index); | |
5101 | if (disk) { | |
5102 | dl->disk = *disk; | |
5103 | /* only set index on disks that are a member of | |
5104 | * a populated contianer, i.e. one with | |
5105 | * raid_devs | |
5106 | */ | |
5107 | if (is_failed(&dl->disk)) | |
5108 | dl->index = -2; | |
5109 | else if (is_spare(&dl->disk)) | |
5110 | dl->index = -1; | |
5111 | break; | |
5112 | } | |
5113 | } | |
5114 | ||
5115 | if (i >= mpb->num_disks) { | |
5116 | struct intel_disk *idisk; | |
5117 | ||
5118 | idisk = disk_list_get(dl->serial, disk_list); | |
ecf408e9 | 5119 | if (idisk && is_spare(&idisk->disk) && |
a2b97981 DW |
5120 | !is_failed(&idisk->disk) && !is_configured(&idisk->disk)) |
5121 | dl->index = -1; | |
5122 | else { | |
5123 | dl->index = -2; | |
5124 | continue; | |
5125 | } | |
5126 | } | |
5127 | ||
5128 | dl->next = champion->disks; | |
5129 | champion->disks = dl; | |
5130 | s->disks = NULL; | |
5131 | } | |
5132 | ||
5133 | /* delete 'champion' from super_list */ | |
5134 | for (del = super_list; *del; ) { | |
5135 | if (*del == champion) { | |
5136 | *del = (*del)->next; | |
5137 | break; | |
5138 | } else | |
5139 | del = &(*del)->next; | |
5140 | } | |
5141 | champion->next = NULL; | |
5142 | ||
5143 | out: | |
5144 | while (disk_list) { | |
5145 | struct intel_disk *idisk = disk_list; | |
5146 | ||
5147 | disk_list = disk_list->next; | |
5148 | free(idisk); | |
5149 | } | |
5150 | ||
5151 | return champion; | |
5152 | } | |
5153 | ||
9587c373 LM |
5154 | static int |
5155 | get_sra_super_block(int fd, struct intel_super **super_list, char *devname, int *max, int keep_fd); | |
4dd2df09 | 5156 | static int get_super_block(struct intel_super **super_list, char *devnm, char *devname, |
9587c373 | 5157 | int major, int minor, int keep_fd); |
ec50f7b6 LM |
5158 | static int |
5159 | get_devlist_super_block(struct md_list *devlist, struct intel_super **super_list, | |
5160 | int *max, int keep_fd); | |
5161 | ||
cdddbdbc | 5162 | static int load_super_imsm_all(struct supertype *st, int fd, void **sbp, |
ec50f7b6 LM |
5163 | char *devname, struct md_list *devlist, |
5164 | int keep_fd) | |
cdddbdbc | 5165 | { |
a2b97981 DW |
5166 | struct intel_super *super_list = NULL; |
5167 | struct intel_super *super = NULL; | |
a2b97981 | 5168 | int err = 0; |
9587c373 | 5169 | int i = 0; |
dab4a513 | 5170 | |
4389ce73 | 5171 | if (is_fd_valid(fd)) |
9587c373 LM |
5172 | /* 'fd' is an opened container */ |
5173 | err = get_sra_super_block(fd, &super_list, devname, &i, keep_fd); | |
5174 | else | |
ec50f7b6 LM |
5175 | /* get super block from devlist devices */ |
5176 | err = get_devlist_super_block(devlist, &super_list, &i, keep_fd); | |
9587c373 | 5177 | if (err) |
1602d52c | 5178 | goto error; |
a2b97981 DW |
5179 | /* all mpbs enter, maybe one leaves */ |
5180 | super = imsm_thunderdome(&super_list, i); | |
5181 | if (!super) { | |
5182 | err = 1; | |
5183 | goto error; | |
cdddbdbc DW |
5184 | } |
5185 | ||
47ee5a45 DW |
5186 | if (find_missing(super) != 0) { |
5187 | free_imsm(super); | |
a2b97981 DW |
5188 | err = 2; |
5189 | goto error; | |
47ee5a45 | 5190 | } |
8e59f3d8 AK |
5191 | |
5192 | /* load migration record */ | |
2f86fda3 | 5193 | err = load_imsm_migr_rec(super); |
4c965cc9 AK |
5194 | if (err == -1) { |
5195 | /* migration is in progress, | |
5196 | * but migr_rec cannot be loaded, | |
5197 | */ | |
8e59f3d8 AK |
5198 | err = 4; |
5199 | goto error; | |
5200 | } | |
e2f41b2c AK |
5201 | |
5202 | /* Check migration compatibility */ | |
089f9d79 | 5203 | if (err == 0 && check_mpb_migr_compatibility(super) != 0) { |
e7b84f9d | 5204 | pr_err("Unsupported migration detected"); |
e2f41b2c AK |
5205 | if (devname) |
5206 | fprintf(stderr, " on %s\n", devname); | |
5207 | else | |
5208 | fprintf(stderr, " (IMSM).\n"); | |
5209 | ||
5210 | err = 5; | |
5211 | goto error; | |
5212 | } | |
5213 | ||
a2b97981 DW |
5214 | err = 0; |
5215 | ||
5216 | error: | |
5217 | while (super_list) { | |
5218 | struct intel_super *s = super_list; | |
5219 | ||
5220 | super_list = super_list->next; | |
5221 | free_imsm(s); | |
5222 | } | |
9587c373 | 5223 | |
a2b97981 DW |
5224 | if (err) |
5225 | return err; | |
f7e7067b | 5226 | |
cdddbdbc | 5227 | *sbp = super; |
4389ce73 | 5228 | if (is_fd_valid(fd)) |
4dd2df09 | 5229 | strcpy(st->container_devnm, fd2devnm(fd)); |
9587c373 | 5230 | else |
4dd2df09 | 5231 | st->container_devnm[0] = 0; |
a2b97981 | 5232 | if (err == 0 && st->ss == NULL) { |
bf5a934a | 5233 | st->ss = &super_imsm; |
cdddbdbc DW |
5234 | st->minor_version = 0; |
5235 | st->max_devs = IMSM_MAX_DEVICES; | |
5236 | } | |
cdddbdbc DW |
5237 | return 0; |
5238 | } | |
2b959fbf | 5239 | |
ec50f7b6 LM |
5240 | static int |
5241 | get_devlist_super_block(struct md_list *devlist, struct intel_super **super_list, | |
5242 | int *max, int keep_fd) | |
5243 | { | |
5244 | struct md_list *tmpdev; | |
5245 | int err = 0; | |
5246 | int i = 0; | |
9587c373 | 5247 | |
ec50f7b6 LM |
5248 | for (i = 0, tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) { |
5249 | if (tmpdev->used != 1) | |
5250 | continue; | |
5251 | if (tmpdev->container == 1) { | |
ca9de185 | 5252 | int lmax = 0; |
ec50f7b6 | 5253 | int fd = dev_open(tmpdev->devname, O_RDONLY|O_EXCL); |
4389ce73 | 5254 | if (!is_fd_valid(fd)) { |
e7b84f9d | 5255 | pr_err("cannot open device %s: %s\n", |
ec50f7b6 LM |
5256 | tmpdev->devname, strerror(errno)); |
5257 | err = 8; | |
5258 | goto error; | |
5259 | } | |
5260 | err = get_sra_super_block(fd, super_list, | |
5261 | tmpdev->devname, &lmax, | |
5262 | keep_fd); | |
5263 | i += lmax; | |
5264 | close(fd); | |
5265 | if (err) { | |
5266 | err = 7; | |
5267 | goto error; | |
5268 | } | |
5269 | } else { | |
5270 | int major = major(tmpdev->st_rdev); | |
5271 | int minor = minor(tmpdev->st_rdev); | |
5272 | err = get_super_block(super_list, | |
4dd2df09 | 5273 | NULL, |
ec50f7b6 LM |
5274 | tmpdev->devname, |
5275 | major, minor, | |
5276 | keep_fd); | |
5277 | i++; | |
5278 | if (err) { | |
5279 | err = 6; | |
5280 | goto error; | |
5281 | } | |
5282 | } | |
5283 | } | |
5284 | error: | |
5285 | *max = i; | |
5286 | return err; | |
5287 | } | |
9587c373 | 5288 | |
4dd2df09 | 5289 | static int get_super_block(struct intel_super **super_list, char *devnm, char *devname, |
9587c373 LM |
5290 | int major, int minor, int keep_fd) |
5291 | { | |
594dc1b8 | 5292 | struct intel_super *s; |
9587c373 LM |
5293 | char nm[32]; |
5294 | int dfd = -1; | |
9587c373 LM |
5295 | int err = 0; |
5296 | int retry; | |
5297 | ||
5298 | s = alloc_super(); | |
5299 | if (!s) { | |
5300 | err = 1; | |
5301 | goto error; | |
5302 | } | |
5303 | ||
5304 | sprintf(nm, "%d:%d", major, minor); | |
5305 | dfd = dev_open(nm, O_RDWR); | |
4389ce73 | 5306 | if (!is_fd_valid(dfd)) { |
9587c373 LM |
5307 | err = 2; |
5308 | goto error; | |
5309 | } | |
5310 | ||
aec01630 JS |
5311 | if (!get_dev_sector_size(dfd, NULL, &s->sector_size)) { |
5312 | err = 2; | |
5313 | goto error; | |
5314 | } | |
cb8f6859 | 5315 | find_intel_hba_capability(dfd, s, devname); |
9587c373 LM |
5316 | err = load_and_parse_mpb(dfd, s, NULL, keep_fd); |
5317 | ||
5318 | /* retry the load if we might have raced against mdmon */ | |
4dd2df09 | 5319 | if (err == 3 && devnm && mdmon_running(devnm)) |
9587c373 | 5320 | for (retry = 0; retry < 3; retry++) { |
239b3cc0 | 5321 | sleep_for(0, MSEC_TO_NSEC(3), true); |
9587c373 LM |
5322 | err = load_and_parse_mpb(dfd, s, NULL, keep_fd); |
5323 | if (err != 3) | |
5324 | break; | |
5325 | } | |
5326 | error: | |
5327 | if (!err) { | |
5328 | s->next = *super_list; | |
5329 | *super_list = s; | |
5330 | } else { | |
5331 | if (s) | |
8d67477f | 5332 | free_imsm(s); |
4389ce73 | 5333 | close_fd(&dfd); |
9587c373 | 5334 | } |
4389ce73 MT |
5335 | if (!keep_fd) |
5336 | close_fd(&dfd); | |
9587c373 LM |
5337 | return err; |
5338 | ||
5339 | } | |
5340 | ||
5341 | static int | |
5342 | get_sra_super_block(int fd, struct intel_super **super_list, char *devname, int *max, int keep_fd) | |
5343 | { | |
5344 | struct mdinfo *sra; | |
4dd2df09 | 5345 | char *devnm; |
9587c373 LM |
5346 | struct mdinfo *sd; |
5347 | int err = 0; | |
5348 | int i = 0; | |
4dd2df09 | 5349 | sra = sysfs_read(fd, NULL, GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE); |
9587c373 LM |
5350 | if (!sra) |
5351 | return 1; | |
5352 | ||
5353 | if (sra->array.major_version != -1 || | |
5354 | sra->array.minor_version != -2 || | |
5355 | strcmp(sra->text_version, "imsm") != 0) { | |
5356 | err = 1; | |
5357 | goto error; | |
5358 | } | |
5359 | /* load all mpbs */ | |
4dd2df09 | 5360 | devnm = fd2devnm(fd); |
9587c373 | 5361 | for (sd = sra->devs, i = 0; sd; sd = sd->next, i++) { |
4dd2df09 | 5362 | if (get_super_block(super_list, devnm, devname, |
9587c373 LM |
5363 | sd->disk.major, sd->disk.minor, keep_fd) != 0) { |
5364 | err = 7; | |
5365 | goto error; | |
5366 | } | |
5367 | } | |
5368 | error: | |
5369 | sysfs_free(sra); | |
5370 | *max = i; | |
5371 | return err; | |
5372 | } | |
5373 | ||
2b959fbf N |
5374 | static int load_container_imsm(struct supertype *st, int fd, char *devname) |
5375 | { | |
ec50f7b6 | 5376 | return load_super_imsm_all(st, fd, &st->sb, devname, NULL, 1); |
2b959fbf | 5377 | } |
cdddbdbc DW |
5378 | |
5379 | static int load_super_imsm(struct supertype *st, int fd, char *devname) | |
5380 | { | |
5381 | struct intel_super *super; | |
5382 | int rv; | |
8a3544f8 | 5383 | int retry; |
cdddbdbc | 5384 | |
357ac106 | 5385 | if (test_partition(fd)) |
691c6ee1 N |
5386 | /* IMSM not allowed on partitions */ |
5387 | return 1; | |
5388 | ||
37424f13 DW |
5389 | free_super_imsm(st); |
5390 | ||
49133e57 | 5391 | super = alloc_super(); |
8d67477f TM |
5392 | if (!super) |
5393 | return 1; | |
3a85bf0e MG |
5394 | |
5395 | if (!get_dev_sector_size(fd, NULL, &super->sector_size)) { | |
5396 | free_imsm(super); | |
5397 | return 1; | |
5398 | } | |
ea2bc72b LM |
5399 | /* Load hba and capabilities if they exist. |
5400 | * But do not preclude loading metadata in case capabilities or hba are | |
5401 | * non-compliant and ignore_hw_compat is set. | |
5402 | */ | |
d424212e | 5403 | rv = find_intel_hba_capability(fd, super, devname); |
f2f5c343 | 5404 | /* no orom/efi or non-intel hba of the disk */ |
089f9d79 | 5405 | if (rv != 0 && st->ignore_hw_compat == 0) { |
f2f5c343 | 5406 | if (devname) |
e7b84f9d | 5407 | pr_err("No OROM/EFI properties for %s\n", devname); |
f2f5c343 LM |
5408 | free_imsm(super); |
5409 | return 2; | |
5410 | } | |
a2b97981 | 5411 | rv = load_and_parse_mpb(fd, super, devname, 0); |
cdddbdbc | 5412 | |
8a3544f8 AP |
5413 | /* retry the load if we might have raced against mdmon */ |
5414 | if (rv == 3) { | |
f96b1302 AP |
5415 | struct mdstat_ent *mdstat = NULL; |
5416 | char *name = fd2kname(fd); | |
5417 | ||
5418 | if (name) | |
5419 | mdstat = mdstat_by_component(name); | |
8a3544f8 AP |
5420 | |
5421 | if (mdstat && mdmon_running(mdstat->devnm) && getpid() != mdmon_pid(mdstat->devnm)) { | |
5422 | for (retry = 0; retry < 3; retry++) { | |
239b3cc0 | 5423 | sleep_for(0, MSEC_TO_NSEC(3), true); |
8a3544f8 AP |
5424 | rv = load_and_parse_mpb(fd, super, devname, 0); |
5425 | if (rv != 3) | |
5426 | break; | |
5427 | } | |
5428 | } | |
5429 | ||
5430 | free_mdstat(mdstat); | |
5431 | } | |
5432 | ||
cdddbdbc DW |
5433 | if (rv) { |
5434 | if (devname) | |
7a862a02 | 5435 | pr_err("Failed to load all information sections on %s\n", devname); |
cdddbdbc DW |
5436 | free_imsm(super); |
5437 | return rv; | |
5438 | } | |
5439 | ||
5440 | st->sb = super; | |
5441 | if (st->ss == NULL) { | |
5442 | st->ss = &super_imsm; | |
5443 | st->minor_version = 0; | |
5444 | st->max_devs = IMSM_MAX_DEVICES; | |
5445 | } | |
8e59f3d8 AK |
5446 | |
5447 | /* load migration record */ | |
2f86fda3 | 5448 | if (load_imsm_migr_rec(super) == 0) { |
2e062e82 AK |
5449 | /* Check for unsupported migration features */ |
5450 | if (check_mpb_migr_compatibility(super) != 0) { | |
e7b84f9d | 5451 | pr_err("Unsupported migration detected"); |
2e062e82 AK |
5452 | if (devname) |
5453 | fprintf(stderr, " on %s\n", devname); | |
5454 | else | |
5455 | fprintf(stderr, " (IMSM).\n"); | |
5456 | return 3; | |
5457 | } | |
e2f41b2c AK |
5458 | } |
5459 | ||
cdddbdbc DW |
5460 | return 0; |
5461 | } | |
5462 | ||
ef6ffade DW |
5463 | static __u16 info_to_blocks_per_strip(mdu_array_info_t *info) |
5464 | { | |
5465 | if (info->level == 1) | |
5466 | return 128; | |
5467 | return info->chunk_size >> 9; | |
5468 | } | |
5469 | ||
5551b113 CA |
5470 | static unsigned long long info_to_blocks_per_member(mdu_array_info_t *info, |
5471 | unsigned long long size) | |
fcfd9599 | 5472 | { |
4025c288 | 5473 | if (info->level == 1) |
5551b113 | 5474 | return size * 2; |
4025c288 | 5475 | else |
5551b113 | 5476 | return (size * 2) & ~(info_to_blocks_per_strip(info) - 1); |
fcfd9599 DW |
5477 | } |
5478 | ||
4d1313e9 DW |
5479 | static void imsm_update_version_info(struct intel_super *super) |
5480 | { | |
5481 | /* update the version and attributes */ | |
5482 | struct imsm_super *mpb = super->anchor; | |
5483 | char *version; | |
5484 | struct imsm_dev *dev; | |
5485 | struct imsm_map *map; | |
5486 | int i; | |
5487 | ||
5488 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
5489 | dev = get_imsm_dev(super, i); | |
238c0a71 | 5490 | map = get_imsm_map(dev, MAP_0); |
4d1313e9 DW |
5491 | if (__le32_to_cpu(dev->size_high) > 0) |
5492 | mpb->attributes |= MPB_ATTRIB_2TB; | |
5493 | ||
5494 | /* FIXME detect when an array spans a port multiplier */ | |
5495 | #if 0 | |
5496 | mpb->attributes |= MPB_ATTRIB_PM; | |
5497 | #endif | |
5498 | ||
5499 | if (mpb->num_raid_devs > 1 || | |
5500 | mpb->attributes != MPB_ATTRIB_CHECKSUM_VERIFY) { | |
5501 | version = MPB_VERSION_ATTRIBS; | |
5502 | switch (get_imsm_raid_level(map)) { | |
5503 | case 0: mpb->attributes |= MPB_ATTRIB_RAID0; break; | |
5504 | case 1: mpb->attributes |= MPB_ATTRIB_RAID1; break; | |
5505 | case 10: mpb->attributes |= MPB_ATTRIB_RAID10; break; | |
5506 | case 5: mpb->attributes |= MPB_ATTRIB_RAID5; break; | |
5507 | } | |
5508 | } else { | |
5509 | if (map->num_members >= 5) | |
5510 | version = MPB_VERSION_5OR6_DISK_ARRAY; | |
5511 | else if (dev->status == DEV_CLONE_N_GO) | |
5512 | version = MPB_VERSION_CNG; | |
5513 | else if (get_imsm_raid_level(map) == 5) | |
5514 | version = MPB_VERSION_RAID5; | |
5515 | else if (map->num_members >= 3) | |
5516 | version = MPB_VERSION_3OR4_DISK_ARRAY; | |
5517 | else if (get_imsm_raid_level(map) == 1) | |
5518 | version = MPB_VERSION_RAID1; | |
5519 | else | |
5520 | version = MPB_VERSION_RAID0; | |
5521 | } | |
5522 | strcpy(((char *) mpb->sig) + strlen(MPB_SIGNATURE), version); | |
5523 | } | |
5524 | } | |
5525 | ||
e2eb503b MT |
5526 | /** |
5527 | * imsm_check_name() - check imsm naming criteria. | |
5528 | * @super: &intel_super pointer, not NULL. | |
5529 | * @name: name to check. | |
5530 | * @verbose: verbose level. | |
5531 | * | |
5532 | * Name must be no longer than &MAX_RAID_SERIAL_LEN and must be unique across volumes. | |
5533 | * | |
5534 | * Returns: &true if @name matches, &false otherwise. | |
5535 | */ | |
5536 | static bool imsm_is_name_allowed(struct intel_super *super, const char * const name, | |
5537 | const int verbose) | |
aa534678 DW |
5538 | { |
5539 | struct imsm_super *mpb = super->anchor; | |
aa534678 DW |
5540 | int i; |
5541 | ||
e2eb503b MT |
5542 | if (is_string_lq(name, MAX_RAID_SERIAL_LEN + 1) == false) { |
5543 | pr_vrb("imsm: Name \"%s\" is too long\n", name); | |
5544 | return false; | |
9bd99a90 RS |
5545 | } |
5546 | ||
aa534678 DW |
5547 | for (i = 0; i < mpb->num_raid_devs; i++) { |
5548 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
5549 | ||
5550 | if (strncmp((char *) dev->volume, name, MAX_RAID_SERIAL_LEN) == 0) { | |
e2eb503b MT |
5551 | pr_vrb("imsm: Name \"%s\" already exists\n", name); |
5552 | return false; | |
aa534678 DW |
5553 | } |
5554 | } | |
5555 | ||
e2eb503b | 5556 | return true; |
aa534678 DW |
5557 | } |
5558 | ||
8b353278 | 5559 | static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info, |
5308f117 | 5560 | struct shape *s, char *name, |
83cd1e97 N |
5561 | char *homehost, int *uuid, |
5562 | long long data_offset) | |
cdddbdbc | 5563 | { |
c2c087e6 DW |
5564 | /* We are creating a volume inside a pre-existing container. |
5565 | * so st->sb is already set. | |
5566 | */ | |
5567 | struct intel_super *super = st->sb; | |
f36a9ecd | 5568 | unsigned int sector_size = super->sector_size; |
949c47a0 | 5569 | struct imsm_super *mpb = super->anchor; |
ba2de7ba | 5570 | struct intel_dev *dv; |
c2c087e6 DW |
5571 | struct imsm_dev *dev; |
5572 | struct imsm_vol *vol; | |
5573 | struct imsm_map *map; | |
5574 | int idx = mpb->num_raid_devs; | |
5575 | int i; | |
760365f9 | 5576 | int namelen; |
c2c087e6 | 5577 | unsigned long long array_blocks; |
2c092cad | 5578 | size_t size_old, size_new; |
b53bfba6 TM |
5579 | unsigned int data_disks; |
5580 | unsigned long long size_per_member; | |
cdddbdbc | 5581 | |
88c32bb1 | 5582 | if (super->orom && mpb->num_raid_devs >= super->orom->vpa) { |
7a862a02 | 5583 | pr_err("This imsm-container already has the maximum of %d volumes\n", super->orom->vpa); |
c2c087e6 DW |
5584 | return 0; |
5585 | } | |
5586 | ||
2c092cad DW |
5587 | /* ensure the mpb is large enough for the new data */ |
5588 | size_old = __le32_to_cpu(mpb->mpb_size); | |
5589 | size_new = disks_to_mpb_size(info->nr_disks); | |
5590 | if (size_new > size_old) { | |
5591 | void *mpb_new; | |
f36a9ecd | 5592 | size_t size_round = ROUND_UP(size_new, sector_size); |
2c092cad | 5593 | |
f36a9ecd | 5594 | if (posix_memalign(&mpb_new, sector_size, size_round) != 0) { |
e7b84f9d | 5595 | pr_err("could not allocate new mpb\n"); |
2c092cad DW |
5596 | return 0; |
5597 | } | |
85337573 AO |
5598 | if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE, |
5599 | MIGR_REC_BUF_SECTORS* | |
5600 | MAX_SECTOR_SIZE) != 0) { | |
1ade5cc1 | 5601 | pr_err("could not allocate migr_rec buffer\n"); |
8e59f3d8 AK |
5602 | free(super->buf); |
5603 | free(super); | |
ea944c8f | 5604 | free(mpb_new); |
8e59f3d8 AK |
5605 | return 0; |
5606 | } | |
2c092cad DW |
5607 | memcpy(mpb_new, mpb, size_old); |
5608 | free(mpb); | |
5609 | mpb = mpb_new; | |
949c47a0 | 5610 | super->anchor = mpb_new; |
2c092cad DW |
5611 | mpb->mpb_size = __cpu_to_le32(size_new); |
5612 | memset(mpb_new + size_old, 0, size_round - size_old); | |
bbab0940 | 5613 | super->len = size_round; |
2c092cad | 5614 | } |
bf5a934a | 5615 | super->current_vol = idx; |
3960e579 DW |
5616 | |
5617 | /* handle 'failed_disks' by either: | |
5618 | * a) create dummy disk entries in the table if this the first | |
5619 | * volume in the array. We add them here as this is the only | |
5620 | * opportunity to add them. add_to_super_imsm_volume() | |
5621 | * handles the non-failed disks and continues incrementing | |
5622 | * mpb->num_disks. | |
5623 | * b) validate that 'failed_disks' matches the current number | |
5624 | * of missing disks if the container is populated | |
d23fe947 | 5625 | */ |
3960e579 | 5626 | if (super->current_vol == 0) { |
d23fe947 | 5627 | mpb->num_disks = 0; |
3960e579 DW |
5628 | for (i = 0; i < info->failed_disks; i++) { |
5629 | struct imsm_disk *disk; | |
5630 | ||
5631 | mpb->num_disks++; | |
5632 | disk = __get_imsm_disk(mpb, i); | |
5633 | disk->status = CONFIGURED_DISK | FAILED_DISK; | |
5634 | disk->scsi_id = __cpu_to_le32(~(__u32)0); | |
5635 | snprintf((char *) disk->serial, MAX_RAID_SERIAL_LEN, | |
5b975129 | 5636 | "missing:%d", (__u8)i); |
3960e579 DW |
5637 | } |
5638 | find_missing(super); | |
5639 | } else { | |
5640 | int missing = 0; | |
5641 | struct dl *d; | |
5642 | ||
5643 | for (d = super->missing; d; d = d->next) | |
5644 | missing++; | |
5645 | if (info->failed_disks > missing) { | |
e7b84f9d | 5646 | pr_err("unable to add 'missing' disk to container\n"); |
3960e579 DW |
5647 | return 0; |
5648 | } | |
5649 | } | |
5a038140 | 5650 | |
e2eb503b | 5651 | if (imsm_is_name_allowed(super, name, 1) == false) |
aa534678 | 5652 | return 0; |
e2eb503b | 5653 | |
503975b9 N |
5654 | dv = xmalloc(sizeof(*dv)); |
5655 | dev = xcalloc(1, sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1)); | |
760365f9 JS |
5656 | /* |
5657 | * Explicitly allow truncating to not confuse gcc's | |
5658 | * -Werror=stringop-truncation | |
5659 | */ | |
5660 | namelen = min((int) strlen(name), MAX_RAID_SERIAL_LEN); | |
5661 | memcpy(dev->volume, name, namelen); | |
e03640bd | 5662 | array_blocks = calc_array_size(info->level, info->raid_disks, |
03bcbc65 | 5663 | info->layout, info->chunk_size, |
b53bfba6 TM |
5664 | s->size * BLOCKS_PER_KB); |
5665 | data_disks = get_data_disks(info->level, info->layout, | |
5666 | info->raid_disks); | |
5667 | array_blocks = round_size_to_mb(array_blocks, data_disks); | |
5668 | size_per_member = array_blocks / data_disks; | |
979d38be | 5669 | |
fcc2c9da | 5670 | set_imsm_dev_size(dev, array_blocks); |
1a2487c2 | 5671 | dev->status = (DEV_READ_COALESCING | DEV_WRITE_COALESCING); |
c2c087e6 DW |
5672 | vol = &dev->vol; |
5673 | vol->migr_state = 0; | |
1484e727 | 5674 | set_migr_type(dev, MIGR_INIT); |
3960e579 | 5675 | vol->dirty = !info->state; |
4036e7ee | 5676 | set_vol_curr_migr_unit(dev, 0); |
238c0a71 | 5677 | map = get_imsm_map(dev, MAP_0); |
5551b113 | 5678 | set_pba_of_lba0(map, super->create_offset); |
ef6ffade | 5679 | map->blocks_per_strip = __cpu_to_le16(info_to_blocks_per_strip(info)); |
0556e1a2 | 5680 | map->failed_disk_num = ~0; |
bf4442ab | 5681 | if (info->level > 0) |
fffaf1ff N |
5682 | map->map_state = (info->state ? IMSM_T_STATE_NORMAL |
5683 | : IMSM_T_STATE_UNINITIALIZED); | |
bf4442ab AK |
5684 | else |
5685 | map->map_state = info->failed_disks ? IMSM_T_STATE_FAILED : | |
5686 | IMSM_T_STATE_NORMAL; | |
252d23c0 | 5687 | map->ddf = 1; |
ef6ffade DW |
5688 | |
5689 | if (info->level == 1 && info->raid_disks > 2) { | |
38950822 AW |
5690 | free(dev); |
5691 | free(dv); | |
7a862a02 | 5692 | pr_err("imsm does not support more than 2 disksin a raid1 volume\n"); |
ef6ffade DW |
5693 | return 0; |
5694 | } | |
81062a36 DW |
5695 | |
5696 | map->raid_level = info->level; | |
1c275381 | 5697 | if (info->level == 10) |
c2c087e6 | 5698 | map->raid_level = 1; |
1c275381 | 5699 | set_num_domains(map); |
ef6ffade | 5700 | |
44490938 MD |
5701 | size_per_member += NUM_BLOCKS_DIRTY_STRIPE_REGION; |
5702 | set_blocks_per_member(map, info_to_blocks_per_member(info, | |
5703 | size_per_member / | |
5704 | BLOCKS_PER_KB)); | |
5705 | ||
c2c087e6 | 5706 | map->num_members = info->raid_disks; |
1c275381 | 5707 | update_num_data_stripes(map, array_blocks); |
c2c087e6 DW |
5708 | for (i = 0; i < map->num_members; i++) { |
5709 | /* initialized in add_to_super */ | |
4eb26970 | 5710 | set_imsm_ord_tbl_ent(map, i, IMSM_ORD_REBUILD); |
c2c087e6 | 5711 | } |
949c47a0 | 5712 | mpb->num_raid_devs++; |
2a24dc1b PB |
5713 | mpb->num_raid_devs_created++; |
5714 | dev->my_vol_raid_dev_num = mpb->num_raid_devs_created; | |
ba2de7ba | 5715 | |
b7580566 | 5716 | if (s->consistency_policy <= CONSISTENCY_POLICY_RESYNC) { |
c2462068 | 5717 | dev->rwh_policy = RWH_MULTIPLE_OFF; |
2432ce9b | 5718 | } else if (s->consistency_policy == CONSISTENCY_POLICY_PPL) { |
c2462068 | 5719 | dev->rwh_policy = RWH_MULTIPLE_DISTRIBUTED; |
2432ce9b AP |
5720 | } else { |
5721 | free(dev); | |
5722 | free(dv); | |
5723 | pr_err("imsm does not support consistency policy %s\n", | |
5f21d674 | 5724 | map_num_s(consistency_policies, s->consistency_policy)); |
2432ce9b AP |
5725 | return 0; |
5726 | } | |
5727 | ||
ba2de7ba DW |
5728 | dv->dev = dev; |
5729 | dv->index = super->current_vol; | |
5730 | dv->next = super->devlist; | |
5731 | super->devlist = dv; | |
c2c087e6 | 5732 | |
4d1313e9 DW |
5733 | imsm_update_version_info(super); |
5734 | ||
c2c087e6 | 5735 | return 1; |
cdddbdbc DW |
5736 | } |
5737 | ||
bf5a934a | 5738 | static int init_super_imsm(struct supertype *st, mdu_array_info_t *info, |
5308f117 | 5739 | struct shape *s, char *name, |
83cd1e97 N |
5740 | char *homehost, int *uuid, |
5741 | unsigned long long data_offset) | |
bf5a934a DW |
5742 | { |
5743 | /* This is primarily called by Create when creating a new array. | |
5744 | * We will then get add_to_super called for each component, and then | |
5745 | * write_init_super called to write it out to each device. | |
5746 | * For IMSM, Create can create on fresh devices or on a pre-existing | |
5747 | * array. | |
5748 | * To create on a pre-existing array a different method will be called. | |
5749 | * This one is just for fresh drives. | |
5750 | */ | |
5751 | struct intel_super *super; | |
5752 | struct imsm_super *mpb; | |
5753 | size_t mpb_size; | |
4d1313e9 | 5754 | char *version; |
bf5a934a | 5755 | |
83cd1e97 | 5756 | if (data_offset != INVALID_SECTORS) { |
ed503f89 | 5757 | pr_err("data-offset not supported by imsm\n"); |
83cd1e97 N |
5758 | return 0; |
5759 | } | |
5760 | ||
bf5a934a | 5761 | if (st->sb) |
5308f117 | 5762 | return init_super_imsm_volume(st, info, s, name, homehost, uuid, |
83cd1e97 | 5763 | data_offset); |
e683ca88 DW |
5764 | |
5765 | if (info) | |
5766 | mpb_size = disks_to_mpb_size(info->nr_disks); | |
5767 | else | |
f36a9ecd | 5768 | mpb_size = MAX_SECTOR_SIZE; |
bf5a934a | 5769 | |
49133e57 | 5770 | super = alloc_super(); |
f36a9ecd PB |
5771 | if (super && |
5772 | posix_memalign(&super->buf, MAX_SECTOR_SIZE, mpb_size) != 0) { | |
8d67477f | 5773 | free_imsm(super); |
e683ca88 DW |
5774 | super = NULL; |
5775 | } | |
5776 | if (!super) { | |
1ade5cc1 | 5777 | pr_err("could not allocate superblock\n"); |
bf5a934a DW |
5778 | return 0; |
5779 | } | |
de44e46f PB |
5780 | if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE, |
5781 | MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE) != 0) { | |
1ade5cc1 | 5782 | pr_err("could not allocate migr_rec buffer\n"); |
8e59f3d8 | 5783 | free(super->buf); |
8d67477f | 5784 | free_imsm(super); |
8e59f3d8 AK |
5785 | return 0; |
5786 | } | |
e683ca88 | 5787 | memset(super->buf, 0, mpb_size); |
ef649044 | 5788 | mpb = super->buf; |
e683ca88 DW |
5789 | mpb->mpb_size = __cpu_to_le32(mpb_size); |
5790 | st->sb = super; | |
5791 | ||
5792 | if (info == NULL) { | |
5793 | /* zeroing superblock */ | |
5794 | return 0; | |
5795 | } | |
bf5a934a | 5796 | |
4d1313e9 DW |
5797 | mpb->attributes = MPB_ATTRIB_CHECKSUM_VERIFY; |
5798 | ||
5799 | version = (char *) mpb->sig; | |
5800 | strcpy(version, MPB_SIGNATURE); | |
5801 | version += strlen(MPB_SIGNATURE); | |
5802 | strcpy(version, MPB_VERSION_RAID0); | |
bf5a934a | 5803 | |
bf5a934a DW |
5804 | return 1; |
5805 | } | |
5806 | ||
f2cc4f7d AO |
5807 | static int drive_validate_sector_size(struct intel_super *super, struct dl *dl) |
5808 | { | |
5809 | unsigned int member_sector_size; | |
5810 | ||
4389ce73 | 5811 | if (!is_fd_valid(dl->fd)) { |
f2cc4f7d AO |
5812 | pr_err("Invalid file descriptor for %s\n", dl->devname); |
5813 | return 0; | |
5814 | } | |
5815 | ||
5816 | if (!get_dev_sector_size(dl->fd, dl->devname, &member_sector_size)) | |
5817 | return 0; | |
5818 | if (member_sector_size != super->sector_size) | |
5819 | return 0; | |
5820 | return 1; | |
5821 | } | |
5822 | ||
f20c3968 | 5823 | static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk, |
bf5a934a DW |
5824 | int fd, char *devname) |
5825 | { | |
5826 | struct intel_super *super = st->sb; | |
d23fe947 | 5827 | struct imsm_super *mpb = super->anchor; |
3960e579 | 5828 | struct imsm_disk *_disk; |
bf5a934a DW |
5829 | struct imsm_dev *dev; |
5830 | struct imsm_map *map; | |
3960e579 | 5831 | struct dl *dl, *df; |
4eb26970 | 5832 | int slot; |
9a7df595 MT |
5833 | int autolayout = 0; |
5834 | ||
5835 | if (!is_fd_valid(fd)) | |
5836 | autolayout = 1; | |
bf5a934a | 5837 | |
949c47a0 | 5838 | dev = get_imsm_dev(super, super->current_vol); |
238c0a71 | 5839 | map = get_imsm_map(dev, MAP_0); |
bf5a934a | 5840 | |
208933a7 | 5841 | if (! (dk->state & (1<<MD_DISK_SYNC))) { |
e7b84f9d | 5842 | pr_err("%s: Cannot add spare devices to IMSM volume\n", |
208933a7 N |
5843 | devname); |
5844 | return 1; | |
5845 | } | |
5846 | ||
9a7df595 MT |
5847 | for (dl = super->disks; dl ; dl = dl->next) { |
5848 | if (autolayout) { | |
efb30e7f DW |
5849 | if (dl->raiddisk == dk->raid_disk) |
5850 | break; | |
9a7df595 MT |
5851 | } else if (dl->major == dk->major && dl->minor == dk->minor) |
5852 | break; | |
efb30e7f | 5853 | } |
d23fe947 | 5854 | |
208933a7 | 5855 | if (!dl) { |
9a7df595 MT |
5856 | if (!autolayout) |
5857 | pr_err("%s is not a member of the same container.\n", | |
5858 | devname); | |
f20c3968 | 5859 | return 1; |
208933a7 | 5860 | } |
bf5a934a | 5861 | |
9a7df595 MT |
5862 | if (!autolayout && super->current_vol > 0) { |
5863 | int _slot = get_disk_slot_in_dev(super, 0, dl->index); | |
5864 | ||
5865 | if (_slot != dk->raid_disk) { | |
5866 | pr_err("Member %s is in %d slot for the first volume, but is in %d slot for a new volume.\n", | |
5867 | dl->devname, _slot, dk->raid_disk); | |
5868 | pr_err("Raid members are in different order than for the first volume, aborting.\n"); | |
5869 | return 1; | |
5870 | } | |
5871 | } | |
5872 | ||
59632db9 MZ |
5873 | if (mpb->num_disks == 0) |
5874 | if (!get_dev_sector_size(dl->fd, dl->devname, | |
5875 | &super->sector_size)) | |
5876 | return 1; | |
5877 | ||
f2cc4f7d AO |
5878 | if (!drive_validate_sector_size(super, dl)) { |
5879 | pr_err("Combining drives of different sector size in one volume is not allowed\n"); | |
5880 | return 1; | |
5881 | } | |
5882 | ||
d23fe947 DW |
5883 | /* add a pristine spare to the metadata */ |
5884 | if (dl->index < 0) { | |
5885 | dl->index = super->anchor->num_disks; | |
5886 | super->anchor->num_disks++; | |
5887 | } | |
4eb26970 DW |
5888 | /* Check the device has not already been added */ |
5889 | slot = get_imsm_disk_slot(map, dl->index); | |
5890 | if (slot >= 0 && | |
238c0a71 | 5891 | (get_imsm_ord_tbl_ent(dev, slot, MAP_X) & IMSM_ORD_REBUILD) == 0) { |
e7b84f9d | 5892 | pr_err("%s has been included in this array twice\n", |
4eb26970 DW |
5893 | devname); |
5894 | return 1; | |
5895 | } | |
656b6b5a | 5896 | set_imsm_ord_tbl_ent(map, dk->raid_disk, dl->index); |
ee5aad5a | 5897 | dl->disk.status = CONFIGURED_DISK; |
d23fe947 | 5898 | |
3960e579 DW |
5899 | /* update size of 'missing' disks to be at least as large as the |
5900 | * largest acitve member (we only have dummy missing disks when | |
5901 | * creating the first volume) | |
5902 | */ | |
5903 | if (super->current_vol == 0) { | |
5904 | for (df = super->missing; df; df = df->next) { | |
5551b113 CA |
5905 | if (total_blocks(&dl->disk) > total_blocks(&df->disk)) |
5906 | set_total_blocks(&df->disk, total_blocks(&dl->disk)); | |
3960e579 DW |
5907 | _disk = __get_imsm_disk(mpb, df->index); |
5908 | *_disk = df->disk; | |
5909 | } | |
5910 | } | |
5911 | ||
5912 | /* refresh unset/failed slots to point to valid 'missing' entries */ | |
5913 | for (df = super->missing; df; df = df->next) | |
5914 | for (slot = 0; slot < mpb->num_disks; slot++) { | |
238c0a71 | 5915 | __u32 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X); |
3960e579 DW |
5916 | |
5917 | if ((ord & IMSM_ORD_REBUILD) == 0) | |
5918 | continue; | |
5919 | set_imsm_ord_tbl_ent(map, slot, df->index | IMSM_ORD_REBUILD); | |
1ace8403 | 5920 | if (is_gen_migration(dev)) { |
238c0a71 AK |
5921 | struct imsm_map *map2 = get_imsm_map(dev, |
5922 | MAP_1); | |
0a108d63 | 5923 | int slot2 = get_imsm_disk_slot(map2, df->index); |
089f9d79 | 5924 | if (slot2 < map2->num_members && slot2 >= 0) { |
1ace8403 | 5925 | __u32 ord2 = get_imsm_ord_tbl_ent(dev, |
238c0a71 AK |
5926 | slot2, |
5927 | MAP_1); | |
1ace8403 AK |
5928 | if ((unsigned)df->index == |
5929 | ord_to_idx(ord2)) | |
5930 | set_imsm_ord_tbl_ent(map2, | |
0a108d63 | 5931 | slot2, |
1ace8403 AK |
5932 | df->index | |
5933 | IMSM_ORD_REBUILD); | |
5934 | } | |
5935 | } | |
3960e579 DW |
5936 | dprintf("set slot:%d to missing disk:%d\n", slot, df->index); |
5937 | break; | |
5938 | } | |
5939 | ||
d23fe947 DW |
5940 | /* if we are creating the first raid device update the family number */ |
5941 | if (super->current_vol == 0) { | |
5942 | __u32 sum; | |
5943 | struct imsm_dev *_dev = __get_imsm_dev(mpb, 0); | |
d23fe947 | 5944 | |
3960e579 | 5945 | _disk = __get_imsm_disk(mpb, dl->index); |
756a15f3 | 5946 | if (!_disk) { |
e7b84f9d | 5947 | pr_err("BUG mpb setup error\n"); |
791b666a AW |
5948 | return 1; |
5949 | } | |
d23fe947 DW |
5950 | *_dev = *dev; |
5951 | *_disk = dl->disk; | |
148acb7b DW |
5952 | sum = random32(); |
5953 | sum += __gen_imsm_checksum(mpb); | |
d23fe947 | 5954 | mpb->family_num = __cpu_to_le32(sum); |
148acb7b | 5955 | mpb->orig_family_num = mpb->family_num; |
e48aed3c | 5956 | mpb->creation_time = __cpu_to_le64((__u64)time(NULL)); |
d23fe947 | 5957 | } |
ca0748fa | 5958 | super->current_disk = dl; |
f20c3968 | 5959 | return 0; |
bf5a934a DW |
5960 | } |
5961 | ||
a8619d23 AK |
5962 | /* mark_spare() |
5963 | * Function marks disk as spare and restores disk serial | |
5964 | * in case it was previously marked as failed by takeover operation | |
5965 | * reruns: | |
5966 | * -1 : critical error | |
5967 | * 0 : disk is marked as spare but serial is not set | |
5968 | * 1 : success | |
5969 | */ | |
5970 | int mark_spare(struct dl *disk) | |
5971 | { | |
5972 | __u8 serial[MAX_RAID_SERIAL_LEN]; | |
5973 | int ret_val = -1; | |
5974 | ||
5975 | if (!disk) | |
5976 | return ret_val; | |
5977 | ||
5978 | ret_val = 0; | |
6da53c0e | 5979 | if (!imsm_read_serial(disk->fd, NULL, serial, MAX_RAID_SERIAL_LEN)) { |
a8619d23 AK |
5980 | /* Restore disk serial number, because takeover marks disk |
5981 | * as failed and adds to serial ':0' before it becomes | |
5982 | * a spare disk. | |
5983 | */ | |
5984 | serialcpy(disk->serial, serial); | |
5985 | serialcpy(disk->disk.serial, serial); | |
5986 | ret_val = 1; | |
5987 | } | |
5988 | disk->disk.status = SPARE_DISK; | |
5989 | disk->index = -1; | |
5990 | ||
5991 | return ret_val; | |
5992 | } | |
88654014 | 5993 | |
12724c01 TM |
5994 | |
5995 | static int write_super_imsm_spare(struct intel_super *super, struct dl *d); | |
5996 | ||
f20c3968 | 5997 | static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk, |
72ca9bcf N |
5998 | int fd, char *devname, |
5999 | unsigned long long data_offset) | |
cdddbdbc | 6000 | { |
c2c087e6 | 6001 | struct intel_super *super = st->sb; |
c2c087e6 DW |
6002 | struct dl *dd; |
6003 | unsigned long long size; | |
fa7bb6f8 | 6004 | unsigned int member_sector_size; |
f2f27e63 | 6005 | __u32 id; |
c2c087e6 DW |
6006 | int rv; |
6007 | struct stat stb; | |
6008 | ||
88654014 LM |
6009 | /* If we are on an RAID enabled platform check that the disk is |
6010 | * attached to the raid controller. | |
6011 | * We do not need to test disks attachment for container based additions, | |
6012 | * they shall be already tested when container was created/assembled. | |
88c32bb1 | 6013 | */ |
d424212e | 6014 | rv = find_intel_hba_capability(fd, super, devname); |
f2f5c343 | 6015 | /* no orom/efi or non-intel hba of the disk */ |
f0f5a016 LM |
6016 | if (rv != 0) { |
6017 | dprintf("capability: %p fd: %d ret: %d\n", | |
6018 | super->orom, fd, rv); | |
6019 | return 1; | |
88c32bb1 DW |
6020 | } |
6021 | ||
f20c3968 DW |
6022 | if (super->current_vol >= 0) |
6023 | return add_to_super_imsm_volume(st, dk, fd, devname); | |
bf5a934a | 6024 | |
c2c087e6 | 6025 | fstat(fd, &stb); |
503975b9 | 6026 | dd = xcalloc(sizeof(*dd), 1); |
c2c087e6 DW |
6027 | dd->major = major(stb.st_rdev); |
6028 | dd->minor = minor(stb.st_rdev); | |
503975b9 | 6029 | dd->devname = devname ? xstrdup(devname) : NULL; |
c2c087e6 | 6030 | dd->fd = fd; |
689c9bf3 | 6031 | dd->e = NULL; |
1a64be56 | 6032 | dd->action = DISK_ADD; |
6da53c0e | 6033 | rv = imsm_read_serial(fd, devname, dd->serial, MAX_RAID_SERIAL_LEN); |
32ba9157 | 6034 | if (rv) { |
e7b84f9d | 6035 | pr_err("failed to retrieve scsi serial, aborting\n"); |
3a85bf0e | 6036 | __free_imsm_disk(dd, 0); |
0030e8d6 | 6037 | abort(); |
c2c087e6 | 6038 | } |
7c798f87 | 6039 | |
20bee0f8 PB |
6040 | if (super->hba && ((super->hba->type == SYS_DEV_NVME) || |
6041 | (super->hba->type == SYS_DEV_VMD))) { | |
6042 | int i; | |
7c798f87 MT |
6043 | char cntrl_path[PATH_MAX]; |
6044 | char *cntrl_name; | |
6045 | char pci_dev_path[PATH_MAX]; | |
20bee0f8 | 6046 | |
7c798f87 MT |
6047 | if (!diskfd_to_devpath(fd, 2, pci_dev_path) || |
6048 | !diskfd_to_devpath(fd, 1, cntrl_path)) { | |
8662f92d | 6049 | pr_err("failed to get dev paths, aborting\n"); |
3a85bf0e | 6050 | __free_imsm_disk(dd, 0); |
a8f3cfd5 MT |
6051 | return 1; |
6052 | } | |
6053 | ||
7c798f87 MT |
6054 | cntrl_name = basename(cntrl_path); |
6055 | if (is_multipath_nvme(fd)) | |
6056 | pr_err("%s controller supports Multi-Path I/O, Intel (R) VROC does not support multipathing\n", | |
6057 | cntrl_name); | |
6058 | ||
6059 | if (devpath_to_vendor(pci_dev_path) == 0x8086) { | |
20bee0f8 PB |
6060 | /* |
6061 | * If Intel's NVMe drive has serial ended with | |
6062 | * "-A","-B","-1" or "-2" it means that this is "x8" | |
6063 | * device (double drive on single PCIe card). | |
6064 | * User should be warned about potential data loss. | |
6065 | */ | |
6066 | for (i = MAX_RAID_SERIAL_LEN-1; i > 0; i--) { | |
6067 | /* Skip empty character at the end */ | |
6068 | if (dd->serial[i] == 0) | |
6069 | continue; | |
6070 | ||
6071 | if (((dd->serial[i] == 'A') || | |
6072 | (dd->serial[i] == 'B') || | |
6073 | (dd->serial[i] == '1') || | |
6074 | (dd->serial[i] == '2')) && | |
6075 | (dd->serial[i-1] == '-')) | |
6076 | pr_err("\tThe action you are about to take may put your data at risk.\n" | |
6077 | "\tPlease note that x8 devices may consist of two separate x4 devices " | |
6078 | "located on a single PCIe port.\n" | |
6079 | "\tRAID 0 is the only supported configuration for this type of x8 device.\n"); | |
6080 | break; | |
6081 | } | |
32716c51 PB |
6082 | } else if (super->hba->type == SYS_DEV_VMD && super->orom && |
6083 | !imsm_orom_has_tpv_support(super->orom)) { | |
6084 | pr_err("\tPlatform configuration does not support non-Intel NVMe drives.\n" | |
8b751247 | 6085 | "\tPlease refer to Intel(R) RSTe/VROC user guide.\n"); |
3a85bf0e | 6086 | __free_imsm_disk(dd, 0); |
32716c51 | 6087 | return 1; |
20bee0f8 PB |
6088 | } |
6089 | } | |
c2c087e6 | 6090 | |
c2c087e6 | 6091 | get_dev_size(fd, NULL, &size); |
3a85bf0e MG |
6092 | if (!get_dev_sector_size(fd, NULL, &member_sector_size)) { |
6093 | __free_imsm_disk(dd, 0); | |
aec01630 | 6094 | return 1; |
3a85bf0e | 6095 | } |
fa7bb6f8 PB |
6096 | |
6097 | if (super->sector_size == 0) { | |
6098 | /* this a first device, so sector_size is not set yet */ | |
6099 | super->sector_size = member_sector_size; | |
fa7bb6f8 PB |
6100 | } |
6101 | ||
71e5411e | 6102 | /* clear migr_rec when adding disk to container */ |
85337573 AO |
6103 | memset(super->migr_rec_buf, 0, MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE); |
6104 | if (lseek64(fd, size - MIGR_REC_SECTOR_POSITION*member_sector_size, | |
de44e46f | 6105 | SEEK_SET) >= 0) { |
466070ad | 6106 | if ((unsigned int)write(fd, super->migr_rec_buf, |
85337573 AO |
6107 | MIGR_REC_BUF_SECTORS*member_sector_size) != |
6108 | MIGR_REC_BUF_SECTORS*member_sector_size) | |
71e5411e PB |
6109 | perror("Write migr_rec failed"); |
6110 | } | |
6111 | ||
c2c087e6 | 6112 | size /= 512; |
1f24f035 | 6113 | serialcpy(dd->disk.serial, dd->serial); |
5551b113 CA |
6114 | set_total_blocks(&dd->disk, size); |
6115 | if (__le32_to_cpu(dd->disk.total_blocks_hi) > 0) { | |
6116 | struct imsm_super *mpb = super->anchor; | |
6117 | mpb->attributes |= MPB_ATTRIB_2TB_DISK; | |
6118 | } | |
a8619d23 | 6119 | mark_spare(dd); |
c2c087e6 | 6120 | if (sysfs_disk_to_scsi_id(fd, &id) == 0) |
b9f594fe | 6121 | dd->disk.scsi_id = __cpu_to_le32(id); |
c2c087e6 | 6122 | else |
b9f594fe | 6123 | dd->disk.scsi_id = __cpu_to_le32(0); |
43dad3d6 DW |
6124 | |
6125 | if (st->update_tail) { | |
1a64be56 LM |
6126 | dd->next = super->disk_mgmt_list; |
6127 | super->disk_mgmt_list = dd; | |
43dad3d6 | 6128 | } else { |
12724c01 TM |
6129 | /* this is called outside of mdmon |
6130 | * write initial spare metadata | |
6131 | * mdmon will overwrite it. | |
6132 | */ | |
43dad3d6 DW |
6133 | dd->next = super->disks; |
6134 | super->disks = dd; | |
12724c01 | 6135 | write_super_imsm_spare(super, dd); |
43dad3d6 | 6136 | } |
f20c3968 DW |
6137 | |
6138 | return 0; | |
cdddbdbc DW |
6139 | } |
6140 | ||
1a64be56 LM |
6141 | static int remove_from_super_imsm(struct supertype *st, mdu_disk_info_t *dk) |
6142 | { | |
6143 | struct intel_super *super = st->sb; | |
6144 | struct dl *dd; | |
6145 | ||
6146 | /* remove from super works only in mdmon - for communication | |
6147 | * manager - monitor. Check if communication memory buffer | |
6148 | * is prepared. | |
6149 | */ | |
6150 | if (!st->update_tail) { | |
1ade5cc1 | 6151 | pr_err("shall be used in mdmon context only\n"); |
1a64be56 LM |
6152 | return 1; |
6153 | } | |
503975b9 | 6154 | dd = xcalloc(1, sizeof(*dd)); |
1a64be56 LM |
6155 | dd->major = dk->major; |
6156 | dd->minor = dk->minor; | |
1a64be56 | 6157 | dd->fd = -1; |
a8619d23 | 6158 | mark_spare(dd); |
1a64be56 LM |
6159 | dd->action = DISK_REMOVE; |
6160 | ||
6161 | dd->next = super->disk_mgmt_list; | |
6162 | super->disk_mgmt_list = dd; | |
6163 | ||
1a64be56 LM |
6164 | return 0; |
6165 | } | |
6166 | ||
f796af5d DW |
6167 | static int store_imsm_mpb(int fd, struct imsm_super *mpb); |
6168 | ||
6169 | static union { | |
f36a9ecd | 6170 | char buf[MAX_SECTOR_SIZE]; |
f796af5d | 6171 | struct imsm_super anchor; |
f36a9ecd | 6172 | } spare_record __attribute__ ((aligned(MAX_SECTOR_SIZE))); |
c2c087e6 | 6173 | |
12724c01 TM |
6174 | |
6175 | static int write_super_imsm_spare(struct intel_super *super, struct dl *d) | |
d23fe947 | 6176 | { |
d23fe947 | 6177 | struct imsm_super *mpb = super->anchor; |
f796af5d | 6178 | struct imsm_super *spare = &spare_record.anchor; |
d23fe947 | 6179 | __u32 sum; |
12724c01 TM |
6180 | |
6181 | if (d->index != -1) | |
6182 | return 1; | |
d23fe947 | 6183 | |
68641cdb JS |
6184 | spare->mpb_size = __cpu_to_le32(sizeof(struct imsm_super)); |
6185 | spare->generation_num = __cpu_to_le32(1UL); | |
f796af5d | 6186 | spare->attributes = MPB_ATTRIB_CHECKSUM_VERIFY; |
68641cdb JS |
6187 | spare->num_disks = 1; |
6188 | spare->num_raid_devs = 0; | |
6189 | spare->cache_size = mpb->cache_size; | |
6190 | spare->pwr_cycle_count = __cpu_to_le32(1); | |
f796af5d DW |
6191 | |
6192 | snprintf((char *) spare->sig, MAX_SIGNATURE_LENGTH, | |
6193 | MPB_SIGNATURE MPB_VERSION_RAID0); | |
d23fe947 | 6194 | |
12724c01 TM |
6195 | spare->disk[0] = d->disk; |
6196 | if (__le32_to_cpu(d->disk.total_blocks_hi) > 0) | |
6197 | spare->attributes |= MPB_ATTRIB_2TB_DISK; | |
6198 | ||
6199 | if (super->sector_size == 4096) | |
6200 | convert_to_4k_imsm_disk(&spare->disk[0]); | |
d23fe947 | 6201 | |
12724c01 TM |
6202 | sum = __gen_imsm_checksum(spare); |
6203 | spare->family_num = __cpu_to_le32(sum); | |
6204 | spare->orig_family_num = 0; | |
6205 | sum = __gen_imsm_checksum(spare); | |
6206 | spare->check_sum = __cpu_to_le32(sum); | |
027c374f | 6207 | |
12724c01 TM |
6208 | if (store_imsm_mpb(d->fd, spare)) { |
6209 | pr_err("failed for device %d:%d %s\n", | |
6210 | d->major, d->minor, strerror(errno)); | |
6211 | return 1; | |
6212 | } | |
6213 | ||
6214 | return 0; | |
6215 | } | |
6216 | /* spare records have their own family number and do not have any defined raid | |
6217 | * devices | |
6218 | */ | |
6219 | static int write_super_imsm_spares(struct intel_super *super, int doclose) | |
6220 | { | |
6221 | struct dl *d; | |
f36a9ecd | 6222 | |
12724c01 TM |
6223 | for (d = super->disks; d; d = d->next) { |
6224 | if (d->index != -1) | |
6225 | continue; | |
d23fe947 | 6226 | |
12724c01 | 6227 | if (write_super_imsm_spare(super, d)) |
e74255d9 | 6228 | return 1; |
12724c01 | 6229 | |
4389ce73 MT |
6230 | if (doclose) |
6231 | close_fd(&d->fd); | |
d23fe947 DW |
6232 | } |
6233 | ||
e74255d9 | 6234 | return 0; |
d23fe947 DW |
6235 | } |
6236 | ||
36988a3d | 6237 | static int write_super_imsm(struct supertype *st, int doclose) |
cdddbdbc | 6238 | { |
36988a3d | 6239 | struct intel_super *super = st->sb; |
f36a9ecd | 6240 | unsigned int sector_size = super->sector_size; |
949c47a0 | 6241 | struct imsm_super *mpb = super->anchor; |
c2c087e6 DW |
6242 | struct dl *d; |
6243 | __u32 generation; | |
6244 | __u32 sum; | |
d23fe947 | 6245 | int spares = 0; |
949c47a0 | 6246 | int i; |
a48ac0a8 | 6247 | __u32 mpb_size = sizeof(struct imsm_super) - sizeof(struct imsm_disk); |
36988a3d | 6248 | int num_disks = 0; |
146c6260 | 6249 | int clear_migration_record = 1; |
bbab0940 | 6250 | __u32 bbm_log_size; |
cdddbdbc | 6251 | |
c2c087e6 DW |
6252 | /* 'generation' is incremented everytime the metadata is written */ |
6253 | generation = __le32_to_cpu(mpb->generation_num); | |
6254 | generation++; | |
6255 | mpb->generation_num = __cpu_to_le32(generation); | |
6256 | ||
148acb7b DW |
6257 | /* fix up cases where previous mdadm releases failed to set |
6258 | * orig_family_num | |
6259 | */ | |
6260 | if (mpb->orig_family_num == 0) | |
6261 | mpb->orig_family_num = mpb->family_num; | |
6262 | ||
d23fe947 | 6263 | for (d = super->disks; d; d = d->next) { |
8796fdc4 | 6264 | if (d->index == -1) |
d23fe947 | 6265 | spares++; |
36988a3d | 6266 | else { |
d23fe947 | 6267 | mpb->disk[d->index] = d->disk; |
36988a3d AK |
6268 | num_disks++; |
6269 | } | |
d23fe947 | 6270 | } |
36988a3d | 6271 | for (d = super->missing; d; d = d->next) { |
47ee5a45 | 6272 | mpb->disk[d->index] = d->disk; |
36988a3d AK |
6273 | num_disks++; |
6274 | } | |
6275 | mpb->num_disks = num_disks; | |
6276 | mpb_size += sizeof(struct imsm_disk) * mpb->num_disks; | |
b9f594fe | 6277 | |
949c47a0 DW |
6278 | for (i = 0; i < mpb->num_raid_devs; i++) { |
6279 | struct imsm_dev *dev = __get_imsm_dev(mpb, i); | |
36988a3d | 6280 | struct imsm_dev *dev2 = get_imsm_dev(super, i); |
756a15f3 MG |
6281 | |
6282 | imsm_copy_dev(dev, dev2); | |
6283 | mpb_size += sizeof_imsm_dev(dev, 0); | |
6284 | ||
146c6260 AK |
6285 | if (is_gen_migration(dev2)) |
6286 | clear_migration_record = 0; | |
949c47a0 | 6287 | } |
bbab0940 TM |
6288 | |
6289 | bbm_log_size = get_imsm_bbm_log_size(super->bbm_log); | |
6290 | ||
6291 | if (bbm_log_size) { | |
6292 | memcpy((void *)mpb + mpb_size, super->bbm_log, bbm_log_size); | |
6293 | mpb->attributes |= MPB_ATTRIB_BBM; | |
6294 | } else | |
6295 | mpb->attributes &= ~MPB_ATTRIB_BBM; | |
6296 | ||
6297 | super->anchor->bbm_log_size = __cpu_to_le32(bbm_log_size); | |
6298 | mpb_size += bbm_log_size; | |
a48ac0a8 | 6299 | mpb->mpb_size = __cpu_to_le32(mpb_size); |
949c47a0 | 6300 | |
bbab0940 TM |
6301 | #ifdef DEBUG |
6302 | assert(super->len == 0 || mpb_size <= super->len); | |
6303 | #endif | |
6304 | ||
c2c087e6 | 6305 | /* recalculate checksum */ |
949c47a0 | 6306 | sum = __gen_imsm_checksum(mpb); |
c2c087e6 DW |
6307 | mpb->check_sum = __cpu_to_le32(sum); |
6308 | ||
51d83f5d AK |
6309 | if (super->clean_migration_record_by_mdmon) { |
6310 | clear_migration_record = 1; | |
6311 | super->clean_migration_record_by_mdmon = 0; | |
6312 | } | |
146c6260 | 6313 | if (clear_migration_record) |
de44e46f | 6314 | memset(super->migr_rec_buf, 0, |
85337573 | 6315 | MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE); |
146c6260 | 6316 | |
f36a9ecd PB |
6317 | if (sector_size == 4096) |
6318 | convert_to_4k(super); | |
6319 | ||
d23fe947 | 6320 | /* write the mpb for disks that compose raid devices */ |
c2c087e6 | 6321 | for (d = super->disks; d ; d = d->next) { |
86c54047 | 6322 | if (d->index < 0 || is_failed(&d->disk)) |
d23fe947 | 6323 | continue; |
30602f53 | 6324 | |
146c6260 AK |
6325 | if (clear_migration_record) { |
6326 | unsigned long long dsize; | |
6327 | ||
6328 | get_dev_size(d->fd, NULL, &dsize); | |
de44e46f PB |
6329 | if (lseek64(d->fd, dsize - sector_size, |
6330 | SEEK_SET) >= 0) { | |
466070ad PB |
6331 | if ((unsigned int)write(d->fd, |
6332 | super->migr_rec_buf, | |
de44e46f PB |
6333 | MIGR_REC_BUF_SECTORS*sector_size) != |
6334 | MIGR_REC_BUF_SECTORS*sector_size) | |
9e2d750d | 6335 | perror("Write migr_rec failed"); |
146c6260 AK |
6336 | } |
6337 | } | |
51d83f5d AK |
6338 | |
6339 | if (store_imsm_mpb(d->fd, mpb)) | |
6340 | fprintf(stderr, | |
1ade5cc1 N |
6341 | "failed for device %d:%d (fd: %d)%s\n", |
6342 | d->major, d->minor, | |
51d83f5d AK |
6343 | d->fd, strerror(errno)); |
6344 | ||
4389ce73 MT |
6345 | if (doclose) |
6346 | close_fd(&d->fd); | |
c2c087e6 DW |
6347 | } |
6348 | ||
d23fe947 DW |
6349 | if (spares) |
6350 | return write_super_imsm_spares(super, doclose); | |
6351 | ||
e74255d9 | 6352 | return 0; |
c2c087e6 DW |
6353 | } |
6354 | ||
9b1fb677 | 6355 | static int create_array(struct supertype *st, int dev_idx) |
43dad3d6 DW |
6356 | { |
6357 | size_t len; | |
6358 | struct imsm_update_create_array *u; | |
6359 | struct intel_super *super = st->sb; | |
9b1fb677 | 6360 | struct imsm_dev *dev = get_imsm_dev(super, dev_idx); |
238c0a71 | 6361 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
54c2c1ea DW |
6362 | struct disk_info *inf; |
6363 | struct imsm_disk *disk; | |
6364 | int i; | |
43dad3d6 | 6365 | |
54c2c1ea DW |
6366 | len = sizeof(*u) - sizeof(*dev) + sizeof_imsm_dev(dev, 0) + |
6367 | sizeof(*inf) * map->num_members; | |
503975b9 | 6368 | u = xmalloc(len); |
43dad3d6 | 6369 | u->type = update_create_array; |
9b1fb677 | 6370 | u->dev_idx = dev_idx; |
43dad3d6 | 6371 | imsm_copy_dev(&u->dev, dev); |
54c2c1ea DW |
6372 | inf = get_disk_info(u); |
6373 | for (i = 0; i < map->num_members; i++) { | |
238c0a71 | 6374 | int idx = get_imsm_disk_idx(dev, i, MAP_X); |
9b1fb677 | 6375 | |
54c2c1ea | 6376 | disk = get_imsm_disk(super, idx); |
1ca5c8e0 N |
6377 | if (!disk) |
6378 | disk = get_imsm_missing(super, idx); | |
54c2c1ea DW |
6379 | serialcpy(inf[i].serial, disk->serial); |
6380 | } | |
43dad3d6 DW |
6381 | append_metadata_update(st, u, len); |
6382 | ||
6383 | return 0; | |
6384 | } | |
6385 | ||
1a64be56 | 6386 | static int mgmt_disk(struct supertype *st) |
43dad3d6 DW |
6387 | { |
6388 | struct intel_super *super = st->sb; | |
6389 | size_t len; | |
1a64be56 | 6390 | struct imsm_update_add_remove_disk *u; |
43dad3d6 | 6391 | |
1a64be56 | 6392 | if (!super->disk_mgmt_list) |
43dad3d6 DW |
6393 | return 0; |
6394 | ||
6395 | len = sizeof(*u); | |
503975b9 | 6396 | u = xmalloc(len); |
1a64be56 | 6397 | u->type = update_add_remove_disk; |
43dad3d6 DW |
6398 | append_metadata_update(st, u, len); |
6399 | ||
6400 | return 0; | |
6401 | } | |
2432ce9b AP |
6402 | |
6403 | __u32 crc32c_le(__u32 crc, unsigned char const *p, size_t len); | |
6404 | ||
e397cefe AP |
6405 | static int write_ppl_header(unsigned long long ppl_sector, int fd, void *buf) |
6406 | { | |
6407 | struct ppl_header *ppl_hdr = buf; | |
6408 | int ret; | |
6409 | ||
6410 | ppl_hdr->checksum = __cpu_to_le32(~crc32c_le(~0, buf, PPL_HEADER_SIZE)); | |
6411 | ||
6412 | if (lseek64(fd, ppl_sector * 512, SEEK_SET) < 0) { | |
6413 | ret = -errno; | |
6414 | perror("Failed to seek to PPL header location"); | |
6415 | return ret; | |
6416 | } | |
6417 | ||
6418 | if (write(fd, buf, PPL_HEADER_SIZE) != PPL_HEADER_SIZE) { | |
6419 | ret = -errno; | |
6420 | perror("Write PPL header failed"); | |
6421 | return ret; | |
6422 | } | |
6423 | ||
6424 | fsync(fd); | |
6425 | ||
6426 | return 0; | |
6427 | } | |
6428 | ||
2432ce9b AP |
6429 | static int write_init_ppl_imsm(struct supertype *st, struct mdinfo *info, int fd) |
6430 | { | |
6431 | struct intel_super *super = st->sb; | |
6432 | void *buf; | |
6433 | struct ppl_header *ppl_hdr; | |
6434 | int ret; | |
6435 | ||
b2514242 PB |
6436 | /* first clear entire ppl space */ |
6437 | ret = zero_disk_range(fd, info->ppl_sector, info->ppl_size); | |
6438 | if (ret) | |
6439 | return ret; | |
6440 | ||
6441 | ret = posix_memalign(&buf, MAX_SECTOR_SIZE, PPL_HEADER_SIZE); | |
2432ce9b AP |
6442 | if (ret) { |
6443 | pr_err("Failed to allocate PPL header buffer\n"); | |
e397cefe | 6444 | return -ret; |
2432ce9b AP |
6445 | } |
6446 | ||
6447 | memset(buf, 0, PPL_HEADER_SIZE); | |
6448 | ppl_hdr = buf; | |
6449 | memset(ppl_hdr->reserved, 0xff, PPL_HDR_RESERVED); | |
6450 | ppl_hdr->signature = __cpu_to_le32(super->anchor->orig_family_num); | |
b23d0750 AP |
6451 | |
6452 | if (info->mismatch_cnt) { | |
6453 | /* | |
6454 | * We are overwriting an invalid ppl. Make one entry with wrong | |
6455 | * checksum to prevent the kernel from skipping resync. | |
6456 | */ | |
6457 | ppl_hdr->entries_count = __cpu_to_le32(1); | |
6458 | ppl_hdr->entries[0].checksum = ~0; | |
6459 | } | |
6460 | ||
e397cefe | 6461 | ret = write_ppl_header(info->ppl_sector, fd, buf); |
2432ce9b AP |
6462 | |
6463 | free(buf); | |
6464 | return ret; | |
6465 | } | |
6466 | ||
e397cefe AP |
6467 | static int is_rebuilding(struct imsm_dev *dev); |
6468 | ||
2432ce9b AP |
6469 | static int validate_ppl_imsm(struct supertype *st, struct mdinfo *info, |
6470 | struct mdinfo *disk) | |
6471 | { | |
6472 | struct intel_super *super = st->sb; | |
6473 | struct dl *d; | |
e397cefe | 6474 | void *buf_orig, *buf, *buf_prev = NULL; |
2432ce9b | 6475 | int ret = 0; |
e397cefe | 6476 | struct ppl_header *ppl_hdr = NULL; |
2432ce9b AP |
6477 | __u32 crc; |
6478 | struct imsm_dev *dev; | |
2432ce9b | 6479 | __u32 idx; |
44b6b876 PB |
6480 | unsigned int i; |
6481 | unsigned long long ppl_offset = 0; | |
6482 | unsigned long long prev_gen_num = 0; | |
2432ce9b AP |
6483 | |
6484 | if (disk->disk.raid_disk < 0) | |
6485 | return 0; | |
6486 | ||
2432ce9b | 6487 | dev = get_imsm_dev(super, info->container_member); |
2fc0fc63 | 6488 | idx = get_imsm_disk_idx(dev, disk->disk.raid_disk, MAP_0); |
2432ce9b AP |
6489 | d = get_imsm_dl_disk(super, idx); |
6490 | ||
6491 | if (!d || d->index < 0 || is_failed(&d->disk)) | |
e397cefe AP |
6492 | return 0; |
6493 | ||
6494 | if (posix_memalign(&buf_orig, MAX_SECTOR_SIZE, PPL_HEADER_SIZE * 2)) { | |
6495 | pr_err("Failed to allocate PPL header buffer\n"); | |
6496 | return -1; | |
6497 | } | |
6498 | buf = buf_orig; | |
2432ce9b | 6499 | |
44b6b876 PB |
6500 | ret = 1; |
6501 | while (ppl_offset < MULTIPLE_PPL_AREA_SIZE_IMSM) { | |
e397cefe AP |
6502 | void *tmp; |
6503 | ||
44b6b876 | 6504 | dprintf("Checking potential PPL at offset: %llu\n", ppl_offset); |
2432ce9b | 6505 | |
44b6b876 PB |
6506 | if (lseek64(d->fd, info->ppl_sector * 512 + ppl_offset, |
6507 | SEEK_SET) < 0) { | |
6508 | perror("Failed to seek to PPL header location"); | |
6509 | ret = -1; | |
e397cefe | 6510 | break; |
44b6b876 | 6511 | } |
2432ce9b | 6512 | |
44b6b876 PB |
6513 | if (read(d->fd, buf, PPL_HEADER_SIZE) != PPL_HEADER_SIZE) { |
6514 | perror("Read PPL header failed"); | |
6515 | ret = -1; | |
e397cefe | 6516 | break; |
44b6b876 | 6517 | } |
2432ce9b | 6518 | |
44b6b876 | 6519 | ppl_hdr = buf; |
2432ce9b | 6520 | |
44b6b876 PB |
6521 | crc = __le32_to_cpu(ppl_hdr->checksum); |
6522 | ppl_hdr->checksum = 0; | |
6523 | ||
6524 | if (crc != ~crc32c_le(~0, buf, PPL_HEADER_SIZE)) { | |
6525 | dprintf("Wrong PPL header checksum on %s\n", | |
6526 | d->devname); | |
e397cefe | 6527 | break; |
44b6b876 PB |
6528 | } |
6529 | ||
6530 | if (prev_gen_num > __le64_to_cpu(ppl_hdr->generation)) { | |
6531 | /* previous was newest, it was already checked */ | |
e397cefe | 6532 | break; |
44b6b876 PB |
6533 | } |
6534 | ||
6535 | if ((__le32_to_cpu(ppl_hdr->signature) != | |
6536 | super->anchor->orig_family_num)) { | |
6537 | dprintf("Wrong PPL header signature on %s\n", | |
6538 | d->devname); | |
6539 | ret = 1; | |
e397cefe | 6540 | break; |
44b6b876 PB |
6541 | } |
6542 | ||
6543 | ret = 0; | |
6544 | prev_gen_num = __le64_to_cpu(ppl_hdr->generation); | |
2432ce9b | 6545 | |
44b6b876 PB |
6546 | ppl_offset += PPL_HEADER_SIZE; |
6547 | for (i = 0; i < __le32_to_cpu(ppl_hdr->entries_count); i++) | |
6548 | ppl_offset += | |
6549 | __le32_to_cpu(ppl_hdr->entries[i].pp_size); | |
e397cefe AP |
6550 | |
6551 | if (!buf_prev) | |
6552 | buf_prev = buf + PPL_HEADER_SIZE; | |
6553 | tmp = buf_prev; | |
6554 | buf_prev = buf; | |
6555 | buf = tmp; | |
2432ce9b AP |
6556 | } |
6557 | ||
e397cefe AP |
6558 | if (buf_prev) { |
6559 | buf = buf_prev; | |
6560 | ppl_hdr = buf_prev; | |
6561 | } | |
2432ce9b | 6562 | |
54148aba PB |
6563 | /* |
6564 | * Update metadata to use mutliple PPLs area (1MB). | |
6565 | * This is done once for all RAID members | |
6566 | */ | |
6567 | if (info->consistency_policy == CONSISTENCY_POLICY_PPL && | |
6568 | info->ppl_size != (MULTIPLE_PPL_AREA_SIZE_IMSM >> 9)) { | |
6569 | char subarray[20]; | |
6570 | struct mdinfo *member_dev; | |
6571 | ||
6572 | sprintf(subarray, "%d", info->container_member); | |
6573 | ||
6574 | if (mdmon_running(st->container_devnm)) | |
6575 | st->update_tail = &st->updates; | |
6576 | ||
03312b52 | 6577 | if (st->ss->update_subarray(st, subarray, UOPT_PPL, NULL)) { |
54148aba PB |
6578 | pr_err("Failed to update subarray %s\n", |
6579 | subarray); | |
6580 | } else { | |
6581 | if (st->update_tail) | |
6582 | flush_metadata_updates(st); | |
6583 | else | |
6584 | st->ss->sync_metadata(st); | |
6585 | info->ppl_size = (MULTIPLE_PPL_AREA_SIZE_IMSM >> 9); | |
6586 | for (member_dev = info->devs; member_dev; | |
6587 | member_dev = member_dev->next) | |
6588 | member_dev->ppl_size = | |
6589 | (MULTIPLE_PPL_AREA_SIZE_IMSM >> 9); | |
6590 | } | |
6591 | } | |
6592 | ||
b23d0750 | 6593 | if (ret == 1) { |
2fc0fc63 AP |
6594 | struct imsm_map *map = get_imsm_map(dev, MAP_X); |
6595 | ||
50b9c10d PB |
6596 | if (map->map_state == IMSM_T_STATE_UNINITIALIZED || |
6597 | (map->map_state == IMSM_T_STATE_NORMAL && | |
2ec9d182 | 6598 | !(dev->vol.dirty & RAIDVOL_DIRTY)) || |
e397cefe | 6599 | (is_rebuilding(dev) && |
4036e7ee | 6600 | vol_curr_migr_unit(dev) == 0 && |
2ec9d182 | 6601 | get_imsm_disk_idx(dev, disk->disk.raid_disk, MAP_1) != idx)) |
b23d0750 AP |
6602 | ret = st->ss->write_init_ppl(st, info, d->fd); |
6603 | else | |
6604 | info->mismatch_cnt++; | |
e397cefe AP |
6605 | } else if (ret == 0 && |
6606 | ppl_hdr->entries_count == 0 && | |
6607 | is_rebuilding(dev) && | |
6608 | info->resync_start == 0) { | |
6609 | /* | |
6610 | * The header has no entries - add a single empty entry and | |
6611 | * rewrite the header to prevent the kernel from going into | |
6612 | * resync after an interrupted rebuild. | |
6613 | */ | |
6614 | ppl_hdr->entries_count = __cpu_to_le32(1); | |
6615 | ret = write_ppl_header(info->ppl_sector, d->fd, buf); | |
b23d0750 | 6616 | } |
2432ce9b | 6617 | |
e397cefe AP |
6618 | free(buf_orig); |
6619 | ||
2432ce9b AP |
6620 | return ret; |
6621 | } | |
6622 | ||
2432ce9b AP |
6623 | static int write_init_ppl_imsm_all(struct supertype *st, struct mdinfo *info) |
6624 | { | |
6625 | struct intel_super *super = st->sb; | |
6626 | struct dl *d; | |
6627 | int ret = 0; | |
6628 | ||
6629 | if (info->consistency_policy != CONSISTENCY_POLICY_PPL || | |
6630 | info->array.level != 5) | |
6631 | return 0; | |
6632 | ||
6633 | for (d = super->disks; d ; d = d->next) { | |
6634 | if (d->index < 0 || is_failed(&d->disk)) | |
6635 | continue; | |
6636 | ||
6637 | ret = st->ss->write_init_ppl(st, info, d->fd); | |
6638 | if (ret) | |
6639 | break; | |
6640 | } | |
6641 | ||
6642 | return ret; | |
6643 | } | |
43dad3d6 | 6644 | |
fbc42556 JR |
6645 | /******************************************************************************* |
6646 | * Function: write_init_bitmap_imsm_vol | |
6647 | * Description: Write a bitmap header and prepares the area for the bitmap. | |
6648 | * Parameters: | |
6649 | * st : supertype information | |
6650 | * vol_idx : the volume index to use | |
6651 | * | |
6652 | * Returns: | |
6653 | * 0 : success | |
6654 | * -1 : fail | |
6655 | ******************************************************************************/ | |
6656 | static int write_init_bitmap_imsm_vol(struct supertype *st, int vol_idx) | |
6657 | { | |
6658 | struct intel_super *super = st->sb; | |
6659 | int prev_current_vol = super->current_vol; | |
6660 | struct dl *d; | |
6661 | int ret = 0; | |
6662 | ||
6663 | super->current_vol = vol_idx; | |
6664 | for (d = super->disks; d; d = d->next) { | |
6665 | if (d->index < 0 || is_failed(&d->disk)) | |
6666 | continue; | |
6667 | ret = st->ss->write_bitmap(st, d->fd, NoUpdate); | |
6668 | if (ret) | |
6669 | break; | |
6670 | } | |
6671 | super->current_vol = prev_current_vol; | |
6672 | return ret; | |
6673 | } | |
6674 | ||
6675 | /******************************************************************************* | |
6676 | * Function: write_init_bitmap_imsm_all | |
6677 | * Description: Write a bitmap header and prepares the area for the bitmap. | |
6678 | * Operation is executed for volumes with CONSISTENCY_POLICY_BITMAP. | |
6679 | * Parameters: | |
6680 | * st : supertype information | |
6681 | * info : info about the volume where the bitmap should be written | |
6682 | * vol_idx : the volume index to use | |
6683 | * | |
6684 | * Returns: | |
6685 | * 0 : success | |
6686 | * -1 : fail | |
6687 | ******************************************************************************/ | |
6688 | static int write_init_bitmap_imsm_all(struct supertype *st, struct mdinfo *info, | |
6689 | int vol_idx) | |
6690 | { | |
6691 | int ret = 0; | |
6692 | ||
6693 | if (info && (info->consistency_policy == CONSISTENCY_POLICY_BITMAP)) | |
6694 | ret = write_init_bitmap_imsm_vol(st, vol_idx); | |
6695 | ||
6696 | return ret; | |
6697 | } | |
6698 | ||
c2c087e6 DW |
6699 | static int write_init_super_imsm(struct supertype *st) |
6700 | { | |
9b1fb677 DW |
6701 | struct intel_super *super = st->sb; |
6702 | int current_vol = super->current_vol; | |
2432ce9b AP |
6703 | int rv = 0; |
6704 | struct mdinfo info; | |
6705 | ||
6706 | getinfo_super_imsm(st, &info, NULL); | |
9b1fb677 DW |
6707 | |
6708 | /* we are done with current_vol reset it to point st at the container */ | |
6709 | super->current_vol = -1; | |
6710 | ||
8273f55e | 6711 | if (st->update_tail) { |
43dad3d6 DW |
6712 | /* queue the recently created array / added disk |
6713 | * as a metadata update */ | |
8273f55e | 6714 | |
43dad3d6 | 6715 | /* determine if we are creating a volume or adding a disk */ |
9b1fb677 | 6716 | if (current_vol < 0) { |
1a64be56 LM |
6717 | /* in the mgmt (add/remove) disk case we are running |
6718 | * in mdmon context, so don't close fd's | |
43dad3d6 | 6719 | */ |
2432ce9b AP |
6720 | rv = mgmt_disk(st); |
6721 | } else { | |
fbc42556 | 6722 | /* adding the second volume to the array */ |
2432ce9b | 6723 | rv = write_init_ppl_imsm_all(st, &info); |
fbc42556 JR |
6724 | if (!rv) |
6725 | rv = write_init_bitmap_imsm_all(st, &info, current_vol); | |
2432ce9b AP |
6726 | if (!rv) |
6727 | rv = create_array(st, current_vol); | |
6728 | } | |
d682f344 N |
6729 | } else { |
6730 | struct dl *d; | |
6731 | for (d = super->disks; d; d = d->next) | |
ba728be7 | 6732 | Kill(d->devname, NULL, 0, -1, 1); |
fbc42556 | 6733 | if (current_vol >= 0) { |
2432ce9b | 6734 | rv = write_init_ppl_imsm_all(st, &info); |
fbc42556 JR |
6735 | if (!rv) |
6736 | rv = write_init_bitmap_imsm_all(st, &info, current_vol); | |
6737 | } | |
6738 | ||
2432ce9b AP |
6739 | if (!rv) |
6740 | rv = write_super_imsm(st, 1); | |
d682f344 | 6741 | } |
2432ce9b AP |
6742 | |
6743 | return rv; | |
cdddbdbc DW |
6744 | } |
6745 | ||
e683ca88 | 6746 | static int store_super_imsm(struct supertype *st, int fd) |
cdddbdbc | 6747 | { |
e683ca88 DW |
6748 | struct intel_super *super = st->sb; |
6749 | struct imsm_super *mpb = super ? super->anchor : NULL; | |
551c80c1 | 6750 | |
e683ca88 | 6751 | if (!mpb) |
ad97895e DW |
6752 | return 1; |
6753 | ||
f36a9ecd PB |
6754 | if (super->sector_size == 4096) |
6755 | convert_to_4k(super); | |
e683ca88 | 6756 | return store_imsm_mpb(fd, mpb); |
cdddbdbc DW |
6757 | } |
6758 | ||
cdddbdbc | 6759 | static int validate_geometry_imsm_container(struct supertype *st, int level, |
1f5d54a0 | 6760 | int raiddisks, |
af4348dd N |
6761 | unsigned long long data_offset, |
6762 | char *dev, | |
2c514b71 NB |
6763 | unsigned long long *freesize, |
6764 | int verbose) | |
cdddbdbc | 6765 | { |
c2c087e6 DW |
6766 | int fd; |
6767 | unsigned long long ldsize; | |
8662f92d | 6768 | struct intel_super *super = NULL; |
f2f5c343 | 6769 | int rv = 0; |
cdddbdbc | 6770 | |
6f2af6a4 | 6771 | if (!is_container(level)) |
c2c087e6 DW |
6772 | return 0; |
6773 | if (!dev) | |
6774 | return 1; | |
6775 | ||
dca80fcd | 6776 | fd = dev_open(dev, O_RDONLY|O_EXCL); |
4389ce73 MT |
6777 | if (!is_fd_valid(fd)) { |
6778 | pr_vrb("imsm: Cannot open %s: %s\n", dev, strerror(errno)); | |
c2c087e6 DW |
6779 | return 0; |
6780 | } | |
8662f92d MT |
6781 | if (!get_dev_size(fd, dev, &ldsize)) |
6782 | goto exit; | |
f2f5c343 LM |
6783 | |
6784 | /* capabilities retrieve could be possible | |
6785 | * note that there is no fd for the disks in array. | |
6786 | */ | |
6787 | super = alloc_super(); | |
8662f92d MT |
6788 | if (!super) |
6789 | goto exit; | |
6790 | ||
6791 | if (!get_dev_sector_size(fd, NULL, &super->sector_size)) | |
6792 | goto exit; | |
fa7bb6f8 | 6793 | |
ba728be7 | 6794 | rv = find_intel_hba_capability(fd, super, verbose > 0 ? dev : NULL); |
f2f5c343 LM |
6795 | if (rv != 0) { |
6796 | #if DEBUG | |
6797 | char str[256]; | |
6798 | fd2devname(fd, str); | |
1ade5cc1 | 6799 | dprintf("fd: %d %s orom: %p rv: %d raiddisk: %d\n", |
f2f5c343 LM |
6800 | fd, str, super->orom, rv, raiddisks); |
6801 | #endif | |
6802 | /* no orom/efi or non-intel hba of the disk */ | |
8662f92d MT |
6803 | rv = 0; |
6804 | goto exit; | |
f2f5c343 | 6805 | } |
9126b9a8 CA |
6806 | if (super->orom) { |
6807 | if (raiddisks > super->orom->tds) { | |
6808 | if (verbose) | |
7a862a02 | 6809 | pr_err("%d exceeds maximum number of platform supported disks: %d\n", |
9126b9a8 | 6810 | raiddisks, super->orom->tds); |
8662f92d | 6811 | goto exit; |
9126b9a8 CA |
6812 | } |
6813 | if ((super->orom->attr & IMSM_OROM_ATTR_2TB_DISK) == 0 && | |
6814 | (ldsize >> 9) >> 32 > 0) { | |
6815 | if (verbose) | |
e7b84f9d | 6816 | pr_err("%s exceeds maximum platform supported size\n", dev); |
8662f92d MT |
6817 | goto exit; |
6818 | } | |
6819 | ||
6820 | if (super->hba->type == SYS_DEV_VMD || | |
6821 | super->hba->type == SYS_DEV_NVME) { | |
6822 | if (!imsm_is_nvme_namespace_supported(fd, 1)) { | |
6823 | if (verbose) | |
6824 | pr_err("NVMe namespace %s is not supported by IMSM\n", | |
6825 | basename(dev)); | |
6826 | goto exit; | |
6827 | } | |
9126b9a8 | 6828 | } |
f2f5c343 | 6829 | } |
1f5d54a0 MT |
6830 | if (freesize) |
6831 | *freesize = avail_size_imsm(st, ldsize >> 9, data_offset); | |
8662f92d MT |
6832 | rv = 1; |
6833 | exit: | |
6834 | if (super) | |
6835 | free_imsm(super); | |
6836 | close(fd); | |
c2c087e6 | 6837 | |
8662f92d | 6838 | return rv; |
cdddbdbc DW |
6839 | } |
6840 | ||
0dcecb2e DW |
6841 | static unsigned long long find_size(struct extent *e, int *idx, int num_extents) |
6842 | { | |
6843 | const unsigned long long base_start = e[*idx].start; | |
6844 | unsigned long long end = base_start + e[*idx].size; | |
6845 | int i; | |
6846 | ||
6847 | if (base_start == end) | |
6848 | return 0; | |
6849 | ||
6850 | *idx = *idx + 1; | |
6851 | for (i = *idx; i < num_extents; i++) { | |
6852 | /* extend overlapping extents */ | |
6853 | if (e[i].start >= base_start && | |
6854 | e[i].start <= end) { | |
6855 | if (e[i].size == 0) | |
6856 | return 0; | |
6857 | if (e[i].start + e[i].size > end) | |
6858 | end = e[i].start + e[i].size; | |
6859 | } else if (e[i].start > end) { | |
6860 | *idx = i; | |
6861 | break; | |
6862 | } | |
6863 | } | |
6864 | ||
6865 | return end - base_start; | |
6866 | } | |
6867 | ||
aa19fdd4 | 6868 | /** merge_extents() - analyze extents and get free size. |
9bc426fa | 6869 | * @super: Intel metadata, not NULL. |
aa19fdd4 | 6870 | * @expanding: if set, we are expanding &super->current_vol. |
9bc426fa | 6871 | * |
aa19fdd4 MT |
6872 | * Build a composite disk with all known extents and generate a size given the |
6873 | * "all disks in an array must share a common start offset" constraint. | |
6874 | * If a volume is expanded, then return free space after the volume. | |
9bc426fa | 6875 | * |
aa19fdd4 | 6876 | * Return: Free space or 0 on failure. |
9bc426fa | 6877 | */ |
aa19fdd4 | 6878 | static unsigned long long merge_extents(struct intel_super *super, const bool expanding) |
0dcecb2e | 6879 | { |
9bc426fa | 6880 | struct extent *e; |
0dcecb2e | 6881 | struct dl *dl; |
aa19fdd4 MT |
6882 | int i, j, pos_vol_idx = -1; |
6883 | int extent_idx = 0; | |
6884 | int sum_extents = 0; | |
6885 | unsigned long long pos = 0; | |
b9d77223 | 6886 | unsigned long long start = 0; |
1dea84ae MT |
6887 | unsigned long long free_size = 0; |
6888 | ||
6889 | unsigned long pre_reservation = 0; | |
6890 | unsigned long post_reservation = IMSM_RESERVED_SECTORS; | |
6891 | unsigned long reservation_size; | |
0dcecb2e | 6892 | |
9bc426fa MT |
6893 | for (dl = super->disks; dl; dl = dl->next) |
6894 | if (dl->e) | |
6895 | sum_extents += dl->extent_cnt; | |
6896 | e = xcalloc(sum_extents, sizeof(struct extent)); | |
6897 | ||
0dcecb2e DW |
6898 | /* coalesce and sort all extents. also, check to see if we need to |
6899 | * reserve space between member arrays | |
6900 | */ | |
6901 | j = 0; | |
6902 | for (dl = super->disks; dl; dl = dl->next) { | |
6903 | if (!dl->e) | |
6904 | continue; | |
6905 | for (i = 0; i < dl->extent_cnt; i++) | |
6906 | e[j++] = dl->e[i]; | |
6907 | } | |
6908 | qsort(e, sum_extents, sizeof(*e), cmp_extent); | |
6909 | ||
6910 | /* merge extents */ | |
6911 | i = 0; | |
6912 | j = 0; | |
6913 | while (i < sum_extents) { | |
6914 | e[j].start = e[i].start; | |
aa19fdd4 | 6915 | e[j].vol = e[i].vol; |
0dcecb2e DW |
6916 | e[j].size = find_size(e, &i, sum_extents); |
6917 | j++; | |
6918 | if (e[j-1].size == 0) | |
6919 | break; | |
6920 | } | |
6921 | ||
0dcecb2e DW |
6922 | i = 0; |
6923 | do { | |
aa19fdd4 | 6924 | unsigned long long esize = e[i].start - pos; |
0dcecb2e | 6925 | |
1dea84ae MT |
6926 | if (expanding ? pos_vol_idx == super->current_vol : esize >= free_size) { |
6927 | free_size = esize; | |
0dcecb2e | 6928 | start = pos; |
aa19fdd4 | 6929 | extent_idx = i; |
0dcecb2e | 6930 | } |
aa19fdd4 | 6931 | |
0dcecb2e | 6932 | pos = e[i].start + e[i].size; |
aa19fdd4 MT |
6933 | pos_vol_idx = e[i].vol; |
6934 | ||
0dcecb2e DW |
6935 | i++; |
6936 | } while (e[i-1].size); | |
0dcecb2e | 6937 | |
1dea84ae MT |
6938 | if (free_size == 0) { |
6939 | dprintf("imsm: Cannot find free size.\n"); | |
6940 | free(e); | |
a7dd165b | 6941 | return 0; |
1dea84ae | 6942 | } |
a7dd165b | 6943 | |
1dea84ae MT |
6944 | if (!expanding && extent_idx != 0) |
6945 | /* | |
6946 | * Not a real first volume in a container is created, pre_reservation is needed. | |
6947 | */ | |
6948 | pre_reservation = IMSM_RESERVED_SECTORS; | |
0dcecb2e | 6949 | |
1dea84ae MT |
6950 | if (e[extent_idx].size == 0) |
6951 | /* | |
6952 | * extent_idx points to the metadata, post_reservation is allready done. | |
6953 | */ | |
6954 | post_reservation = 0; | |
6955 | free(e); | |
0dcecb2e | 6956 | |
1dea84ae MT |
6957 | reservation_size = pre_reservation + post_reservation; |
6958 | ||
6959 | if (free_size < reservation_size) { | |
6960 | dprintf("imsm: Reservation size is greater than free space.\n"); | |
6961 | return 0; | |
6962 | } | |
0dcecb2e | 6963 | |
1dea84ae MT |
6964 | super->create_offset = start + pre_reservation; |
6965 | return free_size - reservation_size; | |
0dcecb2e DW |
6966 | } |
6967 | ||
88c32bb1 DW |
6968 | static int is_raid_level_supported(const struct imsm_orom *orom, int level, int raiddisks) |
6969 | { | |
6970 | if (level < 0 || level == 6 || level == 4) | |
6971 | return 0; | |
6972 | ||
6973 | /* if we have an orom prevent invalid raid levels */ | |
6974 | if (orom) | |
6975 | switch (level) { | |
6976 | case 0: return imsm_orom_has_raid0(orom); | |
6977 | case 1: | |
6978 | if (raiddisks > 2) | |
6979 | return imsm_orom_has_raid1e(orom); | |
1c556e92 DW |
6980 | return imsm_orom_has_raid1(orom) && raiddisks == 2; |
6981 | case 10: return imsm_orom_has_raid10(orom) && raiddisks == 4; | |
6982 | case 5: return imsm_orom_has_raid5(orom) && raiddisks > 2; | |
88c32bb1 DW |
6983 | } |
6984 | else | |
6985 | return 1; /* not on an Intel RAID platform so anything goes */ | |
6986 | ||
6987 | return 0; | |
6988 | } | |
6989 | ||
ca9de185 LM |
6990 | static int |
6991 | active_arrays_by_format(char *name, char* hba, struct md_list **devlist, | |
6992 | int dpa, int verbose) | |
6993 | { | |
6994 | struct mdstat_ent *mdstat = mdstat_read(0, 0); | |
594dc1b8 | 6995 | struct mdstat_ent *memb; |
ca9de185 LM |
6996 | int count = 0; |
6997 | int num = 0; | |
594dc1b8 | 6998 | struct md_list *dv; |
ca9de185 LM |
6999 | int found; |
7000 | ||
7001 | for (memb = mdstat ; memb ; memb = memb->next) { | |
7002 | if (memb->metadata_version && | |
fc54fe7a | 7003 | (strncmp(memb->metadata_version, "external:", 9) == 0) && |
ca9de185 LM |
7004 | (strcmp(&memb->metadata_version[9], name) == 0) && |
7005 | !is_subarray(memb->metadata_version+9) && | |
7006 | memb->members) { | |
7007 | struct dev_member *dev = memb->members; | |
7008 | int fd = -1; | |
4389ce73 | 7009 | while (dev && !is_fd_valid(fd)) { |
503975b9 | 7010 | char *path = xmalloc(strlen(dev->name) + strlen("/dev/") + 1); |
dd5ab402 | 7011 | num = snprintf(path, PATH_MAX, "%s%s", "/dev/", dev->name); |
503975b9 N |
7012 | if (num > 0) |
7013 | fd = open(path, O_RDONLY, 0); | |
4389ce73 | 7014 | if (num <= 0 || !is_fd_valid(fd)) { |
676e87a8 | 7015 | pr_vrb("Cannot open %s: %s\n", |
503975b9 | 7016 | dev->name, strerror(errno)); |
ca9de185 | 7017 | } |
503975b9 | 7018 | free(path); |
ca9de185 LM |
7019 | dev = dev->next; |
7020 | } | |
7021 | found = 0; | |
4389ce73 | 7022 | if (is_fd_valid(fd) && disk_attached_to_hba(fd, hba)) { |
ca9de185 LM |
7023 | struct mdstat_ent *vol; |
7024 | for (vol = mdstat ; vol ; vol = vol->next) { | |
089f9d79 | 7025 | if (vol->active > 0 && |
ca9de185 | 7026 | vol->metadata_version && |
9581efb1 | 7027 | is_container_member(vol, memb->devnm)) { |
ca9de185 LM |
7028 | found++; |
7029 | count++; | |
7030 | } | |
7031 | } | |
7032 | if (*devlist && (found < dpa)) { | |
503975b9 | 7033 | dv = xcalloc(1, sizeof(*dv)); |
9581efb1 N |
7034 | dv->devname = xmalloc(strlen(memb->devnm) + strlen("/dev/") + 1); |
7035 | sprintf(dv->devname, "%s%s", "/dev/", memb->devnm); | |
503975b9 N |
7036 | dv->found = found; |
7037 | dv->used = 0; | |
7038 | dv->next = *devlist; | |
7039 | *devlist = dv; | |
ca9de185 LM |
7040 | } |
7041 | } | |
4389ce73 | 7042 | close_fd(&fd); |
ca9de185 LM |
7043 | } |
7044 | } | |
7045 | free_mdstat(mdstat); | |
7046 | return count; | |
7047 | } | |
7048 | ||
7049 | #ifdef DEBUG_LOOP | |
7050 | static struct md_list* | |
7051 | get_loop_devices(void) | |
7052 | { | |
7053 | int i; | |
7054 | struct md_list *devlist = NULL; | |
594dc1b8 | 7055 | struct md_list *dv; |
ca9de185 LM |
7056 | |
7057 | for(i = 0; i < 12; i++) { | |
503975b9 N |
7058 | dv = xcalloc(1, sizeof(*dv)); |
7059 | dv->devname = xmalloc(40); | |
ca9de185 LM |
7060 | sprintf(dv->devname, "/dev/loop%d", i); |
7061 | dv->next = devlist; | |
7062 | devlist = dv; | |
7063 | } | |
7064 | return devlist; | |
7065 | } | |
7066 | #endif | |
7067 | ||
7068 | static struct md_list* | |
7069 | get_devices(const char *hba_path) | |
7070 | { | |
7071 | struct md_list *devlist = NULL; | |
594dc1b8 | 7072 | struct md_list *dv; |
ca9de185 LM |
7073 | struct dirent *ent; |
7074 | DIR *dir; | |
7075 | int err = 0; | |
7076 | ||
7077 | #if DEBUG_LOOP | |
7078 | devlist = get_loop_devices(); | |
7079 | return devlist; | |
7080 | #endif | |
7081 | /* scroll through /sys/dev/block looking for devices attached to | |
7082 | * this hba | |
7083 | */ | |
7084 | dir = opendir("/sys/dev/block"); | |
7085 | for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) { | |
7086 | int fd; | |
7087 | char buf[1024]; | |
7088 | int major, minor; | |
7089 | char *path = NULL; | |
7090 | if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2) | |
7091 | continue; | |
7c798f87 | 7092 | path = devt_to_devpath(makedev(major, minor), 1, NULL); |
ca9de185 LM |
7093 | if (!path) |
7094 | continue; | |
7095 | if (!path_attached_to_hba(path, hba_path)) { | |
7096 | free(path); | |
7097 | path = NULL; | |
7098 | continue; | |
7099 | } | |
7100 | free(path); | |
7101 | path = NULL; | |
7102 | fd = dev_open(ent->d_name, O_RDONLY); | |
4389ce73 | 7103 | if (is_fd_valid(fd)) { |
ca9de185 LM |
7104 | fd2devname(fd, buf); |
7105 | close(fd); | |
7106 | } else { | |
e7b84f9d | 7107 | pr_err("cannot open device: %s\n", |
ca9de185 LM |
7108 | ent->d_name); |
7109 | continue; | |
7110 | } | |
7111 | ||
503975b9 N |
7112 | dv = xcalloc(1, sizeof(*dv)); |
7113 | dv->devname = xstrdup(buf); | |
ca9de185 LM |
7114 | dv->next = devlist; |
7115 | devlist = dv; | |
7116 | } | |
7117 | if (err) { | |
7118 | while(devlist) { | |
7119 | dv = devlist; | |
7120 | devlist = devlist->next; | |
7121 | free(dv->devname); | |
7122 | free(dv); | |
7123 | } | |
7124 | } | |
562aa102 | 7125 | closedir(dir); |
ca9de185 LM |
7126 | return devlist; |
7127 | } | |
7128 | ||
7129 | static int | |
7130 | count_volumes_list(struct md_list *devlist, char *homehost, | |
7131 | int verbose, int *found) | |
7132 | { | |
7133 | struct md_list *tmpdev; | |
7134 | int count = 0; | |
594dc1b8 | 7135 | struct supertype *st; |
ca9de185 LM |
7136 | |
7137 | /* first walk the list of devices to find a consistent set | |
7138 | * that match the criterea, if that is possible. | |
7139 | * We flag the ones we like with 'used'. | |
7140 | */ | |
7141 | *found = 0; | |
7142 | st = match_metadata_desc_imsm("imsm"); | |
7143 | if (st == NULL) { | |
676e87a8 | 7144 | pr_vrb("cannot allocate memory for imsm supertype\n"); |
ca9de185 LM |
7145 | return 0; |
7146 | } | |
7147 | ||
7148 | for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) { | |
7149 | char *devname = tmpdev->devname; | |
0a6bff09 | 7150 | dev_t rdev; |
ca9de185 LM |
7151 | struct supertype *tst; |
7152 | int dfd; | |
7153 | if (tmpdev->used > 1) | |
7154 | continue; | |
7155 | tst = dup_super(st); | |
7156 | if (tst == NULL) { | |
676e87a8 | 7157 | pr_vrb("cannot allocate memory for imsm supertype\n"); |
ca9de185 LM |
7158 | goto err_1; |
7159 | } | |
7160 | tmpdev->container = 0; | |
7161 | dfd = dev_open(devname, O_RDONLY|O_EXCL); | |
4389ce73 | 7162 | if (!is_fd_valid(dfd)) { |
1ade5cc1 | 7163 | dprintf("cannot open device %s: %s\n", |
ca9de185 LM |
7164 | devname, strerror(errno)); |
7165 | tmpdev->used = 2; | |
0a6bff09 | 7166 | } else if (!fstat_is_blkdev(dfd, devname, &rdev)) { |
ca9de185 LM |
7167 | tmpdev->used = 2; |
7168 | } else if (must_be_container(dfd)) { | |
7169 | struct supertype *cst; | |
7170 | cst = super_by_fd(dfd, NULL); | |
7171 | if (cst == NULL) { | |
1ade5cc1 | 7172 | dprintf("cannot recognize container type %s\n", |
ca9de185 LM |
7173 | devname); |
7174 | tmpdev->used = 2; | |
7175 | } else if (tst->ss != st->ss) { | |
1ade5cc1 | 7176 | dprintf("non-imsm container - ignore it: %s\n", |
ca9de185 LM |
7177 | devname); |
7178 | tmpdev->used = 2; | |
7179 | } else if (!tst->ss->load_container || | |
7180 | tst->ss->load_container(tst, dfd, NULL)) | |
7181 | tmpdev->used = 2; | |
7182 | else { | |
7183 | tmpdev->container = 1; | |
7184 | } | |
7185 | if (cst) | |
7186 | cst->ss->free_super(cst); | |
7187 | } else { | |
0a6bff09 | 7188 | tmpdev->st_rdev = rdev; |
ca9de185 | 7189 | if (tst->ss->load_super(tst,dfd, NULL)) { |
1ade5cc1 | 7190 | dprintf("no RAID superblock on %s\n", |
ca9de185 LM |
7191 | devname); |
7192 | tmpdev->used = 2; | |
7193 | } else if (tst->ss->compare_super == NULL) { | |
1ade5cc1 | 7194 | dprintf("Cannot assemble %s metadata on %s\n", |
ca9de185 LM |
7195 | tst->ss->name, devname); |
7196 | tmpdev->used = 2; | |
7197 | } | |
7198 | } | |
4389ce73 MT |
7199 | close_fd(&dfd); |
7200 | ||
ca9de185 LM |
7201 | if (tmpdev->used == 2 || tmpdev->used == 4) { |
7202 | /* Ignore unrecognised devices during auto-assembly */ | |
7203 | goto loop; | |
7204 | } | |
7205 | else { | |
7206 | struct mdinfo info; | |
7207 | tst->ss->getinfo_super(tst, &info, NULL); | |
7208 | ||
7209 | if (st->minor_version == -1) | |
7210 | st->minor_version = tst->minor_version; | |
7211 | ||
7212 | if (memcmp(info.uuid, uuid_zero, | |
7213 | sizeof(int[4])) == 0) { | |
7214 | /* this is a floating spare. It cannot define | |
7215 | * an array unless there are no more arrays of | |
7216 | * this type to be found. It can be included | |
7217 | * in an array of this type though. | |
7218 | */ | |
7219 | tmpdev->used = 3; | |
7220 | goto loop; | |
7221 | } | |
7222 | ||
7223 | if (st->ss != tst->ss || | |
7224 | st->minor_version != tst->minor_version || | |
c7b8547c | 7225 | st->ss->compare_super(st, tst, 1) != 0) { |
ca9de185 LM |
7226 | /* Some mismatch. If exactly one array matches this host, |
7227 | * we can resolve on that one. | |
7228 | * Or, if we are auto assembling, we just ignore the second | |
7229 | * for now. | |
7230 | */ | |
1ade5cc1 | 7231 | dprintf("superblock on %s doesn't match others - assembly aborted\n", |
ca9de185 LM |
7232 | devname); |
7233 | goto loop; | |
7234 | } | |
7235 | tmpdev->used = 1; | |
7236 | *found = 1; | |
7237 | dprintf("found: devname: %s\n", devname); | |
7238 | } | |
7239 | loop: | |
7240 | if (tst) | |
7241 | tst->ss->free_super(tst); | |
7242 | } | |
7243 | if (*found != 0) { | |
7244 | int err; | |
7245 | if ((err = load_super_imsm_all(st, -1, &st->sb, NULL, devlist, 0)) == 0) { | |
7246 | struct mdinfo *iter, *head = st->ss->container_content(st, NULL); | |
7247 | for (iter = head; iter; iter = iter->next) { | |
7248 | dprintf("content->text_version: %s vol\n", | |
7249 | iter->text_version); | |
7250 | if (iter->array.state & (1<<MD_SB_BLOCK_VOLUME)) { | |
7251 | /* do not assemble arrays with unsupported | |
7252 | configurations */ | |
1ade5cc1 | 7253 | dprintf("Cannot activate member %s.\n", |
ca9de185 LM |
7254 | iter->text_version); |
7255 | } else | |
7256 | count++; | |
7257 | } | |
7258 | sysfs_free(head); | |
7259 | ||
7260 | } else { | |
1ade5cc1 | 7261 | dprintf("No valid super block on device list: err: %d %p\n", |
ca9de185 LM |
7262 | err, st->sb); |
7263 | } | |
7264 | } else { | |
1ade5cc1 | 7265 | dprintf("no more devices to examine\n"); |
ca9de185 LM |
7266 | } |
7267 | ||
7268 | for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) { | |
089f9d79 | 7269 | if (tmpdev->used == 1 && tmpdev->found) { |
ca9de185 LM |
7270 | if (count) { |
7271 | if (count < tmpdev->found) | |
7272 | count = 0; | |
7273 | else | |
7274 | count -= tmpdev->found; | |
7275 | } | |
7276 | } | |
7277 | if (tmpdev->used == 1) | |
7278 | tmpdev->used = 4; | |
7279 | } | |
7280 | err_1: | |
7281 | if (st) | |
7282 | st->ss->free_super(st); | |
7283 | return count; | |
7284 | } | |
7285 | ||
d3c11416 AO |
7286 | static int __count_volumes(char *hba_path, int dpa, int verbose, |
7287 | int cmp_hba_path) | |
ca9de185 | 7288 | { |
72a45777 | 7289 | struct sys_dev *idev, *intel_devices = find_intel_devices(); |
ca9de185 | 7290 | int count = 0; |
72a45777 PB |
7291 | const struct orom_entry *entry; |
7292 | struct devid_list *dv, *devid_list; | |
ca9de185 | 7293 | |
d3c11416 | 7294 | if (!hba_path) |
ca9de185 LM |
7295 | return 0; |
7296 | ||
72a45777 | 7297 | for (idev = intel_devices; idev; idev = idev->next) { |
d3c11416 AO |
7298 | if (strstr(idev->path, hba_path)) |
7299 | break; | |
72a45777 PB |
7300 | } |
7301 | ||
7302 | if (!idev || !idev->dev_id) | |
ca9de185 | 7303 | return 0; |
72a45777 PB |
7304 | |
7305 | entry = get_orom_entry_by_device_id(idev->dev_id); | |
7306 | ||
7307 | if (!entry || !entry->devid_list) | |
7308 | return 0; | |
7309 | ||
7310 | devid_list = entry->devid_list; | |
7311 | for (dv = devid_list; dv; dv = dv->next) { | |
594dc1b8 | 7312 | struct md_list *devlist; |
d3c11416 AO |
7313 | struct sys_dev *device = NULL; |
7314 | char *hpath; | |
72a45777 PB |
7315 | int found = 0; |
7316 | ||
d3c11416 AO |
7317 | if (cmp_hba_path) |
7318 | device = device_by_id_and_path(dv->devid, hba_path); | |
7319 | else | |
7320 | device = device_by_id(dv->devid); | |
7321 | ||
72a45777 | 7322 | if (device) |
d3c11416 | 7323 | hpath = device->path; |
72a45777 PB |
7324 | else |
7325 | return 0; | |
7326 | ||
d3c11416 | 7327 | devlist = get_devices(hpath); |
72a45777 PB |
7328 | /* if no intel devices return zero volumes */ |
7329 | if (devlist == NULL) | |
7330 | return 0; | |
7331 | ||
d3c11416 AO |
7332 | count += active_arrays_by_format("imsm", hpath, &devlist, dpa, |
7333 | verbose); | |
7334 | dprintf("path: %s active arrays: %d\n", hpath, count); | |
72a45777 PB |
7335 | if (devlist == NULL) |
7336 | return 0; | |
7337 | do { | |
7338 | found = 0; | |
7339 | count += count_volumes_list(devlist, | |
7340 | NULL, | |
7341 | verbose, | |
7342 | &found); | |
7343 | dprintf("found %d count: %d\n", found, count); | |
7344 | } while (found); | |
7345 | ||
d3c11416 | 7346 | dprintf("path: %s total number of volumes: %d\n", hpath, count); |
72a45777 PB |
7347 | |
7348 | while (devlist) { | |
7349 | struct md_list *dv = devlist; | |
7350 | devlist = devlist->next; | |
7351 | free(dv->devname); | |
7352 | free(dv); | |
7353 | } | |
ca9de185 LM |
7354 | } |
7355 | return count; | |
7356 | } | |
7357 | ||
d3c11416 AO |
7358 | static int count_volumes(struct intel_hba *hba, int dpa, int verbose) |
7359 | { | |
7360 | if (!hba) | |
7361 | return 0; | |
7362 | if (hba->type == SYS_DEV_VMD) { | |
7363 | struct sys_dev *dev; | |
7364 | int count = 0; | |
7365 | ||
7366 | for (dev = find_intel_devices(); dev; dev = dev->next) { | |
7367 | if (dev->type == SYS_DEV_VMD) | |
7368 | count += __count_volumes(dev->path, dpa, | |
7369 | verbose, 1); | |
7370 | } | |
7371 | return count; | |
7372 | } | |
7373 | return __count_volumes(hba->path, dpa, verbose, 0); | |
7374 | } | |
7375 | ||
cd9d1ac7 DW |
7376 | static int imsm_default_chunk(const struct imsm_orom *orom) |
7377 | { | |
7378 | /* up to 512 if the plaform supports it, otherwise the platform max. | |
7379 | * 128 if no platform detected | |
7380 | */ | |
7381 | int fs = max(7, orom ? fls(orom->sss) : 0); | |
7382 | ||
7383 | return min(512, (1 << fs)); | |
7384 | } | |
73408129 | 7385 | |
6592ce37 DW |
7386 | static int |
7387 | validate_geometry_imsm_orom(struct intel_super *super, int level, int layout, | |
2cc699af | 7388 | int raiddisks, int *chunk, unsigned long long size, int verbose) |
6592ce37 | 7389 | { |
660260d0 DW |
7390 | /* check/set platform and metadata limits/defaults */ |
7391 | if (super->orom && raiddisks > super->orom->dpa) { | |
676e87a8 | 7392 | pr_vrb("platform supports a maximum of %d disks per array\n", |
660260d0 | 7393 | super->orom->dpa); |
73408129 LM |
7394 | return 0; |
7395 | } | |
7396 | ||
5d500228 | 7397 | /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */ |
660260d0 | 7398 | if (!is_raid_level_supported(super->orom, level, raiddisks)) { |
676e87a8 | 7399 | pr_vrb("platform does not support raid%d with %d disk%s\n", |
6592ce37 DW |
7400 | level, raiddisks, raiddisks > 1 ? "s" : ""); |
7401 | return 0; | |
7402 | } | |
cd9d1ac7 | 7403 | |
7ccc4cc4 | 7404 | if (*chunk == 0 || *chunk == UnSet) |
cd9d1ac7 DW |
7405 | *chunk = imsm_default_chunk(super->orom); |
7406 | ||
7ccc4cc4 | 7407 | if (super->orom && !imsm_orom_has_chunk(super->orom, *chunk)) { |
676e87a8 | 7408 | pr_vrb("platform does not support a chunk size of: %d\n", *chunk); |
cd9d1ac7 | 7409 | return 0; |
6592ce37 | 7410 | } |
cd9d1ac7 | 7411 | |
6592ce37 DW |
7412 | if (layout != imsm_level_to_layout(level)) { |
7413 | if (level == 5) | |
676e87a8 | 7414 | pr_vrb("imsm raid 5 only supports the left-asymmetric layout\n"); |
6592ce37 | 7415 | else if (level == 10) |
676e87a8 | 7416 | pr_vrb("imsm raid 10 only supports the n2 layout\n"); |
6592ce37 | 7417 | else |
676e87a8 | 7418 | pr_vrb("imsm unknown layout %#x for this raid level %d\n", |
6592ce37 DW |
7419 | layout, level); |
7420 | return 0; | |
7421 | } | |
2cc699af | 7422 | |
7ccc4cc4 | 7423 | if (super->orom && (super->orom->attr & IMSM_OROM_ATTR_2TB) == 0 && |
2cc699af | 7424 | (calc_array_size(level, raiddisks, layout, *chunk, size) >> 32) > 0) { |
676e87a8 | 7425 | pr_vrb("platform does not support a volume size over 2TB\n"); |
2cc699af CA |
7426 | return 0; |
7427 | } | |
614902f6 | 7428 | |
6592ce37 DW |
7429 | return 1; |
7430 | } | |
7431 | ||
1011e834 | 7432 | /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd |
c2c087e6 DW |
7433 | * FIX ME add ahci details |
7434 | */ | |
8b353278 | 7435 | static int validate_geometry_imsm_volume(struct supertype *st, int level, |
c21e737b | 7436 | int layout, int raiddisks, int *chunk, |
af4348dd N |
7437 | unsigned long long size, |
7438 | unsigned long long data_offset, | |
7439 | char *dev, | |
2c514b71 NB |
7440 | unsigned long long *freesize, |
7441 | int verbose) | |
cdddbdbc | 7442 | { |
9e04ac1c | 7443 | dev_t rdev; |
c2c087e6 | 7444 | struct intel_super *super = st->sb; |
b2916f25 | 7445 | struct imsm_super *mpb; |
c2c087e6 DW |
7446 | struct dl *dl; |
7447 | unsigned long long pos = 0; | |
7448 | unsigned long long maxsize; | |
7449 | struct extent *e; | |
7450 | int i; | |
cdddbdbc | 7451 | |
88c32bb1 DW |
7452 | /* We must have the container info already read in. */ |
7453 | if (!super) | |
c2c087e6 DW |
7454 | return 0; |
7455 | ||
b2916f25 JS |
7456 | mpb = super->anchor; |
7457 | ||
2cc699af | 7458 | if (!validate_geometry_imsm_orom(super, level, layout, raiddisks, chunk, size, verbose)) { |
3e684231 | 7459 | pr_err("RAID geometry validation failed. Cannot proceed with the action(s).\n"); |
c2c087e6 | 7460 | return 0; |
d54559f0 | 7461 | } |
c2c087e6 DW |
7462 | if (!dev) { |
7463 | /* General test: make sure there is space for | |
2da8544a DW |
7464 | * 'raiddisks' device extents of size 'size' at a given |
7465 | * offset | |
c2c087e6 | 7466 | */ |
e46273eb | 7467 | unsigned long long minsize = size; |
b7528a20 | 7468 | unsigned long long start_offset = MaxSector; |
c2c087e6 DW |
7469 | int dcnt = 0; |
7470 | if (minsize == 0) | |
7471 | minsize = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS; | |
7472 | for (dl = super->disks; dl ; dl = dl->next) { | |
7473 | int found = 0; | |
7474 | ||
bf5a934a | 7475 | pos = 0; |
c2c087e6 | 7476 | i = 0; |
05501181 | 7477 | e = get_extents(super, dl, 0); |
c2c087e6 DW |
7478 | if (!e) continue; |
7479 | do { | |
7480 | unsigned long long esize; | |
7481 | esize = e[i].start - pos; | |
7482 | if (esize >= minsize) | |
7483 | found = 1; | |
b7528a20 | 7484 | if (found && start_offset == MaxSector) { |
2da8544a DW |
7485 | start_offset = pos; |
7486 | break; | |
7487 | } else if (found && pos != start_offset) { | |
7488 | found = 0; | |
7489 | break; | |
7490 | } | |
c2c087e6 DW |
7491 | pos = e[i].start + e[i].size; |
7492 | i++; | |
7493 | } while (e[i-1].size); | |
7494 | if (found) | |
7495 | dcnt++; | |
7496 | free(e); | |
7497 | } | |
7498 | if (dcnt < raiddisks) { | |
2c514b71 | 7499 | if (verbose) |
7a862a02 | 7500 | pr_err("imsm: Not enough devices with space for this array (%d < %d)\n", |
2c514b71 | 7501 | dcnt, raiddisks); |
c2c087e6 DW |
7502 | return 0; |
7503 | } | |
7504 | return 1; | |
7505 | } | |
0dcecb2e | 7506 | |
c2c087e6 | 7507 | /* This device must be a member of the set */ |
9e04ac1c | 7508 | if (!stat_is_blkdev(dev, &rdev)) |
c2c087e6 DW |
7509 | return 0; |
7510 | for (dl = super->disks ; dl ; dl = dl->next) { | |
9e04ac1c ZL |
7511 | if (dl->major == (int)major(rdev) && |
7512 | dl->minor == (int)minor(rdev)) | |
c2c087e6 DW |
7513 | break; |
7514 | } | |
7515 | if (!dl) { | |
2c514b71 | 7516 | if (verbose) |
7a862a02 | 7517 | pr_err("%s is not in the same imsm set\n", dev); |
c2c087e6 | 7518 | return 0; |
a20d2ba5 DW |
7519 | } else if (super->orom && dl->index < 0 && mpb->num_raid_devs) { |
7520 | /* If a volume is present then the current creation attempt | |
7521 | * cannot incorporate new spares because the orom may not | |
7522 | * understand this configuration (all member disks must be | |
7523 | * members of each array in the container). | |
7524 | */ | |
7a862a02 N |
7525 | pr_err("%s is a spare and a volume is already defined for this container\n", dev); |
7526 | pr_err("The option-rom requires all member disks to be a member of all volumes\n"); | |
a20d2ba5 | 7527 | return 0; |
5fe62b94 WD |
7528 | } else if (super->orom && mpb->num_raid_devs > 0 && |
7529 | mpb->num_disks != raiddisks) { | |
7a862a02 | 7530 | pr_err("The option-rom requires all member disks to be a member of all volumes\n"); |
5fe62b94 | 7531 | return 0; |
c2c087e6 | 7532 | } |
0dcecb2e DW |
7533 | |
7534 | /* retrieve the largest free space block */ | |
05501181 | 7535 | e = get_extents(super, dl, 0); |
c2c087e6 DW |
7536 | maxsize = 0; |
7537 | i = 0; | |
0dcecb2e DW |
7538 | if (e) { |
7539 | do { | |
7540 | unsigned long long esize; | |
7541 | ||
7542 | esize = e[i].start - pos; | |
7543 | if (esize >= maxsize) | |
7544 | maxsize = esize; | |
7545 | pos = e[i].start + e[i].size; | |
7546 | i++; | |
7547 | } while (e[i-1].size); | |
7548 | dl->e = e; | |
7549 | dl->extent_cnt = i; | |
7550 | } else { | |
7551 | if (verbose) | |
e7b84f9d | 7552 | pr_err("unable to determine free space for: %s\n", |
0dcecb2e DW |
7553 | dev); |
7554 | return 0; | |
7555 | } | |
7556 | if (maxsize < size) { | |
7557 | if (verbose) | |
e7b84f9d | 7558 | pr_err("%s not enough space (%llu < %llu)\n", |
0dcecb2e DW |
7559 | dev, maxsize, size); |
7560 | return 0; | |
7561 | } | |
7562 | ||
aa19fdd4 | 7563 | maxsize = merge_extents(super, false); |
3baa56ab | 7564 | |
1a1ced1e KS |
7565 | if (mpb->num_raid_devs > 0 && size && size != maxsize) |
7566 | pr_err("attempting to create a second volume with size less then remaining space.\n"); | |
3baa56ab | 7567 | |
a7dd165b | 7568 | if (maxsize < size || maxsize == 0) { |
b3071342 LD |
7569 | if (verbose) { |
7570 | if (maxsize == 0) | |
7a862a02 | 7571 | pr_err("no free space left on device. Aborting...\n"); |
b3071342 | 7572 | else |
7a862a02 | 7573 | pr_err("not enough space to create volume of given size (%llu < %llu). Aborting...\n", |
b3071342 LD |
7574 | maxsize, size); |
7575 | } | |
0dcecb2e | 7576 | return 0; |
0dcecb2e DW |
7577 | } |
7578 | ||
c2c087e6 DW |
7579 | *freesize = maxsize; |
7580 | ||
ca9de185 | 7581 | if (super->orom) { |
72a45777 | 7582 | int count = count_volumes(super->hba, |
ca9de185 LM |
7583 | super->orom->dpa, verbose); |
7584 | if (super->orom->vphba <= count) { | |
676e87a8 | 7585 | pr_vrb("platform does not support more than %d raid volumes.\n", |
ca9de185 LM |
7586 | super->orom->vphba); |
7587 | return 0; | |
7588 | } | |
7589 | } | |
c2c087e6 | 7590 | return 1; |
cdddbdbc DW |
7591 | } |
7592 | ||
6d4d9ab2 MT |
7593 | /** |
7594 | * imsm_get_free_size() - get the biggest, common free space from members. | |
7595 | * @super: &intel_super pointer, not NULL. | |
7596 | * @raiddisks: number of raid disks. | |
7597 | * @size: requested size, could be 0 (means max size). | |
5f027b93 | 7598 | * @chunk: requested chunk size in KiB. |
6d4d9ab2 MT |
7599 | * @freesize: pointer for returned size value. |
7600 | * | |
7601 | * Return: &IMSM_STATUS_OK or &IMSM_STATUS_ERROR. | |
7602 | * | |
7603 | * @freesize is set to meaningful value, this can be @size, or calculated | |
7604 | * max free size. | |
7605 | * super->create_offset value is modified and set appropriately in | |
7606 | * merge_extends() for further creation. | |
7607 | */ | |
7608 | static imsm_status_t imsm_get_free_size(struct intel_super *super, | |
7609 | const int raiddisks, | |
7610 | unsigned long long size, | |
7611 | const int chunk, | |
aa19fdd4 MT |
7612 | unsigned long long *freesize, |
7613 | bool expanding) | |
efb30e7f | 7614 | { |
efb30e7f DW |
7615 | struct imsm_super *mpb = super->anchor; |
7616 | struct dl *dl; | |
7617 | int i; | |
efb30e7f | 7618 | struct extent *e; |
5f027b93 MT |
7619 | int cnt = 0; |
7620 | int used = 0; | |
efb30e7f | 7621 | unsigned long long maxsize; |
5f027b93 MT |
7622 | unsigned long long minsize = size; |
7623 | ||
7624 | if (minsize == 0) | |
7625 | minsize = chunk * 2; | |
efb30e7f DW |
7626 | |
7627 | /* find the largest common start free region of the possible disks */ | |
efb30e7f DW |
7628 | for (dl = super->disks; dl; dl = dl->next) { |
7629 | dl->raiddisk = -1; | |
7630 | ||
7631 | if (dl->index >= 0) | |
7632 | used++; | |
7633 | ||
7634 | /* don't activate new spares if we are orom constrained | |
7635 | * and there is already a volume active in the container | |
7636 | */ | |
7637 | if (super->orom && dl->index < 0 && mpb->num_raid_devs) | |
7638 | continue; | |
7639 | ||
05501181 | 7640 | e = get_extents(super, dl, 0); |
efb30e7f DW |
7641 | if (!e) |
7642 | continue; | |
7643 | for (i = 1; e[i-1].size; i++) | |
7644 | ; | |
7645 | dl->e = e; | |
7646 | dl->extent_cnt = i; | |
efb30e7f DW |
7647 | cnt++; |
7648 | } | |
7649 | ||
aa19fdd4 | 7650 | maxsize = merge_extents(super, expanding); |
5f027b93 MT |
7651 | if (maxsize < minsize) { |
7652 | pr_err("imsm: Free space is %llu but must be equal or larger than %llu.\n", | |
7653 | maxsize, minsize); | |
7654 | return IMSM_STATUS_ERROR; | |
7655 | } | |
efb30e7f | 7656 | |
5f027b93 MT |
7657 | if (cnt < raiddisks || (super->orom && used && used != raiddisks)) { |
7658 | pr_err("imsm: Not enough devices with space to create array.\n"); | |
6d4d9ab2 | 7659 | return IMSM_STATUS_ERROR; |
efb30e7f DW |
7660 | } |
7661 | ||
7662 | if (size == 0) { | |
7663 | size = maxsize; | |
7664 | if (chunk) { | |
612e59d8 CA |
7665 | size /= 2 * chunk; |
7666 | size *= 2 * chunk; | |
efb30e7f | 7667 | } |
f878b242 LM |
7668 | maxsize = size; |
7669 | } | |
1a1ced1e KS |
7670 | if (mpb->num_raid_devs > 0 && size && size != maxsize) |
7671 | pr_err("attempting to create a second volume with size less then remaining space.\n"); | |
efb30e7f DW |
7672 | *freesize = size; |
7673 | ||
13bcac90 AK |
7674 | dprintf("imsm: imsm_get_free_size() returns : %llu\n", size); |
7675 | ||
6d4d9ab2 | 7676 | return IMSM_STATUS_OK; |
efb30e7f DW |
7677 | } |
7678 | ||
6d4d9ab2 MT |
7679 | /** |
7680 | * autolayout_imsm() - automatically layout a new volume. | |
7681 | * @super: &intel_super pointer, not NULL. | |
7682 | * @raiddisks: number of raid disks. | |
7683 | * @size: requested size, could be 0 (means max size). | |
7684 | * @chunk: requested chunk. | |
7685 | * @freesize: pointer for returned size value. | |
7686 | * | |
7687 | * We are being asked to automatically layout a new volume based on the current | |
7688 | * contents of the container. If the parameters can be satisfied autolayout_imsm | |
7689 | * will record the disks, start offset, and will return size of the volume to | |
7690 | * be created. See imsm_get_free_size() for details. | |
7691 | * add_to_super() and getinfo_super() detect when autolayout is in progress. | |
7692 | * If first volume exists, slots are set consistently to it. | |
7693 | * | |
7694 | * Return: &IMSM_STATUS_OK on success, &IMSM_STATUS_ERROR otherwise. | |
7695 | * | |
7696 | * Disks are marked for creation via dl->raiddisk. | |
7697 | */ | |
7698 | static imsm_status_t autolayout_imsm(struct intel_super *super, | |
7699 | const int raiddisks, | |
7700 | unsigned long long size, const int chunk, | |
7701 | unsigned long long *freesize) | |
13bcac90 | 7702 | { |
6d4d9ab2 MT |
7703 | int curr_slot = 0; |
7704 | struct dl *disk; | |
7705 | int vol_cnt = super->anchor->num_raid_devs; | |
7706 | imsm_status_t rv; | |
13bcac90 | 7707 | |
aa19fdd4 | 7708 | rv = imsm_get_free_size(super, raiddisks, size, chunk, freesize, false); |
6d4d9ab2 MT |
7709 | if (rv != IMSM_STATUS_OK) |
7710 | return IMSM_STATUS_ERROR; | |
7711 | ||
7712 | for (disk = super->disks; disk; disk = disk->next) { | |
7713 | if (!disk->e) | |
7714 | continue; | |
7715 | ||
7716 | if (curr_slot == raiddisks) | |
7717 | break; | |
7718 | ||
7719 | if (vol_cnt == 0) { | |
7720 | disk->raiddisk = curr_slot; | |
7721 | } else { | |
7722 | int _slot = get_disk_slot_in_dev(super, 0, disk->index); | |
7723 | ||
7724 | if (_slot == -1) { | |
7725 | pr_err("Disk %s is not used in first volume, aborting\n", | |
7726 | disk->devname); | |
7727 | return IMSM_STATUS_ERROR; | |
7728 | } | |
7729 | disk->raiddisk = _slot; | |
7730 | } | |
7731 | curr_slot++; | |
13bcac90 AK |
7732 | } |
7733 | ||
6d4d9ab2 | 7734 | return IMSM_STATUS_OK; |
13bcac90 AK |
7735 | } |
7736 | ||
bf5a934a | 7737 | static int validate_geometry_imsm(struct supertype *st, int level, int layout, |
c21e737b | 7738 | int raiddisks, int *chunk, unsigned long long size, |
af4348dd | 7739 | unsigned long long data_offset, |
bf5a934a | 7740 | char *dev, unsigned long long *freesize, |
5308f117 | 7741 | int consistency_policy, int verbose) |
bf5a934a DW |
7742 | { |
7743 | int fd, cfd; | |
7744 | struct mdinfo *sra; | |
20cbe8d2 | 7745 | int is_member = 0; |
bf5a934a | 7746 | |
d54559f0 LM |
7747 | /* load capability |
7748 | * if given unused devices create a container | |
bf5a934a DW |
7749 | * if given given devices in a container create a member volume |
7750 | */ | |
6f2af6a4 | 7751 | if (is_container(level)) |
bf5a934a | 7752 | /* Must be a fresh device to add to a container */ |
1f5d54a0 MT |
7753 | return validate_geometry_imsm_container(st, level, raiddisks, |
7754 | data_offset, dev, | |
7755 | freesize, verbose); | |
9587c373 | 7756 | |
06a6101c BK |
7757 | /* |
7758 | * Size is given in sectors. | |
7759 | */ | |
7760 | if (size && (size < 2048)) { | |
22dc741f | 7761 | pr_err("Given size must be greater than 1M.\n"); |
54865c30 RS |
7762 | /* Depends on algorithm in Create.c : |
7763 | * if container was given (dev == NULL) return -1, | |
7764 | * if block device was given ( dev != NULL) return 0. | |
7765 | */ | |
7766 | return dev ? -1 : 0; | |
7767 | } | |
7768 | ||
8592f29d | 7769 | if (!dev) { |
6d4d9ab2 MT |
7770 | struct intel_super *super = st->sb; |
7771 | ||
7772 | /* | |
071f839e | 7773 | * Autolayout mode, st->sb must be set. |
6d4d9ab2 | 7774 | */ |
071f839e KT |
7775 | if (!super) { |
7776 | pr_vrb("superblock must be set for autolayout, aborting\n"); | |
7777 | return 0; | |
6d4d9ab2 MT |
7778 | } |
7779 | ||
7780 | if (!validate_geometry_imsm_orom(st->sb, level, layout, | |
7781 | raiddisks, chunk, size, | |
7782 | verbose)) | |
7783 | return 0; | |
7784 | ||
071f839e | 7785 | if (super->orom && freesize) { |
6d4d9ab2 MT |
7786 | imsm_status_t rv; |
7787 | int count = count_volumes(super->hba, super->orom->dpa, | |
7788 | verbose); | |
7789 | if (super->orom->vphba <= count) { | |
7790 | pr_vrb("platform does not support more than %d raid volumes.\n", | |
7791 | super->orom->vphba); | |
e91a3bad | 7792 | return 0; |
ca9de185 | 7793 | } |
6d4d9ab2 MT |
7794 | |
7795 | rv = autolayout_imsm(super, raiddisks, size, *chunk, | |
7796 | freesize); | |
7797 | if (rv != IMSM_STATUS_OK) | |
7798 | return 0; | |
8592f29d N |
7799 | } |
7800 | return 1; | |
7801 | } | |
bf5a934a DW |
7802 | if (st->sb) { |
7803 | /* creating in a given container */ | |
7804 | return validate_geometry_imsm_volume(st, level, layout, | |
7805 | raiddisks, chunk, size, | |
af4348dd | 7806 | data_offset, |
bf5a934a DW |
7807 | dev, freesize, verbose); |
7808 | } | |
7809 | ||
bf5a934a DW |
7810 | /* This device needs to be a device in an 'imsm' container */ |
7811 | fd = open(dev, O_RDONLY|O_EXCL, 0); | |
4389ce73 MT |
7812 | |
7813 | if (is_fd_valid(fd)) { | |
7814 | pr_vrb("Cannot create this array on device %s\n", dev); | |
bf5a934a DW |
7815 | close(fd); |
7816 | return 0; | |
7817 | } | |
4389ce73 MT |
7818 | if (errno == EBUSY) |
7819 | fd = open(dev, O_RDONLY, 0); | |
7820 | ||
7821 | if (!is_fd_valid(fd)) { | |
7822 | pr_vrb("Cannot open %s: %s\n", dev, strerror(errno)); | |
bf5a934a DW |
7823 | return 0; |
7824 | } | |
4389ce73 | 7825 | |
bf5a934a DW |
7826 | /* Well, it is in use by someone, maybe an 'imsm' container. */ |
7827 | cfd = open_container(fd); | |
4389ce73 MT |
7828 | close_fd(&fd); |
7829 | ||
7830 | if (!is_fd_valid(cfd)) { | |
7831 | pr_vrb("Cannot use %s: It is busy\n", dev); | |
bf5a934a DW |
7832 | return 0; |
7833 | } | |
4dd2df09 | 7834 | sra = sysfs_read(cfd, NULL, GET_VERSION); |
bf5a934a | 7835 | if (sra && sra->array.major_version == -1 && |
20cbe8d2 AW |
7836 | strcmp(sra->text_version, "imsm") == 0) |
7837 | is_member = 1; | |
7838 | sysfs_free(sra); | |
7839 | if (is_member) { | |
bf5a934a DW |
7840 | /* This is a member of a imsm container. Load the container |
7841 | * and try to create a volume | |
7842 | */ | |
7843 | struct intel_super *super; | |
7844 | ||
ec50f7b6 | 7845 | if (load_super_imsm_all(st, cfd, (void **) &super, NULL, NULL, 1) == 0) { |
bf5a934a | 7846 | st->sb = super; |
4dd2df09 | 7847 | strcpy(st->container_devnm, fd2devnm(cfd)); |
bf5a934a DW |
7848 | close(cfd); |
7849 | return validate_geometry_imsm_volume(st, level, layout, | |
7850 | raiddisks, chunk, | |
af4348dd | 7851 | size, data_offset, dev, |
ecbd9e81 N |
7852 | freesize, 1) |
7853 | ? 1 : -1; | |
bf5a934a | 7854 | } |
20cbe8d2 | 7855 | } |
bf5a934a | 7856 | |
20cbe8d2 | 7857 | if (verbose) |
e7b84f9d | 7858 | pr_err("failed container membership check\n"); |
20cbe8d2 AW |
7859 | |
7860 | close(cfd); | |
7861 | return 0; | |
bf5a934a | 7862 | } |
0bd16cf2 | 7863 | |
30f58b22 | 7864 | static void default_geometry_imsm(struct supertype *st, int *level, int *layout, int *chunk) |
0bd16cf2 DJ |
7865 | { |
7866 | struct intel_super *super = st->sb; | |
7867 | ||
30f58b22 DW |
7868 | if (level && *level == UnSet) |
7869 | *level = LEVEL_CONTAINER; | |
7870 | ||
7871 | if (level && layout && *layout == UnSet) | |
7872 | *layout = imsm_level_to_layout(*level); | |
0bd16cf2 | 7873 | |
cd9d1ac7 DW |
7874 | if (chunk && (*chunk == UnSet || *chunk == 0)) |
7875 | *chunk = imsm_default_chunk(super->orom); | |
0bd16cf2 DJ |
7876 | } |
7877 | ||
33414a01 DW |
7878 | static void handle_missing(struct intel_super *super, struct imsm_dev *dev); |
7879 | ||
3364781b | 7880 | static int kill_subarray_imsm(struct supertype *st, char *subarray_id) |
33414a01 | 7881 | { |
3364781b | 7882 | /* remove the subarray currently referenced by subarray_id */ |
33414a01 DW |
7883 | __u8 i; |
7884 | struct intel_dev **dp; | |
7885 | struct intel_super *super = st->sb; | |
3364781b | 7886 | __u8 current_vol = strtoul(subarray_id, NULL, 10); |
33414a01 DW |
7887 | struct imsm_super *mpb = super->anchor; |
7888 | ||
3364781b | 7889 | if (mpb->num_raid_devs == 0) |
33414a01 | 7890 | return 2; |
33414a01 DW |
7891 | |
7892 | /* block deletions that would change the uuid of active subarrays | |
7893 | * | |
7894 | * FIXME when immutable ids are available, but note that we'll | |
7895 | * also need to fixup the invalidated/active subarray indexes in | |
7896 | * mdstat | |
7897 | */ | |
7898 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
7899 | char subarray[4]; | |
7900 | ||
7901 | if (i < current_vol) | |
7902 | continue; | |
dd5ab402 | 7903 | snprintf(subarray, sizeof(subarray), "%u", i); |
4dd2df09 | 7904 | if (is_subarray_active(subarray, st->devnm)) { |
e7b84f9d N |
7905 | pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n", |
7906 | current_vol, i); | |
33414a01 DW |
7907 | |
7908 | return 2; | |
7909 | } | |
7910 | } | |
7911 | ||
7912 | if (st->update_tail) { | |
503975b9 | 7913 | struct imsm_update_kill_array *u = xmalloc(sizeof(*u)); |
33414a01 | 7914 | |
33414a01 DW |
7915 | u->type = update_kill_array; |
7916 | u->dev_idx = current_vol; | |
7917 | append_metadata_update(st, u, sizeof(*u)); | |
7918 | ||
7919 | return 0; | |
7920 | } | |
7921 | ||
7922 | for (dp = &super->devlist; *dp;) | |
7923 | if ((*dp)->index == current_vol) { | |
7924 | *dp = (*dp)->next; | |
7925 | } else { | |
7926 | handle_missing(super, (*dp)->dev); | |
7927 | if ((*dp)->index > current_vol) | |
7928 | (*dp)->index--; | |
7929 | dp = &(*dp)->next; | |
7930 | } | |
7931 | ||
7932 | /* no more raid devices, all active components are now spares, | |
7933 | * but of course failed are still failed | |
7934 | */ | |
7935 | if (--mpb->num_raid_devs == 0) { | |
7936 | struct dl *d; | |
7937 | ||
7938 | for (d = super->disks; d; d = d->next) | |
a8619d23 AK |
7939 | if (d->index > -2) |
7940 | mark_spare(d); | |
33414a01 DW |
7941 | } |
7942 | ||
7943 | super->updates_pending++; | |
7944 | ||
7945 | return 0; | |
7946 | } | |
aa534678 | 7947 | |
4345e135 MK |
7948 | /** |
7949 | * get_rwh_policy_from_update() - Get the rwh policy for update option. | |
7950 | * @update: Update option. | |
7951 | */ | |
7952 | static int get_rwh_policy_from_update(enum update_opt update) | |
19ad203e | 7953 | { |
4345e135 MK |
7954 | switch (update) { |
7955 | case UOPT_PPL: | |
19ad203e | 7956 | return RWH_MULTIPLE_DISTRIBUTED; |
4345e135 | 7957 | case UOPT_NO_PPL: |
19ad203e | 7958 | return RWH_MULTIPLE_OFF; |
4345e135 | 7959 | case UOPT_BITMAP: |
19ad203e | 7960 | return RWH_BITMAP; |
4345e135 | 7961 | case UOPT_NO_BITMAP: |
19ad203e | 7962 | return RWH_OFF; |
4345e135 MK |
7963 | default: |
7964 | break; | |
7965 | } | |
7966 | return UOPT_UNDEFINED; | |
19ad203e JR |
7967 | } |
7968 | ||
a951a4f7 | 7969 | static int update_subarray_imsm(struct supertype *st, char *subarray, |
03312b52 | 7970 | enum update_opt update, struct mddev_ident *ident) |
aa534678 DW |
7971 | { |
7972 | /* update the subarray currently referenced by ->current_vol */ | |
7973 | struct intel_super *super = st->sb; | |
7974 | struct imsm_super *mpb = super->anchor; | |
7975 | ||
03312b52 | 7976 | if (update == UOPT_NAME) { |
aa534678 | 7977 | char *name = ident->name; |
a951a4f7 N |
7978 | char *ep; |
7979 | int vol; | |
aa534678 | 7980 | |
e2eb503b | 7981 | if (imsm_is_name_allowed(super, name, 1) == false) |
aa534678 DW |
7982 | return 2; |
7983 | ||
a951a4f7 N |
7984 | vol = strtoul(subarray, &ep, 10); |
7985 | if (*ep != '\0' || vol >= super->anchor->num_raid_devs) | |
7986 | return 2; | |
7987 | ||
aa534678 | 7988 | if (st->update_tail) { |
503975b9 | 7989 | struct imsm_update_rename_array *u = xmalloc(sizeof(*u)); |
aa534678 | 7990 | |
aa534678 | 7991 | u->type = update_rename_array; |
a951a4f7 | 7992 | u->dev_idx = vol; |
618f4e6d XN |
7993 | strncpy((char *) u->name, name, MAX_RAID_SERIAL_LEN); |
7994 | u->name[MAX_RAID_SERIAL_LEN-1] = '\0'; | |
aa534678 DW |
7995 | append_metadata_update(st, u, sizeof(*u)); |
7996 | } else { | |
7997 | struct imsm_dev *dev; | |
ebad3af2 | 7998 | int i, namelen; |
aa534678 | 7999 | |
a951a4f7 | 8000 | dev = get_imsm_dev(super, vol); |
ebad3af2 JS |
8001 | memset(dev->volume, '\0', MAX_RAID_SERIAL_LEN); |
8002 | namelen = min((int)strlen(name), MAX_RAID_SERIAL_LEN); | |
8003 | memcpy(dev->volume, name, namelen); | |
aa534678 DW |
8004 | for (i = 0; i < mpb->num_raid_devs; i++) { |
8005 | dev = get_imsm_dev(super, i); | |
8006 | handle_missing(super, dev); | |
8007 | } | |
8008 | super->updates_pending++; | |
8009 | } | |
03312b52 | 8010 | } else if (get_rwh_policy_from_update(update) != UOPT_UNDEFINED) { |
e6e9dd3f AP |
8011 | int new_policy; |
8012 | char *ep; | |
8013 | int vol = strtoul(subarray, &ep, 10); | |
8014 | ||
8015 | if (*ep != '\0' || vol >= super->anchor->num_raid_devs) | |
8016 | return 2; | |
8017 | ||
03312b52 | 8018 | new_policy = get_rwh_policy_from_update(update); |
e6e9dd3f AP |
8019 | |
8020 | if (st->update_tail) { | |
8021 | struct imsm_update_rwh_policy *u = xmalloc(sizeof(*u)); | |
8022 | ||
8023 | u->type = update_rwh_policy; | |
8024 | u->dev_idx = vol; | |
8025 | u->new_policy = new_policy; | |
8026 | append_metadata_update(st, u, sizeof(*u)); | |
8027 | } else { | |
8028 | struct imsm_dev *dev; | |
8029 | ||
8030 | dev = get_imsm_dev(super, vol); | |
8031 | dev->rwh_policy = new_policy; | |
8032 | super->updates_pending++; | |
8033 | } | |
19ad203e JR |
8034 | if (new_policy == RWH_BITMAP) |
8035 | return write_init_bitmap_imsm_vol(st, vol); | |
aa534678 DW |
8036 | } else |
8037 | return 2; | |
8038 | ||
8039 | return 0; | |
8040 | } | |
bf5a934a | 8041 | |
195d1d76 | 8042 | static bool is_gen_migration(struct imsm_dev *dev) |
28bce06f | 8043 | { |
195d1d76 PP |
8044 | if (dev && dev->vol.migr_state && |
8045 | migr_type(dev) == MIGR_GEN_MIGR) | |
8046 | return true; | |
28bce06f | 8047 | |
195d1d76 | 8048 | return false; |
28bce06f AK |
8049 | } |
8050 | ||
1e5c6983 DW |
8051 | static int is_rebuilding(struct imsm_dev *dev) |
8052 | { | |
8053 | struct imsm_map *migr_map; | |
8054 | ||
8055 | if (!dev->vol.migr_state) | |
8056 | return 0; | |
8057 | ||
8058 | if (migr_type(dev) != MIGR_REBUILD) | |
8059 | return 0; | |
8060 | ||
238c0a71 | 8061 | migr_map = get_imsm_map(dev, MAP_1); |
1e5c6983 DW |
8062 | |
8063 | if (migr_map->map_state == IMSM_T_STATE_DEGRADED) | |
8064 | return 1; | |
8065 | else | |
8066 | return 0; | |
8067 | } | |
8068 | ||
6ce1fbf1 AK |
8069 | static int is_initializing(struct imsm_dev *dev) |
8070 | { | |
8071 | struct imsm_map *migr_map; | |
8072 | ||
8073 | if (!dev->vol.migr_state) | |
8074 | return 0; | |
8075 | ||
8076 | if (migr_type(dev) != MIGR_INIT) | |
8077 | return 0; | |
8078 | ||
238c0a71 | 8079 | migr_map = get_imsm_map(dev, MAP_1); |
6ce1fbf1 AK |
8080 | |
8081 | if (migr_map->map_state == IMSM_T_STATE_UNINITIALIZED) | |
8082 | return 1; | |
8083 | ||
8084 | return 0; | |
6ce1fbf1 AK |
8085 | } |
8086 | ||
c47b0ff6 AK |
8087 | static void update_recovery_start(struct intel_super *super, |
8088 | struct imsm_dev *dev, | |
8089 | struct mdinfo *array) | |
1e5c6983 DW |
8090 | { |
8091 | struct mdinfo *rebuild = NULL; | |
8092 | struct mdinfo *d; | |
8093 | __u32 units; | |
8094 | ||
8095 | if (!is_rebuilding(dev)) | |
8096 | return; | |
8097 | ||
8098 | /* Find the rebuild target, but punt on the dual rebuild case */ | |
8099 | for (d = array->devs; d; d = d->next) | |
8100 | if (d->recovery_start == 0) { | |
8101 | if (rebuild) | |
8102 | return; | |
8103 | rebuild = d; | |
8104 | } | |
8105 | ||
4363fd80 DW |
8106 | if (!rebuild) { |
8107 | /* (?) none of the disks are marked with | |
8108 | * IMSM_ORD_REBUILD, so assume they are missing and the | |
8109 | * disk_ord_tbl was not correctly updated | |
8110 | */ | |
1ade5cc1 | 8111 | dprintf("failed to locate out-of-sync disk\n"); |
4363fd80 DW |
8112 | return; |
8113 | } | |
8114 | ||
4036e7ee | 8115 | units = vol_curr_migr_unit(dev); |
c47b0ff6 | 8116 | rebuild->recovery_start = units * blocks_per_migr_unit(super, dev); |
1e5c6983 DW |
8117 | } |
8118 | ||
276d77db | 8119 | static int recover_backup_imsm(struct supertype *st, struct mdinfo *info); |
1e5c6983 | 8120 | |
00bbdbda | 8121 | static struct mdinfo *container_content_imsm(struct supertype *st, char *subarray) |
cdddbdbc | 8122 | { |
4f5bc454 DW |
8123 | /* Given a container loaded by load_super_imsm_all, |
8124 | * extract information about all the arrays into | |
8125 | * an mdinfo tree. | |
00bbdbda | 8126 | * If 'subarray' is given, just extract info about that array. |
4f5bc454 DW |
8127 | * |
8128 | * For each imsm_dev create an mdinfo, fill it in, | |
8129 | * then look for matching devices in super->disks | |
8130 | * and create appropriate device mdinfo. | |
8131 | */ | |
8132 | struct intel_super *super = st->sb; | |
949c47a0 | 8133 | struct imsm_super *mpb = super->anchor; |
4f5bc454 | 8134 | struct mdinfo *rest = NULL; |
00bbdbda | 8135 | unsigned int i; |
81219e70 | 8136 | int sb_errors = 0; |
abef11a3 AK |
8137 | struct dl *d; |
8138 | int spare_disks = 0; | |
b6180160 | 8139 | int current_vol = super->current_vol; |
cdddbdbc | 8140 | |
19482bcc AK |
8141 | /* do not assemble arrays when not all attributes are supported */ |
8142 | if (imsm_check_attributes(mpb->attributes) == 0) { | |
81219e70 | 8143 | sb_errors = 1; |
7a862a02 | 8144 | pr_err("Unsupported attributes in IMSM metadata.Arrays activation is blocked.\n"); |
19482bcc AK |
8145 | } |
8146 | ||
abef11a3 AK |
8147 | /* count spare devices, not used in maps |
8148 | */ | |
8149 | for (d = super->disks; d; d = d->next) | |
8150 | if (d->index == -1) | |
8151 | spare_disks++; | |
8152 | ||
4f5bc454 | 8153 | for (i = 0; i < mpb->num_raid_devs; i++) { |
00bbdbda N |
8154 | struct imsm_dev *dev; |
8155 | struct imsm_map *map; | |
86e3692b | 8156 | struct imsm_map *map2; |
4f5bc454 | 8157 | struct mdinfo *this; |
a6482415 | 8158 | int slot; |
a6482415 | 8159 | int chunk; |
00bbdbda | 8160 | char *ep; |
8b9cd157 | 8161 | int level; |
00bbdbda N |
8162 | |
8163 | if (subarray && | |
8164 | (i != strtoul(subarray, &ep, 10) || *ep != '\0')) | |
8165 | continue; | |
8166 | ||
8167 | dev = get_imsm_dev(super, i); | |
238c0a71 AK |
8168 | map = get_imsm_map(dev, MAP_0); |
8169 | map2 = get_imsm_map(dev, MAP_1); | |
8b9cd157 | 8170 | level = get_imsm_raid_level(map); |
4f5bc454 | 8171 | |
1ce0101c DW |
8172 | /* do not publish arrays that are in the middle of an |
8173 | * unsupported migration | |
8174 | */ | |
8175 | if (dev->vol.migr_state && | |
28bce06f | 8176 | (migr_type(dev) == MIGR_STATE_CHANGE)) { |
7a862a02 | 8177 | pr_err("cannot assemble volume '%.16s': unsupported migration in progress\n", |
1ce0101c DW |
8178 | dev->volume); |
8179 | continue; | |
8180 | } | |
2db86302 LM |
8181 | /* do not publish arrays that are not support by controller's |
8182 | * OROM/EFI | |
8183 | */ | |
1ce0101c | 8184 | |
503975b9 | 8185 | this = xmalloc(sizeof(*this)); |
4f5bc454 | 8186 | |
301406c9 | 8187 | super->current_vol = i; |
a5d85af7 | 8188 | getinfo_super_imsm_volume(st, this, NULL); |
9894ec0d | 8189 | this->next = rest; |
a6482415 | 8190 | chunk = __le16_to_cpu(map->blocks_per_strip) >> 1; |
81219e70 LM |
8191 | /* mdadm does not support all metadata features- set the bit in all arrays state */ |
8192 | if (!validate_geometry_imsm_orom(super, | |
8b9cd157 MK |
8193 | level, /* RAID level */ |
8194 | imsm_level_to_layout(level), | |
81219e70 | 8195 | map->num_members, /* raid disks */ |
fcc2c9da | 8196 | &chunk, imsm_dev_size(dev), |
81219e70 | 8197 | 1 /* verbose */)) { |
7a862a02 | 8198 | pr_err("IMSM RAID geometry validation failed. Array %s activation is blocked.\n", |
81219e70 LM |
8199 | dev->volume); |
8200 | this->array.state |= | |
8201 | (1<<MD_SB_BLOCK_CONTAINER_RESHAPE) | | |
8202 | (1<<MD_SB_BLOCK_VOLUME); | |
8203 | } | |
81219e70 LM |
8204 | |
8205 | /* if array has bad blocks, set suitable bit in all arrays state */ | |
8206 | if (sb_errors) | |
8207 | this->array.state |= | |
8208 | (1<<MD_SB_BLOCK_CONTAINER_RESHAPE) | | |
8209 | (1<<MD_SB_BLOCK_VOLUME); | |
8210 | ||
4f5bc454 | 8211 | for (slot = 0 ; slot < map->num_members; slot++) { |
1e5c6983 | 8212 | unsigned long long recovery_start; |
4f5bc454 DW |
8213 | struct mdinfo *info_d; |
8214 | struct dl *d; | |
8215 | int idx; | |
9a1608e5 | 8216 | int skip; |
7eef0453 | 8217 | __u32 ord; |
8b9cd157 | 8218 | int missing = 0; |
4f5bc454 | 8219 | |
9a1608e5 | 8220 | skip = 0; |
238c0a71 AK |
8221 | idx = get_imsm_disk_idx(dev, slot, MAP_0); |
8222 | ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X); | |
4f5bc454 DW |
8223 | for (d = super->disks; d ; d = d->next) |
8224 | if (d->index == idx) | |
0fbd635c | 8225 | break; |
4f5bc454 | 8226 | |
1e5c6983 | 8227 | recovery_start = MaxSector; |
4f5bc454 | 8228 | if (d == NULL) |
9a1608e5 | 8229 | skip = 1; |
25ed7e59 | 8230 | if (d && is_failed(&d->disk)) |
9a1608e5 | 8231 | skip = 1; |
8b9cd157 | 8232 | if (!skip && (ord & IMSM_ORD_REBUILD)) |
1e5c6983 | 8233 | recovery_start = 0; |
1e93d0d1 BK |
8234 | if (!(ord & IMSM_ORD_REBUILD)) |
8235 | this->array.working_disks++; | |
1011e834 | 8236 | /* |
9a1608e5 | 8237 | * if we skip some disks the array will be assmebled degraded; |
1e5c6983 DW |
8238 | * reset resync start to avoid a dirty-degraded |
8239 | * situation when performing the intial sync | |
9a1608e5 | 8240 | */ |
8b9cd157 MK |
8241 | if (skip) |
8242 | missing++; | |
8243 | ||
8244 | if (!(dev->vol.dirty & RAIDVOL_DIRTY)) { | |
8245 | if ((!able_to_resync(level, missing) || | |
8246 | recovery_start == 0)) | |
8247 | this->resync_start = MaxSector; | |
8b9cd157 MK |
8248 | } |
8249 | ||
9a1608e5 DW |
8250 | if (skip) |
8251 | continue; | |
4f5bc454 | 8252 | |
503975b9 | 8253 | info_d = xcalloc(1, sizeof(*info_d)); |
4f5bc454 DW |
8254 | info_d->next = this->devs; |
8255 | this->devs = info_d; | |
8256 | ||
4f5bc454 DW |
8257 | info_d->disk.number = d->index; |
8258 | info_d->disk.major = d->major; | |
8259 | info_d->disk.minor = d->minor; | |
8260 | info_d->disk.raid_disk = slot; | |
1e5c6983 | 8261 | info_d->recovery_start = recovery_start; |
86e3692b AK |
8262 | if (map2) { |
8263 | if (slot < map2->num_members) | |
8264 | info_d->disk.state = (1 << MD_DISK_ACTIVE); | |
04c3c514 AK |
8265 | else |
8266 | this->array.spare_disks++; | |
86e3692b AK |
8267 | } else { |
8268 | if (slot < map->num_members) | |
8269 | info_d->disk.state = (1 << MD_DISK_ACTIVE); | |
04c3c514 AK |
8270 | else |
8271 | this->array.spare_disks++; | |
86e3692b | 8272 | } |
4f5bc454 DW |
8273 | |
8274 | info_d->events = __le32_to_cpu(mpb->generation_num); | |
5551b113 | 8275 | info_d->data_offset = pba_of_lba0(map); |
44490938 | 8276 | info_d->component_size = calc_component_size(map, dev); |
06fb291a PB |
8277 | |
8278 | if (map->raid_level == 5) { | |
2432ce9b AP |
8279 | info_d->ppl_sector = this->ppl_sector; |
8280 | info_d->ppl_size = this->ppl_size; | |
98e96bdb AP |
8281 | if (this->consistency_policy == CONSISTENCY_POLICY_PPL && |
8282 | recovery_start == 0) | |
8283 | this->resync_start = 0; | |
06fb291a | 8284 | } |
b12796be | 8285 | |
5e46202e | 8286 | info_d->bb.supported = 1; |
b12796be TM |
8287 | get_volume_badblocks(super->bbm_log, ord_to_idx(ord), |
8288 | info_d->data_offset, | |
8289 | info_d->component_size, | |
8290 | &info_d->bb); | |
4f5bc454 | 8291 | } |
1e5c6983 | 8292 | /* now that the disk list is up-to-date fixup recovery_start */ |
c47b0ff6 | 8293 | update_recovery_start(super, dev, this); |
abef11a3 | 8294 | this->array.spare_disks += spare_disks; |
276d77db AK |
8295 | |
8296 | /* check for reshape */ | |
8297 | if (this->reshape_active == 1) | |
8298 | recover_backup_imsm(st, this); | |
9a1608e5 | 8299 | rest = this; |
4f5bc454 DW |
8300 | } |
8301 | ||
b6180160 | 8302 | super->current_vol = current_vol; |
4f5bc454 | 8303 | return rest; |
cdddbdbc DW |
8304 | } |
8305 | ||
3b451610 AK |
8306 | static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev, |
8307 | int failed, int look_in_map) | |
c2a1e7da | 8308 | { |
3b451610 AK |
8309 | struct imsm_map *map; |
8310 | ||
8311 | map = get_imsm_map(dev, look_in_map); | |
c2a1e7da DW |
8312 | |
8313 | if (!failed) | |
1011e834 | 8314 | return map->map_state == IMSM_T_STATE_UNINITIALIZED ? |
3393c6af | 8315 | IMSM_T_STATE_UNINITIALIZED : IMSM_T_STATE_NORMAL; |
c2a1e7da DW |
8316 | |
8317 | switch (get_imsm_raid_level(map)) { | |
8318 | case 0: | |
8319 | return IMSM_T_STATE_FAILED; | |
8320 | break; | |
8321 | case 1: | |
8322 | if (failed < map->num_members) | |
8323 | return IMSM_T_STATE_DEGRADED; | |
8324 | else | |
8325 | return IMSM_T_STATE_FAILED; | |
8326 | break; | |
8327 | case 10: | |
8328 | { | |
8329 | /** | |
c92a2527 DW |
8330 | * check to see if any mirrors have failed, otherwise we |
8331 | * are degraded. Even numbered slots are mirrored on | |
8332 | * slot+1 | |
c2a1e7da | 8333 | */ |
c2a1e7da | 8334 | int i; |
d9b420a5 N |
8335 | /* gcc -Os complains that this is unused */ |
8336 | int insync = insync; | |
c2a1e7da DW |
8337 | |
8338 | for (i = 0; i < map->num_members; i++) { | |
238c0a71 | 8339 | __u32 ord = get_imsm_ord_tbl_ent(dev, i, MAP_X); |
c92a2527 DW |
8340 | int idx = ord_to_idx(ord); |
8341 | struct imsm_disk *disk; | |
c2a1e7da | 8342 | |
c92a2527 | 8343 | /* reset the potential in-sync count on even-numbered |
1011e834 | 8344 | * slots. num_copies is always 2 for imsm raid10 |
c92a2527 DW |
8345 | */ |
8346 | if ((i & 1) == 0) | |
8347 | insync = 2; | |
c2a1e7da | 8348 | |
c92a2527 | 8349 | disk = get_imsm_disk(super, idx); |
25ed7e59 | 8350 | if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD) |
c92a2527 | 8351 | insync--; |
c2a1e7da | 8352 | |
c92a2527 DW |
8353 | /* no in-sync disks left in this mirror the |
8354 | * array has failed | |
8355 | */ | |
8356 | if (insync == 0) | |
8357 | return IMSM_T_STATE_FAILED; | |
c2a1e7da DW |
8358 | } |
8359 | ||
8360 | return IMSM_T_STATE_DEGRADED; | |
8361 | } | |
8362 | case 5: | |
8363 | if (failed < 2) | |
8364 | return IMSM_T_STATE_DEGRADED; | |
8365 | else | |
8366 | return IMSM_T_STATE_FAILED; | |
8367 | break; | |
8368 | default: | |
8369 | break; | |
8370 | } | |
8371 | ||
8372 | return map->map_state; | |
8373 | } | |
8374 | ||
3b451610 AK |
8375 | static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev, |
8376 | int look_in_map) | |
c2a1e7da DW |
8377 | { |
8378 | int i; | |
8379 | int failed = 0; | |
8380 | struct imsm_disk *disk; | |
d5985138 AK |
8381 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
8382 | struct imsm_map *prev = get_imsm_map(dev, MAP_1); | |
68fe4598 | 8383 | struct imsm_map *map_for_loop; |
0556e1a2 DW |
8384 | __u32 ord; |
8385 | int idx; | |
d5985138 | 8386 | int idx_1; |
c2a1e7da | 8387 | |
0556e1a2 DW |
8388 | /* at the beginning of migration we set IMSM_ORD_REBUILD on |
8389 | * disks that are being rebuilt. New failures are recorded to | |
8390 | * map[0]. So we look through all the disks we started with and | |
8391 | * see if any failures are still present, or if any new ones | |
8392 | * have arrived | |
0556e1a2 | 8393 | */ |
d5985138 AK |
8394 | map_for_loop = map; |
8395 | if (prev && (map->num_members < prev->num_members)) | |
8396 | map_for_loop = prev; | |
68fe4598 LD |
8397 | |
8398 | for (i = 0; i < map_for_loop->num_members; i++) { | |
d5985138 | 8399 | idx_1 = -255; |
238c0a71 AK |
8400 | /* when MAP_X is passed both maps failures are counted |
8401 | */ | |
d5985138 | 8402 | if (prev && |
089f9d79 JS |
8403 | (look_in_map == MAP_1 || look_in_map == MAP_X) && |
8404 | i < prev->num_members) { | |
d5985138 AK |
8405 | ord = __le32_to_cpu(prev->disk_ord_tbl[i]); |
8406 | idx_1 = ord_to_idx(ord); | |
c2a1e7da | 8407 | |
d5985138 AK |
8408 | disk = get_imsm_disk(super, idx_1); |
8409 | if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD) | |
8410 | failed++; | |
8411 | } | |
089f9d79 JS |
8412 | if ((look_in_map == MAP_0 || look_in_map == MAP_X) && |
8413 | i < map->num_members) { | |
d5985138 AK |
8414 | ord = __le32_to_cpu(map->disk_ord_tbl[i]); |
8415 | idx = ord_to_idx(ord); | |
8416 | ||
8417 | if (idx != idx_1) { | |
8418 | disk = get_imsm_disk(super, idx); | |
8419 | if (!disk || is_failed(disk) || | |
8420 | ord & IMSM_ORD_REBUILD) | |
8421 | failed++; | |
8422 | } | |
8423 | } | |
c2a1e7da DW |
8424 | } |
8425 | ||
8426 | return failed; | |
845dea95 NB |
8427 | } |
8428 | ||
97b4d0e9 | 8429 | static int imsm_open_new(struct supertype *c, struct active_array *a, |
60815698 | 8430 | int inst) |
97b4d0e9 DW |
8431 | { |
8432 | struct intel_super *super = c->sb; | |
8433 | struct imsm_super *mpb = super->anchor; | |
bbab0940 | 8434 | struct imsm_update_prealloc_bb_mem u; |
9587c373 | 8435 | |
60815698 MG |
8436 | if (inst >= mpb->num_raid_devs) { |
8437 | pr_err("subarry index %d, out of range\n", inst); | |
97b4d0e9 DW |
8438 | return -ENODEV; |
8439 | } | |
8440 | ||
60815698 MG |
8441 | dprintf("imsm: open_new %d\n", inst); |
8442 | a->info.container_member = inst; | |
bbab0940 TM |
8443 | |
8444 | u.type = update_prealloc_badblocks_mem; | |
8445 | imsm_update_metadata_locally(c, &u, sizeof(u)); | |
8446 | ||
97b4d0e9 DW |
8447 | return 0; |
8448 | } | |
8449 | ||
0c046afd DW |
8450 | static int is_resyncing(struct imsm_dev *dev) |
8451 | { | |
8452 | struct imsm_map *migr_map; | |
8453 | ||
8454 | if (!dev->vol.migr_state) | |
8455 | return 0; | |
8456 | ||
1484e727 DW |
8457 | if (migr_type(dev) == MIGR_INIT || |
8458 | migr_type(dev) == MIGR_REPAIR) | |
0c046afd DW |
8459 | return 1; |
8460 | ||
4c9bc37b AK |
8461 | if (migr_type(dev) == MIGR_GEN_MIGR) |
8462 | return 0; | |
8463 | ||
238c0a71 | 8464 | migr_map = get_imsm_map(dev, MAP_1); |
0c046afd | 8465 | |
089f9d79 JS |
8466 | if (migr_map->map_state == IMSM_T_STATE_NORMAL && |
8467 | dev->vol.migr_type != MIGR_GEN_MIGR) | |
0c046afd DW |
8468 | return 1; |
8469 | else | |
8470 | return 0; | |
8471 | } | |
8472 | ||
0556e1a2 | 8473 | /* return true if we recorded new information */ |
4c9e8c1e TM |
8474 | static int mark_failure(struct intel_super *super, |
8475 | struct imsm_dev *dev, struct imsm_disk *disk, int idx) | |
47ee5a45 | 8476 | { |
0556e1a2 DW |
8477 | __u32 ord; |
8478 | int slot; | |
8479 | struct imsm_map *map; | |
86c54047 DW |
8480 | char buf[MAX_RAID_SERIAL_LEN+3]; |
8481 | unsigned int len, shift = 0; | |
0556e1a2 DW |
8482 | |
8483 | /* new failures are always set in map[0] */ | |
238c0a71 | 8484 | map = get_imsm_map(dev, MAP_0); |
0556e1a2 DW |
8485 | |
8486 | slot = get_imsm_disk_slot(map, idx); | |
8487 | if (slot < 0) | |
8488 | return 0; | |
8489 | ||
8490 | ord = __le32_to_cpu(map->disk_ord_tbl[slot]); | |
25ed7e59 | 8491 | if (is_failed(disk) && (ord & IMSM_ORD_REBUILD)) |
0556e1a2 DW |
8492 | return 0; |
8493 | ||
7d0c5e24 LD |
8494 | memcpy(buf, disk->serial, MAX_RAID_SERIAL_LEN); |
8495 | buf[MAX_RAID_SERIAL_LEN] = '\000'; | |
8496 | strcat(buf, ":0"); | |
86c54047 DW |
8497 | if ((len = strlen(buf)) >= MAX_RAID_SERIAL_LEN) |
8498 | shift = len - MAX_RAID_SERIAL_LEN + 1; | |
167d8bb8 | 8499 | memcpy(disk->serial, &buf[shift], len + 1 - shift); |
86c54047 | 8500 | |
f2f27e63 | 8501 | disk->status |= FAILED_DISK; |
0556e1a2 | 8502 | set_imsm_ord_tbl_ent(map, slot, idx | IMSM_ORD_REBUILD); |
17788994 AK |
8503 | /* mark failures in second map if second map exists and this disk |
8504 | * in this slot. | |
8505 | * This is valid for migration, initialization and rebuild | |
8506 | */ | |
8507 | if (dev->vol.migr_state) { | |
238c0a71 | 8508 | struct imsm_map *map2 = get_imsm_map(dev, MAP_1); |
0a108d63 AK |
8509 | int slot2 = get_imsm_disk_slot(map2, idx); |
8510 | ||
089f9d79 | 8511 | if (slot2 < map2->num_members && slot2 >= 0) |
0a108d63 | 8512 | set_imsm_ord_tbl_ent(map2, slot2, |
1ace8403 AK |
8513 | idx | IMSM_ORD_REBUILD); |
8514 | } | |
d7a1fda2 MT |
8515 | if (map->failed_disk_num == 0xff || |
8516 | (!is_rebuilding(dev) && map->failed_disk_num > slot)) | |
0556e1a2 | 8517 | map->failed_disk_num = slot; |
4c9e8c1e TM |
8518 | |
8519 | clear_disk_badblocks(super->bbm_log, ord_to_idx(ord)); | |
8520 | ||
0556e1a2 DW |
8521 | return 1; |
8522 | } | |
8523 | ||
4c9e8c1e TM |
8524 | static void mark_missing(struct intel_super *super, |
8525 | struct imsm_dev *dev, struct imsm_disk *disk, int idx) | |
0556e1a2 | 8526 | { |
4c9e8c1e | 8527 | mark_failure(super, dev, disk, idx); |
0556e1a2 DW |
8528 | |
8529 | if (disk->scsi_id == __cpu_to_le32(~(__u32)0)) | |
8530 | return; | |
8531 | ||
47ee5a45 DW |
8532 | disk->scsi_id = __cpu_to_le32(~(__u32)0); |
8533 | memmove(&disk->serial[0], &disk->serial[1], MAX_RAID_SERIAL_LEN - 1); | |
8534 | } | |
8535 | ||
33414a01 DW |
8536 | static void handle_missing(struct intel_super *super, struct imsm_dev *dev) |
8537 | { | |
33414a01 | 8538 | struct dl *dl; |
33414a01 DW |
8539 | |
8540 | if (!super->missing) | |
8541 | return; | |
33414a01 | 8542 | |
79b68f1b PC |
8543 | /* When orom adds replacement for missing disk it does |
8544 | * not remove entry of missing disk, but just updates map with | |
8545 | * new added disk. So it is not enough just to test if there is | |
8546 | * any missing disk, we have to look if there are any failed disks | |
8547 | * in map to stop migration */ | |
8548 | ||
33414a01 | 8549 | dprintf("imsm: mark missing\n"); |
3d59f0c0 AK |
8550 | /* end process for initialization and rebuild only |
8551 | */ | |
195d1d76 | 8552 | if (is_gen_migration(dev) == false) { |
fb12a745 | 8553 | int failed = imsm_count_failed(super, dev, MAP_0); |
3d59f0c0 | 8554 | |
fb12a745 TM |
8555 | if (failed) { |
8556 | __u8 map_state; | |
8557 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
8558 | struct imsm_map *map1; | |
8559 | int i, ord, ord_map1; | |
8560 | int rebuilt = 1; | |
3d59f0c0 | 8561 | |
fb12a745 TM |
8562 | for (i = 0; i < map->num_members; i++) { |
8563 | ord = get_imsm_ord_tbl_ent(dev, i, MAP_0); | |
8564 | if (!(ord & IMSM_ORD_REBUILD)) | |
8565 | continue; | |
8566 | ||
8567 | map1 = get_imsm_map(dev, MAP_1); | |
8568 | if (!map1) | |
8569 | continue; | |
8570 | ||
8571 | ord_map1 = __le32_to_cpu(map1->disk_ord_tbl[i]); | |
8572 | if (ord_map1 & IMSM_ORD_REBUILD) | |
8573 | rebuilt = 0; | |
8574 | } | |
8575 | ||
8576 | if (rebuilt) { | |
8577 | map_state = imsm_check_degraded(super, dev, | |
8578 | failed, MAP_0); | |
8579 | end_migration(dev, super, map_state); | |
8580 | } | |
8581 | } | |
3d59f0c0 | 8582 | } |
33414a01 | 8583 | for (dl = super->missing; dl; dl = dl->next) |
4c9e8c1e | 8584 | mark_missing(super, dev, &dl->disk, dl->index); |
33414a01 DW |
8585 | super->updates_pending++; |
8586 | } | |
8587 | ||
f3871fdc AK |
8588 | static unsigned long long imsm_set_array_size(struct imsm_dev *dev, |
8589 | long long new_size) | |
70bdf0dc | 8590 | { |
70bdf0dc | 8591 | unsigned long long array_blocks; |
9529d343 MD |
8592 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
8593 | int used_disks = imsm_num_data_members(map); | |
70bdf0dc AK |
8594 | |
8595 | if (used_disks == 0) { | |
8596 | /* when problems occures | |
8597 | * return current array_blocks value | |
8598 | */ | |
fcc2c9da | 8599 | array_blocks = imsm_dev_size(dev); |
70bdf0dc AK |
8600 | |
8601 | return array_blocks; | |
8602 | } | |
8603 | ||
8604 | /* set array size in metadata | |
8605 | */ | |
9529d343 | 8606 | if (new_size <= 0) |
f3871fdc AK |
8607 | /* OLCE size change is caused by added disks |
8608 | */ | |
44490938 | 8609 | array_blocks = per_dev_array_size(map) * used_disks; |
9529d343 | 8610 | else |
f3871fdc AK |
8611 | /* Online Volume Size Change |
8612 | * Using available free space | |
8613 | */ | |
8614 | array_blocks = new_size; | |
70bdf0dc | 8615 | |
b53bfba6 | 8616 | array_blocks = round_size_to_mb(array_blocks, used_disks); |
fcc2c9da | 8617 | set_imsm_dev_size(dev, array_blocks); |
70bdf0dc AK |
8618 | |
8619 | return array_blocks; | |
8620 | } | |
8621 | ||
28bce06f AK |
8622 | static void imsm_set_disk(struct active_array *a, int n, int state); |
8623 | ||
0e2d1a4e AK |
8624 | static void imsm_progress_container_reshape(struct intel_super *super) |
8625 | { | |
8626 | /* if no device has a migr_state, but some device has a | |
8627 | * different number of members than the previous device, start | |
8628 | * changing the number of devices in this device to match | |
8629 | * previous. | |
8630 | */ | |
8631 | struct imsm_super *mpb = super->anchor; | |
8632 | int prev_disks = -1; | |
8633 | int i; | |
1dfaa380 | 8634 | int copy_map_size; |
0e2d1a4e AK |
8635 | |
8636 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
8637 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
238c0a71 | 8638 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
0e2d1a4e AK |
8639 | struct imsm_map *map2; |
8640 | int prev_num_members; | |
0e2d1a4e AK |
8641 | |
8642 | if (dev->vol.migr_state) | |
8643 | return; | |
8644 | ||
8645 | if (prev_disks == -1) | |
8646 | prev_disks = map->num_members; | |
8647 | if (prev_disks == map->num_members) | |
8648 | continue; | |
8649 | ||
8650 | /* OK, this array needs to enter reshape mode. | |
8651 | * i.e it needs a migr_state | |
8652 | */ | |
8653 | ||
1dfaa380 | 8654 | copy_map_size = sizeof_imsm_map(map); |
0e2d1a4e AK |
8655 | prev_num_members = map->num_members; |
8656 | map->num_members = prev_disks; | |
8657 | dev->vol.migr_state = 1; | |
4036e7ee | 8658 | set_vol_curr_migr_unit(dev, 0); |
ea672ee1 | 8659 | set_migr_type(dev, MIGR_GEN_MIGR); |
0e2d1a4e AK |
8660 | for (i = prev_num_members; |
8661 | i < map->num_members; i++) | |
8662 | set_imsm_ord_tbl_ent(map, i, i); | |
238c0a71 | 8663 | map2 = get_imsm_map(dev, MAP_1); |
0e2d1a4e | 8664 | /* Copy the current map */ |
1dfaa380 | 8665 | memcpy(map2, map, copy_map_size); |
0e2d1a4e AK |
8666 | map2->num_members = prev_num_members; |
8667 | ||
f3871fdc | 8668 | imsm_set_array_size(dev, -1); |
51d83f5d | 8669 | super->clean_migration_record_by_mdmon = 1; |
0e2d1a4e AK |
8670 | super->updates_pending++; |
8671 | } | |
8672 | } | |
8673 | ||
aad6f216 | 8674 | /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild |
0c046afd DW |
8675 | * states are handled in imsm_set_disk() with one exception, when a |
8676 | * resync is stopped due to a new failure this routine will set the | |
8677 | * 'degraded' state for the array. | |
8678 | */ | |
01f157d7 | 8679 | static int imsm_set_array_state(struct active_array *a, int consistent) |
a862209d DW |
8680 | { |
8681 | int inst = a->info.container_member; | |
8682 | struct intel_super *super = a->container->sb; | |
949c47a0 | 8683 | struct imsm_dev *dev = get_imsm_dev(super, inst); |
238c0a71 | 8684 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
3b451610 AK |
8685 | int failed = imsm_count_failed(super, dev, MAP_0); |
8686 | __u8 map_state = imsm_check_degraded(super, dev, failed, MAP_0); | |
1e5c6983 | 8687 | __u32 blocks_per_unit; |
a862209d | 8688 | |
1af97990 AK |
8689 | if (dev->vol.migr_state && |
8690 | dev->vol.migr_type == MIGR_GEN_MIGR) { | |
8691 | /* array state change is blocked due to reshape action | |
aad6f216 N |
8692 | * We might need to |
8693 | * - abort the reshape (if last_checkpoint is 0 and action!= reshape) | |
8694 | * - finish the reshape (if last_checkpoint is big and action != reshape) | |
4036e7ee | 8695 | * - update vol_curr_migr_unit |
1af97990 | 8696 | */ |
aad6f216 | 8697 | if (a->curr_action == reshape) { |
4036e7ee | 8698 | /* still reshaping, maybe update vol_curr_migr_unit */ |
633b5610 | 8699 | goto mark_checkpoint; |
aad6f216 | 8700 | } else { |
aad6f216 N |
8701 | if (a->last_checkpoint >= a->info.component_size) { |
8702 | unsigned long long array_blocks; | |
8703 | int used_disks; | |
e154ced3 | 8704 | struct mdinfo *mdi; |
aad6f216 | 8705 | |
9529d343 | 8706 | used_disks = imsm_num_data_members(map); |
d55adef9 AK |
8707 | if (used_disks > 0) { |
8708 | array_blocks = | |
44490938 | 8709 | per_dev_array_size(map) * |
d55adef9 | 8710 | used_disks; |
b53bfba6 TM |
8711 | array_blocks = |
8712 | round_size_to_mb(array_blocks, | |
8713 | used_disks); | |
d55adef9 AK |
8714 | a->info.custom_array_size = array_blocks; |
8715 | /* encourage manager to update array | |
8716 | * size | |
8717 | */ | |
e154ced3 | 8718 | |
d55adef9 | 8719 | a->check_reshape = 1; |
633b5610 | 8720 | } |
e154ced3 AK |
8721 | /* finalize online capacity expansion/reshape */ |
8722 | for (mdi = a->info.devs; mdi; mdi = mdi->next) | |
8723 | imsm_set_disk(a, | |
8724 | mdi->disk.raid_disk, | |
8725 | mdi->curr_state); | |
8726 | ||
0e2d1a4e | 8727 | imsm_progress_container_reshape(super); |
e154ced3 | 8728 | } |
aad6f216 | 8729 | } |
1af97990 AK |
8730 | } |
8731 | ||
47ee5a45 | 8732 | /* before we activate this array handle any missing disks */ |
33414a01 DW |
8733 | if (consistent == 2) |
8734 | handle_missing(super, dev); | |
1e5c6983 | 8735 | |
0c046afd | 8736 | if (consistent == 2 && |
b7941fd6 | 8737 | (!is_resync_complete(&a->info) || |
0c046afd DW |
8738 | map_state != IMSM_T_STATE_NORMAL || |
8739 | dev->vol.migr_state)) | |
01f157d7 | 8740 | consistent = 0; |
272906ef | 8741 | |
b7941fd6 | 8742 | if (is_resync_complete(&a->info)) { |
0c046afd | 8743 | /* complete intialization / resync, |
0556e1a2 DW |
8744 | * recovery and interrupted recovery is completed in |
8745 | * ->set_disk | |
0c046afd DW |
8746 | */ |
8747 | if (is_resyncing(dev)) { | |
8748 | dprintf("imsm: mark resync done\n"); | |
809da78e | 8749 | end_migration(dev, super, map_state); |
115c3803 | 8750 | super->updates_pending++; |
484240d8 | 8751 | a->last_checkpoint = 0; |
115c3803 | 8752 | } |
b9172665 AK |
8753 | } else if ((!is_resyncing(dev) && !failed) && |
8754 | (imsm_reshape_blocks_arrays_changes(super) == 0)) { | |
0c046afd | 8755 | /* mark the start of the init process if nothing is failed */ |
b7941fd6 | 8756 | dprintf("imsm: mark resync start\n"); |
1484e727 | 8757 | if (map->map_state == IMSM_T_STATE_UNINITIALIZED) |
8e59f3d8 | 8758 | migrate(dev, super, IMSM_T_STATE_NORMAL, MIGR_INIT); |
1484e727 | 8759 | else |
8e59f3d8 | 8760 | migrate(dev, super, IMSM_T_STATE_NORMAL, MIGR_REPAIR); |
3393c6af | 8761 | super->updates_pending++; |
115c3803 | 8762 | } |
a862209d | 8763 | |
fdb7e802 MK |
8764 | if (a->prev_action == idle) |
8765 | goto skip_mark_checkpoint; | |
8766 | ||
633b5610 | 8767 | mark_checkpoint: |
5b83bacf AK |
8768 | /* skip checkpointing for general migration, |
8769 | * it is controlled in mdadm | |
8770 | */ | |
8771 | if (is_gen_migration(dev)) | |
8772 | goto skip_mark_checkpoint; | |
8773 | ||
4036e7ee MT |
8774 | /* check if we can update vol_curr_migr_unit from resync_start, |
8775 | * recovery_start | |
8776 | */ | |
c47b0ff6 | 8777 | blocks_per_unit = blocks_per_migr_unit(super, dev); |
4f0a7acc | 8778 | if (blocks_per_unit) { |
4036e7ee MT |
8779 | set_vol_curr_migr_unit(dev, |
8780 | a->last_checkpoint / blocks_per_unit); | |
8781 | dprintf("imsm: mark checkpoint (%llu)\n", | |
8782 | vol_curr_migr_unit(dev)); | |
8783 | super->updates_pending++; | |
1e5c6983 | 8784 | } |
f8f603f1 | 8785 | |
5b83bacf | 8786 | skip_mark_checkpoint: |
3393c6af | 8787 | /* mark dirty / clean */ |
2432ce9b AP |
8788 | if (((dev->vol.dirty & RAIDVOL_DIRTY) && consistent) || |
8789 | (!(dev->vol.dirty & RAIDVOL_DIRTY) && !consistent)) { | |
b7941fd6 | 8790 | dprintf("imsm: mark '%s'\n", consistent ? "clean" : "dirty"); |
2432ce9b AP |
8791 | if (consistent) { |
8792 | dev->vol.dirty = RAIDVOL_CLEAN; | |
8793 | } else { | |
8794 | dev->vol.dirty = RAIDVOL_DIRTY; | |
c2462068 PB |
8795 | if (dev->rwh_policy == RWH_DISTRIBUTED || |
8796 | dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED) | |
2432ce9b AP |
8797 | dev->vol.dirty |= RAIDVOL_DSRECORD_VALID; |
8798 | } | |
a862209d DW |
8799 | super->updates_pending++; |
8800 | } | |
28bce06f | 8801 | |
01f157d7 | 8802 | return consistent; |
a862209d DW |
8803 | } |
8804 | ||
6f50473f TM |
8805 | static int imsm_disk_slot_to_ord(struct active_array *a, int slot) |
8806 | { | |
8807 | int inst = a->info.container_member; | |
8808 | struct intel_super *super = a->container->sb; | |
8809 | struct imsm_dev *dev = get_imsm_dev(super, inst); | |
8810 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
8811 | ||
8812 | if (slot > map->num_members) { | |
8813 | pr_err("imsm: imsm_disk_slot_to_ord %d out of range 0..%d\n", | |
8814 | slot, map->num_members - 1); | |
8815 | return -1; | |
8816 | } | |
8817 | ||
8818 | if (slot < 0) | |
8819 | return -1; | |
8820 | ||
8821 | return get_imsm_ord_tbl_ent(dev, slot, MAP_0); | |
8822 | } | |
8823 | ||
8d45d196 | 8824 | static void imsm_set_disk(struct active_array *a, int n, int state) |
845dea95 | 8825 | { |
8d45d196 DW |
8826 | int inst = a->info.container_member; |
8827 | struct intel_super *super = a->container->sb; | |
949c47a0 | 8828 | struct imsm_dev *dev = get_imsm_dev(super, inst); |
238c0a71 | 8829 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
8d45d196 | 8830 | struct imsm_disk *disk; |
7ce05701 LD |
8831 | struct mdinfo *mdi; |
8832 | int recovery_not_finished = 0; | |
0c046afd | 8833 | int failed; |
6f50473f | 8834 | int ord; |
0c046afd | 8835 | __u8 map_state; |
fb12a745 TM |
8836 | int rebuild_done = 0; |
8837 | int i; | |
8d45d196 | 8838 | |
fb12a745 | 8839 | ord = get_imsm_ord_tbl_ent(dev, n, MAP_X); |
6f50473f | 8840 | if (ord < 0) |
8d45d196 DW |
8841 | return; |
8842 | ||
4e6e574a | 8843 | dprintf("imsm: set_disk %d:%x\n", n, state); |
b10b37b8 | 8844 | disk = get_imsm_disk(super, ord_to_idx(ord)); |
8d45d196 | 8845 | |
5802a811 | 8846 | /* check for new failures */ |
ae7d61e3 | 8847 | if (disk && (state & DS_FAULTY)) { |
4c9e8c1e | 8848 | if (mark_failure(super, dev, disk, ord_to_idx(ord))) |
0556e1a2 | 8849 | super->updates_pending++; |
8d45d196 | 8850 | } |
47ee5a45 | 8851 | |
19859edc | 8852 | /* check if in_sync */ |
0556e1a2 | 8853 | if (state & DS_INSYNC && ord & IMSM_ORD_REBUILD && is_rebuilding(dev)) { |
238c0a71 | 8854 | struct imsm_map *migr_map = get_imsm_map(dev, MAP_1); |
b10b37b8 DW |
8855 | |
8856 | set_imsm_ord_tbl_ent(migr_map, n, ord_to_idx(ord)); | |
fb12a745 | 8857 | rebuild_done = 1; |
19859edc DW |
8858 | super->updates_pending++; |
8859 | } | |
8d45d196 | 8860 | |
3b451610 AK |
8861 | failed = imsm_count_failed(super, dev, MAP_0); |
8862 | map_state = imsm_check_degraded(super, dev, failed, MAP_0); | |
5802a811 | 8863 | |
0c046afd | 8864 | /* check if recovery complete, newly degraded, or failed */ |
94002678 AK |
8865 | dprintf("imsm: Detected transition to state "); |
8866 | switch (map_state) { | |
8867 | case IMSM_T_STATE_NORMAL: /* transition to normal state */ | |
8868 | dprintf("normal: "); | |
8869 | if (is_rebuilding(dev)) { | |
1ade5cc1 | 8870 | dprintf_cont("while rebuilding"); |
7ce05701 LD |
8871 | /* check if recovery is really finished */ |
8872 | for (mdi = a->info.devs; mdi ; mdi = mdi->next) | |
8873 | if (mdi->recovery_start != MaxSector) { | |
8874 | recovery_not_finished = 1; | |
8875 | break; | |
8876 | } | |
8877 | if (recovery_not_finished) { | |
1ade5cc1 N |
8878 | dprintf_cont("\n"); |
8879 | dprintf("Rebuild has not finished yet, state not changed"); | |
7ce05701 LD |
8880 | if (a->last_checkpoint < mdi->recovery_start) { |
8881 | a->last_checkpoint = mdi->recovery_start; | |
8882 | super->updates_pending++; | |
8883 | } | |
8884 | break; | |
8885 | } | |
94002678 | 8886 | end_migration(dev, super, map_state); |
94002678 AK |
8887 | map->failed_disk_num = ~0; |
8888 | super->updates_pending++; | |
8889 | a->last_checkpoint = 0; | |
8890 | break; | |
8891 | } | |
8892 | if (is_gen_migration(dev)) { | |
1ade5cc1 | 8893 | dprintf_cont("while general migration"); |
bf2f0071 | 8894 | if (a->last_checkpoint >= a->info.component_size) |
809da78e | 8895 | end_migration(dev, super, map_state); |
94002678 AK |
8896 | else |
8897 | map->map_state = map_state; | |
28bce06f | 8898 | map->failed_disk_num = ~0; |
94002678 | 8899 | super->updates_pending++; |
bf2f0071 | 8900 | break; |
94002678 AK |
8901 | } |
8902 | break; | |
8903 | case IMSM_T_STATE_DEGRADED: /* transition to degraded state */ | |
1ade5cc1 | 8904 | dprintf_cont("degraded: "); |
089f9d79 | 8905 | if (map->map_state != map_state && !dev->vol.migr_state) { |
1ade5cc1 | 8906 | dprintf_cont("mark degraded"); |
94002678 AK |
8907 | map->map_state = map_state; |
8908 | super->updates_pending++; | |
8909 | a->last_checkpoint = 0; | |
8910 | break; | |
8911 | } | |
8912 | if (is_rebuilding(dev)) { | |
d7a1fda2 | 8913 | dprintf_cont("while rebuilding "); |
a4e96fd8 MT |
8914 | if (state & DS_FAULTY) { |
8915 | dprintf_cont("removing failed drive "); | |
d7a1fda2 MT |
8916 | if (n == map->failed_disk_num) { |
8917 | dprintf_cont("end migration"); | |
8918 | end_migration(dev, super, map_state); | |
a4e96fd8 | 8919 | a->last_checkpoint = 0; |
d7a1fda2 | 8920 | } else { |
a4e96fd8 | 8921 | dprintf_cont("fail detected during rebuild, changing map state"); |
d7a1fda2 MT |
8922 | map->map_state = map_state; |
8923 | } | |
94002678 | 8924 | super->updates_pending++; |
fb12a745 TM |
8925 | } |
8926 | ||
a4e96fd8 MT |
8927 | if (!rebuild_done) |
8928 | break; | |
8929 | ||
fb12a745 TM |
8930 | /* check if recovery is really finished */ |
8931 | for (mdi = a->info.devs; mdi ; mdi = mdi->next) | |
8932 | if (mdi->recovery_start != MaxSector) { | |
8933 | recovery_not_finished = 1; | |
8934 | break; | |
8935 | } | |
8936 | if (recovery_not_finished) { | |
8937 | dprintf_cont("\n"); | |
a4e96fd8 | 8938 | dprintf_cont("Rebuild has not finished yet"); |
fb12a745 TM |
8939 | if (a->last_checkpoint < mdi->recovery_start) { |
8940 | a->last_checkpoint = | |
8941 | mdi->recovery_start; | |
8942 | super->updates_pending++; | |
8943 | } | |
8944 | break; | |
94002678 | 8945 | } |
fb12a745 TM |
8946 | |
8947 | dprintf_cont(" Rebuild done, still degraded"); | |
a4e96fd8 MT |
8948 | end_migration(dev, super, map_state); |
8949 | a->last_checkpoint = 0; | |
8950 | super->updates_pending++; | |
fb12a745 TM |
8951 | |
8952 | for (i = 0; i < map->num_members; i++) { | |
8953 | int idx = get_imsm_ord_tbl_ent(dev, i, MAP_0); | |
8954 | ||
8955 | if (idx & IMSM_ORD_REBUILD) | |
8956 | map->failed_disk_num = i; | |
8957 | } | |
8958 | super->updates_pending++; | |
94002678 AK |
8959 | break; |
8960 | } | |
8961 | if (is_gen_migration(dev)) { | |
1ade5cc1 | 8962 | dprintf_cont("while general migration"); |
bf2f0071 | 8963 | if (a->last_checkpoint >= a->info.component_size) |
809da78e | 8964 | end_migration(dev, super, map_state); |
94002678 AK |
8965 | else { |
8966 | map->map_state = map_state; | |
3b451610 | 8967 | manage_second_map(super, dev); |
94002678 AK |
8968 | } |
8969 | super->updates_pending++; | |
bf2f0071 | 8970 | break; |
28bce06f | 8971 | } |
6ce1fbf1 | 8972 | if (is_initializing(dev)) { |
1ade5cc1 | 8973 | dprintf_cont("while initialization."); |
6ce1fbf1 AK |
8974 | map->map_state = map_state; |
8975 | super->updates_pending++; | |
8976 | break; | |
8977 | } | |
94002678 AK |
8978 | break; |
8979 | case IMSM_T_STATE_FAILED: /* transition to failed state */ | |
1ade5cc1 | 8980 | dprintf_cont("failed: "); |
94002678 | 8981 | if (is_gen_migration(dev)) { |
1ade5cc1 | 8982 | dprintf_cont("while general migration"); |
94002678 AK |
8983 | map->map_state = map_state; |
8984 | super->updates_pending++; | |
8985 | break; | |
8986 | } | |
8987 | if (map->map_state != map_state) { | |
1ade5cc1 | 8988 | dprintf_cont("mark failed"); |
94002678 AK |
8989 | end_migration(dev, super, map_state); |
8990 | super->updates_pending++; | |
8991 | a->last_checkpoint = 0; | |
8992 | break; | |
8993 | } | |
8994 | break; | |
8995 | default: | |
1ade5cc1 | 8996 | dprintf_cont("state %i\n", map_state); |
5802a811 | 8997 | } |
1ade5cc1 | 8998 | dprintf_cont("\n"); |
845dea95 NB |
8999 | } |
9000 | ||
f796af5d | 9001 | static int store_imsm_mpb(int fd, struct imsm_super *mpb) |
c2a1e7da | 9002 | { |
f796af5d | 9003 | void *buf = mpb; |
c2a1e7da DW |
9004 | __u32 mpb_size = __le32_to_cpu(mpb->mpb_size); |
9005 | unsigned long long dsize; | |
9006 | unsigned long long sectors; | |
f36a9ecd | 9007 | unsigned int sector_size; |
c2a1e7da | 9008 | |
aec01630 JS |
9009 | if (!get_dev_sector_size(fd, NULL, §or_size)) |
9010 | return 1; | |
c2a1e7da DW |
9011 | get_dev_size(fd, NULL, &dsize); |
9012 | ||
f36a9ecd | 9013 | if (mpb_size > sector_size) { |
272f648f | 9014 | /* -1 to account for anchor */ |
f36a9ecd | 9015 | sectors = mpb_sectors(mpb, sector_size) - 1; |
c2a1e7da | 9016 | |
272f648f | 9017 | /* write the extended mpb to the sectors preceeding the anchor */ |
f36a9ecd PB |
9018 | if (lseek64(fd, dsize - (sector_size * (2 + sectors)), |
9019 | SEEK_SET) < 0) | |
272f648f | 9020 | return 1; |
c2a1e7da | 9021 | |
f36a9ecd PB |
9022 | if ((unsigned long long)write(fd, buf + sector_size, |
9023 | sector_size * sectors) != sector_size * sectors) | |
272f648f DW |
9024 | return 1; |
9025 | } | |
c2a1e7da | 9026 | |
272f648f | 9027 | /* first block is stored on second to last sector of the disk */ |
f36a9ecd | 9028 | if (lseek64(fd, dsize - (sector_size * 2), SEEK_SET) < 0) |
c2a1e7da DW |
9029 | return 1; |
9030 | ||
466070ad | 9031 | if ((unsigned int)write(fd, buf, sector_size) != sector_size) |
c2a1e7da DW |
9032 | return 1; |
9033 | ||
c2a1e7da DW |
9034 | return 0; |
9035 | } | |
9036 | ||
2e735d19 | 9037 | static void imsm_sync_metadata(struct supertype *container) |
845dea95 | 9038 | { |
2e735d19 | 9039 | struct intel_super *super = container->sb; |
c2a1e7da | 9040 | |
1a64be56 | 9041 | dprintf("sync metadata: %d\n", super->updates_pending); |
c2a1e7da DW |
9042 | if (!super->updates_pending) |
9043 | return; | |
9044 | ||
36988a3d | 9045 | write_super_imsm(container, 0); |
c2a1e7da DW |
9046 | |
9047 | super->updates_pending = 0; | |
845dea95 NB |
9048 | } |
9049 | ||
272906ef DW |
9050 | static struct dl *imsm_readd(struct intel_super *super, int idx, struct active_array *a) |
9051 | { | |
9052 | struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member); | |
238c0a71 | 9053 | int i = get_imsm_disk_idx(dev, idx, MAP_X); |
272906ef DW |
9054 | struct dl *dl; |
9055 | ||
9056 | for (dl = super->disks; dl; dl = dl->next) | |
9057 | if (dl->index == i) | |
9058 | break; | |
9059 | ||
25ed7e59 | 9060 | if (dl && is_failed(&dl->disk)) |
272906ef DW |
9061 | dl = NULL; |
9062 | ||
9063 | if (dl) | |
1ade5cc1 | 9064 | dprintf("found %x:%x\n", dl->major, dl->minor); |
272906ef DW |
9065 | |
9066 | return dl; | |
9067 | } | |
9068 | ||
a20d2ba5 | 9069 | static struct dl *imsm_add_spare(struct intel_super *super, int slot, |
8ba77d32 AK |
9070 | struct active_array *a, int activate_new, |
9071 | struct mdinfo *additional_test_list) | |
272906ef DW |
9072 | { |
9073 | struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member); | |
238c0a71 | 9074 | int idx = get_imsm_disk_idx(dev, slot, MAP_X); |
a20d2ba5 DW |
9075 | struct imsm_super *mpb = super->anchor; |
9076 | struct imsm_map *map; | |
272906ef DW |
9077 | unsigned long long pos; |
9078 | struct mdinfo *d; | |
9079 | struct extent *ex; | |
a20d2ba5 | 9080 | int i, j; |
272906ef | 9081 | int found; |
569cc43f DW |
9082 | __u32 array_start = 0; |
9083 | __u32 array_end = 0; | |
272906ef | 9084 | struct dl *dl; |
6c932028 | 9085 | struct mdinfo *test_list; |
272906ef DW |
9086 | |
9087 | for (dl = super->disks; dl; dl = dl->next) { | |
9088 | /* If in this array, skip */ | |
9089 | for (d = a->info.devs ; d ; d = d->next) | |
4389ce73 | 9090 | if (is_fd_valid(d->state_fd) && |
e553d2a4 | 9091 | d->disk.major == dl->major && |
272906ef | 9092 | d->disk.minor == dl->minor) { |
8ba77d32 AK |
9093 | dprintf("%x:%x already in array\n", |
9094 | dl->major, dl->minor); | |
272906ef DW |
9095 | break; |
9096 | } | |
9097 | if (d) | |
9098 | continue; | |
6c932028 AK |
9099 | test_list = additional_test_list; |
9100 | while (test_list) { | |
9101 | if (test_list->disk.major == dl->major && | |
9102 | test_list->disk.minor == dl->minor) { | |
8ba77d32 AK |
9103 | dprintf("%x:%x already in additional test list\n", |
9104 | dl->major, dl->minor); | |
9105 | break; | |
9106 | } | |
6c932028 | 9107 | test_list = test_list->next; |
8ba77d32 | 9108 | } |
6c932028 | 9109 | if (test_list) |
8ba77d32 | 9110 | continue; |
272906ef | 9111 | |
e553d2a4 | 9112 | /* skip in use or failed drives */ |
25ed7e59 | 9113 | if (is_failed(&dl->disk) || idx == dl->index || |
df474657 DW |
9114 | dl->index == -2) { |
9115 | dprintf("%x:%x status (failed: %d index: %d)\n", | |
25ed7e59 | 9116 | dl->major, dl->minor, is_failed(&dl->disk), idx); |
9a1608e5 DW |
9117 | continue; |
9118 | } | |
9119 | ||
a20d2ba5 DW |
9120 | /* skip pure spares when we are looking for partially |
9121 | * assimilated drives | |
9122 | */ | |
9123 | if (dl->index == -1 && !activate_new) | |
9124 | continue; | |
9125 | ||
f2cc4f7d AO |
9126 | if (!drive_validate_sector_size(super, dl)) |
9127 | continue; | |
9128 | ||
272906ef | 9129 | /* Does this unused device have the requisite free space? |
a20d2ba5 | 9130 | * It needs to be able to cover all member volumes |
272906ef | 9131 | */ |
05501181 | 9132 | ex = get_extents(super, dl, 1); |
272906ef DW |
9133 | if (!ex) { |
9134 | dprintf("cannot get extents\n"); | |
9135 | continue; | |
9136 | } | |
a20d2ba5 DW |
9137 | for (i = 0; i < mpb->num_raid_devs; i++) { |
9138 | dev = get_imsm_dev(super, i); | |
238c0a71 | 9139 | map = get_imsm_map(dev, MAP_0); |
272906ef | 9140 | |
a20d2ba5 DW |
9141 | /* check if this disk is already a member of |
9142 | * this array | |
272906ef | 9143 | */ |
620b1713 | 9144 | if (get_imsm_disk_slot(map, dl->index) >= 0) |
a20d2ba5 DW |
9145 | continue; |
9146 | ||
9147 | found = 0; | |
9148 | j = 0; | |
9149 | pos = 0; | |
5551b113 | 9150 | array_start = pba_of_lba0(map); |
329c8278 | 9151 | array_end = array_start + |
44490938 | 9152 | per_dev_array_size(map) - 1; |
a20d2ba5 DW |
9153 | |
9154 | do { | |
9155 | /* check that we can start at pba_of_lba0 with | |
44490938 | 9156 | * num_data_stripes*blocks_per_stripe of space |
a20d2ba5 | 9157 | */ |
329c8278 | 9158 | if (array_start >= pos && array_end < ex[j].start) { |
a20d2ba5 DW |
9159 | found = 1; |
9160 | break; | |
9161 | } | |
9162 | pos = ex[j].start + ex[j].size; | |
9163 | j++; | |
9164 | } while (ex[j-1].size); | |
9165 | ||
9166 | if (!found) | |
272906ef | 9167 | break; |
a20d2ba5 | 9168 | } |
272906ef DW |
9169 | |
9170 | free(ex); | |
a20d2ba5 | 9171 | if (i < mpb->num_raid_devs) { |
329c8278 DW |
9172 | dprintf("%x:%x does not have %u to %u available\n", |
9173 | dl->major, dl->minor, array_start, array_end); | |
272906ef DW |
9174 | /* No room */ |
9175 | continue; | |
a20d2ba5 DW |
9176 | } |
9177 | return dl; | |
272906ef DW |
9178 | } |
9179 | ||
9180 | return dl; | |
9181 | } | |
9182 | ||
95d07a2c LM |
9183 | static int imsm_rebuild_allowed(struct supertype *cont, int dev_idx, int failed) |
9184 | { | |
9185 | struct imsm_dev *dev2; | |
9186 | struct imsm_map *map; | |
9187 | struct dl *idisk; | |
9188 | int slot; | |
9189 | int idx; | |
9190 | __u8 state; | |
9191 | ||
9192 | dev2 = get_imsm_dev(cont->sb, dev_idx); | |
756a15f3 MG |
9193 | |
9194 | state = imsm_check_degraded(cont->sb, dev2, failed, MAP_0); | |
9195 | if (state == IMSM_T_STATE_FAILED) { | |
9196 | map = get_imsm_map(dev2, MAP_0); | |
9197 | for (slot = 0; slot < map->num_members; slot++) { | |
9198 | /* | |
9199 | * Check if failed disks are deleted from intel | |
9200 | * disk list or are marked to be deleted | |
9201 | */ | |
9202 | idx = get_imsm_disk_idx(dev2, slot, MAP_X); | |
9203 | idisk = get_imsm_dl_disk(cont->sb, idx); | |
9204 | /* | |
9205 | * Do not rebuild the array if failed disks | |
9206 | * from failed sub-array are not removed from | |
9207 | * container. | |
9208 | */ | |
9209 | if (idisk && | |
9210 | is_failed(&idisk->disk) && | |
9211 | (idisk->action != DISK_REMOVE)) | |
9212 | return 0; | |
95d07a2c LM |
9213 | } |
9214 | } | |
9215 | return 1; | |
9216 | } | |
9217 | ||
88758e9d DW |
9218 | static struct mdinfo *imsm_activate_spare(struct active_array *a, |
9219 | struct metadata_update **updates) | |
9220 | { | |
9221 | /** | |
d23fe947 DW |
9222 | * Find a device with unused free space and use it to replace a |
9223 | * failed/vacant region in an array. We replace failed regions one a | |
9224 | * array at a time. The result is that a new spare disk will be added | |
9225 | * to the first failed array and after the monitor has finished | |
9226 | * propagating failures the remainder will be consumed. | |
88758e9d | 9227 | * |
d23fe947 DW |
9228 | * FIXME add a capability for mdmon to request spares from another |
9229 | * container. | |
88758e9d DW |
9230 | */ |
9231 | ||
9232 | struct intel_super *super = a->container->sb; | |
88758e9d | 9233 | int inst = a->info.container_member; |
949c47a0 | 9234 | struct imsm_dev *dev = get_imsm_dev(super, inst); |
238c0a71 | 9235 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
88758e9d DW |
9236 | int failed = a->info.array.raid_disks; |
9237 | struct mdinfo *rv = NULL; | |
9238 | struct mdinfo *d; | |
9239 | struct mdinfo *di; | |
9240 | struct metadata_update *mu; | |
9241 | struct dl *dl; | |
9242 | struct imsm_update_activate_spare *u; | |
9243 | int num_spares = 0; | |
9244 | int i; | |
95d07a2c | 9245 | int allowed; |
88758e9d | 9246 | |
4389ce73 MT |
9247 | for (d = a->info.devs ; d; d = d->next) { |
9248 | if (!is_fd_valid(d->state_fd)) | |
9249 | continue; | |
9250 | ||
9251 | if (d->curr_state & DS_FAULTY) | |
88758e9d DW |
9252 | /* wait for Removal to happen */ |
9253 | return NULL; | |
4389ce73 MT |
9254 | |
9255 | failed--; | |
88758e9d DW |
9256 | } |
9257 | ||
9258 | dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n", | |
9259 | inst, failed, a->info.array.raid_disks, a->info.array.level); | |
1af97990 | 9260 | |
e2962bfc AK |
9261 | if (imsm_reshape_blocks_arrays_changes(super)) |
9262 | return NULL; | |
1af97990 | 9263 | |
fc8ca064 AK |
9264 | /* Cannot activate another spare if rebuild is in progress already |
9265 | */ | |
9266 | if (is_rebuilding(dev)) { | |
7a862a02 | 9267 | dprintf("imsm: No spare activation allowed. Rebuild in progress already.\n"); |
fc8ca064 AK |
9268 | return NULL; |
9269 | } | |
9270 | ||
89c67882 AK |
9271 | if (a->info.array.level == 4) |
9272 | /* No repair for takeovered array | |
9273 | * imsm doesn't support raid4 | |
9274 | */ | |
9275 | return NULL; | |
9276 | ||
3b451610 AK |
9277 | if (imsm_check_degraded(super, dev, failed, MAP_0) != |
9278 | IMSM_T_STATE_DEGRADED) | |
88758e9d DW |
9279 | return NULL; |
9280 | ||
83ca7d45 AP |
9281 | if (get_imsm_map(dev, MAP_0)->map_state == IMSM_T_STATE_UNINITIALIZED) { |
9282 | dprintf("imsm: No spare activation allowed. Volume is not initialized.\n"); | |
9283 | return NULL; | |
9284 | } | |
9285 | ||
95d07a2c LM |
9286 | /* |
9287 | * If there are any failed disks check state of the other volume. | |
9288 | * Block rebuild if the another one is failed until failed disks | |
9289 | * are removed from container. | |
9290 | */ | |
9291 | if (failed) { | |
7a862a02 | 9292 | dprintf("found failed disks in %.*s, check if there anotherfailed sub-array.\n", |
c4acd1e5 | 9293 | MAX_RAID_SERIAL_LEN, dev->volume); |
95d07a2c LM |
9294 | /* check if states of the other volumes allow for rebuild */ |
9295 | for (i = 0; i < super->anchor->num_raid_devs; i++) { | |
9296 | if (i != inst) { | |
9297 | allowed = imsm_rebuild_allowed(a->container, | |
9298 | i, failed); | |
9299 | if (!allowed) | |
9300 | return NULL; | |
9301 | } | |
9302 | } | |
9303 | } | |
9304 | ||
88758e9d | 9305 | /* For each slot, if it is not working, find a spare */ |
88758e9d DW |
9306 | for (i = 0; i < a->info.array.raid_disks; i++) { |
9307 | for (d = a->info.devs ; d ; d = d->next) | |
9308 | if (d->disk.raid_disk == i) | |
9309 | break; | |
9310 | dprintf("found %d: %p %x\n", i, d, d?d->curr_state:0); | |
4389ce73 | 9311 | if (d && is_fd_valid(d->state_fd)) |
88758e9d DW |
9312 | continue; |
9313 | ||
272906ef | 9314 | /* |
a20d2ba5 DW |
9315 | * OK, this device needs recovery. Try to re-add the |
9316 | * previous occupant of this slot, if this fails see if | |
9317 | * we can continue the assimilation of a spare that was | |
9318 | * partially assimilated, finally try to activate a new | |
9319 | * spare. | |
272906ef DW |
9320 | */ |
9321 | dl = imsm_readd(super, i, a); | |
9322 | if (!dl) | |
b303fe21 | 9323 | dl = imsm_add_spare(super, i, a, 0, rv); |
a20d2ba5 | 9324 | if (!dl) |
b303fe21 | 9325 | dl = imsm_add_spare(super, i, a, 1, rv); |
272906ef DW |
9326 | if (!dl) |
9327 | continue; | |
1011e834 | 9328 | |
272906ef | 9329 | /* found a usable disk with enough space */ |
503975b9 | 9330 | di = xcalloc(1, sizeof(*di)); |
272906ef DW |
9331 | |
9332 | /* dl->index will be -1 in the case we are activating a | |
9333 | * pristine spare. imsm_process_update() will create a | |
9334 | * new index in this case. Once a disk is found to be | |
9335 | * failed in all member arrays it is kicked from the | |
9336 | * metadata | |
9337 | */ | |
9338 | di->disk.number = dl->index; | |
d23fe947 | 9339 | |
272906ef DW |
9340 | /* (ab)use di->devs to store a pointer to the device |
9341 | * we chose | |
9342 | */ | |
9343 | di->devs = (struct mdinfo *) dl; | |
9344 | ||
9345 | di->disk.raid_disk = i; | |
9346 | di->disk.major = dl->major; | |
9347 | di->disk.minor = dl->minor; | |
9348 | di->disk.state = 0; | |
d23534e4 | 9349 | di->recovery_start = 0; |
5551b113 | 9350 | di->data_offset = pba_of_lba0(map); |
272906ef DW |
9351 | di->component_size = a->info.component_size; |
9352 | di->container_member = inst; | |
5e46202e | 9353 | di->bb.supported = 1; |
2c8890e9 | 9354 | if (a->info.consistency_policy == CONSISTENCY_POLICY_PPL) { |
2432ce9b | 9355 | di->ppl_sector = get_ppl_sector(super, inst); |
c2462068 | 9356 | di->ppl_size = MULTIPLE_PPL_AREA_SIZE_IMSM >> 9; |
2432ce9b | 9357 | } |
148acb7b | 9358 | super->random = random32(); |
272906ef DW |
9359 | di->next = rv; |
9360 | rv = di; | |
9361 | num_spares++; | |
9362 | dprintf("%x:%x to be %d at %llu\n", dl->major, dl->minor, | |
9363 | i, di->data_offset); | |
88758e9d DW |
9364 | } |
9365 | ||
9366 | if (!rv) | |
9367 | /* No spares found */ | |
9368 | return rv; | |
9369 | /* Now 'rv' has a list of devices to return. | |
9370 | * Create a metadata_update record to update the | |
9371 | * disk_ord_tbl for the array | |
9372 | */ | |
503975b9 | 9373 | mu = xmalloc(sizeof(*mu)); |
1011e834 | 9374 | mu->buf = xcalloc(num_spares, |
503975b9 | 9375 | sizeof(struct imsm_update_activate_spare)); |
88758e9d | 9376 | mu->space = NULL; |
cb23f1f4 | 9377 | mu->space_list = NULL; |
88758e9d DW |
9378 | mu->len = sizeof(struct imsm_update_activate_spare) * num_spares; |
9379 | mu->next = *updates; | |
9380 | u = (struct imsm_update_activate_spare *) mu->buf; | |
9381 | ||
9382 | for (di = rv ; di ; di = di->next) { | |
9383 | u->type = update_activate_spare; | |
d23fe947 DW |
9384 | u->dl = (struct dl *) di->devs; |
9385 | di->devs = NULL; | |
88758e9d DW |
9386 | u->slot = di->disk.raid_disk; |
9387 | u->array = inst; | |
9388 | u->next = u + 1; | |
9389 | u++; | |
9390 | } | |
9391 | (u-1)->next = NULL; | |
9392 | *updates = mu; | |
9393 | ||
9394 | return rv; | |
9395 | } | |
9396 | ||
54c2c1ea | 9397 | static int disks_overlap(struct intel_super *super, int idx, struct imsm_update_create_array *u) |
8273f55e | 9398 | { |
54c2c1ea | 9399 | struct imsm_dev *dev = get_imsm_dev(super, idx); |
238c0a71 AK |
9400 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
9401 | struct imsm_map *new_map = get_imsm_map(&u->dev, MAP_0); | |
54c2c1ea DW |
9402 | struct disk_info *inf = get_disk_info(u); |
9403 | struct imsm_disk *disk; | |
8273f55e DW |
9404 | int i; |
9405 | int j; | |
8273f55e | 9406 | |
54c2c1ea | 9407 | for (i = 0; i < map->num_members; i++) { |
238c0a71 | 9408 | disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i, MAP_X)); |
54c2c1ea DW |
9409 | for (j = 0; j < new_map->num_members; j++) |
9410 | if (serialcmp(disk->serial, inf[j].serial) == 0) | |
8273f55e DW |
9411 | return 1; |
9412 | } | |
9413 | ||
9414 | return 0; | |
9415 | } | |
9416 | ||
1a64be56 LM |
9417 | static struct dl *get_disk_super(struct intel_super *super, int major, int minor) |
9418 | { | |
594dc1b8 JS |
9419 | struct dl *dl; |
9420 | ||
1a64be56 | 9421 | for (dl = super->disks; dl; dl = dl->next) |
089f9d79 | 9422 | if (dl->major == major && dl->minor == minor) |
1a64be56 LM |
9423 | return dl; |
9424 | return NULL; | |
9425 | } | |
9426 | ||
9427 | static int remove_disk_super(struct intel_super *super, int major, int minor) | |
9428 | { | |
594dc1b8 | 9429 | struct dl *prev; |
1a64be56 LM |
9430 | struct dl *dl; |
9431 | ||
9432 | prev = NULL; | |
9433 | for (dl = super->disks; dl; dl = dl->next) { | |
089f9d79 | 9434 | if (dl->major == major && dl->minor == minor) { |
1a64be56 LM |
9435 | /* remove */ |
9436 | if (prev) | |
9437 | prev->next = dl->next; | |
9438 | else | |
9439 | super->disks = dl->next; | |
9440 | dl->next = NULL; | |
3a85bf0e | 9441 | __free_imsm_disk(dl, 1); |
1ade5cc1 | 9442 | dprintf("removed %x:%x\n", major, minor); |
1a64be56 LM |
9443 | break; |
9444 | } | |
9445 | prev = dl; | |
9446 | } | |
9447 | return 0; | |
9448 | } | |
9449 | ||
f21e18ca | 9450 | static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index); |
ae6aad82 | 9451 | |
1a64be56 LM |
9452 | static int add_remove_disk_update(struct intel_super *super) |
9453 | { | |
9454 | int check_degraded = 0; | |
594dc1b8 JS |
9455 | struct dl *disk; |
9456 | ||
1a64be56 LM |
9457 | /* add/remove some spares to/from the metadata/contrainer */ |
9458 | while (super->disk_mgmt_list) { | |
9459 | struct dl *disk_cfg; | |
9460 | ||
9461 | disk_cfg = super->disk_mgmt_list; | |
9462 | super->disk_mgmt_list = disk_cfg->next; | |
9463 | disk_cfg->next = NULL; | |
9464 | ||
9465 | if (disk_cfg->action == DISK_ADD) { | |
9466 | disk_cfg->next = super->disks; | |
9467 | super->disks = disk_cfg; | |
9468 | check_degraded = 1; | |
1ade5cc1 N |
9469 | dprintf("added %x:%x\n", |
9470 | disk_cfg->major, disk_cfg->minor); | |
1a64be56 LM |
9471 | } else if (disk_cfg->action == DISK_REMOVE) { |
9472 | dprintf("Disk remove action processed: %x.%x\n", | |
9473 | disk_cfg->major, disk_cfg->minor); | |
9474 | disk = get_disk_super(super, | |
9475 | disk_cfg->major, | |
9476 | disk_cfg->minor); | |
9477 | if (disk) { | |
9478 | /* store action status */ | |
9479 | disk->action = DISK_REMOVE; | |
9480 | /* remove spare disks only */ | |
9481 | if (disk->index == -1) { | |
9482 | remove_disk_super(super, | |
9483 | disk_cfg->major, | |
9484 | disk_cfg->minor); | |
91c97c54 MT |
9485 | } else { |
9486 | disk_cfg->fd = disk->fd; | |
9487 | disk->fd = -1; | |
1a64be56 LM |
9488 | } |
9489 | } | |
9490 | /* release allocate disk structure */ | |
3a85bf0e | 9491 | __free_imsm_disk(disk_cfg, 1); |
1a64be56 LM |
9492 | } |
9493 | } | |
9494 | return check_degraded; | |
9495 | } | |
9496 | ||
a29911da PC |
9497 | static int apply_reshape_migration_update(struct imsm_update_reshape_migration *u, |
9498 | struct intel_super *super, | |
9499 | void ***space_list) | |
9500 | { | |
9501 | struct intel_dev *id; | |
9502 | void **tofree = NULL; | |
9503 | int ret_val = 0; | |
9504 | ||
1ade5cc1 | 9505 | dprintf("(enter)\n"); |
089f9d79 | 9506 | if (u->subdev < 0 || u->subdev > 1) { |
a29911da PC |
9507 | dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev); |
9508 | return ret_val; | |
9509 | } | |
089f9d79 | 9510 | if (space_list == NULL || *space_list == NULL) { |
a29911da PC |
9511 | dprintf("imsm: Error: Memory is not allocated\n"); |
9512 | return ret_val; | |
9513 | } | |
9514 | ||
9515 | for (id = super->devlist ; id; id = id->next) { | |
9516 | if (id->index == (unsigned)u->subdev) { | |
9517 | struct imsm_dev *dev = get_imsm_dev(super, u->subdev); | |
9518 | struct imsm_map *map; | |
9519 | struct imsm_dev *new_dev = | |
9520 | (struct imsm_dev *)*space_list; | |
238c0a71 | 9521 | struct imsm_map *migr_map = get_imsm_map(dev, MAP_1); |
a29911da PC |
9522 | int to_state; |
9523 | struct dl *new_disk; | |
9524 | ||
9525 | if (new_dev == NULL) | |
9526 | return ret_val; | |
9527 | *space_list = **space_list; | |
9528 | memcpy(new_dev, dev, sizeof_imsm_dev(dev, 0)); | |
238c0a71 | 9529 | map = get_imsm_map(new_dev, MAP_0); |
a29911da PC |
9530 | if (migr_map) { |
9531 | dprintf("imsm: Error: migration in progress"); | |
9532 | return ret_val; | |
9533 | } | |
9534 | ||
9535 | to_state = map->map_state; | |
9536 | if ((u->new_level == 5) && (map->raid_level == 0)) { | |
9537 | map->num_members++; | |
9538 | /* this should not happen */ | |
9539 | if (u->new_disks[0] < 0) { | |
9540 | map->failed_disk_num = | |
9541 | map->num_members - 1; | |
9542 | to_state = IMSM_T_STATE_DEGRADED; | |
9543 | } else | |
9544 | to_state = IMSM_T_STATE_NORMAL; | |
9545 | } | |
8e59f3d8 | 9546 | migrate(new_dev, super, to_state, MIGR_GEN_MIGR); |
a29911da PC |
9547 | if (u->new_level > -1) |
9548 | map->raid_level = u->new_level; | |
238c0a71 | 9549 | migr_map = get_imsm_map(new_dev, MAP_1); |
a29911da PC |
9550 | if ((u->new_level == 5) && |
9551 | (migr_map->raid_level == 0)) { | |
9552 | int ord = map->num_members - 1; | |
9553 | migr_map->num_members--; | |
9554 | if (u->new_disks[0] < 0) | |
9555 | ord |= IMSM_ORD_REBUILD; | |
9556 | set_imsm_ord_tbl_ent(map, | |
9557 | map->num_members - 1, | |
9558 | ord); | |
9559 | } | |
9560 | id->dev = new_dev; | |
9561 | tofree = (void **)dev; | |
9562 | ||
4bba0439 PC |
9563 | /* update chunk size |
9564 | */ | |
06fb291a | 9565 | if (u->new_chunksize > 0) { |
9529d343 MD |
9566 | struct imsm_map *dest_map = |
9567 | get_imsm_map(dev, MAP_0); | |
06fb291a | 9568 | int used_disks = |
9529d343 | 9569 | imsm_num_data_members(dest_map); |
06fb291a PB |
9570 | |
9571 | if (used_disks == 0) | |
9572 | return ret_val; | |
9573 | ||
4bba0439 PC |
9574 | map->blocks_per_strip = |
9575 | __cpu_to_le16(u->new_chunksize * 2); | |
1c275381 | 9576 | update_num_data_stripes(map, imsm_dev_size(dev)); |
06fb291a | 9577 | } |
4bba0439 | 9578 | |
44490938 MD |
9579 | /* ensure blocks_per_member has valid value |
9580 | */ | |
9581 | set_blocks_per_member(map, | |
9582 | per_dev_array_size(map) + | |
9583 | NUM_BLOCKS_DIRTY_STRIPE_REGION); | |
9584 | ||
a29911da PC |
9585 | /* add disk |
9586 | */ | |
089f9d79 JS |
9587 | if (u->new_level != 5 || migr_map->raid_level != 0 || |
9588 | migr_map->raid_level == map->raid_level) | |
a29911da PC |
9589 | goto skip_disk_add; |
9590 | ||
9591 | if (u->new_disks[0] >= 0) { | |
9592 | /* use passes spare | |
9593 | */ | |
9594 | new_disk = get_disk_super(super, | |
9595 | major(u->new_disks[0]), | |
9596 | minor(u->new_disks[0])); | |
7a862a02 | 9597 | dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n", |
a29911da PC |
9598 | major(u->new_disks[0]), |
9599 | minor(u->new_disks[0]), | |
9600 | new_disk, new_disk->index); | |
9601 | if (new_disk == NULL) | |
9602 | goto error_disk_add; | |
9603 | ||
9604 | new_disk->index = map->num_members - 1; | |
9605 | /* slot to fill in autolayout | |
9606 | */ | |
9607 | new_disk->raiddisk = new_disk->index; | |
9608 | new_disk->disk.status |= CONFIGURED_DISK; | |
9609 | new_disk->disk.status &= ~SPARE_DISK; | |
9610 | } else | |
9611 | goto error_disk_add; | |
9612 | ||
9613 | skip_disk_add: | |
9614 | *tofree = *space_list; | |
9615 | /* calculate new size | |
9616 | */ | |
f3871fdc | 9617 | imsm_set_array_size(new_dev, -1); |
a29911da PC |
9618 | |
9619 | ret_val = 1; | |
9620 | } | |
9621 | } | |
9622 | ||
9623 | if (tofree) | |
9624 | *space_list = tofree; | |
9625 | return ret_val; | |
9626 | ||
9627 | error_disk_add: | |
9628 | dprintf("Error: imsm: Cannot find disk.\n"); | |
9629 | return ret_val; | |
9630 | } | |
9631 | ||
f3871fdc AK |
9632 | static int apply_size_change_update(struct imsm_update_size_change *u, |
9633 | struct intel_super *super) | |
9634 | { | |
9635 | struct intel_dev *id; | |
9636 | int ret_val = 0; | |
9637 | ||
1ade5cc1 | 9638 | dprintf("(enter)\n"); |
089f9d79 | 9639 | if (u->subdev < 0 || u->subdev > 1) { |
f3871fdc AK |
9640 | dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev); |
9641 | return ret_val; | |
9642 | } | |
9643 | ||
9644 | for (id = super->devlist ; id; id = id->next) { | |
9645 | if (id->index == (unsigned)u->subdev) { | |
9646 | struct imsm_dev *dev = get_imsm_dev(super, u->subdev); | |
9647 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
9529d343 | 9648 | int used_disks = imsm_num_data_members(map); |
f3871fdc | 9649 | unsigned long long blocks_per_member; |
44490938 MD |
9650 | unsigned long long new_size_per_disk; |
9651 | ||
9652 | if (used_disks == 0) | |
9653 | return 0; | |
f3871fdc AK |
9654 | |
9655 | /* calculate new size | |
9656 | */ | |
44490938 MD |
9657 | new_size_per_disk = u->new_size / used_disks; |
9658 | blocks_per_member = new_size_per_disk + | |
9659 | NUM_BLOCKS_DIRTY_STRIPE_REGION; | |
f3871fdc | 9660 | |
1c275381 MT |
9661 | imsm_set_array_size(dev, u->new_size); |
9662 | set_blocks_per_member(map, blocks_per_member); | |
9663 | update_num_data_stripes(map, u->new_size); | |
f3871fdc AK |
9664 | ret_val = 1; |
9665 | break; | |
9666 | } | |
9667 | } | |
9668 | ||
9669 | return ret_val; | |
9670 | } | |
9671 | ||
69d40de4 JR |
9672 | static int prepare_spare_to_activate(struct supertype *st, |
9673 | struct imsm_update_activate_spare *u) | |
9674 | { | |
9675 | struct intel_super *super = st->sb; | |
9676 | int prev_current_vol = super->current_vol; | |
9677 | struct active_array *a; | |
9678 | int ret = 1; | |
9679 | ||
9680 | for (a = st->arrays; a; a = a->next) | |
9681 | /* | |
9682 | * Additional initialization (adding bitmap header, filling | |
9683 | * the bitmap area with '1's to force initial rebuild for a whole | |
9684 | * data-area) is required when adding the spare to the volume | |
9685 | * with write-intent bitmap. | |
9686 | */ | |
9687 | if (a->info.container_member == u->array && | |
9688 | a->info.consistency_policy == CONSISTENCY_POLICY_BITMAP) { | |
9689 | struct dl *dl; | |
9690 | ||
9691 | for (dl = super->disks; dl; dl = dl->next) | |
9692 | if (dl == u->dl) | |
9693 | break; | |
9694 | if (!dl) | |
9695 | break; | |
9696 | ||
9697 | super->current_vol = u->array; | |
9698 | if (st->ss->write_bitmap(st, dl->fd, NoUpdate)) | |
9699 | ret = 0; | |
9700 | super->current_vol = prev_current_vol; | |
9701 | } | |
9702 | return ret; | |
9703 | } | |
9704 | ||
061d7da3 | 9705 | static int apply_update_activate_spare(struct imsm_update_activate_spare *u, |
ca9de185 | 9706 | struct intel_super *super, |
061d7da3 LO |
9707 | struct active_array *active_array) |
9708 | { | |
9709 | struct imsm_super *mpb = super->anchor; | |
9710 | struct imsm_dev *dev = get_imsm_dev(super, u->array); | |
238c0a71 | 9711 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
061d7da3 LO |
9712 | struct imsm_map *migr_map; |
9713 | struct active_array *a; | |
9714 | struct imsm_disk *disk; | |
9715 | __u8 to_state; | |
9716 | struct dl *dl; | |
9717 | unsigned int found; | |
9718 | int failed; | |
5961eeec | 9719 | int victim; |
061d7da3 | 9720 | int i; |
5961eeec | 9721 | int second_map_created = 0; |
061d7da3 | 9722 | |
5961eeec | 9723 | for (; u; u = u->next) { |
238c0a71 | 9724 | victim = get_imsm_disk_idx(dev, u->slot, MAP_X); |
061d7da3 | 9725 | |
5961eeec | 9726 | if (victim < 0) |
9727 | return 0; | |
061d7da3 | 9728 | |
5961eeec | 9729 | for (dl = super->disks; dl; dl = dl->next) |
9730 | if (dl == u->dl) | |
9731 | break; | |
061d7da3 | 9732 | |
5961eeec | 9733 | if (!dl) { |
7a862a02 | 9734 | pr_err("error: imsm_activate_spare passed an unknown disk (index: %d)\n", |
5961eeec | 9735 | u->dl->index); |
9736 | return 0; | |
9737 | } | |
061d7da3 | 9738 | |
5961eeec | 9739 | /* count failures (excluding rebuilds and the victim) |
9740 | * to determine map[0] state | |
9741 | */ | |
9742 | failed = 0; | |
9743 | for (i = 0; i < map->num_members; i++) { | |
9744 | if (i == u->slot) | |
9745 | continue; | |
9746 | disk = get_imsm_disk(super, | |
238c0a71 | 9747 | get_imsm_disk_idx(dev, i, MAP_X)); |
5961eeec | 9748 | if (!disk || is_failed(disk)) |
9749 | failed++; | |
9750 | } | |
061d7da3 | 9751 | |
5961eeec | 9752 | /* adding a pristine spare, assign a new index */ |
9753 | if (dl->index < 0) { | |
9754 | dl->index = super->anchor->num_disks; | |
9755 | super->anchor->num_disks++; | |
9756 | } | |
9757 | disk = &dl->disk; | |
9758 | disk->status |= CONFIGURED_DISK; | |
9759 | disk->status &= ~SPARE_DISK; | |
9760 | ||
9761 | /* mark rebuild */ | |
238c0a71 | 9762 | to_state = imsm_check_degraded(super, dev, failed, MAP_0); |
5961eeec | 9763 | if (!second_map_created) { |
9764 | second_map_created = 1; | |
9765 | map->map_state = IMSM_T_STATE_DEGRADED; | |
9766 | migrate(dev, super, to_state, MIGR_REBUILD); | |
9767 | } else | |
9768 | map->map_state = to_state; | |
238c0a71 | 9769 | migr_map = get_imsm_map(dev, MAP_1); |
5961eeec | 9770 | set_imsm_ord_tbl_ent(map, u->slot, dl->index); |
9771 | set_imsm_ord_tbl_ent(migr_map, u->slot, | |
9772 | dl->index | IMSM_ORD_REBUILD); | |
9773 | ||
9774 | /* update the family_num to mark a new container | |
9775 | * generation, being careful to record the existing | |
9776 | * family_num in orig_family_num to clean up after | |
9777 | * earlier mdadm versions that neglected to set it. | |
9778 | */ | |
9779 | if (mpb->orig_family_num == 0) | |
9780 | mpb->orig_family_num = mpb->family_num; | |
9781 | mpb->family_num += super->random; | |
9782 | ||
9783 | /* count arrays using the victim in the metadata */ | |
9784 | found = 0; | |
9785 | for (a = active_array; a ; a = a->next) { | |
76c152ca | 9786 | int dev_idx = a->info.container_member; |
061d7da3 | 9787 | |
76c152ca | 9788 | if (get_disk_slot_in_dev(super, dev_idx, victim) >= 0) |
5961eeec | 9789 | found++; |
9790 | } | |
061d7da3 | 9791 | |
5961eeec | 9792 | /* delete the victim if it is no longer being |
9793 | * utilized anywhere | |
061d7da3 | 9794 | */ |
5961eeec | 9795 | if (!found) { |
9796 | struct dl **dlp; | |
061d7da3 | 9797 | |
5961eeec | 9798 | /* We know that 'manager' isn't touching anything, |
9799 | * so it is safe to delete | |
9800 | */ | |
9801 | for (dlp = &super->disks; *dlp; dlp = &(*dlp)->next) | |
061d7da3 LO |
9802 | if ((*dlp)->index == victim) |
9803 | break; | |
5961eeec | 9804 | |
9805 | /* victim may be on the missing list */ | |
9806 | if (!*dlp) | |
9807 | for (dlp = &super->missing; *dlp; | |
9808 | dlp = &(*dlp)->next) | |
9809 | if ((*dlp)->index == victim) | |
9810 | break; | |
9811 | imsm_delete(super, dlp, victim); | |
9812 | } | |
061d7da3 LO |
9813 | } |
9814 | ||
9815 | return 1; | |
9816 | } | |
a29911da | 9817 | |
2e5dc010 N |
9818 | static int apply_reshape_container_disks_update(struct imsm_update_reshape *u, |
9819 | struct intel_super *super, | |
9820 | void ***space_list) | |
9821 | { | |
9822 | struct dl *new_disk; | |
9823 | struct intel_dev *id; | |
9824 | int i; | |
9825 | int delta_disks = u->new_raid_disks - u->old_raid_disks; | |
ee4beede | 9826 | int disk_count = u->old_raid_disks; |
2e5dc010 N |
9827 | void **tofree = NULL; |
9828 | int devices_to_reshape = 1; | |
9829 | struct imsm_super *mpb = super->anchor; | |
9830 | int ret_val = 0; | |
d098291a | 9831 | unsigned int dev_id; |
2e5dc010 | 9832 | |
1ade5cc1 | 9833 | dprintf("(enter)\n"); |
2e5dc010 N |
9834 | |
9835 | /* enable spares to use in array */ | |
9836 | for (i = 0; i < delta_disks; i++) { | |
9837 | new_disk = get_disk_super(super, | |
9838 | major(u->new_disks[i]), | |
9839 | minor(u->new_disks[i])); | |
7a862a02 | 9840 | dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n", |
2e5dc010 N |
9841 | major(u->new_disks[i]), minor(u->new_disks[i]), |
9842 | new_disk, new_disk->index); | |
089f9d79 JS |
9843 | if (new_disk == NULL || |
9844 | (new_disk->index >= 0 && | |
9845 | new_disk->index < u->old_raid_disks)) | |
2e5dc010 | 9846 | goto update_reshape_exit; |
ee4beede | 9847 | new_disk->index = disk_count++; |
2e5dc010 N |
9848 | /* slot to fill in autolayout |
9849 | */ | |
9850 | new_disk->raiddisk = new_disk->index; | |
9851 | new_disk->disk.status |= | |
9852 | CONFIGURED_DISK; | |
9853 | new_disk->disk.status &= ~SPARE_DISK; | |
9854 | } | |
9855 | ||
ed7333bd AK |
9856 | dprintf("imsm: volume set mpb->num_raid_devs = %i\n", |
9857 | mpb->num_raid_devs); | |
2e5dc010 N |
9858 | /* manage changes in volume |
9859 | */ | |
d098291a | 9860 | for (dev_id = 0; dev_id < mpb->num_raid_devs; dev_id++) { |
2e5dc010 N |
9861 | void **sp = *space_list; |
9862 | struct imsm_dev *newdev; | |
9863 | struct imsm_map *newmap, *oldmap; | |
9864 | ||
d098291a AK |
9865 | for (id = super->devlist ; id; id = id->next) { |
9866 | if (id->index == dev_id) | |
9867 | break; | |
9868 | } | |
9869 | if (id == NULL) | |
9870 | break; | |
2e5dc010 N |
9871 | if (!sp) |
9872 | continue; | |
9873 | *space_list = *sp; | |
9874 | newdev = (void*)sp; | |
9875 | /* Copy the dev, but not (all of) the map */ | |
9876 | memcpy(newdev, id->dev, sizeof(*newdev)); | |
238c0a71 AK |
9877 | oldmap = get_imsm_map(id->dev, MAP_0); |
9878 | newmap = get_imsm_map(newdev, MAP_0); | |
2e5dc010 N |
9879 | /* Copy the current map */ |
9880 | memcpy(newmap, oldmap, sizeof_imsm_map(oldmap)); | |
9881 | /* update one device only | |
9882 | */ | |
9883 | if (devices_to_reshape) { | |
ed7333bd AK |
9884 | dprintf("imsm: modifying subdev: %i\n", |
9885 | id->index); | |
2e5dc010 N |
9886 | devices_to_reshape--; |
9887 | newdev->vol.migr_state = 1; | |
4036e7ee | 9888 | set_vol_curr_migr_unit(newdev, 0); |
ea672ee1 | 9889 | set_migr_type(newdev, MIGR_GEN_MIGR); |
2e5dc010 N |
9890 | newmap->num_members = u->new_raid_disks; |
9891 | for (i = 0; i < delta_disks; i++) { | |
9892 | set_imsm_ord_tbl_ent(newmap, | |
9893 | u->old_raid_disks + i, | |
9894 | u->old_raid_disks + i); | |
9895 | } | |
9896 | /* New map is correct, now need to save old map | |
9897 | */ | |
238c0a71 | 9898 | newmap = get_imsm_map(newdev, MAP_1); |
2e5dc010 N |
9899 | memcpy(newmap, oldmap, sizeof_imsm_map(oldmap)); |
9900 | ||
f3871fdc | 9901 | imsm_set_array_size(newdev, -1); |
2e5dc010 N |
9902 | } |
9903 | ||
9904 | sp = (void **)id->dev; | |
9905 | id->dev = newdev; | |
9906 | *sp = tofree; | |
9907 | tofree = sp; | |
8e59f3d8 AK |
9908 | |
9909 | /* Clear migration record */ | |
9910 | memset(super->migr_rec, 0, sizeof(struct migr_record)); | |
2e5dc010 | 9911 | } |
819bc634 AK |
9912 | if (tofree) |
9913 | *space_list = tofree; | |
2e5dc010 N |
9914 | ret_val = 1; |
9915 | ||
9916 | update_reshape_exit: | |
9917 | ||
9918 | return ret_val; | |
9919 | } | |
9920 | ||
bb025c2f | 9921 | static int apply_takeover_update(struct imsm_update_takeover *u, |
8ca6df95 KW |
9922 | struct intel_super *super, |
9923 | void ***space_list) | |
bb025c2f KW |
9924 | { |
9925 | struct imsm_dev *dev = NULL; | |
8ca6df95 KW |
9926 | struct intel_dev *dv; |
9927 | struct imsm_dev *dev_new; | |
bb025c2f KW |
9928 | struct imsm_map *map; |
9929 | struct dl *dm, *du; | |
8ca6df95 | 9930 | int i; |
bb025c2f KW |
9931 | |
9932 | for (dv = super->devlist; dv; dv = dv->next) | |
9933 | if (dv->index == (unsigned int)u->subarray) { | |
9934 | dev = dv->dev; | |
9935 | break; | |
9936 | } | |
9937 | ||
9938 | if (dev == NULL) | |
9939 | return 0; | |
9940 | ||
238c0a71 | 9941 | map = get_imsm_map(dev, MAP_0); |
bb025c2f KW |
9942 | |
9943 | if (u->direction == R10_TO_R0) { | |
43d5ec18 | 9944 | /* Number of failed disks must be half of initial disk number */ |
3b451610 AK |
9945 | if (imsm_count_failed(super, dev, MAP_0) != |
9946 | (map->num_members / 2)) | |
43d5ec18 KW |
9947 | return 0; |
9948 | ||
bb025c2f KW |
9949 | /* iterate through devices to mark removed disks as spare */ |
9950 | for (dm = super->disks; dm; dm = dm->next) { | |
9951 | if (dm->disk.status & FAILED_DISK) { | |
9952 | int idx = dm->index; | |
9953 | /* update indexes on the disk list */ | |
9954 | /* FIXME this loop-with-the-loop looks wrong, I'm not convinced | |
9955 | the index values will end up being correct.... NB */ | |
9956 | for (du = super->disks; du; du = du->next) | |
9957 | if (du->index > idx) | |
9958 | du->index--; | |
9959 | /* mark as spare disk */ | |
a8619d23 | 9960 | mark_spare(dm); |
bb025c2f KW |
9961 | } |
9962 | } | |
bb025c2f | 9963 | /* update map */ |
1c275381 | 9964 | map->num_members /= map->num_domains; |
bb025c2f | 9965 | map->map_state = IMSM_T_STATE_NORMAL; |
bb025c2f | 9966 | map->raid_level = 0; |
1c275381 MT |
9967 | set_num_domains(map); |
9968 | update_num_data_stripes(map, imsm_dev_size(dev)); | |
bb025c2f KW |
9969 | map->failed_disk_num = -1; |
9970 | } | |
9971 | ||
8ca6df95 KW |
9972 | if (u->direction == R0_TO_R10) { |
9973 | void **space; | |
4a353e6e | 9974 | |
8ca6df95 KW |
9975 | /* update slots in current disk list */ |
9976 | for (dm = super->disks; dm; dm = dm->next) { | |
9977 | if (dm->index >= 0) | |
9978 | dm->index *= 2; | |
9979 | } | |
9980 | /* create new *missing* disks */ | |
9981 | for (i = 0; i < map->num_members; i++) { | |
9982 | space = *space_list; | |
9983 | if (!space) | |
9984 | continue; | |
9985 | *space_list = *space; | |
9986 | du = (void *)space; | |
9987 | memcpy(du, super->disks, sizeof(*du)); | |
8ca6df95 KW |
9988 | du->fd = -1; |
9989 | du->minor = 0; | |
9990 | du->major = 0; | |
9991 | du->index = (i * 2) + 1; | |
9992 | sprintf((char *)du->disk.serial, | |
9993 | " MISSING_%d", du->index); | |
9994 | sprintf((char *)du->serial, | |
9995 | "MISSING_%d", du->index); | |
9996 | du->next = super->missing; | |
9997 | super->missing = du; | |
9998 | } | |
9999 | /* create new dev and map */ | |
10000 | space = *space_list; | |
10001 | if (!space) | |
10002 | return 0; | |
10003 | *space_list = *space; | |
10004 | dev_new = (void *)space; | |
10005 | memcpy(dev_new, dev, sizeof(*dev)); | |
10006 | /* update new map */ | |
238c0a71 | 10007 | map = get_imsm_map(dev_new, MAP_0); |
1c275381 | 10008 | |
1a2487c2 | 10009 | map->map_state = IMSM_T_STATE_DEGRADED; |
8ca6df95 | 10010 | map->raid_level = 1; |
1c275381 MT |
10011 | set_num_domains(map); |
10012 | map->num_members = map->num_members * map->num_domains; | |
10013 | update_num_data_stripes(map, imsm_dev_size(dev)); | |
4a353e6e | 10014 | |
8ca6df95 KW |
10015 | /* replace dev<->dev_new */ |
10016 | dv->dev = dev_new; | |
10017 | } | |
bb025c2f KW |
10018 | /* update disk order table */ |
10019 | for (du = super->disks; du; du = du->next) | |
10020 | if (du->index >= 0) | |
10021 | set_imsm_ord_tbl_ent(map, du->index, du->index); | |
8ca6df95 | 10022 | for (du = super->missing; du; du = du->next) |
1a2487c2 KW |
10023 | if (du->index >= 0) { |
10024 | set_imsm_ord_tbl_ent(map, du->index, du->index); | |
4c9e8c1e | 10025 | mark_missing(super, dv->dev, &du->disk, du->index); |
1a2487c2 | 10026 | } |
bb025c2f KW |
10027 | |
10028 | return 1; | |
10029 | } | |
10030 | ||
e8319a19 DW |
10031 | static void imsm_process_update(struct supertype *st, |
10032 | struct metadata_update *update) | |
10033 | { | |
10034 | /** | |
10035 | * crack open the metadata_update envelope to find the update record | |
10036 | * update can be one of: | |
d195167d AK |
10037 | * update_reshape_container_disks - all the arrays in the container |
10038 | * are being reshaped to have more devices. We need to mark | |
10039 | * the arrays for general migration and convert selected spares | |
10040 | * into active devices. | |
10041 | * update_activate_spare - a spare device has replaced a failed | |
1011e834 N |
10042 | * device in an array, update the disk_ord_tbl. If this disk is |
10043 | * present in all member arrays then also clear the SPARE_DISK | |
10044 | * flag | |
d195167d AK |
10045 | * update_create_array |
10046 | * update_kill_array | |
10047 | * update_rename_array | |
10048 | * update_add_remove_disk | |
e8319a19 DW |
10049 | */ |
10050 | struct intel_super *super = st->sb; | |
4d7b1503 | 10051 | struct imsm_super *mpb; |
e8319a19 DW |
10052 | enum imsm_update_type type = *(enum imsm_update_type *) update->buf; |
10053 | ||
4d7b1503 DW |
10054 | /* update requires a larger buf but the allocation failed */ |
10055 | if (super->next_len && !super->next_buf) { | |
10056 | super->next_len = 0; | |
10057 | return; | |
10058 | } | |
10059 | ||
10060 | if (super->next_buf) { | |
10061 | memcpy(super->next_buf, super->buf, super->len); | |
10062 | free(super->buf); | |
10063 | super->len = super->next_len; | |
10064 | super->buf = super->next_buf; | |
10065 | ||
10066 | super->next_len = 0; | |
10067 | super->next_buf = NULL; | |
10068 | } | |
10069 | ||
10070 | mpb = super->anchor; | |
10071 | ||
e8319a19 | 10072 | switch (type) { |
0ec5d470 AK |
10073 | case update_general_migration_checkpoint: { |
10074 | struct intel_dev *id; | |
10075 | struct imsm_update_general_migration_checkpoint *u = | |
10076 | (void *)update->buf; | |
10077 | ||
1ade5cc1 | 10078 | dprintf("called for update_general_migration_checkpoint\n"); |
0ec5d470 AK |
10079 | |
10080 | /* find device under general migration */ | |
10081 | for (id = super->devlist ; id; id = id->next) { | |
10082 | if (is_gen_migration(id->dev)) { | |
4036e7ee MT |
10083 | set_vol_curr_migr_unit(id->dev, |
10084 | u->curr_migr_unit); | |
0ec5d470 AK |
10085 | super->updates_pending++; |
10086 | } | |
10087 | } | |
10088 | break; | |
10089 | } | |
bb025c2f KW |
10090 | case update_takeover: { |
10091 | struct imsm_update_takeover *u = (void *)update->buf; | |
1a2487c2 KW |
10092 | if (apply_takeover_update(u, super, &update->space_list)) { |
10093 | imsm_update_version_info(super); | |
bb025c2f | 10094 | super->updates_pending++; |
1a2487c2 | 10095 | } |
bb025c2f KW |
10096 | break; |
10097 | } | |
10098 | ||
78b10e66 | 10099 | case update_reshape_container_disks: { |
d195167d | 10100 | struct imsm_update_reshape *u = (void *)update->buf; |
2e5dc010 N |
10101 | if (apply_reshape_container_disks_update( |
10102 | u, super, &update->space_list)) | |
10103 | super->updates_pending++; | |
78b10e66 N |
10104 | break; |
10105 | } | |
48c5303a | 10106 | case update_reshape_migration: { |
a29911da PC |
10107 | struct imsm_update_reshape_migration *u = (void *)update->buf; |
10108 | if (apply_reshape_migration_update( | |
10109 | u, super, &update->space_list)) | |
10110 | super->updates_pending++; | |
48c5303a PC |
10111 | break; |
10112 | } | |
f3871fdc AK |
10113 | case update_size_change: { |
10114 | struct imsm_update_size_change *u = (void *)update->buf; | |
10115 | if (apply_size_change_update(u, super)) | |
10116 | super->updates_pending++; | |
10117 | break; | |
10118 | } | |
e8319a19 | 10119 | case update_activate_spare: { |
1011e834 | 10120 | struct imsm_update_activate_spare *u = (void *) update->buf; |
69d40de4 JR |
10121 | |
10122 | if (prepare_spare_to_activate(st, u) && | |
10123 | apply_update_activate_spare(u, super, st->arrays)) | |
061d7da3 | 10124 | super->updates_pending++; |
8273f55e DW |
10125 | break; |
10126 | } | |
10127 | case update_create_array: { | |
10128 | /* someone wants to create a new array, we need to be aware of | |
10129 | * a few races/collisions: | |
10130 | * 1/ 'Create' called by two separate instances of mdadm | |
10131 | * 2/ 'Create' versus 'activate_spare': mdadm has chosen | |
10132 | * devices that have since been assimilated via | |
10133 | * activate_spare. | |
10134 | * In the event this update can not be carried out mdadm will | |
10135 | * (FIX ME) notice that its update did not take hold. | |
10136 | */ | |
10137 | struct imsm_update_create_array *u = (void *) update->buf; | |
ba2de7ba | 10138 | struct intel_dev *dv; |
8273f55e DW |
10139 | struct imsm_dev *dev; |
10140 | struct imsm_map *map, *new_map; | |
10141 | unsigned long long start, end; | |
10142 | unsigned long long new_start, new_end; | |
10143 | int i; | |
54c2c1ea DW |
10144 | struct disk_info *inf; |
10145 | struct dl *dl; | |
8273f55e DW |
10146 | |
10147 | /* handle racing creates: first come first serve */ | |
10148 | if (u->dev_idx < mpb->num_raid_devs) { | |
1ade5cc1 | 10149 | dprintf("subarray %d already defined\n", u->dev_idx); |
ba2de7ba | 10150 | goto create_error; |
8273f55e DW |
10151 | } |
10152 | ||
10153 | /* check update is next in sequence */ | |
10154 | if (u->dev_idx != mpb->num_raid_devs) { | |
1ade5cc1 N |
10155 | dprintf("can not create array %d expected index %d\n", |
10156 | u->dev_idx, mpb->num_raid_devs); | |
ba2de7ba | 10157 | goto create_error; |
8273f55e DW |
10158 | } |
10159 | ||
238c0a71 | 10160 | new_map = get_imsm_map(&u->dev, MAP_0); |
5551b113 | 10161 | new_start = pba_of_lba0(new_map); |
44490938 | 10162 | new_end = new_start + per_dev_array_size(new_map); |
54c2c1ea | 10163 | inf = get_disk_info(u); |
8273f55e DW |
10164 | |
10165 | /* handle activate_spare versus create race: | |
10166 | * check to make sure that overlapping arrays do not include | |
10167 | * overalpping disks | |
10168 | */ | |
10169 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
949c47a0 | 10170 | dev = get_imsm_dev(super, i); |
238c0a71 | 10171 | map = get_imsm_map(dev, MAP_0); |
5551b113 | 10172 | start = pba_of_lba0(map); |
44490938 | 10173 | end = start + per_dev_array_size(map); |
8273f55e DW |
10174 | if ((new_start >= start && new_start <= end) || |
10175 | (start >= new_start && start <= new_end)) | |
54c2c1ea DW |
10176 | /* overlap */; |
10177 | else | |
10178 | continue; | |
10179 | ||
10180 | if (disks_overlap(super, i, u)) { | |
1ade5cc1 | 10181 | dprintf("arrays overlap\n"); |
ba2de7ba | 10182 | goto create_error; |
8273f55e DW |
10183 | } |
10184 | } | |
8273f55e | 10185 | |
949c47a0 DW |
10186 | /* check that prepare update was successful */ |
10187 | if (!update->space) { | |
1ade5cc1 | 10188 | dprintf("prepare update failed\n"); |
ba2de7ba | 10189 | goto create_error; |
949c47a0 DW |
10190 | } |
10191 | ||
54c2c1ea DW |
10192 | /* check that all disks are still active before committing |
10193 | * changes. FIXME: could we instead handle this by creating a | |
10194 | * degraded array? That's probably not what the user expects, | |
10195 | * so better to drop this update on the floor. | |
10196 | */ | |
10197 | for (i = 0; i < new_map->num_members; i++) { | |
10198 | dl = serial_to_dl(inf[i].serial, super); | |
10199 | if (!dl) { | |
1ade5cc1 | 10200 | dprintf("disk disappeared\n"); |
ba2de7ba | 10201 | goto create_error; |
54c2c1ea | 10202 | } |
949c47a0 DW |
10203 | } |
10204 | ||
8273f55e | 10205 | super->updates_pending++; |
54c2c1ea DW |
10206 | |
10207 | /* convert spares to members and fixup ord_tbl */ | |
10208 | for (i = 0; i < new_map->num_members; i++) { | |
10209 | dl = serial_to_dl(inf[i].serial, super); | |
10210 | if (dl->index == -1) { | |
10211 | dl->index = mpb->num_disks; | |
10212 | mpb->num_disks++; | |
10213 | dl->disk.status |= CONFIGURED_DISK; | |
10214 | dl->disk.status &= ~SPARE_DISK; | |
10215 | } | |
10216 | set_imsm_ord_tbl_ent(new_map, i, dl->index); | |
10217 | } | |
10218 | ||
ba2de7ba DW |
10219 | dv = update->space; |
10220 | dev = dv->dev; | |
949c47a0 DW |
10221 | update->space = NULL; |
10222 | imsm_copy_dev(dev, &u->dev); | |
ba2de7ba DW |
10223 | dv->index = u->dev_idx; |
10224 | dv->next = super->devlist; | |
10225 | super->devlist = dv; | |
8273f55e | 10226 | mpb->num_raid_devs++; |
8273f55e | 10227 | |
4d1313e9 | 10228 | imsm_update_version_info(super); |
8273f55e | 10229 | break; |
ba2de7ba DW |
10230 | create_error: |
10231 | /* mdmon knows how to release update->space, but not | |
10232 | * ((struct intel_dev *) update->space)->dev | |
10233 | */ | |
10234 | if (update->space) { | |
10235 | dv = update->space; | |
10236 | free(dv->dev); | |
10237 | } | |
8273f55e | 10238 | break; |
e8319a19 | 10239 | } |
33414a01 DW |
10240 | case update_kill_array: { |
10241 | struct imsm_update_kill_array *u = (void *) update->buf; | |
10242 | int victim = u->dev_idx; | |
10243 | struct active_array *a; | |
10244 | struct intel_dev **dp; | |
33414a01 DW |
10245 | |
10246 | /* sanity check that we are not affecting the uuid of | |
10247 | * active arrays, or deleting an active array | |
10248 | * | |
10249 | * FIXME when immutable ids are available, but note that | |
10250 | * we'll also need to fixup the invalidated/active | |
10251 | * subarray indexes in mdstat | |
10252 | */ | |
10253 | for (a = st->arrays; a; a = a->next) | |
10254 | if (a->info.container_member >= victim) | |
10255 | break; | |
10256 | /* by definition if mdmon is running at least one array | |
10257 | * is active in the container, so checking | |
10258 | * mpb->num_raid_devs is just extra paranoia | |
10259 | */ | |
756a15f3 | 10260 | if (a || mpb->num_raid_devs == 1 || victim >= super->anchor->num_raid_devs) { |
33414a01 DW |
10261 | dprintf("failed to delete subarray-%d\n", victim); |
10262 | break; | |
10263 | } | |
10264 | ||
10265 | for (dp = &super->devlist; *dp;) | |
f21e18ca | 10266 | if ((*dp)->index == (unsigned)super->current_vol) { |
33414a01 DW |
10267 | *dp = (*dp)->next; |
10268 | } else { | |
f21e18ca | 10269 | if ((*dp)->index > (unsigned)victim) |
33414a01 DW |
10270 | (*dp)->index--; |
10271 | dp = &(*dp)->next; | |
10272 | } | |
10273 | mpb->num_raid_devs--; | |
10274 | super->updates_pending++; | |
10275 | break; | |
10276 | } | |
aa534678 DW |
10277 | case update_rename_array: { |
10278 | struct imsm_update_rename_array *u = (void *) update->buf; | |
10279 | char name[MAX_RAID_SERIAL_LEN+1]; | |
10280 | int target = u->dev_idx; | |
10281 | struct active_array *a; | |
10282 | struct imsm_dev *dev; | |
10283 | ||
10284 | /* sanity check that we are not affecting the uuid of | |
10285 | * an active array | |
10286 | */ | |
40659392 | 10287 | memset(name, 0, sizeof(name)); |
aa534678 DW |
10288 | snprintf(name, MAX_RAID_SERIAL_LEN, "%s", (char *) u->name); |
10289 | name[MAX_RAID_SERIAL_LEN] = '\0'; | |
10290 | for (a = st->arrays; a; a = a->next) | |
10291 | if (a->info.container_member == target) | |
10292 | break; | |
10293 | dev = get_imsm_dev(super, u->dev_idx); | |
e2eb503b MT |
10294 | |
10295 | if (a || !dev || imsm_is_name_allowed(super, name, 0) == false) { | |
aa534678 DW |
10296 | dprintf("failed to rename subarray-%d\n", target); |
10297 | break; | |
10298 | } | |
10299 | ||
40659392 | 10300 | memcpy(dev->volume, name, MAX_RAID_SERIAL_LEN); |
aa534678 DW |
10301 | super->updates_pending++; |
10302 | break; | |
10303 | } | |
1a64be56 | 10304 | case update_add_remove_disk: { |
43dad3d6 | 10305 | /* we may be able to repair some arrays if disks are |
095b8088 | 10306 | * being added, check the status of add_remove_disk |
1a64be56 LM |
10307 | * if discs has been added. |
10308 | */ | |
10309 | if (add_remove_disk_update(super)) { | |
43dad3d6 | 10310 | struct active_array *a; |
072b727f DW |
10311 | |
10312 | super->updates_pending++; | |
1a64be56 | 10313 | for (a = st->arrays; a; a = a->next) |
43dad3d6 DW |
10314 | a->check_degraded = 1; |
10315 | } | |
43dad3d6 | 10316 | break; |
e8319a19 | 10317 | } |
bbab0940 TM |
10318 | case update_prealloc_badblocks_mem: |
10319 | break; | |
e6e9dd3f AP |
10320 | case update_rwh_policy: { |
10321 | struct imsm_update_rwh_policy *u = (void *)update->buf; | |
10322 | int target = u->dev_idx; | |
10323 | struct imsm_dev *dev = get_imsm_dev(super, target); | |
e6e9dd3f AP |
10324 | |
10325 | if (dev->rwh_policy != u->new_policy) { | |
10326 | dev->rwh_policy = u->new_policy; | |
10327 | super->updates_pending++; | |
10328 | } | |
10329 | break; | |
10330 | } | |
1a64be56 | 10331 | default: |
ebf3be99 | 10332 | pr_err("error: unsupported process update type:(type: %d)\n", type); |
1a64be56 | 10333 | } |
e8319a19 | 10334 | } |
88758e9d | 10335 | |
bc0b9d34 PC |
10336 | static struct mdinfo *get_spares_for_grow(struct supertype *st); |
10337 | ||
5fe6f031 N |
10338 | static int imsm_prepare_update(struct supertype *st, |
10339 | struct metadata_update *update) | |
8273f55e | 10340 | { |
949c47a0 | 10341 | /** |
4d7b1503 DW |
10342 | * Allocate space to hold new disk entries, raid-device entries or a new |
10343 | * mpb if necessary. The manager synchronously waits for updates to | |
10344 | * complete in the monitor, so new mpb buffers allocated here can be | |
10345 | * integrated by the monitor thread without worrying about live pointers | |
10346 | * in the manager thread. | |
8273f55e | 10347 | */ |
095b8088 | 10348 | enum imsm_update_type type; |
4d7b1503 | 10349 | struct intel_super *super = st->sb; |
f36a9ecd | 10350 | unsigned int sector_size = super->sector_size; |
4d7b1503 DW |
10351 | struct imsm_super *mpb = super->anchor; |
10352 | size_t buf_len; | |
10353 | size_t len = 0; | |
949c47a0 | 10354 | |
095b8088 N |
10355 | if (update->len < (int)sizeof(type)) |
10356 | return 0; | |
10357 | ||
10358 | type = *(enum imsm_update_type *) update->buf; | |
10359 | ||
949c47a0 | 10360 | switch (type) { |
0ec5d470 | 10361 | case update_general_migration_checkpoint: |
095b8088 N |
10362 | if (update->len < (int)sizeof(struct imsm_update_general_migration_checkpoint)) |
10363 | return 0; | |
1ade5cc1 | 10364 | dprintf("called for update_general_migration_checkpoint\n"); |
0ec5d470 | 10365 | break; |
abedf5fc KW |
10366 | case update_takeover: { |
10367 | struct imsm_update_takeover *u = (void *)update->buf; | |
095b8088 N |
10368 | if (update->len < (int)sizeof(*u)) |
10369 | return 0; | |
abedf5fc KW |
10370 | if (u->direction == R0_TO_R10) { |
10371 | void **tail = (void **)&update->space_list; | |
10372 | struct imsm_dev *dev = get_imsm_dev(super, u->subarray); | |
238c0a71 | 10373 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
abedf5fc KW |
10374 | int num_members = map->num_members; |
10375 | void *space; | |
10376 | int size, i; | |
abedf5fc KW |
10377 | /* allocate memory for added disks */ |
10378 | for (i = 0; i < num_members; i++) { | |
10379 | size = sizeof(struct dl); | |
503975b9 | 10380 | space = xmalloc(size); |
abedf5fc KW |
10381 | *tail = space; |
10382 | tail = space; | |
10383 | *tail = NULL; | |
10384 | } | |
10385 | /* allocate memory for new device */ | |
10386 | size = sizeof_imsm_dev(super->devlist->dev, 0) + | |
10387 | (num_members * sizeof(__u32)); | |
503975b9 N |
10388 | space = xmalloc(size); |
10389 | *tail = space; | |
10390 | tail = space; | |
10391 | *tail = NULL; | |
10392 | len = disks_to_mpb_size(num_members * 2); | |
abedf5fc KW |
10393 | } |
10394 | ||
10395 | break; | |
10396 | } | |
78b10e66 | 10397 | case update_reshape_container_disks: { |
d195167d AK |
10398 | /* Every raid device in the container is about to |
10399 | * gain some more devices, and we will enter a | |
10400 | * reconfiguration. | |
10401 | * So each 'imsm_map' will be bigger, and the imsm_vol | |
10402 | * will now hold 2 of them. | |
10403 | * Thus we need new 'struct imsm_dev' allocations sized | |
10404 | * as sizeof_imsm_dev but with more devices in both maps. | |
10405 | */ | |
10406 | struct imsm_update_reshape *u = (void *)update->buf; | |
10407 | struct intel_dev *dl; | |
10408 | void **space_tail = (void**)&update->space_list; | |
10409 | ||
095b8088 N |
10410 | if (update->len < (int)sizeof(*u)) |
10411 | return 0; | |
10412 | ||
1ade5cc1 | 10413 | dprintf("for update_reshape\n"); |
d195167d AK |
10414 | |
10415 | for (dl = super->devlist; dl; dl = dl->next) { | |
10416 | int size = sizeof_imsm_dev(dl->dev, 1); | |
10417 | void *s; | |
d677e0b8 AK |
10418 | if (u->new_raid_disks > u->old_raid_disks) |
10419 | size += sizeof(__u32)*2* | |
10420 | (u->new_raid_disks - u->old_raid_disks); | |
503975b9 | 10421 | s = xmalloc(size); |
d195167d AK |
10422 | *space_tail = s; |
10423 | space_tail = s; | |
10424 | *space_tail = NULL; | |
10425 | } | |
10426 | ||
10427 | len = disks_to_mpb_size(u->new_raid_disks); | |
10428 | dprintf("New anchor length is %llu\n", (unsigned long long)len); | |
78b10e66 N |
10429 | break; |
10430 | } | |
48c5303a | 10431 | case update_reshape_migration: { |
bc0b9d34 PC |
10432 | /* for migration level 0->5 we need to add disks |
10433 | * so the same as for container operation we will copy | |
10434 | * device to the bigger location. | |
10435 | * in memory prepared device and new disk area are prepared | |
10436 | * for usage in process update | |
10437 | */ | |
10438 | struct imsm_update_reshape_migration *u = (void *)update->buf; | |
10439 | struct intel_dev *id; | |
10440 | void **space_tail = (void **)&update->space_list; | |
10441 | int size; | |
10442 | void *s; | |
10443 | int current_level = -1; | |
10444 | ||
095b8088 N |
10445 | if (update->len < (int)sizeof(*u)) |
10446 | return 0; | |
10447 | ||
1ade5cc1 | 10448 | dprintf("for update_reshape\n"); |
bc0b9d34 PC |
10449 | |
10450 | /* add space for bigger array in update | |
10451 | */ | |
10452 | for (id = super->devlist; id; id = id->next) { | |
10453 | if (id->index == (unsigned)u->subdev) { | |
10454 | size = sizeof_imsm_dev(id->dev, 1); | |
10455 | if (u->new_raid_disks > u->old_raid_disks) | |
10456 | size += sizeof(__u32)*2* | |
10457 | (u->new_raid_disks - u->old_raid_disks); | |
503975b9 | 10458 | s = xmalloc(size); |
bc0b9d34 PC |
10459 | *space_tail = s; |
10460 | space_tail = s; | |
10461 | *space_tail = NULL; | |
10462 | break; | |
10463 | } | |
10464 | } | |
10465 | if (update->space_list == NULL) | |
10466 | break; | |
10467 | ||
10468 | /* add space for disk in update | |
10469 | */ | |
10470 | size = sizeof(struct dl); | |
503975b9 | 10471 | s = xmalloc(size); |
bc0b9d34 PC |
10472 | *space_tail = s; |
10473 | space_tail = s; | |
10474 | *space_tail = NULL; | |
10475 | ||
10476 | /* add spare device to update | |
10477 | */ | |
10478 | for (id = super->devlist ; id; id = id->next) | |
10479 | if (id->index == (unsigned)u->subdev) { | |
10480 | struct imsm_dev *dev; | |
10481 | struct imsm_map *map; | |
10482 | ||
10483 | dev = get_imsm_dev(super, u->subdev); | |
238c0a71 | 10484 | map = get_imsm_map(dev, MAP_0); |
bc0b9d34 PC |
10485 | current_level = map->raid_level; |
10486 | break; | |
10487 | } | |
089f9d79 | 10488 | if (u->new_level == 5 && u->new_level != current_level) { |
bc0b9d34 PC |
10489 | struct mdinfo *spares; |
10490 | ||
10491 | spares = get_spares_for_grow(st); | |
10492 | if (spares) { | |
10493 | struct dl *dl; | |
10494 | struct mdinfo *dev; | |
10495 | ||
10496 | dev = spares->devs; | |
10497 | if (dev) { | |
10498 | u->new_disks[0] = | |
10499 | makedev(dev->disk.major, | |
10500 | dev->disk.minor); | |
10501 | dl = get_disk_super(super, | |
10502 | dev->disk.major, | |
10503 | dev->disk.minor); | |
10504 | dl->index = u->old_raid_disks; | |
10505 | dev = dev->next; | |
10506 | } | |
10507 | sysfs_free(spares); | |
10508 | } | |
10509 | } | |
10510 | len = disks_to_mpb_size(u->new_raid_disks); | |
10511 | dprintf("New anchor length is %llu\n", (unsigned long long)len); | |
48c5303a PC |
10512 | break; |
10513 | } | |
f3871fdc | 10514 | case update_size_change: { |
095b8088 N |
10515 | if (update->len < (int)sizeof(struct imsm_update_size_change)) |
10516 | return 0; | |
10517 | break; | |
10518 | } | |
10519 | case update_activate_spare: { | |
10520 | if (update->len < (int)sizeof(struct imsm_update_activate_spare)) | |
10521 | return 0; | |
f3871fdc AK |
10522 | break; |
10523 | } | |
949c47a0 DW |
10524 | case update_create_array: { |
10525 | struct imsm_update_create_array *u = (void *) update->buf; | |
ba2de7ba | 10526 | struct intel_dev *dv; |
54c2c1ea | 10527 | struct imsm_dev *dev = &u->dev; |
238c0a71 | 10528 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
54c2c1ea DW |
10529 | struct dl *dl; |
10530 | struct disk_info *inf; | |
10531 | int i; | |
10532 | int activate = 0; | |
949c47a0 | 10533 | |
095b8088 N |
10534 | if (update->len < (int)sizeof(*u)) |
10535 | return 0; | |
10536 | ||
54c2c1ea DW |
10537 | inf = get_disk_info(u); |
10538 | len = sizeof_imsm_dev(dev, 1); | |
ba2de7ba | 10539 | /* allocate a new super->devlist entry */ |
503975b9 N |
10540 | dv = xmalloc(sizeof(*dv)); |
10541 | dv->dev = xmalloc(len); | |
10542 | update->space = dv; | |
949c47a0 | 10543 | |
54c2c1ea DW |
10544 | /* count how many spares will be converted to members */ |
10545 | for (i = 0; i < map->num_members; i++) { | |
10546 | dl = serial_to_dl(inf[i].serial, super); | |
10547 | if (!dl) { | |
10548 | /* hmm maybe it failed?, nothing we can do about | |
10549 | * it here | |
10550 | */ | |
10551 | continue; | |
10552 | } | |
10553 | if (count_memberships(dl, super) == 0) | |
10554 | activate++; | |
10555 | } | |
10556 | len += activate * sizeof(struct imsm_disk); | |
949c47a0 | 10557 | break; |
095b8088 N |
10558 | } |
10559 | case update_kill_array: { | |
10560 | if (update->len < (int)sizeof(struct imsm_update_kill_array)) | |
10561 | return 0; | |
949c47a0 DW |
10562 | break; |
10563 | } | |
095b8088 N |
10564 | case update_rename_array: { |
10565 | if (update->len < (int)sizeof(struct imsm_update_rename_array)) | |
10566 | return 0; | |
10567 | break; | |
10568 | } | |
10569 | case update_add_remove_disk: | |
10570 | /* no update->len needed */ | |
10571 | break; | |
bbab0940 TM |
10572 | case update_prealloc_badblocks_mem: |
10573 | super->extra_space += sizeof(struct bbm_log) - | |
10574 | get_imsm_bbm_log_size(super->bbm_log); | |
10575 | break; | |
e6e9dd3f AP |
10576 | case update_rwh_policy: { |
10577 | if (update->len < (int)sizeof(struct imsm_update_rwh_policy)) | |
10578 | return 0; | |
10579 | break; | |
10580 | } | |
095b8088 N |
10581 | default: |
10582 | return 0; | |
949c47a0 | 10583 | } |
8273f55e | 10584 | |
4d7b1503 DW |
10585 | /* check if we need a larger metadata buffer */ |
10586 | if (super->next_buf) | |
10587 | buf_len = super->next_len; | |
10588 | else | |
10589 | buf_len = super->len; | |
10590 | ||
bbab0940 | 10591 | if (__le32_to_cpu(mpb->mpb_size) + super->extra_space + len > buf_len) { |
4d7b1503 DW |
10592 | /* ok we need a larger buf than what is currently allocated |
10593 | * if this allocation fails process_update will notice that | |
10594 | * ->next_len is set and ->next_buf is NULL | |
10595 | */ | |
bbab0940 TM |
10596 | buf_len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + |
10597 | super->extra_space + len, sector_size); | |
4d7b1503 DW |
10598 | if (super->next_buf) |
10599 | free(super->next_buf); | |
10600 | ||
10601 | super->next_len = buf_len; | |
f36a9ecd | 10602 | if (posix_memalign(&super->next_buf, sector_size, buf_len) == 0) |
1f45a8ad DW |
10603 | memset(super->next_buf, 0, buf_len); |
10604 | else | |
4d7b1503 DW |
10605 | super->next_buf = NULL; |
10606 | } | |
5fe6f031 | 10607 | return 1; |
8273f55e DW |
10608 | } |
10609 | ||
ae6aad82 | 10610 | /* must be called while manager is quiesced */ |
f21e18ca | 10611 | static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index) |
ae6aad82 DW |
10612 | { |
10613 | struct imsm_super *mpb = super->anchor; | |
ae6aad82 DW |
10614 | struct dl *iter; |
10615 | struct imsm_dev *dev; | |
10616 | struct imsm_map *map; | |
4c9e8c1e | 10617 | unsigned int i, j, num_members; |
fb12a745 | 10618 | __u32 ord, ord_map0; |
4c9e8c1e | 10619 | struct bbm_log *log = super->bbm_log; |
ae6aad82 | 10620 | |
1ade5cc1 | 10621 | dprintf("deleting device[%d] from imsm_super\n", index); |
ae6aad82 DW |
10622 | |
10623 | /* shift all indexes down one */ | |
10624 | for (iter = super->disks; iter; iter = iter->next) | |
f21e18ca | 10625 | if (iter->index > (int)index) |
ae6aad82 | 10626 | iter->index--; |
47ee5a45 | 10627 | for (iter = super->missing; iter; iter = iter->next) |
f21e18ca | 10628 | if (iter->index > (int)index) |
47ee5a45 | 10629 | iter->index--; |
ae6aad82 DW |
10630 | |
10631 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
10632 | dev = get_imsm_dev(super, i); | |
238c0a71 | 10633 | map = get_imsm_map(dev, MAP_0); |
24565c9a DW |
10634 | num_members = map->num_members; |
10635 | for (j = 0; j < num_members; j++) { | |
10636 | /* update ord entries being careful not to propagate | |
10637 | * ord-flags to the first map | |
10638 | */ | |
238c0a71 | 10639 | ord = get_imsm_ord_tbl_ent(dev, j, MAP_X); |
fb12a745 | 10640 | ord_map0 = get_imsm_ord_tbl_ent(dev, j, MAP_0); |
ae6aad82 | 10641 | |
24565c9a DW |
10642 | if (ord_to_idx(ord) <= index) |
10643 | continue; | |
ae6aad82 | 10644 | |
238c0a71 | 10645 | map = get_imsm_map(dev, MAP_0); |
fb12a745 | 10646 | set_imsm_ord_tbl_ent(map, j, ord_map0 - 1); |
238c0a71 | 10647 | map = get_imsm_map(dev, MAP_1); |
24565c9a DW |
10648 | if (map) |
10649 | set_imsm_ord_tbl_ent(map, j, ord - 1); | |
ae6aad82 DW |
10650 | } |
10651 | } | |
10652 | ||
4c9e8c1e TM |
10653 | for (i = 0; i < log->entry_count; i++) { |
10654 | struct bbm_log_entry *entry = &log->marked_block_entries[i]; | |
10655 | ||
10656 | if (entry->disk_ordinal <= index) | |
10657 | continue; | |
10658 | entry->disk_ordinal--; | |
10659 | } | |
10660 | ||
ae6aad82 DW |
10661 | mpb->num_disks--; |
10662 | super->updates_pending++; | |
24565c9a DW |
10663 | if (*dlp) { |
10664 | struct dl *dl = *dlp; | |
10665 | ||
10666 | *dlp = (*dlp)->next; | |
3a85bf0e | 10667 | __free_imsm_disk(dl, 1); |
24565c9a | 10668 | } |
ae6aad82 | 10669 | } |
9a717282 | 10670 | |
9a717282 AK |
10671 | static int imsm_get_allowed_degradation(int level, int raid_disks, |
10672 | struct intel_super *super, | |
10673 | struct imsm_dev *dev) | |
10674 | { | |
10675 | switch (level) { | |
bf5cf7c7 | 10676 | case 1: |
9a717282 AK |
10677 | case 10:{ |
10678 | int ret_val = 0; | |
10679 | struct imsm_map *map; | |
10680 | int i; | |
10681 | ||
10682 | ret_val = raid_disks/2; | |
10683 | /* check map if all disks pairs not failed | |
10684 | * in both maps | |
10685 | */ | |
238c0a71 | 10686 | map = get_imsm_map(dev, MAP_0); |
9a717282 AK |
10687 | for (i = 0; i < ret_val; i++) { |
10688 | int degradation = 0; | |
10689 | if (get_imsm_disk(super, i) == NULL) | |
10690 | degradation++; | |
10691 | if (get_imsm_disk(super, i + 1) == NULL) | |
10692 | degradation++; | |
10693 | if (degradation == 2) | |
10694 | return 0; | |
10695 | } | |
238c0a71 | 10696 | map = get_imsm_map(dev, MAP_1); |
9a717282 AK |
10697 | /* if there is no second map |
10698 | * result can be returned | |
10699 | */ | |
10700 | if (map == NULL) | |
10701 | return ret_val; | |
10702 | /* check degradation in second map | |
10703 | */ | |
10704 | for (i = 0; i < ret_val; i++) { | |
10705 | int degradation = 0; | |
10706 | if (get_imsm_disk(super, i) == NULL) | |
10707 | degradation++; | |
10708 | if (get_imsm_disk(super, i + 1) == NULL) | |
10709 | degradation++; | |
10710 | if (degradation == 2) | |
10711 | return 0; | |
10712 | } | |
10713 | return ret_val; | |
10714 | } | |
10715 | case 5: | |
10716 | return 1; | |
10717 | case 6: | |
10718 | return 2; | |
10719 | default: | |
10720 | return 0; | |
10721 | } | |
10722 | } | |
10723 | ||
d31ad643 PB |
10724 | /******************************************************************************* |
10725 | * Function: validate_container_imsm | |
10726 | * Description: This routine validates container after assemble, | |
10727 | * eg. if devices in container are under the same controller. | |
10728 | * | |
10729 | * Parameters: | |
10730 | * info : linked list with info about devices used in array | |
10731 | * Returns: | |
10732 | * 1 : HBA mismatch | |
10733 | * 0 : Success | |
10734 | ******************************************************************************/ | |
10735 | int validate_container_imsm(struct mdinfo *info) | |
10736 | { | |
420dafcd | 10737 | if (check_no_platform()) |
6b781d33 | 10738 | return 0; |
d31ad643 | 10739 | |
6b781d33 AP |
10740 | struct sys_dev *idev; |
10741 | struct sys_dev *hba = NULL; | |
10742 | struct sys_dev *intel_devices = find_intel_devices(); | |
10743 | char *dev_path = devt_to_devpath(makedev(info->disk.major, | |
7c798f87 | 10744 | info->disk.minor), 1, NULL); |
6b781d33 AP |
10745 | |
10746 | for (idev = intel_devices; idev; idev = idev->next) { | |
10747 | if (dev_path && strstr(dev_path, idev->path)) { | |
10748 | hba = idev; | |
10749 | break; | |
d31ad643 | 10750 | } |
6b781d33 AP |
10751 | } |
10752 | if (dev_path) | |
d31ad643 PB |
10753 | free(dev_path); |
10754 | ||
6b781d33 AP |
10755 | if (!hba) { |
10756 | pr_err("WARNING - Cannot detect HBA for device %s!\n", | |
10757 | devid2kname(makedev(info->disk.major, info->disk.minor))); | |
10758 | return 1; | |
10759 | } | |
10760 | ||
10761 | const struct imsm_orom *orom = get_orom_by_device_id(hba->dev_id); | |
10762 | struct mdinfo *dev; | |
10763 | ||
10764 | for (dev = info->next; dev; dev = dev->next) { | |
7c798f87 MT |
10765 | dev_path = devt_to_devpath(makedev(dev->disk.major, |
10766 | dev->disk.minor), 1, NULL); | |
6b781d33 AP |
10767 | |
10768 | struct sys_dev *hba2 = NULL; | |
10769 | for (idev = intel_devices; idev; idev = idev->next) { | |
10770 | if (dev_path && strstr(dev_path, idev->path)) { | |
10771 | hba2 = idev; | |
10772 | break; | |
d31ad643 PB |
10773 | } |
10774 | } | |
6b781d33 AP |
10775 | if (dev_path) |
10776 | free(dev_path); | |
10777 | ||
10778 | const struct imsm_orom *orom2 = hba2 == NULL ? NULL : | |
10779 | get_orom_by_device_id(hba2->dev_id); | |
10780 | ||
10781 | if (hba2 && hba->type != hba2->type) { | |
10782 | pr_err("WARNING - HBAs of devices do not match %s != %s\n", | |
10783 | get_sys_dev_type(hba->type), get_sys_dev_type(hba2->type)); | |
10784 | return 1; | |
10785 | } | |
10786 | ||
07cb1e57 | 10787 | if (orom != orom2) { |
6b781d33 AP |
10788 | pr_err("WARNING - IMSM container assembled with disks under different HBAs!\n" |
10789 | " This operation is not supported and can lead to data loss.\n"); | |
10790 | return 1; | |
10791 | } | |
10792 | ||
10793 | if (!orom) { | |
10794 | pr_err("WARNING - IMSM container assembled with disks under HBAs without IMSM platform support!\n" | |
10795 | " This operation is not supported and can lead to data loss.\n"); | |
10796 | return 1; | |
10797 | } | |
d31ad643 | 10798 | } |
6b781d33 | 10799 | |
d31ad643 PB |
10800 | return 0; |
10801 | } | |
32141c17 | 10802 | |
6f50473f TM |
10803 | /******************************************************************************* |
10804 | * Function: imsm_record_badblock | |
10805 | * Description: This routine stores new bad block record in BBM log | |
10806 | * | |
10807 | * Parameters: | |
10808 | * a : array containing a bad block | |
10809 | * slot : disk number containing a bad block | |
10810 | * sector : bad block sector | |
10811 | * length : bad block sectors range | |
10812 | * Returns: | |
10813 | * 1 : Success | |
10814 | * 0 : Error | |
10815 | ******************************************************************************/ | |
10816 | static int imsm_record_badblock(struct active_array *a, int slot, | |
10817 | unsigned long long sector, int length) | |
10818 | { | |
10819 | struct intel_super *super = a->container->sb; | |
10820 | int ord; | |
10821 | int ret; | |
10822 | ||
10823 | ord = imsm_disk_slot_to_ord(a, slot); | |
10824 | if (ord < 0) | |
10825 | return 0; | |
10826 | ||
10827 | ret = record_new_badblock(super->bbm_log, ord_to_idx(ord), sector, | |
10828 | length); | |
10829 | if (ret) | |
10830 | super->updates_pending++; | |
10831 | ||
10832 | return ret; | |
10833 | } | |
c07a5a4f TM |
10834 | /******************************************************************************* |
10835 | * Function: imsm_clear_badblock | |
10836 | * Description: This routine clears bad block record from BBM log | |
10837 | * | |
10838 | * Parameters: | |
10839 | * a : array containing a bad block | |
10840 | * slot : disk number containing a bad block | |
10841 | * sector : bad block sector | |
10842 | * length : bad block sectors range | |
10843 | * Returns: | |
10844 | * 1 : Success | |
10845 | * 0 : Error | |
10846 | ******************************************************************************/ | |
10847 | static int imsm_clear_badblock(struct active_array *a, int slot, | |
10848 | unsigned long long sector, int length) | |
10849 | { | |
10850 | struct intel_super *super = a->container->sb; | |
10851 | int ord; | |
10852 | int ret; | |
10853 | ||
10854 | ord = imsm_disk_slot_to_ord(a, slot); | |
10855 | if (ord < 0) | |
10856 | return 0; | |
10857 | ||
10858 | ret = clear_badblock(super->bbm_log, ord_to_idx(ord), sector, length); | |
10859 | if (ret) | |
10860 | super->updates_pending++; | |
10861 | ||
10862 | return ret; | |
10863 | } | |
928f1424 TM |
10864 | /******************************************************************************* |
10865 | * Function: imsm_get_badblocks | |
10866 | * Description: This routine get list of bad blocks for an array | |
10867 | * | |
10868 | * Parameters: | |
10869 | * a : array | |
10870 | * slot : disk number | |
10871 | * Returns: | |
10872 | * bb : structure containing bad blocks | |
10873 | * NULL : error | |
10874 | ******************************************************************************/ | |
10875 | static struct md_bb *imsm_get_badblocks(struct active_array *a, int slot) | |
10876 | { | |
10877 | int inst = a->info.container_member; | |
10878 | struct intel_super *super = a->container->sb; | |
10879 | struct imsm_dev *dev = get_imsm_dev(super, inst); | |
10880 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
10881 | int ord; | |
10882 | ||
10883 | ord = imsm_disk_slot_to_ord(a, slot); | |
10884 | if (ord < 0) | |
10885 | return NULL; | |
10886 | ||
10887 | get_volume_badblocks(super->bbm_log, ord_to_idx(ord), pba_of_lba0(map), | |
44490938 | 10888 | per_dev_array_size(map), &super->bb); |
928f1424 TM |
10889 | |
10890 | return &super->bb; | |
10891 | } | |
27156a57 TM |
10892 | /******************************************************************************* |
10893 | * Function: examine_badblocks_imsm | |
10894 | * Description: Prints list of bad blocks on a disk to the standard output | |
10895 | * | |
10896 | * Parameters: | |
10897 | * st : metadata handler | |
10898 | * fd : open file descriptor for device | |
10899 | * devname : device name | |
10900 | * Returns: | |
10901 | * 0 : Success | |
10902 | * 1 : Error | |
10903 | ******************************************************************************/ | |
10904 | static int examine_badblocks_imsm(struct supertype *st, int fd, char *devname) | |
10905 | { | |
10906 | struct intel_super *super = st->sb; | |
10907 | struct bbm_log *log = super->bbm_log; | |
10908 | struct dl *d = NULL; | |
10909 | int any = 0; | |
10910 | ||
10911 | for (d = super->disks; d ; d = d->next) { | |
10912 | if (strcmp(d->devname, devname) == 0) | |
10913 | break; | |
10914 | } | |
10915 | ||
10916 | if ((d == NULL) || (d->index < 0)) { /* serial mismatch probably */ | |
10917 | pr_err("%s doesn't appear to be part of a raid array\n", | |
10918 | devname); | |
10919 | return 1; | |
10920 | } | |
10921 | ||
10922 | if (log != NULL) { | |
10923 | unsigned int i; | |
10924 | struct bbm_log_entry *entry = &log->marked_block_entries[0]; | |
10925 | ||
10926 | for (i = 0; i < log->entry_count; i++) { | |
10927 | if (entry[i].disk_ordinal == d->index) { | |
10928 | unsigned long long sector = __le48_to_cpu( | |
10929 | &entry[i].defective_block_start); | |
10930 | int cnt = entry[i].marked_count + 1; | |
10931 | ||
10932 | if (!any) { | |
10933 | printf("Bad-blocks on %s:\n", devname); | |
10934 | any = 1; | |
10935 | } | |
10936 | ||
10937 | printf("%20llu for %d sectors\n", sector, cnt); | |
10938 | } | |
10939 | } | |
10940 | } | |
10941 | ||
10942 | if (!any) | |
10943 | printf("No bad-blocks list configured on %s\n", devname); | |
10944 | ||
10945 | return 0; | |
10946 | } | |
687629c2 AK |
10947 | /******************************************************************************* |
10948 | * Function: init_migr_record_imsm | |
10949 | * Description: Function inits imsm migration record | |
10950 | * Parameters: | |
10951 | * super : imsm internal array info | |
10952 | * dev : device under migration | |
10953 | * info : general array info to find the smallest device | |
10954 | * Returns: | |
10955 | * none | |
10956 | ******************************************************************************/ | |
10957 | void init_migr_record_imsm(struct supertype *st, struct imsm_dev *dev, | |
10958 | struct mdinfo *info) | |
10959 | { | |
10960 | struct intel_super *super = st->sb; | |
10961 | struct migr_record *migr_rec = super->migr_rec; | |
10962 | int new_data_disks; | |
10963 | unsigned long long dsize, dev_sectors; | |
10964 | long long unsigned min_dev_sectors = -1LLU; | |
238c0a71 AK |
10965 | struct imsm_map *map_dest = get_imsm_map(dev, MAP_0); |
10966 | struct imsm_map *map_src = get_imsm_map(dev, MAP_1); | |
687629c2 | 10967 | unsigned long long num_migr_units; |
3ef4403c | 10968 | unsigned long long array_blocks; |
2f86fda3 | 10969 | struct dl *dl_disk = NULL; |
687629c2 AK |
10970 | |
10971 | memset(migr_rec, 0, sizeof(struct migr_record)); | |
10972 | migr_rec->family_num = __cpu_to_le32(super->anchor->family_num); | |
10973 | ||
10974 | /* only ascending reshape supported now */ | |
10975 | migr_rec->ascending_migr = __cpu_to_le32(1); | |
10976 | ||
10977 | migr_rec->dest_depth_per_unit = GEN_MIGR_AREA_SIZE / | |
10978 | max(map_dest->blocks_per_strip, map_src->blocks_per_strip); | |
e1742195 AK |
10979 | migr_rec->dest_depth_per_unit *= |
10980 | max(map_dest->blocks_per_strip, map_src->blocks_per_strip); | |
9529d343 | 10981 | new_data_disks = imsm_num_data_members(map_dest); |
687629c2 AK |
10982 | migr_rec->blocks_per_unit = |
10983 | __cpu_to_le32(migr_rec->dest_depth_per_unit * new_data_disks); | |
10984 | migr_rec->dest_depth_per_unit = | |
10985 | __cpu_to_le32(migr_rec->dest_depth_per_unit); | |
3ef4403c | 10986 | array_blocks = info->component_size * new_data_disks; |
687629c2 AK |
10987 | num_migr_units = |
10988 | array_blocks / __le32_to_cpu(migr_rec->blocks_per_unit); | |
10989 | ||
10990 | if (array_blocks % __le32_to_cpu(migr_rec->blocks_per_unit)) | |
10991 | num_migr_units++; | |
9f421827 | 10992 | set_num_migr_units(migr_rec, num_migr_units); |
687629c2 AK |
10993 | |
10994 | migr_rec->post_migr_vol_cap = dev->size_low; | |
10995 | migr_rec->post_migr_vol_cap_hi = dev->size_high; | |
10996 | ||
687629c2 | 10997 | /* Find the smallest dev */ |
2f86fda3 MT |
10998 | for (dl_disk = super->disks; dl_disk ; dl_disk = dl_disk->next) { |
10999 | /* ignore spares in container */ | |
11000 | if (dl_disk->index < 0) | |
687629c2 | 11001 | continue; |
2f86fda3 | 11002 | get_dev_size(dl_disk->fd, NULL, &dsize); |
687629c2 AK |
11003 | dev_sectors = dsize / 512; |
11004 | if (dev_sectors < min_dev_sectors) | |
11005 | min_dev_sectors = dev_sectors; | |
687629c2 | 11006 | } |
9f421827 | 11007 | set_migr_chkp_area_pba(migr_rec, min_dev_sectors - |
687629c2 AK |
11008 | RAID_DISK_RESERVED_BLOCKS_IMSM_HI); |
11009 | ||
11010 | write_imsm_migr_rec(st); | |
11011 | ||
11012 | return; | |
11013 | } | |
11014 | ||
11015 | /******************************************************************************* | |
11016 | * Function: save_backup_imsm | |
11017 | * Description: Function saves critical data stripes to Migration Copy Area | |
11018 | * and updates the current migration unit status. | |
11019 | * Use restore_stripes() to form a destination stripe, | |
11020 | * and to write it to the Copy Area. | |
11021 | * Parameters: | |
11022 | * st : supertype information | |
aea93171 | 11023 | * dev : imsm device that backup is saved for |
687629c2 AK |
11024 | * info : general array info |
11025 | * buf : input buffer | |
687629c2 AK |
11026 | * length : length of data to backup (blocks_per_unit) |
11027 | * Returns: | |
11028 | * 0 : success | |
11029 | *, -1 : fail | |
11030 | ******************************************************************************/ | |
11031 | int save_backup_imsm(struct supertype *st, | |
11032 | struct imsm_dev *dev, | |
11033 | struct mdinfo *info, | |
11034 | void *buf, | |
687629c2 AK |
11035 | int length) |
11036 | { | |
11037 | int rv = -1; | |
11038 | struct intel_super *super = st->sb; | |
687629c2 | 11039 | int i; |
238c0a71 | 11040 | struct imsm_map *map_dest = get_imsm_map(dev, MAP_0); |
687629c2 | 11041 | int new_disks = map_dest->num_members; |
ab724b98 | 11042 | int dest_layout = 0; |
4389ce73 MT |
11043 | int dest_chunk, targets[new_disks]; |
11044 | unsigned long long start, target_offsets[new_disks]; | |
9529d343 | 11045 | int data_disks = imsm_num_data_members(map_dest); |
687629c2 | 11046 | |
2f86fda3 MT |
11047 | for (i = 0; i < new_disks; i++) { |
11048 | struct dl *dl_disk = get_imsm_dl_disk(super, i); | |
4389ce73 MT |
11049 | if (dl_disk && is_fd_valid(dl_disk->fd)) |
11050 | targets[i] = dl_disk->fd; | |
11051 | else | |
11052 | goto abort; | |
2f86fda3 | 11053 | } |
7e45b550 | 11054 | |
d1877f69 | 11055 | start = info->reshape_progress * 512; |
687629c2 | 11056 | for (i = 0; i < new_disks; i++) { |
9f421827 | 11057 | target_offsets[i] = migr_chkp_area_pba(super->migr_rec) * 512; |
d1877f69 AK |
11058 | /* move back copy area adderss, it will be moved forward |
11059 | * in restore_stripes() using start input variable | |
11060 | */ | |
11061 | target_offsets[i] -= start/data_disks; | |
687629c2 AK |
11062 | } |
11063 | ||
68eb8bc6 | 11064 | dest_layout = imsm_level_to_layout(map_dest->raid_level); |
ab724b98 AK |
11065 | dest_chunk = __le16_to_cpu(map_dest->blocks_per_strip) * 512; |
11066 | ||
687629c2 AK |
11067 | if (restore_stripes(targets, /* list of dest devices */ |
11068 | target_offsets, /* migration record offsets */ | |
11069 | new_disks, | |
ab724b98 AK |
11070 | dest_chunk, |
11071 | map_dest->raid_level, | |
11072 | dest_layout, | |
11073 | -1, /* source backup file descriptor */ | |
11074 | 0, /* input buf offset | |
11075 | * always 0 buf is already offseted */ | |
d1877f69 | 11076 | start, |
687629c2 AK |
11077 | length, |
11078 | buf) != 0) { | |
e7b84f9d | 11079 | pr_err("Error restoring stripes\n"); |
687629c2 AK |
11080 | goto abort; |
11081 | } | |
11082 | ||
11083 | rv = 0; | |
11084 | ||
11085 | abort: | |
687629c2 AK |
11086 | return rv; |
11087 | } | |
11088 | ||
11089 | /******************************************************************************* | |
11090 | * Function: save_checkpoint_imsm | |
11091 | * Description: Function called for current unit status update | |
11092 | * in the migration record. It writes it to disk. | |
11093 | * Parameters: | |
11094 | * super : imsm internal array info | |
11095 | * info : general array info | |
11096 | * Returns: | |
11097 | * 0: success | |
11098 | * 1: failure | |
0228d92c AK |
11099 | * 2: failure, means no valid migration record |
11100 | * / no general migration in progress / | |
687629c2 AK |
11101 | ******************************************************************************/ |
11102 | int save_checkpoint_imsm(struct supertype *st, struct mdinfo *info, int state) | |
11103 | { | |
11104 | struct intel_super *super = st->sb; | |
f8b72ef5 AK |
11105 | unsigned long long blocks_per_unit; |
11106 | unsigned long long curr_migr_unit; | |
11107 | ||
2f86fda3 | 11108 | if (load_imsm_migr_rec(super) != 0) { |
7a862a02 | 11109 | dprintf("imsm: ERROR: Cannot read migration record for checkpoint save.\n"); |
2e062e82 AK |
11110 | return 1; |
11111 | } | |
11112 | ||
f8b72ef5 AK |
11113 | blocks_per_unit = __le32_to_cpu(super->migr_rec->blocks_per_unit); |
11114 | if (blocks_per_unit == 0) { | |
0228d92c AK |
11115 | dprintf("imsm: no migration in progress.\n"); |
11116 | return 2; | |
687629c2 | 11117 | } |
f8b72ef5 AK |
11118 | curr_migr_unit = info->reshape_progress / blocks_per_unit; |
11119 | /* check if array is alligned to copy area | |
11120 | * if it is not alligned, add one to current migration unit value | |
11121 | * this can happend on array reshape finish only | |
11122 | */ | |
11123 | if (info->reshape_progress % blocks_per_unit) | |
11124 | curr_migr_unit++; | |
687629c2 | 11125 | |
9f421827 | 11126 | set_current_migr_unit(super->migr_rec, curr_migr_unit); |
687629c2 | 11127 | super->migr_rec->rec_status = __cpu_to_le32(state); |
9f421827 PB |
11128 | set_migr_dest_1st_member_lba(super->migr_rec, |
11129 | super->migr_rec->dest_depth_per_unit * curr_migr_unit); | |
11130 | ||
687629c2 | 11131 | if (write_imsm_migr_rec(st) < 0) { |
7a862a02 | 11132 | dprintf("imsm: Cannot write migration record outside backup area\n"); |
687629c2 AK |
11133 | return 1; |
11134 | } | |
11135 | ||
11136 | return 0; | |
11137 | } | |
11138 | ||
276d77db AK |
11139 | /******************************************************************************* |
11140 | * Function: recover_backup_imsm | |
11141 | * Description: Function recovers critical data from the Migration Copy Area | |
11142 | * while assembling an array. | |
11143 | * Parameters: | |
11144 | * super : imsm internal array info | |
11145 | * info : general array info | |
11146 | * Returns: | |
11147 | * 0 : success (or there is no data to recover) | |
11148 | * 1 : fail | |
11149 | ******************************************************************************/ | |
11150 | int recover_backup_imsm(struct supertype *st, struct mdinfo *info) | |
11151 | { | |
11152 | struct intel_super *super = st->sb; | |
11153 | struct migr_record *migr_rec = super->migr_rec; | |
594dc1b8 | 11154 | struct imsm_map *map_dest; |
276d77db AK |
11155 | struct intel_dev *id = NULL; |
11156 | unsigned long long read_offset; | |
11157 | unsigned long long write_offset; | |
11158 | unsigned unit_len; | |
2f86fda3 | 11159 | int new_disks, err; |
276d77db AK |
11160 | char *buf = NULL; |
11161 | int retval = 1; | |
f36a9ecd | 11162 | unsigned int sector_size = super->sector_size; |
4036e7ee MT |
11163 | unsigned long long curr_migr_unit = current_migr_unit(migr_rec); |
11164 | unsigned long long num_migr_units = get_num_migr_units(migr_rec); | |
90fd7001 | 11165 | char buffer[SYSFS_MAX_BUF_SIZE]; |
6c3560c0 | 11166 | int skipped_disks = 0; |
2f86fda3 | 11167 | struct dl *dl_disk; |
276d77db | 11168 | |
90fd7001 | 11169 | err = sysfs_get_str(info, NULL, "array_state", (char *)buffer, sizeof(buffer)); |
276d77db AK |
11170 | if (err < 1) |
11171 | return 1; | |
11172 | ||
11173 | /* recover data only during assemblation */ | |
11174 | if (strncmp(buffer, "inactive", 8) != 0) | |
11175 | return 0; | |
11176 | /* no data to recover */ | |
11177 | if (__le32_to_cpu(migr_rec->rec_status) == UNIT_SRC_NORMAL) | |
11178 | return 0; | |
11179 | if (curr_migr_unit >= num_migr_units) | |
11180 | return 1; | |
11181 | ||
11182 | /* find device during reshape */ | |
11183 | for (id = super->devlist; id; id = id->next) | |
11184 | if (is_gen_migration(id->dev)) | |
11185 | break; | |
11186 | if (id == NULL) | |
11187 | return 1; | |
11188 | ||
238c0a71 | 11189 | map_dest = get_imsm_map(id->dev, MAP_0); |
276d77db AK |
11190 | new_disks = map_dest->num_members; |
11191 | ||
9f421827 | 11192 | read_offset = migr_chkp_area_pba(migr_rec) * 512; |
276d77db | 11193 | |
9f421827 | 11194 | write_offset = (migr_dest_1st_member_lba(migr_rec) + |
5551b113 | 11195 | pba_of_lba0(map_dest)) * 512; |
276d77db AK |
11196 | |
11197 | unit_len = __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512; | |
f36a9ecd | 11198 | if (posix_memalign((void **)&buf, sector_size, unit_len) != 0) |
276d77db | 11199 | goto abort; |
276d77db | 11200 | |
2f86fda3 MT |
11201 | for (dl_disk = super->disks; dl_disk; dl_disk = dl_disk->next) { |
11202 | if (dl_disk->index < 0) | |
11203 | continue; | |
276d77db | 11204 | |
4389ce73 | 11205 | if (!is_fd_valid(dl_disk->fd)) { |
6c3560c0 AK |
11206 | skipped_disks++; |
11207 | continue; | |
11208 | } | |
2f86fda3 | 11209 | if (lseek64(dl_disk->fd, read_offset, SEEK_SET) < 0) { |
e7b84f9d N |
11210 | pr_err("Cannot seek to block: %s\n", |
11211 | strerror(errno)); | |
137debce AK |
11212 | skipped_disks++; |
11213 | continue; | |
276d77db | 11214 | } |
83b3de77 | 11215 | if (read(dl_disk->fd, buf, unit_len) != (ssize_t)unit_len) { |
e7b84f9d N |
11216 | pr_err("Cannot read copy area block: %s\n", |
11217 | strerror(errno)); | |
137debce AK |
11218 | skipped_disks++; |
11219 | continue; | |
276d77db | 11220 | } |
2f86fda3 | 11221 | if (lseek64(dl_disk->fd, write_offset, SEEK_SET) < 0) { |
e7b84f9d N |
11222 | pr_err("Cannot seek to block: %s\n", |
11223 | strerror(errno)); | |
137debce AK |
11224 | skipped_disks++; |
11225 | continue; | |
276d77db | 11226 | } |
83b3de77 | 11227 | if (write(dl_disk->fd, buf, unit_len) != (ssize_t)unit_len) { |
e7b84f9d N |
11228 | pr_err("Cannot restore block: %s\n", |
11229 | strerror(errno)); | |
137debce AK |
11230 | skipped_disks++; |
11231 | continue; | |
276d77db AK |
11232 | } |
11233 | } | |
11234 | ||
137debce AK |
11235 | if (skipped_disks > imsm_get_allowed_degradation(info->new_level, |
11236 | new_disks, | |
11237 | super, | |
11238 | id->dev)) { | |
7a862a02 | 11239 | pr_err("Cannot restore data from backup. Too many failed disks\n"); |
6c3560c0 AK |
11240 | goto abort; |
11241 | } | |
11242 | ||
befb629b AK |
11243 | if (save_checkpoint_imsm(st, info, UNIT_SRC_NORMAL)) { |
11244 | /* ignore error == 2, this can mean end of reshape here | |
11245 | */ | |
7a862a02 | 11246 | dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL) during restart\n"); |
befb629b | 11247 | } else |
276d77db | 11248 | retval = 0; |
276d77db AK |
11249 | |
11250 | abort: | |
276d77db AK |
11251 | free(buf); |
11252 | return retval; | |
11253 | } | |
11254 | ||
e21aea08 MT |
11255 | /** |
11256 | * test_and_add_drive_controller_policy_imsm() - add disk controller to policies list. | |
11257 | * @type: Policy type to search on list. | |
11258 | * @pols: List of currently recorded policies. | |
11259 | * @disk_fd: File descriptor of the device to check. | |
11260 | * @hba: The hba disk is attached, could be NULL if verification is disabled. | |
11261 | * @verbose: verbose flag. | |
11262 | * | |
11263 | * IMSM cares about drive physical placement. If @hba is not set, it adds unknown policy. | |
11264 | * If there is no controller policy on pols we are free to add first one. If there is a policy then, | |
11265 | * new must be the same - no controller mixing allowed. | |
11266 | */ | |
11267 | static mdadm_status_t | |
11268 | test_and_add_drive_controller_policy_imsm(const char * const type, dev_policy_t **pols, int disk_fd, | |
11269 | struct sys_dev *hba, const int verbose) | |
11270 | { | |
11271 | const char *controller_policy = get_sys_dev_type(SYS_DEV_UNKNOWN); | |
11272 | struct dev_policy *pol = pol_find(*pols, (char *)type); | |
11273 | char devname[MAX_RAID_SERIAL_LEN]; | |
11274 | ||
11275 | if (hba) | |
11276 | controller_policy = get_sys_dev_type(hba->type); | |
11277 | ||
11278 | if (!pol) { | |
11279 | pol_add(pols, (char *)type, (char *)controller_policy, "imsm"); | |
11280 | return MDADM_STATUS_SUCCESS; | |
2cda7640 | 11281 | } |
e21aea08 MT |
11282 | |
11283 | if (strcmp(pol->value, controller_policy) == 0) | |
11284 | return MDADM_STATUS_SUCCESS; | |
11285 | ||
11286 | fd2devname(disk_fd, devname); | |
11287 | pr_vrb("Intel(R) raid controller \"%s\" found for %s, but \"%s\" was detected earlier\n", | |
11288 | controller_policy, devname, pol->value); | |
11289 | pr_vrb("Disks under different controllers cannot be used, aborting\n"); | |
11290 | ||
11291 | return MDADM_STATUS_ERROR; | |
11292 | } | |
11293 | ||
acb8f13b BK |
11294 | /** |
11295 | * test_and_add_drive_encryption_policy_imsm() - add disk encryption to policies list. | |
11296 | * @type: policy type to search in the list. | |
11297 | * @pols: list of currently recorded policies. | |
11298 | * @disk_fd: file descriptor of the device to check. | |
11299 | * @hba: The hba to which the drive is attached, could be NULL if verification is disabled. | |
11300 | * @verbose: verbose flag. | |
11301 | * | |
11302 | * IMSM cares about drive encryption state. It is not allowed to mix disks with different | |
11303 | * encryption state within one md device. | |
11304 | * If there is no encryption policy on pols we are free to add first one. | |
11305 | * If there is a policy then, new must be the same. | |
11306 | */ | |
11307 | static mdadm_status_t | |
11308 | test_and_add_drive_encryption_policy_imsm(const char * const type, dev_policy_t **pols, int disk_fd, | |
11309 | struct sys_dev *hba, const int verbose) | |
11310 | { | |
11311 | struct dev_policy *expected_policy = pol_find(*pols, (char *)type); | |
11312 | struct encryption_information information = {0}; | |
11313 | char *encryption_state = "Unknown"; | |
11314 | int status = MDADM_STATUS_SUCCESS; | |
11315 | bool encryption_checked = true; | |
11316 | char devname[PATH_MAX]; | |
11317 | ||
11318 | if (!hba) | |
11319 | goto check_policy; | |
11320 | ||
11321 | switch (hba->type) { | |
11322 | case SYS_DEV_NVME: | |
11323 | case SYS_DEV_VMD: | |
11324 | status = get_nvme_opal_encryption_information(disk_fd, &information, verbose); | |
11325 | break; | |
11326 | case SYS_DEV_SATA: | |
11327 | case SYS_DEV_SATA_VMD: | |
11328 | status = get_ata_encryption_information(disk_fd, &information, verbose); | |
11329 | break; | |
11330 | default: | |
11331 | encryption_checked = false; | |
11332 | } | |
11333 | ||
11334 | if (status) { | |
11335 | fd2devname(disk_fd, devname); | |
11336 | pr_vrb("Failed to read encryption information of device %s\n", devname); | |
11337 | return MDADM_STATUS_ERROR; | |
11338 | } | |
11339 | ||
11340 | if (encryption_checked) { | |
11341 | if (information.status == ENC_STATUS_LOCKED) { | |
11342 | fd2devname(disk_fd, devname); | |
11343 | pr_vrb("Device %s is in Locked state, cannot use. Aborting.\n", devname); | |
11344 | return MDADM_STATUS_ERROR; | |
11345 | } | |
11346 | encryption_state = (char *)get_encryption_status_string(information.status); | |
11347 | } | |
11348 | ||
11349 | check_policy: | |
11350 | if (expected_policy) { | |
11351 | if (strcmp(expected_policy->value, encryption_state) == 0) | |
11352 | return MDADM_STATUS_SUCCESS; | |
11353 | ||
11354 | fd2devname(disk_fd, devname); | |
11355 | pr_vrb("Encryption status \"%s\" detected for disk %s, but \"%s\" status was detected eariler.\n", | |
11356 | encryption_state, devname, expected_policy->value); | |
11357 | pr_vrb("Disks with different encryption status cannot be used.\n"); | |
11358 | return MDADM_STATUS_ERROR; | |
11359 | } | |
11360 | ||
11361 | pol_add(pols, (char *)type, encryption_state, "imsm"); | |
11362 | ||
11363 | return MDADM_STATUS_SUCCESS; | |
11364 | } | |
11365 | ||
e21aea08 MT |
11366 | struct imsm_drive_policy { |
11367 | char *type; | |
11368 | mdadm_status_t (*test_and_add_drive_policy)(const char * const type, | |
11369 | struct dev_policy **pols, int disk_fd, | |
11370 | struct sys_dev *hba, const int verbose); | |
11371 | }; | |
11372 | ||
11373 | struct imsm_drive_policy imsm_policies[] = { | |
11374 | {"controller", test_and_add_drive_controller_policy_imsm}, | |
acb8f13b | 11375 | {"encryption", test_and_add_drive_encryption_policy_imsm} |
e21aea08 MT |
11376 | }; |
11377 | ||
11378 | mdadm_status_t test_and_add_drive_policies_imsm(struct dev_policy **pols, int disk_fd, | |
11379 | const int verbose) | |
11380 | { | |
11381 | struct imsm_drive_policy *imsm_pol; | |
11382 | struct sys_dev *hba = NULL; | |
11383 | char path[PATH_MAX]; | |
11384 | mdadm_status_t ret; | |
11385 | unsigned int i; | |
11386 | ||
11387 | /* If imsm platform verification is disabled, do not search for hba. */ | |
11388 | if (check_no_platform() != 1) { | |
11389 | if (!diskfd_to_devpath(disk_fd, 1, path)) { | |
11390 | pr_vrb("IMSM: Failed to retrieve device path by file descriptor.\n"); | |
11391 | return MDADM_STATUS_ERROR; | |
11392 | } | |
11393 | ||
11394 | hba = find_disk_attached_hba(disk_fd, path); | |
11395 | if (!hba) { | |
11396 | pr_vrb("IMSM: Failed to find hba for %s\n", path); | |
11397 | return MDADM_STATUS_ERROR; | |
11398 | } | |
11399 | } | |
11400 | ||
11401 | for (i = 0; i < ARRAY_SIZE(imsm_policies); i++) { | |
11402 | imsm_pol = &imsm_policies[i]; | |
11403 | ||
11404 | ret = imsm_pol->test_and_add_drive_policy(imsm_pol->type, pols, disk_fd, hba, | |
11405 | verbose); | |
11406 | if (ret != MDADM_STATUS_SUCCESS) | |
11407 | /* Inherit error code */ | |
11408 | return ret; | |
11409 | } | |
11410 | ||
11411 | return MDADM_STATUS_SUCCESS; | |
2cda7640 ML |
11412 | } |
11413 | ||
f6562011 MT |
11414 | /** |
11415 | * get_spare_criteria_imsm() - set spare criteria. | |
11416 | * @st: supertype. | |
11417 | * @mddev_path: path to md device devnode, it must be container. | |
11418 | * @c: spare_criteria struct to fill, not NULL. | |
11419 | * | |
11420 | * If superblock is not loaded, use mddev_path to load_container. It must be given in this case. | |
11421 | * Filles size and sector size accordingly to superblock. | |
11422 | */ | |
11423 | mdadm_status_t get_spare_criteria_imsm(struct supertype *st, char *mddev_path, | |
11424 | struct spare_criteria *c) | |
11425 | { | |
11426 | mdadm_status_t ret = MDADM_STATUS_ERROR; | |
11427 | bool free_superblock = false; | |
11428 | unsigned long long size = 0; | |
11429 | struct intel_super *super; | |
11430 | struct extent *e; | |
11431 | struct dl *dl; | |
11432 | int i; | |
11433 | ||
11434 | /* If no superblock and no mddev_path, we cannot load superblock. */ | |
11435 | assert(st->sb || mddev_path); | |
11436 | ||
11437 | if (mddev_path) { | |
11438 | int fd = open(mddev_path, O_RDONLY); | |
e21aea08 | 11439 | mdadm_status_t rv; |
f6562011 MT |
11440 | |
11441 | if (!is_fd_valid(fd)) | |
11442 | return MDADM_STATUS_ERROR; | |
11443 | ||
11444 | if (!st->sb) { | |
11445 | if (load_container_imsm(st, fd, st->devnm)) { | |
11446 | close(fd); | |
11447 | return MDADM_STATUS_ERROR; | |
11448 | } | |
11449 | free_superblock = true; | |
11450 | } | |
e21aea08 MT |
11451 | |
11452 | rv = mddev_test_and_add_drive_policies(st, &c->pols, fd, 0); | |
f6562011 | 11453 | close(fd); |
e21aea08 MT |
11454 | |
11455 | if (rv != MDADM_STATUS_SUCCESS) | |
11456 | goto out; | |
f6562011 MT |
11457 | } |
11458 | ||
11459 | super = st->sb; | |
11460 | ||
11461 | /* find first active disk in array */ | |
11462 | dl = super->disks; | |
11463 | while (dl && (is_failed(&dl->disk) || dl->index == -1)) | |
11464 | dl = dl->next; | |
11465 | ||
11466 | if (!dl) | |
11467 | goto out; | |
11468 | ||
11469 | /* find last lba used by subarrays */ | |
11470 | e = get_extents(super, dl, 0); | |
11471 | if (!e) | |
11472 | goto out; | |
11473 | ||
11474 | for (i = 0; e[i].size; i++) | |
11475 | continue; | |
11476 | if (i > 0) | |
11477 | size = e[i - 1].start + e[i - 1].size; | |
11478 | free(e); | |
11479 | ||
11480 | /* add the amount of space needed for metadata */ | |
11481 | size += imsm_min_reserved_sectors(super); | |
11482 | ||
11483 | c->min_size = size * 512; | |
11484 | c->sector_size = super->sector_size; | |
11485 | c->criteria_set = true; | |
11486 | ret = MDADM_STATUS_SUCCESS; | |
11487 | ||
11488 | out: | |
11489 | if (free_superblock) | |
11490 | free_super_imsm(st); | |
11491 | ||
11492 | if (ret != MDADM_STATUS_SUCCESS) | |
11493 | c->criteria_set = false; | |
11494 | ||
11495 | return ret; | |
11496 | } | |
11497 | ||
4dd2df09 | 11498 | static char *imsm_find_array_devnm_by_subdev(int subdev, char *container) |
78b10e66 | 11499 | { |
4dd2df09 | 11500 | static char devnm[32]; |
78b10e66 N |
11501 | char subdev_name[20]; |
11502 | struct mdstat_ent *mdstat; | |
11503 | ||
11504 | sprintf(subdev_name, "%d", subdev); | |
11505 | mdstat = mdstat_by_subdev(subdev_name, container); | |
11506 | if (!mdstat) | |
4dd2df09 | 11507 | return NULL; |
78b10e66 | 11508 | |
4dd2df09 | 11509 | strcpy(devnm, mdstat->devnm); |
78b10e66 | 11510 | free_mdstat(mdstat); |
4dd2df09 | 11511 | return devnm; |
78b10e66 N |
11512 | } |
11513 | ||
11514 | static int imsm_reshape_is_allowed_on_container(struct supertype *st, | |
11515 | struct geo_params *geo, | |
fbf3d202 AK |
11516 | int *old_raid_disks, |
11517 | int direction) | |
78b10e66 | 11518 | { |
694575e7 KW |
11519 | /* currently we only support increasing the number of devices |
11520 | * for a container. This increases the number of device for each | |
11521 | * member array. They must all be RAID0 or RAID5. | |
11522 | */ | |
78b10e66 N |
11523 | int ret_val = 0; |
11524 | struct mdinfo *info, *member; | |
11525 | int devices_that_can_grow = 0; | |
11526 | ||
7a862a02 | 11527 | dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): st->devnm = (%s)\n", st->devnm); |
78b10e66 | 11528 | |
d04f65f4 | 11529 | if (geo->size > 0 || |
78b10e66 N |
11530 | geo->level != UnSet || |
11531 | geo->layout != UnSet || | |
11532 | geo->chunksize != 0 || | |
11533 | geo->raid_disks == UnSet) { | |
7a862a02 | 11534 | dprintf("imsm: Container operation is allowed for raid disks number change only.\n"); |
78b10e66 N |
11535 | return ret_val; |
11536 | } | |
11537 | ||
fbf3d202 | 11538 | if (direction == ROLLBACK_METADATA_CHANGES) { |
7a862a02 | 11539 | dprintf("imsm: Metadata changes rollback is not supported for container operation.\n"); |
fbf3d202 AK |
11540 | return ret_val; |
11541 | } | |
11542 | ||
78b10e66 N |
11543 | info = container_content_imsm(st, NULL); |
11544 | for (member = info; member; member = member->next) { | |
4dd2df09 | 11545 | char *result; |
78b10e66 N |
11546 | |
11547 | dprintf("imsm: checking device_num: %i\n", | |
11548 | member->container_member); | |
11549 | ||
d7d205bd | 11550 | if (geo->raid_disks <= member->array.raid_disks) { |
78b10e66 N |
11551 | /* we work on container for Online Capacity Expansion |
11552 | * only so raid_disks has to grow | |
11553 | */ | |
7a862a02 | 11554 | dprintf("imsm: for container operation raid disks increase is required\n"); |
78b10e66 N |
11555 | break; |
11556 | } | |
11557 | ||
089f9d79 | 11558 | if (info->array.level != 0 && info->array.level != 5) { |
78b10e66 N |
11559 | /* we cannot use this container with other raid level |
11560 | */ | |
7a862a02 | 11561 | dprintf("imsm: for container operation wrong raid level (%i) detected\n", |
78b10e66 N |
11562 | info->array.level); |
11563 | break; | |
11564 | } else { | |
11565 | /* check for platform support | |
11566 | * for this raid level configuration | |
11567 | */ | |
11568 | struct intel_super *super = st->sb; | |
11569 | if (!is_raid_level_supported(super->orom, | |
11570 | member->array.level, | |
11571 | geo->raid_disks)) { | |
7a862a02 | 11572 | dprintf("platform does not support raid%d with %d disk%s\n", |
78b10e66 N |
11573 | info->array.level, |
11574 | geo->raid_disks, | |
11575 | geo->raid_disks > 1 ? "s" : ""); | |
11576 | break; | |
11577 | } | |
2a4a08e7 AK |
11578 | /* check if component size is aligned to chunk size |
11579 | */ | |
11580 | if (info->component_size % | |
11581 | (info->array.chunk_size/512)) { | |
7a862a02 | 11582 | dprintf("Component size is not aligned to chunk size\n"); |
2a4a08e7 AK |
11583 | break; |
11584 | } | |
78b10e66 N |
11585 | } |
11586 | ||
11587 | if (*old_raid_disks && | |
11588 | info->array.raid_disks != *old_raid_disks) | |
11589 | break; | |
11590 | *old_raid_disks = info->array.raid_disks; | |
11591 | ||
11592 | /* All raid5 and raid0 volumes in container | |
11593 | * have to be ready for Online Capacity Expansion | |
11594 | * so they need to be assembled. We have already | |
11595 | * checked that no recovery etc is happening. | |
11596 | */ | |
4dd2df09 N |
11597 | result = imsm_find_array_devnm_by_subdev(member->container_member, |
11598 | st->container_devnm); | |
11599 | if (result == NULL) { | |
78b10e66 N |
11600 | dprintf("imsm: cannot find array\n"); |
11601 | break; | |
11602 | } | |
11603 | devices_that_can_grow++; | |
11604 | } | |
11605 | sysfs_free(info); | |
11606 | if (!member && devices_that_can_grow) | |
11607 | ret_val = 1; | |
11608 | ||
11609 | if (ret_val) | |
1ade5cc1 | 11610 | dprintf("Container operation allowed\n"); |
78b10e66 | 11611 | else |
1ade5cc1 | 11612 | dprintf("Error: %i\n", ret_val); |
78b10e66 N |
11613 | |
11614 | return ret_val; | |
11615 | } | |
11616 | ||
11617 | /* Function: get_spares_for_grow | |
11618 | * Description: Allocates memory and creates list of spare devices | |
1011e834 | 11619 | * avaliable in container. Checks if spare drive size is acceptable. |
78b10e66 N |
11620 | * Parameters: Pointer to the supertype structure |
11621 | * Returns: Pointer to the list of spare devices (mdinfo structure) on success, | |
1011e834 | 11622 | * NULL if fail |
78b10e66 N |
11623 | */ |
11624 | static struct mdinfo *get_spares_for_grow(struct supertype *st) | |
11625 | { | |
ae996e81 MT |
11626 | struct spare_criteria sc = {0}; |
11627 | struct mdinfo *spares; | |
fbfdcb06 | 11628 | |
f6562011 | 11629 | get_spare_criteria_imsm(st, NULL, &sc); |
ae996e81 MT |
11630 | spares = container_choose_spares(st, &sc, NULL, NULL, NULL, 0); |
11631 | ||
11632 | dev_policy_free(sc.pols); | |
11633 | ||
11634 | return spares; | |
78b10e66 N |
11635 | } |
11636 | ||
11637 | /****************************************************************************** | |
11638 | * function: imsm_create_metadata_update_for_reshape | |
11639 | * Function creates update for whole IMSM container. | |
11640 | * | |
11641 | ******************************************************************************/ | |
11642 | static int imsm_create_metadata_update_for_reshape( | |
11643 | struct supertype *st, | |
11644 | struct geo_params *geo, | |
11645 | int old_raid_disks, | |
11646 | struct imsm_update_reshape **updatep) | |
11647 | { | |
11648 | struct intel_super *super = st->sb; | |
11649 | struct imsm_super *mpb = super->anchor; | |
594dc1b8 JS |
11650 | int update_memory_size; |
11651 | struct imsm_update_reshape *u; | |
11652 | struct mdinfo *spares; | |
78b10e66 | 11653 | int i; |
594dc1b8 | 11654 | int delta_disks; |
bbd24d86 | 11655 | struct mdinfo *dev; |
78b10e66 | 11656 | |
1ade5cc1 | 11657 | dprintf("(enter) raid_disks = %i\n", geo->raid_disks); |
78b10e66 N |
11658 | |
11659 | delta_disks = geo->raid_disks - old_raid_disks; | |
11660 | ||
11661 | /* size of all update data without anchor */ | |
11662 | update_memory_size = sizeof(struct imsm_update_reshape); | |
11663 | ||
11664 | /* now add space for spare disks that we need to add. */ | |
11665 | update_memory_size += sizeof(u->new_disks[0]) * (delta_disks - 1); | |
11666 | ||
503975b9 | 11667 | u = xcalloc(1, update_memory_size); |
78b10e66 N |
11668 | u->type = update_reshape_container_disks; |
11669 | u->old_raid_disks = old_raid_disks; | |
11670 | u->new_raid_disks = geo->raid_disks; | |
11671 | ||
11672 | /* now get spare disks list | |
11673 | */ | |
11674 | spares = get_spares_for_grow(st); | |
11675 | ||
d7be7d87 | 11676 | if (spares == NULL || delta_disks > spares->array.spare_disks) { |
7a862a02 | 11677 | pr_err("imsm: ERROR: Cannot get spare devices for %s.\n", geo->dev_name); |
e4c72d1d | 11678 | i = -1; |
78b10e66 N |
11679 | goto abort; |
11680 | } | |
11681 | ||
11682 | /* we have got spares | |
11683 | * update disk list in imsm_disk list table in anchor | |
11684 | */ | |
11685 | dprintf("imsm: %i spares are available.\n\n", | |
11686 | spares->array.spare_disks); | |
11687 | ||
bbd24d86 | 11688 | dev = spares->devs; |
78b10e66 | 11689 | for (i = 0; i < delta_disks; i++) { |
78b10e66 N |
11690 | struct dl *dl; |
11691 | ||
bbd24d86 AK |
11692 | if (dev == NULL) |
11693 | break; | |
78b10e66 N |
11694 | u->new_disks[i] = makedev(dev->disk.major, |
11695 | dev->disk.minor); | |
11696 | dl = get_disk_super(super, dev->disk.major, dev->disk.minor); | |
ee4beede AK |
11697 | dl->index = mpb->num_disks; |
11698 | mpb->num_disks++; | |
bbd24d86 | 11699 | dev = dev->next; |
78b10e66 | 11700 | } |
78b10e66 N |
11701 | |
11702 | abort: | |
11703 | /* free spares | |
11704 | */ | |
11705 | sysfs_free(spares); | |
11706 | ||
d677e0b8 | 11707 | dprintf("imsm: reshape update preparation :"); |
78b10e66 | 11708 | if (i == delta_disks) { |
1ade5cc1 | 11709 | dprintf_cont(" OK\n"); |
78b10e66 N |
11710 | *updatep = u; |
11711 | return update_memory_size; | |
11712 | } | |
11713 | free(u); | |
1ade5cc1 | 11714 | dprintf_cont(" Error\n"); |
78b10e66 N |
11715 | |
11716 | return 0; | |
11717 | } | |
11718 | ||
f3871fdc AK |
11719 | /****************************************************************************** |
11720 | * function: imsm_create_metadata_update_for_size_change() | |
11721 | * Creates update for IMSM array for array size change. | |
11722 | * | |
11723 | ******************************************************************************/ | |
11724 | static int imsm_create_metadata_update_for_size_change( | |
11725 | struct supertype *st, | |
11726 | struct geo_params *geo, | |
11727 | struct imsm_update_size_change **updatep) | |
11728 | { | |
11729 | struct intel_super *super = st->sb; | |
594dc1b8 JS |
11730 | int update_memory_size; |
11731 | struct imsm_update_size_change *u; | |
f3871fdc | 11732 | |
1ade5cc1 | 11733 | dprintf("(enter) New size = %llu\n", geo->size); |
f3871fdc AK |
11734 | |
11735 | /* size of all update data without anchor */ | |
11736 | update_memory_size = sizeof(struct imsm_update_size_change); | |
11737 | ||
503975b9 | 11738 | u = xcalloc(1, update_memory_size); |
f3871fdc AK |
11739 | u->type = update_size_change; |
11740 | u->subdev = super->current_vol; | |
11741 | u->new_size = geo->size; | |
11742 | ||
11743 | dprintf("imsm: reshape update preparation : OK\n"); | |
11744 | *updatep = u; | |
11745 | ||
11746 | return update_memory_size; | |
11747 | } | |
11748 | ||
48c5303a PC |
11749 | /****************************************************************************** |
11750 | * function: imsm_create_metadata_update_for_migration() | |
11751 | * Creates update for IMSM array. | |
11752 | * | |
11753 | ******************************************************************************/ | |
11754 | static int imsm_create_metadata_update_for_migration( | |
11755 | struct supertype *st, | |
11756 | struct geo_params *geo, | |
11757 | struct imsm_update_reshape_migration **updatep) | |
11758 | { | |
11759 | struct intel_super *super = st->sb; | |
594dc1b8 | 11760 | int update_memory_size; |
756a15f3 | 11761 | int current_chunk_size; |
594dc1b8 | 11762 | struct imsm_update_reshape_migration *u; |
756a15f3 MG |
11763 | struct imsm_dev *dev = get_imsm_dev(super, super->current_vol); |
11764 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
48c5303a PC |
11765 | int previous_level = -1; |
11766 | ||
1ade5cc1 | 11767 | dprintf("(enter) New Level = %i\n", geo->level); |
48c5303a PC |
11768 | |
11769 | /* size of all update data without anchor */ | |
11770 | update_memory_size = sizeof(struct imsm_update_reshape_migration); | |
11771 | ||
503975b9 | 11772 | u = xcalloc(1, update_memory_size); |
48c5303a PC |
11773 | u->type = update_reshape_migration; |
11774 | u->subdev = super->current_vol; | |
11775 | u->new_level = geo->level; | |
11776 | u->new_layout = geo->layout; | |
11777 | u->new_raid_disks = u->old_raid_disks = geo->raid_disks; | |
11778 | u->new_disks[0] = -1; | |
4bba0439 | 11779 | u->new_chunksize = -1; |
48c5303a | 11780 | |
756a15f3 | 11781 | current_chunk_size = __le16_to_cpu(map->blocks_per_strip) / 2; |
48c5303a | 11782 | |
756a15f3 MG |
11783 | if (geo->chunksize != current_chunk_size) { |
11784 | u->new_chunksize = geo->chunksize / 1024; | |
11785 | dprintf("imsm: chunk size change from %i to %i\n", | |
11786 | current_chunk_size, u->new_chunksize); | |
48c5303a | 11787 | } |
756a15f3 MG |
11788 | previous_level = map->raid_level; |
11789 | ||
089f9d79 | 11790 | if (geo->level == 5 && previous_level == 0) { |
48c5303a PC |
11791 | struct mdinfo *spares = NULL; |
11792 | ||
11793 | u->new_raid_disks++; | |
11794 | spares = get_spares_for_grow(st); | |
089f9d79 | 11795 | if (spares == NULL || spares->array.spare_disks < 1) { |
48c5303a PC |
11796 | free(u); |
11797 | sysfs_free(spares); | |
11798 | update_memory_size = 0; | |
565cc99e | 11799 | pr_err("cannot get spare device for requested migration\n"); |
48c5303a PC |
11800 | return 0; |
11801 | } | |
11802 | sysfs_free(spares); | |
11803 | } | |
11804 | dprintf("imsm: reshape update preparation : OK\n"); | |
11805 | *updatep = u; | |
11806 | ||
11807 | return update_memory_size; | |
11808 | } | |
11809 | ||
8dd70bce AK |
11810 | static void imsm_update_metadata_locally(struct supertype *st, |
11811 | void *buf, int len) | |
11812 | { | |
11813 | struct metadata_update mu; | |
11814 | ||
11815 | mu.buf = buf; | |
11816 | mu.len = len; | |
11817 | mu.space = NULL; | |
11818 | mu.space_list = NULL; | |
11819 | mu.next = NULL; | |
5fe6f031 N |
11820 | if (imsm_prepare_update(st, &mu)) |
11821 | imsm_process_update(st, &mu); | |
8dd70bce AK |
11822 | |
11823 | while (mu.space_list) { | |
11824 | void **space = mu.space_list; | |
11825 | mu.space_list = *space; | |
11826 | free(space); | |
11827 | } | |
11828 | } | |
78b10e66 | 11829 | |
cbaa7904 MT |
11830 | /** |
11831 | * imsm_analyze_expand() - check expand properties and calculate new size. | |
11832 | * @st: imsm supertype. | |
11833 | * @geo: new geometry params. | |
11834 | * @array: array info. | |
11835 | * @direction: reshape direction. | |
11836 | * | |
11837 | * Obtain free space after the &array and verify if expand to requested size is | |
11838 | * possible. If geo->size is set to %MAX_SIZE, assume that max free size is | |
11839 | * requested. | |
11840 | * | |
11841 | * Return: | |
11842 | * On success %IMSM_STATUS_OK is returned, geo->size and geo->raid_disks are | |
11843 | * updated. | |
11844 | * On error, %IMSM_STATUS_ERROR is returned. | |
11845 | */ | |
11846 | static imsm_status_t imsm_analyze_expand(struct supertype *st, | |
11847 | struct geo_params *geo, | |
11848 | struct mdinfo *array, | |
11849 | int direction) | |
11850 | { | |
11851 | struct intel_super *super = st->sb; | |
11852 | struct imsm_dev *dev = get_imsm_dev(super, super->current_vol); | |
11853 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
11854 | int data_disks = imsm_num_data_members(map); | |
11855 | ||
11856 | unsigned long long current_size; | |
11857 | unsigned long long free_size; | |
11858 | unsigned long long new_size; | |
11859 | unsigned long long max_size; | |
11860 | ||
11861 | const int chunk_kib = geo->chunksize / 1024; | |
11862 | imsm_status_t rv; | |
11863 | ||
11864 | if (direction == ROLLBACK_METADATA_CHANGES) { | |
11865 | /** | |
11866 | * Accept size for rollback only. | |
11867 | */ | |
11868 | new_size = geo->size * 2; | |
11869 | goto success; | |
11870 | } | |
11871 | ||
cbaa7904 MT |
11872 | if (data_disks == 0) { |
11873 | pr_err("imsm: Cannot retrieve data disks.\n"); | |
11874 | return IMSM_STATUS_ERROR; | |
11875 | } | |
11876 | current_size = array->custom_array_size / data_disks; | |
11877 | ||
aa19fdd4 | 11878 | rv = imsm_get_free_size(super, dev->vol.map->num_members, 0, chunk_kib, &free_size, true); |
cbaa7904 MT |
11879 | if (rv != IMSM_STATUS_OK) { |
11880 | pr_err("imsm: Cannot find free space for expand.\n"); | |
11881 | return IMSM_STATUS_ERROR; | |
11882 | } | |
11883 | max_size = round_member_size_to_mb(free_size + current_size); | |
11884 | ||
11885 | if (geo->size == MAX_SIZE) | |
11886 | new_size = max_size; | |
11887 | else | |
11888 | new_size = round_member_size_to_mb(geo->size * 2); | |
11889 | ||
11890 | if (new_size == 0) { | |
11891 | pr_err("imsm: Rounded requested size is 0.\n"); | |
11892 | return IMSM_STATUS_ERROR; | |
11893 | } | |
11894 | ||
11895 | if (new_size > max_size) { | |
11896 | pr_err("imsm: Rounded requested size (%llu) is larger than free space available (%llu).\n", | |
11897 | new_size, max_size); | |
11898 | return IMSM_STATUS_ERROR; | |
11899 | } | |
11900 | ||
11901 | if (new_size == current_size) { | |
11902 | pr_err("imsm: Rounded requested size (%llu) is same as current size (%llu).\n", | |
11903 | new_size, current_size); | |
11904 | return IMSM_STATUS_ERROR; | |
11905 | } | |
11906 | ||
11907 | if (new_size < current_size) { | |
11908 | pr_err("imsm: Size reduction is not supported, rounded requested size (%llu) is smaller than current (%llu).\n", | |
11909 | new_size, current_size); | |
11910 | return IMSM_STATUS_ERROR; | |
11911 | } | |
11912 | ||
11913 | success: | |
11914 | dprintf("imsm: New size per member is %llu.\n", new_size); | |
11915 | geo->size = data_disks * new_size; | |
11916 | geo->raid_disks = dev->vol.map->num_members; | |
11917 | return IMSM_STATUS_OK; | |
11918 | } | |
11919 | ||
471bceb6 | 11920 | /*************************************************************************** |
694575e7 | 11921 | * Function: imsm_analyze_change |
471bceb6 | 11922 | * Description: Function analyze change for single volume |
1011e834 | 11923 | * and validate if transition is supported |
fbf3d202 AK |
11924 | * Parameters: Geometry parameters, supertype structure, |
11925 | * metadata change direction (apply/rollback) | |
694575e7 | 11926 | * Returns: Operation type code on success, -1 if fail |
471bceb6 KW |
11927 | ****************************************************************************/ |
11928 | enum imsm_reshape_type imsm_analyze_change(struct supertype *st, | |
fbf3d202 AK |
11929 | struct geo_params *geo, |
11930 | int direction) | |
694575e7 | 11931 | { |
471bceb6 KW |
11932 | struct mdinfo info; |
11933 | int change = -1; | |
11934 | int check_devs = 0; | |
c21e737b | 11935 | int chunk; |
67a2db32 AK |
11936 | /* number of added/removed disks in operation result */ |
11937 | int devNumChange = 0; | |
11938 | /* imsm compatible layout value for array geometry verification */ | |
11939 | int imsm_layout = -1; | |
6d4d9ab2 | 11940 | imsm_status_t rv; |
471bceb6 KW |
11941 | |
11942 | getinfo_super_imsm_volume(st, &info, NULL); | |
089f9d79 JS |
11943 | if (geo->level != info.array.level && geo->level >= 0 && |
11944 | geo->level != UnSet) { | |
471bceb6 KW |
11945 | switch (info.array.level) { |
11946 | case 0: | |
11947 | if (geo->level == 5) { | |
b5347799 | 11948 | change = CH_MIGRATION; |
e13ce846 | 11949 | if (geo->layout != ALGORITHM_LEFT_ASYMMETRIC) { |
7a862a02 | 11950 | pr_err("Error. Requested Layout not supported (left-asymmetric layout is supported only)!\n"); |
e13ce846 AK |
11951 | change = -1; |
11952 | goto analyse_change_exit; | |
11953 | } | |
67a2db32 | 11954 | imsm_layout = geo->layout; |
471bceb6 | 11955 | check_devs = 1; |
e91a3bad LM |
11956 | devNumChange = 1; /* parity disk added */ |
11957 | } else if (geo->level == 10) { | |
471bceb6 KW |
11958 | change = CH_TAKEOVER; |
11959 | check_devs = 1; | |
e91a3bad | 11960 | devNumChange = 2; /* two mirrors added */ |
67a2db32 | 11961 | imsm_layout = 0x102; /* imsm supported layout */ |
471bceb6 | 11962 | } |
dfe77a9e KW |
11963 | break; |
11964 | case 1: | |
471bceb6 KW |
11965 | case 10: |
11966 | if (geo->level == 0) { | |
11967 | change = CH_TAKEOVER; | |
11968 | check_devs = 1; | |
e91a3bad | 11969 | devNumChange = -(geo->raid_disks/2); |
67a2db32 | 11970 | imsm_layout = 0; /* imsm raid0 layout */ |
471bceb6 KW |
11971 | } |
11972 | break; | |
11973 | } | |
11974 | if (change == -1) { | |
7a862a02 | 11975 | pr_err("Error. Level Migration from %d to %d not supported!\n", |
e7b84f9d | 11976 | info.array.level, geo->level); |
471bceb6 KW |
11977 | goto analyse_change_exit; |
11978 | } | |
11979 | } else | |
11980 | geo->level = info.array.level; | |
11981 | ||
089f9d79 JS |
11982 | if (geo->layout != info.array.layout && |
11983 | (geo->layout != UnSet && geo->layout != -1)) { | |
b5347799 | 11984 | change = CH_MIGRATION; |
089f9d79 JS |
11985 | if (info.array.layout == 0 && info.array.level == 5 && |
11986 | geo->layout == 5) { | |
471bceb6 | 11987 | /* reshape 5 -> 4 */ |
089f9d79 JS |
11988 | } else if (info.array.layout == 5 && info.array.level == 5 && |
11989 | geo->layout == 0) { | |
471bceb6 KW |
11990 | /* reshape 4 -> 5 */ |
11991 | geo->layout = 0; | |
11992 | geo->level = 5; | |
11993 | } else { | |
7a862a02 | 11994 | pr_err("Error. Layout Migration from %d to %d not supported!\n", |
e7b84f9d | 11995 | info.array.layout, geo->layout); |
471bceb6 KW |
11996 | change = -1; |
11997 | goto analyse_change_exit; | |
11998 | } | |
67a2db32 | 11999 | } else { |
471bceb6 | 12000 | geo->layout = info.array.layout; |
67a2db32 AK |
12001 | if (imsm_layout == -1) |
12002 | imsm_layout = info.array.layout; | |
12003 | } | |
471bceb6 | 12004 | |
089f9d79 JS |
12005 | if (geo->chunksize > 0 && geo->chunksize != UnSet && |
12006 | geo->chunksize != info.array.chunk_size) { | |
2d2b0eb7 MD |
12007 | if (info.array.level == 10) { |
12008 | pr_err("Error. Chunk size change for RAID 10 is not supported.\n"); | |
12009 | change = -1; | |
12010 | goto analyse_change_exit; | |
1e9b2c3f PB |
12011 | } else if (info.component_size % (geo->chunksize/512)) { |
12012 | pr_err("New chunk size (%dK) does not evenly divide device size (%lluk). Aborting...\n", | |
12013 | geo->chunksize/1024, info.component_size/2); | |
12014 | change = -1; | |
12015 | goto analyse_change_exit; | |
2d2b0eb7 | 12016 | } |
b5347799 | 12017 | change = CH_MIGRATION; |
2d2b0eb7 | 12018 | } else { |
471bceb6 | 12019 | geo->chunksize = info.array.chunk_size; |
2d2b0eb7 | 12020 | } |
471bceb6 | 12021 | |
cbaa7904 | 12022 | if (geo->size > 0) { |
7abc9871 | 12023 | if (change != -1) { |
7a862a02 | 12024 | pr_err("Error. Size change should be the only one at a time.\n"); |
7abc9871 AK |
12025 | change = -1; |
12026 | goto analyse_change_exit; | |
12027 | } | |
6d4d9ab2 | 12028 | |
cbaa7904 | 12029 | rv = imsm_analyze_expand(st, geo, &info, direction); |
6d4d9ab2 | 12030 | if (rv != IMSM_STATUS_OK) |
cbaa7904 | 12031 | goto analyse_change_exit; |
7abc9871 AK |
12032 | change = CH_ARRAY_SIZE; |
12033 | } | |
cbaa7904 MT |
12034 | |
12035 | chunk = geo->chunksize / 1024; | |
471bceb6 KW |
12036 | if (!validate_geometry_imsm(st, |
12037 | geo->level, | |
67a2db32 | 12038 | imsm_layout, |
e91a3bad | 12039 | geo->raid_disks + devNumChange, |
c21e737b | 12040 | &chunk, |
af4348dd | 12041 | geo->size, INVALID_SECTORS, |
5308f117 | 12042 | 0, 0, info.consistency_policy, 1)) |
471bceb6 KW |
12043 | change = -1; |
12044 | ||
12045 | if (check_devs) { | |
12046 | struct intel_super *super = st->sb; | |
12047 | struct imsm_super *mpb = super->anchor; | |
12048 | ||
12049 | if (mpb->num_raid_devs > 1) { | |
f1cc8ab9 LF |
12050 | pr_err("Error. Cannot perform operation on %s- for this operation " |
12051 | "it MUST be single array in container\n", geo->dev_name); | |
471bceb6 KW |
12052 | change = -1; |
12053 | } | |
12054 | } | |
12055 | ||
12056 | analyse_change_exit: | |
089f9d79 JS |
12057 | if (direction == ROLLBACK_METADATA_CHANGES && |
12058 | (change == CH_MIGRATION || change == CH_TAKEOVER)) { | |
7a862a02 | 12059 | dprintf("imsm: Metadata changes rollback is not supported for migration and takeover operations.\n"); |
fbf3d202 AK |
12060 | change = -1; |
12061 | } | |
471bceb6 | 12062 | return change; |
694575e7 KW |
12063 | } |
12064 | ||
bb025c2f KW |
12065 | int imsm_takeover(struct supertype *st, struct geo_params *geo) |
12066 | { | |
12067 | struct intel_super *super = st->sb; | |
12068 | struct imsm_update_takeover *u; | |
12069 | ||
503975b9 | 12070 | u = xmalloc(sizeof(struct imsm_update_takeover)); |
bb025c2f KW |
12071 | |
12072 | u->type = update_takeover; | |
12073 | u->subarray = super->current_vol; | |
12074 | ||
12075 | /* 10->0 transition */ | |
12076 | if (geo->level == 0) | |
12077 | u->direction = R10_TO_R0; | |
12078 | ||
0529c688 KW |
12079 | /* 0->10 transition */ |
12080 | if (geo->level == 10) | |
12081 | u->direction = R0_TO_R10; | |
12082 | ||
bb025c2f KW |
12083 | /* update metadata locally */ |
12084 | imsm_update_metadata_locally(st, u, | |
12085 | sizeof(struct imsm_update_takeover)); | |
12086 | /* and possibly remotely */ | |
12087 | if (st->update_tail) | |
12088 | append_metadata_update(st, u, | |
12089 | sizeof(struct imsm_update_takeover)); | |
12090 | else | |
12091 | free(u); | |
12092 | ||
12093 | return 0; | |
12094 | } | |
12095 | ||
895ffd99 MT |
12096 | /* Flush size update if size calculated by num_data_stripes is higher than |
12097 | * imsm_dev_size to eliminate differences during reshape. | |
12098 | * Mdmon will recalculate them correctly. | |
12099 | * If subarray index is not set then check whole container. | |
12100 | * Returns: | |
12101 | * 0 - no error occurred | |
12102 | * 1 - error detected | |
12103 | */ | |
12104 | static int imsm_fix_size_mismatch(struct supertype *st, int subarray_index) | |
12105 | { | |
12106 | struct intel_super *super = st->sb; | |
12107 | int tmp = super->current_vol; | |
12108 | int ret_val = 1; | |
12109 | int i; | |
12110 | ||
12111 | for (i = 0; i < super->anchor->num_raid_devs; i++) { | |
12112 | if (subarray_index >= 0 && i != subarray_index) | |
12113 | continue; | |
12114 | super->current_vol = i; | |
12115 | struct imsm_dev *dev = get_imsm_dev(super, super->current_vol); | |
12116 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
12117 | unsigned int disc_count = imsm_num_data_members(map); | |
12118 | struct geo_params geo; | |
12119 | struct imsm_update_size_change *update; | |
12120 | unsigned long long calc_size = per_dev_array_size(map) * disc_count; | |
12121 | unsigned long long d_size = imsm_dev_size(dev); | |
12122 | int u_size; | |
12123 | ||
42e02e61 | 12124 | if (calc_size == d_size) |
895ffd99 MT |
12125 | continue; |
12126 | ||
ff904202 MT |
12127 | /* There is a difference, confirm that imsm_dev_size is |
12128 | * smaller and push update. | |
895ffd99 | 12129 | */ |
ff904202 MT |
12130 | if (d_size > calc_size) { |
12131 | pr_err("imsm: dev size of subarray %d is incorrect\n", | |
12132 | i); | |
895ffd99 MT |
12133 | goto exit; |
12134 | } | |
12135 | memset(&geo, 0, sizeof(struct geo_params)); | |
12136 | geo.size = d_size; | |
12137 | u_size = imsm_create_metadata_update_for_size_change(st, &geo, | |
12138 | &update); | |
895ffd99 MT |
12139 | imsm_update_metadata_locally(st, update, u_size); |
12140 | if (st->update_tail) { | |
12141 | append_metadata_update(st, update, u_size); | |
12142 | flush_metadata_updates(st); | |
12143 | st->update_tail = &st->updates; | |
12144 | } else { | |
12145 | imsm_sync_metadata(st); | |
5ce5a15f | 12146 | free(update); |
895ffd99 MT |
12147 | } |
12148 | } | |
12149 | ret_val = 0; | |
12150 | exit: | |
12151 | super->current_vol = tmp; | |
12152 | return ret_val; | |
12153 | } | |
12154 | ||
0acda705 MK |
12155 | /** |
12156 | * shape_to_geo() - fill geo_params from shape. | |
12157 | * | |
12158 | * @shape: array details. | |
12159 | * @geo: new geometry params. | |
12160 | * Returns: 0 on success, 1 otherwise. | |
12161 | */ | |
12162 | static void shape_to_geo(struct shape *shape, struct geo_params *geo) | |
12163 | { | |
12164 | assert(shape); | |
12165 | assert(geo); | |
12166 | ||
12167 | geo->dev_name = shape->dev; | |
12168 | geo->size = shape->size; | |
12169 | geo->level = shape->level; | |
12170 | geo->layout = shape->layout; | |
12171 | geo->chunksize = shape->chunk; | |
12172 | geo->raid_disks = shape->raiddisks; | |
12173 | } | |
12174 | ||
12175 | static int imsm_reshape_super(struct supertype *st, struct shape *shape, struct context *c) | |
78b10e66 | 12176 | { |
78b10e66 | 12177 | int ret_val = 1; |
0acda705 | 12178 | struct geo_params geo = {0}; |
78b10e66 | 12179 | |
1ade5cc1 | 12180 | dprintf("(enter)\n"); |
78b10e66 | 12181 | |
0acda705 | 12182 | shape_to_geo(shape, &geo); |
4dd2df09 | 12183 | strcpy(geo.devnm, st->devnm); |
0acda705 MK |
12184 | if (shape->delta_disks != UnSet) |
12185 | geo.raid_disks += shape->delta_disks; | |
78b10e66 | 12186 | |
1ade5cc1 N |
12187 | dprintf("for level : %i\n", geo.level); |
12188 | dprintf("for raid_disks : %i\n", geo.raid_disks); | |
78b10e66 | 12189 | |
4dd2df09 | 12190 | if (strcmp(st->container_devnm, st->devnm) == 0) { |
694575e7 KW |
12191 | /* On container level we can only increase number of devices. */ |
12192 | dprintf("imsm: info: Container operation\n"); | |
78b10e66 | 12193 | int old_raid_disks = 0; |
6dc0be30 | 12194 | |
78b10e66 | 12195 | if (imsm_reshape_is_allowed_on_container( |
0acda705 | 12196 | st, &geo, &old_raid_disks, shape->direction)) { |
78b10e66 N |
12197 | struct imsm_update_reshape *u = NULL; |
12198 | int len; | |
12199 | ||
895ffd99 MT |
12200 | if (imsm_fix_size_mismatch(st, -1)) { |
12201 | dprintf("imsm: Cannot fix size mismatch\n"); | |
12202 | goto exit_imsm_reshape_super; | |
12203 | } | |
12204 | ||
78b10e66 N |
12205 | len = imsm_create_metadata_update_for_reshape( |
12206 | st, &geo, old_raid_disks, &u); | |
12207 | ||
ed08d51c AK |
12208 | if (len <= 0) { |
12209 | dprintf("imsm: Cannot prepare update\n"); | |
12210 | goto exit_imsm_reshape_super; | |
12211 | } | |
12212 | ||
8dd70bce AK |
12213 | ret_val = 0; |
12214 | /* update metadata locally */ | |
12215 | imsm_update_metadata_locally(st, u, len); | |
12216 | /* and possibly remotely */ | |
12217 | if (st->update_tail) | |
12218 | append_metadata_update(st, u, len); | |
12219 | else | |
ed08d51c | 12220 | free(u); |
8dd70bce | 12221 | |
694575e7 | 12222 | } else { |
7a862a02 | 12223 | pr_err("(imsm) Operation is not allowed on this container\n"); |
694575e7 KW |
12224 | } |
12225 | } else { | |
12226 | /* On volume level we support following operations | |
471bceb6 KW |
12227 | * - takeover: raid10 -> raid0; raid0 -> raid10 |
12228 | * - chunk size migration | |
12229 | * - migration: raid5 -> raid0; raid0 -> raid5 | |
12230 | */ | |
12231 | struct intel_super *super = st->sb; | |
12232 | struct intel_dev *dev = super->devlist; | |
4dd2df09 | 12233 | int change; |
694575e7 | 12234 | dprintf("imsm: info: Volume operation\n"); |
471bceb6 KW |
12235 | /* find requested device */ |
12236 | while (dev) { | |
1011e834 | 12237 | char *devnm = |
4dd2df09 N |
12238 | imsm_find_array_devnm_by_subdev( |
12239 | dev->index, st->container_devnm); | |
12240 | if (devnm && strcmp(devnm, geo.devnm) == 0) | |
471bceb6 KW |
12241 | break; |
12242 | dev = dev->next; | |
12243 | } | |
12244 | if (dev == NULL) { | |
4dd2df09 N |
12245 | pr_err("Cannot find %s (%s) subarray\n", |
12246 | geo.dev_name, geo.devnm); | |
471bceb6 KW |
12247 | goto exit_imsm_reshape_super; |
12248 | } | |
12249 | super->current_vol = dev->index; | |
0acda705 | 12250 | change = imsm_analyze_change(st, &geo, shape->direction); |
694575e7 | 12251 | switch (change) { |
471bceb6 | 12252 | case CH_TAKEOVER: |
bb025c2f | 12253 | ret_val = imsm_takeover(st, &geo); |
694575e7 | 12254 | break; |
48c5303a PC |
12255 | case CH_MIGRATION: { |
12256 | struct imsm_update_reshape_migration *u = NULL; | |
12257 | int len = | |
12258 | imsm_create_metadata_update_for_migration( | |
12259 | st, &geo, &u); | |
12260 | if (len < 1) { | |
7a862a02 | 12261 | dprintf("imsm: Cannot prepare update\n"); |
48c5303a PC |
12262 | break; |
12263 | } | |
471bceb6 | 12264 | ret_val = 0; |
48c5303a PC |
12265 | /* update metadata locally */ |
12266 | imsm_update_metadata_locally(st, u, len); | |
12267 | /* and possibly remotely */ | |
12268 | if (st->update_tail) | |
12269 | append_metadata_update(st, u, len); | |
12270 | else | |
12271 | free(u); | |
12272 | } | |
12273 | break; | |
7abc9871 | 12274 | case CH_ARRAY_SIZE: { |
f3871fdc AK |
12275 | struct imsm_update_size_change *u = NULL; |
12276 | int len = | |
12277 | imsm_create_metadata_update_for_size_change( | |
12278 | st, &geo, &u); | |
12279 | if (len < 1) { | |
7a862a02 | 12280 | dprintf("imsm: Cannot prepare update\n"); |
f3871fdc AK |
12281 | break; |
12282 | } | |
12283 | ret_val = 0; | |
12284 | /* update metadata locally */ | |
12285 | imsm_update_metadata_locally(st, u, len); | |
12286 | /* and possibly remotely */ | |
12287 | if (st->update_tail) | |
12288 | append_metadata_update(st, u, len); | |
12289 | else | |
12290 | free(u); | |
7abc9871 AK |
12291 | } |
12292 | break; | |
471bceb6 KW |
12293 | default: |
12294 | ret_val = 1; | |
694575e7 | 12295 | } |
694575e7 | 12296 | } |
78b10e66 | 12297 | |
ed08d51c | 12298 | exit_imsm_reshape_super: |
78b10e66 N |
12299 | dprintf("imsm: reshape_super Exit code = %i\n", ret_val); |
12300 | return ret_val; | |
12301 | } | |
2cda7640 | 12302 | |
0febb20c AO |
12303 | #define COMPLETED_OK 0 |
12304 | #define COMPLETED_NONE 1 | |
12305 | #define COMPLETED_DELAYED 2 | |
12306 | ||
12307 | static int read_completed(int fd, unsigned long long *val) | |
12308 | { | |
12309 | int ret; | |
90fd7001 | 12310 | char buf[SYSFS_MAX_BUF_SIZE]; |
0febb20c | 12311 | |
90fd7001 | 12312 | ret = sysfs_fd_get_str(fd, buf, sizeof(buf)); |
0febb20c AO |
12313 | if (ret < 0) |
12314 | return ret; | |
12315 | ||
12316 | ret = COMPLETED_OK; | |
b823c8f9 | 12317 | if (str_is_none(buf) == true) { |
0febb20c AO |
12318 | ret = COMPLETED_NONE; |
12319 | } else if (strncmp(buf, "delayed", 7) == 0) { | |
12320 | ret = COMPLETED_DELAYED; | |
12321 | } else { | |
12322 | char *ep; | |
12323 | *val = strtoull(buf, &ep, 0); | |
12324 | if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' ')) | |
12325 | ret = -1; | |
12326 | } | |
12327 | return ret; | |
12328 | } | |
12329 | ||
eee67a47 AK |
12330 | /******************************************************************************* |
12331 | * Function: wait_for_reshape_imsm | |
12332 | * Description: Function writes new sync_max value and waits until | |
12333 | * reshape process reach new position | |
12334 | * Parameters: | |
12335 | * sra : general array info | |
eee67a47 AK |
12336 | * ndata : number of disks in new array's layout |
12337 | * Returns: | |
12338 | * 0 : success, | |
12339 | * 1 : there is no reshape in progress, | |
12340 | * -1 : fail | |
12341 | ******************************************************************************/ | |
ae9f01f8 | 12342 | int wait_for_reshape_imsm(struct mdinfo *sra, int ndata) |
eee67a47 | 12343 | { |
85ca499c | 12344 | int fd = sysfs_get_fd(sra, NULL, "sync_completed"); |
df2647fa | 12345 | int retry = 3; |
eee67a47 | 12346 | unsigned long long completed; |
ae9f01f8 AK |
12347 | /* to_complete : new sync_max position */ |
12348 | unsigned long long to_complete = sra->reshape_progress; | |
12349 | unsigned long long position_to_set = to_complete / ndata; | |
eee67a47 | 12350 | |
4389ce73 | 12351 | if (!is_fd_valid(fd)) { |
1ade5cc1 | 12352 | dprintf("cannot open reshape_position\n"); |
eee67a47 | 12353 | return 1; |
ae9f01f8 | 12354 | } |
eee67a47 | 12355 | |
df2647fa PB |
12356 | do { |
12357 | if (sysfs_fd_get_ll(fd, &completed) < 0) { | |
12358 | if (!retry) { | |
12359 | dprintf("cannot read reshape_position (no reshape in progres)\n"); | |
12360 | close(fd); | |
12361 | return 1; | |
12362 | } | |
239b3cc0 | 12363 | sleep_for(0, MSEC_TO_NSEC(30), true); |
df2647fa PB |
12364 | } else |
12365 | break; | |
12366 | } while (retry--); | |
eee67a47 | 12367 | |
85ca499c | 12368 | if (completed > position_to_set) { |
1ade5cc1 | 12369 | dprintf("wrong next position to set %llu (%llu)\n", |
85ca499c | 12370 | to_complete, position_to_set); |
ae9f01f8 AK |
12371 | close(fd); |
12372 | return -1; | |
12373 | } | |
12374 | dprintf("Position set: %llu\n", position_to_set); | |
12375 | if (sysfs_set_num(sra, NULL, "sync_max", | |
12376 | position_to_set) != 0) { | |
1ade5cc1 | 12377 | dprintf("cannot set reshape position to %llu\n", |
ae9f01f8 AK |
12378 | position_to_set); |
12379 | close(fd); | |
12380 | return -1; | |
eee67a47 AK |
12381 | } |
12382 | ||
eee67a47 | 12383 | do { |
0febb20c | 12384 | int rc; |
90fd7001 | 12385 | char action[SYSFS_MAX_BUF_SIZE]; |
5ff3a780 | 12386 | int timeout = 3000; |
0febb20c | 12387 | |
5ff3a780 | 12388 | sysfs_wait(fd, &timeout); |
a47e44fb | 12389 | if (sysfs_get_str(sra, NULL, "sync_action", |
90fd7001 | 12390 | action, sizeof(action)) > 0 && |
d7d3809a | 12391 | strncmp(action, "reshape", 7) != 0) { |
b2be2b62 AO |
12392 | if (strncmp(action, "idle", 4) == 0) |
12393 | break; | |
d7d3809a AP |
12394 | close(fd); |
12395 | return -1; | |
12396 | } | |
0febb20c AO |
12397 | |
12398 | rc = read_completed(fd, &completed); | |
12399 | if (rc < 0) { | |
1ade5cc1 | 12400 | dprintf("cannot read reshape_position (in loop)\n"); |
eee67a47 AK |
12401 | close(fd); |
12402 | return 1; | |
0febb20c AO |
12403 | } else if (rc == COMPLETED_NONE) |
12404 | break; | |
85ca499c | 12405 | } while (completed < position_to_set); |
b2be2b62 | 12406 | |
eee67a47 AK |
12407 | close(fd); |
12408 | return 0; | |
eee67a47 AK |
12409 | } |
12410 | ||
b915c95f AK |
12411 | /******************************************************************************* |
12412 | * Function: check_degradation_change | |
12413 | * Description: Check that array hasn't become failed. | |
12414 | * Parameters: | |
12415 | * info : for sysfs access | |
12416 | * sources : source disks descriptors | |
12417 | * degraded: previous degradation level | |
12418 | * Returns: | |
12419 | * degradation level | |
12420 | ******************************************************************************/ | |
12421 | int check_degradation_change(struct mdinfo *info, | |
12422 | int *sources, | |
12423 | int degraded) | |
12424 | { | |
12425 | unsigned long long new_degraded; | |
e1993023 LD |
12426 | int rv; |
12427 | ||
12428 | rv = sysfs_get_ll(info, NULL, "degraded", &new_degraded); | |
089f9d79 | 12429 | if (rv == -1 || (new_degraded != (unsigned long long)degraded)) { |
b915c95f AK |
12430 | /* check each device to ensure it is still working */ |
12431 | struct mdinfo *sd; | |
12432 | new_degraded = 0; | |
12433 | for (sd = info->devs ; sd ; sd = sd->next) { | |
12434 | if (sd->disk.state & (1<<MD_DISK_FAULTY)) | |
12435 | continue; | |
12436 | if (sd->disk.state & (1<<MD_DISK_SYNC)) { | |
90fd7001 | 12437 | char sbuf[SYSFS_MAX_BUF_SIZE]; |
4389ce73 | 12438 | int raid_disk = sd->disk.raid_disk; |
cf52eff5 | 12439 | |
b915c95f | 12440 | if (sysfs_get_str(info, |
cf52eff5 | 12441 | sd, "state", sbuf, sizeof(sbuf)) < 0 || |
b915c95f AK |
12442 | strstr(sbuf, "faulty") || |
12443 | strstr(sbuf, "in_sync") == NULL) { | |
12444 | /* this device is dead */ | |
12445 | sd->disk.state = (1<<MD_DISK_FAULTY); | |
4389ce73 MT |
12446 | if (raid_disk >= 0) |
12447 | close_fd(&sources[raid_disk]); | |
b915c95f AK |
12448 | new_degraded++; |
12449 | } | |
12450 | } | |
12451 | } | |
12452 | } | |
12453 | ||
12454 | return new_degraded; | |
12455 | } | |
12456 | ||
10f22854 AK |
12457 | /******************************************************************************* |
12458 | * Function: imsm_manage_reshape | |
12459 | * Description: Function finds array under reshape and it manages reshape | |
12460 | * process. It creates stripes backups (if required) and sets | |
942e1cdb | 12461 | * checkpoints. |
10f22854 AK |
12462 | * Parameters: |
12463 | * afd : Backup handle (nattive) - not used | |
12464 | * sra : general array info | |
12465 | * reshape : reshape parameters - not used | |
12466 | * st : supertype structure | |
12467 | * blocks : size of critical section [blocks] | |
12468 | * fds : table of source device descriptor | |
12469 | * offsets : start of array (offest per devices) | |
12470 | * dests : not used | |
12471 | * destfd : table of destination device descriptor | |
12472 | * destoffsets : table of destination offsets (per device) | |
12473 | * Returns: | |
12474 | * 1 : success, reshape is done | |
12475 | * 0 : fail | |
12476 | ******************************************************************************/ | |
999b4972 N |
12477 | static int imsm_manage_reshape( |
12478 | int afd, struct mdinfo *sra, struct reshape *reshape, | |
10f22854 | 12479 | struct supertype *st, unsigned long backup_blocks, |
999b4972 N |
12480 | int *fds, unsigned long long *offsets, |
12481 | int dests, int *destfd, unsigned long long *destoffsets) | |
12482 | { | |
10f22854 AK |
12483 | int ret_val = 0; |
12484 | struct intel_super *super = st->sb; | |
594dc1b8 | 12485 | struct intel_dev *dv; |
de44e46f | 12486 | unsigned int sector_size = super->sector_size; |
10f22854 | 12487 | struct imsm_dev *dev = NULL; |
9529d343 | 12488 | struct imsm_map *map_src, *map_dest; |
10f22854 AK |
12489 | int migr_vol_qan = 0; |
12490 | int ndata, odata; /* [bytes] */ | |
12491 | int chunk; /* [bytes] */ | |
12492 | struct migr_record *migr_rec; | |
12493 | char *buf = NULL; | |
12494 | unsigned int buf_size; /* [bytes] */ | |
12495 | unsigned long long max_position; /* array size [bytes] */ | |
12496 | unsigned long long next_step; /* [blocks]/[bytes] */ | |
12497 | unsigned long long old_data_stripe_length; | |
10f22854 AK |
12498 | unsigned long long start_src; /* [bytes] */ |
12499 | unsigned long long start; /* [bytes] */ | |
12500 | unsigned long long start_buf_shift; /* [bytes] */ | |
b915c95f | 12501 | int degraded = 0; |
ab724b98 | 12502 | int source_layout = 0; |
895ffd99 | 12503 | int subarray_index = -1; |
10f22854 | 12504 | |
79a16a9b JS |
12505 | if (!sra) |
12506 | return ret_val; | |
12507 | ||
12508 | if (!fds || !offsets) | |
10f22854 AK |
12509 | goto abort; |
12510 | ||
12511 | /* Find volume during the reshape */ | |
12512 | for (dv = super->devlist; dv; dv = dv->next) { | |
fc54fe7a JS |
12513 | if (dv->dev->vol.migr_type == MIGR_GEN_MIGR && |
12514 | dv->dev->vol.migr_state == 1) { | |
10f22854 AK |
12515 | dev = dv->dev; |
12516 | migr_vol_qan++; | |
895ffd99 | 12517 | subarray_index = dv->index; |
10f22854 AK |
12518 | } |
12519 | } | |
12520 | /* Only one volume can migrate at the same time */ | |
12521 | if (migr_vol_qan != 1) { | |
676e87a8 | 12522 | pr_err("%s", migr_vol_qan ? |
10f22854 AK |
12523 | "Number of migrating volumes greater than 1\n" : |
12524 | "There is no volume during migrationg\n"); | |
12525 | goto abort; | |
12526 | } | |
12527 | ||
9529d343 | 12528 | map_dest = get_imsm_map(dev, MAP_0); |
238c0a71 | 12529 | map_src = get_imsm_map(dev, MAP_1); |
10f22854 AK |
12530 | if (map_src == NULL) |
12531 | goto abort; | |
10f22854 | 12532 | |
9529d343 MD |
12533 | ndata = imsm_num_data_members(map_dest); |
12534 | odata = imsm_num_data_members(map_src); | |
10f22854 | 12535 | |
7b1ab482 | 12536 | chunk = __le16_to_cpu(map_src->blocks_per_strip) * 512; |
10f22854 AK |
12537 | old_data_stripe_length = odata * chunk; |
12538 | ||
12539 | migr_rec = super->migr_rec; | |
12540 | ||
10f22854 AK |
12541 | /* initialize migration record for start condition */ |
12542 | if (sra->reshape_progress == 0) | |
12543 | init_migr_record_imsm(st, dev, sra); | |
b2c59438 AK |
12544 | else { |
12545 | if (__le32_to_cpu(migr_rec->rec_status) != UNIT_SRC_NORMAL) { | |
7a862a02 | 12546 | dprintf("imsm: cannot restart migration when data are present in copy area.\n"); |
b2c59438 AK |
12547 | goto abort; |
12548 | } | |
6a75c8ca AK |
12549 | /* Save checkpoint to update migration record for current |
12550 | * reshape position (in md). It can be farther than current | |
12551 | * reshape position in metadata. | |
12552 | */ | |
12553 | if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL) == 1) { | |
12554 | /* ignore error == 2, this can mean end of reshape here | |
12555 | */ | |
7a862a02 | 12556 | dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL, initial save)\n"); |
6a75c8ca AK |
12557 | goto abort; |
12558 | } | |
b2c59438 | 12559 | } |
10f22854 AK |
12560 | |
12561 | /* size for data */ | |
12562 | buf_size = __le32_to_cpu(migr_rec->blocks_per_unit) * 512; | |
12563 | /* extend buffer size for parity disk */ | |
12564 | buf_size += __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512; | |
3e684231 | 12565 | /* add space for stripe alignment */ |
10f22854 | 12566 | buf_size += old_data_stripe_length; |
de44e46f PB |
12567 | if (posix_memalign((void **)&buf, MAX_SECTOR_SIZE, buf_size)) { |
12568 | dprintf("imsm: Cannot allocate checkpoint buffer\n"); | |
10f22854 AK |
12569 | goto abort; |
12570 | } | |
12571 | ||
3ef4403c | 12572 | max_position = sra->component_size * ndata; |
68eb8bc6 | 12573 | source_layout = imsm_level_to_layout(map_src->raid_level); |
10f22854 | 12574 | |
9f421827 PB |
12575 | while (current_migr_unit(migr_rec) < |
12576 | get_num_migr_units(migr_rec)) { | |
10f22854 AK |
12577 | /* current reshape position [blocks] */ |
12578 | unsigned long long current_position = | |
12579 | __le32_to_cpu(migr_rec->blocks_per_unit) | |
9f421827 | 12580 | * current_migr_unit(migr_rec); |
10f22854 AK |
12581 | unsigned long long border; |
12582 | ||
b915c95f AK |
12583 | /* Check that array hasn't become failed. |
12584 | */ | |
12585 | degraded = check_degradation_change(sra, fds, degraded); | |
12586 | if (degraded > 1) { | |
7a862a02 | 12587 | dprintf("imsm: Abort reshape due to degradation level (%i)\n", degraded); |
b915c95f AK |
12588 | goto abort; |
12589 | } | |
12590 | ||
10f22854 AK |
12591 | next_step = __le32_to_cpu(migr_rec->blocks_per_unit); |
12592 | ||
12593 | if ((current_position + next_step) > max_position) | |
12594 | next_step = max_position - current_position; | |
12595 | ||
92144abf | 12596 | start = current_position * 512; |
10f22854 | 12597 | |
942e1cdb | 12598 | /* align reading start to old geometry */ |
10f22854 AK |
12599 | start_buf_shift = start % old_data_stripe_length; |
12600 | start_src = start - start_buf_shift; | |
12601 | ||
12602 | border = (start_src / odata) - (start / ndata); | |
12603 | border /= 512; | |
12604 | if (border <= __le32_to_cpu(migr_rec->dest_depth_per_unit)) { | |
12605 | /* save critical stripes to buf | |
12606 | * start - start address of current unit | |
12607 | * to backup [bytes] | |
12608 | * start_src - start address of current unit | |
12609 | * to backup alligned to source array | |
12610 | * [bytes] | |
12611 | */ | |
594dc1b8 | 12612 | unsigned long long next_step_filler; |
10f22854 AK |
12613 | unsigned long long copy_length = next_step * 512; |
12614 | ||
12615 | /* allign copy area length to stripe in old geometry */ | |
12616 | next_step_filler = ((copy_length + start_buf_shift) | |
12617 | % old_data_stripe_length); | |
12618 | if (next_step_filler) | |
12619 | next_step_filler = (old_data_stripe_length | |
12620 | - next_step_filler); | |
7a862a02 | 12621 | dprintf("save_stripes() parameters: start = %llu,\tstart_src = %llu,\tnext_step*512 = %llu,\tstart_in_buf_shift = %llu,\tnext_step_filler = %llu\n", |
10f22854 AK |
12622 | start, start_src, copy_length, |
12623 | start_buf_shift, next_step_filler); | |
12624 | ||
12625 | if (save_stripes(fds, offsets, map_src->num_members, | |
ab724b98 AK |
12626 | chunk, map_src->raid_level, |
12627 | source_layout, 0, NULL, start_src, | |
10f22854 AK |
12628 | copy_length + |
12629 | next_step_filler + start_buf_shift, | |
12630 | buf)) { | |
7a862a02 | 12631 | dprintf("imsm: Cannot save stripes to buffer\n"); |
10f22854 AK |
12632 | goto abort; |
12633 | } | |
12634 | /* Convert data to destination format and store it | |
12635 | * in backup general migration area | |
12636 | */ | |
12637 | if (save_backup_imsm(st, dev, sra, | |
aea93171 | 12638 | buf + start_buf_shift, copy_length)) { |
7a862a02 | 12639 | dprintf("imsm: Cannot save stripes to target devices\n"); |
10f22854 AK |
12640 | goto abort; |
12641 | } | |
12642 | if (save_checkpoint_imsm(st, sra, | |
12643 | UNIT_SRC_IN_CP_AREA)) { | |
7a862a02 | 12644 | dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_IN_CP_AREA)\n"); |
10f22854 AK |
12645 | goto abort; |
12646 | } | |
8016a6d4 AK |
12647 | } else { |
12648 | /* set next step to use whole border area */ | |
12649 | border /= next_step; | |
12650 | if (border > 1) | |
12651 | next_step *= border; | |
10f22854 AK |
12652 | } |
12653 | /* When data backed up, checkpoint stored, | |
12654 | * kick the kernel to reshape unit of data | |
12655 | */ | |
12656 | next_step = next_step + sra->reshape_progress; | |
8016a6d4 AK |
12657 | /* limit next step to array max position */ |
12658 | if (next_step > max_position) | |
12659 | next_step = max_position; | |
10f22854 AK |
12660 | sysfs_set_num(sra, NULL, "suspend_lo", sra->reshape_progress); |
12661 | sysfs_set_num(sra, NULL, "suspend_hi", next_step); | |
ae9f01f8 | 12662 | sra->reshape_progress = next_step; |
10f22854 AK |
12663 | |
12664 | /* wait until reshape finish */ | |
c85338c6 | 12665 | if (wait_for_reshape_imsm(sra, ndata)) { |
c47b0ff6 AK |
12666 | dprintf("wait_for_reshape_imsm returned error!\n"); |
12667 | goto abort; | |
12668 | } | |
84d11e6c N |
12669 | if (sigterm) |
12670 | goto abort; | |
10f22854 | 12671 | |
0228d92c AK |
12672 | if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL) == 1) { |
12673 | /* ignore error == 2, this can mean end of reshape here | |
12674 | */ | |
7a862a02 | 12675 | dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL)\n"); |
10f22854 AK |
12676 | goto abort; |
12677 | } | |
12678 | ||
12679 | } | |
12680 | ||
71e5411e PB |
12681 | /* clear migr_rec on disks after successful migration */ |
12682 | struct dl *d; | |
12683 | ||
85337573 | 12684 | memset(super->migr_rec_buf, 0, MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE); |
71e5411e PB |
12685 | for (d = super->disks; d; d = d->next) { |
12686 | if (d->index < 0 || is_failed(&d->disk)) | |
12687 | continue; | |
12688 | unsigned long long dsize; | |
12689 | ||
12690 | get_dev_size(d->fd, NULL, &dsize); | |
de44e46f | 12691 | if (lseek64(d->fd, dsize - MIGR_REC_SECTOR_POSITION*sector_size, |
71e5411e | 12692 | SEEK_SET) >= 0) { |
466070ad | 12693 | if ((unsigned int)write(d->fd, super->migr_rec_buf, |
de44e46f PB |
12694 | MIGR_REC_BUF_SECTORS*sector_size) != |
12695 | MIGR_REC_BUF_SECTORS*sector_size) | |
71e5411e PB |
12696 | perror("Write migr_rec failed"); |
12697 | } | |
12698 | } | |
12699 | ||
10f22854 AK |
12700 | /* return '1' if done */ |
12701 | ret_val = 1; | |
895ffd99 MT |
12702 | |
12703 | /* After the reshape eliminate size mismatch in metadata. | |
12704 | * Don't update md/component_size here, volume hasn't | |
12705 | * to take whole space. It is allowed by kernel. | |
12706 | * md/component_size will be set propoperly after next assembly. | |
12707 | */ | |
12708 | imsm_fix_size_mismatch(st, subarray_index); | |
12709 | ||
10f22854 AK |
12710 | abort: |
12711 | free(buf); | |
942e1cdb N |
12712 | /* See Grow.c: abort_reshape() for further explanation */ |
12713 | sysfs_set_num(sra, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL); | |
12714 | sysfs_set_num(sra, NULL, "suspend_hi", 0); | |
12715 | sysfs_set_num(sra, NULL, "suspend_lo", 0); | |
10f22854 AK |
12716 | |
12717 | return ret_val; | |
999b4972 | 12718 | } |
0c21b485 | 12719 | |
fbc42556 JR |
12720 | /******************************************************************************* |
12721 | * Function: calculate_bitmap_min_chunksize | |
12722 | * Description: Calculates the minimal valid bitmap chunk size | |
12723 | * Parameters: | |
12724 | * max_bits : indicate how many bits can be used for the bitmap | |
12725 | * data_area_size : the size of the data area covered by the bitmap | |
12726 | * | |
12727 | * Returns: | |
12728 | * The bitmap chunk size | |
12729 | ******************************************************************************/ | |
12730 | static unsigned long long | |
12731 | calculate_bitmap_min_chunksize(unsigned long long max_bits, | |
12732 | unsigned long long data_area_size) | |
12733 | { | |
12734 | unsigned long long min_chunk = | |
12735 | 4096; /* sub-page chunks don't work yet.. */ | |
12736 | unsigned long long bits = data_area_size / min_chunk + 1; | |
12737 | ||
12738 | while (bits > max_bits) { | |
12739 | min_chunk *= 2; | |
12740 | bits = (bits + 1) / 2; | |
12741 | } | |
12742 | return min_chunk; | |
12743 | } | |
12744 | ||
12745 | /******************************************************************************* | |
12746 | * Function: calculate_bitmap_chunksize | |
12747 | * Description: Calculates the bitmap chunk size for the given device | |
12748 | * Parameters: | |
12749 | * st : supertype information | |
12750 | * dev : device for the bitmap | |
12751 | * | |
12752 | * Returns: | |
12753 | * The bitmap chunk size | |
12754 | ******************************************************************************/ | |
12755 | static unsigned long long calculate_bitmap_chunksize(struct supertype *st, | |
12756 | struct imsm_dev *dev) | |
12757 | { | |
12758 | struct intel_super *super = st->sb; | |
12759 | unsigned long long min_chunksize; | |
12760 | unsigned long long result = IMSM_DEFAULT_BITMAP_CHUNKSIZE; | |
12761 | size_t dev_size = imsm_dev_size(dev); | |
12762 | ||
12763 | min_chunksize = calculate_bitmap_min_chunksize( | |
12764 | IMSM_BITMAP_AREA_SIZE * super->sector_size, dev_size); | |
12765 | ||
12766 | if (result < min_chunksize) | |
12767 | result = min_chunksize; | |
12768 | ||
12769 | return result; | |
12770 | } | |
12771 | ||
12772 | /******************************************************************************* | |
12773 | * Function: init_bitmap_header | |
12774 | * Description: Initialize the bitmap header structure | |
12775 | * Parameters: | |
12776 | * st : supertype information | |
12777 | * bms : bitmap header struct to initialize | |
12778 | * dev : device for the bitmap | |
12779 | * | |
12780 | * Returns: | |
12781 | * 0 : success | |
12782 | * -1 : fail | |
12783 | ******************************************************************************/ | |
12784 | static int init_bitmap_header(struct supertype *st, struct bitmap_super_s *bms, | |
12785 | struct imsm_dev *dev) | |
12786 | { | |
12787 | int vol_uuid[4]; | |
12788 | ||
12789 | if (!bms || !dev) | |
12790 | return -1; | |
12791 | ||
12792 | bms->magic = __cpu_to_le32(BITMAP_MAGIC); | |
12793 | bms->version = __cpu_to_le32(BITMAP_MAJOR_HI); | |
12794 | bms->daemon_sleep = __cpu_to_le32(IMSM_DEFAULT_BITMAP_DAEMON_SLEEP); | |
12795 | bms->sync_size = __cpu_to_le64(IMSM_BITMAP_AREA_SIZE); | |
12796 | bms->write_behind = __cpu_to_le32(0); | |
12797 | ||
12798 | uuid_from_super_imsm(st, vol_uuid); | |
12799 | memcpy(bms->uuid, vol_uuid, 16); | |
12800 | ||
12801 | bms->chunksize = calculate_bitmap_chunksize(st, dev); | |
12802 | ||
12803 | return 0; | |
12804 | } | |
12805 | ||
12806 | /******************************************************************************* | |
12807 | * Function: validate_internal_bitmap_for_drive | |
12808 | * Description: Verify if the bitmap header for a given drive. | |
12809 | * Parameters: | |
12810 | * st : supertype information | |
12811 | * offset : The offset from the beginning of the drive where to look for | |
12812 | * the bitmap header. | |
12813 | * d : the drive info | |
12814 | * | |
12815 | * Returns: | |
12816 | * 0 : success | |
12817 | * -1 : fail | |
12818 | ******************************************************************************/ | |
12819 | static int validate_internal_bitmap_for_drive(struct supertype *st, | |
12820 | unsigned long long offset, | |
12821 | struct dl *d) | |
12822 | { | |
12823 | struct intel_super *super = st->sb; | |
12824 | int ret = -1; | |
12825 | int vol_uuid[4]; | |
12826 | bitmap_super_t *bms; | |
12827 | int fd; | |
12828 | ||
12829 | if (!d) | |
12830 | return -1; | |
12831 | ||
12832 | void *read_buf; | |
12833 | ||
12834 | if (posix_memalign(&read_buf, MAX_SECTOR_SIZE, IMSM_BITMAP_HEADER_SIZE)) | |
12835 | return -1; | |
12836 | ||
12837 | fd = d->fd; | |
4389ce73 | 12838 | if (!is_fd_valid(fd)) { |
fbc42556 | 12839 | fd = open(d->devname, O_RDONLY, 0); |
4389ce73 MT |
12840 | |
12841 | if (!is_fd_valid(fd)) { | |
fbc42556 JR |
12842 | dprintf("cannot open the device %s\n", d->devname); |
12843 | goto abort; | |
12844 | } | |
12845 | } | |
12846 | ||
12847 | if (lseek64(fd, offset * super->sector_size, SEEK_SET) < 0) | |
12848 | goto abort; | |
12849 | if (read(fd, read_buf, IMSM_BITMAP_HEADER_SIZE) != | |
12850 | IMSM_BITMAP_HEADER_SIZE) | |
12851 | goto abort; | |
12852 | ||
12853 | uuid_from_super_imsm(st, vol_uuid); | |
12854 | ||
12855 | bms = read_buf; | |
12856 | if ((bms->magic != __cpu_to_le32(BITMAP_MAGIC)) || | |
12857 | (bms->version != __cpu_to_le32(BITMAP_MAJOR_HI)) || | |
12858 | (!same_uuid((int *)bms->uuid, vol_uuid, st->ss->swapuuid))) { | |
12859 | dprintf("wrong bitmap header detected\n"); | |
12860 | goto abort; | |
12861 | } | |
12862 | ||
12863 | ret = 0; | |
12864 | abort: | |
4389ce73 MT |
12865 | if (!is_fd_valid(d->fd)) |
12866 | close_fd(&fd); | |
12867 | ||
fbc42556 JR |
12868 | if (read_buf) |
12869 | free(read_buf); | |
12870 | ||
12871 | return ret; | |
12872 | } | |
12873 | ||
12874 | /******************************************************************************* | |
12875 | * Function: validate_internal_bitmap_imsm | |
12876 | * Description: Verify if the bitmap header is in place and with proper data. | |
12877 | * Parameters: | |
12878 | * st : supertype information | |
12879 | * | |
12880 | * Returns: | |
12881 | * 0 : success or device w/o RWH_BITMAP | |
12882 | * -1 : fail | |
12883 | ******************************************************************************/ | |
12884 | static int validate_internal_bitmap_imsm(struct supertype *st) | |
12885 | { | |
12886 | struct intel_super *super = st->sb; | |
12887 | struct imsm_dev *dev = get_imsm_dev(super, super->current_vol); | |
12888 | unsigned long long offset; | |
12889 | struct dl *d; | |
12890 | ||
fbc42556 JR |
12891 | if (dev->rwh_policy != RWH_BITMAP) |
12892 | return 0; | |
12893 | ||
12894 | offset = get_bitmap_header_sector(super, super->current_vol); | |
12895 | for (d = super->disks; d; d = d->next) { | |
12896 | if (d->index < 0 || is_failed(&d->disk)) | |
12897 | continue; | |
12898 | ||
12899 | if (validate_internal_bitmap_for_drive(st, offset, d)) { | |
12900 | pr_err("imsm: bitmap validation failed\n"); | |
12901 | return -1; | |
12902 | } | |
12903 | } | |
12904 | return 0; | |
12905 | } | |
12906 | ||
12907 | /******************************************************************************* | |
12908 | * Function: add_internal_bitmap_imsm | |
12909 | * Description: Mark the volume to use the bitmap and updates the chunk size value. | |
12910 | * Parameters: | |
12911 | * st : supertype information | |
12912 | * chunkp : bitmap chunk size | |
12913 | * delay : not used for imsm | |
12914 | * write_behind : not used for imsm | |
12915 | * size : not used for imsm | |
12916 | * may_change : not used for imsm | |
12917 | * amajor : not used for imsm | |
12918 | * | |
12919 | * Returns: | |
12920 | * 0 : success | |
12921 | * -1 : fail | |
12922 | ******************************************************************************/ | |
12923 | static int add_internal_bitmap_imsm(struct supertype *st, int *chunkp, | |
12924 | int delay, int write_behind, | |
12925 | unsigned long long size, int may_change, | |
12926 | int amajor) | |
12927 | { | |
12928 | struct intel_super *super = st->sb; | |
12929 | int vol_idx = super->current_vol; | |
12930 | struct imsm_dev *dev; | |
12931 | ||
12932 | if (!super->devlist || vol_idx == -1 || !chunkp) | |
12933 | return -1; | |
12934 | ||
12935 | dev = get_imsm_dev(super, vol_idx); | |
fbc42556 | 12936 | dev->rwh_policy = RWH_BITMAP; |
fbc42556 | 12937 | *chunkp = calculate_bitmap_chunksize(st, dev); |
fbc42556 JR |
12938 | return 0; |
12939 | } | |
12940 | ||
12941 | /******************************************************************************* | |
12942 | * Function: locate_bitmap_imsm | |
12943 | * Description: Seek 'fd' to start of write-intent-bitmap. | |
12944 | * Parameters: | |
12945 | * st : supertype information | |
12946 | * fd : file descriptor for the device | |
12947 | * node_num : not used for imsm | |
12948 | * | |
12949 | * Returns: | |
12950 | * 0 : success | |
12951 | * -1 : fail | |
12952 | ******************************************************************************/ | |
12953 | static int locate_bitmap_imsm(struct supertype *st, int fd, int node_num) | |
12954 | { | |
12955 | struct intel_super *super = st->sb; | |
12956 | unsigned long long offset; | |
12957 | int vol_idx = super->current_vol; | |
12958 | ||
12959 | if (!super->devlist || vol_idx == -1) | |
12960 | return -1; | |
12961 | ||
12962 | offset = get_bitmap_header_sector(super, super->current_vol); | |
12963 | dprintf("bitmap header offset is %llu\n", offset); | |
12964 | ||
12965 | lseek64(fd, offset << 9, 0); | |
12966 | ||
12967 | return 0; | |
12968 | } | |
12969 | ||
12970 | /******************************************************************************* | |
12971 | * Function: write_init_bitmap_imsm | |
12972 | * Description: Write a bitmap header and prepares the area for the bitmap. | |
12973 | * Parameters: | |
12974 | * st : supertype information | |
12975 | * fd : file descriptor for the device | |
12976 | * update : not used for imsm | |
12977 | * | |
12978 | * Returns: | |
12979 | * 0 : success | |
12980 | * -1 : fail | |
12981 | ******************************************************************************/ | |
12982 | static int write_init_bitmap_imsm(struct supertype *st, int fd, | |
12983 | enum bitmap_update update) | |
12984 | { | |
12985 | struct intel_super *super = st->sb; | |
12986 | int vol_idx = super->current_vol; | |
12987 | int ret = 0; | |
12988 | unsigned long long offset; | |
12989 | bitmap_super_t bms = { 0 }; | |
12990 | size_t written = 0; | |
12991 | size_t to_write; | |
12992 | ssize_t rv_num; | |
12993 | void *buf; | |
12994 | ||
12995 | if (!super->devlist || !super->sector_size || vol_idx == -1) | |
12996 | return -1; | |
12997 | ||
12998 | struct imsm_dev *dev = get_imsm_dev(super, vol_idx); | |
12999 | ||
13000 | /* first clear the space for bitmap header */ | |
13001 | unsigned long long bitmap_area_start = | |
13002 | get_bitmap_header_sector(super, vol_idx); | |
13003 | ||
13004 | dprintf("zeroing area start (%llu) and size (%u)\n", bitmap_area_start, | |
13005 | IMSM_BITMAP_AND_HEADER_SIZE / super->sector_size); | |
13006 | if (zero_disk_range(fd, bitmap_area_start, | |
13007 | IMSM_BITMAP_HEADER_SIZE / super->sector_size)) { | |
13008 | pr_err("imsm: cannot zeroing the space for the bitmap\n"); | |
13009 | return -1; | |
13010 | } | |
13011 | ||
13012 | /* The bitmap area should be filled with "1"s to perform initial | |
13013 | * synchronization. | |
13014 | */ | |
13015 | if (posix_memalign(&buf, MAX_SECTOR_SIZE, MAX_SECTOR_SIZE)) | |
13016 | return -1; | |
13017 | memset(buf, 0xFF, MAX_SECTOR_SIZE); | |
13018 | offset = get_bitmap_sector(super, vol_idx); | |
13019 | lseek64(fd, offset << 9, 0); | |
13020 | while (written < IMSM_BITMAP_AREA_SIZE) { | |
13021 | to_write = IMSM_BITMAP_AREA_SIZE - written; | |
13022 | if (to_write > MAX_SECTOR_SIZE) | |
13023 | to_write = MAX_SECTOR_SIZE; | |
13024 | rv_num = write(fd, buf, MAX_SECTOR_SIZE); | |
13025 | if (rv_num != MAX_SECTOR_SIZE) { | |
13026 | ret = -1; | |
13027 | dprintf("cannot initialize bitmap area\n"); | |
13028 | goto abort; | |
13029 | } | |
13030 | written += rv_num; | |
13031 | } | |
13032 | ||
13033 | /* write a bitmap header */ | |
13034 | init_bitmap_header(st, &bms, dev); | |
13035 | memset(buf, 0, MAX_SECTOR_SIZE); | |
13036 | memcpy(buf, &bms, sizeof(bitmap_super_t)); | |
13037 | if (locate_bitmap_imsm(st, fd, 0)) { | |
13038 | ret = -1; | |
13039 | dprintf("cannot locate the bitmap\n"); | |
13040 | goto abort; | |
13041 | } | |
13042 | if (write(fd, buf, MAX_SECTOR_SIZE) != MAX_SECTOR_SIZE) { | |
13043 | ret = -1; | |
13044 | dprintf("cannot write the bitmap header\n"); | |
13045 | goto abort; | |
13046 | } | |
13047 | fsync(fd); | |
13048 | ||
13049 | abort: | |
13050 | free(buf); | |
13051 | ||
13052 | return ret; | |
13053 | } | |
13054 | ||
13055 | /******************************************************************************* | |
13056 | * Function: is_vol_to_setup_bitmap | |
13057 | * Description: Checks if a bitmap should be activated on the dev. | |
13058 | * Parameters: | |
13059 | * info : info about the volume to setup the bitmap | |
13060 | * dev : the device to check against bitmap creation | |
13061 | * | |
13062 | * Returns: | |
13063 | * 0 : bitmap should be set up on the device | |
13064 | * -1 : otherwise | |
13065 | ******************************************************************************/ | |
13066 | static int is_vol_to_setup_bitmap(struct mdinfo *info, struct imsm_dev *dev) | |
13067 | { | |
13068 | if (!dev || !info) | |
13069 | return -1; | |
13070 | ||
13071 | if ((strcmp((char *)dev->volume, info->name) == 0) && | |
13072 | (dev->rwh_policy == RWH_BITMAP)) | |
13073 | return -1; | |
13074 | ||
13075 | return 0; | |
13076 | } | |
13077 | ||
13078 | /******************************************************************************* | |
13079 | * Function: set_bitmap_sysfs | |
13080 | * Description: Set the sysfs atributes of a given volume to activate the bitmap. | |
13081 | * Parameters: | |
13082 | * info : info about the volume where the bitmap should be setup | |
13083 | * chunksize : bitmap chunk size | |
13084 | * location : location of the bitmap | |
13085 | * | |
13086 | * Returns: | |
13087 | * 0 : success | |
13088 | * -1 : fail | |
13089 | ******************************************************************************/ | |
13090 | static int set_bitmap_sysfs(struct mdinfo *info, unsigned long long chunksize, | |
13091 | char *location) | |
13092 | { | |
13093 | /* The bitmap/metadata is set to external to allow changing of value for | |
13094 | * bitmap/location. When external is used, the kernel will treat an offset | |
13095 | * related to the device's first lba (in opposition to the "internal" case | |
13096 | * when this value is related to the beginning of the superblock). | |
13097 | */ | |
13098 | if (sysfs_set_str(info, NULL, "bitmap/metadata", "external")) { | |
13099 | dprintf("failed to set bitmap/metadata\n"); | |
13100 | return -1; | |
13101 | } | |
13102 | ||
13103 | /* It can only be changed when no bitmap is active. | |
13104 | * Should be bigger than 512 and must be power of 2. | |
13105 | * It is expecting the value in bytes. | |
13106 | */ | |
13107 | if (sysfs_set_num(info, NULL, "bitmap/chunksize", | |
13108 | __cpu_to_le32(chunksize))) { | |
13109 | dprintf("failed to set bitmap/chunksize\n"); | |
13110 | return -1; | |
13111 | } | |
13112 | ||
13113 | /* It is expecting the value in sectors. */ | |
13114 | if (sysfs_set_num(info, NULL, "bitmap/space", | |
13115 | __cpu_to_le64(IMSM_BITMAP_AREA_SIZE))) { | |
13116 | dprintf("failed to set bitmap/space\n"); | |
13117 | return -1; | |
13118 | } | |
13119 | ||
13120 | /* Determines the delay between the bitmap updates. | |
13121 | * It is expecting the value in seconds. | |
13122 | */ | |
13123 | if (sysfs_set_num(info, NULL, "bitmap/time_base", | |
13124 | __cpu_to_le64(IMSM_DEFAULT_BITMAP_DAEMON_SLEEP))) { | |
13125 | dprintf("failed to set bitmap/time_base\n"); | |
13126 | return -1; | |
13127 | } | |
13128 | ||
13129 | /* It is expecting the value in sectors with a sign at the beginning. */ | |
13130 | if (sysfs_set_str(info, NULL, "bitmap/location", location)) { | |
13131 | dprintf("failed to set bitmap/location\n"); | |
13132 | return -1; | |
13133 | } | |
13134 | ||
13135 | return 0; | |
13136 | } | |
13137 | ||
13138 | /******************************************************************************* | |
13139 | * Function: set_bitmap_imsm | |
13140 | * Description: Setup the bitmap for the given volume | |
13141 | * Parameters: | |
13142 | * st : supertype information | |
13143 | * info : info about the volume where the bitmap should be setup | |
13144 | * | |
13145 | * Returns: | |
13146 | * 0 : success | |
13147 | * -1 : fail | |
13148 | ******************************************************************************/ | |
13149 | static int set_bitmap_imsm(struct supertype *st, struct mdinfo *info) | |
13150 | { | |
13151 | struct intel_super *super = st->sb; | |
13152 | int prev_current_vol = super->current_vol; | |
13153 | struct imsm_dev *dev; | |
13154 | int ret = -1; | |
13155 | char location[16] = ""; | |
13156 | unsigned long long chunksize; | |
13157 | struct intel_dev *dev_it; | |
13158 | ||
13159 | for (dev_it = super->devlist; dev_it; dev_it = dev_it->next) { | |
13160 | super->current_vol = dev_it->index; | |
13161 | dev = get_imsm_dev(super, super->current_vol); | |
13162 | ||
13163 | if (is_vol_to_setup_bitmap(info, dev)) { | |
13164 | if (validate_internal_bitmap_imsm(st)) { | |
13165 | dprintf("bitmap header validation failed\n"); | |
13166 | goto abort; | |
13167 | } | |
13168 | ||
13169 | chunksize = calculate_bitmap_chunksize(st, dev); | |
13170 | dprintf("chunk size is %llu\n", chunksize); | |
13171 | ||
13172 | snprintf(location, sizeof(location), "+%llu", | |
13173 | get_bitmap_sector(super, super->current_vol)); | |
13174 | dprintf("bitmap offset is %s\n", location); | |
13175 | ||
13176 | if (set_bitmap_sysfs(info, chunksize, location)) { | |
13177 | dprintf("cannot setup the bitmap\n"); | |
13178 | goto abort; | |
13179 | } | |
13180 | } | |
13181 | } | |
13182 | ret = 0; | |
13183 | abort: | |
13184 | super->current_vol = prev_current_vol; | |
13185 | return ret; | |
13186 | } | |
13187 | ||
cdddbdbc | 13188 | struct superswitch super_imsm = { |
cdddbdbc DW |
13189 | .examine_super = examine_super_imsm, |
13190 | .brief_examine_super = brief_examine_super_imsm, | |
4737ae25 | 13191 | .brief_examine_subarrays = brief_examine_subarrays_imsm, |
9d84c8ea | 13192 | .export_examine_super = export_examine_super_imsm, |
cdddbdbc DW |
13193 | .detail_super = detail_super_imsm, |
13194 | .brief_detail_super = brief_detail_super_imsm, | |
bf5a934a | 13195 | .write_init_super = write_init_super_imsm, |
0e600426 N |
13196 | .validate_geometry = validate_geometry_imsm, |
13197 | .add_to_super = add_to_super_imsm, | |
1a64be56 | 13198 | .remove_from_super = remove_from_super_imsm, |
d665cc31 | 13199 | .detail_platform = detail_platform_imsm, |
e50cf220 | 13200 | .export_detail_platform = export_detail_platform_imsm, |
33414a01 | 13201 | .kill_subarray = kill_subarray_imsm, |
aa534678 | 13202 | .update_subarray = update_subarray_imsm, |
2b959fbf | 13203 | .load_container = load_container_imsm, |
71204a50 | 13204 | .default_geometry = default_geometry_imsm, |
e21aea08 | 13205 | .test_and_add_drive_policies = test_and_add_drive_policies_imsm, |
71204a50 N |
13206 | .reshape_super = imsm_reshape_super, |
13207 | .manage_reshape = imsm_manage_reshape, | |
9e2d750d | 13208 | .recover_backup = recover_backup_imsm, |
27156a57 | 13209 | .examine_badblocks = examine_badblocks_imsm, |
cdddbdbc DW |
13210 | .match_home = match_home_imsm, |
13211 | .uuid_from_super= uuid_from_super_imsm, | |
13212 | .getinfo_super = getinfo_super_imsm, | |
5c4cd5da | 13213 | .getinfo_super_disks = getinfo_super_disks_imsm, |
cdddbdbc DW |
13214 | .update_super = update_super_imsm, |
13215 | ||
13216 | .avail_size = avail_size_imsm, | |
fbfdcb06 | 13217 | .get_spare_criteria = get_spare_criteria_imsm, |
cdddbdbc DW |
13218 | |
13219 | .compare_super = compare_super_imsm, | |
13220 | ||
13221 | .load_super = load_super_imsm, | |
bf5a934a | 13222 | .init_super = init_super_imsm, |
e683ca88 | 13223 | .store_super = store_super_imsm, |
cdddbdbc DW |
13224 | .free_super = free_super_imsm, |
13225 | .match_metadata_desc = match_metadata_desc_imsm, | |
bf5a934a | 13226 | .container_content = container_content_imsm, |
0c21b485 | 13227 | .validate_container = validate_container_imsm, |
cdddbdbc | 13228 | |
fbc42556 JR |
13229 | .add_internal_bitmap = add_internal_bitmap_imsm, |
13230 | .locate_bitmap = locate_bitmap_imsm, | |
13231 | .write_bitmap = write_init_bitmap_imsm, | |
13232 | .set_bitmap = set_bitmap_imsm, | |
13233 | ||
2432ce9b AP |
13234 | .write_init_ppl = write_init_ppl_imsm, |
13235 | .validate_ppl = validate_ppl_imsm, | |
13236 | ||
cdddbdbc | 13237 | .external = 1, |
1c832795 | 13238 | .swapuuid = 0, |
4cce4069 | 13239 | .name = "imsm", |
845dea95 NB |
13240 | |
13241 | /* for mdmon */ | |
13242 | .open_new = imsm_open_new, | |
ed9d66aa | 13243 | .set_array_state= imsm_set_array_state, |
845dea95 NB |
13244 | .set_disk = imsm_set_disk, |
13245 | .sync_metadata = imsm_sync_metadata, | |
88758e9d | 13246 | .activate_spare = imsm_activate_spare, |
e8319a19 | 13247 | .process_update = imsm_process_update, |
8273f55e | 13248 | .prepare_update = imsm_prepare_update, |
6f50473f | 13249 | .record_bad_block = imsm_record_badblock, |
c07a5a4f | 13250 | .clear_bad_block = imsm_clear_badblock, |
928f1424 | 13251 | .get_bad_blocks = imsm_get_badblocks, |
cdddbdbc | 13252 | }; |