]>
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 | { | |
540 | struct intel_hba *result=NULL; | |
541 | for (result = hba; result; result = result->next) { | |
542 | if (result->type == device->type && strcmp(result->path, device->path) == 0) | |
543 | break; | |
544 | } | |
545 | return result; | |
546 | } | |
547 | ||
b4cf4cba | 548 | static int attach_hba_to_super(struct intel_super *super, struct sys_dev *device) |
88654014 LM |
549 | { |
550 | struct intel_hba *hba; | |
551 | ||
552 | /* check if disk attached to Intel HBA */ | |
553 | hba = find_intel_hba(super->hba, device); | |
554 | if (hba != NULL) | |
555 | return 1; | |
556 | /* Check if HBA is already attached to super */ | |
557 | if (super->hba == NULL) { | |
558 | super->hba = alloc_intel_hba(device); | |
559 | return 1; | |
6b781d33 AP |
560 | } |
561 | ||
562 | hba = super->hba; | |
563 | /* Intel metadata allows for all disks attached to the same type HBA. | |
614902f6 | 564 | * Do not support HBA types mixing |
6b781d33 AP |
565 | */ |
566 | if (device->type != hba->type) | |
88654014 | 567 | return 2; |
6b781d33 | 568 | |
60f0f54d PB |
569 | /* Always forbid spanning between VMD domains (seen as different controllers by mdadm) */ |
570 | if (device->type == SYS_DEV_VMD && !path_attached_to_hba(device->path, hba->path)) | |
571 | return 2; | |
572 | ||
6b781d33 AP |
573 | /* Multiple same type HBAs can be used if they share the same OROM */ |
574 | const struct imsm_orom *device_orom = get_orom_by_device_id(device->dev_id); | |
575 | ||
576 | if (device_orom != super->orom) | |
577 | return 2; | |
578 | ||
579 | while (hba->next) | |
580 | hba = hba->next; | |
581 | ||
582 | hba->next = alloc_intel_hba(device); | |
583 | return 1; | |
88654014 LM |
584 | } |
585 | ||
586 | static struct sys_dev* find_disk_attached_hba(int fd, const char *devname) | |
587 | { | |
9bc4ae77 | 588 | struct sys_dev *list, *elem; |
88654014 LM |
589 | char *disk_path; |
590 | ||
591 | if ((list = find_intel_devices()) == NULL) | |
592 | return 0; | |
593 | ||
594 | if (fd < 0) | |
595 | disk_path = (char *) devname; | |
596 | else | |
597 | disk_path = diskfd_to_devpath(fd); | |
598 | ||
9bc4ae77 | 599 | if (!disk_path) |
88654014 | 600 | return 0; |
88654014 | 601 | |
9bc4ae77 N |
602 | for (elem = list; elem; elem = elem->next) |
603 | if (path_attached_to_hba(disk_path, elem->path)) | |
88654014 | 604 | return elem; |
9bc4ae77 | 605 | |
88654014 LM |
606 | if (disk_path != devname) |
607 | free(disk_path); | |
88654014 LM |
608 | |
609 | return NULL; | |
610 | } | |
611 | ||
d424212e N |
612 | static int find_intel_hba_capability(int fd, struct intel_super *super, |
613 | char *devname); | |
f2f5c343 | 614 | |
cdddbdbc DW |
615 | static struct supertype *match_metadata_desc_imsm(char *arg) |
616 | { | |
617 | struct supertype *st; | |
618 | ||
619 | if (strcmp(arg, "imsm") != 0 && | |
620 | strcmp(arg, "default") != 0 | |
621 | ) | |
622 | return NULL; | |
623 | ||
503975b9 | 624 | st = xcalloc(1, sizeof(*st)); |
cdddbdbc DW |
625 | st->ss = &super_imsm; |
626 | st->max_devs = IMSM_MAX_DEVICES; | |
627 | st->minor_version = 0; | |
628 | st->sb = NULL; | |
629 | return st; | |
630 | } | |
631 | ||
0e600426 | 632 | #ifndef MDASSEMBLE |
cdddbdbc DW |
633 | static __u8 *get_imsm_version(struct imsm_super *mpb) |
634 | { | |
635 | return &mpb->sig[MPB_SIG_LEN]; | |
636 | } | |
9e2d750d | 637 | #endif |
cdddbdbc | 638 | |
949c47a0 DW |
639 | /* retrieve a disk directly from the anchor when the anchor is known to be |
640 | * up-to-date, currently only at load time | |
641 | */ | |
642 | static struct imsm_disk *__get_imsm_disk(struct imsm_super *mpb, __u8 index) | |
cdddbdbc | 643 | { |
949c47a0 | 644 | if (index >= mpb->num_disks) |
cdddbdbc DW |
645 | return NULL; |
646 | return &mpb->disk[index]; | |
647 | } | |
648 | ||
95d07a2c LM |
649 | /* retrieve the disk description based on a index of the disk |
650 | * in the sub-array | |
651 | */ | |
652 | static struct dl *get_imsm_dl_disk(struct intel_super *super, __u8 index) | |
949c47a0 | 653 | { |
b9f594fe DW |
654 | struct dl *d; |
655 | ||
656 | for (d = super->disks; d; d = d->next) | |
657 | if (d->index == index) | |
95d07a2c LM |
658 | return d; |
659 | ||
660 | return NULL; | |
661 | } | |
662 | /* retrieve a disk from the parsed metadata */ | |
663 | static struct imsm_disk *get_imsm_disk(struct intel_super *super, __u8 index) | |
664 | { | |
665 | struct dl *dl; | |
666 | ||
667 | dl = get_imsm_dl_disk(super, index); | |
668 | if (dl) | |
669 | return &dl->disk; | |
670 | ||
b9f594fe | 671 | return NULL; |
949c47a0 DW |
672 | } |
673 | ||
674 | /* generate a checksum directly from the anchor when the anchor is known to be | |
675 | * up-to-date, currently only at load or write_super after coalescing | |
676 | */ | |
677 | static __u32 __gen_imsm_checksum(struct imsm_super *mpb) | |
cdddbdbc DW |
678 | { |
679 | __u32 end = mpb->mpb_size / sizeof(end); | |
680 | __u32 *p = (__u32 *) mpb; | |
681 | __u32 sum = 0; | |
682 | ||
5d500228 N |
683 | while (end--) { |
684 | sum += __le32_to_cpu(*p); | |
97f734fd N |
685 | p++; |
686 | } | |
cdddbdbc | 687 | |
5d500228 | 688 | return sum - __le32_to_cpu(mpb->check_sum); |
cdddbdbc DW |
689 | } |
690 | ||
a965f303 DW |
691 | static size_t sizeof_imsm_map(struct imsm_map *map) |
692 | { | |
693 | return sizeof(struct imsm_map) + sizeof(__u32) * (map->num_members - 1); | |
694 | } | |
695 | ||
696 | struct imsm_map *get_imsm_map(struct imsm_dev *dev, int second_map) | |
cdddbdbc | 697 | { |
5e7b0330 AK |
698 | /* A device can have 2 maps if it is in the middle of a migration. |
699 | * If second_map is: | |
238c0a71 AK |
700 | * MAP_0 - we return the first map |
701 | * MAP_1 - we return the second map if it exists, else NULL | |
702 | * MAP_X - we return the second map if it exists, else the first | |
5e7b0330 | 703 | */ |
a965f303 | 704 | struct imsm_map *map = &dev->vol.map[0]; |
9535fc47 | 705 | struct imsm_map *map2 = NULL; |
a965f303 | 706 | |
9535fc47 AK |
707 | if (dev->vol.migr_state) |
708 | map2 = (void *)map + sizeof_imsm_map(map); | |
a965f303 | 709 | |
9535fc47 | 710 | switch (second_map) { |
3b451610 | 711 | case MAP_0: |
9535fc47 | 712 | break; |
3b451610 | 713 | case MAP_1: |
9535fc47 AK |
714 | map = map2; |
715 | break; | |
238c0a71 | 716 | case MAP_X: |
9535fc47 AK |
717 | if (map2) |
718 | map = map2; | |
719 | break; | |
9535fc47 AK |
720 | default: |
721 | map = NULL; | |
722 | } | |
723 | return map; | |
5e7b0330 | 724 | |
a965f303 | 725 | } |
cdddbdbc | 726 | |
3393c6af DW |
727 | /* return the size of the device. |
728 | * migr_state increases the returned size if map[0] were to be duplicated | |
729 | */ | |
730 | static size_t sizeof_imsm_dev(struct imsm_dev *dev, int migr_state) | |
a965f303 DW |
731 | { |
732 | size_t size = sizeof(*dev) - sizeof(struct imsm_map) + | |
238c0a71 | 733 | sizeof_imsm_map(get_imsm_map(dev, MAP_0)); |
cdddbdbc DW |
734 | |
735 | /* migrating means an additional map */ | |
a965f303 | 736 | if (dev->vol.migr_state) |
238c0a71 | 737 | size += sizeof_imsm_map(get_imsm_map(dev, MAP_1)); |
3393c6af | 738 | else if (migr_state) |
238c0a71 | 739 | size += sizeof_imsm_map(get_imsm_map(dev, MAP_0)); |
cdddbdbc DW |
740 | |
741 | return size; | |
742 | } | |
743 | ||
54c2c1ea DW |
744 | #ifndef MDASSEMBLE |
745 | /* retrieve disk serial number list from a metadata update */ | |
746 | static struct disk_info *get_disk_info(struct imsm_update_create_array *update) | |
747 | { | |
748 | void *u = update; | |
749 | struct disk_info *inf; | |
750 | ||
751 | inf = u + sizeof(*update) - sizeof(struct imsm_dev) + | |
752 | sizeof_imsm_dev(&update->dev, 0); | |
753 | ||
754 | return inf; | |
755 | } | |
756 | #endif | |
757 | ||
949c47a0 | 758 | static struct imsm_dev *__get_imsm_dev(struct imsm_super *mpb, __u8 index) |
cdddbdbc DW |
759 | { |
760 | int offset; | |
761 | int i; | |
762 | void *_mpb = mpb; | |
763 | ||
949c47a0 | 764 | if (index >= mpb->num_raid_devs) |
cdddbdbc DW |
765 | return NULL; |
766 | ||
767 | /* devices start after all disks */ | |
768 | offset = ((void *) &mpb->disk[mpb->num_disks]) - _mpb; | |
769 | ||
770 | for (i = 0; i <= index; i++) | |
771 | if (i == index) | |
772 | return _mpb + offset; | |
773 | else | |
3393c6af | 774 | offset += sizeof_imsm_dev(_mpb + offset, 0); |
cdddbdbc DW |
775 | |
776 | return NULL; | |
777 | } | |
778 | ||
949c47a0 DW |
779 | static struct imsm_dev *get_imsm_dev(struct intel_super *super, __u8 index) |
780 | { | |
ba2de7ba DW |
781 | struct intel_dev *dv; |
782 | ||
949c47a0 DW |
783 | if (index >= super->anchor->num_raid_devs) |
784 | return NULL; | |
ba2de7ba DW |
785 | for (dv = super->devlist; dv; dv = dv->next) |
786 | if (dv->index == index) | |
787 | return dv->dev; | |
788 | return NULL; | |
949c47a0 DW |
789 | } |
790 | ||
98130f40 AK |
791 | /* |
792 | * for second_map: | |
238c0a71 AK |
793 | * == MAP_0 get first map |
794 | * == MAP_1 get second map | |
795 | * == MAP_X than get map according to the current migr_state | |
98130f40 AK |
796 | */ |
797 | static __u32 get_imsm_ord_tbl_ent(struct imsm_dev *dev, | |
798 | int slot, | |
799 | int second_map) | |
7eef0453 DW |
800 | { |
801 | struct imsm_map *map; | |
802 | ||
5e7b0330 | 803 | map = get_imsm_map(dev, second_map); |
7eef0453 | 804 | |
ff077194 DW |
805 | /* top byte identifies disk under rebuild */ |
806 | return __le32_to_cpu(map->disk_ord_tbl[slot]); | |
807 | } | |
808 | ||
809 | #define ord_to_idx(ord) (((ord) << 8) >> 8) | |
98130f40 | 810 | static __u32 get_imsm_disk_idx(struct imsm_dev *dev, int slot, int second_map) |
ff077194 | 811 | { |
98130f40 | 812 | __u32 ord = get_imsm_ord_tbl_ent(dev, slot, second_map); |
ff077194 DW |
813 | |
814 | return ord_to_idx(ord); | |
7eef0453 DW |
815 | } |
816 | ||
be73972f DW |
817 | static void set_imsm_ord_tbl_ent(struct imsm_map *map, int slot, __u32 ord) |
818 | { | |
819 | map->disk_ord_tbl[slot] = __cpu_to_le32(ord); | |
820 | } | |
821 | ||
f21e18ca | 822 | static int get_imsm_disk_slot(struct imsm_map *map, unsigned idx) |
620b1713 DW |
823 | { |
824 | int slot; | |
825 | __u32 ord; | |
826 | ||
827 | for (slot = 0; slot < map->num_members; slot++) { | |
828 | ord = __le32_to_cpu(map->disk_ord_tbl[slot]); | |
829 | if (ord_to_idx(ord) == idx) | |
830 | return slot; | |
831 | } | |
832 | ||
833 | return -1; | |
834 | } | |
835 | ||
cdddbdbc DW |
836 | static int get_imsm_raid_level(struct imsm_map *map) |
837 | { | |
838 | if (map->raid_level == 1) { | |
839 | if (map->num_members == 2) | |
840 | return 1; | |
841 | else | |
842 | return 10; | |
843 | } | |
844 | ||
845 | return map->raid_level; | |
846 | } | |
847 | ||
c2c087e6 DW |
848 | static int cmp_extent(const void *av, const void *bv) |
849 | { | |
850 | const struct extent *a = av; | |
851 | const struct extent *b = bv; | |
852 | if (a->start < b->start) | |
853 | return -1; | |
854 | if (a->start > b->start) | |
855 | return 1; | |
856 | return 0; | |
857 | } | |
858 | ||
0dcecb2e | 859 | static int count_memberships(struct dl *dl, struct intel_super *super) |
c2c087e6 | 860 | { |
c2c087e6 | 861 | int memberships = 0; |
620b1713 | 862 | int i; |
c2c087e6 | 863 | |
949c47a0 DW |
864 | for (i = 0; i < super->anchor->num_raid_devs; i++) { |
865 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
238c0a71 | 866 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
c2c087e6 | 867 | |
620b1713 DW |
868 | if (get_imsm_disk_slot(map, dl->index) >= 0) |
869 | memberships++; | |
c2c087e6 | 870 | } |
0dcecb2e DW |
871 | |
872 | return memberships; | |
873 | } | |
874 | ||
b81221b7 CA |
875 | static __u32 imsm_min_reserved_sectors(struct intel_super *super); |
876 | ||
5551b113 CA |
877 | static int split_ull(unsigned long long n, __u32 *lo, __u32 *hi) |
878 | { | |
879 | if (lo == 0 || hi == 0) | |
880 | return 1; | |
881 | *lo = __le32_to_cpu((unsigned)n); | |
882 | *hi = __le32_to_cpu((unsigned)(n >> 32)); | |
883 | return 0; | |
884 | } | |
885 | ||
886 | static unsigned long long join_u32(__u32 lo, __u32 hi) | |
887 | { | |
888 | return (unsigned long long)__le32_to_cpu(lo) | | |
889 | (((unsigned long long)__le32_to_cpu(hi)) << 32); | |
890 | } | |
891 | ||
892 | static unsigned long long total_blocks(struct imsm_disk *disk) | |
893 | { | |
894 | if (disk == NULL) | |
895 | return 0; | |
896 | return join_u32(disk->total_blocks_lo, disk->total_blocks_hi); | |
897 | } | |
898 | ||
899 | static unsigned long long pba_of_lba0(struct imsm_map *map) | |
900 | { | |
901 | if (map == NULL) | |
902 | return 0; | |
903 | return join_u32(map->pba_of_lba0_lo, map->pba_of_lba0_hi); | |
904 | } | |
905 | ||
906 | static unsigned long long blocks_per_member(struct imsm_map *map) | |
907 | { | |
908 | if (map == NULL) | |
909 | return 0; | |
910 | return join_u32(map->blocks_per_member_lo, map->blocks_per_member_hi); | |
911 | } | |
912 | ||
ae2416e7 | 913 | #ifndef MDASSEMBLE |
5551b113 CA |
914 | static unsigned long long num_data_stripes(struct imsm_map *map) |
915 | { | |
916 | if (map == NULL) | |
917 | return 0; | |
918 | return join_u32(map->num_data_stripes_lo, map->num_data_stripes_hi); | |
919 | } | |
920 | ||
921 | static void set_total_blocks(struct imsm_disk *disk, unsigned long long n) | |
922 | { | |
923 | split_ull(n, &disk->total_blocks_lo, &disk->total_blocks_hi); | |
924 | } | |
ae2416e7 | 925 | #endif |
5551b113 CA |
926 | |
927 | static void set_pba_of_lba0(struct imsm_map *map, unsigned long long n) | |
928 | { | |
929 | split_ull(n, &map->pba_of_lba0_lo, &map->pba_of_lba0_hi); | |
930 | } | |
931 | ||
932 | static void set_blocks_per_member(struct imsm_map *map, unsigned long long n) | |
933 | { | |
934 | split_ull(n, &map->blocks_per_member_lo, &map->blocks_per_member_hi); | |
935 | } | |
936 | ||
937 | static void set_num_data_stripes(struct imsm_map *map, unsigned long long n) | |
938 | { | |
939 | split_ull(n, &map->num_data_stripes_lo, &map->num_data_stripes_hi); | |
940 | } | |
941 | ||
0dcecb2e DW |
942 | static struct extent *get_extents(struct intel_super *super, struct dl *dl) |
943 | { | |
944 | /* find a list of used extents on the given physical device */ | |
945 | struct extent *rv, *e; | |
620b1713 | 946 | int i; |
0dcecb2e | 947 | int memberships = count_memberships(dl, super); |
b276dd33 DW |
948 | __u32 reservation; |
949 | ||
950 | /* trim the reserved area for spares, so they can join any array | |
951 | * regardless of whether the OROM has assigned sectors from the | |
952 | * IMSM_RESERVED_SECTORS region | |
953 | */ | |
954 | if (dl->index == -1) | |
b81221b7 | 955 | reservation = imsm_min_reserved_sectors(super); |
b276dd33 DW |
956 | else |
957 | reservation = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS; | |
0dcecb2e | 958 | |
503975b9 | 959 | rv = xcalloc(sizeof(struct extent), (memberships + 1)); |
c2c087e6 DW |
960 | e = rv; |
961 | ||
949c47a0 DW |
962 | for (i = 0; i < super->anchor->num_raid_devs; i++) { |
963 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
238c0a71 | 964 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
c2c087e6 | 965 | |
620b1713 | 966 | if (get_imsm_disk_slot(map, dl->index) >= 0) { |
5551b113 CA |
967 | e->start = pba_of_lba0(map); |
968 | e->size = blocks_per_member(map); | |
620b1713 | 969 | e++; |
c2c087e6 DW |
970 | } |
971 | } | |
972 | qsort(rv, memberships, sizeof(*rv), cmp_extent); | |
973 | ||
1011e834 | 974 | /* determine the start of the metadata |
14e8215b DW |
975 | * when no raid devices are defined use the default |
976 | * ...otherwise allow the metadata to truncate the value | |
977 | * as is the case with older versions of imsm | |
978 | */ | |
979 | if (memberships) { | |
980 | struct extent *last = &rv[memberships - 1]; | |
5551b113 | 981 | unsigned long long remainder; |
14e8215b | 982 | |
5551b113 | 983 | remainder = total_blocks(&dl->disk) - (last->start + last->size); |
dda5855f DW |
984 | /* round down to 1k block to satisfy precision of the kernel |
985 | * 'size' interface | |
986 | */ | |
987 | remainder &= ~1UL; | |
988 | /* make sure remainder is still sane */ | |
f21e18ca | 989 | if (remainder < (unsigned)ROUND_UP(super->len, 512) >> 9) |
dda5855f | 990 | remainder = ROUND_UP(super->len, 512) >> 9; |
14e8215b DW |
991 | if (reservation > remainder) |
992 | reservation = remainder; | |
993 | } | |
5551b113 | 994 | e->start = total_blocks(&dl->disk) - reservation; |
c2c087e6 DW |
995 | e->size = 0; |
996 | return rv; | |
997 | } | |
998 | ||
14e8215b DW |
999 | /* try to determine how much space is reserved for metadata from |
1000 | * the last get_extents() entry, otherwise fallback to the | |
1001 | * default | |
1002 | */ | |
1003 | static __u32 imsm_reserved_sectors(struct intel_super *super, struct dl *dl) | |
1004 | { | |
1005 | struct extent *e; | |
1006 | int i; | |
1007 | __u32 rv; | |
1008 | ||
1009 | /* for spares just return a minimal reservation which will grow | |
1010 | * once the spare is picked up by an array | |
1011 | */ | |
1012 | if (dl->index == -1) | |
1013 | return MPB_SECTOR_CNT; | |
1014 | ||
1015 | e = get_extents(super, dl); | |
1016 | if (!e) | |
1017 | return MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS; | |
1018 | ||
1019 | /* scroll to last entry */ | |
1020 | for (i = 0; e[i].size; i++) | |
1021 | continue; | |
1022 | ||
5551b113 | 1023 | rv = total_blocks(&dl->disk) - e[i].start; |
14e8215b DW |
1024 | |
1025 | free(e); | |
1026 | ||
1027 | return rv; | |
1028 | } | |
1029 | ||
25ed7e59 DW |
1030 | static int is_spare(struct imsm_disk *disk) |
1031 | { | |
1032 | return (disk->status & SPARE_DISK) == SPARE_DISK; | |
1033 | } | |
1034 | ||
1035 | static int is_configured(struct imsm_disk *disk) | |
1036 | { | |
1037 | return (disk->status & CONFIGURED_DISK) == CONFIGURED_DISK; | |
1038 | } | |
1039 | ||
1040 | static int is_failed(struct imsm_disk *disk) | |
1041 | { | |
1042 | return (disk->status & FAILED_DISK) == FAILED_DISK; | |
1043 | } | |
1044 | ||
b81221b7 CA |
1045 | /* try to determine how much space is reserved for metadata from |
1046 | * the last get_extents() entry on the smallest active disk, | |
1047 | * otherwise fallback to the default | |
1048 | */ | |
1049 | static __u32 imsm_min_reserved_sectors(struct intel_super *super) | |
1050 | { | |
1051 | struct extent *e; | |
1052 | int i; | |
5551b113 CA |
1053 | unsigned long long min_active; |
1054 | __u32 remainder; | |
b81221b7 CA |
1055 | __u32 rv = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS; |
1056 | struct dl *dl, *dl_min = NULL; | |
1057 | ||
1058 | if (!super) | |
1059 | return rv; | |
1060 | ||
1061 | min_active = 0; | |
1062 | for (dl = super->disks; dl; dl = dl->next) { | |
1063 | if (dl->index < 0) | |
1064 | continue; | |
5551b113 CA |
1065 | unsigned long long blocks = total_blocks(&dl->disk); |
1066 | if (blocks < min_active || min_active == 0) { | |
b81221b7 | 1067 | dl_min = dl; |
5551b113 | 1068 | min_active = blocks; |
b81221b7 CA |
1069 | } |
1070 | } | |
1071 | if (!dl_min) | |
1072 | return rv; | |
1073 | ||
1074 | /* find last lba used by subarrays on the smallest active disk */ | |
1075 | e = get_extents(super, dl_min); | |
1076 | if (!e) | |
1077 | return rv; | |
1078 | for (i = 0; e[i].size; i++) | |
1079 | continue; | |
1080 | ||
1081 | remainder = min_active - e[i].start; | |
1082 | free(e); | |
1083 | ||
1084 | /* to give priority to recovery we should not require full | |
1085 | IMSM_RESERVED_SECTORS from the spare */ | |
1086 | rv = MPB_SECTOR_CNT + NUM_BLOCKS_DIRTY_STRIPE_REGION; | |
1087 | ||
1088 | /* if real reservation is smaller use that value */ | |
1089 | return (remainder < rv) ? remainder : rv; | |
1090 | } | |
1091 | ||
80e7f8c3 AC |
1092 | /* Return minimum size of a spare that can be used in this array*/ |
1093 | static unsigned long long min_acceptable_spare_size_imsm(struct supertype *st) | |
1094 | { | |
1095 | struct intel_super *super = st->sb; | |
1096 | struct dl *dl; | |
1097 | struct extent *e; | |
1098 | int i; | |
1099 | unsigned long long rv = 0; | |
1100 | ||
1101 | if (!super) | |
1102 | return rv; | |
1103 | /* find first active disk in array */ | |
1104 | dl = super->disks; | |
1105 | while (dl && (is_failed(&dl->disk) || dl->index == -1)) | |
1106 | dl = dl->next; | |
1107 | if (!dl) | |
1108 | return rv; | |
1109 | /* find last lba used by subarrays */ | |
1110 | e = get_extents(super, dl); | |
1111 | if (!e) | |
1112 | return rv; | |
1113 | for (i = 0; e[i].size; i++) | |
1114 | continue; | |
1115 | if (i > 0) | |
1116 | rv = e[i-1].start + e[i-1].size; | |
1117 | free(e); | |
b81221b7 | 1118 | |
80e7f8c3 | 1119 | /* add the amount of space needed for metadata */ |
b81221b7 CA |
1120 | rv = rv + imsm_min_reserved_sectors(super); |
1121 | ||
80e7f8c3 AC |
1122 | return rv * 512; |
1123 | } | |
1124 | ||
d1e02575 AK |
1125 | static int is_gen_migration(struct imsm_dev *dev); |
1126 | ||
1799c9e8 | 1127 | #ifndef MDASSEMBLE |
c47b0ff6 AK |
1128 | static __u64 blocks_per_migr_unit(struct intel_super *super, |
1129 | struct imsm_dev *dev); | |
1e5c6983 | 1130 | |
c47b0ff6 AK |
1131 | static void print_imsm_dev(struct intel_super *super, |
1132 | struct imsm_dev *dev, | |
1133 | char *uuid, | |
1134 | int disk_idx) | |
cdddbdbc DW |
1135 | { |
1136 | __u64 sz; | |
0d80bb2f | 1137 | int slot, i; |
238c0a71 AK |
1138 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
1139 | struct imsm_map *map2 = get_imsm_map(dev, MAP_1); | |
b10b37b8 | 1140 | __u32 ord; |
cdddbdbc DW |
1141 | |
1142 | printf("\n"); | |
1e7bc0ed | 1143 | printf("[%.16s]:\n", dev->volume); |
44470971 | 1144 | printf(" UUID : %s\n", uuid); |
dd8bcb3b AK |
1145 | printf(" RAID Level : %d", get_imsm_raid_level(map)); |
1146 | if (map2) | |
1147 | printf(" <-- %d", get_imsm_raid_level(map2)); | |
1148 | printf("\n"); | |
1149 | printf(" Members : %d", map->num_members); | |
1150 | if (map2) | |
1151 | printf(" <-- %d", map2->num_members); | |
1152 | printf("\n"); | |
0d80bb2f DW |
1153 | printf(" Slots : ["); |
1154 | for (i = 0; i < map->num_members; i++) { | |
238c0a71 | 1155 | ord = get_imsm_ord_tbl_ent(dev, i, MAP_0); |
0d80bb2f DW |
1156 | printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U"); |
1157 | } | |
dd8bcb3b AK |
1158 | printf("]"); |
1159 | if (map2) { | |
1160 | printf(" <-- ["); | |
1161 | for (i = 0; i < map2->num_members; i++) { | |
238c0a71 | 1162 | ord = get_imsm_ord_tbl_ent(dev, i, MAP_1); |
dd8bcb3b AK |
1163 | printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U"); |
1164 | } | |
1165 | printf("]"); | |
1166 | } | |
1167 | printf("\n"); | |
7095bccb AK |
1168 | printf(" Failed disk : "); |
1169 | if (map->failed_disk_num == 0xff) | |
1170 | printf("none"); | |
1171 | else | |
1172 | printf("%i", map->failed_disk_num); | |
1173 | printf("\n"); | |
620b1713 DW |
1174 | slot = get_imsm_disk_slot(map, disk_idx); |
1175 | if (slot >= 0) { | |
238c0a71 | 1176 | ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X); |
b10b37b8 DW |
1177 | printf(" This Slot : %d%s\n", slot, |
1178 | ord & IMSM_ORD_REBUILD ? " (out-of-sync)" : ""); | |
1179 | } else | |
cdddbdbc DW |
1180 | printf(" This Slot : ?\n"); |
1181 | sz = __le32_to_cpu(dev->size_high); | |
1182 | sz <<= 32; | |
1183 | sz += __le32_to_cpu(dev->size_low); | |
1184 | printf(" Array Size : %llu%s\n", (unsigned long long)sz, | |
1185 | human_size(sz * 512)); | |
5551b113 | 1186 | sz = blocks_per_member(map); |
cdddbdbc DW |
1187 | printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz, |
1188 | human_size(sz * 512)); | |
5551b113 CA |
1189 | printf(" Sector Offset : %llu\n", |
1190 | pba_of_lba0(map)); | |
1191 | printf(" Num Stripes : %llu\n", | |
1192 | num_data_stripes(map)); | |
dd8bcb3b | 1193 | printf(" Chunk Size : %u KiB", |
cdddbdbc | 1194 | __le16_to_cpu(map->blocks_per_strip) / 2); |
dd8bcb3b AK |
1195 | if (map2) |
1196 | printf(" <-- %u KiB", | |
1197 | __le16_to_cpu(map2->blocks_per_strip) / 2); | |
1198 | printf("\n"); | |
cdddbdbc | 1199 | printf(" Reserved : %d\n", __le32_to_cpu(dev->reserved_blocks)); |
8655a7b1 | 1200 | printf(" Migrate State : "); |
1484e727 DW |
1201 | if (dev->vol.migr_state) { |
1202 | if (migr_type(dev) == MIGR_INIT) | |
8655a7b1 | 1203 | printf("initialize\n"); |
1484e727 | 1204 | else if (migr_type(dev) == MIGR_REBUILD) |
8655a7b1 | 1205 | printf("rebuild\n"); |
1484e727 | 1206 | else if (migr_type(dev) == MIGR_VERIFY) |
8655a7b1 | 1207 | printf("check\n"); |
1484e727 | 1208 | else if (migr_type(dev) == MIGR_GEN_MIGR) |
8655a7b1 | 1209 | printf("general migration\n"); |
1484e727 | 1210 | else if (migr_type(dev) == MIGR_STATE_CHANGE) |
8655a7b1 | 1211 | printf("state change\n"); |
1484e727 | 1212 | else if (migr_type(dev) == MIGR_REPAIR) |
8655a7b1 | 1213 | printf("repair\n"); |
1484e727 | 1214 | else |
8655a7b1 DW |
1215 | printf("<unknown:%d>\n", migr_type(dev)); |
1216 | } else | |
1217 | printf("idle\n"); | |
3393c6af DW |
1218 | printf(" Map State : %s", map_state_str[map->map_state]); |
1219 | if (dev->vol.migr_state) { | |
238c0a71 | 1220 | struct imsm_map *map = get_imsm_map(dev, MAP_1); |
1e5c6983 | 1221 | |
b10b37b8 | 1222 | printf(" <-- %s", map_state_str[map->map_state]); |
464d40e8 LD |
1223 | printf("\n Checkpoint : %u ", |
1224 | __le32_to_cpu(dev->vol.curr_migr_unit)); | |
3136abe5 | 1225 | if ((is_gen_migration(dev)) && ((slot > 1) || (slot < 0))) |
464d40e8 LD |
1226 | printf("(N/A)"); |
1227 | else | |
1228 | printf("(%llu)", (unsigned long long) | |
1229 | blocks_per_migr_unit(super, dev)); | |
3393c6af DW |
1230 | } |
1231 | printf("\n"); | |
cdddbdbc | 1232 | printf(" Dirty State : %s\n", dev->vol.dirty ? "dirty" : "clean"); |
cdddbdbc DW |
1233 | } |
1234 | ||
0ec1f4e8 | 1235 | static void print_imsm_disk(struct imsm_disk *disk, int index, __u32 reserved) |
cdddbdbc | 1236 | { |
1f24f035 | 1237 | char str[MAX_RAID_SERIAL_LEN + 1]; |
cdddbdbc DW |
1238 | __u64 sz; |
1239 | ||
0ec1f4e8 | 1240 | if (index < -1 || !disk) |
e9d82038 DW |
1241 | return; |
1242 | ||
cdddbdbc | 1243 | printf("\n"); |
1f24f035 | 1244 | snprintf(str, MAX_RAID_SERIAL_LEN + 1, "%s", disk->serial); |
0ec1f4e8 DW |
1245 | if (index >= 0) |
1246 | printf(" Disk%02d Serial : %s\n", index, str); | |
1247 | else | |
1248 | printf(" Disk Serial : %s\n", str); | |
25ed7e59 DW |
1249 | printf(" State :%s%s%s\n", is_spare(disk) ? " spare" : "", |
1250 | is_configured(disk) ? " active" : "", | |
1251 | is_failed(disk) ? " failed" : ""); | |
cdddbdbc | 1252 | printf(" Id : %08x\n", __le32_to_cpu(disk->scsi_id)); |
5551b113 | 1253 | sz = total_blocks(disk) - reserved; |
cdddbdbc DW |
1254 | printf(" Usable Size : %llu%s\n", (unsigned long long)sz, |
1255 | human_size(sz * 512)); | |
1256 | } | |
1257 | ||
520e69e2 AK |
1258 | void examine_migr_rec_imsm(struct intel_super *super) |
1259 | { | |
1260 | struct migr_record *migr_rec = super->migr_rec; | |
1261 | struct imsm_super *mpb = super->anchor; | |
1262 | int i; | |
1263 | ||
1264 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
1265 | struct imsm_dev *dev = __get_imsm_dev(mpb, i); | |
3136abe5 | 1266 | struct imsm_map *map; |
b4ab44d8 | 1267 | int slot = -1; |
3136abe5 | 1268 | |
520e69e2 AK |
1269 | if (is_gen_migration(dev) == 0) |
1270 | continue; | |
1271 | ||
1272 | printf("\nMigration Record Information:"); | |
3136abe5 | 1273 | |
44bfe6df AK |
1274 | /* first map under migration */ |
1275 | map = get_imsm_map(dev, MAP_0); | |
3136abe5 AK |
1276 | if (map) |
1277 | slot = get_imsm_disk_slot(map, super->disks->index); | |
1278 | if ((map == NULL) || (slot > 1) || (slot < 0)) { | |
520e69e2 AK |
1279 | printf(" Empty\n "); |
1280 | printf("Examine one of first two disks in array\n"); | |
1281 | break; | |
1282 | } | |
1283 | printf("\n Status : "); | |
1284 | if (__le32_to_cpu(migr_rec->rec_status) == UNIT_SRC_NORMAL) | |
1285 | printf("Normal\n"); | |
1286 | else | |
1287 | printf("Contains Data\n"); | |
1288 | printf(" Current Unit : %u\n", | |
1289 | __le32_to_cpu(migr_rec->curr_migr_unit)); | |
1290 | printf(" Family : %u\n", | |
1291 | __le32_to_cpu(migr_rec->family_num)); | |
1292 | printf(" Ascending : %u\n", | |
1293 | __le32_to_cpu(migr_rec->ascending_migr)); | |
1294 | printf(" Blocks Per Unit : %u\n", | |
1295 | __le32_to_cpu(migr_rec->blocks_per_unit)); | |
1296 | printf(" Dest. Depth Per Unit : %u\n", | |
1297 | __le32_to_cpu(migr_rec->dest_depth_per_unit)); | |
1298 | printf(" Checkpoint Area pba : %u\n", | |
1299 | __le32_to_cpu(migr_rec->ckpt_area_pba)); | |
1300 | printf(" First member lba : %u\n", | |
1301 | __le32_to_cpu(migr_rec->dest_1st_member_lba)); | |
1302 | printf(" Total Number of Units : %u\n", | |
1303 | __le32_to_cpu(migr_rec->num_migr_units)); | |
1304 | printf(" Size of volume : %u\n", | |
1305 | __le32_to_cpu(migr_rec->post_migr_vol_cap)); | |
1306 | printf(" Expansion space for LBA64 : %u\n", | |
1307 | __le32_to_cpu(migr_rec->post_migr_vol_cap_hi)); | |
1308 | printf(" Record was read from : %u\n", | |
1309 | __le32_to_cpu(migr_rec->ckpt_read_disk_num)); | |
1310 | ||
1311 | break; | |
1312 | } | |
1313 | } | |
9e2d750d | 1314 | #endif /* MDASSEMBLE */ |
19482bcc AK |
1315 | /******************************************************************************* |
1316 | * function: imsm_check_attributes | |
1317 | * Description: Function checks if features represented by attributes flags | |
1011e834 | 1318 | * are supported by mdadm. |
19482bcc AK |
1319 | * Parameters: |
1320 | * attributes - Attributes read from metadata | |
1321 | * Returns: | |
1011e834 N |
1322 | * 0 - passed attributes contains unsupported features flags |
1323 | * 1 - all features are supported | |
19482bcc AK |
1324 | ******************************************************************************/ |
1325 | static int imsm_check_attributes(__u32 attributes) | |
1326 | { | |
1327 | int ret_val = 1; | |
418f9b36 N |
1328 | __u32 not_supported = MPB_ATTRIB_SUPPORTED^0xffffffff; |
1329 | ||
1330 | not_supported &= ~MPB_ATTRIB_IGNORED; | |
19482bcc AK |
1331 | |
1332 | not_supported &= attributes; | |
1333 | if (not_supported) { | |
e7b84f9d | 1334 | pr_err("(IMSM): Unsupported attributes : %x\n", |
418f9b36 | 1335 | (unsigned)__le32_to_cpu(not_supported)); |
19482bcc AK |
1336 | if (not_supported & MPB_ATTRIB_CHECKSUM_VERIFY) { |
1337 | dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n"); | |
1338 | not_supported ^= MPB_ATTRIB_CHECKSUM_VERIFY; | |
1339 | } | |
1340 | if (not_supported & MPB_ATTRIB_2TB) { | |
1341 | dprintf("\t\tMPB_ATTRIB_2TB\n"); | |
1342 | not_supported ^= MPB_ATTRIB_2TB; | |
1343 | } | |
1344 | if (not_supported & MPB_ATTRIB_RAID0) { | |
1345 | dprintf("\t\tMPB_ATTRIB_RAID0\n"); | |
1346 | not_supported ^= MPB_ATTRIB_RAID0; | |
1347 | } | |
1348 | if (not_supported & MPB_ATTRIB_RAID1) { | |
1349 | dprintf("\t\tMPB_ATTRIB_RAID1\n"); | |
1350 | not_supported ^= MPB_ATTRIB_RAID1; | |
1351 | } | |
1352 | if (not_supported & MPB_ATTRIB_RAID10) { | |
1353 | dprintf("\t\tMPB_ATTRIB_RAID10\n"); | |
1354 | not_supported ^= MPB_ATTRIB_RAID10; | |
1355 | } | |
1356 | if (not_supported & MPB_ATTRIB_RAID1E) { | |
1357 | dprintf("\t\tMPB_ATTRIB_RAID1E\n"); | |
1358 | not_supported ^= MPB_ATTRIB_RAID1E; | |
1359 | } | |
1360 | if (not_supported & MPB_ATTRIB_RAID5) { | |
1361 | dprintf("\t\tMPB_ATTRIB_RAID5\n"); | |
1362 | not_supported ^= MPB_ATTRIB_RAID5; | |
1363 | } | |
1364 | if (not_supported & MPB_ATTRIB_RAIDCNG) { | |
1365 | dprintf("\t\tMPB_ATTRIB_RAIDCNG\n"); | |
1366 | not_supported ^= MPB_ATTRIB_RAIDCNG; | |
1367 | } | |
1368 | if (not_supported & MPB_ATTRIB_BBM) { | |
1369 | dprintf("\t\tMPB_ATTRIB_BBM\n"); | |
1370 | not_supported ^= MPB_ATTRIB_BBM; | |
1371 | } | |
1372 | if (not_supported & MPB_ATTRIB_CHECKSUM_VERIFY) { | |
1373 | dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n"); | |
1374 | not_supported ^= MPB_ATTRIB_CHECKSUM_VERIFY; | |
1375 | } | |
1376 | if (not_supported & MPB_ATTRIB_EXP_STRIPE_SIZE) { | |
1377 | dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n"); | |
1378 | not_supported ^= MPB_ATTRIB_EXP_STRIPE_SIZE; | |
1379 | } | |
1380 | if (not_supported & MPB_ATTRIB_2TB_DISK) { | |
1381 | dprintf("\t\tMPB_ATTRIB_2TB_DISK\n"); | |
1382 | not_supported ^= MPB_ATTRIB_2TB_DISK; | |
1383 | } | |
1384 | if (not_supported & MPB_ATTRIB_NEVER_USE2) { | |
1385 | dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n"); | |
1386 | not_supported ^= MPB_ATTRIB_NEVER_USE2; | |
1387 | } | |
1388 | if (not_supported & MPB_ATTRIB_NEVER_USE) { | |
1389 | dprintf("\t\tMPB_ATTRIB_NEVER_USE\n"); | |
1390 | not_supported ^= MPB_ATTRIB_NEVER_USE; | |
1391 | } | |
1392 | ||
1393 | if (not_supported) | |
1ade5cc1 | 1394 | dprintf("(IMSM): Unknown attributes : %x\n", not_supported); |
19482bcc AK |
1395 | |
1396 | ret_val = 0; | |
1397 | } | |
1398 | ||
1399 | return ret_val; | |
1400 | } | |
1401 | ||
9e2d750d | 1402 | #ifndef MDASSEMBLE |
a5d85af7 | 1403 | static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map); |
44470971 | 1404 | |
cdddbdbc DW |
1405 | static void examine_super_imsm(struct supertype *st, char *homehost) |
1406 | { | |
1407 | struct intel_super *super = st->sb; | |
949c47a0 | 1408 | struct imsm_super *mpb = super->anchor; |
cdddbdbc DW |
1409 | char str[MAX_SIGNATURE_LENGTH]; |
1410 | int i; | |
27fd6274 DW |
1411 | struct mdinfo info; |
1412 | char nbuf[64]; | |
cdddbdbc | 1413 | __u32 sum; |
14e8215b | 1414 | __u32 reserved = imsm_reserved_sectors(super, super->disks); |
94827db3 | 1415 | struct dl *dl; |
27fd6274 | 1416 | |
cdddbdbc DW |
1417 | snprintf(str, MPB_SIG_LEN, "%s", mpb->sig); |
1418 | printf(" Magic : %s\n", str); | |
1419 | snprintf(str, strlen(MPB_VERSION_RAID0), "%s", get_imsm_version(mpb)); | |
1420 | printf(" Version : %s\n", get_imsm_version(mpb)); | |
148acb7b | 1421 | printf(" Orig Family : %08x\n", __le32_to_cpu(mpb->orig_family_num)); |
cdddbdbc DW |
1422 | printf(" Family : %08x\n", __le32_to_cpu(mpb->family_num)); |
1423 | printf(" Generation : %08x\n", __le32_to_cpu(mpb->generation_num)); | |
19482bcc AK |
1424 | printf(" Attributes : "); |
1425 | if (imsm_check_attributes(mpb->attributes)) | |
1426 | printf("All supported\n"); | |
1427 | else | |
1428 | printf("not supported\n"); | |
a5d85af7 | 1429 | getinfo_super_imsm(st, &info, NULL); |
ae2bfd4e | 1430 | fname_from_uuid(st, &info, nbuf, ':'); |
27fd6274 | 1431 | printf(" UUID : %s\n", nbuf + 5); |
cdddbdbc DW |
1432 | sum = __le32_to_cpu(mpb->check_sum); |
1433 | printf(" Checksum : %08x %s\n", sum, | |
949c47a0 | 1434 | __gen_imsm_checksum(mpb) == sum ? "correct" : "incorrect"); |
87eb16df | 1435 | printf(" MPB Sectors : %d\n", mpb_sectors(mpb)); |
cdddbdbc DW |
1436 | printf(" Disks : %d\n", mpb->num_disks); |
1437 | printf(" RAID Devices : %d\n", mpb->num_raid_devs); | |
0ec1f4e8 | 1438 | print_imsm_disk(__get_imsm_disk(mpb, super->disks->index), super->disks->index, reserved); |
604b746f JD |
1439 | if (super->bbm_log) { |
1440 | struct bbm_log *log = super->bbm_log; | |
1441 | ||
1442 | printf("\n"); | |
1443 | printf("Bad Block Management Log:\n"); | |
1444 | printf(" Log Size : %d\n", __le32_to_cpu(mpb->bbm_log_size)); | |
1445 | printf(" Signature : %x\n", __le32_to_cpu(log->signature)); | |
1446 | printf(" Entry Count : %d\n", __le32_to_cpu(log->entry_count)); | |
1447 | printf(" Spare Blocks : %d\n", __le32_to_cpu(log->reserved_spare_block_count)); | |
13a3b65d N |
1448 | printf(" First Spare : %llx\n", |
1449 | (unsigned long long) __le64_to_cpu(log->first_spare_lba)); | |
604b746f | 1450 | } |
44470971 DW |
1451 | for (i = 0; i < mpb->num_raid_devs; i++) { |
1452 | struct mdinfo info; | |
1453 | struct imsm_dev *dev = __get_imsm_dev(mpb, i); | |
1454 | ||
1455 | super->current_vol = i; | |
a5d85af7 | 1456 | getinfo_super_imsm(st, &info, NULL); |
ae2bfd4e | 1457 | fname_from_uuid(st, &info, nbuf, ':'); |
c47b0ff6 | 1458 | print_imsm_dev(super, dev, nbuf + 5, super->disks->index); |
44470971 | 1459 | } |
cdddbdbc DW |
1460 | for (i = 0; i < mpb->num_disks; i++) { |
1461 | if (i == super->disks->index) | |
1462 | continue; | |
0ec1f4e8 | 1463 | print_imsm_disk(__get_imsm_disk(mpb, i), i, reserved); |
cdddbdbc | 1464 | } |
94827db3 | 1465 | |
0ec1f4e8 DW |
1466 | for (dl = super->disks; dl; dl = dl->next) |
1467 | if (dl->index == -1) | |
1468 | print_imsm_disk(&dl->disk, -1, reserved); | |
520e69e2 AK |
1469 | |
1470 | examine_migr_rec_imsm(super); | |
cdddbdbc DW |
1471 | } |
1472 | ||
061f2c6a | 1473 | static void brief_examine_super_imsm(struct supertype *st, int verbose) |
cdddbdbc | 1474 | { |
27fd6274 | 1475 | /* We just write a generic IMSM ARRAY entry */ |
ff54de6e N |
1476 | struct mdinfo info; |
1477 | char nbuf[64]; | |
1e7bc0ed | 1478 | struct intel_super *super = st->sb; |
1e7bc0ed | 1479 | |
0d5a423f DW |
1480 | if (!super->anchor->num_raid_devs) { |
1481 | printf("ARRAY metadata=imsm\n"); | |
1e7bc0ed | 1482 | return; |
0d5a423f | 1483 | } |
ff54de6e | 1484 | |
a5d85af7 | 1485 | getinfo_super_imsm(st, &info, NULL); |
4737ae25 N |
1486 | fname_from_uuid(st, &info, nbuf, ':'); |
1487 | printf("ARRAY metadata=imsm UUID=%s\n", nbuf + 5); | |
1488 | } | |
1489 | ||
1490 | static void brief_examine_subarrays_imsm(struct supertype *st, int verbose) | |
1491 | { | |
1492 | /* We just write a generic IMSM ARRAY entry */ | |
1493 | struct mdinfo info; | |
1494 | char nbuf[64]; | |
1495 | char nbuf1[64]; | |
1496 | struct intel_super *super = st->sb; | |
1497 | int i; | |
1498 | ||
1499 | if (!super->anchor->num_raid_devs) | |
1500 | return; | |
1501 | ||
a5d85af7 | 1502 | getinfo_super_imsm(st, &info, NULL); |
ae2bfd4e | 1503 | fname_from_uuid(st, &info, nbuf, ':'); |
1e7bc0ed DW |
1504 | for (i = 0; i < super->anchor->num_raid_devs; i++) { |
1505 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
1506 | ||
1507 | super->current_vol = i; | |
a5d85af7 | 1508 | getinfo_super_imsm(st, &info, NULL); |
ae2bfd4e | 1509 | fname_from_uuid(st, &info, nbuf1, ':'); |
1124b3cf | 1510 | printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n", |
cf8de691 | 1511 | dev->volume, nbuf + 5, i, nbuf1 + 5); |
1e7bc0ed | 1512 | } |
cdddbdbc DW |
1513 | } |
1514 | ||
9d84c8ea DW |
1515 | static void export_examine_super_imsm(struct supertype *st) |
1516 | { | |
1517 | struct intel_super *super = st->sb; | |
1518 | struct imsm_super *mpb = super->anchor; | |
1519 | struct mdinfo info; | |
1520 | char nbuf[64]; | |
1521 | ||
a5d85af7 | 1522 | getinfo_super_imsm(st, &info, NULL); |
9d84c8ea DW |
1523 | fname_from_uuid(st, &info, nbuf, ':'); |
1524 | printf("MD_METADATA=imsm\n"); | |
1525 | printf("MD_LEVEL=container\n"); | |
1526 | printf("MD_UUID=%s\n", nbuf+5); | |
1527 | printf("MD_DEVICES=%u\n", mpb->num_disks); | |
1528 | } | |
1529 | ||
74db60b0 N |
1530 | static int copy_metadata_imsm(struct supertype *st, int from, int to) |
1531 | { | |
1532 | /* The second last 512byte sector of the device contains | |
1533 | * the "struct imsm_super" metadata. | |
1534 | * This contains mpb_size which is the size in bytes of the | |
1535 | * extended metadata. This is located immediately before | |
1536 | * the imsm_super. | |
1537 | * We want to read all that, plus the last sector which | |
1538 | * may contain a migration record, and write it all | |
1539 | * to the target. | |
1540 | */ | |
1541 | void *buf; | |
1542 | unsigned long long dsize, offset; | |
1543 | int sectors; | |
1544 | struct imsm_super *sb; | |
1545 | int written = 0; | |
1546 | ||
1547 | if (posix_memalign(&buf, 4096, 4096) != 0) | |
1548 | return 1; | |
1549 | ||
1550 | if (!get_dev_size(from, NULL, &dsize)) | |
1551 | goto err; | |
1552 | ||
1553 | if (lseek64(from, dsize-1024, 0) < 0) | |
1554 | goto err; | |
1555 | if (read(from, buf, 512) != 512) | |
1556 | goto err; | |
1557 | sb = buf; | |
1558 | if (strncmp((char*)sb->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) | |
1559 | goto err; | |
1560 | ||
1561 | sectors = mpb_sectors(sb) + 2; | |
1562 | offset = dsize - sectors * 512; | |
1563 | if (lseek64(from, offset, 0) < 0 || | |
1564 | lseek64(to, offset, 0) < 0) | |
1565 | goto err; | |
1566 | while (written < sectors * 512) { | |
1567 | int n = sectors*512 - written; | |
1568 | if (n > 4096) | |
1569 | n = 4096; | |
1570 | if (read(from, buf, n) != n) | |
1571 | goto err; | |
1572 | if (write(to, buf, n) != n) | |
1573 | goto err; | |
1574 | written += n; | |
1575 | } | |
1576 | free(buf); | |
1577 | return 0; | |
1578 | err: | |
1579 | free(buf); | |
1580 | return 1; | |
1581 | } | |
1582 | ||
cdddbdbc DW |
1583 | static void detail_super_imsm(struct supertype *st, char *homehost) |
1584 | { | |
3ebe00a1 DW |
1585 | struct mdinfo info; |
1586 | char nbuf[64]; | |
1587 | ||
a5d85af7 | 1588 | getinfo_super_imsm(st, &info, NULL); |
ae2bfd4e | 1589 | fname_from_uuid(st, &info, nbuf, ':'); |
3ebe00a1 | 1590 | printf("\n UUID : %s\n", nbuf + 5); |
cdddbdbc DW |
1591 | } |
1592 | ||
1593 | static void brief_detail_super_imsm(struct supertype *st) | |
1594 | { | |
ff54de6e N |
1595 | struct mdinfo info; |
1596 | char nbuf[64]; | |
a5d85af7 | 1597 | getinfo_super_imsm(st, &info, NULL); |
ae2bfd4e | 1598 | fname_from_uuid(st, &info, nbuf, ':'); |
ff54de6e | 1599 | printf(" UUID=%s", nbuf + 5); |
cdddbdbc | 1600 | } |
d665cc31 DW |
1601 | |
1602 | static int imsm_read_serial(int fd, char *devname, __u8 *serial); | |
1603 | static void fd2devname(int fd, char *name); | |
1604 | ||
120dc887 | 1605 | static int ahci_enumerate_ports(const char *hba_path, int port_count, int host_base, int verbose) |
d665cc31 | 1606 | { |
120dc887 LM |
1607 | /* dump an unsorted list of devices attached to AHCI Intel storage |
1608 | * controller, as well as non-connected ports | |
d665cc31 DW |
1609 | */ |
1610 | int hba_len = strlen(hba_path) + 1; | |
1611 | struct dirent *ent; | |
1612 | DIR *dir; | |
1613 | char *path = NULL; | |
1614 | int err = 0; | |
1615 | unsigned long port_mask = (1 << port_count) - 1; | |
1616 | ||
f21e18ca | 1617 | if (port_count > (int)sizeof(port_mask) * 8) { |
ba728be7 | 1618 | if (verbose > 0) |
e7b84f9d | 1619 | pr_err("port_count %d out of range\n", port_count); |
d665cc31 DW |
1620 | return 2; |
1621 | } | |
1622 | ||
1623 | /* scroll through /sys/dev/block looking for devices attached to | |
1624 | * this hba | |
1625 | */ | |
1626 | dir = opendir("/sys/dev/block"); | |
1627 | for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) { | |
1628 | int fd; | |
1629 | char model[64]; | |
1630 | char vendor[64]; | |
1631 | char buf[1024]; | |
1632 | int major, minor; | |
1633 | char *device; | |
1634 | char *c; | |
1635 | int port; | |
1636 | int type; | |
1637 | ||
1638 | if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2) | |
1639 | continue; | |
1640 | path = devt_to_devpath(makedev(major, minor)); | |
1641 | if (!path) | |
1642 | continue; | |
1643 | if (!path_attached_to_hba(path, hba_path)) { | |
1644 | free(path); | |
1645 | path = NULL; | |
1646 | continue; | |
1647 | } | |
1648 | ||
1649 | /* retrieve the scsi device type */ | |
1650 | if (asprintf(&device, "/sys/dev/block/%d:%d/device/xxxxxxx", major, minor) < 0) { | |
ba728be7 | 1651 | if (verbose > 0) |
e7b84f9d | 1652 | pr_err("failed to allocate 'device'\n"); |
d665cc31 DW |
1653 | err = 2; |
1654 | break; | |
1655 | } | |
1656 | sprintf(device, "/sys/dev/block/%d:%d/device/type", major, minor); | |
1657 | if (load_sys(device, buf) != 0) { | |
ba728be7 | 1658 | if (verbose > 0) |
e7b84f9d | 1659 | pr_err("failed to read device type for %s\n", |
d665cc31 DW |
1660 | path); |
1661 | err = 2; | |
1662 | free(device); | |
1663 | break; | |
1664 | } | |
1665 | type = strtoul(buf, NULL, 10); | |
1666 | ||
1667 | /* if it's not a disk print the vendor and model */ | |
1668 | if (!(type == 0 || type == 7 || type == 14)) { | |
1669 | vendor[0] = '\0'; | |
1670 | model[0] = '\0'; | |
1671 | sprintf(device, "/sys/dev/block/%d:%d/device/vendor", major, minor); | |
1672 | if (load_sys(device, buf) == 0) { | |
1673 | strncpy(vendor, buf, sizeof(vendor)); | |
1674 | vendor[sizeof(vendor) - 1] = '\0'; | |
1675 | c = (char *) &vendor[sizeof(vendor) - 1]; | |
1676 | while (isspace(*c) || *c == '\0') | |
1677 | *c-- = '\0'; | |
1678 | ||
1679 | } | |
1680 | sprintf(device, "/sys/dev/block/%d:%d/device/model", major, minor); | |
1681 | if (load_sys(device, buf) == 0) { | |
1682 | strncpy(model, buf, sizeof(model)); | |
1683 | model[sizeof(model) - 1] = '\0'; | |
1684 | c = (char *) &model[sizeof(model) - 1]; | |
1685 | while (isspace(*c) || *c == '\0') | |
1686 | *c-- = '\0'; | |
1687 | } | |
1688 | ||
1689 | if (vendor[0] && model[0]) | |
1690 | sprintf(buf, "%.64s %.64s", vendor, model); | |
1691 | else | |
1692 | switch (type) { /* numbers from hald/linux/device.c */ | |
1693 | case 1: sprintf(buf, "tape"); break; | |
1694 | case 2: sprintf(buf, "printer"); break; | |
1695 | case 3: sprintf(buf, "processor"); break; | |
1696 | case 4: | |
1697 | case 5: sprintf(buf, "cdrom"); break; | |
1698 | case 6: sprintf(buf, "scanner"); break; | |
1699 | case 8: sprintf(buf, "media_changer"); break; | |
1700 | case 9: sprintf(buf, "comm"); break; | |
1701 | case 12: sprintf(buf, "raid"); break; | |
1702 | default: sprintf(buf, "unknown"); | |
1703 | } | |
1704 | } else | |
1705 | buf[0] = '\0'; | |
1706 | free(device); | |
1707 | ||
1708 | /* chop device path to 'host%d' and calculate the port number */ | |
1709 | c = strchr(&path[hba_len], '/'); | |
4e5e717d | 1710 | if (!c) { |
ba728be7 | 1711 | if (verbose > 0) |
e7b84f9d | 1712 | pr_err("%s - invalid path name\n", path + hba_len); |
4e5e717d AW |
1713 | err = 2; |
1714 | break; | |
1715 | } | |
d665cc31 | 1716 | *c = '\0'; |
0858eccf AP |
1717 | if ((sscanf(&path[hba_len], "ata%d", &port) == 1) || |
1718 | ((sscanf(&path[hba_len], "host%d", &port) == 1))) | |
d665cc31 DW |
1719 | port -= host_base; |
1720 | else { | |
ba728be7 | 1721 | if (verbose > 0) { |
d665cc31 | 1722 | *c = '/'; /* repair the full string */ |
e7b84f9d | 1723 | pr_err("failed to determine port number for %s\n", |
d665cc31 DW |
1724 | path); |
1725 | } | |
1726 | err = 2; | |
1727 | break; | |
1728 | } | |
1729 | ||
1730 | /* mark this port as used */ | |
1731 | port_mask &= ~(1 << port); | |
1732 | ||
1733 | /* print out the device information */ | |
1734 | if (buf[0]) { | |
1735 | printf(" Port%d : - non-disk device (%s) -\n", port, buf); | |
1736 | continue; | |
1737 | } | |
1738 | ||
1739 | fd = dev_open(ent->d_name, O_RDONLY); | |
1740 | if (fd < 0) | |
1741 | printf(" Port%d : - disk info unavailable -\n", port); | |
1742 | else { | |
1743 | fd2devname(fd, buf); | |
1744 | printf(" Port%d : %s", port, buf); | |
1745 | if (imsm_read_serial(fd, NULL, (__u8 *) buf) == 0) | |
664d5325 | 1746 | printf(" (%.*s)\n", MAX_RAID_SERIAL_LEN, buf); |
d665cc31 | 1747 | else |
664d5325 | 1748 | printf(" ()\n"); |
4dab422a | 1749 | close(fd); |
d665cc31 | 1750 | } |
d665cc31 DW |
1751 | free(path); |
1752 | path = NULL; | |
1753 | } | |
1754 | if (path) | |
1755 | free(path); | |
1756 | if (dir) | |
1757 | closedir(dir); | |
1758 | if (err == 0) { | |
1759 | int i; | |
1760 | ||
1761 | for (i = 0; i < port_count; i++) | |
1762 | if (port_mask & (1 << i)) | |
1763 | printf(" Port%d : - no device attached -\n", i); | |
1764 | } | |
1765 | ||
1766 | return err; | |
1767 | } | |
1768 | ||
60f0f54d PB |
1769 | static int print_vmd_attached_devs(struct sys_dev *hba) |
1770 | { | |
1771 | struct dirent *ent; | |
1772 | DIR *dir; | |
1773 | char path[292]; | |
1774 | char link[256]; | |
1775 | char *c, *rp; | |
1776 | ||
1777 | if (hba->type != SYS_DEV_VMD) | |
1778 | return 1; | |
1779 | ||
1780 | /* scroll through /sys/dev/block looking for devices attached to | |
1781 | * this hba | |
1782 | */ | |
1783 | dir = opendir("/sys/bus/pci/drivers/nvme"); | |
1784 | for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) { | |
1785 | int n; | |
1786 | ||
1787 | /* is 'ent' a device? check that the 'subsystem' link exists and | |
1788 | * that its target matches 'bus' | |
1789 | */ | |
1790 | sprintf(path, "/sys/bus/pci/drivers/nvme/%s/subsystem", | |
1791 | ent->d_name); | |
1792 | n = readlink(path, link, sizeof(link)); | |
1793 | if (n < 0 || n >= (int)sizeof(link)) | |
1794 | continue; | |
1795 | link[n] = '\0'; | |
1796 | c = strrchr(link, '/'); | |
1797 | if (!c) | |
1798 | continue; | |
1799 | if (strncmp("pci", c+1, strlen("pci")) != 0) | |
1800 | continue; | |
1801 | ||
1802 | sprintf(path, "/sys/bus/pci/drivers/nvme/%s", ent->d_name); | |
1803 | /* if not a intel NVMe - skip it*/ | |
1804 | if (devpath_to_vendor(path) != 0x8086) | |
1805 | continue; | |
1806 | ||
1807 | rp = realpath(path, NULL); | |
1808 | if (!rp) | |
1809 | continue; | |
1810 | ||
1811 | if (path_attached_to_hba(rp, hba->path)) { | |
1812 | printf(" NVMe under VMD : %s\n", rp); | |
1813 | } | |
1814 | free(rp); | |
1815 | } | |
1816 | ||
1817 | return 0; | |
1818 | } | |
1819 | ||
120dc887 LM |
1820 | static void print_found_intel_controllers(struct sys_dev *elem) |
1821 | { | |
1822 | for (; elem; elem = elem->next) { | |
e7b84f9d | 1823 | pr_err("found Intel(R) "); |
120dc887 LM |
1824 | if (elem->type == SYS_DEV_SATA) |
1825 | fprintf(stderr, "SATA "); | |
155cbb4c LM |
1826 | else if (elem->type == SYS_DEV_SAS) |
1827 | fprintf(stderr, "SAS "); | |
0858eccf AP |
1828 | else if (elem->type == SYS_DEV_NVME) |
1829 | fprintf(stderr, "NVMe "); | |
60f0f54d PB |
1830 | |
1831 | if (elem->type == SYS_DEV_VMD) | |
1832 | fprintf(stderr, "VMD domain"); | |
1833 | else | |
1834 | fprintf(stderr, "RAID controller"); | |
1835 | ||
120dc887 LM |
1836 | if (elem->pci_id) |
1837 | fprintf(stderr, " at %s", elem->pci_id); | |
1838 | fprintf(stderr, ".\n"); | |
1839 | } | |
1840 | fflush(stderr); | |
1841 | } | |
1842 | ||
120dc887 LM |
1843 | static int ahci_get_port_count(const char *hba_path, int *port_count) |
1844 | { | |
1845 | struct dirent *ent; | |
1846 | DIR *dir; | |
1847 | int host_base = -1; | |
1848 | ||
1849 | *port_count = 0; | |
1850 | if ((dir = opendir(hba_path)) == NULL) | |
1851 | return -1; | |
1852 | ||
1853 | for (ent = readdir(dir); ent; ent = readdir(dir)) { | |
1854 | int host; | |
1855 | ||
0858eccf AP |
1856 | if ((sscanf(ent->d_name, "ata%d", &host) != 1) && |
1857 | ((sscanf(ent->d_name, "host%d", &host) != 1))) | |
120dc887 LM |
1858 | continue; |
1859 | if (*port_count == 0) | |
1860 | host_base = host; | |
1861 | else if (host < host_base) | |
1862 | host_base = host; | |
1863 | ||
1864 | if (host + 1 > *port_count + host_base) | |
1865 | *port_count = host + 1 - host_base; | |
1866 | } | |
1867 | closedir(dir); | |
1868 | return host_base; | |
1869 | } | |
1870 | ||
a891a3c2 LM |
1871 | static void print_imsm_capability(const struct imsm_orom *orom) |
1872 | { | |
0858eccf AP |
1873 | printf(" Platform : Intel(R) "); |
1874 | if (orom->capabilities == 0 && orom->driver_features == 0) | |
1875 | printf("Matrix Storage Manager\n"); | |
1876 | else | |
1877 | printf("Rapid Storage Technology%s\n", | |
1878 | imsm_orom_is_enterprise(orom) ? " enterprise" : ""); | |
1879 | if (orom->major_ver || orom->minor_ver || orom->hotfix_ver || orom->build) | |
1880 | printf(" Version : %d.%d.%d.%d\n", orom->major_ver, | |
1881 | orom->minor_ver, orom->hotfix_ver, orom->build); | |
a891a3c2 LM |
1882 | printf(" RAID Levels :%s%s%s%s%s\n", |
1883 | imsm_orom_has_raid0(orom) ? " raid0" : "", | |
1884 | imsm_orom_has_raid1(orom) ? " raid1" : "", | |
1885 | imsm_orom_has_raid1e(orom) ? " raid1e" : "", | |
1886 | imsm_orom_has_raid10(orom) ? " raid10" : "", | |
1887 | imsm_orom_has_raid5(orom) ? " raid5" : ""); | |
1888 | printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n", | |
1889 | imsm_orom_has_chunk(orom, 2) ? " 2k" : "", | |
1890 | imsm_orom_has_chunk(orom, 4) ? " 4k" : "", | |
1891 | imsm_orom_has_chunk(orom, 8) ? " 8k" : "", | |
1892 | imsm_orom_has_chunk(orom, 16) ? " 16k" : "", | |
1893 | imsm_orom_has_chunk(orom, 32) ? " 32k" : "", | |
1894 | imsm_orom_has_chunk(orom, 64) ? " 64k" : "", | |
1895 | imsm_orom_has_chunk(orom, 128) ? " 128k" : "", | |
1896 | imsm_orom_has_chunk(orom, 256) ? " 256k" : "", | |
1897 | imsm_orom_has_chunk(orom, 512) ? " 512k" : "", | |
1898 | imsm_orom_has_chunk(orom, 1024*1) ? " 1M" : "", | |
1899 | imsm_orom_has_chunk(orom, 1024*2) ? " 2M" : "", | |
1900 | imsm_orom_has_chunk(orom, 1024*4) ? " 4M" : "", | |
1901 | imsm_orom_has_chunk(orom, 1024*8) ? " 8M" : "", | |
1902 | imsm_orom_has_chunk(orom, 1024*16) ? " 16M" : "", | |
1903 | imsm_orom_has_chunk(orom, 1024*32) ? " 32M" : "", | |
1904 | imsm_orom_has_chunk(orom, 1024*64) ? " 64M" : ""); | |
29cd0821 CA |
1905 | printf(" 2TB volumes :%s supported\n", |
1906 | (orom->attr & IMSM_OROM_ATTR_2TB)?"":" not"); | |
1907 | printf(" 2TB disks :%s supported\n", | |
1908 | (orom->attr & IMSM_OROM_ATTR_2TB_DISK)?"":" not"); | |
0e7f69a8 | 1909 | printf(" Max Disks : %d\n", orom->tds); |
0858eccf AP |
1910 | printf(" Max Volumes : %d per array, %d per %s\n", |
1911 | orom->vpa, orom->vphba, | |
1912 | imsm_orom_is_nvme(orom) ? "platform" : "controller"); | |
a891a3c2 LM |
1913 | return; |
1914 | } | |
1915 | ||
e50cf220 MN |
1916 | static void print_imsm_capability_export(const struct imsm_orom *orom) |
1917 | { | |
1918 | printf("MD_FIRMWARE_TYPE=imsm\n"); | |
0858eccf AP |
1919 | if (orom->major_ver || orom->minor_ver || orom->hotfix_ver || orom->build) |
1920 | printf("IMSM_VERSION=%d.%d.%d.%d\n", orom->major_ver, orom->minor_ver, | |
1921 | orom->hotfix_ver, orom->build); | |
e50cf220 MN |
1922 | printf("IMSM_SUPPORTED_RAID_LEVELS=%s%s%s%s%s\n", |
1923 | imsm_orom_has_raid0(orom) ? "raid0 " : "", | |
1924 | imsm_orom_has_raid1(orom) ? "raid1 " : "", | |
1925 | imsm_orom_has_raid1e(orom) ? "raid1e " : "", | |
1926 | imsm_orom_has_raid5(orom) ? "raid10 " : "", | |
1927 | imsm_orom_has_raid10(orom) ? "raid5 " : ""); | |
1928 | printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n", | |
1929 | imsm_orom_has_chunk(orom, 2) ? "2k " : "", | |
1930 | imsm_orom_has_chunk(orom, 4) ? "4k " : "", | |
1931 | imsm_orom_has_chunk(orom, 8) ? "8k " : "", | |
1932 | imsm_orom_has_chunk(orom, 16) ? "16k " : "", | |
1933 | imsm_orom_has_chunk(orom, 32) ? "32k " : "", | |
1934 | imsm_orom_has_chunk(orom, 64) ? "64k " : "", | |
1935 | imsm_orom_has_chunk(orom, 128) ? "128k " : "", | |
1936 | imsm_orom_has_chunk(orom, 256) ? "256k " : "", | |
1937 | imsm_orom_has_chunk(orom, 512) ? "512k " : "", | |
1938 | imsm_orom_has_chunk(orom, 1024*1) ? "1M " : "", | |
1939 | imsm_orom_has_chunk(orom, 1024*2) ? "2M " : "", | |
1940 | imsm_orom_has_chunk(orom, 1024*4) ? "4M " : "", | |
1941 | imsm_orom_has_chunk(orom, 1024*8) ? "8M " : "", | |
1942 | imsm_orom_has_chunk(orom, 1024*16) ? "16M " : "", | |
1943 | imsm_orom_has_chunk(orom, 1024*32) ? "32M " : "", | |
1944 | imsm_orom_has_chunk(orom, 1024*64) ? "64M " : ""); | |
1945 | printf("IMSM_2TB_VOLUMES=%s\n",(orom->attr & IMSM_OROM_ATTR_2TB) ? "yes" : "no"); | |
1946 | printf("IMSM_2TB_DISKS=%s\n",(orom->attr & IMSM_OROM_ATTR_2TB_DISK) ? "yes" : "no"); | |
1947 | printf("IMSM_MAX_DISKS=%d\n",orom->tds); | |
1948 | printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom->vpa); | |
1949 | printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom->vphba); | |
1950 | } | |
1951 | ||
9eafa1de | 1952 | static int detail_platform_imsm(int verbose, int enumerate_only, char *controller_path) |
d665cc31 DW |
1953 | { |
1954 | /* There are two components to imsm platform support, the ahci SATA | |
1955 | * controller and the option-rom. To find the SATA controller we | |
1956 | * simply look in /sys/bus/pci/drivers/ahci to see if an ahci | |
1957 | * controller with the Intel vendor id is present. This approach | |
1958 | * allows mdadm to leverage the kernel's ahci detection logic, with the | |
1959 | * caveat that if ahci.ko is not loaded mdadm will not be able to | |
1960 | * detect platform raid capabilities. The option-rom resides in a | |
1961 | * platform "Adapter ROM". We scan for its signature to retrieve the | |
1962 | * platform capabilities. If raid support is disabled in the BIOS the | |
1963 | * option-rom capability structure will not be available. | |
1964 | */ | |
d665cc31 | 1965 | struct sys_dev *list, *hba; |
d665cc31 DW |
1966 | int host_base = 0; |
1967 | int port_count = 0; | |
9eafa1de | 1968 | int result=1; |
d665cc31 | 1969 | |
5615172f | 1970 | if (enumerate_only) { |
a891a3c2 | 1971 | if (check_env("IMSM_NO_PLATFORM")) |
5615172f | 1972 | return 0; |
a891a3c2 LM |
1973 | list = find_intel_devices(); |
1974 | if (!list) | |
1975 | return 2; | |
1976 | for (hba = list; hba; hba = hba->next) { | |
6b781d33 AP |
1977 | if (find_imsm_capability(hba)) { |
1978 | result = 0; | |
a891a3c2 LM |
1979 | break; |
1980 | } | |
9eafa1de | 1981 | else |
6b781d33 | 1982 | result = 2; |
a891a3c2 | 1983 | } |
a891a3c2 | 1984 | return result; |
5615172f DW |
1985 | } |
1986 | ||
155cbb4c LM |
1987 | list = find_intel_devices(); |
1988 | if (!list) { | |
ba728be7 | 1989 | if (verbose > 0) |
7a862a02 | 1990 | pr_err("no active Intel(R) RAID controller found.\n"); |
d665cc31 | 1991 | return 2; |
ba728be7 | 1992 | } else if (verbose > 0) |
155cbb4c | 1993 | print_found_intel_controllers(list); |
d665cc31 | 1994 | |
a891a3c2 | 1995 | for (hba = list; hba; hba = hba->next) { |
0858eccf | 1996 | if (controller_path && (compare_paths(hba->path, controller_path) != 0)) |
9eafa1de | 1997 | continue; |
0858eccf | 1998 | if (!find_imsm_capability(hba)) { |
60f0f54d | 1999 | char buf[PATH_MAX]; |
e7b84f9d | 2000 | pr_err("imsm capabilities not found for controller: %s (type %s)\n", |
60f0f54d PB |
2001 | hba->type == SYS_DEV_VMD ? vmd_domain_to_controller(hba, buf) : hba->path, |
2002 | get_sys_dev_type(hba->type)); | |
0858eccf AP |
2003 | continue; |
2004 | } | |
2005 | result = 0; | |
2006 | } | |
2007 | ||
2008 | if (controller_path && result == 1) { | |
2009 | pr_err("no active Intel(R) RAID controller found under %s\n", | |
2010 | controller_path); | |
2011 | return result; | |
2012 | } | |
2013 | ||
5e1d6128 | 2014 | const struct orom_entry *entry; |
0858eccf | 2015 | |
5e1d6128 | 2016 | for (entry = orom_entries; entry; entry = entry->next) { |
60f0f54d PB |
2017 | if (entry->type == SYS_DEV_VMD) { |
2018 | for (hba = list; hba; hba = hba->next) { | |
2019 | if (hba->type == SYS_DEV_VMD) { | |
2020 | char buf[PATH_MAX]; | |
2021 | print_imsm_capability(&entry->orom); | |
2022 | printf(" I/O Controller : %s (%s)\n", | |
2023 | vmd_domain_to_controller(hba, buf), get_sys_dev_type(hba->type)); | |
2024 | print_vmd_attached_devs(hba); | |
2025 | printf("\n"); | |
2026 | } | |
2027 | } | |
2028 | continue; | |
2029 | } | |
0858eccf | 2030 | |
60f0f54d PB |
2031 | print_imsm_capability(&entry->orom); |
2032 | if (entry->type == SYS_DEV_NVME) { | |
0858eccf AP |
2033 | for (hba = list; hba; hba = hba->next) { |
2034 | if (hba->type == SYS_DEV_NVME) | |
2035 | printf(" NVMe Device : %s\n", hba->path); | |
2036 | } | |
60f0f54d | 2037 | printf("\n"); |
0858eccf AP |
2038 | continue; |
2039 | } | |
2040 | ||
2041 | struct devid_list *devid; | |
5e1d6128 | 2042 | for (devid = entry->devid_list; devid; devid = devid->next) { |
0858eccf AP |
2043 | hba = device_by_id(devid->devid); |
2044 | if (!hba) | |
2045 | continue; | |
2046 | ||
9eafa1de MN |
2047 | printf(" I/O Controller : %s (%s)\n", |
2048 | hba->path, get_sys_dev_type(hba->type)); | |
2049 | if (hba->type == SYS_DEV_SATA) { | |
2050 | host_base = ahci_get_port_count(hba->path, &port_count); | |
2051 | if (ahci_enumerate_ports(hba->path, port_count, host_base, verbose)) { | |
2052 | if (verbose > 0) | |
7a862a02 | 2053 | pr_err("failed to enumerate ports on SATA controller at %s.\n", hba->pci_id); |
9eafa1de MN |
2054 | result |= 2; |
2055 | } | |
120dc887 LM |
2056 | } |
2057 | } | |
0858eccf | 2058 | printf("\n"); |
d665cc31 | 2059 | } |
155cbb4c | 2060 | |
120dc887 | 2061 | return result; |
d665cc31 | 2062 | } |
e50cf220 | 2063 | |
9eafa1de | 2064 | static int export_detail_platform_imsm(int verbose, char *controller_path) |
e50cf220 | 2065 | { |
e50cf220 MN |
2066 | struct sys_dev *list, *hba; |
2067 | int result=1; | |
2068 | ||
2069 | list = find_intel_devices(); | |
2070 | if (!list) { | |
2071 | if (verbose > 0) | |
2072 | pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n"); | |
2073 | result = 2; | |
e50cf220 MN |
2074 | return result; |
2075 | } | |
2076 | ||
2077 | for (hba = list; hba; hba = hba->next) { | |
9eafa1de MN |
2078 | if (controller_path && (compare_paths(hba->path,controller_path) != 0)) |
2079 | continue; | |
60f0f54d PB |
2080 | if (!find_imsm_capability(hba) && verbose > 0) { |
2081 | char buf[PATH_MAX]; | |
2082 | pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n", | |
2083 | hba->type == SYS_DEV_VMD ? vmd_domain_to_controller(hba, buf) : hba->path); | |
2084 | } | |
0858eccf | 2085 | else |
e50cf220 | 2086 | result = 0; |
e50cf220 MN |
2087 | } |
2088 | ||
5e1d6128 | 2089 | const struct orom_entry *entry; |
0858eccf | 2090 | |
60f0f54d PB |
2091 | for (entry = orom_entries; entry; entry = entry->next) { |
2092 | if (entry->type == SYS_DEV_VMD) { | |
2093 | for (hba = list; hba; hba = hba->next) | |
2094 | print_imsm_capability_export(&entry->orom); | |
2095 | continue; | |
2096 | } | |
5e1d6128 | 2097 | print_imsm_capability_export(&entry->orom); |
60f0f54d | 2098 | } |
0858eccf | 2099 | |
e50cf220 MN |
2100 | return result; |
2101 | } | |
2102 | ||
cdddbdbc DW |
2103 | #endif |
2104 | ||
2105 | static int match_home_imsm(struct supertype *st, char *homehost) | |
2106 | { | |
5115ca67 DW |
2107 | /* the imsm metadata format does not specify any host |
2108 | * identification information. We return -1 since we can never | |
2109 | * confirm nor deny whether a given array is "meant" for this | |
148acb7b | 2110 | * host. We rely on compare_super and the 'family_num' fields to |
5115ca67 DW |
2111 | * exclude member disks that do not belong, and we rely on |
2112 | * mdadm.conf to specify the arrays that should be assembled. | |
2113 | * Auto-assembly may still pick up "foreign" arrays. | |
2114 | */ | |
cdddbdbc | 2115 | |
9362c1c8 | 2116 | return -1; |
cdddbdbc DW |
2117 | } |
2118 | ||
2119 | static void uuid_from_super_imsm(struct supertype *st, int uuid[4]) | |
2120 | { | |
51006d85 N |
2121 | /* The uuid returned here is used for: |
2122 | * uuid to put into bitmap file (Create, Grow) | |
2123 | * uuid for backup header when saving critical section (Grow) | |
2124 | * comparing uuids when re-adding a device into an array | |
2125 | * In these cases the uuid required is that of the data-array, | |
2126 | * not the device-set. | |
2127 | * uuid to recognise same set when adding a missing device back | |
2128 | * to an array. This is a uuid for the device-set. | |
1011e834 | 2129 | * |
51006d85 N |
2130 | * For each of these we can make do with a truncated |
2131 | * or hashed uuid rather than the original, as long as | |
2132 | * everyone agrees. | |
2133 | * In each case the uuid required is that of the data-array, | |
2134 | * not the device-set. | |
43dad3d6 | 2135 | */ |
51006d85 N |
2136 | /* imsm does not track uuid's so we synthesis one using sha1 on |
2137 | * - The signature (Which is constant for all imsm array, but no matter) | |
148acb7b | 2138 | * - the orig_family_num of the container |
51006d85 N |
2139 | * - the index number of the volume |
2140 | * - the 'serial' number of the volume. | |
2141 | * Hopefully these are all constant. | |
2142 | */ | |
2143 | struct intel_super *super = st->sb; | |
43dad3d6 | 2144 | |
51006d85 N |
2145 | char buf[20]; |
2146 | struct sha1_ctx ctx; | |
2147 | struct imsm_dev *dev = NULL; | |
148acb7b | 2148 | __u32 family_num; |
51006d85 | 2149 | |
148acb7b DW |
2150 | /* some mdadm versions failed to set ->orig_family_num, in which |
2151 | * case fall back to ->family_num. orig_family_num will be | |
2152 | * fixed up with the first metadata update. | |
2153 | */ | |
2154 | family_num = super->anchor->orig_family_num; | |
2155 | if (family_num == 0) | |
2156 | family_num = super->anchor->family_num; | |
51006d85 | 2157 | sha1_init_ctx(&ctx); |
92bd8f8d | 2158 | sha1_process_bytes(super->anchor->sig, MPB_SIG_LEN, &ctx); |
148acb7b | 2159 | sha1_process_bytes(&family_num, sizeof(__u32), &ctx); |
51006d85 N |
2160 | if (super->current_vol >= 0) |
2161 | dev = get_imsm_dev(super, super->current_vol); | |
2162 | if (dev) { | |
2163 | __u32 vol = super->current_vol; | |
2164 | sha1_process_bytes(&vol, sizeof(vol), &ctx); | |
2165 | sha1_process_bytes(dev->volume, MAX_RAID_SERIAL_LEN, &ctx); | |
2166 | } | |
2167 | sha1_finish_ctx(&ctx, buf); | |
2168 | memcpy(uuid, buf, 4*4); | |
cdddbdbc DW |
2169 | } |
2170 | ||
0d481d37 | 2171 | #if 0 |
4f5bc454 DW |
2172 | static void |
2173 | get_imsm_numerical_version(struct imsm_super *mpb, int *m, int *p) | |
cdddbdbc | 2174 | { |
cdddbdbc DW |
2175 | __u8 *v = get_imsm_version(mpb); |
2176 | __u8 *end = mpb->sig + MAX_SIGNATURE_LENGTH; | |
2177 | char major[] = { 0, 0, 0 }; | |
2178 | char minor[] = { 0 ,0, 0 }; | |
2179 | char patch[] = { 0, 0, 0 }; | |
2180 | char *ver_parse[] = { major, minor, patch }; | |
2181 | int i, j; | |
2182 | ||
2183 | i = j = 0; | |
2184 | while (*v != '\0' && v < end) { | |
2185 | if (*v != '.' && j < 2) | |
2186 | ver_parse[i][j++] = *v; | |
2187 | else { | |
2188 | i++; | |
2189 | j = 0; | |
2190 | } | |
2191 | v++; | |
2192 | } | |
2193 | ||
4f5bc454 DW |
2194 | *m = strtol(minor, NULL, 0); |
2195 | *p = strtol(patch, NULL, 0); | |
2196 | } | |
0d481d37 | 2197 | #endif |
4f5bc454 | 2198 | |
1e5c6983 DW |
2199 | static __u32 migr_strip_blocks_resync(struct imsm_dev *dev) |
2200 | { | |
2201 | /* migr_strip_size when repairing or initializing parity */ | |
238c0a71 | 2202 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
1e5c6983 DW |
2203 | __u32 chunk = __le32_to_cpu(map->blocks_per_strip); |
2204 | ||
2205 | switch (get_imsm_raid_level(map)) { | |
2206 | case 5: | |
2207 | case 10: | |
2208 | return chunk; | |
2209 | default: | |
2210 | return 128*1024 >> 9; | |
2211 | } | |
2212 | } | |
2213 | ||
2214 | static __u32 migr_strip_blocks_rebuild(struct imsm_dev *dev) | |
2215 | { | |
2216 | /* migr_strip_size when rebuilding a degraded disk, no idea why | |
2217 | * this is different than migr_strip_size_resync(), but it's good | |
2218 | * to be compatible | |
2219 | */ | |
238c0a71 | 2220 | struct imsm_map *map = get_imsm_map(dev, MAP_1); |
1e5c6983 DW |
2221 | __u32 chunk = __le32_to_cpu(map->blocks_per_strip); |
2222 | ||
2223 | switch (get_imsm_raid_level(map)) { | |
2224 | case 1: | |
2225 | case 10: | |
2226 | if (map->num_members % map->num_domains == 0) | |
2227 | return 128*1024 >> 9; | |
2228 | else | |
2229 | return chunk; | |
2230 | case 5: | |
2231 | return max((__u32) 64*1024 >> 9, chunk); | |
2232 | default: | |
2233 | return 128*1024 >> 9; | |
2234 | } | |
2235 | } | |
2236 | ||
2237 | static __u32 num_stripes_per_unit_resync(struct imsm_dev *dev) | |
2238 | { | |
238c0a71 AK |
2239 | struct imsm_map *lo = get_imsm_map(dev, MAP_0); |
2240 | struct imsm_map *hi = get_imsm_map(dev, MAP_1); | |
1e5c6983 DW |
2241 | __u32 lo_chunk = __le32_to_cpu(lo->blocks_per_strip); |
2242 | __u32 hi_chunk = __le32_to_cpu(hi->blocks_per_strip); | |
2243 | ||
2244 | return max((__u32) 1, hi_chunk / lo_chunk); | |
2245 | } | |
2246 | ||
2247 | static __u32 num_stripes_per_unit_rebuild(struct imsm_dev *dev) | |
2248 | { | |
238c0a71 | 2249 | struct imsm_map *lo = get_imsm_map(dev, MAP_0); |
1e5c6983 DW |
2250 | int level = get_imsm_raid_level(lo); |
2251 | ||
2252 | if (level == 1 || level == 10) { | |
238c0a71 | 2253 | struct imsm_map *hi = get_imsm_map(dev, MAP_1); |
1e5c6983 DW |
2254 | |
2255 | return hi->num_domains; | |
2256 | } else | |
2257 | return num_stripes_per_unit_resync(dev); | |
2258 | } | |
2259 | ||
98130f40 | 2260 | static __u8 imsm_num_data_members(struct imsm_dev *dev, int second_map) |
1e5c6983 DW |
2261 | { |
2262 | /* named 'imsm_' because raid0, raid1 and raid10 | |
2263 | * counter-intuitively have the same number of data disks | |
2264 | */ | |
98130f40 | 2265 | struct imsm_map *map = get_imsm_map(dev, second_map); |
1e5c6983 DW |
2266 | |
2267 | switch (get_imsm_raid_level(map)) { | |
2268 | case 0: | |
36fd8ccc AK |
2269 | return map->num_members; |
2270 | break; | |
1e5c6983 DW |
2271 | case 1: |
2272 | case 10: | |
36fd8ccc | 2273 | return map->num_members/2; |
1e5c6983 DW |
2274 | case 5: |
2275 | return map->num_members - 1; | |
2276 | default: | |
1ade5cc1 | 2277 | dprintf("unsupported raid level\n"); |
1e5c6983 DW |
2278 | return 0; |
2279 | } | |
2280 | } | |
2281 | ||
2282 | static __u32 parity_segment_depth(struct imsm_dev *dev) | |
2283 | { | |
238c0a71 | 2284 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
1e5c6983 DW |
2285 | __u32 chunk = __le32_to_cpu(map->blocks_per_strip); |
2286 | ||
2287 | switch(get_imsm_raid_level(map)) { | |
2288 | case 1: | |
2289 | case 10: | |
2290 | return chunk * map->num_domains; | |
2291 | case 5: | |
2292 | return chunk * map->num_members; | |
2293 | default: | |
2294 | return chunk; | |
2295 | } | |
2296 | } | |
2297 | ||
2298 | static __u32 map_migr_block(struct imsm_dev *dev, __u32 block) | |
2299 | { | |
238c0a71 | 2300 | struct imsm_map *map = get_imsm_map(dev, MAP_1); |
1e5c6983 DW |
2301 | __u32 chunk = __le32_to_cpu(map->blocks_per_strip); |
2302 | __u32 strip = block / chunk; | |
2303 | ||
2304 | switch (get_imsm_raid_level(map)) { | |
2305 | case 1: | |
2306 | case 10: { | |
2307 | __u32 vol_strip = (strip * map->num_domains) + 1; | |
2308 | __u32 vol_stripe = vol_strip / map->num_members; | |
2309 | ||
2310 | return vol_stripe * chunk + block % chunk; | |
2311 | } case 5: { | |
2312 | __u32 stripe = strip / (map->num_members - 1); | |
2313 | ||
2314 | return stripe * chunk + block % chunk; | |
2315 | } | |
2316 | default: | |
2317 | return 0; | |
2318 | } | |
2319 | } | |
2320 | ||
c47b0ff6 AK |
2321 | static __u64 blocks_per_migr_unit(struct intel_super *super, |
2322 | struct imsm_dev *dev) | |
1e5c6983 DW |
2323 | { |
2324 | /* calculate the conversion factor between per member 'blocks' | |
2325 | * (md/{resync,rebuild}_start) and imsm migration units, return | |
2326 | * 0 for the 'not migrating' and 'unsupported migration' cases | |
2327 | */ | |
2328 | if (!dev->vol.migr_state) | |
2329 | return 0; | |
2330 | ||
2331 | switch (migr_type(dev)) { | |
c47b0ff6 AK |
2332 | case MIGR_GEN_MIGR: { |
2333 | struct migr_record *migr_rec = super->migr_rec; | |
2334 | return __le32_to_cpu(migr_rec->blocks_per_unit); | |
2335 | } | |
1e5c6983 DW |
2336 | case MIGR_VERIFY: |
2337 | case MIGR_REPAIR: | |
2338 | case MIGR_INIT: { | |
238c0a71 | 2339 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
1e5c6983 DW |
2340 | __u32 stripes_per_unit; |
2341 | __u32 blocks_per_unit; | |
2342 | __u32 parity_depth; | |
2343 | __u32 migr_chunk; | |
2344 | __u32 block_map; | |
2345 | __u32 block_rel; | |
2346 | __u32 segment; | |
2347 | __u32 stripe; | |
2348 | __u8 disks; | |
2349 | ||
2350 | /* yes, this is really the translation of migr_units to | |
2351 | * per-member blocks in the 'resync' case | |
2352 | */ | |
2353 | stripes_per_unit = num_stripes_per_unit_resync(dev); | |
2354 | migr_chunk = migr_strip_blocks_resync(dev); | |
238c0a71 | 2355 | disks = imsm_num_data_members(dev, MAP_0); |
1e5c6983 | 2356 | blocks_per_unit = stripes_per_unit * migr_chunk * disks; |
7b1ab482 | 2357 | stripe = __le16_to_cpu(map->blocks_per_strip) * disks; |
1e5c6983 DW |
2358 | segment = blocks_per_unit / stripe; |
2359 | block_rel = blocks_per_unit - segment * stripe; | |
2360 | parity_depth = parity_segment_depth(dev); | |
2361 | block_map = map_migr_block(dev, block_rel); | |
2362 | return block_map + parity_depth * segment; | |
2363 | } | |
2364 | case MIGR_REBUILD: { | |
2365 | __u32 stripes_per_unit; | |
2366 | __u32 migr_chunk; | |
2367 | ||
2368 | stripes_per_unit = num_stripes_per_unit_rebuild(dev); | |
2369 | migr_chunk = migr_strip_blocks_rebuild(dev); | |
2370 | return migr_chunk * stripes_per_unit; | |
2371 | } | |
1e5c6983 DW |
2372 | case MIGR_STATE_CHANGE: |
2373 | default: | |
2374 | return 0; | |
2375 | } | |
2376 | } | |
2377 | ||
c2c087e6 DW |
2378 | static int imsm_level_to_layout(int level) |
2379 | { | |
2380 | switch (level) { | |
2381 | case 0: | |
2382 | case 1: | |
2383 | return 0; | |
2384 | case 5: | |
2385 | case 6: | |
a380c027 | 2386 | return ALGORITHM_LEFT_ASYMMETRIC; |
c2c087e6 | 2387 | case 10: |
c92a2527 | 2388 | return 0x102; |
c2c087e6 | 2389 | } |
a18a888e | 2390 | return UnSet; |
c2c087e6 DW |
2391 | } |
2392 | ||
8e59f3d8 AK |
2393 | /******************************************************************************* |
2394 | * Function: read_imsm_migr_rec | |
2395 | * Description: Function reads imsm migration record from last sector of disk | |
2396 | * Parameters: | |
2397 | * fd : disk descriptor | |
2398 | * super : metadata info | |
2399 | * Returns: | |
2400 | * 0 : success, | |
2401 | * -1 : fail | |
2402 | ******************************************************************************/ | |
2403 | static int read_imsm_migr_rec(int fd, struct intel_super *super) | |
2404 | { | |
2405 | int ret_val = -1; | |
2406 | unsigned long long dsize; | |
2407 | ||
2408 | get_dev_size(fd, NULL, &dsize); | |
17a4eaf9 | 2409 | if (lseek64(fd, dsize - MIGR_REC_POSITION, SEEK_SET) < 0) { |
e7b84f9d N |
2410 | pr_err("Cannot seek to anchor block: %s\n", |
2411 | strerror(errno)); | |
8e59f3d8 AK |
2412 | goto out; |
2413 | } | |
17a4eaf9 AK |
2414 | if (read(fd, super->migr_rec_buf, MIGR_REC_BUF_SIZE) != |
2415 | MIGR_REC_BUF_SIZE) { | |
e7b84f9d N |
2416 | pr_err("Cannot read migr record block: %s\n", |
2417 | strerror(errno)); | |
8e59f3d8 AK |
2418 | goto out; |
2419 | } | |
2420 | ret_val = 0; | |
2421 | ||
2422 | out: | |
2423 | return ret_val; | |
2424 | } | |
2425 | ||
3136abe5 AK |
2426 | static struct imsm_dev *imsm_get_device_during_migration( |
2427 | struct intel_super *super) | |
2428 | { | |
2429 | ||
2430 | struct intel_dev *dv; | |
2431 | ||
2432 | for (dv = super->devlist; dv; dv = dv->next) { | |
2433 | if (is_gen_migration(dv->dev)) | |
2434 | return dv->dev; | |
2435 | } | |
2436 | return NULL; | |
2437 | } | |
2438 | ||
8e59f3d8 AK |
2439 | /******************************************************************************* |
2440 | * Function: load_imsm_migr_rec | |
2441 | * Description: Function reads imsm migration record (it is stored at the last | |
2442 | * sector of disk) | |
2443 | * Parameters: | |
2444 | * super : imsm internal array info | |
2445 | * info : general array info | |
2446 | * Returns: | |
2447 | * 0 : success | |
2448 | * -1 : fail | |
4c965cc9 | 2449 | * -2 : no migration in progress |
8e59f3d8 AK |
2450 | ******************************************************************************/ |
2451 | static int load_imsm_migr_rec(struct intel_super *super, struct mdinfo *info) | |
2452 | { | |
2453 | struct mdinfo *sd; | |
2454 | struct dl *dl = NULL; | |
2455 | char nm[30]; | |
2456 | int retval = -1; | |
2457 | int fd = -1; | |
3136abe5 AK |
2458 | struct imsm_dev *dev; |
2459 | struct imsm_map *map = NULL; | |
b4ab44d8 | 2460 | int slot = -1; |
3136abe5 AK |
2461 | |
2462 | /* find map under migration */ | |
2463 | dev = imsm_get_device_during_migration(super); | |
2464 | /* nothing to load,no migration in progress? | |
2465 | */ | |
2466 | if (dev == NULL) | |
4c965cc9 | 2467 | return -2; |
44bfe6df | 2468 | map = get_imsm_map(dev, MAP_0); |
8e59f3d8 AK |
2469 | |
2470 | if (info) { | |
2471 | for (sd = info->devs ; sd ; sd = sd->next) { | |
3136abe5 AK |
2472 | /* skip spare and failed disks |
2473 | */ | |
2474 | if (sd->disk.raid_disk < 0) | |
2475 | continue; | |
8e59f3d8 | 2476 | /* read only from one of the first two slots */ |
3136abe5 AK |
2477 | if (map) |
2478 | slot = get_imsm_disk_slot(map, | |
2479 | sd->disk.raid_disk); | |
2480 | if ((map == NULL) || (slot > 1) || (slot < 0)) | |
8e59f3d8 | 2481 | continue; |
3136abe5 | 2482 | |
8e59f3d8 AK |
2483 | sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor); |
2484 | fd = dev_open(nm, O_RDONLY); | |
2485 | if (fd >= 0) | |
2486 | break; | |
2487 | } | |
2488 | } | |
2489 | if (fd < 0) { | |
2490 | for (dl = super->disks; dl; dl = dl->next) { | |
3136abe5 AK |
2491 | /* skip spare and failed disks |
2492 | */ | |
2493 | if (dl->index < 0) | |
2494 | continue; | |
8e59f3d8 | 2495 | /* read only from one of the first two slots */ |
3136abe5 AK |
2496 | if (map) |
2497 | slot = get_imsm_disk_slot(map, dl->index); | |
2498 | if ((map == NULL) || (slot > 1) || (slot < 0)) | |
8e59f3d8 AK |
2499 | continue; |
2500 | sprintf(nm, "%d:%d", dl->major, dl->minor); | |
2501 | fd = dev_open(nm, O_RDONLY); | |
2502 | if (fd >= 0) | |
2503 | break; | |
2504 | } | |
2505 | } | |
2506 | if (fd < 0) | |
2507 | goto out; | |
2508 | retval = read_imsm_migr_rec(fd, super); | |
2509 | ||
2510 | out: | |
2511 | if (fd >= 0) | |
2512 | close(fd); | |
2513 | return retval; | |
2514 | } | |
2515 | ||
9e2d750d | 2516 | #ifndef MDASSEMBLE |
c17608ea AK |
2517 | /******************************************************************************* |
2518 | * function: imsm_create_metadata_checkpoint_update | |
2519 | * Description: It creates update for checkpoint change. | |
2520 | * Parameters: | |
2521 | * super : imsm internal array info | |
2522 | * u : pointer to prepared update | |
2523 | * Returns: | |
2524 | * Uptate length. | |
2525 | * If length is equal to 0, input pointer u contains no update | |
2526 | ******************************************************************************/ | |
2527 | static int imsm_create_metadata_checkpoint_update( | |
2528 | struct intel_super *super, | |
2529 | struct imsm_update_general_migration_checkpoint **u) | |
2530 | { | |
2531 | ||
2532 | int update_memory_size = 0; | |
2533 | ||
1ade5cc1 | 2534 | dprintf("(enter)\n"); |
c17608ea AK |
2535 | |
2536 | if (u == NULL) | |
2537 | return 0; | |
2538 | *u = NULL; | |
2539 | ||
2540 | /* size of all update data without anchor */ | |
2541 | update_memory_size = | |
2542 | sizeof(struct imsm_update_general_migration_checkpoint); | |
2543 | ||
503975b9 | 2544 | *u = xcalloc(1, update_memory_size); |
c17608ea | 2545 | if (*u == NULL) { |
1ade5cc1 | 2546 | dprintf("error: cannot get memory\n"); |
c17608ea AK |
2547 | return 0; |
2548 | } | |
2549 | (*u)->type = update_general_migration_checkpoint; | |
2550 | (*u)->curr_migr_unit = __le32_to_cpu(super->migr_rec->curr_migr_unit); | |
1ade5cc1 | 2551 | dprintf("prepared for %u\n", (*u)->curr_migr_unit); |
c17608ea AK |
2552 | |
2553 | return update_memory_size; | |
2554 | } | |
2555 | ||
c17608ea AK |
2556 | static void imsm_update_metadata_locally(struct supertype *st, |
2557 | void *buf, int len); | |
2558 | ||
687629c2 AK |
2559 | /******************************************************************************* |
2560 | * Function: write_imsm_migr_rec | |
2561 | * Description: Function writes imsm migration record | |
2562 | * (at the last sector of disk) | |
2563 | * Parameters: | |
2564 | * super : imsm internal array info | |
2565 | * Returns: | |
2566 | * 0 : success | |
2567 | * -1 : if fail | |
2568 | ******************************************************************************/ | |
2569 | static int write_imsm_migr_rec(struct supertype *st) | |
2570 | { | |
2571 | struct intel_super *super = st->sb; | |
2572 | unsigned long long dsize; | |
2573 | char nm[30]; | |
2574 | int fd = -1; | |
2575 | int retval = -1; | |
2576 | struct dl *sd; | |
c17608ea AK |
2577 | int len; |
2578 | struct imsm_update_general_migration_checkpoint *u; | |
3136abe5 AK |
2579 | struct imsm_dev *dev; |
2580 | struct imsm_map *map = NULL; | |
2581 | ||
2582 | /* find map under migration */ | |
2583 | dev = imsm_get_device_during_migration(super); | |
2584 | /* if no migration, write buffer anyway to clear migr_record | |
2585 | * on disk based on first available device | |
2586 | */ | |
2587 | if (dev == NULL) | |
2588 | dev = get_imsm_dev(super, super->current_vol < 0 ? 0 : | |
2589 | super->current_vol); | |
2590 | ||
44bfe6df | 2591 | map = get_imsm_map(dev, MAP_0); |
687629c2 AK |
2592 | |
2593 | for (sd = super->disks ; sd ; sd = sd->next) { | |
b4ab44d8 | 2594 | int slot = -1; |
3136abe5 AK |
2595 | |
2596 | /* skip failed and spare devices */ | |
2597 | if (sd->index < 0) | |
2598 | continue; | |
687629c2 | 2599 | /* write to 2 first slots only */ |
3136abe5 AK |
2600 | if (map) |
2601 | slot = get_imsm_disk_slot(map, sd->index); | |
2602 | if ((map == NULL) || (slot > 1) || (slot < 0)) | |
687629c2 | 2603 | continue; |
3136abe5 | 2604 | |
687629c2 AK |
2605 | sprintf(nm, "%d:%d", sd->major, sd->minor); |
2606 | fd = dev_open(nm, O_RDWR); | |
2607 | if (fd < 0) | |
2608 | continue; | |
2609 | get_dev_size(fd, NULL, &dsize); | |
17a4eaf9 | 2610 | if (lseek64(fd, dsize - MIGR_REC_POSITION, SEEK_SET) < 0) { |
e7b84f9d N |
2611 | pr_err("Cannot seek to anchor block: %s\n", |
2612 | strerror(errno)); | |
687629c2 AK |
2613 | goto out; |
2614 | } | |
17a4eaf9 AK |
2615 | if (write(fd, super->migr_rec_buf, MIGR_REC_BUF_SIZE) != |
2616 | MIGR_REC_BUF_SIZE) { | |
e7b84f9d N |
2617 | pr_err("Cannot write migr record block: %s\n", |
2618 | strerror(errno)); | |
687629c2 AK |
2619 | goto out; |
2620 | } | |
2621 | close(fd); | |
2622 | fd = -1; | |
2623 | } | |
c17608ea AK |
2624 | /* update checkpoint information in metadata */ |
2625 | len = imsm_create_metadata_checkpoint_update(super, &u); | |
2626 | ||
2627 | if (len <= 0) { | |
2628 | dprintf("imsm: Cannot prepare update\n"); | |
2629 | goto out; | |
2630 | } | |
2631 | /* update metadata locally */ | |
2632 | imsm_update_metadata_locally(st, u, len); | |
2633 | /* and possibly remotely */ | |
2634 | if (st->update_tail) { | |
2635 | append_metadata_update(st, u, len); | |
2636 | /* during reshape we do all work inside metadata handler | |
2637 | * manage_reshape(), so metadata update has to be triggered | |
2638 | * insida it | |
2639 | */ | |
2640 | flush_metadata_updates(st); | |
2641 | st->update_tail = &st->updates; | |
2642 | } else | |
2643 | free(u); | |
687629c2 AK |
2644 | |
2645 | retval = 0; | |
2646 | out: | |
2647 | if (fd >= 0) | |
2648 | close(fd); | |
2649 | return retval; | |
2650 | } | |
9e2d750d | 2651 | #endif /* MDASSEMBLE */ |
687629c2 | 2652 | |
e2962bfc AK |
2653 | /* spare/missing disks activations are not allowe when |
2654 | * array/container performs reshape operation, because | |
2655 | * all arrays in container works on the same disks set | |
2656 | */ | |
2657 | int imsm_reshape_blocks_arrays_changes(struct intel_super *super) | |
2658 | { | |
2659 | int rv = 0; | |
2660 | struct intel_dev *i_dev; | |
2661 | struct imsm_dev *dev; | |
2662 | ||
2663 | /* check whole container | |
2664 | */ | |
2665 | for (i_dev = super->devlist; i_dev; i_dev = i_dev->next) { | |
2666 | dev = i_dev->dev; | |
3ad25638 | 2667 | if (is_gen_migration(dev)) { |
e2962bfc AK |
2668 | /* No repair during any migration in container |
2669 | */ | |
2670 | rv = 1; | |
2671 | break; | |
2672 | } | |
2673 | } | |
2674 | return rv; | |
2675 | } | |
c41e00b2 AK |
2676 | static unsigned long long imsm_component_size_aligment_check(int level, |
2677 | int chunk_size, | |
2678 | unsigned long long component_size) | |
2679 | { | |
2680 | unsigned int component_size_alligment; | |
2681 | ||
2682 | /* check component size aligment | |
2683 | */ | |
2684 | component_size_alligment = component_size % (chunk_size/512); | |
2685 | ||
1ade5cc1 | 2686 | dprintf("(Level: %i, chunk_size = %i, component_size = %llu), component_size_alligment = %u\n", |
c41e00b2 AK |
2687 | level, chunk_size, component_size, |
2688 | component_size_alligment); | |
2689 | ||
2690 | if (component_size_alligment && (level != 1) && (level != UnSet)) { | |
2691 | dprintf("imsm: reported component size alligned from %llu ", | |
2692 | component_size); | |
2693 | component_size -= component_size_alligment; | |
1ade5cc1 | 2694 | dprintf_cont("to %llu (%i).\n", |
c41e00b2 AK |
2695 | component_size, component_size_alligment); |
2696 | } | |
2697 | ||
2698 | return component_size; | |
2699 | } | |
e2962bfc | 2700 | |
a5d85af7 | 2701 | static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info, char *dmap) |
bf5a934a DW |
2702 | { |
2703 | struct intel_super *super = st->sb; | |
c47b0ff6 | 2704 | struct migr_record *migr_rec = super->migr_rec; |
949c47a0 | 2705 | struct imsm_dev *dev = get_imsm_dev(super, super->current_vol); |
238c0a71 AK |
2706 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
2707 | struct imsm_map *prev_map = get_imsm_map(dev, MAP_1); | |
b335e593 | 2708 | struct imsm_map *map_to_analyse = map; |
efb30e7f | 2709 | struct dl *dl; |
a5d85af7 | 2710 | int map_disks = info->array.raid_disks; |
bf5a934a | 2711 | |
95eeceeb | 2712 | memset(info, 0, sizeof(*info)); |
b335e593 AK |
2713 | if (prev_map) |
2714 | map_to_analyse = prev_map; | |
2715 | ||
ca0748fa | 2716 | dl = super->current_disk; |
9894ec0d | 2717 | |
bf5a934a | 2718 | info->container_member = super->current_vol; |
cd0430a1 | 2719 | info->array.raid_disks = map->num_members; |
b335e593 | 2720 | info->array.level = get_imsm_raid_level(map_to_analyse); |
bf5a934a DW |
2721 | info->array.layout = imsm_level_to_layout(info->array.level); |
2722 | info->array.md_minor = -1; | |
2723 | info->array.ctime = 0; | |
2724 | info->array.utime = 0; | |
b335e593 AK |
2725 | info->array.chunk_size = |
2726 | __le16_to_cpu(map_to_analyse->blocks_per_strip) << 9; | |
301406c9 | 2727 | info->array.state = !dev->vol.dirty; |
da9b4a62 DW |
2728 | info->custom_array_size = __le32_to_cpu(dev->size_high); |
2729 | info->custom_array_size <<= 32; | |
2730 | info->custom_array_size |= __le32_to_cpu(dev->size_low); | |
3ad25638 AK |
2731 | info->recovery_blocked = imsm_reshape_blocks_arrays_changes(st->sb); |
2732 | ||
3f510843 | 2733 | if (is_gen_migration(dev)) { |
3f83228a | 2734 | info->reshape_active = 1; |
b335e593 AK |
2735 | info->new_level = get_imsm_raid_level(map); |
2736 | info->new_layout = imsm_level_to_layout(info->new_level); | |
2737 | info->new_chunk = __le16_to_cpu(map->blocks_per_strip) << 9; | |
3f83228a | 2738 | info->delta_disks = map->num_members - prev_map->num_members; |
493f5dd6 N |
2739 | if (info->delta_disks) { |
2740 | /* this needs to be applied to every array | |
2741 | * in the container. | |
2742 | */ | |
81219e70 | 2743 | info->reshape_active = CONTAINER_RESHAPE; |
493f5dd6 | 2744 | } |
3f83228a N |
2745 | /* We shape information that we give to md might have to be |
2746 | * modify to cope with md's requirement for reshaping arrays. | |
2747 | * For example, when reshaping a RAID0, md requires it to be | |
2748 | * presented as a degraded RAID4. | |
2749 | * Also if a RAID0 is migrating to a RAID5 we need to specify | |
2750 | * the array as already being RAID5, but the 'before' layout | |
2751 | * is a RAID4-like layout. | |
2752 | */ | |
2753 | switch (info->array.level) { | |
2754 | case 0: | |
2755 | switch(info->new_level) { | |
2756 | case 0: | |
2757 | /* conversion is happening as RAID4 */ | |
2758 | info->array.level = 4; | |
2759 | info->array.raid_disks += 1; | |
2760 | break; | |
2761 | case 5: | |
2762 | /* conversion is happening as RAID5 */ | |
2763 | info->array.level = 5; | |
2764 | info->array.layout = ALGORITHM_PARITY_N; | |
3f83228a N |
2765 | info->delta_disks -= 1; |
2766 | break; | |
2767 | default: | |
2768 | /* FIXME error message */ | |
2769 | info->array.level = UnSet; | |
2770 | break; | |
2771 | } | |
2772 | break; | |
2773 | } | |
b335e593 AK |
2774 | } else { |
2775 | info->new_level = UnSet; | |
2776 | info->new_layout = UnSet; | |
2777 | info->new_chunk = info->array.chunk_size; | |
3f83228a | 2778 | info->delta_disks = 0; |
b335e593 | 2779 | } |
ca0748fa | 2780 | |
efb30e7f DW |
2781 | if (dl) { |
2782 | info->disk.major = dl->major; | |
2783 | info->disk.minor = dl->minor; | |
ca0748fa | 2784 | info->disk.number = dl->index; |
656b6b5a N |
2785 | info->disk.raid_disk = get_imsm_disk_slot(map_to_analyse, |
2786 | dl->index); | |
efb30e7f | 2787 | } |
bf5a934a | 2788 | |
5551b113 CA |
2789 | info->data_offset = pba_of_lba0(map_to_analyse); |
2790 | info->component_size = blocks_per_member(map_to_analyse); | |
139dae11 | 2791 | |
c41e00b2 AK |
2792 | info->component_size = imsm_component_size_aligment_check( |
2793 | info->array.level, | |
2794 | info->array.chunk_size, | |
2795 | info->component_size); | |
139dae11 | 2796 | |
301406c9 | 2797 | memset(info->uuid, 0, sizeof(info->uuid)); |
921d9e16 | 2798 | info->recovery_start = MaxSector; |
bf5a934a | 2799 | |
d2e6d5d6 | 2800 | info->reshape_progress = 0; |
b6796ce1 | 2801 | info->resync_start = MaxSector; |
b9172665 AK |
2802 | if ((map_to_analyse->map_state == IMSM_T_STATE_UNINITIALIZED || |
2803 | dev->vol.dirty) && | |
2804 | imsm_reshape_blocks_arrays_changes(super) == 0) { | |
301406c9 | 2805 | info->resync_start = 0; |
b6796ce1 AK |
2806 | } |
2807 | if (dev->vol.migr_state) { | |
1e5c6983 DW |
2808 | switch (migr_type(dev)) { |
2809 | case MIGR_REPAIR: | |
2810 | case MIGR_INIT: { | |
c47b0ff6 AK |
2811 | __u64 blocks_per_unit = blocks_per_migr_unit(super, |
2812 | dev); | |
1e5c6983 DW |
2813 | __u64 units = __le32_to_cpu(dev->vol.curr_migr_unit); |
2814 | ||
2815 | info->resync_start = blocks_per_unit * units; | |
2816 | break; | |
2817 | } | |
d2e6d5d6 | 2818 | case MIGR_GEN_MIGR: { |
c47b0ff6 AK |
2819 | __u64 blocks_per_unit = blocks_per_migr_unit(super, |
2820 | dev); | |
2821 | __u64 units = __le32_to_cpu(migr_rec->curr_migr_unit); | |
04fa9523 AK |
2822 | unsigned long long array_blocks; |
2823 | int used_disks; | |
d2e6d5d6 | 2824 | |
befb629b AK |
2825 | if (__le32_to_cpu(migr_rec->ascending_migr) && |
2826 | (units < | |
2827 | (__le32_to_cpu(migr_rec->num_migr_units)-1)) && | |
2828 | (super->migr_rec->rec_status == | |
2829 | __cpu_to_le32(UNIT_SRC_IN_CP_AREA))) | |
2830 | units++; | |
2831 | ||
d2e6d5d6 | 2832 | info->reshape_progress = blocks_per_unit * units; |
6289d1e0 | 2833 | |
7a862a02 | 2834 | dprintf("IMSM: General Migration checkpoint : %llu (%llu) -> read reshape progress : %llu\n", |
19986c72 MB |
2835 | (unsigned long long)units, |
2836 | (unsigned long long)blocks_per_unit, | |
2837 | info->reshape_progress); | |
75156c46 | 2838 | |
238c0a71 | 2839 | used_disks = imsm_num_data_members(dev, MAP_1); |
75156c46 | 2840 | if (used_disks > 0) { |
5551b113 | 2841 | array_blocks = blocks_per_member(map) * |
75156c46 AK |
2842 | used_disks; |
2843 | /* round array size down to closest MB | |
2844 | */ | |
2845 | info->custom_array_size = (array_blocks | |
2846 | >> SECT_PER_MB_SHIFT) | |
2847 | << SECT_PER_MB_SHIFT; | |
2848 | } | |
d2e6d5d6 | 2849 | } |
1e5c6983 DW |
2850 | case MIGR_VERIFY: |
2851 | /* we could emulate the checkpointing of | |
2852 | * 'sync_action=check' migrations, but for now | |
2853 | * we just immediately complete them | |
2854 | */ | |
2855 | case MIGR_REBUILD: | |
2856 | /* this is handled by container_content_imsm() */ | |
1e5c6983 DW |
2857 | case MIGR_STATE_CHANGE: |
2858 | /* FIXME handle other migrations */ | |
2859 | default: | |
2860 | /* we are not dirty, so... */ | |
2861 | info->resync_start = MaxSector; | |
2862 | } | |
b6796ce1 | 2863 | } |
301406c9 DW |
2864 | |
2865 | strncpy(info->name, (char *) dev->volume, MAX_RAID_SERIAL_LEN); | |
2866 | info->name[MAX_RAID_SERIAL_LEN] = 0; | |
bf5a934a | 2867 | |
f35f2525 N |
2868 | info->array.major_version = -1; |
2869 | info->array.minor_version = -2; | |
4dd2df09 | 2870 | sprintf(info->text_version, "/%s/%d", st->container_devnm, info->container_member); |
a67dd8cc | 2871 | info->safe_mode_delay = 4000; /* 4 secs like the Matrix driver */ |
51006d85 | 2872 | uuid_from_super_imsm(st, info->uuid); |
a5d85af7 N |
2873 | |
2874 | if (dmap) { | |
2875 | int i, j; | |
2876 | for (i=0; i<map_disks; i++) { | |
2877 | dmap[i] = 0; | |
2878 | if (i < info->array.raid_disks) { | |
2879 | struct imsm_disk *dsk; | |
238c0a71 | 2880 | j = get_imsm_disk_idx(dev, i, MAP_X); |
a5d85af7 N |
2881 | dsk = get_imsm_disk(super, j); |
2882 | if (dsk && (dsk->status & CONFIGURED_DISK)) | |
2883 | dmap[i] = 1; | |
2884 | } | |
2885 | } | |
2886 | } | |
81ac8b4d | 2887 | } |
bf5a934a | 2888 | |
3b451610 AK |
2889 | static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev, |
2890 | int failed, int look_in_map); | |
2891 | ||
2892 | static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev, | |
2893 | int look_in_map); | |
2894 | ||
b4ab44d8 | 2895 | #ifndef MDASSEMBLE |
3b451610 AK |
2896 | static void manage_second_map(struct intel_super *super, struct imsm_dev *dev) |
2897 | { | |
2898 | if (is_gen_migration(dev)) { | |
2899 | int failed; | |
2900 | __u8 map_state; | |
2901 | struct imsm_map *map2 = get_imsm_map(dev, MAP_1); | |
2902 | ||
2903 | failed = imsm_count_failed(super, dev, MAP_1); | |
238c0a71 | 2904 | map_state = imsm_check_degraded(super, dev, failed, MAP_1); |
3b451610 AK |
2905 | if (map2->map_state != map_state) { |
2906 | map2->map_state = map_state; | |
2907 | super->updates_pending++; | |
2908 | } | |
2909 | } | |
2910 | } | |
b4ab44d8 | 2911 | #endif |
97b4d0e9 DW |
2912 | |
2913 | static struct imsm_disk *get_imsm_missing(struct intel_super *super, __u8 index) | |
2914 | { | |
2915 | struct dl *d; | |
2916 | ||
2917 | for (d = super->missing; d; d = d->next) | |
2918 | if (d->index == index) | |
2919 | return &d->disk; | |
2920 | return NULL; | |
2921 | } | |
2922 | ||
a5d85af7 | 2923 | static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map) |
4f5bc454 DW |
2924 | { |
2925 | struct intel_super *super = st->sb; | |
4f5bc454 | 2926 | struct imsm_disk *disk; |
a5d85af7 | 2927 | int map_disks = info->array.raid_disks; |
ab3cb6b3 N |
2928 | int max_enough = -1; |
2929 | int i; | |
2930 | struct imsm_super *mpb; | |
4f5bc454 | 2931 | |
bf5a934a | 2932 | if (super->current_vol >= 0) { |
a5d85af7 | 2933 | getinfo_super_imsm_volume(st, info, map); |
bf5a934a DW |
2934 | return; |
2935 | } | |
95eeceeb | 2936 | memset(info, 0, sizeof(*info)); |
d23fe947 DW |
2937 | |
2938 | /* Set raid_disks to zero so that Assemble will always pull in valid | |
2939 | * spares | |
2940 | */ | |
2941 | info->array.raid_disks = 0; | |
cdddbdbc DW |
2942 | info->array.level = LEVEL_CONTAINER; |
2943 | info->array.layout = 0; | |
2944 | info->array.md_minor = -1; | |
1011e834 | 2945 | info->array.ctime = 0; /* N/A for imsm */ |
cdddbdbc DW |
2946 | info->array.utime = 0; |
2947 | info->array.chunk_size = 0; | |
2948 | ||
2949 | info->disk.major = 0; | |
2950 | info->disk.minor = 0; | |
cdddbdbc | 2951 | info->disk.raid_disk = -1; |
c2c087e6 | 2952 | info->reshape_active = 0; |
f35f2525 N |
2953 | info->array.major_version = -1; |
2954 | info->array.minor_version = -2; | |
c2c087e6 | 2955 | strcpy(info->text_version, "imsm"); |
a67dd8cc | 2956 | info->safe_mode_delay = 0; |
c2c087e6 DW |
2957 | info->disk.number = -1; |
2958 | info->disk.state = 0; | |
c5afc314 | 2959 | info->name[0] = 0; |
921d9e16 | 2960 | info->recovery_start = MaxSector; |
3ad25638 | 2961 | info->recovery_blocked = imsm_reshape_blocks_arrays_changes(st->sb); |
c2c087e6 | 2962 | |
97b4d0e9 | 2963 | /* do we have the all the insync disks that we expect? */ |
ab3cb6b3 | 2964 | mpb = super->anchor; |
97b4d0e9 | 2965 | |
ab3cb6b3 N |
2966 | for (i = 0; i < mpb->num_raid_devs; i++) { |
2967 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
2968 | int failed, enough, j, missing = 0; | |
2969 | struct imsm_map *map; | |
2970 | __u8 state; | |
97b4d0e9 | 2971 | |
3b451610 AK |
2972 | failed = imsm_count_failed(super, dev, MAP_0); |
2973 | state = imsm_check_degraded(super, dev, failed, MAP_0); | |
238c0a71 | 2974 | map = get_imsm_map(dev, MAP_0); |
ab3cb6b3 N |
2975 | |
2976 | /* any newly missing disks? | |
2977 | * (catches single-degraded vs double-degraded) | |
2978 | */ | |
2979 | for (j = 0; j < map->num_members; j++) { | |
238c0a71 | 2980 | __u32 ord = get_imsm_ord_tbl_ent(dev, j, MAP_0); |
ab3cb6b3 N |
2981 | __u32 idx = ord_to_idx(ord); |
2982 | ||
2983 | if (!(ord & IMSM_ORD_REBUILD) && | |
2984 | get_imsm_missing(super, idx)) { | |
2985 | missing = 1; | |
2986 | break; | |
2987 | } | |
97b4d0e9 | 2988 | } |
ab3cb6b3 N |
2989 | |
2990 | if (state == IMSM_T_STATE_FAILED) | |
2991 | enough = -1; | |
2992 | else if (state == IMSM_T_STATE_DEGRADED && | |
2993 | (state != map->map_state || missing)) | |
2994 | enough = 0; | |
2995 | else /* we're normal, or already degraded */ | |
2996 | enough = 1; | |
d2bde6d3 AK |
2997 | if (is_gen_migration(dev) && missing) { |
2998 | /* during general migration we need all disks | |
2999 | * that process is running on. | |
3000 | * No new missing disk is allowed. | |
3001 | */ | |
3002 | max_enough = -1; | |
3003 | enough = -1; | |
3004 | /* no more checks necessary | |
3005 | */ | |
3006 | break; | |
3007 | } | |
ab3cb6b3 N |
3008 | /* in the missing/failed disk case check to see |
3009 | * if at least one array is runnable | |
3010 | */ | |
3011 | max_enough = max(max_enough, enough); | |
3012 | } | |
1ade5cc1 | 3013 | dprintf("enough: %d\n", max_enough); |
ab3cb6b3 | 3014 | info->container_enough = max_enough; |
97b4d0e9 | 3015 | |
4a04ec6c | 3016 | if (super->disks) { |
14e8215b DW |
3017 | __u32 reserved = imsm_reserved_sectors(super, super->disks); |
3018 | ||
b9f594fe | 3019 | disk = &super->disks->disk; |
5551b113 | 3020 | info->data_offset = total_blocks(&super->disks->disk) - reserved; |
14e8215b | 3021 | info->component_size = reserved; |
25ed7e59 | 3022 | info->disk.state = is_configured(disk) ? (1 << MD_DISK_ACTIVE) : 0; |
df474657 DW |
3023 | /* we don't change info->disk.raid_disk here because |
3024 | * this state will be finalized in mdmon after we have | |
3025 | * found the 'most fresh' version of the metadata | |
3026 | */ | |
25ed7e59 DW |
3027 | info->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0; |
3028 | info->disk.state |= is_spare(disk) ? 0 : (1 << MD_DISK_SYNC); | |
cdddbdbc | 3029 | } |
a575e2a7 DW |
3030 | |
3031 | /* only call uuid_from_super_imsm when this disk is part of a populated container, | |
3032 | * ->compare_super may have updated the 'num_raid_devs' field for spares | |
3033 | */ | |
3034 | if (info->disk.state & (1 << MD_DISK_SYNC) || super->anchor->num_raid_devs) | |
36ba7d48 | 3035 | uuid_from_super_imsm(st, info->uuid); |
22e263f6 AC |
3036 | else |
3037 | memcpy(info->uuid, uuid_zero, sizeof(uuid_zero)); | |
a5d85af7 N |
3038 | |
3039 | /* I don't know how to compute 'map' on imsm, so use safe default */ | |
3040 | if (map) { | |
3041 | int i; | |
3042 | for (i = 0; i < map_disks; i++) | |
3043 | map[i] = 1; | |
3044 | } | |
3045 | ||
cdddbdbc DW |
3046 | } |
3047 | ||
5c4cd5da AC |
3048 | /* allocates memory and fills disk in mdinfo structure |
3049 | * for each disk in array */ | |
3050 | struct mdinfo *getinfo_super_disks_imsm(struct supertype *st) | |
3051 | { | |
3052 | struct mdinfo *mddev = NULL; | |
3053 | struct intel_super *super = st->sb; | |
3054 | struct imsm_disk *disk; | |
3055 | int count = 0; | |
3056 | struct dl *dl; | |
3057 | if (!super || !super->disks) | |
3058 | return NULL; | |
3059 | dl = super->disks; | |
503975b9 | 3060 | mddev = xcalloc(1, sizeof(*mddev)); |
5c4cd5da AC |
3061 | while (dl) { |
3062 | struct mdinfo *tmp; | |
3063 | disk = &dl->disk; | |
503975b9 | 3064 | tmp = xcalloc(1, sizeof(*tmp)); |
5c4cd5da AC |
3065 | if (mddev->devs) |
3066 | tmp->next = mddev->devs; | |
3067 | mddev->devs = tmp; | |
3068 | tmp->disk.number = count++; | |
3069 | tmp->disk.major = dl->major; | |
3070 | tmp->disk.minor = dl->minor; | |
3071 | tmp->disk.state = is_configured(disk) ? | |
3072 | (1 << MD_DISK_ACTIVE) : 0; | |
3073 | tmp->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0; | |
3074 | tmp->disk.state |= is_spare(disk) ? 0 : (1 << MD_DISK_SYNC); | |
3075 | tmp->disk.raid_disk = -1; | |
3076 | dl = dl->next; | |
3077 | } | |
3078 | return mddev; | |
3079 | } | |
3080 | ||
cdddbdbc DW |
3081 | static int update_super_imsm(struct supertype *st, struct mdinfo *info, |
3082 | char *update, char *devname, int verbose, | |
3083 | int uuid_set, char *homehost) | |
3084 | { | |
f352c545 DW |
3085 | /* For 'assemble' and 'force' we need to return non-zero if any |
3086 | * change was made. For others, the return value is ignored. | |
3087 | * Update options are: | |
3088 | * force-one : This device looks a bit old but needs to be included, | |
3089 | * update age info appropriately. | |
3090 | * assemble: clear any 'faulty' flag to allow this device to | |
3091 | * be assembled. | |
3092 | * force-array: Array is degraded but being forced, mark it clean | |
3093 | * if that will be needed to assemble it. | |
3094 | * | |
3095 | * newdev: not used ???? | |
3096 | * grow: Array has gained a new device - this is currently for | |
3097 | * linear only | |
3098 | * resync: mark as dirty so a resync will happen. | |
3099 | * name: update the name - preserving the homehost | |
6e46bf34 | 3100 | * uuid: Change the uuid of the array to match watch is given |
f352c545 DW |
3101 | * |
3102 | * Following are not relevant for this imsm: | |
3103 | * sparc2.2 : update from old dodgey metadata | |
3104 | * super-minor: change the preferred_minor number | |
3105 | * summaries: update redundant counters. | |
f352c545 DW |
3106 | * homehost: update the recorded homehost |
3107 | * _reshape_progress: record new reshape_progress position. | |
3108 | */ | |
6e46bf34 DW |
3109 | int rv = 1; |
3110 | struct intel_super *super = st->sb; | |
3111 | struct imsm_super *mpb; | |
f352c545 | 3112 | |
6e46bf34 DW |
3113 | /* we can only update container info */ |
3114 | if (!super || super->current_vol >= 0 || !super->anchor) | |
3115 | return 1; | |
3116 | ||
3117 | mpb = super->anchor; | |
3118 | ||
81a5b4f5 N |
3119 | if (strcmp(update, "uuid") == 0) { |
3120 | /* We take this to mean that the family_num should be updated. | |
3121 | * However that is much smaller than the uuid so we cannot really | |
3122 | * allow an explicit uuid to be given. And it is hard to reliably | |
3123 | * know if one was. | |
3124 | * So if !uuid_set we know the current uuid is random and just used | |
3125 | * the first 'int' and copy it to the other 3 positions. | |
3126 | * Otherwise we require the 4 'int's to be the same as would be the | |
3127 | * case if we are using a random uuid. So an explicit uuid will be | |
3128 | * accepted as long as all for ints are the same... which shouldn't hurt | |
6e46bf34 | 3129 | */ |
81a5b4f5 N |
3130 | if (!uuid_set) { |
3131 | info->uuid[1] = info->uuid[2] = info->uuid[3] = info->uuid[0]; | |
6e46bf34 | 3132 | rv = 0; |
81a5b4f5 N |
3133 | } else { |
3134 | if (info->uuid[0] != info->uuid[1] || | |
3135 | info->uuid[1] != info->uuid[2] || | |
3136 | info->uuid[2] != info->uuid[3]) | |
3137 | rv = -1; | |
3138 | else | |
3139 | rv = 0; | |
6e46bf34 | 3140 | } |
81a5b4f5 N |
3141 | if (rv == 0) |
3142 | mpb->orig_family_num = info->uuid[0]; | |
6e46bf34 DW |
3143 | } else if (strcmp(update, "assemble") == 0) |
3144 | rv = 0; | |
3145 | else | |
1e2b2765 | 3146 | rv = -1; |
f352c545 | 3147 | |
6e46bf34 DW |
3148 | /* successful update? recompute checksum */ |
3149 | if (rv == 0) | |
3150 | mpb->check_sum = __le32_to_cpu(__gen_imsm_checksum(mpb)); | |
f352c545 DW |
3151 | |
3152 | return rv; | |
cdddbdbc DW |
3153 | } |
3154 | ||
c2c087e6 | 3155 | static size_t disks_to_mpb_size(int disks) |
cdddbdbc | 3156 | { |
c2c087e6 | 3157 | size_t size; |
cdddbdbc | 3158 | |
c2c087e6 DW |
3159 | size = sizeof(struct imsm_super); |
3160 | size += (disks - 1) * sizeof(struct imsm_disk); | |
3161 | size += 2 * sizeof(struct imsm_dev); | |
3162 | /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */ | |
3163 | size += (4 - 2) * sizeof(struct imsm_map); | |
3164 | /* 4 possible disk_ord_tbl's */ | |
3165 | size += 4 * (disks - 1) * sizeof(__u32); | |
3166 | ||
3167 | return size; | |
3168 | } | |
3169 | ||
387fcd59 N |
3170 | static __u64 avail_size_imsm(struct supertype *st, __u64 devsize, |
3171 | unsigned long long data_offset) | |
c2c087e6 DW |
3172 | { |
3173 | if (devsize < (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS)) | |
3174 | return 0; | |
3175 | ||
3176 | return devsize - (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS); | |
cdddbdbc DW |
3177 | } |
3178 | ||
ba2de7ba DW |
3179 | static void free_devlist(struct intel_super *super) |
3180 | { | |
3181 | struct intel_dev *dv; | |
3182 | ||
3183 | while (super->devlist) { | |
3184 | dv = super->devlist->next; | |
3185 | free(super->devlist->dev); | |
3186 | free(super->devlist); | |
3187 | super->devlist = dv; | |
3188 | } | |
3189 | } | |
3190 | ||
3191 | static void imsm_copy_dev(struct imsm_dev *dest, struct imsm_dev *src) | |
3192 | { | |
3193 | memcpy(dest, src, sizeof_imsm_dev(src, 0)); | |
3194 | } | |
3195 | ||
cdddbdbc DW |
3196 | static int compare_super_imsm(struct supertype *st, struct supertype *tst) |
3197 | { | |
3198 | /* | |
3199 | * return: | |
3200 | * 0 same, or first was empty, and second was copied | |
3201 | * 1 second had wrong number | |
3202 | * 2 wrong uuid | |
3203 | * 3 wrong other info | |
3204 | */ | |
3205 | struct intel_super *first = st->sb; | |
3206 | struct intel_super *sec = tst->sb; | |
3207 | ||
5d500228 N |
3208 | if (!first) { |
3209 | st->sb = tst->sb; | |
3210 | tst->sb = NULL; | |
3211 | return 0; | |
3212 | } | |
8603ea6f LM |
3213 | /* in platform dependent environment test if the disks |
3214 | * use the same Intel hba | |
cb8f6859 | 3215 | * If not on Intel hba at all, allow anything. |
8603ea6f | 3216 | */ |
6b781d33 AP |
3217 | if (!check_env("IMSM_NO_PLATFORM") && first->hba && sec->hba) { |
3218 | if (first->hba->type != sec->hba->type) { | |
8603ea6f | 3219 | fprintf(stderr, |
6b781d33 AP |
3220 | "HBAs of devices do not match %s != %s\n", |
3221 | get_sys_dev_type(first->hba->type), | |
3222 | get_sys_dev_type(sec->hba->type)); | |
3223 | return 3; | |
3224 | } | |
3225 | if (first->orom != sec->orom) { | |
3226 | fprintf(stderr, | |
3227 | "HBAs of devices do not match %s != %s\n", | |
3228 | first->hba->pci_id, sec->hba->pci_id); | |
8603ea6f LM |
3229 | return 3; |
3230 | } | |
3231 | } | |
cdddbdbc | 3232 | |
d23fe947 DW |
3233 | /* if an anchor does not have num_raid_devs set then it is a free |
3234 | * floating spare | |
3235 | */ | |
3236 | if (first->anchor->num_raid_devs > 0 && | |
3237 | sec->anchor->num_raid_devs > 0) { | |
a2b97981 DW |
3238 | /* Determine if these disks might ever have been |
3239 | * related. Further disambiguation can only take place | |
3240 | * in load_super_imsm_all | |
3241 | */ | |
3242 | __u32 first_family = first->anchor->orig_family_num; | |
3243 | __u32 sec_family = sec->anchor->orig_family_num; | |
3244 | ||
f796af5d DW |
3245 | if (memcmp(first->anchor->sig, sec->anchor->sig, |
3246 | MAX_SIGNATURE_LENGTH) != 0) | |
3247 | return 3; | |
3248 | ||
a2b97981 DW |
3249 | if (first_family == 0) |
3250 | first_family = first->anchor->family_num; | |
3251 | if (sec_family == 0) | |
3252 | sec_family = sec->anchor->family_num; | |
3253 | ||
3254 | if (first_family != sec_family) | |
d23fe947 | 3255 | return 3; |
f796af5d | 3256 | |
d23fe947 | 3257 | } |
cdddbdbc | 3258 | |
3e372e5a DW |
3259 | /* if 'first' is a spare promote it to a populated mpb with sec's |
3260 | * family number | |
3261 | */ | |
3262 | if (first->anchor->num_raid_devs == 0 && | |
3263 | sec->anchor->num_raid_devs > 0) { | |
78d30f94 | 3264 | int i; |
ba2de7ba DW |
3265 | struct intel_dev *dv; |
3266 | struct imsm_dev *dev; | |
78d30f94 DW |
3267 | |
3268 | /* we need to copy raid device info from sec if an allocation | |
3269 | * fails here we don't associate the spare | |
3270 | */ | |
3271 | for (i = 0; i < sec->anchor->num_raid_devs; i++) { | |
503975b9 N |
3272 | dv = xmalloc(sizeof(*dv)); |
3273 | dev = xmalloc(sizeof_imsm_dev(get_imsm_dev(sec, i), 1)); | |
ba2de7ba DW |
3274 | dv->dev = dev; |
3275 | dv->index = i; | |
3276 | dv->next = first->devlist; | |
3277 | first->devlist = dv; | |
78d30f94 | 3278 | } |
709743c5 | 3279 | if (i < sec->anchor->num_raid_devs) { |
ba2de7ba DW |
3280 | /* allocation failure */ |
3281 | free_devlist(first); | |
e12b3daa | 3282 | pr_err("imsm: failed to associate spare\n"); |
ba2de7ba | 3283 | return 3; |
78d30f94 | 3284 | } |
3e372e5a | 3285 | first->anchor->num_raid_devs = sec->anchor->num_raid_devs; |
148acb7b | 3286 | first->anchor->orig_family_num = sec->anchor->orig_family_num; |
3e372e5a | 3287 | first->anchor->family_num = sec->anchor->family_num; |
ac6449be | 3288 | memcpy(first->anchor->sig, sec->anchor->sig, MAX_SIGNATURE_LENGTH); |
709743c5 DW |
3289 | for (i = 0; i < sec->anchor->num_raid_devs; i++) |
3290 | imsm_copy_dev(get_imsm_dev(first, i), get_imsm_dev(sec, i)); | |
3e372e5a DW |
3291 | } |
3292 | ||
cdddbdbc DW |
3293 | return 0; |
3294 | } | |
3295 | ||
0030e8d6 DW |
3296 | static void fd2devname(int fd, char *name) |
3297 | { | |
3298 | struct stat st; | |
3299 | char path[256]; | |
33a6535d | 3300 | char dname[PATH_MAX]; |
0030e8d6 DW |
3301 | char *nm; |
3302 | int rv; | |
3303 | ||
3304 | name[0] = '\0'; | |
3305 | if (fstat(fd, &st) != 0) | |
3306 | return; | |
3307 | sprintf(path, "/sys/dev/block/%d:%d", | |
3308 | major(st.st_rdev), minor(st.st_rdev)); | |
3309 | ||
9cf014ec | 3310 | rv = readlink(path, dname, sizeof(dname)-1); |
0030e8d6 DW |
3311 | if (rv <= 0) |
3312 | return; | |
9587c373 | 3313 | |
0030e8d6 DW |
3314 | dname[rv] = '\0'; |
3315 | nm = strrchr(dname, '/'); | |
7897de29 JS |
3316 | if (nm) { |
3317 | nm++; | |
3318 | snprintf(name, MAX_RAID_SERIAL_LEN, "/dev/%s", nm); | |
3319 | } | |
0030e8d6 DW |
3320 | } |
3321 | ||
cdddbdbc DW |
3322 | extern int scsi_get_serial(int fd, void *buf, size_t buf_len); |
3323 | ||
3324 | static int imsm_read_serial(int fd, char *devname, | |
3325 | __u8 serial[MAX_RAID_SERIAL_LEN]) | |
3326 | { | |
3327 | unsigned char scsi_serial[255]; | |
cdddbdbc DW |
3328 | int rv; |
3329 | int rsp_len; | |
1f24f035 | 3330 | int len; |
316e2bf4 DW |
3331 | char *dest; |
3332 | char *src; | |
3333 | char *rsp_buf; | |
3334 | int i; | |
cdddbdbc DW |
3335 | |
3336 | memset(scsi_serial, 0, sizeof(scsi_serial)); | |
cdddbdbc | 3337 | |
f9ba0ff1 DW |
3338 | rv = scsi_get_serial(fd, scsi_serial, sizeof(scsi_serial)); |
3339 | ||
40ebbb9c | 3340 | if (rv && check_env("IMSM_DEVNAME_AS_SERIAL")) { |
f9ba0ff1 DW |
3341 | memset(serial, 0, MAX_RAID_SERIAL_LEN); |
3342 | fd2devname(fd, (char *) serial); | |
0030e8d6 DW |
3343 | return 0; |
3344 | } | |
3345 | ||
cdddbdbc DW |
3346 | if (rv != 0) { |
3347 | if (devname) | |
e7b84f9d N |
3348 | pr_err("Failed to retrieve serial for %s\n", |
3349 | devname); | |
cdddbdbc DW |
3350 | return rv; |
3351 | } | |
3352 | ||
3353 | rsp_len = scsi_serial[3]; | |
03cd4cc8 DW |
3354 | if (!rsp_len) { |
3355 | if (devname) | |
e7b84f9d N |
3356 | pr_err("Failed to retrieve serial for %s\n", |
3357 | devname); | |
03cd4cc8 DW |
3358 | return 2; |
3359 | } | |
1f24f035 | 3360 | rsp_buf = (char *) &scsi_serial[4]; |
5c3db629 | 3361 | |
316e2bf4 DW |
3362 | /* trim all whitespace and non-printable characters and convert |
3363 | * ':' to ';' | |
3364 | */ | |
3365 | for (i = 0, dest = rsp_buf; i < rsp_len; i++) { | |
3366 | src = &rsp_buf[i]; | |
3367 | if (*src > 0x20) { | |
3368 | /* ':' is reserved for use in placeholder serial | |
3369 | * numbers for missing disks | |
3370 | */ | |
3371 | if (*src == ':') | |
3372 | *dest++ = ';'; | |
3373 | else | |
3374 | *dest++ = *src; | |
3375 | } | |
3376 | } | |
3377 | len = dest - rsp_buf; | |
3378 | dest = rsp_buf; | |
3379 | ||
3380 | /* truncate leading characters */ | |
3381 | if (len > MAX_RAID_SERIAL_LEN) { | |
3382 | dest += len - MAX_RAID_SERIAL_LEN; | |
1f24f035 | 3383 | len = MAX_RAID_SERIAL_LEN; |
316e2bf4 | 3384 | } |
5c3db629 | 3385 | |
5c3db629 | 3386 | memset(serial, 0, MAX_RAID_SERIAL_LEN); |
316e2bf4 | 3387 | memcpy(serial, dest, len); |
cdddbdbc DW |
3388 | |
3389 | return 0; | |
3390 | } | |
3391 | ||
1f24f035 DW |
3392 | static int serialcmp(__u8 *s1, __u8 *s2) |
3393 | { | |
3394 | return strncmp((char *) s1, (char *) s2, MAX_RAID_SERIAL_LEN); | |
3395 | } | |
3396 | ||
3397 | static void serialcpy(__u8 *dest, __u8 *src) | |
3398 | { | |
3399 | strncpy((char *) dest, (char *) src, MAX_RAID_SERIAL_LEN); | |
3400 | } | |
3401 | ||
54c2c1ea DW |
3402 | static struct dl *serial_to_dl(__u8 *serial, struct intel_super *super) |
3403 | { | |
3404 | struct dl *dl; | |
3405 | ||
3406 | for (dl = super->disks; dl; dl = dl->next) | |
3407 | if (serialcmp(dl->serial, serial) == 0) | |
3408 | break; | |
3409 | ||
3410 | return dl; | |
3411 | } | |
3412 | ||
a2b97981 DW |
3413 | static struct imsm_disk * |
3414 | __serial_to_disk(__u8 *serial, struct imsm_super *mpb, int *idx) | |
3415 | { | |
3416 | int i; | |
3417 | ||
3418 | for (i = 0; i < mpb->num_disks; i++) { | |
3419 | struct imsm_disk *disk = __get_imsm_disk(mpb, i); | |
3420 | ||
3421 | if (serialcmp(disk->serial, serial) == 0) { | |
3422 | if (idx) | |
3423 | *idx = i; | |
3424 | return disk; | |
3425 | } | |
3426 | } | |
3427 | ||
3428 | return NULL; | |
3429 | } | |
3430 | ||
cdddbdbc DW |
3431 | static int |
3432 | load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd) | |
3433 | { | |
a2b97981 | 3434 | struct imsm_disk *disk; |
cdddbdbc DW |
3435 | struct dl *dl; |
3436 | struct stat stb; | |
cdddbdbc | 3437 | int rv; |
a2b97981 | 3438 | char name[40]; |
d23fe947 DW |
3439 | __u8 serial[MAX_RAID_SERIAL_LEN]; |
3440 | ||
3441 | rv = imsm_read_serial(fd, devname, serial); | |
3442 | ||
3443 | if (rv != 0) | |
3444 | return 2; | |
3445 | ||
503975b9 | 3446 | dl = xcalloc(1, sizeof(*dl)); |
cdddbdbc | 3447 | |
a2b97981 DW |
3448 | fstat(fd, &stb); |
3449 | dl->major = major(stb.st_rdev); | |
3450 | dl->minor = minor(stb.st_rdev); | |
3451 | dl->next = super->disks; | |
3452 | dl->fd = keep_fd ? fd : -1; | |
3453 | assert(super->disks == NULL); | |
3454 | super->disks = dl; | |
3455 | serialcpy(dl->serial, serial); | |
3456 | dl->index = -2; | |
3457 | dl->e = NULL; | |
3458 | fd2devname(fd, name); | |
3459 | if (devname) | |
503975b9 | 3460 | dl->devname = xstrdup(devname); |
a2b97981 | 3461 | else |
503975b9 | 3462 | dl->devname = xstrdup(name); |
cdddbdbc | 3463 | |
d23fe947 | 3464 | /* look up this disk's index in the current anchor */ |
a2b97981 DW |
3465 | disk = __serial_to_disk(dl->serial, super->anchor, &dl->index); |
3466 | if (disk) { | |
3467 | dl->disk = *disk; | |
3468 | /* only set index on disks that are a member of a | |
3469 | * populated contianer, i.e. one with raid_devs | |
3470 | */ | |
3471 | if (is_failed(&dl->disk)) | |
3f6efecc | 3472 | dl->index = -2; |
a2b97981 DW |
3473 | else if (is_spare(&dl->disk)) |
3474 | dl->index = -1; | |
3f6efecc DW |
3475 | } |
3476 | ||
949c47a0 DW |
3477 | return 0; |
3478 | } | |
3479 | ||
0e600426 | 3480 | #ifndef MDASSEMBLE |
0c046afd DW |
3481 | /* When migrating map0 contains the 'destination' state while map1 |
3482 | * contains the current state. When not migrating map0 contains the | |
3483 | * current state. This routine assumes that map[0].map_state is set to | |
3484 | * the current array state before being called. | |
3485 | * | |
3486 | * Migration is indicated by one of the following states | |
3487 | * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed) | |
e3bba0e0 | 3488 | * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal |
0c046afd | 3489 | * map1state=unitialized) |
1484e727 | 3490 | * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal |
0c046afd | 3491 | * map1state=normal) |
e3bba0e0 | 3492 | * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal |
0c046afd | 3493 | * map1state=degraded) |
8e59f3d8 AK |
3494 | * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal |
3495 | * map1state=normal) | |
0c046afd | 3496 | */ |
8e59f3d8 AK |
3497 | static void migrate(struct imsm_dev *dev, struct intel_super *super, |
3498 | __u8 to_state, int migr_type) | |
3393c6af | 3499 | { |
0c046afd | 3500 | struct imsm_map *dest; |
238c0a71 | 3501 | struct imsm_map *src = get_imsm_map(dev, MAP_0); |
3393c6af | 3502 | |
0c046afd | 3503 | dev->vol.migr_state = 1; |
1484e727 | 3504 | set_migr_type(dev, migr_type); |
f8f603f1 | 3505 | dev->vol.curr_migr_unit = 0; |
238c0a71 | 3506 | dest = get_imsm_map(dev, MAP_1); |
0c046afd | 3507 | |
0556e1a2 | 3508 | /* duplicate and then set the target end state in map[0] */ |
3393c6af | 3509 | memcpy(dest, src, sizeof_imsm_map(src)); |
28bce06f AK |
3510 | if ((migr_type == MIGR_REBUILD) || |
3511 | (migr_type == MIGR_GEN_MIGR)) { | |
0556e1a2 DW |
3512 | __u32 ord; |
3513 | int i; | |
3514 | ||
3515 | for (i = 0; i < src->num_members; i++) { | |
3516 | ord = __le32_to_cpu(src->disk_ord_tbl[i]); | |
3517 | set_imsm_ord_tbl_ent(src, i, ord_to_idx(ord)); | |
3518 | } | |
3519 | } | |
3520 | ||
8e59f3d8 AK |
3521 | if (migr_type == MIGR_GEN_MIGR) |
3522 | /* Clear migration record */ | |
3523 | memset(super->migr_rec, 0, sizeof(struct migr_record)); | |
3524 | ||
0c046afd | 3525 | src->map_state = to_state; |
949c47a0 | 3526 | } |
f8f603f1 | 3527 | |
809da78e AK |
3528 | static void end_migration(struct imsm_dev *dev, struct intel_super *super, |
3529 | __u8 map_state) | |
f8f603f1 | 3530 | { |
238c0a71 AK |
3531 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
3532 | struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state == 0 ? | |
3533 | MAP_0 : MAP_1); | |
28bce06f | 3534 | int i, j; |
0556e1a2 DW |
3535 | |
3536 | /* merge any IMSM_ORD_REBUILD bits that were not successfully | |
3537 | * completed in the last migration. | |
3538 | * | |
28bce06f | 3539 | * FIXME add support for raid-level-migration |
0556e1a2 | 3540 | */ |
809da78e AK |
3541 | if ((map_state != map->map_state) && (is_gen_migration(dev) == 0) && |
3542 | (prev->map_state != IMSM_T_STATE_UNINITIALIZED)) { | |
3543 | /* when final map state is other than expected | |
3544 | * merge maps (not for migration) | |
3545 | */ | |
3546 | int failed; | |
3547 | ||
3548 | for (i = 0; i < prev->num_members; i++) | |
3549 | for (j = 0; j < map->num_members; j++) | |
3550 | /* during online capacity expansion | |
3551 | * disks position can be changed | |
3552 | * if takeover is used | |
3553 | */ | |
3554 | if (ord_to_idx(map->disk_ord_tbl[j]) == | |
3555 | ord_to_idx(prev->disk_ord_tbl[i])) { | |
3556 | map->disk_ord_tbl[j] |= | |
3557 | prev->disk_ord_tbl[i]; | |
3558 | break; | |
3559 | } | |
3560 | failed = imsm_count_failed(super, dev, MAP_0); | |
3561 | map_state = imsm_check_degraded(super, dev, failed, MAP_0); | |
3562 | } | |
f8f603f1 DW |
3563 | |
3564 | dev->vol.migr_state = 0; | |
ea672ee1 | 3565 | set_migr_type(dev, 0); |
f8f603f1 DW |
3566 | dev->vol.curr_migr_unit = 0; |
3567 | map->map_state = map_state; | |
3568 | } | |
0e600426 | 3569 | #endif |
949c47a0 DW |
3570 | |
3571 | static int parse_raid_devices(struct intel_super *super) | |
3572 | { | |
3573 | int i; | |
3574 | struct imsm_dev *dev_new; | |
4d7b1503 | 3575 | size_t len, len_migr; |
401d313b | 3576 | size_t max_len = 0; |
4d7b1503 DW |
3577 | size_t space_needed = 0; |
3578 | struct imsm_super *mpb = super->anchor; | |
949c47a0 DW |
3579 | |
3580 | for (i = 0; i < super->anchor->num_raid_devs; i++) { | |
3581 | struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i); | |
ba2de7ba | 3582 | struct intel_dev *dv; |
949c47a0 | 3583 | |
4d7b1503 DW |
3584 | len = sizeof_imsm_dev(dev_iter, 0); |
3585 | len_migr = sizeof_imsm_dev(dev_iter, 1); | |
3586 | if (len_migr > len) | |
3587 | space_needed += len_migr - len; | |
ca9de185 | 3588 | |
503975b9 | 3589 | dv = xmalloc(sizeof(*dv)); |
401d313b AK |
3590 | if (max_len < len_migr) |
3591 | max_len = len_migr; | |
3592 | if (max_len > len_migr) | |
3593 | space_needed += max_len - len_migr; | |
503975b9 | 3594 | dev_new = xmalloc(max_len); |
949c47a0 | 3595 | imsm_copy_dev(dev_new, dev_iter); |
ba2de7ba DW |
3596 | dv->dev = dev_new; |
3597 | dv->index = i; | |
3598 | dv->next = super->devlist; | |
3599 | super->devlist = dv; | |
949c47a0 | 3600 | } |
cdddbdbc | 3601 | |
4d7b1503 DW |
3602 | /* ensure that super->buf is large enough when all raid devices |
3603 | * are migrating | |
3604 | */ | |
3605 | if (__le32_to_cpu(mpb->mpb_size) + space_needed > super->len) { | |
3606 | void *buf; | |
3607 | ||
3608 | len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + space_needed, 512); | |
3609 | if (posix_memalign(&buf, 512, len) != 0) | |
3610 | return 1; | |
3611 | ||
1f45a8ad DW |
3612 | memcpy(buf, super->buf, super->len); |
3613 | memset(buf + super->len, 0, len - super->len); | |
4d7b1503 DW |
3614 | free(super->buf); |
3615 | super->buf = buf; | |
3616 | super->len = len; | |
3617 | } | |
ca9de185 | 3618 | |
cdddbdbc DW |
3619 | return 0; |
3620 | } | |
3621 | ||
604b746f JD |
3622 | /* retrieve a pointer to the bbm log which starts after all raid devices */ |
3623 | struct bbm_log *__get_imsm_bbm_log(struct imsm_super *mpb) | |
3624 | { | |
3625 | void *ptr = NULL; | |
3626 | ||
3627 | if (__le32_to_cpu(mpb->bbm_log_size)) { | |
3628 | ptr = mpb; | |
3629 | ptr += mpb->mpb_size - __le32_to_cpu(mpb->bbm_log_size); | |
1011e834 | 3630 | } |
604b746f JD |
3631 | |
3632 | return ptr; | |
3633 | } | |
3634 | ||
e2f41b2c AK |
3635 | /******************************************************************************* |
3636 | * Function: check_mpb_migr_compatibility | |
3637 | * Description: Function checks for unsupported migration features: | |
3638 | * - migration optimization area (pba_of_lba0) | |
3639 | * - descending reshape (ascending_migr) | |
3640 | * Parameters: | |
3641 | * super : imsm metadata information | |
3642 | * Returns: | |
3643 | * 0 : migration is compatible | |
3644 | * -1 : migration is not compatible | |
3645 | ******************************************************************************/ | |
3646 | int check_mpb_migr_compatibility(struct intel_super *super) | |
3647 | { | |
3648 | struct imsm_map *map0, *map1; | |
3649 | struct migr_record *migr_rec = super->migr_rec; | |
3650 | int i; | |
3651 | ||
3652 | for (i = 0; i < super->anchor->num_raid_devs; i++) { | |
3653 | struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i); | |
3654 | ||
3655 | if (dev_iter && | |
3656 | dev_iter->vol.migr_state == 1 && | |
3657 | dev_iter->vol.migr_type == MIGR_GEN_MIGR) { | |
3658 | /* This device is migrating */ | |
238c0a71 AK |
3659 | map0 = get_imsm_map(dev_iter, MAP_0); |
3660 | map1 = get_imsm_map(dev_iter, MAP_1); | |
5551b113 | 3661 | if (pba_of_lba0(map0) != pba_of_lba0(map1)) |
e2f41b2c AK |
3662 | /* migration optimization area was used */ |
3663 | return -1; | |
3664 | if (migr_rec->ascending_migr == 0 | |
3665 | && migr_rec->dest_depth_per_unit > 0) | |
3666 | /* descending reshape not supported yet */ | |
3667 | return -1; | |
3668 | } | |
3669 | } | |
3670 | return 0; | |
3671 | } | |
3672 | ||
d23fe947 | 3673 | static void __free_imsm(struct intel_super *super, int free_disks); |
9ca2c81c | 3674 | |
cdddbdbc | 3675 | /* load_imsm_mpb - read matrix metadata |
f2f5c343 | 3676 | * allocates super->mpb to be freed by free_imsm |
cdddbdbc DW |
3677 | */ |
3678 | static int load_imsm_mpb(int fd, struct intel_super *super, char *devname) | |
3679 | { | |
3680 | unsigned long long dsize; | |
cdddbdbc DW |
3681 | unsigned long long sectors; |
3682 | struct stat; | |
6416d527 | 3683 | struct imsm_super *anchor; |
cdddbdbc DW |
3684 | __u32 check_sum; |
3685 | ||
cdddbdbc | 3686 | get_dev_size(fd, NULL, &dsize); |
64436f06 N |
3687 | if (dsize < 1024) { |
3688 | if (devname) | |
e7b84f9d N |
3689 | pr_err("%s: device to small for imsm\n", |
3690 | devname); | |
64436f06 N |
3691 | return 1; |
3692 | } | |
cdddbdbc DW |
3693 | |
3694 | if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0) { | |
3695 | if (devname) | |
e7b84f9d N |
3696 | pr_err("Cannot seek to anchor block on %s: %s\n", |
3697 | devname, strerror(errno)); | |
cdddbdbc DW |
3698 | return 1; |
3699 | } | |
3700 | ||
949c47a0 | 3701 | if (posix_memalign((void**)&anchor, 512, 512) != 0) { |
ad97895e | 3702 | if (devname) |
7a862a02 | 3703 | pr_err("Failed to allocate imsm anchor buffer on %s\n", devname); |
ad97895e DW |
3704 | return 1; |
3705 | } | |
949c47a0 | 3706 | if (read(fd, anchor, 512) != 512) { |
cdddbdbc | 3707 | if (devname) |
e7b84f9d N |
3708 | pr_err("Cannot read anchor block on %s: %s\n", |
3709 | devname, strerror(errno)); | |
6416d527 | 3710 | free(anchor); |
cdddbdbc DW |
3711 | return 1; |
3712 | } | |
3713 | ||
6416d527 | 3714 | if (strncmp((char *) anchor->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) { |
cdddbdbc | 3715 | if (devname) |
e7b84f9d | 3716 | pr_err("no IMSM anchor on %s\n", devname); |
6416d527 | 3717 | free(anchor); |
cdddbdbc DW |
3718 | return 2; |
3719 | } | |
3720 | ||
d23fe947 | 3721 | __free_imsm(super, 0); |
f2f5c343 LM |
3722 | /* reload capability and hba */ |
3723 | ||
3724 | /* capability and hba must be updated with new super allocation */ | |
d424212e | 3725 | find_intel_hba_capability(fd, super, devname); |
949c47a0 DW |
3726 | super->len = ROUND_UP(anchor->mpb_size, 512); |
3727 | if (posix_memalign(&super->buf, 512, super->len) != 0) { | |
cdddbdbc | 3728 | if (devname) |
e7b84f9d N |
3729 | pr_err("unable to allocate %zu byte mpb buffer\n", |
3730 | super->len); | |
6416d527 | 3731 | free(anchor); |
cdddbdbc DW |
3732 | return 2; |
3733 | } | |
949c47a0 | 3734 | memcpy(super->buf, anchor, 512); |
cdddbdbc | 3735 | |
6416d527 NB |
3736 | sectors = mpb_sectors(anchor) - 1; |
3737 | free(anchor); | |
8e59f3d8 | 3738 | |
17a4eaf9 | 3739 | if (posix_memalign(&super->migr_rec_buf, 512, MIGR_REC_BUF_SIZE) != 0) { |
1ade5cc1 | 3740 | pr_err("could not allocate migr_rec buffer\n"); |
8e59f3d8 AK |
3741 | free(super->buf); |
3742 | return 2; | |
3743 | } | |
51d83f5d | 3744 | super->clean_migration_record_by_mdmon = 0; |
8e59f3d8 | 3745 | |
949c47a0 | 3746 | if (!sectors) { |
ecf45690 DW |
3747 | check_sum = __gen_imsm_checksum(super->anchor); |
3748 | if (check_sum != __le32_to_cpu(super->anchor->check_sum)) { | |
3749 | if (devname) | |
e7b84f9d N |
3750 | pr_err("IMSM checksum %x != %x on %s\n", |
3751 | check_sum, | |
3752 | __le32_to_cpu(super->anchor->check_sum), | |
3753 | devname); | |
ecf45690 DW |
3754 | return 2; |
3755 | } | |
3756 | ||
a2b97981 | 3757 | return 0; |
949c47a0 | 3758 | } |
cdddbdbc DW |
3759 | |
3760 | /* read the extended mpb */ | |
3761 | if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0) { | |
3762 | if (devname) | |
e7b84f9d N |
3763 | pr_err("Cannot seek to extended mpb on %s: %s\n", |
3764 | devname, strerror(errno)); | |
cdddbdbc DW |
3765 | return 1; |
3766 | } | |
3767 | ||
f21e18ca | 3768 | if ((unsigned)read(fd, super->buf + 512, super->len - 512) != super->len - 512) { |
cdddbdbc | 3769 | if (devname) |
e7b84f9d N |
3770 | pr_err("Cannot read extended mpb on %s: %s\n", |
3771 | devname, strerror(errno)); | |
cdddbdbc DW |
3772 | return 2; |
3773 | } | |
3774 | ||
949c47a0 DW |
3775 | check_sum = __gen_imsm_checksum(super->anchor); |
3776 | if (check_sum != __le32_to_cpu(super->anchor->check_sum)) { | |
cdddbdbc | 3777 | if (devname) |
e7b84f9d N |
3778 | pr_err("IMSM checksum %x != %x on %s\n", |
3779 | check_sum, __le32_to_cpu(super->anchor->check_sum), | |
3780 | devname); | |
db575f3b | 3781 | return 3; |
cdddbdbc DW |
3782 | } |
3783 | ||
604b746f JD |
3784 | /* FIXME the BBM log is disk specific so we cannot use this global |
3785 | * buffer for all disks. Ok for now since we only look at the global | |
3786 | * bbm_log_size parameter to gate assembly | |
3787 | */ | |
3788 | super->bbm_log = __get_imsm_bbm_log(super->anchor); | |
3789 | ||
a2b97981 DW |
3790 | return 0; |
3791 | } | |
3792 | ||
8e59f3d8 AK |
3793 | static int read_imsm_migr_rec(int fd, struct intel_super *super); |
3794 | ||
97f81ee2 CA |
3795 | /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */ |
3796 | static void clear_hi(struct intel_super *super) | |
3797 | { | |
3798 | struct imsm_super *mpb = super->anchor; | |
3799 | int i, n; | |
3800 | if (mpb->attributes & MPB_ATTRIB_2TB_DISK) | |
3801 | return; | |
3802 | for (i = 0; i < mpb->num_disks; ++i) { | |
3803 | struct imsm_disk *disk = &mpb->disk[i]; | |
3804 | disk->total_blocks_hi = 0; | |
3805 | } | |
3806 | for (i = 0; i < mpb->num_raid_devs; ++i) { | |
3807 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
3808 | if (!dev) | |
3809 | return; | |
3810 | for (n = 0; n < 2; ++n) { | |
3811 | struct imsm_map *map = get_imsm_map(dev, n); | |
3812 | if (!map) | |
3813 | continue; | |
3814 | map->pba_of_lba0_hi = 0; | |
3815 | map->blocks_per_member_hi = 0; | |
3816 | map->num_data_stripes_hi = 0; | |
3817 | } | |
3818 | } | |
3819 | } | |
3820 | ||
a2b97981 DW |
3821 | static int |
3822 | load_and_parse_mpb(int fd, struct intel_super *super, char *devname, int keep_fd) | |
3823 | { | |
3824 | int err; | |
3825 | ||
3826 | err = load_imsm_mpb(fd, super, devname); | |
3827 | if (err) | |
3828 | return err; | |
3829 | err = load_imsm_disk(fd, super, devname, keep_fd); | |
3830 | if (err) | |
3831 | return err; | |
3832 | err = parse_raid_devices(super); | |
97f81ee2 | 3833 | clear_hi(super); |
a2b97981 | 3834 | return err; |
cdddbdbc DW |
3835 | } |
3836 | ||
ae6aad82 DW |
3837 | static void __free_imsm_disk(struct dl *d) |
3838 | { | |
3839 | if (d->fd >= 0) | |
3840 | close(d->fd); | |
3841 | if (d->devname) | |
3842 | free(d->devname); | |
0dcecb2e DW |
3843 | if (d->e) |
3844 | free(d->e); | |
ae6aad82 DW |
3845 | free(d); |
3846 | ||
3847 | } | |
1a64be56 | 3848 | |
cdddbdbc DW |
3849 | static void free_imsm_disks(struct intel_super *super) |
3850 | { | |
47ee5a45 | 3851 | struct dl *d; |
cdddbdbc | 3852 | |
47ee5a45 DW |
3853 | while (super->disks) { |
3854 | d = super->disks; | |
cdddbdbc | 3855 | super->disks = d->next; |
ae6aad82 | 3856 | __free_imsm_disk(d); |
cdddbdbc | 3857 | } |
cb82edca AK |
3858 | while (super->disk_mgmt_list) { |
3859 | d = super->disk_mgmt_list; | |
3860 | super->disk_mgmt_list = d->next; | |
3861 | __free_imsm_disk(d); | |
3862 | } | |
47ee5a45 DW |
3863 | while (super->missing) { |
3864 | d = super->missing; | |
3865 | super->missing = d->next; | |
3866 | __free_imsm_disk(d); | |
3867 | } | |
3868 | ||
cdddbdbc DW |
3869 | } |
3870 | ||
9ca2c81c | 3871 | /* free all the pieces hanging off of a super pointer */ |
d23fe947 | 3872 | static void __free_imsm(struct intel_super *super, int free_disks) |
cdddbdbc | 3873 | { |
88654014 LM |
3874 | struct intel_hba *elem, *next; |
3875 | ||
9ca2c81c | 3876 | if (super->buf) { |
949c47a0 | 3877 | free(super->buf); |
9ca2c81c DW |
3878 | super->buf = NULL; |
3879 | } | |
f2f5c343 LM |
3880 | /* unlink capability description */ |
3881 | super->orom = NULL; | |
8e59f3d8 AK |
3882 | if (super->migr_rec_buf) { |
3883 | free(super->migr_rec_buf); | |
3884 | super->migr_rec_buf = NULL; | |
3885 | } | |
d23fe947 DW |
3886 | if (free_disks) |
3887 | free_imsm_disks(super); | |
ba2de7ba | 3888 | free_devlist(super); |
88654014 LM |
3889 | elem = super->hba; |
3890 | while (elem) { | |
3891 | if (elem->path) | |
3892 | free((void *)elem->path); | |
3893 | next = elem->next; | |
3894 | free(elem); | |
3895 | elem = next; | |
88c32bb1 | 3896 | } |
88654014 | 3897 | super->hba = NULL; |
cdddbdbc DW |
3898 | } |
3899 | ||
9ca2c81c DW |
3900 | static void free_imsm(struct intel_super *super) |
3901 | { | |
d23fe947 | 3902 | __free_imsm(super, 1); |
9ca2c81c DW |
3903 | free(super); |
3904 | } | |
cdddbdbc DW |
3905 | |
3906 | static void free_super_imsm(struct supertype *st) | |
3907 | { | |
3908 | struct intel_super *super = st->sb; | |
3909 | ||
3910 | if (!super) | |
3911 | return; | |
3912 | ||
3913 | free_imsm(super); | |
3914 | st->sb = NULL; | |
3915 | } | |
3916 | ||
49133e57 | 3917 | static struct intel_super *alloc_super(void) |
c2c087e6 | 3918 | { |
503975b9 | 3919 | struct intel_super *super = xcalloc(1, sizeof(*super)); |
c2c087e6 | 3920 | |
503975b9 N |
3921 | super->current_vol = -1; |
3922 | super->create_offset = ~((unsigned long long) 0); | |
c2c087e6 DW |
3923 | return super; |
3924 | } | |
3925 | ||
f0f5a016 LM |
3926 | /* |
3927 | * find and allocate hba and OROM/EFI based on valid fd of RAID component device | |
3928 | */ | |
d424212e | 3929 | static int find_intel_hba_capability(int fd, struct intel_super *super, char *devname) |
f0f5a016 LM |
3930 | { |
3931 | struct sys_dev *hba_name; | |
3932 | int rv = 0; | |
3933 | ||
3934 | if ((fd < 0) || check_env("IMSM_NO_PLATFORM")) { | |
f2f5c343 | 3935 | super->orom = NULL; |
f0f5a016 LM |
3936 | super->hba = NULL; |
3937 | return 0; | |
3938 | } | |
3939 | hba_name = find_disk_attached_hba(fd, NULL); | |
3940 | if (!hba_name) { | |
d424212e | 3941 | if (devname) |
e7b84f9d N |
3942 | pr_err("%s is not attached to Intel(R) RAID controller.\n", |
3943 | devname); | |
f0f5a016 LM |
3944 | return 1; |
3945 | } | |
3946 | rv = attach_hba_to_super(super, hba_name); | |
3947 | if (rv == 2) { | |
d424212e N |
3948 | if (devname) { |
3949 | struct intel_hba *hba = super->hba; | |
f0f5a016 | 3950 | |
60f0f54d PB |
3951 | pr_err("%s is attached to Intel(R) %s %s (%s),\n" |
3952 | " but the container is assigned to Intel(R) %s %s (", | |
d424212e | 3953 | devname, |
614902f6 | 3954 | get_sys_dev_type(hba_name->type), |
60f0f54d | 3955 | hba_name->type == SYS_DEV_VMD ? "domain" : "RAID controller", |
f0f5a016 | 3956 | hba_name->pci_id ? : "Err!", |
60f0f54d PB |
3957 | get_sys_dev_type(super->hba->type), |
3958 | hba->type == SYS_DEV_VMD ? "domain" : "RAID controller"); | |
f0f5a016 | 3959 | |
f0f5a016 LM |
3960 | while (hba) { |
3961 | fprintf(stderr, "%s", hba->pci_id ? : "Err!"); | |
3962 | if (hba->next) | |
3963 | fprintf(stderr, ", "); | |
3964 | hba = hba->next; | |
3965 | } | |
6b781d33 | 3966 | fprintf(stderr, ").\n" |
60f0f54d PB |
3967 | " Mixing devices attached to different %s is not allowed.\n", |
3968 | hba_name->type == SYS_DEV_VMD ? "VMD domains" : "controllers"); | |
f0f5a016 | 3969 | } |
f0f5a016 LM |
3970 | return 2; |
3971 | } | |
6b781d33 | 3972 | super->orom = find_imsm_capability(hba_name); |
f2f5c343 LM |
3973 | if (!super->orom) |
3974 | return 3; | |
614902f6 | 3975 | |
f0f5a016 LM |
3976 | return 0; |
3977 | } | |
3978 | ||
47ee5a45 DW |
3979 | /* find_missing - helper routine for load_super_imsm_all that identifies |
3980 | * disks that have disappeared from the system. This routine relies on | |
3981 | * the mpb being uptodate, which it is at load time. | |
3982 | */ | |
3983 | static int find_missing(struct intel_super *super) | |
3984 | { | |
3985 | int i; | |
3986 | struct imsm_super *mpb = super->anchor; | |
3987 | struct dl *dl; | |
3988 | struct imsm_disk *disk; | |
47ee5a45 DW |
3989 | |
3990 | for (i = 0; i < mpb->num_disks; i++) { | |
3991 | disk = __get_imsm_disk(mpb, i); | |
54c2c1ea | 3992 | dl = serial_to_dl(disk->serial, super); |
47ee5a45 DW |
3993 | if (dl) |
3994 | continue; | |
47ee5a45 | 3995 | |
503975b9 | 3996 | dl = xmalloc(sizeof(*dl)); |
47ee5a45 DW |
3997 | dl->major = 0; |
3998 | dl->minor = 0; | |
3999 | dl->fd = -1; | |
503975b9 | 4000 | dl->devname = xstrdup("missing"); |
47ee5a45 DW |
4001 | dl->index = i; |
4002 | serialcpy(dl->serial, disk->serial); | |
4003 | dl->disk = *disk; | |
689c9bf3 | 4004 | dl->e = NULL; |
47ee5a45 DW |
4005 | dl->next = super->missing; |
4006 | super->missing = dl; | |
4007 | } | |
4008 | ||
4009 | return 0; | |
4010 | } | |
4011 | ||
3960e579 | 4012 | #ifndef MDASSEMBLE |
a2b97981 DW |
4013 | static struct intel_disk *disk_list_get(__u8 *serial, struct intel_disk *disk_list) |
4014 | { | |
4015 | struct intel_disk *idisk = disk_list; | |
4016 | ||
4017 | while (idisk) { | |
4018 | if (serialcmp(idisk->disk.serial, serial) == 0) | |
4019 | break; | |
4020 | idisk = idisk->next; | |
4021 | } | |
4022 | ||
4023 | return idisk; | |
4024 | } | |
4025 | ||
4026 | static int __prep_thunderdome(struct intel_super **table, int tbl_size, | |
4027 | struct intel_super *super, | |
4028 | struct intel_disk **disk_list) | |
4029 | { | |
4030 | struct imsm_disk *d = &super->disks->disk; | |
4031 | struct imsm_super *mpb = super->anchor; | |
4032 | int i, j; | |
4033 | ||
4034 | for (i = 0; i < tbl_size; i++) { | |
4035 | struct imsm_super *tbl_mpb = table[i]->anchor; | |
4036 | struct imsm_disk *tbl_d = &table[i]->disks->disk; | |
4037 | ||
4038 | if (tbl_mpb->family_num == mpb->family_num) { | |
4039 | if (tbl_mpb->check_sum == mpb->check_sum) { | |
1ade5cc1 N |
4040 | dprintf("mpb from %d:%d matches %d:%d\n", |
4041 | super->disks->major, | |
a2b97981 DW |
4042 | super->disks->minor, |
4043 | table[i]->disks->major, | |
4044 | table[i]->disks->minor); | |
4045 | break; | |
4046 | } | |
4047 | ||
4048 | if (((is_configured(d) && !is_configured(tbl_d)) || | |
4049 | is_configured(d) == is_configured(tbl_d)) && | |
4050 | tbl_mpb->generation_num < mpb->generation_num) { | |
4051 | /* current version of the mpb is a | |
4052 | * better candidate than the one in | |
4053 | * super_table, but copy over "cross | |
4054 | * generational" status | |
4055 | */ | |
4056 | struct intel_disk *idisk; | |
4057 | ||
1ade5cc1 N |
4058 | dprintf("mpb from %d:%d replaces %d:%d\n", |
4059 | super->disks->major, | |
a2b97981 DW |
4060 | super->disks->minor, |
4061 | table[i]->disks->major, | |
4062 | table[i]->disks->minor); | |
4063 | ||
4064 | idisk = disk_list_get(tbl_d->serial, *disk_list); | |
4065 | if (idisk && is_failed(&idisk->disk)) | |
4066 | tbl_d->status |= FAILED_DISK; | |
4067 | break; | |
4068 | } else { | |
4069 | struct intel_disk *idisk; | |
4070 | struct imsm_disk *disk; | |
4071 | ||
4072 | /* tbl_mpb is more up to date, but copy | |
4073 | * over cross generational status before | |
4074 | * returning | |
4075 | */ | |
4076 | disk = __serial_to_disk(d->serial, mpb, NULL); | |
4077 | if (disk && is_failed(disk)) | |
4078 | d->status |= FAILED_DISK; | |
4079 | ||
4080 | idisk = disk_list_get(d->serial, *disk_list); | |
4081 | if (idisk) { | |
4082 | idisk->owner = i; | |
4083 | if (disk && is_configured(disk)) | |
4084 | idisk->disk.status |= CONFIGURED_DISK; | |
4085 | } | |
4086 | ||
1ade5cc1 N |
4087 | dprintf("mpb from %d:%d prefer %d:%d\n", |
4088 | super->disks->major, | |
a2b97981 DW |
4089 | super->disks->minor, |
4090 | table[i]->disks->major, | |
4091 | table[i]->disks->minor); | |
4092 | ||
4093 | return tbl_size; | |
4094 | } | |
4095 | } | |
4096 | } | |
4097 | ||
4098 | if (i >= tbl_size) | |
4099 | table[tbl_size++] = super; | |
4100 | else | |
4101 | table[i] = super; | |
4102 | ||
4103 | /* update/extend the merged list of imsm_disk records */ | |
4104 | for (j = 0; j < mpb->num_disks; j++) { | |
4105 | struct imsm_disk *disk = __get_imsm_disk(mpb, j); | |
4106 | struct intel_disk *idisk; | |
4107 | ||
4108 | idisk = disk_list_get(disk->serial, *disk_list); | |
4109 | if (idisk) { | |
4110 | idisk->disk.status |= disk->status; | |
4111 | if (is_configured(&idisk->disk) || | |
4112 | is_failed(&idisk->disk)) | |
4113 | idisk->disk.status &= ~(SPARE_DISK); | |
4114 | } else { | |
503975b9 | 4115 | idisk = xcalloc(1, sizeof(*idisk)); |
a2b97981 DW |
4116 | idisk->owner = IMSM_UNKNOWN_OWNER; |
4117 | idisk->disk = *disk; | |
4118 | idisk->next = *disk_list; | |
4119 | *disk_list = idisk; | |
4120 | } | |
4121 | ||
4122 | if (serialcmp(idisk->disk.serial, d->serial) == 0) | |
4123 | idisk->owner = i; | |
4124 | } | |
4125 | ||
4126 | return tbl_size; | |
4127 | } | |
4128 | ||
4129 | static struct intel_super * | |
4130 | validate_members(struct intel_super *super, struct intel_disk *disk_list, | |
4131 | const int owner) | |
4132 | { | |
4133 | struct imsm_super *mpb = super->anchor; | |
4134 | int ok_count = 0; | |
4135 | int i; | |
4136 | ||
4137 | for (i = 0; i < mpb->num_disks; i++) { | |
4138 | struct imsm_disk *disk = __get_imsm_disk(mpb, i); | |
4139 | struct intel_disk *idisk; | |
4140 | ||
4141 | idisk = disk_list_get(disk->serial, disk_list); | |
4142 | if (idisk) { | |
4143 | if (idisk->owner == owner || | |
4144 | idisk->owner == IMSM_UNKNOWN_OWNER) | |
4145 | ok_count++; | |
4146 | else | |
1ade5cc1 N |
4147 | dprintf("'%.16s' owner %d != %d\n", |
4148 | disk->serial, idisk->owner, | |
a2b97981 DW |
4149 | owner); |
4150 | } else { | |
1ade5cc1 N |
4151 | dprintf("unknown disk %x [%d]: %.16s\n", |
4152 | __le32_to_cpu(mpb->family_num), i, | |
a2b97981 DW |
4153 | disk->serial); |
4154 | break; | |
4155 | } | |
4156 | } | |
4157 | ||
4158 | if (ok_count == mpb->num_disks) | |
4159 | return super; | |
4160 | return NULL; | |
4161 | } | |
4162 | ||
4163 | static void show_conflicts(__u32 family_num, struct intel_super *super_list) | |
4164 | { | |
4165 | struct intel_super *s; | |
4166 | ||
4167 | for (s = super_list; s; s = s->next) { | |
4168 | if (family_num != s->anchor->family_num) | |
4169 | continue; | |
e12b3daa | 4170 | pr_err("Conflict, offlining family %#x on '%s'\n", |
a2b97981 DW |
4171 | __le32_to_cpu(family_num), s->disks->devname); |
4172 | } | |
4173 | } | |
4174 | ||
4175 | static struct intel_super * | |
4176 | imsm_thunderdome(struct intel_super **super_list, int len) | |
4177 | { | |
4178 | struct intel_super *super_table[len]; | |
4179 | struct intel_disk *disk_list = NULL; | |
4180 | struct intel_super *champion, *spare; | |
4181 | struct intel_super *s, **del; | |
4182 | int tbl_size = 0; | |
4183 | int conflict; | |
4184 | int i; | |
4185 | ||
4186 | memset(super_table, 0, sizeof(super_table)); | |
4187 | for (s = *super_list; s; s = s->next) | |
4188 | tbl_size = __prep_thunderdome(super_table, tbl_size, s, &disk_list); | |
4189 | ||
4190 | for (i = 0; i < tbl_size; i++) { | |
4191 | struct imsm_disk *d; | |
4192 | struct intel_disk *idisk; | |
4193 | struct imsm_super *mpb = super_table[i]->anchor; | |
4194 | ||
4195 | s = super_table[i]; | |
4196 | d = &s->disks->disk; | |
4197 | ||
4198 | /* 'd' must appear in merged disk list for its | |
4199 | * configuration to be valid | |
4200 | */ | |
4201 | idisk = disk_list_get(d->serial, disk_list); | |
4202 | if (idisk && idisk->owner == i) | |
4203 | s = validate_members(s, disk_list, i); | |
4204 | else | |
4205 | s = NULL; | |
4206 | ||
4207 | if (!s) | |
1ade5cc1 N |
4208 | dprintf("marking family: %#x from %d:%d offline\n", |
4209 | mpb->family_num, | |
a2b97981 DW |
4210 | super_table[i]->disks->major, |
4211 | super_table[i]->disks->minor); | |
4212 | super_table[i] = s; | |
4213 | } | |
4214 | ||
4215 | /* This is where the mdadm implementation differs from the Windows | |
4216 | * driver which has no strict concept of a container. We can only | |
4217 | * assemble one family from a container, so when returning a prodigal | |
4218 | * array member to this system the code will not be able to disambiguate | |
4219 | * the container contents that should be assembled ("foreign" versus | |
4220 | * "local"). It requires user intervention to set the orig_family_num | |
4221 | * to a new value to establish a new container. The Windows driver in | |
4222 | * this situation fixes up the volume name in place and manages the | |
4223 | * foreign array as an independent entity. | |
4224 | */ | |
4225 | s = NULL; | |
4226 | spare = NULL; | |
4227 | conflict = 0; | |
4228 | for (i = 0; i < tbl_size; i++) { | |
4229 | struct intel_super *tbl_ent = super_table[i]; | |
4230 | int is_spare = 0; | |
4231 | ||
4232 | if (!tbl_ent) | |
4233 | continue; | |
4234 | ||
4235 | if (tbl_ent->anchor->num_raid_devs == 0) { | |
4236 | spare = tbl_ent; | |
4237 | is_spare = 1; | |
4238 | } | |
4239 | ||
4240 | if (s && !is_spare) { | |
4241 | show_conflicts(tbl_ent->anchor->family_num, *super_list); | |
4242 | conflict++; | |
4243 | } else if (!s && !is_spare) | |
4244 | s = tbl_ent; | |
4245 | } | |
4246 | ||
4247 | if (!s) | |
4248 | s = spare; | |
4249 | if (!s) { | |
4250 | champion = NULL; | |
4251 | goto out; | |
4252 | } | |
4253 | champion = s; | |
4254 | ||
4255 | if (conflict) | |
7a862a02 | 4256 | pr_err("Chose family %#x on '%s', assemble conflicts to new container with '--update=uuid'\n", |
a2b97981 DW |
4257 | __le32_to_cpu(s->anchor->family_num), s->disks->devname); |
4258 | ||
4259 | /* collect all dl's onto 'champion', and update them to | |
4260 | * champion's version of the status | |
4261 | */ | |
4262 | for (s = *super_list; s; s = s->next) { | |
4263 | struct imsm_super *mpb = champion->anchor; | |
4264 | struct dl *dl = s->disks; | |
4265 | ||
4266 | if (s == champion) | |
4267 | continue; | |
4268 | ||
5d7b407a CA |
4269 | mpb->attributes |= s->anchor->attributes & MPB_ATTRIB_2TB_DISK; |
4270 | ||
a2b97981 DW |
4271 | for (i = 0; i < mpb->num_disks; i++) { |
4272 | struct imsm_disk *disk; | |
4273 | ||
4274 | disk = __serial_to_disk(dl->serial, mpb, &dl->index); | |
4275 | if (disk) { | |
4276 | dl->disk = *disk; | |
4277 | /* only set index on disks that are a member of | |
4278 | * a populated contianer, i.e. one with | |
4279 | * raid_devs | |
4280 | */ | |
4281 | if (is_failed(&dl->disk)) | |
4282 | dl->index = -2; | |
4283 | else if (is_spare(&dl->disk)) | |
4284 | dl->index = -1; | |
4285 | break; | |
4286 | } | |
4287 | } | |
4288 | ||
4289 | if (i >= mpb->num_disks) { | |
4290 | struct intel_disk *idisk; | |
4291 | ||
4292 | idisk = disk_list_get(dl->serial, disk_list); | |
ecf408e9 | 4293 | if (idisk && is_spare(&idisk->disk) && |
a2b97981 DW |
4294 | !is_failed(&idisk->disk) && !is_configured(&idisk->disk)) |
4295 | dl->index = -1; | |
4296 | else { | |
4297 | dl->index = -2; | |
4298 | continue; | |
4299 | } | |
4300 | } | |
4301 | ||
4302 | dl->next = champion->disks; | |
4303 | champion->disks = dl; | |
4304 | s->disks = NULL; | |
4305 | } | |
4306 | ||
4307 | /* delete 'champion' from super_list */ | |
4308 | for (del = super_list; *del; ) { | |
4309 | if (*del == champion) { | |
4310 | *del = (*del)->next; | |
4311 | break; | |
4312 | } else | |
4313 | del = &(*del)->next; | |
4314 | } | |
4315 | champion->next = NULL; | |
4316 | ||
4317 | out: | |
4318 | while (disk_list) { | |
4319 | struct intel_disk *idisk = disk_list; | |
4320 | ||
4321 | disk_list = disk_list->next; | |
4322 | free(idisk); | |
4323 | } | |
4324 | ||
4325 | return champion; | |
4326 | } | |
4327 | ||
9587c373 LM |
4328 | static int |
4329 | get_sra_super_block(int fd, struct intel_super **super_list, char *devname, int *max, int keep_fd); | |
4dd2df09 | 4330 | static int get_super_block(struct intel_super **super_list, char *devnm, char *devname, |
9587c373 | 4331 | int major, int minor, int keep_fd); |
ec50f7b6 LM |
4332 | static int |
4333 | get_devlist_super_block(struct md_list *devlist, struct intel_super **super_list, | |
4334 | int *max, int keep_fd); | |
4335 | ||
cdddbdbc | 4336 | static int load_super_imsm_all(struct supertype *st, int fd, void **sbp, |
ec50f7b6 LM |
4337 | char *devname, struct md_list *devlist, |
4338 | int keep_fd) | |
cdddbdbc | 4339 | { |
a2b97981 DW |
4340 | struct intel_super *super_list = NULL; |
4341 | struct intel_super *super = NULL; | |
a2b97981 | 4342 | int err = 0; |
9587c373 | 4343 | int i = 0; |
dab4a513 | 4344 | |
9587c373 LM |
4345 | if (fd >= 0) |
4346 | /* 'fd' is an opened container */ | |
4347 | err = get_sra_super_block(fd, &super_list, devname, &i, keep_fd); | |
4348 | else | |
ec50f7b6 LM |
4349 | /* get super block from devlist devices */ |
4350 | err = get_devlist_super_block(devlist, &super_list, &i, keep_fd); | |
9587c373 | 4351 | if (err) |
1602d52c | 4352 | goto error; |
a2b97981 DW |
4353 | /* all mpbs enter, maybe one leaves */ |
4354 | super = imsm_thunderdome(&super_list, i); | |
4355 | if (!super) { | |
4356 | err = 1; | |
4357 | goto error; | |
cdddbdbc DW |
4358 | } |
4359 | ||
47ee5a45 DW |
4360 | if (find_missing(super) != 0) { |
4361 | free_imsm(super); | |
a2b97981 DW |
4362 | err = 2; |
4363 | goto error; | |
47ee5a45 | 4364 | } |
8e59f3d8 AK |
4365 | |
4366 | /* load migration record */ | |
4367 | err = load_imsm_migr_rec(super, NULL); | |
4c965cc9 AK |
4368 | if (err == -1) { |
4369 | /* migration is in progress, | |
4370 | * but migr_rec cannot be loaded, | |
4371 | */ | |
8e59f3d8 AK |
4372 | err = 4; |
4373 | goto error; | |
4374 | } | |
e2f41b2c AK |
4375 | |
4376 | /* Check migration compatibility */ | |
4c965cc9 | 4377 | if ((err == 0) && (check_mpb_migr_compatibility(super) != 0)) { |
e7b84f9d | 4378 | pr_err("Unsupported migration detected"); |
e2f41b2c AK |
4379 | if (devname) |
4380 | fprintf(stderr, " on %s\n", devname); | |
4381 | else | |
4382 | fprintf(stderr, " (IMSM).\n"); | |
4383 | ||
4384 | err = 5; | |
4385 | goto error; | |
4386 | } | |
4387 | ||
a2b97981 DW |
4388 | err = 0; |
4389 | ||
4390 | error: | |
4391 | while (super_list) { | |
4392 | struct intel_super *s = super_list; | |
4393 | ||
4394 | super_list = super_list->next; | |
4395 | free_imsm(s); | |
4396 | } | |
9587c373 | 4397 | |
a2b97981 DW |
4398 | if (err) |
4399 | return err; | |
f7e7067b | 4400 | |
cdddbdbc | 4401 | *sbp = super; |
9587c373 | 4402 | if (fd >= 0) |
4dd2df09 | 4403 | strcpy(st->container_devnm, fd2devnm(fd)); |
9587c373 | 4404 | else |
4dd2df09 | 4405 | st->container_devnm[0] = 0; |
a2b97981 | 4406 | if (err == 0 && st->ss == NULL) { |
bf5a934a | 4407 | st->ss = &super_imsm; |
cdddbdbc DW |
4408 | st->minor_version = 0; |
4409 | st->max_devs = IMSM_MAX_DEVICES; | |
4410 | } | |
cdddbdbc DW |
4411 | return 0; |
4412 | } | |
2b959fbf | 4413 | |
ec50f7b6 LM |
4414 | static int |
4415 | get_devlist_super_block(struct md_list *devlist, struct intel_super **super_list, | |
4416 | int *max, int keep_fd) | |
4417 | { | |
4418 | struct md_list *tmpdev; | |
4419 | int err = 0; | |
4420 | int i = 0; | |
9587c373 | 4421 | |
ec50f7b6 LM |
4422 | for (i = 0, tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) { |
4423 | if (tmpdev->used != 1) | |
4424 | continue; | |
4425 | if (tmpdev->container == 1) { | |
ca9de185 | 4426 | int lmax = 0; |
ec50f7b6 LM |
4427 | int fd = dev_open(tmpdev->devname, O_RDONLY|O_EXCL); |
4428 | if (fd < 0) { | |
e7b84f9d | 4429 | pr_err("cannot open device %s: %s\n", |
ec50f7b6 LM |
4430 | tmpdev->devname, strerror(errno)); |
4431 | err = 8; | |
4432 | goto error; | |
4433 | } | |
4434 | err = get_sra_super_block(fd, super_list, | |
4435 | tmpdev->devname, &lmax, | |
4436 | keep_fd); | |
4437 | i += lmax; | |
4438 | close(fd); | |
4439 | if (err) { | |
4440 | err = 7; | |
4441 | goto error; | |
4442 | } | |
4443 | } else { | |
4444 | int major = major(tmpdev->st_rdev); | |
4445 | int minor = minor(tmpdev->st_rdev); | |
4446 | err = get_super_block(super_list, | |
4dd2df09 | 4447 | NULL, |
ec50f7b6 LM |
4448 | tmpdev->devname, |
4449 | major, minor, | |
4450 | keep_fd); | |
4451 | i++; | |
4452 | if (err) { | |
4453 | err = 6; | |
4454 | goto error; | |
4455 | } | |
4456 | } | |
4457 | } | |
4458 | error: | |
4459 | *max = i; | |
4460 | return err; | |
4461 | } | |
9587c373 | 4462 | |
4dd2df09 | 4463 | static int get_super_block(struct intel_super **super_list, char *devnm, char *devname, |
9587c373 LM |
4464 | int major, int minor, int keep_fd) |
4465 | { | |
4466 | struct intel_super*s = NULL; | |
4467 | char nm[32]; | |
4468 | int dfd = -1; | |
9587c373 LM |
4469 | int err = 0; |
4470 | int retry; | |
4471 | ||
4472 | s = alloc_super(); | |
4473 | if (!s) { | |
4474 | err = 1; | |
4475 | goto error; | |
4476 | } | |
4477 | ||
4478 | sprintf(nm, "%d:%d", major, minor); | |
4479 | dfd = dev_open(nm, O_RDWR); | |
4480 | if (dfd < 0) { | |
4481 | err = 2; | |
4482 | goto error; | |
4483 | } | |
4484 | ||
cb8f6859 | 4485 | find_intel_hba_capability(dfd, s, devname); |
9587c373 LM |
4486 | err = load_and_parse_mpb(dfd, s, NULL, keep_fd); |
4487 | ||
4488 | /* retry the load if we might have raced against mdmon */ | |
4dd2df09 | 4489 | if (err == 3 && devnm && mdmon_running(devnm)) |
9587c373 LM |
4490 | for (retry = 0; retry < 3; retry++) { |
4491 | usleep(3000); | |
4492 | err = load_and_parse_mpb(dfd, s, NULL, keep_fd); | |
4493 | if (err != 3) | |
4494 | break; | |
4495 | } | |
4496 | error: | |
4497 | if (!err) { | |
4498 | s->next = *super_list; | |
4499 | *super_list = s; | |
4500 | } else { | |
4501 | if (s) | |
4502 | free(s); | |
36614e95 | 4503 | if (dfd >= 0) |
9587c373 LM |
4504 | close(dfd); |
4505 | } | |
4506 | if ((dfd >= 0) && (!keep_fd)) | |
4507 | close(dfd); | |
4508 | return err; | |
4509 | ||
4510 | } | |
4511 | ||
4512 | static int | |
4513 | get_sra_super_block(int fd, struct intel_super **super_list, char *devname, int *max, int keep_fd) | |
4514 | { | |
4515 | struct mdinfo *sra; | |
4dd2df09 | 4516 | char *devnm; |
9587c373 LM |
4517 | struct mdinfo *sd; |
4518 | int err = 0; | |
4519 | int i = 0; | |
4dd2df09 | 4520 | sra = sysfs_read(fd, NULL, GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE); |
9587c373 LM |
4521 | if (!sra) |
4522 | return 1; | |
4523 | ||
4524 | if (sra->array.major_version != -1 || | |
4525 | sra->array.minor_version != -2 || | |
4526 | strcmp(sra->text_version, "imsm") != 0) { | |
4527 | err = 1; | |
4528 | goto error; | |
4529 | } | |
4530 | /* load all mpbs */ | |
4dd2df09 | 4531 | devnm = fd2devnm(fd); |
9587c373 | 4532 | for (sd = sra->devs, i = 0; sd; sd = sd->next, i++) { |
4dd2df09 | 4533 | if (get_super_block(super_list, devnm, devname, |
9587c373 LM |
4534 | sd->disk.major, sd->disk.minor, keep_fd) != 0) { |
4535 | err = 7; | |
4536 | goto error; | |
4537 | } | |
4538 | } | |
4539 | error: | |
4540 | sysfs_free(sra); | |
4541 | *max = i; | |
4542 | return err; | |
4543 | } | |
4544 | ||
2b959fbf N |
4545 | static int load_container_imsm(struct supertype *st, int fd, char *devname) |
4546 | { | |
ec50f7b6 | 4547 | return load_super_imsm_all(st, fd, &st->sb, devname, NULL, 1); |
2b959fbf | 4548 | } |
cdddbdbc DW |
4549 | #endif |
4550 | ||
4551 | static int load_super_imsm(struct supertype *st, int fd, char *devname) | |
4552 | { | |
4553 | struct intel_super *super; | |
4554 | int rv; | |
8a3544f8 | 4555 | int retry; |
cdddbdbc | 4556 | |
357ac106 | 4557 | if (test_partition(fd)) |
691c6ee1 N |
4558 | /* IMSM not allowed on partitions */ |
4559 | return 1; | |
4560 | ||
37424f13 DW |
4561 | free_super_imsm(st); |
4562 | ||
49133e57 | 4563 | super = alloc_super(); |
ea2bc72b LM |
4564 | /* Load hba and capabilities if they exist. |
4565 | * But do not preclude loading metadata in case capabilities or hba are | |
4566 | * non-compliant and ignore_hw_compat is set. | |
4567 | */ | |
d424212e | 4568 | rv = find_intel_hba_capability(fd, super, devname); |
f2f5c343 | 4569 | /* no orom/efi or non-intel hba of the disk */ |
ea2bc72b | 4570 | if ((rv != 0) && (st->ignore_hw_compat == 0)) { |
f2f5c343 | 4571 | if (devname) |
e7b84f9d | 4572 | pr_err("No OROM/EFI properties for %s\n", devname); |
f2f5c343 LM |
4573 | free_imsm(super); |
4574 | return 2; | |
4575 | } | |
a2b97981 | 4576 | rv = load_and_parse_mpb(fd, super, devname, 0); |
cdddbdbc | 4577 | |
8a3544f8 AP |
4578 | /* retry the load if we might have raced against mdmon */ |
4579 | if (rv == 3) { | |
4580 | struct mdstat_ent *mdstat = mdstat_by_component(fd2devnm(fd)); | |
4581 | ||
4582 | if (mdstat && mdmon_running(mdstat->devnm) && getpid() != mdmon_pid(mdstat->devnm)) { | |
4583 | for (retry = 0; retry < 3; retry++) { | |
4584 | usleep(3000); | |
4585 | rv = load_and_parse_mpb(fd, super, devname, 0); | |
4586 | if (rv != 3) | |
4587 | break; | |
4588 | } | |
4589 | } | |
4590 | ||
4591 | free_mdstat(mdstat); | |
4592 | } | |
4593 | ||
cdddbdbc DW |
4594 | if (rv) { |
4595 | if (devname) | |
7a862a02 | 4596 | pr_err("Failed to load all information sections on %s\n", devname); |
cdddbdbc DW |
4597 | free_imsm(super); |
4598 | return rv; | |
4599 | } | |
4600 | ||
4601 | st->sb = super; | |
4602 | if (st->ss == NULL) { | |
4603 | st->ss = &super_imsm; | |
4604 | st->minor_version = 0; | |
4605 | st->max_devs = IMSM_MAX_DEVICES; | |
4606 | } | |
8e59f3d8 AK |
4607 | |
4608 | /* load migration record */ | |
2e062e82 AK |
4609 | if (load_imsm_migr_rec(super, NULL) == 0) { |
4610 | /* Check for unsupported migration features */ | |
4611 | if (check_mpb_migr_compatibility(super) != 0) { | |
e7b84f9d | 4612 | pr_err("Unsupported migration detected"); |
2e062e82 AK |
4613 | if (devname) |
4614 | fprintf(stderr, " on %s\n", devname); | |
4615 | else | |
4616 | fprintf(stderr, " (IMSM).\n"); | |
4617 | return 3; | |
4618 | } | |
e2f41b2c AK |
4619 | } |
4620 | ||
cdddbdbc DW |
4621 | return 0; |
4622 | } | |
4623 | ||
ef6ffade DW |
4624 | static __u16 info_to_blocks_per_strip(mdu_array_info_t *info) |
4625 | { | |
4626 | if (info->level == 1) | |
4627 | return 128; | |
4628 | return info->chunk_size >> 9; | |
4629 | } | |
4630 | ||
5551b113 CA |
4631 | static unsigned long long info_to_blocks_per_member(mdu_array_info_t *info, |
4632 | unsigned long long size) | |
fcfd9599 | 4633 | { |
4025c288 | 4634 | if (info->level == 1) |
5551b113 | 4635 | return size * 2; |
4025c288 | 4636 | else |
5551b113 | 4637 | return (size * 2) & ~(info_to_blocks_per_strip(info) - 1); |
fcfd9599 DW |
4638 | } |
4639 | ||
4d1313e9 DW |
4640 | static void imsm_update_version_info(struct intel_super *super) |
4641 | { | |
4642 | /* update the version and attributes */ | |
4643 | struct imsm_super *mpb = super->anchor; | |
4644 | char *version; | |
4645 | struct imsm_dev *dev; | |
4646 | struct imsm_map *map; | |
4647 | int i; | |
4648 | ||
4649 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
4650 | dev = get_imsm_dev(super, i); | |
238c0a71 | 4651 | map = get_imsm_map(dev, MAP_0); |
4d1313e9 DW |
4652 | if (__le32_to_cpu(dev->size_high) > 0) |
4653 | mpb->attributes |= MPB_ATTRIB_2TB; | |
4654 | ||
4655 | /* FIXME detect when an array spans a port multiplier */ | |
4656 | #if 0 | |
4657 | mpb->attributes |= MPB_ATTRIB_PM; | |
4658 | #endif | |
4659 | ||
4660 | if (mpb->num_raid_devs > 1 || | |
4661 | mpb->attributes != MPB_ATTRIB_CHECKSUM_VERIFY) { | |
4662 | version = MPB_VERSION_ATTRIBS; | |
4663 | switch (get_imsm_raid_level(map)) { | |
4664 | case 0: mpb->attributes |= MPB_ATTRIB_RAID0; break; | |
4665 | case 1: mpb->attributes |= MPB_ATTRIB_RAID1; break; | |
4666 | case 10: mpb->attributes |= MPB_ATTRIB_RAID10; break; | |
4667 | case 5: mpb->attributes |= MPB_ATTRIB_RAID5; break; | |
4668 | } | |
4669 | } else { | |
4670 | if (map->num_members >= 5) | |
4671 | version = MPB_VERSION_5OR6_DISK_ARRAY; | |
4672 | else if (dev->status == DEV_CLONE_N_GO) | |
4673 | version = MPB_VERSION_CNG; | |
4674 | else if (get_imsm_raid_level(map) == 5) | |
4675 | version = MPB_VERSION_RAID5; | |
4676 | else if (map->num_members >= 3) | |
4677 | version = MPB_VERSION_3OR4_DISK_ARRAY; | |
4678 | else if (get_imsm_raid_level(map) == 1) | |
4679 | version = MPB_VERSION_RAID1; | |
4680 | else | |
4681 | version = MPB_VERSION_RAID0; | |
4682 | } | |
4683 | strcpy(((char *) mpb->sig) + strlen(MPB_SIGNATURE), version); | |
4684 | } | |
4685 | } | |
4686 | ||
aa534678 DW |
4687 | static int check_name(struct intel_super *super, char *name, int quiet) |
4688 | { | |
4689 | struct imsm_super *mpb = super->anchor; | |
4690 | char *reason = NULL; | |
4691 | int i; | |
4692 | ||
4693 | if (strlen(name) > MAX_RAID_SERIAL_LEN) | |
4694 | reason = "must be 16 characters or less"; | |
4695 | ||
4696 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
4697 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
4698 | ||
4699 | if (strncmp((char *) dev->volume, name, MAX_RAID_SERIAL_LEN) == 0) { | |
4700 | reason = "already exists"; | |
4701 | break; | |
4702 | } | |
4703 | } | |
4704 | ||
4705 | if (reason && !quiet) | |
e7b84f9d | 4706 | pr_err("imsm volume name %s\n", reason); |
aa534678 DW |
4707 | |
4708 | return !reason; | |
4709 | } | |
4710 | ||
8b353278 DW |
4711 | static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info, |
4712 | unsigned long long size, char *name, | |
83cd1e97 N |
4713 | char *homehost, int *uuid, |
4714 | long long data_offset) | |
cdddbdbc | 4715 | { |
c2c087e6 DW |
4716 | /* We are creating a volume inside a pre-existing container. |
4717 | * so st->sb is already set. | |
4718 | */ | |
4719 | struct intel_super *super = st->sb; | |
949c47a0 | 4720 | struct imsm_super *mpb = super->anchor; |
ba2de7ba | 4721 | struct intel_dev *dv; |
c2c087e6 DW |
4722 | struct imsm_dev *dev; |
4723 | struct imsm_vol *vol; | |
4724 | struct imsm_map *map; | |
4725 | int idx = mpb->num_raid_devs; | |
4726 | int i; | |
4727 | unsigned long long array_blocks; | |
2c092cad | 4728 | size_t size_old, size_new; |
5551b113 | 4729 | unsigned long long num_data_stripes; |
cdddbdbc | 4730 | |
88c32bb1 | 4731 | if (super->orom && mpb->num_raid_devs >= super->orom->vpa) { |
7a862a02 | 4732 | pr_err("This imsm-container already has the maximum of %d volumes\n", super->orom->vpa); |
c2c087e6 DW |
4733 | return 0; |
4734 | } | |
4735 | ||
2c092cad DW |
4736 | /* ensure the mpb is large enough for the new data */ |
4737 | size_old = __le32_to_cpu(mpb->mpb_size); | |
4738 | size_new = disks_to_mpb_size(info->nr_disks); | |
4739 | if (size_new > size_old) { | |
4740 | void *mpb_new; | |
4741 | size_t size_round = ROUND_UP(size_new, 512); | |
4742 | ||
4743 | if (posix_memalign(&mpb_new, 512, size_round) != 0) { | |
e7b84f9d | 4744 | pr_err("could not allocate new mpb\n"); |
2c092cad DW |
4745 | return 0; |
4746 | } | |
17a4eaf9 AK |
4747 | if (posix_memalign(&super->migr_rec_buf, 512, |
4748 | MIGR_REC_BUF_SIZE) != 0) { | |
1ade5cc1 | 4749 | pr_err("could not allocate migr_rec buffer\n"); |
8e59f3d8 AK |
4750 | free(super->buf); |
4751 | free(super); | |
ea944c8f | 4752 | free(mpb_new); |
8e59f3d8 AK |
4753 | return 0; |
4754 | } | |
2c092cad DW |
4755 | memcpy(mpb_new, mpb, size_old); |
4756 | free(mpb); | |
4757 | mpb = mpb_new; | |
949c47a0 | 4758 | super->anchor = mpb_new; |
2c092cad DW |
4759 | mpb->mpb_size = __cpu_to_le32(size_new); |
4760 | memset(mpb_new + size_old, 0, size_round - size_old); | |
4761 | } | |
bf5a934a | 4762 | super->current_vol = idx; |
3960e579 DW |
4763 | |
4764 | /* handle 'failed_disks' by either: | |
4765 | * a) create dummy disk entries in the table if this the first | |
4766 | * volume in the array. We add them here as this is the only | |
4767 | * opportunity to add them. add_to_super_imsm_volume() | |
4768 | * handles the non-failed disks and continues incrementing | |
4769 | * mpb->num_disks. | |
4770 | * b) validate that 'failed_disks' matches the current number | |
4771 | * of missing disks if the container is populated | |
d23fe947 | 4772 | */ |
3960e579 | 4773 | if (super->current_vol == 0) { |
d23fe947 | 4774 | mpb->num_disks = 0; |
3960e579 DW |
4775 | for (i = 0; i < info->failed_disks; i++) { |
4776 | struct imsm_disk *disk; | |
4777 | ||
4778 | mpb->num_disks++; | |
4779 | disk = __get_imsm_disk(mpb, i); | |
4780 | disk->status = CONFIGURED_DISK | FAILED_DISK; | |
4781 | disk->scsi_id = __cpu_to_le32(~(__u32)0); | |
4782 | snprintf((char *) disk->serial, MAX_RAID_SERIAL_LEN, | |
4783 | "missing:%d", i); | |
4784 | } | |
4785 | find_missing(super); | |
4786 | } else { | |
4787 | int missing = 0; | |
4788 | struct dl *d; | |
4789 | ||
4790 | for (d = super->missing; d; d = d->next) | |
4791 | missing++; | |
4792 | if (info->failed_disks > missing) { | |
e7b84f9d | 4793 | pr_err("unable to add 'missing' disk to container\n"); |
3960e579 DW |
4794 | return 0; |
4795 | } | |
4796 | } | |
5a038140 | 4797 | |
aa534678 DW |
4798 | if (!check_name(super, name, 0)) |
4799 | return 0; | |
503975b9 N |
4800 | dv = xmalloc(sizeof(*dv)); |
4801 | dev = xcalloc(1, sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1)); | |
c2c087e6 | 4802 | strncpy((char *) dev->volume, name, MAX_RAID_SERIAL_LEN); |
e03640bd | 4803 | array_blocks = calc_array_size(info->level, info->raid_disks, |
03bcbc65 | 4804 | info->layout, info->chunk_size, |
5551b113 | 4805 | size * 2); |
979d38be DW |
4806 | /* round array size down to closest MB */ |
4807 | array_blocks = (array_blocks >> SECT_PER_MB_SHIFT) << SECT_PER_MB_SHIFT; | |
4808 | ||
c2c087e6 DW |
4809 | dev->size_low = __cpu_to_le32((__u32) array_blocks); |
4810 | dev->size_high = __cpu_to_le32((__u32) (array_blocks >> 32)); | |
1a2487c2 | 4811 | dev->status = (DEV_READ_COALESCING | DEV_WRITE_COALESCING); |
c2c087e6 DW |
4812 | vol = &dev->vol; |
4813 | vol->migr_state = 0; | |
1484e727 | 4814 | set_migr_type(dev, MIGR_INIT); |
3960e579 | 4815 | vol->dirty = !info->state; |
f8f603f1 | 4816 | vol->curr_migr_unit = 0; |
238c0a71 | 4817 | map = get_imsm_map(dev, MAP_0); |
5551b113 CA |
4818 | set_pba_of_lba0(map, super->create_offset); |
4819 | set_blocks_per_member(map, info_to_blocks_per_member(info, size)); | |
ef6ffade | 4820 | map->blocks_per_strip = __cpu_to_le16(info_to_blocks_per_strip(info)); |
0556e1a2 | 4821 | map->failed_disk_num = ~0; |
bf4442ab | 4822 | if (info->level > 0) |
fffaf1ff N |
4823 | map->map_state = (info->state ? IMSM_T_STATE_NORMAL |
4824 | : IMSM_T_STATE_UNINITIALIZED); | |
bf4442ab AK |
4825 | else |
4826 | map->map_state = info->failed_disks ? IMSM_T_STATE_FAILED : | |
4827 | IMSM_T_STATE_NORMAL; | |
252d23c0 | 4828 | map->ddf = 1; |
ef6ffade DW |
4829 | |
4830 | if (info->level == 1 && info->raid_disks > 2) { | |
38950822 AW |
4831 | free(dev); |
4832 | free(dv); | |
7a862a02 | 4833 | pr_err("imsm does not support more than 2 disksin a raid1 volume\n"); |
ef6ffade DW |
4834 | return 0; |
4835 | } | |
81062a36 DW |
4836 | |
4837 | map->raid_level = info->level; | |
4d1313e9 | 4838 | if (info->level == 10) { |
c2c087e6 | 4839 | map->raid_level = 1; |
4d1313e9 | 4840 | map->num_domains = info->raid_disks / 2; |
81062a36 DW |
4841 | } else if (info->level == 1) |
4842 | map->num_domains = info->raid_disks; | |
4843 | else | |
ff596308 | 4844 | map->num_domains = 1; |
81062a36 | 4845 | |
5551b113 CA |
4846 | /* info->size is only int so use the 'size' parameter instead */ |
4847 | num_data_stripes = (size * 2) / info_to_blocks_per_strip(info); | |
4848 | num_data_stripes /= map->num_domains; | |
4849 | set_num_data_stripes(map, num_data_stripes); | |
ef6ffade | 4850 | |
c2c087e6 DW |
4851 | map->num_members = info->raid_disks; |
4852 | for (i = 0; i < map->num_members; i++) { | |
4853 | /* initialized in add_to_super */ | |
4eb26970 | 4854 | set_imsm_ord_tbl_ent(map, i, IMSM_ORD_REBUILD); |
c2c087e6 | 4855 | } |
949c47a0 | 4856 | mpb->num_raid_devs++; |
ba2de7ba DW |
4857 | |
4858 | dv->dev = dev; | |
4859 | dv->index = super->current_vol; | |
4860 | dv->next = super->devlist; | |
4861 | super->devlist = dv; | |
c2c087e6 | 4862 | |
4d1313e9 DW |
4863 | imsm_update_version_info(super); |
4864 | ||
c2c087e6 | 4865 | return 1; |
cdddbdbc DW |
4866 | } |
4867 | ||
bf5a934a DW |
4868 | static int init_super_imsm(struct supertype *st, mdu_array_info_t *info, |
4869 | unsigned long long size, char *name, | |
83cd1e97 N |
4870 | char *homehost, int *uuid, |
4871 | unsigned long long data_offset) | |
bf5a934a DW |
4872 | { |
4873 | /* This is primarily called by Create when creating a new array. | |
4874 | * We will then get add_to_super called for each component, and then | |
4875 | * write_init_super called to write it out to each device. | |
4876 | * For IMSM, Create can create on fresh devices or on a pre-existing | |
4877 | * array. | |
4878 | * To create on a pre-existing array a different method will be called. | |
4879 | * This one is just for fresh drives. | |
4880 | */ | |
4881 | struct intel_super *super; | |
4882 | struct imsm_super *mpb; | |
4883 | size_t mpb_size; | |
4d1313e9 | 4884 | char *version; |
bf5a934a | 4885 | |
83cd1e97 | 4886 | if (data_offset != INVALID_SECTORS) { |
ed503f89 | 4887 | pr_err("data-offset not supported by imsm\n"); |
83cd1e97 N |
4888 | return 0; |
4889 | } | |
4890 | ||
bf5a934a | 4891 | if (st->sb) |
83cd1e97 N |
4892 | return init_super_imsm_volume(st, info, size, name, homehost, uuid, |
4893 | data_offset); | |
e683ca88 DW |
4894 | |
4895 | if (info) | |
4896 | mpb_size = disks_to_mpb_size(info->nr_disks); | |
4897 | else | |
4898 | mpb_size = 512; | |
bf5a934a | 4899 | |
49133e57 | 4900 | super = alloc_super(); |
e683ca88 | 4901 | if (super && posix_memalign(&super->buf, 512, mpb_size) != 0) { |
bf5a934a | 4902 | free(super); |
e683ca88 DW |
4903 | super = NULL; |
4904 | } | |
4905 | if (!super) { | |
1ade5cc1 | 4906 | pr_err("could not allocate superblock\n"); |
bf5a934a DW |
4907 | return 0; |
4908 | } | |
17a4eaf9 | 4909 | if (posix_memalign(&super->migr_rec_buf, 512, MIGR_REC_BUF_SIZE) != 0) { |
1ade5cc1 | 4910 | pr_err("could not allocate migr_rec buffer\n"); |
8e59f3d8 AK |
4911 | free(super->buf); |
4912 | free(super); | |
4913 | return 0; | |
4914 | } | |
e683ca88 | 4915 | memset(super->buf, 0, mpb_size); |
ef649044 | 4916 | mpb = super->buf; |
e683ca88 DW |
4917 | mpb->mpb_size = __cpu_to_le32(mpb_size); |
4918 | st->sb = super; | |
4919 | ||
4920 | if (info == NULL) { | |
4921 | /* zeroing superblock */ | |
4922 | return 0; | |
4923 | } | |
bf5a934a | 4924 | |
4d1313e9 DW |
4925 | mpb->attributes = MPB_ATTRIB_CHECKSUM_VERIFY; |
4926 | ||
4927 | version = (char *) mpb->sig; | |
4928 | strcpy(version, MPB_SIGNATURE); | |
4929 | version += strlen(MPB_SIGNATURE); | |
4930 | strcpy(version, MPB_VERSION_RAID0); | |
bf5a934a | 4931 | |
bf5a934a DW |
4932 | return 1; |
4933 | } | |
4934 | ||
0e600426 | 4935 | #ifndef MDASSEMBLE |
f20c3968 | 4936 | static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk, |
bf5a934a DW |
4937 | int fd, char *devname) |
4938 | { | |
4939 | struct intel_super *super = st->sb; | |
d23fe947 | 4940 | struct imsm_super *mpb = super->anchor; |
3960e579 | 4941 | struct imsm_disk *_disk; |
bf5a934a DW |
4942 | struct imsm_dev *dev; |
4943 | struct imsm_map *map; | |
3960e579 | 4944 | struct dl *dl, *df; |
4eb26970 | 4945 | int slot; |
bf5a934a | 4946 | |
949c47a0 | 4947 | dev = get_imsm_dev(super, super->current_vol); |
238c0a71 | 4948 | map = get_imsm_map(dev, MAP_0); |
bf5a934a | 4949 | |
208933a7 | 4950 | if (! (dk->state & (1<<MD_DISK_SYNC))) { |
e7b84f9d | 4951 | pr_err("%s: Cannot add spare devices to IMSM volume\n", |
208933a7 N |
4952 | devname); |
4953 | return 1; | |
4954 | } | |
4955 | ||
efb30e7f DW |
4956 | if (fd == -1) { |
4957 | /* we're doing autolayout so grab the pre-marked (in | |
4958 | * validate_geometry) raid_disk | |
4959 | */ | |
4960 | for (dl = super->disks; dl; dl = dl->next) | |
4961 | if (dl->raiddisk == dk->raid_disk) | |
4962 | break; | |
4963 | } else { | |
4964 | for (dl = super->disks; dl ; dl = dl->next) | |
4965 | if (dl->major == dk->major && | |
4966 | dl->minor == dk->minor) | |
4967 | break; | |
4968 | } | |
d23fe947 | 4969 | |
208933a7 | 4970 | if (!dl) { |
e7b84f9d | 4971 | pr_err("%s is not a member of the same container\n", devname); |
f20c3968 | 4972 | return 1; |
208933a7 | 4973 | } |
bf5a934a | 4974 | |
d23fe947 DW |
4975 | /* add a pristine spare to the metadata */ |
4976 | if (dl->index < 0) { | |
4977 | dl->index = super->anchor->num_disks; | |
4978 | super->anchor->num_disks++; | |
4979 | } | |
4eb26970 DW |
4980 | /* Check the device has not already been added */ |
4981 | slot = get_imsm_disk_slot(map, dl->index); | |
4982 | if (slot >= 0 && | |
238c0a71 | 4983 | (get_imsm_ord_tbl_ent(dev, slot, MAP_X) & IMSM_ORD_REBUILD) == 0) { |
e7b84f9d | 4984 | pr_err("%s has been included in this array twice\n", |
4eb26970 DW |
4985 | devname); |
4986 | return 1; | |
4987 | } | |
656b6b5a | 4988 | set_imsm_ord_tbl_ent(map, dk->raid_disk, dl->index); |
ee5aad5a | 4989 | dl->disk.status = CONFIGURED_DISK; |
d23fe947 | 4990 | |
3960e579 DW |
4991 | /* update size of 'missing' disks to be at least as large as the |
4992 | * largest acitve member (we only have dummy missing disks when | |
4993 | * creating the first volume) | |
4994 | */ | |
4995 | if (super->current_vol == 0) { | |
4996 | for (df = super->missing; df; df = df->next) { | |
5551b113 CA |
4997 | if (total_blocks(&dl->disk) > total_blocks(&df->disk)) |
4998 | set_total_blocks(&df->disk, total_blocks(&dl->disk)); | |
3960e579 DW |
4999 | _disk = __get_imsm_disk(mpb, df->index); |
5000 | *_disk = df->disk; | |
5001 | } | |
5002 | } | |
5003 | ||
5004 | /* refresh unset/failed slots to point to valid 'missing' entries */ | |
5005 | for (df = super->missing; df; df = df->next) | |
5006 | for (slot = 0; slot < mpb->num_disks; slot++) { | |
238c0a71 | 5007 | __u32 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X); |
3960e579 DW |
5008 | |
5009 | if ((ord & IMSM_ORD_REBUILD) == 0) | |
5010 | continue; | |
5011 | set_imsm_ord_tbl_ent(map, slot, df->index | IMSM_ORD_REBUILD); | |
1ace8403 | 5012 | if (is_gen_migration(dev)) { |
238c0a71 AK |
5013 | struct imsm_map *map2 = get_imsm_map(dev, |
5014 | MAP_1); | |
0a108d63 AK |
5015 | int slot2 = get_imsm_disk_slot(map2, df->index); |
5016 | if ((slot2 < map2->num_members) && | |
5017 | (slot2 >= 0)) { | |
1ace8403 | 5018 | __u32 ord2 = get_imsm_ord_tbl_ent(dev, |
238c0a71 AK |
5019 | slot2, |
5020 | MAP_1); | |
1ace8403 AK |
5021 | if ((unsigned)df->index == |
5022 | ord_to_idx(ord2)) | |
5023 | set_imsm_ord_tbl_ent(map2, | |
0a108d63 | 5024 | slot2, |
1ace8403 AK |
5025 | df->index | |
5026 | IMSM_ORD_REBUILD); | |
5027 | } | |
5028 | } | |
3960e579 DW |
5029 | dprintf("set slot:%d to missing disk:%d\n", slot, df->index); |
5030 | break; | |
5031 | } | |
5032 | ||
d23fe947 DW |
5033 | /* if we are creating the first raid device update the family number */ |
5034 | if (super->current_vol == 0) { | |
5035 | __u32 sum; | |
5036 | struct imsm_dev *_dev = __get_imsm_dev(mpb, 0); | |
d23fe947 | 5037 | |
3960e579 | 5038 | _disk = __get_imsm_disk(mpb, dl->index); |
791b666a | 5039 | if (!_dev || !_disk) { |
e7b84f9d | 5040 | pr_err("BUG mpb setup error\n"); |
791b666a AW |
5041 | return 1; |
5042 | } | |
d23fe947 DW |
5043 | *_dev = *dev; |
5044 | *_disk = dl->disk; | |
148acb7b DW |
5045 | sum = random32(); |
5046 | sum += __gen_imsm_checksum(mpb); | |
d23fe947 | 5047 | mpb->family_num = __cpu_to_le32(sum); |
148acb7b | 5048 | mpb->orig_family_num = mpb->family_num; |
d23fe947 | 5049 | } |
ca0748fa | 5050 | super->current_disk = dl; |
f20c3968 | 5051 | return 0; |
bf5a934a DW |
5052 | } |
5053 | ||
a8619d23 AK |
5054 | /* mark_spare() |
5055 | * Function marks disk as spare and restores disk serial | |
5056 | * in case it was previously marked as failed by takeover operation | |
5057 | * reruns: | |
5058 | * -1 : critical error | |
5059 | * 0 : disk is marked as spare but serial is not set | |
5060 | * 1 : success | |
5061 | */ | |
5062 | int mark_spare(struct dl *disk) | |
5063 | { | |
5064 | __u8 serial[MAX_RAID_SERIAL_LEN]; | |
5065 | int ret_val = -1; | |
5066 | ||
5067 | if (!disk) | |
5068 | return ret_val; | |
5069 | ||
5070 | ret_val = 0; | |
5071 | if (!imsm_read_serial(disk->fd, NULL, serial)) { | |
5072 | /* Restore disk serial number, because takeover marks disk | |
5073 | * as failed and adds to serial ':0' before it becomes | |
5074 | * a spare disk. | |
5075 | */ | |
5076 | serialcpy(disk->serial, serial); | |
5077 | serialcpy(disk->disk.serial, serial); | |
5078 | ret_val = 1; | |
5079 | } | |
5080 | disk->disk.status = SPARE_DISK; | |
5081 | disk->index = -1; | |
5082 | ||
5083 | return ret_val; | |
5084 | } | |
88654014 | 5085 | |
f20c3968 | 5086 | static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk, |
72ca9bcf N |
5087 | int fd, char *devname, |
5088 | unsigned long long data_offset) | |
cdddbdbc | 5089 | { |
c2c087e6 | 5090 | struct intel_super *super = st->sb; |
c2c087e6 DW |
5091 | struct dl *dd; |
5092 | unsigned long long size; | |
f2f27e63 | 5093 | __u32 id; |
c2c087e6 DW |
5094 | int rv; |
5095 | struct stat stb; | |
5096 | ||
88654014 LM |
5097 | /* If we are on an RAID enabled platform check that the disk is |
5098 | * attached to the raid controller. | |
5099 | * We do not need to test disks attachment for container based additions, | |
5100 | * they shall be already tested when container was created/assembled. | |
88c32bb1 | 5101 | */ |
d424212e | 5102 | rv = find_intel_hba_capability(fd, super, devname); |
f2f5c343 | 5103 | /* no orom/efi or non-intel hba of the disk */ |
f0f5a016 LM |
5104 | if (rv != 0) { |
5105 | dprintf("capability: %p fd: %d ret: %d\n", | |
5106 | super->orom, fd, rv); | |
5107 | return 1; | |
88c32bb1 DW |
5108 | } |
5109 | ||
f20c3968 DW |
5110 | if (super->current_vol >= 0) |
5111 | return add_to_super_imsm_volume(st, dk, fd, devname); | |
bf5a934a | 5112 | |
c2c087e6 | 5113 | fstat(fd, &stb); |
503975b9 | 5114 | dd = xcalloc(sizeof(*dd), 1); |
c2c087e6 DW |
5115 | dd->major = major(stb.st_rdev); |
5116 | dd->minor = minor(stb.st_rdev); | |
503975b9 | 5117 | dd->devname = devname ? xstrdup(devname) : NULL; |
c2c087e6 | 5118 | dd->fd = fd; |
689c9bf3 | 5119 | dd->e = NULL; |
1a64be56 | 5120 | dd->action = DISK_ADD; |
c2c087e6 | 5121 | rv = imsm_read_serial(fd, devname, dd->serial); |
32ba9157 | 5122 | if (rv) { |
e7b84f9d | 5123 | pr_err("failed to retrieve scsi serial, aborting\n"); |
949c47a0 | 5124 | free(dd); |
0030e8d6 | 5125 | abort(); |
c2c087e6 DW |
5126 | } |
5127 | ||
c2c087e6 | 5128 | get_dev_size(fd, NULL, &size); |
71e5411e PB |
5129 | /* clear migr_rec when adding disk to container */ |
5130 | memset(super->migr_rec_buf, 0, MIGR_REC_BUF_SIZE); | |
5131 | if (lseek64(fd, size - MIGR_REC_POSITION, SEEK_SET) >= 0) { | |
5132 | if (write(fd, super->migr_rec_buf, | |
5133 | MIGR_REC_BUF_SIZE) != MIGR_REC_BUF_SIZE) | |
5134 | perror("Write migr_rec failed"); | |
5135 | } | |
5136 | ||
c2c087e6 | 5137 | size /= 512; |
1f24f035 | 5138 | serialcpy(dd->disk.serial, dd->serial); |
5551b113 CA |
5139 | set_total_blocks(&dd->disk, size); |
5140 | if (__le32_to_cpu(dd->disk.total_blocks_hi) > 0) { | |
5141 | struct imsm_super *mpb = super->anchor; | |
5142 | mpb->attributes |= MPB_ATTRIB_2TB_DISK; | |
5143 | } | |
a8619d23 | 5144 | mark_spare(dd); |
c2c087e6 | 5145 | if (sysfs_disk_to_scsi_id(fd, &id) == 0) |
b9f594fe | 5146 | dd->disk.scsi_id = __cpu_to_le32(id); |
c2c087e6 | 5147 | else |
b9f594fe | 5148 | dd->disk.scsi_id = __cpu_to_le32(0); |
43dad3d6 DW |
5149 | |
5150 | if (st->update_tail) { | |
1a64be56 LM |
5151 | dd->next = super->disk_mgmt_list; |
5152 | super->disk_mgmt_list = dd; | |
43dad3d6 DW |
5153 | } else { |
5154 | dd->next = super->disks; | |
5155 | super->disks = dd; | |
ceaf0ee1 | 5156 | super->updates_pending++; |
43dad3d6 | 5157 | } |
f20c3968 DW |
5158 | |
5159 | return 0; | |
cdddbdbc DW |
5160 | } |
5161 | ||
1a64be56 LM |
5162 | static int remove_from_super_imsm(struct supertype *st, mdu_disk_info_t *dk) |
5163 | { | |
5164 | struct intel_super *super = st->sb; | |
5165 | struct dl *dd; | |
5166 | ||
5167 | /* remove from super works only in mdmon - for communication | |
5168 | * manager - monitor. Check if communication memory buffer | |
5169 | * is prepared. | |
5170 | */ | |
5171 | if (!st->update_tail) { | |
1ade5cc1 | 5172 | pr_err("shall be used in mdmon context only\n"); |
1a64be56 LM |
5173 | return 1; |
5174 | } | |
503975b9 | 5175 | dd = xcalloc(1, sizeof(*dd)); |
1a64be56 LM |
5176 | dd->major = dk->major; |
5177 | dd->minor = dk->minor; | |
1a64be56 | 5178 | dd->fd = -1; |
a8619d23 | 5179 | mark_spare(dd); |
1a64be56 LM |
5180 | dd->action = DISK_REMOVE; |
5181 | ||
5182 | dd->next = super->disk_mgmt_list; | |
5183 | super->disk_mgmt_list = dd; | |
5184 | ||
1a64be56 LM |
5185 | return 0; |
5186 | } | |
5187 | ||
f796af5d DW |
5188 | static int store_imsm_mpb(int fd, struct imsm_super *mpb); |
5189 | ||
5190 | static union { | |
5191 | char buf[512]; | |
5192 | struct imsm_super anchor; | |
5193 | } spare_record __attribute__ ((aligned(512))); | |
c2c087e6 | 5194 | |
d23fe947 DW |
5195 | /* spare records have their own family number and do not have any defined raid |
5196 | * devices | |
5197 | */ | |
5198 | static int write_super_imsm_spares(struct intel_super *super, int doclose) | |
5199 | { | |
d23fe947 | 5200 | struct imsm_super *mpb = super->anchor; |
f796af5d | 5201 | struct imsm_super *spare = &spare_record.anchor; |
d23fe947 DW |
5202 | __u32 sum; |
5203 | struct dl *d; | |
5204 | ||
68641cdb JS |
5205 | spare->mpb_size = __cpu_to_le32(sizeof(struct imsm_super)); |
5206 | spare->generation_num = __cpu_to_le32(1UL); | |
f796af5d | 5207 | spare->attributes = MPB_ATTRIB_CHECKSUM_VERIFY; |
68641cdb JS |
5208 | spare->num_disks = 1; |
5209 | spare->num_raid_devs = 0; | |
5210 | spare->cache_size = mpb->cache_size; | |
5211 | spare->pwr_cycle_count = __cpu_to_le32(1); | |
f796af5d DW |
5212 | |
5213 | snprintf((char *) spare->sig, MAX_SIGNATURE_LENGTH, | |
5214 | MPB_SIGNATURE MPB_VERSION_RAID0); | |
d23fe947 DW |
5215 | |
5216 | for (d = super->disks; d; d = d->next) { | |
8796fdc4 | 5217 | if (d->index != -1) |
d23fe947 DW |
5218 | continue; |
5219 | ||
f796af5d | 5220 | spare->disk[0] = d->disk; |
027c374f CA |
5221 | if (__le32_to_cpu(d->disk.total_blocks_hi) > 0) |
5222 | spare->attributes |= MPB_ATTRIB_2TB_DISK; | |
5223 | ||
f796af5d DW |
5224 | sum = __gen_imsm_checksum(spare); |
5225 | spare->family_num = __cpu_to_le32(sum); | |
5226 | spare->orig_family_num = 0; | |
5227 | sum = __gen_imsm_checksum(spare); | |
5228 | spare->check_sum = __cpu_to_le32(sum); | |
d23fe947 | 5229 | |
f796af5d | 5230 | if (store_imsm_mpb(d->fd, spare)) { |
1ade5cc1 N |
5231 | pr_err("failed for device %d:%d %s\n", |
5232 | d->major, d->minor, strerror(errno)); | |
e74255d9 | 5233 | return 1; |
d23fe947 DW |
5234 | } |
5235 | if (doclose) { | |
5236 | close(d->fd); | |
5237 | d->fd = -1; | |
5238 | } | |
5239 | } | |
5240 | ||
e74255d9 | 5241 | return 0; |
d23fe947 DW |
5242 | } |
5243 | ||
36988a3d | 5244 | static int write_super_imsm(struct supertype *st, int doclose) |
cdddbdbc | 5245 | { |
36988a3d | 5246 | struct intel_super *super = st->sb; |
949c47a0 | 5247 | struct imsm_super *mpb = super->anchor; |
c2c087e6 DW |
5248 | struct dl *d; |
5249 | __u32 generation; | |
5250 | __u32 sum; | |
d23fe947 | 5251 | int spares = 0; |
949c47a0 | 5252 | int i; |
a48ac0a8 | 5253 | __u32 mpb_size = sizeof(struct imsm_super) - sizeof(struct imsm_disk); |
36988a3d | 5254 | int num_disks = 0; |
146c6260 | 5255 | int clear_migration_record = 1; |
cdddbdbc | 5256 | |
c2c087e6 DW |
5257 | /* 'generation' is incremented everytime the metadata is written */ |
5258 | generation = __le32_to_cpu(mpb->generation_num); | |
5259 | generation++; | |
5260 | mpb->generation_num = __cpu_to_le32(generation); | |
5261 | ||
148acb7b DW |
5262 | /* fix up cases where previous mdadm releases failed to set |
5263 | * orig_family_num | |
5264 | */ | |
5265 | if (mpb->orig_family_num == 0) | |
5266 | mpb->orig_family_num = mpb->family_num; | |
5267 | ||
d23fe947 | 5268 | for (d = super->disks; d; d = d->next) { |
8796fdc4 | 5269 | if (d->index == -1) |
d23fe947 | 5270 | spares++; |
36988a3d | 5271 | else { |
d23fe947 | 5272 | mpb->disk[d->index] = d->disk; |
36988a3d AK |
5273 | num_disks++; |
5274 | } | |
d23fe947 | 5275 | } |
36988a3d | 5276 | for (d = super->missing; d; d = d->next) { |
47ee5a45 | 5277 | mpb->disk[d->index] = d->disk; |
36988a3d AK |
5278 | num_disks++; |
5279 | } | |
5280 | mpb->num_disks = num_disks; | |
5281 | mpb_size += sizeof(struct imsm_disk) * mpb->num_disks; | |
b9f594fe | 5282 | |
949c47a0 DW |
5283 | for (i = 0; i < mpb->num_raid_devs; i++) { |
5284 | struct imsm_dev *dev = __get_imsm_dev(mpb, i); | |
36988a3d AK |
5285 | struct imsm_dev *dev2 = get_imsm_dev(super, i); |
5286 | if (dev && dev2) { | |
5287 | imsm_copy_dev(dev, dev2); | |
5288 | mpb_size += sizeof_imsm_dev(dev, 0); | |
5289 | } | |
146c6260 AK |
5290 | if (is_gen_migration(dev2)) |
5291 | clear_migration_record = 0; | |
949c47a0 | 5292 | } |
a48ac0a8 DW |
5293 | mpb_size += __le32_to_cpu(mpb->bbm_log_size); |
5294 | mpb->mpb_size = __cpu_to_le32(mpb_size); | |
949c47a0 | 5295 | |
c2c087e6 | 5296 | /* recalculate checksum */ |
949c47a0 | 5297 | sum = __gen_imsm_checksum(mpb); |
c2c087e6 DW |
5298 | mpb->check_sum = __cpu_to_le32(sum); |
5299 | ||
51d83f5d AK |
5300 | if (super->clean_migration_record_by_mdmon) { |
5301 | clear_migration_record = 1; | |
5302 | super->clean_migration_record_by_mdmon = 0; | |
5303 | } | |
146c6260 | 5304 | if (clear_migration_record) |
17a4eaf9 | 5305 | memset(super->migr_rec_buf, 0, MIGR_REC_BUF_SIZE); |
146c6260 | 5306 | |
d23fe947 | 5307 | /* write the mpb for disks that compose raid devices */ |
c2c087e6 | 5308 | for (d = super->disks; d ; d = d->next) { |
86c54047 | 5309 | if (d->index < 0 || is_failed(&d->disk)) |
d23fe947 | 5310 | continue; |
30602f53 | 5311 | |
146c6260 AK |
5312 | if (clear_migration_record) { |
5313 | unsigned long long dsize; | |
5314 | ||
5315 | get_dev_size(d->fd, NULL, &dsize); | |
5316 | if (lseek64(d->fd, dsize - 512, SEEK_SET) >= 0) { | |
17a4eaf9 AK |
5317 | if (write(d->fd, super->migr_rec_buf, |
5318 | MIGR_REC_BUF_SIZE) != MIGR_REC_BUF_SIZE) | |
9e2d750d | 5319 | perror("Write migr_rec failed"); |
146c6260 AK |
5320 | } |
5321 | } | |
51d83f5d AK |
5322 | |
5323 | if (store_imsm_mpb(d->fd, mpb)) | |
5324 | fprintf(stderr, | |
1ade5cc1 N |
5325 | "failed for device %d:%d (fd: %d)%s\n", |
5326 | d->major, d->minor, | |
51d83f5d AK |
5327 | d->fd, strerror(errno)); |
5328 | ||
c2c087e6 DW |
5329 | if (doclose) { |
5330 | close(d->fd); | |
5331 | d->fd = -1; | |
5332 | } | |
5333 | } | |
5334 | ||
d23fe947 DW |
5335 | if (spares) |
5336 | return write_super_imsm_spares(super, doclose); | |
5337 | ||
e74255d9 | 5338 | return 0; |
c2c087e6 DW |
5339 | } |
5340 | ||
9b1fb677 | 5341 | static int create_array(struct supertype *st, int dev_idx) |
43dad3d6 DW |
5342 | { |
5343 | size_t len; | |
5344 | struct imsm_update_create_array *u; | |
5345 | struct intel_super *super = st->sb; | |
9b1fb677 | 5346 | struct imsm_dev *dev = get_imsm_dev(super, dev_idx); |
238c0a71 | 5347 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
54c2c1ea DW |
5348 | struct disk_info *inf; |
5349 | struct imsm_disk *disk; | |
5350 | int i; | |
43dad3d6 | 5351 | |
54c2c1ea DW |
5352 | len = sizeof(*u) - sizeof(*dev) + sizeof_imsm_dev(dev, 0) + |
5353 | sizeof(*inf) * map->num_members; | |
503975b9 | 5354 | u = xmalloc(len); |
43dad3d6 | 5355 | u->type = update_create_array; |
9b1fb677 | 5356 | u->dev_idx = dev_idx; |
43dad3d6 | 5357 | imsm_copy_dev(&u->dev, dev); |
54c2c1ea DW |
5358 | inf = get_disk_info(u); |
5359 | for (i = 0; i < map->num_members; i++) { | |
238c0a71 | 5360 | int idx = get_imsm_disk_idx(dev, i, MAP_X); |
9b1fb677 | 5361 | |
54c2c1ea | 5362 | disk = get_imsm_disk(super, idx); |
1ca5c8e0 N |
5363 | if (!disk) |
5364 | disk = get_imsm_missing(super, idx); | |
54c2c1ea DW |
5365 | serialcpy(inf[i].serial, disk->serial); |
5366 | } | |
43dad3d6 DW |
5367 | append_metadata_update(st, u, len); |
5368 | ||
5369 | return 0; | |
5370 | } | |
5371 | ||
1a64be56 | 5372 | static int mgmt_disk(struct supertype *st) |
43dad3d6 DW |
5373 | { |
5374 | struct intel_super *super = st->sb; | |
5375 | size_t len; | |
1a64be56 | 5376 | struct imsm_update_add_remove_disk *u; |
43dad3d6 | 5377 | |
1a64be56 | 5378 | if (!super->disk_mgmt_list) |
43dad3d6 DW |
5379 | return 0; |
5380 | ||
5381 | len = sizeof(*u); | |
503975b9 | 5382 | u = xmalloc(len); |
1a64be56 | 5383 | u->type = update_add_remove_disk; |
43dad3d6 DW |
5384 | append_metadata_update(st, u, len); |
5385 | ||
5386 | return 0; | |
5387 | } | |
5388 | ||
c2c087e6 DW |
5389 | static int write_init_super_imsm(struct supertype *st) |
5390 | { | |
9b1fb677 DW |
5391 | struct intel_super *super = st->sb; |
5392 | int current_vol = super->current_vol; | |
5393 | ||
5394 | /* we are done with current_vol reset it to point st at the container */ | |
5395 | super->current_vol = -1; | |
5396 | ||
8273f55e | 5397 | if (st->update_tail) { |
43dad3d6 DW |
5398 | /* queue the recently created array / added disk |
5399 | * as a metadata update */ | |
43dad3d6 | 5400 | int rv; |
8273f55e | 5401 | |
43dad3d6 | 5402 | /* determine if we are creating a volume or adding a disk */ |
9b1fb677 | 5403 | if (current_vol < 0) { |
1a64be56 LM |
5404 | /* in the mgmt (add/remove) disk case we are running |
5405 | * in mdmon context, so don't close fd's | |
43dad3d6 | 5406 | */ |
1a64be56 | 5407 | return mgmt_disk(st); |
43dad3d6 | 5408 | } else |
9b1fb677 | 5409 | rv = create_array(st, current_vol); |
8273f55e | 5410 | |
43dad3d6 | 5411 | return rv; |
d682f344 N |
5412 | } else { |
5413 | struct dl *d; | |
5414 | for (d = super->disks; d; d = d->next) | |
ba728be7 | 5415 | Kill(d->devname, NULL, 0, -1, 1); |
36988a3d | 5416 | return write_super_imsm(st, 1); |
d682f344 | 5417 | } |
cdddbdbc | 5418 | } |
0e600426 | 5419 | #endif |
cdddbdbc | 5420 | |
e683ca88 | 5421 | static int store_super_imsm(struct supertype *st, int fd) |
cdddbdbc | 5422 | { |
e683ca88 DW |
5423 | struct intel_super *super = st->sb; |
5424 | struct imsm_super *mpb = super ? super->anchor : NULL; | |
551c80c1 | 5425 | |
e683ca88 | 5426 | if (!mpb) |
ad97895e DW |
5427 | return 1; |
5428 | ||
1799c9e8 | 5429 | #ifndef MDASSEMBLE |
e683ca88 | 5430 | return store_imsm_mpb(fd, mpb); |
1799c9e8 N |
5431 | #else |
5432 | return 1; | |
5433 | #endif | |
cdddbdbc DW |
5434 | } |
5435 | ||
0e600426 N |
5436 | static int imsm_bbm_log_size(struct imsm_super *mpb) |
5437 | { | |
5438 | return __le32_to_cpu(mpb->bbm_log_size); | |
5439 | } | |
5440 | ||
5441 | #ifndef MDASSEMBLE | |
cdddbdbc DW |
5442 | static int validate_geometry_imsm_container(struct supertype *st, int level, |
5443 | int layout, int raiddisks, int chunk, | |
af4348dd N |
5444 | unsigned long long size, |
5445 | unsigned long long data_offset, | |
5446 | char *dev, | |
2c514b71 NB |
5447 | unsigned long long *freesize, |
5448 | int verbose) | |
cdddbdbc | 5449 | { |
c2c087e6 DW |
5450 | int fd; |
5451 | unsigned long long ldsize; | |
f2f5c343 LM |
5452 | struct intel_super *super=NULL; |
5453 | int rv = 0; | |
cdddbdbc | 5454 | |
c2c087e6 DW |
5455 | if (level != LEVEL_CONTAINER) |
5456 | return 0; | |
5457 | if (!dev) | |
5458 | return 1; | |
5459 | ||
5460 | fd = open(dev, O_RDONLY|O_EXCL, 0); | |
5461 | if (fd < 0) { | |
ba728be7 | 5462 | if (verbose > 0) |
e7b84f9d | 5463 | pr_err("imsm: Cannot open %s: %s\n", |
2c514b71 | 5464 | dev, strerror(errno)); |
c2c087e6 DW |
5465 | return 0; |
5466 | } | |
5467 | if (!get_dev_size(fd, dev, &ldsize)) { | |
5468 | close(fd); | |
5469 | return 0; | |
5470 | } | |
f2f5c343 LM |
5471 | |
5472 | /* capabilities retrieve could be possible | |
5473 | * note that there is no fd for the disks in array. | |
5474 | */ | |
5475 | super = alloc_super(); | |
ba728be7 | 5476 | rv = find_intel_hba_capability(fd, super, verbose > 0 ? dev : NULL); |
f2f5c343 LM |
5477 | if (rv != 0) { |
5478 | #if DEBUG | |
5479 | char str[256]; | |
5480 | fd2devname(fd, str); | |
1ade5cc1 | 5481 | dprintf("fd: %d %s orom: %p rv: %d raiddisk: %d\n", |
f2f5c343 LM |
5482 | fd, str, super->orom, rv, raiddisks); |
5483 | #endif | |
5484 | /* no orom/efi or non-intel hba of the disk */ | |
5485 | close(fd); | |
5486 | free_imsm(super); | |
5487 | return 0; | |
5488 | } | |
c2c087e6 | 5489 | close(fd); |
9126b9a8 CA |
5490 | if (super->orom) { |
5491 | if (raiddisks > super->orom->tds) { | |
5492 | if (verbose) | |
7a862a02 | 5493 | pr_err("%d exceeds maximum number of platform supported disks: %d\n", |
9126b9a8 CA |
5494 | raiddisks, super->orom->tds); |
5495 | free_imsm(super); | |
5496 | return 0; | |
5497 | } | |
5498 | if ((super->orom->attr & IMSM_OROM_ATTR_2TB_DISK) == 0 && | |
5499 | (ldsize >> 9) >> 32 > 0) { | |
5500 | if (verbose) | |
e7b84f9d | 5501 | pr_err("%s exceeds maximum platform supported size\n", dev); |
9126b9a8 CA |
5502 | free_imsm(super); |
5503 | return 0; | |
5504 | } | |
f2f5c343 | 5505 | } |
c2c087e6 | 5506 | |
af4348dd | 5507 | *freesize = avail_size_imsm(st, ldsize >> 9, data_offset); |
f2f5c343 | 5508 | free_imsm(super); |
c2c087e6 DW |
5509 | |
5510 | return 1; | |
cdddbdbc DW |
5511 | } |
5512 | ||
0dcecb2e DW |
5513 | static unsigned long long find_size(struct extent *e, int *idx, int num_extents) |
5514 | { | |
5515 | const unsigned long long base_start = e[*idx].start; | |
5516 | unsigned long long end = base_start + e[*idx].size; | |
5517 | int i; | |
5518 | ||
5519 | if (base_start == end) | |
5520 | return 0; | |
5521 | ||
5522 | *idx = *idx + 1; | |
5523 | for (i = *idx; i < num_extents; i++) { | |
5524 | /* extend overlapping extents */ | |
5525 | if (e[i].start >= base_start && | |
5526 | e[i].start <= end) { | |
5527 | if (e[i].size == 0) | |
5528 | return 0; | |
5529 | if (e[i].start + e[i].size > end) | |
5530 | end = e[i].start + e[i].size; | |
5531 | } else if (e[i].start > end) { | |
5532 | *idx = i; | |
5533 | break; | |
5534 | } | |
5535 | } | |
5536 | ||
5537 | return end - base_start; | |
5538 | } | |
5539 | ||
5540 | static unsigned long long merge_extents(struct intel_super *super, int sum_extents) | |
5541 | { | |
5542 | /* build a composite disk with all known extents and generate a new | |
5543 | * 'maxsize' given the "all disks in an array must share a common start | |
5544 | * offset" constraint | |
5545 | */ | |
503975b9 | 5546 | struct extent *e = xcalloc(sum_extents, sizeof(*e)); |
0dcecb2e DW |
5547 | struct dl *dl; |
5548 | int i, j; | |
5549 | int start_extent; | |
5550 | unsigned long long pos; | |
b9d77223 | 5551 | unsigned long long start = 0; |
0dcecb2e DW |
5552 | unsigned long long maxsize; |
5553 | unsigned long reserve; | |
5554 | ||
0dcecb2e DW |
5555 | /* coalesce and sort all extents. also, check to see if we need to |
5556 | * reserve space between member arrays | |
5557 | */ | |
5558 | j = 0; | |
5559 | for (dl = super->disks; dl; dl = dl->next) { | |
5560 | if (!dl->e) | |
5561 | continue; | |
5562 | for (i = 0; i < dl->extent_cnt; i++) | |
5563 | e[j++] = dl->e[i]; | |
5564 | } | |
5565 | qsort(e, sum_extents, sizeof(*e), cmp_extent); | |
5566 | ||
5567 | /* merge extents */ | |
5568 | i = 0; | |
5569 | j = 0; | |
5570 | while (i < sum_extents) { | |
5571 | e[j].start = e[i].start; | |
5572 | e[j].size = find_size(e, &i, sum_extents); | |
5573 | j++; | |
5574 | if (e[j-1].size == 0) | |
5575 | break; | |
5576 | } | |
5577 | ||
5578 | pos = 0; | |
5579 | maxsize = 0; | |
5580 | start_extent = 0; | |
5581 | i = 0; | |
5582 | do { | |
5583 | unsigned long long esize; | |
5584 | ||
5585 | esize = e[i].start - pos; | |
5586 | if (esize >= maxsize) { | |
5587 | maxsize = esize; | |
5588 | start = pos; | |
5589 | start_extent = i; | |
5590 | } | |
5591 | pos = e[i].start + e[i].size; | |
5592 | i++; | |
5593 | } while (e[i-1].size); | |
5594 | free(e); | |
5595 | ||
a7dd165b DW |
5596 | if (maxsize == 0) |
5597 | return 0; | |
5598 | ||
5599 | /* FIXME assumes volume at offset 0 is the first volume in a | |
5600 | * container | |
5601 | */ | |
0dcecb2e DW |
5602 | if (start_extent > 0) |
5603 | reserve = IMSM_RESERVED_SECTORS; /* gap between raid regions */ | |
5604 | else | |
5605 | reserve = 0; | |
5606 | ||
5607 | if (maxsize < reserve) | |
a7dd165b | 5608 | return 0; |
0dcecb2e | 5609 | |
5551b113 | 5610 | super->create_offset = ~((unsigned long long) 0); |
0dcecb2e | 5611 | if (start + reserve > super->create_offset) |
a7dd165b | 5612 | return 0; /* start overflows create_offset */ |
0dcecb2e DW |
5613 | super->create_offset = start + reserve; |
5614 | ||
5615 | return maxsize - reserve; | |
5616 | } | |
5617 | ||
88c32bb1 DW |
5618 | static int is_raid_level_supported(const struct imsm_orom *orom, int level, int raiddisks) |
5619 | { | |
5620 | if (level < 0 || level == 6 || level == 4) | |
5621 | return 0; | |
5622 | ||
5623 | /* if we have an orom prevent invalid raid levels */ | |
5624 | if (orom) | |
5625 | switch (level) { | |
5626 | case 0: return imsm_orom_has_raid0(orom); | |
5627 | case 1: | |
5628 | if (raiddisks > 2) | |
5629 | return imsm_orom_has_raid1e(orom); | |
1c556e92 DW |
5630 | return imsm_orom_has_raid1(orom) && raiddisks == 2; |
5631 | case 10: return imsm_orom_has_raid10(orom) && raiddisks == 4; | |
5632 | case 5: return imsm_orom_has_raid5(orom) && raiddisks > 2; | |
88c32bb1 DW |
5633 | } |
5634 | else | |
5635 | return 1; /* not on an Intel RAID platform so anything goes */ | |
5636 | ||
5637 | return 0; | |
5638 | } | |
5639 | ||
ca9de185 LM |
5640 | static int |
5641 | active_arrays_by_format(char *name, char* hba, struct md_list **devlist, | |
5642 | int dpa, int verbose) | |
5643 | { | |
5644 | struct mdstat_ent *mdstat = mdstat_read(0, 0); | |
5645 | struct mdstat_ent *memb = NULL; | |
5646 | int count = 0; | |
5647 | int num = 0; | |
5648 | struct md_list *dv = NULL; | |
5649 | int found; | |
5650 | ||
5651 | for (memb = mdstat ; memb ; memb = memb->next) { | |
5652 | if (memb->metadata_version && | |
5653 | (strncmp(memb->metadata_version, "external:", 9) == 0) && | |
5654 | (strcmp(&memb->metadata_version[9], name) == 0) && | |
5655 | !is_subarray(memb->metadata_version+9) && | |
5656 | memb->members) { | |
5657 | struct dev_member *dev = memb->members; | |
5658 | int fd = -1; | |
5659 | while(dev && (fd < 0)) { | |
503975b9 N |
5660 | char *path = xmalloc(strlen(dev->name) + strlen("/dev/") + 1); |
5661 | num = sprintf(path, "%s%s", "/dev/", dev->name); | |
5662 | if (num > 0) | |
5663 | fd = open(path, O_RDONLY, 0); | |
5664 | if ((num <= 0) || (fd < 0)) { | |
5665 | pr_vrb(": Cannot open %s: %s\n", | |
5666 | dev->name, strerror(errno)); | |
ca9de185 | 5667 | } |
503975b9 | 5668 | free(path); |
ca9de185 LM |
5669 | dev = dev->next; |
5670 | } | |
5671 | found = 0; | |
5672 | if ((fd >= 0) && disk_attached_to_hba(fd, hba)) { | |
5673 | struct mdstat_ent *vol; | |
5674 | for (vol = mdstat ; vol ; vol = vol->next) { | |
5675 | if ((vol->active > 0) && | |
5676 | vol->metadata_version && | |
9581efb1 | 5677 | is_container_member(vol, memb->devnm)) { |
ca9de185 LM |
5678 | found++; |
5679 | count++; | |
5680 | } | |
5681 | } | |
5682 | if (*devlist && (found < dpa)) { | |
503975b9 | 5683 | dv = xcalloc(1, sizeof(*dv)); |
9581efb1 N |
5684 | dv->devname = xmalloc(strlen(memb->devnm) + strlen("/dev/") + 1); |
5685 | sprintf(dv->devname, "%s%s", "/dev/", memb->devnm); | |
503975b9 N |
5686 | dv->found = found; |
5687 | dv->used = 0; | |
5688 | dv->next = *devlist; | |
5689 | *devlist = dv; | |
ca9de185 LM |
5690 | } |
5691 | } | |
5692 | if (fd >= 0) | |
5693 | close(fd); | |
5694 | } | |
5695 | } | |
5696 | free_mdstat(mdstat); | |
5697 | return count; | |
5698 | } | |
5699 | ||
5700 | #ifdef DEBUG_LOOP | |
5701 | static struct md_list* | |
5702 | get_loop_devices(void) | |
5703 | { | |
5704 | int i; | |
5705 | struct md_list *devlist = NULL; | |
5706 | struct md_list *dv = NULL; | |
5707 | ||
5708 | for(i = 0; i < 12; i++) { | |
503975b9 N |
5709 | dv = xcalloc(1, sizeof(*dv)); |
5710 | dv->devname = xmalloc(40); | |
ca9de185 LM |
5711 | sprintf(dv->devname, "/dev/loop%d", i); |
5712 | dv->next = devlist; | |
5713 | devlist = dv; | |
5714 | } | |
5715 | return devlist; | |
5716 | } | |
5717 | #endif | |
5718 | ||
5719 | static struct md_list* | |
5720 | get_devices(const char *hba_path) | |
5721 | { | |
5722 | struct md_list *devlist = NULL; | |
5723 | struct md_list *dv = NULL; | |
5724 | struct dirent *ent; | |
5725 | DIR *dir; | |
5726 | int err = 0; | |
5727 | ||
5728 | #if DEBUG_LOOP | |
5729 | devlist = get_loop_devices(); | |
5730 | return devlist; | |
5731 | #endif | |
5732 | /* scroll through /sys/dev/block looking for devices attached to | |
5733 | * this hba | |
5734 | */ | |
5735 | dir = opendir("/sys/dev/block"); | |
5736 | for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) { | |
5737 | int fd; | |
5738 | char buf[1024]; | |
5739 | int major, minor; | |
5740 | char *path = NULL; | |
5741 | if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2) | |
5742 | continue; | |
5743 | path = devt_to_devpath(makedev(major, minor)); | |
5744 | if (!path) | |
5745 | continue; | |
5746 | if (!path_attached_to_hba(path, hba_path)) { | |
5747 | free(path); | |
5748 | path = NULL; | |
5749 | continue; | |
5750 | } | |
5751 | free(path); | |
5752 | path = NULL; | |
5753 | fd = dev_open(ent->d_name, O_RDONLY); | |
5754 | if (fd >= 0) { | |
5755 | fd2devname(fd, buf); | |
5756 | close(fd); | |
5757 | } else { | |
e7b84f9d | 5758 | pr_err("cannot open device: %s\n", |
ca9de185 LM |
5759 | ent->d_name); |
5760 | continue; | |
5761 | } | |
5762 | ||
503975b9 N |
5763 | dv = xcalloc(1, sizeof(*dv)); |
5764 | dv->devname = xstrdup(buf); | |
ca9de185 LM |
5765 | dv->next = devlist; |
5766 | devlist = dv; | |
5767 | } | |
5768 | if (err) { | |
5769 | while(devlist) { | |
5770 | dv = devlist; | |
5771 | devlist = devlist->next; | |
5772 | free(dv->devname); | |
5773 | free(dv); | |
5774 | } | |
5775 | } | |
562aa102 | 5776 | closedir(dir); |
ca9de185 LM |
5777 | return devlist; |
5778 | } | |
5779 | ||
5780 | static int | |
5781 | count_volumes_list(struct md_list *devlist, char *homehost, | |
5782 | int verbose, int *found) | |
5783 | { | |
5784 | struct md_list *tmpdev; | |
5785 | int count = 0; | |
5786 | struct supertype *st = NULL; | |
5787 | ||
5788 | /* first walk the list of devices to find a consistent set | |
5789 | * that match the criterea, if that is possible. | |
5790 | * We flag the ones we like with 'used'. | |
5791 | */ | |
5792 | *found = 0; | |
5793 | st = match_metadata_desc_imsm("imsm"); | |
5794 | if (st == NULL) { | |
5795 | pr_vrb(": cannot allocate memory for imsm supertype\n"); | |
5796 | return 0; | |
5797 | } | |
5798 | ||
5799 | for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) { | |
5800 | char *devname = tmpdev->devname; | |
5801 | struct stat stb; | |
5802 | struct supertype *tst; | |
5803 | int dfd; | |
5804 | if (tmpdev->used > 1) | |
5805 | continue; | |
5806 | tst = dup_super(st); | |
5807 | if (tst == NULL) { | |
5808 | pr_vrb(": cannot allocate memory for imsm supertype\n"); | |
5809 | goto err_1; | |
5810 | } | |
5811 | tmpdev->container = 0; | |
5812 | dfd = dev_open(devname, O_RDONLY|O_EXCL); | |
5813 | if (dfd < 0) { | |
1ade5cc1 | 5814 | dprintf("cannot open device %s: %s\n", |
ca9de185 LM |
5815 | devname, strerror(errno)); |
5816 | tmpdev->used = 2; | |
5817 | } else if (fstat(dfd, &stb)< 0) { | |
5818 | /* Impossible! */ | |
1ade5cc1 | 5819 | dprintf("fstat failed for %s: %s\n", |
ca9de185 LM |
5820 | devname, strerror(errno)); |
5821 | tmpdev->used = 2; | |
5822 | } else if ((stb.st_mode & S_IFMT) != S_IFBLK) { | |
1ade5cc1 | 5823 | dprintf("%s is not a block device.\n", |
ca9de185 LM |
5824 | devname); |
5825 | tmpdev->used = 2; | |
5826 | } else if (must_be_container(dfd)) { | |
5827 | struct supertype *cst; | |
5828 | cst = super_by_fd(dfd, NULL); | |
5829 | if (cst == NULL) { | |
1ade5cc1 | 5830 | dprintf("cannot recognize container type %s\n", |
ca9de185 LM |
5831 | devname); |
5832 | tmpdev->used = 2; | |
5833 | } else if (tst->ss != st->ss) { | |
1ade5cc1 | 5834 | dprintf("non-imsm container - ignore it: %s\n", |
ca9de185 LM |
5835 | devname); |
5836 | tmpdev->used = 2; | |
5837 | } else if (!tst->ss->load_container || | |
5838 | tst->ss->load_container(tst, dfd, NULL)) | |
5839 | tmpdev->used = 2; | |
5840 | else { | |
5841 | tmpdev->container = 1; | |
5842 | } | |
5843 | if (cst) | |
5844 | cst->ss->free_super(cst); | |
5845 | } else { | |
5846 | tmpdev->st_rdev = stb.st_rdev; | |
5847 | if (tst->ss->load_super(tst,dfd, NULL)) { | |
1ade5cc1 | 5848 | dprintf("no RAID superblock on %s\n", |
ca9de185 LM |
5849 | devname); |
5850 | tmpdev->used = 2; | |
5851 | } else if (tst->ss->compare_super == NULL) { | |
1ade5cc1 | 5852 | dprintf("Cannot assemble %s metadata on %s\n", |
ca9de185 LM |
5853 | tst->ss->name, devname); |
5854 | tmpdev->used = 2; | |
5855 | } | |
5856 | } | |
5857 | if (dfd >= 0) | |
5858 | close(dfd); | |
5859 | if (tmpdev->used == 2 || tmpdev->used == 4) { | |
5860 | /* Ignore unrecognised devices during auto-assembly */ | |
5861 | goto loop; | |
5862 | } | |
5863 | else { | |
5864 | struct mdinfo info; | |
5865 | tst->ss->getinfo_super(tst, &info, NULL); | |
5866 | ||
5867 | if (st->minor_version == -1) | |
5868 | st->minor_version = tst->minor_version; | |
5869 | ||
5870 | if (memcmp(info.uuid, uuid_zero, | |
5871 | sizeof(int[4])) == 0) { | |
5872 | /* this is a floating spare. It cannot define | |
5873 | * an array unless there are no more arrays of | |
5874 | * this type to be found. It can be included | |
5875 | * in an array of this type though. | |
5876 | */ | |
5877 | tmpdev->used = 3; | |
5878 | goto loop; | |
5879 | } | |
5880 | ||
5881 | if (st->ss != tst->ss || | |
5882 | st->minor_version != tst->minor_version || | |
5883 | st->ss->compare_super(st, tst) != 0) { | |
5884 | /* Some mismatch. If exactly one array matches this host, | |
5885 | * we can resolve on that one. | |
5886 | * Or, if we are auto assembling, we just ignore the second | |
5887 | * for now. | |
5888 | */ | |
1ade5cc1 | 5889 | dprintf("superblock on %s doesn't match others - assembly aborted\n", |
ca9de185 LM |
5890 | devname); |
5891 | goto loop; | |
5892 | } | |
5893 | tmpdev->used = 1; | |
5894 | *found = 1; | |
5895 | dprintf("found: devname: %s\n", devname); | |
5896 | } | |
5897 | loop: | |
5898 | if (tst) | |
5899 | tst->ss->free_super(tst); | |
5900 | } | |
5901 | if (*found != 0) { | |
5902 | int err; | |
5903 | if ((err = load_super_imsm_all(st, -1, &st->sb, NULL, devlist, 0)) == 0) { | |
5904 | struct mdinfo *iter, *head = st->ss->container_content(st, NULL); | |
5905 | for (iter = head; iter; iter = iter->next) { | |
5906 | dprintf("content->text_version: %s vol\n", | |
5907 | iter->text_version); | |
5908 | if (iter->array.state & (1<<MD_SB_BLOCK_VOLUME)) { | |
5909 | /* do not assemble arrays with unsupported | |
5910 | configurations */ | |
1ade5cc1 | 5911 | dprintf("Cannot activate member %s.\n", |
ca9de185 LM |
5912 | iter->text_version); |
5913 | } else | |
5914 | count++; | |
5915 | } | |
5916 | sysfs_free(head); | |
5917 | ||
5918 | } else { | |
1ade5cc1 | 5919 | dprintf("No valid super block on device list: err: %d %p\n", |
ca9de185 LM |
5920 | err, st->sb); |
5921 | } | |
5922 | } else { | |
1ade5cc1 | 5923 | dprintf("no more devices to examine\n"); |
ca9de185 LM |
5924 | } |
5925 | ||
5926 | for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) { | |
5927 | if ((tmpdev->used == 1) && (tmpdev->found)) { | |
5928 | if (count) { | |
5929 | if (count < tmpdev->found) | |
5930 | count = 0; | |
5931 | else | |
5932 | count -= tmpdev->found; | |
5933 | } | |
5934 | } | |
5935 | if (tmpdev->used == 1) | |
5936 | tmpdev->used = 4; | |
5937 | } | |
5938 | err_1: | |
5939 | if (st) | |
5940 | st->ss->free_super(st); | |
5941 | return count; | |
5942 | } | |
5943 | ||
ca9de185 | 5944 | static int |
72a45777 | 5945 | count_volumes(struct intel_hba *hba, int dpa, int verbose) |
ca9de185 | 5946 | { |
72a45777 | 5947 | struct sys_dev *idev, *intel_devices = find_intel_devices(); |
ca9de185 | 5948 | int count = 0; |
72a45777 PB |
5949 | const struct orom_entry *entry; |
5950 | struct devid_list *dv, *devid_list; | |
ca9de185 | 5951 | |
72a45777 | 5952 | if (!hba || !hba->path) |
ca9de185 LM |
5953 | return 0; |
5954 | ||
72a45777 PB |
5955 | for (idev = intel_devices; idev; idev = idev->next) { |
5956 | if (strstr(idev->path, hba->path)) | |
5957 | break; | |
5958 | } | |
5959 | ||
5960 | if (!idev || !idev->dev_id) | |
ca9de185 | 5961 | return 0; |
72a45777 PB |
5962 | |
5963 | entry = get_orom_entry_by_device_id(idev->dev_id); | |
5964 | ||
5965 | if (!entry || !entry->devid_list) | |
5966 | return 0; | |
5967 | ||
5968 | devid_list = entry->devid_list; | |
5969 | for (dv = devid_list; dv; dv = dv->next) { | |
72a45777 PB |
5970 | struct md_list *devlist = NULL; |
5971 | struct sys_dev *device = device_by_id(dv->devid); | |
5972 | char *hba_path; | |
5973 | int found = 0; | |
5974 | ||
5975 | if (device) | |
5976 | hba_path = device->path; | |
5977 | else | |
5978 | return 0; | |
5979 | ||
60f0f54d PB |
5980 | /* VMD has one orom entry for all domain, but spanning is not allowed. |
5981 | * VMD arrays should be counted per domain (controller), so skip | |
5982 | * domains that are not the given one. | |
5983 | */ | |
5984 | if ((hba->type == SYS_DEV_VMD) && | |
5985 | (strncmp(device->path, hba->path, strlen(device->path)) != 0)) | |
5986 | continue; | |
5987 | ||
72a45777 PB |
5988 | devlist = get_devices(hba_path); |
5989 | /* if no intel devices return zero volumes */ | |
5990 | if (devlist == NULL) | |
5991 | return 0; | |
5992 | ||
5993 | count += active_arrays_by_format("imsm", hba_path, &devlist, dpa, verbose); | |
5994 | dprintf("path: %s active arrays: %d\n", hba_path, count); | |
5995 | if (devlist == NULL) | |
5996 | return 0; | |
5997 | do { | |
5998 | found = 0; | |
5999 | count += count_volumes_list(devlist, | |
6000 | NULL, | |
6001 | verbose, | |
6002 | &found); | |
6003 | dprintf("found %d count: %d\n", found, count); | |
6004 | } while (found); | |
6005 | ||
6006 | dprintf("path: %s total number of volumes: %d\n", hba_path, count); | |
6007 | ||
6008 | while (devlist) { | |
6009 | struct md_list *dv = devlist; | |
6010 | devlist = devlist->next; | |
6011 | free(dv->devname); | |
6012 | free(dv); | |
6013 | } | |
ca9de185 LM |
6014 | } |
6015 | return count; | |
6016 | } | |
6017 | ||
cd9d1ac7 DW |
6018 | static int imsm_default_chunk(const struct imsm_orom *orom) |
6019 | { | |
6020 | /* up to 512 if the plaform supports it, otherwise the platform max. | |
6021 | * 128 if no platform detected | |
6022 | */ | |
6023 | int fs = max(7, orom ? fls(orom->sss) : 0); | |
6024 | ||
6025 | return min(512, (1 << fs)); | |
6026 | } | |
73408129 | 6027 | |
6592ce37 DW |
6028 | static int |
6029 | validate_geometry_imsm_orom(struct intel_super *super, int level, int layout, | |
2cc699af | 6030 | int raiddisks, int *chunk, unsigned long long size, int verbose) |
6592ce37 | 6031 | { |
660260d0 DW |
6032 | /* check/set platform and metadata limits/defaults */ |
6033 | if (super->orom && raiddisks > super->orom->dpa) { | |
6034 | pr_vrb(": platform supports a maximum of %d disks per array\n", | |
6035 | super->orom->dpa); | |
73408129 LM |
6036 | return 0; |
6037 | } | |
6038 | ||
5d500228 | 6039 | /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */ |
660260d0 | 6040 | if (!is_raid_level_supported(super->orom, level, raiddisks)) { |
6592ce37 DW |
6041 | pr_vrb(": platform does not support raid%d with %d disk%s\n", |
6042 | level, raiddisks, raiddisks > 1 ? "s" : ""); | |
6043 | return 0; | |
6044 | } | |
cd9d1ac7 | 6045 | |
7ccc4cc4 | 6046 | if (*chunk == 0 || *chunk == UnSet) |
cd9d1ac7 DW |
6047 | *chunk = imsm_default_chunk(super->orom); |
6048 | ||
7ccc4cc4 | 6049 | if (super->orom && !imsm_orom_has_chunk(super->orom, *chunk)) { |
7a862a02 | 6050 | pr_vrb(": platform does not support a chunk size of: %d\n", *chunk); |
cd9d1ac7 | 6051 | return 0; |
6592ce37 | 6052 | } |
cd9d1ac7 | 6053 | |
6592ce37 DW |
6054 | if (layout != imsm_level_to_layout(level)) { |
6055 | if (level == 5) | |
6056 | pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n"); | |
6057 | else if (level == 10) | |
6058 | pr_vrb(": imsm raid 10 only supports the n2 layout\n"); | |
6059 | else | |
6060 | pr_vrb(": imsm unknown layout %#x for this raid level %d\n", | |
6061 | layout, level); | |
6062 | return 0; | |
6063 | } | |
2cc699af | 6064 | |
7ccc4cc4 | 6065 | if (super->orom && (super->orom->attr & IMSM_OROM_ATTR_2TB) == 0 && |
2cc699af CA |
6066 | (calc_array_size(level, raiddisks, layout, *chunk, size) >> 32) > 0) { |
6067 | pr_vrb(": platform does not support a volume size over 2TB\n"); | |
6068 | return 0; | |
6069 | } | |
614902f6 | 6070 | |
6592ce37 DW |
6071 | return 1; |
6072 | } | |
6073 | ||
1011e834 | 6074 | /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd |
c2c087e6 DW |
6075 | * FIX ME add ahci details |
6076 | */ | |
8b353278 | 6077 | static int validate_geometry_imsm_volume(struct supertype *st, int level, |
c21e737b | 6078 | int layout, int raiddisks, int *chunk, |
af4348dd N |
6079 | unsigned long long size, |
6080 | unsigned long long data_offset, | |
6081 | char *dev, | |
2c514b71 NB |
6082 | unsigned long long *freesize, |
6083 | int verbose) | |
cdddbdbc | 6084 | { |
c2c087e6 DW |
6085 | struct stat stb; |
6086 | struct intel_super *super = st->sb; | |
b2916f25 | 6087 | struct imsm_super *mpb; |
c2c087e6 DW |
6088 | struct dl *dl; |
6089 | unsigned long long pos = 0; | |
6090 | unsigned long long maxsize; | |
6091 | struct extent *e; | |
6092 | int i; | |
cdddbdbc | 6093 | |
88c32bb1 DW |
6094 | /* We must have the container info already read in. */ |
6095 | if (!super) | |
c2c087e6 DW |
6096 | return 0; |
6097 | ||
b2916f25 JS |
6098 | mpb = super->anchor; |
6099 | ||
2cc699af | 6100 | if (!validate_geometry_imsm_orom(super, level, layout, raiddisks, chunk, size, verbose)) { |
7a862a02 | 6101 | pr_err("RAID gemetry validation failed. Cannot proceed with the action(s).\n"); |
c2c087e6 | 6102 | return 0; |
d54559f0 | 6103 | } |
c2c087e6 DW |
6104 | if (!dev) { |
6105 | /* General test: make sure there is space for | |
2da8544a DW |
6106 | * 'raiddisks' device extents of size 'size' at a given |
6107 | * offset | |
c2c087e6 | 6108 | */ |
e46273eb | 6109 | unsigned long long minsize = size; |
b7528a20 | 6110 | unsigned long long start_offset = MaxSector; |
c2c087e6 DW |
6111 | int dcnt = 0; |
6112 | if (minsize == 0) | |
6113 | minsize = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS; | |
6114 | for (dl = super->disks; dl ; dl = dl->next) { | |
6115 | int found = 0; | |
6116 | ||
bf5a934a | 6117 | pos = 0; |
c2c087e6 DW |
6118 | i = 0; |
6119 | e = get_extents(super, dl); | |
6120 | if (!e) continue; | |
6121 | do { | |
6122 | unsigned long long esize; | |
6123 | esize = e[i].start - pos; | |
6124 | if (esize >= minsize) | |
6125 | found = 1; | |
b7528a20 | 6126 | if (found && start_offset == MaxSector) { |
2da8544a DW |
6127 | start_offset = pos; |
6128 | break; | |
6129 | } else if (found && pos != start_offset) { | |
6130 | found = 0; | |
6131 | break; | |
6132 | } | |
c2c087e6 DW |
6133 | pos = e[i].start + e[i].size; |
6134 | i++; | |
6135 | } while (e[i-1].size); | |
6136 | if (found) | |
6137 | dcnt++; | |
6138 | free(e); | |
6139 | } | |
6140 | if (dcnt < raiddisks) { | |
2c514b71 | 6141 | if (verbose) |
7a862a02 | 6142 | pr_err("imsm: Not enough devices with space for this array (%d < %d)\n", |
2c514b71 | 6143 | dcnt, raiddisks); |
c2c087e6 DW |
6144 | return 0; |
6145 | } | |
6146 | return 1; | |
6147 | } | |
0dcecb2e | 6148 | |
c2c087e6 DW |
6149 | /* This device must be a member of the set */ |
6150 | if (stat(dev, &stb) < 0) | |
6151 | return 0; | |
6152 | if ((S_IFMT & stb.st_mode) != S_IFBLK) | |
6153 | return 0; | |
6154 | for (dl = super->disks ; dl ; dl = dl->next) { | |
f21e18ca N |
6155 | if (dl->major == (int)major(stb.st_rdev) && |
6156 | dl->minor == (int)minor(stb.st_rdev)) | |
c2c087e6 DW |
6157 | break; |
6158 | } | |
6159 | if (!dl) { | |
2c514b71 | 6160 | if (verbose) |
7a862a02 | 6161 | pr_err("%s is not in the same imsm set\n", dev); |
c2c087e6 | 6162 | return 0; |
a20d2ba5 DW |
6163 | } else if (super->orom && dl->index < 0 && mpb->num_raid_devs) { |
6164 | /* If a volume is present then the current creation attempt | |
6165 | * cannot incorporate new spares because the orom may not | |
6166 | * understand this configuration (all member disks must be | |
6167 | * members of each array in the container). | |
6168 | */ | |
7a862a02 N |
6169 | pr_err("%s is a spare and a volume is already defined for this container\n", dev); |
6170 | pr_err("The option-rom requires all member disks to be a member of all volumes\n"); | |
a20d2ba5 | 6171 | return 0; |
5fe62b94 WD |
6172 | } else if (super->orom && mpb->num_raid_devs > 0 && |
6173 | mpb->num_disks != raiddisks) { | |
7a862a02 | 6174 | pr_err("The option-rom requires all member disks to be a member of all volumes\n"); |
5fe62b94 | 6175 | return 0; |
c2c087e6 | 6176 | } |
0dcecb2e DW |
6177 | |
6178 | /* retrieve the largest free space block */ | |
c2c087e6 DW |
6179 | e = get_extents(super, dl); |
6180 | maxsize = 0; | |
6181 | i = 0; | |
0dcecb2e DW |
6182 | if (e) { |
6183 | do { | |
6184 | unsigned long long esize; | |
6185 | ||
6186 | esize = e[i].start - pos; | |
6187 | if (esize >= maxsize) | |
6188 | maxsize = esize; | |
6189 | pos = e[i].start + e[i].size; | |
6190 | i++; | |
6191 | } while (e[i-1].size); | |
6192 | dl->e = e; | |
6193 | dl->extent_cnt = i; | |
6194 | } else { | |
6195 | if (verbose) | |
e7b84f9d | 6196 | pr_err("unable to determine free space for: %s\n", |
0dcecb2e DW |
6197 | dev); |
6198 | return 0; | |
6199 | } | |
6200 | if (maxsize < size) { | |
6201 | if (verbose) | |
e7b84f9d | 6202 | pr_err("%s not enough space (%llu < %llu)\n", |
0dcecb2e DW |
6203 | dev, maxsize, size); |
6204 | return 0; | |
6205 | } | |
6206 | ||
6207 | /* count total number of extents for merge */ | |
6208 | i = 0; | |
6209 | for (dl = super->disks; dl; dl = dl->next) | |
6210 | if (dl->e) | |
6211 | i += dl->extent_cnt; | |
6212 | ||
6213 | maxsize = merge_extents(super, i); | |
3baa56ab LO |
6214 | |
6215 | if (!check_env("IMSM_NO_PLATFORM") && | |
6216 | mpb->num_raid_devs > 0 && size && size != maxsize) { | |
7a862a02 | 6217 | pr_err("attempting to create a second volume with size less then remaining space. Aborting...\n"); |
3baa56ab LO |
6218 | return 0; |
6219 | } | |
6220 | ||
a7dd165b | 6221 | if (maxsize < size || maxsize == 0) { |
b3071342 LD |
6222 | if (verbose) { |
6223 | if (maxsize == 0) | |
7a862a02 | 6224 | pr_err("no free space left on device. Aborting...\n"); |
b3071342 | 6225 | else |
7a862a02 | 6226 | pr_err("not enough space to create volume of given size (%llu < %llu). Aborting...\n", |
b3071342 LD |
6227 | maxsize, size); |
6228 | } | |
0dcecb2e | 6229 | return 0; |
0dcecb2e DW |
6230 | } |
6231 | ||
c2c087e6 DW |
6232 | *freesize = maxsize; |
6233 | ||
ca9de185 | 6234 | if (super->orom) { |
72a45777 | 6235 | int count = count_volumes(super->hba, |
ca9de185 LM |
6236 | super->orom->dpa, verbose); |
6237 | if (super->orom->vphba <= count) { | |
40110b90 | 6238 | pr_vrb(": platform does not support more than %d raid volumes.\n", |
ca9de185 LM |
6239 | super->orom->vphba); |
6240 | return 0; | |
6241 | } | |
6242 | } | |
c2c087e6 | 6243 | return 1; |
cdddbdbc DW |
6244 | } |
6245 | ||
13bcac90 | 6246 | static int imsm_get_free_size(struct supertype *st, int raiddisks, |
efb30e7f DW |
6247 | unsigned long long size, int chunk, |
6248 | unsigned long long *freesize) | |
6249 | { | |
6250 | struct intel_super *super = st->sb; | |
6251 | struct imsm_super *mpb = super->anchor; | |
6252 | struct dl *dl; | |
6253 | int i; | |
6254 | int extent_cnt; | |
6255 | struct extent *e; | |
6256 | unsigned long long maxsize; | |
6257 | unsigned long long minsize; | |
6258 | int cnt; | |
6259 | int used; | |
6260 | ||
6261 | /* find the largest common start free region of the possible disks */ | |
6262 | used = 0; | |
6263 | extent_cnt = 0; | |
6264 | cnt = 0; | |
6265 | for (dl = super->disks; dl; dl = dl->next) { | |
6266 | dl->raiddisk = -1; | |
6267 | ||
6268 | if (dl->index >= 0) | |
6269 | used++; | |
6270 | ||
6271 | /* don't activate new spares if we are orom constrained | |
6272 | * and there is already a volume active in the container | |
6273 | */ | |
6274 | if (super->orom && dl->index < 0 && mpb->num_raid_devs) | |
6275 | continue; | |
6276 | ||
6277 | e = get_extents(super, dl); | |
6278 | if (!e) | |
6279 | continue; | |
6280 | for (i = 1; e[i-1].size; i++) | |
6281 | ; | |
6282 | dl->e = e; | |
6283 | dl->extent_cnt = i; | |
6284 | extent_cnt += i; | |
6285 | cnt++; | |
6286 | } | |
6287 | ||
6288 | maxsize = merge_extents(super, extent_cnt); | |
6289 | minsize = size; | |
6290 | if (size == 0) | |
612e59d8 CA |
6291 | /* chunk is in K */ |
6292 | minsize = chunk * 2; | |
efb30e7f DW |
6293 | |
6294 | if (cnt < raiddisks || | |
6295 | (super->orom && used && used != raiddisks) || | |
a7dd165b DW |
6296 | maxsize < minsize || |
6297 | maxsize == 0) { | |
e7b84f9d | 6298 | pr_err("not enough devices with space to create array.\n"); |
efb30e7f DW |
6299 | return 0; /* No enough free spaces large enough */ |
6300 | } | |
6301 | ||
6302 | if (size == 0) { | |
6303 | size = maxsize; | |
6304 | if (chunk) { | |
612e59d8 CA |
6305 | size /= 2 * chunk; |
6306 | size *= 2 * chunk; | |
efb30e7f | 6307 | } |
f878b242 LM |
6308 | maxsize = size; |
6309 | } | |
6310 | if (!check_env("IMSM_NO_PLATFORM") && | |
6311 | mpb->num_raid_devs > 0 && size && size != maxsize) { | |
7a862a02 | 6312 | pr_err("attempting to create a second volume with size less then remaining space. Aborting...\n"); |
f878b242 | 6313 | return 0; |
efb30e7f | 6314 | } |
efb30e7f DW |
6315 | cnt = 0; |
6316 | for (dl = super->disks; dl; dl = dl->next) | |
6317 | if (dl->e) | |
6318 | dl->raiddisk = cnt++; | |
6319 | ||
6320 | *freesize = size; | |
6321 | ||
13bcac90 AK |
6322 | dprintf("imsm: imsm_get_free_size() returns : %llu\n", size); |
6323 | ||
efb30e7f DW |
6324 | return 1; |
6325 | } | |
6326 | ||
13bcac90 AK |
6327 | static int reserve_space(struct supertype *st, int raiddisks, |
6328 | unsigned long long size, int chunk, | |
6329 | unsigned long long *freesize) | |
6330 | { | |
6331 | struct intel_super *super = st->sb; | |
6332 | struct dl *dl; | |
6333 | int cnt; | |
6334 | int rv = 0; | |
6335 | ||
6336 | rv = imsm_get_free_size(st, raiddisks, size, chunk, freesize); | |
6337 | if (rv) { | |
6338 | cnt = 0; | |
6339 | for (dl = super->disks; dl; dl = dl->next) | |
6340 | if (dl->e) | |
6341 | dl->raiddisk = cnt++; | |
6342 | rv = 1; | |
6343 | } | |
6344 | ||
6345 | return rv; | |
6346 | } | |
6347 | ||
bf5a934a | 6348 | static int validate_geometry_imsm(struct supertype *st, int level, int layout, |
c21e737b | 6349 | int raiddisks, int *chunk, unsigned long long size, |
af4348dd | 6350 | unsigned long long data_offset, |
bf5a934a DW |
6351 | char *dev, unsigned long long *freesize, |
6352 | int verbose) | |
6353 | { | |
6354 | int fd, cfd; | |
6355 | struct mdinfo *sra; | |
20cbe8d2 | 6356 | int is_member = 0; |
bf5a934a | 6357 | |
d54559f0 LM |
6358 | /* load capability |
6359 | * if given unused devices create a container | |
bf5a934a DW |
6360 | * if given given devices in a container create a member volume |
6361 | */ | |
6362 | if (level == LEVEL_CONTAINER) { | |
6363 | /* Must be a fresh device to add to a container */ | |
6364 | return validate_geometry_imsm_container(st, level, layout, | |
c21e737b | 6365 | raiddisks, |
7ccc4cc4 | 6366 | *chunk, |
af4348dd | 6367 | size, data_offset, |
bf5a934a DW |
6368 | dev, freesize, |
6369 | verbose); | |
6370 | } | |
9587c373 | 6371 | |
8592f29d | 6372 | if (!dev) { |
e91a3bad | 6373 | if (st->sb) { |
ca9de185 | 6374 | struct intel_super *super = st->sb; |
e91a3bad | 6375 | if (!validate_geometry_imsm_orom(st->sb, level, layout, |
2cc699af | 6376 | raiddisks, chunk, size, |
e91a3bad LM |
6377 | verbose)) |
6378 | return 0; | |
efb30e7f DW |
6379 | /* we are being asked to automatically layout a |
6380 | * new volume based on the current contents of | |
6381 | * the container. If the the parameters can be | |
6382 | * satisfied reserve_space will record the disks, | |
6383 | * start offset, and size of the volume to be | |
6384 | * created. add_to_super and getinfo_super | |
6385 | * detect when autolayout is in progress. | |
6386 | */ | |
ca9de185 LM |
6387 | /* assuming that freesize is always given when array is |
6388 | created */ | |
6389 | if (super->orom && freesize) { | |
6390 | int count; | |
72a45777 | 6391 | count = count_volumes(super->hba, |
ca9de185 LM |
6392 | super->orom->dpa, verbose); |
6393 | if (super->orom->vphba <= count) { | |
7a862a02 | 6394 | pr_vrb(": platform does not support more than %d raid volumes.\n", |
ca9de185 LM |
6395 | super->orom->vphba); |
6396 | return 0; | |
6397 | } | |
6398 | } | |
e91a3bad LM |
6399 | if (freesize) |
6400 | return reserve_space(st, raiddisks, size, | |
7ccc4cc4 | 6401 | *chunk, freesize); |
8592f29d N |
6402 | } |
6403 | return 1; | |
6404 | } | |
bf5a934a DW |
6405 | if (st->sb) { |
6406 | /* creating in a given container */ | |
6407 | return validate_geometry_imsm_volume(st, level, layout, | |
6408 | raiddisks, chunk, size, | |
af4348dd | 6409 | data_offset, |
bf5a934a DW |
6410 | dev, freesize, verbose); |
6411 | } | |
6412 | ||
bf5a934a DW |
6413 | /* This device needs to be a device in an 'imsm' container */ |
6414 | fd = open(dev, O_RDONLY|O_EXCL, 0); | |
6415 | if (fd >= 0) { | |
6416 | if (verbose) | |
e7b84f9d N |
6417 | pr_err("Cannot create this array on device %s\n", |
6418 | dev); | |
bf5a934a DW |
6419 | close(fd); |
6420 | return 0; | |
6421 | } | |
6422 | if (errno != EBUSY || (fd = open(dev, O_RDONLY, 0)) < 0) { | |
6423 | if (verbose) | |
e7b84f9d | 6424 | pr_err("Cannot open %s: %s\n", |
bf5a934a DW |
6425 | dev, strerror(errno)); |
6426 | return 0; | |
6427 | } | |
6428 | /* Well, it is in use by someone, maybe an 'imsm' container. */ | |
6429 | cfd = open_container(fd); | |
20cbe8d2 | 6430 | close(fd); |
bf5a934a | 6431 | if (cfd < 0) { |
bf5a934a | 6432 | if (verbose) |
e7b84f9d | 6433 | pr_err("Cannot use %s: It is busy\n", |
bf5a934a DW |
6434 | dev); |
6435 | return 0; | |
6436 | } | |
4dd2df09 | 6437 | sra = sysfs_read(cfd, NULL, GET_VERSION); |
bf5a934a | 6438 | if (sra && sra->array.major_version == -1 && |
20cbe8d2 AW |
6439 | strcmp(sra->text_version, "imsm") == 0) |
6440 | is_member = 1; | |
6441 | sysfs_free(sra); | |
6442 | if (is_member) { | |
bf5a934a DW |
6443 | /* This is a member of a imsm container. Load the container |
6444 | * and try to create a volume | |
6445 | */ | |
6446 | struct intel_super *super; | |
6447 | ||
ec50f7b6 | 6448 | if (load_super_imsm_all(st, cfd, (void **) &super, NULL, NULL, 1) == 0) { |
bf5a934a | 6449 | st->sb = super; |
4dd2df09 | 6450 | strcpy(st->container_devnm, fd2devnm(cfd)); |
bf5a934a DW |
6451 | close(cfd); |
6452 | return validate_geometry_imsm_volume(st, level, layout, | |
6453 | raiddisks, chunk, | |
af4348dd | 6454 | size, data_offset, dev, |
ecbd9e81 N |
6455 | freesize, 1) |
6456 | ? 1 : -1; | |
bf5a934a | 6457 | } |
20cbe8d2 | 6458 | } |
bf5a934a | 6459 | |
20cbe8d2 | 6460 | if (verbose) |
e7b84f9d | 6461 | pr_err("failed container membership check\n"); |
20cbe8d2 AW |
6462 | |
6463 | close(cfd); | |
6464 | return 0; | |
bf5a934a | 6465 | } |
0bd16cf2 | 6466 | |
30f58b22 | 6467 | static void default_geometry_imsm(struct supertype *st, int *level, int *layout, int *chunk) |
0bd16cf2 DJ |
6468 | { |
6469 | struct intel_super *super = st->sb; | |
6470 | ||
30f58b22 DW |
6471 | if (level && *level == UnSet) |
6472 | *level = LEVEL_CONTAINER; | |
6473 | ||
6474 | if (level && layout && *layout == UnSet) | |
6475 | *layout = imsm_level_to_layout(*level); | |
0bd16cf2 | 6476 | |
cd9d1ac7 DW |
6477 | if (chunk && (*chunk == UnSet || *chunk == 0)) |
6478 | *chunk = imsm_default_chunk(super->orom); | |
0bd16cf2 DJ |
6479 | } |
6480 | ||
33414a01 DW |
6481 | static void handle_missing(struct intel_super *super, struct imsm_dev *dev); |
6482 | ||
6483 | static int kill_subarray_imsm(struct supertype *st) | |
6484 | { | |
6485 | /* remove the subarray currently referenced by ->current_vol */ | |
6486 | __u8 i; | |
6487 | struct intel_dev **dp; | |
6488 | struct intel_super *super = st->sb; | |
6489 | __u8 current_vol = super->current_vol; | |
6490 | struct imsm_super *mpb = super->anchor; | |
6491 | ||
6492 | if (super->current_vol < 0) | |
6493 | return 2; | |
6494 | super->current_vol = -1; /* invalidate subarray cursor */ | |
6495 | ||
6496 | /* block deletions that would change the uuid of active subarrays | |
6497 | * | |
6498 | * FIXME when immutable ids are available, but note that we'll | |
6499 | * also need to fixup the invalidated/active subarray indexes in | |
6500 | * mdstat | |
6501 | */ | |
6502 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
6503 | char subarray[4]; | |
6504 | ||
6505 | if (i < current_vol) | |
6506 | continue; | |
6507 | sprintf(subarray, "%u", i); | |
4dd2df09 | 6508 | if (is_subarray_active(subarray, st->devnm)) { |
e7b84f9d N |
6509 | pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n", |
6510 | current_vol, i); | |
33414a01 DW |
6511 | |
6512 | return 2; | |
6513 | } | |
6514 | } | |
6515 | ||
6516 | if (st->update_tail) { | |
503975b9 | 6517 | struct imsm_update_kill_array *u = xmalloc(sizeof(*u)); |
33414a01 | 6518 | |
33414a01 DW |
6519 | u->type = update_kill_array; |
6520 | u->dev_idx = current_vol; | |
6521 | append_metadata_update(st, u, sizeof(*u)); | |
6522 | ||
6523 | return 0; | |
6524 | } | |
6525 | ||
6526 | for (dp = &super->devlist; *dp;) | |
6527 | if ((*dp)->index == current_vol) { | |
6528 | *dp = (*dp)->next; | |
6529 | } else { | |
6530 | handle_missing(super, (*dp)->dev); | |
6531 | if ((*dp)->index > current_vol) | |
6532 | (*dp)->index--; | |
6533 | dp = &(*dp)->next; | |
6534 | } | |
6535 | ||
6536 | /* no more raid devices, all active components are now spares, | |
6537 | * but of course failed are still failed | |
6538 | */ | |
6539 | if (--mpb->num_raid_devs == 0) { | |
6540 | struct dl *d; | |
6541 | ||
6542 | for (d = super->disks; d; d = d->next) | |
a8619d23 AK |
6543 | if (d->index > -2) |
6544 | mark_spare(d); | |
33414a01 DW |
6545 | } |
6546 | ||
6547 | super->updates_pending++; | |
6548 | ||
6549 | return 0; | |
6550 | } | |
aa534678 | 6551 | |
a951a4f7 | 6552 | static int update_subarray_imsm(struct supertype *st, char *subarray, |
fa56eddb | 6553 | char *update, struct mddev_ident *ident) |
aa534678 DW |
6554 | { |
6555 | /* update the subarray currently referenced by ->current_vol */ | |
6556 | struct intel_super *super = st->sb; | |
6557 | struct imsm_super *mpb = super->anchor; | |
6558 | ||
aa534678 DW |
6559 | if (strcmp(update, "name") == 0) { |
6560 | char *name = ident->name; | |
a951a4f7 N |
6561 | char *ep; |
6562 | int vol; | |
aa534678 | 6563 | |
4dd2df09 | 6564 | if (is_subarray_active(subarray, st->devnm)) { |
e7b84f9d | 6565 | pr_err("Unable to update name of active subarray\n"); |
aa534678 DW |
6566 | return 2; |
6567 | } | |
6568 | ||
6569 | if (!check_name(super, name, 0)) | |
6570 | return 2; | |
6571 | ||
a951a4f7 N |
6572 | vol = strtoul(subarray, &ep, 10); |
6573 | if (*ep != '\0' || vol >= super->anchor->num_raid_devs) | |
6574 | return 2; | |
6575 | ||
aa534678 | 6576 | if (st->update_tail) { |
503975b9 | 6577 | struct imsm_update_rename_array *u = xmalloc(sizeof(*u)); |
aa534678 | 6578 | |
aa534678 | 6579 | u->type = update_rename_array; |
a951a4f7 | 6580 | u->dev_idx = vol; |
aa534678 DW |
6581 | snprintf((char *) u->name, MAX_RAID_SERIAL_LEN, "%s", name); |
6582 | append_metadata_update(st, u, sizeof(*u)); | |
6583 | } else { | |
6584 | struct imsm_dev *dev; | |
6585 | int i; | |
6586 | ||
a951a4f7 | 6587 | dev = get_imsm_dev(super, vol); |
aa534678 DW |
6588 | snprintf((char *) dev->volume, MAX_RAID_SERIAL_LEN, "%s", name); |
6589 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
6590 | dev = get_imsm_dev(super, i); | |
6591 | handle_missing(super, dev); | |
6592 | } | |
6593 | super->updates_pending++; | |
6594 | } | |
6595 | } else | |
6596 | return 2; | |
6597 | ||
6598 | return 0; | |
6599 | } | |
d1e02575 | 6600 | #endif /* MDASSEMBLE */ |
bf5a934a | 6601 | |
28bce06f AK |
6602 | static int is_gen_migration(struct imsm_dev *dev) |
6603 | { | |
7534230b AK |
6604 | if (dev == NULL) |
6605 | return 0; | |
6606 | ||
28bce06f AK |
6607 | if (!dev->vol.migr_state) |
6608 | return 0; | |
6609 | ||
6610 | if (migr_type(dev) == MIGR_GEN_MIGR) | |
6611 | return 1; | |
6612 | ||
6613 | return 0; | |
6614 | } | |
6615 | ||
1e5c6983 DW |
6616 | static int is_rebuilding(struct imsm_dev *dev) |
6617 | { | |
6618 | struct imsm_map *migr_map; | |
6619 | ||
6620 | if (!dev->vol.migr_state) | |
6621 | return 0; | |
6622 | ||
6623 | if (migr_type(dev) != MIGR_REBUILD) | |
6624 | return 0; | |
6625 | ||
238c0a71 | 6626 | migr_map = get_imsm_map(dev, MAP_1); |
1e5c6983 DW |
6627 | |
6628 | if (migr_map->map_state == IMSM_T_STATE_DEGRADED) | |
6629 | return 1; | |
6630 | else | |
6631 | return 0; | |
6632 | } | |
6633 | ||
b4ab44d8 | 6634 | #ifndef MDASSEMBLE |
6ce1fbf1 AK |
6635 | static int is_initializing(struct imsm_dev *dev) |
6636 | { | |
6637 | struct imsm_map *migr_map; | |
6638 | ||
6639 | if (!dev->vol.migr_state) | |
6640 | return 0; | |
6641 | ||
6642 | if (migr_type(dev) != MIGR_INIT) | |
6643 | return 0; | |
6644 | ||
238c0a71 | 6645 | migr_map = get_imsm_map(dev, MAP_1); |
6ce1fbf1 AK |
6646 | |
6647 | if (migr_map->map_state == IMSM_T_STATE_UNINITIALIZED) | |
6648 | return 1; | |
6649 | ||
6650 | return 0; | |
6ce1fbf1 | 6651 | } |
b4ab44d8 | 6652 | #endif |
6ce1fbf1 | 6653 | |
c47b0ff6 AK |
6654 | static void update_recovery_start(struct intel_super *super, |
6655 | struct imsm_dev *dev, | |
6656 | struct mdinfo *array) | |
1e5c6983 DW |
6657 | { |
6658 | struct mdinfo *rebuild = NULL; | |
6659 | struct mdinfo *d; | |
6660 | __u32 units; | |
6661 | ||
6662 | if (!is_rebuilding(dev)) | |
6663 | return; | |
6664 | ||
6665 | /* Find the rebuild target, but punt on the dual rebuild case */ | |
6666 | for (d = array->devs; d; d = d->next) | |
6667 | if (d->recovery_start == 0) { | |
6668 | if (rebuild) | |
6669 | return; | |
6670 | rebuild = d; | |
6671 | } | |
6672 | ||
4363fd80 DW |
6673 | if (!rebuild) { |
6674 | /* (?) none of the disks are marked with | |
6675 | * IMSM_ORD_REBUILD, so assume they are missing and the | |
6676 | * disk_ord_tbl was not correctly updated | |
6677 | */ | |
1ade5cc1 | 6678 | dprintf("failed to locate out-of-sync disk\n"); |
4363fd80 DW |
6679 | return; |
6680 | } | |
6681 | ||
1e5c6983 | 6682 | units = __le32_to_cpu(dev->vol.curr_migr_unit); |
c47b0ff6 | 6683 | rebuild->recovery_start = units * blocks_per_migr_unit(super, dev); |
1e5c6983 DW |
6684 | } |
6685 | ||
9e2d750d | 6686 | #ifndef MDASSEMBLE |
276d77db | 6687 | static int recover_backup_imsm(struct supertype *st, struct mdinfo *info); |
9e2d750d | 6688 | #endif |
1e5c6983 | 6689 | |
00bbdbda | 6690 | static struct mdinfo *container_content_imsm(struct supertype *st, char *subarray) |
cdddbdbc | 6691 | { |
4f5bc454 DW |
6692 | /* Given a container loaded by load_super_imsm_all, |
6693 | * extract information about all the arrays into | |
6694 | * an mdinfo tree. | |
00bbdbda | 6695 | * If 'subarray' is given, just extract info about that array. |
4f5bc454 DW |
6696 | * |
6697 | * For each imsm_dev create an mdinfo, fill it in, | |
6698 | * then look for matching devices in super->disks | |
6699 | * and create appropriate device mdinfo. | |
6700 | */ | |
6701 | struct intel_super *super = st->sb; | |
949c47a0 | 6702 | struct imsm_super *mpb = super->anchor; |
4f5bc454 | 6703 | struct mdinfo *rest = NULL; |
00bbdbda | 6704 | unsigned int i; |
81219e70 | 6705 | int sb_errors = 0; |
abef11a3 AK |
6706 | struct dl *d; |
6707 | int spare_disks = 0; | |
cdddbdbc | 6708 | |
19482bcc AK |
6709 | /* do not assemble arrays when not all attributes are supported */ |
6710 | if (imsm_check_attributes(mpb->attributes) == 0) { | |
81219e70 | 6711 | sb_errors = 1; |
7a862a02 | 6712 | pr_err("Unsupported attributes in IMSM metadata.Arrays activation is blocked.\n"); |
19482bcc AK |
6713 | } |
6714 | ||
a06d022d | 6715 | /* check for bad blocks */ |
81219e70 | 6716 | if (imsm_bbm_log_size(super->anchor)) { |
7a862a02 | 6717 | pr_err("BBM log found in IMSM metadata.Arrays activation is blocked.\n"); |
81219e70 LM |
6718 | sb_errors = 1; |
6719 | } | |
6720 | ||
abef11a3 AK |
6721 | /* count spare devices, not used in maps |
6722 | */ | |
6723 | for (d = super->disks; d; d = d->next) | |
6724 | if (d->index == -1) | |
6725 | spare_disks++; | |
6726 | ||
4f5bc454 | 6727 | for (i = 0; i < mpb->num_raid_devs; i++) { |
00bbdbda N |
6728 | struct imsm_dev *dev; |
6729 | struct imsm_map *map; | |
86e3692b | 6730 | struct imsm_map *map2; |
4f5bc454 | 6731 | struct mdinfo *this; |
a6482415 N |
6732 | int slot; |
6733 | #ifndef MDASSEMBLE | |
6734 | int chunk; | |
6735 | #endif | |
00bbdbda N |
6736 | char *ep; |
6737 | ||
6738 | if (subarray && | |
6739 | (i != strtoul(subarray, &ep, 10) || *ep != '\0')) | |
6740 | continue; | |
6741 | ||
6742 | dev = get_imsm_dev(super, i); | |
238c0a71 AK |
6743 | map = get_imsm_map(dev, MAP_0); |
6744 | map2 = get_imsm_map(dev, MAP_1); | |
4f5bc454 | 6745 | |
1ce0101c DW |
6746 | /* do not publish arrays that are in the middle of an |
6747 | * unsupported migration | |
6748 | */ | |
6749 | if (dev->vol.migr_state && | |
28bce06f | 6750 | (migr_type(dev) == MIGR_STATE_CHANGE)) { |
7a862a02 | 6751 | pr_err("cannot assemble volume '%.16s': unsupported migration in progress\n", |
1ce0101c DW |
6752 | dev->volume); |
6753 | continue; | |
6754 | } | |
2db86302 LM |
6755 | /* do not publish arrays that are not support by controller's |
6756 | * OROM/EFI | |
6757 | */ | |
1ce0101c | 6758 | |
503975b9 | 6759 | this = xmalloc(sizeof(*this)); |
4f5bc454 | 6760 | |
301406c9 | 6761 | super->current_vol = i; |
a5d85af7 | 6762 | getinfo_super_imsm_volume(st, this, NULL); |
9894ec0d | 6763 | this->next = rest; |
81219e70 | 6764 | #ifndef MDASSEMBLE |
a6482415 | 6765 | chunk = __le16_to_cpu(map->blocks_per_strip) >> 1; |
81219e70 LM |
6766 | /* mdadm does not support all metadata features- set the bit in all arrays state */ |
6767 | if (!validate_geometry_imsm_orom(super, | |
6768 | get_imsm_raid_level(map), /* RAID level */ | |
6769 | imsm_level_to_layout(get_imsm_raid_level(map)), | |
6770 | map->num_members, /* raid disks */ | |
2cc699af | 6771 | &chunk, join_u32(dev->size_low, dev->size_high), |
81219e70 | 6772 | 1 /* verbose */)) { |
7a862a02 | 6773 | pr_err("IMSM RAID geometry validation failed. Array %s activation is blocked.\n", |
81219e70 LM |
6774 | dev->volume); |
6775 | this->array.state |= | |
6776 | (1<<MD_SB_BLOCK_CONTAINER_RESHAPE) | | |
6777 | (1<<MD_SB_BLOCK_VOLUME); | |
6778 | } | |
6779 | #endif | |
6780 | ||
6781 | /* if array has bad blocks, set suitable bit in all arrays state */ | |
6782 | if (sb_errors) | |
6783 | this->array.state |= | |
6784 | (1<<MD_SB_BLOCK_CONTAINER_RESHAPE) | | |
6785 | (1<<MD_SB_BLOCK_VOLUME); | |
6786 | ||
4f5bc454 | 6787 | for (slot = 0 ; slot < map->num_members; slot++) { |
1e5c6983 | 6788 | unsigned long long recovery_start; |
4f5bc454 DW |
6789 | struct mdinfo *info_d; |
6790 | struct dl *d; | |
6791 | int idx; | |
9a1608e5 | 6792 | int skip; |
7eef0453 | 6793 | __u32 ord; |
4f5bc454 | 6794 | |
9a1608e5 | 6795 | skip = 0; |
238c0a71 AK |
6796 | idx = get_imsm_disk_idx(dev, slot, MAP_0); |
6797 | ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X); | |
4f5bc454 DW |
6798 | for (d = super->disks; d ; d = d->next) |
6799 | if (d->index == idx) | |
0fbd635c | 6800 | break; |
4f5bc454 | 6801 | |
1e5c6983 | 6802 | recovery_start = MaxSector; |
4f5bc454 | 6803 | if (d == NULL) |
9a1608e5 | 6804 | skip = 1; |
25ed7e59 | 6805 | if (d && is_failed(&d->disk)) |
9a1608e5 | 6806 | skip = 1; |
7eef0453 | 6807 | if (ord & IMSM_ORD_REBUILD) |
1e5c6983 | 6808 | recovery_start = 0; |
9a1608e5 | 6809 | |
1011e834 | 6810 | /* |
9a1608e5 | 6811 | * if we skip some disks the array will be assmebled degraded; |
1e5c6983 DW |
6812 | * reset resync start to avoid a dirty-degraded |
6813 | * situation when performing the intial sync | |
9a1608e5 DW |
6814 | * |
6815 | * FIXME handle dirty degraded | |
6816 | */ | |
1e5c6983 | 6817 | if ((skip || recovery_start == 0) && !dev->vol.dirty) |
b7528a20 | 6818 | this->resync_start = MaxSector; |
9a1608e5 DW |
6819 | if (skip) |
6820 | continue; | |
4f5bc454 | 6821 | |
503975b9 | 6822 | info_d = xcalloc(1, sizeof(*info_d)); |
4f5bc454 DW |
6823 | info_d->next = this->devs; |
6824 | this->devs = info_d; | |
6825 | ||
4f5bc454 DW |
6826 | info_d->disk.number = d->index; |
6827 | info_d->disk.major = d->major; | |
6828 | info_d->disk.minor = d->minor; | |
6829 | info_d->disk.raid_disk = slot; | |
1e5c6983 | 6830 | info_d->recovery_start = recovery_start; |
86e3692b AK |
6831 | if (map2) { |
6832 | if (slot < map2->num_members) | |
6833 | info_d->disk.state = (1 << MD_DISK_ACTIVE); | |
04c3c514 AK |
6834 | else |
6835 | this->array.spare_disks++; | |
86e3692b AK |
6836 | } else { |
6837 | if (slot < map->num_members) | |
6838 | info_d->disk.state = (1 << MD_DISK_ACTIVE); | |
04c3c514 AK |
6839 | else |
6840 | this->array.spare_disks++; | |
86e3692b | 6841 | } |
1e5c6983 DW |
6842 | if (info_d->recovery_start == MaxSector) |
6843 | this->array.working_disks++; | |
4f5bc454 DW |
6844 | |
6845 | info_d->events = __le32_to_cpu(mpb->generation_num); | |
5551b113 CA |
6846 | info_d->data_offset = pba_of_lba0(map); |
6847 | info_d->component_size = blocks_per_member(map); | |
4f5bc454 | 6848 | } |
1e5c6983 | 6849 | /* now that the disk list is up-to-date fixup recovery_start */ |
c47b0ff6 | 6850 | update_recovery_start(super, dev, this); |
abef11a3 | 6851 | this->array.spare_disks += spare_disks; |
276d77db | 6852 | |
9e2d750d | 6853 | #ifndef MDASSEMBLE |
276d77db AK |
6854 | /* check for reshape */ |
6855 | if (this->reshape_active == 1) | |
6856 | recover_backup_imsm(st, this); | |
9e2d750d | 6857 | #endif |
9a1608e5 | 6858 | rest = this; |
4f5bc454 DW |
6859 | } |
6860 | ||
6861 | return rest; | |
cdddbdbc DW |
6862 | } |
6863 | ||
3b451610 AK |
6864 | static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev, |
6865 | int failed, int look_in_map) | |
c2a1e7da | 6866 | { |
3b451610 AK |
6867 | struct imsm_map *map; |
6868 | ||
6869 | map = get_imsm_map(dev, look_in_map); | |
c2a1e7da DW |
6870 | |
6871 | if (!failed) | |
1011e834 | 6872 | return map->map_state == IMSM_T_STATE_UNINITIALIZED ? |
3393c6af | 6873 | IMSM_T_STATE_UNINITIALIZED : IMSM_T_STATE_NORMAL; |
c2a1e7da DW |
6874 | |
6875 | switch (get_imsm_raid_level(map)) { | |
6876 | case 0: | |
6877 | return IMSM_T_STATE_FAILED; | |
6878 | break; | |
6879 | case 1: | |
6880 | if (failed < map->num_members) | |
6881 | return IMSM_T_STATE_DEGRADED; | |
6882 | else | |
6883 | return IMSM_T_STATE_FAILED; | |
6884 | break; | |
6885 | case 10: | |
6886 | { | |
6887 | /** | |
c92a2527 DW |
6888 | * check to see if any mirrors have failed, otherwise we |
6889 | * are degraded. Even numbered slots are mirrored on | |
6890 | * slot+1 | |
c2a1e7da | 6891 | */ |
c2a1e7da | 6892 | int i; |
d9b420a5 N |
6893 | /* gcc -Os complains that this is unused */ |
6894 | int insync = insync; | |
c2a1e7da DW |
6895 | |
6896 | for (i = 0; i < map->num_members; i++) { | |
238c0a71 | 6897 | __u32 ord = get_imsm_ord_tbl_ent(dev, i, MAP_X); |
c92a2527 DW |
6898 | int idx = ord_to_idx(ord); |
6899 | struct imsm_disk *disk; | |
c2a1e7da | 6900 | |
c92a2527 | 6901 | /* reset the potential in-sync count on even-numbered |
1011e834 | 6902 | * slots. num_copies is always 2 for imsm raid10 |
c92a2527 DW |
6903 | */ |
6904 | if ((i & 1) == 0) | |
6905 | insync = 2; | |
c2a1e7da | 6906 | |
c92a2527 | 6907 | disk = get_imsm_disk(super, idx); |
25ed7e59 | 6908 | if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD) |
c92a2527 | 6909 | insync--; |
c2a1e7da | 6910 | |
c92a2527 DW |
6911 | /* no in-sync disks left in this mirror the |
6912 | * array has failed | |
6913 | */ | |
6914 | if (insync == 0) | |
6915 | return IMSM_T_STATE_FAILED; | |
c2a1e7da DW |
6916 | } |
6917 | ||
6918 | return IMSM_T_STATE_DEGRADED; | |
6919 | } | |
6920 | case 5: | |
6921 | if (failed < 2) | |
6922 | return IMSM_T_STATE_DEGRADED; | |
6923 | else | |
6924 | return IMSM_T_STATE_FAILED; | |
6925 | break; | |
6926 | default: | |
6927 | break; | |
6928 | } | |
6929 | ||
6930 | return map->map_state; | |
6931 | } | |
6932 | ||
3b451610 AK |
6933 | static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev, |
6934 | int look_in_map) | |
c2a1e7da DW |
6935 | { |
6936 | int i; | |
6937 | int failed = 0; | |
6938 | struct imsm_disk *disk; | |
d5985138 AK |
6939 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
6940 | struct imsm_map *prev = get_imsm_map(dev, MAP_1); | |
68fe4598 | 6941 | struct imsm_map *map_for_loop; |
0556e1a2 DW |
6942 | __u32 ord; |
6943 | int idx; | |
d5985138 | 6944 | int idx_1; |
c2a1e7da | 6945 | |
0556e1a2 DW |
6946 | /* at the beginning of migration we set IMSM_ORD_REBUILD on |
6947 | * disks that are being rebuilt. New failures are recorded to | |
6948 | * map[0]. So we look through all the disks we started with and | |
6949 | * see if any failures are still present, or if any new ones | |
6950 | * have arrived | |
0556e1a2 | 6951 | */ |
d5985138 AK |
6952 | map_for_loop = map; |
6953 | if (prev && (map->num_members < prev->num_members)) | |
6954 | map_for_loop = prev; | |
68fe4598 LD |
6955 | |
6956 | for (i = 0; i < map_for_loop->num_members; i++) { | |
d5985138 | 6957 | idx_1 = -255; |
238c0a71 AK |
6958 | /* when MAP_X is passed both maps failures are counted |
6959 | */ | |
d5985138 | 6960 | if (prev && |
238c0a71 AK |
6961 | ((look_in_map == MAP_1) || (look_in_map == MAP_X)) && |
6962 | (i < prev->num_members)) { | |
d5985138 AK |
6963 | ord = __le32_to_cpu(prev->disk_ord_tbl[i]); |
6964 | idx_1 = ord_to_idx(ord); | |
c2a1e7da | 6965 | |
d5985138 AK |
6966 | disk = get_imsm_disk(super, idx_1); |
6967 | if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD) | |
6968 | failed++; | |
6969 | } | |
238c0a71 AK |
6970 | if (((look_in_map == MAP_0) || (look_in_map == MAP_X)) && |
6971 | (i < map->num_members)) { | |
d5985138 AK |
6972 | ord = __le32_to_cpu(map->disk_ord_tbl[i]); |
6973 | idx = ord_to_idx(ord); | |
6974 | ||
6975 | if (idx != idx_1) { | |
6976 | disk = get_imsm_disk(super, idx); | |
6977 | if (!disk || is_failed(disk) || | |
6978 | ord & IMSM_ORD_REBUILD) | |
6979 | failed++; | |
6980 | } | |
6981 | } | |
c2a1e7da DW |
6982 | } |
6983 | ||
6984 | return failed; | |
845dea95 NB |
6985 | } |
6986 | ||
97b4d0e9 DW |
6987 | #ifndef MDASSEMBLE |
6988 | static int imsm_open_new(struct supertype *c, struct active_array *a, | |
6989 | char *inst) | |
6990 | { | |
6991 | struct intel_super *super = c->sb; | |
6992 | struct imsm_super *mpb = super->anchor; | |
9587c373 | 6993 | |
97b4d0e9 | 6994 | if (atoi(inst) >= mpb->num_raid_devs) { |
1ade5cc1 | 6995 | pr_err("subarry index %d, out of range\n", atoi(inst)); |
97b4d0e9 DW |
6996 | return -ENODEV; |
6997 | } | |
6998 | ||
6999 | dprintf("imsm: open_new %s\n", inst); | |
7000 | a->info.container_member = atoi(inst); | |
7001 | return 0; | |
7002 | } | |
7003 | ||
0c046afd DW |
7004 | static int is_resyncing(struct imsm_dev *dev) |
7005 | { | |
7006 | struct imsm_map *migr_map; | |
7007 | ||
7008 | if (!dev->vol.migr_state) | |
7009 | return 0; | |
7010 | ||
1484e727 DW |
7011 | if (migr_type(dev) == MIGR_INIT || |
7012 | migr_type(dev) == MIGR_REPAIR) | |
0c046afd DW |
7013 | return 1; |
7014 | ||
4c9bc37b AK |
7015 | if (migr_type(dev) == MIGR_GEN_MIGR) |
7016 | return 0; | |
7017 | ||
238c0a71 | 7018 | migr_map = get_imsm_map(dev, MAP_1); |
0c046afd | 7019 | |
4c9bc37b AK |
7020 | if ((migr_map->map_state == IMSM_T_STATE_NORMAL) && |
7021 | (dev->vol.migr_type != MIGR_GEN_MIGR)) | |
0c046afd DW |
7022 | return 1; |
7023 | else | |
7024 | return 0; | |
7025 | } | |
7026 | ||
0556e1a2 DW |
7027 | /* return true if we recorded new information */ |
7028 | static int mark_failure(struct imsm_dev *dev, struct imsm_disk *disk, int idx) | |
47ee5a45 | 7029 | { |
0556e1a2 DW |
7030 | __u32 ord; |
7031 | int slot; | |
7032 | struct imsm_map *map; | |
86c54047 DW |
7033 | char buf[MAX_RAID_SERIAL_LEN+3]; |
7034 | unsigned int len, shift = 0; | |
0556e1a2 DW |
7035 | |
7036 | /* new failures are always set in map[0] */ | |
238c0a71 | 7037 | map = get_imsm_map(dev, MAP_0); |
0556e1a2 DW |
7038 | |
7039 | slot = get_imsm_disk_slot(map, idx); | |
7040 | if (slot < 0) | |
7041 | return 0; | |
7042 | ||
7043 | ord = __le32_to_cpu(map->disk_ord_tbl[slot]); | |
25ed7e59 | 7044 | if (is_failed(disk) && (ord & IMSM_ORD_REBUILD)) |
0556e1a2 DW |
7045 | return 0; |
7046 | ||
7d0c5e24 LD |
7047 | memcpy(buf, disk->serial, MAX_RAID_SERIAL_LEN); |
7048 | buf[MAX_RAID_SERIAL_LEN] = '\000'; | |
7049 | strcat(buf, ":0"); | |
86c54047 DW |
7050 | if ((len = strlen(buf)) >= MAX_RAID_SERIAL_LEN) |
7051 | shift = len - MAX_RAID_SERIAL_LEN + 1; | |
7052 | strncpy((char *)disk->serial, &buf[shift], MAX_RAID_SERIAL_LEN); | |
7053 | ||
f2f27e63 | 7054 | disk->status |= FAILED_DISK; |
0556e1a2 | 7055 | set_imsm_ord_tbl_ent(map, slot, idx | IMSM_ORD_REBUILD); |
17788994 AK |
7056 | /* mark failures in second map if second map exists and this disk |
7057 | * in this slot. | |
7058 | * This is valid for migration, initialization and rebuild | |
7059 | */ | |
7060 | if (dev->vol.migr_state) { | |
238c0a71 | 7061 | struct imsm_map *map2 = get_imsm_map(dev, MAP_1); |
0a108d63 AK |
7062 | int slot2 = get_imsm_disk_slot(map2, idx); |
7063 | ||
7064 | if ((slot2 < map2->num_members) && | |
7065 | (slot2 >= 0)) | |
7066 | set_imsm_ord_tbl_ent(map2, slot2, | |
1ace8403 AK |
7067 | idx | IMSM_ORD_REBUILD); |
7068 | } | |
f21e18ca | 7069 | if (map->failed_disk_num == 0xff) |
0556e1a2 DW |
7070 | map->failed_disk_num = slot; |
7071 | return 1; | |
7072 | } | |
7073 | ||
7074 | static void mark_missing(struct imsm_dev *dev, struct imsm_disk *disk, int idx) | |
7075 | { | |
7076 | mark_failure(dev, disk, idx); | |
7077 | ||
7078 | if (disk->scsi_id == __cpu_to_le32(~(__u32)0)) | |
7079 | return; | |
7080 | ||
47ee5a45 DW |
7081 | disk->scsi_id = __cpu_to_le32(~(__u32)0); |
7082 | memmove(&disk->serial[0], &disk->serial[1], MAX_RAID_SERIAL_LEN - 1); | |
7083 | } | |
7084 | ||
33414a01 DW |
7085 | static void handle_missing(struct intel_super *super, struct imsm_dev *dev) |
7086 | { | |
33414a01 | 7087 | struct dl *dl; |
33414a01 DW |
7088 | |
7089 | if (!super->missing) | |
7090 | return; | |
33414a01 | 7091 | |
79b68f1b PC |
7092 | /* When orom adds replacement for missing disk it does |
7093 | * not remove entry of missing disk, but just updates map with | |
7094 | * new added disk. So it is not enough just to test if there is | |
7095 | * any missing disk, we have to look if there are any failed disks | |
7096 | * in map to stop migration */ | |
7097 | ||
33414a01 | 7098 | dprintf("imsm: mark missing\n"); |
3d59f0c0 AK |
7099 | /* end process for initialization and rebuild only |
7100 | */ | |
7101 | if (is_gen_migration(dev) == 0) { | |
7102 | __u8 map_state; | |
7103 | int failed; | |
7104 | ||
7105 | failed = imsm_count_failed(super, dev, MAP_0); | |
7106 | map_state = imsm_check_degraded(super, dev, failed, MAP_0); | |
7107 | ||
79b68f1b PC |
7108 | if (failed) |
7109 | end_migration(dev, super, map_state); | |
3d59f0c0 | 7110 | } |
33414a01 DW |
7111 | for (dl = super->missing; dl; dl = dl->next) |
7112 | mark_missing(dev, &dl->disk, dl->index); | |
7113 | super->updates_pending++; | |
7114 | } | |
7115 | ||
f3871fdc AK |
7116 | static unsigned long long imsm_set_array_size(struct imsm_dev *dev, |
7117 | long long new_size) | |
70bdf0dc | 7118 | { |
238c0a71 | 7119 | int used_disks = imsm_num_data_members(dev, MAP_0); |
70bdf0dc AK |
7120 | unsigned long long array_blocks; |
7121 | struct imsm_map *map; | |
7122 | ||
7123 | if (used_disks == 0) { | |
7124 | /* when problems occures | |
7125 | * return current array_blocks value | |
7126 | */ | |
7127 | array_blocks = __le32_to_cpu(dev->size_high); | |
7128 | array_blocks = array_blocks << 32; | |
7129 | array_blocks += __le32_to_cpu(dev->size_low); | |
7130 | ||
7131 | return array_blocks; | |
7132 | } | |
7133 | ||
7134 | /* set array size in metadata | |
7135 | */ | |
f3871fdc AK |
7136 | if (new_size <= 0) { |
7137 | /* OLCE size change is caused by added disks | |
7138 | */ | |
7139 | map = get_imsm_map(dev, MAP_0); | |
7140 | array_blocks = blocks_per_member(map) * used_disks; | |
7141 | } else { | |
7142 | /* Online Volume Size Change | |
7143 | * Using available free space | |
7144 | */ | |
7145 | array_blocks = new_size; | |
7146 | } | |
70bdf0dc AK |
7147 | |
7148 | /* round array size down to closest MB | |
7149 | */ | |
7150 | array_blocks = (array_blocks >> SECT_PER_MB_SHIFT) << SECT_PER_MB_SHIFT; | |
7151 | dev->size_low = __cpu_to_le32((__u32)array_blocks); | |
7152 | dev->size_high = __cpu_to_le32((__u32)(array_blocks >> 32)); | |
7153 | ||
7154 | return array_blocks; | |
7155 | } | |
7156 | ||
28bce06f AK |
7157 | static void imsm_set_disk(struct active_array *a, int n, int state); |
7158 | ||
0e2d1a4e AK |
7159 | static void imsm_progress_container_reshape(struct intel_super *super) |
7160 | { | |
7161 | /* if no device has a migr_state, but some device has a | |
7162 | * different number of members than the previous device, start | |
7163 | * changing the number of devices in this device to match | |
7164 | * previous. | |
7165 | */ | |
7166 | struct imsm_super *mpb = super->anchor; | |
7167 | int prev_disks = -1; | |
7168 | int i; | |
1dfaa380 | 7169 | int copy_map_size; |
0e2d1a4e AK |
7170 | |
7171 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
7172 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
238c0a71 | 7173 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
0e2d1a4e AK |
7174 | struct imsm_map *map2; |
7175 | int prev_num_members; | |
0e2d1a4e AK |
7176 | |
7177 | if (dev->vol.migr_state) | |
7178 | return; | |
7179 | ||
7180 | if (prev_disks == -1) | |
7181 | prev_disks = map->num_members; | |
7182 | if (prev_disks == map->num_members) | |
7183 | continue; | |
7184 | ||
7185 | /* OK, this array needs to enter reshape mode. | |
7186 | * i.e it needs a migr_state | |
7187 | */ | |
7188 | ||
1dfaa380 | 7189 | copy_map_size = sizeof_imsm_map(map); |
0e2d1a4e AK |
7190 | prev_num_members = map->num_members; |
7191 | map->num_members = prev_disks; | |
7192 | dev->vol.migr_state = 1; | |
7193 | dev->vol.curr_migr_unit = 0; | |
ea672ee1 | 7194 | set_migr_type(dev, MIGR_GEN_MIGR); |
0e2d1a4e AK |
7195 | for (i = prev_num_members; |
7196 | i < map->num_members; i++) | |
7197 | set_imsm_ord_tbl_ent(map, i, i); | |
238c0a71 | 7198 | map2 = get_imsm_map(dev, MAP_1); |
0e2d1a4e | 7199 | /* Copy the current map */ |
1dfaa380 | 7200 | memcpy(map2, map, copy_map_size); |
0e2d1a4e AK |
7201 | map2->num_members = prev_num_members; |
7202 | ||
f3871fdc | 7203 | imsm_set_array_size(dev, -1); |
51d83f5d | 7204 | super->clean_migration_record_by_mdmon = 1; |
0e2d1a4e AK |
7205 | super->updates_pending++; |
7206 | } | |
7207 | } | |
7208 | ||
aad6f216 | 7209 | /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild |
0c046afd DW |
7210 | * states are handled in imsm_set_disk() with one exception, when a |
7211 | * resync is stopped due to a new failure this routine will set the | |
7212 | * 'degraded' state for the array. | |
7213 | */ | |
01f157d7 | 7214 | static int imsm_set_array_state(struct active_array *a, int consistent) |
a862209d DW |
7215 | { |
7216 | int inst = a->info.container_member; | |
7217 | struct intel_super *super = a->container->sb; | |
949c47a0 | 7218 | struct imsm_dev *dev = get_imsm_dev(super, inst); |
238c0a71 | 7219 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
3b451610 AK |
7220 | int failed = imsm_count_failed(super, dev, MAP_0); |
7221 | __u8 map_state = imsm_check_degraded(super, dev, failed, MAP_0); | |
1e5c6983 | 7222 | __u32 blocks_per_unit; |
a862209d | 7223 | |
1af97990 AK |
7224 | if (dev->vol.migr_state && |
7225 | dev->vol.migr_type == MIGR_GEN_MIGR) { | |
7226 | /* array state change is blocked due to reshape action | |
aad6f216 N |
7227 | * We might need to |
7228 | * - abort the reshape (if last_checkpoint is 0 and action!= reshape) | |
7229 | * - finish the reshape (if last_checkpoint is big and action != reshape) | |
7230 | * - update curr_migr_unit | |
1af97990 | 7231 | */ |
aad6f216 N |
7232 | if (a->curr_action == reshape) { |
7233 | /* still reshaping, maybe update curr_migr_unit */ | |
633b5610 | 7234 | goto mark_checkpoint; |
aad6f216 N |
7235 | } else { |
7236 | if (a->last_checkpoint == 0 && a->prev_action == reshape) { | |
7237 | /* for some reason we aborted the reshape. | |
b66e591b AK |
7238 | * |
7239 | * disable automatic metadata rollback | |
7240 | * user action is required to recover process | |
aad6f216 | 7241 | */ |
b66e591b | 7242 | if (0) { |
238c0a71 AK |
7243 | struct imsm_map *map2 = |
7244 | get_imsm_map(dev, MAP_1); | |
7245 | dev->vol.migr_state = 0; | |
7246 | set_migr_type(dev, 0); | |
7247 | dev->vol.curr_migr_unit = 0; | |
7248 | memcpy(map, map2, | |
7249 | sizeof_imsm_map(map2)); | |
7250 | super->updates_pending++; | |
b66e591b | 7251 | } |
aad6f216 N |
7252 | } |
7253 | if (a->last_checkpoint >= a->info.component_size) { | |
7254 | unsigned long long array_blocks; | |
7255 | int used_disks; | |
e154ced3 | 7256 | struct mdinfo *mdi; |
aad6f216 | 7257 | |
238c0a71 | 7258 | used_disks = imsm_num_data_members(dev, MAP_0); |
d55adef9 AK |
7259 | if (used_disks > 0) { |
7260 | array_blocks = | |
5551b113 | 7261 | blocks_per_member(map) * |
d55adef9 AK |
7262 | used_disks; |
7263 | /* round array size down to closest MB | |
7264 | */ | |
7265 | array_blocks = (array_blocks | |
7266 | >> SECT_PER_MB_SHIFT) | |
7267 | << SECT_PER_MB_SHIFT; | |
d55adef9 AK |
7268 | a->info.custom_array_size = array_blocks; |
7269 | /* encourage manager to update array | |
7270 | * size | |
7271 | */ | |
e154ced3 | 7272 | |
d55adef9 | 7273 | a->check_reshape = 1; |
633b5610 | 7274 | } |
e154ced3 AK |
7275 | /* finalize online capacity expansion/reshape */ |
7276 | for (mdi = a->info.devs; mdi; mdi = mdi->next) | |
7277 | imsm_set_disk(a, | |
7278 | mdi->disk.raid_disk, | |
7279 | mdi->curr_state); | |
7280 | ||
0e2d1a4e | 7281 | imsm_progress_container_reshape(super); |
e154ced3 | 7282 | } |
aad6f216 | 7283 | } |
1af97990 AK |
7284 | } |
7285 | ||
47ee5a45 | 7286 | /* before we activate this array handle any missing disks */ |
33414a01 DW |
7287 | if (consistent == 2) |
7288 | handle_missing(super, dev); | |
1e5c6983 | 7289 | |
0c046afd | 7290 | if (consistent == 2 && |
b7941fd6 | 7291 | (!is_resync_complete(&a->info) || |
0c046afd DW |
7292 | map_state != IMSM_T_STATE_NORMAL || |
7293 | dev->vol.migr_state)) | |
01f157d7 | 7294 | consistent = 0; |
272906ef | 7295 | |
b7941fd6 | 7296 | if (is_resync_complete(&a->info)) { |
0c046afd | 7297 | /* complete intialization / resync, |
0556e1a2 DW |
7298 | * recovery and interrupted recovery is completed in |
7299 | * ->set_disk | |
0c046afd DW |
7300 | */ |
7301 | if (is_resyncing(dev)) { | |
7302 | dprintf("imsm: mark resync done\n"); | |
809da78e | 7303 | end_migration(dev, super, map_state); |
115c3803 | 7304 | super->updates_pending++; |
484240d8 | 7305 | a->last_checkpoint = 0; |
115c3803 | 7306 | } |
b9172665 AK |
7307 | } else if ((!is_resyncing(dev) && !failed) && |
7308 | (imsm_reshape_blocks_arrays_changes(super) == 0)) { | |
0c046afd | 7309 | /* mark the start of the init process if nothing is failed */ |
b7941fd6 | 7310 | dprintf("imsm: mark resync start\n"); |
1484e727 | 7311 | if (map->map_state == IMSM_T_STATE_UNINITIALIZED) |
8e59f3d8 | 7312 | migrate(dev, super, IMSM_T_STATE_NORMAL, MIGR_INIT); |
1484e727 | 7313 | else |
8e59f3d8 | 7314 | migrate(dev, super, IMSM_T_STATE_NORMAL, MIGR_REPAIR); |
3393c6af | 7315 | super->updates_pending++; |
115c3803 | 7316 | } |
a862209d | 7317 | |
633b5610 | 7318 | mark_checkpoint: |
5b83bacf AK |
7319 | /* skip checkpointing for general migration, |
7320 | * it is controlled in mdadm | |
7321 | */ | |
7322 | if (is_gen_migration(dev)) | |
7323 | goto skip_mark_checkpoint; | |
7324 | ||
1e5c6983 | 7325 | /* check if we can update curr_migr_unit from resync_start, recovery_start */ |
c47b0ff6 | 7326 | blocks_per_unit = blocks_per_migr_unit(super, dev); |
4f0a7acc | 7327 | if (blocks_per_unit) { |
1e5c6983 DW |
7328 | __u32 units32; |
7329 | __u64 units; | |
7330 | ||
4f0a7acc | 7331 | units = a->last_checkpoint / blocks_per_unit; |
1e5c6983 DW |
7332 | units32 = units; |
7333 | ||
7334 | /* check that we did not overflow 32-bits, and that | |
7335 | * curr_migr_unit needs updating | |
7336 | */ | |
7337 | if (units32 == units && | |
bfd80a56 | 7338 | units32 != 0 && |
1e5c6983 DW |
7339 | __le32_to_cpu(dev->vol.curr_migr_unit) != units32) { |
7340 | dprintf("imsm: mark checkpoint (%u)\n", units32); | |
7341 | dev->vol.curr_migr_unit = __cpu_to_le32(units32); | |
7342 | super->updates_pending++; | |
7343 | } | |
7344 | } | |
f8f603f1 | 7345 | |
5b83bacf | 7346 | skip_mark_checkpoint: |
3393c6af | 7347 | /* mark dirty / clean */ |
0c046afd | 7348 | if (dev->vol.dirty != !consistent) { |
b7941fd6 | 7349 | dprintf("imsm: mark '%s'\n", consistent ? "clean" : "dirty"); |
0c046afd DW |
7350 | if (consistent) |
7351 | dev->vol.dirty = 0; | |
7352 | else | |
7353 | dev->vol.dirty = 1; | |
a862209d DW |
7354 | super->updates_pending++; |
7355 | } | |
28bce06f | 7356 | |
01f157d7 | 7357 | return consistent; |
a862209d DW |
7358 | } |
7359 | ||
8d45d196 | 7360 | static void imsm_set_disk(struct active_array *a, int n, int state) |
845dea95 | 7361 | { |
8d45d196 DW |
7362 | int inst = a->info.container_member; |
7363 | struct intel_super *super = a->container->sb; | |
949c47a0 | 7364 | struct imsm_dev *dev = get_imsm_dev(super, inst); |
238c0a71 | 7365 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
8d45d196 | 7366 | struct imsm_disk *disk; |
7ce05701 LD |
7367 | struct mdinfo *mdi; |
7368 | int recovery_not_finished = 0; | |
0c046afd | 7369 | int failed; |
b10b37b8 | 7370 | __u32 ord; |
0c046afd | 7371 | __u8 map_state; |
8d45d196 DW |
7372 | |
7373 | if (n > map->num_members) | |
e12b3daa | 7374 | pr_err("imsm: set_disk %d out of range 0..%d\n", |
8d45d196 DW |
7375 | n, map->num_members - 1); |
7376 | ||
7377 | if (n < 0) | |
7378 | return; | |
7379 | ||
4e6e574a | 7380 | dprintf("imsm: set_disk %d:%x\n", n, state); |
8d45d196 | 7381 | |
238c0a71 | 7382 | ord = get_imsm_ord_tbl_ent(dev, n, MAP_0); |
b10b37b8 | 7383 | disk = get_imsm_disk(super, ord_to_idx(ord)); |
8d45d196 | 7384 | |
5802a811 | 7385 | /* check for new failures */ |
0556e1a2 DW |
7386 | if (state & DS_FAULTY) { |
7387 | if (mark_failure(dev, disk, ord_to_idx(ord))) | |
7388 | super->updates_pending++; | |
8d45d196 | 7389 | } |
47ee5a45 | 7390 | |
19859edc | 7391 | /* check if in_sync */ |
0556e1a2 | 7392 | if (state & DS_INSYNC && ord & IMSM_ORD_REBUILD && is_rebuilding(dev)) { |
238c0a71 | 7393 | struct imsm_map *migr_map = get_imsm_map(dev, MAP_1); |
b10b37b8 DW |
7394 | |
7395 | set_imsm_ord_tbl_ent(migr_map, n, ord_to_idx(ord)); | |
19859edc DW |
7396 | super->updates_pending++; |
7397 | } | |
8d45d196 | 7398 | |
3b451610 AK |
7399 | failed = imsm_count_failed(super, dev, MAP_0); |
7400 | map_state = imsm_check_degraded(super, dev, failed, MAP_0); | |
5802a811 | 7401 | |
0c046afd | 7402 | /* check if recovery complete, newly degraded, or failed */ |
94002678 AK |
7403 | dprintf("imsm: Detected transition to state "); |
7404 | switch (map_state) { | |
7405 | case IMSM_T_STATE_NORMAL: /* transition to normal state */ | |
7406 | dprintf("normal: "); | |
7407 | if (is_rebuilding(dev)) { | |
1ade5cc1 | 7408 | dprintf_cont("while rebuilding"); |
7ce05701 LD |
7409 | /* check if recovery is really finished */ |
7410 | for (mdi = a->info.devs; mdi ; mdi = mdi->next) | |
7411 | if (mdi->recovery_start != MaxSector) { | |
7412 | recovery_not_finished = 1; | |
7413 | break; | |
7414 | } | |
7415 | if (recovery_not_finished) { | |
1ade5cc1 N |
7416 | dprintf_cont("\n"); |
7417 | dprintf("Rebuild has not finished yet, state not changed"); | |
7ce05701 LD |
7418 | if (a->last_checkpoint < mdi->recovery_start) { |
7419 | a->last_checkpoint = mdi->recovery_start; | |
7420 | super->updates_pending++; | |
7421 | } | |
7422 | break; | |
7423 | } | |
94002678 | 7424 | end_migration(dev, super, map_state); |
238c0a71 | 7425 | map = get_imsm_map(dev, MAP_0); |
94002678 AK |
7426 | map->failed_disk_num = ~0; |
7427 | super->updates_pending++; | |
7428 | a->last_checkpoint = 0; | |
7429 | break; | |
7430 | } | |
7431 | if (is_gen_migration(dev)) { | |
1ade5cc1 | 7432 | dprintf_cont("while general migration"); |
bf2f0071 | 7433 | if (a->last_checkpoint >= a->info.component_size) |
809da78e | 7434 | end_migration(dev, super, map_state); |
94002678 AK |
7435 | else |
7436 | map->map_state = map_state; | |
238c0a71 | 7437 | map = get_imsm_map(dev, MAP_0); |
28bce06f | 7438 | map->failed_disk_num = ~0; |
94002678 | 7439 | super->updates_pending++; |
bf2f0071 | 7440 | break; |
94002678 AK |
7441 | } |
7442 | break; | |
7443 | case IMSM_T_STATE_DEGRADED: /* transition to degraded state */ | |
1ade5cc1 | 7444 | dprintf_cont("degraded: "); |
94002678 AK |
7445 | if ((map->map_state != map_state) && |
7446 | !dev->vol.migr_state) { | |
1ade5cc1 | 7447 | dprintf_cont("mark degraded"); |
94002678 AK |
7448 | map->map_state = map_state; |
7449 | super->updates_pending++; | |
7450 | a->last_checkpoint = 0; | |
7451 | break; | |
7452 | } | |
7453 | if (is_rebuilding(dev)) { | |
1ade5cc1 | 7454 | dprintf_cont("while rebuilding."); |
94002678 | 7455 | if (map->map_state != map_state) { |
1ade5cc1 | 7456 | dprintf_cont(" Map state change"); |
94002678 AK |
7457 | end_migration(dev, super, map_state); |
7458 | super->updates_pending++; | |
7459 | } | |
7460 | break; | |
7461 | } | |
7462 | if (is_gen_migration(dev)) { | |
1ade5cc1 | 7463 | dprintf_cont("while general migration"); |
bf2f0071 | 7464 | if (a->last_checkpoint >= a->info.component_size) |
809da78e | 7465 | end_migration(dev, super, map_state); |
94002678 AK |
7466 | else { |
7467 | map->map_state = map_state; | |
3b451610 | 7468 | manage_second_map(super, dev); |
94002678 AK |
7469 | } |
7470 | super->updates_pending++; | |
bf2f0071 | 7471 | break; |
28bce06f | 7472 | } |
6ce1fbf1 | 7473 | if (is_initializing(dev)) { |
1ade5cc1 | 7474 | dprintf_cont("while initialization."); |
6ce1fbf1 AK |
7475 | map->map_state = map_state; |
7476 | super->updates_pending++; | |
7477 | break; | |
7478 | } | |
94002678 AK |
7479 | break; |
7480 | case IMSM_T_STATE_FAILED: /* transition to failed state */ | |
1ade5cc1 | 7481 | dprintf_cont("failed: "); |
94002678 | 7482 | if (is_gen_migration(dev)) { |
1ade5cc1 | 7483 | dprintf_cont("while general migration"); |
94002678 AK |
7484 | map->map_state = map_state; |
7485 | super->updates_pending++; | |
7486 | break; | |
7487 | } | |
7488 | if (map->map_state != map_state) { | |
1ade5cc1 | 7489 | dprintf_cont("mark failed"); |
94002678 AK |
7490 | end_migration(dev, super, map_state); |
7491 | super->updates_pending++; | |
7492 | a->last_checkpoint = 0; | |
7493 | break; | |
7494 | } | |
7495 | break; | |
7496 | default: | |
1ade5cc1 | 7497 | dprintf_cont("state %i\n", map_state); |
5802a811 | 7498 | } |
1ade5cc1 | 7499 | dprintf_cont("\n"); |
845dea95 NB |
7500 | } |
7501 | ||
f796af5d | 7502 | static int store_imsm_mpb(int fd, struct imsm_super *mpb) |
c2a1e7da | 7503 | { |
f796af5d | 7504 | void *buf = mpb; |
c2a1e7da DW |
7505 | __u32 mpb_size = __le32_to_cpu(mpb->mpb_size); |
7506 | unsigned long long dsize; | |
7507 | unsigned long long sectors; | |
7508 | ||
7509 | get_dev_size(fd, NULL, &dsize); | |
7510 | ||
272f648f DW |
7511 | if (mpb_size > 512) { |
7512 | /* -1 to account for anchor */ | |
7513 | sectors = mpb_sectors(mpb) - 1; | |
c2a1e7da | 7514 | |
272f648f DW |
7515 | /* write the extended mpb to the sectors preceeding the anchor */ |
7516 | if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0) | |
7517 | return 1; | |
c2a1e7da | 7518 | |
f21e18ca N |
7519 | if ((unsigned long long)write(fd, buf + 512, 512 * sectors) |
7520 | != 512 * sectors) | |
272f648f DW |
7521 | return 1; |
7522 | } | |
c2a1e7da | 7523 | |
272f648f DW |
7524 | /* first block is stored on second to last sector of the disk */ |
7525 | if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0) | |
c2a1e7da DW |
7526 | return 1; |
7527 | ||
f796af5d | 7528 | if (write(fd, buf, 512) != 512) |
c2a1e7da DW |
7529 | return 1; |
7530 | ||
c2a1e7da DW |
7531 | return 0; |
7532 | } | |
7533 | ||
2e735d19 | 7534 | static void imsm_sync_metadata(struct supertype *container) |
845dea95 | 7535 | { |
2e735d19 | 7536 | struct intel_super *super = container->sb; |
c2a1e7da | 7537 | |
1a64be56 | 7538 | dprintf("sync metadata: %d\n", super->updates_pending); |
c2a1e7da DW |
7539 | if (!super->updates_pending) |
7540 | return; | |
7541 | ||
36988a3d | 7542 | write_super_imsm(container, 0); |
c2a1e7da DW |
7543 | |
7544 | super->updates_pending = 0; | |
845dea95 NB |
7545 | } |
7546 | ||
272906ef DW |
7547 | static struct dl *imsm_readd(struct intel_super *super, int idx, struct active_array *a) |
7548 | { | |
7549 | struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member); | |
238c0a71 | 7550 | int i = get_imsm_disk_idx(dev, idx, MAP_X); |
272906ef DW |
7551 | struct dl *dl; |
7552 | ||
7553 | for (dl = super->disks; dl; dl = dl->next) | |
7554 | if (dl->index == i) | |
7555 | break; | |
7556 | ||
25ed7e59 | 7557 | if (dl && is_failed(&dl->disk)) |
272906ef DW |
7558 | dl = NULL; |
7559 | ||
7560 | if (dl) | |
1ade5cc1 | 7561 | dprintf("found %x:%x\n", dl->major, dl->minor); |
272906ef DW |
7562 | |
7563 | return dl; | |
7564 | } | |
7565 | ||
a20d2ba5 | 7566 | static struct dl *imsm_add_spare(struct intel_super *super, int slot, |
8ba77d32 AK |
7567 | struct active_array *a, int activate_new, |
7568 | struct mdinfo *additional_test_list) | |
272906ef DW |
7569 | { |
7570 | struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member); | |
238c0a71 | 7571 | int idx = get_imsm_disk_idx(dev, slot, MAP_X); |
a20d2ba5 DW |
7572 | struct imsm_super *mpb = super->anchor; |
7573 | struct imsm_map *map; | |
272906ef DW |
7574 | unsigned long long pos; |
7575 | struct mdinfo *d; | |
7576 | struct extent *ex; | |
a20d2ba5 | 7577 | int i, j; |
272906ef | 7578 | int found; |
569cc43f DW |
7579 | __u32 array_start = 0; |
7580 | __u32 array_end = 0; | |
272906ef | 7581 | struct dl *dl; |
6c932028 | 7582 | struct mdinfo *test_list; |
272906ef DW |
7583 | |
7584 | for (dl = super->disks; dl; dl = dl->next) { | |
7585 | /* If in this array, skip */ | |
7586 | for (d = a->info.devs ; d ; d = d->next) | |
e553d2a4 DW |
7587 | if (d->state_fd >= 0 && |
7588 | d->disk.major == dl->major && | |
272906ef | 7589 | d->disk.minor == dl->minor) { |
8ba77d32 AK |
7590 | dprintf("%x:%x already in array\n", |
7591 | dl->major, dl->minor); | |
272906ef DW |
7592 | break; |
7593 | } | |
7594 | if (d) | |
7595 | continue; | |
6c932028 AK |
7596 | test_list = additional_test_list; |
7597 | while (test_list) { | |
7598 | if (test_list->disk.major == dl->major && | |
7599 | test_list->disk.minor == dl->minor) { | |
8ba77d32 AK |
7600 | dprintf("%x:%x already in additional test list\n", |
7601 | dl->major, dl->minor); | |
7602 | break; | |
7603 | } | |
6c932028 | 7604 | test_list = test_list->next; |
8ba77d32 | 7605 | } |
6c932028 | 7606 | if (test_list) |
8ba77d32 | 7607 | continue; |
272906ef | 7608 | |
e553d2a4 | 7609 | /* skip in use or failed drives */ |
25ed7e59 | 7610 | if (is_failed(&dl->disk) || idx == dl->index || |
df474657 DW |
7611 | dl->index == -2) { |
7612 | dprintf("%x:%x status (failed: %d index: %d)\n", | |
25ed7e59 | 7613 | dl->major, dl->minor, is_failed(&dl->disk), idx); |
9a1608e5 DW |
7614 | continue; |
7615 | } | |
7616 | ||
a20d2ba5 DW |
7617 | /* skip pure spares when we are looking for partially |
7618 | * assimilated drives | |
7619 | */ | |
7620 | if (dl->index == -1 && !activate_new) | |
7621 | continue; | |
7622 | ||
272906ef | 7623 | /* Does this unused device have the requisite free space? |
a20d2ba5 | 7624 | * It needs to be able to cover all member volumes |
272906ef DW |
7625 | */ |
7626 | ex = get_extents(super, dl); | |
7627 | if (!ex) { | |
7628 | dprintf("cannot get extents\n"); | |
7629 | continue; | |
7630 | } | |
a20d2ba5 DW |
7631 | for (i = 0; i < mpb->num_raid_devs; i++) { |
7632 | dev = get_imsm_dev(super, i); | |
238c0a71 | 7633 | map = get_imsm_map(dev, MAP_0); |
272906ef | 7634 | |
a20d2ba5 DW |
7635 | /* check if this disk is already a member of |
7636 | * this array | |
272906ef | 7637 | */ |
620b1713 | 7638 | if (get_imsm_disk_slot(map, dl->index) >= 0) |
a20d2ba5 DW |
7639 | continue; |
7640 | ||
7641 | found = 0; | |
7642 | j = 0; | |
7643 | pos = 0; | |
5551b113 | 7644 | array_start = pba_of_lba0(map); |
329c8278 | 7645 | array_end = array_start + |
5551b113 | 7646 | blocks_per_member(map) - 1; |
a20d2ba5 DW |
7647 | |
7648 | do { | |
7649 | /* check that we can start at pba_of_lba0 with | |
7650 | * blocks_per_member of space | |
7651 | */ | |
329c8278 | 7652 | if (array_start >= pos && array_end < ex[j].start) { |
a20d2ba5 DW |
7653 | found = 1; |
7654 | break; | |
7655 | } | |
7656 | pos = ex[j].start + ex[j].size; | |
7657 | j++; | |
7658 | } while (ex[j-1].size); | |
7659 | ||
7660 | if (!found) | |
272906ef | 7661 | break; |
a20d2ba5 | 7662 | } |
272906ef DW |
7663 | |
7664 | free(ex); | |
a20d2ba5 | 7665 | if (i < mpb->num_raid_devs) { |
329c8278 DW |
7666 | dprintf("%x:%x does not have %u to %u available\n", |
7667 | dl->major, dl->minor, array_start, array_end); | |
272906ef DW |
7668 | /* No room */ |
7669 | continue; | |
a20d2ba5 DW |
7670 | } |
7671 | return dl; | |
272906ef DW |
7672 | } |
7673 | ||
7674 | return dl; | |
7675 | } | |
7676 | ||
95d07a2c LM |
7677 | static int imsm_rebuild_allowed(struct supertype *cont, int dev_idx, int failed) |
7678 | { | |
7679 | struct imsm_dev *dev2; | |
7680 | struct imsm_map *map; | |
7681 | struct dl *idisk; | |
7682 | int slot; | |
7683 | int idx; | |
7684 | __u8 state; | |
7685 | ||
7686 | dev2 = get_imsm_dev(cont->sb, dev_idx); | |
7687 | if (dev2) { | |
238c0a71 | 7688 | state = imsm_check_degraded(cont->sb, dev2, failed, MAP_0); |
95d07a2c | 7689 | if (state == IMSM_T_STATE_FAILED) { |
238c0a71 | 7690 | map = get_imsm_map(dev2, MAP_0); |
95d07a2c LM |
7691 | if (!map) |
7692 | return 1; | |
7693 | for (slot = 0; slot < map->num_members; slot++) { | |
7694 | /* | |
7695 | * Check if failed disks are deleted from intel | |
7696 | * disk list or are marked to be deleted | |
7697 | */ | |
238c0a71 | 7698 | idx = get_imsm_disk_idx(dev2, slot, MAP_X); |
95d07a2c LM |
7699 | idisk = get_imsm_dl_disk(cont->sb, idx); |
7700 | /* | |
7701 | * Do not rebuild the array if failed disks | |
7702 | * from failed sub-array are not removed from | |
7703 | * container. | |
7704 | */ | |
7705 | if (idisk && | |
7706 | is_failed(&idisk->disk) && | |
7707 | (idisk->action != DISK_REMOVE)) | |
7708 | return 0; | |
7709 | } | |
7710 | } | |
7711 | } | |
7712 | return 1; | |
7713 | } | |
7714 | ||
88758e9d DW |
7715 | static struct mdinfo *imsm_activate_spare(struct active_array *a, |
7716 | struct metadata_update **updates) | |
7717 | { | |
7718 | /** | |
d23fe947 DW |
7719 | * Find a device with unused free space and use it to replace a |
7720 | * failed/vacant region in an array. We replace failed regions one a | |
7721 | * array at a time. The result is that a new spare disk will be added | |
7722 | * to the first failed array and after the monitor has finished | |
7723 | * propagating failures the remainder will be consumed. | |
88758e9d | 7724 | * |
d23fe947 DW |
7725 | * FIXME add a capability for mdmon to request spares from another |
7726 | * container. | |
88758e9d DW |
7727 | */ |
7728 | ||
7729 | struct intel_super *super = a->container->sb; | |
88758e9d | 7730 | int inst = a->info.container_member; |
949c47a0 | 7731 | struct imsm_dev *dev = get_imsm_dev(super, inst); |
238c0a71 | 7732 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
88758e9d DW |
7733 | int failed = a->info.array.raid_disks; |
7734 | struct mdinfo *rv = NULL; | |
7735 | struct mdinfo *d; | |
7736 | struct mdinfo *di; | |
7737 | struct metadata_update *mu; | |
7738 | struct dl *dl; | |
7739 | struct imsm_update_activate_spare *u; | |
7740 | int num_spares = 0; | |
7741 | int i; | |
95d07a2c | 7742 | int allowed; |
88758e9d DW |
7743 | |
7744 | for (d = a->info.devs ; d ; d = d->next) { | |
7745 | if ((d->curr_state & DS_FAULTY) && | |
7746 | d->state_fd >= 0) | |
7747 | /* wait for Removal to happen */ | |
7748 | return NULL; | |
7749 | if (d->state_fd >= 0) | |
7750 | failed--; | |
7751 | } | |
7752 | ||
7753 | dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n", | |
7754 | inst, failed, a->info.array.raid_disks, a->info.array.level); | |
1af97990 | 7755 | |
e2962bfc AK |
7756 | if (imsm_reshape_blocks_arrays_changes(super)) |
7757 | return NULL; | |
1af97990 | 7758 | |
fc8ca064 AK |
7759 | /* Cannot activate another spare if rebuild is in progress already |
7760 | */ | |
7761 | if (is_rebuilding(dev)) { | |
7a862a02 | 7762 | dprintf("imsm: No spare activation allowed. Rebuild in progress already.\n"); |
fc8ca064 AK |
7763 | return NULL; |
7764 | } | |
7765 | ||
89c67882 AK |
7766 | if (a->info.array.level == 4) |
7767 | /* No repair for takeovered array | |
7768 | * imsm doesn't support raid4 | |
7769 | */ | |
7770 | return NULL; | |
7771 | ||
3b451610 AK |
7772 | if (imsm_check_degraded(super, dev, failed, MAP_0) != |
7773 | IMSM_T_STATE_DEGRADED) | |
88758e9d DW |
7774 | return NULL; |
7775 | ||
95d07a2c LM |
7776 | /* |
7777 | * If there are any failed disks check state of the other volume. | |
7778 | * Block rebuild if the another one is failed until failed disks | |
7779 | * are removed from container. | |
7780 | */ | |
7781 | if (failed) { | |
7a862a02 | 7782 | dprintf("found failed disks in %.*s, check if there anotherfailed sub-array.\n", |
c4acd1e5 | 7783 | MAX_RAID_SERIAL_LEN, dev->volume); |
95d07a2c LM |
7784 | /* check if states of the other volumes allow for rebuild */ |
7785 | for (i = 0; i < super->anchor->num_raid_devs; i++) { | |
7786 | if (i != inst) { | |
7787 | allowed = imsm_rebuild_allowed(a->container, | |
7788 | i, failed); | |
7789 | if (!allowed) | |
7790 | return NULL; | |
7791 | } | |
7792 | } | |
7793 | } | |
7794 | ||
88758e9d | 7795 | /* For each slot, if it is not working, find a spare */ |
88758e9d DW |
7796 | for (i = 0; i < a->info.array.raid_disks; i++) { |
7797 | for (d = a->info.devs ; d ; d = d->next) | |
7798 | if (d->disk.raid_disk == i) | |
7799 | break; | |
7800 | dprintf("found %d: %p %x\n", i, d, d?d->curr_state:0); | |
7801 | if (d && (d->state_fd >= 0)) | |
7802 | continue; | |
7803 | ||
272906ef | 7804 | /* |
a20d2ba5 DW |
7805 | * OK, this device needs recovery. Try to re-add the |
7806 | * previous occupant of this slot, if this fails see if | |
7807 | * we can continue the assimilation of a spare that was | |
7808 | * partially assimilated, finally try to activate a new | |
7809 | * spare. | |
272906ef DW |
7810 | */ |
7811 | dl = imsm_readd(super, i, a); | |
7812 | if (!dl) | |
b303fe21 | 7813 | dl = imsm_add_spare(super, i, a, 0, rv); |
a20d2ba5 | 7814 | if (!dl) |
b303fe21 | 7815 | dl = imsm_add_spare(super, i, a, 1, rv); |
272906ef DW |
7816 | if (!dl) |
7817 | continue; | |
1011e834 | 7818 | |
272906ef | 7819 | /* found a usable disk with enough space */ |
503975b9 | 7820 | di = xcalloc(1, sizeof(*di)); |
272906ef DW |
7821 | |
7822 | /* dl->index will be -1 in the case we are activating a | |
7823 | * pristine spare. imsm_process_update() will create a | |
7824 | * new index in this case. Once a disk is found to be | |
7825 | * failed in all member arrays it is kicked from the | |
7826 | * metadata | |
7827 | */ | |
7828 | di->disk.number = dl->index; | |
d23fe947 | 7829 | |
272906ef DW |
7830 | /* (ab)use di->devs to store a pointer to the device |
7831 | * we chose | |
7832 | */ | |
7833 | di->devs = (struct mdinfo *) dl; | |
7834 | ||
7835 | di->disk.raid_disk = i; | |
7836 | di->disk.major = dl->major; | |
7837 | di->disk.minor = dl->minor; | |
7838 | di->disk.state = 0; | |
d23534e4 | 7839 | di->recovery_start = 0; |
5551b113 | 7840 | di->data_offset = pba_of_lba0(map); |
272906ef DW |
7841 | di->component_size = a->info.component_size; |
7842 | di->container_member = inst; | |
148acb7b | 7843 | super->random = random32(); |
272906ef DW |
7844 | di->next = rv; |
7845 | rv = di; | |
7846 | num_spares++; | |
7847 | dprintf("%x:%x to be %d at %llu\n", dl->major, dl->minor, | |
7848 | i, di->data_offset); | |
88758e9d DW |
7849 | } |
7850 | ||
7851 | if (!rv) | |
7852 | /* No spares found */ | |
7853 | return rv; | |
7854 | /* Now 'rv' has a list of devices to return. | |
7855 | * Create a metadata_update record to update the | |
7856 | * disk_ord_tbl for the array | |
7857 | */ | |
503975b9 | 7858 | mu = xmalloc(sizeof(*mu)); |
1011e834 | 7859 | mu->buf = xcalloc(num_spares, |
503975b9 | 7860 | sizeof(struct imsm_update_activate_spare)); |
88758e9d | 7861 | mu->space = NULL; |
cb23f1f4 | 7862 | mu->space_list = NULL; |
88758e9d DW |
7863 | mu->len = sizeof(struct imsm_update_activate_spare) * num_spares; |
7864 | mu->next = *updates; | |
7865 | u = (struct imsm_update_activate_spare *) mu->buf; | |
7866 | ||
7867 | for (di = rv ; di ; di = di->next) { | |
7868 | u->type = update_activate_spare; | |
d23fe947 DW |
7869 | u->dl = (struct dl *) di->devs; |
7870 | di->devs = NULL; | |
88758e9d DW |
7871 | u->slot = di->disk.raid_disk; |
7872 | u->array = inst; | |
7873 | u->next = u + 1; | |
7874 | u++; | |
7875 | } | |
7876 | (u-1)->next = NULL; | |
7877 | *updates = mu; | |
7878 | ||
7879 | return rv; | |
7880 | } | |
7881 | ||
54c2c1ea | 7882 | static int disks_overlap(struct intel_super *super, int idx, struct imsm_update_create_array *u) |
8273f55e | 7883 | { |
54c2c1ea | 7884 | struct imsm_dev *dev = get_imsm_dev(super, idx); |
238c0a71 AK |
7885 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
7886 | struct imsm_map *new_map = get_imsm_map(&u->dev, MAP_0); | |
54c2c1ea DW |
7887 | struct disk_info *inf = get_disk_info(u); |
7888 | struct imsm_disk *disk; | |
8273f55e DW |
7889 | int i; |
7890 | int j; | |
8273f55e | 7891 | |
54c2c1ea | 7892 | for (i = 0; i < map->num_members; i++) { |
238c0a71 | 7893 | disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i, MAP_X)); |
54c2c1ea DW |
7894 | for (j = 0; j < new_map->num_members; j++) |
7895 | if (serialcmp(disk->serial, inf[j].serial) == 0) | |
8273f55e DW |
7896 | return 1; |
7897 | } | |
7898 | ||
7899 | return 0; | |
7900 | } | |
7901 | ||
1a64be56 LM |
7902 | static struct dl *get_disk_super(struct intel_super *super, int major, int minor) |
7903 | { | |
7904 | struct dl *dl = NULL; | |
7905 | for (dl = super->disks; dl; dl = dl->next) | |
7906 | if ((dl->major == major) && (dl->minor == minor)) | |
7907 | return dl; | |
7908 | return NULL; | |
7909 | } | |
7910 | ||
7911 | static int remove_disk_super(struct intel_super *super, int major, int minor) | |
7912 | { | |
7913 | struct dl *prev = NULL; | |
7914 | struct dl *dl; | |
7915 | ||
7916 | prev = NULL; | |
7917 | for (dl = super->disks; dl; dl = dl->next) { | |
7918 | if ((dl->major == major) && (dl->minor == minor)) { | |
7919 | /* remove */ | |
7920 | if (prev) | |
7921 | prev->next = dl->next; | |
7922 | else | |
7923 | super->disks = dl->next; | |
7924 | dl->next = NULL; | |
7925 | __free_imsm_disk(dl); | |
1ade5cc1 | 7926 | dprintf("removed %x:%x\n", major, minor); |
1a64be56 LM |
7927 | break; |
7928 | } | |
7929 | prev = dl; | |
7930 | } | |
7931 | return 0; | |
7932 | } | |
7933 | ||
f21e18ca | 7934 | static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index); |
ae6aad82 | 7935 | |
1a64be56 LM |
7936 | static int add_remove_disk_update(struct intel_super *super) |
7937 | { | |
7938 | int check_degraded = 0; | |
7939 | struct dl *disk = NULL; | |
7940 | /* add/remove some spares to/from the metadata/contrainer */ | |
7941 | while (super->disk_mgmt_list) { | |
7942 | struct dl *disk_cfg; | |
7943 | ||
7944 | disk_cfg = super->disk_mgmt_list; | |
7945 | super->disk_mgmt_list = disk_cfg->next; | |
7946 | disk_cfg->next = NULL; | |
7947 | ||
7948 | if (disk_cfg->action == DISK_ADD) { | |
7949 | disk_cfg->next = super->disks; | |
7950 | super->disks = disk_cfg; | |
7951 | check_degraded = 1; | |
1ade5cc1 N |
7952 | dprintf("added %x:%x\n", |
7953 | disk_cfg->major, disk_cfg->minor); | |
1a64be56 LM |
7954 | } else if (disk_cfg->action == DISK_REMOVE) { |
7955 | dprintf("Disk remove action processed: %x.%x\n", | |
7956 | disk_cfg->major, disk_cfg->minor); | |
7957 | disk = get_disk_super(super, | |
7958 | disk_cfg->major, | |
7959 | disk_cfg->minor); | |
7960 | if (disk) { | |
7961 | /* store action status */ | |
7962 | disk->action = DISK_REMOVE; | |
7963 | /* remove spare disks only */ | |
7964 | if (disk->index == -1) { | |
7965 | remove_disk_super(super, | |
7966 | disk_cfg->major, | |
7967 | disk_cfg->minor); | |
7968 | } | |
7969 | } | |
7970 | /* release allocate disk structure */ | |
7971 | __free_imsm_disk(disk_cfg); | |
7972 | } | |
7973 | } | |
7974 | return check_degraded; | |
7975 | } | |
7976 | ||
a29911da PC |
7977 | static int apply_reshape_migration_update(struct imsm_update_reshape_migration *u, |
7978 | struct intel_super *super, | |
7979 | void ***space_list) | |
7980 | { | |
7981 | struct intel_dev *id; | |
7982 | void **tofree = NULL; | |
7983 | int ret_val = 0; | |
7984 | ||
1ade5cc1 | 7985 | dprintf("(enter)\n"); |
a29911da PC |
7986 | if ((u->subdev < 0) || |
7987 | (u->subdev > 1)) { | |
7988 | dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev); | |
7989 | return ret_val; | |
7990 | } | |
7991 | if ((space_list == NULL) || (*space_list == NULL)) { | |
7992 | dprintf("imsm: Error: Memory is not allocated\n"); | |
7993 | return ret_val; | |
7994 | } | |
7995 | ||
7996 | for (id = super->devlist ; id; id = id->next) { | |
7997 | if (id->index == (unsigned)u->subdev) { | |
7998 | struct imsm_dev *dev = get_imsm_dev(super, u->subdev); | |
7999 | struct imsm_map *map; | |
8000 | struct imsm_dev *new_dev = | |
8001 | (struct imsm_dev *)*space_list; | |
238c0a71 | 8002 | struct imsm_map *migr_map = get_imsm_map(dev, MAP_1); |
a29911da PC |
8003 | int to_state; |
8004 | struct dl *new_disk; | |
8005 | ||
8006 | if (new_dev == NULL) | |
8007 | return ret_val; | |
8008 | *space_list = **space_list; | |
8009 | memcpy(new_dev, dev, sizeof_imsm_dev(dev, 0)); | |
238c0a71 | 8010 | map = get_imsm_map(new_dev, MAP_0); |
a29911da PC |
8011 | if (migr_map) { |
8012 | dprintf("imsm: Error: migration in progress"); | |
8013 | return ret_val; | |
8014 | } | |
8015 | ||
8016 | to_state = map->map_state; | |
8017 | if ((u->new_level == 5) && (map->raid_level == 0)) { | |
8018 | map->num_members++; | |
8019 | /* this should not happen */ | |
8020 | if (u->new_disks[0] < 0) { | |
8021 | map->failed_disk_num = | |
8022 | map->num_members - 1; | |
8023 | to_state = IMSM_T_STATE_DEGRADED; | |
8024 | } else | |
8025 | to_state = IMSM_T_STATE_NORMAL; | |
8026 | } | |
8e59f3d8 | 8027 | migrate(new_dev, super, to_state, MIGR_GEN_MIGR); |
a29911da PC |
8028 | if (u->new_level > -1) |
8029 | map->raid_level = u->new_level; | |
238c0a71 | 8030 | migr_map = get_imsm_map(new_dev, MAP_1); |
a29911da PC |
8031 | if ((u->new_level == 5) && |
8032 | (migr_map->raid_level == 0)) { | |
8033 | int ord = map->num_members - 1; | |
8034 | migr_map->num_members--; | |
8035 | if (u->new_disks[0] < 0) | |
8036 | ord |= IMSM_ORD_REBUILD; | |
8037 | set_imsm_ord_tbl_ent(map, | |
8038 | map->num_members - 1, | |
8039 | ord); | |
8040 | } | |
8041 | id->dev = new_dev; | |
8042 | tofree = (void **)dev; | |
8043 | ||
4bba0439 PC |
8044 | /* update chunk size |
8045 | */ | |
8046 | if (u->new_chunksize > 0) | |
8047 | map->blocks_per_strip = | |
8048 | __cpu_to_le16(u->new_chunksize * 2); | |
8049 | ||
a29911da PC |
8050 | /* add disk |
8051 | */ | |
8052 | if ((u->new_level != 5) || | |
8053 | (migr_map->raid_level != 0) || | |
8054 | (migr_map->raid_level == map->raid_level)) | |
8055 | goto skip_disk_add; | |
8056 | ||
8057 | if (u->new_disks[0] >= 0) { | |
8058 | /* use passes spare | |
8059 | */ | |
8060 | new_disk = get_disk_super(super, | |
8061 | major(u->new_disks[0]), | |
8062 | minor(u->new_disks[0])); | |
7a862a02 | 8063 | dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n", |
a29911da PC |
8064 | major(u->new_disks[0]), |
8065 | minor(u->new_disks[0]), | |
8066 | new_disk, new_disk->index); | |
8067 | if (new_disk == NULL) | |
8068 | goto error_disk_add; | |
8069 | ||
8070 | new_disk->index = map->num_members - 1; | |
8071 | /* slot to fill in autolayout | |
8072 | */ | |
8073 | new_disk->raiddisk = new_disk->index; | |
8074 | new_disk->disk.status |= CONFIGURED_DISK; | |
8075 | new_disk->disk.status &= ~SPARE_DISK; | |
8076 | } else | |
8077 | goto error_disk_add; | |
8078 | ||
8079 | skip_disk_add: | |
8080 | *tofree = *space_list; | |
8081 | /* calculate new size | |
8082 | */ | |
f3871fdc | 8083 | imsm_set_array_size(new_dev, -1); |
a29911da PC |
8084 | |
8085 | ret_val = 1; | |
8086 | } | |
8087 | } | |
8088 | ||
8089 | if (tofree) | |
8090 | *space_list = tofree; | |
8091 | return ret_val; | |
8092 | ||
8093 | error_disk_add: | |
8094 | dprintf("Error: imsm: Cannot find disk.\n"); | |
8095 | return ret_val; | |
8096 | } | |
8097 | ||
f3871fdc AK |
8098 | static int apply_size_change_update(struct imsm_update_size_change *u, |
8099 | struct intel_super *super) | |
8100 | { | |
8101 | struct intel_dev *id; | |
8102 | int ret_val = 0; | |
8103 | ||
1ade5cc1 | 8104 | dprintf("(enter)\n"); |
f3871fdc AK |
8105 | if ((u->subdev < 0) || |
8106 | (u->subdev > 1)) { | |
8107 | dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev); | |
8108 | return ret_val; | |
8109 | } | |
8110 | ||
8111 | for (id = super->devlist ; id; id = id->next) { | |
8112 | if (id->index == (unsigned)u->subdev) { | |
8113 | struct imsm_dev *dev = get_imsm_dev(super, u->subdev); | |
8114 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
8115 | int used_disks = imsm_num_data_members(dev, MAP_0); | |
8116 | unsigned long long blocks_per_member; | |
8117 | ||
8118 | /* calculate new size | |
8119 | */ | |
8120 | blocks_per_member = u->new_size / used_disks; | |
1ade5cc1 | 8121 | dprintf("(size: %llu, blocks per member: %llu)\n", |
f3871fdc AK |
8122 | u->new_size, blocks_per_member); |
8123 | set_blocks_per_member(map, blocks_per_member); | |
8124 | imsm_set_array_size(dev, u->new_size); | |
8125 | ||
8126 | ret_val = 1; | |
8127 | break; | |
8128 | } | |
8129 | } | |
8130 | ||
8131 | return ret_val; | |
8132 | } | |
8133 | ||
061d7da3 | 8134 | static int apply_update_activate_spare(struct imsm_update_activate_spare *u, |
ca9de185 | 8135 | struct intel_super *super, |
061d7da3 LO |
8136 | struct active_array *active_array) |
8137 | { | |
8138 | struct imsm_super *mpb = super->anchor; | |
8139 | struct imsm_dev *dev = get_imsm_dev(super, u->array); | |
238c0a71 | 8140 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
061d7da3 LO |
8141 | struct imsm_map *migr_map; |
8142 | struct active_array *a; | |
8143 | struct imsm_disk *disk; | |
8144 | __u8 to_state; | |
8145 | struct dl *dl; | |
8146 | unsigned int found; | |
8147 | int failed; | |
5961eeec | 8148 | int victim; |
061d7da3 | 8149 | int i; |
5961eeec | 8150 | int second_map_created = 0; |
061d7da3 | 8151 | |
5961eeec | 8152 | for (; u; u = u->next) { |
238c0a71 | 8153 | victim = get_imsm_disk_idx(dev, u->slot, MAP_X); |
061d7da3 | 8154 | |
5961eeec | 8155 | if (victim < 0) |
8156 | return 0; | |
061d7da3 | 8157 | |
5961eeec | 8158 | for (dl = super->disks; dl; dl = dl->next) |
8159 | if (dl == u->dl) | |
8160 | break; | |
061d7da3 | 8161 | |
5961eeec | 8162 | if (!dl) { |
7a862a02 | 8163 | pr_err("error: imsm_activate_spare passed an unknown disk (index: %d)\n", |
5961eeec | 8164 | u->dl->index); |
8165 | return 0; | |
8166 | } | |
061d7da3 | 8167 | |
5961eeec | 8168 | /* count failures (excluding rebuilds and the victim) |
8169 | * to determine map[0] state | |
8170 | */ | |
8171 | failed = 0; | |
8172 | for (i = 0; i < map->num_members; i++) { | |
8173 | if (i == u->slot) | |
8174 | continue; | |
8175 | disk = get_imsm_disk(super, | |
238c0a71 | 8176 | get_imsm_disk_idx(dev, i, MAP_X)); |
5961eeec | 8177 | if (!disk || is_failed(disk)) |
8178 | failed++; | |
8179 | } | |
061d7da3 | 8180 | |
5961eeec | 8181 | /* adding a pristine spare, assign a new index */ |
8182 | if (dl->index < 0) { | |
8183 | dl->index = super->anchor->num_disks; | |
8184 | super->anchor->num_disks++; | |
8185 | } | |
8186 | disk = &dl->disk; | |
8187 | disk->status |= CONFIGURED_DISK; | |
8188 | disk->status &= ~SPARE_DISK; | |
8189 | ||
8190 | /* mark rebuild */ | |
238c0a71 | 8191 | to_state = imsm_check_degraded(super, dev, failed, MAP_0); |
5961eeec | 8192 | if (!second_map_created) { |
8193 | second_map_created = 1; | |
8194 | map->map_state = IMSM_T_STATE_DEGRADED; | |
8195 | migrate(dev, super, to_state, MIGR_REBUILD); | |
8196 | } else | |
8197 | map->map_state = to_state; | |
238c0a71 | 8198 | migr_map = get_imsm_map(dev, MAP_1); |
5961eeec | 8199 | set_imsm_ord_tbl_ent(map, u->slot, dl->index); |
8200 | set_imsm_ord_tbl_ent(migr_map, u->slot, | |
8201 | dl->index | IMSM_ORD_REBUILD); | |
8202 | ||
8203 | /* update the family_num to mark a new container | |
8204 | * generation, being careful to record the existing | |
8205 | * family_num in orig_family_num to clean up after | |
8206 | * earlier mdadm versions that neglected to set it. | |
8207 | */ | |
8208 | if (mpb->orig_family_num == 0) | |
8209 | mpb->orig_family_num = mpb->family_num; | |
8210 | mpb->family_num += super->random; | |
8211 | ||
8212 | /* count arrays using the victim in the metadata */ | |
8213 | found = 0; | |
8214 | for (a = active_array; a ; a = a->next) { | |
8215 | dev = get_imsm_dev(super, a->info.container_member); | |
238c0a71 | 8216 | map = get_imsm_map(dev, MAP_0); |
061d7da3 | 8217 | |
5961eeec | 8218 | if (get_imsm_disk_slot(map, victim) >= 0) |
8219 | found++; | |
8220 | } | |
061d7da3 | 8221 | |
5961eeec | 8222 | /* delete the victim if it is no longer being |
8223 | * utilized anywhere | |
061d7da3 | 8224 | */ |
5961eeec | 8225 | if (!found) { |
8226 | struct dl **dlp; | |
061d7da3 | 8227 | |
5961eeec | 8228 | /* We know that 'manager' isn't touching anything, |
8229 | * so it is safe to delete | |
8230 | */ | |
8231 | for (dlp = &super->disks; *dlp; dlp = &(*dlp)->next) | |
061d7da3 LO |
8232 | if ((*dlp)->index == victim) |
8233 | break; | |
5961eeec | 8234 | |
8235 | /* victim may be on the missing list */ | |
8236 | if (!*dlp) | |
8237 | for (dlp = &super->missing; *dlp; | |
8238 | dlp = &(*dlp)->next) | |
8239 | if ((*dlp)->index == victim) | |
8240 | break; | |
8241 | imsm_delete(super, dlp, victim); | |
8242 | } | |
061d7da3 LO |
8243 | } |
8244 | ||
8245 | return 1; | |
8246 | } | |
a29911da | 8247 | |
2e5dc010 N |
8248 | static int apply_reshape_container_disks_update(struct imsm_update_reshape *u, |
8249 | struct intel_super *super, | |
8250 | void ***space_list) | |
8251 | { | |
8252 | struct dl *new_disk; | |
8253 | struct intel_dev *id; | |
8254 | int i; | |
8255 | int delta_disks = u->new_raid_disks - u->old_raid_disks; | |
ee4beede | 8256 | int disk_count = u->old_raid_disks; |
2e5dc010 N |
8257 | void **tofree = NULL; |
8258 | int devices_to_reshape = 1; | |
8259 | struct imsm_super *mpb = super->anchor; | |
8260 | int ret_val = 0; | |
d098291a | 8261 | unsigned int dev_id; |
2e5dc010 | 8262 | |
1ade5cc1 | 8263 | dprintf("(enter)\n"); |
2e5dc010 N |
8264 | |
8265 | /* enable spares to use in array */ | |
8266 | for (i = 0; i < delta_disks; i++) { | |
8267 | new_disk = get_disk_super(super, | |
8268 | major(u->new_disks[i]), | |
8269 | minor(u->new_disks[i])); | |
7a862a02 | 8270 | dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n", |
2e5dc010 N |
8271 | major(u->new_disks[i]), minor(u->new_disks[i]), |
8272 | new_disk, new_disk->index); | |
8273 | if ((new_disk == NULL) || | |
8274 | ((new_disk->index >= 0) && | |
8275 | (new_disk->index < u->old_raid_disks))) | |
8276 | goto update_reshape_exit; | |
ee4beede | 8277 | new_disk->index = disk_count++; |
2e5dc010 N |
8278 | /* slot to fill in autolayout |
8279 | */ | |
8280 | new_disk->raiddisk = new_disk->index; | |
8281 | new_disk->disk.status |= | |
8282 | CONFIGURED_DISK; | |
8283 | new_disk->disk.status &= ~SPARE_DISK; | |
8284 | } | |
8285 | ||
ed7333bd AK |
8286 | dprintf("imsm: volume set mpb->num_raid_devs = %i\n", |
8287 | mpb->num_raid_devs); | |
2e5dc010 N |
8288 | /* manage changes in volume |
8289 | */ | |
d098291a | 8290 | for (dev_id = 0; dev_id < mpb->num_raid_devs; dev_id++) { |
2e5dc010 N |
8291 | void **sp = *space_list; |
8292 | struct imsm_dev *newdev; | |
8293 | struct imsm_map *newmap, *oldmap; | |
8294 | ||
d098291a AK |
8295 | for (id = super->devlist ; id; id = id->next) { |
8296 | if (id->index == dev_id) | |
8297 | break; | |
8298 | } | |
8299 | if (id == NULL) | |
8300 | break; | |
2e5dc010 N |
8301 | if (!sp) |
8302 | continue; | |
8303 | *space_list = *sp; | |
8304 | newdev = (void*)sp; | |
8305 | /* Copy the dev, but not (all of) the map */ | |
8306 | memcpy(newdev, id->dev, sizeof(*newdev)); | |
238c0a71 AK |
8307 | oldmap = get_imsm_map(id->dev, MAP_0); |
8308 | newmap = get_imsm_map(newdev, MAP_0); | |
2e5dc010 N |
8309 | /* Copy the current map */ |
8310 | memcpy(newmap, oldmap, sizeof_imsm_map(oldmap)); | |
8311 | /* update one device only | |
8312 | */ | |
8313 | if (devices_to_reshape) { | |
ed7333bd AK |
8314 | dprintf("imsm: modifying subdev: %i\n", |
8315 | id->index); | |
2e5dc010 N |
8316 | devices_to_reshape--; |
8317 | newdev->vol.migr_state = 1; | |
8318 | newdev->vol.curr_migr_unit = 0; | |
ea672ee1 | 8319 | set_migr_type(newdev, MIGR_GEN_MIGR); |
2e5dc010 N |
8320 | newmap->num_members = u->new_raid_disks; |
8321 | for (i = 0; i < delta_disks; i++) { | |
8322 | set_imsm_ord_tbl_ent(newmap, | |
8323 | u->old_raid_disks + i, | |
8324 | u->old_raid_disks + i); | |
8325 | } | |
8326 | /* New map is correct, now need to save old map | |
8327 | */ | |
238c0a71 | 8328 | newmap = get_imsm_map(newdev, MAP_1); |
2e5dc010 N |
8329 | memcpy(newmap, oldmap, sizeof_imsm_map(oldmap)); |
8330 | ||
f3871fdc | 8331 | imsm_set_array_size(newdev, -1); |
2e5dc010 N |
8332 | } |
8333 | ||
8334 | sp = (void **)id->dev; | |
8335 | id->dev = newdev; | |
8336 | *sp = tofree; | |
8337 | tofree = sp; | |
8e59f3d8 AK |
8338 | |
8339 | /* Clear migration record */ | |
8340 | memset(super->migr_rec, 0, sizeof(struct migr_record)); | |
2e5dc010 | 8341 | } |
819bc634 AK |
8342 | if (tofree) |
8343 | *space_list = tofree; | |
2e5dc010 N |
8344 | ret_val = 1; |
8345 | ||
8346 | update_reshape_exit: | |
8347 | ||
8348 | return ret_val; | |
8349 | } | |
8350 | ||
bb025c2f | 8351 | static int apply_takeover_update(struct imsm_update_takeover *u, |
8ca6df95 KW |
8352 | struct intel_super *super, |
8353 | void ***space_list) | |
bb025c2f KW |
8354 | { |
8355 | struct imsm_dev *dev = NULL; | |
8ca6df95 KW |
8356 | struct intel_dev *dv; |
8357 | struct imsm_dev *dev_new; | |
bb025c2f KW |
8358 | struct imsm_map *map; |
8359 | struct dl *dm, *du; | |
8ca6df95 | 8360 | int i; |
bb025c2f KW |
8361 | |
8362 | for (dv = super->devlist; dv; dv = dv->next) | |
8363 | if (dv->index == (unsigned int)u->subarray) { | |
8364 | dev = dv->dev; | |
8365 | break; | |
8366 | } | |
8367 | ||
8368 | if (dev == NULL) | |
8369 | return 0; | |
8370 | ||
238c0a71 | 8371 | map = get_imsm_map(dev, MAP_0); |
bb025c2f KW |
8372 | |
8373 | if (u->direction == R10_TO_R0) { | |
43d5ec18 | 8374 | /* Number of failed disks must be half of initial disk number */ |
3b451610 AK |
8375 | if (imsm_count_failed(super, dev, MAP_0) != |
8376 | (map->num_members / 2)) | |
43d5ec18 KW |
8377 | return 0; |
8378 | ||
bb025c2f KW |
8379 | /* iterate through devices to mark removed disks as spare */ |
8380 | for (dm = super->disks; dm; dm = dm->next) { | |
8381 | if (dm->disk.status & FAILED_DISK) { | |
8382 | int idx = dm->index; | |
8383 | /* update indexes on the disk list */ | |
8384 | /* FIXME this loop-with-the-loop looks wrong, I'm not convinced | |
8385 | the index values will end up being correct.... NB */ | |
8386 | for (du = super->disks; du; du = du->next) | |
8387 | if (du->index > idx) | |
8388 | du->index--; | |
8389 | /* mark as spare disk */ | |
a8619d23 | 8390 | mark_spare(dm); |
bb025c2f KW |
8391 | } |
8392 | } | |
bb025c2f KW |
8393 | /* update map */ |
8394 | map->num_members = map->num_members / 2; | |
8395 | map->map_state = IMSM_T_STATE_NORMAL; | |
8396 | map->num_domains = 1; | |
8397 | map->raid_level = 0; | |
8398 | map->failed_disk_num = -1; | |
8399 | } | |
8400 | ||
8ca6df95 KW |
8401 | if (u->direction == R0_TO_R10) { |
8402 | void **space; | |
8403 | /* update slots in current disk list */ | |
8404 | for (dm = super->disks; dm; dm = dm->next) { | |
8405 | if (dm->index >= 0) | |
8406 | dm->index *= 2; | |
8407 | } | |
8408 | /* create new *missing* disks */ | |
8409 | for (i = 0; i < map->num_members; i++) { | |
8410 | space = *space_list; | |
8411 | if (!space) | |
8412 | continue; | |
8413 | *space_list = *space; | |
8414 | du = (void *)space; | |
8415 | memcpy(du, super->disks, sizeof(*du)); | |
8ca6df95 KW |
8416 | du->fd = -1; |
8417 | du->minor = 0; | |
8418 | du->major = 0; | |
8419 | du->index = (i * 2) + 1; | |
8420 | sprintf((char *)du->disk.serial, | |
8421 | " MISSING_%d", du->index); | |
8422 | sprintf((char *)du->serial, | |
8423 | "MISSING_%d", du->index); | |
8424 | du->next = super->missing; | |
8425 | super->missing = du; | |
8426 | } | |
8427 | /* create new dev and map */ | |
8428 | space = *space_list; | |
8429 | if (!space) | |
8430 | return 0; | |
8431 | *space_list = *space; | |
8432 | dev_new = (void *)space; | |
8433 | memcpy(dev_new, dev, sizeof(*dev)); | |
8434 | /* update new map */ | |
238c0a71 | 8435 | map = get_imsm_map(dev_new, MAP_0); |
8ca6df95 | 8436 | map->num_members = map->num_members * 2; |
1a2487c2 | 8437 | map->map_state = IMSM_T_STATE_DEGRADED; |
8ca6df95 KW |
8438 | map->num_domains = 2; |
8439 | map->raid_level = 1; | |
8440 | /* replace dev<->dev_new */ | |
8441 | dv->dev = dev_new; | |
8442 | } | |
bb025c2f KW |
8443 | /* update disk order table */ |
8444 | for (du = super->disks; du; du = du->next) | |
8445 | if (du->index >= 0) | |
8446 | set_imsm_ord_tbl_ent(map, du->index, du->index); | |
8ca6df95 | 8447 | for (du = super->missing; du; du = du->next) |
1a2487c2 KW |
8448 | if (du->index >= 0) { |
8449 | set_imsm_ord_tbl_ent(map, du->index, du->index); | |
e4c72d1d | 8450 | mark_missing(dv->dev, &du->disk, du->index); |
1a2487c2 | 8451 | } |
bb025c2f KW |
8452 | |
8453 | return 1; | |
8454 | } | |
8455 | ||
e8319a19 DW |
8456 | static void imsm_process_update(struct supertype *st, |
8457 | struct metadata_update *update) | |
8458 | { | |
8459 | /** | |
8460 | * crack open the metadata_update envelope to find the update record | |
8461 | * update can be one of: | |
d195167d AK |
8462 | * update_reshape_container_disks - all the arrays in the container |
8463 | * are being reshaped to have more devices. We need to mark | |
8464 | * the arrays for general migration and convert selected spares | |
8465 | * into active devices. | |
8466 | * update_activate_spare - a spare device has replaced a failed | |
1011e834 N |
8467 | * device in an array, update the disk_ord_tbl. If this disk is |
8468 | * present in all member arrays then also clear the SPARE_DISK | |
8469 | * flag | |
d195167d AK |
8470 | * update_create_array |
8471 | * update_kill_array | |
8472 | * update_rename_array | |
8473 | * update_add_remove_disk | |
e8319a19 DW |
8474 | */ |
8475 | struct intel_super *super = st->sb; | |
4d7b1503 | 8476 | struct imsm_super *mpb; |
e8319a19 DW |
8477 | enum imsm_update_type type = *(enum imsm_update_type *) update->buf; |
8478 | ||
4d7b1503 DW |
8479 | /* update requires a larger buf but the allocation failed */ |
8480 | if (super->next_len && !super->next_buf) { | |
8481 | super->next_len = 0; | |
8482 | return; | |
8483 | } | |
8484 | ||
8485 | if (super->next_buf) { | |
8486 | memcpy(super->next_buf, super->buf, super->len); | |
8487 | free(super->buf); | |
8488 | super->len = super->next_len; | |
8489 | super->buf = super->next_buf; | |
8490 | ||
8491 | super->next_len = 0; | |
8492 | super->next_buf = NULL; | |
8493 | } | |
8494 | ||
8495 | mpb = super->anchor; | |
8496 | ||
e8319a19 | 8497 | switch (type) { |
0ec5d470 AK |
8498 | case update_general_migration_checkpoint: { |
8499 | struct intel_dev *id; | |
8500 | struct imsm_update_general_migration_checkpoint *u = | |
8501 | (void *)update->buf; | |
8502 | ||
1ade5cc1 | 8503 | dprintf("called for update_general_migration_checkpoint\n"); |
0ec5d470 AK |
8504 | |
8505 | /* find device under general migration */ | |
8506 | for (id = super->devlist ; id; id = id->next) { | |
8507 | if (is_gen_migration(id->dev)) { | |
8508 | id->dev->vol.curr_migr_unit = | |
8509 | __cpu_to_le32(u->curr_migr_unit); | |
8510 | super->updates_pending++; | |
8511 | } | |
8512 | } | |
8513 | break; | |
8514 | } | |
bb025c2f KW |
8515 | case update_takeover: { |
8516 | struct imsm_update_takeover *u = (void *)update->buf; | |
1a2487c2 KW |
8517 | if (apply_takeover_update(u, super, &update->space_list)) { |
8518 | imsm_update_version_info(super); | |
bb025c2f | 8519 | super->updates_pending++; |
1a2487c2 | 8520 | } |
bb025c2f KW |
8521 | break; |
8522 | } | |
8523 | ||
78b10e66 | 8524 | case update_reshape_container_disks: { |
d195167d | 8525 | struct imsm_update_reshape *u = (void *)update->buf; |
2e5dc010 N |
8526 | if (apply_reshape_container_disks_update( |
8527 | u, super, &update->space_list)) | |
8528 | super->updates_pending++; | |
78b10e66 N |
8529 | break; |
8530 | } | |
48c5303a | 8531 | case update_reshape_migration: { |
a29911da PC |
8532 | struct imsm_update_reshape_migration *u = (void *)update->buf; |
8533 | if (apply_reshape_migration_update( | |
8534 | u, super, &update->space_list)) | |
8535 | super->updates_pending++; | |
48c5303a PC |
8536 | break; |
8537 | } | |
f3871fdc AK |
8538 | case update_size_change: { |
8539 | struct imsm_update_size_change *u = (void *)update->buf; | |
8540 | if (apply_size_change_update(u, super)) | |
8541 | super->updates_pending++; | |
8542 | break; | |
8543 | } | |
e8319a19 | 8544 | case update_activate_spare: { |
1011e834 | 8545 | struct imsm_update_activate_spare *u = (void *) update->buf; |
061d7da3 LO |
8546 | if (apply_update_activate_spare(u, super, st->arrays)) |
8547 | super->updates_pending++; | |
8273f55e DW |
8548 | break; |
8549 | } | |
8550 | case update_create_array: { | |
8551 | /* someone wants to create a new array, we need to be aware of | |
8552 | * a few races/collisions: | |
8553 | * 1/ 'Create' called by two separate instances of mdadm | |
8554 | * 2/ 'Create' versus 'activate_spare': mdadm has chosen | |
8555 | * devices that have since been assimilated via | |
8556 | * activate_spare. | |
8557 | * In the event this update can not be carried out mdadm will | |
8558 | * (FIX ME) notice that its update did not take hold. | |
8559 | */ | |
8560 | struct imsm_update_create_array *u = (void *) update->buf; | |
ba2de7ba | 8561 | struct intel_dev *dv; |
8273f55e DW |
8562 | struct imsm_dev *dev; |
8563 | struct imsm_map *map, *new_map; | |
8564 | unsigned long long start, end; | |
8565 | unsigned long long new_start, new_end; | |
8566 | int i; | |
54c2c1ea DW |
8567 | struct disk_info *inf; |
8568 | struct dl *dl; | |
8273f55e DW |
8569 | |
8570 | /* handle racing creates: first come first serve */ | |
8571 | if (u->dev_idx < mpb->num_raid_devs) { | |
1ade5cc1 | 8572 | dprintf("subarray %d already defined\n", u->dev_idx); |
ba2de7ba | 8573 | goto create_error; |
8273f55e DW |
8574 | } |
8575 | ||
8576 | /* check update is next in sequence */ | |
8577 | if (u->dev_idx != mpb->num_raid_devs) { | |
1ade5cc1 N |
8578 | dprintf("can not create array %d expected index %d\n", |
8579 | u->dev_idx, mpb->num_raid_devs); | |
ba2de7ba | 8580 | goto create_error; |
8273f55e DW |
8581 | } |
8582 | ||
238c0a71 | 8583 | new_map = get_imsm_map(&u->dev, MAP_0); |
5551b113 CA |
8584 | new_start = pba_of_lba0(new_map); |
8585 | new_end = new_start + blocks_per_member(new_map); | |
54c2c1ea | 8586 | inf = get_disk_info(u); |
8273f55e DW |
8587 | |
8588 | /* handle activate_spare versus create race: | |
8589 | * check to make sure that overlapping arrays do not include | |
8590 | * overalpping disks | |
8591 | */ | |
8592 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
949c47a0 | 8593 | dev = get_imsm_dev(super, i); |
238c0a71 | 8594 | map = get_imsm_map(dev, MAP_0); |
5551b113 CA |
8595 | start = pba_of_lba0(map); |
8596 | end = start + blocks_per_member(map); | |
8273f55e DW |
8597 | if ((new_start >= start && new_start <= end) || |
8598 | (start >= new_start && start <= new_end)) | |
54c2c1ea DW |
8599 | /* overlap */; |
8600 | else | |
8601 | continue; | |
8602 | ||
8603 | if (disks_overlap(super, i, u)) { | |
1ade5cc1 | 8604 | dprintf("arrays overlap\n"); |
ba2de7ba | 8605 | goto create_error; |
8273f55e DW |
8606 | } |
8607 | } | |
8273f55e | 8608 | |
949c47a0 DW |
8609 | /* check that prepare update was successful */ |
8610 | if (!update->space) { | |
1ade5cc1 | 8611 | dprintf("prepare update failed\n"); |
ba2de7ba | 8612 | goto create_error; |
949c47a0 DW |
8613 | } |
8614 | ||
54c2c1ea DW |
8615 | /* check that all disks are still active before committing |
8616 | * changes. FIXME: could we instead handle this by creating a | |
8617 | * degraded array? That's probably not what the user expects, | |
8618 | * so better to drop this update on the floor. | |
8619 | */ | |
8620 | for (i = 0; i < new_map->num_members; i++) { | |
8621 | dl = serial_to_dl(inf[i].serial, super); | |
8622 | if (!dl) { | |
1ade5cc1 | 8623 | dprintf("disk disappeared\n"); |
ba2de7ba | 8624 | goto create_error; |
54c2c1ea | 8625 | } |
949c47a0 DW |
8626 | } |
8627 | ||
8273f55e | 8628 | super->updates_pending++; |
54c2c1ea DW |
8629 | |
8630 | /* convert spares to members and fixup ord_tbl */ | |
8631 | for (i = 0; i < new_map->num_members; i++) { | |
8632 | dl = serial_to_dl(inf[i].serial, super); | |
8633 | if (dl->index == -1) { | |
8634 | dl->index = mpb->num_disks; | |
8635 | mpb->num_disks++; | |
8636 | dl->disk.status |= CONFIGURED_DISK; | |
8637 | dl->disk.status &= ~SPARE_DISK; | |
8638 | } | |
8639 | set_imsm_ord_tbl_ent(new_map, i, dl->index); | |
8640 | } | |
8641 | ||
ba2de7ba DW |
8642 | dv = update->space; |
8643 | dev = dv->dev; | |
949c47a0 DW |
8644 | update->space = NULL; |
8645 | imsm_copy_dev(dev, &u->dev); | |
ba2de7ba DW |
8646 | dv->index = u->dev_idx; |
8647 | dv->next = super->devlist; | |
8648 | super->devlist = dv; | |
8273f55e | 8649 | mpb->num_raid_devs++; |
8273f55e | 8650 | |
4d1313e9 | 8651 | imsm_update_version_info(super); |
8273f55e | 8652 | break; |
ba2de7ba DW |
8653 | create_error: |
8654 | /* mdmon knows how to release update->space, but not | |
8655 | * ((struct intel_dev *) update->space)->dev | |
8656 | */ | |
8657 | if (update->space) { | |
8658 | dv = update->space; | |
8659 | free(dv->dev); | |
8660 | } | |
8273f55e | 8661 | break; |
e8319a19 | 8662 | } |
33414a01 DW |
8663 | case update_kill_array: { |
8664 | struct imsm_update_kill_array *u = (void *) update->buf; | |
8665 | int victim = u->dev_idx; | |
8666 | struct active_array *a; | |
8667 | struct intel_dev **dp; | |
8668 | struct imsm_dev *dev; | |
8669 | ||
8670 | /* sanity check that we are not affecting the uuid of | |
8671 | * active arrays, or deleting an active array | |
8672 | * | |
8673 | * FIXME when immutable ids are available, but note that | |
8674 | * we'll also need to fixup the invalidated/active | |
8675 | * subarray indexes in mdstat | |
8676 | */ | |
8677 | for (a = st->arrays; a; a = a->next) | |
8678 | if (a->info.container_member >= victim) | |
8679 | break; | |
8680 | /* by definition if mdmon is running at least one array | |
8681 | * is active in the container, so checking | |
8682 | * mpb->num_raid_devs is just extra paranoia | |
8683 | */ | |
8684 | dev = get_imsm_dev(super, victim); | |
8685 | if (a || !dev || mpb->num_raid_devs == 1) { | |
8686 | dprintf("failed to delete subarray-%d\n", victim); | |
8687 | break; | |
8688 | } | |
8689 | ||
8690 | for (dp = &super->devlist; *dp;) | |
f21e18ca | 8691 | if ((*dp)->index == (unsigned)super->current_vol) { |
33414a01 DW |
8692 | *dp = (*dp)->next; |
8693 | } else { | |
f21e18ca | 8694 | if ((*dp)->index > (unsigned)victim) |
33414a01 DW |
8695 | (*dp)->index--; |
8696 | dp = &(*dp)->next; | |
8697 | } | |
8698 | mpb->num_raid_devs--; | |
8699 | super->updates_pending++; | |
8700 | break; | |
8701 | } | |
aa534678 DW |
8702 | case update_rename_array: { |
8703 | struct imsm_update_rename_array *u = (void *) update->buf; | |
8704 | char name[MAX_RAID_SERIAL_LEN+1]; | |
8705 | int target = u->dev_idx; | |
8706 | struct active_array *a; | |
8707 | struct imsm_dev *dev; | |
8708 | ||
8709 | /* sanity check that we are not affecting the uuid of | |
8710 | * an active array | |
8711 | */ | |
8712 | snprintf(name, MAX_RAID_SERIAL_LEN, "%s", (char *) u->name); | |
8713 | name[MAX_RAID_SERIAL_LEN] = '\0'; | |
8714 | for (a = st->arrays; a; a = a->next) | |
8715 | if (a->info.container_member == target) | |
8716 | break; | |
8717 | dev = get_imsm_dev(super, u->dev_idx); | |
8718 | if (a || !dev || !check_name(super, name, 1)) { | |
8719 | dprintf("failed to rename subarray-%d\n", target); | |
8720 | break; | |
8721 | } | |
8722 | ||
cdbe98cd | 8723 | snprintf((char *) dev->volume, MAX_RAID_SERIAL_LEN, "%s", name); |
aa534678 DW |
8724 | super->updates_pending++; |
8725 | break; | |
8726 | } | |
1a64be56 | 8727 | case update_add_remove_disk: { |
43dad3d6 | 8728 | /* we may be able to repair some arrays if disks are |
095b8088 | 8729 | * being added, check the status of add_remove_disk |
1a64be56 LM |
8730 | * if discs has been added. |
8731 | */ | |
8732 | if (add_remove_disk_update(super)) { | |
43dad3d6 | 8733 | struct active_array *a; |
072b727f DW |
8734 | |
8735 | super->updates_pending++; | |
1a64be56 | 8736 | for (a = st->arrays; a; a = a->next) |
43dad3d6 DW |
8737 | a->check_degraded = 1; |
8738 | } | |
43dad3d6 | 8739 | break; |
e8319a19 | 8740 | } |
1a64be56 | 8741 | default: |
7a862a02 | 8742 | pr_err("error: unsuported process update type:(type: %d)\n", type); |
1a64be56 | 8743 | } |
e8319a19 | 8744 | } |
88758e9d | 8745 | |
bc0b9d34 PC |
8746 | static struct mdinfo *get_spares_for_grow(struct supertype *st); |
8747 | ||
5fe6f031 N |
8748 | static int imsm_prepare_update(struct supertype *st, |
8749 | struct metadata_update *update) | |
8273f55e | 8750 | { |
949c47a0 | 8751 | /** |
4d7b1503 DW |
8752 | * Allocate space to hold new disk entries, raid-device entries or a new |
8753 | * mpb if necessary. The manager synchronously waits for updates to | |
8754 | * complete in the monitor, so new mpb buffers allocated here can be | |
8755 | * integrated by the monitor thread without worrying about live pointers | |
8756 | * in the manager thread. | |
8273f55e | 8757 | */ |
095b8088 | 8758 | enum imsm_update_type type; |
4d7b1503 DW |
8759 | struct intel_super *super = st->sb; |
8760 | struct imsm_super *mpb = super->anchor; | |
8761 | size_t buf_len; | |
8762 | size_t len = 0; | |
949c47a0 | 8763 | |
095b8088 N |
8764 | if (update->len < (int)sizeof(type)) |
8765 | return 0; | |
8766 | ||
8767 | type = *(enum imsm_update_type *) update->buf; | |
8768 | ||
949c47a0 | 8769 | switch (type) { |
0ec5d470 | 8770 | case update_general_migration_checkpoint: |
095b8088 N |
8771 | if (update->len < (int)sizeof(struct imsm_update_general_migration_checkpoint)) |
8772 | return 0; | |
1ade5cc1 | 8773 | dprintf("called for update_general_migration_checkpoint\n"); |
0ec5d470 | 8774 | break; |
abedf5fc KW |
8775 | case update_takeover: { |
8776 | struct imsm_update_takeover *u = (void *)update->buf; | |
095b8088 N |
8777 | if (update->len < (int)sizeof(*u)) |
8778 | return 0; | |
abedf5fc KW |
8779 | if (u->direction == R0_TO_R10) { |
8780 | void **tail = (void **)&update->space_list; | |
8781 | struct imsm_dev *dev = get_imsm_dev(super, u->subarray); | |
238c0a71 | 8782 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
abedf5fc KW |
8783 | int num_members = map->num_members; |
8784 | void *space; | |
8785 | int size, i; | |
abedf5fc KW |
8786 | /* allocate memory for added disks */ |
8787 | for (i = 0; i < num_members; i++) { | |
8788 | size = sizeof(struct dl); | |
503975b9 | 8789 | space = xmalloc(size); |
abedf5fc KW |
8790 | *tail = space; |
8791 | tail = space; | |
8792 | *tail = NULL; | |
8793 | } | |
8794 | /* allocate memory for new device */ | |
8795 | size = sizeof_imsm_dev(super->devlist->dev, 0) + | |
8796 | (num_members * sizeof(__u32)); | |
503975b9 N |
8797 | space = xmalloc(size); |
8798 | *tail = space; | |
8799 | tail = space; | |
8800 | *tail = NULL; | |
8801 | len = disks_to_mpb_size(num_members * 2); | |
abedf5fc KW |
8802 | } |
8803 | ||
8804 | break; | |
8805 | } | |
78b10e66 | 8806 | case update_reshape_container_disks: { |
d195167d AK |
8807 | /* Every raid device in the container is about to |
8808 | * gain some more devices, and we will enter a | |
8809 | * reconfiguration. | |
8810 | * So each 'imsm_map' will be bigger, and the imsm_vol | |
8811 | * will now hold 2 of them. | |
8812 | * Thus we need new 'struct imsm_dev' allocations sized | |
8813 | * as sizeof_imsm_dev but with more devices in both maps. | |
8814 | */ | |
8815 | struct imsm_update_reshape *u = (void *)update->buf; | |
8816 | struct intel_dev *dl; | |
8817 | void **space_tail = (void**)&update->space_list; | |
8818 | ||
095b8088 N |
8819 | if (update->len < (int)sizeof(*u)) |
8820 | return 0; | |
8821 | ||
1ade5cc1 | 8822 | dprintf("for update_reshape\n"); |
d195167d AK |
8823 | |
8824 | for (dl = super->devlist; dl; dl = dl->next) { | |
8825 | int size = sizeof_imsm_dev(dl->dev, 1); | |
8826 | void *s; | |
d677e0b8 AK |
8827 | if (u->new_raid_disks > u->old_raid_disks) |
8828 | size += sizeof(__u32)*2* | |
8829 | (u->new_raid_disks - u->old_raid_disks); | |
503975b9 | 8830 | s = xmalloc(size); |
d195167d AK |
8831 | *space_tail = s; |
8832 | space_tail = s; | |
8833 | *space_tail = NULL; | |
8834 | } | |
8835 | ||
8836 | len = disks_to_mpb_size(u->new_raid_disks); | |
8837 | dprintf("New anchor length is %llu\n", (unsigned long long)len); | |
78b10e66 N |
8838 | break; |
8839 | } | |
48c5303a | 8840 | case update_reshape_migration: { |
bc0b9d34 PC |
8841 | /* for migration level 0->5 we need to add disks |
8842 | * so the same as for container operation we will copy | |
8843 | * device to the bigger location. | |
8844 | * in memory prepared device and new disk area are prepared | |
8845 | * for usage in process update | |
8846 | */ | |
8847 | struct imsm_update_reshape_migration *u = (void *)update->buf; | |
8848 | struct intel_dev *id; | |
8849 | void **space_tail = (void **)&update->space_list; | |
8850 | int size; | |
8851 | void *s; | |
8852 | int current_level = -1; | |
8853 | ||
095b8088 N |
8854 | if (update->len < (int)sizeof(*u)) |
8855 | return 0; | |
8856 | ||
1ade5cc1 | 8857 | dprintf("for update_reshape\n"); |
bc0b9d34 PC |
8858 | |
8859 | /* add space for bigger array in update | |
8860 | */ | |
8861 | for (id = super->devlist; id; id = id->next) { | |
8862 | if (id->index == (unsigned)u->subdev) { | |
8863 | size = sizeof_imsm_dev(id->dev, 1); | |
8864 | if (u->new_raid_disks > u->old_raid_disks) | |
8865 | size += sizeof(__u32)*2* | |
8866 | (u->new_raid_disks - u->old_raid_disks); | |
503975b9 | 8867 | s = xmalloc(size); |
bc0b9d34 PC |
8868 | *space_tail = s; |
8869 | space_tail = s; | |
8870 | *space_tail = NULL; | |
8871 | break; | |
8872 | } | |
8873 | } | |
8874 | if (update->space_list == NULL) | |
8875 | break; | |
8876 | ||
8877 | /* add space for disk in update | |
8878 | */ | |
8879 | size = sizeof(struct dl); | |
503975b9 | 8880 | s = xmalloc(size); |
bc0b9d34 PC |
8881 | *space_tail = s; |
8882 | space_tail = s; | |
8883 | *space_tail = NULL; | |
8884 | ||
8885 | /* add spare device to update | |
8886 | */ | |
8887 | for (id = super->devlist ; id; id = id->next) | |
8888 | if (id->index == (unsigned)u->subdev) { | |
8889 | struct imsm_dev *dev; | |
8890 | struct imsm_map *map; | |
8891 | ||
8892 | dev = get_imsm_dev(super, u->subdev); | |
238c0a71 | 8893 | map = get_imsm_map(dev, MAP_0); |
bc0b9d34 PC |
8894 | current_level = map->raid_level; |
8895 | break; | |
8896 | } | |
8897 | if ((u->new_level == 5) && (u->new_level != current_level)) { | |
8898 | struct mdinfo *spares; | |
8899 | ||
8900 | spares = get_spares_for_grow(st); | |
8901 | if (spares) { | |
8902 | struct dl *dl; | |
8903 | struct mdinfo *dev; | |
8904 | ||
8905 | dev = spares->devs; | |
8906 | if (dev) { | |
8907 | u->new_disks[0] = | |
8908 | makedev(dev->disk.major, | |
8909 | dev->disk.minor); | |
8910 | dl = get_disk_super(super, | |
8911 | dev->disk.major, | |
8912 | dev->disk.minor); | |
8913 | dl->index = u->old_raid_disks; | |
8914 | dev = dev->next; | |
8915 | } | |
8916 | sysfs_free(spares); | |
8917 | } | |
8918 | } | |
8919 | len = disks_to_mpb_size(u->new_raid_disks); | |
8920 | dprintf("New anchor length is %llu\n", (unsigned long long)len); | |
48c5303a PC |
8921 | break; |
8922 | } | |
f3871fdc | 8923 | case update_size_change: { |
095b8088 N |
8924 | if (update->len < (int)sizeof(struct imsm_update_size_change)) |
8925 | return 0; | |
8926 | break; | |
8927 | } | |
8928 | case update_activate_spare: { | |
8929 | if (update->len < (int)sizeof(struct imsm_update_activate_spare)) | |
8930 | return 0; | |
f3871fdc AK |
8931 | break; |
8932 | } | |
949c47a0 DW |
8933 | case update_create_array: { |
8934 | struct imsm_update_create_array *u = (void *) update->buf; | |
ba2de7ba | 8935 | struct intel_dev *dv; |
54c2c1ea | 8936 | struct imsm_dev *dev = &u->dev; |
238c0a71 | 8937 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
54c2c1ea DW |
8938 | struct dl *dl; |
8939 | struct disk_info *inf; | |
8940 | int i; | |
8941 | int activate = 0; | |
949c47a0 | 8942 | |
095b8088 N |
8943 | if (update->len < (int)sizeof(*u)) |
8944 | return 0; | |
8945 | ||
54c2c1ea DW |
8946 | inf = get_disk_info(u); |
8947 | len = sizeof_imsm_dev(dev, 1); | |
ba2de7ba | 8948 | /* allocate a new super->devlist entry */ |
503975b9 N |
8949 | dv = xmalloc(sizeof(*dv)); |
8950 | dv->dev = xmalloc(len); | |
8951 | update->space = dv; | |
949c47a0 | 8952 | |
54c2c1ea DW |
8953 | /* count how many spares will be converted to members */ |
8954 | for (i = 0; i < map->num_members; i++) { | |
8955 | dl = serial_to_dl(inf[i].serial, super); | |
8956 | if (!dl) { | |
8957 | /* hmm maybe it failed?, nothing we can do about | |
8958 | * it here | |
8959 | */ | |
8960 | continue; | |
8961 | } | |
8962 | if (count_memberships(dl, super) == 0) | |
8963 | activate++; | |
8964 | } | |
8965 | len += activate * sizeof(struct imsm_disk); | |
949c47a0 | 8966 | break; |
095b8088 N |
8967 | } |
8968 | case update_kill_array: { | |
8969 | if (update->len < (int)sizeof(struct imsm_update_kill_array)) | |
8970 | return 0; | |
949c47a0 DW |
8971 | break; |
8972 | } | |
095b8088 N |
8973 | case update_rename_array: { |
8974 | if (update->len < (int)sizeof(struct imsm_update_rename_array)) | |
8975 | return 0; | |
8976 | break; | |
8977 | } | |
8978 | case update_add_remove_disk: | |
8979 | /* no update->len needed */ | |
8980 | break; | |
8981 | default: | |
8982 | return 0; | |
949c47a0 | 8983 | } |
8273f55e | 8984 | |
4d7b1503 DW |
8985 | /* check if we need a larger metadata buffer */ |
8986 | if (super->next_buf) | |
8987 | buf_len = super->next_len; | |
8988 | else | |
8989 | buf_len = super->len; | |
8990 | ||
8991 | if (__le32_to_cpu(mpb->mpb_size) + len > buf_len) { | |
8992 | /* ok we need a larger buf than what is currently allocated | |
8993 | * if this allocation fails process_update will notice that | |
8994 | * ->next_len is set and ->next_buf is NULL | |
8995 | */ | |
8996 | buf_len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + len, 512); | |
8997 | if (super->next_buf) | |
8998 | free(super->next_buf); | |
8999 | ||
9000 | super->next_len = buf_len; | |
1f45a8ad DW |
9001 | if (posix_memalign(&super->next_buf, 512, buf_len) == 0) |
9002 | memset(super->next_buf, 0, buf_len); | |
9003 | else | |
4d7b1503 DW |
9004 | super->next_buf = NULL; |
9005 | } | |
5fe6f031 | 9006 | return 1; |
8273f55e DW |
9007 | } |
9008 | ||
ae6aad82 | 9009 | /* must be called while manager is quiesced */ |
f21e18ca | 9010 | static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index) |
ae6aad82 DW |
9011 | { |
9012 | struct imsm_super *mpb = super->anchor; | |
ae6aad82 DW |
9013 | struct dl *iter; |
9014 | struct imsm_dev *dev; | |
9015 | struct imsm_map *map; | |
24565c9a DW |
9016 | int i, j, num_members; |
9017 | __u32 ord; | |
ae6aad82 | 9018 | |
1ade5cc1 | 9019 | dprintf("deleting device[%d] from imsm_super\n", index); |
ae6aad82 DW |
9020 | |
9021 | /* shift all indexes down one */ | |
9022 | for (iter = super->disks; iter; iter = iter->next) | |
f21e18ca | 9023 | if (iter->index > (int)index) |
ae6aad82 | 9024 | iter->index--; |
47ee5a45 | 9025 | for (iter = super->missing; iter; iter = iter->next) |
f21e18ca | 9026 | if (iter->index > (int)index) |
47ee5a45 | 9027 | iter->index--; |
ae6aad82 DW |
9028 | |
9029 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
9030 | dev = get_imsm_dev(super, i); | |
238c0a71 | 9031 | map = get_imsm_map(dev, MAP_0); |
24565c9a DW |
9032 | num_members = map->num_members; |
9033 | for (j = 0; j < num_members; j++) { | |
9034 | /* update ord entries being careful not to propagate | |
9035 | * ord-flags to the first map | |
9036 | */ | |
238c0a71 | 9037 | ord = get_imsm_ord_tbl_ent(dev, j, MAP_X); |
ae6aad82 | 9038 | |
24565c9a DW |
9039 | if (ord_to_idx(ord) <= index) |
9040 | continue; | |
ae6aad82 | 9041 | |
238c0a71 | 9042 | map = get_imsm_map(dev, MAP_0); |
24565c9a | 9043 | set_imsm_ord_tbl_ent(map, j, ord_to_idx(ord - 1)); |
238c0a71 | 9044 | map = get_imsm_map(dev, MAP_1); |
24565c9a DW |
9045 | if (map) |
9046 | set_imsm_ord_tbl_ent(map, j, ord - 1); | |
ae6aad82 DW |
9047 | } |
9048 | } | |
9049 | ||
9050 | mpb->num_disks--; | |
9051 | super->updates_pending++; | |
24565c9a DW |
9052 | if (*dlp) { |
9053 | struct dl *dl = *dlp; | |
9054 | ||
9055 | *dlp = (*dlp)->next; | |
9056 | __free_imsm_disk(dl); | |
9057 | } | |
ae6aad82 | 9058 | } |
9e2d750d | 9059 | #endif /* MDASSEMBLE */ |
9a717282 AK |
9060 | |
9061 | static void close_targets(int *targets, int new_disks) | |
9062 | { | |
9063 | int i; | |
9064 | ||
9065 | if (!targets) | |
9066 | return; | |
9067 | ||
9068 | for (i = 0; i < new_disks; i++) { | |
9069 | if (targets[i] >= 0) { | |
9070 | close(targets[i]); | |
9071 | targets[i] = -1; | |
9072 | } | |
9073 | } | |
9074 | } | |
9075 | ||
9076 | static int imsm_get_allowed_degradation(int level, int raid_disks, | |
9077 | struct intel_super *super, | |
9078 | struct imsm_dev *dev) | |
9079 | { | |
9080 | switch (level) { | |
bf5cf7c7 | 9081 | case 1: |
9a717282 AK |
9082 | case 10:{ |
9083 | int ret_val = 0; | |
9084 | struct imsm_map *map; | |
9085 | int i; | |
9086 | ||
9087 | ret_val = raid_disks/2; | |
9088 | /* check map if all disks pairs not failed | |
9089 | * in both maps | |
9090 | */ | |
238c0a71 | 9091 | map = get_imsm_map(dev, MAP_0); |
9a717282 AK |
9092 | for (i = 0; i < ret_val; i++) { |
9093 | int degradation = 0; | |
9094 | if (get_imsm_disk(super, i) == NULL) | |
9095 | degradation++; | |
9096 | if (get_imsm_disk(super, i + 1) == NULL) | |
9097 | degradation++; | |
9098 | if (degradation == 2) | |
9099 | return 0; | |
9100 | } | |
238c0a71 | 9101 | map = get_imsm_map(dev, MAP_1); |
9a717282 AK |
9102 | /* if there is no second map |
9103 | * result can be returned | |
9104 | */ | |
9105 | if (map == NULL) | |
9106 | return ret_val; | |
9107 | /* check degradation in second map | |
9108 | */ | |
9109 | for (i = 0; i < ret_val; i++) { | |
9110 | int degradation = 0; | |
9111 | if (get_imsm_disk(super, i) == NULL) | |
9112 | degradation++; | |
9113 | if (get_imsm_disk(super, i + 1) == NULL) | |
9114 | degradation++; | |
9115 | if (degradation == 2) | |
9116 | return 0; | |
9117 | } | |
9118 | return ret_val; | |
9119 | } | |
9120 | case 5: | |
9121 | return 1; | |
9122 | case 6: | |
9123 | return 2; | |
9124 | default: | |
9125 | return 0; | |
9126 | } | |
9127 | } | |
9128 | ||
687629c2 AK |
9129 | /******************************************************************************* |
9130 | * Function: open_backup_targets | |
9131 | * Description: Function opens file descriptors for all devices given in | |
9132 | * info->devs | |
9133 | * Parameters: | |
9134 | * info : general array info | |
9135 | * raid_disks : number of disks | |
9136 | * raid_fds : table of device's file descriptors | |
9a717282 AK |
9137 | * super : intel super for raid10 degradation check |
9138 | * dev : intel device for raid10 degradation check | |
687629c2 AK |
9139 | * Returns: |
9140 | * 0 : success | |
9141 | * -1 : fail | |
9142 | ******************************************************************************/ | |
9a717282 AK |
9143 | int open_backup_targets(struct mdinfo *info, int raid_disks, int *raid_fds, |
9144 | struct intel_super *super, struct imsm_dev *dev) | |
687629c2 AK |
9145 | { |
9146 | struct mdinfo *sd; | |
f627f5ad | 9147 | int i; |
9a717282 | 9148 | int opened = 0; |
f627f5ad AK |
9149 | |
9150 | for (i = 0; i < raid_disks; i++) | |
9151 | raid_fds[i] = -1; | |
687629c2 AK |
9152 | |
9153 | for (sd = info->devs ; sd ; sd = sd->next) { | |
9154 | char *dn; | |
9155 | ||
9156 | if (sd->disk.state & (1<<MD_DISK_FAULTY)) { | |
9157 | dprintf("disk is faulty!!\n"); | |
9158 | continue; | |
9159 | } | |
9160 | ||
9161 | if ((sd->disk.raid_disk >= raid_disks) || | |
9162 | (sd->disk.raid_disk < 0)) | |
9163 | continue; | |
9164 | ||
9165 | dn = map_dev(sd->disk.major, | |
9166 | sd->disk.minor, 1); | |
9167 | raid_fds[sd->disk.raid_disk] = dev_open(dn, O_RDWR); | |
9168 | if (raid_fds[sd->disk.raid_disk] < 0) { | |
e12b3daa | 9169 | pr_err("cannot open component\n"); |
9a717282 | 9170 | continue; |
687629c2 | 9171 | } |
9a717282 AK |
9172 | opened++; |
9173 | } | |
9174 | /* check if maximum array degradation level is not exceeded | |
9175 | */ | |
9176 | if ((raid_disks - opened) > | |
9177 | imsm_get_allowed_degradation(info->new_level, | |
9178 | raid_disks, | |
9179 | super, dev)) { | |
e12b3daa | 9180 | pr_err("Not enough disks can be opened.\n"); |
9a717282 AK |
9181 | close_targets(raid_fds, raid_disks); |
9182 | return -2; | |
687629c2 AK |
9183 | } |
9184 | return 0; | |
9185 | } | |
9186 | ||
d31ad643 PB |
9187 | /******************************************************************************* |
9188 | * Function: validate_container_imsm | |
9189 | * Description: This routine validates container after assemble, | |
9190 | * eg. if devices in container are under the same controller. | |
9191 | * | |
9192 | * Parameters: | |
9193 | * info : linked list with info about devices used in array | |
9194 | * Returns: | |
9195 | * 1 : HBA mismatch | |
9196 | * 0 : Success | |
9197 | ******************************************************************************/ | |
9198 | int validate_container_imsm(struct mdinfo *info) | |
9199 | { | |
6b781d33 AP |
9200 | if (check_env("IMSM_NO_PLATFORM")) |
9201 | return 0; | |
d31ad643 | 9202 | |
6b781d33 AP |
9203 | struct sys_dev *idev; |
9204 | struct sys_dev *hba = NULL; | |
9205 | struct sys_dev *intel_devices = find_intel_devices(); | |
9206 | char *dev_path = devt_to_devpath(makedev(info->disk.major, | |
9207 | info->disk.minor)); | |
9208 | ||
9209 | for (idev = intel_devices; idev; idev = idev->next) { | |
9210 | if (dev_path && strstr(dev_path, idev->path)) { | |
9211 | hba = idev; | |
9212 | break; | |
d31ad643 | 9213 | } |
6b781d33 AP |
9214 | } |
9215 | if (dev_path) | |
d31ad643 PB |
9216 | free(dev_path); |
9217 | ||
6b781d33 AP |
9218 | if (!hba) { |
9219 | pr_err("WARNING - Cannot detect HBA for device %s!\n", | |
9220 | devid2kname(makedev(info->disk.major, info->disk.minor))); | |
9221 | return 1; | |
9222 | } | |
9223 | ||
9224 | const struct imsm_orom *orom = get_orom_by_device_id(hba->dev_id); | |
9225 | struct mdinfo *dev; | |
9226 | ||
9227 | for (dev = info->next; dev; dev = dev->next) { | |
9228 | dev_path = devt_to_devpath(makedev(dev->disk.major, dev->disk.minor)); | |
9229 | ||
9230 | struct sys_dev *hba2 = NULL; | |
9231 | for (idev = intel_devices; idev; idev = idev->next) { | |
9232 | if (dev_path && strstr(dev_path, idev->path)) { | |
9233 | hba2 = idev; | |
9234 | break; | |
d31ad643 PB |
9235 | } |
9236 | } | |
6b781d33 AP |
9237 | if (dev_path) |
9238 | free(dev_path); | |
9239 | ||
9240 | const struct imsm_orom *orom2 = hba2 == NULL ? NULL : | |
9241 | get_orom_by_device_id(hba2->dev_id); | |
9242 | ||
9243 | if (hba2 && hba->type != hba2->type) { | |
9244 | pr_err("WARNING - HBAs of devices do not match %s != %s\n", | |
9245 | get_sys_dev_type(hba->type), get_sys_dev_type(hba2->type)); | |
9246 | return 1; | |
9247 | } | |
9248 | ||
60f0f54d | 9249 | if ((orom != orom2) || ((hba->type == SYS_DEV_VMD) && (hba != hba2))) { |
6b781d33 AP |
9250 | pr_err("WARNING - IMSM container assembled with disks under different HBAs!\n" |
9251 | " This operation is not supported and can lead to data loss.\n"); | |
9252 | return 1; | |
9253 | } | |
9254 | ||
9255 | if (!orom) { | |
9256 | pr_err("WARNING - IMSM container assembled with disks under HBAs without IMSM platform support!\n" | |
9257 | " This operation is not supported and can lead to data loss.\n"); | |
9258 | return 1; | |
9259 | } | |
d31ad643 | 9260 | } |
6b781d33 | 9261 | |
d31ad643 PB |
9262 | return 0; |
9263 | } | |
9e2d750d | 9264 | #ifndef MDASSEMBLE |
687629c2 AK |
9265 | /******************************************************************************* |
9266 | * Function: init_migr_record_imsm | |
9267 | * Description: Function inits imsm migration record | |
9268 | * Parameters: | |
9269 | * super : imsm internal array info | |
9270 | * dev : device under migration | |
9271 | * info : general array info to find the smallest device | |
9272 | * Returns: | |
9273 | * none | |
9274 | ******************************************************************************/ | |
9275 | void init_migr_record_imsm(struct supertype *st, struct imsm_dev *dev, | |
9276 | struct mdinfo *info) | |
9277 | { | |
9278 | struct intel_super *super = st->sb; | |
9279 | struct migr_record *migr_rec = super->migr_rec; | |
9280 | int new_data_disks; | |
9281 | unsigned long long dsize, dev_sectors; | |
9282 | long long unsigned min_dev_sectors = -1LLU; | |
9283 | struct mdinfo *sd; | |
9284 | char nm[30]; | |
9285 | int fd; | |
238c0a71 AK |
9286 | struct imsm_map *map_dest = get_imsm_map(dev, MAP_0); |
9287 | struct imsm_map *map_src = get_imsm_map(dev, MAP_1); | |
687629c2 | 9288 | unsigned long long num_migr_units; |
3ef4403c | 9289 | unsigned long long array_blocks; |
687629c2 AK |
9290 | |
9291 | memset(migr_rec, 0, sizeof(struct migr_record)); | |
9292 | migr_rec->family_num = __cpu_to_le32(super->anchor->family_num); | |
9293 | ||
9294 | /* only ascending reshape supported now */ | |
9295 | migr_rec->ascending_migr = __cpu_to_le32(1); | |
9296 | ||
9297 | migr_rec->dest_depth_per_unit = GEN_MIGR_AREA_SIZE / | |
9298 | max(map_dest->blocks_per_strip, map_src->blocks_per_strip); | |
e1742195 AK |
9299 | migr_rec->dest_depth_per_unit *= |
9300 | max(map_dest->blocks_per_strip, map_src->blocks_per_strip); | |
238c0a71 | 9301 | new_data_disks = imsm_num_data_members(dev, MAP_0); |
687629c2 AK |
9302 | migr_rec->blocks_per_unit = |
9303 | __cpu_to_le32(migr_rec->dest_depth_per_unit * new_data_disks); | |
9304 | migr_rec->dest_depth_per_unit = | |
9305 | __cpu_to_le32(migr_rec->dest_depth_per_unit); | |
3ef4403c | 9306 | array_blocks = info->component_size * new_data_disks; |
687629c2 AK |
9307 | num_migr_units = |
9308 | array_blocks / __le32_to_cpu(migr_rec->blocks_per_unit); | |
9309 | ||
9310 | if (array_blocks % __le32_to_cpu(migr_rec->blocks_per_unit)) | |
9311 | num_migr_units++; | |
9312 | migr_rec->num_migr_units = __cpu_to_le32(num_migr_units); | |
9313 | ||
9314 | migr_rec->post_migr_vol_cap = dev->size_low; | |
9315 | migr_rec->post_migr_vol_cap_hi = dev->size_high; | |
9316 | ||
687629c2 AK |
9317 | /* Find the smallest dev */ |
9318 | for (sd = info->devs ; sd ; sd = sd->next) { | |
9319 | sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor); | |
9320 | fd = dev_open(nm, O_RDONLY); | |
9321 | if (fd < 0) | |
9322 | continue; | |
9323 | get_dev_size(fd, NULL, &dsize); | |
9324 | dev_sectors = dsize / 512; | |
9325 | if (dev_sectors < min_dev_sectors) | |
9326 | min_dev_sectors = dev_sectors; | |
9327 | close(fd); | |
9328 | } | |
9329 | migr_rec->ckpt_area_pba = __cpu_to_le32(min_dev_sectors - | |
9330 | RAID_DISK_RESERVED_BLOCKS_IMSM_HI); | |
9331 | ||
9332 | write_imsm_migr_rec(st); | |
9333 | ||
9334 | return; | |
9335 | } | |
9336 | ||
9337 | /******************************************************************************* | |
9338 | * Function: save_backup_imsm | |
9339 | * Description: Function saves critical data stripes to Migration Copy Area | |
9340 | * and updates the current migration unit status. | |
9341 | * Use restore_stripes() to form a destination stripe, | |
9342 | * and to write it to the Copy Area. | |
9343 | * Parameters: | |
9344 | * st : supertype information | |
aea93171 | 9345 | * dev : imsm device that backup is saved for |
687629c2 AK |
9346 | * info : general array info |
9347 | * buf : input buffer | |
687629c2 AK |
9348 | * length : length of data to backup (blocks_per_unit) |
9349 | * Returns: | |
9350 | * 0 : success | |
9351 | *, -1 : fail | |
9352 | ******************************************************************************/ | |
9353 | int save_backup_imsm(struct supertype *st, | |
9354 | struct imsm_dev *dev, | |
9355 | struct mdinfo *info, | |
9356 | void *buf, | |
687629c2 AK |
9357 | int length) |
9358 | { | |
9359 | int rv = -1; | |
9360 | struct intel_super *super = st->sb; | |
9361 | unsigned long long *target_offsets = NULL; | |
9362 | int *targets = NULL; | |
9363 | int i; | |
238c0a71 | 9364 | struct imsm_map *map_dest = get_imsm_map(dev, MAP_0); |
687629c2 | 9365 | int new_disks = map_dest->num_members; |
ab724b98 AK |
9366 | int dest_layout = 0; |
9367 | int dest_chunk; | |
d1877f69 | 9368 | unsigned long long start; |
238c0a71 | 9369 | int data_disks = imsm_num_data_members(dev, MAP_0); |
687629c2 | 9370 | |
503975b9 | 9371 | targets = xmalloc(new_disks * sizeof(int)); |
687629c2 | 9372 | |
7e45b550 AK |
9373 | for (i = 0; i < new_disks; i++) |
9374 | targets[i] = -1; | |
9375 | ||
503975b9 | 9376 | target_offsets = xcalloc(new_disks, sizeof(unsigned long long)); |
687629c2 | 9377 | |
d1877f69 | 9378 | start = info->reshape_progress * 512; |
687629c2 | 9379 | for (i = 0; i < new_disks; i++) { |
687629c2 AK |
9380 | target_offsets[i] = (unsigned long long) |
9381 | __le32_to_cpu(super->migr_rec->ckpt_area_pba) * 512; | |
d1877f69 AK |
9382 | /* move back copy area adderss, it will be moved forward |
9383 | * in restore_stripes() using start input variable | |
9384 | */ | |
9385 | target_offsets[i] -= start/data_disks; | |
687629c2 AK |
9386 | } |
9387 | ||
9a717282 AK |
9388 | if (open_backup_targets(info, new_disks, targets, |
9389 | super, dev)) | |
687629c2 AK |
9390 | goto abort; |
9391 | ||
68eb8bc6 | 9392 | dest_layout = imsm_level_to_layout(map_dest->raid_level); |
ab724b98 AK |
9393 | dest_chunk = __le16_to_cpu(map_dest->blocks_per_strip) * 512; |
9394 | ||
687629c2 AK |
9395 | if (restore_stripes(targets, /* list of dest devices */ |
9396 | target_offsets, /* migration record offsets */ | |
9397 | new_disks, | |
ab724b98 AK |
9398 | dest_chunk, |
9399 | map_dest->raid_level, | |
9400 | dest_layout, | |
9401 | -1, /* source backup file descriptor */ | |
9402 | 0, /* input buf offset | |
9403 | * always 0 buf is already offseted */ | |
d1877f69 | 9404 | start, |
687629c2 AK |
9405 | length, |
9406 | buf) != 0) { | |
e7b84f9d | 9407 | pr_err("Error restoring stripes\n"); |
687629c2 AK |
9408 | goto abort; |
9409 | } | |
9410 | ||
9411 | rv = 0; | |
9412 | ||
9413 | abort: | |
9414 | if (targets) { | |
9a717282 | 9415 | close_targets(targets, new_disks); |
687629c2 AK |
9416 | free(targets); |
9417 | } | |
9418 | free(target_offsets); | |
9419 | ||
9420 | return rv; | |
9421 | } | |
9422 | ||
9423 | /******************************************************************************* | |
9424 | * Function: save_checkpoint_imsm | |
9425 | * Description: Function called for current unit status update | |
9426 | * in the migration record. It writes it to disk. | |
9427 | * Parameters: | |
9428 | * super : imsm internal array info | |
9429 | * info : general array info | |
9430 | * Returns: | |
9431 | * 0: success | |
9432 | * 1: failure | |
0228d92c AK |
9433 | * 2: failure, means no valid migration record |
9434 | * / no general migration in progress / | |
687629c2 AK |
9435 | ******************************************************************************/ |
9436 | int save_checkpoint_imsm(struct supertype *st, struct mdinfo *info, int state) | |
9437 | { | |
9438 | struct intel_super *super = st->sb; | |
f8b72ef5 AK |
9439 | unsigned long long blocks_per_unit; |
9440 | unsigned long long curr_migr_unit; | |
9441 | ||
2e062e82 | 9442 | if (load_imsm_migr_rec(super, info) != 0) { |
7a862a02 | 9443 | dprintf("imsm: ERROR: Cannot read migration record for checkpoint save.\n"); |
2e062e82 AK |
9444 | return 1; |
9445 | } | |
9446 | ||
f8b72ef5 AK |
9447 | blocks_per_unit = __le32_to_cpu(super->migr_rec->blocks_per_unit); |
9448 | if (blocks_per_unit == 0) { | |
0228d92c AK |
9449 | dprintf("imsm: no migration in progress.\n"); |
9450 | return 2; | |
687629c2 | 9451 | } |
f8b72ef5 AK |
9452 | curr_migr_unit = info->reshape_progress / blocks_per_unit; |
9453 | /* check if array is alligned to copy area | |
9454 | * if it is not alligned, add one to current migration unit value | |
9455 | * this can happend on array reshape finish only | |
9456 | */ | |
9457 | if (info->reshape_progress % blocks_per_unit) | |
9458 | curr_migr_unit++; | |
687629c2 AK |
9459 | |
9460 | super->migr_rec->curr_migr_unit = | |
f8b72ef5 | 9461 | __cpu_to_le32(curr_migr_unit); |
687629c2 AK |
9462 | super->migr_rec->rec_status = __cpu_to_le32(state); |
9463 | super->migr_rec->dest_1st_member_lba = | |
f8b72ef5 AK |
9464 | __cpu_to_le32(curr_migr_unit * |
9465 | __le32_to_cpu(super->migr_rec->dest_depth_per_unit)); | |
687629c2 | 9466 | if (write_imsm_migr_rec(st) < 0) { |
7a862a02 | 9467 | dprintf("imsm: Cannot write migration record outside backup area\n"); |
687629c2 AK |
9468 | return 1; |
9469 | } | |
9470 | ||
9471 | return 0; | |
9472 | } | |
9473 | ||
276d77db AK |
9474 | /******************************************************************************* |
9475 | * Function: recover_backup_imsm | |
9476 | * Description: Function recovers critical data from the Migration Copy Area | |
9477 | * while assembling an array. | |
9478 | * Parameters: | |
9479 | * super : imsm internal array info | |
9480 | * info : general array info | |
9481 | * Returns: | |
9482 | * 0 : success (or there is no data to recover) | |
9483 | * 1 : fail | |
9484 | ******************************************************************************/ | |
9485 | int recover_backup_imsm(struct supertype *st, struct mdinfo *info) | |
9486 | { | |
9487 | struct intel_super *super = st->sb; | |
9488 | struct migr_record *migr_rec = super->migr_rec; | |
9489 | struct imsm_map *map_dest = NULL; | |
9490 | struct intel_dev *id = NULL; | |
9491 | unsigned long long read_offset; | |
9492 | unsigned long long write_offset; | |
9493 | unsigned unit_len; | |
9494 | int *targets = NULL; | |
9495 | int new_disks, i, err; | |
9496 | char *buf = NULL; | |
9497 | int retval = 1; | |
9498 | unsigned long curr_migr_unit = __le32_to_cpu(migr_rec->curr_migr_unit); | |
9499 | unsigned long num_migr_units = __le32_to_cpu(migr_rec->num_migr_units); | |
276d77db | 9500 | char buffer[20]; |
6c3560c0 | 9501 | int skipped_disks = 0; |
276d77db AK |
9502 | |
9503 | err = sysfs_get_str(info, NULL, "array_state", (char *)buffer, 20); | |
9504 | if (err < 1) | |
9505 | return 1; | |
9506 | ||
9507 | /* recover data only during assemblation */ | |
9508 | if (strncmp(buffer, "inactive", 8) != 0) | |
9509 | return 0; | |
9510 | /* no data to recover */ | |
9511 | if (__le32_to_cpu(migr_rec->rec_status) == UNIT_SRC_NORMAL) | |
9512 | return 0; | |
9513 | if (curr_migr_unit >= num_migr_units) | |
9514 | return 1; | |
9515 | ||
9516 | /* find device during reshape */ | |
9517 | for (id = super->devlist; id; id = id->next) | |
9518 | if (is_gen_migration(id->dev)) | |
9519 | break; | |
9520 | if (id == NULL) | |
9521 | return 1; | |
9522 | ||
238c0a71 | 9523 | map_dest = get_imsm_map(id->dev, MAP_0); |
276d77db AK |
9524 | new_disks = map_dest->num_members; |
9525 | ||
9526 | read_offset = (unsigned long long) | |
9527 | __le32_to_cpu(migr_rec->ckpt_area_pba) * 512; | |
9528 | ||
9529 | write_offset = ((unsigned long long) | |
9530 | __le32_to_cpu(migr_rec->dest_1st_member_lba) + | |
5551b113 | 9531 | pba_of_lba0(map_dest)) * 512; |
276d77db AK |
9532 | |
9533 | unit_len = __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512; | |
9534 | if (posix_memalign((void **)&buf, 512, unit_len) != 0) | |
9535 | goto abort; | |
503975b9 | 9536 | targets = xcalloc(new_disks, sizeof(int)); |
276d77db | 9537 | |
9a717282 | 9538 | if (open_backup_targets(info, new_disks, targets, super, id->dev)) { |
e7b84f9d | 9539 | pr_err("Cannot open some devices belonging to array.\n"); |
f627f5ad AK |
9540 | goto abort; |
9541 | } | |
276d77db AK |
9542 | |
9543 | for (i = 0; i < new_disks; i++) { | |
6c3560c0 AK |
9544 | if (targets[i] < 0) { |
9545 | skipped_disks++; | |
9546 | continue; | |
9547 | } | |
276d77db | 9548 | if (lseek64(targets[i], read_offset, SEEK_SET) < 0) { |
e7b84f9d N |
9549 | pr_err("Cannot seek to block: %s\n", |
9550 | strerror(errno)); | |
137debce AK |
9551 | skipped_disks++; |
9552 | continue; | |
276d77db | 9553 | } |
9ec11d1a | 9554 | if ((unsigned)read(targets[i], buf, unit_len) != unit_len) { |
e7b84f9d N |
9555 | pr_err("Cannot read copy area block: %s\n", |
9556 | strerror(errno)); | |
137debce AK |
9557 | skipped_disks++; |
9558 | continue; | |
276d77db AK |
9559 | } |
9560 | if (lseek64(targets[i], write_offset, SEEK_SET) < 0) { | |
e7b84f9d N |
9561 | pr_err("Cannot seek to block: %s\n", |
9562 | strerror(errno)); | |
137debce AK |
9563 | skipped_disks++; |
9564 | continue; | |
276d77db | 9565 | } |
9ec11d1a | 9566 | if ((unsigned)write(targets[i], buf, unit_len) != unit_len) { |
e7b84f9d N |
9567 | pr_err("Cannot restore block: %s\n", |
9568 | strerror(errno)); | |
137debce AK |
9569 | skipped_disks++; |
9570 | continue; | |
276d77db AK |
9571 | } |
9572 | } | |
9573 | ||
137debce AK |
9574 | if (skipped_disks > imsm_get_allowed_degradation(info->new_level, |
9575 | new_disks, | |
9576 | super, | |
9577 | id->dev)) { | |
7a862a02 | 9578 | pr_err("Cannot restore data from backup. Too many failed disks\n"); |
6c3560c0 AK |
9579 | goto abort; |
9580 | } | |
9581 | ||
befb629b AK |
9582 | if (save_checkpoint_imsm(st, info, UNIT_SRC_NORMAL)) { |
9583 | /* ignore error == 2, this can mean end of reshape here | |
9584 | */ | |
7a862a02 | 9585 | dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL) during restart\n"); |
befb629b | 9586 | } else |
276d77db | 9587 | retval = 0; |
276d77db AK |
9588 | |
9589 | abort: | |
9590 | if (targets) { | |
9591 | for (i = 0; i < new_disks; i++) | |
9592 | if (targets[i]) | |
9593 | close(targets[i]); | |
9594 | free(targets); | |
9595 | } | |
9596 | free(buf); | |
9597 | return retval; | |
9598 | } | |
9599 | ||
2cda7640 ML |
9600 | static char disk_by_path[] = "/dev/disk/by-path/"; |
9601 | ||
9602 | static const char *imsm_get_disk_controller_domain(const char *path) | |
9603 | { | |
2cda7640 | 9604 | char disk_path[PATH_MAX]; |
96234762 LM |
9605 | char *drv=NULL; |
9606 | struct stat st; | |
2cda7640 | 9607 | |
6d8d290a | 9608 | strcpy(disk_path, disk_by_path); |
96234762 LM |
9609 | strncat(disk_path, path, PATH_MAX - strlen(disk_path) - 1); |
9610 | if (stat(disk_path, &st) == 0) { | |
9611 | struct sys_dev* hba; | |
9612 | char *path=NULL; | |
9613 | ||
9614 | path = devt_to_devpath(st.st_rdev); | |
9615 | if (path == NULL) | |
9616 | return "unknown"; | |
9617 | hba = find_disk_attached_hba(-1, path); | |
9618 | if (hba && hba->type == SYS_DEV_SAS) | |
9619 | drv = "isci"; | |
9620 | else if (hba && hba->type == SYS_DEV_SATA) | |
9621 | drv = "ahci"; | |
1011e834 | 9622 | else |
96234762 LM |
9623 | drv = "unknown"; |
9624 | dprintf("path: %s hba: %s attached: %s\n", | |
9625 | path, (hba) ? hba->path : "NULL", drv); | |
9626 | free(path); | |
2cda7640 | 9627 | } |
96234762 | 9628 | return drv; |
2cda7640 ML |
9629 | } |
9630 | ||
4dd2df09 | 9631 | static char *imsm_find_array_devnm_by_subdev(int subdev, char *container) |
78b10e66 | 9632 | { |
4dd2df09 | 9633 | static char devnm[32]; |
78b10e66 N |
9634 | char subdev_name[20]; |
9635 | struct mdstat_ent *mdstat; | |
9636 | ||
9637 | sprintf(subdev_name, "%d", subdev); | |
9638 | mdstat = mdstat_by_subdev(subdev_name, container); | |
9639 | if (!mdstat) | |
4dd2df09 | 9640 | return NULL; |
78b10e66 | 9641 | |
4dd2df09 | 9642 | strcpy(devnm, mdstat->devnm); |
78b10e66 | 9643 | free_mdstat(mdstat); |
4dd2df09 | 9644 | return devnm; |
78b10e66 N |
9645 | } |
9646 | ||
9647 | static int imsm_reshape_is_allowed_on_container(struct supertype *st, | |
9648 | struct geo_params *geo, | |
fbf3d202 AK |
9649 | int *old_raid_disks, |
9650 | int direction) | |
78b10e66 | 9651 | { |
694575e7 KW |
9652 | /* currently we only support increasing the number of devices |
9653 | * for a container. This increases the number of device for each | |
9654 | * member array. They must all be RAID0 or RAID5. | |
9655 | */ | |
78b10e66 N |
9656 | int ret_val = 0; |
9657 | struct mdinfo *info, *member; | |
9658 | int devices_that_can_grow = 0; | |
9659 | ||
7a862a02 | 9660 | dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): st->devnm = (%s)\n", st->devnm); |
78b10e66 | 9661 | |
d04f65f4 | 9662 | if (geo->size > 0 || |
78b10e66 N |
9663 | geo->level != UnSet || |
9664 | geo->layout != UnSet || | |
9665 | geo->chunksize != 0 || | |
9666 | geo->raid_disks == UnSet) { | |
7a862a02 | 9667 | dprintf("imsm: Container operation is allowed for raid disks number change only.\n"); |
78b10e66 N |
9668 | return ret_val; |
9669 | } | |
9670 | ||
fbf3d202 | 9671 | if (direction == ROLLBACK_METADATA_CHANGES) { |
7a862a02 | 9672 | dprintf("imsm: Metadata changes rollback is not supported for container operation.\n"); |
fbf3d202 AK |
9673 | return ret_val; |
9674 | } | |
9675 | ||
78b10e66 N |
9676 | info = container_content_imsm(st, NULL); |
9677 | for (member = info; member; member = member->next) { | |
4dd2df09 | 9678 | char *result; |
78b10e66 N |
9679 | |
9680 | dprintf("imsm: checking device_num: %i\n", | |
9681 | member->container_member); | |
9682 | ||
d7d205bd | 9683 | if (geo->raid_disks <= member->array.raid_disks) { |
78b10e66 N |
9684 | /* we work on container for Online Capacity Expansion |
9685 | * only so raid_disks has to grow | |
9686 | */ | |
7a862a02 | 9687 | dprintf("imsm: for container operation raid disks increase is required\n"); |
78b10e66 N |
9688 | break; |
9689 | } | |
9690 | ||
9691 | if ((info->array.level != 0) && | |
9692 | (info->array.level != 5)) { | |
9693 | /* we cannot use this container with other raid level | |
9694 | */ | |
7a862a02 | 9695 | dprintf("imsm: for container operation wrong raid level (%i) detected\n", |
78b10e66 N |
9696 | info->array.level); |
9697 | break; | |
9698 | } else { | |
9699 | /* check for platform support | |
9700 | * for this raid level configuration | |
9701 | */ | |
9702 | struct intel_super *super = st->sb; | |
9703 | if (!is_raid_level_supported(super->orom, | |
9704 | member->array.level, | |
9705 | geo->raid_disks)) { | |
7a862a02 | 9706 | dprintf("platform does not support raid%d with %d disk%s\n", |
78b10e66 N |
9707 | info->array.level, |
9708 | geo->raid_disks, | |
9709 | geo->raid_disks > 1 ? "s" : ""); | |
9710 | break; | |
9711 | } | |
2a4a08e7 AK |
9712 | /* check if component size is aligned to chunk size |
9713 | */ | |
9714 | if (info->component_size % | |
9715 | (info->array.chunk_size/512)) { | |
7a862a02 | 9716 | dprintf("Component size is not aligned to chunk size\n"); |
2a4a08e7 AK |
9717 | break; |
9718 | } | |
78b10e66 N |
9719 | } |
9720 | ||
9721 | if (*old_raid_disks && | |
9722 | info->array.raid_disks != *old_raid_disks) | |
9723 | break; | |
9724 | *old_raid_disks = info->array.raid_disks; | |
9725 | ||
9726 | /* All raid5 and raid0 volumes in container | |
9727 | * have to be ready for Online Capacity Expansion | |
9728 | * so they need to be assembled. We have already | |
9729 | * checked that no recovery etc is happening. | |
9730 | */ | |
4dd2df09 N |
9731 | result = imsm_find_array_devnm_by_subdev(member->container_member, |
9732 | st->container_devnm); | |
9733 | if (result == NULL) { | |
78b10e66 N |
9734 | dprintf("imsm: cannot find array\n"); |
9735 | break; | |
9736 | } | |
9737 | devices_that_can_grow++; | |
9738 | } | |
9739 | sysfs_free(info); | |
9740 | if (!member && devices_that_can_grow) | |
9741 | ret_val = 1; | |
9742 | ||
9743 | if (ret_val) | |
1ade5cc1 | 9744 | dprintf("Container operation allowed\n"); |
78b10e66 | 9745 | else |
1ade5cc1 | 9746 | dprintf("Error: %i\n", ret_val); |
78b10e66 N |
9747 | |
9748 | return ret_val; | |
9749 | } | |
9750 | ||
9751 | /* Function: get_spares_for_grow | |
9752 | * Description: Allocates memory and creates list of spare devices | |
1011e834 | 9753 | * avaliable in container. Checks if spare drive size is acceptable. |
78b10e66 N |
9754 | * Parameters: Pointer to the supertype structure |
9755 | * Returns: Pointer to the list of spare devices (mdinfo structure) on success, | |
1011e834 | 9756 | * NULL if fail |
78b10e66 N |
9757 | */ |
9758 | static struct mdinfo *get_spares_for_grow(struct supertype *st) | |
9759 | { | |
78b10e66 | 9760 | unsigned long long min_size = min_acceptable_spare_size_imsm(st); |
326727d9 | 9761 | return container_choose_spares(st, min_size, NULL, NULL, NULL, 0); |
78b10e66 N |
9762 | } |
9763 | ||
9764 | /****************************************************************************** | |
9765 | * function: imsm_create_metadata_update_for_reshape | |
9766 | * Function creates update for whole IMSM container. | |
9767 | * | |
9768 | ******************************************************************************/ | |
9769 | static int imsm_create_metadata_update_for_reshape( | |
9770 | struct supertype *st, | |
9771 | struct geo_params *geo, | |
9772 | int old_raid_disks, | |
9773 | struct imsm_update_reshape **updatep) | |
9774 | { | |
9775 | struct intel_super *super = st->sb; | |
9776 | struct imsm_super *mpb = super->anchor; | |
9777 | int update_memory_size = 0; | |
9778 | struct imsm_update_reshape *u = NULL; | |
9779 | struct mdinfo *spares = NULL; | |
9780 | int i; | |
9781 | int delta_disks = 0; | |
bbd24d86 | 9782 | struct mdinfo *dev; |
78b10e66 | 9783 | |
1ade5cc1 | 9784 | dprintf("(enter) raid_disks = %i\n", geo->raid_disks); |
78b10e66 N |
9785 | |
9786 | delta_disks = geo->raid_disks - old_raid_disks; | |
9787 | ||
9788 | /* size of all update data without anchor */ | |
9789 | update_memory_size = sizeof(struct imsm_update_reshape); | |
9790 | ||
9791 | /* now add space for spare disks that we need to add. */ | |
9792 | update_memory_size += sizeof(u->new_disks[0]) * (delta_disks - 1); | |
9793 | ||
503975b9 | 9794 | u = xcalloc(1, update_memory_size); |
78b10e66 N |
9795 | u->type = update_reshape_container_disks; |
9796 | u->old_raid_disks = old_raid_disks; | |
9797 | u->new_raid_disks = geo->raid_disks; | |
9798 | ||
9799 | /* now get spare disks list | |
9800 | */ | |
9801 | spares = get_spares_for_grow(st); | |
9802 | ||
9803 | if (spares == NULL | |
9804 | || delta_disks > spares->array.spare_disks) { | |
7a862a02 | 9805 | pr_err("imsm: ERROR: Cannot get spare devices for %s.\n", geo->dev_name); |
e4c72d1d | 9806 | i = -1; |
78b10e66 N |
9807 | goto abort; |
9808 | } | |
9809 | ||
9810 | /* we have got spares | |
9811 | * update disk list in imsm_disk list table in anchor | |
9812 | */ | |
9813 | dprintf("imsm: %i spares are available.\n\n", | |
9814 | spares->array.spare_disks); | |
9815 | ||
bbd24d86 | 9816 | dev = spares->devs; |
78b10e66 | 9817 | for (i = 0; i < delta_disks; i++) { |
78b10e66 N |
9818 | struct dl *dl; |
9819 | ||
bbd24d86 AK |
9820 | if (dev == NULL) |
9821 | break; | |
78b10e66 N |
9822 | u->new_disks[i] = makedev(dev->disk.major, |
9823 | dev->disk.minor); | |
9824 | dl = get_disk_super(super, dev->disk.major, dev->disk.minor); | |
ee4beede AK |
9825 | dl->index = mpb->num_disks; |
9826 | mpb->num_disks++; | |
bbd24d86 | 9827 | dev = dev->next; |
78b10e66 | 9828 | } |
78b10e66 N |
9829 | |
9830 | abort: | |
9831 | /* free spares | |
9832 | */ | |
9833 | sysfs_free(spares); | |
9834 | ||
d677e0b8 | 9835 | dprintf("imsm: reshape update preparation :"); |
78b10e66 | 9836 | if (i == delta_disks) { |
1ade5cc1 | 9837 | dprintf_cont(" OK\n"); |
78b10e66 N |
9838 | *updatep = u; |
9839 | return update_memory_size; | |
9840 | } | |
9841 | free(u); | |
1ade5cc1 | 9842 | dprintf_cont(" Error\n"); |
78b10e66 N |
9843 | |
9844 | return 0; | |
9845 | } | |
9846 | ||
f3871fdc AK |
9847 | /****************************************************************************** |
9848 | * function: imsm_create_metadata_update_for_size_change() | |
9849 | * Creates update for IMSM array for array size change. | |
9850 | * | |
9851 | ******************************************************************************/ | |
9852 | static int imsm_create_metadata_update_for_size_change( | |
9853 | struct supertype *st, | |
9854 | struct geo_params *geo, | |
9855 | struct imsm_update_size_change **updatep) | |
9856 | { | |
9857 | struct intel_super *super = st->sb; | |
9858 | int update_memory_size = 0; | |
9859 | struct imsm_update_size_change *u = NULL; | |
9860 | ||
1ade5cc1 | 9861 | dprintf("(enter) New size = %llu\n", geo->size); |
f3871fdc AK |
9862 | |
9863 | /* size of all update data without anchor */ | |
9864 | update_memory_size = sizeof(struct imsm_update_size_change); | |
9865 | ||
503975b9 | 9866 | u = xcalloc(1, update_memory_size); |
f3871fdc AK |
9867 | u->type = update_size_change; |
9868 | u->subdev = super->current_vol; | |
9869 | u->new_size = geo->size; | |
9870 | ||
9871 | dprintf("imsm: reshape update preparation : OK\n"); | |
9872 | *updatep = u; | |
9873 | ||
9874 | return update_memory_size; | |
9875 | } | |
9876 | ||
48c5303a PC |
9877 | /****************************************************************************** |
9878 | * function: imsm_create_metadata_update_for_migration() | |
9879 | * Creates update for IMSM array. | |
9880 | * | |
9881 | ******************************************************************************/ | |
9882 | static int imsm_create_metadata_update_for_migration( | |
9883 | struct supertype *st, | |
9884 | struct geo_params *geo, | |
9885 | struct imsm_update_reshape_migration **updatep) | |
9886 | { | |
9887 | struct intel_super *super = st->sb; | |
9888 | int update_memory_size = 0; | |
9889 | struct imsm_update_reshape_migration *u = NULL; | |
9890 | struct imsm_dev *dev; | |
9891 | int previous_level = -1; | |
9892 | ||
1ade5cc1 | 9893 | dprintf("(enter) New Level = %i\n", geo->level); |
48c5303a PC |
9894 | |
9895 | /* size of all update data without anchor */ | |
9896 | update_memory_size = sizeof(struct imsm_update_reshape_migration); | |
9897 | ||
503975b9 | 9898 | u = xcalloc(1, update_memory_size); |
48c5303a PC |
9899 | u->type = update_reshape_migration; |
9900 | u->subdev = super->current_vol; | |
9901 | u->new_level = geo->level; | |
9902 | u->new_layout = geo->layout; | |
9903 | u->new_raid_disks = u->old_raid_disks = geo->raid_disks; | |
9904 | u->new_disks[0] = -1; | |
4bba0439 | 9905 | u->new_chunksize = -1; |
48c5303a PC |
9906 | |
9907 | dev = get_imsm_dev(super, u->subdev); | |
9908 | if (dev) { | |
9909 | struct imsm_map *map; | |
9910 | ||
238c0a71 | 9911 | map = get_imsm_map(dev, MAP_0); |
4bba0439 PC |
9912 | if (map) { |
9913 | int current_chunk_size = | |
9914 | __le16_to_cpu(map->blocks_per_strip) / 2; | |
9915 | ||
9916 | if (geo->chunksize != current_chunk_size) { | |
9917 | u->new_chunksize = geo->chunksize / 1024; | |
7a862a02 | 9918 | dprintf("imsm: chunk size change from %i to %i\n", |
4bba0439 PC |
9919 | current_chunk_size, u->new_chunksize); |
9920 | } | |
48c5303a | 9921 | previous_level = map->raid_level; |
4bba0439 | 9922 | } |
48c5303a PC |
9923 | } |
9924 | if ((geo->level == 5) && (previous_level == 0)) { | |
9925 | struct mdinfo *spares = NULL; | |
9926 | ||
9927 | u->new_raid_disks++; | |
9928 | spares = get_spares_for_grow(st); | |
9929 | if ((spares == NULL) || (spares->array.spare_disks < 1)) { | |
9930 | free(u); | |
9931 | sysfs_free(spares); | |
9932 | update_memory_size = 0; | |
7a862a02 | 9933 | dprintf("error: cannot get spare device for requested migration"); |
48c5303a PC |
9934 | return 0; |
9935 | } | |
9936 | sysfs_free(spares); | |
9937 | } | |
9938 | dprintf("imsm: reshape update preparation : OK\n"); | |
9939 | *updatep = u; | |
9940 | ||
9941 | return update_memory_size; | |
9942 | } | |
9943 | ||
8dd70bce AK |
9944 | static void imsm_update_metadata_locally(struct supertype *st, |
9945 | void *buf, int len) | |
9946 | { | |
9947 | struct metadata_update mu; | |
9948 | ||
9949 | mu.buf = buf; | |
9950 | mu.len = len; | |
9951 | mu.space = NULL; | |
9952 | mu.space_list = NULL; | |
9953 | mu.next = NULL; | |
5fe6f031 N |
9954 | if (imsm_prepare_update(st, &mu)) |
9955 | imsm_process_update(st, &mu); | |
8dd70bce AK |
9956 | |
9957 | while (mu.space_list) { | |
9958 | void **space = mu.space_list; | |
9959 | mu.space_list = *space; | |
9960 | free(space); | |
9961 | } | |
9962 | } | |
78b10e66 | 9963 | |
471bceb6 | 9964 | /*************************************************************************** |
694575e7 | 9965 | * Function: imsm_analyze_change |
471bceb6 | 9966 | * Description: Function analyze change for single volume |
1011e834 | 9967 | * and validate if transition is supported |
fbf3d202 AK |
9968 | * Parameters: Geometry parameters, supertype structure, |
9969 | * metadata change direction (apply/rollback) | |
694575e7 | 9970 | * Returns: Operation type code on success, -1 if fail |
471bceb6 KW |
9971 | ****************************************************************************/ |
9972 | enum imsm_reshape_type imsm_analyze_change(struct supertype *st, | |
fbf3d202 AK |
9973 | struct geo_params *geo, |
9974 | int direction) | |
694575e7 | 9975 | { |
471bceb6 KW |
9976 | struct mdinfo info; |
9977 | int change = -1; | |
9978 | int check_devs = 0; | |
c21e737b | 9979 | int chunk; |
67a2db32 AK |
9980 | /* number of added/removed disks in operation result */ |
9981 | int devNumChange = 0; | |
9982 | /* imsm compatible layout value for array geometry verification */ | |
9983 | int imsm_layout = -1; | |
7abc9871 AK |
9984 | int data_disks; |
9985 | struct imsm_dev *dev; | |
9986 | struct intel_super *super; | |
d04f65f4 | 9987 | unsigned long long current_size; |
65d38cca | 9988 | unsigned long long free_size; |
d04f65f4 | 9989 | unsigned long long max_size; |
65d38cca | 9990 | int rv; |
471bceb6 KW |
9991 | |
9992 | getinfo_super_imsm_volume(st, &info, NULL); | |
471bceb6 KW |
9993 | if ((geo->level != info.array.level) && |
9994 | (geo->level >= 0) && | |
9995 | (geo->level != UnSet)) { | |
9996 | switch (info.array.level) { | |
9997 | case 0: | |
9998 | if (geo->level == 5) { | |
b5347799 | 9999 | change = CH_MIGRATION; |
e13ce846 | 10000 | if (geo->layout != ALGORITHM_LEFT_ASYMMETRIC) { |
7a862a02 | 10001 | pr_err("Error. Requested Layout not supported (left-asymmetric layout is supported only)!\n"); |
e13ce846 AK |
10002 | change = -1; |
10003 | goto analyse_change_exit; | |
10004 | } | |
67a2db32 | 10005 | imsm_layout = geo->layout; |
471bceb6 | 10006 | check_devs = 1; |
e91a3bad LM |
10007 | devNumChange = 1; /* parity disk added */ |
10008 | } else if (geo->level == 10) { | |
471bceb6 KW |
10009 | change = CH_TAKEOVER; |
10010 | check_devs = 1; | |
e91a3bad | 10011 | devNumChange = 2; /* two mirrors added */ |
67a2db32 | 10012 | imsm_layout = 0x102; /* imsm supported layout */ |
471bceb6 | 10013 | } |
dfe77a9e KW |
10014 | break; |
10015 | case 1: | |
471bceb6 KW |
10016 | case 10: |
10017 | if (geo->level == 0) { | |
10018 | change = CH_TAKEOVER; | |
10019 | check_devs = 1; | |
e91a3bad | 10020 | devNumChange = -(geo->raid_disks/2); |
67a2db32 | 10021 | imsm_layout = 0; /* imsm raid0 layout */ |
471bceb6 KW |
10022 | } |
10023 | break; | |
10024 | } | |
10025 | if (change == -1) { | |
7a862a02 | 10026 | pr_err("Error. Level Migration from %d to %d not supported!\n", |
e7b84f9d | 10027 | info.array.level, geo->level); |
471bceb6 KW |
10028 | goto analyse_change_exit; |
10029 | } | |
10030 | } else | |
10031 | geo->level = info.array.level; | |
10032 | ||
10033 | if ((geo->layout != info.array.layout) | |
10034 | && ((geo->layout != UnSet) && (geo->layout != -1))) { | |
b5347799 | 10035 | change = CH_MIGRATION; |
471bceb6 KW |
10036 | if ((info.array.layout == 0) |
10037 | && (info.array.level == 5) | |
10038 | && (geo->layout == 5)) { | |
10039 | /* reshape 5 -> 4 */ | |
10040 | } else if ((info.array.layout == 5) | |
10041 | && (info.array.level == 5) | |
10042 | && (geo->layout == 0)) { | |
10043 | /* reshape 4 -> 5 */ | |
10044 | geo->layout = 0; | |
10045 | geo->level = 5; | |
10046 | } else { | |
7a862a02 | 10047 | pr_err("Error. Layout Migration from %d to %d not supported!\n", |
e7b84f9d | 10048 | info.array.layout, geo->layout); |
471bceb6 KW |
10049 | change = -1; |
10050 | goto analyse_change_exit; | |
10051 | } | |
67a2db32 | 10052 | } else { |
471bceb6 | 10053 | geo->layout = info.array.layout; |
67a2db32 AK |
10054 | if (imsm_layout == -1) |
10055 | imsm_layout = info.array.layout; | |
10056 | } | |
471bceb6 KW |
10057 | |
10058 | if ((geo->chunksize > 0) && (geo->chunksize != UnSet) | |
10059 | && (geo->chunksize != info.array.chunk_size)) | |
b5347799 | 10060 | change = CH_MIGRATION; |
471bceb6 KW |
10061 | else |
10062 | geo->chunksize = info.array.chunk_size; | |
10063 | ||
c21e737b | 10064 | chunk = geo->chunksize / 1024; |
7abc9871 AK |
10065 | |
10066 | super = st->sb; | |
10067 | dev = get_imsm_dev(super, super->current_vol); | |
10068 | data_disks = imsm_num_data_members(dev , MAP_0); | |
c41e00b2 | 10069 | /* compute current size per disk member |
7abc9871 | 10070 | */ |
c41e00b2 AK |
10071 | current_size = info.custom_array_size / data_disks; |
10072 | ||
d5d2c614 | 10073 | if ((geo->size > 0) && (geo->size != MAX_SIZE)) { |
c41e00b2 AK |
10074 | /* align component size |
10075 | */ | |
10076 | geo->size = imsm_component_size_aligment_check( | |
10077 | get_imsm_raid_level(dev->vol.map), | |
10078 | chunk * 1024, | |
10079 | geo->size * 2); | |
65d0b4ce | 10080 | if (geo->size == 0) { |
7a862a02 | 10081 | pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is 0).\n", |
65d0b4ce LD |
10082 | current_size); |
10083 | goto analyse_change_exit; | |
10084 | } | |
c41e00b2 | 10085 | } |
7abc9871 | 10086 | |
d04f65f4 | 10087 | if ((current_size != geo->size) && (geo->size > 0)) { |
7abc9871 | 10088 | if (change != -1) { |
7a862a02 | 10089 | pr_err("Error. Size change should be the only one at a time.\n"); |
7abc9871 AK |
10090 | change = -1; |
10091 | goto analyse_change_exit; | |
10092 | } | |
10093 | if ((super->current_vol + 1) != super->anchor->num_raid_devs) { | |
7a862a02 | 10094 | pr_err("Error. The last volume in container can be expanded only (%i/%s).\n", |
4dd2df09 | 10095 | super->current_vol, st->devnm); |
7abc9871 AK |
10096 | goto analyse_change_exit; |
10097 | } | |
65d38cca LD |
10098 | /* check the maximum available size |
10099 | */ | |
10100 | rv = imsm_get_free_size(st, dev->vol.map->num_members, | |
10101 | 0, chunk, &free_size); | |
10102 | if (rv == 0) | |
10103 | /* Cannot find maximum available space | |
10104 | */ | |
10105 | max_size = 0; | |
10106 | else { | |
10107 | max_size = free_size + current_size; | |
10108 | /* align component size | |
10109 | */ | |
10110 | max_size = imsm_component_size_aligment_check( | |
10111 | get_imsm_raid_level(dev->vol.map), | |
10112 | chunk * 1024, | |
10113 | max_size); | |
10114 | } | |
d04f65f4 | 10115 | if (geo->size == MAX_SIZE) { |
b130333f AK |
10116 | /* requested size change to the maximum available size |
10117 | */ | |
65d38cca | 10118 | if (max_size == 0) { |
7a862a02 | 10119 | pr_err("Error. Cannot find maximum available space.\n"); |
b130333f AK |
10120 | change = -1; |
10121 | goto analyse_change_exit; | |
65d38cca LD |
10122 | } else |
10123 | geo->size = max_size; | |
c41e00b2 | 10124 | } |
b130333f | 10125 | |
fbf3d202 AK |
10126 | if ((direction == ROLLBACK_METADATA_CHANGES)) { |
10127 | /* accept size for rollback only | |
10128 | */ | |
10129 | } else { | |
10130 | /* round size due to metadata compatibility | |
10131 | */ | |
10132 | geo->size = (geo->size >> SECT_PER_MB_SHIFT) | |
10133 | << SECT_PER_MB_SHIFT; | |
10134 | dprintf("Prepare update for size change to %llu\n", | |
10135 | geo->size ); | |
10136 | if (current_size >= geo->size) { | |
7a862a02 | 10137 | pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is %llu).\n", |
e7b84f9d | 10138 | current_size, geo->size); |
fbf3d202 AK |
10139 | goto analyse_change_exit; |
10140 | } | |
65d38cca | 10141 | if (max_size && geo->size > max_size) { |
7a862a02 | 10142 | pr_err("Error. Requested size is larger than maximum available size (maximum available size is %llu, requested size /rounded/ is %llu).\n", |
e7b84f9d | 10143 | max_size, geo->size); |
65d38cca LD |
10144 | goto analyse_change_exit; |
10145 | } | |
7abc9871 AK |
10146 | } |
10147 | geo->size *= data_disks; | |
10148 | geo->raid_disks = dev->vol.map->num_members; | |
10149 | change = CH_ARRAY_SIZE; | |
10150 | } | |
471bceb6 KW |
10151 | if (!validate_geometry_imsm(st, |
10152 | geo->level, | |
67a2db32 | 10153 | imsm_layout, |
e91a3bad | 10154 | geo->raid_disks + devNumChange, |
c21e737b | 10155 | &chunk, |
af4348dd | 10156 | geo->size, INVALID_SECTORS, |
471bceb6 KW |
10157 | 0, 0, 1)) |
10158 | change = -1; | |
10159 | ||
10160 | if (check_devs) { | |
10161 | struct intel_super *super = st->sb; | |
10162 | struct imsm_super *mpb = super->anchor; | |
10163 | ||
10164 | if (mpb->num_raid_devs > 1) { | |
7a862a02 | 10165 | pr_err("Error. Cannot perform operation on %s- for this operation it MUST be single array in container\n", |
e7b84f9d | 10166 | geo->dev_name); |
471bceb6 KW |
10167 | change = -1; |
10168 | } | |
10169 | } | |
10170 | ||
10171 | analyse_change_exit: | |
fbf3d202 AK |
10172 | if ((direction == ROLLBACK_METADATA_CHANGES) && |
10173 | ((change == CH_MIGRATION) || (change == CH_TAKEOVER))) { | |
7a862a02 | 10174 | dprintf("imsm: Metadata changes rollback is not supported for migration and takeover operations.\n"); |
fbf3d202 AK |
10175 | change = -1; |
10176 | } | |
471bceb6 | 10177 | return change; |
694575e7 KW |
10178 | } |
10179 | ||
bb025c2f KW |
10180 | int imsm_takeover(struct supertype *st, struct geo_params *geo) |
10181 | { | |
10182 | struct intel_super *super = st->sb; | |
10183 | struct imsm_update_takeover *u; | |
10184 | ||
503975b9 | 10185 | u = xmalloc(sizeof(struct imsm_update_takeover)); |
bb025c2f KW |
10186 | |
10187 | u->type = update_takeover; | |
10188 | u->subarray = super->current_vol; | |
10189 | ||
10190 | /* 10->0 transition */ | |
10191 | if (geo->level == 0) | |
10192 | u->direction = R10_TO_R0; | |
10193 | ||
0529c688 KW |
10194 | /* 0->10 transition */ |
10195 | if (geo->level == 10) | |
10196 | u->direction = R0_TO_R10; | |
10197 | ||
bb025c2f KW |
10198 | /* update metadata locally */ |
10199 | imsm_update_metadata_locally(st, u, | |
10200 | sizeof(struct imsm_update_takeover)); | |
10201 | /* and possibly remotely */ | |
10202 | if (st->update_tail) | |
10203 | append_metadata_update(st, u, | |
10204 | sizeof(struct imsm_update_takeover)); | |
10205 | else | |
10206 | free(u); | |
10207 | ||
10208 | return 0; | |
10209 | } | |
10210 | ||
d04f65f4 N |
10211 | static int imsm_reshape_super(struct supertype *st, unsigned long long size, |
10212 | int level, | |
78b10e66 | 10213 | int layout, int chunksize, int raid_disks, |
41784c88 | 10214 | int delta_disks, char *backup, char *dev, |
016e00f5 | 10215 | int direction, int verbose) |
78b10e66 | 10216 | { |
78b10e66 N |
10217 | int ret_val = 1; |
10218 | struct geo_params geo; | |
10219 | ||
1ade5cc1 | 10220 | dprintf("(enter)\n"); |
78b10e66 | 10221 | |
71204a50 | 10222 | memset(&geo, 0, sizeof(struct geo_params)); |
78b10e66 N |
10223 | |
10224 | geo.dev_name = dev; | |
4dd2df09 | 10225 | strcpy(geo.devnm, st->devnm); |
78b10e66 N |
10226 | geo.size = size; |
10227 | geo.level = level; | |
10228 | geo.layout = layout; | |
10229 | geo.chunksize = chunksize; | |
10230 | geo.raid_disks = raid_disks; | |
41784c88 AK |
10231 | if (delta_disks != UnSet) |
10232 | geo.raid_disks += delta_disks; | |
78b10e66 | 10233 | |
1ade5cc1 N |
10234 | dprintf("for level : %i\n", geo.level); |
10235 | dprintf("for raid_disks : %i\n", geo.raid_disks); | |
78b10e66 N |
10236 | |
10237 | if (experimental() == 0) | |
10238 | return ret_val; | |
10239 | ||
4dd2df09 | 10240 | if (strcmp(st->container_devnm, st->devnm) == 0) { |
694575e7 KW |
10241 | /* On container level we can only increase number of devices. */ |
10242 | dprintf("imsm: info: Container operation\n"); | |
78b10e66 | 10243 | int old_raid_disks = 0; |
6dc0be30 | 10244 | |
78b10e66 | 10245 | if (imsm_reshape_is_allowed_on_container( |
fbf3d202 | 10246 | st, &geo, &old_raid_disks, direction)) { |
78b10e66 N |
10247 | struct imsm_update_reshape *u = NULL; |
10248 | int len; | |
10249 | ||
10250 | len = imsm_create_metadata_update_for_reshape( | |
10251 | st, &geo, old_raid_disks, &u); | |
10252 | ||
ed08d51c AK |
10253 | if (len <= 0) { |
10254 | dprintf("imsm: Cannot prepare update\n"); | |
10255 | goto exit_imsm_reshape_super; | |
10256 | } | |
10257 | ||
8dd70bce AK |
10258 | ret_val = 0; |
10259 | /* update metadata locally */ | |
10260 | imsm_update_metadata_locally(st, u, len); | |
10261 | /* and possibly remotely */ | |
10262 | if (st->update_tail) | |
10263 | append_metadata_update(st, u, len); | |
10264 | else | |
ed08d51c | 10265 | free(u); |
8dd70bce | 10266 | |
694575e7 | 10267 | } else { |
7a862a02 | 10268 | pr_err("(imsm) Operation is not allowed on this container\n"); |
694575e7 KW |
10269 | } |
10270 | } else { | |
10271 | /* On volume level we support following operations | |
471bceb6 KW |
10272 | * - takeover: raid10 -> raid0; raid0 -> raid10 |
10273 | * - chunk size migration | |
10274 | * - migration: raid5 -> raid0; raid0 -> raid5 | |
10275 | */ | |
10276 | struct intel_super *super = st->sb; | |
10277 | struct intel_dev *dev = super->devlist; | |
4dd2df09 | 10278 | int change; |
694575e7 | 10279 | dprintf("imsm: info: Volume operation\n"); |
471bceb6 KW |
10280 | /* find requested device */ |
10281 | while (dev) { | |
1011e834 | 10282 | char *devnm = |
4dd2df09 N |
10283 | imsm_find_array_devnm_by_subdev( |
10284 | dev->index, st->container_devnm); | |
10285 | if (devnm && strcmp(devnm, geo.devnm) == 0) | |
471bceb6 KW |
10286 | break; |
10287 | dev = dev->next; | |
10288 | } | |
10289 | if (dev == NULL) { | |
4dd2df09 N |
10290 | pr_err("Cannot find %s (%s) subarray\n", |
10291 | geo.dev_name, geo.devnm); | |
471bceb6 KW |
10292 | goto exit_imsm_reshape_super; |
10293 | } | |
10294 | super->current_vol = dev->index; | |
fbf3d202 | 10295 | change = imsm_analyze_change(st, &geo, direction); |
694575e7 | 10296 | switch (change) { |
471bceb6 | 10297 | case CH_TAKEOVER: |
bb025c2f | 10298 | ret_val = imsm_takeover(st, &geo); |
694575e7 | 10299 | break; |
48c5303a PC |
10300 | case CH_MIGRATION: { |
10301 | struct imsm_update_reshape_migration *u = NULL; | |
10302 | int len = | |
10303 | imsm_create_metadata_update_for_migration( | |
10304 | st, &geo, &u); | |
10305 | if (len < 1) { | |
7a862a02 | 10306 | dprintf("imsm: Cannot prepare update\n"); |
48c5303a PC |
10307 | break; |
10308 | } | |
471bceb6 | 10309 | ret_val = 0; |
48c5303a PC |
10310 | /* update metadata locally */ |
10311 | imsm_update_metadata_locally(st, u, len); | |
10312 | /* and possibly remotely */ | |
10313 | if (st->update_tail) | |
10314 | append_metadata_update(st, u, len); | |
10315 | else | |
10316 | free(u); | |
10317 | } | |
10318 | break; | |
7abc9871 | 10319 | case CH_ARRAY_SIZE: { |
f3871fdc AK |
10320 | struct imsm_update_size_change *u = NULL; |
10321 | int len = | |
10322 | imsm_create_metadata_update_for_size_change( | |
10323 | st, &geo, &u); | |
10324 | if (len < 1) { | |
7a862a02 | 10325 | dprintf("imsm: Cannot prepare update\n"); |
f3871fdc AK |
10326 | break; |
10327 | } | |
10328 | ret_val = 0; | |
10329 | /* update metadata locally */ | |
10330 | imsm_update_metadata_locally(st, u, len); | |
10331 | /* and possibly remotely */ | |
10332 | if (st->update_tail) | |
10333 | append_metadata_update(st, u, len); | |
10334 | else | |
10335 | free(u); | |
7abc9871 AK |
10336 | } |
10337 | break; | |
471bceb6 KW |
10338 | default: |
10339 | ret_val = 1; | |
694575e7 | 10340 | } |
694575e7 | 10341 | } |
78b10e66 | 10342 | |
ed08d51c | 10343 | exit_imsm_reshape_super: |
78b10e66 N |
10344 | dprintf("imsm: reshape_super Exit code = %i\n", ret_val); |
10345 | return ret_val; | |
10346 | } | |
2cda7640 | 10347 | |
eee67a47 AK |
10348 | /******************************************************************************* |
10349 | * Function: wait_for_reshape_imsm | |
10350 | * Description: Function writes new sync_max value and waits until | |
10351 | * reshape process reach new position | |
10352 | * Parameters: | |
10353 | * sra : general array info | |
eee67a47 AK |
10354 | * ndata : number of disks in new array's layout |
10355 | * Returns: | |
10356 | * 0 : success, | |
10357 | * 1 : there is no reshape in progress, | |
10358 | * -1 : fail | |
10359 | ******************************************************************************/ | |
ae9f01f8 | 10360 | int wait_for_reshape_imsm(struct mdinfo *sra, int ndata) |
eee67a47 | 10361 | { |
85ca499c | 10362 | int fd = sysfs_get_fd(sra, NULL, "sync_completed"); |
eee67a47 | 10363 | unsigned long long completed; |
ae9f01f8 AK |
10364 | /* to_complete : new sync_max position */ |
10365 | unsigned long long to_complete = sra->reshape_progress; | |
10366 | unsigned long long position_to_set = to_complete / ndata; | |
eee67a47 | 10367 | |
ae9f01f8 | 10368 | if (fd < 0) { |
1ade5cc1 | 10369 | dprintf("cannot open reshape_position\n"); |
eee67a47 | 10370 | return 1; |
ae9f01f8 | 10371 | } |
eee67a47 | 10372 | |
ae9f01f8 | 10373 | if (sysfs_fd_get_ll(fd, &completed) < 0) { |
1ade5cc1 | 10374 | dprintf("cannot read reshape_position (no reshape in progres)\n"); |
ae9f01f8 | 10375 | close(fd); |
c85338c6 | 10376 | return 1; |
ae9f01f8 | 10377 | } |
eee67a47 | 10378 | |
85ca499c | 10379 | if (completed > position_to_set) { |
1ade5cc1 | 10380 | dprintf("wrong next position to set %llu (%llu)\n", |
85ca499c | 10381 | to_complete, position_to_set); |
ae9f01f8 AK |
10382 | close(fd); |
10383 | return -1; | |
10384 | } | |
10385 | dprintf("Position set: %llu\n", position_to_set); | |
10386 | if (sysfs_set_num(sra, NULL, "sync_max", | |
10387 | position_to_set) != 0) { | |
1ade5cc1 | 10388 | dprintf("cannot set reshape position to %llu\n", |
ae9f01f8 AK |
10389 | position_to_set); |
10390 | close(fd); | |
10391 | return -1; | |
eee67a47 AK |
10392 | } |
10393 | ||
eee67a47 AK |
10394 | do { |
10395 | char action[20]; | |
5ff3a780 AP |
10396 | int timeout = 3000; |
10397 | sysfs_wait(fd, &timeout); | |
a47e44fb AK |
10398 | if (sysfs_get_str(sra, NULL, "sync_action", |
10399 | action, 20) > 0 && | |
d7d3809a AP |
10400 | strncmp(action, "reshape", 7) != 0) { |
10401 | close(fd); | |
10402 | return -1; | |
10403 | } | |
eee67a47 | 10404 | if (sysfs_fd_get_ll(fd, &completed) < 0) { |
1ade5cc1 | 10405 | dprintf("cannot read reshape_position (in loop)\n"); |
eee67a47 AK |
10406 | close(fd); |
10407 | return 1; | |
10408 | } | |
85ca499c | 10409 | } while (completed < position_to_set); |
eee67a47 AK |
10410 | close(fd); |
10411 | return 0; | |
10412 | ||
10413 | } | |
10414 | ||
b915c95f AK |
10415 | /******************************************************************************* |
10416 | * Function: check_degradation_change | |
10417 | * Description: Check that array hasn't become failed. | |
10418 | * Parameters: | |
10419 | * info : for sysfs access | |
10420 | * sources : source disks descriptors | |
10421 | * degraded: previous degradation level | |
10422 | * Returns: | |
10423 | * degradation level | |
10424 | ******************************************************************************/ | |
10425 | int check_degradation_change(struct mdinfo *info, | |
10426 | int *sources, | |
10427 | int degraded) | |
10428 | { | |
10429 | unsigned long long new_degraded; | |
e1993023 LD |
10430 | int rv; |
10431 | ||
10432 | rv = sysfs_get_ll(info, NULL, "degraded", &new_degraded); | |
10433 | if ((rv == -1) || (new_degraded != (unsigned long long)degraded)) { | |
b915c95f AK |
10434 | /* check each device to ensure it is still working */ |
10435 | struct mdinfo *sd; | |
10436 | new_degraded = 0; | |
10437 | for (sd = info->devs ; sd ; sd = sd->next) { | |
10438 | if (sd->disk.state & (1<<MD_DISK_FAULTY)) | |
10439 | continue; | |
10440 | if (sd->disk.state & (1<<MD_DISK_SYNC)) { | |
10441 | char sbuf[20]; | |
10442 | if (sysfs_get_str(info, | |
10443 | sd, "state", sbuf, 20) < 0 || | |
10444 | strstr(sbuf, "faulty") || | |
10445 | strstr(sbuf, "in_sync") == NULL) { | |
10446 | /* this device is dead */ | |
10447 | sd->disk.state = (1<<MD_DISK_FAULTY); | |
10448 | if (sd->disk.raid_disk >= 0 && | |
10449 | sources[sd->disk.raid_disk] >= 0) { | |
10450 | close(sources[ | |
10451 | sd->disk.raid_disk]); | |
10452 | sources[sd->disk.raid_disk] = | |
10453 | -1; | |
10454 | } | |
10455 | new_degraded++; | |
10456 | } | |
10457 | } | |
10458 | } | |
10459 | } | |
10460 | ||
10461 | return new_degraded; | |
10462 | } | |
10463 | ||
10f22854 AK |
10464 | /******************************************************************************* |
10465 | * Function: imsm_manage_reshape | |
10466 | * Description: Function finds array under reshape and it manages reshape | |
10467 | * process. It creates stripes backups (if required) and sets | |
942e1cdb | 10468 | * checkpoints. |
10f22854 AK |
10469 | * Parameters: |
10470 | * afd : Backup handle (nattive) - not used | |
10471 | * sra : general array info | |
10472 | * reshape : reshape parameters - not used | |
10473 | * st : supertype structure | |
10474 | * blocks : size of critical section [blocks] | |
10475 | * fds : table of source device descriptor | |
10476 | * offsets : start of array (offest per devices) | |
10477 | * dests : not used | |
10478 | * destfd : table of destination device descriptor | |
10479 | * destoffsets : table of destination offsets (per device) | |
10480 | * Returns: | |
10481 | * 1 : success, reshape is done | |
10482 | * 0 : fail | |
10483 | ******************************************************************************/ | |
999b4972 N |
10484 | static int imsm_manage_reshape( |
10485 | int afd, struct mdinfo *sra, struct reshape *reshape, | |
10f22854 | 10486 | struct supertype *st, unsigned long backup_blocks, |
999b4972 N |
10487 | int *fds, unsigned long long *offsets, |
10488 | int dests, int *destfd, unsigned long long *destoffsets) | |
10489 | { | |
10f22854 AK |
10490 | int ret_val = 0; |
10491 | struct intel_super *super = st->sb; | |
10492 | struct intel_dev *dv = NULL; | |
10493 | struct imsm_dev *dev = NULL; | |
a6b6d984 | 10494 | struct imsm_map *map_src; |
10f22854 AK |
10495 | int migr_vol_qan = 0; |
10496 | int ndata, odata; /* [bytes] */ | |
10497 | int chunk; /* [bytes] */ | |
10498 | struct migr_record *migr_rec; | |
10499 | char *buf = NULL; | |
10500 | unsigned int buf_size; /* [bytes] */ | |
10501 | unsigned long long max_position; /* array size [bytes] */ | |
10502 | unsigned long long next_step; /* [blocks]/[bytes] */ | |
10503 | unsigned long long old_data_stripe_length; | |
10f22854 AK |
10504 | unsigned long long start_src; /* [bytes] */ |
10505 | unsigned long long start; /* [bytes] */ | |
10506 | unsigned long long start_buf_shift; /* [bytes] */ | |
b915c95f | 10507 | int degraded = 0; |
ab724b98 | 10508 | int source_layout = 0; |
10f22854 | 10509 | |
1ab242d8 | 10510 | if (!fds || !offsets || !sra) |
10f22854 AK |
10511 | goto abort; |
10512 | ||
10513 | /* Find volume during the reshape */ | |
10514 | for (dv = super->devlist; dv; dv = dv->next) { | |
10515 | if (dv->dev->vol.migr_type == MIGR_GEN_MIGR | |
10516 | && dv->dev->vol.migr_state == 1) { | |
10517 | dev = dv->dev; | |
10518 | migr_vol_qan++; | |
10519 | } | |
10520 | } | |
10521 | /* Only one volume can migrate at the same time */ | |
10522 | if (migr_vol_qan != 1) { | |
e7b84f9d | 10523 | pr_err(": %s", migr_vol_qan ? |
10f22854 AK |
10524 | "Number of migrating volumes greater than 1\n" : |
10525 | "There is no volume during migrationg\n"); | |
10526 | goto abort; | |
10527 | } | |
10528 | ||
238c0a71 | 10529 | map_src = get_imsm_map(dev, MAP_1); |
10f22854 AK |
10530 | if (map_src == NULL) |
10531 | goto abort; | |
10f22854 | 10532 | |
238c0a71 AK |
10533 | ndata = imsm_num_data_members(dev, MAP_0); |
10534 | odata = imsm_num_data_members(dev, MAP_1); | |
10f22854 | 10535 | |
7b1ab482 | 10536 | chunk = __le16_to_cpu(map_src->blocks_per_strip) * 512; |
10f22854 AK |
10537 | old_data_stripe_length = odata * chunk; |
10538 | ||
10539 | migr_rec = super->migr_rec; | |
10540 | ||
10f22854 AK |
10541 | /* initialize migration record for start condition */ |
10542 | if (sra->reshape_progress == 0) | |
10543 | init_migr_record_imsm(st, dev, sra); | |
b2c59438 AK |
10544 | else { |
10545 | if (__le32_to_cpu(migr_rec->rec_status) != UNIT_SRC_NORMAL) { | |
7a862a02 | 10546 | dprintf("imsm: cannot restart migration when data are present in copy area.\n"); |
b2c59438 AK |
10547 | goto abort; |
10548 | } | |
6a75c8ca AK |
10549 | /* Save checkpoint to update migration record for current |
10550 | * reshape position (in md). It can be farther than current | |
10551 | * reshape position in metadata. | |
10552 | */ | |
10553 | if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL) == 1) { | |
10554 | /* ignore error == 2, this can mean end of reshape here | |
10555 | */ | |
7a862a02 | 10556 | dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL, initial save)\n"); |
6a75c8ca AK |
10557 | goto abort; |
10558 | } | |
b2c59438 | 10559 | } |
10f22854 AK |
10560 | |
10561 | /* size for data */ | |
10562 | buf_size = __le32_to_cpu(migr_rec->blocks_per_unit) * 512; | |
10563 | /* extend buffer size for parity disk */ | |
10564 | buf_size += __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512; | |
10565 | /* add space for stripe aligment */ | |
10566 | buf_size += old_data_stripe_length; | |
10567 | if (posix_memalign((void **)&buf, 4096, buf_size)) { | |
10568 | dprintf("imsm: Cannot allocate checpoint buffer\n"); | |
10569 | goto abort; | |
10570 | } | |
10571 | ||
3ef4403c | 10572 | max_position = sra->component_size * ndata; |
68eb8bc6 | 10573 | source_layout = imsm_level_to_layout(map_src->raid_level); |
10f22854 AK |
10574 | |
10575 | while (__le32_to_cpu(migr_rec->curr_migr_unit) < | |
10576 | __le32_to_cpu(migr_rec->num_migr_units)) { | |
10577 | /* current reshape position [blocks] */ | |
10578 | unsigned long long current_position = | |
10579 | __le32_to_cpu(migr_rec->blocks_per_unit) | |
10580 | * __le32_to_cpu(migr_rec->curr_migr_unit); | |
10581 | unsigned long long border; | |
10582 | ||
b915c95f AK |
10583 | /* Check that array hasn't become failed. |
10584 | */ | |
10585 | degraded = check_degradation_change(sra, fds, degraded); | |
10586 | if (degraded > 1) { | |
7a862a02 | 10587 | dprintf("imsm: Abort reshape due to degradation level (%i)\n", degraded); |
b915c95f AK |
10588 | goto abort; |
10589 | } | |
10590 | ||
10f22854 AK |
10591 | next_step = __le32_to_cpu(migr_rec->blocks_per_unit); |
10592 | ||
10593 | if ((current_position + next_step) > max_position) | |
10594 | next_step = max_position - current_position; | |
10595 | ||
92144abf | 10596 | start = current_position * 512; |
10f22854 | 10597 | |
942e1cdb | 10598 | /* align reading start to old geometry */ |
10f22854 AK |
10599 | start_buf_shift = start % old_data_stripe_length; |
10600 | start_src = start - start_buf_shift; | |
10601 | ||
10602 | border = (start_src / odata) - (start / ndata); | |
10603 | border /= 512; | |
10604 | if (border <= __le32_to_cpu(migr_rec->dest_depth_per_unit)) { | |
10605 | /* save critical stripes to buf | |
10606 | * start - start address of current unit | |
10607 | * to backup [bytes] | |
10608 | * start_src - start address of current unit | |
10609 | * to backup alligned to source array | |
10610 | * [bytes] | |
10611 | */ | |
10612 | unsigned long long next_step_filler = 0; | |
10613 | unsigned long long copy_length = next_step * 512; | |
10614 | ||
10615 | /* allign copy area length to stripe in old geometry */ | |
10616 | next_step_filler = ((copy_length + start_buf_shift) | |
10617 | % old_data_stripe_length); | |
10618 | if (next_step_filler) | |
10619 | next_step_filler = (old_data_stripe_length | |
10620 | - next_step_filler); | |
7a862a02 | 10621 | 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 |
10622 | start, start_src, copy_length, |
10623 | start_buf_shift, next_step_filler); | |
10624 | ||
10625 | if (save_stripes(fds, offsets, map_src->num_members, | |
ab724b98 AK |
10626 | chunk, map_src->raid_level, |
10627 | source_layout, 0, NULL, start_src, | |
10f22854 AK |
10628 | copy_length + |
10629 | next_step_filler + start_buf_shift, | |
10630 | buf)) { | |
7a862a02 | 10631 | dprintf("imsm: Cannot save stripes to buffer\n"); |
10f22854 AK |
10632 | goto abort; |
10633 | } | |
10634 | /* Convert data to destination format and store it | |
10635 | * in backup general migration area | |
10636 | */ | |
10637 | if (save_backup_imsm(st, dev, sra, | |
aea93171 | 10638 | buf + start_buf_shift, copy_length)) { |
7a862a02 | 10639 | dprintf("imsm: Cannot save stripes to target devices\n"); |
10f22854 AK |
10640 | goto abort; |
10641 | } | |
10642 | if (save_checkpoint_imsm(st, sra, | |
10643 | UNIT_SRC_IN_CP_AREA)) { | |
7a862a02 | 10644 | dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_IN_CP_AREA)\n"); |
10f22854 AK |
10645 | goto abort; |
10646 | } | |
8016a6d4 AK |
10647 | } else { |
10648 | /* set next step to use whole border area */ | |
10649 | border /= next_step; | |
10650 | if (border > 1) | |
10651 | next_step *= border; | |
10f22854 AK |
10652 | } |
10653 | /* When data backed up, checkpoint stored, | |
10654 | * kick the kernel to reshape unit of data | |
10655 | */ | |
10656 | next_step = next_step + sra->reshape_progress; | |
8016a6d4 AK |
10657 | /* limit next step to array max position */ |
10658 | if (next_step > max_position) | |
10659 | next_step = max_position; | |
10f22854 AK |
10660 | sysfs_set_num(sra, NULL, "suspend_lo", sra->reshape_progress); |
10661 | sysfs_set_num(sra, NULL, "suspend_hi", next_step); | |
ae9f01f8 | 10662 | sra->reshape_progress = next_step; |
10f22854 AK |
10663 | |
10664 | /* wait until reshape finish */ | |
c85338c6 | 10665 | if (wait_for_reshape_imsm(sra, ndata)) { |
c47b0ff6 AK |
10666 | dprintf("wait_for_reshape_imsm returned error!\n"); |
10667 | goto abort; | |
10668 | } | |
84d11e6c N |
10669 | if (sigterm) |
10670 | goto abort; | |
10f22854 | 10671 | |
0228d92c AK |
10672 | if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL) == 1) { |
10673 | /* ignore error == 2, this can mean end of reshape here | |
10674 | */ | |
7a862a02 | 10675 | dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL)\n"); |
10f22854 AK |
10676 | goto abort; |
10677 | } | |
10678 | ||
10679 | } | |
10680 | ||
71e5411e PB |
10681 | /* clear migr_rec on disks after successful migration */ |
10682 | struct dl *d; | |
10683 | ||
10684 | memset(super->migr_rec_buf, 0, MIGR_REC_BUF_SIZE); | |
10685 | for (d = super->disks; d; d = d->next) { | |
10686 | if (d->index < 0 || is_failed(&d->disk)) | |
10687 | continue; | |
10688 | unsigned long long dsize; | |
10689 | ||
10690 | get_dev_size(d->fd, NULL, &dsize); | |
10691 | if (lseek64(d->fd, dsize - MIGR_REC_POSITION, | |
10692 | SEEK_SET) >= 0) { | |
10693 | if (write(d->fd, super->migr_rec_buf, | |
10694 | MIGR_REC_BUF_SIZE) != MIGR_REC_BUF_SIZE) | |
10695 | perror("Write migr_rec failed"); | |
10696 | } | |
10697 | } | |
10698 | ||
10f22854 AK |
10699 | /* return '1' if done */ |
10700 | ret_val = 1; | |
10701 | abort: | |
10702 | free(buf); | |
942e1cdb N |
10703 | /* See Grow.c: abort_reshape() for further explanation */ |
10704 | sysfs_set_num(sra, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL); | |
10705 | sysfs_set_num(sra, NULL, "suspend_hi", 0); | |
10706 | sysfs_set_num(sra, NULL, "suspend_lo", 0); | |
10f22854 AK |
10707 | |
10708 | return ret_val; | |
999b4972 | 10709 | } |
0c21b485 | 10710 | |
71204a50 | 10711 | #endif /* MDASSEMBLE */ |
999b4972 | 10712 | |
cdddbdbc DW |
10713 | struct superswitch super_imsm = { |
10714 | #ifndef MDASSEMBLE | |
10715 | .examine_super = examine_super_imsm, | |
10716 | .brief_examine_super = brief_examine_super_imsm, | |
4737ae25 | 10717 | .brief_examine_subarrays = brief_examine_subarrays_imsm, |
9d84c8ea | 10718 | .export_examine_super = export_examine_super_imsm, |
cdddbdbc DW |
10719 | .detail_super = detail_super_imsm, |
10720 | .brief_detail_super = brief_detail_super_imsm, | |
bf5a934a | 10721 | .write_init_super = write_init_super_imsm, |
0e600426 N |
10722 | .validate_geometry = validate_geometry_imsm, |
10723 | .add_to_super = add_to_super_imsm, | |
1a64be56 | 10724 | .remove_from_super = remove_from_super_imsm, |
d665cc31 | 10725 | .detail_platform = detail_platform_imsm, |
e50cf220 | 10726 | .export_detail_platform = export_detail_platform_imsm, |
33414a01 | 10727 | .kill_subarray = kill_subarray_imsm, |
aa534678 | 10728 | .update_subarray = update_subarray_imsm, |
2b959fbf | 10729 | .load_container = load_container_imsm, |
71204a50 N |
10730 | .default_geometry = default_geometry_imsm, |
10731 | .get_disk_controller_domain = imsm_get_disk_controller_domain, | |
10732 | .reshape_super = imsm_reshape_super, | |
10733 | .manage_reshape = imsm_manage_reshape, | |
9e2d750d | 10734 | .recover_backup = recover_backup_imsm, |
74db60b0 | 10735 | .copy_metadata = copy_metadata_imsm, |
cdddbdbc DW |
10736 | #endif |
10737 | .match_home = match_home_imsm, | |
10738 | .uuid_from_super= uuid_from_super_imsm, | |
10739 | .getinfo_super = getinfo_super_imsm, | |
5c4cd5da | 10740 | .getinfo_super_disks = getinfo_super_disks_imsm, |
cdddbdbc DW |
10741 | .update_super = update_super_imsm, |
10742 | ||
10743 | .avail_size = avail_size_imsm, | |
80e7f8c3 | 10744 | .min_acceptable_spare_size = min_acceptable_spare_size_imsm, |
cdddbdbc DW |
10745 | |
10746 | .compare_super = compare_super_imsm, | |
10747 | ||
10748 | .load_super = load_super_imsm, | |
bf5a934a | 10749 | .init_super = init_super_imsm, |
e683ca88 | 10750 | .store_super = store_super_imsm, |
cdddbdbc DW |
10751 | .free_super = free_super_imsm, |
10752 | .match_metadata_desc = match_metadata_desc_imsm, | |
bf5a934a | 10753 | .container_content = container_content_imsm, |
0c21b485 | 10754 | .validate_container = validate_container_imsm, |
cdddbdbc | 10755 | |
cdddbdbc | 10756 | .external = 1, |
4cce4069 | 10757 | .name = "imsm", |
845dea95 | 10758 | |
0e600426 | 10759 | #ifndef MDASSEMBLE |
845dea95 NB |
10760 | /* for mdmon */ |
10761 | .open_new = imsm_open_new, | |
ed9d66aa | 10762 | .set_array_state= imsm_set_array_state, |
845dea95 NB |
10763 | .set_disk = imsm_set_disk, |
10764 | .sync_metadata = imsm_sync_metadata, | |
88758e9d | 10765 | .activate_spare = imsm_activate_spare, |
e8319a19 | 10766 | .process_update = imsm_process_update, |
8273f55e | 10767 | .prepare_update = imsm_prepare_update, |
0e600426 | 10768 | #endif /* MDASSEMBLE */ |
cdddbdbc | 10769 | }; |