]>
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" |
51006d85 | 23 | #include "sha1.h" |
88c32bb1 | 24 | #include "platform-intel.h" |
cdddbdbc DW |
25 | #include <values.h> |
26 | #include <scsi/sg.h> | |
27 | #include <ctype.h> | |
d665cc31 | 28 | #include <dirent.h> |
cdddbdbc DW |
29 | |
30 | /* MPB == Metadata Parameter Block */ | |
31 | #define MPB_SIGNATURE "Intel Raid ISM Cfg Sig. " | |
32 | #define MPB_SIG_LEN (strlen(MPB_SIGNATURE)) | |
33 | #define MPB_VERSION_RAID0 "1.0.00" | |
34 | #define MPB_VERSION_RAID1 "1.1.00" | |
fe7ed8cb DW |
35 | #define MPB_VERSION_MANY_VOLUMES_PER_ARRAY "1.2.00" |
36 | #define MPB_VERSION_3OR4_DISK_ARRAY "1.2.01" | |
cdddbdbc | 37 | #define MPB_VERSION_RAID5 "1.2.02" |
fe7ed8cb DW |
38 | #define MPB_VERSION_5OR6_DISK_ARRAY "1.2.04" |
39 | #define MPB_VERSION_CNG "1.2.06" | |
40 | #define MPB_VERSION_ATTRIBS "1.3.00" | |
cdddbdbc DW |
41 | #define MAX_SIGNATURE_LENGTH 32 |
42 | #define MAX_RAID_SERIAL_LEN 16 | |
fe7ed8cb | 43 | |
19482bcc AK |
44 | /* supports RAID0 */ |
45 | #define MPB_ATTRIB_RAID0 __cpu_to_le32(0x00000001) | |
46 | /* supports RAID1 */ | |
47 | #define MPB_ATTRIB_RAID1 __cpu_to_le32(0x00000002) | |
48 | /* supports RAID10 */ | |
49 | #define MPB_ATTRIB_RAID10 __cpu_to_le32(0x00000004) | |
50 | /* supports RAID1E */ | |
51 | #define MPB_ATTRIB_RAID1E __cpu_to_le32(0x00000008) | |
52 | /* supports RAID5 */ | |
53 | #define MPB_ATTRIB_RAID5 __cpu_to_le32(0x00000010) | |
54 | /* supports RAID CNG */ | |
55 | #define MPB_ATTRIB_RAIDCNG __cpu_to_le32(0x00000020) | |
56 | /* supports expanded stripe sizes of 256K, 512K and 1MB */ | |
57 | #define MPB_ATTRIB_EXP_STRIPE_SIZE __cpu_to_le32(0x00000040) | |
58 | ||
59 | /* The OROM Support RST Caching of Volumes */ | |
60 | #define MPB_ATTRIB_NVM __cpu_to_le32(0x02000000) | |
61 | /* The OROM supports creating disks greater than 2TB */ | |
62 | #define MPB_ATTRIB_2TB_DISK __cpu_to_le32(0x04000000) | |
63 | /* The OROM supports Bad Block Management */ | |
64 | #define MPB_ATTRIB_BBM __cpu_to_le32(0x08000000) | |
65 | ||
66 | /* THe OROM Supports NVM Caching of Volumes */ | |
67 | #define MPB_ATTRIB_NEVER_USE2 __cpu_to_le32(0x10000000) | |
68 | /* The OROM supports creating volumes greater than 2TB */ | |
69 | #define MPB_ATTRIB_2TB __cpu_to_le32(0x20000000) | |
70 | /* originally for PMP, now it's wasted b/c. Never use this bit! */ | |
71 | #define MPB_ATTRIB_NEVER_USE __cpu_to_le32(0x40000000) | |
72 | /* Verify MPB contents against checksum after reading MPB */ | |
73 | #define MPB_ATTRIB_CHECKSUM_VERIFY __cpu_to_le32(0x80000000) | |
74 | ||
75 | /* Define all supported attributes that have to be accepted by mdadm | |
76 | */ | |
418f9b36 | 77 | #define MPB_ATTRIB_SUPPORTED (MPB_ATTRIB_CHECKSUM_VERIFY | \ |
19482bcc AK |
78 | MPB_ATTRIB_2TB | \ |
79 | MPB_ATTRIB_2TB_DISK | \ | |
80 | MPB_ATTRIB_RAID0 | \ | |
81 | MPB_ATTRIB_RAID1 | \ | |
82 | MPB_ATTRIB_RAID10 | \ | |
83 | MPB_ATTRIB_RAID5 | \ | |
bbab0940 TM |
84 | MPB_ATTRIB_EXP_STRIPE_SIZE | \ |
85 | MPB_ATTRIB_BBM) | |
418f9b36 N |
86 | |
87 | /* Define attributes that are unused but not harmful */ | |
88 | #define MPB_ATTRIB_IGNORED (MPB_ATTRIB_NEVER_USE) | |
fe7ed8cb | 89 | |
8e59f3d8 | 90 | #define MPB_SECTOR_CNT 2210 |
611d9529 MD |
91 | #define IMSM_RESERVED_SECTORS 8192 |
92 | #define NUM_BLOCKS_DIRTY_STRIPE_REGION 2048 | |
979d38be | 93 | #define SECT_PER_MB_SHIFT 11 |
f36a9ecd | 94 | #define MAX_SECTOR_SIZE 4096 |
c2462068 PB |
95 | #define MULTIPLE_PPL_AREA_SIZE_IMSM (1024 * 1024) /* Size of the whole |
96 | * mutliple PPL area | |
97 | */ | |
cdddbdbc | 98 | |
fbc42556 JR |
99 | /* |
100 | * Internal Write-intent bitmap is stored in the same area where PPL. | |
101 | * Both features are mutually exclusive, so it is not an issue. | |
102 | * The first 8KiB of the area are reserved and shall not be used. | |
103 | */ | |
104 | #define IMSM_BITMAP_AREA_RESERVED_SIZE 8192 | |
105 | ||
106 | #define IMSM_BITMAP_HEADER_OFFSET (IMSM_BITMAP_AREA_RESERVED_SIZE) | |
107 | #define IMSM_BITMAP_HEADER_SIZE MAX_SECTOR_SIZE | |
108 | ||
109 | #define IMSM_BITMAP_START_OFFSET (IMSM_BITMAP_HEADER_OFFSET + IMSM_BITMAP_HEADER_SIZE) | |
110 | #define IMSM_BITMAP_AREA_SIZE (MULTIPLE_PPL_AREA_SIZE_IMSM - IMSM_BITMAP_START_OFFSET) | |
111 | #define IMSM_BITMAP_AND_HEADER_SIZE (IMSM_BITMAP_AREA_SIZE + IMSM_BITMAP_HEADER_SIZE) | |
112 | ||
113 | #define IMSM_DEFAULT_BITMAP_CHUNKSIZE (64 * 1024 * 1024) | |
114 | #define IMSM_DEFAULT_BITMAP_DAEMON_SLEEP 5 | |
115 | ||
761e3bd9 N |
116 | /* |
117 | * This macro let's us ensure that no-one accidentally | |
118 | * changes the size of a struct | |
119 | */ | |
120 | #define ASSERT_SIZE(_struct, size) \ | |
121 | static inline void __assert_size_##_struct(void) \ | |
122 | { \ | |
123 | switch (0) { \ | |
124 | case 0: break; \ | |
125 | case (sizeof(struct _struct) == size): break; \ | |
126 | } \ | |
127 | } | |
128 | ||
cdddbdbc DW |
129 | /* Disk configuration info. */ |
130 | #define IMSM_MAX_DEVICES 255 | |
131 | struct imsm_disk { | |
132 | __u8 serial[MAX_RAID_SERIAL_LEN];/* 0xD8 - 0xE7 ascii serial number */ | |
5551b113 | 133 | __u32 total_blocks_lo; /* 0xE8 - 0xEB total blocks lo */ |
cdddbdbc | 134 | __u32 scsi_id; /* 0xEC - 0xEF scsi ID */ |
f2f27e63 DW |
135 | #define SPARE_DISK __cpu_to_le32(0x01) /* Spare */ |
136 | #define CONFIGURED_DISK __cpu_to_le32(0x02) /* Member of some RaidDev */ | |
137 | #define FAILED_DISK __cpu_to_le32(0x04) /* Permanent failure */ | |
2432ce9b | 138 | #define JOURNAL_DISK __cpu_to_le32(0x2000000) /* Device marked as Journaling Drive */ |
cdddbdbc | 139 | __u32 status; /* 0xF0 - 0xF3 */ |
1011e834 | 140 | __u32 owner_cfg_num; /* which config 0,1,2... owns this disk */ |
5551b113 CA |
141 | __u32 total_blocks_hi; /* 0xF4 - 0xF5 total blocks hi */ |
142 | #define IMSM_DISK_FILLERS 3 | |
143 | __u32 filler[IMSM_DISK_FILLERS]; /* 0xF5 - 0x107 MPB_DISK_FILLERS for future expansion */ | |
cdddbdbc | 144 | }; |
761e3bd9 | 145 | ASSERT_SIZE(imsm_disk, 48) |
cdddbdbc | 146 | |
3b451610 AK |
147 | /* map selector for map managment |
148 | */ | |
238c0a71 AK |
149 | #define MAP_0 0 |
150 | #define MAP_1 1 | |
151 | #define MAP_X -1 | |
3b451610 | 152 | |
cdddbdbc DW |
153 | /* RAID map configuration infos. */ |
154 | struct imsm_map { | |
5551b113 CA |
155 | __u32 pba_of_lba0_lo; /* start address of partition */ |
156 | __u32 blocks_per_member_lo;/* blocks per member */ | |
157 | __u32 num_data_stripes_lo; /* number of data stripes */ | |
cdddbdbc DW |
158 | __u16 blocks_per_strip; |
159 | __u8 map_state; /* Normal, Uninitialized, Degraded, Failed */ | |
160 | #define IMSM_T_STATE_NORMAL 0 | |
161 | #define IMSM_T_STATE_UNINITIALIZED 1 | |
e3bba0e0 DW |
162 | #define IMSM_T_STATE_DEGRADED 2 |
163 | #define IMSM_T_STATE_FAILED 3 | |
cdddbdbc DW |
164 | __u8 raid_level; |
165 | #define IMSM_T_RAID0 0 | |
166 | #define IMSM_T_RAID1 1 | |
167 | #define IMSM_T_RAID5 5 /* since metadata version 1.2.02 ? */ | |
168 | __u8 num_members; /* number of member disks */ | |
fe7ed8cb DW |
169 | __u8 num_domains; /* number of parity domains */ |
170 | __u8 failed_disk_num; /* valid only when state is degraded */ | |
252d23c0 | 171 | __u8 ddf; |
5551b113 CA |
172 | __u32 pba_of_lba0_hi; |
173 | __u32 blocks_per_member_hi; | |
174 | __u32 num_data_stripes_hi; | |
175 | __u32 filler[4]; /* expansion area */ | |
7eef0453 | 176 | #define IMSM_ORD_REBUILD (1 << 24) |
cdddbdbc | 177 | __u32 disk_ord_tbl[1]; /* disk_ord_tbl[num_members], |
7eef0453 DW |
178 | * top byte contains some flags |
179 | */ | |
761e3bd9 N |
180 | }; |
181 | ASSERT_SIZE(imsm_map, 52) | |
cdddbdbc DW |
182 | |
183 | struct imsm_vol { | |
4036e7ee | 184 | __u32 curr_migr_unit_lo; |
fe7ed8cb | 185 | __u32 checkpoint_id; /* id to access curr_migr_unit */ |
cdddbdbc | 186 | __u8 migr_state; /* Normal or Migrating */ |
e3bba0e0 DW |
187 | #define MIGR_INIT 0 |
188 | #define MIGR_REBUILD 1 | |
189 | #define MIGR_VERIFY 2 /* analagous to echo check > sync_action */ | |
190 | #define MIGR_GEN_MIGR 3 | |
191 | #define MIGR_STATE_CHANGE 4 | |
1484e727 | 192 | #define MIGR_REPAIR 5 |
cdddbdbc | 193 | __u8 migr_type; /* Initializing, Rebuilding, ... */ |
2432ce9b AP |
194 | #define RAIDVOL_CLEAN 0 |
195 | #define RAIDVOL_DIRTY 1 | |
196 | #define RAIDVOL_DSRECORD_VALID 2 | |
cdddbdbc | 197 | __u8 dirty; |
fe7ed8cb DW |
198 | __u8 fs_state; /* fast-sync state for CnG (0xff == disabled) */ |
199 | __u16 verify_errors; /* number of mismatches */ | |
200 | __u16 bad_blocks; /* number of bad blocks during verify */ | |
4036e7ee MT |
201 | __u32 curr_migr_unit_hi; |
202 | __u32 filler[3]; | |
cdddbdbc DW |
203 | struct imsm_map map[1]; |
204 | /* here comes another one if migr_state */ | |
761e3bd9 N |
205 | }; |
206 | ASSERT_SIZE(imsm_vol, 84) | |
cdddbdbc DW |
207 | |
208 | struct imsm_dev { | |
fe7ed8cb | 209 | __u8 volume[MAX_RAID_SERIAL_LEN]; |
cdddbdbc DW |
210 | __u32 size_low; |
211 | __u32 size_high; | |
fe7ed8cb DW |
212 | #define DEV_BOOTABLE __cpu_to_le32(0x01) |
213 | #define DEV_BOOT_DEVICE __cpu_to_le32(0x02) | |
214 | #define DEV_READ_COALESCING __cpu_to_le32(0x04) | |
215 | #define DEV_WRITE_COALESCING __cpu_to_le32(0x08) | |
216 | #define DEV_LAST_SHUTDOWN_DIRTY __cpu_to_le32(0x10) | |
217 | #define DEV_HIDDEN_AT_BOOT __cpu_to_le32(0x20) | |
218 | #define DEV_CURRENTLY_HIDDEN __cpu_to_le32(0x40) | |
219 | #define DEV_VERIFY_AND_FIX __cpu_to_le32(0x80) | |
220 | #define DEV_MAP_STATE_UNINIT __cpu_to_le32(0x100) | |
221 | #define DEV_NO_AUTO_RECOVERY __cpu_to_le32(0x200) | |
222 | #define DEV_CLONE_N_GO __cpu_to_le32(0x400) | |
223 | #define DEV_CLONE_MAN_SYNC __cpu_to_le32(0x800) | |
224 | #define DEV_CNG_MASTER_DISK_NUM __cpu_to_le32(0x1000) | |
cdddbdbc DW |
225 | __u32 status; /* Persistent RaidDev status */ |
226 | __u32 reserved_blocks; /* Reserved blocks at beginning of volume */ | |
fe7ed8cb DW |
227 | __u8 migr_priority; |
228 | __u8 num_sub_vols; | |
229 | __u8 tid; | |
230 | __u8 cng_master_disk; | |
231 | __u16 cache_policy; | |
232 | __u8 cng_state; | |
233 | __u8 cng_sub_state; | |
2432ce9b AP |
234 | __u16 my_vol_raid_dev_num; /* Used in Unique volume Id for this RaidDev */ |
235 | ||
236 | /* NVM_EN */ | |
237 | __u8 nv_cache_mode; | |
238 | __u8 nv_cache_flags; | |
239 | ||
240 | /* Unique Volume Id of the NvCache Volume associated with this volume */ | |
241 | __u32 nvc_vol_orig_family_num; | |
242 | __u16 nvc_vol_raid_dev_num; | |
243 | ||
244 | #define RWH_OFF 0 | |
245 | #define RWH_DISTRIBUTED 1 | |
246 | #define RWH_JOURNALING_DRIVE 2 | |
c2462068 PB |
247 | #define RWH_MULTIPLE_DISTRIBUTED 3 |
248 | #define RWH_MULTIPLE_PPLS_JOURNALING_DRIVE 4 | |
249 | #define RWH_MULTIPLE_OFF 5 | |
fbc42556 | 250 | #define RWH_BITMAP 6 |
2432ce9b AP |
251 | __u8 rwh_policy; /* Raid Write Hole Policy */ |
252 | __u8 jd_serial[MAX_RAID_SERIAL_LEN]; /* Journal Drive serial number */ | |
253 | __u8 filler1; | |
254 | ||
255 | #define IMSM_DEV_FILLERS 3 | |
cdddbdbc DW |
256 | __u32 filler[IMSM_DEV_FILLERS]; |
257 | struct imsm_vol vol; | |
761e3bd9 N |
258 | }; |
259 | ASSERT_SIZE(imsm_dev, 164) | |
cdddbdbc DW |
260 | |
261 | struct imsm_super { | |
262 | __u8 sig[MAX_SIGNATURE_LENGTH]; /* 0x00 - 0x1F */ | |
263 | __u32 check_sum; /* 0x20 - 0x23 MPB Checksum */ | |
264 | __u32 mpb_size; /* 0x24 - 0x27 Size of MPB */ | |
265 | __u32 family_num; /* 0x28 - 0x2B Checksum from first time this config was written */ | |
266 | __u32 generation_num; /* 0x2C - 0x2F Incremented each time this array's MPB is written */ | |
604b746f JD |
267 | __u32 error_log_size; /* 0x30 - 0x33 in bytes */ |
268 | __u32 attributes; /* 0x34 - 0x37 */ | |
cdddbdbc DW |
269 | __u8 num_disks; /* 0x38 Number of configured disks */ |
270 | __u8 num_raid_devs; /* 0x39 Number of configured volumes */ | |
604b746f JD |
271 | __u8 error_log_pos; /* 0x3A */ |
272 | __u8 fill[1]; /* 0x3B */ | |
273 | __u32 cache_size; /* 0x3c - 0x40 in mb */ | |
274 | __u32 orig_family_num; /* 0x40 - 0x43 original family num */ | |
275 | __u32 pwr_cycle_count; /* 0x44 - 0x47 simulated power cycle count for array */ | |
276 | __u32 bbm_log_size; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */ | |
2a24dc1b PB |
277 | __u16 num_raid_devs_created; /* 0x4C - 0x4D Used for generating unique |
278 | * volume IDs for raid_dev created in this array | |
279 | * (starts at 1) | |
280 | */ | |
281 | __u16 filler1; /* 0x4E - 0x4F */ | |
e48aed3c AP |
282 | __u64 creation_time; /* 0x50 - 0x57 Array creation time */ |
283 | #define IMSM_FILLERS 32 | |
284 | __u32 filler[IMSM_FILLERS]; /* 0x58 - 0xD7 RAID_MPB_FILLERS */ | |
cdddbdbc DW |
285 | struct imsm_disk disk[1]; /* 0xD8 diskTbl[numDisks] */ |
286 | /* here comes imsm_dev[num_raid_devs] */ | |
604b746f | 287 | /* here comes BBM logs */ |
761e3bd9 N |
288 | }; |
289 | ASSERT_SIZE(imsm_super, 264) | |
cdddbdbc | 290 | |
604b746f | 291 | #define BBM_LOG_MAX_ENTRIES 254 |
8d67477f TM |
292 | #define BBM_LOG_MAX_LBA_ENTRY_VAL 256 /* Represents 256 LBAs */ |
293 | #define BBM_LOG_SIGNATURE 0xabadb10c | |
294 | ||
295 | struct bbm_log_block_addr { | |
296 | __u16 w1; | |
297 | __u32 dw1; | |
298 | } __attribute__ ((__packed__)); | |
604b746f JD |
299 | |
300 | struct bbm_log_entry { | |
8d67477f TM |
301 | __u8 marked_count; /* Number of blocks marked - 1 */ |
302 | __u8 disk_ordinal; /* Disk entry within the imsm_super */ | |
303 | struct bbm_log_block_addr defective_block_start; | |
604b746f JD |
304 | } __attribute__ ((__packed__)); |
305 | ||
306 | struct bbm_log { | |
307 | __u32 signature; /* 0xABADB10C */ | |
308 | __u32 entry_count; | |
8d67477f | 309 | struct bbm_log_entry marked_block_entries[BBM_LOG_MAX_ENTRIES]; |
761e3bd9 N |
310 | }; |
311 | ASSERT_SIZE(bbm_log, 2040) | |
604b746f | 312 | |
cdddbdbc | 313 | static char *map_state_str[] = { "normal", "uninitialized", "degraded", "failed" }; |
cdddbdbc | 314 | |
b53bfba6 TM |
315 | #define BLOCKS_PER_KB (1024/512) |
316 | ||
8e59f3d8 AK |
317 | #define RAID_DISK_RESERVED_BLOCKS_IMSM_HI 2209 |
318 | ||
319 | #define GEN_MIGR_AREA_SIZE 2048 /* General Migration Copy Area size in blocks */ | |
320 | ||
de44e46f PB |
321 | #define MIGR_REC_BUF_SECTORS 1 /* size of migr_record i/o buffer in sectors */ |
322 | #define MIGR_REC_SECTOR_POSITION 1 /* migr_record position offset on disk, | |
323 | * MIGR_REC_BUF_SECTORS <= MIGR_REC_SECTOR_POS | |
17a4eaf9 AK |
324 | */ |
325 | ||
8e59f3d8 AK |
326 | #define UNIT_SRC_NORMAL 0 /* Source data for curr_migr_unit must |
327 | * be recovered using srcMap */ | |
328 | #define UNIT_SRC_IN_CP_AREA 1 /* Source data for curr_migr_unit has | |
329 | * already been migrated and must | |
330 | * be recovered from checkpoint area */ | |
2432ce9b | 331 | |
c2462068 | 332 | #define PPL_ENTRY_SPACE (128 * 1024) /* Size of single PPL, without the header */ |
2432ce9b | 333 | |
8e59f3d8 AK |
334 | struct migr_record { |
335 | __u32 rec_status; /* Status used to determine how to restart | |
336 | * migration in case it aborts | |
337 | * in some fashion */ | |
9f421827 | 338 | __u32 curr_migr_unit_lo; /* 0..numMigrUnits-1 */ |
8e59f3d8 AK |
339 | __u32 family_num; /* Family number of MPB |
340 | * containing the RaidDev | |
341 | * that is migrating */ | |
342 | __u32 ascending_migr; /* True if migrating in increasing | |
343 | * order of lbas */ | |
344 | __u32 blocks_per_unit; /* Num disk blocks per unit of operation */ | |
345 | __u32 dest_depth_per_unit; /* Num member blocks each destMap | |
346 | * member disk | |
347 | * advances per unit-of-operation */ | |
9f421827 PB |
348 | __u32 ckpt_area_pba_lo; /* Pba of first block of ckpt copy area */ |
349 | __u32 dest_1st_member_lba_lo; /* First member lba on first | |
350 | * stripe of destination */ | |
351 | __u32 num_migr_units_lo; /* Total num migration units-of-op */ | |
8e59f3d8 AK |
352 | __u32 post_migr_vol_cap; /* Size of volume after |
353 | * migration completes */ | |
354 | __u32 post_migr_vol_cap_hi; /* Expansion space for LBA64 */ | |
355 | __u32 ckpt_read_disk_num; /* Which member disk in destSubMap[0] the | |
356 | * migration ckpt record was read from | |
357 | * (for recovered migrations) */ | |
9f421827 PB |
358 | __u32 curr_migr_unit_hi; /* 0..numMigrUnits-1 high order 32 bits */ |
359 | __u32 ckpt_area_pba_hi; /* Pba of first block of ckpt copy area | |
360 | * high order 32 bits */ | |
361 | __u32 dest_1st_member_lba_hi; /* First member lba on first stripe of | |
362 | * destination - high order 32 bits */ | |
363 | __u32 num_migr_units_hi; /* Total num migration units-of-op | |
364 | * high order 32 bits */ | |
4036e7ee | 365 | __u32 filler[16]; |
761e3bd9 | 366 | }; |
4036e7ee | 367 | ASSERT_SIZE(migr_record, 128) |
8e59f3d8 | 368 | |
76c152ca MT |
369 | /** |
370 | * enum imsm_status - internal IMSM return values representation. | |
371 | * @STATUS_OK: function succeeded. | |
372 | * @STATUS_ERROR: General error ocurred (not specified). | |
373 | * | |
374 | * Typedefed to imsm_status_t. | |
375 | */ | |
376 | typedef enum imsm_status { | |
377 | IMSM_STATUS_ERROR = -1, | |
378 | IMSM_STATUS_OK = 0, | |
379 | } imsm_status_t; | |
380 | ||
ec50f7b6 LM |
381 | struct md_list { |
382 | /* usage marker: | |
383 | * 1: load metadata | |
384 | * 2: metadata does not match | |
385 | * 4: already checked | |
386 | */ | |
387 | int used; | |
388 | char *devname; | |
389 | int found; | |
390 | int container; | |
391 | dev_t st_rdev; | |
392 | struct md_list *next; | |
393 | }; | |
394 | ||
e7b84f9d | 395 | #define pr_vrb(fmt, arg...) (void) (verbose && pr_err(fmt, ##arg)) |
ec50f7b6 | 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 | ||
c2c087e6 DW |
501 | struct extent { |
502 | unsigned long long start, size; | |
503 | }; | |
504 | ||
694575e7 KW |
505 | /* definitions of reshape process types */ |
506 | enum imsm_reshape_type { | |
507 | CH_TAKEOVER, | |
b5347799 | 508 | CH_MIGRATION, |
7abc9871 | 509 | CH_ARRAY_SIZE, |
694575e7 KW |
510 | }; |
511 | ||
88758e9d DW |
512 | /* definition of messages passed to imsm_process_update */ |
513 | enum imsm_update_type { | |
514 | update_activate_spare, | |
8273f55e | 515 | update_create_array, |
33414a01 | 516 | update_kill_array, |
aa534678 | 517 | update_rename_array, |
1a64be56 | 518 | update_add_remove_disk, |
78b10e66 | 519 | update_reshape_container_disks, |
48c5303a | 520 | update_reshape_migration, |
2d40f3a1 AK |
521 | update_takeover, |
522 | update_general_migration_checkpoint, | |
f3871fdc | 523 | update_size_change, |
bbab0940 | 524 | update_prealloc_badblocks_mem, |
e6e9dd3f | 525 | update_rwh_policy, |
88758e9d DW |
526 | }; |
527 | ||
528 | struct imsm_update_activate_spare { | |
529 | enum imsm_update_type type; | |
d23fe947 | 530 | struct dl *dl; |
88758e9d DW |
531 | int slot; |
532 | int array; | |
533 | struct imsm_update_activate_spare *next; | |
534 | }; | |
535 | ||
78b10e66 | 536 | struct geo_params { |
4dd2df09 | 537 | char devnm[32]; |
78b10e66 | 538 | char *dev_name; |
d04f65f4 | 539 | unsigned long long size; |
78b10e66 N |
540 | int level; |
541 | int layout; | |
542 | int chunksize; | |
543 | int raid_disks; | |
544 | }; | |
545 | ||
bb025c2f KW |
546 | enum takeover_direction { |
547 | R10_TO_R0, | |
548 | R0_TO_R10 | |
549 | }; | |
550 | struct imsm_update_takeover { | |
551 | enum imsm_update_type type; | |
552 | int subarray; | |
553 | enum takeover_direction direction; | |
554 | }; | |
78b10e66 N |
555 | |
556 | struct imsm_update_reshape { | |
557 | enum imsm_update_type type; | |
558 | int old_raid_disks; | |
559 | int new_raid_disks; | |
48c5303a PC |
560 | |
561 | int new_disks[1]; /* new_raid_disks - old_raid_disks makedev number */ | |
562 | }; | |
563 | ||
564 | struct imsm_update_reshape_migration { | |
565 | enum imsm_update_type type; | |
566 | int old_raid_disks; | |
567 | int new_raid_disks; | |
568 | /* fields for array migration changes | |
569 | */ | |
570 | int subdev; | |
571 | int new_level; | |
572 | int new_layout; | |
4bba0439 | 573 | int new_chunksize; |
48c5303a | 574 | |
d195167d | 575 | int new_disks[1]; /* new_raid_disks - old_raid_disks makedev number */ |
78b10e66 N |
576 | }; |
577 | ||
f3871fdc AK |
578 | struct imsm_update_size_change { |
579 | enum imsm_update_type type; | |
580 | int subdev; | |
581 | long long new_size; | |
582 | }; | |
583 | ||
2d40f3a1 AK |
584 | struct imsm_update_general_migration_checkpoint { |
585 | enum imsm_update_type type; | |
4036e7ee | 586 | __u64 curr_migr_unit; |
2d40f3a1 AK |
587 | }; |
588 | ||
54c2c1ea DW |
589 | struct disk_info { |
590 | __u8 serial[MAX_RAID_SERIAL_LEN]; | |
591 | }; | |
592 | ||
8273f55e DW |
593 | struct imsm_update_create_array { |
594 | enum imsm_update_type type; | |
8273f55e | 595 | int dev_idx; |
6a3e913e | 596 | struct imsm_dev dev; |
8273f55e DW |
597 | }; |
598 | ||
33414a01 DW |
599 | struct imsm_update_kill_array { |
600 | enum imsm_update_type type; | |
601 | int dev_idx; | |
602 | }; | |
603 | ||
aa534678 DW |
604 | struct imsm_update_rename_array { |
605 | enum imsm_update_type type; | |
606 | __u8 name[MAX_RAID_SERIAL_LEN]; | |
607 | int dev_idx; | |
608 | }; | |
609 | ||
1a64be56 | 610 | struct imsm_update_add_remove_disk { |
43dad3d6 DW |
611 | enum imsm_update_type type; |
612 | }; | |
613 | ||
bbab0940 TM |
614 | struct imsm_update_prealloc_bb_mem { |
615 | enum imsm_update_type type; | |
616 | }; | |
617 | ||
e6e9dd3f AP |
618 | struct imsm_update_rwh_policy { |
619 | enum imsm_update_type type; | |
620 | int new_policy; | |
621 | int dev_idx; | |
622 | }; | |
623 | ||
88654014 LM |
624 | static const char *_sys_dev_type[] = { |
625 | [SYS_DEV_UNKNOWN] = "Unknown", | |
626 | [SYS_DEV_SAS] = "SAS", | |
614902f6 | 627 | [SYS_DEV_SATA] = "SATA", |
60f0f54d PB |
628 | [SYS_DEV_NVME] = "NVMe", |
629 | [SYS_DEV_VMD] = "VMD" | |
88654014 LM |
630 | }; |
631 | ||
632 | const char *get_sys_dev_type(enum sys_dev_type type) | |
633 | { | |
634 | if (type >= SYS_DEV_MAX) | |
635 | type = SYS_DEV_UNKNOWN; | |
636 | ||
637 | return _sys_dev_type[type]; | |
638 | } | |
639 | ||
640 | static struct intel_hba * alloc_intel_hba(struct sys_dev *device) | |
641 | { | |
503975b9 N |
642 | struct intel_hba *result = xmalloc(sizeof(*result)); |
643 | ||
644 | result->type = device->type; | |
645 | result->path = xstrdup(device->path); | |
646 | result->next = NULL; | |
647 | if (result->path && (result->pci_id = strrchr(result->path, '/')) != NULL) | |
648 | result->pci_id++; | |
649 | ||
88654014 LM |
650 | return result; |
651 | } | |
652 | ||
653 | static struct intel_hba * find_intel_hba(struct intel_hba *hba, struct sys_dev *device) | |
654 | { | |
594dc1b8 JS |
655 | struct intel_hba *result; |
656 | ||
88654014 LM |
657 | for (result = hba; result; result = result->next) { |
658 | if (result->type == device->type && strcmp(result->path, device->path) == 0) | |
659 | break; | |
660 | } | |
661 | return result; | |
662 | } | |
663 | ||
b4cf4cba | 664 | static int attach_hba_to_super(struct intel_super *super, struct sys_dev *device) |
88654014 LM |
665 | { |
666 | struct intel_hba *hba; | |
667 | ||
668 | /* check if disk attached to Intel HBA */ | |
669 | hba = find_intel_hba(super->hba, device); | |
670 | if (hba != NULL) | |
671 | return 1; | |
672 | /* Check if HBA is already attached to super */ | |
673 | if (super->hba == NULL) { | |
674 | super->hba = alloc_intel_hba(device); | |
675 | return 1; | |
6b781d33 AP |
676 | } |
677 | ||
678 | hba = super->hba; | |
679 | /* Intel metadata allows for all disks attached to the same type HBA. | |
614902f6 | 680 | * Do not support HBA types mixing |
6b781d33 AP |
681 | */ |
682 | if (device->type != hba->type) | |
88654014 | 683 | return 2; |
6b781d33 AP |
684 | |
685 | /* Multiple same type HBAs can be used if they share the same OROM */ | |
686 | const struct imsm_orom *device_orom = get_orom_by_device_id(device->dev_id); | |
687 | ||
688 | if (device_orom != super->orom) | |
689 | return 2; | |
690 | ||
691 | while (hba->next) | |
692 | hba = hba->next; | |
693 | ||
694 | hba->next = alloc_intel_hba(device); | |
695 | return 1; | |
88654014 LM |
696 | } |
697 | ||
698 | static struct sys_dev* find_disk_attached_hba(int fd, const char *devname) | |
699 | { | |
9bc4ae77 | 700 | struct sys_dev *list, *elem; |
88654014 LM |
701 | char *disk_path; |
702 | ||
703 | if ((list = find_intel_devices()) == NULL) | |
704 | return 0; | |
705 | ||
4389ce73 | 706 | if (!is_fd_valid(fd)) |
88654014 LM |
707 | disk_path = (char *) devname; |
708 | else | |
7c798f87 | 709 | disk_path = diskfd_to_devpath(fd, 1, NULL); |
88654014 | 710 | |
9bc4ae77 | 711 | if (!disk_path) |
88654014 | 712 | return 0; |
88654014 | 713 | |
9bc4ae77 N |
714 | for (elem = list; elem; elem = elem->next) |
715 | if (path_attached_to_hba(disk_path, elem->path)) | |
88654014 | 716 | return elem; |
9bc4ae77 | 717 | |
88654014 LM |
718 | if (disk_path != devname) |
719 | free(disk_path); | |
88654014 LM |
720 | |
721 | return NULL; | |
722 | } | |
723 | ||
d424212e N |
724 | static int find_intel_hba_capability(int fd, struct intel_super *super, |
725 | char *devname); | |
f2f5c343 | 726 | |
cdddbdbc DW |
727 | static struct supertype *match_metadata_desc_imsm(char *arg) |
728 | { | |
729 | struct supertype *st; | |
730 | ||
731 | if (strcmp(arg, "imsm") != 0 && | |
732 | strcmp(arg, "default") != 0 | |
733 | ) | |
734 | return NULL; | |
735 | ||
503975b9 | 736 | st = xcalloc(1, sizeof(*st)); |
cdddbdbc DW |
737 | st->ss = &super_imsm; |
738 | st->max_devs = IMSM_MAX_DEVICES; | |
739 | st->minor_version = 0; | |
740 | st->sb = NULL; | |
741 | return st; | |
742 | } | |
743 | ||
cdddbdbc DW |
744 | static __u8 *get_imsm_version(struct imsm_super *mpb) |
745 | { | |
746 | return &mpb->sig[MPB_SIG_LEN]; | |
747 | } | |
748 | ||
949c47a0 DW |
749 | /* retrieve a disk directly from the anchor when the anchor is known to be |
750 | * up-to-date, currently only at load time | |
751 | */ | |
752 | static struct imsm_disk *__get_imsm_disk(struct imsm_super *mpb, __u8 index) | |
cdddbdbc | 753 | { |
949c47a0 | 754 | if (index >= mpb->num_disks) |
cdddbdbc DW |
755 | return NULL; |
756 | return &mpb->disk[index]; | |
757 | } | |
758 | ||
95d07a2c LM |
759 | /* retrieve the disk description based on a index of the disk |
760 | * in the sub-array | |
761 | */ | |
762 | static struct dl *get_imsm_dl_disk(struct intel_super *super, __u8 index) | |
949c47a0 | 763 | { |
b9f594fe DW |
764 | struct dl *d; |
765 | ||
766 | for (d = super->disks; d; d = d->next) | |
767 | if (d->index == index) | |
95d07a2c LM |
768 | return d; |
769 | ||
770 | return NULL; | |
771 | } | |
772 | /* retrieve a disk from the parsed metadata */ | |
773 | static struct imsm_disk *get_imsm_disk(struct intel_super *super, __u8 index) | |
774 | { | |
775 | struct dl *dl; | |
776 | ||
777 | dl = get_imsm_dl_disk(super, index); | |
778 | if (dl) | |
779 | return &dl->disk; | |
780 | ||
b9f594fe | 781 | return NULL; |
949c47a0 DW |
782 | } |
783 | ||
784 | /* generate a checksum directly from the anchor when the anchor is known to be | |
785 | * up-to-date, currently only at load or write_super after coalescing | |
786 | */ | |
787 | static __u32 __gen_imsm_checksum(struct imsm_super *mpb) | |
cdddbdbc DW |
788 | { |
789 | __u32 end = mpb->mpb_size / sizeof(end); | |
790 | __u32 *p = (__u32 *) mpb; | |
791 | __u32 sum = 0; | |
792 | ||
5d500228 N |
793 | while (end--) { |
794 | sum += __le32_to_cpu(*p); | |
97f734fd N |
795 | p++; |
796 | } | |
cdddbdbc | 797 | |
5d500228 | 798 | return sum - __le32_to_cpu(mpb->check_sum); |
cdddbdbc DW |
799 | } |
800 | ||
a965f303 DW |
801 | static size_t sizeof_imsm_map(struct imsm_map *map) |
802 | { | |
803 | return sizeof(struct imsm_map) + sizeof(__u32) * (map->num_members - 1); | |
804 | } | |
805 | ||
806 | struct imsm_map *get_imsm_map(struct imsm_dev *dev, int second_map) | |
cdddbdbc | 807 | { |
5e7b0330 AK |
808 | /* A device can have 2 maps if it is in the middle of a migration. |
809 | * If second_map is: | |
238c0a71 AK |
810 | * MAP_0 - we return the first map |
811 | * MAP_1 - we return the second map if it exists, else NULL | |
812 | * MAP_X - we return the second map if it exists, else the first | |
5e7b0330 | 813 | */ |
a965f303 | 814 | struct imsm_map *map = &dev->vol.map[0]; |
9535fc47 | 815 | struct imsm_map *map2 = NULL; |
a965f303 | 816 | |
9535fc47 AK |
817 | if (dev->vol.migr_state) |
818 | map2 = (void *)map + sizeof_imsm_map(map); | |
a965f303 | 819 | |
9535fc47 | 820 | switch (second_map) { |
3b451610 | 821 | case MAP_0: |
9535fc47 | 822 | break; |
3b451610 | 823 | case MAP_1: |
9535fc47 AK |
824 | map = map2; |
825 | break; | |
238c0a71 | 826 | case MAP_X: |
9535fc47 AK |
827 | if (map2) |
828 | map = map2; | |
829 | break; | |
9535fc47 AK |
830 | default: |
831 | map = NULL; | |
832 | } | |
833 | return map; | |
5e7b0330 | 834 | |
a965f303 | 835 | } |
cdddbdbc | 836 | |
3393c6af DW |
837 | /* return the size of the device. |
838 | * migr_state increases the returned size if map[0] were to be duplicated | |
839 | */ | |
840 | static size_t sizeof_imsm_dev(struct imsm_dev *dev, int migr_state) | |
a965f303 DW |
841 | { |
842 | size_t size = sizeof(*dev) - sizeof(struct imsm_map) + | |
238c0a71 | 843 | sizeof_imsm_map(get_imsm_map(dev, MAP_0)); |
cdddbdbc DW |
844 | |
845 | /* migrating means an additional map */ | |
a965f303 | 846 | if (dev->vol.migr_state) |
238c0a71 | 847 | size += sizeof_imsm_map(get_imsm_map(dev, MAP_1)); |
3393c6af | 848 | else if (migr_state) |
238c0a71 | 849 | size += sizeof_imsm_map(get_imsm_map(dev, MAP_0)); |
cdddbdbc DW |
850 | |
851 | return size; | |
852 | } | |
853 | ||
54c2c1ea DW |
854 | /* retrieve disk serial number list from a metadata update */ |
855 | static struct disk_info *get_disk_info(struct imsm_update_create_array *update) | |
856 | { | |
857 | void *u = update; | |
858 | struct disk_info *inf; | |
859 | ||
860 | inf = u + sizeof(*update) - sizeof(struct imsm_dev) + | |
861 | sizeof_imsm_dev(&update->dev, 0); | |
862 | ||
863 | return inf; | |
864 | } | |
54c2c1ea | 865 | |
756a15f3 MG |
866 | /** |
867 | * __get_imsm_dev() - Get device with index from imsm_super. | |
868 | * @mpb: &imsm_super pointer, not NULL. | |
869 | * @index: Device index. | |
870 | * | |
871 | * Function works as non-NULL, aborting in such a case, | |
872 | * when NULL would be returned. | |
873 | * | |
874 | * Device index should be in range 0 up to num_raid_devs. | |
875 | * Function assumes the index was already verified. | |
876 | * Index must be valid, otherwise abort() is called. | |
877 | * | |
878 | * Return: Pointer to corresponding imsm_dev. | |
879 | * | |
880 | */ | |
949c47a0 | 881 | static struct imsm_dev *__get_imsm_dev(struct imsm_super *mpb, __u8 index) |
cdddbdbc DW |
882 | { |
883 | int offset; | |
884 | int i; | |
885 | void *_mpb = mpb; | |
886 | ||
949c47a0 | 887 | if (index >= mpb->num_raid_devs) |
756a15f3 | 888 | goto error; |
cdddbdbc DW |
889 | |
890 | /* devices start after all disks */ | |
891 | offset = ((void *) &mpb->disk[mpb->num_disks]) - _mpb; | |
892 | ||
756a15f3 | 893 | for (i = 0; i <= index; i++, offset += sizeof_imsm_dev(_mpb + offset, 0)) |
cdddbdbc DW |
894 | if (i == index) |
895 | return _mpb + offset; | |
756a15f3 MG |
896 | error: |
897 | pr_err("cannot find imsm_dev with index %u in imsm_super\n", index); | |
898 | abort(); | |
cdddbdbc DW |
899 | } |
900 | ||
756a15f3 MG |
901 | /** |
902 | * get_imsm_dev() - Get device with index from intel_super. | |
903 | * @super: &intel_super pointer, not NULL. | |
904 | * @index: Device index. | |
905 | * | |
906 | * Function works as non-NULL, aborting in such a case, | |
907 | * when NULL would be returned. | |
908 | * | |
909 | * Device index should be in range 0 up to num_raid_devs. | |
910 | * Function assumes the index was already verified. | |
911 | * Index must be valid, otherwise abort() is called. | |
912 | * | |
913 | * Return: Pointer to corresponding imsm_dev. | |
914 | * | |
915 | */ | |
949c47a0 DW |
916 | static struct imsm_dev *get_imsm_dev(struct intel_super *super, __u8 index) |
917 | { | |
ba2de7ba DW |
918 | struct intel_dev *dv; |
919 | ||
949c47a0 | 920 | if (index >= super->anchor->num_raid_devs) |
756a15f3 MG |
921 | goto error; |
922 | ||
ba2de7ba DW |
923 | for (dv = super->devlist; dv; dv = dv->next) |
924 | if (dv->index == index) | |
925 | return dv->dev; | |
756a15f3 MG |
926 | error: |
927 | pr_err("cannot find imsm_dev with index %u in intel_super\n", index); | |
928 | abort(); | |
949c47a0 DW |
929 | } |
930 | ||
8d67477f TM |
931 | static inline unsigned long long __le48_to_cpu(const struct bbm_log_block_addr |
932 | *addr) | |
933 | { | |
934 | return ((((__u64)__le32_to_cpu(addr->dw1)) << 16) | | |
935 | __le16_to_cpu(addr->w1)); | |
936 | } | |
937 | ||
938 | static inline struct bbm_log_block_addr __cpu_to_le48(unsigned long long sec) | |
939 | { | |
940 | struct bbm_log_block_addr addr; | |
941 | ||
942 | addr.w1 = __cpu_to_le16((__u16)(sec & 0xffff)); | |
943 | addr.dw1 = __cpu_to_le32((__u32)(sec >> 16) & 0xffffffff); | |
944 | return addr; | |
945 | } | |
946 | ||
8d67477f TM |
947 | /* get size of the bbm log */ |
948 | static __u32 get_imsm_bbm_log_size(struct bbm_log *log) | |
949 | { | |
950 | if (!log || log->entry_count == 0) | |
951 | return 0; | |
952 | ||
953 | return sizeof(log->signature) + | |
954 | sizeof(log->entry_count) + | |
955 | log->entry_count * sizeof(struct bbm_log_entry); | |
956 | } | |
6f50473f TM |
957 | |
958 | /* check if bad block is not partially stored in bbm log */ | |
959 | static int is_stored_in_bbm(struct bbm_log *log, const __u8 idx, const unsigned | |
960 | long long sector, const int length, __u32 *pos) | |
961 | { | |
962 | __u32 i; | |
963 | ||
964 | for (i = *pos; i < log->entry_count; i++) { | |
965 | struct bbm_log_entry *entry = &log->marked_block_entries[i]; | |
966 | unsigned long long bb_start; | |
967 | unsigned long long bb_end; | |
968 | ||
969 | bb_start = __le48_to_cpu(&entry->defective_block_start); | |
970 | bb_end = bb_start + (entry->marked_count + 1); | |
971 | ||
972 | if ((entry->disk_ordinal == idx) && (bb_start >= sector) && | |
973 | (bb_end <= sector + length)) { | |
974 | *pos = i; | |
975 | return 1; | |
976 | } | |
977 | } | |
978 | return 0; | |
979 | } | |
980 | ||
981 | /* record new bad block in bbm log */ | |
982 | static int record_new_badblock(struct bbm_log *log, const __u8 idx, unsigned | |
983 | long long sector, int length) | |
984 | { | |
985 | int new_bb = 0; | |
986 | __u32 pos = 0; | |
987 | struct bbm_log_entry *entry = NULL; | |
988 | ||
989 | while (is_stored_in_bbm(log, idx, sector, length, &pos)) { | |
990 | struct bbm_log_entry *e = &log->marked_block_entries[pos]; | |
991 | ||
992 | if ((e->marked_count + 1 == BBM_LOG_MAX_LBA_ENTRY_VAL) && | |
993 | (__le48_to_cpu(&e->defective_block_start) == sector)) { | |
994 | sector += BBM_LOG_MAX_LBA_ENTRY_VAL; | |
995 | length -= BBM_LOG_MAX_LBA_ENTRY_VAL; | |
996 | pos = pos + 1; | |
997 | continue; | |
998 | } | |
999 | entry = e; | |
1000 | break; | |
1001 | } | |
1002 | ||
1003 | if (entry) { | |
1004 | int cnt = (length <= BBM_LOG_MAX_LBA_ENTRY_VAL) ? length : | |
1005 | BBM_LOG_MAX_LBA_ENTRY_VAL; | |
1006 | entry->defective_block_start = __cpu_to_le48(sector); | |
1007 | entry->marked_count = cnt - 1; | |
1008 | if (cnt == length) | |
1009 | return 1; | |
1010 | sector += cnt; | |
1011 | length -= cnt; | |
1012 | } | |
1013 | ||
1014 | new_bb = ROUND_UP(length, BBM_LOG_MAX_LBA_ENTRY_VAL) / | |
1015 | BBM_LOG_MAX_LBA_ENTRY_VAL; | |
1016 | if (log->entry_count + new_bb > BBM_LOG_MAX_ENTRIES) | |
1017 | return 0; | |
1018 | ||
1019 | while (length > 0) { | |
1020 | int cnt = (length <= BBM_LOG_MAX_LBA_ENTRY_VAL) ? length : | |
1021 | BBM_LOG_MAX_LBA_ENTRY_VAL; | |
1022 | struct bbm_log_entry *entry = | |
1023 | &log->marked_block_entries[log->entry_count]; | |
1024 | ||
1025 | entry->defective_block_start = __cpu_to_le48(sector); | |
1026 | entry->marked_count = cnt - 1; | |
1027 | entry->disk_ordinal = idx; | |
1028 | ||
1029 | sector += cnt; | |
1030 | length -= cnt; | |
1031 | ||
1032 | log->entry_count++; | |
1033 | } | |
1034 | ||
1035 | return new_bb; | |
1036 | } | |
c07a5a4f | 1037 | |
4c9e8c1e TM |
1038 | /* clear all bad blocks for given disk */ |
1039 | static void clear_disk_badblocks(struct bbm_log *log, const __u8 idx) | |
1040 | { | |
1041 | __u32 i = 0; | |
1042 | ||
1043 | while (i < log->entry_count) { | |
1044 | struct bbm_log_entry *entries = log->marked_block_entries; | |
1045 | ||
1046 | if (entries[i].disk_ordinal == idx) { | |
1047 | if (i < log->entry_count - 1) | |
1048 | entries[i] = entries[log->entry_count - 1]; | |
1049 | log->entry_count--; | |
1050 | } else { | |
1051 | i++; | |
1052 | } | |
1053 | } | |
1054 | } | |
1055 | ||
c07a5a4f TM |
1056 | /* clear given bad block */ |
1057 | static int clear_badblock(struct bbm_log *log, const __u8 idx, const unsigned | |
1058 | long long sector, const int length) { | |
1059 | __u32 i = 0; | |
1060 | ||
1061 | while (i < log->entry_count) { | |
1062 | struct bbm_log_entry *entries = log->marked_block_entries; | |
1063 | ||
1064 | if ((entries[i].disk_ordinal == idx) && | |
1065 | (__le48_to_cpu(&entries[i].defective_block_start) == | |
1066 | sector) && (entries[i].marked_count + 1 == length)) { | |
1067 | if (i < log->entry_count - 1) | |
1068 | entries[i] = entries[log->entry_count - 1]; | |
1069 | log->entry_count--; | |
1070 | break; | |
1071 | } | |
1072 | i++; | |
1073 | } | |
1074 | ||
1075 | return 1; | |
1076 | } | |
8d67477f TM |
1077 | |
1078 | /* allocate and load BBM log from metadata */ | |
1079 | static int load_bbm_log(struct intel_super *super) | |
1080 | { | |
1081 | struct imsm_super *mpb = super->anchor; | |
1082 | __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size); | |
1083 | ||
1084 | super->bbm_log = xcalloc(1, sizeof(struct bbm_log)); | |
1085 | if (!super->bbm_log) | |
1086 | return 1; | |
1087 | ||
1088 | if (bbm_log_size) { | |
1089 | struct bbm_log *log = (void *)mpb + | |
1090 | __le32_to_cpu(mpb->mpb_size) - bbm_log_size; | |
1091 | ||
1092 | __u32 entry_count; | |
1093 | ||
1094 | if (bbm_log_size < sizeof(log->signature) + | |
1095 | sizeof(log->entry_count)) | |
1096 | return 2; | |
1097 | ||
1098 | entry_count = __le32_to_cpu(log->entry_count); | |
1099 | if ((__le32_to_cpu(log->signature) != BBM_LOG_SIGNATURE) || | |
1100 | (entry_count > BBM_LOG_MAX_ENTRIES)) | |
1101 | return 3; | |
1102 | ||
1103 | if (bbm_log_size != | |
1104 | sizeof(log->signature) + sizeof(log->entry_count) + | |
1105 | entry_count * sizeof(struct bbm_log_entry)) | |
1106 | return 4; | |
1107 | ||
1108 | memcpy(super->bbm_log, log, bbm_log_size); | |
1109 | } else { | |
1110 | super->bbm_log->signature = __cpu_to_le32(BBM_LOG_SIGNATURE); | |
1111 | super->bbm_log->entry_count = 0; | |
1112 | } | |
1113 | ||
1114 | return 0; | |
1115 | } | |
1116 | ||
b12796be TM |
1117 | /* checks if bad block is within volume boundaries */ |
1118 | static int is_bad_block_in_volume(const struct bbm_log_entry *entry, | |
1119 | const unsigned long long start_sector, | |
1120 | const unsigned long long size) | |
1121 | { | |
1122 | unsigned long long bb_start; | |
1123 | unsigned long long bb_end; | |
1124 | ||
1125 | bb_start = __le48_to_cpu(&entry->defective_block_start); | |
1126 | bb_end = bb_start + (entry->marked_count + 1); | |
1127 | ||
1128 | if (((bb_start >= start_sector) && (bb_start < start_sector + size)) || | |
1129 | ((bb_end >= start_sector) && (bb_end <= start_sector + size))) | |
1130 | return 1; | |
1131 | ||
1132 | return 0; | |
1133 | } | |
1134 | ||
1135 | /* get list of bad blocks on a drive for a volume */ | |
1136 | static void get_volume_badblocks(const struct bbm_log *log, const __u8 idx, | |
1137 | const unsigned long long start_sector, | |
1138 | const unsigned long long size, | |
1139 | struct md_bb *bbs) | |
1140 | { | |
1141 | __u32 count = 0; | |
1142 | __u32 i; | |
1143 | ||
1144 | for (i = 0; i < log->entry_count; i++) { | |
1145 | const struct bbm_log_entry *ent = | |
1146 | &log->marked_block_entries[i]; | |
1147 | struct md_bb_entry *bb; | |
1148 | ||
1149 | if ((ent->disk_ordinal == idx) && | |
1150 | is_bad_block_in_volume(ent, start_sector, size)) { | |
1151 | ||
1152 | if (!bbs->entries) { | |
1153 | bbs->entries = xmalloc(BBM_LOG_MAX_ENTRIES * | |
1154 | sizeof(*bb)); | |
1155 | if (!bbs->entries) | |
1156 | break; | |
1157 | } | |
1158 | ||
1159 | bb = &bbs->entries[count++]; | |
1160 | bb->sector = __le48_to_cpu(&ent->defective_block_start); | |
1161 | bb->length = ent->marked_count + 1; | |
1162 | } | |
1163 | } | |
1164 | bbs->count = count; | |
1165 | } | |
1166 | ||
98130f40 AK |
1167 | /* |
1168 | * for second_map: | |
238c0a71 AK |
1169 | * == MAP_0 get first map |
1170 | * == MAP_1 get second map | |
1171 | * == MAP_X than get map according to the current migr_state | |
98130f40 AK |
1172 | */ |
1173 | static __u32 get_imsm_ord_tbl_ent(struct imsm_dev *dev, | |
1174 | int slot, | |
1175 | int second_map) | |
7eef0453 DW |
1176 | { |
1177 | struct imsm_map *map; | |
1178 | ||
5e7b0330 | 1179 | map = get_imsm_map(dev, second_map); |
7eef0453 | 1180 | |
ff077194 DW |
1181 | /* top byte identifies disk under rebuild */ |
1182 | return __le32_to_cpu(map->disk_ord_tbl[slot]); | |
1183 | } | |
1184 | ||
1185 | #define ord_to_idx(ord) (((ord) << 8) >> 8) | |
98130f40 | 1186 | static __u32 get_imsm_disk_idx(struct imsm_dev *dev, int slot, int second_map) |
ff077194 | 1187 | { |
98130f40 | 1188 | __u32 ord = get_imsm_ord_tbl_ent(dev, slot, second_map); |
ff077194 DW |
1189 | |
1190 | return ord_to_idx(ord); | |
7eef0453 DW |
1191 | } |
1192 | ||
be73972f DW |
1193 | static void set_imsm_ord_tbl_ent(struct imsm_map *map, int slot, __u32 ord) |
1194 | { | |
1195 | map->disk_ord_tbl[slot] = __cpu_to_le32(ord); | |
1196 | } | |
1197 | ||
76c152ca | 1198 | static int get_imsm_disk_slot(struct imsm_map *map, const unsigned int idx) |
620b1713 DW |
1199 | { |
1200 | int slot; | |
1201 | __u32 ord; | |
1202 | ||
1203 | for (slot = 0; slot < map->num_members; slot++) { | |
1204 | ord = __le32_to_cpu(map->disk_ord_tbl[slot]); | |
1205 | if (ord_to_idx(ord) == idx) | |
1206 | return slot; | |
1207 | } | |
1208 | ||
76c152ca | 1209 | return IMSM_STATUS_ERROR; |
620b1713 DW |
1210 | } |
1211 | ||
cdddbdbc DW |
1212 | static int get_imsm_raid_level(struct imsm_map *map) |
1213 | { | |
1214 | if (map->raid_level == 1) { | |
1215 | if (map->num_members == 2) | |
1216 | return 1; | |
1217 | else | |
1218 | return 10; | |
1219 | } | |
1220 | ||
1221 | return map->raid_level; | |
1222 | } | |
1223 | ||
76c152ca MT |
1224 | /** |
1225 | * get_disk_slot_in_dev() - retrieve disk slot from &imsm_dev. | |
1226 | * @super: &intel_super pointer, not NULL. | |
1227 | * @dev_idx: imsm device index. | |
1228 | * @idx: disk index. | |
1229 | * | |
1230 | * Return: Slot on success, IMSM_STATUS_ERROR otherwise. | |
1231 | */ | |
1232 | static int get_disk_slot_in_dev(struct intel_super *super, const __u8 dev_idx, | |
1233 | const unsigned int idx) | |
1234 | { | |
1235 | struct imsm_dev *dev = get_imsm_dev(super, dev_idx); | |
1236 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
1237 | ||
1238 | return get_imsm_disk_slot(map, idx); | |
1239 | } | |
1240 | ||
c2c087e6 DW |
1241 | static int cmp_extent(const void *av, const void *bv) |
1242 | { | |
1243 | const struct extent *a = av; | |
1244 | const struct extent *b = bv; | |
1245 | if (a->start < b->start) | |
1246 | return -1; | |
1247 | if (a->start > b->start) | |
1248 | return 1; | |
1249 | return 0; | |
1250 | } | |
1251 | ||
0dcecb2e | 1252 | static int count_memberships(struct dl *dl, struct intel_super *super) |
c2c087e6 | 1253 | { |
c2c087e6 | 1254 | int memberships = 0; |
620b1713 | 1255 | int i; |
c2c087e6 | 1256 | |
76c152ca MT |
1257 | for (i = 0; i < super->anchor->num_raid_devs; i++) |
1258 | if (get_disk_slot_in_dev(super, i, dl->index) >= 0) | |
620b1713 | 1259 | memberships++; |
0dcecb2e DW |
1260 | |
1261 | return memberships; | |
1262 | } | |
1263 | ||
b81221b7 CA |
1264 | static __u32 imsm_min_reserved_sectors(struct intel_super *super); |
1265 | ||
486720e0 | 1266 | static int split_ull(unsigned long long n, void *lo, void *hi) |
5551b113 CA |
1267 | { |
1268 | if (lo == 0 || hi == 0) | |
1269 | return 1; | |
486720e0 JS |
1270 | __put_unaligned32(__cpu_to_le32((__u32)n), lo); |
1271 | __put_unaligned32(__cpu_to_le32((n >> 32)), hi); | |
5551b113 CA |
1272 | return 0; |
1273 | } | |
1274 | ||
1275 | static unsigned long long join_u32(__u32 lo, __u32 hi) | |
1276 | { | |
1277 | return (unsigned long long)__le32_to_cpu(lo) | | |
1278 | (((unsigned long long)__le32_to_cpu(hi)) << 32); | |
1279 | } | |
1280 | ||
1281 | static unsigned long long total_blocks(struct imsm_disk *disk) | |
1282 | { | |
1283 | if (disk == NULL) | |
1284 | return 0; | |
1285 | return join_u32(disk->total_blocks_lo, disk->total_blocks_hi); | |
1286 | } | |
1287 | ||
1c275381 MT |
1288 | /** |
1289 | * imsm_num_data_members() - get data drives count for an array. | |
1290 | * @map: Map to analyze. | |
1291 | * | |
1292 | * num_data_members value represents minimal count of drives for level. | |
1293 | * The name of the property could be misleading for RAID5 with asymmetric layout | |
1294 | * because some data required to be calculated from parity. | |
1295 | * The property is extracted from level and num_members value. | |
1296 | * | |
1297 | * Return: num_data_members value on success, zero otherwise. | |
1298 | */ | |
1299 | static __u8 imsm_num_data_members(struct imsm_map *map) | |
1300 | { | |
1301 | switch (get_imsm_raid_level(map)) { | |
1302 | case 0: | |
1303 | return map->num_members; | |
1304 | case 1: | |
1305 | case 10: | |
1306 | return map->num_members / 2; | |
1307 | case 5: | |
1308 | return map->num_members - 1; | |
1309 | default: | |
1310 | dprintf("unsupported raid level\n"); | |
1311 | return 0; | |
1312 | } | |
1313 | } | |
1314 | ||
5551b113 CA |
1315 | static unsigned long long pba_of_lba0(struct imsm_map *map) |
1316 | { | |
1317 | if (map == NULL) | |
1318 | return 0; | |
1319 | return join_u32(map->pba_of_lba0_lo, map->pba_of_lba0_hi); | |
1320 | } | |
1321 | ||
1322 | static unsigned long long blocks_per_member(struct imsm_map *map) | |
1323 | { | |
1324 | if (map == NULL) | |
1325 | return 0; | |
1326 | return join_u32(map->blocks_per_member_lo, map->blocks_per_member_hi); | |
1327 | } | |
1328 | ||
1329 | static unsigned long long num_data_stripes(struct imsm_map *map) | |
1330 | { | |
1331 | if (map == NULL) | |
1332 | return 0; | |
1333 | return join_u32(map->num_data_stripes_lo, map->num_data_stripes_hi); | |
1334 | } | |
1335 | ||
4036e7ee MT |
1336 | static unsigned long long vol_curr_migr_unit(struct imsm_dev *dev) |
1337 | { | |
1338 | if (dev == NULL) | |
1339 | return 0; | |
1340 | ||
1341 | return join_u32(dev->vol.curr_migr_unit_lo, dev->vol.curr_migr_unit_hi); | |
1342 | } | |
1343 | ||
fcc2c9da MD |
1344 | static unsigned long long imsm_dev_size(struct imsm_dev *dev) |
1345 | { | |
1346 | if (dev == NULL) | |
1347 | return 0; | |
1348 | return join_u32(dev->size_low, dev->size_high); | |
1349 | } | |
1350 | ||
9f421827 PB |
1351 | static unsigned long long migr_chkp_area_pba(struct migr_record *migr_rec) |
1352 | { | |
1353 | if (migr_rec == NULL) | |
1354 | return 0; | |
1355 | return join_u32(migr_rec->ckpt_area_pba_lo, | |
1356 | migr_rec->ckpt_area_pba_hi); | |
1357 | } | |
1358 | ||
1359 | static unsigned long long current_migr_unit(struct migr_record *migr_rec) | |
1360 | { | |
1361 | if (migr_rec == NULL) | |
1362 | return 0; | |
1363 | return join_u32(migr_rec->curr_migr_unit_lo, | |
1364 | migr_rec->curr_migr_unit_hi); | |
1365 | } | |
1366 | ||
1367 | static unsigned long long migr_dest_1st_member_lba(struct migr_record *migr_rec) | |
1368 | { | |
1369 | if (migr_rec == NULL) | |
1370 | return 0; | |
1371 | return join_u32(migr_rec->dest_1st_member_lba_lo, | |
1372 | migr_rec->dest_1st_member_lba_hi); | |
1373 | } | |
1374 | ||
1375 | static unsigned long long get_num_migr_units(struct migr_record *migr_rec) | |
1376 | { | |
1377 | if (migr_rec == NULL) | |
1378 | return 0; | |
1379 | return join_u32(migr_rec->num_migr_units_lo, | |
1380 | migr_rec->num_migr_units_hi); | |
1381 | } | |
1382 | ||
5551b113 CA |
1383 | static void set_total_blocks(struct imsm_disk *disk, unsigned long long n) |
1384 | { | |
1385 | split_ull(n, &disk->total_blocks_lo, &disk->total_blocks_hi); | |
1386 | } | |
1387 | ||
1c275381 MT |
1388 | /** |
1389 | * set_num_domains() - Set number of domains for an array. | |
1390 | * @map: Map to be updated. | |
1391 | * | |
1392 | * num_domains property represents copies count of each data drive, thus make | |
1393 | * it meaningful only for RAID1 and RAID10. IMSM supports two domains for | |
1394 | * raid1 and raid10. | |
1395 | */ | |
1396 | static void set_num_domains(struct imsm_map *map) | |
1397 | { | |
1398 | int level = get_imsm_raid_level(map); | |
1399 | ||
1400 | if (level == 1 || level == 10) | |
1401 | map->num_domains = 2; | |
1402 | else | |
1403 | map->num_domains = 1; | |
1404 | } | |
1405 | ||
5551b113 CA |
1406 | static void set_pba_of_lba0(struct imsm_map *map, unsigned long long n) |
1407 | { | |
1408 | split_ull(n, &map->pba_of_lba0_lo, &map->pba_of_lba0_hi); | |
1409 | } | |
1410 | ||
1411 | static void set_blocks_per_member(struct imsm_map *map, unsigned long long n) | |
1412 | { | |
1413 | split_ull(n, &map->blocks_per_member_lo, &map->blocks_per_member_hi); | |
1414 | } | |
1415 | ||
1416 | static void set_num_data_stripes(struct imsm_map *map, unsigned long long n) | |
1417 | { | |
1418 | split_ull(n, &map->num_data_stripes_lo, &map->num_data_stripes_hi); | |
1419 | } | |
1420 | ||
1c275381 MT |
1421 | /** |
1422 | * update_num_data_stripes() - Calculate and update num_data_stripes value. | |
1423 | * @map: map to be updated. | |
1424 | * @dev_size: size of volume. | |
1425 | * | |
1426 | * num_data_stripes value is addictionally divided by num_domains, therefore for | |
1427 | * levels where num_domains is not 1, nds is a part of real value. | |
1428 | */ | |
1429 | static void update_num_data_stripes(struct imsm_map *map, | |
1430 | unsigned long long dev_size) | |
1431 | { | |
1432 | unsigned long long nds = dev_size / imsm_num_data_members(map); | |
1433 | ||
1434 | nds /= map->num_domains; | |
1435 | nds /= map->blocks_per_strip; | |
1436 | set_num_data_stripes(map, nds); | |
1437 | } | |
1438 | ||
4036e7ee MT |
1439 | static void set_vol_curr_migr_unit(struct imsm_dev *dev, unsigned long long n) |
1440 | { | |
1441 | if (dev == NULL) | |
1442 | return; | |
1443 | ||
1444 | split_ull(n, &dev->vol.curr_migr_unit_lo, &dev->vol.curr_migr_unit_hi); | |
1445 | } | |
1446 | ||
fcc2c9da MD |
1447 | static void set_imsm_dev_size(struct imsm_dev *dev, unsigned long long n) |
1448 | { | |
1449 | split_ull(n, &dev->size_low, &dev->size_high); | |
1450 | } | |
1451 | ||
9f421827 PB |
1452 | static void set_migr_chkp_area_pba(struct migr_record *migr_rec, |
1453 | unsigned long long n) | |
1454 | { | |
1455 | split_ull(n, &migr_rec->ckpt_area_pba_lo, &migr_rec->ckpt_area_pba_hi); | |
1456 | } | |
1457 | ||
1458 | static void set_current_migr_unit(struct migr_record *migr_rec, | |
1459 | unsigned long long n) | |
1460 | { | |
1461 | split_ull(n, &migr_rec->curr_migr_unit_lo, | |
1462 | &migr_rec->curr_migr_unit_hi); | |
1463 | } | |
1464 | ||
1465 | static void set_migr_dest_1st_member_lba(struct migr_record *migr_rec, | |
1466 | unsigned long long n) | |
1467 | { | |
1468 | split_ull(n, &migr_rec->dest_1st_member_lba_lo, | |
1469 | &migr_rec->dest_1st_member_lba_hi); | |
1470 | } | |
1471 | ||
1472 | static void set_num_migr_units(struct migr_record *migr_rec, | |
1473 | unsigned long long n) | |
1474 | { | |
1475 | split_ull(n, &migr_rec->num_migr_units_lo, | |
1476 | &migr_rec->num_migr_units_hi); | |
1477 | } | |
1478 | ||
44490938 MD |
1479 | static unsigned long long per_dev_array_size(struct imsm_map *map) |
1480 | { | |
1481 | unsigned long long array_size = 0; | |
1482 | ||
1483 | if (map == NULL) | |
1484 | return array_size; | |
1485 | ||
1486 | array_size = num_data_stripes(map) * map->blocks_per_strip; | |
1487 | if (get_imsm_raid_level(map) == 1 || get_imsm_raid_level(map) == 10) | |
1488 | array_size *= 2; | |
1489 | ||
1490 | return array_size; | |
1491 | } | |
1492 | ||
05501181 PB |
1493 | static struct extent *get_extents(struct intel_super *super, struct dl *dl, |
1494 | int get_minimal_reservation) | |
0dcecb2e DW |
1495 | { |
1496 | /* find a list of used extents on the given physical device */ | |
1497 | struct extent *rv, *e; | |
620b1713 | 1498 | int i; |
0dcecb2e | 1499 | int memberships = count_memberships(dl, super); |
b276dd33 DW |
1500 | __u32 reservation; |
1501 | ||
1502 | /* trim the reserved area for spares, so they can join any array | |
1503 | * regardless of whether the OROM has assigned sectors from the | |
1504 | * IMSM_RESERVED_SECTORS region | |
1505 | */ | |
05501181 | 1506 | if (dl->index == -1 || get_minimal_reservation) |
b81221b7 | 1507 | reservation = imsm_min_reserved_sectors(super); |
b276dd33 DW |
1508 | else |
1509 | reservation = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS; | |
0dcecb2e | 1510 | |
503975b9 | 1511 | rv = xcalloc(sizeof(struct extent), (memberships + 1)); |
c2c087e6 DW |
1512 | e = rv; |
1513 | ||
949c47a0 DW |
1514 | for (i = 0; i < super->anchor->num_raid_devs; i++) { |
1515 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
238c0a71 | 1516 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
c2c087e6 | 1517 | |
620b1713 | 1518 | if (get_imsm_disk_slot(map, dl->index) >= 0) { |
5551b113 | 1519 | e->start = pba_of_lba0(map); |
44490938 | 1520 | e->size = per_dev_array_size(map); |
620b1713 | 1521 | e++; |
c2c087e6 DW |
1522 | } |
1523 | } | |
1524 | qsort(rv, memberships, sizeof(*rv), cmp_extent); | |
1525 | ||
1011e834 | 1526 | /* determine the start of the metadata |
14e8215b DW |
1527 | * when no raid devices are defined use the default |
1528 | * ...otherwise allow the metadata to truncate the value | |
1529 | * as is the case with older versions of imsm | |
1530 | */ | |
1531 | if (memberships) { | |
1532 | struct extent *last = &rv[memberships - 1]; | |
5551b113 | 1533 | unsigned long long remainder; |
14e8215b | 1534 | |
5551b113 | 1535 | remainder = total_blocks(&dl->disk) - (last->start + last->size); |
dda5855f DW |
1536 | /* round down to 1k block to satisfy precision of the kernel |
1537 | * 'size' interface | |
1538 | */ | |
1539 | remainder &= ~1UL; | |
1540 | /* make sure remainder is still sane */ | |
f21e18ca | 1541 | if (remainder < (unsigned)ROUND_UP(super->len, 512) >> 9) |
dda5855f | 1542 | remainder = ROUND_UP(super->len, 512) >> 9; |
14e8215b DW |
1543 | if (reservation > remainder) |
1544 | reservation = remainder; | |
1545 | } | |
5551b113 | 1546 | e->start = total_blocks(&dl->disk) - reservation; |
c2c087e6 DW |
1547 | e->size = 0; |
1548 | return rv; | |
1549 | } | |
1550 | ||
14e8215b DW |
1551 | /* try to determine how much space is reserved for metadata from |
1552 | * the last get_extents() entry, otherwise fallback to the | |
1553 | * default | |
1554 | */ | |
1555 | static __u32 imsm_reserved_sectors(struct intel_super *super, struct dl *dl) | |
1556 | { | |
1557 | struct extent *e; | |
1558 | int i; | |
1559 | __u32 rv; | |
1560 | ||
1561 | /* for spares just return a minimal reservation which will grow | |
1562 | * once the spare is picked up by an array | |
1563 | */ | |
1564 | if (dl->index == -1) | |
1565 | return MPB_SECTOR_CNT; | |
1566 | ||
05501181 | 1567 | e = get_extents(super, dl, 0); |
14e8215b DW |
1568 | if (!e) |
1569 | return MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS; | |
1570 | ||
1571 | /* scroll to last entry */ | |
1572 | for (i = 0; e[i].size; i++) | |
1573 | continue; | |
1574 | ||
5551b113 | 1575 | rv = total_blocks(&dl->disk) - e[i].start; |
14e8215b DW |
1576 | |
1577 | free(e); | |
1578 | ||
1579 | return rv; | |
1580 | } | |
1581 | ||
25ed7e59 DW |
1582 | static int is_spare(struct imsm_disk *disk) |
1583 | { | |
1584 | return (disk->status & SPARE_DISK) == SPARE_DISK; | |
1585 | } | |
1586 | ||
1587 | static int is_configured(struct imsm_disk *disk) | |
1588 | { | |
1589 | return (disk->status & CONFIGURED_DISK) == CONFIGURED_DISK; | |
1590 | } | |
1591 | ||
1592 | static int is_failed(struct imsm_disk *disk) | |
1593 | { | |
1594 | return (disk->status & FAILED_DISK) == FAILED_DISK; | |
1595 | } | |
1596 | ||
2432ce9b AP |
1597 | static int is_journal(struct imsm_disk *disk) |
1598 | { | |
1599 | return (disk->status & JOURNAL_DISK) == JOURNAL_DISK; | |
1600 | } | |
1601 | ||
b53bfba6 TM |
1602 | /* round array size down to closest MB and ensure it splits evenly |
1603 | * between members | |
1604 | */ | |
1605 | static unsigned long long round_size_to_mb(unsigned long long size, unsigned int | |
1606 | disk_count) | |
1607 | { | |
1608 | size /= disk_count; | |
1609 | size = (size >> SECT_PER_MB_SHIFT) << SECT_PER_MB_SHIFT; | |
1610 | size *= disk_count; | |
1611 | ||
1612 | return size; | |
1613 | } | |
1614 | ||
8b9cd157 MK |
1615 | static int able_to_resync(int raid_level, int missing_disks) |
1616 | { | |
1617 | int max_missing_disks = 0; | |
1618 | ||
1619 | switch (raid_level) { | |
1620 | case 10: | |
1621 | max_missing_disks = 1; | |
1622 | break; | |
1623 | default: | |
1624 | max_missing_disks = 0; | |
1625 | } | |
1626 | return missing_disks <= max_missing_disks; | |
1627 | } | |
1628 | ||
b81221b7 CA |
1629 | /* try to determine how much space is reserved for metadata from |
1630 | * the last get_extents() entry on the smallest active disk, | |
1631 | * otherwise fallback to the default | |
1632 | */ | |
1633 | static __u32 imsm_min_reserved_sectors(struct intel_super *super) | |
1634 | { | |
1635 | struct extent *e; | |
1636 | int i; | |
5551b113 CA |
1637 | unsigned long long min_active; |
1638 | __u32 remainder; | |
b81221b7 CA |
1639 | __u32 rv = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS; |
1640 | struct dl *dl, *dl_min = NULL; | |
1641 | ||
1642 | if (!super) | |
1643 | return rv; | |
1644 | ||
1645 | min_active = 0; | |
1646 | for (dl = super->disks; dl; dl = dl->next) { | |
1647 | if (dl->index < 0) | |
1648 | continue; | |
5551b113 CA |
1649 | unsigned long long blocks = total_blocks(&dl->disk); |
1650 | if (blocks < min_active || min_active == 0) { | |
b81221b7 | 1651 | dl_min = dl; |
5551b113 | 1652 | min_active = blocks; |
b81221b7 CA |
1653 | } |
1654 | } | |
1655 | if (!dl_min) | |
1656 | return rv; | |
1657 | ||
1658 | /* find last lba used by subarrays on the smallest active disk */ | |
05501181 | 1659 | e = get_extents(super, dl_min, 0); |
b81221b7 CA |
1660 | if (!e) |
1661 | return rv; | |
1662 | for (i = 0; e[i].size; i++) | |
1663 | continue; | |
1664 | ||
1665 | remainder = min_active - e[i].start; | |
1666 | free(e); | |
1667 | ||
1668 | /* to give priority to recovery we should not require full | |
1669 | IMSM_RESERVED_SECTORS from the spare */ | |
1670 | rv = MPB_SECTOR_CNT + NUM_BLOCKS_DIRTY_STRIPE_REGION; | |
1671 | ||
1672 | /* if real reservation is smaller use that value */ | |
1673 | return (remainder < rv) ? remainder : rv; | |
1674 | } | |
1675 | ||
fbfdcb06 AO |
1676 | /* |
1677 | * Return minimum size of a spare and sector size | |
1678 | * that can be used in this array | |
1679 | */ | |
1680 | int get_spare_criteria_imsm(struct supertype *st, struct spare_criteria *c) | |
80e7f8c3 AC |
1681 | { |
1682 | struct intel_super *super = st->sb; | |
1683 | struct dl *dl; | |
1684 | struct extent *e; | |
1685 | int i; | |
fbfdcb06 AO |
1686 | unsigned long long size = 0; |
1687 | ||
1688 | c->min_size = 0; | |
4b57ecf6 | 1689 | c->sector_size = 0; |
80e7f8c3 AC |
1690 | |
1691 | if (!super) | |
fbfdcb06 | 1692 | return -EINVAL; |
80e7f8c3 AC |
1693 | /* find first active disk in array */ |
1694 | dl = super->disks; | |
1695 | while (dl && (is_failed(&dl->disk) || dl->index == -1)) | |
1696 | dl = dl->next; | |
1697 | if (!dl) | |
fbfdcb06 | 1698 | return -EINVAL; |
80e7f8c3 | 1699 | /* find last lba used by subarrays */ |
05501181 | 1700 | e = get_extents(super, dl, 0); |
80e7f8c3 | 1701 | if (!e) |
fbfdcb06 | 1702 | return -EINVAL; |
80e7f8c3 AC |
1703 | for (i = 0; e[i].size; i++) |
1704 | continue; | |
1705 | if (i > 0) | |
fbfdcb06 | 1706 | size = e[i-1].start + e[i-1].size; |
80e7f8c3 | 1707 | free(e); |
b81221b7 | 1708 | |
80e7f8c3 | 1709 | /* add the amount of space needed for metadata */ |
fbfdcb06 AO |
1710 | size += imsm_min_reserved_sectors(super); |
1711 | ||
1712 | c->min_size = size * 512; | |
4b57ecf6 | 1713 | c->sector_size = super->sector_size; |
b81221b7 | 1714 | |
fbfdcb06 | 1715 | return 0; |
80e7f8c3 AC |
1716 | } |
1717 | ||
195d1d76 | 1718 | static bool is_gen_migration(struct imsm_dev *dev); |
d1e02575 | 1719 | |
f36a9ecd PB |
1720 | #define IMSM_4K_DIV 8 |
1721 | ||
c47b0ff6 AK |
1722 | static __u64 blocks_per_migr_unit(struct intel_super *super, |
1723 | struct imsm_dev *dev); | |
1e5c6983 | 1724 | |
c47b0ff6 AK |
1725 | static void print_imsm_dev(struct intel_super *super, |
1726 | struct imsm_dev *dev, | |
1727 | char *uuid, | |
1728 | int disk_idx) | |
cdddbdbc DW |
1729 | { |
1730 | __u64 sz; | |
0d80bb2f | 1731 | int slot, i; |
238c0a71 AK |
1732 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
1733 | struct imsm_map *map2 = get_imsm_map(dev, MAP_1); | |
b10b37b8 | 1734 | __u32 ord; |
cdddbdbc DW |
1735 | |
1736 | printf("\n"); | |
1e7bc0ed | 1737 | printf("[%.16s]:\n", dev->volume); |
ba1b3bc8 | 1738 | printf(" Subarray : %d\n", super->current_vol); |
44470971 | 1739 | printf(" UUID : %s\n", uuid); |
dd8bcb3b AK |
1740 | printf(" RAID Level : %d", get_imsm_raid_level(map)); |
1741 | if (map2) | |
1742 | printf(" <-- %d", get_imsm_raid_level(map2)); | |
1743 | printf("\n"); | |
1744 | printf(" Members : %d", map->num_members); | |
1745 | if (map2) | |
1746 | printf(" <-- %d", map2->num_members); | |
1747 | printf("\n"); | |
0d80bb2f DW |
1748 | printf(" Slots : ["); |
1749 | for (i = 0; i < map->num_members; i++) { | |
238c0a71 | 1750 | ord = get_imsm_ord_tbl_ent(dev, i, MAP_0); |
0d80bb2f DW |
1751 | printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U"); |
1752 | } | |
dd8bcb3b AK |
1753 | printf("]"); |
1754 | if (map2) { | |
1755 | printf(" <-- ["); | |
1756 | for (i = 0; i < map2->num_members; i++) { | |
238c0a71 | 1757 | ord = get_imsm_ord_tbl_ent(dev, i, MAP_1); |
dd8bcb3b AK |
1758 | printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U"); |
1759 | } | |
1760 | printf("]"); | |
1761 | } | |
1762 | printf("\n"); | |
7095bccb AK |
1763 | printf(" Failed disk : "); |
1764 | if (map->failed_disk_num == 0xff) | |
1765 | printf("none"); | |
1766 | else | |
1767 | printf("%i", map->failed_disk_num); | |
1768 | printf("\n"); | |
620b1713 DW |
1769 | slot = get_imsm_disk_slot(map, disk_idx); |
1770 | if (slot >= 0) { | |
238c0a71 | 1771 | ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X); |
b10b37b8 DW |
1772 | printf(" This Slot : %d%s\n", slot, |
1773 | ord & IMSM_ORD_REBUILD ? " (out-of-sync)" : ""); | |
1774 | } else | |
cdddbdbc | 1775 | printf(" This Slot : ?\n"); |
84918897 | 1776 | printf(" Sector Size : %u\n", super->sector_size); |
fcc2c9da | 1777 | sz = imsm_dev_size(dev); |
84918897 MK |
1778 | printf(" Array Size : %llu%s\n", |
1779 | (unsigned long long)sz * 512 / super->sector_size, | |
cdddbdbc | 1780 | human_size(sz * 512)); |
5551b113 | 1781 | sz = blocks_per_member(map); |
84918897 MK |
1782 | printf(" Per Dev Size : %llu%s\n", |
1783 | (unsigned long long)sz * 512 / super->sector_size, | |
cdddbdbc | 1784 | human_size(sz * 512)); |
5551b113 | 1785 | printf(" Sector Offset : %llu\n", |
7d8935cb | 1786 | pba_of_lba0(map) * 512 / super->sector_size); |
5551b113 CA |
1787 | printf(" Num Stripes : %llu\n", |
1788 | num_data_stripes(map)); | |
dd8bcb3b | 1789 | printf(" Chunk Size : %u KiB", |
cdddbdbc | 1790 | __le16_to_cpu(map->blocks_per_strip) / 2); |
dd8bcb3b AK |
1791 | if (map2) |
1792 | printf(" <-- %u KiB", | |
1793 | __le16_to_cpu(map2->blocks_per_strip) / 2); | |
1794 | printf("\n"); | |
cdddbdbc | 1795 | printf(" Reserved : %d\n", __le32_to_cpu(dev->reserved_blocks)); |
8655a7b1 | 1796 | printf(" Migrate State : "); |
1484e727 DW |
1797 | if (dev->vol.migr_state) { |
1798 | if (migr_type(dev) == MIGR_INIT) | |
8655a7b1 | 1799 | printf("initialize\n"); |
1484e727 | 1800 | else if (migr_type(dev) == MIGR_REBUILD) |
8655a7b1 | 1801 | printf("rebuild\n"); |
1484e727 | 1802 | else if (migr_type(dev) == MIGR_VERIFY) |
8655a7b1 | 1803 | printf("check\n"); |
1484e727 | 1804 | else if (migr_type(dev) == MIGR_GEN_MIGR) |
8655a7b1 | 1805 | printf("general migration\n"); |
1484e727 | 1806 | else if (migr_type(dev) == MIGR_STATE_CHANGE) |
8655a7b1 | 1807 | printf("state change\n"); |
1484e727 | 1808 | else if (migr_type(dev) == MIGR_REPAIR) |
8655a7b1 | 1809 | printf("repair\n"); |
1484e727 | 1810 | else |
8655a7b1 DW |
1811 | printf("<unknown:%d>\n", migr_type(dev)); |
1812 | } else | |
1813 | printf("idle\n"); | |
3393c6af DW |
1814 | printf(" Map State : %s", map_state_str[map->map_state]); |
1815 | if (dev->vol.migr_state) { | |
238c0a71 | 1816 | struct imsm_map *map = get_imsm_map(dev, MAP_1); |
1e5c6983 | 1817 | |
b10b37b8 | 1818 | printf(" <-- %s", map_state_str[map->map_state]); |
4036e7ee | 1819 | printf("\n Checkpoint : %llu ", vol_curr_migr_unit(dev)); |
089f9d79 | 1820 | if (is_gen_migration(dev) && (slot > 1 || slot < 0)) |
464d40e8 LD |
1821 | printf("(N/A)"); |
1822 | else | |
1823 | printf("(%llu)", (unsigned long long) | |
1824 | blocks_per_migr_unit(super, dev)); | |
3393c6af DW |
1825 | } |
1826 | printf("\n"); | |
2432ce9b AP |
1827 | printf(" Dirty State : %s\n", (dev->vol.dirty & RAIDVOL_DIRTY) ? |
1828 | "dirty" : "clean"); | |
1829 | printf(" RWH Policy : "); | |
c2462068 | 1830 | if (dev->rwh_policy == RWH_OFF || dev->rwh_policy == RWH_MULTIPLE_OFF) |
2432ce9b AP |
1831 | printf("off\n"); |
1832 | else if (dev->rwh_policy == RWH_DISTRIBUTED) | |
1833 | printf("PPL distributed\n"); | |
1834 | else if (dev->rwh_policy == RWH_JOURNALING_DRIVE) | |
1835 | printf("PPL journaling drive\n"); | |
c2462068 PB |
1836 | else if (dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED) |
1837 | printf("Multiple distributed PPLs\n"); | |
1838 | else if (dev->rwh_policy == RWH_MULTIPLE_PPLS_JOURNALING_DRIVE) | |
1839 | printf("Multiple PPLs on journaling drive\n"); | |
fbc42556 JR |
1840 | else if (dev->rwh_policy == RWH_BITMAP) |
1841 | printf("Write-intent bitmap\n"); | |
2432ce9b AP |
1842 | else |
1843 | printf("<unknown:%d>\n", dev->rwh_policy); | |
ba1b3bc8 AP |
1844 | |
1845 | printf(" Volume ID : %u\n", dev->my_vol_raid_dev_num); | |
cdddbdbc DW |
1846 | } |
1847 | ||
ef5c214e MK |
1848 | static void print_imsm_disk(struct imsm_disk *disk, |
1849 | int index, | |
1850 | __u32 reserved, | |
1851 | unsigned int sector_size) { | |
1f24f035 | 1852 | char str[MAX_RAID_SERIAL_LEN + 1]; |
cdddbdbc DW |
1853 | __u64 sz; |
1854 | ||
0ec1f4e8 | 1855 | if (index < -1 || !disk) |
e9d82038 DW |
1856 | return; |
1857 | ||
cdddbdbc | 1858 | printf("\n"); |
1f24f035 | 1859 | snprintf(str, MAX_RAID_SERIAL_LEN + 1, "%s", disk->serial); |
0ec1f4e8 DW |
1860 | if (index >= 0) |
1861 | printf(" Disk%02d Serial : %s\n", index, str); | |
1862 | else | |
1863 | printf(" Disk Serial : %s\n", str); | |
2432ce9b AP |
1864 | printf(" State :%s%s%s%s\n", is_spare(disk) ? " spare" : "", |
1865 | is_configured(disk) ? " active" : "", | |
1866 | is_failed(disk) ? " failed" : "", | |
1867 | is_journal(disk) ? " journal" : ""); | |
cdddbdbc | 1868 | printf(" Id : %08x\n", __le32_to_cpu(disk->scsi_id)); |
5551b113 | 1869 | sz = total_blocks(disk) - reserved; |
ef5c214e MK |
1870 | printf(" Usable Size : %llu%s\n", |
1871 | (unsigned long long)sz * 512 / sector_size, | |
cdddbdbc DW |
1872 | human_size(sz * 512)); |
1873 | } | |
1874 | ||
de44e46f PB |
1875 | void convert_to_4k_imsm_migr_rec(struct intel_super *super) |
1876 | { | |
1877 | struct migr_record *migr_rec = super->migr_rec; | |
1878 | ||
1879 | migr_rec->blocks_per_unit /= IMSM_4K_DIV; | |
de44e46f PB |
1880 | migr_rec->dest_depth_per_unit /= IMSM_4K_DIV; |
1881 | split_ull((join_u32(migr_rec->post_migr_vol_cap, | |
1882 | migr_rec->post_migr_vol_cap_hi) / IMSM_4K_DIV), | |
1883 | &migr_rec->post_migr_vol_cap, &migr_rec->post_migr_vol_cap_hi); | |
9f421827 PB |
1884 | set_migr_chkp_area_pba(migr_rec, |
1885 | migr_chkp_area_pba(migr_rec) / IMSM_4K_DIV); | |
1886 | set_migr_dest_1st_member_lba(migr_rec, | |
1887 | migr_dest_1st_member_lba(migr_rec) / IMSM_4K_DIV); | |
de44e46f PB |
1888 | } |
1889 | ||
f36a9ecd PB |
1890 | void convert_to_4k_imsm_disk(struct imsm_disk *disk) |
1891 | { | |
1892 | set_total_blocks(disk, (total_blocks(disk)/IMSM_4K_DIV)); | |
1893 | } | |
1894 | ||
1895 | void convert_to_4k(struct intel_super *super) | |
1896 | { | |
1897 | struct imsm_super *mpb = super->anchor; | |
1898 | struct imsm_disk *disk; | |
1899 | int i; | |
e4467bc7 | 1900 | __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size); |
f36a9ecd PB |
1901 | |
1902 | for (i = 0; i < mpb->num_disks ; i++) { | |
1903 | disk = __get_imsm_disk(mpb, i); | |
1904 | /* disk */ | |
1905 | convert_to_4k_imsm_disk(disk); | |
1906 | } | |
1907 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
1908 | struct imsm_dev *dev = __get_imsm_dev(mpb, i); | |
1909 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
1910 | /* dev */ | |
fcc2c9da | 1911 | set_imsm_dev_size(dev, imsm_dev_size(dev)/IMSM_4K_DIV); |
4036e7ee MT |
1912 | set_vol_curr_migr_unit(dev, |
1913 | vol_curr_migr_unit(dev) / IMSM_4K_DIV); | |
f36a9ecd PB |
1914 | |
1915 | /* map0 */ | |
1916 | set_blocks_per_member(map, blocks_per_member(map)/IMSM_4K_DIV); | |
1917 | map->blocks_per_strip /= IMSM_4K_DIV; | |
1918 | set_pba_of_lba0(map, pba_of_lba0(map)/IMSM_4K_DIV); | |
1919 | ||
1920 | if (dev->vol.migr_state) { | |
1921 | /* map1 */ | |
1922 | map = get_imsm_map(dev, MAP_1); | |
1923 | set_blocks_per_member(map, | |
1924 | blocks_per_member(map)/IMSM_4K_DIV); | |
1925 | map->blocks_per_strip /= IMSM_4K_DIV; | |
1926 | set_pba_of_lba0(map, pba_of_lba0(map)/IMSM_4K_DIV); | |
1927 | } | |
1928 | } | |
e4467bc7 TM |
1929 | if (bbm_log_size) { |
1930 | struct bbm_log *log = (void *)mpb + | |
1931 | __le32_to_cpu(mpb->mpb_size) - bbm_log_size; | |
1932 | __u32 i; | |
1933 | ||
1934 | for (i = 0; i < log->entry_count; i++) { | |
1935 | struct bbm_log_entry *entry = | |
1936 | &log->marked_block_entries[i]; | |
1937 | ||
1938 | __u8 count = entry->marked_count + 1; | |
1939 | unsigned long long sector = | |
1940 | __le48_to_cpu(&entry->defective_block_start); | |
1941 | ||
1942 | entry->defective_block_start = | |
1943 | __cpu_to_le48(sector/IMSM_4K_DIV); | |
1944 | entry->marked_count = max(count/IMSM_4K_DIV, 1) - 1; | |
1945 | } | |
1946 | } | |
f36a9ecd PB |
1947 | |
1948 | mpb->check_sum = __gen_imsm_checksum(mpb); | |
1949 | } | |
1950 | ||
520e69e2 AK |
1951 | void examine_migr_rec_imsm(struct intel_super *super) |
1952 | { | |
1953 | struct migr_record *migr_rec = super->migr_rec; | |
1954 | struct imsm_super *mpb = super->anchor; | |
1955 | int i; | |
1956 | ||
1957 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
1958 | struct imsm_dev *dev = __get_imsm_dev(mpb, i); | |
3136abe5 | 1959 | struct imsm_map *map; |
b4ab44d8 | 1960 | int slot = -1; |
3136abe5 | 1961 | |
195d1d76 | 1962 | if (is_gen_migration(dev) == false) |
520e69e2 AK |
1963 | continue; |
1964 | ||
1965 | printf("\nMigration Record Information:"); | |
3136abe5 | 1966 | |
44bfe6df AK |
1967 | /* first map under migration */ |
1968 | map = get_imsm_map(dev, MAP_0); | |
76c152ca | 1969 | |
3136abe5 AK |
1970 | if (map) |
1971 | slot = get_imsm_disk_slot(map, super->disks->index); | |
089f9d79 | 1972 | if (map == NULL || slot > 1 || slot < 0) { |
520e69e2 AK |
1973 | printf(" Empty\n "); |
1974 | printf("Examine one of first two disks in array\n"); | |
1975 | break; | |
1976 | } | |
1977 | printf("\n Status : "); | |
1978 | if (__le32_to_cpu(migr_rec->rec_status) == UNIT_SRC_NORMAL) | |
1979 | printf("Normal\n"); | |
1980 | else | |
1981 | printf("Contains Data\n"); | |
9f421827 PB |
1982 | printf(" Current Unit : %llu\n", |
1983 | current_migr_unit(migr_rec)); | |
520e69e2 AK |
1984 | printf(" Family : %u\n", |
1985 | __le32_to_cpu(migr_rec->family_num)); | |
1986 | printf(" Ascending : %u\n", | |
1987 | __le32_to_cpu(migr_rec->ascending_migr)); | |
1988 | printf(" Blocks Per Unit : %u\n", | |
1989 | __le32_to_cpu(migr_rec->blocks_per_unit)); | |
1990 | printf(" Dest. Depth Per Unit : %u\n", | |
1991 | __le32_to_cpu(migr_rec->dest_depth_per_unit)); | |
9f421827 PB |
1992 | printf(" Checkpoint Area pba : %llu\n", |
1993 | migr_chkp_area_pba(migr_rec)); | |
1994 | printf(" First member lba : %llu\n", | |
1995 | migr_dest_1st_member_lba(migr_rec)); | |
1996 | printf(" Total Number of Units : %llu\n", | |
1997 | get_num_migr_units(migr_rec)); | |
1998 | printf(" Size of volume : %llu\n", | |
1999 | join_u32(migr_rec->post_migr_vol_cap, | |
2000 | migr_rec->post_migr_vol_cap_hi)); | |
520e69e2 AK |
2001 | printf(" Record was read from : %u\n", |
2002 | __le32_to_cpu(migr_rec->ckpt_read_disk_num)); | |
2003 | ||
2004 | break; | |
2005 | } | |
2006 | } | |
f36a9ecd | 2007 | |
de44e46f PB |
2008 | void convert_from_4k_imsm_migr_rec(struct intel_super *super) |
2009 | { | |
2010 | struct migr_record *migr_rec = super->migr_rec; | |
2011 | ||
2012 | migr_rec->blocks_per_unit *= IMSM_4K_DIV; | |
de44e46f PB |
2013 | migr_rec->dest_depth_per_unit *= IMSM_4K_DIV; |
2014 | split_ull((join_u32(migr_rec->post_migr_vol_cap, | |
2015 | migr_rec->post_migr_vol_cap_hi) * IMSM_4K_DIV), | |
2016 | &migr_rec->post_migr_vol_cap, | |
2017 | &migr_rec->post_migr_vol_cap_hi); | |
9f421827 PB |
2018 | set_migr_chkp_area_pba(migr_rec, |
2019 | migr_chkp_area_pba(migr_rec) * IMSM_4K_DIV); | |
2020 | set_migr_dest_1st_member_lba(migr_rec, | |
2021 | migr_dest_1st_member_lba(migr_rec) * IMSM_4K_DIV); | |
de44e46f PB |
2022 | } |
2023 | ||
f36a9ecd PB |
2024 | void convert_from_4k(struct intel_super *super) |
2025 | { | |
2026 | struct imsm_super *mpb = super->anchor; | |
2027 | struct imsm_disk *disk; | |
2028 | int i; | |
e4467bc7 | 2029 | __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size); |
f36a9ecd PB |
2030 | |
2031 | for (i = 0; i < mpb->num_disks ; i++) { | |
2032 | disk = __get_imsm_disk(mpb, i); | |
2033 | /* disk */ | |
2034 | set_total_blocks(disk, (total_blocks(disk)*IMSM_4K_DIV)); | |
2035 | } | |
2036 | ||
2037 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
2038 | struct imsm_dev *dev = __get_imsm_dev(mpb, i); | |
2039 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
2040 | /* dev */ | |
fcc2c9da | 2041 | set_imsm_dev_size(dev, imsm_dev_size(dev)*IMSM_4K_DIV); |
4036e7ee MT |
2042 | set_vol_curr_migr_unit(dev, |
2043 | vol_curr_migr_unit(dev) * IMSM_4K_DIV); | |
f36a9ecd PB |
2044 | |
2045 | /* map0 */ | |
2046 | set_blocks_per_member(map, blocks_per_member(map)*IMSM_4K_DIV); | |
2047 | map->blocks_per_strip *= IMSM_4K_DIV; | |
2048 | set_pba_of_lba0(map, pba_of_lba0(map)*IMSM_4K_DIV); | |
2049 | ||
2050 | if (dev->vol.migr_state) { | |
2051 | /* map1 */ | |
2052 | map = get_imsm_map(dev, MAP_1); | |
2053 | set_blocks_per_member(map, | |
2054 | blocks_per_member(map)*IMSM_4K_DIV); | |
2055 | map->blocks_per_strip *= IMSM_4K_DIV; | |
2056 | set_pba_of_lba0(map, pba_of_lba0(map)*IMSM_4K_DIV); | |
2057 | } | |
2058 | } | |
e4467bc7 TM |
2059 | if (bbm_log_size) { |
2060 | struct bbm_log *log = (void *)mpb + | |
2061 | __le32_to_cpu(mpb->mpb_size) - bbm_log_size; | |
2062 | __u32 i; | |
2063 | ||
2064 | for (i = 0; i < log->entry_count; i++) { | |
2065 | struct bbm_log_entry *entry = | |
2066 | &log->marked_block_entries[i]; | |
2067 | ||
2068 | __u8 count = entry->marked_count + 1; | |
2069 | unsigned long long sector = | |
2070 | __le48_to_cpu(&entry->defective_block_start); | |
2071 | ||
2072 | entry->defective_block_start = | |
2073 | __cpu_to_le48(sector*IMSM_4K_DIV); | |
2074 | entry->marked_count = count*IMSM_4K_DIV - 1; | |
2075 | } | |
2076 | } | |
f36a9ecd PB |
2077 | |
2078 | mpb->check_sum = __gen_imsm_checksum(mpb); | |
2079 | } | |
2080 | ||
19482bcc AK |
2081 | /******************************************************************************* |
2082 | * function: imsm_check_attributes | |
2083 | * Description: Function checks if features represented by attributes flags | |
1011e834 | 2084 | * are supported by mdadm. |
19482bcc AK |
2085 | * Parameters: |
2086 | * attributes - Attributes read from metadata | |
2087 | * Returns: | |
1011e834 N |
2088 | * 0 - passed attributes contains unsupported features flags |
2089 | * 1 - all features are supported | |
19482bcc AK |
2090 | ******************************************************************************/ |
2091 | static int imsm_check_attributes(__u32 attributes) | |
2092 | { | |
2093 | int ret_val = 1; | |
418f9b36 N |
2094 | __u32 not_supported = MPB_ATTRIB_SUPPORTED^0xffffffff; |
2095 | ||
2096 | not_supported &= ~MPB_ATTRIB_IGNORED; | |
19482bcc AK |
2097 | |
2098 | not_supported &= attributes; | |
2099 | if (not_supported) { | |
e7b84f9d | 2100 | pr_err("(IMSM): Unsupported attributes : %x\n", |
418f9b36 | 2101 | (unsigned)__le32_to_cpu(not_supported)); |
19482bcc AK |
2102 | if (not_supported & MPB_ATTRIB_CHECKSUM_VERIFY) { |
2103 | dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n"); | |
2104 | not_supported ^= MPB_ATTRIB_CHECKSUM_VERIFY; | |
2105 | } | |
2106 | if (not_supported & MPB_ATTRIB_2TB) { | |
2107 | dprintf("\t\tMPB_ATTRIB_2TB\n"); | |
2108 | not_supported ^= MPB_ATTRIB_2TB; | |
2109 | } | |
2110 | if (not_supported & MPB_ATTRIB_RAID0) { | |
2111 | dprintf("\t\tMPB_ATTRIB_RAID0\n"); | |
2112 | not_supported ^= MPB_ATTRIB_RAID0; | |
2113 | } | |
2114 | if (not_supported & MPB_ATTRIB_RAID1) { | |
2115 | dprintf("\t\tMPB_ATTRIB_RAID1\n"); | |
2116 | not_supported ^= MPB_ATTRIB_RAID1; | |
2117 | } | |
2118 | if (not_supported & MPB_ATTRIB_RAID10) { | |
2119 | dprintf("\t\tMPB_ATTRIB_RAID10\n"); | |
2120 | not_supported ^= MPB_ATTRIB_RAID10; | |
2121 | } | |
2122 | if (not_supported & MPB_ATTRIB_RAID1E) { | |
2123 | dprintf("\t\tMPB_ATTRIB_RAID1E\n"); | |
2124 | not_supported ^= MPB_ATTRIB_RAID1E; | |
2125 | } | |
2126 | if (not_supported & MPB_ATTRIB_RAID5) { | |
2127 | dprintf("\t\tMPB_ATTRIB_RAID5\n"); | |
2128 | not_supported ^= MPB_ATTRIB_RAID5; | |
2129 | } | |
2130 | if (not_supported & MPB_ATTRIB_RAIDCNG) { | |
2131 | dprintf("\t\tMPB_ATTRIB_RAIDCNG\n"); | |
2132 | not_supported ^= MPB_ATTRIB_RAIDCNG; | |
2133 | } | |
2134 | if (not_supported & MPB_ATTRIB_BBM) { | |
2135 | dprintf("\t\tMPB_ATTRIB_BBM\n"); | |
2136 | not_supported ^= MPB_ATTRIB_BBM; | |
2137 | } | |
2138 | if (not_supported & MPB_ATTRIB_CHECKSUM_VERIFY) { | |
2139 | dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n"); | |
2140 | not_supported ^= MPB_ATTRIB_CHECKSUM_VERIFY; | |
2141 | } | |
2142 | if (not_supported & MPB_ATTRIB_EXP_STRIPE_SIZE) { | |
2143 | dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n"); | |
2144 | not_supported ^= MPB_ATTRIB_EXP_STRIPE_SIZE; | |
2145 | } | |
2146 | if (not_supported & MPB_ATTRIB_2TB_DISK) { | |
2147 | dprintf("\t\tMPB_ATTRIB_2TB_DISK\n"); | |
2148 | not_supported ^= MPB_ATTRIB_2TB_DISK; | |
2149 | } | |
2150 | if (not_supported & MPB_ATTRIB_NEVER_USE2) { | |
2151 | dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n"); | |
2152 | not_supported ^= MPB_ATTRIB_NEVER_USE2; | |
2153 | } | |
2154 | if (not_supported & MPB_ATTRIB_NEVER_USE) { | |
2155 | dprintf("\t\tMPB_ATTRIB_NEVER_USE\n"); | |
2156 | not_supported ^= MPB_ATTRIB_NEVER_USE; | |
2157 | } | |
2158 | ||
2159 | if (not_supported) | |
1ade5cc1 | 2160 | dprintf("(IMSM): Unknown attributes : %x\n", not_supported); |
19482bcc AK |
2161 | |
2162 | ret_val = 0; | |
2163 | } | |
2164 | ||
2165 | return ret_val; | |
2166 | } | |
2167 | ||
a5d85af7 | 2168 | static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map); |
44470971 | 2169 | |
cdddbdbc DW |
2170 | static void examine_super_imsm(struct supertype *st, char *homehost) |
2171 | { | |
2172 | struct intel_super *super = st->sb; | |
949c47a0 | 2173 | struct imsm_super *mpb = super->anchor; |
cdddbdbc DW |
2174 | char str[MAX_SIGNATURE_LENGTH]; |
2175 | int i; | |
27fd6274 DW |
2176 | struct mdinfo info; |
2177 | char nbuf[64]; | |
cdddbdbc | 2178 | __u32 sum; |
14e8215b | 2179 | __u32 reserved = imsm_reserved_sectors(super, super->disks); |
94827db3 | 2180 | struct dl *dl; |
e48aed3c | 2181 | time_t creation_time; |
27fd6274 | 2182 | |
618f4e6d XN |
2183 | strncpy(str, (char *)mpb->sig, MPB_SIG_LEN); |
2184 | str[MPB_SIG_LEN-1] = '\0'; | |
cdddbdbc | 2185 | printf(" Magic : %s\n", str); |
cdddbdbc | 2186 | printf(" Version : %s\n", get_imsm_version(mpb)); |
148acb7b | 2187 | printf(" Orig Family : %08x\n", __le32_to_cpu(mpb->orig_family_num)); |
cdddbdbc DW |
2188 | printf(" Family : %08x\n", __le32_to_cpu(mpb->family_num)); |
2189 | printf(" Generation : %08x\n", __le32_to_cpu(mpb->generation_num)); | |
e48aed3c AP |
2190 | creation_time = __le64_to_cpu(mpb->creation_time); |
2191 | printf(" Creation Time : %.24s\n", | |
2192 | creation_time ? ctime(&creation_time) : "Unknown"); | |
19482bcc AK |
2193 | printf(" Attributes : "); |
2194 | if (imsm_check_attributes(mpb->attributes)) | |
2195 | printf("All supported\n"); | |
2196 | else | |
2197 | printf("not supported\n"); | |
a5d85af7 | 2198 | getinfo_super_imsm(st, &info, NULL); |
ae2bfd4e | 2199 | fname_from_uuid(st, &info, nbuf, ':'); |
27fd6274 | 2200 | printf(" UUID : %s\n", nbuf + 5); |
cdddbdbc DW |
2201 | sum = __le32_to_cpu(mpb->check_sum); |
2202 | printf(" Checksum : %08x %s\n", sum, | |
949c47a0 | 2203 | __gen_imsm_checksum(mpb) == sum ? "correct" : "incorrect"); |
f36a9ecd | 2204 | printf(" MPB Sectors : %d\n", mpb_sectors(mpb, super->sector_size)); |
cdddbdbc DW |
2205 | printf(" Disks : %d\n", mpb->num_disks); |
2206 | printf(" RAID Devices : %d\n", mpb->num_raid_devs); | |
ef5c214e MK |
2207 | print_imsm_disk(__get_imsm_disk(mpb, super->disks->index), |
2208 | super->disks->index, reserved, super->sector_size); | |
8d67477f | 2209 | if (get_imsm_bbm_log_size(super->bbm_log)) { |
604b746f JD |
2210 | struct bbm_log *log = super->bbm_log; |
2211 | ||
2212 | printf("\n"); | |
2213 | printf("Bad Block Management Log:\n"); | |
2214 | printf(" Log Size : %d\n", __le32_to_cpu(mpb->bbm_log_size)); | |
2215 | printf(" Signature : %x\n", __le32_to_cpu(log->signature)); | |
2216 | printf(" Entry Count : %d\n", __le32_to_cpu(log->entry_count)); | |
604b746f | 2217 | } |
44470971 DW |
2218 | for (i = 0; i < mpb->num_raid_devs; i++) { |
2219 | struct mdinfo info; | |
2220 | struct imsm_dev *dev = __get_imsm_dev(mpb, i); | |
2221 | ||
2222 | super->current_vol = i; | |
a5d85af7 | 2223 | getinfo_super_imsm(st, &info, NULL); |
ae2bfd4e | 2224 | fname_from_uuid(st, &info, nbuf, ':'); |
c47b0ff6 | 2225 | print_imsm_dev(super, dev, nbuf + 5, super->disks->index); |
44470971 | 2226 | } |
cdddbdbc DW |
2227 | for (i = 0; i < mpb->num_disks; i++) { |
2228 | if (i == super->disks->index) | |
2229 | continue; | |
ef5c214e MK |
2230 | print_imsm_disk(__get_imsm_disk(mpb, i), i, reserved, |
2231 | super->sector_size); | |
cdddbdbc | 2232 | } |
94827db3 | 2233 | |
0ec1f4e8 DW |
2234 | for (dl = super->disks; dl; dl = dl->next) |
2235 | if (dl->index == -1) | |
ef5c214e MK |
2236 | print_imsm_disk(&dl->disk, -1, reserved, |
2237 | super->sector_size); | |
520e69e2 AK |
2238 | |
2239 | examine_migr_rec_imsm(super); | |
cdddbdbc DW |
2240 | } |
2241 | ||
061f2c6a | 2242 | static void brief_examine_super_imsm(struct supertype *st, int verbose) |
cdddbdbc | 2243 | { |
27fd6274 | 2244 | /* We just write a generic IMSM ARRAY entry */ |
ff54de6e N |
2245 | struct mdinfo info; |
2246 | char nbuf[64]; | |
2247 | ||
a5d85af7 | 2248 | getinfo_super_imsm(st, &info, NULL); |
4737ae25 N |
2249 | fname_from_uuid(st, &info, nbuf, ':'); |
2250 | printf("ARRAY metadata=imsm UUID=%s\n", nbuf + 5); | |
2251 | } | |
2252 | ||
2253 | static void brief_examine_subarrays_imsm(struct supertype *st, int verbose) | |
2254 | { | |
2255 | /* We just write a generic IMSM ARRAY entry */ | |
2256 | struct mdinfo info; | |
2257 | char nbuf[64]; | |
2258 | char nbuf1[64]; | |
2259 | struct intel_super *super = st->sb; | |
2260 | int i; | |
2261 | ||
2262 | if (!super->anchor->num_raid_devs) | |
2263 | return; | |
2264 | ||
a5d85af7 | 2265 | getinfo_super_imsm(st, &info, NULL); |
ae2bfd4e | 2266 | fname_from_uuid(st, &info, nbuf, ':'); |
1e7bc0ed DW |
2267 | for (i = 0; i < super->anchor->num_raid_devs; i++) { |
2268 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
2269 | ||
2270 | super->current_vol = i; | |
a5d85af7 | 2271 | getinfo_super_imsm(st, &info, NULL); |
ae2bfd4e | 2272 | fname_from_uuid(st, &info, nbuf1, ':'); |
1124b3cf | 2273 | printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n", |
cf8de691 | 2274 | dev->volume, nbuf + 5, i, nbuf1 + 5); |
1e7bc0ed | 2275 | } |
cdddbdbc DW |
2276 | } |
2277 | ||
9d84c8ea DW |
2278 | static void export_examine_super_imsm(struct supertype *st) |
2279 | { | |
2280 | struct intel_super *super = st->sb; | |
2281 | struct imsm_super *mpb = super->anchor; | |
2282 | struct mdinfo info; | |
2283 | char nbuf[64]; | |
2284 | ||
a5d85af7 | 2285 | getinfo_super_imsm(st, &info, NULL); |
9d84c8ea DW |
2286 | fname_from_uuid(st, &info, nbuf, ':'); |
2287 | printf("MD_METADATA=imsm\n"); | |
2288 | printf("MD_LEVEL=container\n"); | |
2289 | printf("MD_UUID=%s\n", nbuf+5); | |
2290 | printf("MD_DEVICES=%u\n", mpb->num_disks); | |
e48aed3c | 2291 | printf("MD_CREATION_TIME=%llu\n", __le64_to_cpu(mpb->creation_time)); |
9d84c8ea DW |
2292 | } |
2293 | ||
b771faef BK |
2294 | static void detail_super_imsm(struct supertype *st, char *homehost, |
2295 | char *subarray) | |
cdddbdbc | 2296 | { |
3ebe00a1 DW |
2297 | struct mdinfo info; |
2298 | char nbuf[64]; | |
b771faef BK |
2299 | struct intel_super *super = st->sb; |
2300 | int temp_vol = super->current_vol; | |
2301 | ||
2302 | if (subarray) | |
2303 | super->current_vol = strtoul(subarray, NULL, 10); | |
3ebe00a1 | 2304 | |
a5d85af7 | 2305 | getinfo_super_imsm(st, &info, NULL); |
ae2bfd4e | 2306 | fname_from_uuid(st, &info, nbuf, ':'); |
65884368 | 2307 | printf("\n UUID : %s\n", nbuf + 5); |
b771faef BK |
2308 | |
2309 | super->current_vol = temp_vol; | |
cdddbdbc DW |
2310 | } |
2311 | ||
b771faef | 2312 | static void brief_detail_super_imsm(struct supertype *st, char *subarray) |
cdddbdbc | 2313 | { |
ff54de6e N |
2314 | struct mdinfo info; |
2315 | char nbuf[64]; | |
b771faef BK |
2316 | struct intel_super *super = st->sb; |
2317 | int temp_vol = super->current_vol; | |
2318 | ||
2319 | if (subarray) | |
2320 | super->current_vol = strtoul(subarray, NULL, 10); | |
2321 | ||
a5d85af7 | 2322 | getinfo_super_imsm(st, &info, NULL); |
ae2bfd4e | 2323 | fname_from_uuid(st, &info, nbuf, ':'); |
ff54de6e | 2324 | printf(" UUID=%s", nbuf + 5); |
b771faef BK |
2325 | |
2326 | super->current_vol = temp_vol; | |
cdddbdbc | 2327 | } |
d665cc31 | 2328 | |
6da53c0e BK |
2329 | static int imsm_read_serial(int fd, char *devname, __u8 *serial, |
2330 | size_t serial_buf_len); | |
d665cc31 DW |
2331 | static void fd2devname(int fd, char *name); |
2332 | ||
120dc887 | 2333 | static int ahci_enumerate_ports(const char *hba_path, int port_count, int host_base, int verbose) |
d665cc31 | 2334 | { |
120dc887 LM |
2335 | /* dump an unsorted list of devices attached to AHCI Intel storage |
2336 | * controller, as well as non-connected ports | |
d665cc31 DW |
2337 | */ |
2338 | int hba_len = strlen(hba_path) + 1; | |
2339 | struct dirent *ent; | |
2340 | DIR *dir; | |
2341 | char *path = NULL; | |
2342 | int err = 0; | |
2343 | unsigned long port_mask = (1 << port_count) - 1; | |
2344 | ||
f21e18ca | 2345 | if (port_count > (int)sizeof(port_mask) * 8) { |
ba728be7 | 2346 | if (verbose > 0) |
e7b84f9d | 2347 | pr_err("port_count %d out of range\n", port_count); |
d665cc31 DW |
2348 | return 2; |
2349 | } | |
2350 | ||
2351 | /* scroll through /sys/dev/block looking for devices attached to | |
2352 | * this hba | |
2353 | */ | |
2354 | dir = opendir("/sys/dev/block"); | |
1a6dd6b9 PB |
2355 | if (!dir) |
2356 | return 1; | |
2357 | ||
2358 | for (ent = readdir(dir); ent; ent = readdir(dir)) { | |
d665cc31 DW |
2359 | int fd; |
2360 | char model[64]; | |
2361 | char vendor[64]; | |
2362 | char buf[1024]; | |
2363 | int major, minor; | |
fcebeb77 | 2364 | char device[PATH_MAX]; |
d665cc31 DW |
2365 | char *c; |
2366 | int port; | |
2367 | int type; | |
2368 | ||
2369 | if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2) | |
2370 | continue; | |
7c798f87 | 2371 | path = devt_to_devpath(makedev(major, minor), 1, NULL); |
d665cc31 DW |
2372 | if (!path) |
2373 | continue; | |
2374 | if (!path_attached_to_hba(path, hba_path)) { | |
2375 | free(path); | |
2376 | path = NULL; | |
2377 | continue; | |
2378 | } | |
2379 | ||
fcebeb77 MT |
2380 | /* retrieve the scsi device */ |
2381 | if (!devt_to_devpath(makedev(major, minor), 1, device)) { | |
ba728be7 | 2382 | if (verbose > 0) |
fcebeb77 | 2383 | pr_err("failed to get device\n"); |
d665cc31 DW |
2384 | err = 2; |
2385 | break; | |
2386 | } | |
fcebeb77 | 2387 | if (devpath_to_char(device, "type", buf, sizeof(buf), 0)) { |
d665cc31 | 2388 | err = 2; |
d665cc31 DW |
2389 | break; |
2390 | } | |
2391 | type = strtoul(buf, NULL, 10); | |
2392 | ||
2393 | /* if it's not a disk print the vendor and model */ | |
2394 | if (!(type == 0 || type == 7 || type == 14)) { | |
2395 | vendor[0] = '\0'; | |
2396 | model[0] = '\0'; | |
fcebeb77 MT |
2397 | |
2398 | if (devpath_to_char(device, "vendor", buf, | |
2399 | sizeof(buf), 0) == 0) { | |
d665cc31 DW |
2400 | strncpy(vendor, buf, sizeof(vendor)); |
2401 | vendor[sizeof(vendor) - 1] = '\0'; | |
2402 | c = (char *) &vendor[sizeof(vendor) - 1]; | |
2403 | while (isspace(*c) || *c == '\0') | |
2404 | *c-- = '\0'; | |
2405 | ||
2406 | } | |
fcebeb77 MT |
2407 | |
2408 | if (devpath_to_char(device, "model", buf, | |
2409 | sizeof(buf), 0) == 0) { | |
d665cc31 DW |
2410 | strncpy(model, buf, sizeof(model)); |
2411 | model[sizeof(model) - 1] = '\0'; | |
2412 | c = (char *) &model[sizeof(model) - 1]; | |
2413 | while (isspace(*c) || *c == '\0') | |
2414 | *c-- = '\0'; | |
2415 | } | |
2416 | ||
2417 | if (vendor[0] && model[0]) | |
2418 | sprintf(buf, "%.64s %.64s", vendor, model); | |
2419 | else | |
2420 | switch (type) { /* numbers from hald/linux/device.c */ | |
2421 | case 1: sprintf(buf, "tape"); break; | |
2422 | case 2: sprintf(buf, "printer"); break; | |
2423 | case 3: sprintf(buf, "processor"); break; | |
2424 | case 4: | |
2425 | case 5: sprintf(buf, "cdrom"); break; | |
2426 | case 6: sprintf(buf, "scanner"); break; | |
2427 | case 8: sprintf(buf, "media_changer"); break; | |
2428 | case 9: sprintf(buf, "comm"); break; | |
2429 | case 12: sprintf(buf, "raid"); break; | |
2430 | default: sprintf(buf, "unknown"); | |
2431 | } | |
2432 | } else | |
2433 | buf[0] = '\0'; | |
d665cc31 DW |
2434 | |
2435 | /* chop device path to 'host%d' and calculate the port number */ | |
2436 | c = strchr(&path[hba_len], '/'); | |
4e5e717d | 2437 | if (!c) { |
ba728be7 | 2438 | if (verbose > 0) |
e7b84f9d | 2439 | pr_err("%s - invalid path name\n", path + hba_len); |
4e5e717d AW |
2440 | err = 2; |
2441 | break; | |
2442 | } | |
d665cc31 | 2443 | *c = '\0'; |
0858eccf AP |
2444 | if ((sscanf(&path[hba_len], "ata%d", &port) == 1) || |
2445 | ((sscanf(&path[hba_len], "host%d", &port) == 1))) | |
d665cc31 DW |
2446 | port -= host_base; |
2447 | else { | |
ba728be7 | 2448 | if (verbose > 0) { |
d665cc31 | 2449 | *c = '/'; /* repair the full string */ |
e7b84f9d | 2450 | pr_err("failed to determine port number for %s\n", |
d665cc31 DW |
2451 | path); |
2452 | } | |
2453 | err = 2; | |
2454 | break; | |
2455 | } | |
2456 | ||
2457 | /* mark this port as used */ | |
2458 | port_mask &= ~(1 << port); | |
2459 | ||
2460 | /* print out the device information */ | |
2461 | if (buf[0]) { | |
2462 | printf(" Port%d : - non-disk device (%s) -\n", port, buf); | |
2463 | continue; | |
2464 | } | |
2465 | ||
2466 | fd = dev_open(ent->d_name, O_RDONLY); | |
4389ce73 | 2467 | if (!is_fd_valid(fd)) |
d665cc31 DW |
2468 | printf(" Port%d : - disk info unavailable -\n", port); |
2469 | else { | |
2470 | fd2devname(fd, buf); | |
2471 | printf(" Port%d : %s", port, buf); | |
6da53c0e BK |
2472 | if (imsm_read_serial(fd, NULL, (__u8 *)buf, |
2473 | sizeof(buf)) == 0) | |
2474 | printf(" (%s)\n", buf); | |
d665cc31 | 2475 | else |
664d5325 | 2476 | printf(" ()\n"); |
4dab422a | 2477 | close(fd); |
d665cc31 | 2478 | } |
d665cc31 DW |
2479 | free(path); |
2480 | path = NULL; | |
2481 | } | |
2482 | if (path) | |
2483 | free(path); | |
2484 | if (dir) | |
2485 | closedir(dir); | |
2486 | if (err == 0) { | |
2487 | int i; | |
2488 | ||
2489 | for (i = 0; i < port_count; i++) | |
2490 | if (port_mask & (1 << i)) | |
2491 | printf(" Port%d : - no device attached -\n", i); | |
2492 | } | |
2493 | ||
2494 | return err; | |
2495 | } | |
2496 | ||
6da53c0e | 2497 | static int print_nvme_info(struct sys_dev *hba) |
60f0f54d PB |
2498 | { |
2499 | struct dirent *ent; | |
2500 | DIR *dir; | |
60f0f54d | 2501 | |
6da53c0e | 2502 | dir = opendir("/sys/block/"); |
b9135011 | 2503 | if (!dir) |
b5eece69 | 2504 | return 1; |
b9135011 JS |
2505 | |
2506 | for (ent = readdir(dir); ent; ent = readdir(dir)) { | |
8662f92d MT |
2507 | char ns_path[PATH_MAX]; |
2508 | char cntrl_path[PATH_MAX]; | |
2509 | char buf[PATH_MAX]; | |
2510 | int fd = -1; | |
60f0f54d | 2511 | |
8662f92d MT |
2512 | if (!strstr(ent->d_name, "nvme")) |
2513 | goto skip; | |
d835518b | 2514 | |
8662f92d | 2515 | fd = open_dev(ent->d_name); |
4389ce73 | 2516 | if (!is_fd_valid(fd)) |
8662f92d | 2517 | goto skip; |
d835518b | 2518 | |
8662f92d MT |
2519 | if (!diskfd_to_devpath(fd, 0, ns_path) || |
2520 | !diskfd_to_devpath(fd, 1, cntrl_path)) | |
2521 | goto skip; | |
2522 | ||
2523 | if (!path_attached_to_hba(cntrl_path, hba->path)) | |
2524 | goto skip; | |
2525 | ||
2526 | if (!imsm_is_nvme_namespace_supported(fd, 0)) | |
2527 | goto skip; | |
2528 | ||
2529 | fd2devname(fd, buf); | |
2530 | if (hba->type == SYS_DEV_VMD) | |
2531 | printf(" NVMe under VMD : %s", buf); | |
2532 | else if (hba->type == SYS_DEV_NVME) | |
2533 | printf(" NVMe Device : %s", buf); | |
2534 | ||
2535 | if (!imsm_read_serial(fd, NULL, (__u8 *)buf, | |
2536 | sizeof(buf))) | |
2537 | printf(" (%s)\n", buf); | |
2538 | else | |
2539 | printf("()\n"); | |
2540 | ||
2541 | skip: | |
4389ce73 | 2542 | close_fd(&fd); |
60f0f54d PB |
2543 | } |
2544 | ||
b9135011 | 2545 | closedir(dir); |
b5eece69 | 2546 | return 0; |
60f0f54d PB |
2547 | } |
2548 | ||
120dc887 LM |
2549 | static void print_found_intel_controllers(struct sys_dev *elem) |
2550 | { | |
2551 | for (; elem; elem = elem->next) { | |
e7b84f9d | 2552 | pr_err("found Intel(R) "); |
120dc887 LM |
2553 | if (elem->type == SYS_DEV_SATA) |
2554 | fprintf(stderr, "SATA "); | |
155cbb4c LM |
2555 | else if (elem->type == SYS_DEV_SAS) |
2556 | fprintf(stderr, "SAS "); | |
0858eccf AP |
2557 | else if (elem->type == SYS_DEV_NVME) |
2558 | fprintf(stderr, "NVMe "); | |
60f0f54d PB |
2559 | |
2560 | if (elem->type == SYS_DEV_VMD) | |
2561 | fprintf(stderr, "VMD domain"); | |
2562 | else | |
2563 | fprintf(stderr, "RAID controller"); | |
2564 | ||
120dc887 LM |
2565 | if (elem->pci_id) |
2566 | fprintf(stderr, " at %s", elem->pci_id); | |
2567 | fprintf(stderr, ".\n"); | |
2568 | } | |
2569 | fflush(stderr); | |
2570 | } | |
2571 | ||
120dc887 LM |
2572 | static int ahci_get_port_count(const char *hba_path, int *port_count) |
2573 | { | |
2574 | struct dirent *ent; | |
2575 | DIR *dir; | |
2576 | int host_base = -1; | |
2577 | ||
2578 | *port_count = 0; | |
2579 | if ((dir = opendir(hba_path)) == NULL) | |
2580 | return -1; | |
2581 | ||
2582 | for (ent = readdir(dir); ent; ent = readdir(dir)) { | |
2583 | int host; | |
2584 | ||
0858eccf AP |
2585 | if ((sscanf(ent->d_name, "ata%d", &host) != 1) && |
2586 | ((sscanf(ent->d_name, "host%d", &host) != 1))) | |
120dc887 LM |
2587 | continue; |
2588 | if (*port_count == 0) | |
2589 | host_base = host; | |
2590 | else if (host < host_base) | |
2591 | host_base = host; | |
2592 | ||
2593 | if (host + 1 > *port_count + host_base) | |
2594 | *port_count = host + 1 - host_base; | |
2595 | } | |
2596 | closedir(dir); | |
2597 | return host_base; | |
2598 | } | |
2599 | ||
a891a3c2 LM |
2600 | static void print_imsm_capability(const struct imsm_orom *orom) |
2601 | { | |
0858eccf AP |
2602 | printf(" Platform : Intel(R) "); |
2603 | if (orom->capabilities == 0 && orom->driver_features == 0) | |
2604 | printf("Matrix Storage Manager\n"); | |
ab0c6bb9 AP |
2605 | else if (imsm_orom_is_enterprise(orom) && orom->major_ver >= 6) |
2606 | printf("Virtual RAID on CPU\n"); | |
0858eccf AP |
2607 | else |
2608 | printf("Rapid Storage Technology%s\n", | |
2609 | imsm_orom_is_enterprise(orom) ? " enterprise" : ""); | |
2610 | if (orom->major_ver || orom->minor_ver || orom->hotfix_ver || orom->build) | |
2611 | printf(" Version : %d.%d.%d.%d\n", orom->major_ver, | |
2612 | orom->minor_ver, orom->hotfix_ver, orom->build); | |
a891a3c2 LM |
2613 | printf(" RAID Levels :%s%s%s%s%s\n", |
2614 | imsm_orom_has_raid0(orom) ? " raid0" : "", | |
2615 | imsm_orom_has_raid1(orom) ? " raid1" : "", | |
2616 | imsm_orom_has_raid1e(orom) ? " raid1e" : "", | |
2617 | imsm_orom_has_raid10(orom) ? " raid10" : "", | |
2618 | imsm_orom_has_raid5(orom) ? " raid5" : ""); | |
2619 | printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n", | |
2620 | imsm_orom_has_chunk(orom, 2) ? " 2k" : "", | |
2621 | imsm_orom_has_chunk(orom, 4) ? " 4k" : "", | |
2622 | imsm_orom_has_chunk(orom, 8) ? " 8k" : "", | |
2623 | imsm_orom_has_chunk(orom, 16) ? " 16k" : "", | |
2624 | imsm_orom_has_chunk(orom, 32) ? " 32k" : "", | |
2625 | imsm_orom_has_chunk(orom, 64) ? " 64k" : "", | |
2626 | imsm_orom_has_chunk(orom, 128) ? " 128k" : "", | |
2627 | imsm_orom_has_chunk(orom, 256) ? " 256k" : "", | |
2628 | imsm_orom_has_chunk(orom, 512) ? " 512k" : "", | |
2629 | imsm_orom_has_chunk(orom, 1024*1) ? " 1M" : "", | |
2630 | imsm_orom_has_chunk(orom, 1024*2) ? " 2M" : "", | |
2631 | imsm_orom_has_chunk(orom, 1024*4) ? " 4M" : "", | |
2632 | imsm_orom_has_chunk(orom, 1024*8) ? " 8M" : "", | |
2633 | imsm_orom_has_chunk(orom, 1024*16) ? " 16M" : "", | |
2634 | imsm_orom_has_chunk(orom, 1024*32) ? " 32M" : "", | |
2635 | imsm_orom_has_chunk(orom, 1024*64) ? " 64M" : ""); | |
29cd0821 CA |
2636 | printf(" 2TB volumes :%s supported\n", |
2637 | (orom->attr & IMSM_OROM_ATTR_2TB)?"":" not"); | |
2638 | printf(" 2TB disks :%s supported\n", | |
2639 | (orom->attr & IMSM_OROM_ATTR_2TB_DISK)?"":" not"); | |
0e7f69a8 | 2640 | printf(" Max Disks : %d\n", orom->tds); |
0858eccf AP |
2641 | printf(" Max Volumes : %d per array, %d per %s\n", |
2642 | orom->vpa, orom->vphba, | |
2643 | imsm_orom_is_nvme(orom) ? "platform" : "controller"); | |
a891a3c2 LM |
2644 | return; |
2645 | } | |
2646 | ||
e50cf220 MN |
2647 | static void print_imsm_capability_export(const struct imsm_orom *orom) |
2648 | { | |
2649 | printf("MD_FIRMWARE_TYPE=imsm\n"); | |
0858eccf AP |
2650 | if (orom->major_ver || orom->minor_ver || orom->hotfix_ver || orom->build) |
2651 | printf("IMSM_VERSION=%d.%d.%d.%d\n", orom->major_ver, orom->minor_ver, | |
2652 | orom->hotfix_ver, orom->build); | |
e50cf220 MN |
2653 | printf("IMSM_SUPPORTED_RAID_LEVELS=%s%s%s%s%s\n", |
2654 | imsm_orom_has_raid0(orom) ? "raid0 " : "", | |
2655 | imsm_orom_has_raid1(orom) ? "raid1 " : "", | |
2656 | imsm_orom_has_raid1e(orom) ? "raid1e " : "", | |
2657 | imsm_orom_has_raid5(orom) ? "raid10 " : "", | |
2658 | imsm_orom_has_raid10(orom) ? "raid5 " : ""); | |
2659 | printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n", | |
2660 | imsm_orom_has_chunk(orom, 2) ? "2k " : "", | |
2661 | imsm_orom_has_chunk(orom, 4) ? "4k " : "", | |
2662 | imsm_orom_has_chunk(orom, 8) ? "8k " : "", | |
2663 | imsm_orom_has_chunk(orom, 16) ? "16k " : "", | |
2664 | imsm_orom_has_chunk(orom, 32) ? "32k " : "", | |
2665 | imsm_orom_has_chunk(orom, 64) ? "64k " : "", | |
2666 | imsm_orom_has_chunk(orom, 128) ? "128k " : "", | |
2667 | imsm_orom_has_chunk(orom, 256) ? "256k " : "", | |
2668 | imsm_orom_has_chunk(orom, 512) ? "512k " : "", | |
2669 | imsm_orom_has_chunk(orom, 1024*1) ? "1M " : "", | |
2670 | imsm_orom_has_chunk(orom, 1024*2) ? "2M " : "", | |
2671 | imsm_orom_has_chunk(orom, 1024*4) ? "4M " : "", | |
2672 | imsm_orom_has_chunk(orom, 1024*8) ? "8M " : "", | |
2673 | imsm_orom_has_chunk(orom, 1024*16) ? "16M " : "", | |
2674 | imsm_orom_has_chunk(orom, 1024*32) ? "32M " : "", | |
2675 | imsm_orom_has_chunk(orom, 1024*64) ? "64M " : ""); | |
2676 | printf("IMSM_2TB_VOLUMES=%s\n",(orom->attr & IMSM_OROM_ATTR_2TB) ? "yes" : "no"); | |
2677 | printf("IMSM_2TB_DISKS=%s\n",(orom->attr & IMSM_OROM_ATTR_2TB_DISK) ? "yes" : "no"); | |
2678 | printf("IMSM_MAX_DISKS=%d\n",orom->tds); | |
2679 | printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom->vpa); | |
2680 | printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom->vphba); | |
2681 | } | |
2682 | ||
9eafa1de | 2683 | static int detail_platform_imsm(int verbose, int enumerate_only, char *controller_path) |
d665cc31 DW |
2684 | { |
2685 | /* There are two components to imsm platform support, the ahci SATA | |
2686 | * controller and the option-rom. To find the SATA controller we | |
2687 | * simply look in /sys/bus/pci/drivers/ahci to see if an ahci | |
2688 | * controller with the Intel vendor id is present. This approach | |
2689 | * allows mdadm to leverage the kernel's ahci detection logic, with the | |
2690 | * caveat that if ahci.ko is not loaded mdadm will not be able to | |
2691 | * detect platform raid capabilities. The option-rom resides in a | |
2692 | * platform "Adapter ROM". We scan for its signature to retrieve the | |
2693 | * platform capabilities. If raid support is disabled in the BIOS the | |
2694 | * option-rom capability structure will not be available. | |
2695 | */ | |
d665cc31 | 2696 | struct sys_dev *list, *hba; |
d665cc31 DW |
2697 | int host_base = 0; |
2698 | int port_count = 0; | |
9eafa1de | 2699 | int result=1; |
d665cc31 | 2700 | |
5615172f | 2701 | if (enumerate_only) { |
a891a3c2 | 2702 | if (check_env("IMSM_NO_PLATFORM")) |
5615172f | 2703 | return 0; |
a891a3c2 LM |
2704 | list = find_intel_devices(); |
2705 | if (!list) | |
2706 | return 2; | |
2707 | for (hba = list; hba; hba = hba->next) { | |
6b781d33 AP |
2708 | if (find_imsm_capability(hba)) { |
2709 | result = 0; | |
a891a3c2 LM |
2710 | break; |
2711 | } | |
9eafa1de | 2712 | else |
6b781d33 | 2713 | result = 2; |
a891a3c2 | 2714 | } |
a891a3c2 | 2715 | return result; |
5615172f DW |
2716 | } |
2717 | ||
155cbb4c LM |
2718 | list = find_intel_devices(); |
2719 | if (!list) { | |
ba728be7 | 2720 | if (verbose > 0) |
7a862a02 | 2721 | pr_err("no active Intel(R) RAID controller found.\n"); |
d665cc31 | 2722 | return 2; |
ba728be7 | 2723 | } else if (verbose > 0) |
155cbb4c | 2724 | print_found_intel_controllers(list); |
d665cc31 | 2725 | |
a891a3c2 | 2726 | for (hba = list; hba; hba = hba->next) { |
0858eccf | 2727 | if (controller_path && (compare_paths(hba->path, controller_path) != 0)) |
9eafa1de | 2728 | continue; |
0858eccf | 2729 | if (!find_imsm_capability(hba)) { |
60f0f54d | 2730 | char buf[PATH_MAX]; |
e7b84f9d | 2731 | pr_err("imsm capabilities not found for controller: %s (type %s)\n", |
60f0f54d PB |
2732 | hba->type == SYS_DEV_VMD ? vmd_domain_to_controller(hba, buf) : hba->path, |
2733 | get_sys_dev_type(hba->type)); | |
0858eccf AP |
2734 | continue; |
2735 | } | |
2736 | result = 0; | |
2737 | } | |
2738 | ||
2739 | if (controller_path && result == 1) { | |
2740 | pr_err("no active Intel(R) RAID controller found under %s\n", | |
2741 | controller_path); | |
2742 | return result; | |
2743 | } | |
2744 | ||
5e1d6128 | 2745 | const struct orom_entry *entry; |
0858eccf | 2746 | |
5e1d6128 | 2747 | for (entry = orom_entries; entry; entry = entry->next) { |
60f0f54d | 2748 | if (entry->type == SYS_DEV_VMD) { |
07cb1e57 | 2749 | print_imsm_capability(&entry->orom); |
32716c51 PB |
2750 | printf(" 3rd party NVMe :%s supported\n", |
2751 | imsm_orom_has_tpv_support(&entry->orom)?"":" not"); | |
60f0f54d PB |
2752 | for (hba = list; hba; hba = hba->next) { |
2753 | if (hba->type == SYS_DEV_VMD) { | |
2754 | char buf[PATH_MAX]; | |
60f0f54d PB |
2755 | printf(" I/O Controller : %s (%s)\n", |
2756 | vmd_domain_to_controller(hba, buf), get_sys_dev_type(hba->type)); | |
6da53c0e | 2757 | if (print_nvme_info(hba)) { |
b5eece69 PB |
2758 | if (verbose > 0) |
2759 | pr_err("failed to get devices attached to VMD domain.\n"); | |
2760 | result |= 2; | |
2761 | } | |
60f0f54d PB |
2762 | } |
2763 | } | |
07cb1e57 | 2764 | printf("\n"); |
60f0f54d PB |
2765 | continue; |
2766 | } | |
0858eccf | 2767 | |
60f0f54d PB |
2768 | print_imsm_capability(&entry->orom); |
2769 | if (entry->type == SYS_DEV_NVME) { | |
0858eccf AP |
2770 | for (hba = list; hba; hba = hba->next) { |
2771 | if (hba->type == SYS_DEV_NVME) | |
6da53c0e | 2772 | print_nvme_info(hba); |
0858eccf | 2773 | } |
60f0f54d | 2774 | printf("\n"); |
0858eccf AP |
2775 | continue; |
2776 | } | |
2777 | ||
2778 | struct devid_list *devid; | |
5e1d6128 | 2779 | for (devid = entry->devid_list; devid; devid = devid->next) { |
0858eccf AP |
2780 | hba = device_by_id(devid->devid); |
2781 | if (!hba) | |
2782 | continue; | |
2783 | ||
9eafa1de MN |
2784 | printf(" I/O Controller : %s (%s)\n", |
2785 | hba->path, get_sys_dev_type(hba->type)); | |
2786 | if (hba->type == SYS_DEV_SATA) { | |
2787 | host_base = ahci_get_port_count(hba->path, &port_count); | |
2788 | if (ahci_enumerate_ports(hba->path, port_count, host_base, verbose)) { | |
2789 | if (verbose > 0) | |
7a862a02 | 2790 | pr_err("failed to enumerate ports on SATA controller at %s.\n", hba->pci_id); |
9eafa1de MN |
2791 | result |= 2; |
2792 | } | |
120dc887 LM |
2793 | } |
2794 | } | |
0858eccf | 2795 | printf("\n"); |
d665cc31 | 2796 | } |
155cbb4c | 2797 | |
120dc887 | 2798 | return result; |
d665cc31 | 2799 | } |
e50cf220 | 2800 | |
9eafa1de | 2801 | static int export_detail_platform_imsm(int verbose, char *controller_path) |
e50cf220 | 2802 | { |
e50cf220 MN |
2803 | struct sys_dev *list, *hba; |
2804 | int result=1; | |
2805 | ||
2806 | list = find_intel_devices(); | |
2807 | if (!list) { | |
2808 | if (verbose > 0) | |
2809 | pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n"); | |
2810 | result = 2; | |
e50cf220 MN |
2811 | return result; |
2812 | } | |
2813 | ||
2814 | for (hba = list; hba; hba = hba->next) { | |
9eafa1de MN |
2815 | if (controller_path && (compare_paths(hba->path,controller_path) != 0)) |
2816 | continue; | |
60f0f54d PB |
2817 | if (!find_imsm_capability(hba) && verbose > 0) { |
2818 | char buf[PATH_MAX]; | |
2819 | pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n", | |
2820 | hba->type == SYS_DEV_VMD ? vmd_domain_to_controller(hba, buf) : hba->path); | |
2821 | } | |
0858eccf | 2822 | else |
e50cf220 | 2823 | result = 0; |
e50cf220 MN |
2824 | } |
2825 | ||
5e1d6128 | 2826 | const struct orom_entry *entry; |
0858eccf | 2827 | |
60f0f54d PB |
2828 | for (entry = orom_entries; entry; entry = entry->next) { |
2829 | if (entry->type == SYS_DEV_VMD) { | |
2830 | for (hba = list; hba; hba = hba->next) | |
2831 | print_imsm_capability_export(&entry->orom); | |
2832 | continue; | |
2833 | } | |
5e1d6128 | 2834 | print_imsm_capability_export(&entry->orom); |
60f0f54d | 2835 | } |
0858eccf | 2836 | |
e50cf220 MN |
2837 | return result; |
2838 | } | |
2839 | ||
cdddbdbc DW |
2840 | static int match_home_imsm(struct supertype *st, char *homehost) |
2841 | { | |
5115ca67 DW |
2842 | /* the imsm metadata format does not specify any host |
2843 | * identification information. We return -1 since we can never | |
2844 | * confirm nor deny whether a given array is "meant" for this | |
148acb7b | 2845 | * host. We rely on compare_super and the 'family_num' fields to |
5115ca67 DW |
2846 | * exclude member disks that do not belong, and we rely on |
2847 | * mdadm.conf to specify the arrays that should be assembled. | |
2848 | * Auto-assembly may still pick up "foreign" arrays. | |
2849 | */ | |
cdddbdbc | 2850 | |
9362c1c8 | 2851 | return -1; |
cdddbdbc DW |
2852 | } |
2853 | ||
2854 | static void uuid_from_super_imsm(struct supertype *st, int uuid[4]) | |
2855 | { | |
51006d85 N |
2856 | /* The uuid returned here is used for: |
2857 | * uuid to put into bitmap file (Create, Grow) | |
2858 | * uuid for backup header when saving critical section (Grow) | |
2859 | * comparing uuids when re-adding a device into an array | |
2860 | * In these cases the uuid required is that of the data-array, | |
2861 | * not the device-set. | |
2862 | * uuid to recognise same set when adding a missing device back | |
2863 | * to an array. This is a uuid for the device-set. | |
1011e834 | 2864 | * |
51006d85 N |
2865 | * For each of these we can make do with a truncated |
2866 | * or hashed uuid rather than the original, as long as | |
2867 | * everyone agrees. | |
2868 | * In each case the uuid required is that of the data-array, | |
2869 | * not the device-set. | |
43dad3d6 | 2870 | */ |
51006d85 N |
2871 | /* imsm does not track uuid's so we synthesis one using sha1 on |
2872 | * - The signature (Which is constant for all imsm array, but no matter) | |
148acb7b | 2873 | * - the orig_family_num of the container |
51006d85 N |
2874 | * - the index number of the volume |
2875 | * - the 'serial' number of the volume. | |
2876 | * Hopefully these are all constant. | |
2877 | */ | |
2878 | struct intel_super *super = st->sb; | |
43dad3d6 | 2879 | |
51006d85 N |
2880 | char buf[20]; |
2881 | struct sha1_ctx ctx; | |
2882 | struct imsm_dev *dev = NULL; | |
148acb7b | 2883 | __u32 family_num; |
51006d85 | 2884 | |
148acb7b DW |
2885 | /* some mdadm versions failed to set ->orig_family_num, in which |
2886 | * case fall back to ->family_num. orig_family_num will be | |
2887 | * fixed up with the first metadata update. | |
2888 | */ | |
2889 | family_num = super->anchor->orig_family_num; | |
2890 | if (family_num == 0) | |
2891 | family_num = super->anchor->family_num; | |
51006d85 | 2892 | sha1_init_ctx(&ctx); |
92bd8f8d | 2893 | sha1_process_bytes(super->anchor->sig, MPB_SIG_LEN, &ctx); |
148acb7b | 2894 | sha1_process_bytes(&family_num, sizeof(__u32), &ctx); |
51006d85 N |
2895 | if (super->current_vol >= 0) |
2896 | dev = get_imsm_dev(super, super->current_vol); | |
2897 | if (dev) { | |
2898 | __u32 vol = super->current_vol; | |
2899 | sha1_process_bytes(&vol, sizeof(vol), &ctx); | |
2900 | sha1_process_bytes(dev->volume, MAX_RAID_SERIAL_LEN, &ctx); | |
2901 | } | |
2902 | sha1_finish_ctx(&ctx, buf); | |
2903 | memcpy(uuid, buf, 4*4); | |
cdddbdbc DW |
2904 | } |
2905 | ||
0d481d37 | 2906 | #if 0 |
4f5bc454 DW |
2907 | static void |
2908 | get_imsm_numerical_version(struct imsm_super *mpb, int *m, int *p) | |
cdddbdbc | 2909 | { |
cdddbdbc DW |
2910 | __u8 *v = get_imsm_version(mpb); |
2911 | __u8 *end = mpb->sig + MAX_SIGNATURE_LENGTH; | |
2912 | char major[] = { 0, 0, 0 }; | |
2913 | char minor[] = { 0 ,0, 0 }; | |
2914 | char patch[] = { 0, 0, 0 }; | |
2915 | char *ver_parse[] = { major, minor, patch }; | |
2916 | int i, j; | |
2917 | ||
2918 | i = j = 0; | |
2919 | while (*v != '\0' && v < end) { | |
2920 | if (*v != '.' && j < 2) | |
2921 | ver_parse[i][j++] = *v; | |
2922 | else { | |
2923 | i++; | |
2924 | j = 0; | |
2925 | } | |
2926 | v++; | |
2927 | } | |
2928 | ||
4f5bc454 DW |
2929 | *m = strtol(minor, NULL, 0); |
2930 | *p = strtol(patch, NULL, 0); | |
2931 | } | |
0d481d37 | 2932 | #endif |
4f5bc454 | 2933 | |
1e5c6983 DW |
2934 | static __u32 migr_strip_blocks_resync(struct imsm_dev *dev) |
2935 | { | |
2936 | /* migr_strip_size when repairing or initializing parity */ | |
238c0a71 | 2937 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
1e5c6983 DW |
2938 | __u32 chunk = __le32_to_cpu(map->blocks_per_strip); |
2939 | ||
2940 | switch (get_imsm_raid_level(map)) { | |
2941 | case 5: | |
2942 | case 10: | |
2943 | return chunk; | |
2944 | default: | |
2945 | return 128*1024 >> 9; | |
2946 | } | |
2947 | } | |
2948 | ||
2949 | static __u32 migr_strip_blocks_rebuild(struct imsm_dev *dev) | |
2950 | { | |
2951 | /* migr_strip_size when rebuilding a degraded disk, no idea why | |
2952 | * this is different than migr_strip_size_resync(), but it's good | |
2953 | * to be compatible | |
2954 | */ | |
238c0a71 | 2955 | struct imsm_map *map = get_imsm_map(dev, MAP_1); |
1e5c6983 DW |
2956 | __u32 chunk = __le32_to_cpu(map->blocks_per_strip); |
2957 | ||
2958 | switch (get_imsm_raid_level(map)) { | |
2959 | case 1: | |
2960 | case 10: | |
2961 | if (map->num_members % map->num_domains == 0) | |
2962 | return 128*1024 >> 9; | |
2963 | else | |
2964 | return chunk; | |
2965 | case 5: | |
2966 | return max((__u32) 64*1024 >> 9, chunk); | |
2967 | default: | |
2968 | return 128*1024 >> 9; | |
2969 | } | |
2970 | } | |
2971 | ||
2972 | static __u32 num_stripes_per_unit_resync(struct imsm_dev *dev) | |
2973 | { | |
238c0a71 AK |
2974 | struct imsm_map *lo = get_imsm_map(dev, MAP_0); |
2975 | struct imsm_map *hi = get_imsm_map(dev, MAP_1); | |
1e5c6983 DW |
2976 | __u32 lo_chunk = __le32_to_cpu(lo->blocks_per_strip); |
2977 | __u32 hi_chunk = __le32_to_cpu(hi->blocks_per_strip); | |
2978 | ||
2979 | return max((__u32) 1, hi_chunk / lo_chunk); | |
2980 | } | |
2981 | ||
2982 | static __u32 num_stripes_per_unit_rebuild(struct imsm_dev *dev) | |
2983 | { | |
238c0a71 | 2984 | struct imsm_map *lo = get_imsm_map(dev, MAP_0); |
1e5c6983 DW |
2985 | int level = get_imsm_raid_level(lo); |
2986 | ||
2987 | if (level == 1 || level == 10) { | |
238c0a71 | 2988 | struct imsm_map *hi = get_imsm_map(dev, MAP_1); |
1e5c6983 DW |
2989 | |
2990 | return hi->num_domains; | |
2991 | } else | |
2992 | return num_stripes_per_unit_resync(dev); | |
2993 | } | |
2994 | ||
44490938 MD |
2995 | static unsigned long long calc_component_size(struct imsm_map *map, |
2996 | struct imsm_dev *dev) | |
2997 | { | |
2998 | unsigned long long component_size; | |
2999 | unsigned long long dev_size = imsm_dev_size(dev); | |
a4f7290c | 3000 | long long calc_dev_size = 0; |
44490938 MD |
3001 | unsigned int member_disks = imsm_num_data_members(map); |
3002 | ||
3003 | if (member_disks == 0) | |
3004 | return 0; | |
3005 | ||
3006 | component_size = per_dev_array_size(map); | |
3007 | calc_dev_size = component_size * member_disks; | |
3008 | ||
3009 | /* Component size is rounded to 1MB so difference between size from | |
3010 | * metadata and size calculated from num_data_stripes equals up to | |
3011 | * 2048 blocks per each device. If the difference is higher it means | |
3012 | * that array size was expanded and num_data_stripes was not updated. | |
3013 | */ | |
a4f7290c | 3014 | if (llabs(calc_dev_size - (long long)dev_size) > |
44490938 MD |
3015 | (1 << SECT_PER_MB_SHIFT) * member_disks) { |
3016 | component_size = dev_size / member_disks; | |
3017 | dprintf("Invalid num_data_stripes in metadata; expected=%llu, found=%llu\n", | |
3018 | component_size / map->blocks_per_strip, | |
3019 | num_data_stripes(map)); | |
3020 | } | |
3021 | ||
3022 | return component_size; | |
3023 | } | |
3024 | ||
1e5c6983 DW |
3025 | static __u32 parity_segment_depth(struct imsm_dev *dev) |
3026 | { | |
238c0a71 | 3027 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
1e5c6983 DW |
3028 | __u32 chunk = __le32_to_cpu(map->blocks_per_strip); |
3029 | ||
3030 | switch(get_imsm_raid_level(map)) { | |
3031 | case 1: | |
3032 | case 10: | |
3033 | return chunk * map->num_domains; | |
3034 | case 5: | |
3035 | return chunk * map->num_members; | |
3036 | default: | |
3037 | return chunk; | |
3038 | } | |
3039 | } | |
3040 | ||
3041 | static __u32 map_migr_block(struct imsm_dev *dev, __u32 block) | |
3042 | { | |
238c0a71 | 3043 | struct imsm_map *map = get_imsm_map(dev, MAP_1); |
1e5c6983 DW |
3044 | __u32 chunk = __le32_to_cpu(map->blocks_per_strip); |
3045 | __u32 strip = block / chunk; | |
3046 | ||
3047 | switch (get_imsm_raid_level(map)) { | |
3048 | case 1: | |
3049 | case 10: { | |
3050 | __u32 vol_strip = (strip * map->num_domains) + 1; | |
3051 | __u32 vol_stripe = vol_strip / map->num_members; | |
3052 | ||
3053 | return vol_stripe * chunk + block % chunk; | |
3054 | } case 5: { | |
3055 | __u32 stripe = strip / (map->num_members - 1); | |
3056 | ||
3057 | return stripe * chunk + block % chunk; | |
3058 | } | |
3059 | default: | |
3060 | return 0; | |
3061 | } | |
3062 | } | |
3063 | ||
c47b0ff6 AK |
3064 | static __u64 blocks_per_migr_unit(struct intel_super *super, |
3065 | struct imsm_dev *dev) | |
1e5c6983 DW |
3066 | { |
3067 | /* calculate the conversion factor between per member 'blocks' | |
3068 | * (md/{resync,rebuild}_start) and imsm migration units, return | |
3069 | * 0 for the 'not migrating' and 'unsupported migration' cases | |
3070 | */ | |
3071 | if (!dev->vol.migr_state) | |
3072 | return 0; | |
3073 | ||
3074 | switch (migr_type(dev)) { | |
c47b0ff6 AK |
3075 | case MIGR_GEN_MIGR: { |
3076 | struct migr_record *migr_rec = super->migr_rec; | |
3077 | return __le32_to_cpu(migr_rec->blocks_per_unit); | |
3078 | } | |
1e5c6983 DW |
3079 | case MIGR_VERIFY: |
3080 | case MIGR_REPAIR: | |
3081 | case MIGR_INIT: { | |
238c0a71 | 3082 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
1e5c6983 DW |
3083 | __u32 stripes_per_unit; |
3084 | __u32 blocks_per_unit; | |
3085 | __u32 parity_depth; | |
3086 | __u32 migr_chunk; | |
3087 | __u32 block_map; | |
3088 | __u32 block_rel; | |
3089 | __u32 segment; | |
3090 | __u32 stripe; | |
3091 | __u8 disks; | |
3092 | ||
3093 | /* yes, this is really the translation of migr_units to | |
3094 | * per-member blocks in the 'resync' case | |
3095 | */ | |
3096 | stripes_per_unit = num_stripes_per_unit_resync(dev); | |
3097 | migr_chunk = migr_strip_blocks_resync(dev); | |
9529d343 | 3098 | disks = imsm_num_data_members(map); |
1e5c6983 | 3099 | blocks_per_unit = stripes_per_unit * migr_chunk * disks; |
7b1ab482 | 3100 | stripe = __le16_to_cpu(map->blocks_per_strip) * disks; |
1e5c6983 DW |
3101 | segment = blocks_per_unit / stripe; |
3102 | block_rel = blocks_per_unit - segment * stripe; | |
3103 | parity_depth = parity_segment_depth(dev); | |
3104 | block_map = map_migr_block(dev, block_rel); | |
3105 | return block_map + parity_depth * segment; | |
3106 | } | |
3107 | case MIGR_REBUILD: { | |
3108 | __u32 stripes_per_unit; | |
3109 | __u32 migr_chunk; | |
3110 | ||
3111 | stripes_per_unit = num_stripes_per_unit_rebuild(dev); | |
3112 | migr_chunk = migr_strip_blocks_rebuild(dev); | |
3113 | return migr_chunk * stripes_per_unit; | |
3114 | } | |
1e5c6983 DW |
3115 | case MIGR_STATE_CHANGE: |
3116 | default: | |
3117 | return 0; | |
3118 | } | |
3119 | } | |
3120 | ||
c2c087e6 DW |
3121 | static int imsm_level_to_layout(int level) |
3122 | { | |
3123 | switch (level) { | |
3124 | case 0: | |
3125 | case 1: | |
3126 | return 0; | |
3127 | case 5: | |
3128 | case 6: | |
a380c027 | 3129 | return ALGORITHM_LEFT_ASYMMETRIC; |
c2c087e6 | 3130 | case 10: |
c92a2527 | 3131 | return 0x102; |
c2c087e6 | 3132 | } |
a18a888e | 3133 | return UnSet; |
c2c087e6 DW |
3134 | } |
3135 | ||
8e59f3d8 AK |
3136 | /******************************************************************************* |
3137 | * Function: read_imsm_migr_rec | |
3138 | * Description: Function reads imsm migration record from last sector of disk | |
3139 | * Parameters: | |
3140 | * fd : disk descriptor | |
3141 | * super : metadata info | |
3142 | * Returns: | |
3143 | * 0 : success, | |
3144 | * -1 : fail | |
3145 | ******************************************************************************/ | |
3146 | static int read_imsm_migr_rec(int fd, struct intel_super *super) | |
3147 | { | |
3148 | int ret_val = -1; | |
de44e46f | 3149 | unsigned int sector_size = super->sector_size; |
8e59f3d8 AK |
3150 | unsigned long long dsize; |
3151 | ||
3152 | get_dev_size(fd, NULL, &dsize); | |
de44e46f PB |
3153 | if (lseek64(fd, dsize - (sector_size*MIGR_REC_SECTOR_POSITION), |
3154 | SEEK_SET) < 0) { | |
e7b84f9d N |
3155 | pr_err("Cannot seek to anchor block: %s\n", |
3156 | strerror(errno)); | |
8e59f3d8 AK |
3157 | goto out; |
3158 | } | |
466070ad | 3159 | if ((unsigned int)read(fd, super->migr_rec_buf, |
de44e46f PB |
3160 | MIGR_REC_BUF_SECTORS*sector_size) != |
3161 | MIGR_REC_BUF_SECTORS*sector_size) { | |
e7b84f9d N |
3162 | pr_err("Cannot read migr record block: %s\n", |
3163 | strerror(errno)); | |
8e59f3d8 AK |
3164 | goto out; |
3165 | } | |
3166 | ret_val = 0; | |
de44e46f PB |
3167 | if (sector_size == 4096) |
3168 | convert_from_4k_imsm_migr_rec(super); | |
8e59f3d8 AK |
3169 | |
3170 | out: | |
3171 | return ret_val; | |
3172 | } | |
3173 | ||
3136abe5 AK |
3174 | static struct imsm_dev *imsm_get_device_during_migration( |
3175 | struct intel_super *super) | |
3176 | { | |
3177 | ||
3178 | struct intel_dev *dv; | |
3179 | ||
3180 | for (dv = super->devlist; dv; dv = dv->next) { | |
3181 | if (is_gen_migration(dv->dev)) | |
3182 | return dv->dev; | |
3183 | } | |
3184 | return NULL; | |
3185 | } | |
3186 | ||
8e59f3d8 AK |
3187 | /******************************************************************************* |
3188 | * Function: load_imsm_migr_rec | |
3189 | * Description: Function reads imsm migration record (it is stored at the last | |
3190 | * sector of disk) | |
3191 | * Parameters: | |
3192 | * super : imsm internal array info | |
8e59f3d8 AK |
3193 | * Returns: |
3194 | * 0 : success | |
3195 | * -1 : fail | |
4c965cc9 | 3196 | * -2 : no migration in progress |
8e59f3d8 | 3197 | ******************************************************************************/ |
2f86fda3 | 3198 | static int load_imsm_migr_rec(struct intel_super *super) |
8e59f3d8 | 3199 | { |
594dc1b8 | 3200 | struct dl *dl; |
8e59f3d8 AK |
3201 | char nm[30]; |
3202 | int retval = -1; | |
3203 | int fd = -1; | |
3136abe5 | 3204 | struct imsm_dev *dev; |
594dc1b8 | 3205 | struct imsm_map *map; |
b4ab44d8 | 3206 | int slot = -1; |
2f86fda3 | 3207 | int keep_fd = 1; |
3136abe5 AK |
3208 | |
3209 | /* find map under migration */ | |
3210 | dev = imsm_get_device_during_migration(super); | |
3211 | /* nothing to load,no migration in progress? | |
3212 | */ | |
3213 | if (dev == NULL) | |
4c965cc9 | 3214 | return -2; |
8e59f3d8 | 3215 | |
2f86fda3 MT |
3216 | map = get_imsm_map(dev, MAP_0); |
3217 | if (!map) | |
3218 | return -1; | |
3136abe5 | 3219 | |
2f86fda3 MT |
3220 | for (dl = super->disks; dl; dl = dl->next) { |
3221 | /* skip spare and failed disks | |
3222 | */ | |
3223 | if (dl->index < 0) | |
3224 | continue; | |
3225 | /* read only from one of the first two slots | |
3226 | */ | |
3227 | slot = get_imsm_disk_slot(map, dl->index); | |
3228 | if (slot > 1 || slot < 0) | |
3229 | continue; | |
3230 | ||
4389ce73 | 3231 | if (!is_fd_valid(dl->fd)) { |
8e59f3d8 AK |
3232 | sprintf(nm, "%d:%d", dl->major, dl->minor); |
3233 | fd = dev_open(nm, O_RDONLY); | |
4389ce73 MT |
3234 | |
3235 | if (is_fd_valid(fd)) { | |
2f86fda3 | 3236 | keep_fd = 0; |
8e59f3d8 | 3237 | break; |
2f86fda3 MT |
3238 | } |
3239 | } else { | |
3240 | fd = dl->fd; | |
3241 | break; | |
8e59f3d8 AK |
3242 | } |
3243 | } | |
2f86fda3 | 3244 | |
4389ce73 | 3245 | if (!is_fd_valid(fd)) |
2f86fda3 | 3246 | return retval; |
8e59f3d8 | 3247 | retval = read_imsm_migr_rec(fd, super); |
2f86fda3 | 3248 | if (!keep_fd) |
8e59f3d8 | 3249 | close(fd); |
2f86fda3 | 3250 | |
8e59f3d8 AK |
3251 | return retval; |
3252 | } | |
3253 | ||
c17608ea AK |
3254 | /******************************************************************************* |
3255 | * function: imsm_create_metadata_checkpoint_update | |
3256 | * Description: It creates update for checkpoint change. | |
3257 | * Parameters: | |
3258 | * super : imsm internal array info | |
3259 | * u : pointer to prepared update | |
3260 | * Returns: | |
3261 | * Uptate length. | |
3262 | * If length is equal to 0, input pointer u contains no update | |
3263 | ******************************************************************************/ | |
3264 | static int imsm_create_metadata_checkpoint_update( | |
3265 | struct intel_super *super, | |
3266 | struct imsm_update_general_migration_checkpoint **u) | |
3267 | { | |
3268 | ||
3269 | int update_memory_size = 0; | |
3270 | ||
1ade5cc1 | 3271 | dprintf("(enter)\n"); |
c17608ea AK |
3272 | |
3273 | if (u == NULL) | |
3274 | return 0; | |
3275 | *u = NULL; | |
3276 | ||
3277 | /* size of all update data without anchor */ | |
3278 | update_memory_size = | |
3279 | sizeof(struct imsm_update_general_migration_checkpoint); | |
3280 | ||
503975b9 | 3281 | *u = xcalloc(1, update_memory_size); |
c17608ea | 3282 | if (*u == NULL) { |
1ade5cc1 | 3283 | dprintf("error: cannot get memory\n"); |
c17608ea AK |
3284 | return 0; |
3285 | } | |
3286 | (*u)->type = update_general_migration_checkpoint; | |
9f421827 | 3287 | (*u)->curr_migr_unit = current_migr_unit(super->migr_rec); |
83b3de77 | 3288 | dprintf("prepared for %llu\n", (unsigned long long)(*u)->curr_migr_unit); |
c17608ea AK |
3289 | |
3290 | return update_memory_size; | |
3291 | } | |
3292 | ||
c17608ea AK |
3293 | static void imsm_update_metadata_locally(struct supertype *st, |
3294 | void *buf, int len); | |
3295 | ||
687629c2 AK |
3296 | /******************************************************************************* |
3297 | * Function: write_imsm_migr_rec | |
3298 | * Description: Function writes imsm migration record | |
3299 | * (at the last sector of disk) | |
3300 | * Parameters: | |
3301 | * super : imsm internal array info | |
3302 | * Returns: | |
3303 | * 0 : success | |
3304 | * -1 : if fail | |
3305 | ******************************************************************************/ | |
3306 | static int write_imsm_migr_rec(struct supertype *st) | |
3307 | { | |
3308 | struct intel_super *super = st->sb; | |
de44e46f | 3309 | unsigned int sector_size = super->sector_size; |
687629c2 | 3310 | unsigned long long dsize; |
687629c2 AK |
3311 | int retval = -1; |
3312 | struct dl *sd; | |
c17608ea AK |
3313 | int len; |
3314 | struct imsm_update_general_migration_checkpoint *u; | |
3136abe5 | 3315 | struct imsm_dev *dev; |
594dc1b8 | 3316 | struct imsm_map *map; |
3136abe5 AK |
3317 | |
3318 | /* find map under migration */ | |
3319 | dev = imsm_get_device_during_migration(super); | |
3320 | /* if no migration, write buffer anyway to clear migr_record | |
3321 | * on disk based on first available device | |
3322 | */ | |
3323 | if (dev == NULL) | |
3324 | dev = get_imsm_dev(super, super->current_vol < 0 ? 0 : | |
3325 | super->current_vol); | |
3326 | ||
44bfe6df | 3327 | map = get_imsm_map(dev, MAP_0); |
687629c2 | 3328 | |
de44e46f PB |
3329 | if (sector_size == 4096) |
3330 | convert_to_4k_imsm_migr_rec(super); | |
687629c2 | 3331 | for (sd = super->disks ; sd ; sd = sd->next) { |
b4ab44d8 | 3332 | int slot = -1; |
3136abe5 AK |
3333 | |
3334 | /* skip failed and spare devices */ | |
3335 | if (sd->index < 0) | |
3336 | continue; | |
687629c2 | 3337 | /* write to 2 first slots only */ |
3136abe5 AK |
3338 | if (map) |
3339 | slot = get_imsm_disk_slot(map, sd->index); | |
089f9d79 | 3340 | if (map == NULL || slot > 1 || slot < 0) |
687629c2 | 3341 | continue; |
3136abe5 | 3342 | |
2f86fda3 MT |
3343 | get_dev_size(sd->fd, NULL, &dsize); |
3344 | if (lseek64(sd->fd, dsize - (MIGR_REC_SECTOR_POSITION * | |
3345 | sector_size), | |
de44e46f | 3346 | SEEK_SET) < 0) { |
e7b84f9d N |
3347 | pr_err("Cannot seek to anchor block: %s\n", |
3348 | strerror(errno)); | |
687629c2 AK |
3349 | goto out; |
3350 | } | |
2f86fda3 | 3351 | if ((unsigned int)write(sd->fd, super->migr_rec_buf, |
de44e46f PB |
3352 | MIGR_REC_BUF_SECTORS*sector_size) != |
3353 | MIGR_REC_BUF_SECTORS*sector_size) { | |
e7b84f9d N |
3354 | pr_err("Cannot write migr record block: %s\n", |
3355 | strerror(errno)); | |
687629c2 AK |
3356 | goto out; |
3357 | } | |
687629c2 | 3358 | } |
de44e46f PB |
3359 | if (sector_size == 4096) |
3360 | convert_from_4k_imsm_migr_rec(super); | |
c17608ea AK |
3361 | /* update checkpoint information in metadata */ |
3362 | len = imsm_create_metadata_checkpoint_update(super, &u); | |
c17608ea AK |
3363 | if (len <= 0) { |
3364 | dprintf("imsm: Cannot prepare update\n"); | |
3365 | goto out; | |
3366 | } | |
3367 | /* update metadata locally */ | |
3368 | imsm_update_metadata_locally(st, u, len); | |
3369 | /* and possibly remotely */ | |
3370 | if (st->update_tail) { | |
3371 | append_metadata_update(st, u, len); | |
3372 | /* during reshape we do all work inside metadata handler | |
3373 | * manage_reshape(), so metadata update has to be triggered | |
3374 | * insida it | |
3375 | */ | |
3376 | flush_metadata_updates(st); | |
3377 | st->update_tail = &st->updates; | |
3378 | } else | |
3379 | free(u); | |
687629c2 AK |
3380 | |
3381 | retval = 0; | |
3382 | out: | |
687629c2 AK |
3383 | return retval; |
3384 | } | |
3385 | ||
e2962bfc AK |
3386 | /* spare/missing disks activations are not allowe when |
3387 | * array/container performs reshape operation, because | |
3388 | * all arrays in container works on the same disks set | |
3389 | */ | |
3390 | int imsm_reshape_blocks_arrays_changes(struct intel_super *super) | |
3391 | { | |
3392 | int rv = 0; | |
3393 | struct intel_dev *i_dev; | |
3394 | struct imsm_dev *dev; | |
3395 | ||
3396 | /* check whole container | |
3397 | */ | |
3398 | for (i_dev = super->devlist; i_dev; i_dev = i_dev->next) { | |
3399 | dev = i_dev->dev; | |
3ad25638 | 3400 | if (is_gen_migration(dev)) { |
e2962bfc AK |
3401 | /* No repair during any migration in container |
3402 | */ | |
3403 | rv = 1; | |
3404 | break; | |
3405 | } | |
3406 | } | |
3407 | return rv; | |
3408 | } | |
3e684231 | 3409 | static unsigned long long imsm_component_size_alignment_check(int level, |
c41e00b2 | 3410 | int chunk_size, |
f36a9ecd | 3411 | unsigned int sector_size, |
c41e00b2 AK |
3412 | unsigned long long component_size) |
3413 | { | |
3e684231 | 3414 | unsigned int component_size_alignment; |
c41e00b2 | 3415 | |
3e684231 | 3416 | /* check component size alignment |
c41e00b2 | 3417 | */ |
3e684231 | 3418 | component_size_alignment = component_size % (chunk_size/sector_size); |
c41e00b2 | 3419 | |
3e684231 | 3420 | dprintf("(Level: %i, chunk_size = %i, component_size = %llu), component_size_alignment = %u\n", |
c41e00b2 | 3421 | level, chunk_size, component_size, |
3e684231 | 3422 | component_size_alignment); |
c41e00b2 | 3423 | |
3e684231 MZ |
3424 | if (component_size_alignment && (level != 1) && (level != UnSet)) { |
3425 | dprintf("imsm: reported component size aligned from %llu ", | |
c41e00b2 | 3426 | component_size); |
3e684231 | 3427 | component_size -= component_size_alignment; |
1ade5cc1 | 3428 | dprintf_cont("to %llu (%i).\n", |
3e684231 | 3429 | component_size, component_size_alignment); |
c41e00b2 AK |
3430 | } |
3431 | ||
3432 | return component_size; | |
3433 | } | |
e2962bfc | 3434 | |
fbc42556 JR |
3435 | /******************************************************************************* |
3436 | * Function: get_bitmap_header_sector | |
3437 | * Description: Returns the sector where the bitmap header is placed. | |
3438 | * Parameters: | |
3439 | * st : supertype information | |
3440 | * dev_idx : index of the device with bitmap | |
3441 | * | |
3442 | * Returns: | |
3443 | * The sector where the bitmap header is placed | |
3444 | ******************************************************************************/ | |
3445 | static unsigned long long get_bitmap_header_sector(struct intel_super *super, | |
3446 | int dev_idx) | |
3447 | { | |
3448 | struct imsm_dev *dev = get_imsm_dev(super, dev_idx); | |
3449 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
3450 | ||
3451 | if (!super->sector_size) { | |
3452 | dprintf("sector size is not set\n"); | |
3453 | return 0; | |
3454 | } | |
3455 | ||
3456 | return pba_of_lba0(map) + calc_component_size(map, dev) + | |
3457 | (IMSM_BITMAP_HEADER_OFFSET / super->sector_size); | |
3458 | } | |
3459 | ||
3460 | /******************************************************************************* | |
3461 | * Function: get_bitmap_sector | |
3462 | * Description: Returns the sector where the bitmap is placed. | |
3463 | * Parameters: | |
3464 | * st : supertype information | |
3465 | * dev_idx : index of the device with bitmap | |
3466 | * | |
3467 | * Returns: | |
3468 | * The sector where the bitmap is placed | |
3469 | ******************************************************************************/ | |
3470 | static unsigned long long get_bitmap_sector(struct intel_super *super, | |
3471 | int dev_idx) | |
3472 | { | |
3473 | if (!super->sector_size) { | |
3474 | dprintf("sector size is not set\n"); | |
3475 | return 0; | |
3476 | } | |
3477 | ||
3478 | return get_bitmap_header_sector(super, dev_idx) + | |
3479 | (IMSM_BITMAP_HEADER_SIZE / super->sector_size); | |
3480 | } | |
3481 | ||
2432ce9b AP |
3482 | static unsigned long long get_ppl_sector(struct intel_super *super, int dev_idx) |
3483 | { | |
3484 | struct imsm_dev *dev = get_imsm_dev(super, dev_idx); | |
3485 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
3486 | ||
3487 | return pba_of_lba0(map) + | |
3488 | (num_data_stripes(map) * map->blocks_per_strip); | |
3489 | } | |
3490 | ||
a5d85af7 | 3491 | static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info, char *dmap) |
bf5a934a DW |
3492 | { |
3493 | struct intel_super *super = st->sb; | |
c47b0ff6 | 3494 | struct migr_record *migr_rec = super->migr_rec; |
949c47a0 | 3495 | struct imsm_dev *dev = get_imsm_dev(super, super->current_vol); |
238c0a71 AK |
3496 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
3497 | struct imsm_map *prev_map = get_imsm_map(dev, MAP_1); | |
b335e593 | 3498 | struct imsm_map *map_to_analyse = map; |
efb30e7f | 3499 | struct dl *dl; |
a5d85af7 | 3500 | int map_disks = info->array.raid_disks; |
bf5a934a | 3501 | |
95eeceeb | 3502 | memset(info, 0, sizeof(*info)); |
b335e593 AK |
3503 | if (prev_map) |
3504 | map_to_analyse = prev_map; | |
3505 | ||
ca0748fa | 3506 | dl = super->current_disk; |
9894ec0d | 3507 | |
bf5a934a | 3508 | info->container_member = super->current_vol; |
cd0430a1 | 3509 | info->array.raid_disks = map->num_members; |
b335e593 | 3510 | info->array.level = get_imsm_raid_level(map_to_analyse); |
bf5a934a DW |
3511 | info->array.layout = imsm_level_to_layout(info->array.level); |
3512 | info->array.md_minor = -1; | |
3513 | info->array.ctime = 0; | |
3514 | info->array.utime = 0; | |
b335e593 AK |
3515 | info->array.chunk_size = |
3516 | __le16_to_cpu(map_to_analyse->blocks_per_strip) << 9; | |
2432ce9b | 3517 | info->array.state = !(dev->vol.dirty & RAIDVOL_DIRTY); |
fcc2c9da | 3518 | info->custom_array_size = imsm_dev_size(dev); |
3ad25638 AK |
3519 | info->recovery_blocked = imsm_reshape_blocks_arrays_changes(st->sb); |
3520 | ||
3f510843 | 3521 | if (is_gen_migration(dev)) { |
195d1d76 PP |
3522 | /* |
3523 | * device prev_map should be added if it is in the middle | |
3524 | * of migration | |
3525 | */ | |
3526 | assert(prev_map); | |
3527 | ||
3f83228a | 3528 | info->reshape_active = 1; |
b335e593 AK |
3529 | info->new_level = get_imsm_raid_level(map); |
3530 | info->new_layout = imsm_level_to_layout(info->new_level); | |
3531 | info->new_chunk = __le16_to_cpu(map->blocks_per_strip) << 9; | |
3f83228a | 3532 | info->delta_disks = map->num_members - prev_map->num_members; |
493f5dd6 N |
3533 | if (info->delta_disks) { |
3534 | /* this needs to be applied to every array | |
3535 | * in the container. | |
3536 | */ | |
81219e70 | 3537 | info->reshape_active = CONTAINER_RESHAPE; |
493f5dd6 | 3538 | } |
3f83228a N |
3539 | /* We shape information that we give to md might have to be |
3540 | * modify to cope with md's requirement for reshaping arrays. | |
3541 | * For example, when reshaping a RAID0, md requires it to be | |
3542 | * presented as a degraded RAID4. | |
3543 | * Also if a RAID0 is migrating to a RAID5 we need to specify | |
3544 | * the array as already being RAID5, but the 'before' layout | |
3545 | * is a RAID4-like layout. | |
3546 | */ | |
3547 | switch (info->array.level) { | |
3548 | case 0: | |
3549 | switch(info->new_level) { | |
3550 | case 0: | |
3551 | /* conversion is happening as RAID4 */ | |
3552 | info->array.level = 4; | |
3553 | info->array.raid_disks += 1; | |
3554 | break; | |
3555 | case 5: | |
3556 | /* conversion is happening as RAID5 */ | |
3557 | info->array.level = 5; | |
3558 | info->array.layout = ALGORITHM_PARITY_N; | |
3f83228a N |
3559 | info->delta_disks -= 1; |
3560 | break; | |
3561 | default: | |
3562 | /* FIXME error message */ | |
3563 | info->array.level = UnSet; | |
3564 | break; | |
3565 | } | |
3566 | break; | |
3567 | } | |
b335e593 AK |
3568 | } else { |
3569 | info->new_level = UnSet; | |
3570 | info->new_layout = UnSet; | |
3571 | info->new_chunk = info->array.chunk_size; | |
3f83228a | 3572 | info->delta_disks = 0; |
b335e593 | 3573 | } |
ca0748fa | 3574 | |
efb30e7f DW |
3575 | if (dl) { |
3576 | info->disk.major = dl->major; | |
3577 | info->disk.minor = dl->minor; | |
ca0748fa | 3578 | info->disk.number = dl->index; |
656b6b5a N |
3579 | info->disk.raid_disk = get_imsm_disk_slot(map_to_analyse, |
3580 | dl->index); | |
efb30e7f | 3581 | } |
bf5a934a | 3582 | |
5551b113 | 3583 | info->data_offset = pba_of_lba0(map_to_analyse); |
44490938 | 3584 | info->component_size = calc_component_size(map, dev); |
3e684231 | 3585 | info->component_size = imsm_component_size_alignment_check( |
c41e00b2 AK |
3586 | info->array.level, |
3587 | info->array.chunk_size, | |
f36a9ecd | 3588 | super->sector_size, |
c41e00b2 | 3589 | info->component_size); |
5e46202e | 3590 | info->bb.supported = 1; |
139dae11 | 3591 | |
301406c9 | 3592 | memset(info->uuid, 0, sizeof(info->uuid)); |
921d9e16 | 3593 | info->recovery_start = MaxSector; |
bf5a934a | 3594 | |
c2462068 PB |
3595 | if (info->array.level == 5 && |
3596 | (dev->rwh_policy == RWH_DISTRIBUTED || | |
3597 | dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)) { | |
2432ce9b AP |
3598 | info->consistency_policy = CONSISTENCY_POLICY_PPL; |
3599 | info->ppl_sector = get_ppl_sector(super, super->current_vol); | |
c2462068 PB |
3600 | if (dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED) |
3601 | info->ppl_size = MULTIPLE_PPL_AREA_SIZE_IMSM >> 9; | |
3602 | else | |
3603 | info->ppl_size = (PPL_HEADER_SIZE + PPL_ENTRY_SPACE) | |
3604 | >> 9; | |
2432ce9b AP |
3605 | } else if (info->array.level <= 0) { |
3606 | info->consistency_policy = CONSISTENCY_POLICY_NONE; | |
3607 | } else { | |
fbc42556 JR |
3608 | if (dev->rwh_policy == RWH_BITMAP) { |
3609 | info->bitmap_offset = get_bitmap_sector(super, super->current_vol); | |
3610 | info->consistency_policy = CONSISTENCY_POLICY_BITMAP; | |
3611 | } else { | |
3612 | info->consistency_policy = CONSISTENCY_POLICY_RESYNC; | |
3613 | } | |
2432ce9b AP |
3614 | } |
3615 | ||
d2e6d5d6 | 3616 | info->reshape_progress = 0; |
b6796ce1 | 3617 | info->resync_start = MaxSector; |
b9172665 | 3618 | if ((map_to_analyse->map_state == IMSM_T_STATE_UNINITIALIZED || |
2432ce9b | 3619 | !(info->array.state & 1)) && |
b9172665 | 3620 | imsm_reshape_blocks_arrays_changes(super) == 0) { |
301406c9 | 3621 | info->resync_start = 0; |
b6796ce1 AK |
3622 | } |
3623 | if (dev->vol.migr_state) { | |
1e5c6983 DW |
3624 | switch (migr_type(dev)) { |
3625 | case MIGR_REPAIR: | |
3626 | case MIGR_INIT: { | |
c47b0ff6 AK |
3627 | __u64 blocks_per_unit = blocks_per_migr_unit(super, |
3628 | dev); | |
4036e7ee | 3629 | __u64 units = vol_curr_migr_unit(dev); |
1e5c6983 DW |
3630 | |
3631 | info->resync_start = blocks_per_unit * units; | |
3632 | break; | |
3633 | } | |
d2e6d5d6 | 3634 | case MIGR_GEN_MIGR: { |
c47b0ff6 AK |
3635 | __u64 blocks_per_unit = blocks_per_migr_unit(super, |
3636 | dev); | |
9f421827 | 3637 | __u64 units = current_migr_unit(migr_rec); |
04fa9523 | 3638 | int used_disks; |
d2e6d5d6 | 3639 | |
befb629b AK |
3640 | if (__le32_to_cpu(migr_rec->ascending_migr) && |
3641 | (units < | |
9f421827 | 3642 | (get_num_migr_units(migr_rec)-1)) && |
befb629b AK |
3643 | (super->migr_rec->rec_status == |
3644 | __cpu_to_le32(UNIT_SRC_IN_CP_AREA))) | |
3645 | units++; | |
3646 | ||
d2e6d5d6 | 3647 | info->reshape_progress = blocks_per_unit * units; |
6289d1e0 | 3648 | |
7a862a02 | 3649 | dprintf("IMSM: General Migration checkpoint : %llu (%llu) -> read reshape progress : %llu\n", |
19986c72 MB |
3650 | (unsigned long long)units, |
3651 | (unsigned long long)blocks_per_unit, | |
3652 | info->reshape_progress); | |
75156c46 | 3653 | |
9529d343 | 3654 | used_disks = imsm_num_data_members(prev_map); |
75156c46 | 3655 | if (used_disks > 0) { |
895ffd99 | 3656 | info->custom_array_size = per_dev_array_size(map) * |
75156c46 | 3657 | used_disks; |
75156c46 | 3658 | } |
d2e6d5d6 | 3659 | } |
1e5c6983 DW |
3660 | case MIGR_VERIFY: |
3661 | /* we could emulate the checkpointing of | |
3662 | * 'sync_action=check' migrations, but for now | |
3663 | * we just immediately complete them | |
3664 | */ | |
3665 | case MIGR_REBUILD: | |
3666 | /* this is handled by container_content_imsm() */ | |
1e5c6983 DW |
3667 | case MIGR_STATE_CHANGE: |
3668 | /* FIXME handle other migrations */ | |
3669 | default: | |
3670 | /* we are not dirty, so... */ | |
3671 | info->resync_start = MaxSector; | |
3672 | } | |
b6796ce1 | 3673 | } |
301406c9 DW |
3674 | |
3675 | strncpy(info->name, (char *) dev->volume, MAX_RAID_SERIAL_LEN); | |
3676 | info->name[MAX_RAID_SERIAL_LEN] = 0; | |
bf5a934a | 3677 | |
f35f2525 N |
3678 | info->array.major_version = -1; |
3679 | info->array.minor_version = -2; | |
4dd2df09 | 3680 | sprintf(info->text_version, "/%s/%d", st->container_devnm, info->container_member); |
a67dd8cc | 3681 | info->safe_mode_delay = 4000; /* 4 secs like the Matrix driver */ |
51006d85 | 3682 | uuid_from_super_imsm(st, info->uuid); |
a5d85af7 N |
3683 | |
3684 | if (dmap) { | |
3685 | int i, j; | |
3686 | for (i=0; i<map_disks; i++) { | |
3687 | dmap[i] = 0; | |
3688 | if (i < info->array.raid_disks) { | |
3689 | struct imsm_disk *dsk; | |
238c0a71 | 3690 | j = get_imsm_disk_idx(dev, i, MAP_X); |
a5d85af7 N |
3691 | dsk = get_imsm_disk(super, j); |
3692 | if (dsk && (dsk->status & CONFIGURED_DISK)) | |
3693 | dmap[i] = 1; | |
3694 | } | |
3695 | } | |
3696 | } | |
81ac8b4d | 3697 | } |
bf5a934a | 3698 | |
3b451610 AK |
3699 | static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev, |
3700 | int failed, int look_in_map); | |
3701 | ||
3702 | static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev, | |
3703 | int look_in_map); | |
3704 | ||
3705 | static void manage_second_map(struct intel_super *super, struct imsm_dev *dev) | |
3706 | { | |
3707 | if (is_gen_migration(dev)) { | |
3708 | int failed; | |
3709 | __u8 map_state; | |
3710 | struct imsm_map *map2 = get_imsm_map(dev, MAP_1); | |
3711 | ||
3712 | failed = imsm_count_failed(super, dev, MAP_1); | |
238c0a71 | 3713 | map_state = imsm_check_degraded(super, dev, failed, MAP_1); |
3b451610 AK |
3714 | if (map2->map_state != map_state) { |
3715 | map2->map_state = map_state; | |
3716 | super->updates_pending++; | |
3717 | } | |
3718 | } | |
3719 | } | |
97b4d0e9 DW |
3720 | |
3721 | static struct imsm_disk *get_imsm_missing(struct intel_super *super, __u8 index) | |
3722 | { | |
3723 | struct dl *d; | |
3724 | ||
3725 | for (d = super->missing; d; d = d->next) | |
3726 | if (d->index == index) | |
3727 | return &d->disk; | |
3728 | return NULL; | |
3729 | } | |
3730 | ||
a5d85af7 | 3731 | static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map) |
4f5bc454 DW |
3732 | { |
3733 | struct intel_super *super = st->sb; | |
4f5bc454 | 3734 | struct imsm_disk *disk; |
a5d85af7 | 3735 | int map_disks = info->array.raid_disks; |
ab3cb6b3 N |
3736 | int max_enough = -1; |
3737 | int i; | |
3738 | struct imsm_super *mpb; | |
4f5bc454 | 3739 | |
bf5a934a | 3740 | if (super->current_vol >= 0) { |
a5d85af7 | 3741 | getinfo_super_imsm_volume(st, info, map); |
bf5a934a DW |
3742 | return; |
3743 | } | |
95eeceeb | 3744 | memset(info, 0, sizeof(*info)); |
d23fe947 DW |
3745 | |
3746 | /* Set raid_disks to zero so that Assemble will always pull in valid | |
3747 | * spares | |
3748 | */ | |
3749 | info->array.raid_disks = 0; | |
cdddbdbc DW |
3750 | info->array.level = LEVEL_CONTAINER; |
3751 | info->array.layout = 0; | |
3752 | info->array.md_minor = -1; | |
1011e834 | 3753 | info->array.ctime = 0; /* N/A for imsm */ |
cdddbdbc DW |
3754 | info->array.utime = 0; |
3755 | info->array.chunk_size = 0; | |
3756 | ||
3757 | info->disk.major = 0; | |
3758 | info->disk.minor = 0; | |
cdddbdbc | 3759 | info->disk.raid_disk = -1; |
c2c087e6 | 3760 | info->reshape_active = 0; |
f35f2525 N |
3761 | info->array.major_version = -1; |
3762 | info->array.minor_version = -2; | |
c2c087e6 | 3763 | strcpy(info->text_version, "imsm"); |
a67dd8cc | 3764 | info->safe_mode_delay = 0; |
c2c087e6 DW |
3765 | info->disk.number = -1; |
3766 | info->disk.state = 0; | |
c5afc314 | 3767 | info->name[0] = 0; |
921d9e16 | 3768 | info->recovery_start = MaxSector; |
3ad25638 | 3769 | info->recovery_blocked = imsm_reshape_blocks_arrays_changes(st->sb); |
5e46202e | 3770 | info->bb.supported = 1; |
c2c087e6 | 3771 | |
97b4d0e9 | 3772 | /* do we have the all the insync disks that we expect? */ |
ab3cb6b3 | 3773 | mpb = super->anchor; |
b7d81a38 | 3774 | info->events = __le32_to_cpu(mpb->generation_num); |
97b4d0e9 | 3775 | |
ab3cb6b3 N |
3776 | for (i = 0; i < mpb->num_raid_devs; i++) { |
3777 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
3778 | int failed, enough, j, missing = 0; | |
3779 | struct imsm_map *map; | |
3780 | __u8 state; | |
97b4d0e9 | 3781 | |
3b451610 AK |
3782 | failed = imsm_count_failed(super, dev, MAP_0); |
3783 | state = imsm_check_degraded(super, dev, failed, MAP_0); | |
238c0a71 | 3784 | map = get_imsm_map(dev, MAP_0); |
ab3cb6b3 N |
3785 | |
3786 | /* any newly missing disks? | |
3787 | * (catches single-degraded vs double-degraded) | |
3788 | */ | |
3789 | for (j = 0; j < map->num_members; j++) { | |
238c0a71 | 3790 | __u32 ord = get_imsm_ord_tbl_ent(dev, j, MAP_0); |
ab3cb6b3 N |
3791 | __u32 idx = ord_to_idx(ord); |
3792 | ||
20dc76d1 MT |
3793 | if (super->disks && super->disks->index == (int)idx) |
3794 | info->disk.raid_disk = j; | |
3795 | ||
ab3cb6b3 N |
3796 | if (!(ord & IMSM_ORD_REBUILD) && |
3797 | get_imsm_missing(super, idx)) { | |
3798 | missing = 1; | |
3799 | break; | |
3800 | } | |
97b4d0e9 | 3801 | } |
ab3cb6b3 N |
3802 | |
3803 | if (state == IMSM_T_STATE_FAILED) | |
3804 | enough = -1; | |
3805 | else if (state == IMSM_T_STATE_DEGRADED && | |
3806 | (state != map->map_state || missing)) | |
3807 | enough = 0; | |
3808 | else /* we're normal, or already degraded */ | |
3809 | enough = 1; | |
d2bde6d3 AK |
3810 | if (is_gen_migration(dev) && missing) { |
3811 | /* during general migration we need all disks | |
3812 | * that process is running on. | |
3813 | * No new missing disk is allowed. | |
3814 | */ | |
3815 | max_enough = -1; | |
3816 | enough = -1; | |
3817 | /* no more checks necessary | |
3818 | */ | |
3819 | break; | |
3820 | } | |
ab3cb6b3 N |
3821 | /* in the missing/failed disk case check to see |
3822 | * if at least one array is runnable | |
3823 | */ | |
3824 | max_enough = max(max_enough, enough); | |
3825 | } | |
1ade5cc1 | 3826 | dprintf("enough: %d\n", max_enough); |
ab3cb6b3 | 3827 | info->container_enough = max_enough; |
97b4d0e9 | 3828 | |
4a04ec6c | 3829 | if (super->disks) { |
14e8215b DW |
3830 | __u32 reserved = imsm_reserved_sectors(super, super->disks); |
3831 | ||
b9f594fe | 3832 | disk = &super->disks->disk; |
5551b113 | 3833 | info->data_offset = total_blocks(&super->disks->disk) - reserved; |
14e8215b | 3834 | info->component_size = reserved; |
25ed7e59 | 3835 | info->disk.state = is_configured(disk) ? (1 << MD_DISK_ACTIVE) : 0; |
df474657 DW |
3836 | /* we don't change info->disk.raid_disk here because |
3837 | * this state will be finalized in mdmon after we have | |
3838 | * found the 'most fresh' version of the metadata | |
3839 | */ | |
25ed7e59 | 3840 | info->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0; |
2432ce9b AP |
3841 | info->disk.state |= (is_spare(disk) || is_journal(disk)) ? |
3842 | 0 : (1 << MD_DISK_SYNC); | |
cdddbdbc | 3843 | } |
a575e2a7 DW |
3844 | |
3845 | /* only call uuid_from_super_imsm when this disk is part of a populated container, | |
3846 | * ->compare_super may have updated the 'num_raid_devs' field for spares | |
3847 | */ | |
3848 | if (info->disk.state & (1 << MD_DISK_SYNC) || super->anchor->num_raid_devs) | |
36ba7d48 | 3849 | uuid_from_super_imsm(st, info->uuid); |
22e263f6 AC |
3850 | else |
3851 | memcpy(info->uuid, uuid_zero, sizeof(uuid_zero)); | |
a5d85af7 N |
3852 | |
3853 | /* I don't know how to compute 'map' on imsm, so use safe default */ | |
3854 | if (map) { | |
3855 | int i; | |
3856 | for (i = 0; i < map_disks; i++) | |
3857 | map[i] = 1; | |
3858 | } | |
3859 | ||
cdddbdbc DW |
3860 | } |
3861 | ||
5c4cd5da AC |
3862 | /* allocates memory and fills disk in mdinfo structure |
3863 | * for each disk in array */ | |
3864 | struct mdinfo *getinfo_super_disks_imsm(struct supertype *st) | |
3865 | { | |
594dc1b8 | 3866 | struct mdinfo *mddev; |
5c4cd5da AC |
3867 | struct intel_super *super = st->sb; |
3868 | struct imsm_disk *disk; | |
3869 | int count = 0; | |
3870 | struct dl *dl; | |
3871 | if (!super || !super->disks) | |
3872 | return NULL; | |
3873 | dl = super->disks; | |
503975b9 | 3874 | mddev = xcalloc(1, sizeof(*mddev)); |
5c4cd5da AC |
3875 | while (dl) { |
3876 | struct mdinfo *tmp; | |
3877 | disk = &dl->disk; | |
503975b9 | 3878 | tmp = xcalloc(1, sizeof(*tmp)); |
5c4cd5da AC |
3879 | if (mddev->devs) |
3880 | tmp->next = mddev->devs; | |
3881 | mddev->devs = tmp; | |
3882 | tmp->disk.number = count++; | |
3883 | tmp->disk.major = dl->major; | |
3884 | tmp->disk.minor = dl->minor; | |
3885 | tmp->disk.state = is_configured(disk) ? | |
3886 | (1 << MD_DISK_ACTIVE) : 0; | |
3887 | tmp->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0; | |
3888 | tmp->disk.state |= is_spare(disk) ? 0 : (1 << MD_DISK_SYNC); | |
3889 | tmp->disk.raid_disk = -1; | |
3890 | dl = dl->next; | |
3891 | } | |
3892 | return mddev; | |
3893 | } | |
3894 | ||
cdddbdbc | 3895 | static int update_super_imsm(struct supertype *st, struct mdinfo *info, |
03312b52 MK |
3896 | enum update_opt update, char *devname, |
3897 | int verbose, int uuid_set, char *homehost) | |
cdddbdbc | 3898 | { |
f352c545 DW |
3899 | /* For 'assemble' and 'force' we need to return non-zero if any |
3900 | * change was made. For others, the return value is ignored. | |
3901 | * Update options are: | |
3902 | * force-one : This device looks a bit old but needs to be included, | |
3903 | * update age info appropriately. | |
3904 | * assemble: clear any 'faulty' flag to allow this device to | |
3905 | * be assembled. | |
3906 | * force-array: Array is degraded but being forced, mark it clean | |
3907 | * if that will be needed to assemble it. | |
3908 | * | |
3909 | * newdev: not used ???? | |
3910 | * grow: Array has gained a new device - this is currently for | |
3911 | * linear only | |
3912 | * resync: mark as dirty so a resync will happen. | |
3913 | * name: update the name - preserving the homehost | |
6e46bf34 | 3914 | * uuid: Change the uuid of the array to match watch is given |
f352c545 DW |
3915 | * |
3916 | * Following are not relevant for this imsm: | |
3917 | * sparc2.2 : update from old dodgey metadata | |
3918 | * super-minor: change the preferred_minor number | |
3919 | * summaries: update redundant counters. | |
f352c545 DW |
3920 | * homehost: update the recorded homehost |
3921 | * _reshape_progress: record new reshape_progress position. | |
3922 | */ | |
6e46bf34 DW |
3923 | int rv = 1; |
3924 | struct intel_super *super = st->sb; | |
3925 | struct imsm_super *mpb; | |
f352c545 | 3926 | |
6e46bf34 DW |
3927 | /* we can only update container info */ |
3928 | if (!super || super->current_vol >= 0 || !super->anchor) | |
3929 | return 1; | |
3930 | ||
3931 | mpb = super->anchor; | |
3932 | ||
03312b52 | 3933 | switch (update) { |
4345e135 | 3934 | case UOPT_UUID: |
81a5b4f5 N |
3935 | /* We take this to mean that the family_num should be updated. |
3936 | * However that is much smaller than the uuid so we cannot really | |
3937 | * allow an explicit uuid to be given. And it is hard to reliably | |
3938 | * know if one was. | |
3939 | * So if !uuid_set we know the current uuid is random and just used | |
3940 | * the first 'int' and copy it to the other 3 positions. | |
3941 | * Otherwise we require the 4 'int's to be the same as would be the | |
3942 | * case if we are using a random uuid. So an explicit uuid will be | |
3943 | * accepted as long as all for ints are the same... which shouldn't hurt | |
6e46bf34 | 3944 | */ |
81a5b4f5 N |
3945 | if (!uuid_set) { |
3946 | info->uuid[1] = info->uuid[2] = info->uuid[3] = info->uuid[0]; | |
6e46bf34 | 3947 | rv = 0; |
81a5b4f5 N |
3948 | } else { |
3949 | if (info->uuid[0] != info->uuid[1] || | |
3950 | info->uuid[1] != info->uuid[2] || | |
3951 | info->uuid[2] != info->uuid[3]) | |
3952 | rv = -1; | |
3953 | else | |
3954 | rv = 0; | |
6e46bf34 | 3955 | } |
81a5b4f5 N |
3956 | if (rv == 0) |
3957 | mpb->orig_family_num = info->uuid[0]; | |
4345e135 MK |
3958 | break; |
3959 | case UOPT_SPEC_ASSEMBLE: | |
6e46bf34 | 3960 | rv = 0; |
4345e135 MK |
3961 | break; |
3962 | default: | |
1e2b2765 | 3963 | rv = -1; |
4345e135 MK |
3964 | break; |
3965 | } | |
f352c545 | 3966 | |
6e46bf34 DW |
3967 | /* successful update? recompute checksum */ |
3968 | if (rv == 0) | |
3969 | mpb->check_sum = __le32_to_cpu(__gen_imsm_checksum(mpb)); | |
f352c545 DW |
3970 | |
3971 | return rv; | |
cdddbdbc DW |
3972 | } |
3973 | ||
c2c087e6 | 3974 | static size_t disks_to_mpb_size(int disks) |
cdddbdbc | 3975 | { |
c2c087e6 | 3976 | size_t size; |
cdddbdbc | 3977 | |
c2c087e6 DW |
3978 | size = sizeof(struct imsm_super); |
3979 | size += (disks - 1) * sizeof(struct imsm_disk); | |
3980 | size += 2 * sizeof(struct imsm_dev); | |
3981 | /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */ | |
3982 | size += (4 - 2) * sizeof(struct imsm_map); | |
3983 | /* 4 possible disk_ord_tbl's */ | |
3984 | size += 4 * (disks - 1) * sizeof(__u32); | |
bbab0940 TM |
3985 | /* maximum bbm log */ |
3986 | size += sizeof(struct bbm_log); | |
c2c087e6 DW |
3987 | |
3988 | return size; | |
3989 | } | |
3990 | ||
387fcd59 N |
3991 | static __u64 avail_size_imsm(struct supertype *st, __u64 devsize, |
3992 | unsigned long long data_offset) | |
c2c087e6 DW |
3993 | { |
3994 | if (devsize < (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS)) | |
3995 | return 0; | |
3996 | ||
3997 | return devsize - (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS); | |
cdddbdbc DW |
3998 | } |
3999 | ||
ba2de7ba DW |
4000 | static void free_devlist(struct intel_super *super) |
4001 | { | |
4002 | struct intel_dev *dv; | |
4003 | ||
4004 | while (super->devlist) { | |
4005 | dv = super->devlist->next; | |
4006 | free(super->devlist->dev); | |
4007 | free(super->devlist); | |
4008 | super->devlist = dv; | |
4009 | } | |
4010 | } | |
4011 | ||
4012 | static void imsm_copy_dev(struct imsm_dev *dest, struct imsm_dev *src) | |
4013 | { | |
4014 | memcpy(dest, src, sizeof_imsm_dev(src, 0)); | |
4015 | } | |
4016 | ||
c7b8547c MT |
4017 | static int compare_super_imsm(struct supertype *st, struct supertype *tst, |
4018 | int verbose) | |
cdddbdbc | 4019 | { |
601ffa78 | 4020 | /* return: |
cdddbdbc | 4021 | * 0 same, or first was empty, and second was copied |
601ffa78 | 4022 | * 1 sb are different |
cdddbdbc DW |
4023 | */ |
4024 | struct intel_super *first = st->sb; | |
4025 | struct intel_super *sec = tst->sb; | |
4026 | ||
5d500228 N |
4027 | if (!first) { |
4028 | st->sb = tst->sb; | |
4029 | tst->sb = NULL; | |
4030 | return 0; | |
4031 | } | |
601ffa78 | 4032 | |
8603ea6f LM |
4033 | /* in platform dependent environment test if the disks |
4034 | * use the same Intel hba | |
601ffa78 OS |
4035 | * if not on Intel hba at all, allow anything. |
4036 | * doesn't check HBAs if num_raid_devs is not set, as it means | |
4037 | * it is a free floating spare, and all spares regardless of HBA type | |
4038 | * will fall into separate container during the assembly | |
8603ea6f | 4039 | */ |
601ffa78 | 4040 | if (first->hba && sec->hba && first->anchor->num_raid_devs != 0) { |
6b781d33 | 4041 | if (first->hba->type != sec->hba->type) { |
c7b8547c MT |
4042 | if (verbose) |
4043 | pr_err("HBAs of devices do not match %s != %s\n", | |
4044 | get_sys_dev_type(first->hba->type), | |
4045 | get_sys_dev_type(sec->hba->type)); | |
601ffa78 | 4046 | return 1; |
6b781d33 AP |
4047 | } |
4048 | if (first->orom != sec->orom) { | |
c7b8547c MT |
4049 | if (verbose) |
4050 | pr_err("HBAs of devices do not match %s != %s\n", | |
4051 | first->hba->pci_id, sec->hba->pci_id); | |
601ffa78 | 4052 | return 1; |
8603ea6f LM |
4053 | } |
4054 | } | |
cdddbdbc | 4055 | |
d23fe947 DW |
4056 | if (first->anchor->num_raid_devs > 0 && |
4057 | sec->anchor->num_raid_devs > 0) { | |
a2b97981 DW |
4058 | /* Determine if these disks might ever have been |
4059 | * related. Further disambiguation can only take place | |
4060 | * in load_super_imsm_all | |
4061 | */ | |
4062 | __u32 first_family = first->anchor->orig_family_num; | |
4063 | __u32 sec_family = sec->anchor->orig_family_num; | |
4064 | ||
f796af5d DW |
4065 | if (memcmp(first->anchor->sig, sec->anchor->sig, |
4066 | MAX_SIGNATURE_LENGTH) != 0) | |
601ffa78 | 4067 | return 1; |
f796af5d | 4068 | |
a2b97981 DW |
4069 | if (first_family == 0) |
4070 | first_family = first->anchor->family_num; | |
4071 | if (sec_family == 0) | |
4072 | sec_family = sec->anchor->family_num; | |
4073 | ||
4074 | if (first_family != sec_family) | |
601ffa78 | 4075 | return 1; |
f796af5d | 4076 | |
d23fe947 | 4077 | } |
cdddbdbc | 4078 | |
601ffa78 OS |
4079 | /* if an anchor does not have num_raid_devs set then it is a free |
4080 | * floating spare. don't assosiate spare with any array, as during assembly | |
4081 | * spares shall fall into separate container, from which they can be moved | |
4082 | * when necessary | |
4083 | */ | |
4084 | if (first->anchor->num_raid_devs ^ sec->anchor->num_raid_devs) | |
4085 | return 1; | |
3e372e5a | 4086 | |
cdddbdbc DW |
4087 | return 0; |
4088 | } | |
4089 | ||
0030e8d6 DW |
4090 | static void fd2devname(int fd, char *name) |
4091 | { | |
0030e8d6 | 4092 | char *nm; |
0030e8d6 | 4093 | |
7c798f87 MT |
4094 | nm = fd2kname(fd); |
4095 | if (!nm) | |
0030e8d6 | 4096 | return; |
9587c373 | 4097 | |
7c798f87 | 4098 | snprintf(name, MAX_RAID_SERIAL_LEN, "/dev/%s", nm); |
0030e8d6 DW |
4099 | } |
4100 | ||
21e9380b AP |
4101 | static int nvme_get_serial(int fd, void *buf, size_t buf_len) |
4102 | { | |
fcebeb77 | 4103 | char path[PATH_MAX]; |
21e9380b AP |
4104 | char *name = fd2kname(fd); |
4105 | ||
4106 | if (!name) | |
4107 | return 1; | |
4108 | ||
4109 | if (strncmp(name, "nvme", 4) != 0) | |
4110 | return 1; | |
4111 | ||
fcebeb77 MT |
4112 | if (!diskfd_to_devpath(fd, 1, path)) |
4113 | return 1; | |
21e9380b | 4114 | |
fcebeb77 | 4115 | return devpath_to_char(path, "serial", buf, buf_len, 0); |
21e9380b AP |
4116 | } |
4117 | ||
cdddbdbc DW |
4118 | extern int scsi_get_serial(int fd, void *buf, size_t buf_len); |
4119 | ||
4120 | static int imsm_read_serial(int fd, char *devname, | |
6da53c0e | 4121 | __u8 *serial, size_t serial_buf_len) |
cdddbdbc | 4122 | { |
21e9380b | 4123 | char buf[50]; |
cdddbdbc | 4124 | int rv; |
6da53c0e | 4125 | size_t len; |
316e2bf4 DW |
4126 | char *dest; |
4127 | char *src; | |
21e9380b AP |
4128 | unsigned int i; |
4129 | ||
4130 | memset(buf, 0, sizeof(buf)); | |
cdddbdbc | 4131 | |
21e9380b | 4132 | rv = nvme_get_serial(fd, buf, sizeof(buf)); |
cdddbdbc | 4133 | |
21e9380b AP |
4134 | if (rv) |
4135 | rv = scsi_get_serial(fd, buf, sizeof(buf)); | |
f9ba0ff1 | 4136 | |
40ebbb9c | 4137 | if (rv && check_env("IMSM_DEVNAME_AS_SERIAL")) { |
f9ba0ff1 DW |
4138 | memset(serial, 0, MAX_RAID_SERIAL_LEN); |
4139 | fd2devname(fd, (char *) serial); | |
0030e8d6 DW |
4140 | return 0; |
4141 | } | |
4142 | ||
cdddbdbc DW |
4143 | if (rv != 0) { |
4144 | if (devname) | |
e7b84f9d N |
4145 | pr_err("Failed to retrieve serial for %s\n", |
4146 | devname); | |
cdddbdbc DW |
4147 | return rv; |
4148 | } | |
4149 | ||
316e2bf4 DW |
4150 | /* trim all whitespace and non-printable characters and convert |
4151 | * ':' to ';' | |
4152 | */ | |
21e9380b AP |
4153 | for (i = 0, dest = buf; i < sizeof(buf) && buf[i]; i++) { |
4154 | src = &buf[i]; | |
316e2bf4 DW |
4155 | if (*src > 0x20) { |
4156 | /* ':' is reserved for use in placeholder serial | |
4157 | * numbers for missing disks | |
4158 | */ | |
4159 | if (*src == ':') | |
4160 | *dest++ = ';'; | |
4161 | else | |
4162 | *dest++ = *src; | |
4163 | } | |
4164 | } | |
21e9380b AP |
4165 | len = dest - buf; |
4166 | dest = buf; | |
316e2bf4 | 4167 | |
6da53c0e BK |
4168 | if (len > serial_buf_len) { |
4169 | /* truncate leading characters */ | |
4170 | dest += len - serial_buf_len; | |
4171 | len = serial_buf_len; | |
316e2bf4 | 4172 | } |
5c3db629 | 4173 | |
6da53c0e | 4174 | memset(serial, 0, serial_buf_len); |
316e2bf4 | 4175 | memcpy(serial, dest, len); |
cdddbdbc DW |
4176 | |
4177 | return 0; | |
4178 | } | |
4179 | ||
1f24f035 DW |
4180 | static int serialcmp(__u8 *s1, __u8 *s2) |
4181 | { | |
4182 | return strncmp((char *) s1, (char *) s2, MAX_RAID_SERIAL_LEN); | |
4183 | } | |
4184 | ||
4185 | static void serialcpy(__u8 *dest, __u8 *src) | |
4186 | { | |
4187 | strncpy((char *) dest, (char *) src, MAX_RAID_SERIAL_LEN); | |
4188 | } | |
4189 | ||
54c2c1ea DW |
4190 | static struct dl *serial_to_dl(__u8 *serial, struct intel_super *super) |
4191 | { | |
4192 | struct dl *dl; | |
4193 | ||
4194 | for (dl = super->disks; dl; dl = dl->next) | |
4195 | if (serialcmp(dl->serial, serial) == 0) | |
4196 | break; | |
4197 | ||
4198 | return dl; | |
4199 | } | |
4200 | ||
a2b97981 DW |
4201 | static struct imsm_disk * |
4202 | __serial_to_disk(__u8 *serial, struct imsm_super *mpb, int *idx) | |
4203 | { | |
4204 | int i; | |
4205 | ||
4206 | for (i = 0; i < mpb->num_disks; i++) { | |
4207 | struct imsm_disk *disk = __get_imsm_disk(mpb, i); | |
4208 | ||
4209 | if (serialcmp(disk->serial, serial) == 0) { | |
4210 | if (idx) | |
4211 | *idx = i; | |
4212 | return disk; | |
4213 | } | |
4214 | } | |
4215 | ||
4216 | return NULL; | |
4217 | } | |
4218 | ||
cdddbdbc DW |
4219 | static int |
4220 | load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd) | |
4221 | { | |
a2b97981 | 4222 | struct imsm_disk *disk; |
cdddbdbc DW |
4223 | struct dl *dl; |
4224 | struct stat stb; | |
cdddbdbc | 4225 | int rv; |
a2b97981 | 4226 | char name[40]; |
d23fe947 DW |
4227 | __u8 serial[MAX_RAID_SERIAL_LEN]; |
4228 | ||
6da53c0e | 4229 | rv = imsm_read_serial(fd, devname, serial, MAX_RAID_SERIAL_LEN); |
d23fe947 DW |
4230 | |
4231 | if (rv != 0) | |
4232 | return 2; | |
4233 | ||
503975b9 | 4234 | dl = xcalloc(1, sizeof(*dl)); |
cdddbdbc | 4235 | |
a2b97981 DW |
4236 | fstat(fd, &stb); |
4237 | dl->major = major(stb.st_rdev); | |
4238 | dl->minor = minor(stb.st_rdev); | |
4239 | dl->next = super->disks; | |
4240 | dl->fd = keep_fd ? fd : -1; | |
4241 | assert(super->disks == NULL); | |
4242 | super->disks = dl; | |
4243 | serialcpy(dl->serial, serial); | |
4244 | dl->index = -2; | |
4245 | dl->e = NULL; | |
4246 | fd2devname(fd, name); | |
4247 | if (devname) | |
503975b9 | 4248 | dl->devname = xstrdup(devname); |
a2b97981 | 4249 | else |
503975b9 | 4250 | dl->devname = xstrdup(name); |
cdddbdbc | 4251 | |
d23fe947 | 4252 | /* look up this disk's index in the current anchor */ |
a2b97981 DW |
4253 | disk = __serial_to_disk(dl->serial, super->anchor, &dl->index); |
4254 | if (disk) { | |
4255 | dl->disk = *disk; | |
4256 | /* only set index on disks that are a member of a | |
4257 | * populated contianer, i.e. one with raid_devs | |
4258 | */ | |
4259 | if (is_failed(&dl->disk)) | |
3f6efecc | 4260 | dl->index = -2; |
2432ce9b | 4261 | else if (is_spare(&dl->disk) || is_journal(&dl->disk)) |
a2b97981 | 4262 | dl->index = -1; |
3f6efecc DW |
4263 | } |
4264 | ||
949c47a0 DW |
4265 | return 0; |
4266 | } | |
4267 | ||
0c046afd DW |
4268 | /* When migrating map0 contains the 'destination' state while map1 |
4269 | * contains the current state. When not migrating map0 contains the | |
4270 | * current state. This routine assumes that map[0].map_state is set to | |
4271 | * the current array state before being called. | |
4272 | * | |
4273 | * Migration is indicated by one of the following states | |
4274 | * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed) | |
e3bba0e0 | 4275 | * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal |
0c046afd | 4276 | * map1state=unitialized) |
1484e727 | 4277 | * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal |
0c046afd | 4278 | * map1state=normal) |
e3bba0e0 | 4279 | * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal |
0c046afd | 4280 | * map1state=degraded) |
8e59f3d8 AK |
4281 | * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal |
4282 | * map1state=normal) | |
0c046afd | 4283 | */ |
8e59f3d8 AK |
4284 | static void migrate(struct imsm_dev *dev, struct intel_super *super, |
4285 | __u8 to_state, int migr_type) | |
3393c6af | 4286 | { |
0c046afd | 4287 | struct imsm_map *dest; |
238c0a71 | 4288 | struct imsm_map *src = get_imsm_map(dev, MAP_0); |
3393c6af | 4289 | |
0c046afd | 4290 | dev->vol.migr_state = 1; |
1484e727 | 4291 | set_migr_type(dev, migr_type); |
4036e7ee | 4292 | set_vol_curr_migr_unit(dev, 0); |
238c0a71 | 4293 | dest = get_imsm_map(dev, MAP_1); |
0c046afd | 4294 | |
0556e1a2 | 4295 | /* duplicate and then set the target end state in map[0] */ |
3393c6af | 4296 | memcpy(dest, src, sizeof_imsm_map(src)); |
fb12a745 | 4297 | if (migr_type == MIGR_GEN_MIGR) { |
0556e1a2 DW |
4298 | __u32 ord; |
4299 | int i; | |
4300 | ||
4301 | for (i = 0; i < src->num_members; i++) { | |
4302 | ord = __le32_to_cpu(src->disk_ord_tbl[i]); | |
4303 | set_imsm_ord_tbl_ent(src, i, ord_to_idx(ord)); | |
4304 | } | |
4305 | } | |
4306 | ||
8e59f3d8 AK |
4307 | if (migr_type == MIGR_GEN_MIGR) |
4308 | /* Clear migration record */ | |
4309 | memset(super->migr_rec, 0, sizeof(struct migr_record)); | |
4310 | ||
0c046afd | 4311 | src->map_state = to_state; |
949c47a0 | 4312 | } |
f8f603f1 | 4313 | |
809da78e AK |
4314 | static void end_migration(struct imsm_dev *dev, struct intel_super *super, |
4315 | __u8 map_state) | |
f8f603f1 | 4316 | { |
238c0a71 AK |
4317 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
4318 | struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state == 0 ? | |
4319 | MAP_0 : MAP_1); | |
28bce06f | 4320 | int i, j; |
0556e1a2 DW |
4321 | |
4322 | /* merge any IMSM_ORD_REBUILD bits that were not successfully | |
4323 | * completed in the last migration. | |
4324 | * | |
28bce06f | 4325 | * FIXME add support for raid-level-migration |
0556e1a2 | 4326 | */ |
195d1d76 | 4327 | if (map_state != map->map_state && (is_gen_migration(dev) == false) && |
089f9d79 | 4328 | prev->map_state != IMSM_T_STATE_UNINITIALIZED) { |
809da78e AK |
4329 | /* when final map state is other than expected |
4330 | * merge maps (not for migration) | |
4331 | */ | |
4332 | int failed; | |
4333 | ||
4334 | for (i = 0; i < prev->num_members; i++) | |
4335 | for (j = 0; j < map->num_members; j++) | |
4336 | /* during online capacity expansion | |
4337 | * disks position can be changed | |
4338 | * if takeover is used | |
4339 | */ | |
4340 | if (ord_to_idx(map->disk_ord_tbl[j]) == | |
4341 | ord_to_idx(prev->disk_ord_tbl[i])) { | |
4342 | map->disk_ord_tbl[j] |= | |
4343 | prev->disk_ord_tbl[i]; | |
4344 | break; | |
4345 | } | |
4346 | failed = imsm_count_failed(super, dev, MAP_0); | |
4347 | map_state = imsm_check_degraded(super, dev, failed, MAP_0); | |
4348 | } | |
f8f603f1 DW |
4349 | |
4350 | dev->vol.migr_state = 0; | |
ea672ee1 | 4351 | set_migr_type(dev, 0); |
4036e7ee | 4352 | set_vol_curr_migr_unit(dev, 0); |
f8f603f1 DW |
4353 | map->map_state = map_state; |
4354 | } | |
949c47a0 DW |
4355 | |
4356 | static int parse_raid_devices(struct intel_super *super) | |
4357 | { | |
4358 | int i; | |
4359 | struct imsm_dev *dev_new; | |
4d7b1503 | 4360 | size_t len, len_migr; |
401d313b | 4361 | size_t max_len = 0; |
4d7b1503 DW |
4362 | size_t space_needed = 0; |
4363 | struct imsm_super *mpb = super->anchor; | |
949c47a0 DW |
4364 | |
4365 | for (i = 0; i < super->anchor->num_raid_devs; i++) { | |
4366 | struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i); | |
ba2de7ba | 4367 | struct intel_dev *dv; |
949c47a0 | 4368 | |
4d7b1503 DW |
4369 | len = sizeof_imsm_dev(dev_iter, 0); |
4370 | len_migr = sizeof_imsm_dev(dev_iter, 1); | |
4371 | if (len_migr > len) | |
4372 | space_needed += len_migr - len; | |
ca9de185 | 4373 | |
503975b9 | 4374 | dv = xmalloc(sizeof(*dv)); |
401d313b AK |
4375 | if (max_len < len_migr) |
4376 | max_len = len_migr; | |
4377 | if (max_len > len_migr) | |
4378 | space_needed += max_len - len_migr; | |
503975b9 | 4379 | dev_new = xmalloc(max_len); |
949c47a0 | 4380 | imsm_copy_dev(dev_new, dev_iter); |
ba2de7ba DW |
4381 | dv->dev = dev_new; |
4382 | dv->index = i; | |
4383 | dv->next = super->devlist; | |
4384 | super->devlist = dv; | |
949c47a0 | 4385 | } |
cdddbdbc | 4386 | |
4d7b1503 DW |
4387 | /* ensure that super->buf is large enough when all raid devices |
4388 | * are migrating | |
4389 | */ | |
4390 | if (__le32_to_cpu(mpb->mpb_size) + space_needed > super->len) { | |
4391 | void *buf; | |
4392 | ||
f36a9ecd PB |
4393 | len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + space_needed, |
4394 | super->sector_size); | |
4395 | if (posix_memalign(&buf, MAX_SECTOR_SIZE, len) != 0) | |
4d7b1503 DW |
4396 | return 1; |
4397 | ||
1f45a8ad DW |
4398 | memcpy(buf, super->buf, super->len); |
4399 | memset(buf + super->len, 0, len - super->len); | |
4d7b1503 DW |
4400 | free(super->buf); |
4401 | super->buf = buf; | |
4402 | super->len = len; | |
4403 | } | |
ca9de185 | 4404 | |
bbab0940 TM |
4405 | super->extra_space += space_needed; |
4406 | ||
cdddbdbc DW |
4407 | return 0; |
4408 | } | |
4409 | ||
e2f41b2c AK |
4410 | /******************************************************************************* |
4411 | * Function: check_mpb_migr_compatibility | |
4412 | * Description: Function checks for unsupported migration features: | |
4413 | * - migration optimization area (pba_of_lba0) | |
4414 | * - descending reshape (ascending_migr) | |
4415 | * Parameters: | |
4416 | * super : imsm metadata information | |
4417 | * Returns: | |
4418 | * 0 : migration is compatible | |
4419 | * -1 : migration is not compatible | |
4420 | ******************************************************************************/ | |
4421 | int check_mpb_migr_compatibility(struct intel_super *super) | |
4422 | { | |
4423 | struct imsm_map *map0, *map1; | |
4424 | struct migr_record *migr_rec = super->migr_rec; | |
4425 | int i; | |
4426 | ||
4427 | for (i = 0; i < super->anchor->num_raid_devs; i++) { | |
4428 | struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i); | |
4429 | ||
756a15f3 | 4430 | if (dev_iter->vol.migr_state == 1 && |
e2f41b2c AK |
4431 | dev_iter->vol.migr_type == MIGR_GEN_MIGR) { |
4432 | /* This device is migrating */ | |
238c0a71 AK |
4433 | map0 = get_imsm_map(dev_iter, MAP_0); |
4434 | map1 = get_imsm_map(dev_iter, MAP_1); | |
5551b113 | 4435 | if (pba_of_lba0(map0) != pba_of_lba0(map1)) |
e2f41b2c AK |
4436 | /* migration optimization area was used */ |
4437 | return -1; | |
fc54fe7a JS |
4438 | if (migr_rec->ascending_migr == 0 && |
4439 | migr_rec->dest_depth_per_unit > 0) | |
e2f41b2c AK |
4440 | /* descending reshape not supported yet */ |
4441 | return -1; | |
4442 | } | |
4443 | } | |
4444 | return 0; | |
4445 | } | |
4446 | ||
d23fe947 | 4447 | static void __free_imsm(struct intel_super *super, int free_disks); |
9ca2c81c | 4448 | |
cdddbdbc | 4449 | /* load_imsm_mpb - read matrix metadata |
f2f5c343 | 4450 | * allocates super->mpb to be freed by free_imsm |
cdddbdbc DW |
4451 | */ |
4452 | static int load_imsm_mpb(int fd, struct intel_super *super, char *devname) | |
4453 | { | |
4454 | unsigned long long dsize; | |
cdddbdbc | 4455 | unsigned long long sectors; |
f36a9ecd | 4456 | unsigned int sector_size = super->sector_size; |
cdddbdbc | 4457 | struct stat; |
6416d527 | 4458 | struct imsm_super *anchor; |
cdddbdbc DW |
4459 | __u32 check_sum; |
4460 | ||
cdddbdbc | 4461 | get_dev_size(fd, NULL, &dsize); |
f36a9ecd | 4462 | if (dsize < 2*sector_size) { |
64436f06 | 4463 | if (devname) |
e7b84f9d N |
4464 | pr_err("%s: device to small for imsm\n", |
4465 | devname); | |
64436f06 N |
4466 | return 1; |
4467 | } | |
cdddbdbc | 4468 | |
f36a9ecd | 4469 | if (lseek64(fd, dsize - (sector_size * 2), SEEK_SET) < 0) { |
cdddbdbc | 4470 | if (devname) |
e7b84f9d N |
4471 | pr_err("Cannot seek to anchor block on %s: %s\n", |
4472 | devname, strerror(errno)); | |
cdddbdbc DW |
4473 | return 1; |
4474 | } | |
4475 | ||
f36a9ecd | 4476 | if (posix_memalign((void **)&anchor, sector_size, sector_size) != 0) { |
ad97895e | 4477 | if (devname) |
7a862a02 | 4478 | pr_err("Failed to allocate imsm anchor buffer on %s\n", devname); |
ad97895e DW |
4479 | return 1; |
4480 | } | |
466070ad | 4481 | if ((unsigned int)read(fd, anchor, sector_size) != sector_size) { |
cdddbdbc | 4482 | if (devname) |
e7b84f9d N |
4483 | pr_err("Cannot read anchor block on %s: %s\n", |
4484 | devname, strerror(errno)); | |
6416d527 | 4485 | free(anchor); |
cdddbdbc DW |
4486 | return 1; |
4487 | } | |
4488 | ||
6416d527 | 4489 | if (strncmp((char *) anchor->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) { |
cdddbdbc | 4490 | if (devname) |
e7b84f9d | 4491 | pr_err("no IMSM anchor on %s\n", devname); |
6416d527 | 4492 | free(anchor); |
cdddbdbc DW |
4493 | return 2; |
4494 | } | |
4495 | ||
d23fe947 | 4496 | __free_imsm(super, 0); |
f2f5c343 LM |
4497 | /* reload capability and hba */ |
4498 | ||
4499 | /* capability and hba must be updated with new super allocation */ | |
d424212e | 4500 | find_intel_hba_capability(fd, super, devname); |
f36a9ecd PB |
4501 | super->len = ROUND_UP(anchor->mpb_size, sector_size); |
4502 | if (posix_memalign(&super->buf, MAX_SECTOR_SIZE, super->len) != 0) { | |
cdddbdbc | 4503 | if (devname) |
e7b84f9d N |
4504 | pr_err("unable to allocate %zu byte mpb buffer\n", |
4505 | super->len); | |
6416d527 | 4506 | free(anchor); |
cdddbdbc DW |
4507 | return 2; |
4508 | } | |
f36a9ecd | 4509 | memcpy(super->buf, anchor, sector_size); |
cdddbdbc | 4510 | |
f36a9ecd | 4511 | sectors = mpb_sectors(anchor, sector_size) - 1; |
6416d527 | 4512 | free(anchor); |
8e59f3d8 | 4513 | |
85337573 AO |
4514 | if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE, |
4515 | MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE) != 0) { | |
1ade5cc1 | 4516 | pr_err("could not allocate migr_rec buffer\n"); |
8e59f3d8 AK |
4517 | free(super->buf); |
4518 | return 2; | |
4519 | } | |
51d83f5d | 4520 | super->clean_migration_record_by_mdmon = 0; |
8e59f3d8 | 4521 | |
949c47a0 | 4522 | if (!sectors) { |
ecf45690 DW |
4523 | check_sum = __gen_imsm_checksum(super->anchor); |
4524 | if (check_sum != __le32_to_cpu(super->anchor->check_sum)) { | |
4525 | if (devname) | |
e7b84f9d N |
4526 | pr_err("IMSM checksum %x != %x on %s\n", |
4527 | check_sum, | |
4528 | __le32_to_cpu(super->anchor->check_sum), | |
4529 | devname); | |
ecf45690 DW |
4530 | return 2; |
4531 | } | |
4532 | ||
a2b97981 | 4533 | return 0; |
949c47a0 | 4534 | } |
cdddbdbc DW |
4535 | |
4536 | /* read the extended mpb */ | |
f36a9ecd | 4537 | if (lseek64(fd, dsize - (sector_size * (2 + sectors)), SEEK_SET) < 0) { |
cdddbdbc | 4538 | if (devname) |
e7b84f9d N |
4539 | pr_err("Cannot seek to extended mpb on %s: %s\n", |
4540 | devname, strerror(errno)); | |
cdddbdbc DW |
4541 | return 1; |
4542 | } | |
4543 | ||
f36a9ecd PB |
4544 | if ((unsigned int)read(fd, super->buf + sector_size, |
4545 | super->len - sector_size) != super->len - sector_size) { | |
cdddbdbc | 4546 | if (devname) |
e7b84f9d N |
4547 | pr_err("Cannot read extended mpb on %s: %s\n", |
4548 | devname, strerror(errno)); | |
cdddbdbc DW |
4549 | return 2; |
4550 | } | |
4551 | ||
949c47a0 DW |
4552 | check_sum = __gen_imsm_checksum(super->anchor); |
4553 | if (check_sum != __le32_to_cpu(super->anchor->check_sum)) { | |
cdddbdbc | 4554 | if (devname) |
e7b84f9d N |
4555 | pr_err("IMSM checksum %x != %x on %s\n", |
4556 | check_sum, __le32_to_cpu(super->anchor->check_sum), | |
4557 | devname); | |
db575f3b | 4558 | return 3; |
cdddbdbc DW |
4559 | } |
4560 | ||
a2b97981 DW |
4561 | return 0; |
4562 | } | |
4563 | ||
8e59f3d8 AK |
4564 | static int read_imsm_migr_rec(int fd, struct intel_super *super); |
4565 | ||
97f81ee2 CA |
4566 | /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */ |
4567 | static void clear_hi(struct intel_super *super) | |
4568 | { | |
4569 | struct imsm_super *mpb = super->anchor; | |
4570 | int i, n; | |
4571 | if (mpb->attributes & MPB_ATTRIB_2TB_DISK) | |
4572 | return; | |
4573 | for (i = 0; i < mpb->num_disks; ++i) { | |
4574 | struct imsm_disk *disk = &mpb->disk[i]; | |
4575 | disk->total_blocks_hi = 0; | |
4576 | } | |
4577 | for (i = 0; i < mpb->num_raid_devs; ++i) { | |
4578 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
97f81ee2 CA |
4579 | for (n = 0; n < 2; ++n) { |
4580 | struct imsm_map *map = get_imsm_map(dev, n); | |
4581 | if (!map) | |
4582 | continue; | |
4583 | map->pba_of_lba0_hi = 0; | |
4584 | map->blocks_per_member_hi = 0; | |
4585 | map->num_data_stripes_hi = 0; | |
4586 | } | |
4587 | } | |
4588 | } | |
4589 | ||
a2b97981 DW |
4590 | static int |
4591 | load_and_parse_mpb(int fd, struct intel_super *super, char *devname, int keep_fd) | |
4592 | { | |
4593 | int err; | |
4594 | ||
4595 | err = load_imsm_mpb(fd, super, devname); | |
4596 | if (err) | |
4597 | return err; | |
f36a9ecd PB |
4598 | if (super->sector_size == 4096) |
4599 | convert_from_4k(super); | |
a2b97981 DW |
4600 | err = load_imsm_disk(fd, super, devname, keep_fd); |
4601 | if (err) | |
4602 | return err; | |
4603 | err = parse_raid_devices(super); | |
8d67477f TM |
4604 | if (err) |
4605 | return err; | |
4606 | err = load_bbm_log(super); | |
97f81ee2 | 4607 | clear_hi(super); |
a2b97981 | 4608 | return err; |
cdddbdbc DW |
4609 | } |
4610 | ||
4389ce73 | 4611 | static void __free_imsm_disk(struct dl *d, int do_close) |
ae6aad82 | 4612 | { |
4389ce73 MT |
4613 | if (do_close) |
4614 | close_fd(&d->fd); | |
ae6aad82 DW |
4615 | if (d->devname) |
4616 | free(d->devname); | |
0dcecb2e DW |
4617 | if (d->e) |
4618 | free(d->e); | |
ae6aad82 DW |
4619 | free(d); |
4620 | ||
4621 | } | |
1a64be56 | 4622 | |
cdddbdbc DW |
4623 | static void free_imsm_disks(struct intel_super *super) |
4624 | { | |
47ee5a45 | 4625 | struct dl *d; |
cdddbdbc | 4626 | |
47ee5a45 DW |
4627 | while (super->disks) { |
4628 | d = super->disks; | |
cdddbdbc | 4629 | super->disks = d->next; |
3a85bf0e | 4630 | __free_imsm_disk(d, 1); |
cdddbdbc | 4631 | } |
cb82edca AK |
4632 | while (super->disk_mgmt_list) { |
4633 | d = super->disk_mgmt_list; | |
4634 | super->disk_mgmt_list = d->next; | |
3a85bf0e | 4635 | __free_imsm_disk(d, 1); |
cb82edca | 4636 | } |
47ee5a45 DW |
4637 | while (super->missing) { |
4638 | d = super->missing; | |
4639 | super->missing = d->next; | |
3a85bf0e | 4640 | __free_imsm_disk(d, 1); |
47ee5a45 DW |
4641 | } |
4642 | ||
cdddbdbc DW |
4643 | } |
4644 | ||
9ca2c81c | 4645 | /* free all the pieces hanging off of a super pointer */ |
d23fe947 | 4646 | static void __free_imsm(struct intel_super *super, int free_disks) |
cdddbdbc | 4647 | { |
88654014 LM |
4648 | struct intel_hba *elem, *next; |
4649 | ||
9ca2c81c | 4650 | if (super->buf) { |
949c47a0 | 4651 | free(super->buf); |
9ca2c81c DW |
4652 | super->buf = NULL; |
4653 | } | |
f2f5c343 LM |
4654 | /* unlink capability description */ |
4655 | super->orom = NULL; | |
8e59f3d8 AK |
4656 | if (super->migr_rec_buf) { |
4657 | free(super->migr_rec_buf); | |
4658 | super->migr_rec_buf = NULL; | |
4659 | } | |
d23fe947 DW |
4660 | if (free_disks) |
4661 | free_imsm_disks(super); | |
ba2de7ba | 4662 | free_devlist(super); |
88654014 LM |
4663 | elem = super->hba; |
4664 | while (elem) { | |
4665 | if (elem->path) | |
4666 | free((void *)elem->path); | |
4667 | next = elem->next; | |
4668 | free(elem); | |
4669 | elem = next; | |
88c32bb1 | 4670 | } |
8d67477f TM |
4671 | if (super->bbm_log) |
4672 | free(super->bbm_log); | |
88654014 | 4673 | super->hba = NULL; |
cdddbdbc DW |
4674 | } |
4675 | ||
9ca2c81c DW |
4676 | static void free_imsm(struct intel_super *super) |
4677 | { | |
d23fe947 | 4678 | __free_imsm(super, 1); |
928f1424 | 4679 | free(super->bb.entries); |
9ca2c81c DW |
4680 | free(super); |
4681 | } | |
cdddbdbc DW |
4682 | |
4683 | static void free_super_imsm(struct supertype *st) | |
4684 | { | |
4685 | struct intel_super *super = st->sb; | |
4686 | ||
4687 | if (!super) | |
4688 | return; | |
4689 | ||
4690 | free_imsm(super); | |
4691 | st->sb = NULL; | |
4692 | } | |
4693 | ||
49133e57 | 4694 | static struct intel_super *alloc_super(void) |
c2c087e6 | 4695 | { |
503975b9 | 4696 | struct intel_super *super = xcalloc(1, sizeof(*super)); |
c2c087e6 | 4697 | |
503975b9 N |
4698 | super->current_vol = -1; |
4699 | super->create_offset = ~((unsigned long long) 0); | |
928f1424 TM |
4700 | |
4701 | super->bb.entries = xmalloc(BBM_LOG_MAX_ENTRIES * | |
4702 | sizeof(struct md_bb_entry)); | |
4703 | if (!super->bb.entries) { | |
4704 | free(super); | |
4705 | return NULL; | |
4706 | } | |
4707 | ||
c2c087e6 DW |
4708 | return super; |
4709 | } | |
4710 | ||
f0f5a016 LM |
4711 | /* |
4712 | * find and allocate hba and OROM/EFI based on valid fd of RAID component device | |
4713 | */ | |
d424212e | 4714 | static int find_intel_hba_capability(int fd, struct intel_super *super, char *devname) |
f0f5a016 LM |
4715 | { |
4716 | struct sys_dev *hba_name; | |
4717 | int rv = 0; | |
4718 | ||
4389ce73 | 4719 | if (is_fd_valid(fd) && test_partition(fd)) { |
3a30e28e MT |
4720 | pr_err("imsm: %s is a partition, cannot be used in IMSM\n", |
4721 | devname); | |
4722 | return 1; | |
4723 | } | |
4389ce73 | 4724 | if (!is_fd_valid(fd) || check_env("IMSM_NO_PLATFORM")) { |
f2f5c343 | 4725 | super->orom = NULL; |
f0f5a016 LM |
4726 | super->hba = NULL; |
4727 | return 0; | |
4728 | } | |
4729 | hba_name = find_disk_attached_hba(fd, NULL); | |
4730 | if (!hba_name) { | |
d424212e | 4731 | if (devname) |
e7b84f9d N |
4732 | pr_err("%s is not attached to Intel(R) RAID controller.\n", |
4733 | devname); | |
f0f5a016 LM |
4734 | return 1; |
4735 | } | |
4736 | rv = attach_hba_to_super(super, hba_name); | |
4737 | if (rv == 2) { | |
d424212e N |
4738 | if (devname) { |
4739 | struct intel_hba *hba = super->hba; | |
f0f5a016 | 4740 | |
60f0f54d PB |
4741 | pr_err("%s is attached to Intel(R) %s %s (%s),\n" |
4742 | " but the container is assigned to Intel(R) %s %s (", | |
d424212e | 4743 | devname, |
614902f6 | 4744 | get_sys_dev_type(hba_name->type), |
60f0f54d | 4745 | hba_name->type == SYS_DEV_VMD ? "domain" : "RAID controller", |
f0f5a016 | 4746 | hba_name->pci_id ? : "Err!", |
60f0f54d PB |
4747 | get_sys_dev_type(super->hba->type), |
4748 | hba->type == SYS_DEV_VMD ? "domain" : "RAID controller"); | |
f0f5a016 | 4749 | |
f0f5a016 LM |
4750 | while (hba) { |
4751 | fprintf(stderr, "%s", hba->pci_id ? : "Err!"); | |
4752 | if (hba->next) | |
4753 | fprintf(stderr, ", "); | |
4754 | hba = hba->next; | |
4755 | } | |
6b781d33 | 4756 | fprintf(stderr, ").\n" |
cca67208 | 4757 | " Mixing devices attached to different controllers is not allowed.\n"); |
f0f5a016 | 4758 | } |
f0f5a016 LM |
4759 | return 2; |
4760 | } | |
6b781d33 | 4761 | super->orom = find_imsm_capability(hba_name); |
f2f5c343 LM |
4762 | if (!super->orom) |
4763 | return 3; | |
614902f6 | 4764 | |
f0f5a016 LM |
4765 | return 0; |
4766 | } | |
4767 | ||
47ee5a45 DW |
4768 | /* find_missing - helper routine for load_super_imsm_all that identifies |
4769 | * disks that have disappeared from the system. This routine relies on | |
4770 | * the mpb being uptodate, which it is at load time. | |
4771 | */ | |
4772 | static int find_missing(struct intel_super *super) | |
4773 | { | |
4774 | int i; | |
4775 | struct imsm_super *mpb = super->anchor; | |
4776 | struct dl *dl; | |
4777 | struct imsm_disk *disk; | |
47ee5a45 DW |
4778 | |
4779 | for (i = 0; i < mpb->num_disks; i++) { | |
4780 | disk = __get_imsm_disk(mpb, i); | |
54c2c1ea | 4781 | dl = serial_to_dl(disk->serial, super); |
47ee5a45 DW |
4782 | if (dl) |
4783 | continue; | |
47ee5a45 | 4784 | |
503975b9 | 4785 | dl = xmalloc(sizeof(*dl)); |
47ee5a45 DW |
4786 | dl->major = 0; |
4787 | dl->minor = 0; | |
4788 | dl->fd = -1; | |
503975b9 | 4789 | dl->devname = xstrdup("missing"); |
47ee5a45 DW |
4790 | dl->index = i; |
4791 | serialcpy(dl->serial, disk->serial); | |
4792 | dl->disk = *disk; | |
689c9bf3 | 4793 | dl->e = NULL; |
47ee5a45 DW |
4794 | dl->next = super->missing; |
4795 | super->missing = dl; | |
4796 | } | |
4797 | ||
4798 | return 0; | |
4799 | } | |
4800 | ||
a2b97981 DW |
4801 | static struct intel_disk *disk_list_get(__u8 *serial, struct intel_disk *disk_list) |
4802 | { | |
4803 | struct intel_disk *idisk = disk_list; | |
4804 | ||
4805 | while (idisk) { | |
4806 | if (serialcmp(idisk->disk.serial, serial) == 0) | |
4807 | break; | |
4808 | idisk = idisk->next; | |
4809 | } | |
4810 | ||
4811 | return idisk; | |
4812 | } | |
4813 | ||
4814 | static int __prep_thunderdome(struct intel_super **table, int tbl_size, | |
4815 | struct intel_super *super, | |
4816 | struct intel_disk **disk_list) | |
4817 | { | |
4818 | struct imsm_disk *d = &super->disks->disk; | |
4819 | struct imsm_super *mpb = super->anchor; | |
4820 | int i, j; | |
4821 | ||
4822 | for (i = 0; i < tbl_size; i++) { | |
4823 | struct imsm_super *tbl_mpb = table[i]->anchor; | |
4824 | struct imsm_disk *tbl_d = &table[i]->disks->disk; | |
4825 | ||
4826 | if (tbl_mpb->family_num == mpb->family_num) { | |
4827 | if (tbl_mpb->check_sum == mpb->check_sum) { | |
1ade5cc1 N |
4828 | dprintf("mpb from %d:%d matches %d:%d\n", |
4829 | super->disks->major, | |
a2b97981 DW |
4830 | super->disks->minor, |
4831 | table[i]->disks->major, | |
4832 | table[i]->disks->minor); | |
4833 | break; | |
4834 | } | |
4835 | ||
4836 | if (((is_configured(d) && !is_configured(tbl_d)) || | |
4837 | is_configured(d) == is_configured(tbl_d)) && | |
4838 | tbl_mpb->generation_num < mpb->generation_num) { | |
4839 | /* current version of the mpb is a | |
4840 | * better candidate than the one in | |
4841 | * super_table, but copy over "cross | |
4842 | * generational" status | |
4843 | */ | |
4844 | struct intel_disk *idisk; | |
4845 | ||
1ade5cc1 N |
4846 | dprintf("mpb from %d:%d replaces %d:%d\n", |
4847 | super->disks->major, | |
a2b97981 DW |
4848 | super->disks->minor, |
4849 | table[i]->disks->major, | |
4850 | table[i]->disks->minor); | |
4851 | ||
4852 | idisk = disk_list_get(tbl_d->serial, *disk_list); | |
4853 | if (idisk && is_failed(&idisk->disk)) | |
4854 | tbl_d->status |= FAILED_DISK; | |
4855 | break; | |
4856 | } else { | |
4857 | struct intel_disk *idisk; | |
4858 | struct imsm_disk *disk; | |
4859 | ||
4860 | /* tbl_mpb is more up to date, but copy | |
4861 | * over cross generational status before | |
4862 | * returning | |
4863 | */ | |
4864 | disk = __serial_to_disk(d->serial, mpb, NULL); | |
4865 | if (disk && is_failed(disk)) | |
4866 | d->status |= FAILED_DISK; | |
4867 | ||
4868 | idisk = disk_list_get(d->serial, *disk_list); | |
4869 | if (idisk) { | |
4870 | idisk->owner = i; | |
4871 | if (disk && is_configured(disk)) | |
4872 | idisk->disk.status |= CONFIGURED_DISK; | |
4873 | } | |
4874 | ||
1ade5cc1 N |
4875 | dprintf("mpb from %d:%d prefer %d:%d\n", |
4876 | super->disks->major, | |
a2b97981 DW |
4877 | super->disks->minor, |
4878 | table[i]->disks->major, | |
4879 | table[i]->disks->minor); | |
4880 | ||
4881 | return tbl_size; | |
4882 | } | |
4883 | } | |
4884 | } | |
4885 | ||
4886 | if (i >= tbl_size) | |
4887 | table[tbl_size++] = super; | |
4888 | else | |
4889 | table[i] = super; | |
4890 | ||
4891 | /* update/extend the merged list of imsm_disk records */ | |
4892 | for (j = 0; j < mpb->num_disks; j++) { | |
4893 | struct imsm_disk *disk = __get_imsm_disk(mpb, j); | |
4894 | struct intel_disk *idisk; | |
4895 | ||
4896 | idisk = disk_list_get(disk->serial, *disk_list); | |
4897 | if (idisk) { | |
4898 | idisk->disk.status |= disk->status; | |
4899 | if (is_configured(&idisk->disk) || | |
4900 | is_failed(&idisk->disk)) | |
4901 | idisk->disk.status &= ~(SPARE_DISK); | |
4902 | } else { | |
503975b9 | 4903 | idisk = xcalloc(1, sizeof(*idisk)); |
a2b97981 DW |
4904 | idisk->owner = IMSM_UNKNOWN_OWNER; |
4905 | idisk->disk = *disk; | |
4906 | idisk->next = *disk_list; | |
4907 | *disk_list = idisk; | |
4908 | } | |
4909 | ||
4910 | if (serialcmp(idisk->disk.serial, d->serial) == 0) | |
4911 | idisk->owner = i; | |
4912 | } | |
4913 | ||
4914 | return tbl_size; | |
4915 | } | |
4916 | ||
4917 | static struct intel_super * | |
4918 | validate_members(struct intel_super *super, struct intel_disk *disk_list, | |
4919 | const int owner) | |
4920 | { | |
4921 | struct imsm_super *mpb = super->anchor; | |
4922 | int ok_count = 0; | |
4923 | int i; | |
4924 | ||
4925 | for (i = 0; i < mpb->num_disks; i++) { | |
4926 | struct imsm_disk *disk = __get_imsm_disk(mpb, i); | |
4927 | struct intel_disk *idisk; | |
4928 | ||
4929 | idisk = disk_list_get(disk->serial, disk_list); | |
4930 | if (idisk) { | |
4931 | if (idisk->owner == owner || | |
4932 | idisk->owner == IMSM_UNKNOWN_OWNER) | |
4933 | ok_count++; | |
4934 | else | |
1ade5cc1 N |
4935 | dprintf("'%.16s' owner %d != %d\n", |
4936 | disk->serial, idisk->owner, | |
a2b97981 DW |
4937 | owner); |
4938 | } else { | |
1ade5cc1 N |
4939 | dprintf("unknown disk %x [%d]: %.16s\n", |
4940 | __le32_to_cpu(mpb->family_num), i, | |
a2b97981 DW |
4941 | disk->serial); |
4942 | break; | |
4943 | } | |
4944 | } | |
4945 | ||
4946 | if (ok_count == mpb->num_disks) | |
4947 | return super; | |
4948 | return NULL; | |
4949 | } | |
4950 | ||
4951 | static void show_conflicts(__u32 family_num, struct intel_super *super_list) | |
4952 | { | |
4953 | struct intel_super *s; | |
4954 | ||
4955 | for (s = super_list; s; s = s->next) { | |
4956 | if (family_num != s->anchor->family_num) | |
4957 | continue; | |
e12b3daa | 4958 | pr_err("Conflict, offlining family %#x on '%s'\n", |
a2b97981 DW |
4959 | __le32_to_cpu(family_num), s->disks->devname); |
4960 | } | |
4961 | } | |
4962 | ||
4963 | static struct intel_super * | |
4964 | imsm_thunderdome(struct intel_super **super_list, int len) | |
4965 | { | |
4966 | struct intel_super *super_table[len]; | |
4967 | struct intel_disk *disk_list = NULL; | |
4968 | struct intel_super *champion, *spare; | |
4969 | struct intel_super *s, **del; | |
4970 | int tbl_size = 0; | |
4971 | int conflict; | |
4972 | int i; | |
4973 | ||
4974 | memset(super_table, 0, sizeof(super_table)); | |
4975 | for (s = *super_list; s; s = s->next) | |
4976 | tbl_size = __prep_thunderdome(super_table, tbl_size, s, &disk_list); | |
4977 | ||
4978 | for (i = 0; i < tbl_size; i++) { | |
4979 | struct imsm_disk *d; | |
4980 | struct intel_disk *idisk; | |
4981 | struct imsm_super *mpb = super_table[i]->anchor; | |
4982 | ||
4983 | s = super_table[i]; | |
4984 | d = &s->disks->disk; | |
4985 | ||
4986 | /* 'd' must appear in merged disk list for its | |
4987 | * configuration to be valid | |
4988 | */ | |
4989 | idisk = disk_list_get(d->serial, disk_list); | |
4990 | if (idisk && idisk->owner == i) | |
4991 | s = validate_members(s, disk_list, i); | |
4992 | else | |
4993 | s = NULL; | |
4994 | ||
4995 | if (!s) | |
1ade5cc1 N |
4996 | dprintf("marking family: %#x from %d:%d offline\n", |
4997 | mpb->family_num, | |
a2b97981 DW |
4998 | super_table[i]->disks->major, |
4999 | super_table[i]->disks->minor); | |
5000 | super_table[i] = s; | |
5001 | } | |
5002 | ||
5003 | /* This is where the mdadm implementation differs from the Windows | |
5004 | * driver which has no strict concept of a container. We can only | |
5005 | * assemble one family from a container, so when returning a prodigal | |
5006 | * array member to this system the code will not be able to disambiguate | |
5007 | * the container contents that should be assembled ("foreign" versus | |
5008 | * "local"). It requires user intervention to set the orig_family_num | |
5009 | * to a new value to establish a new container. The Windows driver in | |
5010 | * this situation fixes up the volume name in place and manages the | |
5011 | * foreign array as an independent entity. | |
5012 | */ | |
5013 | s = NULL; | |
5014 | spare = NULL; | |
5015 | conflict = 0; | |
5016 | for (i = 0; i < tbl_size; i++) { | |
5017 | struct intel_super *tbl_ent = super_table[i]; | |
5018 | int is_spare = 0; | |
5019 | ||
5020 | if (!tbl_ent) | |
5021 | continue; | |
5022 | ||
5023 | if (tbl_ent->anchor->num_raid_devs == 0) { | |
5024 | spare = tbl_ent; | |
5025 | is_spare = 1; | |
5026 | } | |
5027 | ||
5028 | if (s && !is_spare) { | |
5029 | show_conflicts(tbl_ent->anchor->family_num, *super_list); | |
5030 | conflict++; | |
5031 | } else if (!s && !is_spare) | |
5032 | s = tbl_ent; | |
5033 | } | |
5034 | ||
5035 | if (!s) | |
5036 | s = spare; | |
5037 | if (!s) { | |
5038 | champion = NULL; | |
5039 | goto out; | |
5040 | } | |
5041 | champion = s; | |
5042 | ||
5043 | if (conflict) | |
7a862a02 | 5044 | pr_err("Chose family %#x on '%s', assemble conflicts to new container with '--update=uuid'\n", |
a2b97981 DW |
5045 | __le32_to_cpu(s->anchor->family_num), s->disks->devname); |
5046 | ||
5047 | /* collect all dl's onto 'champion', and update them to | |
5048 | * champion's version of the status | |
5049 | */ | |
5050 | for (s = *super_list; s; s = s->next) { | |
5051 | struct imsm_super *mpb = champion->anchor; | |
5052 | struct dl *dl = s->disks; | |
5053 | ||
5054 | if (s == champion) | |
5055 | continue; | |
5056 | ||
5d7b407a CA |
5057 | mpb->attributes |= s->anchor->attributes & MPB_ATTRIB_2TB_DISK; |
5058 | ||
a2b97981 DW |
5059 | for (i = 0; i < mpb->num_disks; i++) { |
5060 | struct imsm_disk *disk; | |
5061 | ||
5062 | disk = __serial_to_disk(dl->serial, mpb, &dl->index); | |
5063 | if (disk) { | |
5064 | dl->disk = *disk; | |
5065 | /* only set index on disks that are a member of | |
5066 | * a populated contianer, i.e. one with | |
5067 | * raid_devs | |
5068 | */ | |
5069 | if (is_failed(&dl->disk)) | |
5070 | dl->index = -2; | |
5071 | else if (is_spare(&dl->disk)) | |
5072 | dl->index = -1; | |
5073 | break; | |
5074 | } | |
5075 | } | |
5076 | ||
5077 | if (i >= mpb->num_disks) { | |
5078 | struct intel_disk *idisk; | |
5079 | ||
5080 | idisk = disk_list_get(dl->serial, disk_list); | |
ecf408e9 | 5081 | if (idisk && is_spare(&idisk->disk) && |
a2b97981 DW |
5082 | !is_failed(&idisk->disk) && !is_configured(&idisk->disk)) |
5083 | dl->index = -1; | |
5084 | else { | |
5085 | dl->index = -2; | |
5086 | continue; | |
5087 | } | |
5088 | } | |
5089 | ||
5090 | dl->next = champion->disks; | |
5091 | champion->disks = dl; | |
5092 | s->disks = NULL; | |
5093 | } | |
5094 | ||
5095 | /* delete 'champion' from super_list */ | |
5096 | for (del = super_list; *del; ) { | |
5097 | if (*del == champion) { | |
5098 | *del = (*del)->next; | |
5099 | break; | |
5100 | } else | |
5101 | del = &(*del)->next; | |
5102 | } | |
5103 | champion->next = NULL; | |
5104 | ||
5105 | out: | |
5106 | while (disk_list) { | |
5107 | struct intel_disk *idisk = disk_list; | |
5108 | ||
5109 | disk_list = disk_list->next; | |
5110 | free(idisk); | |
5111 | } | |
5112 | ||
5113 | return champion; | |
5114 | } | |
5115 | ||
9587c373 LM |
5116 | static int |
5117 | get_sra_super_block(int fd, struct intel_super **super_list, char *devname, int *max, int keep_fd); | |
4dd2df09 | 5118 | static int get_super_block(struct intel_super **super_list, char *devnm, char *devname, |
9587c373 | 5119 | int major, int minor, int keep_fd); |
ec50f7b6 LM |
5120 | static int |
5121 | get_devlist_super_block(struct md_list *devlist, struct intel_super **super_list, | |
5122 | int *max, int keep_fd); | |
5123 | ||
cdddbdbc | 5124 | static int load_super_imsm_all(struct supertype *st, int fd, void **sbp, |
ec50f7b6 LM |
5125 | char *devname, struct md_list *devlist, |
5126 | int keep_fd) | |
cdddbdbc | 5127 | { |
a2b97981 DW |
5128 | struct intel_super *super_list = NULL; |
5129 | struct intel_super *super = NULL; | |
a2b97981 | 5130 | int err = 0; |
9587c373 | 5131 | int i = 0; |
dab4a513 | 5132 | |
4389ce73 | 5133 | if (is_fd_valid(fd)) |
9587c373 LM |
5134 | /* 'fd' is an opened container */ |
5135 | err = get_sra_super_block(fd, &super_list, devname, &i, keep_fd); | |
5136 | else | |
ec50f7b6 LM |
5137 | /* get super block from devlist devices */ |
5138 | err = get_devlist_super_block(devlist, &super_list, &i, keep_fd); | |
9587c373 | 5139 | if (err) |
1602d52c | 5140 | goto error; |
a2b97981 DW |
5141 | /* all mpbs enter, maybe one leaves */ |
5142 | super = imsm_thunderdome(&super_list, i); | |
5143 | if (!super) { | |
5144 | err = 1; | |
5145 | goto error; | |
cdddbdbc DW |
5146 | } |
5147 | ||
47ee5a45 DW |
5148 | if (find_missing(super) != 0) { |
5149 | free_imsm(super); | |
a2b97981 DW |
5150 | err = 2; |
5151 | goto error; | |
47ee5a45 | 5152 | } |
8e59f3d8 AK |
5153 | |
5154 | /* load migration record */ | |
2f86fda3 | 5155 | err = load_imsm_migr_rec(super); |
4c965cc9 AK |
5156 | if (err == -1) { |
5157 | /* migration is in progress, | |
5158 | * but migr_rec cannot be loaded, | |
5159 | */ | |
8e59f3d8 AK |
5160 | err = 4; |
5161 | goto error; | |
5162 | } | |
e2f41b2c AK |
5163 | |
5164 | /* Check migration compatibility */ | |
089f9d79 | 5165 | if (err == 0 && check_mpb_migr_compatibility(super) != 0) { |
e7b84f9d | 5166 | pr_err("Unsupported migration detected"); |
e2f41b2c AK |
5167 | if (devname) |
5168 | fprintf(stderr, " on %s\n", devname); | |
5169 | else | |
5170 | fprintf(stderr, " (IMSM).\n"); | |
5171 | ||
5172 | err = 5; | |
5173 | goto error; | |
5174 | } | |
5175 | ||
a2b97981 DW |
5176 | err = 0; |
5177 | ||
5178 | error: | |
5179 | while (super_list) { | |
5180 | struct intel_super *s = super_list; | |
5181 | ||
5182 | super_list = super_list->next; | |
5183 | free_imsm(s); | |
5184 | } | |
9587c373 | 5185 | |
a2b97981 DW |
5186 | if (err) |
5187 | return err; | |
f7e7067b | 5188 | |
cdddbdbc | 5189 | *sbp = super; |
4389ce73 | 5190 | if (is_fd_valid(fd)) |
4dd2df09 | 5191 | strcpy(st->container_devnm, fd2devnm(fd)); |
9587c373 | 5192 | else |
4dd2df09 | 5193 | st->container_devnm[0] = 0; |
a2b97981 | 5194 | if (err == 0 && st->ss == NULL) { |
bf5a934a | 5195 | st->ss = &super_imsm; |
cdddbdbc DW |
5196 | st->minor_version = 0; |
5197 | st->max_devs = IMSM_MAX_DEVICES; | |
5198 | } | |
cdddbdbc DW |
5199 | return 0; |
5200 | } | |
2b959fbf | 5201 | |
ec50f7b6 LM |
5202 | static int |
5203 | get_devlist_super_block(struct md_list *devlist, struct intel_super **super_list, | |
5204 | int *max, int keep_fd) | |
5205 | { | |
5206 | struct md_list *tmpdev; | |
5207 | int err = 0; | |
5208 | int i = 0; | |
9587c373 | 5209 | |
ec50f7b6 LM |
5210 | for (i = 0, tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) { |
5211 | if (tmpdev->used != 1) | |
5212 | continue; | |
5213 | if (tmpdev->container == 1) { | |
ca9de185 | 5214 | int lmax = 0; |
ec50f7b6 | 5215 | int fd = dev_open(tmpdev->devname, O_RDONLY|O_EXCL); |
4389ce73 | 5216 | if (!is_fd_valid(fd)) { |
e7b84f9d | 5217 | pr_err("cannot open device %s: %s\n", |
ec50f7b6 LM |
5218 | tmpdev->devname, strerror(errno)); |
5219 | err = 8; | |
5220 | goto error; | |
5221 | } | |
5222 | err = get_sra_super_block(fd, super_list, | |
5223 | tmpdev->devname, &lmax, | |
5224 | keep_fd); | |
5225 | i += lmax; | |
5226 | close(fd); | |
5227 | if (err) { | |
5228 | err = 7; | |
5229 | goto error; | |
5230 | } | |
5231 | } else { | |
5232 | int major = major(tmpdev->st_rdev); | |
5233 | int minor = minor(tmpdev->st_rdev); | |
5234 | err = get_super_block(super_list, | |
4dd2df09 | 5235 | NULL, |
ec50f7b6 LM |
5236 | tmpdev->devname, |
5237 | major, minor, | |
5238 | keep_fd); | |
5239 | i++; | |
5240 | if (err) { | |
5241 | err = 6; | |
5242 | goto error; | |
5243 | } | |
5244 | } | |
5245 | } | |
5246 | error: | |
5247 | *max = i; | |
5248 | return err; | |
5249 | } | |
9587c373 | 5250 | |
4dd2df09 | 5251 | static int get_super_block(struct intel_super **super_list, char *devnm, char *devname, |
9587c373 LM |
5252 | int major, int minor, int keep_fd) |
5253 | { | |
594dc1b8 | 5254 | struct intel_super *s; |
9587c373 LM |
5255 | char nm[32]; |
5256 | int dfd = -1; | |
9587c373 LM |
5257 | int err = 0; |
5258 | int retry; | |
5259 | ||
5260 | s = alloc_super(); | |
5261 | if (!s) { | |
5262 | err = 1; | |
5263 | goto error; | |
5264 | } | |
5265 | ||
5266 | sprintf(nm, "%d:%d", major, minor); | |
5267 | dfd = dev_open(nm, O_RDWR); | |
4389ce73 | 5268 | if (!is_fd_valid(dfd)) { |
9587c373 LM |
5269 | err = 2; |
5270 | goto error; | |
5271 | } | |
5272 | ||
aec01630 JS |
5273 | if (!get_dev_sector_size(dfd, NULL, &s->sector_size)) { |
5274 | err = 2; | |
5275 | goto error; | |
5276 | } | |
cb8f6859 | 5277 | find_intel_hba_capability(dfd, s, devname); |
9587c373 LM |
5278 | err = load_and_parse_mpb(dfd, s, NULL, keep_fd); |
5279 | ||
5280 | /* retry the load if we might have raced against mdmon */ | |
4dd2df09 | 5281 | if (err == 3 && devnm && mdmon_running(devnm)) |
9587c373 | 5282 | for (retry = 0; retry < 3; retry++) { |
239b3cc0 | 5283 | sleep_for(0, MSEC_TO_NSEC(3), true); |
9587c373 LM |
5284 | err = load_and_parse_mpb(dfd, s, NULL, keep_fd); |
5285 | if (err != 3) | |
5286 | break; | |
5287 | } | |
5288 | error: | |
5289 | if (!err) { | |
5290 | s->next = *super_list; | |
5291 | *super_list = s; | |
5292 | } else { | |
5293 | if (s) | |
8d67477f | 5294 | free_imsm(s); |
4389ce73 | 5295 | close_fd(&dfd); |
9587c373 | 5296 | } |
4389ce73 MT |
5297 | if (!keep_fd) |
5298 | close_fd(&dfd); | |
9587c373 LM |
5299 | return err; |
5300 | ||
5301 | } | |
5302 | ||
5303 | static int | |
5304 | get_sra_super_block(int fd, struct intel_super **super_list, char *devname, int *max, int keep_fd) | |
5305 | { | |
5306 | struct mdinfo *sra; | |
4dd2df09 | 5307 | char *devnm; |
9587c373 LM |
5308 | struct mdinfo *sd; |
5309 | int err = 0; | |
5310 | int i = 0; | |
4dd2df09 | 5311 | sra = sysfs_read(fd, NULL, GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE); |
9587c373 LM |
5312 | if (!sra) |
5313 | return 1; | |
5314 | ||
5315 | if (sra->array.major_version != -1 || | |
5316 | sra->array.minor_version != -2 || | |
5317 | strcmp(sra->text_version, "imsm") != 0) { | |
5318 | err = 1; | |
5319 | goto error; | |
5320 | } | |
5321 | /* load all mpbs */ | |
4dd2df09 | 5322 | devnm = fd2devnm(fd); |
9587c373 | 5323 | for (sd = sra->devs, i = 0; sd; sd = sd->next, i++) { |
4dd2df09 | 5324 | if (get_super_block(super_list, devnm, devname, |
9587c373 LM |
5325 | sd->disk.major, sd->disk.minor, keep_fd) != 0) { |
5326 | err = 7; | |
5327 | goto error; | |
5328 | } | |
5329 | } | |
5330 | error: | |
5331 | sysfs_free(sra); | |
5332 | *max = i; | |
5333 | return err; | |
5334 | } | |
5335 | ||
2b959fbf N |
5336 | static int load_container_imsm(struct supertype *st, int fd, char *devname) |
5337 | { | |
ec50f7b6 | 5338 | return load_super_imsm_all(st, fd, &st->sb, devname, NULL, 1); |
2b959fbf | 5339 | } |
cdddbdbc DW |
5340 | |
5341 | static int load_super_imsm(struct supertype *st, int fd, char *devname) | |
5342 | { | |
5343 | struct intel_super *super; | |
5344 | int rv; | |
8a3544f8 | 5345 | int retry; |
cdddbdbc | 5346 | |
357ac106 | 5347 | if (test_partition(fd)) |
691c6ee1 N |
5348 | /* IMSM not allowed on partitions */ |
5349 | return 1; | |
5350 | ||
37424f13 DW |
5351 | free_super_imsm(st); |
5352 | ||
49133e57 | 5353 | super = alloc_super(); |
8d67477f TM |
5354 | if (!super) |
5355 | return 1; | |
3a85bf0e MG |
5356 | |
5357 | if (!get_dev_sector_size(fd, NULL, &super->sector_size)) { | |
5358 | free_imsm(super); | |
5359 | return 1; | |
5360 | } | |
ea2bc72b LM |
5361 | /* Load hba and capabilities if they exist. |
5362 | * But do not preclude loading metadata in case capabilities or hba are | |
5363 | * non-compliant and ignore_hw_compat is set. | |
5364 | */ | |
d424212e | 5365 | rv = find_intel_hba_capability(fd, super, devname); |
f2f5c343 | 5366 | /* no orom/efi or non-intel hba of the disk */ |
089f9d79 | 5367 | if (rv != 0 && st->ignore_hw_compat == 0) { |
f2f5c343 | 5368 | if (devname) |
e7b84f9d | 5369 | pr_err("No OROM/EFI properties for %s\n", devname); |
f2f5c343 LM |
5370 | free_imsm(super); |
5371 | return 2; | |
5372 | } | |
a2b97981 | 5373 | rv = load_and_parse_mpb(fd, super, devname, 0); |
cdddbdbc | 5374 | |
8a3544f8 AP |
5375 | /* retry the load if we might have raced against mdmon */ |
5376 | if (rv == 3) { | |
f96b1302 AP |
5377 | struct mdstat_ent *mdstat = NULL; |
5378 | char *name = fd2kname(fd); | |
5379 | ||
5380 | if (name) | |
5381 | mdstat = mdstat_by_component(name); | |
8a3544f8 AP |
5382 | |
5383 | if (mdstat && mdmon_running(mdstat->devnm) && getpid() != mdmon_pid(mdstat->devnm)) { | |
5384 | for (retry = 0; retry < 3; retry++) { | |
239b3cc0 | 5385 | sleep_for(0, MSEC_TO_NSEC(3), true); |
8a3544f8 AP |
5386 | rv = load_and_parse_mpb(fd, super, devname, 0); |
5387 | if (rv != 3) | |
5388 | break; | |
5389 | } | |
5390 | } | |
5391 | ||
5392 | free_mdstat(mdstat); | |
5393 | } | |
5394 | ||
cdddbdbc DW |
5395 | if (rv) { |
5396 | if (devname) | |
7a862a02 | 5397 | pr_err("Failed to load all information sections on %s\n", devname); |
cdddbdbc DW |
5398 | free_imsm(super); |
5399 | return rv; | |
5400 | } | |
5401 | ||
5402 | st->sb = super; | |
5403 | if (st->ss == NULL) { | |
5404 | st->ss = &super_imsm; | |
5405 | st->minor_version = 0; | |
5406 | st->max_devs = IMSM_MAX_DEVICES; | |
5407 | } | |
8e59f3d8 AK |
5408 | |
5409 | /* load migration record */ | |
2f86fda3 | 5410 | if (load_imsm_migr_rec(super) == 0) { |
2e062e82 AK |
5411 | /* Check for unsupported migration features */ |
5412 | if (check_mpb_migr_compatibility(super) != 0) { | |
e7b84f9d | 5413 | pr_err("Unsupported migration detected"); |
2e062e82 AK |
5414 | if (devname) |
5415 | fprintf(stderr, " on %s\n", devname); | |
5416 | else | |
5417 | fprintf(stderr, " (IMSM).\n"); | |
5418 | return 3; | |
5419 | } | |
e2f41b2c AK |
5420 | } |
5421 | ||
cdddbdbc DW |
5422 | return 0; |
5423 | } | |
5424 | ||
ef6ffade DW |
5425 | static __u16 info_to_blocks_per_strip(mdu_array_info_t *info) |
5426 | { | |
5427 | if (info->level == 1) | |
5428 | return 128; | |
5429 | return info->chunk_size >> 9; | |
5430 | } | |
5431 | ||
5551b113 CA |
5432 | static unsigned long long info_to_blocks_per_member(mdu_array_info_t *info, |
5433 | unsigned long long size) | |
fcfd9599 | 5434 | { |
4025c288 | 5435 | if (info->level == 1) |
5551b113 | 5436 | return size * 2; |
4025c288 | 5437 | else |
5551b113 | 5438 | return (size * 2) & ~(info_to_blocks_per_strip(info) - 1); |
fcfd9599 DW |
5439 | } |
5440 | ||
4d1313e9 DW |
5441 | static void imsm_update_version_info(struct intel_super *super) |
5442 | { | |
5443 | /* update the version and attributes */ | |
5444 | struct imsm_super *mpb = super->anchor; | |
5445 | char *version; | |
5446 | struct imsm_dev *dev; | |
5447 | struct imsm_map *map; | |
5448 | int i; | |
5449 | ||
5450 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
5451 | dev = get_imsm_dev(super, i); | |
238c0a71 | 5452 | map = get_imsm_map(dev, MAP_0); |
4d1313e9 DW |
5453 | if (__le32_to_cpu(dev->size_high) > 0) |
5454 | mpb->attributes |= MPB_ATTRIB_2TB; | |
5455 | ||
5456 | /* FIXME detect when an array spans a port multiplier */ | |
5457 | #if 0 | |
5458 | mpb->attributes |= MPB_ATTRIB_PM; | |
5459 | #endif | |
5460 | ||
5461 | if (mpb->num_raid_devs > 1 || | |
5462 | mpb->attributes != MPB_ATTRIB_CHECKSUM_VERIFY) { | |
5463 | version = MPB_VERSION_ATTRIBS; | |
5464 | switch (get_imsm_raid_level(map)) { | |
5465 | case 0: mpb->attributes |= MPB_ATTRIB_RAID0; break; | |
5466 | case 1: mpb->attributes |= MPB_ATTRIB_RAID1; break; | |
5467 | case 10: mpb->attributes |= MPB_ATTRIB_RAID10; break; | |
5468 | case 5: mpb->attributes |= MPB_ATTRIB_RAID5; break; | |
5469 | } | |
5470 | } else { | |
5471 | if (map->num_members >= 5) | |
5472 | version = MPB_VERSION_5OR6_DISK_ARRAY; | |
5473 | else if (dev->status == DEV_CLONE_N_GO) | |
5474 | version = MPB_VERSION_CNG; | |
5475 | else if (get_imsm_raid_level(map) == 5) | |
5476 | version = MPB_VERSION_RAID5; | |
5477 | else if (map->num_members >= 3) | |
5478 | version = MPB_VERSION_3OR4_DISK_ARRAY; | |
5479 | else if (get_imsm_raid_level(map) == 1) | |
5480 | version = MPB_VERSION_RAID1; | |
5481 | else | |
5482 | version = MPB_VERSION_RAID0; | |
5483 | } | |
5484 | strcpy(((char *) mpb->sig) + strlen(MPB_SIGNATURE), version); | |
5485 | } | |
5486 | } | |
5487 | ||
aa534678 DW |
5488 | static int check_name(struct intel_super *super, char *name, int quiet) |
5489 | { | |
5490 | struct imsm_super *mpb = super->anchor; | |
5491 | char *reason = NULL; | |
9bd99a90 RS |
5492 | char *start = name; |
5493 | size_t len = strlen(name); | |
aa534678 DW |
5494 | int i; |
5495 | ||
9bd99a90 RS |
5496 | if (len > 0) { |
5497 | while (isspace(start[len - 1])) | |
5498 | start[--len] = 0; | |
5499 | while (*start && isspace(*start)) | |
5500 | ++start, --len; | |
5501 | memmove(name, start, len + 1); | |
5502 | } | |
5503 | ||
5504 | if (len > MAX_RAID_SERIAL_LEN) | |
aa534678 | 5505 | reason = "must be 16 characters or less"; |
9bd99a90 RS |
5506 | else if (len == 0) |
5507 | reason = "must be a non-empty string"; | |
aa534678 DW |
5508 | |
5509 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
5510 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
5511 | ||
5512 | if (strncmp((char *) dev->volume, name, MAX_RAID_SERIAL_LEN) == 0) { | |
5513 | reason = "already exists"; | |
5514 | break; | |
5515 | } | |
5516 | } | |
5517 | ||
5518 | if (reason && !quiet) | |
e7b84f9d | 5519 | pr_err("imsm volume name %s\n", reason); |
aa534678 DW |
5520 | |
5521 | return !reason; | |
5522 | } | |
5523 | ||
8b353278 | 5524 | static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info, |
5308f117 | 5525 | struct shape *s, char *name, |
83cd1e97 N |
5526 | char *homehost, int *uuid, |
5527 | long long data_offset) | |
cdddbdbc | 5528 | { |
c2c087e6 DW |
5529 | /* We are creating a volume inside a pre-existing container. |
5530 | * so st->sb is already set. | |
5531 | */ | |
5532 | struct intel_super *super = st->sb; | |
f36a9ecd | 5533 | unsigned int sector_size = super->sector_size; |
949c47a0 | 5534 | struct imsm_super *mpb = super->anchor; |
ba2de7ba | 5535 | struct intel_dev *dv; |
c2c087e6 DW |
5536 | struct imsm_dev *dev; |
5537 | struct imsm_vol *vol; | |
5538 | struct imsm_map *map; | |
5539 | int idx = mpb->num_raid_devs; | |
5540 | int i; | |
760365f9 | 5541 | int namelen; |
c2c087e6 | 5542 | unsigned long long array_blocks; |
2c092cad | 5543 | size_t size_old, size_new; |
b53bfba6 TM |
5544 | unsigned int data_disks; |
5545 | unsigned long long size_per_member; | |
cdddbdbc | 5546 | |
88c32bb1 | 5547 | if (super->orom && mpb->num_raid_devs >= super->orom->vpa) { |
7a862a02 | 5548 | pr_err("This imsm-container already has the maximum of %d volumes\n", super->orom->vpa); |
c2c087e6 DW |
5549 | return 0; |
5550 | } | |
5551 | ||
2c092cad DW |
5552 | /* ensure the mpb is large enough for the new data */ |
5553 | size_old = __le32_to_cpu(mpb->mpb_size); | |
5554 | size_new = disks_to_mpb_size(info->nr_disks); | |
5555 | if (size_new > size_old) { | |
5556 | void *mpb_new; | |
f36a9ecd | 5557 | size_t size_round = ROUND_UP(size_new, sector_size); |
2c092cad | 5558 | |
f36a9ecd | 5559 | if (posix_memalign(&mpb_new, sector_size, size_round) != 0) { |
e7b84f9d | 5560 | pr_err("could not allocate new mpb\n"); |
2c092cad DW |
5561 | return 0; |
5562 | } | |
85337573 AO |
5563 | if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE, |
5564 | MIGR_REC_BUF_SECTORS* | |
5565 | MAX_SECTOR_SIZE) != 0) { | |
1ade5cc1 | 5566 | pr_err("could not allocate migr_rec buffer\n"); |
8e59f3d8 AK |
5567 | free(super->buf); |
5568 | free(super); | |
ea944c8f | 5569 | free(mpb_new); |
8e59f3d8 AK |
5570 | return 0; |
5571 | } | |
2c092cad DW |
5572 | memcpy(mpb_new, mpb, size_old); |
5573 | free(mpb); | |
5574 | mpb = mpb_new; | |
949c47a0 | 5575 | super->anchor = mpb_new; |
2c092cad DW |
5576 | mpb->mpb_size = __cpu_to_le32(size_new); |
5577 | memset(mpb_new + size_old, 0, size_round - size_old); | |
bbab0940 | 5578 | super->len = size_round; |
2c092cad | 5579 | } |
bf5a934a | 5580 | super->current_vol = idx; |
3960e579 DW |
5581 | |
5582 | /* handle 'failed_disks' by either: | |
5583 | * a) create dummy disk entries in the table if this the first | |
5584 | * volume in the array. We add them here as this is the only | |
5585 | * opportunity to add them. add_to_super_imsm_volume() | |
5586 | * handles the non-failed disks and continues incrementing | |
5587 | * mpb->num_disks. | |
5588 | * b) validate that 'failed_disks' matches the current number | |
5589 | * of missing disks if the container is populated | |
d23fe947 | 5590 | */ |
3960e579 | 5591 | if (super->current_vol == 0) { |
d23fe947 | 5592 | mpb->num_disks = 0; |
3960e579 DW |
5593 | for (i = 0; i < info->failed_disks; i++) { |
5594 | struct imsm_disk *disk; | |
5595 | ||
5596 | mpb->num_disks++; | |
5597 | disk = __get_imsm_disk(mpb, i); | |
5598 | disk->status = CONFIGURED_DISK | FAILED_DISK; | |
5599 | disk->scsi_id = __cpu_to_le32(~(__u32)0); | |
5600 | snprintf((char *) disk->serial, MAX_RAID_SERIAL_LEN, | |
5b975129 | 5601 | "missing:%d", (__u8)i); |
3960e579 DW |
5602 | } |
5603 | find_missing(super); | |
5604 | } else { | |
5605 | int missing = 0; | |
5606 | struct dl *d; | |
5607 | ||
5608 | for (d = super->missing; d; d = d->next) | |
5609 | missing++; | |
5610 | if (info->failed_disks > missing) { | |
e7b84f9d | 5611 | pr_err("unable to add 'missing' disk to container\n"); |
3960e579 DW |
5612 | return 0; |
5613 | } | |
5614 | } | |
5a038140 | 5615 | |
aa534678 DW |
5616 | if (!check_name(super, name, 0)) |
5617 | return 0; | |
503975b9 N |
5618 | dv = xmalloc(sizeof(*dv)); |
5619 | dev = xcalloc(1, sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1)); | |
760365f9 JS |
5620 | /* |
5621 | * Explicitly allow truncating to not confuse gcc's | |
5622 | * -Werror=stringop-truncation | |
5623 | */ | |
5624 | namelen = min((int) strlen(name), MAX_RAID_SERIAL_LEN); | |
5625 | memcpy(dev->volume, name, namelen); | |
e03640bd | 5626 | array_blocks = calc_array_size(info->level, info->raid_disks, |
03bcbc65 | 5627 | info->layout, info->chunk_size, |
b53bfba6 TM |
5628 | s->size * BLOCKS_PER_KB); |
5629 | data_disks = get_data_disks(info->level, info->layout, | |
5630 | info->raid_disks); | |
5631 | array_blocks = round_size_to_mb(array_blocks, data_disks); | |
5632 | size_per_member = array_blocks / data_disks; | |
979d38be | 5633 | |
fcc2c9da | 5634 | set_imsm_dev_size(dev, array_blocks); |
1a2487c2 | 5635 | dev->status = (DEV_READ_COALESCING | DEV_WRITE_COALESCING); |
c2c087e6 DW |
5636 | vol = &dev->vol; |
5637 | vol->migr_state = 0; | |
1484e727 | 5638 | set_migr_type(dev, MIGR_INIT); |
3960e579 | 5639 | vol->dirty = !info->state; |
4036e7ee | 5640 | set_vol_curr_migr_unit(dev, 0); |
238c0a71 | 5641 | map = get_imsm_map(dev, MAP_0); |
5551b113 | 5642 | set_pba_of_lba0(map, super->create_offset); |
ef6ffade | 5643 | map->blocks_per_strip = __cpu_to_le16(info_to_blocks_per_strip(info)); |
0556e1a2 | 5644 | map->failed_disk_num = ~0; |
bf4442ab | 5645 | if (info->level > 0) |
fffaf1ff N |
5646 | map->map_state = (info->state ? IMSM_T_STATE_NORMAL |
5647 | : IMSM_T_STATE_UNINITIALIZED); | |
bf4442ab AK |
5648 | else |
5649 | map->map_state = info->failed_disks ? IMSM_T_STATE_FAILED : | |
5650 | IMSM_T_STATE_NORMAL; | |
252d23c0 | 5651 | map->ddf = 1; |
ef6ffade DW |
5652 | |
5653 | if (info->level == 1 && info->raid_disks > 2) { | |
38950822 AW |
5654 | free(dev); |
5655 | free(dv); | |
7a862a02 | 5656 | pr_err("imsm does not support more than 2 disksin a raid1 volume\n"); |
ef6ffade DW |
5657 | return 0; |
5658 | } | |
81062a36 DW |
5659 | |
5660 | map->raid_level = info->level; | |
1c275381 | 5661 | if (info->level == 10) |
c2c087e6 | 5662 | map->raid_level = 1; |
1c275381 | 5663 | set_num_domains(map); |
ef6ffade | 5664 | |
44490938 MD |
5665 | size_per_member += NUM_BLOCKS_DIRTY_STRIPE_REGION; |
5666 | set_blocks_per_member(map, info_to_blocks_per_member(info, | |
5667 | size_per_member / | |
5668 | BLOCKS_PER_KB)); | |
5669 | ||
c2c087e6 | 5670 | map->num_members = info->raid_disks; |
1c275381 | 5671 | update_num_data_stripes(map, array_blocks); |
c2c087e6 DW |
5672 | for (i = 0; i < map->num_members; i++) { |
5673 | /* initialized in add_to_super */ | |
4eb26970 | 5674 | set_imsm_ord_tbl_ent(map, i, IMSM_ORD_REBUILD); |
c2c087e6 | 5675 | } |
949c47a0 | 5676 | mpb->num_raid_devs++; |
2a24dc1b PB |
5677 | mpb->num_raid_devs_created++; |
5678 | dev->my_vol_raid_dev_num = mpb->num_raid_devs_created; | |
ba2de7ba | 5679 | |
b7580566 | 5680 | if (s->consistency_policy <= CONSISTENCY_POLICY_RESYNC) { |
c2462068 | 5681 | dev->rwh_policy = RWH_MULTIPLE_OFF; |
2432ce9b | 5682 | } else if (s->consistency_policy == CONSISTENCY_POLICY_PPL) { |
c2462068 | 5683 | dev->rwh_policy = RWH_MULTIPLE_DISTRIBUTED; |
2432ce9b AP |
5684 | } else { |
5685 | free(dev); | |
5686 | free(dv); | |
5687 | pr_err("imsm does not support consistency policy %s\n", | |
5f21d674 | 5688 | map_num_s(consistency_policies, s->consistency_policy)); |
2432ce9b AP |
5689 | return 0; |
5690 | } | |
5691 | ||
ba2de7ba DW |
5692 | dv->dev = dev; |
5693 | dv->index = super->current_vol; | |
5694 | dv->next = super->devlist; | |
5695 | super->devlist = dv; | |
c2c087e6 | 5696 | |
4d1313e9 DW |
5697 | imsm_update_version_info(super); |
5698 | ||
c2c087e6 | 5699 | return 1; |
cdddbdbc DW |
5700 | } |
5701 | ||
bf5a934a | 5702 | static int init_super_imsm(struct supertype *st, mdu_array_info_t *info, |
5308f117 | 5703 | struct shape *s, char *name, |
83cd1e97 N |
5704 | char *homehost, int *uuid, |
5705 | unsigned long long data_offset) | |
bf5a934a DW |
5706 | { |
5707 | /* This is primarily called by Create when creating a new array. | |
5708 | * We will then get add_to_super called for each component, and then | |
5709 | * write_init_super called to write it out to each device. | |
5710 | * For IMSM, Create can create on fresh devices or on a pre-existing | |
5711 | * array. | |
5712 | * To create on a pre-existing array a different method will be called. | |
5713 | * This one is just for fresh drives. | |
5714 | */ | |
5715 | struct intel_super *super; | |
5716 | struct imsm_super *mpb; | |
5717 | size_t mpb_size; | |
4d1313e9 | 5718 | char *version; |
bf5a934a | 5719 | |
83cd1e97 | 5720 | if (data_offset != INVALID_SECTORS) { |
ed503f89 | 5721 | pr_err("data-offset not supported by imsm\n"); |
83cd1e97 N |
5722 | return 0; |
5723 | } | |
5724 | ||
bf5a934a | 5725 | if (st->sb) |
5308f117 | 5726 | return init_super_imsm_volume(st, info, s, name, homehost, uuid, |
83cd1e97 | 5727 | data_offset); |
e683ca88 DW |
5728 | |
5729 | if (info) | |
5730 | mpb_size = disks_to_mpb_size(info->nr_disks); | |
5731 | else | |
f36a9ecd | 5732 | mpb_size = MAX_SECTOR_SIZE; |
bf5a934a | 5733 | |
49133e57 | 5734 | super = alloc_super(); |
f36a9ecd PB |
5735 | if (super && |
5736 | posix_memalign(&super->buf, MAX_SECTOR_SIZE, mpb_size) != 0) { | |
8d67477f | 5737 | free_imsm(super); |
e683ca88 DW |
5738 | super = NULL; |
5739 | } | |
5740 | if (!super) { | |
1ade5cc1 | 5741 | pr_err("could not allocate superblock\n"); |
bf5a934a DW |
5742 | return 0; |
5743 | } | |
de44e46f PB |
5744 | if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE, |
5745 | MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE) != 0) { | |
1ade5cc1 | 5746 | pr_err("could not allocate migr_rec buffer\n"); |
8e59f3d8 | 5747 | free(super->buf); |
8d67477f | 5748 | free_imsm(super); |
8e59f3d8 AK |
5749 | return 0; |
5750 | } | |
e683ca88 | 5751 | memset(super->buf, 0, mpb_size); |
ef649044 | 5752 | mpb = super->buf; |
e683ca88 DW |
5753 | mpb->mpb_size = __cpu_to_le32(mpb_size); |
5754 | st->sb = super; | |
5755 | ||
5756 | if (info == NULL) { | |
5757 | /* zeroing superblock */ | |
5758 | return 0; | |
5759 | } | |
bf5a934a | 5760 | |
4d1313e9 DW |
5761 | mpb->attributes = MPB_ATTRIB_CHECKSUM_VERIFY; |
5762 | ||
5763 | version = (char *) mpb->sig; | |
5764 | strcpy(version, MPB_SIGNATURE); | |
5765 | version += strlen(MPB_SIGNATURE); | |
5766 | strcpy(version, MPB_VERSION_RAID0); | |
bf5a934a | 5767 | |
bf5a934a DW |
5768 | return 1; |
5769 | } | |
5770 | ||
f2cc4f7d AO |
5771 | static int drive_validate_sector_size(struct intel_super *super, struct dl *dl) |
5772 | { | |
5773 | unsigned int member_sector_size; | |
5774 | ||
4389ce73 | 5775 | if (!is_fd_valid(dl->fd)) { |
f2cc4f7d AO |
5776 | pr_err("Invalid file descriptor for %s\n", dl->devname); |
5777 | return 0; | |
5778 | } | |
5779 | ||
5780 | if (!get_dev_sector_size(dl->fd, dl->devname, &member_sector_size)) | |
5781 | return 0; | |
5782 | if (member_sector_size != super->sector_size) | |
5783 | return 0; | |
5784 | return 1; | |
5785 | } | |
5786 | ||
f20c3968 | 5787 | static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk, |
bf5a934a DW |
5788 | int fd, char *devname) |
5789 | { | |
5790 | struct intel_super *super = st->sb; | |
d23fe947 | 5791 | struct imsm_super *mpb = super->anchor; |
3960e579 | 5792 | struct imsm_disk *_disk; |
bf5a934a DW |
5793 | struct imsm_dev *dev; |
5794 | struct imsm_map *map; | |
3960e579 | 5795 | struct dl *dl, *df; |
4eb26970 | 5796 | int slot; |
9a7df595 MT |
5797 | int autolayout = 0; |
5798 | ||
5799 | if (!is_fd_valid(fd)) | |
5800 | autolayout = 1; | |
bf5a934a | 5801 | |
949c47a0 | 5802 | dev = get_imsm_dev(super, super->current_vol); |
238c0a71 | 5803 | map = get_imsm_map(dev, MAP_0); |
bf5a934a | 5804 | |
208933a7 | 5805 | if (! (dk->state & (1<<MD_DISK_SYNC))) { |
e7b84f9d | 5806 | pr_err("%s: Cannot add spare devices to IMSM volume\n", |
208933a7 N |
5807 | devname); |
5808 | return 1; | |
5809 | } | |
5810 | ||
9a7df595 MT |
5811 | for (dl = super->disks; dl ; dl = dl->next) { |
5812 | if (autolayout) { | |
efb30e7f DW |
5813 | if (dl->raiddisk == dk->raid_disk) |
5814 | break; | |
9a7df595 MT |
5815 | } else if (dl->major == dk->major && dl->minor == dk->minor) |
5816 | break; | |
efb30e7f | 5817 | } |
d23fe947 | 5818 | |
208933a7 | 5819 | if (!dl) { |
9a7df595 MT |
5820 | if (!autolayout) |
5821 | pr_err("%s is not a member of the same container.\n", | |
5822 | devname); | |
f20c3968 | 5823 | return 1; |
208933a7 | 5824 | } |
bf5a934a | 5825 | |
9a7df595 MT |
5826 | if (!autolayout && super->current_vol > 0) { |
5827 | int _slot = get_disk_slot_in_dev(super, 0, dl->index); | |
5828 | ||
5829 | if (_slot != dk->raid_disk) { | |
5830 | pr_err("Member %s is in %d slot for the first volume, but is in %d slot for a new volume.\n", | |
5831 | dl->devname, _slot, dk->raid_disk); | |
5832 | pr_err("Raid members are in different order than for the first volume, aborting.\n"); | |
5833 | return 1; | |
5834 | } | |
5835 | } | |
5836 | ||
59632db9 MZ |
5837 | if (mpb->num_disks == 0) |
5838 | if (!get_dev_sector_size(dl->fd, dl->devname, | |
5839 | &super->sector_size)) | |
5840 | return 1; | |
5841 | ||
f2cc4f7d AO |
5842 | if (!drive_validate_sector_size(super, dl)) { |
5843 | pr_err("Combining drives of different sector size in one volume is not allowed\n"); | |
5844 | return 1; | |
5845 | } | |
5846 | ||
d23fe947 DW |
5847 | /* add a pristine spare to the metadata */ |
5848 | if (dl->index < 0) { | |
5849 | dl->index = super->anchor->num_disks; | |
5850 | super->anchor->num_disks++; | |
5851 | } | |
4eb26970 DW |
5852 | /* Check the device has not already been added */ |
5853 | slot = get_imsm_disk_slot(map, dl->index); | |
5854 | if (slot >= 0 && | |
238c0a71 | 5855 | (get_imsm_ord_tbl_ent(dev, slot, MAP_X) & IMSM_ORD_REBUILD) == 0) { |
e7b84f9d | 5856 | pr_err("%s has been included in this array twice\n", |
4eb26970 DW |
5857 | devname); |
5858 | return 1; | |
5859 | } | |
656b6b5a | 5860 | set_imsm_ord_tbl_ent(map, dk->raid_disk, dl->index); |
ee5aad5a | 5861 | dl->disk.status = CONFIGURED_DISK; |
d23fe947 | 5862 | |
3960e579 DW |
5863 | /* update size of 'missing' disks to be at least as large as the |
5864 | * largest acitve member (we only have dummy missing disks when | |
5865 | * creating the first volume) | |
5866 | */ | |
5867 | if (super->current_vol == 0) { | |
5868 | for (df = super->missing; df; df = df->next) { | |
5551b113 CA |
5869 | if (total_blocks(&dl->disk) > total_blocks(&df->disk)) |
5870 | set_total_blocks(&df->disk, total_blocks(&dl->disk)); | |
3960e579 DW |
5871 | _disk = __get_imsm_disk(mpb, df->index); |
5872 | *_disk = df->disk; | |
5873 | } | |
5874 | } | |
5875 | ||
5876 | /* refresh unset/failed slots to point to valid 'missing' entries */ | |
5877 | for (df = super->missing; df; df = df->next) | |
5878 | for (slot = 0; slot < mpb->num_disks; slot++) { | |
238c0a71 | 5879 | __u32 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X); |
3960e579 DW |
5880 | |
5881 | if ((ord & IMSM_ORD_REBUILD) == 0) | |
5882 | continue; | |
5883 | set_imsm_ord_tbl_ent(map, slot, df->index | IMSM_ORD_REBUILD); | |
1ace8403 | 5884 | if (is_gen_migration(dev)) { |
238c0a71 AK |
5885 | struct imsm_map *map2 = get_imsm_map(dev, |
5886 | MAP_1); | |
0a108d63 | 5887 | int slot2 = get_imsm_disk_slot(map2, df->index); |
089f9d79 | 5888 | if (slot2 < map2->num_members && slot2 >= 0) { |
1ace8403 | 5889 | __u32 ord2 = get_imsm_ord_tbl_ent(dev, |
238c0a71 AK |
5890 | slot2, |
5891 | MAP_1); | |
1ace8403 AK |
5892 | if ((unsigned)df->index == |
5893 | ord_to_idx(ord2)) | |
5894 | set_imsm_ord_tbl_ent(map2, | |
0a108d63 | 5895 | slot2, |
1ace8403 AK |
5896 | df->index | |
5897 | IMSM_ORD_REBUILD); | |
5898 | } | |
5899 | } | |
3960e579 DW |
5900 | dprintf("set slot:%d to missing disk:%d\n", slot, df->index); |
5901 | break; | |
5902 | } | |
5903 | ||
d23fe947 DW |
5904 | /* if we are creating the first raid device update the family number */ |
5905 | if (super->current_vol == 0) { | |
5906 | __u32 sum; | |
5907 | struct imsm_dev *_dev = __get_imsm_dev(mpb, 0); | |
d23fe947 | 5908 | |
3960e579 | 5909 | _disk = __get_imsm_disk(mpb, dl->index); |
756a15f3 | 5910 | if (!_disk) { |
e7b84f9d | 5911 | pr_err("BUG mpb setup error\n"); |
791b666a AW |
5912 | return 1; |
5913 | } | |
d23fe947 DW |
5914 | *_dev = *dev; |
5915 | *_disk = dl->disk; | |
148acb7b DW |
5916 | sum = random32(); |
5917 | sum += __gen_imsm_checksum(mpb); | |
d23fe947 | 5918 | mpb->family_num = __cpu_to_le32(sum); |
148acb7b | 5919 | mpb->orig_family_num = mpb->family_num; |
e48aed3c | 5920 | mpb->creation_time = __cpu_to_le64((__u64)time(NULL)); |
d23fe947 | 5921 | } |
ca0748fa | 5922 | super->current_disk = dl; |
f20c3968 | 5923 | return 0; |
bf5a934a DW |
5924 | } |
5925 | ||
a8619d23 AK |
5926 | /* mark_spare() |
5927 | * Function marks disk as spare and restores disk serial | |
5928 | * in case it was previously marked as failed by takeover operation | |
5929 | * reruns: | |
5930 | * -1 : critical error | |
5931 | * 0 : disk is marked as spare but serial is not set | |
5932 | * 1 : success | |
5933 | */ | |
5934 | int mark_spare(struct dl *disk) | |
5935 | { | |
5936 | __u8 serial[MAX_RAID_SERIAL_LEN]; | |
5937 | int ret_val = -1; | |
5938 | ||
5939 | if (!disk) | |
5940 | return ret_val; | |
5941 | ||
5942 | ret_val = 0; | |
6da53c0e | 5943 | if (!imsm_read_serial(disk->fd, NULL, serial, MAX_RAID_SERIAL_LEN)) { |
a8619d23 AK |
5944 | /* Restore disk serial number, because takeover marks disk |
5945 | * as failed and adds to serial ':0' before it becomes | |
5946 | * a spare disk. | |
5947 | */ | |
5948 | serialcpy(disk->serial, serial); | |
5949 | serialcpy(disk->disk.serial, serial); | |
5950 | ret_val = 1; | |
5951 | } | |
5952 | disk->disk.status = SPARE_DISK; | |
5953 | disk->index = -1; | |
5954 | ||
5955 | return ret_val; | |
5956 | } | |
88654014 | 5957 | |
12724c01 TM |
5958 | |
5959 | static int write_super_imsm_spare(struct intel_super *super, struct dl *d); | |
5960 | ||
f20c3968 | 5961 | static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk, |
72ca9bcf N |
5962 | int fd, char *devname, |
5963 | unsigned long long data_offset) | |
cdddbdbc | 5964 | { |
c2c087e6 | 5965 | struct intel_super *super = st->sb; |
c2c087e6 DW |
5966 | struct dl *dd; |
5967 | unsigned long long size; | |
fa7bb6f8 | 5968 | unsigned int member_sector_size; |
f2f27e63 | 5969 | __u32 id; |
c2c087e6 DW |
5970 | int rv; |
5971 | struct stat stb; | |
5972 | ||
88654014 LM |
5973 | /* If we are on an RAID enabled platform check that the disk is |
5974 | * attached to the raid controller. | |
5975 | * We do not need to test disks attachment for container based additions, | |
5976 | * they shall be already tested when container was created/assembled. | |
88c32bb1 | 5977 | */ |
d424212e | 5978 | rv = find_intel_hba_capability(fd, super, devname); |
f2f5c343 | 5979 | /* no orom/efi or non-intel hba of the disk */ |
f0f5a016 LM |
5980 | if (rv != 0) { |
5981 | dprintf("capability: %p fd: %d ret: %d\n", | |
5982 | super->orom, fd, rv); | |
5983 | return 1; | |
88c32bb1 DW |
5984 | } |
5985 | ||
f20c3968 DW |
5986 | if (super->current_vol >= 0) |
5987 | return add_to_super_imsm_volume(st, dk, fd, devname); | |
bf5a934a | 5988 | |
c2c087e6 | 5989 | fstat(fd, &stb); |
503975b9 | 5990 | dd = xcalloc(sizeof(*dd), 1); |
c2c087e6 DW |
5991 | dd->major = major(stb.st_rdev); |
5992 | dd->minor = minor(stb.st_rdev); | |
503975b9 | 5993 | dd->devname = devname ? xstrdup(devname) : NULL; |
c2c087e6 | 5994 | dd->fd = fd; |
689c9bf3 | 5995 | dd->e = NULL; |
1a64be56 | 5996 | dd->action = DISK_ADD; |
6da53c0e | 5997 | rv = imsm_read_serial(fd, devname, dd->serial, MAX_RAID_SERIAL_LEN); |
32ba9157 | 5998 | if (rv) { |
e7b84f9d | 5999 | pr_err("failed to retrieve scsi serial, aborting\n"); |
3a85bf0e | 6000 | __free_imsm_disk(dd, 0); |
0030e8d6 | 6001 | abort(); |
c2c087e6 | 6002 | } |
7c798f87 | 6003 | |
20bee0f8 PB |
6004 | if (super->hba && ((super->hba->type == SYS_DEV_NVME) || |
6005 | (super->hba->type == SYS_DEV_VMD))) { | |
6006 | int i; | |
7c798f87 MT |
6007 | char cntrl_path[PATH_MAX]; |
6008 | char *cntrl_name; | |
6009 | char pci_dev_path[PATH_MAX]; | |
20bee0f8 | 6010 | |
7c798f87 MT |
6011 | if (!diskfd_to_devpath(fd, 2, pci_dev_path) || |
6012 | !diskfd_to_devpath(fd, 1, cntrl_path)) { | |
8662f92d | 6013 | pr_err("failed to get dev paths, aborting\n"); |
3a85bf0e | 6014 | __free_imsm_disk(dd, 0); |
a8f3cfd5 MT |
6015 | return 1; |
6016 | } | |
6017 | ||
7c798f87 MT |
6018 | cntrl_name = basename(cntrl_path); |
6019 | if (is_multipath_nvme(fd)) | |
6020 | pr_err("%s controller supports Multi-Path I/O, Intel (R) VROC does not support multipathing\n", | |
6021 | cntrl_name); | |
6022 | ||
6023 | if (devpath_to_vendor(pci_dev_path) == 0x8086) { | |
20bee0f8 PB |
6024 | /* |
6025 | * If Intel's NVMe drive has serial ended with | |
6026 | * "-A","-B","-1" or "-2" it means that this is "x8" | |
6027 | * device (double drive on single PCIe card). | |
6028 | * User should be warned about potential data loss. | |
6029 | */ | |
6030 | for (i = MAX_RAID_SERIAL_LEN-1; i > 0; i--) { | |
6031 | /* Skip empty character at the end */ | |
6032 | if (dd->serial[i] == 0) | |
6033 | continue; | |
6034 | ||
6035 | if (((dd->serial[i] == 'A') || | |
6036 | (dd->serial[i] == 'B') || | |
6037 | (dd->serial[i] == '1') || | |
6038 | (dd->serial[i] == '2')) && | |
6039 | (dd->serial[i-1] == '-')) | |
6040 | pr_err("\tThe action you are about to take may put your data at risk.\n" | |
6041 | "\tPlease note that x8 devices may consist of two separate x4 devices " | |
6042 | "located on a single PCIe port.\n" | |
6043 | "\tRAID 0 is the only supported configuration for this type of x8 device.\n"); | |
6044 | break; | |
6045 | } | |
32716c51 PB |
6046 | } else if (super->hba->type == SYS_DEV_VMD && super->orom && |
6047 | !imsm_orom_has_tpv_support(super->orom)) { | |
6048 | pr_err("\tPlatform configuration does not support non-Intel NVMe drives.\n" | |
8b751247 | 6049 | "\tPlease refer to Intel(R) RSTe/VROC user guide.\n"); |
3a85bf0e | 6050 | __free_imsm_disk(dd, 0); |
32716c51 | 6051 | return 1; |
20bee0f8 PB |
6052 | } |
6053 | } | |
c2c087e6 | 6054 | |
c2c087e6 | 6055 | get_dev_size(fd, NULL, &size); |
3a85bf0e MG |
6056 | if (!get_dev_sector_size(fd, NULL, &member_sector_size)) { |
6057 | __free_imsm_disk(dd, 0); | |
aec01630 | 6058 | return 1; |
3a85bf0e | 6059 | } |
fa7bb6f8 PB |
6060 | |
6061 | if (super->sector_size == 0) { | |
6062 | /* this a first device, so sector_size is not set yet */ | |
6063 | super->sector_size = member_sector_size; | |
fa7bb6f8 PB |
6064 | } |
6065 | ||
71e5411e | 6066 | /* clear migr_rec when adding disk to container */ |
85337573 AO |
6067 | memset(super->migr_rec_buf, 0, MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE); |
6068 | if (lseek64(fd, size - MIGR_REC_SECTOR_POSITION*member_sector_size, | |
de44e46f | 6069 | SEEK_SET) >= 0) { |
466070ad | 6070 | if ((unsigned int)write(fd, super->migr_rec_buf, |
85337573 AO |
6071 | MIGR_REC_BUF_SECTORS*member_sector_size) != |
6072 | MIGR_REC_BUF_SECTORS*member_sector_size) | |
71e5411e PB |
6073 | perror("Write migr_rec failed"); |
6074 | } | |
6075 | ||
c2c087e6 | 6076 | size /= 512; |
1f24f035 | 6077 | serialcpy(dd->disk.serial, dd->serial); |
5551b113 CA |
6078 | set_total_blocks(&dd->disk, size); |
6079 | if (__le32_to_cpu(dd->disk.total_blocks_hi) > 0) { | |
6080 | struct imsm_super *mpb = super->anchor; | |
6081 | mpb->attributes |= MPB_ATTRIB_2TB_DISK; | |
6082 | } | |
a8619d23 | 6083 | mark_spare(dd); |
c2c087e6 | 6084 | if (sysfs_disk_to_scsi_id(fd, &id) == 0) |
b9f594fe | 6085 | dd->disk.scsi_id = __cpu_to_le32(id); |
c2c087e6 | 6086 | else |
b9f594fe | 6087 | dd->disk.scsi_id = __cpu_to_le32(0); |
43dad3d6 DW |
6088 | |
6089 | if (st->update_tail) { | |
1a64be56 LM |
6090 | dd->next = super->disk_mgmt_list; |
6091 | super->disk_mgmt_list = dd; | |
43dad3d6 | 6092 | } else { |
12724c01 TM |
6093 | /* this is called outside of mdmon |
6094 | * write initial spare metadata | |
6095 | * mdmon will overwrite it. | |
6096 | */ | |
43dad3d6 DW |
6097 | dd->next = super->disks; |
6098 | super->disks = dd; | |
12724c01 | 6099 | write_super_imsm_spare(super, dd); |
43dad3d6 | 6100 | } |
f20c3968 DW |
6101 | |
6102 | return 0; | |
cdddbdbc DW |
6103 | } |
6104 | ||
1a64be56 LM |
6105 | static int remove_from_super_imsm(struct supertype *st, mdu_disk_info_t *dk) |
6106 | { | |
6107 | struct intel_super *super = st->sb; | |
6108 | struct dl *dd; | |
6109 | ||
6110 | /* remove from super works only in mdmon - for communication | |
6111 | * manager - monitor. Check if communication memory buffer | |
6112 | * is prepared. | |
6113 | */ | |
6114 | if (!st->update_tail) { | |
1ade5cc1 | 6115 | pr_err("shall be used in mdmon context only\n"); |
1a64be56 LM |
6116 | return 1; |
6117 | } | |
503975b9 | 6118 | dd = xcalloc(1, sizeof(*dd)); |
1a64be56 LM |
6119 | dd->major = dk->major; |
6120 | dd->minor = dk->minor; | |
1a64be56 | 6121 | dd->fd = -1; |
a8619d23 | 6122 | mark_spare(dd); |
1a64be56 LM |
6123 | dd->action = DISK_REMOVE; |
6124 | ||
6125 | dd->next = super->disk_mgmt_list; | |
6126 | super->disk_mgmt_list = dd; | |
6127 | ||
1a64be56 LM |
6128 | return 0; |
6129 | } | |
6130 | ||
f796af5d DW |
6131 | static int store_imsm_mpb(int fd, struct imsm_super *mpb); |
6132 | ||
6133 | static union { | |
f36a9ecd | 6134 | char buf[MAX_SECTOR_SIZE]; |
f796af5d | 6135 | struct imsm_super anchor; |
f36a9ecd | 6136 | } spare_record __attribute__ ((aligned(MAX_SECTOR_SIZE))); |
c2c087e6 | 6137 | |
12724c01 TM |
6138 | |
6139 | static int write_super_imsm_spare(struct intel_super *super, struct dl *d) | |
d23fe947 | 6140 | { |
d23fe947 | 6141 | struct imsm_super *mpb = super->anchor; |
f796af5d | 6142 | struct imsm_super *spare = &spare_record.anchor; |
d23fe947 | 6143 | __u32 sum; |
12724c01 TM |
6144 | |
6145 | if (d->index != -1) | |
6146 | return 1; | |
d23fe947 | 6147 | |
68641cdb JS |
6148 | spare->mpb_size = __cpu_to_le32(sizeof(struct imsm_super)); |
6149 | spare->generation_num = __cpu_to_le32(1UL); | |
f796af5d | 6150 | spare->attributes = MPB_ATTRIB_CHECKSUM_VERIFY; |
68641cdb JS |
6151 | spare->num_disks = 1; |
6152 | spare->num_raid_devs = 0; | |
6153 | spare->cache_size = mpb->cache_size; | |
6154 | spare->pwr_cycle_count = __cpu_to_le32(1); | |
f796af5d DW |
6155 | |
6156 | snprintf((char *) spare->sig, MAX_SIGNATURE_LENGTH, | |
6157 | MPB_SIGNATURE MPB_VERSION_RAID0); | |
d23fe947 | 6158 | |
12724c01 TM |
6159 | spare->disk[0] = d->disk; |
6160 | if (__le32_to_cpu(d->disk.total_blocks_hi) > 0) | |
6161 | spare->attributes |= MPB_ATTRIB_2TB_DISK; | |
6162 | ||
6163 | if (super->sector_size == 4096) | |
6164 | convert_to_4k_imsm_disk(&spare->disk[0]); | |
d23fe947 | 6165 | |
12724c01 TM |
6166 | sum = __gen_imsm_checksum(spare); |
6167 | spare->family_num = __cpu_to_le32(sum); | |
6168 | spare->orig_family_num = 0; | |
6169 | sum = __gen_imsm_checksum(spare); | |
6170 | spare->check_sum = __cpu_to_le32(sum); | |
027c374f | 6171 | |
12724c01 TM |
6172 | if (store_imsm_mpb(d->fd, spare)) { |
6173 | pr_err("failed for device %d:%d %s\n", | |
6174 | d->major, d->minor, strerror(errno)); | |
6175 | return 1; | |
6176 | } | |
6177 | ||
6178 | return 0; | |
6179 | } | |
6180 | /* spare records have their own family number and do not have any defined raid | |
6181 | * devices | |
6182 | */ | |
6183 | static int write_super_imsm_spares(struct intel_super *super, int doclose) | |
6184 | { | |
6185 | struct dl *d; | |
f36a9ecd | 6186 | |
12724c01 TM |
6187 | for (d = super->disks; d; d = d->next) { |
6188 | if (d->index != -1) | |
6189 | continue; | |
d23fe947 | 6190 | |
12724c01 | 6191 | if (write_super_imsm_spare(super, d)) |
e74255d9 | 6192 | return 1; |
12724c01 | 6193 | |
4389ce73 MT |
6194 | if (doclose) |
6195 | close_fd(&d->fd); | |
d23fe947 DW |
6196 | } |
6197 | ||
e74255d9 | 6198 | return 0; |
d23fe947 DW |
6199 | } |
6200 | ||
36988a3d | 6201 | static int write_super_imsm(struct supertype *st, int doclose) |
cdddbdbc | 6202 | { |
36988a3d | 6203 | struct intel_super *super = st->sb; |
f36a9ecd | 6204 | unsigned int sector_size = super->sector_size; |
949c47a0 | 6205 | struct imsm_super *mpb = super->anchor; |
c2c087e6 DW |
6206 | struct dl *d; |
6207 | __u32 generation; | |
6208 | __u32 sum; | |
d23fe947 | 6209 | int spares = 0; |
949c47a0 | 6210 | int i; |
a48ac0a8 | 6211 | __u32 mpb_size = sizeof(struct imsm_super) - sizeof(struct imsm_disk); |
36988a3d | 6212 | int num_disks = 0; |
146c6260 | 6213 | int clear_migration_record = 1; |
bbab0940 | 6214 | __u32 bbm_log_size; |
cdddbdbc | 6215 | |
c2c087e6 DW |
6216 | /* 'generation' is incremented everytime the metadata is written */ |
6217 | generation = __le32_to_cpu(mpb->generation_num); | |
6218 | generation++; | |
6219 | mpb->generation_num = __cpu_to_le32(generation); | |
6220 | ||
148acb7b DW |
6221 | /* fix up cases where previous mdadm releases failed to set |
6222 | * orig_family_num | |
6223 | */ | |
6224 | if (mpb->orig_family_num == 0) | |
6225 | mpb->orig_family_num = mpb->family_num; | |
6226 | ||
d23fe947 | 6227 | for (d = super->disks; d; d = d->next) { |
8796fdc4 | 6228 | if (d->index == -1) |
d23fe947 | 6229 | spares++; |
36988a3d | 6230 | else { |
d23fe947 | 6231 | mpb->disk[d->index] = d->disk; |
36988a3d AK |
6232 | num_disks++; |
6233 | } | |
d23fe947 | 6234 | } |
36988a3d | 6235 | for (d = super->missing; d; d = d->next) { |
47ee5a45 | 6236 | mpb->disk[d->index] = d->disk; |
36988a3d AK |
6237 | num_disks++; |
6238 | } | |
6239 | mpb->num_disks = num_disks; | |
6240 | mpb_size += sizeof(struct imsm_disk) * mpb->num_disks; | |
b9f594fe | 6241 | |
949c47a0 DW |
6242 | for (i = 0; i < mpb->num_raid_devs; i++) { |
6243 | struct imsm_dev *dev = __get_imsm_dev(mpb, i); | |
36988a3d | 6244 | struct imsm_dev *dev2 = get_imsm_dev(super, i); |
756a15f3 MG |
6245 | |
6246 | imsm_copy_dev(dev, dev2); | |
6247 | mpb_size += sizeof_imsm_dev(dev, 0); | |
6248 | ||
146c6260 AK |
6249 | if (is_gen_migration(dev2)) |
6250 | clear_migration_record = 0; | |
949c47a0 | 6251 | } |
bbab0940 TM |
6252 | |
6253 | bbm_log_size = get_imsm_bbm_log_size(super->bbm_log); | |
6254 | ||
6255 | if (bbm_log_size) { | |
6256 | memcpy((void *)mpb + mpb_size, super->bbm_log, bbm_log_size); | |
6257 | mpb->attributes |= MPB_ATTRIB_BBM; | |
6258 | } else | |
6259 | mpb->attributes &= ~MPB_ATTRIB_BBM; | |
6260 | ||
6261 | super->anchor->bbm_log_size = __cpu_to_le32(bbm_log_size); | |
6262 | mpb_size += bbm_log_size; | |
a48ac0a8 | 6263 | mpb->mpb_size = __cpu_to_le32(mpb_size); |
949c47a0 | 6264 | |
bbab0940 TM |
6265 | #ifdef DEBUG |
6266 | assert(super->len == 0 || mpb_size <= super->len); | |
6267 | #endif | |
6268 | ||
c2c087e6 | 6269 | /* recalculate checksum */ |
949c47a0 | 6270 | sum = __gen_imsm_checksum(mpb); |
c2c087e6 DW |
6271 | mpb->check_sum = __cpu_to_le32(sum); |
6272 | ||
51d83f5d AK |
6273 | if (super->clean_migration_record_by_mdmon) { |
6274 | clear_migration_record = 1; | |
6275 | super->clean_migration_record_by_mdmon = 0; | |
6276 | } | |
146c6260 | 6277 | if (clear_migration_record) |
de44e46f | 6278 | memset(super->migr_rec_buf, 0, |
85337573 | 6279 | MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE); |
146c6260 | 6280 | |
f36a9ecd PB |
6281 | if (sector_size == 4096) |
6282 | convert_to_4k(super); | |
6283 | ||
d23fe947 | 6284 | /* write the mpb for disks that compose raid devices */ |
c2c087e6 | 6285 | for (d = super->disks; d ; d = d->next) { |
86c54047 | 6286 | if (d->index < 0 || is_failed(&d->disk)) |
d23fe947 | 6287 | continue; |
30602f53 | 6288 | |
146c6260 AK |
6289 | if (clear_migration_record) { |
6290 | unsigned long long dsize; | |
6291 | ||
6292 | get_dev_size(d->fd, NULL, &dsize); | |
de44e46f PB |
6293 | if (lseek64(d->fd, dsize - sector_size, |
6294 | SEEK_SET) >= 0) { | |
466070ad PB |
6295 | if ((unsigned int)write(d->fd, |
6296 | super->migr_rec_buf, | |
de44e46f PB |
6297 | MIGR_REC_BUF_SECTORS*sector_size) != |
6298 | MIGR_REC_BUF_SECTORS*sector_size) | |
9e2d750d | 6299 | perror("Write migr_rec failed"); |
146c6260 AK |
6300 | } |
6301 | } | |
51d83f5d AK |
6302 | |
6303 | if (store_imsm_mpb(d->fd, mpb)) | |
6304 | fprintf(stderr, | |
1ade5cc1 N |
6305 | "failed for device %d:%d (fd: %d)%s\n", |
6306 | d->major, d->minor, | |
51d83f5d AK |
6307 | d->fd, strerror(errno)); |
6308 | ||
4389ce73 MT |
6309 | if (doclose) |
6310 | close_fd(&d->fd); | |
c2c087e6 DW |
6311 | } |
6312 | ||
d23fe947 DW |
6313 | if (spares) |
6314 | return write_super_imsm_spares(super, doclose); | |
6315 | ||
e74255d9 | 6316 | return 0; |
c2c087e6 DW |
6317 | } |
6318 | ||
9b1fb677 | 6319 | static int create_array(struct supertype *st, int dev_idx) |
43dad3d6 DW |
6320 | { |
6321 | size_t len; | |
6322 | struct imsm_update_create_array *u; | |
6323 | struct intel_super *super = st->sb; | |
9b1fb677 | 6324 | struct imsm_dev *dev = get_imsm_dev(super, dev_idx); |
238c0a71 | 6325 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
54c2c1ea DW |
6326 | struct disk_info *inf; |
6327 | struct imsm_disk *disk; | |
6328 | int i; | |
43dad3d6 | 6329 | |
54c2c1ea DW |
6330 | len = sizeof(*u) - sizeof(*dev) + sizeof_imsm_dev(dev, 0) + |
6331 | sizeof(*inf) * map->num_members; | |
503975b9 | 6332 | u = xmalloc(len); |
43dad3d6 | 6333 | u->type = update_create_array; |
9b1fb677 | 6334 | u->dev_idx = dev_idx; |
43dad3d6 | 6335 | imsm_copy_dev(&u->dev, dev); |
54c2c1ea DW |
6336 | inf = get_disk_info(u); |
6337 | for (i = 0; i < map->num_members; i++) { | |
238c0a71 | 6338 | int idx = get_imsm_disk_idx(dev, i, MAP_X); |
9b1fb677 | 6339 | |
54c2c1ea | 6340 | disk = get_imsm_disk(super, idx); |
1ca5c8e0 N |
6341 | if (!disk) |
6342 | disk = get_imsm_missing(super, idx); | |
54c2c1ea DW |
6343 | serialcpy(inf[i].serial, disk->serial); |
6344 | } | |
43dad3d6 DW |
6345 | append_metadata_update(st, u, len); |
6346 | ||
6347 | return 0; | |
6348 | } | |
6349 | ||
1a64be56 | 6350 | static int mgmt_disk(struct supertype *st) |
43dad3d6 DW |
6351 | { |
6352 | struct intel_super *super = st->sb; | |
6353 | size_t len; | |
1a64be56 | 6354 | struct imsm_update_add_remove_disk *u; |
43dad3d6 | 6355 | |
1a64be56 | 6356 | if (!super->disk_mgmt_list) |
43dad3d6 DW |
6357 | return 0; |
6358 | ||
6359 | len = sizeof(*u); | |
503975b9 | 6360 | u = xmalloc(len); |
1a64be56 | 6361 | u->type = update_add_remove_disk; |
43dad3d6 DW |
6362 | append_metadata_update(st, u, len); |
6363 | ||
6364 | return 0; | |
6365 | } | |
2432ce9b AP |
6366 | |
6367 | __u32 crc32c_le(__u32 crc, unsigned char const *p, size_t len); | |
6368 | ||
e397cefe AP |
6369 | static int write_ppl_header(unsigned long long ppl_sector, int fd, void *buf) |
6370 | { | |
6371 | struct ppl_header *ppl_hdr = buf; | |
6372 | int ret; | |
6373 | ||
6374 | ppl_hdr->checksum = __cpu_to_le32(~crc32c_le(~0, buf, PPL_HEADER_SIZE)); | |
6375 | ||
6376 | if (lseek64(fd, ppl_sector * 512, SEEK_SET) < 0) { | |
6377 | ret = -errno; | |
6378 | perror("Failed to seek to PPL header location"); | |
6379 | return ret; | |
6380 | } | |
6381 | ||
6382 | if (write(fd, buf, PPL_HEADER_SIZE) != PPL_HEADER_SIZE) { | |
6383 | ret = -errno; | |
6384 | perror("Write PPL header failed"); | |
6385 | return ret; | |
6386 | } | |
6387 | ||
6388 | fsync(fd); | |
6389 | ||
6390 | return 0; | |
6391 | } | |
6392 | ||
2432ce9b AP |
6393 | static int write_init_ppl_imsm(struct supertype *st, struct mdinfo *info, int fd) |
6394 | { | |
6395 | struct intel_super *super = st->sb; | |
6396 | void *buf; | |
6397 | struct ppl_header *ppl_hdr; | |
6398 | int ret; | |
6399 | ||
b2514242 PB |
6400 | /* first clear entire ppl space */ |
6401 | ret = zero_disk_range(fd, info->ppl_sector, info->ppl_size); | |
6402 | if (ret) | |
6403 | return ret; | |
6404 | ||
6405 | ret = posix_memalign(&buf, MAX_SECTOR_SIZE, PPL_HEADER_SIZE); | |
2432ce9b AP |
6406 | if (ret) { |
6407 | pr_err("Failed to allocate PPL header buffer\n"); | |
e397cefe | 6408 | return -ret; |
2432ce9b AP |
6409 | } |
6410 | ||
6411 | memset(buf, 0, PPL_HEADER_SIZE); | |
6412 | ppl_hdr = buf; | |
6413 | memset(ppl_hdr->reserved, 0xff, PPL_HDR_RESERVED); | |
6414 | ppl_hdr->signature = __cpu_to_le32(super->anchor->orig_family_num); | |
b23d0750 AP |
6415 | |
6416 | if (info->mismatch_cnt) { | |
6417 | /* | |
6418 | * We are overwriting an invalid ppl. Make one entry with wrong | |
6419 | * checksum to prevent the kernel from skipping resync. | |
6420 | */ | |
6421 | ppl_hdr->entries_count = __cpu_to_le32(1); | |
6422 | ppl_hdr->entries[0].checksum = ~0; | |
6423 | } | |
6424 | ||
e397cefe | 6425 | ret = write_ppl_header(info->ppl_sector, fd, buf); |
2432ce9b AP |
6426 | |
6427 | free(buf); | |
6428 | return ret; | |
6429 | } | |
6430 | ||
e397cefe AP |
6431 | static int is_rebuilding(struct imsm_dev *dev); |
6432 | ||
2432ce9b AP |
6433 | static int validate_ppl_imsm(struct supertype *st, struct mdinfo *info, |
6434 | struct mdinfo *disk) | |
6435 | { | |
6436 | struct intel_super *super = st->sb; | |
6437 | struct dl *d; | |
e397cefe | 6438 | void *buf_orig, *buf, *buf_prev = NULL; |
2432ce9b | 6439 | int ret = 0; |
e397cefe | 6440 | struct ppl_header *ppl_hdr = NULL; |
2432ce9b AP |
6441 | __u32 crc; |
6442 | struct imsm_dev *dev; | |
2432ce9b | 6443 | __u32 idx; |
44b6b876 PB |
6444 | unsigned int i; |
6445 | unsigned long long ppl_offset = 0; | |
6446 | unsigned long long prev_gen_num = 0; | |
2432ce9b AP |
6447 | |
6448 | if (disk->disk.raid_disk < 0) | |
6449 | return 0; | |
6450 | ||
2432ce9b | 6451 | dev = get_imsm_dev(super, info->container_member); |
2fc0fc63 | 6452 | idx = get_imsm_disk_idx(dev, disk->disk.raid_disk, MAP_0); |
2432ce9b AP |
6453 | d = get_imsm_dl_disk(super, idx); |
6454 | ||
6455 | if (!d || d->index < 0 || is_failed(&d->disk)) | |
e397cefe AP |
6456 | return 0; |
6457 | ||
6458 | if (posix_memalign(&buf_orig, MAX_SECTOR_SIZE, PPL_HEADER_SIZE * 2)) { | |
6459 | pr_err("Failed to allocate PPL header buffer\n"); | |
6460 | return -1; | |
6461 | } | |
6462 | buf = buf_orig; | |
2432ce9b | 6463 | |
44b6b876 PB |
6464 | ret = 1; |
6465 | while (ppl_offset < MULTIPLE_PPL_AREA_SIZE_IMSM) { | |
e397cefe AP |
6466 | void *tmp; |
6467 | ||
44b6b876 | 6468 | dprintf("Checking potential PPL at offset: %llu\n", ppl_offset); |
2432ce9b | 6469 | |
44b6b876 PB |
6470 | if (lseek64(d->fd, info->ppl_sector * 512 + ppl_offset, |
6471 | SEEK_SET) < 0) { | |
6472 | perror("Failed to seek to PPL header location"); | |
6473 | ret = -1; | |
e397cefe | 6474 | break; |
44b6b876 | 6475 | } |
2432ce9b | 6476 | |
44b6b876 PB |
6477 | if (read(d->fd, buf, PPL_HEADER_SIZE) != PPL_HEADER_SIZE) { |
6478 | perror("Read PPL header failed"); | |
6479 | ret = -1; | |
e397cefe | 6480 | break; |
44b6b876 | 6481 | } |
2432ce9b | 6482 | |
44b6b876 | 6483 | ppl_hdr = buf; |
2432ce9b | 6484 | |
44b6b876 PB |
6485 | crc = __le32_to_cpu(ppl_hdr->checksum); |
6486 | ppl_hdr->checksum = 0; | |
6487 | ||
6488 | if (crc != ~crc32c_le(~0, buf, PPL_HEADER_SIZE)) { | |
6489 | dprintf("Wrong PPL header checksum on %s\n", | |
6490 | d->devname); | |
e397cefe | 6491 | break; |
44b6b876 PB |
6492 | } |
6493 | ||
6494 | if (prev_gen_num > __le64_to_cpu(ppl_hdr->generation)) { | |
6495 | /* previous was newest, it was already checked */ | |
e397cefe | 6496 | break; |
44b6b876 PB |
6497 | } |
6498 | ||
6499 | if ((__le32_to_cpu(ppl_hdr->signature) != | |
6500 | super->anchor->orig_family_num)) { | |
6501 | dprintf("Wrong PPL header signature on %s\n", | |
6502 | d->devname); | |
6503 | ret = 1; | |
e397cefe | 6504 | break; |
44b6b876 PB |
6505 | } |
6506 | ||
6507 | ret = 0; | |
6508 | prev_gen_num = __le64_to_cpu(ppl_hdr->generation); | |
2432ce9b | 6509 | |
44b6b876 PB |
6510 | ppl_offset += PPL_HEADER_SIZE; |
6511 | for (i = 0; i < __le32_to_cpu(ppl_hdr->entries_count); i++) | |
6512 | ppl_offset += | |
6513 | __le32_to_cpu(ppl_hdr->entries[i].pp_size); | |
e397cefe AP |
6514 | |
6515 | if (!buf_prev) | |
6516 | buf_prev = buf + PPL_HEADER_SIZE; | |
6517 | tmp = buf_prev; | |
6518 | buf_prev = buf; | |
6519 | buf = tmp; | |
2432ce9b AP |
6520 | } |
6521 | ||
e397cefe AP |
6522 | if (buf_prev) { |
6523 | buf = buf_prev; | |
6524 | ppl_hdr = buf_prev; | |
6525 | } | |
2432ce9b | 6526 | |
54148aba PB |
6527 | /* |
6528 | * Update metadata to use mutliple PPLs area (1MB). | |
6529 | * This is done once for all RAID members | |
6530 | */ | |
6531 | if (info->consistency_policy == CONSISTENCY_POLICY_PPL && | |
6532 | info->ppl_size != (MULTIPLE_PPL_AREA_SIZE_IMSM >> 9)) { | |
6533 | char subarray[20]; | |
6534 | struct mdinfo *member_dev; | |
6535 | ||
6536 | sprintf(subarray, "%d", info->container_member); | |
6537 | ||
6538 | if (mdmon_running(st->container_devnm)) | |
6539 | st->update_tail = &st->updates; | |
6540 | ||
03312b52 | 6541 | if (st->ss->update_subarray(st, subarray, UOPT_PPL, NULL)) { |
54148aba PB |
6542 | pr_err("Failed to update subarray %s\n", |
6543 | subarray); | |
6544 | } else { | |
6545 | if (st->update_tail) | |
6546 | flush_metadata_updates(st); | |
6547 | else | |
6548 | st->ss->sync_metadata(st); | |
6549 | info->ppl_size = (MULTIPLE_PPL_AREA_SIZE_IMSM >> 9); | |
6550 | for (member_dev = info->devs; member_dev; | |
6551 | member_dev = member_dev->next) | |
6552 | member_dev->ppl_size = | |
6553 | (MULTIPLE_PPL_AREA_SIZE_IMSM >> 9); | |
6554 | } | |
6555 | } | |
6556 | ||
b23d0750 | 6557 | if (ret == 1) { |
2fc0fc63 AP |
6558 | struct imsm_map *map = get_imsm_map(dev, MAP_X); |
6559 | ||
50b9c10d PB |
6560 | if (map->map_state == IMSM_T_STATE_UNINITIALIZED || |
6561 | (map->map_state == IMSM_T_STATE_NORMAL && | |
2ec9d182 | 6562 | !(dev->vol.dirty & RAIDVOL_DIRTY)) || |
e397cefe | 6563 | (is_rebuilding(dev) && |
4036e7ee | 6564 | vol_curr_migr_unit(dev) == 0 && |
2ec9d182 | 6565 | get_imsm_disk_idx(dev, disk->disk.raid_disk, MAP_1) != idx)) |
b23d0750 AP |
6566 | ret = st->ss->write_init_ppl(st, info, d->fd); |
6567 | else | |
6568 | info->mismatch_cnt++; | |
e397cefe AP |
6569 | } else if (ret == 0 && |
6570 | ppl_hdr->entries_count == 0 && | |
6571 | is_rebuilding(dev) && | |
6572 | info->resync_start == 0) { | |
6573 | /* | |
6574 | * The header has no entries - add a single empty entry and | |
6575 | * rewrite the header to prevent the kernel from going into | |
6576 | * resync after an interrupted rebuild. | |
6577 | */ | |
6578 | ppl_hdr->entries_count = __cpu_to_le32(1); | |
6579 | ret = write_ppl_header(info->ppl_sector, d->fd, buf); | |
b23d0750 | 6580 | } |
2432ce9b | 6581 | |
e397cefe AP |
6582 | free(buf_orig); |
6583 | ||
2432ce9b AP |
6584 | return ret; |
6585 | } | |
6586 | ||
2432ce9b AP |
6587 | static int write_init_ppl_imsm_all(struct supertype *st, struct mdinfo *info) |
6588 | { | |
6589 | struct intel_super *super = st->sb; | |
6590 | struct dl *d; | |
6591 | int ret = 0; | |
6592 | ||
6593 | if (info->consistency_policy != CONSISTENCY_POLICY_PPL || | |
6594 | info->array.level != 5) | |
6595 | return 0; | |
6596 | ||
6597 | for (d = super->disks; d ; d = d->next) { | |
6598 | if (d->index < 0 || is_failed(&d->disk)) | |
6599 | continue; | |
6600 | ||
6601 | ret = st->ss->write_init_ppl(st, info, d->fd); | |
6602 | if (ret) | |
6603 | break; | |
6604 | } | |
6605 | ||
6606 | return ret; | |
6607 | } | |
43dad3d6 | 6608 | |
fbc42556 JR |
6609 | /******************************************************************************* |
6610 | * Function: write_init_bitmap_imsm_vol | |
6611 | * Description: Write a bitmap header and prepares the area for the bitmap. | |
6612 | * Parameters: | |
6613 | * st : supertype information | |
6614 | * vol_idx : the volume index to use | |
6615 | * | |
6616 | * Returns: | |
6617 | * 0 : success | |
6618 | * -1 : fail | |
6619 | ******************************************************************************/ | |
6620 | static int write_init_bitmap_imsm_vol(struct supertype *st, int vol_idx) | |
6621 | { | |
6622 | struct intel_super *super = st->sb; | |
6623 | int prev_current_vol = super->current_vol; | |
6624 | struct dl *d; | |
6625 | int ret = 0; | |
6626 | ||
6627 | super->current_vol = vol_idx; | |
6628 | for (d = super->disks; d; d = d->next) { | |
6629 | if (d->index < 0 || is_failed(&d->disk)) | |
6630 | continue; | |
6631 | ret = st->ss->write_bitmap(st, d->fd, NoUpdate); | |
6632 | if (ret) | |
6633 | break; | |
6634 | } | |
6635 | super->current_vol = prev_current_vol; | |
6636 | return ret; | |
6637 | } | |
6638 | ||
6639 | /******************************************************************************* | |
6640 | * Function: write_init_bitmap_imsm_all | |
6641 | * Description: Write a bitmap header and prepares the area for the bitmap. | |
6642 | * Operation is executed for volumes with CONSISTENCY_POLICY_BITMAP. | |
6643 | * Parameters: | |
6644 | * st : supertype information | |
6645 | * info : info about the volume where the bitmap should be written | |
6646 | * vol_idx : the volume index to use | |
6647 | * | |
6648 | * Returns: | |
6649 | * 0 : success | |
6650 | * -1 : fail | |
6651 | ******************************************************************************/ | |
6652 | static int write_init_bitmap_imsm_all(struct supertype *st, struct mdinfo *info, | |
6653 | int vol_idx) | |
6654 | { | |
6655 | int ret = 0; | |
6656 | ||
6657 | if (info && (info->consistency_policy == CONSISTENCY_POLICY_BITMAP)) | |
6658 | ret = write_init_bitmap_imsm_vol(st, vol_idx); | |
6659 | ||
6660 | return ret; | |
6661 | } | |
6662 | ||
c2c087e6 DW |
6663 | static int write_init_super_imsm(struct supertype *st) |
6664 | { | |
9b1fb677 DW |
6665 | struct intel_super *super = st->sb; |
6666 | int current_vol = super->current_vol; | |
2432ce9b AP |
6667 | int rv = 0; |
6668 | struct mdinfo info; | |
6669 | ||
6670 | getinfo_super_imsm(st, &info, NULL); | |
9b1fb677 DW |
6671 | |
6672 | /* we are done with current_vol reset it to point st at the container */ | |
6673 | super->current_vol = -1; | |
6674 | ||
8273f55e | 6675 | if (st->update_tail) { |
43dad3d6 DW |
6676 | /* queue the recently created array / added disk |
6677 | * as a metadata update */ | |
8273f55e | 6678 | |
43dad3d6 | 6679 | /* determine if we are creating a volume or adding a disk */ |
9b1fb677 | 6680 | if (current_vol < 0) { |
1a64be56 LM |
6681 | /* in the mgmt (add/remove) disk case we are running |
6682 | * in mdmon context, so don't close fd's | |
43dad3d6 | 6683 | */ |
2432ce9b AP |
6684 | rv = mgmt_disk(st); |
6685 | } else { | |
fbc42556 | 6686 | /* adding the second volume to the array */ |
2432ce9b | 6687 | rv = write_init_ppl_imsm_all(st, &info); |
fbc42556 JR |
6688 | if (!rv) |
6689 | rv = write_init_bitmap_imsm_all(st, &info, current_vol); | |
2432ce9b AP |
6690 | if (!rv) |
6691 | rv = create_array(st, current_vol); | |
6692 | } | |
d682f344 N |
6693 | } else { |
6694 | struct dl *d; | |
6695 | for (d = super->disks; d; d = d->next) | |
ba728be7 | 6696 | Kill(d->devname, NULL, 0, -1, 1); |
fbc42556 | 6697 | if (current_vol >= 0) { |
2432ce9b | 6698 | rv = write_init_ppl_imsm_all(st, &info); |
fbc42556 JR |
6699 | if (!rv) |
6700 | rv = write_init_bitmap_imsm_all(st, &info, current_vol); | |
6701 | } | |
6702 | ||
2432ce9b AP |
6703 | if (!rv) |
6704 | rv = write_super_imsm(st, 1); | |
d682f344 | 6705 | } |
2432ce9b AP |
6706 | |
6707 | return rv; | |
cdddbdbc DW |
6708 | } |
6709 | ||
e683ca88 | 6710 | static int store_super_imsm(struct supertype *st, int fd) |
cdddbdbc | 6711 | { |
e683ca88 DW |
6712 | struct intel_super *super = st->sb; |
6713 | struct imsm_super *mpb = super ? super->anchor : NULL; | |
551c80c1 | 6714 | |
e683ca88 | 6715 | if (!mpb) |
ad97895e DW |
6716 | return 1; |
6717 | ||
f36a9ecd PB |
6718 | if (super->sector_size == 4096) |
6719 | convert_to_4k(super); | |
e683ca88 | 6720 | return store_imsm_mpb(fd, mpb); |
cdddbdbc DW |
6721 | } |
6722 | ||
cdddbdbc | 6723 | static int validate_geometry_imsm_container(struct supertype *st, int level, |
1f5d54a0 | 6724 | int raiddisks, |
af4348dd N |
6725 | unsigned long long data_offset, |
6726 | char *dev, | |
2c514b71 NB |
6727 | unsigned long long *freesize, |
6728 | int verbose) | |
cdddbdbc | 6729 | { |
c2c087e6 DW |
6730 | int fd; |
6731 | unsigned long long ldsize; | |
8662f92d | 6732 | struct intel_super *super = NULL; |
f2f5c343 | 6733 | int rv = 0; |
cdddbdbc | 6734 | |
6f2af6a4 | 6735 | if (!is_container(level)) |
c2c087e6 DW |
6736 | return 0; |
6737 | if (!dev) | |
6738 | return 1; | |
6739 | ||
dca80fcd | 6740 | fd = dev_open(dev, O_RDONLY|O_EXCL); |
4389ce73 MT |
6741 | if (!is_fd_valid(fd)) { |
6742 | pr_vrb("imsm: Cannot open %s: %s\n", dev, strerror(errno)); | |
c2c087e6 DW |
6743 | return 0; |
6744 | } | |
8662f92d MT |
6745 | if (!get_dev_size(fd, dev, &ldsize)) |
6746 | goto exit; | |
f2f5c343 LM |
6747 | |
6748 | /* capabilities retrieve could be possible | |
6749 | * note that there is no fd for the disks in array. | |
6750 | */ | |
6751 | super = alloc_super(); | |
8662f92d MT |
6752 | if (!super) |
6753 | goto exit; | |
6754 | ||
6755 | if (!get_dev_sector_size(fd, NULL, &super->sector_size)) | |
6756 | goto exit; | |
fa7bb6f8 | 6757 | |
ba728be7 | 6758 | rv = find_intel_hba_capability(fd, super, verbose > 0 ? dev : NULL); |
f2f5c343 LM |
6759 | if (rv != 0) { |
6760 | #if DEBUG | |
6761 | char str[256]; | |
6762 | fd2devname(fd, str); | |
1ade5cc1 | 6763 | dprintf("fd: %d %s orom: %p rv: %d raiddisk: %d\n", |
f2f5c343 LM |
6764 | fd, str, super->orom, rv, raiddisks); |
6765 | #endif | |
6766 | /* no orom/efi or non-intel hba of the disk */ | |
8662f92d MT |
6767 | rv = 0; |
6768 | goto exit; | |
f2f5c343 | 6769 | } |
9126b9a8 CA |
6770 | if (super->orom) { |
6771 | if (raiddisks > super->orom->tds) { | |
6772 | if (verbose) | |
7a862a02 | 6773 | pr_err("%d exceeds maximum number of platform supported disks: %d\n", |
9126b9a8 | 6774 | raiddisks, super->orom->tds); |
8662f92d | 6775 | goto exit; |
9126b9a8 CA |
6776 | } |
6777 | if ((super->orom->attr & IMSM_OROM_ATTR_2TB_DISK) == 0 && | |
6778 | (ldsize >> 9) >> 32 > 0) { | |
6779 | if (verbose) | |
e7b84f9d | 6780 | pr_err("%s exceeds maximum platform supported size\n", dev); |
8662f92d MT |
6781 | goto exit; |
6782 | } | |
6783 | ||
6784 | if (super->hba->type == SYS_DEV_VMD || | |
6785 | super->hba->type == SYS_DEV_NVME) { | |
6786 | if (!imsm_is_nvme_namespace_supported(fd, 1)) { | |
6787 | if (verbose) | |
6788 | pr_err("NVMe namespace %s is not supported by IMSM\n", | |
6789 | basename(dev)); | |
6790 | goto exit; | |
6791 | } | |
9126b9a8 | 6792 | } |
f2f5c343 | 6793 | } |
1f5d54a0 MT |
6794 | if (freesize) |
6795 | *freesize = avail_size_imsm(st, ldsize >> 9, data_offset); | |
8662f92d MT |
6796 | rv = 1; |
6797 | exit: | |
6798 | if (super) | |
6799 | free_imsm(super); | |
6800 | close(fd); | |
c2c087e6 | 6801 | |
8662f92d | 6802 | return rv; |
cdddbdbc DW |
6803 | } |
6804 | ||
0dcecb2e DW |
6805 | static unsigned long long find_size(struct extent *e, int *idx, int num_extents) |
6806 | { | |
6807 | const unsigned long long base_start = e[*idx].start; | |
6808 | unsigned long long end = base_start + e[*idx].size; | |
6809 | int i; | |
6810 | ||
6811 | if (base_start == end) | |
6812 | return 0; | |
6813 | ||
6814 | *idx = *idx + 1; | |
6815 | for (i = *idx; i < num_extents; i++) { | |
6816 | /* extend overlapping extents */ | |
6817 | if (e[i].start >= base_start && | |
6818 | e[i].start <= end) { | |
6819 | if (e[i].size == 0) | |
6820 | return 0; | |
6821 | if (e[i].start + e[i].size > end) | |
6822 | end = e[i].start + e[i].size; | |
6823 | } else if (e[i].start > end) { | |
6824 | *idx = i; | |
6825 | break; | |
6826 | } | |
6827 | } | |
6828 | ||
6829 | return end - base_start; | |
6830 | } | |
6831 | ||
6832 | static unsigned long long merge_extents(struct intel_super *super, int sum_extents) | |
6833 | { | |
6834 | /* build a composite disk with all known extents and generate a new | |
6835 | * 'maxsize' given the "all disks in an array must share a common start | |
6836 | * offset" constraint | |
6837 | */ | |
503975b9 | 6838 | struct extent *e = xcalloc(sum_extents, sizeof(*e)); |
0dcecb2e DW |
6839 | struct dl *dl; |
6840 | int i, j; | |
6841 | int start_extent; | |
6842 | unsigned long long pos; | |
b9d77223 | 6843 | unsigned long long start = 0; |
0dcecb2e DW |
6844 | unsigned long long maxsize; |
6845 | unsigned long reserve; | |
6846 | ||
0dcecb2e DW |
6847 | /* coalesce and sort all extents. also, check to see if we need to |
6848 | * reserve space between member arrays | |
6849 | */ | |
6850 | j = 0; | |
6851 | for (dl = super->disks; dl; dl = dl->next) { | |
6852 | if (!dl->e) | |
6853 | continue; | |
6854 | for (i = 0; i < dl->extent_cnt; i++) | |
6855 | e[j++] = dl->e[i]; | |
6856 | } | |
6857 | qsort(e, sum_extents, sizeof(*e), cmp_extent); | |
6858 | ||
6859 | /* merge extents */ | |
6860 | i = 0; | |
6861 | j = 0; | |
6862 | while (i < sum_extents) { | |
6863 | e[j].start = e[i].start; | |
6864 | e[j].size = find_size(e, &i, sum_extents); | |
6865 | j++; | |
6866 | if (e[j-1].size == 0) | |
6867 | break; | |
6868 | } | |
6869 | ||
6870 | pos = 0; | |
6871 | maxsize = 0; | |
6872 | start_extent = 0; | |
6873 | i = 0; | |
6874 | do { | |
6875 | unsigned long long esize; | |
6876 | ||
6877 | esize = e[i].start - pos; | |
6878 | if (esize >= maxsize) { | |
6879 | maxsize = esize; | |
6880 | start = pos; | |
6881 | start_extent = i; | |
6882 | } | |
6883 | pos = e[i].start + e[i].size; | |
6884 | i++; | |
6885 | } while (e[i-1].size); | |
6886 | free(e); | |
6887 | ||
a7dd165b DW |
6888 | if (maxsize == 0) |
6889 | return 0; | |
6890 | ||
6891 | /* FIXME assumes volume at offset 0 is the first volume in a | |
6892 | * container | |
6893 | */ | |
0dcecb2e DW |
6894 | if (start_extent > 0) |
6895 | reserve = IMSM_RESERVED_SECTORS; /* gap between raid regions */ | |
6896 | else | |
6897 | reserve = 0; | |
6898 | ||
6899 | if (maxsize < reserve) | |
a7dd165b | 6900 | return 0; |
0dcecb2e | 6901 | |
5551b113 | 6902 | super->create_offset = ~((unsigned long long) 0); |
0dcecb2e | 6903 | if (start + reserve > super->create_offset) |
a7dd165b | 6904 | return 0; /* start overflows create_offset */ |
0dcecb2e DW |
6905 | super->create_offset = start + reserve; |
6906 | ||
6907 | return maxsize - reserve; | |
6908 | } | |
6909 | ||
88c32bb1 DW |
6910 | static int is_raid_level_supported(const struct imsm_orom *orom, int level, int raiddisks) |
6911 | { | |
6912 | if (level < 0 || level == 6 || level == 4) | |
6913 | return 0; | |
6914 | ||
6915 | /* if we have an orom prevent invalid raid levels */ | |
6916 | if (orom) | |
6917 | switch (level) { | |
6918 | case 0: return imsm_orom_has_raid0(orom); | |
6919 | case 1: | |
6920 | if (raiddisks > 2) | |
6921 | return imsm_orom_has_raid1e(orom); | |
1c556e92 DW |
6922 | return imsm_orom_has_raid1(orom) && raiddisks == 2; |
6923 | case 10: return imsm_orom_has_raid10(orom) && raiddisks == 4; | |
6924 | case 5: return imsm_orom_has_raid5(orom) && raiddisks > 2; | |
88c32bb1 DW |
6925 | } |
6926 | else | |
6927 | return 1; /* not on an Intel RAID platform so anything goes */ | |
6928 | ||
6929 | return 0; | |
6930 | } | |
6931 | ||
ca9de185 LM |
6932 | static int |
6933 | active_arrays_by_format(char *name, char* hba, struct md_list **devlist, | |
6934 | int dpa, int verbose) | |
6935 | { | |
6936 | struct mdstat_ent *mdstat = mdstat_read(0, 0); | |
594dc1b8 | 6937 | struct mdstat_ent *memb; |
ca9de185 LM |
6938 | int count = 0; |
6939 | int num = 0; | |
594dc1b8 | 6940 | struct md_list *dv; |
ca9de185 LM |
6941 | int found; |
6942 | ||
6943 | for (memb = mdstat ; memb ; memb = memb->next) { | |
6944 | if (memb->metadata_version && | |
fc54fe7a | 6945 | (strncmp(memb->metadata_version, "external:", 9) == 0) && |
ca9de185 LM |
6946 | (strcmp(&memb->metadata_version[9], name) == 0) && |
6947 | !is_subarray(memb->metadata_version+9) && | |
6948 | memb->members) { | |
6949 | struct dev_member *dev = memb->members; | |
6950 | int fd = -1; | |
4389ce73 | 6951 | while (dev && !is_fd_valid(fd)) { |
503975b9 N |
6952 | char *path = xmalloc(strlen(dev->name) + strlen("/dev/") + 1); |
6953 | num = sprintf(path, "%s%s", "/dev/", dev->name); | |
6954 | if (num > 0) | |
6955 | fd = open(path, O_RDONLY, 0); | |
4389ce73 | 6956 | if (num <= 0 || !is_fd_valid(fd)) { |
676e87a8 | 6957 | pr_vrb("Cannot open %s: %s\n", |
503975b9 | 6958 | dev->name, strerror(errno)); |
ca9de185 | 6959 | } |
503975b9 | 6960 | free(path); |
ca9de185 LM |
6961 | dev = dev->next; |
6962 | } | |
6963 | found = 0; | |
4389ce73 | 6964 | if (is_fd_valid(fd) && disk_attached_to_hba(fd, hba)) { |
ca9de185 LM |
6965 | struct mdstat_ent *vol; |
6966 | for (vol = mdstat ; vol ; vol = vol->next) { | |
089f9d79 | 6967 | if (vol->active > 0 && |
ca9de185 | 6968 | vol->metadata_version && |
9581efb1 | 6969 | is_container_member(vol, memb->devnm)) { |
ca9de185 LM |
6970 | found++; |
6971 | count++; | |
6972 | } | |
6973 | } | |
6974 | if (*devlist && (found < dpa)) { | |
503975b9 | 6975 | dv = xcalloc(1, sizeof(*dv)); |
9581efb1 N |
6976 | dv->devname = xmalloc(strlen(memb->devnm) + strlen("/dev/") + 1); |
6977 | sprintf(dv->devname, "%s%s", "/dev/", memb->devnm); | |
503975b9 N |
6978 | dv->found = found; |
6979 | dv->used = 0; | |
6980 | dv->next = *devlist; | |
6981 | *devlist = dv; | |
ca9de185 LM |
6982 | } |
6983 | } | |
4389ce73 | 6984 | close_fd(&fd); |
ca9de185 LM |
6985 | } |
6986 | } | |
6987 | free_mdstat(mdstat); | |
6988 | return count; | |
6989 | } | |
6990 | ||
6991 | #ifdef DEBUG_LOOP | |
6992 | static struct md_list* | |
6993 | get_loop_devices(void) | |
6994 | { | |
6995 | int i; | |
6996 | struct md_list *devlist = NULL; | |
594dc1b8 | 6997 | struct md_list *dv; |
ca9de185 LM |
6998 | |
6999 | for(i = 0; i < 12; i++) { | |
503975b9 N |
7000 | dv = xcalloc(1, sizeof(*dv)); |
7001 | dv->devname = xmalloc(40); | |
ca9de185 LM |
7002 | sprintf(dv->devname, "/dev/loop%d", i); |
7003 | dv->next = devlist; | |
7004 | devlist = dv; | |
7005 | } | |
7006 | return devlist; | |
7007 | } | |
7008 | #endif | |
7009 | ||
7010 | static struct md_list* | |
7011 | get_devices(const char *hba_path) | |
7012 | { | |
7013 | struct md_list *devlist = NULL; | |
594dc1b8 | 7014 | struct md_list *dv; |
ca9de185 LM |
7015 | struct dirent *ent; |
7016 | DIR *dir; | |
7017 | int err = 0; | |
7018 | ||
7019 | #if DEBUG_LOOP | |
7020 | devlist = get_loop_devices(); | |
7021 | return devlist; | |
7022 | #endif | |
7023 | /* scroll through /sys/dev/block looking for devices attached to | |
7024 | * this hba | |
7025 | */ | |
7026 | dir = opendir("/sys/dev/block"); | |
7027 | for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) { | |
7028 | int fd; | |
7029 | char buf[1024]; | |
7030 | int major, minor; | |
7031 | char *path = NULL; | |
7032 | if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2) | |
7033 | continue; | |
7c798f87 | 7034 | path = devt_to_devpath(makedev(major, minor), 1, NULL); |
ca9de185 LM |
7035 | if (!path) |
7036 | continue; | |
7037 | if (!path_attached_to_hba(path, hba_path)) { | |
7038 | free(path); | |
7039 | path = NULL; | |
7040 | continue; | |
7041 | } | |
7042 | free(path); | |
7043 | path = NULL; | |
7044 | fd = dev_open(ent->d_name, O_RDONLY); | |
4389ce73 | 7045 | if (is_fd_valid(fd)) { |
ca9de185 LM |
7046 | fd2devname(fd, buf); |
7047 | close(fd); | |
7048 | } else { | |
e7b84f9d | 7049 | pr_err("cannot open device: %s\n", |
ca9de185 LM |
7050 | ent->d_name); |
7051 | continue; | |
7052 | } | |
7053 | ||
503975b9 N |
7054 | dv = xcalloc(1, sizeof(*dv)); |
7055 | dv->devname = xstrdup(buf); | |
ca9de185 LM |
7056 | dv->next = devlist; |
7057 | devlist = dv; | |
7058 | } | |
7059 | if (err) { | |
7060 | while(devlist) { | |
7061 | dv = devlist; | |
7062 | devlist = devlist->next; | |
7063 | free(dv->devname); | |
7064 | free(dv); | |
7065 | } | |
7066 | } | |
562aa102 | 7067 | closedir(dir); |
ca9de185 LM |
7068 | return devlist; |
7069 | } | |
7070 | ||
7071 | static int | |
7072 | count_volumes_list(struct md_list *devlist, char *homehost, | |
7073 | int verbose, int *found) | |
7074 | { | |
7075 | struct md_list *tmpdev; | |
7076 | int count = 0; | |
594dc1b8 | 7077 | struct supertype *st; |
ca9de185 LM |
7078 | |
7079 | /* first walk the list of devices to find a consistent set | |
7080 | * that match the criterea, if that is possible. | |
7081 | * We flag the ones we like with 'used'. | |
7082 | */ | |
7083 | *found = 0; | |
7084 | st = match_metadata_desc_imsm("imsm"); | |
7085 | if (st == NULL) { | |
676e87a8 | 7086 | pr_vrb("cannot allocate memory for imsm supertype\n"); |
ca9de185 LM |
7087 | return 0; |
7088 | } | |
7089 | ||
7090 | for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) { | |
7091 | char *devname = tmpdev->devname; | |
0a6bff09 | 7092 | dev_t rdev; |
ca9de185 LM |
7093 | struct supertype *tst; |
7094 | int dfd; | |
7095 | if (tmpdev->used > 1) | |
7096 | continue; | |
7097 | tst = dup_super(st); | |
7098 | if (tst == NULL) { | |
676e87a8 | 7099 | pr_vrb("cannot allocate memory for imsm supertype\n"); |
ca9de185 LM |
7100 | goto err_1; |
7101 | } | |
7102 | tmpdev->container = 0; | |
7103 | dfd = dev_open(devname, O_RDONLY|O_EXCL); | |
4389ce73 | 7104 | if (!is_fd_valid(dfd)) { |
1ade5cc1 | 7105 | dprintf("cannot open device %s: %s\n", |
ca9de185 LM |
7106 | devname, strerror(errno)); |
7107 | tmpdev->used = 2; | |
0a6bff09 | 7108 | } else if (!fstat_is_blkdev(dfd, devname, &rdev)) { |
ca9de185 LM |
7109 | tmpdev->used = 2; |
7110 | } else if (must_be_container(dfd)) { | |
7111 | struct supertype *cst; | |
7112 | cst = super_by_fd(dfd, NULL); | |
7113 | if (cst == NULL) { | |
1ade5cc1 | 7114 | dprintf("cannot recognize container type %s\n", |
ca9de185 LM |
7115 | devname); |
7116 | tmpdev->used = 2; | |
7117 | } else if (tst->ss != st->ss) { | |
1ade5cc1 | 7118 | dprintf("non-imsm container - ignore it: %s\n", |
ca9de185 LM |
7119 | devname); |
7120 | tmpdev->used = 2; | |
7121 | } else if (!tst->ss->load_container || | |
7122 | tst->ss->load_container(tst, dfd, NULL)) | |
7123 | tmpdev->used = 2; | |
7124 | else { | |
7125 | tmpdev->container = 1; | |
7126 | } | |
7127 | if (cst) | |
7128 | cst->ss->free_super(cst); | |
7129 | } else { | |
0a6bff09 | 7130 | tmpdev->st_rdev = rdev; |
ca9de185 | 7131 | if (tst->ss->load_super(tst,dfd, NULL)) { |
1ade5cc1 | 7132 | dprintf("no RAID superblock on %s\n", |
ca9de185 LM |
7133 | devname); |
7134 | tmpdev->used = 2; | |
7135 | } else if (tst->ss->compare_super == NULL) { | |
1ade5cc1 | 7136 | dprintf("Cannot assemble %s metadata on %s\n", |
ca9de185 LM |
7137 | tst->ss->name, devname); |
7138 | tmpdev->used = 2; | |
7139 | } | |
7140 | } | |
4389ce73 MT |
7141 | close_fd(&dfd); |
7142 | ||
ca9de185 LM |
7143 | if (tmpdev->used == 2 || tmpdev->used == 4) { |
7144 | /* Ignore unrecognised devices during auto-assembly */ | |
7145 | goto loop; | |
7146 | } | |
7147 | else { | |
7148 | struct mdinfo info; | |
7149 | tst->ss->getinfo_super(tst, &info, NULL); | |
7150 | ||
7151 | if (st->minor_version == -1) | |
7152 | st->minor_version = tst->minor_version; | |
7153 | ||
7154 | if (memcmp(info.uuid, uuid_zero, | |
7155 | sizeof(int[4])) == 0) { | |
7156 | /* this is a floating spare. It cannot define | |
7157 | * an array unless there are no more arrays of | |
7158 | * this type to be found. It can be included | |
7159 | * in an array of this type though. | |
7160 | */ | |
7161 | tmpdev->used = 3; | |
7162 | goto loop; | |
7163 | } | |
7164 | ||
7165 | if (st->ss != tst->ss || | |
7166 | st->minor_version != tst->minor_version || | |
c7b8547c | 7167 | st->ss->compare_super(st, tst, 1) != 0) { |
ca9de185 LM |
7168 | /* Some mismatch. If exactly one array matches this host, |
7169 | * we can resolve on that one. | |
7170 | * Or, if we are auto assembling, we just ignore the second | |
7171 | * for now. | |
7172 | */ | |
1ade5cc1 | 7173 | dprintf("superblock on %s doesn't match others - assembly aborted\n", |
ca9de185 LM |
7174 | devname); |
7175 | goto loop; | |
7176 | } | |
7177 | tmpdev->used = 1; | |
7178 | *found = 1; | |
7179 | dprintf("found: devname: %s\n", devname); | |
7180 | } | |
7181 | loop: | |
7182 | if (tst) | |
7183 | tst->ss->free_super(tst); | |
7184 | } | |
7185 | if (*found != 0) { | |
7186 | int err; | |
7187 | if ((err = load_super_imsm_all(st, -1, &st->sb, NULL, devlist, 0)) == 0) { | |
7188 | struct mdinfo *iter, *head = st->ss->container_content(st, NULL); | |
7189 | for (iter = head; iter; iter = iter->next) { | |
7190 | dprintf("content->text_version: %s vol\n", | |
7191 | iter->text_version); | |
7192 | if (iter->array.state & (1<<MD_SB_BLOCK_VOLUME)) { | |
7193 | /* do not assemble arrays with unsupported | |
7194 | configurations */ | |
1ade5cc1 | 7195 | dprintf("Cannot activate member %s.\n", |
ca9de185 LM |
7196 | iter->text_version); |
7197 | } else | |
7198 | count++; | |
7199 | } | |
7200 | sysfs_free(head); | |
7201 | ||
7202 | } else { | |
1ade5cc1 | 7203 | dprintf("No valid super block on device list: err: %d %p\n", |
ca9de185 LM |
7204 | err, st->sb); |
7205 | } | |
7206 | } else { | |
1ade5cc1 | 7207 | dprintf("no more devices to examine\n"); |
ca9de185 LM |
7208 | } |
7209 | ||
7210 | for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) { | |
089f9d79 | 7211 | if (tmpdev->used == 1 && tmpdev->found) { |
ca9de185 LM |
7212 | if (count) { |
7213 | if (count < tmpdev->found) | |
7214 | count = 0; | |
7215 | else | |
7216 | count -= tmpdev->found; | |
7217 | } | |
7218 | } | |
7219 | if (tmpdev->used == 1) | |
7220 | tmpdev->used = 4; | |
7221 | } | |
7222 | err_1: | |
7223 | if (st) | |
7224 | st->ss->free_super(st); | |
7225 | return count; | |
7226 | } | |
7227 | ||
d3c11416 AO |
7228 | static int __count_volumes(char *hba_path, int dpa, int verbose, |
7229 | int cmp_hba_path) | |
ca9de185 | 7230 | { |
72a45777 | 7231 | struct sys_dev *idev, *intel_devices = find_intel_devices(); |
ca9de185 | 7232 | int count = 0; |
72a45777 PB |
7233 | const struct orom_entry *entry; |
7234 | struct devid_list *dv, *devid_list; | |
ca9de185 | 7235 | |
d3c11416 | 7236 | if (!hba_path) |
ca9de185 LM |
7237 | return 0; |
7238 | ||
72a45777 | 7239 | for (idev = intel_devices; idev; idev = idev->next) { |
d3c11416 AO |
7240 | if (strstr(idev->path, hba_path)) |
7241 | break; | |
72a45777 PB |
7242 | } |
7243 | ||
7244 | if (!idev || !idev->dev_id) | |
ca9de185 | 7245 | return 0; |
72a45777 PB |
7246 | |
7247 | entry = get_orom_entry_by_device_id(idev->dev_id); | |
7248 | ||
7249 | if (!entry || !entry->devid_list) | |
7250 | return 0; | |
7251 | ||
7252 | devid_list = entry->devid_list; | |
7253 | for (dv = devid_list; dv; dv = dv->next) { | |
594dc1b8 | 7254 | struct md_list *devlist; |
d3c11416 AO |
7255 | struct sys_dev *device = NULL; |
7256 | char *hpath; | |
72a45777 PB |
7257 | int found = 0; |
7258 | ||
d3c11416 AO |
7259 | if (cmp_hba_path) |
7260 | device = device_by_id_and_path(dv->devid, hba_path); | |
7261 | else | |
7262 | device = device_by_id(dv->devid); | |
7263 | ||
72a45777 | 7264 | if (device) |
d3c11416 | 7265 | hpath = device->path; |
72a45777 PB |
7266 | else |
7267 | return 0; | |
7268 | ||
d3c11416 | 7269 | devlist = get_devices(hpath); |
72a45777 PB |
7270 | /* if no intel devices return zero volumes */ |
7271 | if (devlist == NULL) | |
7272 | return 0; | |
7273 | ||
d3c11416 AO |
7274 | count += active_arrays_by_format("imsm", hpath, &devlist, dpa, |
7275 | verbose); | |
7276 | dprintf("path: %s active arrays: %d\n", hpath, count); | |
72a45777 PB |
7277 | if (devlist == NULL) |
7278 | return 0; | |
7279 | do { | |
7280 | found = 0; | |
7281 | count += count_volumes_list(devlist, | |
7282 | NULL, | |
7283 | verbose, | |
7284 | &found); | |
7285 | dprintf("found %d count: %d\n", found, count); | |
7286 | } while (found); | |
7287 | ||
d3c11416 | 7288 | dprintf("path: %s total number of volumes: %d\n", hpath, count); |
72a45777 PB |
7289 | |
7290 | while (devlist) { | |
7291 | struct md_list *dv = devlist; | |
7292 | devlist = devlist->next; | |
7293 | free(dv->devname); | |
7294 | free(dv); | |
7295 | } | |
ca9de185 LM |
7296 | } |
7297 | return count; | |
7298 | } | |
7299 | ||
d3c11416 AO |
7300 | static int count_volumes(struct intel_hba *hba, int dpa, int verbose) |
7301 | { | |
7302 | if (!hba) | |
7303 | return 0; | |
7304 | if (hba->type == SYS_DEV_VMD) { | |
7305 | struct sys_dev *dev; | |
7306 | int count = 0; | |
7307 | ||
7308 | for (dev = find_intel_devices(); dev; dev = dev->next) { | |
7309 | if (dev->type == SYS_DEV_VMD) | |
7310 | count += __count_volumes(dev->path, dpa, | |
7311 | verbose, 1); | |
7312 | } | |
7313 | return count; | |
7314 | } | |
7315 | return __count_volumes(hba->path, dpa, verbose, 0); | |
7316 | } | |
7317 | ||
cd9d1ac7 DW |
7318 | static int imsm_default_chunk(const struct imsm_orom *orom) |
7319 | { | |
7320 | /* up to 512 if the plaform supports it, otherwise the platform max. | |
7321 | * 128 if no platform detected | |
7322 | */ | |
7323 | int fs = max(7, orom ? fls(orom->sss) : 0); | |
7324 | ||
7325 | return min(512, (1 << fs)); | |
7326 | } | |
73408129 | 7327 | |
6592ce37 DW |
7328 | static int |
7329 | validate_geometry_imsm_orom(struct intel_super *super, int level, int layout, | |
2cc699af | 7330 | int raiddisks, int *chunk, unsigned long long size, int verbose) |
6592ce37 | 7331 | { |
660260d0 DW |
7332 | /* check/set platform and metadata limits/defaults */ |
7333 | if (super->orom && raiddisks > super->orom->dpa) { | |
676e87a8 | 7334 | pr_vrb("platform supports a maximum of %d disks per array\n", |
660260d0 | 7335 | super->orom->dpa); |
73408129 LM |
7336 | return 0; |
7337 | } | |
7338 | ||
5d500228 | 7339 | /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */ |
660260d0 | 7340 | if (!is_raid_level_supported(super->orom, level, raiddisks)) { |
676e87a8 | 7341 | pr_vrb("platform does not support raid%d with %d disk%s\n", |
6592ce37 DW |
7342 | level, raiddisks, raiddisks > 1 ? "s" : ""); |
7343 | return 0; | |
7344 | } | |
cd9d1ac7 | 7345 | |
7ccc4cc4 | 7346 | if (*chunk == 0 || *chunk == UnSet) |
cd9d1ac7 DW |
7347 | *chunk = imsm_default_chunk(super->orom); |
7348 | ||
7ccc4cc4 | 7349 | if (super->orom && !imsm_orom_has_chunk(super->orom, *chunk)) { |
676e87a8 | 7350 | pr_vrb("platform does not support a chunk size of: %d\n", *chunk); |
cd9d1ac7 | 7351 | return 0; |
6592ce37 | 7352 | } |
cd9d1ac7 | 7353 | |
6592ce37 DW |
7354 | if (layout != imsm_level_to_layout(level)) { |
7355 | if (level == 5) | |
676e87a8 | 7356 | pr_vrb("imsm raid 5 only supports the left-asymmetric layout\n"); |
6592ce37 | 7357 | else if (level == 10) |
676e87a8 | 7358 | pr_vrb("imsm raid 10 only supports the n2 layout\n"); |
6592ce37 | 7359 | else |
676e87a8 | 7360 | pr_vrb("imsm unknown layout %#x for this raid level %d\n", |
6592ce37 DW |
7361 | layout, level); |
7362 | return 0; | |
7363 | } | |
2cc699af | 7364 | |
7ccc4cc4 | 7365 | if (super->orom && (super->orom->attr & IMSM_OROM_ATTR_2TB) == 0 && |
2cc699af | 7366 | (calc_array_size(level, raiddisks, layout, *chunk, size) >> 32) > 0) { |
676e87a8 | 7367 | pr_vrb("platform does not support a volume size over 2TB\n"); |
2cc699af CA |
7368 | return 0; |
7369 | } | |
614902f6 | 7370 | |
6592ce37 DW |
7371 | return 1; |
7372 | } | |
7373 | ||
1011e834 | 7374 | /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd |
c2c087e6 DW |
7375 | * FIX ME add ahci details |
7376 | */ | |
8b353278 | 7377 | static int validate_geometry_imsm_volume(struct supertype *st, int level, |
c21e737b | 7378 | int layout, int raiddisks, int *chunk, |
af4348dd N |
7379 | unsigned long long size, |
7380 | unsigned long long data_offset, | |
7381 | char *dev, | |
2c514b71 NB |
7382 | unsigned long long *freesize, |
7383 | int verbose) | |
cdddbdbc | 7384 | { |
9e04ac1c | 7385 | dev_t rdev; |
c2c087e6 | 7386 | struct intel_super *super = st->sb; |
b2916f25 | 7387 | struct imsm_super *mpb; |
c2c087e6 DW |
7388 | struct dl *dl; |
7389 | unsigned long long pos = 0; | |
7390 | unsigned long long maxsize; | |
7391 | struct extent *e; | |
7392 | int i; | |
cdddbdbc | 7393 | |
88c32bb1 DW |
7394 | /* We must have the container info already read in. */ |
7395 | if (!super) | |
c2c087e6 DW |
7396 | return 0; |
7397 | ||
b2916f25 JS |
7398 | mpb = super->anchor; |
7399 | ||
2cc699af | 7400 | if (!validate_geometry_imsm_orom(super, level, layout, raiddisks, chunk, size, verbose)) { |
3e684231 | 7401 | pr_err("RAID geometry validation failed. Cannot proceed with the action(s).\n"); |
c2c087e6 | 7402 | return 0; |
d54559f0 | 7403 | } |
c2c087e6 DW |
7404 | if (!dev) { |
7405 | /* General test: make sure there is space for | |
2da8544a DW |
7406 | * 'raiddisks' device extents of size 'size' at a given |
7407 | * offset | |
c2c087e6 | 7408 | */ |
e46273eb | 7409 | unsigned long long minsize = size; |
b7528a20 | 7410 | unsigned long long start_offset = MaxSector; |
c2c087e6 DW |
7411 | int dcnt = 0; |
7412 | if (minsize == 0) | |
7413 | minsize = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS; | |
7414 | for (dl = super->disks; dl ; dl = dl->next) { | |
7415 | int found = 0; | |
7416 | ||
bf5a934a | 7417 | pos = 0; |
c2c087e6 | 7418 | i = 0; |
05501181 | 7419 | e = get_extents(super, dl, 0); |
c2c087e6 DW |
7420 | if (!e) continue; |
7421 | do { | |
7422 | unsigned long long esize; | |
7423 | esize = e[i].start - pos; | |
7424 | if (esize >= minsize) | |
7425 | found = 1; | |
b7528a20 | 7426 | if (found && start_offset == MaxSector) { |
2da8544a DW |
7427 | start_offset = pos; |
7428 | break; | |
7429 | } else if (found && pos != start_offset) { | |
7430 | found = 0; | |
7431 | break; | |
7432 | } | |
c2c087e6 DW |
7433 | pos = e[i].start + e[i].size; |
7434 | i++; | |
7435 | } while (e[i-1].size); | |
7436 | if (found) | |
7437 | dcnt++; | |
7438 | free(e); | |
7439 | } | |
7440 | if (dcnt < raiddisks) { | |
2c514b71 | 7441 | if (verbose) |
7a862a02 | 7442 | pr_err("imsm: Not enough devices with space for this array (%d < %d)\n", |
2c514b71 | 7443 | dcnt, raiddisks); |
c2c087e6 DW |
7444 | return 0; |
7445 | } | |
7446 | return 1; | |
7447 | } | |
0dcecb2e | 7448 | |
c2c087e6 | 7449 | /* This device must be a member of the set */ |
9e04ac1c | 7450 | if (!stat_is_blkdev(dev, &rdev)) |
c2c087e6 DW |
7451 | return 0; |
7452 | for (dl = super->disks ; dl ; dl = dl->next) { | |
9e04ac1c ZL |
7453 | if (dl->major == (int)major(rdev) && |
7454 | dl->minor == (int)minor(rdev)) | |
c2c087e6 DW |
7455 | break; |
7456 | } | |
7457 | if (!dl) { | |
2c514b71 | 7458 | if (verbose) |
7a862a02 | 7459 | pr_err("%s is not in the same imsm set\n", dev); |
c2c087e6 | 7460 | return 0; |
a20d2ba5 DW |
7461 | } else if (super->orom && dl->index < 0 && mpb->num_raid_devs) { |
7462 | /* If a volume is present then the current creation attempt | |
7463 | * cannot incorporate new spares because the orom may not | |
7464 | * understand this configuration (all member disks must be | |
7465 | * members of each array in the container). | |
7466 | */ | |
7a862a02 N |
7467 | pr_err("%s is a spare and a volume is already defined for this container\n", dev); |
7468 | pr_err("The option-rom requires all member disks to be a member of all volumes\n"); | |
a20d2ba5 | 7469 | return 0; |
5fe62b94 WD |
7470 | } else if (super->orom && mpb->num_raid_devs > 0 && |
7471 | mpb->num_disks != raiddisks) { | |
7a862a02 | 7472 | pr_err("The option-rom requires all member disks to be a member of all volumes\n"); |
5fe62b94 | 7473 | return 0; |
c2c087e6 | 7474 | } |
0dcecb2e DW |
7475 | |
7476 | /* retrieve the largest free space block */ | |
05501181 | 7477 | e = get_extents(super, dl, 0); |
c2c087e6 DW |
7478 | maxsize = 0; |
7479 | i = 0; | |
0dcecb2e DW |
7480 | if (e) { |
7481 | do { | |
7482 | unsigned long long esize; | |
7483 | ||
7484 | esize = e[i].start - pos; | |
7485 | if (esize >= maxsize) | |
7486 | maxsize = esize; | |
7487 | pos = e[i].start + e[i].size; | |
7488 | i++; | |
7489 | } while (e[i-1].size); | |
7490 | dl->e = e; | |
7491 | dl->extent_cnt = i; | |
7492 | } else { | |
7493 | if (verbose) | |
e7b84f9d | 7494 | pr_err("unable to determine free space for: %s\n", |
0dcecb2e DW |
7495 | dev); |
7496 | return 0; | |
7497 | } | |
7498 | if (maxsize < size) { | |
7499 | if (verbose) | |
e7b84f9d | 7500 | pr_err("%s not enough space (%llu < %llu)\n", |
0dcecb2e DW |
7501 | dev, maxsize, size); |
7502 | return 0; | |
7503 | } | |
7504 | ||
7505 | /* count total number of extents for merge */ | |
7506 | i = 0; | |
7507 | for (dl = super->disks; dl; dl = dl->next) | |
7508 | if (dl->e) | |
7509 | i += dl->extent_cnt; | |
7510 | ||
7511 | maxsize = merge_extents(super, i); | |
3baa56ab | 7512 | |
1a1ced1e KS |
7513 | if (mpb->num_raid_devs > 0 && size && size != maxsize) |
7514 | pr_err("attempting to create a second volume with size less then remaining space.\n"); | |
3baa56ab | 7515 | |
a7dd165b | 7516 | if (maxsize < size || maxsize == 0) { |
b3071342 LD |
7517 | if (verbose) { |
7518 | if (maxsize == 0) | |
7a862a02 | 7519 | pr_err("no free space left on device. Aborting...\n"); |
b3071342 | 7520 | else |
7a862a02 | 7521 | pr_err("not enough space to create volume of given size (%llu < %llu). Aborting...\n", |
b3071342 LD |
7522 | maxsize, size); |
7523 | } | |
0dcecb2e | 7524 | return 0; |
0dcecb2e DW |
7525 | } |
7526 | ||
c2c087e6 DW |
7527 | *freesize = maxsize; |
7528 | ||
ca9de185 | 7529 | if (super->orom) { |
72a45777 | 7530 | int count = count_volumes(super->hba, |
ca9de185 LM |
7531 | super->orom->dpa, verbose); |
7532 | if (super->orom->vphba <= count) { | |
676e87a8 | 7533 | pr_vrb("platform does not support more than %d raid volumes.\n", |
ca9de185 LM |
7534 | super->orom->vphba); |
7535 | return 0; | |
7536 | } | |
7537 | } | |
c2c087e6 | 7538 | return 1; |
cdddbdbc DW |
7539 | } |
7540 | ||
6d4d9ab2 MT |
7541 | /** |
7542 | * imsm_get_free_size() - get the biggest, common free space from members. | |
7543 | * @super: &intel_super pointer, not NULL. | |
7544 | * @raiddisks: number of raid disks. | |
7545 | * @size: requested size, could be 0 (means max size). | |
7546 | * @chunk: requested chunk. | |
7547 | * @freesize: pointer for returned size value. | |
7548 | * | |
7549 | * Return: &IMSM_STATUS_OK or &IMSM_STATUS_ERROR. | |
7550 | * | |
7551 | * @freesize is set to meaningful value, this can be @size, or calculated | |
7552 | * max free size. | |
7553 | * super->create_offset value is modified and set appropriately in | |
7554 | * merge_extends() for further creation. | |
7555 | */ | |
7556 | static imsm_status_t imsm_get_free_size(struct intel_super *super, | |
7557 | const int raiddisks, | |
7558 | unsigned long long size, | |
7559 | const int chunk, | |
7560 | unsigned long long *freesize) | |
efb30e7f | 7561 | { |
efb30e7f DW |
7562 | struct imsm_super *mpb = super->anchor; |
7563 | struct dl *dl; | |
7564 | int i; | |
7565 | int extent_cnt; | |
7566 | struct extent *e; | |
7567 | unsigned long long maxsize; | |
7568 | unsigned long long minsize; | |
7569 | int cnt; | |
7570 | int used; | |
7571 | ||
7572 | /* find the largest common start free region of the possible disks */ | |
7573 | used = 0; | |
7574 | extent_cnt = 0; | |
7575 | cnt = 0; | |
7576 | for (dl = super->disks; dl; dl = dl->next) { | |
7577 | dl->raiddisk = -1; | |
7578 | ||
7579 | if (dl->index >= 0) | |
7580 | used++; | |
7581 | ||
7582 | /* don't activate new spares if we are orom constrained | |
7583 | * and there is already a volume active in the container | |
7584 | */ | |
7585 | if (super->orom && dl->index < 0 && mpb->num_raid_devs) | |
7586 | continue; | |
7587 | ||
05501181 | 7588 | e = get_extents(super, dl, 0); |
efb30e7f DW |
7589 | if (!e) |
7590 | continue; | |
7591 | for (i = 1; e[i-1].size; i++) | |
7592 | ; | |
7593 | dl->e = e; | |
7594 | dl->extent_cnt = i; | |
7595 | extent_cnt += i; | |
7596 | cnt++; | |
7597 | } | |
7598 | ||
7599 | maxsize = merge_extents(super, extent_cnt); | |
7600 | minsize = size; | |
7601 | if (size == 0) | |
612e59d8 CA |
7602 | /* chunk is in K */ |
7603 | minsize = chunk * 2; | |
efb30e7f | 7604 | |
6d4d9ab2 MT |
7605 | if (cnt < raiddisks || (super->orom && used && used != raiddisks) || |
7606 | maxsize < minsize || maxsize == 0) { | |
e7b84f9d | 7607 | pr_err("not enough devices with space to create array.\n"); |
6d4d9ab2 | 7608 | return IMSM_STATUS_ERROR; |
efb30e7f DW |
7609 | } |
7610 | ||
7611 | if (size == 0) { | |
7612 | size = maxsize; | |
7613 | if (chunk) { | |
612e59d8 CA |
7614 | size /= 2 * chunk; |
7615 | size *= 2 * chunk; | |
efb30e7f | 7616 | } |
f878b242 LM |
7617 | maxsize = size; |
7618 | } | |
1a1ced1e KS |
7619 | if (mpb->num_raid_devs > 0 && size && size != maxsize) |
7620 | pr_err("attempting to create a second volume with size less then remaining space.\n"); | |
efb30e7f DW |
7621 | *freesize = size; |
7622 | ||
13bcac90 AK |
7623 | dprintf("imsm: imsm_get_free_size() returns : %llu\n", size); |
7624 | ||
6d4d9ab2 | 7625 | return IMSM_STATUS_OK; |
efb30e7f DW |
7626 | } |
7627 | ||
6d4d9ab2 MT |
7628 | /** |
7629 | * autolayout_imsm() - automatically layout a new volume. | |
7630 | * @super: &intel_super pointer, not NULL. | |
7631 | * @raiddisks: number of raid disks. | |
7632 | * @size: requested size, could be 0 (means max size). | |
7633 | * @chunk: requested chunk. | |
7634 | * @freesize: pointer for returned size value. | |
7635 | * | |
7636 | * We are being asked to automatically layout a new volume based on the current | |
7637 | * contents of the container. If the parameters can be satisfied autolayout_imsm | |
7638 | * will record the disks, start offset, and will return size of the volume to | |
7639 | * be created. See imsm_get_free_size() for details. | |
7640 | * add_to_super() and getinfo_super() detect when autolayout is in progress. | |
7641 | * If first volume exists, slots are set consistently to it. | |
7642 | * | |
7643 | * Return: &IMSM_STATUS_OK on success, &IMSM_STATUS_ERROR otherwise. | |
7644 | * | |
7645 | * Disks are marked for creation via dl->raiddisk. | |
7646 | */ | |
7647 | static imsm_status_t autolayout_imsm(struct intel_super *super, | |
7648 | const int raiddisks, | |
7649 | unsigned long long size, const int chunk, | |
7650 | unsigned long long *freesize) | |
13bcac90 | 7651 | { |
6d4d9ab2 MT |
7652 | int curr_slot = 0; |
7653 | struct dl *disk; | |
7654 | int vol_cnt = super->anchor->num_raid_devs; | |
7655 | imsm_status_t rv; | |
13bcac90 | 7656 | |
6d4d9ab2 MT |
7657 | rv = imsm_get_free_size(super, raiddisks, size, chunk, freesize); |
7658 | if (rv != IMSM_STATUS_OK) | |
7659 | return IMSM_STATUS_ERROR; | |
7660 | ||
7661 | for (disk = super->disks; disk; disk = disk->next) { | |
7662 | if (!disk->e) | |
7663 | continue; | |
7664 | ||
7665 | if (curr_slot == raiddisks) | |
7666 | break; | |
7667 | ||
7668 | if (vol_cnt == 0) { | |
7669 | disk->raiddisk = curr_slot; | |
7670 | } else { | |
7671 | int _slot = get_disk_slot_in_dev(super, 0, disk->index); | |
7672 | ||
7673 | if (_slot == -1) { | |
7674 | pr_err("Disk %s is not used in first volume, aborting\n", | |
7675 | disk->devname); | |
7676 | return IMSM_STATUS_ERROR; | |
7677 | } | |
7678 | disk->raiddisk = _slot; | |
7679 | } | |
7680 | curr_slot++; | |
13bcac90 AK |
7681 | } |
7682 | ||
6d4d9ab2 | 7683 | return IMSM_STATUS_OK; |
13bcac90 AK |
7684 | } |
7685 | ||
bf5a934a | 7686 | static int validate_geometry_imsm(struct supertype *st, int level, int layout, |
c21e737b | 7687 | int raiddisks, int *chunk, unsigned long long size, |
af4348dd | 7688 | unsigned long long data_offset, |
bf5a934a | 7689 | char *dev, unsigned long long *freesize, |
5308f117 | 7690 | int consistency_policy, int verbose) |
bf5a934a DW |
7691 | { |
7692 | int fd, cfd; | |
7693 | struct mdinfo *sra; | |
20cbe8d2 | 7694 | int is_member = 0; |
bf5a934a | 7695 | |
d54559f0 LM |
7696 | /* load capability |
7697 | * if given unused devices create a container | |
bf5a934a DW |
7698 | * if given given devices in a container create a member volume |
7699 | */ | |
6f2af6a4 | 7700 | if (is_container(level)) |
bf5a934a | 7701 | /* Must be a fresh device to add to a container */ |
1f5d54a0 MT |
7702 | return validate_geometry_imsm_container(st, level, raiddisks, |
7703 | data_offset, dev, | |
7704 | freesize, verbose); | |
9587c373 | 7705 | |
06a6101c BK |
7706 | /* |
7707 | * Size is given in sectors. | |
7708 | */ | |
7709 | if (size && (size < 2048)) { | |
22dc741f | 7710 | pr_err("Given size must be greater than 1M.\n"); |
54865c30 RS |
7711 | /* Depends on algorithm in Create.c : |
7712 | * if container was given (dev == NULL) return -1, | |
7713 | * if block device was given ( dev != NULL) return 0. | |
7714 | */ | |
7715 | return dev ? -1 : 0; | |
7716 | } | |
7717 | ||
8592f29d | 7718 | if (!dev) { |
6d4d9ab2 MT |
7719 | struct intel_super *super = st->sb; |
7720 | ||
7721 | /* | |
7722 | * Autolayout mode, st->sb and freesize must be set. | |
7723 | */ | |
7724 | if (!super || !freesize) { | |
7725 | pr_vrb("freesize and superblock must be set for autolayout, aborting\n"); | |
7726 | return 1; | |
7727 | } | |
7728 | ||
7729 | if (!validate_geometry_imsm_orom(st->sb, level, layout, | |
7730 | raiddisks, chunk, size, | |
7731 | verbose)) | |
7732 | return 0; | |
7733 | ||
7734 | if (super->orom) { | |
7735 | imsm_status_t rv; | |
7736 | int count = count_volumes(super->hba, super->orom->dpa, | |
7737 | verbose); | |
7738 | if (super->orom->vphba <= count) { | |
7739 | pr_vrb("platform does not support more than %d raid volumes.\n", | |
7740 | super->orom->vphba); | |
e91a3bad | 7741 | return 0; |
ca9de185 | 7742 | } |
6d4d9ab2 MT |
7743 | |
7744 | rv = autolayout_imsm(super, raiddisks, size, *chunk, | |
7745 | freesize); | |
7746 | if (rv != IMSM_STATUS_OK) | |
7747 | return 0; | |
8592f29d N |
7748 | } |
7749 | return 1; | |
7750 | } | |
bf5a934a DW |
7751 | if (st->sb) { |
7752 | /* creating in a given container */ | |
7753 | return validate_geometry_imsm_volume(st, level, layout, | |
7754 | raiddisks, chunk, size, | |
af4348dd | 7755 | data_offset, |
bf5a934a DW |
7756 | dev, freesize, verbose); |
7757 | } | |
7758 | ||
bf5a934a DW |
7759 | /* This device needs to be a device in an 'imsm' container */ |
7760 | fd = open(dev, O_RDONLY|O_EXCL, 0); | |
4389ce73 MT |
7761 | |
7762 | if (is_fd_valid(fd)) { | |
7763 | pr_vrb("Cannot create this array on device %s\n", dev); | |
bf5a934a DW |
7764 | close(fd); |
7765 | return 0; | |
7766 | } | |
4389ce73 MT |
7767 | if (errno == EBUSY) |
7768 | fd = open(dev, O_RDONLY, 0); | |
7769 | ||
7770 | if (!is_fd_valid(fd)) { | |
7771 | pr_vrb("Cannot open %s: %s\n", dev, strerror(errno)); | |
bf5a934a DW |
7772 | return 0; |
7773 | } | |
4389ce73 | 7774 | |
bf5a934a DW |
7775 | /* Well, it is in use by someone, maybe an 'imsm' container. */ |
7776 | cfd = open_container(fd); | |
4389ce73 MT |
7777 | close_fd(&fd); |
7778 | ||
7779 | if (!is_fd_valid(cfd)) { | |
7780 | pr_vrb("Cannot use %s: It is busy\n", dev); | |
bf5a934a DW |
7781 | return 0; |
7782 | } | |
4dd2df09 | 7783 | sra = sysfs_read(cfd, NULL, GET_VERSION); |
bf5a934a | 7784 | if (sra && sra->array.major_version == -1 && |
20cbe8d2 AW |
7785 | strcmp(sra->text_version, "imsm") == 0) |
7786 | is_member = 1; | |
7787 | sysfs_free(sra); | |
7788 | if (is_member) { | |
bf5a934a DW |
7789 | /* This is a member of a imsm container. Load the container |
7790 | * and try to create a volume | |
7791 | */ | |
7792 | struct intel_super *super; | |
7793 | ||
ec50f7b6 | 7794 | if (load_super_imsm_all(st, cfd, (void **) &super, NULL, NULL, 1) == 0) { |
bf5a934a | 7795 | st->sb = super; |
4dd2df09 | 7796 | strcpy(st->container_devnm, fd2devnm(cfd)); |
bf5a934a DW |
7797 | close(cfd); |
7798 | return validate_geometry_imsm_volume(st, level, layout, | |
7799 | raiddisks, chunk, | |
af4348dd | 7800 | size, data_offset, dev, |
ecbd9e81 N |
7801 | freesize, 1) |
7802 | ? 1 : -1; | |
bf5a934a | 7803 | } |
20cbe8d2 | 7804 | } |
bf5a934a | 7805 | |
20cbe8d2 | 7806 | if (verbose) |
e7b84f9d | 7807 | pr_err("failed container membership check\n"); |
20cbe8d2 AW |
7808 | |
7809 | close(cfd); | |
7810 | return 0; | |
bf5a934a | 7811 | } |
0bd16cf2 | 7812 | |
30f58b22 | 7813 | static void default_geometry_imsm(struct supertype *st, int *level, int *layout, int *chunk) |
0bd16cf2 DJ |
7814 | { |
7815 | struct intel_super *super = st->sb; | |
7816 | ||
30f58b22 DW |
7817 | if (level && *level == UnSet) |
7818 | *level = LEVEL_CONTAINER; | |
7819 | ||
7820 | if (level && layout && *layout == UnSet) | |
7821 | *layout = imsm_level_to_layout(*level); | |
0bd16cf2 | 7822 | |
cd9d1ac7 DW |
7823 | if (chunk && (*chunk == UnSet || *chunk == 0)) |
7824 | *chunk = imsm_default_chunk(super->orom); | |
0bd16cf2 DJ |
7825 | } |
7826 | ||
33414a01 DW |
7827 | static void handle_missing(struct intel_super *super, struct imsm_dev *dev); |
7828 | ||
3364781b | 7829 | static int kill_subarray_imsm(struct supertype *st, char *subarray_id) |
33414a01 | 7830 | { |
3364781b | 7831 | /* remove the subarray currently referenced by subarray_id */ |
33414a01 DW |
7832 | __u8 i; |
7833 | struct intel_dev **dp; | |
7834 | struct intel_super *super = st->sb; | |
3364781b | 7835 | __u8 current_vol = strtoul(subarray_id, NULL, 10); |
33414a01 DW |
7836 | struct imsm_super *mpb = super->anchor; |
7837 | ||
3364781b | 7838 | if (mpb->num_raid_devs == 0) |
33414a01 | 7839 | return 2; |
33414a01 DW |
7840 | |
7841 | /* block deletions that would change the uuid of active subarrays | |
7842 | * | |
7843 | * FIXME when immutable ids are available, but note that we'll | |
7844 | * also need to fixup the invalidated/active subarray indexes in | |
7845 | * mdstat | |
7846 | */ | |
7847 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
7848 | char subarray[4]; | |
7849 | ||
7850 | if (i < current_vol) | |
7851 | continue; | |
7852 | sprintf(subarray, "%u", i); | |
4dd2df09 | 7853 | if (is_subarray_active(subarray, st->devnm)) { |
e7b84f9d N |
7854 | pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n", |
7855 | current_vol, i); | |
33414a01 DW |
7856 | |
7857 | return 2; | |
7858 | } | |
7859 | } | |
7860 | ||
7861 | if (st->update_tail) { | |
503975b9 | 7862 | struct imsm_update_kill_array *u = xmalloc(sizeof(*u)); |
33414a01 | 7863 | |
33414a01 DW |
7864 | u->type = update_kill_array; |
7865 | u->dev_idx = current_vol; | |
7866 | append_metadata_update(st, u, sizeof(*u)); | |
7867 | ||
7868 | return 0; | |
7869 | } | |
7870 | ||
7871 | for (dp = &super->devlist; *dp;) | |
7872 | if ((*dp)->index == current_vol) { | |
7873 | *dp = (*dp)->next; | |
7874 | } else { | |
7875 | handle_missing(super, (*dp)->dev); | |
7876 | if ((*dp)->index > current_vol) | |
7877 | (*dp)->index--; | |
7878 | dp = &(*dp)->next; | |
7879 | } | |
7880 | ||
7881 | /* no more raid devices, all active components are now spares, | |
7882 | * but of course failed are still failed | |
7883 | */ | |
7884 | if (--mpb->num_raid_devs == 0) { | |
7885 | struct dl *d; | |
7886 | ||
7887 | for (d = super->disks; d; d = d->next) | |
a8619d23 AK |
7888 | if (d->index > -2) |
7889 | mark_spare(d); | |
33414a01 DW |
7890 | } |
7891 | ||
7892 | super->updates_pending++; | |
7893 | ||
7894 | return 0; | |
7895 | } | |
aa534678 | 7896 | |
4345e135 MK |
7897 | /** |
7898 | * get_rwh_policy_from_update() - Get the rwh policy for update option. | |
7899 | * @update: Update option. | |
7900 | */ | |
7901 | static int get_rwh_policy_from_update(enum update_opt update) | |
19ad203e | 7902 | { |
4345e135 MK |
7903 | switch (update) { |
7904 | case UOPT_PPL: | |
19ad203e | 7905 | return RWH_MULTIPLE_DISTRIBUTED; |
4345e135 | 7906 | case UOPT_NO_PPL: |
19ad203e | 7907 | return RWH_MULTIPLE_OFF; |
4345e135 | 7908 | case UOPT_BITMAP: |
19ad203e | 7909 | return RWH_BITMAP; |
4345e135 | 7910 | case UOPT_NO_BITMAP: |
19ad203e | 7911 | return RWH_OFF; |
4345e135 MK |
7912 | default: |
7913 | break; | |
7914 | } | |
7915 | return UOPT_UNDEFINED; | |
19ad203e JR |
7916 | } |
7917 | ||
a951a4f7 | 7918 | static int update_subarray_imsm(struct supertype *st, char *subarray, |
03312b52 | 7919 | enum update_opt update, struct mddev_ident *ident) |
aa534678 DW |
7920 | { |
7921 | /* update the subarray currently referenced by ->current_vol */ | |
7922 | struct intel_super *super = st->sb; | |
7923 | struct imsm_super *mpb = super->anchor; | |
7924 | ||
03312b52 | 7925 | if (update == UOPT_NAME) { |
aa534678 | 7926 | char *name = ident->name; |
a951a4f7 N |
7927 | char *ep; |
7928 | int vol; | |
aa534678 | 7929 | |
aa534678 DW |
7930 | if (!check_name(super, name, 0)) |
7931 | return 2; | |
7932 | ||
a951a4f7 N |
7933 | vol = strtoul(subarray, &ep, 10); |
7934 | if (*ep != '\0' || vol >= super->anchor->num_raid_devs) | |
7935 | return 2; | |
7936 | ||
aa534678 | 7937 | if (st->update_tail) { |
503975b9 | 7938 | struct imsm_update_rename_array *u = xmalloc(sizeof(*u)); |
aa534678 | 7939 | |
aa534678 | 7940 | u->type = update_rename_array; |
a951a4f7 | 7941 | u->dev_idx = vol; |
618f4e6d XN |
7942 | strncpy((char *) u->name, name, MAX_RAID_SERIAL_LEN); |
7943 | u->name[MAX_RAID_SERIAL_LEN-1] = '\0'; | |
aa534678 DW |
7944 | append_metadata_update(st, u, sizeof(*u)); |
7945 | } else { | |
7946 | struct imsm_dev *dev; | |
ebad3af2 | 7947 | int i, namelen; |
aa534678 | 7948 | |
a951a4f7 | 7949 | dev = get_imsm_dev(super, vol); |
ebad3af2 JS |
7950 | memset(dev->volume, '\0', MAX_RAID_SERIAL_LEN); |
7951 | namelen = min((int)strlen(name), MAX_RAID_SERIAL_LEN); | |
7952 | memcpy(dev->volume, name, namelen); | |
aa534678 DW |
7953 | for (i = 0; i < mpb->num_raid_devs; i++) { |
7954 | dev = get_imsm_dev(super, i); | |
7955 | handle_missing(super, dev); | |
7956 | } | |
7957 | super->updates_pending++; | |
7958 | } | |
03312b52 | 7959 | } else if (get_rwh_policy_from_update(update) != UOPT_UNDEFINED) { |
e6e9dd3f AP |
7960 | int new_policy; |
7961 | char *ep; | |
7962 | int vol = strtoul(subarray, &ep, 10); | |
7963 | ||
7964 | if (*ep != '\0' || vol >= super->anchor->num_raid_devs) | |
7965 | return 2; | |
7966 | ||
03312b52 | 7967 | new_policy = get_rwh_policy_from_update(update); |
e6e9dd3f AP |
7968 | |
7969 | if (st->update_tail) { | |
7970 | struct imsm_update_rwh_policy *u = xmalloc(sizeof(*u)); | |
7971 | ||
7972 | u->type = update_rwh_policy; | |
7973 | u->dev_idx = vol; | |
7974 | u->new_policy = new_policy; | |
7975 | append_metadata_update(st, u, sizeof(*u)); | |
7976 | } else { | |
7977 | struct imsm_dev *dev; | |
7978 | ||
7979 | dev = get_imsm_dev(super, vol); | |
7980 | dev->rwh_policy = new_policy; | |
7981 | super->updates_pending++; | |
7982 | } | |
19ad203e JR |
7983 | if (new_policy == RWH_BITMAP) |
7984 | return write_init_bitmap_imsm_vol(st, vol); | |
aa534678 DW |
7985 | } else |
7986 | return 2; | |
7987 | ||
7988 | return 0; | |
7989 | } | |
bf5a934a | 7990 | |
195d1d76 | 7991 | static bool is_gen_migration(struct imsm_dev *dev) |
28bce06f | 7992 | { |
195d1d76 PP |
7993 | if (dev && dev->vol.migr_state && |
7994 | migr_type(dev) == MIGR_GEN_MIGR) | |
7995 | return true; | |
28bce06f | 7996 | |
195d1d76 | 7997 | return false; |
28bce06f AK |
7998 | } |
7999 | ||
1e5c6983 DW |
8000 | static int is_rebuilding(struct imsm_dev *dev) |
8001 | { | |
8002 | struct imsm_map *migr_map; | |
8003 | ||
8004 | if (!dev->vol.migr_state) | |
8005 | return 0; | |
8006 | ||
8007 | if (migr_type(dev) != MIGR_REBUILD) | |
8008 | return 0; | |
8009 | ||
238c0a71 | 8010 | migr_map = get_imsm_map(dev, MAP_1); |
1e5c6983 DW |
8011 | |
8012 | if (migr_map->map_state == IMSM_T_STATE_DEGRADED) | |
8013 | return 1; | |
8014 | else | |
8015 | return 0; | |
8016 | } | |
8017 | ||
6ce1fbf1 AK |
8018 | static int is_initializing(struct imsm_dev *dev) |
8019 | { | |
8020 | struct imsm_map *migr_map; | |
8021 | ||
8022 | if (!dev->vol.migr_state) | |
8023 | return 0; | |
8024 | ||
8025 | if (migr_type(dev) != MIGR_INIT) | |
8026 | return 0; | |
8027 | ||
238c0a71 | 8028 | migr_map = get_imsm_map(dev, MAP_1); |
6ce1fbf1 AK |
8029 | |
8030 | if (migr_map->map_state == IMSM_T_STATE_UNINITIALIZED) | |
8031 | return 1; | |
8032 | ||
8033 | return 0; | |
6ce1fbf1 AK |
8034 | } |
8035 | ||
c47b0ff6 AK |
8036 | static void update_recovery_start(struct intel_super *super, |
8037 | struct imsm_dev *dev, | |
8038 | struct mdinfo *array) | |
1e5c6983 DW |
8039 | { |
8040 | struct mdinfo *rebuild = NULL; | |
8041 | struct mdinfo *d; | |
8042 | __u32 units; | |
8043 | ||
8044 | if (!is_rebuilding(dev)) | |
8045 | return; | |
8046 | ||
8047 | /* Find the rebuild target, but punt on the dual rebuild case */ | |
8048 | for (d = array->devs; d; d = d->next) | |
8049 | if (d->recovery_start == 0) { | |
8050 | if (rebuild) | |
8051 | return; | |
8052 | rebuild = d; | |
8053 | } | |
8054 | ||
4363fd80 DW |
8055 | if (!rebuild) { |
8056 | /* (?) none of the disks are marked with | |
8057 | * IMSM_ORD_REBUILD, so assume they are missing and the | |
8058 | * disk_ord_tbl was not correctly updated | |
8059 | */ | |
1ade5cc1 | 8060 | dprintf("failed to locate out-of-sync disk\n"); |
4363fd80 DW |
8061 | return; |
8062 | } | |
8063 | ||
4036e7ee | 8064 | units = vol_curr_migr_unit(dev); |
c47b0ff6 | 8065 | rebuild->recovery_start = units * blocks_per_migr_unit(super, dev); |
1e5c6983 DW |
8066 | } |
8067 | ||
276d77db | 8068 | static int recover_backup_imsm(struct supertype *st, struct mdinfo *info); |
1e5c6983 | 8069 | |
00bbdbda | 8070 | static struct mdinfo *container_content_imsm(struct supertype *st, char *subarray) |
cdddbdbc | 8071 | { |
4f5bc454 DW |
8072 | /* Given a container loaded by load_super_imsm_all, |
8073 | * extract information about all the arrays into | |
8074 | * an mdinfo tree. | |
00bbdbda | 8075 | * If 'subarray' is given, just extract info about that array. |
4f5bc454 DW |
8076 | * |
8077 | * For each imsm_dev create an mdinfo, fill it in, | |
8078 | * then look for matching devices in super->disks | |
8079 | * and create appropriate device mdinfo. | |
8080 | */ | |
8081 | struct intel_super *super = st->sb; | |
949c47a0 | 8082 | struct imsm_super *mpb = super->anchor; |
4f5bc454 | 8083 | struct mdinfo *rest = NULL; |
00bbdbda | 8084 | unsigned int i; |
81219e70 | 8085 | int sb_errors = 0; |
abef11a3 AK |
8086 | struct dl *d; |
8087 | int spare_disks = 0; | |
b6180160 | 8088 | int current_vol = super->current_vol; |
cdddbdbc | 8089 | |
19482bcc AK |
8090 | /* do not assemble arrays when not all attributes are supported */ |
8091 | if (imsm_check_attributes(mpb->attributes) == 0) { | |
81219e70 | 8092 | sb_errors = 1; |
7a862a02 | 8093 | pr_err("Unsupported attributes in IMSM metadata.Arrays activation is blocked.\n"); |
19482bcc AK |
8094 | } |
8095 | ||
abef11a3 AK |
8096 | /* count spare devices, not used in maps |
8097 | */ | |
8098 | for (d = super->disks; d; d = d->next) | |
8099 | if (d->index == -1) | |
8100 | spare_disks++; | |
8101 | ||
4f5bc454 | 8102 | for (i = 0; i < mpb->num_raid_devs; i++) { |
00bbdbda N |
8103 | struct imsm_dev *dev; |
8104 | struct imsm_map *map; | |
86e3692b | 8105 | struct imsm_map *map2; |
4f5bc454 | 8106 | struct mdinfo *this; |
a6482415 | 8107 | int slot; |
a6482415 | 8108 | int chunk; |
00bbdbda | 8109 | char *ep; |
8b9cd157 | 8110 | int level; |
00bbdbda N |
8111 | |
8112 | if (subarray && | |
8113 | (i != strtoul(subarray, &ep, 10) || *ep != '\0')) | |
8114 | continue; | |
8115 | ||
8116 | dev = get_imsm_dev(super, i); | |
238c0a71 AK |
8117 | map = get_imsm_map(dev, MAP_0); |
8118 | map2 = get_imsm_map(dev, MAP_1); | |
8b9cd157 | 8119 | level = get_imsm_raid_level(map); |
4f5bc454 | 8120 | |
1ce0101c DW |
8121 | /* do not publish arrays that are in the middle of an |
8122 | * unsupported migration | |
8123 | */ | |
8124 | if (dev->vol.migr_state && | |
28bce06f | 8125 | (migr_type(dev) == MIGR_STATE_CHANGE)) { |
7a862a02 | 8126 | pr_err("cannot assemble volume '%.16s': unsupported migration in progress\n", |
1ce0101c DW |
8127 | dev->volume); |
8128 | continue; | |
8129 | } | |
2db86302 LM |
8130 | /* do not publish arrays that are not support by controller's |
8131 | * OROM/EFI | |
8132 | */ | |
1ce0101c | 8133 | |
503975b9 | 8134 | this = xmalloc(sizeof(*this)); |
4f5bc454 | 8135 | |
301406c9 | 8136 | super->current_vol = i; |
a5d85af7 | 8137 | getinfo_super_imsm_volume(st, this, NULL); |
9894ec0d | 8138 | this->next = rest; |
a6482415 | 8139 | chunk = __le16_to_cpu(map->blocks_per_strip) >> 1; |
81219e70 LM |
8140 | /* mdadm does not support all metadata features- set the bit in all arrays state */ |
8141 | if (!validate_geometry_imsm_orom(super, | |
8b9cd157 MK |
8142 | level, /* RAID level */ |
8143 | imsm_level_to_layout(level), | |
81219e70 | 8144 | map->num_members, /* raid disks */ |
fcc2c9da | 8145 | &chunk, imsm_dev_size(dev), |
81219e70 | 8146 | 1 /* verbose */)) { |
7a862a02 | 8147 | pr_err("IMSM RAID geometry validation failed. Array %s activation is blocked.\n", |
81219e70 LM |
8148 | dev->volume); |
8149 | this->array.state |= | |
8150 | (1<<MD_SB_BLOCK_CONTAINER_RESHAPE) | | |
8151 | (1<<MD_SB_BLOCK_VOLUME); | |
8152 | } | |
81219e70 LM |
8153 | |
8154 | /* if array has bad blocks, set suitable bit in all arrays state */ | |
8155 | if (sb_errors) | |
8156 | this->array.state |= | |
8157 | (1<<MD_SB_BLOCK_CONTAINER_RESHAPE) | | |
8158 | (1<<MD_SB_BLOCK_VOLUME); | |
8159 | ||
4f5bc454 | 8160 | for (slot = 0 ; slot < map->num_members; slot++) { |
1e5c6983 | 8161 | unsigned long long recovery_start; |
4f5bc454 DW |
8162 | struct mdinfo *info_d; |
8163 | struct dl *d; | |
8164 | int idx; | |
9a1608e5 | 8165 | int skip; |
7eef0453 | 8166 | __u32 ord; |
8b9cd157 | 8167 | int missing = 0; |
4f5bc454 | 8168 | |
9a1608e5 | 8169 | skip = 0; |
238c0a71 AK |
8170 | idx = get_imsm_disk_idx(dev, slot, MAP_0); |
8171 | ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X); | |
4f5bc454 DW |
8172 | for (d = super->disks; d ; d = d->next) |
8173 | if (d->index == idx) | |
0fbd635c | 8174 | break; |
4f5bc454 | 8175 | |
1e5c6983 | 8176 | recovery_start = MaxSector; |
4f5bc454 | 8177 | if (d == NULL) |
9a1608e5 | 8178 | skip = 1; |
25ed7e59 | 8179 | if (d && is_failed(&d->disk)) |
9a1608e5 | 8180 | skip = 1; |
8b9cd157 | 8181 | if (!skip && (ord & IMSM_ORD_REBUILD)) |
1e5c6983 | 8182 | recovery_start = 0; |
1e93d0d1 BK |
8183 | if (!(ord & IMSM_ORD_REBUILD)) |
8184 | this->array.working_disks++; | |
1011e834 | 8185 | /* |
9a1608e5 | 8186 | * if we skip some disks the array will be assmebled degraded; |
1e5c6983 DW |
8187 | * reset resync start to avoid a dirty-degraded |
8188 | * situation when performing the intial sync | |
9a1608e5 | 8189 | */ |
8b9cd157 MK |
8190 | if (skip) |
8191 | missing++; | |
8192 | ||
8193 | if (!(dev->vol.dirty & RAIDVOL_DIRTY)) { | |
8194 | if ((!able_to_resync(level, missing) || | |
8195 | recovery_start == 0)) | |
8196 | this->resync_start = MaxSector; | |
8b9cd157 MK |
8197 | } |
8198 | ||
9a1608e5 DW |
8199 | if (skip) |
8200 | continue; | |
4f5bc454 | 8201 | |
503975b9 | 8202 | info_d = xcalloc(1, sizeof(*info_d)); |
4f5bc454 DW |
8203 | info_d->next = this->devs; |
8204 | this->devs = info_d; | |
8205 | ||
4f5bc454 DW |
8206 | info_d->disk.number = d->index; |
8207 | info_d->disk.major = d->major; | |
8208 | info_d->disk.minor = d->minor; | |
8209 | info_d->disk.raid_disk = slot; | |
1e5c6983 | 8210 | info_d->recovery_start = recovery_start; |
86e3692b AK |
8211 | if (map2) { |
8212 | if (slot < map2->num_members) | |
8213 | info_d->disk.state = (1 << MD_DISK_ACTIVE); | |
04c3c514 AK |
8214 | else |
8215 | this->array.spare_disks++; | |
86e3692b AK |
8216 | } else { |
8217 | if (slot < map->num_members) | |
8218 | info_d->disk.state = (1 << MD_DISK_ACTIVE); | |
04c3c514 AK |
8219 | else |
8220 | this->array.spare_disks++; | |
86e3692b | 8221 | } |
4f5bc454 DW |
8222 | |
8223 | info_d->events = __le32_to_cpu(mpb->generation_num); | |
5551b113 | 8224 | info_d->data_offset = pba_of_lba0(map); |
44490938 | 8225 | info_d->component_size = calc_component_size(map, dev); |
06fb291a PB |
8226 | |
8227 | if (map->raid_level == 5) { | |
2432ce9b AP |
8228 | info_d->ppl_sector = this->ppl_sector; |
8229 | info_d->ppl_size = this->ppl_size; | |
98e96bdb AP |
8230 | if (this->consistency_policy == CONSISTENCY_POLICY_PPL && |
8231 | recovery_start == 0) | |
8232 | this->resync_start = 0; | |
06fb291a | 8233 | } |
b12796be | 8234 | |
5e46202e | 8235 | info_d->bb.supported = 1; |
b12796be TM |
8236 | get_volume_badblocks(super->bbm_log, ord_to_idx(ord), |
8237 | info_d->data_offset, | |
8238 | info_d->component_size, | |
8239 | &info_d->bb); | |
4f5bc454 | 8240 | } |
1e5c6983 | 8241 | /* now that the disk list is up-to-date fixup recovery_start */ |
c47b0ff6 | 8242 | update_recovery_start(super, dev, this); |
abef11a3 | 8243 | this->array.spare_disks += spare_disks; |
276d77db AK |
8244 | |
8245 | /* check for reshape */ | |
8246 | if (this->reshape_active == 1) | |
8247 | recover_backup_imsm(st, this); | |
9a1608e5 | 8248 | rest = this; |
4f5bc454 DW |
8249 | } |
8250 | ||
b6180160 | 8251 | super->current_vol = current_vol; |
4f5bc454 | 8252 | return rest; |
cdddbdbc DW |
8253 | } |
8254 | ||
3b451610 AK |
8255 | static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev, |
8256 | int failed, int look_in_map) | |
c2a1e7da | 8257 | { |
3b451610 AK |
8258 | struct imsm_map *map; |
8259 | ||
8260 | map = get_imsm_map(dev, look_in_map); | |
c2a1e7da DW |
8261 | |
8262 | if (!failed) | |
1011e834 | 8263 | return map->map_state == IMSM_T_STATE_UNINITIALIZED ? |
3393c6af | 8264 | IMSM_T_STATE_UNINITIALIZED : IMSM_T_STATE_NORMAL; |
c2a1e7da DW |
8265 | |
8266 | switch (get_imsm_raid_level(map)) { | |
8267 | case 0: | |
8268 | return IMSM_T_STATE_FAILED; | |
8269 | break; | |
8270 | case 1: | |
8271 | if (failed < map->num_members) | |
8272 | return IMSM_T_STATE_DEGRADED; | |
8273 | else | |
8274 | return IMSM_T_STATE_FAILED; | |
8275 | break; | |
8276 | case 10: | |
8277 | { | |
8278 | /** | |
c92a2527 DW |
8279 | * check to see if any mirrors have failed, otherwise we |
8280 | * are degraded. Even numbered slots are mirrored on | |
8281 | * slot+1 | |
c2a1e7da | 8282 | */ |
c2a1e7da | 8283 | int i; |
d9b420a5 N |
8284 | /* gcc -Os complains that this is unused */ |
8285 | int insync = insync; | |
c2a1e7da DW |
8286 | |
8287 | for (i = 0; i < map->num_members; i++) { | |
238c0a71 | 8288 | __u32 ord = get_imsm_ord_tbl_ent(dev, i, MAP_X); |
c92a2527 DW |
8289 | int idx = ord_to_idx(ord); |
8290 | struct imsm_disk *disk; | |
c2a1e7da | 8291 | |
c92a2527 | 8292 | /* reset the potential in-sync count on even-numbered |
1011e834 | 8293 | * slots. num_copies is always 2 for imsm raid10 |
c92a2527 DW |
8294 | */ |
8295 | if ((i & 1) == 0) | |
8296 | insync = 2; | |
c2a1e7da | 8297 | |
c92a2527 | 8298 | disk = get_imsm_disk(super, idx); |
25ed7e59 | 8299 | if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD) |
c92a2527 | 8300 | insync--; |
c2a1e7da | 8301 | |
c92a2527 DW |
8302 | /* no in-sync disks left in this mirror the |
8303 | * array has failed | |
8304 | */ | |
8305 | if (insync == 0) | |
8306 | return IMSM_T_STATE_FAILED; | |
c2a1e7da DW |
8307 | } |
8308 | ||
8309 | return IMSM_T_STATE_DEGRADED; | |
8310 | } | |
8311 | case 5: | |
8312 | if (failed < 2) | |
8313 | return IMSM_T_STATE_DEGRADED; | |
8314 | else | |
8315 | return IMSM_T_STATE_FAILED; | |
8316 | break; | |
8317 | default: | |
8318 | break; | |
8319 | } | |
8320 | ||
8321 | return map->map_state; | |
8322 | } | |
8323 | ||
3b451610 AK |
8324 | static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev, |
8325 | int look_in_map) | |
c2a1e7da DW |
8326 | { |
8327 | int i; | |
8328 | int failed = 0; | |
8329 | struct imsm_disk *disk; | |
d5985138 AK |
8330 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
8331 | struct imsm_map *prev = get_imsm_map(dev, MAP_1); | |
68fe4598 | 8332 | struct imsm_map *map_for_loop; |
0556e1a2 DW |
8333 | __u32 ord; |
8334 | int idx; | |
d5985138 | 8335 | int idx_1; |
c2a1e7da | 8336 | |
0556e1a2 DW |
8337 | /* at the beginning of migration we set IMSM_ORD_REBUILD on |
8338 | * disks that are being rebuilt. New failures are recorded to | |
8339 | * map[0]. So we look through all the disks we started with and | |
8340 | * see if any failures are still present, or if any new ones | |
8341 | * have arrived | |
0556e1a2 | 8342 | */ |
d5985138 AK |
8343 | map_for_loop = map; |
8344 | if (prev && (map->num_members < prev->num_members)) | |
8345 | map_for_loop = prev; | |
68fe4598 LD |
8346 | |
8347 | for (i = 0; i < map_for_loop->num_members; i++) { | |
d5985138 | 8348 | idx_1 = -255; |
238c0a71 AK |
8349 | /* when MAP_X is passed both maps failures are counted |
8350 | */ | |
d5985138 | 8351 | if (prev && |
089f9d79 JS |
8352 | (look_in_map == MAP_1 || look_in_map == MAP_X) && |
8353 | i < prev->num_members) { | |
d5985138 AK |
8354 | ord = __le32_to_cpu(prev->disk_ord_tbl[i]); |
8355 | idx_1 = ord_to_idx(ord); | |
c2a1e7da | 8356 | |
d5985138 AK |
8357 | disk = get_imsm_disk(super, idx_1); |
8358 | if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD) | |
8359 | failed++; | |
8360 | } | |
089f9d79 JS |
8361 | if ((look_in_map == MAP_0 || look_in_map == MAP_X) && |
8362 | i < map->num_members) { | |
d5985138 AK |
8363 | ord = __le32_to_cpu(map->disk_ord_tbl[i]); |
8364 | idx = ord_to_idx(ord); | |
8365 | ||
8366 | if (idx != idx_1) { | |
8367 | disk = get_imsm_disk(super, idx); | |
8368 | if (!disk || is_failed(disk) || | |
8369 | ord & IMSM_ORD_REBUILD) | |
8370 | failed++; | |
8371 | } | |
8372 | } | |
c2a1e7da DW |
8373 | } |
8374 | ||
8375 | return failed; | |
845dea95 NB |
8376 | } |
8377 | ||
97b4d0e9 | 8378 | static int imsm_open_new(struct supertype *c, struct active_array *a, |
60815698 | 8379 | int inst) |
97b4d0e9 DW |
8380 | { |
8381 | struct intel_super *super = c->sb; | |
8382 | struct imsm_super *mpb = super->anchor; | |
bbab0940 | 8383 | struct imsm_update_prealloc_bb_mem u; |
9587c373 | 8384 | |
60815698 MG |
8385 | if (inst >= mpb->num_raid_devs) { |
8386 | pr_err("subarry index %d, out of range\n", inst); | |
97b4d0e9 DW |
8387 | return -ENODEV; |
8388 | } | |
8389 | ||
60815698 MG |
8390 | dprintf("imsm: open_new %d\n", inst); |
8391 | a->info.container_member = inst; | |
bbab0940 TM |
8392 | |
8393 | u.type = update_prealloc_badblocks_mem; | |
8394 | imsm_update_metadata_locally(c, &u, sizeof(u)); | |
8395 | ||
97b4d0e9 DW |
8396 | return 0; |
8397 | } | |
8398 | ||
0c046afd DW |
8399 | static int is_resyncing(struct imsm_dev *dev) |
8400 | { | |
8401 | struct imsm_map *migr_map; | |
8402 | ||
8403 | if (!dev->vol.migr_state) | |
8404 | return 0; | |
8405 | ||
1484e727 DW |
8406 | if (migr_type(dev) == MIGR_INIT || |
8407 | migr_type(dev) == MIGR_REPAIR) | |
0c046afd DW |
8408 | return 1; |
8409 | ||
4c9bc37b AK |
8410 | if (migr_type(dev) == MIGR_GEN_MIGR) |
8411 | return 0; | |
8412 | ||
238c0a71 | 8413 | migr_map = get_imsm_map(dev, MAP_1); |
0c046afd | 8414 | |
089f9d79 JS |
8415 | if (migr_map->map_state == IMSM_T_STATE_NORMAL && |
8416 | dev->vol.migr_type != MIGR_GEN_MIGR) | |
0c046afd DW |
8417 | return 1; |
8418 | else | |
8419 | return 0; | |
8420 | } | |
8421 | ||
0556e1a2 | 8422 | /* return true if we recorded new information */ |
4c9e8c1e TM |
8423 | static int mark_failure(struct intel_super *super, |
8424 | struct imsm_dev *dev, struct imsm_disk *disk, int idx) | |
47ee5a45 | 8425 | { |
0556e1a2 DW |
8426 | __u32 ord; |
8427 | int slot; | |
8428 | struct imsm_map *map; | |
86c54047 DW |
8429 | char buf[MAX_RAID_SERIAL_LEN+3]; |
8430 | unsigned int len, shift = 0; | |
0556e1a2 DW |
8431 | |
8432 | /* new failures are always set in map[0] */ | |
238c0a71 | 8433 | map = get_imsm_map(dev, MAP_0); |
0556e1a2 DW |
8434 | |
8435 | slot = get_imsm_disk_slot(map, idx); | |
8436 | if (slot < 0) | |
8437 | return 0; | |
8438 | ||
8439 | ord = __le32_to_cpu(map->disk_ord_tbl[slot]); | |
25ed7e59 | 8440 | if (is_failed(disk) && (ord & IMSM_ORD_REBUILD)) |
0556e1a2 DW |
8441 | return 0; |
8442 | ||
7d0c5e24 LD |
8443 | memcpy(buf, disk->serial, MAX_RAID_SERIAL_LEN); |
8444 | buf[MAX_RAID_SERIAL_LEN] = '\000'; | |
8445 | strcat(buf, ":0"); | |
86c54047 DW |
8446 | if ((len = strlen(buf)) >= MAX_RAID_SERIAL_LEN) |
8447 | shift = len - MAX_RAID_SERIAL_LEN + 1; | |
167d8bb8 | 8448 | memcpy(disk->serial, &buf[shift], len + 1 - shift); |
86c54047 | 8449 | |
f2f27e63 | 8450 | disk->status |= FAILED_DISK; |
0556e1a2 | 8451 | set_imsm_ord_tbl_ent(map, slot, idx | IMSM_ORD_REBUILD); |
17788994 AK |
8452 | /* mark failures in second map if second map exists and this disk |
8453 | * in this slot. | |
8454 | * This is valid for migration, initialization and rebuild | |
8455 | */ | |
8456 | if (dev->vol.migr_state) { | |
238c0a71 | 8457 | struct imsm_map *map2 = get_imsm_map(dev, MAP_1); |
0a108d63 AK |
8458 | int slot2 = get_imsm_disk_slot(map2, idx); |
8459 | ||
089f9d79 | 8460 | if (slot2 < map2->num_members && slot2 >= 0) |
0a108d63 | 8461 | set_imsm_ord_tbl_ent(map2, slot2, |
1ace8403 AK |
8462 | idx | IMSM_ORD_REBUILD); |
8463 | } | |
d7a1fda2 MT |
8464 | if (map->failed_disk_num == 0xff || |
8465 | (!is_rebuilding(dev) && map->failed_disk_num > slot)) | |
0556e1a2 | 8466 | map->failed_disk_num = slot; |
4c9e8c1e TM |
8467 | |
8468 | clear_disk_badblocks(super->bbm_log, ord_to_idx(ord)); | |
8469 | ||
0556e1a2 DW |
8470 | return 1; |
8471 | } | |
8472 | ||
4c9e8c1e TM |
8473 | static void mark_missing(struct intel_super *super, |
8474 | struct imsm_dev *dev, struct imsm_disk *disk, int idx) | |
0556e1a2 | 8475 | { |
4c9e8c1e | 8476 | mark_failure(super, dev, disk, idx); |
0556e1a2 DW |
8477 | |
8478 | if (disk->scsi_id == __cpu_to_le32(~(__u32)0)) | |
8479 | return; | |
8480 | ||
47ee5a45 DW |
8481 | disk->scsi_id = __cpu_to_le32(~(__u32)0); |
8482 | memmove(&disk->serial[0], &disk->serial[1], MAX_RAID_SERIAL_LEN - 1); | |
8483 | } | |
8484 | ||
33414a01 DW |
8485 | static void handle_missing(struct intel_super *super, struct imsm_dev *dev) |
8486 | { | |
33414a01 | 8487 | struct dl *dl; |
33414a01 DW |
8488 | |
8489 | if (!super->missing) | |
8490 | return; | |
33414a01 | 8491 | |
79b68f1b PC |
8492 | /* When orom adds replacement for missing disk it does |
8493 | * not remove entry of missing disk, but just updates map with | |
8494 | * new added disk. So it is not enough just to test if there is | |
8495 | * any missing disk, we have to look if there are any failed disks | |
8496 | * in map to stop migration */ | |
8497 | ||
33414a01 | 8498 | dprintf("imsm: mark missing\n"); |
3d59f0c0 AK |
8499 | /* end process for initialization and rebuild only |
8500 | */ | |
195d1d76 | 8501 | if (is_gen_migration(dev) == false) { |
fb12a745 | 8502 | int failed = imsm_count_failed(super, dev, MAP_0); |
3d59f0c0 | 8503 | |
fb12a745 TM |
8504 | if (failed) { |
8505 | __u8 map_state; | |
8506 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
8507 | struct imsm_map *map1; | |
8508 | int i, ord, ord_map1; | |
8509 | int rebuilt = 1; | |
3d59f0c0 | 8510 | |
fb12a745 TM |
8511 | for (i = 0; i < map->num_members; i++) { |
8512 | ord = get_imsm_ord_tbl_ent(dev, i, MAP_0); | |
8513 | if (!(ord & IMSM_ORD_REBUILD)) | |
8514 | continue; | |
8515 | ||
8516 | map1 = get_imsm_map(dev, MAP_1); | |
8517 | if (!map1) | |
8518 | continue; | |
8519 | ||
8520 | ord_map1 = __le32_to_cpu(map1->disk_ord_tbl[i]); | |
8521 | if (ord_map1 & IMSM_ORD_REBUILD) | |
8522 | rebuilt = 0; | |
8523 | } | |
8524 | ||
8525 | if (rebuilt) { | |
8526 | map_state = imsm_check_degraded(super, dev, | |
8527 | failed, MAP_0); | |
8528 | end_migration(dev, super, map_state); | |
8529 | } | |
8530 | } | |
3d59f0c0 | 8531 | } |
33414a01 | 8532 | for (dl = super->missing; dl; dl = dl->next) |
4c9e8c1e | 8533 | mark_missing(super, dev, &dl->disk, dl->index); |
33414a01 DW |
8534 | super->updates_pending++; |
8535 | } | |
8536 | ||
f3871fdc AK |
8537 | static unsigned long long imsm_set_array_size(struct imsm_dev *dev, |
8538 | long long new_size) | |
70bdf0dc | 8539 | { |
70bdf0dc | 8540 | unsigned long long array_blocks; |
9529d343 MD |
8541 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
8542 | int used_disks = imsm_num_data_members(map); | |
70bdf0dc AK |
8543 | |
8544 | if (used_disks == 0) { | |
8545 | /* when problems occures | |
8546 | * return current array_blocks value | |
8547 | */ | |
fcc2c9da | 8548 | array_blocks = imsm_dev_size(dev); |
70bdf0dc AK |
8549 | |
8550 | return array_blocks; | |
8551 | } | |
8552 | ||
8553 | /* set array size in metadata | |
8554 | */ | |
9529d343 | 8555 | if (new_size <= 0) |
f3871fdc AK |
8556 | /* OLCE size change is caused by added disks |
8557 | */ | |
44490938 | 8558 | array_blocks = per_dev_array_size(map) * used_disks; |
9529d343 | 8559 | else |
f3871fdc AK |
8560 | /* Online Volume Size Change |
8561 | * Using available free space | |
8562 | */ | |
8563 | array_blocks = new_size; | |
70bdf0dc | 8564 | |
b53bfba6 | 8565 | array_blocks = round_size_to_mb(array_blocks, used_disks); |
fcc2c9da | 8566 | set_imsm_dev_size(dev, array_blocks); |
70bdf0dc AK |
8567 | |
8568 | return array_blocks; | |
8569 | } | |
8570 | ||
28bce06f AK |
8571 | static void imsm_set_disk(struct active_array *a, int n, int state); |
8572 | ||
0e2d1a4e AK |
8573 | static void imsm_progress_container_reshape(struct intel_super *super) |
8574 | { | |
8575 | /* if no device has a migr_state, but some device has a | |
8576 | * different number of members than the previous device, start | |
8577 | * changing the number of devices in this device to match | |
8578 | * previous. | |
8579 | */ | |
8580 | struct imsm_super *mpb = super->anchor; | |
8581 | int prev_disks = -1; | |
8582 | int i; | |
1dfaa380 | 8583 | int copy_map_size; |
0e2d1a4e AK |
8584 | |
8585 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
8586 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
238c0a71 | 8587 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
0e2d1a4e AK |
8588 | struct imsm_map *map2; |
8589 | int prev_num_members; | |
0e2d1a4e AK |
8590 | |
8591 | if (dev->vol.migr_state) | |
8592 | return; | |
8593 | ||
8594 | if (prev_disks == -1) | |
8595 | prev_disks = map->num_members; | |
8596 | if (prev_disks == map->num_members) | |
8597 | continue; | |
8598 | ||
8599 | /* OK, this array needs to enter reshape mode. | |
8600 | * i.e it needs a migr_state | |
8601 | */ | |
8602 | ||
1dfaa380 | 8603 | copy_map_size = sizeof_imsm_map(map); |
0e2d1a4e AK |
8604 | prev_num_members = map->num_members; |
8605 | map->num_members = prev_disks; | |
8606 | dev->vol.migr_state = 1; | |
4036e7ee | 8607 | set_vol_curr_migr_unit(dev, 0); |
ea672ee1 | 8608 | set_migr_type(dev, MIGR_GEN_MIGR); |
0e2d1a4e AK |
8609 | for (i = prev_num_members; |
8610 | i < map->num_members; i++) | |
8611 | set_imsm_ord_tbl_ent(map, i, i); | |
238c0a71 | 8612 | map2 = get_imsm_map(dev, MAP_1); |
0e2d1a4e | 8613 | /* Copy the current map */ |
1dfaa380 | 8614 | memcpy(map2, map, copy_map_size); |
0e2d1a4e AK |
8615 | map2->num_members = prev_num_members; |
8616 | ||
f3871fdc | 8617 | imsm_set_array_size(dev, -1); |
51d83f5d | 8618 | super->clean_migration_record_by_mdmon = 1; |
0e2d1a4e AK |
8619 | super->updates_pending++; |
8620 | } | |
8621 | } | |
8622 | ||
aad6f216 | 8623 | /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild |
0c046afd DW |
8624 | * states are handled in imsm_set_disk() with one exception, when a |
8625 | * resync is stopped due to a new failure this routine will set the | |
8626 | * 'degraded' state for the array. | |
8627 | */ | |
01f157d7 | 8628 | static int imsm_set_array_state(struct active_array *a, int consistent) |
a862209d DW |
8629 | { |
8630 | int inst = a->info.container_member; | |
8631 | struct intel_super *super = a->container->sb; | |
949c47a0 | 8632 | struct imsm_dev *dev = get_imsm_dev(super, inst); |
238c0a71 | 8633 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
3b451610 AK |
8634 | int failed = imsm_count_failed(super, dev, MAP_0); |
8635 | __u8 map_state = imsm_check_degraded(super, dev, failed, MAP_0); | |
1e5c6983 | 8636 | __u32 blocks_per_unit; |
a862209d | 8637 | |
1af97990 AK |
8638 | if (dev->vol.migr_state && |
8639 | dev->vol.migr_type == MIGR_GEN_MIGR) { | |
8640 | /* array state change is blocked due to reshape action | |
aad6f216 N |
8641 | * We might need to |
8642 | * - abort the reshape (if last_checkpoint is 0 and action!= reshape) | |
8643 | * - finish the reshape (if last_checkpoint is big and action != reshape) | |
4036e7ee | 8644 | * - update vol_curr_migr_unit |
1af97990 | 8645 | */ |
aad6f216 | 8646 | if (a->curr_action == reshape) { |
4036e7ee | 8647 | /* still reshaping, maybe update vol_curr_migr_unit */ |
633b5610 | 8648 | goto mark_checkpoint; |
aad6f216 N |
8649 | } else { |
8650 | if (a->last_checkpoint == 0 && a->prev_action == reshape) { | |
8651 | /* for some reason we aborted the reshape. | |
b66e591b AK |
8652 | * |
8653 | * disable automatic metadata rollback | |
8654 | * user action is required to recover process | |
aad6f216 | 8655 | */ |
b66e591b | 8656 | if (0) { |
238c0a71 AK |
8657 | struct imsm_map *map2 = |
8658 | get_imsm_map(dev, MAP_1); | |
8659 | dev->vol.migr_state = 0; | |
8660 | set_migr_type(dev, 0); | |
4036e7ee | 8661 | set_vol_curr_migr_unit(dev, 0); |
238c0a71 AK |
8662 | memcpy(map, map2, |
8663 | sizeof_imsm_map(map2)); | |
8664 | super->updates_pending++; | |
b66e591b | 8665 | } |
aad6f216 N |
8666 | } |
8667 | if (a->last_checkpoint >= a->info.component_size) { | |
8668 | unsigned long long array_blocks; | |
8669 | int used_disks; | |
e154ced3 | 8670 | struct mdinfo *mdi; |
aad6f216 | 8671 | |
9529d343 | 8672 | used_disks = imsm_num_data_members(map); |
d55adef9 AK |
8673 | if (used_disks > 0) { |
8674 | array_blocks = | |
44490938 | 8675 | per_dev_array_size(map) * |
d55adef9 | 8676 | used_disks; |
b53bfba6 TM |
8677 | array_blocks = |
8678 | round_size_to_mb(array_blocks, | |
8679 | used_disks); | |
d55adef9 AK |
8680 | a->info.custom_array_size = array_blocks; |
8681 | /* encourage manager to update array | |
8682 | * size | |
8683 | */ | |
e154ced3 | 8684 | |
d55adef9 | 8685 | a->check_reshape = 1; |
633b5610 | 8686 | } |
e154ced3 AK |
8687 | /* finalize online capacity expansion/reshape */ |
8688 | for (mdi = a->info.devs; mdi; mdi = mdi->next) | |
8689 | imsm_set_disk(a, | |
8690 | mdi->disk.raid_disk, | |
8691 | mdi->curr_state); | |
8692 | ||
0e2d1a4e | 8693 | imsm_progress_container_reshape(super); |
e154ced3 | 8694 | } |
aad6f216 | 8695 | } |
1af97990 AK |
8696 | } |
8697 | ||
47ee5a45 | 8698 | /* before we activate this array handle any missing disks */ |
33414a01 DW |
8699 | if (consistent == 2) |
8700 | handle_missing(super, dev); | |
1e5c6983 | 8701 | |
0c046afd | 8702 | if (consistent == 2 && |
b7941fd6 | 8703 | (!is_resync_complete(&a->info) || |
0c046afd DW |
8704 | map_state != IMSM_T_STATE_NORMAL || |
8705 | dev->vol.migr_state)) | |
01f157d7 | 8706 | consistent = 0; |
272906ef | 8707 | |
b7941fd6 | 8708 | if (is_resync_complete(&a->info)) { |
0c046afd | 8709 | /* complete intialization / resync, |
0556e1a2 DW |
8710 | * recovery and interrupted recovery is completed in |
8711 | * ->set_disk | |
0c046afd DW |
8712 | */ |
8713 | if (is_resyncing(dev)) { | |
8714 | dprintf("imsm: mark resync done\n"); | |
809da78e | 8715 | end_migration(dev, super, map_state); |
115c3803 | 8716 | super->updates_pending++; |
484240d8 | 8717 | a->last_checkpoint = 0; |
115c3803 | 8718 | } |
b9172665 AK |
8719 | } else if ((!is_resyncing(dev) && !failed) && |
8720 | (imsm_reshape_blocks_arrays_changes(super) == 0)) { | |
0c046afd | 8721 | /* mark the start of the init process if nothing is failed */ |
b7941fd6 | 8722 | dprintf("imsm: mark resync start\n"); |
1484e727 | 8723 | if (map->map_state == IMSM_T_STATE_UNINITIALIZED) |
8e59f3d8 | 8724 | migrate(dev, super, IMSM_T_STATE_NORMAL, MIGR_INIT); |
1484e727 | 8725 | else |
8e59f3d8 | 8726 | migrate(dev, super, IMSM_T_STATE_NORMAL, MIGR_REPAIR); |
3393c6af | 8727 | super->updates_pending++; |
115c3803 | 8728 | } |
a862209d | 8729 | |
633b5610 | 8730 | mark_checkpoint: |
5b83bacf AK |
8731 | /* skip checkpointing for general migration, |
8732 | * it is controlled in mdadm | |
8733 | */ | |
8734 | if (is_gen_migration(dev)) | |
8735 | goto skip_mark_checkpoint; | |
8736 | ||
4036e7ee MT |
8737 | /* check if we can update vol_curr_migr_unit from resync_start, |
8738 | * recovery_start | |
8739 | */ | |
c47b0ff6 | 8740 | blocks_per_unit = blocks_per_migr_unit(super, dev); |
4f0a7acc | 8741 | if (blocks_per_unit) { |
4036e7ee MT |
8742 | set_vol_curr_migr_unit(dev, |
8743 | a->last_checkpoint / blocks_per_unit); | |
8744 | dprintf("imsm: mark checkpoint (%llu)\n", | |
8745 | vol_curr_migr_unit(dev)); | |
8746 | super->updates_pending++; | |
1e5c6983 | 8747 | } |
f8f603f1 | 8748 | |
5b83bacf | 8749 | skip_mark_checkpoint: |
3393c6af | 8750 | /* mark dirty / clean */ |
2432ce9b AP |
8751 | if (((dev->vol.dirty & RAIDVOL_DIRTY) && consistent) || |
8752 | (!(dev->vol.dirty & RAIDVOL_DIRTY) && !consistent)) { | |
b7941fd6 | 8753 | dprintf("imsm: mark '%s'\n", consistent ? "clean" : "dirty"); |
2432ce9b AP |
8754 | if (consistent) { |
8755 | dev->vol.dirty = RAIDVOL_CLEAN; | |
8756 | } else { | |
8757 | dev->vol.dirty = RAIDVOL_DIRTY; | |
c2462068 PB |
8758 | if (dev->rwh_policy == RWH_DISTRIBUTED || |
8759 | dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED) | |
2432ce9b AP |
8760 | dev->vol.dirty |= RAIDVOL_DSRECORD_VALID; |
8761 | } | |
a862209d DW |
8762 | super->updates_pending++; |
8763 | } | |
28bce06f | 8764 | |
01f157d7 | 8765 | return consistent; |
a862209d DW |
8766 | } |
8767 | ||
6f50473f TM |
8768 | static int imsm_disk_slot_to_ord(struct active_array *a, int slot) |
8769 | { | |
8770 | int inst = a->info.container_member; | |
8771 | struct intel_super *super = a->container->sb; | |
8772 | struct imsm_dev *dev = get_imsm_dev(super, inst); | |
8773 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
8774 | ||
8775 | if (slot > map->num_members) { | |
8776 | pr_err("imsm: imsm_disk_slot_to_ord %d out of range 0..%d\n", | |
8777 | slot, map->num_members - 1); | |
8778 | return -1; | |
8779 | } | |
8780 | ||
8781 | if (slot < 0) | |
8782 | return -1; | |
8783 | ||
8784 | return get_imsm_ord_tbl_ent(dev, slot, MAP_0); | |
8785 | } | |
8786 | ||
8d45d196 | 8787 | static void imsm_set_disk(struct active_array *a, int n, int state) |
845dea95 | 8788 | { |
8d45d196 DW |
8789 | int inst = a->info.container_member; |
8790 | struct intel_super *super = a->container->sb; | |
949c47a0 | 8791 | struct imsm_dev *dev = get_imsm_dev(super, inst); |
238c0a71 | 8792 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
8d45d196 | 8793 | struct imsm_disk *disk; |
7ce05701 LD |
8794 | struct mdinfo *mdi; |
8795 | int recovery_not_finished = 0; | |
0c046afd | 8796 | int failed; |
6f50473f | 8797 | int ord; |
0c046afd | 8798 | __u8 map_state; |
fb12a745 TM |
8799 | int rebuild_done = 0; |
8800 | int i; | |
8d45d196 | 8801 | |
fb12a745 | 8802 | ord = get_imsm_ord_tbl_ent(dev, n, MAP_X); |
6f50473f | 8803 | if (ord < 0) |
8d45d196 DW |
8804 | return; |
8805 | ||
4e6e574a | 8806 | dprintf("imsm: set_disk %d:%x\n", n, state); |
b10b37b8 | 8807 | disk = get_imsm_disk(super, ord_to_idx(ord)); |
8d45d196 | 8808 | |
5802a811 | 8809 | /* check for new failures */ |
ae7d61e3 | 8810 | if (disk && (state & DS_FAULTY)) { |
4c9e8c1e | 8811 | if (mark_failure(super, dev, disk, ord_to_idx(ord))) |
0556e1a2 | 8812 | super->updates_pending++; |
8d45d196 | 8813 | } |
47ee5a45 | 8814 | |
19859edc | 8815 | /* check if in_sync */ |
0556e1a2 | 8816 | if (state & DS_INSYNC && ord & IMSM_ORD_REBUILD && is_rebuilding(dev)) { |
238c0a71 | 8817 | struct imsm_map *migr_map = get_imsm_map(dev, MAP_1); |
b10b37b8 DW |
8818 | |
8819 | set_imsm_ord_tbl_ent(migr_map, n, ord_to_idx(ord)); | |
fb12a745 | 8820 | rebuild_done = 1; |
19859edc DW |
8821 | super->updates_pending++; |
8822 | } | |
8d45d196 | 8823 | |
3b451610 AK |
8824 | failed = imsm_count_failed(super, dev, MAP_0); |
8825 | map_state = imsm_check_degraded(super, dev, failed, MAP_0); | |
5802a811 | 8826 | |
0c046afd | 8827 | /* check if recovery complete, newly degraded, or failed */ |
94002678 AK |
8828 | dprintf("imsm: Detected transition to state "); |
8829 | switch (map_state) { | |
8830 | case IMSM_T_STATE_NORMAL: /* transition to normal state */ | |
8831 | dprintf("normal: "); | |
8832 | if (is_rebuilding(dev)) { | |
1ade5cc1 | 8833 | dprintf_cont("while rebuilding"); |
7ce05701 LD |
8834 | /* check if recovery is really finished */ |
8835 | for (mdi = a->info.devs; mdi ; mdi = mdi->next) | |
8836 | if (mdi->recovery_start != MaxSector) { | |
8837 | recovery_not_finished = 1; | |
8838 | break; | |
8839 | } | |
8840 | if (recovery_not_finished) { | |
1ade5cc1 N |
8841 | dprintf_cont("\n"); |
8842 | dprintf("Rebuild has not finished yet, state not changed"); | |
7ce05701 LD |
8843 | if (a->last_checkpoint < mdi->recovery_start) { |
8844 | a->last_checkpoint = mdi->recovery_start; | |
8845 | super->updates_pending++; | |
8846 | } | |
8847 | break; | |
8848 | } | |
94002678 | 8849 | end_migration(dev, super, map_state); |
94002678 AK |
8850 | map->failed_disk_num = ~0; |
8851 | super->updates_pending++; | |
8852 | a->last_checkpoint = 0; | |
8853 | break; | |
8854 | } | |
8855 | if (is_gen_migration(dev)) { | |
1ade5cc1 | 8856 | dprintf_cont("while general migration"); |
bf2f0071 | 8857 | if (a->last_checkpoint >= a->info.component_size) |
809da78e | 8858 | end_migration(dev, super, map_state); |
94002678 AK |
8859 | else |
8860 | map->map_state = map_state; | |
28bce06f | 8861 | map->failed_disk_num = ~0; |
94002678 | 8862 | super->updates_pending++; |
bf2f0071 | 8863 | break; |
94002678 AK |
8864 | } |
8865 | break; | |
8866 | case IMSM_T_STATE_DEGRADED: /* transition to degraded state */ | |
1ade5cc1 | 8867 | dprintf_cont("degraded: "); |
089f9d79 | 8868 | if (map->map_state != map_state && !dev->vol.migr_state) { |
1ade5cc1 | 8869 | dprintf_cont("mark degraded"); |
94002678 AK |
8870 | map->map_state = map_state; |
8871 | super->updates_pending++; | |
8872 | a->last_checkpoint = 0; | |
8873 | break; | |
8874 | } | |
8875 | if (is_rebuilding(dev)) { | |
d7a1fda2 | 8876 | dprintf_cont("while rebuilding "); |
a4e96fd8 MT |
8877 | if (state & DS_FAULTY) { |
8878 | dprintf_cont("removing failed drive "); | |
d7a1fda2 MT |
8879 | if (n == map->failed_disk_num) { |
8880 | dprintf_cont("end migration"); | |
8881 | end_migration(dev, super, map_state); | |
a4e96fd8 | 8882 | a->last_checkpoint = 0; |
d7a1fda2 | 8883 | } else { |
a4e96fd8 | 8884 | dprintf_cont("fail detected during rebuild, changing map state"); |
d7a1fda2 MT |
8885 | map->map_state = map_state; |
8886 | } | |
94002678 | 8887 | super->updates_pending++; |
fb12a745 TM |
8888 | } |
8889 | ||
a4e96fd8 MT |
8890 | if (!rebuild_done) |
8891 | break; | |
8892 | ||
fb12a745 TM |
8893 | /* check if recovery is really finished */ |
8894 | for (mdi = a->info.devs; mdi ; mdi = mdi->next) | |
8895 | if (mdi->recovery_start != MaxSector) { | |
8896 | recovery_not_finished = 1; | |
8897 | break; | |
8898 | } | |
8899 | if (recovery_not_finished) { | |
8900 | dprintf_cont("\n"); | |
a4e96fd8 | 8901 | dprintf_cont("Rebuild has not finished yet"); |
fb12a745 TM |
8902 | if (a->last_checkpoint < mdi->recovery_start) { |
8903 | a->last_checkpoint = | |
8904 | mdi->recovery_start; | |
8905 | super->updates_pending++; | |
8906 | } | |
8907 | break; | |
94002678 | 8908 | } |
fb12a745 TM |
8909 | |
8910 | dprintf_cont(" Rebuild done, still degraded"); | |
a4e96fd8 MT |
8911 | end_migration(dev, super, map_state); |
8912 | a->last_checkpoint = 0; | |
8913 | super->updates_pending++; | |
fb12a745 TM |
8914 | |
8915 | for (i = 0; i < map->num_members; i++) { | |
8916 | int idx = get_imsm_ord_tbl_ent(dev, i, MAP_0); | |
8917 | ||
8918 | if (idx & IMSM_ORD_REBUILD) | |
8919 | map->failed_disk_num = i; | |
8920 | } | |
8921 | super->updates_pending++; | |
94002678 AK |
8922 | break; |
8923 | } | |
8924 | if (is_gen_migration(dev)) { | |
1ade5cc1 | 8925 | dprintf_cont("while general migration"); |
bf2f0071 | 8926 | if (a->last_checkpoint >= a->info.component_size) |
809da78e | 8927 | end_migration(dev, super, map_state); |
94002678 AK |
8928 | else { |
8929 | map->map_state = map_state; | |
3b451610 | 8930 | manage_second_map(super, dev); |
94002678 AK |
8931 | } |
8932 | super->updates_pending++; | |
bf2f0071 | 8933 | break; |
28bce06f | 8934 | } |
6ce1fbf1 | 8935 | if (is_initializing(dev)) { |
1ade5cc1 | 8936 | dprintf_cont("while initialization."); |
6ce1fbf1 AK |
8937 | map->map_state = map_state; |
8938 | super->updates_pending++; | |
8939 | break; | |
8940 | } | |
94002678 AK |
8941 | break; |
8942 | case IMSM_T_STATE_FAILED: /* transition to failed state */ | |
1ade5cc1 | 8943 | dprintf_cont("failed: "); |
94002678 | 8944 | if (is_gen_migration(dev)) { |
1ade5cc1 | 8945 | dprintf_cont("while general migration"); |
94002678 AK |
8946 | map->map_state = map_state; |
8947 | super->updates_pending++; | |
8948 | break; | |
8949 | } | |
8950 | if (map->map_state != map_state) { | |
1ade5cc1 | 8951 | dprintf_cont("mark failed"); |
94002678 AK |
8952 | end_migration(dev, super, map_state); |
8953 | super->updates_pending++; | |
8954 | a->last_checkpoint = 0; | |
8955 | break; | |
8956 | } | |
8957 | break; | |
8958 | default: | |
1ade5cc1 | 8959 | dprintf_cont("state %i\n", map_state); |
5802a811 | 8960 | } |
1ade5cc1 | 8961 | dprintf_cont("\n"); |
845dea95 NB |
8962 | } |
8963 | ||
f796af5d | 8964 | static int store_imsm_mpb(int fd, struct imsm_super *mpb) |
c2a1e7da | 8965 | { |
f796af5d | 8966 | void *buf = mpb; |
c2a1e7da DW |
8967 | __u32 mpb_size = __le32_to_cpu(mpb->mpb_size); |
8968 | unsigned long long dsize; | |
8969 | unsigned long long sectors; | |
f36a9ecd | 8970 | unsigned int sector_size; |
c2a1e7da | 8971 | |
aec01630 JS |
8972 | if (!get_dev_sector_size(fd, NULL, §or_size)) |
8973 | return 1; | |
c2a1e7da DW |
8974 | get_dev_size(fd, NULL, &dsize); |
8975 | ||
f36a9ecd | 8976 | if (mpb_size > sector_size) { |
272f648f | 8977 | /* -1 to account for anchor */ |
f36a9ecd | 8978 | sectors = mpb_sectors(mpb, sector_size) - 1; |
c2a1e7da | 8979 | |
272f648f | 8980 | /* write the extended mpb to the sectors preceeding the anchor */ |
f36a9ecd PB |
8981 | if (lseek64(fd, dsize - (sector_size * (2 + sectors)), |
8982 | SEEK_SET) < 0) | |
272f648f | 8983 | return 1; |
c2a1e7da | 8984 | |
f36a9ecd PB |
8985 | if ((unsigned long long)write(fd, buf + sector_size, |
8986 | sector_size * sectors) != sector_size * sectors) | |
272f648f DW |
8987 | return 1; |
8988 | } | |
c2a1e7da | 8989 | |
272f648f | 8990 | /* first block is stored on second to last sector of the disk */ |
f36a9ecd | 8991 | if (lseek64(fd, dsize - (sector_size * 2), SEEK_SET) < 0) |
c2a1e7da DW |
8992 | return 1; |
8993 | ||
466070ad | 8994 | if ((unsigned int)write(fd, buf, sector_size) != sector_size) |
c2a1e7da DW |
8995 | return 1; |
8996 | ||
c2a1e7da DW |
8997 | return 0; |
8998 | } | |
8999 | ||
2e735d19 | 9000 | static void imsm_sync_metadata(struct supertype *container) |
845dea95 | 9001 | { |
2e735d19 | 9002 | struct intel_super *super = container->sb; |
c2a1e7da | 9003 | |
1a64be56 | 9004 | dprintf("sync metadata: %d\n", super->updates_pending); |
c2a1e7da DW |
9005 | if (!super->updates_pending) |
9006 | return; | |
9007 | ||
36988a3d | 9008 | write_super_imsm(container, 0); |
c2a1e7da DW |
9009 | |
9010 | super->updates_pending = 0; | |
845dea95 NB |
9011 | } |
9012 | ||
272906ef DW |
9013 | static struct dl *imsm_readd(struct intel_super *super, int idx, struct active_array *a) |
9014 | { | |
9015 | struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member); | |
238c0a71 | 9016 | int i = get_imsm_disk_idx(dev, idx, MAP_X); |
272906ef DW |
9017 | struct dl *dl; |
9018 | ||
9019 | for (dl = super->disks; dl; dl = dl->next) | |
9020 | if (dl->index == i) | |
9021 | break; | |
9022 | ||
25ed7e59 | 9023 | if (dl && is_failed(&dl->disk)) |
272906ef DW |
9024 | dl = NULL; |
9025 | ||
9026 | if (dl) | |
1ade5cc1 | 9027 | dprintf("found %x:%x\n", dl->major, dl->minor); |
272906ef DW |
9028 | |
9029 | return dl; | |
9030 | } | |
9031 | ||
a20d2ba5 | 9032 | static struct dl *imsm_add_spare(struct intel_super *super, int slot, |
8ba77d32 AK |
9033 | struct active_array *a, int activate_new, |
9034 | struct mdinfo *additional_test_list) | |
272906ef DW |
9035 | { |
9036 | struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member); | |
238c0a71 | 9037 | int idx = get_imsm_disk_idx(dev, slot, MAP_X); |
a20d2ba5 DW |
9038 | struct imsm_super *mpb = super->anchor; |
9039 | struct imsm_map *map; | |
272906ef DW |
9040 | unsigned long long pos; |
9041 | struct mdinfo *d; | |
9042 | struct extent *ex; | |
a20d2ba5 | 9043 | int i, j; |
272906ef | 9044 | int found; |
569cc43f DW |
9045 | __u32 array_start = 0; |
9046 | __u32 array_end = 0; | |
272906ef | 9047 | struct dl *dl; |
6c932028 | 9048 | struct mdinfo *test_list; |
272906ef DW |
9049 | |
9050 | for (dl = super->disks; dl; dl = dl->next) { | |
9051 | /* If in this array, skip */ | |
9052 | for (d = a->info.devs ; d ; d = d->next) | |
4389ce73 | 9053 | if (is_fd_valid(d->state_fd) && |
e553d2a4 | 9054 | d->disk.major == dl->major && |
272906ef | 9055 | d->disk.minor == dl->minor) { |
8ba77d32 AK |
9056 | dprintf("%x:%x already in array\n", |
9057 | dl->major, dl->minor); | |
272906ef DW |
9058 | break; |
9059 | } | |
9060 | if (d) | |
9061 | continue; | |
6c932028 AK |
9062 | test_list = additional_test_list; |
9063 | while (test_list) { | |
9064 | if (test_list->disk.major == dl->major && | |
9065 | test_list->disk.minor == dl->minor) { | |
8ba77d32 AK |
9066 | dprintf("%x:%x already in additional test list\n", |
9067 | dl->major, dl->minor); | |
9068 | break; | |
9069 | } | |
6c932028 | 9070 | test_list = test_list->next; |
8ba77d32 | 9071 | } |
6c932028 | 9072 | if (test_list) |
8ba77d32 | 9073 | continue; |
272906ef | 9074 | |
e553d2a4 | 9075 | /* skip in use or failed drives */ |
25ed7e59 | 9076 | if (is_failed(&dl->disk) || idx == dl->index || |
df474657 DW |
9077 | dl->index == -2) { |
9078 | dprintf("%x:%x status (failed: %d index: %d)\n", | |
25ed7e59 | 9079 | dl->major, dl->minor, is_failed(&dl->disk), idx); |
9a1608e5 DW |
9080 | continue; |
9081 | } | |
9082 | ||
a20d2ba5 DW |
9083 | /* skip pure spares when we are looking for partially |
9084 | * assimilated drives | |
9085 | */ | |
9086 | if (dl->index == -1 && !activate_new) | |
9087 | continue; | |
9088 | ||
f2cc4f7d AO |
9089 | if (!drive_validate_sector_size(super, dl)) |
9090 | continue; | |
9091 | ||
272906ef | 9092 | /* Does this unused device have the requisite free space? |
a20d2ba5 | 9093 | * It needs to be able to cover all member volumes |
272906ef | 9094 | */ |
05501181 | 9095 | ex = get_extents(super, dl, 1); |
272906ef DW |
9096 | if (!ex) { |
9097 | dprintf("cannot get extents\n"); | |
9098 | continue; | |
9099 | } | |
a20d2ba5 DW |
9100 | for (i = 0; i < mpb->num_raid_devs; i++) { |
9101 | dev = get_imsm_dev(super, i); | |
238c0a71 | 9102 | map = get_imsm_map(dev, MAP_0); |
272906ef | 9103 | |
a20d2ba5 DW |
9104 | /* check if this disk is already a member of |
9105 | * this array | |
272906ef | 9106 | */ |
620b1713 | 9107 | if (get_imsm_disk_slot(map, dl->index) >= 0) |
a20d2ba5 DW |
9108 | continue; |
9109 | ||
9110 | found = 0; | |
9111 | j = 0; | |
9112 | pos = 0; | |
5551b113 | 9113 | array_start = pba_of_lba0(map); |
329c8278 | 9114 | array_end = array_start + |
44490938 | 9115 | per_dev_array_size(map) - 1; |
a20d2ba5 DW |
9116 | |
9117 | do { | |
9118 | /* check that we can start at pba_of_lba0 with | |
44490938 | 9119 | * num_data_stripes*blocks_per_stripe of space |
a20d2ba5 | 9120 | */ |
329c8278 | 9121 | if (array_start >= pos && array_end < ex[j].start) { |
a20d2ba5 DW |
9122 | found = 1; |
9123 | break; | |
9124 | } | |
9125 | pos = ex[j].start + ex[j].size; | |
9126 | j++; | |
9127 | } while (ex[j-1].size); | |
9128 | ||
9129 | if (!found) | |
272906ef | 9130 | break; |
a20d2ba5 | 9131 | } |
272906ef DW |
9132 | |
9133 | free(ex); | |
a20d2ba5 | 9134 | if (i < mpb->num_raid_devs) { |
329c8278 DW |
9135 | dprintf("%x:%x does not have %u to %u available\n", |
9136 | dl->major, dl->minor, array_start, array_end); | |
272906ef DW |
9137 | /* No room */ |
9138 | continue; | |
a20d2ba5 DW |
9139 | } |
9140 | return dl; | |
272906ef DW |
9141 | } |
9142 | ||
9143 | return dl; | |
9144 | } | |
9145 | ||
95d07a2c LM |
9146 | static int imsm_rebuild_allowed(struct supertype *cont, int dev_idx, int failed) |
9147 | { | |
9148 | struct imsm_dev *dev2; | |
9149 | struct imsm_map *map; | |
9150 | struct dl *idisk; | |
9151 | int slot; | |
9152 | int idx; | |
9153 | __u8 state; | |
9154 | ||
9155 | dev2 = get_imsm_dev(cont->sb, dev_idx); | |
756a15f3 MG |
9156 | |
9157 | state = imsm_check_degraded(cont->sb, dev2, failed, MAP_0); | |
9158 | if (state == IMSM_T_STATE_FAILED) { | |
9159 | map = get_imsm_map(dev2, MAP_0); | |
9160 | for (slot = 0; slot < map->num_members; slot++) { | |
9161 | /* | |
9162 | * Check if failed disks are deleted from intel | |
9163 | * disk list or are marked to be deleted | |
9164 | */ | |
9165 | idx = get_imsm_disk_idx(dev2, slot, MAP_X); | |
9166 | idisk = get_imsm_dl_disk(cont->sb, idx); | |
9167 | /* | |
9168 | * Do not rebuild the array if failed disks | |
9169 | * from failed sub-array are not removed from | |
9170 | * container. | |
9171 | */ | |
9172 | if (idisk && | |
9173 | is_failed(&idisk->disk) && | |
9174 | (idisk->action != DISK_REMOVE)) | |
9175 | return 0; | |
95d07a2c LM |
9176 | } |
9177 | } | |
9178 | return 1; | |
9179 | } | |
9180 | ||
88758e9d DW |
9181 | static struct mdinfo *imsm_activate_spare(struct active_array *a, |
9182 | struct metadata_update **updates) | |
9183 | { | |
9184 | /** | |
d23fe947 DW |
9185 | * Find a device with unused free space and use it to replace a |
9186 | * failed/vacant region in an array. We replace failed regions one a | |
9187 | * array at a time. The result is that a new spare disk will be added | |
9188 | * to the first failed array and after the monitor has finished | |
9189 | * propagating failures the remainder will be consumed. | |
88758e9d | 9190 | * |
d23fe947 DW |
9191 | * FIXME add a capability for mdmon to request spares from another |
9192 | * container. | |
88758e9d DW |
9193 | */ |
9194 | ||
9195 | struct intel_super *super = a->container->sb; | |
88758e9d | 9196 | int inst = a->info.container_member; |
949c47a0 | 9197 | struct imsm_dev *dev = get_imsm_dev(super, inst); |
238c0a71 | 9198 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
88758e9d DW |
9199 | int failed = a->info.array.raid_disks; |
9200 | struct mdinfo *rv = NULL; | |
9201 | struct mdinfo *d; | |
9202 | struct mdinfo *di; | |
9203 | struct metadata_update *mu; | |
9204 | struct dl *dl; | |
9205 | struct imsm_update_activate_spare *u; | |
9206 | int num_spares = 0; | |
9207 | int i; | |
95d07a2c | 9208 | int allowed; |
88758e9d | 9209 | |
4389ce73 MT |
9210 | for (d = a->info.devs ; d; d = d->next) { |
9211 | if (!is_fd_valid(d->state_fd)) | |
9212 | continue; | |
9213 | ||
9214 | if (d->curr_state & DS_FAULTY) | |
88758e9d DW |
9215 | /* wait for Removal to happen */ |
9216 | return NULL; | |
4389ce73 MT |
9217 | |
9218 | failed--; | |
88758e9d DW |
9219 | } |
9220 | ||
9221 | dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n", | |
9222 | inst, failed, a->info.array.raid_disks, a->info.array.level); | |
1af97990 | 9223 | |
e2962bfc AK |
9224 | if (imsm_reshape_blocks_arrays_changes(super)) |
9225 | return NULL; | |
1af97990 | 9226 | |
fc8ca064 AK |
9227 | /* Cannot activate another spare if rebuild is in progress already |
9228 | */ | |
9229 | if (is_rebuilding(dev)) { | |
7a862a02 | 9230 | dprintf("imsm: No spare activation allowed. Rebuild in progress already.\n"); |
fc8ca064 AK |
9231 | return NULL; |
9232 | } | |
9233 | ||
89c67882 AK |
9234 | if (a->info.array.level == 4) |
9235 | /* No repair for takeovered array | |
9236 | * imsm doesn't support raid4 | |
9237 | */ | |
9238 | return NULL; | |
9239 | ||
3b451610 AK |
9240 | if (imsm_check_degraded(super, dev, failed, MAP_0) != |
9241 | IMSM_T_STATE_DEGRADED) | |
88758e9d DW |
9242 | return NULL; |
9243 | ||
83ca7d45 AP |
9244 | if (get_imsm_map(dev, MAP_0)->map_state == IMSM_T_STATE_UNINITIALIZED) { |
9245 | dprintf("imsm: No spare activation allowed. Volume is not initialized.\n"); | |
9246 | return NULL; | |
9247 | } | |
9248 | ||
95d07a2c LM |
9249 | /* |
9250 | * If there are any failed disks check state of the other volume. | |
9251 | * Block rebuild if the another one is failed until failed disks | |
9252 | * are removed from container. | |
9253 | */ | |
9254 | if (failed) { | |
7a862a02 | 9255 | dprintf("found failed disks in %.*s, check if there anotherfailed sub-array.\n", |
c4acd1e5 | 9256 | MAX_RAID_SERIAL_LEN, dev->volume); |
95d07a2c LM |
9257 | /* check if states of the other volumes allow for rebuild */ |
9258 | for (i = 0; i < super->anchor->num_raid_devs; i++) { | |
9259 | if (i != inst) { | |
9260 | allowed = imsm_rebuild_allowed(a->container, | |
9261 | i, failed); | |
9262 | if (!allowed) | |
9263 | return NULL; | |
9264 | } | |
9265 | } | |
9266 | } | |
9267 | ||
88758e9d | 9268 | /* For each slot, if it is not working, find a spare */ |
88758e9d DW |
9269 | for (i = 0; i < a->info.array.raid_disks; i++) { |
9270 | for (d = a->info.devs ; d ; d = d->next) | |
9271 | if (d->disk.raid_disk == i) | |
9272 | break; | |
9273 | dprintf("found %d: %p %x\n", i, d, d?d->curr_state:0); | |
4389ce73 | 9274 | if (d && is_fd_valid(d->state_fd)) |
88758e9d DW |
9275 | continue; |
9276 | ||
272906ef | 9277 | /* |
a20d2ba5 DW |
9278 | * OK, this device needs recovery. Try to re-add the |
9279 | * previous occupant of this slot, if this fails see if | |
9280 | * we can continue the assimilation of a spare that was | |
9281 | * partially assimilated, finally try to activate a new | |
9282 | * spare. | |
272906ef DW |
9283 | */ |
9284 | dl = imsm_readd(super, i, a); | |
9285 | if (!dl) | |
b303fe21 | 9286 | dl = imsm_add_spare(super, i, a, 0, rv); |
a20d2ba5 | 9287 | if (!dl) |
b303fe21 | 9288 | dl = imsm_add_spare(super, i, a, 1, rv); |
272906ef DW |
9289 | if (!dl) |
9290 | continue; | |
1011e834 | 9291 | |
272906ef | 9292 | /* found a usable disk with enough space */ |
503975b9 | 9293 | di = xcalloc(1, sizeof(*di)); |
272906ef DW |
9294 | |
9295 | /* dl->index will be -1 in the case we are activating a | |
9296 | * pristine spare. imsm_process_update() will create a | |
9297 | * new index in this case. Once a disk is found to be | |
9298 | * failed in all member arrays it is kicked from the | |
9299 | * metadata | |
9300 | */ | |
9301 | di->disk.number = dl->index; | |
d23fe947 | 9302 | |
272906ef DW |
9303 | /* (ab)use di->devs to store a pointer to the device |
9304 | * we chose | |
9305 | */ | |
9306 | di->devs = (struct mdinfo *) dl; | |
9307 | ||
9308 | di->disk.raid_disk = i; | |
9309 | di->disk.major = dl->major; | |
9310 | di->disk.minor = dl->minor; | |
9311 | di->disk.state = 0; | |
d23534e4 | 9312 | di->recovery_start = 0; |
5551b113 | 9313 | di->data_offset = pba_of_lba0(map); |
272906ef DW |
9314 | di->component_size = a->info.component_size; |
9315 | di->container_member = inst; | |
5e46202e | 9316 | di->bb.supported = 1; |
2c8890e9 | 9317 | if (a->info.consistency_policy == CONSISTENCY_POLICY_PPL) { |
2432ce9b | 9318 | di->ppl_sector = get_ppl_sector(super, inst); |
c2462068 | 9319 | di->ppl_size = MULTIPLE_PPL_AREA_SIZE_IMSM >> 9; |
2432ce9b | 9320 | } |
148acb7b | 9321 | super->random = random32(); |
272906ef DW |
9322 | di->next = rv; |
9323 | rv = di; | |
9324 | num_spares++; | |
9325 | dprintf("%x:%x to be %d at %llu\n", dl->major, dl->minor, | |
9326 | i, di->data_offset); | |
88758e9d DW |
9327 | } |
9328 | ||
9329 | if (!rv) | |
9330 | /* No spares found */ | |
9331 | return rv; | |
9332 | /* Now 'rv' has a list of devices to return. | |
9333 | * Create a metadata_update record to update the | |
9334 | * disk_ord_tbl for the array | |
9335 | */ | |
503975b9 | 9336 | mu = xmalloc(sizeof(*mu)); |
1011e834 | 9337 | mu->buf = xcalloc(num_spares, |
503975b9 | 9338 | sizeof(struct imsm_update_activate_spare)); |
88758e9d | 9339 | mu->space = NULL; |
cb23f1f4 | 9340 | mu->space_list = NULL; |
88758e9d DW |
9341 | mu->len = sizeof(struct imsm_update_activate_spare) * num_spares; |
9342 | mu->next = *updates; | |
9343 | u = (struct imsm_update_activate_spare *) mu->buf; | |
9344 | ||
9345 | for (di = rv ; di ; di = di->next) { | |
9346 | u->type = update_activate_spare; | |
d23fe947 DW |
9347 | u->dl = (struct dl *) di->devs; |
9348 | di->devs = NULL; | |
88758e9d DW |
9349 | u->slot = di->disk.raid_disk; |
9350 | u->array = inst; | |
9351 | u->next = u + 1; | |
9352 | u++; | |
9353 | } | |
9354 | (u-1)->next = NULL; | |
9355 | *updates = mu; | |
9356 | ||
9357 | return rv; | |
9358 | } | |
9359 | ||
54c2c1ea | 9360 | static int disks_overlap(struct intel_super *super, int idx, struct imsm_update_create_array *u) |
8273f55e | 9361 | { |
54c2c1ea | 9362 | struct imsm_dev *dev = get_imsm_dev(super, idx); |
238c0a71 AK |
9363 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
9364 | struct imsm_map *new_map = get_imsm_map(&u->dev, MAP_0); | |
54c2c1ea DW |
9365 | struct disk_info *inf = get_disk_info(u); |
9366 | struct imsm_disk *disk; | |
8273f55e DW |
9367 | int i; |
9368 | int j; | |
8273f55e | 9369 | |
54c2c1ea | 9370 | for (i = 0; i < map->num_members; i++) { |
238c0a71 | 9371 | disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i, MAP_X)); |
54c2c1ea DW |
9372 | for (j = 0; j < new_map->num_members; j++) |
9373 | if (serialcmp(disk->serial, inf[j].serial) == 0) | |
8273f55e DW |
9374 | return 1; |
9375 | } | |
9376 | ||
9377 | return 0; | |
9378 | } | |
9379 | ||
1a64be56 LM |
9380 | static struct dl *get_disk_super(struct intel_super *super, int major, int minor) |
9381 | { | |
594dc1b8 JS |
9382 | struct dl *dl; |
9383 | ||
1a64be56 | 9384 | for (dl = super->disks; dl; dl = dl->next) |
089f9d79 | 9385 | if (dl->major == major && dl->minor == minor) |
1a64be56 LM |
9386 | return dl; |
9387 | return NULL; | |
9388 | } | |
9389 | ||
9390 | static int remove_disk_super(struct intel_super *super, int major, int minor) | |
9391 | { | |
594dc1b8 | 9392 | struct dl *prev; |
1a64be56 LM |
9393 | struct dl *dl; |
9394 | ||
9395 | prev = NULL; | |
9396 | for (dl = super->disks; dl; dl = dl->next) { | |
089f9d79 | 9397 | if (dl->major == major && dl->minor == minor) { |
1a64be56 LM |
9398 | /* remove */ |
9399 | if (prev) | |
9400 | prev->next = dl->next; | |
9401 | else | |
9402 | super->disks = dl->next; | |
9403 | dl->next = NULL; | |
3a85bf0e | 9404 | __free_imsm_disk(dl, 1); |
1ade5cc1 | 9405 | dprintf("removed %x:%x\n", major, minor); |
1a64be56 LM |
9406 | break; |
9407 | } | |
9408 | prev = dl; | |
9409 | } | |
9410 | return 0; | |
9411 | } | |
9412 | ||
f21e18ca | 9413 | static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index); |
ae6aad82 | 9414 | |
1a64be56 LM |
9415 | static int add_remove_disk_update(struct intel_super *super) |
9416 | { | |
9417 | int check_degraded = 0; | |
594dc1b8 JS |
9418 | struct dl *disk; |
9419 | ||
1a64be56 LM |
9420 | /* add/remove some spares to/from the metadata/contrainer */ |
9421 | while (super->disk_mgmt_list) { | |
9422 | struct dl *disk_cfg; | |
9423 | ||
9424 | disk_cfg = super->disk_mgmt_list; | |
9425 | super->disk_mgmt_list = disk_cfg->next; | |
9426 | disk_cfg->next = NULL; | |
9427 | ||
9428 | if (disk_cfg->action == DISK_ADD) { | |
9429 | disk_cfg->next = super->disks; | |
9430 | super->disks = disk_cfg; | |
9431 | check_degraded = 1; | |
1ade5cc1 N |
9432 | dprintf("added %x:%x\n", |
9433 | disk_cfg->major, disk_cfg->minor); | |
1a64be56 LM |
9434 | } else if (disk_cfg->action == DISK_REMOVE) { |
9435 | dprintf("Disk remove action processed: %x.%x\n", | |
9436 | disk_cfg->major, disk_cfg->minor); | |
9437 | disk = get_disk_super(super, | |
9438 | disk_cfg->major, | |
9439 | disk_cfg->minor); | |
9440 | if (disk) { | |
9441 | /* store action status */ | |
9442 | disk->action = DISK_REMOVE; | |
9443 | /* remove spare disks only */ | |
9444 | if (disk->index == -1) { | |
9445 | remove_disk_super(super, | |
9446 | disk_cfg->major, | |
9447 | disk_cfg->minor); | |
91c97c54 MT |
9448 | } else { |
9449 | disk_cfg->fd = disk->fd; | |
9450 | disk->fd = -1; | |
1a64be56 LM |
9451 | } |
9452 | } | |
9453 | /* release allocate disk structure */ | |
3a85bf0e | 9454 | __free_imsm_disk(disk_cfg, 1); |
1a64be56 LM |
9455 | } |
9456 | } | |
9457 | return check_degraded; | |
9458 | } | |
9459 | ||
a29911da PC |
9460 | static int apply_reshape_migration_update(struct imsm_update_reshape_migration *u, |
9461 | struct intel_super *super, | |
9462 | void ***space_list) | |
9463 | { | |
9464 | struct intel_dev *id; | |
9465 | void **tofree = NULL; | |
9466 | int ret_val = 0; | |
9467 | ||
1ade5cc1 | 9468 | dprintf("(enter)\n"); |
089f9d79 | 9469 | if (u->subdev < 0 || u->subdev > 1) { |
a29911da PC |
9470 | dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev); |
9471 | return ret_val; | |
9472 | } | |
089f9d79 | 9473 | if (space_list == NULL || *space_list == NULL) { |
a29911da PC |
9474 | dprintf("imsm: Error: Memory is not allocated\n"); |
9475 | return ret_val; | |
9476 | } | |
9477 | ||
9478 | for (id = super->devlist ; id; id = id->next) { | |
9479 | if (id->index == (unsigned)u->subdev) { | |
9480 | struct imsm_dev *dev = get_imsm_dev(super, u->subdev); | |
9481 | struct imsm_map *map; | |
9482 | struct imsm_dev *new_dev = | |
9483 | (struct imsm_dev *)*space_list; | |
238c0a71 | 9484 | struct imsm_map *migr_map = get_imsm_map(dev, MAP_1); |
a29911da PC |
9485 | int to_state; |
9486 | struct dl *new_disk; | |
9487 | ||
9488 | if (new_dev == NULL) | |
9489 | return ret_val; | |
9490 | *space_list = **space_list; | |
9491 | memcpy(new_dev, dev, sizeof_imsm_dev(dev, 0)); | |
238c0a71 | 9492 | map = get_imsm_map(new_dev, MAP_0); |
a29911da PC |
9493 | if (migr_map) { |
9494 | dprintf("imsm: Error: migration in progress"); | |
9495 | return ret_val; | |
9496 | } | |
9497 | ||
9498 | to_state = map->map_state; | |
9499 | if ((u->new_level == 5) && (map->raid_level == 0)) { | |
9500 | map->num_members++; | |
9501 | /* this should not happen */ | |
9502 | if (u->new_disks[0] < 0) { | |
9503 | map->failed_disk_num = | |
9504 | map->num_members - 1; | |
9505 | to_state = IMSM_T_STATE_DEGRADED; | |
9506 | } else | |
9507 | to_state = IMSM_T_STATE_NORMAL; | |
9508 | } | |
8e59f3d8 | 9509 | migrate(new_dev, super, to_state, MIGR_GEN_MIGR); |
a29911da PC |
9510 | if (u->new_level > -1) |
9511 | map->raid_level = u->new_level; | |
238c0a71 | 9512 | migr_map = get_imsm_map(new_dev, MAP_1); |
a29911da PC |
9513 | if ((u->new_level == 5) && |
9514 | (migr_map->raid_level == 0)) { | |
9515 | int ord = map->num_members - 1; | |
9516 | migr_map->num_members--; | |
9517 | if (u->new_disks[0] < 0) | |
9518 | ord |= IMSM_ORD_REBUILD; | |
9519 | set_imsm_ord_tbl_ent(map, | |
9520 | map->num_members - 1, | |
9521 | ord); | |
9522 | } | |
9523 | id->dev = new_dev; | |
9524 | tofree = (void **)dev; | |
9525 | ||
4bba0439 PC |
9526 | /* update chunk size |
9527 | */ | |
06fb291a | 9528 | if (u->new_chunksize > 0) { |
9529d343 MD |
9529 | struct imsm_map *dest_map = |
9530 | get_imsm_map(dev, MAP_0); | |
06fb291a | 9531 | int used_disks = |
9529d343 | 9532 | imsm_num_data_members(dest_map); |
06fb291a PB |
9533 | |
9534 | if (used_disks == 0) | |
9535 | return ret_val; | |
9536 | ||
4bba0439 PC |
9537 | map->blocks_per_strip = |
9538 | __cpu_to_le16(u->new_chunksize * 2); | |
1c275381 | 9539 | update_num_data_stripes(map, imsm_dev_size(dev)); |
06fb291a | 9540 | } |
4bba0439 | 9541 | |
44490938 MD |
9542 | /* ensure blocks_per_member has valid value |
9543 | */ | |
9544 | set_blocks_per_member(map, | |
9545 | per_dev_array_size(map) + | |
9546 | NUM_BLOCKS_DIRTY_STRIPE_REGION); | |
9547 | ||
a29911da PC |
9548 | /* add disk |
9549 | */ | |
089f9d79 JS |
9550 | if (u->new_level != 5 || migr_map->raid_level != 0 || |
9551 | migr_map->raid_level == map->raid_level) | |
a29911da PC |
9552 | goto skip_disk_add; |
9553 | ||
9554 | if (u->new_disks[0] >= 0) { | |
9555 | /* use passes spare | |
9556 | */ | |
9557 | new_disk = get_disk_super(super, | |
9558 | major(u->new_disks[0]), | |
9559 | minor(u->new_disks[0])); | |
7a862a02 | 9560 | dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n", |
a29911da PC |
9561 | major(u->new_disks[0]), |
9562 | minor(u->new_disks[0]), | |
9563 | new_disk, new_disk->index); | |
9564 | if (new_disk == NULL) | |
9565 | goto error_disk_add; | |
9566 | ||
9567 | new_disk->index = map->num_members - 1; | |
9568 | /* slot to fill in autolayout | |
9569 | */ | |
9570 | new_disk->raiddisk = new_disk->index; | |
9571 | new_disk->disk.status |= CONFIGURED_DISK; | |
9572 | new_disk->disk.status &= ~SPARE_DISK; | |
9573 | } else | |
9574 | goto error_disk_add; | |
9575 | ||
9576 | skip_disk_add: | |
9577 | *tofree = *space_list; | |
9578 | /* calculate new size | |
9579 | */ | |
f3871fdc | 9580 | imsm_set_array_size(new_dev, -1); |
a29911da PC |
9581 | |
9582 | ret_val = 1; | |
9583 | } | |
9584 | } | |
9585 | ||
9586 | if (tofree) | |
9587 | *space_list = tofree; | |
9588 | return ret_val; | |
9589 | ||
9590 | error_disk_add: | |
9591 | dprintf("Error: imsm: Cannot find disk.\n"); | |
9592 | return ret_val; | |
9593 | } | |
9594 | ||
f3871fdc AK |
9595 | static int apply_size_change_update(struct imsm_update_size_change *u, |
9596 | struct intel_super *super) | |
9597 | { | |
9598 | struct intel_dev *id; | |
9599 | int ret_val = 0; | |
9600 | ||
1ade5cc1 | 9601 | dprintf("(enter)\n"); |
089f9d79 | 9602 | if (u->subdev < 0 || u->subdev > 1) { |
f3871fdc AK |
9603 | dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev); |
9604 | return ret_val; | |
9605 | } | |
9606 | ||
9607 | for (id = super->devlist ; id; id = id->next) { | |
9608 | if (id->index == (unsigned)u->subdev) { | |
9609 | struct imsm_dev *dev = get_imsm_dev(super, u->subdev); | |
9610 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
9529d343 | 9611 | int used_disks = imsm_num_data_members(map); |
f3871fdc | 9612 | unsigned long long blocks_per_member; |
44490938 MD |
9613 | unsigned long long new_size_per_disk; |
9614 | ||
9615 | if (used_disks == 0) | |
9616 | return 0; | |
f3871fdc AK |
9617 | |
9618 | /* calculate new size | |
9619 | */ | |
44490938 MD |
9620 | new_size_per_disk = u->new_size / used_disks; |
9621 | blocks_per_member = new_size_per_disk + | |
9622 | NUM_BLOCKS_DIRTY_STRIPE_REGION; | |
f3871fdc | 9623 | |
1c275381 MT |
9624 | imsm_set_array_size(dev, u->new_size); |
9625 | set_blocks_per_member(map, blocks_per_member); | |
9626 | update_num_data_stripes(map, u->new_size); | |
f3871fdc AK |
9627 | ret_val = 1; |
9628 | break; | |
9629 | } | |
9630 | } | |
9631 | ||
9632 | return ret_val; | |
9633 | } | |
9634 | ||
69d40de4 JR |
9635 | static int prepare_spare_to_activate(struct supertype *st, |
9636 | struct imsm_update_activate_spare *u) | |
9637 | { | |
9638 | struct intel_super *super = st->sb; | |
9639 | int prev_current_vol = super->current_vol; | |
9640 | struct active_array *a; | |
9641 | int ret = 1; | |
9642 | ||
9643 | for (a = st->arrays; a; a = a->next) | |
9644 | /* | |
9645 | * Additional initialization (adding bitmap header, filling | |
9646 | * the bitmap area with '1's to force initial rebuild for a whole | |
9647 | * data-area) is required when adding the spare to the volume | |
9648 | * with write-intent bitmap. | |
9649 | */ | |
9650 | if (a->info.container_member == u->array && | |
9651 | a->info.consistency_policy == CONSISTENCY_POLICY_BITMAP) { | |
9652 | struct dl *dl; | |
9653 | ||
9654 | for (dl = super->disks; dl; dl = dl->next) | |
9655 | if (dl == u->dl) | |
9656 | break; | |
9657 | if (!dl) | |
9658 | break; | |
9659 | ||
9660 | super->current_vol = u->array; | |
9661 | if (st->ss->write_bitmap(st, dl->fd, NoUpdate)) | |
9662 | ret = 0; | |
9663 | super->current_vol = prev_current_vol; | |
9664 | } | |
9665 | return ret; | |
9666 | } | |
9667 | ||
061d7da3 | 9668 | static int apply_update_activate_spare(struct imsm_update_activate_spare *u, |
ca9de185 | 9669 | struct intel_super *super, |
061d7da3 LO |
9670 | struct active_array *active_array) |
9671 | { | |
9672 | struct imsm_super *mpb = super->anchor; | |
9673 | struct imsm_dev *dev = get_imsm_dev(super, u->array); | |
238c0a71 | 9674 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
061d7da3 LO |
9675 | struct imsm_map *migr_map; |
9676 | struct active_array *a; | |
9677 | struct imsm_disk *disk; | |
9678 | __u8 to_state; | |
9679 | struct dl *dl; | |
9680 | unsigned int found; | |
9681 | int failed; | |
5961eeec | 9682 | int victim; |
061d7da3 | 9683 | int i; |
5961eeec | 9684 | int second_map_created = 0; |
061d7da3 | 9685 | |
5961eeec | 9686 | for (; u; u = u->next) { |
238c0a71 | 9687 | victim = get_imsm_disk_idx(dev, u->slot, MAP_X); |
061d7da3 | 9688 | |
5961eeec | 9689 | if (victim < 0) |
9690 | return 0; | |
061d7da3 | 9691 | |
5961eeec | 9692 | for (dl = super->disks; dl; dl = dl->next) |
9693 | if (dl == u->dl) | |
9694 | break; | |
061d7da3 | 9695 | |
5961eeec | 9696 | if (!dl) { |
7a862a02 | 9697 | pr_err("error: imsm_activate_spare passed an unknown disk (index: %d)\n", |
5961eeec | 9698 | u->dl->index); |
9699 | return 0; | |
9700 | } | |
061d7da3 | 9701 | |
5961eeec | 9702 | /* count failures (excluding rebuilds and the victim) |
9703 | * to determine map[0] state | |
9704 | */ | |
9705 | failed = 0; | |
9706 | for (i = 0; i < map->num_members; i++) { | |
9707 | if (i == u->slot) | |
9708 | continue; | |
9709 | disk = get_imsm_disk(super, | |
238c0a71 | 9710 | get_imsm_disk_idx(dev, i, MAP_X)); |
5961eeec | 9711 | if (!disk || is_failed(disk)) |
9712 | failed++; | |
9713 | } | |
061d7da3 | 9714 | |
5961eeec | 9715 | /* adding a pristine spare, assign a new index */ |
9716 | if (dl->index < 0) { | |
9717 | dl->index = super->anchor->num_disks; | |
9718 | super->anchor->num_disks++; | |
9719 | } | |
9720 | disk = &dl->disk; | |
9721 | disk->status |= CONFIGURED_DISK; | |
9722 | disk->status &= ~SPARE_DISK; | |
9723 | ||
9724 | /* mark rebuild */ | |
238c0a71 | 9725 | to_state = imsm_check_degraded(super, dev, failed, MAP_0); |
5961eeec | 9726 | if (!second_map_created) { |
9727 | second_map_created = 1; | |
9728 | map->map_state = IMSM_T_STATE_DEGRADED; | |
9729 | migrate(dev, super, to_state, MIGR_REBUILD); | |
9730 | } else | |
9731 | map->map_state = to_state; | |
238c0a71 | 9732 | migr_map = get_imsm_map(dev, MAP_1); |
5961eeec | 9733 | set_imsm_ord_tbl_ent(map, u->slot, dl->index); |
9734 | set_imsm_ord_tbl_ent(migr_map, u->slot, | |
9735 | dl->index | IMSM_ORD_REBUILD); | |
9736 | ||
9737 | /* update the family_num to mark a new container | |
9738 | * generation, being careful to record the existing | |
9739 | * family_num in orig_family_num to clean up after | |
9740 | * earlier mdadm versions that neglected to set it. | |
9741 | */ | |
9742 | if (mpb->orig_family_num == 0) | |
9743 | mpb->orig_family_num = mpb->family_num; | |
9744 | mpb->family_num += super->random; | |
9745 | ||
9746 | /* count arrays using the victim in the metadata */ | |
9747 | found = 0; | |
9748 | for (a = active_array; a ; a = a->next) { | |
76c152ca | 9749 | int dev_idx = a->info.container_member; |
061d7da3 | 9750 | |
76c152ca | 9751 | if (get_disk_slot_in_dev(super, dev_idx, victim) >= 0) |
5961eeec | 9752 | found++; |
9753 | } | |
061d7da3 | 9754 | |
5961eeec | 9755 | /* delete the victim if it is no longer being |
9756 | * utilized anywhere | |
061d7da3 | 9757 | */ |
5961eeec | 9758 | if (!found) { |
9759 | struct dl **dlp; | |
061d7da3 | 9760 | |
5961eeec | 9761 | /* We know that 'manager' isn't touching anything, |
9762 | * so it is safe to delete | |
9763 | */ | |
9764 | for (dlp = &super->disks; *dlp; dlp = &(*dlp)->next) | |
061d7da3 LO |
9765 | if ((*dlp)->index == victim) |
9766 | break; | |
5961eeec | 9767 | |
9768 | /* victim may be on the missing list */ | |
9769 | if (!*dlp) | |
9770 | for (dlp = &super->missing; *dlp; | |
9771 | dlp = &(*dlp)->next) | |
9772 | if ((*dlp)->index == victim) | |
9773 | break; | |
9774 | imsm_delete(super, dlp, victim); | |
9775 | } | |
061d7da3 LO |
9776 | } |
9777 | ||
9778 | return 1; | |
9779 | } | |
a29911da | 9780 | |
2e5dc010 N |
9781 | static int apply_reshape_container_disks_update(struct imsm_update_reshape *u, |
9782 | struct intel_super *super, | |
9783 | void ***space_list) | |
9784 | { | |
9785 | struct dl *new_disk; | |
9786 | struct intel_dev *id; | |
9787 | int i; | |
9788 | int delta_disks = u->new_raid_disks - u->old_raid_disks; | |
ee4beede | 9789 | int disk_count = u->old_raid_disks; |
2e5dc010 N |
9790 | void **tofree = NULL; |
9791 | int devices_to_reshape = 1; | |
9792 | struct imsm_super *mpb = super->anchor; | |
9793 | int ret_val = 0; | |
d098291a | 9794 | unsigned int dev_id; |
2e5dc010 | 9795 | |
1ade5cc1 | 9796 | dprintf("(enter)\n"); |
2e5dc010 N |
9797 | |
9798 | /* enable spares to use in array */ | |
9799 | for (i = 0; i < delta_disks; i++) { | |
9800 | new_disk = get_disk_super(super, | |
9801 | major(u->new_disks[i]), | |
9802 | minor(u->new_disks[i])); | |
7a862a02 | 9803 | dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n", |
2e5dc010 N |
9804 | major(u->new_disks[i]), minor(u->new_disks[i]), |
9805 | new_disk, new_disk->index); | |
089f9d79 JS |
9806 | if (new_disk == NULL || |
9807 | (new_disk->index >= 0 && | |
9808 | new_disk->index < u->old_raid_disks)) | |
2e5dc010 | 9809 | goto update_reshape_exit; |
ee4beede | 9810 | new_disk->index = disk_count++; |
2e5dc010 N |
9811 | /* slot to fill in autolayout |
9812 | */ | |
9813 | new_disk->raiddisk = new_disk->index; | |
9814 | new_disk->disk.status |= | |
9815 | CONFIGURED_DISK; | |
9816 | new_disk->disk.status &= ~SPARE_DISK; | |
9817 | } | |
9818 | ||
ed7333bd AK |
9819 | dprintf("imsm: volume set mpb->num_raid_devs = %i\n", |
9820 | mpb->num_raid_devs); | |
2e5dc010 N |
9821 | /* manage changes in volume |
9822 | */ | |
d098291a | 9823 | for (dev_id = 0; dev_id < mpb->num_raid_devs; dev_id++) { |
2e5dc010 N |
9824 | void **sp = *space_list; |
9825 | struct imsm_dev *newdev; | |
9826 | struct imsm_map *newmap, *oldmap; | |
9827 | ||
d098291a AK |
9828 | for (id = super->devlist ; id; id = id->next) { |
9829 | if (id->index == dev_id) | |
9830 | break; | |
9831 | } | |
9832 | if (id == NULL) | |
9833 | break; | |
2e5dc010 N |
9834 | if (!sp) |
9835 | continue; | |
9836 | *space_list = *sp; | |
9837 | newdev = (void*)sp; | |
9838 | /* Copy the dev, but not (all of) the map */ | |
9839 | memcpy(newdev, id->dev, sizeof(*newdev)); | |
238c0a71 AK |
9840 | oldmap = get_imsm_map(id->dev, MAP_0); |
9841 | newmap = get_imsm_map(newdev, MAP_0); | |
2e5dc010 N |
9842 | /* Copy the current map */ |
9843 | memcpy(newmap, oldmap, sizeof_imsm_map(oldmap)); | |
9844 | /* update one device only | |
9845 | */ | |
9846 | if (devices_to_reshape) { | |
ed7333bd AK |
9847 | dprintf("imsm: modifying subdev: %i\n", |
9848 | id->index); | |
2e5dc010 N |
9849 | devices_to_reshape--; |
9850 | newdev->vol.migr_state = 1; | |
4036e7ee | 9851 | set_vol_curr_migr_unit(newdev, 0); |
ea672ee1 | 9852 | set_migr_type(newdev, MIGR_GEN_MIGR); |
2e5dc010 N |
9853 | newmap->num_members = u->new_raid_disks; |
9854 | for (i = 0; i < delta_disks; i++) { | |
9855 | set_imsm_ord_tbl_ent(newmap, | |
9856 | u->old_raid_disks + i, | |
9857 | u->old_raid_disks + i); | |
9858 | } | |
9859 | /* New map is correct, now need to save old map | |
9860 | */ | |
238c0a71 | 9861 | newmap = get_imsm_map(newdev, MAP_1); |
2e5dc010 N |
9862 | memcpy(newmap, oldmap, sizeof_imsm_map(oldmap)); |
9863 | ||
f3871fdc | 9864 | imsm_set_array_size(newdev, -1); |
2e5dc010 N |
9865 | } |
9866 | ||
9867 | sp = (void **)id->dev; | |
9868 | id->dev = newdev; | |
9869 | *sp = tofree; | |
9870 | tofree = sp; | |
8e59f3d8 AK |
9871 | |
9872 | /* Clear migration record */ | |
9873 | memset(super->migr_rec, 0, sizeof(struct migr_record)); | |
2e5dc010 | 9874 | } |
819bc634 AK |
9875 | if (tofree) |
9876 | *space_list = tofree; | |
2e5dc010 N |
9877 | ret_val = 1; |
9878 | ||
9879 | update_reshape_exit: | |
9880 | ||
9881 | return ret_val; | |
9882 | } | |
9883 | ||
bb025c2f | 9884 | static int apply_takeover_update(struct imsm_update_takeover *u, |
8ca6df95 KW |
9885 | struct intel_super *super, |
9886 | void ***space_list) | |
bb025c2f KW |
9887 | { |
9888 | struct imsm_dev *dev = NULL; | |
8ca6df95 KW |
9889 | struct intel_dev *dv; |
9890 | struct imsm_dev *dev_new; | |
bb025c2f KW |
9891 | struct imsm_map *map; |
9892 | struct dl *dm, *du; | |
8ca6df95 | 9893 | int i; |
bb025c2f KW |
9894 | |
9895 | for (dv = super->devlist; dv; dv = dv->next) | |
9896 | if (dv->index == (unsigned int)u->subarray) { | |
9897 | dev = dv->dev; | |
9898 | break; | |
9899 | } | |
9900 | ||
9901 | if (dev == NULL) | |
9902 | return 0; | |
9903 | ||
238c0a71 | 9904 | map = get_imsm_map(dev, MAP_0); |
bb025c2f KW |
9905 | |
9906 | if (u->direction == R10_TO_R0) { | |
43d5ec18 | 9907 | /* Number of failed disks must be half of initial disk number */ |
3b451610 AK |
9908 | if (imsm_count_failed(super, dev, MAP_0) != |
9909 | (map->num_members / 2)) | |
43d5ec18 KW |
9910 | return 0; |
9911 | ||
bb025c2f KW |
9912 | /* iterate through devices to mark removed disks as spare */ |
9913 | for (dm = super->disks; dm; dm = dm->next) { | |
9914 | if (dm->disk.status & FAILED_DISK) { | |
9915 | int idx = dm->index; | |
9916 | /* update indexes on the disk list */ | |
9917 | /* FIXME this loop-with-the-loop looks wrong, I'm not convinced | |
9918 | the index values will end up being correct.... NB */ | |
9919 | for (du = super->disks; du; du = du->next) | |
9920 | if (du->index > idx) | |
9921 | du->index--; | |
9922 | /* mark as spare disk */ | |
a8619d23 | 9923 | mark_spare(dm); |
bb025c2f KW |
9924 | } |
9925 | } | |
bb025c2f | 9926 | /* update map */ |
1c275381 | 9927 | map->num_members /= map->num_domains; |
bb025c2f | 9928 | map->map_state = IMSM_T_STATE_NORMAL; |
bb025c2f | 9929 | map->raid_level = 0; |
1c275381 MT |
9930 | set_num_domains(map); |
9931 | update_num_data_stripes(map, imsm_dev_size(dev)); | |
bb025c2f KW |
9932 | map->failed_disk_num = -1; |
9933 | } | |
9934 | ||
8ca6df95 KW |
9935 | if (u->direction == R0_TO_R10) { |
9936 | void **space; | |
4a353e6e | 9937 | |
8ca6df95 KW |
9938 | /* update slots in current disk list */ |
9939 | for (dm = super->disks; dm; dm = dm->next) { | |
9940 | if (dm->index >= 0) | |
9941 | dm->index *= 2; | |
9942 | } | |
9943 | /* create new *missing* disks */ | |
9944 | for (i = 0; i < map->num_members; i++) { | |
9945 | space = *space_list; | |
9946 | if (!space) | |
9947 | continue; | |
9948 | *space_list = *space; | |
9949 | du = (void *)space; | |
9950 | memcpy(du, super->disks, sizeof(*du)); | |
8ca6df95 KW |
9951 | du->fd = -1; |
9952 | du->minor = 0; | |
9953 | du->major = 0; | |
9954 | du->index = (i * 2) + 1; | |
9955 | sprintf((char *)du->disk.serial, | |
9956 | " MISSING_%d", du->index); | |
9957 | sprintf((char *)du->serial, | |
9958 | "MISSING_%d", du->index); | |
9959 | du->next = super->missing; | |
9960 | super->missing = du; | |
9961 | } | |
9962 | /* create new dev and map */ | |
9963 | space = *space_list; | |
9964 | if (!space) | |
9965 | return 0; | |
9966 | *space_list = *space; | |
9967 | dev_new = (void *)space; | |
9968 | memcpy(dev_new, dev, sizeof(*dev)); | |
9969 | /* update new map */ | |
238c0a71 | 9970 | map = get_imsm_map(dev_new, MAP_0); |
1c275381 | 9971 | |
1a2487c2 | 9972 | map->map_state = IMSM_T_STATE_DEGRADED; |
8ca6df95 | 9973 | map->raid_level = 1; |
1c275381 MT |
9974 | set_num_domains(map); |
9975 | map->num_members = map->num_members * map->num_domains; | |
9976 | update_num_data_stripes(map, imsm_dev_size(dev)); | |
4a353e6e | 9977 | |
8ca6df95 KW |
9978 | /* replace dev<->dev_new */ |
9979 | dv->dev = dev_new; | |
9980 | } | |
bb025c2f KW |
9981 | /* update disk order table */ |
9982 | for (du = super->disks; du; du = du->next) | |
9983 | if (du->index >= 0) | |
9984 | set_imsm_ord_tbl_ent(map, du->index, du->index); | |
8ca6df95 | 9985 | for (du = super->missing; du; du = du->next) |
1a2487c2 KW |
9986 | if (du->index >= 0) { |
9987 | set_imsm_ord_tbl_ent(map, du->index, du->index); | |
4c9e8c1e | 9988 | mark_missing(super, dv->dev, &du->disk, du->index); |
1a2487c2 | 9989 | } |
bb025c2f KW |
9990 | |
9991 | return 1; | |
9992 | } | |
9993 | ||
e8319a19 DW |
9994 | static void imsm_process_update(struct supertype *st, |
9995 | struct metadata_update *update) | |
9996 | { | |
9997 | /** | |
9998 | * crack open the metadata_update envelope to find the update record | |
9999 | * update can be one of: | |
d195167d AK |
10000 | * update_reshape_container_disks - all the arrays in the container |
10001 | * are being reshaped to have more devices. We need to mark | |
10002 | * the arrays for general migration and convert selected spares | |
10003 | * into active devices. | |
10004 | * update_activate_spare - a spare device has replaced a failed | |
1011e834 N |
10005 | * device in an array, update the disk_ord_tbl. If this disk is |
10006 | * present in all member arrays then also clear the SPARE_DISK | |
10007 | * flag | |
d195167d AK |
10008 | * update_create_array |
10009 | * update_kill_array | |
10010 | * update_rename_array | |
10011 | * update_add_remove_disk | |
e8319a19 DW |
10012 | */ |
10013 | struct intel_super *super = st->sb; | |
4d7b1503 | 10014 | struct imsm_super *mpb; |
e8319a19 DW |
10015 | enum imsm_update_type type = *(enum imsm_update_type *) update->buf; |
10016 | ||
4d7b1503 DW |
10017 | /* update requires a larger buf but the allocation failed */ |
10018 | if (super->next_len && !super->next_buf) { | |
10019 | super->next_len = 0; | |
10020 | return; | |
10021 | } | |
10022 | ||
10023 | if (super->next_buf) { | |
10024 | memcpy(super->next_buf, super->buf, super->len); | |
10025 | free(super->buf); | |
10026 | super->len = super->next_len; | |
10027 | super->buf = super->next_buf; | |
10028 | ||
10029 | super->next_len = 0; | |
10030 | super->next_buf = NULL; | |
10031 | } | |
10032 | ||
10033 | mpb = super->anchor; | |
10034 | ||
e8319a19 | 10035 | switch (type) { |
0ec5d470 AK |
10036 | case update_general_migration_checkpoint: { |
10037 | struct intel_dev *id; | |
10038 | struct imsm_update_general_migration_checkpoint *u = | |
10039 | (void *)update->buf; | |
10040 | ||
1ade5cc1 | 10041 | dprintf("called for update_general_migration_checkpoint\n"); |
0ec5d470 AK |
10042 | |
10043 | /* find device under general migration */ | |
10044 | for (id = super->devlist ; id; id = id->next) { | |
10045 | if (is_gen_migration(id->dev)) { | |
4036e7ee MT |
10046 | set_vol_curr_migr_unit(id->dev, |
10047 | u->curr_migr_unit); | |
0ec5d470 AK |
10048 | super->updates_pending++; |
10049 | } | |
10050 | } | |
10051 | break; | |
10052 | } | |
bb025c2f KW |
10053 | case update_takeover: { |
10054 | struct imsm_update_takeover *u = (void *)update->buf; | |
1a2487c2 KW |
10055 | if (apply_takeover_update(u, super, &update->space_list)) { |
10056 | imsm_update_version_info(super); | |
bb025c2f | 10057 | super->updates_pending++; |
1a2487c2 | 10058 | } |
bb025c2f KW |
10059 | break; |
10060 | } | |
10061 | ||
78b10e66 | 10062 | case update_reshape_container_disks: { |
d195167d | 10063 | struct imsm_update_reshape *u = (void *)update->buf; |
2e5dc010 N |
10064 | if (apply_reshape_container_disks_update( |
10065 | u, super, &update->space_list)) | |
10066 | super->updates_pending++; | |
78b10e66 N |
10067 | break; |
10068 | } | |
48c5303a | 10069 | case update_reshape_migration: { |
a29911da PC |
10070 | struct imsm_update_reshape_migration *u = (void *)update->buf; |
10071 | if (apply_reshape_migration_update( | |
10072 | u, super, &update->space_list)) | |
10073 | super->updates_pending++; | |
48c5303a PC |
10074 | break; |
10075 | } | |
f3871fdc AK |
10076 | case update_size_change: { |
10077 | struct imsm_update_size_change *u = (void *)update->buf; | |
10078 | if (apply_size_change_update(u, super)) | |
10079 | super->updates_pending++; | |
10080 | break; | |
10081 | } | |
e8319a19 | 10082 | case update_activate_spare: { |
1011e834 | 10083 | struct imsm_update_activate_spare *u = (void *) update->buf; |
69d40de4 JR |
10084 | |
10085 | if (prepare_spare_to_activate(st, u) && | |
10086 | apply_update_activate_spare(u, super, st->arrays)) | |
061d7da3 | 10087 | super->updates_pending++; |
8273f55e DW |
10088 | break; |
10089 | } | |
10090 | case update_create_array: { | |
10091 | /* someone wants to create a new array, we need to be aware of | |
10092 | * a few races/collisions: | |
10093 | * 1/ 'Create' called by two separate instances of mdadm | |
10094 | * 2/ 'Create' versus 'activate_spare': mdadm has chosen | |
10095 | * devices that have since been assimilated via | |
10096 | * activate_spare. | |
10097 | * In the event this update can not be carried out mdadm will | |
10098 | * (FIX ME) notice that its update did not take hold. | |
10099 | */ | |
10100 | struct imsm_update_create_array *u = (void *) update->buf; | |
ba2de7ba | 10101 | struct intel_dev *dv; |
8273f55e DW |
10102 | struct imsm_dev *dev; |
10103 | struct imsm_map *map, *new_map; | |
10104 | unsigned long long start, end; | |
10105 | unsigned long long new_start, new_end; | |
10106 | int i; | |
54c2c1ea DW |
10107 | struct disk_info *inf; |
10108 | struct dl *dl; | |
8273f55e DW |
10109 | |
10110 | /* handle racing creates: first come first serve */ | |
10111 | if (u->dev_idx < mpb->num_raid_devs) { | |
1ade5cc1 | 10112 | dprintf("subarray %d already defined\n", u->dev_idx); |
ba2de7ba | 10113 | goto create_error; |
8273f55e DW |
10114 | } |
10115 | ||
10116 | /* check update is next in sequence */ | |
10117 | if (u->dev_idx != mpb->num_raid_devs) { | |
1ade5cc1 N |
10118 | dprintf("can not create array %d expected index %d\n", |
10119 | u->dev_idx, mpb->num_raid_devs); | |
ba2de7ba | 10120 | goto create_error; |
8273f55e DW |
10121 | } |
10122 | ||
238c0a71 | 10123 | new_map = get_imsm_map(&u->dev, MAP_0); |
5551b113 | 10124 | new_start = pba_of_lba0(new_map); |
44490938 | 10125 | new_end = new_start + per_dev_array_size(new_map); |
54c2c1ea | 10126 | inf = get_disk_info(u); |
8273f55e DW |
10127 | |
10128 | /* handle activate_spare versus create race: | |
10129 | * check to make sure that overlapping arrays do not include | |
10130 | * overalpping disks | |
10131 | */ | |
10132 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
949c47a0 | 10133 | dev = get_imsm_dev(super, i); |
238c0a71 | 10134 | map = get_imsm_map(dev, MAP_0); |
5551b113 | 10135 | start = pba_of_lba0(map); |
44490938 | 10136 | end = start + per_dev_array_size(map); |
8273f55e DW |
10137 | if ((new_start >= start && new_start <= end) || |
10138 | (start >= new_start && start <= new_end)) | |
54c2c1ea DW |
10139 | /* overlap */; |
10140 | else | |
10141 | continue; | |
10142 | ||
10143 | if (disks_overlap(super, i, u)) { | |
1ade5cc1 | 10144 | dprintf("arrays overlap\n"); |
ba2de7ba | 10145 | goto create_error; |
8273f55e DW |
10146 | } |
10147 | } | |
8273f55e | 10148 | |
949c47a0 DW |
10149 | /* check that prepare update was successful */ |
10150 | if (!update->space) { | |
1ade5cc1 | 10151 | dprintf("prepare update failed\n"); |
ba2de7ba | 10152 | goto create_error; |
949c47a0 DW |
10153 | } |
10154 | ||
54c2c1ea DW |
10155 | /* check that all disks are still active before committing |
10156 | * changes. FIXME: could we instead handle this by creating a | |
10157 | * degraded array? That's probably not what the user expects, | |
10158 | * so better to drop this update on the floor. | |
10159 | */ | |
10160 | for (i = 0; i < new_map->num_members; i++) { | |
10161 | dl = serial_to_dl(inf[i].serial, super); | |
10162 | if (!dl) { | |
1ade5cc1 | 10163 | dprintf("disk disappeared\n"); |
ba2de7ba | 10164 | goto create_error; |
54c2c1ea | 10165 | } |
949c47a0 DW |
10166 | } |
10167 | ||
8273f55e | 10168 | super->updates_pending++; |
54c2c1ea DW |
10169 | |
10170 | /* convert spares to members and fixup ord_tbl */ | |
10171 | for (i = 0; i < new_map->num_members; i++) { | |
10172 | dl = serial_to_dl(inf[i].serial, super); | |
10173 | if (dl->index == -1) { | |
10174 | dl->index = mpb->num_disks; | |
10175 | mpb->num_disks++; | |
10176 | dl->disk.status |= CONFIGURED_DISK; | |
10177 | dl->disk.status &= ~SPARE_DISK; | |
10178 | } | |
10179 | set_imsm_ord_tbl_ent(new_map, i, dl->index); | |
10180 | } | |
10181 | ||
ba2de7ba DW |
10182 | dv = update->space; |
10183 | dev = dv->dev; | |
949c47a0 DW |
10184 | update->space = NULL; |
10185 | imsm_copy_dev(dev, &u->dev); | |
ba2de7ba DW |
10186 | dv->index = u->dev_idx; |
10187 | dv->next = super->devlist; | |
10188 | super->devlist = dv; | |
8273f55e | 10189 | mpb->num_raid_devs++; |
8273f55e | 10190 | |
4d1313e9 | 10191 | imsm_update_version_info(super); |
8273f55e | 10192 | break; |
ba2de7ba DW |
10193 | create_error: |
10194 | /* mdmon knows how to release update->space, but not | |
10195 | * ((struct intel_dev *) update->space)->dev | |
10196 | */ | |
10197 | if (update->space) { | |
10198 | dv = update->space; | |
10199 | free(dv->dev); | |
10200 | } | |
8273f55e | 10201 | break; |
e8319a19 | 10202 | } |
33414a01 DW |
10203 | case update_kill_array: { |
10204 | struct imsm_update_kill_array *u = (void *) update->buf; | |
10205 | int victim = u->dev_idx; | |
10206 | struct active_array *a; | |
10207 | struct intel_dev **dp; | |
33414a01 DW |
10208 | |
10209 | /* sanity check that we are not affecting the uuid of | |
10210 | * active arrays, or deleting an active array | |
10211 | * | |
10212 | * FIXME when immutable ids are available, but note that | |
10213 | * we'll also need to fixup the invalidated/active | |
10214 | * subarray indexes in mdstat | |
10215 | */ | |
10216 | for (a = st->arrays; a; a = a->next) | |
10217 | if (a->info.container_member >= victim) | |
10218 | break; | |
10219 | /* by definition if mdmon is running at least one array | |
10220 | * is active in the container, so checking | |
10221 | * mpb->num_raid_devs is just extra paranoia | |
10222 | */ | |
756a15f3 | 10223 | if (a || mpb->num_raid_devs == 1 || victim >= super->anchor->num_raid_devs) { |
33414a01 DW |
10224 | dprintf("failed to delete subarray-%d\n", victim); |
10225 | break; | |
10226 | } | |
10227 | ||
10228 | for (dp = &super->devlist; *dp;) | |
f21e18ca | 10229 | if ((*dp)->index == (unsigned)super->current_vol) { |
33414a01 DW |
10230 | *dp = (*dp)->next; |
10231 | } else { | |
f21e18ca | 10232 | if ((*dp)->index > (unsigned)victim) |
33414a01 DW |
10233 | (*dp)->index--; |
10234 | dp = &(*dp)->next; | |
10235 | } | |
10236 | mpb->num_raid_devs--; | |
10237 | super->updates_pending++; | |
10238 | break; | |
10239 | } | |
aa534678 DW |
10240 | case update_rename_array: { |
10241 | struct imsm_update_rename_array *u = (void *) update->buf; | |
10242 | char name[MAX_RAID_SERIAL_LEN+1]; | |
10243 | int target = u->dev_idx; | |
10244 | struct active_array *a; | |
10245 | struct imsm_dev *dev; | |
10246 | ||
10247 | /* sanity check that we are not affecting the uuid of | |
10248 | * an active array | |
10249 | */ | |
40659392 | 10250 | memset(name, 0, sizeof(name)); |
aa534678 DW |
10251 | snprintf(name, MAX_RAID_SERIAL_LEN, "%s", (char *) u->name); |
10252 | name[MAX_RAID_SERIAL_LEN] = '\0'; | |
10253 | for (a = st->arrays; a; a = a->next) | |
10254 | if (a->info.container_member == target) | |
10255 | break; | |
10256 | dev = get_imsm_dev(super, u->dev_idx); | |
756a15f3 | 10257 | if (a || !check_name(super, name, 1)) { |
aa534678 DW |
10258 | dprintf("failed to rename subarray-%d\n", target); |
10259 | break; | |
10260 | } | |
10261 | ||
40659392 | 10262 | memcpy(dev->volume, name, MAX_RAID_SERIAL_LEN); |
aa534678 DW |
10263 | super->updates_pending++; |
10264 | break; | |
10265 | } | |
1a64be56 | 10266 | case update_add_remove_disk: { |
43dad3d6 | 10267 | /* we may be able to repair some arrays if disks are |
095b8088 | 10268 | * being added, check the status of add_remove_disk |
1a64be56 LM |
10269 | * if discs has been added. |
10270 | */ | |
10271 | if (add_remove_disk_update(super)) { | |
43dad3d6 | 10272 | struct active_array *a; |
072b727f DW |
10273 | |
10274 | super->updates_pending++; | |
1a64be56 | 10275 | for (a = st->arrays; a; a = a->next) |
43dad3d6 DW |
10276 | a->check_degraded = 1; |
10277 | } | |
43dad3d6 | 10278 | break; |
e8319a19 | 10279 | } |
bbab0940 TM |
10280 | case update_prealloc_badblocks_mem: |
10281 | break; | |
e6e9dd3f AP |
10282 | case update_rwh_policy: { |
10283 | struct imsm_update_rwh_policy *u = (void *)update->buf; | |
10284 | int target = u->dev_idx; | |
10285 | struct imsm_dev *dev = get_imsm_dev(super, target); | |
e6e9dd3f AP |
10286 | |
10287 | if (dev->rwh_policy != u->new_policy) { | |
10288 | dev->rwh_policy = u->new_policy; | |
10289 | super->updates_pending++; | |
10290 | } | |
10291 | break; | |
10292 | } | |
1a64be56 | 10293 | default: |
ebf3be99 | 10294 | pr_err("error: unsupported process update type:(type: %d)\n", type); |
1a64be56 | 10295 | } |
e8319a19 | 10296 | } |
88758e9d | 10297 | |
bc0b9d34 PC |
10298 | static struct mdinfo *get_spares_for_grow(struct supertype *st); |
10299 | ||
5fe6f031 N |
10300 | static int imsm_prepare_update(struct supertype *st, |
10301 | struct metadata_update *update) | |
8273f55e | 10302 | { |
949c47a0 | 10303 | /** |
4d7b1503 DW |
10304 | * Allocate space to hold new disk entries, raid-device entries or a new |
10305 | * mpb if necessary. The manager synchronously waits for updates to | |
10306 | * complete in the monitor, so new mpb buffers allocated here can be | |
10307 | * integrated by the monitor thread without worrying about live pointers | |
10308 | * in the manager thread. | |
8273f55e | 10309 | */ |
095b8088 | 10310 | enum imsm_update_type type; |
4d7b1503 | 10311 | struct intel_super *super = st->sb; |
f36a9ecd | 10312 | unsigned int sector_size = super->sector_size; |
4d7b1503 DW |
10313 | struct imsm_super *mpb = super->anchor; |
10314 | size_t buf_len; | |
10315 | size_t len = 0; | |
949c47a0 | 10316 | |
095b8088 N |
10317 | if (update->len < (int)sizeof(type)) |
10318 | return 0; | |
10319 | ||
10320 | type = *(enum imsm_update_type *) update->buf; | |
10321 | ||
949c47a0 | 10322 | switch (type) { |
0ec5d470 | 10323 | case update_general_migration_checkpoint: |
095b8088 N |
10324 | if (update->len < (int)sizeof(struct imsm_update_general_migration_checkpoint)) |
10325 | return 0; | |
1ade5cc1 | 10326 | dprintf("called for update_general_migration_checkpoint\n"); |
0ec5d470 | 10327 | break; |
abedf5fc KW |
10328 | case update_takeover: { |
10329 | struct imsm_update_takeover *u = (void *)update->buf; | |
095b8088 N |
10330 | if (update->len < (int)sizeof(*u)) |
10331 | return 0; | |
abedf5fc KW |
10332 | if (u->direction == R0_TO_R10) { |
10333 | void **tail = (void **)&update->space_list; | |
10334 | struct imsm_dev *dev = get_imsm_dev(super, u->subarray); | |
238c0a71 | 10335 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
abedf5fc KW |
10336 | int num_members = map->num_members; |
10337 | void *space; | |
10338 | int size, i; | |
abedf5fc KW |
10339 | /* allocate memory for added disks */ |
10340 | for (i = 0; i < num_members; i++) { | |
10341 | size = sizeof(struct dl); | |
503975b9 | 10342 | space = xmalloc(size); |
abedf5fc KW |
10343 | *tail = space; |
10344 | tail = space; | |
10345 | *tail = NULL; | |
10346 | } | |
10347 | /* allocate memory for new device */ | |
10348 | size = sizeof_imsm_dev(super->devlist->dev, 0) + | |
10349 | (num_members * sizeof(__u32)); | |
503975b9 N |
10350 | space = xmalloc(size); |
10351 | *tail = space; | |
10352 | tail = space; | |
10353 | *tail = NULL; | |
10354 | len = disks_to_mpb_size(num_members * 2); | |
abedf5fc KW |
10355 | } |
10356 | ||
10357 | break; | |
10358 | } | |
78b10e66 | 10359 | case update_reshape_container_disks: { |
d195167d AK |
10360 | /* Every raid device in the container is about to |
10361 | * gain some more devices, and we will enter a | |
10362 | * reconfiguration. | |
10363 | * So each 'imsm_map' will be bigger, and the imsm_vol | |
10364 | * will now hold 2 of them. | |
10365 | * Thus we need new 'struct imsm_dev' allocations sized | |
10366 | * as sizeof_imsm_dev but with more devices in both maps. | |
10367 | */ | |
10368 | struct imsm_update_reshape *u = (void *)update->buf; | |
10369 | struct intel_dev *dl; | |
10370 | void **space_tail = (void**)&update->space_list; | |
10371 | ||
095b8088 N |
10372 | if (update->len < (int)sizeof(*u)) |
10373 | return 0; | |
10374 | ||
1ade5cc1 | 10375 | dprintf("for update_reshape\n"); |
d195167d AK |
10376 | |
10377 | for (dl = super->devlist; dl; dl = dl->next) { | |
10378 | int size = sizeof_imsm_dev(dl->dev, 1); | |
10379 | void *s; | |
d677e0b8 AK |
10380 | if (u->new_raid_disks > u->old_raid_disks) |
10381 | size += sizeof(__u32)*2* | |
10382 | (u->new_raid_disks - u->old_raid_disks); | |
503975b9 | 10383 | s = xmalloc(size); |
d195167d AK |
10384 | *space_tail = s; |
10385 | space_tail = s; | |
10386 | *space_tail = NULL; | |
10387 | } | |
10388 | ||
10389 | len = disks_to_mpb_size(u->new_raid_disks); | |
10390 | dprintf("New anchor length is %llu\n", (unsigned long long)len); | |
78b10e66 N |
10391 | break; |
10392 | } | |
48c5303a | 10393 | case update_reshape_migration: { |
bc0b9d34 PC |
10394 | /* for migration level 0->5 we need to add disks |
10395 | * so the same as for container operation we will copy | |
10396 | * device to the bigger location. | |
10397 | * in memory prepared device and new disk area are prepared | |
10398 | * for usage in process update | |
10399 | */ | |
10400 | struct imsm_update_reshape_migration *u = (void *)update->buf; | |
10401 | struct intel_dev *id; | |
10402 | void **space_tail = (void **)&update->space_list; | |
10403 | int size; | |
10404 | void *s; | |
10405 | int current_level = -1; | |
10406 | ||
095b8088 N |
10407 | if (update->len < (int)sizeof(*u)) |
10408 | return 0; | |
10409 | ||
1ade5cc1 | 10410 | dprintf("for update_reshape\n"); |
bc0b9d34 PC |
10411 | |
10412 | /* add space for bigger array in update | |
10413 | */ | |
10414 | for (id = super->devlist; id; id = id->next) { | |
10415 | if (id->index == (unsigned)u->subdev) { | |
10416 | size = sizeof_imsm_dev(id->dev, 1); | |
10417 | if (u->new_raid_disks > u->old_raid_disks) | |
10418 | size += sizeof(__u32)*2* | |
10419 | (u->new_raid_disks - u->old_raid_disks); | |
503975b9 | 10420 | s = xmalloc(size); |
bc0b9d34 PC |
10421 | *space_tail = s; |
10422 | space_tail = s; | |
10423 | *space_tail = NULL; | |
10424 | break; | |
10425 | } | |
10426 | } | |
10427 | if (update->space_list == NULL) | |
10428 | break; | |
10429 | ||
10430 | /* add space for disk in update | |
10431 | */ | |
10432 | size = sizeof(struct dl); | |
503975b9 | 10433 | s = xmalloc(size); |
bc0b9d34 PC |
10434 | *space_tail = s; |
10435 | space_tail = s; | |
10436 | *space_tail = NULL; | |
10437 | ||
10438 | /* add spare device to update | |
10439 | */ | |
10440 | for (id = super->devlist ; id; id = id->next) | |
10441 | if (id->index == (unsigned)u->subdev) { | |
10442 | struct imsm_dev *dev; | |
10443 | struct imsm_map *map; | |
10444 | ||
10445 | dev = get_imsm_dev(super, u->subdev); | |
238c0a71 | 10446 | map = get_imsm_map(dev, MAP_0); |
bc0b9d34 PC |
10447 | current_level = map->raid_level; |
10448 | break; | |
10449 | } | |
089f9d79 | 10450 | if (u->new_level == 5 && u->new_level != current_level) { |
bc0b9d34 PC |
10451 | struct mdinfo *spares; |
10452 | ||
10453 | spares = get_spares_for_grow(st); | |
10454 | if (spares) { | |
10455 | struct dl *dl; | |
10456 | struct mdinfo *dev; | |
10457 | ||
10458 | dev = spares->devs; | |
10459 | if (dev) { | |
10460 | u->new_disks[0] = | |
10461 | makedev(dev->disk.major, | |
10462 | dev->disk.minor); | |
10463 | dl = get_disk_super(super, | |
10464 | dev->disk.major, | |
10465 | dev->disk.minor); | |
10466 | dl->index = u->old_raid_disks; | |
10467 | dev = dev->next; | |
10468 | } | |
10469 | sysfs_free(spares); | |
10470 | } | |
10471 | } | |
10472 | len = disks_to_mpb_size(u->new_raid_disks); | |
10473 | dprintf("New anchor length is %llu\n", (unsigned long long)len); | |
48c5303a PC |
10474 | break; |
10475 | } | |
f3871fdc | 10476 | case update_size_change: { |
095b8088 N |
10477 | if (update->len < (int)sizeof(struct imsm_update_size_change)) |
10478 | return 0; | |
10479 | break; | |
10480 | } | |
10481 | case update_activate_spare: { | |
10482 | if (update->len < (int)sizeof(struct imsm_update_activate_spare)) | |
10483 | return 0; | |
f3871fdc AK |
10484 | break; |
10485 | } | |
949c47a0 DW |
10486 | case update_create_array: { |
10487 | struct imsm_update_create_array *u = (void *) update->buf; | |
ba2de7ba | 10488 | struct intel_dev *dv; |
54c2c1ea | 10489 | struct imsm_dev *dev = &u->dev; |
238c0a71 | 10490 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
54c2c1ea DW |
10491 | struct dl *dl; |
10492 | struct disk_info *inf; | |
10493 | int i; | |
10494 | int activate = 0; | |
949c47a0 | 10495 | |
095b8088 N |
10496 | if (update->len < (int)sizeof(*u)) |
10497 | return 0; | |
10498 | ||
54c2c1ea DW |
10499 | inf = get_disk_info(u); |
10500 | len = sizeof_imsm_dev(dev, 1); | |
ba2de7ba | 10501 | /* allocate a new super->devlist entry */ |
503975b9 N |
10502 | dv = xmalloc(sizeof(*dv)); |
10503 | dv->dev = xmalloc(len); | |
10504 | update->space = dv; | |
949c47a0 | 10505 | |
54c2c1ea DW |
10506 | /* count how many spares will be converted to members */ |
10507 | for (i = 0; i < map->num_members; i++) { | |
10508 | dl = serial_to_dl(inf[i].serial, super); | |
10509 | if (!dl) { | |
10510 | /* hmm maybe it failed?, nothing we can do about | |
10511 | * it here | |
10512 | */ | |
10513 | continue; | |
10514 | } | |
10515 | if (count_memberships(dl, super) == 0) | |
10516 | activate++; | |
10517 | } | |
10518 | len += activate * sizeof(struct imsm_disk); | |
949c47a0 | 10519 | break; |
095b8088 N |
10520 | } |
10521 | case update_kill_array: { | |
10522 | if (update->len < (int)sizeof(struct imsm_update_kill_array)) | |
10523 | return 0; | |
949c47a0 DW |
10524 | break; |
10525 | } | |
095b8088 N |
10526 | case update_rename_array: { |
10527 | if (update->len < (int)sizeof(struct imsm_update_rename_array)) | |
10528 | return 0; | |
10529 | break; | |
10530 | } | |
10531 | case update_add_remove_disk: | |
10532 | /* no update->len needed */ | |
10533 | break; | |
bbab0940 TM |
10534 | case update_prealloc_badblocks_mem: |
10535 | super->extra_space += sizeof(struct bbm_log) - | |
10536 | get_imsm_bbm_log_size(super->bbm_log); | |
10537 | break; | |
e6e9dd3f AP |
10538 | case update_rwh_policy: { |
10539 | if (update->len < (int)sizeof(struct imsm_update_rwh_policy)) | |
10540 | return 0; | |
10541 | break; | |
10542 | } | |
095b8088 N |
10543 | default: |
10544 | return 0; | |
949c47a0 | 10545 | } |
8273f55e | 10546 | |
4d7b1503 DW |
10547 | /* check if we need a larger metadata buffer */ |
10548 | if (super->next_buf) | |
10549 | buf_len = super->next_len; | |
10550 | else | |
10551 | buf_len = super->len; | |
10552 | ||
bbab0940 | 10553 | if (__le32_to_cpu(mpb->mpb_size) + super->extra_space + len > buf_len) { |
4d7b1503 DW |
10554 | /* ok we need a larger buf than what is currently allocated |
10555 | * if this allocation fails process_update will notice that | |
10556 | * ->next_len is set and ->next_buf is NULL | |
10557 | */ | |
bbab0940 TM |
10558 | buf_len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + |
10559 | super->extra_space + len, sector_size); | |
4d7b1503 DW |
10560 | if (super->next_buf) |
10561 | free(super->next_buf); | |
10562 | ||
10563 | super->next_len = buf_len; | |
f36a9ecd | 10564 | if (posix_memalign(&super->next_buf, sector_size, buf_len) == 0) |
1f45a8ad DW |
10565 | memset(super->next_buf, 0, buf_len); |
10566 | else | |
4d7b1503 DW |
10567 | super->next_buf = NULL; |
10568 | } | |
5fe6f031 | 10569 | return 1; |
8273f55e DW |
10570 | } |
10571 | ||
ae6aad82 | 10572 | /* must be called while manager is quiesced */ |
f21e18ca | 10573 | static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index) |
ae6aad82 DW |
10574 | { |
10575 | struct imsm_super *mpb = super->anchor; | |
ae6aad82 DW |
10576 | struct dl *iter; |
10577 | struct imsm_dev *dev; | |
10578 | struct imsm_map *map; | |
4c9e8c1e | 10579 | unsigned int i, j, num_members; |
fb12a745 | 10580 | __u32 ord, ord_map0; |
4c9e8c1e | 10581 | struct bbm_log *log = super->bbm_log; |
ae6aad82 | 10582 | |
1ade5cc1 | 10583 | dprintf("deleting device[%d] from imsm_super\n", index); |
ae6aad82 DW |
10584 | |
10585 | /* shift all indexes down one */ | |
10586 | for (iter = super->disks; iter; iter = iter->next) | |
f21e18ca | 10587 | if (iter->index > (int)index) |
ae6aad82 | 10588 | iter->index--; |
47ee5a45 | 10589 | for (iter = super->missing; iter; iter = iter->next) |
f21e18ca | 10590 | if (iter->index > (int)index) |
47ee5a45 | 10591 | iter->index--; |
ae6aad82 DW |
10592 | |
10593 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
10594 | dev = get_imsm_dev(super, i); | |
238c0a71 | 10595 | map = get_imsm_map(dev, MAP_0); |
24565c9a DW |
10596 | num_members = map->num_members; |
10597 | for (j = 0; j < num_members; j++) { | |
10598 | /* update ord entries being careful not to propagate | |
10599 | * ord-flags to the first map | |
10600 | */ | |
238c0a71 | 10601 | ord = get_imsm_ord_tbl_ent(dev, j, MAP_X); |
fb12a745 | 10602 | ord_map0 = get_imsm_ord_tbl_ent(dev, j, MAP_0); |
ae6aad82 | 10603 | |
24565c9a DW |
10604 | if (ord_to_idx(ord) <= index) |
10605 | continue; | |
ae6aad82 | 10606 | |
238c0a71 | 10607 | map = get_imsm_map(dev, MAP_0); |
fb12a745 | 10608 | set_imsm_ord_tbl_ent(map, j, ord_map0 - 1); |
238c0a71 | 10609 | map = get_imsm_map(dev, MAP_1); |
24565c9a DW |
10610 | if (map) |
10611 | set_imsm_ord_tbl_ent(map, j, ord - 1); | |
ae6aad82 DW |
10612 | } |
10613 | } | |
10614 | ||
4c9e8c1e TM |
10615 | for (i = 0; i < log->entry_count; i++) { |
10616 | struct bbm_log_entry *entry = &log->marked_block_entries[i]; | |
10617 | ||
10618 | if (entry->disk_ordinal <= index) | |
10619 | continue; | |
10620 | entry->disk_ordinal--; | |
10621 | } | |
10622 | ||
ae6aad82 DW |
10623 | mpb->num_disks--; |
10624 | super->updates_pending++; | |
24565c9a DW |
10625 | if (*dlp) { |
10626 | struct dl *dl = *dlp; | |
10627 | ||
10628 | *dlp = (*dlp)->next; | |
3a85bf0e | 10629 | __free_imsm_disk(dl, 1); |
24565c9a | 10630 | } |
ae6aad82 | 10631 | } |
9a717282 | 10632 | |
9a717282 AK |
10633 | static int imsm_get_allowed_degradation(int level, int raid_disks, |
10634 | struct intel_super *super, | |
10635 | struct imsm_dev *dev) | |
10636 | { | |
10637 | switch (level) { | |
bf5cf7c7 | 10638 | case 1: |
9a717282 AK |
10639 | case 10:{ |
10640 | int ret_val = 0; | |
10641 | struct imsm_map *map; | |
10642 | int i; | |
10643 | ||
10644 | ret_val = raid_disks/2; | |
10645 | /* check map if all disks pairs not failed | |
10646 | * in both maps | |
10647 | */ | |
238c0a71 | 10648 | map = get_imsm_map(dev, MAP_0); |
9a717282 AK |
10649 | for (i = 0; i < ret_val; i++) { |
10650 | int degradation = 0; | |
10651 | if (get_imsm_disk(super, i) == NULL) | |
10652 | degradation++; | |
10653 | if (get_imsm_disk(super, i + 1) == NULL) | |
10654 | degradation++; | |
10655 | if (degradation == 2) | |
10656 | return 0; | |
10657 | } | |
238c0a71 | 10658 | map = get_imsm_map(dev, MAP_1); |
9a717282 AK |
10659 | /* if there is no second map |
10660 | * result can be returned | |
10661 | */ | |
10662 | if (map == NULL) | |
10663 | return ret_val; | |
10664 | /* check degradation in second map | |
10665 | */ | |
10666 | for (i = 0; i < ret_val; i++) { | |
10667 | int degradation = 0; | |
10668 | if (get_imsm_disk(super, i) == NULL) | |
10669 | degradation++; | |
10670 | if (get_imsm_disk(super, i + 1) == NULL) | |
10671 | degradation++; | |
10672 | if (degradation == 2) | |
10673 | return 0; | |
10674 | } | |
10675 | return ret_val; | |
10676 | } | |
10677 | case 5: | |
10678 | return 1; | |
10679 | case 6: | |
10680 | return 2; | |
10681 | default: | |
10682 | return 0; | |
10683 | } | |
10684 | } | |
10685 | ||
d31ad643 PB |
10686 | /******************************************************************************* |
10687 | * Function: validate_container_imsm | |
10688 | * Description: This routine validates container after assemble, | |
10689 | * eg. if devices in container are under the same controller. | |
10690 | * | |
10691 | * Parameters: | |
10692 | * info : linked list with info about devices used in array | |
10693 | * Returns: | |
10694 | * 1 : HBA mismatch | |
10695 | * 0 : Success | |
10696 | ******************************************************************************/ | |
10697 | int validate_container_imsm(struct mdinfo *info) | |
10698 | { | |
6b781d33 AP |
10699 | if (check_env("IMSM_NO_PLATFORM")) |
10700 | return 0; | |
d31ad643 | 10701 | |
6b781d33 AP |
10702 | struct sys_dev *idev; |
10703 | struct sys_dev *hba = NULL; | |
10704 | struct sys_dev *intel_devices = find_intel_devices(); | |
10705 | char *dev_path = devt_to_devpath(makedev(info->disk.major, | |
7c798f87 | 10706 | info->disk.minor), 1, NULL); |
6b781d33 AP |
10707 | |
10708 | for (idev = intel_devices; idev; idev = idev->next) { | |
10709 | if (dev_path && strstr(dev_path, idev->path)) { | |
10710 | hba = idev; | |
10711 | break; | |
d31ad643 | 10712 | } |
6b781d33 AP |
10713 | } |
10714 | if (dev_path) | |
d31ad643 PB |
10715 | free(dev_path); |
10716 | ||
6b781d33 AP |
10717 | if (!hba) { |
10718 | pr_err("WARNING - Cannot detect HBA for device %s!\n", | |
10719 | devid2kname(makedev(info->disk.major, info->disk.minor))); | |
10720 | return 1; | |
10721 | } | |
10722 | ||
10723 | const struct imsm_orom *orom = get_orom_by_device_id(hba->dev_id); | |
10724 | struct mdinfo *dev; | |
10725 | ||
10726 | for (dev = info->next; dev; dev = dev->next) { | |
7c798f87 MT |
10727 | dev_path = devt_to_devpath(makedev(dev->disk.major, |
10728 | dev->disk.minor), 1, NULL); | |
6b781d33 AP |
10729 | |
10730 | struct sys_dev *hba2 = NULL; | |
10731 | for (idev = intel_devices; idev; idev = idev->next) { | |
10732 | if (dev_path && strstr(dev_path, idev->path)) { | |
10733 | hba2 = idev; | |
10734 | break; | |
d31ad643 PB |
10735 | } |
10736 | } | |
6b781d33 AP |
10737 | if (dev_path) |
10738 | free(dev_path); | |
10739 | ||
10740 | const struct imsm_orom *orom2 = hba2 == NULL ? NULL : | |
10741 | get_orom_by_device_id(hba2->dev_id); | |
10742 | ||
10743 | if (hba2 && hba->type != hba2->type) { | |
10744 | pr_err("WARNING - HBAs of devices do not match %s != %s\n", | |
10745 | get_sys_dev_type(hba->type), get_sys_dev_type(hba2->type)); | |
10746 | return 1; | |
10747 | } | |
10748 | ||
07cb1e57 | 10749 | if (orom != orom2) { |
6b781d33 AP |
10750 | pr_err("WARNING - IMSM container assembled with disks under different HBAs!\n" |
10751 | " This operation is not supported and can lead to data loss.\n"); | |
10752 | return 1; | |
10753 | } | |
10754 | ||
10755 | if (!orom) { | |
10756 | pr_err("WARNING - IMSM container assembled with disks under HBAs without IMSM platform support!\n" | |
10757 | " This operation is not supported and can lead to data loss.\n"); | |
10758 | return 1; | |
10759 | } | |
d31ad643 | 10760 | } |
6b781d33 | 10761 | |
d31ad643 PB |
10762 | return 0; |
10763 | } | |
32141c17 | 10764 | |
6f50473f TM |
10765 | /******************************************************************************* |
10766 | * Function: imsm_record_badblock | |
10767 | * Description: This routine stores new bad block record in BBM log | |
10768 | * | |
10769 | * Parameters: | |
10770 | * a : array containing a bad block | |
10771 | * slot : disk number containing a bad block | |
10772 | * sector : bad block sector | |
10773 | * length : bad block sectors range | |
10774 | * Returns: | |
10775 | * 1 : Success | |
10776 | * 0 : Error | |
10777 | ******************************************************************************/ | |
10778 | static int imsm_record_badblock(struct active_array *a, int slot, | |
10779 | unsigned long long sector, int length) | |
10780 | { | |
10781 | struct intel_super *super = a->container->sb; | |
10782 | int ord; | |
10783 | int ret; | |
10784 | ||
10785 | ord = imsm_disk_slot_to_ord(a, slot); | |
10786 | if (ord < 0) | |
10787 | return 0; | |
10788 | ||
10789 | ret = record_new_badblock(super->bbm_log, ord_to_idx(ord), sector, | |
10790 | length); | |
10791 | if (ret) | |
10792 | super->updates_pending++; | |
10793 | ||
10794 | return ret; | |
10795 | } | |
c07a5a4f TM |
10796 | /******************************************************************************* |
10797 | * Function: imsm_clear_badblock | |
10798 | * Description: This routine clears bad block record from BBM log | |
10799 | * | |
10800 | * Parameters: | |
10801 | * a : array containing a bad block | |
10802 | * slot : disk number containing a bad block | |
10803 | * sector : bad block sector | |
10804 | * length : bad block sectors range | |
10805 | * Returns: | |
10806 | * 1 : Success | |
10807 | * 0 : Error | |
10808 | ******************************************************************************/ | |
10809 | static int imsm_clear_badblock(struct active_array *a, int slot, | |
10810 | unsigned long long sector, int length) | |
10811 | { | |
10812 | struct intel_super *super = a->container->sb; | |
10813 | int ord; | |
10814 | int ret; | |
10815 | ||
10816 | ord = imsm_disk_slot_to_ord(a, slot); | |
10817 | if (ord < 0) | |
10818 | return 0; | |
10819 | ||
10820 | ret = clear_badblock(super->bbm_log, ord_to_idx(ord), sector, length); | |
10821 | if (ret) | |
10822 | super->updates_pending++; | |
10823 | ||
10824 | return ret; | |
10825 | } | |
928f1424 TM |
10826 | /******************************************************************************* |
10827 | * Function: imsm_get_badblocks | |
10828 | * Description: This routine get list of bad blocks for an array | |
10829 | * | |
10830 | * Parameters: | |
10831 | * a : array | |
10832 | * slot : disk number | |
10833 | * Returns: | |
10834 | * bb : structure containing bad blocks | |
10835 | * NULL : error | |
10836 | ******************************************************************************/ | |
10837 | static struct md_bb *imsm_get_badblocks(struct active_array *a, int slot) | |
10838 | { | |
10839 | int inst = a->info.container_member; | |
10840 | struct intel_super *super = a->container->sb; | |
10841 | struct imsm_dev *dev = get_imsm_dev(super, inst); | |
10842 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
10843 | int ord; | |
10844 | ||
10845 | ord = imsm_disk_slot_to_ord(a, slot); | |
10846 | if (ord < 0) | |
10847 | return NULL; | |
10848 | ||
10849 | get_volume_badblocks(super->bbm_log, ord_to_idx(ord), pba_of_lba0(map), | |
44490938 | 10850 | per_dev_array_size(map), &super->bb); |
928f1424 TM |
10851 | |
10852 | return &super->bb; | |
10853 | } | |
27156a57 TM |
10854 | /******************************************************************************* |
10855 | * Function: examine_badblocks_imsm | |
10856 | * Description: Prints list of bad blocks on a disk to the standard output | |
10857 | * | |
10858 | * Parameters: | |
10859 | * st : metadata handler | |
10860 | * fd : open file descriptor for device | |
10861 | * devname : device name | |
10862 | * Returns: | |
10863 | * 0 : Success | |
10864 | * 1 : Error | |
10865 | ******************************************************************************/ | |
10866 | static int examine_badblocks_imsm(struct supertype *st, int fd, char *devname) | |
10867 | { | |
10868 | struct intel_super *super = st->sb; | |
10869 | struct bbm_log *log = super->bbm_log; | |
10870 | struct dl *d = NULL; | |
10871 | int any = 0; | |
10872 | ||
10873 | for (d = super->disks; d ; d = d->next) { | |
10874 | if (strcmp(d->devname, devname) == 0) | |
10875 | break; | |
10876 | } | |
10877 | ||
10878 | if ((d == NULL) || (d->index < 0)) { /* serial mismatch probably */ | |
10879 | pr_err("%s doesn't appear to be part of a raid array\n", | |
10880 | devname); | |
10881 | return 1; | |
10882 | } | |
10883 | ||
10884 | if (log != NULL) { | |
10885 | unsigned int i; | |
10886 | struct bbm_log_entry *entry = &log->marked_block_entries[0]; | |
10887 | ||
10888 | for (i = 0; i < log->entry_count; i++) { | |
10889 | if (entry[i].disk_ordinal == d->index) { | |
10890 | unsigned long long sector = __le48_to_cpu( | |
10891 | &entry[i].defective_block_start); | |
10892 | int cnt = entry[i].marked_count + 1; | |
10893 | ||
10894 | if (!any) { | |
10895 | printf("Bad-blocks on %s:\n", devname); | |
10896 | any = 1; | |
10897 | } | |
10898 | ||
10899 | printf("%20llu for %d sectors\n", sector, cnt); | |
10900 | } | |
10901 | } | |
10902 | } | |
10903 | ||
10904 | if (!any) | |
10905 | printf("No bad-blocks list configured on %s\n", devname); | |
10906 | ||
10907 | return 0; | |
10908 | } | |
687629c2 AK |
10909 | /******************************************************************************* |
10910 | * Function: init_migr_record_imsm | |
10911 | * Description: Function inits imsm migration record | |
10912 | * Parameters: | |
10913 | * super : imsm internal array info | |
10914 | * dev : device under migration | |
10915 | * info : general array info to find the smallest device | |
10916 | * Returns: | |
10917 | * none | |
10918 | ******************************************************************************/ | |
10919 | void init_migr_record_imsm(struct supertype *st, struct imsm_dev *dev, | |
10920 | struct mdinfo *info) | |
10921 | { | |
10922 | struct intel_super *super = st->sb; | |
10923 | struct migr_record *migr_rec = super->migr_rec; | |
10924 | int new_data_disks; | |
10925 | unsigned long long dsize, dev_sectors; | |
10926 | long long unsigned min_dev_sectors = -1LLU; | |
238c0a71 AK |
10927 | struct imsm_map *map_dest = get_imsm_map(dev, MAP_0); |
10928 | struct imsm_map *map_src = get_imsm_map(dev, MAP_1); | |
687629c2 | 10929 | unsigned long long num_migr_units; |
3ef4403c | 10930 | unsigned long long array_blocks; |
2f86fda3 | 10931 | struct dl *dl_disk = NULL; |
687629c2 AK |
10932 | |
10933 | memset(migr_rec, 0, sizeof(struct migr_record)); | |
10934 | migr_rec->family_num = __cpu_to_le32(super->anchor->family_num); | |
10935 | ||
10936 | /* only ascending reshape supported now */ | |
10937 | migr_rec->ascending_migr = __cpu_to_le32(1); | |
10938 | ||
10939 | migr_rec->dest_depth_per_unit = GEN_MIGR_AREA_SIZE / | |
10940 | max(map_dest->blocks_per_strip, map_src->blocks_per_strip); | |
e1742195 AK |
10941 | migr_rec->dest_depth_per_unit *= |
10942 | max(map_dest->blocks_per_strip, map_src->blocks_per_strip); | |
9529d343 | 10943 | new_data_disks = imsm_num_data_members(map_dest); |
687629c2 AK |
10944 | migr_rec->blocks_per_unit = |
10945 | __cpu_to_le32(migr_rec->dest_depth_per_unit * new_data_disks); | |
10946 | migr_rec->dest_depth_per_unit = | |
10947 | __cpu_to_le32(migr_rec->dest_depth_per_unit); | |
3ef4403c | 10948 | array_blocks = info->component_size * new_data_disks; |
687629c2 AK |
10949 | num_migr_units = |
10950 | array_blocks / __le32_to_cpu(migr_rec->blocks_per_unit); | |
10951 | ||
10952 | if (array_blocks % __le32_to_cpu(migr_rec->blocks_per_unit)) | |
10953 | num_migr_units++; | |
9f421827 | 10954 | set_num_migr_units(migr_rec, num_migr_units); |
687629c2 AK |
10955 | |
10956 | migr_rec->post_migr_vol_cap = dev->size_low; | |
10957 | migr_rec->post_migr_vol_cap_hi = dev->size_high; | |
10958 | ||
687629c2 | 10959 | /* Find the smallest dev */ |
2f86fda3 MT |
10960 | for (dl_disk = super->disks; dl_disk ; dl_disk = dl_disk->next) { |
10961 | /* ignore spares in container */ | |
10962 | if (dl_disk->index < 0) | |
687629c2 | 10963 | continue; |
2f86fda3 | 10964 | get_dev_size(dl_disk->fd, NULL, &dsize); |
687629c2 AK |
10965 | dev_sectors = dsize / 512; |
10966 | if (dev_sectors < min_dev_sectors) | |
10967 | min_dev_sectors = dev_sectors; | |
687629c2 | 10968 | } |
9f421827 | 10969 | set_migr_chkp_area_pba(migr_rec, min_dev_sectors - |
687629c2 AK |
10970 | RAID_DISK_RESERVED_BLOCKS_IMSM_HI); |
10971 | ||
10972 | write_imsm_migr_rec(st); | |
10973 | ||
10974 | return; | |
10975 | } | |
10976 | ||
10977 | /******************************************************************************* | |
10978 | * Function: save_backup_imsm | |
10979 | * Description: Function saves critical data stripes to Migration Copy Area | |
10980 | * and updates the current migration unit status. | |
10981 | * Use restore_stripes() to form a destination stripe, | |
10982 | * and to write it to the Copy Area. | |
10983 | * Parameters: | |
10984 | * st : supertype information | |
aea93171 | 10985 | * dev : imsm device that backup is saved for |
687629c2 AK |
10986 | * info : general array info |
10987 | * buf : input buffer | |
687629c2 AK |
10988 | * length : length of data to backup (blocks_per_unit) |
10989 | * Returns: | |
10990 | * 0 : success | |
10991 | *, -1 : fail | |
10992 | ******************************************************************************/ | |
10993 | int save_backup_imsm(struct supertype *st, | |
10994 | struct imsm_dev *dev, | |
10995 | struct mdinfo *info, | |
10996 | void *buf, | |
687629c2 AK |
10997 | int length) |
10998 | { | |
10999 | int rv = -1; | |
11000 | struct intel_super *super = st->sb; | |
687629c2 | 11001 | int i; |
238c0a71 | 11002 | struct imsm_map *map_dest = get_imsm_map(dev, MAP_0); |
687629c2 | 11003 | int new_disks = map_dest->num_members; |
ab724b98 | 11004 | int dest_layout = 0; |
4389ce73 MT |
11005 | int dest_chunk, targets[new_disks]; |
11006 | unsigned long long start, target_offsets[new_disks]; | |
9529d343 | 11007 | int data_disks = imsm_num_data_members(map_dest); |
687629c2 | 11008 | |
2f86fda3 MT |
11009 | for (i = 0; i < new_disks; i++) { |
11010 | struct dl *dl_disk = get_imsm_dl_disk(super, i); | |
4389ce73 MT |
11011 | if (dl_disk && is_fd_valid(dl_disk->fd)) |
11012 | targets[i] = dl_disk->fd; | |
11013 | else | |
11014 | goto abort; | |
2f86fda3 | 11015 | } |
7e45b550 | 11016 | |
d1877f69 | 11017 | start = info->reshape_progress * 512; |
687629c2 | 11018 | for (i = 0; i < new_disks; i++) { |
9f421827 | 11019 | target_offsets[i] = migr_chkp_area_pba(super->migr_rec) * 512; |
d1877f69 AK |
11020 | /* move back copy area adderss, it will be moved forward |
11021 | * in restore_stripes() using start input variable | |
11022 | */ | |
11023 | target_offsets[i] -= start/data_disks; | |
687629c2 AK |
11024 | } |
11025 | ||
68eb8bc6 | 11026 | dest_layout = imsm_level_to_layout(map_dest->raid_level); |
ab724b98 AK |
11027 | dest_chunk = __le16_to_cpu(map_dest->blocks_per_strip) * 512; |
11028 | ||
687629c2 AK |
11029 | if (restore_stripes(targets, /* list of dest devices */ |
11030 | target_offsets, /* migration record offsets */ | |
11031 | new_disks, | |
ab724b98 AK |
11032 | dest_chunk, |
11033 | map_dest->raid_level, | |
11034 | dest_layout, | |
11035 | -1, /* source backup file descriptor */ | |
11036 | 0, /* input buf offset | |
11037 | * always 0 buf is already offseted */ | |
d1877f69 | 11038 | start, |
687629c2 AK |
11039 | length, |
11040 | buf) != 0) { | |
e7b84f9d | 11041 | pr_err("Error restoring stripes\n"); |
687629c2 AK |
11042 | goto abort; |
11043 | } | |
11044 | ||
11045 | rv = 0; | |
11046 | ||
11047 | abort: | |
687629c2 AK |
11048 | return rv; |
11049 | } | |
11050 | ||
11051 | /******************************************************************************* | |
11052 | * Function: save_checkpoint_imsm | |
11053 | * Description: Function called for current unit status update | |
11054 | * in the migration record. It writes it to disk. | |
11055 | * Parameters: | |
11056 | * super : imsm internal array info | |
11057 | * info : general array info | |
11058 | * Returns: | |
11059 | * 0: success | |
11060 | * 1: failure | |
0228d92c AK |
11061 | * 2: failure, means no valid migration record |
11062 | * / no general migration in progress / | |
687629c2 AK |
11063 | ******************************************************************************/ |
11064 | int save_checkpoint_imsm(struct supertype *st, struct mdinfo *info, int state) | |
11065 | { | |
11066 | struct intel_super *super = st->sb; | |
f8b72ef5 AK |
11067 | unsigned long long blocks_per_unit; |
11068 | unsigned long long curr_migr_unit; | |
11069 | ||
2f86fda3 | 11070 | if (load_imsm_migr_rec(super) != 0) { |
7a862a02 | 11071 | dprintf("imsm: ERROR: Cannot read migration record for checkpoint save.\n"); |
2e062e82 AK |
11072 | return 1; |
11073 | } | |
11074 | ||
f8b72ef5 AK |
11075 | blocks_per_unit = __le32_to_cpu(super->migr_rec->blocks_per_unit); |
11076 | if (blocks_per_unit == 0) { | |
0228d92c AK |
11077 | dprintf("imsm: no migration in progress.\n"); |
11078 | return 2; | |
687629c2 | 11079 | } |
f8b72ef5 AK |
11080 | curr_migr_unit = info->reshape_progress / blocks_per_unit; |
11081 | /* check if array is alligned to copy area | |
11082 | * if it is not alligned, add one to current migration unit value | |
11083 | * this can happend on array reshape finish only | |
11084 | */ | |
11085 | if (info->reshape_progress % blocks_per_unit) | |
11086 | curr_migr_unit++; | |
687629c2 | 11087 | |
9f421827 | 11088 | set_current_migr_unit(super->migr_rec, curr_migr_unit); |
687629c2 | 11089 | super->migr_rec->rec_status = __cpu_to_le32(state); |
9f421827 PB |
11090 | set_migr_dest_1st_member_lba(super->migr_rec, |
11091 | super->migr_rec->dest_depth_per_unit * curr_migr_unit); | |
11092 | ||
687629c2 | 11093 | if (write_imsm_migr_rec(st) < 0) { |
7a862a02 | 11094 | dprintf("imsm: Cannot write migration record outside backup area\n"); |
687629c2 AK |
11095 | return 1; |
11096 | } | |
11097 | ||
11098 | return 0; | |
11099 | } | |
11100 | ||
276d77db AK |
11101 | /******************************************************************************* |
11102 | * Function: recover_backup_imsm | |
11103 | * Description: Function recovers critical data from the Migration Copy Area | |
11104 | * while assembling an array. | |
11105 | * Parameters: | |
11106 | * super : imsm internal array info | |
11107 | * info : general array info | |
11108 | * Returns: | |
11109 | * 0 : success (or there is no data to recover) | |
11110 | * 1 : fail | |
11111 | ******************************************************************************/ | |
11112 | int recover_backup_imsm(struct supertype *st, struct mdinfo *info) | |
11113 | { | |
11114 | struct intel_super *super = st->sb; | |
11115 | struct migr_record *migr_rec = super->migr_rec; | |
594dc1b8 | 11116 | struct imsm_map *map_dest; |
276d77db AK |
11117 | struct intel_dev *id = NULL; |
11118 | unsigned long long read_offset; | |
11119 | unsigned long long write_offset; | |
11120 | unsigned unit_len; | |
2f86fda3 | 11121 | int new_disks, err; |
276d77db AK |
11122 | char *buf = NULL; |
11123 | int retval = 1; | |
f36a9ecd | 11124 | unsigned int sector_size = super->sector_size; |
4036e7ee MT |
11125 | unsigned long long curr_migr_unit = current_migr_unit(migr_rec); |
11126 | unsigned long long num_migr_units = get_num_migr_units(migr_rec); | |
276d77db | 11127 | char buffer[20]; |
6c3560c0 | 11128 | int skipped_disks = 0; |
2f86fda3 | 11129 | struct dl *dl_disk; |
276d77db AK |
11130 | |
11131 | err = sysfs_get_str(info, NULL, "array_state", (char *)buffer, 20); | |
11132 | if (err < 1) | |
11133 | return 1; | |
11134 | ||
11135 | /* recover data only during assemblation */ | |
11136 | if (strncmp(buffer, "inactive", 8) != 0) | |
11137 | return 0; | |
11138 | /* no data to recover */ | |
11139 | if (__le32_to_cpu(migr_rec->rec_status) == UNIT_SRC_NORMAL) | |
11140 | return 0; | |
11141 | if (curr_migr_unit >= num_migr_units) | |
11142 | return 1; | |
11143 | ||
11144 | /* find device during reshape */ | |
11145 | for (id = super->devlist; id; id = id->next) | |
11146 | if (is_gen_migration(id->dev)) | |
11147 | break; | |
11148 | if (id == NULL) | |
11149 | return 1; | |
11150 | ||
238c0a71 | 11151 | map_dest = get_imsm_map(id->dev, MAP_0); |
276d77db AK |
11152 | new_disks = map_dest->num_members; |
11153 | ||
9f421827 | 11154 | read_offset = migr_chkp_area_pba(migr_rec) * 512; |
276d77db | 11155 | |
9f421827 | 11156 | write_offset = (migr_dest_1st_member_lba(migr_rec) + |
5551b113 | 11157 | pba_of_lba0(map_dest)) * 512; |
276d77db AK |
11158 | |
11159 | unit_len = __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512; | |
f36a9ecd | 11160 | if (posix_memalign((void **)&buf, sector_size, unit_len) != 0) |
276d77db | 11161 | goto abort; |
276d77db | 11162 | |
2f86fda3 MT |
11163 | for (dl_disk = super->disks; dl_disk; dl_disk = dl_disk->next) { |
11164 | if (dl_disk->index < 0) | |
11165 | continue; | |
276d77db | 11166 | |
4389ce73 | 11167 | if (!is_fd_valid(dl_disk->fd)) { |
6c3560c0 AK |
11168 | skipped_disks++; |
11169 | continue; | |
11170 | } | |
2f86fda3 | 11171 | if (lseek64(dl_disk->fd, read_offset, SEEK_SET) < 0) { |
e7b84f9d N |
11172 | pr_err("Cannot seek to block: %s\n", |
11173 | strerror(errno)); | |
137debce AK |
11174 | skipped_disks++; |
11175 | continue; | |
276d77db | 11176 | } |
83b3de77 | 11177 | if (read(dl_disk->fd, buf, unit_len) != (ssize_t)unit_len) { |
e7b84f9d N |
11178 | pr_err("Cannot read copy area block: %s\n", |
11179 | strerror(errno)); | |
137debce AK |
11180 | skipped_disks++; |
11181 | continue; | |
276d77db | 11182 | } |
2f86fda3 | 11183 | if (lseek64(dl_disk->fd, write_offset, SEEK_SET) < 0) { |
e7b84f9d N |
11184 | pr_err("Cannot seek to block: %s\n", |
11185 | strerror(errno)); | |
137debce AK |
11186 | skipped_disks++; |
11187 | continue; | |
276d77db | 11188 | } |
83b3de77 | 11189 | if (write(dl_disk->fd, buf, unit_len) != (ssize_t)unit_len) { |
e7b84f9d N |
11190 | pr_err("Cannot restore block: %s\n", |
11191 | strerror(errno)); | |
137debce AK |
11192 | skipped_disks++; |
11193 | continue; | |
276d77db AK |
11194 | } |
11195 | } | |
11196 | ||
137debce AK |
11197 | if (skipped_disks > imsm_get_allowed_degradation(info->new_level, |
11198 | new_disks, | |
11199 | super, | |
11200 | id->dev)) { | |
7a862a02 | 11201 | pr_err("Cannot restore data from backup. Too many failed disks\n"); |
6c3560c0 AK |
11202 | goto abort; |
11203 | } | |
11204 | ||
befb629b AK |
11205 | if (save_checkpoint_imsm(st, info, UNIT_SRC_NORMAL)) { |
11206 | /* ignore error == 2, this can mean end of reshape here | |
11207 | */ | |
7a862a02 | 11208 | dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL) during restart\n"); |
befb629b | 11209 | } else |
276d77db | 11210 | retval = 0; |
276d77db AK |
11211 | |
11212 | abort: | |
276d77db AK |
11213 | free(buf); |
11214 | return retval; | |
11215 | } | |
11216 | ||
2cda7640 ML |
11217 | static char disk_by_path[] = "/dev/disk/by-path/"; |
11218 | ||
11219 | static const char *imsm_get_disk_controller_domain(const char *path) | |
11220 | { | |
2cda7640 | 11221 | char disk_path[PATH_MAX]; |
96234762 LM |
11222 | char *drv=NULL; |
11223 | struct stat st; | |
2cda7640 | 11224 | |
6d8d290a | 11225 | strcpy(disk_path, disk_by_path); |
96234762 LM |
11226 | strncat(disk_path, path, PATH_MAX - strlen(disk_path) - 1); |
11227 | if (stat(disk_path, &st) == 0) { | |
11228 | struct sys_dev* hba; | |
594dc1b8 | 11229 | char *path; |
96234762 | 11230 | |
7c798f87 | 11231 | path = devt_to_devpath(st.st_rdev, 1, NULL); |
96234762 LM |
11232 | if (path == NULL) |
11233 | return "unknown"; | |
11234 | hba = find_disk_attached_hba(-1, path); | |
11235 | if (hba && hba->type == SYS_DEV_SAS) | |
11236 | drv = "isci"; | |
11237 | else if (hba && hba->type == SYS_DEV_SATA) | |
11238 | drv = "ahci"; | |
c6839718 MT |
11239 | else if (hba && hba->type == SYS_DEV_VMD) |
11240 | drv = "vmd"; | |
11241 | else if (hba && hba->type == SYS_DEV_NVME) | |
11242 | drv = "nvme"; | |
1011e834 | 11243 | else |
96234762 LM |
11244 | drv = "unknown"; |
11245 | dprintf("path: %s hba: %s attached: %s\n", | |
11246 | path, (hba) ? hba->path : "NULL", drv); | |
11247 | free(path); | |
2cda7640 | 11248 | } |
96234762 | 11249 | return drv; |
2cda7640 ML |
11250 | } |
11251 | ||
4dd2df09 | 11252 | static char *imsm_find_array_devnm_by_subdev(int subdev, char *container) |
78b10e66 | 11253 | { |
4dd2df09 | 11254 | static char devnm[32]; |
78b10e66 N |
11255 | char subdev_name[20]; |
11256 | struct mdstat_ent *mdstat; | |
11257 | ||
11258 | sprintf(subdev_name, "%d", subdev); | |
11259 | mdstat = mdstat_by_subdev(subdev_name, container); | |
11260 | if (!mdstat) | |
4dd2df09 | 11261 | return NULL; |
78b10e66 | 11262 | |
4dd2df09 | 11263 | strcpy(devnm, mdstat->devnm); |
78b10e66 | 11264 | free_mdstat(mdstat); |
4dd2df09 | 11265 | return devnm; |
78b10e66 N |
11266 | } |
11267 | ||
11268 | static int imsm_reshape_is_allowed_on_container(struct supertype *st, | |
11269 | struct geo_params *geo, | |
fbf3d202 AK |
11270 | int *old_raid_disks, |
11271 | int direction) | |
78b10e66 | 11272 | { |
694575e7 KW |
11273 | /* currently we only support increasing the number of devices |
11274 | * for a container. This increases the number of device for each | |
11275 | * member array. They must all be RAID0 or RAID5. | |
11276 | */ | |
78b10e66 N |
11277 | int ret_val = 0; |
11278 | struct mdinfo *info, *member; | |
11279 | int devices_that_can_grow = 0; | |
11280 | ||
7a862a02 | 11281 | dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): st->devnm = (%s)\n", st->devnm); |
78b10e66 | 11282 | |
d04f65f4 | 11283 | if (geo->size > 0 || |
78b10e66 N |
11284 | geo->level != UnSet || |
11285 | geo->layout != UnSet || | |
11286 | geo->chunksize != 0 || | |
11287 | geo->raid_disks == UnSet) { | |
7a862a02 | 11288 | dprintf("imsm: Container operation is allowed for raid disks number change only.\n"); |
78b10e66 N |
11289 | return ret_val; |
11290 | } | |
11291 | ||
fbf3d202 | 11292 | if (direction == ROLLBACK_METADATA_CHANGES) { |
7a862a02 | 11293 | dprintf("imsm: Metadata changes rollback is not supported for container operation.\n"); |
fbf3d202 AK |
11294 | return ret_val; |
11295 | } | |
11296 | ||
78b10e66 N |
11297 | info = container_content_imsm(st, NULL); |
11298 | for (member = info; member; member = member->next) { | |
4dd2df09 | 11299 | char *result; |
78b10e66 N |
11300 | |
11301 | dprintf("imsm: checking device_num: %i\n", | |
11302 | member->container_member); | |
11303 | ||
d7d205bd | 11304 | if (geo->raid_disks <= member->array.raid_disks) { |
78b10e66 N |
11305 | /* we work on container for Online Capacity Expansion |
11306 | * only so raid_disks has to grow | |
11307 | */ | |
7a862a02 | 11308 | dprintf("imsm: for container operation raid disks increase is required\n"); |
78b10e66 N |
11309 | break; |
11310 | } | |
11311 | ||
089f9d79 | 11312 | if (info->array.level != 0 && info->array.level != 5) { |
78b10e66 N |
11313 | /* we cannot use this container with other raid level |
11314 | */ | |
7a862a02 | 11315 | dprintf("imsm: for container operation wrong raid level (%i) detected\n", |
78b10e66 N |
11316 | info->array.level); |
11317 | break; | |
11318 | } else { | |
11319 | /* check for platform support | |
11320 | * for this raid level configuration | |
11321 | */ | |
11322 | struct intel_super *super = st->sb; | |
11323 | if (!is_raid_level_supported(super->orom, | |
11324 | member->array.level, | |
11325 | geo->raid_disks)) { | |
7a862a02 | 11326 | dprintf("platform does not support raid%d with %d disk%s\n", |
78b10e66 N |
11327 | info->array.level, |
11328 | geo->raid_disks, | |
11329 | geo->raid_disks > 1 ? "s" : ""); | |
11330 | break; | |
11331 | } | |
2a4a08e7 AK |
11332 | /* check if component size is aligned to chunk size |
11333 | */ | |
11334 | if (info->component_size % | |
11335 | (info->array.chunk_size/512)) { | |
7a862a02 | 11336 | dprintf("Component size is not aligned to chunk size\n"); |
2a4a08e7 AK |
11337 | break; |
11338 | } | |
78b10e66 N |
11339 | } |
11340 | ||
11341 | if (*old_raid_disks && | |
11342 | info->array.raid_disks != *old_raid_disks) | |
11343 | break; | |
11344 | *old_raid_disks = info->array.raid_disks; | |
11345 | ||
11346 | /* All raid5 and raid0 volumes in container | |
11347 | * have to be ready for Online Capacity Expansion | |
11348 | * so they need to be assembled. We have already | |
11349 | * checked that no recovery etc is happening. | |
11350 | */ | |
4dd2df09 N |
11351 | result = imsm_find_array_devnm_by_subdev(member->container_member, |
11352 | st->container_devnm); | |
11353 | if (result == NULL) { | |
78b10e66 N |
11354 | dprintf("imsm: cannot find array\n"); |
11355 | break; | |
11356 | } | |
11357 | devices_that_can_grow++; | |
11358 | } | |
11359 | sysfs_free(info); | |
11360 | if (!member && devices_that_can_grow) | |
11361 | ret_val = 1; | |
11362 | ||
11363 | if (ret_val) | |
1ade5cc1 | 11364 | dprintf("Container operation allowed\n"); |
78b10e66 | 11365 | else |
1ade5cc1 | 11366 | dprintf("Error: %i\n", ret_val); |
78b10e66 N |
11367 | |
11368 | return ret_val; | |
11369 | } | |
11370 | ||
11371 | /* Function: get_spares_for_grow | |
11372 | * Description: Allocates memory and creates list of spare devices | |
1011e834 | 11373 | * avaliable in container. Checks if spare drive size is acceptable. |
78b10e66 N |
11374 | * Parameters: Pointer to the supertype structure |
11375 | * Returns: Pointer to the list of spare devices (mdinfo structure) on success, | |
1011e834 | 11376 | * NULL if fail |
78b10e66 N |
11377 | */ |
11378 | static struct mdinfo *get_spares_for_grow(struct supertype *st) | |
11379 | { | |
fbfdcb06 AO |
11380 | struct spare_criteria sc; |
11381 | ||
11382 | get_spare_criteria_imsm(st, &sc); | |
11383 | return container_choose_spares(st, &sc, NULL, NULL, NULL, 0); | |
78b10e66 N |
11384 | } |
11385 | ||
11386 | /****************************************************************************** | |
11387 | * function: imsm_create_metadata_update_for_reshape | |
11388 | * Function creates update for whole IMSM container. | |
11389 | * | |
11390 | ******************************************************************************/ | |
11391 | static int imsm_create_metadata_update_for_reshape( | |
11392 | struct supertype *st, | |
11393 | struct geo_params *geo, | |
11394 | int old_raid_disks, | |
11395 | struct imsm_update_reshape **updatep) | |
11396 | { | |
11397 | struct intel_super *super = st->sb; | |
11398 | struct imsm_super *mpb = super->anchor; | |
594dc1b8 JS |
11399 | int update_memory_size; |
11400 | struct imsm_update_reshape *u; | |
11401 | struct mdinfo *spares; | |
78b10e66 | 11402 | int i; |
594dc1b8 | 11403 | int delta_disks; |
bbd24d86 | 11404 | struct mdinfo *dev; |
78b10e66 | 11405 | |
1ade5cc1 | 11406 | dprintf("(enter) raid_disks = %i\n", geo->raid_disks); |
78b10e66 N |
11407 | |
11408 | delta_disks = geo->raid_disks - old_raid_disks; | |
11409 | ||
11410 | /* size of all update data without anchor */ | |
11411 | update_memory_size = sizeof(struct imsm_update_reshape); | |
11412 | ||
11413 | /* now add space for spare disks that we need to add. */ | |
11414 | update_memory_size += sizeof(u->new_disks[0]) * (delta_disks - 1); | |
11415 | ||
503975b9 | 11416 | u = xcalloc(1, update_memory_size); |
78b10e66 N |
11417 | u->type = update_reshape_container_disks; |
11418 | u->old_raid_disks = old_raid_disks; | |
11419 | u->new_raid_disks = geo->raid_disks; | |
11420 | ||
11421 | /* now get spare disks list | |
11422 | */ | |
11423 | spares = get_spares_for_grow(st); | |
11424 | ||
d7be7d87 | 11425 | if (spares == NULL || delta_disks > spares->array.spare_disks) { |
7a862a02 | 11426 | pr_err("imsm: ERROR: Cannot get spare devices for %s.\n", geo->dev_name); |
e4c72d1d | 11427 | i = -1; |
78b10e66 N |
11428 | goto abort; |
11429 | } | |
11430 | ||
11431 | /* we have got spares | |
11432 | * update disk list in imsm_disk list table in anchor | |
11433 | */ | |
11434 | dprintf("imsm: %i spares are available.\n\n", | |
11435 | spares->array.spare_disks); | |
11436 | ||
bbd24d86 | 11437 | dev = spares->devs; |
78b10e66 | 11438 | for (i = 0; i < delta_disks; i++) { |
78b10e66 N |
11439 | struct dl *dl; |
11440 | ||
bbd24d86 AK |
11441 | if (dev == NULL) |
11442 | break; | |
78b10e66 N |
11443 | u->new_disks[i] = makedev(dev->disk.major, |
11444 | dev->disk.minor); | |
11445 | dl = get_disk_super(super, dev->disk.major, dev->disk.minor); | |
ee4beede AK |
11446 | dl->index = mpb->num_disks; |
11447 | mpb->num_disks++; | |
bbd24d86 | 11448 | dev = dev->next; |
78b10e66 | 11449 | } |
78b10e66 N |
11450 | |
11451 | abort: | |
11452 | /* free spares | |
11453 | */ | |
11454 | sysfs_free(spares); | |
11455 | ||
d677e0b8 | 11456 | dprintf("imsm: reshape update preparation :"); |
78b10e66 | 11457 | if (i == delta_disks) { |
1ade5cc1 | 11458 | dprintf_cont(" OK\n"); |
78b10e66 N |
11459 | *updatep = u; |
11460 | return update_memory_size; | |
11461 | } | |
11462 | free(u); | |
1ade5cc1 | 11463 | dprintf_cont(" Error\n"); |
78b10e66 N |
11464 | |
11465 | return 0; | |
11466 | } | |
11467 | ||
f3871fdc AK |
11468 | /****************************************************************************** |
11469 | * function: imsm_create_metadata_update_for_size_change() | |
11470 | * Creates update for IMSM array for array size change. | |
11471 | * | |
11472 | ******************************************************************************/ | |
11473 | static int imsm_create_metadata_update_for_size_change( | |
11474 | struct supertype *st, | |
11475 | struct geo_params *geo, | |
11476 | struct imsm_update_size_change **updatep) | |
11477 | { | |
11478 | struct intel_super *super = st->sb; | |
594dc1b8 JS |
11479 | int update_memory_size; |
11480 | struct imsm_update_size_change *u; | |
f3871fdc | 11481 | |
1ade5cc1 | 11482 | dprintf("(enter) New size = %llu\n", geo->size); |
f3871fdc AK |
11483 | |
11484 | /* size of all update data without anchor */ | |
11485 | update_memory_size = sizeof(struct imsm_update_size_change); | |
11486 | ||
503975b9 | 11487 | u = xcalloc(1, update_memory_size); |
f3871fdc AK |
11488 | u->type = update_size_change; |
11489 | u->subdev = super->current_vol; | |
11490 | u->new_size = geo->size; | |
11491 | ||
11492 | dprintf("imsm: reshape update preparation : OK\n"); | |
11493 | *updatep = u; | |
11494 | ||
11495 | return update_memory_size; | |
11496 | } | |
11497 | ||
48c5303a PC |
11498 | /****************************************************************************** |
11499 | * function: imsm_create_metadata_update_for_migration() | |
11500 | * Creates update for IMSM array. | |
11501 | * | |
11502 | ******************************************************************************/ | |
11503 | static int imsm_create_metadata_update_for_migration( | |
11504 | struct supertype *st, | |
11505 | struct geo_params *geo, | |
11506 | struct imsm_update_reshape_migration **updatep) | |
11507 | { | |
11508 | struct intel_super *super = st->sb; | |
594dc1b8 | 11509 | int update_memory_size; |
756a15f3 | 11510 | int current_chunk_size; |
594dc1b8 | 11511 | struct imsm_update_reshape_migration *u; |
756a15f3 MG |
11512 | struct imsm_dev *dev = get_imsm_dev(super, super->current_vol); |
11513 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
48c5303a PC |
11514 | int previous_level = -1; |
11515 | ||
1ade5cc1 | 11516 | dprintf("(enter) New Level = %i\n", geo->level); |
48c5303a PC |
11517 | |
11518 | /* size of all update data without anchor */ | |
11519 | update_memory_size = sizeof(struct imsm_update_reshape_migration); | |
11520 | ||
503975b9 | 11521 | u = xcalloc(1, update_memory_size); |
48c5303a PC |
11522 | u->type = update_reshape_migration; |
11523 | u->subdev = super->current_vol; | |
11524 | u->new_level = geo->level; | |
11525 | u->new_layout = geo->layout; | |
11526 | u->new_raid_disks = u->old_raid_disks = geo->raid_disks; | |
11527 | u->new_disks[0] = -1; | |
4bba0439 | 11528 | u->new_chunksize = -1; |
48c5303a | 11529 | |
756a15f3 | 11530 | current_chunk_size = __le16_to_cpu(map->blocks_per_strip) / 2; |
48c5303a | 11531 | |
756a15f3 MG |
11532 | if (geo->chunksize != current_chunk_size) { |
11533 | u->new_chunksize = geo->chunksize / 1024; | |
11534 | dprintf("imsm: chunk size change from %i to %i\n", | |
11535 | current_chunk_size, u->new_chunksize); | |
48c5303a | 11536 | } |
756a15f3 MG |
11537 | previous_level = map->raid_level; |
11538 | ||
089f9d79 | 11539 | if (geo->level == 5 && previous_level == 0) { |
48c5303a PC |
11540 | struct mdinfo *spares = NULL; |
11541 | ||
11542 | u->new_raid_disks++; | |
11543 | spares = get_spares_for_grow(st); | |
089f9d79 | 11544 | if (spares == NULL || spares->array.spare_disks < 1) { |
48c5303a PC |
11545 | free(u); |
11546 | sysfs_free(spares); | |
11547 | update_memory_size = 0; | |
565cc99e | 11548 | pr_err("cannot get spare device for requested migration\n"); |
48c5303a PC |
11549 | return 0; |
11550 | } | |
11551 | sysfs_free(spares); | |
11552 | } | |
11553 | dprintf("imsm: reshape update preparation : OK\n"); | |
11554 | *updatep = u; | |
11555 | ||
11556 | return update_memory_size; | |
11557 | } | |
11558 | ||
8dd70bce AK |
11559 | static void imsm_update_metadata_locally(struct supertype *st, |
11560 | void *buf, int len) | |
11561 | { | |
11562 | struct metadata_update mu; | |
11563 | ||
11564 | mu.buf = buf; | |
11565 | mu.len = len; | |
11566 | mu.space = NULL; | |
11567 | mu.space_list = NULL; | |
11568 | mu.next = NULL; | |
5fe6f031 N |
11569 | if (imsm_prepare_update(st, &mu)) |
11570 | imsm_process_update(st, &mu); | |
8dd70bce AK |
11571 | |
11572 | while (mu.space_list) { | |
11573 | void **space = mu.space_list; | |
11574 | mu.space_list = *space; | |
11575 | free(space); | |
11576 | } | |
11577 | } | |
78b10e66 | 11578 | |
471bceb6 | 11579 | /*************************************************************************** |
694575e7 | 11580 | * Function: imsm_analyze_change |
471bceb6 | 11581 | * Description: Function analyze change for single volume |
1011e834 | 11582 | * and validate if transition is supported |
fbf3d202 AK |
11583 | * Parameters: Geometry parameters, supertype structure, |
11584 | * metadata change direction (apply/rollback) | |
694575e7 | 11585 | * Returns: Operation type code on success, -1 if fail |
471bceb6 KW |
11586 | ****************************************************************************/ |
11587 | enum imsm_reshape_type imsm_analyze_change(struct supertype *st, | |
fbf3d202 AK |
11588 | struct geo_params *geo, |
11589 | int direction) | |
694575e7 | 11590 | { |
471bceb6 KW |
11591 | struct mdinfo info; |
11592 | int change = -1; | |
11593 | int check_devs = 0; | |
c21e737b | 11594 | int chunk; |
67a2db32 AK |
11595 | /* number of added/removed disks in operation result */ |
11596 | int devNumChange = 0; | |
11597 | /* imsm compatible layout value for array geometry verification */ | |
11598 | int imsm_layout = -1; | |
7abc9871 AK |
11599 | int data_disks; |
11600 | struct imsm_dev *dev; | |
9529d343 | 11601 | struct imsm_map *map; |
7abc9871 | 11602 | struct intel_super *super; |
d04f65f4 | 11603 | unsigned long long current_size; |
65d38cca | 11604 | unsigned long long free_size; |
d04f65f4 | 11605 | unsigned long long max_size; |
6d4d9ab2 | 11606 | imsm_status_t rv; |
471bceb6 KW |
11607 | |
11608 | getinfo_super_imsm_volume(st, &info, NULL); | |
089f9d79 JS |
11609 | if (geo->level != info.array.level && geo->level >= 0 && |
11610 | geo->level != UnSet) { | |
471bceb6 KW |
11611 | switch (info.array.level) { |
11612 | case 0: | |
11613 | if (geo->level == 5) { | |
b5347799 | 11614 | change = CH_MIGRATION; |
e13ce846 | 11615 | if (geo->layout != ALGORITHM_LEFT_ASYMMETRIC) { |
7a862a02 | 11616 | pr_err("Error. Requested Layout not supported (left-asymmetric layout is supported only)!\n"); |
e13ce846 AK |
11617 | change = -1; |
11618 | goto analyse_change_exit; | |
11619 | } | |
67a2db32 | 11620 | imsm_layout = geo->layout; |
471bceb6 | 11621 | check_devs = 1; |
e91a3bad LM |
11622 | devNumChange = 1; /* parity disk added */ |
11623 | } else if (geo->level == 10) { | |
471bceb6 KW |
11624 | change = CH_TAKEOVER; |
11625 | check_devs = 1; | |
e91a3bad | 11626 | devNumChange = 2; /* two mirrors added */ |
67a2db32 | 11627 | imsm_layout = 0x102; /* imsm supported layout */ |
471bceb6 | 11628 | } |
dfe77a9e KW |
11629 | break; |
11630 | case 1: | |
471bceb6 KW |
11631 | case 10: |
11632 | if (geo->level == 0) { | |
11633 | change = CH_TAKEOVER; | |
11634 | check_devs = 1; | |
e91a3bad | 11635 | devNumChange = -(geo->raid_disks/2); |
67a2db32 | 11636 | imsm_layout = 0; /* imsm raid0 layout */ |
471bceb6 KW |
11637 | } |
11638 | break; | |
11639 | } | |
11640 | if (change == -1) { | |
7a862a02 | 11641 | pr_err("Error. Level Migration from %d to %d not supported!\n", |
e7b84f9d | 11642 | info.array.level, geo->level); |
471bceb6 KW |
11643 | goto analyse_change_exit; |
11644 | } | |
11645 | } else | |
11646 | geo->level = info.array.level; | |
11647 | ||
089f9d79 JS |
11648 | if (geo->layout != info.array.layout && |
11649 | (geo->layout != UnSet && geo->layout != -1)) { | |
b5347799 | 11650 | change = CH_MIGRATION; |
089f9d79 JS |
11651 | if (info.array.layout == 0 && info.array.level == 5 && |
11652 | geo->layout == 5) { | |
471bceb6 | 11653 | /* reshape 5 -> 4 */ |
089f9d79 JS |
11654 | } else if (info.array.layout == 5 && info.array.level == 5 && |
11655 | geo->layout == 0) { | |
471bceb6 KW |
11656 | /* reshape 4 -> 5 */ |
11657 | geo->layout = 0; | |
11658 | geo->level = 5; | |
11659 | } else { | |
7a862a02 | 11660 | pr_err("Error. Layout Migration from %d to %d not supported!\n", |
e7b84f9d | 11661 | info.array.layout, geo->layout); |
471bceb6 KW |
11662 | change = -1; |
11663 | goto analyse_change_exit; | |
11664 | } | |
67a2db32 | 11665 | } else { |
471bceb6 | 11666 | geo->layout = info.array.layout; |
67a2db32 AK |
11667 | if (imsm_layout == -1) |
11668 | imsm_layout = info.array.layout; | |
11669 | } | |
471bceb6 | 11670 | |
089f9d79 JS |
11671 | if (geo->chunksize > 0 && geo->chunksize != UnSet && |
11672 | geo->chunksize != info.array.chunk_size) { | |
2d2b0eb7 MD |
11673 | if (info.array.level == 10) { |
11674 | pr_err("Error. Chunk size change for RAID 10 is not supported.\n"); | |
11675 | change = -1; | |
11676 | goto analyse_change_exit; | |
1e9b2c3f PB |
11677 | } else if (info.component_size % (geo->chunksize/512)) { |
11678 | pr_err("New chunk size (%dK) does not evenly divide device size (%lluk). Aborting...\n", | |
11679 | geo->chunksize/1024, info.component_size/2); | |
11680 | change = -1; | |
11681 | goto analyse_change_exit; | |
2d2b0eb7 | 11682 | } |
b5347799 | 11683 | change = CH_MIGRATION; |
2d2b0eb7 | 11684 | } else { |
471bceb6 | 11685 | geo->chunksize = info.array.chunk_size; |
2d2b0eb7 | 11686 | } |
471bceb6 | 11687 | |
c21e737b | 11688 | chunk = geo->chunksize / 1024; |
7abc9871 AK |
11689 | |
11690 | super = st->sb; | |
11691 | dev = get_imsm_dev(super, super->current_vol); | |
9529d343 MD |
11692 | map = get_imsm_map(dev, MAP_0); |
11693 | data_disks = imsm_num_data_members(map); | |
c41e00b2 | 11694 | /* compute current size per disk member |
7abc9871 | 11695 | */ |
c41e00b2 AK |
11696 | current_size = info.custom_array_size / data_disks; |
11697 | ||
089f9d79 | 11698 | if (geo->size > 0 && geo->size != MAX_SIZE) { |
c41e00b2 AK |
11699 | /* align component size |
11700 | */ | |
3e684231 | 11701 | geo->size = imsm_component_size_alignment_check( |
c41e00b2 | 11702 | get_imsm_raid_level(dev->vol.map), |
f36a9ecd | 11703 | chunk * 1024, super->sector_size, |
c41e00b2 | 11704 | geo->size * 2); |
65d0b4ce | 11705 | if (geo->size == 0) { |
7a862a02 | 11706 | pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is 0).\n", |
65d0b4ce LD |
11707 | current_size); |
11708 | goto analyse_change_exit; | |
11709 | } | |
c41e00b2 | 11710 | } |
7abc9871 | 11711 | |
089f9d79 | 11712 | if (current_size != geo->size && geo->size > 0) { |
7abc9871 | 11713 | if (change != -1) { |
7a862a02 | 11714 | pr_err("Error. Size change should be the only one at a time.\n"); |
7abc9871 AK |
11715 | change = -1; |
11716 | goto analyse_change_exit; | |
11717 | } | |
11718 | if ((super->current_vol + 1) != super->anchor->num_raid_devs) { | |
7a862a02 | 11719 | pr_err("Error. The last volume in container can be expanded only (%i/%s).\n", |
4dd2df09 | 11720 | super->current_vol, st->devnm); |
7abc9871 AK |
11721 | goto analyse_change_exit; |
11722 | } | |
65d38cca LD |
11723 | /* check the maximum available size |
11724 | */ | |
6d4d9ab2 MT |
11725 | rv = imsm_get_free_size(super, dev->vol.map->num_members, |
11726 | 0, chunk, &free_size); | |
11727 | ||
11728 | if (rv != IMSM_STATUS_OK) | |
65d38cca LD |
11729 | /* Cannot find maximum available space |
11730 | */ | |
11731 | max_size = 0; | |
11732 | else { | |
11733 | max_size = free_size + current_size; | |
11734 | /* align component size | |
11735 | */ | |
3e684231 | 11736 | max_size = imsm_component_size_alignment_check( |
65d38cca | 11737 | get_imsm_raid_level(dev->vol.map), |
f36a9ecd | 11738 | chunk * 1024, super->sector_size, |
65d38cca LD |
11739 | max_size); |
11740 | } | |
d04f65f4 | 11741 | if (geo->size == MAX_SIZE) { |
b130333f AK |
11742 | /* requested size change to the maximum available size |
11743 | */ | |
65d38cca | 11744 | if (max_size == 0) { |
7a862a02 | 11745 | pr_err("Error. Cannot find maximum available space.\n"); |
b130333f AK |
11746 | change = -1; |
11747 | goto analyse_change_exit; | |
65d38cca LD |
11748 | } else |
11749 | geo->size = max_size; | |
c41e00b2 | 11750 | } |
b130333f | 11751 | |
681b7ae2 | 11752 | if (direction == ROLLBACK_METADATA_CHANGES) { |
fbf3d202 AK |
11753 | /* accept size for rollback only |
11754 | */ | |
11755 | } else { | |
11756 | /* round size due to metadata compatibility | |
11757 | */ | |
11758 | geo->size = (geo->size >> SECT_PER_MB_SHIFT) | |
11759 | << SECT_PER_MB_SHIFT; | |
11760 | dprintf("Prepare update for size change to %llu\n", | |
11761 | geo->size ); | |
11762 | if (current_size >= geo->size) { | |
7a862a02 | 11763 | pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is %llu).\n", |
e7b84f9d | 11764 | current_size, geo->size); |
fbf3d202 AK |
11765 | goto analyse_change_exit; |
11766 | } | |
65d38cca | 11767 | if (max_size && geo->size > max_size) { |
7a862a02 | 11768 | pr_err("Error. Requested size is larger than maximum available size (maximum available size is %llu, requested size /rounded/ is %llu).\n", |
e7b84f9d | 11769 | max_size, geo->size); |
65d38cca LD |
11770 | goto analyse_change_exit; |
11771 | } | |
7abc9871 AK |
11772 | } |
11773 | geo->size *= data_disks; | |
11774 | geo->raid_disks = dev->vol.map->num_members; | |
11775 | change = CH_ARRAY_SIZE; | |
11776 | } | |
471bceb6 KW |
11777 | if (!validate_geometry_imsm(st, |
11778 | geo->level, | |
67a2db32 | 11779 | imsm_layout, |
e91a3bad | 11780 | geo->raid_disks + devNumChange, |
c21e737b | 11781 | &chunk, |
af4348dd | 11782 | geo->size, INVALID_SECTORS, |
5308f117 | 11783 | 0, 0, info.consistency_policy, 1)) |
471bceb6 KW |
11784 | change = -1; |
11785 | ||
11786 | if (check_devs) { | |
11787 | struct intel_super *super = st->sb; | |
11788 | struct imsm_super *mpb = super->anchor; | |
11789 | ||
11790 | if (mpb->num_raid_devs > 1) { | |
f1cc8ab9 LF |
11791 | pr_err("Error. Cannot perform operation on %s- for this operation " |
11792 | "it MUST be single array in container\n", geo->dev_name); | |
471bceb6 KW |
11793 | change = -1; |
11794 | } | |
11795 | } | |
11796 | ||
11797 | analyse_change_exit: | |
089f9d79 JS |
11798 | if (direction == ROLLBACK_METADATA_CHANGES && |
11799 | (change == CH_MIGRATION || change == CH_TAKEOVER)) { | |
7a862a02 | 11800 | dprintf("imsm: Metadata changes rollback is not supported for migration and takeover operations.\n"); |
fbf3d202 AK |
11801 | change = -1; |
11802 | } | |
471bceb6 | 11803 | return change; |
694575e7 KW |
11804 | } |
11805 | ||
bb025c2f KW |
11806 | int imsm_takeover(struct supertype *st, struct geo_params *geo) |
11807 | { | |
11808 | struct intel_super *super = st->sb; | |
11809 | struct imsm_update_takeover *u; | |
11810 | ||
503975b9 | 11811 | u = xmalloc(sizeof(struct imsm_update_takeover)); |
bb025c2f KW |
11812 | |
11813 | u->type = update_takeover; | |
11814 | u->subarray = super->current_vol; | |
11815 | ||
11816 | /* 10->0 transition */ | |
11817 | if (geo->level == 0) | |
11818 | u->direction = R10_TO_R0; | |
11819 | ||
0529c688 KW |
11820 | /* 0->10 transition */ |
11821 | if (geo->level == 10) | |
11822 | u->direction = R0_TO_R10; | |
11823 | ||
bb025c2f KW |
11824 | /* update metadata locally */ |
11825 | imsm_update_metadata_locally(st, u, | |
11826 | sizeof(struct imsm_update_takeover)); | |
11827 | /* and possibly remotely */ | |
11828 | if (st->update_tail) | |
11829 | append_metadata_update(st, u, | |
11830 | sizeof(struct imsm_update_takeover)); | |
11831 | else | |
11832 | free(u); | |
11833 | ||
11834 | return 0; | |
11835 | } | |
11836 | ||
895ffd99 MT |
11837 | /* Flush size update if size calculated by num_data_stripes is higher than |
11838 | * imsm_dev_size to eliminate differences during reshape. | |
11839 | * Mdmon will recalculate them correctly. | |
11840 | * If subarray index is not set then check whole container. | |
11841 | * Returns: | |
11842 | * 0 - no error occurred | |
11843 | * 1 - error detected | |
11844 | */ | |
11845 | static int imsm_fix_size_mismatch(struct supertype *st, int subarray_index) | |
11846 | { | |
11847 | struct intel_super *super = st->sb; | |
11848 | int tmp = super->current_vol; | |
11849 | int ret_val = 1; | |
11850 | int i; | |
11851 | ||
11852 | for (i = 0; i < super->anchor->num_raid_devs; i++) { | |
11853 | if (subarray_index >= 0 && i != subarray_index) | |
11854 | continue; | |
11855 | super->current_vol = i; | |
11856 | struct imsm_dev *dev = get_imsm_dev(super, super->current_vol); | |
11857 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
11858 | unsigned int disc_count = imsm_num_data_members(map); | |
11859 | struct geo_params geo; | |
11860 | struct imsm_update_size_change *update; | |
11861 | unsigned long long calc_size = per_dev_array_size(map) * disc_count; | |
11862 | unsigned long long d_size = imsm_dev_size(dev); | |
11863 | int u_size; | |
11864 | ||
42e02e61 | 11865 | if (calc_size == d_size) |
895ffd99 MT |
11866 | continue; |
11867 | ||
ff904202 MT |
11868 | /* There is a difference, confirm that imsm_dev_size is |
11869 | * smaller and push update. | |
895ffd99 | 11870 | */ |
ff904202 MT |
11871 | if (d_size > calc_size) { |
11872 | pr_err("imsm: dev size of subarray %d is incorrect\n", | |
11873 | i); | |
895ffd99 MT |
11874 | goto exit; |
11875 | } | |
11876 | memset(&geo, 0, sizeof(struct geo_params)); | |
11877 | geo.size = d_size; | |
11878 | u_size = imsm_create_metadata_update_for_size_change(st, &geo, | |
11879 | &update); | |
895ffd99 MT |
11880 | imsm_update_metadata_locally(st, update, u_size); |
11881 | if (st->update_tail) { | |
11882 | append_metadata_update(st, update, u_size); | |
11883 | flush_metadata_updates(st); | |
11884 | st->update_tail = &st->updates; | |
11885 | } else { | |
11886 | imsm_sync_metadata(st); | |
5ce5a15f | 11887 | free(update); |
895ffd99 MT |
11888 | } |
11889 | } | |
11890 | ret_val = 0; | |
11891 | exit: | |
11892 | super->current_vol = tmp; | |
11893 | return ret_val; | |
11894 | } | |
11895 | ||
d04f65f4 N |
11896 | static int imsm_reshape_super(struct supertype *st, unsigned long long size, |
11897 | int level, | |
78b10e66 | 11898 | int layout, int chunksize, int raid_disks, |
41784c88 | 11899 | int delta_disks, char *backup, char *dev, |
016e00f5 | 11900 | int direction, int verbose) |
78b10e66 | 11901 | { |
78b10e66 N |
11902 | int ret_val = 1; |
11903 | struct geo_params geo; | |
11904 | ||
1ade5cc1 | 11905 | dprintf("(enter)\n"); |
78b10e66 | 11906 | |
71204a50 | 11907 | memset(&geo, 0, sizeof(struct geo_params)); |
78b10e66 N |
11908 | |
11909 | geo.dev_name = dev; | |
4dd2df09 | 11910 | strcpy(geo.devnm, st->devnm); |
78b10e66 N |
11911 | geo.size = size; |
11912 | geo.level = level; | |
11913 | geo.layout = layout; | |
11914 | geo.chunksize = chunksize; | |
11915 | geo.raid_disks = raid_disks; | |
41784c88 AK |
11916 | if (delta_disks != UnSet) |
11917 | geo.raid_disks += delta_disks; | |
78b10e66 | 11918 | |
1ade5cc1 N |
11919 | dprintf("for level : %i\n", geo.level); |
11920 | dprintf("for raid_disks : %i\n", geo.raid_disks); | |
78b10e66 | 11921 | |
4dd2df09 | 11922 | if (strcmp(st->container_devnm, st->devnm) == 0) { |
694575e7 KW |
11923 | /* On container level we can only increase number of devices. */ |
11924 | dprintf("imsm: info: Container operation\n"); | |
78b10e66 | 11925 | int old_raid_disks = 0; |
6dc0be30 | 11926 | |
78b10e66 | 11927 | if (imsm_reshape_is_allowed_on_container( |
fbf3d202 | 11928 | st, &geo, &old_raid_disks, direction)) { |
78b10e66 N |
11929 | struct imsm_update_reshape *u = NULL; |
11930 | int len; | |
11931 | ||
895ffd99 MT |
11932 | if (imsm_fix_size_mismatch(st, -1)) { |
11933 | dprintf("imsm: Cannot fix size mismatch\n"); | |
11934 | goto exit_imsm_reshape_super; | |
11935 | } | |
11936 | ||
78b10e66 N |
11937 | len = imsm_create_metadata_update_for_reshape( |
11938 | st, &geo, old_raid_disks, &u); | |
11939 | ||
ed08d51c AK |
11940 | if (len <= 0) { |
11941 | dprintf("imsm: Cannot prepare update\n"); | |
11942 | goto exit_imsm_reshape_super; | |
11943 | } | |
11944 | ||
8dd70bce AK |
11945 | ret_val = 0; |
11946 | /* update metadata locally */ | |
11947 | imsm_update_metadata_locally(st, u, len); | |
11948 | /* and possibly remotely */ | |
11949 | if (st->update_tail) | |
11950 | append_metadata_update(st, u, len); | |
11951 | else | |
ed08d51c | 11952 | free(u); |
8dd70bce | 11953 | |
694575e7 | 11954 | } else { |
7a862a02 | 11955 | pr_err("(imsm) Operation is not allowed on this container\n"); |
694575e7 KW |
11956 | } |
11957 | } else { | |
11958 | /* On volume level we support following operations | |
471bceb6 KW |
11959 | * - takeover: raid10 -> raid0; raid0 -> raid10 |
11960 | * - chunk size migration | |
11961 | * - migration: raid5 -> raid0; raid0 -> raid5 | |
11962 | */ | |
11963 | struct intel_super *super = st->sb; | |
11964 | struct intel_dev *dev = super->devlist; | |
4dd2df09 | 11965 | int change; |
694575e7 | 11966 | dprintf("imsm: info: Volume operation\n"); |
471bceb6 KW |
11967 | /* find requested device */ |
11968 | while (dev) { | |
1011e834 | 11969 | char *devnm = |
4dd2df09 N |
11970 | imsm_find_array_devnm_by_subdev( |
11971 | dev->index, st->container_devnm); | |
11972 | if (devnm && strcmp(devnm, geo.devnm) == 0) | |
471bceb6 KW |
11973 | break; |
11974 | dev = dev->next; | |
11975 | } | |
11976 | if (dev == NULL) { | |
4dd2df09 N |
11977 | pr_err("Cannot find %s (%s) subarray\n", |
11978 | geo.dev_name, geo.devnm); | |
471bceb6 KW |
11979 | goto exit_imsm_reshape_super; |
11980 | } | |
11981 | super->current_vol = dev->index; | |
fbf3d202 | 11982 | change = imsm_analyze_change(st, &geo, direction); |
694575e7 | 11983 | switch (change) { |
471bceb6 | 11984 | case CH_TAKEOVER: |
bb025c2f | 11985 | ret_val = imsm_takeover(st, &geo); |
694575e7 | 11986 | break; |
48c5303a PC |
11987 | case CH_MIGRATION: { |
11988 | struct imsm_update_reshape_migration *u = NULL; | |
11989 | int len = | |
11990 | imsm_create_metadata_update_for_migration( | |
11991 | st, &geo, &u); | |
11992 | if (len < 1) { | |
7a862a02 | 11993 | dprintf("imsm: Cannot prepare update\n"); |
48c5303a PC |
11994 | break; |
11995 | } | |
471bceb6 | 11996 | ret_val = 0; |
48c5303a PC |
11997 | /* update metadata locally */ |
11998 | imsm_update_metadata_locally(st, u, len); | |
11999 | /* and possibly remotely */ | |
12000 | if (st->update_tail) | |
12001 | append_metadata_update(st, u, len); | |
12002 | else | |
12003 | free(u); | |
12004 | } | |
12005 | break; | |
7abc9871 | 12006 | case CH_ARRAY_SIZE: { |
f3871fdc AK |
12007 | struct imsm_update_size_change *u = NULL; |
12008 | int len = | |
12009 | imsm_create_metadata_update_for_size_change( | |
12010 | st, &geo, &u); | |
12011 | if (len < 1) { | |
7a862a02 | 12012 | dprintf("imsm: Cannot prepare update\n"); |
f3871fdc AK |
12013 | break; |
12014 | } | |
12015 | ret_val = 0; | |
12016 | /* update metadata locally */ | |
12017 | imsm_update_metadata_locally(st, u, len); | |
12018 | /* and possibly remotely */ | |
12019 | if (st->update_tail) | |
12020 | append_metadata_update(st, u, len); | |
12021 | else | |
12022 | free(u); | |
7abc9871 AK |
12023 | } |
12024 | break; | |
471bceb6 KW |
12025 | default: |
12026 | ret_val = 1; | |
694575e7 | 12027 | } |
694575e7 | 12028 | } |
78b10e66 | 12029 | |
ed08d51c | 12030 | exit_imsm_reshape_super: |
78b10e66 N |
12031 | dprintf("imsm: reshape_super Exit code = %i\n", ret_val); |
12032 | return ret_val; | |
12033 | } | |
2cda7640 | 12034 | |
0febb20c AO |
12035 | #define COMPLETED_OK 0 |
12036 | #define COMPLETED_NONE 1 | |
12037 | #define COMPLETED_DELAYED 2 | |
12038 | ||
12039 | static int read_completed(int fd, unsigned long long *val) | |
12040 | { | |
12041 | int ret; | |
12042 | char buf[50]; | |
12043 | ||
12044 | ret = sysfs_fd_get_str(fd, buf, 50); | |
12045 | if (ret < 0) | |
12046 | return ret; | |
12047 | ||
12048 | ret = COMPLETED_OK; | |
12049 | if (strncmp(buf, "none", 4) == 0) { | |
12050 | ret = COMPLETED_NONE; | |
12051 | } else if (strncmp(buf, "delayed", 7) == 0) { | |
12052 | ret = COMPLETED_DELAYED; | |
12053 | } else { | |
12054 | char *ep; | |
12055 | *val = strtoull(buf, &ep, 0); | |
12056 | if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' ')) | |
12057 | ret = -1; | |
12058 | } | |
12059 | return ret; | |
12060 | } | |
12061 | ||
eee67a47 AK |
12062 | /******************************************************************************* |
12063 | * Function: wait_for_reshape_imsm | |
12064 | * Description: Function writes new sync_max value and waits until | |
12065 | * reshape process reach new position | |
12066 | * Parameters: | |
12067 | * sra : general array info | |
eee67a47 AK |
12068 | * ndata : number of disks in new array's layout |
12069 | * Returns: | |
12070 | * 0 : success, | |
12071 | * 1 : there is no reshape in progress, | |
12072 | * -1 : fail | |
12073 | ******************************************************************************/ | |
ae9f01f8 | 12074 | int wait_for_reshape_imsm(struct mdinfo *sra, int ndata) |
eee67a47 | 12075 | { |
85ca499c | 12076 | int fd = sysfs_get_fd(sra, NULL, "sync_completed"); |
df2647fa | 12077 | int retry = 3; |
eee67a47 | 12078 | unsigned long long completed; |
ae9f01f8 AK |
12079 | /* to_complete : new sync_max position */ |
12080 | unsigned long long to_complete = sra->reshape_progress; | |
12081 | unsigned long long position_to_set = to_complete / ndata; | |
eee67a47 | 12082 | |
4389ce73 | 12083 | if (!is_fd_valid(fd)) { |
1ade5cc1 | 12084 | dprintf("cannot open reshape_position\n"); |
eee67a47 | 12085 | return 1; |
ae9f01f8 | 12086 | } |
eee67a47 | 12087 | |
df2647fa PB |
12088 | do { |
12089 | if (sysfs_fd_get_ll(fd, &completed) < 0) { | |
12090 | if (!retry) { | |
12091 | dprintf("cannot read reshape_position (no reshape in progres)\n"); | |
12092 | close(fd); | |
12093 | return 1; | |
12094 | } | |
239b3cc0 | 12095 | sleep_for(0, MSEC_TO_NSEC(30), true); |
df2647fa PB |
12096 | } else |
12097 | break; | |
12098 | } while (retry--); | |
eee67a47 | 12099 | |
85ca499c | 12100 | if (completed > position_to_set) { |
1ade5cc1 | 12101 | dprintf("wrong next position to set %llu (%llu)\n", |
85ca499c | 12102 | to_complete, position_to_set); |
ae9f01f8 AK |
12103 | close(fd); |
12104 | return -1; | |
12105 | } | |
12106 | dprintf("Position set: %llu\n", position_to_set); | |
12107 | if (sysfs_set_num(sra, NULL, "sync_max", | |
12108 | position_to_set) != 0) { | |
1ade5cc1 | 12109 | dprintf("cannot set reshape position to %llu\n", |
ae9f01f8 AK |
12110 | position_to_set); |
12111 | close(fd); | |
12112 | return -1; | |
eee67a47 AK |
12113 | } |
12114 | ||
eee67a47 | 12115 | do { |
0febb20c | 12116 | int rc; |
eee67a47 | 12117 | char action[20]; |
5ff3a780 | 12118 | int timeout = 3000; |
0febb20c | 12119 | |
5ff3a780 | 12120 | sysfs_wait(fd, &timeout); |
a47e44fb AK |
12121 | if (sysfs_get_str(sra, NULL, "sync_action", |
12122 | action, 20) > 0 && | |
d7d3809a | 12123 | strncmp(action, "reshape", 7) != 0) { |
b2be2b62 AO |
12124 | if (strncmp(action, "idle", 4) == 0) |
12125 | break; | |
d7d3809a AP |
12126 | close(fd); |
12127 | return -1; | |
12128 | } | |
0febb20c AO |
12129 | |
12130 | rc = read_completed(fd, &completed); | |
12131 | if (rc < 0) { | |
1ade5cc1 | 12132 | dprintf("cannot read reshape_position (in loop)\n"); |
eee67a47 AK |
12133 | close(fd); |
12134 | return 1; | |
0febb20c AO |
12135 | } else if (rc == COMPLETED_NONE) |
12136 | break; | |
85ca499c | 12137 | } while (completed < position_to_set); |
b2be2b62 | 12138 | |
eee67a47 AK |
12139 | close(fd); |
12140 | return 0; | |
eee67a47 AK |
12141 | } |
12142 | ||
b915c95f AK |
12143 | /******************************************************************************* |
12144 | * Function: check_degradation_change | |
12145 | * Description: Check that array hasn't become failed. | |
12146 | * Parameters: | |
12147 | * info : for sysfs access | |
12148 | * sources : source disks descriptors | |
12149 | * degraded: previous degradation level | |
12150 | * Returns: | |
12151 | * degradation level | |
12152 | ******************************************************************************/ | |
12153 | int check_degradation_change(struct mdinfo *info, | |
12154 | int *sources, | |
12155 | int degraded) | |
12156 | { | |
12157 | unsigned long long new_degraded; | |
e1993023 LD |
12158 | int rv; |
12159 | ||
12160 | rv = sysfs_get_ll(info, NULL, "degraded", &new_degraded); | |
089f9d79 | 12161 | if (rv == -1 || (new_degraded != (unsigned long long)degraded)) { |
b915c95f AK |
12162 | /* check each device to ensure it is still working */ |
12163 | struct mdinfo *sd; | |
12164 | new_degraded = 0; | |
12165 | for (sd = info->devs ; sd ; sd = sd->next) { | |
12166 | if (sd->disk.state & (1<<MD_DISK_FAULTY)) | |
12167 | continue; | |
12168 | if (sd->disk.state & (1<<MD_DISK_SYNC)) { | |
cf52eff5 | 12169 | char sbuf[100]; |
4389ce73 | 12170 | int raid_disk = sd->disk.raid_disk; |
cf52eff5 | 12171 | |
b915c95f | 12172 | if (sysfs_get_str(info, |
cf52eff5 | 12173 | sd, "state", sbuf, sizeof(sbuf)) < 0 || |
b915c95f AK |
12174 | strstr(sbuf, "faulty") || |
12175 | strstr(sbuf, "in_sync") == NULL) { | |
12176 | /* this device is dead */ | |
12177 | sd->disk.state = (1<<MD_DISK_FAULTY); | |
4389ce73 MT |
12178 | if (raid_disk >= 0) |
12179 | close_fd(&sources[raid_disk]); | |
b915c95f AK |
12180 | new_degraded++; |
12181 | } | |
12182 | } | |
12183 | } | |
12184 | } | |
12185 | ||
12186 | return new_degraded; | |
12187 | } | |
12188 | ||
10f22854 AK |
12189 | /******************************************************************************* |
12190 | * Function: imsm_manage_reshape | |
12191 | * Description: Function finds array under reshape and it manages reshape | |
12192 | * process. It creates stripes backups (if required) and sets | |
942e1cdb | 12193 | * checkpoints. |
10f22854 AK |
12194 | * Parameters: |
12195 | * afd : Backup handle (nattive) - not used | |
12196 | * sra : general array info | |
12197 | * reshape : reshape parameters - not used | |
12198 | * st : supertype structure | |
12199 | * blocks : size of critical section [blocks] | |
12200 | * fds : table of source device descriptor | |
12201 | * offsets : start of array (offest per devices) | |
12202 | * dests : not used | |
12203 | * destfd : table of destination device descriptor | |
12204 | * destoffsets : table of destination offsets (per device) | |
12205 | * Returns: | |
12206 | * 1 : success, reshape is done | |
12207 | * 0 : fail | |
12208 | ******************************************************************************/ | |
999b4972 N |
12209 | static int imsm_manage_reshape( |
12210 | int afd, struct mdinfo *sra, struct reshape *reshape, | |
10f22854 | 12211 | struct supertype *st, unsigned long backup_blocks, |
999b4972 N |
12212 | int *fds, unsigned long long *offsets, |
12213 | int dests, int *destfd, unsigned long long *destoffsets) | |
12214 | { | |
10f22854 AK |
12215 | int ret_val = 0; |
12216 | struct intel_super *super = st->sb; | |
594dc1b8 | 12217 | struct intel_dev *dv; |
de44e46f | 12218 | unsigned int sector_size = super->sector_size; |
10f22854 | 12219 | struct imsm_dev *dev = NULL; |
9529d343 | 12220 | struct imsm_map *map_src, *map_dest; |
10f22854 AK |
12221 | int migr_vol_qan = 0; |
12222 | int ndata, odata; /* [bytes] */ | |
12223 | int chunk; /* [bytes] */ | |
12224 | struct migr_record *migr_rec; | |
12225 | char *buf = NULL; | |
12226 | unsigned int buf_size; /* [bytes] */ | |
12227 | unsigned long long max_position; /* array size [bytes] */ | |
12228 | unsigned long long next_step; /* [blocks]/[bytes] */ | |
12229 | unsigned long long old_data_stripe_length; | |
10f22854 AK |
12230 | unsigned long long start_src; /* [bytes] */ |
12231 | unsigned long long start; /* [bytes] */ | |
12232 | unsigned long long start_buf_shift; /* [bytes] */ | |
b915c95f | 12233 | int degraded = 0; |
ab724b98 | 12234 | int source_layout = 0; |
895ffd99 | 12235 | int subarray_index = -1; |
10f22854 | 12236 | |
79a16a9b JS |
12237 | if (!sra) |
12238 | return ret_val; | |
12239 | ||
12240 | if (!fds || !offsets) | |
10f22854 AK |
12241 | goto abort; |
12242 | ||
12243 | /* Find volume during the reshape */ | |
12244 | for (dv = super->devlist; dv; dv = dv->next) { | |
fc54fe7a JS |
12245 | if (dv->dev->vol.migr_type == MIGR_GEN_MIGR && |
12246 | dv->dev->vol.migr_state == 1) { | |
10f22854 AK |
12247 | dev = dv->dev; |
12248 | migr_vol_qan++; | |
895ffd99 | 12249 | subarray_index = dv->index; |
10f22854 AK |
12250 | } |
12251 | } | |
12252 | /* Only one volume can migrate at the same time */ | |
12253 | if (migr_vol_qan != 1) { | |
676e87a8 | 12254 | pr_err("%s", migr_vol_qan ? |
10f22854 AK |
12255 | "Number of migrating volumes greater than 1\n" : |
12256 | "There is no volume during migrationg\n"); | |
12257 | goto abort; | |
12258 | } | |
12259 | ||
9529d343 | 12260 | map_dest = get_imsm_map(dev, MAP_0); |
238c0a71 | 12261 | map_src = get_imsm_map(dev, MAP_1); |
10f22854 AK |
12262 | if (map_src == NULL) |
12263 | goto abort; | |
10f22854 | 12264 | |
9529d343 MD |
12265 | ndata = imsm_num_data_members(map_dest); |
12266 | odata = imsm_num_data_members(map_src); | |
10f22854 | 12267 | |
7b1ab482 | 12268 | chunk = __le16_to_cpu(map_src->blocks_per_strip) * 512; |
10f22854 AK |
12269 | old_data_stripe_length = odata * chunk; |
12270 | ||
12271 | migr_rec = super->migr_rec; | |
12272 | ||
10f22854 AK |
12273 | /* initialize migration record for start condition */ |
12274 | if (sra->reshape_progress == 0) | |
12275 | init_migr_record_imsm(st, dev, sra); | |
b2c59438 AK |
12276 | else { |
12277 | if (__le32_to_cpu(migr_rec->rec_status) != UNIT_SRC_NORMAL) { | |
7a862a02 | 12278 | dprintf("imsm: cannot restart migration when data are present in copy area.\n"); |
b2c59438 AK |
12279 | goto abort; |
12280 | } | |
6a75c8ca AK |
12281 | /* Save checkpoint to update migration record for current |
12282 | * reshape position (in md). It can be farther than current | |
12283 | * reshape position in metadata. | |
12284 | */ | |
12285 | if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL) == 1) { | |
12286 | /* ignore error == 2, this can mean end of reshape here | |
12287 | */ | |
7a862a02 | 12288 | dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL, initial save)\n"); |
6a75c8ca AK |
12289 | goto abort; |
12290 | } | |
b2c59438 | 12291 | } |
10f22854 AK |
12292 | |
12293 | /* size for data */ | |
12294 | buf_size = __le32_to_cpu(migr_rec->blocks_per_unit) * 512; | |
12295 | /* extend buffer size for parity disk */ | |
12296 | buf_size += __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512; | |
3e684231 | 12297 | /* add space for stripe alignment */ |
10f22854 | 12298 | buf_size += old_data_stripe_length; |
de44e46f PB |
12299 | if (posix_memalign((void **)&buf, MAX_SECTOR_SIZE, buf_size)) { |
12300 | dprintf("imsm: Cannot allocate checkpoint buffer\n"); | |
10f22854 AK |
12301 | goto abort; |
12302 | } | |
12303 | ||
3ef4403c | 12304 | max_position = sra->component_size * ndata; |
68eb8bc6 | 12305 | source_layout = imsm_level_to_layout(map_src->raid_level); |
10f22854 | 12306 | |
9f421827 PB |
12307 | while (current_migr_unit(migr_rec) < |
12308 | get_num_migr_units(migr_rec)) { | |
10f22854 AK |
12309 | /* current reshape position [blocks] */ |
12310 | unsigned long long current_position = | |
12311 | __le32_to_cpu(migr_rec->blocks_per_unit) | |
9f421827 | 12312 | * current_migr_unit(migr_rec); |
10f22854 AK |
12313 | unsigned long long border; |
12314 | ||
b915c95f AK |
12315 | /* Check that array hasn't become failed. |
12316 | */ | |
12317 | degraded = check_degradation_change(sra, fds, degraded); | |
12318 | if (degraded > 1) { | |
7a862a02 | 12319 | dprintf("imsm: Abort reshape due to degradation level (%i)\n", degraded); |
b915c95f AK |
12320 | goto abort; |
12321 | } | |
12322 | ||
10f22854 AK |
12323 | next_step = __le32_to_cpu(migr_rec->blocks_per_unit); |
12324 | ||
12325 | if ((current_position + next_step) > max_position) | |
12326 | next_step = max_position - current_position; | |
12327 | ||
92144abf | 12328 | start = current_position * 512; |
10f22854 | 12329 | |
942e1cdb | 12330 | /* align reading start to old geometry */ |
10f22854 AK |
12331 | start_buf_shift = start % old_data_stripe_length; |
12332 | start_src = start - start_buf_shift; | |
12333 | ||
12334 | border = (start_src / odata) - (start / ndata); | |
12335 | border /= 512; | |
12336 | if (border <= __le32_to_cpu(migr_rec->dest_depth_per_unit)) { | |
12337 | /* save critical stripes to buf | |
12338 | * start - start address of current unit | |
12339 | * to backup [bytes] | |
12340 | * start_src - start address of current unit | |
12341 | * to backup alligned to source array | |
12342 | * [bytes] | |
12343 | */ | |
594dc1b8 | 12344 | unsigned long long next_step_filler; |
10f22854 AK |
12345 | unsigned long long copy_length = next_step * 512; |
12346 | ||
12347 | /* allign copy area length to stripe in old geometry */ | |
12348 | next_step_filler = ((copy_length + start_buf_shift) | |
12349 | % old_data_stripe_length); | |
12350 | if (next_step_filler) | |
12351 | next_step_filler = (old_data_stripe_length | |
12352 | - next_step_filler); | |
7a862a02 | 12353 | 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 |
12354 | start, start_src, copy_length, |
12355 | start_buf_shift, next_step_filler); | |
12356 | ||
12357 | if (save_stripes(fds, offsets, map_src->num_members, | |
ab724b98 AK |
12358 | chunk, map_src->raid_level, |
12359 | source_layout, 0, NULL, start_src, | |
10f22854 AK |
12360 | copy_length + |
12361 | next_step_filler + start_buf_shift, | |
12362 | buf)) { | |
7a862a02 | 12363 | dprintf("imsm: Cannot save stripes to buffer\n"); |
10f22854 AK |
12364 | goto abort; |
12365 | } | |
12366 | /* Convert data to destination format and store it | |
12367 | * in backup general migration area | |
12368 | */ | |
12369 | if (save_backup_imsm(st, dev, sra, | |
aea93171 | 12370 | buf + start_buf_shift, copy_length)) { |
7a862a02 | 12371 | dprintf("imsm: Cannot save stripes to target devices\n"); |
10f22854 AK |
12372 | goto abort; |
12373 | } | |
12374 | if (save_checkpoint_imsm(st, sra, | |
12375 | UNIT_SRC_IN_CP_AREA)) { | |
7a862a02 | 12376 | dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_IN_CP_AREA)\n"); |
10f22854 AK |
12377 | goto abort; |
12378 | } | |
8016a6d4 AK |
12379 | } else { |
12380 | /* set next step to use whole border area */ | |
12381 | border /= next_step; | |
12382 | if (border > 1) | |
12383 | next_step *= border; | |
10f22854 AK |
12384 | } |
12385 | /* When data backed up, checkpoint stored, | |
12386 | * kick the kernel to reshape unit of data | |
12387 | */ | |
12388 | next_step = next_step + sra->reshape_progress; | |
8016a6d4 AK |
12389 | /* limit next step to array max position */ |
12390 | if (next_step > max_position) | |
12391 | next_step = max_position; | |
10f22854 AK |
12392 | sysfs_set_num(sra, NULL, "suspend_lo", sra->reshape_progress); |
12393 | sysfs_set_num(sra, NULL, "suspend_hi", next_step); | |
ae9f01f8 | 12394 | sra->reshape_progress = next_step; |
10f22854 AK |
12395 | |
12396 | /* wait until reshape finish */ | |
c85338c6 | 12397 | if (wait_for_reshape_imsm(sra, ndata)) { |
c47b0ff6 AK |
12398 | dprintf("wait_for_reshape_imsm returned error!\n"); |
12399 | goto abort; | |
12400 | } | |
84d11e6c N |
12401 | if (sigterm) |
12402 | goto abort; | |
10f22854 | 12403 | |
0228d92c AK |
12404 | if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL) == 1) { |
12405 | /* ignore error == 2, this can mean end of reshape here | |
12406 | */ | |
7a862a02 | 12407 | dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL)\n"); |
10f22854 AK |
12408 | goto abort; |
12409 | } | |
12410 | ||
12411 | } | |
12412 | ||
71e5411e PB |
12413 | /* clear migr_rec on disks after successful migration */ |
12414 | struct dl *d; | |
12415 | ||
85337573 | 12416 | memset(super->migr_rec_buf, 0, MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE); |
71e5411e PB |
12417 | for (d = super->disks; d; d = d->next) { |
12418 | if (d->index < 0 || is_failed(&d->disk)) | |
12419 | continue; | |
12420 | unsigned long long dsize; | |
12421 | ||
12422 | get_dev_size(d->fd, NULL, &dsize); | |
de44e46f | 12423 | if (lseek64(d->fd, dsize - MIGR_REC_SECTOR_POSITION*sector_size, |
71e5411e | 12424 | SEEK_SET) >= 0) { |
466070ad | 12425 | if ((unsigned int)write(d->fd, super->migr_rec_buf, |
de44e46f PB |
12426 | MIGR_REC_BUF_SECTORS*sector_size) != |
12427 | MIGR_REC_BUF_SECTORS*sector_size) | |
71e5411e PB |
12428 | perror("Write migr_rec failed"); |
12429 | } | |
12430 | } | |
12431 | ||
10f22854 AK |
12432 | /* return '1' if done */ |
12433 | ret_val = 1; | |
895ffd99 MT |
12434 | |
12435 | /* After the reshape eliminate size mismatch in metadata. | |
12436 | * Don't update md/component_size here, volume hasn't | |
12437 | * to take whole space. It is allowed by kernel. | |
12438 | * md/component_size will be set propoperly after next assembly. | |
12439 | */ | |
12440 | imsm_fix_size_mismatch(st, subarray_index); | |
12441 | ||
10f22854 AK |
12442 | abort: |
12443 | free(buf); | |
942e1cdb N |
12444 | /* See Grow.c: abort_reshape() for further explanation */ |
12445 | sysfs_set_num(sra, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL); | |
12446 | sysfs_set_num(sra, NULL, "suspend_hi", 0); | |
12447 | sysfs_set_num(sra, NULL, "suspend_lo", 0); | |
10f22854 AK |
12448 | |
12449 | return ret_val; | |
999b4972 | 12450 | } |
0c21b485 | 12451 | |
fbc42556 JR |
12452 | /******************************************************************************* |
12453 | * Function: calculate_bitmap_min_chunksize | |
12454 | * Description: Calculates the minimal valid bitmap chunk size | |
12455 | * Parameters: | |
12456 | * max_bits : indicate how many bits can be used for the bitmap | |
12457 | * data_area_size : the size of the data area covered by the bitmap | |
12458 | * | |
12459 | * Returns: | |
12460 | * The bitmap chunk size | |
12461 | ******************************************************************************/ | |
12462 | static unsigned long long | |
12463 | calculate_bitmap_min_chunksize(unsigned long long max_bits, | |
12464 | unsigned long long data_area_size) | |
12465 | { | |
12466 | unsigned long long min_chunk = | |
12467 | 4096; /* sub-page chunks don't work yet.. */ | |
12468 | unsigned long long bits = data_area_size / min_chunk + 1; | |
12469 | ||
12470 | while (bits > max_bits) { | |
12471 | min_chunk *= 2; | |
12472 | bits = (bits + 1) / 2; | |
12473 | } | |
12474 | return min_chunk; | |
12475 | } | |
12476 | ||
12477 | /******************************************************************************* | |
12478 | * Function: calculate_bitmap_chunksize | |
12479 | * Description: Calculates the bitmap chunk size for the given device | |
12480 | * Parameters: | |
12481 | * st : supertype information | |
12482 | * dev : device for the bitmap | |
12483 | * | |
12484 | * Returns: | |
12485 | * The bitmap chunk size | |
12486 | ******************************************************************************/ | |
12487 | static unsigned long long calculate_bitmap_chunksize(struct supertype *st, | |
12488 | struct imsm_dev *dev) | |
12489 | { | |
12490 | struct intel_super *super = st->sb; | |
12491 | unsigned long long min_chunksize; | |
12492 | unsigned long long result = IMSM_DEFAULT_BITMAP_CHUNKSIZE; | |
12493 | size_t dev_size = imsm_dev_size(dev); | |
12494 | ||
12495 | min_chunksize = calculate_bitmap_min_chunksize( | |
12496 | IMSM_BITMAP_AREA_SIZE * super->sector_size, dev_size); | |
12497 | ||
12498 | if (result < min_chunksize) | |
12499 | result = min_chunksize; | |
12500 | ||
12501 | return result; | |
12502 | } | |
12503 | ||
12504 | /******************************************************************************* | |
12505 | * Function: init_bitmap_header | |
12506 | * Description: Initialize the bitmap header structure | |
12507 | * Parameters: | |
12508 | * st : supertype information | |
12509 | * bms : bitmap header struct to initialize | |
12510 | * dev : device for the bitmap | |
12511 | * | |
12512 | * Returns: | |
12513 | * 0 : success | |
12514 | * -1 : fail | |
12515 | ******************************************************************************/ | |
12516 | static int init_bitmap_header(struct supertype *st, struct bitmap_super_s *bms, | |
12517 | struct imsm_dev *dev) | |
12518 | { | |
12519 | int vol_uuid[4]; | |
12520 | ||
12521 | if (!bms || !dev) | |
12522 | return -1; | |
12523 | ||
12524 | bms->magic = __cpu_to_le32(BITMAP_MAGIC); | |
12525 | bms->version = __cpu_to_le32(BITMAP_MAJOR_HI); | |
12526 | bms->daemon_sleep = __cpu_to_le32(IMSM_DEFAULT_BITMAP_DAEMON_SLEEP); | |
12527 | bms->sync_size = __cpu_to_le64(IMSM_BITMAP_AREA_SIZE); | |
12528 | bms->write_behind = __cpu_to_le32(0); | |
12529 | ||
12530 | uuid_from_super_imsm(st, vol_uuid); | |
12531 | memcpy(bms->uuid, vol_uuid, 16); | |
12532 | ||
12533 | bms->chunksize = calculate_bitmap_chunksize(st, dev); | |
12534 | ||
12535 | return 0; | |
12536 | } | |
12537 | ||
12538 | /******************************************************************************* | |
12539 | * Function: validate_internal_bitmap_for_drive | |
12540 | * Description: Verify if the bitmap header for a given drive. | |
12541 | * Parameters: | |
12542 | * st : supertype information | |
12543 | * offset : The offset from the beginning of the drive where to look for | |
12544 | * the bitmap header. | |
12545 | * d : the drive info | |
12546 | * | |
12547 | * Returns: | |
12548 | * 0 : success | |
12549 | * -1 : fail | |
12550 | ******************************************************************************/ | |
12551 | static int validate_internal_bitmap_for_drive(struct supertype *st, | |
12552 | unsigned long long offset, | |
12553 | struct dl *d) | |
12554 | { | |
12555 | struct intel_super *super = st->sb; | |
12556 | int ret = -1; | |
12557 | int vol_uuid[4]; | |
12558 | bitmap_super_t *bms; | |
12559 | int fd; | |
12560 | ||
12561 | if (!d) | |
12562 | return -1; | |
12563 | ||
12564 | void *read_buf; | |
12565 | ||
12566 | if (posix_memalign(&read_buf, MAX_SECTOR_SIZE, IMSM_BITMAP_HEADER_SIZE)) | |
12567 | return -1; | |
12568 | ||
12569 | fd = d->fd; | |
4389ce73 | 12570 | if (!is_fd_valid(fd)) { |
fbc42556 | 12571 | fd = open(d->devname, O_RDONLY, 0); |
4389ce73 MT |
12572 | |
12573 | if (!is_fd_valid(fd)) { | |
fbc42556 JR |
12574 | dprintf("cannot open the device %s\n", d->devname); |
12575 | goto abort; | |
12576 | } | |
12577 | } | |
12578 | ||
12579 | if (lseek64(fd, offset * super->sector_size, SEEK_SET) < 0) | |
12580 | goto abort; | |
12581 | if (read(fd, read_buf, IMSM_BITMAP_HEADER_SIZE) != | |
12582 | IMSM_BITMAP_HEADER_SIZE) | |
12583 | goto abort; | |
12584 | ||
12585 | uuid_from_super_imsm(st, vol_uuid); | |
12586 | ||
12587 | bms = read_buf; | |
12588 | if ((bms->magic != __cpu_to_le32(BITMAP_MAGIC)) || | |
12589 | (bms->version != __cpu_to_le32(BITMAP_MAJOR_HI)) || | |
12590 | (!same_uuid((int *)bms->uuid, vol_uuid, st->ss->swapuuid))) { | |
12591 | dprintf("wrong bitmap header detected\n"); | |
12592 | goto abort; | |
12593 | } | |
12594 | ||
12595 | ret = 0; | |
12596 | abort: | |
4389ce73 MT |
12597 | if (!is_fd_valid(d->fd)) |
12598 | close_fd(&fd); | |
12599 | ||
fbc42556 JR |
12600 | if (read_buf) |
12601 | free(read_buf); | |
12602 | ||
12603 | return ret; | |
12604 | } | |
12605 | ||
12606 | /******************************************************************************* | |
12607 | * Function: validate_internal_bitmap_imsm | |
12608 | * Description: Verify if the bitmap header is in place and with proper data. | |
12609 | * Parameters: | |
12610 | * st : supertype information | |
12611 | * | |
12612 | * Returns: | |
12613 | * 0 : success or device w/o RWH_BITMAP | |
12614 | * -1 : fail | |
12615 | ******************************************************************************/ | |
12616 | static int validate_internal_bitmap_imsm(struct supertype *st) | |
12617 | { | |
12618 | struct intel_super *super = st->sb; | |
12619 | struct imsm_dev *dev = get_imsm_dev(super, super->current_vol); | |
12620 | unsigned long long offset; | |
12621 | struct dl *d; | |
12622 | ||
fbc42556 JR |
12623 | if (dev->rwh_policy != RWH_BITMAP) |
12624 | return 0; | |
12625 | ||
12626 | offset = get_bitmap_header_sector(super, super->current_vol); | |
12627 | for (d = super->disks; d; d = d->next) { | |
12628 | if (d->index < 0 || is_failed(&d->disk)) | |
12629 | continue; | |
12630 | ||
12631 | if (validate_internal_bitmap_for_drive(st, offset, d)) { | |
12632 | pr_err("imsm: bitmap validation failed\n"); | |
12633 | return -1; | |
12634 | } | |
12635 | } | |
12636 | return 0; | |
12637 | } | |
12638 | ||
12639 | /******************************************************************************* | |
12640 | * Function: add_internal_bitmap_imsm | |
12641 | * Description: Mark the volume to use the bitmap and updates the chunk size value. | |
12642 | * Parameters: | |
12643 | * st : supertype information | |
12644 | * chunkp : bitmap chunk size | |
12645 | * delay : not used for imsm | |
12646 | * write_behind : not used for imsm | |
12647 | * size : not used for imsm | |
12648 | * may_change : not used for imsm | |
12649 | * amajor : not used for imsm | |
12650 | * | |
12651 | * Returns: | |
12652 | * 0 : success | |
12653 | * -1 : fail | |
12654 | ******************************************************************************/ | |
12655 | static int add_internal_bitmap_imsm(struct supertype *st, int *chunkp, | |
12656 | int delay, int write_behind, | |
12657 | unsigned long long size, int may_change, | |
12658 | int amajor) | |
12659 | { | |
12660 | struct intel_super *super = st->sb; | |
12661 | int vol_idx = super->current_vol; | |
12662 | struct imsm_dev *dev; | |
12663 | ||
12664 | if (!super->devlist || vol_idx == -1 || !chunkp) | |
12665 | return -1; | |
12666 | ||
12667 | dev = get_imsm_dev(super, vol_idx); | |
fbc42556 | 12668 | dev->rwh_policy = RWH_BITMAP; |
fbc42556 | 12669 | *chunkp = calculate_bitmap_chunksize(st, dev); |
fbc42556 JR |
12670 | return 0; |
12671 | } | |
12672 | ||
12673 | /******************************************************************************* | |
12674 | * Function: locate_bitmap_imsm | |
12675 | * Description: Seek 'fd' to start of write-intent-bitmap. | |
12676 | * Parameters: | |
12677 | * st : supertype information | |
12678 | * fd : file descriptor for the device | |
12679 | * node_num : not used for imsm | |
12680 | * | |
12681 | * Returns: | |
12682 | * 0 : success | |
12683 | * -1 : fail | |
12684 | ******************************************************************************/ | |
12685 | static int locate_bitmap_imsm(struct supertype *st, int fd, int node_num) | |
12686 | { | |
12687 | struct intel_super *super = st->sb; | |
12688 | unsigned long long offset; | |
12689 | int vol_idx = super->current_vol; | |
12690 | ||
12691 | if (!super->devlist || vol_idx == -1) | |
12692 | return -1; | |
12693 | ||
12694 | offset = get_bitmap_header_sector(super, super->current_vol); | |
12695 | dprintf("bitmap header offset is %llu\n", offset); | |
12696 | ||
12697 | lseek64(fd, offset << 9, 0); | |
12698 | ||
12699 | return 0; | |
12700 | } | |
12701 | ||
12702 | /******************************************************************************* | |
12703 | * Function: write_init_bitmap_imsm | |
12704 | * Description: Write a bitmap header and prepares the area for the bitmap. | |
12705 | * Parameters: | |
12706 | * st : supertype information | |
12707 | * fd : file descriptor for the device | |
12708 | * update : not used for imsm | |
12709 | * | |
12710 | * Returns: | |
12711 | * 0 : success | |
12712 | * -1 : fail | |
12713 | ******************************************************************************/ | |
12714 | static int write_init_bitmap_imsm(struct supertype *st, int fd, | |
12715 | enum bitmap_update update) | |
12716 | { | |
12717 | struct intel_super *super = st->sb; | |
12718 | int vol_idx = super->current_vol; | |
12719 | int ret = 0; | |
12720 | unsigned long long offset; | |
12721 | bitmap_super_t bms = { 0 }; | |
12722 | size_t written = 0; | |
12723 | size_t to_write; | |
12724 | ssize_t rv_num; | |
12725 | void *buf; | |
12726 | ||
12727 | if (!super->devlist || !super->sector_size || vol_idx == -1) | |
12728 | return -1; | |
12729 | ||
12730 | struct imsm_dev *dev = get_imsm_dev(super, vol_idx); | |
12731 | ||
12732 | /* first clear the space for bitmap header */ | |
12733 | unsigned long long bitmap_area_start = | |
12734 | get_bitmap_header_sector(super, vol_idx); | |
12735 | ||
12736 | dprintf("zeroing area start (%llu) and size (%u)\n", bitmap_area_start, | |
12737 | IMSM_BITMAP_AND_HEADER_SIZE / super->sector_size); | |
12738 | if (zero_disk_range(fd, bitmap_area_start, | |
12739 | IMSM_BITMAP_HEADER_SIZE / super->sector_size)) { | |
12740 | pr_err("imsm: cannot zeroing the space for the bitmap\n"); | |
12741 | return -1; | |
12742 | } | |
12743 | ||
12744 | /* The bitmap area should be filled with "1"s to perform initial | |
12745 | * synchronization. | |
12746 | */ | |
12747 | if (posix_memalign(&buf, MAX_SECTOR_SIZE, MAX_SECTOR_SIZE)) | |
12748 | return -1; | |
12749 | memset(buf, 0xFF, MAX_SECTOR_SIZE); | |
12750 | offset = get_bitmap_sector(super, vol_idx); | |
12751 | lseek64(fd, offset << 9, 0); | |
12752 | while (written < IMSM_BITMAP_AREA_SIZE) { | |
12753 | to_write = IMSM_BITMAP_AREA_SIZE - written; | |
12754 | if (to_write > MAX_SECTOR_SIZE) | |
12755 | to_write = MAX_SECTOR_SIZE; | |
12756 | rv_num = write(fd, buf, MAX_SECTOR_SIZE); | |
12757 | if (rv_num != MAX_SECTOR_SIZE) { | |
12758 | ret = -1; | |
12759 | dprintf("cannot initialize bitmap area\n"); | |
12760 | goto abort; | |
12761 | } | |
12762 | written += rv_num; | |
12763 | } | |
12764 | ||
12765 | /* write a bitmap header */ | |
12766 | init_bitmap_header(st, &bms, dev); | |
12767 | memset(buf, 0, MAX_SECTOR_SIZE); | |
12768 | memcpy(buf, &bms, sizeof(bitmap_super_t)); | |
12769 | if (locate_bitmap_imsm(st, fd, 0)) { | |
12770 | ret = -1; | |
12771 | dprintf("cannot locate the bitmap\n"); | |
12772 | goto abort; | |
12773 | } | |
12774 | if (write(fd, buf, MAX_SECTOR_SIZE) != MAX_SECTOR_SIZE) { | |
12775 | ret = -1; | |
12776 | dprintf("cannot write the bitmap header\n"); | |
12777 | goto abort; | |
12778 | } | |
12779 | fsync(fd); | |
12780 | ||
12781 | abort: | |
12782 | free(buf); | |
12783 | ||
12784 | return ret; | |
12785 | } | |
12786 | ||
12787 | /******************************************************************************* | |
12788 | * Function: is_vol_to_setup_bitmap | |
12789 | * Description: Checks if a bitmap should be activated on the dev. | |
12790 | * Parameters: | |
12791 | * info : info about the volume to setup the bitmap | |
12792 | * dev : the device to check against bitmap creation | |
12793 | * | |
12794 | * Returns: | |
12795 | * 0 : bitmap should be set up on the device | |
12796 | * -1 : otherwise | |
12797 | ******************************************************************************/ | |
12798 | static int is_vol_to_setup_bitmap(struct mdinfo *info, struct imsm_dev *dev) | |
12799 | { | |
12800 | if (!dev || !info) | |
12801 | return -1; | |
12802 | ||
12803 | if ((strcmp((char *)dev->volume, info->name) == 0) && | |
12804 | (dev->rwh_policy == RWH_BITMAP)) | |
12805 | return -1; | |
12806 | ||
12807 | return 0; | |
12808 | } | |
12809 | ||
12810 | /******************************************************************************* | |
12811 | * Function: set_bitmap_sysfs | |
12812 | * Description: Set the sysfs atributes of a given volume to activate the bitmap. | |
12813 | * Parameters: | |
12814 | * info : info about the volume where the bitmap should be setup | |
12815 | * chunksize : bitmap chunk size | |
12816 | * location : location of the bitmap | |
12817 | * | |
12818 | * Returns: | |
12819 | * 0 : success | |
12820 | * -1 : fail | |
12821 | ******************************************************************************/ | |
12822 | static int set_bitmap_sysfs(struct mdinfo *info, unsigned long long chunksize, | |
12823 | char *location) | |
12824 | { | |
12825 | /* The bitmap/metadata is set to external to allow changing of value for | |
12826 | * bitmap/location. When external is used, the kernel will treat an offset | |
12827 | * related to the device's first lba (in opposition to the "internal" case | |
12828 | * when this value is related to the beginning of the superblock). | |
12829 | */ | |
12830 | if (sysfs_set_str(info, NULL, "bitmap/metadata", "external")) { | |
12831 | dprintf("failed to set bitmap/metadata\n"); | |
12832 | return -1; | |
12833 | } | |
12834 | ||
12835 | /* It can only be changed when no bitmap is active. | |
12836 | * Should be bigger than 512 and must be power of 2. | |
12837 | * It is expecting the value in bytes. | |
12838 | */ | |
12839 | if (sysfs_set_num(info, NULL, "bitmap/chunksize", | |
12840 | __cpu_to_le32(chunksize))) { | |
12841 | dprintf("failed to set bitmap/chunksize\n"); | |
12842 | return -1; | |
12843 | } | |
12844 | ||
12845 | /* It is expecting the value in sectors. */ | |
12846 | if (sysfs_set_num(info, NULL, "bitmap/space", | |
12847 | __cpu_to_le64(IMSM_BITMAP_AREA_SIZE))) { | |
12848 | dprintf("failed to set bitmap/space\n"); | |
12849 | return -1; | |
12850 | } | |
12851 | ||
12852 | /* Determines the delay between the bitmap updates. | |
12853 | * It is expecting the value in seconds. | |
12854 | */ | |
12855 | if (sysfs_set_num(info, NULL, "bitmap/time_base", | |
12856 | __cpu_to_le64(IMSM_DEFAULT_BITMAP_DAEMON_SLEEP))) { | |
12857 | dprintf("failed to set bitmap/time_base\n"); | |
12858 | return -1; | |
12859 | } | |
12860 | ||
12861 | /* It is expecting the value in sectors with a sign at the beginning. */ | |
12862 | if (sysfs_set_str(info, NULL, "bitmap/location", location)) { | |
12863 | dprintf("failed to set bitmap/location\n"); | |
12864 | return -1; | |
12865 | } | |
12866 | ||
12867 | return 0; | |
12868 | } | |
12869 | ||
12870 | /******************************************************************************* | |
12871 | * Function: set_bitmap_imsm | |
12872 | * Description: Setup the bitmap for the given volume | |
12873 | * Parameters: | |
12874 | * st : supertype information | |
12875 | * info : info about the volume where the bitmap should be setup | |
12876 | * | |
12877 | * Returns: | |
12878 | * 0 : success | |
12879 | * -1 : fail | |
12880 | ******************************************************************************/ | |
12881 | static int set_bitmap_imsm(struct supertype *st, struct mdinfo *info) | |
12882 | { | |
12883 | struct intel_super *super = st->sb; | |
12884 | int prev_current_vol = super->current_vol; | |
12885 | struct imsm_dev *dev; | |
12886 | int ret = -1; | |
12887 | char location[16] = ""; | |
12888 | unsigned long long chunksize; | |
12889 | struct intel_dev *dev_it; | |
12890 | ||
12891 | for (dev_it = super->devlist; dev_it; dev_it = dev_it->next) { | |
12892 | super->current_vol = dev_it->index; | |
12893 | dev = get_imsm_dev(super, super->current_vol); | |
12894 | ||
12895 | if (is_vol_to_setup_bitmap(info, dev)) { | |
12896 | if (validate_internal_bitmap_imsm(st)) { | |
12897 | dprintf("bitmap header validation failed\n"); | |
12898 | goto abort; | |
12899 | } | |
12900 | ||
12901 | chunksize = calculate_bitmap_chunksize(st, dev); | |
12902 | dprintf("chunk size is %llu\n", chunksize); | |
12903 | ||
12904 | snprintf(location, sizeof(location), "+%llu", | |
12905 | get_bitmap_sector(super, super->current_vol)); | |
12906 | dprintf("bitmap offset is %s\n", location); | |
12907 | ||
12908 | if (set_bitmap_sysfs(info, chunksize, location)) { | |
12909 | dprintf("cannot setup the bitmap\n"); | |
12910 | goto abort; | |
12911 | } | |
12912 | } | |
12913 | } | |
12914 | ret = 0; | |
12915 | abort: | |
12916 | super->current_vol = prev_current_vol; | |
12917 | return ret; | |
12918 | } | |
12919 | ||
cdddbdbc | 12920 | struct superswitch super_imsm = { |
cdddbdbc DW |
12921 | .examine_super = examine_super_imsm, |
12922 | .brief_examine_super = brief_examine_super_imsm, | |
4737ae25 | 12923 | .brief_examine_subarrays = brief_examine_subarrays_imsm, |
9d84c8ea | 12924 | .export_examine_super = export_examine_super_imsm, |
cdddbdbc DW |
12925 | .detail_super = detail_super_imsm, |
12926 | .brief_detail_super = brief_detail_super_imsm, | |
bf5a934a | 12927 | .write_init_super = write_init_super_imsm, |
0e600426 N |
12928 | .validate_geometry = validate_geometry_imsm, |
12929 | .add_to_super = add_to_super_imsm, | |
1a64be56 | 12930 | .remove_from_super = remove_from_super_imsm, |
d665cc31 | 12931 | .detail_platform = detail_platform_imsm, |
e50cf220 | 12932 | .export_detail_platform = export_detail_platform_imsm, |
33414a01 | 12933 | .kill_subarray = kill_subarray_imsm, |
aa534678 | 12934 | .update_subarray = update_subarray_imsm, |
2b959fbf | 12935 | .load_container = load_container_imsm, |
71204a50 N |
12936 | .default_geometry = default_geometry_imsm, |
12937 | .get_disk_controller_domain = imsm_get_disk_controller_domain, | |
12938 | .reshape_super = imsm_reshape_super, | |
12939 | .manage_reshape = imsm_manage_reshape, | |
9e2d750d | 12940 | .recover_backup = recover_backup_imsm, |
27156a57 | 12941 | .examine_badblocks = examine_badblocks_imsm, |
cdddbdbc DW |
12942 | .match_home = match_home_imsm, |
12943 | .uuid_from_super= uuid_from_super_imsm, | |
12944 | .getinfo_super = getinfo_super_imsm, | |
5c4cd5da | 12945 | .getinfo_super_disks = getinfo_super_disks_imsm, |
cdddbdbc DW |
12946 | .update_super = update_super_imsm, |
12947 | ||
12948 | .avail_size = avail_size_imsm, | |
fbfdcb06 | 12949 | .get_spare_criteria = get_spare_criteria_imsm, |
cdddbdbc DW |
12950 | |
12951 | .compare_super = compare_super_imsm, | |
12952 | ||
12953 | .load_super = load_super_imsm, | |
bf5a934a | 12954 | .init_super = init_super_imsm, |
e683ca88 | 12955 | .store_super = store_super_imsm, |
cdddbdbc DW |
12956 | .free_super = free_super_imsm, |
12957 | .match_metadata_desc = match_metadata_desc_imsm, | |
bf5a934a | 12958 | .container_content = container_content_imsm, |
0c21b485 | 12959 | .validate_container = validate_container_imsm, |
cdddbdbc | 12960 | |
fbc42556 JR |
12961 | .add_internal_bitmap = add_internal_bitmap_imsm, |
12962 | .locate_bitmap = locate_bitmap_imsm, | |
12963 | .write_bitmap = write_init_bitmap_imsm, | |
12964 | .set_bitmap = set_bitmap_imsm, | |
12965 | ||
2432ce9b AP |
12966 | .write_init_ppl = write_init_ppl_imsm, |
12967 | .validate_ppl = validate_ppl_imsm, | |
12968 | ||
cdddbdbc | 12969 | .external = 1, |
4cce4069 | 12970 | .name = "imsm", |
845dea95 NB |
12971 | |
12972 | /* for mdmon */ | |
12973 | .open_new = imsm_open_new, | |
ed9d66aa | 12974 | .set_array_state= imsm_set_array_state, |
845dea95 NB |
12975 | .set_disk = imsm_set_disk, |
12976 | .sync_metadata = imsm_sync_metadata, | |
88758e9d | 12977 | .activate_spare = imsm_activate_spare, |
e8319a19 | 12978 | .process_update = imsm_process_update, |
8273f55e | 12979 | .prepare_update = imsm_prepare_update, |
6f50473f | 12980 | .record_bad_block = imsm_record_badblock, |
c07a5a4f | 12981 | .clear_bad_block = imsm_clear_badblock, |
928f1424 | 12982 | .get_bad_blocks = imsm_get_badblocks, |
cdddbdbc | 12983 | }; |