]>
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 | |
ec50f7b6 LM |
369 | struct md_list { |
370 | /* usage marker: | |
371 | * 1: load metadata | |
372 | * 2: metadata does not match | |
373 | * 4: already checked | |
374 | */ | |
375 | int used; | |
376 | char *devname; | |
377 | int found; | |
378 | int container; | |
379 | dev_t st_rdev; | |
380 | struct md_list *next; | |
381 | }; | |
382 | ||
e7b84f9d | 383 | #define pr_vrb(fmt, arg...) (void) (verbose && pr_err(fmt, ##arg)) |
ec50f7b6 | 384 | |
1484e727 DW |
385 | static __u8 migr_type(struct imsm_dev *dev) |
386 | { | |
387 | if (dev->vol.migr_type == MIGR_VERIFY && | |
388 | dev->status & DEV_VERIFY_AND_FIX) | |
389 | return MIGR_REPAIR; | |
390 | else | |
391 | return dev->vol.migr_type; | |
392 | } | |
393 | ||
394 | static void set_migr_type(struct imsm_dev *dev, __u8 migr_type) | |
395 | { | |
396 | /* for compatibility with older oroms convert MIGR_REPAIR, into | |
397 | * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status | |
398 | */ | |
399 | if (migr_type == MIGR_REPAIR) { | |
400 | dev->vol.migr_type = MIGR_VERIFY; | |
401 | dev->status |= DEV_VERIFY_AND_FIX; | |
402 | } else { | |
403 | dev->vol.migr_type = migr_type; | |
404 | dev->status &= ~DEV_VERIFY_AND_FIX; | |
405 | } | |
406 | } | |
407 | ||
f36a9ecd | 408 | static unsigned int sector_count(__u32 bytes, unsigned int sector_size) |
cdddbdbc | 409 | { |
f36a9ecd | 410 | return ROUND_UP(bytes, sector_size) / sector_size; |
87eb16df | 411 | } |
cdddbdbc | 412 | |
f36a9ecd PB |
413 | static unsigned int mpb_sectors(struct imsm_super *mpb, |
414 | unsigned int sector_size) | |
87eb16df | 415 | { |
f36a9ecd | 416 | return sector_count(__le32_to_cpu(mpb->mpb_size), sector_size); |
cdddbdbc DW |
417 | } |
418 | ||
ba2de7ba DW |
419 | struct intel_dev { |
420 | struct imsm_dev *dev; | |
421 | struct intel_dev *next; | |
f21e18ca | 422 | unsigned index; |
ba2de7ba DW |
423 | }; |
424 | ||
88654014 LM |
425 | struct intel_hba { |
426 | enum sys_dev_type type; | |
427 | char *path; | |
428 | char *pci_id; | |
429 | struct intel_hba *next; | |
430 | }; | |
431 | ||
1a64be56 LM |
432 | enum action { |
433 | DISK_REMOVE = 1, | |
434 | DISK_ADD | |
435 | }; | |
cdddbdbc DW |
436 | /* internal representation of IMSM metadata */ |
437 | struct intel_super { | |
438 | union { | |
949c47a0 DW |
439 | void *buf; /* O_DIRECT buffer for reading/writing metadata */ |
440 | struct imsm_super *anchor; /* immovable parameters */ | |
cdddbdbc | 441 | }; |
8e59f3d8 AK |
442 | union { |
443 | void *migr_rec_buf; /* buffer for I/O operations */ | |
444 | struct migr_record *migr_rec; /* migration record */ | |
445 | }; | |
51d83f5d AK |
446 | int clean_migration_record_by_mdmon; /* when reshape is switched to next |
447 | array, it indicates that mdmon is allowed to clean migration | |
448 | record */ | |
949c47a0 | 449 | size_t len; /* size of the 'buf' allocation */ |
bbab0940 | 450 | size_t extra_space; /* extra space in 'buf' that is not used yet */ |
4d7b1503 DW |
451 | void *next_buf; /* for realloc'ing buf from the manager */ |
452 | size_t next_len; | |
c2c087e6 | 453 | int updates_pending; /* count of pending updates for mdmon */ |
bf5a934a | 454 | int current_vol; /* index of raid device undergoing creation */ |
5551b113 | 455 | unsigned long long create_offset; /* common start for 'current_vol' */ |
148acb7b | 456 | __u32 random; /* random data for seeding new family numbers */ |
ba2de7ba | 457 | struct intel_dev *devlist; |
fa7bb6f8 | 458 | unsigned int sector_size; /* sector size of used member drives */ |
cdddbdbc DW |
459 | struct dl { |
460 | struct dl *next; | |
461 | int index; | |
462 | __u8 serial[MAX_RAID_SERIAL_LEN]; | |
463 | int major, minor; | |
464 | char *devname; | |
b9f594fe | 465 | struct imsm_disk disk; |
cdddbdbc | 466 | int fd; |
0dcecb2e DW |
467 | int extent_cnt; |
468 | struct extent *e; /* for determining freespace @ create */ | |
efb30e7f | 469 | int raiddisk; /* slot to fill in autolayout */ |
1a64be56 | 470 | enum action action; |
ca0748fa | 471 | } *disks, *current_disk; |
1a64be56 LM |
472 | struct dl *disk_mgmt_list; /* list of disks to add/remove while mdmon |
473 | active */ | |
47ee5a45 | 474 | struct dl *missing; /* disks removed while we weren't looking */ |
43dad3d6 | 475 | struct bbm_log *bbm_log; |
88654014 | 476 | struct intel_hba *hba; /* device path of the raid controller for this metadata */ |
88c32bb1 | 477 | const struct imsm_orom *orom; /* platform firmware support */ |
a2b97981 | 478 | struct intel_super *next; /* (temp) list for disambiguating family_num */ |
928f1424 | 479 | struct md_bb bb; /* memory for get_bad_blocks call */ |
a2b97981 DW |
480 | }; |
481 | ||
482 | struct intel_disk { | |
483 | struct imsm_disk disk; | |
484 | #define IMSM_UNKNOWN_OWNER (-1) | |
485 | int owner; | |
486 | struct intel_disk *next; | |
cdddbdbc DW |
487 | }; |
488 | ||
c2c087e6 DW |
489 | struct extent { |
490 | unsigned long long start, size; | |
491 | }; | |
492 | ||
694575e7 KW |
493 | /* definitions of reshape process types */ |
494 | enum imsm_reshape_type { | |
495 | CH_TAKEOVER, | |
b5347799 | 496 | CH_MIGRATION, |
7abc9871 | 497 | CH_ARRAY_SIZE, |
694575e7 KW |
498 | }; |
499 | ||
88758e9d DW |
500 | /* definition of messages passed to imsm_process_update */ |
501 | enum imsm_update_type { | |
502 | update_activate_spare, | |
8273f55e | 503 | update_create_array, |
33414a01 | 504 | update_kill_array, |
aa534678 | 505 | update_rename_array, |
1a64be56 | 506 | update_add_remove_disk, |
78b10e66 | 507 | update_reshape_container_disks, |
48c5303a | 508 | update_reshape_migration, |
2d40f3a1 AK |
509 | update_takeover, |
510 | update_general_migration_checkpoint, | |
f3871fdc | 511 | update_size_change, |
bbab0940 | 512 | update_prealloc_badblocks_mem, |
e6e9dd3f | 513 | update_rwh_policy, |
88758e9d DW |
514 | }; |
515 | ||
516 | struct imsm_update_activate_spare { | |
517 | enum imsm_update_type type; | |
d23fe947 | 518 | struct dl *dl; |
88758e9d DW |
519 | int slot; |
520 | int array; | |
521 | struct imsm_update_activate_spare *next; | |
522 | }; | |
523 | ||
78b10e66 | 524 | struct geo_params { |
4dd2df09 | 525 | char devnm[32]; |
78b10e66 | 526 | char *dev_name; |
d04f65f4 | 527 | unsigned long long size; |
78b10e66 N |
528 | int level; |
529 | int layout; | |
530 | int chunksize; | |
531 | int raid_disks; | |
532 | }; | |
533 | ||
bb025c2f KW |
534 | enum takeover_direction { |
535 | R10_TO_R0, | |
536 | R0_TO_R10 | |
537 | }; | |
538 | struct imsm_update_takeover { | |
539 | enum imsm_update_type type; | |
540 | int subarray; | |
541 | enum takeover_direction direction; | |
542 | }; | |
78b10e66 N |
543 | |
544 | struct imsm_update_reshape { | |
545 | enum imsm_update_type type; | |
546 | int old_raid_disks; | |
547 | int new_raid_disks; | |
48c5303a PC |
548 | |
549 | int new_disks[1]; /* new_raid_disks - old_raid_disks makedev number */ | |
550 | }; | |
551 | ||
552 | struct imsm_update_reshape_migration { | |
553 | enum imsm_update_type type; | |
554 | int old_raid_disks; | |
555 | int new_raid_disks; | |
556 | /* fields for array migration changes | |
557 | */ | |
558 | int subdev; | |
559 | int new_level; | |
560 | int new_layout; | |
4bba0439 | 561 | int new_chunksize; |
48c5303a | 562 | |
d195167d | 563 | int new_disks[1]; /* new_raid_disks - old_raid_disks makedev number */ |
78b10e66 N |
564 | }; |
565 | ||
f3871fdc AK |
566 | struct imsm_update_size_change { |
567 | enum imsm_update_type type; | |
568 | int subdev; | |
569 | long long new_size; | |
570 | }; | |
571 | ||
2d40f3a1 AK |
572 | struct imsm_update_general_migration_checkpoint { |
573 | enum imsm_update_type type; | |
4036e7ee | 574 | __u64 curr_migr_unit; |
2d40f3a1 AK |
575 | }; |
576 | ||
54c2c1ea DW |
577 | struct disk_info { |
578 | __u8 serial[MAX_RAID_SERIAL_LEN]; | |
579 | }; | |
580 | ||
8273f55e DW |
581 | struct imsm_update_create_array { |
582 | enum imsm_update_type type; | |
8273f55e | 583 | int dev_idx; |
6a3e913e | 584 | struct imsm_dev dev; |
8273f55e DW |
585 | }; |
586 | ||
33414a01 DW |
587 | struct imsm_update_kill_array { |
588 | enum imsm_update_type type; | |
589 | int dev_idx; | |
590 | }; | |
591 | ||
aa534678 DW |
592 | struct imsm_update_rename_array { |
593 | enum imsm_update_type type; | |
594 | __u8 name[MAX_RAID_SERIAL_LEN]; | |
595 | int dev_idx; | |
596 | }; | |
597 | ||
1a64be56 | 598 | struct imsm_update_add_remove_disk { |
43dad3d6 DW |
599 | enum imsm_update_type type; |
600 | }; | |
601 | ||
bbab0940 TM |
602 | struct imsm_update_prealloc_bb_mem { |
603 | enum imsm_update_type type; | |
604 | }; | |
605 | ||
e6e9dd3f AP |
606 | struct imsm_update_rwh_policy { |
607 | enum imsm_update_type type; | |
608 | int new_policy; | |
609 | int dev_idx; | |
610 | }; | |
611 | ||
88654014 LM |
612 | static const char *_sys_dev_type[] = { |
613 | [SYS_DEV_UNKNOWN] = "Unknown", | |
614 | [SYS_DEV_SAS] = "SAS", | |
614902f6 | 615 | [SYS_DEV_SATA] = "SATA", |
60f0f54d PB |
616 | [SYS_DEV_NVME] = "NVMe", |
617 | [SYS_DEV_VMD] = "VMD" | |
88654014 LM |
618 | }; |
619 | ||
620 | const char *get_sys_dev_type(enum sys_dev_type type) | |
621 | { | |
622 | if (type >= SYS_DEV_MAX) | |
623 | type = SYS_DEV_UNKNOWN; | |
624 | ||
625 | return _sys_dev_type[type]; | |
626 | } | |
627 | ||
628 | static struct intel_hba * alloc_intel_hba(struct sys_dev *device) | |
629 | { | |
503975b9 N |
630 | struct intel_hba *result = xmalloc(sizeof(*result)); |
631 | ||
632 | result->type = device->type; | |
633 | result->path = xstrdup(device->path); | |
634 | result->next = NULL; | |
635 | if (result->path && (result->pci_id = strrchr(result->path, '/')) != NULL) | |
636 | result->pci_id++; | |
637 | ||
88654014 LM |
638 | return result; |
639 | } | |
640 | ||
641 | static struct intel_hba * find_intel_hba(struct intel_hba *hba, struct sys_dev *device) | |
642 | { | |
594dc1b8 JS |
643 | struct intel_hba *result; |
644 | ||
88654014 LM |
645 | for (result = hba; result; result = result->next) { |
646 | if (result->type == device->type && strcmp(result->path, device->path) == 0) | |
647 | break; | |
648 | } | |
649 | return result; | |
650 | } | |
651 | ||
b4cf4cba | 652 | static int attach_hba_to_super(struct intel_super *super, struct sys_dev *device) |
88654014 LM |
653 | { |
654 | struct intel_hba *hba; | |
655 | ||
656 | /* check if disk attached to Intel HBA */ | |
657 | hba = find_intel_hba(super->hba, device); | |
658 | if (hba != NULL) | |
659 | return 1; | |
660 | /* Check if HBA is already attached to super */ | |
661 | if (super->hba == NULL) { | |
662 | super->hba = alloc_intel_hba(device); | |
663 | return 1; | |
6b781d33 AP |
664 | } |
665 | ||
666 | hba = super->hba; | |
667 | /* Intel metadata allows for all disks attached to the same type HBA. | |
614902f6 | 668 | * Do not support HBA types mixing |
6b781d33 AP |
669 | */ |
670 | if (device->type != hba->type) | |
88654014 | 671 | return 2; |
6b781d33 AP |
672 | |
673 | /* Multiple same type HBAs can be used if they share the same OROM */ | |
674 | const struct imsm_orom *device_orom = get_orom_by_device_id(device->dev_id); | |
675 | ||
676 | if (device_orom != super->orom) | |
677 | return 2; | |
678 | ||
679 | while (hba->next) | |
680 | hba = hba->next; | |
681 | ||
682 | hba->next = alloc_intel_hba(device); | |
683 | return 1; | |
88654014 LM |
684 | } |
685 | ||
686 | static struct sys_dev* find_disk_attached_hba(int fd, const char *devname) | |
687 | { | |
9bc4ae77 | 688 | struct sys_dev *list, *elem; |
88654014 LM |
689 | char *disk_path; |
690 | ||
691 | if ((list = find_intel_devices()) == NULL) | |
692 | return 0; | |
693 | ||
694 | if (fd < 0) | |
695 | disk_path = (char *) devname; | |
696 | else | |
7c798f87 | 697 | disk_path = diskfd_to_devpath(fd, 1, NULL); |
88654014 | 698 | |
9bc4ae77 | 699 | if (!disk_path) |
88654014 | 700 | return 0; |
88654014 | 701 | |
9bc4ae77 N |
702 | for (elem = list; elem; elem = elem->next) |
703 | if (path_attached_to_hba(disk_path, elem->path)) | |
88654014 | 704 | return elem; |
9bc4ae77 | 705 | |
88654014 LM |
706 | if (disk_path != devname) |
707 | free(disk_path); | |
88654014 LM |
708 | |
709 | return NULL; | |
710 | } | |
711 | ||
d424212e N |
712 | static int find_intel_hba_capability(int fd, struct intel_super *super, |
713 | char *devname); | |
f2f5c343 | 714 | |
cdddbdbc DW |
715 | static struct supertype *match_metadata_desc_imsm(char *arg) |
716 | { | |
717 | struct supertype *st; | |
718 | ||
719 | if (strcmp(arg, "imsm") != 0 && | |
720 | strcmp(arg, "default") != 0 | |
721 | ) | |
722 | return NULL; | |
723 | ||
503975b9 | 724 | st = xcalloc(1, sizeof(*st)); |
cdddbdbc DW |
725 | st->ss = &super_imsm; |
726 | st->max_devs = IMSM_MAX_DEVICES; | |
727 | st->minor_version = 0; | |
728 | st->sb = NULL; | |
729 | return st; | |
730 | } | |
731 | ||
cdddbdbc DW |
732 | static __u8 *get_imsm_version(struct imsm_super *mpb) |
733 | { | |
734 | return &mpb->sig[MPB_SIG_LEN]; | |
735 | } | |
736 | ||
949c47a0 DW |
737 | /* retrieve a disk directly from the anchor when the anchor is known to be |
738 | * up-to-date, currently only at load time | |
739 | */ | |
740 | static struct imsm_disk *__get_imsm_disk(struct imsm_super *mpb, __u8 index) | |
cdddbdbc | 741 | { |
949c47a0 | 742 | if (index >= mpb->num_disks) |
cdddbdbc DW |
743 | return NULL; |
744 | return &mpb->disk[index]; | |
745 | } | |
746 | ||
95d07a2c LM |
747 | /* retrieve the disk description based on a index of the disk |
748 | * in the sub-array | |
749 | */ | |
750 | static struct dl *get_imsm_dl_disk(struct intel_super *super, __u8 index) | |
949c47a0 | 751 | { |
b9f594fe DW |
752 | struct dl *d; |
753 | ||
754 | for (d = super->disks; d; d = d->next) | |
755 | if (d->index == index) | |
95d07a2c LM |
756 | return d; |
757 | ||
758 | return NULL; | |
759 | } | |
760 | /* retrieve a disk from the parsed metadata */ | |
761 | static struct imsm_disk *get_imsm_disk(struct intel_super *super, __u8 index) | |
762 | { | |
763 | struct dl *dl; | |
764 | ||
765 | dl = get_imsm_dl_disk(super, index); | |
766 | if (dl) | |
767 | return &dl->disk; | |
768 | ||
b9f594fe | 769 | return NULL; |
949c47a0 DW |
770 | } |
771 | ||
772 | /* generate a checksum directly from the anchor when the anchor is known to be | |
773 | * up-to-date, currently only at load or write_super after coalescing | |
774 | */ | |
775 | static __u32 __gen_imsm_checksum(struct imsm_super *mpb) | |
cdddbdbc DW |
776 | { |
777 | __u32 end = mpb->mpb_size / sizeof(end); | |
778 | __u32 *p = (__u32 *) mpb; | |
779 | __u32 sum = 0; | |
780 | ||
5d500228 N |
781 | while (end--) { |
782 | sum += __le32_to_cpu(*p); | |
97f734fd N |
783 | p++; |
784 | } | |
cdddbdbc | 785 | |
5d500228 | 786 | return sum - __le32_to_cpu(mpb->check_sum); |
cdddbdbc DW |
787 | } |
788 | ||
a965f303 DW |
789 | static size_t sizeof_imsm_map(struct imsm_map *map) |
790 | { | |
791 | return sizeof(struct imsm_map) + sizeof(__u32) * (map->num_members - 1); | |
792 | } | |
793 | ||
794 | struct imsm_map *get_imsm_map(struct imsm_dev *dev, int second_map) | |
cdddbdbc | 795 | { |
5e7b0330 AK |
796 | /* A device can have 2 maps if it is in the middle of a migration. |
797 | * If second_map is: | |
238c0a71 AK |
798 | * MAP_0 - we return the first map |
799 | * MAP_1 - we return the second map if it exists, else NULL | |
800 | * MAP_X - we return the second map if it exists, else the first | |
5e7b0330 | 801 | */ |
a965f303 | 802 | struct imsm_map *map = &dev->vol.map[0]; |
9535fc47 | 803 | struct imsm_map *map2 = NULL; |
a965f303 | 804 | |
9535fc47 AK |
805 | if (dev->vol.migr_state) |
806 | map2 = (void *)map + sizeof_imsm_map(map); | |
a965f303 | 807 | |
9535fc47 | 808 | switch (second_map) { |
3b451610 | 809 | case MAP_0: |
9535fc47 | 810 | break; |
3b451610 | 811 | case MAP_1: |
9535fc47 AK |
812 | map = map2; |
813 | break; | |
238c0a71 | 814 | case MAP_X: |
9535fc47 AK |
815 | if (map2) |
816 | map = map2; | |
817 | break; | |
9535fc47 AK |
818 | default: |
819 | map = NULL; | |
820 | } | |
821 | return map; | |
5e7b0330 | 822 | |
a965f303 | 823 | } |
cdddbdbc | 824 | |
3393c6af DW |
825 | /* return the size of the device. |
826 | * migr_state increases the returned size if map[0] were to be duplicated | |
827 | */ | |
828 | static size_t sizeof_imsm_dev(struct imsm_dev *dev, int migr_state) | |
a965f303 DW |
829 | { |
830 | size_t size = sizeof(*dev) - sizeof(struct imsm_map) + | |
238c0a71 | 831 | sizeof_imsm_map(get_imsm_map(dev, MAP_0)); |
cdddbdbc DW |
832 | |
833 | /* migrating means an additional map */ | |
a965f303 | 834 | if (dev->vol.migr_state) |
238c0a71 | 835 | size += sizeof_imsm_map(get_imsm_map(dev, MAP_1)); |
3393c6af | 836 | else if (migr_state) |
238c0a71 | 837 | size += sizeof_imsm_map(get_imsm_map(dev, MAP_0)); |
cdddbdbc DW |
838 | |
839 | return size; | |
840 | } | |
841 | ||
54c2c1ea DW |
842 | /* retrieve disk serial number list from a metadata update */ |
843 | static struct disk_info *get_disk_info(struct imsm_update_create_array *update) | |
844 | { | |
845 | void *u = update; | |
846 | struct disk_info *inf; | |
847 | ||
848 | inf = u + sizeof(*update) - sizeof(struct imsm_dev) + | |
849 | sizeof_imsm_dev(&update->dev, 0); | |
850 | ||
851 | return inf; | |
852 | } | |
54c2c1ea | 853 | |
949c47a0 | 854 | static struct imsm_dev *__get_imsm_dev(struct imsm_super *mpb, __u8 index) |
cdddbdbc DW |
855 | { |
856 | int offset; | |
857 | int i; | |
858 | void *_mpb = mpb; | |
859 | ||
949c47a0 | 860 | if (index >= mpb->num_raid_devs) |
cdddbdbc DW |
861 | return NULL; |
862 | ||
863 | /* devices start after all disks */ | |
864 | offset = ((void *) &mpb->disk[mpb->num_disks]) - _mpb; | |
865 | ||
866 | for (i = 0; i <= index; i++) | |
867 | if (i == index) | |
868 | return _mpb + offset; | |
869 | else | |
3393c6af | 870 | offset += sizeof_imsm_dev(_mpb + offset, 0); |
cdddbdbc DW |
871 | |
872 | return NULL; | |
873 | } | |
874 | ||
949c47a0 DW |
875 | static struct imsm_dev *get_imsm_dev(struct intel_super *super, __u8 index) |
876 | { | |
ba2de7ba DW |
877 | struct intel_dev *dv; |
878 | ||
949c47a0 DW |
879 | if (index >= super->anchor->num_raid_devs) |
880 | return NULL; | |
ba2de7ba DW |
881 | for (dv = super->devlist; dv; dv = dv->next) |
882 | if (dv->index == index) | |
883 | return dv->dev; | |
884 | return NULL; | |
949c47a0 DW |
885 | } |
886 | ||
8d67477f TM |
887 | static inline unsigned long long __le48_to_cpu(const struct bbm_log_block_addr |
888 | *addr) | |
889 | { | |
890 | return ((((__u64)__le32_to_cpu(addr->dw1)) << 16) | | |
891 | __le16_to_cpu(addr->w1)); | |
892 | } | |
893 | ||
894 | static inline struct bbm_log_block_addr __cpu_to_le48(unsigned long long sec) | |
895 | { | |
896 | struct bbm_log_block_addr addr; | |
897 | ||
898 | addr.w1 = __cpu_to_le16((__u16)(sec & 0xffff)); | |
899 | addr.dw1 = __cpu_to_le32((__u32)(sec >> 16) & 0xffffffff); | |
900 | return addr; | |
901 | } | |
902 | ||
8d67477f TM |
903 | /* get size of the bbm log */ |
904 | static __u32 get_imsm_bbm_log_size(struct bbm_log *log) | |
905 | { | |
906 | if (!log || log->entry_count == 0) | |
907 | return 0; | |
908 | ||
909 | return sizeof(log->signature) + | |
910 | sizeof(log->entry_count) + | |
911 | log->entry_count * sizeof(struct bbm_log_entry); | |
912 | } | |
6f50473f TM |
913 | |
914 | /* check if bad block is not partially stored in bbm log */ | |
915 | static int is_stored_in_bbm(struct bbm_log *log, const __u8 idx, const unsigned | |
916 | long long sector, const int length, __u32 *pos) | |
917 | { | |
918 | __u32 i; | |
919 | ||
920 | for (i = *pos; i < log->entry_count; i++) { | |
921 | struct bbm_log_entry *entry = &log->marked_block_entries[i]; | |
922 | unsigned long long bb_start; | |
923 | unsigned long long bb_end; | |
924 | ||
925 | bb_start = __le48_to_cpu(&entry->defective_block_start); | |
926 | bb_end = bb_start + (entry->marked_count + 1); | |
927 | ||
928 | if ((entry->disk_ordinal == idx) && (bb_start >= sector) && | |
929 | (bb_end <= sector + length)) { | |
930 | *pos = i; | |
931 | return 1; | |
932 | } | |
933 | } | |
934 | return 0; | |
935 | } | |
936 | ||
937 | /* record new bad block in bbm log */ | |
938 | static int record_new_badblock(struct bbm_log *log, const __u8 idx, unsigned | |
939 | long long sector, int length) | |
940 | { | |
941 | int new_bb = 0; | |
942 | __u32 pos = 0; | |
943 | struct bbm_log_entry *entry = NULL; | |
944 | ||
945 | while (is_stored_in_bbm(log, idx, sector, length, &pos)) { | |
946 | struct bbm_log_entry *e = &log->marked_block_entries[pos]; | |
947 | ||
948 | if ((e->marked_count + 1 == BBM_LOG_MAX_LBA_ENTRY_VAL) && | |
949 | (__le48_to_cpu(&e->defective_block_start) == sector)) { | |
950 | sector += BBM_LOG_MAX_LBA_ENTRY_VAL; | |
951 | length -= BBM_LOG_MAX_LBA_ENTRY_VAL; | |
952 | pos = pos + 1; | |
953 | continue; | |
954 | } | |
955 | entry = e; | |
956 | break; | |
957 | } | |
958 | ||
959 | if (entry) { | |
960 | int cnt = (length <= BBM_LOG_MAX_LBA_ENTRY_VAL) ? length : | |
961 | BBM_LOG_MAX_LBA_ENTRY_VAL; | |
962 | entry->defective_block_start = __cpu_to_le48(sector); | |
963 | entry->marked_count = cnt - 1; | |
964 | if (cnt == length) | |
965 | return 1; | |
966 | sector += cnt; | |
967 | length -= cnt; | |
968 | } | |
969 | ||
970 | new_bb = ROUND_UP(length, BBM_LOG_MAX_LBA_ENTRY_VAL) / | |
971 | BBM_LOG_MAX_LBA_ENTRY_VAL; | |
972 | if (log->entry_count + new_bb > BBM_LOG_MAX_ENTRIES) | |
973 | return 0; | |
974 | ||
975 | while (length > 0) { | |
976 | int cnt = (length <= BBM_LOG_MAX_LBA_ENTRY_VAL) ? length : | |
977 | BBM_LOG_MAX_LBA_ENTRY_VAL; | |
978 | struct bbm_log_entry *entry = | |
979 | &log->marked_block_entries[log->entry_count]; | |
980 | ||
981 | entry->defective_block_start = __cpu_to_le48(sector); | |
982 | entry->marked_count = cnt - 1; | |
983 | entry->disk_ordinal = idx; | |
984 | ||
985 | sector += cnt; | |
986 | length -= cnt; | |
987 | ||
988 | log->entry_count++; | |
989 | } | |
990 | ||
991 | return new_bb; | |
992 | } | |
c07a5a4f | 993 | |
4c9e8c1e TM |
994 | /* clear all bad blocks for given disk */ |
995 | static void clear_disk_badblocks(struct bbm_log *log, const __u8 idx) | |
996 | { | |
997 | __u32 i = 0; | |
998 | ||
999 | while (i < log->entry_count) { | |
1000 | struct bbm_log_entry *entries = log->marked_block_entries; | |
1001 | ||
1002 | if (entries[i].disk_ordinal == idx) { | |
1003 | if (i < log->entry_count - 1) | |
1004 | entries[i] = entries[log->entry_count - 1]; | |
1005 | log->entry_count--; | |
1006 | } else { | |
1007 | i++; | |
1008 | } | |
1009 | } | |
1010 | } | |
1011 | ||
c07a5a4f TM |
1012 | /* clear given bad block */ |
1013 | static int clear_badblock(struct bbm_log *log, const __u8 idx, const unsigned | |
1014 | long long sector, const int length) { | |
1015 | __u32 i = 0; | |
1016 | ||
1017 | while (i < log->entry_count) { | |
1018 | struct bbm_log_entry *entries = log->marked_block_entries; | |
1019 | ||
1020 | if ((entries[i].disk_ordinal == idx) && | |
1021 | (__le48_to_cpu(&entries[i].defective_block_start) == | |
1022 | sector) && (entries[i].marked_count + 1 == length)) { | |
1023 | if (i < log->entry_count - 1) | |
1024 | entries[i] = entries[log->entry_count - 1]; | |
1025 | log->entry_count--; | |
1026 | break; | |
1027 | } | |
1028 | i++; | |
1029 | } | |
1030 | ||
1031 | return 1; | |
1032 | } | |
8d67477f TM |
1033 | |
1034 | /* allocate and load BBM log from metadata */ | |
1035 | static int load_bbm_log(struct intel_super *super) | |
1036 | { | |
1037 | struct imsm_super *mpb = super->anchor; | |
1038 | __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size); | |
1039 | ||
1040 | super->bbm_log = xcalloc(1, sizeof(struct bbm_log)); | |
1041 | if (!super->bbm_log) | |
1042 | return 1; | |
1043 | ||
1044 | if (bbm_log_size) { | |
1045 | struct bbm_log *log = (void *)mpb + | |
1046 | __le32_to_cpu(mpb->mpb_size) - bbm_log_size; | |
1047 | ||
1048 | __u32 entry_count; | |
1049 | ||
1050 | if (bbm_log_size < sizeof(log->signature) + | |
1051 | sizeof(log->entry_count)) | |
1052 | return 2; | |
1053 | ||
1054 | entry_count = __le32_to_cpu(log->entry_count); | |
1055 | if ((__le32_to_cpu(log->signature) != BBM_LOG_SIGNATURE) || | |
1056 | (entry_count > BBM_LOG_MAX_ENTRIES)) | |
1057 | return 3; | |
1058 | ||
1059 | if (bbm_log_size != | |
1060 | sizeof(log->signature) + sizeof(log->entry_count) + | |
1061 | entry_count * sizeof(struct bbm_log_entry)) | |
1062 | return 4; | |
1063 | ||
1064 | memcpy(super->bbm_log, log, bbm_log_size); | |
1065 | } else { | |
1066 | super->bbm_log->signature = __cpu_to_le32(BBM_LOG_SIGNATURE); | |
1067 | super->bbm_log->entry_count = 0; | |
1068 | } | |
1069 | ||
1070 | return 0; | |
1071 | } | |
1072 | ||
b12796be TM |
1073 | /* checks if bad block is within volume boundaries */ |
1074 | static int is_bad_block_in_volume(const struct bbm_log_entry *entry, | |
1075 | const unsigned long long start_sector, | |
1076 | const unsigned long long size) | |
1077 | { | |
1078 | unsigned long long bb_start; | |
1079 | unsigned long long bb_end; | |
1080 | ||
1081 | bb_start = __le48_to_cpu(&entry->defective_block_start); | |
1082 | bb_end = bb_start + (entry->marked_count + 1); | |
1083 | ||
1084 | if (((bb_start >= start_sector) && (bb_start < start_sector + size)) || | |
1085 | ((bb_end >= start_sector) && (bb_end <= start_sector + size))) | |
1086 | return 1; | |
1087 | ||
1088 | return 0; | |
1089 | } | |
1090 | ||
1091 | /* get list of bad blocks on a drive for a volume */ | |
1092 | static void get_volume_badblocks(const struct bbm_log *log, const __u8 idx, | |
1093 | const unsigned long long start_sector, | |
1094 | const unsigned long long size, | |
1095 | struct md_bb *bbs) | |
1096 | { | |
1097 | __u32 count = 0; | |
1098 | __u32 i; | |
1099 | ||
1100 | for (i = 0; i < log->entry_count; i++) { | |
1101 | const struct bbm_log_entry *ent = | |
1102 | &log->marked_block_entries[i]; | |
1103 | struct md_bb_entry *bb; | |
1104 | ||
1105 | if ((ent->disk_ordinal == idx) && | |
1106 | is_bad_block_in_volume(ent, start_sector, size)) { | |
1107 | ||
1108 | if (!bbs->entries) { | |
1109 | bbs->entries = xmalloc(BBM_LOG_MAX_ENTRIES * | |
1110 | sizeof(*bb)); | |
1111 | if (!bbs->entries) | |
1112 | break; | |
1113 | } | |
1114 | ||
1115 | bb = &bbs->entries[count++]; | |
1116 | bb->sector = __le48_to_cpu(&ent->defective_block_start); | |
1117 | bb->length = ent->marked_count + 1; | |
1118 | } | |
1119 | } | |
1120 | bbs->count = count; | |
1121 | } | |
1122 | ||
98130f40 AK |
1123 | /* |
1124 | * for second_map: | |
238c0a71 AK |
1125 | * == MAP_0 get first map |
1126 | * == MAP_1 get second map | |
1127 | * == MAP_X than get map according to the current migr_state | |
98130f40 AK |
1128 | */ |
1129 | static __u32 get_imsm_ord_tbl_ent(struct imsm_dev *dev, | |
1130 | int slot, | |
1131 | int second_map) | |
7eef0453 DW |
1132 | { |
1133 | struct imsm_map *map; | |
1134 | ||
5e7b0330 | 1135 | map = get_imsm_map(dev, second_map); |
7eef0453 | 1136 | |
ff077194 DW |
1137 | /* top byte identifies disk under rebuild */ |
1138 | return __le32_to_cpu(map->disk_ord_tbl[slot]); | |
1139 | } | |
1140 | ||
1141 | #define ord_to_idx(ord) (((ord) << 8) >> 8) | |
98130f40 | 1142 | static __u32 get_imsm_disk_idx(struct imsm_dev *dev, int slot, int second_map) |
ff077194 | 1143 | { |
98130f40 | 1144 | __u32 ord = get_imsm_ord_tbl_ent(dev, slot, second_map); |
ff077194 DW |
1145 | |
1146 | return ord_to_idx(ord); | |
7eef0453 DW |
1147 | } |
1148 | ||
be73972f DW |
1149 | static void set_imsm_ord_tbl_ent(struct imsm_map *map, int slot, __u32 ord) |
1150 | { | |
1151 | map->disk_ord_tbl[slot] = __cpu_to_le32(ord); | |
1152 | } | |
1153 | ||
f21e18ca | 1154 | static int get_imsm_disk_slot(struct imsm_map *map, unsigned idx) |
620b1713 DW |
1155 | { |
1156 | int slot; | |
1157 | __u32 ord; | |
1158 | ||
1159 | for (slot = 0; slot < map->num_members; slot++) { | |
1160 | ord = __le32_to_cpu(map->disk_ord_tbl[slot]); | |
1161 | if (ord_to_idx(ord) == idx) | |
1162 | return slot; | |
1163 | } | |
1164 | ||
1165 | return -1; | |
1166 | } | |
1167 | ||
cdddbdbc DW |
1168 | static int get_imsm_raid_level(struct imsm_map *map) |
1169 | { | |
1170 | if (map->raid_level == 1) { | |
1171 | if (map->num_members == 2) | |
1172 | return 1; | |
1173 | else | |
1174 | return 10; | |
1175 | } | |
1176 | ||
1177 | return map->raid_level; | |
1178 | } | |
1179 | ||
c2c087e6 DW |
1180 | static int cmp_extent(const void *av, const void *bv) |
1181 | { | |
1182 | const struct extent *a = av; | |
1183 | const struct extent *b = bv; | |
1184 | if (a->start < b->start) | |
1185 | return -1; | |
1186 | if (a->start > b->start) | |
1187 | return 1; | |
1188 | return 0; | |
1189 | } | |
1190 | ||
0dcecb2e | 1191 | static int count_memberships(struct dl *dl, struct intel_super *super) |
c2c087e6 | 1192 | { |
c2c087e6 | 1193 | int memberships = 0; |
620b1713 | 1194 | int i; |
c2c087e6 | 1195 | |
949c47a0 DW |
1196 | for (i = 0; i < super->anchor->num_raid_devs; i++) { |
1197 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
238c0a71 | 1198 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
c2c087e6 | 1199 | |
620b1713 DW |
1200 | if (get_imsm_disk_slot(map, dl->index) >= 0) |
1201 | memberships++; | |
c2c087e6 | 1202 | } |
0dcecb2e DW |
1203 | |
1204 | return memberships; | |
1205 | } | |
1206 | ||
b81221b7 CA |
1207 | static __u32 imsm_min_reserved_sectors(struct intel_super *super); |
1208 | ||
486720e0 | 1209 | static int split_ull(unsigned long long n, void *lo, void *hi) |
5551b113 CA |
1210 | { |
1211 | if (lo == 0 || hi == 0) | |
1212 | return 1; | |
486720e0 JS |
1213 | __put_unaligned32(__cpu_to_le32((__u32)n), lo); |
1214 | __put_unaligned32(__cpu_to_le32((n >> 32)), hi); | |
5551b113 CA |
1215 | return 0; |
1216 | } | |
1217 | ||
1218 | static unsigned long long join_u32(__u32 lo, __u32 hi) | |
1219 | { | |
1220 | return (unsigned long long)__le32_to_cpu(lo) | | |
1221 | (((unsigned long long)__le32_to_cpu(hi)) << 32); | |
1222 | } | |
1223 | ||
1224 | static unsigned long long total_blocks(struct imsm_disk *disk) | |
1225 | { | |
1226 | if (disk == NULL) | |
1227 | return 0; | |
1228 | return join_u32(disk->total_blocks_lo, disk->total_blocks_hi); | |
1229 | } | |
1230 | ||
1c275381 MT |
1231 | /** |
1232 | * imsm_num_data_members() - get data drives count for an array. | |
1233 | * @map: Map to analyze. | |
1234 | * | |
1235 | * num_data_members value represents minimal count of drives for level. | |
1236 | * The name of the property could be misleading for RAID5 with asymmetric layout | |
1237 | * because some data required to be calculated from parity. | |
1238 | * The property is extracted from level and num_members value. | |
1239 | * | |
1240 | * Return: num_data_members value on success, zero otherwise. | |
1241 | */ | |
1242 | static __u8 imsm_num_data_members(struct imsm_map *map) | |
1243 | { | |
1244 | switch (get_imsm_raid_level(map)) { | |
1245 | case 0: | |
1246 | return map->num_members; | |
1247 | case 1: | |
1248 | case 10: | |
1249 | return map->num_members / 2; | |
1250 | case 5: | |
1251 | return map->num_members - 1; | |
1252 | default: | |
1253 | dprintf("unsupported raid level\n"); | |
1254 | return 0; | |
1255 | } | |
1256 | } | |
1257 | ||
5551b113 CA |
1258 | static unsigned long long pba_of_lba0(struct imsm_map *map) |
1259 | { | |
1260 | if (map == NULL) | |
1261 | return 0; | |
1262 | return join_u32(map->pba_of_lba0_lo, map->pba_of_lba0_hi); | |
1263 | } | |
1264 | ||
1265 | static unsigned long long blocks_per_member(struct imsm_map *map) | |
1266 | { | |
1267 | if (map == NULL) | |
1268 | return 0; | |
1269 | return join_u32(map->blocks_per_member_lo, map->blocks_per_member_hi); | |
1270 | } | |
1271 | ||
1272 | static unsigned long long num_data_stripes(struct imsm_map *map) | |
1273 | { | |
1274 | if (map == NULL) | |
1275 | return 0; | |
1276 | return join_u32(map->num_data_stripes_lo, map->num_data_stripes_hi); | |
1277 | } | |
1278 | ||
4036e7ee MT |
1279 | static unsigned long long vol_curr_migr_unit(struct imsm_dev *dev) |
1280 | { | |
1281 | if (dev == NULL) | |
1282 | return 0; | |
1283 | ||
1284 | return join_u32(dev->vol.curr_migr_unit_lo, dev->vol.curr_migr_unit_hi); | |
1285 | } | |
1286 | ||
fcc2c9da MD |
1287 | static unsigned long long imsm_dev_size(struct imsm_dev *dev) |
1288 | { | |
1289 | if (dev == NULL) | |
1290 | return 0; | |
1291 | return join_u32(dev->size_low, dev->size_high); | |
1292 | } | |
1293 | ||
9f421827 PB |
1294 | static unsigned long long migr_chkp_area_pba(struct migr_record *migr_rec) |
1295 | { | |
1296 | if (migr_rec == NULL) | |
1297 | return 0; | |
1298 | return join_u32(migr_rec->ckpt_area_pba_lo, | |
1299 | migr_rec->ckpt_area_pba_hi); | |
1300 | } | |
1301 | ||
1302 | static unsigned long long current_migr_unit(struct migr_record *migr_rec) | |
1303 | { | |
1304 | if (migr_rec == NULL) | |
1305 | return 0; | |
1306 | return join_u32(migr_rec->curr_migr_unit_lo, | |
1307 | migr_rec->curr_migr_unit_hi); | |
1308 | } | |
1309 | ||
1310 | static unsigned long long migr_dest_1st_member_lba(struct migr_record *migr_rec) | |
1311 | { | |
1312 | if (migr_rec == NULL) | |
1313 | return 0; | |
1314 | return join_u32(migr_rec->dest_1st_member_lba_lo, | |
1315 | migr_rec->dest_1st_member_lba_hi); | |
1316 | } | |
1317 | ||
1318 | static unsigned long long get_num_migr_units(struct migr_record *migr_rec) | |
1319 | { | |
1320 | if (migr_rec == NULL) | |
1321 | return 0; | |
1322 | return join_u32(migr_rec->num_migr_units_lo, | |
1323 | migr_rec->num_migr_units_hi); | |
1324 | } | |
1325 | ||
5551b113 CA |
1326 | static void set_total_blocks(struct imsm_disk *disk, unsigned long long n) |
1327 | { | |
1328 | split_ull(n, &disk->total_blocks_lo, &disk->total_blocks_hi); | |
1329 | } | |
1330 | ||
1c275381 MT |
1331 | /** |
1332 | * set_num_domains() - Set number of domains for an array. | |
1333 | * @map: Map to be updated. | |
1334 | * | |
1335 | * num_domains property represents copies count of each data drive, thus make | |
1336 | * it meaningful only for RAID1 and RAID10. IMSM supports two domains for | |
1337 | * raid1 and raid10. | |
1338 | */ | |
1339 | static void set_num_domains(struct imsm_map *map) | |
1340 | { | |
1341 | int level = get_imsm_raid_level(map); | |
1342 | ||
1343 | if (level == 1 || level == 10) | |
1344 | map->num_domains = 2; | |
1345 | else | |
1346 | map->num_domains = 1; | |
1347 | } | |
1348 | ||
5551b113 CA |
1349 | static void set_pba_of_lba0(struct imsm_map *map, unsigned long long n) |
1350 | { | |
1351 | split_ull(n, &map->pba_of_lba0_lo, &map->pba_of_lba0_hi); | |
1352 | } | |
1353 | ||
1354 | static void set_blocks_per_member(struct imsm_map *map, unsigned long long n) | |
1355 | { | |
1356 | split_ull(n, &map->blocks_per_member_lo, &map->blocks_per_member_hi); | |
1357 | } | |
1358 | ||
1359 | static void set_num_data_stripes(struct imsm_map *map, unsigned long long n) | |
1360 | { | |
1361 | split_ull(n, &map->num_data_stripes_lo, &map->num_data_stripes_hi); | |
1362 | } | |
1363 | ||
1c275381 MT |
1364 | /** |
1365 | * update_num_data_stripes() - Calculate and update num_data_stripes value. | |
1366 | * @map: map to be updated. | |
1367 | * @dev_size: size of volume. | |
1368 | * | |
1369 | * num_data_stripes value is addictionally divided by num_domains, therefore for | |
1370 | * levels where num_domains is not 1, nds is a part of real value. | |
1371 | */ | |
1372 | static void update_num_data_stripes(struct imsm_map *map, | |
1373 | unsigned long long dev_size) | |
1374 | { | |
1375 | unsigned long long nds = dev_size / imsm_num_data_members(map); | |
1376 | ||
1377 | nds /= map->num_domains; | |
1378 | nds /= map->blocks_per_strip; | |
1379 | set_num_data_stripes(map, nds); | |
1380 | } | |
1381 | ||
4036e7ee MT |
1382 | static void set_vol_curr_migr_unit(struct imsm_dev *dev, unsigned long long n) |
1383 | { | |
1384 | if (dev == NULL) | |
1385 | return; | |
1386 | ||
1387 | split_ull(n, &dev->vol.curr_migr_unit_lo, &dev->vol.curr_migr_unit_hi); | |
1388 | } | |
1389 | ||
fcc2c9da MD |
1390 | static void set_imsm_dev_size(struct imsm_dev *dev, unsigned long long n) |
1391 | { | |
1392 | split_ull(n, &dev->size_low, &dev->size_high); | |
1393 | } | |
1394 | ||
9f421827 PB |
1395 | static void set_migr_chkp_area_pba(struct migr_record *migr_rec, |
1396 | unsigned long long n) | |
1397 | { | |
1398 | split_ull(n, &migr_rec->ckpt_area_pba_lo, &migr_rec->ckpt_area_pba_hi); | |
1399 | } | |
1400 | ||
1401 | static void set_current_migr_unit(struct migr_record *migr_rec, | |
1402 | unsigned long long n) | |
1403 | { | |
1404 | split_ull(n, &migr_rec->curr_migr_unit_lo, | |
1405 | &migr_rec->curr_migr_unit_hi); | |
1406 | } | |
1407 | ||
1408 | static void set_migr_dest_1st_member_lba(struct migr_record *migr_rec, | |
1409 | unsigned long long n) | |
1410 | { | |
1411 | split_ull(n, &migr_rec->dest_1st_member_lba_lo, | |
1412 | &migr_rec->dest_1st_member_lba_hi); | |
1413 | } | |
1414 | ||
1415 | static void set_num_migr_units(struct migr_record *migr_rec, | |
1416 | unsigned long long n) | |
1417 | { | |
1418 | split_ull(n, &migr_rec->num_migr_units_lo, | |
1419 | &migr_rec->num_migr_units_hi); | |
1420 | } | |
1421 | ||
44490938 MD |
1422 | static unsigned long long per_dev_array_size(struct imsm_map *map) |
1423 | { | |
1424 | unsigned long long array_size = 0; | |
1425 | ||
1426 | if (map == NULL) | |
1427 | return array_size; | |
1428 | ||
1429 | array_size = num_data_stripes(map) * map->blocks_per_strip; | |
1430 | if (get_imsm_raid_level(map) == 1 || get_imsm_raid_level(map) == 10) | |
1431 | array_size *= 2; | |
1432 | ||
1433 | return array_size; | |
1434 | } | |
1435 | ||
05501181 PB |
1436 | static struct extent *get_extents(struct intel_super *super, struct dl *dl, |
1437 | int get_minimal_reservation) | |
0dcecb2e DW |
1438 | { |
1439 | /* find a list of used extents on the given physical device */ | |
1440 | struct extent *rv, *e; | |
620b1713 | 1441 | int i; |
0dcecb2e | 1442 | int memberships = count_memberships(dl, super); |
b276dd33 DW |
1443 | __u32 reservation; |
1444 | ||
1445 | /* trim the reserved area for spares, so they can join any array | |
1446 | * regardless of whether the OROM has assigned sectors from the | |
1447 | * IMSM_RESERVED_SECTORS region | |
1448 | */ | |
05501181 | 1449 | if (dl->index == -1 || get_minimal_reservation) |
b81221b7 | 1450 | reservation = imsm_min_reserved_sectors(super); |
b276dd33 DW |
1451 | else |
1452 | reservation = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS; | |
0dcecb2e | 1453 | |
503975b9 | 1454 | rv = xcalloc(sizeof(struct extent), (memberships + 1)); |
c2c087e6 DW |
1455 | e = rv; |
1456 | ||
949c47a0 DW |
1457 | for (i = 0; i < super->anchor->num_raid_devs; i++) { |
1458 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
238c0a71 | 1459 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
c2c087e6 | 1460 | |
620b1713 | 1461 | if (get_imsm_disk_slot(map, dl->index) >= 0) { |
5551b113 | 1462 | e->start = pba_of_lba0(map); |
44490938 | 1463 | e->size = per_dev_array_size(map); |
620b1713 | 1464 | e++; |
c2c087e6 DW |
1465 | } |
1466 | } | |
1467 | qsort(rv, memberships, sizeof(*rv), cmp_extent); | |
1468 | ||
1011e834 | 1469 | /* determine the start of the metadata |
14e8215b DW |
1470 | * when no raid devices are defined use the default |
1471 | * ...otherwise allow the metadata to truncate the value | |
1472 | * as is the case with older versions of imsm | |
1473 | */ | |
1474 | if (memberships) { | |
1475 | struct extent *last = &rv[memberships - 1]; | |
5551b113 | 1476 | unsigned long long remainder; |
14e8215b | 1477 | |
5551b113 | 1478 | remainder = total_blocks(&dl->disk) - (last->start + last->size); |
dda5855f DW |
1479 | /* round down to 1k block to satisfy precision of the kernel |
1480 | * 'size' interface | |
1481 | */ | |
1482 | remainder &= ~1UL; | |
1483 | /* make sure remainder is still sane */ | |
f21e18ca | 1484 | if (remainder < (unsigned)ROUND_UP(super->len, 512) >> 9) |
dda5855f | 1485 | remainder = ROUND_UP(super->len, 512) >> 9; |
14e8215b DW |
1486 | if (reservation > remainder) |
1487 | reservation = remainder; | |
1488 | } | |
5551b113 | 1489 | e->start = total_blocks(&dl->disk) - reservation; |
c2c087e6 DW |
1490 | e->size = 0; |
1491 | return rv; | |
1492 | } | |
1493 | ||
14e8215b DW |
1494 | /* try to determine how much space is reserved for metadata from |
1495 | * the last get_extents() entry, otherwise fallback to the | |
1496 | * default | |
1497 | */ | |
1498 | static __u32 imsm_reserved_sectors(struct intel_super *super, struct dl *dl) | |
1499 | { | |
1500 | struct extent *e; | |
1501 | int i; | |
1502 | __u32 rv; | |
1503 | ||
1504 | /* for spares just return a minimal reservation which will grow | |
1505 | * once the spare is picked up by an array | |
1506 | */ | |
1507 | if (dl->index == -1) | |
1508 | return MPB_SECTOR_CNT; | |
1509 | ||
05501181 | 1510 | e = get_extents(super, dl, 0); |
14e8215b DW |
1511 | if (!e) |
1512 | return MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS; | |
1513 | ||
1514 | /* scroll to last entry */ | |
1515 | for (i = 0; e[i].size; i++) | |
1516 | continue; | |
1517 | ||
5551b113 | 1518 | rv = total_blocks(&dl->disk) - e[i].start; |
14e8215b DW |
1519 | |
1520 | free(e); | |
1521 | ||
1522 | return rv; | |
1523 | } | |
1524 | ||
25ed7e59 DW |
1525 | static int is_spare(struct imsm_disk *disk) |
1526 | { | |
1527 | return (disk->status & SPARE_DISK) == SPARE_DISK; | |
1528 | } | |
1529 | ||
1530 | static int is_configured(struct imsm_disk *disk) | |
1531 | { | |
1532 | return (disk->status & CONFIGURED_DISK) == CONFIGURED_DISK; | |
1533 | } | |
1534 | ||
1535 | static int is_failed(struct imsm_disk *disk) | |
1536 | { | |
1537 | return (disk->status & FAILED_DISK) == FAILED_DISK; | |
1538 | } | |
1539 | ||
2432ce9b AP |
1540 | static int is_journal(struct imsm_disk *disk) |
1541 | { | |
1542 | return (disk->status & JOURNAL_DISK) == JOURNAL_DISK; | |
1543 | } | |
1544 | ||
b53bfba6 TM |
1545 | /* round array size down to closest MB and ensure it splits evenly |
1546 | * between members | |
1547 | */ | |
1548 | static unsigned long long round_size_to_mb(unsigned long long size, unsigned int | |
1549 | disk_count) | |
1550 | { | |
1551 | size /= disk_count; | |
1552 | size = (size >> SECT_PER_MB_SHIFT) << SECT_PER_MB_SHIFT; | |
1553 | size *= disk_count; | |
1554 | ||
1555 | return size; | |
1556 | } | |
1557 | ||
8b9cd157 MK |
1558 | static int able_to_resync(int raid_level, int missing_disks) |
1559 | { | |
1560 | int max_missing_disks = 0; | |
1561 | ||
1562 | switch (raid_level) { | |
1563 | case 10: | |
1564 | max_missing_disks = 1; | |
1565 | break; | |
1566 | default: | |
1567 | max_missing_disks = 0; | |
1568 | } | |
1569 | return missing_disks <= max_missing_disks; | |
1570 | } | |
1571 | ||
b81221b7 CA |
1572 | /* try to determine how much space is reserved for metadata from |
1573 | * the last get_extents() entry on the smallest active disk, | |
1574 | * otherwise fallback to the default | |
1575 | */ | |
1576 | static __u32 imsm_min_reserved_sectors(struct intel_super *super) | |
1577 | { | |
1578 | struct extent *e; | |
1579 | int i; | |
5551b113 CA |
1580 | unsigned long long min_active; |
1581 | __u32 remainder; | |
b81221b7 CA |
1582 | __u32 rv = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS; |
1583 | struct dl *dl, *dl_min = NULL; | |
1584 | ||
1585 | if (!super) | |
1586 | return rv; | |
1587 | ||
1588 | min_active = 0; | |
1589 | for (dl = super->disks; dl; dl = dl->next) { | |
1590 | if (dl->index < 0) | |
1591 | continue; | |
5551b113 CA |
1592 | unsigned long long blocks = total_blocks(&dl->disk); |
1593 | if (blocks < min_active || min_active == 0) { | |
b81221b7 | 1594 | dl_min = dl; |
5551b113 | 1595 | min_active = blocks; |
b81221b7 CA |
1596 | } |
1597 | } | |
1598 | if (!dl_min) | |
1599 | return rv; | |
1600 | ||
1601 | /* find last lba used by subarrays on the smallest active disk */ | |
05501181 | 1602 | e = get_extents(super, dl_min, 0); |
b81221b7 CA |
1603 | if (!e) |
1604 | return rv; | |
1605 | for (i = 0; e[i].size; i++) | |
1606 | continue; | |
1607 | ||
1608 | remainder = min_active - e[i].start; | |
1609 | free(e); | |
1610 | ||
1611 | /* to give priority to recovery we should not require full | |
1612 | IMSM_RESERVED_SECTORS from the spare */ | |
1613 | rv = MPB_SECTOR_CNT + NUM_BLOCKS_DIRTY_STRIPE_REGION; | |
1614 | ||
1615 | /* if real reservation is smaller use that value */ | |
1616 | return (remainder < rv) ? remainder : rv; | |
1617 | } | |
1618 | ||
fbfdcb06 AO |
1619 | /* |
1620 | * Return minimum size of a spare and sector size | |
1621 | * that can be used in this array | |
1622 | */ | |
1623 | int get_spare_criteria_imsm(struct supertype *st, struct spare_criteria *c) | |
80e7f8c3 AC |
1624 | { |
1625 | struct intel_super *super = st->sb; | |
1626 | struct dl *dl; | |
1627 | struct extent *e; | |
1628 | int i; | |
fbfdcb06 AO |
1629 | unsigned long long size = 0; |
1630 | ||
1631 | c->min_size = 0; | |
4b57ecf6 | 1632 | c->sector_size = 0; |
80e7f8c3 AC |
1633 | |
1634 | if (!super) | |
fbfdcb06 | 1635 | return -EINVAL; |
80e7f8c3 AC |
1636 | /* find first active disk in array */ |
1637 | dl = super->disks; | |
1638 | while (dl && (is_failed(&dl->disk) || dl->index == -1)) | |
1639 | dl = dl->next; | |
1640 | if (!dl) | |
fbfdcb06 | 1641 | return -EINVAL; |
80e7f8c3 | 1642 | /* find last lba used by subarrays */ |
05501181 | 1643 | e = get_extents(super, dl, 0); |
80e7f8c3 | 1644 | if (!e) |
fbfdcb06 | 1645 | return -EINVAL; |
80e7f8c3 AC |
1646 | for (i = 0; e[i].size; i++) |
1647 | continue; | |
1648 | if (i > 0) | |
fbfdcb06 | 1649 | size = e[i-1].start + e[i-1].size; |
80e7f8c3 | 1650 | free(e); |
b81221b7 | 1651 | |
80e7f8c3 | 1652 | /* add the amount of space needed for metadata */ |
fbfdcb06 AO |
1653 | size += imsm_min_reserved_sectors(super); |
1654 | ||
1655 | c->min_size = size * 512; | |
4b57ecf6 | 1656 | c->sector_size = super->sector_size; |
b81221b7 | 1657 | |
fbfdcb06 | 1658 | return 0; |
80e7f8c3 AC |
1659 | } |
1660 | ||
d1e02575 AK |
1661 | static int is_gen_migration(struct imsm_dev *dev); |
1662 | ||
f36a9ecd PB |
1663 | #define IMSM_4K_DIV 8 |
1664 | ||
c47b0ff6 AK |
1665 | static __u64 blocks_per_migr_unit(struct intel_super *super, |
1666 | struct imsm_dev *dev); | |
1e5c6983 | 1667 | |
c47b0ff6 AK |
1668 | static void print_imsm_dev(struct intel_super *super, |
1669 | struct imsm_dev *dev, | |
1670 | char *uuid, | |
1671 | int disk_idx) | |
cdddbdbc DW |
1672 | { |
1673 | __u64 sz; | |
0d80bb2f | 1674 | int slot, i; |
238c0a71 AK |
1675 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
1676 | struct imsm_map *map2 = get_imsm_map(dev, MAP_1); | |
b10b37b8 | 1677 | __u32 ord; |
cdddbdbc DW |
1678 | |
1679 | printf("\n"); | |
1e7bc0ed | 1680 | printf("[%.16s]:\n", dev->volume); |
ba1b3bc8 | 1681 | printf(" Subarray : %d\n", super->current_vol); |
44470971 | 1682 | printf(" UUID : %s\n", uuid); |
dd8bcb3b AK |
1683 | printf(" RAID Level : %d", get_imsm_raid_level(map)); |
1684 | if (map2) | |
1685 | printf(" <-- %d", get_imsm_raid_level(map2)); | |
1686 | printf("\n"); | |
1687 | printf(" Members : %d", map->num_members); | |
1688 | if (map2) | |
1689 | printf(" <-- %d", map2->num_members); | |
1690 | printf("\n"); | |
0d80bb2f DW |
1691 | printf(" Slots : ["); |
1692 | for (i = 0; i < map->num_members; i++) { | |
238c0a71 | 1693 | ord = get_imsm_ord_tbl_ent(dev, i, MAP_0); |
0d80bb2f DW |
1694 | printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U"); |
1695 | } | |
dd8bcb3b AK |
1696 | printf("]"); |
1697 | if (map2) { | |
1698 | printf(" <-- ["); | |
1699 | for (i = 0; i < map2->num_members; i++) { | |
238c0a71 | 1700 | ord = get_imsm_ord_tbl_ent(dev, i, MAP_1); |
dd8bcb3b AK |
1701 | printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U"); |
1702 | } | |
1703 | printf("]"); | |
1704 | } | |
1705 | printf("\n"); | |
7095bccb AK |
1706 | printf(" Failed disk : "); |
1707 | if (map->failed_disk_num == 0xff) | |
1708 | printf("none"); | |
1709 | else | |
1710 | printf("%i", map->failed_disk_num); | |
1711 | printf("\n"); | |
620b1713 DW |
1712 | slot = get_imsm_disk_slot(map, disk_idx); |
1713 | if (slot >= 0) { | |
238c0a71 | 1714 | ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X); |
b10b37b8 DW |
1715 | printf(" This Slot : %d%s\n", slot, |
1716 | ord & IMSM_ORD_REBUILD ? " (out-of-sync)" : ""); | |
1717 | } else | |
cdddbdbc | 1718 | printf(" This Slot : ?\n"); |
84918897 | 1719 | printf(" Sector Size : %u\n", super->sector_size); |
fcc2c9da | 1720 | sz = imsm_dev_size(dev); |
84918897 MK |
1721 | printf(" Array Size : %llu%s\n", |
1722 | (unsigned long long)sz * 512 / super->sector_size, | |
cdddbdbc | 1723 | human_size(sz * 512)); |
5551b113 | 1724 | sz = blocks_per_member(map); |
84918897 MK |
1725 | printf(" Per Dev Size : %llu%s\n", |
1726 | (unsigned long long)sz * 512 / super->sector_size, | |
cdddbdbc | 1727 | human_size(sz * 512)); |
5551b113 | 1728 | printf(" Sector Offset : %llu\n", |
7d8935cb | 1729 | pba_of_lba0(map) * 512 / super->sector_size); |
5551b113 CA |
1730 | printf(" Num Stripes : %llu\n", |
1731 | num_data_stripes(map)); | |
dd8bcb3b | 1732 | printf(" Chunk Size : %u KiB", |
cdddbdbc | 1733 | __le16_to_cpu(map->blocks_per_strip) / 2); |
dd8bcb3b AK |
1734 | if (map2) |
1735 | printf(" <-- %u KiB", | |
1736 | __le16_to_cpu(map2->blocks_per_strip) / 2); | |
1737 | printf("\n"); | |
cdddbdbc | 1738 | printf(" Reserved : %d\n", __le32_to_cpu(dev->reserved_blocks)); |
8655a7b1 | 1739 | printf(" Migrate State : "); |
1484e727 DW |
1740 | if (dev->vol.migr_state) { |
1741 | if (migr_type(dev) == MIGR_INIT) | |
8655a7b1 | 1742 | printf("initialize\n"); |
1484e727 | 1743 | else if (migr_type(dev) == MIGR_REBUILD) |
8655a7b1 | 1744 | printf("rebuild\n"); |
1484e727 | 1745 | else if (migr_type(dev) == MIGR_VERIFY) |
8655a7b1 | 1746 | printf("check\n"); |
1484e727 | 1747 | else if (migr_type(dev) == MIGR_GEN_MIGR) |
8655a7b1 | 1748 | printf("general migration\n"); |
1484e727 | 1749 | else if (migr_type(dev) == MIGR_STATE_CHANGE) |
8655a7b1 | 1750 | printf("state change\n"); |
1484e727 | 1751 | else if (migr_type(dev) == MIGR_REPAIR) |
8655a7b1 | 1752 | printf("repair\n"); |
1484e727 | 1753 | else |
8655a7b1 DW |
1754 | printf("<unknown:%d>\n", migr_type(dev)); |
1755 | } else | |
1756 | printf("idle\n"); | |
3393c6af DW |
1757 | printf(" Map State : %s", map_state_str[map->map_state]); |
1758 | if (dev->vol.migr_state) { | |
238c0a71 | 1759 | struct imsm_map *map = get_imsm_map(dev, MAP_1); |
1e5c6983 | 1760 | |
b10b37b8 | 1761 | printf(" <-- %s", map_state_str[map->map_state]); |
4036e7ee | 1762 | printf("\n Checkpoint : %llu ", vol_curr_migr_unit(dev)); |
089f9d79 | 1763 | if (is_gen_migration(dev) && (slot > 1 || slot < 0)) |
464d40e8 LD |
1764 | printf("(N/A)"); |
1765 | else | |
1766 | printf("(%llu)", (unsigned long long) | |
1767 | blocks_per_migr_unit(super, dev)); | |
3393c6af DW |
1768 | } |
1769 | printf("\n"); | |
2432ce9b AP |
1770 | printf(" Dirty State : %s\n", (dev->vol.dirty & RAIDVOL_DIRTY) ? |
1771 | "dirty" : "clean"); | |
1772 | printf(" RWH Policy : "); | |
c2462068 | 1773 | if (dev->rwh_policy == RWH_OFF || dev->rwh_policy == RWH_MULTIPLE_OFF) |
2432ce9b AP |
1774 | printf("off\n"); |
1775 | else if (dev->rwh_policy == RWH_DISTRIBUTED) | |
1776 | printf("PPL distributed\n"); | |
1777 | else if (dev->rwh_policy == RWH_JOURNALING_DRIVE) | |
1778 | printf("PPL journaling drive\n"); | |
c2462068 PB |
1779 | else if (dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED) |
1780 | printf("Multiple distributed PPLs\n"); | |
1781 | else if (dev->rwh_policy == RWH_MULTIPLE_PPLS_JOURNALING_DRIVE) | |
1782 | printf("Multiple PPLs on journaling drive\n"); | |
fbc42556 JR |
1783 | else if (dev->rwh_policy == RWH_BITMAP) |
1784 | printf("Write-intent bitmap\n"); | |
2432ce9b AP |
1785 | else |
1786 | printf("<unknown:%d>\n", dev->rwh_policy); | |
ba1b3bc8 AP |
1787 | |
1788 | printf(" Volume ID : %u\n", dev->my_vol_raid_dev_num); | |
cdddbdbc DW |
1789 | } |
1790 | ||
ef5c214e MK |
1791 | static void print_imsm_disk(struct imsm_disk *disk, |
1792 | int index, | |
1793 | __u32 reserved, | |
1794 | unsigned int sector_size) { | |
1f24f035 | 1795 | char str[MAX_RAID_SERIAL_LEN + 1]; |
cdddbdbc DW |
1796 | __u64 sz; |
1797 | ||
0ec1f4e8 | 1798 | if (index < -1 || !disk) |
e9d82038 DW |
1799 | return; |
1800 | ||
cdddbdbc | 1801 | printf("\n"); |
1f24f035 | 1802 | snprintf(str, MAX_RAID_SERIAL_LEN + 1, "%s", disk->serial); |
0ec1f4e8 DW |
1803 | if (index >= 0) |
1804 | printf(" Disk%02d Serial : %s\n", index, str); | |
1805 | else | |
1806 | printf(" Disk Serial : %s\n", str); | |
2432ce9b AP |
1807 | printf(" State :%s%s%s%s\n", is_spare(disk) ? " spare" : "", |
1808 | is_configured(disk) ? " active" : "", | |
1809 | is_failed(disk) ? " failed" : "", | |
1810 | is_journal(disk) ? " journal" : ""); | |
cdddbdbc | 1811 | printf(" Id : %08x\n", __le32_to_cpu(disk->scsi_id)); |
5551b113 | 1812 | sz = total_blocks(disk) - reserved; |
ef5c214e MK |
1813 | printf(" Usable Size : %llu%s\n", |
1814 | (unsigned long long)sz * 512 / sector_size, | |
cdddbdbc DW |
1815 | human_size(sz * 512)); |
1816 | } | |
1817 | ||
de44e46f PB |
1818 | void convert_to_4k_imsm_migr_rec(struct intel_super *super) |
1819 | { | |
1820 | struct migr_record *migr_rec = super->migr_rec; | |
1821 | ||
1822 | migr_rec->blocks_per_unit /= IMSM_4K_DIV; | |
de44e46f PB |
1823 | migr_rec->dest_depth_per_unit /= IMSM_4K_DIV; |
1824 | split_ull((join_u32(migr_rec->post_migr_vol_cap, | |
1825 | migr_rec->post_migr_vol_cap_hi) / IMSM_4K_DIV), | |
1826 | &migr_rec->post_migr_vol_cap, &migr_rec->post_migr_vol_cap_hi); | |
9f421827 PB |
1827 | set_migr_chkp_area_pba(migr_rec, |
1828 | migr_chkp_area_pba(migr_rec) / IMSM_4K_DIV); | |
1829 | set_migr_dest_1st_member_lba(migr_rec, | |
1830 | migr_dest_1st_member_lba(migr_rec) / IMSM_4K_DIV); | |
de44e46f PB |
1831 | } |
1832 | ||
f36a9ecd PB |
1833 | void convert_to_4k_imsm_disk(struct imsm_disk *disk) |
1834 | { | |
1835 | set_total_blocks(disk, (total_blocks(disk)/IMSM_4K_DIV)); | |
1836 | } | |
1837 | ||
1838 | void convert_to_4k(struct intel_super *super) | |
1839 | { | |
1840 | struct imsm_super *mpb = super->anchor; | |
1841 | struct imsm_disk *disk; | |
1842 | int i; | |
e4467bc7 | 1843 | __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size); |
f36a9ecd PB |
1844 | |
1845 | for (i = 0; i < mpb->num_disks ; i++) { | |
1846 | disk = __get_imsm_disk(mpb, i); | |
1847 | /* disk */ | |
1848 | convert_to_4k_imsm_disk(disk); | |
1849 | } | |
1850 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
1851 | struct imsm_dev *dev = __get_imsm_dev(mpb, i); | |
1852 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
1853 | /* dev */ | |
fcc2c9da | 1854 | set_imsm_dev_size(dev, imsm_dev_size(dev)/IMSM_4K_DIV); |
4036e7ee MT |
1855 | set_vol_curr_migr_unit(dev, |
1856 | vol_curr_migr_unit(dev) / IMSM_4K_DIV); | |
f36a9ecd PB |
1857 | |
1858 | /* map0 */ | |
1859 | set_blocks_per_member(map, blocks_per_member(map)/IMSM_4K_DIV); | |
1860 | map->blocks_per_strip /= IMSM_4K_DIV; | |
1861 | set_pba_of_lba0(map, pba_of_lba0(map)/IMSM_4K_DIV); | |
1862 | ||
1863 | if (dev->vol.migr_state) { | |
1864 | /* map1 */ | |
1865 | map = get_imsm_map(dev, MAP_1); | |
1866 | set_blocks_per_member(map, | |
1867 | blocks_per_member(map)/IMSM_4K_DIV); | |
1868 | map->blocks_per_strip /= IMSM_4K_DIV; | |
1869 | set_pba_of_lba0(map, pba_of_lba0(map)/IMSM_4K_DIV); | |
1870 | } | |
1871 | } | |
e4467bc7 TM |
1872 | if (bbm_log_size) { |
1873 | struct bbm_log *log = (void *)mpb + | |
1874 | __le32_to_cpu(mpb->mpb_size) - bbm_log_size; | |
1875 | __u32 i; | |
1876 | ||
1877 | for (i = 0; i < log->entry_count; i++) { | |
1878 | struct bbm_log_entry *entry = | |
1879 | &log->marked_block_entries[i]; | |
1880 | ||
1881 | __u8 count = entry->marked_count + 1; | |
1882 | unsigned long long sector = | |
1883 | __le48_to_cpu(&entry->defective_block_start); | |
1884 | ||
1885 | entry->defective_block_start = | |
1886 | __cpu_to_le48(sector/IMSM_4K_DIV); | |
1887 | entry->marked_count = max(count/IMSM_4K_DIV, 1) - 1; | |
1888 | } | |
1889 | } | |
f36a9ecd PB |
1890 | |
1891 | mpb->check_sum = __gen_imsm_checksum(mpb); | |
1892 | } | |
1893 | ||
520e69e2 AK |
1894 | void examine_migr_rec_imsm(struct intel_super *super) |
1895 | { | |
1896 | struct migr_record *migr_rec = super->migr_rec; | |
1897 | struct imsm_super *mpb = super->anchor; | |
1898 | int i; | |
1899 | ||
1900 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
1901 | struct imsm_dev *dev = __get_imsm_dev(mpb, i); | |
3136abe5 | 1902 | struct imsm_map *map; |
b4ab44d8 | 1903 | int slot = -1; |
3136abe5 | 1904 | |
520e69e2 AK |
1905 | if (is_gen_migration(dev) == 0) |
1906 | continue; | |
1907 | ||
1908 | printf("\nMigration Record Information:"); | |
3136abe5 | 1909 | |
44bfe6df AK |
1910 | /* first map under migration */ |
1911 | map = get_imsm_map(dev, MAP_0); | |
3136abe5 AK |
1912 | if (map) |
1913 | slot = get_imsm_disk_slot(map, super->disks->index); | |
089f9d79 | 1914 | if (map == NULL || slot > 1 || slot < 0) { |
520e69e2 AK |
1915 | printf(" Empty\n "); |
1916 | printf("Examine one of first two disks in array\n"); | |
1917 | break; | |
1918 | } | |
1919 | printf("\n Status : "); | |
1920 | if (__le32_to_cpu(migr_rec->rec_status) == UNIT_SRC_NORMAL) | |
1921 | printf("Normal\n"); | |
1922 | else | |
1923 | printf("Contains Data\n"); | |
9f421827 PB |
1924 | printf(" Current Unit : %llu\n", |
1925 | current_migr_unit(migr_rec)); | |
520e69e2 AK |
1926 | printf(" Family : %u\n", |
1927 | __le32_to_cpu(migr_rec->family_num)); | |
1928 | printf(" Ascending : %u\n", | |
1929 | __le32_to_cpu(migr_rec->ascending_migr)); | |
1930 | printf(" Blocks Per Unit : %u\n", | |
1931 | __le32_to_cpu(migr_rec->blocks_per_unit)); | |
1932 | printf(" Dest. Depth Per Unit : %u\n", | |
1933 | __le32_to_cpu(migr_rec->dest_depth_per_unit)); | |
9f421827 PB |
1934 | printf(" Checkpoint Area pba : %llu\n", |
1935 | migr_chkp_area_pba(migr_rec)); | |
1936 | printf(" First member lba : %llu\n", | |
1937 | migr_dest_1st_member_lba(migr_rec)); | |
1938 | printf(" Total Number of Units : %llu\n", | |
1939 | get_num_migr_units(migr_rec)); | |
1940 | printf(" Size of volume : %llu\n", | |
1941 | join_u32(migr_rec->post_migr_vol_cap, | |
1942 | migr_rec->post_migr_vol_cap_hi)); | |
520e69e2 AK |
1943 | printf(" Record was read from : %u\n", |
1944 | __le32_to_cpu(migr_rec->ckpt_read_disk_num)); | |
1945 | ||
1946 | break; | |
1947 | } | |
1948 | } | |
f36a9ecd | 1949 | |
de44e46f PB |
1950 | void convert_from_4k_imsm_migr_rec(struct intel_super *super) |
1951 | { | |
1952 | struct migr_record *migr_rec = super->migr_rec; | |
1953 | ||
1954 | migr_rec->blocks_per_unit *= IMSM_4K_DIV; | |
de44e46f PB |
1955 | migr_rec->dest_depth_per_unit *= IMSM_4K_DIV; |
1956 | split_ull((join_u32(migr_rec->post_migr_vol_cap, | |
1957 | migr_rec->post_migr_vol_cap_hi) * IMSM_4K_DIV), | |
1958 | &migr_rec->post_migr_vol_cap, | |
1959 | &migr_rec->post_migr_vol_cap_hi); | |
9f421827 PB |
1960 | set_migr_chkp_area_pba(migr_rec, |
1961 | migr_chkp_area_pba(migr_rec) * IMSM_4K_DIV); | |
1962 | set_migr_dest_1st_member_lba(migr_rec, | |
1963 | migr_dest_1st_member_lba(migr_rec) * IMSM_4K_DIV); | |
de44e46f PB |
1964 | } |
1965 | ||
f36a9ecd PB |
1966 | void convert_from_4k(struct intel_super *super) |
1967 | { | |
1968 | struct imsm_super *mpb = super->anchor; | |
1969 | struct imsm_disk *disk; | |
1970 | int i; | |
e4467bc7 | 1971 | __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size); |
f36a9ecd PB |
1972 | |
1973 | for (i = 0; i < mpb->num_disks ; i++) { | |
1974 | disk = __get_imsm_disk(mpb, i); | |
1975 | /* disk */ | |
1976 | set_total_blocks(disk, (total_blocks(disk)*IMSM_4K_DIV)); | |
1977 | } | |
1978 | ||
1979 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
1980 | struct imsm_dev *dev = __get_imsm_dev(mpb, i); | |
1981 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
1982 | /* dev */ | |
fcc2c9da | 1983 | set_imsm_dev_size(dev, imsm_dev_size(dev)*IMSM_4K_DIV); |
4036e7ee MT |
1984 | set_vol_curr_migr_unit(dev, |
1985 | vol_curr_migr_unit(dev) * IMSM_4K_DIV); | |
f36a9ecd PB |
1986 | |
1987 | /* map0 */ | |
1988 | set_blocks_per_member(map, blocks_per_member(map)*IMSM_4K_DIV); | |
1989 | map->blocks_per_strip *= IMSM_4K_DIV; | |
1990 | set_pba_of_lba0(map, pba_of_lba0(map)*IMSM_4K_DIV); | |
1991 | ||
1992 | if (dev->vol.migr_state) { | |
1993 | /* map1 */ | |
1994 | map = get_imsm_map(dev, MAP_1); | |
1995 | set_blocks_per_member(map, | |
1996 | blocks_per_member(map)*IMSM_4K_DIV); | |
1997 | map->blocks_per_strip *= IMSM_4K_DIV; | |
1998 | set_pba_of_lba0(map, pba_of_lba0(map)*IMSM_4K_DIV); | |
1999 | } | |
2000 | } | |
e4467bc7 TM |
2001 | if (bbm_log_size) { |
2002 | struct bbm_log *log = (void *)mpb + | |
2003 | __le32_to_cpu(mpb->mpb_size) - bbm_log_size; | |
2004 | __u32 i; | |
2005 | ||
2006 | for (i = 0; i < log->entry_count; i++) { | |
2007 | struct bbm_log_entry *entry = | |
2008 | &log->marked_block_entries[i]; | |
2009 | ||
2010 | __u8 count = entry->marked_count + 1; | |
2011 | unsigned long long sector = | |
2012 | __le48_to_cpu(&entry->defective_block_start); | |
2013 | ||
2014 | entry->defective_block_start = | |
2015 | __cpu_to_le48(sector*IMSM_4K_DIV); | |
2016 | entry->marked_count = count*IMSM_4K_DIV - 1; | |
2017 | } | |
2018 | } | |
f36a9ecd PB |
2019 | |
2020 | mpb->check_sum = __gen_imsm_checksum(mpb); | |
2021 | } | |
2022 | ||
19482bcc AK |
2023 | /******************************************************************************* |
2024 | * function: imsm_check_attributes | |
2025 | * Description: Function checks if features represented by attributes flags | |
1011e834 | 2026 | * are supported by mdadm. |
19482bcc AK |
2027 | * Parameters: |
2028 | * attributes - Attributes read from metadata | |
2029 | * Returns: | |
1011e834 N |
2030 | * 0 - passed attributes contains unsupported features flags |
2031 | * 1 - all features are supported | |
19482bcc AK |
2032 | ******************************************************************************/ |
2033 | static int imsm_check_attributes(__u32 attributes) | |
2034 | { | |
2035 | int ret_val = 1; | |
418f9b36 N |
2036 | __u32 not_supported = MPB_ATTRIB_SUPPORTED^0xffffffff; |
2037 | ||
2038 | not_supported &= ~MPB_ATTRIB_IGNORED; | |
19482bcc AK |
2039 | |
2040 | not_supported &= attributes; | |
2041 | if (not_supported) { | |
e7b84f9d | 2042 | pr_err("(IMSM): Unsupported attributes : %x\n", |
418f9b36 | 2043 | (unsigned)__le32_to_cpu(not_supported)); |
19482bcc AK |
2044 | if (not_supported & MPB_ATTRIB_CHECKSUM_VERIFY) { |
2045 | dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n"); | |
2046 | not_supported ^= MPB_ATTRIB_CHECKSUM_VERIFY; | |
2047 | } | |
2048 | if (not_supported & MPB_ATTRIB_2TB) { | |
2049 | dprintf("\t\tMPB_ATTRIB_2TB\n"); | |
2050 | not_supported ^= MPB_ATTRIB_2TB; | |
2051 | } | |
2052 | if (not_supported & MPB_ATTRIB_RAID0) { | |
2053 | dprintf("\t\tMPB_ATTRIB_RAID0\n"); | |
2054 | not_supported ^= MPB_ATTRIB_RAID0; | |
2055 | } | |
2056 | if (not_supported & MPB_ATTRIB_RAID1) { | |
2057 | dprintf("\t\tMPB_ATTRIB_RAID1\n"); | |
2058 | not_supported ^= MPB_ATTRIB_RAID1; | |
2059 | } | |
2060 | if (not_supported & MPB_ATTRIB_RAID10) { | |
2061 | dprintf("\t\tMPB_ATTRIB_RAID10\n"); | |
2062 | not_supported ^= MPB_ATTRIB_RAID10; | |
2063 | } | |
2064 | if (not_supported & MPB_ATTRIB_RAID1E) { | |
2065 | dprintf("\t\tMPB_ATTRIB_RAID1E\n"); | |
2066 | not_supported ^= MPB_ATTRIB_RAID1E; | |
2067 | } | |
2068 | if (not_supported & MPB_ATTRIB_RAID5) { | |
2069 | dprintf("\t\tMPB_ATTRIB_RAID5\n"); | |
2070 | not_supported ^= MPB_ATTRIB_RAID5; | |
2071 | } | |
2072 | if (not_supported & MPB_ATTRIB_RAIDCNG) { | |
2073 | dprintf("\t\tMPB_ATTRIB_RAIDCNG\n"); | |
2074 | not_supported ^= MPB_ATTRIB_RAIDCNG; | |
2075 | } | |
2076 | if (not_supported & MPB_ATTRIB_BBM) { | |
2077 | dprintf("\t\tMPB_ATTRIB_BBM\n"); | |
2078 | not_supported ^= MPB_ATTRIB_BBM; | |
2079 | } | |
2080 | if (not_supported & MPB_ATTRIB_CHECKSUM_VERIFY) { | |
2081 | dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n"); | |
2082 | not_supported ^= MPB_ATTRIB_CHECKSUM_VERIFY; | |
2083 | } | |
2084 | if (not_supported & MPB_ATTRIB_EXP_STRIPE_SIZE) { | |
2085 | dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n"); | |
2086 | not_supported ^= MPB_ATTRIB_EXP_STRIPE_SIZE; | |
2087 | } | |
2088 | if (not_supported & MPB_ATTRIB_2TB_DISK) { | |
2089 | dprintf("\t\tMPB_ATTRIB_2TB_DISK\n"); | |
2090 | not_supported ^= MPB_ATTRIB_2TB_DISK; | |
2091 | } | |
2092 | if (not_supported & MPB_ATTRIB_NEVER_USE2) { | |
2093 | dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n"); | |
2094 | not_supported ^= MPB_ATTRIB_NEVER_USE2; | |
2095 | } | |
2096 | if (not_supported & MPB_ATTRIB_NEVER_USE) { | |
2097 | dprintf("\t\tMPB_ATTRIB_NEVER_USE\n"); | |
2098 | not_supported ^= MPB_ATTRIB_NEVER_USE; | |
2099 | } | |
2100 | ||
2101 | if (not_supported) | |
1ade5cc1 | 2102 | dprintf("(IMSM): Unknown attributes : %x\n", not_supported); |
19482bcc AK |
2103 | |
2104 | ret_val = 0; | |
2105 | } | |
2106 | ||
2107 | return ret_val; | |
2108 | } | |
2109 | ||
a5d85af7 | 2110 | static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map); |
44470971 | 2111 | |
cdddbdbc DW |
2112 | static void examine_super_imsm(struct supertype *st, char *homehost) |
2113 | { | |
2114 | struct intel_super *super = st->sb; | |
949c47a0 | 2115 | struct imsm_super *mpb = super->anchor; |
cdddbdbc DW |
2116 | char str[MAX_SIGNATURE_LENGTH]; |
2117 | int i; | |
27fd6274 DW |
2118 | struct mdinfo info; |
2119 | char nbuf[64]; | |
cdddbdbc | 2120 | __u32 sum; |
14e8215b | 2121 | __u32 reserved = imsm_reserved_sectors(super, super->disks); |
94827db3 | 2122 | struct dl *dl; |
e48aed3c | 2123 | time_t creation_time; |
27fd6274 | 2124 | |
618f4e6d XN |
2125 | strncpy(str, (char *)mpb->sig, MPB_SIG_LEN); |
2126 | str[MPB_SIG_LEN-1] = '\0'; | |
cdddbdbc | 2127 | printf(" Magic : %s\n", str); |
cdddbdbc | 2128 | printf(" Version : %s\n", get_imsm_version(mpb)); |
148acb7b | 2129 | printf(" Orig Family : %08x\n", __le32_to_cpu(mpb->orig_family_num)); |
cdddbdbc DW |
2130 | printf(" Family : %08x\n", __le32_to_cpu(mpb->family_num)); |
2131 | printf(" Generation : %08x\n", __le32_to_cpu(mpb->generation_num)); | |
e48aed3c AP |
2132 | creation_time = __le64_to_cpu(mpb->creation_time); |
2133 | printf(" Creation Time : %.24s\n", | |
2134 | creation_time ? ctime(&creation_time) : "Unknown"); | |
19482bcc AK |
2135 | printf(" Attributes : "); |
2136 | if (imsm_check_attributes(mpb->attributes)) | |
2137 | printf("All supported\n"); | |
2138 | else | |
2139 | printf("not supported\n"); | |
a5d85af7 | 2140 | getinfo_super_imsm(st, &info, NULL); |
ae2bfd4e | 2141 | fname_from_uuid(st, &info, nbuf, ':'); |
27fd6274 | 2142 | printf(" UUID : %s\n", nbuf + 5); |
cdddbdbc DW |
2143 | sum = __le32_to_cpu(mpb->check_sum); |
2144 | printf(" Checksum : %08x %s\n", sum, | |
949c47a0 | 2145 | __gen_imsm_checksum(mpb) == sum ? "correct" : "incorrect"); |
f36a9ecd | 2146 | printf(" MPB Sectors : %d\n", mpb_sectors(mpb, super->sector_size)); |
cdddbdbc DW |
2147 | printf(" Disks : %d\n", mpb->num_disks); |
2148 | printf(" RAID Devices : %d\n", mpb->num_raid_devs); | |
ef5c214e MK |
2149 | print_imsm_disk(__get_imsm_disk(mpb, super->disks->index), |
2150 | super->disks->index, reserved, super->sector_size); | |
8d67477f | 2151 | if (get_imsm_bbm_log_size(super->bbm_log)) { |
604b746f JD |
2152 | struct bbm_log *log = super->bbm_log; |
2153 | ||
2154 | printf("\n"); | |
2155 | printf("Bad Block Management Log:\n"); | |
2156 | printf(" Log Size : %d\n", __le32_to_cpu(mpb->bbm_log_size)); | |
2157 | printf(" Signature : %x\n", __le32_to_cpu(log->signature)); | |
2158 | printf(" Entry Count : %d\n", __le32_to_cpu(log->entry_count)); | |
604b746f | 2159 | } |
44470971 DW |
2160 | for (i = 0; i < mpb->num_raid_devs; i++) { |
2161 | struct mdinfo info; | |
2162 | struct imsm_dev *dev = __get_imsm_dev(mpb, i); | |
2163 | ||
2164 | super->current_vol = i; | |
a5d85af7 | 2165 | getinfo_super_imsm(st, &info, NULL); |
ae2bfd4e | 2166 | fname_from_uuid(st, &info, nbuf, ':'); |
c47b0ff6 | 2167 | print_imsm_dev(super, dev, nbuf + 5, super->disks->index); |
44470971 | 2168 | } |
cdddbdbc DW |
2169 | for (i = 0; i < mpb->num_disks; i++) { |
2170 | if (i == super->disks->index) | |
2171 | continue; | |
ef5c214e MK |
2172 | print_imsm_disk(__get_imsm_disk(mpb, i), i, reserved, |
2173 | super->sector_size); | |
cdddbdbc | 2174 | } |
94827db3 | 2175 | |
0ec1f4e8 DW |
2176 | for (dl = super->disks; dl; dl = dl->next) |
2177 | if (dl->index == -1) | |
ef5c214e MK |
2178 | print_imsm_disk(&dl->disk, -1, reserved, |
2179 | super->sector_size); | |
520e69e2 AK |
2180 | |
2181 | examine_migr_rec_imsm(super); | |
cdddbdbc DW |
2182 | } |
2183 | ||
061f2c6a | 2184 | static void brief_examine_super_imsm(struct supertype *st, int verbose) |
cdddbdbc | 2185 | { |
27fd6274 | 2186 | /* We just write a generic IMSM ARRAY entry */ |
ff54de6e N |
2187 | struct mdinfo info; |
2188 | char nbuf[64]; | |
2189 | ||
a5d85af7 | 2190 | getinfo_super_imsm(st, &info, NULL); |
4737ae25 N |
2191 | fname_from_uuid(st, &info, nbuf, ':'); |
2192 | printf("ARRAY metadata=imsm UUID=%s\n", nbuf + 5); | |
2193 | } | |
2194 | ||
2195 | static void brief_examine_subarrays_imsm(struct supertype *st, int verbose) | |
2196 | { | |
2197 | /* We just write a generic IMSM ARRAY entry */ | |
2198 | struct mdinfo info; | |
2199 | char nbuf[64]; | |
2200 | char nbuf1[64]; | |
2201 | struct intel_super *super = st->sb; | |
2202 | int i; | |
2203 | ||
2204 | if (!super->anchor->num_raid_devs) | |
2205 | return; | |
2206 | ||
a5d85af7 | 2207 | getinfo_super_imsm(st, &info, NULL); |
ae2bfd4e | 2208 | fname_from_uuid(st, &info, nbuf, ':'); |
1e7bc0ed DW |
2209 | for (i = 0; i < super->anchor->num_raid_devs; i++) { |
2210 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
2211 | ||
2212 | super->current_vol = i; | |
a5d85af7 | 2213 | getinfo_super_imsm(st, &info, NULL); |
ae2bfd4e | 2214 | fname_from_uuid(st, &info, nbuf1, ':'); |
1124b3cf | 2215 | printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n", |
cf8de691 | 2216 | dev->volume, nbuf + 5, i, nbuf1 + 5); |
1e7bc0ed | 2217 | } |
cdddbdbc DW |
2218 | } |
2219 | ||
9d84c8ea DW |
2220 | static void export_examine_super_imsm(struct supertype *st) |
2221 | { | |
2222 | struct intel_super *super = st->sb; | |
2223 | struct imsm_super *mpb = super->anchor; | |
2224 | struct mdinfo info; | |
2225 | char nbuf[64]; | |
2226 | ||
a5d85af7 | 2227 | getinfo_super_imsm(st, &info, NULL); |
9d84c8ea DW |
2228 | fname_from_uuid(st, &info, nbuf, ':'); |
2229 | printf("MD_METADATA=imsm\n"); | |
2230 | printf("MD_LEVEL=container\n"); | |
2231 | printf("MD_UUID=%s\n", nbuf+5); | |
2232 | printf("MD_DEVICES=%u\n", mpb->num_disks); | |
e48aed3c | 2233 | printf("MD_CREATION_TIME=%llu\n", __le64_to_cpu(mpb->creation_time)); |
9d84c8ea DW |
2234 | } |
2235 | ||
b771faef BK |
2236 | static void detail_super_imsm(struct supertype *st, char *homehost, |
2237 | char *subarray) | |
cdddbdbc | 2238 | { |
3ebe00a1 DW |
2239 | struct mdinfo info; |
2240 | char nbuf[64]; | |
b771faef BK |
2241 | struct intel_super *super = st->sb; |
2242 | int temp_vol = super->current_vol; | |
2243 | ||
2244 | if (subarray) | |
2245 | super->current_vol = strtoul(subarray, NULL, 10); | |
3ebe00a1 | 2246 | |
a5d85af7 | 2247 | getinfo_super_imsm(st, &info, NULL); |
ae2bfd4e | 2248 | fname_from_uuid(st, &info, nbuf, ':'); |
65884368 | 2249 | printf("\n UUID : %s\n", nbuf + 5); |
b771faef BK |
2250 | |
2251 | super->current_vol = temp_vol; | |
cdddbdbc DW |
2252 | } |
2253 | ||
b771faef | 2254 | static void brief_detail_super_imsm(struct supertype *st, char *subarray) |
cdddbdbc | 2255 | { |
ff54de6e N |
2256 | struct mdinfo info; |
2257 | char nbuf[64]; | |
b771faef BK |
2258 | struct intel_super *super = st->sb; |
2259 | int temp_vol = super->current_vol; | |
2260 | ||
2261 | if (subarray) | |
2262 | super->current_vol = strtoul(subarray, NULL, 10); | |
2263 | ||
a5d85af7 | 2264 | getinfo_super_imsm(st, &info, NULL); |
ae2bfd4e | 2265 | fname_from_uuid(st, &info, nbuf, ':'); |
ff54de6e | 2266 | printf(" UUID=%s", nbuf + 5); |
b771faef BK |
2267 | |
2268 | super->current_vol = temp_vol; | |
cdddbdbc | 2269 | } |
d665cc31 | 2270 | |
6da53c0e BK |
2271 | static int imsm_read_serial(int fd, char *devname, __u8 *serial, |
2272 | size_t serial_buf_len); | |
d665cc31 DW |
2273 | static void fd2devname(int fd, char *name); |
2274 | ||
120dc887 | 2275 | static int ahci_enumerate_ports(const char *hba_path, int port_count, int host_base, int verbose) |
d665cc31 | 2276 | { |
120dc887 LM |
2277 | /* dump an unsorted list of devices attached to AHCI Intel storage |
2278 | * controller, as well as non-connected ports | |
d665cc31 DW |
2279 | */ |
2280 | int hba_len = strlen(hba_path) + 1; | |
2281 | struct dirent *ent; | |
2282 | DIR *dir; | |
2283 | char *path = NULL; | |
2284 | int err = 0; | |
2285 | unsigned long port_mask = (1 << port_count) - 1; | |
2286 | ||
f21e18ca | 2287 | if (port_count > (int)sizeof(port_mask) * 8) { |
ba728be7 | 2288 | if (verbose > 0) |
e7b84f9d | 2289 | pr_err("port_count %d out of range\n", port_count); |
d665cc31 DW |
2290 | return 2; |
2291 | } | |
2292 | ||
2293 | /* scroll through /sys/dev/block looking for devices attached to | |
2294 | * this hba | |
2295 | */ | |
2296 | dir = opendir("/sys/dev/block"); | |
1a6dd6b9 PB |
2297 | if (!dir) |
2298 | return 1; | |
2299 | ||
2300 | for (ent = readdir(dir); ent; ent = readdir(dir)) { | |
d665cc31 DW |
2301 | int fd; |
2302 | char model[64]; | |
2303 | char vendor[64]; | |
2304 | char buf[1024]; | |
2305 | int major, minor; | |
fcebeb77 | 2306 | char device[PATH_MAX]; |
d665cc31 DW |
2307 | char *c; |
2308 | int port; | |
2309 | int type; | |
2310 | ||
2311 | if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2) | |
2312 | continue; | |
7c798f87 | 2313 | path = devt_to_devpath(makedev(major, minor), 1, NULL); |
d665cc31 DW |
2314 | if (!path) |
2315 | continue; | |
2316 | if (!path_attached_to_hba(path, hba_path)) { | |
2317 | free(path); | |
2318 | path = NULL; | |
2319 | continue; | |
2320 | } | |
2321 | ||
fcebeb77 MT |
2322 | /* retrieve the scsi device */ |
2323 | if (!devt_to_devpath(makedev(major, minor), 1, device)) { | |
ba728be7 | 2324 | if (verbose > 0) |
fcebeb77 | 2325 | pr_err("failed to get device\n"); |
d665cc31 DW |
2326 | err = 2; |
2327 | break; | |
2328 | } | |
fcebeb77 | 2329 | if (devpath_to_char(device, "type", buf, sizeof(buf), 0)) { |
d665cc31 | 2330 | err = 2; |
d665cc31 DW |
2331 | break; |
2332 | } | |
2333 | type = strtoul(buf, NULL, 10); | |
2334 | ||
2335 | /* if it's not a disk print the vendor and model */ | |
2336 | if (!(type == 0 || type == 7 || type == 14)) { | |
2337 | vendor[0] = '\0'; | |
2338 | model[0] = '\0'; | |
fcebeb77 MT |
2339 | |
2340 | if (devpath_to_char(device, "vendor", buf, | |
2341 | sizeof(buf), 0) == 0) { | |
d665cc31 DW |
2342 | strncpy(vendor, buf, sizeof(vendor)); |
2343 | vendor[sizeof(vendor) - 1] = '\0'; | |
2344 | c = (char *) &vendor[sizeof(vendor) - 1]; | |
2345 | while (isspace(*c) || *c == '\0') | |
2346 | *c-- = '\0'; | |
2347 | ||
2348 | } | |
fcebeb77 MT |
2349 | |
2350 | if (devpath_to_char(device, "model", buf, | |
2351 | sizeof(buf), 0) == 0) { | |
d665cc31 DW |
2352 | strncpy(model, buf, sizeof(model)); |
2353 | model[sizeof(model) - 1] = '\0'; | |
2354 | c = (char *) &model[sizeof(model) - 1]; | |
2355 | while (isspace(*c) || *c == '\0') | |
2356 | *c-- = '\0'; | |
2357 | } | |
2358 | ||
2359 | if (vendor[0] && model[0]) | |
2360 | sprintf(buf, "%.64s %.64s", vendor, model); | |
2361 | else | |
2362 | switch (type) { /* numbers from hald/linux/device.c */ | |
2363 | case 1: sprintf(buf, "tape"); break; | |
2364 | case 2: sprintf(buf, "printer"); break; | |
2365 | case 3: sprintf(buf, "processor"); break; | |
2366 | case 4: | |
2367 | case 5: sprintf(buf, "cdrom"); break; | |
2368 | case 6: sprintf(buf, "scanner"); break; | |
2369 | case 8: sprintf(buf, "media_changer"); break; | |
2370 | case 9: sprintf(buf, "comm"); break; | |
2371 | case 12: sprintf(buf, "raid"); break; | |
2372 | default: sprintf(buf, "unknown"); | |
2373 | } | |
2374 | } else | |
2375 | buf[0] = '\0'; | |
d665cc31 DW |
2376 | |
2377 | /* chop device path to 'host%d' and calculate the port number */ | |
2378 | c = strchr(&path[hba_len], '/'); | |
4e5e717d | 2379 | if (!c) { |
ba728be7 | 2380 | if (verbose > 0) |
e7b84f9d | 2381 | pr_err("%s - invalid path name\n", path + hba_len); |
4e5e717d AW |
2382 | err = 2; |
2383 | break; | |
2384 | } | |
d665cc31 | 2385 | *c = '\0'; |
0858eccf AP |
2386 | if ((sscanf(&path[hba_len], "ata%d", &port) == 1) || |
2387 | ((sscanf(&path[hba_len], "host%d", &port) == 1))) | |
d665cc31 DW |
2388 | port -= host_base; |
2389 | else { | |
ba728be7 | 2390 | if (verbose > 0) { |
d665cc31 | 2391 | *c = '/'; /* repair the full string */ |
e7b84f9d | 2392 | pr_err("failed to determine port number for %s\n", |
d665cc31 DW |
2393 | path); |
2394 | } | |
2395 | err = 2; | |
2396 | break; | |
2397 | } | |
2398 | ||
2399 | /* mark this port as used */ | |
2400 | port_mask &= ~(1 << port); | |
2401 | ||
2402 | /* print out the device information */ | |
2403 | if (buf[0]) { | |
2404 | printf(" Port%d : - non-disk device (%s) -\n", port, buf); | |
2405 | continue; | |
2406 | } | |
2407 | ||
2408 | fd = dev_open(ent->d_name, O_RDONLY); | |
2409 | if (fd < 0) | |
2410 | printf(" Port%d : - disk info unavailable -\n", port); | |
2411 | else { | |
2412 | fd2devname(fd, buf); | |
2413 | printf(" Port%d : %s", port, buf); | |
6da53c0e BK |
2414 | if (imsm_read_serial(fd, NULL, (__u8 *)buf, |
2415 | sizeof(buf)) == 0) | |
2416 | printf(" (%s)\n", buf); | |
d665cc31 | 2417 | else |
664d5325 | 2418 | printf(" ()\n"); |
4dab422a | 2419 | close(fd); |
d665cc31 | 2420 | } |
d665cc31 DW |
2421 | free(path); |
2422 | path = NULL; | |
2423 | } | |
2424 | if (path) | |
2425 | free(path); | |
2426 | if (dir) | |
2427 | closedir(dir); | |
2428 | if (err == 0) { | |
2429 | int i; | |
2430 | ||
2431 | for (i = 0; i < port_count; i++) | |
2432 | if (port_mask & (1 << i)) | |
2433 | printf(" Port%d : - no device attached -\n", i); | |
2434 | } | |
2435 | ||
2436 | return err; | |
2437 | } | |
2438 | ||
6da53c0e | 2439 | static int print_nvme_info(struct sys_dev *hba) |
60f0f54d PB |
2440 | { |
2441 | struct dirent *ent; | |
2442 | DIR *dir; | |
60f0f54d | 2443 | |
6da53c0e | 2444 | dir = opendir("/sys/block/"); |
b9135011 | 2445 | if (!dir) |
b5eece69 | 2446 | return 1; |
b9135011 JS |
2447 | |
2448 | for (ent = readdir(dir); ent; ent = readdir(dir)) { | |
8662f92d MT |
2449 | char ns_path[PATH_MAX]; |
2450 | char cntrl_path[PATH_MAX]; | |
2451 | char buf[PATH_MAX]; | |
2452 | int fd = -1; | |
60f0f54d | 2453 | |
8662f92d MT |
2454 | if (!strstr(ent->d_name, "nvme")) |
2455 | goto skip; | |
d835518b | 2456 | |
8662f92d MT |
2457 | fd = open_dev(ent->d_name); |
2458 | if (fd < 0) | |
2459 | goto skip; | |
d835518b | 2460 | |
8662f92d MT |
2461 | if (!diskfd_to_devpath(fd, 0, ns_path) || |
2462 | !diskfd_to_devpath(fd, 1, cntrl_path)) | |
2463 | goto skip; | |
2464 | ||
2465 | if (!path_attached_to_hba(cntrl_path, hba->path)) | |
2466 | goto skip; | |
2467 | ||
2468 | if (!imsm_is_nvme_namespace_supported(fd, 0)) | |
2469 | goto skip; | |
2470 | ||
2471 | fd2devname(fd, buf); | |
2472 | if (hba->type == SYS_DEV_VMD) | |
2473 | printf(" NVMe under VMD : %s", buf); | |
2474 | else if (hba->type == SYS_DEV_NVME) | |
2475 | printf(" NVMe Device : %s", buf); | |
2476 | ||
2477 | if (!imsm_read_serial(fd, NULL, (__u8 *)buf, | |
2478 | sizeof(buf))) | |
2479 | printf(" (%s)\n", buf); | |
2480 | else | |
2481 | printf("()\n"); | |
2482 | ||
2483 | skip: | |
2484 | if (fd > -1) | |
d835518b | 2485 | close(fd); |
60f0f54d PB |
2486 | } |
2487 | ||
b9135011 | 2488 | closedir(dir); |
b5eece69 | 2489 | return 0; |
60f0f54d PB |
2490 | } |
2491 | ||
120dc887 LM |
2492 | static void print_found_intel_controllers(struct sys_dev *elem) |
2493 | { | |
2494 | for (; elem; elem = elem->next) { | |
e7b84f9d | 2495 | pr_err("found Intel(R) "); |
120dc887 LM |
2496 | if (elem->type == SYS_DEV_SATA) |
2497 | fprintf(stderr, "SATA "); | |
155cbb4c LM |
2498 | else if (elem->type == SYS_DEV_SAS) |
2499 | fprintf(stderr, "SAS "); | |
0858eccf AP |
2500 | else if (elem->type == SYS_DEV_NVME) |
2501 | fprintf(stderr, "NVMe "); | |
60f0f54d PB |
2502 | |
2503 | if (elem->type == SYS_DEV_VMD) | |
2504 | fprintf(stderr, "VMD domain"); | |
2505 | else | |
2506 | fprintf(stderr, "RAID controller"); | |
2507 | ||
120dc887 LM |
2508 | if (elem->pci_id) |
2509 | fprintf(stderr, " at %s", elem->pci_id); | |
2510 | fprintf(stderr, ".\n"); | |
2511 | } | |
2512 | fflush(stderr); | |
2513 | } | |
2514 | ||
120dc887 LM |
2515 | static int ahci_get_port_count(const char *hba_path, int *port_count) |
2516 | { | |
2517 | struct dirent *ent; | |
2518 | DIR *dir; | |
2519 | int host_base = -1; | |
2520 | ||
2521 | *port_count = 0; | |
2522 | if ((dir = opendir(hba_path)) == NULL) | |
2523 | return -1; | |
2524 | ||
2525 | for (ent = readdir(dir); ent; ent = readdir(dir)) { | |
2526 | int host; | |
2527 | ||
0858eccf AP |
2528 | if ((sscanf(ent->d_name, "ata%d", &host) != 1) && |
2529 | ((sscanf(ent->d_name, "host%d", &host) != 1))) | |
120dc887 LM |
2530 | continue; |
2531 | if (*port_count == 0) | |
2532 | host_base = host; | |
2533 | else if (host < host_base) | |
2534 | host_base = host; | |
2535 | ||
2536 | if (host + 1 > *port_count + host_base) | |
2537 | *port_count = host + 1 - host_base; | |
2538 | } | |
2539 | closedir(dir); | |
2540 | return host_base; | |
2541 | } | |
2542 | ||
a891a3c2 LM |
2543 | static void print_imsm_capability(const struct imsm_orom *orom) |
2544 | { | |
0858eccf AP |
2545 | printf(" Platform : Intel(R) "); |
2546 | if (orom->capabilities == 0 && orom->driver_features == 0) | |
2547 | printf("Matrix Storage Manager\n"); | |
ab0c6bb9 AP |
2548 | else if (imsm_orom_is_enterprise(orom) && orom->major_ver >= 6) |
2549 | printf("Virtual RAID on CPU\n"); | |
0858eccf AP |
2550 | else |
2551 | printf("Rapid Storage Technology%s\n", | |
2552 | imsm_orom_is_enterprise(orom) ? " enterprise" : ""); | |
2553 | if (orom->major_ver || orom->minor_ver || orom->hotfix_ver || orom->build) | |
2554 | printf(" Version : %d.%d.%d.%d\n", orom->major_ver, | |
2555 | orom->minor_ver, orom->hotfix_ver, orom->build); | |
a891a3c2 LM |
2556 | printf(" RAID Levels :%s%s%s%s%s\n", |
2557 | imsm_orom_has_raid0(orom) ? " raid0" : "", | |
2558 | imsm_orom_has_raid1(orom) ? " raid1" : "", | |
2559 | imsm_orom_has_raid1e(orom) ? " raid1e" : "", | |
2560 | imsm_orom_has_raid10(orom) ? " raid10" : "", | |
2561 | imsm_orom_has_raid5(orom) ? " raid5" : ""); | |
2562 | printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n", | |
2563 | imsm_orom_has_chunk(orom, 2) ? " 2k" : "", | |
2564 | imsm_orom_has_chunk(orom, 4) ? " 4k" : "", | |
2565 | imsm_orom_has_chunk(orom, 8) ? " 8k" : "", | |
2566 | imsm_orom_has_chunk(orom, 16) ? " 16k" : "", | |
2567 | imsm_orom_has_chunk(orom, 32) ? " 32k" : "", | |
2568 | imsm_orom_has_chunk(orom, 64) ? " 64k" : "", | |
2569 | imsm_orom_has_chunk(orom, 128) ? " 128k" : "", | |
2570 | imsm_orom_has_chunk(orom, 256) ? " 256k" : "", | |
2571 | imsm_orom_has_chunk(orom, 512) ? " 512k" : "", | |
2572 | imsm_orom_has_chunk(orom, 1024*1) ? " 1M" : "", | |
2573 | imsm_orom_has_chunk(orom, 1024*2) ? " 2M" : "", | |
2574 | imsm_orom_has_chunk(orom, 1024*4) ? " 4M" : "", | |
2575 | imsm_orom_has_chunk(orom, 1024*8) ? " 8M" : "", | |
2576 | imsm_orom_has_chunk(orom, 1024*16) ? " 16M" : "", | |
2577 | imsm_orom_has_chunk(orom, 1024*32) ? " 32M" : "", | |
2578 | imsm_orom_has_chunk(orom, 1024*64) ? " 64M" : ""); | |
29cd0821 CA |
2579 | printf(" 2TB volumes :%s supported\n", |
2580 | (orom->attr & IMSM_OROM_ATTR_2TB)?"":" not"); | |
2581 | printf(" 2TB disks :%s supported\n", | |
2582 | (orom->attr & IMSM_OROM_ATTR_2TB_DISK)?"":" not"); | |
0e7f69a8 | 2583 | printf(" Max Disks : %d\n", orom->tds); |
0858eccf AP |
2584 | printf(" Max Volumes : %d per array, %d per %s\n", |
2585 | orom->vpa, orom->vphba, | |
2586 | imsm_orom_is_nvme(orom) ? "platform" : "controller"); | |
a891a3c2 LM |
2587 | return; |
2588 | } | |
2589 | ||
e50cf220 MN |
2590 | static void print_imsm_capability_export(const struct imsm_orom *orom) |
2591 | { | |
2592 | printf("MD_FIRMWARE_TYPE=imsm\n"); | |
0858eccf AP |
2593 | if (orom->major_ver || orom->minor_ver || orom->hotfix_ver || orom->build) |
2594 | printf("IMSM_VERSION=%d.%d.%d.%d\n", orom->major_ver, orom->minor_ver, | |
2595 | orom->hotfix_ver, orom->build); | |
e50cf220 MN |
2596 | printf("IMSM_SUPPORTED_RAID_LEVELS=%s%s%s%s%s\n", |
2597 | imsm_orom_has_raid0(orom) ? "raid0 " : "", | |
2598 | imsm_orom_has_raid1(orom) ? "raid1 " : "", | |
2599 | imsm_orom_has_raid1e(orom) ? "raid1e " : "", | |
2600 | imsm_orom_has_raid5(orom) ? "raid10 " : "", | |
2601 | imsm_orom_has_raid10(orom) ? "raid5 " : ""); | |
2602 | printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n", | |
2603 | imsm_orom_has_chunk(orom, 2) ? "2k " : "", | |
2604 | imsm_orom_has_chunk(orom, 4) ? "4k " : "", | |
2605 | imsm_orom_has_chunk(orom, 8) ? "8k " : "", | |
2606 | imsm_orom_has_chunk(orom, 16) ? "16k " : "", | |
2607 | imsm_orom_has_chunk(orom, 32) ? "32k " : "", | |
2608 | imsm_orom_has_chunk(orom, 64) ? "64k " : "", | |
2609 | imsm_orom_has_chunk(orom, 128) ? "128k " : "", | |
2610 | imsm_orom_has_chunk(orom, 256) ? "256k " : "", | |
2611 | imsm_orom_has_chunk(orom, 512) ? "512k " : "", | |
2612 | imsm_orom_has_chunk(orom, 1024*1) ? "1M " : "", | |
2613 | imsm_orom_has_chunk(orom, 1024*2) ? "2M " : "", | |
2614 | imsm_orom_has_chunk(orom, 1024*4) ? "4M " : "", | |
2615 | imsm_orom_has_chunk(orom, 1024*8) ? "8M " : "", | |
2616 | imsm_orom_has_chunk(orom, 1024*16) ? "16M " : "", | |
2617 | imsm_orom_has_chunk(orom, 1024*32) ? "32M " : "", | |
2618 | imsm_orom_has_chunk(orom, 1024*64) ? "64M " : ""); | |
2619 | printf("IMSM_2TB_VOLUMES=%s\n",(orom->attr & IMSM_OROM_ATTR_2TB) ? "yes" : "no"); | |
2620 | printf("IMSM_2TB_DISKS=%s\n",(orom->attr & IMSM_OROM_ATTR_2TB_DISK) ? "yes" : "no"); | |
2621 | printf("IMSM_MAX_DISKS=%d\n",orom->tds); | |
2622 | printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom->vpa); | |
2623 | printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom->vphba); | |
2624 | } | |
2625 | ||
9eafa1de | 2626 | static int detail_platform_imsm(int verbose, int enumerate_only, char *controller_path) |
d665cc31 DW |
2627 | { |
2628 | /* There are two components to imsm platform support, the ahci SATA | |
2629 | * controller and the option-rom. To find the SATA controller we | |
2630 | * simply look in /sys/bus/pci/drivers/ahci to see if an ahci | |
2631 | * controller with the Intel vendor id is present. This approach | |
2632 | * allows mdadm to leverage the kernel's ahci detection logic, with the | |
2633 | * caveat that if ahci.ko is not loaded mdadm will not be able to | |
2634 | * detect platform raid capabilities. The option-rom resides in a | |
2635 | * platform "Adapter ROM". We scan for its signature to retrieve the | |
2636 | * platform capabilities. If raid support is disabled in the BIOS the | |
2637 | * option-rom capability structure will not be available. | |
2638 | */ | |
d665cc31 | 2639 | struct sys_dev *list, *hba; |
d665cc31 DW |
2640 | int host_base = 0; |
2641 | int port_count = 0; | |
9eafa1de | 2642 | int result=1; |
d665cc31 | 2643 | |
5615172f | 2644 | if (enumerate_only) { |
a891a3c2 | 2645 | if (check_env("IMSM_NO_PLATFORM")) |
5615172f | 2646 | return 0; |
a891a3c2 LM |
2647 | list = find_intel_devices(); |
2648 | if (!list) | |
2649 | return 2; | |
2650 | for (hba = list; hba; hba = hba->next) { | |
6b781d33 AP |
2651 | if (find_imsm_capability(hba)) { |
2652 | result = 0; | |
a891a3c2 LM |
2653 | break; |
2654 | } | |
9eafa1de | 2655 | else |
6b781d33 | 2656 | result = 2; |
a891a3c2 | 2657 | } |
a891a3c2 | 2658 | return result; |
5615172f DW |
2659 | } |
2660 | ||
155cbb4c LM |
2661 | list = find_intel_devices(); |
2662 | if (!list) { | |
ba728be7 | 2663 | if (verbose > 0) |
7a862a02 | 2664 | pr_err("no active Intel(R) RAID controller found.\n"); |
d665cc31 | 2665 | return 2; |
ba728be7 | 2666 | } else if (verbose > 0) |
155cbb4c | 2667 | print_found_intel_controllers(list); |
d665cc31 | 2668 | |
a891a3c2 | 2669 | for (hba = list; hba; hba = hba->next) { |
0858eccf | 2670 | if (controller_path && (compare_paths(hba->path, controller_path) != 0)) |
9eafa1de | 2671 | continue; |
0858eccf | 2672 | if (!find_imsm_capability(hba)) { |
60f0f54d | 2673 | char buf[PATH_MAX]; |
e7b84f9d | 2674 | pr_err("imsm capabilities not found for controller: %s (type %s)\n", |
60f0f54d PB |
2675 | hba->type == SYS_DEV_VMD ? vmd_domain_to_controller(hba, buf) : hba->path, |
2676 | get_sys_dev_type(hba->type)); | |
0858eccf AP |
2677 | continue; |
2678 | } | |
2679 | result = 0; | |
2680 | } | |
2681 | ||
2682 | if (controller_path && result == 1) { | |
2683 | pr_err("no active Intel(R) RAID controller found under %s\n", | |
2684 | controller_path); | |
2685 | return result; | |
2686 | } | |
2687 | ||
5e1d6128 | 2688 | const struct orom_entry *entry; |
0858eccf | 2689 | |
5e1d6128 | 2690 | for (entry = orom_entries; entry; entry = entry->next) { |
60f0f54d | 2691 | if (entry->type == SYS_DEV_VMD) { |
07cb1e57 | 2692 | print_imsm_capability(&entry->orom); |
32716c51 PB |
2693 | printf(" 3rd party NVMe :%s supported\n", |
2694 | imsm_orom_has_tpv_support(&entry->orom)?"":" not"); | |
60f0f54d PB |
2695 | for (hba = list; hba; hba = hba->next) { |
2696 | if (hba->type == SYS_DEV_VMD) { | |
2697 | char buf[PATH_MAX]; | |
60f0f54d PB |
2698 | printf(" I/O Controller : %s (%s)\n", |
2699 | vmd_domain_to_controller(hba, buf), get_sys_dev_type(hba->type)); | |
6da53c0e | 2700 | if (print_nvme_info(hba)) { |
b5eece69 PB |
2701 | if (verbose > 0) |
2702 | pr_err("failed to get devices attached to VMD domain.\n"); | |
2703 | result |= 2; | |
2704 | } | |
60f0f54d PB |
2705 | } |
2706 | } | |
07cb1e57 | 2707 | printf("\n"); |
60f0f54d PB |
2708 | continue; |
2709 | } | |
0858eccf | 2710 | |
60f0f54d PB |
2711 | print_imsm_capability(&entry->orom); |
2712 | if (entry->type == SYS_DEV_NVME) { | |
0858eccf AP |
2713 | for (hba = list; hba; hba = hba->next) { |
2714 | if (hba->type == SYS_DEV_NVME) | |
6da53c0e | 2715 | print_nvme_info(hba); |
0858eccf | 2716 | } |
60f0f54d | 2717 | printf("\n"); |
0858eccf AP |
2718 | continue; |
2719 | } | |
2720 | ||
2721 | struct devid_list *devid; | |
5e1d6128 | 2722 | for (devid = entry->devid_list; devid; devid = devid->next) { |
0858eccf AP |
2723 | hba = device_by_id(devid->devid); |
2724 | if (!hba) | |
2725 | continue; | |
2726 | ||
9eafa1de MN |
2727 | printf(" I/O Controller : %s (%s)\n", |
2728 | hba->path, get_sys_dev_type(hba->type)); | |
2729 | if (hba->type == SYS_DEV_SATA) { | |
2730 | host_base = ahci_get_port_count(hba->path, &port_count); | |
2731 | if (ahci_enumerate_ports(hba->path, port_count, host_base, verbose)) { | |
2732 | if (verbose > 0) | |
7a862a02 | 2733 | pr_err("failed to enumerate ports on SATA controller at %s.\n", hba->pci_id); |
9eafa1de MN |
2734 | result |= 2; |
2735 | } | |
120dc887 LM |
2736 | } |
2737 | } | |
0858eccf | 2738 | printf("\n"); |
d665cc31 | 2739 | } |
155cbb4c | 2740 | |
120dc887 | 2741 | return result; |
d665cc31 | 2742 | } |
e50cf220 | 2743 | |
9eafa1de | 2744 | static int export_detail_platform_imsm(int verbose, char *controller_path) |
e50cf220 | 2745 | { |
e50cf220 MN |
2746 | struct sys_dev *list, *hba; |
2747 | int result=1; | |
2748 | ||
2749 | list = find_intel_devices(); | |
2750 | if (!list) { | |
2751 | if (verbose > 0) | |
2752 | pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n"); | |
2753 | result = 2; | |
e50cf220 MN |
2754 | return result; |
2755 | } | |
2756 | ||
2757 | for (hba = list; hba; hba = hba->next) { | |
9eafa1de MN |
2758 | if (controller_path && (compare_paths(hba->path,controller_path) != 0)) |
2759 | continue; | |
60f0f54d PB |
2760 | if (!find_imsm_capability(hba) && verbose > 0) { |
2761 | char buf[PATH_MAX]; | |
2762 | pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n", | |
2763 | hba->type == SYS_DEV_VMD ? vmd_domain_to_controller(hba, buf) : hba->path); | |
2764 | } | |
0858eccf | 2765 | else |
e50cf220 | 2766 | result = 0; |
e50cf220 MN |
2767 | } |
2768 | ||
5e1d6128 | 2769 | const struct orom_entry *entry; |
0858eccf | 2770 | |
60f0f54d PB |
2771 | for (entry = orom_entries; entry; entry = entry->next) { |
2772 | if (entry->type == SYS_DEV_VMD) { | |
2773 | for (hba = list; hba; hba = hba->next) | |
2774 | print_imsm_capability_export(&entry->orom); | |
2775 | continue; | |
2776 | } | |
5e1d6128 | 2777 | print_imsm_capability_export(&entry->orom); |
60f0f54d | 2778 | } |
0858eccf | 2779 | |
e50cf220 MN |
2780 | return result; |
2781 | } | |
2782 | ||
cdddbdbc DW |
2783 | static int match_home_imsm(struct supertype *st, char *homehost) |
2784 | { | |
5115ca67 DW |
2785 | /* the imsm metadata format does not specify any host |
2786 | * identification information. We return -1 since we can never | |
2787 | * confirm nor deny whether a given array is "meant" for this | |
148acb7b | 2788 | * host. We rely on compare_super and the 'family_num' fields to |
5115ca67 DW |
2789 | * exclude member disks that do not belong, and we rely on |
2790 | * mdadm.conf to specify the arrays that should be assembled. | |
2791 | * Auto-assembly may still pick up "foreign" arrays. | |
2792 | */ | |
cdddbdbc | 2793 | |
9362c1c8 | 2794 | return -1; |
cdddbdbc DW |
2795 | } |
2796 | ||
2797 | static void uuid_from_super_imsm(struct supertype *st, int uuid[4]) | |
2798 | { | |
51006d85 N |
2799 | /* The uuid returned here is used for: |
2800 | * uuid to put into bitmap file (Create, Grow) | |
2801 | * uuid for backup header when saving critical section (Grow) | |
2802 | * comparing uuids when re-adding a device into an array | |
2803 | * In these cases the uuid required is that of the data-array, | |
2804 | * not the device-set. | |
2805 | * uuid to recognise same set when adding a missing device back | |
2806 | * to an array. This is a uuid for the device-set. | |
1011e834 | 2807 | * |
51006d85 N |
2808 | * For each of these we can make do with a truncated |
2809 | * or hashed uuid rather than the original, as long as | |
2810 | * everyone agrees. | |
2811 | * In each case the uuid required is that of the data-array, | |
2812 | * not the device-set. | |
43dad3d6 | 2813 | */ |
51006d85 N |
2814 | /* imsm does not track uuid's so we synthesis one using sha1 on |
2815 | * - The signature (Which is constant for all imsm array, but no matter) | |
148acb7b | 2816 | * - the orig_family_num of the container |
51006d85 N |
2817 | * - the index number of the volume |
2818 | * - the 'serial' number of the volume. | |
2819 | * Hopefully these are all constant. | |
2820 | */ | |
2821 | struct intel_super *super = st->sb; | |
43dad3d6 | 2822 | |
51006d85 N |
2823 | char buf[20]; |
2824 | struct sha1_ctx ctx; | |
2825 | struct imsm_dev *dev = NULL; | |
148acb7b | 2826 | __u32 family_num; |
51006d85 | 2827 | |
148acb7b DW |
2828 | /* some mdadm versions failed to set ->orig_family_num, in which |
2829 | * case fall back to ->family_num. orig_family_num will be | |
2830 | * fixed up with the first metadata update. | |
2831 | */ | |
2832 | family_num = super->anchor->orig_family_num; | |
2833 | if (family_num == 0) | |
2834 | family_num = super->anchor->family_num; | |
51006d85 | 2835 | sha1_init_ctx(&ctx); |
92bd8f8d | 2836 | sha1_process_bytes(super->anchor->sig, MPB_SIG_LEN, &ctx); |
148acb7b | 2837 | sha1_process_bytes(&family_num, sizeof(__u32), &ctx); |
51006d85 N |
2838 | if (super->current_vol >= 0) |
2839 | dev = get_imsm_dev(super, super->current_vol); | |
2840 | if (dev) { | |
2841 | __u32 vol = super->current_vol; | |
2842 | sha1_process_bytes(&vol, sizeof(vol), &ctx); | |
2843 | sha1_process_bytes(dev->volume, MAX_RAID_SERIAL_LEN, &ctx); | |
2844 | } | |
2845 | sha1_finish_ctx(&ctx, buf); | |
2846 | memcpy(uuid, buf, 4*4); | |
cdddbdbc DW |
2847 | } |
2848 | ||
0d481d37 | 2849 | #if 0 |
4f5bc454 DW |
2850 | static void |
2851 | get_imsm_numerical_version(struct imsm_super *mpb, int *m, int *p) | |
cdddbdbc | 2852 | { |
cdddbdbc DW |
2853 | __u8 *v = get_imsm_version(mpb); |
2854 | __u8 *end = mpb->sig + MAX_SIGNATURE_LENGTH; | |
2855 | char major[] = { 0, 0, 0 }; | |
2856 | char minor[] = { 0 ,0, 0 }; | |
2857 | char patch[] = { 0, 0, 0 }; | |
2858 | char *ver_parse[] = { major, minor, patch }; | |
2859 | int i, j; | |
2860 | ||
2861 | i = j = 0; | |
2862 | while (*v != '\0' && v < end) { | |
2863 | if (*v != '.' && j < 2) | |
2864 | ver_parse[i][j++] = *v; | |
2865 | else { | |
2866 | i++; | |
2867 | j = 0; | |
2868 | } | |
2869 | v++; | |
2870 | } | |
2871 | ||
4f5bc454 DW |
2872 | *m = strtol(minor, NULL, 0); |
2873 | *p = strtol(patch, NULL, 0); | |
2874 | } | |
0d481d37 | 2875 | #endif |
4f5bc454 | 2876 | |
1e5c6983 DW |
2877 | static __u32 migr_strip_blocks_resync(struct imsm_dev *dev) |
2878 | { | |
2879 | /* migr_strip_size when repairing or initializing parity */ | |
238c0a71 | 2880 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
1e5c6983 DW |
2881 | __u32 chunk = __le32_to_cpu(map->blocks_per_strip); |
2882 | ||
2883 | switch (get_imsm_raid_level(map)) { | |
2884 | case 5: | |
2885 | case 10: | |
2886 | return chunk; | |
2887 | default: | |
2888 | return 128*1024 >> 9; | |
2889 | } | |
2890 | } | |
2891 | ||
2892 | static __u32 migr_strip_blocks_rebuild(struct imsm_dev *dev) | |
2893 | { | |
2894 | /* migr_strip_size when rebuilding a degraded disk, no idea why | |
2895 | * this is different than migr_strip_size_resync(), but it's good | |
2896 | * to be compatible | |
2897 | */ | |
238c0a71 | 2898 | struct imsm_map *map = get_imsm_map(dev, MAP_1); |
1e5c6983 DW |
2899 | __u32 chunk = __le32_to_cpu(map->blocks_per_strip); |
2900 | ||
2901 | switch (get_imsm_raid_level(map)) { | |
2902 | case 1: | |
2903 | case 10: | |
2904 | if (map->num_members % map->num_domains == 0) | |
2905 | return 128*1024 >> 9; | |
2906 | else | |
2907 | return chunk; | |
2908 | case 5: | |
2909 | return max((__u32) 64*1024 >> 9, chunk); | |
2910 | default: | |
2911 | return 128*1024 >> 9; | |
2912 | } | |
2913 | } | |
2914 | ||
2915 | static __u32 num_stripes_per_unit_resync(struct imsm_dev *dev) | |
2916 | { | |
238c0a71 AK |
2917 | struct imsm_map *lo = get_imsm_map(dev, MAP_0); |
2918 | struct imsm_map *hi = get_imsm_map(dev, MAP_1); | |
1e5c6983 DW |
2919 | __u32 lo_chunk = __le32_to_cpu(lo->blocks_per_strip); |
2920 | __u32 hi_chunk = __le32_to_cpu(hi->blocks_per_strip); | |
2921 | ||
2922 | return max((__u32) 1, hi_chunk / lo_chunk); | |
2923 | } | |
2924 | ||
2925 | static __u32 num_stripes_per_unit_rebuild(struct imsm_dev *dev) | |
2926 | { | |
238c0a71 | 2927 | struct imsm_map *lo = get_imsm_map(dev, MAP_0); |
1e5c6983 DW |
2928 | int level = get_imsm_raid_level(lo); |
2929 | ||
2930 | if (level == 1 || level == 10) { | |
238c0a71 | 2931 | struct imsm_map *hi = get_imsm_map(dev, MAP_1); |
1e5c6983 DW |
2932 | |
2933 | return hi->num_domains; | |
2934 | } else | |
2935 | return num_stripes_per_unit_resync(dev); | |
2936 | } | |
2937 | ||
44490938 MD |
2938 | static unsigned long long calc_component_size(struct imsm_map *map, |
2939 | struct imsm_dev *dev) | |
2940 | { | |
2941 | unsigned long long component_size; | |
2942 | unsigned long long dev_size = imsm_dev_size(dev); | |
a4f7290c | 2943 | long long calc_dev_size = 0; |
44490938 MD |
2944 | unsigned int member_disks = imsm_num_data_members(map); |
2945 | ||
2946 | if (member_disks == 0) | |
2947 | return 0; | |
2948 | ||
2949 | component_size = per_dev_array_size(map); | |
2950 | calc_dev_size = component_size * member_disks; | |
2951 | ||
2952 | /* Component size is rounded to 1MB so difference between size from | |
2953 | * metadata and size calculated from num_data_stripes equals up to | |
2954 | * 2048 blocks per each device. If the difference is higher it means | |
2955 | * that array size was expanded and num_data_stripes was not updated. | |
2956 | */ | |
a4f7290c | 2957 | if (llabs(calc_dev_size - (long long)dev_size) > |
44490938 MD |
2958 | (1 << SECT_PER_MB_SHIFT) * member_disks) { |
2959 | component_size = dev_size / member_disks; | |
2960 | dprintf("Invalid num_data_stripes in metadata; expected=%llu, found=%llu\n", | |
2961 | component_size / map->blocks_per_strip, | |
2962 | num_data_stripes(map)); | |
2963 | } | |
2964 | ||
2965 | return component_size; | |
2966 | } | |
2967 | ||
1e5c6983 DW |
2968 | static __u32 parity_segment_depth(struct imsm_dev *dev) |
2969 | { | |
238c0a71 | 2970 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
1e5c6983 DW |
2971 | __u32 chunk = __le32_to_cpu(map->blocks_per_strip); |
2972 | ||
2973 | switch(get_imsm_raid_level(map)) { | |
2974 | case 1: | |
2975 | case 10: | |
2976 | return chunk * map->num_domains; | |
2977 | case 5: | |
2978 | return chunk * map->num_members; | |
2979 | default: | |
2980 | return chunk; | |
2981 | } | |
2982 | } | |
2983 | ||
2984 | static __u32 map_migr_block(struct imsm_dev *dev, __u32 block) | |
2985 | { | |
238c0a71 | 2986 | struct imsm_map *map = get_imsm_map(dev, MAP_1); |
1e5c6983 DW |
2987 | __u32 chunk = __le32_to_cpu(map->blocks_per_strip); |
2988 | __u32 strip = block / chunk; | |
2989 | ||
2990 | switch (get_imsm_raid_level(map)) { | |
2991 | case 1: | |
2992 | case 10: { | |
2993 | __u32 vol_strip = (strip * map->num_domains) + 1; | |
2994 | __u32 vol_stripe = vol_strip / map->num_members; | |
2995 | ||
2996 | return vol_stripe * chunk + block % chunk; | |
2997 | } case 5: { | |
2998 | __u32 stripe = strip / (map->num_members - 1); | |
2999 | ||
3000 | return stripe * chunk + block % chunk; | |
3001 | } | |
3002 | default: | |
3003 | return 0; | |
3004 | } | |
3005 | } | |
3006 | ||
c47b0ff6 AK |
3007 | static __u64 blocks_per_migr_unit(struct intel_super *super, |
3008 | struct imsm_dev *dev) | |
1e5c6983 DW |
3009 | { |
3010 | /* calculate the conversion factor between per member 'blocks' | |
3011 | * (md/{resync,rebuild}_start) and imsm migration units, return | |
3012 | * 0 for the 'not migrating' and 'unsupported migration' cases | |
3013 | */ | |
3014 | if (!dev->vol.migr_state) | |
3015 | return 0; | |
3016 | ||
3017 | switch (migr_type(dev)) { | |
c47b0ff6 AK |
3018 | case MIGR_GEN_MIGR: { |
3019 | struct migr_record *migr_rec = super->migr_rec; | |
3020 | return __le32_to_cpu(migr_rec->blocks_per_unit); | |
3021 | } | |
1e5c6983 DW |
3022 | case MIGR_VERIFY: |
3023 | case MIGR_REPAIR: | |
3024 | case MIGR_INIT: { | |
238c0a71 | 3025 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
1e5c6983 DW |
3026 | __u32 stripes_per_unit; |
3027 | __u32 blocks_per_unit; | |
3028 | __u32 parity_depth; | |
3029 | __u32 migr_chunk; | |
3030 | __u32 block_map; | |
3031 | __u32 block_rel; | |
3032 | __u32 segment; | |
3033 | __u32 stripe; | |
3034 | __u8 disks; | |
3035 | ||
3036 | /* yes, this is really the translation of migr_units to | |
3037 | * per-member blocks in the 'resync' case | |
3038 | */ | |
3039 | stripes_per_unit = num_stripes_per_unit_resync(dev); | |
3040 | migr_chunk = migr_strip_blocks_resync(dev); | |
9529d343 | 3041 | disks = imsm_num_data_members(map); |
1e5c6983 | 3042 | blocks_per_unit = stripes_per_unit * migr_chunk * disks; |
7b1ab482 | 3043 | stripe = __le16_to_cpu(map->blocks_per_strip) * disks; |
1e5c6983 DW |
3044 | segment = blocks_per_unit / stripe; |
3045 | block_rel = blocks_per_unit - segment * stripe; | |
3046 | parity_depth = parity_segment_depth(dev); | |
3047 | block_map = map_migr_block(dev, block_rel); | |
3048 | return block_map + parity_depth * segment; | |
3049 | } | |
3050 | case MIGR_REBUILD: { | |
3051 | __u32 stripes_per_unit; | |
3052 | __u32 migr_chunk; | |
3053 | ||
3054 | stripes_per_unit = num_stripes_per_unit_rebuild(dev); | |
3055 | migr_chunk = migr_strip_blocks_rebuild(dev); | |
3056 | return migr_chunk * stripes_per_unit; | |
3057 | } | |
1e5c6983 DW |
3058 | case MIGR_STATE_CHANGE: |
3059 | default: | |
3060 | return 0; | |
3061 | } | |
3062 | } | |
3063 | ||
c2c087e6 DW |
3064 | static int imsm_level_to_layout(int level) |
3065 | { | |
3066 | switch (level) { | |
3067 | case 0: | |
3068 | case 1: | |
3069 | return 0; | |
3070 | case 5: | |
3071 | case 6: | |
a380c027 | 3072 | return ALGORITHM_LEFT_ASYMMETRIC; |
c2c087e6 | 3073 | case 10: |
c92a2527 | 3074 | return 0x102; |
c2c087e6 | 3075 | } |
a18a888e | 3076 | return UnSet; |
c2c087e6 DW |
3077 | } |
3078 | ||
8e59f3d8 AK |
3079 | /******************************************************************************* |
3080 | * Function: read_imsm_migr_rec | |
3081 | * Description: Function reads imsm migration record from last sector of disk | |
3082 | * Parameters: | |
3083 | * fd : disk descriptor | |
3084 | * super : metadata info | |
3085 | * Returns: | |
3086 | * 0 : success, | |
3087 | * -1 : fail | |
3088 | ******************************************************************************/ | |
3089 | static int read_imsm_migr_rec(int fd, struct intel_super *super) | |
3090 | { | |
3091 | int ret_val = -1; | |
de44e46f | 3092 | unsigned int sector_size = super->sector_size; |
8e59f3d8 AK |
3093 | unsigned long long dsize; |
3094 | ||
3095 | get_dev_size(fd, NULL, &dsize); | |
de44e46f PB |
3096 | if (lseek64(fd, dsize - (sector_size*MIGR_REC_SECTOR_POSITION), |
3097 | SEEK_SET) < 0) { | |
e7b84f9d N |
3098 | pr_err("Cannot seek to anchor block: %s\n", |
3099 | strerror(errno)); | |
8e59f3d8 AK |
3100 | goto out; |
3101 | } | |
466070ad | 3102 | if ((unsigned int)read(fd, super->migr_rec_buf, |
de44e46f PB |
3103 | MIGR_REC_BUF_SECTORS*sector_size) != |
3104 | MIGR_REC_BUF_SECTORS*sector_size) { | |
e7b84f9d N |
3105 | pr_err("Cannot read migr record block: %s\n", |
3106 | strerror(errno)); | |
8e59f3d8 AK |
3107 | goto out; |
3108 | } | |
3109 | ret_val = 0; | |
de44e46f PB |
3110 | if (sector_size == 4096) |
3111 | convert_from_4k_imsm_migr_rec(super); | |
8e59f3d8 AK |
3112 | |
3113 | out: | |
3114 | return ret_val; | |
3115 | } | |
3116 | ||
3136abe5 AK |
3117 | static struct imsm_dev *imsm_get_device_during_migration( |
3118 | struct intel_super *super) | |
3119 | { | |
3120 | ||
3121 | struct intel_dev *dv; | |
3122 | ||
3123 | for (dv = super->devlist; dv; dv = dv->next) { | |
3124 | if (is_gen_migration(dv->dev)) | |
3125 | return dv->dev; | |
3126 | } | |
3127 | return NULL; | |
3128 | } | |
3129 | ||
8e59f3d8 AK |
3130 | /******************************************************************************* |
3131 | * Function: load_imsm_migr_rec | |
3132 | * Description: Function reads imsm migration record (it is stored at the last | |
3133 | * sector of disk) | |
3134 | * Parameters: | |
3135 | * super : imsm internal array info | |
8e59f3d8 AK |
3136 | * Returns: |
3137 | * 0 : success | |
3138 | * -1 : fail | |
4c965cc9 | 3139 | * -2 : no migration in progress |
8e59f3d8 | 3140 | ******************************************************************************/ |
2f86fda3 | 3141 | static int load_imsm_migr_rec(struct intel_super *super) |
8e59f3d8 | 3142 | { |
594dc1b8 | 3143 | struct dl *dl; |
8e59f3d8 AK |
3144 | char nm[30]; |
3145 | int retval = -1; | |
3146 | int fd = -1; | |
3136abe5 | 3147 | struct imsm_dev *dev; |
594dc1b8 | 3148 | struct imsm_map *map; |
b4ab44d8 | 3149 | int slot = -1; |
2f86fda3 | 3150 | int keep_fd = 1; |
3136abe5 AK |
3151 | |
3152 | /* find map under migration */ | |
3153 | dev = imsm_get_device_during_migration(super); | |
3154 | /* nothing to load,no migration in progress? | |
3155 | */ | |
3156 | if (dev == NULL) | |
4c965cc9 | 3157 | return -2; |
8e59f3d8 | 3158 | |
2f86fda3 MT |
3159 | map = get_imsm_map(dev, MAP_0); |
3160 | if (!map) | |
3161 | return -1; | |
3136abe5 | 3162 | |
2f86fda3 MT |
3163 | for (dl = super->disks; dl; dl = dl->next) { |
3164 | /* skip spare and failed disks | |
3165 | */ | |
3166 | if (dl->index < 0) | |
3167 | continue; | |
3168 | /* read only from one of the first two slots | |
3169 | */ | |
3170 | slot = get_imsm_disk_slot(map, dl->index); | |
3171 | if (slot > 1 || slot < 0) | |
3172 | continue; | |
3173 | ||
3174 | if (dl->fd < 0) { | |
8e59f3d8 AK |
3175 | sprintf(nm, "%d:%d", dl->major, dl->minor); |
3176 | fd = dev_open(nm, O_RDONLY); | |
2f86fda3 MT |
3177 | if (fd >= 0) { |
3178 | keep_fd = 0; | |
8e59f3d8 | 3179 | break; |
2f86fda3 MT |
3180 | } |
3181 | } else { | |
3182 | fd = dl->fd; | |
3183 | break; | |
8e59f3d8 AK |
3184 | } |
3185 | } | |
2f86fda3 | 3186 | |
8e59f3d8 | 3187 | if (fd < 0) |
2f86fda3 | 3188 | return retval; |
8e59f3d8 | 3189 | retval = read_imsm_migr_rec(fd, super); |
2f86fda3 | 3190 | if (!keep_fd) |
8e59f3d8 | 3191 | close(fd); |
2f86fda3 | 3192 | |
8e59f3d8 AK |
3193 | return retval; |
3194 | } | |
3195 | ||
c17608ea AK |
3196 | /******************************************************************************* |
3197 | * function: imsm_create_metadata_checkpoint_update | |
3198 | * Description: It creates update for checkpoint change. | |
3199 | * Parameters: | |
3200 | * super : imsm internal array info | |
3201 | * u : pointer to prepared update | |
3202 | * Returns: | |
3203 | * Uptate length. | |
3204 | * If length is equal to 0, input pointer u contains no update | |
3205 | ******************************************************************************/ | |
3206 | static int imsm_create_metadata_checkpoint_update( | |
3207 | struct intel_super *super, | |
3208 | struct imsm_update_general_migration_checkpoint **u) | |
3209 | { | |
3210 | ||
3211 | int update_memory_size = 0; | |
3212 | ||
1ade5cc1 | 3213 | dprintf("(enter)\n"); |
c17608ea AK |
3214 | |
3215 | if (u == NULL) | |
3216 | return 0; | |
3217 | *u = NULL; | |
3218 | ||
3219 | /* size of all update data without anchor */ | |
3220 | update_memory_size = | |
3221 | sizeof(struct imsm_update_general_migration_checkpoint); | |
3222 | ||
503975b9 | 3223 | *u = xcalloc(1, update_memory_size); |
c17608ea | 3224 | if (*u == NULL) { |
1ade5cc1 | 3225 | dprintf("error: cannot get memory\n"); |
c17608ea AK |
3226 | return 0; |
3227 | } | |
3228 | (*u)->type = update_general_migration_checkpoint; | |
9f421827 | 3229 | (*u)->curr_migr_unit = current_migr_unit(super->migr_rec); |
83b3de77 | 3230 | dprintf("prepared for %llu\n", (unsigned long long)(*u)->curr_migr_unit); |
c17608ea AK |
3231 | |
3232 | return update_memory_size; | |
3233 | } | |
3234 | ||
c17608ea AK |
3235 | static void imsm_update_metadata_locally(struct supertype *st, |
3236 | void *buf, int len); | |
3237 | ||
687629c2 AK |
3238 | /******************************************************************************* |
3239 | * Function: write_imsm_migr_rec | |
3240 | * Description: Function writes imsm migration record | |
3241 | * (at the last sector of disk) | |
3242 | * Parameters: | |
3243 | * super : imsm internal array info | |
3244 | * Returns: | |
3245 | * 0 : success | |
3246 | * -1 : if fail | |
3247 | ******************************************************************************/ | |
3248 | static int write_imsm_migr_rec(struct supertype *st) | |
3249 | { | |
3250 | struct intel_super *super = st->sb; | |
de44e46f | 3251 | unsigned int sector_size = super->sector_size; |
687629c2 | 3252 | unsigned long long dsize; |
687629c2 AK |
3253 | int retval = -1; |
3254 | struct dl *sd; | |
c17608ea AK |
3255 | int len; |
3256 | struct imsm_update_general_migration_checkpoint *u; | |
3136abe5 | 3257 | struct imsm_dev *dev; |
594dc1b8 | 3258 | struct imsm_map *map; |
3136abe5 AK |
3259 | |
3260 | /* find map under migration */ | |
3261 | dev = imsm_get_device_during_migration(super); | |
3262 | /* if no migration, write buffer anyway to clear migr_record | |
3263 | * on disk based on first available device | |
3264 | */ | |
3265 | if (dev == NULL) | |
3266 | dev = get_imsm_dev(super, super->current_vol < 0 ? 0 : | |
3267 | super->current_vol); | |
3268 | ||
44bfe6df | 3269 | map = get_imsm_map(dev, MAP_0); |
687629c2 | 3270 | |
de44e46f PB |
3271 | if (sector_size == 4096) |
3272 | convert_to_4k_imsm_migr_rec(super); | |
687629c2 | 3273 | for (sd = super->disks ; sd ; sd = sd->next) { |
b4ab44d8 | 3274 | int slot = -1; |
3136abe5 AK |
3275 | |
3276 | /* skip failed and spare devices */ | |
3277 | if (sd->index < 0) | |
3278 | continue; | |
687629c2 | 3279 | /* write to 2 first slots only */ |
3136abe5 AK |
3280 | if (map) |
3281 | slot = get_imsm_disk_slot(map, sd->index); | |
089f9d79 | 3282 | if (map == NULL || slot > 1 || slot < 0) |
687629c2 | 3283 | continue; |
3136abe5 | 3284 | |
2f86fda3 MT |
3285 | get_dev_size(sd->fd, NULL, &dsize); |
3286 | if (lseek64(sd->fd, dsize - (MIGR_REC_SECTOR_POSITION * | |
3287 | sector_size), | |
de44e46f | 3288 | SEEK_SET) < 0) { |
e7b84f9d N |
3289 | pr_err("Cannot seek to anchor block: %s\n", |
3290 | strerror(errno)); | |
687629c2 AK |
3291 | goto out; |
3292 | } | |
2f86fda3 | 3293 | if ((unsigned int)write(sd->fd, super->migr_rec_buf, |
de44e46f PB |
3294 | MIGR_REC_BUF_SECTORS*sector_size) != |
3295 | MIGR_REC_BUF_SECTORS*sector_size) { | |
e7b84f9d N |
3296 | pr_err("Cannot write migr record block: %s\n", |
3297 | strerror(errno)); | |
687629c2 AK |
3298 | goto out; |
3299 | } | |
687629c2 | 3300 | } |
de44e46f PB |
3301 | if (sector_size == 4096) |
3302 | convert_from_4k_imsm_migr_rec(super); | |
c17608ea AK |
3303 | /* update checkpoint information in metadata */ |
3304 | len = imsm_create_metadata_checkpoint_update(super, &u); | |
c17608ea AK |
3305 | if (len <= 0) { |
3306 | dprintf("imsm: Cannot prepare update\n"); | |
3307 | goto out; | |
3308 | } | |
3309 | /* update metadata locally */ | |
3310 | imsm_update_metadata_locally(st, u, len); | |
3311 | /* and possibly remotely */ | |
3312 | if (st->update_tail) { | |
3313 | append_metadata_update(st, u, len); | |
3314 | /* during reshape we do all work inside metadata handler | |
3315 | * manage_reshape(), so metadata update has to be triggered | |
3316 | * insida it | |
3317 | */ | |
3318 | flush_metadata_updates(st); | |
3319 | st->update_tail = &st->updates; | |
3320 | } else | |
3321 | free(u); | |
687629c2 AK |
3322 | |
3323 | retval = 0; | |
3324 | out: | |
687629c2 AK |
3325 | return retval; |
3326 | } | |
3327 | ||
e2962bfc AK |
3328 | /* spare/missing disks activations are not allowe when |
3329 | * array/container performs reshape operation, because | |
3330 | * all arrays in container works on the same disks set | |
3331 | */ | |
3332 | int imsm_reshape_blocks_arrays_changes(struct intel_super *super) | |
3333 | { | |
3334 | int rv = 0; | |
3335 | struct intel_dev *i_dev; | |
3336 | struct imsm_dev *dev; | |
3337 | ||
3338 | /* check whole container | |
3339 | */ | |
3340 | for (i_dev = super->devlist; i_dev; i_dev = i_dev->next) { | |
3341 | dev = i_dev->dev; | |
3ad25638 | 3342 | if (is_gen_migration(dev)) { |
e2962bfc AK |
3343 | /* No repair during any migration in container |
3344 | */ | |
3345 | rv = 1; | |
3346 | break; | |
3347 | } | |
3348 | } | |
3349 | return rv; | |
3350 | } | |
3e684231 | 3351 | static unsigned long long imsm_component_size_alignment_check(int level, |
c41e00b2 | 3352 | int chunk_size, |
f36a9ecd | 3353 | unsigned int sector_size, |
c41e00b2 AK |
3354 | unsigned long long component_size) |
3355 | { | |
3e684231 | 3356 | unsigned int component_size_alignment; |
c41e00b2 | 3357 | |
3e684231 | 3358 | /* check component size alignment |
c41e00b2 | 3359 | */ |
3e684231 | 3360 | component_size_alignment = component_size % (chunk_size/sector_size); |
c41e00b2 | 3361 | |
3e684231 | 3362 | dprintf("(Level: %i, chunk_size = %i, component_size = %llu), component_size_alignment = %u\n", |
c41e00b2 | 3363 | level, chunk_size, component_size, |
3e684231 | 3364 | component_size_alignment); |
c41e00b2 | 3365 | |
3e684231 MZ |
3366 | if (component_size_alignment && (level != 1) && (level != UnSet)) { |
3367 | dprintf("imsm: reported component size aligned from %llu ", | |
c41e00b2 | 3368 | component_size); |
3e684231 | 3369 | component_size -= component_size_alignment; |
1ade5cc1 | 3370 | dprintf_cont("to %llu (%i).\n", |
3e684231 | 3371 | component_size, component_size_alignment); |
c41e00b2 AK |
3372 | } |
3373 | ||
3374 | return component_size; | |
3375 | } | |
e2962bfc | 3376 | |
fbc42556 JR |
3377 | /******************************************************************************* |
3378 | * Function: get_bitmap_header_sector | |
3379 | * Description: Returns the sector where the bitmap header is placed. | |
3380 | * Parameters: | |
3381 | * st : supertype information | |
3382 | * dev_idx : index of the device with bitmap | |
3383 | * | |
3384 | * Returns: | |
3385 | * The sector where the bitmap header is placed | |
3386 | ******************************************************************************/ | |
3387 | static unsigned long long get_bitmap_header_sector(struct intel_super *super, | |
3388 | int dev_idx) | |
3389 | { | |
3390 | struct imsm_dev *dev = get_imsm_dev(super, dev_idx); | |
3391 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
3392 | ||
3393 | if (!super->sector_size) { | |
3394 | dprintf("sector size is not set\n"); | |
3395 | return 0; | |
3396 | } | |
3397 | ||
3398 | return pba_of_lba0(map) + calc_component_size(map, dev) + | |
3399 | (IMSM_BITMAP_HEADER_OFFSET / super->sector_size); | |
3400 | } | |
3401 | ||
3402 | /******************************************************************************* | |
3403 | * Function: get_bitmap_sector | |
3404 | * Description: Returns the sector where the bitmap is placed. | |
3405 | * Parameters: | |
3406 | * st : supertype information | |
3407 | * dev_idx : index of the device with bitmap | |
3408 | * | |
3409 | * Returns: | |
3410 | * The sector where the bitmap is placed | |
3411 | ******************************************************************************/ | |
3412 | static unsigned long long get_bitmap_sector(struct intel_super *super, | |
3413 | int dev_idx) | |
3414 | { | |
3415 | if (!super->sector_size) { | |
3416 | dprintf("sector size is not set\n"); | |
3417 | return 0; | |
3418 | } | |
3419 | ||
3420 | return get_bitmap_header_sector(super, dev_idx) + | |
3421 | (IMSM_BITMAP_HEADER_SIZE / super->sector_size); | |
3422 | } | |
3423 | ||
2432ce9b AP |
3424 | static unsigned long long get_ppl_sector(struct intel_super *super, int dev_idx) |
3425 | { | |
3426 | struct imsm_dev *dev = get_imsm_dev(super, dev_idx); | |
3427 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
3428 | ||
3429 | return pba_of_lba0(map) + | |
3430 | (num_data_stripes(map) * map->blocks_per_strip); | |
3431 | } | |
3432 | ||
a5d85af7 | 3433 | static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info, char *dmap) |
bf5a934a DW |
3434 | { |
3435 | struct intel_super *super = st->sb; | |
c47b0ff6 | 3436 | struct migr_record *migr_rec = super->migr_rec; |
949c47a0 | 3437 | struct imsm_dev *dev = get_imsm_dev(super, super->current_vol); |
238c0a71 AK |
3438 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
3439 | struct imsm_map *prev_map = get_imsm_map(dev, MAP_1); | |
b335e593 | 3440 | struct imsm_map *map_to_analyse = map; |
efb30e7f | 3441 | struct dl *dl; |
a5d85af7 | 3442 | int map_disks = info->array.raid_disks; |
bf5a934a | 3443 | |
95eeceeb | 3444 | memset(info, 0, sizeof(*info)); |
b335e593 AK |
3445 | if (prev_map) |
3446 | map_to_analyse = prev_map; | |
3447 | ||
ca0748fa | 3448 | dl = super->current_disk; |
9894ec0d | 3449 | |
bf5a934a | 3450 | info->container_member = super->current_vol; |
cd0430a1 | 3451 | info->array.raid_disks = map->num_members; |
b335e593 | 3452 | info->array.level = get_imsm_raid_level(map_to_analyse); |
bf5a934a DW |
3453 | info->array.layout = imsm_level_to_layout(info->array.level); |
3454 | info->array.md_minor = -1; | |
3455 | info->array.ctime = 0; | |
3456 | info->array.utime = 0; | |
b335e593 AK |
3457 | info->array.chunk_size = |
3458 | __le16_to_cpu(map_to_analyse->blocks_per_strip) << 9; | |
2432ce9b | 3459 | info->array.state = !(dev->vol.dirty & RAIDVOL_DIRTY); |
fcc2c9da | 3460 | info->custom_array_size = imsm_dev_size(dev); |
3ad25638 AK |
3461 | info->recovery_blocked = imsm_reshape_blocks_arrays_changes(st->sb); |
3462 | ||
3f510843 | 3463 | if (is_gen_migration(dev)) { |
3f83228a | 3464 | info->reshape_active = 1; |
b335e593 AK |
3465 | info->new_level = get_imsm_raid_level(map); |
3466 | info->new_layout = imsm_level_to_layout(info->new_level); | |
3467 | info->new_chunk = __le16_to_cpu(map->blocks_per_strip) << 9; | |
3f83228a | 3468 | info->delta_disks = map->num_members - prev_map->num_members; |
493f5dd6 N |
3469 | if (info->delta_disks) { |
3470 | /* this needs to be applied to every array | |
3471 | * in the container. | |
3472 | */ | |
81219e70 | 3473 | info->reshape_active = CONTAINER_RESHAPE; |
493f5dd6 | 3474 | } |
3f83228a N |
3475 | /* We shape information that we give to md might have to be |
3476 | * modify to cope with md's requirement for reshaping arrays. | |
3477 | * For example, when reshaping a RAID0, md requires it to be | |
3478 | * presented as a degraded RAID4. | |
3479 | * Also if a RAID0 is migrating to a RAID5 we need to specify | |
3480 | * the array as already being RAID5, but the 'before' layout | |
3481 | * is a RAID4-like layout. | |
3482 | */ | |
3483 | switch (info->array.level) { | |
3484 | case 0: | |
3485 | switch(info->new_level) { | |
3486 | case 0: | |
3487 | /* conversion is happening as RAID4 */ | |
3488 | info->array.level = 4; | |
3489 | info->array.raid_disks += 1; | |
3490 | break; | |
3491 | case 5: | |
3492 | /* conversion is happening as RAID5 */ | |
3493 | info->array.level = 5; | |
3494 | info->array.layout = ALGORITHM_PARITY_N; | |
3f83228a N |
3495 | info->delta_disks -= 1; |
3496 | break; | |
3497 | default: | |
3498 | /* FIXME error message */ | |
3499 | info->array.level = UnSet; | |
3500 | break; | |
3501 | } | |
3502 | break; | |
3503 | } | |
b335e593 AK |
3504 | } else { |
3505 | info->new_level = UnSet; | |
3506 | info->new_layout = UnSet; | |
3507 | info->new_chunk = info->array.chunk_size; | |
3f83228a | 3508 | info->delta_disks = 0; |
b335e593 | 3509 | } |
ca0748fa | 3510 | |
efb30e7f DW |
3511 | if (dl) { |
3512 | info->disk.major = dl->major; | |
3513 | info->disk.minor = dl->minor; | |
ca0748fa | 3514 | info->disk.number = dl->index; |
656b6b5a N |
3515 | info->disk.raid_disk = get_imsm_disk_slot(map_to_analyse, |
3516 | dl->index); | |
efb30e7f | 3517 | } |
bf5a934a | 3518 | |
5551b113 | 3519 | info->data_offset = pba_of_lba0(map_to_analyse); |
44490938 | 3520 | info->component_size = calc_component_size(map, dev); |
3e684231 | 3521 | info->component_size = imsm_component_size_alignment_check( |
c41e00b2 AK |
3522 | info->array.level, |
3523 | info->array.chunk_size, | |
f36a9ecd | 3524 | super->sector_size, |
c41e00b2 | 3525 | info->component_size); |
5e46202e | 3526 | info->bb.supported = 1; |
139dae11 | 3527 | |
301406c9 | 3528 | memset(info->uuid, 0, sizeof(info->uuid)); |
921d9e16 | 3529 | info->recovery_start = MaxSector; |
bf5a934a | 3530 | |
c2462068 PB |
3531 | if (info->array.level == 5 && |
3532 | (dev->rwh_policy == RWH_DISTRIBUTED || | |
3533 | dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)) { | |
2432ce9b AP |
3534 | info->consistency_policy = CONSISTENCY_POLICY_PPL; |
3535 | info->ppl_sector = get_ppl_sector(super, super->current_vol); | |
c2462068 PB |
3536 | if (dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED) |
3537 | info->ppl_size = MULTIPLE_PPL_AREA_SIZE_IMSM >> 9; | |
3538 | else | |
3539 | info->ppl_size = (PPL_HEADER_SIZE + PPL_ENTRY_SPACE) | |
3540 | >> 9; | |
2432ce9b AP |
3541 | } else if (info->array.level <= 0) { |
3542 | info->consistency_policy = CONSISTENCY_POLICY_NONE; | |
3543 | } else { | |
fbc42556 JR |
3544 | if (dev->rwh_policy == RWH_BITMAP) { |
3545 | info->bitmap_offset = get_bitmap_sector(super, super->current_vol); | |
3546 | info->consistency_policy = CONSISTENCY_POLICY_BITMAP; | |
3547 | } else { | |
3548 | info->consistency_policy = CONSISTENCY_POLICY_RESYNC; | |
3549 | } | |
2432ce9b AP |
3550 | } |
3551 | ||
d2e6d5d6 | 3552 | info->reshape_progress = 0; |
b6796ce1 | 3553 | info->resync_start = MaxSector; |
b9172665 | 3554 | if ((map_to_analyse->map_state == IMSM_T_STATE_UNINITIALIZED || |
2432ce9b | 3555 | !(info->array.state & 1)) && |
b9172665 | 3556 | imsm_reshape_blocks_arrays_changes(super) == 0) { |
301406c9 | 3557 | info->resync_start = 0; |
b6796ce1 AK |
3558 | } |
3559 | if (dev->vol.migr_state) { | |
1e5c6983 DW |
3560 | switch (migr_type(dev)) { |
3561 | case MIGR_REPAIR: | |
3562 | case MIGR_INIT: { | |
c47b0ff6 AK |
3563 | __u64 blocks_per_unit = blocks_per_migr_unit(super, |
3564 | dev); | |
4036e7ee | 3565 | __u64 units = vol_curr_migr_unit(dev); |
1e5c6983 DW |
3566 | |
3567 | info->resync_start = blocks_per_unit * units; | |
3568 | break; | |
3569 | } | |
d2e6d5d6 | 3570 | case MIGR_GEN_MIGR: { |
c47b0ff6 AK |
3571 | __u64 blocks_per_unit = blocks_per_migr_unit(super, |
3572 | dev); | |
9f421827 | 3573 | __u64 units = current_migr_unit(migr_rec); |
04fa9523 | 3574 | int used_disks; |
d2e6d5d6 | 3575 | |
befb629b AK |
3576 | if (__le32_to_cpu(migr_rec->ascending_migr) && |
3577 | (units < | |
9f421827 | 3578 | (get_num_migr_units(migr_rec)-1)) && |
befb629b AK |
3579 | (super->migr_rec->rec_status == |
3580 | __cpu_to_le32(UNIT_SRC_IN_CP_AREA))) | |
3581 | units++; | |
3582 | ||
d2e6d5d6 | 3583 | info->reshape_progress = blocks_per_unit * units; |
6289d1e0 | 3584 | |
7a862a02 | 3585 | dprintf("IMSM: General Migration checkpoint : %llu (%llu) -> read reshape progress : %llu\n", |
19986c72 MB |
3586 | (unsigned long long)units, |
3587 | (unsigned long long)blocks_per_unit, | |
3588 | info->reshape_progress); | |
75156c46 | 3589 | |
9529d343 | 3590 | used_disks = imsm_num_data_members(prev_map); |
75156c46 | 3591 | if (used_disks > 0) { |
895ffd99 | 3592 | info->custom_array_size = per_dev_array_size(map) * |
75156c46 | 3593 | used_disks; |
75156c46 | 3594 | } |
d2e6d5d6 | 3595 | } |
1e5c6983 DW |
3596 | case MIGR_VERIFY: |
3597 | /* we could emulate the checkpointing of | |
3598 | * 'sync_action=check' migrations, but for now | |
3599 | * we just immediately complete them | |
3600 | */ | |
3601 | case MIGR_REBUILD: | |
3602 | /* this is handled by container_content_imsm() */ | |
1e5c6983 DW |
3603 | case MIGR_STATE_CHANGE: |
3604 | /* FIXME handle other migrations */ | |
3605 | default: | |
3606 | /* we are not dirty, so... */ | |
3607 | info->resync_start = MaxSector; | |
3608 | } | |
b6796ce1 | 3609 | } |
301406c9 DW |
3610 | |
3611 | strncpy(info->name, (char *) dev->volume, MAX_RAID_SERIAL_LEN); | |
3612 | info->name[MAX_RAID_SERIAL_LEN] = 0; | |
bf5a934a | 3613 | |
f35f2525 N |
3614 | info->array.major_version = -1; |
3615 | info->array.minor_version = -2; | |
4dd2df09 | 3616 | sprintf(info->text_version, "/%s/%d", st->container_devnm, info->container_member); |
a67dd8cc | 3617 | info->safe_mode_delay = 4000; /* 4 secs like the Matrix driver */ |
51006d85 | 3618 | uuid_from_super_imsm(st, info->uuid); |
a5d85af7 N |
3619 | |
3620 | if (dmap) { | |
3621 | int i, j; | |
3622 | for (i=0; i<map_disks; i++) { | |
3623 | dmap[i] = 0; | |
3624 | if (i < info->array.raid_disks) { | |
3625 | struct imsm_disk *dsk; | |
238c0a71 | 3626 | j = get_imsm_disk_idx(dev, i, MAP_X); |
a5d85af7 N |
3627 | dsk = get_imsm_disk(super, j); |
3628 | if (dsk && (dsk->status & CONFIGURED_DISK)) | |
3629 | dmap[i] = 1; | |
3630 | } | |
3631 | } | |
3632 | } | |
81ac8b4d | 3633 | } |
bf5a934a | 3634 | |
3b451610 AK |
3635 | static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev, |
3636 | int failed, int look_in_map); | |
3637 | ||
3638 | static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev, | |
3639 | int look_in_map); | |
3640 | ||
3641 | static void manage_second_map(struct intel_super *super, struct imsm_dev *dev) | |
3642 | { | |
3643 | if (is_gen_migration(dev)) { | |
3644 | int failed; | |
3645 | __u8 map_state; | |
3646 | struct imsm_map *map2 = get_imsm_map(dev, MAP_1); | |
3647 | ||
3648 | failed = imsm_count_failed(super, dev, MAP_1); | |
238c0a71 | 3649 | map_state = imsm_check_degraded(super, dev, failed, MAP_1); |
3b451610 AK |
3650 | if (map2->map_state != map_state) { |
3651 | map2->map_state = map_state; | |
3652 | super->updates_pending++; | |
3653 | } | |
3654 | } | |
3655 | } | |
97b4d0e9 DW |
3656 | |
3657 | static struct imsm_disk *get_imsm_missing(struct intel_super *super, __u8 index) | |
3658 | { | |
3659 | struct dl *d; | |
3660 | ||
3661 | for (d = super->missing; d; d = d->next) | |
3662 | if (d->index == index) | |
3663 | return &d->disk; | |
3664 | return NULL; | |
3665 | } | |
3666 | ||
a5d85af7 | 3667 | static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map) |
4f5bc454 DW |
3668 | { |
3669 | struct intel_super *super = st->sb; | |
4f5bc454 | 3670 | struct imsm_disk *disk; |
a5d85af7 | 3671 | int map_disks = info->array.raid_disks; |
ab3cb6b3 N |
3672 | int max_enough = -1; |
3673 | int i; | |
3674 | struct imsm_super *mpb; | |
4f5bc454 | 3675 | |
bf5a934a | 3676 | if (super->current_vol >= 0) { |
a5d85af7 | 3677 | getinfo_super_imsm_volume(st, info, map); |
bf5a934a DW |
3678 | return; |
3679 | } | |
95eeceeb | 3680 | memset(info, 0, sizeof(*info)); |
d23fe947 DW |
3681 | |
3682 | /* Set raid_disks to zero so that Assemble will always pull in valid | |
3683 | * spares | |
3684 | */ | |
3685 | info->array.raid_disks = 0; | |
cdddbdbc DW |
3686 | info->array.level = LEVEL_CONTAINER; |
3687 | info->array.layout = 0; | |
3688 | info->array.md_minor = -1; | |
1011e834 | 3689 | info->array.ctime = 0; /* N/A for imsm */ |
cdddbdbc DW |
3690 | info->array.utime = 0; |
3691 | info->array.chunk_size = 0; | |
3692 | ||
3693 | info->disk.major = 0; | |
3694 | info->disk.minor = 0; | |
cdddbdbc | 3695 | info->disk.raid_disk = -1; |
c2c087e6 | 3696 | info->reshape_active = 0; |
f35f2525 N |
3697 | info->array.major_version = -1; |
3698 | info->array.minor_version = -2; | |
c2c087e6 | 3699 | strcpy(info->text_version, "imsm"); |
a67dd8cc | 3700 | info->safe_mode_delay = 0; |
c2c087e6 DW |
3701 | info->disk.number = -1; |
3702 | info->disk.state = 0; | |
c5afc314 | 3703 | info->name[0] = 0; |
921d9e16 | 3704 | info->recovery_start = MaxSector; |
3ad25638 | 3705 | info->recovery_blocked = imsm_reshape_blocks_arrays_changes(st->sb); |
5e46202e | 3706 | info->bb.supported = 1; |
c2c087e6 | 3707 | |
97b4d0e9 | 3708 | /* do we have the all the insync disks that we expect? */ |
ab3cb6b3 | 3709 | mpb = super->anchor; |
b7d81a38 | 3710 | info->events = __le32_to_cpu(mpb->generation_num); |
97b4d0e9 | 3711 | |
ab3cb6b3 N |
3712 | for (i = 0; i < mpb->num_raid_devs; i++) { |
3713 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
3714 | int failed, enough, j, missing = 0; | |
3715 | struct imsm_map *map; | |
3716 | __u8 state; | |
97b4d0e9 | 3717 | |
3b451610 AK |
3718 | failed = imsm_count_failed(super, dev, MAP_0); |
3719 | state = imsm_check_degraded(super, dev, failed, MAP_0); | |
238c0a71 | 3720 | map = get_imsm_map(dev, MAP_0); |
ab3cb6b3 N |
3721 | |
3722 | /* any newly missing disks? | |
3723 | * (catches single-degraded vs double-degraded) | |
3724 | */ | |
3725 | for (j = 0; j < map->num_members; j++) { | |
238c0a71 | 3726 | __u32 ord = get_imsm_ord_tbl_ent(dev, j, MAP_0); |
ab3cb6b3 N |
3727 | __u32 idx = ord_to_idx(ord); |
3728 | ||
20dc76d1 MT |
3729 | if (super->disks && super->disks->index == (int)idx) |
3730 | info->disk.raid_disk = j; | |
3731 | ||
ab3cb6b3 N |
3732 | if (!(ord & IMSM_ORD_REBUILD) && |
3733 | get_imsm_missing(super, idx)) { | |
3734 | missing = 1; | |
3735 | break; | |
3736 | } | |
97b4d0e9 | 3737 | } |
ab3cb6b3 N |
3738 | |
3739 | if (state == IMSM_T_STATE_FAILED) | |
3740 | enough = -1; | |
3741 | else if (state == IMSM_T_STATE_DEGRADED && | |
3742 | (state != map->map_state || missing)) | |
3743 | enough = 0; | |
3744 | else /* we're normal, or already degraded */ | |
3745 | enough = 1; | |
d2bde6d3 AK |
3746 | if (is_gen_migration(dev) && missing) { |
3747 | /* during general migration we need all disks | |
3748 | * that process is running on. | |
3749 | * No new missing disk is allowed. | |
3750 | */ | |
3751 | max_enough = -1; | |
3752 | enough = -1; | |
3753 | /* no more checks necessary | |
3754 | */ | |
3755 | break; | |
3756 | } | |
ab3cb6b3 N |
3757 | /* in the missing/failed disk case check to see |
3758 | * if at least one array is runnable | |
3759 | */ | |
3760 | max_enough = max(max_enough, enough); | |
3761 | } | |
1ade5cc1 | 3762 | dprintf("enough: %d\n", max_enough); |
ab3cb6b3 | 3763 | info->container_enough = max_enough; |
97b4d0e9 | 3764 | |
4a04ec6c | 3765 | if (super->disks) { |
14e8215b DW |
3766 | __u32 reserved = imsm_reserved_sectors(super, super->disks); |
3767 | ||
b9f594fe | 3768 | disk = &super->disks->disk; |
5551b113 | 3769 | info->data_offset = total_blocks(&super->disks->disk) - reserved; |
14e8215b | 3770 | info->component_size = reserved; |
25ed7e59 | 3771 | info->disk.state = is_configured(disk) ? (1 << MD_DISK_ACTIVE) : 0; |
df474657 DW |
3772 | /* we don't change info->disk.raid_disk here because |
3773 | * this state will be finalized in mdmon after we have | |
3774 | * found the 'most fresh' version of the metadata | |
3775 | */ | |
25ed7e59 | 3776 | info->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0; |
2432ce9b AP |
3777 | info->disk.state |= (is_spare(disk) || is_journal(disk)) ? |
3778 | 0 : (1 << MD_DISK_SYNC); | |
cdddbdbc | 3779 | } |
a575e2a7 DW |
3780 | |
3781 | /* only call uuid_from_super_imsm when this disk is part of a populated container, | |
3782 | * ->compare_super may have updated the 'num_raid_devs' field for spares | |
3783 | */ | |
3784 | if (info->disk.state & (1 << MD_DISK_SYNC) || super->anchor->num_raid_devs) | |
36ba7d48 | 3785 | uuid_from_super_imsm(st, info->uuid); |
22e263f6 AC |
3786 | else |
3787 | memcpy(info->uuid, uuid_zero, sizeof(uuid_zero)); | |
a5d85af7 N |
3788 | |
3789 | /* I don't know how to compute 'map' on imsm, so use safe default */ | |
3790 | if (map) { | |
3791 | int i; | |
3792 | for (i = 0; i < map_disks; i++) | |
3793 | map[i] = 1; | |
3794 | } | |
3795 | ||
cdddbdbc DW |
3796 | } |
3797 | ||
5c4cd5da AC |
3798 | /* allocates memory and fills disk in mdinfo structure |
3799 | * for each disk in array */ | |
3800 | struct mdinfo *getinfo_super_disks_imsm(struct supertype *st) | |
3801 | { | |
594dc1b8 | 3802 | struct mdinfo *mddev; |
5c4cd5da AC |
3803 | struct intel_super *super = st->sb; |
3804 | struct imsm_disk *disk; | |
3805 | int count = 0; | |
3806 | struct dl *dl; | |
3807 | if (!super || !super->disks) | |
3808 | return NULL; | |
3809 | dl = super->disks; | |
503975b9 | 3810 | mddev = xcalloc(1, sizeof(*mddev)); |
5c4cd5da AC |
3811 | while (dl) { |
3812 | struct mdinfo *tmp; | |
3813 | disk = &dl->disk; | |
503975b9 | 3814 | tmp = xcalloc(1, sizeof(*tmp)); |
5c4cd5da AC |
3815 | if (mddev->devs) |
3816 | tmp->next = mddev->devs; | |
3817 | mddev->devs = tmp; | |
3818 | tmp->disk.number = count++; | |
3819 | tmp->disk.major = dl->major; | |
3820 | tmp->disk.minor = dl->minor; | |
3821 | tmp->disk.state = is_configured(disk) ? | |
3822 | (1 << MD_DISK_ACTIVE) : 0; | |
3823 | tmp->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0; | |
3824 | tmp->disk.state |= is_spare(disk) ? 0 : (1 << MD_DISK_SYNC); | |
3825 | tmp->disk.raid_disk = -1; | |
3826 | dl = dl->next; | |
3827 | } | |
3828 | return mddev; | |
3829 | } | |
3830 | ||
cdddbdbc DW |
3831 | static int update_super_imsm(struct supertype *st, struct mdinfo *info, |
3832 | char *update, char *devname, int verbose, | |
3833 | int uuid_set, char *homehost) | |
3834 | { | |
f352c545 DW |
3835 | /* For 'assemble' and 'force' we need to return non-zero if any |
3836 | * change was made. For others, the return value is ignored. | |
3837 | * Update options are: | |
3838 | * force-one : This device looks a bit old but needs to be included, | |
3839 | * update age info appropriately. | |
3840 | * assemble: clear any 'faulty' flag to allow this device to | |
3841 | * be assembled. | |
3842 | * force-array: Array is degraded but being forced, mark it clean | |
3843 | * if that will be needed to assemble it. | |
3844 | * | |
3845 | * newdev: not used ???? | |
3846 | * grow: Array has gained a new device - this is currently for | |
3847 | * linear only | |
3848 | * resync: mark as dirty so a resync will happen. | |
3849 | * name: update the name - preserving the homehost | |
6e46bf34 | 3850 | * uuid: Change the uuid of the array to match watch is given |
f352c545 DW |
3851 | * |
3852 | * Following are not relevant for this imsm: | |
3853 | * sparc2.2 : update from old dodgey metadata | |
3854 | * super-minor: change the preferred_minor number | |
3855 | * summaries: update redundant counters. | |
f352c545 DW |
3856 | * homehost: update the recorded homehost |
3857 | * _reshape_progress: record new reshape_progress position. | |
3858 | */ | |
6e46bf34 DW |
3859 | int rv = 1; |
3860 | struct intel_super *super = st->sb; | |
3861 | struct imsm_super *mpb; | |
f352c545 | 3862 | |
6e46bf34 DW |
3863 | /* we can only update container info */ |
3864 | if (!super || super->current_vol >= 0 || !super->anchor) | |
3865 | return 1; | |
3866 | ||
3867 | mpb = super->anchor; | |
3868 | ||
81a5b4f5 N |
3869 | if (strcmp(update, "uuid") == 0) { |
3870 | /* We take this to mean that the family_num should be updated. | |
3871 | * However that is much smaller than the uuid so we cannot really | |
3872 | * allow an explicit uuid to be given. And it is hard to reliably | |
3873 | * know if one was. | |
3874 | * So if !uuid_set we know the current uuid is random and just used | |
3875 | * the first 'int' and copy it to the other 3 positions. | |
3876 | * Otherwise we require the 4 'int's to be the same as would be the | |
3877 | * case if we are using a random uuid. So an explicit uuid will be | |
3878 | * accepted as long as all for ints are the same... which shouldn't hurt | |
6e46bf34 | 3879 | */ |
81a5b4f5 N |
3880 | if (!uuid_set) { |
3881 | info->uuid[1] = info->uuid[2] = info->uuid[3] = info->uuid[0]; | |
6e46bf34 | 3882 | rv = 0; |
81a5b4f5 N |
3883 | } else { |
3884 | if (info->uuid[0] != info->uuid[1] || | |
3885 | info->uuid[1] != info->uuid[2] || | |
3886 | info->uuid[2] != info->uuid[3]) | |
3887 | rv = -1; | |
3888 | else | |
3889 | rv = 0; | |
6e46bf34 | 3890 | } |
81a5b4f5 N |
3891 | if (rv == 0) |
3892 | mpb->orig_family_num = info->uuid[0]; | |
6e46bf34 DW |
3893 | } else if (strcmp(update, "assemble") == 0) |
3894 | rv = 0; | |
3895 | else | |
1e2b2765 | 3896 | rv = -1; |
f352c545 | 3897 | |
6e46bf34 DW |
3898 | /* successful update? recompute checksum */ |
3899 | if (rv == 0) | |
3900 | mpb->check_sum = __le32_to_cpu(__gen_imsm_checksum(mpb)); | |
f352c545 DW |
3901 | |
3902 | return rv; | |
cdddbdbc DW |
3903 | } |
3904 | ||
c2c087e6 | 3905 | static size_t disks_to_mpb_size(int disks) |
cdddbdbc | 3906 | { |
c2c087e6 | 3907 | size_t size; |
cdddbdbc | 3908 | |
c2c087e6 DW |
3909 | size = sizeof(struct imsm_super); |
3910 | size += (disks - 1) * sizeof(struct imsm_disk); | |
3911 | size += 2 * sizeof(struct imsm_dev); | |
3912 | /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */ | |
3913 | size += (4 - 2) * sizeof(struct imsm_map); | |
3914 | /* 4 possible disk_ord_tbl's */ | |
3915 | size += 4 * (disks - 1) * sizeof(__u32); | |
bbab0940 TM |
3916 | /* maximum bbm log */ |
3917 | size += sizeof(struct bbm_log); | |
c2c087e6 DW |
3918 | |
3919 | return size; | |
3920 | } | |
3921 | ||
387fcd59 N |
3922 | static __u64 avail_size_imsm(struct supertype *st, __u64 devsize, |
3923 | unsigned long long data_offset) | |
c2c087e6 DW |
3924 | { |
3925 | if (devsize < (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS)) | |
3926 | return 0; | |
3927 | ||
3928 | return devsize - (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS); | |
cdddbdbc DW |
3929 | } |
3930 | ||
ba2de7ba DW |
3931 | static void free_devlist(struct intel_super *super) |
3932 | { | |
3933 | struct intel_dev *dv; | |
3934 | ||
3935 | while (super->devlist) { | |
3936 | dv = super->devlist->next; | |
3937 | free(super->devlist->dev); | |
3938 | free(super->devlist); | |
3939 | super->devlist = dv; | |
3940 | } | |
3941 | } | |
3942 | ||
3943 | static void imsm_copy_dev(struct imsm_dev *dest, struct imsm_dev *src) | |
3944 | { | |
3945 | memcpy(dest, src, sizeof_imsm_dev(src, 0)); | |
3946 | } | |
3947 | ||
c7b8547c MT |
3948 | static int compare_super_imsm(struct supertype *st, struct supertype *tst, |
3949 | int verbose) | |
cdddbdbc | 3950 | { |
601ffa78 | 3951 | /* return: |
cdddbdbc | 3952 | * 0 same, or first was empty, and second was copied |
601ffa78 | 3953 | * 1 sb are different |
cdddbdbc DW |
3954 | */ |
3955 | struct intel_super *first = st->sb; | |
3956 | struct intel_super *sec = tst->sb; | |
3957 | ||
5d500228 N |
3958 | if (!first) { |
3959 | st->sb = tst->sb; | |
3960 | tst->sb = NULL; | |
3961 | return 0; | |
3962 | } | |
601ffa78 | 3963 | |
8603ea6f LM |
3964 | /* in platform dependent environment test if the disks |
3965 | * use the same Intel hba | |
601ffa78 OS |
3966 | * if not on Intel hba at all, allow anything. |
3967 | * doesn't check HBAs if num_raid_devs is not set, as it means | |
3968 | * it is a free floating spare, and all spares regardless of HBA type | |
3969 | * will fall into separate container during the assembly | |
8603ea6f | 3970 | */ |
601ffa78 | 3971 | if (first->hba && sec->hba && first->anchor->num_raid_devs != 0) { |
6b781d33 | 3972 | if (first->hba->type != sec->hba->type) { |
c7b8547c MT |
3973 | if (verbose) |
3974 | pr_err("HBAs of devices do not match %s != %s\n", | |
3975 | get_sys_dev_type(first->hba->type), | |
3976 | get_sys_dev_type(sec->hba->type)); | |
601ffa78 | 3977 | return 1; |
6b781d33 AP |
3978 | } |
3979 | if (first->orom != sec->orom) { | |
c7b8547c MT |
3980 | if (verbose) |
3981 | pr_err("HBAs of devices do not match %s != %s\n", | |
3982 | first->hba->pci_id, sec->hba->pci_id); | |
601ffa78 | 3983 | return 1; |
8603ea6f LM |
3984 | } |
3985 | } | |
cdddbdbc | 3986 | |
d23fe947 DW |
3987 | if (first->anchor->num_raid_devs > 0 && |
3988 | sec->anchor->num_raid_devs > 0) { | |
a2b97981 DW |
3989 | /* Determine if these disks might ever have been |
3990 | * related. Further disambiguation can only take place | |
3991 | * in load_super_imsm_all | |
3992 | */ | |
3993 | __u32 first_family = first->anchor->orig_family_num; | |
3994 | __u32 sec_family = sec->anchor->orig_family_num; | |
3995 | ||
f796af5d DW |
3996 | if (memcmp(first->anchor->sig, sec->anchor->sig, |
3997 | MAX_SIGNATURE_LENGTH) != 0) | |
601ffa78 | 3998 | return 1; |
f796af5d | 3999 | |
a2b97981 DW |
4000 | if (first_family == 0) |
4001 | first_family = first->anchor->family_num; | |
4002 | if (sec_family == 0) | |
4003 | sec_family = sec->anchor->family_num; | |
4004 | ||
4005 | if (first_family != sec_family) | |
601ffa78 | 4006 | return 1; |
f796af5d | 4007 | |
d23fe947 | 4008 | } |
cdddbdbc | 4009 | |
601ffa78 OS |
4010 | /* if an anchor does not have num_raid_devs set then it is a free |
4011 | * floating spare. don't assosiate spare with any array, as during assembly | |
4012 | * spares shall fall into separate container, from which they can be moved | |
4013 | * when necessary | |
4014 | */ | |
4015 | if (first->anchor->num_raid_devs ^ sec->anchor->num_raid_devs) | |
4016 | return 1; | |
3e372e5a | 4017 | |
cdddbdbc DW |
4018 | return 0; |
4019 | } | |
4020 | ||
0030e8d6 DW |
4021 | static void fd2devname(int fd, char *name) |
4022 | { | |
0030e8d6 | 4023 | char *nm; |
0030e8d6 | 4024 | |
7c798f87 MT |
4025 | nm = fd2kname(fd); |
4026 | if (!nm) | |
0030e8d6 | 4027 | return; |
9587c373 | 4028 | |
7c798f87 | 4029 | snprintf(name, MAX_RAID_SERIAL_LEN, "/dev/%s", nm); |
0030e8d6 DW |
4030 | } |
4031 | ||
21e9380b AP |
4032 | static int nvme_get_serial(int fd, void *buf, size_t buf_len) |
4033 | { | |
fcebeb77 | 4034 | char path[PATH_MAX]; |
21e9380b AP |
4035 | char *name = fd2kname(fd); |
4036 | ||
4037 | if (!name) | |
4038 | return 1; | |
4039 | ||
4040 | if (strncmp(name, "nvme", 4) != 0) | |
4041 | return 1; | |
4042 | ||
fcebeb77 MT |
4043 | if (!diskfd_to_devpath(fd, 1, path)) |
4044 | return 1; | |
21e9380b | 4045 | |
fcebeb77 | 4046 | return devpath_to_char(path, "serial", buf, buf_len, 0); |
21e9380b AP |
4047 | } |
4048 | ||
cdddbdbc DW |
4049 | extern int scsi_get_serial(int fd, void *buf, size_t buf_len); |
4050 | ||
4051 | static int imsm_read_serial(int fd, char *devname, | |
6da53c0e | 4052 | __u8 *serial, size_t serial_buf_len) |
cdddbdbc | 4053 | { |
21e9380b | 4054 | char buf[50]; |
cdddbdbc | 4055 | int rv; |
6da53c0e | 4056 | size_t len; |
316e2bf4 DW |
4057 | char *dest; |
4058 | char *src; | |
21e9380b AP |
4059 | unsigned int i; |
4060 | ||
4061 | memset(buf, 0, sizeof(buf)); | |
cdddbdbc | 4062 | |
21e9380b | 4063 | rv = nvme_get_serial(fd, buf, sizeof(buf)); |
cdddbdbc | 4064 | |
21e9380b AP |
4065 | if (rv) |
4066 | rv = scsi_get_serial(fd, buf, sizeof(buf)); | |
f9ba0ff1 | 4067 | |
40ebbb9c | 4068 | if (rv && check_env("IMSM_DEVNAME_AS_SERIAL")) { |
f9ba0ff1 DW |
4069 | memset(serial, 0, MAX_RAID_SERIAL_LEN); |
4070 | fd2devname(fd, (char *) serial); | |
0030e8d6 DW |
4071 | return 0; |
4072 | } | |
4073 | ||
cdddbdbc DW |
4074 | if (rv != 0) { |
4075 | if (devname) | |
e7b84f9d N |
4076 | pr_err("Failed to retrieve serial for %s\n", |
4077 | devname); | |
cdddbdbc DW |
4078 | return rv; |
4079 | } | |
4080 | ||
316e2bf4 DW |
4081 | /* trim all whitespace and non-printable characters and convert |
4082 | * ':' to ';' | |
4083 | */ | |
21e9380b AP |
4084 | for (i = 0, dest = buf; i < sizeof(buf) && buf[i]; i++) { |
4085 | src = &buf[i]; | |
316e2bf4 DW |
4086 | if (*src > 0x20) { |
4087 | /* ':' is reserved for use in placeholder serial | |
4088 | * numbers for missing disks | |
4089 | */ | |
4090 | if (*src == ':') | |
4091 | *dest++ = ';'; | |
4092 | else | |
4093 | *dest++ = *src; | |
4094 | } | |
4095 | } | |
21e9380b AP |
4096 | len = dest - buf; |
4097 | dest = buf; | |
316e2bf4 | 4098 | |
6da53c0e BK |
4099 | if (len > serial_buf_len) { |
4100 | /* truncate leading characters */ | |
4101 | dest += len - serial_buf_len; | |
4102 | len = serial_buf_len; | |
316e2bf4 | 4103 | } |
5c3db629 | 4104 | |
6da53c0e | 4105 | memset(serial, 0, serial_buf_len); |
316e2bf4 | 4106 | memcpy(serial, dest, len); |
cdddbdbc DW |
4107 | |
4108 | return 0; | |
4109 | } | |
4110 | ||
1f24f035 DW |
4111 | static int serialcmp(__u8 *s1, __u8 *s2) |
4112 | { | |
4113 | return strncmp((char *) s1, (char *) s2, MAX_RAID_SERIAL_LEN); | |
4114 | } | |
4115 | ||
4116 | static void serialcpy(__u8 *dest, __u8 *src) | |
4117 | { | |
4118 | strncpy((char *) dest, (char *) src, MAX_RAID_SERIAL_LEN); | |
4119 | } | |
4120 | ||
54c2c1ea DW |
4121 | static struct dl *serial_to_dl(__u8 *serial, struct intel_super *super) |
4122 | { | |
4123 | struct dl *dl; | |
4124 | ||
4125 | for (dl = super->disks; dl; dl = dl->next) | |
4126 | if (serialcmp(dl->serial, serial) == 0) | |
4127 | break; | |
4128 | ||
4129 | return dl; | |
4130 | } | |
4131 | ||
a2b97981 DW |
4132 | static struct imsm_disk * |
4133 | __serial_to_disk(__u8 *serial, struct imsm_super *mpb, int *idx) | |
4134 | { | |
4135 | int i; | |
4136 | ||
4137 | for (i = 0; i < mpb->num_disks; i++) { | |
4138 | struct imsm_disk *disk = __get_imsm_disk(mpb, i); | |
4139 | ||
4140 | if (serialcmp(disk->serial, serial) == 0) { | |
4141 | if (idx) | |
4142 | *idx = i; | |
4143 | return disk; | |
4144 | } | |
4145 | } | |
4146 | ||
4147 | return NULL; | |
4148 | } | |
4149 | ||
cdddbdbc DW |
4150 | static int |
4151 | load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd) | |
4152 | { | |
a2b97981 | 4153 | struct imsm_disk *disk; |
cdddbdbc DW |
4154 | struct dl *dl; |
4155 | struct stat stb; | |
cdddbdbc | 4156 | int rv; |
a2b97981 | 4157 | char name[40]; |
d23fe947 DW |
4158 | __u8 serial[MAX_RAID_SERIAL_LEN]; |
4159 | ||
6da53c0e | 4160 | rv = imsm_read_serial(fd, devname, serial, MAX_RAID_SERIAL_LEN); |
d23fe947 DW |
4161 | |
4162 | if (rv != 0) | |
4163 | return 2; | |
4164 | ||
503975b9 | 4165 | dl = xcalloc(1, sizeof(*dl)); |
cdddbdbc | 4166 | |
a2b97981 DW |
4167 | fstat(fd, &stb); |
4168 | dl->major = major(stb.st_rdev); | |
4169 | dl->minor = minor(stb.st_rdev); | |
4170 | dl->next = super->disks; | |
4171 | dl->fd = keep_fd ? fd : -1; | |
4172 | assert(super->disks == NULL); | |
4173 | super->disks = dl; | |
4174 | serialcpy(dl->serial, serial); | |
4175 | dl->index = -2; | |
4176 | dl->e = NULL; | |
4177 | fd2devname(fd, name); | |
4178 | if (devname) | |
503975b9 | 4179 | dl->devname = xstrdup(devname); |
a2b97981 | 4180 | else |
503975b9 | 4181 | dl->devname = xstrdup(name); |
cdddbdbc | 4182 | |
d23fe947 | 4183 | /* look up this disk's index in the current anchor */ |
a2b97981 DW |
4184 | disk = __serial_to_disk(dl->serial, super->anchor, &dl->index); |
4185 | if (disk) { | |
4186 | dl->disk = *disk; | |
4187 | /* only set index on disks that are a member of a | |
4188 | * populated contianer, i.e. one with raid_devs | |
4189 | */ | |
4190 | if (is_failed(&dl->disk)) | |
3f6efecc | 4191 | dl->index = -2; |
2432ce9b | 4192 | else if (is_spare(&dl->disk) || is_journal(&dl->disk)) |
a2b97981 | 4193 | dl->index = -1; |
3f6efecc DW |
4194 | } |
4195 | ||
949c47a0 DW |
4196 | return 0; |
4197 | } | |
4198 | ||
0c046afd DW |
4199 | /* When migrating map0 contains the 'destination' state while map1 |
4200 | * contains the current state. When not migrating map0 contains the | |
4201 | * current state. This routine assumes that map[0].map_state is set to | |
4202 | * the current array state before being called. | |
4203 | * | |
4204 | * Migration is indicated by one of the following states | |
4205 | * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed) | |
e3bba0e0 | 4206 | * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal |
0c046afd | 4207 | * map1state=unitialized) |
1484e727 | 4208 | * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal |
0c046afd | 4209 | * map1state=normal) |
e3bba0e0 | 4210 | * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal |
0c046afd | 4211 | * map1state=degraded) |
8e59f3d8 AK |
4212 | * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal |
4213 | * map1state=normal) | |
0c046afd | 4214 | */ |
8e59f3d8 AK |
4215 | static void migrate(struct imsm_dev *dev, struct intel_super *super, |
4216 | __u8 to_state, int migr_type) | |
3393c6af | 4217 | { |
0c046afd | 4218 | struct imsm_map *dest; |
238c0a71 | 4219 | struct imsm_map *src = get_imsm_map(dev, MAP_0); |
3393c6af | 4220 | |
0c046afd | 4221 | dev->vol.migr_state = 1; |
1484e727 | 4222 | set_migr_type(dev, migr_type); |
4036e7ee | 4223 | set_vol_curr_migr_unit(dev, 0); |
238c0a71 | 4224 | dest = get_imsm_map(dev, MAP_1); |
0c046afd | 4225 | |
0556e1a2 | 4226 | /* duplicate and then set the target end state in map[0] */ |
3393c6af | 4227 | memcpy(dest, src, sizeof_imsm_map(src)); |
fb12a745 | 4228 | if (migr_type == MIGR_GEN_MIGR) { |
0556e1a2 DW |
4229 | __u32 ord; |
4230 | int i; | |
4231 | ||
4232 | for (i = 0; i < src->num_members; i++) { | |
4233 | ord = __le32_to_cpu(src->disk_ord_tbl[i]); | |
4234 | set_imsm_ord_tbl_ent(src, i, ord_to_idx(ord)); | |
4235 | } | |
4236 | } | |
4237 | ||
8e59f3d8 AK |
4238 | if (migr_type == MIGR_GEN_MIGR) |
4239 | /* Clear migration record */ | |
4240 | memset(super->migr_rec, 0, sizeof(struct migr_record)); | |
4241 | ||
0c046afd | 4242 | src->map_state = to_state; |
949c47a0 | 4243 | } |
f8f603f1 | 4244 | |
809da78e AK |
4245 | static void end_migration(struct imsm_dev *dev, struct intel_super *super, |
4246 | __u8 map_state) | |
f8f603f1 | 4247 | { |
238c0a71 AK |
4248 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
4249 | struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state == 0 ? | |
4250 | MAP_0 : MAP_1); | |
28bce06f | 4251 | int i, j; |
0556e1a2 DW |
4252 | |
4253 | /* merge any IMSM_ORD_REBUILD bits that were not successfully | |
4254 | * completed in the last migration. | |
4255 | * | |
28bce06f | 4256 | * FIXME add support for raid-level-migration |
0556e1a2 | 4257 | */ |
089f9d79 JS |
4258 | if (map_state != map->map_state && (is_gen_migration(dev) == 0) && |
4259 | prev->map_state != IMSM_T_STATE_UNINITIALIZED) { | |
809da78e AK |
4260 | /* when final map state is other than expected |
4261 | * merge maps (not for migration) | |
4262 | */ | |
4263 | int failed; | |
4264 | ||
4265 | for (i = 0; i < prev->num_members; i++) | |
4266 | for (j = 0; j < map->num_members; j++) | |
4267 | /* during online capacity expansion | |
4268 | * disks position can be changed | |
4269 | * if takeover is used | |
4270 | */ | |
4271 | if (ord_to_idx(map->disk_ord_tbl[j]) == | |
4272 | ord_to_idx(prev->disk_ord_tbl[i])) { | |
4273 | map->disk_ord_tbl[j] |= | |
4274 | prev->disk_ord_tbl[i]; | |
4275 | break; | |
4276 | } | |
4277 | failed = imsm_count_failed(super, dev, MAP_0); | |
4278 | map_state = imsm_check_degraded(super, dev, failed, MAP_0); | |
4279 | } | |
f8f603f1 DW |
4280 | |
4281 | dev->vol.migr_state = 0; | |
ea672ee1 | 4282 | set_migr_type(dev, 0); |
4036e7ee | 4283 | set_vol_curr_migr_unit(dev, 0); |
f8f603f1 DW |
4284 | map->map_state = map_state; |
4285 | } | |
949c47a0 DW |
4286 | |
4287 | static int parse_raid_devices(struct intel_super *super) | |
4288 | { | |
4289 | int i; | |
4290 | struct imsm_dev *dev_new; | |
4d7b1503 | 4291 | size_t len, len_migr; |
401d313b | 4292 | size_t max_len = 0; |
4d7b1503 DW |
4293 | size_t space_needed = 0; |
4294 | struct imsm_super *mpb = super->anchor; | |
949c47a0 DW |
4295 | |
4296 | for (i = 0; i < super->anchor->num_raid_devs; i++) { | |
4297 | struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i); | |
ba2de7ba | 4298 | struct intel_dev *dv; |
949c47a0 | 4299 | |
4d7b1503 DW |
4300 | len = sizeof_imsm_dev(dev_iter, 0); |
4301 | len_migr = sizeof_imsm_dev(dev_iter, 1); | |
4302 | if (len_migr > len) | |
4303 | space_needed += len_migr - len; | |
ca9de185 | 4304 | |
503975b9 | 4305 | dv = xmalloc(sizeof(*dv)); |
401d313b AK |
4306 | if (max_len < len_migr) |
4307 | max_len = len_migr; | |
4308 | if (max_len > len_migr) | |
4309 | space_needed += max_len - len_migr; | |
503975b9 | 4310 | dev_new = xmalloc(max_len); |
949c47a0 | 4311 | imsm_copy_dev(dev_new, dev_iter); |
ba2de7ba DW |
4312 | dv->dev = dev_new; |
4313 | dv->index = i; | |
4314 | dv->next = super->devlist; | |
4315 | super->devlist = dv; | |
949c47a0 | 4316 | } |
cdddbdbc | 4317 | |
4d7b1503 DW |
4318 | /* ensure that super->buf is large enough when all raid devices |
4319 | * are migrating | |
4320 | */ | |
4321 | if (__le32_to_cpu(mpb->mpb_size) + space_needed > super->len) { | |
4322 | void *buf; | |
4323 | ||
f36a9ecd PB |
4324 | len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + space_needed, |
4325 | super->sector_size); | |
4326 | if (posix_memalign(&buf, MAX_SECTOR_SIZE, len) != 0) | |
4d7b1503 DW |
4327 | return 1; |
4328 | ||
1f45a8ad DW |
4329 | memcpy(buf, super->buf, super->len); |
4330 | memset(buf + super->len, 0, len - super->len); | |
4d7b1503 DW |
4331 | free(super->buf); |
4332 | super->buf = buf; | |
4333 | super->len = len; | |
4334 | } | |
ca9de185 | 4335 | |
bbab0940 TM |
4336 | super->extra_space += space_needed; |
4337 | ||
cdddbdbc DW |
4338 | return 0; |
4339 | } | |
4340 | ||
e2f41b2c AK |
4341 | /******************************************************************************* |
4342 | * Function: check_mpb_migr_compatibility | |
4343 | * Description: Function checks for unsupported migration features: | |
4344 | * - migration optimization area (pba_of_lba0) | |
4345 | * - descending reshape (ascending_migr) | |
4346 | * Parameters: | |
4347 | * super : imsm metadata information | |
4348 | * Returns: | |
4349 | * 0 : migration is compatible | |
4350 | * -1 : migration is not compatible | |
4351 | ******************************************************************************/ | |
4352 | int check_mpb_migr_compatibility(struct intel_super *super) | |
4353 | { | |
4354 | struct imsm_map *map0, *map1; | |
4355 | struct migr_record *migr_rec = super->migr_rec; | |
4356 | int i; | |
4357 | ||
4358 | for (i = 0; i < super->anchor->num_raid_devs; i++) { | |
4359 | struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i); | |
4360 | ||
4361 | if (dev_iter && | |
4362 | dev_iter->vol.migr_state == 1 && | |
4363 | dev_iter->vol.migr_type == MIGR_GEN_MIGR) { | |
4364 | /* This device is migrating */ | |
238c0a71 AK |
4365 | map0 = get_imsm_map(dev_iter, MAP_0); |
4366 | map1 = get_imsm_map(dev_iter, MAP_1); | |
5551b113 | 4367 | if (pba_of_lba0(map0) != pba_of_lba0(map1)) |
e2f41b2c AK |
4368 | /* migration optimization area was used */ |
4369 | return -1; | |
fc54fe7a JS |
4370 | if (migr_rec->ascending_migr == 0 && |
4371 | migr_rec->dest_depth_per_unit > 0) | |
e2f41b2c AK |
4372 | /* descending reshape not supported yet */ |
4373 | return -1; | |
4374 | } | |
4375 | } | |
4376 | return 0; | |
4377 | } | |
4378 | ||
d23fe947 | 4379 | static void __free_imsm(struct intel_super *super, int free_disks); |
9ca2c81c | 4380 | |
cdddbdbc | 4381 | /* load_imsm_mpb - read matrix metadata |
f2f5c343 | 4382 | * allocates super->mpb to be freed by free_imsm |
cdddbdbc DW |
4383 | */ |
4384 | static int load_imsm_mpb(int fd, struct intel_super *super, char *devname) | |
4385 | { | |
4386 | unsigned long long dsize; | |
cdddbdbc | 4387 | unsigned long long sectors; |
f36a9ecd | 4388 | unsigned int sector_size = super->sector_size; |
cdddbdbc | 4389 | struct stat; |
6416d527 | 4390 | struct imsm_super *anchor; |
cdddbdbc DW |
4391 | __u32 check_sum; |
4392 | ||
cdddbdbc | 4393 | get_dev_size(fd, NULL, &dsize); |
f36a9ecd | 4394 | if (dsize < 2*sector_size) { |
64436f06 | 4395 | if (devname) |
e7b84f9d N |
4396 | pr_err("%s: device to small for imsm\n", |
4397 | devname); | |
64436f06 N |
4398 | return 1; |
4399 | } | |
cdddbdbc | 4400 | |
f36a9ecd | 4401 | if (lseek64(fd, dsize - (sector_size * 2), SEEK_SET) < 0) { |
cdddbdbc | 4402 | if (devname) |
e7b84f9d N |
4403 | pr_err("Cannot seek to anchor block on %s: %s\n", |
4404 | devname, strerror(errno)); | |
cdddbdbc DW |
4405 | return 1; |
4406 | } | |
4407 | ||
f36a9ecd | 4408 | if (posix_memalign((void **)&anchor, sector_size, sector_size) != 0) { |
ad97895e | 4409 | if (devname) |
7a862a02 | 4410 | pr_err("Failed to allocate imsm anchor buffer on %s\n", devname); |
ad97895e DW |
4411 | return 1; |
4412 | } | |
466070ad | 4413 | if ((unsigned int)read(fd, anchor, sector_size) != sector_size) { |
cdddbdbc | 4414 | if (devname) |
e7b84f9d N |
4415 | pr_err("Cannot read anchor block on %s: %s\n", |
4416 | devname, strerror(errno)); | |
6416d527 | 4417 | free(anchor); |
cdddbdbc DW |
4418 | return 1; |
4419 | } | |
4420 | ||
6416d527 | 4421 | if (strncmp((char *) anchor->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) { |
cdddbdbc | 4422 | if (devname) |
e7b84f9d | 4423 | pr_err("no IMSM anchor on %s\n", devname); |
6416d527 | 4424 | free(anchor); |
cdddbdbc DW |
4425 | return 2; |
4426 | } | |
4427 | ||
d23fe947 | 4428 | __free_imsm(super, 0); |
f2f5c343 LM |
4429 | /* reload capability and hba */ |
4430 | ||
4431 | /* capability and hba must be updated with new super allocation */ | |
d424212e | 4432 | find_intel_hba_capability(fd, super, devname); |
f36a9ecd PB |
4433 | super->len = ROUND_UP(anchor->mpb_size, sector_size); |
4434 | if (posix_memalign(&super->buf, MAX_SECTOR_SIZE, super->len) != 0) { | |
cdddbdbc | 4435 | if (devname) |
e7b84f9d N |
4436 | pr_err("unable to allocate %zu byte mpb buffer\n", |
4437 | super->len); | |
6416d527 | 4438 | free(anchor); |
cdddbdbc DW |
4439 | return 2; |
4440 | } | |
f36a9ecd | 4441 | memcpy(super->buf, anchor, sector_size); |
cdddbdbc | 4442 | |
f36a9ecd | 4443 | sectors = mpb_sectors(anchor, sector_size) - 1; |
6416d527 | 4444 | free(anchor); |
8e59f3d8 | 4445 | |
85337573 AO |
4446 | if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE, |
4447 | MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE) != 0) { | |
1ade5cc1 | 4448 | pr_err("could not allocate migr_rec buffer\n"); |
8e59f3d8 AK |
4449 | free(super->buf); |
4450 | return 2; | |
4451 | } | |
51d83f5d | 4452 | super->clean_migration_record_by_mdmon = 0; |
8e59f3d8 | 4453 | |
949c47a0 | 4454 | if (!sectors) { |
ecf45690 DW |
4455 | check_sum = __gen_imsm_checksum(super->anchor); |
4456 | if (check_sum != __le32_to_cpu(super->anchor->check_sum)) { | |
4457 | if (devname) | |
e7b84f9d N |
4458 | pr_err("IMSM checksum %x != %x on %s\n", |
4459 | check_sum, | |
4460 | __le32_to_cpu(super->anchor->check_sum), | |
4461 | devname); | |
ecf45690 DW |
4462 | return 2; |
4463 | } | |
4464 | ||
a2b97981 | 4465 | return 0; |
949c47a0 | 4466 | } |
cdddbdbc DW |
4467 | |
4468 | /* read the extended mpb */ | |
f36a9ecd | 4469 | if (lseek64(fd, dsize - (sector_size * (2 + sectors)), SEEK_SET) < 0) { |
cdddbdbc | 4470 | if (devname) |
e7b84f9d N |
4471 | pr_err("Cannot seek to extended mpb on %s: %s\n", |
4472 | devname, strerror(errno)); | |
cdddbdbc DW |
4473 | return 1; |
4474 | } | |
4475 | ||
f36a9ecd PB |
4476 | if ((unsigned int)read(fd, super->buf + sector_size, |
4477 | super->len - sector_size) != super->len - sector_size) { | |
cdddbdbc | 4478 | if (devname) |
e7b84f9d N |
4479 | pr_err("Cannot read extended mpb on %s: %s\n", |
4480 | devname, strerror(errno)); | |
cdddbdbc DW |
4481 | return 2; |
4482 | } | |
4483 | ||
949c47a0 DW |
4484 | check_sum = __gen_imsm_checksum(super->anchor); |
4485 | if (check_sum != __le32_to_cpu(super->anchor->check_sum)) { | |
cdddbdbc | 4486 | if (devname) |
e7b84f9d N |
4487 | pr_err("IMSM checksum %x != %x on %s\n", |
4488 | check_sum, __le32_to_cpu(super->anchor->check_sum), | |
4489 | devname); | |
db575f3b | 4490 | return 3; |
cdddbdbc DW |
4491 | } |
4492 | ||
a2b97981 DW |
4493 | return 0; |
4494 | } | |
4495 | ||
8e59f3d8 AK |
4496 | static int read_imsm_migr_rec(int fd, struct intel_super *super); |
4497 | ||
97f81ee2 CA |
4498 | /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */ |
4499 | static void clear_hi(struct intel_super *super) | |
4500 | { | |
4501 | struct imsm_super *mpb = super->anchor; | |
4502 | int i, n; | |
4503 | if (mpb->attributes & MPB_ATTRIB_2TB_DISK) | |
4504 | return; | |
4505 | for (i = 0; i < mpb->num_disks; ++i) { | |
4506 | struct imsm_disk *disk = &mpb->disk[i]; | |
4507 | disk->total_blocks_hi = 0; | |
4508 | } | |
4509 | for (i = 0; i < mpb->num_raid_devs; ++i) { | |
4510 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
4511 | if (!dev) | |
4512 | return; | |
4513 | for (n = 0; n < 2; ++n) { | |
4514 | struct imsm_map *map = get_imsm_map(dev, n); | |
4515 | if (!map) | |
4516 | continue; | |
4517 | map->pba_of_lba0_hi = 0; | |
4518 | map->blocks_per_member_hi = 0; | |
4519 | map->num_data_stripes_hi = 0; | |
4520 | } | |
4521 | } | |
4522 | } | |
4523 | ||
a2b97981 DW |
4524 | static int |
4525 | load_and_parse_mpb(int fd, struct intel_super *super, char *devname, int keep_fd) | |
4526 | { | |
4527 | int err; | |
4528 | ||
4529 | err = load_imsm_mpb(fd, super, devname); | |
4530 | if (err) | |
4531 | return err; | |
f36a9ecd PB |
4532 | if (super->sector_size == 4096) |
4533 | convert_from_4k(super); | |
a2b97981 DW |
4534 | err = load_imsm_disk(fd, super, devname, keep_fd); |
4535 | if (err) | |
4536 | return err; | |
4537 | err = parse_raid_devices(super); | |
8d67477f TM |
4538 | if (err) |
4539 | return err; | |
4540 | err = load_bbm_log(super); | |
97f81ee2 | 4541 | clear_hi(super); |
a2b97981 | 4542 | return err; |
cdddbdbc DW |
4543 | } |
4544 | ||
3a85bf0e | 4545 | static void __free_imsm_disk(struct dl *d, int close_fd) |
ae6aad82 | 4546 | { |
3a85bf0e | 4547 | if (close_fd && d->fd > -1) |
ae6aad82 DW |
4548 | close(d->fd); |
4549 | if (d->devname) | |
4550 | free(d->devname); | |
0dcecb2e DW |
4551 | if (d->e) |
4552 | free(d->e); | |
ae6aad82 DW |
4553 | free(d); |
4554 | ||
4555 | } | |
1a64be56 | 4556 | |
cdddbdbc DW |
4557 | static void free_imsm_disks(struct intel_super *super) |
4558 | { | |
47ee5a45 | 4559 | struct dl *d; |
cdddbdbc | 4560 | |
47ee5a45 DW |
4561 | while (super->disks) { |
4562 | d = super->disks; | |
cdddbdbc | 4563 | super->disks = d->next; |
3a85bf0e | 4564 | __free_imsm_disk(d, 1); |
cdddbdbc | 4565 | } |
cb82edca AK |
4566 | while (super->disk_mgmt_list) { |
4567 | d = super->disk_mgmt_list; | |
4568 | super->disk_mgmt_list = d->next; | |
3a85bf0e | 4569 | __free_imsm_disk(d, 1); |
cb82edca | 4570 | } |
47ee5a45 DW |
4571 | while (super->missing) { |
4572 | d = super->missing; | |
4573 | super->missing = d->next; | |
3a85bf0e | 4574 | __free_imsm_disk(d, 1); |
47ee5a45 DW |
4575 | } |
4576 | ||
cdddbdbc DW |
4577 | } |
4578 | ||
9ca2c81c | 4579 | /* free all the pieces hanging off of a super pointer */ |
d23fe947 | 4580 | static void __free_imsm(struct intel_super *super, int free_disks) |
cdddbdbc | 4581 | { |
88654014 LM |
4582 | struct intel_hba *elem, *next; |
4583 | ||
9ca2c81c | 4584 | if (super->buf) { |
949c47a0 | 4585 | free(super->buf); |
9ca2c81c DW |
4586 | super->buf = NULL; |
4587 | } | |
f2f5c343 LM |
4588 | /* unlink capability description */ |
4589 | super->orom = NULL; | |
8e59f3d8 AK |
4590 | if (super->migr_rec_buf) { |
4591 | free(super->migr_rec_buf); | |
4592 | super->migr_rec_buf = NULL; | |
4593 | } | |
d23fe947 DW |
4594 | if (free_disks) |
4595 | free_imsm_disks(super); | |
ba2de7ba | 4596 | free_devlist(super); |
88654014 LM |
4597 | elem = super->hba; |
4598 | while (elem) { | |
4599 | if (elem->path) | |
4600 | free((void *)elem->path); | |
4601 | next = elem->next; | |
4602 | free(elem); | |
4603 | elem = next; | |
88c32bb1 | 4604 | } |
8d67477f TM |
4605 | if (super->bbm_log) |
4606 | free(super->bbm_log); | |
88654014 | 4607 | super->hba = NULL; |
cdddbdbc DW |
4608 | } |
4609 | ||
9ca2c81c DW |
4610 | static void free_imsm(struct intel_super *super) |
4611 | { | |
d23fe947 | 4612 | __free_imsm(super, 1); |
928f1424 | 4613 | free(super->bb.entries); |
9ca2c81c DW |
4614 | free(super); |
4615 | } | |
cdddbdbc DW |
4616 | |
4617 | static void free_super_imsm(struct supertype *st) | |
4618 | { | |
4619 | struct intel_super *super = st->sb; | |
4620 | ||
4621 | if (!super) | |
4622 | return; | |
4623 | ||
4624 | free_imsm(super); | |
4625 | st->sb = NULL; | |
4626 | } | |
4627 | ||
49133e57 | 4628 | static struct intel_super *alloc_super(void) |
c2c087e6 | 4629 | { |
503975b9 | 4630 | struct intel_super *super = xcalloc(1, sizeof(*super)); |
c2c087e6 | 4631 | |
503975b9 N |
4632 | super->current_vol = -1; |
4633 | super->create_offset = ~((unsigned long long) 0); | |
928f1424 TM |
4634 | |
4635 | super->bb.entries = xmalloc(BBM_LOG_MAX_ENTRIES * | |
4636 | sizeof(struct md_bb_entry)); | |
4637 | if (!super->bb.entries) { | |
4638 | free(super); | |
4639 | return NULL; | |
4640 | } | |
4641 | ||
c2c087e6 DW |
4642 | return super; |
4643 | } | |
4644 | ||
f0f5a016 LM |
4645 | /* |
4646 | * find and allocate hba and OROM/EFI based on valid fd of RAID component device | |
4647 | */ | |
d424212e | 4648 | static int find_intel_hba_capability(int fd, struct intel_super *super, char *devname) |
f0f5a016 LM |
4649 | { |
4650 | struct sys_dev *hba_name; | |
4651 | int rv = 0; | |
4652 | ||
3a30e28e MT |
4653 | if (fd >= 0 && test_partition(fd)) { |
4654 | pr_err("imsm: %s is a partition, cannot be used in IMSM\n", | |
4655 | devname); | |
4656 | return 1; | |
4657 | } | |
089f9d79 | 4658 | if (fd < 0 || check_env("IMSM_NO_PLATFORM")) { |
f2f5c343 | 4659 | super->orom = NULL; |
f0f5a016 LM |
4660 | super->hba = NULL; |
4661 | return 0; | |
4662 | } | |
4663 | hba_name = find_disk_attached_hba(fd, NULL); | |
4664 | if (!hba_name) { | |
d424212e | 4665 | if (devname) |
e7b84f9d N |
4666 | pr_err("%s is not attached to Intel(R) RAID controller.\n", |
4667 | devname); | |
f0f5a016 LM |
4668 | return 1; |
4669 | } | |
4670 | rv = attach_hba_to_super(super, hba_name); | |
4671 | if (rv == 2) { | |
d424212e N |
4672 | if (devname) { |
4673 | struct intel_hba *hba = super->hba; | |
f0f5a016 | 4674 | |
60f0f54d PB |
4675 | pr_err("%s is attached to Intel(R) %s %s (%s),\n" |
4676 | " but the container is assigned to Intel(R) %s %s (", | |
d424212e | 4677 | devname, |
614902f6 | 4678 | get_sys_dev_type(hba_name->type), |
60f0f54d | 4679 | hba_name->type == SYS_DEV_VMD ? "domain" : "RAID controller", |
f0f5a016 | 4680 | hba_name->pci_id ? : "Err!", |
60f0f54d PB |
4681 | get_sys_dev_type(super->hba->type), |
4682 | hba->type == SYS_DEV_VMD ? "domain" : "RAID controller"); | |
f0f5a016 | 4683 | |
f0f5a016 LM |
4684 | while (hba) { |
4685 | fprintf(stderr, "%s", hba->pci_id ? : "Err!"); | |
4686 | if (hba->next) | |
4687 | fprintf(stderr, ", "); | |
4688 | hba = hba->next; | |
4689 | } | |
6b781d33 | 4690 | fprintf(stderr, ").\n" |
cca67208 | 4691 | " Mixing devices attached to different controllers is not allowed.\n"); |
f0f5a016 | 4692 | } |
f0f5a016 LM |
4693 | return 2; |
4694 | } | |
6b781d33 | 4695 | super->orom = find_imsm_capability(hba_name); |
f2f5c343 LM |
4696 | if (!super->orom) |
4697 | return 3; | |
614902f6 | 4698 | |
f0f5a016 LM |
4699 | return 0; |
4700 | } | |
4701 | ||
47ee5a45 DW |
4702 | /* find_missing - helper routine for load_super_imsm_all that identifies |
4703 | * disks that have disappeared from the system. This routine relies on | |
4704 | * the mpb being uptodate, which it is at load time. | |
4705 | */ | |
4706 | static int find_missing(struct intel_super *super) | |
4707 | { | |
4708 | int i; | |
4709 | struct imsm_super *mpb = super->anchor; | |
4710 | struct dl *dl; | |
4711 | struct imsm_disk *disk; | |
47ee5a45 DW |
4712 | |
4713 | for (i = 0; i < mpb->num_disks; i++) { | |
4714 | disk = __get_imsm_disk(mpb, i); | |
54c2c1ea | 4715 | dl = serial_to_dl(disk->serial, super); |
47ee5a45 DW |
4716 | if (dl) |
4717 | continue; | |
47ee5a45 | 4718 | |
503975b9 | 4719 | dl = xmalloc(sizeof(*dl)); |
47ee5a45 DW |
4720 | dl->major = 0; |
4721 | dl->minor = 0; | |
4722 | dl->fd = -1; | |
503975b9 | 4723 | dl->devname = xstrdup("missing"); |
47ee5a45 DW |
4724 | dl->index = i; |
4725 | serialcpy(dl->serial, disk->serial); | |
4726 | dl->disk = *disk; | |
689c9bf3 | 4727 | dl->e = NULL; |
47ee5a45 DW |
4728 | dl->next = super->missing; |
4729 | super->missing = dl; | |
4730 | } | |
4731 | ||
4732 | return 0; | |
4733 | } | |
4734 | ||
a2b97981 DW |
4735 | static struct intel_disk *disk_list_get(__u8 *serial, struct intel_disk *disk_list) |
4736 | { | |
4737 | struct intel_disk *idisk = disk_list; | |
4738 | ||
4739 | while (idisk) { | |
4740 | if (serialcmp(idisk->disk.serial, serial) == 0) | |
4741 | break; | |
4742 | idisk = idisk->next; | |
4743 | } | |
4744 | ||
4745 | return idisk; | |
4746 | } | |
4747 | ||
4748 | static int __prep_thunderdome(struct intel_super **table, int tbl_size, | |
4749 | struct intel_super *super, | |
4750 | struct intel_disk **disk_list) | |
4751 | { | |
4752 | struct imsm_disk *d = &super->disks->disk; | |
4753 | struct imsm_super *mpb = super->anchor; | |
4754 | int i, j; | |
4755 | ||
4756 | for (i = 0; i < tbl_size; i++) { | |
4757 | struct imsm_super *tbl_mpb = table[i]->anchor; | |
4758 | struct imsm_disk *tbl_d = &table[i]->disks->disk; | |
4759 | ||
4760 | if (tbl_mpb->family_num == mpb->family_num) { | |
4761 | if (tbl_mpb->check_sum == mpb->check_sum) { | |
1ade5cc1 N |
4762 | dprintf("mpb from %d:%d matches %d:%d\n", |
4763 | super->disks->major, | |
a2b97981 DW |
4764 | super->disks->minor, |
4765 | table[i]->disks->major, | |
4766 | table[i]->disks->minor); | |
4767 | break; | |
4768 | } | |
4769 | ||
4770 | if (((is_configured(d) && !is_configured(tbl_d)) || | |
4771 | is_configured(d) == is_configured(tbl_d)) && | |
4772 | tbl_mpb->generation_num < mpb->generation_num) { | |
4773 | /* current version of the mpb is a | |
4774 | * better candidate than the one in | |
4775 | * super_table, but copy over "cross | |
4776 | * generational" status | |
4777 | */ | |
4778 | struct intel_disk *idisk; | |
4779 | ||
1ade5cc1 N |
4780 | dprintf("mpb from %d:%d replaces %d:%d\n", |
4781 | super->disks->major, | |
a2b97981 DW |
4782 | super->disks->minor, |
4783 | table[i]->disks->major, | |
4784 | table[i]->disks->minor); | |
4785 | ||
4786 | idisk = disk_list_get(tbl_d->serial, *disk_list); | |
4787 | if (idisk && is_failed(&idisk->disk)) | |
4788 | tbl_d->status |= FAILED_DISK; | |
4789 | break; | |
4790 | } else { | |
4791 | struct intel_disk *idisk; | |
4792 | struct imsm_disk *disk; | |
4793 | ||
4794 | /* tbl_mpb is more up to date, but copy | |
4795 | * over cross generational status before | |
4796 | * returning | |
4797 | */ | |
4798 | disk = __serial_to_disk(d->serial, mpb, NULL); | |
4799 | if (disk && is_failed(disk)) | |
4800 | d->status |= FAILED_DISK; | |
4801 | ||
4802 | idisk = disk_list_get(d->serial, *disk_list); | |
4803 | if (idisk) { | |
4804 | idisk->owner = i; | |
4805 | if (disk && is_configured(disk)) | |
4806 | idisk->disk.status |= CONFIGURED_DISK; | |
4807 | } | |
4808 | ||
1ade5cc1 N |
4809 | dprintf("mpb from %d:%d prefer %d:%d\n", |
4810 | super->disks->major, | |
a2b97981 DW |
4811 | super->disks->minor, |
4812 | table[i]->disks->major, | |
4813 | table[i]->disks->minor); | |
4814 | ||
4815 | return tbl_size; | |
4816 | } | |
4817 | } | |
4818 | } | |
4819 | ||
4820 | if (i >= tbl_size) | |
4821 | table[tbl_size++] = super; | |
4822 | else | |
4823 | table[i] = super; | |
4824 | ||
4825 | /* update/extend the merged list of imsm_disk records */ | |
4826 | for (j = 0; j < mpb->num_disks; j++) { | |
4827 | struct imsm_disk *disk = __get_imsm_disk(mpb, j); | |
4828 | struct intel_disk *idisk; | |
4829 | ||
4830 | idisk = disk_list_get(disk->serial, *disk_list); | |
4831 | if (idisk) { | |
4832 | idisk->disk.status |= disk->status; | |
4833 | if (is_configured(&idisk->disk) || | |
4834 | is_failed(&idisk->disk)) | |
4835 | idisk->disk.status &= ~(SPARE_DISK); | |
4836 | } else { | |
503975b9 | 4837 | idisk = xcalloc(1, sizeof(*idisk)); |
a2b97981 DW |
4838 | idisk->owner = IMSM_UNKNOWN_OWNER; |
4839 | idisk->disk = *disk; | |
4840 | idisk->next = *disk_list; | |
4841 | *disk_list = idisk; | |
4842 | } | |
4843 | ||
4844 | if (serialcmp(idisk->disk.serial, d->serial) == 0) | |
4845 | idisk->owner = i; | |
4846 | } | |
4847 | ||
4848 | return tbl_size; | |
4849 | } | |
4850 | ||
4851 | static struct intel_super * | |
4852 | validate_members(struct intel_super *super, struct intel_disk *disk_list, | |
4853 | const int owner) | |
4854 | { | |
4855 | struct imsm_super *mpb = super->anchor; | |
4856 | int ok_count = 0; | |
4857 | int i; | |
4858 | ||
4859 | for (i = 0; i < mpb->num_disks; i++) { | |
4860 | struct imsm_disk *disk = __get_imsm_disk(mpb, i); | |
4861 | struct intel_disk *idisk; | |
4862 | ||
4863 | idisk = disk_list_get(disk->serial, disk_list); | |
4864 | if (idisk) { | |
4865 | if (idisk->owner == owner || | |
4866 | idisk->owner == IMSM_UNKNOWN_OWNER) | |
4867 | ok_count++; | |
4868 | else | |
1ade5cc1 N |
4869 | dprintf("'%.16s' owner %d != %d\n", |
4870 | disk->serial, idisk->owner, | |
a2b97981 DW |
4871 | owner); |
4872 | } else { | |
1ade5cc1 N |
4873 | dprintf("unknown disk %x [%d]: %.16s\n", |
4874 | __le32_to_cpu(mpb->family_num), i, | |
a2b97981 DW |
4875 | disk->serial); |
4876 | break; | |
4877 | } | |
4878 | } | |
4879 | ||
4880 | if (ok_count == mpb->num_disks) | |
4881 | return super; | |
4882 | return NULL; | |
4883 | } | |
4884 | ||
4885 | static void show_conflicts(__u32 family_num, struct intel_super *super_list) | |
4886 | { | |
4887 | struct intel_super *s; | |
4888 | ||
4889 | for (s = super_list; s; s = s->next) { | |
4890 | if (family_num != s->anchor->family_num) | |
4891 | continue; | |
e12b3daa | 4892 | pr_err("Conflict, offlining family %#x on '%s'\n", |
a2b97981 DW |
4893 | __le32_to_cpu(family_num), s->disks->devname); |
4894 | } | |
4895 | } | |
4896 | ||
4897 | static struct intel_super * | |
4898 | imsm_thunderdome(struct intel_super **super_list, int len) | |
4899 | { | |
4900 | struct intel_super *super_table[len]; | |
4901 | struct intel_disk *disk_list = NULL; | |
4902 | struct intel_super *champion, *spare; | |
4903 | struct intel_super *s, **del; | |
4904 | int tbl_size = 0; | |
4905 | int conflict; | |
4906 | int i; | |
4907 | ||
4908 | memset(super_table, 0, sizeof(super_table)); | |
4909 | for (s = *super_list; s; s = s->next) | |
4910 | tbl_size = __prep_thunderdome(super_table, tbl_size, s, &disk_list); | |
4911 | ||
4912 | for (i = 0; i < tbl_size; i++) { | |
4913 | struct imsm_disk *d; | |
4914 | struct intel_disk *idisk; | |
4915 | struct imsm_super *mpb = super_table[i]->anchor; | |
4916 | ||
4917 | s = super_table[i]; | |
4918 | d = &s->disks->disk; | |
4919 | ||
4920 | /* 'd' must appear in merged disk list for its | |
4921 | * configuration to be valid | |
4922 | */ | |
4923 | idisk = disk_list_get(d->serial, disk_list); | |
4924 | if (idisk && idisk->owner == i) | |
4925 | s = validate_members(s, disk_list, i); | |
4926 | else | |
4927 | s = NULL; | |
4928 | ||
4929 | if (!s) | |
1ade5cc1 N |
4930 | dprintf("marking family: %#x from %d:%d offline\n", |
4931 | mpb->family_num, | |
a2b97981 DW |
4932 | super_table[i]->disks->major, |
4933 | super_table[i]->disks->minor); | |
4934 | super_table[i] = s; | |
4935 | } | |
4936 | ||
4937 | /* This is where the mdadm implementation differs from the Windows | |
4938 | * driver which has no strict concept of a container. We can only | |
4939 | * assemble one family from a container, so when returning a prodigal | |
4940 | * array member to this system the code will not be able to disambiguate | |
4941 | * the container contents that should be assembled ("foreign" versus | |
4942 | * "local"). It requires user intervention to set the orig_family_num | |
4943 | * to a new value to establish a new container. The Windows driver in | |
4944 | * this situation fixes up the volume name in place and manages the | |
4945 | * foreign array as an independent entity. | |
4946 | */ | |
4947 | s = NULL; | |
4948 | spare = NULL; | |
4949 | conflict = 0; | |
4950 | for (i = 0; i < tbl_size; i++) { | |
4951 | struct intel_super *tbl_ent = super_table[i]; | |
4952 | int is_spare = 0; | |
4953 | ||
4954 | if (!tbl_ent) | |
4955 | continue; | |
4956 | ||
4957 | if (tbl_ent->anchor->num_raid_devs == 0) { | |
4958 | spare = tbl_ent; | |
4959 | is_spare = 1; | |
4960 | } | |
4961 | ||
4962 | if (s && !is_spare) { | |
4963 | show_conflicts(tbl_ent->anchor->family_num, *super_list); | |
4964 | conflict++; | |
4965 | } else if (!s && !is_spare) | |
4966 | s = tbl_ent; | |
4967 | } | |
4968 | ||
4969 | if (!s) | |
4970 | s = spare; | |
4971 | if (!s) { | |
4972 | champion = NULL; | |
4973 | goto out; | |
4974 | } | |
4975 | champion = s; | |
4976 | ||
4977 | if (conflict) | |
7a862a02 | 4978 | pr_err("Chose family %#x on '%s', assemble conflicts to new container with '--update=uuid'\n", |
a2b97981 DW |
4979 | __le32_to_cpu(s->anchor->family_num), s->disks->devname); |
4980 | ||
4981 | /* collect all dl's onto 'champion', and update them to | |
4982 | * champion's version of the status | |
4983 | */ | |
4984 | for (s = *super_list; s; s = s->next) { | |
4985 | struct imsm_super *mpb = champion->anchor; | |
4986 | struct dl *dl = s->disks; | |
4987 | ||
4988 | if (s == champion) | |
4989 | continue; | |
4990 | ||
5d7b407a CA |
4991 | mpb->attributes |= s->anchor->attributes & MPB_ATTRIB_2TB_DISK; |
4992 | ||
a2b97981 DW |
4993 | for (i = 0; i < mpb->num_disks; i++) { |
4994 | struct imsm_disk *disk; | |
4995 | ||
4996 | disk = __serial_to_disk(dl->serial, mpb, &dl->index); | |
4997 | if (disk) { | |
4998 | dl->disk = *disk; | |
4999 | /* only set index on disks that are a member of | |
5000 | * a populated contianer, i.e. one with | |
5001 | * raid_devs | |
5002 | */ | |
5003 | if (is_failed(&dl->disk)) | |
5004 | dl->index = -2; | |
5005 | else if (is_spare(&dl->disk)) | |
5006 | dl->index = -1; | |
5007 | break; | |
5008 | } | |
5009 | } | |
5010 | ||
5011 | if (i >= mpb->num_disks) { | |
5012 | struct intel_disk *idisk; | |
5013 | ||
5014 | idisk = disk_list_get(dl->serial, disk_list); | |
ecf408e9 | 5015 | if (idisk && is_spare(&idisk->disk) && |
a2b97981 DW |
5016 | !is_failed(&idisk->disk) && !is_configured(&idisk->disk)) |
5017 | dl->index = -1; | |
5018 | else { | |
5019 | dl->index = -2; | |
5020 | continue; | |
5021 | } | |
5022 | } | |
5023 | ||
5024 | dl->next = champion->disks; | |
5025 | champion->disks = dl; | |
5026 | s->disks = NULL; | |
5027 | } | |
5028 | ||
5029 | /* delete 'champion' from super_list */ | |
5030 | for (del = super_list; *del; ) { | |
5031 | if (*del == champion) { | |
5032 | *del = (*del)->next; | |
5033 | break; | |
5034 | } else | |
5035 | del = &(*del)->next; | |
5036 | } | |
5037 | champion->next = NULL; | |
5038 | ||
5039 | out: | |
5040 | while (disk_list) { | |
5041 | struct intel_disk *idisk = disk_list; | |
5042 | ||
5043 | disk_list = disk_list->next; | |
5044 | free(idisk); | |
5045 | } | |
5046 | ||
5047 | return champion; | |
5048 | } | |
5049 | ||
9587c373 LM |
5050 | static int |
5051 | get_sra_super_block(int fd, struct intel_super **super_list, char *devname, int *max, int keep_fd); | |
4dd2df09 | 5052 | static int get_super_block(struct intel_super **super_list, char *devnm, char *devname, |
9587c373 | 5053 | int major, int minor, int keep_fd); |
ec50f7b6 LM |
5054 | static int |
5055 | get_devlist_super_block(struct md_list *devlist, struct intel_super **super_list, | |
5056 | int *max, int keep_fd); | |
5057 | ||
cdddbdbc | 5058 | static int load_super_imsm_all(struct supertype *st, int fd, void **sbp, |
ec50f7b6 LM |
5059 | char *devname, struct md_list *devlist, |
5060 | int keep_fd) | |
cdddbdbc | 5061 | { |
a2b97981 DW |
5062 | struct intel_super *super_list = NULL; |
5063 | struct intel_super *super = NULL; | |
a2b97981 | 5064 | int err = 0; |
9587c373 | 5065 | int i = 0; |
dab4a513 | 5066 | |
9587c373 LM |
5067 | if (fd >= 0) |
5068 | /* 'fd' is an opened container */ | |
5069 | err = get_sra_super_block(fd, &super_list, devname, &i, keep_fd); | |
5070 | else | |
ec50f7b6 LM |
5071 | /* get super block from devlist devices */ |
5072 | err = get_devlist_super_block(devlist, &super_list, &i, keep_fd); | |
9587c373 | 5073 | if (err) |
1602d52c | 5074 | goto error; |
a2b97981 DW |
5075 | /* all mpbs enter, maybe one leaves */ |
5076 | super = imsm_thunderdome(&super_list, i); | |
5077 | if (!super) { | |
5078 | err = 1; | |
5079 | goto error; | |
cdddbdbc DW |
5080 | } |
5081 | ||
47ee5a45 DW |
5082 | if (find_missing(super) != 0) { |
5083 | free_imsm(super); | |
a2b97981 DW |
5084 | err = 2; |
5085 | goto error; | |
47ee5a45 | 5086 | } |
8e59f3d8 AK |
5087 | |
5088 | /* load migration record */ | |
2f86fda3 | 5089 | err = load_imsm_migr_rec(super); |
4c965cc9 AK |
5090 | if (err == -1) { |
5091 | /* migration is in progress, | |
5092 | * but migr_rec cannot be loaded, | |
5093 | */ | |
8e59f3d8 AK |
5094 | err = 4; |
5095 | goto error; | |
5096 | } | |
e2f41b2c AK |
5097 | |
5098 | /* Check migration compatibility */ | |
089f9d79 | 5099 | if (err == 0 && check_mpb_migr_compatibility(super) != 0) { |
e7b84f9d | 5100 | pr_err("Unsupported migration detected"); |
e2f41b2c AK |
5101 | if (devname) |
5102 | fprintf(stderr, " on %s\n", devname); | |
5103 | else | |
5104 | fprintf(stderr, " (IMSM).\n"); | |
5105 | ||
5106 | err = 5; | |
5107 | goto error; | |
5108 | } | |
5109 | ||
a2b97981 DW |
5110 | err = 0; |
5111 | ||
5112 | error: | |
5113 | while (super_list) { | |
5114 | struct intel_super *s = super_list; | |
5115 | ||
5116 | super_list = super_list->next; | |
5117 | free_imsm(s); | |
5118 | } | |
9587c373 | 5119 | |
a2b97981 DW |
5120 | if (err) |
5121 | return err; | |
f7e7067b | 5122 | |
cdddbdbc | 5123 | *sbp = super; |
9587c373 | 5124 | if (fd >= 0) |
4dd2df09 | 5125 | strcpy(st->container_devnm, fd2devnm(fd)); |
9587c373 | 5126 | else |
4dd2df09 | 5127 | st->container_devnm[0] = 0; |
a2b97981 | 5128 | if (err == 0 && st->ss == NULL) { |
bf5a934a | 5129 | st->ss = &super_imsm; |
cdddbdbc DW |
5130 | st->minor_version = 0; |
5131 | st->max_devs = IMSM_MAX_DEVICES; | |
5132 | } | |
cdddbdbc DW |
5133 | return 0; |
5134 | } | |
2b959fbf | 5135 | |
ec50f7b6 LM |
5136 | static int |
5137 | get_devlist_super_block(struct md_list *devlist, struct intel_super **super_list, | |
5138 | int *max, int keep_fd) | |
5139 | { | |
5140 | struct md_list *tmpdev; | |
5141 | int err = 0; | |
5142 | int i = 0; | |
9587c373 | 5143 | |
ec50f7b6 LM |
5144 | for (i = 0, tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) { |
5145 | if (tmpdev->used != 1) | |
5146 | continue; | |
5147 | if (tmpdev->container == 1) { | |
ca9de185 | 5148 | int lmax = 0; |
ec50f7b6 LM |
5149 | int fd = dev_open(tmpdev->devname, O_RDONLY|O_EXCL); |
5150 | if (fd < 0) { | |
e7b84f9d | 5151 | pr_err("cannot open device %s: %s\n", |
ec50f7b6 LM |
5152 | tmpdev->devname, strerror(errno)); |
5153 | err = 8; | |
5154 | goto error; | |
5155 | } | |
5156 | err = get_sra_super_block(fd, super_list, | |
5157 | tmpdev->devname, &lmax, | |
5158 | keep_fd); | |
5159 | i += lmax; | |
5160 | close(fd); | |
5161 | if (err) { | |
5162 | err = 7; | |
5163 | goto error; | |
5164 | } | |
5165 | } else { | |
5166 | int major = major(tmpdev->st_rdev); | |
5167 | int minor = minor(tmpdev->st_rdev); | |
5168 | err = get_super_block(super_list, | |
4dd2df09 | 5169 | NULL, |
ec50f7b6 LM |
5170 | tmpdev->devname, |
5171 | major, minor, | |
5172 | keep_fd); | |
5173 | i++; | |
5174 | if (err) { | |
5175 | err = 6; | |
5176 | goto error; | |
5177 | } | |
5178 | } | |
5179 | } | |
5180 | error: | |
5181 | *max = i; | |
5182 | return err; | |
5183 | } | |
9587c373 | 5184 | |
4dd2df09 | 5185 | static int get_super_block(struct intel_super **super_list, char *devnm, char *devname, |
9587c373 LM |
5186 | int major, int minor, int keep_fd) |
5187 | { | |
594dc1b8 | 5188 | struct intel_super *s; |
9587c373 LM |
5189 | char nm[32]; |
5190 | int dfd = -1; | |
9587c373 LM |
5191 | int err = 0; |
5192 | int retry; | |
5193 | ||
5194 | s = alloc_super(); | |
5195 | if (!s) { | |
5196 | err = 1; | |
5197 | goto error; | |
5198 | } | |
5199 | ||
5200 | sprintf(nm, "%d:%d", major, minor); | |
5201 | dfd = dev_open(nm, O_RDWR); | |
5202 | if (dfd < 0) { | |
5203 | err = 2; | |
5204 | goto error; | |
5205 | } | |
5206 | ||
aec01630 JS |
5207 | if (!get_dev_sector_size(dfd, NULL, &s->sector_size)) { |
5208 | err = 2; | |
5209 | goto error; | |
5210 | } | |
cb8f6859 | 5211 | find_intel_hba_capability(dfd, s, devname); |
9587c373 LM |
5212 | err = load_and_parse_mpb(dfd, s, NULL, keep_fd); |
5213 | ||
5214 | /* retry the load if we might have raced against mdmon */ | |
4dd2df09 | 5215 | if (err == 3 && devnm && mdmon_running(devnm)) |
9587c373 LM |
5216 | for (retry = 0; retry < 3; retry++) { |
5217 | usleep(3000); | |
5218 | err = load_and_parse_mpb(dfd, s, NULL, keep_fd); | |
5219 | if (err != 3) | |
5220 | break; | |
5221 | } | |
5222 | error: | |
5223 | if (!err) { | |
5224 | s->next = *super_list; | |
5225 | *super_list = s; | |
5226 | } else { | |
5227 | if (s) | |
8d67477f | 5228 | free_imsm(s); |
36614e95 | 5229 | if (dfd >= 0) |
9587c373 LM |
5230 | close(dfd); |
5231 | } | |
089f9d79 | 5232 | if (dfd >= 0 && !keep_fd) |
9587c373 LM |
5233 | close(dfd); |
5234 | return err; | |
5235 | ||
5236 | } | |
5237 | ||
5238 | static int | |
5239 | get_sra_super_block(int fd, struct intel_super **super_list, char *devname, int *max, int keep_fd) | |
5240 | { | |
5241 | struct mdinfo *sra; | |
4dd2df09 | 5242 | char *devnm; |
9587c373 LM |
5243 | struct mdinfo *sd; |
5244 | int err = 0; | |
5245 | int i = 0; | |
4dd2df09 | 5246 | sra = sysfs_read(fd, NULL, GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE); |
9587c373 LM |
5247 | if (!sra) |
5248 | return 1; | |
5249 | ||
5250 | if (sra->array.major_version != -1 || | |
5251 | sra->array.minor_version != -2 || | |
5252 | strcmp(sra->text_version, "imsm") != 0) { | |
5253 | err = 1; | |
5254 | goto error; | |
5255 | } | |
5256 | /* load all mpbs */ | |
4dd2df09 | 5257 | devnm = fd2devnm(fd); |
9587c373 | 5258 | for (sd = sra->devs, i = 0; sd; sd = sd->next, i++) { |
4dd2df09 | 5259 | if (get_super_block(super_list, devnm, devname, |
9587c373 LM |
5260 | sd->disk.major, sd->disk.minor, keep_fd) != 0) { |
5261 | err = 7; | |
5262 | goto error; | |
5263 | } | |
5264 | } | |
5265 | error: | |
5266 | sysfs_free(sra); | |
5267 | *max = i; | |
5268 | return err; | |
5269 | } | |
5270 | ||
2b959fbf N |
5271 | static int load_container_imsm(struct supertype *st, int fd, char *devname) |
5272 | { | |
ec50f7b6 | 5273 | return load_super_imsm_all(st, fd, &st->sb, devname, NULL, 1); |
2b959fbf | 5274 | } |
cdddbdbc DW |
5275 | |
5276 | static int load_super_imsm(struct supertype *st, int fd, char *devname) | |
5277 | { | |
5278 | struct intel_super *super; | |
5279 | int rv; | |
8a3544f8 | 5280 | int retry; |
cdddbdbc | 5281 | |
357ac106 | 5282 | if (test_partition(fd)) |
691c6ee1 N |
5283 | /* IMSM not allowed on partitions */ |
5284 | return 1; | |
5285 | ||
37424f13 DW |
5286 | free_super_imsm(st); |
5287 | ||
49133e57 | 5288 | super = alloc_super(); |
8d67477f TM |
5289 | if (!super) |
5290 | return 1; | |
3a85bf0e MG |
5291 | |
5292 | if (!get_dev_sector_size(fd, NULL, &super->sector_size)) { | |
5293 | free_imsm(super); | |
5294 | return 1; | |
5295 | } | |
ea2bc72b LM |
5296 | /* Load hba and capabilities if they exist. |
5297 | * But do not preclude loading metadata in case capabilities or hba are | |
5298 | * non-compliant and ignore_hw_compat is set. | |
5299 | */ | |
d424212e | 5300 | rv = find_intel_hba_capability(fd, super, devname); |
f2f5c343 | 5301 | /* no orom/efi or non-intel hba of the disk */ |
089f9d79 | 5302 | if (rv != 0 && st->ignore_hw_compat == 0) { |
f2f5c343 | 5303 | if (devname) |
e7b84f9d | 5304 | pr_err("No OROM/EFI properties for %s\n", devname); |
f2f5c343 LM |
5305 | free_imsm(super); |
5306 | return 2; | |
5307 | } | |
a2b97981 | 5308 | rv = load_and_parse_mpb(fd, super, devname, 0); |
cdddbdbc | 5309 | |
8a3544f8 AP |
5310 | /* retry the load if we might have raced against mdmon */ |
5311 | if (rv == 3) { | |
f96b1302 AP |
5312 | struct mdstat_ent *mdstat = NULL; |
5313 | char *name = fd2kname(fd); | |
5314 | ||
5315 | if (name) | |
5316 | mdstat = mdstat_by_component(name); | |
8a3544f8 AP |
5317 | |
5318 | if (mdstat && mdmon_running(mdstat->devnm) && getpid() != mdmon_pid(mdstat->devnm)) { | |
5319 | for (retry = 0; retry < 3; retry++) { | |
5320 | usleep(3000); | |
5321 | rv = load_and_parse_mpb(fd, super, devname, 0); | |
5322 | if (rv != 3) | |
5323 | break; | |
5324 | } | |
5325 | } | |
5326 | ||
5327 | free_mdstat(mdstat); | |
5328 | } | |
5329 | ||
cdddbdbc DW |
5330 | if (rv) { |
5331 | if (devname) | |
7a862a02 | 5332 | pr_err("Failed to load all information sections on %s\n", devname); |
cdddbdbc DW |
5333 | free_imsm(super); |
5334 | return rv; | |
5335 | } | |
5336 | ||
5337 | st->sb = super; | |
5338 | if (st->ss == NULL) { | |
5339 | st->ss = &super_imsm; | |
5340 | st->minor_version = 0; | |
5341 | st->max_devs = IMSM_MAX_DEVICES; | |
5342 | } | |
8e59f3d8 AK |
5343 | |
5344 | /* load migration record */ | |
2f86fda3 | 5345 | if (load_imsm_migr_rec(super) == 0) { |
2e062e82 AK |
5346 | /* Check for unsupported migration features */ |
5347 | if (check_mpb_migr_compatibility(super) != 0) { | |
e7b84f9d | 5348 | pr_err("Unsupported migration detected"); |
2e062e82 AK |
5349 | if (devname) |
5350 | fprintf(stderr, " on %s\n", devname); | |
5351 | else | |
5352 | fprintf(stderr, " (IMSM).\n"); | |
5353 | return 3; | |
5354 | } | |
e2f41b2c AK |
5355 | } |
5356 | ||
cdddbdbc DW |
5357 | return 0; |
5358 | } | |
5359 | ||
ef6ffade DW |
5360 | static __u16 info_to_blocks_per_strip(mdu_array_info_t *info) |
5361 | { | |
5362 | if (info->level == 1) | |
5363 | return 128; | |
5364 | return info->chunk_size >> 9; | |
5365 | } | |
5366 | ||
5551b113 CA |
5367 | static unsigned long long info_to_blocks_per_member(mdu_array_info_t *info, |
5368 | unsigned long long size) | |
fcfd9599 | 5369 | { |
4025c288 | 5370 | if (info->level == 1) |
5551b113 | 5371 | return size * 2; |
4025c288 | 5372 | else |
5551b113 | 5373 | return (size * 2) & ~(info_to_blocks_per_strip(info) - 1); |
fcfd9599 DW |
5374 | } |
5375 | ||
4d1313e9 DW |
5376 | static void imsm_update_version_info(struct intel_super *super) |
5377 | { | |
5378 | /* update the version and attributes */ | |
5379 | struct imsm_super *mpb = super->anchor; | |
5380 | char *version; | |
5381 | struct imsm_dev *dev; | |
5382 | struct imsm_map *map; | |
5383 | int i; | |
5384 | ||
5385 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
5386 | dev = get_imsm_dev(super, i); | |
238c0a71 | 5387 | map = get_imsm_map(dev, MAP_0); |
4d1313e9 DW |
5388 | if (__le32_to_cpu(dev->size_high) > 0) |
5389 | mpb->attributes |= MPB_ATTRIB_2TB; | |
5390 | ||
5391 | /* FIXME detect when an array spans a port multiplier */ | |
5392 | #if 0 | |
5393 | mpb->attributes |= MPB_ATTRIB_PM; | |
5394 | #endif | |
5395 | ||
5396 | if (mpb->num_raid_devs > 1 || | |
5397 | mpb->attributes != MPB_ATTRIB_CHECKSUM_VERIFY) { | |
5398 | version = MPB_VERSION_ATTRIBS; | |
5399 | switch (get_imsm_raid_level(map)) { | |
5400 | case 0: mpb->attributes |= MPB_ATTRIB_RAID0; break; | |
5401 | case 1: mpb->attributes |= MPB_ATTRIB_RAID1; break; | |
5402 | case 10: mpb->attributes |= MPB_ATTRIB_RAID10; break; | |
5403 | case 5: mpb->attributes |= MPB_ATTRIB_RAID5; break; | |
5404 | } | |
5405 | } else { | |
5406 | if (map->num_members >= 5) | |
5407 | version = MPB_VERSION_5OR6_DISK_ARRAY; | |
5408 | else if (dev->status == DEV_CLONE_N_GO) | |
5409 | version = MPB_VERSION_CNG; | |
5410 | else if (get_imsm_raid_level(map) == 5) | |
5411 | version = MPB_VERSION_RAID5; | |
5412 | else if (map->num_members >= 3) | |
5413 | version = MPB_VERSION_3OR4_DISK_ARRAY; | |
5414 | else if (get_imsm_raid_level(map) == 1) | |
5415 | version = MPB_VERSION_RAID1; | |
5416 | else | |
5417 | version = MPB_VERSION_RAID0; | |
5418 | } | |
5419 | strcpy(((char *) mpb->sig) + strlen(MPB_SIGNATURE), version); | |
5420 | } | |
5421 | } | |
5422 | ||
aa534678 DW |
5423 | static int check_name(struct intel_super *super, char *name, int quiet) |
5424 | { | |
5425 | struct imsm_super *mpb = super->anchor; | |
5426 | char *reason = NULL; | |
9bd99a90 RS |
5427 | char *start = name; |
5428 | size_t len = strlen(name); | |
aa534678 DW |
5429 | int i; |
5430 | ||
9bd99a90 RS |
5431 | if (len > 0) { |
5432 | while (isspace(start[len - 1])) | |
5433 | start[--len] = 0; | |
5434 | while (*start && isspace(*start)) | |
5435 | ++start, --len; | |
5436 | memmove(name, start, len + 1); | |
5437 | } | |
5438 | ||
5439 | if (len > MAX_RAID_SERIAL_LEN) | |
aa534678 | 5440 | reason = "must be 16 characters or less"; |
9bd99a90 RS |
5441 | else if (len == 0) |
5442 | reason = "must be a non-empty string"; | |
aa534678 DW |
5443 | |
5444 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
5445 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
5446 | ||
5447 | if (strncmp((char *) dev->volume, name, MAX_RAID_SERIAL_LEN) == 0) { | |
5448 | reason = "already exists"; | |
5449 | break; | |
5450 | } | |
5451 | } | |
5452 | ||
5453 | if (reason && !quiet) | |
e7b84f9d | 5454 | pr_err("imsm volume name %s\n", reason); |
aa534678 DW |
5455 | |
5456 | return !reason; | |
5457 | } | |
5458 | ||
8b353278 | 5459 | static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info, |
5308f117 | 5460 | struct shape *s, char *name, |
83cd1e97 N |
5461 | char *homehost, int *uuid, |
5462 | long long data_offset) | |
cdddbdbc | 5463 | { |
c2c087e6 DW |
5464 | /* We are creating a volume inside a pre-existing container. |
5465 | * so st->sb is already set. | |
5466 | */ | |
5467 | struct intel_super *super = st->sb; | |
f36a9ecd | 5468 | unsigned int sector_size = super->sector_size; |
949c47a0 | 5469 | struct imsm_super *mpb = super->anchor; |
ba2de7ba | 5470 | struct intel_dev *dv; |
c2c087e6 DW |
5471 | struct imsm_dev *dev; |
5472 | struct imsm_vol *vol; | |
5473 | struct imsm_map *map; | |
5474 | int idx = mpb->num_raid_devs; | |
5475 | int i; | |
760365f9 | 5476 | int namelen; |
c2c087e6 | 5477 | unsigned long long array_blocks; |
2c092cad | 5478 | size_t size_old, size_new; |
b53bfba6 TM |
5479 | unsigned int data_disks; |
5480 | unsigned long long size_per_member; | |
cdddbdbc | 5481 | |
88c32bb1 | 5482 | if (super->orom && mpb->num_raid_devs >= super->orom->vpa) { |
7a862a02 | 5483 | pr_err("This imsm-container already has the maximum of %d volumes\n", super->orom->vpa); |
c2c087e6 DW |
5484 | return 0; |
5485 | } | |
5486 | ||
2c092cad DW |
5487 | /* ensure the mpb is large enough for the new data */ |
5488 | size_old = __le32_to_cpu(mpb->mpb_size); | |
5489 | size_new = disks_to_mpb_size(info->nr_disks); | |
5490 | if (size_new > size_old) { | |
5491 | void *mpb_new; | |
f36a9ecd | 5492 | size_t size_round = ROUND_UP(size_new, sector_size); |
2c092cad | 5493 | |
f36a9ecd | 5494 | if (posix_memalign(&mpb_new, sector_size, size_round) != 0) { |
e7b84f9d | 5495 | pr_err("could not allocate new mpb\n"); |
2c092cad DW |
5496 | return 0; |
5497 | } | |
85337573 AO |
5498 | if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE, |
5499 | MIGR_REC_BUF_SECTORS* | |
5500 | MAX_SECTOR_SIZE) != 0) { | |
1ade5cc1 | 5501 | pr_err("could not allocate migr_rec buffer\n"); |
8e59f3d8 AK |
5502 | free(super->buf); |
5503 | free(super); | |
ea944c8f | 5504 | free(mpb_new); |
8e59f3d8 AK |
5505 | return 0; |
5506 | } | |
2c092cad DW |
5507 | memcpy(mpb_new, mpb, size_old); |
5508 | free(mpb); | |
5509 | mpb = mpb_new; | |
949c47a0 | 5510 | super->anchor = mpb_new; |
2c092cad DW |
5511 | mpb->mpb_size = __cpu_to_le32(size_new); |
5512 | memset(mpb_new + size_old, 0, size_round - size_old); | |
bbab0940 | 5513 | super->len = size_round; |
2c092cad | 5514 | } |
bf5a934a | 5515 | super->current_vol = idx; |
3960e579 DW |
5516 | |
5517 | /* handle 'failed_disks' by either: | |
5518 | * a) create dummy disk entries in the table if this the first | |
5519 | * volume in the array. We add them here as this is the only | |
5520 | * opportunity to add them. add_to_super_imsm_volume() | |
5521 | * handles the non-failed disks and continues incrementing | |
5522 | * mpb->num_disks. | |
5523 | * b) validate that 'failed_disks' matches the current number | |
5524 | * of missing disks if the container is populated | |
d23fe947 | 5525 | */ |
3960e579 | 5526 | if (super->current_vol == 0) { |
d23fe947 | 5527 | mpb->num_disks = 0; |
3960e579 DW |
5528 | for (i = 0; i < info->failed_disks; i++) { |
5529 | struct imsm_disk *disk; | |
5530 | ||
5531 | mpb->num_disks++; | |
5532 | disk = __get_imsm_disk(mpb, i); | |
5533 | disk->status = CONFIGURED_DISK | FAILED_DISK; | |
5534 | disk->scsi_id = __cpu_to_le32(~(__u32)0); | |
5535 | snprintf((char *) disk->serial, MAX_RAID_SERIAL_LEN, | |
5b975129 | 5536 | "missing:%d", (__u8)i); |
3960e579 DW |
5537 | } |
5538 | find_missing(super); | |
5539 | } else { | |
5540 | int missing = 0; | |
5541 | struct dl *d; | |
5542 | ||
5543 | for (d = super->missing; d; d = d->next) | |
5544 | missing++; | |
5545 | if (info->failed_disks > missing) { | |
e7b84f9d | 5546 | pr_err("unable to add 'missing' disk to container\n"); |
3960e579 DW |
5547 | return 0; |
5548 | } | |
5549 | } | |
5a038140 | 5550 | |
aa534678 DW |
5551 | if (!check_name(super, name, 0)) |
5552 | return 0; | |
503975b9 N |
5553 | dv = xmalloc(sizeof(*dv)); |
5554 | dev = xcalloc(1, sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1)); | |
760365f9 JS |
5555 | /* |
5556 | * Explicitly allow truncating to not confuse gcc's | |
5557 | * -Werror=stringop-truncation | |
5558 | */ | |
5559 | namelen = min((int) strlen(name), MAX_RAID_SERIAL_LEN); | |
5560 | memcpy(dev->volume, name, namelen); | |
e03640bd | 5561 | array_blocks = calc_array_size(info->level, info->raid_disks, |
03bcbc65 | 5562 | info->layout, info->chunk_size, |
b53bfba6 TM |
5563 | s->size * BLOCKS_PER_KB); |
5564 | data_disks = get_data_disks(info->level, info->layout, | |
5565 | info->raid_disks); | |
5566 | array_blocks = round_size_to_mb(array_blocks, data_disks); | |
5567 | size_per_member = array_blocks / data_disks; | |
979d38be | 5568 | |
fcc2c9da | 5569 | set_imsm_dev_size(dev, array_blocks); |
1a2487c2 | 5570 | dev->status = (DEV_READ_COALESCING | DEV_WRITE_COALESCING); |
c2c087e6 DW |
5571 | vol = &dev->vol; |
5572 | vol->migr_state = 0; | |
1484e727 | 5573 | set_migr_type(dev, MIGR_INIT); |
3960e579 | 5574 | vol->dirty = !info->state; |
4036e7ee | 5575 | set_vol_curr_migr_unit(dev, 0); |
238c0a71 | 5576 | map = get_imsm_map(dev, MAP_0); |
5551b113 | 5577 | set_pba_of_lba0(map, super->create_offset); |
ef6ffade | 5578 | map->blocks_per_strip = __cpu_to_le16(info_to_blocks_per_strip(info)); |
0556e1a2 | 5579 | map->failed_disk_num = ~0; |
bf4442ab | 5580 | if (info->level > 0) |
fffaf1ff N |
5581 | map->map_state = (info->state ? IMSM_T_STATE_NORMAL |
5582 | : IMSM_T_STATE_UNINITIALIZED); | |
bf4442ab AK |
5583 | else |
5584 | map->map_state = info->failed_disks ? IMSM_T_STATE_FAILED : | |
5585 | IMSM_T_STATE_NORMAL; | |
252d23c0 | 5586 | map->ddf = 1; |
ef6ffade DW |
5587 | |
5588 | if (info->level == 1 && info->raid_disks > 2) { | |
38950822 AW |
5589 | free(dev); |
5590 | free(dv); | |
7a862a02 | 5591 | pr_err("imsm does not support more than 2 disksin a raid1 volume\n"); |
ef6ffade DW |
5592 | return 0; |
5593 | } | |
81062a36 DW |
5594 | |
5595 | map->raid_level = info->level; | |
1c275381 | 5596 | if (info->level == 10) |
c2c087e6 | 5597 | map->raid_level = 1; |
1c275381 | 5598 | set_num_domains(map); |
ef6ffade | 5599 | |
44490938 MD |
5600 | size_per_member += NUM_BLOCKS_DIRTY_STRIPE_REGION; |
5601 | set_blocks_per_member(map, info_to_blocks_per_member(info, | |
5602 | size_per_member / | |
5603 | BLOCKS_PER_KB)); | |
5604 | ||
c2c087e6 | 5605 | map->num_members = info->raid_disks; |
1c275381 | 5606 | update_num_data_stripes(map, array_blocks); |
c2c087e6 DW |
5607 | for (i = 0; i < map->num_members; i++) { |
5608 | /* initialized in add_to_super */ | |
4eb26970 | 5609 | set_imsm_ord_tbl_ent(map, i, IMSM_ORD_REBUILD); |
c2c087e6 | 5610 | } |
949c47a0 | 5611 | mpb->num_raid_devs++; |
2a24dc1b PB |
5612 | mpb->num_raid_devs_created++; |
5613 | dev->my_vol_raid_dev_num = mpb->num_raid_devs_created; | |
ba2de7ba | 5614 | |
b7580566 | 5615 | if (s->consistency_policy <= CONSISTENCY_POLICY_RESYNC) { |
c2462068 | 5616 | dev->rwh_policy = RWH_MULTIPLE_OFF; |
2432ce9b | 5617 | } else if (s->consistency_policy == CONSISTENCY_POLICY_PPL) { |
c2462068 | 5618 | dev->rwh_policy = RWH_MULTIPLE_DISTRIBUTED; |
2432ce9b AP |
5619 | } else { |
5620 | free(dev); | |
5621 | free(dv); | |
5622 | pr_err("imsm does not support consistency policy %s\n", | |
5623 | map_num(consistency_policies, s->consistency_policy)); | |
5624 | return 0; | |
5625 | } | |
5626 | ||
ba2de7ba DW |
5627 | dv->dev = dev; |
5628 | dv->index = super->current_vol; | |
5629 | dv->next = super->devlist; | |
5630 | super->devlist = dv; | |
c2c087e6 | 5631 | |
4d1313e9 DW |
5632 | imsm_update_version_info(super); |
5633 | ||
c2c087e6 | 5634 | return 1; |
cdddbdbc DW |
5635 | } |
5636 | ||
bf5a934a | 5637 | static int init_super_imsm(struct supertype *st, mdu_array_info_t *info, |
5308f117 | 5638 | struct shape *s, char *name, |
83cd1e97 N |
5639 | char *homehost, int *uuid, |
5640 | unsigned long long data_offset) | |
bf5a934a DW |
5641 | { |
5642 | /* This is primarily called by Create when creating a new array. | |
5643 | * We will then get add_to_super called for each component, and then | |
5644 | * write_init_super called to write it out to each device. | |
5645 | * For IMSM, Create can create on fresh devices or on a pre-existing | |
5646 | * array. | |
5647 | * To create on a pre-existing array a different method will be called. | |
5648 | * This one is just for fresh drives. | |
5649 | */ | |
5650 | struct intel_super *super; | |
5651 | struct imsm_super *mpb; | |
5652 | size_t mpb_size; | |
4d1313e9 | 5653 | char *version; |
bf5a934a | 5654 | |
83cd1e97 | 5655 | if (data_offset != INVALID_SECTORS) { |
ed503f89 | 5656 | pr_err("data-offset not supported by imsm\n"); |
83cd1e97 N |
5657 | return 0; |
5658 | } | |
5659 | ||
bf5a934a | 5660 | if (st->sb) |
5308f117 | 5661 | return init_super_imsm_volume(st, info, s, name, homehost, uuid, |
83cd1e97 | 5662 | data_offset); |
e683ca88 DW |
5663 | |
5664 | if (info) | |
5665 | mpb_size = disks_to_mpb_size(info->nr_disks); | |
5666 | else | |
f36a9ecd | 5667 | mpb_size = MAX_SECTOR_SIZE; |
bf5a934a | 5668 | |
49133e57 | 5669 | super = alloc_super(); |
f36a9ecd PB |
5670 | if (super && |
5671 | posix_memalign(&super->buf, MAX_SECTOR_SIZE, mpb_size) != 0) { | |
8d67477f | 5672 | free_imsm(super); |
e683ca88 DW |
5673 | super = NULL; |
5674 | } | |
5675 | if (!super) { | |
1ade5cc1 | 5676 | pr_err("could not allocate superblock\n"); |
bf5a934a DW |
5677 | return 0; |
5678 | } | |
de44e46f PB |
5679 | if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE, |
5680 | MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE) != 0) { | |
1ade5cc1 | 5681 | pr_err("could not allocate migr_rec buffer\n"); |
8e59f3d8 | 5682 | free(super->buf); |
8d67477f | 5683 | free_imsm(super); |
8e59f3d8 AK |
5684 | return 0; |
5685 | } | |
e683ca88 | 5686 | memset(super->buf, 0, mpb_size); |
ef649044 | 5687 | mpb = super->buf; |
e683ca88 DW |
5688 | mpb->mpb_size = __cpu_to_le32(mpb_size); |
5689 | st->sb = super; | |
5690 | ||
5691 | if (info == NULL) { | |
5692 | /* zeroing superblock */ | |
5693 | return 0; | |
5694 | } | |
bf5a934a | 5695 | |
4d1313e9 DW |
5696 | mpb->attributes = MPB_ATTRIB_CHECKSUM_VERIFY; |
5697 | ||
5698 | version = (char *) mpb->sig; | |
5699 | strcpy(version, MPB_SIGNATURE); | |
5700 | version += strlen(MPB_SIGNATURE); | |
5701 | strcpy(version, MPB_VERSION_RAID0); | |
bf5a934a | 5702 | |
bf5a934a DW |
5703 | return 1; |
5704 | } | |
5705 | ||
f2cc4f7d AO |
5706 | static int drive_validate_sector_size(struct intel_super *super, struct dl *dl) |
5707 | { | |
5708 | unsigned int member_sector_size; | |
5709 | ||
5710 | if (dl->fd < 0) { | |
5711 | pr_err("Invalid file descriptor for %s\n", dl->devname); | |
5712 | return 0; | |
5713 | } | |
5714 | ||
5715 | if (!get_dev_sector_size(dl->fd, dl->devname, &member_sector_size)) | |
5716 | return 0; | |
5717 | if (member_sector_size != super->sector_size) | |
5718 | return 0; | |
5719 | return 1; | |
5720 | } | |
5721 | ||
f20c3968 | 5722 | static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk, |
bf5a934a DW |
5723 | int fd, char *devname) |
5724 | { | |
5725 | struct intel_super *super = st->sb; | |
d23fe947 | 5726 | struct imsm_super *mpb = super->anchor; |
3960e579 | 5727 | struct imsm_disk *_disk; |
bf5a934a DW |
5728 | struct imsm_dev *dev; |
5729 | struct imsm_map *map; | |
3960e579 | 5730 | struct dl *dl, *df; |
4eb26970 | 5731 | int slot; |
bf5a934a | 5732 | |
949c47a0 | 5733 | dev = get_imsm_dev(super, super->current_vol); |
238c0a71 | 5734 | map = get_imsm_map(dev, MAP_0); |
bf5a934a | 5735 | |
208933a7 | 5736 | if (! (dk->state & (1<<MD_DISK_SYNC))) { |
e7b84f9d | 5737 | pr_err("%s: Cannot add spare devices to IMSM volume\n", |
208933a7 N |
5738 | devname); |
5739 | return 1; | |
5740 | } | |
5741 | ||
efb30e7f DW |
5742 | if (fd == -1) { |
5743 | /* we're doing autolayout so grab the pre-marked (in | |
5744 | * validate_geometry) raid_disk | |
5745 | */ | |
5746 | for (dl = super->disks; dl; dl = dl->next) | |
5747 | if (dl->raiddisk == dk->raid_disk) | |
5748 | break; | |
5749 | } else { | |
5750 | for (dl = super->disks; dl ; dl = dl->next) | |
5751 | if (dl->major == dk->major && | |
5752 | dl->minor == dk->minor) | |
5753 | break; | |
5754 | } | |
d23fe947 | 5755 | |
208933a7 | 5756 | if (!dl) { |
e7b84f9d | 5757 | pr_err("%s is not a member of the same container\n", devname); |
f20c3968 | 5758 | return 1; |
208933a7 | 5759 | } |
bf5a934a | 5760 | |
59632db9 MZ |
5761 | if (mpb->num_disks == 0) |
5762 | if (!get_dev_sector_size(dl->fd, dl->devname, | |
5763 | &super->sector_size)) | |
5764 | return 1; | |
5765 | ||
f2cc4f7d AO |
5766 | if (!drive_validate_sector_size(super, dl)) { |
5767 | pr_err("Combining drives of different sector size in one volume is not allowed\n"); | |
5768 | return 1; | |
5769 | } | |
5770 | ||
d23fe947 DW |
5771 | /* add a pristine spare to the metadata */ |
5772 | if (dl->index < 0) { | |
5773 | dl->index = super->anchor->num_disks; | |
5774 | super->anchor->num_disks++; | |
5775 | } | |
4eb26970 DW |
5776 | /* Check the device has not already been added */ |
5777 | slot = get_imsm_disk_slot(map, dl->index); | |
5778 | if (slot >= 0 && | |
238c0a71 | 5779 | (get_imsm_ord_tbl_ent(dev, slot, MAP_X) & IMSM_ORD_REBUILD) == 0) { |
e7b84f9d | 5780 | pr_err("%s has been included in this array twice\n", |
4eb26970 DW |
5781 | devname); |
5782 | return 1; | |
5783 | } | |
656b6b5a | 5784 | set_imsm_ord_tbl_ent(map, dk->raid_disk, dl->index); |
ee5aad5a | 5785 | dl->disk.status = CONFIGURED_DISK; |
d23fe947 | 5786 | |
3960e579 DW |
5787 | /* update size of 'missing' disks to be at least as large as the |
5788 | * largest acitve member (we only have dummy missing disks when | |
5789 | * creating the first volume) | |
5790 | */ | |
5791 | if (super->current_vol == 0) { | |
5792 | for (df = super->missing; df; df = df->next) { | |
5551b113 CA |
5793 | if (total_blocks(&dl->disk) > total_blocks(&df->disk)) |
5794 | set_total_blocks(&df->disk, total_blocks(&dl->disk)); | |
3960e579 DW |
5795 | _disk = __get_imsm_disk(mpb, df->index); |
5796 | *_disk = df->disk; | |
5797 | } | |
5798 | } | |
5799 | ||
5800 | /* refresh unset/failed slots to point to valid 'missing' entries */ | |
5801 | for (df = super->missing; df; df = df->next) | |
5802 | for (slot = 0; slot < mpb->num_disks; slot++) { | |
238c0a71 | 5803 | __u32 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X); |
3960e579 DW |
5804 | |
5805 | if ((ord & IMSM_ORD_REBUILD) == 0) | |
5806 | continue; | |
5807 | set_imsm_ord_tbl_ent(map, slot, df->index | IMSM_ORD_REBUILD); | |
1ace8403 | 5808 | if (is_gen_migration(dev)) { |
238c0a71 AK |
5809 | struct imsm_map *map2 = get_imsm_map(dev, |
5810 | MAP_1); | |
0a108d63 | 5811 | int slot2 = get_imsm_disk_slot(map2, df->index); |
089f9d79 | 5812 | if (slot2 < map2->num_members && slot2 >= 0) { |
1ace8403 | 5813 | __u32 ord2 = get_imsm_ord_tbl_ent(dev, |
238c0a71 AK |
5814 | slot2, |
5815 | MAP_1); | |
1ace8403 AK |
5816 | if ((unsigned)df->index == |
5817 | ord_to_idx(ord2)) | |
5818 | set_imsm_ord_tbl_ent(map2, | |
0a108d63 | 5819 | slot2, |
1ace8403 AK |
5820 | df->index | |
5821 | IMSM_ORD_REBUILD); | |
5822 | } | |
5823 | } | |
3960e579 DW |
5824 | dprintf("set slot:%d to missing disk:%d\n", slot, df->index); |
5825 | break; | |
5826 | } | |
5827 | ||
d23fe947 DW |
5828 | /* if we are creating the first raid device update the family number */ |
5829 | if (super->current_vol == 0) { | |
5830 | __u32 sum; | |
5831 | struct imsm_dev *_dev = __get_imsm_dev(mpb, 0); | |
d23fe947 | 5832 | |
3960e579 | 5833 | _disk = __get_imsm_disk(mpb, dl->index); |
791b666a | 5834 | if (!_dev || !_disk) { |
e7b84f9d | 5835 | pr_err("BUG mpb setup error\n"); |
791b666a AW |
5836 | return 1; |
5837 | } | |
d23fe947 DW |
5838 | *_dev = *dev; |
5839 | *_disk = dl->disk; | |
148acb7b DW |
5840 | sum = random32(); |
5841 | sum += __gen_imsm_checksum(mpb); | |
d23fe947 | 5842 | mpb->family_num = __cpu_to_le32(sum); |
148acb7b | 5843 | mpb->orig_family_num = mpb->family_num; |
e48aed3c | 5844 | mpb->creation_time = __cpu_to_le64((__u64)time(NULL)); |
d23fe947 | 5845 | } |
ca0748fa | 5846 | super->current_disk = dl; |
f20c3968 | 5847 | return 0; |
bf5a934a DW |
5848 | } |
5849 | ||
a8619d23 AK |
5850 | /* mark_spare() |
5851 | * Function marks disk as spare and restores disk serial | |
5852 | * in case it was previously marked as failed by takeover operation | |
5853 | * reruns: | |
5854 | * -1 : critical error | |
5855 | * 0 : disk is marked as spare but serial is not set | |
5856 | * 1 : success | |
5857 | */ | |
5858 | int mark_spare(struct dl *disk) | |
5859 | { | |
5860 | __u8 serial[MAX_RAID_SERIAL_LEN]; | |
5861 | int ret_val = -1; | |
5862 | ||
5863 | if (!disk) | |
5864 | return ret_val; | |
5865 | ||
5866 | ret_val = 0; | |
6da53c0e | 5867 | if (!imsm_read_serial(disk->fd, NULL, serial, MAX_RAID_SERIAL_LEN)) { |
a8619d23 AK |
5868 | /* Restore disk serial number, because takeover marks disk |
5869 | * as failed and adds to serial ':0' before it becomes | |
5870 | * a spare disk. | |
5871 | */ | |
5872 | serialcpy(disk->serial, serial); | |
5873 | serialcpy(disk->disk.serial, serial); | |
5874 | ret_val = 1; | |
5875 | } | |
5876 | disk->disk.status = SPARE_DISK; | |
5877 | disk->index = -1; | |
5878 | ||
5879 | return ret_val; | |
5880 | } | |
88654014 | 5881 | |
12724c01 TM |
5882 | |
5883 | static int write_super_imsm_spare(struct intel_super *super, struct dl *d); | |
5884 | ||
f20c3968 | 5885 | static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk, |
72ca9bcf N |
5886 | int fd, char *devname, |
5887 | unsigned long long data_offset) | |
cdddbdbc | 5888 | { |
c2c087e6 | 5889 | struct intel_super *super = st->sb; |
c2c087e6 DW |
5890 | struct dl *dd; |
5891 | unsigned long long size; | |
fa7bb6f8 | 5892 | unsigned int member_sector_size; |
f2f27e63 | 5893 | __u32 id; |
c2c087e6 DW |
5894 | int rv; |
5895 | struct stat stb; | |
5896 | ||
88654014 LM |
5897 | /* If we are on an RAID enabled platform check that the disk is |
5898 | * attached to the raid controller. | |
5899 | * We do not need to test disks attachment for container based additions, | |
5900 | * they shall be already tested when container was created/assembled. | |
88c32bb1 | 5901 | */ |
d424212e | 5902 | rv = find_intel_hba_capability(fd, super, devname); |
f2f5c343 | 5903 | /* no orom/efi or non-intel hba of the disk */ |
f0f5a016 LM |
5904 | if (rv != 0) { |
5905 | dprintf("capability: %p fd: %d ret: %d\n", | |
5906 | super->orom, fd, rv); | |
5907 | return 1; | |
88c32bb1 DW |
5908 | } |
5909 | ||
f20c3968 DW |
5910 | if (super->current_vol >= 0) |
5911 | return add_to_super_imsm_volume(st, dk, fd, devname); | |
bf5a934a | 5912 | |
c2c087e6 | 5913 | fstat(fd, &stb); |
503975b9 | 5914 | dd = xcalloc(sizeof(*dd), 1); |
c2c087e6 DW |
5915 | dd->major = major(stb.st_rdev); |
5916 | dd->minor = minor(stb.st_rdev); | |
503975b9 | 5917 | dd->devname = devname ? xstrdup(devname) : NULL; |
c2c087e6 | 5918 | dd->fd = fd; |
689c9bf3 | 5919 | dd->e = NULL; |
1a64be56 | 5920 | dd->action = DISK_ADD; |
6da53c0e | 5921 | rv = imsm_read_serial(fd, devname, dd->serial, MAX_RAID_SERIAL_LEN); |
32ba9157 | 5922 | if (rv) { |
e7b84f9d | 5923 | pr_err("failed to retrieve scsi serial, aborting\n"); |
3a85bf0e | 5924 | __free_imsm_disk(dd, 0); |
0030e8d6 | 5925 | abort(); |
c2c087e6 | 5926 | } |
7c798f87 | 5927 | |
20bee0f8 PB |
5928 | if (super->hba && ((super->hba->type == SYS_DEV_NVME) || |
5929 | (super->hba->type == SYS_DEV_VMD))) { | |
5930 | int i; | |
7c798f87 MT |
5931 | char cntrl_path[PATH_MAX]; |
5932 | char *cntrl_name; | |
5933 | char pci_dev_path[PATH_MAX]; | |
20bee0f8 | 5934 | |
7c798f87 MT |
5935 | if (!diskfd_to_devpath(fd, 2, pci_dev_path) || |
5936 | !diskfd_to_devpath(fd, 1, cntrl_path)) { | |
8662f92d | 5937 | pr_err("failed to get dev paths, aborting\n"); |
3a85bf0e | 5938 | __free_imsm_disk(dd, 0); |
a8f3cfd5 MT |
5939 | return 1; |
5940 | } | |
5941 | ||
7c798f87 MT |
5942 | cntrl_name = basename(cntrl_path); |
5943 | if (is_multipath_nvme(fd)) | |
5944 | pr_err("%s controller supports Multi-Path I/O, Intel (R) VROC does not support multipathing\n", | |
5945 | cntrl_name); | |
5946 | ||
5947 | if (devpath_to_vendor(pci_dev_path) == 0x8086) { | |
20bee0f8 PB |
5948 | /* |
5949 | * If Intel's NVMe drive has serial ended with | |
5950 | * "-A","-B","-1" or "-2" it means that this is "x8" | |
5951 | * device (double drive on single PCIe card). | |
5952 | * User should be warned about potential data loss. | |
5953 | */ | |
5954 | for (i = MAX_RAID_SERIAL_LEN-1; i > 0; i--) { | |
5955 | /* Skip empty character at the end */ | |
5956 | if (dd->serial[i] == 0) | |
5957 | continue; | |
5958 | ||
5959 | if (((dd->serial[i] == 'A') || | |
5960 | (dd->serial[i] == 'B') || | |
5961 | (dd->serial[i] == '1') || | |
5962 | (dd->serial[i] == '2')) && | |
5963 | (dd->serial[i-1] == '-')) | |
5964 | pr_err("\tThe action you are about to take may put your data at risk.\n" | |
5965 | "\tPlease note that x8 devices may consist of two separate x4 devices " | |
5966 | "located on a single PCIe port.\n" | |
5967 | "\tRAID 0 is the only supported configuration for this type of x8 device.\n"); | |
5968 | break; | |
5969 | } | |
32716c51 PB |
5970 | } else if (super->hba->type == SYS_DEV_VMD && super->orom && |
5971 | !imsm_orom_has_tpv_support(super->orom)) { | |
5972 | pr_err("\tPlatform configuration does not support non-Intel NVMe drives.\n" | |
8b751247 | 5973 | "\tPlease refer to Intel(R) RSTe/VROC user guide.\n"); |
3a85bf0e | 5974 | __free_imsm_disk(dd, 0); |
32716c51 | 5975 | return 1; |
20bee0f8 PB |
5976 | } |
5977 | } | |
c2c087e6 | 5978 | |
c2c087e6 | 5979 | get_dev_size(fd, NULL, &size); |
3a85bf0e MG |
5980 | if (!get_dev_sector_size(fd, NULL, &member_sector_size)) { |
5981 | __free_imsm_disk(dd, 0); | |
aec01630 | 5982 | return 1; |
3a85bf0e | 5983 | } |
fa7bb6f8 PB |
5984 | |
5985 | if (super->sector_size == 0) { | |
5986 | /* this a first device, so sector_size is not set yet */ | |
5987 | super->sector_size = member_sector_size; | |
fa7bb6f8 PB |
5988 | } |
5989 | ||
71e5411e | 5990 | /* clear migr_rec when adding disk to container */ |
85337573 AO |
5991 | memset(super->migr_rec_buf, 0, MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE); |
5992 | if (lseek64(fd, size - MIGR_REC_SECTOR_POSITION*member_sector_size, | |
de44e46f | 5993 | SEEK_SET) >= 0) { |
466070ad | 5994 | if ((unsigned int)write(fd, super->migr_rec_buf, |
85337573 AO |
5995 | MIGR_REC_BUF_SECTORS*member_sector_size) != |
5996 | MIGR_REC_BUF_SECTORS*member_sector_size) | |
71e5411e PB |
5997 | perror("Write migr_rec failed"); |
5998 | } | |
5999 | ||
c2c087e6 | 6000 | size /= 512; |
1f24f035 | 6001 | serialcpy(dd->disk.serial, dd->serial); |
5551b113 CA |
6002 | set_total_blocks(&dd->disk, size); |
6003 | if (__le32_to_cpu(dd->disk.total_blocks_hi) > 0) { | |
6004 | struct imsm_super *mpb = super->anchor; | |
6005 | mpb->attributes |= MPB_ATTRIB_2TB_DISK; | |
6006 | } | |
a8619d23 | 6007 | mark_spare(dd); |
c2c087e6 | 6008 | if (sysfs_disk_to_scsi_id(fd, &id) == 0) |
b9f594fe | 6009 | dd->disk.scsi_id = __cpu_to_le32(id); |
c2c087e6 | 6010 | else |
b9f594fe | 6011 | dd->disk.scsi_id = __cpu_to_le32(0); |
43dad3d6 DW |
6012 | |
6013 | if (st->update_tail) { | |
1a64be56 LM |
6014 | dd->next = super->disk_mgmt_list; |
6015 | super->disk_mgmt_list = dd; | |
43dad3d6 | 6016 | } else { |
12724c01 TM |
6017 | /* this is called outside of mdmon |
6018 | * write initial spare metadata | |
6019 | * mdmon will overwrite it. | |
6020 | */ | |
43dad3d6 DW |
6021 | dd->next = super->disks; |
6022 | super->disks = dd; | |
12724c01 | 6023 | write_super_imsm_spare(super, dd); |
43dad3d6 | 6024 | } |
f20c3968 DW |
6025 | |
6026 | return 0; | |
cdddbdbc DW |
6027 | } |
6028 | ||
1a64be56 LM |
6029 | static int remove_from_super_imsm(struct supertype *st, mdu_disk_info_t *dk) |
6030 | { | |
6031 | struct intel_super *super = st->sb; | |
6032 | struct dl *dd; | |
6033 | ||
6034 | /* remove from super works only in mdmon - for communication | |
6035 | * manager - monitor. Check if communication memory buffer | |
6036 | * is prepared. | |
6037 | */ | |
6038 | if (!st->update_tail) { | |
1ade5cc1 | 6039 | pr_err("shall be used in mdmon context only\n"); |
1a64be56 LM |
6040 | return 1; |
6041 | } | |
503975b9 | 6042 | dd = xcalloc(1, sizeof(*dd)); |
1a64be56 LM |
6043 | dd->major = dk->major; |
6044 | dd->minor = dk->minor; | |
1a64be56 | 6045 | dd->fd = -1; |
a8619d23 | 6046 | mark_spare(dd); |
1a64be56 LM |
6047 | dd->action = DISK_REMOVE; |
6048 | ||
6049 | dd->next = super->disk_mgmt_list; | |
6050 | super->disk_mgmt_list = dd; | |
6051 | ||
1a64be56 LM |
6052 | return 0; |
6053 | } | |
6054 | ||
f796af5d DW |
6055 | static int store_imsm_mpb(int fd, struct imsm_super *mpb); |
6056 | ||
6057 | static union { | |
f36a9ecd | 6058 | char buf[MAX_SECTOR_SIZE]; |
f796af5d | 6059 | struct imsm_super anchor; |
f36a9ecd | 6060 | } spare_record __attribute__ ((aligned(MAX_SECTOR_SIZE))); |
c2c087e6 | 6061 | |
12724c01 TM |
6062 | |
6063 | static int write_super_imsm_spare(struct intel_super *super, struct dl *d) | |
d23fe947 | 6064 | { |
d23fe947 | 6065 | struct imsm_super *mpb = super->anchor; |
f796af5d | 6066 | struct imsm_super *spare = &spare_record.anchor; |
d23fe947 | 6067 | __u32 sum; |
12724c01 TM |
6068 | |
6069 | if (d->index != -1) | |
6070 | return 1; | |
d23fe947 | 6071 | |
68641cdb JS |
6072 | spare->mpb_size = __cpu_to_le32(sizeof(struct imsm_super)); |
6073 | spare->generation_num = __cpu_to_le32(1UL); | |
f796af5d | 6074 | spare->attributes = MPB_ATTRIB_CHECKSUM_VERIFY; |
68641cdb JS |
6075 | spare->num_disks = 1; |
6076 | spare->num_raid_devs = 0; | |
6077 | spare->cache_size = mpb->cache_size; | |
6078 | spare->pwr_cycle_count = __cpu_to_le32(1); | |
f796af5d DW |
6079 | |
6080 | snprintf((char *) spare->sig, MAX_SIGNATURE_LENGTH, | |
6081 | MPB_SIGNATURE MPB_VERSION_RAID0); | |
d23fe947 | 6082 | |
12724c01 TM |
6083 | spare->disk[0] = d->disk; |
6084 | if (__le32_to_cpu(d->disk.total_blocks_hi) > 0) | |
6085 | spare->attributes |= MPB_ATTRIB_2TB_DISK; | |
6086 | ||
6087 | if (super->sector_size == 4096) | |
6088 | convert_to_4k_imsm_disk(&spare->disk[0]); | |
d23fe947 | 6089 | |
12724c01 TM |
6090 | sum = __gen_imsm_checksum(spare); |
6091 | spare->family_num = __cpu_to_le32(sum); | |
6092 | spare->orig_family_num = 0; | |
6093 | sum = __gen_imsm_checksum(spare); | |
6094 | spare->check_sum = __cpu_to_le32(sum); | |
027c374f | 6095 | |
12724c01 TM |
6096 | if (store_imsm_mpb(d->fd, spare)) { |
6097 | pr_err("failed for device %d:%d %s\n", | |
6098 | d->major, d->minor, strerror(errno)); | |
6099 | return 1; | |
6100 | } | |
6101 | ||
6102 | return 0; | |
6103 | } | |
6104 | /* spare records have their own family number and do not have any defined raid | |
6105 | * devices | |
6106 | */ | |
6107 | static int write_super_imsm_spares(struct intel_super *super, int doclose) | |
6108 | { | |
6109 | struct dl *d; | |
f36a9ecd | 6110 | |
12724c01 TM |
6111 | for (d = super->disks; d; d = d->next) { |
6112 | if (d->index != -1) | |
6113 | continue; | |
d23fe947 | 6114 | |
12724c01 | 6115 | if (write_super_imsm_spare(super, d)) |
e74255d9 | 6116 | return 1; |
12724c01 | 6117 | |
d23fe947 DW |
6118 | if (doclose) { |
6119 | close(d->fd); | |
6120 | d->fd = -1; | |
6121 | } | |
6122 | } | |
6123 | ||
e74255d9 | 6124 | return 0; |
d23fe947 DW |
6125 | } |
6126 | ||
36988a3d | 6127 | static int write_super_imsm(struct supertype *st, int doclose) |
cdddbdbc | 6128 | { |
36988a3d | 6129 | struct intel_super *super = st->sb; |
f36a9ecd | 6130 | unsigned int sector_size = super->sector_size; |
949c47a0 | 6131 | struct imsm_super *mpb = super->anchor; |
c2c087e6 DW |
6132 | struct dl *d; |
6133 | __u32 generation; | |
6134 | __u32 sum; | |
d23fe947 | 6135 | int spares = 0; |
949c47a0 | 6136 | int i; |
a48ac0a8 | 6137 | __u32 mpb_size = sizeof(struct imsm_super) - sizeof(struct imsm_disk); |
36988a3d | 6138 | int num_disks = 0; |
146c6260 | 6139 | int clear_migration_record = 1; |
bbab0940 | 6140 | __u32 bbm_log_size; |
cdddbdbc | 6141 | |
c2c087e6 DW |
6142 | /* 'generation' is incremented everytime the metadata is written */ |
6143 | generation = __le32_to_cpu(mpb->generation_num); | |
6144 | generation++; | |
6145 | mpb->generation_num = __cpu_to_le32(generation); | |
6146 | ||
148acb7b DW |
6147 | /* fix up cases where previous mdadm releases failed to set |
6148 | * orig_family_num | |
6149 | */ | |
6150 | if (mpb->orig_family_num == 0) | |
6151 | mpb->orig_family_num = mpb->family_num; | |
6152 | ||
d23fe947 | 6153 | for (d = super->disks; d; d = d->next) { |
8796fdc4 | 6154 | if (d->index == -1) |
d23fe947 | 6155 | spares++; |
36988a3d | 6156 | else { |
d23fe947 | 6157 | mpb->disk[d->index] = d->disk; |
36988a3d AK |
6158 | num_disks++; |
6159 | } | |
d23fe947 | 6160 | } |
36988a3d | 6161 | for (d = super->missing; d; d = d->next) { |
47ee5a45 | 6162 | mpb->disk[d->index] = d->disk; |
36988a3d AK |
6163 | num_disks++; |
6164 | } | |
6165 | mpb->num_disks = num_disks; | |
6166 | mpb_size += sizeof(struct imsm_disk) * mpb->num_disks; | |
b9f594fe | 6167 | |
949c47a0 DW |
6168 | for (i = 0; i < mpb->num_raid_devs; i++) { |
6169 | struct imsm_dev *dev = __get_imsm_dev(mpb, i); | |
36988a3d AK |
6170 | struct imsm_dev *dev2 = get_imsm_dev(super, i); |
6171 | if (dev && dev2) { | |
6172 | imsm_copy_dev(dev, dev2); | |
6173 | mpb_size += sizeof_imsm_dev(dev, 0); | |
6174 | } | |
146c6260 AK |
6175 | if (is_gen_migration(dev2)) |
6176 | clear_migration_record = 0; | |
949c47a0 | 6177 | } |
bbab0940 TM |
6178 | |
6179 | bbm_log_size = get_imsm_bbm_log_size(super->bbm_log); | |
6180 | ||
6181 | if (bbm_log_size) { | |
6182 | memcpy((void *)mpb + mpb_size, super->bbm_log, bbm_log_size); | |
6183 | mpb->attributes |= MPB_ATTRIB_BBM; | |
6184 | } else | |
6185 | mpb->attributes &= ~MPB_ATTRIB_BBM; | |
6186 | ||
6187 | super->anchor->bbm_log_size = __cpu_to_le32(bbm_log_size); | |
6188 | mpb_size += bbm_log_size; | |
a48ac0a8 | 6189 | mpb->mpb_size = __cpu_to_le32(mpb_size); |
949c47a0 | 6190 | |
bbab0940 TM |
6191 | #ifdef DEBUG |
6192 | assert(super->len == 0 || mpb_size <= super->len); | |
6193 | #endif | |
6194 | ||
c2c087e6 | 6195 | /* recalculate checksum */ |
949c47a0 | 6196 | sum = __gen_imsm_checksum(mpb); |
c2c087e6 DW |
6197 | mpb->check_sum = __cpu_to_le32(sum); |
6198 | ||
51d83f5d AK |
6199 | if (super->clean_migration_record_by_mdmon) { |
6200 | clear_migration_record = 1; | |
6201 | super->clean_migration_record_by_mdmon = 0; | |
6202 | } | |
146c6260 | 6203 | if (clear_migration_record) |
de44e46f | 6204 | memset(super->migr_rec_buf, 0, |
85337573 | 6205 | MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE); |
146c6260 | 6206 | |
f36a9ecd PB |
6207 | if (sector_size == 4096) |
6208 | convert_to_4k(super); | |
6209 | ||
d23fe947 | 6210 | /* write the mpb for disks that compose raid devices */ |
c2c087e6 | 6211 | for (d = super->disks; d ; d = d->next) { |
86c54047 | 6212 | if (d->index < 0 || is_failed(&d->disk)) |
d23fe947 | 6213 | continue; |
30602f53 | 6214 | |
146c6260 AK |
6215 | if (clear_migration_record) { |
6216 | unsigned long long dsize; | |
6217 | ||
6218 | get_dev_size(d->fd, NULL, &dsize); | |
de44e46f PB |
6219 | if (lseek64(d->fd, dsize - sector_size, |
6220 | SEEK_SET) >= 0) { | |
466070ad PB |
6221 | if ((unsigned int)write(d->fd, |
6222 | super->migr_rec_buf, | |
de44e46f PB |
6223 | MIGR_REC_BUF_SECTORS*sector_size) != |
6224 | MIGR_REC_BUF_SECTORS*sector_size) | |
9e2d750d | 6225 | perror("Write migr_rec failed"); |
146c6260 AK |
6226 | } |
6227 | } | |
51d83f5d AK |
6228 | |
6229 | if (store_imsm_mpb(d->fd, mpb)) | |
6230 | fprintf(stderr, | |
1ade5cc1 N |
6231 | "failed for device %d:%d (fd: %d)%s\n", |
6232 | d->major, d->minor, | |
51d83f5d AK |
6233 | d->fd, strerror(errno)); |
6234 | ||
c2c087e6 DW |
6235 | if (doclose) { |
6236 | close(d->fd); | |
6237 | d->fd = -1; | |
6238 | } | |
6239 | } | |
6240 | ||
d23fe947 DW |
6241 | if (spares) |
6242 | return write_super_imsm_spares(super, doclose); | |
6243 | ||
e74255d9 | 6244 | return 0; |
c2c087e6 DW |
6245 | } |
6246 | ||
9b1fb677 | 6247 | static int create_array(struct supertype *st, int dev_idx) |
43dad3d6 DW |
6248 | { |
6249 | size_t len; | |
6250 | struct imsm_update_create_array *u; | |
6251 | struct intel_super *super = st->sb; | |
9b1fb677 | 6252 | struct imsm_dev *dev = get_imsm_dev(super, dev_idx); |
238c0a71 | 6253 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
54c2c1ea DW |
6254 | struct disk_info *inf; |
6255 | struct imsm_disk *disk; | |
6256 | int i; | |
43dad3d6 | 6257 | |
54c2c1ea DW |
6258 | len = sizeof(*u) - sizeof(*dev) + sizeof_imsm_dev(dev, 0) + |
6259 | sizeof(*inf) * map->num_members; | |
503975b9 | 6260 | u = xmalloc(len); |
43dad3d6 | 6261 | u->type = update_create_array; |
9b1fb677 | 6262 | u->dev_idx = dev_idx; |
43dad3d6 | 6263 | imsm_copy_dev(&u->dev, dev); |
54c2c1ea DW |
6264 | inf = get_disk_info(u); |
6265 | for (i = 0; i < map->num_members; i++) { | |
238c0a71 | 6266 | int idx = get_imsm_disk_idx(dev, i, MAP_X); |
9b1fb677 | 6267 | |
54c2c1ea | 6268 | disk = get_imsm_disk(super, idx); |
1ca5c8e0 N |
6269 | if (!disk) |
6270 | disk = get_imsm_missing(super, idx); | |
54c2c1ea DW |
6271 | serialcpy(inf[i].serial, disk->serial); |
6272 | } | |
43dad3d6 DW |
6273 | append_metadata_update(st, u, len); |
6274 | ||
6275 | return 0; | |
6276 | } | |
6277 | ||
1a64be56 | 6278 | static int mgmt_disk(struct supertype *st) |
43dad3d6 DW |
6279 | { |
6280 | struct intel_super *super = st->sb; | |
6281 | size_t len; | |
1a64be56 | 6282 | struct imsm_update_add_remove_disk *u; |
43dad3d6 | 6283 | |
1a64be56 | 6284 | if (!super->disk_mgmt_list) |
43dad3d6 DW |
6285 | return 0; |
6286 | ||
6287 | len = sizeof(*u); | |
503975b9 | 6288 | u = xmalloc(len); |
1a64be56 | 6289 | u->type = update_add_remove_disk; |
43dad3d6 DW |
6290 | append_metadata_update(st, u, len); |
6291 | ||
6292 | return 0; | |
6293 | } | |
2432ce9b AP |
6294 | |
6295 | __u32 crc32c_le(__u32 crc, unsigned char const *p, size_t len); | |
6296 | ||
e397cefe AP |
6297 | static int write_ppl_header(unsigned long long ppl_sector, int fd, void *buf) |
6298 | { | |
6299 | struct ppl_header *ppl_hdr = buf; | |
6300 | int ret; | |
6301 | ||
6302 | ppl_hdr->checksum = __cpu_to_le32(~crc32c_le(~0, buf, PPL_HEADER_SIZE)); | |
6303 | ||
6304 | if (lseek64(fd, ppl_sector * 512, SEEK_SET) < 0) { | |
6305 | ret = -errno; | |
6306 | perror("Failed to seek to PPL header location"); | |
6307 | return ret; | |
6308 | } | |
6309 | ||
6310 | if (write(fd, buf, PPL_HEADER_SIZE) != PPL_HEADER_SIZE) { | |
6311 | ret = -errno; | |
6312 | perror("Write PPL header failed"); | |
6313 | return ret; | |
6314 | } | |
6315 | ||
6316 | fsync(fd); | |
6317 | ||
6318 | return 0; | |
6319 | } | |
6320 | ||
2432ce9b AP |
6321 | static int write_init_ppl_imsm(struct supertype *st, struct mdinfo *info, int fd) |
6322 | { | |
6323 | struct intel_super *super = st->sb; | |
6324 | void *buf; | |
6325 | struct ppl_header *ppl_hdr; | |
6326 | int ret; | |
6327 | ||
b2514242 PB |
6328 | /* first clear entire ppl space */ |
6329 | ret = zero_disk_range(fd, info->ppl_sector, info->ppl_size); | |
6330 | if (ret) | |
6331 | return ret; | |
6332 | ||
6333 | ret = posix_memalign(&buf, MAX_SECTOR_SIZE, PPL_HEADER_SIZE); | |
2432ce9b AP |
6334 | if (ret) { |
6335 | pr_err("Failed to allocate PPL header buffer\n"); | |
e397cefe | 6336 | return -ret; |
2432ce9b AP |
6337 | } |
6338 | ||
6339 | memset(buf, 0, PPL_HEADER_SIZE); | |
6340 | ppl_hdr = buf; | |
6341 | memset(ppl_hdr->reserved, 0xff, PPL_HDR_RESERVED); | |
6342 | ppl_hdr->signature = __cpu_to_le32(super->anchor->orig_family_num); | |
b23d0750 AP |
6343 | |
6344 | if (info->mismatch_cnt) { | |
6345 | /* | |
6346 | * We are overwriting an invalid ppl. Make one entry with wrong | |
6347 | * checksum to prevent the kernel from skipping resync. | |
6348 | */ | |
6349 | ppl_hdr->entries_count = __cpu_to_le32(1); | |
6350 | ppl_hdr->entries[0].checksum = ~0; | |
6351 | } | |
6352 | ||
e397cefe | 6353 | ret = write_ppl_header(info->ppl_sector, fd, buf); |
2432ce9b AP |
6354 | |
6355 | free(buf); | |
6356 | return ret; | |
6357 | } | |
6358 | ||
e397cefe AP |
6359 | static int is_rebuilding(struct imsm_dev *dev); |
6360 | ||
2432ce9b AP |
6361 | static int validate_ppl_imsm(struct supertype *st, struct mdinfo *info, |
6362 | struct mdinfo *disk) | |
6363 | { | |
6364 | struct intel_super *super = st->sb; | |
6365 | struct dl *d; | |
e397cefe | 6366 | void *buf_orig, *buf, *buf_prev = NULL; |
2432ce9b | 6367 | int ret = 0; |
e397cefe | 6368 | struct ppl_header *ppl_hdr = NULL; |
2432ce9b AP |
6369 | __u32 crc; |
6370 | struct imsm_dev *dev; | |
2432ce9b | 6371 | __u32 idx; |
44b6b876 PB |
6372 | unsigned int i; |
6373 | unsigned long long ppl_offset = 0; | |
6374 | unsigned long long prev_gen_num = 0; | |
2432ce9b AP |
6375 | |
6376 | if (disk->disk.raid_disk < 0) | |
6377 | return 0; | |
6378 | ||
2432ce9b | 6379 | dev = get_imsm_dev(super, info->container_member); |
2fc0fc63 | 6380 | idx = get_imsm_disk_idx(dev, disk->disk.raid_disk, MAP_0); |
2432ce9b AP |
6381 | d = get_imsm_dl_disk(super, idx); |
6382 | ||
6383 | if (!d || d->index < 0 || is_failed(&d->disk)) | |
e397cefe AP |
6384 | return 0; |
6385 | ||
6386 | if (posix_memalign(&buf_orig, MAX_SECTOR_SIZE, PPL_HEADER_SIZE * 2)) { | |
6387 | pr_err("Failed to allocate PPL header buffer\n"); | |
6388 | return -1; | |
6389 | } | |
6390 | buf = buf_orig; | |
2432ce9b | 6391 | |
44b6b876 PB |
6392 | ret = 1; |
6393 | while (ppl_offset < MULTIPLE_PPL_AREA_SIZE_IMSM) { | |
e397cefe AP |
6394 | void *tmp; |
6395 | ||
44b6b876 | 6396 | dprintf("Checking potential PPL at offset: %llu\n", ppl_offset); |
2432ce9b | 6397 | |
44b6b876 PB |
6398 | if (lseek64(d->fd, info->ppl_sector * 512 + ppl_offset, |
6399 | SEEK_SET) < 0) { | |
6400 | perror("Failed to seek to PPL header location"); | |
6401 | ret = -1; | |
e397cefe | 6402 | break; |
44b6b876 | 6403 | } |
2432ce9b | 6404 | |
44b6b876 PB |
6405 | if (read(d->fd, buf, PPL_HEADER_SIZE) != PPL_HEADER_SIZE) { |
6406 | perror("Read PPL header failed"); | |
6407 | ret = -1; | |
e397cefe | 6408 | break; |
44b6b876 | 6409 | } |
2432ce9b | 6410 | |
44b6b876 | 6411 | ppl_hdr = buf; |
2432ce9b | 6412 | |
44b6b876 PB |
6413 | crc = __le32_to_cpu(ppl_hdr->checksum); |
6414 | ppl_hdr->checksum = 0; | |
6415 | ||
6416 | if (crc != ~crc32c_le(~0, buf, PPL_HEADER_SIZE)) { | |
6417 | dprintf("Wrong PPL header checksum on %s\n", | |
6418 | d->devname); | |
e397cefe | 6419 | break; |
44b6b876 PB |
6420 | } |
6421 | ||
6422 | if (prev_gen_num > __le64_to_cpu(ppl_hdr->generation)) { | |
6423 | /* previous was newest, it was already checked */ | |
e397cefe | 6424 | break; |
44b6b876 PB |
6425 | } |
6426 | ||
6427 | if ((__le32_to_cpu(ppl_hdr->signature) != | |
6428 | super->anchor->orig_family_num)) { | |
6429 | dprintf("Wrong PPL header signature on %s\n", | |
6430 | d->devname); | |
6431 | ret = 1; | |
e397cefe | 6432 | break; |
44b6b876 PB |
6433 | } |
6434 | ||
6435 | ret = 0; | |
6436 | prev_gen_num = __le64_to_cpu(ppl_hdr->generation); | |
2432ce9b | 6437 | |
44b6b876 PB |
6438 | ppl_offset += PPL_HEADER_SIZE; |
6439 | for (i = 0; i < __le32_to_cpu(ppl_hdr->entries_count); i++) | |
6440 | ppl_offset += | |
6441 | __le32_to_cpu(ppl_hdr->entries[i].pp_size); | |
e397cefe AP |
6442 | |
6443 | if (!buf_prev) | |
6444 | buf_prev = buf + PPL_HEADER_SIZE; | |
6445 | tmp = buf_prev; | |
6446 | buf_prev = buf; | |
6447 | buf = tmp; | |
2432ce9b AP |
6448 | } |
6449 | ||
e397cefe AP |
6450 | if (buf_prev) { |
6451 | buf = buf_prev; | |
6452 | ppl_hdr = buf_prev; | |
6453 | } | |
2432ce9b | 6454 | |
54148aba PB |
6455 | /* |
6456 | * Update metadata to use mutliple PPLs area (1MB). | |
6457 | * This is done once for all RAID members | |
6458 | */ | |
6459 | if (info->consistency_policy == CONSISTENCY_POLICY_PPL && | |
6460 | info->ppl_size != (MULTIPLE_PPL_AREA_SIZE_IMSM >> 9)) { | |
6461 | char subarray[20]; | |
6462 | struct mdinfo *member_dev; | |
6463 | ||
6464 | sprintf(subarray, "%d", info->container_member); | |
6465 | ||
6466 | if (mdmon_running(st->container_devnm)) | |
6467 | st->update_tail = &st->updates; | |
6468 | ||
6469 | if (st->ss->update_subarray(st, subarray, "ppl", NULL)) { | |
6470 | pr_err("Failed to update subarray %s\n", | |
6471 | subarray); | |
6472 | } else { | |
6473 | if (st->update_tail) | |
6474 | flush_metadata_updates(st); | |
6475 | else | |
6476 | st->ss->sync_metadata(st); | |
6477 | info->ppl_size = (MULTIPLE_PPL_AREA_SIZE_IMSM >> 9); | |
6478 | for (member_dev = info->devs; member_dev; | |
6479 | member_dev = member_dev->next) | |
6480 | member_dev->ppl_size = | |
6481 | (MULTIPLE_PPL_AREA_SIZE_IMSM >> 9); | |
6482 | } | |
6483 | } | |
6484 | ||
b23d0750 | 6485 | if (ret == 1) { |
2fc0fc63 AP |
6486 | struct imsm_map *map = get_imsm_map(dev, MAP_X); |
6487 | ||
50b9c10d PB |
6488 | if (map->map_state == IMSM_T_STATE_UNINITIALIZED || |
6489 | (map->map_state == IMSM_T_STATE_NORMAL && | |
2ec9d182 | 6490 | !(dev->vol.dirty & RAIDVOL_DIRTY)) || |
e397cefe | 6491 | (is_rebuilding(dev) && |
4036e7ee | 6492 | vol_curr_migr_unit(dev) == 0 && |
2ec9d182 | 6493 | get_imsm_disk_idx(dev, disk->disk.raid_disk, MAP_1) != idx)) |
b23d0750 AP |
6494 | ret = st->ss->write_init_ppl(st, info, d->fd); |
6495 | else | |
6496 | info->mismatch_cnt++; | |
e397cefe AP |
6497 | } else if (ret == 0 && |
6498 | ppl_hdr->entries_count == 0 && | |
6499 | is_rebuilding(dev) && | |
6500 | info->resync_start == 0) { | |
6501 | /* | |
6502 | * The header has no entries - add a single empty entry and | |
6503 | * rewrite the header to prevent the kernel from going into | |
6504 | * resync after an interrupted rebuild. | |
6505 | */ | |
6506 | ppl_hdr->entries_count = __cpu_to_le32(1); | |
6507 | ret = write_ppl_header(info->ppl_sector, d->fd, buf); | |
b23d0750 | 6508 | } |
2432ce9b | 6509 | |
e397cefe AP |
6510 | free(buf_orig); |
6511 | ||
2432ce9b AP |
6512 | return ret; |
6513 | } | |
6514 | ||
2432ce9b AP |
6515 | static int write_init_ppl_imsm_all(struct supertype *st, struct mdinfo *info) |
6516 | { | |
6517 | struct intel_super *super = st->sb; | |
6518 | struct dl *d; | |
6519 | int ret = 0; | |
6520 | ||
6521 | if (info->consistency_policy != CONSISTENCY_POLICY_PPL || | |
6522 | info->array.level != 5) | |
6523 | return 0; | |
6524 | ||
6525 | for (d = super->disks; d ; d = d->next) { | |
6526 | if (d->index < 0 || is_failed(&d->disk)) | |
6527 | continue; | |
6528 | ||
6529 | ret = st->ss->write_init_ppl(st, info, d->fd); | |
6530 | if (ret) | |
6531 | break; | |
6532 | } | |
6533 | ||
6534 | return ret; | |
6535 | } | |
43dad3d6 | 6536 | |
fbc42556 JR |
6537 | /******************************************************************************* |
6538 | * Function: write_init_bitmap_imsm_vol | |
6539 | * Description: Write a bitmap header and prepares the area for the bitmap. | |
6540 | * Parameters: | |
6541 | * st : supertype information | |
6542 | * vol_idx : the volume index to use | |
6543 | * | |
6544 | * Returns: | |
6545 | * 0 : success | |
6546 | * -1 : fail | |
6547 | ******************************************************************************/ | |
6548 | static int write_init_bitmap_imsm_vol(struct supertype *st, int vol_idx) | |
6549 | { | |
6550 | struct intel_super *super = st->sb; | |
6551 | int prev_current_vol = super->current_vol; | |
6552 | struct dl *d; | |
6553 | int ret = 0; | |
6554 | ||
6555 | super->current_vol = vol_idx; | |
6556 | for (d = super->disks; d; d = d->next) { | |
6557 | if (d->index < 0 || is_failed(&d->disk)) | |
6558 | continue; | |
6559 | ret = st->ss->write_bitmap(st, d->fd, NoUpdate); | |
6560 | if (ret) | |
6561 | break; | |
6562 | } | |
6563 | super->current_vol = prev_current_vol; | |
6564 | return ret; | |
6565 | } | |
6566 | ||
6567 | /******************************************************************************* | |
6568 | * Function: write_init_bitmap_imsm_all | |
6569 | * Description: Write a bitmap header and prepares the area for the bitmap. | |
6570 | * Operation is executed for volumes with CONSISTENCY_POLICY_BITMAP. | |
6571 | * Parameters: | |
6572 | * st : supertype information | |
6573 | * info : info about the volume where the bitmap should be written | |
6574 | * vol_idx : the volume index to use | |
6575 | * | |
6576 | * Returns: | |
6577 | * 0 : success | |
6578 | * -1 : fail | |
6579 | ******************************************************************************/ | |
6580 | static int write_init_bitmap_imsm_all(struct supertype *st, struct mdinfo *info, | |
6581 | int vol_idx) | |
6582 | { | |
6583 | int ret = 0; | |
6584 | ||
6585 | if (info && (info->consistency_policy == CONSISTENCY_POLICY_BITMAP)) | |
6586 | ret = write_init_bitmap_imsm_vol(st, vol_idx); | |
6587 | ||
6588 | return ret; | |
6589 | } | |
6590 | ||
c2c087e6 DW |
6591 | static int write_init_super_imsm(struct supertype *st) |
6592 | { | |
9b1fb677 DW |
6593 | struct intel_super *super = st->sb; |
6594 | int current_vol = super->current_vol; | |
2432ce9b AP |
6595 | int rv = 0; |
6596 | struct mdinfo info; | |
6597 | ||
6598 | getinfo_super_imsm(st, &info, NULL); | |
9b1fb677 DW |
6599 | |
6600 | /* we are done with current_vol reset it to point st at the container */ | |
6601 | super->current_vol = -1; | |
6602 | ||
8273f55e | 6603 | if (st->update_tail) { |
43dad3d6 DW |
6604 | /* queue the recently created array / added disk |
6605 | * as a metadata update */ | |
8273f55e | 6606 | |
43dad3d6 | 6607 | /* determine if we are creating a volume or adding a disk */ |
9b1fb677 | 6608 | if (current_vol < 0) { |
1a64be56 LM |
6609 | /* in the mgmt (add/remove) disk case we are running |
6610 | * in mdmon context, so don't close fd's | |
43dad3d6 | 6611 | */ |
2432ce9b AP |
6612 | rv = mgmt_disk(st); |
6613 | } else { | |
fbc42556 | 6614 | /* adding the second volume to the array */ |
2432ce9b | 6615 | rv = write_init_ppl_imsm_all(st, &info); |
fbc42556 JR |
6616 | if (!rv) |
6617 | rv = write_init_bitmap_imsm_all(st, &info, current_vol); | |
2432ce9b AP |
6618 | if (!rv) |
6619 | rv = create_array(st, current_vol); | |
6620 | } | |
d682f344 N |
6621 | } else { |
6622 | struct dl *d; | |
6623 | for (d = super->disks; d; d = d->next) | |
ba728be7 | 6624 | Kill(d->devname, NULL, 0, -1, 1); |
fbc42556 | 6625 | if (current_vol >= 0) { |
2432ce9b | 6626 | rv = write_init_ppl_imsm_all(st, &info); |
fbc42556 JR |
6627 | if (!rv) |
6628 | rv = write_init_bitmap_imsm_all(st, &info, current_vol); | |
6629 | } | |
6630 | ||
2432ce9b AP |
6631 | if (!rv) |
6632 | rv = write_super_imsm(st, 1); | |
d682f344 | 6633 | } |
2432ce9b AP |
6634 | |
6635 | return rv; | |
cdddbdbc DW |
6636 | } |
6637 | ||
e683ca88 | 6638 | static int store_super_imsm(struct supertype *st, int fd) |
cdddbdbc | 6639 | { |
e683ca88 DW |
6640 | struct intel_super *super = st->sb; |
6641 | struct imsm_super *mpb = super ? super->anchor : NULL; | |
551c80c1 | 6642 | |
e683ca88 | 6643 | if (!mpb) |
ad97895e DW |
6644 | return 1; |
6645 | ||
f36a9ecd PB |
6646 | if (super->sector_size == 4096) |
6647 | convert_to_4k(super); | |
e683ca88 | 6648 | return store_imsm_mpb(fd, mpb); |
cdddbdbc DW |
6649 | } |
6650 | ||
cdddbdbc | 6651 | static int validate_geometry_imsm_container(struct supertype *st, int level, |
1f5d54a0 | 6652 | int raiddisks, |
af4348dd N |
6653 | unsigned long long data_offset, |
6654 | char *dev, | |
2c514b71 NB |
6655 | unsigned long long *freesize, |
6656 | int verbose) | |
cdddbdbc | 6657 | { |
c2c087e6 DW |
6658 | int fd; |
6659 | unsigned long long ldsize; | |
8662f92d | 6660 | struct intel_super *super = NULL; |
f2f5c343 | 6661 | int rv = 0; |
cdddbdbc | 6662 | |
c2c087e6 DW |
6663 | if (level != LEVEL_CONTAINER) |
6664 | return 0; | |
6665 | if (!dev) | |
6666 | return 1; | |
6667 | ||
dca80fcd | 6668 | fd = dev_open(dev, O_RDONLY|O_EXCL); |
c2c087e6 | 6669 | if (fd < 0) { |
ba728be7 | 6670 | if (verbose > 0) |
e7b84f9d | 6671 | pr_err("imsm: Cannot open %s: %s\n", |
2c514b71 | 6672 | dev, strerror(errno)); |
c2c087e6 DW |
6673 | return 0; |
6674 | } | |
8662f92d MT |
6675 | if (!get_dev_size(fd, dev, &ldsize)) |
6676 | goto exit; | |
f2f5c343 LM |
6677 | |
6678 | /* capabilities retrieve could be possible | |
6679 | * note that there is no fd for the disks in array. | |
6680 | */ | |
6681 | super = alloc_super(); | |
8662f92d MT |
6682 | if (!super) |
6683 | goto exit; | |
6684 | ||
6685 | if (!get_dev_sector_size(fd, NULL, &super->sector_size)) | |
6686 | goto exit; | |
fa7bb6f8 | 6687 | |
ba728be7 | 6688 | rv = find_intel_hba_capability(fd, super, verbose > 0 ? dev : NULL); |
f2f5c343 LM |
6689 | if (rv != 0) { |
6690 | #if DEBUG | |
6691 | char str[256]; | |
6692 | fd2devname(fd, str); | |
1ade5cc1 | 6693 | dprintf("fd: %d %s orom: %p rv: %d raiddisk: %d\n", |
f2f5c343 LM |
6694 | fd, str, super->orom, rv, raiddisks); |
6695 | #endif | |
6696 | /* no orom/efi or non-intel hba of the disk */ | |
8662f92d MT |
6697 | rv = 0; |
6698 | goto exit; | |
f2f5c343 | 6699 | } |
9126b9a8 CA |
6700 | if (super->orom) { |
6701 | if (raiddisks > super->orom->tds) { | |
6702 | if (verbose) | |
7a862a02 | 6703 | pr_err("%d exceeds maximum number of platform supported disks: %d\n", |
9126b9a8 | 6704 | raiddisks, super->orom->tds); |
8662f92d | 6705 | goto exit; |
9126b9a8 CA |
6706 | } |
6707 | if ((super->orom->attr & IMSM_OROM_ATTR_2TB_DISK) == 0 && | |
6708 | (ldsize >> 9) >> 32 > 0) { | |
6709 | if (verbose) | |
e7b84f9d | 6710 | pr_err("%s exceeds maximum platform supported size\n", dev); |
8662f92d MT |
6711 | goto exit; |
6712 | } | |
6713 | ||
6714 | if (super->hba->type == SYS_DEV_VMD || | |
6715 | super->hba->type == SYS_DEV_NVME) { | |
6716 | if (!imsm_is_nvme_namespace_supported(fd, 1)) { | |
6717 | if (verbose) | |
6718 | pr_err("NVMe namespace %s is not supported by IMSM\n", | |
6719 | basename(dev)); | |
6720 | goto exit; | |
6721 | } | |
9126b9a8 | 6722 | } |
f2f5c343 | 6723 | } |
1f5d54a0 MT |
6724 | if (freesize) |
6725 | *freesize = avail_size_imsm(st, ldsize >> 9, data_offset); | |
8662f92d MT |
6726 | rv = 1; |
6727 | exit: | |
6728 | if (super) | |
6729 | free_imsm(super); | |
6730 | close(fd); | |
c2c087e6 | 6731 | |
8662f92d | 6732 | return rv; |
cdddbdbc DW |
6733 | } |
6734 | ||
0dcecb2e DW |
6735 | static unsigned long long find_size(struct extent *e, int *idx, int num_extents) |
6736 | { | |
6737 | const unsigned long long base_start = e[*idx].start; | |
6738 | unsigned long long end = base_start + e[*idx].size; | |
6739 | int i; | |
6740 | ||
6741 | if (base_start == end) | |
6742 | return 0; | |
6743 | ||
6744 | *idx = *idx + 1; | |
6745 | for (i = *idx; i < num_extents; i++) { | |
6746 | /* extend overlapping extents */ | |
6747 | if (e[i].start >= base_start && | |
6748 | e[i].start <= end) { | |
6749 | if (e[i].size == 0) | |
6750 | return 0; | |
6751 | if (e[i].start + e[i].size > end) | |
6752 | end = e[i].start + e[i].size; | |
6753 | } else if (e[i].start > end) { | |
6754 | *idx = i; | |
6755 | break; | |
6756 | } | |
6757 | } | |
6758 | ||
6759 | return end - base_start; | |
6760 | } | |
6761 | ||
6762 | static unsigned long long merge_extents(struct intel_super *super, int sum_extents) | |
6763 | { | |
6764 | /* build a composite disk with all known extents and generate a new | |
6765 | * 'maxsize' given the "all disks in an array must share a common start | |
6766 | * offset" constraint | |
6767 | */ | |
503975b9 | 6768 | struct extent *e = xcalloc(sum_extents, sizeof(*e)); |
0dcecb2e DW |
6769 | struct dl *dl; |
6770 | int i, j; | |
6771 | int start_extent; | |
6772 | unsigned long long pos; | |
b9d77223 | 6773 | unsigned long long start = 0; |
0dcecb2e DW |
6774 | unsigned long long maxsize; |
6775 | unsigned long reserve; | |
6776 | ||
0dcecb2e DW |
6777 | /* coalesce and sort all extents. also, check to see if we need to |
6778 | * reserve space between member arrays | |
6779 | */ | |
6780 | j = 0; | |
6781 | for (dl = super->disks; dl; dl = dl->next) { | |
6782 | if (!dl->e) | |
6783 | continue; | |
6784 | for (i = 0; i < dl->extent_cnt; i++) | |
6785 | e[j++] = dl->e[i]; | |
6786 | } | |
6787 | qsort(e, sum_extents, sizeof(*e), cmp_extent); | |
6788 | ||
6789 | /* merge extents */ | |
6790 | i = 0; | |
6791 | j = 0; | |
6792 | while (i < sum_extents) { | |
6793 | e[j].start = e[i].start; | |
6794 | e[j].size = find_size(e, &i, sum_extents); | |
6795 | j++; | |
6796 | if (e[j-1].size == 0) | |
6797 | break; | |
6798 | } | |
6799 | ||
6800 | pos = 0; | |
6801 | maxsize = 0; | |
6802 | start_extent = 0; | |
6803 | i = 0; | |
6804 | do { | |
6805 | unsigned long long esize; | |
6806 | ||
6807 | esize = e[i].start - pos; | |
6808 | if (esize >= maxsize) { | |
6809 | maxsize = esize; | |
6810 | start = pos; | |
6811 | start_extent = i; | |
6812 | } | |
6813 | pos = e[i].start + e[i].size; | |
6814 | i++; | |
6815 | } while (e[i-1].size); | |
6816 | free(e); | |
6817 | ||
a7dd165b DW |
6818 | if (maxsize == 0) |
6819 | return 0; | |
6820 | ||
6821 | /* FIXME assumes volume at offset 0 is the first volume in a | |
6822 | * container | |
6823 | */ | |
0dcecb2e DW |
6824 | if (start_extent > 0) |
6825 | reserve = IMSM_RESERVED_SECTORS; /* gap between raid regions */ | |
6826 | else | |
6827 | reserve = 0; | |
6828 | ||
6829 | if (maxsize < reserve) | |
a7dd165b | 6830 | return 0; |
0dcecb2e | 6831 | |
5551b113 | 6832 | super->create_offset = ~((unsigned long long) 0); |
0dcecb2e | 6833 | if (start + reserve > super->create_offset) |
a7dd165b | 6834 | return 0; /* start overflows create_offset */ |
0dcecb2e DW |
6835 | super->create_offset = start + reserve; |
6836 | ||
6837 | return maxsize - reserve; | |
6838 | } | |
6839 | ||
88c32bb1 DW |
6840 | static int is_raid_level_supported(const struct imsm_orom *orom, int level, int raiddisks) |
6841 | { | |
6842 | if (level < 0 || level == 6 || level == 4) | |
6843 | return 0; | |
6844 | ||
6845 | /* if we have an orom prevent invalid raid levels */ | |
6846 | if (orom) | |
6847 | switch (level) { | |
6848 | case 0: return imsm_orom_has_raid0(orom); | |
6849 | case 1: | |
6850 | if (raiddisks > 2) | |
6851 | return imsm_orom_has_raid1e(orom); | |
1c556e92 DW |
6852 | return imsm_orom_has_raid1(orom) && raiddisks == 2; |
6853 | case 10: return imsm_orom_has_raid10(orom) && raiddisks == 4; | |
6854 | case 5: return imsm_orom_has_raid5(orom) && raiddisks > 2; | |
88c32bb1 DW |
6855 | } |
6856 | else | |
6857 | return 1; /* not on an Intel RAID platform so anything goes */ | |
6858 | ||
6859 | return 0; | |
6860 | } | |
6861 | ||
ca9de185 LM |
6862 | static int |
6863 | active_arrays_by_format(char *name, char* hba, struct md_list **devlist, | |
6864 | int dpa, int verbose) | |
6865 | { | |
6866 | struct mdstat_ent *mdstat = mdstat_read(0, 0); | |
594dc1b8 | 6867 | struct mdstat_ent *memb; |
ca9de185 LM |
6868 | int count = 0; |
6869 | int num = 0; | |
594dc1b8 | 6870 | struct md_list *dv; |
ca9de185 LM |
6871 | int found; |
6872 | ||
6873 | for (memb = mdstat ; memb ; memb = memb->next) { | |
6874 | if (memb->metadata_version && | |
fc54fe7a | 6875 | (strncmp(memb->metadata_version, "external:", 9) == 0) && |
ca9de185 LM |
6876 | (strcmp(&memb->metadata_version[9], name) == 0) && |
6877 | !is_subarray(memb->metadata_version+9) && | |
6878 | memb->members) { | |
6879 | struct dev_member *dev = memb->members; | |
6880 | int fd = -1; | |
6881 | while(dev && (fd < 0)) { | |
503975b9 N |
6882 | char *path = xmalloc(strlen(dev->name) + strlen("/dev/") + 1); |
6883 | num = sprintf(path, "%s%s", "/dev/", dev->name); | |
6884 | if (num > 0) | |
6885 | fd = open(path, O_RDONLY, 0); | |
089f9d79 | 6886 | if (num <= 0 || fd < 0) { |
676e87a8 | 6887 | pr_vrb("Cannot open %s: %s\n", |
503975b9 | 6888 | dev->name, strerror(errno)); |
ca9de185 | 6889 | } |
503975b9 | 6890 | free(path); |
ca9de185 LM |
6891 | dev = dev->next; |
6892 | } | |
6893 | found = 0; | |
089f9d79 | 6894 | if (fd >= 0 && disk_attached_to_hba(fd, hba)) { |
ca9de185 LM |
6895 | struct mdstat_ent *vol; |
6896 | for (vol = mdstat ; vol ; vol = vol->next) { | |
089f9d79 | 6897 | if (vol->active > 0 && |
ca9de185 | 6898 | vol->metadata_version && |
9581efb1 | 6899 | is_container_member(vol, memb->devnm)) { |
ca9de185 LM |
6900 | found++; |
6901 | count++; | |
6902 | } | |
6903 | } | |
6904 | if (*devlist && (found < dpa)) { | |
503975b9 | 6905 | dv = xcalloc(1, sizeof(*dv)); |
9581efb1 N |
6906 | dv->devname = xmalloc(strlen(memb->devnm) + strlen("/dev/") + 1); |
6907 | sprintf(dv->devname, "%s%s", "/dev/", memb->devnm); | |
503975b9 N |
6908 | dv->found = found; |
6909 | dv->used = 0; | |
6910 | dv->next = *devlist; | |
6911 | *devlist = dv; | |
ca9de185 LM |
6912 | } |
6913 | } | |
6914 | if (fd >= 0) | |
6915 | close(fd); | |
6916 | } | |
6917 | } | |
6918 | free_mdstat(mdstat); | |
6919 | return count; | |
6920 | } | |
6921 | ||
6922 | #ifdef DEBUG_LOOP | |
6923 | static struct md_list* | |
6924 | get_loop_devices(void) | |
6925 | { | |
6926 | int i; | |
6927 | struct md_list *devlist = NULL; | |
594dc1b8 | 6928 | struct md_list *dv; |
ca9de185 LM |
6929 | |
6930 | for(i = 0; i < 12; i++) { | |
503975b9 N |
6931 | dv = xcalloc(1, sizeof(*dv)); |
6932 | dv->devname = xmalloc(40); | |
ca9de185 LM |
6933 | sprintf(dv->devname, "/dev/loop%d", i); |
6934 | dv->next = devlist; | |
6935 | devlist = dv; | |
6936 | } | |
6937 | return devlist; | |
6938 | } | |
6939 | #endif | |
6940 | ||
6941 | static struct md_list* | |
6942 | get_devices(const char *hba_path) | |
6943 | { | |
6944 | struct md_list *devlist = NULL; | |
594dc1b8 | 6945 | struct md_list *dv; |
ca9de185 LM |
6946 | struct dirent *ent; |
6947 | DIR *dir; | |
6948 | int err = 0; | |
6949 | ||
6950 | #if DEBUG_LOOP | |
6951 | devlist = get_loop_devices(); | |
6952 | return devlist; | |
6953 | #endif | |
6954 | /* scroll through /sys/dev/block looking for devices attached to | |
6955 | * this hba | |
6956 | */ | |
6957 | dir = opendir("/sys/dev/block"); | |
6958 | for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) { | |
6959 | int fd; | |
6960 | char buf[1024]; | |
6961 | int major, minor; | |
6962 | char *path = NULL; | |
6963 | if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2) | |
6964 | continue; | |
7c798f87 | 6965 | path = devt_to_devpath(makedev(major, minor), 1, NULL); |
ca9de185 LM |
6966 | if (!path) |
6967 | continue; | |
6968 | if (!path_attached_to_hba(path, hba_path)) { | |
6969 | free(path); | |
6970 | path = NULL; | |
6971 | continue; | |
6972 | } | |
6973 | free(path); | |
6974 | path = NULL; | |
6975 | fd = dev_open(ent->d_name, O_RDONLY); | |
6976 | if (fd >= 0) { | |
6977 | fd2devname(fd, buf); | |
6978 | close(fd); | |
6979 | } else { | |
e7b84f9d | 6980 | pr_err("cannot open device: %s\n", |
ca9de185 LM |
6981 | ent->d_name); |
6982 | continue; | |
6983 | } | |
6984 | ||
503975b9 N |
6985 | dv = xcalloc(1, sizeof(*dv)); |
6986 | dv->devname = xstrdup(buf); | |
ca9de185 LM |
6987 | dv->next = devlist; |
6988 | devlist = dv; | |
6989 | } | |
6990 | if (err) { | |
6991 | while(devlist) { | |
6992 | dv = devlist; | |
6993 | devlist = devlist->next; | |
6994 | free(dv->devname); | |
6995 | free(dv); | |
6996 | } | |
6997 | } | |
562aa102 | 6998 | closedir(dir); |
ca9de185 LM |
6999 | return devlist; |
7000 | } | |
7001 | ||
7002 | static int | |
7003 | count_volumes_list(struct md_list *devlist, char *homehost, | |
7004 | int verbose, int *found) | |
7005 | { | |
7006 | struct md_list *tmpdev; | |
7007 | int count = 0; | |
594dc1b8 | 7008 | struct supertype *st; |
ca9de185 LM |
7009 | |
7010 | /* first walk the list of devices to find a consistent set | |
7011 | * that match the criterea, if that is possible. | |
7012 | * We flag the ones we like with 'used'. | |
7013 | */ | |
7014 | *found = 0; | |
7015 | st = match_metadata_desc_imsm("imsm"); | |
7016 | if (st == NULL) { | |
676e87a8 | 7017 | pr_vrb("cannot allocate memory for imsm supertype\n"); |
ca9de185 LM |
7018 | return 0; |
7019 | } | |
7020 | ||
7021 | for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) { | |
7022 | char *devname = tmpdev->devname; | |
0a6bff09 | 7023 | dev_t rdev; |
ca9de185 LM |
7024 | struct supertype *tst; |
7025 | int dfd; | |
7026 | if (tmpdev->used > 1) | |
7027 | continue; | |
7028 | tst = dup_super(st); | |
7029 | if (tst == NULL) { | |
676e87a8 | 7030 | pr_vrb("cannot allocate memory for imsm supertype\n"); |
ca9de185 LM |
7031 | goto err_1; |
7032 | } | |
7033 | tmpdev->container = 0; | |
7034 | dfd = dev_open(devname, O_RDONLY|O_EXCL); | |
7035 | if (dfd < 0) { | |
1ade5cc1 | 7036 | dprintf("cannot open device %s: %s\n", |
ca9de185 LM |
7037 | devname, strerror(errno)); |
7038 | tmpdev->used = 2; | |
0a6bff09 | 7039 | } else if (!fstat_is_blkdev(dfd, devname, &rdev)) { |
ca9de185 LM |
7040 | tmpdev->used = 2; |
7041 | } else if (must_be_container(dfd)) { | |
7042 | struct supertype *cst; | |
7043 | cst = super_by_fd(dfd, NULL); | |
7044 | if (cst == NULL) { | |
1ade5cc1 | 7045 | dprintf("cannot recognize container type %s\n", |
ca9de185 LM |
7046 | devname); |
7047 | tmpdev->used = 2; | |
7048 | } else if (tst->ss != st->ss) { | |
1ade5cc1 | 7049 | dprintf("non-imsm container - ignore it: %s\n", |
ca9de185 LM |
7050 | devname); |
7051 | tmpdev->used = 2; | |
7052 | } else if (!tst->ss->load_container || | |
7053 | tst->ss->load_container(tst, dfd, NULL)) | |
7054 | tmpdev->used = 2; | |
7055 | else { | |
7056 | tmpdev->container = 1; | |
7057 | } | |
7058 | if (cst) | |
7059 | cst->ss->free_super(cst); | |
7060 | } else { | |
0a6bff09 | 7061 | tmpdev->st_rdev = rdev; |
ca9de185 | 7062 | if (tst->ss->load_super(tst,dfd, NULL)) { |
1ade5cc1 | 7063 | dprintf("no RAID superblock on %s\n", |
ca9de185 LM |
7064 | devname); |
7065 | tmpdev->used = 2; | |
7066 | } else if (tst->ss->compare_super == NULL) { | |
1ade5cc1 | 7067 | dprintf("Cannot assemble %s metadata on %s\n", |
ca9de185 LM |
7068 | tst->ss->name, devname); |
7069 | tmpdev->used = 2; | |
7070 | } | |
7071 | } | |
7072 | if (dfd >= 0) | |
7073 | close(dfd); | |
7074 | if (tmpdev->used == 2 || tmpdev->used == 4) { | |
7075 | /* Ignore unrecognised devices during auto-assembly */ | |
7076 | goto loop; | |
7077 | } | |
7078 | else { | |
7079 | struct mdinfo info; | |
7080 | tst->ss->getinfo_super(tst, &info, NULL); | |
7081 | ||
7082 | if (st->minor_version == -1) | |
7083 | st->minor_version = tst->minor_version; | |
7084 | ||
7085 | if (memcmp(info.uuid, uuid_zero, | |
7086 | sizeof(int[4])) == 0) { | |
7087 | /* this is a floating spare. It cannot define | |
7088 | * an array unless there are no more arrays of | |
7089 | * this type to be found. It can be included | |
7090 | * in an array of this type though. | |
7091 | */ | |
7092 | tmpdev->used = 3; | |
7093 | goto loop; | |
7094 | } | |
7095 | ||
7096 | if (st->ss != tst->ss || | |
7097 | st->minor_version != tst->minor_version || | |
c7b8547c | 7098 | st->ss->compare_super(st, tst, 1) != 0) { |
ca9de185 LM |
7099 | /* Some mismatch. If exactly one array matches this host, |
7100 | * we can resolve on that one. | |
7101 | * Or, if we are auto assembling, we just ignore the second | |
7102 | * for now. | |
7103 | */ | |
1ade5cc1 | 7104 | dprintf("superblock on %s doesn't match others - assembly aborted\n", |
ca9de185 LM |
7105 | devname); |
7106 | goto loop; | |
7107 | } | |
7108 | tmpdev->used = 1; | |
7109 | *found = 1; | |
7110 | dprintf("found: devname: %s\n", devname); | |
7111 | } | |
7112 | loop: | |
7113 | if (tst) | |
7114 | tst->ss->free_super(tst); | |
7115 | } | |
7116 | if (*found != 0) { | |
7117 | int err; | |
7118 | if ((err = load_super_imsm_all(st, -1, &st->sb, NULL, devlist, 0)) == 0) { | |
7119 | struct mdinfo *iter, *head = st->ss->container_content(st, NULL); | |
7120 | for (iter = head; iter; iter = iter->next) { | |
7121 | dprintf("content->text_version: %s vol\n", | |
7122 | iter->text_version); | |
7123 | if (iter->array.state & (1<<MD_SB_BLOCK_VOLUME)) { | |
7124 | /* do not assemble arrays with unsupported | |
7125 | configurations */ | |
1ade5cc1 | 7126 | dprintf("Cannot activate member %s.\n", |
ca9de185 LM |
7127 | iter->text_version); |
7128 | } else | |
7129 | count++; | |
7130 | } | |
7131 | sysfs_free(head); | |
7132 | ||
7133 | } else { | |
1ade5cc1 | 7134 | dprintf("No valid super block on device list: err: %d %p\n", |
ca9de185 LM |
7135 | err, st->sb); |
7136 | } | |
7137 | } else { | |
1ade5cc1 | 7138 | dprintf("no more devices to examine\n"); |
ca9de185 LM |
7139 | } |
7140 | ||
7141 | for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) { | |
089f9d79 | 7142 | if (tmpdev->used == 1 && tmpdev->found) { |
ca9de185 LM |
7143 | if (count) { |
7144 | if (count < tmpdev->found) | |
7145 | count = 0; | |
7146 | else | |
7147 | count -= tmpdev->found; | |
7148 | } | |
7149 | } | |
7150 | if (tmpdev->used == 1) | |
7151 | tmpdev->used = 4; | |
7152 | } | |
7153 | err_1: | |
7154 | if (st) | |
7155 | st->ss->free_super(st); | |
7156 | return count; | |
7157 | } | |
7158 | ||
d3c11416 AO |
7159 | static int __count_volumes(char *hba_path, int dpa, int verbose, |
7160 | int cmp_hba_path) | |
ca9de185 | 7161 | { |
72a45777 | 7162 | struct sys_dev *idev, *intel_devices = find_intel_devices(); |
ca9de185 | 7163 | int count = 0; |
72a45777 PB |
7164 | const struct orom_entry *entry; |
7165 | struct devid_list *dv, *devid_list; | |
ca9de185 | 7166 | |
d3c11416 | 7167 | if (!hba_path) |
ca9de185 LM |
7168 | return 0; |
7169 | ||
72a45777 | 7170 | for (idev = intel_devices; idev; idev = idev->next) { |
d3c11416 AO |
7171 | if (strstr(idev->path, hba_path)) |
7172 | break; | |
72a45777 PB |
7173 | } |
7174 | ||
7175 | if (!idev || !idev->dev_id) | |
ca9de185 | 7176 | return 0; |
72a45777 PB |
7177 | |
7178 | entry = get_orom_entry_by_device_id(idev->dev_id); | |
7179 | ||
7180 | if (!entry || !entry->devid_list) | |
7181 | return 0; | |
7182 | ||
7183 | devid_list = entry->devid_list; | |
7184 | for (dv = devid_list; dv; dv = dv->next) { | |
594dc1b8 | 7185 | struct md_list *devlist; |
d3c11416 AO |
7186 | struct sys_dev *device = NULL; |
7187 | char *hpath; | |
72a45777 PB |
7188 | int found = 0; |
7189 | ||
d3c11416 AO |
7190 | if (cmp_hba_path) |
7191 | device = device_by_id_and_path(dv->devid, hba_path); | |
7192 | else | |
7193 | device = device_by_id(dv->devid); | |
7194 | ||
72a45777 | 7195 | if (device) |
d3c11416 | 7196 | hpath = device->path; |
72a45777 PB |
7197 | else |
7198 | return 0; | |
7199 | ||
d3c11416 | 7200 | devlist = get_devices(hpath); |
72a45777 PB |
7201 | /* if no intel devices return zero volumes */ |
7202 | if (devlist == NULL) | |
7203 | return 0; | |
7204 | ||
d3c11416 AO |
7205 | count += active_arrays_by_format("imsm", hpath, &devlist, dpa, |
7206 | verbose); | |
7207 | dprintf("path: %s active arrays: %d\n", hpath, count); | |
72a45777 PB |
7208 | if (devlist == NULL) |
7209 | return 0; | |
7210 | do { | |
7211 | found = 0; | |
7212 | count += count_volumes_list(devlist, | |
7213 | NULL, | |
7214 | verbose, | |
7215 | &found); | |
7216 | dprintf("found %d count: %d\n", found, count); | |
7217 | } while (found); | |
7218 | ||
d3c11416 | 7219 | dprintf("path: %s total number of volumes: %d\n", hpath, count); |
72a45777 PB |
7220 | |
7221 | while (devlist) { | |
7222 | struct md_list *dv = devlist; | |
7223 | devlist = devlist->next; | |
7224 | free(dv->devname); | |
7225 | free(dv); | |
7226 | } | |
ca9de185 LM |
7227 | } |
7228 | return count; | |
7229 | } | |
7230 | ||
d3c11416 AO |
7231 | static int count_volumes(struct intel_hba *hba, int dpa, int verbose) |
7232 | { | |
7233 | if (!hba) | |
7234 | return 0; | |
7235 | if (hba->type == SYS_DEV_VMD) { | |
7236 | struct sys_dev *dev; | |
7237 | int count = 0; | |
7238 | ||
7239 | for (dev = find_intel_devices(); dev; dev = dev->next) { | |
7240 | if (dev->type == SYS_DEV_VMD) | |
7241 | count += __count_volumes(dev->path, dpa, | |
7242 | verbose, 1); | |
7243 | } | |
7244 | return count; | |
7245 | } | |
7246 | return __count_volumes(hba->path, dpa, verbose, 0); | |
7247 | } | |
7248 | ||
cd9d1ac7 DW |
7249 | static int imsm_default_chunk(const struct imsm_orom *orom) |
7250 | { | |
7251 | /* up to 512 if the plaform supports it, otherwise the platform max. | |
7252 | * 128 if no platform detected | |
7253 | */ | |
7254 | int fs = max(7, orom ? fls(orom->sss) : 0); | |
7255 | ||
7256 | return min(512, (1 << fs)); | |
7257 | } | |
73408129 | 7258 | |
6592ce37 DW |
7259 | static int |
7260 | validate_geometry_imsm_orom(struct intel_super *super, int level, int layout, | |
2cc699af | 7261 | int raiddisks, int *chunk, unsigned long long size, int verbose) |
6592ce37 | 7262 | { |
660260d0 DW |
7263 | /* check/set platform and metadata limits/defaults */ |
7264 | if (super->orom && raiddisks > super->orom->dpa) { | |
676e87a8 | 7265 | pr_vrb("platform supports a maximum of %d disks per array\n", |
660260d0 | 7266 | super->orom->dpa); |
73408129 LM |
7267 | return 0; |
7268 | } | |
7269 | ||
5d500228 | 7270 | /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */ |
660260d0 | 7271 | if (!is_raid_level_supported(super->orom, level, raiddisks)) { |
676e87a8 | 7272 | pr_vrb("platform does not support raid%d with %d disk%s\n", |
6592ce37 DW |
7273 | level, raiddisks, raiddisks > 1 ? "s" : ""); |
7274 | return 0; | |
7275 | } | |
cd9d1ac7 | 7276 | |
7ccc4cc4 | 7277 | if (*chunk == 0 || *chunk == UnSet) |
cd9d1ac7 DW |
7278 | *chunk = imsm_default_chunk(super->orom); |
7279 | ||
7ccc4cc4 | 7280 | if (super->orom && !imsm_orom_has_chunk(super->orom, *chunk)) { |
676e87a8 | 7281 | pr_vrb("platform does not support a chunk size of: %d\n", *chunk); |
cd9d1ac7 | 7282 | return 0; |
6592ce37 | 7283 | } |
cd9d1ac7 | 7284 | |
6592ce37 DW |
7285 | if (layout != imsm_level_to_layout(level)) { |
7286 | if (level == 5) | |
676e87a8 | 7287 | pr_vrb("imsm raid 5 only supports the left-asymmetric layout\n"); |
6592ce37 | 7288 | else if (level == 10) |
676e87a8 | 7289 | pr_vrb("imsm raid 10 only supports the n2 layout\n"); |
6592ce37 | 7290 | else |
676e87a8 | 7291 | pr_vrb("imsm unknown layout %#x for this raid level %d\n", |
6592ce37 DW |
7292 | layout, level); |
7293 | return 0; | |
7294 | } | |
2cc699af | 7295 | |
7ccc4cc4 | 7296 | if (super->orom && (super->orom->attr & IMSM_OROM_ATTR_2TB) == 0 && |
2cc699af | 7297 | (calc_array_size(level, raiddisks, layout, *chunk, size) >> 32) > 0) { |
676e87a8 | 7298 | pr_vrb("platform does not support a volume size over 2TB\n"); |
2cc699af CA |
7299 | return 0; |
7300 | } | |
614902f6 | 7301 | |
6592ce37 DW |
7302 | return 1; |
7303 | } | |
7304 | ||
1011e834 | 7305 | /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd |
c2c087e6 DW |
7306 | * FIX ME add ahci details |
7307 | */ | |
8b353278 | 7308 | static int validate_geometry_imsm_volume(struct supertype *st, int level, |
c21e737b | 7309 | int layout, int raiddisks, int *chunk, |
af4348dd N |
7310 | unsigned long long size, |
7311 | unsigned long long data_offset, | |
7312 | char *dev, | |
2c514b71 NB |
7313 | unsigned long long *freesize, |
7314 | int verbose) | |
cdddbdbc | 7315 | { |
9e04ac1c | 7316 | dev_t rdev; |
c2c087e6 | 7317 | struct intel_super *super = st->sb; |
b2916f25 | 7318 | struct imsm_super *mpb; |
c2c087e6 DW |
7319 | struct dl *dl; |
7320 | unsigned long long pos = 0; | |
7321 | unsigned long long maxsize; | |
7322 | struct extent *e; | |
7323 | int i; | |
cdddbdbc | 7324 | |
88c32bb1 DW |
7325 | /* We must have the container info already read in. */ |
7326 | if (!super) | |
c2c087e6 DW |
7327 | return 0; |
7328 | ||
b2916f25 JS |
7329 | mpb = super->anchor; |
7330 | ||
2cc699af | 7331 | if (!validate_geometry_imsm_orom(super, level, layout, raiddisks, chunk, size, verbose)) { |
3e684231 | 7332 | pr_err("RAID geometry validation failed. Cannot proceed with the action(s).\n"); |
c2c087e6 | 7333 | return 0; |
d54559f0 | 7334 | } |
c2c087e6 DW |
7335 | if (!dev) { |
7336 | /* General test: make sure there is space for | |
2da8544a DW |
7337 | * 'raiddisks' device extents of size 'size' at a given |
7338 | * offset | |
c2c087e6 | 7339 | */ |
e46273eb | 7340 | unsigned long long minsize = size; |
b7528a20 | 7341 | unsigned long long start_offset = MaxSector; |
c2c087e6 DW |
7342 | int dcnt = 0; |
7343 | if (minsize == 0) | |
7344 | minsize = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS; | |
7345 | for (dl = super->disks; dl ; dl = dl->next) { | |
7346 | int found = 0; | |
7347 | ||
bf5a934a | 7348 | pos = 0; |
c2c087e6 | 7349 | i = 0; |
05501181 | 7350 | e = get_extents(super, dl, 0); |
c2c087e6 DW |
7351 | if (!e) continue; |
7352 | do { | |
7353 | unsigned long long esize; | |
7354 | esize = e[i].start - pos; | |
7355 | if (esize >= minsize) | |
7356 | found = 1; | |
b7528a20 | 7357 | if (found && start_offset == MaxSector) { |
2da8544a DW |
7358 | start_offset = pos; |
7359 | break; | |
7360 | } else if (found && pos != start_offset) { | |
7361 | found = 0; | |
7362 | break; | |
7363 | } | |
c2c087e6 DW |
7364 | pos = e[i].start + e[i].size; |
7365 | i++; | |
7366 | } while (e[i-1].size); | |
7367 | if (found) | |
7368 | dcnt++; | |
7369 | free(e); | |
7370 | } | |
7371 | if (dcnt < raiddisks) { | |
2c514b71 | 7372 | if (verbose) |
7a862a02 | 7373 | pr_err("imsm: Not enough devices with space for this array (%d < %d)\n", |
2c514b71 | 7374 | dcnt, raiddisks); |
c2c087e6 DW |
7375 | return 0; |
7376 | } | |
7377 | return 1; | |
7378 | } | |
0dcecb2e | 7379 | |
c2c087e6 | 7380 | /* This device must be a member of the set */ |
9e04ac1c | 7381 | if (!stat_is_blkdev(dev, &rdev)) |
c2c087e6 DW |
7382 | return 0; |
7383 | for (dl = super->disks ; dl ; dl = dl->next) { | |
9e04ac1c ZL |
7384 | if (dl->major == (int)major(rdev) && |
7385 | dl->minor == (int)minor(rdev)) | |
c2c087e6 DW |
7386 | break; |
7387 | } | |
7388 | if (!dl) { | |
2c514b71 | 7389 | if (verbose) |
7a862a02 | 7390 | pr_err("%s is not in the same imsm set\n", dev); |
c2c087e6 | 7391 | return 0; |
a20d2ba5 DW |
7392 | } else if (super->orom && dl->index < 0 && mpb->num_raid_devs) { |
7393 | /* If a volume is present then the current creation attempt | |
7394 | * cannot incorporate new spares because the orom may not | |
7395 | * understand this configuration (all member disks must be | |
7396 | * members of each array in the container). | |
7397 | */ | |
7a862a02 N |
7398 | pr_err("%s is a spare and a volume is already defined for this container\n", dev); |
7399 | pr_err("The option-rom requires all member disks to be a member of all volumes\n"); | |
a20d2ba5 | 7400 | return 0; |
5fe62b94 WD |
7401 | } else if (super->orom && mpb->num_raid_devs > 0 && |
7402 | mpb->num_disks != raiddisks) { | |
7a862a02 | 7403 | pr_err("The option-rom requires all member disks to be a member of all volumes\n"); |
5fe62b94 | 7404 | return 0; |
c2c087e6 | 7405 | } |
0dcecb2e DW |
7406 | |
7407 | /* retrieve the largest free space block */ | |
05501181 | 7408 | e = get_extents(super, dl, 0); |
c2c087e6 DW |
7409 | maxsize = 0; |
7410 | i = 0; | |
0dcecb2e DW |
7411 | if (e) { |
7412 | do { | |
7413 | unsigned long long esize; | |
7414 | ||
7415 | esize = e[i].start - pos; | |
7416 | if (esize >= maxsize) | |
7417 | maxsize = esize; | |
7418 | pos = e[i].start + e[i].size; | |
7419 | i++; | |
7420 | } while (e[i-1].size); | |
7421 | dl->e = e; | |
7422 | dl->extent_cnt = i; | |
7423 | } else { | |
7424 | if (verbose) | |
e7b84f9d | 7425 | pr_err("unable to determine free space for: %s\n", |
0dcecb2e DW |
7426 | dev); |
7427 | return 0; | |
7428 | } | |
7429 | if (maxsize < size) { | |
7430 | if (verbose) | |
e7b84f9d | 7431 | pr_err("%s not enough space (%llu < %llu)\n", |
0dcecb2e DW |
7432 | dev, maxsize, size); |
7433 | return 0; | |
7434 | } | |
7435 | ||
7436 | /* count total number of extents for merge */ | |
7437 | i = 0; | |
7438 | for (dl = super->disks; dl; dl = dl->next) | |
7439 | if (dl->e) | |
7440 | i += dl->extent_cnt; | |
7441 | ||
7442 | maxsize = merge_extents(super, i); | |
3baa56ab | 7443 | |
1a1ced1e KS |
7444 | if (mpb->num_raid_devs > 0 && size && size != maxsize) |
7445 | pr_err("attempting to create a second volume with size less then remaining space.\n"); | |
3baa56ab | 7446 | |
a7dd165b | 7447 | if (maxsize < size || maxsize == 0) { |
b3071342 LD |
7448 | if (verbose) { |
7449 | if (maxsize == 0) | |
7a862a02 | 7450 | pr_err("no free space left on device. Aborting...\n"); |
b3071342 | 7451 | else |
7a862a02 | 7452 | pr_err("not enough space to create volume of given size (%llu < %llu). Aborting...\n", |
b3071342 LD |
7453 | maxsize, size); |
7454 | } | |
0dcecb2e | 7455 | return 0; |
0dcecb2e DW |
7456 | } |
7457 | ||
c2c087e6 DW |
7458 | *freesize = maxsize; |
7459 | ||
ca9de185 | 7460 | if (super->orom) { |
72a45777 | 7461 | int count = count_volumes(super->hba, |
ca9de185 LM |
7462 | super->orom->dpa, verbose); |
7463 | if (super->orom->vphba <= count) { | |
676e87a8 | 7464 | pr_vrb("platform does not support more than %d raid volumes.\n", |
ca9de185 LM |
7465 | super->orom->vphba); |
7466 | return 0; | |
7467 | } | |
7468 | } | |
c2c087e6 | 7469 | return 1; |
cdddbdbc DW |
7470 | } |
7471 | ||
13bcac90 | 7472 | static int imsm_get_free_size(struct supertype *st, int raiddisks, |
efb30e7f DW |
7473 | unsigned long long size, int chunk, |
7474 | unsigned long long *freesize) | |
7475 | { | |
7476 | struct intel_super *super = st->sb; | |
7477 | struct imsm_super *mpb = super->anchor; | |
7478 | struct dl *dl; | |
7479 | int i; | |
7480 | int extent_cnt; | |
7481 | struct extent *e; | |
7482 | unsigned long long maxsize; | |
7483 | unsigned long long minsize; | |
7484 | int cnt; | |
7485 | int used; | |
7486 | ||
7487 | /* find the largest common start free region of the possible disks */ | |
7488 | used = 0; | |
7489 | extent_cnt = 0; | |
7490 | cnt = 0; | |
7491 | for (dl = super->disks; dl; dl = dl->next) { | |
7492 | dl->raiddisk = -1; | |
7493 | ||
7494 | if (dl->index >= 0) | |
7495 | used++; | |
7496 | ||
7497 | /* don't activate new spares if we are orom constrained | |
7498 | * and there is already a volume active in the container | |
7499 | */ | |
7500 | if (super->orom && dl->index < 0 && mpb->num_raid_devs) | |
7501 | continue; | |
7502 | ||
05501181 | 7503 | e = get_extents(super, dl, 0); |
efb30e7f DW |
7504 | if (!e) |
7505 | continue; | |
7506 | for (i = 1; e[i-1].size; i++) | |
7507 | ; | |
7508 | dl->e = e; | |
7509 | dl->extent_cnt = i; | |
7510 | extent_cnt += i; | |
7511 | cnt++; | |
7512 | } | |
7513 | ||
7514 | maxsize = merge_extents(super, extent_cnt); | |
7515 | minsize = size; | |
7516 | if (size == 0) | |
612e59d8 CA |
7517 | /* chunk is in K */ |
7518 | minsize = chunk * 2; | |
efb30e7f DW |
7519 | |
7520 | if (cnt < raiddisks || | |
7521 | (super->orom && used && used != raiddisks) || | |
a7dd165b DW |
7522 | maxsize < minsize || |
7523 | maxsize == 0) { | |
e7b84f9d | 7524 | pr_err("not enough devices with space to create array.\n"); |
efb30e7f DW |
7525 | return 0; /* No enough free spaces large enough */ |
7526 | } | |
7527 | ||
7528 | if (size == 0) { | |
7529 | size = maxsize; | |
7530 | if (chunk) { | |
612e59d8 CA |
7531 | size /= 2 * chunk; |
7532 | size *= 2 * chunk; | |
efb30e7f | 7533 | } |
f878b242 LM |
7534 | maxsize = size; |
7535 | } | |
1a1ced1e KS |
7536 | if (mpb->num_raid_devs > 0 && size && size != maxsize) |
7537 | pr_err("attempting to create a second volume with size less then remaining space.\n"); | |
efb30e7f DW |
7538 | cnt = 0; |
7539 | for (dl = super->disks; dl; dl = dl->next) | |
7540 | if (dl->e) | |
7541 | dl->raiddisk = cnt++; | |
7542 | ||
7543 | *freesize = size; | |
7544 | ||
13bcac90 AK |
7545 | dprintf("imsm: imsm_get_free_size() returns : %llu\n", size); |
7546 | ||
efb30e7f DW |
7547 | return 1; |
7548 | } | |
7549 | ||
13bcac90 AK |
7550 | static int reserve_space(struct supertype *st, int raiddisks, |
7551 | unsigned long long size, int chunk, | |
7552 | unsigned long long *freesize) | |
7553 | { | |
7554 | struct intel_super *super = st->sb; | |
7555 | struct dl *dl; | |
7556 | int cnt; | |
7557 | int rv = 0; | |
7558 | ||
7559 | rv = imsm_get_free_size(st, raiddisks, size, chunk, freesize); | |
7560 | if (rv) { | |
7561 | cnt = 0; | |
7562 | for (dl = super->disks; dl; dl = dl->next) | |
7563 | if (dl->e) | |
7564 | dl->raiddisk = cnt++; | |
7565 | rv = 1; | |
7566 | } | |
7567 | ||
7568 | return rv; | |
7569 | } | |
7570 | ||
bf5a934a | 7571 | static int validate_geometry_imsm(struct supertype *st, int level, int layout, |
c21e737b | 7572 | int raiddisks, int *chunk, unsigned long long size, |
af4348dd | 7573 | unsigned long long data_offset, |
bf5a934a | 7574 | char *dev, unsigned long long *freesize, |
5308f117 | 7575 | int consistency_policy, int verbose) |
bf5a934a DW |
7576 | { |
7577 | int fd, cfd; | |
7578 | struct mdinfo *sra; | |
20cbe8d2 | 7579 | int is_member = 0; |
bf5a934a | 7580 | |
d54559f0 LM |
7581 | /* load capability |
7582 | * if given unused devices create a container | |
bf5a934a DW |
7583 | * if given given devices in a container create a member volume |
7584 | */ | |
1f5d54a0 | 7585 | if (level == LEVEL_CONTAINER) |
bf5a934a | 7586 | /* Must be a fresh device to add to a container */ |
1f5d54a0 MT |
7587 | return validate_geometry_imsm_container(st, level, raiddisks, |
7588 | data_offset, dev, | |
7589 | freesize, verbose); | |
9587c373 | 7590 | |
06a6101c BK |
7591 | /* |
7592 | * Size is given in sectors. | |
7593 | */ | |
7594 | if (size && (size < 2048)) { | |
22dc741f | 7595 | pr_err("Given size must be greater than 1M.\n"); |
54865c30 RS |
7596 | /* Depends on algorithm in Create.c : |
7597 | * if container was given (dev == NULL) return -1, | |
7598 | * if block device was given ( dev != NULL) return 0. | |
7599 | */ | |
7600 | return dev ? -1 : 0; | |
7601 | } | |
7602 | ||
8592f29d | 7603 | if (!dev) { |
e91a3bad | 7604 | if (st->sb) { |
ca9de185 | 7605 | struct intel_super *super = st->sb; |
e91a3bad | 7606 | if (!validate_geometry_imsm_orom(st->sb, level, layout, |
2cc699af | 7607 | raiddisks, chunk, size, |
e91a3bad LM |
7608 | verbose)) |
7609 | return 0; | |
efb30e7f DW |
7610 | /* we are being asked to automatically layout a |
7611 | * new volume based on the current contents of | |
7612 | * the container. If the the parameters can be | |
7613 | * satisfied reserve_space will record the disks, | |
7614 | * start offset, and size of the volume to be | |
7615 | * created. add_to_super and getinfo_super | |
7616 | * detect when autolayout is in progress. | |
7617 | */ | |
ca9de185 LM |
7618 | /* assuming that freesize is always given when array is |
7619 | created */ | |
7620 | if (super->orom && freesize) { | |
7621 | int count; | |
72a45777 | 7622 | count = count_volumes(super->hba, |
ca9de185 LM |
7623 | super->orom->dpa, verbose); |
7624 | if (super->orom->vphba <= count) { | |
676e87a8 | 7625 | pr_vrb("platform does not support more than %d raid volumes.\n", |
ca9de185 LM |
7626 | super->orom->vphba); |
7627 | return 0; | |
7628 | } | |
7629 | } | |
e91a3bad LM |
7630 | if (freesize) |
7631 | return reserve_space(st, raiddisks, size, | |
7ccc4cc4 | 7632 | *chunk, freesize); |
8592f29d N |
7633 | } |
7634 | return 1; | |
7635 | } | |
bf5a934a DW |
7636 | if (st->sb) { |
7637 | /* creating in a given container */ | |
7638 | return validate_geometry_imsm_volume(st, level, layout, | |
7639 | raiddisks, chunk, size, | |
af4348dd | 7640 | data_offset, |
bf5a934a DW |
7641 | dev, freesize, verbose); |
7642 | } | |
7643 | ||
bf5a934a DW |
7644 | /* This device needs to be a device in an 'imsm' container */ |
7645 | fd = open(dev, O_RDONLY|O_EXCL, 0); | |
7646 | if (fd >= 0) { | |
7647 | if (verbose) | |
e7b84f9d N |
7648 | pr_err("Cannot create this array on device %s\n", |
7649 | dev); | |
bf5a934a DW |
7650 | close(fd); |
7651 | return 0; | |
7652 | } | |
7653 | if (errno != EBUSY || (fd = open(dev, O_RDONLY, 0)) < 0) { | |
7654 | if (verbose) | |
e7b84f9d | 7655 | pr_err("Cannot open %s: %s\n", |
bf5a934a DW |
7656 | dev, strerror(errno)); |
7657 | return 0; | |
7658 | } | |
7659 | /* Well, it is in use by someone, maybe an 'imsm' container. */ | |
7660 | cfd = open_container(fd); | |
20cbe8d2 | 7661 | close(fd); |
bf5a934a | 7662 | if (cfd < 0) { |
bf5a934a | 7663 | if (verbose) |
e7b84f9d | 7664 | pr_err("Cannot use %s: It is busy\n", |
bf5a934a DW |
7665 | dev); |
7666 | return 0; | |
7667 | } | |
4dd2df09 | 7668 | sra = sysfs_read(cfd, NULL, GET_VERSION); |
bf5a934a | 7669 | if (sra && sra->array.major_version == -1 && |
20cbe8d2 AW |
7670 | strcmp(sra->text_version, "imsm") == 0) |
7671 | is_member = 1; | |
7672 | sysfs_free(sra); | |
7673 | if (is_member) { | |
bf5a934a DW |
7674 | /* This is a member of a imsm container. Load the container |
7675 | * and try to create a volume | |
7676 | */ | |
7677 | struct intel_super *super; | |
7678 | ||
ec50f7b6 | 7679 | if (load_super_imsm_all(st, cfd, (void **) &super, NULL, NULL, 1) == 0) { |
bf5a934a | 7680 | st->sb = super; |
4dd2df09 | 7681 | strcpy(st->container_devnm, fd2devnm(cfd)); |
bf5a934a DW |
7682 | close(cfd); |
7683 | return validate_geometry_imsm_volume(st, level, layout, | |
7684 | raiddisks, chunk, | |
af4348dd | 7685 | size, data_offset, dev, |
ecbd9e81 N |
7686 | freesize, 1) |
7687 | ? 1 : -1; | |
bf5a934a | 7688 | } |
20cbe8d2 | 7689 | } |
bf5a934a | 7690 | |
20cbe8d2 | 7691 | if (verbose) |
e7b84f9d | 7692 | pr_err("failed container membership check\n"); |
20cbe8d2 AW |
7693 | |
7694 | close(cfd); | |
7695 | return 0; | |
bf5a934a | 7696 | } |
0bd16cf2 | 7697 | |
30f58b22 | 7698 | static void default_geometry_imsm(struct supertype *st, int *level, int *layout, int *chunk) |
0bd16cf2 DJ |
7699 | { |
7700 | struct intel_super *super = st->sb; | |
7701 | ||
30f58b22 DW |
7702 | if (level && *level == UnSet) |
7703 | *level = LEVEL_CONTAINER; | |
7704 | ||
7705 | if (level && layout && *layout == UnSet) | |
7706 | *layout = imsm_level_to_layout(*level); | |
0bd16cf2 | 7707 | |
cd9d1ac7 DW |
7708 | if (chunk && (*chunk == UnSet || *chunk == 0)) |
7709 | *chunk = imsm_default_chunk(super->orom); | |
0bd16cf2 DJ |
7710 | } |
7711 | ||
33414a01 DW |
7712 | static void handle_missing(struct intel_super *super, struct imsm_dev *dev); |
7713 | ||
3364781b | 7714 | static int kill_subarray_imsm(struct supertype *st, char *subarray_id) |
33414a01 | 7715 | { |
3364781b | 7716 | /* remove the subarray currently referenced by subarray_id */ |
33414a01 DW |
7717 | __u8 i; |
7718 | struct intel_dev **dp; | |
7719 | struct intel_super *super = st->sb; | |
3364781b | 7720 | __u8 current_vol = strtoul(subarray_id, NULL, 10); |
33414a01 DW |
7721 | struct imsm_super *mpb = super->anchor; |
7722 | ||
3364781b | 7723 | if (mpb->num_raid_devs == 0) |
33414a01 | 7724 | return 2; |
33414a01 DW |
7725 | |
7726 | /* block deletions that would change the uuid of active subarrays | |
7727 | * | |
7728 | * FIXME when immutable ids are available, but note that we'll | |
7729 | * also need to fixup the invalidated/active subarray indexes in | |
7730 | * mdstat | |
7731 | */ | |
7732 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
7733 | char subarray[4]; | |
7734 | ||
7735 | if (i < current_vol) | |
7736 | continue; | |
7737 | sprintf(subarray, "%u", i); | |
4dd2df09 | 7738 | if (is_subarray_active(subarray, st->devnm)) { |
e7b84f9d N |
7739 | pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n", |
7740 | current_vol, i); | |
33414a01 DW |
7741 | |
7742 | return 2; | |
7743 | } | |
7744 | } | |
7745 | ||
7746 | if (st->update_tail) { | |
503975b9 | 7747 | struct imsm_update_kill_array *u = xmalloc(sizeof(*u)); |
33414a01 | 7748 | |
33414a01 DW |
7749 | u->type = update_kill_array; |
7750 | u->dev_idx = current_vol; | |
7751 | append_metadata_update(st, u, sizeof(*u)); | |
7752 | ||
7753 | return 0; | |
7754 | } | |
7755 | ||
7756 | for (dp = &super->devlist; *dp;) | |
7757 | if ((*dp)->index == current_vol) { | |
7758 | *dp = (*dp)->next; | |
7759 | } else { | |
7760 | handle_missing(super, (*dp)->dev); | |
7761 | if ((*dp)->index > current_vol) | |
7762 | (*dp)->index--; | |
7763 | dp = &(*dp)->next; | |
7764 | } | |
7765 | ||
7766 | /* no more raid devices, all active components are now spares, | |
7767 | * but of course failed are still failed | |
7768 | */ | |
7769 | if (--mpb->num_raid_devs == 0) { | |
7770 | struct dl *d; | |
7771 | ||
7772 | for (d = super->disks; d; d = d->next) | |
a8619d23 AK |
7773 | if (d->index > -2) |
7774 | mark_spare(d); | |
33414a01 DW |
7775 | } |
7776 | ||
7777 | super->updates_pending++; | |
7778 | ||
7779 | return 0; | |
7780 | } | |
aa534678 | 7781 | |
19ad203e JR |
7782 | static int get_rwh_policy_from_update(char *update) |
7783 | { | |
7784 | if (strcmp(update, "ppl") == 0) | |
7785 | return RWH_MULTIPLE_DISTRIBUTED; | |
7786 | else if (strcmp(update, "no-ppl") == 0) | |
7787 | return RWH_MULTIPLE_OFF; | |
7788 | else if (strcmp(update, "bitmap") == 0) | |
7789 | return RWH_BITMAP; | |
7790 | else if (strcmp(update, "no-bitmap") == 0) | |
7791 | return RWH_OFF; | |
7792 | return -1; | |
7793 | } | |
7794 | ||
a951a4f7 | 7795 | static int update_subarray_imsm(struct supertype *st, char *subarray, |
fa56eddb | 7796 | char *update, struct mddev_ident *ident) |
aa534678 DW |
7797 | { |
7798 | /* update the subarray currently referenced by ->current_vol */ | |
7799 | struct intel_super *super = st->sb; | |
7800 | struct imsm_super *mpb = super->anchor; | |
7801 | ||
aa534678 DW |
7802 | if (strcmp(update, "name") == 0) { |
7803 | char *name = ident->name; | |
a951a4f7 N |
7804 | char *ep; |
7805 | int vol; | |
aa534678 | 7806 | |
4dd2df09 | 7807 | if (is_subarray_active(subarray, st->devnm)) { |
e7b84f9d | 7808 | pr_err("Unable to update name of active subarray\n"); |
aa534678 DW |
7809 | return 2; |
7810 | } | |
7811 | ||
7812 | if (!check_name(super, name, 0)) | |
7813 | return 2; | |
7814 | ||
a951a4f7 N |
7815 | vol = strtoul(subarray, &ep, 10); |
7816 | if (*ep != '\0' || vol >= super->anchor->num_raid_devs) | |
7817 | return 2; | |
7818 | ||
aa534678 | 7819 | if (st->update_tail) { |
503975b9 | 7820 | struct imsm_update_rename_array *u = xmalloc(sizeof(*u)); |
aa534678 | 7821 | |
aa534678 | 7822 | u->type = update_rename_array; |
a951a4f7 | 7823 | u->dev_idx = vol; |
618f4e6d XN |
7824 | strncpy((char *) u->name, name, MAX_RAID_SERIAL_LEN); |
7825 | u->name[MAX_RAID_SERIAL_LEN-1] = '\0'; | |
aa534678 DW |
7826 | append_metadata_update(st, u, sizeof(*u)); |
7827 | } else { | |
7828 | struct imsm_dev *dev; | |
ebad3af2 | 7829 | int i, namelen; |
aa534678 | 7830 | |
a951a4f7 | 7831 | dev = get_imsm_dev(super, vol); |
ebad3af2 JS |
7832 | memset(dev->volume, '\0', MAX_RAID_SERIAL_LEN); |
7833 | namelen = min((int)strlen(name), MAX_RAID_SERIAL_LEN); | |
7834 | memcpy(dev->volume, name, namelen); | |
aa534678 DW |
7835 | for (i = 0; i < mpb->num_raid_devs; i++) { |
7836 | dev = get_imsm_dev(super, i); | |
7837 | handle_missing(super, dev); | |
7838 | } | |
7839 | super->updates_pending++; | |
7840 | } | |
19ad203e | 7841 | } else if (get_rwh_policy_from_update(update) != -1) { |
e6e9dd3f AP |
7842 | int new_policy; |
7843 | char *ep; | |
7844 | int vol = strtoul(subarray, &ep, 10); | |
7845 | ||
7846 | if (*ep != '\0' || vol >= super->anchor->num_raid_devs) | |
7847 | return 2; | |
7848 | ||
19ad203e | 7849 | new_policy = get_rwh_policy_from_update(update); |
e6e9dd3f AP |
7850 | |
7851 | if (st->update_tail) { | |
7852 | struct imsm_update_rwh_policy *u = xmalloc(sizeof(*u)); | |
7853 | ||
7854 | u->type = update_rwh_policy; | |
7855 | u->dev_idx = vol; | |
7856 | u->new_policy = new_policy; | |
7857 | append_metadata_update(st, u, sizeof(*u)); | |
7858 | } else { | |
7859 | struct imsm_dev *dev; | |
7860 | ||
7861 | dev = get_imsm_dev(super, vol); | |
7862 | dev->rwh_policy = new_policy; | |
7863 | super->updates_pending++; | |
7864 | } | |
19ad203e JR |
7865 | if (new_policy == RWH_BITMAP) |
7866 | return write_init_bitmap_imsm_vol(st, vol); | |
aa534678 DW |
7867 | } else |
7868 | return 2; | |
7869 | ||
7870 | return 0; | |
7871 | } | |
bf5a934a | 7872 | |
28bce06f AK |
7873 | static int is_gen_migration(struct imsm_dev *dev) |
7874 | { | |
7534230b AK |
7875 | if (dev == NULL) |
7876 | return 0; | |
7877 | ||
28bce06f AK |
7878 | if (!dev->vol.migr_state) |
7879 | return 0; | |
7880 | ||
7881 | if (migr_type(dev) == MIGR_GEN_MIGR) | |
7882 | return 1; | |
7883 | ||
7884 | return 0; | |
7885 | } | |
7886 | ||
1e5c6983 DW |
7887 | static int is_rebuilding(struct imsm_dev *dev) |
7888 | { | |
7889 | struct imsm_map *migr_map; | |
7890 | ||
7891 | if (!dev->vol.migr_state) | |
7892 | return 0; | |
7893 | ||
7894 | if (migr_type(dev) != MIGR_REBUILD) | |
7895 | return 0; | |
7896 | ||
238c0a71 | 7897 | migr_map = get_imsm_map(dev, MAP_1); |
1e5c6983 DW |
7898 | |
7899 | if (migr_map->map_state == IMSM_T_STATE_DEGRADED) | |
7900 | return 1; | |
7901 | else | |
7902 | return 0; | |
7903 | } | |
7904 | ||
6ce1fbf1 AK |
7905 | static int is_initializing(struct imsm_dev *dev) |
7906 | { | |
7907 | struct imsm_map *migr_map; | |
7908 | ||
7909 | if (!dev->vol.migr_state) | |
7910 | return 0; | |
7911 | ||
7912 | if (migr_type(dev) != MIGR_INIT) | |
7913 | return 0; | |
7914 | ||
238c0a71 | 7915 | migr_map = get_imsm_map(dev, MAP_1); |
6ce1fbf1 AK |
7916 | |
7917 | if (migr_map->map_state == IMSM_T_STATE_UNINITIALIZED) | |
7918 | return 1; | |
7919 | ||
7920 | return 0; | |
6ce1fbf1 AK |
7921 | } |
7922 | ||
c47b0ff6 AK |
7923 | static void update_recovery_start(struct intel_super *super, |
7924 | struct imsm_dev *dev, | |
7925 | struct mdinfo *array) | |
1e5c6983 DW |
7926 | { |
7927 | struct mdinfo *rebuild = NULL; | |
7928 | struct mdinfo *d; | |
7929 | __u32 units; | |
7930 | ||
7931 | if (!is_rebuilding(dev)) | |
7932 | return; | |
7933 | ||
7934 | /* Find the rebuild target, but punt on the dual rebuild case */ | |
7935 | for (d = array->devs; d; d = d->next) | |
7936 | if (d->recovery_start == 0) { | |
7937 | if (rebuild) | |
7938 | return; | |
7939 | rebuild = d; | |
7940 | } | |
7941 | ||
4363fd80 DW |
7942 | if (!rebuild) { |
7943 | /* (?) none of the disks are marked with | |
7944 | * IMSM_ORD_REBUILD, so assume they are missing and the | |
7945 | * disk_ord_tbl was not correctly updated | |
7946 | */ | |
1ade5cc1 | 7947 | dprintf("failed to locate out-of-sync disk\n"); |
4363fd80 DW |
7948 | return; |
7949 | } | |
7950 | ||
4036e7ee | 7951 | units = vol_curr_migr_unit(dev); |
c47b0ff6 | 7952 | rebuild->recovery_start = units * blocks_per_migr_unit(super, dev); |
1e5c6983 DW |
7953 | } |
7954 | ||
276d77db | 7955 | static int recover_backup_imsm(struct supertype *st, struct mdinfo *info); |
1e5c6983 | 7956 | |
00bbdbda | 7957 | static struct mdinfo *container_content_imsm(struct supertype *st, char *subarray) |
cdddbdbc | 7958 | { |
4f5bc454 DW |
7959 | /* Given a container loaded by load_super_imsm_all, |
7960 | * extract information about all the arrays into | |
7961 | * an mdinfo tree. | |
00bbdbda | 7962 | * If 'subarray' is given, just extract info about that array. |
4f5bc454 DW |
7963 | * |
7964 | * For each imsm_dev create an mdinfo, fill it in, | |
7965 | * then look for matching devices in super->disks | |
7966 | * and create appropriate device mdinfo. | |
7967 | */ | |
7968 | struct intel_super *super = st->sb; | |
949c47a0 | 7969 | struct imsm_super *mpb = super->anchor; |
4f5bc454 | 7970 | struct mdinfo *rest = NULL; |
00bbdbda | 7971 | unsigned int i; |
81219e70 | 7972 | int sb_errors = 0; |
abef11a3 AK |
7973 | struct dl *d; |
7974 | int spare_disks = 0; | |
b6180160 | 7975 | int current_vol = super->current_vol; |
cdddbdbc | 7976 | |
19482bcc AK |
7977 | /* do not assemble arrays when not all attributes are supported */ |
7978 | if (imsm_check_attributes(mpb->attributes) == 0) { | |
81219e70 | 7979 | sb_errors = 1; |
7a862a02 | 7980 | pr_err("Unsupported attributes in IMSM metadata.Arrays activation is blocked.\n"); |
19482bcc AK |
7981 | } |
7982 | ||
abef11a3 AK |
7983 | /* count spare devices, not used in maps |
7984 | */ | |
7985 | for (d = super->disks; d; d = d->next) | |
7986 | if (d->index == -1) | |
7987 | spare_disks++; | |
7988 | ||
4f5bc454 | 7989 | for (i = 0; i < mpb->num_raid_devs; i++) { |
00bbdbda N |
7990 | struct imsm_dev *dev; |
7991 | struct imsm_map *map; | |
86e3692b | 7992 | struct imsm_map *map2; |
4f5bc454 | 7993 | struct mdinfo *this; |
a6482415 | 7994 | int slot; |
a6482415 | 7995 | int chunk; |
00bbdbda | 7996 | char *ep; |
8b9cd157 | 7997 | int level; |
00bbdbda N |
7998 | |
7999 | if (subarray && | |
8000 | (i != strtoul(subarray, &ep, 10) || *ep != '\0')) | |
8001 | continue; | |
8002 | ||
8003 | dev = get_imsm_dev(super, i); | |
238c0a71 AK |
8004 | map = get_imsm_map(dev, MAP_0); |
8005 | map2 = get_imsm_map(dev, MAP_1); | |
8b9cd157 | 8006 | level = get_imsm_raid_level(map); |
4f5bc454 | 8007 | |
1ce0101c DW |
8008 | /* do not publish arrays that are in the middle of an |
8009 | * unsupported migration | |
8010 | */ | |
8011 | if (dev->vol.migr_state && | |
28bce06f | 8012 | (migr_type(dev) == MIGR_STATE_CHANGE)) { |
7a862a02 | 8013 | pr_err("cannot assemble volume '%.16s': unsupported migration in progress\n", |
1ce0101c DW |
8014 | dev->volume); |
8015 | continue; | |
8016 | } | |
2db86302 LM |
8017 | /* do not publish arrays that are not support by controller's |
8018 | * OROM/EFI | |
8019 | */ | |
1ce0101c | 8020 | |
503975b9 | 8021 | this = xmalloc(sizeof(*this)); |
4f5bc454 | 8022 | |
301406c9 | 8023 | super->current_vol = i; |
a5d85af7 | 8024 | getinfo_super_imsm_volume(st, this, NULL); |
9894ec0d | 8025 | this->next = rest; |
a6482415 | 8026 | chunk = __le16_to_cpu(map->blocks_per_strip) >> 1; |
81219e70 LM |
8027 | /* mdadm does not support all metadata features- set the bit in all arrays state */ |
8028 | if (!validate_geometry_imsm_orom(super, | |
8b9cd157 MK |
8029 | level, /* RAID level */ |
8030 | imsm_level_to_layout(level), | |
81219e70 | 8031 | map->num_members, /* raid disks */ |
fcc2c9da | 8032 | &chunk, imsm_dev_size(dev), |
81219e70 | 8033 | 1 /* verbose */)) { |
7a862a02 | 8034 | pr_err("IMSM RAID geometry validation failed. Array %s activation is blocked.\n", |
81219e70 LM |
8035 | dev->volume); |
8036 | this->array.state |= | |
8037 | (1<<MD_SB_BLOCK_CONTAINER_RESHAPE) | | |
8038 | (1<<MD_SB_BLOCK_VOLUME); | |
8039 | } | |
81219e70 LM |
8040 | |
8041 | /* if array has bad blocks, set suitable bit in all arrays state */ | |
8042 | if (sb_errors) | |
8043 | this->array.state |= | |
8044 | (1<<MD_SB_BLOCK_CONTAINER_RESHAPE) | | |
8045 | (1<<MD_SB_BLOCK_VOLUME); | |
8046 | ||
4f5bc454 | 8047 | for (slot = 0 ; slot < map->num_members; slot++) { |
1e5c6983 | 8048 | unsigned long long recovery_start; |
4f5bc454 DW |
8049 | struct mdinfo *info_d; |
8050 | struct dl *d; | |
8051 | int idx; | |
9a1608e5 | 8052 | int skip; |
7eef0453 | 8053 | __u32 ord; |
8b9cd157 | 8054 | int missing = 0; |
4f5bc454 | 8055 | |
9a1608e5 | 8056 | skip = 0; |
238c0a71 AK |
8057 | idx = get_imsm_disk_idx(dev, slot, MAP_0); |
8058 | ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X); | |
4f5bc454 DW |
8059 | for (d = super->disks; d ; d = d->next) |
8060 | if (d->index == idx) | |
0fbd635c | 8061 | break; |
4f5bc454 | 8062 | |
1e5c6983 | 8063 | recovery_start = MaxSector; |
4f5bc454 | 8064 | if (d == NULL) |
9a1608e5 | 8065 | skip = 1; |
25ed7e59 | 8066 | if (d && is_failed(&d->disk)) |
9a1608e5 | 8067 | skip = 1; |
8b9cd157 | 8068 | if (!skip && (ord & IMSM_ORD_REBUILD)) |
1e5c6983 | 8069 | recovery_start = 0; |
1e93d0d1 BK |
8070 | if (!(ord & IMSM_ORD_REBUILD)) |
8071 | this->array.working_disks++; | |
1011e834 | 8072 | /* |
9a1608e5 | 8073 | * if we skip some disks the array will be assmebled degraded; |
1e5c6983 DW |
8074 | * reset resync start to avoid a dirty-degraded |
8075 | * situation when performing the intial sync | |
9a1608e5 | 8076 | */ |
8b9cd157 MK |
8077 | if (skip) |
8078 | missing++; | |
8079 | ||
8080 | if (!(dev->vol.dirty & RAIDVOL_DIRTY)) { | |
8081 | if ((!able_to_resync(level, missing) || | |
8082 | recovery_start == 0)) | |
8083 | this->resync_start = MaxSector; | |
8b9cd157 MK |
8084 | } |
8085 | ||
9a1608e5 DW |
8086 | if (skip) |
8087 | continue; | |
4f5bc454 | 8088 | |
503975b9 | 8089 | info_d = xcalloc(1, sizeof(*info_d)); |
4f5bc454 DW |
8090 | info_d->next = this->devs; |
8091 | this->devs = info_d; | |
8092 | ||
4f5bc454 DW |
8093 | info_d->disk.number = d->index; |
8094 | info_d->disk.major = d->major; | |
8095 | info_d->disk.minor = d->minor; | |
8096 | info_d->disk.raid_disk = slot; | |
1e5c6983 | 8097 | info_d->recovery_start = recovery_start; |
86e3692b AK |
8098 | if (map2) { |
8099 | if (slot < map2->num_members) | |
8100 | info_d->disk.state = (1 << MD_DISK_ACTIVE); | |
04c3c514 AK |
8101 | else |
8102 | this->array.spare_disks++; | |
86e3692b AK |
8103 | } else { |
8104 | if (slot < map->num_members) | |
8105 | info_d->disk.state = (1 << MD_DISK_ACTIVE); | |
04c3c514 AK |
8106 | else |
8107 | this->array.spare_disks++; | |
86e3692b | 8108 | } |
4f5bc454 DW |
8109 | |
8110 | info_d->events = __le32_to_cpu(mpb->generation_num); | |
5551b113 | 8111 | info_d->data_offset = pba_of_lba0(map); |
44490938 | 8112 | info_d->component_size = calc_component_size(map, dev); |
06fb291a PB |
8113 | |
8114 | if (map->raid_level == 5) { | |
2432ce9b AP |
8115 | info_d->ppl_sector = this->ppl_sector; |
8116 | info_d->ppl_size = this->ppl_size; | |
98e96bdb AP |
8117 | if (this->consistency_policy == CONSISTENCY_POLICY_PPL && |
8118 | recovery_start == 0) | |
8119 | this->resync_start = 0; | |
06fb291a | 8120 | } |
b12796be | 8121 | |
5e46202e | 8122 | info_d->bb.supported = 1; |
b12796be TM |
8123 | get_volume_badblocks(super->bbm_log, ord_to_idx(ord), |
8124 | info_d->data_offset, | |
8125 | info_d->component_size, | |
8126 | &info_d->bb); | |
4f5bc454 | 8127 | } |
1e5c6983 | 8128 | /* now that the disk list is up-to-date fixup recovery_start */ |
c47b0ff6 | 8129 | update_recovery_start(super, dev, this); |
abef11a3 | 8130 | this->array.spare_disks += spare_disks; |
276d77db AK |
8131 | |
8132 | /* check for reshape */ | |
8133 | if (this->reshape_active == 1) | |
8134 | recover_backup_imsm(st, this); | |
9a1608e5 | 8135 | rest = this; |
4f5bc454 DW |
8136 | } |
8137 | ||
b6180160 | 8138 | super->current_vol = current_vol; |
4f5bc454 | 8139 | return rest; |
cdddbdbc DW |
8140 | } |
8141 | ||
3b451610 AK |
8142 | static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev, |
8143 | int failed, int look_in_map) | |
c2a1e7da | 8144 | { |
3b451610 AK |
8145 | struct imsm_map *map; |
8146 | ||
8147 | map = get_imsm_map(dev, look_in_map); | |
c2a1e7da DW |
8148 | |
8149 | if (!failed) | |
1011e834 | 8150 | return map->map_state == IMSM_T_STATE_UNINITIALIZED ? |
3393c6af | 8151 | IMSM_T_STATE_UNINITIALIZED : IMSM_T_STATE_NORMAL; |
c2a1e7da DW |
8152 | |
8153 | switch (get_imsm_raid_level(map)) { | |
8154 | case 0: | |
8155 | return IMSM_T_STATE_FAILED; | |
8156 | break; | |
8157 | case 1: | |
8158 | if (failed < map->num_members) | |
8159 | return IMSM_T_STATE_DEGRADED; | |
8160 | else | |
8161 | return IMSM_T_STATE_FAILED; | |
8162 | break; | |
8163 | case 10: | |
8164 | { | |
8165 | /** | |
c92a2527 DW |
8166 | * check to see if any mirrors have failed, otherwise we |
8167 | * are degraded. Even numbered slots are mirrored on | |
8168 | * slot+1 | |
c2a1e7da | 8169 | */ |
c2a1e7da | 8170 | int i; |
d9b420a5 N |
8171 | /* gcc -Os complains that this is unused */ |
8172 | int insync = insync; | |
c2a1e7da DW |
8173 | |
8174 | for (i = 0; i < map->num_members; i++) { | |
238c0a71 | 8175 | __u32 ord = get_imsm_ord_tbl_ent(dev, i, MAP_X); |
c92a2527 DW |
8176 | int idx = ord_to_idx(ord); |
8177 | struct imsm_disk *disk; | |
c2a1e7da | 8178 | |
c92a2527 | 8179 | /* reset the potential in-sync count on even-numbered |
1011e834 | 8180 | * slots. num_copies is always 2 for imsm raid10 |
c92a2527 DW |
8181 | */ |
8182 | if ((i & 1) == 0) | |
8183 | insync = 2; | |
c2a1e7da | 8184 | |
c92a2527 | 8185 | disk = get_imsm_disk(super, idx); |
25ed7e59 | 8186 | if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD) |
c92a2527 | 8187 | insync--; |
c2a1e7da | 8188 | |
c92a2527 DW |
8189 | /* no in-sync disks left in this mirror the |
8190 | * array has failed | |
8191 | */ | |
8192 | if (insync == 0) | |
8193 | return IMSM_T_STATE_FAILED; | |
c2a1e7da DW |
8194 | } |
8195 | ||
8196 | return IMSM_T_STATE_DEGRADED; | |
8197 | } | |
8198 | case 5: | |
8199 | if (failed < 2) | |
8200 | return IMSM_T_STATE_DEGRADED; | |
8201 | else | |
8202 | return IMSM_T_STATE_FAILED; | |
8203 | break; | |
8204 | default: | |
8205 | break; | |
8206 | } | |
8207 | ||
8208 | return map->map_state; | |
8209 | } | |
8210 | ||
3b451610 AK |
8211 | static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev, |
8212 | int look_in_map) | |
c2a1e7da DW |
8213 | { |
8214 | int i; | |
8215 | int failed = 0; | |
8216 | struct imsm_disk *disk; | |
d5985138 AK |
8217 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
8218 | struct imsm_map *prev = get_imsm_map(dev, MAP_1); | |
68fe4598 | 8219 | struct imsm_map *map_for_loop; |
0556e1a2 DW |
8220 | __u32 ord; |
8221 | int idx; | |
d5985138 | 8222 | int idx_1; |
c2a1e7da | 8223 | |
0556e1a2 DW |
8224 | /* at the beginning of migration we set IMSM_ORD_REBUILD on |
8225 | * disks that are being rebuilt. New failures are recorded to | |
8226 | * map[0]. So we look through all the disks we started with and | |
8227 | * see if any failures are still present, or if any new ones | |
8228 | * have arrived | |
0556e1a2 | 8229 | */ |
d5985138 AK |
8230 | map_for_loop = map; |
8231 | if (prev && (map->num_members < prev->num_members)) | |
8232 | map_for_loop = prev; | |
68fe4598 LD |
8233 | |
8234 | for (i = 0; i < map_for_loop->num_members; i++) { | |
d5985138 | 8235 | idx_1 = -255; |
238c0a71 AK |
8236 | /* when MAP_X is passed both maps failures are counted |
8237 | */ | |
d5985138 | 8238 | if (prev && |
089f9d79 JS |
8239 | (look_in_map == MAP_1 || look_in_map == MAP_X) && |
8240 | i < prev->num_members) { | |
d5985138 AK |
8241 | ord = __le32_to_cpu(prev->disk_ord_tbl[i]); |
8242 | idx_1 = ord_to_idx(ord); | |
c2a1e7da | 8243 | |
d5985138 AK |
8244 | disk = get_imsm_disk(super, idx_1); |
8245 | if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD) | |
8246 | failed++; | |
8247 | } | |
089f9d79 JS |
8248 | if ((look_in_map == MAP_0 || look_in_map == MAP_X) && |
8249 | i < map->num_members) { | |
d5985138 AK |
8250 | ord = __le32_to_cpu(map->disk_ord_tbl[i]); |
8251 | idx = ord_to_idx(ord); | |
8252 | ||
8253 | if (idx != idx_1) { | |
8254 | disk = get_imsm_disk(super, idx); | |
8255 | if (!disk || is_failed(disk) || | |
8256 | ord & IMSM_ORD_REBUILD) | |
8257 | failed++; | |
8258 | } | |
8259 | } | |
c2a1e7da DW |
8260 | } |
8261 | ||
8262 | return failed; | |
845dea95 NB |
8263 | } |
8264 | ||
97b4d0e9 DW |
8265 | static int imsm_open_new(struct supertype *c, struct active_array *a, |
8266 | char *inst) | |
8267 | { | |
8268 | struct intel_super *super = c->sb; | |
8269 | struct imsm_super *mpb = super->anchor; | |
bbab0940 | 8270 | struct imsm_update_prealloc_bb_mem u; |
9587c373 | 8271 | |
97b4d0e9 | 8272 | if (atoi(inst) >= mpb->num_raid_devs) { |
1ade5cc1 | 8273 | pr_err("subarry index %d, out of range\n", atoi(inst)); |
97b4d0e9 DW |
8274 | return -ENODEV; |
8275 | } | |
8276 | ||
8277 | dprintf("imsm: open_new %s\n", inst); | |
8278 | a->info.container_member = atoi(inst); | |
bbab0940 TM |
8279 | |
8280 | u.type = update_prealloc_badblocks_mem; | |
8281 | imsm_update_metadata_locally(c, &u, sizeof(u)); | |
8282 | ||
97b4d0e9 DW |
8283 | return 0; |
8284 | } | |
8285 | ||
0c046afd DW |
8286 | static int is_resyncing(struct imsm_dev *dev) |
8287 | { | |
8288 | struct imsm_map *migr_map; | |
8289 | ||
8290 | if (!dev->vol.migr_state) | |
8291 | return 0; | |
8292 | ||
1484e727 DW |
8293 | if (migr_type(dev) == MIGR_INIT || |
8294 | migr_type(dev) == MIGR_REPAIR) | |
0c046afd DW |
8295 | return 1; |
8296 | ||
4c9bc37b AK |
8297 | if (migr_type(dev) == MIGR_GEN_MIGR) |
8298 | return 0; | |
8299 | ||
238c0a71 | 8300 | migr_map = get_imsm_map(dev, MAP_1); |
0c046afd | 8301 | |
089f9d79 JS |
8302 | if (migr_map->map_state == IMSM_T_STATE_NORMAL && |
8303 | dev->vol.migr_type != MIGR_GEN_MIGR) | |
0c046afd DW |
8304 | return 1; |
8305 | else | |
8306 | return 0; | |
8307 | } | |
8308 | ||
0556e1a2 | 8309 | /* return true if we recorded new information */ |
4c9e8c1e TM |
8310 | static int mark_failure(struct intel_super *super, |
8311 | struct imsm_dev *dev, struct imsm_disk *disk, int idx) | |
47ee5a45 | 8312 | { |
0556e1a2 DW |
8313 | __u32 ord; |
8314 | int slot; | |
8315 | struct imsm_map *map; | |
86c54047 DW |
8316 | char buf[MAX_RAID_SERIAL_LEN+3]; |
8317 | unsigned int len, shift = 0; | |
0556e1a2 DW |
8318 | |
8319 | /* new failures are always set in map[0] */ | |
238c0a71 | 8320 | map = get_imsm_map(dev, MAP_0); |
0556e1a2 DW |
8321 | |
8322 | slot = get_imsm_disk_slot(map, idx); | |
8323 | if (slot < 0) | |
8324 | return 0; | |
8325 | ||
8326 | ord = __le32_to_cpu(map->disk_ord_tbl[slot]); | |
25ed7e59 | 8327 | if (is_failed(disk) && (ord & IMSM_ORD_REBUILD)) |
0556e1a2 DW |
8328 | return 0; |
8329 | ||
7d0c5e24 LD |
8330 | memcpy(buf, disk->serial, MAX_RAID_SERIAL_LEN); |
8331 | buf[MAX_RAID_SERIAL_LEN] = '\000'; | |
8332 | strcat(buf, ":0"); | |
86c54047 DW |
8333 | if ((len = strlen(buf)) >= MAX_RAID_SERIAL_LEN) |
8334 | shift = len - MAX_RAID_SERIAL_LEN + 1; | |
167d8bb8 | 8335 | memcpy(disk->serial, &buf[shift], len + 1 - shift); |
86c54047 | 8336 | |
f2f27e63 | 8337 | disk->status |= FAILED_DISK; |
0556e1a2 | 8338 | set_imsm_ord_tbl_ent(map, slot, idx | IMSM_ORD_REBUILD); |
17788994 AK |
8339 | /* mark failures in second map if second map exists and this disk |
8340 | * in this slot. | |
8341 | * This is valid for migration, initialization and rebuild | |
8342 | */ | |
8343 | if (dev->vol.migr_state) { | |
238c0a71 | 8344 | struct imsm_map *map2 = get_imsm_map(dev, MAP_1); |
0a108d63 AK |
8345 | int slot2 = get_imsm_disk_slot(map2, idx); |
8346 | ||
089f9d79 | 8347 | if (slot2 < map2->num_members && slot2 >= 0) |
0a108d63 | 8348 | set_imsm_ord_tbl_ent(map2, slot2, |
1ace8403 AK |
8349 | idx | IMSM_ORD_REBUILD); |
8350 | } | |
d7a1fda2 MT |
8351 | if (map->failed_disk_num == 0xff || |
8352 | (!is_rebuilding(dev) && map->failed_disk_num > slot)) | |
0556e1a2 | 8353 | map->failed_disk_num = slot; |
4c9e8c1e TM |
8354 | |
8355 | clear_disk_badblocks(super->bbm_log, ord_to_idx(ord)); | |
8356 | ||
0556e1a2 DW |
8357 | return 1; |
8358 | } | |
8359 | ||
4c9e8c1e TM |
8360 | static void mark_missing(struct intel_super *super, |
8361 | struct imsm_dev *dev, struct imsm_disk *disk, int idx) | |
0556e1a2 | 8362 | { |
4c9e8c1e | 8363 | mark_failure(super, dev, disk, idx); |
0556e1a2 DW |
8364 | |
8365 | if (disk->scsi_id == __cpu_to_le32(~(__u32)0)) | |
8366 | return; | |
8367 | ||
47ee5a45 DW |
8368 | disk->scsi_id = __cpu_to_le32(~(__u32)0); |
8369 | memmove(&disk->serial[0], &disk->serial[1], MAX_RAID_SERIAL_LEN - 1); | |
8370 | } | |
8371 | ||
33414a01 DW |
8372 | static void handle_missing(struct intel_super *super, struct imsm_dev *dev) |
8373 | { | |
33414a01 | 8374 | struct dl *dl; |
33414a01 DW |
8375 | |
8376 | if (!super->missing) | |
8377 | return; | |
33414a01 | 8378 | |
79b68f1b PC |
8379 | /* When orom adds replacement for missing disk it does |
8380 | * not remove entry of missing disk, but just updates map with | |
8381 | * new added disk. So it is not enough just to test if there is | |
8382 | * any missing disk, we have to look if there are any failed disks | |
8383 | * in map to stop migration */ | |
8384 | ||
33414a01 | 8385 | dprintf("imsm: mark missing\n"); |
3d59f0c0 AK |
8386 | /* end process for initialization and rebuild only |
8387 | */ | |
8388 | if (is_gen_migration(dev) == 0) { | |
fb12a745 | 8389 | int failed = imsm_count_failed(super, dev, MAP_0); |
3d59f0c0 | 8390 | |
fb12a745 TM |
8391 | if (failed) { |
8392 | __u8 map_state; | |
8393 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
8394 | struct imsm_map *map1; | |
8395 | int i, ord, ord_map1; | |
8396 | int rebuilt = 1; | |
3d59f0c0 | 8397 | |
fb12a745 TM |
8398 | for (i = 0; i < map->num_members; i++) { |
8399 | ord = get_imsm_ord_tbl_ent(dev, i, MAP_0); | |
8400 | if (!(ord & IMSM_ORD_REBUILD)) | |
8401 | continue; | |
8402 | ||
8403 | map1 = get_imsm_map(dev, MAP_1); | |
8404 | if (!map1) | |
8405 | continue; | |
8406 | ||
8407 | ord_map1 = __le32_to_cpu(map1->disk_ord_tbl[i]); | |
8408 | if (ord_map1 & IMSM_ORD_REBUILD) | |
8409 | rebuilt = 0; | |
8410 | } | |
8411 | ||
8412 | if (rebuilt) { | |
8413 | map_state = imsm_check_degraded(super, dev, | |
8414 | failed, MAP_0); | |
8415 | end_migration(dev, super, map_state); | |
8416 | } | |
8417 | } | |
3d59f0c0 | 8418 | } |
33414a01 | 8419 | for (dl = super->missing; dl; dl = dl->next) |
4c9e8c1e | 8420 | mark_missing(super, dev, &dl->disk, dl->index); |
33414a01 DW |
8421 | super->updates_pending++; |
8422 | } | |
8423 | ||
f3871fdc AK |
8424 | static unsigned long long imsm_set_array_size(struct imsm_dev *dev, |
8425 | long long new_size) | |
70bdf0dc | 8426 | { |
70bdf0dc | 8427 | unsigned long long array_blocks; |
9529d343 MD |
8428 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
8429 | int used_disks = imsm_num_data_members(map); | |
70bdf0dc AK |
8430 | |
8431 | if (used_disks == 0) { | |
8432 | /* when problems occures | |
8433 | * return current array_blocks value | |
8434 | */ | |
fcc2c9da | 8435 | array_blocks = imsm_dev_size(dev); |
70bdf0dc AK |
8436 | |
8437 | return array_blocks; | |
8438 | } | |
8439 | ||
8440 | /* set array size in metadata | |
8441 | */ | |
9529d343 | 8442 | if (new_size <= 0) |
f3871fdc AK |
8443 | /* OLCE size change is caused by added disks |
8444 | */ | |
44490938 | 8445 | array_blocks = per_dev_array_size(map) * used_disks; |
9529d343 | 8446 | else |
f3871fdc AK |
8447 | /* Online Volume Size Change |
8448 | * Using available free space | |
8449 | */ | |
8450 | array_blocks = new_size; | |
70bdf0dc | 8451 | |
b53bfba6 | 8452 | array_blocks = round_size_to_mb(array_blocks, used_disks); |
fcc2c9da | 8453 | set_imsm_dev_size(dev, array_blocks); |
70bdf0dc AK |
8454 | |
8455 | return array_blocks; | |
8456 | } | |
8457 | ||
28bce06f AK |
8458 | static void imsm_set_disk(struct active_array *a, int n, int state); |
8459 | ||
0e2d1a4e AK |
8460 | static void imsm_progress_container_reshape(struct intel_super *super) |
8461 | { | |
8462 | /* if no device has a migr_state, but some device has a | |
8463 | * different number of members than the previous device, start | |
8464 | * changing the number of devices in this device to match | |
8465 | * previous. | |
8466 | */ | |
8467 | struct imsm_super *mpb = super->anchor; | |
8468 | int prev_disks = -1; | |
8469 | int i; | |
1dfaa380 | 8470 | int copy_map_size; |
0e2d1a4e AK |
8471 | |
8472 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
8473 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
238c0a71 | 8474 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
0e2d1a4e AK |
8475 | struct imsm_map *map2; |
8476 | int prev_num_members; | |
0e2d1a4e AK |
8477 | |
8478 | if (dev->vol.migr_state) | |
8479 | return; | |
8480 | ||
8481 | if (prev_disks == -1) | |
8482 | prev_disks = map->num_members; | |
8483 | if (prev_disks == map->num_members) | |
8484 | continue; | |
8485 | ||
8486 | /* OK, this array needs to enter reshape mode. | |
8487 | * i.e it needs a migr_state | |
8488 | */ | |
8489 | ||
1dfaa380 | 8490 | copy_map_size = sizeof_imsm_map(map); |
0e2d1a4e AK |
8491 | prev_num_members = map->num_members; |
8492 | map->num_members = prev_disks; | |
8493 | dev->vol.migr_state = 1; | |
4036e7ee | 8494 | set_vol_curr_migr_unit(dev, 0); |
ea672ee1 | 8495 | set_migr_type(dev, MIGR_GEN_MIGR); |
0e2d1a4e AK |
8496 | for (i = prev_num_members; |
8497 | i < map->num_members; i++) | |
8498 | set_imsm_ord_tbl_ent(map, i, i); | |
238c0a71 | 8499 | map2 = get_imsm_map(dev, MAP_1); |
0e2d1a4e | 8500 | /* Copy the current map */ |
1dfaa380 | 8501 | memcpy(map2, map, copy_map_size); |
0e2d1a4e AK |
8502 | map2->num_members = prev_num_members; |
8503 | ||
f3871fdc | 8504 | imsm_set_array_size(dev, -1); |
51d83f5d | 8505 | super->clean_migration_record_by_mdmon = 1; |
0e2d1a4e AK |
8506 | super->updates_pending++; |
8507 | } | |
8508 | } | |
8509 | ||
aad6f216 | 8510 | /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild |
0c046afd DW |
8511 | * states are handled in imsm_set_disk() with one exception, when a |
8512 | * resync is stopped due to a new failure this routine will set the | |
8513 | * 'degraded' state for the array. | |
8514 | */ | |
01f157d7 | 8515 | static int imsm_set_array_state(struct active_array *a, int consistent) |
a862209d DW |
8516 | { |
8517 | int inst = a->info.container_member; | |
8518 | struct intel_super *super = a->container->sb; | |
949c47a0 | 8519 | struct imsm_dev *dev = get_imsm_dev(super, inst); |
238c0a71 | 8520 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
3b451610 AK |
8521 | int failed = imsm_count_failed(super, dev, MAP_0); |
8522 | __u8 map_state = imsm_check_degraded(super, dev, failed, MAP_0); | |
1e5c6983 | 8523 | __u32 blocks_per_unit; |
a862209d | 8524 | |
1af97990 AK |
8525 | if (dev->vol.migr_state && |
8526 | dev->vol.migr_type == MIGR_GEN_MIGR) { | |
8527 | /* array state change is blocked due to reshape action | |
aad6f216 N |
8528 | * We might need to |
8529 | * - abort the reshape (if last_checkpoint is 0 and action!= reshape) | |
8530 | * - finish the reshape (if last_checkpoint is big and action != reshape) | |
4036e7ee | 8531 | * - update vol_curr_migr_unit |
1af97990 | 8532 | */ |
aad6f216 | 8533 | if (a->curr_action == reshape) { |
4036e7ee | 8534 | /* still reshaping, maybe update vol_curr_migr_unit */ |
633b5610 | 8535 | goto mark_checkpoint; |
aad6f216 N |
8536 | } else { |
8537 | if (a->last_checkpoint == 0 && a->prev_action == reshape) { | |
8538 | /* for some reason we aborted the reshape. | |
b66e591b AK |
8539 | * |
8540 | * disable automatic metadata rollback | |
8541 | * user action is required to recover process | |
aad6f216 | 8542 | */ |
b66e591b | 8543 | if (0) { |
238c0a71 AK |
8544 | struct imsm_map *map2 = |
8545 | get_imsm_map(dev, MAP_1); | |
8546 | dev->vol.migr_state = 0; | |
8547 | set_migr_type(dev, 0); | |
4036e7ee | 8548 | set_vol_curr_migr_unit(dev, 0); |
238c0a71 AK |
8549 | memcpy(map, map2, |
8550 | sizeof_imsm_map(map2)); | |
8551 | super->updates_pending++; | |
b66e591b | 8552 | } |
aad6f216 N |
8553 | } |
8554 | if (a->last_checkpoint >= a->info.component_size) { | |
8555 | unsigned long long array_blocks; | |
8556 | int used_disks; | |
e154ced3 | 8557 | struct mdinfo *mdi; |
aad6f216 | 8558 | |
9529d343 | 8559 | used_disks = imsm_num_data_members(map); |
d55adef9 AK |
8560 | if (used_disks > 0) { |
8561 | array_blocks = | |
44490938 | 8562 | per_dev_array_size(map) * |
d55adef9 | 8563 | used_disks; |
b53bfba6 TM |
8564 | array_blocks = |
8565 | round_size_to_mb(array_blocks, | |
8566 | used_disks); | |
d55adef9 AK |
8567 | a->info.custom_array_size = array_blocks; |
8568 | /* encourage manager to update array | |
8569 | * size | |
8570 | */ | |
e154ced3 | 8571 | |
d55adef9 | 8572 | a->check_reshape = 1; |
633b5610 | 8573 | } |
e154ced3 AK |
8574 | /* finalize online capacity expansion/reshape */ |
8575 | for (mdi = a->info.devs; mdi; mdi = mdi->next) | |
8576 | imsm_set_disk(a, | |
8577 | mdi->disk.raid_disk, | |
8578 | mdi->curr_state); | |
8579 | ||
0e2d1a4e | 8580 | imsm_progress_container_reshape(super); |
e154ced3 | 8581 | } |
aad6f216 | 8582 | } |
1af97990 AK |
8583 | } |
8584 | ||
47ee5a45 | 8585 | /* before we activate this array handle any missing disks */ |
33414a01 DW |
8586 | if (consistent == 2) |
8587 | handle_missing(super, dev); | |
1e5c6983 | 8588 | |
0c046afd | 8589 | if (consistent == 2 && |
b7941fd6 | 8590 | (!is_resync_complete(&a->info) || |
0c046afd DW |
8591 | map_state != IMSM_T_STATE_NORMAL || |
8592 | dev->vol.migr_state)) | |
01f157d7 | 8593 | consistent = 0; |
272906ef | 8594 | |
b7941fd6 | 8595 | if (is_resync_complete(&a->info)) { |
0c046afd | 8596 | /* complete intialization / resync, |
0556e1a2 DW |
8597 | * recovery and interrupted recovery is completed in |
8598 | * ->set_disk | |
0c046afd DW |
8599 | */ |
8600 | if (is_resyncing(dev)) { | |
8601 | dprintf("imsm: mark resync done\n"); | |
809da78e | 8602 | end_migration(dev, super, map_state); |
115c3803 | 8603 | super->updates_pending++; |
484240d8 | 8604 | a->last_checkpoint = 0; |
115c3803 | 8605 | } |
b9172665 AK |
8606 | } else if ((!is_resyncing(dev) && !failed) && |
8607 | (imsm_reshape_blocks_arrays_changes(super) == 0)) { | |
0c046afd | 8608 | /* mark the start of the init process if nothing is failed */ |
b7941fd6 | 8609 | dprintf("imsm: mark resync start\n"); |
1484e727 | 8610 | if (map->map_state == IMSM_T_STATE_UNINITIALIZED) |
8e59f3d8 | 8611 | migrate(dev, super, IMSM_T_STATE_NORMAL, MIGR_INIT); |
1484e727 | 8612 | else |
8e59f3d8 | 8613 | migrate(dev, super, IMSM_T_STATE_NORMAL, MIGR_REPAIR); |
3393c6af | 8614 | super->updates_pending++; |
115c3803 | 8615 | } |
a862209d | 8616 | |
633b5610 | 8617 | mark_checkpoint: |
5b83bacf AK |
8618 | /* skip checkpointing for general migration, |
8619 | * it is controlled in mdadm | |
8620 | */ | |
8621 | if (is_gen_migration(dev)) | |
8622 | goto skip_mark_checkpoint; | |
8623 | ||
4036e7ee MT |
8624 | /* check if we can update vol_curr_migr_unit from resync_start, |
8625 | * recovery_start | |
8626 | */ | |
c47b0ff6 | 8627 | blocks_per_unit = blocks_per_migr_unit(super, dev); |
4f0a7acc | 8628 | if (blocks_per_unit) { |
4036e7ee MT |
8629 | set_vol_curr_migr_unit(dev, |
8630 | a->last_checkpoint / blocks_per_unit); | |
8631 | dprintf("imsm: mark checkpoint (%llu)\n", | |
8632 | vol_curr_migr_unit(dev)); | |
8633 | super->updates_pending++; | |
1e5c6983 | 8634 | } |
f8f603f1 | 8635 | |
5b83bacf | 8636 | skip_mark_checkpoint: |
3393c6af | 8637 | /* mark dirty / clean */ |
2432ce9b AP |
8638 | if (((dev->vol.dirty & RAIDVOL_DIRTY) && consistent) || |
8639 | (!(dev->vol.dirty & RAIDVOL_DIRTY) && !consistent)) { | |
b7941fd6 | 8640 | dprintf("imsm: mark '%s'\n", consistent ? "clean" : "dirty"); |
2432ce9b AP |
8641 | if (consistent) { |
8642 | dev->vol.dirty = RAIDVOL_CLEAN; | |
8643 | } else { | |
8644 | dev->vol.dirty = RAIDVOL_DIRTY; | |
c2462068 PB |
8645 | if (dev->rwh_policy == RWH_DISTRIBUTED || |
8646 | dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED) | |
2432ce9b AP |
8647 | dev->vol.dirty |= RAIDVOL_DSRECORD_VALID; |
8648 | } | |
a862209d DW |
8649 | super->updates_pending++; |
8650 | } | |
28bce06f | 8651 | |
01f157d7 | 8652 | return consistent; |
a862209d DW |
8653 | } |
8654 | ||
6f50473f TM |
8655 | static int imsm_disk_slot_to_ord(struct active_array *a, int slot) |
8656 | { | |
8657 | int inst = a->info.container_member; | |
8658 | struct intel_super *super = a->container->sb; | |
8659 | struct imsm_dev *dev = get_imsm_dev(super, inst); | |
8660 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
8661 | ||
8662 | if (slot > map->num_members) { | |
8663 | pr_err("imsm: imsm_disk_slot_to_ord %d out of range 0..%d\n", | |
8664 | slot, map->num_members - 1); | |
8665 | return -1; | |
8666 | } | |
8667 | ||
8668 | if (slot < 0) | |
8669 | return -1; | |
8670 | ||
8671 | return get_imsm_ord_tbl_ent(dev, slot, MAP_0); | |
8672 | } | |
8673 | ||
8d45d196 | 8674 | static void imsm_set_disk(struct active_array *a, int n, int state) |
845dea95 | 8675 | { |
8d45d196 DW |
8676 | int inst = a->info.container_member; |
8677 | struct intel_super *super = a->container->sb; | |
949c47a0 | 8678 | struct imsm_dev *dev = get_imsm_dev(super, inst); |
238c0a71 | 8679 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
8d45d196 | 8680 | struct imsm_disk *disk; |
7ce05701 LD |
8681 | struct mdinfo *mdi; |
8682 | int recovery_not_finished = 0; | |
0c046afd | 8683 | int failed; |
6f50473f | 8684 | int ord; |
0c046afd | 8685 | __u8 map_state; |
fb12a745 TM |
8686 | int rebuild_done = 0; |
8687 | int i; | |
8d45d196 | 8688 | |
fb12a745 | 8689 | ord = get_imsm_ord_tbl_ent(dev, n, MAP_X); |
6f50473f | 8690 | if (ord < 0) |
8d45d196 DW |
8691 | return; |
8692 | ||
4e6e574a | 8693 | dprintf("imsm: set_disk %d:%x\n", n, state); |
b10b37b8 | 8694 | disk = get_imsm_disk(super, ord_to_idx(ord)); |
8d45d196 | 8695 | |
5802a811 | 8696 | /* check for new failures */ |
ae7d61e3 | 8697 | if (disk && (state & DS_FAULTY)) { |
4c9e8c1e | 8698 | if (mark_failure(super, dev, disk, ord_to_idx(ord))) |
0556e1a2 | 8699 | super->updates_pending++; |
8d45d196 | 8700 | } |
47ee5a45 | 8701 | |
19859edc | 8702 | /* check if in_sync */ |
0556e1a2 | 8703 | if (state & DS_INSYNC && ord & IMSM_ORD_REBUILD && is_rebuilding(dev)) { |
238c0a71 | 8704 | struct imsm_map *migr_map = get_imsm_map(dev, MAP_1); |
b10b37b8 DW |
8705 | |
8706 | set_imsm_ord_tbl_ent(migr_map, n, ord_to_idx(ord)); | |
fb12a745 | 8707 | rebuild_done = 1; |
19859edc DW |
8708 | super->updates_pending++; |
8709 | } | |
8d45d196 | 8710 | |
3b451610 AK |
8711 | failed = imsm_count_failed(super, dev, MAP_0); |
8712 | map_state = imsm_check_degraded(super, dev, failed, MAP_0); | |
5802a811 | 8713 | |
0c046afd | 8714 | /* check if recovery complete, newly degraded, or failed */ |
94002678 AK |
8715 | dprintf("imsm: Detected transition to state "); |
8716 | switch (map_state) { | |
8717 | case IMSM_T_STATE_NORMAL: /* transition to normal state */ | |
8718 | dprintf("normal: "); | |
8719 | if (is_rebuilding(dev)) { | |
1ade5cc1 | 8720 | dprintf_cont("while rebuilding"); |
7ce05701 LD |
8721 | /* check if recovery is really finished */ |
8722 | for (mdi = a->info.devs; mdi ; mdi = mdi->next) | |
8723 | if (mdi->recovery_start != MaxSector) { | |
8724 | recovery_not_finished = 1; | |
8725 | break; | |
8726 | } | |
8727 | if (recovery_not_finished) { | |
1ade5cc1 N |
8728 | dprintf_cont("\n"); |
8729 | dprintf("Rebuild has not finished yet, state not changed"); | |
7ce05701 LD |
8730 | if (a->last_checkpoint < mdi->recovery_start) { |
8731 | a->last_checkpoint = mdi->recovery_start; | |
8732 | super->updates_pending++; | |
8733 | } | |
8734 | break; | |
8735 | } | |
94002678 | 8736 | end_migration(dev, super, map_state); |
94002678 AK |
8737 | map->failed_disk_num = ~0; |
8738 | super->updates_pending++; | |
8739 | a->last_checkpoint = 0; | |
8740 | break; | |
8741 | } | |
8742 | if (is_gen_migration(dev)) { | |
1ade5cc1 | 8743 | dprintf_cont("while general migration"); |
bf2f0071 | 8744 | if (a->last_checkpoint >= a->info.component_size) |
809da78e | 8745 | end_migration(dev, super, map_state); |
94002678 AK |
8746 | else |
8747 | map->map_state = map_state; | |
28bce06f | 8748 | map->failed_disk_num = ~0; |
94002678 | 8749 | super->updates_pending++; |
bf2f0071 | 8750 | break; |
94002678 AK |
8751 | } |
8752 | break; | |
8753 | case IMSM_T_STATE_DEGRADED: /* transition to degraded state */ | |
1ade5cc1 | 8754 | dprintf_cont("degraded: "); |
089f9d79 | 8755 | if (map->map_state != map_state && !dev->vol.migr_state) { |
1ade5cc1 | 8756 | dprintf_cont("mark degraded"); |
94002678 AK |
8757 | map->map_state = map_state; |
8758 | super->updates_pending++; | |
8759 | a->last_checkpoint = 0; | |
8760 | break; | |
8761 | } | |
8762 | if (is_rebuilding(dev)) { | |
d7a1fda2 | 8763 | dprintf_cont("while rebuilding "); |
a4e96fd8 MT |
8764 | if (state & DS_FAULTY) { |
8765 | dprintf_cont("removing failed drive "); | |
d7a1fda2 MT |
8766 | if (n == map->failed_disk_num) { |
8767 | dprintf_cont("end migration"); | |
8768 | end_migration(dev, super, map_state); | |
a4e96fd8 | 8769 | a->last_checkpoint = 0; |
d7a1fda2 | 8770 | } else { |
a4e96fd8 | 8771 | dprintf_cont("fail detected during rebuild, changing map state"); |
d7a1fda2 MT |
8772 | map->map_state = map_state; |
8773 | } | |
94002678 | 8774 | super->updates_pending++; |
fb12a745 TM |
8775 | } |
8776 | ||
a4e96fd8 MT |
8777 | if (!rebuild_done) |
8778 | break; | |
8779 | ||
fb12a745 TM |
8780 | /* check if recovery is really finished */ |
8781 | for (mdi = a->info.devs; mdi ; mdi = mdi->next) | |
8782 | if (mdi->recovery_start != MaxSector) { | |
8783 | recovery_not_finished = 1; | |
8784 | break; | |
8785 | } | |
8786 | if (recovery_not_finished) { | |
8787 | dprintf_cont("\n"); | |
a4e96fd8 | 8788 | dprintf_cont("Rebuild has not finished yet"); |
fb12a745 TM |
8789 | if (a->last_checkpoint < mdi->recovery_start) { |
8790 | a->last_checkpoint = | |
8791 | mdi->recovery_start; | |
8792 | super->updates_pending++; | |
8793 | } | |
8794 | break; | |
94002678 | 8795 | } |
fb12a745 TM |
8796 | |
8797 | dprintf_cont(" Rebuild done, still degraded"); | |
a4e96fd8 MT |
8798 | end_migration(dev, super, map_state); |
8799 | a->last_checkpoint = 0; | |
8800 | super->updates_pending++; | |
fb12a745 TM |
8801 | |
8802 | for (i = 0; i < map->num_members; i++) { | |
8803 | int idx = get_imsm_ord_tbl_ent(dev, i, MAP_0); | |
8804 | ||
8805 | if (idx & IMSM_ORD_REBUILD) | |
8806 | map->failed_disk_num = i; | |
8807 | } | |
8808 | super->updates_pending++; | |
94002678 AK |
8809 | break; |
8810 | } | |
8811 | if (is_gen_migration(dev)) { | |
1ade5cc1 | 8812 | dprintf_cont("while general migration"); |
bf2f0071 | 8813 | if (a->last_checkpoint >= a->info.component_size) |
809da78e | 8814 | end_migration(dev, super, map_state); |
94002678 AK |
8815 | else { |
8816 | map->map_state = map_state; | |
3b451610 | 8817 | manage_second_map(super, dev); |
94002678 AK |
8818 | } |
8819 | super->updates_pending++; | |
bf2f0071 | 8820 | break; |
28bce06f | 8821 | } |
6ce1fbf1 | 8822 | if (is_initializing(dev)) { |
1ade5cc1 | 8823 | dprintf_cont("while initialization."); |
6ce1fbf1 AK |
8824 | map->map_state = map_state; |
8825 | super->updates_pending++; | |
8826 | break; | |
8827 | } | |
94002678 AK |
8828 | break; |
8829 | case IMSM_T_STATE_FAILED: /* transition to failed state */ | |
1ade5cc1 | 8830 | dprintf_cont("failed: "); |
94002678 | 8831 | if (is_gen_migration(dev)) { |
1ade5cc1 | 8832 | dprintf_cont("while general migration"); |
94002678 AK |
8833 | map->map_state = map_state; |
8834 | super->updates_pending++; | |
8835 | break; | |
8836 | } | |
8837 | if (map->map_state != map_state) { | |
1ade5cc1 | 8838 | dprintf_cont("mark failed"); |
94002678 AK |
8839 | end_migration(dev, super, map_state); |
8840 | super->updates_pending++; | |
8841 | a->last_checkpoint = 0; | |
8842 | break; | |
8843 | } | |
8844 | break; | |
8845 | default: | |
1ade5cc1 | 8846 | dprintf_cont("state %i\n", map_state); |
5802a811 | 8847 | } |
1ade5cc1 | 8848 | dprintf_cont("\n"); |
845dea95 NB |
8849 | } |
8850 | ||
f796af5d | 8851 | static int store_imsm_mpb(int fd, struct imsm_super *mpb) |
c2a1e7da | 8852 | { |
f796af5d | 8853 | void *buf = mpb; |
c2a1e7da DW |
8854 | __u32 mpb_size = __le32_to_cpu(mpb->mpb_size); |
8855 | unsigned long long dsize; | |
8856 | unsigned long long sectors; | |
f36a9ecd | 8857 | unsigned int sector_size; |
c2a1e7da | 8858 | |
aec01630 JS |
8859 | if (!get_dev_sector_size(fd, NULL, §or_size)) |
8860 | return 1; | |
c2a1e7da DW |
8861 | get_dev_size(fd, NULL, &dsize); |
8862 | ||
f36a9ecd | 8863 | if (mpb_size > sector_size) { |
272f648f | 8864 | /* -1 to account for anchor */ |
f36a9ecd | 8865 | sectors = mpb_sectors(mpb, sector_size) - 1; |
c2a1e7da | 8866 | |
272f648f | 8867 | /* write the extended mpb to the sectors preceeding the anchor */ |
f36a9ecd PB |
8868 | if (lseek64(fd, dsize - (sector_size * (2 + sectors)), |
8869 | SEEK_SET) < 0) | |
272f648f | 8870 | return 1; |
c2a1e7da | 8871 | |
f36a9ecd PB |
8872 | if ((unsigned long long)write(fd, buf + sector_size, |
8873 | sector_size * sectors) != sector_size * sectors) | |
272f648f DW |
8874 | return 1; |
8875 | } | |
c2a1e7da | 8876 | |
272f648f | 8877 | /* first block is stored on second to last sector of the disk */ |
f36a9ecd | 8878 | if (lseek64(fd, dsize - (sector_size * 2), SEEK_SET) < 0) |
c2a1e7da DW |
8879 | return 1; |
8880 | ||
466070ad | 8881 | if ((unsigned int)write(fd, buf, sector_size) != sector_size) |
c2a1e7da DW |
8882 | return 1; |
8883 | ||
c2a1e7da DW |
8884 | return 0; |
8885 | } | |
8886 | ||
2e735d19 | 8887 | static void imsm_sync_metadata(struct supertype *container) |
845dea95 | 8888 | { |
2e735d19 | 8889 | struct intel_super *super = container->sb; |
c2a1e7da | 8890 | |
1a64be56 | 8891 | dprintf("sync metadata: %d\n", super->updates_pending); |
c2a1e7da DW |
8892 | if (!super->updates_pending) |
8893 | return; | |
8894 | ||
36988a3d | 8895 | write_super_imsm(container, 0); |
c2a1e7da DW |
8896 | |
8897 | super->updates_pending = 0; | |
845dea95 NB |
8898 | } |
8899 | ||
272906ef DW |
8900 | static struct dl *imsm_readd(struct intel_super *super, int idx, struct active_array *a) |
8901 | { | |
8902 | struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member); | |
238c0a71 | 8903 | int i = get_imsm_disk_idx(dev, idx, MAP_X); |
272906ef DW |
8904 | struct dl *dl; |
8905 | ||
8906 | for (dl = super->disks; dl; dl = dl->next) | |
8907 | if (dl->index == i) | |
8908 | break; | |
8909 | ||
25ed7e59 | 8910 | if (dl && is_failed(&dl->disk)) |
272906ef DW |
8911 | dl = NULL; |
8912 | ||
8913 | if (dl) | |
1ade5cc1 | 8914 | dprintf("found %x:%x\n", dl->major, dl->minor); |
272906ef DW |
8915 | |
8916 | return dl; | |
8917 | } | |
8918 | ||
a20d2ba5 | 8919 | static struct dl *imsm_add_spare(struct intel_super *super, int slot, |
8ba77d32 AK |
8920 | struct active_array *a, int activate_new, |
8921 | struct mdinfo *additional_test_list) | |
272906ef DW |
8922 | { |
8923 | struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member); | |
238c0a71 | 8924 | int idx = get_imsm_disk_idx(dev, slot, MAP_X); |
a20d2ba5 DW |
8925 | struct imsm_super *mpb = super->anchor; |
8926 | struct imsm_map *map; | |
272906ef DW |
8927 | unsigned long long pos; |
8928 | struct mdinfo *d; | |
8929 | struct extent *ex; | |
a20d2ba5 | 8930 | int i, j; |
272906ef | 8931 | int found; |
569cc43f DW |
8932 | __u32 array_start = 0; |
8933 | __u32 array_end = 0; | |
272906ef | 8934 | struct dl *dl; |
6c932028 | 8935 | struct mdinfo *test_list; |
272906ef DW |
8936 | |
8937 | for (dl = super->disks; dl; dl = dl->next) { | |
8938 | /* If in this array, skip */ | |
8939 | for (d = a->info.devs ; d ; d = d->next) | |
e553d2a4 DW |
8940 | if (d->state_fd >= 0 && |
8941 | d->disk.major == dl->major && | |
272906ef | 8942 | d->disk.minor == dl->minor) { |
8ba77d32 AK |
8943 | dprintf("%x:%x already in array\n", |
8944 | dl->major, dl->minor); | |
272906ef DW |
8945 | break; |
8946 | } | |
8947 | if (d) | |
8948 | continue; | |
6c932028 AK |
8949 | test_list = additional_test_list; |
8950 | while (test_list) { | |
8951 | if (test_list->disk.major == dl->major && | |
8952 | test_list->disk.minor == dl->minor) { | |
8ba77d32 AK |
8953 | dprintf("%x:%x already in additional test list\n", |
8954 | dl->major, dl->minor); | |
8955 | break; | |
8956 | } | |
6c932028 | 8957 | test_list = test_list->next; |
8ba77d32 | 8958 | } |
6c932028 | 8959 | if (test_list) |
8ba77d32 | 8960 | continue; |
272906ef | 8961 | |
e553d2a4 | 8962 | /* skip in use or failed drives */ |
25ed7e59 | 8963 | if (is_failed(&dl->disk) || idx == dl->index || |
df474657 DW |
8964 | dl->index == -2) { |
8965 | dprintf("%x:%x status (failed: %d index: %d)\n", | |
25ed7e59 | 8966 | dl->major, dl->minor, is_failed(&dl->disk), idx); |
9a1608e5 DW |
8967 | continue; |
8968 | } | |
8969 | ||
a20d2ba5 DW |
8970 | /* skip pure spares when we are looking for partially |
8971 | * assimilated drives | |
8972 | */ | |
8973 | if (dl->index == -1 && !activate_new) | |
8974 | continue; | |
8975 | ||
f2cc4f7d AO |
8976 | if (!drive_validate_sector_size(super, dl)) |
8977 | continue; | |
8978 | ||
272906ef | 8979 | /* Does this unused device have the requisite free space? |
a20d2ba5 | 8980 | * It needs to be able to cover all member volumes |
272906ef | 8981 | */ |
05501181 | 8982 | ex = get_extents(super, dl, 1); |
272906ef DW |
8983 | if (!ex) { |
8984 | dprintf("cannot get extents\n"); | |
8985 | continue; | |
8986 | } | |
a20d2ba5 DW |
8987 | for (i = 0; i < mpb->num_raid_devs; i++) { |
8988 | dev = get_imsm_dev(super, i); | |
238c0a71 | 8989 | map = get_imsm_map(dev, MAP_0); |
272906ef | 8990 | |
a20d2ba5 DW |
8991 | /* check if this disk is already a member of |
8992 | * this array | |
272906ef | 8993 | */ |
620b1713 | 8994 | if (get_imsm_disk_slot(map, dl->index) >= 0) |
a20d2ba5 DW |
8995 | continue; |
8996 | ||
8997 | found = 0; | |
8998 | j = 0; | |
8999 | pos = 0; | |
5551b113 | 9000 | array_start = pba_of_lba0(map); |
329c8278 | 9001 | array_end = array_start + |
44490938 | 9002 | per_dev_array_size(map) - 1; |
a20d2ba5 DW |
9003 | |
9004 | do { | |
9005 | /* check that we can start at pba_of_lba0 with | |
44490938 | 9006 | * num_data_stripes*blocks_per_stripe of space |
a20d2ba5 | 9007 | */ |
329c8278 | 9008 | if (array_start >= pos && array_end < ex[j].start) { |
a20d2ba5 DW |
9009 | found = 1; |
9010 | break; | |
9011 | } | |
9012 | pos = ex[j].start + ex[j].size; | |
9013 | j++; | |
9014 | } while (ex[j-1].size); | |
9015 | ||
9016 | if (!found) | |
272906ef | 9017 | break; |
a20d2ba5 | 9018 | } |
272906ef DW |
9019 | |
9020 | free(ex); | |
a20d2ba5 | 9021 | if (i < mpb->num_raid_devs) { |
329c8278 DW |
9022 | dprintf("%x:%x does not have %u to %u available\n", |
9023 | dl->major, dl->minor, array_start, array_end); | |
272906ef DW |
9024 | /* No room */ |
9025 | continue; | |
a20d2ba5 DW |
9026 | } |
9027 | return dl; | |
272906ef DW |
9028 | } |
9029 | ||
9030 | return dl; | |
9031 | } | |
9032 | ||
95d07a2c LM |
9033 | static int imsm_rebuild_allowed(struct supertype *cont, int dev_idx, int failed) |
9034 | { | |
9035 | struct imsm_dev *dev2; | |
9036 | struct imsm_map *map; | |
9037 | struct dl *idisk; | |
9038 | int slot; | |
9039 | int idx; | |
9040 | __u8 state; | |
9041 | ||
9042 | dev2 = get_imsm_dev(cont->sb, dev_idx); | |
9043 | if (dev2) { | |
238c0a71 | 9044 | state = imsm_check_degraded(cont->sb, dev2, failed, MAP_0); |
95d07a2c | 9045 | if (state == IMSM_T_STATE_FAILED) { |
238c0a71 | 9046 | map = get_imsm_map(dev2, MAP_0); |
95d07a2c LM |
9047 | if (!map) |
9048 | return 1; | |
9049 | for (slot = 0; slot < map->num_members; slot++) { | |
9050 | /* | |
9051 | * Check if failed disks are deleted from intel | |
9052 | * disk list or are marked to be deleted | |
9053 | */ | |
238c0a71 | 9054 | idx = get_imsm_disk_idx(dev2, slot, MAP_X); |
95d07a2c LM |
9055 | idisk = get_imsm_dl_disk(cont->sb, idx); |
9056 | /* | |
9057 | * Do not rebuild the array if failed disks | |
9058 | * from failed sub-array are not removed from | |
9059 | * container. | |
9060 | */ | |
9061 | if (idisk && | |
9062 | is_failed(&idisk->disk) && | |
9063 | (idisk->action != DISK_REMOVE)) | |
9064 | return 0; | |
9065 | } | |
9066 | } | |
9067 | } | |
9068 | return 1; | |
9069 | } | |
9070 | ||
88758e9d DW |
9071 | static struct mdinfo *imsm_activate_spare(struct active_array *a, |
9072 | struct metadata_update **updates) | |
9073 | { | |
9074 | /** | |
d23fe947 DW |
9075 | * Find a device with unused free space and use it to replace a |
9076 | * failed/vacant region in an array. We replace failed regions one a | |
9077 | * array at a time. The result is that a new spare disk will be added | |
9078 | * to the first failed array and after the monitor has finished | |
9079 | * propagating failures the remainder will be consumed. | |
88758e9d | 9080 | * |
d23fe947 DW |
9081 | * FIXME add a capability for mdmon to request spares from another |
9082 | * container. | |
88758e9d DW |
9083 | */ |
9084 | ||
9085 | struct intel_super *super = a->container->sb; | |
88758e9d | 9086 | int inst = a->info.container_member; |
949c47a0 | 9087 | struct imsm_dev *dev = get_imsm_dev(super, inst); |
238c0a71 | 9088 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
88758e9d DW |
9089 | int failed = a->info.array.raid_disks; |
9090 | struct mdinfo *rv = NULL; | |
9091 | struct mdinfo *d; | |
9092 | struct mdinfo *di; | |
9093 | struct metadata_update *mu; | |
9094 | struct dl *dl; | |
9095 | struct imsm_update_activate_spare *u; | |
9096 | int num_spares = 0; | |
9097 | int i; | |
95d07a2c | 9098 | int allowed; |
88758e9d DW |
9099 | |
9100 | for (d = a->info.devs ; d ; d = d->next) { | |
9101 | if ((d->curr_state & DS_FAULTY) && | |
9102 | d->state_fd >= 0) | |
9103 | /* wait for Removal to happen */ | |
9104 | return NULL; | |
9105 | if (d->state_fd >= 0) | |
9106 | failed--; | |
9107 | } | |
9108 | ||
9109 | dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n", | |
9110 | inst, failed, a->info.array.raid_disks, a->info.array.level); | |
1af97990 | 9111 | |
e2962bfc AK |
9112 | if (imsm_reshape_blocks_arrays_changes(super)) |
9113 | return NULL; | |
1af97990 | 9114 | |
fc8ca064 AK |
9115 | /* Cannot activate another spare if rebuild is in progress already |
9116 | */ | |
9117 | if (is_rebuilding(dev)) { | |
7a862a02 | 9118 | dprintf("imsm: No spare activation allowed. Rebuild in progress already.\n"); |
fc8ca064 AK |
9119 | return NULL; |
9120 | } | |
9121 | ||
89c67882 AK |
9122 | if (a->info.array.level == 4) |
9123 | /* No repair for takeovered array | |
9124 | * imsm doesn't support raid4 | |
9125 | */ | |
9126 | return NULL; | |
9127 | ||
3b451610 AK |
9128 | if (imsm_check_degraded(super, dev, failed, MAP_0) != |
9129 | IMSM_T_STATE_DEGRADED) | |
88758e9d DW |
9130 | return NULL; |
9131 | ||
83ca7d45 AP |
9132 | if (get_imsm_map(dev, MAP_0)->map_state == IMSM_T_STATE_UNINITIALIZED) { |
9133 | dprintf("imsm: No spare activation allowed. Volume is not initialized.\n"); | |
9134 | return NULL; | |
9135 | } | |
9136 | ||
95d07a2c LM |
9137 | /* |
9138 | * If there are any failed disks check state of the other volume. | |
9139 | * Block rebuild if the another one is failed until failed disks | |
9140 | * are removed from container. | |
9141 | */ | |
9142 | if (failed) { | |
7a862a02 | 9143 | dprintf("found failed disks in %.*s, check if there anotherfailed sub-array.\n", |
c4acd1e5 | 9144 | MAX_RAID_SERIAL_LEN, dev->volume); |
95d07a2c LM |
9145 | /* check if states of the other volumes allow for rebuild */ |
9146 | for (i = 0; i < super->anchor->num_raid_devs; i++) { | |
9147 | if (i != inst) { | |
9148 | allowed = imsm_rebuild_allowed(a->container, | |
9149 | i, failed); | |
9150 | if (!allowed) | |
9151 | return NULL; | |
9152 | } | |
9153 | } | |
9154 | } | |
9155 | ||
88758e9d | 9156 | /* For each slot, if it is not working, find a spare */ |
88758e9d DW |
9157 | for (i = 0; i < a->info.array.raid_disks; i++) { |
9158 | for (d = a->info.devs ; d ; d = d->next) | |
9159 | if (d->disk.raid_disk == i) | |
9160 | break; | |
9161 | dprintf("found %d: %p %x\n", i, d, d?d->curr_state:0); | |
9162 | if (d && (d->state_fd >= 0)) | |
9163 | continue; | |
9164 | ||
272906ef | 9165 | /* |
a20d2ba5 DW |
9166 | * OK, this device needs recovery. Try to re-add the |
9167 | * previous occupant of this slot, if this fails see if | |
9168 | * we can continue the assimilation of a spare that was | |
9169 | * partially assimilated, finally try to activate a new | |
9170 | * spare. | |
272906ef DW |
9171 | */ |
9172 | dl = imsm_readd(super, i, a); | |
9173 | if (!dl) | |
b303fe21 | 9174 | dl = imsm_add_spare(super, i, a, 0, rv); |
a20d2ba5 | 9175 | if (!dl) |
b303fe21 | 9176 | dl = imsm_add_spare(super, i, a, 1, rv); |
272906ef DW |
9177 | if (!dl) |
9178 | continue; | |
1011e834 | 9179 | |
272906ef | 9180 | /* found a usable disk with enough space */ |
503975b9 | 9181 | di = xcalloc(1, sizeof(*di)); |
272906ef DW |
9182 | |
9183 | /* dl->index will be -1 in the case we are activating a | |
9184 | * pristine spare. imsm_process_update() will create a | |
9185 | * new index in this case. Once a disk is found to be | |
9186 | * failed in all member arrays it is kicked from the | |
9187 | * metadata | |
9188 | */ | |
9189 | di->disk.number = dl->index; | |
d23fe947 | 9190 | |
272906ef DW |
9191 | /* (ab)use di->devs to store a pointer to the device |
9192 | * we chose | |
9193 | */ | |
9194 | di->devs = (struct mdinfo *) dl; | |
9195 | ||
9196 | di->disk.raid_disk = i; | |
9197 | di->disk.major = dl->major; | |
9198 | di->disk.minor = dl->minor; | |
9199 | di->disk.state = 0; | |
d23534e4 | 9200 | di->recovery_start = 0; |
5551b113 | 9201 | di->data_offset = pba_of_lba0(map); |
272906ef DW |
9202 | di->component_size = a->info.component_size; |
9203 | di->container_member = inst; | |
5e46202e | 9204 | di->bb.supported = 1; |
2c8890e9 | 9205 | if (a->info.consistency_policy == CONSISTENCY_POLICY_PPL) { |
2432ce9b | 9206 | di->ppl_sector = get_ppl_sector(super, inst); |
c2462068 | 9207 | di->ppl_size = MULTIPLE_PPL_AREA_SIZE_IMSM >> 9; |
2432ce9b | 9208 | } |
148acb7b | 9209 | super->random = random32(); |
272906ef DW |
9210 | di->next = rv; |
9211 | rv = di; | |
9212 | num_spares++; | |
9213 | dprintf("%x:%x to be %d at %llu\n", dl->major, dl->minor, | |
9214 | i, di->data_offset); | |
88758e9d DW |
9215 | } |
9216 | ||
9217 | if (!rv) | |
9218 | /* No spares found */ | |
9219 | return rv; | |
9220 | /* Now 'rv' has a list of devices to return. | |
9221 | * Create a metadata_update record to update the | |
9222 | * disk_ord_tbl for the array | |
9223 | */ | |
503975b9 | 9224 | mu = xmalloc(sizeof(*mu)); |
1011e834 | 9225 | mu->buf = xcalloc(num_spares, |
503975b9 | 9226 | sizeof(struct imsm_update_activate_spare)); |
88758e9d | 9227 | mu->space = NULL; |
cb23f1f4 | 9228 | mu->space_list = NULL; |
88758e9d DW |
9229 | mu->len = sizeof(struct imsm_update_activate_spare) * num_spares; |
9230 | mu->next = *updates; | |
9231 | u = (struct imsm_update_activate_spare *) mu->buf; | |
9232 | ||
9233 | for (di = rv ; di ; di = di->next) { | |
9234 | u->type = update_activate_spare; | |
d23fe947 DW |
9235 | u->dl = (struct dl *) di->devs; |
9236 | di->devs = NULL; | |
88758e9d DW |
9237 | u->slot = di->disk.raid_disk; |
9238 | u->array = inst; | |
9239 | u->next = u + 1; | |
9240 | u++; | |
9241 | } | |
9242 | (u-1)->next = NULL; | |
9243 | *updates = mu; | |
9244 | ||
9245 | return rv; | |
9246 | } | |
9247 | ||
54c2c1ea | 9248 | static int disks_overlap(struct intel_super *super, int idx, struct imsm_update_create_array *u) |
8273f55e | 9249 | { |
54c2c1ea | 9250 | struct imsm_dev *dev = get_imsm_dev(super, idx); |
238c0a71 AK |
9251 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
9252 | struct imsm_map *new_map = get_imsm_map(&u->dev, MAP_0); | |
54c2c1ea DW |
9253 | struct disk_info *inf = get_disk_info(u); |
9254 | struct imsm_disk *disk; | |
8273f55e DW |
9255 | int i; |
9256 | int j; | |
8273f55e | 9257 | |
54c2c1ea | 9258 | for (i = 0; i < map->num_members; i++) { |
238c0a71 | 9259 | disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i, MAP_X)); |
54c2c1ea DW |
9260 | for (j = 0; j < new_map->num_members; j++) |
9261 | if (serialcmp(disk->serial, inf[j].serial) == 0) | |
8273f55e DW |
9262 | return 1; |
9263 | } | |
9264 | ||
9265 | return 0; | |
9266 | } | |
9267 | ||
1a64be56 LM |
9268 | static struct dl *get_disk_super(struct intel_super *super, int major, int minor) |
9269 | { | |
594dc1b8 JS |
9270 | struct dl *dl; |
9271 | ||
1a64be56 | 9272 | for (dl = super->disks; dl; dl = dl->next) |
089f9d79 | 9273 | if (dl->major == major && dl->minor == minor) |
1a64be56 LM |
9274 | return dl; |
9275 | return NULL; | |
9276 | } | |
9277 | ||
9278 | static int remove_disk_super(struct intel_super *super, int major, int minor) | |
9279 | { | |
594dc1b8 | 9280 | struct dl *prev; |
1a64be56 LM |
9281 | struct dl *dl; |
9282 | ||
9283 | prev = NULL; | |
9284 | for (dl = super->disks; dl; dl = dl->next) { | |
089f9d79 | 9285 | if (dl->major == major && dl->minor == minor) { |
1a64be56 LM |
9286 | /* remove */ |
9287 | if (prev) | |
9288 | prev->next = dl->next; | |
9289 | else | |
9290 | super->disks = dl->next; | |
9291 | dl->next = NULL; | |
3a85bf0e | 9292 | __free_imsm_disk(dl, 1); |
1ade5cc1 | 9293 | dprintf("removed %x:%x\n", major, minor); |
1a64be56 LM |
9294 | break; |
9295 | } | |
9296 | prev = dl; | |
9297 | } | |
9298 | return 0; | |
9299 | } | |
9300 | ||
f21e18ca | 9301 | static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index); |
ae6aad82 | 9302 | |
1a64be56 LM |
9303 | static int add_remove_disk_update(struct intel_super *super) |
9304 | { | |
9305 | int check_degraded = 0; | |
594dc1b8 JS |
9306 | struct dl *disk; |
9307 | ||
1a64be56 LM |
9308 | /* add/remove some spares to/from the metadata/contrainer */ |
9309 | while (super->disk_mgmt_list) { | |
9310 | struct dl *disk_cfg; | |
9311 | ||
9312 | disk_cfg = super->disk_mgmt_list; | |
9313 | super->disk_mgmt_list = disk_cfg->next; | |
9314 | disk_cfg->next = NULL; | |
9315 | ||
9316 | if (disk_cfg->action == DISK_ADD) { | |
9317 | disk_cfg->next = super->disks; | |
9318 | super->disks = disk_cfg; | |
9319 | check_degraded = 1; | |
1ade5cc1 N |
9320 | dprintf("added %x:%x\n", |
9321 | disk_cfg->major, disk_cfg->minor); | |
1a64be56 LM |
9322 | } else if (disk_cfg->action == DISK_REMOVE) { |
9323 | dprintf("Disk remove action processed: %x.%x\n", | |
9324 | disk_cfg->major, disk_cfg->minor); | |
9325 | disk = get_disk_super(super, | |
9326 | disk_cfg->major, | |
9327 | disk_cfg->minor); | |
9328 | if (disk) { | |
9329 | /* store action status */ | |
9330 | disk->action = DISK_REMOVE; | |
9331 | /* remove spare disks only */ | |
9332 | if (disk->index == -1) { | |
9333 | remove_disk_super(super, | |
9334 | disk_cfg->major, | |
9335 | disk_cfg->minor); | |
91c97c54 MT |
9336 | } else { |
9337 | disk_cfg->fd = disk->fd; | |
9338 | disk->fd = -1; | |
1a64be56 LM |
9339 | } |
9340 | } | |
9341 | /* release allocate disk structure */ | |
3a85bf0e | 9342 | __free_imsm_disk(disk_cfg, 1); |
1a64be56 LM |
9343 | } |
9344 | } | |
9345 | return check_degraded; | |
9346 | } | |
9347 | ||
a29911da PC |
9348 | static int apply_reshape_migration_update(struct imsm_update_reshape_migration *u, |
9349 | struct intel_super *super, | |
9350 | void ***space_list) | |
9351 | { | |
9352 | struct intel_dev *id; | |
9353 | void **tofree = NULL; | |
9354 | int ret_val = 0; | |
9355 | ||
1ade5cc1 | 9356 | dprintf("(enter)\n"); |
089f9d79 | 9357 | if (u->subdev < 0 || u->subdev > 1) { |
a29911da PC |
9358 | dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev); |
9359 | return ret_val; | |
9360 | } | |
089f9d79 | 9361 | if (space_list == NULL || *space_list == NULL) { |
a29911da PC |
9362 | dprintf("imsm: Error: Memory is not allocated\n"); |
9363 | return ret_val; | |
9364 | } | |
9365 | ||
9366 | for (id = super->devlist ; id; id = id->next) { | |
9367 | if (id->index == (unsigned)u->subdev) { | |
9368 | struct imsm_dev *dev = get_imsm_dev(super, u->subdev); | |
9369 | struct imsm_map *map; | |
9370 | struct imsm_dev *new_dev = | |
9371 | (struct imsm_dev *)*space_list; | |
238c0a71 | 9372 | struct imsm_map *migr_map = get_imsm_map(dev, MAP_1); |
a29911da PC |
9373 | int to_state; |
9374 | struct dl *new_disk; | |
9375 | ||
9376 | if (new_dev == NULL) | |
9377 | return ret_val; | |
9378 | *space_list = **space_list; | |
9379 | memcpy(new_dev, dev, sizeof_imsm_dev(dev, 0)); | |
238c0a71 | 9380 | map = get_imsm_map(new_dev, MAP_0); |
a29911da PC |
9381 | if (migr_map) { |
9382 | dprintf("imsm: Error: migration in progress"); | |
9383 | return ret_val; | |
9384 | } | |
9385 | ||
9386 | to_state = map->map_state; | |
9387 | if ((u->new_level == 5) && (map->raid_level == 0)) { | |
9388 | map->num_members++; | |
9389 | /* this should not happen */ | |
9390 | if (u->new_disks[0] < 0) { | |
9391 | map->failed_disk_num = | |
9392 | map->num_members - 1; | |
9393 | to_state = IMSM_T_STATE_DEGRADED; | |
9394 | } else | |
9395 | to_state = IMSM_T_STATE_NORMAL; | |
9396 | } | |
8e59f3d8 | 9397 | migrate(new_dev, super, to_state, MIGR_GEN_MIGR); |
a29911da PC |
9398 | if (u->new_level > -1) |
9399 | map->raid_level = u->new_level; | |
238c0a71 | 9400 | migr_map = get_imsm_map(new_dev, MAP_1); |
a29911da PC |
9401 | if ((u->new_level == 5) && |
9402 | (migr_map->raid_level == 0)) { | |
9403 | int ord = map->num_members - 1; | |
9404 | migr_map->num_members--; | |
9405 | if (u->new_disks[0] < 0) | |
9406 | ord |= IMSM_ORD_REBUILD; | |
9407 | set_imsm_ord_tbl_ent(map, | |
9408 | map->num_members - 1, | |
9409 | ord); | |
9410 | } | |
9411 | id->dev = new_dev; | |
9412 | tofree = (void **)dev; | |
9413 | ||
4bba0439 PC |
9414 | /* update chunk size |
9415 | */ | |
06fb291a | 9416 | if (u->new_chunksize > 0) { |
9529d343 MD |
9417 | struct imsm_map *dest_map = |
9418 | get_imsm_map(dev, MAP_0); | |
06fb291a | 9419 | int used_disks = |
9529d343 | 9420 | imsm_num_data_members(dest_map); |
06fb291a PB |
9421 | |
9422 | if (used_disks == 0) | |
9423 | return ret_val; | |
9424 | ||
4bba0439 PC |
9425 | map->blocks_per_strip = |
9426 | __cpu_to_le16(u->new_chunksize * 2); | |
1c275381 | 9427 | update_num_data_stripes(map, imsm_dev_size(dev)); |
06fb291a | 9428 | } |
4bba0439 | 9429 | |
44490938 MD |
9430 | /* ensure blocks_per_member has valid value |
9431 | */ | |
9432 | set_blocks_per_member(map, | |
9433 | per_dev_array_size(map) + | |
9434 | NUM_BLOCKS_DIRTY_STRIPE_REGION); | |
9435 | ||
a29911da PC |
9436 | /* add disk |
9437 | */ | |
089f9d79 JS |
9438 | if (u->new_level != 5 || migr_map->raid_level != 0 || |
9439 | migr_map->raid_level == map->raid_level) | |
a29911da PC |
9440 | goto skip_disk_add; |
9441 | ||
9442 | if (u->new_disks[0] >= 0) { | |
9443 | /* use passes spare | |
9444 | */ | |
9445 | new_disk = get_disk_super(super, | |
9446 | major(u->new_disks[0]), | |
9447 | minor(u->new_disks[0])); | |
7a862a02 | 9448 | dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n", |
a29911da PC |
9449 | major(u->new_disks[0]), |
9450 | minor(u->new_disks[0]), | |
9451 | new_disk, new_disk->index); | |
9452 | if (new_disk == NULL) | |
9453 | goto error_disk_add; | |
9454 | ||
9455 | new_disk->index = map->num_members - 1; | |
9456 | /* slot to fill in autolayout | |
9457 | */ | |
9458 | new_disk->raiddisk = new_disk->index; | |
9459 | new_disk->disk.status |= CONFIGURED_DISK; | |
9460 | new_disk->disk.status &= ~SPARE_DISK; | |
9461 | } else | |
9462 | goto error_disk_add; | |
9463 | ||
9464 | skip_disk_add: | |
9465 | *tofree = *space_list; | |
9466 | /* calculate new size | |
9467 | */ | |
f3871fdc | 9468 | imsm_set_array_size(new_dev, -1); |
a29911da PC |
9469 | |
9470 | ret_val = 1; | |
9471 | } | |
9472 | } | |
9473 | ||
9474 | if (tofree) | |
9475 | *space_list = tofree; | |
9476 | return ret_val; | |
9477 | ||
9478 | error_disk_add: | |
9479 | dprintf("Error: imsm: Cannot find disk.\n"); | |
9480 | return ret_val; | |
9481 | } | |
9482 | ||
f3871fdc AK |
9483 | static int apply_size_change_update(struct imsm_update_size_change *u, |
9484 | struct intel_super *super) | |
9485 | { | |
9486 | struct intel_dev *id; | |
9487 | int ret_val = 0; | |
9488 | ||
1ade5cc1 | 9489 | dprintf("(enter)\n"); |
089f9d79 | 9490 | if (u->subdev < 0 || u->subdev > 1) { |
f3871fdc AK |
9491 | dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev); |
9492 | return ret_val; | |
9493 | } | |
9494 | ||
9495 | for (id = super->devlist ; id; id = id->next) { | |
9496 | if (id->index == (unsigned)u->subdev) { | |
9497 | struct imsm_dev *dev = get_imsm_dev(super, u->subdev); | |
9498 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
9529d343 | 9499 | int used_disks = imsm_num_data_members(map); |
f3871fdc | 9500 | unsigned long long blocks_per_member; |
44490938 MD |
9501 | unsigned long long new_size_per_disk; |
9502 | ||
9503 | if (used_disks == 0) | |
9504 | return 0; | |
f3871fdc AK |
9505 | |
9506 | /* calculate new size | |
9507 | */ | |
44490938 MD |
9508 | new_size_per_disk = u->new_size / used_disks; |
9509 | blocks_per_member = new_size_per_disk + | |
9510 | NUM_BLOCKS_DIRTY_STRIPE_REGION; | |
f3871fdc | 9511 | |
1c275381 MT |
9512 | imsm_set_array_size(dev, u->new_size); |
9513 | set_blocks_per_member(map, blocks_per_member); | |
9514 | update_num_data_stripes(map, u->new_size); | |
f3871fdc AK |
9515 | ret_val = 1; |
9516 | break; | |
9517 | } | |
9518 | } | |
9519 | ||
9520 | return ret_val; | |
9521 | } | |
9522 | ||
69d40de4 JR |
9523 | static int prepare_spare_to_activate(struct supertype *st, |
9524 | struct imsm_update_activate_spare *u) | |
9525 | { | |
9526 | struct intel_super *super = st->sb; | |
9527 | int prev_current_vol = super->current_vol; | |
9528 | struct active_array *a; | |
9529 | int ret = 1; | |
9530 | ||
9531 | for (a = st->arrays; a; a = a->next) | |
9532 | /* | |
9533 | * Additional initialization (adding bitmap header, filling | |
9534 | * the bitmap area with '1's to force initial rebuild for a whole | |
9535 | * data-area) is required when adding the spare to the volume | |
9536 | * with write-intent bitmap. | |
9537 | */ | |
9538 | if (a->info.container_member == u->array && | |
9539 | a->info.consistency_policy == CONSISTENCY_POLICY_BITMAP) { | |
9540 | struct dl *dl; | |
9541 | ||
9542 | for (dl = super->disks; dl; dl = dl->next) | |
9543 | if (dl == u->dl) | |
9544 | break; | |
9545 | if (!dl) | |
9546 | break; | |
9547 | ||
9548 | super->current_vol = u->array; | |
9549 | if (st->ss->write_bitmap(st, dl->fd, NoUpdate)) | |
9550 | ret = 0; | |
9551 | super->current_vol = prev_current_vol; | |
9552 | } | |
9553 | return ret; | |
9554 | } | |
9555 | ||
061d7da3 | 9556 | static int apply_update_activate_spare(struct imsm_update_activate_spare *u, |
ca9de185 | 9557 | struct intel_super *super, |
061d7da3 LO |
9558 | struct active_array *active_array) |
9559 | { | |
9560 | struct imsm_super *mpb = super->anchor; | |
9561 | struct imsm_dev *dev = get_imsm_dev(super, u->array); | |
238c0a71 | 9562 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
061d7da3 LO |
9563 | struct imsm_map *migr_map; |
9564 | struct active_array *a; | |
9565 | struct imsm_disk *disk; | |
9566 | __u8 to_state; | |
9567 | struct dl *dl; | |
9568 | unsigned int found; | |
9569 | int failed; | |
5961eeec | 9570 | int victim; |
061d7da3 | 9571 | int i; |
5961eeec | 9572 | int second_map_created = 0; |
061d7da3 | 9573 | |
5961eeec | 9574 | for (; u; u = u->next) { |
238c0a71 | 9575 | victim = get_imsm_disk_idx(dev, u->slot, MAP_X); |
061d7da3 | 9576 | |
5961eeec | 9577 | if (victim < 0) |
9578 | return 0; | |
061d7da3 | 9579 | |
5961eeec | 9580 | for (dl = super->disks; dl; dl = dl->next) |
9581 | if (dl == u->dl) | |
9582 | break; | |
061d7da3 | 9583 | |
5961eeec | 9584 | if (!dl) { |
7a862a02 | 9585 | pr_err("error: imsm_activate_spare passed an unknown disk (index: %d)\n", |
5961eeec | 9586 | u->dl->index); |
9587 | return 0; | |
9588 | } | |
061d7da3 | 9589 | |
5961eeec | 9590 | /* count failures (excluding rebuilds and the victim) |
9591 | * to determine map[0] state | |
9592 | */ | |
9593 | failed = 0; | |
9594 | for (i = 0; i < map->num_members; i++) { | |
9595 | if (i == u->slot) | |
9596 | continue; | |
9597 | disk = get_imsm_disk(super, | |
238c0a71 | 9598 | get_imsm_disk_idx(dev, i, MAP_X)); |
5961eeec | 9599 | if (!disk || is_failed(disk)) |
9600 | failed++; | |
9601 | } | |
061d7da3 | 9602 | |
5961eeec | 9603 | /* adding a pristine spare, assign a new index */ |
9604 | if (dl->index < 0) { | |
9605 | dl->index = super->anchor->num_disks; | |
9606 | super->anchor->num_disks++; | |
9607 | } | |
9608 | disk = &dl->disk; | |
9609 | disk->status |= CONFIGURED_DISK; | |
9610 | disk->status &= ~SPARE_DISK; | |
9611 | ||
9612 | /* mark rebuild */ | |
238c0a71 | 9613 | to_state = imsm_check_degraded(super, dev, failed, MAP_0); |
5961eeec | 9614 | if (!second_map_created) { |
9615 | second_map_created = 1; | |
9616 | map->map_state = IMSM_T_STATE_DEGRADED; | |
9617 | migrate(dev, super, to_state, MIGR_REBUILD); | |
9618 | } else | |
9619 | map->map_state = to_state; | |
238c0a71 | 9620 | migr_map = get_imsm_map(dev, MAP_1); |
5961eeec | 9621 | set_imsm_ord_tbl_ent(map, u->slot, dl->index); |
9622 | set_imsm_ord_tbl_ent(migr_map, u->slot, | |
9623 | dl->index | IMSM_ORD_REBUILD); | |
9624 | ||
9625 | /* update the family_num to mark a new container | |
9626 | * generation, being careful to record the existing | |
9627 | * family_num in orig_family_num to clean up after | |
9628 | * earlier mdadm versions that neglected to set it. | |
9629 | */ | |
9630 | if (mpb->orig_family_num == 0) | |
9631 | mpb->orig_family_num = mpb->family_num; | |
9632 | mpb->family_num += super->random; | |
9633 | ||
9634 | /* count arrays using the victim in the metadata */ | |
9635 | found = 0; | |
9636 | for (a = active_array; a ; a = a->next) { | |
9637 | dev = get_imsm_dev(super, a->info.container_member); | |
238c0a71 | 9638 | map = get_imsm_map(dev, MAP_0); |
061d7da3 | 9639 | |
5961eeec | 9640 | if (get_imsm_disk_slot(map, victim) >= 0) |
9641 | found++; | |
9642 | } | |
061d7da3 | 9643 | |
5961eeec | 9644 | /* delete the victim if it is no longer being |
9645 | * utilized anywhere | |
061d7da3 | 9646 | */ |
5961eeec | 9647 | if (!found) { |
9648 | struct dl **dlp; | |
061d7da3 | 9649 | |
5961eeec | 9650 | /* We know that 'manager' isn't touching anything, |
9651 | * so it is safe to delete | |
9652 | */ | |
9653 | for (dlp = &super->disks; *dlp; dlp = &(*dlp)->next) | |
061d7da3 LO |
9654 | if ((*dlp)->index == victim) |
9655 | break; | |
5961eeec | 9656 | |
9657 | /* victim may be on the missing list */ | |
9658 | if (!*dlp) | |
9659 | for (dlp = &super->missing; *dlp; | |
9660 | dlp = &(*dlp)->next) | |
9661 | if ((*dlp)->index == victim) | |
9662 | break; | |
9663 | imsm_delete(super, dlp, victim); | |
9664 | } | |
061d7da3 LO |
9665 | } |
9666 | ||
9667 | return 1; | |
9668 | } | |
a29911da | 9669 | |
2e5dc010 N |
9670 | static int apply_reshape_container_disks_update(struct imsm_update_reshape *u, |
9671 | struct intel_super *super, | |
9672 | void ***space_list) | |
9673 | { | |
9674 | struct dl *new_disk; | |
9675 | struct intel_dev *id; | |
9676 | int i; | |
9677 | int delta_disks = u->new_raid_disks - u->old_raid_disks; | |
ee4beede | 9678 | int disk_count = u->old_raid_disks; |
2e5dc010 N |
9679 | void **tofree = NULL; |
9680 | int devices_to_reshape = 1; | |
9681 | struct imsm_super *mpb = super->anchor; | |
9682 | int ret_val = 0; | |
d098291a | 9683 | unsigned int dev_id; |
2e5dc010 | 9684 | |
1ade5cc1 | 9685 | dprintf("(enter)\n"); |
2e5dc010 N |
9686 | |
9687 | /* enable spares to use in array */ | |
9688 | for (i = 0; i < delta_disks; i++) { | |
9689 | new_disk = get_disk_super(super, | |
9690 | major(u->new_disks[i]), | |
9691 | minor(u->new_disks[i])); | |
7a862a02 | 9692 | dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n", |
2e5dc010 N |
9693 | major(u->new_disks[i]), minor(u->new_disks[i]), |
9694 | new_disk, new_disk->index); | |
089f9d79 JS |
9695 | if (new_disk == NULL || |
9696 | (new_disk->index >= 0 && | |
9697 | new_disk->index < u->old_raid_disks)) | |
2e5dc010 | 9698 | goto update_reshape_exit; |
ee4beede | 9699 | new_disk->index = disk_count++; |
2e5dc010 N |
9700 | /* slot to fill in autolayout |
9701 | */ | |
9702 | new_disk->raiddisk = new_disk->index; | |
9703 | new_disk->disk.status |= | |
9704 | CONFIGURED_DISK; | |
9705 | new_disk->disk.status &= ~SPARE_DISK; | |
9706 | } | |
9707 | ||
ed7333bd AK |
9708 | dprintf("imsm: volume set mpb->num_raid_devs = %i\n", |
9709 | mpb->num_raid_devs); | |
2e5dc010 N |
9710 | /* manage changes in volume |
9711 | */ | |
d098291a | 9712 | for (dev_id = 0; dev_id < mpb->num_raid_devs; dev_id++) { |
2e5dc010 N |
9713 | void **sp = *space_list; |
9714 | struct imsm_dev *newdev; | |
9715 | struct imsm_map *newmap, *oldmap; | |
9716 | ||
d098291a AK |
9717 | for (id = super->devlist ; id; id = id->next) { |
9718 | if (id->index == dev_id) | |
9719 | break; | |
9720 | } | |
9721 | if (id == NULL) | |
9722 | break; | |
2e5dc010 N |
9723 | if (!sp) |
9724 | continue; | |
9725 | *space_list = *sp; | |
9726 | newdev = (void*)sp; | |
9727 | /* Copy the dev, but not (all of) the map */ | |
9728 | memcpy(newdev, id->dev, sizeof(*newdev)); | |
238c0a71 AK |
9729 | oldmap = get_imsm_map(id->dev, MAP_0); |
9730 | newmap = get_imsm_map(newdev, MAP_0); | |
2e5dc010 N |
9731 | /* Copy the current map */ |
9732 | memcpy(newmap, oldmap, sizeof_imsm_map(oldmap)); | |
9733 | /* update one device only | |
9734 | */ | |
9735 | if (devices_to_reshape) { | |
ed7333bd AK |
9736 | dprintf("imsm: modifying subdev: %i\n", |
9737 | id->index); | |
2e5dc010 N |
9738 | devices_to_reshape--; |
9739 | newdev->vol.migr_state = 1; | |
4036e7ee | 9740 | set_vol_curr_migr_unit(newdev, 0); |
ea672ee1 | 9741 | set_migr_type(newdev, MIGR_GEN_MIGR); |
2e5dc010 N |
9742 | newmap->num_members = u->new_raid_disks; |
9743 | for (i = 0; i < delta_disks; i++) { | |
9744 | set_imsm_ord_tbl_ent(newmap, | |
9745 | u->old_raid_disks + i, | |
9746 | u->old_raid_disks + i); | |
9747 | } | |
9748 | /* New map is correct, now need to save old map | |
9749 | */ | |
238c0a71 | 9750 | newmap = get_imsm_map(newdev, MAP_1); |
2e5dc010 N |
9751 | memcpy(newmap, oldmap, sizeof_imsm_map(oldmap)); |
9752 | ||
f3871fdc | 9753 | imsm_set_array_size(newdev, -1); |
2e5dc010 N |
9754 | } |
9755 | ||
9756 | sp = (void **)id->dev; | |
9757 | id->dev = newdev; | |
9758 | *sp = tofree; | |
9759 | tofree = sp; | |
8e59f3d8 AK |
9760 | |
9761 | /* Clear migration record */ | |
9762 | memset(super->migr_rec, 0, sizeof(struct migr_record)); | |
2e5dc010 | 9763 | } |
819bc634 AK |
9764 | if (tofree) |
9765 | *space_list = tofree; | |
2e5dc010 N |
9766 | ret_val = 1; |
9767 | ||
9768 | update_reshape_exit: | |
9769 | ||
9770 | return ret_val; | |
9771 | } | |
9772 | ||
bb025c2f | 9773 | static int apply_takeover_update(struct imsm_update_takeover *u, |
8ca6df95 KW |
9774 | struct intel_super *super, |
9775 | void ***space_list) | |
bb025c2f KW |
9776 | { |
9777 | struct imsm_dev *dev = NULL; | |
8ca6df95 KW |
9778 | struct intel_dev *dv; |
9779 | struct imsm_dev *dev_new; | |
bb025c2f KW |
9780 | struct imsm_map *map; |
9781 | struct dl *dm, *du; | |
8ca6df95 | 9782 | int i; |
bb025c2f KW |
9783 | |
9784 | for (dv = super->devlist; dv; dv = dv->next) | |
9785 | if (dv->index == (unsigned int)u->subarray) { | |
9786 | dev = dv->dev; | |
9787 | break; | |
9788 | } | |
9789 | ||
9790 | if (dev == NULL) | |
9791 | return 0; | |
9792 | ||
238c0a71 | 9793 | map = get_imsm_map(dev, MAP_0); |
bb025c2f KW |
9794 | |
9795 | if (u->direction == R10_TO_R0) { | |
43d5ec18 | 9796 | /* Number of failed disks must be half of initial disk number */ |
3b451610 AK |
9797 | if (imsm_count_failed(super, dev, MAP_0) != |
9798 | (map->num_members / 2)) | |
43d5ec18 KW |
9799 | return 0; |
9800 | ||
bb025c2f KW |
9801 | /* iterate through devices to mark removed disks as spare */ |
9802 | for (dm = super->disks; dm; dm = dm->next) { | |
9803 | if (dm->disk.status & FAILED_DISK) { | |
9804 | int idx = dm->index; | |
9805 | /* update indexes on the disk list */ | |
9806 | /* FIXME this loop-with-the-loop looks wrong, I'm not convinced | |
9807 | the index values will end up being correct.... NB */ | |
9808 | for (du = super->disks; du; du = du->next) | |
9809 | if (du->index > idx) | |
9810 | du->index--; | |
9811 | /* mark as spare disk */ | |
a8619d23 | 9812 | mark_spare(dm); |
bb025c2f KW |
9813 | } |
9814 | } | |
bb025c2f | 9815 | /* update map */ |
1c275381 | 9816 | map->num_members /= map->num_domains; |
bb025c2f | 9817 | map->map_state = IMSM_T_STATE_NORMAL; |
bb025c2f | 9818 | map->raid_level = 0; |
1c275381 MT |
9819 | set_num_domains(map); |
9820 | update_num_data_stripes(map, imsm_dev_size(dev)); | |
bb025c2f KW |
9821 | map->failed_disk_num = -1; |
9822 | } | |
9823 | ||
8ca6df95 KW |
9824 | if (u->direction == R0_TO_R10) { |
9825 | void **space; | |
4a353e6e | 9826 | |
8ca6df95 KW |
9827 | /* update slots in current disk list */ |
9828 | for (dm = super->disks; dm; dm = dm->next) { | |
9829 | if (dm->index >= 0) | |
9830 | dm->index *= 2; | |
9831 | } | |
9832 | /* create new *missing* disks */ | |
9833 | for (i = 0; i < map->num_members; i++) { | |
9834 | space = *space_list; | |
9835 | if (!space) | |
9836 | continue; | |
9837 | *space_list = *space; | |
9838 | du = (void *)space; | |
9839 | memcpy(du, super->disks, sizeof(*du)); | |
8ca6df95 KW |
9840 | du->fd = -1; |
9841 | du->minor = 0; | |
9842 | du->major = 0; | |
9843 | du->index = (i * 2) + 1; | |
9844 | sprintf((char *)du->disk.serial, | |
9845 | " MISSING_%d", du->index); | |
9846 | sprintf((char *)du->serial, | |
9847 | "MISSING_%d", du->index); | |
9848 | du->next = super->missing; | |
9849 | super->missing = du; | |
9850 | } | |
9851 | /* create new dev and map */ | |
9852 | space = *space_list; | |
9853 | if (!space) | |
9854 | return 0; | |
9855 | *space_list = *space; | |
9856 | dev_new = (void *)space; | |
9857 | memcpy(dev_new, dev, sizeof(*dev)); | |
9858 | /* update new map */ | |
238c0a71 | 9859 | map = get_imsm_map(dev_new, MAP_0); |
1c275381 | 9860 | |
1a2487c2 | 9861 | map->map_state = IMSM_T_STATE_DEGRADED; |
8ca6df95 | 9862 | map->raid_level = 1; |
1c275381 MT |
9863 | set_num_domains(map); |
9864 | map->num_members = map->num_members * map->num_domains; | |
9865 | update_num_data_stripes(map, imsm_dev_size(dev)); | |
4a353e6e | 9866 | |
8ca6df95 KW |
9867 | /* replace dev<->dev_new */ |
9868 | dv->dev = dev_new; | |
9869 | } | |
bb025c2f KW |
9870 | /* update disk order table */ |
9871 | for (du = super->disks; du; du = du->next) | |
9872 | if (du->index >= 0) | |
9873 | set_imsm_ord_tbl_ent(map, du->index, du->index); | |
8ca6df95 | 9874 | for (du = super->missing; du; du = du->next) |
1a2487c2 KW |
9875 | if (du->index >= 0) { |
9876 | set_imsm_ord_tbl_ent(map, du->index, du->index); | |
4c9e8c1e | 9877 | mark_missing(super, dv->dev, &du->disk, du->index); |
1a2487c2 | 9878 | } |
bb025c2f KW |
9879 | |
9880 | return 1; | |
9881 | } | |
9882 | ||
e8319a19 DW |
9883 | static void imsm_process_update(struct supertype *st, |
9884 | struct metadata_update *update) | |
9885 | { | |
9886 | /** | |
9887 | * crack open the metadata_update envelope to find the update record | |
9888 | * update can be one of: | |
d195167d AK |
9889 | * update_reshape_container_disks - all the arrays in the container |
9890 | * are being reshaped to have more devices. We need to mark | |
9891 | * the arrays for general migration and convert selected spares | |
9892 | * into active devices. | |
9893 | * update_activate_spare - a spare device has replaced a failed | |
1011e834 N |
9894 | * device in an array, update the disk_ord_tbl. If this disk is |
9895 | * present in all member arrays then also clear the SPARE_DISK | |
9896 | * flag | |
d195167d AK |
9897 | * update_create_array |
9898 | * update_kill_array | |
9899 | * update_rename_array | |
9900 | * update_add_remove_disk | |
e8319a19 DW |
9901 | */ |
9902 | struct intel_super *super = st->sb; | |
4d7b1503 | 9903 | struct imsm_super *mpb; |
e8319a19 DW |
9904 | enum imsm_update_type type = *(enum imsm_update_type *) update->buf; |
9905 | ||
4d7b1503 DW |
9906 | /* update requires a larger buf but the allocation failed */ |
9907 | if (super->next_len && !super->next_buf) { | |
9908 | super->next_len = 0; | |
9909 | return; | |
9910 | } | |
9911 | ||
9912 | if (super->next_buf) { | |
9913 | memcpy(super->next_buf, super->buf, super->len); | |
9914 | free(super->buf); | |
9915 | super->len = super->next_len; | |
9916 | super->buf = super->next_buf; | |
9917 | ||
9918 | super->next_len = 0; | |
9919 | super->next_buf = NULL; | |
9920 | } | |
9921 | ||
9922 | mpb = super->anchor; | |
9923 | ||
e8319a19 | 9924 | switch (type) { |
0ec5d470 AK |
9925 | case update_general_migration_checkpoint: { |
9926 | struct intel_dev *id; | |
9927 | struct imsm_update_general_migration_checkpoint *u = | |
9928 | (void *)update->buf; | |
9929 | ||
1ade5cc1 | 9930 | dprintf("called for update_general_migration_checkpoint\n"); |
0ec5d470 AK |
9931 | |
9932 | /* find device under general migration */ | |
9933 | for (id = super->devlist ; id; id = id->next) { | |
9934 | if (is_gen_migration(id->dev)) { | |
4036e7ee MT |
9935 | set_vol_curr_migr_unit(id->dev, |
9936 | u->curr_migr_unit); | |
0ec5d470 AK |
9937 | super->updates_pending++; |
9938 | } | |
9939 | } | |
9940 | break; | |
9941 | } | |
bb025c2f KW |
9942 | case update_takeover: { |
9943 | struct imsm_update_takeover *u = (void *)update->buf; | |
1a2487c2 KW |
9944 | if (apply_takeover_update(u, super, &update->space_list)) { |
9945 | imsm_update_version_info(super); | |
bb025c2f | 9946 | super->updates_pending++; |
1a2487c2 | 9947 | } |
bb025c2f KW |
9948 | break; |
9949 | } | |
9950 | ||
78b10e66 | 9951 | case update_reshape_container_disks: { |
d195167d | 9952 | struct imsm_update_reshape *u = (void *)update->buf; |
2e5dc010 N |
9953 | if (apply_reshape_container_disks_update( |
9954 | u, super, &update->space_list)) | |
9955 | super->updates_pending++; | |
78b10e66 N |
9956 | break; |
9957 | } | |
48c5303a | 9958 | case update_reshape_migration: { |
a29911da PC |
9959 | struct imsm_update_reshape_migration *u = (void *)update->buf; |
9960 | if (apply_reshape_migration_update( | |
9961 | u, super, &update->space_list)) | |
9962 | super->updates_pending++; | |
48c5303a PC |
9963 | break; |
9964 | } | |
f3871fdc AK |
9965 | case update_size_change: { |
9966 | struct imsm_update_size_change *u = (void *)update->buf; | |
9967 | if (apply_size_change_update(u, super)) | |
9968 | super->updates_pending++; | |
9969 | break; | |
9970 | } | |
e8319a19 | 9971 | case update_activate_spare: { |
1011e834 | 9972 | struct imsm_update_activate_spare *u = (void *) update->buf; |
69d40de4 JR |
9973 | |
9974 | if (prepare_spare_to_activate(st, u) && | |
9975 | apply_update_activate_spare(u, super, st->arrays)) | |
061d7da3 | 9976 | super->updates_pending++; |
8273f55e DW |
9977 | break; |
9978 | } | |
9979 | case update_create_array: { | |
9980 | /* someone wants to create a new array, we need to be aware of | |
9981 | * a few races/collisions: | |
9982 | * 1/ 'Create' called by two separate instances of mdadm | |
9983 | * 2/ 'Create' versus 'activate_spare': mdadm has chosen | |
9984 | * devices that have since been assimilated via | |
9985 | * activate_spare. | |
9986 | * In the event this update can not be carried out mdadm will | |
9987 | * (FIX ME) notice that its update did not take hold. | |
9988 | */ | |
9989 | struct imsm_update_create_array *u = (void *) update->buf; | |
ba2de7ba | 9990 | struct intel_dev *dv; |
8273f55e DW |
9991 | struct imsm_dev *dev; |
9992 | struct imsm_map *map, *new_map; | |
9993 | unsigned long long start, end; | |
9994 | unsigned long long new_start, new_end; | |
9995 | int i; | |
54c2c1ea DW |
9996 | struct disk_info *inf; |
9997 | struct dl *dl; | |
8273f55e DW |
9998 | |
9999 | /* handle racing creates: first come first serve */ | |
10000 | if (u->dev_idx < mpb->num_raid_devs) { | |
1ade5cc1 | 10001 | dprintf("subarray %d already defined\n", u->dev_idx); |
ba2de7ba | 10002 | goto create_error; |
8273f55e DW |
10003 | } |
10004 | ||
10005 | /* check update is next in sequence */ | |
10006 | if (u->dev_idx != mpb->num_raid_devs) { | |
1ade5cc1 N |
10007 | dprintf("can not create array %d expected index %d\n", |
10008 | u->dev_idx, mpb->num_raid_devs); | |
ba2de7ba | 10009 | goto create_error; |
8273f55e DW |
10010 | } |
10011 | ||
238c0a71 | 10012 | new_map = get_imsm_map(&u->dev, MAP_0); |
5551b113 | 10013 | new_start = pba_of_lba0(new_map); |
44490938 | 10014 | new_end = new_start + per_dev_array_size(new_map); |
54c2c1ea | 10015 | inf = get_disk_info(u); |
8273f55e DW |
10016 | |
10017 | /* handle activate_spare versus create race: | |
10018 | * check to make sure that overlapping arrays do not include | |
10019 | * overalpping disks | |
10020 | */ | |
10021 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
949c47a0 | 10022 | dev = get_imsm_dev(super, i); |
238c0a71 | 10023 | map = get_imsm_map(dev, MAP_0); |
5551b113 | 10024 | start = pba_of_lba0(map); |
44490938 | 10025 | end = start + per_dev_array_size(map); |
8273f55e DW |
10026 | if ((new_start >= start && new_start <= end) || |
10027 | (start >= new_start && start <= new_end)) | |
54c2c1ea DW |
10028 | /* overlap */; |
10029 | else | |
10030 | continue; | |
10031 | ||
10032 | if (disks_overlap(super, i, u)) { | |
1ade5cc1 | 10033 | dprintf("arrays overlap\n"); |
ba2de7ba | 10034 | goto create_error; |
8273f55e DW |
10035 | } |
10036 | } | |
8273f55e | 10037 | |
949c47a0 DW |
10038 | /* check that prepare update was successful */ |
10039 | if (!update->space) { | |
1ade5cc1 | 10040 | dprintf("prepare update failed\n"); |
ba2de7ba | 10041 | goto create_error; |
949c47a0 DW |
10042 | } |
10043 | ||
54c2c1ea DW |
10044 | /* check that all disks are still active before committing |
10045 | * changes. FIXME: could we instead handle this by creating a | |
10046 | * degraded array? That's probably not what the user expects, | |
10047 | * so better to drop this update on the floor. | |
10048 | */ | |
10049 | for (i = 0; i < new_map->num_members; i++) { | |
10050 | dl = serial_to_dl(inf[i].serial, super); | |
10051 | if (!dl) { | |
1ade5cc1 | 10052 | dprintf("disk disappeared\n"); |
ba2de7ba | 10053 | goto create_error; |
54c2c1ea | 10054 | } |
949c47a0 DW |
10055 | } |
10056 | ||
8273f55e | 10057 | super->updates_pending++; |
54c2c1ea DW |
10058 | |
10059 | /* convert spares to members and fixup ord_tbl */ | |
10060 | for (i = 0; i < new_map->num_members; i++) { | |
10061 | dl = serial_to_dl(inf[i].serial, super); | |
10062 | if (dl->index == -1) { | |
10063 | dl->index = mpb->num_disks; | |
10064 | mpb->num_disks++; | |
10065 | dl->disk.status |= CONFIGURED_DISK; | |
10066 | dl->disk.status &= ~SPARE_DISK; | |
10067 | } | |
10068 | set_imsm_ord_tbl_ent(new_map, i, dl->index); | |
10069 | } | |
10070 | ||
ba2de7ba DW |
10071 | dv = update->space; |
10072 | dev = dv->dev; | |
949c47a0 DW |
10073 | update->space = NULL; |
10074 | imsm_copy_dev(dev, &u->dev); | |
ba2de7ba DW |
10075 | dv->index = u->dev_idx; |
10076 | dv->next = super->devlist; | |
10077 | super->devlist = dv; | |
8273f55e | 10078 | mpb->num_raid_devs++; |
8273f55e | 10079 | |
4d1313e9 | 10080 | imsm_update_version_info(super); |
8273f55e | 10081 | break; |
ba2de7ba DW |
10082 | create_error: |
10083 | /* mdmon knows how to release update->space, but not | |
10084 | * ((struct intel_dev *) update->space)->dev | |
10085 | */ | |
10086 | if (update->space) { | |
10087 | dv = update->space; | |
10088 | free(dv->dev); | |
10089 | } | |
8273f55e | 10090 | break; |
e8319a19 | 10091 | } |
33414a01 DW |
10092 | case update_kill_array: { |
10093 | struct imsm_update_kill_array *u = (void *) update->buf; | |
10094 | int victim = u->dev_idx; | |
10095 | struct active_array *a; | |
10096 | struct intel_dev **dp; | |
10097 | struct imsm_dev *dev; | |
10098 | ||
10099 | /* sanity check that we are not affecting the uuid of | |
10100 | * active arrays, or deleting an active array | |
10101 | * | |
10102 | * FIXME when immutable ids are available, but note that | |
10103 | * we'll also need to fixup the invalidated/active | |
10104 | * subarray indexes in mdstat | |
10105 | */ | |
10106 | for (a = st->arrays; a; a = a->next) | |
10107 | if (a->info.container_member >= victim) | |
10108 | break; | |
10109 | /* by definition if mdmon is running at least one array | |
10110 | * is active in the container, so checking | |
10111 | * mpb->num_raid_devs is just extra paranoia | |
10112 | */ | |
10113 | dev = get_imsm_dev(super, victim); | |
10114 | if (a || !dev || mpb->num_raid_devs == 1) { | |
10115 | dprintf("failed to delete subarray-%d\n", victim); | |
10116 | break; | |
10117 | } | |
10118 | ||
10119 | for (dp = &super->devlist; *dp;) | |
f21e18ca | 10120 | if ((*dp)->index == (unsigned)super->current_vol) { |
33414a01 DW |
10121 | *dp = (*dp)->next; |
10122 | } else { | |
f21e18ca | 10123 | if ((*dp)->index > (unsigned)victim) |
33414a01 DW |
10124 | (*dp)->index--; |
10125 | dp = &(*dp)->next; | |
10126 | } | |
10127 | mpb->num_raid_devs--; | |
10128 | super->updates_pending++; | |
10129 | break; | |
10130 | } | |
aa534678 DW |
10131 | case update_rename_array: { |
10132 | struct imsm_update_rename_array *u = (void *) update->buf; | |
10133 | char name[MAX_RAID_SERIAL_LEN+1]; | |
10134 | int target = u->dev_idx; | |
10135 | struct active_array *a; | |
10136 | struct imsm_dev *dev; | |
10137 | ||
10138 | /* sanity check that we are not affecting the uuid of | |
10139 | * an active array | |
10140 | */ | |
40659392 | 10141 | memset(name, 0, sizeof(name)); |
aa534678 DW |
10142 | snprintf(name, MAX_RAID_SERIAL_LEN, "%s", (char *) u->name); |
10143 | name[MAX_RAID_SERIAL_LEN] = '\0'; | |
10144 | for (a = st->arrays; a; a = a->next) | |
10145 | if (a->info.container_member == target) | |
10146 | break; | |
10147 | dev = get_imsm_dev(super, u->dev_idx); | |
10148 | if (a || !dev || !check_name(super, name, 1)) { | |
10149 | dprintf("failed to rename subarray-%d\n", target); | |
10150 | break; | |
10151 | } | |
10152 | ||
40659392 | 10153 | memcpy(dev->volume, name, MAX_RAID_SERIAL_LEN); |
aa534678 DW |
10154 | super->updates_pending++; |
10155 | break; | |
10156 | } | |
1a64be56 | 10157 | case update_add_remove_disk: { |
43dad3d6 | 10158 | /* we may be able to repair some arrays if disks are |
095b8088 | 10159 | * being added, check the status of add_remove_disk |
1a64be56 LM |
10160 | * if discs has been added. |
10161 | */ | |
10162 | if (add_remove_disk_update(super)) { | |
43dad3d6 | 10163 | struct active_array *a; |
072b727f DW |
10164 | |
10165 | super->updates_pending++; | |
1a64be56 | 10166 | for (a = st->arrays; a; a = a->next) |
43dad3d6 DW |
10167 | a->check_degraded = 1; |
10168 | } | |
43dad3d6 | 10169 | break; |
e8319a19 | 10170 | } |
bbab0940 TM |
10171 | case update_prealloc_badblocks_mem: |
10172 | break; | |
e6e9dd3f AP |
10173 | case update_rwh_policy: { |
10174 | struct imsm_update_rwh_policy *u = (void *)update->buf; | |
10175 | int target = u->dev_idx; | |
10176 | struct imsm_dev *dev = get_imsm_dev(super, target); | |
10177 | if (!dev) { | |
10178 | dprintf("could not find subarray-%d\n", target); | |
10179 | break; | |
10180 | } | |
10181 | ||
10182 | if (dev->rwh_policy != u->new_policy) { | |
10183 | dev->rwh_policy = u->new_policy; | |
10184 | super->updates_pending++; | |
10185 | } | |
10186 | break; | |
10187 | } | |
1a64be56 | 10188 | default: |
ebf3be99 | 10189 | pr_err("error: unsupported process update type:(type: %d)\n", type); |
1a64be56 | 10190 | } |
e8319a19 | 10191 | } |
88758e9d | 10192 | |
bc0b9d34 PC |
10193 | static struct mdinfo *get_spares_for_grow(struct supertype *st); |
10194 | ||
5fe6f031 N |
10195 | static int imsm_prepare_update(struct supertype *st, |
10196 | struct metadata_update *update) | |
8273f55e | 10197 | { |
949c47a0 | 10198 | /** |
4d7b1503 DW |
10199 | * Allocate space to hold new disk entries, raid-device entries or a new |
10200 | * mpb if necessary. The manager synchronously waits for updates to | |
10201 | * complete in the monitor, so new mpb buffers allocated here can be | |
10202 | * integrated by the monitor thread without worrying about live pointers | |
10203 | * in the manager thread. | |
8273f55e | 10204 | */ |
095b8088 | 10205 | enum imsm_update_type type; |
4d7b1503 | 10206 | struct intel_super *super = st->sb; |
f36a9ecd | 10207 | unsigned int sector_size = super->sector_size; |
4d7b1503 DW |
10208 | struct imsm_super *mpb = super->anchor; |
10209 | size_t buf_len; | |
10210 | size_t len = 0; | |
949c47a0 | 10211 | |
095b8088 N |
10212 | if (update->len < (int)sizeof(type)) |
10213 | return 0; | |
10214 | ||
10215 | type = *(enum imsm_update_type *) update->buf; | |
10216 | ||
949c47a0 | 10217 | switch (type) { |
0ec5d470 | 10218 | case update_general_migration_checkpoint: |
095b8088 N |
10219 | if (update->len < (int)sizeof(struct imsm_update_general_migration_checkpoint)) |
10220 | return 0; | |
1ade5cc1 | 10221 | dprintf("called for update_general_migration_checkpoint\n"); |
0ec5d470 | 10222 | break; |
abedf5fc KW |
10223 | case update_takeover: { |
10224 | struct imsm_update_takeover *u = (void *)update->buf; | |
095b8088 N |
10225 | if (update->len < (int)sizeof(*u)) |
10226 | return 0; | |
abedf5fc KW |
10227 | if (u->direction == R0_TO_R10) { |
10228 | void **tail = (void **)&update->space_list; | |
10229 | struct imsm_dev *dev = get_imsm_dev(super, u->subarray); | |
238c0a71 | 10230 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
abedf5fc KW |
10231 | int num_members = map->num_members; |
10232 | void *space; | |
10233 | int size, i; | |
abedf5fc KW |
10234 | /* allocate memory for added disks */ |
10235 | for (i = 0; i < num_members; i++) { | |
10236 | size = sizeof(struct dl); | |
503975b9 | 10237 | space = xmalloc(size); |
abedf5fc KW |
10238 | *tail = space; |
10239 | tail = space; | |
10240 | *tail = NULL; | |
10241 | } | |
10242 | /* allocate memory for new device */ | |
10243 | size = sizeof_imsm_dev(super->devlist->dev, 0) + | |
10244 | (num_members * sizeof(__u32)); | |
503975b9 N |
10245 | space = xmalloc(size); |
10246 | *tail = space; | |
10247 | tail = space; | |
10248 | *tail = NULL; | |
10249 | len = disks_to_mpb_size(num_members * 2); | |
abedf5fc KW |
10250 | } |
10251 | ||
10252 | break; | |
10253 | } | |
78b10e66 | 10254 | case update_reshape_container_disks: { |
d195167d AK |
10255 | /* Every raid device in the container is about to |
10256 | * gain some more devices, and we will enter a | |
10257 | * reconfiguration. | |
10258 | * So each 'imsm_map' will be bigger, and the imsm_vol | |
10259 | * will now hold 2 of them. | |
10260 | * Thus we need new 'struct imsm_dev' allocations sized | |
10261 | * as sizeof_imsm_dev but with more devices in both maps. | |
10262 | */ | |
10263 | struct imsm_update_reshape *u = (void *)update->buf; | |
10264 | struct intel_dev *dl; | |
10265 | void **space_tail = (void**)&update->space_list; | |
10266 | ||
095b8088 N |
10267 | if (update->len < (int)sizeof(*u)) |
10268 | return 0; | |
10269 | ||
1ade5cc1 | 10270 | dprintf("for update_reshape\n"); |
d195167d AK |
10271 | |
10272 | for (dl = super->devlist; dl; dl = dl->next) { | |
10273 | int size = sizeof_imsm_dev(dl->dev, 1); | |
10274 | void *s; | |
d677e0b8 AK |
10275 | if (u->new_raid_disks > u->old_raid_disks) |
10276 | size += sizeof(__u32)*2* | |
10277 | (u->new_raid_disks - u->old_raid_disks); | |
503975b9 | 10278 | s = xmalloc(size); |
d195167d AK |
10279 | *space_tail = s; |
10280 | space_tail = s; | |
10281 | *space_tail = NULL; | |
10282 | } | |
10283 | ||
10284 | len = disks_to_mpb_size(u->new_raid_disks); | |
10285 | dprintf("New anchor length is %llu\n", (unsigned long long)len); | |
78b10e66 N |
10286 | break; |
10287 | } | |
48c5303a | 10288 | case update_reshape_migration: { |
bc0b9d34 PC |
10289 | /* for migration level 0->5 we need to add disks |
10290 | * so the same as for container operation we will copy | |
10291 | * device to the bigger location. | |
10292 | * in memory prepared device and new disk area are prepared | |
10293 | * for usage in process update | |
10294 | */ | |
10295 | struct imsm_update_reshape_migration *u = (void *)update->buf; | |
10296 | struct intel_dev *id; | |
10297 | void **space_tail = (void **)&update->space_list; | |
10298 | int size; | |
10299 | void *s; | |
10300 | int current_level = -1; | |
10301 | ||
095b8088 N |
10302 | if (update->len < (int)sizeof(*u)) |
10303 | return 0; | |
10304 | ||
1ade5cc1 | 10305 | dprintf("for update_reshape\n"); |
bc0b9d34 PC |
10306 | |
10307 | /* add space for bigger array in update | |
10308 | */ | |
10309 | for (id = super->devlist; id; id = id->next) { | |
10310 | if (id->index == (unsigned)u->subdev) { | |
10311 | size = sizeof_imsm_dev(id->dev, 1); | |
10312 | if (u->new_raid_disks > u->old_raid_disks) | |
10313 | size += sizeof(__u32)*2* | |
10314 | (u->new_raid_disks - u->old_raid_disks); | |
503975b9 | 10315 | s = xmalloc(size); |
bc0b9d34 PC |
10316 | *space_tail = s; |
10317 | space_tail = s; | |
10318 | *space_tail = NULL; | |
10319 | break; | |
10320 | } | |
10321 | } | |
10322 | if (update->space_list == NULL) | |
10323 | break; | |
10324 | ||
10325 | /* add space for disk in update | |
10326 | */ | |
10327 | size = sizeof(struct dl); | |
503975b9 | 10328 | s = xmalloc(size); |
bc0b9d34 PC |
10329 | *space_tail = s; |
10330 | space_tail = s; | |
10331 | *space_tail = NULL; | |
10332 | ||
10333 | /* add spare device to update | |
10334 | */ | |
10335 | for (id = super->devlist ; id; id = id->next) | |
10336 | if (id->index == (unsigned)u->subdev) { | |
10337 | struct imsm_dev *dev; | |
10338 | struct imsm_map *map; | |
10339 | ||
10340 | dev = get_imsm_dev(super, u->subdev); | |
238c0a71 | 10341 | map = get_imsm_map(dev, MAP_0); |
bc0b9d34 PC |
10342 | current_level = map->raid_level; |
10343 | break; | |
10344 | } | |
089f9d79 | 10345 | if (u->new_level == 5 && u->new_level != current_level) { |
bc0b9d34 PC |
10346 | struct mdinfo *spares; |
10347 | ||
10348 | spares = get_spares_for_grow(st); | |
10349 | if (spares) { | |
10350 | struct dl *dl; | |
10351 | struct mdinfo *dev; | |
10352 | ||
10353 | dev = spares->devs; | |
10354 | if (dev) { | |
10355 | u->new_disks[0] = | |
10356 | makedev(dev->disk.major, | |
10357 | dev->disk.minor); | |
10358 | dl = get_disk_super(super, | |
10359 | dev->disk.major, | |
10360 | dev->disk.minor); | |
10361 | dl->index = u->old_raid_disks; | |
10362 | dev = dev->next; | |
10363 | } | |
10364 | sysfs_free(spares); | |
10365 | } | |
10366 | } | |
10367 | len = disks_to_mpb_size(u->new_raid_disks); | |
10368 | dprintf("New anchor length is %llu\n", (unsigned long long)len); | |
48c5303a PC |
10369 | break; |
10370 | } | |
f3871fdc | 10371 | case update_size_change: { |
095b8088 N |
10372 | if (update->len < (int)sizeof(struct imsm_update_size_change)) |
10373 | return 0; | |
10374 | break; | |
10375 | } | |
10376 | case update_activate_spare: { | |
10377 | if (update->len < (int)sizeof(struct imsm_update_activate_spare)) | |
10378 | return 0; | |
f3871fdc AK |
10379 | break; |
10380 | } | |
949c47a0 DW |
10381 | case update_create_array: { |
10382 | struct imsm_update_create_array *u = (void *) update->buf; | |
ba2de7ba | 10383 | struct intel_dev *dv; |
54c2c1ea | 10384 | struct imsm_dev *dev = &u->dev; |
238c0a71 | 10385 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
54c2c1ea DW |
10386 | struct dl *dl; |
10387 | struct disk_info *inf; | |
10388 | int i; | |
10389 | int activate = 0; | |
949c47a0 | 10390 | |
095b8088 N |
10391 | if (update->len < (int)sizeof(*u)) |
10392 | return 0; | |
10393 | ||
54c2c1ea DW |
10394 | inf = get_disk_info(u); |
10395 | len = sizeof_imsm_dev(dev, 1); | |
ba2de7ba | 10396 | /* allocate a new super->devlist entry */ |
503975b9 N |
10397 | dv = xmalloc(sizeof(*dv)); |
10398 | dv->dev = xmalloc(len); | |
10399 | update->space = dv; | |
949c47a0 | 10400 | |
54c2c1ea DW |
10401 | /* count how many spares will be converted to members */ |
10402 | for (i = 0; i < map->num_members; i++) { | |
10403 | dl = serial_to_dl(inf[i].serial, super); | |
10404 | if (!dl) { | |
10405 | /* hmm maybe it failed?, nothing we can do about | |
10406 | * it here | |
10407 | */ | |
10408 | continue; | |
10409 | } | |
10410 | if (count_memberships(dl, super) == 0) | |
10411 | activate++; | |
10412 | } | |
10413 | len += activate * sizeof(struct imsm_disk); | |
949c47a0 | 10414 | break; |
095b8088 N |
10415 | } |
10416 | case update_kill_array: { | |
10417 | if (update->len < (int)sizeof(struct imsm_update_kill_array)) | |
10418 | return 0; | |
949c47a0 DW |
10419 | break; |
10420 | } | |
095b8088 N |
10421 | case update_rename_array: { |
10422 | if (update->len < (int)sizeof(struct imsm_update_rename_array)) | |
10423 | return 0; | |
10424 | break; | |
10425 | } | |
10426 | case update_add_remove_disk: | |
10427 | /* no update->len needed */ | |
10428 | break; | |
bbab0940 TM |
10429 | case update_prealloc_badblocks_mem: |
10430 | super->extra_space += sizeof(struct bbm_log) - | |
10431 | get_imsm_bbm_log_size(super->bbm_log); | |
10432 | break; | |
e6e9dd3f AP |
10433 | case update_rwh_policy: { |
10434 | if (update->len < (int)sizeof(struct imsm_update_rwh_policy)) | |
10435 | return 0; | |
10436 | break; | |
10437 | } | |
095b8088 N |
10438 | default: |
10439 | return 0; | |
949c47a0 | 10440 | } |
8273f55e | 10441 | |
4d7b1503 DW |
10442 | /* check if we need a larger metadata buffer */ |
10443 | if (super->next_buf) | |
10444 | buf_len = super->next_len; | |
10445 | else | |
10446 | buf_len = super->len; | |
10447 | ||
bbab0940 | 10448 | if (__le32_to_cpu(mpb->mpb_size) + super->extra_space + len > buf_len) { |
4d7b1503 DW |
10449 | /* ok we need a larger buf than what is currently allocated |
10450 | * if this allocation fails process_update will notice that | |
10451 | * ->next_len is set and ->next_buf is NULL | |
10452 | */ | |
bbab0940 TM |
10453 | buf_len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + |
10454 | super->extra_space + len, sector_size); | |
4d7b1503 DW |
10455 | if (super->next_buf) |
10456 | free(super->next_buf); | |
10457 | ||
10458 | super->next_len = buf_len; | |
f36a9ecd | 10459 | if (posix_memalign(&super->next_buf, sector_size, buf_len) == 0) |
1f45a8ad DW |
10460 | memset(super->next_buf, 0, buf_len); |
10461 | else | |
4d7b1503 DW |
10462 | super->next_buf = NULL; |
10463 | } | |
5fe6f031 | 10464 | return 1; |
8273f55e DW |
10465 | } |
10466 | ||
ae6aad82 | 10467 | /* must be called while manager is quiesced */ |
f21e18ca | 10468 | static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index) |
ae6aad82 DW |
10469 | { |
10470 | struct imsm_super *mpb = super->anchor; | |
ae6aad82 DW |
10471 | struct dl *iter; |
10472 | struct imsm_dev *dev; | |
10473 | struct imsm_map *map; | |
4c9e8c1e | 10474 | unsigned int i, j, num_members; |
fb12a745 | 10475 | __u32 ord, ord_map0; |
4c9e8c1e | 10476 | struct bbm_log *log = super->bbm_log; |
ae6aad82 | 10477 | |
1ade5cc1 | 10478 | dprintf("deleting device[%d] from imsm_super\n", index); |
ae6aad82 DW |
10479 | |
10480 | /* shift all indexes down one */ | |
10481 | for (iter = super->disks; iter; iter = iter->next) | |
f21e18ca | 10482 | if (iter->index > (int)index) |
ae6aad82 | 10483 | iter->index--; |
47ee5a45 | 10484 | for (iter = super->missing; iter; iter = iter->next) |
f21e18ca | 10485 | if (iter->index > (int)index) |
47ee5a45 | 10486 | iter->index--; |
ae6aad82 DW |
10487 | |
10488 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
10489 | dev = get_imsm_dev(super, i); | |
238c0a71 | 10490 | map = get_imsm_map(dev, MAP_0); |
24565c9a DW |
10491 | num_members = map->num_members; |
10492 | for (j = 0; j < num_members; j++) { | |
10493 | /* update ord entries being careful not to propagate | |
10494 | * ord-flags to the first map | |
10495 | */ | |
238c0a71 | 10496 | ord = get_imsm_ord_tbl_ent(dev, j, MAP_X); |
fb12a745 | 10497 | ord_map0 = get_imsm_ord_tbl_ent(dev, j, MAP_0); |
ae6aad82 | 10498 | |
24565c9a DW |
10499 | if (ord_to_idx(ord) <= index) |
10500 | continue; | |
ae6aad82 | 10501 | |
238c0a71 | 10502 | map = get_imsm_map(dev, MAP_0); |
fb12a745 | 10503 | set_imsm_ord_tbl_ent(map, j, ord_map0 - 1); |
238c0a71 | 10504 | map = get_imsm_map(dev, MAP_1); |
24565c9a DW |
10505 | if (map) |
10506 | set_imsm_ord_tbl_ent(map, j, ord - 1); | |
ae6aad82 DW |
10507 | } |
10508 | } | |
10509 | ||
4c9e8c1e TM |
10510 | for (i = 0; i < log->entry_count; i++) { |
10511 | struct bbm_log_entry *entry = &log->marked_block_entries[i]; | |
10512 | ||
10513 | if (entry->disk_ordinal <= index) | |
10514 | continue; | |
10515 | entry->disk_ordinal--; | |
10516 | } | |
10517 | ||
ae6aad82 DW |
10518 | mpb->num_disks--; |
10519 | super->updates_pending++; | |
24565c9a DW |
10520 | if (*dlp) { |
10521 | struct dl *dl = *dlp; | |
10522 | ||
10523 | *dlp = (*dlp)->next; | |
3a85bf0e | 10524 | __free_imsm_disk(dl, 1); |
24565c9a | 10525 | } |
ae6aad82 | 10526 | } |
9a717282 | 10527 | |
9a717282 AK |
10528 | static int imsm_get_allowed_degradation(int level, int raid_disks, |
10529 | struct intel_super *super, | |
10530 | struct imsm_dev *dev) | |
10531 | { | |
10532 | switch (level) { | |
bf5cf7c7 | 10533 | case 1: |
9a717282 AK |
10534 | case 10:{ |
10535 | int ret_val = 0; | |
10536 | struct imsm_map *map; | |
10537 | int i; | |
10538 | ||
10539 | ret_val = raid_disks/2; | |
10540 | /* check map if all disks pairs not failed | |
10541 | * in both maps | |
10542 | */ | |
238c0a71 | 10543 | map = get_imsm_map(dev, MAP_0); |
9a717282 AK |
10544 | for (i = 0; i < ret_val; i++) { |
10545 | int degradation = 0; | |
10546 | if (get_imsm_disk(super, i) == NULL) | |
10547 | degradation++; | |
10548 | if (get_imsm_disk(super, i + 1) == NULL) | |
10549 | degradation++; | |
10550 | if (degradation == 2) | |
10551 | return 0; | |
10552 | } | |
238c0a71 | 10553 | map = get_imsm_map(dev, MAP_1); |
9a717282 AK |
10554 | /* if there is no second map |
10555 | * result can be returned | |
10556 | */ | |
10557 | if (map == NULL) | |
10558 | return ret_val; | |
10559 | /* check degradation in second map | |
10560 | */ | |
10561 | for (i = 0; i < ret_val; i++) { | |
10562 | int degradation = 0; | |
10563 | if (get_imsm_disk(super, i) == NULL) | |
10564 | degradation++; | |
10565 | if (get_imsm_disk(super, i + 1) == NULL) | |
10566 | degradation++; | |
10567 | if (degradation == 2) | |
10568 | return 0; | |
10569 | } | |
10570 | return ret_val; | |
10571 | } | |
10572 | case 5: | |
10573 | return 1; | |
10574 | case 6: | |
10575 | return 2; | |
10576 | default: | |
10577 | return 0; | |
10578 | } | |
10579 | } | |
10580 | ||
d31ad643 PB |
10581 | /******************************************************************************* |
10582 | * Function: validate_container_imsm | |
10583 | * Description: This routine validates container after assemble, | |
10584 | * eg. if devices in container are under the same controller. | |
10585 | * | |
10586 | * Parameters: | |
10587 | * info : linked list with info about devices used in array | |
10588 | * Returns: | |
10589 | * 1 : HBA mismatch | |
10590 | * 0 : Success | |
10591 | ******************************************************************************/ | |
10592 | int validate_container_imsm(struct mdinfo *info) | |
10593 | { | |
6b781d33 AP |
10594 | if (check_env("IMSM_NO_PLATFORM")) |
10595 | return 0; | |
d31ad643 | 10596 | |
6b781d33 AP |
10597 | struct sys_dev *idev; |
10598 | struct sys_dev *hba = NULL; | |
10599 | struct sys_dev *intel_devices = find_intel_devices(); | |
10600 | char *dev_path = devt_to_devpath(makedev(info->disk.major, | |
7c798f87 | 10601 | info->disk.minor), 1, NULL); |
6b781d33 AP |
10602 | |
10603 | for (idev = intel_devices; idev; idev = idev->next) { | |
10604 | if (dev_path && strstr(dev_path, idev->path)) { | |
10605 | hba = idev; | |
10606 | break; | |
d31ad643 | 10607 | } |
6b781d33 AP |
10608 | } |
10609 | if (dev_path) | |
d31ad643 PB |
10610 | free(dev_path); |
10611 | ||
6b781d33 AP |
10612 | if (!hba) { |
10613 | pr_err("WARNING - Cannot detect HBA for device %s!\n", | |
10614 | devid2kname(makedev(info->disk.major, info->disk.minor))); | |
10615 | return 1; | |
10616 | } | |
10617 | ||
10618 | const struct imsm_orom *orom = get_orom_by_device_id(hba->dev_id); | |
10619 | struct mdinfo *dev; | |
10620 | ||
10621 | for (dev = info->next; dev; dev = dev->next) { | |
7c798f87 MT |
10622 | dev_path = devt_to_devpath(makedev(dev->disk.major, |
10623 | dev->disk.minor), 1, NULL); | |
6b781d33 AP |
10624 | |
10625 | struct sys_dev *hba2 = NULL; | |
10626 | for (idev = intel_devices; idev; idev = idev->next) { | |
10627 | if (dev_path && strstr(dev_path, idev->path)) { | |
10628 | hba2 = idev; | |
10629 | break; | |
d31ad643 PB |
10630 | } |
10631 | } | |
6b781d33 AP |
10632 | if (dev_path) |
10633 | free(dev_path); | |
10634 | ||
10635 | const struct imsm_orom *orom2 = hba2 == NULL ? NULL : | |
10636 | get_orom_by_device_id(hba2->dev_id); | |
10637 | ||
10638 | if (hba2 && hba->type != hba2->type) { | |
10639 | pr_err("WARNING - HBAs of devices do not match %s != %s\n", | |
10640 | get_sys_dev_type(hba->type), get_sys_dev_type(hba2->type)); | |
10641 | return 1; | |
10642 | } | |
10643 | ||
07cb1e57 | 10644 | if (orom != orom2) { |
6b781d33 AP |
10645 | pr_err("WARNING - IMSM container assembled with disks under different HBAs!\n" |
10646 | " This operation is not supported and can lead to data loss.\n"); | |
10647 | return 1; | |
10648 | } | |
10649 | ||
10650 | if (!orom) { | |
10651 | pr_err("WARNING - IMSM container assembled with disks under HBAs without IMSM platform support!\n" | |
10652 | " This operation is not supported and can lead to data loss.\n"); | |
10653 | return 1; | |
10654 | } | |
d31ad643 | 10655 | } |
6b781d33 | 10656 | |
d31ad643 PB |
10657 | return 0; |
10658 | } | |
32141c17 | 10659 | |
6f50473f TM |
10660 | /******************************************************************************* |
10661 | * Function: imsm_record_badblock | |
10662 | * Description: This routine stores new bad block record in BBM log | |
10663 | * | |
10664 | * Parameters: | |
10665 | * a : array containing a bad block | |
10666 | * slot : disk number containing a bad block | |
10667 | * sector : bad block sector | |
10668 | * length : bad block sectors range | |
10669 | * Returns: | |
10670 | * 1 : Success | |
10671 | * 0 : Error | |
10672 | ******************************************************************************/ | |
10673 | static int imsm_record_badblock(struct active_array *a, int slot, | |
10674 | unsigned long long sector, int length) | |
10675 | { | |
10676 | struct intel_super *super = a->container->sb; | |
10677 | int ord; | |
10678 | int ret; | |
10679 | ||
10680 | ord = imsm_disk_slot_to_ord(a, slot); | |
10681 | if (ord < 0) | |
10682 | return 0; | |
10683 | ||
10684 | ret = record_new_badblock(super->bbm_log, ord_to_idx(ord), sector, | |
10685 | length); | |
10686 | if (ret) | |
10687 | super->updates_pending++; | |
10688 | ||
10689 | return ret; | |
10690 | } | |
c07a5a4f TM |
10691 | /******************************************************************************* |
10692 | * Function: imsm_clear_badblock | |
10693 | * Description: This routine clears bad block record from BBM log | |
10694 | * | |
10695 | * Parameters: | |
10696 | * a : array containing a bad block | |
10697 | * slot : disk number containing a bad block | |
10698 | * sector : bad block sector | |
10699 | * length : bad block sectors range | |
10700 | * Returns: | |
10701 | * 1 : Success | |
10702 | * 0 : Error | |
10703 | ******************************************************************************/ | |
10704 | static int imsm_clear_badblock(struct active_array *a, int slot, | |
10705 | unsigned long long sector, int length) | |
10706 | { | |
10707 | struct intel_super *super = a->container->sb; | |
10708 | int ord; | |
10709 | int ret; | |
10710 | ||
10711 | ord = imsm_disk_slot_to_ord(a, slot); | |
10712 | if (ord < 0) | |
10713 | return 0; | |
10714 | ||
10715 | ret = clear_badblock(super->bbm_log, ord_to_idx(ord), sector, length); | |
10716 | if (ret) | |
10717 | super->updates_pending++; | |
10718 | ||
10719 | return ret; | |
10720 | } | |
928f1424 TM |
10721 | /******************************************************************************* |
10722 | * Function: imsm_get_badblocks | |
10723 | * Description: This routine get list of bad blocks for an array | |
10724 | * | |
10725 | * Parameters: | |
10726 | * a : array | |
10727 | * slot : disk number | |
10728 | * Returns: | |
10729 | * bb : structure containing bad blocks | |
10730 | * NULL : error | |
10731 | ******************************************************************************/ | |
10732 | static struct md_bb *imsm_get_badblocks(struct active_array *a, int slot) | |
10733 | { | |
10734 | int inst = a->info.container_member; | |
10735 | struct intel_super *super = a->container->sb; | |
10736 | struct imsm_dev *dev = get_imsm_dev(super, inst); | |
10737 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
10738 | int ord; | |
10739 | ||
10740 | ord = imsm_disk_slot_to_ord(a, slot); | |
10741 | if (ord < 0) | |
10742 | return NULL; | |
10743 | ||
10744 | get_volume_badblocks(super->bbm_log, ord_to_idx(ord), pba_of_lba0(map), | |
44490938 | 10745 | per_dev_array_size(map), &super->bb); |
928f1424 TM |
10746 | |
10747 | return &super->bb; | |
10748 | } | |
27156a57 TM |
10749 | /******************************************************************************* |
10750 | * Function: examine_badblocks_imsm | |
10751 | * Description: Prints list of bad blocks on a disk to the standard output | |
10752 | * | |
10753 | * Parameters: | |
10754 | * st : metadata handler | |
10755 | * fd : open file descriptor for device | |
10756 | * devname : device name | |
10757 | * Returns: | |
10758 | * 0 : Success | |
10759 | * 1 : Error | |
10760 | ******************************************************************************/ | |
10761 | static int examine_badblocks_imsm(struct supertype *st, int fd, char *devname) | |
10762 | { | |
10763 | struct intel_super *super = st->sb; | |
10764 | struct bbm_log *log = super->bbm_log; | |
10765 | struct dl *d = NULL; | |
10766 | int any = 0; | |
10767 | ||
10768 | for (d = super->disks; d ; d = d->next) { | |
10769 | if (strcmp(d->devname, devname) == 0) | |
10770 | break; | |
10771 | } | |
10772 | ||
10773 | if ((d == NULL) || (d->index < 0)) { /* serial mismatch probably */ | |
10774 | pr_err("%s doesn't appear to be part of a raid array\n", | |
10775 | devname); | |
10776 | return 1; | |
10777 | } | |
10778 | ||
10779 | if (log != NULL) { | |
10780 | unsigned int i; | |
10781 | struct bbm_log_entry *entry = &log->marked_block_entries[0]; | |
10782 | ||
10783 | for (i = 0; i < log->entry_count; i++) { | |
10784 | if (entry[i].disk_ordinal == d->index) { | |
10785 | unsigned long long sector = __le48_to_cpu( | |
10786 | &entry[i].defective_block_start); | |
10787 | int cnt = entry[i].marked_count + 1; | |
10788 | ||
10789 | if (!any) { | |
10790 | printf("Bad-blocks on %s:\n", devname); | |
10791 | any = 1; | |
10792 | } | |
10793 | ||
10794 | printf("%20llu for %d sectors\n", sector, cnt); | |
10795 | } | |
10796 | } | |
10797 | } | |
10798 | ||
10799 | if (!any) | |
10800 | printf("No bad-blocks list configured on %s\n", devname); | |
10801 | ||
10802 | return 0; | |
10803 | } | |
687629c2 AK |
10804 | /******************************************************************************* |
10805 | * Function: init_migr_record_imsm | |
10806 | * Description: Function inits imsm migration record | |
10807 | * Parameters: | |
10808 | * super : imsm internal array info | |
10809 | * dev : device under migration | |
10810 | * info : general array info to find the smallest device | |
10811 | * Returns: | |
10812 | * none | |
10813 | ******************************************************************************/ | |
10814 | void init_migr_record_imsm(struct supertype *st, struct imsm_dev *dev, | |
10815 | struct mdinfo *info) | |
10816 | { | |
10817 | struct intel_super *super = st->sb; | |
10818 | struct migr_record *migr_rec = super->migr_rec; | |
10819 | int new_data_disks; | |
10820 | unsigned long long dsize, dev_sectors; | |
10821 | long long unsigned min_dev_sectors = -1LLU; | |
238c0a71 AK |
10822 | struct imsm_map *map_dest = get_imsm_map(dev, MAP_0); |
10823 | struct imsm_map *map_src = get_imsm_map(dev, MAP_1); | |
687629c2 | 10824 | unsigned long long num_migr_units; |
3ef4403c | 10825 | unsigned long long array_blocks; |
2f86fda3 | 10826 | struct dl *dl_disk = NULL; |
687629c2 AK |
10827 | |
10828 | memset(migr_rec, 0, sizeof(struct migr_record)); | |
10829 | migr_rec->family_num = __cpu_to_le32(super->anchor->family_num); | |
10830 | ||
10831 | /* only ascending reshape supported now */ | |
10832 | migr_rec->ascending_migr = __cpu_to_le32(1); | |
10833 | ||
10834 | migr_rec->dest_depth_per_unit = GEN_MIGR_AREA_SIZE / | |
10835 | max(map_dest->blocks_per_strip, map_src->blocks_per_strip); | |
e1742195 AK |
10836 | migr_rec->dest_depth_per_unit *= |
10837 | max(map_dest->blocks_per_strip, map_src->blocks_per_strip); | |
9529d343 | 10838 | new_data_disks = imsm_num_data_members(map_dest); |
687629c2 AK |
10839 | migr_rec->blocks_per_unit = |
10840 | __cpu_to_le32(migr_rec->dest_depth_per_unit * new_data_disks); | |
10841 | migr_rec->dest_depth_per_unit = | |
10842 | __cpu_to_le32(migr_rec->dest_depth_per_unit); | |
3ef4403c | 10843 | array_blocks = info->component_size * new_data_disks; |
687629c2 AK |
10844 | num_migr_units = |
10845 | array_blocks / __le32_to_cpu(migr_rec->blocks_per_unit); | |
10846 | ||
10847 | if (array_blocks % __le32_to_cpu(migr_rec->blocks_per_unit)) | |
10848 | num_migr_units++; | |
9f421827 | 10849 | set_num_migr_units(migr_rec, num_migr_units); |
687629c2 AK |
10850 | |
10851 | migr_rec->post_migr_vol_cap = dev->size_low; | |
10852 | migr_rec->post_migr_vol_cap_hi = dev->size_high; | |
10853 | ||
687629c2 | 10854 | /* Find the smallest dev */ |
2f86fda3 MT |
10855 | for (dl_disk = super->disks; dl_disk ; dl_disk = dl_disk->next) { |
10856 | /* ignore spares in container */ | |
10857 | if (dl_disk->index < 0) | |
687629c2 | 10858 | continue; |
2f86fda3 | 10859 | get_dev_size(dl_disk->fd, NULL, &dsize); |
687629c2 AK |
10860 | dev_sectors = dsize / 512; |
10861 | if (dev_sectors < min_dev_sectors) | |
10862 | min_dev_sectors = dev_sectors; | |
687629c2 | 10863 | } |
9f421827 | 10864 | set_migr_chkp_area_pba(migr_rec, min_dev_sectors - |
687629c2 AK |
10865 | RAID_DISK_RESERVED_BLOCKS_IMSM_HI); |
10866 | ||
10867 | write_imsm_migr_rec(st); | |
10868 | ||
10869 | return; | |
10870 | } | |
10871 | ||
10872 | /******************************************************************************* | |
10873 | * Function: save_backup_imsm | |
10874 | * Description: Function saves critical data stripes to Migration Copy Area | |
10875 | * and updates the current migration unit status. | |
10876 | * Use restore_stripes() to form a destination stripe, | |
10877 | * and to write it to the Copy Area. | |
10878 | * Parameters: | |
10879 | * st : supertype information | |
aea93171 | 10880 | * dev : imsm device that backup is saved for |
687629c2 AK |
10881 | * info : general array info |
10882 | * buf : input buffer | |
687629c2 AK |
10883 | * length : length of data to backup (blocks_per_unit) |
10884 | * Returns: | |
10885 | * 0 : success | |
10886 | *, -1 : fail | |
10887 | ******************************************************************************/ | |
10888 | int save_backup_imsm(struct supertype *st, | |
10889 | struct imsm_dev *dev, | |
10890 | struct mdinfo *info, | |
10891 | void *buf, | |
687629c2 AK |
10892 | int length) |
10893 | { | |
10894 | int rv = -1; | |
10895 | struct intel_super *super = st->sb; | |
594dc1b8 JS |
10896 | unsigned long long *target_offsets; |
10897 | int *targets; | |
687629c2 | 10898 | int i; |
238c0a71 | 10899 | struct imsm_map *map_dest = get_imsm_map(dev, MAP_0); |
687629c2 | 10900 | int new_disks = map_dest->num_members; |
ab724b98 AK |
10901 | int dest_layout = 0; |
10902 | int dest_chunk; | |
d1877f69 | 10903 | unsigned long long start; |
9529d343 | 10904 | int data_disks = imsm_num_data_members(map_dest); |
687629c2 | 10905 | |
503975b9 | 10906 | targets = xmalloc(new_disks * sizeof(int)); |
687629c2 | 10907 | |
2f86fda3 MT |
10908 | for (i = 0; i < new_disks; i++) { |
10909 | struct dl *dl_disk = get_imsm_dl_disk(super, i); | |
10910 | ||
10911 | targets[i] = dl_disk->fd; | |
10912 | } | |
7e45b550 | 10913 | |
503975b9 | 10914 | target_offsets = xcalloc(new_disks, sizeof(unsigned long long)); |
687629c2 | 10915 | |
d1877f69 | 10916 | start = info->reshape_progress * 512; |
687629c2 | 10917 | for (i = 0; i < new_disks; i++) { |
9f421827 | 10918 | target_offsets[i] = migr_chkp_area_pba(super->migr_rec) * 512; |
d1877f69 AK |
10919 | /* move back copy area adderss, it will be moved forward |
10920 | * in restore_stripes() using start input variable | |
10921 | */ | |
10922 | target_offsets[i] -= start/data_disks; | |
687629c2 AK |
10923 | } |
10924 | ||
68eb8bc6 | 10925 | dest_layout = imsm_level_to_layout(map_dest->raid_level); |
ab724b98 AK |
10926 | dest_chunk = __le16_to_cpu(map_dest->blocks_per_strip) * 512; |
10927 | ||
687629c2 AK |
10928 | if (restore_stripes(targets, /* list of dest devices */ |
10929 | target_offsets, /* migration record offsets */ | |
10930 | new_disks, | |
ab724b98 AK |
10931 | dest_chunk, |
10932 | map_dest->raid_level, | |
10933 | dest_layout, | |
10934 | -1, /* source backup file descriptor */ | |
10935 | 0, /* input buf offset | |
10936 | * always 0 buf is already offseted */ | |
d1877f69 | 10937 | start, |
687629c2 AK |
10938 | length, |
10939 | buf) != 0) { | |
e7b84f9d | 10940 | pr_err("Error restoring stripes\n"); |
687629c2 AK |
10941 | goto abort; |
10942 | } | |
10943 | ||
10944 | rv = 0; | |
10945 | ||
10946 | abort: | |
10947 | if (targets) { | |
687629c2 AK |
10948 | free(targets); |
10949 | } | |
10950 | free(target_offsets); | |
10951 | ||
10952 | return rv; | |
10953 | } | |
10954 | ||
10955 | /******************************************************************************* | |
10956 | * Function: save_checkpoint_imsm | |
10957 | * Description: Function called for current unit status update | |
10958 | * in the migration record. It writes it to disk. | |
10959 | * Parameters: | |
10960 | * super : imsm internal array info | |
10961 | * info : general array info | |
10962 | * Returns: | |
10963 | * 0: success | |
10964 | * 1: failure | |
0228d92c AK |
10965 | * 2: failure, means no valid migration record |
10966 | * / no general migration in progress / | |
687629c2 AK |
10967 | ******************************************************************************/ |
10968 | int save_checkpoint_imsm(struct supertype *st, struct mdinfo *info, int state) | |
10969 | { | |
10970 | struct intel_super *super = st->sb; | |
f8b72ef5 AK |
10971 | unsigned long long blocks_per_unit; |
10972 | unsigned long long curr_migr_unit; | |
10973 | ||
2f86fda3 | 10974 | if (load_imsm_migr_rec(super) != 0) { |
7a862a02 | 10975 | dprintf("imsm: ERROR: Cannot read migration record for checkpoint save.\n"); |
2e062e82 AK |
10976 | return 1; |
10977 | } | |
10978 | ||
f8b72ef5 AK |
10979 | blocks_per_unit = __le32_to_cpu(super->migr_rec->blocks_per_unit); |
10980 | if (blocks_per_unit == 0) { | |
0228d92c AK |
10981 | dprintf("imsm: no migration in progress.\n"); |
10982 | return 2; | |
687629c2 | 10983 | } |
f8b72ef5 AK |
10984 | curr_migr_unit = info->reshape_progress / blocks_per_unit; |
10985 | /* check if array is alligned to copy area | |
10986 | * if it is not alligned, add one to current migration unit value | |
10987 | * this can happend on array reshape finish only | |
10988 | */ | |
10989 | if (info->reshape_progress % blocks_per_unit) | |
10990 | curr_migr_unit++; | |
687629c2 | 10991 | |
9f421827 | 10992 | set_current_migr_unit(super->migr_rec, curr_migr_unit); |
687629c2 | 10993 | super->migr_rec->rec_status = __cpu_to_le32(state); |
9f421827 PB |
10994 | set_migr_dest_1st_member_lba(super->migr_rec, |
10995 | super->migr_rec->dest_depth_per_unit * curr_migr_unit); | |
10996 | ||
687629c2 | 10997 | if (write_imsm_migr_rec(st) < 0) { |
7a862a02 | 10998 | dprintf("imsm: Cannot write migration record outside backup area\n"); |
687629c2 AK |
10999 | return 1; |
11000 | } | |
11001 | ||
11002 | return 0; | |
11003 | } | |
11004 | ||
276d77db AK |
11005 | /******************************************************************************* |
11006 | * Function: recover_backup_imsm | |
11007 | * Description: Function recovers critical data from the Migration Copy Area | |
11008 | * while assembling an array. | |
11009 | * Parameters: | |
11010 | * super : imsm internal array info | |
11011 | * info : general array info | |
11012 | * Returns: | |
11013 | * 0 : success (or there is no data to recover) | |
11014 | * 1 : fail | |
11015 | ******************************************************************************/ | |
11016 | int recover_backup_imsm(struct supertype *st, struct mdinfo *info) | |
11017 | { | |
11018 | struct intel_super *super = st->sb; | |
11019 | struct migr_record *migr_rec = super->migr_rec; | |
594dc1b8 | 11020 | struct imsm_map *map_dest; |
276d77db AK |
11021 | struct intel_dev *id = NULL; |
11022 | unsigned long long read_offset; | |
11023 | unsigned long long write_offset; | |
11024 | unsigned unit_len; | |
2f86fda3 | 11025 | int new_disks, err; |
276d77db AK |
11026 | char *buf = NULL; |
11027 | int retval = 1; | |
f36a9ecd | 11028 | unsigned int sector_size = super->sector_size; |
4036e7ee MT |
11029 | unsigned long long curr_migr_unit = current_migr_unit(migr_rec); |
11030 | unsigned long long num_migr_units = get_num_migr_units(migr_rec); | |
276d77db | 11031 | char buffer[20]; |
6c3560c0 | 11032 | int skipped_disks = 0; |
2f86fda3 | 11033 | struct dl *dl_disk; |
276d77db AK |
11034 | |
11035 | err = sysfs_get_str(info, NULL, "array_state", (char *)buffer, 20); | |
11036 | if (err < 1) | |
11037 | return 1; | |
11038 | ||
11039 | /* recover data only during assemblation */ | |
11040 | if (strncmp(buffer, "inactive", 8) != 0) | |
11041 | return 0; | |
11042 | /* no data to recover */ | |
11043 | if (__le32_to_cpu(migr_rec->rec_status) == UNIT_SRC_NORMAL) | |
11044 | return 0; | |
11045 | if (curr_migr_unit >= num_migr_units) | |
11046 | return 1; | |
11047 | ||
11048 | /* find device during reshape */ | |
11049 | for (id = super->devlist; id; id = id->next) | |
11050 | if (is_gen_migration(id->dev)) | |
11051 | break; | |
11052 | if (id == NULL) | |
11053 | return 1; | |
11054 | ||
238c0a71 | 11055 | map_dest = get_imsm_map(id->dev, MAP_0); |
276d77db AK |
11056 | new_disks = map_dest->num_members; |
11057 | ||
9f421827 | 11058 | read_offset = migr_chkp_area_pba(migr_rec) * 512; |
276d77db | 11059 | |
9f421827 | 11060 | write_offset = (migr_dest_1st_member_lba(migr_rec) + |
5551b113 | 11061 | pba_of_lba0(map_dest)) * 512; |
276d77db AK |
11062 | |
11063 | unit_len = __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512; | |
f36a9ecd | 11064 | if (posix_memalign((void **)&buf, sector_size, unit_len) != 0) |
276d77db | 11065 | goto abort; |
276d77db | 11066 | |
2f86fda3 MT |
11067 | for (dl_disk = super->disks; dl_disk; dl_disk = dl_disk->next) { |
11068 | if (dl_disk->index < 0) | |
11069 | continue; | |
276d77db | 11070 | |
2f86fda3 | 11071 | if (dl_disk->fd < 0) { |
6c3560c0 AK |
11072 | skipped_disks++; |
11073 | continue; | |
11074 | } | |
2f86fda3 | 11075 | if (lseek64(dl_disk->fd, read_offset, SEEK_SET) < 0) { |
e7b84f9d N |
11076 | pr_err("Cannot seek to block: %s\n", |
11077 | strerror(errno)); | |
137debce AK |
11078 | skipped_disks++; |
11079 | continue; | |
276d77db | 11080 | } |
83b3de77 | 11081 | if (read(dl_disk->fd, buf, unit_len) != (ssize_t)unit_len) { |
e7b84f9d N |
11082 | pr_err("Cannot read copy area block: %s\n", |
11083 | strerror(errno)); | |
137debce AK |
11084 | skipped_disks++; |
11085 | continue; | |
276d77db | 11086 | } |
2f86fda3 | 11087 | if (lseek64(dl_disk->fd, write_offset, SEEK_SET) < 0) { |
e7b84f9d N |
11088 | pr_err("Cannot seek to block: %s\n", |
11089 | strerror(errno)); | |
137debce AK |
11090 | skipped_disks++; |
11091 | continue; | |
276d77db | 11092 | } |
83b3de77 | 11093 | if (write(dl_disk->fd, buf, unit_len) != (ssize_t)unit_len) { |
e7b84f9d N |
11094 | pr_err("Cannot restore block: %s\n", |
11095 | strerror(errno)); | |
137debce AK |
11096 | skipped_disks++; |
11097 | continue; | |
276d77db AK |
11098 | } |
11099 | } | |
11100 | ||
137debce AK |
11101 | if (skipped_disks > imsm_get_allowed_degradation(info->new_level, |
11102 | new_disks, | |
11103 | super, | |
11104 | id->dev)) { | |
7a862a02 | 11105 | pr_err("Cannot restore data from backup. Too many failed disks\n"); |
6c3560c0 AK |
11106 | goto abort; |
11107 | } | |
11108 | ||
befb629b AK |
11109 | if (save_checkpoint_imsm(st, info, UNIT_SRC_NORMAL)) { |
11110 | /* ignore error == 2, this can mean end of reshape here | |
11111 | */ | |
7a862a02 | 11112 | dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL) during restart\n"); |
befb629b | 11113 | } else |
276d77db | 11114 | retval = 0; |
276d77db AK |
11115 | |
11116 | abort: | |
276d77db AK |
11117 | free(buf); |
11118 | return retval; | |
11119 | } | |
11120 | ||
2cda7640 ML |
11121 | static char disk_by_path[] = "/dev/disk/by-path/"; |
11122 | ||
11123 | static const char *imsm_get_disk_controller_domain(const char *path) | |
11124 | { | |
2cda7640 | 11125 | char disk_path[PATH_MAX]; |
96234762 LM |
11126 | char *drv=NULL; |
11127 | struct stat st; | |
2cda7640 | 11128 | |
6d8d290a | 11129 | strcpy(disk_path, disk_by_path); |
96234762 LM |
11130 | strncat(disk_path, path, PATH_MAX - strlen(disk_path) - 1); |
11131 | if (stat(disk_path, &st) == 0) { | |
11132 | struct sys_dev* hba; | |
594dc1b8 | 11133 | char *path; |
96234762 | 11134 | |
7c798f87 | 11135 | path = devt_to_devpath(st.st_rdev, 1, NULL); |
96234762 LM |
11136 | if (path == NULL) |
11137 | return "unknown"; | |
11138 | hba = find_disk_attached_hba(-1, path); | |
11139 | if (hba && hba->type == SYS_DEV_SAS) | |
11140 | drv = "isci"; | |
11141 | else if (hba && hba->type == SYS_DEV_SATA) | |
11142 | drv = "ahci"; | |
c6839718 MT |
11143 | else if (hba && hba->type == SYS_DEV_VMD) |
11144 | drv = "vmd"; | |
11145 | else if (hba && hba->type == SYS_DEV_NVME) | |
11146 | drv = "nvme"; | |
1011e834 | 11147 | else |
96234762 LM |
11148 | drv = "unknown"; |
11149 | dprintf("path: %s hba: %s attached: %s\n", | |
11150 | path, (hba) ? hba->path : "NULL", drv); | |
11151 | free(path); | |
2cda7640 | 11152 | } |
96234762 | 11153 | return drv; |
2cda7640 ML |
11154 | } |
11155 | ||
4dd2df09 | 11156 | static char *imsm_find_array_devnm_by_subdev(int subdev, char *container) |
78b10e66 | 11157 | { |
4dd2df09 | 11158 | static char devnm[32]; |
78b10e66 N |
11159 | char subdev_name[20]; |
11160 | struct mdstat_ent *mdstat; | |
11161 | ||
11162 | sprintf(subdev_name, "%d", subdev); | |
11163 | mdstat = mdstat_by_subdev(subdev_name, container); | |
11164 | if (!mdstat) | |
4dd2df09 | 11165 | return NULL; |
78b10e66 | 11166 | |
4dd2df09 | 11167 | strcpy(devnm, mdstat->devnm); |
78b10e66 | 11168 | free_mdstat(mdstat); |
4dd2df09 | 11169 | return devnm; |
78b10e66 N |
11170 | } |
11171 | ||
11172 | static int imsm_reshape_is_allowed_on_container(struct supertype *st, | |
11173 | struct geo_params *geo, | |
fbf3d202 AK |
11174 | int *old_raid_disks, |
11175 | int direction) | |
78b10e66 | 11176 | { |
694575e7 KW |
11177 | /* currently we only support increasing the number of devices |
11178 | * for a container. This increases the number of device for each | |
11179 | * member array. They must all be RAID0 or RAID5. | |
11180 | */ | |
78b10e66 N |
11181 | int ret_val = 0; |
11182 | struct mdinfo *info, *member; | |
11183 | int devices_that_can_grow = 0; | |
11184 | ||
7a862a02 | 11185 | dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): st->devnm = (%s)\n", st->devnm); |
78b10e66 | 11186 | |
d04f65f4 | 11187 | if (geo->size > 0 || |
78b10e66 N |
11188 | geo->level != UnSet || |
11189 | geo->layout != UnSet || | |
11190 | geo->chunksize != 0 || | |
11191 | geo->raid_disks == UnSet) { | |
7a862a02 | 11192 | dprintf("imsm: Container operation is allowed for raid disks number change only.\n"); |
78b10e66 N |
11193 | return ret_val; |
11194 | } | |
11195 | ||
fbf3d202 | 11196 | if (direction == ROLLBACK_METADATA_CHANGES) { |
7a862a02 | 11197 | dprintf("imsm: Metadata changes rollback is not supported for container operation.\n"); |
fbf3d202 AK |
11198 | return ret_val; |
11199 | } | |
11200 | ||
78b10e66 N |
11201 | info = container_content_imsm(st, NULL); |
11202 | for (member = info; member; member = member->next) { | |
4dd2df09 | 11203 | char *result; |
78b10e66 N |
11204 | |
11205 | dprintf("imsm: checking device_num: %i\n", | |
11206 | member->container_member); | |
11207 | ||
d7d205bd | 11208 | if (geo->raid_disks <= member->array.raid_disks) { |
78b10e66 N |
11209 | /* we work on container for Online Capacity Expansion |
11210 | * only so raid_disks has to grow | |
11211 | */ | |
7a862a02 | 11212 | dprintf("imsm: for container operation raid disks increase is required\n"); |
78b10e66 N |
11213 | break; |
11214 | } | |
11215 | ||
089f9d79 | 11216 | if (info->array.level != 0 && info->array.level != 5) { |
78b10e66 N |
11217 | /* we cannot use this container with other raid level |
11218 | */ | |
7a862a02 | 11219 | dprintf("imsm: for container operation wrong raid level (%i) detected\n", |
78b10e66 N |
11220 | info->array.level); |
11221 | break; | |
11222 | } else { | |
11223 | /* check for platform support | |
11224 | * for this raid level configuration | |
11225 | */ | |
11226 | struct intel_super *super = st->sb; | |
11227 | if (!is_raid_level_supported(super->orom, | |
11228 | member->array.level, | |
11229 | geo->raid_disks)) { | |
7a862a02 | 11230 | dprintf("platform does not support raid%d with %d disk%s\n", |
78b10e66 N |
11231 | info->array.level, |
11232 | geo->raid_disks, | |
11233 | geo->raid_disks > 1 ? "s" : ""); | |
11234 | break; | |
11235 | } | |
2a4a08e7 AK |
11236 | /* check if component size is aligned to chunk size |
11237 | */ | |
11238 | if (info->component_size % | |
11239 | (info->array.chunk_size/512)) { | |
7a862a02 | 11240 | dprintf("Component size is not aligned to chunk size\n"); |
2a4a08e7 AK |
11241 | break; |
11242 | } | |
78b10e66 N |
11243 | } |
11244 | ||
11245 | if (*old_raid_disks && | |
11246 | info->array.raid_disks != *old_raid_disks) | |
11247 | break; | |
11248 | *old_raid_disks = info->array.raid_disks; | |
11249 | ||
11250 | /* All raid5 and raid0 volumes in container | |
11251 | * have to be ready for Online Capacity Expansion | |
11252 | * so they need to be assembled. We have already | |
11253 | * checked that no recovery etc is happening. | |
11254 | */ | |
4dd2df09 N |
11255 | result = imsm_find_array_devnm_by_subdev(member->container_member, |
11256 | st->container_devnm); | |
11257 | if (result == NULL) { | |
78b10e66 N |
11258 | dprintf("imsm: cannot find array\n"); |
11259 | break; | |
11260 | } | |
11261 | devices_that_can_grow++; | |
11262 | } | |
11263 | sysfs_free(info); | |
11264 | if (!member && devices_that_can_grow) | |
11265 | ret_val = 1; | |
11266 | ||
11267 | if (ret_val) | |
1ade5cc1 | 11268 | dprintf("Container operation allowed\n"); |
78b10e66 | 11269 | else |
1ade5cc1 | 11270 | dprintf("Error: %i\n", ret_val); |
78b10e66 N |
11271 | |
11272 | return ret_val; | |
11273 | } | |
11274 | ||
11275 | /* Function: get_spares_for_grow | |
11276 | * Description: Allocates memory and creates list of spare devices | |
1011e834 | 11277 | * avaliable in container. Checks if spare drive size is acceptable. |
78b10e66 N |
11278 | * Parameters: Pointer to the supertype structure |
11279 | * Returns: Pointer to the list of spare devices (mdinfo structure) on success, | |
1011e834 | 11280 | * NULL if fail |
78b10e66 N |
11281 | */ |
11282 | static struct mdinfo *get_spares_for_grow(struct supertype *st) | |
11283 | { | |
fbfdcb06 AO |
11284 | struct spare_criteria sc; |
11285 | ||
11286 | get_spare_criteria_imsm(st, &sc); | |
11287 | return container_choose_spares(st, &sc, NULL, NULL, NULL, 0); | |
78b10e66 N |
11288 | } |
11289 | ||
11290 | /****************************************************************************** | |
11291 | * function: imsm_create_metadata_update_for_reshape | |
11292 | * Function creates update for whole IMSM container. | |
11293 | * | |
11294 | ******************************************************************************/ | |
11295 | static int imsm_create_metadata_update_for_reshape( | |
11296 | struct supertype *st, | |
11297 | struct geo_params *geo, | |
11298 | int old_raid_disks, | |
11299 | struct imsm_update_reshape **updatep) | |
11300 | { | |
11301 | struct intel_super *super = st->sb; | |
11302 | struct imsm_super *mpb = super->anchor; | |
594dc1b8 JS |
11303 | int update_memory_size; |
11304 | struct imsm_update_reshape *u; | |
11305 | struct mdinfo *spares; | |
78b10e66 | 11306 | int i; |
594dc1b8 | 11307 | int delta_disks; |
bbd24d86 | 11308 | struct mdinfo *dev; |
78b10e66 | 11309 | |
1ade5cc1 | 11310 | dprintf("(enter) raid_disks = %i\n", geo->raid_disks); |
78b10e66 N |
11311 | |
11312 | delta_disks = geo->raid_disks - old_raid_disks; | |
11313 | ||
11314 | /* size of all update data without anchor */ | |
11315 | update_memory_size = sizeof(struct imsm_update_reshape); | |
11316 | ||
11317 | /* now add space for spare disks that we need to add. */ | |
11318 | update_memory_size += sizeof(u->new_disks[0]) * (delta_disks - 1); | |
11319 | ||
503975b9 | 11320 | u = xcalloc(1, update_memory_size); |
78b10e66 N |
11321 | u->type = update_reshape_container_disks; |
11322 | u->old_raid_disks = old_raid_disks; | |
11323 | u->new_raid_disks = geo->raid_disks; | |
11324 | ||
11325 | /* now get spare disks list | |
11326 | */ | |
11327 | spares = get_spares_for_grow(st); | |
11328 | ||
d7be7d87 | 11329 | if (spares == NULL || delta_disks > spares->array.spare_disks) { |
7a862a02 | 11330 | pr_err("imsm: ERROR: Cannot get spare devices for %s.\n", geo->dev_name); |
e4c72d1d | 11331 | i = -1; |
78b10e66 N |
11332 | goto abort; |
11333 | } | |
11334 | ||
11335 | /* we have got spares | |
11336 | * update disk list in imsm_disk list table in anchor | |
11337 | */ | |
11338 | dprintf("imsm: %i spares are available.\n\n", | |
11339 | spares->array.spare_disks); | |
11340 | ||
bbd24d86 | 11341 | dev = spares->devs; |
78b10e66 | 11342 | for (i = 0; i < delta_disks; i++) { |
78b10e66 N |
11343 | struct dl *dl; |
11344 | ||
bbd24d86 AK |
11345 | if (dev == NULL) |
11346 | break; | |
78b10e66 N |
11347 | u->new_disks[i] = makedev(dev->disk.major, |
11348 | dev->disk.minor); | |
11349 | dl = get_disk_super(super, dev->disk.major, dev->disk.minor); | |
ee4beede AK |
11350 | dl->index = mpb->num_disks; |
11351 | mpb->num_disks++; | |
bbd24d86 | 11352 | dev = dev->next; |
78b10e66 | 11353 | } |
78b10e66 N |
11354 | |
11355 | abort: | |
11356 | /* free spares | |
11357 | */ | |
11358 | sysfs_free(spares); | |
11359 | ||
d677e0b8 | 11360 | dprintf("imsm: reshape update preparation :"); |
78b10e66 | 11361 | if (i == delta_disks) { |
1ade5cc1 | 11362 | dprintf_cont(" OK\n"); |
78b10e66 N |
11363 | *updatep = u; |
11364 | return update_memory_size; | |
11365 | } | |
11366 | free(u); | |
1ade5cc1 | 11367 | dprintf_cont(" Error\n"); |
78b10e66 N |
11368 | |
11369 | return 0; | |
11370 | } | |
11371 | ||
f3871fdc AK |
11372 | /****************************************************************************** |
11373 | * function: imsm_create_metadata_update_for_size_change() | |
11374 | * Creates update for IMSM array for array size change. | |
11375 | * | |
11376 | ******************************************************************************/ | |
11377 | static int imsm_create_metadata_update_for_size_change( | |
11378 | struct supertype *st, | |
11379 | struct geo_params *geo, | |
11380 | struct imsm_update_size_change **updatep) | |
11381 | { | |
11382 | struct intel_super *super = st->sb; | |
594dc1b8 JS |
11383 | int update_memory_size; |
11384 | struct imsm_update_size_change *u; | |
f3871fdc | 11385 | |
1ade5cc1 | 11386 | dprintf("(enter) New size = %llu\n", geo->size); |
f3871fdc AK |
11387 | |
11388 | /* size of all update data without anchor */ | |
11389 | update_memory_size = sizeof(struct imsm_update_size_change); | |
11390 | ||
503975b9 | 11391 | u = xcalloc(1, update_memory_size); |
f3871fdc AK |
11392 | u->type = update_size_change; |
11393 | u->subdev = super->current_vol; | |
11394 | u->new_size = geo->size; | |
11395 | ||
11396 | dprintf("imsm: reshape update preparation : OK\n"); | |
11397 | *updatep = u; | |
11398 | ||
11399 | return update_memory_size; | |
11400 | } | |
11401 | ||
48c5303a PC |
11402 | /****************************************************************************** |
11403 | * function: imsm_create_metadata_update_for_migration() | |
11404 | * Creates update for IMSM array. | |
11405 | * | |
11406 | ******************************************************************************/ | |
11407 | static int imsm_create_metadata_update_for_migration( | |
11408 | struct supertype *st, | |
11409 | struct geo_params *geo, | |
11410 | struct imsm_update_reshape_migration **updatep) | |
11411 | { | |
11412 | struct intel_super *super = st->sb; | |
594dc1b8 JS |
11413 | int update_memory_size; |
11414 | struct imsm_update_reshape_migration *u; | |
48c5303a PC |
11415 | struct imsm_dev *dev; |
11416 | int previous_level = -1; | |
11417 | ||
1ade5cc1 | 11418 | dprintf("(enter) New Level = %i\n", geo->level); |
48c5303a PC |
11419 | |
11420 | /* size of all update data without anchor */ | |
11421 | update_memory_size = sizeof(struct imsm_update_reshape_migration); | |
11422 | ||
503975b9 | 11423 | u = xcalloc(1, update_memory_size); |
48c5303a PC |
11424 | u->type = update_reshape_migration; |
11425 | u->subdev = super->current_vol; | |
11426 | u->new_level = geo->level; | |
11427 | u->new_layout = geo->layout; | |
11428 | u->new_raid_disks = u->old_raid_disks = geo->raid_disks; | |
11429 | u->new_disks[0] = -1; | |
4bba0439 | 11430 | u->new_chunksize = -1; |
48c5303a PC |
11431 | |
11432 | dev = get_imsm_dev(super, u->subdev); | |
11433 | if (dev) { | |
11434 | struct imsm_map *map; | |
11435 | ||
238c0a71 | 11436 | map = get_imsm_map(dev, MAP_0); |
4bba0439 PC |
11437 | if (map) { |
11438 | int current_chunk_size = | |
11439 | __le16_to_cpu(map->blocks_per_strip) / 2; | |
11440 | ||
11441 | if (geo->chunksize != current_chunk_size) { | |
11442 | u->new_chunksize = geo->chunksize / 1024; | |
7a862a02 | 11443 | dprintf("imsm: chunk size change from %i to %i\n", |
4bba0439 PC |
11444 | current_chunk_size, u->new_chunksize); |
11445 | } | |
48c5303a | 11446 | previous_level = map->raid_level; |
4bba0439 | 11447 | } |
48c5303a | 11448 | } |
089f9d79 | 11449 | if (geo->level == 5 && previous_level == 0) { |
48c5303a PC |
11450 | struct mdinfo *spares = NULL; |
11451 | ||
11452 | u->new_raid_disks++; | |
11453 | spares = get_spares_for_grow(st); | |
089f9d79 | 11454 | if (spares == NULL || spares->array.spare_disks < 1) { |
48c5303a PC |
11455 | free(u); |
11456 | sysfs_free(spares); | |
11457 | update_memory_size = 0; | |
565cc99e | 11458 | pr_err("cannot get spare device for requested migration\n"); |
48c5303a PC |
11459 | return 0; |
11460 | } | |
11461 | sysfs_free(spares); | |
11462 | } | |
11463 | dprintf("imsm: reshape update preparation : OK\n"); | |
11464 | *updatep = u; | |
11465 | ||
11466 | return update_memory_size; | |
11467 | } | |
11468 | ||
8dd70bce AK |
11469 | static void imsm_update_metadata_locally(struct supertype *st, |
11470 | void *buf, int len) | |
11471 | { | |
11472 | struct metadata_update mu; | |
11473 | ||
11474 | mu.buf = buf; | |
11475 | mu.len = len; | |
11476 | mu.space = NULL; | |
11477 | mu.space_list = NULL; | |
11478 | mu.next = NULL; | |
5fe6f031 N |
11479 | if (imsm_prepare_update(st, &mu)) |
11480 | imsm_process_update(st, &mu); | |
8dd70bce AK |
11481 | |
11482 | while (mu.space_list) { | |
11483 | void **space = mu.space_list; | |
11484 | mu.space_list = *space; | |
11485 | free(space); | |
11486 | } | |
11487 | } | |
78b10e66 | 11488 | |
471bceb6 | 11489 | /*************************************************************************** |
694575e7 | 11490 | * Function: imsm_analyze_change |
471bceb6 | 11491 | * Description: Function analyze change for single volume |
1011e834 | 11492 | * and validate if transition is supported |
fbf3d202 AK |
11493 | * Parameters: Geometry parameters, supertype structure, |
11494 | * metadata change direction (apply/rollback) | |
694575e7 | 11495 | * Returns: Operation type code on success, -1 if fail |
471bceb6 KW |
11496 | ****************************************************************************/ |
11497 | enum imsm_reshape_type imsm_analyze_change(struct supertype *st, | |
fbf3d202 AK |
11498 | struct geo_params *geo, |
11499 | int direction) | |
694575e7 | 11500 | { |
471bceb6 KW |
11501 | struct mdinfo info; |
11502 | int change = -1; | |
11503 | int check_devs = 0; | |
c21e737b | 11504 | int chunk; |
67a2db32 AK |
11505 | /* number of added/removed disks in operation result */ |
11506 | int devNumChange = 0; | |
11507 | /* imsm compatible layout value for array geometry verification */ | |
11508 | int imsm_layout = -1; | |
7abc9871 AK |
11509 | int data_disks; |
11510 | struct imsm_dev *dev; | |
9529d343 | 11511 | struct imsm_map *map; |
7abc9871 | 11512 | struct intel_super *super; |
d04f65f4 | 11513 | unsigned long long current_size; |
65d38cca | 11514 | unsigned long long free_size; |
d04f65f4 | 11515 | unsigned long long max_size; |
65d38cca | 11516 | int rv; |
471bceb6 KW |
11517 | |
11518 | getinfo_super_imsm_volume(st, &info, NULL); | |
089f9d79 JS |
11519 | if (geo->level != info.array.level && geo->level >= 0 && |
11520 | geo->level != UnSet) { | |
471bceb6 KW |
11521 | switch (info.array.level) { |
11522 | case 0: | |
11523 | if (geo->level == 5) { | |
b5347799 | 11524 | change = CH_MIGRATION; |
e13ce846 | 11525 | if (geo->layout != ALGORITHM_LEFT_ASYMMETRIC) { |
7a862a02 | 11526 | pr_err("Error. Requested Layout not supported (left-asymmetric layout is supported only)!\n"); |
e13ce846 AK |
11527 | change = -1; |
11528 | goto analyse_change_exit; | |
11529 | } | |
67a2db32 | 11530 | imsm_layout = geo->layout; |
471bceb6 | 11531 | check_devs = 1; |
e91a3bad LM |
11532 | devNumChange = 1; /* parity disk added */ |
11533 | } else if (geo->level == 10) { | |
471bceb6 KW |
11534 | change = CH_TAKEOVER; |
11535 | check_devs = 1; | |
e91a3bad | 11536 | devNumChange = 2; /* two mirrors added */ |
67a2db32 | 11537 | imsm_layout = 0x102; /* imsm supported layout */ |
471bceb6 | 11538 | } |
dfe77a9e KW |
11539 | break; |
11540 | case 1: | |
471bceb6 KW |
11541 | case 10: |
11542 | if (geo->level == 0) { | |
11543 | change = CH_TAKEOVER; | |
11544 | check_devs = 1; | |
e91a3bad | 11545 | devNumChange = -(geo->raid_disks/2); |
67a2db32 | 11546 | imsm_layout = 0; /* imsm raid0 layout */ |
471bceb6 KW |
11547 | } |
11548 | break; | |
11549 | } | |
11550 | if (change == -1) { | |
7a862a02 | 11551 | pr_err("Error. Level Migration from %d to %d not supported!\n", |
e7b84f9d | 11552 | info.array.level, geo->level); |
471bceb6 KW |
11553 | goto analyse_change_exit; |
11554 | } | |
11555 | } else | |
11556 | geo->level = info.array.level; | |
11557 | ||
089f9d79 JS |
11558 | if (geo->layout != info.array.layout && |
11559 | (geo->layout != UnSet && geo->layout != -1)) { | |
b5347799 | 11560 | change = CH_MIGRATION; |
089f9d79 JS |
11561 | if (info.array.layout == 0 && info.array.level == 5 && |
11562 | geo->layout == 5) { | |
471bceb6 | 11563 | /* reshape 5 -> 4 */ |
089f9d79 JS |
11564 | } else if (info.array.layout == 5 && info.array.level == 5 && |
11565 | geo->layout == 0) { | |
471bceb6 KW |
11566 | /* reshape 4 -> 5 */ |
11567 | geo->layout = 0; | |
11568 | geo->level = 5; | |
11569 | } else { | |
7a862a02 | 11570 | pr_err("Error. Layout Migration from %d to %d not supported!\n", |
e7b84f9d | 11571 | info.array.layout, geo->layout); |
471bceb6 KW |
11572 | change = -1; |
11573 | goto analyse_change_exit; | |
11574 | } | |
67a2db32 | 11575 | } else { |
471bceb6 | 11576 | geo->layout = info.array.layout; |
67a2db32 AK |
11577 | if (imsm_layout == -1) |
11578 | imsm_layout = info.array.layout; | |
11579 | } | |
471bceb6 | 11580 | |
089f9d79 JS |
11581 | if (geo->chunksize > 0 && geo->chunksize != UnSet && |
11582 | geo->chunksize != info.array.chunk_size) { | |
2d2b0eb7 MD |
11583 | if (info.array.level == 10) { |
11584 | pr_err("Error. Chunk size change for RAID 10 is not supported.\n"); | |
11585 | change = -1; | |
11586 | goto analyse_change_exit; | |
1e9b2c3f PB |
11587 | } else if (info.component_size % (geo->chunksize/512)) { |
11588 | pr_err("New chunk size (%dK) does not evenly divide device size (%lluk). Aborting...\n", | |
11589 | geo->chunksize/1024, info.component_size/2); | |
11590 | change = -1; | |
11591 | goto analyse_change_exit; | |
2d2b0eb7 | 11592 | } |
b5347799 | 11593 | change = CH_MIGRATION; |
2d2b0eb7 | 11594 | } else { |
471bceb6 | 11595 | geo->chunksize = info.array.chunk_size; |
2d2b0eb7 | 11596 | } |
471bceb6 | 11597 | |
c21e737b | 11598 | chunk = geo->chunksize / 1024; |
7abc9871 AK |
11599 | |
11600 | super = st->sb; | |
11601 | dev = get_imsm_dev(super, super->current_vol); | |
9529d343 MD |
11602 | map = get_imsm_map(dev, MAP_0); |
11603 | data_disks = imsm_num_data_members(map); | |
c41e00b2 | 11604 | /* compute current size per disk member |
7abc9871 | 11605 | */ |
c41e00b2 AK |
11606 | current_size = info.custom_array_size / data_disks; |
11607 | ||
089f9d79 | 11608 | if (geo->size > 0 && geo->size != MAX_SIZE) { |
c41e00b2 AK |
11609 | /* align component size |
11610 | */ | |
3e684231 | 11611 | geo->size = imsm_component_size_alignment_check( |
c41e00b2 | 11612 | get_imsm_raid_level(dev->vol.map), |
f36a9ecd | 11613 | chunk * 1024, super->sector_size, |
c41e00b2 | 11614 | geo->size * 2); |
65d0b4ce | 11615 | if (geo->size == 0) { |
7a862a02 | 11616 | pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is 0).\n", |
65d0b4ce LD |
11617 | current_size); |
11618 | goto analyse_change_exit; | |
11619 | } | |
c41e00b2 | 11620 | } |
7abc9871 | 11621 | |
089f9d79 | 11622 | if (current_size != geo->size && geo->size > 0) { |
7abc9871 | 11623 | if (change != -1) { |
7a862a02 | 11624 | pr_err("Error. Size change should be the only one at a time.\n"); |
7abc9871 AK |
11625 | change = -1; |
11626 | goto analyse_change_exit; | |
11627 | } | |
11628 | if ((super->current_vol + 1) != super->anchor->num_raid_devs) { | |
7a862a02 | 11629 | pr_err("Error. The last volume in container can be expanded only (%i/%s).\n", |
4dd2df09 | 11630 | super->current_vol, st->devnm); |
7abc9871 AK |
11631 | goto analyse_change_exit; |
11632 | } | |
65d38cca LD |
11633 | /* check the maximum available size |
11634 | */ | |
11635 | rv = imsm_get_free_size(st, dev->vol.map->num_members, | |
11636 | 0, chunk, &free_size); | |
11637 | if (rv == 0) | |
11638 | /* Cannot find maximum available space | |
11639 | */ | |
11640 | max_size = 0; | |
11641 | else { | |
11642 | max_size = free_size + current_size; | |
11643 | /* align component size | |
11644 | */ | |
3e684231 | 11645 | max_size = imsm_component_size_alignment_check( |
65d38cca | 11646 | get_imsm_raid_level(dev->vol.map), |
f36a9ecd | 11647 | chunk * 1024, super->sector_size, |
65d38cca LD |
11648 | max_size); |
11649 | } | |
d04f65f4 | 11650 | if (geo->size == MAX_SIZE) { |
b130333f AK |
11651 | /* requested size change to the maximum available size |
11652 | */ | |
65d38cca | 11653 | if (max_size == 0) { |
7a862a02 | 11654 | pr_err("Error. Cannot find maximum available space.\n"); |
b130333f AK |
11655 | change = -1; |
11656 | goto analyse_change_exit; | |
65d38cca LD |
11657 | } else |
11658 | geo->size = max_size; | |
c41e00b2 | 11659 | } |
b130333f | 11660 | |
681b7ae2 | 11661 | if (direction == ROLLBACK_METADATA_CHANGES) { |
fbf3d202 AK |
11662 | /* accept size for rollback only |
11663 | */ | |
11664 | } else { | |
11665 | /* round size due to metadata compatibility | |
11666 | */ | |
11667 | geo->size = (geo->size >> SECT_PER_MB_SHIFT) | |
11668 | << SECT_PER_MB_SHIFT; | |
11669 | dprintf("Prepare update for size change to %llu\n", | |
11670 | geo->size ); | |
11671 | if (current_size >= geo->size) { | |
7a862a02 | 11672 | pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is %llu).\n", |
e7b84f9d | 11673 | current_size, geo->size); |
fbf3d202 AK |
11674 | goto analyse_change_exit; |
11675 | } | |
65d38cca | 11676 | if (max_size && geo->size > max_size) { |
7a862a02 | 11677 | pr_err("Error. Requested size is larger than maximum available size (maximum available size is %llu, requested size /rounded/ is %llu).\n", |
e7b84f9d | 11678 | max_size, geo->size); |
65d38cca LD |
11679 | goto analyse_change_exit; |
11680 | } | |
7abc9871 AK |
11681 | } |
11682 | geo->size *= data_disks; | |
11683 | geo->raid_disks = dev->vol.map->num_members; | |
11684 | change = CH_ARRAY_SIZE; | |
11685 | } | |
471bceb6 KW |
11686 | if (!validate_geometry_imsm(st, |
11687 | geo->level, | |
67a2db32 | 11688 | imsm_layout, |
e91a3bad | 11689 | geo->raid_disks + devNumChange, |
c21e737b | 11690 | &chunk, |
af4348dd | 11691 | geo->size, INVALID_SECTORS, |
5308f117 | 11692 | 0, 0, info.consistency_policy, 1)) |
471bceb6 KW |
11693 | change = -1; |
11694 | ||
11695 | if (check_devs) { | |
11696 | struct intel_super *super = st->sb; | |
11697 | struct imsm_super *mpb = super->anchor; | |
11698 | ||
11699 | if (mpb->num_raid_devs > 1) { | |
7a862a02 | 11700 | pr_err("Error. Cannot perform operation on %s- for this operation it MUST be single array in container\n", |
e7b84f9d | 11701 | geo->dev_name); |
471bceb6 KW |
11702 | change = -1; |
11703 | } | |
11704 | } | |
11705 | ||
11706 | analyse_change_exit: | |
089f9d79 JS |
11707 | if (direction == ROLLBACK_METADATA_CHANGES && |
11708 | (change == CH_MIGRATION || change == CH_TAKEOVER)) { | |
7a862a02 | 11709 | dprintf("imsm: Metadata changes rollback is not supported for migration and takeover operations.\n"); |
fbf3d202 AK |
11710 | change = -1; |
11711 | } | |
471bceb6 | 11712 | return change; |
694575e7 KW |
11713 | } |
11714 | ||
bb025c2f KW |
11715 | int imsm_takeover(struct supertype *st, struct geo_params *geo) |
11716 | { | |
11717 | struct intel_super *super = st->sb; | |
11718 | struct imsm_update_takeover *u; | |
11719 | ||
503975b9 | 11720 | u = xmalloc(sizeof(struct imsm_update_takeover)); |
bb025c2f KW |
11721 | |
11722 | u->type = update_takeover; | |
11723 | u->subarray = super->current_vol; | |
11724 | ||
11725 | /* 10->0 transition */ | |
11726 | if (geo->level == 0) | |
11727 | u->direction = R10_TO_R0; | |
11728 | ||
0529c688 KW |
11729 | /* 0->10 transition */ |
11730 | if (geo->level == 10) | |
11731 | u->direction = R0_TO_R10; | |
11732 | ||
bb025c2f KW |
11733 | /* update metadata locally */ |
11734 | imsm_update_metadata_locally(st, u, | |
11735 | sizeof(struct imsm_update_takeover)); | |
11736 | /* and possibly remotely */ | |
11737 | if (st->update_tail) | |
11738 | append_metadata_update(st, u, | |
11739 | sizeof(struct imsm_update_takeover)); | |
11740 | else | |
11741 | free(u); | |
11742 | ||
11743 | return 0; | |
11744 | } | |
11745 | ||
895ffd99 MT |
11746 | /* Flush size update if size calculated by num_data_stripes is higher than |
11747 | * imsm_dev_size to eliminate differences during reshape. | |
11748 | * Mdmon will recalculate them correctly. | |
11749 | * If subarray index is not set then check whole container. | |
11750 | * Returns: | |
11751 | * 0 - no error occurred | |
11752 | * 1 - error detected | |
11753 | */ | |
11754 | static int imsm_fix_size_mismatch(struct supertype *st, int subarray_index) | |
11755 | { | |
11756 | struct intel_super *super = st->sb; | |
11757 | int tmp = super->current_vol; | |
11758 | int ret_val = 1; | |
11759 | int i; | |
11760 | ||
11761 | for (i = 0; i < super->anchor->num_raid_devs; i++) { | |
11762 | if (subarray_index >= 0 && i != subarray_index) | |
11763 | continue; | |
11764 | super->current_vol = i; | |
11765 | struct imsm_dev *dev = get_imsm_dev(super, super->current_vol); | |
11766 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
11767 | unsigned int disc_count = imsm_num_data_members(map); | |
11768 | struct geo_params geo; | |
11769 | struct imsm_update_size_change *update; | |
11770 | unsigned long long calc_size = per_dev_array_size(map) * disc_count; | |
11771 | unsigned long long d_size = imsm_dev_size(dev); | |
11772 | int u_size; | |
11773 | ||
11774 | if (calc_size == d_size || dev->vol.migr_type == MIGR_GEN_MIGR) | |
11775 | continue; | |
11776 | ||
ff904202 MT |
11777 | /* There is a difference, confirm that imsm_dev_size is |
11778 | * smaller and push update. | |
895ffd99 | 11779 | */ |
ff904202 MT |
11780 | if (d_size > calc_size) { |
11781 | pr_err("imsm: dev size of subarray %d is incorrect\n", | |
11782 | i); | |
895ffd99 MT |
11783 | goto exit; |
11784 | } | |
11785 | memset(&geo, 0, sizeof(struct geo_params)); | |
11786 | geo.size = d_size; | |
11787 | u_size = imsm_create_metadata_update_for_size_change(st, &geo, | |
11788 | &update); | |
11789 | if (u_size < 1) { | |
11790 | dprintf("imsm: Cannot prepare size change update\n"); | |
11791 | goto exit; | |
11792 | } | |
11793 | imsm_update_metadata_locally(st, update, u_size); | |
11794 | if (st->update_tail) { | |
11795 | append_metadata_update(st, update, u_size); | |
11796 | flush_metadata_updates(st); | |
11797 | st->update_tail = &st->updates; | |
11798 | } else { | |
11799 | imsm_sync_metadata(st); | |
11800 | } | |
11801 | } | |
11802 | ret_val = 0; | |
11803 | exit: | |
11804 | super->current_vol = tmp; | |
11805 | return ret_val; | |
11806 | } | |
11807 | ||
d04f65f4 N |
11808 | static int imsm_reshape_super(struct supertype *st, unsigned long long size, |
11809 | int level, | |
78b10e66 | 11810 | int layout, int chunksize, int raid_disks, |
41784c88 | 11811 | int delta_disks, char *backup, char *dev, |
016e00f5 | 11812 | int direction, int verbose) |
78b10e66 | 11813 | { |
78b10e66 N |
11814 | int ret_val = 1; |
11815 | struct geo_params geo; | |
11816 | ||
1ade5cc1 | 11817 | dprintf("(enter)\n"); |
78b10e66 | 11818 | |
71204a50 | 11819 | memset(&geo, 0, sizeof(struct geo_params)); |
78b10e66 N |
11820 | |
11821 | geo.dev_name = dev; | |
4dd2df09 | 11822 | strcpy(geo.devnm, st->devnm); |
78b10e66 N |
11823 | geo.size = size; |
11824 | geo.level = level; | |
11825 | geo.layout = layout; | |
11826 | geo.chunksize = chunksize; | |
11827 | geo.raid_disks = raid_disks; | |
41784c88 AK |
11828 | if (delta_disks != UnSet) |
11829 | geo.raid_disks += delta_disks; | |
78b10e66 | 11830 | |
1ade5cc1 N |
11831 | dprintf("for level : %i\n", geo.level); |
11832 | dprintf("for raid_disks : %i\n", geo.raid_disks); | |
78b10e66 | 11833 | |
4dd2df09 | 11834 | if (strcmp(st->container_devnm, st->devnm) == 0) { |
694575e7 KW |
11835 | /* On container level we can only increase number of devices. */ |
11836 | dprintf("imsm: info: Container operation\n"); | |
78b10e66 | 11837 | int old_raid_disks = 0; |
6dc0be30 | 11838 | |
78b10e66 | 11839 | if (imsm_reshape_is_allowed_on_container( |
fbf3d202 | 11840 | st, &geo, &old_raid_disks, direction)) { |
78b10e66 N |
11841 | struct imsm_update_reshape *u = NULL; |
11842 | int len; | |
11843 | ||
895ffd99 MT |
11844 | if (imsm_fix_size_mismatch(st, -1)) { |
11845 | dprintf("imsm: Cannot fix size mismatch\n"); | |
11846 | goto exit_imsm_reshape_super; | |
11847 | } | |
11848 | ||
78b10e66 N |
11849 | len = imsm_create_metadata_update_for_reshape( |
11850 | st, &geo, old_raid_disks, &u); | |
11851 | ||
ed08d51c AK |
11852 | if (len <= 0) { |
11853 | dprintf("imsm: Cannot prepare update\n"); | |
11854 | goto exit_imsm_reshape_super; | |
11855 | } | |
11856 | ||
8dd70bce AK |
11857 | ret_val = 0; |
11858 | /* update metadata locally */ | |
11859 | imsm_update_metadata_locally(st, u, len); | |
11860 | /* and possibly remotely */ | |
11861 | if (st->update_tail) | |
11862 | append_metadata_update(st, u, len); | |
11863 | else | |
ed08d51c | 11864 | free(u); |
8dd70bce | 11865 | |
694575e7 | 11866 | } else { |
7a862a02 | 11867 | pr_err("(imsm) Operation is not allowed on this container\n"); |
694575e7 KW |
11868 | } |
11869 | } else { | |
11870 | /* On volume level we support following operations | |
471bceb6 KW |
11871 | * - takeover: raid10 -> raid0; raid0 -> raid10 |
11872 | * - chunk size migration | |
11873 | * - migration: raid5 -> raid0; raid0 -> raid5 | |
11874 | */ | |
11875 | struct intel_super *super = st->sb; | |
11876 | struct intel_dev *dev = super->devlist; | |
4dd2df09 | 11877 | int change; |
694575e7 | 11878 | dprintf("imsm: info: Volume operation\n"); |
471bceb6 KW |
11879 | /* find requested device */ |
11880 | while (dev) { | |
1011e834 | 11881 | char *devnm = |
4dd2df09 N |
11882 | imsm_find_array_devnm_by_subdev( |
11883 | dev->index, st->container_devnm); | |
11884 | if (devnm && strcmp(devnm, geo.devnm) == 0) | |
471bceb6 KW |
11885 | break; |
11886 | dev = dev->next; | |
11887 | } | |
11888 | if (dev == NULL) { | |
4dd2df09 N |
11889 | pr_err("Cannot find %s (%s) subarray\n", |
11890 | geo.dev_name, geo.devnm); | |
471bceb6 KW |
11891 | goto exit_imsm_reshape_super; |
11892 | } | |
11893 | super->current_vol = dev->index; | |
fbf3d202 | 11894 | change = imsm_analyze_change(st, &geo, direction); |
694575e7 | 11895 | switch (change) { |
471bceb6 | 11896 | case CH_TAKEOVER: |
bb025c2f | 11897 | ret_val = imsm_takeover(st, &geo); |
694575e7 | 11898 | break; |
48c5303a PC |
11899 | case CH_MIGRATION: { |
11900 | struct imsm_update_reshape_migration *u = NULL; | |
11901 | int len = | |
11902 | imsm_create_metadata_update_for_migration( | |
11903 | st, &geo, &u); | |
11904 | if (len < 1) { | |
7a862a02 | 11905 | dprintf("imsm: Cannot prepare update\n"); |
48c5303a PC |
11906 | break; |
11907 | } | |
471bceb6 | 11908 | ret_val = 0; |
48c5303a PC |
11909 | /* update metadata locally */ |
11910 | imsm_update_metadata_locally(st, u, len); | |
11911 | /* and possibly remotely */ | |
11912 | if (st->update_tail) | |
11913 | append_metadata_update(st, u, len); | |
11914 | else | |
11915 | free(u); | |
11916 | } | |
11917 | break; | |
7abc9871 | 11918 | case CH_ARRAY_SIZE: { |
f3871fdc AK |
11919 | struct imsm_update_size_change *u = NULL; |
11920 | int len = | |
11921 | imsm_create_metadata_update_for_size_change( | |
11922 | st, &geo, &u); | |
11923 | if (len < 1) { | |
7a862a02 | 11924 | dprintf("imsm: Cannot prepare update\n"); |
f3871fdc AK |
11925 | break; |
11926 | } | |
11927 | ret_val = 0; | |
11928 | /* update metadata locally */ | |
11929 | imsm_update_metadata_locally(st, u, len); | |
11930 | /* and possibly remotely */ | |
11931 | if (st->update_tail) | |
11932 | append_metadata_update(st, u, len); | |
11933 | else | |
11934 | free(u); | |
7abc9871 AK |
11935 | } |
11936 | break; | |
471bceb6 KW |
11937 | default: |
11938 | ret_val = 1; | |
694575e7 | 11939 | } |
694575e7 | 11940 | } |
78b10e66 | 11941 | |
ed08d51c | 11942 | exit_imsm_reshape_super: |
78b10e66 N |
11943 | dprintf("imsm: reshape_super Exit code = %i\n", ret_val); |
11944 | return ret_val; | |
11945 | } | |
2cda7640 | 11946 | |
0febb20c AO |
11947 | #define COMPLETED_OK 0 |
11948 | #define COMPLETED_NONE 1 | |
11949 | #define COMPLETED_DELAYED 2 | |
11950 | ||
11951 | static int read_completed(int fd, unsigned long long *val) | |
11952 | { | |
11953 | int ret; | |
11954 | char buf[50]; | |
11955 | ||
11956 | ret = sysfs_fd_get_str(fd, buf, 50); | |
11957 | if (ret < 0) | |
11958 | return ret; | |
11959 | ||
11960 | ret = COMPLETED_OK; | |
11961 | if (strncmp(buf, "none", 4) == 0) { | |
11962 | ret = COMPLETED_NONE; | |
11963 | } else if (strncmp(buf, "delayed", 7) == 0) { | |
11964 | ret = COMPLETED_DELAYED; | |
11965 | } else { | |
11966 | char *ep; | |
11967 | *val = strtoull(buf, &ep, 0); | |
11968 | if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' ')) | |
11969 | ret = -1; | |
11970 | } | |
11971 | return ret; | |
11972 | } | |
11973 | ||
eee67a47 AK |
11974 | /******************************************************************************* |
11975 | * Function: wait_for_reshape_imsm | |
11976 | * Description: Function writes new sync_max value and waits until | |
11977 | * reshape process reach new position | |
11978 | * Parameters: | |
11979 | * sra : general array info | |
eee67a47 AK |
11980 | * ndata : number of disks in new array's layout |
11981 | * Returns: | |
11982 | * 0 : success, | |
11983 | * 1 : there is no reshape in progress, | |
11984 | * -1 : fail | |
11985 | ******************************************************************************/ | |
ae9f01f8 | 11986 | int wait_for_reshape_imsm(struct mdinfo *sra, int ndata) |
eee67a47 | 11987 | { |
85ca499c | 11988 | int fd = sysfs_get_fd(sra, NULL, "sync_completed"); |
df2647fa | 11989 | int retry = 3; |
eee67a47 | 11990 | unsigned long long completed; |
ae9f01f8 AK |
11991 | /* to_complete : new sync_max position */ |
11992 | unsigned long long to_complete = sra->reshape_progress; | |
11993 | unsigned long long position_to_set = to_complete / ndata; | |
eee67a47 | 11994 | |
ae9f01f8 | 11995 | if (fd < 0) { |
1ade5cc1 | 11996 | dprintf("cannot open reshape_position\n"); |
eee67a47 | 11997 | return 1; |
ae9f01f8 | 11998 | } |
eee67a47 | 11999 | |
df2647fa PB |
12000 | do { |
12001 | if (sysfs_fd_get_ll(fd, &completed) < 0) { | |
12002 | if (!retry) { | |
12003 | dprintf("cannot read reshape_position (no reshape in progres)\n"); | |
12004 | close(fd); | |
12005 | return 1; | |
12006 | } | |
12007 | usleep(30000); | |
12008 | } else | |
12009 | break; | |
12010 | } while (retry--); | |
eee67a47 | 12011 | |
85ca499c | 12012 | if (completed > position_to_set) { |
1ade5cc1 | 12013 | dprintf("wrong next position to set %llu (%llu)\n", |
85ca499c | 12014 | to_complete, position_to_set); |
ae9f01f8 AK |
12015 | close(fd); |
12016 | return -1; | |
12017 | } | |
12018 | dprintf("Position set: %llu\n", position_to_set); | |
12019 | if (sysfs_set_num(sra, NULL, "sync_max", | |
12020 | position_to_set) != 0) { | |
1ade5cc1 | 12021 | dprintf("cannot set reshape position to %llu\n", |
ae9f01f8 AK |
12022 | position_to_set); |
12023 | close(fd); | |
12024 | return -1; | |
eee67a47 AK |
12025 | } |
12026 | ||
eee67a47 | 12027 | do { |
0febb20c | 12028 | int rc; |
eee67a47 | 12029 | char action[20]; |
5ff3a780 | 12030 | int timeout = 3000; |
0febb20c | 12031 | |
5ff3a780 | 12032 | sysfs_wait(fd, &timeout); |
a47e44fb AK |
12033 | if (sysfs_get_str(sra, NULL, "sync_action", |
12034 | action, 20) > 0 && | |
d7d3809a | 12035 | strncmp(action, "reshape", 7) != 0) { |
b2be2b62 AO |
12036 | if (strncmp(action, "idle", 4) == 0) |
12037 | break; | |
d7d3809a AP |
12038 | close(fd); |
12039 | return -1; | |
12040 | } | |
0febb20c AO |
12041 | |
12042 | rc = read_completed(fd, &completed); | |
12043 | if (rc < 0) { | |
1ade5cc1 | 12044 | dprintf("cannot read reshape_position (in loop)\n"); |
eee67a47 AK |
12045 | close(fd); |
12046 | return 1; | |
0febb20c AO |
12047 | } else if (rc == COMPLETED_NONE) |
12048 | break; | |
85ca499c | 12049 | } while (completed < position_to_set); |
b2be2b62 | 12050 | |
eee67a47 AK |
12051 | close(fd); |
12052 | return 0; | |
eee67a47 AK |
12053 | } |
12054 | ||
b915c95f AK |
12055 | /******************************************************************************* |
12056 | * Function: check_degradation_change | |
12057 | * Description: Check that array hasn't become failed. | |
12058 | * Parameters: | |
12059 | * info : for sysfs access | |
12060 | * sources : source disks descriptors | |
12061 | * degraded: previous degradation level | |
12062 | * Returns: | |
12063 | * degradation level | |
12064 | ******************************************************************************/ | |
12065 | int check_degradation_change(struct mdinfo *info, | |
12066 | int *sources, | |
12067 | int degraded) | |
12068 | { | |
12069 | unsigned long long new_degraded; | |
e1993023 LD |
12070 | int rv; |
12071 | ||
12072 | rv = sysfs_get_ll(info, NULL, "degraded", &new_degraded); | |
089f9d79 | 12073 | if (rv == -1 || (new_degraded != (unsigned long long)degraded)) { |
b915c95f AK |
12074 | /* check each device to ensure it is still working */ |
12075 | struct mdinfo *sd; | |
12076 | new_degraded = 0; | |
12077 | for (sd = info->devs ; sd ; sd = sd->next) { | |
12078 | if (sd->disk.state & (1<<MD_DISK_FAULTY)) | |
12079 | continue; | |
12080 | if (sd->disk.state & (1<<MD_DISK_SYNC)) { | |
cf52eff5 TM |
12081 | char sbuf[100]; |
12082 | ||
b915c95f | 12083 | if (sysfs_get_str(info, |
cf52eff5 | 12084 | sd, "state", sbuf, sizeof(sbuf)) < 0 || |
b915c95f AK |
12085 | strstr(sbuf, "faulty") || |
12086 | strstr(sbuf, "in_sync") == NULL) { | |
12087 | /* this device is dead */ | |
12088 | sd->disk.state = (1<<MD_DISK_FAULTY); | |
12089 | if (sd->disk.raid_disk >= 0 && | |
12090 | sources[sd->disk.raid_disk] >= 0) { | |
12091 | close(sources[ | |
12092 | sd->disk.raid_disk]); | |
12093 | sources[sd->disk.raid_disk] = | |
12094 | -1; | |
12095 | } | |
12096 | new_degraded++; | |
12097 | } | |
12098 | } | |
12099 | } | |
12100 | } | |
12101 | ||
12102 | return new_degraded; | |
12103 | } | |
12104 | ||
10f22854 AK |
12105 | /******************************************************************************* |
12106 | * Function: imsm_manage_reshape | |
12107 | * Description: Function finds array under reshape and it manages reshape | |
12108 | * process. It creates stripes backups (if required) and sets | |
942e1cdb | 12109 | * checkpoints. |
10f22854 AK |
12110 | * Parameters: |
12111 | * afd : Backup handle (nattive) - not used | |
12112 | * sra : general array info | |
12113 | * reshape : reshape parameters - not used | |
12114 | * st : supertype structure | |
12115 | * blocks : size of critical section [blocks] | |
12116 | * fds : table of source device descriptor | |
12117 | * offsets : start of array (offest per devices) | |
12118 | * dests : not used | |
12119 | * destfd : table of destination device descriptor | |
12120 | * destoffsets : table of destination offsets (per device) | |
12121 | * Returns: | |
12122 | * 1 : success, reshape is done | |
12123 | * 0 : fail | |
12124 | ******************************************************************************/ | |
999b4972 N |
12125 | static int imsm_manage_reshape( |
12126 | int afd, struct mdinfo *sra, struct reshape *reshape, | |
10f22854 | 12127 | struct supertype *st, unsigned long backup_blocks, |
999b4972 N |
12128 | int *fds, unsigned long long *offsets, |
12129 | int dests, int *destfd, unsigned long long *destoffsets) | |
12130 | { | |
10f22854 AK |
12131 | int ret_val = 0; |
12132 | struct intel_super *super = st->sb; | |
594dc1b8 | 12133 | struct intel_dev *dv; |
de44e46f | 12134 | unsigned int sector_size = super->sector_size; |
10f22854 | 12135 | struct imsm_dev *dev = NULL; |
9529d343 | 12136 | struct imsm_map *map_src, *map_dest; |
10f22854 AK |
12137 | int migr_vol_qan = 0; |
12138 | int ndata, odata; /* [bytes] */ | |
12139 | int chunk; /* [bytes] */ | |
12140 | struct migr_record *migr_rec; | |
12141 | char *buf = NULL; | |
12142 | unsigned int buf_size; /* [bytes] */ | |
12143 | unsigned long long max_position; /* array size [bytes] */ | |
12144 | unsigned long long next_step; /* [blocks]/[bytes] */ | |
12145 | unsigned long long old_data_stripe_length; | |
10f22854 AK |
12146 | unsigned long long start_src; /* [bytes] */ |
12147 | unsigned long long start; /* [bytes] */ | |
12148 | unsigned long long start_buf_shift; /* [bytes] */ | |
b915c95f | 12149 | int degraded = 0; |
ab724b98 | 12150 | int source_layout = 0; |
895ffd99 | 12151 | int subarray_index = -1; |
10f22854 | 12152 | |
79a16a9b JS |
12153 | if (!sra) |
12154 | return ret_val; | |
12155 | ||
12156 | if (!fds || !offsets) | |
10f22854 AK |
12157 | goto abort; |
12158 | ||
12159 | /* Find volume during the reshape */ | |
12160 | for (dv = super->devlist; dv; dv = dv->next) { | |
fc54fe7a JS |
12161 | if (dv->dev->vol.migr_type == MIGR_GEN_MIGR && |
12162 | dv->dev->vol.migr_state == 1) { | |
10f22854 AK |
12163 | dev = dv->dev; |
12164 | migr_vol_qan++; | |
895ffd99 | 12165 | subarray_index = dv->index; |
10f22854 AK |
12166 | } |
12167 | } | |
12168 | /* Only one volume can migrate at the same time */ | |
12169 | if (migr_vol_qan != 1) { | |
676e87a8 | 12170 | pr_err("%s", migr_vol_qan ? |
10f22854 AK |
12171 | "Number of migrating volumes greater than 1\n" : |
12172 | "There is no volume during migrationg\n"); | |
12173 | goto abort; | |
12174 | } | |
12175 | ||
9529d343 | 12176 | map_dest = get_imsm_map(dev, MAP_0); |
238c0a71 | 12177 | map_src = get_imsm_map(dev, MAP_1); |
10f22854 AK |
12178 | if (map_src == NULL) |
12179 | goto abort; | |
10f22854 | 12180 | |
9529d343 MD |
12181 | ndata = imsm_num_data_members(map_dest); |
12182 | odata = imsm_num_data_members(map_src); | |
10f22854 | 12183 | |
7b1ab482 | 12184 | chunk = __le16_to_cpu(map_src->blocks_per_strip) * 512; |
10f22854 AK |
12185 | old_data_stripe_length = odata * chunk; |
12186 | ||
12187 | migr_rec = super->migr_rec; | |
12188 | ||
10f22854 AK |
12189 | /* initialize migration record for start condition */ |
12190 | if (sra->reshape_progress == 0) | |
12191 | init_migr_record_imsm(st, dev, sra); | |
b2c59438 AK |
12192 | else { |
12193 | if (__le32_to_cpu(migr_rec->rec_status) != UNIT_SRC_NORMAL) { | |
7a862a02 | 12194 | dprintf("imsm: cannot restart migration when data are present in copy area.\n"); |
b2c59438 AK |
12195 | goto abort; |
12196 | } | |
6a75c8ca AK |
12197 | /* Save checkpoint to update migration record for current |
12198 | * reshape position (in md). It can be farther than current | |
12199 | * reshape position in metadata. | |
12200 | */ | |
12201 | if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL) == 1) { | |
12202 | /* ignore error == 2, this can mean end of reshape here | |
12203 | */ | |
7a862a02 | 12204 | dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL, initial save)\n"); |
6a75c8ca AK |
12205 | goto abort; |
12206 | } | |
b2c59438 | 12207 | } |
10f22854 AK |
12208 | |
12209 | /* size for data */ | |
12210 | buf_size = __le32_to_cpu(migr_rec->blocks_per_unit) * 512; | |
12211 | /* extend buffer size for parity disk */ | |
12212 | buf_size += __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512; | |
3e684231 | 12213 | /* add space for stripe alignment */ |
10f22854 | 12214 | buf_size += old_data_stripe_length; |
de44e46f PB |
12215 | if (posix_memalign((void **)&buf, MAX_SECTOR_SIZE, buf_size)) { |
12216 | dprintf("imsm: Cannot allocate checkpoint buffer\n"); | |
10f22854 AK |
12217 | goto abort; |
12218 | } | |
12219 | ||
3ef4403c | 12220 | max_position = sra->component_size * ndata; |
68eb8bc6 | 12221 | source_layout = imsm_level_to_layout(map_src->raid_level); |
10f22854 | 12222 | |
9f421827 PB |
12223 | while (current_migr_unit(migr_rec) < |
12224 | get_num_migr_units(migr_rec)) { | |
10f22854 AK |
12225 | /* current reshape position [blocks] */ |
12226 | unsigned long long current_position = | |
12227 | __le32_to_cpu(migr_rec->blocks_per_unit) | |
9f421827 | 12228 | * current_migr_unit(migr_rec); |
10f22854 AK |
12229 | unsigned long long border; |
12230 | ||
b915c95f AK |
12231 | /* Check that array hasn't become failed. |
12232 | */ | |
12233 | degraded = check_degradation_change(sra, fds, degraded); | |
12234 | if (degraded > 1) { | |
7a862a02 | 12235 | dprintf("imsm: Abort reshape due to degradation level (%i)\n", degraded); |
b915c95f AK |
12236 | goto abort; |
12237 | } | |
12238 | ||
10f22854 AK |
12239 | next_step = __le32_to_cpu(migr_rec->blocks_per_unit); |
12240 | ||
12241 | if ((current_position + next_step) > max_position) | |
12242 | next_step = max_position - current_position; | |
12243 | ||
92144abf | 12244 | start = current_position * 512; |
10f22854 | 12245 | |
942e1cdb | 12246 | /* align reading start to old geometry */ |
10f22854 AK |
12247 | start_buf_shift = start % old_data_stripe_length; |
12248 | start_src = start - start_buf_shift; | |
12249 | ||
12250 | border = (start_src / odata) - (start / ndata); | |
12251 | border /= 512; | |
12252 | if (border <= __le32_to_cpu(migr_rec->dest_depth_per_unit)) { | |
12253 | /* save critical stripes to buf | |
12254 | * start - start address of current unit | |
12255 | * to backup [bytes] | |
12256 | * start_src - start address of current unit | |
12257 | * to backup alligned to source array | |
12258 | * [bytes] | |
12259 | */ | |
594dc1b8 | 12260 | unsigned long long next_step_filler; |
10f22854 AK |
12261 | unsigned long long copy_length = next_step * 512; |
12262 | ||
12263 | /* allign copy area length to stripe in old geometry */ | |
12264 | next_step_filler = ((copy_length + start_buf_shift) | |
12265 | % old_data_stripe_length); | |
12266 | if (next_step_filler) | |
12267 | next_step_filler = (old_data_stripe_length | |
12268 | - next_step_filler); | |
7a862a02 | 12269 | 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 |
12270 | start, start_src, copy_length, |
12271 | start_buf_shift, next_step_filler); | |
12272 | ||
12273 | if (save_stripes(fds, offsets, map_src->num_members, | |
ab724b98 AK |
12274 | chunk, map_src->raid_level, |
12275 | source_layout, 0, NULL, start_src, | |
10f22854 AK |
12276 | copy_length + |
12277 | next_step_filler + start_buf_shift, | |
12278 | buf)) { | |
7a862a02 | 12279 | dprintf("imsm: Cannot save stripes to buffer\n"); |
10f22854 AK |
12280 | goto abort; |
12281 | } | |
12282 | /* Convert data to destination format and store it | |
12283 | * in backup general migration area | |
12284 | */ | |
12285 | if (save_backup_imsm(st, dev, sra, | |
aea93171 | 12286 | buf + start_buf_shift, copy_length)) { |
7a862a02 | 12287 | dprintf("imsm: Cannot save stripes to target devices\n"); |
10f22854 AK |
12288 | goto abort; |
12289 | } | |
12290 | if (save_checkpoint_imsm(st, sra, | |
12291 | UNIT_SRC_IN_CP_AREA)) { | |
7a862a02 | 12292 | dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_IN_CP_AREA)\n"); |
10f22854 AK |
12293 | goto abort; |
12294 | } | |
8016a6d4 AK |
12295 | } else { |
12296 | /* set next step to use whole border area */ | |
12297 | border /= next_step; | |
12298 | if (border > 1) | |
12299 | next_step *= border; | |
10f22854 AK |
12300 | } |
12301 | /* When data backed up, checkpoint stored, | |
12302 | * kick the kernel to reshape unit of data | |
12303 | */ | |
12304 | next_step = next_step + sra->reshape_progress; | |
8016a6d4 AK |
12305 | /* limit next step to array max position */ |
12306 | if (next_step > max_position) | |
12307 | next_step = max_position; | |
10f22854 AK |
12308 | sysfs_set_num(sra, NULL, "suspend_lo", sra->reshape_progress); |
12309 | sysfs_set_num(sra, NULL, "suspend_hi", next_step); | |
ae9f01f8 | 12310 | sra->reshape_progress = next_step; |
10f22854 AK |
12311 | |
12312 | /* wait until reshape finish */ | |
c85338c6 | 12313 | if (wait_for_reshape_imsm(sra, ndata)) { |
c47b0ff6 AK |
12314 | dprintf("wait_for_reshape_imsm returned error!\n"); |
12315 | goto abort; | |
12316 | } | |
84d11e6c N |
12317 | if (sigterm) |
12318 | goto abort; | |
10f22854 | 12319 | |
0228d92c AK |
12320 | if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL) == 1) { |
12321 | /* ignore error == 2, this can mean end of reshape here | |
12322 | */ | |
7a862a02 | 12323 | dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL)\n"); |
10f22854 AK |
12324 | goto abort; |
12325 | } | |
12326 | ||
12327 | } | |
12328 | ||
71e5411e PB |
12329 | /* clear migr_rec on disks after successful migration */ |
12330 | struct dl *d; | |
12331 | ||
85337573 | 12332 | memset(super->migr_rec_buf, 0, MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE); |
71e5411e PB |
12333 | for (d = super->disks; d; d = d->next) { |
12334 | if (d->index < 0 || is_failed(&d->disk)) | |
12335 | continue; | |
12336 | unsigned long long dsize; | |
12337 | ||
12338 | get_dev_size(d->fd, NULL, &dsize); | |
de44e46f | 12339 | if (lseek64(d->fd, dsize - MIGR_REC_SECTOR_POSITION*sector_size, |
71e5411e | 12340 | SEEK_SET) >= 0) { |
466070ad | 12341 | if ((unsigned int)write(d->fd, super->migr_rec_buf, |
de44e46f PB |
12342 | MIGR_REC_BUF_SECTORS*sector_size) != |
12343 | MIGR_REC_BUF_SECTORS*sector_size) | |
71e5411e PB |
12344 | perror("Write migr_rec failed"); |
12345 | } | |
12346 | } | |
12347 | ||
10f22854 AK |
12348 | /* return '1' if done */ |
12349 | ret_val = 1; | |
895ffd99 MT |
12350 | |
12351 | /* After the reshape eliminate size mismatch in metadata. | |
12352 | * Don't update md/component_size here, volume hasn't | |
12353 | * to take whole space. It is allowed by kernel. | |
12354 | * md/component_size will be set propoperly after next assembly. | |
12355 | */ | |
12356 | imsm_fix_size_mismatch(st, subarray_index); | |
12357 | ||
10f22854 AK |
12358 | abort: |
12359 | free(buf); | |
942e1cdb N |
12360 | /* See Grow.c: abort_reshape() for further explanation */ |
12361 | sysfs_set_num(sra, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL); | |
12362 | sysfs_set_num(sra, NULL, "suspend_hi", 0); | |
12363 | sysfs_set_num(sra, NULL, "suspend_lo", 0); | |
10f22854 AK |
12364 | |
12365 | return ret_val; | |
999b4972 | 12366 | } |
0c21b485 | 12367 | |
fbc42556 JR |
12368 | /******************************************************************************* |
12369 | * Function: calculate_bitmap_min_chunksize | |
12370 | * Description: Calculates the minimal valid bitmap chunk size | |
12371 | * Parameters: | |
12372 | * max_bits : indicate how many bits can be used for the bitmap | |
12373 | * data_area_size : the size of the data area covered by the bitmap | |
12374 | * | |
12375 | * Returns: | |
12376 | * The bitmap chunk size | |
12377 | ******************************************************************************/ | |
12378 | static unsigned long long | |
12379 | calculate_bitmap_min_chunksize(unsigned long long max_bits, | |
12380 | unsigned long long data_area_size) | |
12381 | { | |
12382 | unsigned long long min_chunk = | |
12383 | 4096; /* sub-page chunks don't work yet.. */ | |
12384 | unsigned long long bits = data_area_size / min_chunk + 1; | |
12385 | ||
12386 | while (bits > max_bits) { | |
12387 | min_chunk *= 2; | |
12388 | bits = (bits + 1) / 2; | |
12389 | } | |
12390 | return min_chunk; | |
12391 | } | |
12392 | ||
12393 | /******************************************************************************* | |
12394 | * Function: calculate_bitmap_chunksize | |
12395 | * Description: Calculates the bitmap chunk size for the given device | |
12396 | * Parameters: | |
12397 | * st : supertype information | |
12398 | * dev : device for the bitmap | |
12399 | * | |
12400 | * Returns: | |
12401 | * The bitmap chunk size | |
12402 | ******************************************************************************/ | |
12403 | static unsigned long long calculate_bitmap_chunksize(struct supertype *st, | |
12404 | struct imsm_dev *dev) | |
12405 | { | |
12406 | struct intel_super *super = st->sb; | |
12407 | unsigned long long min_chunksize; | |
12408 | unsigned long long result = IMSM_DEFAULT_BITMAP_CHUNKSIZE; | |
12409 | size_t dev_size = imsm_dev_size(dev); | |
12410 | ||
12411 | min_chunksize = calculate_bitmap_min_chunksize( | |
12412 | IMSM_BITMAP_AREA_SIZE * super->sector_size, dev_size); | |
12413 | ||
12414 | if (result < min_chunksize) | |
12415 | result = min_chunksize; | |
12416 | ||
12417 | return result; | |
12418 | } | |
12419 | ||
12420 | /******************************************************************************* | |
12421 | * Function: init_bitmap_header | |
12422 | * Description: Initialize the bitmap header structure | |
12423 | * Parameters: | |
12424 | * st : supertype information | |
12425 | * bms : bitmap header struct to initialize | |
12426 | * dev : device for the bitmap | |
12427 | * | |
12428 | * Returns: | |
12429 | * 0 : success | |
12430 | * -1 : fail | |
12431 | ******************************************************************************/ | |
12432 | static int init_bitmap_header(struct supertype *st, struct bitmap_super_s *bms, | |
12433 | struct imsm_dev *dev) | |
12434 | { | |
12435 | int vol_uuid[4]; | |
12436 | ||
12437 | if (!bms || !dev) | |
12438 | return -1; | |
12439 | ||
12440 | bms->magic = __cpu_to_le32(BITMAP_MAGIC); | |
12441 | bms->version = __cpu_to_le32(BITMAP_MAJOR_HI); | |
12442 | bms->daemon_sleep = __cpu_to_le32(IMSM_DEFAULT_BITMAP_DAEMON_SLEEP); | |
12443 | bms->sync_size = __cpu_to_le64(IMSM_BITMAP_AREA_SIZE); | |
12444 | bms->write_behind = __cpu_to_le32(0); | |
12445 | ||
12446 | uuid_from_super_imsm(st, vol_uuid); | |
12447 | memcpy(bms->uuid, vol_uuid, 16); | |
12448 | ||
12449 | bms->chunksize = calculate_bitmap_chunksize(st, dev); | |
12450 | ||
12451 | return 0; | |
12452 | } | |
12453 | ||
12454 | /******************************************************************************* | |
12455 | * Function: validate_internal_bitmap_for_drive | |
12456 | * Description: Verify if the bitmap header for a given drive. | |
12457 | * Parameters: | |
12458 | * st : supertype information | |
12459 | * offset : The offset from the beginning of the drive where to look for | |
12460 | * the bitmap header. | |
12461 | * d : the drive info | |
12462 | * | |
12463 | * Returns: | |
12464 | * 0 : success | |
12465 | * -1 : fail | |
12466 | ******************************************************************************/ | |
12467 | static int validate_internal_bitmap_for_drive(struct supertype *st, | |
12468 | unsigned long long offset, | |
12469 | struct dl *d) | |
12470 | { | |
12471 | struct intel_super *super = st->sb; | |
12472 | int ret = -1; | |
12473 | int vol_uuid[4]; | |
12474 | bitmap_super_t *bms; | |
12475 | int fd; | |
12476 | ||
12477 | if (!d) | |
12478 | return -1; | |
12479 | ||
12480 | void *read_buf; | |
12481 | ||
12482 | if (posix_memalign(&read_buf, MAX_SECTOR_SIZE, IMSM_BITMAP_HEADER_SIZE)) | |
12483 | return -1; | |
12484 | ||
12485 | fd = d->fd; | |
12486 | if (fd < 0) { | |
12487 | fd = open(d->devname, O_RDONLY, 0); | |
12488 | if (fd < 0) { | |
12489 | dprintf("cannot open the device %s\n", d->devname); | |
12490 | goto abort; | |
12491 | } | |
12492 | } | |
12493 | ||
12494 | if (lseek64(fd, offset * super->sector_size, SEEK_SET) < 0) | |
12495 | goto abort; | |
12496 | if (read(fd, read_buf, IMSM_BITMAP_HEADER_SIZE) != | |
12497 | IMSM_BITMAP_HEADER_SIZE) | |
12498 | goto abort; | |
12499 | ||
12500 | uuid_from_super_imsm(st, vol_uuid); | |
12501 | ||
12502 | bms = read_buf; | |
12503 | if ((bms->magic != __cpu_to_le32(BITMAP_MAGIC)) || | |
12504 | (bms->version != __cpu_to_le32(BITMAP_MAJOR_HI)) || | |
12505 | (!same_uuid((int *)bms->uuid, vol_uuid, st->ss->swapuuid))) { | |
12506 | dprintf("wrong bitmap header detected\n"); | |
12507 | goto abort; | |
12508 | } | |
12509 | ||
12510 | ret = 0; | |
12511 | abort: | |
12512 | if ((d->fd < 0) && (fd >= 0)) | |
12513 | close(fd); | |
12514 | if (read_buf) | |
12515 | free(read_buf); | |
12516 | ||
12517 | return ret; | |
12518 | } | |
12519 | ||
12520 | /******************************************************************************* | |
12521 | * Function: validate_internal_bitmap_imsm | |
12522 | * Description: Verify if the bitmap header is in place and with proper data. | |
12523 | * Parameters: | |
12524 | * st : supertype information | |
12525 | * | |
12526 | * Returns: | |
12527 | * 0 : success or device w/o RWH_BITMAP | |
12528 | * -1 : fail | |
12529 | ******************************************************************************/ | |
12530 | static int validate_internal_bitmap_imsm(struct supertype *st) | |
12531 | { | |
12532 | struct intel_super *super = st->sb; | |
12533 | struct imsm_dev *dev = get_imsm_dev(super, super->current_vol); | |
12534 | unsigned long long offset; | |
12535 | struct dl *d; | |
12536 | ||
12537 | if (!dev) | |
12538 | return -1; | |
12539 | ||
12540 | if (dev->rwh_policy != RWH_BITMAP) | |
12541 | return 0; | |
12542 | ||
12543 | offset = get_bitmap_header_sector(super, super->current_vol); | |
12544 | for (d = super->disks; d; d = d->next) { | |
12545 | if (d->index < 0 || is_failed(&d->disk)) | |
12546 | continue; | |
12547 | ||
12548 | if (validate_internal_bitmap_for_drive(st, offset, d)) { | |
12549 | pr_err("imsm: bitmap validation failed\n"); | |
12550 | return -1; | |
12551 | } | |
12552 | } | |
12553 | return 0; | |
12554 | } | |
12555 | ||
12556 | /******************************************************************************* | |
12557 | * Function: add_internal_bitmap_imsm | |
12558 | * Description: Mark the volume to use the bitmap and updates the chunk size value. | |
12559 | * Parameters: | |
12560 | * st : supertype information | |
12561 | * chunkp : bitmap chunk size | |
12562 | * delay : not used for imsm | |
12563 | * write_behind : not used for imsm | |
12564 | * size : not used for imsm | |
12565 | * may_change : not used for imsm | |
12566 | * amajor : not used for imsm | |
12567 | * | |
12568 | * Returns: | |
12569 | * 0 : success | |
12570 | * -1 : fail | |
12571 | ******************************************************************************/ | |
12572 | static int add_internal_bitmap_imsm(struct supertype *st, int *chunkp, | |
12573 | int delay, int write_behind, | |
12574 | unsigned long long size, int may_change, | |
12575 | int amajor) | |
12576 | { | |
12577 | struct intel_super *super = st->sb; | |
12578 | int vol_idx = super->current_vol; | |
12579 | struct imsm_dev *dev; | |
12580 | ||
12581 | if (!super->devlist || vol_idx == -1 || !chunkp) | |
12582 | return -1; | |
12583 | ||
12584 | dev = get_imsm_dev(super, vol_idx); | |
12585 | ||
12586 | if (!dev) { | |
12587 | dprintf("cannot find the device for volume index %d\n", | |
12588 | vol_idx); | |
12589 | return -1; | |
12590 | } | |
12591 | dev->rwh_policy = RWH_BITMAP; | |
12592 | ||
12593 | *chunkp = calculate_bitmap_chunksize(st, dev); | |
12594 | ||
12595 | return 0; | |
12596 | } | |
12597 | ||
12598 | /******************************************************************************* | |
12599 | * Function: locate_bitmap_imsm | |
12600 | * Description: Seek 'fd' to start of write-intent-bitmap. | |
12601 | * Parameters: | |
12602 | * st : supertype information | |
12603 | * fd : file descriptor for the device | |
12604 | * node_num : not used for imsm | |
12605 | * | |
12606 | * Returns: | |
12607 | * 0 : success | |
12608 | * -1 : fail | |
12609 | ******************************************************************************/ | |
12610 | static int locate_bitmap_imsm(struct supertype *st, int fd, int node_num) | |
12611 | { | |
12612 | struct intel_super *super = st->sb; | |
12613 | unsigned long long offset; | |
12614 | int vol_idx = super->current_vol; | |
12615 | ||
12616 | if (!super->devlist || vol_idx == -1) | |
12617 | return -1; | |
12618 | ||
12619 | offset = get_bitmap_header_sector(super, super->current_vol); | |
12620 | dprintf("bitmap header offset is %llu\n", offset); | |
12621 | ||
12622 | lseek64(fd, offset << 9, 0); | |
12623 | ||
12624 | return 0; | |
12625 | } | |
12626 | ||
12627 | /******************************************************************************* | |
12628 | * Function: write_init_bitmap_imsm | |
12629 | * Description: Write a bitmap header and prepares the area for the bitmap. | |
12630 | * Parameters: | |
12631 | * st : supertype information | |
12632 | * fd : file descriptor for the device | |
12633 | * update : not used for imsm | |
12634 | * | |
12635 | * Returns: | |
12636 | * 0 : success | |
12637 | * -1 : fail | |
12638 | ******************************************************************************/ | |
12639 | static int write_init_bitmap_imsm(struct supertype *st, int fd, | |
12640 | enum bitmap_update update) | |
12641 | { | |
12642 | struct intel_super *super = st->sb; | |
12643 | int vol_idx = super->current_vol; | |
12644 | int ret = 0; | |
12645 | unsigned long long offset; | |
12646 | bitmap_super_t bms = { 0 }; | |
12647 | size_t written = 0; | |
12648 | size_t to_write; | |
12649 | ssize_t rv_num; | |
12650 | void *buf; | |
12651 | ||
12652 | if (!super->devlist || !super->sector_size || vol_idx == -1) | |
12653 | return -1; | |
12654 | ||
12655 | struct imsm_dev *dev = get_imsm_dev(super, vol_idx); | |
12656 | ||
12657 | /* first clear the space for bitmap header */ | |
12658 | unsigned long long bitmap_area_start = | |
12659 | get_bitmap_header_sector(super, vol_idx); | |
12660 | ||
12661 | dprintf("zeroing area start (%llu) and size (%u)\n", bitmap_area_start, | |
12662 | IMSM_BITMAP_AND_HEADER_SIZE / super->sector_size); | |
12663 | if (zero_disk_range(fd, bitmap_area_start, | |
12664 | IMSM_BITMAP_HEADER_SIZE / super->sector_size)) { | |
12665 | pr_err("imsm: cannot zeroing the space for the bitmap\n"); | |
12666 | return -1; | |
12667 | } | |
12668 | ||
12669 | /* The bitmap area should be filled with "1"s to perform initial | |
12670 | * synchronization. | |
12671 | */ | |
12672 | if (posix_memalign(&buf, MAX_SECTOR_SIZE, MAX_SECTOR_SIZE)) | |
12673 | return -1; | |
12674 | memset(buf, 0xFF, MAX_SECTOR_SIZE); | |
12675 | offset = get_bitmap_sector(super, vol_idx); | |
12676 | lseek64(fd, offset << 9, 0); | |
12677 | while (written < IMSM_BITMAP_AREA_SIZE) { | |
12678 | to_write = IMSM_BITMAP_AREA_SIZE - written; | |
12679 | if (to_write > MAX_SECTOR_SIZE) | |
12680 | to_write = MAX_SECTOR_SIZE; | |
12681 | rv_num = write(fd, buf, MAX_SECTOR_SIZE); | |
12682 | if (rv_num != MAX_SECTOR_SIZE) { | |
12683 | ret = -1; | |
12684 | dprintf("cannot initialize bitmap area\n"); | |
12685 | goto abort; | |
12686 | } | |
12687 | written += rv_num; | |
12688 | } | |
12689 | ||
12690 | /* write a bitmap header */ | |
12691 | init_bitmap_header(st, &bms, dev); | |
12692 | memset(buf, 0, MAX_SECTOR_SIZE); | |
12693 | memcpy(buf, &bms, sizeof(bitmap_super_t)); | |
12694 | if (locate_bitmap_imsm(st, fd, 0)) { | |
12695 | ret = -1; | |
12696 | dprintf("cannot locate the bitmap\n"); | |
12697 | goto abort; | |
12698 | } | |
12699 | if (write(fd, buf, MAX_SECTOR_SIZE) != MAX_SECTOR_SIZE) { | |
12700 | ret = -1; | |
12701 | dprintf("cannot write the bitmap header\n"); | |
12702 | goto abort; | |
12703 | } | |
12704 | fsync(fd); | |
12705 | ||
12706 | abort: | |
12707 | free(buf); | |
12708 | ||
12709 | return ret; | |
12710 | } | |
12711 | ||
12712 | /******************************************************************************* | |
12713 | * Function: is_vol_to_setup_bitmap | |
12714 | * Description: Checks if a bitmap should be activated on the dev. | |
12715 | * Parameters: | |
12716 | * info : info about the volume to setup the bitmap | |
12717 | * dev : the device to check against bitmap creation | |
12718 | * | |
12719 | * Returns: | |
12720 | * 0 : bitmap should be set up on the device | |
12721 | * -1 : otherwise | |
12722 | ******************************************************************************/ | |
12723 | static int is_vol_to_setup_bitmap(struct mdinfo *info, struct imsm_dev *dev) | |
12724 | { | |
12725 | if (!dev || !info) | |
12726 | return -1; | |
12727 | ||
12728 | if ((strcmp((char *)dev->volume, info->name) == 0) && | |
12729 | (dev->rwh_policy == RWH_BITMAP)) | |
12730 | return -1; | |
12731 | ||
12732 | return 0; | |
12733 | } | |
12734 | ||
12735 | /******************************************************************************* | |
12736 | * Function: set_bitmap_sysfs | |
12737 | * Description: Set the sysfs atributes of a given volume to activate the bitmap. | |
12738 | * Parameters: | |
12739 | * info : info about the volume where the bitmap should be setup | |
12740 | * chunksize : bitmap chunk size | |
12741 | * location : location of the bitmap | |
12742 | * | |
12743 | * Returns: | |
12744 | * 0 : success | |
12745 | * -1 : fail | |
12746 | ******************************************************************************/ | |
12747 | static int set_bitmap_sysfs(struct mdinfo *info, unsigned long long chunksize, | |
12748 | char *location) | |
12749 | { | |
12750 | /* The bitmap/metadata is set to external to allow changing of value for | |
12751 | * bitmap/location. When external is used, the kernel will treat an offset | |
12752 | * related to the device's first lba (in opposition to the "internal" case | |
12753 | * when this value is related to the beginning of the superblock). | |
12754 | */ | |
12755 | if (sysfs_set_str(info, NULL, "bitmap/metadata", "external")) { | |
12756 | dprintf("failed to set bitmap/metadata\n"); | |
12757 | return -1; | |
12758 | } | |
12759 | ||
12760 | /* It can only be changed when no bitmap is active. | |
12761 | * Should be bigger than 512 and must be power of 2. | |
12762 | * It is expecting the value in bytes. | |
12763 | */ | |
12764 | if (sysfs_set_num(info, NULL, "bitmap/chunksize", | |
12765 | __cpu_to_le32(chunksize))) { | |
12766 | dprintf("failed to set bitmap/chunksize\n"); | |
12767 | return -1; | |
12768 | } | |
12769 | ||
12770 | /* It is expecting the value in sectors. */ | |
12771 | if (sysfs_set_num(info, NULL, "bitmap/space", | |
12772 | __cpu_to_le64(IMSM_BITMAP_AREA_SIZE))) { | |
12773 | dprintf("failed to set bitmap/space\n"); | |
12774 | return -1; | |
12775 | } | |
12776 | ||
12777 | /* Determines the delay between the bitmap updates. | |
12778 | * It is expecting the value in seconds. | |
12779 | */ | |
12780 | if (sysfs_set_num(info, NULL, "bitmap/time_base", | |
12781 | __cpu_to_le64(IMSM_DEFAULT_BITMAP_DAEMON_SLEEP))) { | |
12782 | dprintf("failed to set bitmap/time_base\n"); | |
12783 | return -1; | |
12784 | } | |
12785 | ||
12786 | /* It is expecting the value in sectors with a sign at the beginning. */ | |
12787 | if (sysfs_set_str(info, NULL, "bitmap/location", location)) { | |
12788 | dprintf("failed to set bitmap/location\n"); | |
12789 | return -1; | |
12790 | } | |
12791 | ||
12792 | return 0; | |
12793 | } | |
12794 | ||
12795 | /******************************************************************************* | |
12796 | * Function: set_bitmap_imsm | |
12797 | * Description: Setup the bitmap for the given volume | |
12798 | * Parameters: | |
12799 | * st : supertype information | |
12800 | * info : info about the volume where the bitmap should be setup | |
12801 | * | |
12802 | * Returns: | |
12803 | * 0 : success | |
12804 | * -1 : fail | |
12805 | ******************************************************************************/ | |
12806 | static int set_bitmap_imsm(struct supertype *st, struct mdinfo *info) | |
12807 | { | |
12808 | struct intel_super *super = st->sb; | |
12809 | int prev_current_vol = super->current_vol; | |
12810 | struct imsm_dev *dev; | |
12811 | int ret = -1; | |
12812 | char location[16] = ""; | |
12813 | unsigned long long chunksize; | |
12814 | struct intel_dev *dev_it; | |
12815 | ||
12816 | for (dev_it = super->devlist; dev_it; dev_it = dev_it->next) { | |
12817 | super->current_vol = dev_it->index; | |
12818 | dev = get_imsm_dev(super, super->current_vol); | |
12819 | ||
12820 | if (is_vol_to_setup_bitmap(info, dev)) { | |
12821 | if (validate_internal_bitmap_imsm(st)) { | |
12822 | dprintf("bitmap header validation failed\n"); | |
12823 | goto abort; | |
12824 | } | |
12825 | ||
12826 | chunksize = calculate_bitmap_chunksize(st, dev); | |
12827 | dprintf("chunk size is %llu\n", chunksize); | |
12828 | ||
12829 | snprintf(location, sizeof(location), "+%llu", | |
12830 | get_bitmap_sector(super, super->current_vol)); | |
12831 | dprintf("bitmap offset is %s\n", location); | |
12832 | ||
12833 | if (set_bitmap_sysfs(info, chunksize, location)) { | |
12834 | dprintf("cannot setup the bitmap\n"); | |
12835 | goto abort; | |
12836 | } | |
12837 | } | |
12838 | } | |
12839 | ret = 0; | |
12840 | abort: | |
12841 | super->current_vol = prev_current_vol; | |
12842 | return ret; | |
12843 | } | |
12844 | ||
cdddbdbc | 12845 | struct superswitch super_imsm = { |
cdddbdbc DW |
12846 | .examine_super = examine_super_imsm, |
12847 | .brief_examine_super = brief_examine_super_imsm, | |
4737ae25 | 12848 | .brief_examine_subarrays = brief_examine_subarrays_imsm, |
9d84c8ea | 12849 | .export_examine_super = export_examine_super_imsm, |
cdddbdbc DW |
12850 | .detail_super = detail_super_imsm, |
12851 | .brief_detail_super = brief_detail_super_imsm, | |
bf5a934a | 12852 | .write_init_super = write_init_super_imsm, |
0e600426 N |
12853 | .validate_geometry = validate_geometry_imsm, |
12854 | .add_to_super = add_to_super_imsm, | |
1a64be56 | 12855 | .remove_from_super = remove_from_super_imsm, |
d665cc31 | 12856 | .detail_platform = detail_platform_imsm, |
e50cf220 | 12857 | .export_detail_platform = export_detail_platform_imsm, |
33414a01 | 12858 | .kill_subarray = kill_subarray_imsm, |
aa534678 | 12859 | .update_subarray = update_subarray_imsm, |
2b959fbf | 12860 | .load_container = load_container_imsm, |
71204a50 N |
12861 | .default_geometry = default_geometry_imsm, |
12862 | .get_disk_controller_domain = imsm_get_disk_controller_domain, | |
12863 | .reshape_super = imsm_reshape_super, | |
12864 | .manage_reshape = imsm_manage_reshape, | |
9e2d750d | 12865 | .recover_backup = recover_backup_imsm, |
27156a57 | 12866 | .examine_badblocks = examine_badblocks_imsm, |
cdddbdbc DW |
12867 | .match_home = match_home_imsm, |
12868 | .uuid_from_super= uuid_from_super_imsm, | |
12869 | .getinfo_super = getinfo_super_imsm, | |
5c4cd5da | 12870 | .getinfo_super_disks = getinfo_super_disks_imsm, |
cdddbdbc DW |
12871 | .update_super = update_super_imsm, |
12872 | ||
12873 | .avail_size = avail_size_imsm, | |
fbfdcb06 | 12874 | .get_spare_criteria = get_spare_criteria_imsm, |
cdddbdbc DW |
12875 | |
12876 | .compare_super = compare_super_imsm, | |
12877 | ||
12878 | .load_super = load_super_imsm, | |
bf5a934a | 12879 | .init_super = init_super_imsm, |
e683ca88 | 12880 | .store_super = store_super_imsm, |
cdddbdbc DW |
12881 | .free_super = free_super_imsm, |
12882 | .match_metadata_desc = match_metadata_desc_imsm, | |
bf5a934a | 12883 | .container_content = container_content_imsm, |
0c21b485 | 12884 | .validate_container = validate_container_imsm, |
cdddbdbc | 12885 | |
fbc42556 JR |
12886 | .add_internal_bitmap = add_internal_bitmap_imsm, |
12887 | .locate_bitmap = locate_bitmap_imsm, | |
12888 | .write_bitmap = write_init_bitmap_imsm, | |
12889 | .set_bitmap = set_bitmap_imsm, | |
12890 | ||
2432ce9b AP |
12891 | .write_init_ppl = write_init_ppl_imsm, |
12892 | .validate_ppl = validate_ppl_imsm, | |
12893 | ||
cdddbdbc | 12894 | .external = 1, |
4cce4069 | 12895 | .name = "imsm", |
845dea95 NB |
12896 | |
12897 | /* for mdmon */ | |
12898 | .open_new = imsm_open_new, | |
ed9d66aa | 12899 | .set_array_state= imsm_set_array_state, |
845dea95 NB |
12900 | .set_disk = imsm_set_disk, |
12901 | .sync_metadata = imsm_sync_metadata, | |
88758e9d | 12902 | .activate_spare = imsm_activate_spare, |
e8319a19 | 12903 | .process_update = imsm_process_update, |
8273f55e | 12904 | .prepare_update = imsm_prepare_update, |
6f50473f | 12905 | .record_bad_block = imsm_record_badblock, |
c07a5a4f | 12906 | .clear_bad_block = imsm_clear_badblock, |
928f1424 | 12907 | .get_bad_blocks = imsm_get_badblocks, |
cdddbdbc | 12908 | }; |