]>
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 | \ | |
418f9b36 N |
84 | MPB_ATTRIB_EXP_STRIPE_SIZE) |
85 | ||
86 | /* Define attributes that are unused but not harmful */ | |
87 | #define MPB_ATTRIB_IGNORED (MPB_ATTRIB_NEVER_USE) | |
fe7ed8cb | 88 | |
8e59f3d8 | 89 | #define MPB_SECTOR_CNT 2210 |
c2c087e6 | 90 | #define IMSM_RESERVED_SECTORS 4096 |
b81221b7 | 91 | #define NUM_BLOCKS_DIRTY_STRIPE_REGION 2056 |
979d38be | 92 | #define SECT_PER_MB_SHIFT 11 |
cdddbdbc DW |
93 | |
94 | /* Disk configuration info. */ | |
95 | #define IMSM_MAX_DEVICES 255 | |
96 | struct imsm_disk { | |
97 | __u8 serial[MAX_RAID_SERIAL_LEN];/* 0xD8 - 0xE7 ascii serial number */ | |
5551b113 | 98 | __u32 total_blocks_lo; /* 0xE8 - 0xEB total blocks lo */ |
cdddbdbc | 99 | __u32 scsi_id; /* 0xEC - 0xEF scsi ID */ |
f2f27e63 DW |
100 | #define SPARE_DISK __cpu_to_le32(0x01) /* Spare */ |
101 | #define CONFIGURED_DISK __cpu_to_le32(0x02) /* Member of some RaidDev */ | |
102 | #define FAILED_DISK __cpu_to_le32(0x04) /* Permanent failure */ | |
cdddbdbc | 103 | __u32 status; /* 0xF0 - 0xF3 */ |
1011e834 | 104 | __u32 owner_cfg_num; /* which config 0,1,2... owns this disk */ |
5551b113 CA |
105 | __u32 total_blocks_hi; /* 0xF4 - 0xF5 total blocks hi */ |
106 | #define IMSM_DISK_FILLERS 3 | |
107 | __u32 filler[IMSM_DISK_FILLERS]; /* 0xF5 - 0x107 MPB_DISK_FILLERS for future expansion */ | |
cdddbdbc DW |
108 | }; |
109 | ||
3b451610 AK |
110 | /* map selector for map managment |
111 | */ | |
238c0a71 AK |
112 | #define MAP_0 0 |
113 | #define MAP_1 1 | |
114 | #define MAP_X -1 | |
3b451610 | 115 | |
cdddbdbc DW |
116 | /* RAID map configuration infos. */ |
117 | struct imsm_map { | |
5551b113 CA |
118 | __u32 pba_of_lba0_lo; /* start address of partition */ |
119 | __u32 blocks_per_member_lo;/* blocks per member */ | |
120 | __u32 num_data_stripes_lo; /* number of data stripes */ | |
cdddbdbc DW |
121 | __u16 blocks_per_strip; |
122 | __u8 map_state; /* Normal, Uninitialized, Degraded, Failed */ | |
123 | #define IMSM_T_STATE_NORMAL 0 | |
124 | #define IMSM_T_STATE_UNINITIALIZED 1 | |
e3bba0e0 DW |
125 | #define IMSM_T_STATE_DEGRADED 2 |
126 | #define IMSM_T_STATE_FAILED 3 | |
cdddbdbc DW |
127 | __u8 raid_level; |
128 | #define IMSM_T_RAID0 0 | |
129 | #define IMSM_T_RAID1 1 | |
130 | #define IMSM_T_RAID5 5 /* since metadata version 1.2.02 ? */ | |
131 | __u8 num_members; /* number of member disks */ | |
fe7ed8cb DW |
132 | __u8 num_domains; /* number of parity domains */ |
133 | __u8 failed_disk_num; /* valid only when state is degraded */ | |
252d23c0 | 134 | __u8 ddf; |
5551b113 CA |
135 | __u32 pba_of_lba0_hi; |
136 | __u32 blocks_per_member_hi; | |
137 | __u32 num_data_stripes_hi; | |
138 | __u32 filler[4]; /* expansion area */ | |
7eef0453 | 139 | #define IMSM_ORD_REBUILD (1 << 24) |
cdddbdbc | 140 | __u32 disk_ord_tbl[1]; /* disk_ord_tbl[num_members], |
7eef0453 DW |
141 | * top byte contains some flags |
142 | */ | |
cdddbdbc DW |
143 | } __attribute__ ((packed)); |
144 | ||
145 | struct imsm_vol { | |
f8f603f1 | 146 | __u32 curr_migr_unit; |
fe7ed8cb | 147 | __u32 checkpoint_id; /* id to access curr_migr_unit */ |
cdddbdbc | 148 | __u8 migr_state; /* Normal or Migrating */ |
e3bba0e0 DW |
149 | #define MIGR_INIT 0 |
150 | #define MIGR_REBUILD 1 | |
151 | #define MIGR_VERIFY 2 /* analagous to echo check > sync_action */ | |
152 | #define MIGR_GEN_MIGR 3 | |
153 | #define MIGR_STATE_CHANGE 4 | |
1484e727 | 154 | #define MIGR_REPAIR 5 |
cdddbdbc DW |
155 | __u8 migr_type; /* Initializing, Rebuilding, ... */ |
156 | __u8 dirty; | |
fe7ed8cb DW |
157 | __u8 fs_state; /* fast-sync state for CnG (0xff == disabled) */ |
158 | __u16 verify_errors; /* number of mismatches */ | |
159 | __u16 bad_blocks; /* number of bad blocks during verify */ | |
160 | __u32 filler[4]; | |
cdddbdbc DW |
161 | struct imsm_map map[1]; |
162 | /* here comes another one if migr_state */ | |
163 | } __attribute__ ((packed)); | |
164 | ||
165 | struct imsm_dev { | |
fe7ed8cb | 166 | __u8 volume[MAX_RAID_SERIAL_LEN]; |
cdddbdbc DW |
167 | __u32 size_low; |
168 | __u32 size_high; | |
fe7ed8cb DW |
169 | #define DEV_BOOTABLE __cpu_to_le32(0x01) |
170 | #define DEV_BOOT_DEVICE __cpu_to_le32(0x02) | |
171 | #define DEV_READ_COALESCING __cpu_to_le32(0x04) | |
172 | #define DEV_WRITE_COALESCING __cpu_to_le32(0x08) | |
173 | #define DEV_LAST_SHUTDOWN_DIRTY __cpu_to_le32(0x10) | |
174 | #define DEV_HIDDEN_AT_BOOT __cpu_to_le32(0x20) | |
175 | #define DEV_CURRENTLY_HIDDEN __cpu_to_le32(0x40) | |
176 | #define DEV_VERIFY_AND_FIX __cpu_to_le32(0x80) | |
177 | #define DEV_MAP_STATE_UNINIT __cpu_to_le32(0x100) | |
178 | #define DEV_NO_AUTO_RECOVERY __cpu_to_le32(0x200) | |
179 | #define DEV_CLONE_N_GO __cpu_to_le32(0x400) | |
180 | #define DEV_CLONE_MAN_SYNC __cpu_to_le32(0x800) | |
181 | #define DEV_CNG_MASTER_DISK_NUM __cpu_to_le32(0x1000) | |
cdddbdbc DW |
182 | __u32 status; /* Persistent RaidDev status */ |
183 | __u32 reserved_blocks; /* Reserved blocks at beginning of volume */ | |
fe7ed8cb DW |
184 | __u8 migr_priority; |
185 | __u8 num_sub_vols; | |
186 | __u8 tid; | |
187 | __u8 cng_master_disk; | |
188 | __u16 cache_policy; | |
189 | __u8 cng_state; | |
190 | __u8 cng_sub_state; | |
191 | #define IMSM_DEV_FILLERS 10 | |
cdddbdbc DW |
192 | __u32 filler[IMSM_DEV_FILLERS]; |
193 | struct imsm_vol vol; | |
194 | } __attribute__ ((packed)); | |
195 | ||
196 | struct imsm_super { | |
197 | __u8 sig[MAX_SIGNATURE_LENGTH]; /* 0x00 - 0x1F */ | |
198 | __u32 check_sum; /* 0x20 - 0x23 MPB Checksum */ | |
199 | __u32 mpb_size; /* 0x24 - 0x27 Size of MPB */ | |
200 | __u32 family_num; /* 0x28 - 0x2B Checksum from first time this config was written */ | |
201 | __u32 generation_num; /* 0x2C - 0x2F Incremented each time this array's MPB is written */ | |
604b746f JD |
202 | __u32 error_log_size; /* 0x30 - 0x33 in bytes */ |
203 | __u32 attributes; /* 0x34 - 0x37 */ | |
cdddbdbc DW |
204 | __u8 num_disks; /* 0x38 Number of configured disks */ |
205 | __u8 num_raid_devs; /* 0x39 Number of configured volumes */ | |
604b746f JD |
206 | __u8 error_log_pos; /* 0x3A */ |
207 | __u8 fill[1]; /* 0x3B */ | |
208 | __u32 cache_size; /* 0x3c - 0x40 in mb */ | |
209 | __u32 orig_family_num; /* 0x40 - 0x43 original family num */ | |
210 | __u32 pwr_cycle_count; /* 0x44 - 0x47 simulated power cycle count for array */ | |
211 | __u32 bbm_log_size; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */ | |
212 | #define IMSM_FILLERS 35 | |
213 | __u32 filler[IMSM_FILLERS]; /* 0x4C - 0xD7 RAID_MPB_FILLERS */ | |
cdddbdbc DW |
214 | struct imsm_disk disk[1]; /* 0xD8 diskTbl[numDisks] */ |
215 | /* here comes imsm_dev[num_raid_devs] */ | |
604b746f | 216 | /* here comes BBM logs */ |
cdddbdbc DW |
217 | } __attribute__ ((packed)); |
218 | ||
604b746f JD |
219 | #define BBM_LOG_MAX_ENTRIES 254 |
220 | ||
221 | struct bbm_log_entry { | |
222 | __u64 defective_block_start; | |
223 | #define UNREADABLE 0xFFFFFFFF | |
224 | __u32 spare_block_offset; | |
225 | __u16 remapped_marked_count; | |
226 | __u16 disk_ordinal; | |
227 | } __attribute__ ((__packed__)); | |
228 | ||
229 | struct bbm_log { | |
230 | __u32 signature; /* 0xABADB10C */ | |
231 | __u32 entry_count; | |
232 | __u32 reserved_spare_block_count; /* 0 */ | |
233 | __u32 reserved; /* 0xFFFF */ | |
234 | __u64 first_spare_lba; | |
235 | struct bbm_log_entry mapped_block_entries[BBM_LOG_MAX_ENTRIES]; | |
236 | } __attribute__ ((__packed__)); | |
237 | ||
cdddbdbc DW |
238 | #ifndef MDASSEMBLE |
239 | static char *map_state_str[] = { "normal", "uninitialized", "degraded", "failed" }; | |
240 | #endif | |
241 | ||
8e59f3d8 AK |
242 | #define RAID_DISK_RESERVED_BLOCKS_IMSM_HI 2209 |
243 | ||
244 | #define GEN_MIGR_AREA_SIZE 2048 /* General Migration Copy Area size in blocks */ | |
245 | ||
17a4eaf9 AK |
246 | #define MIGR_REC_BUF_SIZE 512 /* size of migr_record i/o buffer */ |
247 | #define MIGR_REC_POSITION 512 /* migr_record position offset on disk, | |
248 | * MIGR_REC_BUF_SIZE <= MIGR_REC_POSITION | |
249 | */ | |
250 | ||
8e59f3d8 AK |
251 | #define UNIT_SRC_NORMAL 0 /* Source data for curr_migr_unit must |
252 | * be recovered using srcMap */ | |
253 | #define UNIT_SRC_IN_CP_AREA 1 /* Source data for curr_migr_unit has | |
254 | * already been migrated and must | |
255 | * be recovered from checkpoint area */ | |
256 | struct migr_record { | |
257 | __u32 rec_status; /* Status used to determine how to restart | |
258 | * migration in case it aborts | |
259 | * in some fashion */ | |
260 | __u32 curr_migr_unit; /* 0..numMigrUnits-1 */ | |
261 | __u32 family_num; /* Family number of MPB | |
262 | * containing the RaidDev | |
263 | * that is migrating */ | |
264 | __u32 ascending_migr; /* True if migrating in increasing | |
265 | * order of lbas */ | |
266 | __u32 blocks_per_unit; /* Num disk blocks per unit of operation */ | |
267 | __u32 dest_depth_per_unit; /* Num member blocks each destMap | |
268 | * member disk | |
269 | * advances per unit-of-operation */ | |
270 | __u32 ckpt_area_pba; /* Pba of first block of ckpt copy area */ | |
271 | __u32 dest_1st_member_lba; /* First member lba on first | |
272 | * stripe of destination */ | |
273 | __u32 num_migr_units; /* Total num migration units-of-op */ | |
274 | __u32 post_migr_vol_cap; /* Size of volume after | |
275 | * migration completes */ | |
276 | __u32 post_migr_vol_cap_hi; /* Expansion space for LBA64 */ | |
277 | __u32 ckpt_read_disk_num; /* Which member disk in destSubMap[0] the | |
278 | * migration ckpt record was read from | |
279 | * (for recovered migrations) */ | |
280 | } __attribute__ ((__packed__)); | |
281 | ||
ec50f7b6 LM |
282 | struct md_list { |
283 | /* usage marker: | |
284 | * 1: load metadata | |
285 | * 2: metadata does not match | |
286 | * 4: already checked | |
287 | */ | |
288 | int used; | |
289 | char *devname; | |
290 | int found; | |
291 | int container; | |
292 | dev_t st_rdev; | |
293 | struct md_list *next; | |
294 | }; | |
295 | ||
e7b84f9d | 296 | #define pr_vrb(fmt, arg...) (void) (verbose && pr_err(fmt, ##arg)) |
ec50f7b6 | 297 | |
1484e727 DW |
298 | static __u8 migr_type(struct imsm_dev *dev) |
299 | { | |
300 | if (dev->vol.migr_type == MIGR_VERIFY && | |
301 | dev->status & DEV_VERIFY_AND_FIX) | |
302 | return MIGR_REPAIR; | |
303 | else | |
304 | return dev->vol.migr_type; | |
305 | } | |
306 | ||
307 | static void set_migr_type(struct imsm_dev *dev, __u8 migr_type) | |
308 | { | |
309 | /* for compatibility with older oroms convert MIGR_REPAIR, into | |
310 | * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status | |
311 | */ | |
312 | if (migr_type == MIGR_REPAIR) { | |
313 | dev->vol.migr_type = MIGR_VERIFY; | |
314 | dev->status |= DEV_VERIFY_AND_FIX; | |
315 | } else { | |
316 | dev->vol.migr_type = migr_type; | |
317 | dev->status &= ~DEV_VERIFY_AND_FIX; | |
318 | } | |
319 | } | |
320 | ||
87eb16df | 321 | static unsigned int sector_count(__u32 bytes) |
cdddbdbc | 322 | { |
654a3817 | 323 | return ROUND_UP(bytes, 512) / 512; |
87eb16df | 324 | } |
cdddbdbc | 325 | |
87eb16df DW |
326 | static unsigned int mpb_sectors(struct imsm_super *mpb) |
327 | { | |
328 | return sector_count(__le32_to_cpu(mpb->mpb_size)); | |
cdddbdbc DW |
329 | } |
330 | ||
ba2de7ba DW |
331 | struct intel_dev { |
332 | struct imsm_dev *dev; | |
333 | struct intel_dev *next; | |
f21e18ca | 334 | unsigned index; |
ba2de7ba DW |
335 | }; |
336 | ||
88654014 LM |
337 | struct intel_hba { |
338 | enum sys_dev_type type; | |
339 | char *path; | |
340 | char *pci_id; | |
341 | struct intel_hba *next; | |
342 | }; | |
343 | ||
1a64be56 LM |
344 | enum action { |
345 | DISK_REMOVE = 1, | |
346 | DISK_ADD | |
347 | }; | |
cdddbdbc DW |
348 | /* internal representation of IMSM metadata */ |
349 | struct intel_super { | |
350 | union { | |
949c47a0 DW |
351 | void *buf; /* O_DIRECT buffer for reading/writing metadata */ |
352 | struct imsm_super *anchor; /* immovable parameters */ | |
cdddbdbc | 353 | }; |
8e59f3d8 AK |
354 | union { |
355 | void *migr_rec_buf; /* buffer for I/O operations */ | |
356 | struct migr_record *migr_rec; /* migration record */ | |
357 | }; | |
51d83f5d AK |
358 | int clean_migration_record_by_mdmon; /* when reshape is switched to next |
359 | array, it indicates that mdmon is allowed to clean migration | |
360 | record */ | |
949c47a0 | 361 | size_t len; /* size of the 'buf' allocation */ |
4d7b1503 DW |
362 | void *next_buf; /* for realloc'ing buf from the manager */ |
363 | size_t next_len; | |
c2c087e6 | 364 | int updates_pending; /* count of pending updates for mdmon */ |
bf5a934a | 365 | int current_vol; /* index of raid device undergoing creation */ |
5551b113 | 366 | unsigned long long create_offset; /* common start for 'current_vol' */ |
148acb7b | 367 | __u32 random; /* random data for seeding new family numbers */ |
ba2de7ba | 368 | struct intel_dev *devlist; |
cdddbdbc DW |
369 | struct dl { |
370 | struct dl *next; | |
371 | int index; | |
372 | __u8 serial[MAX_RAID_SERIAL_LEN]; | |
373 | int major, minor; | |
374 | char *devname; | |
b9f594fe | 375 | struct imsm_disk disk; |
cdddbdbc | 376 | int fd; |
0dcecb2e DW |
377 | int extent_cnt; |
378 | struct extent *e; /* for determining freespace @ create */ | |
efb30e7f | 379 | int raiddisk; /* slot to fill in autolayout */ |
1a64be56 | 380 | enum action action; |
ca0748fa | 381 | } *disks, *current_disk; |
1a64be56 LM |
382 | struct dl *disk_mgmt_list; /* list of disks to add/remove while mdmon |
383 | active */ | |
47ee5a45 | 384 | struct dl *missing; /* disks removed while we weren't looking */ |
43dad3d6 | 385 | struct bbm_log *bbm_log; |
88654014 | 386 | struct intel_hba *hba; /* device path of the raid controller for this metadata */ |
88c32bb1 | 387 | const struct imsm_orom *orom; /* platform firmware support */ |
a2b97981 DW |
388 | struct intel_super *next; /* (temp) list for disambiguating family_num */ |
389 | }; | |
390 | ||
391 | struct intel_disk { | |
392 | struct imsm_disk disk; | |
393 | #define IMSM_UNKNOWN_OWNER (-1) | |
394 | int owner; | |
395 | struct intel_disk *next; | |
cdddbdbc DW |
396 | }; |
397 | ||
c2c087e6 DW |
398 | struct extent { |
399 | unsigned long long start, size; | |
400 | }; | |
401 | ||
694575e7 KW |
402 | /* definitions of reshape process types */ |
403 | enum imsm_reshape_type { | |
404 | CH_TAKEOVER, | |
b5347799 | 405 | CH_MIGRATION, |
7abc9871 | 406 | CH_ARRAY_SIZE, |
694575e7 KW |
407 | }; |
408 | ||
88758e9d DW |
409 | /* definition of messages passed to imsm_process_update */ |
410 | enum imsm_update_type { | |
411 | update_activate_spare, | |
8273f55e | 412 | update_create_array, |
33414a01 | 413 | update_kill_array, |
aa534678 | 414 | update_rename_array, |
1a64be56 | 415 | update_add_remove_disk, |
78b10e66 | 416 | update_reshape_container_disks, |
48c5303a | 417 | update_reshape_migration, |
2d40f3a1 AK |
418 | update_takeover, |
419 | update_general_migration_checkpoint, | |
f3871fdc | 420 | update_size_change, |
88758e9d DW |
421 | }; |
422 | ||
423 | struct imsm_update_activate_spare { | |
424 | enum imsm_update_type type; | |
d23fe947 | 425 | struct dl *dl; |
88758e9d DW |
426 | int slot; |
427 | int array; | |
428 | struct imsm_update_activate_spare *next; | |
429 | }; | |
430 | ||
78b10e66 | 431 | struct geo_params { |
4dd2df09 | 432 | char devnm[32]; |
78b10e66 | 433 | char *dev_name; |
d04f65f4 | 434 | unsigned long long size; |
78b10e66 N |
435 | int level; |
436 | int layout; | |
437 | int chunksize; | |
438 | int raid_disks; | |
439 | }; | |
440 | ||
bb025c2f KW |
441 | enum takeover_direction { |
442 | R10_TO_R0, | |
443 | R0_TO_R10 | |
444 | }; | |
445 | struct imsm_update_takeover { | |
446 | enum imsm_update_type type; | |
447 | int subarray; | |
448 | enum takeover_direction direction; | |
449 | }; | |
78b10e66 N |
450 | |
451 | struct imsm_update_reshape { | |
452 | enum imsm_update_type type; | |
453 | int old_raid_disks; | |
454 | int new_raid_disks; | |
48c5303a PC |
455 | |
456 | int new_disks[1]; /* new_raid_disks - old_raid_disks makedev number */ | |
457 | }; | |
458 | ||
459 | struct imsm_update_reshape_migration { | |
460 | enum imsm_update_type type; | |
461 | int old_raid_disks; | |
462 | int new_raid_disks; | |
463 | /* fields for array migration changes | |
464 | */ | |
465 | int subdev; | |
466 | int new_level; | |
467 | int new_layout; | |
4bba0439 | 468 | int new_chunksize; |
48c5303a | 469 | |
d195167d | 470 | int new_disks[1]; /* new_raid_disks - old_raid_disks makedev number */ |
78b10e66 N |
471 | }; |
472 | ||
f3871fdc AK |
473 | struct imsm_update_size_change { |
474 | enum imsm_update_type type; | |
475 | int subdev; | |
476 | long long new_size; | |
477 | }; | |
478 | ||
2d40f3a1 AK |
479 | struct imsm_update_general_migration_checkpoint { |
480 | enum imsm_update_type type; | |
481 | __u32 curr_migr_unit; | |
482 | }; | |
483 | ||
54c2c1ea DW |
484 | struct disk_info { |
485 | __u8 serial[MAX_RAID_SERIAL_LEN]; | |
486 | }; | |
487 | ||
8273f55e DW |
488 | struct imsm_update_create_array { |
489 | enum imsm_update_type type; | |
8273f55e | 490 | int dev_idx; |
6a3e913e | 491 | struct imsm_dev dev; |
8273f55e DW |
492 | }; |
493 | ||
33414a01 DW |
494 | struct imsm_update_kill_array { |
495 | enum imsm_update_type type; | |
496 | int dev_idx; | |
497 | }; | |
498 | ||
aa534678 DW |
499 | struct imsm_update_rename_array { |
500 | enum imsm_update_type type; | |
501 | __u8 name[MAX_RAID_SERIAL_LEN]; | |
502 | int dev_idx; | |
503 | }; | |
504 | ||
1a64be56 | 505 | struct imsm_update_add_remove_disk { |
43dad3d6 DW |
506 | enum imsm_update_type type; |
507 | }; | |
508 | ||
88654014 LM |
509 | static const char *_sys_dev_type[] = { |
510 | [SYS_DEV_UNKNOWN] = "Unknown", | |
511 | [SYS_DEV_SAS] = "SAS", | |
614902f6 | 512 | [SYS_DEV_SATA] = "SATA", |
60f0f54d PB |
513 | [SYS_DEV_NVME] = "NVMe", |
514 | [SYS_DEV_VMD] = "VMD" | |
88654014 LM |
515 | }; |
516 | ||
517 | const char *get_sys_dev_type(enum sys_dev_type type) | |
518 | { | |
519 | if (type >= SYS_DEV_MAX) | |
520 | type = SYS_DEV_UNKNOWN; | |
521 | ||
522 | return _sys_dev_type[type]; | |
523 | } | |
524 | ||
525 | static struct intel_hba * alloc_intel_hba(struct sys_dev *device) | |
526 | { | |
503975b9 N |
527 | struct intel_hba *result = xmalloc(sizeof(*result)); |
528 | ||
529 | result->type = device->type; | |
530 | result->path = xstrdup(device->path); | |
531 | result->next = NULL; | |
532 | if (result->path && (result->pci_id = strrchr(result->path, '/')) != NULL) | |
533 | result->pci_id++; | |
534 | ||
88654014 LM |
535 | return result; |
536 | } | |
537 | ||
538 | static struct intel_hba * find_intel_hba(struct intel_hba *hba, struct sys_dev *device) | |
539 | { | |
594dc1b8 JS |
540 | struct intel_hba *result; |
541 | ||
88654014 LM |
542 | for (result = hba; result; result = result->next) { |
543 | if (result->type == device->type && strcmp(result->path, device->path) == 0) | |
544 | break; | |
545 | } | |
546 | return result; | |
547 | } | |
548 | ||
b4cf4cba | 549 | static int attach_hba_to_super(struct intel_super *super, struct sys_dev *device) |
88654014 LM |
550 | { |
551 | struct intel_hba *hba; | |
552 | ||
553 | /* check if disk attached to Intel HBA */ | |
554 | hba = find_intel_hba(super->hba, device); | |
555 | if (hba != NULL) | |
556 | return 1; | |
557 | /* Check if HBA is already attached to super */ | |
558 | if (super->hba == NULL) { | |
559 | super->hba = alloc_intel_hba(device); | |
560 | return 1; | |
6b781d33 AP |
561 | } |
562 | ||
563 | hba = super->hba; | |
564 | /* Intel metadata allows for all disks attached to the same type HBA. | |
614902f6 | 565 | * Do not support HBA types mixing |
6b781d33 AP |
566 | */ |
567 | if (device->type != hba->type) | |
88654014 | 568 | return 2; |
6b781d33 | 569 | |
60f0f54d PB |
570 | /* Always forbid spanning between VMD domains (seen as different controllers by mdadm) */ |
571 | if (device->type == SYS_DEV_VMD && !path_attached_to_hba(device->path, hba->path)) | |
572 | return 2; | |
573 | ||
6b781d33 AP |
574 | /* Multiple same type HBAs can be used if they share the same OROM */ |
575 | const struct imsm_orom *device_orom = get_orom_by_device_id(device->dev_id); | |
576 | ||
577 | if (device_orom != super->orom) | |
578 | return 2; | |
579 | ||
580 | while (hba->next) | |
581 | hba = hba->next; | |
582 | ||
583 | hba->next = alloc_intel_hba(device); | |
584 | return 1; | |
88654014 LM |
585 | } |
586 | ||
587 | static struct sys_dev* find_disk_attached_hba(int fd, const char *devname) | |
588 | { | |
9bc4ae77 | 589 | struct sys_dev *list, *elem; |
88654014 LM |
590 | char *disk_path; |
591 | ||
592 | if ((list = find_intel_devices()) == NULL) | |
593 | return 0; | |
594 | ||
595 | if (fd < 0) | |
596 | disk_path = (char *) devname; | |
597 | else | |
598 | disk_path = diskfd_to_devpath(fd); | |
599 | ||
9bc4ae77 | 600 | if (!disk_path) |
88654014 | 601 | return 0; |
88654014 | 602 | |
9bc4ae77 N |
603 | for (elem = list; elem; elem = elem->next) |
604 | if (path_attached_to_hba(disk_path, elem->path)) | |
88654014 | 605 | return elem; |
9bc4ae77 | 606 | |
88654014 LM |
607 | if (disk_path != devname) |
608 | free(disk_path); | |
88654014 LM |
609 | |
610 | return NULL; | |
611 | } | |
612 | ||
d424212e N |
613 | static int find_intel_hba_capability(int fd, struct intel_super *super, |
614 | char *devname); | |
f2f5c343 | 615 | |
cdddbdbc DW |
616 | static struct supertype *match_metadata_desc_imsm(char *arg) |
617 | { | |
618 | struct supertype *st; | |
619 | ||
620 | if (strcmp(arg, "imsm") != 0 && | |
621 | strcmp(arg, "default") != 0 | |
622 | ) | |
623 | return NULL; | |
624 | ||
503975b9 | 625 | st = xcalloc(1, sizeof(*st)); |
cdddbdbc DW |
626 | st->ss = &super_imsm; |
627 | st->max_devs = IMSM_MAX_DEVICES; | |
628 | st->minor_version = 0; | |
629 | st->sb = NULL; | |
630 | return st; | |
631 | } | |
632 | ||
0e600426 | 633 | #ifndef MDASSEMBLE |
cdddbdbc DW |
634 | static __u8 *get_imsm_version(struct imsm_super *mpb) |
635 | { | |
636 | return &mpb->sig[MPB_SIG_LEN]; | |
637 | } | |
9e2d750d | 638 | #endif |
cdddbdbc | 639 | |
949c47a0 DW |
640 | /* retrieve a disk directly from the anchor when the anchor is known to be |
641 | * up-to-date, currently only at load time | |
642 | */ | |
643 | static struct imsm_disk *__get_imsm_disk(struct imsm_super *mpb, __u8 index) | |
cdddbdbc | 644 | { |
949c47a0 | 645 | if (index >= mpb->num_disks) |
cdddbdbc DW |
646 | return NULL; |
647 | return &mpb->disk[index]; | |
648 | } | |
649 | ||
95d07a2c LM |
650 | /* retrieve the disk description based on a index of the disk |
651 | * in the sub-array | |
652 | */ | |
653 | static struct dl *get_imsm_dl_disk(struct intel_super *super, __u8 index) | |
949c47a0 | 654 | { |
b9f594fe DW |
655 | struct dl *d; |
656 | ||
657 | for (d = super->disks; d; d = d->next) | |
658 | if (d->index == index) | |
95d07a2c LM |
659 | return d; |
660 | ||
661 | return NULL; | |
662 | } | |
663 | /* retrieve a disk from the parsed metadata */ | |
664 | static struct imsm_disk *get_imsm_disk(struct intel_super *super, __u8 index) | |
665 | { | |
666 | struct dl *dl; | |
667 | ||
668 | dl = get_imsm_dl_disk(super, index); | |
669 | if (dl) | |
670 | return &dl->disk; | |
671 | ||
b9f594fe | 672 | return NULL; |
949c47a0 DW |
673 | } |
674 | ||
675 | /* generate a checksum directly from the anchor when the anchor is known to be | |
676 | * up-to-date, currently only at load or write_super after coalescing | |
677 | */ | |
678 | static __u32 __gen_imsm_checksum(struct imsm_super *mpb) | |
cdddbdbc DW |
679 | { |
680 | __u32 end = mpb->mpb_size / sizeof(end); | |
681 | __u32 *p = (__u32 *) mpb; | |
682 | __u32 sum = 0; | |
683 | ||
5d500228 N |
684 | while (end--) { |
685 | sum += __le32_to_cpu(*p); | |
97f734fd N |
686 | p++; |
687 | } | |
cdddbdbc | 688 | |
5d500228 | 689 | return sum - __le32_to_cpu(mpb->check_sum); |
cdddbdbc DW |
690 | } |
691 | ||
a965f303 DW |
692 | static size_t sizeof_imsm_map(struct imsm_map *map) |
693 | { | |
694 | return sizeof(struct imsm_map) + sizeof(__u32) * (map->num_members - 1); | |
695 | } | |
696 | ||
697 | struct imsm_map *get_imsm_map(struct imsm_dev *dev, int second_map) | |
cdddbdbc | 698 | { |
5e7b0330 AK |
699 | /* A device can have 2 maps if it is in the middle of a migration. |
700 | * If second_map is: | |
238c0a71 AK |
701 | * MAP_0 - we return the first map |
702 | * MAP_1 - we return the second map if it exists, else NULL | |
703 | * MAP_X - we return the second map if it exists, else the first | |
5e7b0330 | 704 | */ |
a965f303 | 705 | struct imsm_map *map = &dev->vol.map[0]; |
9535fc47 | 706 | struct imsm_map *map2 = NULL; |
a965f303 | 707 | |
9535fc47 AK |
708 | if (dev->vol.migr_state) |
709 | map2 = (void *)map + sizeof_imsm_map(map); | |
a965f303 | 710 | |
9535fc47 | 711 | switch (second_map) { |
3b451610 | 712 | case MAP_0: |
9535fc47 | 713 | break; |
3b451610 | 714 | case MAP_1: |
9535fc47 AK |
715 | map = map2; |
716 | break; | |
238c0a71 | 717 | case MAP_X: |
9535fc47 AK |
718 | if (map2) |
719 | map = map2; | |
720 | break; | |
9535fc47 AK |
721 | default: |
722 | map = NULL; | |
723 | } | |
724 | return map; | |
5e7b0330 | 725 | |
a965f303 | 726 | } |
cdddbdbc | 727 | |
3393c6af DW |
728 | /* return the size of the device. |
729 | * migr_state increases the returned size if map[0] were to be duplicated | |
730 | */ | |
731 | static size_t sizeof_imsm_dev(struct imsm_dev *dev, int migr_state) | |
a965f303 DW |
732 | { |
733 | size_t size = sizeof(*dev) - sizeof(struct imsm_map) + | |
238c0a71 | 734 | sizeof_imsm_map(get_imsm_map(dev, MAP_0)); |
cdddbdbc DW |
735 | |
736 | /* migrating means an additional map */ | |
a965f303 | 737 | if (dev->vol.migr_state) |
238c0a71 | 738 | size += sizeof_imsm_map(get_imsm_map(dev, MAP_1)); |
3393c6af | 739 | else if (migr_state) |
238c0a71 | 740 | size += sizeof_imsm_map(get_imsm_map(dev, MAP_0)); |
cdddbdbc DW |
741 | |
742 | return size; | |
743 | } | |
744 | ||
54c2c1ea DW |
745 | #ifndef MDASSEMBLE |
746 | /* retrieve disk serial number list from a metadata update */ | |
747 | static struct disk_info *get_disk_info(struct imsm_update_create_array *update) | |
748 | { | |
749 | void *u = update; | |
750 | struct disk_info *inf; | |
751 | ||
752 | inf = u + sizeof(*update) - sizeof(struct imsm_dev) + | |
753 | sizeof_imsm_dev(&update->dev, 0); | |
754 | ||
755 | return inf; | |
756 | } | |
757 | #endif | |
758 | ||
949c47a0 | 759 | static struct imsm_dev *__get_imsm_dev(struct imsm_super *mpb, __u8 index) |
cdddbdbc DW |
760 | { |
761 | int offset; | |
762 | int i; | |
763 | void *_mpb = mpb; | |
764 | ||
949c47a0 | 765 | if (index >= mpb->num_raid_devs) |
cdddbdbc DW |
766 | return NULL; |
767 | ||
768 | /* devices start after all disks */ | |
769 | offset = ((void *) &mpb->disk[mpb->num_disks]) - _mpb; | |
770 | ||
771 | for (i = 0; i <= index; i++) | |
772 | if (i == index) | |
773 | return _mpb + offset; | |
774 | else | |
3393c6af | 775 | offset += sizeof_imsm_dev(_mpb + offset, 0); |
cdddbdbc DW |
776 | |
777 | return NULL; | |
778 | } | |
779 | ||
949c47a0 DW |
780 | static struct imsm_dev *get_imsm_dev(struct intel_super *super, __u8 index) |
781 | { | |
ba2de7ba DW |
782 | struct intel_dev *dv; |
783 | ||
949c47a0 DW |
784 | if (index >= super->anchor->num_raid_devs) |
785 | return NULL; | |
ba2de7ba DW |
786 | for (dv = super->devlist; dv; dv = dv->next) |
787 | if (dv->index == index) | |
788 | return dv->dev; | |
789 | return NULL; | |
949c47a0 DW |
790 | } |
791 | ||
98130f40 AK |
792 | /* |
793 | * for second_map: | |
238c0a71 AK |
794 | * == MAP_0 get first map |
795 | * == MAP_1 get second map | |
796 | * == MAP_X than get map according to the current migr_state | |
98130f40 AK |
797 | */ |
798 | static __u32 get_imsm_ord_tbl_ent(struct imsm_dev *dev, | |
799 | int slot, | |
800 | int second_map) | |
7eef0453 DW |
801 | { |
802 | struct imsm_map *map; | |
803 | ||
5e7b0330 | 804 | map = get_imsm_map(dev, second_map); |
7eef0453 | 805 | |
ff077194 DW |
806 | /* top byte identifies disk under rebuild */ |
807 | return __le32_to_cpu(map->disk_ord_tbl[slot]); | |
808 | } | |
809 | ||
810 | #define ord_to_idx(ord) (((ord) << 8) >> 8) | |
98130f40 | 811 | static __u32 get_imsm_disk_idx(struct imsm_dev *dev, int slot, int second_map) |
ff077194 | 812 | { |
98130f40 | 813 | __u32 ord = get_imsm_ord_tbl_ent(dev, slot, second_map); |
ff077194 DW |
814 | |
815 | return ord_to_idx(ord); | |
7eef0453 DW |
816 | } |
817 | ||
be73972f DW |
818 | static void set_imsm_ord_tbl_ent(struct imsm_map *map, int slot, __u32 ord) |
819 | { | |
820 | map->disk_ord_tbl[slot] = __cpu_to_le32(ord); | |
821 | } | |
822 | ||
f21e18ca | 823 | static int get_imsm_disk_slot(struct imsm_map *map, unsigned idx) |
620b1713 DW |
824 | { |
825 | int slot; | |
826 | __u32 ord; | |
827 | ||
828 | for (slot = 0; slot < map->num_members; slot++) { | |
829 | ord = __le32_to_cpu(map->disk_ord_tbl[slot]); | |
830 | if (ord_to_idx(ord) == idx) | |
831 | return slot; | |
832 | } | |
833 | ||
834 | return -1; | |
835 | } | |
836 | ||
cdddbdbc DW |
837 | static int get_imsm_raid_level(struct imsm_map *map) |
838 | { | |
839 | if (map->raid_level == 1) { | |
840 | if (map->num_members == 2) | |
841 | return 1; | |
842 | else | |
843 | return 10; | |
844 | } | |
845 | ||
846 | return map->raid_level; | |
847 | } | |
848 | ||
c2c087e6 DW |
849 | static int cmp_extent(const void *av, const void *bv) |
850 | { | |
851 | const struct extent *a = av; | |
852 | const struct extent *b = bv; | |
853 | if (a->start < b->start) | |
854 | return -1; | |
855 | if (a->start > b->start) | |
856 | return 1; | |
857 | return 0; | |
858 | } | |
859 | ||
0dcecb2e | 860 | static int count_memberships(struct dl *dl, struct intel_super *super) |
c2c087e6 | 861 | { |
c2c087e6 | 862 | int memberships = 0; |
620b1713 | 863 | int i; |
c2c087e6 | 864 | |
949c47a0 DW |
865 | for (i = 0; i < super->anchor->num_raid_devs; i++) { |
866 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
238c0a71 | 867 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
c2c087e6 | 868 | |
620b1713 DW |
869 | if (get_imsm_disk_slot(map, dl->index) >= 0) |
870 | memberships++; | |
c2c087e6 | 871 | } |
0dcecb2e DW |
872 | |
873 | return memberships; | |
874 | } | |
875 | ||
b81221b7 CA |
876 | static __u32 imsm_min_reserved_sectors(struct intel_super *super); |
877 | ||
5551b113 CA |
878 | static int split_ull(unsigned long long n, __u32 *lo, __u32 *hi) |
879 | { | |
880 | if (lo == 0 || hi == 0) | |
881 | return 1; | |
882 | *lo = __le32_to_cpu((unsigned)n); | |
883 | *hi = __le32_to_cpu((unsigned)(n >> 32)); | |
884 | return 0; | |
885 | } | |
886 | ||
887 | static unsigned long long join_u32(__u32 lo, __u32 hi) | |
888 | { | |
889 | return (unsigned long long)__le32_to_cpu(lo) | | |
890 | (((unsigned long long)__le32_to_cpu(hi)) << 32); | |
891 | } | |
892 | ||
893 | static unsigned long long total_blocks(struct imsm_disk *disk) | |
894 | { | |
895 | if (disk == NULL) | |
896 | return 0; | |
897 | return join_u32(disk->total_blocks_lo, disk->total_blocks_hi); | |
898 | } | |
899 | ||
900 | static unsigned long long pba_of_lba0(struct imsm_map *map) | |
901 | { | |
902 | if (map == NULL) | |
903 | return 0; | |
904 | return join_u32(map->pba_of_lba0_lo, map->pba_of_lba0_hi); | |
905 | } | |
906 | ||
907 | static unsigned long long blocks_per_member(struct imsm_map *map) | |
908 | { | |
909 | if (map == NULL) | |
910 | return 0; | |
911 | return join_u32(map->blocks_per_member_lo, map->blocks_per_member_hi); | |
912 | } | |
913 | ||
ae2416e7 | 914 | #ifndef MDASSEMBLE |
5551b113 CA |
915 | static unsigned long long num_data_stripes(struct imsm_map *map) |
916 | { | |
917 | if (map == NULL) | |
918 | return 0; | |
919 | return join_u32(map->num_data_stripes_lo, map->num_data_stripes_hi); | |
920 | } | |
921 | ||
922 | static void set_total_blocks(struct imsm_disk *disk, unsigned long long n) | |
923 | { | |
924 | split_ull(n, &disk->total_blocks_lo, &disk->total_blocks_hi); | |
925 | } | |
ae2416e7 | 926 | #endif |
5551b113 CA |
927 | |
928 | static void set_pba_of_lba0(struct imsm_map *map, unsigned long long n) | |
929 | { | |
930 | split_ull(n, &map->pba_of_lba0_lo, &map->pba_of_lba0_hi); | |
931 | } | |
932 | ||
933 | static void set_blocks_per_member(struct imsm_map *map, unsigned long long n) | |
934 | { | |
935 | split_ull(n, &map->blocks_per_member_lo, &map->blocks_per_member_hi); | |
936 | } | |
937 | ||
938 | static void set_num_data_stripes(struct imsm_map *map, unsigned long long n) | |
939 | { | |
940 | split_ull(n, &map->num_data_stripes_lo, &map->num_data_stripes_hi); | |
941 | } | |
942 | ||
0dcecb2e DW |
943 | static struct extent *get_extents(struct intel_super *super, struct dl *dl) |
944 | { | |
945 | /* find a list of used extents on the given physical device */ | |
946 | struct extent *rv, *e; | |
620b1713 | 947 | int i; |
0dcecb2e | 948 | int memberships = count_memberships(dl, super); |
b276dd33 DW |
949 | __u32 reservation; |
950 | ||
951 | /* trim the reserved area for spares, so they can join any array | |
952 | * regardless of whether the OROM has assigned sectors from the | |
953 | * IMSM_RESERVED_SECTORS region | |
954 | */ | |
955 | if (dl->index == -1) | |
b81221b7 | 956 | reservation = imsm_min_reserved_sectors(super); |
b276dd33 DW |
957 | else |
958 | reservation = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS; | |
0dcecb2e | 959 | |
503975b9 | 960 | rv = xcalloc(sizeof(struct extent), (memberships + 1)); |
c2c087e6 DW |
961 | e = rv; |
962 | ||
949c47a0 DW |
963 | for (i = 0; i < super->anchor->num_raid_devs; i++) { |
964 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
238c0a71 | 965 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
c2c087e6 | 966 | |
620b1713 | 967 | if (get_imsm_disk_slot(map, dl->index) >= 0) { |
5551b113 CA |
968 | e->start = pba_of_lba0(map); |
969 | e->size = blocks_per_member(map); | |
620b1713 | 970 | e++; |
c2c087e6 DW |
971 | } |
972 | } | |
973 | qsort(rv, memberships, sizeof(*rv), cmp_extent); | |
974 | ||
1011e834 | 975 | /* determine the start of the metadata |
14e8215b DW |
976 | * when no raid devices are defined use the default |
977 | * ...otherwise allow the metadata to truncate the value | |
978 | * as is the case with older versions of imsm | |
979 | */ | |
980 | if (memberships) { | |
981 | struct extent *last = &rv[memberships - 1]; | |
5551b113 | 982 | unsigned long long remainder; |
14e8215b | 983 | |
5551b113 | 984 | remainder = total_blocks(&dl->disk) - (last->start + last->size); |
dda5855f DW |
985 | /* round down to 1k block to satisfy precision of the kernel |
986 | * 'size' interface | |
987 | */ | |
988 | remainder &= ~1UL; | |
989 | /* make sure remainder is still sane */ | |
f21e18ca | 990 | if (remainder < (unsigned)ROUND_UP(super->len, 512) >> 9) |
dda5855f | 991 | remainder = ROUND_UP(super->len, 512) >> 9; |
14e8215b DW |
992 | if (reservation > remainder) |
993 | reservation = remainder; | |
994 | } | |
5551b113 | 995 | e->start = total_blocks(&dl->disk) - reservation; |
c2c087e6 DW |
996 | e->size = 0; |
997 | return rv; | |
998 | } | |
999 | ||
14e8215b DW |
1000 | /* try to determine how much space is reserved for metadata from |
1001 | * the last get_extents() entry, otherwise fallback to the | |
1002 | * default | |
1003 | */ | |
1004 | static __u32 imsm_reserved_sectors(struct intel_super *super, struct dl *dl) | |
1005 | { | |
1006 | struct extent *e; | |
1007 | int i; | |
1008 | __u32 rv; | |
1009 | ||
1010 | /* for spares just return a minimal reservation which will grow | |
1011 | * once the spare is picked up by an array | |
1012 | */ | |
1013 | if (dl->index == -1) | |
1014 | return MPB_SECTOR_CNT; | |
1015 | ||
1016 | e = get_extents(super, dl); | |
1017 | if (!e) | |
1018 | return MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS; | |
1019 | ||
1020 | /* scroll to last entry */ | |
1021 | for (i = 0; e[i].size; i++) | |
1022 | continue; | |
1023 | ||
5551b113 | 1024 | rv = total_blocks(&dl->disk) - e[i].start; |
14e8215b DW |
1025 | |
1026 | free(e); | |
1027 | ||
1028 | return rv; | |
1029 | } | |
1030 | ||
25ed7e59 DW |
1031 | static int is_spare(struct imsm_disk *disk) |
1032 | { | |
1033 | return (disk->status & SPARE_DISK) == SPARE_DISK; | |
1034 | } | |
1035 | ||
1036 | static int is_configured(struct imsm_disk *disk) | |
1037 | { | |
1038 | return (disk->status & CONFIGURED_DISK) == CONFIGURED_DISK; | |
1039 | } | |
1040 | ||
1041 | static int is_failed(struct imsm_disk *disk) | |
1042 | { | |
1043 | return (disk->status & FAILED_DISK) == FAILED_DISK; | |
1044 | } | |
1045 | ||
b81221b7 CA |
1046 | /* try to determine how much space is reserved for metadata from |
1047 | * the last get_extents() entry on the smallest active disk, | |
1048 | * otherwise fallback to the default | |
1049 | */ | |
1050 | static __u32 imsm_min_reserved_sectors(struct intel_super *super) | |
1051 | { | |
1052 | struct extent *e; | |
1053 | int i; | |
5551b113 CA |
1054 | unsigned long long min_active; |
1055 | __u32 remainder; | |
b81221b7 CA |
1056 | __u32 rv = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS; |
1057 | struct dl *dl, *dl_min = NULL; | |
1058 | ||
1059 | if (!super) | |
1060 | return rv; | |
1061 | ||
1062 | min_active = 0; | |
1063 | for (dl = super->disks; dl; dl = dl->next) { | |
1064 | if (dl->index < 0) | |
1065 | continue; | |
5551b113 CA |
1066 | unsigned long long blocks = total_blocks(&dl->disk); |
1067 | if (blocks < min_active || min_active == 0) { | |
b81221b7 | 1068 | dl_min = dl; |
5551b113 | 1069 | min_active = blocks; |
b81221b7 CA |
1070 | } |
1071 | } | |
1072 | if (!dl_min) | |
1073 | return rv; | |
1074 | ||
1075 | /* find last lba used by subarrays on the smallest active disk */ | |
1076 | e = get_extents(super, dl_min); | |
1077 | if (!e) | |
1078 | return rv; | |
1079 | for (i = 0; e[i].size; i++) | |
1080 | continue; | |
1081 | ||
1082 | remainder = min_active - e[i].start; | |
1083 | free(e); | |
1084 | ||
1085 | /* to give priority to recovery we should not require full | |
1086 | IMSM_RESERVED_SECTORS from the spare */ | |
1087 | rv = MPB_SECTOR_CNT + NUM_BLOCKS_DIRTY_STRIPE_REGION; | |
1088 | ||
1089 | /* if real reservation is smaller use that value */ | |
1090 | return (remainder < rv) ? remainder : rv; | |
1091 | } | |
1092 | ||
80e7f8c3 AC |
1093 | /* Return minimum size of a spare that can be used in this array*/ |
1094 | static unsigned long long min_acceptable_spare_size_imsm(struct supertype *st) | |
1095 | { | |
1096 | struct intel_super *super = st->sb; | |
1097 | struct dl *dl; | |
1098 | struct extent *e; | |
1099 | int i; | |
1100 | unsigned long long rv = 0; | |
1101 | ||
1102 | if (!super) | |
1103 | return rv; | |
1104 | /* find first active disk in array */ | |
1105 | dl = super->disks; | |
1106 | while (dl && (is_failed(&dl->disk) || dl->index == -1)) | |
1107 | dl = dl->next; | |
1108 | if (!dl) | |
1109 | return rv; | |
1110 | /* find last lba used by subarrays */ | |
1111 | e = get_extents(super, dl); | |
1112 | if (!e) | |
1113 | return rv; | |
1114 | for (i = 0; e[i].size; i++) | |
1115 | continue; | |
1116 | if (i > 0) | |
1117 | rv = e[i-1].start + e[i-1].size; | |
1118 | free(e); | |
b81221b7 | 1119 | |
80e7f8c3 | 1120 | /* add the amount of space needed for metadata */ |
b81221b7 CA |
1121 | rv = rv + imsm_min_reserved_sectors(super); |
1122 | ||
80e7f8c3 AC |
1123 | return rv * 512; |
1124 | } | |
1125 | ||
d1e02575 AK |
1126 | static int is_gen_migration(struct imsm_dev *dev); |
1127 | ||
1799c9e8 | 1128 | #ifndef MDASSEMBLE |
c47b0ff6 AK |
1129 | static __u64 blocks_per_migr_unit(struct intel_super *super, |
1130 | struct imsm_dev *dev); | |
1e5c6983 | 1131 | |
c47b0ff6 AK |
1132 | static void print_imsm_dev(struct intel_super *super, |
1133 | struct imsm_dev *dev, | |
1134 | char *uuid, | |
1135 | int disk_idx) | |
cdddbdbc DW |
1136 | { |
1137 | __u64 sz; | |
0d80bb2f | 1138 | int slot, i; |
238c0a71 AK |
1139 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
1140 | struct imsm_map *map2 = get_imsm_map(dev, MAP_1); | |
b10b37b8 | 1141 | __u32 ord; |
cdddbdbc DW |
1142 | |
1143 | printf("\n"); | |
1e7bc0ed | 1144 | printf("[%.16s]:\n", dev->volume); |
44470971 | 1145 | printf(" UUID : %s\n", uuid); |
dd8bcb3b AK |
1146 | printf(" RAID Level : %d", get_imsm_raid_level(map)); |
1147 | if (map2) | |
1148 | printf(" <-- %d", get_imsm_raid_level(map2)); | |
1149 | printf("\n"); | |
1150 | printf(" Members : %d", map->num_members); | |
1151 | if (map2) | |
1152 | printf(" <-- %d", map2->num_members); | |
1153 | printf("\n"); | |
0d80bb2f DW |
1154 | printf(" Slots : ["); |
1155 | for (i = 0; i < map->num_members; i++) { | |
238c0a71 | 1156 | ord = get_imsm_ord_tbl_ent(dev, i, MAP_0); |
0d80bb2f DW |
1157 | printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U"); |
1158 | } | |
dd8bcb3b AK |
1159 | printf("]"); |
1160 | if (map2) { | |
1161 | printf(" <-- ["); | |
1162 | for (i = 0; i < map2->num_members; i++) { | |
238c0a71 | 1163 | ord = get_imsm_ord_tbl_ent(dev, i, MAP_1); |
dd8bcb3b AK |
1164 | printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U"); |
1165 | } | |
1166 | printf("]"); | |
1167 | } | |
1168 | printf("\n"); | |
7095bccb AK |
1169 | printf(" Failed disk : "); |
1170 | if (map->failed_disk_num == 0xff) | |
1171 | printf("none"); | |
1172 | else | |
1173 | printf("%i", map->failed_disk_num); | |
1174 | printf("\n"); | |
620b1713 DW |
1175 | slot = get_imsm_disk_slot(map, disk_idx); |
1176 | if (slot >= 0) { | |
238c0a71 | 1177 | ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X); |
b10b37b8 DW |
1178 | printf(" This Slot : %d%s\n", slot, |
1179 | ord & IMSM_ORD_REBUILD ? " (out-of-sync)" : ""); | |
1180 | } else | |
cdddbdbc DW |
1181 | printf(" This Slot : ?\n"); |
1182 | sz = __le32_to_cpu(dev->size_high); | |
1183 | sz <<= 32; | |
1184 | sz += __le32_to_cpu(dev->size_low); | |
1185 | printf(" Array Size : %llu%s\n", (unsigned long long)sz, | |
1186 | human_size(sz * 512)); | |
5551b113 | 1187 | sz = blocks_per_member(map); |
cdddbdbc DW |
1188 | printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz, |
1189 | human_size(sz * 512)); | |
5551b113 CA |
1190 | printf(" Sector Offset : %llu\n", |
1191 | pba_of_lba0(map)); | |
1192 | printf(" Num Stripes : %llu\n", | |
1193 | num_data_stripes(map)); | |
dd8bcb3b | 1194 | printf(" Chunk Size : %u KiB", |
cdddbdbc | 1195 | __le16_to_cpu(map->blocks_per_strip) / 2); |
dd8bcb3b AK |
1196 | if (map2) |
1197 | printf(" <-- %u KiB", | |
1198 | __le16_to_cpu(map2->blocks_per_strip) / 2); | |
1199 | printf("\n"); | |
cdddbdbc | 1200 | printf(" Reserved : %d\n", __le32_to_cpu(dev->reserved_blocks)); |
8655a7b1 | 1201 | printf(" Migrate State : "); |
1484e727 DW |
1202 | if (dev->vol.migr_state) { |
1203 | if (migr_type(dev) == MIGR_INIT) | |
8655a7b1 | 1204 | printf("initialize\n"); |
1484e727 | 1205 | else if (migr_type(dev) == MIGR_REBUILD) |
8655a7b1 | 1206 | printf("rebuild\n"); |
1484e727 | 1207 | else if (migr_type(dev) == MIGR_VERIFY) |
8655a7b1 | 1208 | printf("check\n"); |
1484e727 | 1209 | else if (migr_type(dev) == MIGR_GEN_MIGR) |
8655a7b1 | 1210 | printf("general migration\n"); |
1484e727 | 1211 | else if (migr_type(dev) == MIGR_STATE_CHANGE) |
8655a7b1 | 1212 | printf("state change\n"); |
1484e727 | 1213 | else if (migr_type(dev) == MIGR_REPAIR) |
8655a7b1 | 1214 | printf("repair\n"); |
1484e727 | 1215 | else |
8655a7b1 DW |
1216 | printf("<unknown:%d>\n", migr_type(dev)); |
1217 | } else | |
1218 | printf("idle\n"); | |
3393c6af DW |
1219 | printf(" Map State : %s", map_state_str[map->map_state]); |
1220 | if (dev->vol.migr_state) { | |
238c0a71 | 1221 | struct imsm_map *map = get_imsm_map(dev, MAP_1); |
1e5c6983 | 1222 | |
b10b37b8 | 1223 | printf(" <-- %s", map_state_str[map->map_state]); |
464d40e8 LD |
1224 | printf("\n Checkpoint : %u ", |
1225 | __le32_to_cpu(dev->vol.curr_migr_unit)); | |
3136abe5 | 1226 | if ((is_gen_migration(dev)) && ((slot > 1) || (slot < 0))) |
464d40e8 LD |
1227 | printf("(N/A)"); |
1228 | else | |
1229 | printf("(%llu)", (unsigned long long) | |
1230 | blocks_per_migr_unit(super, dev)); | |
3393c6af DW |
1231 | } |
1232 | printf("\n"); | |
cdddbdbc | 1233 | printf(" Dirty State : %s\n", dev->vol.dirty ? "dirty" : "clean"); |
cdddbdbc DW |
1234 | } |
1235 | ||
0ec1f4e8 | 1236 | static void print_imsm_disk(struct imsm_disk *disk, int index, __u32 reserved) |
cdddbdbc | 1237 | { |
1f24f035 | 1238 | char str[MAX_RAID_SERIAL_LEN + 1]; |
cdddbdbc DW |
1239 | __u64 sz; |
1240 | ||
0ec1f4e8 | 1241 | if (index < -1 || !disk) |
e9d82038 DW |
1242 | return; |
1243 | ||
cdddbdbc | 1244 | printf("\n"); |
1f24f035 | 1245 | snprintf(str, MAX_RAID_SERIAL_LEN + 1, "%s", disk->serial); |
0ec1f4e8 DW |
1246 | if (index >= 0) |
1247 | printf(" Disk%02d Serial : %s\n", index, str); | |
1248 | else | |
1249 | printf(" Disk Serial : %s\n", str); | |
25ed7e59 DW |
1250 | printf(" State :%s%s%s\n", is_spare(disk) ? " spare" : "", |
1251 | is_configured(disk) ? " active" : "", | |
1252 | is_failed(disk) ? " failed" : ""); | |
cdddbdbc | 1253 | printf(" Id : %08x\n", __le32_to_cpu(disk->scsi_id)); |
5551b113 | 1254 | sz = total_blocks(disk) - reserved; |
cdddbdbc DW |
1255 | printf(" Usable Size : %llu%s\n", (unsigned long long)sz, |
1256 | human_size(sz * 512)); | |
1257 | } | |
1258 | ||
520e69e2 AK |
1259 | void examine_migr_rec_imsm(struct intel_super *super) |
1260 | { | |
1261 | struct migr_record *migr_rec = super->migr_rec; | |
1262 | struct imsm_super *mpb = super->anchor; | |
1263 | int i; | |
1264 | ||
1265 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
1266 | struct imsm_dev *dev = __get_imsm_dev(mpb, i); | |
3136abe5 | 1267 | struct imsm_map *map; |
b4ab44d8 | 1268 | int slot = -1; |
3136abe5 | 1269 | |
520e69e2 AK |
1270 | if (is_gen_migration(dev) == 0) |
1271 | continue; | |
1272 | ||
1273 | printf("\nMigration Record Information:"); | |
3136abe5 | 1274 | |
44bfe6df AK |
1275 | /* first map under migration */ |
1276 | map = get_imsm_map(dev, MAP_0); | |
3136abe5 AK |
1277 | if (map) |
1278 | slot = get_imsm_disk_slot(map, super->disks->index); | |
1279 | if ((map == NULL) || (slot > 1) || (slot < 0)) { | |
520e69e2 AK |
1280 | printf(" Empty\n "); |
1281 | printf("Examine one of first two disks in array\n"); | |
1282 | break; | |
1283 | } | |
1284 | printf("\n Status : "); | |
1285 | if (__le32_to_cpu(migr_rec->rec_status) == UNIT_SRC_NORMAL) | |
1286 | printf("Normal\n"); | |
1287 | else | |
1288 | printf("Contains Data\n"); | |
1289 | printf(" Current Unit : %u\n", | |
1290 | __le32_to_cpu(migr_rec->curr_migr_unit)); | |
1291 | printf(" Family : %u\n", | |
1292 | __le32_to_cpu(migr_rec->family_num)); | |
1293 | printf(" Ascending : %u\n", | |
1294 | __le32_to_cpu(migr_rec->ascending_migr)); | |
1295 | printf(" Blocks Per Unit : %u\n", | |
1296 | __le32_to_cpu(migr_rec->blocks_per_unit)); | |
1297 | printf(" Dest. Depth Per Unit : %u\n", | |
1298 | __le32_to_cpu(migr_rec->dest_depth_per_unit)); | |
1299 | printf(" Checkpoint Area pba : %u\n", | |
1300 | __le32_to_cpu(migr_rec->ckpt_area_pba)); | |
1301 | printf(" First member lba : %u\n", | |
1302 | __le32_to_cpu(migr_rec->dest_1st_member_lba)); | |
1303 | printf(" Total Number of Units : %u\n", | |
1304 | __le32_to_cpu(migr_rec->num_migr_units)); | |
1305 | printf(" Size of volume : %u\n", | |
1306 | __le32_to_cpu(migr_rec->post_migr_vol_cap)); | |
1307 | printf(" Expansion space for LBA64 : %u\n", | |
1308 | __le32_to_cpu(migr_rec->post_migr_vol_cap_hi)); | |
1309 | printf(" Record was read from : %u\n", | |
1310 | __le32_to_cpu(migr_rec->ckpt_read_disk_num)); | |
1311 | ||
1312 | break; | |
1313 | } | |
1314 | } | |
9e2d750d | 1315 | #endif /* MDASSEMBLE */ |
19482bcc AK |
1316 | /******************************************************************************* |
1317 | * function: imsm_check_attributes | |
1318 | * Description: Function checks if features represented by attributes flags | |
1011e834 | 1319 | * are supported by mdadm. |
19482bcc AK |
1320 | * Parameters: |
1321 | * attributes - Attributes read from metadata | |
1322 | * Returns: | |
1011e834 N |
1323 | * 0 - passed attributes contains unsupported features flags |
1324 | * 1 - all features are supported | |
19482bcc AK |
1325 | ******************************************************************************/ |
1326 | static int imsm_check_attributes(__u32 attributes) | |
1327 | { | |
1328 | int ret_val = 1; | |
418f9b36 N |
1329 | __u32 not_supported = MPB_ATTRIB_SUPPORTED^0xffffffff; |
1330 | ||
1331 | not_supported &= ~MPB_ATTRIB_IGNORED; | |
19482bcc AK |
1332 | |
1333 | not_supported &= attributes; | |
1334 | if (not_supported) { | |
e7b84f9d | 1335 | pr_err("(IMSM): Unsupported attributes : %x\n", |
418f9b36 | 1336 | (unsigned)__le32_to_cpu(not_supported)); |
19482bcc AK |
1337 | if (not_supported & MPB_ATTRIB_CHECKSUM_VERIFY) { |
1338 | dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n"); | |
1339 | not_supported ^= MPB_ATTRIB_CHECKSUM_VERIFY; | |
1340 | } | |
1341 | if (not_supported & MPB_ATTRIB_2TB) { | |
1342 | dprintf("\t\tMPB_ATTRIB_2TB\n"); | |
1343 | not_supported ^= MPB_ATTRIB_2TB; | |
1344 | } | |
1345 | if (not_supported & MPB_ATTRIB_RAID0) { | |
1346 | dprintf("\t\tMPB_ATTRIB_RAID0\n"); | |
1347 | not_supported ^= MPB_ATTRIB_RAID0; | |
1348 | } | |
1349 | if (not_supported & MPB_ATTRIB_RAID1) { | |
1350 | dprintf("\t\tMPB_ATTRIB_RAID1\n"); | |
1351 | not_supported ^= MPB_ATTRIB_RAID1; | |
1352 | } | |
1353 | if (not_supported & MPB_ATTRIB_RAID10) { | |
1354 | dprintf("\t\tMPB_ATTRIB_RAID10\n"); | |
1355 | not_supported ^= MPB_ATTRIB_RAID10; | |
1356 | } | |
1357 | if (not_supported & MPB_ATTRIB_RAID1E) { | |
1358 | dprintf("\t\tMPB_ATTRIB_RAID1E\n"); | |
1359 | not_supported ^= MPB_ATTRIB_RAID1E; | |
1360 | } | |
1361 | if (not_supported & MPB_ATTRIB_RAID5) { | |
1362 | dprintf("\t\tMPB_ATTRIB_RAID5\n"); | |
1363 | not_supported ^= MPB_ATTRIB_RAID5; | |
1364 | } | |
1365 | if (not_supported & MPB_ATTRIB_RAIDCNG) { | |
1366 | dprintf("\t\tMPB_ATTRIB_RAIDCNG\n"); | |
1367 | not_supported ^= MPB_ATTRIB_RAIDCNG; | |
1368 | } | |
1369 | if (not_supported & MPB_ATTRIB_BBM) { | |
1370 | dprintf("\t\tMPB_ATTRIB_BBM\n"); | |
1371 | not_supported ^= MPB_ATTRIB_BBM; | |
1372 | } | |
1373 | if (not_supported & MPB_ATTRIB_CHECKSUM_VERIFY) { | |
1374 | dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n"); | |
1375 | not_supported ^= MPB_ATTRIB_CHECKSUM_VERIFY; | |
1376 | } | |
1377 | if (not_supported & MPB_ATTRIB_EXP_STRIPE_SIZE) { | |
1378 | dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n"); | |
1379 | not_supported ^= MPB_ATTRIB_EXP_STRIPE_SIZE; | |
1380 | } | |
1381 | if (not_supported & MPB_ATTRIB_2TB_DISK) { | |
1382 | dprintf("\t\tMPB_ATTRIB_2TB_DISK\n"); | |
1383 | not_supported ^= MPB_ATTRIB_2TB_DISK; | |
1384 | } | |
1385 | if (not_supported & MPB_ATTRIB_NEVER_USE2) { | |
1386 | dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n"); | |
1387 | not_supported ^= MPB_ATTRIB_NEVER_USE2; | |
1388 | } | |
1389 | if (not_supported & MPB_ATTRIB_NEVER_USE) { | |
1390 | dprintf("\t\tMPB_ATTRIB_NEVER_USE\n"); | |
1391 | not_supported ^= MPB_ATTRIB_NEVER_USE; | |
1392 | } | |
1393 | ||
1394 | if (not_supported) | |
1ade5cc1 | 1395 | dprintf("(IMSM): Unknown attributes : %x\n", not_supported); |
19482bcc AK |
1396 | |
1397 | ret_val = 0; | |
1398 | } | |
1399 | ||
1400 | return ret_val; | |
1401 | } | |
1402 | ||
9e2d750d | 1403 | #ifndef MDASSEMBLE |
a5d85af7 | 1404 | static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map); |
44470971 | 1405 | |
cdddbdbc DW |
1406 | static void examine_super_imsm(struct supertype *st, char *homehost) |
1407 | { | |
1408 | struct intel_super *super = st->sb; | |
949c47a0 | 1409 | struct imsm_super *mpb = super->anchor; |
cdddbdbc DW |
1410 | char str[MAX_SIGNATURE_LENGTH]; |
1411 | int i; | |
27fd6274 DW |
1412 | struct mdinfo info; |
1413 | char nbuf[64]; | |
cdddbdbc | 1414 | __u32 sum; |
14e8215b | 1415 | __u32 reserved = imsm_reserved_sectors(super, super->disks); |
94827db3 | 1416 | struct dl *dl; |
27fd6274 | 1417 | |
cdddbdbc DW |
1418 | snprintf(str, MPB_SIG_LEN, "%s", mpb->sig); |
1419 | printf(" Magic : %s\n", str); | |
1420 | snprintf(str, strlen(MPB_VERSION_RAID0), "%s", get_imsm_version(mpb)); | |
1421 | printf(" Version : %s\n", get_imsm_version(mpb)); | |
148acb7b | 1422 | printf(" Orig Family : %08x\n", __le32_to_cpu(mpb->orig_family_num)); |
cdddbdbc DW |
1423 | printf(" Family : %08x\n", __le32_to_cpu(mpb->family_num)); |
1424 | printf(" Generation : %08x\n", __le32_to_cpu(mpb->generation_num)); | |
19482bcc AK |
1425 | printf(" Attributes : "); |
1426 | if (imsm_check_attributes(mpb->attributes)) | |
1427 | printf("All supported\n"); | |
1428 | else | |
1429 | printf("not supported\n"); | |
a5d85af7 | 1430 | getinfo_super_imsm(st, &info, NULL); |
ae2bfd4e | 1431 | fname_from_uuid(st, &info, nbuf, ':'); |
27fd6274 | 1432 | printf(" UUID : %s\n", nbuf + 5); |
cdddbdbc DW |
1433 | sum = __le32_to_cpu(mpb->check_sum); |
1434 | printf(" Checksum : %08x %s\n", sum, | |
949c47a0 | 1435 | __gen_imsm_checksum(mpb) == sum ? "correct" : "incorrect"); |
87eb16df | 1436 | printf(" MPB Sectors : %d\n", mpb_sectors(mpb)); |
cdddbdbc DW |
1437 | printf(" Disks : %d\n", mpb->num_disks); |
1438 | printf(" RAID Devices : %d\n", mpb->num_raid_devs); | |
0ec1f4e8 | 1439 | print_imsm_disk(__get_imsm_disk(mpb, super->disks->index), super->disks->index, reserved); |
604b746f JD |
1440 | if (super->bbm_log) { |
1441 | struct bbm_log *log = super->bbm_log; | |
1442 | ||
1443 | printf("\n"); | |
1444 | printf("Bad Block Management Log:\n"); | |
1445 | printf(" Log Size : %d\n", __le32_to_cpu(mpb->bbm_log_size)); | |
1446 | printf(" Signature : %x\n", __le32_to_cpu(log->signature)); | |
1447 | printf(" Entry Count : %d\n", __le32_to_cpu(log->entry_count)); | |
1448 | printf(" Spare Blocks : %d\n", __le32_to_cpu(log->reserved_spare_block_count)); | |
13a3b65d N |
1449 | printf(" First Spare : %llx\n", |
1450 | (unsigned long long) __le64_to_cpu(log->first_spare_lba)); | |
604b746f | 1451 | } |
44470971 DW |
1452 | for (i = 0; i < mpb->num_raid_devs; i++) { |
1453 | struct mdinfo info; | |
1454 | struct imsm_dev *dev = __get_imsm_dev(mpb, i); | |
1455 | ||
1456 | super->current_vol = i; | |
a5d85af7 | 1457 | getinfo_super_imsm(st, &info, NULL); |
ae2bfd4e | 1458 | fname_from_uuid(st, &info, nbuf, ':'); |
c47b0ff6 | 1459 | print_imsm_dev(super, dev, nbuf + 5, super->disks->index); |
44470971 | 1460 | } |
cdddbdbc DW |
1461 | for (i = 0; i < mpb->num_disks; i++) { |
1462 | if (i == super->disks->index) | |
1463 | continue; | |
0ec1f4e8 | 1464 | print_imsm_disk(__get_imsm_disk(mpb, i), i, reserved); |
cdddbdbc | 1465 | } |
94827db3 | 1466 | |
0ec1f4e8 DW |
1467 | for (dl = super->disks; dl; dl = dl->next) |
1468 | if (dl->index == -1) | |
1469 | print_imsm_disk(&dl->disk, -1, reserved); | |
520e69e2 AK |
1470 | |
1471 | examine_migr_rec_imsm(super); | |
cdddbdbc DW |
1472 | } |
1473 | ||
061f2c6a | 1474 | static void brief_examine_super_imsm(struct supertype *st, int verbose) |
cdddbdbc | 1475 | { |
27fd6274 | 1476 | /* We just write a generic IMSM ARRAY entry */ |
ff54de6e N |
1477 | struct mdinfo info; |
1478 | char nbuf[64]; | |
1e7bc0ed | 1479 | struct intel_super *super = st->sb; |
1e7bc0ed | 1480 | |
0d5a423f DW |
1481 | if (!super->anchor->num_raid_devs) { |
1482 | printf("ARRAY metadata=imsm\n"); | |
1e7bc0ed | 1483 | return; |
0d5a423f | 1484 | } |
ff54de6e | 1485 | |
a5d85af7 | 1486 | getinfo_super_imsm(st, &info, NULL); |
4737ae25 N |
1487 | fname_from_uuid(st, &info, nbuf, ':'); |
1488 | printf("ARRAY metadata=imsm UUID=%s\n", nbuf + 5); | |
1489 | } | |
1490 | ||
1491 | static void brief_examine_subarrays_imsm(struct supertype *st, int verbose) | |
1492 | { | |
1493 | /* We just write a generic IMSM ARRAY entry */ | |
1494 | struct mdinfo info; | |
1495 | char nbuf[64]; | |
1496 | char nbuf1[64]; | |
1497 | struct intel_super *super = st->sb; | |
1498 | int i; | |
1499 | ||
1500 | if (!super->anchor->num_raid_devs) | |
1501 | return; | |
1502 | ||
a5d85af7 | 1503 | getinfo_super_imsm(st, &info, NULL); |
ae2bfd4e | 1504 | fname_from_uuid(st, &info, nbuf, ':'); |
1e7bc0ed DW |
1505 | for (i = 0; i < super->anchor->num_raid_devs; i++) { |
1506 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
1507 | ||
1508 | super->current_vol = i; | |
a5d85af7 | 1509 | getinfo_super_imsm(st, &info, NULL); |
ae2bfd4e | 1510 | fname_from_uuid(st, &info, nbuf1, ':'); |
1124b3cf | 1511 | printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n", |
cf8de691 | 1512 | dev->volume, nbuf + 5, i, nbuf1 + 5); |
1e7bc0ed | 1513 | } |
cdddbdbc DW |
1514 | } |
1515 | ||
9d84c8ea DW |
1516 | static void export_examine_super_imsm(struct supertype *st) |
1517 | { | |
1518 | struct intel_super *super = st->sb; | |
1519 | struct imsm_super *mpb = super->anchor; | |
1520 | struct mdinfo info; | |
1521 | char nbuf[64]; | |
1522 | ||
a5d85af7 | 1523 | getinfo_super_imsm(st, &info, NULL); |
9d84c8ea DW |
1524 | fname_from_uuid(st, &info, nbuf, ':'); |
1525 | printf("MD_METADATA=imsm\n"); | |
1526 | printf("MD_LEVEL=container\n"); | |
1527 | printf("MD_UUID=%s\n", nbuf+5); | |
1528 | printf("MD_DEVICES=%u\n", mpb->num_disks); | |
1529 | } | |
1530 | ||
74db60b0 N |
1531 | static int copy_metadata_imsm(struct supertype *st, int from, int to) |
1532 | { | |
1533 | /* The second last 512byte sector of the device contains | |
1534 | * the "struct imsm_super" metadata. | |
1535 | * This contains mpb_size which is the size in bytes of the | |
1536 | * extended metadata. This is located immediately before | |
1537 | * the imsm_super. | |
1538 | * We want to read all that, plus the last sector which | |
1539 | * may contain a migration record, and write it all | |
1540 | * to the target. | |
1541 | */ | |
1542 | void *buf; | |
1543 | unsigned long long dsize, offset; | |
1544 | int sectors; | |
1545 | struct imsm_super *sb; | |
1546 | int written = 0; | |
1547 | ||
1548 | if (posix_memalign(&buf, 4096, 4096) != 0) | |
1549 | return 1; | |
1550 | ||
1551 | if (!get_dev_size(from, NULL, &dsize)) | |
1552 | goto err; | |
1553 | ||
1554 | if (lseek64(from, dsize-1024, 0) < 0) | |
1555 | goto err; | |
1556 | if (read(from, buf, 512) != 512) | |
1557 | goto err; | |
1558 | sb = buf; | |
1559 | if (strncmp((char*)sb->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) | |
1560 | goto err; | |
1561 | ||
1562 | sectors = mpb_sectors(sb) + 2; | |
1563 | offset = dsize - sectors * 512; | |
1564 | if (lseek64(from, offset, 0) < 0 || | |
1565 | lseek64(to, offset, 0) < 0) | |
1566 | goto err; | |
1567 | while (written < sectors * 512) { | |
1568 | int n = sectors*512 - written; | |
1569 | if (n > 4096) | |
1570 | n = 4096; | |
1571 | if (read(from, buf, n) != n) | |
1572 | goto err; | |
1573 | if (write(to, buf, n) != n) | |
1574 | goto err; | |
1575 | written += n; | |
1576 | } | |
1577 | free(buf); | |
1578 | return 0; | |
1579 | err: | |
1580 | free(buf); | |
1581 | return 1; | |
1582 | } | |
1583 | ||
cdddbdbc DW |
1584 | static void detail_super_imsm(struct supertype *st, char *homehost) |
1585 | { | |
3ebe00a1 DW |
1586 | struct mdinfo info; |
1587 | char nbuf[64]; | |
1588 | ||
a5d85af7 | 1589 | getinfo_super_imsm(st, &info, NULL); |
ae2bfd4e | 1590 | fname_from_uuid(st, &info, nbuf, ':'); |
3ebe00a1 | 1591 | printf("\n UUID : %s\n", nbuf + 5); |
cdddbdbc DW |
1592 | } |
1593 | ||
1594 | static void brief_detail_super_imsm(struct supertype *st) | |
1595 | { | |
ff54de6e N |
1596 | struct mdinfo info; |
1597 | char nbuf[64]; | |
a5d85af7 | 1598 | getinfo_super_imsm(st, &info, NULL); |
ae2bfd4e | 1599 | fname_from_uuid(st, &info, nbuf, ':'); |
ff54de6e | 1600 | printf(" UUID=%s", nbuf + 5); |
cdddbdbc | 1601 | } |
d665cc31 DW |
1602 | |
1603 | static int imsm_read_serial(int fd, char *devname, __u8 *serial); | |
1604 | static void fd2devname(int fd, char *name); | |
1605 | ||
120dc887 | 1606 | static int ahci_enumerate_ports(const char *hba_path, int port_count, int host_base, int verbose) |
d665cc31 | 1607 | { |
120dc887 LM |
1608 | /* dump an unsorted list of devices attached to AHCI Intel storage |
1609 | * controller, as well as non-connected ports | |
d665cc31 DW |
1610 | */ |
1611 | int hba_len = strlen(hba_path) + 1; | |
1612 | struct dirent *ent; | |
1613 | DIR *dir; | |
1614 | char *path = NULL; | |
1615 | int err = 0; | |
1616 | unsigned long port_mask = (1 << port_count) - 1; | |
1617 | ||
f21e18ca | 1618 | if (port_count > (int)sizeof(port_mask) * 8) { |
ba728be7 | 1619 | if (verbose > 0) |
e7b84f9d | 1620 | pr_err("port_count %d out of range\n", port_count); |
d665cc31 DW |
1621 | return 2; |
1622 | } | |
1623 | ||
1624 | /* scroll through /sys/dev/block looking for devices attached to | |
1625 | * this hba | |
1626 | */ | |
1627 | dir = opendir("/sys/dev/block"); | |
1a6dd6b9 PB |
1628 | if (!dir) |
1629 | return 1; | |
1630 | ||
1631 | for (ent = readdir(dir); ent; ent = readdir(dir)) { | |
d665cc31 DW |
1632 | int fd; |
1633 | char model[64]; | |
1634 | char vendor[64]; | |
1635 | char buf[1024]; | |
1636 | int major, minor; | |
1637 | char *device; | |
1638 | char *c; | |
1639 | int port; | |
1640 | int type; | |
1641 | ||
1642 | if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2) | |
1643 | continue; | |
1644 | path = devt_to_devpath(makedev(major, minor)); | |
1645 | if (!path) | |
1646 | continue; | |
1647 | if (!path_attached_to_hba(path, hba_path)) { | |
1648 | free(path); | |
1649 | path = NULL; | |
1650 | continue; | |
1651 | } | |
1652 | ||
1653 | /* retrieve the scsi device type */ | |
1654 | if (asprintf(&device, "/sys/dev/block/%d:%d/device/xxxxxxx", major, minor) < 0) { | |
ba728be7 | 1655 | if (verbose > 0) |
e7b84f9d | 1656 | pr_err("failed to allocate 'device'\n"); |
d665cc31 DW |
1657 | err = 2; |
1658 | break; | |
1659 | } | |
1660 | sprintf(device, "/sys/dev/block/%d:%d/device/type", major, minor); | |
193b6c0b | 1661 | if (load_sys(device, buf, sizeof(buf)) != 0) { |
ba728be7 | 1662 | if (verbose > 0) |
e7b84f9d | 1663 | pr_err("failed to read device type for %s\n", |
d665cc31 DW |
1664 | path); |
1665 | err = 2; | |
1666 | free(device); | |
1667 | break; | |
1668 | } | |
1669 | type = strtoul(buf, NULL, 10); | |
1670 | ||
1671 | /* if it's not a disk print the vendor and model */ | |
1672 | if (!(type == 0 || type == 7 || type == 14)) { | |
1673 | vendor[0] = '\0'; | |
1674 | model[0] = '\0'; | |
1675 | sprintf(device, "/sys/dev/block/%d:%d/device/vendor", major, minor); | |
193b6c0b | 1676 | if (load_sys(device, buf, sizeof(buf)) == 0) { |
d665cc31 DW |
1677 | strncpy(vendor, buf, sizeof(vendor)); |
1678 | vendor[sizeof(vendor) - 1] = '\0'; | |
1679 | c = (char *) &vendor[sizeof(vendor) - 1]; | |
1680 | while (isspace(*c) || *c == '\0') | |
1681 | *c-- = '\0'; | |
1682 | ||
1683 | } | |
1684 | sprintf(device, "/sys/dev/block/%d:%d/device/model", major, minor); | |
193b6c0b | 1685 | if (load_sys(device, buf, sizeof(buf)) == 0) { |
d665cc31 DW |
1686 | strncpy(model, buf, sizeof(model)); |
1687 | model[sizeof(model) - 1] = '\0'; | |
1688 | c = (char *) &model[sizeof(model) - 1]; | |
1689 | while (isspace(*c) || *c == '\0') | |
1690 | *c-- = '\0'; | |
1691 | } | |
1692 | ||
1693 | if (vendor[0] && model[0]) | |
1694 | sprintf(buf, "%.64s %.64s", vendor, model); | |
1695 | else | |
1696 | switch (type) { /* numbers from hald/linux/device.c */ | |
1697 | case 1: sprintf(buf, "tape"); break; | |
1698 | case 2: sprintf(buf, "printer"); break; | |
1699 | case 3: sprintf(buf, "processor"); break; | |
1700 | case 4: | |
1701 | case 5: sprintf(buf, "cdrom"); break; | |
1702 | case 6: sprintf(buf, "scanner"); break; | |
1703 | case 8: sprintf(buf, "media_changer"); break; | |
1704 | case 9: sprintf(buf, "comm"); break; | |
1705 | case 12: sprintf(buf, "raid"); break; | |
1706 | default: sprintf(buf, "unknown"); | |
1707 | } | |
1708 | } else | |
1709 | buf[0] = '\0'; | |
1710 | free(device); | |
1711 | ||
1712 | /* chop device path to 'host%d' and calculate the port number */ | |
1713 | c = strchr(&path[hba_len], '/'); | |
4e5e717d | 1714 | if (!c) { |
ba728be7 | 1715 | if (verbose > 0) |
e7b84f9d | 1716 | pr_err("%s - invalid path name\n", path + hba_len); |
4e5e717d AW |
1717 | err = 2; |
1718 | break; | |
1719 | } | |
d665cc31 | 1720 | *c = '\0'; |
0858eccf AP |
1721 | if ((sscanf(&path[hba_len], "ata%d", &port) == 1) || |
1722 | ((sscanf(&path[hba_len], "host%d", &port) == 1))) | |
d665cc31 DW |
1723 | port -= host_base; |
1724 | else { | |
ba728be7 | 1725 | if (verbose > 0) { |
d665cc31 | 1726 | *c = '/'; /* repair the full string */ |
e7b84f9d | 1727 | pr_err("failed to determine port number for %s\n", |
d665cc31 DW |
1728 | path); |
1729 | } | |
1730 | err = 2; | |
1731 | break; | |
1732 | } | |
1733 | ||
1734 | /* mark this port as used */ | |
1735 | port_mask &= ~(1 << port); | |
1736 | ||
1737 | /* print out the device information */ | |
1738 | if (buf[0]) { | |
1739 | printf(" Port%d : - non-disk device (%s) -\n", port, buf); | |
1740 | continue; | |
1741 | } | |
1742 | ||
1743 | fd = dev_open(ent->d_name, O_RDONLY); | |
1744 | if (fd < 0) | |
1745 | printf(" Port%d : - disk info unavailable -\n", port); | |
1746 | else { | |
1747 | fd2devname(fd, buf); | |
1748 | printf(" Port%d : %s", port, buf); | |
1749 | if (imsm_read_serial(fd, NULL, (__u8 *) buf) == 0) | |
664d5325 | 1750 | printf(" (%.*s)\n", MAX_RAID_SERIAL_LEN, buf); |
d665cc31 | 1751 | else |
664d5325 | 1752 | printf(" ()\n"); |
4dab422a | 1753 | close(fd); |
d665cc31 | 1754 | } |
d665cc31 DW |
1755 | free(path); |
1756 | path = NULL; | |
1757 | } | |
1758 | if (path) | |
1759 | free(path); | |
1760 | if (dir) | |
1761 | closedir(dir); | |
1762 | if (err == 0) { | |
1763 | int i; | |
1764 | ||
1765 | for (i = 0; i < port_count; i++) | |
1766 | if (port_mask & (1 << i)) | |
1767 | printf(" Port%d : - no device attached -\n", i); | |
1768 | } | |
1769 | ||
1770 | return err; | |
1771 | } | |
1772 | ||
b5eece69 | 1773 | static int print_vmd_attached_devs(struct sys_dev *hba) |
60f0f54d PB |
1774 | { |
1775 | struct dirent *ent; | |
1776 | DIR *dir; | |
1777 | char path[292]; | |
1778 | char link[256]; | |
1779 | char *c, *rp; | |
1780 | ||
1781 | if (hba->type != SYS_DEV_VMD) | |
b5eece69 | 1782 | return 1; |
60f0f54d PB |
1783 | |
1784 | /* scroll through /sys/dev/block looking for devices attached to | |
1785 | * this hba | |
1786 | */ | |
1787 | dir = opendir("/sys/bus/pci/drivers/nvme"); | |
b9135011 | 1788 | if (!dir) |
b5eece69 | 1789 | return 1; |
b9135011 JS |
1790 | |
1791 | for (ent = readdir(dir); ent; ent = readdir(dir)) { | |
60f0f54d PB |
1792 | int n; |
1793 | ||
1794 | /* is 'ent' a device? check that the 'subsystem' link exists and | |
1795 | * that its target matches 'bus' | |
1796 | */ | |
1797 | sprintf(path, "/sys/bus/pci/drivers/nvme/%s/subsystem", | |
1798 | ent->d_name); | |
1799 | n = readlink(path, link, sizeof(link)); | |
1800 | if (n < 0 || n >= (int)sizeof(link)) | |
1801 | continue; | |
1802 | link[n] = '\0'; | |
1803 | c = strrchr(link, '/'); | |
1804 | if (!c) | |
1805 | continue; | |
1806 | if (strncmp("pci", c+1, strlen("pci")) != 0) | |
1807 | continue; | |
1808 | ||
1809 | sprintf(path, "/sys/bus/pci/drivers/nvme/%s", ent->d_name); | |
1810 | /* if not a intel NVMe - skip it*/ | |
1811 | if (devpath_to_vendor(path) != 0x8086) | |
1812 | continue; | |
1813 | ||
1814 | rp = realpath(path, NULL); | |
1815 | if (!rp) | |
1816 | continue; | |
1817 | ||
1818 | if (path_attached_to_hba(rp, hba->path)) { | |
1819 | printf(" NVMe under VMD : %s\n", rp); | |
1820 | } | |
1821 | free(rp); | |
1822 | } | |
1823 | ||
b9135011 | 1824 | closedir(dir); |
b5eece69 | 1825 | return 0; |
60f0f54d PB |
1826 | } |
1827 | ||
120dc887 LM |
1828 | static void print_found_intel_controllers(struct sys_dev *elem) |
1829 | { | |
1830 | for (; elem; elem = elem->next) { | |
e7b84f9d | 1831 | pr_err("found Intel(R) "); |
120dc887 LM |
1832 | if (elem->type == SYS_DEV_SATA) |
1833 | fprintf(stderr, "SATA "); | |
155cbb4c LM |
1834 | else if (elem->type == SYS_DEV_SAS) |
1835 | fprintf(stderr, "SAS "); | |
0858eccf AP |
1836 | else if (elem->type == SYS_DEV_NVME) |
1837 | fprintf(stderr, "NVMe "); | |
60f0f54d PB |
1838 | |
1839 | if (elem->type == SYS_DEV_VMD) | |
1840 | fprintf(stderr, "VMD domain"); | |
1841 | else | |
1842 | fprintf(stderr, "RAID controller"); | |
1843 | ||
120dc887 LM |
1844 | if (elem->pci_id) |
1845 | fprintf(stderr, " at %s", elem->pci_id); | |
1846 | fprintf(stderr, ".\n"); | |
1847 | } | |
1848 | fflush(stderr); | |
1849 | } | |
1850 | ||
120dc887 LM |
1851 | static int ahci_get_port_count(const char *hba_path, int *port_count) |
1852 | { | |
1853 | struct dirent *ent; | |
1854 | DIR *dir; | |
1855 | int host_base = -1; | |
1856 | ||
1857 | *port_count = 0; | |
1858 | if ((dir = opendir(hba_path)) == NULL) | |
1859 | return -1; | |
1860 | ||
1861 | for (ent = readdir(dir); ent; ent = readdir(dir)) { | |
1862 | int host; | |
1863 | ||
0858eccf AP |
1864 | if ((sscanf(ent->d_name, "ata%d", &host) != 1) && |
1865 | ((sscanf(ent->d_name, "host%d", &host) != 1))) | |
120dc887 LM |
1866 | continue; |
1867 | if (*port_count == 0) | |
1868 | host_base = host; | |
1869 | else if (host < host_base) | |
1870 | host_base = host; | |
1871 | ||
1872 | if (host + 1 > *port_count + host_base) | |
1873 | *port_count = host + 1 - host_base; | |
1874 | } | |
1875 | closedir(dir); | |
1876 | return host_base; | |
1877 | } | |
1878 | ||
a891a3c2 LM |
1879 | static void print_imsm_capability(const struct imsm_orom *orom) |
1880 | { | |
0858eccf AP |
1881 | printf(" Platform : Intel(R) "); |
1882 | if (orom->capabilities == 0 && orom->driver_features == 0) | |
1883 | printf("Matrix Storage Manager\n"); | |
1884 | else | |
1885 | printf("Rapid Storage Technology%s\n", | |
1886 | imsm_orom_is_enterprise(orom) ? " enterprise" : ""); | |
1887 | if (orom->major_ver || orom->minor_ver || orom->hotfix_ver || orom->build) | |
1888 | printf(" Version : %d.%d.%d.%d\n", orom->major_ver, | |
1889 | orom->minor_ver, orom->hotfix_ver, orom->build); | |
a891a3c2 LM |
1890 | printf(" RAID Levels :%s%s%s%s%s\n", |
1891 | imsm_orom_has_raid0(orom) ? " raid0" : "", | |
1892 | imsm_orom_has_raid1(orom) ? " raid1" : "", | |
1893 | imsm_orom_has_raid1e(orom) ? " raid1e" : "", | |
1894 | imsm_orom_has_raid10(orom) ? " raid10" : "", | |
1895 | imsm_orom_has_raid5(orom) ? " raid5" : ""); | |
1896 | printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n", | |
1897 | imsm_orom_has_chunk(orom, 2) ? " 2k" : "", | |
1898 | imsm_orom_has_chunk(orom, 4) ? " 4k" : "", | |
1899 | imsm_orom_has_chunk(orom, 8) ? " 8k" : "", | |
1900 | imsm_orom_has_chunk(orom, 16) ? " 16k" : "", | |
1901 | imsm_orom_has_chunk(orom, 32) ? " 32k" : "", | |
1902 | imsm_orom_has_chunk(orom, 64) ? " 64k" : "", | |
1903 | imsm_orom_has_chunk(orom, 128) ? " 128k" : "", | |
1904 | imsm_orom_has_chunk(orom, 256) ? " 256k" : "", | |
1905 | imsm_orom_has_chunk(orom, 512) ? " 512k" : "", | |
1906 | imsm_orom_has_chunk(orom, 1024*1) ? " 1M" : "", | |
1907 | imsm_orom_has_chunk(orom, 1024*2) ? " 2M" : "", | |
1908 | imsm_orom_has_chunk(orom, 1024*4) ? " 4M" : "", | |
1909 | imsm_orom_has_chunk(orom, 1024*8) ? " 8M" : "", | |
1910 | imsm_orom_has_chunk(orom, 1024*16) ? " 16M" : "", | |
1911 | imsm_orom_has_chunk(orom, 1024*32) ? " 32M" : "", | |
1912 | imsm_orom_has_chunk(orom, 1024*64) ? " 64M" : ""); | |
29cd0821 CA |
1913 | printf(" 2TB volumes :%s supported\n", |
1914 | (orom->attr & IMSM_OROM_ATTR_2TB)?"":" not"); | |
1915 | printf(" 2TB disks :%s supported\n", | |
1916 | (orom->attr & IMSM_OROM_ATTR_2TB_DISK)?"":" not"); | |
0e7f69a8 | 1917 | printf(" Max Disks : %d\n", orom->tds); |
0858eccf AP |
1918 | printf(" Max Volumes : %d per array, %d per %s\n", |
1919 | orom->vpa, orom->vphba, | |
1920 | imsm_orom_is_nvme(orom) ? "platform" : "controller"); | |
a891a3c2 LM |
1921 | return; |
1922 | } | |
1923 | ||
e50cf220 MN |
1924 | static void print_imsm_capability_export(const struct imsm_orom *orom) |
1925 | { | |
1926 | printf("MD_FIRMWARE_TYPE=imsm\n"); | |
0858eccf AP |
1927 | if (orom->major_ver || orom->minor_ver || orom->hotfix_ver || orom->build) |
1928 | printf("IMSM_VERSION=%d.%d.%d.%d\n", orom->major_ver, orom->minor_ver, | |
1929 | orom->hotfix_ver, orom->build); | |
e50cf220 MN |
1930 | printf("IMSM_SUPPORTED_RAID_LEVELS=%s%s%s%s%s\n", |
1931 | imsm_orom_has_raid0(orom) ? "raid0 " : "", | |
1932 | imsm_orom_has_raid1(orom) ? "raid1 " : "", | |
1933 | imsm_orom_has_raid1e(orom) ? "raid1e " : "", | |
1934 | imsm_orom_has_raid5(orom) ? "raid10 " : "", | |
1935 | imsm_orom_has_raid10(orom) ? "raid5 " : ""); | |
1936 | printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n", | |
1937 | imsm_orom_has_chunk(orom, 2) ? "2k " : "", | |
1938 | imsm_orom_has_chunk(orom, 4) ? "4k " : "", | |
1939 | imsm_orom_has_chunk(orom, 8) ? "8k " : "", | |
1940 | imsm_orom_has_chunk(orom, 16) ? "16k " : "", | |
1941 | imsm_orom_has_chunk(orom, 32) ? "32k " : "", | |
1942 | imsm_orom_has_chunk(orom, 64) ? "64k " : "", | |
1943 | imsm_orom_has_chunk(orom, 128) ? "128k " : "", | |
1944 | imsm_orom_has_chunk(orom, 256) ? "256k " : "", | |
1945 | imsm_orom_has_chunk(orom, 512) ? "512k " : "", | |
1946 | imsm_orom_has_chunk(orom, 1024*1) ? "1M " : "", | |
1947 | imsm_orom_has_chunk(orom, 1024*2) ? "2M " : "", | |
1948 | imsm_orom_has_chunk(orom, 1024*4) ? "4M " : "", | |
1949 | imsm_orom_has_chunk(orom, 1024*8) ? "8M " : "", | |
1950 | imsm_orom_has_chunk(orom, 1024*16) ? "16M " : "", | |
1951 | imsm_orom_has_chunk(orom, 1024*32) ? "32M " : "", | |
1952 | imsm_orom_has_chunk(orom, 1024*64) ? "64M " : ""); | |
1953 | printf("IMSM_2TB_VOLUMES=%s\n",(orom->attr & IMSM_OROM_ATTR_2TB) ? "yes" : "no"); | |
1954 | printf("IMSM_2TB_DISKS=%s\n",(orom->attr & IMSM_OROM_ATTR_2TB_DISK) ? "yes" : "no"); | |
1955 | printf("IMSM_MAX_DISKS=%d\n",orom->tds); | |
1956 | printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom->vpa); | |
1957 | printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom->vphba); | |
1958 | } | |
1959 | ||
9eafa1de | 1960 | static int detail_platform_imsm(int verbose, int enumerate_only, char *controller_path) |
d665cc31 DW |
1961 | { |
1962 | /* There are two components to imsm platform support, the ahci SATA | |
1963 | * controller and the option-rom. To find the SATA controller we | |
1964 | * simply look in /sys/bus/pci/drivers/ahci to see if an ahci | |
1965 | * controller with the Intel vendor id is present. This approach | |
1966 | * allows mdadm to leverage the kernel's ahci detection logic, with the | |
1967 | * caveat that if ahci.ko is not loaded mdadm will not be able to | |
1968 | * detect platform raid capabilities. The option-rom resides in a | |
1969 | * platform "Adapter ROM". We scan for its signature to retrieve the | |
1970 | * platform capabilities. If raid support is disabled in the BIOS the | |
1971 | * option-rom capability structure will not be available. | |
1972 | */ | |
d665cc31 | 1973 | struct sys_dev *list, *hba; |
d665cc31 DW |
1974 | int host_base = 0; |
1975 | int port_count = 0; | |
9eafa1de | 1976 | int result=1; |
d665cc31 | 1977 | |
5615172f | 1978 | if (enumerate_only) { |
a891a3c2 | 1979 | if (check_env("IMSM_NO_PLATFORM")) |
5615172f | 1980 | return 0; |
a891a3c2 LM |
1981 | list = find_intel_devices(); |
1982 | if (!list) | |
1983 | return 2; | |
1984 | for (hba = list; hba; hba = hba->next) { | |
6b781d33 AP |
1985 | if (find_imsm_capability(hba)) { |
1986 | result = 0; | |
a891a3c2 LM |
1987 | break; |
1988 | } | |
9eafa1de | 1989 | else |
6b781d33 | 1990 | result = 2; |
a891a3c2 | 1991 | } |
a891a3c2 | 1992 | return result; |
5615172f DW |
1993 | } |
1994 | ||
155cbb4c LM |
1995 | list = find_intel_devices(); |
1996 | if (!list) { | |
ba728be7 | 1997 | if (verbose > 0) |
7a862a02 | 1998 | pr_err("no active Intel(R) RAID controller found.\n"); |
d665cc31 | 1999 | return 2; |
ba728be7 | 2000 | } else if (verbose > 0) |
155cbb4c | 2001 | print_found_intel_controllers(list); |
d665cc31 | 2002 | |
a891a3c2 | 2003 | for (hba = list; hba; hba = hba->next) { |
0858eccf | 2004 | if (controller_path && (compare_paths(hba->path, controller_path) != 0)) |
9eafa1de | 2005 | continue; |
0858eccf | 2006 | if (!find_imsm_capability(hba)) { |
60f0f54d | 2007 | char buf[PATH_MAX]; |
e7b84f9d | 2008 | pr_err("imsm capabilities not found for controller: %s (type %s)\n", |
60f0f54d PB |
2009 | hba->type == SYS_DEV_VMD ? vmd_domain_to_controller(hba, buf) : hba->path, |
2010 | get_sys_dev_type(hba->type)); | |
0858eccf AP |
2011 | continue; |
2012 | } | |
2013 | result = 0; | |
2014 | } | |
2015 | ||
2016 | if (controller_path && result == 1) { | |
2017 | pr_err("no active Intel(R) RAID controller found under %s\n", | |
2018 | controller_path); | |
2019 | return result; | |
2020 | } | |
2021 | ||
5e1d6128 | 2022 | const struct orom_entry *entry; |
0858eccf | 2023 | |
5e1d6128 | 2024 | for (entry = orom_entries; entry; entry = entry->next) { |
60f0f54d PB |
2025 | if (entry->type == SYS_DEV_VMD) { |
2026 | for (hba = list; hba; hba = hba->next) { | |
2027 | if (hba->type == SYS_DEV_VMD) { | |
2028 | char buf[PATH_MAX]; | |
2029 | print_imsm_capability(&entry->orom); | |
2030 | printf(" I/O Controller : %s (%s)\n", | |
2031 | vmd_domain_to_controller(hba, buf), get_sys_dev_type(hba->type)); | |
b5eece69 PB |
2032 | if (print_vmd_attached_devs(hba)) { |
2033 | if (verbose > 0) | |
2034 | pr_err("failed to get devices attached to VMD domain.\n"); | |
2035 | result |= 2; | |
2036 | } | |
60f0f54d PB |
2037 | printf("\n"); |
2038 | } | |
2039 | } | |
2040 | continue; | |
2041 | } | |
0858eccf | 2042 | |
60f0f54d PB |
2043 | print_imsm_capability(&entry->orom); |
2044 | if (entry->type == SYS_DEV_NVME) { | |
0858eccf AP |
2045 | for (hba = list; hba; hba = hba->next) { |
2046 | if (hba->type == SYS_DEV_NVME) | |
2047 | printf(" NVMe Device : %s\n", hba->path); | |
2048 | } | |
60f0f54d | 2049 | printf("\n"); |
0858eccf AP |
2050 | continue; |
2051 | } | |
2052 | ||
2053 | struct devid_list *devid; | |
5e1d6128 | 2054 | for (devid = entry->devid_list; devid; devid = devid->next) { |
0858eccf AP |
2055 | hba = device_by_id(devid->devid); |
2056 | if (!hba) | |
2057 | continue; | |
2058 | ||
9eafa1de MN |
2059 | printf(" I/O Controller : %s (%s)\n", |
2060 | hba->path, get_sys_dev_type(hba->type)); | |
2061 | if (hba->type == SYS_DEV_SATA) { | |
2062 | host_base = ahci_get_port_count(hba->path, &port_count); | |
2063 | if (ahci_enumerate_ports(hba->path, port_count, host_base, verbose)) { | |
2064 | if (verbose > 0) | |
7a862a02 | 2065 | pr_err("failed to enumerate ports on SATA controller at %s.\n", hba->pci_id); |
9eafa1de MN |
2066 | result |= 2; |
2067 | } | |
120dc887 LM |
2068 | } |
2069 | } | |
0858eccf | 2070 | printf("\n"); |
d665cc31 | 2071 | } |
155cbb4c | 2072 | |
120dc887 | 2073 | return result; |
d665cc31 | 2074 | } |
e50cf220 | 2075 | |
9eafa1de | 2076 | static int export_detail_platform_imsm(int verbose, char *controller_path) |
e50cf220 | 2077 | { |
e50cf220 MN |
2078 | struct sys_dev *list, *hba; |
2079 | int result=1; | |
2080 | ||
2081 | list = find_intel_devices(); | |
2082 | if (!list) { | |
2083 | if (verbose > 0) | |
2084 | pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n"); | |
2085 | result = 2; | |
e50cf220 MN |
2086 | return result; |
2087 | } | |
2088 | ||
2089 | for (hba = list; hba; hba = hba->next) { | |
9eafa1de MN |
2090 | if (controller_path && (compare_paths(hba->path,controller_path) != 0)) |
2091 | continue; | |
60f0f54d PB |
2092 | if (!find_imsm_capability(hba) && verbose > 0) { |
2093 | char buf[PATH_MAX]; | |
2094 | pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n", | |
2095 | hba->type == SYS_DEV_VMD ? vmd_domain_to_controller(hba, buf) : hba->path); | |
2096 | } | |
0858eccf | 2097 | else |
e50cf220 | 2098 | result = 0; |
e50cf220 MN |
2099 | } |
2100 | ||
5e1d6128 | 2101 | const struct orom_entry *entry; |
0858eccf | 2102 | |
60f0f54d PB |
2103 | for (entry = orom_entries; entry; entry = entry->next) { |
2104 | if (entry->type == SYS_DEV_VMD) { | |
2105 | for (hba = list; hba; hba = hba->next) | |
2106 | print_imsm_capability_export(&entry->orom); | |
2107 | continue; | |
2108 | } | |
5e1d6128 | 2109 | print_imsm_capability_export(&entry->orom); |
60f0f54d | 2110 | } |
0858eccf | 2111 | |
e50cf220 MN |
2112 | return result; |
2113 | } | |
2114 | ||
cdddbdbc DW |
2115 | #endif |
2116 | ||
2117 | static int match_home_imsm(struct supertype *st, char *homehost) | |
2118 | { | |
5115ca67 DW |
2119 | /* the imsm metadata format does not specify any host |
2120 | * identification information. We return -1 since we can never | |
2121 | * confirm nor deny whether a given array is "meant" for this | |
148acb7b | 2122 | * host. We rely on compare_super and the 'family_num' fields to |
5115ca67 DW |
2123 | * exclude member disks that do not belong, and we rely on |
2124 | * mdadm.conf to specify the arrays that should be assembled. | |
2125 | * Auto-assembly may still pick up "foreign" arrays. | |
2126 | */ | |
cdddbdbc | 2127 | |
9362c1c8 | 2128 | return -1; |
cdddbdbc DW |
2129 | } |
2130 | ||
2131 | static void uuid_from_super_imsm(struct supertype *st, int uuid[4]) | |
2132 | { | |
51006d85 N |
2133 | /* The uuid returned here is used for: |
2134 | * uuid to put into bitmap file (Create, Grow) | |
2135 | * uuid for backup header when saving critical section (Grow) | |
2136 | * comparing uuids when re-adding a device into an array | |
2137 | * In these cases the uuid required is that of the data-array, | |
2138 | * not the device-set. | |
2139 | * uuid to recognise same set when adding a missing device back | |
2140 | * to an array. This is a uuid for the device-set. | |
1011e834 | 2141 | * |
51006d85 N |
2142 | * For each of these we can make do with a truncated |
2143 | * or hashed uuid rather than the original, as long as | |
2144 | * everyone agrees. | |
2145 | * In each case the uuid required is that of the data-array, | |
2146 | * not the device-set. | |
43dad3d6 | 2147 | */ |
51006d85 N |
2148 | /* imsm does not track uuid's so we synthesis one using sha1 on |
2149 | * - The signature (Which is constant for all imsm array, but no matter) | |
148acb7b | 2150 | * - the orig_family_num of the container |
51006d85 N |
2151 | * - the index number of the volume |
2152 | * - the 'serial' number of the volume. | |
2153 | * Hopefully these are all constant. | |
2154 | */ | |
2155 | struct intel_super *super = st->sb; | |
43dad3d6 | 2156 | |
51006d85 N |
2157 | char buf[20]; |
2158 | struct sha1_ctx ctx; | |
2159 | struct imsm_dev *dev = NULL; | |
148acb7b | 2160 | __u32 family_num; |
51006d85 | 2161 | |
148acb7b DW |
2162 | /* some mdadm versions failed to set ->orig_family_num, in which |
2163 | * case fall back to ->family_num. orig_family_num will be | |
2164 | * fixed up with the first metadata update. | |
2165 | */ | |
2166 | family_num = super->anchor->orig_family_num; | |
2167 | if (family_num == 0) | |
2168 | family_num = super->anchor->family_num; | |
51006d85 | 2169 | sha1_init_ctx(&ctx); |
92bd8f8d | 2170 | sha1_process_bytes(super->anchor->sig, MPB_SIG_LEN, &ctx); |
148acb7b | 2171 | sha1_process_bytes(&family_num, sizeof(__u32), &ctx); |
51006d85 N |
2172 | if (super->current_vol >= 0) |
2173 | dev = get_imsm_dev(super, super->current_vol); | |
2174 | if (dev) { | |
2175 | __u32 vol = super->current_vol; | |
2176 | sha1_process_bytes(&vol, sizeof(vol), &ctx); | |
2177 | sha1_process_bytes(dev->volume, MAX_RAID_SERIAL_LEN, &ctx); | |
2178 | } | |
2179 | sha1_finish_ctx(&ctx, buf); | |
2180 | memcpy(uuid, buf, 4*4); | |
cdddbdbc DW |
2181 | } |
2182 | ||
0d481d37 | 2183 | #if 0 |
4f5bc454 DW |
2184 | static void |
2185 | get_imsm_numerical_version(struct imsm_super *mpb, int *m, int *p) | |
cdddbdbc | 2186 | { |
cdddbdbc DW |
2187 | __u8 *v = get_imsm_version(mpb); |
2188 | __u8 *end = mpb->sig + MAX_SIGNATURE_LENGTH; | |
2189 | char major[] = { 0, 0, 0 }; | |
2190 | char minor[] = { 0 ,0, 0 }; | |
2191 | char patch[] = { 0, 0, 0 }; | |
2192 | char *ver_parse[] = { major, minor, patch }; | |
2193 | int i, j; | |
2194 | ||
2195 | i = j = 0; | |
2196 | while (*v != '\0' && v < end) { | |
2197 | if (*v != '.' && j < 2) | |
2198 | ver_parse[i][j++] = *v; | |
2199 | else { | |
2200 | i++; | |
2201 | j = 0; | |
2202 | } | |
2203 | v++; | |
2204 | } | |
2205 | ||
4f5bc454 DW |
2206 | *m = strtol(minor, NULL, 0); |
2207 | *p = strtol(patch, NULL, 0); | |
2208 | } | |
0d481d37 | 2209 | #endif |
4f5bc454 | 2210 | |
1e5c6983 DW |
2211 | static __u32 migr_strip_blocks_resync(struct imsm_dev *dev) |
2212 | { | |
2213 | /* migr_strip_size when repairing or initializing parity */ | |
238c0a71 | 2214 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
1e5c6983 DW |
2215 | __u32 chunk = __le32_to_cpu(map->blocks_per_strip); |
2216 | ||
2217 | switch (get_imsm_raid_level(map)) { | |
2218 | case 5: | |
2219 | case 10: | |
2220 | return chunk; | |
2221 | default: | |
2222 | return 128*1024 >> 9; | |
2223 | } | |
2224 | } | |
2225 | ||
2226 | static __u32 migr_strip_blocks_rebuild(struct imsm_dev *dev) | |
2227 | { | |
2228 | /* migr_strip_size when rebuilding a degraded disk, no idea why | |
2229 | * this is different than migr_strip_size_resync(), but it's good | |
2230 | * to be compatible | |
2231 | */ | |
238c0a71 | 2232 | struct imsm_map *map = get_imsm_map(dev, MAP_1); |
1e5c6983 DW |
2233 | __u32 chunk = __le32_to_cpu(map->blocks_per_strip); |
2234 | ||
2235 | switch (get_imsm_raid_level(map)) { | |
2236 | case 1: | |
2237 | case 10: | |
2238 | if (map->num_members % map->num_domains == 0) | |
2239 | return 128*1024 >> 9; | |
2240 | else | |
2241 | return chunk; | |
2242 | case 5: | |
2243 | return max((__u32) 64*1024 >> 9, chunk); | |
2244 | default: | |
2245 | return 128*1024 >> 9; | |
2246 | } | |
2247 | } | |
2248 | ||
2249 | static __u32 num_stripes_per_unit_resync(struct imsm_dev *dev) | |
2250 | { | |
238c0a71 AK |
2251 | struct imsm_map *lo = get_imsm_map(dev, MAP_0); |
2252 | struct imsm_map *hi = get_imsm_map(dev, MAP_1); | |
1e5c6983 DW |
2253 | __u32 lo_chunk = __le32_to_cpu(lo->blocks_per_strip); |
2254 | __u32 hi_chunk = __le32_to_cpu(hi->blocks_per_strip); | |
2255 | ||
2256 | return max((__u32) 1, hi_chunk / lo_chunk); | |
2257 | } | |
2258 | ||
2259 | static __u32 num_stripes_per_unit_rebuild(struct imsm_dev *dev) | |
2260 | { | |
238c0a71 | 2261 | struct imsm_map *lo = get_imsm_map(dev, MAP_0); |
1e5c6983 DW |
2262 | int level = get_imsm_raid_level(lo); |
2263 | ||
2264 | if (level == 1 || level == 10) { | |
238c0a71 | 2265 | struct imsm_map *hi = get_imsm_map(dev, MAP_1); |
1e5c6983 DW |
2266 | |
2267 | return hi->num_domains; | |
2268 | } else | |
2269 | return num_stripes_per_unit_resync(dev); | |
2270 | } | |
2271 | ||
98130f40 | 2272 | static __u8 imsm_num_data_members(struct imsm_dev *dev, int second_map) |
1e5c6983 DW |
2273 | { |
2274 | /* named 'imsm_' because raid0, raid1 and raid10 | |
2275 | * counter-intuitively have the same number of data disks | |
2276 | */ | |
98130f40 | 2277 | struct imsm_map *map = get_imsm_map(dev, second_map); |
1e5c6983 DW |
2278 | |
2279 | switch (get_imsm_raid_level(map)) { | |
2280 | case 0: | |
36fd8ccc AK |
2281 | return map->num_members; |
2282 | break; | |
1e5c6983 DW |
2283 | case 1: |
2284 | case 10: | |
36fd8ccc | 2285 | return map->num_members/2; |
1e5c6983 DW |
2286 | case 5: |
2287 | return map->num_members - 1; | |
2288 | default: | |
1ade5cc1 | 2289 | dprintf("unsupported raid level\n"); |
1e5c6983 DW |
2290 | return 0; |
2291 | } | |
2292 | } | |
2293 | ||
2294 | static __u32 parity_segment_depth(struct imsm_dev *dev) | |
2295 | { | |
238c0a71 | 2296 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
1e5c6983 DW |
2297 | __u32 chunk = __le32_to_cpu(map->blocks_per_strip); |
2298 | ||
2299 | switch(get_imsm_raid_level(map)) { | |
2300 | case 1: | |
2301 | case 10: | |
2302 | return chunk * map->num_domains; | |
2303 | case 5: | |
2304 | return chunk * map->num_members; | |
2305 | default: | |
2306 | return chunk; | |
2307 | } | |
2308 | } | |
2309 | ||
2310 | static __u32 map_migr_block(struct imsm_dev *dev, __u32 block) | |
2311 | { | |
238c0a71 | 2312 | struct imsm_map *map = get_imsm_map(dev, MAP_1); |
1e5c6983 DW |
2313 | __u32 chunk = __le32_to_cpu(map->blocks_per_strip); |
2314 | __u32 strip = block / chunk; | |
2315 | ||
2316 | switch (get_imsm_raid_level(map)) { | |
2317 | case 1: | |
2318 | case 10: { | |
2319 | __u32 vol_strip = (strip * map->num_domains) + 1; | |
2320 | __u32 vol_stripe = vol_strip / map->num_members; | |
2321 | ||
2322 | return vol_stripe * chunk + block % chunk; | |
2323 | } case 5: { | |
2324 | __u32 stripe = strip / (map->num_members - 1); | |
2325 | ||
2326 | return stripe * chunk + block % chunk; | |
2327 | } | |
2328 | default: | |
2329 | return 0; | |
2330 | } | |
2331 | } | |
2332 | ||
c47b0ff6 AK |
2333 | static __u64 blocks_per_migr_unit(struct intel_super *super, |
2334 | struct imsm_dev *dev) | |
1e5c6983 DW |
2335 | { |
2336 | /* calculate the conversion factor between per member 'blocks' | |
2337 | * (md/{resync,rebuild}_start) and imsm migration units, return | |
2338 | * 0 for the 'not migrating' and 'unsupported migration' cases | |
2339 | */ | |
2340 | if (!dev->vol.migr_state) | |
2341 | return 0; | |
2342 | ||
2343 | switch (migr_type(dev)) { | |
c47b0ff6 AK |
2344 | case MIGR_GEN_MIGR: { |
2345 | struct migr_record *migr_rec = super->migr_rec; | |
2346 | return __le32_to_cpu(migr_rec->blocks_per_unit); | |
2347 | } | |
1e5c6983 DW |
2348 | case MIGR_VERIFY: |
2349 | case MIGR_REPAIR: | |
2350 | case MIGR_INIT: { | |
238c0a71 | 2351 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
1e5c6983 DW |
2352 | __u32 stripes_per_unit; |
2353 | __u32 blocks_per_unit; | |
2354 | __u32 parity_depth; | |
2355 | __u32 migr_chunk; | |
2356 | __u32 block_map; | |
2357 | __u32 block_rel; | |
2358 | __u32 segment; | |
2359 | __u32 stripe; | |
2360 | __u8 disks; | |
2361 | ||
2362 | /* yes, this is really the translation of migr_units to | |
2363 | * per-member blocks in the 'resync' case | |
2364 | */ | |
2365 | stripes_per_unit = num_stripes_per_unit_resync(dev); | |
2366 | migr_chunk = migr_strip_blocks_resync(dev); | |
238c0a71 | 2367 | disks = imsm_num_data_members(dev, MAP_0); |
1e5c6983 | 2368 | blocks_per_unit = stripes_per_unit * migr_chunk * disks; |
7b1ab482 | 2369 | stripe = __le16_to_cpu(map->blocks_per_strip) * disks; |
1e5c6983 DW |
2370 | segment = blocks_per_unit / stripe; |
2371 | block_rel = blocks_per_unit - segment * stripe; | |
2372 | parity_depth = parity_segment_depth(dev); | |
2373 | block_map = map_migr_block(dev, block_rel); | |
2374 | return block_map + parity_depth * segment; | |
2375 | } | |
2376 | case MIGR_REBUILD: { | |
2377 | __u32 stripes_per_unit; | |
2378 | __u32 migr_chunk; | |
2379 | ||
2380 | stripes_per_unit = num_stripes_per_unit_rebuild(dev); | |
2381 | migr_chunk = migr_strip_blocks_rebuild(dev); | |
2382 | return migr_chunk * stripes_per_unit; | |
2383 | } | |
1e5c6983 DW |
2384 | case MIGR_STATE_CHANGE: |
2385 | default: | |
2386 | return 0; | |
2387 | } | |
2388 | } | |
2389 | ||
c2c087e6 DW |
2390 | static int imsm_level_to_layout(int level) |
2391 | { | |
2392 | switch (level) { | |
2393 | case 0: | |
2394 | case 1: | |
2395 | return 0; | |
2396 | case 5: | |
2397 | case 6: | |
a380c027 | 2398 | return ALGORITHM_LEFT_ASYMMETRIC; |
c2c087e6 | 2399 | case 10: |
c92a2527 | 2400 | return 0x102; |
c2c087e6 | 2401 | } |
a18a888e | 2402 | return UnSet; |
c2c087e6 DW |
2403 | } |
2404 | ||
8e59f3d8 AK |
2405 | /******************************************************************************* |
2406 | * Function: read_imsm_migr_rec | |
2407 | * Description: Function reads imsm migration record from last sector of disk | |
2408 | * Parameters: | |
2409 | * fd : disk descriptor | |
2410 | * super : metadata info | |
2411 | * Returns: | |
2412 | * 0 : success, | |
2413 | * -1 : fail | |
2414 | ******************************************************************************/ | |
2415 | static int read_imsm_migr_rec(int fd, struct intel_super *super) | |
2416 | { | |
2417 | int ret_val = -1; | |
2418 | unsigned long long dsize; | |
2419 | ||
2420 | get_dev_size(fd, NULL, &dsize); | |
17a4eaf9 | 2421 | if (lseek64(fd, dsize - MIGR_REC_POSITION, SEEK_SET) < 0) { |
e7b84f9d N |
2422 | pr_err("Cannot seek to anchor block: %s\n", |
2423 | strerror(errno)); | |
8e59f3d8 AK |
2424 | goto out; |
2425 | } | |
17a4eaf9 AK |
2426 | if (read(fd, super->migr_rec_buf, MIGR_REC_BUF_SIZE) != |
2427 | MIGR_REC_BUF_SIZE) { | |
e7b84f9d N |
2428 | pr_err("Cannot read migr record block: %s\n", |
2429 | strerror(errno)); | |
8e59f3d8 AK |
2430 | goto out; |
2431 | } | |
2432 | ret_val = 0; | |
2433 | ||
2434 | out: | |
2435 | return ret_val; | |
2436 | } | |
2437 | ||
3136abe5 AK |
2438 | static struct imsm_dev *imsm_get_device_during_migration( |
2439 | struct intel_super *super) | |
2440 | { | |
2441 | ||
2442 | struct intel_dev *dv; | |
2443 | ||
2444 | for (dv = super->devlist; dv; dv = dv->next) { | |
2445 | if (is_gen_migration(dv->dev)) | |
2446 | return dv->dev; | |
2447 | } | |
2448 | return NULL; | |
2449 | } | |
2450 | ||
8e59f3d8 AK |
2451 | /******************************************************************************* |
2452 | * Function: load_imsm_migr_rec | |
2453 | * Description: Function reads imsm migration record (it is stored at the last | |
2454 | * sector of disk) | |
2455 | * Parameters: | |
2456 | * super : imsm internal array info | |
2457 | * info : general array info | |
2458 | * Returns: | |
2459 | * 0 : success | |
2460 | * -1 : fail | |
4c965cc9 | 2461 | * -2 : no migration in progress |
8e59f3d8 AK |
2462 | ******************************************************************************/ |
2463 | static int load_imsm_migr_rec(struct intel_super *super, struct mdinfo *info) | |
2464 | { | |
2465 | struct mdinfo *sd; | |
594dc1b8 | 2466 | struct dl *dl; |
8e59f3d8 AK |
2467 | char nm[30]; |
2468 | int retval = -1; | |
2469 | int fd = -1; | |
3136abe5 | 2470 | struct imsm_dev *dev; |
594dc1b8 | 2471 | struct imsm_map *map; |
b4ab44d8 | 2472 | int slot = -1; |
3136abe5 AK |
2473 | |
2474 | /* find map under migration */ | |
2475 | dev = imsm_get_device_during_migration(super); | |
2476 | /* nothing to load,no migration in progress? | |
2477 | */ | |
2478 | if (dev == NULL) | |
4c965cc9 | 2479 | return -2; |
44bfe6df | 2480 | map = get_imsm_map(dev, MAP_0); |
8e59f3d8 AK |
2481 | |
2482 | if (info) { | |
2483 | for (sd = info->devs ; sd ; sd = sd->next) { | |
3136abe5 AK |
2484 | /* skip spare and failed disks |
2485 | */ | |
2486 | if (sd->disk.raid_disk < 0) | |
2487 | continue; | |
8e59f3d8 | 2488 | /* read only from one of the first two slots */ |
3136abe5 AK |
2489 | if (map) |
2490 | slot = get_imsm_disk_slot(map, | |
2491 | sd->disk.raid_disk); | |
2492 | if ((map == NULL) || (slot > 1) || (slot < 0)) | |
8e59f3d8 | 2493 | continue; |
3136abe5 | 2494 | |
8e59f3d8 AK |
2495 | sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor); |
2496 | fd = dev_open(nm, O_RDONLY); | |
2497 | if (fd >= 0) | |
2498 | break; | |
2499 | } | |
2500 | } | |
2501 | if (fd < 0) { | |
2502 | for (dl = super->disks; dl; dl = dl->next) { | |
3136abe5 AK |
2503 | /* skip spare and failed disks |
2504 | */ | |
2505 | if (dl->index < 0) | |
2506 | continue; | |
8e59f3d8 | 2507 | /* read only from one of the first two slots */ |
3136abe5 AK |
2508 | if (map) |
2509 | slot = get_imsm_disk_slot(map, dl->index); | |
2510 | if ((map == NULL) || (slot > 1) || (slot < 0)) | |
8e59f3d8 AK |
2511 | continue; |
2512 | sprintf(nm, "%d:%d", dl->major, dl->minor); | |
2513 | fd = dev_open(nm, O_RDONLY); | |
2514 | if (fd >= 0) | |
2515 | break; | |
2516 | } | |
2517 | } | |
2518 | if (fd < 0) | |
2519 | goto out; | |
2520 | retval = read_imsm_migr_rec(fd, super); | |
2521 | ||
2522 | out: | |
2523 | if (fd >= 0) | |
2524 | close(fd); | |
2525 | return retval; | |
2526 | } | |
2527 | ||
9e2d750d | 2528 | #ifndef MDASSEMBLE |
c17608ea AK |
2529 | /******************************************************************************* |
2530 | * function: imsm_create_metadata_checkpoint_update | |
2531 | * Description: It creates update for checkpoint change. | |
2532 | * Parameters: | |
2533 | * super : imsm internal array info | |
2534 | * u : pointer to prepared update | |
2535 | * Returns: | |
2536 | * Uptate length. | |
2537 | * If length is equal to 0, input pointer u contains no update | |
2538 | ******************************************************************************/ | |
2539 | static int imsm_create_metadata_checkpoint_update( | |
2540 | struct intel_super *super, | |
2541 | struct imsm_update_general_migration_checkpoint **u) | |
2542 | { | |
2543 | ||
2544 | int update_memory_size = 0; | |
2545 | ||
1ade5cc1 | 2546 | dprintf("(enter)\n"); |
c17608ea AK |
2547 | |
2548 | if (u == NULL) | |
2549 | return 0; | |
2550 | *u = NULL; | |
2551 | ||
2552 | /* size of all update data without anchor */ | |
2553 | update_memory_size = | |
2554 | sizeof(struct imsm_update_general_migration_checkpoint); | |
2555 | ||
503975b9 | 2556 | *u = xcalloc(1, update_memory_size); |
c17608ea | 2557 | if (*u == NULL) { |
1ade5cc1 | 2558 | dprintf("error: cannot get memory\n"); |
c17608ea AK |
2559 | return 0; |
2560 | } | |
2561 | (*u)->type = update_general_migration_checkpoint; | |
2562 | (*u)->curr_migr_unit = __le32_to_cpu(super->migr_rec->curr_migr_unit); | |
1ade5cc1 | 2563 | dprintf("prepared for %u\n", (*u)->curr_migr_unit); |
c17608ea AK |
2564 | |
2565 | return update_memory_size; | |
2566 | } | |
2567 | ||
c17608ea AK |
2568 | static void imsm_update_metadata_locally(struct supertype *st, |
2569 | void *buf, int len); | |
2570 | ||
687629c2 AK |
2571 | /******************************************************************************* |
2572 | * Function: write_imsm_migr_rec | |
2573 | * Description: Function writes imsm migration record | |
2574 | * (at the last sector of disk) | |
2575 | * Parameters: | |
2576 | * super : imsm internal array info | |
2577 | * Returns: | |
2578 | * 0 : success | |
2579 | * -1 : if fail | |
2580 | ******************************************************************************/ | |
2581 | static int write_imsm_migr_rec(struct supertype *st) | |
2582 | { | |
2583 | struct intel_super *super = st->sb; | |
2584 | unsigned long long dsize; | |
2585 | char nm[30]; | |
2586 | int fd = -1; | |
2587 | int retval = -1; | |
2588 | struct dl *sd; | |
c17608ea AK |
2589 | int len; |
2590 | struct imsm_update_general_migration_checkpoint *u; | |
3136abe5 | 2591 | struct imsm_dev *dev; |
594dc1b8 | 2592 | struct imsm_map *map; |
3136abe5 AK |
2593 | |
2594 | /* find map under migration */ | |
2595 | dev = imsm_get_device_during_migration(super); | |
2596 | /* if no migration, write buffer anyway to clear migr_record | |
2597 | * on disk based on first available device | |
2598 | */ | |
2599 | if (dev == NULL) | |
2600 | dev = get_imsm_dev(super, super->current_vol < 0 ? 0 : | |
2601 | super->current_vol); | |
2602 | ||
44bfe6df | 2603 | map = get_imsm_map(dev, MAP_0); |
687629c2 AK |
2604 | |
2605 | for (sd = super->disks ; sd ; sd = sd->next) { | |
b4ab44d8 | 2606 | int slot = -1; |
3136abe5 AK |
2607 | |
2608 | /* skip failed and spare devices */ | |
2609 | if (sd->index < 0) | |
2610 | continue; | |
687629c2 | 2611 | /* write to 2 first slots only */ |
3136abe5 AK |
2612 | if (map) |
2613 | slot = get_imsm_disk_slot(map, sd->index); | |
2614 | if ((map == NULL) || (slot > 1) || (slot < 0)) | |
687629c2 | 2615 | continue; |
3136abe5 | 2616 | |
687629c2 AK |
2617 | sprintf(nm, "%d:%d", sd->major, sd->minor); |
2618 | fd = dev_open(nm, O_RDWR); | |
2619 | if (fd < 0) | |
2620 | continue; | |
2621 | get_dev_size(fd, NULL, &dsize); | |
17a4eaf9 | 2622 | if (lseek64(fd, dsize - MIGR_REC_POSITION, SEEK_SET) < 0) { |
e7b84f9d N |
2623 | pr_err("Cannot seek to anchor block: %s\n", |
2624 | strerror(errno)); | |
687629c2 AK |
2625 | goto out; |
2626 | } | |
17a4eaf9 AK |
2627 | if (write(fd, super->migr_rec_buf, MIGR_REC_BUF_SIZE) != |
2628 | MIGR_REC_BUF_SIZE) { | |
e7b84f9d N |
2629 | pr_err("Cannot write migr record block: %s\n", |
2630 | strerror(errno)); | |
687629c2 AK |
2631 | goto out; |
2632 | } | |
2633 | close(fd); | |
2634 | fd = -1; | |
2635 | } | |
c17608ea AK |
2636 | /* update checkpoint information in metadata */ |
2637 | len = imsm_create_metadata_checkpoint_update(super, &u); | |
2638 | ||
2639 | if (len <= 0) { | |
2640 | dprintf("imsm: Cannot prepare update\n"); | |
2641 | goto out; | |
2642 | } | |
2643 | /* update metadata locally */ | |
2644 | imsm_update_metadata_locally(st, u, len); | |
2645 | /* and possibly remotely */ | |
2646 | if (st->update_tail) { | |
2647 | append_metadata_update(st, u, len); | |
2648 | /* during reshape we do all work inside metadata handler | |
2649 | * manage_reshape(), so metadata update has to be triggered | |
2650 | * insida it | |
2651 | */ | |
2652 | flush_metadata_updates(st); | |
2653 | st->update_tail = &st->updates; | |
2654 | } else | |
2655 | free(u); | |
687629c2 AK |
2656 | |
2657 | retval = 0; | |
2658 | out: | |
2659 | if (fd >= 0) | |
2660 | close(fd); | |
2661 | return retval; | |
2662 | } | |
9e2d750d | 2663 | #endif /* MDASSEMBLE */ |
687629c2 | 2664 | |
e2962bfc AK |
2665 | /* spare/missing disks activations are not allowe when |
2666 | * array/container performs reshape operation, because | |
2667 | * all arrays in container works on the same disks set | |
2668 | */ | |
2669 | int imsm_reshape_blocks_arrays_changes(struct intel_super *super) | |
2670 | { | |
2671 | int rv = 0; | |
2672 | struct intel_dev *i_dev; | |
2673 | struct imsm_dev *dev; | |
2674 | ||
2675 | /* check whole container | |
2676 | */ | |
2677 | for (i_dev = super->devlist; i_dev; i_dev = i_dev->next) { | |
2678 | dev = i_dev->dev; | |
3ad25638 | 2679 | if (is_gen_migration(dev)) { |
e2962bfc AK |
2680 | /* No repair during any migration in container |
2681 | */ | |
2682 | rv = 1; | |
2683 | break; | |
2684 | } | |
2685 | } | |
2686 | return rv; | |
2687 | } | |
c41e00b2 AK |
2688 | static unsigned long long imsm_component_size_aligment_check(int level, |
2689 | int chunk_size, | |
2690 | unsigned long long component_size) | |
2691 | { | |
2692 | unsigned int component_size_alligment; | |
2693 | ||
2694 | /* check component size aligment | |
2695 | */ | |
2696 | component_size_alligment = component_size % (chunk_size/512); | |
2697 | ||
1ade5cc1 | 2698 | dprintf("(Level: %i, chunk_size = %i, component_size = %llu), component_size_alligment = %u\n", |
c41e00b2 AK |
2699 | level, chunk_size, component_size, |
2700 | component_size_alligment); | |
2701 | ||
2702 | if (component_size_alligment && (level != 1) && (level != UnSet)) { | |
2703 | dprintf("imsm: reported component size alligned from %llu ", | |
2704 | component_size); | |
2705 | component_size -= component_size_alligment; | |
1ade5cc1 | 2706 | dprintf_cont("to %llu (%i).\n", |
c41e00b2 AK |
2707 | component_size, component_size_alligment); |
2708 | } | |
2709 | ||
2710 | return component_size; | |
2711 | } | |
e2962bfc | 2712 | |
a5d85af7 | 2713 | static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info, char *dmap) |
bf5a934a DW |
2714 | { |
2715 | struct intel_super *super = st->sb; | |
c47b0ff6 | 2716 | struct migr_record *migr_rec = super->migr_rec; |
949c47a0 | 2717 | struct imsm_dev *dev = get_imsm_dev(super, super->current_vol); |
238c0a71 AK |
2718 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
2719 | struct imsm_map *prev_map = get_imsm_map(dev, MAP_1); | |
b335e593 | 2720 | struct imsm_map *map_to_analyse = map; |
efb30e7f | 2721 | struct dl *dl; |
a5d85af7 | 2722 | int map_disks = info->array.raid_disks; |
bf5a934a | 2723 | |
95eeceeb | 2724 | memset(info, 0, sizeof(*info)); |
b335e593 AK |
2725 | if (prev_map) |
2726 | map_to_analyse = prev_map; | |
2727 | ||
ca0748fa | 2728 | dl = super->current_disk; |
9894ec0d | 2729 | |
bf5a934a | 2730 | info->container_member = super->current_vol; |
cd0430a1 | 2731 | info->array.raid_disks = map->num_members; |
b335e593 | 2732 | info->array.level = get_imsm_raid_level(map_to_analyse); |
bf5a934a DW |
2733 | info->array.layout = imsm_level_to_layout(info->array.level); |
2734 | info->array.md_minor = -1; | |
2735 | info->array.ctime = 0; | |
2736 | info->array.utime = 0; | |
b335e593 AK |
2737 | info->array.chunk_size = |
2738 | __le16_to_cpu(map_to_analyse->blocks_per_strip) << 9; | |
301406c9 | 2739 | info->array.state = !dev->vol.dirty; |
da9b4a62 DW |
2740 | info->custom_array_size = __le32_to_cpu(dev->size_high); |
2741 | info->custom_array_size <<= 32; | |
2742 | info->custom_array_size |= __le32_to_cpu(dev->size_low); | |
3ad25638 AK |
2743 | info->recovery_blocked = imsm_reshape_blocks_arrays_changes(st->sb); |
2744 | ||
3f510843 | 2745 | if (is_gen_migration(dev)) { |
3f83228a | 2746 | info->reshape_active = 1; |
b335e593 AK |
2747 | info->new_level = get_imsm_raid_level(map); |
2748 | info->new_layout = imsm_level_to_layout(info->new_level); | |
2749 | info->new_chunk = __le16_to_cpu(map->blocks_per_strip) << 9; | |
3f83228a | 2750 | info->delta_disks = map->num_members - prev_map->num_members; |
493f5dd6 N |
2751 | if (info->delta_disks) { |
2752 | /* this needs to be applied to every array | |
2753 | * in the container. | |
2754 | */ | |
81219e70 | 2755 | info->reshape_active = CONTAINER_RESHAPE; |
493f5dd6 | 2756 | } |
3f83228a N |
2757 | /* We shape information that we give to md might have to be |
2758 | * modify to cope with md's requirement for reshaping arrays. | |
2759 | * For example, when reshaping a RAID0, md requires it to be | |
2760 | * presented as a degraded RAID4. | |
2761 | * Also if a RAID0 is migrating to a RAID5 we need to specify | |
2762 | * the array as already being RAID5, but the 'before' layout | |
2763 | * is a RAID4-like layout. | |
2764 | */ | |
2765 | switch (info->array.level) { | |
2766 | case 0: | |
2767 | switch(info->new_level) { | |
2768 | case 0: | |
2769 | /* conversion is happening as RAID4 */ | |
2770 | info->array.level = 4; | |
2771 | info->array.raid_disks += 1; | |
2772 | break; | |
2773 | case 5: | |
2774 | /* conversion is happening as RAID5 */ | |
2775 | info->array.level = 5; | |
2776 | info->array.layout = ALGORITHM_PARITY_N; | |
3f83228a N |
2777 | info->delta_disks -= 1; |
2778 | break; | |
2779 | default: | |
2780 | /* FIXME error message */ | |
2781 | info->array.level = UnSet; | |
2782 | break; | |
2783 | } | |
2784 | break; | |
2785 | } | |
b335e593 AK |
2786 | } else { |
2787 | info->new_level = UnSet; | |
2788 | info->new_layout = UnSet; | |
2789 | info->new_chunk = info->array.chunk_size; | |
3f83228a | 2790 | info->delta_disks = 0; |
b335e593 | 2791 | } |
ca0748fa | 2792 | |
efb30e7f DW |
2793 | if (dl) { |
2794 | info->disk.major = dl->major; | |
2795 | info->disk.minor = dl->minor; | |
ca0748fa | 2796 | info->disk.number = dl->index; |
656b6b5a N |
2797 | info->disk.raid_disk = get_imsm_disk_slot(map_to_analyse, |
2798 | dl->index); | |
efb30e7f | 2799 | } |
bf5a934a | 2800 | |
5551b113 CA |
2801 | info->data_offset = pba_of_lba0(map_to_analyse); |
2802 | info->component_size = blocks_per_member(map_to_analyse); | |
139dae11 | 2803 | |
c41e00b2 AK |
2804 | info->component_size = imsm_component_size_aligment_check( |
2805 | info->array.level, | |
2806 | info->array.chunk_size, | |
2807 | info->component_size); | |
139dae11 | 2808 | |
301406c9 | 2809 | memset(info->uuid, 0, sizeof(info->uuid)); |
921d9e16 | 2810 | info->recovery_start = MaxSector; |
bf5a934a | 2811 | |
d2e6d5d6 | 2812 | info->reshape_progress = 0; |
b6796ce1 | 2813 | info->resync_start = MaxSector; |
b9172665 AK |
2814 | if ((map_to_analyse->map_state == IMSM_T_STATE_UNINITIALIZED || |
2815 | dev->vol.dirty) && | |
2816 | imsm_reshape_blocks_arrays_changes(super) == 0) { | |
301406c9 | 2817 | info->resync_start = 0; |
b6796ce1 AK |
2818 | } |
2819 | if (dev->vol.migr_state) { | |
1e5c6983 DW |
2820 | switch (migr_type(dev)) { |
2821 | case MIGR_REPAIR: | |
2822 | case MIGR_INIT: { | |
c47b0ff6 AK |
2823 | __u64 blocks_per_unit = blocks_per_migr_unit(super, |
2824 | dev); | |
1e5c6983 DW |
2825 | __u64 units = __le32_to_cpu(dev->vol.curr_migr_unit); |
2826 | ||
2827 | info->resync_start = blocks_per_unit * units; | |
2828 | break; | |
2829 | } | |
d2e6d5d6 | 2830 | case MIGR_GEN_MIGR: { |
c47b0ff6 AK |
2831 | __u64 blocks_per_unit = blocks_per_migr_unit(super, |
2832 | dev); | |
2833 | __u64 units = __le32_to_cpu(migr_rec->curr_migr_unit); | |
04fa9523 AK |
2834 | unsigned long long array_blocks; |
2835 | int used_disks; | |
d2e6d5d6 | 2836 | |
befb629b AK |
2837 | if (__le32_to_cpu(migr_rec->ascending_migr) && |
2838 | (units < | |
2839 | (__le32_to_cpu(migr_rec->num_migr_units)-1)) && | |
2840 | (super->migr_rec->rec_status == | |
2841 | __cpu_to_le32(UNIT_SRC_IN_CP_AREA))) | |
2842 | units++; | |
2843 | ||
d2e6d5d6 | 2844 | info->reshape_progress = blocks_per_unit * units; |
6289d1e0 | 2845 | |
7a862a02 | 2846 | dprintf("IMSM: General Migration checkpoint : %llu (%llu) -> read reshape progress : %llu\n", |
19986c72 MB |
2847 | (unsigned long long)units, |
2848 | (unsigned long long)blocks_per_unit, | |
2849 | info->reshape_progress); | |
75156c46 | 2850 | |
238c0a71 | 2851 | used_disks = imsm_num_data_members(dev, MAP_1); |
75156c46 | 2852 | if (used_disks > 0) { |
5551b113 | 2853 | array_blocks = blocks_per_member(map) * |
75156c46 AK |
2854 | used_disks; |
2855 | /* round array size down to closest MB | |
2856 | */ | |
2857 | info->custom_array_size = (array_blocks | |
2858 | >> SECT_PER_MB_SHIFT) | |
2859 | << SECT_PER_MB_SHIFT; | |
2860 | } | |
d2e6d5d6 | 2861 | } |
1e5c6983 DW |
2862 | case MIGR_VERIFY: |
2863 | /* we could emulate the checkpointing of | |
2864 | * 'sync_action=check' migrations, but for now | |
2865 | * we just immediately complete them | |
2866 | */ | |
2867 | case MIGR_REBUILD: | |
2868 | /* this is handled by container_content_imsm() */ | |
1e5c6983 DW |
2869 | case MIGR_STATE_CHANGE: |
2870 | /* FIXME handle other migrations */ | |
2871 | default: | |
2872 | /* we are not dirty, so... */ | |
2873 | info->resync_start = MaxSector; | |
2874 | } | |
b6796ce1 | 2875 | } |
301406c9 DW |
2876 | |
2877 | strncpy(info->name, (char *) dev->volume, MAX_RAID_SERIAL_LEN); | |
2878 | info->name[MAX_RAID_SERIAL_LEN] = 0; | |
bf5a934a | 2879 | |
f35f2525 N |
2880 | info->array.major_version = -1; |
2881 | info->array.minor_version = -2; | |
4dd2df09 | 2882 | sprintf(info->text_version, "/%s/%d", st->container_devnm, info->container_member); |
a67dd8cc | 2883 | info->safe_mode_delay = 4000; /* 4 secs like the Matrix driver */ |
51006d85 | 2884 | uuid_from_super_imsm(st, info->uuid); |
a5d85af7 N |
2885 | |
2886 | if (dmap) { | |
2887 | int i, j; | |
2888 | for (i=0; i<map_disks; i++) { | |
2889 | dmap[i] = 0; | |
2890 | if (i < info->array.raid_disks) { | |
2891 | struct imsm_disk *dsk; | |
238c0a71 | 2892 | j = get_imsm_disk_idx(dev, i, MAP_X); |
a5d85af7 N |
2893 | dsk = get_imsm_disk(super, j); |
2894 | if (dsk && (dsk->status & CONFIGURED_DISK)) | |
2895 | dmap[i] = 1; | |
2896 | } | |
2897 | } | |
2898 | } | |
81ac8b4d | 2899 | } |
bf5a934a | 2900 | |
3b451610 AK |
2901 | static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev, |
2902 | int failed, int look_in_map); | |
2903 | ||
2904 | static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev, | |
2905 | int look_in_map); | |
2906 | ||
b4ab44d8 | 2907 | #ifndef MDASSEMBLE |
3b451610 AK |
2908 | static void manage_second_map(struct intel_super *super, struct imsm_dev *dev) |
2909 | { | |
2910 | if (is_gen_migration(dev)) { | |
2911 | int failed; | |
2912 | __u8 map_state; | |
2913 | struct imsm_map *map2 = get_imsm_map(dev, MAP_1); | |
2914 | ||
2915 | failed = imsm_count_failed(super, dev, MAP_1); | |
238c0a71 | 2916 | map_state = imsm_check_degraded(super, dev, failed, MAP_1); |
3b451610 AK |
2917 | if (map2->map_state != map_state) { |
2918 | map2->map_state = map_state; | |
2919 | super->updates_pending++; | |
2920 | } | |
2921 | } | |
2922 | } | |
b4ab44d8 | 2923 | #endif |
97b4d0e9 DW |
2924 | |
2925 | static struct imsm_disk *get_imsm_missing(struct intel_super *super, __u8 index) | |
2926 | { | |
2927 | struct dl *d; | |
2928 | ||
2929 | for (d = super->missing; d; d = d->next) | |
2930 | if (d->index == index) | |
2931 | return &d->disk; | |
2932 | return NULL; | |
2933 | } | |
2934 | ||
a5d85af7 | 2935 | static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map) |
4f5bc454 DW |
2936 | { |
2937 | struct intel_super *super = st->sb; | |
4f5bc454 | 2938 | struct imsm_disk *disk; |
a5d85af7 | 2939 | int map_disks = info->array.raid_disks; |
ab3cb6b3 N |
2940 | int max_enough = -1; |
2941 | int i; | |
2942 | struct imsm_super *mpb; | |
4f5bc454 | 2943 | |
bf5a934a | 2944 | if (super->current_vol >= 0) { |
a5d85af7 | 2945 | getinfo_super_imsm_volume(st, info, map); |
bf5a934a DW |
2946 | return; |
2947 | } | |
95eeceeb | 2948 | memset(info, 0, sizeof(*info)); |
d23fe947 DW |
2949 | |
2950 | /* Set raid_disks to zero so that Assemble will always pull in valid | |
2951 | * spares | |
2952 | */ | |
2953 | info->array.raid_disks = 0; | |
cdddbdbc DW |
2954 | info->array.level = LEVEL_CONTAINER; |
2955 | info->array.layout = 0; | |
2956 | info->array.md_minor = -1; | |
1011e834 | 2957 | info->array.ctime = 0; /* N/A for imsm */ |
cdddbdbc DW |
2958 | info->array.utime = 0; |
2959 | info->array.chunk_size = 0; | |
2960 | ||
2961 | info->disk.major = 0; | |
2962 | info->disk.minor = 0; | |
cdddbdbc | 2963 | info->disk.raid_disk = -1; |
c2c087e6 | 2964 | info->reshape_active = 0; |
f35f2525 N |
2965 | info->array.major_version = -1; |
2966 | info->array.minor_version = -2; | |
c2c087e6 | 2967 | strcpy(info->text_version, "imsm"); |
a67dd8cc | 2968 | info->safe_mode_delay = 0; |
c2c087e6 DW |
2969 | info->disk.number = -1; |
2970 | info->disk.state = 0; | |
c5afc314 | 2971 | info->name[0] = 0; |
921d9e16 | 2972 | info->recovery_start = MaxSector; |
3ad25638 | 2973 | info->recovery_blocked = imsm_reshape_blocks_arrays_changes(st->sb); |
c2c087e6 | 2974 | |
97b4d0e9 | 2975 | /* do we have the all the insync disks that we expect? */ |
ab3cb6b3 | 2976 | mpb = super->anchor; |
97b4d0e9 | 2977 | |
ab3cb6b3 N |
2978 | for (i = 0; i < mpb->num_raid_devs; i++) { |
2979 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
2980 | int failed, enough, j, missing = 0; | |
2981 | struct imsm_map *map; | |
2982 | __u8 state; | |
97b4d0e9 | 2983 | |
3b451610 AK |
2984 | failed = imsm_count_failed(super, dev, MAP_0); |
2985 | state = imsm_check_degraded(super, dev, failed, MAP_0); | |
238c0a71 | 2986 | map = get_imsm_map(dev, MAP_0); |
ab3cb6b3 N |
2987 | |
2988 | /* any newly missing disks? | |
2989 | * (catches single-degraded vs double-degraded) | |
2990 | */ | |
2991 | for (j = 0; j < map->num_members; j++) { | |
238c0a71 | 2992 | __u32 ord = get_imsm_ord_tbl_ent(dev, j, MAP_0); |
ab3cb6b3 N |
2993 | __u32 idx = ord_to_idx(ord); |
2994 | ||
2995 | if (!(ord & IMSM_ORD_REBUILD) && | |
2996 | get_imsm_missing(super, idx)) { | |
2997 | missing = 1; | |
2998 | break; | |
2999 | } | |
97b4d0e9 | 3000 | } |
ab3cb6b3 N |
3001 | |
3002 | if (state == IMSM_T_STATE_FAILED) | |
3003 | enough = -1; | |
3004 | else if (state == IMSM_T_STATE_DEGRADED && | |
3005 | (state != map->map_state || missing)) | |
3006 | enough = 0; | |
3007 | else /* we're normal, or already degraded */ | |
3008 | enough = 1; | |
d2bde6d3 AK |
3009 | if (is_gen_migration(dev) && missing) { |
3010 | /* during general migration we need all disks | |
3011 | * that process is running on. | |
3012 | * No new missing disk is allowed. | |
3013 | */ | |
3014 | max_enough = -1; | |
3015 | enough = -1; | |
3016 | /* no more checks necessary | |
3017 | */ | |
3018 | break; | |
3019 | } | |
ab3cb6b3 N |
3020 | /* in the missing/failed disk case check to see |
3021 | * if at least one array is runnable | |
3022 | */ | |
3023 | max_enough = max(max_enough, enough); | |
3024 | } | |
1ade5cc1 | 3025 | dprintf("enough: %d\n", max_enough); |
ab3cb6b3 | 3026 | info->container_enough = max_enough; |
97b4d0e9 | 3027 | |
4a04ec6c | 3028 | if (super->disks) { |
14e8215b DW |
3029 | __u32 reserved = imsm_reserved_sectors(super, super->disks); |
3030 | ||
b9f594fe | 3031 | disk = &super->disks->disk; |
5551b113 | 3032 | info->data_offset = total_blocks(&super->disks->disk) - reserved; |
14e8215b | 3033 | info->component_size = reserved; |
25ed7e59 | 3034 | info->disk.state = is_configured(disk) ? (1 << MD_DISK_ACTIVE) : 0; |
df474657 DW |
3035 | /* we don't change info->disk.raid_disk here because |
3036 | * this state will be finalized in mdmon after we have | |
3037 | * found the 'most fresh' version of the metadata | |
3038 | */ | |
25ed7e59 DW |
3039 | info->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0; |
3040 | info->disk.state |= is_spare(disk) ? 0 : (1 << MD_DISK_SYNC); | |
cdddbdbc | 3041 | } |
a575e2a7 DW |
3042 | |
3043 | /* only call uuid_from_super_imsm when this disk is part of a populated container, | |
3044 | * ->compare_super may have updated the 'num_raid_devs' field for spares | |
3045 | */ | |
3046 | if (info->disk.state & (1 << MD_DISK_SYNC) || super->anchor->num_raid_devs) | |
36ba7d48 | 3047 | uuid_from_super_imsm(st, info->uuid); |
22e263f6 AC |
3048 | else |
3049 | memcpy(info->uuid, uuid_zero, sizeof(uuid_zero)); | |
a5d85af7 N |
3050 | |
3051 | /* I don't know how to compute 'map' on imsm, so use safe default */ | |
3052 | if (map) { | |
3053 | int i; | |
3054 | for (i = 0; i < map_disks; i++) | |
3055 | map[i] = 1; | |
3056 | } | |
3057 | ||
cdddbdbc DW |
3058 | } |
3059 | ||
5c4cd5da AC |
3060 | /* allocates memory and fills disk in mdinfo structure |
3061 | * for each disk in array */ | |
3062 | struct mdinfo *getinfo_super_disks_imsm(struct supertype *st) | |
3063 | { | |
594dc1b8 | 3064 | struct mdinfo *mddev; |
5c4cd5da AC |
3065 | struct intel_super *super = st->sb; |
3066 | struct imsm_disk *disk; | |
3067 | int count = 0; | |
3068 | struct dl *dl; | |
3069 | if (!super || !super->disks) | |
3070 | return NULL; | |
3071 | dl = super->disks; | |
503975b9 | 3072 | mddev = xcalloc(1, sizeof(*mddev)); |
5c4cd5da AC |
3073 | while (dl) { |
3074 | struct mdinfo *tmp; | |
3075 | disk = &dl->disk; | |
503975b9 | 3076 | tmp = xcalloc(1, sizeof(*tmp)); |
5c4cd5da AC |
3077 | if (mddev->devs) |
3078 | tmp->next = mddev->devs; | |
3079 | mddev->devs = tmp; | |
3080 | tmp->disk.number = count++; | |
3081 | tmp->disk.major = dl->major; | |
3082 | tmp->disk.minor = dl->minor; | |
3083 | tmp->disk.state = is_configured(disk) ? | |
3084 | (1 << MD_DISK_ACTIVE) : 0; | |
3085 | tmp->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0; | |
3086 | tmp->disk.state |= is_spare(disk) ? 0 : (1 << MD_DISK_SYNC); | |
3087 | tmp->disk.raid_disk = -1; | |
3088 | dl = dl->next; | |
3089 | } | |
3090 | return mddev; | |
3091 | } | |
3092 | ||
cdddbdbc DW |
3093 | static int update_super_imsm(struct supertype *st, struct mdinfo *info, |
3094 | char *update, char *devname, int verbose, | |
3095 | int uuid_set, char *homehost) | |
3096 | { | |
f352c545 DW |
3097 | /* For 'assemble' and 'force' we need to return non-zero if any |
3098 | * change was made. For others, the return value is ignored. | |
3099 | * Update options are: | |
3100 | * force-one : This device looks a bit old but needs to be included, | |
3101 | * update age info appropriately. | |
3102 | * assemble: clear any 'faulty' flag to allow this device to | |
3103 | * be assembled. | |
3104 | * force-array: Array is degraded but being forced, mark it clean | |
3105 | * if that will be needed to assemble it. | |
3106 | * | |
3107 | * newdev: not used ???? | |
3108 | * grow: Array has gained a new device - this is currently for | |
3109 | * linear only | |
3110 | * resync: mark as dirty so a resync will happen. | |
3111 | * name: update the name - preserving the homehost | |
6e46bf34 | 3112 | * uuid: Change the uuid of the array to match watch is given |
f352c545 DW |
3113 | * |
3114 | * Following are not relevant for this imsm: | |
3115 | * sparc2.2 : update from old dodgey metadata | |
3116 | * super-minor: change the preferred_minor number | |
3117 | * summaries: update redundant counters. | |
f352c545 DW |
3118 | * homehost: update the recorded homehost |
3119 | * _reshape_progress: record new reshape_progress position. | |
3120 | */ | |
6e46bf34 DW |
3121 | int rv = 1; |
3122 | struct intel_super *super = st->sb; | |
3123 | struct imsm_super *mpb; | |
f352c545 | 3124 | |
6e46bf34 DW |
3125 | /* we can only update container info */ |
3126 | if (!super || super->current_vol >= 0 || !super->anchor) | |
3127 | return 1; | |
3128 | ||
3129 | mpb = super->anchor; | |
3130 | ||
81a5b4f5 N |
3131 | if (strcmp(update, "uuid") == 0) { |
3132 | /* We take this to mean that the family_num should be updated. | |
3133 | * However that is much smaller than the uuid so we cannot really | |
3134 | * allow an explicit uuid to be given. And it is hard to reliably | |
3135 | * know if one was. | |
3136 | * So if !uuid_set we know the current uuid is random and just used | |
3137 | * the first 'int' and copy it to the other 3 positions. | |
3138 | * Otherwise we require the 4 'int's to be the same as would be the | |
3139 | * case if we are using a random uuid. So an explicit uuid will be | |
3140 | * accepted as long as all for ints are the same... which shouldn't hurt | |
6e46bf34 | 3141 | */ |
81a5b4f5 N |
3142 | if (!uuid_set) { |
3143 | info->uuid[1] = info->uuid[2] = info->uuid[3] = info->uuid[0]; | |
6e46bf34 | 3144 | rv = 0; |
81a5b4f5 N |
3145 | } else { |
3146 | if (info->uuid[0] != info->uuid[1] || | |
3147 | info->uuid[1] != info->uuid[2] || | |
3148 | info->uuid[2] != info->uuid[3]) | |
3149 | rv = -1; | |
3150 | else | |
3151 | rv = 0; | |
6e46bf34 | 3152 | } |
81a5b4f5 N |
3153 | if (rv == 0) |
3154 | mpb->orig_family_num = info->uuid[0]; | |
6e46bf34 DW |
3155 | } else if (strcmp(update, "assemble") == 0) |
3156 | rv = 0; | |
3157 | else | |
1e2b2765 | 3158 | rv = -1; |
f352c545 | 3159 | |
6e46bf34 DW |
3160 | /* successful update? recompute checksum */ |
3161 | if (rv == 0) | |
3162 | mpb->check_sum = __le32_to_cpu(__gen_imsm_checksum(mpb)); | |
f352c545 DW |
3163 | |
3164 | return rv; | |
cdddbdbc DW |
3165 | } |
3166 | ||
c2c087e6 | 3167 | static size_t disks_to_mpb_size(int disks) |
cdddbdbc | 3168 | { |
c2c087e6 | 3169 | size_t size; |
cdddbdbc | 3170 | |
c2c087e6 DW |
3171 | size = sizeof(struct imsm_super); |
3172 | size += (disks - 1) * sizeof(struct imsm_disk); | |
3173 | size += 2 * sizeof(struct imsm_dev); | |
3174 | /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */ | |
3175 | size += (4 - 2) * sizeof(struct imsm_map); | |
3176 | /* 4 possible disk_ord_tbl's */ | |
3177 | size += 4 * (disks - 1) * sizeof(__u32); | |
3178 | ||
3179 | return size; | |
3180 | } | |
3181 | ||
387fcd59 N |
3182 | static __u64 avail_size_imsm(struct supertype *st, __u64 devsize, |
3183 | unsigned long long data_offset) | |
c2c087e6 DW |
3184 | { |
3185 | if (devsize < (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS)) | |
3186 | return 0; | |
3187 | ||
3188 | return devsize - (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS); | |
cdddbdbc DW |
3189 | } |
3190 | ||
ba2de7ba DW |
3191 | static void free_devlist(struct intel_super *super) |
3192 | { | |
3193 | struct intel_dev *dv; | |
3194 | ||
3195 | while (super->devlist) { | |
3196 | dv = super->devlist->next; | |
3197 | free(super->devlist->dev); | |
3198 | free(super->devlist); | |
3199 | super->devlist = dv; | |
3200 | } | |
3201 | } | |
3202 | ||
3203 | static void imsm_copy_dev(struct imsm_dev *dest, struct imsm_dev *src) | |
3204 | { | |
3205 | memcpy(dest, src, sizeof_imsm_dev(src, 0)); | |
3206 | } | |
3207 | ||
cdddbdbc DW |
3208 | static int compare_super_imsm(struct supertype *st, struct supertype *tst) |
3209 | { | |
3210 | /* | |
3211 | * return: | |
3212 | * 0 same, or first was empty, and second was copied | |
3213 | * 1 second had wrong number | |
3214 | * 2 wrong uuid | |
3215 | * 3 wrong other info | |
3216 | */ | |
3217 | struct intel_super *first = st->sb; | |
3218 | struct intel_super *sec = tst->sb; | |
3219 | ||
5d500228 N |
3220 | if (!first) { |
3221 | st->sb = tst->sb; | |
3222 | tst->sb = NULL; | |
3223 | return 0; | |
3224 | } | |
8603ea6f LM |
3225 | /* in platform dependent environment test if the disks |
3226 | * use the same Intel hba | |
cb8f6859 | 3227 | * If not on Intel hba at all, allow anything. |
8603ea6f | 3228 | */ |
6b781d33 AP |
3229 | if (!check_env("IMSM_NO_PLATFORM") && first->hba && sec->hba) { |
3230 | if (first->hba->type != sec->hba->type) { | |
8603ea6f | 3231 | fprintf(stderr, |
6b781d33 AP |
3232 | "HBAs of devices do not match %s != %s\n", |
3233 | get_sys_dev_type(first->hba->type), | |
3234 | get_sys_dev_type(sec->hba->type)); | |
3235 | return 3; | |
3236 | } | |
3237 | if (first->orom != sec->orom) { | |
3238 | fprintf(stderr, | |
3239 | "HBAs of devices do not match %s != %s\n", | |
3240 | first->hba->pci_id, sec->hba->pci_id); | |
8603ea6f LM |
3241 | return 3; |
3242 | } | |
3243 | } | |
cdddbdbc | 3244 | |
d23fe947 DW |
3245 | /* if an anchor does not have num_raid_devs set then it is a free |
3246 | * floating spare | |
3247 | */ | |
3248 | if (first->anchor->num_raid_devs > 0 && | |
3249 | sec->anchor->num_raid_devs > 0) { | |
a2b97981 DW |
3250 | /* Determine if these disks might ever have been |
3251 | * related. Further disambiguation can only take place | |
3252 | * in load_super_imsm_all | |
3253 | */ | |
3254 | __u32 first_family = first->anchor->orig_family_num; | |
3255 | __u32 sec_family = sec->anchor->orig_family_num; | |
3256 | ||
f796af5d DW |
3257 | if (memcmp(first->anchor->sig, sec->anchor->sig, |
3258 | MAX_SIGNATURE_LENGTH) != 0) | |
3259 | return 3; | |
3260 | ||
a2b97981 DW |
3261 | if (first_family == 0) |
3262 | first_family = first->anchor->family_num; | |
3263 | if (sec_family == 0) | |
3264 | sec_family = sec->anchor->family_num; | |
3265 | ||
3266 | if (first_family != sec_family) | |
d23fe947 | 3267 | return 3; |
f796af5d | 3268 | |
d23fe947 | 3269 | } |
cdddbdbc | 3270 | |
3e372e5a DW |
3271 | /* if 'first' is a spare promote it to a populated mpb with sec's |
3272 | * family number | |
3273 | */ | |
3274 | if (first->anchor->num_raid_devs == 0 && | |
3275 | sec->anchor->num_raid_devs > 0) { | |
78d30f94 | 3276 | int i; |
ba2de7ba DW |
3277 | struct intel_dev *dv; |
3278 | struct imsm_dev *dev; | |
78d30f94 DW |
3279 | |
3280 | /* we need to copy raid device info from sec if an allocation | |
3281 | * fails here we don't associate the spare | |
3282 | */ | |
3283 | for (i = 0; i < sec->anchor->num_raid_devs; i++) { | |
503975b9 N |
3284 | dv = xmalloc(sizeof(*dv)); |
3285 | dev = xmalloc(sizeof_imsm_dev(get_imsm_dev(sec, i), 1)); | |
ba2de7ba DW |
3286 | dv->dev = dev; |
3287 | dv->index = i; | |
3288 | dv->next = first->devlist; | |
3289 | first->devlist = dv; | |
78d30f94 | 3290 | } |
709743c5 | 3291 | if (i < sec->anchor->num_raid_devs) { |
ba2de7ba DW |
3292 | /* allocation failure */ |
3293 | free_devlist(first); | |
e12b3daa | 3294 | pr_err("imsm: failed to associate spare\n"); |
ba2de7ba | 3295 | return 3; |
78d30f94 | 3296 | } |
3e372e5a | 3297 | first->anchor->num_raid_devs = sec->anchor->num_raid_devs; |
148acb7b | 3298 | first->anchor->orig_family_num = sec->anchor->orig_family_num; |
3e372e5a | 3299 | first->anchor->family_num = sec->anchor->family_num; |
ac6449be | 3300 | memcpy(first->anchor->sig, sec->anchor->sig, MAX_SIGNATURE_LENGTH); |
709743c5 DW |
3301 | for (i = 0; i < sec->anchor->num_raid_devs; i++) |
3302 | imsm_copy_dev(get_imsm_dev(first, i), get_imsm_dev(sec, i)); | |
3e372e5a DW |
3303 | } |
3304 | ||
cdddbdbc DW |
3305 | return 0; |
3306 | } | |
3307 | ||
0030e8d6 DW |
3308 | static void fd2devname(int fd, char *name) |
3309 | { | |
3310 | struct stat st; | |
3311 | char path[256]; | |
33a6535d | 3312 | char dname[PATH_MAX]; |
0030e8d6 DW |
3313 | char *nm; |
3314 | int rv; | |
3315 | ||
3316 | name[0] = '\0'; | |
3317 | if (fstat(fd, &st) != 0) | |
3318 | return; | |
3319 | sprintf(path, "/sys/dev/block/%d:%d", | |
3320 | major(st.st_rdev), minor(st.st_rdev)); | |
3321 | ||
9cf014ec | 3322 | rv = readlink(path, dname, sizeof(dname)-1); |
0030e8d6 DW |
3323 | if (rv <= 0) |
3324 | return; | |
9587c373 | 3325 | |
0030e8d6 DW |
3326 | dname[rv] = '\0'; |
3327 | nm = strrchr(dname, '/'); | |
7897de29 JS |
3328 | if (nm) { |
3329 | nm++; | |
3330 | snprintf(name, MAX_RAID_SERIAL_LEN, "/dev/%s", nm); | |
3331 | } | |
0030e8d6 DW |
3332 | } |
3333 | ||
cdddbdbc DW |
3334 | extern int scsi_get_serial(int fd, void *buf, size_t buf_len); |
3335 | ||
3336 | static int imsm_read_serial(int fd, char *devname, | |
3337 | __u8 serial[MAX_RAID_SERIAL_LEN]) | |
3338 | { | |
3339 | unsigned char scsi_serial[255]; | |
cdddbdbc DW |
3340 | int rv; |
3341 | int rsp_len; | |
1f24f035 | 3342 | int len; |
316e2bf4 DW |
3343 | char *dest; |
3344 | char *src; | |
3345 | char *rsp_buf; | |
3346 | int i; | |
cdddbdbc DW |
3347 | |
3348 | memset(scsi_serial, 0, sizeof(scsi_serial)); | |
cdddbdbc | 3349 | |
f9ba0ff1 DW |
3350 | rv = scsi_get_serial(fd, scsi_serial, sizeof(scsi_serial)); |
3351 | ||
40ebbb9c | 3352 | if (rv && check_env("IMSM_DEVNAME_AS_SERIAL")) { |
f9ba0ff1 DW |
3353 | memset(serial, 0, MAX_RAID_SERIAL_LEN); |
3354 | fd2devname(fd, (char *) serial); | |
0030e8d6 DW |
3355 | return 0; |
3356 | } | |
3357 | ||
cdddbdbc DW |
3358 | if (rv != 0) { |
3359 | if (devname) | |
e7b84f9d N |
3360 | pr_err("Failed to retrieve serial for %s\n", |
3361 | devname); | |
cdddbdbc DW |
3362 | return rv; |
3363 | } | |
3364 | ||
3365 | rsp_len = scsi_serial[3]; | |
03cd4cc8 DW |
3366 | if (!rsp_len) { |
3367 | if (devname) | |
e7b84f9d N |
3368 | pr_err("Failed to retrieve serial for %s\n", |
3369 | devname); | |
03cd4cc8 DW |
3370 | return 2; |
3371 | } | |
1f24f035 | 3372 | rsp_buf = (char *) &scsi_serial[4]; |
5c3db629 | 3373 | |
316e2bf4 DW |
3374 | /* trim all whitespace and non-printable characters and convert |
3375 | * ':' to ';' | |
3376 | */ | |
3377 | for (i = 0, dest = rsp_buf; i < rsp_len; i++) { | |
3378 | src = &rsp_buf[i]; | |
3379 | if (*src > 0x20) { | |
3380 | /* ':' is reserved for use in placeholder serial | |
3381 | * numbers for missing disks | |
3382 | */ | |
3383 | if (*src == ':') | |
3384 | *dest++ = ';'; | |
3385 | else | |
3386 | *dest++ = *src; | |
3387 | } | |
3388 | } | |
3389 | len = dest - rsp_buf; | |
3390 | dest = rsp_buf; | |
3391 | ||
3392 | /* truncate leading characters */ | |
3393 | if (len > MAX_RAID_SERIAL_LEN) { | |
3394 | dest += len - MAX_RAID_SERIAL_LEN; | |
1f24f035 | 3395 | len = MAX_RAID_SERIAL_LEN; |
316e2bf4 | 3396 | } |
5c3db629 | 3397 | |
5c3db629 | 3398 | memset(serial, 0, MAX_RAID_SERIAL_LEN); |
316e2bf4 | 3399 | memcpy(serial, dest, len); |
cdddbdbc DW |
3400 | |
3401 | return 0; | |
3402 | } | |
3403 | ||
1f24f035 DW |
3404 | static int serialcmp(__u8 *s1, __u8 *s2) |
3405 | { | |
3406 | return strncmp((char *) s1, (char *) s2, MAX_RAID_SERIAL_LEN); | |
3407 | } | |
3408 | ||
3409 | static void serialcpy(__u8 *dest, __u8 *src) | |
3410 | { | |
3411 | strncpy((char *) dest, (char *) src, MAX_RAID_SERIAL_LEN); | |
3412 | } | |
3413 | ||
54c2c1ea DW |
3414 | static struct dl *serial_to_dl(__u8 *serial, struct intel_super *super) |
3415 | { | |
3416 | struct dl *dl; | |
3417 | ||
3418 | for (dl = super->disks; dl; dl = dl->next) | |
3419 | if (serialcmp(dl->serial, serial) == 0) | |
3420 | break; | |
3421 | ||
3422 | return dl; | |
3423 | } | |
3424 | ||
a2b97981 DW |
3425 | static struct imsm_disk * |
3426 | __serial_to_disk(__u8 *serial, struct imsm_super *mpb, int *idx) | |
3427 | { | |
3428 | int i; | |
3429 | ||
3430 | for (i = 0; i < mpb->num_disks; i++) { | |
3431 | struct imsm_disk *disk = __get_imsm_disk(mpb, i); | |
3432 | ||
3433 | if (serialcmp(disk->serial, serial) == 0) { | |
3434 | if (idx) | |
3435 | *idx = i; | |
3436 | return disk; | |
3437 | } | |
3438 | } | |
3439 | ||
3440 | return NULL; | |
3441 | } | |
3442 | ||
cdddbdbc DW |
3443 | static int |
3444 | load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd) | |
3445 | { | |
a2b97981 | 3446 | struct imsm_disk *disk; |
cdddbdbc DW |
3447 | struct dl *dl; |
3448 | struct stat stb; | |
cdddbdbc | 3449 | int rv; |
a2b97981 | 3450 | char name[40]; |
d23fe947 DW |
3451 | __u8 serial[MAX_RAID_SERIAL_LEN]; |
3452 | ||
3453 | rv = imsm_read_serial(fd, devname, serial); | |
3454 | ||
3455 | if (rv != 0) | |
3456 | return 2; | |
3457 | ||
503975b9 | 3458 | dl = xcalloc(1, sizeof(*dl)); |
cdddbdbc | 3459 | |
a2b97981 DW |
3460 | fstat(fd, &stb); |
3461 | dl->major = major(stb.st_rdev); | |
3462 | dl->minor = minor(stb.st_rdev); | |
3463 | dl->next = super->disks; | |
3464 | dl->fd = keep_fd ? fd : -1; | |
3465 | assert(super->disks == NULL); | |
3466 | super->disks = dl; | |
3467 | serialcpy(dl->serial, serial); | |
3468 | dl->index = -2; | |
3469 | dl->e = NULL; | |
3470 | fd2devname(fd, name); | |
3471 | if (devname) | |
503975b9 | 3472 | dl->devname = xstrdup(devname); |
a2b97981 | 3473 | else |
503975b9 | 3474 | dl->devname = xstrdup(name); |
cdddbdbc | 3475 | |
d23fe947 | 3476 | /* look up this disk's index in the current anchor */ |
a2b97981 DW |
3477 | disk = __serial_to_disk(dl->serial, super->anchor, &dl->index); |
3478 | if (disk) { | |
3479 | dl->disk = *disk; | |
3480 | /* only set index on disks that are a member of a | |
3481 | * populated contianer, i.e. one with raid_devs | |
3482 | */ | |
3483 | if (is_failed(&dl->disk)) | |
3f6efecc | 3484 | dl->index = -2; |
a2b97981 DW |
3485 | else if (is_spare(&dl->disk)) |
3486 | dl->index = -1; | |
3f6efecc DW |
3487 | } |
3488 | ||
949c47a0 DW |
3489 | return 0; |
3490 | } | |
3491 | ||
0e600426 | 3492 | #ifndef MDASSEMBLE |
0c046afd DW |
3493 | /* When migrating map0 contains the 'destination' state while map1 |
3494 | * contains the current state. When not migrating map0 contains the | |
3495 | * current state. This routine assumes that map[0].map_state is set to | |
3496 | * the current array state before being called. | |
3497 | * | |
3498 | * Migration is indicated by one of the following states | |
3499 | * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed) | |
e3bba0e0 | 3500 | * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal |
0c046afd | 3501 | * map1state=unitialized) |
1484e727 | 3502 | * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal |
0c046afd | 3503 | * map1state=normal) |
e3bba0e0 | 3504 | * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal |
0c046afd | 3505 | * map1state=degraded) |
8e59f3d8 AK |
3506 | * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal |
3507 | * map1state=normal) | |
0c046afd | 3508 | */ |
8e59f3d8 AK |
3509 | static void migrate(struct imsm_dev *dev, struct intel_super *super, |
3510 | __u8 to_state, int migr_type) | |
3393c6af | 3511 | { |
0c046afd | 3512 | struct imsm_map *dest; |
238c0a71 | 3513 | struct imsm_map *src = get_imsm_map(dev, MAP_0); |
3393c6af | 3514 | |
0c046afd | 3515 | dev->vol.migr_state = 1; |
1484e727 | 3516 | set_migr_type(dev, migr_type); |
f8f603f1 | 3517 | dev->vol.curr_migr_unit = 0; |
238c0a71 | 3518 | dest = get_imsm_map(dev, MAP_1); |
0c046afd | 3519 | |
0556e1a2 | 3520 | /* duplicate and then set the target end state in map[0] */ |
3393c6af | 3521 | memcpy(dest, src, sizeof_imsm_map(src)); |
28bce06f AK |
3522 | if ((migr_type == MIGR_REBUILD) || |
3523 | (migr_type == MIGR_GEN_MIGR)) { | |
0556e1a2 DW |
3524 | __u32 ord; |
3525 | int i; | |
3526 | ||
3527 | for (i = 0; i < src->num_members; i++) { | |
3528 | ord = __le32_to_cpu(src->disk_ord_tbl[i]); | |
3529 | set_imsm_ord_tbl_ent(src, i, ord_to_idx(ord)); | |
3530 | } | |
3531 | } | |
3532 | ||
8e59f3d8 AK |
3533 | if (migr_type == MIGR_GEN_MIGR) |
3534 | /* Clear migration record */ | |
3535 | memset(super->migr_rec, 0, sizeof(struct migr_record)); | |
3536 | ||
0c046afd | 3537 | src->map_state = to_state; |
949c47a0 | 3538 | } |
f8f603f1 | 3539 | |
809da78e AK |
3540 | static void end_migration(struct imsm_dev *dev, struct intel_super *super, |
3541 | __u8 map_state) | |
f8f603f1 | 3542 | { |
238c0a71 AK |
3543 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
3544 | struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state == 0 ? | |
3545 | MAP_0 : MAP_1); | |
28bce06f | 3546 | int i, j; |
0556e1a2 DW |
3547 | |
3548 | /* merge any IMSM_ORD_REBUILD bits that were not successfully | |
3549 | * completed in the last migration. | |
3550 | * | |
28bce06f | 3551 | * FIXME add support for raid-level-migration |
0556e1a2 | 3552 | */ |
809da78e AK |
3553 | if ((map_state != map->map_state) && (is_gen_migration(dev) == 0) && |
3554 | (prev->map_state != IMSM_T_STATE_UNINITIALIZED)) { | |
3555 | /* when final map state is other than expected | |
3556 | * merge maps (not for migration) | |
3557 | */ | |
3558 | int failed; | |
3559 | ||
3560 | for (i = 0; i < prev->num_members; i++) | |
3561 | for (j = 0; j < map->num_members; j++) | |
3562 | /* during online capacity expansion | |
3563 | * disks position can be changed | |
3564 | * if takeover is used | |
3565 | */ | |
3566 | if (ord_to_idx(map->disk_ord_tbl[j]) == | |
3567 | ord_to_idx(prev->disk_ord_tbl[i])) { | |
3568 | map->disk_ord_tbl[j] |= | |
3569 | prev->disk_ord_tbl[i]; | |
3570 | break; | |
3571 | } | |
3572 | failed = imsm_count_failed(super, dev, MAP_0); | |
3573 | map_state = imsm_check_degraded(super, dev, failed, MAP_0); | |
3574 | } | |
f8f603f1 DW |
3575 | |
3576 | dev->vol.migr_state = 0; | |
ea672ee1 | 3577 | set_migr_type(dev, 0); |
f8f603f1 DW |
3578 | dev->vol.curr_migr_unit = 0; |
3579 | map->map_state = map_state; | |
3580 | } | |
0e600426 | 3581 | #endif |
949c47a0 DW |
3582 | |
3583 | static int parse_raid_devices(struct intel_super *super) | |
3584 | { | |
3585 | int i; | |
3586 | struct imsm_dev *dev_new; | |
4d7b1503 | 3587 | size_t len, len_migr; |
401d313b | 3588 | size_t max_len = 0; |
4d7b1503 DW |
3589 | size_t space_needed = 0; |
3590 | struct imsm_super *mpb = super->anchor; | |
949c47a0 DW |
3591 | |
3592 | for (i = 0; i < super->anchor->num_raid_devs; i++) { | |
3593 | struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i); | |
ba2de7ba | 3594 | struct intel_dev *dv; |
949c47a0 | 3595 | |
4d7b1503 DW |
3596 | len = sizeof_imsm_dev(dev_iter, 0); |
3597 | len_migr = sizeof_imsm_dev(dev_iter, 1); | |
3598 | if (len_migr > len) | |
3599 | space_needed += len_migr - len; | |
ca9de185 | 3600 | |
503975b9 | 3601 | dv = xmalloc(sizeof(*dv)); |
401d313b AK |
3602 | if (max_len < len_migr) |
3603 | max_len = len_migr; | |
3604 | if (max_len > len_migr) | |
3605 | space_needed += max_len - len_migr; | |
503975b9 | 3606 | dev_new = xmalloc(max_len); |
949c47a0 | 3607 | imsm_copy_dev(dev_new, dev_iter); |
ba2de7ba DW |
3608 | dv->dev = dev_new; |
3609 | dv->index = i; | |
3610 | dv->next = super->devlist; | |
3611 | super->devlist = dv; | |
949c47a0 | 3612 | } |
cdddbdbc | 3613 | |
4d7b1503 DW |
3614 | /* ensure that super->buf is large enough when all raid devices |
3615 | * are migrating | |
3616 | */ | |
3617 | if (__le32_to_cpu(mpb->mpb_size) + space_needed > super->len) { | |
3618 | void *buf; | |
3619 | ||
3620 | len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + space_needed, 512); | |
3621 | if (posix_memalign(&buf, 512, len) != 0) | |
3622 | return 1; | |
3623 | ||
1f45a8ad DW |
3624 | memcpy(buf, super->buf, super->len); |
3625 | memset(buf + super->len, 0, len - super->len); | |
4d7b1503 DW |
3626 | free(super->buf); |
3627 | super->buf = buf; | |
3628 | super->len = len; | |
3629 | } | |
ca9de185 | 3630 | |
cdddbdbc DW |
3631 | return 0; |
3632 | } | |
3633 | ||
604b746f JD |
3634 | /* retrieve a pointer to the bbm log which starts after all raid devices */ |
3635 | struct bbm_log *__get_imsm_bbm_log(struct imsm_super *mpb) | |
3636 | { | |
3637 | void *ptr = NULL; | |
3638 | ||
3639 | if (__le32_to_cpu(mpb->bbm_log_size)) { | |
3640 | ptr = mpb; | |
3641 | ptr += mpb->mpb_size - __le32_to_cpu(mpb->bbm_log_size); | |
1011e834 | 3642 | } |
604b746f JD |
3643 | |
3644 | return ptr; | |
3645 | } | |
3646 | ||
e2f41b2c AK |
3647 | /******************************************************************************* |
3648 | * Function: check_mpb_migr_compatibility | |
3649 | * Description: Function checks for unsupported migration features: | |
3650 | * - migration optimization area (pba_of_lba0) | |
3651 | * - descending reshape (ascending_migr) | |
3652 | * Parameters: | |
3653 | * super : imsm metadata information | |
3654 | * Returns: | |
3655 | * 0 : migration is compatible | |
3656 | * -1 : migration is not compatible | |
3657 | ******************************************************************************/ | |
3658 | int check_mpb_migr_compatibility(struct intel_super *super) | |
3659 | { | |
3660 | struct imsm_map *map0, *map1; | |
3661 | struct migr_record *migr_rec = super->migr_rec; | |
3662 | int i; | |
3663 | ||
3664 | for (i = 0; i < super->anchor->num_raid_devs; i++) { | |
3665 | struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i); | |
3666 | ||
3667 | if (dev_iter && | |
3668 | dev_iter->vol.migr_state == 1 && | |
3669 | dev_iter->vol.migr_type == MIGR_GEN_MIGR) { | |
3670 | /* This device is migrating */ | |
238c0a71 AK |
3671 | map0 = get_imsm_map(dev_iter, MAP_0); |
3672 | map1 = get_imsm_map(dev_iter, MAP_1); | |
5551b113 | 3673 | if (pba_of_lba0(map0) != pba_of_lba0(map1)) |
e2f41b2c AK |
3674 | /* migration optimization area was used */ |
3675 | return -1; | |
3676 | if (migr_rec->ascending_migr == 0 | |
3677 | && migr_rec->dest_depth_per_unit > 0) | |
3678 | /* descending reshape not supported yet */ | |
3679 | return -1; | |
3680 | } | |
3681 | } | |
3682 | return 0; | |
3683 | } | |
3684 | ||
d23fe947 | 3685 | static void __free_imsm(struct intel_super *super, int free_disks); |
9ca2c81c | 3686 | |
cdddbdbc | 3687 | /* load_imsm_mpb - read matrix metadata |
f2f5c343 | 3688 | * allocates super->mpb to be freed by free_imsm |
cdddbdbc DW |
3689 | */ |
3690 | static int load_imsm_mpb(int fd, struct intel_super *super, char *devname) | |
3691 | { | |
3692 | unsigned long long dsize; | |
cdddbdbc DW |
3693 | unsigned long long sectors; |
3694 | struct stat; | |
6416d527 | 3695 | struct imsm_super *anchor; |
cdddbdbc DW |
3696 | __u32 check_sum; |
3697 | ||
cdddbdbc | 3698 | get_dev_size(fd, NULL, &dsize); |
64436f06 N |
3699 | if (dsize < 1024) { |
3700 | if (devname) | |
e7b84f9d N |
3701 | pr_err("%s: device to small for imsm\n", |
3702 | devname); | |
64436f06 N |
3703 | return 1; |
3704 | } | |
cdddbdbc DW |
3705 | |
3706 | if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0) { | |
3707 | if (devname) | |
e7b84f9d N |
3708 | pr_err("Cannot seek to anchor block on %s: %s\n", |
3709 | devname, strerror(errno)); | |
cdddbdbc DW |
3710 | return 1; |
3711 | } | |
3712 | ||
949c47a0 | 3713 | if (posix_memalign((void**)&anchor, 512, 512) != 0) { |
ad97895e | 3714 | if (devname) |
7a862a02 | 3715 | pr_err("Failed to allocate imsm anchor buffer on %s\n", devname); |
ad97895e DW |
3716 | return 1; |
3717 | } | |
949c47a0 | 3718 | if (read(fd, anchor, 512) != 512) { |
cdddbdbc | 3719 | if (devname) |
e7b84f9d N |
3720 | pr_err("Cannot read anchor block on %s: %s\n", |
3721 | devname, strerror(errno)); | |
6416d527 | 3722 | free(anchor); |
cdddbdbc DW |
3723 | return 1; |
3724 | } | |
3725 | ||
6416d527 | 3726 | if (strncmp((char *) anchor->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) { |
cdddbdbc | 3727 | if (devname) |
e7b84f9d | 3728 | pr_err("no IMSM anchor on %s\n", devname); |
6416d527 | 3729 | free(anchor); |
cdddbdbc DW |
3730 | return 2; |
3731 | } | |
3732 | ||
d23fe947 | 3733 | __free_imsm(super, 0); |
f2f5c343 LM |
3734 | /* reload capability and hba */ |
3735 | ||
3736 | /* capability and hba must be updated with new super allocation */ | |
d424212e | 3737 | find_intel_hba_capability(fd, super, devname); |
949c47a0 DW |
3738 | super->len = ROUND_UP(anchor->mpb_size, 512); |
3739 | if (posix_memalign(&super->buf, 512, super->len) != 0) { | |
cdddbdbc | 3740 | if (devname) |
e7b84f9d N |
3741 | pr_err("unable to allocate %zu byte mpb buffer\n", |
3742 | super->len); | |
6416d527 | 3743 | free(anchor); |
cdddbdbc DW |
3744 | return 2; |
3745 | } | |
949c47a0 | 3746 | memcpy(super->buf, anchor, 512); |
cdddbdbc | 3747 | |
6416d527 NB |
3748 | sectors = mpb_sectors(anchor) - 1; |
3749 | free(anchor); | |
8e59f3d8 | 3750 | |
17a4eaf9 | 3751 | if (posix_memalign(&super->migr_rec_buf, 512, MIGR_REC_BUF_SIZE) != 0) { |
1ade5cc1 | 3752 | pr_err("could not allocate migr_rec buffer\n"); |
8e59f3d8 AK |
3753 | free(super->buf); |
3754 | return 2; | |
3755 | } | |
51d83f5d | 3756 | super->clean_migration_record_by_mdmon = 0; |
8e59f3d8 | 3757 | |
949c47a0 | 3758 | if (!sectors) { |
ecf45690 DW |
3759 | check_sum = __gen_imsm_checksum(super->anchor); |
3760 | if (check_sum != __le32_to_cpu(super->anchor->check_sum)) { | |
3761 | if (devname) | |
e7b84f9d N |
3762 | pr_err("IMSM checksum %x != %x on %s\n", |
3763 | check_sum, | |
3764 | __le32_to_cpu(super->anchor->check_sum), | |
3765 | devname); | |
ecf45690 DW |
3766 | return 2; |
3767 | } | |
3768 | ||
a2b97981 | 3769 | return 0; |
949c47a0 | 3770 | } |
cdddbdbc DW |
3771 | |
3772 | /* read the extended mpb */ | |
3773 | if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0) { | |
3774 | if (devname) | |
e7b84f9d N |
3775 | pr_err("Cannot seek to extended mpb on %s: %s\n", |
3776 | devname, strerror(errno)); | |
cdddbdbc DW |
3777 | return 1; |
3778 | } | |
3779 | ||
f21e18ca | 3780 | if ((unsigned)read(fd, super->buf + 512, super->len - 512) != super->len - 512) { |
cdddbdbc | 3781 | if (devname) |
e7b84f9d N |
3782 | pr_err("Cannot read extended mpb on %s: %s\n", |
3783 | devname, strerror(errno)); | |
cdddbdbc DW |
3784 | return 2; |
3785 | } | |
3786 | ||
949c47a0 DW |
3787 | check_sum = __gen_imsm_checksum(super->anchor); |
3788 | if (check_sum != __le32_to_cpu(super->anchor->check_sum)) { | |
cdddbdbc | 3789 | if (devname) |
e7b84f9d N |
3790 | pr_err("IMSM checksum %x != %x on %s\n", |
3791 | check_sum, __le32_to_cpu(super->anchor->check_sum), | |
3792 | devname); | |
db575f3b | 3793 | return 3; |
cdddbdbc DW |
3794 | } |
3795 | ||
604b746f JD |
3796 | /* FIXME the BBM log is disk specific so we cannot use this global |
3797 | * buffer for all disks. Ok for now since we only look at the global | |
3798 | * bbm_log_size parameter to gate assembly | |
3799 | */ | |
3800 | super->bbm_log = __get_imsm_bbm_log(super->anchor); | |
3801 | ||
a2b97981 DW |
3802 | return 0; |
3803 | } | |
3804 | ||
8e59f3d8 AK |
3805 | static int read_imsm_migr_rec(int fd, struct intel_super *super); |
3806 | ||
97f81ee2 CA |
3807 | /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */ |
3808 | static void clear_hi(struct intel_super *super) | |
3809 | { | |
3810 | struct imsm_super *mpb = super->anchor; | |
3811 | int i, n; | |
3812 | if (mpb->attributes & MPB_ATTRIB_2TB_DISK) | |
3813 | return; | |
3814 | for (i = 0; i < mpb->num_disks; ++i) { | |
3815 | struct imsm_disk *disk = &mpb->disk[i]; | |
3816 | disk->total_blocks_hi = 0; | |
3817 | } | |
3818 | for (i = 0; i < mpb->num_raid_devs; ++i) { | |
3819 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
3820 | if (!dev) | |
3821 | return; | |
3822 | for (n = 0; n < 2; ++n) { | |
3823 | struct imsm_map *map = get_imsm_map(dev, n); | |
3824 | if (!map) | |
3825 | continue; | |
3826 | map->pba_of_lba0_hi = 0; | |
3827 | map->blocks_per_member_hi = 0; | |
3828 | map->num_data_stripes_hi = 0; | |
3829 | } | |
3830 | } | |
3831 | } | |
3832 | ||
a2b97981 DW |
3833 | static int |
3834 | load_and_parse_mpb(int fd, struct intel_super *super, char *devname, int keep_fd) | |
3835 | { | |
3836 | int err; | |
3837 | ||
3838 | err = load_imsm_mpb(fd, super, devname); | |
3839 | if (err) | |
3840 | return err; | |
3841 | err = load_imsm_disk(fd, super, devname, keep_fd); | |
3842 | if (err) | |
3843 | return err; | |
3844 | err = parse_raid_devices(super); | |
97f81ee2 | 3845 | clear_hi(super); |
a2b97981 | 3846 | return err; |
cdddbdbc DW |
3847 | } |
3848 | ||
ae6aad82 DW |
3849 | static void __free_imsm_disk(struct dl *d) |
3850 | { | |
3851 | if (d->fd >= 0) | |
3852 | close(d->fd); | |
3853 | if (d->devname) | |
3854 | free(d->devname); | |
0dcecb2e DW |
3855 | if (d->e) |
3856 | free(d->e); | |
ae6aad82 DW |
3857 | free(d); |
3858 | ||
3859 | } | |
1a64be56 | 3860 | |
cdddbdbc DW |
3861 | static void free_imsm_disks(struct intel_super *super) |
3862 | { | |
47ee5a45 | 3863 | struct dl *d; |
cdddbdbc | 3864 | |
47ee5a45 DW |
3865 | while (super->disks) { |
3866 | d = super->disks; | |
cdddbdbc | 3867 | super->disks = d->next; |
ae6aad82 | 3868 | __free_imsm_disk(d); |
cdddbdbc | 3869 | } |
cb82edca AK |
3870 | while (super->disk_mgmt_list) { |
3871 | d = super->disk_mgmt_list; | |
3872 | super->disk_mgmt_list = d->next; | |
3873 | __free_imsm_disk(d); | |
3874 | } | |
47ee5a45 DW |
3875 | while (super->missing) { |
3876 | d = super->missing; | |
3877 | super->missing = d->next; | |
3878 | __free_imsm_disk(d); | |
3879 | } | |
3880 | ||
cdddbdbc DW |
3881 | } |
3882 | ||
9ca2c81c | 3883 | /* free all the pieces hanging off of a super pointer */ |
d23fe947 | 3884 | static void __free_imsm(struct intel_super *super, int free_disks) |
cdddbdbc | 3885 | { |
88654014 LM |
3886 | struct intel_hba *elem, *next; |
3887 | ||
9ca2c81c | 3888 | if (super->buf) { |
949c47a0 | 3889 | free(super->buf); |
9ca2c81c DW |
3890 | super->buf = NULL; |
3891 | } | |
f2f5c343 LM |
3892 | /* unlink capability description */ |
3893 | super->orom = NULL; | |
8e59f3d8 AK |
3894 | if (super->migr_rec_buf) { |
3895 | free(super->migr_rec_buf); | |
3896 | super->migr_rec_buf = NULL; | |
3897 | } | |
d23fe947 DW |
3898 | if (free_disks) |
3899 | free_imsm_disks(super); | |
ba2de7ba | 3900 | free_devlist(super); |
88654014 LM |
3901 | elem = super->hba; |
3902 | while (elem) { | |
3903 | if (elem->path) | |
3904 | free((void *)elem->path); | |
3905 | next = elem->next; | |
3906 | free(elem); | |
3907 | elem = next; | |
88c32bb1 | 3908 | } |
88654014 | 3909 | super->hba = NULL; |
cdddbdbc DW |
3910 | } |
3911 | ||
9ca2c81c DW |
3912 | static void free_imsm(struct intel_super *super) |
3913 | { | |
d23fe947 | 3914 | __free_imsm(super, 1); |
9ca2c81c DW |
3915 | free(super); |
3916 | } | |
cdddbdbc DW |
3917 | |
3918 | static void free_super_imsm(struct supertype *st) | |
3919 | { | |
3920 | struct intel_super *super = st->sb; | |
3921 | ||
3922 | if (!super) | |
3923 | return; | |
3924 | ||
3925 | free_imsm(super); | |
3926 | st->sb = NULL; | |
3927 | } | |
3928 | ||
49133e57 | 3929 | static struct intel_super *alloc_super(void) |
c2c087e6 | 3930 | { |
503975b9 | 3931 | struct intel_super *super = xcalloc(1, sizeof(*super)); |
c2c087e6 | 3932 | |
503975b9 N |
3933 | super->current_vol = -1; |
3934 | super->create_offset = ~((unsigned long long) 0); | |
c2c087e6 DW |
3935 | return super; |
3936 | } | |
3937 | ||
f0f5a016 LM |
3938 | /* |
3939 | * find and allocate hba and OROM/EFI based on valid fd of RAID component device | |
3940 | */ | |
d424212e | 3941 | static int find_intel_hba_capability(int fd, struct intel_super *super, char *devname) |
f0f5a016 LM |
3942 | { |
3943 | struct sys_dev *hba_name; | |
3944 | int rv = 0; | |
3945 | ||
3946 | if ((fd < 0) || check_env("IMSM_NO_PLATFORM")) { | |
f2f5c343 | 3947 | super->orom = NULL; |
f0f5a016 LM |
3948 | super->hba = NULL; |
3949 | return 0; | |
3950 | } | |
3951 | hba_name = find_disk_attached_hba(fd, NULL); | |
3952 | if (!hba_name) { | |
d424212e | 3953 | if (devname) |
e7b84f9d N |
3954 | pr_err("%s is not attached to Intel(R) RAID controller.\n", |
3955 | devname); | |
f0f5a016 LM |
3956 | return 1; |
3957 | } | |
3958 | rv = attach_hba_to_super(super, hba_name); | |
3959 | if (rv == 2) { | |
d424212e N |
3960 | if (devname) { |
3961 | struct intel_hba *hba = super->hba; | |
f0f5a016 | 3962 | |
60f0f54d PB |
3963 | pr_err("%s is attached to Intel(R) %s %s (%s),\n" |
3964 | " but the container is assigned to Intel(R) %s %s (", | |
d424212e | 3965 | devname, |
614902f6 | 3966 | get_sys_dev_type(hba_name->type), |
60f0f54d | 3967 | hba_name->type == SYS_DEV_VMD ? "domain" : "RAID controller", |
f0f5a016 | 3968 | hba_name->pci_id ? : "Err!", |
60f0f54d PB |
3969 | get_sys_dev_type(super->hba->type), |
3970 | hba->type == SYS_DEV_VMD ? "domain" : "RAID controller"); | |
f0f5a016 | 3971 | |
f0f5a016 LM |
3972 | while (hba) { |
3973 | fprintf(stderr, "%s", hba->pci_id ? : "Err!"); | |
3974 | if (hba->next) | |
3975 | fprintf(stderr, ", "); | |
3976 | hba = hba->next; | |
3977 | } | |
6b781d33 | 3978 | fprintf(stderr, ").\n" |
60f0f54d PB |
3979 | " Mixing devices attached to different %s is not allowed.\n", |
3980 | hba_name->type == SYS_DEV_VMD ? "VMD domains" : "controllers"); | |
f0f5a016 | 3981 | } |
f0f5a016 LM |
3982 | return 2; |
3983 | } | |
6b781d33 | 3984 | super->orom = find_imsm_capability(hba_name); |
f2f5c343 LM |
3985 | if (!super->orom) |
3986 | return 3; | |
614902f6 | 3987 | |
f0f5a016 LM |
3988 | return 0; |
3989 | } | |
3990 | ||
47ee5a45 DW |
3991 | /* find_missing - helper routine for load_super_imsm_all that identifies |
3992 | * disks that have disappeared from the system. This routine relies on | |
3993 | * the mpb being uptodate, which it is at load time. | |
3994 | */ | |
3995 | static int find_missing(struct intel_super *super) | |
3996 | { | |
3997 | int i; | |
3998 | struct imsm_super *mpb = super->anchor; | |
3999 | struct dl *dl; | |
4000 | struct imsm_disk *disk; | |
47ee5a45 DW |
4001 | |
4002 | for (i = 0; i < mpb->num_disks; i++) { | |
4003 | disk = __get_imsm_disk(mpb, i); | |
54c2c1ea | 4004 | dl = serial_to_dl(disk->serial, super); |
47ee5a45 DW |
4005 | if (dl) |
4006 | continue; | |
47ee5a45 | 4007 | |
503975b9 | 4008 | dl = xmalloc(sizeof(*dl)); |
47ee5a45 DW |
4009 | dl->major = 0; |
4010 | dl->minor = 0; | |
4011 | dl->fd = -1; | |
503975b9 | 4012 | dl->devname = xstrdup("missing"); |
47ee5a45 DW |
4013 | dl->index = i; |
4014 | serialcpy(dl->serial, disk->serial); | |
4015 | dl->disk = *disk; | |
689c9bf3 | 4016 | dl->e = NULL; |
47ee5a45 DW |
4017 | dl->next = super->missing; |
4018 | super->missing = dl; | |
4019 | } | |
4020 | ||
4021 | return 0; | |
4022 | } | |
4023 | ||
3960e579 | 4024 | #ifndef MDASSEMBLE |
a2b97981 DW |
4025 | static struct intel_disk *disk_list_get(__u8 *serial, struct intel_disk *disk_list) |
4026 | { | |
4027 | struct intel_disk *idisk = disk_list; | |
4028 | ||
4029 | while (idisk) { | |
4030 | if (serialcmp(idisk->disk.serial, serial) == 0) | |
4031 | break; | |
4032 | idisk = idisk->next; | |
4033 | } | |
4034 | ||
4035 | return idisk; | |
4036 | } | |
4037 | ||
4038 | static int __prep_thunderdome(struct intel_super **table, int tbl_size, | |
4039 | struct intel_super *super, | |
4040 | struct intel_disk **disk_list) | |
4041 | { | |
4042 | struct imsm_disk *d = &super->disks->disk; | |
4043 | struct imsm_super *mpb = super->anchor; | |
4044 | int i, j; | |
4045 | ||
4046 | for (i = 0; i < tbl_size; i++) { | |
4047 | struct imsm_super *tbl_mpb = table[i]->anchor; | |
4048 | struct imsm_disk *tbl_d = &table[i]->disks->disk; | |
4049 | ||
4050 | if (tbl_mpb->family_num == mpb->family_num) { | |
4051 | if (tbl_mpb->check_sum == mpb->check_sum) { | |
1ade5cc1 N |
4052 | dprintf("mpb from %d:%d matches %d:%d\n", |
4053 | super->disks->major, | |
a2b97981 DW |
4054 | super->disks->minor, |
4055 | table[i]->disks->major, | |
4056 | table[i]->disks->minor); | |
4057 | break; | |
4058 | } | |
4059 | ||
4060 | if (((is_configured(d) && !is_configured(tbl_d)) || | |
4061 | is_configured(d) == is_configured(tbl_d)) && | |
4062 | tbl_mpb->generation_num < mpb->generation_num) { | |
4063 | /* current version of the mpb is a | |
4064 | * better candidate than the one in | |
4065 | * super_table, but copy over "cross | |
4066 | * generational" status | |
4067 | */ | |
4068 | struct intel_disk *idisk; | |
4069 | ||
1ade5cc1 N |
4070 | dprintf("mpb from %d:%d replaces %d:%d\n", |
4071 | super->disks->major, | |
a2b97981 DW |
4072 | super->disks->minor, |
4073 | table[i]->disks->major, | |
4074 | table[i]->disks->minor); | |
4075 | ||
4076 | idisk = disk_list_get(tbl_d->serial, *disk_list); | |
4077 | if (idisk && is_failed(&idisk->disk)) | |
4078 | tbl_d->status |= FAILED_DISK; | |
4079 | break; | |
4080 | } else { | |
4081 | struct intel_disk *idisk; | |
4082 | struct imsm_disk *disk; | |
4083 | ||
4084 | /* tbl_mpb is more up to date, but copy | |
4085 | * over cross generational status before | |
4086 | * returning | |
4087 | */ | |
4088 | disk = __serial_to_disk(d->serial, mpb, NULL); | |
4089 | if (disk && is_failed(disk)) | |
4090 | d->status |= FAILED_DISK; | |
4091 | ||
4092 | idisk = disk_list_get(d->serial, *disk_list); | |
4093 | if (idisk) { | |
4094 | idisk->owner = i; | |
4095 | if (disk && is_configured(disk)) | |
4096 | idisk->disk.status |= CONFIGURED_DISK; | |
4097 | } | |
4098 | ||
1ade5cc1 N |
4099 | dprintf("mpb from %d:%d prefer %d:%d\n", |
4100 | super->disks->major, | |
a2b97981 DW |
4101 | super->disks->minor, |
4102 | table[i]->disks->major, | |
4103 | table[i]->disks->minor); | |
4104 | ||
4105 | return tbl_size; | |
4106 | } | |
4107 | } | |
4108 | } | |
4109 | ||
4110 | if (i >= tbl_size) | |
4111 | table[tbl_size++] = super; | |
4112 | else | |
4113 | table[i] = super; | |
4114 | ||
4115 | /* update/extend the merged list of imsm_disk records */ | |
4116 | for (j = 0; j < mpb->num_disks; j++) { | |
4117 | struct imsm_disk *disk = __get_imsm_disk(mpb, j); | |
4118 | struct intel_disk *idisk; | |
4119 | ||
4120 | idisk = disk_list_get(disk->serial, *disk_list); | |
4121 | if (idisk) { | |
4122 | idisk->disk.status |= disk->status; | |
4123 | if (is_configured(&idisk->disk) || | |
4124 | is_failed(&idisk->disk)) | |
4125 | idisk->disk.status &= ~(SPARE_DISK); | |
4126 | } else { | |
503975b9 | 4127 | idisk = xcalloc(1, sizeof(*idisk)); |
a2b97981 DW |
4128 | idisk->owner = IMSM_UNKNOWN_OWNER; |
4129 | idisk->disk = *disk; | |
4130 | idisk->next = *disk_list; | |
4131 | *disk_list = idisk; | |
4132 | } | |
4133 | ||
4134 | if (serialcmp(idisk->disk.serial, d->serial) == 0) | |
4135 | idisk->owner = i; | |
4136 | } | |
4137 | ||
4138 | return tbl_size; | |
4139 | } | |
4140 | ||
4141 | static struct intel_super * | |
4142 | validate_members(struct intel_super *super, struct intel_disk *disk_list, | |
4143 | const int owner) | |
4144 | { | |
4145 | struct imsm_super *mpb = super->anchor; | |
4146 | int ok_count = 0; | |
4147 | int i; | |
4148 | ||
4149 | for (i = 0; i < mpb->num_disks; i++) { | |
4150 | struct imsm_disk *disk = __get_imsm_disk(mpb, i); | |
4151 | struct intel_disk *idisk; | |
4152 | ||
4153 | idisk = disk_list_get(disk->serial, disk_list); | |
4154 | if (idisk) { | |
4155 | if (idisk->owner == owner || | |
4156 | idisk->owner == IMSM_UNKNOWN_OWNER) | |
4157 | ok_count++; | |
4158 | else | |
1ade5cc1 N |
4159 | dprintf("'%.16s' owner %d != %d\n", |
4160 | disk->serial, idisk->owner, | |
a2b97981 DW |
4161 | owner); |
4162 | } else { | |
1ade5cc1 N |
4163 | dprintf("unknown disk %x [%d]: %.16s\n", |
4164 | __le32_to_cpu(mpb->family_num), i, | |
a2b97981 DW |
4165 | disk->serial); |
4166 | break; | |
4167 | } | |
4168 | } | |
4169 | ||
4170 | if (ok_count == mpb->num_disks) | |
4171 | return super; | |
4172 | return NULL; | |
4173 | } | |
4174 | ||
4175 | static void show_conflicts(__u32 family_num, struct intel_super *super_list) | |
4176 | { | |
4177 | struct intel_super *s; | |
4178 | ||
4179 | for (s = super_list; s; s = s->next) { | |
4180 | if (family_num != s->anchor->family_num) | |
4181 | continue; | |
e12b3daa | 4182 | pr_err("Conflict, offlining family %#x on '%s'\n", |
a2b97981 DW |
4183 | __le32_to_cpu(family_num), s->disks->devname); |
4184 | } | |
4185 | } | |
4186 | ||
4187 | static struct intel_super * | |
4188 | imsm_thunderdome(struct intel_super **super_list, int len) | |
4189 | { | |
4190 | struct intel_super *super_table[len]; | |
4191 | struct intel_disk *disk_list = NULL; | |
4192 | struct intel_super *champion, *spare; | |
4193 | struct intel_super *s, **del; | |
4194 | int tbl_size = 0; | |
4195 | int conflict; | |
4196 | int i; | |
4197 | ||
4198 | memset(super_table, 0, sizeof(super_table)); | |
4199 | for (s = *super_list; s; s = s->next) | |
4200 | tbl_size = __prep_thunderdome(super_table, tbl_size, s, &disk_list); | |
4201 | ||
4202 | for (i = 0; i < tbl_size; i++) { | |
4203 | struct imsm_disk *d; | |
4204 | struct intel_disk *idisk; | |
4205 | struct imsm_super *mpb = super_table[i]->anchor; | |
4206 | ||
4207 | s = super_table[i]; | |
4208 | d = &s->disks->disk; | |
4209 | ||
4210 | /* 'd' must appear in merged disk list for its | |
4211 | * configuration to be valid | |
4212 | */ | |
4213 | idisk = disk_list_get(d->serial, disk_list); | |
4214 | if (idisk && idisk->owner == i) | |
4215 | s = validate_members(s, disk_list, i); | |
4216 | else | |
4217 | s = NULL; | |
4218 | ||
4219 | if (!s) | |
1ade5cc1 N |
4220 | dprintf("marking family: %#x from %d:%d offline\n", |
4221 | mpb->family_num, | |
a2b97981 DW |
4222 | super_table[i]->disks->major, |
4223 | super_table[i]->disks->minor); | |
4224 | super_table[i] = s; | |
4225 | } | |
4226 | ||
4227 | /* This is where the mdadm implementation differs from the Windows | |
4228 | * driver which has no strict concept of a container. We can only | |
4229 | * assemble one family from a container, so when returning a prodigal | |
4230 | * array member to this system the code will not be able to disambiguate | |
4231 | * the container contents that should be assembled ("foreign" versus | |
4232 | * "local"). It requires user intervention to set the orig_family_num | |
4233 | * to a new value to establish a new container. The Windows driver in | |
4234 | * this situation fixes up the volume name in place and manages the | |
4235 | * foreign array as an independent entity. | |
4236 | */ | |
4237 | s = NULL; | |
4238 | spare = NULL; | |
4239 | conflict = 0; | |
4240 | for (i = 0; i < tbl_size; i++) { | |
4241 | struct intel_super *tbl_ent = super_table[i]; | |
4242 | int is_spare = 0; | |
4243 | ||
4244 | if (!tbl_ent) | |
4245 | continue; | |
4246 | ||
4247 | if (tbl_ent->anchor->num_raid_devs == 0) { | |
4248 | spare = tbl_ent; | |
4249 | is_spare = 1; | |
4250 | } | |
4251 | ||
4252 | if (s && !is_spare) { | |
4253 | show_conflicts(tbl_ent->anchor->family_num, *super_list); | |
4254 | conflict++; | |
4255 | } else if (!s && !is_spare) | |
4256 | s = tbl_ent; | |
4257 | } | |
4258 | ||
4259 | if (!s) | |
4260 | s = spare; | |
4261 | if (!s) { | |
4262 | champion = NULL; | |
4263 | goto out; | |
4264 | } | |
4265 | champion = s; | |
4266 | ||
4267 | if (conflict) | |
7a862a02 | 4268 | pr_err("Chose family %#x on '%s', assemble conflicts to new container with '--update=uuid'\n", |
a2b97981 DW |
4269 | __le32_to_cpu(s->anchor->family_num), s->disks->devname); |
4270 | ||
4271 | /* collect all dl's onto 'champion', and update them to | |
4272 | * champion's version of the status | |
4273 | */ | |
4274 | for (s = *super_list; s; s = s->next) { | |
4275 | struct imsm_super *mpb = champion->anchor; | |
4276 | struct dl *dl = s->disks; | |
4277 | ||
4278 | if (s == champion) | |
4279 | continue; | |
4280 | ||
5d7b407a CA |
4281 | mpb->attributes |= s->anchor->attributes & MPB_ATTRIB_2TB_DISK; |
4282 | ||
a2b97981 DW |
4283 | for (i = 0; i < mpb->num_disks; i++) { |
4284 | struct imsm_disk *disk; | |
4285 | ||
4286 | disk = __serial_to_disk(dl->serial, mpb, &dl->index); | |
4287 | if (disk) { | |
4288 | dl->disk = *disk; | |
4289 | /* only set index on disks that are a member of | |
4290 | * a populated contianer, i.e. one with | |
4291 | * raid_devs | |
4292 | */ | |
4293 | if (is_failed(&dl->disk)) | |
4294 | dl->index = -2; | |
4295 | else if (is_spare(&dl->disk)) | |
4296 | dl->index = -1; | |
4297 | break; | |
4298 | } | |
4299 | } | |
4300 | ||
4301 | if (i >= mpb->num_disks) { | |
4302 | struct intel_disk *idisk; | |
4303 | ||
4304 | idisk = disk_list_get(dl->serial, disk_list); | |
ecf408e9 | 4305 | if (idisk && is_spare(&idisk->disk) && |
a2b97981 DW |
4306 | !is_failed(&idisk->disk) && !is_configured(&idisk->disk)) |
4307 | dl->index = -1; | |
4308 | else { | |
4309 | dl->index = -2; | |
4310 | continue; | |
4311 | } | |
4312 | } | |
4313 | ||
4314 | dl->next = champion->disks; | |
4315 | champion->disks = dl; | |
4316 | s->disks = NULL; | |
4317 | } | |
4318 | ||
4319 | /* delete 'champion' from super_list */ | |
4320 | for (del = super_list; *del; ) { | |
4321 | if (*del == champion) { | |
4322 | *del = (*del)->next; | |
4323 | break; | |
4324 | } else | |
4325 | del = &(*del)->next; | |
4326 | } | |
4327 | champion->next = NULL; | |
4328 | ||
4329 | out: | |
4330 | while (disk_list) { | |
4331 | struct intel_disk *idisk = disk_list; | |
4332 | ||
4333 | disk_list = disk_list->next; | |
4334 | free(idisk); | |
4335 | } | |
4336 | ||
4337 | return champion; | |
4338 | } | |
4339 | ||
9587c373 LM |
4340 | static int |
4341 | get_sra_super_block(int fd, struct intel_super **super_list, char *devname, int *max, int keep_fd); | |
4dd2df09 | 4342 | static int get_super_block(struct intel_super **super_list, char *devnm, char *devname, |
9587c373 | 4343 | int major, int minor, int keep_fd); |
ec50f7b6 LM |
4344 | static int |
4345 | get_devlist_super_block(struct md_list *devlist, struct intel_super **super_list, | |
4346 | int *max, int keep_fd); | |
4347 | ||
cdddbdbc | 4348 | static int load_super_imsm_all(struct supertype *st, int fd, void **sbp, |
ec50f7b6 LM |
4349 | char *devname, struct md_list *devlist, |
4350 | int keep_fd) | |
cdddbdbc | 4351 | { |
a2b97981 DW |
4352 | struct intel_super *super_list = NULL; |
4353 | struct intel_super *super = NULL; | |
a2b97981 | 4354 | int err = 0; |
9587c373 | 4355 | int i = 0; |
dab4a513 | 4356 | |
9587c373 LM |
4357 | if (fd >= 0) |
4358 | /* 'fd' is an opened container */ | |
4359 | err = get_sra_super_block(fd, &super_list, devname, &i, keep_fd); | |
4360 | else | |
ec50f7b6 LM |
4361 | /* get super block from devlist devices */ |
4362 | err = get_devlist_super_block(devlist, &super_list, &i, keep_fd); | |
9587c373 | 4363 | if (err) |
1602d52c | 4364 | goto error; |
a2b97981 DW |
4365 | /* all mpbs enter, maybe one leaves */ |
4366 | super = imsm_thunderdome(&super_list, i); | |
4367 | if (!super) { | |
4368 | err = 1; | |
4369 | goto error; | |
cdddbdbc DW |
4370 | } |
4371 | ||
47ee5a45 DW |
4372 | if (find_missing(super) != 0) { |
4373 | free_imsm(super); | |
a2b97981 DW |
4374 | err = 2; |
4375 | goto error; | |
47ee5a45 | 4376 | } |
8e59f3d8 AK |
4377 | |
4378 | /* load migration record */ | |
4379 | err = load_imsm_migr_rec(super, NULL); | |
4c965cc9 AK |
4380 | if (err == -1) { |
4381 | /* migration is in progress, | |
4382 | * but migr_rec cannot be loaded, | |
4383 | */ | |
8e59f3d8 AK |
4384 | err = 4; |
4385 | goto error; | |
4386 | } | |
e2f41b2c AK |
4387 | |
4388 | /* Check migration compatibility */ | |
4c965cc9 | 4389 | if ((err == 0) && (check_mpb_migr_compatibility(super) != 0)) { |
e7b84f9d | 4390 | pr_err("Unsupported migration detected"); |
e2f41b2c AK |
4391 | if (devname) |
4392 | fprintf(stderr, " on %s\n", devname); | |
4393 | else | |
4394 | fprintf(stderr, " (IMSM).\n"); | |
4395 | ||
4396 | err = 5; | |
4397 | goto error; | |
4398 | } | |
4399 | ||
a2b97981 DW |
4400 | err = 0; |
4401 | ||
4402 | error: | |
4403 | while (super_list) { | |
4404 | struct intel_super *s = super_list; | |
4405 | ||
4406 | super_list = super_list->next; | |
4407 | free_imsm(s); | |
4408 | } | |
9587c373 | 4409 | |
a2b97981 DW |
4410 | if (err) |
4411 | return err; | |
f7e7067b | 4412 | |
cdddbdbc | 4413 | *sbp = super; |
9587c373 | 4414 | if (fd >= 0) |
4dd2df09 | 4415 | strcpy(st->container_devnm, fd2devnm(fd)); |
9587c373 | 4416 | else |
4dd2df09 | 4417 | st->container_devnm[0] = 0; |
a2b97981 | 4418 | if (err == 0 && st->ss == NULL) { |
bf5a934a | 4419 | st->ss = &super_imsm; |
cdddbdbc DW |
4420 | st->minor_version = 0; |
4421 | st->max_devs = IMSM_MAX_DEVICES; | |
4422 | } | |
cdddbdbc DW |
4423 | return 0; |
4424 | } | |
2b959fbf | 4425 | |
ec50f7b6 LM |
4426 | static int |
4427 | get_devlist_super_block(struct md_list *devlist, struct intel_super **super_list, | |
4428 | int *max, int keep_fd) | |
4429 | { | |
4430 | struct md_list *tmpdev; | |
4431 | int err = 0; | |
4432 | int i = 0; | |
9587c373 | 4433 | |
ec50f7b6 LM |
4434 | for (i = 0, tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) { |
4435 | if (tmpdev->used != 1) | |
4436 | continue; | |
4437 | if (tmpdev->container == 1) { | |
ca9de185 | 4438 | int lmax = 0; |
ec50f7b6 LM |
4439 | int fd = dev_open(tmpdev->devname, O_RDONLY|O_EXCL); |
4440 | if (fd < 0) { | |
e7b84f9d | 4441 | pr_err("cannot open device %s: %s\n", |
ec50f7b6 LM |
4442 | tmpdev->devname, strerror(errno)); |
4443 | err = 8; | |
4444 | goto error; | |
4445 | } | |
4446 | err = get_sra_super_block(fd, super_list, | |
4447 | tmpdev->devname, &lmax, | |
4448 | keep_fd); | |
4449 | i += lmax; | |
4450 | close(fd); | |
4451 | if (err) { | |
4452 | err = 7; | |
4453 | goto error; | |
4454 | } | |
4455 | } else { | |
4456 | int major = major(tmpdev->st_rdev); | |
4457 | int minor = minor(tmpdev->st_rdev); | |
4458 | err = get_super_block(super_list, | |
4dd2df09 | 4459 | NULL, |
ec50f7b6 LM |
4460 | tmpdev->devname, |
4461 | major, minor, | |
4462 | keep_fd); | |
4463 | i++; | |
4464 | if (err) { | |
4465 | err = 6; | |
4466 | goto error; | |
4467 | } | |
4468 | } | |
4469 | } | |
4470 | error: | |
4471 | *max = i; | |
4472 | return err; | |
4473 | } | |
9587c373 | 4474 | |
4dd2df09 | 4475 | static int get_super_block(struct intel_super **super_list, char *devnm, char *devname, |
9587c373 LM |
4476 | int major, int minor, int keep_fd) |
4477 | { | |
594dc1b8 | 4478 | struct intel_super *s; |
9587c373 LM |
4479 | char nm[32]; |
4480 | int dfd = -1; | |
9587c373 LM |
4481 | int err = 0; |
4482 | int retry; | |
4483 | ||
4484 | s = alloc_super(); | |
4485 | if (!s) { | |
4486 | err = 1; | |
4487 | goto error; | |
4488 | } | |
4489 | ||
4490 | sprintf(nm, "%d:%d", major, minor); | |
4491 | dfd = dev_open(nm, O_RDWR); | |
4492 | if (dfd < 0) { | |
4493 | err = 2; | |
4494 | goto error; | |
4495 | } | |
4496 | ||
cb8f6859 | 4497 | find_intel_hba_capability(dfd, s, devname); |
9587c373 LM |
4498 | err = load_and_parse_mpb(dfd, s, NULL, keep_fd); |
4499 | ||
4500 | /* retry the load if we might have raced against mdmon */ | |
4dd2df09 | 4501 | if (err == 3 && devnm && mdmon_running(devnm)) |
9587c373 LM |
4502 | for (retry = 0; retry < 3; retry++) { |
4503 | usleep(3000); | |
4504 | err = load_and_parse_mpb(dfd, s, NULL, keep_fd); | |
4505 | if (err != 3) | |
4506 | break; | |
4507 | } | |
4508 | error: | |
4509 | if (!err) { | |
4510 | s->next = *super_list; | |
4511 | *super_list = s; | |
4512 | } else { | |
4513 | if (s) | |
4514 | free(s); | |
36614e95 | 4515 | if (dfd >= 0) |
9587c373 LM |
4516 | close(dfd); |
4517 | } | |
4518 | if ((dfd >= 0) && (!keep_fd)) | |
4519 | close(dfd); | |
4520 | return err; | |
4521 | ||
4522 | } | |
4523 | ||
4524 | static int | |
4525 | get_sra_super_block(int fd, struct intel_super **super_list, char *devname, int *max, int keep_fd) | |
4526 | { | |
4527 | struct mdinfo *sra; | |
4dd2df09 | 4528 | char *devnm; |
9587c373 LM |
4529 | struct mdinfo *sd; |
4530 | int err = 0; | |
4531 | int i = 0; | |
4dd2df09 | 4532 | sra = sysfs_read(fd, NULL, GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE); |
9587c373 LM |
4533 | if (!sra) |
4534 | return 1; | |
4535 | ||
4536 | if (sra->array.major_version != -1 || | |
4537 | sra->array.minor_version != -2 || | |
4538 | strcmp(sra->text_version, "imsm") != 0) { | |
4539 | err = 1; | |
4540 | goto error; | |
4541 | } | |
4542 | /* load all mpbs */ | |
4dd2df09 | 4543 | devnm = fd2devnm(fd); |
9587c373 | 4544 | for (sd = sra->devs, i = 0; sd; sd = sd->next, i++) { |
4dd2df09 | 4545 | if (get_super_block(super_list, devnm, devname, |
9587c373 LM |
4546 | sd->disk.major, sd->disk.minor, keep_fd) != 0) { |
4547 | err = 7; | |
4548 | goto error; | |
4549 | } | |
4550 | } | |
4551 | error: | |
4552 | sysfs_free(sra); | |
4553 | *max = i; | |
4554 | return err; | |
4555 | } | |
4556 | ||
2b959fbf N |
4557 | static int load_container_imsm(struct supertype *st, int fd, char *devname) |
4558 | { | |
ec50f7b6 | 4559 | return load_super_imsm_all(st, fd, &st->sb, devname, NULL, 1); |
2b959fbf | 4560 | } |
cdddbdbc DW |
4561 | #endif |
4562 | ||
4563 | static int load_super_imsm(struct supertype *st, int fd, char *devname) | |
4564 | { | |
4565 | struct intel_super *super; | |
4566 | int rv; | |
8a3544f8 | 4567 | int retry; |
cdddbdbc | 4568 | |
357ac106 | 4569 | if (test_partition(fd)) |
691c6ee1 N |
4570 | /* IMSM not allowed on partitions */ |
4571 | return 1; | |
4572 | ||
37424f13 DW |
4573 | free_super_imsm(st); |
4574 | ||
49133e57 | 4575 | super = alloc_super(); |
ea2bc72b LM |
4576 | /* Load hba and capabilities if they exist. |
4577 | * But do not preclude loading metadata in case capabilities or hba are | |
4578 | * non-compliant and ignore_hw_compat is set. | |
4579 | */ | |
d424212e | 4580 | rv = find_intel_hba_capability(fd, super, devname); |
f2f5c343 | 4581 | /* no orom/efi or non-intel hba of the disk */ |
ea2bc72b | 4582 | if ((rv != 0) && (st->ignore_hw_compat == 0)) { |
f2f5c343 | 4583 | if (devname) |
e7b84f9d | 4584 | pr_err("No OROM/EFI properties for %s\n", devname); |
f2f5c343 LM |
4585 | free_imsm(super); |
4586 | return 2; | |
4587 | } | |
a2b97981 | 4588 | rv = load_and_parse_mpb(fd, super, devname, 0); |
cdddbdbc | 4589 | |
8a3544f8 AP |
4590 | /* retry the load if we might have raced against mdmon */ |
4591 | if (rv == 3) { | |
f96b1302 AP |
4592 | struct mdstat_ent *mdstat = NULL; |
4593 | char *name = fd2kname(fd); | |
4594 | ||
4595 | if (name) | |
4596 | mdstat = mdstat_by_component(name); | |
8a3544f8 AP |
4597 | |
4598 | if (mdstat && mdmon_running(mdstat->devnm) && getpid() != mdmon_pid(mdstat->devnm)) { | |
4599 | for (retry = 0; retry < 3; retry++) { | |
4600 | usleep(3000); | |
4601 | rv = load_and_parse_mpb(fd, super, devname, 0); | |
4602 | if (rv != 3) | |
4603 | break; | |
4604 | } | |
4605 | } | |
4606 | ||
4607 | free_mdstat(mdstat); | |
4608 | } | |
4609 | ||
cdddbdbc DW |
4610 | if (rv) { |
4611 | if (devname) | |
7a862a02 | 4612 | pr_err("Failed to load all information sections on %s\n", devname); |
cdddbdbc DW |
4613 | free_imsm(super); |
4614 | return rv; | |
4615 | } | |
4616 | ||
4617 | st->sb = super; | |
4618 | if (st->ss == NULL) { | |
4619 | st->ss = &super_imsm; | |
4620 | st->minor_version = 0; | |
4621 | st->max_devs = IMSM_MAX_DEVICES; | |
4622 | } | |
8e59f3d8 AK |
4623 | |
4624 | /* load migration record */ | |
2e062e82 AK |
4625 | if (load_imsm_migr_rec(super, NULL) == 0) { |
4626 | /* Check for unsupported migration features */ | |
4627 | if (check_mpb_migr_compatibility(super) != 0) { | |
e7b84f9d | 4628 | pr_err("Unsupported migration detected"); |
2e062e82 AK |
4629 | if (devname) |
4630 | fprintf(stderr, " on %s\n", devname); | |
4631 | else | |
4632 | fprintf(stderr, " (IMSM).\n"); | |
4633 | return 3; | |
4634 | } | |
e2f41b2c AK |
4635 | } |
4636 | ||
cdddbdbc DW |
4637 | return 0; |
4638 | } | |
4639 | ||
ef6ffade DW |
4640 | static __u16 info_to_blocks_per_strip(mdu_array_info_t *info) |
4641 | { | |
4642 | if (info->level == 1) | |
4643 | return 128; | |
4644 | return info->chunk_size >> 9; | |
4645 | } | |
4646 | ||
5551b113 CA |
4647 | static unsigned long long info_to_blocks_per_member(mdu_array_info_t *info, |
4648 | unsigned long long size) | |
fcfd9599 | 4649 | { |
4025c288 | 4650 | if (info->level == 1) |
5551b113 | 4651 | return size * 2; |
4025c288 | 4652 | else |
5551b113 | 4653 | return (size * 2) & ~(info_to_blocks_per_strip(info) - 1); |
fcfd9599 DW |
4654 | } |
4655 | ||
4d1313e9 DW |
4656 | static void imsm_update_version_info(struct intel_super *super) |
4657 | { | |
4658 | /* update the version and attributes */ | |
4659 | struct imsm_super *mpb = super->anchor; | |
4660 | char *version; | |
4661 | struct imsm_dev *dev; | |
4662 | struct imsm_map *map; | |
4663 | int i; | |
4664 | ||
4665 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
4666 | dev = get_imsm_dev(super, i); | |
238c0a71 | 4667 | map = get_imsm_map(dev, MAP_0); |
4d1313e9 DW |
4668 | if (__le32_to_cpu(dev->size_high) > 0) |
4669 | mpb->attributes |= MPB_ATTRIB_2TB; | |
4670 | ||
4671 | /* FIXME detect when an array spans a port multiplier */ | |
4672 | #if 0 | |
4673 | mpb->attributes |= MPB_ATTRIB_PM; | |
4674 | #endif | |
4675 | ||
4676 | if (mpb->num_raid_devs > 1 || | |
4677 | mpb->attributes != MPB_ATTRIB_CHECKSUM_VERIFY) { | |
4678 | version = MPB_VERSION_ATTRIBS; | |
4679 | switch (get_imsm_raid_level(map)) { | |
4680 | case 0: mpb->attributes |= MPB_ATTRIB_RAID0; break; | |
4681 | case 1: mpb->attributes |= MPB_ATTRIB_RAID1; break; | |
4682 | case 10: mpb->attributes |= MPB_ATTRIB_RAID10; break; | |
4683 | case 5: mpb->attributes |= MPB_ATTRIB_RAID5; break; | |
4684 | } | |
4685 | } else { | |
4686 | if (map->num_members >= 5) | |
4687 | version = MPB_VERSION_5OR6_DISK_ARRAY; | |
4688 | else if (dev->status == DEV_CLONE_N_GO) | |
4689 | version = MPB_VERSION_CNG; | |
4690 | else if (get_imsm_raid_level(map) == 5) | |
4691 | version = MPB_VERSION_RAID5; | |
4692 | else if (map->num_members >= 3) | |
4693 | version = MPB_VERSION_3OR4_DISK_ARRAY; | |
4694 | else if (get_imsm_raid_level(map) == 1) | |
4695 | version = MPB_VERSION_RAID1; | |
4696 | else | |
4697 | version = MPB_VERSION_RAID0; | |
4698 | } | |
4699 | strcpy(((char *) mpb->sig) + strlen(MPB_SIGNATURE), version); | |
4700 | } | |
4701 | } | |
4702 | ||
aa534678 DW |
4703 | static int check_name(struct intel_super *super, char *name, int quiet) |
4704 | { | |
4705 | struct imsm_super *mpb = super->anchor; | |
4706 | char *reason = NULL; | |
4707 | int i; | |
4708 | ||
4709 | if (strlen(name) > MAX_RAID_SERIAL_LEN) | |
4710 | reason = "must be 16 characters or less"; | |
4711 | ||
4712 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
4713 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
4714 | ||
4715 | if (strncmp((char *) dev->volume, name, MAX_RAID_SERIAL_LEN) == 0) { | |
4716 | reason = "already exists"; | |
4717 | break; | |
4718 | } | |
4719 | } | |
4720 | ||
4721 | if (reason && !quiet) | |
e7b84f9d | 4722 | pr_err("imsm volume name %s\n", reason); |
aa534678 DW |
4723 | |
4724 | return !reason; | |
4725 | } | |
4726 | ||
8b353278 DW |
4727 | static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info, |
4728 | unsigned long long size, char *name, | |
83cd1e97 N |
4729 | char *homehost, int *uuid, |
4730 | long long data_offset) | |
cdddbdbc | 4731 | { |
c2c087e6 DW |
4732 | /* We are creating a volume inside a pre-existing container. |
4733 | * so st->sb is already set. | |
4734 | */ | |
4735 | struct intel_super *super = st->sb; | |
949c47a0 | 4736 | struct imsm_super *mpb = super->anchor; |
ba2de7ba | 4737 | struct intel_dev *dv; |
c2c087e6 DW |
4738 | struct imsm_dev *dev; |
4739 | struct imsm_vol *vol; | |
4740 | struct imsm_map *map; | |
4741 | int idx = mpb->num_raid_devs; | |
4742 | int i; | |
4743 | unsigned long long array_blocks; | |
2c092cad | 4744 | size_t size_old, size_new; |
5551b113 | 4745 | unsigned long long num_data_stripes; |
cdddbdbc | 4746 | |
88c32bb1 | 4747 | if (super->orom && mpb->num_raid_devs >= super->orom->vpa) { |
7a862a02 | 4748 | pr_err("This imsm-container already has the maximum of %d volumes\n", super->orom->vpa); |
c2c087e6 DW |
4749 | return 0; |
4750 | } | |
4751 | ||
2c092cad DW |
4752 | /* ensure the mpb is large enough for the new data */ |
4753 | size_old = __le32_to_cpu(mpb->mpb_size); | |
4754 | size_new = disks_to_mpb_size(info->nr_disks); | |
4755 | if (size_new > size_old) { | |
4756 | void *mpb_new; | |
4757 | size_t size_round = ROUND_UP(size_new, 512); | |
4758 | ||
4759 | if (posix_memalign(&mpb_new, 512, size_round) != 0) { | |
e7b84f9d | 4760 | pr_err("could not allocate new mpb\n"); |
2c092cad DW |
4761 | return 0; |
4762 | } | |
17a4eaf9 AK |
4763 | if (posix_memalign(&super->migr_rec_buf, 512, |
4764 | MIGR_REC_BUF_SIZE) != 0) { | |
1ade5cc1 | 4765 | pr_err("could not allocate migr_rec buffer\n"); |
8e59f3d8 AK |
4766 | free(super->buf); |
4767 | free(super); | |
ea944c8f | 4768 | free(mpb_new); |
8e59f3d8 AK |
4769 | return 0; |
4770 | } | |
2c092cad DW |
4771 | memcpy(mpb_new, mpb, size_old); |
4772 | free(mpb); | |
4773 | mpb = mpb_new; | |
949c47a0 | 4774 | super->anchor = mpb_new; |
2c092cad DW |
4775 | mpb->mpb_size = __cpu_to_le32(size_new); |
4776 | memset(mpb_new + size_old, 0, size_round - size_old); | |
4777 | } | |
bf5a934a | 4778 | super->current_vol = idx; |
3960e579 DW |
4779 | |
4780 | /* handle 'failed_disks' by either: | |
4781 | * a) create dummy disk entries in the table if this the first | |
4782 | * volume in the array. We add them here as this is the only | |
4783 | * opportunity to add them. add_to_super_imsm_volume() | |
4784 | * handles the non-failed disks and continues incrementing | |
4785 | * mpb->num_disks. | |
4786 | * b) validate that 'failed_disks' matches the current number | |
4787 | * of missing disks if the container is populated | |
d23fe947 | 4788 | */ |
3960e579 | 4789 | if (super->current_vol == 0) { |
d23fe947 | 4790 | mpb->num_disks = 0; |
3960e579 DW |
4791 | for (i = 0; i < info->failed_disks; i++) { |
4792 | struct imsm_disk *disk; | |
4793 | ||
4794 | mpb->num_disks++; | |
4795 | disk = __get_imsm_disk(mpb, i); | |
4796 | disk->status = CONFIGURED_DISK | FAILED_DISK; | |
4797 | disk->scsi_id = __cpu_to_le32(~(__u32)0); | |
4798 | snprintf((char *) disk->serial, MAX_RAID_SERIAL_LEN, | |
4799 | "missing:%d", i); | |
4800 | } | |
4801 | find_missing(super); | |
4802 | } else { | |
4803 | int missing = 0; | |
4804 | struct dl *d; | |
4805 | ||
4806 | for (d = super->missing; d; d = d->next) | |
4807 | missing++; | |
4808 | if (info->failed_disks > missing) { | |
e7b84f9d | 4809 | pr_err("unable to add 'missing' disk to container\n"); |
3960e579 DW |
4810 | return 0; |
4811 | } | |
4812 | } | |
5a038140 | 4813 | |
aa534678 DW |
4814 | if (!check_name(super, name, 0)) |
4815 | return 0; | |
503975b9 N |
4816 | dv = xmalloc(sizeof(*dv)); |
4817 | dev = xcalloc(1, sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1)); | |
c2c087e6 | 4818 | strncpy((char *) dev->volume, name, MAX_RAID_SERIAL_LEN); |
e03640bd | 4819 | array_blocks = calc_array_size(info->level, info->raid_disks, |
03bcbc65 | 4820 | info->layout, info->chunk_size, |
5551b113 | 4821 | size * 2); |
979d38be DW |
4822 | /* round array size down to closest MB */ |
4823 | array_blocks = (array_blocks >> SECT_PER_MB_SHIFT) << SECT_PER_MB_SHIFT; | |
4824 | ||
c2c087e6 DW |
4825 | dev->size_low = __cpu_to_le32((__u32) array_blocks); |
4826 | dev->size_high = __cpu_to_le32((__u32) (array_blocks >> 32)); | |
1a2487c2 | 4827 | dev->status = (DEV_READ_COALESCING | DEV_WRITE_COALESCING); |
c2c087e6 DW |
4828 | vol = &dev->vol; |
4829 | vol->migr_state = 0; | |
1484e727 | 4830 | set_migr_type(dev, MIGR_INIT); |
3960e579 | 4831 | vol->dirty = !info->state; |
f8f603f1 | 4832 | vol->curr_migr_unit = 0; |
238c0a71 | 4833 | map = get_imsm_map(dev, MAP_0); |
5551b113 CA |
4834 | set_pba_of_lba0(map, super->create_offset); |
4835 | set_blocks_per_member(map, info_to_blocks_per_member(info, size)); | |
ef6ffade | 4836 | map->blocks_per_strip = __cpu_to_le16(info_to_blocks_per_strip(info)); |
0556e1a2 | 4837 | map->failed_disk_num = ~0; |
bf4442ab | 4838 | if (info->level > 0) |
fffaf1ff N |
4839 | map->map_state = (info->state ? IMSM_T_STATE_NORMAL |
4840 | : IMSM_T_STATE_UNINITIALIZED); | |
bf4442ab AK |
4841 | else |
4842 | map->map_state = info->failed_disks ? IMSM_T_STATE_FAILED : | |
4843 | IMSM_T_STATE_NORMAL; | |
252d23c0 | 4844 | map->ddf = 1; |
ef6ffade DW |
4845 | |
4846 | if (info->level == 1 && info->raid_disks > 2) { | |
38950822 AW |
4847 | free(dev); |
4848 | free(dv); | |
7a862a02 | 4849 | pr_err("imsm does not support more than 2 disksin a raid1 volume\n"); |
ef6ffade DW |
4850 | return 0; |
4851 | } | |
81062a36 DW |
4852 | |
4853 | map->raid_level = info->level; | |
4d1313e9 | 4854 | if (info->level == 10) { |
c2c087e6 | 4855 | map->raid_level = 1; |
4d1313e9 | 4856 | map->num_domains = info->raid_disks / 2; |
81062a36 DW |
4857 | } else if (info->level == 1) |
4858 | map->num_domains = info->raid_disks; | |
4859 | else | |
ff596308 | 4860 | map->num_domains = 1; |
81062a36 | 4861 | |
5551b113 CA |
4862 | /* info->size is only int so use the 'size' parameter instead */ |
4863 | num_data_stripes = (size * 2) / info_to_blocks_per_strip(info); | |
4864 | num_data_stripes /= map->num_domains; | |
4865 | set_num_data_stripes(map, num_data_stripes); | |
ef6ffade | 4866 | |
c2c087e6 DW |
4867 | map->num_members = info->raid_disks; |
4868 | for (i = 0; i < map->num_members; i++) { | |
4869 | /* initialized in add_to_super */ | |
4eb26970 | 4870 | set_imsm_ord_tbl_ent(map, i, IMSM_ORD_REBUILD); |
c2c087e6 | 4871 | } |
949c47a0 | 4872 | mpb->num_raid_devs++; |
ba2de7ba DW |
4873 | |
4874 | dv->dev = dev; | |
4875 | dv->index = super->current_vol; | |
4876 | dv->next = super->devlist; | |
4877 | super->devlist = dv; | |
c2c087e6 | 4878 | |
4d1313e9 DW |
4879 | imsm_update_version_info(super); |
4880 | ||
c2c087e6 | 4881 | return 1; |
cdddbdbc DW |
4882 | } |
4883 | ||
bf5a934a DW |
4884 | static int init_super_imsm(struct supertype *st, mdu_array_info_t *info, |
4885 | unsigned long long size, char *name, | |
83cd1e97 N |
4886 | char *homehost, int *uuid, |
4887 | unsigned long long data_offset) | |
bf5a934a DW |
4888 | { |
4889 | /* This is primarily called by Create when creating a new array. | |
4890 | * We will then get add_to_super called for each component, and then | |
4891 | * write_init_super called to write it out to each device. | |
4892 | * For IMSM, Create can create on fresh devices or on a pre-existing | |
4893 | * array. | |
4894 | * To create on a pre-existing array a different method will be called. | |
4895 | * This one is just for fresh drives. | |
4896 | */ | |
4897 | struct intel_super *super; | |
4898 | struct imsm_super *mpb; | |
4899 | size_t mpb_size; | |
4d1313e9 | 4900 | char *version; |
bf5a934a | 4901 | |
83cd1e97 | 4902 | if (data_offset != INVALID_SECTORS) { |
ed503f89 | 4903 | pr_err("data-offset not supported by imsm\n"); |
83cd1e97 N |
4904 | return 0; |
4905 | } | |
4906 | ||
bf5a934a | 4907 | if (st->sb) |
83cd1e97 N |
4908 | return init_super_imsm_volume(st, info, size, name, homehost, uuid, |
4909 | data_offset); | |
e683ca88 DW |
4910 | |
4911 | if (info) | |
4912 | mpb_size = disks_to_mpb_size(info->nr_disks); | |
4913 | else | |
4914 | mpb_size = 512; | |
bf5a934a | 4915 | |
49133e57 | 4916 | super = alloc_super(); |
e683ca88 | 4917 | if (super && posix_memalign(&super->buf, 512, mpb_size) != 0) { |
bf5a934a | 4918 | free(super); |
e683ca88 DW |
4919 | super = NULL; |
4920 | } | |
4921 | if (!super) { | |
1ade5cc1 | 4922 | pr_err("could not allocate superblock\n"); |
bf5a934a DW |
4923 | return 0; |
4924 | } | |
17a4eaf9 | 4925 | if (posix_memalign(&super->migr_rec_buf, 512, MIGR_REC_BUF_SIZE) != 0) { |
1ade5cc1 | 4926 | pr_err("could not allocate migr_rec buffer\n"); |
8e59f3d8 AK |
4927 | free(super->buf); |
4928 | free(super); | |
4929 | return 0; | |
4930 | } | |
e683ca88 | 4931 | memset(super->buf, 0, mpb_size); |
ef649044 | 4932 | mpb = super->buf; |
e683ca88 DW |
4933 | mpb->mpb_size = __cpu_to_le32(mpb_size); |
4934 | st->sb = super; | |
4935 | ||
4936 | if (info == NULL) { | |
4937 | /* zeroing superblock */ | |
4938 | return 0; | |
4939 | } | |
bf5a934a | 4940 | |
4d1313e9 DW |
4941 | mpb->attributes = MPB_ATTRIB_CHECKSUM_VERIFY; |
4942 | ||
4943 | version = (char *) mpb->sig; | |
4944 | strcpy(version, MPB_SIGNATURE); | |
4945 | version += strlen(MPB_SIGNATURE); | |
4946 | strcpy(version, MPB_VERSION_RAID0); | |
bf5a934a | 4947 | |
bf5a934a DW |
4948 | return 1; |
4949 | } | |
4950 | ||
0e600426 | 4951 | #ifndef MDASSEMBLE |
f20c3968 | 4952 | static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk, |
bf5a934a DW |
4953 | int fd, char *devname) |
4954 | { | |
4955 | struct intel_super *super = st->sb; | |
d23fe947 | 4956 | struct imsm_super *mpb = super->anchor; |
3960e579 | 4957 | struct imsm_disk *_disk; |
bf5a934a DW |
4958 | struct imsm_dev *dev; |
4959 | struct imsm_map *map; | |
3960e579 | 4960 | struct dl *dl, *df; |
4eb26970 | 4961 | int slot; |
bf5a934a | 4962 | |
949c47a0 | 4963 | dev = get_imsm_dev(super, super->current_vol); |
238c0a71 | 4964 | map = get_imsm_map(dev, MAP_0); |
bf5a934a | 4965 | |
208933a7 | 4966 | if (! (dk->state & (1<<MD_DISK_SYNC))) { |
e7b84f9d | 4967 | pr_err("%s: Cannot add spare devices to IMSM volume\n", |
208933a7 N |
4968 | devname); |
4969 | return 1; | |
4970 | } | |
4971 | ||
efb30e7f DW |
4972 | if (fd == -1) { |
4973 | /* we're doing autolayout so grab the pre-marked (in | |
4974 | * validate_geometry) raid_disk | |
4975 | */ | |
4976 | for (dl = super->disks; dl; dl = dl->next) | |
4977 | if (dl->raiddisk == dk->raid_disk) | |
4978 | break; | |
4979 | } else { | |
4980 | for (dl = super->disks; dl ; dl = dl->next) | |
4981 | if (dl->major == dk->major && | |
4982 | dl->minor == dk->minor) | |
4983 | break; | |
4984 | } | |
d23fe947 | 4985 | |
208933a7 | 4986 | if (!dl) { |
e7b84f9d | 4987 | pr_err("%s is not a member of the same container\n", devname); |
f20c3968 | 4988 | return 1; |
208933a7 | 4989 | } |
bf5a934a | 4990 | |
d23fe947 DW |
4991 | /* add a pristine spare to the metadata */ |
4992 | if (dl->index < 0) { | |
4993 | dl->index = super->anchor->num_disks; | |
4994 | super->anchor->num_disks++; | |
4995 | } | |
4eb26970 DW |
4996 | /* Check the device has not already been added */ |
4997 | slot = get_imsm_disk_slot(map, dl->index); | |
4998 | if (slot >= 0 && | |
238c0a71 | 4999 | (get_imsm_ord_tbl_ent(dev, slot, MAP_X) & IMSM_ORD_REBUILD) == 0) { |
e7b84f9d | 5000 | pr_err("%s has been included in this array twice\n", |
4eb26970 DW |
5001 | devname); |
5002 | return 1; | |
5003 | } | |
656b6b5a | 5004 | set_imsm_ord_tbl_ent(map, dk->raid_disk, dl->index); |
ee5aad5a | 5005 | dl->disk.status = CONFIGURED_DISK; |
d23fe947 | 5006 | |
3960e579 DW |
5007 | /* update size of 'missing' disks to be at least as large as the |
5008 | * largest acitve member (we only have dummy missing disks when | |
5009 | * creating the first volume) | |
5010 | */ | |
5011 | if (super->current_vol == 0) { | |
5012 | for (df = super->missing; df; df = df->next) { | |
5551b113 CA |
5013 | if (total_blocks(&dl->disk) > total_blocks(&df->disk)) |
5014 | set_total_blocks(&df->disk, total_blocks(&dl->disk)); | |
3960e579 DW |
5015 | _disk = __get_imsm_disk(mpb, df->index); |
5016 | *_disk = df->disk; | |
5017 | } | |
5018 | } | |
5019 | ||
5020 | /* refresh unset/failed slots to point to valid 'missing' entries */ | |
5021 | for (df = super->missing; df; df = df->next) | |
5022 | for (slot = 0; slot < mpb->num_disks; slot++) { | |
238c0a71 | 5023 | __u32 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X); |
3960e579 DW |
5024 | |
5025 | if ((ord & IMSM_ORD_REBUILD) == 0) | |
5026 | continue; | |
5027 | set_imsm_ord_tbl_ent(map, slot, df->index | IMSM_ORD_REBUILD); | |
1ace8403 | 5028 | if (is_gen_migration(dev)) { |
238c0a71 AK |
5029 | struct imsm_map *map2 = get_imsm_map(dev, |
5030 | MAP_1); | |
0a108d63 AK |
5031 | int slot2 = get_imsm_disk_slot(map2, df->index); |
5032 | if ((slot2 < map2->num_members) && | |
5033 | (slot2 >= 0)) { | |
1ace8403 | 5034 | __u32 ord2 = get_imsm_ord_tbl_ent(dev, |
238c0a71 AK |
5035 | slot2, |
5036 | MAP_1); | |
1ace8403 AK |
5037 | if ((unsigned)df->index == |
5038 | ord_to_idx(ord2)) | |
5039 | set_imsm_ord_tbl_ent(map2, | |
0a108d63 | 5040 | slot2, |
1ace8403 AK |
5041 | df->index | |
5042 | IMSM_ORD_REBUILD); | |
5043 | } | |
5044 | } | |
3960e579 DW |
5045 | dprintf("set slot:%d to missing disk:%d\n", slot, df->index); |
5046 | break; | |
5047 | } | |
5048 | ||
d23fe947 DW |
5049 | /* if we are creating the first raid device update the family number */ |
5050 | if (super->current_vol == 0) { | |
5051 | __u32 sum; | |
5052 | struct imsm_dev *_dev = __get_imsm_dev(mpb, 0); | |
d23fe947 | 5053 | |
3960e579 | 5054 | _disk = __get_imsm_disk(mpb, dl->index); |
791b666a | 5055 | if (!_dev || !_disk) { |
e7b84f9d | 5056 | pr_err("BUG mpb setup error\n"); |
791b666a AW |
5057 | return 1; |
5058 | } | |
d23fe947 DW |
5059 | *_dev = *dev; |
5060 | *_disk = dl->disk; | |
148acb7b DW |
5061 | sum = random32(); |
5062 | sum += __gen_imsm_checksum(mpb); | |
d23fe947 | 5063 | mpb->family_num = __cpu_to_le32(sum); |
148acb7b | 5064 | mpb->orig_family_num = mpb->family_num; |
d23fe947 | 5065 | } |
ca0748fa | 5066 | super->current_disk = dl; |
f20c3968 | 5067 | return 0; |
bf5a934a DW |
5068 | } |
5069 | ||
a8619d23 AK |
5070 | /* mark_spare() |
5071 | * Function marks disk as spare and restores disk serial | |
5072 | * in case it was previously marked as failed by takeover operation | |
5073 | * reruns: | |
5074 | * -1 : critical error | |
5075 | * 0 : disk is marked as spare but serial is not set | |
5076 | * 1 : success | |
5077 | */ | |
5078 | int mark_spare(struct dl *disk) | |
5079 | { | |
5080 | __u8 serial[MAX_RAID_SERIAL_LEN]; | |
5081 | int ret_val = -1; | |
5082 | ||
5083 | if (!disk) | |
5084 | return ret_val; | |
5085 | ||
5086 | ret_val = 0; | |
5087 | if (!imsm_read_serial(disk->fd, NULL, serial)) { | |
5088 | /* Restore disk serial number, because takeover marks disk | |
5089 | * as failed and adds to serial ':0' before it becomes | |
5090 | * a spare disk. | |
5091 | */ | |
5092 | serialcpy(disk->serial, serial); | |
5093 | serialcpy(disk->disk.serial, serial); | |
5094 | ret_val = 1; | |
5095 | } | |
5096 | disk->disk.status = SPARE_DISK; | |
5097 | disk->index = -1; | |
5098 | ||
5099 | return ret_val; | |
5100 | } | |
88654014 | 5101 | |
f20c3968 | 5102 | static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk, |
72ca9bcf N |
5103 | int fd, char *devname, |
5104 | unsigned long long data_offset) | |
cdddbdbc | 5105 | { |
c2c087e6 | 5106 | struct intel_super *super = st->sb; |
c2c087e6 DW |
5107 | struct dl *dd; |
5108 | unsigned long long size; | |
f2f27e63 | 5109 | __u32 id; |
c2c087e6 DW |
5110 | int rv; |
5111 | struct stat stb; | |
5112 | ||
88654014 LM |
5113 | /* If we are on an RAID enabled platform check that the disk is |
5114 | * attached to the raid controller. | |
5115 | * We do not need to test disks attachment for container based additions, | |
5116 | * they shall be already tested when container was created/assembled. | |
88c32bb1 | 5117 | */ |
d424212e | 5118 | rv = find_intel_hba_capability(fd, super, devname); |
f2f5c343 | 5119 | /* no orom/efi or non-intel hba of the disk */ |
f0f5a016 LM |
5120 | if (rv != 0) { |
5121 | dprintf("capability: %p fd: %d ret: %d\n", | |
5122 | super->orom, fd, rv); | |
5123 | return 1; | |
88c32bb1 DW |
5124 | } |
5125 | ||
f20c3968 DW |
5126 | if (super->current_vol >= 0) |
5127 | return add_to_super_imsm_volume(st, dk, fd, devname); | |
bf5a934a | 5128 | |
c2c087e6 | 5129 | fstat(fd, &stb); |
503975b9 | 5130 | dd = xcalloc(sizeof(*dd), 1); |
c2c087e6 DW |
5131 | dd->major = major(stb.st_rdev); |
5132 | dd->minor = minor(stb.st_rdev); | |
503975b9 | 5133 | dd->devname = devname ? xstrdup(devname) : NULL; |
c2c087e6 | 5134 | dd->fd = fd; |
689c9bf3 | 5135 | dd->e = NULL; |
1a64be56 | 5136 | dd->action = DISK_ADD; |
c2c087e6 | 5137 | rv = imsm_read_serial(fd, devname, dd->serial); |
32ba9157 | 5138 | if (rv) { |
e7b84f9d | 5139 | pr_err("failed to retrieve scsi serial, aborting\n"); |
949c47a0 | 5140 | free(dd); |
0030e8d6 | 5141 | abort(); |
c2c087e6 DW |
5142 | } |
5143 | ||
c2c087e6 | 5144 | get_dev_size(fd, NULL, &size); |
71e5411e PB |
5145 | /* clear migr_rec when adding disk to container */ |
5146 | memset(super->migr_rec_buf, 0, MIGR_REC_BUF_SIZE); | |
5147 | if (lseek64(fd, size - MIGR_REC_POSITION, SEEK_SET) >= 0) { | |
5148 | if (write(fd, super->migr_rec_buf, | |
5149 | MIGR_REC_BUF_SIZE) != MIGR_REC_BUF_SIZE) | |
5150 | perror("Write migr_rec failed"); | |
5151 | } | |
5152 | ||
c2c087e6 | 5153 | size /= 512; |
1f24f035 | 5154 | serialcpy(dd->disk.serial, dd->serial); |
5551b113 CA |
5155 | set_total_blocks(&dd->disk, size); |
5156 | if (__le32_to_cpu(dd->disk.total_blocks_hi) > 0) { | |
5157 | struct imsm_super *mpb = super->anchor; | |
5158 | mpb->attributes |= MPB_ATTRIB_2TB_DISK; | |
5159 | } | |
a8619d23 | 5160 | mark_spare(dd); |
c2c087e6 | 5161 | if (sysfs_disk_to_scsi_id(fd, &id) == 0) |
b9f594fe | 5162 | dd->disk.scsi_id = __cpu_to_le32(id); |
c2c087e6 | 5163 | else |
b9f594fe | 5164 | dd->disk.scsi_id = __cpu_to_le32(0); |
43dad3d6 DW |
5165 | |
5166 | if (st->update_tail) { | |
1a64be56 LM |
5167 | dd->next = super->disk_mgmt_list; |
5168 | super->disk_mgmt_list = dd; | |
43dad3d6 DW |
5169 | } else { |
5170 | dd->next = super->disks; | |
5171 | super->disks = dd; | |
ceaf0ee1 | 5172 | super->updates_pending++; |
43dad3d6 | 5173 | } |
f20c3968 DW |
5174 | |
5175 | return 0; | |
cdddbdbc DW |
5176 | } |
5177 | ||
1a64be56 LM |
5178 | static int remove_from_super_imsm(struct supertype *st, mdu_disk_info_t *dk) |
5179 | { | |
5180 | struct intel_super *super = st->sb; | |
5181 | struct dl *dd; | |
5182 | ||
5183 | /* remove from super works only in mdmon - for communication | |
5184 | * manager - monitor. Check if communication memory buffer | |
5185 | * is prepared. | |
5186 | */ | |
5187 | if (!st->update_tail) { | |
1ade5cc1 | 5188 | pr_err("shall be used in mdmon context only\n"); |
1a64be56 LM |
5189 | return 1; |
5190 | } | |
503975b9 | 5191 | dd = xcalloc(1, sizeof(*dd)); |
1a64be56 LM |
5192 | dd->major = dk->major; |
5193 | dd->minor = dk->minor; | |
1a64be56 | 5194 | dd->fd = -1; |
a8619d23 | 5195 | mark_spare(dd); |
1a64be56 LM |
5196 | dd->action = DISK_REMOVE; |
5197 | ||
5198 | dd->next = super->disk_mgmt_list; | |
5199 | super->disk_mgmt_list = dd; | |
5200 | ||
1a64be56 LM |
5201 | return 0; |
5202 | } | |
5203 | ||
f796af5d DW |
5204 | static int store_imsm_mpb(int fd, struct imsm_super *mpb); |
5205 | ||
5206 | static union { | |
5207 | char buf[512]; | |
5208 | struct imsm_super anchor; | |
5209 | } spare_record __attribute__ ((aligned(512))); | |
c2c087e6 | 5210 | |
d23fe947 DW |
5211 | /* spare records have their own family number and do not have any defined raid |
5212 | * devices | |
5213 | */ | |
5214 | static int write_super_imsm_spares(struct intel_super *super, int doclose) | |
5215 | { | |
d23fe947 | 5216 | struct imsm_super *mpb = super->anchor; |
f796af5d | 5217 | struct imsm_super *spare = &spare_record.anchor; |
d23fe947 DW |
5218 | __u32 sum; |
5219 | struct dl *d; | |
5220 | ||
68641cdb JS |
5221 | spare->mpb_size = __cpu_to_le32(sizeof(struct imsm_super)); |
5222 | spare->generation_num = __cpu_to_le32(1UL); | |
f796af5d | 5223 | spare->attributes = MPB_ATTRIB_CHECKSUM_VERIFY; |
68641cdb JS |
5224 | spare->num_disks = 1; |
5225 | spare->num_raid_devs = 0; | |
5226 | spare->cache_size = mpb->cache_size; | |
5227 | spare->pwr_cycle_count = __cpu_to_le32(1); | |
f796af5d DW |
5228 | |
5229 | snprintf((char *) spare->sig, MAX_SIGNATURE_LENGTH, | |
5230 | MPB_SIGNATURE MPB_VERSION_RAID0); | |
d23fe947 DW |
5231 | |
5232 | for (d = super->disks; d; d = d->next) { | |
8796fdc4 | 5233 | if (d->index != -1) |
d23fe947 DW |
5234 | continue; |
5235 | ||
f796af5d | 5236 | spare->disk[0] = d->disk; |
027c374f CA |
5237 | if (__le32_to_cpu(d->disk.total_blocks_hi) > 0) |
5238 | spare->attributes |= MPB_ATTRIB_2TB_DISK; | |
5239 | ||
f796af5d DW |
5240 | sum = __gen_imsm_checksum(spare); |
5241 | spare->family_num = __cpu_to_le32(sum); | |
5242 | spare->orig_family_num = 0; | |
5243 | sum = __gen_imsm_checksum(spare); | |
5244 | spare->check_sum = __cpu_to_le32(sum); | |
d23fe947 | 5245 | |
f796af5d | 5246 | if (store_imsm_mpb(d->fd, spare)) { |
1ade5cc1 N |
5247 | pr_err("failed for device %d:%d %s\n", |
5248 | d->major, d->minor, strerror(errno)); | |
e74255d9 | 5249 | return 1; |
d23fe947 DW |
5250 | } |
5251 | if (doclose) { | |
5252 | close(d->fd); | |
5253 | d->fd = -1; | |
5254 | } | |
5255 | } | |
5256 | ||
e74255d9 | 5257 | return 0; |
d23fe947 DW |
5258 | } |
5259 | ||
36988a3d | 5260 | static int write_super_imsm(struct supertype *st, int doclose) |
cdddbdbc | 5261 | { |
36988a3d | 5262 | struct intel_super *super = st->sb; |
949c47a0 | 5263 | struct imsm_super *mpb = super->anchor; |
c2c087e6 DW |
5264 | struct dl *d; |
5265 | __u32 generation; | |
5266 | __u32 sum; | |
d23fe947 | 5267 | int spares = 0; |
949c47a0 | 5268 | int i; |
a48ac0a8 | 5269 | __u32 mpb_size = sizeof(struct imsm_super) - sizeof(struct imsm_disk); |
36988a3d | 5270 | int num_disks = 0; |
146c6260 | 5271 | int clear_migration_record = 1; |
cdddbdbc | 5272 | |
c2c087e6 DW |
5273 | /* 'generation' is incremented everytime the metadata is written */ |
5274 | generation = __le32_to_cpu(mpb->generation_num); | |
5275 | generation++; | |
5276 | mpb->generation_num = __cpu_to_le32(generation); | |
5277 | ||
148acb7b DW |
5278 | /* fix up cases where previous mdadm releases failed to set |
5279 | * orig_family_num | |
5280 | */ | |
5281 | if (mpb->orig_family_num == 0) | |
5282 | mpb->orig_family_num = mpb->family_num; | |
5283 | ||
d23fe947 | 5284 | for (d = super->disks; d; d = d->next) { |
8796fdc4 | 5285 | if (d->index == -1) |
d23fe947 | 5286 | spares++; |
36988a3d | 5287 | else { |
d23fe947 | 5288 | mpb->disk[d->index] = d->disk; |
36988a3d AK |
5289 | num_disks++; |
5290 | } | |
d23fe947 | 5291 | } |
36988a3d | 5292 | for (d = super->missing; d; d = d->next) { |
47ee5a45 | 5293 | mpb->disk[d->index] = d->disk; |
36988a3d AK |
5294 | num_disks++; |
5295 | } | |
5296 | mpb->num_disks = num_disks; | |
5297 | mpb_size += sizeof(struct imsm_disk) * mpb->num_disks; | |
b9f594fe | 5298 | |
949c47a0 DW |
5299 | for (i = 0; i < mpb->num_raid_devs; i++) { |
5300 | struct imsm_dev *dev = __get_imsm_dev(mpb, i); | |
36988a3d AK |
5301 | struct imsm_dev *dev2 = get_imsm_dev(super, i); |
5302 | if (dev && dev2) { | |
5303 | imsm_copy_dev(dev, dev2); | |
5304 | mpb_size += sizeof_imsm_dev(dev, 0); | |
5305 | } | |
146c6260 AK |
5306 | if (is_gen_migration(dev2)) |
5307 | clear_migration_record = 0; | |
949c47a0 | 5308 | } |
a48ac0a8 DW |
5309 | mpb_size += __le32_to_cpu(mpb->bbm_log_size); |
5310 | mpb->mpb_size = __cpu_to_le32(mpb_size); | |
949c47a0 | 5311 | |
c2c087e6 | 5312 | /* recalculate checksum */ |
949c47a0 | 5313 | sum = __gen_imsm_checksum(mpb); |
c2c087e6 DW |
5314 | mpb->check_sum = __cpu_to_le32(sum); |
5315 | ||
51d83f5d AK |
5316 | if (super->clean_migration_record_by_mdmon) { |
5317 | clear_migration_record = 1; | |
5318 | super->clean_migration_record_by_mdmon = 0; | |
5319 | } | |
146c6260 | 5320 | if (clear_migration_record) |
17a4eaf9 | 5321 | memset(super->migr_rec_buf, 0, MIGR_REC_BUF_SIZE); |
146c6260 | 5322 | |
d23fe947 | 5323 | /* write the mpb for disks that compose raid devices */ |
c2c087e6 | 5324 | for (d = super->disks; d ; d = d->next) { |
86c54047 | 5325 | if (d->index < 0 || is_failed(&d->disk)) |
d23fe947 | 5326 | continue; |
30602f53 | 5327 | |
146c6260 AK |
5328 | if (clear_migration_record) { |
5329 | unsigned long long dsize; | |
5330 | ||
5331 | get_dev_size(d->fd, NULL, &dsize); | |
5332 | if (lseek64(d->fd, dsize - 512, SEEK_SET) >= 0) { | |
17a4eaf9 AK |
5333 | if (write(d->fd, super->migr_rec_buf, |
5334 | MIGR_REC_BUF_SIZE) != MIGR_REC_BUF_SIZE) | |
9e2d750d | 5335 | perror("Write migr_rec failed"); |
146c6260 AK |
5336 | } |
5337 | } | |
51d83f5d AK |
5338 | |
5339 | if (store_imsm_mpb(d->fd, mpb)) | |
5340 | fprintf(stderr, | |
1ade5cc1 N |
5341 | "failed for device %d:%d (fd: %d)%s\n", |
5342 | d->major, d->minor, | |
51d83f5d AK |
5343 | d->fd, strerror(errno)); |
5344 | ||
c2c087e6 DW |
5345 | if (doclose) { |
5346 | close(d->fd); | |
5347 | d->fd = -1; | |
5348 | } | |
5349 | } | |
5350 | ||
d23fe947 DW |
5351 | if (spares) |
5352 | return write_super_imsm_spares(super, doclose); | |
5353 | ||
e74255d9 | 5354 | return 0; |
c2c087e6 DW |
5355 | } |
5356 | ||
9b1fb677 | 5357 | static int create_array(struct supertype *st, int dev_idx) |
43dad3d6 DW |
5358 | { |
5359 | size_t len; | |
5360 | struct imsm_update_create_array *u; | |
5361 | struct intel_super *super = st->sb; | |
9b1fb677 | 5362 | struct imsm_dev *dev = get_imsm_dev(super, dev_idx); |
238c0a71 | 5363 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
54c2c1ea DW |
5364 | struct disk_info *inf; |
5365 | struct imsm_disk *disk; | |
5366 | int i; | |
43dad3d6 | 5367 | |
54c2c1ea DW |
5368 | len = sizeof(*u) - sizeof(*dev) + sizeof_imsm_dev(dev, 0) + |
5369 | sizeof(*inf) * map->num_members; | |
503975b9 | 5370 | u = xmalloc(len); |
43dad3d6 | 5371 | u->type = update_create_array; |
9b1fb677 | 5372 | u->dev_idx = dev_idx; |
43dad3d6 | 5373 | imsm_copy_dev(&u->dev, dev); |
54c2c1ea DW |
5374 | inf = get_disk_info(u); |
5375 | for (i = 0; i < map->num_members; i++) { | |
238c0a71 | 5376 | int idx = get_imsm_disk_idx(dev, i, MAP_X); |
9b1fb677 | 5377 | |
54c2c1ea | 5378 | disk = get_imsm_disk(super, idx); |
1ca5c8e0 N |
5379 | if (!disk) |
5380 | disk = get_imsm_missing(super, idx); | |
54c2c1ea DW |
5381 | serialcpy(inf[i].serial, disk->serial); |
5382 | } | |
43dad3d6 DW |
5383 | append_metadata_update(st, u, len); |
5384 | ||
5385 | return 0; | |
5386 | } | |
5387 | ||
1a64be56 | 5388 | static int mgmt_disk(struct supertype *st) |
43dad3d6 DW |
5389 | { |
5390 | struct intel_super *super = st->sb; | |
5391 | size_t len; | |
1a64be56 | 5392 | struct imsm_update_add_remove_disk *u; |
43dad3d6 | 5393 | |
1a64be56 | 5394 | if (!super->disk_mgmt_list) |
43dad3d6 DW |
5395 | return 0; |
5396 | ||
5397 | len = sizeof(*u); | |
503975b9 | 5398 | u = xmalloc(len); |
1a64be56 | 5399 | u->type = update_add_remove_disk; |
43dad3d6 DW |
5400 | append_metadata_update(st, u, len); |
5401 | ||
5402 | return 0; | |
5403 | } | |
5404 | ||
c2c087e6 DW |
5405 | static int write_init_super_imsm(struct supertype *st) |
5406 | { | |
9b1fb677 DW |
5407 | struct intel_super *super = st->sb; |
5408 | int current_vol = super->current_vol; | |
5409 | ||
5410 | /* we are done with current_vol reset it to point st at the container */ | |
5411 | super->current_vol = -1; | |
5412 | ||
8273f55e | 5413 | if (st->update_tail) { |
43dad3d6 DW |
5414 | /* queue the recently created array / added disk |
5415 | * as a metadata update */ | |
43dad3d6 | 5416 | int rv; |
8273f55e | 5417 | |
43dad3d6 | 5418 | /* determine if we are creating a volume or adding a disk */ |
9b1fb677 | 5419 | if (current_vol < 0) { |
1a64be56 LM |
5420 | /* in the mgmt (add/remove) disk case we are running |
5421 | * in mdmon context, so don't close fd's | |
43dad3d6 | 5422 | */ |
1a64be56 | 5423 | return mgmt_disk(st); |
43dad3d6 | 5424 | } else |
9b1fb677 | 5425 | rv = create_array(st, current_vol); |
8273f55e | 5426 | |
43dad3d6 | 5427 | return rv; |
d682f344 N |
5428 | } else { |
5429 | struct dl *d; | |
5430 | for (d = super->disks; d; d = d->next) | |
ba728be7 | 5431 | Kill(d->devname, NULL, 0, -1, 1); |
36988a3d | 5432 | return write_super_imsm(st, 1); |
d682f344 | 5433 | } |
cdddbdbc | 5434 | } |
0e600426 | 5435 | #endif |
cdddbdbc | 5436 | |
e683ca88 | 5437 | static int store_super_imsm(struct supertype *st, int fd) |
cdddbdbc | 5438 | { |
e683ca88 DW |
5439 | struct intel_super *super = st->sb; |
5440 | struct imsm_super *mpb = super ? super->anchor : NULL; | |
551c80c1 | 5441 | |
e683ca88 | 5442 | if (!mpb) |
ad97895e DW |
5443 | return 1; |
5444 | ||
1799c9e8 | 5445 | #ifndef MDASSEMBLE |
e683ca88 | 5446 | return store_imsm_mpb(fd, mpb); |
1799c9e8 N |
5447 | #else |
5448 | return 1; | |
5449 | #endif | |
cdddbdbc DW |
5450 | } |
5451 | ||
0e600426 N |
5452 | static int imsm_bbm_log_size(struct imsm_super *mpb) |
5453 | { | |
5454 | return __le32_to_cpu(mpb->bbm_log_size); | |
5455 | } | |
5456 | ||
5457 | #ifndef MDASSEMBLE | |
cdddbdbc DW |
5458 | static int validate_geometry_imsm_container(struct supertype *st, int level, |
5459 | int layout, int raiddisks, int chunk, | |
af4348dd N |
5460 | unsigned long long size, |
5461 | unsigned long long data_offset, | |
5462 | char *dev, | |
2c514b71 NB |
5463 | unsigned long long *freesize, |
5464 | int verbose) | |
cdddbdbc | 5465 | { |
c2c087e6 DW |
5466 | int fd; |
5467 | unsigned long long ldsize; | |
594dc1b8 | 5468 | struct intel_super *super; |
f2f5c343 | 5469 | int rv = 0; |
cdddbdbc | 5470 | |
c2c087e6 DW |
5471 | if (level != LEVEL_CONTAINER) |
5472 | return 0; | |
5473 | if (!dev) | |
5474 | return 1; | |
5475 | ||
5476 | fd = open(dev, O_RDONLY|O_EXCL, 0); | |
5477 | if (fd < 0) { | |
ba728be7 | 5478 | if (verbose > 0) |
e7b84f9d | 5479 | pr_err("imsm: Cannot open %s: %s\n", |
2c514b71 | 5480 | dev, strerror(errno)); |
c2c087e6 DW |
5481 | return 0; |
5482 | } | |
5483 | if (!get_dev_size(fd, dev, &ldsize)) { | |
5484 | close(fd); | |
5485 | return 0; | |
5486 | } | |
f2f5c343 LM |
5487 | |
5488 | /* capabilities retrieve could be possible | |
5489 | * note that there is no fd for the disks in array. | |
5490 | */ | |
5491 | super = alloc_super(); | |
ba728be7 | 5492 | rv = find_intel_hba_capability(fd, super, verbose > 0 ? dev : NULL); |
f2f5c343 LM |
5493 | if (rv != 0) { |
5494 | #if DEBUG | |
5495 | char str[256]; | |
5496 | fd2devname(fd, str); | |
1ade5cc1 | 5497 | dprintf("fd: %d %s orom: %p rv: %d raiddisk: %d\n", |
f2f5c343 LM |
5498 | fd, str, super->orom, rv, raiddisks); |
5499 | #endif | |
5500 | /* no orom/efi or non-intel hba of the disk */ | |
5501 | close(fd); | |
5502 | free_imsm(super); | |
5503 | return 0; | |
5504 | } | |
c2c087e6 | 5505 | close(fd); |
9126b9a8 CA |
5506 | if (super->orom) { |
5507 | if (raiddisks > super->orom->tds) { | |
5508 | if (verbose) | |
7a862a02 | 5509 | pr_err("%d exceeds maximum number of platform supported disks: %d\n", |
9126b9a8 CA |
5510 | raiddisks, super->orom->tds); |
5511 | free_imsm(super); | |
5512 | return 0; | |
5513 | } | |
5514 | if ((super->orom->attr & IMSM_OROM_ATTR_2TB_DISK) == 0 && | |
5515 | (ldsize >> 9) >> 32 > 0) { | |
5516 | if (verbose) | |
e7b84f9d | 5517 | pr_err("%s exceeds maximum platform supported size\n", dev); |
9126b9a8 CA |
5518 | free_imsm(super); |
5519 | return 0; | |
5520 | } | |
f2f5c343 | 5521 | } |
c2c087e6 | 5522 | |
af4348dd | 5523 | *freesize = avail_size_imsm(st, ldsize >> 9, data_offset); |
f2f5c343 | 5524 | free_imsm(super); |
c2c087e6 DW |
5525 | |
5526 | return 1; | |
cdddbdbc DW |
5527 | } |
5528 | ||
0dcecb2e DW |
5529 | static unsigned long long find_size(struct extent *e, int *idx, int num_extents) |
5530 | { | |
5531 | const unsigned long long base_start = e[*idx].start; | |
5532 | unsigned long long end = base_start + e[*idx].size; | |
5533 | int i; | |
5534 | ||
5535 | if (base_start == end) | |
5536 | return 0; | |
5537 | ||
5538 | *idx = *idx + 1; | |
5539 | for (i = *idx; i < num_extents; i++) { | |
5540 | /* extend overlapping extents */ | |
5541 | if (e[i].start >= base_start && | |
5542 | e[i].start <= end) { | |
5543 | if (e[i].size == 0) | |
5544 | return 0; | |
5545 | if (e[i].start + e[i].size > end) | |
5546 | end = e[i].start + e[i].size; | |
5547 | } else if (e[i].start > end) { | |
5548 | *idx = i; | |
5549 | break; | |
5550 | } | |
5551 | } | |
5552 | ||
5553 | return end - base_start; | |
5554 | } | |
5555 | ||
5556 | static unsigned long long merge_extents(struct intel_super *super, int sum_extents) | |
5557 | { | |
5558 | /* build a composite disk with all known extents and generate a new | |
5559 | * 'maxsize' given the "all disks in an array must share a common start | |
5560 | * offset" constraint | |
5561 | */ | |
503975b9 | 5562 | struct extent *e = xcalloc(sum_extents, sizeof(*e)); |
0dcecb2e DW |
5563 | struct dl *dl; |
5564 | int i, j; | |
5565 | int start_extent; | |
5566 | unsigned long long pos; | |
b9d77223 | 5567 | unsigned long long start = 0; |
0dcecb2e DW |
5568 | unsigned long long maxsize; |
5569 | unsigned long reserve; | |
5570 | ||
0dcecb2e DW |
5571 | /* coalesce and sort all extents. also, check to see if we need to |
5572 | * reserve space between member arrays | |
5573 | */ | |
5574 | j = 0; | |
5575 | for (dl = super->disks; dl; dl = dl->next) { | |
5576 | if (!dl->e) | |
5577 | continue; | |
5578 | for (i = 0; i < dl->extent_cnt; i++) | |
5579 | e[j++] = dl->e[i]; | |
5580 | } | |
5581 | qsort(e, sum_extents, sizeof(*e), cmp_extent); | |
5582 | ||
5583 | /* merge extents */ | |
5584 | i = 0; | |
5585 | j = 0; | |
5586 | while (i < sum_extents) { | |
5587 | e[j].start = e[i].start; | |
5588 | e[j].size = find_size(e, &i, sum_extents); | |
5589 | j++; | |
5590 | if (e[j-1].size == 0) | |
5591 | break; | |
5592 | } | |
5593 | ||
5594 | pos = 0; | |
5595 | maxsize = 0; | |
5596 | start_extent = 0; | |
5597 | i = 0; | |
5598 | do { | |
5599 | unsigned long long esize; | |
5600 | ||
5601 | esize = e[i].start - pos; | |
5602 | if (esize >= maxsize) { | |
5603 | maxsize = esize; | |
5604 | start = pos; | |
5605 | start_extent = i; | |
5606 | } | |
5607 | pos = e[i].start + e[i].size; | |
5608 | i++; | |
5609 | } while (e[i-1].size); | |
5610 | free(e); | |
5611 | ||
a7dd165b DW |
5612 | if (maxsize == 0) |
5613 | return 0; | |
5614 | ||
5615 | /* FIXME assumes volume at offset 0 is the first volume in a | |
5616 | * container | |
5617 | */ | |
0dcecb2e DW |
5618 | if (start_extent > 0) |
5619 | reserve = IMSM_RESERVED_SECTORS; /* gap between raid regions */ | |
5620 | else | |
5621 | reserve = 0; | |
5622 | ||
5623 | if (maxsize < reserve) | |
a7dd165b | 5624 | return 0; |
0dcecb2e | 5625 | |
5551b113 | 5626 | super->create_offset = ~((unsigned long long) 0); |
0dcecb2e | 5627 | if (start + reserve > super->create_offset) |
a7dd165b | 5628 | return 0; /* start overflows create_offset */ |
0dcecb2e DW |
5629 | super->create_offset = start + reserve; |
5630 | ||
5631 | return maxsize - reserve; | |
5632 | } | |
5633 | ||
88c32bb1 DW |
5634 | static int is_raid_level_supported(const struct imsm_orom *orom, int level, int raiddisks) |
5635 | { | |
5636 | if (level < 0 || level == 6 || level == 4) | |
5637 | return 0; | |
5638 | ||
5639 | /* if we have an orom prevent invalid raid levels */ | |
5640 | if (orom) | |
5641 | switch (level) { | |
5642 | case 0: return imsm_orom_has_raid0(orom); | |
5643 | case 1: | |
5644 | if (raiddisks > 2) | |
5645 | return imsm_orom_has_raid1e(orom); | |
1c556e92 DW |
5646 | return imsm_orom_has_raid1(orom) && raiddisks == 2; |
5647 | case 10: return imsm_orom_has_raid10(orom) && raiddisks == 4; | |
5648 | case 5: return imsm_orom_has_raid5(orom) && raiddisks > 2; | |
88c32bb1 DW |
5649 | } |
5650 | else | |
5651 | return 1; /* not on an Intel RAID platform so anything goes */ | |
5652 | ||
5653 | return 0; | |
5654 | } | |
5655 | ||
ca9de185 LM |
5656 | static int |
5657 | active_arrays_by_format(char *name, char* hba, struct md_list **devlist, | |
5658 | int dpa, int verbose) | |
5659 | { | |
5660 | struct mdstat_ent *mdstat = mdstat_read(0, 0); | |
594dc1b8 | 5661 | struct mdstat_ent *memb; |
ca9de185 LM |
5662 | int count = 0; |
5663 | int num = 0; | |
594dc1b8 | 5664 | struct md_list *dv; |
ca9de185 LM |
5665 | int found; |
5666 | ||
5667 | for (memb = mdstat ; memb ; memb = memb->next) { | |
5668 | if (memb->metadata_version && | |
5669 | (strncmp(memb->metadata_version, "external:", 9) == 0) && | |
5670 | (strcmp(&memb->metadata_version[9], name) == 0) && | |
5671 | !is_subarray(memb->metadata_version+9) && | |
5672 | memb->members) { | |
5673 | struct dev_member *dev = memb->members; | |
5674 | int fd = -1; | |
5675 | while(dev && (fd < 0)) { | |
503975b9 N |
5676 | char *path = xmalloc(strlen(dev->name) + strlen("/dev/") + 1); |
5677 | num = sprintf(path, "%s%s", "/dev/", dev->name); | |
5678 | if (num > 0) | |
5679 | fd = open(path, O_RDONLY, 0); | |
5680 | if ((num <= 0) || (fd < 0)) { | |
5681 | pr_vrb(": Cannot open %s: %s\n", | |
5682 | dev->name, strerror(errno)); | |
ca9de185 | 5683 | } |
503975b9 | 5684 | free(path); |
ca9de185 LM |
5685 | dev = dev->next; |
5686 | } | |
5687 | found = 0; | |
5688 | if ((fd >= 0) && disk_attached_to_hba(fd, hba)) { | |
5689 | struct mdstat_ent *vol; | |
5690 | for (vol = mdstat ; vol ; vol = vol->next) { | |
5691 | if ((vol->active > 0) && | |
5692 | vol->metadata_version && | |
9581efb1 | 5693 | is_container_member(vol, memb->devnm)) { |
ca9de185 LM |
5694 | found++; |
5695 | count++; | |
5696 | } | |
5697 | } | |
5698 | if (*devlist && (found < dpa)) { | |
503975b9 | 5699 | dv = xcalloc(1, sizeof(*dv)); |
9581efb1 N |
5700 | dv->devname = xmalloc(strlen(memb->devnm) + strlen("/dev/") + 1); |
5701 | sprintf(dv->devname, "%s%s", "/dev/", memb->devnm); | |
503975b9 N |
5702 | dv->found = found; |
5703 | dv->used = 0; | |
5704 | dv->next = *devlist; | |
5705 | *devlist = dv; | |
ca9de185 LM |
5706 | } |
5707 | } | |
5708 | if (fd >= 0) | |
5709 | close(fd); | |
5710 | } | |
5711 | } | |
5712 | free_mdstat(mdstat); | |
5713 | return count; | |
5714 | } | |
5715 | ||
5716 | #ifdef DEBUG_LOOP | |
5717 | static struct md_list* | |
5718 | get_loop_devices(void) | |
5719 | { | |
5720 | int i; | |
5721 | struct md_list *devlist = NULL; | |
594dc1b8 | 5722 | struct md_list *dv; |
ca9de185 LM |
5723 | |
5724 | for(i = 0; i < 12; i++) { | |
503975b9 N |
5725 | dv = xcalloc(1, sizeof(*dv)); |
5726 | dv->devname = xmalloc(40); | |
ca9de185 LM |
5727 | sprintf(dv->devname, "/dev/loop%d", i); |
5728 | dv->next = devlist; | |
5729 | devlist = dv; | |
5730 | } | |
5731 | return devlist; | |
5732 | } | |
5733 | #endif | |
5734 | ||
5735 | static struct md_list* | |
5736 | get_devices(const char *hba_path) | |
5737 | { | |
5738 | struct md_list *devlist = NULL; | |
594dc1b8 | 5739 | struct md_list *dv; |
ca9de185 LM |
5740 | struct dirent *ent; |
5741 | DIR *dir; | |
5742 | int err = 0; | |
5743 | ||
5744 | #if DEBUG_LOOP | |
5745 | devlist = get_loop_devices(); | |
5746 | return devlist; | |
5747 | #endif | |
5748 | /* scroll through /sys/dev/block looking for devices attached to | |
5749 | * this hba | |
5750 | */ | |
5751 | dir = opendir("/sys/dev/block"); | |
5752 | for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) { | |
5753 | int fd; | |
5754 | char buf[1024]; | |
5755 | int major, minor; | |
5756 | char *path = NULL; | |
5757 | if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2) | |
5758 | continue; | |
5759 | path = devt_to_devpath(makedev(major, minor)); | |
5760 | if (!path) | |
5761 | continue; | |
5762 | if (!path_attached_to_hba(path, hba_path)) { | |
5763 | free(path); | |
5764 | path = NULL; | |
5765 | continue; | |
5766 | } | |
5767 | free(path); | |
5768 | path = NULL; | |
5769 | fd = dev_open(ent->d_name, O_RDONLY); | |
5770 | if (fd >= 0) { | |
5771 | fd2devname(fd, buf); | |
5772 | close(fd); | |
5773 | } else { | |
e7b84f9d | 5774 | pr_err("cannot open device: %s\n", |
ca9de185 LM |
5775 | ent->d_name); |
5776 | continue; | |
5777 | } | |
5778 | ||
503975b9 N |
5779 | dv = xcalloc(1, sizeof(*dv)); |
5780 | dv->devname = xstrdup(buf); | |
ca9de185 LM |
5781 | dv->next = devlist; |
5782 | devlist = dv; | |
5783 | } | |
5784 | if (err) { | |
5785 | while(devlist) { | |
5786 | dv = devlist; | |
5787 | devlist = devlist->next; | |
5788 | free(dv->devname); | |
5789 | free(dv); | |
5790 | } | |
5791 | } | |
562aa102 | 5792 | closedir(dir); |
ca9de185 LM |
5793 | return devlist; |
5794 | } | |
5795 | ||
5796 | static int | |
5797 | count_volumes_list(struct md_list *devlist, char *homehost, | |
5798 | int verbose, int *found) | |
5799 | { | |
5800 | struct md_list *tmpdev; | |
5801 | int count = 0; | |
594dc1b8 | 5802 | struct supertype *st; |
ca9de185 LM |
5803 | |
5804 | /* first walk the list of devices to find a consistent set | |
5805 | * that match the criterea, if that is possible. | |
5806 | * We flag the ones we like with 'used'. | |
5807 | */ | |
5808 | *found = 0; | |
5809 | st = match_metadata_desc_imsm("imsm"); | |
5810 | if (st == NULL) { | |
5811 | pr_vrb(": cannot allocate memory for imsm supertype\n"); | |
5812 | return 0; | |
5813 | } | |
5814 | ||
5815 | for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) { | |
5816 | char *devname = tmpdev->devname; | |
5817 | struct stat stb; | |
5818 | struct supertype *tst; | |
5819 | int dfd; | |
5820 | if (tmpdev->used > 1) | |
5821 | continue; | |
5822 | tst = dup_super(st); | |
5823 | if (tst == NULL) { | |
5824 | pr_vrb(": cannot allocate memory for imsm supertype\n"); | |
5825 | goto err_1; | |
5826 | } | |
5827 | tmpdev->container = 0; | |
5828 | dfd = dev_open(devname, O_RDONLY|O_EXCL); | |
5829 | if (dfd < 0) { | |
1ade5cc1 | 5830 | dprintf("cannot open device %s: %s\n", |
ca9de185 LM |
5831 | devname, strerror(errno)); |
5832 | tmpdev->used = 2; | |
5833 | } else if (fstat(dfd, &stb)< 0) { | |
5834 | /* Impossible! */ | |
1ade5cc1 | 5835 | dprintf("fstat failed for %s: %s\n", |
ca9de185 LM |
5836 | devname, strerror(errno)); |
5837 | tmpdev->used = 2; | |
5838 | } else if ((stb.st_mode & S_IFMT) != S_IFBLK) { | |
1ade5cc1 | 5839 | dprintf("%s is not a block device.\n", |
ca9de185 LM |
5840 | devname); |
5841 | tmpdev->used = 2; | |
5842 | } else if (must_be_container(dfd)) { | |
5843 | struct supertype *cst; | |
5844 | cst = super_by_fd(dfd, NULL); | |
5845 | if (cst == NULL) { | |
1ade5cc1 | 5846 | dprintf("cannot recognize container type %s\n", |
ca9de185 LM |
5847 | devname); |
5848 | tmpdev->used = 2; | |
5849 | } else if (tst->ss != st->ss) { | |
1ade5cc1 | 5850 | dprintf("non-imsm container - ignore it: %s\n", |
ca9de185 LM |
5851 | devname); |
5852 | tmpdev->used = 2; | |
5853 | } else if (!tst->ss->load_container || | |
5854 | tst->ss->load_container(tst, dfd, NULL)) | |
5855 | tmpdev->used = 2; | |
5856 | else { | |
5857 | tmpdev->container = 1; | |
5858 | } | |
5859 | if (cst) | |
5860 | cst->ss->free_super(cst); | |
5861 | } else { | |
5862 | tmpdev->st_rdev = stb.st_rdev; | |
5863 | if (tst->ss->load_super(tst,dfd, NULL)) { | |
1ade5cc1 | 5864 | dprintf("no RAID superblock on %s\n", |
ca9de185 LM |
5865 | devname); |
5866 | tmpdev->used = 2; | |
5867 | } else if (tst->ss->compare_super == NULL) { | |
1ade5cc1 | 5868 | dprintf("Cannot assemble %s metadata on %s\n", |
ca9de185 LM |
5869 | tst->ss->name, devname); |
5870 | tmpdev->used = 2; | |
5871 | } | |
5872 | } | |
5873 | if (dfd >= 0) | |
5874 | close(dfd); | |
5875 | if (tmpdev->used == 2 || tmpdev->used == 4) { | |
5876 | /* Ignore unrecognised devices during auto-assembly */ | |
5877 | goto loop; | |
5878 | } | |
5879 | else { | |
5880 | struct mdinfo info; | |
5881 | tst->ss->getinfo_super(tst, &info, NULL); | |
5882 | ||
5883 | if (st->minor_version == -1) | |
5884 | st->minor_version = tst->minor_version; | |
5885 | ||
5886 | if (memcmp(info.uuid, uuid_zero, | |
5887 | sizeof(int[4])) == 0) { | |
5888 | /* this is a floating spare. It cannot define | |
5889 | * an array unless there are no more arrays of | |
5890 | * this type to be found. It can be included | |
5891 | * in an array of this type though. | |
5892 | */ | |
5893 | tmpdev->used = 3; | |
5894 | goto loop; | |
5895 | } | |
5896 | ||
5897 | if (st->ss != tst->ss || | |
5898 | st->minor_version != tst->minor_version || | |
5899 | st->ss->compare_super(st, tst) != 0) { | |
5900 | /* Some mismatch. If exactly one array matches this host, | |
5901 | * we can resolve on that one. | |
5902 | * Or, if we are auto assembling, we just ignore the second | |
5903 | * for now. | |
5904 | */ | |
1ade5cc1 | 5905 | dprintf("superblock on %s doesn't match others - assembly aborted\n", |
ca9de185 LM |
5906 | devname); |
5907 | goto loop; | |
5908 | } | |
5909 | tmpdev->used = 1; | |
5910 | *found = 1; | |
5911 | dprintf("found: devname: %s\n", devname); | |
5912 | } | |
5913 | loop: | |
5914 | if (tst) | |
5915 | tst->ss->free_super(tst); | |
5916 | } | |
5917 | if (*found != 0) { | |
5918 | int err; | |
5919 | if ((err = load_super_imsm_all(st, -1, &st->sb, NULL, devlist, 0)) == 0) { | |
5920 | struct mdinfo *iter, *head = st->ss->container_content(st, NULL); | |
5921 | for (iter = head; iter; iter = iter->next) { | |
5922 | dprintf("content->text_version: %s vol\n", | |
5923 | iter->text_version); | |
5924 | if (iter->array.state & (1<<MD_SB_BLOCK_VOLUME)) { | |
5925 | /* do not assemble arrays with unsupported | |
5926 | configurations */ | |
1ade5cc1 | 5927 | dprintf("Cannot activate member %s.\n", |
ca9de185 LM |
5928 | iter->text_version); |
5929 | } else | |
5930 | count++; | |
5931 | } | |
5932 | sysfs_free(head); | |
5933 | ||
5934 | } else { | |
1ade5cc1 | 5935 | dprintf("No valid super block on device list: err: %d %p\n", |
ca9de185 LM |
5936 | err, st->sb); |
5937 | } | |
5938 | } else { | |
1ade5cc1 | 5939 | dprintf("no more devices to examine\n"); |
ca9de185 LM |
5940 | } |
5941 | ||
5942 | for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) { | |
5943 | if ((tmpdev->used == 1) && (tmpdev->found)) { | |
5944 | if (count) { | |
5945 | if (count < tmpdev->found) | |
5946 | count = 0; | |
5947 | else | |
5948 | count -= tmpdev->found; | |
5949 | } | |
5950 | } | |
5951 | if (tmpdev->used == 1) | |
5952 | tmpdev->used = 4; | |
5953 | } | |
5954 | err_1: | |
5955 | if (st) | |
5956 | st->ss->free_super(st); | |
5957 | return count; | |
5958 | } | |
5959 | ||
ca9de185 | 5960 | static int |
72a45777 | 5961 | count_volumes(struct intel_hba *hba, int dpa, int verbose) |
ca9de185 | 5962 | { |
72a45777 | 5963 | struct sys_dev *idev, *intel_devices = find_intel_devices(); |
ca9de185 | 5964 | int count = 0; |
72a45777 PB |
5965 | const struct orom_entry *entry; |
5966 | struct devid_list *dv, *devid_list; | |
ca9de185 | 5967 | |
72a45777 | 5968 | if (!hba || !hba->path) |
ca9de185 LM |
5969 | return 0; |
5970 | ||
72a45777 PB |
5971 | for (idev = intel_devices; idev; idev = idev->next) { |
5972 | if (strstr(idev->path, hba->path)) | |
5973 | break; | |
5974 | } | |
5975 | ||
5976 | if (!idev || !idev->dev_id) | |
ca9de185 | 5977 | return 0; |
72a45777 PB |
5978 | |
5979 | entry = get_orom_entry_by_device_id(idev->dev_id); | |
5980 | ||
5981 | if (!entry || !entry->devid_list) | |
5982 | return 0; | |
5983 | ||
5984 | devid_list = entry->devid_list; | |
5985 | for (dv = devid_list; dv; dv = dv->next) { | |
594dc1b8 | 5986 | struct md_list *devlist; |
72a45777 PB |
5987 | struct sys_dev *device = device_by_id(dv->devid); |
5988 | char *hba_path; | |
5989 | int found = 0; | |
5990 | ||
5991 | if (device) | |
5992 | hba_path = device->path; | |
5993 | else | |
5994 | return 0; | |
5995 | ||
60f0f54d PB |
5996 | /* VMD has one orom entry for all domain, but spanning is not allowed. |
5997 | * VMD arrays should be counted per domain (controller), so skip | |
5998 | * domains that are not the given one. | |
5999 | */ | |
6000 | if ((hba->type == SYS_DEV_VMD) && | |
6001 | (strncmp(device->path, hba->path, strlen(device->path)) != 0)) | |
6002 | continue; | |
6003 | ||
72a45777 PB |
6004 | devlist = get_devices(hba_path); |
6005 | /* if no intel devices return zero volumes */ | |
6006 | if (devlist == NULL) | |
6007 | return 0; | |
6008 | ||
6009 | count += active_arrays_by_format("imsm", hba_path, &devlist, dpa, verbose); | |
6010 | dprintf("path: %s active arrays: %d\n", hba_path, count); | |
6011 | if (devlist == NULL) | |
6012 | return 0; | |
6013 | do { | |
6014 | found = 0; | |
6015 | count += count_volumes_list(devlist, | |
6016 | NULL, | |
6017 | verbose, | |
6018 | &found); | |
6019 | dprintf("found %d count: %d\n", found, count); | |
6020 | } while (found); | |
6021 | ||
6022 | dprintf("path: %s total number of volumes: %d\n", hba_path, count); | |
6023 | ||
6024 | while (devlist) { | |
6025 | struct md_list *dv = devlist; | |
6026 | devlist = devlist->next; | |
6027 | free(dv->devname); | |
6028 | free(dv); | |
6029 | } | |
ca9de185 LM |
6030 | } |
6031 | return count; | |
6032 | } | |
6033 | ||
cd9d1ac7 DW |
6034 | static int imsm_default_chunk(const struct imsm_orom *orom) |
6035 | { | |
6036 | /* up to 512 if the plaform supports it, otherwise the platform max. | |
6037 | * 128 if no platform detected | |
6038 | */ | |
6039 | int fs = max(7, orom ? fls(orom->sss) : 0); | |
6040 | ||
6041 | return min(512, (1 << fs)); | |
6042 | } | |
73408129 | 6043 | |
6592ce37 DW |
6044 | static int |
6045 | validate_geometry_imsm_orom(struct intel_super *super, int level, int layout, | |
2cc699af | 6046 | int raiddisks, int *chunk, unsigned long long size, int verbose) |
6592ce37 | 6047 | { |
660260d0 DW |
6048 | /* check/set platform and metadata limits/defaults */ |
6049 | if (super->orom && raiddisks > super->orom->dpa) { | |
6050 | pr_vrb(": platform supports a maximum of %d disks per array\n", | |
6051 | super->orom->dpa); | |
73408129 LM |
6052 | return 0; |
6053 | } | |
6054 | ||
5d500228 | 6055 | /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */ |
660260d0 | 6056 | if (!is_raid_level_supported(super->orom, level, raiddisks)) { |
6592ce37 DW |
6057 | pr_vrb(": platform does not support raid%d with %d disk%s\n", |
6058 | level, raiddisks, raiddisks > 1 ? "s" : ""); | |
6059 | return 0; | |
6060 | } | |
cd9d1ac7 | 6061 | |
7ccc4cc4 | 6062 | if (*chunk == 0 || *chunk == UnSet) |
cd9d1ac7 DW |
6063 | *chunk = imsm_default_chunk(super->orom); |
6064 | ||
7ccc4cc4 | 6065 | if (super->orom && !imsm_orom_has_chunk(super->orom, *chunk)) { |
7a862a02 | 6066 | pr_vrb(": platform does not support a chunk size of: %d\n", *chunk); |
cd9d1ac7 | 6067 | return 0; |
6592ce37 | 6068 | } |
cd9d1ac7 | 6069 | |
6592ce37 DW |
6070 | if (layout != imsm_level_to_layout(level)) { |
6071 | if (level == 5) | |
6072 | pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n"); | |
6073 | else if (level == 10) | |
6074 | pr_vrb(": imsm raid 10 only supports the n2 layout\n"); | |
6075 | else | |
6076 | pr_vrb(": imsm unknown layout %#x for this raid level %d\n", | |
6077 | layout, level); | |
6078 | return 0; | |
6079 | } | |
2cc699af | 6080 | |
7ccc4cc4 | 6081 | if (super->orom && (super->orom->attr & IMSM_OROM_ATTR_2TB) == 0 && |
2cc699af CA |
6082 | (calc_array_size(level, raiddisks, layout, *chunk, size) >> 32) > 0) { |
6083 | pr_vrb(": platform does not support a volume size over 2TB\n"); | |
6084 | return 0; | |
6085 | } | |
614902f6 | 6086 | |
6592ce37 DW |
6087 | return 1; |
6088 | } | |
6089 | ||
1011e834 | 6090 | /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd |
c2c087e6 DW |
6091 | * FIX ME add ahci details |
6092 | */ | |
8b353278 | 6093 | static int validate_geometry_imsm_volume(struct supertype *st, int level, |
c21e737b | 6094 | int layout, int raiddisks, int *chunk, |
af4348dd N |
6095 | unsigned long long size, |
6096 | unsigned long long data_offset, | |
6097 | char *dev, | |
2c514b71 NB |
6098 | unsigned long long *freesize, |
6099 | int verbose) | |
cdddbdbc | 6100 | { |
c2c087e6 DW |
6101 | struct stat stb; |
6102 | struct intel_super *super = st->sb; | |
b2916f25 | 6103 | struct imsm_super *mpb; |
c2c087e6 DW |
6104 | struct dl *dl; |
6105 | unsigned long long pos = 0; | |
6106 | unsigned long long maxsize; | |
6107 | struct extent *e; | |
6108 | int i; | |
cdddbdbc | 6109 | |
88c32bb1 DW |
6110 | /* We must have the container info already read in. */ |
6111 | if (!super) | |
c2c087e6 DW |
6112 | return 0; |
6113 | ||
b2916f25 JS |
6114 | mpb = super->anchor; |
6115 | ||
2cc699af | 6116 | if (!validate_geometry_imsm_orom(super, level, layout, raiddisks, chunk, size, verbose)) { |
7a862a02 | 6117 | pr_err("RAID gemetry validation failed. Cannot proceed with the action(s).\n"); |
c2c087e6 | 6118 | return 0; |
d54559f0 | 6119 | } |
c2c087e6 DW |
6120 | if (!dev) { |
6121 | /* General test: make sure there is space for | |
2da8544a DW |
6122 | * 'raiddisks' device extents of size 'size' at a given |
6123 | * offset | |
c2c087e6 | 6124 | */ |
e46273eb | 6125 | unsigned long long minsize = size; |
b7528a20 | 6126 | unsigned long long start_offset = MaxSector; |
c2c087e6 DW |
6127 | int dcnt = 0; |
6128 | if (minsize == 0) | |
6129 | minsize = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS; | |
6130 | for (dl = super->disks; dl ; dl = dl->next) { | |
6131 | int found = 0; | |
6132 | ||
bf5a934a | 6133 | pos = 0; |
c2c087e6 DW |
6134 | i = 0; |
6135 | e = get_extents(super, dl); | |
6136 | if (!e) continue; | |
6137 | do { | |
6138 | unsigned long long esize; | |
6139 | esize = e[i].start - pos; | |
6140 | if (esize >= minsize) | |
6141 | found = 1; | |
b7528a20 | 6142 | if (found && start_offset == MaxSector) { |
2da8544a DW |
6143 | start_offset = pos; |
6144 | break; | |
6145 | } else if (found && pos != start_offset) { | |
6146 | found = 0; | |
6147 | break; | |
6148 | } | |
c2c087e6 DW |
6149 | pos = e[i].start + e[i].size; |
6150 | i++; | |
6151 | } while (e[i-1].size); | |
6152 | if (found) | |
6153 | dcnt++; | |
6154 | free(e); | |
6155 | } | |
6156 | if (dcnt < raiddisks) { | |
2c514b71 | 6157 | if (verbose) |
7a862a02 | 6158 | pr_err("imsm: Not enough devices with space for this array (%d < %d)\n", |
2c514b71 | 6159 | dcnt, raiddisks); |
c2c087e6 DW |
6160 | return 0; |
6161 | } | |
6162 | return 1; | |
6163 | } | |
0dcecb2e | 6164 | |
c2c087e6 DW |
6165 | /* This device must be a member of the set */ |
6166 | if (stat(dev, &stb) < 0) | |
6167 | return 0; | |
6168 | if ((S_IFMT & stb.st_mode) != S_IFBLK) | |
6169 | return 0; | |
6170 | for (dl = super->disks ; dl ; dl = dl->next) { | |
f21e18ca N |
6171 | if (dl->major == (int)major(stb.st_rdev) && |
6172 | dl->minor == (int)minor(stb.st_rdev)) | |
c2c087e6 DW |
6173 | break; |
6174 | } | |
6175 | if (!dl) { | |
2c514b71 | 6176 | if (verbose) |
7a862a02 | 6177 | pr_err("%s is not in the same imsm set\n", dev); |
c2c087e6 | 6178 | return 0; |
a20d2ba5 DW |
6179 | } else if (super->orom && dl->index < 0 && mpb->num_raid_devs) { |
6180 | /* If a volume is present then the current creation attempt | |
6181 | * cannot incorporate new spares because the orom may not | |
6182 | * understand this configuration (all member disks must be | |
6183 | * members of each array in the container). | |
6184 | */ | |
7a862a02 N |
6185 | pr_err("%s is a spare and a volume is already defined for this container\n", dev); |
6186 | pr_err("The option-rom requires all member disks to be a member of all volumes\n"); | |
a20d2ba5 | 6187 | return 0; |
5fe62b94 WD |
6188 | } else if (super->orom && mpb->num_raid_devs > 0 && |
6189 | mpb->num_disks != raiddisks) { | |
7a862a02 | 6190 | pr_err("The option-rom requires all member disks to be a member of all volumes\n"); |
5fe62b94 | 6191 | return 0; |
c2c087e6 | 6192 | } |
0dcecb2e DW |
6193 | |
6194 | /* retrieve the largest free space block */ | |
c2c087e6 DW |
6195 | e = get_extents(super, dl); |
6196 | maxsize = 0; | |
6197 | i = 0; | |
0dcecb2e DW |
6198 | if (e) { |
6199 | do { | |
6200 | unsigned long long esize; | |
6201 | ||
6202 | esize = e[i].start - pos; | |
6203 | if (esize >= maxsize) | |
6204 | maxsize = esize; | |
6205 | pos = e[i].start + e[i].size; | |
6206 | i++; | |
6207 | } while (e[i-1].size); | |
6208 | dl->e = e; | |
6209 | dl->extent_cnt = i; | |
6210 | } else { | |
6211 | if (verbose) | |
e7b84f9d | 6212 | pr_err("unable to determine free space for: %s\n", |
0dcecb2e DW |
6213 | dev); |
6214 | return 0; | |
6215 | } | |
6216 | if (maxsize < size) { | |
6217 | if (verbose) | |
e7b84f9d | 6218 | pr_err("%s not enough space (%llu < %llu)\n", |
0dcecb2e DW |
6219 | dev, maxsize, size); |
6220 | return 0; | |
6221 | } | |
6222 | ||
6223 | /* count total number of extents for merge */ | |
6224 | i = 0; | |
6225 | for (dl = super->disks; dl; dl = dl->next) | |
6226 | if (dl->e) | |
6227 | i += dl->extent_cnt; | |
6228 | ||
6229 | maxsize = merge_extents(super, i); | |
3baa56ab LO |
6230 | |
6231 | if (!check_env("IMSM_NO_PLATFORM") && | |
6232 | mpb->num_raid_devs > 0 && size && size != maxsize) { | |
7a862a02 | 6233 | pr_err("attempting to create a second volume with size less then remaining space. Aborting...\n"); |
3baa56ab LO |
6234 | return 0; |
6235 | } | |
6236 | ||
a7dd165b | 6237 | if (maxsize < size || maxsize == 0) { |
b3071342 LD |
6238 | if (verbose) { |
6239 | if (maxsize == 0) | |
7a862a02 | 6240 | pr_err("no free space left on device. Aborting...\n"); |
b3071342 | 6241 | else |
7a862a02 | 6242 | pr_err("not enough space to create volume of given size (%llu < %llu). Aborting...\n", |
b3071342 LD |
6243 | maxsize, size); |
6244 | } | |
0dcecb2e | 6245 | return 0; |
0dcecb2e DW |
6246 | } |
6247 | ||
c2c087e6 DW |
6248 | *freesize = maxsize; |
6249 | ||
ca9de185 | 6250 | if (super->orom) { |
72a45777 | 6251 | int count = count_volumes(super->hba, |
ca9de185 LM |
6252 | super->orom->dpa, verbose); |
6253 | if (super->orom->vphba <= count) { | |
40110b90 | 6254 | pr_vrb(": platform does not support more than %d raid volumes.\n", |
ca9de185 LM |
6255 | super->orom->vphba); |
6256 | return 0; | |
6257 | } | |
6258 | } | |
c2c087e6 | 6259 | return 1; |
cdddbdbc DW |
6260 | } |
6261 | ||
13bcac90 | 6262 | static int imsm_get_free_size(struct supertype *st, int raiddisks, |
efb30e7f DW |
6263 | unsigned long long size, int chunk, |
6264 | unsigned long long *freesize) | |
6265 | { | |
6266 | struct intel_super *super = st->sb; | |
6267 | struct imsm_super *mpb = super->anchor; | |
6268 | struct dl *dl; | |
6269 | int i; | |
6270 | int extent_cnt; | |
6271 | struct extent *e; | |
6272 | unsigned long long maxsize; | |
6273 | unsigned long long minsize; | |
6274 | int cnt; | |
6275 | int used; | |
6276 | ||
6277 | /* find the largest common start free region of the possible disks */ | |
6278 | used = 0; | |
6279 | extent_cnt = 0; | |
6280 | cnt = 0; | |
6281 | for (dl = super->disks; dl; dl = dl->next) { | |
6282 | dl->raiddisk = -1; | |
6283 | ||
6284 | if (dl->index >= 0) | |
6285 | used++; | |
6286 | ||
6287 | /* don't activate new spares if we are orom constrained | |
6288 | * and there is already a volume active in the container | |
6289 | */ | |
6290 | if (super->orom && dl->index < 0 && mpb->num_raid_devs) | |
6291 | continue; | |
6292 | ||
6293 | e = get_extents(super, dl); | |
6294 | if (!e) | |
6295 | continue; | |
6296 | for (i = 1; e[i-1].size; i++) | |
6297 | ; | |
6298 | dl->e = e; | |
6299 | dl->extent_cnt = i; | |
6300 | extent_cnt += i; | |
6301 | cnt++; | |
6302 | } | |
6303 | ||
6304 | maxsize = merge_extents(super, extent_cnt); | |
6305 | minsize = size; | |
6306 | if (size == 0) | |
612e59d8 CA |
6307 | /* chunk is in K */ |
6308 | minsize = chunk * 2; | |
efb30e7f DW |
6309 | |
6310 | if (cnt < raiddisks || | |
6311 | (super->orom && used && used != raiddisks) || | |
a7dd165b DW |
6312 | maxsize < minsize || |
6313 | maxsize == 0) { | |
e7b84f9d | 6314 | pr_err("not enough devices with space to create array.\n"); |
efb30e7f DW |
6315 | return 0; /* No enough free spaces large enough */ |
6316 | } | |
6317 | ||
6318 | if (size == 0) { | |
6319 | size = maxsize; | |
6320 | if (chunk) { | |
612e59d8 CA |
6321 | size /= 2 * chunk; |
6322 | size *= 2 * chunk; | |
efb30e7f | 6323 | } |
f878b242 LM |
6324 | maxsize = size; |
6325 | } | |
6326 | if (!check_env("IMSM_NO_PLATFORM") && | |
6327 | mpb->num_raid_devs > 0 && size && size != maxsize) { | |
7a862a02 | 6328 | pr_err("attempting to create a second volume with size less then remaining space. Aborting...\n"); |
f878b242 | 6329 | return 0; |
efb30e7f | 6330 | } |
efb30e7f DW |
6331 | cnt = 0; |
6332 | for (dl = super->disks; dl; dl = dl->next) | |
6333 | if (dl->e) | |
6334 | dl->raiddisk = cnt++; | |
6335 | ||
6336 | *freesize = size; | |
6337 | ||
13bcac90 AK |
6338 | dprintf("imsm: imsm_get_free_size() returns : %llu\n", size); |
6339 | ||
efb30e7f DW |
6340 | return 1; |
6341 | } | |
6342 | ||
13bcac90 AK |
6343 | static int reserve_space(struct supertype *st, int raiddisks, |
6344 | unsigned long long size, int chunk, | |
6345 | unsigned long long *freesize) | |
6346 | { | |
6347 | struct intel_super *super = st->sb; | |
6348 | struct dl *dl; | |
6349 | int cnt; | |
6350 | int rv = 0; | |
6351 | ||
6352 | rv = imsm_get_free_size(st, raiddisks, size, chunk, freesize); | |
6353 | if (rv) { | |
6354 | cnt = 0; | |
6355 | for (dl = super->disks; dl; dl = dl->next) | |
6356 | if (dl->e) | |
6357 | dl->raiddisk = cnt++; | |
6358 | rv = 1; | |
6359 | } | |
6360 | ||
6361 | return rv; | |
6362 | } | |
6363 | ||
bf5a934a | 6364 | static int validate_geometry_imsm(struct supertype *st, int level, int layout, |
c21e737b | 6365 | int raiddisks, int *chunk, unsigned long long size, |
af4348dd | 6366 | unsigned long long data_offset, |
bf5a934a DW |
6367 | char *dev, unsigned long long *freesize, |
6368 | int verbose) | |
6369 | { | |
6370 | int fd, cfd; | |
6371 | struct mdinfo *sra; | |
20cbe8d2 | 6372 | int is_member = 0; |
bf5a934a | 6373 | |
d54559f0 LM |
6374 | /* load capability |
6375 | * if given unused devices create a container | |
bf5a934a DW |
6376 | * if given given devices in a container create a member volume |
6377 | */ | |
6378 | if (level == LEVEL_CONTAINER) { | |
6379 | /* Must be a fresh device to add to a container */ | |
6380 | return validate_geometry_imsm_container(st, level, layout, | |
c21e737b | 6381 | raiddisks, |
7ccc4cc4 | 6382 | *chunk, |
af4348dd | 6383 | size, data_offset, |
bf5a934a DW |
6384 | dev, freesize, |
6385 | verbose); | |
6386 | } | |
9587c373 | 6387 | |
8592f29d | 6388 | if (!dev) { |
e91a3bad | 6389 | if (st->sb) { |
ca9de185 | 6390 | struct intel_super *super = st->sb; |
e91a3bad | 6391 | if (!validate_geometry_imsm_orom(st->sb, level, layout, |
2cc699af | 6392 | raiddisks, chunk, size, |
e91a3bad LM |
6393 | verbose)) |
6394 | return 0; | |
efb30e7f DW |
6395 | /* we are being asked to automatically layout a |
6396 | * new volume based on the current contents of | |
6397 | * the container. If the the parameters can be | |
6398 | * satisfied reserve_space will record the disks, | |
6399 | * start offset, and size of the volume to be | |
6400 | * created. add_to_super and getinfo_super | |
6401 | * detect when autolayout is in progress. | |
6402 | */ | |
ca9de185 LM |
6403 | /* assuming that freesize is always given when array is |
6404 | created */ | |
6405 | if (super->orom && freesize) { | |
6406 | int count; | |
72a45777 | 6407 | count = count_volumes(super->hba, |
ca9de185 LM |
6408 | super->orom->dpa, verbose); |
6409 | if (super->orom->vphba <= count) { | |
7a862a02 | 6410 | pr_vrb(": platform does not support more than %d raid volumes.\n", |
ca9de185 LM |
6411 | super->orom->vphba); |
6412 | return 0; | |
6413 | } | |
6414 | } | |
e91a3bad LM |
6415 | if (freesize) |
6416 | return reserve_space(st, raiddisks, size, | |
7ccc4cc4 | 6417 | *chunk, freesize); |
8592f29d N |
6418 | } |
6419 | return 1; | |
6420 | } | |
bf5a934a DW |
6421 | if (st->sb) { |
6422 | /* creating in a given container */ | |
6423 | return validate_geometry_imsm_volume(st, level, layout, | |
6424 | raiddisks, chunk, size, | |
af4348dd | 6425 | data_offset, |
bf5a934a DW |
6426 | dev, freesize, verbose); |
6427 | } | |
6428 | ||
bf5a934a DW |
6429 | /* This device needs to be a device in an 'imsm' container */ |
6430 | fd = open(dev, O_RDONLY|O_EXCL, 0); | |
6431 | if (fd >= 0) { | |
6432 | if (verbose) | |
e7b84f9d N |
6433 | pr_err("Cannot create this array on device %s\n", |
6434 | dev); | |
bf5a934a DW |
6435 | close(fd); |
6436 | return 0; | |
6437 | } | |
6438 | if (errno != EBUSY || (fd = open(dev, O_RDONLY, 0)) < 0) { | |
6439 | if (verbose) | |
e7b84f9d | 6440 | pr_err("Cannot open %s: %s\n", |
bf5a934a DW |
6441 | dev, strerror(errno)); |
6442 | return 0; | |
6443 | } | |
6444 | /* Well, it is in use by someone, maybe an 'imsm' container. */ | |
6445 | cfd = open_container(fd); | |
20cbe8d2 | 6446 | close(fd); |
bf5a934a | 6447 | if (cfd < 0) { |
bf5a934a | 6448 | if (verbose) |
e7b84f9d | 6449 | pr_err("Cannot use %s: It is busy\n", |
bf5a934a DW |
6450 | dev); |
6451 | return 0; | |
6452 | } | |
4dd2df09 | 6453 | sra = sysfs_read(cfd, NULL, GET_VERSION); |
bf5a934a | 6454 | if (sra && sra->array.major_version == -1 && |
20cbe8d2 AW |
6455 | strcmp(sra->text_version, "imsm") == 0) |
6456 | is_member = 1; | |
6457 | sysfs_free(sra); | |
6458 | if (is_member) { | |
bf5a934a DW |
6459 | /* This is a member of a imsm container. Load the container |
6460 | * and try to create a volume | |
6461 | */ | |
6462 | struct intel_super *super; | |
6463 | ||
ec50f7b6 | 6464 | if (load_super_imsm_all(st, cfd, (void **) &super, NULL, NULL, 1) == 0) { |
bf5a934a | 6465 | st->sb = super; |
4dd2df09 | 6466 | strcpy(st->container_devnm, fd2devnm(cfd)); |
bf5a934a DW |
6467 | close(cfd); |
6468 | return validate_geometry_imsm_volume(st, level, layout, | |
6469 | raiddisks, chunk, | |
af4348dd | 6470 | size, data_offset, dev, |
ecbd9e81 N |
6471 | freesize, 1) |
6472 | ? 1 : -1; | |
bf5a934a | 6473 | } |
20cbe8d2 | 6474 | } |
bf5a934a | 6475 | |
20cbe8d2 | 6476 | if (verbose) |
e7b84f9d | 6477 | pr_err("failed container membership check\n"); |
20cbe8d2 AW |
6478 | |
6479 | close(cfd); | |
6480 | return 0; | |
bf5a934a | 6481 | } |
0bd16cf2 | 6482 | |
30f58b22 | 6483 | static void default_geometry_imsm(struct supertype *st, int *level, int *layout, int *chunk) |
0bd16cf2 DJ |
6484 | { |
6485 | struct intel_super *super = st->sb; | |
6486 | ||
30f58b22 DW |
6487 | if (level && *level == UnSet) |
6488 | *level = LEVEL_CONTAINER; | |
6489 | ||
6490 | if (level && layout && *layout == UnSet) | |
6491 | *layout = imsm_level_to_layout(*level); | |
0bd16cf2 | 6492 | |
cd9d1ac7 DW |
6493 | if (chunk && (*chunk == UnSet || *chunk == 0)) |
6494 | *chunk = imsm_default_chunk(super->orom); | |
0bd16cf2 DJ |
6495 | } |
6496 | ||
33414a01 DW |
6497 | static void handle_missing(struct intel_super *super, struct imsm_dev *dev); |
6498 | ||
6499 | static int kill_subarray_imsm(struct supertype *st) | |
6500 | { | |
6501 | /* remove the subarray currently referenced by ->current_vol */ | |
6502 | __u8 i; | |
6503 | struct intel_dev **dp; | |
6504 | struct intel_super *super = st->sb; | |
6505 | __u8 current_vol = super->current_vol; | |
6506 | struct imsm_super *mpb = super->anchor; | |
6507 | ||
6508 | if (super->current_vol < 0) | |
6509 | return 2; | |
6510 | super->current_vol = -1; /* invalidate subarray cursor */ | |
6511 | ||
6512 | /* block deletions that would change the uuid of active subarrays | |
6513 | * | |
6514 | * FIXME when immutable ids are available, but note that we'll | |
6515 | * also need to fixup the invalidated/active subarray indexes in | |
6516 | * mdstat | |
6517 | */ | |
6518 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
6519 | char subarray[4]; | |
6520 | ||
6521 | if (i < current_vol) | |
6522 | continue; | |
6523 | sprintf(subarray, "%u", i); | |
4dd2df09 | 6524 | if (is_subarray_active(subarray, st->devnm)) { |
e7b84f9d N |
6525 | pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n", |
6526 | current_vol, i); | |
33414a01 DW |
6527 | |
6528 | return 2; | |
6529 | } | |
6530 | } | |
6531 | ||
6532 | if (st->update_tail) { | |
503975b9 | 6533 | struct imsm_update_kill_array *u = xmalloc(sizeof(*u)); |
33414a01 | 6534 | |
33414a01 DW |
6535 | u->type = update_kill_array; |
6536 | u->dev_idx = current_vol; | |
6537 | append_metadata_update(st, u, sizeof(*u)); | |
6538 | ||
6539 | return 0; | |
6540 | } | |
6541 | ||
6542 | for (dp = &super->devlist; *dp;) | |
6543 | if ((*dp)->index == current_vol) { | |
6544 | *dp = (*dp)->next; | |
6545 | } else { | |
6546 | handle_missing(super, (*dp)->dev); | |
6547 | if ((*dp)->index > current_vol) | |
6548 | (*dp)->index--; | |
6549 | dp = &(*dp)->next; | |
6550 | } | |
6551 | ||
6552 | /* no more raid devices, all active components are now spares, | |
6553 | * but of course failed are still failed | |
6554 | */ | |
6555 | if (--mpb->num_raid_devs == 0) { | |
6556 | struct dl *d; | |
6557 | ||
6558 | for (d = super->disks; d; d = d->next) | |
a8619d23 AK |
6559 | if (d->index > -2) |
6560 | mark_spare(d); | |
33414a01 DW |
6561 | } |
6562 | ||
6563 | super->updates_pending++; | |
6564 | ||
6565 | return 0; | |
6566 | } | |
aa534678 | 6567 | |
a951a4f7 | 6568 | static int update_subarray_imsm(struct supertype *st, char *subarray, |
fa56eddb | 6569 | char *update, struct mddev_ident *ident) |
aa534678 DW |
6570 | { |
6571 | /* update the subarray currently referenced by ->current_vol */ | |
6572 | struct intel_super *super = st->sb; | |
6573 | struct imsm_super *mpb = super->anchor; | |
6574 | ||
aa534678 DW |
6575 | if (strcmp(update, "name") == 0) { |
6576 | char *name = ident->name; | |
a951a4f7 N |
6577 | char *ep; |
6578 | int vol; | |
aa534678 | 6579 | |
4dd2df09 | 6580 | if (is_subarray_active(subarray, st->devnm)) { |
e7b84f9d | 6581 | pr_err("Unable to update name of active subarray\n"); |
aa534678 DW |
6582 | return 2; |
6583 | } | |
6584 | ||
6585 | if (!check_name(super, name, 0)) | |
6586 | return 2; | |
6587 | ||
a951a4f7 N |
6588 | vol = strtoul(subarray, &ep, 10); |
6589 | if (*ep != '\0' || vol >= super->anchor->num_raid_devs) | |
6590 | return 2; | |
6591 | ||
aa534678 | 6592 | if (st->update_tail) { |
503975b9 | 6593 | struct imsm_update_rename_array *u = xmalloc(sizeof(*u)); |
aa534678 | 6594 | |
aa534678 | 6595 | u->type = update_rename_array; |
a951a4f7 | 6596 | u->dev_idx = vol; |
aa534678 DW |
6597 | snprintf((char *) u->name, MAX_RAID_SERIAL_LEN, "%s", name); |
6598 | append_metadata_update(st, u, sizeof(*u)); | |
6599 | } else { | |
6600 | struct imsm_dev *dev; | |
6601 | int i; | |
6602 | ||
a951a4f7 | 6603 | dev = get_imsm_dev(super, vol); |
aa534678 DW |
6604 | snprintf((char *) dev->volume, MAX_RAID_SERIAL_LEN, "%s", name); |
6605 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
6606 | dev = get_imsm_dev(super, i); | |
6607 | handle_missing(super, dev); | |
6608 | } | |
6609 | super->updates_pending++; | |
6610 | } | |
6611 | } else | |
6612 | return 2; | |
6613 | ||
6614 | return 0; | |
6615 | } | |
d1e02575 | 6616 | #endif /* MDASSEMBLE */ |
bf5a934a | 6617 | |
28bce06f AK |
6618 | static int is_gen_migration(struct imsm_dev *dev) |
6619 | { | |
7534230b AK |
6620 | if (dev == NULL) |
6621 | return 0; | |
6622 | ||
28bce06f AK |
6623 | if (!dev->vol.migr_state) |
6624 | return 0; | |
6625 | ||
6626 | if (migr_type(dev) == MIGR_GEN_MIGR) | |
6627 | return 1; | |
6628 | ||
6629 | return 0; | |
6630 | } | |
6631 | ||
1e5c6983 DW |
6632 | static int is_rebuilding(struct imsm_dev *dev) |
6633 | { | |
6634 | struct imsm_map *migr_map; | |
6635 | ||
6636 | if (!dev->vol.migr_state) | |
6637 | return 0; | |
6638 | ||
6639 | if (migr_type(dev) != MIGR_REBUILD) | |
6640 | return 0; | |
6641 | ||
238c0a71 | 6642 | migr_map = get_imsm_map(dev, MAP_1); |
1e5c6983 DW |
6643 | |
6644 | if (migr_map->map_state == IMSM_T_STATE_DEGRADED) | |
6645 | return 1; | |
6646 | else | |
6647 | return 0; | |
6648 | } | |
6649 | ||
b4ab44d8 | 6650 | #ifndef MDASSEMBLE |
6ce1fbf1 AK |
6651 | static int is_initializing(struct imsm_dev *dev) |
6652 | { | |
6653 | struct imsm_map *migr_map; | |
6654 | ||
6655 | if (!dev->vol.migr_state) | |
6656 | return 0; | |
6657 | ||
6658 | if (migr_type(dev) != MIGR_INIT) | |
6659 | return 0; | |
6660 | ||
238c0a71 | 6661 | migr_map = get_imsm_map(dev, MAP_1); |
6ce1fbf1 AK |
6662 | |
6663 | if (migr_map->map_state == IMSM_T_STATE_UNINITIALIZED) | |
6664 | return 1; | |
6665 | ||
6666 | return 0; | |
6ce1fbf1 | 6667 | } |
b4ab44d8 | 6668 | #endif |
6ce1fbf1 | 6669 | |
c47b0ff6 AK |
6670 | static void update_recovery_start(struct intel_super *super, |
6671 | struct imsm_dev *dev, | |
6672 | struct mdinfo *array) | |
1e5c6983 DW |
6673 | { |
6674 | struct mdinfo *rebuild = NULL; | |
6675 | struct mdinfo *d; | |
6676 | __u32 units; | |
6677 | ||
6678 | if (!is_rebuilding(dev)) | |
6679 | return; | |
6680 | ||
6681 | /* Find the rebuild target, but punt on the dual rebuild case */ | |
6682 | for (d = array->devs; d; d = d->next) | |
6683 | if (d->recovery_start == 0) { | |
6684 | if (rebuild) | |
6685 | return; | |
6686 | rebuild = d; | |
6687 | } | |
6688 | ||
4363fd80 DW |
6689 | if (!rebuild) { |
6690 | /* (?) none of the disks are marked with | |
6691 | * IMSM_ORD_REBUILD, so assume they are missing and the | |
6692 | * disk_ord_tbl was not correctly updated | |
6693 | */ | |
1ade5cc1 | 6694 | dprintf("failed to locate out-of-sync disk\n"); |
4363fd80 DW |
6695 | return; |
6696 | } | |
6697 | ||
1e5c6983 | 6698 | units = __le32_to_cpu(dev->vol.curr_migr_unit); |
c47b0ff6 | 6699 | rebuild->recovery_start = units * blocks_per_migr_unit(super, dev); |
1e5c6983 DW |
6700 | } |
6701 | ||
9e2d750d | 6702 | #ifndef MDASSEMBLE |
276d77db | 6703 | static int recover_backup_imsm(struct supertype *st, struct mdinfo *info); |
9e2d750d | 6704 | #endif |
1e5c6983 | 6705 | |
00bbdbda | 6706 | static struct mdinfo *container_content_imsm(struct supertype *st, char *subarray) |
cdddbdbc | 6707 | { |
4f5bc454 DW |
6708 | /* Given a container loaded by load_super_imsm_all, |
6709 | * extract information about all the arrays into | |
6710 | * an mdinfo tree. | |
00bbdbda | 6711 | * If 'subarray' is given, just extract info about that array. |
4f5bc454 DW |
6712 | * |
6713 | * For each imsm_dev create an mdinfo, fill it in, | |
6714 | * then look for matching devices in super->disks | |
6715 | * and create appropriate device mdinfo. | |
6716 | */ | |
6717 | struct intel_super *super = st->sb; | |
949c47a0 | 6718 | struct imsm_super *mpb = super->anchor; |
4f5bc454 | 6719 | struct mdinfo *rest = NULL; |
00bbdbda | 6720 | unsigned int i; |
81219e70 | 6721 | int sb_errors = 0; |
abef11a3 AK |
6722 | struct dl *d; |
6723 | int spare_disks = 0; | |
cdddbdbc | 6724 | |
19482bcc AK |
6725 | /* do not assemble arrays when not all attributes are supported */ |
6726 | if (imsm_check_attributes(mpb->attributes) == 0) { | |
81219e70 | 6727 | sb_errors = 1; |
7a862a02 | 6728 | pr_err("Unsupported attributes in IMSM metadata.Arrays activation is blocked.\n"); |
19482bcc AK |
6729 | } |
6730 | ||
a06d022d | 6731 | /* check for bad blocks */ |
81219e70 | 6732 | if (imsm_bbm_log_size(super->anchor)) { |
7a862a02 | 6733 | pr_err("BBM log found in IMSM metadata.Arrays activation is blocked.\n"); |
81219e70 LM |
6734 | sb_errors = 1; |
6735 | } | |
6736 | ||
abef11a3 AK |
6737 | /* count spare devices, not used in maps |
6738 | */ | |
6739 | for (d = super->disks; d; d = d->next) | |
6740 | if (d->index == -1) | |
6741 | spare_disks++; | |
6742 | ||
4f5bc454 | 6743 | for (i = 0; i < mpb->num_raid_devs; i++) { |
00bbdbda N |
6744 | struct imsm_dev *dev; |
6745 | struct imsm_map *map; | |
86e3692b | 6746 | struct imsm_map *map2; |
4f5bc454 | 6747 | struct mdinfo *this; |
a6482415 N |
6748 | int slot; |
6749 | #ifndef MDASSEMBLE | |
6750 | int chunk; | |
6751 | #endif | |
00bbdbda N |
6752 | char *ep; |
6753 | ||
6754 | if (subarray && | |
6755 | (i != strtoul(subarray, &ep, 10) || *ep != '\0')) | |
6756 | continue; | |
6757 | ||
6758 | dev = get_imsm_dev(super, i); | |
238c0a71 AK |
6759 | map = get_imsm_map(dev, MAP_0); |
6760 | map2 = get_imsm_map(dev, MAP_1); | |
4f5bc454 | 6761 | |
1ce0101c DW |
6762 | /* do not publish arrays that are in the middle of an |
6763 | * unsupported migration | |
6764 | */ | |
6765 | if (dev->vol.migr_state && | |
28bce06f | 6766 | (migr_type(dev) == MIGR_STATE_CHANGE)) { |
7a862a02 | 6767 | pr_err("cannot assemble volume '%.16s': unsupported migration in progress\n", |
1ce0101c DW |
6768 | dev->volume); |
6769 | continue; | |
6770 | } | |
2db86302 LM |
6771 | /* do not publish arrays that are not support by controller's |
6772 | * OROM/EFI | |
6773 | */ | |
1ce0101c | 6774 | |
503975b9 | 6775 | this = xmalloc(sizeof(*this)); |
4f5bc454 | 6776 | |
301406c9 | 6777 | super->current_vol = i; |
a5d85af7 | 6778 | getinfo_super_imsm_volume(st, this, NULL); |
9894ec0d | 6779 | this->next = rest; |
81219e70 | 6780 | #ifndef MDASSEMBLE |
a6482415 | 6781 | chunk = __le16_to_cpu(map->blocks_per_strip) >> 1; |
81219e70 LM |
6782 | /* mdadm does not support all metadata features- set the bit in all arrays state */ |
6783 | if (!validate_geometry_imsm_orom(super, | |
6784 | get_imsm_raid_level(map), /* RAID level */ | |
6785 | imsm_level_to_layout(get_imsm_raid_level(map)), | |
6786 | map->num_members, /* raid disks */ | |
2cc699af | 6787 | &chunk, join_u32(dev->size_low, dev->size_high), |
81219e70 | 6788 | 1 /* verbose */)) { |
7a862a02 | 6789 | pr_err("IMSM RAID geometry validation failed. Array %s activation is blocked.\n", |
81219e70 LM |
6790 | dev->volume); |
6791 | this->array.state |= | |
6792 | (1<<MD_SB_BLOCK_CONTAINER_RESHAPE) | | |
6793 | (1<<MD_SB_BLOCK_VOLUME); | |
6794 | } | |
6795 | #endif | |
6796 | ||
6797 | /* if array has bad blocks, set suitable bit in all arrays state */ | |
6798 | if (sb_errors) | |
6799 | this->array.state |= | |
6800 | (1<<MD_SB_BLOCK_CONTAINER_RESHAPE) | | |
6801 | (1<<MD_SB_BLOCK_VOLUME); | |
6802 | ||
4f5bc454 | 6803 | for (slot = 0 ; slot < map->num_members; slot++) { |
1e5c6983 | 6804 | unsigned long long recovery_start; |
4f5bc454 DW |
6805 | struct mdinfo *info_d; |
6806 | struct dl *d; | |
6807 | int idx; | |
9a1608e5 | 6808 | int skip; |
7eef0453 | 6809 | __u32 ord; |
4f5bc454 | 6810 | |
9a1608e5 | 6811 | skip = 0; |
238c0a71 AK |
6812 | idx = get_imsm_disk_idx(dev, slot, MAP_0); |
6813 | ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X); | |
4f5bc454 DW |
6814 | for (d = super->disks; d ; d = d->next) |
6815 | if (d->index == idx) | |
0fbd635c | 6816 | break; |
4f5bc454 | 6817 | |
1e5c6983 | 6818 | recovery_start = MaxSector; |
4f5bc454 | 6819 | if (d == NULL) |
9a1608e5 | 6820 | skip = 1; |
25ed7e59 | 6821 | if (d && is_failed(&d->disk)) |
9a1608e5 | 6822 | skip = 1; |
7eef0453 | 6823 | if (ord & IMSM_ORD_REBUILD) |
1e5c6983 | 6824 | recovery_start = 0; |
9a1608e5 | 6825 | |
1011e834 | 6826 | /* |
9a1608e5 | 6827 | * if we skip some disks the array will be assmebled degraded; |
1e5c6983 DW |
6828 | * reset resync start to avoid a dirty-degraded |
6829 | * situation when performing the intial sync | |
9a1608e5 DW |
6830 | * |
6831 | * FIXME handle dirty degraded | |
6832 | */ | |
1e5c6983 | 6833 | if ((skip || recovery_start == 0) && !dev->vol.dirty) |
b7528a20 | 6834 | this->resync_start = MaxSector; |
9a1608e5 DW |
6835 | if (skip) |
6836 | continue; | |
4f5bc454 | 6837 | |
503975b9 | 6838 | info_d = xcalloc(1, sizeof(*info_d)); |
4f5bc454 DW |
6839 | info_d->next = this->devs; |
6840 | this->devs = info_d; | |
6841 | ||
4f5bc454 DW |
6842 | info_d->disk.number = d->index; |
6843 | info_d->disk.major = d->major; | |
6844 | info_d->disk.minor = d->minor; | |
6845 | info_d->disk.raid_disk = slot; | |
1e5c6983 | 6846 | info_d->recovery_start = recovery_start; |
86e3692b AK |
6847 | if (map2) { |
6848 | if (slot < map2->num_members) | |
6849 | info_d->disk.state = (1 << MD_DISK_ACTIVE); | |
04c3c514 AK |
6850 | else |
6851 | this->array.spare_disks++; | |
86e3692b AK |
6852 | } else { |
6853 | if (slot < map->num_members) | |
6854 | info_d->disk.state = (1 << MD_DISK_ACTIVE); | |
04c3c514 AK |
6855 | else |
6856 | this->array.spare_disks++; | |
86e3692b | 6857 | } |
1e5c6983 DW |
6858 | if (info_d->recovery_start == MaxSector) |
6859 | this->array.working_disks++; | |
4f5bc454 DW |
6860 | |
6861 | info_d->events = __le32_to_cpu(mpb->generation_num); | |
5551b113 CA |
6862 | info_d->data_offset = pba_of_lba0(map); |
6863 | info_d->component_size = blocks_per_member(map); | |
4f5bc454 | 6864 | } |
1e5c6983 | 6865 | /* now that the disk list is up-to-date fixup recovery_start */ |
c47b0ff6 | 6866 | update_recovery_start(super, dev, this); |
abef11a3 | 6867 | this->array.spare_disks += spare_disks; |
276d77db | 6868 | |
9e2d750d | 6869 | #ifndef MDASSEMBLE |
276d77db AK |
6870 | /* check for reshape */ |
6871 | if (this->reshape_active == 1) | |
6872 | recover_backup_imsm(st, this); | |
9e2d750d | 6873 | #endif |
9a1608e5 | 6874 | rest = this; |
4f5bc454 DW |
6875 | } |
6876 | ||
6877 | return rest; | |
cdddbdbc DW |
6878 | } |
6879 | ||
3b451610 AK |
6880 | static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev, |
6881 | int failed, int look_in_map) | |
c2a1e7da | 6882 | { |
3b451610 AK |
6883 | struct imsm_map *map; |
6884 | ||
6885 | map = get_imsm_map(dev, look_in_map); | |
c2a1e7da DW |
6886 | |
6887 | if (!failed) | |
1011e834 | 6888 | return map->map_state == IMSM_T_STATE_UNINITIALIZED ? |
3393c6af | 6889 | IMSM_T_STATE_UNINITIALIZED : IMSM_T_STATE_NORMAL; |
c2a1e7da DW |
6890 | |
6891 | switch (get_imsm_raid_level(map)) { | |
6892 | case 0: | |
6893 | return IMSM_T_STATE_FAILED; | |
6894 | break; | |
6895 | case 1: | |
6896 | if (failed < map->num_members) | |
6897 | return IMSM_T_STATE_DEGRADED; | |
6898 | else | |
6899 | return IMSM_T_STATE_FAILED; | |
6900 | break; | |
6901 | case 10: | |
6902 | { | |
6903 | /** | |
c92a2527 DW |
6904 | * check to see if any mirrors have failed, otherwise we |
6905 | * are degraded. Even numbered slots are mirrored on | |
6906 | * slot+1 | |
c2a1e7da | 6907 | */ |
c2a1e7da | 6908 | int i; |
d9b420a5 N |
6909 | /* gcc -Os complains that this is unused */ |
6910 | int insync = insync; | |
c2a1e7da DW |
6911 | |
6912 | for (i = 0; i < map->num_members; i++) { | |
238c0a71 | 6913 | __u32 ord = get_imsm_ord_tbl_ent(dev, i, MAP_X); |
c92a2527 DW |
6914 | int idx = ord_to_idx(ord); |
6915 | struct imsm_disk *disk; | |
c2a1e7da | 6916 | |
c92a2527 | 6917 | /* reset the potential in-sync count on even-numbered |
1011e834 | 6918 | * slots. num_copies is always 2 for imsm raid10 |
c92a2527 DW |
6919 | */ |
6920 | if ((i & 1) == 0) | |
6921 | insync = 2; | |
c2a1e7da | 6922 | |
c92a2527 | 6923 | disk = get_imsm_disk(super, idx); |
25ed7e59 | 6924 | if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD) |
c92a2527 | 6925 | insync--; |
c2a1e7da | 6926 | |
c92a2527 DW |
6927 | /* no in-sync disks left in this mirror the |
6928 | * array has failed | |
6929 | */ | |
6930 | if (insync == 0) | |
6931 | return IMSM_T_STATE_FAILED; | |
c2a1e7da DW |
6932 | } |
6933 | ||
6934 | return IMSM_T_STATE_DEGRADED; | |
6935 | } | |
6936 | case 5: | |
6937 | if (failed < 2) | |
6938 | return IMSM_T_STATE_DEGRADED; | |
6939 | else | |
6940 | return IMSM_T_STATE_FAILED; | |
6941 | break; | |
6942 | default: | |
6943 | break; | |
6944 | } | |
6945 | ||
6946 | return map->map_state; | |
6947 | } | |
6948 | ||
3b451610 AK |
6949 | static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev, |
6950 | int look_in_map) | |
c2a1e7da DW |
6951 | { |
6952 | int i; | |
6953 | int failed = 0; | |
6954 | struct imsm_disk *disk; | |
d5985138 AK |
6955 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
6956 | struct imsm_map *prev = get_imsm_map(dev, MAP_1); | |
68fe4598 | 6957 | struct imsm_map *map_for_loop; |
0556e1a2 DW |
6958 | __u32 ord; |
6959 | int idx; | |
d5985138 | 6960 | int idx_1; |
c2a1e7da | 6961 | |
0556e1a2 DW |
6962 | /* at the beginning of migration we set IMSM_ORD_REBUILD on |
6963 | * disks that are being rebuilt. New failures are recorded to | |
6964 | * map[0]. So we look through all the disks we started with and | |
6965 | * see if any failures are still present, or if any new ones | |
6966 | * have arrived | |
0556e1a2 | 6967 | */ |
d5985138 AK |
6968 | map_for_loop = map; |
6969 | if (prev && (map->num_members < prev->num_members)) | |
6970 | map_for_loop = prev; | |
68fe4598 LD |
6971 | |
6972 | for (i = 0; i < map_for_loop->num_members; i++) { | |
d5985138 | 6973 | idx_1 = -255; |
238c0a71 AK |
6974 | /* when MAP_X is passed both maps failures are counted |
6975 | */ | |
d5985138 | 6976 | if (prev && |
238c0a71 AK |
6977 | ((look_in_map == MAP_1) || (look_in_map == MAP_X)) && |
6978 | (i < prev->num_members)) { | |
d5985138 AK |
6979 | ord = __le32_to_cpu(prev->disk_ord_tbl[i]); |
6980 | idx_1 = ord_to_idx(ord); | |
c2a1e7da | 6981 | |
d5985138 AK |
6982 | disk = get_imsm_disk(super, idx_1); |
6983 | if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD) | |
6984 | failed++; | |
6985 | } | |
238c0a71 AK |
6986 | if (((look_in_map == MAP_0) || (look_in_map == MAP_X)) && |
6987 | (i < map->num_members)) { | |
d5985138 AK |
6988 | ord = __le32_to_cpu(map->disk_ord_tbl[i]); |
6989 | idx = ord_to_idx(ord); | |
6990 | ||
6991 | if (idx != idx_1) { | |
6992 | disk = get_imsm_disk(super, idx); | |
6993 | if (!disk || is_failed(disk) || | |
6994 | ord & IMSM_ORD_REBUILD) | |
6995 | failed++; | |
6996 | } | |
6997 | } | |
c2a1e7da DW |
6998 | } |
6999 | ||
7000 | return failed; | |
845dea95 NB |
7001 | } |
7002 | ||
97b4d0e9 DW |
7003 | #ifndef MDASSEMBLE |
7004 | static int imsm_open_new(struct supertype *c, struct active_array *a, | |
7005 | char *inst) | |
7006 | { | |
7007 | struct intel_super *super = c->sb; | |
7008 | struct imsm_super *mpb = super->anchor; | |
9587c373 | 7009 | |
97b4d0e9 | 7010 | if (atoi(inst) >= mpb->num_raid_devs) { |
1ade5cc1 | 7011 | pr_err("subarry index %d, out of range\n", atoi(inst)); |
97b4d0e9 DW |
7012 | return -ENODEV; |
7013 | } | |
7014 | ||
7015 | dprintf("imsm: open_new %s\n", inst); | |
7016 | a->info.container_member = atoi(inst); | |
7017 | return 0; | |
7018 | } | |
7019 | ||
0c046afd DW |
7020 | static int is_resyncing(struct imsm_dev *dev) |
7021 | { | |
7022 | struct imsm_map *migr_map; | |
7023 | ||
7024 | if (!dev->vol.migr_state) | |
7025 | return 0; | |
7026 | ||
1484e727 DW |
7027 | if (migr_type(dev) == MIGR_INIT || |
7028 | migr_type(dev) == MIGR_REPAIR) | |
0c046afd DW |
7029 | return 1; |
7030 | ||
4c9bc37b AK |
7031 | if (migr_type(dev) == MIGR_GEN_MIGR) |
7032 | return 0; | |
7033 | ||
238c0a71 | 7034 | migr_map = get_imsm_map(dev, MAP_1); |
0c046afd | 7035 | |
4c9bc37b AK |
7036 | if ((migr_map->map_state == IMSM_T_STATE_NORMAL) && |
7037 | (dev->vol.migr_type != MIGR_GEN_MIGR)) | |
0c046afd DW |
7038 | return 1; |
7039 | else | |
7040 | return 0; | |
7041 | } | |
7042 | ||
0556e1a2 DW |
7043 | /* return true if we recorded new information */ |
7044 | static int mark_failure(struct imsm_dev *dev, struct imsm_disk *disk, int idx) | |
47ee5a45 | 7045 | { |
0556e1a2 DW |
7046 | __u32 ord; |
7047 | int slot; | |
7048 | struct imsm_map *map; | |
86c54047 DW |
7049 | char buf[MAX_RAID_SERIAL_LEN+3]; |
7050 | unsigned int len, shift = 0; | |
0556e1a2 DW |
7051 | |
7052 | /* new failures are always set in map[0] */ | |
238c0a71 | 7053 | map = get_imsm_map(dev, MAP_0); |
0556e1a2 DW |
7054 | |
7055 | slot = get_imsm_disk_slot(map, idx); | |
7056 | if (slot < 0) | |
7057 | return 0; | |
7058 | ||
7059 | ord = __le32_to_cpu(map->disk_ord_tbl[slot]); | |
25ed7e59 | 7060 | if (is_failed(disk) && (ord & IMSM_ORD_REBUILD)) |
0556e1a2 DW |
7061 | return 0; |
7062 | ||
7d0c5e24 LD |
7063 | memcpy(buf, disk->serial, MAX_RAID_SERIAL_LEN); |
7064 | buf[MAX_RAID_SERIAL_LEN] = '\000'; | |
7065 | strcat(buf, ":0"); | |
86c54047 DW |
7066 | if ((len = strlen(buf)) >= MAX_RAID_SERIAL_LEN) |
7067 | shift = len - MAX_RAID_SERIAL_LEN + 1; | |
7068 | strncpy((char *)disk->serial, &buf[shift], MAX_RAID_SERIAL_LEN); | |
7069 | ||
f2f27e63 | 7070 | disk->status |= FAILED_DISK; |
0556e1a2 | 7071 | set_imsm_ord_tbl_ent(map, slot, idx | IMSM_ORD_REBUILD); |
17788994 AK |
7072 | /* mark failures in second map if second map exists and this disk |
7073 | * in this slot. | |
7074 | * This is valid for migration, initialization and rebuild | |
7075 | */ | |
7076 | if (dev->vol.migr_state) { | |
238c0a71 | 7077 | struct imsm_map *map2 = get_imsm_map(dev, MAP_1); |
0a108d63 AK |
7078 | int slot2 = get_imsm_disk_slot(map2, idx); |
7079 | ||
7080 | if ((slot2 < map2->num_members) && | |
7081 | (slot2 >= 0)) | |
7082 | set_imsm_ord_tbl_ent(map2, slot2, | |
1ace8403 AK |
7083 | idx | IMSM_ORD_REBUILD); |
7084 | } | |
f21e18ca | 7085 | if (map->failed_disk_num == 0xff) |
0556e1a2 DW |
7086 | map->failed_disk_num = slot; |
7087 | return 1; | |
7088 | } | |
7089 | ||
7090 | static void mark_missing(struct imsm_dev *dev, struct imsm_disk *disk, int idx) | |
7091 | { | |
7092 | mark_failure(dev, disk, idx); | |
7093 | ||
7094 | if (disk->scsi_id == __cpu_to_le32(~(__u32)0)) | |
7095 | return; | |
7096 | ||
47ee5a45 DW |
7097 | disk->scsi_id = __cpu_to_le32(~(__u32)0); |
7098 | memmove(&disk->serial[0], &disk->serial[1], MAX_RAID_SERIAL_LEN - 1); | |
7099 | } | |
7100 | ||
33414a01 DW |
7101 | static void handle_missing(struct intel_super *super, struct imsm_dev *dev) |
7102 | { | |
33414a01 | 7103 | struct dl *dl; |
33414a01 DW |
7104 | |
7105 | if (!super->missing) | |
7106 | return; | |
33414a01 | 7107 | |
79b68f1b PC |
7108 | /* When orom adds replacement for missing disk it does |
7109 | * not remove entry of missing disk, but just updates map with | |
7110 | * new added disk. So it is not enough just to test if there is | |
7111 | * any missing disk, we have to look if there are any failed disks | |
7112 | * in map to stop migration */ | |
7113 | ||
33414a01 | 7114 | dprintf("imsm: mark missing\n"); |
3d59f0c0 AK |
7115 | /* end process for initialization and rebuild only |
7116 | */ | |
7117 | if (is_gen_migration(dev) == 0) { | |
7118 | __u8 map_state; | |
7119 | int failed; | |
7120 | ||
7121 | failed = imsm_count_failed(super, dev, MAP_0); | |
7122 | map_state = imsm_check_degraded(super, dev, failed, MAP_0); | |
7123 | ||
79b68f1b PC |
7124 | if (failed) |
7125 | end_migration(dev, super, map_state); | |
3d59f0c0 | 7126 | } |
33414a01 DW |
7127 | for (dl = super->missing; dl; dl = dl->next) |
7128 | mark_missing(dev, &dl->disk, dl->index); | |
7129 | super->updates_pending++; | |
7130 | } | |
7131 | ||
f3871fdc AK |
7132 | static unsigned long long imsm_set_array_size(struct imsm_dev *dev, |
7133 | long long new_size) | |
70bdf0dc | 7134 | { |
238c0a71 | 7135 | int used_disks = imsm_num_data_members(dev, MAP_0); |
70bdf0dc AK |
7136 | unsigned long long array_blocks; |
7137 | struct imsm_map *map; | |
7138 | ||
7139 | if (used_disks == 0) { | |
7140 | /* when problems occures | |
7141 | * return current array_blocks value | |
7142 | */ | |
7143 | array_blocks = __le32_to_cpu(dev->size_high); | |
7144 | array_blocks = array_blocks << 32; | |
7145 | array_blocks += __le32_to_cpu(dev->size_low); | |
7146 | ||
7147 | return array_blocks; | |
7148 | } | |
7149 | ||
7150 | /* set array size in metadata | |
7151 | */ | |
f3871fdc AK |
7152 | if (new_size <= 0) { |
7153 | /* OLCE size change is caused by added disks | |
7154 | */ | |
7155 | map = get_imsm_map(dev, MAP_0); | |
7156 | array_blocks = blocks_per_member(map) * used_disks; | |
7157 | } else { | |
7158 | /* Online Volume Size Change | |
7159 | * Using available free space | |
7160 | */ | |
7161 | array_blocks = new_size; | |
7162 | } | |
70bdf0dc AK |
7163 | |
7164 | /* round array size down to closest MB | |
7165 | */ | |
7166 | array_blocks = (array_blocks >> SECT_PER_MB_SHIFT) << SECT_PER_MB_SHIFT; | |
7167 | dev->size_low = __cpu_to_le32((__u32)array_blocks); | |
7168 | dev->size_high = __cpu_to_le32((__u32)(array_blocks >> 32)); | |
7169 | ||
7170 | return array_blocks; | |
7171 | } | |
7172 | ||
28bce06f AK |
7173 | static void imsm_set_disk(struct active_array *a, int n, int state); |
7174 | ||
0e2d1a4e AK |
7175 | static void imsm_progress_container_reshape(struct intel_super *super) |
7176 | { | |
7177 | /* if no device has a migr_state, but some device has a | |
7178 | * different number of members than the previous device, start | |
7179 | * changing the number of devices in this device to match | |
7180 | * previous. | |
7181 | */ | |
7182 | struct imsm_super *mpb = super->anchor; | |
7183 | int prev_disks = -1; | |
7184 | int i; | |
1dfaa380 | 7185 | int copy_map_size; |
0e2d1a4e AK |
7186 | |
7187 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
7188 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
238c0a71 | 7189 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
0e2d1a4e AK |
7190 | struct imsm_map *map2; |
7191 | int prev_num_members; | |
0e2d1a4e AK |
7192 | |
7193 | if (dev->vol.migr_state) | |
7194 | return; | |
7195 | ||
7196 | if (prev_disks == -1) | |
7197 | prev_disks = map->num_members; | |
7198 | if (prev_disks == map->num_members) | |
7199 | continue; | |
7200 | ||
7201 | /* OK, this array needs to enter reshape mode. | |
7202 | * i.e it needs a migr_state | |
7203 | */ | |
7204 | ||
1dfaa380 | 7205 | copy_map_size = sizeof_imsm_map(map); |
0e2d1a4e AK |
7206 | prev_num_members = map->num_members; |
7207 | map->num_members = prev_disks; | |
7208 | dev->vol.migr_state = 1; | |
7209 | dev->vol.curr_migr_unit = 0; | |
ea672ee1 | 7210 | set_migr_type(dev, MIGR_GEN_MIGR); |
0e2d1a4e AK |
7211 | for (i = prev_num_members; |
7212 | i < map->num_members; i++) | |
7213 | set_imsm_ord_tbl_ent(map, i, i); | |
238c0a71 | 7214 | map2 = get_imsm_map(dev, MAP_1); |
0e2d1a4e | 7215 | /* Copy the current map */ |
1dfaa380 | 7216 | memcpy(map2, map, copy_map_size); |
0e2d1a4e AK |
7217 | map2->num_members = prev_num_members; |
7218 | ||
f3871fdc | 7219 | imsm_set_array_size(dev, -1); |
51d83f5d | 7220 | super->clean_migration_record_by_mdmon = 1; |
0e2d1a4e AK |
7221 | super->updates_pending++; |
7222 | } | |
7223 | } | |
7224 | ||
aad6f216 | 7225 | /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild |
0c046afd DW |
7226 | * states are handled in imsm_set_disk() with one exception, when a |
7227 | * resync is stopped due to a new failure this routine will set the | |
7228 | * 'degraded' state for the array. | |
7229 | */ | |
01f157d7 | 7230 | static int imsm_set_array_state(struct active_array *a, int consistent) |
a862209d DW |
7231 | { |
7232 | int inst = a->info.container_member; | |
7233 | struct intel_super *super = a->container->sb; | |
949c47a0 | 7234 | struct imsm_dev *dev = get_imsm_dev(super, inst); |
238c0a71 | 7235 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
3b451610 AK |
7236 | int failed = imsm_count_failed(super, dev, MAP_0); |
7237 | __u8 map_state = imsm_check_degraded(super, dev, failed, MAP_0); | |
1e5c6983 | 7238 | __u32 blocks_per_unit; |
a862209d | 7239 | |
1af97990 AK |
7240 | if (dev->vol.migr_state && |
7241 | dev->vol.migr_type == MIGR_GEN_MIGR) { | |
7242 | /* array state change is blocked due to reshape action | |
aad6f216 N |
7243 | * We might need to |
7244 | * - abort the reshape (if last_checkpoint is 0 and action!= reshape) | |
7245 | * - finish the reshape (if last_checkpoint is big and action != reshape) | |
7246 | * - update curr_migr_unit | |
1af97990 | 7247 | */ |
aad6f216 N |
7248 | if (a->curr_action == reshape) { |
7249 | /* still reshaping, maybe update curr_migr_unit */ | |
633b5610 | 7250 | goto mark_checkpoint; |
aad6f216 N |
7251 | } else { |
7252 | if (a->last_checkpoint == 0 && a->prev_action == reshape) { | |
7253 | /* for some reason we aborted the reshape. | |
b66e591b AK |
7254 | * |
7255 | * disable automatic metadata rollback | |
7256 | * user action is required to recover process | |
aad6f216 | 7257 | */ |
b66e591b | 7258 | if (0) { |
238c0a71 AK |
7259 | struct imsm_map *map2 = |
7260 | get_imsm_map(dev, MAP_1); | |
7261 | dev->vol.migr_state = 0; | |
7262 | set_migr_type(dev, 0); | |
7263 | dev->vol.curr_migr_unit = 0; | |
7264 | memcpy(map, map2, | |
7265 | sizeof_imsm_map(map2)); | |
7266 | super->updates_pending++; | |
b66e591b | 7267 | } |
aad6f216 N |
7268 | } |
7269 | if (a->last_checkpoint >= a->info.component_size) { | |
7270 | unsigned long long array_blocks; | |
7271 | int used_disks; | |
e154ced3 | 7272 | struct mdinfo *mdi; |
aad6f216 | 7273 | |
238c0a71 | 7274 | used_disks = imsm_num_data_members(dev, MAP_0); |
d55adef9 AK |
7275 | if (used_disks > 0) { |
7276 | array_blocks = | |
5551b113 | 7277 | blocks_per_member(map) * |
d55adef9 AK |
7278 | used_disks; |
7279 | /* round array size down to closest MB | |
7280 | */ | |
7281 | array_blocks = (array_blocks | |
7282 | >> SECT_PER_MB_SHIFT) | |
7283 | << SECT_PER_MB_SHIFT; | |
d55adef9 AK |
7284 | a->info.custom_array_size = array_blocks; |
7285 | /* encourage manager to update array | |
7286 | * size | |
7287 | */ | |
e154ced3 | 7288 | |
d55adef9 | 7289 | a->check_reshape = 1; |
633b5610 | 7290 | } |
e154ced3 AK |
7291 | /* finalize online capacity expansion/reshape */ |
7292 | for (mdi = a->info.devs; mdi; mdi = mdi->next) | |
7293 | imsm_set_disk(a, | |
7294 | mdi->disk.raid_disk, | |
7295 | mdi->curr_state); | |
7296 | ||
0e2d1a4e | 7297 | imsm_progress_container_reshape(super); |
e154ced3 | 7298 | } |
aad6f216 | 7299 | } |
1af97990 AK |
7300 | } |
7301 | ||
47ee5a45 | 7302 | /* before we activate this array handle any missing disks */ |
33414a01 DW |
7303 | if (consistent == 2) |
7304 | handle_missing(super, dev); | |
1e5c6983 | 7305 | |
0c046afd | 7306 | if (consistent == 2 && |
b7941fd6 | 7307 | (!is_resync_complete(&a->info) || |
0c046afd DW |
7308 | map_state != IMSM_T_STATE_NORMAL || |
7309 | dev->vol.migr_state)) | |
01f157d7 | 7310 | consistent = 0; |
272906ef | 7311 | |
b7941fd6 | 7312 | if (is_resync_complete(&a->info)) { |
0c046afd | 7313 | /* complete intialization / resync, |
0556e1a2 DW |
7314 | * recovery and interrupted recovery is completed in |
7315 | * ->set_disk | |
0c046afd DW |
7316 | */ |
7317 | if (is_resyncing(dev)) { | |
7318 | dprintf("imsm: mark resync done\n"); | |
809da78e | 7319 | end_migration(dev, super, map_state); |
115c3803 | 7320 | super->updates_pending++; |
484240d8 | 7321 | a->last_checkpoint = 0; |
115c3803 | 7322 | } |
b9172665 AK |
7323 | } else if ((!is_resyncing(dev) && !failed) && |
7324 | (imsm_reshape_blocks_arrays_changes(super) == 0)) { | |
0c046afd | 7325 | /* mark the start of the init process if nothing is failed */ |
b7941fd6 | 7326 | dprintf("imsm: mark resync start\n"); |
1484e727 | 7327 | if (map->map_state == IMSM_T_STATE_UNINITIALIZED) |
8e59f3d8 | 7328 | migrate(dev, super, IMSM_T_STATE_NORMAL, MIGR_INIT); |
1484e727 | 7329 | else |
8e59f3d8 | 7330 | migrate(dev, super, IMSM_T_STATE_NORMAL, MIGR_REPAIR); |
3393c6af | 7331 | super->updates_pending++; |
115c3803 | 7332 | } |
a862209d | 7333 | |
633b5610 | 7334 | mark_checkpoint: |
5b83bacf AK |
7335 | /* skip checkpointing for general migration, |
7336 | * it is controlled in mdadm | |
7337 | */ | |
7338 | if (is_gen_migration(dev)) | |
7339 | goto skip_mark_checkpoint; | |
7340 | ||
1e5c6983 | 7341 | /* check if we can update curr_migr_unit from resync_start, recovery_start */ |
c47b0ff6 | 7342 | blocks_per_unit = blocks_per_migr_unit(super, dev); |
4f0a7acc | 7343 | if (blocks_per_unit) { |
1e5c6983 DW |
7344 | __u32 units32; |
7345 | __u64 units; | |
7346 | ||
4f0a7acc | 7347 | units = a->last_checkpoint / blocks_per_unit; |
1e5c6983 DW |
7348 | units32 = units; |
7349 | ||
7350 | /* check that we did not overflow 32-bits, and that | |
7351 | * curr_migr_unit needs updating | |
7352 | */ | |
7353 | if (units32 == units && | |
bfd80a56 | 7354 | units32 != 0 && |
1e5c6983 DW |
7355 | __le32_to_cpu(dev->vol.curr_migr_unit) != units32) { |
7356 | dprintf("imsm: mark checkpoint (%u)\n", units32); | |
7357 | dev->vol.curr_migr_unit = __cpu_to_le32(units32); | |
7358 | super->updates_pending++; | |
7359 | } | |
7360 | } | |
f8f603f1 | 7361 | |
5b83bacf | 7362 | skip_mark_checkpoint: |
3393c6af | 7363 | /* mark dirty / clean */ |
0c046afd | 7364 | if (dev->vol.dirty != !consistent) { |
b7941fd6 | 7365 | dprintf("imsm: mark '%s'\n", consistent ? "clean" : "dirty"); |
0c046afd DW |
7366 | if (consistent) |
7367 | dev->vol.dirty = 0; | |
7368 | else | |
7369 | dev->vol.dirty = 1; | |
a862209d DW |
7370 | super->updates_pending++; |
7371 | } | |
28bce06f | 7372 | |
01f157d7 | 7373 | return consistent; |
a862209d DW |
7374 | } |
7375 | ||
8d45d196 | 7376 | static void imsm_set_disk(struct active_array *a, int n, int state) |
845dea95 | 7377 | { |
8d45d196 DW |
7378 | int inst = a->info.container_member; |
7379 | struct intel_super *super = a->container->sb; | |
949c47a0 | 7380 | struct imsm_dev *dev = get_imsm_dev(super, inst); |
238c0a71 | 7381 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
8d45d196 | 7382 | struct imsm_disk *disk; |
7ce05701 LD |
7383 | struct mdinfo *mdi; |
7384 | int recovery_not_finished = 0; | |
0c046afd | 7385 | int failed; |
b10b37b8 | 7386 | __u32 ord; |
0c046afd | 7387 | __u8 map_state; |
8d45d196 DW |
7388 | |
7389 | if (n > map->num_members) | |
e12b3daa | 7390 | pr_err("imsm: set_disk %d out of range 0..%d\n", |
8d45d196 DW |
7391 | n, map->num_members - 1); |
7392 | ||
7393 | if (n < 0) | |
7394 | return; | |
7395 | ||
4e6e574a | 7396 | dprintf("imsm: set_disk %d:%x\n", n, state); |
8d45d196 | 7397 | |
238c0a71 | 7398 | ord = get_imsm_ord_tbl_ent(dev, n, MAP_0); |
b10b37b8 | 7399 | disk = get_imsm_disk(super, ord_to_idx(ord)); |
8d45d196 | 7400 | |
5802a811 | 7401 | /* check for new failures */ |
0556e1a2 DW |
7402 | if (state & DS_FAULTY) { |
7403 | if (mark_failure(dev, disk, ord_to_idx(ord))) | |
7404 | super->updates_pending++; | |
8d45d196 | 7405 | } |
47ee5a45 | 7406 | |
19859edc | 7407 | /* check if in_sync */ |
0556e1a2 | 7408 | if (state & DS_INSYNC && ord & IMSM_ORD_REBUILD && is_rebuilding(dev)) { |
238c0a71 | 7409 | struct imsm_map *migr_map = get_imsm_map(dev, MAP_1); |
b10b37b8 DW |
7410 | |
7411 | set_imsm_ord_tbl_ent(migr_map, n, ord_to_idx(ord)); | |
19859edc DW |
7412 | super->updates_pending++; |
7413 | } | |
8d45d196 | 7414 | |
3b451610 AK |
7415 | failed = imsm_count_failed(super, dev, MAP_0); |
7416 | map_state = imsm_check_degraded(super, dev, failed, MAP_0); | |
5802a811 | 7417 | |
0c046afd | 7418 | /* check if recovery complete, newly degraded, or failed */ |
94002678 AK |
7419 | dprintf("imsm: Detected transition to state "); |
7420 | switch (map_state) { | |
7421 | case IMSM_T_STATE_NORMAL: /* transition to normal state */ | |
7422 | dprintf("normal: "); | |
7423 | if (is_rebuilding(dev)) { | |
1ade5cc1 | 7424 | dprintf_cont("while rebuilding"); |
7ce05701 LD |
7425 | /* check if recovery is really finished */ |
7426 | for (mdi = a->info.devs; mdi ; mdi = mdi->next) | |
7427 | if (mdi->recovery_start != MaxSector) { | |
7428 | recovery_not_finished = 1; | |
7429 | break; | |
7430 | } | |
7431 | if (recovery_not_finished) { | |
1ade5cc1 N |
7432 | dprintf_cont("\n"); |
7433 | dprintf("Rebuild has not finished yet, state not changed"); | |
7ce05701 LD |
7434 | if (a->last_checkpoint < mdi->recovery_start) { |
7435 | a->last_checkpoint = mdi->recovery_start; | |
7436 | super->updates_pending++; | |
7437 | } | |
7438 | break; | |
7439 | } | |
94002678 | 7440 | end_migration(dev, super, map_state); |
238c0a71 | 7441 | map = get_imsm_map(dev, MAP_0); |
94002678 AK |
7442 | map->failed_disk_num = ~0; |
7443 | super->updates_pending++; | |
7444 | a->last_checkpoint = 0; | |
7445 | break; | |
7446 | } | |
7447 | if (is_gen_migration(dev)) { | |
1ade5cc1 | 7448 | dprintf_cont("while general migration"); |
bf2f0071 | 7449 | if (a->last_checkpoint >= a->info.component_size) |
809da78e | 7450 | end_migration(dev, super, map_state); |
94002678 AK |
7451 | else |
7452 | map->map_state = map_state; | |
238c0a71 | 7453 | map = get_imsm_map(dev, MAP_0); |
28bce06f | 7454 | map->failed_disk_num = ~0; |
94002678 | 7455 | super->updates_pending++; |
bf2f0071 | 7456 | break; |
94002678 AK |
7457 | } |
7458 | break; | |
7459 | case IMSM_T_STATE_DEGRADED: /* transition to degraded state */ | |
1ade5cc1 | 7460 | dprintf_cont("degraded: "); |
94002678 AK |
7461 | if ((map->map_state != map_state) && |
7462 | !dev->vol.migr_state) { | |
1ade5cc1 | 7463 | dprintf_cont("mark degraded"); |
94002678 AK |
7464 | map->map_state = map_state; |
7465 | super->updates_pending++; | |
7466 | a->last_checkpoint = 0; | |
7467 | break; | |
7468 | } | |
7469 | if (is_rebuilding(dev)) { | |
1ade5cc1 | 7470 | dprintf_cont("while rebuilding."); |
94002678 | 7471 | if (map->map_state != map_state) { |
1ade5cc1 | 7472 | dprintf_cont(" Map state change"); |
94002678 AK |
7473 | end_migration(dev, super, map_state); |
7474 | super->updates_pending++; | |
7475 | } | |
7476 | break; | |
7477 | } | |
7478 | if (is_gen_migration(dev)) { | |
1ade5cc1 | 7479 | dprintf_cont("while general migration"); |
bf2f0071 | 7480 | if (a->last_checkpoint >= a->info.component_size) |
809da78e | 7481 | end_migration(dev, super, map_state); |
94002678 AK |
7482 | else { |
7483 | map->map_state = map_state; | |
3b451610 | 7484 | manage_second_map(super, dev); |
94002678 AK |
7485 | } |
7486 | super->updates_pending++; | |
bf2f0071 | 7487 | break; |
28bce06f | 7488 | } |
6ce1fbf1 | 7489 | if (is_initializing(dev)) { |
1ade5cc1 | 7490 | dprintf_cont("while initialization."); |
6ce1fbf1 AK |
7491 | map->map_state = map_state; |
7492 | super->updates_pending++; | |
7493 | break; | |
7494 | } | |
94002678 AK |
7495 | break; |
7496 | case IMSM_T_STATE_FAILED: /* transition to failed state */ | |
1ade5cc1 | 7497 | dprintf_cont("failed: "); |
94002678 | 7498 | if (is_gen_migration(dev)) { |
1ade5cc1 | 7499 | dprintf_cont("while general migration"); |
94002678 AK |
7500 | map->map_state = map_state; |
7501 | super->updates_pending++; | |
7502 | break; | |
7503 | } | |
7504 | if (map->map_state != map_state) { | |
1ade5cc1 | 7505 | dprintf_cont("mark failed"); |
94002678 AK |
7506 | end_migration(dev, super, map_state); |
7507 | super->updates_pending++; | |
7508 | a->last_checkpoint = 0; | |
7509 | break; | |
7510 | } | |
7511 | break; | |
7512 | default: | |
1ade5cc1 | 7513 | dprintf_cont("state %i\n", map_state); |
5802a811 | 7514 | } |
1ade5cc1 | 7515 | dprintf_cont("\n"); |
845dea95 NB |
7516 | } |
7517 | ||
f796af5d | 7518 | static int store_imsm_mpb(int fd, struct imsm_super *mpb) |
c2a1e7da | 7519 | { |
f796af5d | 7520 | void *buf = mpb; |
c2a1e7da DW |
7521 | __u32 mpb_size = __le32_to_cpu(mpb->mpb_size); |
7522 | unsigned long long dsize; | |
7523 | unsigned long long sectors; | |
7524 | ||
7525 | get_dev_size(fd, NULL, &dsize); | |
7526 | ||
272f648f DW |
7527 | if (mpb_size > 512) { |
7528 | /* -1 to account for anchor */ | |
7529 | sectors = mpb_sectors(mpb) - 1; | |
c2a1e7da | 7530 | |
272f648f DW |
7531 | /* write the extended mpb to the sectors preceeding the anchor */ |
7532 | if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0) | |
7533 | return 1; | |
c2a1e7da | 7534 | |
f21e18ca N |
7535 | if ((unsigned long long)write(fd, buf + 512, 512 * sectors) |
7536 | != 512 * sectors) | |
272f648f DW |
7537 | return 1; |
7538 | } | |
c2a1e7da | 7539 | |
272f648f DW |
7540 | /* first block is stored on second to last sector of the disk */ |
7541 | if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0) | |
c2a1e7da DW |
7542 | return 1; |
7543 | ||
f796af5d | 7544 | if (write(fd, buf, 512) != 512) |
c2a1e7da DW |
7545 | return 1; |
7546 | ||
c2a1e7da DW |
7547 | return 0; |
7548 | } | |
7549 | ||
2e735d19 | 7550 | static void imsm_sync_metadata(struct supertype *container) |
845dea95 | 7551 | { |
2e735d19 | 7552 | struct intel_super *super = container->sb; |
c2a1e7da | 7553 | |
1a64be56 | 7554 | dprintf("sync metadata: %d\n", super->updates_pending); |
c2a1e7da DW |
7555 | if (!super->updates_pending) |
7556 | return; | |
7557 | ||
36988a3d | 7558 | write_super_imsm(container, 0); |
c2a1e7da DW |
7559 | |
7560 | super->updates_pending = 0; | |
845dea95 NB |
7561 | } |
7562 | ||
272906ef DW |
7563 | static struct dl *imsm_readd(struct intel_super *super, int idx, struct active_array *a) |
7564 | { | |
7565 | struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member); | |
238c0a71 | 7566 | int i = get_imsm_disk_idx(dev, idx, MAP_X); |
272906ef DW |
7567 | struct dl *dl; |
7568 | ||
7569 | for (dl = super->disks; dl; dl = dl->next) | |
7570 | if (dl->index == i) | |
7571 | break; | |
7572 | ||
25ed7e59 | 7573 | if (dl && is_failed(&dl->disk)) |
272906ef DW |
7574 | dl = NULL; |
7575 | ||
7576 | if (dl) | |
1ade5cc1 | 7577 | dprintf("found %x:%x\n", dl->major, dl->minor); |
272906ef DW |
7578 | |
7579 | return dl; | |
7580 | } | |
7581 | ||
a20d2ba5 | 7582 | static struct dl *imsm_add_spare(struct intel_super *super, int slot, |
8ba77d32 AK |
7583 | struct active_array *a, int activate_new, |
7584 | struct mdinfo *additional_test_list) | |
272906ef DW |
7585 | { |
7586 | struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member); | |
238c0a71 | 7587 | int idx = get_imsm_disk_idx(dev, slot, MAP_X); |
a20d2ba5 DW |
7588 | struct imsm_super *mpb = super->anchor; |
7589 | struct imsm_map *map; | |
272906ef DW |
7590 | unsigned long long pos; |
7591 | struct mdinfo *d; | |
7592 | struct extent *ex; | |
a20d2ba5 | 7593 | int i, j; |
272906ef | 7594 | int found; |
569cc43f DW |
7595 | __u32 array_start = 0; |
7596 | __u32 array_end = 0; | |
272906ef | 7597 | struct dl *dl; |
6c932028 | 7598 | struct mdinfo *test_list; |
272906ef DW |
7599 | |
7600 | for (dl = super->disks; dl; dl = dl->next) { | |
7601 | /* If in this array, skip */ | |
7602 | for (d = a->info.devs ; d ; d = d->next) | |
e553d2a4 DW |
7603 | if (d->state_fd >= 0 && |
7604 | d->disk.major == dl->major && | |
272906ef | 7605 | d->disk.minor == dl->minor) { |
8ba77d32 AK |
7606 | dprintf("%x:%x already in array\n", |
7607 | dl->major, dl->minor); | |
272906ef DW |
7608 | break; |
7609 | } | |
7610 | if (d) | |
7611 | continue; | |
6c932028 AK |
7612 | test_list = additional_test_list; |
7613 | while (test_list) { | |
7614 | if (test_list->disk.major == dl->major && | |
7615 | test_list->disk.minor == dl->minor) { | |
8ba77d32 AK |
7616 | dprintf("%x:%x already in additional test list\n", |
7617 | dl->major, dl->minor); | |
7618 | break; | |
7619 | } | |
6c932028 | 7620 | test_list = test_list->next; |
8ba77d32 | 7621 | } |
6c932028 | 7622 | if (test_list) |
8ba77d32 | 7623 | continue; |
272906ef | 7624 | |
e553d2a4 | 7625 | /* skip in use or failed drives */ |
25ed7e59 | 7626 | if (is_failed(&dl->disk) || idx == dl->index || |
df474657 DW |
7627 | dl->index == -2) { |
7628 | dprintf("%x:%x status (failed: %d index: %d)\n", | |
25ed7e59 | 7629 | dl->major, dl->minor, is_failed(&dl->disk), idx); |
9a1608e5 DW |
7630 | continue; |
7631 | } | |
7632 | ||
a20d2ba5 DW |
7633 | /* skip pure spares when we are looking for partially |
7634 | * assimilated drives | |
7635 | */ | |
7636 | if (dl->index == -1 && !activate_new) | |
7637 | continue; | |
7638 | ||
272906ef | 7639 | /* Does this unused device have the requisite free space? |
a20d2ba5 | 7640 | * It needs to be able to cover all member volumes |
272906ef DW |
7641 | */ |
7642 | ex = get_extents(super, dl); | |
7643 | if (!ex) { | |
7644 | dprintf("cannot get extents\n"); | |
7645 | continue; | |
7646 | } | |
a20d2ba5 DW |
7647 | for (i = 0; i < mpb->num_raid_devs; i++) { |
7648 | dev = get_imsm_dev(super, i); | |
238c0a71 | 7649 | map = get_imsm_map(dev, MAP_0); |
272906ef | 7650 | |
a20d2ba5 DW |
7651 | /* check if this disk is already a member of |
7652 | * this array | |
272906ef | 7653 | */ |
620b1713 | 7654 | if (get_imsm_disk_slot(map, dl->index) >= 0) |
a20d2ba5 DW |
7655 | continue; |
7656 | ||
7657 | found = 0; | |
7658 | j = 0; | |
7659 | pos = 0; | |
5551b113 | 7660 | array_start = pba_of_lba0(map); |
329c8278 | 7661 | array_end = array_start + |
5551b113 | 7662 | blocks_per_member(map) - 1; |
a20d2ba5 DW |
7663 | |
7664 | do { | |
7665 | /* check that we can start at pba_of_lba0 with | |
7666 | * blocks_per_member of space | |
7667 | */ | |
329c8278 | 7668 | if (array_start >= pos && array_end < ex[j].start) { |
a20d2ba5 DW |
7669 | found = 1; |
7670 | break; | |
7671 | } | |
7672 | pos = ex[j].start + ex[j].size; | |
7673 | j++; | |
7674 | } while (ex[j-1].size); | |
7675 | ||
7676 | if (!found) | |
272906ef | 7677 | break; |
a20d2ba5 | 7678 | } |
272906ef DW |
7679 | |
7680 | free(ex); | |
a20d2ba5 | 7681 | if (i < mpb->num_raid_devs) { |
329c8278 DW |
7682 | dprintf("%x:%x does not have %u to %u available\n", |
7683 | dl->major, dl->minor, array_start, array_end); | |
272906ef DW |
7684 | /* No room */ |
7685 | continue; | |
a20d2ba5 DW |
7686 | } |
7687 | return dl; | |
272906ef DW |
7688 | } |
7689 | ||
7690 | return dl; | |
7691 | } | |
7692 | ||
95d07a2c LM |
7693 | static int imsm_rebuild_allowed(struct supertype *cont, int dev_idx, int failed) |
7694 | { | |
7695 | struct imsm_dev *dev2; | |
7696 | struct imsm_map *map; | |
7697 | struct dl *idisk; | |
7698 | int slot; | |
7699 | int idx; | |
7700 | __u8 state; | |
7701 | ||
7702 | dev2 = get_imsm_dev(cont->sb, dev_idx); | |
7703 | if (dev2) { | |
238c0a71 | 7704 | state = imsm_check_degraded(cont->sb, dev2, failed, MAP_0); |
95d07a2c | 7705 | if (state == IMSM_T_STATE_FAILED) { |
238c0a71 | 7706 | map = get_imsm_map(dev2, MAP_0); |
95d07a2c LM |
7707 | if (!map) |
7708 | return 1; | |
7709 | for (slot = 0; slot < map->num_members; slot++) { | |
7710 | /* | |
7711 | * Check if failed disks are deleted from intel | |
7712 | * disk list or are marked to be deleted | |
7713 | */ | |
238c0a71 | 7714 | idx = get_imsm_disk_idx(dev2, slot, MAP_X); |
95d07a2c LM |
7715 | idisk = get_imsm_dl_disk(cont->sb, idx); |
7716 | /* | |
7717 | * Do not rebuild the array if failed disks | |
7718 | * from failed sub-array are not removed from | |
7719 | * container. | |
7720 | */ | |
7721 | if (idisk && | |
7722 | is_failed(&idisk->disk) && | |
7723 | (idisk->action != DISK_REMOVE)) | |
7724 | return 0; | |
7725 | } | |
7726 | } | |
7727 | } | |
7728 | return 1; | |
7729 | } | |
7730 | ||
88758e9d DW |
7731 | static struct mdinfo *imsm_activate_spare(struct active_array *a, |
7732 | struct metadata_update **updates) | |
7733 | { | |
7734 | /** | |
d23fe947 DW |
7735 | * Find a device with unused free space and use it to replace a |
7736 | * failed/vacant region in an array. We replace failed regions one a | |
7737 | * array at a time. The result is that a new spare disk will be added | |
7738 | * to the first failed array and after the monitor has finished | |
7739 | * propagating failures the remainder will be consumed. | |
88758e9d | 7740 | * |
d23fe947 DW |
7741 | * FIXME add a capability for mdmon to request spares from another |
7742 | * container. | |
88758e9d DW |
7743 | */ |
7744 | ||
7745 | struct intel_super *super = a->container->sb; | |
88758e9d | 7746 | int inst = a->info.container_member; |
949c47a0 | 7747 | struct imsm_dev *dev = get_imsm_dev(super, inst); |
238c0a71 | 7748 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
88758e9d DW |
7749 | int failed = a->info.array.raid_disks; |
7750 | struct mdinfo *rv = NULL; | |
7751 | struct mdinfo *d; | |
7752 | struct mdinfo *di; | |
7753 | struct metadata_update *mu; | |
7754 | struct dl *dl; | |
7755 | struct imsm_update_activate_spare *u; | |
7756 | int num_spares = 0; | |
7757 | int i; | |
95d07a2c | 7758 | int allowed; |
88758e9d DW |
7759 | |
7760 | for (d = a->info.devs ; d ; d = d->next) { | |
7761 | if ((d->curr_state & DS_FAULTY) && | |
7762 | d->state_fd >= 0) | |
7763 | /* wait for Removal to happen */ | |
7764 | return NULL; | |
7765 | if (d->state_fd >= 0) | |
7766 | failed--; | |
7767 | } | |
7768 | ||
7769 | dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n", | |
7770 | inst, failed, a->info.array.raid_disks, a->info.array.level); | |
1af97990 | 7771 | |
e2962bfc AK |
7772 | if (imsm_reshape_blocks_arrays_changes(super)) |
7773 | return NULL; | |
1af97990 | 7774 | |
fc8ca064 AK |
7775 | /* Cannot activate another spare if rebuild is in progress already |
7776 | */ | |
7777 | if (is_rebuilding(dev)) { | |
7a862a02 | 7778 | dprintf("imsm: No spare activation allowed. Rebuild in progress already.\n"); |
fc8ca064 AK |
7779 | return NULL; |
7780 | } | |
7781 | ||
89c67882 AK |
7782 | if (a->info.array.level == 4) |
7783 | /* No repair for takeovered array | |
7784 | * imsm doesn't support raid4 | |
7785 | */ | |
7786 | return NULL; | |
7787 | ||
3b451610 AK |
7788 | if (imsm_check_degraded(super, dev, failed, MAP_0) != |
7789 | IMSM_T_STATE_DEGRADED) | |
88758e9d DW |
7790 | return NULL; |
7791 | ||
95d07a2c LM |
7792 | /* |
7793 | * If there are any failed disks check state of the other volume. | |
7794 | * Block rebuild if the another one is failed until failed disks | |
7795 | * are removed from container. | |
7796 | */ | |
7797 | if (failed) { | |
7a862a02 | 7798 | dprintf("found failed disks in %.*s, check if there anotherfailed sub-array.\n", |
c4acd1e5 | 7799 | MAX_RAID_SERIAL_LEN, dev->volume); |
95d07a2c LM |
7800 | /* check if states of the other volumes allow for rebuild */ |
7801 | for (i = 0; i < super->anchor->num_raid_devs; i++) { | |
7802 | if (i != inst) { | |
7803 | allowed = imsm_rebuild_allowed(a->container, | |
7804 | i, failed); | |
7805 | if (!allowed) | |
7806 | return NULL; | |
7807 | } | |
7808 | } | |
7809 | } | |
7810 | ||
88758e9d | 7811 | /* For each slot, if it is not working, find a spare */ |
88758e9d DW |
7812 | for (i = 0; i < a->info.array.raid_disks; i++) { |
7813 | for (d = a->info.devs ; d ; d = d->next) | |
7814 | if (d->disk.raid_disk == i) | |
7815 | break; | |
7816 | dprintf("found %d: %p %x\n", i, d, d?d->curr_state:0); | |
7817 | if (d && (d->state_fd >= 0)) | |
7818 | continue; | |
7819 | ||
272906ef | 7820 | /* |
a20d2ba5 DW |
7821 | * OK, this device needs recovery. Try to re-add the |
7822 | * previous occupant of this slot, if this fails see if | |
7823 | * we can continue the assimilation of a spare that was | |
7824 | * partially assimilated, finally try to activate a new | |
7825 | * spare. | |
272906ef DW |
7826 | */ |
7827 | dl = imsm_readd(super, i, a); | |
7828 | if (!dl) | |
b303fe21 | 7829 | dl = imsm_add_spare(super, i, a, 0, rv); |
a20d2ba5 | 7830 | if (!dl) |
b303fe21 | 7831 | dl = imsm_add_spare(super, i, a, 1, rv); |
272906ef DW |
7832 | if (!dl) |
7833 | continue; | |
1011e834 | 7834 | |
272906ef | 7835 | /* found a usable disk with enough space */ |
503975b9 | 7836 | di = xcalloc(1, sizeof(*di)); |
272906ef DW |
7837 | |
7838 | /* dl->index will be -1 in the case we are activating a | |
7839 | * pristine spare. imsm_process_update() will create a | |
7840 | * new index in this case. Once a disk is found to be | |
7841 | * failed in all member arrays it is kicked from the | |
7842 | * metadata | |
7843 | */ | |
7844 | di->disk.number = dl->index; | |
d23fe947 | 7845 | |
272906ef DW |
7846 | /* (ab)use di->devs to store a pointer to the device |
7847 | * we chose | |
7848 | */ | |
7849 | di->devs = (struct mdinfo *) dl; | |
7850 | ||
7851 | di->disk.raid_disk = i; | |
7852 | di->disk.major = dl->major; | |
7853 | di->disk.minor = dl->minor; | |
7854 | di->disk.state = 0; | |
d23534e4 | 7855 | di->recovery_start = 0; |
5551b113 | 7856 | di->data_offset = pba_of_lba0(map); |
272906ef DW |
7857 | di->component_size = a->info.component_size; |
7858 | di->container_member = inst; | |
148acb7b | 7859 | super->random = random32(); |
272906ef DW |
7860 | di->next = rv; |
7861 | rv = di; | |
7862 | num_spares++; | |
7863 | dprintf("%x:%x to be %d at %llu\n", dl->major, dl->minor, | |
7864 | i, di->data_offset); | |
88758e9d DW |
7865 | } |
7866 | ||
7867 | if (!rv) | |
7868 | /* No spares found */ | |
7869 | return rv; | |
7870 | /* Now 'rv' has a list of devices to return. | |
7871 | * Create a metadata_update record to update the | |
7872 | * disk_ord_tbl for the array | |
7873 | */ | |
503975b9 | 7874 | mu = xmalloc(sizeof(*mu)); |
1011e834 | 7875 | mu->buf = xcalloc(num_spares, |
503975b9 | 7876 | sizeof(struct imsm_update_activate_spare)); |
88758e9d | 7877 | mu->space = NULL; |
cb23f1f4 | 7878 | mu->space_list = NULL; |
88758e9d DW |
7879 | mu->len = sizeof(struct imsm_update_activate_spare) * num_spares; |
7880 | mu->next = *updates; | |
7881 | u = (struct imsm_update_activate_spare *) mu->buf; | |
7882 | ||
7883 | for (di = rv ; di ; di = di->next) { | |
7884 | u->type = update_activate_spare; | |
d23fe947 DW |
7885 | u->dl = (struct dl *) di->devs; |
7886 | di->devs = NULL; | |
88758e9d DW |
7887 | u->slot = di->disk.raid_disk; |
7888 | u->array = inst; | |
7889 | u->next = u + 1; | |
7890 | u++; | |
7891 | } | |
7892 | (u-1)->next = NULL; | |
7893 | *updates = mu; | |
7894 | ||
7895 | return rv; | |
7896 | } | |
7897 | ||
54c2c1ea | 7898 | static int disks_overlap(struct intel_super *super, int idx, struct imsm_update_create_array *u) |
8273f55e | 7899 | { |
54c2c1ea | 7900 | struct imsm_dev *dev = get_imsm_dev(super, idx); |
238c0a71 AK |
7901 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
7902 | struct imsm_map *new_map = get_imsm_map(&u->dev, MAP_0); | |
54c2c1ea DW |
7903 | struct disk_info *inf = get_disk_info(u); |
7904 | struct imsm_disk *disk; | |
8273f55e DW |
7905 | int i; |
7906 | int j; | |
8273f55e | 7907 | |
54c2c1ea | 7908 | for (i = 0; i < map->num_members; i++) { |
238c0a71 | 7909 | disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i, MAP_X)); |
54c2c1ea DW |
7910 | for (j = 0; j < new_map->num_members; j++) |
7911 | if (serialcmp(disk->serial, inf[j].serial) == 0) | |
8273f55e DW |
7912 | return 1; |
7913 | } | |
7914 | ||
7915 | return 0; | |
7916 | } | |
7917 | ||
1a64be56 LM |
7918 | static struct dl *get_disk_super(struct intel_super *super, int major, int minor) |
7919 | { | |
594dc1b8 JS |
7920 | struct dl *dl; |
7921 | ||
1a64be56 LM |
7922 | for (dl = super->disks; dl; dl = dl->next) |
7923 | if ((dl->major == major) && (dl->minor == minor)) | |
7924 | return dl; | |
7925 | return NULL; | |
7926 | } | |
7927 | ||
7928 | static int remove_disk_super(struct intel_super *super, int major, int minor) | |
7929 | { | |
594dc1b8 | 7930 | struct dl *prev; |
1a64be56 LM |
7931 | struct dl *dl; |
7932 | ||
7933 | prev = NULL; | |
7934 | for (dl = super->disks; dl; dl = dl->next) { | |
7935 | if ((dl->major == major) && (dl->minor == minor)) { | |
7936 | /* remove */ | |
7937 | if (prev) | |
7938 | prev->next = dl->next; | |
7939 | else | |
7940 | super->disks = dl->next; | |
7941 | dl->next = NULL; | |
7942 | __free_imsm_disk(dl); | |
1ade5cc1 | 7943 | dprintf("removed %x:%x\n", major, minor); |
1a64be56 LM |
7944 | break; |
7945 | } | |
7946 | prev = dl; | |
7947 | } | |
7948 | return 0; | |
7949 | } | |
7950 | ||
f21e18ca | 7951 | static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index); |
ae6aad82 | 7952 | |
1a64be56 LM |
7953 | static int add_remove_disk_update(struct intel_super *super) |
7954 | { | |
7955 | int check_degraded = 0; | |
594dc1b8 JS |
7956 | struct dl *disk; |
7957 | ||
1a64be56 LM |
7958 | /* add/remove some spares to/from the metadata/contrainer */ |
7959 | while (super->disk_mgmt_list) { | |
7960 | struct dl *disk_cfg; | |
7961 | ||
7962 | disk_cfg = super->disk_mgmt_list; | |
7963 | super->disk_mgmt_list = disk_cfg->next; | |
7964 | disk_cfg->next = NULL; | |
7965 | ||
7966 | if (disk_cfg->action == DISK_ADD) { | |
7967 | disk_cfg->next = super->disks; | |
7968 | super->disks = disk_cfg; | |
7969 | check_degraded = 1; | |
1ade5cc1 N |
7970 | dprintf("added %x:%x\n", |
7971 | disk_cfg->major, disk_cfg->minor); | |
1a64be56 LM |
7972 | } else if (disk_cfg->action == DISK_REMOVE) { |
7973 | dprintf("Disk remove action processed: %x.%x\n", | |
7974 | disk_cfg->major, disk_cfg->minor); | |
7975 | disk = get_disk_super(super, | |
7976 | disk_cfg->major, | |
7977 | disk_cfg->minor); | |
7978 | if (disk) { | |
7979 | /* store action status */ | |
7980 | disk->action = DISK_REMOVE; | |
7981 | /* remove spare disks only */ | |
7982 | if (disk->index == -1) { | |
7983 | remove_disk_super(super, | |
7984 | disk_cfg->major, | |
7985 | disk_cfg->minor); | |
7986 | } | |
7987 | } | |
7988 | /* release allocate disk structure */ | |
7989 | __free_imsm_disk(disk_cfg); | |
7990 | } | |
7991 | } | |
7992 | return check_degraded; | |
7993 | } | |
7994 | ||
a29911da PC |
7995 | static int apply_reshape_migration_update(struct imsm_update_reshape_migration *u, |
7996 | struct intel_super *super, | |
7997 | void ***space_list) | |
7998 | { | |
7999 | struct intel_dev *id; | |
8000 | void **tofree = NULL; | |
8001 | int ret_val = 0; | |
8002 | ||
1ade5cc1 | 8003 | dprintf("(enter)\n"); |
a29911da PC |
8004 | if ((u->subdev < 0) || |
8005 | (u->subdev > 1)) { | |
8006 | dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev); | |
8007 | return ret_val; | |
8008 | } | |
8009 | if ((space_list == NULL) || (*space_list == NULL)) { | |
8010 | dprintf("imsm: Error: Memory is not allocated\n"); | |
8011 | return ret_val; | |
8012 | } | |
8013 | ||
8014 | for (id = super->devlist ; id; id = id->next) { | |
8015 | if (id->index == (unsigned)u->subdev) { | |
8016 | struct imsm_dev *dev = get_imsm_dev(super, u->subdev); | |
8017 | struct imsm_map *map; | |
8018 | struct imsm_dev *new_dev = | |
8019 | (struct imsm_dev *)*space_list; | |
238c0a71 | 8020 | struct imsm_map *migr_map = get_imsm_map(dev, MAP_1); |
a29911da PC |
8021 | int to_state; |
8022 | struct dl *new_disk; | |
8023 | ||
8024 | if (new_dev == NULL) | |
8025 | return ret_val; | |
8026 | *space_list = **space_list; | |
8027 | memcpy(new_dev, dev, sizeof_imsm_dev(dev, 0)); | |
238c0a71 | 8028 | map = get_imsm_map(new_dev, MAP_0); |
a29911da PC |
8029 | if (migr_map) { |
8030 | dprintf("imsm: Error: migration in progress"); | |
8031 | return ret_val; | |
8032 | } | |
8033 | ||
8034 | to_state = map->map_state; | |
8035 | if ((u->new_level == 5) && (map->raid_level == 0)) { | |
8036 | map->num_members++; | |
8037 | /* this should not happen */ | |
8038 | if (u->new_disks[0] < 0) { | |
8039 | map->failed_disk_num = | |
8040 | map->num_members - 1; | |
8041 | to_state = IMSM_T_STATE_DEGRADED; | |
8042 | } else | |
8043 | to_state = IMSM_T_STATE_NORMAL; | |
8044 | } | |
8e59f3d8 | 8045 | migrate(new_dev, super, to_state, MIGR_GEN_MIGR); |
a29911da PC |
8046 | if (u->new_level > -1) |
8047 | map->raid_level = u->new_level; | |
238c0a71 | 8048 | migr_map = get_imsm_map(new_dev, MAP_1); |
a29911da PC |
8049 | if ((u->new_level == 5) && |
8050 | (migr_map->raid_level == 0)) { | |
8051 | int ord = map->num_members - 1; | |
8052 | migr_map->num_members--; | |
8053 | if (u->new_disks[0] < 0) | |
8054 | ord |= IMSM_ORD_REBUILD; | |
8055 | set_imsm_ord_tbl_ent(map, | |
8056 | map->num_members - 1, | |
8057 | ord); | |
8058 | } | |
8059 | id->dev = new_dev; | |
8060 | tofree = (void **)dev; | |
8061 | ||
4bba0439 PC |
8062 | /* update chunk size |
8063 | */ | |
8064 | if (u->new_chunksize > 0) | |
8065 | map->blocks_per_strip = | |
8066 | __cpu_to_le16(u->new_chunksize * 2); | |
8067 | ||
a29911da PC |
8068 | /* add disk |
8069 | */ | |
8070 | if ((u->new_level != 5) || | |
8071 | (migr_map->raid_level != 0) || | |
8072 | (migr_map->raid_level == map->raid_level)) | |
8073 | goto skip_disk_add; | |
8074 | ||
8075 | if (u->new_disks[0] >= 0) { | |
8076 | /* use passes spare | |
8077 | */ | |
8078 | new_disk = get_disk_super(super, | |
8079 | major(u->new_disks[0]), | |
8080 | minor(u->new_disks[0])); | |
7a862a02 | 8081 | dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n", |
a29911da PC |
8082 | major(u->new_disks[0]), |
8083 | minor(u->new_disks[0]), | |
8084 | new_disk, new_disk->index); | |
8085 | if (new_disk == NULL) | |
8086 | goto error_disk_add; | |
8087 | ||
8088 | new_disk->index = map->num_members - 1; | |
8089 | /* slot to fill in autolayout | |
8090 | */ | |
8091 | new_disk->raiddisk = new_disk->index; | |
8092 | new_disk->disk.status |= CONFIGURED_DISK; | |
8093 | new_disk->disk.status &= ~SPARE_DISK; | |
8094 | } else | |
8095 | goto error_disk_add; | |
8096 | ||
8097 | skip_disk_add: | |
8098 | *tofree = *space_list; | |
8099 | /* calculate new size | |
8100 | */ | |
f3871fdc | 8101 | imsm_set_array_size(new_dev, -1); |
a29911da PC |
8102 | |
8103 | ret_val = 1; | |
8104 | } | |
8105 | } | |
8106 | ||
8107 | if (tofree) | |
8108 | *space_list = tofree; | |
8109 | return ret_val; | |
8110 | ||
8111 | error_disk_add: | |
8112 | dprintf("Error: imsm: Cannot find disk.\n"); | |
8113 | return ret_val; | |
8114 | } | |
8115 | ||
f3871fdc AK |
8116 | static int apply_size_change_update(struct imsm_update_size_change *u, |
8117 | struct intel_super *super) | |
8118 | { | |
8119 | struct intel_dev *id; | |
8120 | int ret_val = 0; | |
8121 | ||
1ade5cc1 | 8122 | dprintf("(enter)\n"); |
f3871fdc AK |
8123 | if ((u->subdev < 0) || |
8124 | (u->subdev > 1)) { | |
8125 | dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev); | |
8126 | return ret_val; | |
8127 | } | |
8128 | ||
8129 | for (id = super->devlist ; id; id = id->next) { | |
8130 | if (id->index == (unsigned)u->subdev) { | |
8131 | struct imsm_dev *dev = get_imsm_dev(super, u->subdev); | |
8132 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
8133 | int used_disks = imsm_num_data_members(dev, MAP_0); | |
8134 | unsigned long long blocks_per_member; | |
8135 | ||
8136 | /* calculate new size | |
8137 | */ | |
8138 | blocks_per_member = u->new_size / used_disks; | |
1ade5cc1 | 8139 | dprintf("(size: %llu, blocks per member: %llu)\n", |
f3871fdc AK |
8140 | u->new_size, blocks_per_member); |
8141 | set_blocks_per_member(map, blocks_per_member); | |
8142 | imsm_set_array_size(dev, u->new_size); | |
8143 | ||
8144 | ret_val = 1; | |
8145 | break; | |
8146 | } | |
8147 | } | |
8148 | ||
8149 | return ret_val; | |
8150 | } | |
8151 | ||
061d7da3 | 8152 | static int apply_update_activate_spare(struct imsm_update_activate_spare *u, |
ca9de185 | 8153 | struct intel_super *super, |
061d7da3 LO |
8154 | struct active_array *active_array) |
8155 | { | |
8156 | struct imsm_super *mpb = super->anchor; | |
8157 | struct imsm_dev *dev = get_imsm_dev(super, u->array); | |
238c0a71 | 8158 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
061d7da3 LO |
8159 | struct imsm_map *migr_map; |
8160 | struct active_array *a; | |
8161 | struct imsm_disk *disk; | |
8162 | __u8 to_state; | |
8163 | struct dl *dl; | |
8164 | unsigned int found; | |
8165 | int failed; | |
5961eeec | 8166 | int victim; |
061d7da3 | 8167 | int i; |
5961eeec | 8168 | int second_map_created = 0; |
061d7da3 | 8169 | |
5961eeec | 8170 | for (; u; u = u->next) { |
238c0a71 | 8171 | victim = get_imsm_disk_idx(dev, u->slot, MAP_X); |
061d7da3 | 8172 | |
5961eeec | 8173 | if (victim < 0) |
8174 | return 0; | |
061d7da3 | 8175 | |
5961eeec | 8176 | for (dl = super->disks; dl; dl = dl->next) |
8177 | if (dl == u->dl) | |
8178 | break; | |
061d7da3 | 8179 | |
5961eeec | 8180 | if (!dl) { |
7a862a02 | 8181 | pr_err("error: imsm_activate_spare passed an unknown disk (index: %d)\n", |
5961eeec | 8182 | u->dl->index); |
8183 | return 0; | |
8184 | } | |
061d7da3 | 8185 | |
5961eeec | 8186 | /* count failures (excluding rebuilds and the victim) |
8187 | * to determine map[0] state | |
8188 | */ | |
8189 | failed = 0; | |
8190 | for (i = 0; i < map->num_members; i++) { | |
8191 | if (i == u->slot) | |
8192 | continue; | |
8193 | disk = get_imsm_disk(super, | |
238c0a71 | 8194 | get_imsm_disk_idx(dev, i, MAP_X)); |
5961eeec | 8195 | if (!disk || is_failed(disk)) |
8196 | failed++; | |
8197 | } | |
061d7da3 | 8198 | |
5961eeec | 8199 | /* adding a pristine spare, assign a new index */ |
8200 | if (dl->index < 0) { | |
8201 | dl->index = super->anchor->num_disks; | |
8202 | super->anchor->num_disks++; | |
8203 | } | |
8204 | disk = &dl->disk; | |
8205 | disk->status |= CONFIGURED_DISK; | |
8206 | disk->status &= ~SPARE_DISK; | |
8207 | ||
8208 | /* mark rebuild */ | |
238c0a71 | 8209 | to_state = imsm_check_degraded(super, dev, failed, MAP_0); |
5961eeec | 8210 | if (!second_map_created) { |
8211 | second_map_created = 1; | |
8212 | map->map_state = IMSM_T_STATE_DEGRADED; | |
8213 | migrate(dev, super, to_state, MIGR_REBUILD); | |
8214 | } else | |
8215 | map->map_state = to_state; | |
238c0a71 | 8216 | migr_map = get_imsm_map(dev, MAP_1); |
5961eeec | 8217 | set_imsm_ord_tbl_ent(map, u->slot, dl->index); |
8218 | set_imsm_ord_tbl_ent(migr_map, u->slot, | |
8219 | dl->index | IMSM_ORD_REBUILD); | |
8220 | ||
8221 | /* update the family_num to mark a new container | |
8222 | * generation, being careful to record the existing | |
8223 | * family_num in orig_family_num to clean up after | |
8224 | * earlier mdadm versions that neglected to set it. | |
8225 | */ | |
8226 | if (mpb->orig_family_num == 0) | |
8227 | mpb->orig_family_num = mpb->family_num; | |
8228 | mpb->family_num += super->random; | |
8229 | ||
8230 | /* count arrays using the victim in the metadata */ | |
8231 | found = 0; | |
8232 | for (a = active_array; a ; a = a->next) { | |
8233 | dev = get_imsm_dev(super, a->info.container_member); | |
238c0a71 | 8234 | map = get_imsm_map(dev, MAP_0); |
061d7da3 | 8235 | |
5961eeec | 8236 | if (get_imsm_disk_slot(map, victim) >= 0) |
8237 | found++; | |
8238 | } | |
061d7da3 | 8239 | |
5961eeec | 8240 | /* delete the victim if it is no longer being |
8241 | * utilized anywhere | |
061d7da3 | 8242 | */ |
5961eeec | 8243 | if (!found) { |
8244 | struct dl **dlp; | |
061d7da3 | 8245 | |
5961eeec | 8246 | /* We know that 'manager' isn't touching anything, |
8247 | * so it is safe to delete | |
8248 | */ | |
8249 | for (dlp = &super->disks; *dlp; dlp = &(*dlp)->next) | |
061d7da3 LO |
8250 | if ((*dlp)->index == victim) |
8251 | break; | |
5961eeec | 8252 | |
8253 | /* victim may be on the missing list */ | |
8254 | if (!*dlp) | |
8255 | for (dlp = &super->missing; *dlp; | |
8256 | dlp = &(*dlp)->next) | |
8257 | if ((*dlp)->index == victim) | |
8258 | break; | |
8259 | imsm_delete(super, dlp, victim); | |
8260 | } | |
061d7da3 LO |
8261 | } |
8262 | ||
8263 | return 1; | |
8264 | } | |
a29911da | 8265 | |
2e5dc010 N |
8266 | static int apply_reshape_container_disks_update(struct imsm_update_reshape *u, |
8267 | struct intel_super *super, | |
8268 | void ***space_list) | |
8269 | { | |
8270 | struct dl *new_disk; | |
8271 | struct intel_dev *id; | |
8272 | int i; | |
8273 | int delta_disks = u->new_raid_disks - u->old_raid_disks; | |
ee4beede | 8274 | int disk_count = u->old_raid_disks; |
2e5dc010 N |
8275 | void **tofree = NULL; |
8276 | int devices_to_reshape = 1; | |
8277 | struct imsm_super *mpb = super->anchor; | |
8278 | int ret_val = 0; | |
d098291a | 8279 | unsigned int dev_id; |
2e5dc010 | 8280 | |
1ade5cc1 | 8281 | dprintf("(enter)\n"); |
2e5dc010 N |
8282 | |
8283 | /* enable spares to use in array */ | |
8284 | for (i = 0; i < delta_disks; i++) { | |
8285 | new_disk = get_disk_super(super, | |
8286 | major(u->new_disks[i]), | |
8287 | minor(u->new_disks[i])); | |
7a862a02 | 8288 | dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n", |
2e5dc010 N |
8289 | major(u->new_disks[i]), minor(u->new_disks[i]), |
8290 | new_disk, new_disk->index); | |
8291 | if ((new_disk == NULL) || | |
8292 | ((new_disk->index >= 0) && | |
8293 | (new_disk->index < u->old_raid_disks))) | |
8294 | goto update_reshape_exit; | |
ee4beede | 8295 | new_disk->index = disk_count++; |
2e5dc010 N |
8296 | /* slot to fill in autolayout |
8297 | */ | |
8298 | new_disk->raiddisk = new_disk->index; | |
8299 | new_disk->disk.status |= | |
8300 | CONFIGURED_DISK; | |
8301 | new_disk->disk.status &= ~SPARE_DISK; | |
8302 | } | |
8303 | ||
ed7333bd AK |
8304 | dprintf("imsm: volume set mpb->num_raid_devs = %i\n", |
8305 | mpb->num_raid_devs); | |
2e5dc010 N |
8306 | /* manage changes in volume |
8307 | */ | |
d098291a | 8308 | for (dev_id = 0; dev_id < mpb->num_raid_devs; dev_id++) { |
2e5dc010 N |
8309 | void **sp = *space_list; |
8310 | struct imsm_dev *newdev; | |
8311 | struct imsm_map *newmap, *oldmap; | |
8312 | ||
d098291a AK |
8313 | for (id = super->devlist ; id; id = id->next) { |
8314 | if (id->index == dev_id) | |
8315 | break; | |
8316 | } | |
8317 | if (id == NULL) | |
8318 | break; | |
2e5dc010 N |
8319 | if (!sp) |
8320 | continue; | |
8321 | *space_list = *sp; | |
8322 | newdev = (void*)sp; | |
8323 | /* Copy the dev, but not (all of) the map */ | |
8324 | memcpy(newdev, id->dev, sizeof(*newdev)); | |
238c0a71 AK |
8325 | oldmap = get_imsm_map(id->dev, MAP_0); |
8326 | newmap = get_imsm_map(newdev, MAP_0); | |
2e5dc010 N |
8327 | /* Copy the current map */ |
8328 | memcpy(newmap, oldmap, sizeof_imsm_map(oldmap)); | |
8329 | /* update one device only | |
8330 | */ | |
8331 | if (devices_to_reshape) { | |
ed7333bd AK |
8332 | dprintf("imsm: modifying subdev: %i\n", |
8333 | id->index); | |
2e5dc010 N |
8334 | devices_to_reshape--; |
8335 | newdev->vol.migr_state = 1; | |
8336 | newdev->vol.curr_migr_unit = 0; | |
ea672ee1 | 8337 | set_migr_type(newdev, MIGR_GEN_MIGR); |
2e5dc010 N |
8338 | newmap->num_members = u->new_raid_disks; |
8339 | for (i = 0; i < delta_disks; i++) { | |
8340 | set_imsm_ord_tbl_ent(newmap, | |
8341 | u->old_raid_disks + i, | |
8342 | u->old_raid_disks + i); | |
8343 | } | |
8344 | /* New map is correct, now need to save old map | |
8345 | */ | |
238c0a71 | 8346 | newmap = get_imsm_map(newdev, MAP_1); |
2e5dc010 N |
8347 | memcpy(newmap, oldmap, sizeof_imsm_map(oldmap)); |
8348 | ||
f3871fdc | 8349 | imsm_set_array_size(newdev, -1); |
2e5dc010 N |
8350 | } |
8351 | ||
8352 | sp = (void **)id->dev; | |
8353 | id->dev = newdev; | |
8354 | *sp = tofree; | |
8355 | tofree = sp; | |
8e59f3d8 AK |
8356 | |
8357 | /* Clear migration record */ | |
8358 | memset(super->migr_rec, 0, sizeof(struct migr_record)); | |
2e5dc010 | 8359 | } |
819bc634 AK |
8360 | if (tofree) |
8361 | *space_list = tofree; | |
2e5dc010 N |
8362 | ret_val = 1; |
8363 | ||
8364 | update_reshape_exit: | |
8365 | ||
8366 | return ret_val; | |
8367 | } | |
8368 | ||
bb025c2f | 8369 | static int apply_takeover_update(struct imsm_update_takeover *u, |
8ca6df95 KW |
8370 | struct intel_super *super, |
8371 | void ***space_list) | |
bb025c2f KW |
8372 | { |
8373 | struct imsm_dev *dev = NULL; | |
8ca6df95 KW |
8374 | struct intel_dev *dv; |
8375 | struct imsm_dev *dev_new; | |
bb025c2f KW |
8376 | struct imsm_map *map; |
8377 | struct dl *dm, *du; | |
8ca6df95 | 8378 | int i; |
bb025c2f KW |
8379 | |
8380 | for (dv = super->devlist; dv; dv = dv->next) | |
8381 | if (dv->index == (unsigned int)u->subarray) { | |
8382 | dev = dv->dev; | |
8383 | break; | |
8384 | } | |
8385 | ||
8386 | if (dev == NULL) | |
8387 | return 0; | |
8388 | ||
238c0a71 | 8389 | map = get_imsm_map(dev, MAP_0); |
bb025c2f KW |
8390 | |
8391 | if (u->direction == R10_TO_R0) { | |
43d5ec18 | 8392 | /* Number of failed disks must be half of initial disk number */ |
3b451610 AK |
8393 | if (imsm_count_failed(super, dev, MAP_0) != |
8394 | (map->num_members / 2)) | |
43d5ec18 KW |
8395 | return 0; |
8396 | ||
bb025c2f KW |
8397 | /* iterate through devices to mark removed disks as spare */ |
8398 | for (dm = super->disks; dm; dm = dm->next) { | |
8399 | if (dm->disk.status & FAILED_DISK) { | |
8400 | int idx = dm->index; | |
8401 | /* update indexes on the disk list */ | |
8402 | /* FIXME this loop-with-the-loop looks wrong, I'm not convinced | |
8403 | the index values will end up being correct.... NB */ | |
8404 | for (du = super->disks; du; du = du->next) | |
8405 | if (du->index > idx) | |
8406 | du->index--; | |
8407 | /* mark as spare disk */ | |
a8619d23 | 8408 | mark_spare(dm); |
bb025c2f KW |
8409 | } |
8410 | } | |
bb025c2f KW |
8411 | /* update map */ |
8412 | map->num_members = map->num_members / 2; | |
8413 | map->map_state = IMSM_T_STATE_NORMAL; | |
8414 | map->num_domains = 1; | |
8415 | map->raid_level = 0; | |
8416 | map->failed_disk_num = -1; | |
8417 | } | |
8418 | ||
8ca6df95 KW |
8419 | if (u->direction == R0_TO_R10) { |
8420 | void **space; | |
8421 | /* update slots in current disk list */ | |
8422 | for (dm = super->disks; dm; dm = dm->next) { | |
8423 | if (dm->index >= 0) | |
8424 | dm->index *= 2; | |
8425 | } | |
8426 | /* create new *missing* disks */ | |
8427 | for (i = 0; i < map->num_members; i++) { | |
8428 | space = *space_list; | |
8429 | if (!space) | |
8430 | continue; | |
8431 | *space_list = *space; | |
8432 | du = (void *)space; | |
8433 | memcpy(du, super->disks, sizeof(*du)); | |
8ca6df95 KW |
8434 | du->fd = -1; |
8435 | du->minor = 0; | |
8436 | du->major = 0; | |
8437 | du->index = (i * 2) + 1; | |
8438 | sprintf((char *)du->disk.serial, | |
8439 | " MISSING_%d", du->index); | |
8440 | sprintf((char *)du->serial, | |
8441 | "MISSING_%d", du->index); | |
8442 | du->next = super->missing; | |
8443 | super->missing = du; | |
8444 | } | |
8445 | /* create new dev and map */ | |
8446 | space = *space_list; | |
8447 | if (!space) | |
8448 | return 0; | |
8449 | *space_list = *space; | |
8450 | dev_new = (void *)space; | |
8451 | memcpy(dev_new, dev, sizeof(*dev)); | |
8452 | /* update new map */ | |
238c0a71 | 8453 | map = get_imsm_map(dev_new, MAP_0); |
8ca6df95 | 8454 | map->num_members = map->num_members * 2; |
1a2487c2 | 8455 | map->map_state = IMSM_T_STATE_DEGRADED; |
8ca6df95 KW |
8456 | map->num_domains = 2; |
8457 | map->raid_level = 1; | |
8458 | /* replace dev<->dev_new */ | |
8459 | dv->dev = dev_new; | |
8460 | } | |
bb025c2f KW |
8461 | /* update disk order table */ |
8462 | for (du = super->disks; du; du = du->next) | |
8463 | if (du->index >= 0) | |
8464 | set_imsm_ord_tbl_ent(map, du->index, du->index); | |
8ca6df95 | 8465 | for (du = super->missing; du; du = du->next) |
1a2487c2 KW |
8466 | if (du->index >= 0) { |
8467 | set_imsm_ord_tbl_ent(map, du->index, du->index); | |
e4c72d1d | 8468 | mark_missing(dv->dev, &du->disk, du->index); |
1a2487c2 | 8469 | } |
bb025c2f KW |
8470 | |
8471 | return 1; | |
8472 | } | |
8473 | ||
e8319a19 DW |
8474 | static void imsm_process_update(struct supertype *st, |
8475 | struct metadata_update *update) | |
8476 | { | |
8477 | /** | |
8478 | * crack open the metadata_update envelope to find the update record | |
8479 | * update can be one of: | |
d195167d AK |
8480 | * update_reshape_container_disks - all the arrays in the container |
8481 | * are being reshaped to have more devices. We need to mark | |
8482 | * the arrays for general migration and convert selected spares | |
8483 | * into active devices. | |
8484 | * update_activate_spare - a spare device has replaced a failed | |
1011e834 N |
8485 | * device in an array, update the disk_ord_tbl. If this disk is |
8486 | * present in all member arrays then also clear the SPARE_DISK | |
8487 | * flag | |
d195167d AK |
8488 | * update_create_array |
8489 | * update_kill_array | |
8490 | * update_rename_array | |
8491 | * update_add_remove_disk | |
e8319a19 DW |
8492 | */ |
8493 | struct intel_super *super = st->sb; | |
4d7b1503 | 8494 | struct imsm_super *mpb; |
e8319a19 DW |
8495 | enum imsm_update_type type = *(enum imsm_update_type *) update->buf; |
8496 | ||
4d7b1503 DW |
8497 | /* update requires a larger buf but the allocation failed */ |
8498 | if (super->next_len && !super->next_buf) { | |
8499 | super->next_len = 0; | |
8500 | return; | |
8501 | } | |
8502 | ||
8503 | if (super->next_buf) { | |
8504 | memcpy(super->next_buf, super->buf, super->len); | |
8505 | free(super->buf); | |
8506 | super->len = super->next_len; | |
8507 | super->buf = super->next_buf; | |
8508 | ||
8509 | super->next_len = 0; | |
8510 | super->next_buf = NULL; | |
8511 | } | |
8512 | ||
8513 | mpb = super->anchor; | |
8514 | ||
e8319a19 | 8515 | switch (type) { |
0ec5d470 AK |
8516 | case update_general_migration_checkpoint: { |
8517 | struct intel_dev *id; | |
8518 | struct imsm_update_general_migration_checkpoint *u = | |
8519 | (void *)update->buf; | |
8520 | ||
1ade5cc1 | 8521 | dprintf("called for update_general_migration_checkpoint\n"); |
0ec5d470 AK |
8522 | |
8523 | /* find device under general migration */ | |
8524 | for (id = super->devlist ; id; id = id->next) { | |
8525 | if (is_gen_migration(id->dev)) { | |
8526 | id->dev->vol.curr_migr_unit = | |
8527 | __cpu_to_le32(u->curr_migr_unit); | |
8528 | super->updates_pending++; | |
8529 | } | |
8530 | } | |
8531 | break; | |
8532 | } | |
bb025c2f KW |
8533 | case update_takeover: { |
8534 | struct imsm_update_takeover *u = (void *)update->buf; | |
1a2487c2 KW |
8535 | if (apply_takeover_update(u, super, &update->space_list)) { |
8536 | imsm_update_version_info(super); | |
bb025c2f | 8537 | super->updates_pending++; |
1a2487c2 | 8538 | } |
bb025c2f KW |
8539 | break; |
8540 | } | |
8541 | ||
78b10e66 | 8542 | case update_reshape_container_disks: { |
d195167d | 8543 | struct imsm_update_reshape *u = (void *)update->buf; |
2e5dc010 N |
8544 | if (apply_reshape_container_disks_update( |
8545 | u, super, &update->space_list)) | |
8546 | super->updates_pending++; | |
78b10e66 N |
8547 | break; |
8548 | } | |
48c5303a | 8549 | case update_reshape_migration: { |
a29911da PC |
8550 | struct imsm_update_reshape_migration *u = (void *)update->buf; |
8551 | if (apply_reshape_migration_update( | |
8552 | u, super, &update->space_list)) | |
8553 | super->updates_pending++; | |
48c5303a PC |
8554 | break; |
8555 | } | |
f3871fdc AK |
8556 | case update_size_change: { |
8557 | struct imsm_update_size_change *u = (void *)update->buf; | |
8558 | if (apply_size_change_update(u, super)) | |
8559 | super->updates_pending++; | |
8560 | break; | |
8561 | } | |
e8319a19 | 8562 | case update_activate_spare: { |
1011e834 | 8563 | struct imsm_update_activate_spare *u = (void *) update->buf; |
061d7da3 LO |
8564 | if (apply_update_activate_spare(u, super, st->arrays)) |
8565 | super->updates_pending++; | |
8273f55e DW |
8566 | break; |
8567 | } | |
8568 | case update_create_array: { | |
8569 | /* someone wants to create a new array, we need to be aware of | |
8570 | * a few races/collisions: | |
8571 | * 1/ 'Create' called by two separate instances of mdadm | |
8572 | * 2/ 'Create' versus 'activate_spare': mdadm has chosen | |
8573 | * devices that have since been assimilated via | |
8574 | * activate_spare. | |
8575 | * In the event this update can not be carried out mdadm will | |
8576 | * (FIX ME) notice that its update did not take hold. | |
8577 | */ | |
8578 | struct imsm_update_create_array *u = (void *) update->buf; | |
ba2de7ba | 8579 | struct intel_dev *dv; |
8273f55e DW |
8580 | struct imsm_dev *dev; |
8581 | struct imsm_map *map, *new_map; | |
8582 | unsigned long long start, end; | |
8583 | unsigned long long new_start, new_end; | |
8584 | int i; | |
54c2c1ea DW |
8585 | struct disk_info *inf; |
8586 | struct dl *dl; | |
8273f55e DW |
8587 | |
8588 | /* handle racing creates: first come first serve */ | |
8589 | if (u->dev_idx < mpb->num_raid_devs) { | |
1ade5cc1 | 8590 | dprintf("subarray %d already defined\n", u->dev_idx); |
ba2de7ba | 8591 | goto create_error; |
8273f55e DW |
8592 | } |
8593 | ||
8594 | /* check update is next in sequence */ | |
8595 | if (u->dev_idx != mpb->num_raid_devs) { | |
1ade5cc1 N |
8596 | dprintf("can not create array %d expected index %d\n", |
8597 | u->dev_idx, mpb->num_raid_devs); | |
ba2de7ba | 8598 | goto create_error; |
8273f55e DW |
8599 | } |
8600 | ||
238c0a71 | 8601 | new_map = get_imsm_map(&u->dev, MAP_0); |
5551b113 CA |
8602 | new_start = pba_of_lba0(new_map); |
8603 | new_end = new_start + blocks_per_member(new_map); | |
54c2c1ea | 8604 | inf = get_disk_info(u); |
8273f55e DW |
8605 | |
8606 | /* handle activate_spare versus create race: | |
8607 | * check to make sure that overlapping arrays do not include | |
8608 | * overalpping disks | |
8609 | */ | |
8610 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
949c47a0 | 8611 | dev = get_imsm_dev(super, i); |
238c0a71 | 8612 | map = get_imsm_map(dev, MAP_0); |
5551b113 CA |
8613 | start = pba_of_lba0(map); |
8614 | end = start + blocks_per_member(map); | |
8273f55e DW |
8615 | if ((new_start >= start && new_start <= end) || |
8616 | (start >= new_start && start <= new_end)) | |
54c2c1ea DW |
8617 | /* overlap */; |
8618 | else | |
8619 | continue; | |
8620 | ||
8621 | if (disks_overlap(super, i, u)) { | |
1ade5cc1 | 8622 | dprintf("arrays overlap\n"); |
ba2de7ba | 8623 | goto create_error; |
8273f55e DW |
8624 | } |
8625 | } | |
8273f55e | 8626 | |
949c47a0 DW |
8627 | /* check that prepare update was successful */ |
8628 | if (!update->space) { | |
1ade5cc1 | 8629 | dprintf("prepare update failed\n"); |
ba2de7ba | 8630 | goto create_error; |
949c47a0 DW |
8631 | } |
8632 | ||
54c2c1ea DW |
8633 | /* check that all disks are still active before committing |
8634 | * changes. FIXME: could we instead handle this by creating a | |
8635 | * degraded array? That's probably not what the user expects, | |
8636 | * so better to drop this update on the floor. | |
8637 | */ | |
8638 | for (i = 0; i < new_map->num_members; i++) { | |
8639 | dl = serial_to_dl(inf[i].serial, super); | |
8640 | if (!dl) { | |
1ade5cc1 | 8641 | dprintf("disk disappeared\n"); |
ba2de7ba | 8642 | goto create_error; |
54c2c1ea | 8643 | } |
949c47a0 DW |
8644 | } |
8645 | ||
8273f55e | 8646 | super->updates_pending++; |
54c2c1ea DW |
8647 | |
8648 | /* convert spares to members and fixup ord_tbl */ | |
8649 | for (i = 0; i < new_map->num_members; i++) { | |
8650 | dl = serial_to_dl(inf[i].serial, super); | |
8651 | if (dl->index == -1) { | |
8652 | dl->index = mpb->num_disks; | |
8653 | mpb->num_disks++; | |
8654 | dl->disk.status |= CONFIGURED_DISK; | |
8655 | dl->disk.status &= ~SPARE_DISK; | |
8656 | } | |
8657 | set_imsm_ord_tbl_ent(new_map, i, dl->index); | |
8658 | } | |
8659 | ||
ba2de7ba DW |
8660 | dv = update->space; |
8661 | dev = dv->dev; | |
949c47a0 DW |
8662 | update->space = NULL; |
8663 | imsm_copy_dev(dev, &u->dev); | |
ba2de7ba DW |
8664 | dv->index = u->dev_idx; |
8665 | dv->next = super->devlist; | |
8666 | super->devlist = dv; | |
8273f55e | 8667 | mpb->num_raid_devs++; |
8273f55e | 8668 | |
4d1313e9 | 8669 | imsm_update_version_info(super); |
8273f55e | 8670 | break; |
ba2de7ba DW |
8671 | create_error: |
8672 | /* mdmon knows how to release update->space, but not | |
8673 | * ((struct intel_dev *) update->space)->dev | |
8674 | */ | |
8675 | if (update->space) { | |
8676 | dv = update->space; | |
8677 | free(dv->dev); | |
8678 | } | |
8273f55e | 8679 | break; |
e8319a19 | 8680 | } |
33414a01 DW |
8681 | case update_kill_array: { |
8682 | struct imsm_update_kill_array *u = (void *) update->buf; | |
8683 | int victim = u->dev_idx; | |
8684 | struct active_array *a; | |
8685 | struct intel_dev **dp; | |
8686 | struct imsm_dev *dev; | |
8687 | ||
8688 | /* sanity check that we are not affecting the uuid of | |
8689 | * active arrays, or deleting an active array | |
8690 | * | |
8691 | * FIXME when immutable ids are available, but note that | |
8692 | * we'll also need to fixup the invalidated/active | |
8693 | * subarray indexes in mdstat | |
8694 | */ | |
8695 | for (a = st->arrays; a; a = a->next) | |
8696 | if (a->info.container_member >= victim) | |
8697 | break; | |
8698 | /* by definition if mdmon is running at least one array | |
8699 | * is active in the container, so checking | |
8700 | * mpb->num_raid_devs is just extra paranoia | |
8701 | */ | |
8702 | dev = get_imsm_dev(super, victim); | |
8703 | if (a || !dev || mpb->num_raid_devs == 1) { | |
8704 | dprintf("failed to delete subarray-%d\n", victim); | |
8705 | break; | |
8706 | } | |
8707 | ||
8708 | for (dp = &super->devlist; *dp;) | |
f21e18ca | 8709 | if ((*dp)->index == (unsigned)super->current_vol) { |
33414a01 DW |
8710 | *dp = (*dp)->next; |
8711 | } else { | |
f21e18ca | 8712 | if ((*dp)->index > (unsigned)victim) |
33414a01 DW |
8713 | (*dp)->index--; |
8714 | dp = &(*dp)->next; | |
8715 | } | |
8716 | mpb->num_raid_devs--; | |
8717 | super->updates_pending++; | |
8718 | break; | |
8719 | } | |
aa534678 DW |
8720 | case update_rename_array: { |
8721 | struct imsm_update_rename_array *u = (void *) update->buf; | |
8722 | char name[MAX_RAID_SERIAL_LEN+1]; | |
8723 | int target = u->dev_idx; | |
8724 | struct active_array *a; | |
8725 | struct imsm_dev *dev; | |
8726 | ||
8727 | /* sanity check that we are not affecting the uuid of | |
8728 | * an active array | |
8729 | */ | |
8730 | snprintf(name, MAX_RAID_SERIAL_LEN, "%s", (char *) u->name); | |
8731 | name[MAX_RAID_SERIAL_LEN] = '\0'; | |
8732 | for (a = st->arrays; a; a = a->next) | |
8733 | if (a->info.container_member == target) | |
8734 | break; | |
8735 | dev = get_imsm_dev(super, u->dev_idx); | |
8736 | if (a || !dev || !check_name(super, name, 1)) { | |
8737 | dprintf("failed to rename subarray-%d\n", target); | |
8738 | break; | |
8739 | } | |
8740 | ||
cdbe98cd | 8741 | snprintf((char *) dev->volume, MAX_RAID_SERIAL_LEN, "%s", name); |
aa534678 DW |
8742 | super->updates_pending++; |
8743 | break; | |
8744 | } | |
1a64be56 | 8745 | case update_add_remove_disk: { |
43dad3d6 | 8746 | /* we may be able to repair some arrays if disks are |
095b8088 | 8747 | * being added, check the status of add_remove_disk |
1a64be56 LM |
8748 | * if discs has been added. |
8749 | */ | |
8750 | if (add_remove_disk_update(super)) { | |
43dad3d6 | 8751 | struct active_array *a; |
072b727f DW |
8752 | |
8753 | super->updates_pending++; | |
1a64be56 | 8754 | for (a = st->arrays; a; a = a->next) |
43dad3d6 DW |
8755 | a->check_degraded = 1; |
8756 | } | |
43dad3d6 | 8757 | break; |
e8319a19 | 8758 | } |
1a64be56 | 8759 | default: |
7a862a02 | 8760 | pr_err("error: unsuported process update type:(type: %d)\n", type); |
1a64be56 | 8761 | } |
e8319a19 | 8762 | } |
88758e9d | 8763 | |
bc0b9d34 PC |
8764 | static struct mdinfo *get_spares_for_grow(struct supertype *st); |
8765 | ||
5fe6f031 N |
8766 | static int imsm_prepare_update(struct supertype *st, |
8767 | struct metadata_update *update) | |
8273f55e | 8768 | { |
949c47a0 | 8769 | /** |
4d7b1503 DW |
8770 | * Allocate space to hold new disk entries, raid-device entries or a new |
8771 | * mpb if necessary. The manager synchronously waits for updates to | |
8772 | * complete in the monitor, so new mpb buffers allocated here can be | |
8773 | * integrated by the monitor thread without worrying about live pointers | |
8774 | * in the manager thread. | |
8273f55e | 8775 | */ |
095b8088 | 8776 | enum imsm_update_type type; |
4d7b1503 DW |
8777 | struct intel_super *super = st->sb; |
8778 | struct imsm_super *mpb = super->anchor; | |
8779 | size_t buf_len; | |
8780 | size_t len = 0; | |
949c47a0 | 8781 | |
095b8088 N |
8782 | if (update->len < (int)sizeof(type)) |
8783 | return 0; | |
8784 | ||
8785 | type = *(enum imsm_update_type *) update->buf; | |
8786 | ||
949c47a0 | 8787 | switch (type) { |
0ec5d470 | 8788 | case update_general_migration_checkpoint: |
095b8088 N |
8789 | if (update->len < (int)sizeof(struct imsm_update_general_migration_checkpoint)) |
8790 | return 0; | |
1ade5cc1 | 8791 | dprintf("called for update_general_migration_checkpoint\n"); |
0ec5d470 | 8792 | break; |
abedf5fc KW |
8793 | case update_takeover: { |
8794 | struct imsm_update_takeover *u = (void *)update->buf; | |
095b8088 N |
8795 | if (update->len < (int)sizeof(*u)) |
8796 | return 0; | |
abedf5fc KW |
8797 | if (u->direction == R0_TO_R10) { |
8798 | void **tail = (void **)&update->space_list; | |
8799 | struct imsm_dev *dev = get_imsm_dev(super, u->subarray); | |
238c0a71 | 8800 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
abedf5fc KW |
8801 | int num_members = map->num_members; |
8802 | void *space; | |
8803 | int size, i; | |
abedf5fc KW |
8804 | /* allocate memory for added disks */ |
8805 | for (i = 0; i < num_members; i++) { | |
8806 | size = sizeof(struct dl); | |
503975b9 | 8807 | space = xmalloc(size); |
abedf5fc KW |
8808 | *tail = space; |
8809 | tail = space; | |
8810 | *tail = NULL; | |
8811 | } | |
8812 | /* allocate memory for new device */ | |
8813 | size = sizeof_imsm_dev(super->devlist->dev, 0) + | |
8814 | (num_members * sizeof(__u32)); | |
503975b9 N |
8815 | space = xmalloc(size); |
8816 | *tail = space; | |
8817 | tail = space; | |
8818 | *tail = NULL; | |
8819 | len = disks_to_mpb_size(num_members * 2); | |
abedf5fc KW |
8820 | } |
8821 | ||
8822 | break; | |
8823 | } | |
78b10e66 | 8824 | case update_reshape_container_disks: { |
d195167d AK |
8825 | /* Every raid device in the container is about to |
8826 | * gain some more devices, and we will enter a | |
8827 | * reconfiguration. | |
8828 | * So each 'imsm_map' will be bigger, and the imsm_vol | |
8829 | * will now hold 2 of them. | |
8830 | * Thus we need new 'struct imsm_dev' allocations sized | |
8831 | * as sizeof_imsm_dev but with more devices in both maps. | |
8832 | */ | |
8833 | struct imsm_update_reshape *u = (void *)update->buf; | |
8834 | struct intel_dev *dl; | |
8835 | void **space_tail = (void**)&update->space_list; | |
8836 | ||
095b8088 N |
8837 | if (update->len < (int)sizeof(*u)) |
8838 | return 0; | |
8839 | ||
1ade5cc1 | 8840 | dprintf("for update_reshape\n"); |
d195167d AK |
8841 | |
8842 | for (dl = super->devlist; dl; dl = dl->next) { | |
8843 | int size = sizeof_imsm_dev(dl->dev, 1); | |
8844 | void *s; | |
d677e0b8 AK |
8845 | if (u->new_raid_disks > u->old_raid_disks) |
8846 | size += sizeof(__u32)*2* | |
8847 | (u->new_raid_disks - u->old_raid_disks); | |
503975b9 | 8848 | s = xmalloc(size); |
d195167d AK |
8849 | *space_tail = s; |
8850 | space_tail = s; | |
8851 | *space_tail = NULL; | |
8852 | } | |
8853 | ||
8854 | len = disks_to_mpb_size(u->new_raid_disks); | |
8855 | dprintf("New anchor length is %llu\n", (unsigned long long)len); | |
78b10e66 N |
8856 | break; |
8857 | } | |
48c5303a | 8858 | case update_reshape_migration: { |
bc0b9d34 PC |
8859 | /* for migration level 0->5 we need to add disks |
8860 | * so the same as for container operation we will copy | |
8861 | * device to the bigger location. | |
8862 | * in memory prepared device and new disk area are prepared | |
8863 | * for usage in process update | |
8864 | */ | |
8865 | struct imsm_update_reshape_migration *u = (void *)update->buf; | |
8866 | struct intel_dev *id; | |
8867 | void **space_tail = (void **)&update->space_list; | |
8868 | int size; | |
8869 | void *s; | |
8870 | int current_level = -1; | |
8871 | ||
095b8088 N |
8872 | if (update->len < (int)sizeof(*u)) |
8873 | return 0; | |
8874 | ||
1ade5cc1 | 8875 | dprintf("for update_reshape\n"); |
bc0b9d34 PC |
8876 | |
8877 | /* add space for bigger array in update | |
8878 | */ | |
8879 | for (id = super->devlist; id; id = id->next) { | |
8880 | if (id->index == (unsigned)u->subdev) { | |
8881 | size = sizeof_imsm_dev(id->dev, 1); | |
8882 | if (u->new_raid_disks > u->old_raid_disks) | |
8883 | size += sizeof(__u32)*2* | |
8884 | (u->new_raid_disks - u->old_raid_disks); | |
503975b9 | 8885 | s = xmalloc(size); |
bc0b9d34 PC |
8886 | *space_tail = s; |
8887 | space_tail = s; | |
8888 | *space_tail = NULL; | |
8889 | break; | |
8890 | } | |
8891 | } | |
8892 | if (update->space_list == NULL) | |
8893 | break; | |
8894 | ||
8895 | /* add space for disk in update | |
8896 | */ | |
8897 | size = sizeof(struct dl); | |
503975b9 | 8898 | s = xmalloc(size); |
bc0b9d34 PC |
8899 | *space_tail = s; |
8900 | space_tail = s; | |
8901 | *space_tail = NULL; | |
8902 | ||
8903 | /* add spare device to update | |
8904 | */ | |
8905 | for (id = super->devlist ; id; id = id->next) | |
8906 | if (id->index == (unsigned)u->subdev) { | |
8907 | struct imsm_dev *dev; | |
8908 | struct imsm_map *map; | |
8909 | ||
8910 | dev = get_imsm_dev(super, u->subdev); | |
238c0a71 | 8911 | map = get_imsm_map(dev, MAP_0); |
bc0b9d34 PC |
8912 | current_level = map->raid_level; |
8913 | break; | |
8914 | } | |
8915 | if ((u->new_level == 5) && (u->new_level != current_level)) { | |
8916 | struct mdinfo *spares; | |
8917 | ||
8918 | spares = get_spares_for_grow(st); | |
8919 | if (spares) { | |
8920 | struct dl *dl; | |
8921 | struct mdinfo *dev; | |
8922 | ||
8923 | dev = spares->devs; | |
8924 | if (dev) { | |
8925 | u->new_disks[0] = | |
8926 | makedev(dev->disk.major, | |
8927 | dev->disk.minor); | |
8928 | dl = get_disk_super(super, | |
8929 | dev->disk.major, | |
8930 | dev->disk.minor); | |
8931 | dl->index = u->old_raid_disks; | |
8932 | dev = dev->next; | |
8933 | } | |
8934 | sysfs_free(spares); | |
8935 | } | |
8936 | } | |
8937 | len = disks_to_mpb_size(u->new_raid_disks); | |
8938 | dprintf("New anchor length is %llu\n", (unsigned long long)len); | |
48c5303a PC |
8939 | break; |
8940 | } | |
f3871fdc | 8941 | case update_size_change: { |
095b8088 N |
8942 | if (update->len < (int)sizeof(struct imsm_update_size_change)) |
8943 | return 0; | |
8944 | break; | |
8945 | } | |
8946 | case update_activate_spare: { | |
8947 | if (update->len < (int)sizeof(struct imsm_update_activate_spare)) | |
8948 | return 0; | |
f3871fdc AK |
8949 | break; |
8950 | } | |
949c47a0 DW |
8951 | case update_create_array: { |
8952 | struct imsm_update_create_array *u = (void *) update->buf; | |
ba2de7ba | 8953 | struct intel_dev *dv; |
54c2c1ea | 8954 | struct imsm_dev *dev = &u->dev; |
238c0a71 | 8955 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
54c2c1ea DW |
8956 | struct dl *dl; |
8957 | struct disk_info *inf; | |
8958 | int i; | |
8959 | int activate = 0; | |
949c47a0 | 8960 | |
095b8088 N |
8961 | if (update->len < (int)sizeof(*u)) |
8962 | return 0; | |
8963 | ||
54c2c1ea DW |
8964 | inf = get_disk_info(u); |
8965 | len = sizeof_imsm_dev(dev, 1); | |
ba2de7ba | 8966 | /* allocate a new super->devlist entry */ |
503975b9 N |
8967 | dv = xmalloc(sizeof(*dv)); |
8968 | dv->dev = xmalloc(len); | |
8969 | update->space = dv; | |
949c47a0 | 8970 | |
54c2c1ea DW |
8971 | /* count how many spares will be converted to members */ |
8972 | for (i = 0; i < map->num_members; i++) { | |
8973 | dl = serial_to_dl(inf[i].serial, super); | |
8974 | if (!dl) { | |
8975 | /* hmm maybe it failed?, nothing we can do about | |
8976 | * it here | |
8977 | */ | |
8978 | continue; | |
8979 | } | |
8980 | if (count_memberships(dl, super) == 0) | |
8981 | activate++; | |
8982 | } | |
8983 | len += activate * sizeof(struct imsm_disk); | |
949c47a0 | 8984 | break; |
095b8088 N |
8985 | } |
8986 | case update_kill_array: { | |
8987 | if (update->len < (int)sizeof(struct imsm_update_kill_array)) | |
8988 | return 0; | |
949c47a0 DW |
8989 | break; |
8990 | } | |
095b8088 N |
8991 | case update_rename_array: { |
8992 | if (update->len < (int)sizeof(struct imsm_update_rename_array)) | |
8993 | return 0; | |
8994 | break; | |
8995 | } | |
8996 | case update_add_remove_disk: | |
8997 | /* no update->len needed */ | |
8998 | break; | |
8999 | default: | |
9000 | return 0; | |
949c47a0 | 9001 | } |
8273f55e | 9002 | |
4d7b1503 DW |
9003 | /* check if we need a larger metadata buffer */ |
9004 | if (super->next_buf) | |
9005 | buf_len = super->next_len; | |
9006 | else | |
9007 | buf_len = super->len; | |
9008 | ||
9009 | if (__le32_to_cpu(mpb->mpb_size) + len > buf_len) { | |
9010 | /* ok we need a larger buf than what is currently allocated | |
9011 | * if this allocation fails process_update will notice that | |
9012 | * ->next_len is set and ->next_buf is NULL | |
9013 | */ | |
9014 | buf_len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + len, 512); | |
9015 | if (super->next_buf) | |
9016 | free(super->next_buf); | |
9017 | ||
9018 | super->next_len = buf_len; | |
1f45a8ad DW |
9019 | if (posix_memalign(&super->next_buf, 512, buf_len) == 0) |
9020 | memset(super->next_buf, 0, buf_len); | |
9021 | else | |
4d7b1503 DW |
9022 | super->next_buf = NULL; |
9023 | } | |
5fe6f031 | 9024 | return 1; |
8273f55e DW |
9025 | } |
9026 | ||
ae6aad82 | 9027 | /* must be called while manager is quiesced */ |
f21e18ca | 9028 | static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index) |
ae6aad82 DW |
9029 | { |
9030 | struct imsm_super *mpb = super->anchor; | |
ae6aad82 DW |
9031 | struct dl *iter; |
9032 | struct imsm_dev *dev; | |
9033 | struct imsm_map *map; | |
24565c9a DW |
9034 | int i, j, num_members; |
9035 | __u32 ord; | |
ae6aad82 | 9036 | |
1ade5cc1 | 9037 | dprintf("deleting device[%d] from imsm_super\n", index); |
ae6aad82 DW |
9038 | |
9039 | /* shift all indexes down one */ | |
9040 | for (iter = super->disks; iter; iter = iter->next) | |
f21e18ca | 9041 | if (iter->index > (int)index) |
ae6aad82 | 9042 | iter->index--; |
47ee5a45 | 9043 | for (iter = super->missing; iter; iter = iter->next) |
f21e18ca | 9044 | if (iter->index > (int)index) |
47ee5a45 | 9045 | iter->index--; |
ae6aad82 DW |
9046 | |
9047 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
9048 | dev = get_imsm_dev(super, i); | |
238c0a71 | 9049 | map = get_imsm_map(dev, MAP_0); |
24565c9a DW |
9050 | num_members = map->num_members; |
9051 | for (j = 0; j < num_members; j++) { | |
9052 | /* update ord entries being careful not to propagate | |
9053 | * ord-flags to the first map | |
9054 | */ | |
238c0a71 | 9055 | ord = get_imsm_ord_tbl_ent(dev, j, MAP_X); |
ae6aad82 | 9056 | |
24565c9a DW |
9057 | if (ord_to_idx(ord) <= index) |
9058 | continue; | |
ae6aad82 | 9059 | |
238c0a71 | 9060 | map = get_imsm_map(dev, MAP_0); |
24565c9a | 9061 | set_imsm_ord_tbl_ent(map, j, ord_to_idx(ord - 1)); |
238c0a71 | 9062 | map = get_imsm_map(dev, MAP_1); |
24565c9a DW |
9063 | if (map) |
9064 | set_imsm_ord_tbl_ent(map, j, ord - 1); | |
ae6aad82 DW |
9065 | } |
9066 | } | |
9067 | ||
9068 | mpb->num_disks--; | |
9069 | super->updates_pending++; | |
24565c9a DW |
9070 | if (*dlp) { |
9071 | struct dl *dl = *dlp; | |
9072 | ||
9073 | *dlp = (*dlp)->next; | |
9074 | __free_imsm_disk(dl); | |
9075 | } | |
ae6aad82 | 9076 | } |
9e2d750d | 9077 | #endif /* MDASSEMBLE */ |
9a717282 AK |
9078 | |
9079 | static void close_targets(int *targets, int new_disks) | |
9080 | { | |
9081 | int i; | |
9082 | ||
9083 | if (!targets) | |
9084 | return; | |
9085 | ||
9086 | for (i = 0; i < new_disks; i++) { | |
9087 | if (targets[i] >= 0) { | |
9088 | close(targets[i]); | |
9089 | targets[i] = -1; | |
9090 | } | |
9091 | } | |
9092 | } | |
9093 | ||
9094 | static int imsm_get_allowed_degradation(int level, int raid_disks, | |
9095 | struct intel_super *super, | |
9096 | struct imsm_dev *dev) | |
9097 | { | |
9098 | switch (level) { | |
bf5cf7c7 | 9099 | case 1: |
9a717282 AK |
9100 | case 10:{ |
9101 | int ret_val = 0; | |
9102 | struct imsm_map *map; | |
9103 | int i; | |
9104 | ||
9105 | ret_val = raid_disks/2; | |
9106 | /* check map if all disks pairs not failed | |
9107 | * in both maps | |
9108 | */ | |
238c0a71 | 9109 | map = get_imsm_map(dev, MAP_0); |
9a717282 AK |
9110 | for (i = 0; i < ret_val; i++) { |
9111 | int degradation = 0; | |
9112 | if (get_imsm_disk(super, i) == NULL) | |
9113 | degradation++; | |
9114 | if (get_imsm_disk(super, i + 1) == NULL) | |
9115 | degradation++; | |
9116 | if (degradation == 2) | |
9117 | return 0; | |
9118 | } | |
238c0a71 | 9119 | map = get_imsm_map(dev, MAP_1); |
9a717282 AK |
9120 | /* if there is no second map |
9121 | * result can be returned | |
9122 | */ | |
9123 | if (map == NULL) | |
9124 | return ret_val; | |
9125 | /* check degradation in second map | |
9126 | */ | |
9127 | for (i = 0; i < ret_val; i++) { | |
9128 | int degradation = 0; | |
9129 | if (get_imsm_disk(super, i) == NULL) | |
9130 | degradation++; | |
9131 | if (get_imsm_disk(super, i + 1) == NULL) | |
9132 | degradation++; | |
9133 | if (degradation == 2) | |
9134 | return 0; | |
9135 | } | |
9136 | return ret_val; | |
9137 | } | |
9138 | case 5: | |
9139 | return 1; | |
9140 | case 6: | |
9141 | return 2; | |
9142 | default: | |
9143 | return 0; | |
9144 | } | |
9145 | } | |
9146 | ||
687629c2 AK |
9147 | /******************************************************************************* |
9148 | * Function: open_backup_targets | |
9149 | * Description: Function opens file descriptors for all devices given in | |
9150 | * info->devs | |
9151 | * Parameters: | |
9152 | * info : general array info | |
9153 | * raid_disks : number of disks | |
9154 | * raid_fds : table of device's file descriptors | |
9a717282 AK |
9155 | * super : intel super for raid10 degradation check |
9156 | * dev : intel device for raid10 degradation check | |
687629c2 AK |
9157 | * Returns: |
9158 | * 0 : success | |
9159 | * -1 : fail | |
9160 | ******************************************************************************/ | |
9a717282 AK |
9161 | int open_backup_targets(struct mdinfo *info, int raid_disks, int *raid_fds, |
9162 | struct intel_super *super, struct imsm_dev *dev) | |
687629c2 AK |
9163 | { |
9164 | struct mdinfo *sd; | |
f627f5ad | 9165 | int i; |
9a717282 | 9166 | int opened = 0; |
f627f5ad AK |
9167 | |
9168 | for (i = 0; i < raid_disks; i++) | |
9169 | raid_fds[i] = -1; | |
687629c2 AK |
9170 | |
9171 | for (sd = info->devs ; sd ; sd = sd->next) { | |
9172 | char *dn; | |
9173 | ||
9174 | if (sd->disk.state & (1<<MD_DISK_FAULTY)) { | |
9175 | dprintf("disk is faulty!!\n"); | |
9176 | continue; | |
9177 | } | |
9178 | ||
9179 | if ((sd->disk.raid_disk >= raid_disks) || | |
9180 | (sd->disk.raid_disk < 0)) | |
9181 | continue; | |
9182 | ||
9183 | dn = map_dev(sd->disk.major, | |
9184 | sd->disk.minor, 1); | |
9185 | raid_fds[sd->disk.raid_disk] = dev_open(dn, O_RDWR); | |
9186 | if (raid_fds[sd->disk.raid_disk] < 0) { | |
e12b3daa | 9187 | pr_err("cannot open component\n"); |
9a717282 | 9188 | continue; |
687629c2 | 9189 | } |
9a717282 AK |
9190 | opened++; |
9191 | } | |
9192 | /* check if maximum array degradation level is not exceeded | |
9193 | */ | |
9194 | if ((raid_disks - opened) > | |
9195 | imsm_get_allowed_degradation(info->new_level, | |
9196 | raid_disks, | |
9197 | super, dev)) { | |
e12b3daa | 9198 | pr_err("Not enough disks can be opened.\n"); |
9a717282 AK |
9199 | close_targets(raid_fds, raid_disks); |
9200 | return -2; | |
687629c2 AK |
9201 | } |
9202 | return 0; | |
9203 | } | |
9204 | ||
d31ad643 PB |
9205 | /******************************************************************************* |
9206 | * Function: validate_container_imsm | |
9207 | * Description: This routine validates container after assemble, | |
9208 | * eg. if devices in container are under the same controller. | |
9209 | * | |
9210 | * Parameters: | |
9211 | * info : linked list with info about devices used in array | |
9212 | * Returns: | |
9213 | * 1 : HBA mismatch | |
9214 | * 0 : Success | |
9215 | ******************************************************************************/ | |
9216 | int validate_container_imsm(struct mdinfo *info) | |
9217 | { | |
6b781d33 AP |
9218 | if (check_env("IMSM_NO_PLATFORM")) |
9219 | return 0; | |
d31ad643 | 9220 | |
6b781d33 AP |
9221 | struct sys_dev *idev; |
9222 | struct sys_dev *hba = NULL; | |
9223 | struct sys_dev *intel_devices = find_intel_devices(); | |
9224 | char *dev_path = devt_to_devpath(makedev(info->disk.major, | |
9225 | info->disk.minor)); | |
9226 | ||
9227 | for (idev = intel_devices; idev; idev = idev->next) { | |
9228 | if (dev_path && strstr(dev_path, idev->path)) { | |
9229 | hba = idev; | |
9230 | break; | |
d31ad643 | 9231 | } |
6b781d33 AP |
9232 | } |
9233 | if (dev_path) | |
d31ad643 PB |
9234 | free(dev_path); |
9235 | ||
6b781d33 AP |
9236 | if (!hba) { |
9237 | pr_err("WARNING - Cannot detect HBA for device %s!\n", | |
9238 | devid2kname(makedev(info->disk.major, info->disk.minor))); | |
9239 | return 1; | |
9240 | } | |
9241 | ||
9242 | const struct imsm_orom *orom = get_orom_by_device_id(hba->dev_id); | |
9243 | struct mdinfo *dev; | |
9244 | ||
9245 | for (dev = info->next; dev; dev = dev->next) { | |
9246 | dev_path = devt_to_devpath(makedev(dev->disk.major, dev->disk.minor)); | |
9247 | ||
9248 | struct sys_dev *hba2 = NULL; | |
9249 | for (idev = intel_devices; idev; idev = idev->next) { | |
9250 | if (dev_path && strstr(dev_path, idev->path)) { | |
9251 | hba2 = idev; | |
9252 | break; | |
d31ad643 PB |
9253 | } |
9254 | } | |
6b781d33 AP |
9255 | if (dev_path) |
9256 | free(dev_path); | |
9257 | ||
9258 | const struct imsm_orom *orom2 = hba2 == NULL ? NULL : | |
9259 | get_orom_by_device_id(hba2->dev_id); | |
9260 | ||
9261 | if (hba2 && hba->type != hba2->type) { | |
9262 | pr_err("WARNING - HBAs of devices do not match %s != %s\n", | |
9263 | get_sys_dev_type(hba->type), get_sys_dev_type(hba2->type)); | |
9264 | return 1; | |
9265 | } | |
9266 | ||
60f0f54d | 9267 | if ((orom != orom2) || ((hba->type == SYS_DEV_VMD) && (hba != hba2))) { |
6b781d33 AP |
9268 | pr_err("WARNING - IMSM container assembled with disks under different HBAs!\n" |
9269 | " This operation is not supported and can lead to data loss.\n"); | |
9270 | return 1; | |
9271 | } | |
9272 | ||
9273 | if (!orom) { | |
9274 | pr_err("WARNING - IMSM container assembled with disks under HBAs without IMSM platform support!\n" | |
9275 | " This operation is not supported and can lead to data loss.\n"); | |
9276 | return 1; | |
9277 | } | |
d31ad643 | 9278 | } |
6b781d33 | 9279 | |
d31ad643 PB |
9280 | return 0; |
9281 | } | |
9e2d750d | 9282 | #ifndef MDASSEMBLE |
687629c2 AK |
9283 | /******************************************************************************* |
9284 | * Function: init_migr_record_imsm | |
9285 | * Description: Function inits imsm migration record | |
9286 | * Parameters: | |
9287 | * super : imsm internal array info | |
9288 | * dev : device under migration | |
9289 | * info : general array info to find the smallest device | |
9290 | * Returns: | |
9291 | * none | |
9292 | ******************************************************************************/ | |
9293 | void init_migr_record_imsm(struct supertype *st, struct imsm_dev *dev, | |
9294 | struct mdinfo *info) | |
9295 | { | |
9296 | struct intel_super *super = st->sb; | |
9297 | struct migr_record *migr_rec = super->migr_rec; | |
9298 | int new_data_disks; | |
9299 | unsigned long long dsize, dev_sectors; | |
9300 | long long unsigned min_dev_sectors = -1LLU; | |
9301 | struct mdinfo *sd; | |
9302 | char nm[30]; | |
9303 | int fd; | |
238c0a71 AK |
9304 | struct imsm_map *map_dest = get_imsm_map(dev, MAP_0); |
9305 | struct imsm_map *map_src = get_imsm_map(dev, MAP_1); | |
687629c2 | 9306 | unsigned long long num_migr_units; |
3ef4403c | 9307 | unsigned long long array_blocks; |
687629c2 AK |
9308 | |
9309 | memset(migr_rec, 0, sizeof(struct migr_record)); | |
9310 | migr_rec->family_num = __cpu_to_le32(super->anchor->family_num); | |
9311 | ||
9312 | /* only ascending reshape supported now */ | |
9313 | migr_rec->ascending_migr = __cpu_to_le32(1); | |
9314 | ||
9315 | migr_rec->dest_depth_per_unit = GEN_MIGR_AREA_SIZE / | |
9316 | max(map_dest->blocks_per_strip, map_src->blocks_per_strip); | |
e1742195 AK |
9317 | migr_rec->dest_depth_per_unit *= |
9318 | max(map_dest->blocks_per_strip, map_src->blocks_per_strip); | |
238c0a71 | 9319 | new_data_disks = imsm_num_data_members(dev, MAP_0); |
687629c2 AK |
9320 | migr_rec->blocks_per_unit = |
9321 | __cpu_to_le32(migr_rec->dest_depth_per_unit * new_data_disks); | |
9322 | migr_rec->dest_depth_per_unit = | |
9323 | __cpu_to_le32(migr_rec->dest_depth_per_unit); | |
3ef4403c | 9324 | array_blocks = info->component_size * new_data_disks; |
687629c2 AK |
9325 | num_migr_units = |
9326 | array_blocks / __le32_to_cpu(migr_rec->blocks_per_unit); | |
9327 | ||
9328 | if (array_blocks % __le32_to_cpu(migr_rec->blocks_per_unit)) | |
9329 | num_migr_units++; | |
9330 | migr_rec->num_migr_units = __cpu_to_le32(num_migr_units); | |
9331 | ||
9332 | migr_rec->post_migr_vol_cap = dev->size_low; | |
9333 | migr_rec->post_migr_vol_cap_hi = dev->size_high; | |
9334 | ||
687629c2 AK |
9335 | /* Find the smallest dev */ |
9336 | for (sd = info->devs ; sd ; sd = sd->next) { | |
9337 | sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor); | |
9338 | fd = dev_open(nm, O_RDONLY); | |
9339 | if (fd < 0) | |
9340 | continue; | |
9341 | get_dev_size(fd, NULL, &dsize); | |
9342 | dev_sectors = dsize / 512; | |
9343 | if (dev_sectors < min_dev_sectors) | |
9344 | min_dev_sectors = dev_sectors; | |
9345 | close(fd); | |
9346 | } | |
9347 | migr_rec->ckpt_area_pba = __cpu_to_le32(min_dev_sectors - | |
9348 | RAID_DISK_RESERVED_BLOCKS_IMSM_HI); | |
9349 | ||
9350 | write_imsm_migr_rec(st); | |
9351 | ||
9352 | return; | |
9353 | } | |
9354 | ||
9355 | /******************************************************************************* | |
9356 | * Function: save_backup_imsm | |
9357 | * Description: Function saves critical data stripes to Migration Copy Area | |
9358 | * and updates the current migration unit status. | |
9359 | * Use restore_stripes() to form a destination stripe, | |
9360 | * and to write it to the Copy Area. | |
9361 | * Parameters: | |
9362 | * st : supertype information | |
aea93171 | 9363 | * dev : imsm device that backup is saved for |
687629c2 AK |
9364 | * info : general array info |
9365 | * buf : input buffer | |
687629c2 AK |
9366 | * length : length of data to backup (blocks_per_unit) |
9367 | * Returns: | |
9368 | * 0 : success | |
9369 | *, -1 : fail | |
9370 | ******************************************************************************/ | |
9371 | int save_backup_imsm(struct supertype *st, | |
9372 | struct imsm_dev *dev, | |
9373 | struct mdinfo *info, | |
9374 | void *buf, | |
687629c2 AK |
9375 | int length) |
9376 | { | |
9377 | int rv = -1; | |
9378 | struct intel_super *super = st->sb; | |
594dc1b8 JS |
9379 | unsigned long long *target_offsets; |
9380 | int *targets; | |
687629c2 | 9381 | int i; |
238c0a71 | 9382 | struct imsm_map *map_dest = get_imsm_map(dev, MAP_0); |
687629c2 | 9383 | int new_disks = map_dest->num_members; |
ab724b98 AK |
9384 | int dest_layout = 0; |
9385 | int dest_chunk; | |
d1877f69 | 9386 | unsigned long long start; |
238c0a71 | 9387 | int data_disks = imsm_num_data_members(dev, MAP_0); |
687629c2 | 9388 | |
503975b9 | 9389 | targets = xmalloc(new_disks * sizeof(int)); |
687629c2 | 9390 | |
7e45b550 AK |
9391 | for (i = 0; i < new_disks; i++) |
9392 | targets[i] = -1; | |
9393 | ||
503975b9 | 9394 | target_offsets = xcalloc(new_disks, sizeof(unsigned long long)); |
687629c2 | 9395 | |
d1877f69 | 9396 | start = info->reshape_progress * 512; |
687629c2 | 9397 | for (i = 0; i < new_disks; i++) { |
687629c2 AK |
9398 | target_offsets[i] = (unsigned long long) |
9399 | __le32_to_cpu(super->migr_rec->ckpt_area_pba) * 512; | |
d1877f69 AK |
9400 | /* move back copy area adderss, it will be moved forward |
9401 | * in restore_stripes() using start input variable | |
9402 | */ | |
9403 | target_offsets[i] -= start/data_disks; | |
687629c2 AK |
9404 | } |
9405 | ||
9a717282 AK |
9406 | if (open_backup_targets(info, new_disks, targets, |
9407 | super, dev)) | |
687629c2 AK |
9408 | goto abort; |
9409 | ||
68eb8bc6 | 9410 | dest_layout = imsm_level_to_layout(map_dest->raid_level); |
ab724b98 AK |
9411 | dest_chunk = __le16_to_cpu(map_dest->blocks_per_strip) * 512; |
9412 | ||
687629c2 AK |
9413 | if (restore_stripes(targets, /* list of dest devices */ |
9414 | target_offsets, /* migration record offsets */ | |
9415 | new_disks, | |
ab724b98 AK |
9416 | dest_chunk, |
9417 | map_dest->raid_level, | |
9418 | dest_layout, | |
9419 | -1, /* source backup file descriptor */ | |
9420 | 0, /* input buf offset | |
9421 | * always 0 buf is already offseted */ | |
d1877f69 | 9422 | start, |
687629c2 AK |
9423 | length, |
9424 | buf) != 0) { | |
e7b84f9d | 9425 | pr_err("Error restoring stripes\n"); |
687629c2 AK |
9426 | goto abort; |
9427 | } | |
9428 | ||
9429 | rv = 0; | |
9430 | ||
9431 | abort: | |
9432 | if (targets) { | |
9a717282 | 9433 | close_targets(targets, new_disks); |
687629c2 AK |
9434 | free(targets); |
9435 | } | |
9436 | free(target_offsets); | |
9437 | ||
9438 | return rv; | |
9439 | } | |
9440 | ||
9441 | /******************************************************************************* | |
9442 | * Function: save_checkpoint_imsm | |
9443 | * Description: Function called for current unit status update | |
9444 | * in the migration record. It writes it to disk. | |
9445 | * Parameters: | |
9446 | * super : imsm internal array info | |
9447 | * info : general array info | |
9448 | * Returns: | |
9449 | * 0: success | |
9450 | * 1: failure | |
0228d92c AK |
9451 | * 2: failure, means no valid migration record |
9452 | * / no general migration in progress / | |
687629c2 AK |
9453 | ******************************************************************************/ |
9454 | int save_checkpoint_imsm(struct supertype *st, struct mdinfo *info, int state) | |
9455 | { | |
9456 | struct intel_super *super = st->sb; | |
f8b72ef5 AK |
9457 | unsigned long long blocks_per_unit; |
9458 | unsigned long long curr_migr_unit; | |
9459 | ||
2e062e82 | 9460 | if (load_imsm_migr_rec(super, info) != 0) { |
7a862a02 | 9461 | dprintf("imsm: ERROR: Cannot read migration record for checkpoint save.\n"); |
2e062e82 AK |
9462 | return 1; |
9463 | } | |
9464 | ||
f8b72ef5 AK |
9465 | blocks_per_unit = __le32_to_cpu(super->migr_rec->blocks_per_unit); |
9466 | if (blocks_per_unit == 0) { | |
0228d92c AK |
9467 | dprintf("imsm: no migration in progress.\n"); |
9468 | return 2; | |
687629c2 | 9469 | } |
f8b72ef5 AK |
9470 | curr_migr_unit = info->reshape_progress / blocks_per_unit; |
9471 | /* check if array is alligned to copy area | |
9472 | * if it is not alligned, add one to current migration unit value | |
9473 | * this can happend on array reshape finish only | |
9474 | */ | |
9475 | if (info->reshape_progress % blocks_per_unit) | |
9476 | curr_migr_unit++; | |
687629c2 AK |
9477 | |
9478 | super->migr_rec->curr_migr_unit = | |
f8b72ef5 | 9479 | __cpu_to_le32(curr_migr_unit); |
687629c2 AK |
9480 | super->migr_rec->rec_status = __cpu_to_le32(state); |
9481 | super->migr_rec->dest_1st_member_lba = | |
f8b72ef5 AK |
9482 | __cpu_to_le32(curr_migr_unit * |
9483 | __le32_to_cpu(super->migr_rec->dest_depth_per_unit)); | |
687629c2 | 9484 | if (write_imsm_migr_rec(st) < 0) { |
7a862a02 | 9485 | dprintf("imsm: Cannot write migration record outside backup area\n"); |
687629c2 AK |
9486 | return 1; |
9487 | } | |
9488 | ||
9489 | return 0; | |
9490 | } | |
9491 | ||
276d77db AK |
9492 | /******************************************************************************* |
9493 | * Function: recover_backup_imsm | |
9494 | * Description: Function recovers critical data from the Migration Copy Area | |
9495 | * while assembling an array. | |
9496 | * Parameters: | |
9497 | * super : imsm internal array info | |
9498 | * info : general array info | |
9499 | * Returns: | |
9500 | * 0 : success (or there is no data to recover) | |
9501 | * 1 : fail | |
9502 | ******************************************************************************/ | |
9503 | int recover_backup_imsm(struct supertype *st, struct mdinfo *info) | |
9504 | { | |
9505 | struct intel_super *super = st->sb; | |
9506 | struct migr_record *migr_rec = super->migr_rec; | |
594dc1b8 | 9507 | struct imsm_map *map_dest; |
276d77db AK |
9508 | struct intel_dev *id = NULL; |
9509 | unsigned long long read_offset; | |
9510 | unsigned long long write_offset; | |
9511 | unsigned unit_len; | |
9512 | int *targets = NULL; | |
9513 | int new_disks, i, err; | |
9514 | char *buf = NULL; | |
9515 | int retval = 1; | |
9516 | unsigned long curr_migr_unit = __le32_to_cpu(migr_rec->curr_migr_unit); | |
9517 | unsigned long num_migr_units = __le32_to_cpu(migr_rec->num_migr_units); | |
276d77db | 9518 | char buffer[20]; |
6c3560c0 | 9519 | int skipped_disks = 0; |
276d77db AK |
9520 | |
9521 | err = sysfs_get_str(info, NULL, "array_state", (char *)buffer, 20); | |
9522 | if (err < 1) | |
9523 | return 1; | |
9524 | ||
9525 | /* recover data only during assemblation */ | |
9526 | if (strncmp(buffer, "inactive", 8) != 0) | |
9527 | return 0; | |
9528 | /* no data to recover */ | |
9529 | if (__le32_to_cpu(migr_rec->rec_status) == UNIT_SRC_NORMAL) | |
9530 | return 0; | |
9531 | if (curr_migr_unit >= num_migr_units) | |
9532 | return 1; | |
9533 | ||
9534 | /* find device during reshape */ | |
9535 | for (id = super->devlist; id; id = id->next) | |
9536 | if (is_gen_migration(id->dev)) | |
9537 | break; | |
9538 | if (id == NULL) | |
9539 | return 1; | |
9540 | ||
238c0a71 | 9541 | map_dest = get_imsm_map(id->dev, MAP_0); |
276d77db AK |
9542 | new_disks = map_dest->num_members; |
9543 | ||
9544 | read_offset = (unsigned long long) | |
9545 | __le32_to_cpu(migr_rec->ckpt_area_pba) * 512; | |
9546 | ||
9547 | write_offset = ((unsigned long long) | |
9548 | __le32_to_cpu(migr_rec->dest_1st_member_lba) + | |
5551b113 | 9549 | pba_of_lba0(map_dest)) * 512; |
276d77db AK |
9550 | |
9551 | unit_len = __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512; | |
9552 | if (posix_memalign((void **)&buf, 512, unit_len) != 0) | |
9553 | goto abort; | |
503975b9 | 9554 | targets = xcalloc(new_disks, sizeof(int)); |
276d77db | 9555 | |
9a717282 | 9556 | if (open_backup_targets(info, new_disks, targets, super, id->dev)) { |
e7b84f9d | 9557 | pr_err("Cannot open some devices belonging to array.\n"); |
f627f5ad AK |
9558 | goto abort; |
9559 | } | |
276d77db AK |
9560 | |
9561 | for (i = 0; i < new_disks; i++) { | |
6c3560c0 AK |
9562 | if (targets[i] < 0) { |
9563 | skipped_disks++; | |
9564 | continue; | |
9565 | } | |
276d77db | 9566 | if (lseek64(targets[i], read_offset, SEEK_SET) < 0) { |
e7b84f9d N |
9567 | pr_err("Cannot seek to block: %s\n", |
9568 | strerror(errno)); | |
137debce AK |
9569 | skipped_disks++; |
9570 | continue; | |
276d77db | 9571 | } |
9ec11d1a | 9572 | if ((unsigned)read(targets[i], buf, unit_len) != unit_len) { |
e7b84f9d N |
9573 | pr_err("Cannot read copy area block: %s\n", |
9574 | strerror(errno)); | |
137debce AK |
9575 | skipped_disks++; |
9576 | continue; | |
276d77db AK |
9577 | } |
9578 | if (lseek64(targets[i], write_offset, SEEK_SET) < 0) { | |
e7b84f9d N |
9579 | pr_err("Cannot seek to block: %s\n", |
9580 | strerror(errno)); | |
137debce AK |
9581 | skipped_disks++; |
9582 | continue; | |
276d77db | 9583 | } |
9ec11d1a | 9584 | if ((unsigned)write(targets[i], buf, unit_len) != unit_len) { |
e7b84f9d N |
9585 | pr_err("Cannot restore block: %s\n", |
9586 | strerror(errno)); | |
137debce AK |
9587 | skipped_disks++; |
9588 | continue; | |
276d77db AK |
9589 | } |
9590 | } | |
9591 | ||
137debce AK |
9592 | if (skipped_disks > imsm_get_allowed_degradation(info->new_level, |
9593 | new_disks, | |
9594 | super, | |
9595 | id->dev)) { | |
7a862a02 | 9596 | pr_err("Cannot restore data from backup. Too many failed disks\n"); |
6c3560c0 AK |
9597 | goto abort; |
9598 | } | |
9599 | ||
befb629b AK |
9600 | if (save_checkpoint_imsm(st, info, UNIT_SRC_NORMAL)) { |
9601 | /* ignore error == 2, this can mean end of reshape here | |
9602 | */ | |
7a862a02 | 9603 | dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL) during restart\n"); |
befb629b | 9604 | } else |
276d77db | 9605 | retval = 0; |
276d77db AK |
9606 | |
9607 | abort: | |
9608 | if (targets) { | |
9609 | for (i = 0; i < new_disks; i++) | |
9610 | if (targets[i]) | |
9611 | close(targets[i]); | |
9612 | free(targets); | |
9613 | } | |
9614 | free(buf); | |
9615 | return retval; | |
9616 | } | |
9617 | ||
2cda7640 ML |
9618 | static char disk_by_path[] = "/dev/disk/by-path/"; |
9619 | ||
9620 | static const char *imsm_get_disk_controller_domain(const char *path) | |
9621 | { | |
2cda7640 | 9622 | char disk_path[PATH_MAX]; |
96234762 LM |
9623 | char *drv=NULL; |
9624 | struct stat st; | |
2cda7640 | 9625 | |
6d8d290a | 9626 | strcpy(disk_path, disk_by_path); |
96234762 LM |
9627 | strncat(disk_path, path, PATH_MAX - strlen(disk_path) - 1); |
9628 | if (stat(disk_path, &st) == 0) { | |
9629 | struct sys_dev* hba; | |
594dc1b8 | 9630 | char *path; |
96234762 LM |
9631 | |
9632 | path = devt_to_devpath(st.st_rdev); | |
9633 | if (path == NULL) | |
9634 | return "unknown"; | |
9635 | hba = find_disk_attached_hba(-1, path); | |
9636 | if (hba && hba->type == SYS_DEV_SAS) | |
9637 | drv = "isci"; | |
9638 | else if (hba && hba->type == SYS_DEV_SATA) | |
9639 | drv = "ahci"; | |
1011e834 | 9640 | else |
96234762 LM |
9641 | drv = "unknown"; |
9642 | dprintf("path: %s hba: %s attached: %s\n", | |
9643 | path, (hba) ? hba->path : "NULL", drv); | |
9644 | free(path); | |
2cda7640 | 9645 | } |
96234762 | 9646 | return drv; |
2cda7640 ML |
9647 | } |
9648 | ||
4dd2df09 | 9649 | static char *imsm_find_array_devnm_by_subdev(int subdev, char *container) |
78b10e66 | 9650 | { |
4dd2df09 | 9651 | static char devnm[32]; |
78b10e66 N |
9652 | char subdev_name[20]; |
9653 | struct mdstat_ent *mdstat; | |
9654 | ||
9655 | sprintf(subdev_name, "%d", subdev); | |
9656 | mdstat = mdstat_by_subdev(subdev_name, container); | |
9657 | if (!mdstat) | |
4dd2df09 | 9658 | return NULL; |
78b10e66 | 9659 | |
4dd2df09 | 9660 | strcpy(devnm, mdstat->devnm); |
78b10e66 | 9661 | free_mdstat(mdstat); |
4dd2df09 | 9662 | return devnm; |
78b10e66 N |
9663 | } |
9664 | ||
9665 | static int imsm_reshape_is_allowed_on_container(struct supertype *st, | |
9666 | struct geo_params *geo, | |
fbf3d202 AK |
9667 | int *old_raid_disks, |
9668 | int direction) | |
78b10e66 | 9669 | { |
694575e7 KW |
9670 | /* currently we only support increasing the number of devices |
9671 | * for a container. This increases the number of device for each | |
9672 | * member array. They must all be RAID0 or RAID5. | |
9673 | */ | |
78b10e66 N |
9674 | int ret_val = 0; |
9675 | struct mdinfo *info, *member; | |
9676 | int devices_that_can_grow = 0; | |
9677 | ||
7a862a02 | 9678 | dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): st->devnm = (%s)\n", st->devnm); |
78b10e66 | 9679 | |
d04f65f4 | 9680 | if (geo->size > 0 || |
78b10e66 N |
9681 | geo->level != UnSet || |
9682 | geo->layout != UnSet || | |
9683 | geo->chunksize != 0 || | |
9684 | geo->raid_disks == UnSet) { | |
7a862a02 | 9685 | dprintf("imsm: Container operation is allowed for raid disks number change only.\n"); |
78b10e66 N |
9686 | return ret_val; |
9687 | } | |
9688 | ||
fbf3d202 | 9689 | if (direction == ROLLBACK_METADATA_CHANGES) { |
7a862a02 | 9690 | dprintf("imsm: Metadata changes rollback is not supported for container operation.\n"); |
fbf3d202 AK |
9691 | return ret_val; |
9692 | } | |
9693 | ||
78b10e66 N |
9694 | info = container_content_imsm(st, NULL); |
9695 | for (member = info; member; member = member->next) { | |
4dd2df09 | 9696 | char *result; |
78b10e66 N |
9697 | |
9698 | dprintf("imsm: checking device_num: %i\n", | |
9699 | member->container_member); | |
9700 | ||
d7d205bd | 9701 | if (geo->raid_disks <= member->array.raid_disks) { |
78b10e66 N |
9702 | /* we work on container for Online Capacity Expansion |
9703 | * only so raid_disks has to grow | |
9704 | */ | |
7a862a02 | 9705 | dprintf("imsm: for container operation raid disks increase is required\n"); |
78b10e66 N |
9706 | break; |
9707 | } | |
9708 | ||
9709 | if ((info->array.level != 0) && | |
9710 | (info->array.level != 5)) { | |
9711 | /* we cannot use this container with other raid level | |
9712 | */ | |
7a862a02 | 9713 | dprintf("imsm: for container operation wrong raid level (%i) detected\n", |
78b10e66 N |
9714 | info->array.level); |
9715 | break; | |
9716 | } else { | |
9717 | /* check for platform support | |
9718 | * for this raid level configuration | |
9719 | */ | |
9720 | struct intel_super *super = st->sb; | |
9721 | if (!is_raid_level_supported(super->orom, | |
9722 | member->array.level, | |
9723 | geo->raid_disks)) { | |
7a862a02 | 9724 | dprintf("platform does not support raid%d with %d disk%s\n", |
78b10e66 N |
9725 | info->array.level, |
9726 | geo->raid_disks, | |
9727 | geo->raid_disks > 1 ? "s" : ""); | |
9728 | break; | |
9729 | } | |
2a4a08e7 AK |
9730 | /* check if component size is aligned to chunk size |
9731 | */ | |
9732 | if (info->component_size % | |
9733 | (info->array.chunk_size/512)) { | |
7a862a02 | 9734 | dprintf("Component size is not aligned to chunk size\n"); |
2a4a08e7 AK |
9735 | break; |
9736 | } | |
78b10e66 N |
9737 | } |
9738 | ||
9739 | if (*old_raid_disks && | |
9740 | info->array.raid_disks != *old_raid_disks) | |
9741 | break; | |
9742 | *old_raid_disks = info->array.raid_disks; | |
9743 | ||
9744 | /* All raid5 and raid0 volumes in container | |
9745 | * have to be ready for Online Capacity Expansion | |
9746 | * so they need to be assembled. We have already | |
9747 | * checked that no recovery etc is happening. | |
9748 | */ | |
4dd2df09 N |
9749 | result = imsm_find_array_devnm_by_subdev(member->container_member, |
9750 | st->container_devnm); | |
9751 | if (result == NULL) { | |
78b10e66 N |
9752 | dprintf("imsm: cannot find array\n"); |
9753 | break; | |
9754 | } | |
9755 | devices_that_can_grow++; | |
9756 | } | |
9757 | sysfs_free(info); | |
9758 | if (!member && devices_that_can_grow) | |
9759 | ret_val = 1; | |
9760 | ||
9761 | if (ret_val) | |
1ade5cc1 | 9762 | dprintf("Container operation allowed\n"); |
78b10e66 | 9763 | else |
1ade5cc1 | 9764 | dprintf("Error: %i\n", ret_val); |
78b10e66 N |
9765 | |
9766 | return ret_val; | |
9767 | } | |
9768 | ||
9769 | /* Function: get_spares_for_grow | |
9770 | * Description: Allocates memory and creates list of spare devices | |
1011e834 | 9771 | * avaliable in container. Checks if spare drive size is acceptable. |
78b10e66 N |
9772 | * Parameters: Pointer to the supertype structure |
9773 | * Returns: Pointer to the list of spare devices (mdinfo structure) on success, | |
1011e834 | 9774 | * NULL if fail |
78b10e66 N |
9775 | */ |
9776 | static struct mdinfo *get_spares_for_grow(struct supertype *st) | |
9777 | { | |
78b10e66 | 9778 | unsigned long long min_size = min_acceptable_spare_size_imsm(st); |
326727d9 | 9779 | return container_choose_spares(st, min_size, NULL, NULL, NULL, 0); |
78b10e66 N |
9780 | } |
9781 | ||
9782 | /****************************************************************************** | |
9783 | * function: imsm_create_metadata_update_for_reshape | |
9784 | * Function creates update for whole IMSM container. | |
9785 | * | |
9786 | ******************************************************************************/ | |
9787 | static int imsm_create_metadata_update_for_reshape( | |
9788 | struct supertype *st, | |
9789 | struct geo_params *geo, | |
9790 | int old_raid_disks, | |
9791 | struct imsm_update_reshape **updatep) | |
9792 | { | |
9793 | struct intel_super *super = st->sb; | |
9794 | struct imsm_super *mpb = super->anchor; | |
594dc1b8 JS |
9795 | int update_memory_size; |
9796 | struct imsm_update_reshape *u; | |
9797 | struct mdinfo *spares; | |
78b10e66 | 9798 | int i; |
594dc1b8 | 9799 | int delta_disks; |
bbd24d86 | 9800 | struct mdinfo *dev; |
78b10e66 | 9801 | |
1ade5cc1 | 9802 | dprintf("(enter) raid_disks = %i\n", geo->raid_disks); |
78b10e66 N |
9803 | |
9804 | delta_disks = geo->raid_disks - old_raid_disks; | |
9805 | ||
9806 | /* size of all update data without anchor */ | |
9807 | update_memory_size = sizeof(struct imsm_update_reshape); | |
9808 | ||
9809 | /* now add space for spare disks that we need to add. */ | |
9810 | update_memory_size += sizeof(u->new_disks[0]) * (delta_disks - 1); | |
9811 | ||
503975b9 | 9812 | u = xcalloc(1, update_memory_size); |
78b10e66 N |
9813 | u->type = update_reshape_container_disks; |
9814 | u->old_raid_disks = old_raid_disks; | |
9815 | u->new_raid_disks = geo->raid_disks; | |
9816 | ||
9817 | /* now get spare disks list | |
9818 | */ | |
9819 | spares = get_spares_for_grow(st); | |
9820 | ||
9821 | if (spares == NULL | |
9822 | || delta_disks > spares->array.spare_disks) { | |
7a862a02 | 9823 | pr_err("imsm: ERROR: Cannot get spare devices for %s.\n", geo->dev_name); |
e4c72d1d | 9824 | i = -1; |
78b10e66 N |
9825 | goto abort; |
9826 | } | |
9827 | ||
9828 | /* we have got spares | |
9829 | * update disk list in imsm_disk list table in anchor | |
9830 | */ | |
9831 | dprintf("imsm: %i spares are available.\n\n", | |
9832 | spares->array.spare_disks); | |
9833 | ||
bbd24d86 | 9834 | dev = spares->devs; |
78b10e66 | 9835 | for (i = 0; i < delta_disks; i++) { |
78b10e66 N |
9836 | struct dl *dl; |
9837 | ||
bbd24d86 AK |
9838 | if (dev == NULL) |
9839 | break; | |
78b10e66 N |
9840 | u->new_disks[i] = makedev(dev->disk.major, |
9841 | dev->disk.minor); | |
9842 | dl = get_disk_super(super, dev->disk.major, dev->disk.minor); | |
ee4beede AK |
9843 | dl->index = mpb->num_disks; |
9844 | mpb->num_disks++; | |
bbd24d86 | 9845 | dev = dev->next; |
78b10e66 | 9846 | } |
78b10e66 N |
9847 | |
9848 | abort: | |
9849 | /* free spares | |
9850 | */ | |
9851 | sysfs_free(spares); | |
9852 | ||
d677e0b8 | 9853 | dprintf("imsm: reshape update preparation :"); |
78b10e66 | 9854 | if (i == delta_disks) { |
1ade5cc1 | 9855 | dprintf_cont(" OK\n"); |
78b10e66 N |
9856 | *updatep = u; |
9857 | return update_memory_size; | |
9858 | } | |
9859 | free(u); | |
1ade5cc1 | 9860 | dprintf_cont(" Error\n"); |
78b10e66 N |
9861 | |
9862 | return 0; | |
9863 | } | |
9864 | ||
f3871fdc AK |
9865 | /****************************************************************************** |
9866 | * function: imsm_create_metadata_update_for_size_change() | |
9867 | * Creates update for IMSM array for array size change. | |
9868 | * | |
9869 | ******************************************************************************/ | |
9870 | static int imsm_create_metadata_update_for_size_change( | |
9871 | struct supertype *st, | |
9872 | struct geo_params *geo, | |
9873 | struct imsm_update_size_change **updatep) | |
9874 | { | |
9875 | struct intel_super *super = st->sb; | |
594dc1b8 JS |
9876 | int update_memory_size; |
9877 | struct imsm_update_size_change *u; | |
f3871fdc | 9878 | |
1ade5cc1 | 9879 | dprintf("(enter) New size = %llu\n", geo->size); |
f3871fdc AK |
9880 | |
9881 | /* size of all update data without anchor */ | |
9882 | update_memory_size = sizeof(struct imsm_update_size_change); | |
9883 | ||
503975b9 | 9884 | u = xcalloc(1, update_memory_size); |
f3871fdc AK |
9885 | u->type = update_size_change; |
9886 | u->subdev = super->current_vol; | |
9887 | u->new_size = geo->size; | |
9888 | ||
9889 | dprintf("imsm: reshape update preparation : OK\n"); | |
9890 | *updatep = u; | |
9891 | ||
9892 | return update_memory_size; | |
9893 | } | |
9894 | ||
48c5303a PC |
9895 | /****************************************************************************** |
9896 | * function: imsm_create_metadata_update_for_migration() | |
9897 | * Creates update for IMSM array. | |
9898 | * | |
9899 | ******************************************************************************/ | |
9900 | static int imsm_create_metadata_update_for_migration( | |
9901 | struct supertype *st, | |
9902 | struct geo_params *geo, | |
9903 | struct imsm_update_reshape_migration **updatep) | |
9904 | { | |
9905 | struct intel_super *super = st->sb; | |
594dc1b8 JS |
9906 | int update_memory_size; |
9907 | struct imsm_update_reshape_migration *u; | |
48c5303a PC |
9908 | struct imsm_dev *dev; |
9909 | int previous_level = -1; | |
9910 | ||
1ade5cc1 | 9911 | dprintf("(enter) New Level = %i\n", geo->level); |
48c5303a PC |
9912 | |
9913 | /* size of all update data without anchor */ | |
9914 | update_memory_size = sizeof(struct imsm_update_reshape_migration); | |
9915 | ||
503975b9 | 9916 | u = xcalloc(1, update_memory_size); |
48c5303a PC |
9917 | u->type = update_reshape_migration; |
9918 | u->subdev = super->current_vol; | |
9919 | u->new_level = geo->level; | |
9920 | u->new_layout = geo->layout; | |
9921 | u->new_raid_disks = u->old_raid_disks = geo->raid_disks; | |
9922 | u->new_disks[0] = -1; | |
4bba0439 | 9923 | u->new_chunksize = -1; |
48c5303a PC |
9924 | |
9925 | dev = get_imsm_dev(super, u->subdev); | |
9926 | if (dev) { | |
9927 | struct imsm_map *map; | |
9928 | ||
238c0a71 | 9929 | map = get_imsm_map(dev, MAP_0); |
4bba0439 PC |
9930 | if (map) { |
9931 | int current_chunk_size = | |
9932 | __le16_to_cpu(map->blocks_per_strip) / 2; | |
9933 | ||
9934 | if (geo->chunksize != current_chunk_size) { | |
9935 | u->new_chunksize = geo->chunksize / 1024; | |
7a862a02 | 9936 | dprintf("imsm: chunk size change from %i to %i\n", |
4bba0439 PC |
9937 | current_chunk_size, u->new_chunksize); |
9938 | } | |
48c5303a | 9939 | previous_level = map->raid_level; |
4bba0439 | 9940 | } |
48c5303a PC |
9941 | } |
9942 | if ((geo->level == 5) && (previous_level == 0)) { | |
9943 | struct mdinfo *spares = NULL; | |
9944 | ||
9945 | u->new_raid_disks++; | |
9946 | spares = get_spares_for_grow(st); | |
9947 | if ((spares == NULL) || (spares->array.spare_disks < 1)) { | |
9948 | free(u); | |
9949 | sysfs_free(spares); | |
9950 | update_memory_size = 0; | |
7a862a02 | 9951 | dprintf("error: cannot get spare device for requested migration"); |
48c5303a PC |
9952 | return 0; |
9953 | } | |
9954 | sysfs_free(spares); | |
9955 | } | |
9956 | dprintf("imsm: reshape update preparation : OK\n"); | |
9957 | *updatep = u; | |
9958 | ||
9959 | return update_memory_size; | |
9960 | } | |
9961 | ||
8dd70bce AK |
9962 | static void imsm_update_metadata_locally(struct supertype *st, |
9963 | void *buf, int len) | |
9964 | { | |
9965 | struct metadata_update mu; | |
9966 | ||
9967 | mu.buf = buf; | |
9968 | mu.len = len; | |
9969 | mu.space = NULL; | |
9970 | mu.space_list = NULL; | |
9971 | mu.next = NULL; | |
5fe6f031 N |
9972 | if (imsm_prepare_update(st, &mu)) |
9973 | imsm_process_update(st, &mu); | |
8dd70bce AK |
9974 | |
9975 | while (mu.space_list) { | |
9976 | void **space = mu.space_list; | |
9977 | mu.space_list = *space; | |
9978 | free(space); | |
9979 | } | |
9980 | } | |
78b10e66 | 9981 | |
471bceb6 | 9982 | /*************************************************************************** |
694575e7 | 9983 | * Function: imsm_analyze_change |
471bceb6 | 9984 | * Description: Function analyze change for single volume |
1011e834 | 9985 | * and validate if transition is supported |
fbf3d202 AK |
9986 | * Parameters: Geometry parameters, supertype structure, |
9987 | * metadata change direction (apply/rollback) | |
694575e7 | 9988 | * Returns: Operation type code on success, -1 if fail |
471bceb6 KW |
9989 | ****************************************************************************/ |
9990 | enum imsm_reshape_type imsm_analyze_change(struct supertype *st, | |
fbf3d202 AK |
9991 | struct geo_params *geo, |
9992 | int direction) | |
694575e7 | 9993 | { |
471bceb6 KW |
9994 | struct mdinfo info; |
9995 | int change = -1; | |
9996 | int check_devs = 0; | |
c21e737b | 9997 | int chunk; |
67a2db32 AK |
9998 | /* number of added/removed disks in operation result */ |
9999 | int devNumChange = 0; | |
10000 | /* imsm compatible layout value for array geometry verification */ | |
10001 | int imsm_layout = -1; | |
7abc9871 AK |
10002 | int data_disks; |
10003 | struct imsm_dev *dev; | |
10004 | struct intel_super *super; | |
d04f65f4 | 10005 | unsigned long long current_size; |
65d38cca | 10006 | unsigned long long free_size; |
d04f65f4 | 10007 | unsigned long long max_size; |
65d38cca | 10008 | int rv; |
471bceb6 KW |
10009 | |
10010 | getinfo_super_imsm_volume(st, &info, NULL); | |
471bceb6 KW |
10011 | if ((geo->level != info.array.level) && |
10012 | (geo->level >= 0) && | |
10013 | (geo->level != UnSet)) { | |
10014 | switch (info.array.level) { | |
10015 | case 0: | |
10016 | if (geo->level == 5) { | |
b5347799 | 10017 | change = CH_MIGRATION; |
e13ce846 | 10018 | if (geo->layout != ALGORITHM_LEFT_ASYMMETRIC) { |
7a862a02 | 10019 | pr_err("Error. Requested Layout not supported (left-asymmetric layout is supported only)!\n"); |
e13ce846 AK |
10020 | change = -1; |
10021 | goto analyse_change_exit; | |
10022 | } | |
67a2db32 | 10023 | imsm_layout = geo->layout; |
471bceb6 | 10024 | check_devs = 1; |
e91a3bad LM |
10025 | devNumChange = 1; /* parity disk added */ |
10026 | } else if (geo->level == 10) { | |
471bceb6 KW |
10027 | change = CH_TAKEOVER; |
10028 | check_devs = 1; | |
e91a3bad | 10029 | devNumChange = 2; /* two mirrors added */ |
67a2db32 | 10030 | imsm_layout = 0x102; /* imsm supported layout */ |
471bceb6 | 10031 | } |
dfe77a9e KW |
10032 | break; |
10033 | case 1: | |
471bceb6 KW |
10034 | case 10: |
10035 | if (geo->level == 0) { | |
10036 | change = CH_TAKEOVER; | |
10037 | check_devs = 1; | |
e91a3bad | 10038 | devNumChange = -(geo->raid_disks/2); |
67a2db32 | 10039 | imsm_layout = 0; /* imsm raid0 layout */ |
471bceb6 KW |
10040 | } |
10041 | break; | |
10042 | } | |
10043 | if (change == -1) { | |
7a862a02 | 10044 | pr_err("Error. Level Migration from %d to %d not supported!\n", |
e7b84f9d | 10045 | info.array.level, geo->level); |
471bceb6 KW |
10046 | goto analyse_change_exit; |
10047 | } | |
10048 | } else | |
10049 | geo->level = info.array.level; | |
10050 | ||
10051 | if ((geo->layout != info.array.layout) | |
10052 | && ((geo->layout != UnSet) && (geo->layout != -1))) { | |
b5347799 | 10053 | change = CH_MIGRATION; |
471bceb6 KW |
10054 | if ((info.array.layout == 0) |
10055 | && (info.array.level == 5) | |
10056 | && (geo->layout == 5)) { | |
10057 | /* reshape 5 -> 4 */ | |
10058 | } else if ((info.array.layout == 5) | |
10059 | && (info.array.level == 5) | |
10060 | && (geo->layout == 0)) { | |
10061 | /* reshape 4 -> 5 */ | |
10062 | geo->layout = 0; | |
10063 | geo->level = 5; | |
10064 | } else { | |
7a862a02 | 10065 | pr_err("Error. Layout Migration from %d to %d not supported!\n", |
e7b84f9d | 10066 | info.array.layout, geo->layout); |
471bceb6 KW |
10067 | change = -1; |
10068 | goto analyse_change_exit; | |
10069 | } | |
67a2db32 | 10070 | } else { |
471bceb6 | 10071 | geo->layout = info.array.layout; |
67a2db32 AK |
10072 | if (imsm_layout == -1) |
10073 | imsm_layout = info.array.layout; | |
10074 | } | |
471bceb6 KW |
10075 | |
10076 | if ((geo->chunksize > 0) && (geo->chunksize != UnSet) | |
10077 | && (geo->chunksize != info.array.chunk_size)) | |
b5347799 | 10078 | change = CH_MIGRATION; |
471bceb6 KW |
10079 | else |
10080 | geo->chunksize = info.array.chunk_size; | |
10081 | ||
c21e737b | 10082 | chunk = geo->chunksize / 1024; |
7abc9871 AK |
10083 | |
10084 | super = st->sb; | |
10085 | dev = get_imsm_dev(super, super->current_vol); | |
10086 | data_disks = imsm_num_data_members(dev , MAP_0); | |
c41e00b2 | 10087 | /* compute current size per disk member |
7abc9871 | 10088 | */ |
c41e00b2 AK |
10089 | current_size = info.custom_array_size / data_disks; |
10090 | ||
d5d2c614 | 10091 | if ((geo->size > 0) && (geo->size != MAX_SIZE)) { |
c41e00b2 AK |
10092 | /* align component size |
10093 | */ | |
10094 | geo->size = imsm_component_size_aligment_check( | |
10095 | get_imsm_raid_level(dev->vol.map), | |
10096 | chunk * 1024, | |
10097 | geo->size * 2); | |
65d0b4ce | 10098 | if (geo->size == 0) { |
7a862a02 | 10099 | pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is 0).\n", |
65d0b4ce LD |
10100 | current_size); |
10101 | goto analyse_change_exit; | |
10102 | } | |
c41e00b2 | 10103 | } |
7abc9871 | 10104 | |
d04f65f4 | 10105 | if ((current_size != geo->size) && (geo->size > 0)) { |
7abc9871 | 10106 | if (change != -1) { |
7a862a02 | 10107 | pr_err("Error. Size change should be the only one at a time.\n"); |
7abc9871 AK |
10108 | change = -1; |
10109 | goto analyse_change_exit; | |
10110 | } | |
10111 | if ((super->current_vol + 1) != super->anchor->num_raid_devs) { | |
7a862a02 | 10112 | pr_err("Error. The last volume in container can be expanded only (%i/%s).\n", |
4dd2df09 | 10113 | super->current_vol, st->devnm); |
7abc9871 AK |
10114 | goto analyse_change_exit; |
10115 | } | |
65d38cca LD |
10116 | /* check the maximum available size |
10117 | */ | |
10118 | rv = imsm_get_free_size(st, dev->vol.map->num_members, | |
10119 | 0, chunk, &free_size); | |
10120 | if (rv == 0) | |
10121 | /* Cannot find maximum available space | |
10122 | */ | |
10123 | max_size = 0; | |
10124 | else { | |
10125 | max_size = free_size + current_size; | |
10126 | /* align component size | |
10127 | */ | |
10128 | max_size = imsm_component_size_aligment_check( | |
10129 | get_imsm_raid_level(dev->vol.map), | |
10130 | chunk * 1024, | |
10131 | max_size); | |
10132 | } | |
d04f65f4 | 10133 | if (geo->size == MAX_SIZE) { |
b130333f AK |
10134 | /* requested size change to the maximum available size |
10135 | */ | |
65d38cca | 10136 | if (max_size == 0) { |
7a862a02 | 10137 | pr_err("Error. Cannot find maximum available space.\n"); |
b130333f AK |
10138 | change = -1; |
10139 | goto analyse_change_exit; | |
65d38cca LD |
10140 | } else |
10141 | geo->size = max_size; | |
c41e00b2 | 10142 | } |
b130333f | 10143 | |
fbf3d202 AK |
10144 | if ((direction == ROLLBACK_METADATA_CHANGES)) { |
10145 | /* accept size for rollback only | |
10146 | */ | |
10147 | } else { | |
10148 | /* round size due to metadata compatibility | |
10149 | */ | |
10150 | geo->size = (geo->size >> SECT_PER_MB_SHIFT) | |
10151 | << SECT_PER_MB_SHIFT; | |
10152 | dprintf("Prepare update for size change to %llu\n", | |
10153 | geo->size ); | |
10154 | if (current_size >= geo->size) { | |
7a862a02 | 10155 | pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is %llu).\n", |
e7b84f9d | 10156 | current_size, geo->size); |
fbf3d202 AK |
10157 | goto analyse_change_exit; |
10158 | } | |
65d38cca | 10159 | if (max_size && geo->size > max_size) { |
7a862a02 | 10160 | pr_err("Error. Requested size is larger than maximum available size (maximum available size is %llu, requested size /rounded/ is %llu).\n", |
e7b84f9d | 10161 | max_size, geo->size); |
65d38cca LD |
10162 | goto analyse_change_exit; |
10163 | } | |
7abc9871 AK |
10164 | } |
10165 | geo->size *= data_disks; | |
10166 | geo->raid_disks = dev->vol.map->num_members; | |
10167 | change = CH_ARRAY_SIZE; | |
10168 | } | |
471bceb6 KW |
10169 | if (!validate_geometry_imsm(st, |
10170 | geo->level, | |
67a2db32 | 10171 | imsm_layout, |
e91a3bad | 10172 | geo->raid_disks + devNumChange, |
c21e737b | 10173 | &chunk, |
af4348dd | 10174 | geo->size, INVALID_SECTORS, |
471bceb6 KW |
10175 | 0, 0, 1)) |
10176 | change = -1; | |
10177 | ||
10178 | if (check_devs) { | |
10179 | struct intel_super *super = st->sb; | |
10180 | struct imsm_super *mpb = super->anchor; | |
10181 | ||
10182 | if (mpb->num_raid_devs > 1) { | |
7a862a02 | 10183 | pr_err("Error. Cannot perform operation on %s- for this operation it MUST be single array in container\n", |
e7b84f9d | 10184 | geo->dev_name); |
471bceb6 KW |
10185 | change = -1; |
10186 | } | |
10187 | } | |
10188 | ||
10189 | analyse_change_exit: | |
fbf3d202 AK |
10190 | if ((direction == ROLLBACK_METADATA_CHANGES) && |
10191 | ((change == CH_MIGRATION) || (change == CH_TAKEOVER))) { | |
7a862a02 | 10192 | dprintf("imsm: Metadata changes rollback is not supported for migration and takeover operations.\n"); |
fbf3d202 AK |
10193 | change = -1; |
10194 | } | |
471bceb6 | 10195 | return change; |
694575e7 KW |
10196 | } |
10197 | ||
bb025c2f KW |
10198 | int imsm_takeover(struct supertype *st, struct geo_params *geo) |
10199 | { | |
10200 | struct intel_super *super = st->sb; | |
10201 | struct imsm_update_takeover *u; | |
10202 | ||
503975b9 | 10203 | u = xmalloc(sizeof(struct imsm_update_takeover)); |
bb025c2f KW |
10204 | |
10205 | u->type = update_takeover; | |
10206 | u->subarray = super->current_vol; | |
10207 | ||
10208 | /* 10->0 transition */ | |
10209 | if (geo->level == 0) | |
10210 | u->direction = R10_TO_R0; | |
10211 | ||
0529c688 KW |
10212 | /* 0->10 transition */ |
10213 | if (geo->level == 10) | |
10214 | u->direction = R0_TO_R10; | |
10215 | ||
bb025c2f KW |
10216 | /* update metadata locally */ |
10217 | imsm_update_metadata_locally(st, u, | |
10218 | sizeof(struct imsm_update_takeover)); | |
10219 | /* and possibly remotely */ | |
10220 | if (st->update_tail) | |
10221 | append_metadata_update(st, u, | |
10222 | sizeof(struct imsm_update_takeover)); | |
10223 | else | |
10224 | free(u); | |
10225 | ||
10226 | return 0; | |
10227 | } | |
10228 | ||
d04f65f4 N |
10229 | static int imsm_reshape_super(struct supertype *st, unsigned long long size, |
10230 | int level, | |
78b10e66 | 10231 | int layout, int chunksize, int raid_disks, |
41784c88 | 10232 | int delta_disks, char *backup, char *dev, |
016e00f5 | 10233 | int direction, int verbose) |
78b10e66 | 10234 | { |
78b10e66 N |
10235 | int ret_val = 1; |
10236 | struct geo_params geo; | |
10237 | ||
1ade5cc1 | 10238 | dprintf("(enter)\n"); |
78b10e66 | 10239 | |
71204a50 | 10240 | memset(&geo, 0, sizeof(struct geo_params)); |
78b10e66 N |
10241 | |
10242 | geo.dev_name = dev; | |
4dd2df09 | 10243 | strcpy(geo.devnm, st->devnm); |
78b10e66 N |
10244 | geo.size = size; |
10245 | geo.level = level; | |
10246 | geo.layout = layout; | |
10247 | geo.chunksize = chunksize; | |
10248 | geo.raid_disks = raid_disks; | |
41784c88 AK |
10249 | if (delta_disks != UnSet) |
10250 | geo.raid_disks += delta_disks; | |
78b10e66 | 10251 | |
1ade5cc1 N |
10252 | dprintf("for level : %i\n", geo.level); |
10253 | dprintf("for raid_disks : %i\n", geo.raid_disks); | |
78b10e66 N |
10254 | |
10255 | if (experimental() == 0) | |
10256 | return ret_val; | |
10257 | ||
4dd2df09 | 10258 | if (strcmp(st->container_devnm, st->devnm) == 0) { |
694575e7 KW |
10259 | /* On container level we can only increase number of devices. */ |
10260 | dprintf("imsm: info: Container operation\n"); | |
78b10e66 | 10261 | int old_raid_disks = 0; |
6dc0be30 | 10262 | |
78b10e66 | 10263 | if (imsm_reshape_is_allowed_on_container( |
fbf3d202 | 10264 | st, &geo, &old_raid_disks, direction)) { |
78b10e66 N |
10265 | struct imsm_update_reshape *u = NULL; |
10266 | int len; | |
10267 | ||
10268 | len = imsm_create_metadata_update_for_reshape( | |
10269 | st, &geo, old_raid_disks, &u); | |
10270 | ||
ed08d51c AK |
10271 | if (len <= 0) { |
10272 | dprintf("imsm: Cannot prepare update\n"); | |
10273 | goto exit_imsm_reshape_super; | |
10274 | } | |
10275 | ||
8dd70bce AK |
10276 | ret_val = 0; |
10277 | /* update metadata locally */ | |
10278 | imsm_update_metadata_locally(st, u, len); | |
10279 | /* and possibly remotely */ | |
10280 | if (st->update_tail) | |
10281 | append_metadata_update(st, u, len); | |
10282 | else | |
ed08d51c | 10283 | free(u); |
8dd70bce | 10284 | |
694575e7 | 10285 | } else { |
7a862a02 | 10286 | pr_err("(imsm) Operation is not allowed on this container\n"); |
694575e7 KW |
10287 | } |
10288 | } else { | |
10289 | /* On volume level we support following operations | |
471bceb6 KW |
10290 | * - takeover: raid10 -> raid0; raid0 -> raid10 |
10291 | * - chunk size migration | |
10292 | * - migration: raid5 -> raid0; raid0 -> raid5 | |
10293 | */ | |
10294 | struct intel_super *super = st->sb; | |
10295 | struct intel_dev *dev = super->devlist; | |
4dd2df09 | 10296 | int change; |
694575e7 | 10297 | dprintf("imsm: info: Volume operation\n"); |
471bceb6 KW |
10298 | /* find requested device */ |
10299 | while (dev) { | |
1011e834 | 10300 | char *devnm = |
4dd2df09 N |
10301 | imsm_find_array_devnm_by_subdev( |
10302 | dev->index, st->container_devnm); | |
10303 | if (devnm && strcmp(devnm, geo.devnm) == 0) | |
471bceb6 KW |
10304 | break; |
10305 | dev = dev->next; | |
10306 | } | |
10307 | if (dev == NULL) { | |
4dd2df09 N |
10308 | pr_err("Cannot find %s (%s) subarray\n", |
10309 | geo.dev_name, geo.devnm); | |
471bceb6 KW |
10310 | goto exit_imsm_reshape_super; |
10311 | } | |
10312 | super->current_vol = dev->index; | |
fbf3d202 | 10313 | change = imsm_analyze_change(st, &geo, direction); |
694575e7 | 10314 | switch (change) { |
471bceb6 | 10315 | case CH_TAKEOVER: |
bb025c2f | 10316 | ret_val = imsm_takeover(st, &geo); |
694575e7 | 10317 | break; |
48c5303a PC |
10318 | case CH_MIGRATION: { |
10319 | struct imsm_update_reshape_migration *u = NULL; | |
10320 | int len = | |
10321 | imsm_create_metadata_update_for_migration( | |
10322 | st, &geo, &u); | |
10323 | if (len < 1) { | |
7a862a02 | 10324 | dprintf("imsm: Cannot prepare update\n"); |
48c5303a PC |
10325 | break; |
10326 | } | |
471bceb6 | 10327 | ret_val = 0; |
48c5303a PC |
10328 | /* update metadata locally */ |
10329 | imsm_update_metadata_locally(st, u, len); | |
10330 | /* and possibly remotely */ | |
10331 | if (st->update_tail) | |
10332 | append_metadata_update(st, u, len); | |
10333 | else | |
10334 | free(u); | |
10335 | } | |
10336 | break; | |
7abc9871 | 10337 | case CH_ARRAY_SIZE: { |
f3871fdc AK |
10338 | struct imsm_update_size_change *u = NULL; |
10339 | int len = | |
10340 | imsm_create_metadata_update_for_size_change( | |
10341 | st, &geo, &u); | |
10342 | if (len < 1) { | |
7a862a02 | 10343 | dprintf("imsm: Cannot prepare update\n"); |
f3871fdc AK |
10344 | break; |
10345 | } | |
10346 | ret_val = 0; | |
10347 | /* update metadata locally */ | |
10348 | imsm_update_metadata_locally(st, u, len); | |
10349 | /* and possibly remotely */ | |
10350 | if (st->update_tail) | |
10351 | append_metadata_update(st, u, len); | |
10352 | else | |
10353 | free(u); | |
7abc9871 AK |
10354 | } |
10355 | break; | |
471bceb6 KW |
10356 | default: |
10357 | ret_val = 1; | |
694575e7 | 10358 | } |
694575e7 | 10359 | } |
78b10e66 | 10360 | |
ed08d51c | 10361 | exit_imsm_reshape_super: |
78b10e66 N |
10362 | dprintf("imsm: reshape_super Exit code = %i\n", ret_val); |
10363 | return ret_val; | |
10364 | } | |
2cda7640 | 10365 | |
eee67a47 AK |
10366 | /******************************************************************************* |
10367 | * Function: wait_for_reshape_imsm | |
10368 | * Description: Function writes new sync_max value and waits until | |
10369 | * reshape process reach new position | |
10370 | * Parameters: | |
10371 | * sra : general array info | |
eee67a47 AK |
10372 | * ndata : number of disks in new array's layout |
10373 | * Returns: | |
10374 | * 0 : success, | |
10375 | * 1 : there is no reshape in progress, | |
10376 | * -1 : fail | |
10377 | ******************************************************************************/ | |
ae9f01f8 | 10378 | int wait_for_reshape_imsm(struct mdinfo *sra, int ndata) |
eee67a47 | 10379 | { |
85ca499c | 10380 | int fd = sysfs_get_fd(sra, NULL, "sync_completed"); |
eee67a47 | 10381 | unsigned long long completed; |
ae9f01f8 AK |
10382 | /* to_complete : new sync_max position */ |
10383 | unsigned long long to_complete = sra->reshape_progress; | |
10384 | unsigned long long position_to_set = to_complete / ndata; | |
eee67a47 | 10385 | |
ae9f01f8 | 10386 | if (fd < 0) { |
1ade5cc1 | 10387 | dprintf("cannot open reshape_position\n"); |
eee67a47 | 10388 | return 1; |
ae9f01f8 | 10389 | } |
eee67a47 | 10390 | |
ae9f01f8 | 10391 | if (sysfs_fd_get_ll(fd, &completed) < 0) { |
1ade5cc1 | 10392 | dprintf("cannot read reshape_position (no reshape in progres)\n"); |
ae9f01f8 | 10393 | close(fd); |
c85338c6 | 10394 | return 1; |
ae9f01f8 | 10395 | } |
eee67a47 | 10396 | |
85ca499c | 10397 | if (completed > position_to_set) { |
1ade5cc1 | 10398 | dprintf("wrong next position to set %llu (%llu)\n", |
85ca499c | 10399 | to_complete, position_to_set); |
ae9f01f8 AK |
10400 | close(fd); |
10401 | return -1; | |
10402 | } | |
10403 | dprintf("Position set: %llu\n", position_to_set); | |
10404 | if (sysfs_set_num(sra, NULL, "sync_max", | |
10405 | position_to_set) != 0) { | |
1ade5cc1 | 10406 | dprintf("cannot set reshape position to %llu\n", |
ae9f01f8 AK |
10407 | position_to_set); |
10408 | close(fd); | |
10409 | return -1; | |
eee67a47 AK |
10410 | } |
10411 | ||
eee67a47 AK |
10412 | do { |
10413 | char action[20]; | |
5ff3a780 AP |
10414 | int timeout = 3000; |
10415 | sysfs_wait(fd, &timeout); | |
a47e44fb AK |
10416 | if (sysfs_get_str(sra, NULL, "sync_action", |
10417 | action, 20) > 0 && | |
d7d3809a AP |
10418 | strncmp(action, "reshape", 7) != 0) { |
10419 | close(fd); | |
10420 | return -1; | |
10421 | } | |
eee67a47 | 10422 | if (sysfs_fd_get_ll(fd, &completed) < 0) { |
1ade5cc1 | 10423 | dprintf("cannot read reshape_position (in loop)\n"); |
eee67a47 AK |
10424 | close(fd); |
10425 | return 1; | |
10426 | } | |
85ca499c | 10427 | } while (completed < position_to_set); |
eee67a47 AK |
10428 | close(fd); |
10429 | return 0; | |
10430 | ||
10431 | } | |
10432 | ||
b915c95f AK |
10433 | /******************************************************************************* |
10434 | * Function: check_degradation_change | |
10435 | * Description: Check that array hasn't become failed. | |
10436 | * Parameters: | |
10437 | * info : for sysfs access | |
10438 | * sources : source disks descriptors | |
10439 | * degraded: previous degradation level | |
10440 | * Returns: | |
10441 | * degradation level | |
10442 | ******************************************************************************/ | |
10443 | int check_degradation_change(struct mdinfo *info, | |
10444 | int *sources, | |
10445 | int degraded) | |
10446 | { | |
10447 | unsigned long long new_degraded; | |
e1993023 LD |
10448 | int rv; |
10449 | ||
10450 | rv = sysfs_get_ll(info, NULL, "degraded", &new_degraded); | |
10451 | if ((rv == -1) || (new_degraded != (unsigned long long)degraded)) { | |
b915c95f AK |
10452 | /* check each device to ensure it is still working */ |
10453 | struct mdinfo *sd; | |
10454 | new_degraded = 0; | |
10455 | for (sd = info->devs ; sd ; sd = sd->next) { | |
10456 | if (sd->disk.state & (1<<MD_DISK_FAULTY)) | |
10457 | continue; | |
10458 | if (sd->disk.state & (1<<MD_DISK_SYNC)) { | |
10459 | char sbuf[20]; | |
10460 | if (sysfs_get_str(info, | |
10461 | sd, "state", sbuf, 20) < 0 || | |
10462 | strstr(sbuf, "faulty") || | |
10463 | strstr(sbuf, "in_sync") == NULL) { | |
10464 | /* this device is dead */ | |
10465 | sd->disk.state = (1<<MD_DISK_FAULTY); | |
10466 | if (sd->disk.raid_disk >= 0 && | |
10467 | sources[sd->disk.raid_disk] >= 0) { | |
10468 | close(sources[ | |
10469 | sd->disk.raid_disk]); | |
10470 | sources[sd->disk.raid_disk] = | |
10471 | -1; | |
10472 | } | |
10473 | new_degraded++; | |
10474 | } | |
10475 | } | |
10476 | } | |
10477 | } | |
10478 | ||
10479 | return new_degraded; | |
10480 | } | |
10481 | ||
10f22854 AK |
10482 | /******************************************************************************* |
10483 | * Function: imsm_manage_reshape | |
10484 | * Description: Function finds array under reshape and it manages reshape | |
10485 | * process. It creates stripes backups (if required) and sets | |
942e1cdb | 10486 | * checkpoints. |
10f22854 AK |
10487 | * Parameters: |
10488 | * afd : Backup handle (nattive) - not used | |
10489 | * sra : general array info | |
10490 | * reshape : reshape parameters - not used | |
10491 | * st : supertype structure | |
10492 | * blocks : size of critical section [blocks] | |
10493 | * fds : table of source device descriptor | |
10494 | * offsets : start of array (offest per devices) | |
10495 | * dests : not used | |
10496 | * destfd : table of destination device descriptor | |
10497 | * destoffsets : table of destination offsets (per device) | |
10498 | * Returns: | |
10499 | * 1 : success, reshape is done | |
10500 | * 0 : fail | |
10501 | ******************************************************************************/ | |
999b4972 N |
10502 | static int imsm_manage_reshape( |
10503 | int afd, struct mdinfo *sra, struct reshape *reshape, | |
10f22854 | 10504 | struct supertype *st, unsigned long backup_blocks, |
999b4972 N |
10505 | int *fds, unsigned long long *offsets, |
10506 | int dests, int *destfd, unsigned long long *destoffsets) | |
10507 | { | |
10f22854 AK |
10508 | int ret_val = 0; |
10509 | struct intel_super *super = st->sb; | |
594dc1b8 | 10510 | struct intel_dev *dv; |
10f22854 | 10511 | struct imsm_dev *dev = NULL; |
a6b6d984 | 10512 | struct imsm_map *map_src; |
10f22854 AK |
10513 | int migr_vol_qan = 0; |
10514 | int ndata, odata; /* [bytes] */ | |
10515 | int chunk; /* [bytes] */ | |
10516 | struct migr_record *migr_rec; | |
10517 | char *buf = NULL; | |
10518 | unsigned int buf_size; /* [bytes] */ | |
10519 | unsigned long long max_position; /* array size [bytes] */ | |
10520 | unsigned long long next_step; /* [blocks]/[bytes] */ | |
10521 | unsigned long long old_data_stripe_length; | |
10f22854 AK |
10522 | unsigned long long start_src; /* [bytes] */ |
10523 | unsigned long long start; /* [bytes] */ | |
10524 | unsigned long long start_buf_shift; /* [bytes] */ | |
b915c95f | 10525 | int degraded = 0; |
ab724b98 | 10526 | int source_layout = 0; |
10f22854 | 10527 | |
79a16a9b JS |
10528 | if (!sra) |
10529 | return ret_val; | |
10530 | ||
10531 | if (!fds || !offsets) | |
10f22854 AK |
10532 | goto abort; |
10533 | ||
10534 | /* Find volume during the reshape */ | |
10535 | for (dv = super->devlist; dv; dv = dv->next) { | |
10536 | if (dv->dev->vol.migr_type == MIGR_GEN_MIGR | |
10537 | && dv->dev->vol.migr_state == 1) { | |
10538 | dev = dv->dev; | |
10539 | migr_vol_qan++; | |
10540 | } | |
10541 | } | |
10542 | /* Only one volume can migrate at the same time */ | |
10543 | if (migr_vol_qan != 1) { | |
e7b84f9d | 10544 | pr_err(": %s", migr_vol_qan ? |
10f22854 AK |
10545 | "Number of migrating volumes greater than 1\n" : |
10546 | "There is no volume during migrationg\n"); | |
10547 | goto abort; | |
10548 | } | |
10549 | ||
238c0a71 | 10550 | map_src = get_imsm_map(dev, MAP_1); |
10f22854 AK |
10551 | if (map_src == NULL) |
10552 | goto abort; | |
10f22854 | 10553 | |
238c0a71 AK |
10554 | ndata = imsm_num_data_members(dev, MAP_0); |
10555 | odata = imsm_num_data_members(dev, MAP_1); | |
10f22854 | 10556 | |
7b1ab482 | 10557 | chunk = __le16_to_cpu(map_src->blocks_per_strip) * 512; |
10f22854 AK |
10558 | old_data_stripe_length = odata * chunk; |
10559 | ||
10560 | migr_rec = super->migr_rec; | |
10561 | ||
10f22854 AK |
10562 | /* initialize migration record for start condition */ |
10563 | if (sra->reshape_progress == 0) | |
10564 | init_migr_record_imsm(st, dev, sra); | |
b2c59438 AK |
10565 | else { |
10566 | if (__le32_to_cpu(migr_rec->rec_status) != UNIT_SRC_NORMAL) { | |
7a862a02 | 10567 | dprintf("imsm: cannot restart migration when data are present in copy area.\n"); |
b2c59438 AK |
10568 | goto abort; |
10569 | } | |
6a75c8ca AK |
10570 | /* Save checkpoint to update migration record for current |
10571 | * reshape position (in md). It can be farther than current | |
10572 | * reshape position in metadata. | |
10573 | */ | |
10574 | if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL) == 1) { | |
10575 | /* ignore error == 2, this can mean end of reshape here | |
10576 | */ | |
7a862a02 | 10577 | dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL, initial save)\n"); |
6a75c8ca AK |
10578 | goto abort; |
10579 | } | |
b2c59438 | 10580 | } |
10f22854 AK |
10581 | |
10582 | /* size for data */ | |
10583 | buf_size = __le32_to_cpu(migr_rec->blocks_per_unit) * 512; | |
10584 | /* extend buffer size for parity disk */ | |
10585 | buf_size += __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512; | |
10586 | /* add space for stripe aligment */ | |
10587 | buf_size += old_data_stripe_length; | |
10588 | if (posix_memalign((void **)&buf, 4096, buf_size)) { | |
10589 | dprintf("imsm: Cannot allocate checpoint buffer\n"); | |
10590 | goto abort; | |
10591 | } | |
10592 | ||
3ef4403c | 10593 | max_position = sra->component_size * ndata; |
68eb8bc6 | 10594 | source_layout = imsm_level_to_layout(map_src->raid_level); |
10f22854 AK |
10595 | |
10596 | while (__le32_to_cpu(migr_rec->curr_migr_unit) < | |
10597 | __le32_to_cpu(migr_rec->num_migr_units)) { | |
10598 | /* current reshape position [blocks] */ | |
10599 | unsigned long long current_position = | |
10600 | __le32_to_cpu(migr_rec->blocks_per_unit) | |
10601 | * __le32_to_cpu(migr_rec->curr_migr_unit); | |
10602 | unsigned long long border; | |
10603 | ||
b915c95f AK |
10604 | /* Check that array hasn't become failed. |
10605 | */ | |
10606 | degraded = check_degradation_change(sra, fds, degraded); | |
10607 | if (degraded > 1) { | |
7a862a02 | 10608 | dprintf("imsm: Abort reshape due to degradation level (%i)\n", degraded); |
b915c95f AK |
10609 | goto abort; |
10610 | } | |
10611 | ||
10f22854 AK |
10612 | next_step = __le32_to_cpu(migr_rec->blocks_per_unit); |
10613 | ||
10614 | if ((current_position + next_step) > max_position) | |
10615 | next_step = max_position - current_position; | |
10616 | ||
92144abf | 10617 | start = current_position * 512; |
10f22854 | 10618 | |
942e1cdb | 10619 | /* align reading start to old geometry */ |
10f22854 AK |
10620 | start_buf_shift = start % old_data_stripe_length; |
10621 | start_src = start - start_buf_shift; | |
10622 | ||
10623 | border = (start_src / odata) - (start / ndata); | |
10624 | border /= 512; | |
10625 | if (border <= __le32_to_cpu(migr_rec->dest_depth_per_unit)) { | |
10626 | /* save critical stripes to buf | |
10627 | * start - start address of current unit | |
10628 | * to backup [bytes] | |
10629 | * start_src - start address of current unit | |
10630 | * to backup alligned to source array | |
10631 | * [bytes] | |
10632 | */ | |
594dc1b8 | 10633 | unsigned long long next_step_filler; |
10f22854 AK |
10634 | unsigned long long copy_length = next_step * 512; |
10635 | ||
10636 | /* allign copy area length to stripe in old geometry */ | |
10637 | next_step_filler = ((copy_length + start_buf_shift) | |
10638 | % old_data_stripe_length); | |
10639 | if (next_step_filler) | |
10640 | next_step_filler = (old_data_stripe_length | |
10641 | - next_step_filler); | |
7a862a02 | 10642 | 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 |
10643 | start, start_src, copy_length, |
10644 | start_buf_shift, next_step_filler); | |
10645 | ||
10646 | if (save_stripes(fds, offsets, map_src->num_members, | |
ab724b98 AK |
10647 | chunk, map_src->raid_level, |
10648 | source_layout, 0, NULL, start_src, | |
10f22854 AK |
10649 | copy_length + |
10650 | next_step_filler + start_buf_shift, | |
10651 | buf)) { | |
7a862a02 | 10652 | dprintf("imsm: Cannot save stripes to buffer\n"); |
10f22854 AK |
10653 | goto abort; |
10654 | } | |
10655 | /* Convert data to destination format and store it | |
10656 | * in backup general migration area | |
10657 | */ | |
10658 | if (save_backup_imsm(st, dev, sra, | |
aea93171 | 10659 | buf + start_buf_shift, copy_length)) { |
7a862a02 | 10660 | dprintf("imsm: Cannot save stripes to target devices\n"); |
10f22854 AK |
10661 | goto abort; |
10662 | } | |
10663 | if (save_checkpoint_imsm(st, sra, | |
10664 | UNIT_SRC_IN_CP_AREA)) { | |
7a862a02 | 10665 | dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_IN_CP_AREA)\n"); |
10f22854 AK |
10666 | goto abort; |
10667 | } | |
8016a6d4 AK |
10668 | } else { |
10669 | /* set next step to use whole border area */ | |
10670 | border /= next_step; | |
10671 | if (border > 1) | |
10672 | next_step *= border; | |
10f22854 AK |
10673 | } |
10674 | /* When data backed up, checkpoint stored, | |
10675 | * kick the kernel to reshape unit of data | |
10676 | */ | |
10677 | next_step = next_step + sra->reshape_progress; | |
8016a6d4 AK |
10678 | /* limit next step to array max position */ |
10679 | if (next_step > max_position) | |
10680 | next_step = max_position; | |
10f22854 AK |
10681 | sysfs_set_num(sra, NULL, "suspend_lo", sra->reshape_progress); |
10682 | sysfs_set_num(sra, NULL, "suspend_hi", next_step); | |
ae9f01f8 | 10683 | sra->reshape_progress = next_step; |
10f22854 AK |
10684 | |
10685 | /* wait until reshape finish */ | |
c85338c6 | 10686 | if (wait_for_reshape_imsm(sra, ndata)) { |
c47b0ff6 AK |
10687 | dprintf("wait_for_reshape_imsm returned error!\n"); |
10688 | goto abort; | |
10689 | } | |
84d11e6c N |
10690 | if (sigterm) |
10691 | goto abort; | |
10f22854 | 10692 | |
0228d92c AK |
10693 | if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL) == 1) { |
10694 | /* ignore error == 2, this can mean end of reshape here | |
10695 | */ | |
7a862a02 | 10696 | dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL)\n"); |
10f22854 AK |
10697 | goto abort; |
10698 | } | |
10699 | ||
10700 | } | |
10701 | ||
71e5411e PB |
10702 | /* clear migr_rec on disks after successful migration */ |
10703 | struct dl *d; | |
10704 | ||
10705 | memset(super->migr_rec_buf, 0, MIGR_REC_BUF_SIZE); | |
10706 | for (d = super->disks; d; d = d->next) { | |
10707 | if (d->index < 0 || is_failed(&d->disk)) | |
10708 | continue; | |
10709 | unsigned long long dsize; | |
10710 | ||
10711 | get_dev_size(d->fd, NULL, &dsize); | |
10712 | if (lseek64(d->fd, dsize - MIGR_REC_POSITION, | |
10713 | SEEK_SET) >= 0) { | |
10714 | if (write(d->fd, super->migr_rec_buf, | |
10715 | MIGR_REC_BUF_SIZE) != MIGR_REC_BUF_SIZE) | |
10716 | perror("Write migr_rec failed"); | |
10717 | } | |
10718 | } | |
10719 | ||
10f22854 AK |
10720 | /* return '1' if done */ |
10721 | ret_val = 1; | |
10722 | abort: | |
10723 | free(buf); | |
942e1cdb N |
10724 | /* See Grow.c: abort_reshape() for further explanation */ |
10725 | sysfs_set_num(sra, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL); | |
10726 | sysfs_set_num(sra, NULL, "suspend_hi", 0); | |
10727 | sysfs_set_num(sra, NULL, "suspend_lo", 0); | |
10f22854 AK |
10728 | |
10729 | return ret_val; | |
999b4972 | 10730 | } |
0c21b485 | 10731 | |
71204a50 | 10732 | #endif /* MDASSEMBLE */ |
999b4972 | 10733 | |
cdddbdbc DW |
10734 | struct superswitch super_imsm = { |
10735 | #ifndef MDASSEMBLE | |
10736 | .examine_super = examine_super_imsm, | |
10737 | .brief_examine_super = brief_examine_super_imsm, | |
4737ae25 | 10738 | .brief_examine_subarrays = brief_examine_subarrays_imsm, |
9d84c8ea | 10739 | .export_examine_super = export_examine_super_imsm, |
cdddbdbc DW |
10740 | .detail_super = detail_super_imsm, |
10741 | .brief_detail_super = brief_detail_super_imsm, | |
bf5a934a | 10742 | .write_init_super = write_init_super_imsm, |
0e600426 N |
10743 | .validate_geometry = validate_geometry_imsm, |
10744 | .add_to_super = add_to_super_imsm, | |
1a64be56 | 10745 | .remove_from_super = remove_from_super_imsm, |
d665cc31 | 10746 | .detail_platform = detail_platform_imsm, |
e50cf220 | 10747 | .export_detail_platform = export_detail_platform_imsm, |
33414a01 | 10748 | .kill_subarray = kill_subarray_imsm, |
aa534678 | 10749 | .update_subarray = update_subarray_imsm, |
2b959fbf | 10750 | .load_container = load_container_imsm, |
71204a50 N |
10751 | .default_geometry = default_geometry_imsm, |
10752 | .get_disk_controller_domain = imsm_get_disk_controller_domain, | |
10753 | .reshape_super = imsm_reshape_super, | |
10754 | .manage_reshape = imsm_manage_reshape, | |
9e2d750d | 10755 | .recover_backup = recover_backup_imsm, |
74db60b0 | 10756 | .copy_metadata = copy_metadata_imsm, |
cdddbdbc DW |
10757 | #endif |
10758 | .match_home = match_home_imsm, | |
10759 | .uuid_from_super= uuid_from_super_imsm, | |
10760 | .getinfo_super = getinfo_super_imsm, | |
5c4cd5da | 10761 | .getinfo_super_disks = getinfo_super_disks_imsm, |
cdddbdbc DW |
10762 | .update_super = update_super_imsm, |
10763 | ||
10764 | .avail_size = avail_size_imsm, | |
80e7f8c3 | 10765 | .min_acceptable_spare_size = min_acceptable_spare_size_imsm, |
cdddbdbc DW |
10766 | |
10767 | .compare_super = compare_super_imsm, | |
10768 | ||
10769 | .load_super = load_super_imsm, | |
bf5a934a | 10770 | .init_super = init_super_imsm, |
e683ca88 | 10771 | .store_super = store_super_imsm, |
cdddbdbc DW |
10772 | .free_super = free_super_imsm, |
10773 | .match_metadata_desc = match_metadata_desc_imsm, | |
bf5a934a | 10774 | .container_content = container_content_imsm, |
0c21b485 | 10775 | .validate_container = validate_container_imsm, |
cdddbdbc | 10776 | |
cdddbdbc | 10777 | .external = 1, |
4cce4069 | 10778 | .name = "imsm", |
845dea95 | 10779 | |
0e600426 | 10780 | #ifndef MDASSEMBLE |
845dea95 NB |
10781 | /* for mdmon */ |
10782 | .open_new = imsm_open_new, | |
ed9d66aa | 10783 | .set_array_state= imsm_set_array_state, |
845dea95 NB |
10784 | .set_disk = imsm_set_disk, |
10785 | .sync_metadata = imsm_sync_metadata, | |
88758e9d | 10786 | .activate_spare = imsm_activate_spare, |
e8319a19 | 10787 | .process_update = imsm_process_update, |
8273f55e | 10788 | .prepare_update = imsm_prepare_update, |
0e600426 | 10789 | #endif /* MDASSEMBLE */ |
cdddbdbc | 10790 | }; |