]>
Commit | Line | Data |
---|---|---|
cdddbdbc DW |
1 | /* |
2 | * mdadm - Intel(R) Matrix Storage Manager Support | |
3 | * | |
a54d5262 | 4 | * Copyright (C) 2002-2008 Intel Corporation |
cdddbdbc DW |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify it | |
7 | * under the terms and conditions of the GNU General Public License, | |
8 | * version 2, as published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope it will be useful, but WITHOUT | |
11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
13 | * more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along with | |
16 | * this program; if not, write to the Free Software Foundation, Inc., | |
17 | * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. | |
18 | */ | |
19 | ||
51006d85 | 20 | #define HAVE_STDINT_H 1 |
cdddbdbc | 21 | #include "mdadm.h" |
c2a1e7da | 22 | #include "mdmon.h" |
51006d85 | 23 | #include "sha1.h" |
88c32bb1 | 24 | #include "platform-intel.h" |
cdddbdbc DW |
25 | #include <values.h> |
26 | #include <scsi/sg.h> | |
27 | #include <ctype.h> | |
d665cc31 | 28 | #include <dirent.h> |
cdddbdbc DW |
29 | |
30 | /* MPB == Metadata Parameter Block */ | |
31 | #define MPB_SIGNATURE "Intel Raid ISM Cfg Sig. " | |
32 | #define MPB_SIG_LEN (strlen(MPB_SIGNATURE)) | |
33 | #define MPB_VERSION_RAID0 "1.0.00" | |
34 | #define MPB_VERSION_RAID1 "1.1.00" | |
fe7ed8cb DW |
35 | #define MPB_VERSION_MANY_VOLUMES_PER_ARRAY "1.2.00" |
36 | #define MPB_VERSION_3OR4_DISK_ARRAY "1.2.01" | |
cdddbdbc | 37 | #define MPB_VERSION_RAID5 "1.2.02" |
fe7ed8cb DW |
38 | #define MPB_VERSION_5OR6_DISK_ARRAY "1.2.04" |
39 | #define MPB_VERSION_CNG "1.2.06" | |
40 | #define MPB_VERSION_ATTRIBS "1.3.00" | |
cdddbdbc DW |
41 | #define MAX_SIGNATURE_LENGTH 32 |
42 | #define MAX_RAID_SERIAL_LEN 16 | |
fe7ed8cb | 43 | |
19482bcc AK |
44 | /* supports RAID0 */ |
45 | #define MPB_ATTRIB_RAID0 __cpu_to_le32(0x00000001) | |
46 | /* supports RAID1 */ | |
47 | #define MPB_ATTRIB_RAID1 __cpu_to_le32(0x00000002) | |
48 | /* supports RAID10 */ | |
49 | #define MPB_ATTRIB_RAID10 __cpu_to_le32(0x00000004) | |
50 | /* supports RAID1E */ | |
51 | #define MPB_ATTRIB_RAID1E __cpu_to_le32(0x00000008) | |
52 | /* supports RAID5 */ | |
53 | #define MPB_ATTRIB_RAID5 __cpu_to_le32(0x00000010) | |
54 | /* supports RAID CNG */ | |
55 | #define MPB_ATTRIB_RAIDCNG __cpu_to_le32(0x00000020) | |
56 | /* supports expanded stripe sizes of 256K, 512K and 1MB */ | |
57 | #define MPB_ATTRIB_EXP_STRIPE_SIZE __cpu_to_le32(0x00000040) | |
58 | ||
59 | /* The OROM Support RST Caching of Volumes */ | |
60 | #define MPB_ATTRIB_NVM __cpu_to_le32(0x02000000) | |
61 | /* The OROM supports creating disks greater than 2TB */ | |
62 | #define MPB_ATTRIB_2TB_DISK __cpu_to_le32(0x04000000) | |
63 | /* The OROM supports Bad Block Management */ | |
64 | #define MPB_ATTRIB_BBM __cpu_to_le32(0x08000000) | |
65 | ||
66 | /* THe OROM Supports NVM Caching of Volumes */ | |
67 | #define MPB_ATTRIB_NEVER_USE2 __cpu_to_le32(0x10000000) | |
68 | /* The OROM supports creating volumes greater than 2TB */ | |
69 | #define MPB_ATTRIB_2TB __cpu_to_le32(0x20000000) | |
70 | /* originally for PMP, now it's wasted b/c. Never use this bit! */ | |
71 | #define MPB_ATTRIB_NEVER_USE __cpu_to_le32(0x40000000) | |
72 | /* Verify MPB contents against checksum after reading MPB */ | |
73 | #define MPB_ATTRIB_CHECKSUM_VERIFY __cpu_to_le32(0x80000000) | |
74 | ||
75 | /* Define all supported attributes that have to be accepted by mdadm | |
76 | */ | |
418f9b36 | 77 | #define MPB_ATTRIB_SUPPORTED (MPB_ATTRIB_CHECKSUM_VERIFY | \ |
19482bcc AK |
78 | MPB_ATTRIB_2TB | \ |
79 | MPB_ATTRIB_2TB_DISK | \ | |
80 | MPB_ATTRIB_RAID0 | \ | |
81 | MPB_ATTRIB_RAID1 | \ | |
82 | MPB_ATTRIB_RAID10 | \ | |
83 | MPB_ATTRIB_RAID5 | \ | |
bbab0940 TM |
84 | MPB_ATTRIB_EXP_STRIPE_SIZE | \ |
85 | MPB_ATTRIB_BBM) | |
418f9b36 N |
86 | |
87 | /* Define attributes that are unused but not harmful */ | |
88 | #define MPB_ATTRIB_IGNORED (MPB_ATTRIB_NEVER_USE) | |
fe7ed8cb | 89 | |
8e59f3d8 | 90 | #define MPB_SECTOR_CNT 2210 |
c2c087e6 | 91 | #define IMSM_RESERVED_SECTORS 4096 |
b81221b7 | 92 | #define NUM_BLOCKS_DIRTY_STRIPE_REGION 2056 |
979d38be | 93 | #define SECT_PER_MB_SHIFT 11 |
f36a9ecd | 94 | #define MAX_SECTOR_SIZE 4096 |
cdddbdbc DW |
95 | |
96 | /* Disk configuration info. */ | |
97 | #define IMSM_MAX_DEVICES 255 | |
98 | struct imsm_disk { | |
99 | __u8 serial[MAX_RAID_SERIAL_LEN];/* 0xD8 - 0xE7 ascii serial number */ | |
5551b113 | 100 | __u32 total_blocks_lo; /* 0xE8 - 0xEB total blocks lo */ |
cdddbdbc | 101 | __u32 scsi_id; /* 0xEC - 0xEF scsi ID */ |
f2f27e63 DW |
102 | #define SPARE_DISK __cpu_to_le32(0x01) /* Spare */ |
103 | #define CONFIGURED_DISK __cpu_to_le32(0x02) /* Member of some RaidDev */ | |
104 | #define FAILED_DISK __cpu_to_le32(0x04) /* Permanent failure */ | |
cdddbdbc | 105 | __u32 status; /* 0xF0 - 0xF3 */ |
1011e834 | 106 | __u32 owner_cfg_num; /* which config 0,1,2... owns this disk */ |
5551b113 CA |
107 | __u32 total_blocks_hi; /* 0xF4 - 0xF5 total blocks hi */ |
108 | #define IMSM_DISK_FILLERS 3 | |
109 | __u32 filler[IMSM_DISK_FILLERS]; /* 0xF5 - 0x107 MPB_DISK_FILLERS for future expansion */ | |
cdddbdbc DW |
110 | }; |
111 | ||
3b451610 AK |
112 | /* map selector for map managment |
113 | */ | |
238c0a71 AK |
114 | #define MAP_0 0 |
115 | #define MAP_1 1 | |
116 | #define MAP_X -1 | |
3b451610 | 117 | |
cdddbdbc DW |
118 | /* RAID map configuration infos. */ |
119 | struct imsm_map { | |
5551b113 CA |
120 | __u32 pba_of_lba0_lo; /* start address of partition */ |
121 | __u32 blocks_per_member_lo;/* blocks per member */ | |
122 | __u32 num_data_stripes_lo; /* number of data stripes */ | |
cdddbdbc DW |
123 | __u16 blocks_per_strip; |
124 | __u8 map_state; /* Normal, Uninitialized, Degraded, Failed */ | |
125 | #define IMSM_T_STATE_NORMAL 0 | |
126 | #define IMSM_T_STATE_UNINITIALIZED 1 | |
e3bba0e0 DW |
127 | #define IMSM_T_STATE_DEGRADED 2 |
128 | #define IMSM_T_STATE_FAILED 3 | |
cdddbdbc DW |
129 | __u8 raid_level; |
130 | #define IMSM_T_RAID0 0 | |
131 | #define IMSM_T_RAID1 1 | |
132 | #define IMSM_T_RAID5 5 /* since metadata version 1.2.02 ? */ | |
133 | __u8 num_members; /* number of member disks */ | |
fe7ed8cb DW |
134 | __u8 num_domains; /* number of parity domains */ |
135 | __u8 failed_disk_num; /* valid only when state is degraded */ | |
252d23c0 | 136 | __u8 ddf; |
5551b113 CA |
137 | __u32 pba_of_lba0_hi; |
138 | __u32 blocks_per_member_hi; | |
139 | __u32 num_data_stripes_hi; | |
140 | __u32 filler[4]; /* expansion area */ | |
7eef0453 | 141 | #define IMSM_ORD_REBUILD (1 << 24) |
cdddbdbc | 142 | __u32 disk_ord_tbl[1]; /* disk_ord_tbl[num_members], |
7eef0453 DW |
143 | * top byte contains some flags |
144 | */ | |
cdddbdbc DW |
145 | } __attribute__ ((packed)); |
146 | ||
147 | struct imsm_vol { | |
f8f603f1 | 148 | __u32 curr_migr_unit; |
fe7ed8cb | 149 | __u32 checkpoint_id; /* id to access curr_migr_unit */ |
cdddbdbc | 150 | __u8 migr_state; /* Normal or Migrating */ |
e3bba0e0 DW |
151 | #define MIGR_INIT 0 |
152 | #define MIGR_REBUILD 1 | |
153 | #define MIGR_VERIFY 2 /* analagous to echo check > sync_action */ | |
154 | #define MIGR_GEN_MIGR 3 | |
155 | #define MIGR_STATE_CHANGE 4 | |
1484e727 | 156 | #define MIGR_REPAIR 5 |
cdddbdbc DW |
157 | __u8 migr_type; /* Initializing, Rebuilding, ... */ |
158 | __u8 dirty; | |
fe7ed8cb DW |
159 | __u8 fs_state; /* fast-sync state for CnG (0xff == disabled) */ |
160 | __u16 verify_errors; /* number of mismatches */ | |
161 | __u16 bad_blocks; /* number of bad blocks during verify */ | |
162 | __u32 filler[4]; | |
cdddbdbc DW |
163 | struct imsm_map map[1]; |
164 | /* here comes another one if migr_state */ | |
165 | } __attribute__ ((packed)); | |
166 | ||
167 | struct imsm_dev { | |
fe7ed8cb | 168 | __u8 volume[MAX_RAID_SERIAL_LEN]; |
cdddbdbc DW |
169 | __u32 size_low; |
170 | __u32 size_high; | |
fe7ed8cb DW |
171 | #define DEV_BOOTABLE __cpu_to_le32(0x01) |
172 | #define DEV_BOOT_DEVICE __cpu_to_le32(0x02) | |
173 | #define DEV_READ_COALESCING __cpu_to_le32(0x04) | |
174 | #define DEV_WRITE_COALESCING __cpu_to_le32(0x08) | |
175 | #define DEV_LAST_SHUTDOWN_DIRTY __cpu_to_le32(0x10) | |
176 | #define DEV_HIDDEN_AT_BOOT __cpu_to_le32(0x20) | |
177 | #define DEV_CURRENTLY_HIDDEN __cpu_to_le32(0x40) | |
178 | #define DEV_VERIFY_AND_FIX __cpu_to_le32(0x80) | |
179 | #define DEV_MAP_STATE_UNINIT __cpu_to_le32(0x100) | |
180 | #define DEV_NO_AUTO_RECOVERY __cpu_to_le32(0x200) | |
181 | #define DEV_CLONE_N_GO __cpu_to_le32(0x400) | |
182 | #define DEV_CLONE_MAN_SYNC __cpu_to_le32(0x800) | |
183 | #define DEV_CNG_MASTER_DISK_NUM __cpu_to_le32(0x1000) | |
cdddbdbc DW |
184 | __u32 status; /* Persistent RaidDev status */ |
185 | __u32 reserved_blocks; /* Reserved blocks at beginning of volume */ | |
fe7ed8cb DW |
186 | __u8 migr_priority; |
187 | __u8 num_sub_vols; | |
188 | __u8 tid; | |
189 | __u8 cng_master_disk; | |
190 | __u16 cache_policy; | |
191 | __u8 cng_state; | |
192 | __u8 cng_sub_state; | |
193 | #define IMSM_DEV_FILLERS 10 | |
cdddbdbc DW |
194 | __u32 filler[IMSM_DEV_FILLERS]; |
195 | struct imsm_vol vol; | |
196 | } __attribute__ ((packed)); | |
197 | ||
198 | struct imsm_super { | |
199 | __u8 sig[MAX_SIGNATURE_LENGTH]; /* 0x00 - 0x1F */ | |
200 | __u32 check_sum; /* 0x20 - 0x23 MPB Checksum */ | |
201 | __u32 mpb_size; /* 0x24 - 0x27 Size of MPB */ | |
202 | __u32 family_num; /* 0x28 - 0x2B Checksum from first time this config was written */ | |
203 | __u32 generation_num; /* 0x2C - 0x2F Incremented each time this array's MPB is written */ | |
604b746f JD |
204 | __u32 error_log_size; /* 0x30 - 0x33 in bytes */ |
205 | __u32 attributes; /* 0x34 - 0x37 */ | |
cdddbdbc DW |
206 | __u8 num_disks; /* 0x38 Number of configured disks */ |
207 | __u8 num_raid_devs; /* 0x39 Number of configured volumes */ | |
604b746f JD |
208 | __u8 error_log_pos; /* 0x3A */ |
209 | __u8 fill[1]; /* 0x3B */ | |
210 | __u32 cache_size; /* 0x3c - 0x40 in mb */ | |
211 | __u32 orig_family_num; /* 0x40 - 0x43 original family num */ | |
212 | __u32 pwr_cycle_count; /* 0x44 - 0x47 simulated power cycle count for array */ | |
213 | __u32 bbm_log_size; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */ | |
214 | #define IMSM_FILLERS 35 | |
215 | __u32 filler[IMSM_FILLERS]; /* 0x4C - 0xD7 RAID_MPB_FILLERS */ | |
cdddbdbc DW |
216 | struct imsm_disk disk[1]; /* 0xD8 diskTbl[numDisks] */ |
217 | /* here comes imsm_dev[num_raid_devs] */ | |
604b746f | 218 | /* here comes BBM logs */ |
cdddbdbc DW |
219 | } __attribute__ ((packed)); |
220 | ||
604b746f | 221 | #define BBM_LOG_MAX_ENTRIES 254 |
8d67477f TM |
222 | #define BBM_LOG_MAX_LBA_ENTRY_VAL 256 /* Represents 256 LBAs */ |
223 | #define BBM_LOG_SIGNATURE 0xabadb10c | |
224 | ||
225 | struct bbm_log_block_addr { | |
226 | __u16 w1; | |
227 | __u32 dw1; | |
228 | } __attribute__ ((__packed__)); | |
604b746f JD |
229 | |
230 | struct bbm_log_entry { | |
8d67477f TM |
231 | __u8 marked_count; /* Number of blocks marked - 1 */ |
232 | __u8 disk_ordinal; /* Disk entry within the imsm_super */ | |
233 | struct bbm_log_block_addr defective_block_start; | |
604b746f JD |
234 | } __attribute__ ((__packed__)); |
235 | ||
236 | struct bbm_log { | |
237 | __u32 signature; /* 0xABADB10C */ | |
238 | __u32 entry_count; | |
8d67477f | 239 | struct bbm_log_entry marked_block_entries[BBM_LOG_MAX_ENTRIES]; |
604b746f JD |
240 | } __attribute__ ((__packed__)); |
241 | ||
cdddbdbc DW |
242 | #ifndef MDASSEMBLE |
243 | static char *map_state_str[] = { "normal", "uninitialized", "degraded", "failed" }; | |
244 | #endif | |
245 | ||
8e59f3d8 AK |
246 | #define RAID_DISK_RESERVED_BLOCKS_IMSM_HI 2209 |
247 | ||
248 | #define GEN_MIGR_AREA_SIZE 2048 /* General Migration Copy Area size in blocks */ | |
249 | ||
de44e46f PB |
250 | #define MIGR_REC_BUF_SECTORS 1 /* size of migr_record i/o buffer in sectors */ |
251 | #define MIGR_REC_SECTOR_POSITION 1 /* migr_record position offset on disk, | |
252 | * MIGR_REC_BUF_SECTORS <= MIGR_REC_SECTOR_POS | |
17a4eaf9 AK |
253 | */ |
254 | ||
8e59f3d8 AK |
255 | #define UNIT_SRC_NORMAL 0 /* Source data for curr_migr_unit must |
256 | * be recovered using srcMap */ | |
257 | #define UNIT_SRC_IN_CP_AREA 1 /* Source data for curr_migr_unit has | |
258 | * already been migrated and must | |
259 | * be recovered from checkpoint area */ | |
260 | struct migr_record { | |
261 | __u32 rec_status; /* Status used to determine how to restart | |
262 | * migration in case it aborts | |
263 | * in some fashion */ | |
264 | __u32 curr_migr_unit; /* 0..numMigrUnits-1 */ | |
265 | __u32 family_num; /* Family number of MPB | |
266 | * containing the RaidDev | |
267 | * that is migrating */ | |
268 | __u32 ascending_migr; /* True if migrating in increasing | |
269 | * order of lbas */ | |
270 | __u32 blocks_per_unit; /* Num disk blocks per unit of operation */ | |
271 | __u32 dest_depth_per_unit; /* Num member blocks each destMap | |
272 | * member disk | |
273 | * advances per unit-of-operation */ | |
274 | __u32 ckpt_area_pba; /* Pba of first block of ckpt copy area */ | |
275 | __u32 dest_1st_member_lba; /* First member lba on first | |
276 | * stripe of destination */ | |
277 | __u32 num_migr_units; /* Total num migration units-of-op */ | |
278 | __u32 post_migr_vol_cap; /* Size of volume after | |
279 | * migration completes */ | |
280 | __u32 post_migr_vol_cap_hi; /* Expansion space for LBA64 */ | |
281 | __u32 ckpt_read_disk_num; /* Which member disk in destSubMap[0] the | |
282 | * migration ckpt record was read from | |
283 | * (for recovered migrations) */ | |
284 | } __attribute__ ((__packed__)); | |
285 | ||
ec50f7b6 LM |
286 | struct md_list { |
287 | /* usage marker: | |
288 | * 1: load metadata | |
289 | * 2: metadata does not match | |
290 | * 4: already checked | |
291 | */ | |
292 | int used; | |
293 | char *devname; | |
294 | int found; | |
295 | int container; | |
296 | dev_t st_rdev; | |
297 | struct md_list *next; | |
298 | }; | |
299 | ||
e7b84f9d | 300 | #define pr_vrb(fmt, arg...) (void) (verbose && pr_err(fmt, ##arg)) |
ec50f7b6 | 301 | |
1484e727 DW |
302 | static __u8 migr_type(struct imsm_dev *dev) |
303 | { | |
304 | if (dev->vol.migr_type == MIGR_VERIFY && | |
305 | dev->status & DEV_VERIFY_AND_FIX) | |
306 | return MIGR_REPAIR; | |
307 | else | |
308 | return dev->vol.migr_type; | |
309 | } | |
310 | ||
311 | static void set_migr_type(struct imsm_dev *dev, __u8 migr_type) | |
312 | { | |
313 | /* for compatibility with older oroms convert MIGR_REPAIR, into | |
314 | * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status | |
315 | */ | |
316 | if (migr_type == MIGR_REPAIR) { | |
317 | dev->vol.migr_type = MIGR_VERIFY; | |
318 | dev->status |= DEV_VERIFY_AND_FIX; | |
319 | } else { | |
320 | dev->vol.migr_type = migr_type; | |
321 | dev->status &= ~DEV_VERIFY_AND_FIX; | |
322 | } | |
323 | } | |
324 | ||
f36a9ecd | 325 | static unsigned int sector_count(__u32 bytes, unsigned int sector_size) |
cdddbdbc | 326 | { |
f36a9ecd | 327 | return ROUND_UP(bytes, sector_size) / sector_size; |
87eb16df | 328 | } |
cdddbdbc | 329 | |
f36a9ecd PB |
330 | static unsigned int mpb_sectors(struct imsm_super *mpb, |
331 | unsigned int sector_size) | |
87eb16df | 332 | { |
f36a9ecd | 333 | return sector_count(__le32_to_cpu(mpb->mpb_size), sector_size); |
cdddbdbc DW |
334 | } |
335 | ||
ba2de7ba DW |
336 | struct intel_dev { |
337 | struct imsm_dev *dev; | |
338 | struct intel_dev *next; | |
f21e18ca | 339 | unsigned index; |
ba2de7ba DW |
340 | }; |
341 | ||
88654014 LM |
342 | struct intel_hba { |
343 | enum sys_dev_type type; | |
344 | char *path; | |
345 | char *pci_id; | |
346 | struct intel_hba *next; | |
347 | }; | |
348 | ||
1a64be56 LM |
349 | enum action { |
350 | DISK_REMOVE = 1, | |
351 | DISK_ADD | |
352 | }; | |
cdddbdbc DW |
353 | /* internal representation of IMSM metadata */ |
354 | struct intel_super { | |
355 | union { | |
949c47a0 DW |
356 | void *buf; /* O_DIRECT buffer for reading/writing metadata */ |
357 | struct imsm_super *anchor; /* immovable parameters */ | |
cdddbdbc | 358 | }; |
8e59f3d8 AK |
359 | union { |
360 | void *migr_rec_buf; /* buffer for I/O operations */ | |
361 | struct migr_record *migr_rec; /* migration record */ | |
362 | }; | |
51d83f5d AK |
363 | int clean_migration_record_by_mdmon; /* when reshape is switched to next |
364 | array, it indicates that mdmon is allowed to clean migration | |
365 | record */ | |
949c47a0 | 366 | size_t len; /* size of the 'buf' allocation */ |
bbab0940 | 367 | size_t extra_space; /* extra space in 'buf' that is not used yet */ |
4d7b1503 DW |
368 | void *next_buf; /* for realloc'ing buf from the manager */ |
369 | size_t next_len; | |
c2c087e6 | 370 | int updates_pending; /* count of pending updates for mdmon */ |
bf5a934a | 371 | int current_vol; /* index of raid device undergoing creation */ |
5551b113 | 372 | unsigned long long create_offset; /* common start for 'current_vol' */ |
148acb7b | 373 | __u32 random; /* random data for seeding new family numbers */ |
ba2de7ba | 374 | struct intel_dev *devlist; |
fa7bb6f8 | 375 | unsigned int sector_size; /* sector size of used member drives */ |
cdddbdbc DW |
376 | struct dl { |
377 | struct dl *next; | |
378 | int index; | |
379 | __u8 serial[MAX_RAID_SERIAL_LEN]; | |
380 | int major, minor; | |
381 | char *devname; | |
b9f594fe | 382 | struct imsm_disk disk; |
cdddbdbc | 383 | int fd; |
0dcecb2e DW |
384 | int extent_cnt; |
385 | struct extent *e; /* for determining freespace @ create */ | |
efb30e7f | 386 | int raiddisk; /* slot to fill in autolayout */ |
1a64be56 | 387 | enum action action; |
ca0748fa | 388 | } *disks, *current_disk; |
1a64be56 LM |
389 | struct dl *disk_mgmt_list; /* list of disks to add/remove while mdmon |
390 | active */ | |
47ee5a45 | 391 | struct dl *missing; /* disks removed while we weren't looking */ |
43dad3d6 | 392 | struct bbm_log *bbm_log; |
88654014 | 393 | struct intel_hba *hba; /* device path of the raid controller for this metadata */ |
88c32bb1 | 394 | const struct imsm_orom *orom; /* platform firmware support */ |
a2b97981 | 395 | struct intel_super *next; /* (temp) list for disambiguating family_num */ |
928f1424 | 396 | struct md_bb bb; /* memory for get_bad_blocks call */ |
a2b97981 DW |
397 | }; |
398 | ||
399 | struct intel_disk { | |
400 | struct imsm_disk disk; | |
401 | #define IMSM_UNKNOWN_OWNER (-1) | |
402 | int owner; | |
403 | struct intel_disk *next; | |
cdddbdbc DW |
404 | }; |
405 | ||
c2c087e6 DW |
406 | struct extent { |
407 | unsigned long long start, size; | |
408 | }; | |
409 | ||
694575e7 KW |
410 | /* definitions of reshape process types */ |
411 | enum imsm_reshape_type { | |
412 | CH_TAKEOVER, | |
b5347799 | 413 | CH_MIGRATION, |
7abc9871 | 414 | CH_ARRAY_SIZE, |
694575e7 KW |
415 | }; |
416 | ||
88758e9d DW |
417 | /* definition of messages passed to imsm_process_update */ |
418 | enum imsm_update_type { | |
419 | update_activate_spare, | |
8273f55e | 420 | update_create_array, |
33414a01 | 421 | update_kill_array, |
aa534678 | 422 | update_rename_array, |
1a64be56 | 423 | update_add_remove_disk, |
78b10e66 | 424 | update_reshape_container_disks, |
48c5303a | 425 | update_reshape_migration, |
2d40f3a1 AK |
426 | update_takeover, |
427 | update_general_migration_checkpoint, | |
f3871fdc | 428 | update_size_change, |
bbab0940 | 429 | update_prealloc_badblocks_mem, |
88758e9d DW |
430 | }; |
431 | ||
432 | struct imsm_update_activate_spare { | |
433 | enum imsm_update_type type; | |
d23fe947 | 434 | struct dl *dl; |
88758e9d DW |
435 | int slot; |
436 | int array; | |
437 | struct imsm_update_activate_spare *next; | |
438 | }; | |
439 | ||
78b10e66 | 440 | struct geo_params { |
4dd2df09 | 441 | char devnm[32]; |
78b10e66 | 442 | char *dev_name; |
d04f65f4 | 443 | unsigned long long size; |
78b10e66 N |
444 | int level; |
445 | int layout; | |
446 | int chunksize; | |
447 | int raid_disks; | |
448 | }; | |
449 | ||
bb025c2f KW |
450 | enum takeover_direction { |
451 | R10_TO_R0, | |
452 | R0_TO_R10 | |
453 | }; | |
454 | struct imsm_update_takeover { | |
455 | enum imsm_update_type type; | |
456 | int subarray; | |
457 | enum takeover_direction direction; | |
458 | }; | |
78b10e66 N |
459 | |
460 | struct imsm_update_reshape { | |
461 | enum imsm_update_type type; | |
462 | int old_raid_disks; | |
463 | int new_raid_disks; | |
48c5303a PC |
464 | |
465 | int new_disks[1]; /* new_raid_disks - old_raid_disks makedev number */ | |
466 | }; | |
467 | ||
468 | struct imsm_update_reshape_migration { | |
469 | enum imsm_update_type type; | |
470 | int old_raid_disks; | |
471 | int new_raid_disks; | |
472 | /* fields for array migration changes | |
473 | */ | |
474 | int subdev; | |
475 | int new_level; | |
476 | int new_layout; | |
4bba0439 | 477 | int new_chunksize; |
48c5303a | 478 | |
d195167d | 479 | int new_disks[1]; /* new_raid_disks - old_raid_disks makedev number */ |
78b10e66 N |
480 | }; |
481 | ||
f3871fdc AK |
482 | struct imsm_update_size_change { |
483 | enum imsm_update_type type; | |
484 | int subdev; | |
485 | long long new_size; | |
486 | }; | |
487 | ||
2d40f3a1 AK |
488 | struct imsm_update_general_migration_checkpoint { |
489 | enum imsm_update_type type; | |
490 | __u32 curr_migr_unit; | |
491 | }; | |
492 | ||
54c2c1ea DW |
493 | struct disk_info { |
494 | __u8 serial[MAX_RAID_SERIAL_LEN]; | |
495 | }; | |
496 | ||
8273f55e DW |
497 | struct imsm_update_create_array { |
498 | enum imsm_update_type type; | |
8273f55e | 499 | int dev_idx; |
6a3e913e | 500 | struct imsm_dev dev; |
8273f55e DW |
501 | }; |
502 | ||
33414a01 DW |
503 | struct imsm_update_kill_array { |
504 | enum imsm_update_type type; | |
505 | int dev_idx; | |
506 | }; | |
507 | ||
aa534678 DW |
508 | struct imsm_update_rename_array { |
509 | enum imsm_update_type type; | |
510 | __u8 name[MAX_RAID_SERIAL_LEN]; | |
511 | int dev_idx; | |
512 | }; | |
513 | ||
1a64be56 | 514 | struct imsm_update_add_remove_disk { |
43dad3d6 DW |
515 | enum imsm_update_type type; |
516 | }; | |
517 | ||
bbab0940 TM |
518 | struct imsm_update_prealloc_bb_mem { |
519 | enum imsm_update_type type; | |
520 | }; | |
521 | ||
88654014 LM |
522 | static const char *_sys_dev_type[] = { |
523 | [SYS_DEV_UNKNOWN] = "Unknown", | |
524 | [SYS_DEV_SAS] = "SAS", | |
614902f6 | 525 | [SYS_DEV_SATA] = "SATA", |
60f0f54d PB |
526 | [SYS_DEV_NVME] = "NVMe", |
527 | [SYS_DEV_VMD] = "VMD" | |
88654014 LM |
528 | }; |
529 | ||
530 | const char *get_sys_dev_type(enum sys_dev_type type) | |
531 | { | |
532 | if (type >= SYS_DEV_MAX) | |
533 | type = SYS_DEV_UNKNOWN; | |
534 | ||
535 | return _sys_dev_type[type]; | |
536 | } | |
537 | ||
538 | static struct intel_hba * alloc_intel_hba(struct sys_dev *device) | |
539 | { | |
503975b9 N |
540 | struct intel_hba *result = xmalloc(sizeof(*result)); |
541 | ||
542 | result->type = device->type; | |
543 | result->path = xstrdup(device->path); | |
544 | result->next = NULL; | |
545 | if (result->path && (result->pci_id = strrchr(result->path, '/')) != NULL) | |
546 | result->pci_id++; | |
547 | ||
88654014 LM |
548 | return result; |
549 | } | |
550 | ||
551 | static struct intel_hba * find_intel_hba(struct intel_hba *hba, struct sys_dev *device) | |
552 | { | |
594dc1b8 JS |
553 | struct intel_hba *result; |
554 | ||
88654014 LM |
555 | for (result = hba; result; result = result->next) { |
556 | if (result->type == device->type && strcmp(result->path, device->path) == 0) | |
557 | break; | |
558 | } | |
559 | return result; | |
560 | } | |
561 | ||
b4cf4cba | 562 | static int attach_hba_to_super(struct intel_super *super, struct sys_dev *device) |
88654014 LM |
563 | { |
564 | struct intel_hba *hba; | |
565 | ||
566 | /* check if disk attached to Intel HBA */ | |
567 | hba = find_intel_hba(super->hba, device); | |
568 | if (hba != NULL) | |
569 | return 1; | |
570 | /* Check if HBA is already attached to super */ | |
571 | if (super->hba == NULL) { | |
572 | super->hba = alloc_intel_hba(device); | |
573 | return 1; | |
6b781d33 AP |
574 | } |
575 | ||
576 | hba = super->hba; | |
577 | /* Intel metadata allows for all disks attached to the same type HBA. | |
614902f6 | 578 | * Do not support HBA types mixing |
6b781d33 AP |
579 | */ |
580 | if (device->type != hba->type) | |
88654014 | 581 | return 2; |
6b781d33 AP |
582 | |
583 | /* Multiple same type HBAs can be used if they share the same OROM */ | |
584 | const struct imsm_orom *device_orom = get_orom_by_device_id(device->dev_id); | |
585 | ||
586 | if (device_orom != super->orom) | |
587 | return 2; | |
588 | ||
589 | while (hba->next) | |
590 | hba = hba->next; | |
591 | ||
592 | hba->next = alloc_intel_hba(device); | |
593 | return 1; | |
88654014 LM |
594 | } |
595 | ||
596 | static struct sys_dev* find_disk_attached_hba(int fd, const char *devname) | |
597 | { | |
9bc4ae77 | 598 | struct sys_dev *list, *elem; |
88654014 LM |
599 | char *disk_path; |
600 | ||
601 | if ((list = find_intel_devices()) == NULL) | |
602 | return 0; | |
603 | ||
604 | if (fd < 0) | |
605 | disk_path = (char *) devname; | |
606 | else | |
607 | disk_path = diskfd_to_devpath(fd); | |
608 | ||
9bc4ae77 | 609 | if (!disk_path) |
88654014 | 610 | return 0; |
88654014 | 611 | |
9bc4ae77 N |
612 | for (elem = list; elem; elem = elem->next) |
613 | if (path_attached_to_hba(disk_path, elem->path)) | |
88654014 | 614 | return elem; |
9bc4ae77 | 615 | |
88654014 LM |
616 | if (disk_path != devname) |
617 | free(disk_path); | |
88654014 LM |
618 | |
619 | return NULL; | |
620 | } | |
621 | ||
d424212e N |
622 | static int find_intel_hba_capability(int fd, struct intel_super *super, |
623 | char *devname); | |
f2f5c343 | 624 | |
cdddbdbc DW |
625 | static struct supertype *match_metadata_desc_imsm(char *arg) |
626 | { | |
627 | struct supertype *st; | |
628 | ||
629 | if (strcmp(arg, "imsm") != 0 && | |
630 | strcmp(arg, "default") != 0 | |
631 | ) | |
632 | return NULL; | |
633 | ||
503975b9 | 634 | st = xcalloc(1, sizeof(*st)); |
cdddbdbc DW |
635 | st->ss = &super_imsm; |
636 | st->max_devs = IMSM_MAX_DEVICES; | |
637 | st->minor_version = 0; | |
638 | st->sb = NULL; | |
639 | return st; | |
640 | } | |
641 | ||
0e600426 | 642 | #ifndef MDASSEMBLE |
cdddbdbc DW |
643 | static __u8 *get_imsm_version(struct imsm_super *mpb) |
644 | { | |
645 | return &mpb->sig[MPB_SIG_LEN]; | |
646 | } | |
9e2d750d | 647 | #endif |
cdddbdbc | 648 | |
949c47a0 DW |
649 | /* retrieve a disk directly from the anchor when the anchor is known to be |
650 | * up-to-date, currently only at load time | |
651 | */ | |
652 | static struct imsm_disk *__get_imsm_disk(struct imsm_super *mpb, __u8 index) | |
cdddbdbc | 653 | { |
949c47a0 | 654 | if (index >= mpb->num_disks) |
cdddbdbc DW |
655 | return NULL; |
656 | return &mpb->disk[index]; | |
657 | } | |
658 | ||
95d07a2c LM |
659 | /* retrieve the disk description based on a index of the disk |
660 | * in the sub-array | |
661 | */ | |
662 | static struct dl *get_imsm_dl_disk(struct intel_super *super, __u8 index) | |
949c47a0 | 663 | { |
b9f594fe DW |
664 | struct dl *d; |
665 | ||
666 | for (d = super->disks; d; d = d->next) | |
667 | if (d->index == index) | |
95d07a2c LM |
668 | return d; |
669 | ||
670 | return NULL; | |
671 | } | |
672 | /* retrieve a disk from the parsed metadata */ | |
673 | static struct imsm_disk *get_imsm_disk(struct intel_super *super, __u8 index) | |
674 | { | |
675 | struct dl *dl; | |
676 | ||
677 | dl = get_imsm_dl_disk(super, index); | |
678 | if (dl) | |
679 | return &dl->disk; | |
680 | ||
b9f594fe | 681 | return NULL; |
949c47a0 DW |
682 | } |
683 | ||
684 | /* generate a checksum directly from the anchor when the anchor is known to be | |
685 | * up-to-date, currently only at load or write_super after coalescing | |
686 | */ | |
687 | static __u32 __gen_imsm_checksum(struct imsm_super *mpb) | |
cdddbdbc DW |
688 | { |
689 | __u32 end = mpb->mpb_size / sizeof(end); | |
690 | __u32 *p = (__u32 *) mpb; | |
691 | __u32 sum = 0; | |
692 | ||
5d500228 N |
693 | while (end--) { |
694 | sum += __le32_to_cpu(*p); | |
97f734fd N |
695 | p++; |
696 | } | |
cdddbdbc | 697 | |
5d500228 | 698 | return sum - __le32_to_cpu(mpb->check_sum); |
cdddbdbc DW |
699 | } |
700 | ||
a965f303 DW |
701 | static size_t sizeof_imsm_map(struct imsm_map *map) |
702 | { | |
703 | return sizeof(struct imsm_map) + sizeof(__u32) * (map->num_members - 1); | |
704 | } | |
705 | ||
706 | struct imsm_map *get_imsm_map(struct imsm_dev *dev, int second_map) | |
cdddbdbc | 707 | { |
5e7b0330 AK |
708 | /* A device can have 2 maps if it is in the middle of a migration. |
709 | * If second_map is: | |
238c0a71 AK |
710 | * MAP_0 - we return the first map |
711 | * MAP_1 - we return the second map if it exists, else NULL | |
712 | * MAP_X - we return the second map if it exists, else the first | |
5e7b0330 | 713 | */ |
a965f303 | 714 | struct imsm_map *map = &dev->vol.map[0]; |
9535fc47 | 715 | struct imsm_map *map2 = NULL; |
a965f303 | 716 | |
9535fc47 AK |
717 | if (dev->vol.migr_state) |
718 | map2 = (void *)map + sizeof_imsm_map(map); | |
a965f303 | 719 | |
9535fc47 | 720 | switch (second_map) { |
3b451610 | 721 | case MAP_0: |
9535fc47 | 722 | break; |
3b451610 | 723 | case MAP_1: |
9535fc47 AK |
724 | map = map2; |
725 | break; | |
238c0a71 | 726 | case MAP_X: |
9535fc47 AK |
727 | if (map2) |
728 | map = map2; | |
729 | break; | |
9535fc47 AK |
730 | default: |
731 | map = NULL; | |
732 | } | |
733 | return map; | |
5e7b0330 | 734 | |
a965f303 | 735 | } |
cdddbdbc | 736 | |
3393c6af DW |
737 | /* return the size of the device. |
738 | * migr_state increases the returned size if map[0] were to be duplicated | |
739 | */ | |
740 | static size_t sizeof_imsm_dev(struct imsm_dev *dev, int migr_state) | |
a965f303 DW |
741 | { |
742 | size_t size = sizeof(*dev) - sizeof(struct imsm_map) + | |
238c0a71 | 743 | sizeof_imsm_map(get_imsm_map(dev, MAP_0)); |
cdddbdbc DW |
744 | |
745 | /* migrating means an additional map */ | |
a965f303 | 746 | if (dev->vol.migr_state) |
238c0a71 | 747 | size += sizeof_imsm_map(get_imsm_map(dev, MAP_1)); |
3393c6af | 748 | else if (migr_state) |
238c0a71 | 749 | size += sizeof_imsm_map(get_imsm_map(dev, MAP_0)); |
cdddbdbc DW |
750 | |
751 | return size; | |
752 | } | |
753 | ||
54c2c1ea DW |
754 | #ifndef MDASSEMBLE |
755 | /* retrieve disk serial number list from a metadata update */ | |
756 | static struct disk_info *get_disk_info(struct imsm_update_create_array *update) | |
757 | { | |
758 | void *u = update; | |
759 | struct disk_info *inf; | |
760 | ||
761 | inf = u + sizeof(*update) - sizeof(struct imsm_dev) + | |
762 | sizeof_imsm_dev(&update->dev, 0); | |
763 | ||
764 | return inf; | |
765 | } | |
766 | #endif | |
767 | ||
949c47a0 | 768 | static struct imsm_dev *__get_imsm_dev(struct imsm_super *mpb, __u8 index) |
cdddbdbc DW |
769 | { |
770 | int offset; | |
771 | int i; | |
772 | void *_mpb = mpb; | |
773 | ||
949c47a0 | 774 | if (index >= mpb->num_raid_devs) |
cdddbdbc DW |
775 | return NULL; |
776 | ||
777 | /* devices start after all disks */ | |
778 | offset = ((void *) &mpb->disk[mpb->num_disks]) - _mpb; | |
779 | ||
780 | for (i = 0; i <= index; i++) | |
781 | if (i == index) | |
782 | return _mpb + offset; | |
783 | else | |
3393c6af | 784 | offset += sizeof_imsm_dev(_mpb + offset, 0); |
cdddbdbc DW |
785 | |
786 | return NULL; | |
787 | } | |
788 | ||
949c47a0 DW |
789 | static struct imsm_dev *get_imsm_dev(struct intel_super *super, __u8 index) |
790 | { | |
ba2de7ba DW |
791 | struct intel_dev *dv; |
792 | ||
949c47a0 DW |
793 | if (index >= super->anchor->num_raid_devs) |
794 | return NULL; | |
ba2de7ba DW |
795 | for (dv = super->devlist; dv; dv = dv->next) |
796 | if (dv->index == index) | |
797 | return dv->dev; | |
798 | return NULL; | |
949c47a0 DW |
799 | } |
800 | ||
8d67477f TM |
801 | static inline unsigned long long __le48_to_cpu(const struct bbm_log_block_addr |
802 | *addr) | |
803 | { | |
804 | return ((((__u64)__le32_to_cpu(addr->dw1)) << 16) | | |
805 | __le16_to_cpu(addr->w1)); | |
806 | } | |
807 | ||
808 | static inline struct bbm_log_block_addr __cpu_to_le48(unsigned long long sec) | |
809 | { | |
810 | struct bbm_log_block_addr addr; | |
811 | ||
812 | addr.w1 = __cpu_to_le16((__u16)(sec & 0xffff)); | |
813 | addr.dw1 = __cpu_to_le32((__u32)(sec >> 16) & 0xffffffff); | |
814 | return addr; | |
815 | } | |
816 | ||
817 | #ifndef MDASSEMBLE | |
818 | /* get size of the bbm log */ | |
819 | static __u32 get_imsm_bbm_log_size(struct bbm_log *log) | |
820 | { | |
821 | if (!log || log->entry_count == 0) | |
822 | return 0; | |
823 | ||
824 | return sizeof(log->signature) + | |
825 | sizeof(log->entry_count) + | |
826 | log->entry_count * sizeof(struct bbm_log_entry); | |
827 | } | |
6f50473f TM |
828 | |
829 | /* check if bad block is not partially stored in bbm log */ | |
830 | static int is_stored_in_bbm(struct bbm_log *log, const __u8 idx, const unsigned | |
831 | long long sector, const int length, __u32 *pos) | |
832 | { | |
833 | __u32 i; | |
834 | ||
835 | for (i = *pos; i < log->entry_count; i++) { | |
836 | struct bbm_log_entry *entry = &log->marked_block_entries[i]; | |
837 | unsigned long long bb_start; | |
838 | unsigned long long bb_end; | |
839 | ||
840 | bb_start = __le48_to_cpu(&entry->defective_block_start); | |
841 | bb_end = bb_start + (entry->marked_count + 1); | |
842 | ||
843 | if ((entry->disk_ordinal == idx) && (bb_start >= sector) && | |
844 | (bb_end <= sector + length)) { | |
845 | *pos = i; | |
846 | return 1; | |
847 | } | |
848 | } | |
849 | return 0; | |
850 | } | |
851 | ||
852 | /* record new bad block in bbm log */ | |
853 | static int record_new_badblock(struct bbm_log *log, const __u8 idx, unsigned | |
854 | long long sector, int length) | |
855 | { | |
856 | int new_bb = 0; | |
857 | __u32 pos = 0; | |
858 | struct bbm_log_entry *entry = NULL; | |
859 | ||
860 | while (is_stored_in_bbm(log, idx, sector, length, &pos)) { | |
861 | struct bbm_log_entry *e = &log->marked_block_entries[pos]; | |
862 | ||
863 | if ((e->marked_count + 1 == BBM_LOG_MAX_LBA_ENTRY_VAL) && | |
864 | (__le48_to_cpu(&e->defective_block_start) == sector)) { | |
865 | sector += BBM_LOG_MAX_LBA_ENTRY_VAL; | |
866 | length -= BBM_LOG_MAX_LBA_ENTRY_VAL; | |
867 | pos = pos + 1; | |
868 | continue; | |
869 | } | |
870 | entry = e; | |
871 | break; | |
872 | } | |
873 | ||
874 | if (entry) { | |
875 | int cnt = (length <= BBM_LOG_MAX_LBA_ENTRY_VAL) ? length : | |
876 | BBM_LOG_MAX_LBA_ENTRY_VAL; | |
877 | entry->defective_block_start = __cpu_to_le48(sector); | |
878 | entry->marked_count = cnt - 1; | |
879 | if (cnt == length) | |
880 | return 1; | |
881 | sector += cnt; | |
882 | length -= cnt; | |
883 | } | |
884 | ||
885 | new_bb = ROUND_UP(length, BBM_LOG_MAX_LBA_ENTRY_VAL) / | |
886 | BBM_LOG_MAX_LBA_ENTRY_VAL; | |
887 | if (log->entry_count + new_bb > BBM_LOG_MAX_ENTRIES) | |
888 | return 0; | |
889 | ||
890 | while (length > 0) { | |
891 | int cnt = (length <= BBM_LOG_MAX_LBA_ENTRY_VAL) ? length : | |
892 | BBM_LOG_MAX_LBA_ENTRY_VAL; | |
893 | struct bbm_log_entry *entry = | |
894 | &log->marked_block_entries[log->entry_count]; | |
895 | ||
896 | entry->defective_block_start = __cpu_to_le48(sector); | |
897 | entry->marked_count = cnt - 1; | |
898 | entry->disk_ordinal = idx; | |
899 | ||
900 | sector += cnt; | |
901 | length -= cnt; | |
902 | ||
903 | log->entry_count++; | |
904 | } | |
905 | ||
906 | return new_bb; | |
907 | } | |
c07a5a4f | 908 | |
4c9e8c1e TM |
909 | /* clear all bad blocks for given disk */ |
910 | static void clear_disk_badblocks(struct bbm_log *log, const __u8 idx) | |
911 | { | |
912 | __u32 i = 0; | |
913 | ||
914 | while (i < log->entry_count) { | |
915 | struct bbm_log_entry *entries = log->marked_block_entries; | |
916 | ||
917 | if (entries[i].disk_ordinal == idx) { | |
918 | if (i < log->entry_count - 1) | |
919 | entries[i] = entries[log->entry_count - 1]; | |
920 | log->entry_count--; | |
921 | } else { | |
922 | i++; | |
923 | } | |
924 | } | |
925 | } | |
926 | ||
c07a5a4f TM |
927 | /* clear given bad block */ |
928 | static int clear_badblock(struct bbm_log *log, const __u8 idx, const unsigned | |
929 | long long sector, const int length) { | |
930 | __u32 i = 0; | |
931 | ||
932 | while (i < log->entry_count) { | |
933 | struct bbm_log_entry *entries = log->marked_block_entries; | |
934 | ||
935 | if ((entries[i].disk_ordinal == idx) && | |
936 | (__le48_to_cpu(&entries[i].defective_block_start) == | |
937 | sector) && (entries[i].marked_count + 1 == length)) { | |
938 | if (i < log->entry_count - 1) | |
939 | entries[i] = entries[log->entry_count - 1]; | |
940 | log->entry_count--; | |
941 | break; | |
942 | } | |
943 | i++; | |
944 | } | |
945 | ||
946 | return 1; | |
947 | } | |
8d67477f TM |
948 | #endif /* MDASSEMBLE */ |
949 | ||
950 | /* allocate and load BBM log from metadata */ | |
951 | static int load_bbm_log(struct intel_super *super) | |
952 | { | |
953 | struct imsm_super *mpb = super->anchor; | |
954 | __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size); | |
955 | ||
956 | super->bbm_log = xcalloc(1, sizeof(struct bbm_log)); | |
957 | if (!super->bbm_log) | |
958 | return 1; | |
959 | ||
960 | if (bbm_log_size) { | |
961 | struct bbm_log *log = (void *)mpb + | |
962 | __le32_to_cpu(mpb->mpb_size) - bbm_log_size; | |
963 | ||
964 | __u32 entry_count; | |
965 | ||
966 | if (bbm_log_size < sizeof(log->signature) + | |
967 | sizeof(log->entry_count)) | |
968 | return 2; | |
969 | ||
970 | entry_count = __le32_to_cpu(log->entry_count); | |
971 | if ((__le32_to_cpu(log->signature) != BBM_LOG_SIGNATURE) || | |
972 | (entry_count > BBM_LOG_MAX_ENTRIES)) | |
973 | return 3; | |
974 | ||
975 | if (bbm_log_size != | |
976 | sizeof(log->signature) + sizeof(log->entry_count) + | |
977 | entry_count * sizeof(struct bbm_log_entry)) | |
978 | return 4; | |
979 | ||
980 | memcpy(super->bbm_log, log, bbm_log_size); | |
981 | } else { | |
982 | super->bbm_log->signature = __cpu_to_le32(BBM_LOG_SIGNATURE); | |
983 | super->bbm_log->entry_count = 0; | |
984 | } | |
985 | ||
986 | return 0; | |
987 | } | |
988 | ||
b12796be TM |
989 | /* checks if bad block is within volume boundaries */ |
990 | static int is_bad_block_in_volume(const struct bbm_log_entry *entry, | |
991 | const unsigned long long start_sector, | |
992 | const unsigned long long size) | |
993 | { | |
994 | unsigned long long bb_start; | |
995 | unsigned long long bb_end; | |
996 | ||
997 | bb_start = __le48_to_cpu(&entry->defective_block_start); | |
998 | bb_end = bb_start + (entry->marked_count + 1); | |
999 | ||
1000 | if (((bb_start >= start_sector) && (bb_start < start_sector + size)) || | |
1001 | ((bb_end >= start_sector) && (bb_end <= start_sector + size))) | |
1002 | return 1; | |
1003 | ||
1004 | return 0; | |
1005 | } | |
1006 | ||
1007 | /* get list of bad blocks on a drive for a volume */ | |
1008 | static void get_volume_badblocks(const struct bbm_log *log, const __u8 idx, | |
1009 | const unsigned long long start_sector, | |
1010 | const unsigned long long size, | |
1011 | struct md_bb *bbs) | |
1012 | { | |
1013 | __u32 count = 0; | |
1014 | __u32 i; | |
1015 | ||
1016 | for (i = 0; i < log->entry_count; i++) { | |
1017 | const struct bbm_log_entry *ent = | |
1018 | &log->marked_block_entries[i]; | |
1019 | struct md_bb_entry *bb; | |
1020 | ||
1021 | if ((ent->disk_ordinal == idx) && | |
1022 | is_bad_block_in_volume(ent, start_sector, size)) { | |
1023 | ||
1024 | if (!bbs->entries) { | |
1025 | bbs->entries = xmalloc(BBM_LOG_MAX_ENTRIES * | |
1026 | sizeof(*bb)); | |
1027 | if (!bbs->entries) | |
1028 | break; | |
1029 | } | |
1030 | ||
1031 | bb = &bbs->entries[count++]; | |
1032 | bb->sector = __le48_to_cpu(&ent->defective_block_start); | |
1033 | bb->length = ent->marked_count + 1; | |
1034 | } | |
1035 | } | |
1036 | bbs->count = count; | |
1037 | } | |
1038 | ||
98130f40 AK |
1039 | /* |
1040 | * for second_map: | |
238c0a71 AK |
1041 | * == MAP_0 get first map |
1042 | * == MAP_1 get second map | |
1043 | * == MAP_X than get map according to the current migr_state | |
98130f40 AK |
1044 | */ |
1045 | static __u32 get_imsm_ord_tbl_ent(struct imsm_dev *dev, | |
1046 | int slot, | |
1047 | int second_map) | |
7eef0453 DW |
1048 | { |
1049 | struct imsm_map *map; | |
1050 | ||
5e7b0330 | 1051 | map = get_imsm_map(dev, second_map); |
7eef0453 | 1052 | |
ff077194 DW |
1053 | /* top byte identifies disk under rebuild */ |
1054 | return __le32_to_cpu(map->disk_ord_tbl[slot]); | |
1055 | } | |
1056 | ||
1057 | #define ord_to_idx(ord) (((ord) << 8) >> 8) | |
98130f40 | 1058 | static __u32 get_imsm_disk_idx(struct imsm_dev *dev, int slot, int second_map) |
ff077194 | 1059 | { |
98130f40 | 1060 | __u32 ord = get_imsm_ord_tbl_ent(dev, slot, second_map); |
ff077194 DW |
1061 | |
1062 | return ord_to_idx(ord); | |
7eef0453 DW |
1063 | } |
1064 | ||
be73972f DW |
1065 | static void set_imsm_ord_tbl_ent(struct imsm_map *map, int slot, __u32 ord) |
1066 | { | |
1067 | map->disk_ord_tbl[slot] = __cpu_to_le32(ord); | |
1068 | } | |
1069 | ||
f21e18ca | 1070 | static int get_imsm_disk_slot(struct imsm_map *map, unsigned idx) |
620b1713 DW |
1071 | { |
1072 | int slot; | |
1073 | __u32 ord; | |
1074 | ||
1075 | for (slot = 0; slot < map->num_members; slot++) { | |
1076 | ord = __le32_to_cpu(map->disk_ord_tbl[slot]); | |
1077 | if (ord_to_idx(ord) == idx) | |
1078 | return slot; | |
1079 | } | |
1080 | ||
1081 | return -1; | |
1082 | } | |
1083 | ||
cdddbdbc DW |
1084 | static int get_imsm_raid_level(struct imsm_map *map) |
1085 | { | |
1086 | if (map->raid_level == 1) { | |
1087 | if (map->num_members == 2) | |
1088 | return 1; | |
1089 | else | |
1090 | return 10; | |
1091 | } | |
1092 | ||
1093 | return map->raid_level; | |
1094 | } | |
1095 | ||
c2c087e6 DW |
1096 | static int cmp_extent(const void *av, const void *bv) |
1097 | { | |
1098 | const struct extent *a = av; | |
1099 | const struct extent *b = bv; | |
1100 | if (a->start < b->start) | |
1101 | return -1; | |
1102 | if (a->start > b->start) | |
1103 | return 1; | |
1104 | return 0; | |
1105 | } | |
1106 | ||
0dcecb2e | 1107 | static int count_memberships(struct dl *dl, struct intel_super *super) |
c2c087e6 | 1108 | { |
c2c087e6 | 1109 | int memberships = 0; |
620b1713 | 1110 | int i; |
c2c087e6 | 1111 | |
949c47a0 DW |
1112 | for (i = 0; i < super->anchor->num_raid_devs; i++) { |
1113 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
238c0a71 | 1114 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
c2c087e6 | 1115 | |
620b1713 DW |
1116 | if (get_imsm_disk_slot(map, dl->index) >= 0) |
1117 | memberships++; | |
c2c087e6 | 1118 | } |
0dcecb2e DW |
1119 | |
1120 | return memberships; | |
1121 | } | |
1122 | ||
b81221b7 CA |
1123 | static __u32 imsm_min_reserved_sectors(struct intel_super *super); |
1124 | ||
5551b113 CA |
1125 | static int split_ull(unsigned long long n, __u32 *lo, __u32 *hi) |
1126 | { | |
1127 | if (lo == 0 || hi == 0) | |
1128 | return 1; | |
1129 | *lo = __le32_to_cpu((unsigned)n); | |
1130 | *hi = __le32_to_cpu((unsigned)(n >> 32)); | |
1131 | return 0; | |
1132 | } | |
1133 | ||
1134 | static unsigned long long join_u32(__u32 lo, __u32 hi) | |
1135 | { | |
1136 | return (unsigned long long)__le32_to_cpu(lo) | | |
1137 | (((unsigned long long)__le32_to_cpu(hi)) << 32); | |
1138 | } | |
1139 | ||
1140 | static unsigned long long total_blocks(struct imsm_disk *disk) | |
1141 | { | |
1142 | if (disk == NULL) | |
1143 | return 0; | |
1144 | return join_u32(disk->total_blocks_lo, disk->total_blocks_hi); | |
1145 | } | |
1146 | ||
1147 | static unsigned long long pba_of_lba0(struct imsm_map *map) | |
1148 | { | |
1149 | if (map == NULL) | |
1150 | return 0; | |
1151 | return join_u32(map->pba_of_lba0_lo, map->pba_of_lba0_hi); | |
1152 | } | |
1153 | ||
1154 | static unsigned long long blocks_per_member(struct imsm_map *map) | |
1155 | { | |
1156 | if (map == NULL) | |
1157 | return 0; | |
1158 | return join_u32(map->blocks_per_member_lo, map->blocks_per_member_hi); | |
1159 | } | |
1160 | ||
1161 | static unsigned long long num_data_stripes(struct imsm_map *map) | |
1162 | { | |
1163 | if (map == NULL) | |
1164 | return 0; | |
1165 | return join_u32(map->num_data_stripes_lo, map->num_data_stripes_hi); | |
1166 | } | |
1167 | ||
1168 | static void set_total_blocks(struct imsm_disk *disk, unsigned long long n) | |
1169 | { | |
1170 | split_ull(n, &disk->total_blocks_lo, &disk->total_blocks_hi); | |
1171 | } | |
1172 | ||
1173 | static void set_pba_of_lba0(struct imsm_map *map, unsigned long long n) | |
1174 | { | |
1175 | split_ull(n, &map->pba_of_lba0_lo, &map->pba_of_lba0_hi); | |
1176 | } | |
1177 | ||
1178 | static void set_blocks_per_member(struct imsm_map *map, unsigned long long n) | |
1179 | { | |
1180 | split_ull(n, &map->blocks_per_member_lo, &map->blocks_per_member_hi); | |
1181 | } | |
1182 | ||
1183 | static void set_num_data_stripes(struct imsm_map *map, unsigned long long n) | |
1184 | { | |
1185 | split_ull(n, &map->num_data_stripes_lo, &map->num_data_stripes_hi); | |
1186 | } | |
1187 | ||
0dcecb2e DW |
1188 | static struct extent *get_extents(struct intel_super *super, struct dl *dl) |
1189 | { | |
1190 | /* find a list of used extents on the given physical device */ | |
1191 | struct extent *rv, *e; | |
620b1713 | 1192 | int i; |
0dcecb2e | 1193 | int memberships = count_memberships(dl, super); |
b276dd33 DW |
1194 | __u32 reservation; |
1195 | ||
1196 | /* trim the reserved area for spares, so they can join any array | |
1197 | * regardless of whether the OROM has assigned sectors from the | |
1198 | * IMSM_RESERVED_SECTORS region | |
1199 | */ | |
1200 | if (dl->index == -1) | |
b81221b7 | 1201 | reservation = imsm_min_reserved_sectors(super); |
b276dd33 DW |
1202 | else |
1203 | reservation = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS; | |
0dcecb2e | 1204 | |
503975b9 | 1205 | rv = xcalloc(sizeof(struct extent), (memberships + 1)); |
c2c087e6 DW |
1206 | e = rv; |
1207 | ||
949c47a0 DW |
1208 | for (i = 0; i < super->anchor->num_raid_devs; i++) { |
1209 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
238c0a71 | 1210 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
c2c087e6 | 1211 | |
620b1713 | 1212 | if (get_imsm_disk_slot(map, dl->index) >= 0) { |
5551b113 CA |
1213 | e->start = pba_of_lba0(map); |
1214 | e->size = blocks_per_member(map); | |
620b1713 | 1215 | e++; |
c2c087e6 DW |
1216 | } |
1217 | } | |
1218 | qsort(rv, memberships, sizeof(*rv), cmp_extent); | |
1219 | ||
1011e834 | 1220 | /* determine the start of the metadata |
14e8215b DW |
1221 | * when no raid devices are defined use the default |
1222 | * ...otherwise allow the metadata to truncate the value | |
1223 | * as is the case with older versions of imsm | |
1224 | */ | |
1225 | if (memberships) { | |
1226 | struct extent *last = &rv[memberships - 1]; | |
5551b113 | 1227 | unsigned long long remainder; |
14e8215b | 1228 | |
5551b113 | 1229 | remainder = total_blocks(&dl->disk) - (last->start + last->size); |
dda5855f DW |
1230 | /* round down to 1k block to satisfy precision of the kernel |
1231 | * 'size' interface | |
1232 | */ | |
1233 | remainder &= ~1UL; | |
1234 | /* make sure remainder is still sane */ | |
f21e18ca | 1235 | if (remainder < (unsigned)ROUND_UP(super->len, 512) >> 9) |
dda5855f | 1236 | remainder = ROUND_UP(super->len, 512) >> 9; |
14e8215b DW |
1237 | if (reservation > remainder) |
1238 | reservation = remainder; | |
1239 | } | |
5551b113 | 1240 | e->start = total_blocks(&dl->disk) - reservation; |
c2c087e6 DW |
1241 | e->size = 0; |
1242 | return rv; | |
1243 | } | |
1244 | ||
14e8215b DW |
1245 | /* try to determine how much space is reserved for metadata from |
1246 | * the last get_extents() entry, otherwise fallback to the | |
1247 | * default | |
1248 | */ | |
1249 | static __u32 imsm_reserved_sectors(struct intel_super *super, struct dl *dl) | |
1250 | { | |
1251 | struct extent *e; | |
1252 | int i; | |
1253 | __u32 rv; | |
1254 | ||
1255 | /* for spares just return a minimal reservation which will grow | |
1256 | * once the spare is picked up by an array | |
1257 | */ | |
1258 | if (dl->index == -1) | |
1259 | return MPB_SECTOR_CNT; | |
1260 | ||
1261 | e = get_extents(super, dl); | |
1262 | if (!e) | |
1263 | return MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS; | |
1264 | ||
1265 | /* scroll to last entry */ | |
1266 | for (i = 0; e[i].size; i++) | |
1267 | continue; | |
1268 | ||
5551b113 | 1269 | rv = total_blocks(&dl->disk) - e[i].start; |
14e8215b DW |
1270 | |
1271 | free(e); | |
1272 | ||
1273 | return rv; | |
1274 | } | |
1275 | ||
25ed7e59 DW |
1276 | static int is_spare(struct imsm_disk *disk) |
1277 | { | |
1278 | return (disk->status & SPARE_DISK) == SPARE_DISK; | |
1279 | } | |
1280 | ||
1281 | static int is_configured(struct imsm_disk *disk) | |
1282 | { | |
1283 | return (disk->status & CONFIGURED_DISK) == CONFIGURED_DISK; | |
1284 | } | |
1285 | ||
1286 | static int is_failed(struct imsm_disk *disk) | |
1287 | { | |
1288 | return (disk->status & FAILED_DISK) == FAILED_DISK; | |
1289 | } | |
1290 | ||
b81221b7 CA |
1291 | /* try to determine how much space is reserved for metadata from |
1292 | * the last get_extents() entry on the smallest active disk, | |
1293 | * otherwise fallback to the default | |
1294 | */ | |
1295 | static __u32 imsm_min_reserved_sectors(struct intel_super *super) | |
1296 | { | |
1297 | struct extent *e; | |
1298 | int i; | |
5551b113 CA |
1299 | unsigned long long min_active; |
1300 | __u32 remainder; | |
b81221b7 CA |
1301 | __u32 rv = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS; |
1302 | struct dl *dl, *dl_min = NULL; | |
1303 | ||
1304 | if (!super) | |
1305 | return rv; | |
1306 | ||
1307 | min_active = 0; | |
1308 | for (dl = super->disks; dl; dl = dl->next) { | |
1309 | if (dl->index < 0) | |
1310 | continue; | |
5551b113 CA |
1311 | unsigned long long blocks = total_blocks(&dl->disk); |
1312 | if (blocks < min_active || min_active == 0) { | |
b81221b7 | 1313 | dl_min = dl; |
5551b113 | 1314 | min_active = blocks; |
b81221b7 CA |
1315 | } |
1316 | } | |
1317 | if (!dl_min) | |
1318 | return rv; | |
1319 | ||
1320 | /* find last lba used by subarrays on the smallest active disk */ | |
1321 | e = get_extents(super, dl_min); | |
1322 | if (!e) | |
1323 | return rv; | |
1324 | for (i = 0; e[i].size; i++) | |
1325 | continue; | |
1326 | ||
1327 | remainder = min_active - e[i].start; | |
1328 | free(e); | |
1329 | ||
1330 | /* to give priority to recovery we should not require full | |
1331 | IMSM_RESERVED_SECTORS from the spare */ | |
1332 | rv = MPB_SECTOR_CNT + NUM_BLOCKS_DIRTY_STRIPE_REGION; | |
1333 | ||
1334 | /* if real reservation is smaller use that value */ | |
1335 | return (remainder < rv) ? remainder : rv; | |
1336 | } | |
1337 | ||
80e7f8c3 AC |
1338 | /* Return minimum size of a spare that can be used in this array*/ |
1339 | static unsigned long long min_acceptable_spare_size_imsm(struct supertype *st) | |
1340 | { | |
1341 | struct intel_super *super = st->sb; | |
1342 | struct dl *dl; | |
1343 | struct extent *e; | |
1344 | int i; | |
1345 | unsigned long long rv = 0; | |
1346 | ||
1347 | if (!super) | |
1348 | return rv; | |
1349 | /* find first active disk in array */ | |
1350 | dl = super->disks; | |
1351 | while (dl && (is_failed(&dl->disk) || dl->index == -1)) | |
1352 | dl = dl->next; | |
1353 | if (!dl) | |
1354 | return rv; | |
1355 | /* find last lba used by subarrays */ | |
1356 | e = get_extents(super, dl); | |
1357 | if (!e) | |
1358 | return rv; | |
1359 | for (i = 0; e[i].size; i++) | |
1360 | continue; | |
1361 | if (i > 0) | |
1362 | rv = e[i-1].start + e[i-1].size; | |
1363 | free(e); | |
b81221b7 | 1364 | |
80e7f8c3 | 1365 | /* add the amount of space needed for metadata */ |
b81221b7 CA |
1366 | rv = rv + imsm_min_reserved_sectors(super); |
1367 | ||
80e7f8c3 AC |
1368 | return rv * 512; |
1369 | } | |
1370 | ||
d1e02575 AK |
1371 | static int is_gen_migration(struct imsm_dev *dev); |
1372 | ||
f36a9ecd PB |
1373 | #define IMSM_4K_DIV 8 |
1374 | ||
1799c9e8 | 1375 | #ifndef MDASSEMBLE |
c47b0ff6 AK |
1376 | static __u64 blocks_per_migr_unit(struct intel_super *super, |
1377 | struct imsm_dev *dev); | |
1e5c6983 | 1378 | |
c47b0ff6 AK |
1379 | static void print_imsm_dev(struct intel_super *super, |
1380 | struct imsm_dev *dev, | |
1381 | char *uuid, | |
1382 | int disk_idx) | |
cdddbdbc DW |
1383 | { |
1384 | __u64 sz; | |
0d80bb2f | 1385 | int slot, i; |
238c0a71 AK |
1386 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
1387 | struct imsm_map *map2 = get_imsm_map(dev, MAP_1); | |
b10b37b8 | 1388 | __u32 ord; |
cdddbdbc DW |
1389 | |
1390 | printf("\n"); | |
1e7bc0ed | 1391 | printf("[%.16s]:\n", dev->volume); |
44470971 | 1392 | printf(" UUID : %s\n", uuid); |
dd8bcb3b AK |
1393 | printf(" RAID Level : %d", get_imsm_raid_level(map)); |
1394 | if (map2) | |
1395 | printf(" <-- %d", get_imsm_raid_level(map2)); | |
1396 | printf("\n"); | |
1397 | printf(" Members : %d", map->num_members); | |
1398 | if (map2) | |
1399 | printf(" <-- %d", map2->num_members); | |
1400 | printf("\n"); | |
0d80bb2f DW |
1401 | printf(" Slots : ["); |
1402 | for (i = 0; i < map->num_members; i++) { | |
238c0a71 | 1403 | ord = get_imsm_ord_tbl_ent(dev, i, MAP_0); |
0d80bb2f DW |
1404 | printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U"); |
1405 | } | |
dd8bcb3b AK |
1406 | printf("]"); |
1407 | if (map2) { | |
1408 | printf(" <-- ["); | |
1409 | for (i = 0; i < map2->num_members; i++) { | |
238c0a71 | 1410 | ord = get_imsm_ord_tbl_ent(dev, i, MAP_1); |
dd8bcb3b AK |
1411 | printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U"); |
1412 | } | |
1413 | printf("]"); | |
1414 | } | |
1415 | printf("\n"); | |
7095bccb AK |
1416 | printf(" Failed disk : "); |
1417 | if (map->failed_disk_num == 0xff) | |
1418 | printf("none"); | |
1419 | else | |
1420 | printf("%i", map->failed_disk_num); | |
1421 | printf("\n"); | |
620b1713 DW |
1422 | slot = get_imsm_disk_slot(map, disk_idx); |
1423 | if (slot >= 0) { | |
238c0a71 | 1424 | ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X); |
b10b37b8 DW |
1425 | printf(" This Slot : %d%s\n", slot, |
1426 | ord & IMSM_ORD_REBUILD ? " (out-of-sync)" : ""); | |
1427 | } else | |
cdddbdbc DW |
1428 | printf(" This Slot : ?\n"); |
1429 | sz = __le32_to_cpu(dev->size_high); | |
1430 | sz <<= 32; | |
1431 | sz += __le32_to_cpu(dev->size_low); | |
1432 | printf(" Array Size : %llu%s\n", (unsigned long long)sz, | |
1433 | human_size(sz * 512)); | |
5551b113 | 1434 | sz = blocks_per_member(map); |
cdddbdbc DW |
1435 | printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz, |
1436 | human_size(sz * 512)); | |
5551b113 CA |
1437 | printf(" Sector Offset : %llu\n", |
1438 | pba_of_lba0(map)); | |
1439 | printf(" Num Stripes : %llu\n", | |
1440 | num_data_stripes(map)); | |
dd8bcb3b | 1441 | printf(" Chunk Size : %u KiB", |
cdddbdbc | 1442 | __le16_to_cpu(map->blocks_per_strip) / 2); |
dd8bcb3b AK |
1443 | if (map2) |
1444 | printf(" <-- %u KiB", | |
1445 | __le16_to_cpu(map2->blocks_per_strip) / 2); | |
1446 | printf("\n"); | |
cdddbdbc | 1447 | printf(" Reserved : %d\n", __le32_to_cpu(dev->reserved_blocks)); |
8655a7b1 | 1448 | printf(" Migrate State : "); |
1484e727 DW |
1449 | if (dev->vol.migr_state) { |
1450 | if (migr_type(dev) == MIGR_INIT) | |
8655a7b1 | 1451 | printf("initialize\n"); |
1484e727 | 1452 | else if (migr_type(dev) == MIGR_REBUILD) |
8655a7b1 | 1453 | printf("rebuild\n"); |
1484e727 | 1454 | else if (migr_type(dev) == MIGR_VERIFY) |
8655a7b1 | 1455 | printf("check\n"); |
1484e727 | 1456 | else if (migr_type(dev) == MIGR_GEN_MIGR) |
8655a7b1 | 1457 | printf("general migration\n"); |
1484e727 | 1458 | else if (migr_type(dev) == MIGR_STATE_CHANGE) |
8655a7b1 | 1459 | printf("state change\n"); |
1484e727 | 1460 | else if (migr_type(dev) == MIGR_REPAIR) |
8655a7b1 | 1461 | printf("repair\n"); |
1484e727 | 1462 | else |
8655a7b1 DW |
1463 | printf("<unknown:%d>\n", migr_type(dev)); |
1464 | } else | |
1465 | printf("idle\n"); | |
3393c6af DW |
1466 | printf(" Map State : %s", map_state_str[map->map_state]); |
1467 | if (dev->vol.migr_state) { | |
238c0a71 | 1468 | struct imsm_map *map = get_imsm_map(dev, MAP_1); |
1e5c6983 | 1469 | |
b10b37b8 | 1470 | printf(" <-- %s", map_state_str[map->map_state]); |
464d40e8 LD |
1471 | printf("\n Checkpoint : %u ", |
1472 | __le32_to_cpu(dev->vol.curr_migr_unit)); | |
089f9d79 | 1473 | if (is_gen_migration(dev) && (slot > 1 || slot < 0)) |
464d40e8 LD |
1474 | printf("(N/A)"); |
1475 | else | |
1476 | printf("(%llu)", (unsigned long long) | |
1477 | blocks_per_migr_unit(super, dev)); | |
3393c6af DW |
1478 | } |
1479 | printf("\n"); | |
cdddbdbc | 1480 | printf(" Dirty State : %s\n", dev->vol.dirty ? "dirty" : "clean"); |
cdddbdbc DW |
1481 | } |
1482 | ||
0ec1f4e8 | 1483 | static void print_imsm_disk(struct imsm_disk *disk, int index, __u32 reserved) |
cdddbdbc | 1484 | { |
1f24f035 | 1485 | char str[MAX_RAID_SERIAL_LEN + 1]; |
cdddbdbc DW |
1486 | __u64 sz; |
1487 | ||
0ec1f4e8 | 1488 | if (index < -1 || !disk) |
e9d82038 DW |
1489 | return; |
1490 | ||
cdddbdbc | 1491 | printf("\n"); |
1f24f035 | 1492 | snprintf(str, MAX_RAID_SERIAL_LEN + 1, "%s", disk->serial); |
0ec1f4e8 DW |
1493 | if (index >= 0) |
1494 | printf(" Disk%02d Serial : %s\n", index, str); | |
1495 | else | |
1496 | printf(" Disk Serial : %s\n", str); | |
25ed7e59 DW |
1497 | printf(" State :%s%s%s\n", is_spare(disk) ? " spare" : "", |
1498 | is_configured(disk) ? " active" : "", | |
1499 | is_failed(disk) ? " failed" : ""); | |
cdddbdbc | 1500 | printf(" Id : %08x\n", __le32_to_cpu(disk->scsi_id)); |
5551b113 | 1501 | sz = total_blocks(disk) - reserved; |
cdddbdbc DW |
1502 | printf(" Usable Size : %llu%s\n", (unsigned long long)sz, |
1503 | human_size(sz * 512)); | |
1504 | } | |
1505 | ||
de44e46f PB |
1506 | void convert_to_4k_imsm_migr_rec(struct intel_super *super) |
1507 | { | |
1508 | struct migr_record *migr_rec = super->migr_rec; | |
1509 | ||
1510 | migr_rec->blocks_per_unit /= IMSM_4K_DIV; | |
1511 | migr_rec->ckpt_area_pba /= IMSM_4K_DIV; | |
1512 | migr_rec->dest_1st_member_lba /= IMSM_4K_DIV; | |
1513 | migr_rec->dest_depth_per_unit /= IMSM_4K_DIV; | |
1514 | split_ull((join_u32(migr_rec->post_migr_vol_cap, | |
1515 | migr_rec->post_migr_vol_cap_hi) / IMSM_4K_DIV), | |
1516 | &migr_rec->post_migr_vol_cap, &migr_rec->post_migr_vol_cap_hi); | |
1517 | } | |
1518 | ||
f36a9ecd PB |
1519 | void convert_to_4k_imsm_disk(struct imsm_disk *disk) |
1520 | { | |
1521 | set_total_blocks(disk, (total_blocks(disk)/IMSM_4K_DIV)); | |
1522 | } | |
1523 | ||
1524 | void convert_to_4k(struct intel_super *super) | |
1525 | { | |
1526 | struct imsm_super *mpb = super->anchor; | |
1527 | struct imsm_disk *disk; | |
1528 | int i; | |
e4467bc7 | 1529 | __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size); |
f36a9ecd PB |
1530 | |
1531 | for (i = 0; i < mpb->num_disks ; i++) { | |
1532 | disk = __get_imsm_disk(mpb, i); | |
1533 | /* disk */ | |
1534 | convert_to_4k_imsm_disk(disk); | |
1535 | } | |
1536 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
1537 | struct imsm_dev *dev = __get_imsm_dev(mpb, i); | |
1538 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
1539 | /* dev */ | |
1540 | split_ull((join_u32(dev->size_low, dev->size_high)/IMSM_4K_DIV), | |
1541 | &dev->size_low, &dev->size_high); | |
1542 | dev->vol.curr_migr_unit /= IMSM_4K_DIV; | |
1543 | ||
1544 | /* map0 */ | |
1545 | set_blocks_per_member(map, blocks_per_member(map)/IMSM_4K_DIV); | |
1546 | map->blocks_per_strip /= IMSM_4K_DIV; | |
1547 | set_pba_of_lba0(map, pba_of_lba0(map)/IMSM_4K_DIV); | |
1548 | ||
1549 | if (dev->vol.migr_state) { | |
1550 | /* map1 */ | |
1551 | map = get_imsm_map(dev, MAP_1); | |
1552 | set_blocks_per_member(map, | |
1553 | blocks_per_member(map)/IMSM_4K_DIV); | |
1554 | map->blocks_per_strip /= IMSM_4K_DIV; | |
1555 | set_pba_of_lba0(map, pba_of_lba0(map)/IMSM_4K_DIV); | |
1556 | } | |
1557 | } | |
e4467bc7 TM |
1558 | if (bbm_log_size) { |
1559 | struct bbm_log *log = (void *)mpb + | |
1560 | __le32_to_cpu(mpb->mpb_size) - bbm_log_size; | |
1561 | __u32 i; | |
1562 | ||
1563 | for (i = 0; i < log->entry_count; i++) { | |
1564 | struct bbm_log_entry *entry = | |
1565 | &log->marked_block_entries[i]; | |
1566 | ||
1567 | __u8 count = entry->marked_count + 1; | |
1568 | unsigned long long sector = | |
1569 | __le48_to_cpu(&entry->defective_block_start); | |
1570 | ||
1571 | entry->defective_block_start = | |
1572 | __cpu_to_le48(sector/IMSM_4K_DIV); | |
1573 | entry->marked_count = max(count/IMSM_4K_DIV, 1) - 1; | |
1574 | } | |
1575 | } | |
f36a9ecd PB |
1576 | |
1577 | mpb->check_sum = __gen_imsm_checksum(mpb); | |
1578 | } | |
1579 | ||
520e69e2 AK |
1580 | void examine_migr_rec_imsm(struct intel_super *super) |
1581 | { | |
1582 | struct migr_record *migr_rec = super->migr_rec; | |
1583 | struct imsm_super *mpb = super->anchor; | |
1584 | int i; | |
1585 | ||
1586 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
1587 | struct imsm_dev *dev = __get_imsm_dev(mpb, i); | |
3136abe5 | 1588 | struct imsm_map *map; |
b4ab44d8 | 1589 | int slot = -1; |
3136abe5 | 1590 | |
520e69e2 AK |
1591 | if (is_gen_migration(dev) == 0) |
1592 | continue; | |
1593 | ||
1594 | printf("\nMigration Record Information:"); | |
3136abe5 | 1595 | |
44bfe6df AK |
1596 | /* first map under migration */ |
1597 | map = get_imsm_map(dev, MAP_0); | |
3136abe5 AK |
1598 | if (map) |
1599 | slot = get_imsm_disk_slot(map, super->disks->index); | |
089f9d79 | 1600 | if (map == NULL || slot > 1 || slot < 0) { |
520e69e2 AK |
1601 | printf(" Empty\n "); |
1602 | printf("Examine one of first two disks in array\n"); | |
1603 | break; | |
1604 | } | |
1605 | printf("\n Status : "); | |
1606 | if (__le32_to_cpu(migr_rec->rec_status) == UNIT_SRC_NORMAL) | |
1607 | printf("Normal\n"); | |
1608 | else | |
1609 | printf("Contains Data\n"); | |
1610 | printf(" Current Unit : %u\n", | |
1611 | __le32_to_cpu(migr_rec->curr_migr_unit)); | |
1612 | printf(" Family : %u\n", | |
1613 | __le32_to_cpu(migr_rec->family_num)); | |
1614 | printf(" Ascending : %u\n", | |
1615 | __le32_to_cpu(migr_rec->ascending_migr)); | |
1616 | printf(" Blocks Per Unit : %u\n", | |
1617 | __le32_to_cpu(migr_rec->blocks_per_unit)); | |
1618 | printf(" Dest. Depth Per Unit : %u\n", | |
1619 | __le32_to_cpu(migr_rec->dest_depth_per_unit)); | |
1620 | printf(" Checkpoint Area pba : %u\n", | |
1621 | __le32_to_cpu(migr_rec->ckpt_area_pba)); | |
1622 | printf(" First member lba : %u\n", | |
1623 | __le32_to_cpu(migr_rec->dest_1st_member_lba)); | |
1624 | printf(" Total Number of Units : %u\n", | |
1625 | __le32_to_cpu(migr_rec->num_migr_units)); | |
1626 | printf(" Size of volume : %u\n", | |
1627 | __le32_to_cpu(migr_rec->post_migr_vol_cap)); | |
1628 | printf(" Expansion space for LBA64 : %u\n", | |
1629 | __le32_to_cpu(migr_rec->post_migr_vol_cap_hi)); | |
1630 | printf(" Record was read from : %u\n", | |
1631 | __le32_to_cpu(migr_rec->ckpt_read_disk_num)); | |
1632 | ||
1633 | break; | |
1634 | } | |
1635 | } | |
9e2d750d | 1636 | #endif /* MDASSEMBLE */ |
f36a9ecd | 1637 | |
de44e46f PB |
1638 | void convert_from_4k_imsm_migr_rec(struct intel_super *super) |
1639 | { | |
1640 | struct migr_record *migr_rec = super->migr_rec; | |
1641 | ||
1642 | migr_rec->blocks_per_unit *= IMSM_4K_DIV; | |
1643 | migr_rec->ckpt_area_pba *= IMSM_4K_DIV; | |
1644 | migr_rec->dest_1st_member_lba *= IMSM_4K_DIV; | |
1645 | migr_rec->dest_depth_per_unit *= IMSM_4K_DIV; | |
1646 | split_ull((join_u32(migr_rec->post_migr_vol_cap, | |
1647 | migr_rec->post_migr_vol_cap_hi) * IMSM_4K_DIV), | |
1648 | &migr_rec->post_migr_vol_cap, | |
1649 | &migr_rec->post_migr_vol_cap_hi); | |
1650 | } | |
1651 | ||
f36a9ecd PB |
1652 | void convert_from_4k(struct intel_super *super) |
1653 | { | |
1654 | struct imsm_super *mpb = super->anchor; | |
1655 | struct imsm_disk *disk; | |
1656 | int i; | |
e4467bc7 | 1657 | __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size); |
f36a9ecd PB |
1658 | |
1659 | for (i = 0; i < mpb->num_disks ; i++) { | |
1660 | disk = __get_imsm_disk(mpb, i); | |
1661 | /* disk */ | |
1662 | set_total_blocks(disk, (total_blocks(disk)*IMSM_4K_DIV)); | |
1663 | } | |
1664 | ||
1665 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
1666 | struct imsm_dev *dev = __get_imsm_dev(mpb, i); | |
1667 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
1668 | /* dev */ | |
1669 | split_ull((join_u32(dev->size_low, dev->size_high)*IMSM_4K_DIV), | |
1670 | &dev->size_low, &dev->size_high); | |
1671 | dev->vol.curr_migr_unit *= IMSM_4K_DIV; | |
1672 | ||
1673 | /* map0 */ | |
1674 | set_blocks_per_member(map, blocks_per_member(map)*IMSM_4K_DIV); | |
1675 | map->blocks_per_strip *= IMSM_4K_DIV; | |
1676 | set_pba_of_lba0(map, pba_of_lba0(map)*IMSM_4K_DIV); | |
1677 | ||
1678 | if (dev->vol.migr_state) { | |
1679 | /* map1 */ | |
1680 | map = get_imsm_map(dev, MAP_1); | |
1681 | set_blocks_per_member(map, | |
1682 | blocks_per_member(map)*IMSM_4K_DIV); | |
1683 | map->blocks_per_strip *= IMSM_4K_DIV; | |
1684 | set_pba_of_lba0(map, pba_of_lba0(map)*IMSM_4K_DIV); | |
1685 | } | |
1686 | } | |
e4467bc7 TM |
1687 | if (bbm_log_size) { |
1688 | struct bbm_log *log = (void *)mpb + | |
1689 | __le32_to_cpu(mpb->mpb_size) - bbm_log_size; | |
1690 | __u32 i; | |
1691 | ||
1692 | for (i = 0; i < log->entry_count; i++) { | |
1693 | struct bbm_log_entry *entry = | |
1694 | &log->marked_block_entries[i]; | |
1695 | ||
1696 | __u8 count = entry->marked_count + 1; | |
1697 | unsigned long long sector = | |
1698 | __le48_to_cpu(&entry->defective_block_start); | |
1699 | ||
1700 | entry->defective_block_start = | |
1701 | __cpu_to_le48(sector*IMSM_4K_DIV); | |
1702 | entry->marked_count = count*IMSM_4K_DIV - 1; | |
1703 | } | |
1704 | } | |
f36a9ecd PB |
1705 | |
1706 | mpb->check_sum = __gen_imsm_checksum(mpb); | |
1707 | } | |
1708 | ||
19482bcc AK |
1709 | /******************************************************************************* |
1710 | * function: imsm_check_attributes | |
1711 | * Description: Function checks if features represented by attributes flags | |
1011e834 | 1712 | * are supported by mdadm. |
19482bcc AK |
1713 | * Parameters: |
1714 | * attributes - Attributes read from metadata | |
1715 | * Returns: | |
1011e834 N |
1716 | * 0 - passed attributes contains unsupported features flags |
1717 | * 1 - all features are supported | |
19482bcc AK |
1718 | ******************************************************************************/ |
1719 | static int imsm_check_attributes(__u32 attributes) | |
1720 | { | |
1721 | int ret_val = 1; | |
418f9b36 N |
1722 | __u32 not_supported = MPB_ATTRIB_SUPPORTED^0xffffffff; |
1723 | ||
1724 | not_supported &= ~MPB_ATTRIB_IGNORED; | |
19482bcc AK |
1725 | |
1726 | not_supported &= attributes; | |
1727 | if (not_supported) { | |
e7b84f9d | 1728 | pr_err("(IMSM): Unsupported attributes : %x\n", |
418f9b36 | 1729 | (unsigned)__le32_to_cpu(not_supported)); |
19482bcc AK |
1730 | if (not_supported & MPB_ATTRIB_CHECKSUM_VERIFY) { |
1731 | dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n"); | |
1732 | not_supported ^= MPB_ATTRIB_CHECKSUM_VERIFY; | |
1733 | } | |
1734 | if (not_supported & MPB_ATTRIB_2TB) { | |
1735 | dprintf("\t\tMPB_ATTRIB_2TB\n"); | |
1736 | not_supported ^= MPB_ATTRIB_2TB; | |
1737 | } | |
1738 | if (not_supported & MPB_ATTRIB_RAID0) { | |
1739 | dprintf("\t\tMPB_ATTRIB_RAID0\n"); | |
1740 | not_supported ^= MPB_ATTRIB_RAID0; | |
1741 | } | |
1742 | if (not_supported & MPB_ATTRIB_RAID1) { | |
1743 | dprintf("\t\tMPB_ATTRIB_RAID1\n"); | |
1744 | not_supported ^= MPB_ATTRIB_RAID1; | |
1745 | } | |
1746 | if (not_supported & MPB_ATTRIB_RAID10) { | |
1747 | dprintf("\t\tMPB_ATTRIB_RAID10\n"); | |
1748 | not_supported ^= MPB_ATTRIB_RAID10; | |
1749 | } | |
1750 | if (not_supported & MPB_ATTRIB_RAID1E) { | |
1751 | dprintf("\t\tMPB_ATTRIB_RAID1E\n"); | |
1752 | not_supported ^= MPB_ATTRIB_RAID1E; | |
1753 | } | |
1754 | if (not_supported & MPB_ATTRIB_RAID5) { | |
1755 | dprintf("\t\tMPB_ATTRIB_RAID5\n"); | |
1756 | not_supported ^= MPB_ATTRIB_RAID5; | |
1757 | } | |
1758 | if (not_supported & MPB_ATTRIB_RAIDCNG) { | |
1759 | dprintf("\t\tMPB_ATTRIB_RAIDCNG\n"); | |
1760 | not_supported ^= MPB_ATTRIB_RAIDCNG; | |
1761 | } | |
1762 | if (not_supported & MPB_ATTRIB_BBM) { | |
1763 | dprintf("\t\tMPB_ATTRIB_BBM\n"); | |
1764 | not_supported ^= MPB_ATTRIB_BBM; | |
1765 | } | |
1766 | if (not_supported & MPB_ATTRIB_CHECKSUM_VERIFY) { | |
1767 | dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n"); | |
1768 | not_supported ^= MPB_ATTRIB_CHECKSUM_VERIFY; | |
1769 | } | |
1770 | if (not_supported & MPB_ATTRIB_EXP_STRIPE_SIZE) { | |
1771 | dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n"); | |
1772 | not_supported ^= MPB_ATTRIB_EXP_STRIPE_SIZE; | |
1773 | } | |
1774 | if (not_supported & MPB_ATTRIB_2TB_DISK) { | |
1775 | dprintf("\t\tMPB_ATTRIB_2TB_DISK\n"); | |
1776 | not_supported ^= MPB_ATTRIB_2TB_DISK; | |
1777 | } | |
1778 | if (not_supported & MPB_ATTRIB_NEVER_USE2) { | |
1779 | dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n"); | |
1780 | not_supported ^= MPB_ATTRIB_NEVER_USE2; | |
1781 | } | |
1782 | if (not_supported & MPB_ATTRIB_NEVER_USE) { | |
1783 | dprintf("\t\tMPB_ATTRIB_NEVER_USE\n"); | |
1784 | not_supported ^= MPB_ATTRIB_NEVER_USE; | |
1785 | } | |
1786 | ||
1787 | if (not_supported) | |
1ade5cc1 | 1788 | dprintf("(IMSM): Unknown attributes : %x\n", not_supported); |
19482bcc AK |
1789 | |
1790 | ret_val = 0; | |
1791 | } | |
1792 | ||
1793 | return ret_val; | |
1794 | } | |
1795 | ||
9e2d750d | 1796 | #ifndef MDASSEMBLE |
a5d85af7 | 1797 | static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map); |
44470971 | 1798 | |
cdddbdbc DW |
1799 | static void examine_super_imsm(struct supertype *st, char *homehost) |
1800 | { | |
1801 | struct intel_super *super = st->sb; | |
949c47a0 | 1802 | struct imsm_super *mpb = super->anchor; |
cdddbdbc DW |
1803 | char str[MAX_SIGNATURE_LENGTH]; |
1804 | int i; | |
27fd6274 DW |
1805 | struct mdinfo info; |
1806 | char nbuf[64]; | |
cdddbdbc | 1807 | __u32 sum; |
14e8215b | 1808 | __u32 reserved = imsm_reserved_sectors(super, super->disks); |
94827db3 | 1809 | struct dl *dl; |
27fd6274 | 1810 | |
cdddbdbc DW |
1811 | snprintf(str, MPB_SIG_LEN, "%s", mpb->sig); |
1812 | printf(" Magic : %s\n", str); | |
1813 | snprintf(str, strlen(MPB_VERSION_RAID0), "%s", get_imsm_version(mpb)); | |
1814 | printf(" Version : %s\n", get_imsm_version(mpb)); | |
148acb7b | 1815 | printf(" Orig Family : %08x\n", __le32_to_cpu(mpb->orig_family_num)); |
cdddbdbc DW |
1816 | printf(" Family : %08x\n", __le32_to_cpu(mpb->family_num)); |
1817 | printf(" Generation : %08x\n", __le32_to_cpu(mpb->generation_num)); | |
19482bcc AK |
1818 | printf(" Attributes : "); |
1819 | if (imsm_check_attributes(mpb->attributes)) | |
1820 | printf("All supported\n"); | |
1821 | else | |
1822 | printf("not supported\n"); | |
a5d85af7 | 1823 | getinfo_super_imsm(st, &info, NULL); |
ae2bfd4e | 1824 | fname_from_uuid(st, &info, nbuf, ':'); |
27fd6274 | 1825 | printf(" UUID : %s\n", nbuf + 5); |
cdddbdbc DW |
1826 | sum = __le32_to_cpu(mpb->check_sum); |
1827 | printf(" Checksum : %08x %s\n", sum, | |
949c47a0 | 1828 | __gen_imsm_checksum(mpb) == sum ? "correct" : "incorrect"); |
f36a9ecd | 1829 | printf(" MPB Sectors : %d\n", mpb_sectors(mpb, super->sector_size)); |
cdddbdbc DW |
1830 | printf(" Disks : %d\n", mpb->num_disks); |
1831 | printf(" RAID Devices : %d\n", mpb->num_raid_devs); | |
0ec1f4e8 | 1832 | print_imsm_disk(__get_imsm_disk(mpb, super->disks->index), super->disks->index, reserved); |
8d67477f | 1833 | if (get_imsm_bbm_log_size(super->bbm_log)) { |
604b746f JD |
1834 | struct bbm_log *log = super->bbm_log; |
1835 | ||
1836 | printf("\n"); | |
1837 | printf("Bad Block Management Log:\n"); | |
1838 | printf(" Log Size : %d\n", __le32_to_cpu(mpb->bbm_log_size)); | |
1839 | printf(" Signature : %x\n", __le32_to_cpu(log->signature)); | |
1840 | printf(" Entry Count : %d\n", __le32_to_cpu(log->entry_count)); | |
604b746f | 1841 | } |
44470971 DW |
1842 | for (i = 0; i < mpb->num_raid_devs; i++) { |
1843 | struct mdinfo info; | |
1844 | struct imsm_dev *dev = __get_imsm_dev(mpb, i); | |
1845 | ||
1846 | super->current_vol = i; | |
a5d85af7 | 1847 | getinfo_super_imsm(st, &info, NULL); |
ae2bfd4e | 1848 | fname_from_uuid(st, &info, nbuf, ':'); |
c47b0ff6 | 1849 | print_imsm_dev(super, dev, nbuf + 5, super->disks->index); |
44470971 | 1850 | } |
cdddbdbc DW |
1851 | for (i = 0; i < mpb->num_disks; i++) { |
1852 | if (i == super->disks->index) | |
1853 | continue; | |
0ec1f4e8 | 1854 | print_imsm_disk(__get_imsm_disk(mpb, i), i, reserved); |
cdddbdbc | 1855 | } |
94827db3 | 1856 | |
0ec1f4e8 DW |
1857 | for (dl = super->disks; dl; dl = dl->next) |
1858 | if (dl->index == -1) | |
1859 | print_imsm_disk(&dl->disk, -1, reserved); | |
520e69e2 AK |
1860 | |
1861 | examine_migr_rec_imsm(super); | |
cdddbdbc DW |
1862 | } |
1863 | ||
061f2c6a | 1864 | static void brief_examine_super_imsm(struct supertype *st, int verbose) |
cdddbdbc | 1865 | { |
27fd6274 | 1866 | /* We just write a generic IMSM ARRAY entry */ |
ff54de6e N |
1867 | struct mdinfo info; |
1868 | char nbuf[64]; | |
1e7bc0ed | 1869 | struct intel_super *super = st->sb; |
1e7bc0ed | 1870 | |
0d5a423f DW |
1871 | if (!super->anchor->num_raid_devs) { |
1872 | printf("ARRAY metadata=imsm\n"); | |
1e7bc0ed | 1873 | return; |
0d5a423f | 1874 | } |
ff54de6e | 1875 | |
a5d85af7 | 1876 | getinfo_super_imsm(st, &info, NULL); |
4737ae25 N |
1877 | fname_from_uuid(st, &info, nbuf, ':'); |
1878 | printf("ARRAY metadata=imsm UUID=%s\n", nbuf + 5); | |
1879 | } | |
1880 | ||
1881 | static void brief_examine_subarrays_imsm(struct supertype *st, int verbose) | |
1882 | { | |
1883 | /* We just write a generic IMSM ARRAY entry */ | |
1884 | struct mdinfo info; | |
1885 | char nbuf[64]; | |
1886 | char nbuf1[64]; | |
1887 | struct intel_super *super = st->sb; | |
1888 | int i; | |
1889 | ||
1890 | if (!super->anchor->num_raid_devs) | |
1891 | return; | |
1892 | ||
a5d85af7 | 1893 | getinfo_super_imsm(st, &info, NULL); |
ae2bfd4e | 1894 | fname_from_uuid(st, &info, nbuf, ':'); |
1e7bc0ed DW |
1895 | for (i = 0; i < super->anchor->num_raid_devs; i++) { |
1896 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
1897 | ||
1898 | super->current_vol = i; | |
a5d85af7 | 1899 | getinfo_super_imsm(st, &info, NULL); |
ae2bfd4e | 1900 | fname_from_uuid(st, &info, nbuf1, ':'); |
1124b3cf | 1901 | printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n", |
cf8de691 | 1902 | dev->volume, nbuf + 5, i, nbuf1 + 5); |
1e7bc0ed | 1903 | } |
cdddbdbc DW |
1904 | } |
1905 | ||
9d84c8ea DW |
1906 | static void export_examine_super_imsm(struct supertype *st) |
1907 | { | |
1908 | struct intel_super *super = st->sb; | |
1909 | struct imsm_super *mpb = super->anchor; | |
1910 | struct mdinfo info; | |
1911 | char nbuf[64]; | |
1912 | ||
a5d85af7 | 1913 | getinfo_super_imsm(st, &info, NULL); |
9d84c8ea DW |
1914 | fname_from_uuid(st, &info, nbuf, ':'); |
1915 | printf("MD_METADATA=imsm\n"); | |
1916 | printf("MD_LEVEL=container\n"); | |
1917 | printf("MD_UUID=%s\n", nbuf+5); | |
1918 | printf("MD_DEVICES=%u\n", mpb->num_disks); | |
1919 | } | |
1920 | ||
74db60b0 N |
1921 | static int copy_metadata_imsm(struct supertype *st, int from, int to) |
1922 | { | |
f36a9ecd | 1923 | /* The second last sector of the device contains |
74db60b0 N |
1924 | * the "struct imsm_super" metadata. |
1925 | * This contains mpb_size which is the size in bytes of the | |
1926 | * extended metadata. This is located immediately before | |
1927 | * the imsm_super. | |
1928 | * We want to read all that, plus the last sector which | |
1929 | * may contain a migration record, and write it all | |
1930 | * to the target. | |
1931 | */ | |
1932 | void *buf; | |
1933 | unsigned long long dsize, offset; | |
1934 | int sectors; | |
1935 | struct imsm_super *sb; | |
f36a9ecd PB |
1936 | struct intel_super *super = st->sb; |
1937 | unsigned int sector_size = super->sector_size; | |
1938 | unsigned int written = 0; | |
74db60b0 | 1939 | |
de44e46f | 1940 | if (posix_memalign(&buf, MAX_SECTOR_SIZE, MAX_SECTOR_SIZE) != 0) |
74db60b0 N |
1941 | return 1; |
1942 | ||
1943 | if (!get_dev_size(from, NULL, &dsize)) | |
1944 | goto err; | |
1945 | ||
f36a9ecd | 1946 | if (lseek64(from, dsize-(2*sector_size), 0) < 0) |
74db60b0 | 1947 | goto err; |
f36a9ecd | 1948 | if (read(from, buf, sector_size) != sector_size) |
74db60b0 N |
1949 | goto err; |
1950 | sb = buf; | |
1951 | if (strncmp((char*)sb->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) | |
1952 | goto err; | |
1953 | ||
f36a9ecd PB |
1954 | sectors = mpb_sectors(sb, sector_size) + 2; |
1955 | offset = dsize - sectors * sector_size; | |
74db60b0 N |
1956 | if (lseek64(from, offset, 0) < 0 || |
1957 | lseek64(to, offset, 0) < 0) | |
1958 | goto err; | |
f36a9ecd PB |
1959 | while (written < sectors * sector_size) { |
1960 | int n = sectors*sector_size - written; | |
74db60b0 N |
1961 | if (n > 4096) |
1962 | n = 4096; | |
1963 | if (read(from, buf, n) != n) | |
1964 | goto err; | |
1965 | if (write(to, buf, n) != n) | |
1966 | goto err; | |
1967 | written += n; | |
1968 | } | |
1969 | free(buf); | |
1970 | return 0; | |
1971 | err: | |
1972 | free(buf); | |
1973 | return 1; | |
1974 | } | |
1975 | ||
cdddbdbc DW |
1976 | static void detail_super_imsm(struct supertype *st, char *homehost) |
1977 | { | |
3ebe00a1 DW |
1978 | struct mdinfo info; |
1979 | char nbuf[64]; | |
1980 | ||
a5d85af7 | 1981 | getinfo_super_imsm(st, &info, NULL); |
ae2bfd4e | 1982 | fname_from_uuid(st, &info, nbuf, ':'); |
3ebe00a1 | 1983 | printf("\n UUID : %s\n", nbuf + 5); |
cdddbdbc DW |
1984 | } |
1985 | ||
1986 | static void brief_detail_super_imsm(struct supertype *st) | |
1987 | { | |
ff54de6e N |
1988 | struct mdinfo info; |
1989 | char nbuf[64]; | |
a5d85af7 | 1990 | getinfo_super_imsm(st, &info, NULL); |
ae2bfd4e | 1991 | fname_from_uuid(st, &info, nbuf, ':'); |
ff54de6e | 1992 | printf(" UUID=%s", nbuf + 5); |
cdddbdbc | 1993 | } |
d665cc31 DW |
1994 | |
1995 | static int imsm_read_serial(int fd, char *devname, __u8 *serial); | |
1996 | static void fd2devname(int fd, char *name); | |
1997 | ||
120dc887 | 1998 | static int ahci_enumerate_ports(const char *hba_path, int port_count, int host_base, int verbose) |
d665cc31 | 1999 | { |
120dc887 LM |
2000 | /* dump an unsorted list of devices attached to AHCI Intel storage |
2001 | * controller, as well as non-connected ports | |
d665cc31 DW |
2002 | */ |
2003 | int hba_len = strlen(hba_path) + 1; | |
2004 | struct dirent *ent; | |
2005 | DIR *dir; | |
2006 | char *path = NULL; | |
2007 | int err = 0; | |
2008 | unsigned long port_mask = (1 << port_count) - 1; | |
2009 | ||
f21e18ca | 2010 | if (port_count > (int)sizeof(port_mask) * 8) { |
ba728be7 | 2011 | if (verbose > 0) |
e7b84f9d | 2012 | pr_err("port_count %d out of range\n", port_count); |
d665cc31 DW |
2013 | return 2; |
2014 | } | |
2015 | ||
2016 | /* scroll through /sys/dev/block looking for devices attached to | |
2017 | * this hba | |
2018 | */ | |
2019 | dir = opendir("/sys/dev/block"); | |
1a6dd6b9 PB |
2020 | if (!dir) |
2021 | return 1; | |
2022 | ||
2023 | for (ent = readdir(dir); ent; ent = readdir(dir)) { | |
d665cc31 DW |
2024 | int fd; |
2025 | char model[64]; | |
2026 | char vendor[64]; | |
2027 | char buf[1024]; | |
2028 | int major, minor; | |
2029 | char *device; | |
2030 | char *c; | |
2031 | int port; | |
2032 | int type; | |
2033 | ||
2034 | if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2) | |
2035 | continue; | |
2036 | path = devt_to_devpath(makedev(major, minor)); | |
2037 | if (!path) | |
2038 | continue; | |
2039 | if (!path_attached_to_hba(path, hba_path)) { | |
2040 | free(path); | |
2041 | path = NULL; | |
2042 | continue; | |
2043 | } | |
2044 | ||
2045 | /* retrieve the scsi device type */ | |
2046 | if (asprintf(&device, "/sys/dev/block/%d:%d/device/xxxxxxx", major, minor) < 0) { | |
ba728be7 | 2047 | if (verbose > 0) |
e7b84f9d | 2048 | pr_err("failed to allocate 'device'\n"); |
d665cc31 DW |
2049 | err = 2; |
2050 | break; | |
2051 | } | |
2052 | sprintf(device, "/sys/dev/block/%d:%d/device/type", major, minor); | |
193b6c0b | 2053 | if (load_sys(device, buf, sizeof(buf)) != 0) { |
ba728be7 | 2054 | if (verbose > 0) |
e7b84f9d | 2055 | pr_err("failed to read device type for %s\n", |
d665cc31 DW |
2056 | path); |
2057 | err = 2; | |
2058 | free(device); | |
2059 | break; | |
2060 | } | |
2061 | type = strtoul(buf, NULL, 10); | |
2062 | ||
2063 | /* if it's not a disk print the vendor and model */ | |
2064 | if (!(type == 0 || type == 7 || type == 14)) { | |
2065 | vendor[0] = '\0'; | |
2066 | model[0] = '\0'; | |
2067 | sprintf(device, "/sys/dev/block/%d:%d/device/vendor", major, minor); | |
193b6c0b | 2068 | if (load_sys(device, buf, sizeof(buf)) == 0) { |
d665cc31 DW |
2069 | strncpy(vendor, buf, sizeof(vendor)); |
2070 | vendor[sizeof(vendor) - 1] = '\0'; | |
2071 | c = (char *) &vendor[sizeof(vendor) - 1]; | |
2072 | while (isspace(*c) || *c == '\0') | |
2073 | *c-- = '\0'; | |
2074 | ||
2075 | } | |
2076 | sprintf(device, "/sys/dev/block/%d:%d/device/model", major, minor); | |
193b6c0b | 2077 | if (load_sys(device, buf, sizeof(buf)) == 0) { |
d665cc31 DW |
2078 | strncpy(model, buf, sizeof(model)); |
2079 | model[sizeof(model) - 1] = '\0'; | |
2080 | c = (char *) &model[sizeof(model) - 1]; | |
2081 | while (isspace(*c) || *c == '\0') | |
2082 | *c-- = '\0'; | |
2083 | } | |
2084 | ||
2085 | if (vendor[0] && model[0]) | |
2086 | sprintf(buf, "%.64s %.64s", vendor, model); | |
2087 | else | |
2088 | switch (type) { /* numbers from hald/linux/device.c */ | |
2089 | case 1: sprintf(buf, "tape"); break; | |
2090 | case 2: sprintf(buf, "printer"); break; | |
2091 | case 3: sprintf(buf, "processor"); break; | |
2092 | case 4: | |
2093 | case 5: sprintf(buf, "cdrom"); break; | |
2094 | case 6: sprintf(buf, "scanner"); break; | |
2095 | case 8: sprintf(buf, "media_changer"); break; | |
2096 | case 9: sprintf(buf, "comm"); break; | |
2097 | case 12: sprintf(buf, "raid"); break; | |
2098 | default: sprintf(buf, "unknown"); | |
2099 | } | |
2100 | } else | |
2101 | buf[0] = '\0'; | |
2102 | free(device); | |
2103 | ||
2104 | /* chop device path to 'host%d' and calculate the port number */ | |
2105 | c = strchr(&path[hba_len], '/'); | |
4e5e717d | 2106 | if (!c) { |
ba728be7 | 2107 | if (verbose > 0) |
e7b84f9d | 2108 | pr_err("%s - invalid path name\n", path + hba_len); |
4e5e717d AW |
2109 | err = 2; |
2110 | break; | |
2111 | } | |
d665cc31 | 2112 | *c = '\0'; |
0858eccf AP |
2113 | if ((sscanf(&path[hba_len], "ata%d", &port) == 1) || |
2114 | ((sscanf(&path[hba_len], "host%d", &port) == 1))) | |
d665cc31 DW |
2115 | port -= host_base; |
2116 | else { | |
ba728be7 | 2117 | if (verbose > 0) { |
d665cc31 | 2118 | *c = '/'; /* repair the full string */ |
e7b84f9d | 2119 | pr_err("failed to determine port number for %s\n", |
d665cc31 DW |
2120 | path); |
2121 | } | |
2122 | err = 2; | |
2123 | break; | |
2124 | } | |
2125 | ||
2126 | /* mark this port as used */ | |
2127 | port_mask &= ~(1 << port); | |
2128 | ||
2129 | /* print out the device information */ | |
2130 | if (buf[0]) { | |
2131 | printf(" Port%d : - non-disk device (%s) -\n", port, buf); | |
2132 | continue; | |
2133 | } | |
2134 | ||
2135 | fd = dev_open(ent->d_name, O_RDONLY); | |
2136 | if (fd < 0) | |
2137 | printf(" Port%d : - disk info unavailable -\n", port); | |
2138 | else { | |
2139 | fd2devname(fd, buf); | |
2140 | printf(" Port%d : %s", port, buf); | |
2141 | if (imsm_read_serial(fd, NULL, (__u8 *) buf) == 0) | |
664d5325 | 2142 | printf(" (%.*s)\n", MAX_RAID_SERIAL_LEN, buf); |
d665cc31 | 2143 | else |
664d5325 | 2144 | printf(" ()\n"); |
4dab422a | 2145 | close(fd); |
d665cc31 | 2146 | } |
d665cc31 DW |
2147 | free(path); |
2148 | path = NULL; | |
2149 | } | |
2150 | if (path) | |
2151 | free(path); | |
2152 | if (dir) | |
2153 | closedir(dir); | |
2154 | if (err == 0) { | |
2155 | int i; | |
2156 | ||
2157 | for (i = 0; i < port_count; i++) | |
2158 | if (port_mask & (1 << i)) | |
2159 | printf(" Port%d : - no device attached -\n", i); | |
2160 | } | |
2161 | ||
2162 | return err; | |
2163 | } | |
2164 | ||
b5eece69 | 2165 | static int print_vmd_attached_devs(struct sys_dev *hba) |
60f0f54d PB |
2166 | { |
2167 | struct dirent *ent; | |
2168 | DIR *dir; | |
2169 | char path[292]; | |
2170 | char link[256]; | |
2171 | char *c, *rp; | |
2172 | ||
2173 | if (hba->type != SYS_DEV_VMD) | |
b5eece69 | 2174 | return 1; |
60f0f54d PB |
2175 | |
2176 | /* scroll through /sys/dev/block looking for devices attached to | |
2177 | * this hba | |
2178 | */ | |
2179 | dir = opendir("/sys/bus/pci/drivers/nvme"); | |
b9135011 | 2180 | if (!dir) |
b5eece69 | 2181 | return 1; |
b9135011 JS |
2182 | |
2183 | for (ent = readdir(dir); ent; ent = readdir(dir)) { | |
60f0f54d PB |
2184 | int n; |
2185 | ||
2186 | /* is 'ent' a device? check that the 'subsystem' link exists and | |
2187 | * that its target matches 'bus' | |
2188 | */ | |
2189 | sprintf(path, "/sys/bus/pci/drivers/nvme/%s/subsystem", | |
2190 | ent->d_name); | |
2191 | n = readlink(path, link, sizeof(link)); | |
2192 | if (n < 0 || n >= (int)sizeof(link)) | |
2193 | continue; | |
2194 | link[n] = '\0'; | |
2195 | c = strrchr(link, '/'); | |
2196 | if (!c) | |
2197 | continue; | |
2198 | if (strncmp("pci", c+1, strlen("pci")) != 0) | |
2199 | continue; | |
2200 | ||
2201 | sprintf(path, "/sys/bus/pci/drivers/nvme/%s", ent->d_name); | |
60f0f54d PB |
2202 | |
2203 | rp = realpath(path, NULL); | |
2204 | if (!rp) | |
2205 | continue; | |
2206 | ||
2207 | if (path_attached_to_hba(rp, hba->path)) { | |
2208 | printf(" NVMe under VMD : %s\n", rp); | |
2209 | } | |
2210 | free(rp); | |
2211 | } | |
2212 | ||
b9135011 | 2213 | closedir(dir); |
b5eece69 | 2214 | return 0; |
60f0f54d PB |
2215 | } |
2216 | ||
120dc887 LM |
2217 | static void print_found_intel_controllers(struct sys_dev *elem) |
2218 | { | |
2219 | for (; elem; elem = elem->next) { | |
e7b84f9d | 2220 | pr_err("found Intel(R) "); |
120dc887 LM |
2221 | if (elem->type == SYS_DEV_SATA) |
2222 | fprintf(stderr, "SATA "); | |
155cbb4c LM |
2223 | else if (elem->type == SYS_DEV_SAS) |
2224 | fprintf(stderr, "SAS "); | |
0858eccf AP |
2225 | else if (elem->type == SYS_DEV_NVME) |
2226 | fprintf(stderr, "NVMe "); | |
60f0f54d PB |
2227 | |
2228 | if (elem->type == SYS_DEV_VMD) | |
2229 | fprintf(stderr, "VMD domain"); | |
2230 | else | |
2231 | fprintf(stderr, "RAID controller"); | |
2232 | ||
120dc887 LM |
2233 | if (elem->pci_id) |
2234 | fprintf(stderr, " at %s", elem->pci_id); | |
2235 | fprintf(stderr, ".\n"); | |
2236 | } | |
2237 | fflush(stderr); | |
2238 | } | |
2239 | ||
120dc887 LM |
2240 | static int ahci_get_port_count(const char *hba_path, int *port_count) |
2241 | { | |
2242 | struct dirent *ent; | |
2243 | DIR *dir; | |
2244 | int host_base = -1; | |
2245 | ||
2246 | *port_count = 0; | |
2247 | if ((dir = opendir(hba_path)) == NULL) | |
2248 | return -1; | |
2249 | ||
2250 | for (ent = readdir(dir); ent; ent = readdir(dir)) { | |
2251 | int host; | |
2252 | ||
0858eccf AP |
2253 | if ((sscanf(ent->d_name, "ata%d", &host) != 1) && |
2254 | ((sscanf(ent->d_name, "host%d", &host) != 1))) | |
120dc887 LM |
2255 | continue; |
2256 | if (*port_count == 0) | |
2257 | host_base = host; | |
2258 | else if (host < host_base) | |
2259 | host_base = host; | |
2260 | ||
2261 | if (host + 1 > *port_count + host_base) | |
2262 | *port_count = host + 1 - host_base; | |
2263 | } | |
2264 | closedir(dir); | |
2265 | return host_base; | |
2266 | } | |
2267 | ||
a891a3c2 LM |
2268 | static void print_imsm_capability(const struct imsm_orom *orom) |
2269 | { | |
0858eccf AP |
2270 | printf(" Platform : Intel(R) "); |
2271 | if (orom->capabilities == 0 && orom->driver_features == 0) | |
2272 | printf("Matrix Storage Manager\n"); | |
2273 | else | |
2274 | printf("Rapid Storage Technology%s\n", | |
2275 | imsm_orom_is_enterprise(orom) ? " enterprise" : ""); | |
2276 | if (orom->major_ver || orom->minor_ver || orom->hotfix_ver || orom->build) | |
2277 | printf(" Version : %d.%d.%d.%d\n", orom->major_ver, | |
2278 | orom->minor_ver, orom->hotfix_ver, orom->build); | |
a891a3c2 LM |
2279 | printf(" RAID Levels :%s%s%s%s%s\n", |
2280 | imsm_orom_has_raid0(orom) ? " raid0" : "", | |
2281 | imsm_orom_has_raid1(orom) ? " raid1" : "", | |
2282 | imsm_orom_has_raid1e(orom) ? " raid1e" : "", | |
2283 | imsm_orom_has_raid10(orom) ? " raid10" : "", | |
2284 | imsm_orom_has_raid5(orom) ? " raid5" : ""); | |
2285 | printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n", | |
2286 | imsm_orom_has_chunk(orom, 2) ? " 2k" : "", | |
2287 | imsm_orom_has_chunk(orom, 4) ? " 4k" : "", | |
2288 | imsm_orom_has_chunk(orom, 8) ? " 8k" : "", | |
2289 | imsm_orom_has_chunk(orom, 16) ? " 16k" : "", | |
2290 | imsm_orom_has_chunk(orom, 32) ? " 32k" : "", | |
2291 | imsm_orom_has_chunk(orom, 64) ? " 64k" : "", | |
2292 | imsm_orom_has_chunk(orom, 128) ? " 128k" : "", | |
2293 | imsm_orom_has_chunk(orom, 256) ? " 256k" : "", | |
2294 | imsm_orom_has_chunk(orom, 512) ? " 512k" : "", | |
2295 | imsm_orom_has_chunk(orom, 1024*1) ? " 1M" : "", | |
2296 | imsm_orom_has_chunk(orom, 1024*2) ? " 2M" : "", | |
2297 | imsm_orom_has_chunk(orom, 1024*4) ? " 4M" : "", | |
2298 | imsm_orom_has_chunk(orom, 1024*8) ? " 8M" : "", | |
2299 | imsm_orom_has_chunk(orom, 1024*16) ? " 16M" : "", | |
2300 | imsm_orom_has_chunk(orom, 1024*32) ? " 32M" : "", | |
2301 | imsm_orom_has_chunk(orom, 1024*64) ? " 64M" : ""); | |
29cd0821 CA |
2302 | printf(" 2TB volumes :%s supported\n", |
2303 | (orom->attr & IMSM_OROM_ATTR_2TB)?"":" not"); | |
2304 | printf(" 2TB disks :%s supported\n", | |
2305 | (orom->attr & IMSM_OROM_ATTR_2TB_DISK)?"":" not"); | |
0e7f69a8 | 2306 | printf(" Max Disks : %d\n", orom->tds); |
0858eccf AP |
2307 | printf(" Max Volumes : %d per array, %d per %s\n", |
2308 | orom->vpa, orom->vphba, | |
2309 | imsm_orom_is_nvme(orom) ? "platform" : "controller"); | |
a891a3c2 LM |
2310 | return; |
2311 | } | |
2312 | ||
e50cf220 MN |
2313 | static void print_imsm_capability_export(const struct imsm_orom *orom) |
2314 | { | |
2315 | printf("MD_FIRMWARE_TYPE=imsm\n"); | |
0858eccf AP |
2316 | if (orom->major_ver || orom->minor_ver || orom->hotfix_ver || orom->build) |
2317 | printf("IMSM_VERSION=%d.%d.%d.%d\n", orom->major_ver, orom->minor_ver, | |
2318 | orom->hotfix_ver, orom->build); | |
e50cf220 MN |
2319 | printf("IMSM_SUPPORTED_RAID_LEVELS=%s%s%s%s%s\n", |
2320 | imsm_orom_has_raid0(orom) ? "raid0 " : "", | |
2321 | imsm_orom_has_raid1(orom) ? "raid1 " : "", | |
2322 | imsm_orom_has_raid1e(orom) ? "raid1e " : "", | |
2323 | imsm_orom_has_raid5(orom) ? "raid10 " : "", | |
2324 | imsm_orom_has_raid10(orom) ? "raid5 " : ""); | |
2325 | printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n", | |
2326 | imsm_orom_has_chunk(orom, 2) ? "2k " : "", | |
2327 | imsm_orom_has_chunk(orom, 4) ? "4k " : "", | |
2328 | imsm_orom_has_chunk(orom, 8) ? "8k " : "", | |
2329 | imsm_orom_has_chunk(orom, 16) ? "16k " : "", | |
2330 | imsm_orom_has_chunk(orom, 32) ? "32k " : "", | |
2331 | imsm_orom_has_chunk(orom, 64) ? "64k " : "", | |
2332 | imsm_orom_has_chunk(orom, 128) ? "128k " : "", | |
2333 | imsm_orom_has_chunk(orom, 256) ? "256k " : "", | |
2334 | imsm_orom_has_chunk(orom, 512) ? "512k " : "", | |
2335 | imsm_orom_has_chunk(orom, 1024*1) ? "1M " : "", | |
2336 | imsm_orom_has_chunk(orom, 1024*2) ? "2M " : "", | |
2337 | imsm_orom_has_chunk(orom, 1024*4) ? "4M " : "", | |
2338 | imsm_orom_has_chunk(orom, 1024*8) ? "8M " : "", | |
2339 | imsm_orom_has_chunk(orom, 1024*16) ? "16M " : "", | |
2340 | imsm_orom_has_chunk(orom, 1024*32) ? "32M " : "", | |
2341 | imsm_orom_has_chunk(orom, 1024*64) ? "64M " : ""); | |
2342 | printf("IMSM_2TB_VOLUMES=%s\n",(orom->attr & IMSM_OROM_ATTR_2TB) ? "yes" : "no"); | |
2343 | printf("IMSM_2TB_DISKS=%s\n",(orom->attr & IMSM_OROM_ATTR_2TB_DISK) ? "yes" : "no"); | |
2344 | printf("IMSM_MAX_DISKS=%d\n",orom->tds); | |
2345 | printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom->vpa); | |
2346 | printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom->vphba); | |
2347 | } | |
2348 | ||
9eafa1de | 2349 | static int detail_platform_imsm(int verbose, int enumerate_only, char *controller_path) |
d665cc31 DW |
2350 | { |
2351 | /* There are two components to imsm platform support, the ahci SATA | |
2352 | * controller and the option-rom. To find the SATA controller we | |
2353 | * simply look in /sys/bus/pci/drivers/ahci to see if an ahci | |
2354 | * controller with the Intel vendor id is present. This approach | |
2355 | * allows mdadm to leverage the kernel's ahci detection logic, with the | |
2356 | * caveat that if ahci.ko is not loaded mdadm will not be able to | |
2357 | * detect platform raid capabilities. The option-rom resides in a | |
2358 | * platform "Adapter ROM". We scan for its signature to retrieve the | |
2359 | * platform capabilities. If raid support is disabled in the BIOS the | |
2360 | * option-rom capability structure will not be available. | |
2361 | */ | |
d665cc31 | 2362 | struct sys_dev *list, *hba; |
d665cc31 DW |
2363 | int host_base = 0; |
2364 | int port_count = 0; | |
9eafa1de | 2365 | int result=1; |
d665cc31 | 2366 | |
5615172f | 2367 | if (enumerate_only) { |
a891a3c2 | 2368 | if (check_env("IMSM_NO_PLATFORM")) |
5615172f | 2369 | return 0; |
a891a3c2 LM |
2370 | list = find_intel_devices(); |
2371 | if (!list) | |
2372 | return 2; | |
2373 | for (hba = list; hba; hba = hba->next) { | |
6b781d33 AP |
2374 | if (find_imsm_capability(hba)) { |
2375 | result = 0; | |
a891a3c2 LM |
2376 | break; |
2377 | } | |
9eafa1de | 2378 | else |
6b781d33 | 2379 | result = 2; |
a891a3c2 | 2380 | } |
a891a3c2 | 2381 | return result; |
5615172f DW |
2382 | } |
2383 | ||
155cbb4c LM |
2384 | list = find_intel_devices(); |
2385 | if (!list) { | |
ba728be7 | 2386 | if (verbose > 0) |
7a862a02 | 2387 | pr_err("no active Intel(R) RAID controller found.\n"); |
d665cc31 | 2388 | return 2; |
ba728be7 | 2389 | } else if (verbose > 0) |
155cbb4c | 2390 | print_found_intel_controllers(list); |
d665cc31 | 2391 | |
a891a3c2 | 2392 | for (hba = list; hba; hba = hba->next) { |
0858eccf | 2393 | if (controller_path && (compare_paths(hba->path, controller_path) != 0)) |
9eafa1de | 2394 | continue; |
0858eccf | 2395 | if (!find_imsm_capability(hba)) { |
60f0f54d | 2396 | char buf[PATH_MAX]; |
e7b84f9d | 2397 | pr_err("imsm capabilities not found for controller: %s (type %s)\n", |
60f0f54d PB |
2398 | hba->type == SYS_DEV_VMD ? vmd_domain_to_controller(hba, buf) : hba->path, |
2399 | get_sys_dev_type(hba->type)); | |
0858eccf AP |
2400 | continue; |
2401 | } | |
2402 | result = 0; | |
2403 | } | |
2404 | ||
2405 | if (controller_path && result == 1) { | |
2406 | pr_err("no active Intel(R) RAID controller found under %s\n", | |
2407 | controller_path); | |
2408 | return result; | |
2409 | } | |
2410 | ||
5e1d6128 | 2411 | const struct orom_entry *entry; |
0858eccf | 2412 | |
5e1d6128 | 2413 | for (entry = orom_entries; entry; entry = entry->next) { |
60f0f54d | 2414 | if (entry->type == SYS_DEV_VMD) { |
07cb1e57 | 2415 | print_imsm_capability(&entry->orom); |
32716c51 PB |
2416 | printf(" 3rd party NVMe :%s supported\n", |
2417 | imsm_orom_has_tpv_support(&entry->orom)?"":" not"); | |
60f0f54d PB |
2418 | for (hba = list; hba; hba = hba->next) { |
2419 | if (hba->type == SYS_DEV_VMD) { | |
2420 | char buf[PATH_MAX]; | |
60f0f54d PB |
2421 | printf(" I/O Controller : %s (%s)\n", |
2422 | vmd_domain_to_controller(hba, buf), get_sys_dev_type(hba->type)); | |
b5eece69 PB |
2423 | if (print_vmd_attached_devs(hba)) { |
2424 | if (verbose > 0) | |
2425 | pr_err("failed to get devices attached to VMD domain.\n"); | |
2426 | result |= 2; | |
2427 | } | |
60f0f54d PB |
2428 | } |
2429 | } | |
07cb1e57 | 2430 | printf("\n"); |
60f0f54d PB |
2431 | continue; |
2432 | } | |
0858eccf | 2433 | |
60f0f54d PB |
2434 | print_imsm_capability(&entry->orom); |
2435 | if (entry->type == SYS_DEV_NVME) { | |
0858eccf AP |
2436 | for (hba = list; hba; hba = hba->next) { |
2437 | if (hba->type == SYS_DEV_NVME) | |
2438 | printf(" NVMe Device : %s\n", hba->path); | |
2439 | } | |
60f0f54d | 2440 | printf("\n"); |
0858eccf AP |
2441 | continue; |
2442 | } | |
2443 | ||
2444 | struct devid_list *devid; | |
5e1d6128 | 2445 | for (devid = entry->devid_list; devid; devid = devid->next) { |
0858eccf AP |
2446 | hba = device_by_id(devid->devid); |
2447 | if (!hba) | |
2448 | continue; | |
2449 | ||
9eafa1de MN |
2450 | printf(" I/O Controller : %s (%s)\n", |
2451 | hba->path, get_sys_dev_type(hba->type)); | |
2452 | if (hba->type == SYS_DEV_SATA) { | |
2453 | host_base = ahci_get_port_count(hba->path, &port_count); | |
2454 | if (ahci_enumerate_ports(hba->path, port_count, host_base, verbose)) { | |
2455 | if (verbose > 0) | |
7a862a02 | 2456 | pr_err("failed to enumerate ports on SATA controller at %s.\n", hba->pci_id); |
9eafa1de MN |
2457 | result |= 2; |
2458 | } | |
120dc887 LM |
2459 | } |
2460 | } | |
0858eccf | 2461 | printf("\n"); |
d665cc31 | 2462 | } |
155cbb4c | 2463 | |
120dc887 | 2464 | return result; |
d665cc31 | 2465 | } |
e50cf220 | 2466 | |
9eafa1de | 2467 | static int export_detail_platform_imsm(int verbose, char *controller_path) |
e50cf220 | 2468 | { |
e50cf220 MN |
2469 | struct sys_dev *list, *hba; |
2470 | int result=1; | |
2471 | ||
2472 | list = find_intel_devices(); | |
2473 | if (!list) { | |
2474 | if (verbose > 0) | |
2475 | pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n"); | |
2476 | result = 2; | |
e50cf220 MN |
2477 | return result; |
2478 | } | |
2479 | ||
2480 | for (hba = list; hba; hba = hba->next) { | |
9eafa1de MN |
2481 | if (controller_path && (compare_paths(hba->path,controller_path) != 0)) |
2482 | continue; | |
60f0f54d PB |
2483 | if (!find_imsm_capability(hba) && verbose > 0) { |
2484 | char buf[PATH_MAX]; | |
2485 | pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n", | |
2486 | hba->type == SYS_DEV_VMD ? vmd_domain_to_controller(hba, buf) : hba->path); | |
2487 | } | |
0858eccf | 2488 | else |
e50cf220 | 2489 | result = 0; |
e50cf220 MN |
2490 | } |
2491 | ||
5e1d6128 | 2492 | const struct orom_entry *entry; |
0858eccf | 2493 | |
60f0f54d PB |
2494 | for (entry = orom_entries; entry; entry = entry->next) { |
2495 | if (entry->type == SYS_DEV_VMD) { | |
2496 | for (hba = list; hba; hba = hba->next) | |
2497 | print_imsm_capability_export(&entry->orom); | |
2498 | continue; | |
2499 | } | |
5e1d6128 | 2500 | print_imsm_capability_export(&entry->orom); |
60f0f54d | 2501 | } |
0858eccf | 2502 | |
e50cf220 MN |
2503 | return result; |
2504 | } | |
2505 | ||
cdddbdbc DW |
2506 | #endif |
2507 | ||
2508 | static int match_home_imsm(struct supertype *st, char *homehost) | |
2509 | { | |
5115ca67 DW |
2510 | /* the imsm metadata format does not specify any host |
2511 | * identification information. We return -1 since we can never | |
2512 | * confirm nor deny whether a given array is "meant" for this | |
148acb7b | 2513 | * host. We rely on compare_super and the 'family_num' fields to |
5115ca67 DW |
2514 | * exclude member disks that do not belong, and we rely on |
2515 | * mdadm.conf to specify the arrays that should be assembled. | |
2516 | * Auto-assembly may still pick up "foreign" arrays. | |
2517 | */ | |
cdddbdbc | 2518 | |
9362c1c8 | 2519 | return -1; |
cdddbdbc DW |
2520 | } |
2521 | ||
2522 | static void uuid_from_super_imsm(struct supertype *st, int uuid[4]) | |
2523 | { | |
51006d85 N |
2524 | /* The uuid returned here is used for: |
2525 | * uuid to put into bitmap file (Create, Grow) | |
2526 | * uuid for backup header when saving critical section (Grow) | |
2527 | * comparing uuids when re-adding a device into an array | |
2528 | * In these cases the uuid required is that of the data-array, | |
2529 | * not the device-set. | |
2530 | * uuid to recognise same set when adding a missing device back | |
2531 | * to an array. This is a uuid for the device-set. | |
1011e834 | 2532 | * |
51006d85 N |
2533 | * For each of these we can make do with a truncated |
2534 | * or hashed uuid rather than the original, as long as | |
2535 | * everyone agrees. | |
2536 | * In each case the uuid required is that of the data-array, | |
2537 | * not the device-set. | |
43dad3d6 | 2538 | */ |
51006d85 N |
2539 | /* imsm does not track uuid's so we synthesis one using sha1 on |
2540 | * - The signature (Which is constant for all imsm array, but no matter) | |
148acb7b | 2541 | * - the orig_family_num of the container |
51006d85 N |
2542 | * - the index number of the volume |
2543 | * - the 'serial' number of the volume. | |
2544 | * Hopefully these are all constant. | |
2545 | */ | |
2546 | struct intel_super *super = st->sb; | |
43dad3d6 | 2547 | |
51006d85 N |
2548 | char buf[20]; |
2549 | struct sha1_ctx ctx; | |
2550 | struct imsm_dev *dev = NULL; | |
148acb7b | 2551 | __u32 family_num; |
51006d85 | 2552 | |
148acb7b DW |
2553 | /* some mdadm versions failed to set ->orig_family_num, in which |
2554 | * case fall back to ->family_num. orig_family_num will be | |
2555 | * fixed up with the first metadata update. | |
2556 | */ | |
2557 | family_num = super->anchor->orig_family_num; | |
2558 | if (family_num == 0) | |
2559 | family_num = super->anchor->family_num; | |
51006d85 | 2560 | sha1_init_ctx(&ctx); |
92bd8f8d | 2561 | sha1_process_bytes(super->anchor->sig, MPB_SIG_LEN, &ctx); |
148acb7b | 2562 | sha1_process_bytes(&family_num, sizeof(__u32), &ctx); |
51006d85 N |
2563 | if (super->current_vol >= 0) |
2564 | dev = get_imsm_dev(super, super->current_vol); | |
2565 | if (dev) { | |
2566 | __u32 vol = super->current_vol; | |
2567 | sha1_process_bytes(&vol, sizeof(vol), &ctx); | |
2568 | sha1_process_bytes(dev->volume, MAX_RAID_SERIAL_LEN, &ctx); | |
2569 | } | |
2570 | sha1_finish_ctx(&ctx, buf); | |
2571 | memcpy(uuid, buf, 4*4); | |
cdddbdbc DW |
2572 | } |
2573 | ||
0d481d37 | 2574 | #if 0 |
4f5bc454 DW |
2575 | static void |
2576 | get_imsm_numerical_version(struct imsm_super *mpb, int *m, int *p) | |
cdddbdbc | 2577 | { |
cdddbdbc DW |
2578 | __u8 *v = get_imsm_version(mpb); |
2579 | __u8 *end = mpb->sig + MAX_SIGNATURE_LENGTH; | |
2580 | char major[] = { 0, 0, 0 }; | |
2581 | char minor[] = { 0 ,0, 0 }; | |
2582 | char patch[] = { 0, 0, 0 }; | |
2583 | char *ver_parse[] = { major, minor, patch }; | |
2584 | int i, j; | |
2585 | ||
2586 | i = j = 0; | |
2587 | while (*v != '\0' && v < end) { | |
2588 | if (*v != '.' && j < 2) | |
2589 | ver_parse[i][j++] = *v; | |
2590 | else { | |
2591 | i++; | |
2592 | j = 0; | |
2593 | } | |
2594 | v++; | |
2595 | } | |
2596 | ||
4f5bc454 DW |
2597 | *m = strtol(minor, NULL, 0); |
2598 | *p = strtol(patch, NULL, 0); | |
2599 | } | |
0d481d37 | 2600 | #endif |
4f5bc454 | 2601 | |
1e5c6983 DW |
2602 | static __u32 migr_strip_blocks_resync(struct imsm_dev *dev) |
2603 | { | |
2604 | /* migr_strip_size when repairing or initializing parity */ | |
238c0a71 | 2605 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
1e5c6983 DW |
2606 | __u32 chunk = __le32_to_cpu(map->blocks_per_strip); |
2607 | ||
2608 | switch (get_imsm_raid_level(map)) { | |
2609 | case 5: | |
2610 | case 10: | |
2611 | return chunk; | |
2612 | default: | |
2613 | return 128*1024 >> 9; | |
2614 | } | |
2615 | } | |
2616 | ||
2617 | static __u32 migr_strip_blocks_rebuild(struct imsm_dev *dev) | |
2618 | { | |
2619 | /* migr_strip_size when rebuilding a degraded disk, no idea why | |
2620 | * this is different than migr_strip_size_resync(), but it's good | |
2621 | * to be compatible | |
2622 | */ | |
238c0a71 | 2623 | struct imsm_map *map = get_imsm_map(dev, MAP_1); |
1e5c6983 DW |
2624 | __u32 chunk = __le32_to_cpu(map->blocks_per_strip); |
2625 | ||
2626 | switch (get_imsm_raid_level(map)) { | |
2627 | case 1: | |
2628 | case 10: | |
2629 | if (map->num_members % map->num_domains == 0) | |
2630 | return 128*1024 >> 9; | |
2631 | else | |
2632 | return chunk; | |
2633 | case 5: | |
2634 | return max((__u32) 64*1024 >> 9, chunk); | |
2635 | default: | |
2636 | return 128*1024 >> 9; | |
2637 | } | |
2638 | } | |
2639 | ||
2640 | static __u32 num_stripes_per_unit_resync(struct imsm_dev *dev) | |
2641 | { | |
238c0a71 AK |
2642 | struct imsm_map *lo = get_imsm_map(dev, MAP_0); |
2643 | struct imsm_map *hi = get_imsm_map(dev, MAP_1); | |
1e5c6983 DW |
2644 | __u32 lo_chunk = __le32_to_cpu(lo->blocks_per_strip); |
2645 | __u32 hi_chunk = __le32_to_cpu(hi->blocks_per_strip); | |
2646 | ||
2647 | return max((__u32) 1, hi_chunk / lo_chunk); | |
2648 | } | |
2649 | ||
2650 | static __u32 num_stripes_per_unit_rebuild(struct imsm_dev *dev) | |
2651 | { | |
238c0a71 | 2652 | struct imsm_map *lo = get_imsm_map(dev, MAP_0); |
1e5c6983 DW |
2653 | int level = get_imsm_raid_level(lo); |
2654 | ||
2655 | if (level == 1 || level == 10) { | |
238c0a71 | 2656 | struct imsm_map *hi = get_imsm_map(dev, MAP_1); |
1e5c6983 DW |
2657 | |
2658 | return hi->num_domains; | |
2659 | } else | |
2660 | return num_stripes_per_unit_resync(dev); | |
2661 | } | |
2662 | ||
98130f40 | 2663 | static __u8 imsm_num_data_members(struct imsm_dev *dev, int second_map) |
1e5c6983 DW |
2664 | { |
2665 | /* named 'imsm_' because raid0, raid1 and raid10 | |
2666 | * counter-intuitively have the same number of data disks | |
2667 | */ | |
98130f40 | 2668 | struct imsm_map *map = get_imsm_map(dev, second_map); |
1e5c6983 DW |
2669 | |
2670 | switch (get_imsm_raid_level(map)) { | |
2671 | case 0: | |
36fd8ccc AK |
2672 | return map->num_members; |
2673 | break; | |
1e5c6983 DW |
2674 | case 1: |
2675 | case 10: | |
36fd8ccc | 2676 | return map->num_members/2; |
1e5c6983 DW |
2677 | case 5: |
2678 | return map->num_members - 1; | |
2679 | default: | |
1ade5cc1 | 2680 | dprintf("unsupported raid level\n"); |
1e5c6983 DW |
2681 | return 0; |
2682 | } | |
2683 | } | |
2684 | ||
2685 | static __u32 parity_segment_depth(struct imsm_dev *dev) | |
2686 | { | |
238c0a71 | 2687 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
1e5c6983 DW |
2688 | __u32 chunk = __le32_to_cpu(map->blocks_per_strip); |
2689 | ||
2690 | switch(get_imsm_raid_level(map)) { | |
2691 | case 1: | |
2692 | case 10: | |
2693 | return chunk * map->num_domains; | |
2694 | case 5: | |
2695 | return chunk * map->num_members; | |
2696 | default: | |
2697 | return chunk; | |
2698 | } | |
2699 | } | |
2700 | ||
2701 | static __u32 map_migr_block(struct imsm_dev *dev, __u32 block) | |
2702 | { | |
238c0a71 | 2703 | struct imsm_map *map = get_imsm_map(dev, MAP_1); |
1e5c6983 DW |
2704 | __u32 chunk = __le32_to_cpu(map->blocks_per_strip); |
2705 | __u32 strip = block / chunk; | |
2706 | ||
2707 | switch (get_imsm_raid_level(map)) { | |
2708 | case 1: | |
2709 | case 10: { | |
2710 | __u32 vol_strip = (strip * map->num_domains) + 1; | |
2711 | __u32 vol_stripe = vol_strip / map->num_members; | |
2712 | ||
2713 | return vol_stripe * chunk + block % chunk; | |
2714 | } case 5: { | |
2715 | __u32 stripe = strip / (map->num_members - 1); | |
2716 | ||
2717 | return stripe * chunk + block % chunk; | |
2718 | } | |
2719 | default: | |
2720 | return 0; | |
2721 | } | |
2722 | } | |
2723 | ||
c47b0ff6 AK |
2724 | static __u64 blocks_per_migr_unit(struct intel_super *super, |
2725 | struct imsm_dev *dev) | |
1e5c6983 DW |
2726 | { |
2727 | /* calculate the conversion factor between per member 'blocks' | |
2728 | * (md/{resync,rebuild}_start) and imsm migration units, return | |
2729 | * 0 for the 'not migrating' and 'unsupported migration' cases | |
2730 | */ | |
2731 | if (!dev->vol.migr_state) | |
2732 | return 0; | |
2733 | ||
2734 | switch (migr_type(dev)) { | |
c47b0ff6 AK |
2735 | case MIGR_GEN_MIGR: { |
2736 | struct migr_record *migr_rec = super->migr_rec; | |
2737 | return __le32_to_cpu(migr_rec->blocks_per_unit); | |
2738 | } | |
1e5c6983 DW |
2739 | case MIGR_VERIFY: |
2740 | case MIGR_REPAIR: | |
2741 | case MIGR_INIT: { | |
238c0a71 | 2742 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
1e5c6983 DW |
2743 | __u32 stripes_per_unit; |
2744 | __u32 blocks_per_unit; | |
2745 | __u32 parity_depth; | |
2746 | __u32 migr_chunk; | |
2747 | __u32 block_map; | |
2748 | __u32 block_rel; | |
2749 | __u32 segment; | |
2750 | __u32 stripe; | |
2751 | __u8 disks; | |
2752 | ||
2753 | /* yes, this is really the translation of migr_units to | |
2754 | * per-member blocks in the 'resync' case | |
2755 | */ | |
2756 | stripes_per_unit = num_stripes_per_unit_resync(dev); | |
2757 | migr_chunk = migr_strip_blocks_resync(dev); | |
238c0a71 | 2758 | disks = imsm_num_data_members(dev, MAP_0); |
1e5c6983 | 2759 | blocks_per_unit = stripes_per_unit * migr_chunk * disks; |
7b1ab482 | 2760 | stripe = __le16_to_cpu(map->blocks_per_strip) * disks; |
1e5c6983 DW |
2761 | segment = blocks_per_unit / stripe; |
2762 | block_rel = blocks_per_unit - segment * stripe; | |
2763 | parity_depth = parity_segment_depth(dev); | |
2764 | block_map = map_migr_block(dev, block_rel); | |
2765 | return block_map + parity_depth * segment; | |
2766 | } | |
2767 | case MIGR_REBUILD: { | |
2768 | __u32 stripes_per_unit; | |
2769 | __u32 migr_chunk; | |
2770 | ||
2771 | stripes_per_unit = num_stripes_per_unit_rebuild(dev); | |
2772 | migr_chunk = migr_strip_blocks_rebuild(dev); | |
2773 | return migr_chunk * stripes_per_unit; | |
2774 | } | |
1e5c6983 DW |
2775 | case MIGR_STATE_CHANGE: |
2776 | default: | |
2777 | return 0; | |
2778 | } | |
2779 | } | |
2780 | ||
c2c087e6 DW |
2781 | static int imsm_level_to_layout(int level) |
2782 | { | |
2783 | switch (level) { | |
2784 | case 0: | |
2785 | case 1: | |
2786 | return 0; | |
2787 | case 5: | |
2788 | case 6: | |
a380c027 | 2789 | return ALGORITHM_LEFT_ASYMMETRIC; |
c2c087e6 | 2790 | case 10: |
c92a2527 | 2791 | return 0x102; |
c2c087e6 | 2792 | } |
a18a888e | 2793 | return UnSet; |
c2c087e6 DW |
2794 | } |
2795 | ||
8e59f3d8 AK |
2796 | /******************************************************************************* |
2797 | * Function: read_imsm_migr_rec | |
2798 | * Description: Function reads imsm migration record from last sector of disk | |
2799 | * Parameters: | |
2800 | * fd : disk descriptor | |
2801 | * super : metadata info | |
2802 | * Returns: | |
2803 | * 0 : success, | |
2804 | * -1 : fail | |
2805 | ******************************************************************************/ | |
2806 | static int read_imsm_migr_rec(int fd, struct intel_super *super) | |
2807 | { | |
2808 | int ret_val = -1; | |
de44e46f | 2809 | unsigned int sector_size = super->sector_size; |
8e59f3d8 AK |
2810 | unsigned long long dsize; |
2811 | ||
2812 | get_dev_size(fd, NULL, &dsize); | |
de44e46f PB |
2813 | if (lseek64(fd, dsize - (sector_size*MIGR_REC_SECTOR_POSITION), |
2814 | SEEK_SET) < 0) { | |
e7b84f9d N |
2815 | pr_err("Cannot seek to anchor block: %s\n", |
2816 | strerror(errno)); | |
8e59f3d8 AK |
2817 | goto out; |
2818 | } | |
de44e46f PB |
2819 | if (read(fd, super->migr_rec_buf, |
2820 | MIGR_REC_BUF_SECTORS*sector_size) != | |
2821 | MIGR_REC_BUF_SECTORS*sector_size) { | |
e7b84f9d N |
2822 | pr_err("Cannot read migr record block: %s\n", |
2823 | strerror(errno)); | |
8e59f3d8 AK |
2824 | goto out; |
2825 | } | |
2826 | ret_val = 0; | |
de44e46f PB |
2827 | if (sector_size == 4096) |
2828 | convert_from_4k_imsm_migr_rec(super); | |
8e59f3d8 AK |
2829 | |
2830 | out: | |
2831 | return ret_val; | |
2832 | } | |
2833 | ||
3136abe5 AK |
2834 | static struct imsm_dev *imsm_get_device_during_migration( |
2835 | struct intel_super *super) | |
2836 | { | |
2837 | ||
2838 | struct intel_dev *dv; | |
2839 | ||
2840 | for (dv = super->devlist; dv; dv = dv->next) { | |
2841 | if (is_gen_migration(dv->dev)) | |
2842 | return dv->dev; | |
2843 | } | |
2844 | return NULL; | |
2845 | } | |
2846 | ||
8e59f3d8 AK |
2847 | /******************************************************************************* |
2848 | * Function: load_imsm_migr_rec | |
2849 | * Description: Function reads imsm migration record (it is stored at the last | |
2850 | * sector of disk) | |
2851 | * Parameters: | |
2852 | * super : imsm internal array info | |
2853 | * info : general array info | |
2854 | * Returns: | |
2855 | * 0 : success | |
2856 | * -1 : fail | |
4c965cc9 | 2857 | * -2 : no migration in progress |
8e59f3d8 AK |
2858 | ******************************************************************************/ |
2859 | static int load_imsm_migr_rec(struct intel_super *super, struct mdinfo *info) | |
2860 | { | |
2861 | struct mdinfo *sd; | |
594dc1b8 | 2862 | struct dl *dl; |
8e59f3d8 AK |
2863 | char nm[30]; |
2864 | int retval = -1; | |
2865 | int fd = -1; | |
3136abe5 | 2866 | struct imsm_dev *dev; |
594dc1b8 | 2867 | struct imsm_map *map; |
b4ab44d8 | 2868 | int slot = -1; |
3136abe5 AK |
2869 | |
2870 | /* find map under migration */ | |
2871 | dev = imsm_get_device_during_migration(super); | |
2872 | /* nothing to load,no migration in progress? | |
2873 | */ | |
2874 | if (dev == NULL) | |
4c965cc9 | 2875 | return -2; |
8e59f3d8 AK |
2876 | |
2877 | if (info) { | |
2878 | for (sd = info->devs ; sd ; sd = sd->next) { | |
2879 | /* read only from one of the first two slots */ | |
12fe93e9 TM |
2880 | if ((sd->disk.raid_disk < 0) || |
2881 | (sd->disk.raid_disk > 1)) | |
8e59f3d8 | 2882 | continue; |
3136abe5 | 2883 | |
8e59f3d8 AK |
2884 | sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor); |
2885 | fd = dev_open(nm, O_RDONLY); | |
2886 | if (fd >= 0) | |
2887 | break; | |
2888 | } | |
2889 | } | |
2890 | if (fd < 0) { | |
12fe93e9 | 2891 | map = get_imsm_map(dev, MAP_0); |
8e59f3d8 | 2892 | for (dl = super->disks; dl; dl = dl->next) { |
3136abe5 AK |
2893 | /* skip spare and failed disks |
2894 | */ | |
2895 | if (dl->index < 0) | |
2896 | continue; | |
8e59f3d8 | 2897 | /* read only from one of the first two slots */ |
3136abe5 AK |
2898 | if (map) |
2899 | slot = get_imsm_disk_slot(map, dl->index); | |
089f9d79 | 2900 | if (map == NULL || slot > 1 || slot < 0) |
8e59f3d8 AK |
2901 | continue; |
2902 | sprintf(nm, "%d:%d", dl->major, dl->minor); | |
2903 | fd = dev_open(nm, O_RDONLY); | |
2904 | if (fd >= 0) | |
2905 | break; | |
2906 | } | |
2907 | } | |
2908 | if (fd < 0) | |
2909 | goto out; | |
2910 | retval = read_imsm_migr_rec(fd, super); | |
2911 | ||
2912 | out: | |
2913 | if (fd >= 0) | |
2914 | close(fd); | |
2915 | return retval; | |
2916 | } | |
2917 | ||
9e2d750d | 2918 | #ifndef MDASSEMBLE |
c17608ea AK |
2919 | /******************************************************************************* |
2920 | * function: imsm_create_metadata_checkpoint_update | |
2921 | * Description: It creates update for checkpoint change. | |
2922 | * Parameters: | |
2923 | * super : imsm internal array info | |
2924 | * u : pointer to prepared update | |
2925 | * Returns: | |
2926 | * Uptate length. | |
2927 | * If length is equal to 0, input pointer u contains no update | |
2928 | ******************************************************************************/ | |
2929 | static int imsm_create_metadata_checkpoint_update( | |
2930 | struct intel_super *super, | |
2931 | struct imsm_update_general_migration_checkpoint **u) | |
2932 | { | |
2933 | ||
2934 | int update_memory_size = 0; | |
2935 | ||
1ade5cc1 | 2936 | dprintf("(enter)\n"); |
c17608ea AK |
2937 | |
2938 | if (u == NULL) | |
2939 | return 0; | |
2940 | *u = NULL; | |
2941 | ||
2942 | /* size of all update data without anchor */ | |
2943 | update_memory_size = | |
2944 | sizeof(struct imsm_update_general_migration_checkpoint); | |
2945 | ||
503975b9 | 2946 | *u = xcalloc(1, update_memory_size); |
c17608ea | 2947 | if (*u == NULL) { |
1ade5cc1 | 2948 | dprintf("error: cannot get memory\n"); |
c17608ea AK |
2949 | return 0; |
2950 | } | |
2951 | (*u)->type = update_general_migration_checkpoint; | |
2952 | (*u)->curr_migr_unit = __le32_to_cpu(super->migr_rec->curr_migr_unit); | |
1ade5cc1 | 2953 | dprintf("prepared for %u\n", (*u)->curr_migr_unit); |
c17608ea AK |
2954 | |
2955 | return update_memory_size; | |
2956 | } | |
2957 | ||
c17608ea AK |
2958 | static void imsm_update_metadata_locally(struct supertype *st, |
2959 | void *buf, int len); | |
2960 | ||
687629c2 AK |
2961 | /******************************************************************************* |
2962 | * Function: write_imsm_migr_rec | |
2963 | * Description: Function writes imsm migration record | |
2964 | * (at the last sector of disk) | |
2965 | * Parameters: | |
2966 | * super : imsm internal array info | |
2967 | * Returns: | |
2968 | * 0 : success | |
2969 | * -1 : if fail | |
2970 | ******************************************************************************/ | |
2971 | static int write_imsm_migr_rec(struct supertype *st) | |
2972 | { | |
2973 | struct intel_super *super = st->sb; | |
de44e46f | 2974 | unsigned int sector_size = super->sector_size; |
687629c2 AK |
2975 | unsigned long long dsize; |
2976 | char nm[30]; | |
2977 | int fd = -1; | |
2978 | int retval = -1; | |
2979 | struct dl *sd; | |
c17608ea AK |
2980 | int len; |
2981 | struct imsm_update_general_migration_checkpoint *u; | |
3136abe5 | 2982 | struct imsm_dev *dev; |
594dc1b8 | 2983 | struct imsm_map *map; |
3136abe5 AK |
2984 | |
2985 | /* find map under migration */ | |
2986 | dev = imsm_get_device_during_migration(super); | |
2987 | /* if no migration, write buffer anyway to clear migr_record | |
2988 | * on disk based on first available device | |
2989 | */ | |
2990 | if (dev == NULL) | |
2991 | dev = get_imsm_dev(super, super->current_vol < 0 ? 0 : | |
2992 | super->current_vol); | |
2993 | ||
44bfe6df | 2994 | map = get_imsm_map(dev, MAP_0); |
687629c2 | 2995 | |
de44e46f PB |
2996 | if (sector_size == 4096) |
2997 | convert_to_4k_imsm_migr_rec(super); | |
687629c2 | 2998 | for (sd = super->disks ; sd ; sd = sd->next) { |
b4ab44d8 | 2999 | int slot = -1; |
3136abe5 AK |
3000 | |
3001 | /* skip failed and spare devices */ | |
3002 | if (sd->index < 0) | |
3003 | continue; | |
687629c2 | 3004 | /* write to 2 first slots only */ |
3136abe5 AK |
3005 | if (map) |
3006 | slot = get_imsm_disk_slot(map, sd->index); | |
089f9d79 | 3007 | if (map == NULL || slot > 1 || slot < 0) |
687629c2 | 3008 | continue; |
3136abe5 | 3009 | |
687629c2 AK |
3010 | sprintf(nm, "%d:%d", sd->major, sd->minor); |
3011 | fd = dev_open(nm, O_RDWR); | |
3012 | if (fd < 0) | |
3013 | continue; | |
3014 | get_dev_size(fd, NULL, &dsize); | |
de44e46f PB |
3015 | if (lseek64(fd, dsize - (MIGR_REC_SECTOR_POSITION*sector_size), |
3016 | SEEK_SET) < 0) { | |
e7b84f9d N |
3017 | pr_err("Cannot seek to anchor block: %s\n", |
3018 | strerror(errno)); | |
687629c2 AK |
3019 | goto out; |
3020 | } | |
de44e46f PB |
3021 | if (write(fd, super->migr_rec_buf, |
3022 | MIGR_REC_BUF_SECTORS*sector_size) != | |
3023 | MIGR_REC_BUF_SECTORS*sector_size) { | |
e7b84f9d N |
3024 | pr_err("Cannot write migr record block: %s\n", |
3025 | strerror(errno)); | |
687629c2 AK |
3026 | goto out; |
3027 | } | |
3028 | close(fd); | |
3029 | fd = -1; | |
3030 | } | |
de44e46f PB |
3031 | if (sector_size == 4096) |
3032 | convert_from_4k_imsm_migr_rec(super); | |
c17608ea AK |
3033 | /* update checkpoint information in metadata */ |
3034 | len = imsm_create_metadata_checkpoint_update(super, &u); | |
c17608ea AK |
3035 | if (len <= 0) { |
3036 | dprintf("imsm: Cannot prepare update\n"); | |
3037 | goto out; | |
3038 | } | |
3039 | /* update metadata locally */ | |
3040 | imsm_update_metadata_locally(st, u, len); | |
3041 | /* and possibly remotely */ | |
3042 | if (st->update_tail) { | |
3043 | append_metadata_update(st, u, len); | |
3044 | /* during reshape we do all work inside metadata handler | |
3045 | * manage_reshape(), so metadata update has to be triggered | |
3046 | * insida it | |
3047 | */ | |
3048 | flush_metadata_updates(st); | |
3049 | st->update_tail = &st->updates; | |
3050 | } else | |
3051 | free(u); | |
687629c2 AK |
3052 | |
3053 | retval = 0; | |
3054 | out: | |
3055 | if (fd >= 0) | |
3056 | close(fd); | |
3057 | return retval; | |
3058 | } | |
9e2d750d | 3059 | #endif /* MDASSEMBLE */ |
687629c2 | 3060 | |
e2962bfc AK |
3061 | /* spare/missing disks activations are not allowe when |
3062 | * array/container performs reshape operation, because | |
3063 | * all arrays in container works on the same disks set | |
3064 | */ | |
3065 | int imsm_reshape_blocks_arrays_changes(struct intel_super *super) | |
3066 | { | |
3067 | int rv = 0; | |
3068 | struct intel_dev *i_dev; | |
3069 | struct imsm_dev *dev; | |
3070 | ||
3071 | /* check whole container | |
3072 | */ | |
3073 | for (i_dev = super->devlist; i_dev; i_dev = i_dev->next) { | |
3074 | dev = i_dev->dev; | |
3ad25638 | 3075 | if (is_gen_migration(dev)) { |
e2962bfc AK |
3076 | /* No repair during any migration in container |
3077 | */ | |
3078 | rv = 1; | |
3079 | break; | |
3080 | } | |
3081 | } | |
3082 | return rv; | |
3083 | } | |
c41e00b2 AK |
3084 | static unsigned long long imsm_component_size_aligment_check(int level, |
3085 | int chunk_size, | |
f36a9ecd | 3086 | unsigned int sector_size, |
c41e00b2 AK |
3087 | unsigned long long component_size) |
3088 | { | |
3089 | unsigned int component_size_alligment; | |
3090 | ||
3091 | /* check component size aligment | |
3092 | */ | |
f36a9ecd | 3093 | component_size_alligment = component_size % (chunk_size/sector_size); |
c41e00b2 | 3094 | |
1ade5cc1 | 3095 | dprintf("(Level: %i, chunk_size = %i, component_size = %llu), component_size_alligment = %u\n", |
c41e00b2 AK |
3096 | level, chunk_size, component_size, |
3097 | component_size_alligment); | |
3098 | ||
3099 | if (component_size_alligment && (level != 1) && (level != UnSet)) { | |
3100 | dprintf("imsm: reported component size alligned from %llu ", | |
3101 | component_size); | |
3102 | component_size -= component_size_alligment; | |
1ade5cc1 | 3103 | dprintf_cont("to %llu (%i).\n", |
c41e00b2 AK |
3104 | component_size, component_size_alligment); |
3105 | } | |
3106 | ||
3107 | return component_size; | |
3108 | } | |
e2962bfc | 3109 | |
a5d85af7 | 3110 | static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info, char *dmap) |
bf5a934a DW |
3111 | { |
3112 | struct intel_super *super = st->sb; | |
c47b0ff6 | 3113 | struct migr_record *migr_rec = super->migr_rec; |
949c47a0 | 3114 | struct imsm_dev *dev = get_imsm_dev(super, super->current_vol); |
238c0a71 AK |
3115 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
3116 | struct imsm_map *prev_map = get_imsm_map(dev, MAP_1); | |
b335e593 | 3117 | struct imsm_map *map_to_analyse = map; |
efb30e7f | 3118 | struct dl *dl; |
a5d85af7 | 3119 | int map_disks = info->array.raid_disks; |
bf5a934a | 3120 | |
95eeceeb | 3121 | memset(info, 0, sizeof(*info)); |
b335e593 AK |
3122 | if (prev_map) |
3123 | map_to_analyse = prev_map; | |
3124 | ||
ca0748fa | 3125 | dl = super->current_disk; |
9894ec0d | 3126 | |
bf5a934a | 3127 | info->container_member = super->current_vol; |
cd0430a1 | 3128 | info->array.raid_disks = map->num_members; |
b335e593 | 3129 | info->array.level = get_imsm_raid_level(map_to_analyse); |
bf5a934a DW |
3130 | info->array.layout = imsm_level_to_layout(info->array.level); |
3131 | info->array.md_minor = -1; | |
3132 | info->array.ctime = 0; | |
3133 | info->array.utime = 0; | |
b335e593 AK |
3134 | info->array.chunk_size = |
3135 | __le16_to_cpu(map_to_analyse->blocks_per_strip) << 9; | |
301406c9 | 3136 | info->array.state = !dev->vol.dirty; |
da9b4a62 DW |
3137 | info->custom_array_size = __le32_to_cpu(dev->size_high); |
3138 | info->custom_array_size <<= 32; | |
3139 | info->custom_array_size |= __le32_to_cpu(dev->size_low); | |
3ad25638 AK |
3140 | info->recovery_blocked = imsm_reshape_blocks_arrays_changes(st->sb); |
3141 | ||
3f510843 | 3142 | if (is_gen_migration(dev)) { |
3f83228a | 3143 | info->reshape_active = 1; |
b335e593 AK |
3144 | info->new_level = get_imsm_raid_level(map); |
3145 | info->new_layout = imsm_level_to_layout(info->new_level); | |
3146 | info->new_chunk = __le16_to_cpu(map->blocks_per_strip) << 9; | |
3f83228a | 3147 | info->delta_disks = map->num_members - prev_map->num_members; |
493f5dd6 N |
3148 | if (info->delta_disks) { |
3149 | /* this needs to be applied to every array | |
3150 | * in the container. | |
3151 | */ | |
81219e70 | 3152 | info->reshape_active = CONTAINER_RESHAPE; |
493f5dd6 | 3153 | } |
3f83228a N |
3154 | /* We shape information that we give to md might have to be |
3155 | * modify to cope with md's requirement for reshaping arrays. | |
3156 | * For example, when reshaping a RAID0, md requires it to be | |
3157 | * presented as a degraded RAID4. | |
3158 | * Also if a RAID0 is migrating to a RAID5 we need to specify | |
3159 | * the array as already being RAID5, but the 'before' layout | |
3160 | * is a RAID4-like layout. | |
3161 | */ | |
3162 | switch (info->array.level) { | |
3163 | case 0: | |
3164 | switch(info->new_level) { | |
3165 | case 0: | |
3166 | /* conversion is happening as RAID4 */ | |
3167 | info->array.level = 4; | |
3168 | info->array.raid_disks += 1; | |
3169 | break; | |
3170 | case 5: | |
3171 | /* conversion is happening as RAID5 */ | |
3172 | info->array.level = 5; | |
3173 | info->array.layout = ALGORITHM_PARITY_N; | |
3f83228a N |
3174 | info->delta_disks -= 1; |
3175 | break; | |
3176 | default: | |
3177 | /* FIXME error message */ | |
3178 | info->array.level = UnSet; | |
3179 | break; | |
3180 | } | |
3181 | break; | |
3182 | } | |
b335e593 AK |
3183 | } else { |
3184 | info->new_level = UnSet; | |
3185 | info->new_layout = UnSet; | |
3186 | info->new_chunk = info->array.chunk_size; | |
3f83228a | 3187 | info->delta_disks = 0; |
b335e593 | 3188 | } |
ca0748fa | 3189 | |
efb30e7f DW |
3190 | if (dl) { |
3191 | info->disk.major = dl->major; | |
3192 | info->disk.minor = dl->minor; | |
ca0748fa | 3193 | info->disk.number = dl->index; |
656b6b5a N |
3194 | info->disk.raid_disk = get_imsm_disk_slot(map_to_analyse, |
3195 | dl->index); | |
efb30e7f | 3196 | } |
bf5a934a | 3197 | |
5551b113 | 3198 | info->data_offset = pba_of_lba0(map_to_analyse); |
06fb291a PB |
3199 | |
3200 | if (info->array.level == 5) { | |
3201 | info->component_size = num_data_stripes(map_to_analyse) * | |
3202 | map_to_analyse->blocks_per_strip; | |
3203 | } else { | |
3204 | info->component_size = blocks_per_member(map_to_analyse); | |
3205 | } | |
139dae11 | 3206 | |
c41e00b2 AK |
3207 | info->component_size = imsm_component_size_aligment_check( |
3208 | info->array.level, | |
3209 | info->array.chunk_size, | |
f36a9ecd | 3210 | super->sector_size, |
c41e00b2 | 3211 | info->component_size); |
b12796be | 3212 | info->bb.supported = 0; |
139dae11 | 3213 | |
301406c9 | 3214 | memset(info->uuid, 0, sizeof(info->uuid)); |
921d9e16 | 3215 | info->recovery_start = MaxSector; |
bf5a934a | 3216 | |
d2e6d5d6 | 3217 | info->reshape_progress = 0; |
b6796ce1 | 3218 | info->resync_start = MaxSector; |
b9172665 AK |
3219 | if ((map_to_analyse->map_state == IMSM_T_STATE_UNINITIALIZED || |
3220 | dev->vol.dirty) && | |
3221 | imsm_reshape_blocks_arrays_changes(super) == 0) { | |
301406c9 | 3222 | info->resync_start = 0; |
b6796ce1 AK |
3223 | } |
3224 | if (dev->vol.migr_state) { | |
1e5c6983 DW |
3225 | switch (migr_type(dev)) { |
3226 | case MIGR_REPAIR: | |
3227 | case MIGR_INIT: { | |
c47b0ff6 AK |
3228 | __u64 blocks_per_unit = blocks_per_migr_unit(super, |
3229 | dev); | |
1e5c6983 DW |
3230 | __u64 units = __le32_to_cpu(dev->vol.curr_migr_unit); |
3231 | ||
3232 | info->resync_start = blocks_per_unit * units; | |
3233 | break; | |
3234 | } | |
d2e6d5d6 | 3235 | case MIGR_GEN_MIGR: { |
c47b0ff6 AK |
3236 | __u64 blocks_per_unit = blocks_per_migr_unit(super, |
3237 | dev); | |
3238 | __u64 units = __le32_to_cpu(migr_rec->curr_migr_unit); | |
04fa9523 AK |
3239 | unsigned long long array_blocks; |
3240 | int used_disks; | |
d2e6d5d6 | 3241 | |
befb629b AK |
3242 | if (__le32_to_cpu(migr_rec->ascending_migr) && |
3243 | (units < | |
3244 | (__le32_to_cpu(migr_rec->num_migr_units)-1)) && | |
3245 | (super->migr_rec->rec_status == | |
3246 | __cpu_to_le32(UNIT_SRC_IN_CP_AREA))) | |
3247 | units++; | |
3248 | ||
d2e6d5d6 | 3249 | info->reshape_progress = blocks_per_unit * units; |
6289d1e0 | 3250 | |
7a862a02 | 3251 | dprintf("IMSM: General Migration checkpoint : %llu (%llu) -> read reshape progress : %llu\n", |
19986c72 MB |
3252 | (unsigned long long)units, |
3253 | (unsigned long long)blocks_per_unit, | |
3254 | info->reshape_progress); | |
75156c46 | 3255 | |
238c0a71 | 3256 | used_disks = imsm_num_data_members(dev, MAP_1); |
75156c46 | 3257 | if (used_disks > 0) { |
5551b113 | 3258 | array_blocks = blocks_per_member(map) * |
75156c46 AK |
3259 | used_disks; |
3260 | /* round array size down to closest MB | |
3261 | */ | |
3262 | info->custom_array_size = (array_blocks | |
3263 | >> SECT_PER_MB_SHIFT) | |
3264 | << SECT_PER_MB_SHIFT; | |
3265 | } | |
d2e6d5d6 | 3266 | } |
1e5c6983 DW |
3267 | case MIGR_VERIFY: |
3268 | /* we could emulate the checkpointing of | |
3269 | * 'sync_action=check' migrations, but for now | |
3270 | * we just immediately complete them | |
3271 | */ | |
3272 | case MIGR_REBUILD: | |
3273 | /* this is handled by container_content_imsm() */ | |
1e5c6983 DW |
3274 | case MIGR_STATE_CHANGE: |
3275 | /* FIXME handle other migrations */ | |
3276 | default: | |
3277 | /* we are not dirty, so... */ | |
3278 | info->resync_start = MaxSector; | |
3279 | } | |
b6796ce1 | 3280 | } |
301406c9 DW |
3281 | |
3282 | strncpy(info->name, (char *) dev->volume, MAX_RAID_SERIAL_LEN); | |
3283 | info->name[MAX_RAID_SERIAL_LEN] = 0; | |
bf5a934a | 3284 | |
f35f2525 N |
3285 | info->array.major_version = -1; |
3286 | info->array.minor_version = -2; | |
4dd2df09 | 3287 | sprintf(info->text_version, "/%s/%d", st->container_devnm, info->container_member); |
a67dd8cc | 3288 | info->safe_mode_delay = 4000; /* 4 secs like the Matrix driver */ |
51006d85 | 3289 | uuid_from_super_imsm(st, info->uuid); |
a5d85af7 N |
3290 | |
3291 | if (dmap) { | |
3292 | int i, j; | |
3293 | for (i=0; i<map_disks; i++) { | |
3294 | dmap[i] = 0; | |
3295 | if (i < info->array.raid_disks) { | |
3296 | struct imsm_disk *dsk; | |
238c0a71 | 3297 | j = get_imsm_disk_idx(dev, i, MAP_X); |
a5d85af7 N |
3298 | dsk = get_imsm_disk(super, j); |
3299 | if (dsk && (dsk->status & CONFIGURED_DISK)) | |
3300 | dmap[i] = 1; | |
3301 | } | |
3302 | } | |
3303 | } | |
81ac8b4d | 3304 | } |
bf5a934a | 3305 | |
3b451610 AK |
3306 | static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev, |
3307 | int failed, int look_in_map); | |
3308 | ||
3309 | static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev, | |
3310 | int look_in_map); | |
3311 | ||
b4ab44d8 | 3312 | #ifndef MDASSEMBLE |
3b451610 AK |
3313 | static void manage_second_map(struct intel_super *super, struct imsm_dev *dev) |
3314 | { | |
3315 | if (is_gen_migration(dev)) { | |
3316 | int failed; | |
3317 | __u8 map_state; | |
3318 | struct imsm_map *map2 = get_imsm_map(dev, MAP_1); | |
3319 | ||
3320 | failed = imsm_count_failed(super, dev, MAP_1); | |
238c0a71 | 3321 | map_state = imsm_check_degraded(super, dev, failed, MAP_1); |
3b451610 AK |
3322 | if (map2->map_state != map_state) { |
3323 | map2->map_state = map_state; | |
3324 | super->updates_pending++; | |
3325 | } | |
3326 | } | |
3327 | } | |
b4ab44d8 | 3328 | #endif |
97b4d0e9 DW |
3329 | |
3330 | static struct imsm_disk *get_imsm_missing(struct intel_super *super, __u8 index) | |
3331 | { | |
3332 | struct dl *d; | |
3333 | ||
3334 | for (d = super->missing; d; d = d->next) | |
3335 | if (d->index == index) | |
3336 | return &d->disk; | |
3337 | return NULL; | |
3338 | } | |
3339 | ||
a5d85af7 | 3340 | static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map) |
4f5bc454 DW |
3341 | { |
3342 | struct intel_super *super = st->sb; | |
4f5bc454 | 3343 | struct imsm_disk *disk; |
a5d85af7 | 3344 | int map_disks = info->array.raid_disks; |
ab3cb6b3 N |
3345 | int max_enough = -1; |
3346 | int i; | |
3347 | struct imsm_super *mpb; | |
4f5bc454 | 3348 | |
bf5a934a | 3349 | if (super->current_vol >= 0) { |
a5d85af7 | 3350 | getinfo_super_imsm_volume(st, info, map); |
bf5a934a DW |
3351 | return; |
3352 | } | |
95eeceeb | 3353 | memset(info, 0, sizeof(*info)); |
d23fe947 DW |
3354 | |
3355 | /* Set raid_disks to zero so that Assemble will always pull in valid | |
3356 | * spares | |
3357 | */ | |
3358 | info->array.raid_disks = 0; | |
cdddbdbc DW |
3359 | info->array.level = LEVEL_CONTAINER; |
3360 | info->array.layout = 0; | |
3361 | info->array.md_minor = -1; | |
1011e834 | 3362 | info->array.ctime = 0; /* N/A for imsm */ |
cdddbdbc DW |
3363 | info->array.utime = 0; |
3364 | info->array.chunk_size = 0; | |
3365 | ||
3366 | info->disk.major = 0; | |
3367 | info->disk.minor = 0; | |
cdddbdbc | 3368 | info->disk.raid_disk = -1; |
c2c087e6 | 3369 | info->reshape_active = 0; |
f35f2525 N |
3370 | info->array.major_version = -1; |
3371 | info->array.minor_version = -2; | |
c2c087e6 | 3372 | strcpy(info->text_version, "imsm"); |
a67dd8cc | 3373 | info->safe_mode_delay = 0; |
c2c087e6 DW |
3374 | info->disk.number = -1; |
3375 | info->disk.state = 0; | |
c5afc314 | 3376 | info->name[0] = 0; |
921d9e16 | 3377 | info->recovery_start = MaxSector; |
3ad25638 | 3378 | info->recovery_blocked = imsm_reshape_blocks_arrays_changes(st->sb); |
b12796be | 3379 | info->bb.supported = 0; |
c2c087e6 | 3380 | |
97b4d0e9 | 3381 | /* do we have the all the insync disks that we expect? */ |
ab3cb6b3 | 3382 | mpb = super->anchor; |
b7d81a38 | 3383 | info->events = __le32_to_cpu(mpb->generation_num); |
97b4d0e9 | 3384 | |
ab3cb6b3 N |
3385 | for (i = 0; i < mpb->num_raid_devs; i++) { |
3386 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
3387 | int failed, enough, j, missing = 0; | |
3388 | struct imsm_map *map; | |
3389 | __u8 state; | |
97b4d0e9 | 3390 | |
3b451610 AK |
3391 | failed = imsm_count_failed(super, dev, MAP_0); |
3392 | state = imsm_check_degraded(super, dev, failed, MAP_0); | |
238c0a71 | 3393 | map = get_imsm_map(dev, MAP_0); |
ab3cb6b3 N |
3394 | |
3395 | /* any newly missing disks? | |
3396 | * (catches single-degraded vs double-degraded) | |
3397 | */ | |
3398 | for (j = 0; j < map->num_members; j++) { | |
238c0a71 | 3399 | __u32 ord = get_imsm_ord_tbl_ent(dev, j, MAP_0); |
ab3cb6b3 N |
3400 | __u32 idx = ord_to_idx(ord); |
3401 | ||
3402 | if (!(ord & IMSM_ORD_REBUILD) && | |
3403 | get_imsm_missing(super, idx)) { | |
3404 | missing = 1; | |
3405 | break; | |
3406 | } | |
97b4d0e9 | 3407 | } |
ab3cb6b3 N |
3408 | |
3409 | if (state == IMSM_T_STATE_FAILED) | |
3410 | enough = -1; | |
3411 | else if (state == IMSM_T_STATE_DEGRADED && | |
3412 | (state != map->map_state || missing)) | |
3413 | enough = 0; | |
3414 | else /* we're normal, or already degraded */ | |
3415 | enough = 1; | |
d2bde6d3 AK |
3416 | if (is_gen_migration(dev) && missing) { |
3417 | /* during general migration we need all disks | |
3418 | * that process is running on. | |
3419 | * No new missing disk is allowed. | |
3420 | */ | |
3421 | max_enough = -1; | |
3422 | enough = -1; | |
3423 | /* no more checks necessary | |
3424 | */ | |
3425 | break; | |
3426 | } | |
ab3cb6b3 N |
3427 | /* in the missing/failed disk case check to see |
3428 | * if at least one array is runnable | |
3429 | */ | |
3430 | max_enough = max(max_enough, enough); | |
3431 | } | |
1ade5cc1 | 3432 | dprintf("enough: %d\n", max_enough); |
ab3cb6b3 | 3433 | info->container_enough = max_enough; |
97b4d0e9 | 3434 | |
4a04ec6c | 3435 | if (super->disks) { |
14e8215b DW |
3436 | __u32 reserved = imsm_reserved_sectors(super, super->disks); |
3437 | ||
b9f594fe | 3438 | disk = &super->disks->disk; |
5551b113 | 3439 | info->data_offset = total_blocks(&super->disks->disk) - reserved; |
14e8215b | 3440 | info->component_size = reserved; |
25ed7e59 | 3441 | info->disk.state = is_configured(disk) ? (1 << MD_DISK_ACTIVE) : 0; |
df474657 DW |
3442 | /* we don't change info->disk.raid_disk here because |
3443 | * this state will be finalized in mdmon after we have | |
3444 | * found the 'most fresh' version of the metadata | |
3445 | */ | |
25ed7e59 DW |
3446 | info->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0; |
3447 | info->disk.state |= is_spare(disk) ? 0 : (1 << MD_DISK_SYNC); | |
cdddbdbc | 3448 | } |
a575e2a7 DW |
3449 | |
3450 | /* only call uuid_from_super_imsm when this disk is part of a populated container, | |
3451 | * ->compare_super may have updated the 'num_raid_devs' field for spares | |
3452 | */ | |
3453 | if (info->disk.state & (1 << MD_DISK_SYNC) || super->anchor->num_raid_devs) | |
36ba7d48 | 3454 | uuid_from_super_imsm(st, info->uuid); |
22e263f6 AC |
3455 | else |
3456 | memcpy(info->uuid, uuid_zero, sizeof(uuid_zero)); | |
a5d85af7 N |
3457 | |
3458 | /* I don't know how to compute 'map' on imsm, so use safe default */ | |
3459 | if (map) { | |
3460 | int i; | |
3461 | for (i = 0; i < map_disks; i++) | |
3462 | map[i] = 1; | |
3463 | } | |
3464 | ||
cdddbdbc DW |
3465 | } |
3466 | ||
5c4cd5da AC |
3467 | /* allocates memory and fills disk in mdinfo structure |
3468 | * for each disk in array */ | |
3469 | struct mdinfo *getinfo_super_disks_imsm(struct supertype *st) | |
3470 | { | |
594dc1b8 | 3471 | struct mdinfo *mddev; |
5c4cd5da AC |
3472 | struct intel_super *super = st->sb; |
3473 | struct imsm_disk *disk; | |
3474 | int count = 0; | |
3475 | struct dl *dl; | |
3476 | if (!super || !super->disks) | |
3477 | return NULL; | |
3478 | dl = super->disks; | |
503975b9 | 3479 | mddev = xcalloc(1, sizeof(*mddev)); |
5c4cd5da AC |
3480 | while (dl) { |
3481 | struct mdinfo *tmp; | |
3482 | disk = &dl->disk; | |
503975b9 | 3483 | tmp = xcalloc(1, sizeof(*tmp)); |
5c4cd5da AC |
3484 | if (mddev->devs) |
3485 | tmp->next = mddev->devs; | |
3486 | mddev->devs = tmp; | |
3487 | tmp->disk.number = count++; | |
3488 | tmp->disk.major = dl->major; | |
3489 | tmp->disk.minor = dl->minor; | |
3490 | tmp->disk.state = is_configured(disk) ? | |
3491 | (1 << MD_DISK_ACTIVE) : 0; | |
3492 | tmp->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0; | |
3493 | tmp->disk.state |= is_spare(disk) ? 0 : (1 << MD_DISK_SYNC); | |
3494 | tmp->disk.raid_disk = -1; | |
3495 | dl = dl->next; | |
3496 | } | |
3497 | return mddev; | |
3498 | } | |
3499 | ||
cdddbdbc DW |
3500 | static int update_super_imsm(struct supertype *st, struct mdinfo *info, |
3501 | char *update, char *devname, int verbose, | |
3502 | int uuid_set, char *homehost) | |
3503 | { | |
f352c545 DW |
3504 | /* For 'assemble' and 'force' we need to return non-zero if any |
3505 | * change was made. For others, the return value is ignored. | |
3506 | * Update options are: | |
3507 | * force-one : This device looks a bit old but needs to be included, | |
3508 | * update age info appropriately. | |
3509 | * assemble: clear any 'faulty' flag to allow this device to | |
3510 | * be assembled. | |
3511 | * force-array: Array is degraded but being forced, mark it clean | |
3512 | * if that will be needed to assemble it. | |
3513 | * | |
3514 | * newdev: not used ???? | |
3515 | * grow: Array has gained a new device - this is currently for | |
3516 | * linear only | |
3517 | * resync: mark as dirty so a resync will happen. | |
3518 | * name: update the name - preserving the homehost | |
6e46bf34 | 3519 | * uuid: Change the uuid of the array to match watch is given |
f352c545 DW |
3520 | * |
3521 | * Following are not relevant for this imsm: | |
3522 | * sparc2.2 : update from old dodgey metadata | |
3523 | * super-minor: change the preferred_minor number | |
3524 | * summaries: update redundant counters. | |
f352c545 DW |
3525 | * homehost: update the recorded homehost |
3526 | * _reshape_progress: record new reshape_progress position. | |
3527 | */ | |
6e46bf34 DW |
3528 | int rv = 1; |
3529 | struct intel_super *super = st->sb; | |
3530 | struct imsm_super *mpb; | |
f352c545 | 3531 | |
6e46bf34 DW |
3532 | /* we can only update container info */ |
3533 | if (!super || super->current_vol >= 0 || !super->anchor) | |
3534 | return 1; | |
3535 | ||
3536 | mpb = super->anchor; | |
3537 | ||
81a5b4f5 N |
3538 | if (strcmp(update, "uuid") == 0) { |
3539 | /* We take this to mean that the family_num should be updated. | |
3540 | * However that is much smaller than the uuid so we cannot really | |
3541 | * allow an explicit uuid to be given. And it is hard to reliably | |
3542 | * know if one was. | |
3543 | * So if !uuid_set we know the current uuid is random and just used | |
3544 | * the first 'int' and copy it to the other 3 positions. | |
3545 | * Otherwise we require the 4 'int's to be the same as would be the | |
3546 | * case if we are using a random uuid. So an explicit uuid will be | |
3547 | * accepted as long as all for ints are the same... which shouldn't hurt | |
6e46bf34 | 3548 | */ |
81a5b4f5 N |
3549 | if (!uuid_set) { |
3550 | info->uuid[1] = info->uuid[2] = info->uuid[3] = info->uuid[0]; | |
6e46bf34 | 3551 | rv = 0; |
81a5b4f5 N |
3552 | } else { |
3553 | if (info->uuid[0] != info->uuid[1] || | |
3554 | info->uuid[1] != info->uuid[2] || | |
3555 | info->uuid[2] != info->uuid[3]) | |
3556 | rv = -1; | |
3557 | else | |
3558 | rv = 0; | |
6e46bf34 | 3559 | } |
81a5b4f5 N |
3560 | if (rv == 0) |
3561 | mpb->orig_family_num = info->uuid[0]; | |
6e46bf34 DW |
3562 | } else if (strcmp(update, "assemble") == 0) |
3563 | rv = 0; | |
3564 | else | |
1e2b2765 | 3565 | rv = -1; |
f352c545 | 3566 | |
6e46bf34 DW |
3567 | /* successful update? recompute checksum */ |
3568 | if (rv == 0) | |
3569 | mpb->check_sum = __le32_to_cpu(__gen_imsm_checksum(mpb)); | |
f352c545 DW |
3570 | |
3571 | return rv; | |
cdddbdbc DW |
3572 | } |
3573 | ||
c2c087e6 | 3574 | static size_t disks_to_mpb_size(int disks) |
cdddbdbc | 3575 | { |
c2c087e6 | 3576 | size_t size; |
cdddbdbc | 3577 | |
c2c087e6 DW |
3578 | size = sizeof(struct imsm_super); |
3579 | size += (disks - 1) * sizeof(struct imsm_disk); | |
3580 | size += 2 * sizeof(struct imsm_dev); | |
3581 | /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */ | |
3582 | size += (4 - 2) * sizeof(struct imsm_map); | |
3583 | /* 4 possible disk_ord_tbl's */ | |
3584 | size += 4 * (disks - 1) * sizeof(__u32); | |
bbab0940 TM |
3585 | /* maximum bbm log */ |
3586 | size += sizeof(struct bbm_log); | |
c2c087e6 DW |
3587 | |
3588 | return size; | |
3589 | } | |
3590 | ||
387fcd59 N |
3591 | static __u64 avail_size_imsm(struct supertype *st, __u64 devsize, |
3592 | unsigned long long data_offset) | |
c2c087e6 DW |
3593 | { |
3594 | if (devsize < (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS)) | |
3595 | return 0; | |
3596 | ||
3597 | return devsize - (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS); | |
cdddbdbc DW |
3598 | } |
3599 | ||
ba2de7ba DW |
3600 | static void free_devlist(struct intel_super *super) |
3601 | { | |
3602 | struct intel_dev *dv; | |
3603 | ||
3604 | while (super->devlist) { | |
3605 | dv = super->devlist->next; | |
3606 | free(super->devlist->dev); | |
3607 | free(super->devlist); | |
3608 | super->devlist = dv; | |
3609 | } | |
3610 | } | |
3611 | ||
3612 | static void imsm_copy_dev(struct imsm_dev *dest, struct imsm_dev *src) | |
3613 | { | |
3614 | memcpy(dest, src, sizeof_imsm_dev(src, 0)); | |
3615 | } | |
3616 | ||
cdddbdbc DW |
3617 | static int compare_super_imsm(struct supertype *st, struct supertype *tst) |
3618 | { | |
3619 | /* | |
3620 | * return: | |
3621 | * 0 same, or first was empty, and second was copied | |
3622 | * 1 second had wrong number | |
3623 | * 2 wrong uuid | |
3624 | * 3 wrong other info | |
3625 | */ | |
3626 | struct intel_super *first = st->sb; | |
3627 | struct intel_super *sec = tst->sb; | |
3628 | ||
5d500228 N |
3629 | if (!first) { |
3630 | st->sb = tst->sb; | |
3631 | tst->sb = NULL; | |
3632 | return 0; | |
3633 | } | |
8603ea6f LM |
3634 | /* in platform dependent environment test if the disks |
3635 | * use the same Intel hba | |
cb8f6859 | 3636 | * If not on Intel hba at all, allow anything. |
8603ea6f | 3637 | */ |
6b781d33 AP |
3638 | if (!check_env("IMSM_NO_PLATFORM") && first->hba && sec->hba) { |
3639 | if (first->hba->type != sec->hba->type) { | |
8603ea6f | 3640 | fprintf(stderr, |
6b781d33 AP |
3641 | "HBAs of devices do not match %s != %s\n", |
3642 | get_sys_dev_type(first->hba->type), | |
3643 | get_sys_dev_type(sec->hba->type)); | |
3644 | return 3; | |
3645 | } | |
3646 | if (first->orom != sec->orom) { | |
3647 | fprintf(stderr, | |
3648 | "HBAs of devices do not match %s != %s\n", | |
3649 | first->hba->pci_id, sec->hba->pci_id); | |
8603ea6f LM |
3650 | return 3; |
3651 | } | |
3652 | } | |
cdddbdbc | 3653 | |
d23fe947 DW |
3654 | /* if an anchor does not have num_raid_devs set then it is a free |
3655 | * floating spare | |
3656 | */ | |
3657 | if (first->anchor->num_raid_devs > 0 && | |
3658 | sec->anchor->num_raid_devs > 0) { | |
a2b97981 DW |
3659 | /* Determine if these disks might ever have been |
3660 | * related. Further disambiguation can only take place | |
3661 | * in load_super_imsm_all | |
3662 | */ | |
3663 | __u32 first_family = first->anchor->orig_family_num; | |
3664 | __u32 sec_family = sec->anchor->orig_family_num; | |
3665 | ||
f796af5d DW |
3666 | if (memcmp(first->anchor->sig, sec->anchor->sig, |
3667 | MAX_SIGNATURE_LENGTH) != 0) | |
3668 | return 3; | |
3669 | ||
a2b97981 DW |
3670 | if (first_family == 0) |
3671 | first_family = first->anchor->family_num; | |
3672 | if (sec_family == 0) | |
3673 | sec_family = sec->anchor->family_num; | |
3674 | ||
3675 | if (first_family != sec_family) | |
d23fe947 | 3676 | return 3; |
f796af5d | 3677 | |
d23fe947 | 3678 | } |
cdddbdbc | 3679 | |
3e372e5a DW |
3680 | /* if 'first' is a spare promote it to a populated mpb with sec's |
3681 | * family number | |
3682 | */ | |
3683 | if (first->anchor->num_raid_devs == 0 && | |
3684 | sec->anchor->num_raid_devs > 0) { | |
78d30f94 | 3685 | int i; |
ba2de7ba DW |
3686 | struct intel_dev *dv; |
3687 | struct imsm_dev *dev; | |
78d30f94 DW |
3688 | |
3689 | /* we need to copy raid device info from sec if an allocation | |
3690 | * fails here we don't associate the spare | |
3691 | */ | |
3692 | for (i = 0; i < sec->anchor->num_raid_devs; i++) { | |
503975b9 N |
3693 | dv = xmalloc(sizeof(*dv)); |
3694 | dev = xmalloc(sizeof_imsm_dev(get_imsm_dev(sec, i), 1)); | |
ba2de7ba DW |
3695 | dv->dev = dev; |
3696 | dv->index = i; | |
3697 | dv->next = first->devlist; | |
3698 | first->devlist = dv; | |
78d30f94 | 3699 | } |
709743c5 | 3700 | if (i < sec->anchor->num_raid_devs) { |
ba2de7ba DW |
3701 | /* allocation failure */ |
3702 | free_devlist(first); | |
e12b3daa | 3703 | pr_err("imsm: failed to associate spare\n"); |
ba2de7ba | 3704 | return 3; |
78d30f94 | 3705 | } |
3e372e5a | 3706 | first->anchor->num_raid_devs = sec->anchor->num_raid_devs; |
148acb7b | 3707 | first->anchor->orig_family_num = sec->anchor->orig_family_num; |
3e372e5a | 3708 | first->anchor->family_num = sec->anchor->family_num; |
ac6449be | 3709 | memcpy(first->anchor->sig, sec->anchor->sig, MAX_SIGNATURE_LENGTH); |
709743c5 DW |
3710 | for (i = 0; i < sec->anchor->num_raid_devs; i++) |
3711 | imsm_copy_dev(get_imsm_dev(first, i), get_imsm_dev(sec, i)); | |
3e372e5a DW |
3712 | } |
3713 | ||
cdddbdbc DW |
3714 | return 0; |
3715 | } | |
3716 | ||
0030e8d6 DW |
3717 | static void fd2devname(int fd, char *name) |
3718 | { | |
3719 | struct stat st; | |
3720 | char path[256]; | |
33a6535d | 3721 | char dname[PATH_MAX]; |
0030e8d6 DW |
3722 | char *nm; |
3723 | int rv; | |
3724 | ||
3725 | name[0] = '\0'; | |
3726 | if (fstat(fd, &st) != 0) | |
3727 | return; | |
3728 | sprintf(path, "/sys/dev/block/%d:%d", | |
3729 | major(st.st_rdev), minor(st.st_rdev)); | |
3730 | ||
9cf014ec | 3731 | rv = readlink(path, dname, sizeof(dname)-1); |
0030e8d6 DW |
3732 | if (rv <= 0) |
3733 | return; | |
9587c373 | 3734 | |
0030e8d6 DW |
3735 | dname[rv] = '\0'; |
3736 | nm = strrchr(dname, '/'); | |
7897de29 JS |
3737 | if (nm) { |
3738 | nm++; | |
3739 | snprintf(name, MAX_RAID_SERIAL_LEN, "/dev/%s", nm); | |
3740 | } | |
0030e8d6 DW |
3741 | } |
3742 | ||
21e9380b AP |
3743 | static int nvme_get_serial(int fd, void *buf, size_t buf_len) |
3744 | { | |
3745 | char path[60]; | |
3746 | char *name = fd2kname(fd); | |
3747 | ||
3748 | if (!name) | |
3749 | return 1; | |
3750 | ||
3751 | if (strncmp(name, "nvme", 4) != 0) | |
3752 | return 1; | |
3753 | ||
3754 | snprintf(path, sizeof(path) - 1, "/sys/block/%s/device/serial", name); | |
3755 | ||
3756 | return load_sys(path, buf, buf_len); | |
3757 | } | |
3758 | ||
cdddbdbc DW |
3759 | extern int scsi_get_serial(int fd, void *buf, size_t buf_len); |
3760 | ||
3761 | static int imsm_read_serial(int fd, char *devname, | |
3762 | __u8 serial[MAX_RAID_SERIAL_LEN]) | |
3763 | { | |
21e9380b | 3764 | char buf[50]; |
cdddbdbc | 3765 | int rv; |
1f24f035 | 3766 | int len; |
316e2bf4 DW |
3767 | char *dest; |
3768 | char *src; | |
21e9380b AP |
3769 | unsigned int i; |
3770 | ||
3771 | memset(buf, 0, sizeof(buf)); | |
cdddbdbc | 3772 | |
21e9380b | 3773 | rv = nvme_get_serial(fd, buf, sizeof(buf)); |
cdddbdbc | 3774 | |
21e9380b AP |
3775 | if (rv) |
3776 | rv = scsi_get_serial(fd, buf, sizeof(buf)); | |
f9ba0ff1 | 3777 | |
40ebbb9c | 3778 | if (rv && check_env("IMSM_DEVNAME_AS_SERIAL")) { |
f9ba0ff1 DW |
3779 | memset(serial, 0, MAX_RAID_SERIAL_LEN); |
3780 | fd2devname(fd, (char *) serial); | |
0030e8d6 DW |
3781 | return 0; |
3782 | } | |
3783 | ||
cdddbdbc DW |
3784 | if (rv != 0) { |
3785 | if (devname) | |
e7b84f9d N |
3786 | pr_err("Failed to retrieve serial for %s\n", |
3787 | devname); | |
cdddbdbc DW |
3788 | return rv; |
3789 | } | |
3790 | ||
316e2bf4 DW |
3791 | /* trim all whitespace and non-printable characters and convert |
3792 | * ':' to ';' | |
3793 | */ | |
21e9380b AP |
3794 | for (i = 0, dest = buf; i < sizeof(buf) && buf[i]; i++) { |
3795 | src = &buf[i]; | |
316e2bf4 DW |
3796 | if (*src > 0x20) { |
3797 | /* ':' is reserved for use in placeholder serial | |
3798 | * numbers for missing disks | |
3799 | */ | |
3800 | if (*src == ':') | |
3801 | *dest++ = ';'; | |
3802 | else | |
3803 | *dest++ = *src; | |
3804 | } | |
3805 | } | |
21e9380b AP |
3806 | len = dest - buf; |
3807 | dest = buf; | |
316e2bf4 DW |
3808 | |
3809 | /* truncate leading characters */ | |
3810 | if (len > MAX_RAID_SERIAL_LEN) { | |
3811 | dest += len - MAX_RAID_SERIAL_LEN; | |
1f24f035 | 3812 | len = MAX_RAID_SERIAL_LEN; |
316e2bf4 | 3813 | } |
5c3db629 | 3814 | |
5c3db629 | 3815 | memset(serial, 0, MAX_RAID_SERIAL_LEN); |
316e2bf4 | 3816 | memcpy(serial, dest, len); |
cdddbdbc DW |
3817 | |
3818 | return 0; | |
3819 | } | |
3820 | ||
1f24f035 DW |
3821 | static int serialcmp(__u8 *s1, __u8 *s2) |
3822 | { | |
3823 | return strncmp((char *) s1, (char *) s2, MAX_RAID_SERIAL_LEN); | |
3824 | } | |
3825 | ||
3826 | static void serialcpy(__u8 *dest, __u8 *src) | |
3827 | { | |
3828 | strncpy((char *) dest, (char *) src, MAX_RAID_SERIAL_LEN); | |
3829 | } | |
3830 | ||
54c2c1ea DW |
3831 | static struct dl *serial_to_dl(__u8 *serial, struct intel_super *super) |
3832 | { | |
3833 | struct dl *dl; | |
3834 | ||
3835 | for (dl = super->disks; dl; dl = dl->next) | |
3836 | if (serialcmp(dl->serial, serial) == 0) | |
3837 | break; | |
3838 | ||
3839 | return dl; | |
3840 | } | |
3841 | ||
a2b97981 DW |
3842 | static struct imsm_disk * |
3843 | __serial_to_disk(__u8 *serial, struct imsm_super *mpb, int *idx) | |
3844 | { | |
3845 | int i; | |
3846 | ||
3847 | for (i = 0; i < mpb->num_disks; i++) { | |
3848 | struct imsm_disk *disk = __get_imsm_disk(mpb, i); | |
3849 | ||
3850 | if (serialcmp(disk->serial, serial) == 0) { | |
3851 | if (idx) | |
3852 | *idx = i; | |
3853 | return disk; | |
3854 | } | |
3855 | } | |
3856 | ||
3857 | return NULL; | |
3858 | } | |
3859 | ||
cdddbdbc DW |
3860 | static int |
3861 | load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd) | |
3862 | { | |
a2b97981 | 3863 | struct imsm_disk *disk; |
cdddbdbc DW |
3864 | struct dl *dl; |
3865 | struct stat stb; | |
cdddbdbc | 3866 | int rv; |
a2b97981 | 3867 | char name[40]; |
d23fe947 DW |
3868 | __u8 serial[MAX_RAID_SERIAL_LEN]; |
3869 | ||
3870 | rv = imsm_read_serial(fd, devname, serial); | |
3871 | ||
3872 | if (rv != 0) | |
3873 | return 2; | |
3874 | ||
503975b9 | 3875 | dl = xcalloc(1, sizeof(*dl)); |
cdddbdbc | 3876 | |
a2b97981 DW |
3877 | fstat(fd, &stb); |
3878 | dl->major = major(stb.st_rdev); | |
3879 | dl->minor = minor(stb.st_rdev); | |
3880 | dl->next = super->disks; | |
3881 | dl->fd = keep_fd ? fd : -1; | |
3882 | assert(super->disks == NULL); | |
3883 | super->disks = dl; | |
3884 | serialcpy(dl->serial, serial); | |
3885 | dl->index = -2; | |
3886 | dl->e = NULL; | |
3887 | fd2devname(fd, name); | |
3888 | if (devname) | |
503975b9 | 3889 | dl->devname = xstrdup(devname); |
a2b97981 | 3890 | else |
503975b9 | 3891 | dl->devname = xstrdup(name); |
cdddbdbc | 3892 | |
d23fe947 | 3893 | /* look up this disk's index in the current anchor */ |
a2b97981 DW |
3894 | disk = __serial_to_disk(dl->serial, super->anchor, &dl->index); |
3895 | if (disk) { | |
3896 | dl->disk = *disk; | |
3897 | /* only set index on disks that are a member of a | |
3898 | * populated contianer, i.e. one with raid_devs | |
3899 | */ | |
3900 | if (is_failed(&dl->disk)) | |
3f6efecc | 3901 | dl->index = -2; |
a2b97981 DW |
3902 | else if (is_spare(&dl->disk)) |
3903 | dl->index = -1; | |
3f6efecc DW |
3904 | } |
3905 | ||
949c47a0 DW |
3906 | return 0; |
3907 | } | |
3908 | ||
0e600426 | 3909 | #ifndef MDASSEMBLE |
0c046afd DW |
3910 | /* When migrating map0 contains the 'destination' state while map1 |
3911 | * contains the current state. When not migrating map0 contains the | |
3912 | * current state. This routine assumes that map[0].map_state is set to | |
3913 | * the current array state before being called. | |
3914 | * | |
3915 | * Migration is indicated by one of the following states | |
3916 | * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed) | |
e3bba0e0 | 3917 | * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal |
0c046afd | 3918 | * map1state=unitialized) |
1484e727 | 3919 | * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal |
0c046afd | 3920 | * map1state=normal) |
e3bba0e0 | 3921 | * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal |
0c046afd | 3922 | * map1state=degraded) |
8e59f3d8 AK |
3923 | * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal |
3924 | * map1state=normal) | |
0c046afd | 3925 | */ |
8e59f3d8 AK |
3926 | static void migrate(struct imsm_dev *dev, struct intel_super *super, |
3927 | __u8 to_state, int migr_type) | |
3393c6af | 3928 | { |
0c046afd | 3929 | struct imsm_map *dest; |
238c0a71 | 3930 | struct imsm_map *src = get_imsm_map(dev, MAP_0); |
3393c6af | 3931 | |
0c046afd | 3932 | dev->vol.migr_state = 1; |
1484e727 | 3933 | set_migr_type(dev, migr_type); |
f8f603f1 | 3934 | dev->vol.curr_migr_unit = 0; |
238c0a71 | 3935 | dest = get_imsm_map(dev, MAP_1); |
0c046afd | 3936 | |
0556e1a2 | 3937 | /* duplicate and then set the target end state in map[0] */ |
3393c6af | 3938 | memcpy(dest, src, sizeof_imsm_map(src)); |
089f9d79 | 3939 | if (migr_type == MIGR_REBUILD || migr_type == MIGR_GEN_MIGR) { |
0556e1a2 DW |
3940 | __u32 ord; |
3941 | int i; | |
3942 | ||
3943 | for (i = 0; i < src->num_members; i++) { | |
3944 | ord = __le32_to_cpu(src->disk_ord_tbl[i]); | |
3945 | set_imsm_ord_tbl_ent(src, i, ord_to_idx(ord)); | |
3946 | } | |
3947 | } | |
3948 | ||
8e59f3d8 AK |
3949 | if (migr_type == MIGR_GEN_MIGR) |
3950 | /* Clear migration record */ | |
3951 | memset(super->migr_rec, 0, sizeof(struct migr_record)); | |
3952 | ||
0c046afd | 3953 | src->map_state = to_state; |
949c47a0 | 3954 | } |
f8f603f1 | 3955 | |
809da78e AK |
3956 | static void end_migration(struct imsm_dev *dev, struct intel_super *super, |
3957 | __u8 map_state) | |
f8f603f1 | 3958 | { |
238c0a71 AK |
3959 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
3960 | struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state == 0 ? | |
3961 | MAP_0 : MAP_1); | |
28bce06f | 3962 | int i, j; |
0556e1a2 DW |
3963 | |
3964 | /* merge any IMSM_ORD_REBUILD bits that were not successfully | |
3965 | * completed in the last migration. | |
3966 | * | |
28bce06f | 3967 | * FIXME add support for raid-level-migration |
0556e1a2 | 3968 | */ |
089f9d79 JS |
3969 | if (map_state != map->map_state && (is_gen_migration(dev) == 0) && |
3970 | prev->map_state != IMSM_T_STATE_UNINITIALIZED) { | |
809da78e AK |
3971 | /* when final map state is other than expected |
3972 | * merge maps (not for migration) | |
3973 | */ | |
3974 | int failed; | |
3975 | ||
3976 | for (i = 0; i < prev->num_members; i++) | |
3977 | for (j = 0; j < map->num_members; j++) | |
3978 | /* during online capacity expansion | |
3979 | * disks position can be changed | |
3980 | * if takeover is used | |
3981 | */ | |
3982 | if (ord_to_idx(map->disk_ord_tbl[j]) == | |
3983 | ord_to_idx(prev->disk_ord_tbl[i])) { | |
3984 | map->disk_ord_tbl[j] |= | |
3985 | prev->disk_ord_tbl[i]; | |
3986 | break; | |
3987 | } | |
3988 | failed = imsm_count_failed(super, dev, MAP_0); | |
3989 | map_state = imsm_check_degraded(super, dev, failed, MAP_0); | |
3990 | } | |
f8f603f1 DW |
3991 | |
3992 | dev->vol.migr_state = 0; | |
ea672ee1 | 3993 | set_migr_type(dev, 0); |
f8f603f1 DW |
3994 | dev->vol.curr_migr_unit = 0; |
3995 | map->map_state = map_state; | |
3996 | } | |
0e600426 | 3997 | #endif |
949c47a0 DW |
3998 | |
3999 | static int parse_raid_devices(struct intel_super *super) | |
4000 | { | |
4001 | int i; | |
4002 | struct imsm_dev *dev_new; | |
4d7b1503 | 4003 | size_t len, len_migr; |
401d313b | 4004 | size_t max_len = 0; |
4d7b1503 DW |
4005 | size_t space_needed = 0; |
4006 | struct imsm_super *mpb = super->anchor; | |
949c47a0 DW |
4007 | |
4008 | for (i = 0; i < super->anchor->num_raid_devs; i++) { | |
4009 | struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i); | |
ba2de7ba | 4010 | struct intel_dev *dv; |
949c47a0 | 4011 | |
4d7b1503 DW |
4012 | len = sizeof_imsm_dev(dev_iter, 0); |
4013 | len_migr = sizeof_imsm_dev(dev_iter, 1); | |
4014 | if (len_migr > len) | |
4015 | space_needed += len_migr - len; | |
ca9de185 | 4016 | |
503975b9 | 4017 | dv = xmalloc(sizeof(*dv)); |
401d313b AK |
4018 | if (max_len < len_migr) |
4019 | max_len = len_migr; | |
4020 | if (max_len > len_migr) | |
4021 | space_needed += max_len - len_migr; | |
503975b9 | 4022 | dev_new = xmalloc(max_len); |
949c47a0 | 4023 | imsm_copy_dev(dev_new, dev_iter); |
ba2de7ba DW |
4024 | dv->dev = dev_new; |
4025 | dv->index = i; | |
4026 | dv->next = super->devlist; | |
4027 | super->devlist = dv; | |
949c47a0 | 4028 | } |
cdddbdbc | 4029 | |
4d7b1503 DW |
4030 | /* ensure that super->buf is large enough when all raid devices |
4031 | * are migrating | |
4032 | */ | |
4033 | if (__le32_to_cpu(mpb->mpb_size) + space_needed > super->len) { | |
4034 | void *buf; | |
4035 | ||
f36a9ecd PB |
4036 | len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + space_needed, |
4037 | super->sector_size); | |
4038 | if (posix_memalign(&buf, MAX_SECTOR_SIZE, len) != 0) | |
4d7b1503 DW |
4039 | return 1; |
4040 | ||
1f45a8ad DW |
4041 | memcpy(buf, super->buf, super->len); |
4042 | memset(buf + super->len, 0, len - super->len); | |
4d7b1503 DW |
4043 | free(super->buf); |
4044 | super->buf = buf; | |
4045 | super->len = len; | |
4046 | } | |
ca9de185 | 4047 | |
bbab0940 TM |
4048 | super->extra_space += space_needed; |
4049 | ||
cdddbdbc DW |
4050 | return 0; |
4051 | } | |
4052 | ||
e2f41b2c AK |
4053 | /******************************************************************************* |
4054 | * Function: check_mpb_migr_compatibility | |
4055 | * Description: Function checks for unsupported migration features: | |
4056 | * - migration optimization area (pba_of_lba0) | |
4057 | * - descending reshape (ascending_migr) | |
4058 | * Parameters: | |
4059 | * super : imsm metadata information | |
4060 | * Returns: | |
4061 | * 0 : migration is compatible | |
4062 | * -1 : migration is not compatible | |
4063 | ******************************************************************************/ | |
4064 | int check_mpb_migr_compatibility(struct intel_super *super) | |
4065 | { | |
4066 | struct imsm_map *map0, *map1; | |
4067 | struct migr_record *migr_rec = super->migr_rec; | |
4068 | int i; | |
4069 | ||
4070 | for (i = 0; i < super->anchor->num_raid_devs; i++) { | |
4071 | struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i); | |
4072 | ||
4073 | if (dev_iter && | |
4074 | dev_iter->vol.migr_state == 1 && | |
4075 | dev_iter->vol.migr_type == MIGR_GEN_MIGR) { | |
4076 | /* This device is migrating */ | |
238c0a71 AK |
4077 | map0 = get_imsm_map(dev_iter, MAP_0); |
4078 | map1 = get_imsm_map(dev_iter, MAP_1); | |
5551b113 | 4079 | if (pba_of_lba0(map0) != pba_of_lba0(map1)) |
e2f41b2c AK |
4080 | /* migration optimization area was used */ |
4081 | return -1; | |
4082 | if (migr_rec->ascending_migr == 0 | |
4083 | && migr_rec->dest_depth_per_unit > 0) | |
4084 | /* descending reshape not supported yet */ | |
4085 | return -1; | |
4086 | } | |
4087 | } | |
4088 | return 0; | |
4089 | } | |
4090 | ||
d23fe947 | 4091 | static void __free_imsm(struct intel_super *super, int free_disks); |
9ca2c81c | 4092 | |
cdddbdbc | 4093 | /* load_imsm_mpb - read matrix metadata |
f2f5c343 | 4094 | * allocates super->mpb to be freed by free_imsm |
cdddbdbc DW |
4095 | */ |
4096 | static int load_imsm_mpb(int fd, struct intel_super *super, char *devname) | |
4097 | { | |
4098 | unsigned long long dsize; | |
cdddbdbc | 4099 | unsigned long long sectors; |
f36a9ecd | 4100 | unsigned int sector_size = super->sector_size; |
cdddbdbc | 4101 | struct stat; |
6416d527 | 4102 | struct imsm_super *anchor; |
cdddbdbc DW |
4103 | __u32 check_sum; |
4104 | ||
cdddbdbc | 4105 | get_dev_size(fd, NULL, &dsize); |
f36a9ecd | 4106 | if (dsize < 2*sector_size) { |
64436f06 | 4107 | if (devname) |
e7b84f9d N |
4108 | pr_err("%s: device to small for imsm\n", |
4109 | devname); | |
64436f06 N |
4110 | return 1; |
4111 | } | |
cdddbdbc | 4112 | |
f36a9ecd | 4113 | if (lseek64(fd, dsize - (sector_size * 2), SEEK_SET) < 0) { |
cdddbdbc | 4114 | if (devname) |
e7b84f9d N |
4115 | pr_err("Cannot seek to anchor block on %s: %s\n", |
4116 | devname, strerror(errno)); | |
cdddbdbc DW |
4117 | return 1; |
4118 | } | |
4119 | ||
f36a9ecd | 4120 | if (posix_memalign((void **)&anchor, sector_size, sector_size) != 0) { |
ad97895e | 4121 | if (devname) |
7a862a02 | 4122 | pr_err("Failed to allocate imsm anchor buffer on %s\n", devname); |
ad97895e DW |
4123 | return 1; |
4124 | } | |
f36a9ecd | 4125 | if (read(fd, anchor, sector_size) != sector_size) { |
cdddbdbc | 4126 | if (devname) |
e7b84f9d N |
4127 | pr_err("Cannot read anchor block on %s: %s\n", |
4128 | devname, strerror(errno)); | |
6416d527 | 4129 | free(anchor); |
cdddbdbc DW |
4130 | return 1; |
4131 | } | |
4132 | ||
6416d527 | 4133 | if (strncmp((char *) anchor->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) { |
cdddbdbc | 4134 | if (devname) |
e7b84f9d | 4135 | pr_err("no IMSM anchor on %s\n", devname); |
6416d527 | 4136 | free(anchor); |
cdddbdbc DW |
4137 | return 2; |
4138 | } | |
4139 | ||
d23fe947 | 4140 | __free_imsm(super, 0); |
f2f5c343 LM |
4141 | /* reload capability and hba */ |
4142 | ||
4143 | /* capability and hba must be updated with new super allocation */ | |
d424212e | 4144 | find_intel_hba_capability(fd, super, devname); |
f36a9ecd PB |
4145 | super->len = ROUND_UP(anchor->mpb_size, sector_size); |
4146 | if (posix_memalign(&super->buf, MAX_SECTOR_SIZE, super->len) != 0) { | |
cdddbdbc | 4147 | if (devname) |
e7b84f9d N |
4148 | pr_err("unable to allocate %zu byte mpb buffer\n", |
4149 | super->len); | |
6416d527 | 4150 | free(anchor); |
cdddbdbc DW |
4151 | return 2; |
4152 | } | |
f36a9ecd | 4153 | memcpy(super->buf, anchor, sector_size); |
cdddbdbc | 4154 | |
f36a9ecd | 4155 | sectors = mpb_sectors(anchor, sector_size) - 1; |
6416d527 | 4156 | free(anchor); |
8e59f3d8 | 4157 | |
de44e46f PB |
4158 | if (posix_memalign(&super->migr_rec_buf, sector_size, |
4159 | MIGR_REC_BUF_SECTORS*sector_size) != 0) { | |
1ade5cc1 | 4160 | pr_err("could not allocate migr_rec buffer\n"); |
8e59f3d8 AK |
4161 | free(super->buf); |
4162 | return 2; | |
4163 | } | |
51d83f5d | 4164 | super->clean_migration_record_by_mdmon = 0; |
8e59f3d8 | 4165 | |
949c47a0 | 4166 | if (!sectors) { |
ecf45690 DW |
4167 | check_sum = __gen_imsm_checksum(super->anchor); |
4168 | if (check_sum != __le32_to_cpu(super->anchor->check_sum)) { | |
4169 | if (devname) | |
e7b84f9d N |
4170 | pr_err("IMSM checksum %x != %x on %s\n", |
4171 | check_sum, | |
4172 | __le32_to_cpu(super->anchor->check_sum), | |
4173 | devname); | |
ecf45690 DW |
4174 | return 2; |
4175 | } | |
4176 | ||
a2b97981 | 4177 | return 0; |
949c47a0 | 4178 | } |
cdddbdbc DW |
4179 | |
4180 | /* read the extended mpb */ | |
f36a9ecd | 4181 | if (lseek64(fd, dsize - (sector_size * (2 + sectors)), SEEK_SET) < 0) { |
cdddbdbc | 4182 | if (devname) |
e7b84f9d N |
4183 | pr_err("Cannot seek to extended mpb on %s: %s\n", |
4184 | devname, strerror(errno)); | |
cdddbdbc DW |
4185 | return 1; |
4186 | } | |
4187 | ||
f36a9ecd PB |
4188 | if ((unsigned int)read(fd, super->buf + sector_size, |
4189 | super->len - sector_size) != super->len - sector_size) { | |
cdddbdbc | 4190 | if (devname) |
e7b84f9d N |
4191 | pr_err("Cannot read extended mpb on %s: %s\n", |
4192 | devname, strerror(errno)); | |
cdddbdbc DW |
4193 | return 2; |
4194 | } | |
4195 | ||
949c47a0 DW |
4196 | check_sum = __gen_imsm_checksum(super->anchor); |
4197 | if (check_sum != __le32_to_cpu(super->anchor->check_sum)) { | |
cdddbdbc | 4198 | if (devname) |
e7b84f9d N |
4199 | pr_err("IMSM checksum %x != %x on %s\n", |
4200 | check_sum, __le32_to_cpu(super->anchor->check_sum), | |
4201 | devname); | |
db575f3b | 4202 | return 3; |
cdddbdbc DW |
4203 | } |
4204 | ||
a2b97981 DW |
4205 | return 0; |
4206 | } | |
4207 | ||
8e59f3d8 AK |
4208 | static int read_imsm_migr_rec(int fd, struct intel_super *super); |
4209 | ||
97f81ee2 CA |
4210 | /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */ |
4211 | static void clear_hi(struct intel_super *super) | |
4212 | { | |
4213 | struct imsm_super *mpb = super->anchor; | |
4214 | int i, n; | |
4215 | if (mpb->attributes & MPB_ATTRIB_2TB_DISK) | |
4216 | return; | |
4217 | for (i = 0; i < mpb->num_disks; ++i) { | |
4218 | struct imsm_disk *disk = &mpb->disk[i]; | |
4219 | disk->total_blocks_hi = 0; | |
4220 | } | |
4221 | for (i = 0; i < mpb->num_raid_devs; ++i) { | |
4222 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
4223 | if (!dev) | |
4224 | return; | |
4225 | for (n = 0; n < 2; ++n) { | |
4226 | struct imsm_map *map = get_imsm_map(dev, n); | |
4227 | if (!map) | |
4228 | continue; | |
4229 | map->pba_of_lba0_hi = 0; | |
4230 | map->blocks_per_member_hi = 0; | |
4231 | map->num_data_stripes_hi = 0; | |
4232 | } | |
4233 | } | |
4234 | } | |
4235 | ||
a2b97981 DW |
4236 | static int |
4237 | load_and_parse_mpb(int fd, struct intel_super *super, char *devname, int keep_fd) | |
4238 | { | |
4239 | int err; | |
4240 | ||
4241 | err = load_imsm_mpb(fd, super, devname); | |
4242 | if (err) | |
4243 | return err; | |
f36a9ecd PB |
4244 | if (super->sector_size == 4096) |
4245 | convert_from_4k(super); | |
a2b97981 DW |
4246 | err = load_imsm_disk(fd, super, devname, keep_fd); |
4247 | if (err) | |
4248 | return err; | |
4249 | err = parse_raid_devices(super); | |
8d67477f TM |
4250 | if (err) |
4251 | return err; | |
4252 | err = load_bbm_log(super); | |
97f81ee2 | 4253 | clear_hi(super); |
a2b97981 | 4254 | return err; |
cdddbdbc DW |
4255 | } |
4256 | ||
ae6aad82 DW |
4257 | static void __free_imsm_disk(struct dl *d) |
4258 | { | |
4259 | if (d->fd >= 0) | |
4260 | close(d->fd); | |
4261 | if (d->devname) | |
4262 | free(d->devname); | |
0dcecb2e DW |
4263 | if (d->e) |
4264 | free(d->e); | |
ae6aad82 DW |
4265 | free(d); |
4266 | ||
4267 | } | |
1a64be56 | 4268 | |
cdddbdbc DW |
4269 | static void free_imsm_disks(struct intel_super *super) |
4270 | { | |
47ee5a45 | 4271 | struct dl *d; |
cdddbdbc | 4272 | |
47ee5a45 DW |
4273 | while (super->disks) { |
4274 | d = super->disks; | |
cdddbdbc | 4275 | super->disks = d->next; |
ae6aad82 | 4276 | __free_imsm_disk(d); |
cdddbdbc | 4277 | } |
cb82edca AK |
4278 | while (super->disk_mgmt_list) { |
4279 | d = super->disk_mgmt_list; | |
4280 | super->disk_mgmt_list = d->next; | |
4281 | __free_imsm_disk(d); | |
4282 | } | |
47ee5a45 DW |
4283 | while (super->missing) { |
4284 | d = super->missing; | |
4285 | super->missing = d->next; | |
4286 | __free_imsm_disk(d); | |
4287 | } | |
4288 | ||
cdddbdbc DW |
4289 | } |
4290 | ||
9ca2c81c | 4291 | /* free all the pieces hanging off of a super pointer */ |
d23fe947 | 4292 | static void __free_imsm(struct intel_super *super, int free_disks) |
cdddbdbc | 4293 | { |
88654014 LM |
4294 | struct intel_hba *elem, *next; |
4295 | ||
9ca2c81c | 4296 | if (super->buf) { |
949c47a0 | 4297 | free(super->buf); |
9ca2c81c DW |
4298 | super->buf = NULL; |
4299 | } | |
f2f5c343 LM |
4300 | /* unlink capability description */ |
4301 | super->orom = NULL; | |
8e59f3d8 AK |
4302 | if (super->migr_rec_buf) { |
4303 | free(super->migr_rec_buf); | |
4304 | super->migr_rec_buf = NULL; | |
4305 | } | |
d23fe947 DW |
4306 | if (free_disks) |
4307 | free_imsm_disks(super); | |
ba2de7ba | 4308 | free_devlist(super); |
88654014 LM |
4309 | elem = super->hba; |
4310 | while (elem) { | |
4311 | if (elem->path) | |
4312 | free((void *)elem->path); | |
4313 | next = elem->next; | |
4314 | free(elem); | |
4315 | elem = next; | |
88c32bb1 | 4316 | } |
8d67477f TM |
4317 | if (super->bbm_log) |
4318 | free(super->bbm_log); | |
88654014 | 4319 | super->hba = NULL; |
cdddbdbc DW |
4320 | } |
4321 | ||
9ca2c81c DW |
4322 | static void free_imsm(struct intel_super *super) |
4323 | { | |
d23fe947 | 4324 | __free_imsm(super, 1); |
928f1424 | 4325 | free(super->bb.entries); |
9ca2c81c DW |
4326 | free(super); |
4327 | } | |
cdddbdbc DW |
4328 | |
4329 | static void free_super_imsm(struct supertype *st) | |
4330 | { | |
4331 | struct intel_super *super = st->sb; | |
4332 | ||
4333 | if (!super) | |
4334 | return; | |
4335 | ||
4336 | free_imsm(super); | |
4337 | st->sb = NULL; | |
4338 | } | |
4339 | ||
49133e57 | 4340 | static struct intel_super *alloc_super(void) |
c2c087e6 | 4341 | { |
503975b9 | 4342 | struct intel_super *super = xcalloc(1, sizeof(*super)); |
c2c087e6 | 4343 | |
503975b9 N |
4344 | super->current_vol = -1; |
4345 | super->create_offset = ~((unsigned long long) 0); | |
928f1424 TM |
4346 | |
4347 | super->bb.entries = xmalloc(BBM_LOG_MAX_ENTRIES * | |
4348 | sizeof(struct md_bb_entry)); | |
4349 | if (!super->bb.entries) { | |
4350 | free(super); | |
4351 | return NULL; | |
4352 | } | |
4353 | ||
c2c087e6 DW |
4354 | return super; |
4355 | } | |
4356 | ||
f0f5a016 LM |
4357 | /* |
4358 | * find and allocate hba and OROM/EFI based on valid fd of RAID component device | |
4359 | */ | |
d424212e | 4360 | static int find_intel_hba_capability(int fd, struct intel_super *super, char *devname) |
f0f5a016 LM |
4361 | { |
4362 | struct sys_dev *hba_name; | |
4363 | int rv = 0; | |
4364 | ||
089f9d79 | 4365 | if (fd < 0 || check_env("IMSM_NO_PLATFORM")) { |
f2f5c343 | 4366 | super->orom = NULL; |
f0f5a016 LM |
4367 | super->hba = NULL; |
4368 | return 0; | |
4369 | } | |
4370 | hba_name = find_disk_attached_hba(fd, NULL); | |
4371 | if (!hba_name) { | |
d424212e | 4372 | if (devname) |
e7b84f9d N |
4373 | pr_err("%s is not attached to Intel(R) RAID controller.\n", |
4374 | devname); | |
f0f5a016 LM |
4375 | return 1; |
4376 | } | |
4377 | rv = attach_hba_to_super(super, hba_name); | |
4378 | if (rv == 2) { | |
d424212e N |
4379 | if (devname) { |
4380 | struct intel_hba *hba = super->hba; | |
f0f5a016 | 4381 | |
60f0f54d PB |
4382 | pr_err("%s is attached to Intel(R) %s %s (%s),\n" |
4383 | " but the container is assigned to Intel(R) %s %s (", | |
d424212e | 4384 | devname, |
614902f6 | 4385 | get_sys_dev_type(hba_name->type), |
60f0f54d | 4386 | hba_name->type == SYS_DEV_VMD ? "domain" : "RAID controller", |
f0f5a016 | 4387 | hba_name->pci_id ? : "Err!", |
60f0f54d PB |
4388 | get_sys_dev_type(super->hba->type), |
4389 | hba->type == SYS_DEV_VMD ? "domain" : "RAID controller"); | |
f0f5a016 | 4390 | |
f0f5a016 LM |
4391 | while (hba) { |
4392 | fprintf(stderr, "%s", hba->pci_id ? : "Err!"); | |
4393 | if (hba->next) | |
4394 | fprintf(stderr, ", "); | |
4395 | hba = hba->next; | |
4396 | } | |
6b781d33 | 4397 | fprintf(stderr, ").\n" |
60f0f54d PB |
4398 | " Mixing devices attached to different %s is not allowed.\n", |
4399 | hba_name->type == SYS_DEV_VMD ? "VMD domains" : "controllers"); | |
f0f5a016 | 4400 | } |
f0f5a016 LM |
4401 | return 2; |
4402 | } | |
6b781d33 | 4403 | super->orom = find_imsm_capability(hba_name); |
f2f5c343 LM |
4404 | if (!super->orom) |
4405 | return 3; | |
614902f6 | 4406 | |
f0f5a016 LM |
4407 | return 0; |
4408 | } | |
4409 | ||
47ee5a45 DW |
4410 | /* find_missing - helper routine for load_super_imsm_all that identifies |
4411 | * disks that have disappeared from the system. This routine relies on | |
4412 | * the mpb being uptodate, which it is at load time. | |
4413 | */ | |
4414 | static int find_missing(struct intel_super *super) | |
4415 | { | |
4416 | int i; | |
4417 | struct imsm_super *mpb = super->anchor; | |
4418 | struct dl *dl; | |
4419 | struct imsm_disk *disk; | |
47ee5a45 DW |
4420 | |
4421 | for (i = 0; i < mpb->num_disks; i++) { | |
4422 | disk = __get_imsm_disk(mpb, i); | |
54c2c1ea | 4423 | dl = serial_to_dl(disk->serial, super); |
47ee5a45 DW |
4424 | if (dl) |
4425 | continue; | |
47ee5a45 | 4426 | |
503975b9 | 4427 | dl = xmalloc(sizeof(*dl)); |
47ee5a45 DW |
4428 | dl->major = 0; |
4429 | dl->minor = 0; | |
4430 | dl->fd = -1; | |
503975b9 | 4431 | dl->devname = xstrdup("missing"); |
47ee5a45 DW |
4432 | dl->index = i; |
4433 | serialcpy(dl->serial, disk->serial); | |
4434 | dl->disk = *disk; | |
689c9bf3 | 4435 | dl->e = NULL; |
47ee5a45 DW |
4436 | dl->next = super->missing; |
4437 | super->missing = dl; | |
4438 | } | |
4439 | ||
4440 | return 0; | |
4441 | } | |
4442 | ||
3960e579 | 4443 | #ifndef MDASSEMBLE |
a2b97981 DW |
4444 | static struct intel_disk *disk_list_get(__u8 *serial, struct intel_disk *disk_list) |
4445 | { | |
4446 | struct intel_disk *idisk = disk_list; | |
4447 | ||
4448 | while (idisk) { | |
4449 | if (serialcmp(idisk->disk.serial, serial) == 0) | |
4450 | break; | |
4451 | idisk = idisk->next; | |
4452 | } | |
4453 | ||
4454 | return idisk; | |
4455 | } | |
4456 | ||
4457 | static int __prep_thunderdome(struct intel_super **table, int tbl_size, | |
4458 | struct intel_super *super, | |
4459 | struct intel_disk **disk_list) | |
4460 | { | |
4461 | struct imsm_disk *d = &super->disks->disk; | |
4462 | struct imsm_super *mpb = super->anchor; | |
4463 | int i, j; | |
4464 | ||
4465 | for (i = 0; i < tbl_size; i++) { | |
4466 | struct imsm_super *tbl_mpb = table[i]->anchor; | |
4467 | struct imsm_disk *tbl_d = &table[i]->disks->disk; | |
4468 | ||
4469 | if (tbl_mpb->family_num == mpb->family_num) { | |
4470 | if (tbl_mpb->check_sum == mpb->check_sum) { | |
1ade5cc1 N |
4471 | dprintf("mpb from %d:%d matches %d:%d\n", |
4472 | super->disks->major, | |
a2b97981 DW |
4473 | super->disks->minor, |
4474 | table[i]->disks->major, | |
4475 | table[i]->disks->minor); | |
4476 | break; | |
4477 | } | |
4478 | ||
4479 | if (((is_configured(d) && !is_configured(tbl_d)) || | |
4480 | is_configured(d) == is_configured(tbl_d)) && | |
4481 | tbl_mpb->generation_num < mpb->generation_num) { | |
4482 | /* current version of the mpb is a | |
4483 | * better candidate than the one in | |
4484 | * super_table, but copy over "cross | |
4485 | * generational" status | |
4486 | */ | |
4487 | struct intel_disk *idisk; | |
4488 | ||
1ade5cc1 N |
4489 | dprintf("mpb from %d:%d replaces %d:%d\n", |
4490 | super->disks->major, | |
a2b97981 DW |
4491 | super->disks->minor, |
4492 | table[i]->disks->major, | |
4493 | table[i]->disks->minor); | |
4494 | ||
4495 | idisk = disk_list_get(tbl_d->serial, *disk_list); | |
4496 | if (idisk && is_failed(&idisk->disk)) | |
4497 | tbl_d->status |= FAILED_DISK; | |
4498 | break; | |
4499 | } else { | |
4500 | struct intel_disk *idisk; | |
4501 | struct imsm_disk *disk; | |
4502 | ||
4503 | /* tbl_mpb is more up to date, but copy | |
4504 | * over cross generational status before | |
4505 | * returning | |
4506 | */ | |
4507 | disk = __serial_to_disk(d->serial, mpb, NULL); | |
4508 | if (disk && is_failed(disk)) | |
4509 | d->status |= FAILED_DISK; | |
4510 | ||
4511 | idisk = disk_list_get(d->serial, *disk_list); | |
4512 | if (idisk) { | |
4513 | idisk->owner = i; | |
4514 | if (disk && is_configured(disk)) | |
4515 | idisk->disk.status |= CONFIGURED_DISK; | |
4516 | } | |
4517 | ||
1ade5cc1 N |
4518 | dprintf("mpb from %d:%d prefer %d:%d\n", |
4519 | super->disks->major, | |
a2b97981 DW |
4520 | super->disks->minor, |
4521 | table[i]->disks->major, | |
4522 | table[i]->disks->minor); | |
4523 | ||
4524 | return tbl_size; | |
4525 | } | |
4526 | } | |
4527 | } | |
4528 | ||
4529 | if (i >= tbl_size) | |
4530 | table[tbl_size++] = super; | |
4531 | else | |
4532 | table[i] = super; | |
4533 | ||
4534 | /* update/extend the merged list of imsm_disk records */ | |
4535 | for (j = 0; j < mpb->num_disks; j++) { | |
4536 | struct imsm_disk *disk = __get_imsm_disk(mpb, j); | |
4537 | struct intel_disk *idisk; | |
4538 | ||
4539 | idisk = disk_list_get(disk->serial, *disk_list); | |
4540 | if (idisk) { | |
4541 | idisk->disk.status |= disk->status; | |
4542 | if (is_configured(&idisk->disk) || | |
4543 | is_failed(&idisk->disk)) | |
4544 | idisk->disk.status &= ~(SPARE_DISK); | |
4545 | } else { | |
503975b9 | 4546 | idisk = xcalloc(1, sizeof(*idisk)); |
a2b97981 DW |
4547 | idisk->owner = IMSM_UNKNOWN_OWNER; |
4548 | idisk->disk = *disk; | |
4549 | idisk->next = *disk_list; | |
4550 | *disk_list = idisk; | |
4551 | } | |
4552 | ||
4553 | if (serialcmp(idisk->disk.serial, d->serial) == 0) | |
4554 | idisk->owner = i; | |
4555 | } | |
4556 | ||
4557 | return tbl_size; | |
4558 | } | |
4559 | ||
4560 | static struct intel_super * | |
4561 | validate_members(struct intel_super *super, struct intel_disk *disk_list, | |
4562 | const int owner) | |
4563 | { | |
4564 | struct imsm_super *mpb = super->anchor; | |
4565 | int ok_count = 0; | |
4566 | int i; | |
4567 | ||
4568 | for (i = 0; i < mpb->num_disks; i++) { | |
4569 | struct imsm_disk *disk = __get_imsm_disk(mpb, i); | |
4570 | struct intel_disk *idisk; | |
4571 | ||
4572 | idisk = disk_list_get(disk->serial, disk_list); | |
4573 | if (idisk) { | |
4574 | if (idisk->owner == owner || | |
4575 | idisk->owner == IMSM_UNKNOWN_OWNER) | |
4576 | ok_count++; | |
4577 | else | |
1ade5cc1 N |
4578 | dprintf("'%.16s' owner %d != %d\n", |
4579 | disk->serial, idisk->owner, | |
a2b97981 DW |
4580 | owner); |
4581 | } else { | |
1ade5cc1 N |
4582 | dprintf("unknown disk %x [%d]: %.16s\n", |
4583 | __le32_to_cpu(mpb->family_num), i, | |
a2b97981 DW |
4584 | disk->serial); |
4585 | break; | |
4586 | } | |
4587 | } | |
4588 | ||
4589 | if (ok_count == mpb->num_disks) | |
4590 | return super; | |
4591 | return NULL; | |
4592 | } | |
4593 | ||
4594 | static void show_conflicts(__u32 family_num, struct intel_super *super_list) | |
4595 | { | |
4596 | struct intel_super *s; | |
4597 | ||
4598 | for (s = super_list; s; s = s->next) { | |
4599 | if (family_num != s->anchor->family_num) | |
4600 | continue; | |
e12b3daa | 4601 | pr_err("Conflict, offlining family %#x on '%s'\n", |
a2b97981 DW |
4602 | __le32_to_cpu(family_num), s->disks->devname); |
4603 | } | |
4604 | } | |
4605 | ||
4606 | static struct intel_super * | |
4607 | imsm_thunderdome(struct intel_super **super_list, int len) | |
4608 | { | |
4609 | struct intel_super *super_table[len]; | |
4610 | struct intel_disk *disk_list = NULL; | |
4611 | struct intel_super *champion, *spare; | |
4612 | struct intel_super *s, **del; | |
4613 | int tbl_size = 0; | |
4614 | int conflict; | |
4615 | int i; | |
4616 | ||
4617 | memset(super_table, 0, sizeof(super_table)); | |
4618 | for (s = *super_list; s; s = s->next) | |
4619 | tbl_size = __prep_thunderdome(super_table, tbl_size, s, &disk_list); | |
4620 | ||
4621 | for (i = 0; i < tbl_size; i++) { | |
4622 | struct imsm_disk *d; | |
4623 | struct intel_disk *idisk; | |
4624 | struct imsm_super *mpb = super_table[i]->anchor; | |
4625 | ||
4626 | s = super_table[i]; | |
4627 | d = &s->disks->disk; | |
4628 | ||
4629 | /* 'd' must appear in merged disk list for its | |
4630 | * configuration to be valid | |
4631 | */ | |
4632 | idisk = disk_list_get(d->serial, disk_list); | |
4633 | if (idisk && idisk->owner == i) | |
4634 | s = validate_members(s, disk_list, i); | |
4635 | else | |
4636 | s = NULL; | |
4637 | ||
4638 | if (!s) | |
1ade5cc1 N |
4639 | dprintf("marking family: %#x from %d:%d offline\n", |
4640 | mpb->family_num, | |
a2b97981 DW |
4641 | super_table[i]->disks->major, |
4642 | super_table[i]->disks->minor); | |
4643 | super_table[i] = s; | |
4644 | } | |
4645 | ||
4646 | /* This is where the mdadm implementation differs from the Windows | |
4647 | * driver which has no strict concept of a container. We can only | |
4648 | * assemble one family from a container, so when returning a prodigal | |
4649 | * array member to this system the code will not be able to disambiguate | |
4650 | * the container contents that should be assembled ("foreign" versus | |
4651 | * "local"). It requires user intervention to set the orig_family_num | |
4652 | * to a new value to establish a new container. The Windows driver in | |
4653 | * this situation fixes up the volume name in place and manages the | |
4654 | * foreign array as an independent entity. | |
4655 | */ | |
4656 | s = NULL; | |
4657 | spare = NULL; | |
4658 | conflict = 0; | |
4659 | for (i = 0; i < tbl_size; i++) { | |
4660 | struct intel_super *tbl_ent = super_table[i]; | |
4661 | int is_spare = 0; | |
4662 | ||
4663 | if (!tbl_ent) | |
4664 | continue; | |
4665 | ||
4666 | if (tbl_ent->anchor->num_raid_devs == 0) { | |
4667 | spare = tbl_ent; | |
4668 | is_spare = 1; | |
4669 | } | |
4670 | ||
4671 | if (s && !is_spare) { | |
4672 | show_conflicts(tbl_ent->anchor->family_num, *super_list); | |
4673 | conflict++; | |
4674 | } else if (!s && !is_spare) | |
4675 | s = tbl_ent; | |
4676 | } | |
4677 | ||
4678 | if (!s) | |
4679 | s = spare; | |
4680 | if (!s) { | |
4681 | champion = NULL; | |
4682 | goto out; | |
4683 | } | |
4684 | champion = s; | |
4685 | ||
4686 | if (conflict) | |
7a862a02 | 4687 | pr_err("Chose family %#x on '%s', assemble conflicts to new container with '--update=uuid'\n", |
a2b97981 DW |
4688 | __le32_to_cpu(s->anchor->family_num), s->disks->devname); |
4689 | ||
4690 | /* collect all dl's onto 'champion', and update them to | |
4691 | * champion's version of the status | |
4692 | */ | |
4693 | for (s = *super_list; s; s = s->next) { | |
4694 | struct imsm_super *mpb = champion->anchor; | |
4695 | struct dl *dl = s->disks; | |
4696 | ||
4697 | if (s == champion) | |
4698 | continue; | |
4699 | ||
5d7b407a CA |
4700 | mpb->attributes |= s->anchor->attributes & MPB_ATTRIB_2TB_DISK; |
4701 | ||
a2b97981 DW |
4702 | for (i = 0; i < mpb->num_disks; i++) { |
4703 | struct imsm_disk *disk; | |
4704 | ||
4705 | disk = __serial_to_disk(dl->serial, mpb, &dl->index); | |
4706 | if (disk) { | |
4707 | dl->disk = *disk; | |
4708 | /* only set index on disks that are a member of | |
4709 | * a populated contianer, i.e. one with | |
4710 | * raid_devs | |
4711 | */ | |
4712 | if (is_failed(&dl->disk)) | |
4713 | dl->index = -2; | |
4714 | else if (is_spare(&dl->disk)) | |
4715 | dl->index = -1; | |
4716 | break; | |
4717 | } | |
4718 | } | |
4719 | ||
4720 | if (i >= mpb->num_disks) { | |
4721 | struct intel_disk *idisk; | |
4722 | ||
4723 | idisk = disk_list_get(dl->serial, disk_list); | |
ecf408e9 | 4724 | if (idisk && is_spare(&idisk->disk) && |
a2b97981 DW |
4725 | !is_failed(&idisk->disk) && !is_configured(&idisk->disk)) |
4726 | dl->index = -1; | |
4727 | else { | |
4728 | dl->index = -2; | |
4729 | continue; | |
4730 | } | |
4731 | } | |
4732 | ||
4733 | dl->next = champion->disks; | |
4734 | champion->disks = dl; | |
4735 | s->disks = NULL; | |
4736 | } | |
4737 | ||
4738 | /* delete 'champion' from super_list */ | |
4739 | for (del = super_list; *del; ) { | |
4740 | if (*del == champion) { | |
4741 | *del = (*del)->next; | |
4742 | break; | |
4743 | } else | |
4744 | del = &(*del)->next; | |
4745 | } | |
4746 | champion->next = NULL; | |
4747 | ||
4748 | out: | |
4749 | while (disk_list) { | |
4750 | struct intel_disk *idisk = disk_list; | |
4751 | ||
4752 | disk_list = disk_list->next; | |
4753 | free(idisk); | |
4754 | } | |
4755 | ||
4756 | return champion; | |
4757 | } | |
4758 | ||
9587c373 LM |
4759 | static int |
4760 | get_sra_super_block(int fd, struct intel_super **super_list, char *devname, int *max, int keep_fd); | |
4dd2df09 | 4761 | static int get_super_block(struct intel_super **super_list, char *devnm, char *devname, |
9587c373 | 4762 | int major, int minor, int keep_fd); |
ec50f7b6 LM |
4763 | static int |
4764 | get_devlist_super_block(struct md_list *devlist, struct intel_super **super_list, | |
4765 | int *max, int keep_fd); | |
4766 | ||
cdddbdbc | 4767 | static int load_super_imsm_all(struct supertype *st, int fd, void **sbp, |
ec50f7b6 LM |
4768 | char *devname, struct md_list *devlist, |
4769 | int keep_fd) | |
cdddbdbc | 4770 | { |
a2b97981 DW |
4771 | struct intel_super *super_list = NULL; |
4772 | struct intel_super *super = NULL; | |
a2b97981 | 4773 | int err = 0; |
9587c373 | 4774 | int i = 0; |
dab4a513 | 4775 | |
9587c373 LM |
4776 | if (fd >= 0) |
4777 | /* 'fd' is an opened container */ | |
4778 | err = get_sra_super_block(fd, &super_list, devname, &i, keep_fd); | |
4779 | else | |
ec50f7b6 LM |
4780 | /* get super block from devlist devices */ |
4781 | err = get_devlist_super_block(devlist, &super_list, &i, keep_fd); | |
9587c373 | 4782 | if (err) |
1602d52c | 4783 | goto error; |
a2b97981 DW |
4784 | /* all mpbs enter, maybe one leaves */ |
4785 | super = imsm_thunderdome(&super_list, i); | |
4786 | if (!super) { | |
4787 | err = 1; | |
4788 | goto error; | |
cdddbdbc DW |
4789 | } |
4790 | ||
47ee5a45 DW |
4791 | if (find_missing(super) != 0) { |
4792 | free_imsm(super); | |
a2b97981 DW |
4793 | err = 2; |
4794 | goto error; | |
47ee5a45 | 4795 | } |
8e59f3d8 AK |
4796 | |
4797 | /* load migration record */ | |
4798 | err = load_imsm_migr_rec(super, NULL); | |
4c965cc9 AK |
4799 | if (err == -1) { |
4800 | /* migration is in progress, | |
4801 | * but migr_rec cannot be loaded, | |
4802 | */ | |
8e59f3d8 AK |
4803 | err = 4; |
4804 | goto error; | |
4805 | } | |
e2f41b2c AK |
4806 | |
4807 | /* Check migration compatibility */ | |
089f9d79 | 4808 | if (err == 0 && check_mpb_migr_compatibility(super) != 0) { |
e7b84f9d | 4809 | pr_err("Unsupported migration detected"); |
e2f41b2c AK |
4810 | if (devname) |
4811 | fprintf(stderr, " on %s\n", devname); | |
4812 | else | |
4813 | fprintf(stderr, " (IMSM).\n"); | |
4814 | ||
4815 | err = 5; | |
4816 | goto error; | |
4817 | } | |
4818 | ||
a2b97981 DW |
4819 | err = 0; |
4820 | ||
4821 | error: | |
4822 | while (super_list) { | |
4823 | struct intel_super *s = super_list; | |
4824 | ||
4825 | super_list = super_list->next; | |
4826 | free_imsm(s); | |
4827 | } | |
9587c373 | 4828 | |
a2b97981 DW |
4829 | if (err) |
4830 | return err; | |
f7e7067b | 4831 | |
cdddbdbc | 4832 | *sbp = super; |
9587c373 | 4833 | if (fd >= 0) |
4dd2df09 | 4834 | strcpy(st->container_devnm, fd2devnm(fd)); |
9587c373 | 4835 | else |
4dd2df09 | 4836 | st->container_devnm[0] = 0; |
a2b97981 | 4837 | if (err == 0 && st->ss == NULL) { |
bf5a934a | 4838 | st->ss = &super_imsm; |
cdddbdbc DW |
4839 | st->minor_version = 0; |
4840 | st->max_devs = IMSM_MAX_DEVICES; | |
4841 | } | |
cdddbdbc DW |
4842 | return 0; |
4843 | } | |
2b959fbf | 4844 | |
ec50f7b6 LM |
4845 | static int |
4846 | get_devlist_super_block(struct md_list *devlist, struct intel_super **super_list, | |
4847 | int *max, int keep_fd) | |
4848 | { | |
4849 | struct md_list *tmpdev; | |
4850 | int err = 0; | |
4851 | int i = 0; | |
9587c373 | 4852 | |
ec50f7b6 LM |
4853 | for (i = 0, tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) { |
4854 | if (tmpdev->used != 1) | |
4855 | continue; | |
4856 | if (tmpdev->container == 1) { | |
ca9de185 | 4857 | int lmax = 0; |
ec50f7b6 LM |
4858 | int fd = dev_open(tmpdev->devname, O_RDONLY|O_EXCL); |
4859 | if (fd < 0) { | |
e7b84f9d | 4860 | pr_err("cannot open device %s: %s\n", |
ec50f7b6 LM |
4861 | tmpdev->devname, strerror(errno)); |
4862 | err = 8; | |
4863 | goto error; | |
4864 | } | |
4865 | err = get_sra_super_block(fd, super_list, | |
4866 | tmpdev->devname, &lmax, | |
4867 | keep_fd); | |
4868 | i += lmax; | |
4869 | close(fd); | |
4870 | if (err) { | |
4871 | err = 7; | |
4872 | goto error; | |
4873 | } | |
4874 | } else { | |
4875 | int major = major(tmpdev->st_rdev); | |
4876 | int minor = minor(tmpdev->st_rdev); | |
4877 | err = get_super_block(super_list, | |
4dd2df09 | 4878 | NULL, |
ec50f7b6 LM |
4879 | tmpdev->devname, |
4880 | major, minor, | |
4881 | keep_fd); | |
4882 | i++; | |
4883 | if (err) { | |
4884 | err = 6; | |
4885 | goto error; | |
4886 | } | |
4887 | } | |
4888 | } | |
4889 | error: | |
4890 | *max = i; | |
4891 | return err; | |
4892 | } | |
9587c373 | 4893 | |
4dd2df09 | 4894 | static int get_super_block(struct intel_super **super_list, char *devnm, char *devname, |
9587c373 LM |
4895 | int major, int minor, int keep_fd) |
4896 | { | |
594dc1b8 | 4897 | struct intel_super *s; |
9587c373 LM |
4898 | char nm[32]; |
4899 | int dfd = -1; | |
9587c373 LM |
4900 | int err = 0; |
4901 | int retry; | |
4902 | ||
4903 | s = alloc_super(); | |
4904 | if (!s) { | |
4905 | err = 1; | |
4906 | goto error; | |
4907 | } | |
4908 | ||
4909 | sprintf(nm, "%d:%d", major, minor); | |
4910 | dfd = dev_open(nm, O_RDWR); | |
4911 | if (dfd < 0) { | |
4912 | err = 2; | |
4913 | goto error; | |
4914 | } | |
4915 | ||
fa7bb6f8 | 4916 | get_dev_sector_size(dfd, NULL, &s->sector_size); |
cb8f6859 | 4917 | find_intel_hba_capability(dfd, s, devname); |
9587c373 LM |
4918 | err = load_and_parse_mpb(dfd, s, NULL, keep_fd); |
4919 | ||
4920 | /* retry the load if we might have raced against mdmon */ | |
4dd2df09 | 4921 | if (err == 3 && devnm && mdmon_running(devnm)) |
9587c373 LM |
4922 | for (retry = 0; retry < 3; retry++) { |
4923 | usleep(3000); | |
4924 | err = load_and_parse_mpb(dfd, s, NULL, keep_fd); | |
4925 | if (err != 3) | |
4926 | break; | |
4927 | } | |
4928 | error: | |
4929 | if (!err) { | |
4930 | s->next = *super_list; | |
4931 | *super_list = s; | |
4932 | } else { | |
4933 | if (s) | |
8d67477f | 4934 | free_imsm(s); |
36614e95 | 4935 | if (dfd >= 0) |
9587c373 LM |
4936 | close(dfd); |
4937 | } | |
089f9d79 | 4938 | if (dfd >= 0 && !keep_fd) |
9587c373 LM |
4939 | close(dfd); |
4940 | return err; | |
4941 | ||
4942 | } | |
4943 | ||
4944 | static int | |
4945 | get_sra_super_block(int fd, struct intel_super **super_list, char *devname, int *max, int keep_fd) | |
4946 | { | |
4947 | struct mdinfo *sra; | |
4dd2df09 | 4948 | char *devnm; |
9587c373 LM |
4949 | struct mdinfo *sd; |
4950 | int err = 0; | |
4951 | int i = 0; | |
4dd2df09 | 4952 | sra = sysfs_read(fd, NULL, GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE); |
9587c373 LM |
4953 | if (!sra) |
4954 | return 1; | |
4955 | ||
4956 | if (sra->array.major_version != -1 || | |
4957 | sra->array.minor_version != -2 || | |
4958 | strcmp(sra->text_version, "imsm") != 0) { | |
4959 | err = 1; | |
4960 | goto error; | |
4961 | } | |
4962 | /* load all mpbs */ | |
4dd2df09 | 4963 | devnm = fd2devnm(fd); |
9587c373 | 4964 | for (sd = sra->devs, i = 0; sd; sd = sd->next, i++) { |
4dd2df09 | 4965 | if (get_super_block(super_list, devnm, devname, |
9587c373 LM |
4966 | sd->disk.major, sd->disk.minor, keep_fd) != 0) { |
4967 | err = 7; | |
4968 | goto error; | |
4969 | } | |
4970 | } | |
4971 | error: | |
4972 | sysfs_free(sra); | |
4973 | *max = i; | |
4974 | return err; | |
4975 | } | |
4976 | ||
2b959fbf N |
4977 | static int load_container_imsm(struct supertype *st, int fd, char *devname) |
4978 | { | |
ec50f7b6 | 4979 | return load_super_imsm_all(st, fd, &st->sb, devname, NULL, 1); |
2b959fbf | 4980 | } |
cdddbdbc DW |
4981 | #endif |
4982 | ||
4983 | static int load_super_imsm(struct supertype *st, int fd, char *devname) | |
4984 | { | |
4985 | struct intel_super *super; | |
4986 | int rv; | |
8a3544f8 | 4987 | int retry; |
cdddbdbc | 4988 | |
357ac106 | 4989 | if (test_partition(fd)) |
691c6ee1 N |
4990 | /* IMSM not allowed on partitions */ |
4991 | return 1; | |
4992 | ||
37424f13 DW |
4993 | free_super_imsm(st); |
4994 | ||
49133e57 | 4995 | super = alloc_super(); |
fa7bb6f8 | 4996 | get_dev_sector_size(fd, NULL, &super->sector_size); |
8d67477f TM |
4997 | if (!super) |
4998 | return 1; | |
ea2bc72b LM |
4999 | /* Load hba and capabilities if they exist. |
5000 | * But do not preclude loading metadata in case capabilities or hba are | |
5001 | * non-compliant and ignore_hw_compat is set. | |
5002 | */ | |
d424212e | 5003 | rv = find_intel_hba_capability(fd, super, devname); |
f2f5c343 | 5004 | /* no orom/efi or non-intel hba of the disk */ |
089f9d79 | 5005 | if (rv != 0 && st->ignore_hw_compat == 0) { |
f2f5c343 | 5006 | if (devname) |
e7b84f9d | 5007 | pr_err("No OROM/EFI properties for %s\n", devname); |
f2f5c343 LM |
5008 | free_imsm(super); |
5009 | return 2; | |
5010 | } | |
a2b97981 | 5011 | rv = load_and_parse_mpb(fd, super, devname, 0); |
cdddbdbc | 5012 | |
8a3544f8 AP |
5013 | /* retry the load if we might have raced against mdmon */ |
5014 | if (rv == 3) { | |
f96b1302 AP |
5015 | struct mdstat_ent *mdstat = NULL; |
5016 | char *name = fd2kname(fd); | |
5017 | ||
5018 | if (name) | |
5019 | mdstat = mdstat_by_component(name); | |
8a3544f8 AP |
5020 | |
5021 | if (mdstat && mdmon_running(mdstat->devnm) && getpid() != mdmon_pid(mdstat->devnm)) { | |
5022 | for (retry = 0; retry < 3; retry++) { | |
5023 | usleep(3000); | |
5024 | rv = load_and_parse_mpb(fd, super, devname, 0); | |
5025 | if (rv != 3) | |
5026 | break; | |
5027 | } | |
5028 | } | |
5029 | ||
5030 | free_mdstat(mdstat); | |
5031 | } | |
5032 | ||
cdddbdbc DW |
5033 | if (rv) { |
5034 | if (devname) | |
7a862a02 | 5035 | pr_err("Failed to load all information sections on %s\n", devname); |
cdddbdbc DW |
5036 | free_imsm(super); |
5037 | return rv; | |
5038 | } | |
5039 | ||
5040 | st->sb = super; | |
5041 | if (st->ss == NULL) { | |
5042 | st->ss = &super_imsm; | |
5043 | st->minor_version = 0; | |
5044 | st->max_devs = IMSM_MAX_DEVICES; | |
5045 | } | |
8e59f3d8 AK |
5046 | |
5047 | /* load migration record */ | |
2e062e82 AK |
5048 | if (load_imsm_migr_rec(super, NULL) == 0) { |
5049 | /* Check for unsupported migration features */ | |
5050 | if (check_mpb_migr_compatibility(super) != 0) { | |
e7b84f9d | 5051 | pr_err("Unsupported migration detected"); |
2e062e82 AK |
5052 | if (devname) |
5053 | fprintf(stderr, " on %s\n", devname); | |
5054 | else | |
5055 | fprintf(stderr, " (IMSM).\n"); | |
5056 | return 3; | |
5057 | } | |
e2f41b2c AK |
5058 | } |
5059 | ||
cdddbdbc DW |
5060 | return 0; |
5061 | } | |
5062 | ||
ef6ffade DW |
5063 | static __u16 info_to_blocks_per_strip(mdu_array_info_t *info) |
5064 | { | |
5065 | if (info->level == 1) | |
5066 | return 128; | |
5067 | return info->chunk_size >> 9; | |
5068 | } | |
5069 | ||
5551b113 CA |
5070 | static unsigned long long info_to_blocks_per_member(mdu_array_info_t *info, |
5071 | unsigned long long size) | |
fcfd9599 | 5072 | { |
4025c288 | 5073 | if (info->level == 1) |
5551b113 | 5074 | return size * 2; |
4025c288 | 5075 | else |
5551b113 | 5076 | return (size * 2) & ~(info_to_blocks_per_strip(info) - 1); |
fcfd9599 DW |
5077 | } |
5078 | ||
4d1313e9 DW |
5079 | static void imsm_update_version_info(struct intel_super *super) |
5080 | { | |
5081 | /* update the version and attributes */ | |
5082 | struct imsm_super *mpb = super->anchor; | |
5083 | char *version; | |
5084 | struct imsm_dev *dev; | |
5085 | struct imsm_map *map; | |
5086 | int i; | |
5087 | ||
5088 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
5089 | dev = get_imsm_dev(super, i); | |
238c0a71 | 5090 | map = get_imsm_map(dev, MAP_0); |
4d1313e9 DW |
5091 | if (__le32_to_cpu(dev->size_high) > 0) |
5092 | mpb->attributes |= MPB_ATTRIB_2TB; | |
5093 | ||
5094 | /* FIXME detect when an array spans a port multiplier */ | |
5095 | #if 0 | |
5096 | mpb->attributes |= MPB_ATTRIB_PM; | |
5097 | #endif | |
5098 | ||
5099 | if (mpb->num_raid_devs > 1 || | |
5100 | mpb->attributes != MPB_ATTRIB_CHECKSUM_VERIFY) { | |
5101 | version = MPB_VERSION_ATTRIBS; | |
5102 | switch (get_imsm_raid_level(map)) { | |
5103 | case 0: mpb->attributes |= MPB_ATTRIB_RAID0; break; | |
5104 | case 1: mpb->attributes |= MPB_ATTRIB_RAID1; break; | |
5105 | case 10: mpb->attributes |= MPB_ATTRIB_RAID10; break; | |
5106 | case 5: mpb->attributes |= MPB_ATTRIB_RAID5; break; | |
5107 | } | |
5108 | } else { | |
5109 | if (map->num_members >= 5) | |
5110 | version = MPB_VERSION_5OR6_DISK_ARRAY; | |
5111 | else if (dev->status == DEV_CLONE_N_GO) | |
5112 | version = MPB_VERSION_CNG; | |
5113 | else if (get_imsm_raid_level(map) == 5) | |
5114 | version = MPB_VERSION_RAID5; | |
5115 | else if (map->num_members >= 3) | |
5116 | version = MPB_VERSION_3OR4_DISK_ARRAY; | |
5117 | else if (get_imsm_raid_level(map) == 1) | |
5118 | version = MPB_VERSION_RAID1; | |
5119 | else | |
5120 | version = MPB_VERSION_RAID0; | |
5121 | } | |
5122 | strcpy(((char *) mpb->sig) + strlen(MPB_SIGNATURE), version); | |
5123 | } | |
5124 | } | |
5125 | ||
aa534678 DW |
5126 | static int check_name(struct intel_super *super, char *name, int quiet) |
5127 | { | |
5128 | struct imsm_super *mpb = super->anchor; | |
5129 | char *reason = NULL; | |
5130 | int i; | |
5131 | ||
5132 | if (strlen(name) > MAX_RAID_SERIAL_LEN) | |
5133 | reason = "must be 16 characters or less"; | |
5134 | ||
5135 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
5136 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
5137 | ||
5138 | if (strncmp((char *) dev->volume, name, MAX_RAID_SERIAL_LEN) == 0) { | |
5139 | reason = "already exists"; | |
5140 | break; | |
5141 | } | |
5142 | } | |
5143 | ||
5144 | if (reason && !quiet) | |
e7b84f9d | 5145 | pr_err("imsm volume name %s\n", reason); |
aa534678 DW |
5146 | |
5147 | return !reason; | |
5148 | } | |
5149 | ||
8b353278 DW |
5150 | static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info, |
5151 | unsigned long long size, char *name, | |
83cd1e97 N |
5152 | char *homehost, int *uuid, |
5153 | long long data_offset) | |
cdddbdbc | 5154 | { |
c2c087e6 DW |
5155 | /* We are creating a volume inside a pre-existing container. |
5156 | * so st->sb is already set. | |
5157 | */ | |
5158 | struct intel_super *super = st->sb; | |
f36a9ecd | 5159 | unsigned int sector_size = super->sector_size; |
949c47a0 | 5160 | struct imsm_super *mpb = super->anchor; |
ba2de7ba | 5161 | struct intel_dev *dv; |
c2c087e6 DW |
5162 | struct imsm_dev *dev; |
5163 | struct imsm_vol *vol; | |
5164 | struct imsm_map *map; | |
5165 | int idx = mpb->num_raid_devs; | |
5166 | int i; | |
5167 | unsigned long long array_blocks; | |
2c092cad | 5168 | size_t size_old, size_new; |
5551b113 | 5169 | unsigned long long num_data_stripes; |
cdddbdbc | 5170 | |
88c32bb1 | 5171 | if (super->orom && mpb->num_raid_devs >= super->orom->vpa) { |
7a862a02 | 5172 | pr_err("This imsm-container already has the maximum of %d volumes\n", super->orom->vpa); |
c2c087e6 DW |
5173 | return 0; |
5174 | } | |
5175 | ||
2c092cad DW |
5176 | /* ensure the mpb is large enough for the new data */ |
5177 | size_old = __le32_to_cpu(mpb->mpb_size); | |
5178 | size_new = disks_to_mpb_size(info->nr_disks); | |
5179 | if (size_new > size_old) { | |
5180 | void *mpb_new; | |
f36a9ecd | 5181 | size_t size_round = ROUND_UP(size_new, sector_size); |
2c092cad | 5182 | |
f36a9ecd | 5183 | if (posix_memalign(&mpb_new, sector_size, size_round) != 0) { |
e7b84f9d | 5184 | pr_err("could not allocate new mpb\n"); |
2c092cad DW |
5185 | return 0; |
5186 | } | |
de44e46f PB |
5187 | if (posix_memalign(&super->migr_rec_buf, sector_size, |
5188 | MIGR_REC_BUF_SECTORS*sector_size) != 0) { | |
1ade5cc1 | 5189 | pr_err("could not allocate migr_rec buffer\n"); |
8e59f3d8 AK |
5190 | free(super->buf); |
5191 | free(super); | |
ea944c8f | 5192 | free(mpb_new); |
8e59f3d8 AK |
5193 | return 0; |
5194 | } | |
2c092cad DW |
5195 | memcpy(mpb_new, mpb, size_old); |
5196 | free(mpb); | |
5197 | mpb = mpb_new; | |
949c47a0 | 5198 | super->anchor = mpb_new; |
2c092cad DW |
5199 | mpb->mpb_size = __cpu_to_le32(size_new); |
5200 | memset(mpb_new + size_old, 0, size_round - size_old); | |
bbab0940 | 5201 | super->len = size_round; |
2c092cad | 5202 | } |
bf5a934a | 5203 | super->current_vol = idx; |
3960e579 DW |
5204 | |
5205 | /* handle 'failed_disks' by either: | |
5206 | * a) create dummy disk entries in the table if this the first | |
5207 | * volume in the array. We add them here as this is the only | |
5208 | * opportunity to add them. add_to_super_imsm_volume() | |
5209 | * handles the non-failed disks and continues incrementing | |
5210 | * mpb->num_disks. | |
5211 | * b) validate that 'failed_disks' matches the current number | |
5212 | * of missing disks if the container is populated | |
d23fe947 | 5213 | */ |
3960e579 | 5214 | if (super->current_vol == 0) { |
d23fe947 | 5215 | mpb->num_disks = 0; |
3960e579 DW |
5216 | for (i = 0; i < info->failed_disks; i++) { |
5217 | struct imsm_disk *disk; | |
5218 | ||
5219 | mpb->num_disks++; | |
5220 | disk = __get_imsm_disk(mpb, i); | |
5221 | disk->status = CONFIGURED_DISK | FAILED_DISK; | |
5222 | disk->scsi_id = __cpu_to_le32(~(__u32)0); | |
5223 | snprintf((char *) disk->serial, MAX_RAID_SERIAL_LEN, | |
5224 | "missing:%d", i); | |
5225 | } | |
5226 | find_missing(super); | |
5227 | } else { | |
5228 | int missing = 0; | |
5229 | struct dl *d; | |
5230 | ||
5231 | for (d = super->missing; d; d = d->next) | |
5232 | missing++; | |
5233 | if (info->failed_disks > missing) { | |
e7b84f9d | 5234 | pr_err("unable to add 'missing' disk to container\n"); |
3960e579 DW |
5235 | return 0; |
5236 | } | |
5237 | } | |
5a038140 | 5238 | |
aa534678 DW |
5239 | if (!check_name(super, name, 0)) |
5240 | return 0; | |
503975b9 N |
5241 | dv = xmalloc(sizeof(*dv)); |
5242 | dev = xcalloc(1, sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1)); | |
c2c087e6 | 5243 | strncpy((char *) dev->volume, name, MAX_RAID_SERIAL_LEN); |
e03640bd | 5244 | array_blocks = calc_array_size(info->level, info->raid_disks, |
03bcbc65 | 5245 | info->layout, info->chunk_size, |
5551b113 | 5246 | size * 2); |
979d38be DW |
5247 | /* round array size down to closest MB */ |
5248 | array_blocks = (array_blocks >> SECT_PER_MB_SHIFT) << SECT_PER_MB_SHIFT; | |
5249 | ||
c2c087e6 DW |
5250 | dev->size_low = __cpu_to_le32((__u32) array_blocks); |
5251 | dev->size_high = __cpu_to_le32((__u32) (array_blocks >> 32)); | |
1a2487c2 | 5252 | dev->status = (DEV_READ_COALESCING | DEV_WRITE_COALESCING); |
c2c087e6 DW |
5253 | vol = &dev->vol; |
5254 | vol->migr_state = 0; | |
1484e727 | 5255 | set_migr_type(dev, MIGR_INIT); |
3960e579 | 5256 | vol->dirty = !info->state; |
f8f603f1 | 5257 | vol->curr_migr_unit = 0; |
238c0a71 | 5258 | map = get_imsm_map(dev, MAP_0); |
5551b113 CA |
5259 | set_pba_of_lba0(map, super->create_offset); |
5260 | set_blocks_per_member(map, info_to_blocks_per_member(info, size)); | |
ef6ffade | 5261 | map->blocks_per_strip = __cpu_to_le16(info_to_blocks_per_strip(info)); |
0556e1a2 | 5262 | map->failed_disk_num = ~0; |
bf4442ab | 5263 | if (info->level > 0) |
fffaf1ff N |
5264 | map->map_state = (info->state ? IMSM_T_STATE_NORMAL |
5265 | : IMSM_T_STATE_UNINITIALIZED); | |
bf4442ab AK |
5266 | else |
5267 | map->map_state = info->failed_disks ? IMSM_T_STATE_FAILED : | |
5268 | IMSM_T_STATE_NORMAL; | |
252d23c0 | 5269 | map->ddf = 1; |
ef6ffade DW |
5270 | |
5271 | if (info->level == 1 && info->raid_disks > 2) { | |
38950822 AW |
5272 | free(dev); |
5273 | free(dv); | |
7a862a02 | 5274 | pr_err("imsm does not support more than 2 disksin a raid1 volume\n"); |
ef6ffade DW |
5275 | return 0; |
5276 | } | |
81062a36 DW |
5277 | |
5278 | map->raid_level = info->level; | |
4d1313e9 | 5279 | if (info->level == 10) { |
c2c087e6 | 5280 | map->raid_level = 1; |
4d1313e9 | 5281 | map->num_domains = info->raid_disks / 2; |
81062a36 DW |
5282 | } else if (info->level == 1) |
5283 | map->num_domains = info->raid_disks; | |
5284 | else | |
ff596308 | 5285 | map->num_domains = 1; |
81062a36 | 5286 | |
5551b113 CA |
5287 | /* info->size is only int so use the 'size' parameter instead */ |
5288 | num_data_stripes = (size * 2) / info_to_blocks_per_strip(info); | |
5289 | num_data_stripes /= map->num_domains; | |
5290 | set_num_data_stripes(map, num_data_stripes); | |
ef6ffade | 5291 | |
c2c087e6 DW |
5292 | map->num_members = info->raid_disks; |
5293 | for (i = 0; i < map->num_members; i++) { | |
5294 | /* initialized in add_to_super */ | |
4eb26970 | 5295 | set_imsm_ord_tbl_ent(map, i, IMSM_ORD_REBUILD); |
c2c087e6 | 5296 | } |
949c47a0 | 5297 | mpb->num_raid_devs++; |
ba2de7ba DW |
5298 | |
5299 | dv->dev = dev; | |
5300 | dv->index = super->current_vol; | |
5301 | dv->next = super->devlist; | |
5302 | super->devlist = dv; | |
c2c087e6 | 5303 | |
4d1313e9 DW |
5304 | imsm_update_version_info(super); |
5305 | ||
c2c087e6 | 5306 | return 1; |
cdddbdbc DW |
5307 | } |
5308 | ||
bf5a934a DW |
5309 | static int init_super_imsm(struct supertype *st, mdu_array_info_t *info, |
5310 | unsigned long long size, char *name, | |
83cd1e97 N |
5311 | char *homehost, int *uuid, |
5312 | unsigned long long data_offset) | |
bf5a934a DW |
5313 | { |
5314 | /* This is primarily called by Create when creating a new array. | |
5315 | * We will then get add_to_super called for each component, and then | |
5316 | * write_init_super called to write it out to each device. | |
5317 | * For IMSM, Create can create on fresh devices or on a pre-existing | |
5318 | * array. | |
5319 | * To create on a pre-existing array a different method will be called. | |
5320 | * This one is just for fresh drives. | |
5321 | */ | |
5322 | struct intel_super *super; | |
5323 | struct imsm_super *mpb; | |
5324 | size_t mpb_size; | |
4d1313e9 | 5325 | char *version; |
bf5a934a | 5326 | |
83cd1e97 | 5327 | if (data_offset != INVALID_SECTORS) { |
ed503f89 | 5328 | pr_err("data-offset not supported by imsm\n"); |
83cd1e97 N |
5329 | return 0; |
5330 | } | |
5331 | ||
bf5a934a | 5332 | if (st->sb) |
83cd1e97 N |
5333 | return init_super_imsm_volume(st, info, size, name, homehost, uuid, |
5334 | data_offset); | |
e683ca88 DW |
5335 | |
5336 | if (info) | |
5337 | mpb_size = disks_to_mpb_size(info->nr_disks); | |
5338 | else | |
f36a9ecd | 5339 | mpb_size = MAX_SECTOR_SIZE; |
bf5a934a | 5340 | |
49133e57 | 5341 | super = alloc_super(); |
f36a9ecd PB |
5342 | if (super && |
5343 | posix_memalign(&super->buf, MAX_SECTOR_SIZE, mpb_size) != 0) { | |
8d67477f | 5344 | free_imsm(super); |
e683ca88 DW |
5345 | super = NULL; |
5346 | } | |
5347 | if (!super) { | |
1ade5cc1 | 5348 | pr_err("could not allocate superblock\n"); |
bf5a934a DW |
5349 | return 0; |
5350 | } | |
de44e46f PB |
5351 | if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE, |
5352 | MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE) != 0) { | |
1ade5cc1 | 5353 | pr_err("could not allocate migr_rec buffer\n"); |
8e59f3d8 | 5354 | free(super->buf); |
8d67477f | 5355 | free_imsm(super); |
8e59f3d8 AK |
5356 | return 0; |
5357 | } | |
e683ca88 | 5358 | memset(super->buf, 0, mpb_size); |
ef649044 | 5359 | mpb = super->buf; |
e683ca88 DW |
5360 | mpb->mpb_size = __cpu_to_le32(mpb_size); |
5361 | st->sb = super; | |
5362 | ||
5363 | if (info == NULL) { | |
5364 | /* zeroing superblock */ | |
5365 | return 0; | |
5366 | } | |
bf5a934a | 5367 | |
4d1313e9 DW |
5368 | mpb->attributes = MPB_ATTRIB_CHECKSUM_VERIFY; |
5369 | ||
5370 | version = (char *) mpb->sig; | |
5371 | strcpy(version, MPB_SIGNATURE); | |
5372 | version += strlen(MPB_SIGNATURE); | |
5373 | strcpy(version, MPB_VERSION_RAID0); | |
bf5a934a | 5374 | |
bf5a934a DW |
5375 | return 1; |
5376 | } | |
5377 | ||
0e600426 | 5378 | #ifndef MDASSEMBLE |
f20c3968 | 5379 | static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk, |
bf5a934a DW |
5380 | int fd, char *devname) |
5381 | { | |
5382 | struct intel_super *super = st->sb; | |
d23fe947 | 5383 | struct imsm_super *mpb = super->anchor; |
3960e579 | 5384 | struct imsm_disk *_disk; |
bf5a934a DW |
5385 | struct imsm_dev *dev; |
5386 | struct imsm_map *map; | |
3960e579 | 5387 | struct dl *dl, *df; |
4eb26970 | 5388 | int slot; |
bf5a934a | 5389 | |
949c47a0 | 5390 | dev = get_imsm_dev(super, super->current_vol); |
238c0a71 | 5391 | map = get_imsm_map(dev, MAP_0); |
bf5a934a | 5392 | |
208933a7 | 5393 | if (! (dk->state & (1<<MD_DISK_SYNC))) { |
e7b84f9d | 5394 | pr_err("%s: Cannot add spare devices to IMSM volume\n", |
208933a7 N |
5395 | devname); |
5396 | return 1; | |
5397 | } | |
5398 | ||
efb30e7f DW |
5399 | if (fd == -1) { |
5400 | /* we're doing autolayout so grab the pre-marked (in | |
5401 | * validate_geometry) raid_disk | |
5402 | */ | |
5403 | for (dl = super->disks; dl; dl = dl->next) | |
5404 | if (dl->raiddisk == dk->raid_disk) | |
5405 | break; | |
5406 | } else { | |
5407 | for (dl = super->disks; dl ; dl = dl->next) | |
5408 | if (dl->major == dk->major && | |
5409 | dl->minor == dk->minor) | |
5410 | break; | |
5411 | } | |
d23fe947 | 5412 | |
208933a7 | 5413 | if (!dl) { |
e7b84f9d | 5414 | pr_err("%s is not a member of the same container\n", devname); |
f20c3968 | 5415 | return 1; |
208933a7 | 5416 | } |
bf5a934a | 5417 | |
d23fe947 DW |
5418 | /* add a pristine spare to the metadata */ |
5419 | if (dl->index < 0) { | |
5420 | dl->index = super->anchor->num_disks; | |
5421 | super->anchor->num_disks++; | |
5422 | } | |
4eb26970 DW |
5423 | /* Check the device has not already been added */ |
5424 | slot = get_imsm_disk_slot(map, dl->index); | |
5425 | if (slot >= 0 && | |
238c0a71 | 5426 | (get_imsm_ord_tbl_ent(dev, slot, MAP_X) & IMSM_ORD_REBUILD) == 0) { |
e7b84f9d | 5427 | pr_err("%s has been included in this array twice\n", |
4eb26970 DW |
5428 | devname); |
5429 | return 1; | |
5430 | } | |
656b6b5a | 5431 | set_imsm_ord_tbl_ent(map, dk->raid_disk, dl->index); |
ee5aad5a | 5432 | dl->disk.status = CONFIGURED_DISK; |
d23fe947 | 5433 | |
3960e579 DW |
5434 | /* update size of 'missing' disks to be at least as large as the |
5435 | * largest acitve member (we only have dummy missing disks when | |
5436 | * creating the first volume) | |
5437 | */ | |
5438 | if (super->current_vol == 0) { | |
5439 | for (df = super->missing; df; df = df->next) { | |
5551b113 CA |
5440 | if (total_blocks(&dl->disk) > total_blocks(&df->disk)) |
5441 | set_total_blocks(&df->disk, total_blocks(&dl->disk)); | |
3960e579 DW |
5442 | _disk = __get_imsm_disk(mpb, df->index); |
5443 | *_disk = df->disk; | |
5444 | } | |
5445 | } | |
5446 | ||
5447 | /* refresh unset/failed slots to point to valid 'missing' entries */ | |
5448 | for (df = super->missing; df; df = df->next) | |
5449 | for (slot = 0; slot < mpb->num_disks; slot++) { | |
238c0a71 | 5450 | __u32 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X); |
3960e579 DW |
5451 | |
5452 | if ((ord & IMSM_ORD_REBUILD) == 0) | |
5453 | continue; | |
5454 | set_imsm_ord_tbl_ent(map, slot, df->index | IMSM_ORD_REBUILD); | |
1ace8403 | 5455 | if (is_gen_migration(dev)) { |
238c0a71 AK |
5456 | struct imsm_map *map2 = get_imsm_map(dev, |
5457 | MAP_1); | |
0a108d63 | 5458 | int slot2 = get_imsm_disk_slot(map2, df->index); |
089f9d79 | 5459 | if (slot2 < map2->num_members && slot2 >= 0) { |
1ace8403 | 5460 | __u32 ord2 = get_imsm_ord_tbl_ent(dev, |
238c0a71 AK |
5461 | slot2, |
5462 | MAP_1); | |
1ace8403 AK |
5463 | if ((unsigned)df->index == |
5464 | ord_to_idx(ord2)) | |
5465 | set_imsm_ord_tbl_ent(map2, | |
0a108d63 | 5466 | slot2, |
1ace8403 AK |
5467 | df->index | |
5468 | IMSM_ORD_REBUILD); | |
5469 | } | |
5470 | } | |
3960e579 DW |
5471 | dprintf("set slot:%d to missing disk:%d\n", slot, df->index); |
5472 | break; | |
5473 | } | |
5474 | ||
d23fe947 DW |
5475 | /* if we are creating the first raid device update the family number */ |
5476 | if (super->current_vol == 0) { | |
5477 | __u32 sum; | |
5478 | struct imsm_dev *_dev = __get_imsm_dev(mpb, 0); | |
d23fe947 | 5479 | |
3960e579 | 5480 | _disk = __get_imsm_disk(mpb, dl->index); |
791b666a | 5481 | if (!_dev || !_disk) { |
e7b84f9d | 5482 | pr_err("BUG mpb setup error\n"); |
791b666a AW |
5483 | return 1; |
5484 | } | |
d23fe947 DW |
5485 | *_dev = *dev; |
5486 | *_disk = dl->disk; | |
148acb7b DW |
5487 | sum = random32(); |
5488 | sum += __gen_imsm_checksum(mpb); | |
d23fe947 | 5489 | mpb->family_num = __cpu_to_le32(sum); |
148acb7b | 5490 | mpb->orig_family_num = mpb->family_num; |
d23fe947 | 5491 | } |
ca0748fa | 5492 | super->current_disk = dl; |
f20c3968 | 5493 | return 0; |
bf5a934a DW |
5494 | } |
5495 | ||
a8619d23 AK |
5496 | /* mark_spare() |
5497 | * Function marks disk as spare and restores disk serial | |
5498 | * in case it was previously marked as failed by takeover operation | |
5499 | * reruns: | |
5500 | * -1 : critical error | |
5501 | * 0 : disk is marked as spare but serial is not set | |
5502 | * 1 : success | |
5503 | */ | |
5504 | int mark_spare(struct dl *disk) | |
5505 | { | |
5506 | __u8 serial[MAX_RAID_SERIAL_LEN]; | |
5507 | int ret_val = -1; | |
5508 | ||
5509 | if (!disk) | |
5510 | return ret_val; | |
5511 | ||
5512 | ret_val = 0; | |
5513 | if (!imsm_read_serial(disk->fd, NULL, serial)) { | |
5514 | /* Restore disk serial number, because takeover marks disk | |
5515 | * as failed and adds to serial ':0' before it becomes | |
5516 | * a spare disk. | |
5517 | */ | |
5518 | serialcpy(disk->serial, serial); | |
5519 | serialcpy(disk->disk.serial, serial); | |
5520 | ret_val = 1; | |
5521 | } | |
5522 | disk->disk.status = SPARE_DISK; | |
5523 | disk->index = -1; | |
5524 | ||
5525 | return ret_val; | |
5526 | } | |
88654014 | 5527 | |
f20c3968 | 5528 | static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk, |
72ca9bcf N |
5529 | int fd, char *devname, |
5530 | unsigned long long data_offset) | |
cdddbdbc | 5531 | { |
c2c087e6 | 5532 | struct intel_super *super = st->sb; |
c2c087e6 DW |
5533 | struct dl *dd; |
5534 | unsigned long long size; | |
fa7bb6f8 | 5535 | unsigned int member_sector_size; |
f2f27e63 | 5536 | __u32 id; |
c2c087e6 DW |
5537 | int rv; |
5538 | struct stat stb; | |
5539 | ||
88654014 LM |
5540 | /* If we are on an RAID enabled platform check that the disk is |
5541 | * attached to the raid controller. | |
5542 | * We do not need to test disks attachment for container based additions, | |
5543 | * they shall be already tested when container was created/assembled. | |
88c32bb1 | 5544 | */ |
d424212e | 5545 | rv = find_intel_hba_capability(fd, super, devname); |
f2f5c343 | 5546 | /* no orom/efi or non-intel hba of the disk */ |
f0f5a016 LM |
5547 | if (rv != 0) { |
5548 | dprintf("capability: %p fd: %d ret: %d\n", | |
5549 | super->orom, fd, rv); | |
5550 | return 1; | |
88c32bb1 DW |
5551 | } |
5552 | ||
f20c3968 DW |
5553 | if (super->current_vol >= 0) |
5554 | return add_to_super_imsm_volume(st, dk, fd, devname); | |
bf5a934a | 5555 | |
c2c087e6 | 5556 | fstat(fd, &stb); |
503975b9 | 5557 | dd = xcalloc(sizeof(*dd), 1); |
c2c087e6 DW |
5558 | dd->major = major(stb.st_rdev); |
5559 | dd->minor = minor(stb.st_rdev); | |
503975b9 | 5560 | dd->devname = devname ? xstrdup(devname) : NULL; |
c2c087e6 | 5561 | dd->fd = fd; |
689c9bf3 | 5562 | dd->e = NULL; |
1a64be56 | 5563 | dd->action = DISK_ADD; |
c2c087e6 | 5564 | rv = imsm_read_serial(fd, devname, dd->serial); |
32ba9157 | 5565 | if (rv) { |
e7b84f9d | 5566 | pr_err("failed to retrieve scsi serial, aborting\n"); |
20bee0f8 PB |
5567 | if (dd->devname) |
5568 | free(dd->devname); | |
949c47a0 | 5569 | free(dd); |
0030e8d6 | 5570 | abort(); |
c2c087e6 | 5571 | } |
20bee0f8 PB |
5572 | if (super->hba && ((super->hba->type == SYS_DEV_NVME) || |
5573 | (super->hba->type == SYS_DEV_VMD))) { | |
5574 | int i; | |
5575 | char *devpath = diskfd_to_devpath(fd); | |
5576 | char controller_path[PATH_MAX]; | |
5577 | ||
5578 | if (!devpath) { | |
5579 | pr_err("failed to get devpath, aborting\n"); | |
5580 | if (dd->devname) | |
5581 | free(dd->devname); | |
5582 | free(dd); | |
5583 | return 1; | |
5584 | } | |
5585 | ||
5586 | snprintf(controller_path, PATH_MAX-1, "%s/device", devpath); | |
5587 | free(devpath); | |
5588 | ||
5589 | if (devpath_to_vendor(controller_path) == 0x8086) { | |
5590 | /* | |
5591 | * If Intel's NVMe drive has serial ended with | |
5592 | * "-A","-B","-1" or "-2" it means that this is "x8" | |
5593 | * device (double drive on single PCIe card). | |
5594 | * User should be warned about potential data loss. | |
5595 | */ | |
5596 | for (i = MAX_RAID_SERIAL_LEN-1; i > 0; i--) { | |
5597 | /* Skip empty character at the end */ | |
5598 | if (dd->serial[i] == 0) | |
5599 | continue; | |
5600 | ||
5601 | if (((dd->serial[i] == 'A') || | |
5602 | (dd->serial[i] == 'B') || | |
5603 | (dd->serial[i] == '1') || | |
5604 | (dd->serial[i] == '2')) && | |
5605 | (dd->serial[i-1] == '-')) | |
5606 | pr_err("\tThe action you are about to take may put your data at risk.\n" | |
5607 | "\tPlease note that x8 devices may consist of two separate x4 devices " | |
5608 | "located on a single PCIe port.\n" | |
5609 | "\tRAID 0 is the only supported configuration for this type of x8 device.\n"); | |
5610 | break; | |
5611 | } | |
32716c51 PB |
5612 | } else if (super->hba->type == SYS_DEV_VMD && super->orom && |
5613 | !imsm_orom_has_tpv_support(super->orom)) { | |
5614 | pr_err("\tPlatform configuration does not support non-Intel NVMe drives.\n" | |
5615 | "\tPlease refer to Intel(R) RSTe user guide.\n"); | |
5616 | free(dd->devname); | |
5617 | free(dd); | |
5618 | return 1; | |
20bee0f8 PB |
5619 | } |
5620 | } | |
c2c087e6 | 5621 | |
c2c087e6 | 5622 | get_dev_size(fd, NULL, &size); |
fa7bb6f8 PB |
5623 | get_dev_sector_size(fd, NULL, &member_sector_size); |
5624 | ||
5625 | if (super->sector_size == 0) { | |
5626 | /* this a first device, so sector_size is not set yet */ | |
5627 | super->sector_size = member_sector_size; | |
5628 | } else if (member_sector_size != super->sector_size) { | |
5629 | pr_err("Mixing between different sector size is forbidden, aborting...\n"); | |
5630 | if (dd->devname) | |
5631 | free(dd->devname); | |
5632 | free(dd); | |
5633 | return 1; | |
5634 | } | |
5635 | ||
71e5411e | 5636 | /* clear migr_rec when adding disk to container */ |
de44e46f PB |
5637 | memset(super->migr_rec_buf, 0, MIGR_REC_BUF_SECTORS*super->sector_size); |
5638 | if (lseek64(fd, size - MIGR_REC_SECTOR_POSITION*super->sector_size, | |
5639 | SEEK_SET) >= 0) { | |
71e5411e | 5640 | if (write(fd, super->migr_rec_buf, |
de44e46f PB |
5641 | MIGR_REC_BUF_SECTORS*super->sector_size) != |
5642 | MIGR_REC_BUF_SECTORS*super->sector_size) | |
71e5411e PB |
5643 | perror("Write migr_rec failed"); |
5644 | } | |
5645 | ||
c2c087e6 | 5646 | size /= 512; |
1f24f035 | 5647 | serialcpy(dd->disk.serial, dd->serial); |
5551b113 CA |
5648 | set_total_blocks(&dd->disk, size); |
5649 | if (__le32_to_cpu(dd->disk.total_blocks_hi) > 0) { | |
5650 | struct imsm_super *mpb = super->anchor; | |
5651 | mpb->attributes |= MPB_ATTRIB_2TB_DISK; | |
5652 | } | |
a8619d23 | 5653 | mark_spare(dd); |
c2c087e6 | 5654 | if (sysfs_disk_to_scsi_id(fd, &id) == 0) |
b9f594fe | 5655 | dd->disk.scsi_id = __cpu_to_le32(id); |
c2c087e6 | 5656 | else |
b9f594fe | 5657 | dd->disk.scsi_id = __cpu_to_le32(0); |
43dad3d6 DW |
5658 | |
5659 | if (st->update_tail) { | |
1a64be56 LM |
5660 | dd->next = super->disk_mgmt_list; |
5661 | super->disk_mgmt_list = dd; | |
43dad3d6 DW |
5662 | } else { |
5663 | dd->next = super->disks; | |
5664 | super->disks = dd; | |
ceaf0ee1 | 5665 | super->updates_pending++; |
43dad3d6 | 5666 | } |
f20c3968 DW |
5667 | |
5668 | return 0; | |
cdddbdbc DW |
5669 | } |
5670 | ||
1a64be56 LM |
5671 | static int remove_from_super_imsm(struct supertype *st, mdu_disk_info_t *dk) |
5672 | { | |
5673 | struct intel_super *super = st->sb; | |
5674 | struct dl *dd; | |
5675 | ||
5676 | /* remove from super works only in mdmon - for communication | |
5677 | * manager - monitor. Check if communication memory buffer | |
5678 | * is prepared. | |
5679 | */ | |
5680 | if (!st->update_tail) { | |
1ade5cc1 | 5681 | pr_err("shall be used in mdmon context only\n"); |
1a64be56 LM |
5682 | return 1; |
5683 | } | |
503975b9 | 5684 | dd = xcalloc(1, sizeof(*dd)); |
1a64be56 LM |
5685 | dd->major = dk->major; |
5686 | dd->minor = dk->minor; | |
1a64be56 | 5687 | dd->fd = -1; |
a8619d23 | 5688 | mark_spare(dd); |
1a64be56 LM |
5689 | dd->action = DISK_REMOVE; |
5690 | ||
5691 | dd->next = super->disk_mgmt_list; | |
5692 | super->disk_mgmt_list = dd; | |
5693 | ||
1a64be56 LM |
5694 | return 0; |
5695 | } | |
5696 | ||
f796af5d DW |
5697 | static int store_imsm_mpb(int fd, struct imsm_super *mpb); |
5698 | ||
5699 | static union { | |
f36a9ecd | 5700 | char buf[MAX_SECTOR_SIZE]; |
f796af5d | 5701 | struct imsm_super anchor; |
f36a9ecd | 5702 | } spare_record __attribute__ ((aligned(MAX_SECTOR_SIZE))); |
c2c087e6 | 5703 | |
d23fe947 DW |
5704 | /* spare records have their own family number and do not have any defined raid |
5705 | * devices | |
5706 | */ | |
5707 | static int write_super_imsm_spares(struct intel_super *super, int doclose) | |
5708 | { | |
d23fe947 | 5709 | struct imsm_super *mpb = super->anchor; |
f796af5d | 5710 | struct imsm_super *spare = &spare_record.anchor; |
d23fe947 DW |
5711 | __u32 sum; |
5712 | struct dl *d; | |
5713 | ||
68641cdb JS |
5714 | spare->mpb_size = __cpu_to_le32(sizeof(struct imsm_super)); |
5715 | spare->generation_num = __cpu_to_le32(1UL); | |
f796af5d | 5716 | spare->attributes = MPB_ATTRIB_CHECKSUM_VERIFY; |
68641cdb JS |
5717 | spare->num_disks = 1; |
5718 | spare->num_raid_devs = 0; | |
5719 | spare->cache_size = mpb->cache_size; | |
5720 | spare->pwr_cycle_count = __cpu_to_le32(1); | |
f796af5d DW |
5721 | |
5722 | snprintf((char *) spare->sig, MAX_SIGNATURE_LENGTH, | |
5723 | MPB_SIGNATURE MPB_VERSION_RAID0); | |
d23fe947 DW |
5724 | |
5725 | for (d = super->disks; d; d = d->next) { | |
8796fdc4 | 5726 | if (d->index != -1) |
d23fe947 DW |
5727 | continue; |
5728 | ||
f796af5d | 5729 | spare->disk[0] = d->disk; |
027c374f CA |
5730 | if (__le32_to_cpu(d->disk.total_blocks_hi) > 0) |
5731 | spare->attributes |= MPB_ATTRIB_2TB_DISK; | |
5732 | ||
f36a9ecd PB |
5733 | if (super->sector_size == 4096) |
5734 | convert_to_4k_imsm_disk(&spare->disk[0]); | |
5735 | ||
f796af5d DW |
5736 | sum = __gen_imsm_checksum(spare); |
5737 | spare->family_num = __cpu_to_le32(sum); | |
5738 | spare->orig_family_num = 0; | |
5739 | sum = __gen_imsm_checksum(spare); | |
5740 | spare->check_sum = __cpu_to_le32(sum); | |
d23fe947 | 5741 | |
f796af5d | 5742 | if (store_imsm_mpb(d->fd, spare)) { |
1ade5cc1 N |
5743 | pr_err("failed for device %d:%d %s\n", |
5744 | d->major, d->minor, strerror(errno)); | |
e74255d9 | 5745 | return 1; |
d23fe947 DW |
5746 | } |
5747 | if (doclose) { | |
5748 | close(d->fd); | |
5749 | d->fd = -1; | |
5750 | } | |
5751 | } | |
5752 | ||
e74255d9 | 5753 | return 0; |
d23fe947 DW |
5754 | } |
5755 | ||
36988a3d | 5756 | static int write_super_imsm(struct supertype *st, int doclose) |
cdddbdbc | 5757 | { |
36988a3d | 5758 | struct intel_super *super = st->sb; |
f36a9ecd | 5759 | unsigned int sector_size = super->sector_size; |
949c47a0 | 5760 | struct imsm_super *mpb = super->anchor; |
c2c087e6 DW |
5761 | struct dl *d; |
5762 | __u32 generation; | |
5763 | __u32 sum; | |
d23fe947 | 5764 | int spares = 0; |
949c47a0 | 5765 | int i; |
a48ac0a8 | 5766 | __u32 mpb_size = sizeof(struct imsm_super) - sizeof(struct imsm_disk); |
36988a3d | 5767 | int num_disks = 0; |
146c6260 | 5768 | int clear_migration_record = 1; |
bbab0940 | 5769 | __u32 bbm_log_size; |
cdddbdbc | 5770 | |
c2c087e6 DW |
5771 | /* 'generation' is incremented everytime the metadata is written */ |
5772 | generation = __le32_to_cpu(mpb->generation_num); | |
5773 | generation++; | |
5774 | mpb->generation_num = __cpu_to_le32(generation); | |
5775 | ||
148acb7b DW |
5776 | /* fix up cases where previous mdadm releases failed to set |
5777 | * orig_family_num | |
5778 | */ | |
5779 | if (mpb->orig_family_num == 0) | |
5780 | mpb->orig_family_num = mpb->family_num; | |
5781 | ||
d23fe947 | 5782 | for (d = super->disks; d; d = d->next) { |
8796fdc4 | 5783 | if (d->index == -1) |
d23fe947 | 5784 | spares++; |
36988a3d | 5785 | else { |
d23fe947 | 5786 | mpb->disk[d->index] = d->disk; |
36988a3d AK |
5787 | num_disks++; |
5788 | } | |
d23fe947 | 5789 | } |
36988a3d | 5790 | for (d = super->missing; d; d = d->next) { |
47ee5a45 | 5791 | mpb->disk[d->index] = d->disk; |
36988a3d AK |
5792 | num_disks++; |
5793 | } | |
5794 | mpb->num_disks = num_disks; | |
5795 | mpb_size += sizeof(struct imsm_disk) * mpb->num_disks; | |
b9f594fe | 5796 | |
949c47a0 DW |
5797 | for (i = 0; i < mpb->num_raid_devs; i++) { |
5798 | struct imsm_dev *dev = __get_imsm_dev(mpb, i); | |
36988a3d AK |
5799 | struct imsm_dev *dev2 = get_imsm_dev(super, i); |
5800 | if (dev && dev2) { | |
5801 | imsm_copy_dev(dev, dev2); | |
5802 | mpb_size += sizeof_imsm_dev(dev, 0); | |
5803 | } | |
146c6260 AK |
5804 | if (is_gen_migration(dev2)) |
5805 | clear_migration_record = 0; | |
949c47a0 | 5806 | } |
bbab0940 TM |
5807 | |
5808 | bbm_log_size = get_imsm_bbm_log_size(super->bbm_log); | |
5809 | ||
5810 | if (bbm_log_size) { | |
5811 | memcpy((void *)mpb + mpb_size, super->bbm_log, bbm_log_size); | |
5812 | mpb->attributes |= MPB_ATTRIB_BBM; | |
5813 | } else | |
5814 | mpb->attributes &= ~MPB_ATTRIB_BBM; | |
5815 | ||
5816 | super->anchor->bbm_log_size = __cpu_to_le32(bbm_log_size); | |
5817 | mpb_size += bbm_log_size; | |
a48ac0a8 | 5818 | mpb->mpb_size = __cpu_to_le32(mpb_size); |
949c47a0 | 5819 | |
bbab0940 TM |
5820 | #ifdef DEBUG |
5821 | assert(super->len == 0 || mpb_size <= super->len); | |
5822 | #endif | |
5823 | ||
c2c087e6 | 5824 | /* recalculate checksum */ |
949c47a0 | 5825 | sum = __gen_imsm_checksum(mpb); |
c2c087e6 DW |
5826 | mpb->check_sum = __cpu_to_le32(sum); |
5827 | ||
51d83f5d AK |
5828 | if (super->clean_migration_record_by_mdmon) { |
5829 | clear_migration_record = 1; | |
5830 | super->clean_migration_record_by_mdmon = 0; | |
5831 | } | |
146c6260 | 5832 | if (clear_migration_record) |
de44e46f PB |
5833 | memset(super->migr_rec_buf, 0, |
5834 | MIGR_REC_BUF_SECTORS*sector_size); | |
146c6260 | 5835 | |
f36a9ecd PB |
5836 | if (sector_size == 4096) |
5837 | convert_to_4k(super); | |
5838 | ||
d23fe947 | 5839 | /* write the mpb for disks that compose raid devices */ |
c2c087e6 | 5840 | for (d = super->disks; d ; d = d->next) { |
86c54047 | 5841 | if (d->index < 0 || is_failed(&d->disk)) |
d23fe947 | 5842 | continue; |
30602f53 | 5843 | |
146c6260 AK |
5844 | if (clear_migration_record) { |
5845 | unsigned long long dsize; | |
5846 | ||
5847 | get_dev_size(d->fd, NULL, &dsize); | |
de44e46f PB |
5848 | if (lseek64(d->fd, dsize - sector_size, |
5849 | SEEK_SET) >= 0) { | |
17a4eaf9 | 5850 | if (write(d->fd, super->migr_rec_buf, |
de44e46f PB |
5851 | MIGR_REC_BUF_SECTORS*sector_size) != |
5852 | MIGR_REC_BUF_SECTORS*sector_size) | |
9e2d750d | 5853 | perror("Write migr_rec failed"); |
146c6260 AK |
5854 | } |
5855 | } | |
51d83f5d AK |
5856 | |
5857 | if (store_imsm_mpb(d->fd, mpb)) | |
5858 | fprintf(stderr, | |
1ade5cc1 N |
5859 | "failed for device %d:%d (fd: %d)%s\n", |
5860 | d->major, d->minor, | |
51d83f5d AK |
5861 | d->fd, strerror(errno)); |
5862 | ||
c2c087e6 DW |
5863 | if (doclose) { |
5864 | close(d->fd); | |
5865 | d->fd = -1; | |
5866 | } | |
5867 | } | |
5868 | ||
d23fe947 DW |
5869 | if (spares) |
5870 | return write_super_imsm_spares(super, doclose); | |
5871 | ||
e74255d9 | 5872 | return 0; |
c2c087e6 DW |
5873 | } |
5874 | ||
9b1fb677 | 5875 | static int create_array(struct supertype *st, int dev_idx) |
43dad3d6 DW |
5876 | { |
5877 | size_t len; | |
5878 | struct imsm_update_create_array *u; | |
5879 | struct intel_super *super = st->sb; | |
9b1fb677 | 5880 | struct imsm_dev *dev = get_imsm_dev(super, dev_idx); |
238c0a71 | 5881 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
54c2c1ea DW |
5882 | struct disk_info *inf; |
5883 | struct imsm_disk *disk; | |
5884 | int i; | |
43dad3d6 | 5885 | |
54c2c1ea DW |
5886 | len = sizeof(*u) - sizeof(*dev) + sizeof_imsm_dev(dev, 0) + |
5887 | sizeof(*inf) * map->num_members; | |
503975b9 | 5888 | u = xmalloc(len); |
43dad3d6 | 5889 | u->type = update_create_array; |
9b1fb677 | 5890 | u->dev_idx = dev_idx; |
43dad3d6 | 5891 | imsm_copy_dev(&u->dev, dev); |
54c2c1ea DW |
5892 | inf = get_disk_info(u); |
5893 | for (i = 0; i < map->num_members; i++) { | |
238c0a71 | 5894 | int idx = get_imsm_disk_idx(dev, i, MAP_X); |
9b1fb677 | 5895 | |
54c2c1ea | 5896 | disk = get_imsm_disk(super, idx); |
1ca5c8e0 N |
5897 | if (!disk) |
5898 | disk = get_imsm_missing(super, idx); | |
54c2c1ea DW |
5899 | serialcpy(inf[i].serial, disk->serial); |
5900 | } | |
43dad3d6 DW |
5901 | append_metadata_update(st, u, len); |
5902 | ||
5903 | return 0; | |
5904 | } | |
5905 | ||
1a64be56 | 5906 | static int mgmt_disk(struct supertype *st) |
43dad3d6 DW |
5907 | { |
5908 | struct intel_super *super = st->sb; | |
5909 | size_t len; | |
1a64be56 | 5910 | struct imsm_update_add_remove_disk *u; |
43dad3d6 | 5911 | |
1a64be56 | 5912 | if (!super->disk_mgmt_list) |
43dad3d6 DW |
5913 | return 0; |
5914 | ||
5915 | len = sizeof(*u); | |
503975b9 | 5916 | u = xmalloc(len); |
1a64be56 | 5917 | u->type = update_add_remove_disk; |
43dad3d6 DW |
5918 | append_metadata_update(st, u, len); |
5919 | ||
5920 | return 0; | |
5921 | } | |
5922 | ||
c2c087e6 DW |
5923 | static int write_init_super_imsm(struct supertype *st) |
5924 | { | |
9b1fb677 DW |
5925 | struct intel_super *super = st->sb; |
5926 | int current_vol = super->current_vol; | |
5927 | ||
5928 | /* we are done with current_vol reset it to point st at the container */ | |
5929 | super->current_vol = -1; | |
5930 | ||
8273f55e | 5931 | if (st->update_tail) { |
43dad3d6 DW |
5932 | /* queue the recently created array / added disk |
5933 | * as a metadata update */ | |
43dad3d6 | 5934 | int rv; |
8273f55e | 5935 | |
43dad3d6 | 5936 | /* determine if we are creating a volume or adding a disk */ |
9b1fb677 | 5937 | if (current_vol < 0) { |
1a64be56 LM |
5938 | /* in the mgmt (add/remove) disk case we are running |
5939 | * in mdmon context, so don't close fd's | |
43dad3d6 | 5940 | */ |
1a64be56 | 5941 | return mgmt_disk(st); |
43dad3d6 | 5942 | } else |
9b1fb677 | 5943 | rv = create_array(st, current_vol); |
8273f55e | 5944 | |
43dad3d6 | 5945 | return rv; |
d682f344 N |
5946 | } else { |
5947 | struct dl *d; | |
5948 | for (d = super->disks; d; d = d->next) | |
ba728be7 | 5949 | Kill(d->devname, NULL, 0, -1, 1); |
36988a3d | 5950 | return write_super_imsm(st, 1); |
d682f344 | 5951 | } |
cdddbdbc | 5952 | } |
0e600426 | 5953 | #endif |
cdddbdbc | 5954 | |
e683ca88 | 5955 | static int store_super_imsm(struct supertype *st, int fd) |
cdddbdbc | 5956 | { |
e683ca88 DW |
5957 | struct intel_super *super = st->sb; |
5958 | struct imsm_super *mpb = super ? super->anchor : NULL; | |
551c80c1 | 5959 | |
e683ca88 | 5960 | if (!mpb) |
ad97895e DW |
5961 | return 1; |
5962 | ||
1799c9e8 | 5963 | #ifndef MDASSEMBLE |
f36a9ecd PB |
5964 | if (super->sector_size == 4096) |
5965 | convert_to_4k(super); | |
e683ca88 | 5966 | return store_imsm_mpb(fd, mpb); |
1799c9e8 N |
5967 | #else |
5968 | return 1; | |
5969 | #endif | |
cdddbdbc DW |
5970 | } |
5971 | ||
0e600426 | 5972 | #ifndef MDASSEMBLE |
cdddbdbc DW |
5973 | static int validate_geometry_imsm_container(struct supertype *st, int level, |
5974 | int layout, int raiddisks, int chunk, | |
af4348dd N |
5975 | unsigned long long size, |
5976 | unsigned long long data_offset, | |
5977 | char *dev, | |
2c514b71 NB |
5978 | unsigned long long *freesize, |
5979 | int verbose) | |
cdddbdbc | 5980 | { |
c2c087e6 DW |
5981 | int fd; |
5982 | unsigned long long ldsize; | |
594dc1b8 | 5983 | struct intel_super *super; |
f2f5c343 | 5984 | int rv = 0; |
cdddbdbc | 5985 | |
c2c087e6 DW |
5986 | if (level != LEVEL_CONTAINER) |
5987 | return 0; | |
5988 | if (!dev) | |
5989 | return 1; | |
5990 | ||
5991 | fd = open(dev, O_RDONLY|O_EXCL, 0); | |
5992 | if (fd < 0) { | |
ba728be7 | 5993 | if (verbose > 0) |
e7b84f9d | 5994 | pr_err("imsm: Cannot open %s: %s\n", |
2c514b71 | 5995 | dev, strerror(errno)); |
c2c087e6 DW |
5996 | return 0; |
5997 | } | |
5998 | if (!get_dev_size(fd, dev, &ldsize)) { | |
5999 | close(fd); | |
6000 | return 0; | |
6001 | } | |
f2f5c343 LM |
6002 | |
6003 | /* capabilities retrieve could be possible | |
6004 | * note that there is no fd for the disks in array. | |
6005 | */ | |
6006 | super = alloc_super(); | |
8d67477f TM |
6007 | if (!super) { |
6008 | close(fd); | |
6009 | return 0; | |
6010 | } | |
fa7bb6f8 PB |
6011 | if (!get_dev_sector_size(fd, NULL, &super->sector_size)) { |
6012 | close(fd); | |
6013 | free_imsm(super); | |
6014 | return 0; | |
6015 | } | |
6016 | ||
ba728be7 | 6017 | rv = find_intel_hba_capability(fd, super, verbose > 0 ? dev : NULL); |
f2f5c343 LM |
6018 | if (rv != 0) { |
6019 | #if DEBUG | |
6020 | char str[256]; | |
6021 | fd2devname(fd, str); | |
1ade5cc1 | 6022 | dprintf("fd: %d %s orom: %p rv: %d raiddisk: %d\n", |
f2f5c343 LM |
6023 | fd, str, super->orom, rv, raiddisks); |
6024 | #endif | |
6025 | /* no orom/efi or non-intel hba of the disk */ | |
6026 | close(fd); | |
6027 | free_imsm(super); | |
6028 | return 0; | |
6029 | } | |
c2c087e6 | 6030 | close(fd); |
9126b9a8 CA |
6031 | if (super->orom) { |
6032 | if (raiddisks > super->orom->tds) { | |
6033 | if (verbose) | |
7a862a02 | 6034 | pr_err("%d exceeds maximum number of platform supported disks: %d\n", |
9126b9a8 CA |
6035 | raiddisks, super->orom->tds); |
6036 | free_imsm(super); | |
6037 | return 0; | |
6038 | } | |
6039 | if ((super->orom->attr & IMSM_OROM_ATTR_2TB_DISK) == 0 && | |
6040 | (ldsize >> 9) >> 32 > 0) { | |
6041 | if (verbose) | |
e7b84f9d | 6042 | pr_err("%s exceeds maximum platform supported size\n", dev); |
9126b9a8 CA |
6043 | free_imsm(super); |
6044 | return 0; | |
6045 | } | |
f2f5c343 | 6046 | } |
c2c087e6 | 6047 | |
af4348dd | 6048 | *freesize = avail_size_imsm(st, ldsize >> 9, data_offset); |
f2f5c343 | 6049 | free_imsm(super); |
c2c087e6 DW |
6050 | |
6051 | return 1; | |
cdddbdbc DW |
6052 | } |
6053 | ||
0dcecb2e DW |
6054 | static unsigned long long find_size(struct extent *e, int *idx, int num_extents) |
6055 | { | |
6056 | const unsigned long long base_start = e[*idx].start; | |
6057 | unsigned long long end = base_start + e[*idx].size; | |
6058 | int i; | |
6059 | ||
6060 | if (base_start == end) | |
6061 | return 0; | |
6062 | ||
6063 | *idx = *idx + 1; | |
6064 | for (i = *idx; i < num_extents; i++) { | |
6065 | /* extend overlapping extents */ | |
6066 | if (e[i].start >= base_start && | |
6067 | e[i].start <= end) { | |
6068 | if (e[i].size == 0) | |
6069 | return 0; | |
6070 | if (e[i].start + e[i].size > end) | |
6071 | end = e[i].start + e[i].size; | |
6072 | } else if (e[i].start > end) { | |
6073 | *idx = i; | |
6074 | break; | |
6075 | } | |
6076 | } | |
6077 | ||
6078 | return end - base_start; | |
6079 | } | |
6080 | ||
6081 | static unsigned long long merge_extents(struct intel_super *super, int sum_extents) | |
6082 | { | |
6083 | /* build a composite disk with all known extents and generate a new | |
6084 | * 'maxsize' given the "all disks in an array must share a common start | |
6085 | * offset" constraint | |
6086 | */ | |
503975b9 | 6087 | struct extent *e = xcalloc(sum_extents, sizeof(*e)); |
0dcecb2e DW |
6088 | struct dl *dl; |
6089 | int i, j; | |
6090 | int start_extent; | |
6091 | unsigned long long pos; | |
b9d77223 | 6092 | unsigned long long start = 0; |
0dcecb2e DW |
6093 | unsigned long long maxsize; |
6094 | unsigned long reserve; | |
6095 | ||
0dcecb2e DW |
6096 | /* coalesce and sort all extents. also, check to see if we need to |
6097 | * reserve space between member arrays | |
6098 | */ | |
6099 | j = 0; | |
6100 | for (dl = super->disks; dl; dl = dl->next) { | |
6101 | if (!dl->e) | |
6102 | continue; | |
6103 | for (i = 0; i < dl->extent_cnt; i++) | |
6104 | e[j++] = dl->e[i]; | |
6105 | } | |
6106 | qsort(e, sum_extents, sizeof(*e), cmp_extent); | |
6107 | ||
6108 | /* merge extents */ | |
6109 | i = 0; | |
6110 | j = 0; | |
6111 | while (i < sum_extents) { | |
6112 | e[j].start = e[i].start; | |
6113 | e[j].size = find_size(e, &i, sum_extents); | |
6114 | j++; | |
6115 | if (e[j-1].size == 0) | |
6116 | break; | |
6117 | } | |
6118 | ||
6119 | pos = 0; | |
6120 | maxsize = 0; | |
6121 | start_extent = 0; | |
6122 | i = 0; | |
6123 | do { | |
6124 | unsigned long long esize; | |
6125 | ||
6126 | esize = e[i].start - pos; | |
6127 | if (esize >= maxsize) { | |
6128 | maxsize = esize; | |
6129 | start = pos; | |
6130 | start_extent = i; | |
6131 | } | |
6132 | pos = e[i].start + e[i].size; | |
6133 | i++; | |
6134 | } while (e[i-1].size); | |
6135 | free(e); | |
6136 | ||
a7dd165b DW |
6137 | if (maxsize == 0) |
6138 | return 0; | |
6139 | ||
6140 | /* FIXME assumes volume at offset 0 is the first volume in a | |
6141 | * container | |
6142 | */ | |
0dcecb2e DW |
6143 | if (start_extent > 0) |
6144 | reserve = IMSM_RESERVED_SECTORS; /* gap between raid regions */ | |
6145 | else | |
6146 | reserve = 0; | |
6147 | ||
6148 | if (maxsize < reserve) | |
a7dd165b | 6149 | return 0; |
0dcecb2e | 6150 | |
5551b113 | 6151 | super->create_offset = ~((unsigned long long) 0); |
0dcecb2e | 6152 | if (start + reserve > super->create_offset) |
a7dd165b | 6153 | return 0; /* start overflows create_offset */ |
0dcecb2e DW |
6154 | super->create_offset = start + reserve; |
6155 | ||
6156 | return maxsize - reserve; | |
6157 | } | |
6158 | ||
88c32bb1 DW |
6159 | static int is_raid_level_supported(const struct imsm_orom *orom, int level, int raiddisks) |
6160 | { | |
6161 | if (level < 0 || level == 6 || level == 4) | |
6162 | return 0; | |
6163 | ||
6164 | /* if we have an orom prevent invalid raid levels */ | |
6165 | if (orom) | |
6166 | switch (level) { | |
6167 | case 0: return imsm_orom_has_raid0(orom); | |
6168 | case 1: | |
6169 | if (raiddisks > 2) | |
6170 | return imsm_orom_has_raid1e(orom); | |
1c556e92 DW |
6171 | return imsm_orom_has_raid1(orom) && raiddisks == 2; |
6172 | case 10: return imsm_orom_has_raid10(orom) && raiddisks == 4; | |
6173 | case 5: return imsm_orom_has_raid5(orom) && raiddisks > 2; | |
88c32bb1 DW |
6174 | } |
6175 | else | |
6176 | return 1; /* not on an Intel RAID platform so anything goes */ | |
6177 | ||
6178 | return 0; | |
6179 | } | |
6180 | ||
ca9de185 LM |
6181 | static int |
6182 | active_arrays_by_format(char *name, char* hba, struct md_list **devlist, | |
6183 | int dpa, int verbose) | |
6184 | { | |
6185 | struct mdstat_ent *mdstat = mdstat_read(0, 0); | |
594dc1b8 | 6186 | struct mdstat_ent *memb; |
ca9de185 LM |
6187 | int count = 0; |
6188 | int num = 0; | |
594dc1b8 | 6189 | struct md_list *dv; |
ca9de185 LM |
6190 | int found; |
6191 | ||
6192 | for (memb = mdstat ; memb ; memb = memb->next) { | |
6193 | if (memb->metadata_version && | |
6194 | (strncmp(memb->metadata_version, "external:", 9) == 0) && | |
6195 | (strcmp(&memb->metadata_version[9], name) == 0) && | |
6196 | !is_subarray(memb->metadata_version+9) && | |
6197 | memb->members) { | |
6198 | struct dev_member *dev = memb->members; | |
6199 | int fd = -1; | |
6200 | while(dev && (fd < 0)) { | |
503975b9 N |
6201 | char *path = xmalloc(strlen(dev->name) + strlen("/dev/") + 1); |
6202 | num = sprintf(path, "%s%s", "/dev/", dev->name); | |
6203 | if (num > 0) | |
6204 | fd = open(path, O_RDONLY, 0); | |
089f9d79 | 6205 | if (num <= 0 || fd < 0) { |
676e87a8 | 6206 | pr_vrb("Cannot open %s: %s\n", |
503975b9 | 6207 | dev->name, strerror(errno)); |
ca9de185 | 6208 | } |
503975b9 | 6209 | free(path); |
ca9de185 LM |
6210 | dev = dev->next; |
6211 | } | |
6212 | found = 0; | |
089f9d79 | 6213 | if (fd >= 0 && disk_attached_to_hba(fd, hba)) { |
ca9de185 LM |
6214 | struct mdstat_ent *vol; |
6215 | for (vol = mdstat ; vol ; vol = vol->next) { | |
089f9d79 | 6216 | if (vol->active > 0 && |
ca9de185 | 6217 | vol->metadata_version && |
9581efb1 | 6218 | is_container_member(vol, memb->devnm)) { |
ca9de185 LM |
6219 | found++; |
6220 | count++; | |
6221 | } | |
6222 | } | |
6223 | if (*devlist && (found < dpa)) { | |
503975b9 | 6224 | dv = xcalloc(1, sizeof(*dv)); |
9581efb1 N |
6225 | dv->devname = xmalloc(strlen(memb->devnm) + strlen("/dev/") + 1); |
6226 | sprintf(dv->devname, "%s%s", "/dev/", memb->devnm); | |
503975b9 N |
6227 | dv->found = found; |
6228 | dv->used = 0; | |
6229 | dv->next = *devlist; | |
6230 | *devlist = dv; | |
ca9de185 LM |
6231 | } |
6232 | } | |
6233 | if (fd >= 0) | |
6234 | close(fd); | |
6235 | } | |
6236 | } | |
6237 | free_mdstat(mdstat); | |
6238 | return count; | |
6239 | } | |
6240 | ||
6241 | #ifdef DEBUG_LOOP | |
6242 | static struct md_list* | |
6243 | get_loop_devices(void) | |
6244 | { | |
6245 | int i; | |
6246 | struct md_list *devlist = NULL; | |
594dc1b8 | 6247 | struct md_list *dv; |
ca9de185 LM |
6248 | |
6249 | for(i = 0; i < 12; i++) { | |
503975b9 N |
6250 | dv = xcalloc(1, sizeof(*dv)); |
6251 | dv->devname = xmalloc(40); | |
ca9de185 LM |
6252 | sprintf(dv->devname, "/dev/loop%d", i); |
6253 | dv->next = devlist; | |
6254 | devlist = dv; | |
6255 | } | |
6256 | return devlist; | |
6257 | } | |
6258 | #endif | |
6259 | ||
6260 | static struct md_list* | |
6261 | get_devices(const char *hba_path) | |
6262 | { | |
6263 | struct md_list *devlist = NULL; | |
594dc1b8 | 6264 | struct md_list *dv; |
ca9de185 LM |
6265 | struct dirent *ent; |
6266 | DIR *dir; | |
6267 | int err = 0; | |
6268 | ||
6269 | #if DEBUG_LOOP | |
6270 | devlist = get_loop_devices(); | |
6271 | return devlist; | |
6272 | #endif | |
6273 | /* scroll through /sys/dev/block looking for devices attached to | |
6274 | * this hba | |
6275 | */ | |
6276 | dir = opendir("/sys/dev/block"); | |
6277 | for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) { | |
6278 | int fd; | |
6279 | char buf[1024]; | |
6280 | int major, minor; | |
6281 | char *path = NULL; | |
6282 | if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2) | |
6283 | continue; | |
6284 | path = devt_to_devpath(makedev(major, minor)); | |
6285 | if (!path) | |
6286 | continue; | |
6287 | if (!path_attached_to_hba(path, hba_path)) { | |
6288 | free(path); | |
6289 | path = NULL; | |
6290 | continue; | |
6291 | } | |
6292 | free(path); | |
6293 | path = NULL; | |
6294 | fd = dev_open(ent->d_name, O_RDONLY); | |
6295 | if (fd >= 0) { | |
6296 | fd2devname(fd, buf); | |
6297 | close(fd); | |
6298 | } else { | |
e7b84f9d | 6299 | pr_err("cannot open device: %s\n", |
ca9de185 LM |
6300 | ent->d_name); |
6301 | continue; | |
6302 | } | |
6303 | ||
503975b9 N |
6304 | dv = xcalloc(1, sizeof(*dv)); |
6305 | dv->devname = xstrdup(buf); | |
ca9de185 LM |
6306 | dv->next = devlist; |
6307 | devlist = dv; | |
6308 | } | |
6309 | if (err) { | |
6310 | while(devlist) { | |
6311 | dv = devlist; | |
6312 | devlist = devlist->next; | |
6313 | free(dv->devname); | |
6314 | free(dv); | |
6315 | } | |
6316 | } | |
562aa102 | 6317 | closedir(dir); |
ca9de185 LM |
6318 | return devlist; |
6319 | } | |
6320 | ||
6321 | static int | |
6322 | count_volumes_list(struct md_list *devlist, char *homehost, | |
6323 | int verbose, int *found) | |
6324 | { | |
6325 | struct md_list *tmpdev; | |
6326 | int count = 0; | |
594dc1b8 | 6327 | struct supertype *st; |
ca9de185 LM |
6328 | |
6329 | /* first walk the list of devices to find a consistent set | |
6330 | * that match the criterea, if that is possible. | |
6331 | * We flag the ones we like with 'used'. | |
6332 | */ | |
6333 | *found = 0; | |
6334 | st = match_metadata_desc_imsm("imsm"); | |
6335 | if (st == NULL) { | |
676e87a8 | 6336 | pr_vrb("cannot allocate memory for imsm supertype\n"); |
ca9de185 LM |
6337 | return 0; |
6338 | } | |
6339 | ||
6340 | for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) { | |
6341 | char *devname = tmpdev->devname; | |
6342 | struct stat stb; | |
6343 | struct supertype *tst; | |
6344 | int dfd; | |
6345 | if (tmpdev->used > 1) | |
6346 | continue; | |
6347 | tst = dup_super(st); | |
6348 | if (tst == NULL) { | |
676e87a8 | 6349 | pr_vrb("cannot allocate memory for imsm supertype\n"); |
ca9de185 LM |
6350 | goto err_1; |
6351 | } | |
6352 | tmpdev->container = 0; | |
6353 | dfd = dev_open(devname, O_RDONLY|O_EXCL); | |
6354 | if (dfd < 0) { | |
1ade5cc1 | 6355 | dprintf("cannot open device %s: %s\n", |
ca9de185 LM |
6356 | devname, strerror(errno)); |
6357 | tmpdev->used = 2; | |
6358 | } else if (fstat(dfd, &stb)< 0) { | |
6359 | /* Impossible! */ | |
1ade5cc1 | 6360 | dprintf("fstat failed for %s: %s\n", |
ca9de185 LM |
6361 | devname, strerror(errno)); |
6362 | tmpdev->used = 2; | |
6363 | } else if ((stb.st_mode & S_IFMT) != S_IFBLK) { | |
1ade5cc1 | 6364 | dprintf("%s is not a block device.\n", |
ca9de185 LM |
6365 | devname); |
6366 | tmpdev->used = 2; | |
6367 | } else if (must_be_container(dfd)) { | |
6368 | struct supertype *cst; | |
6369 | cst = super_by_fd(dfd, NULL); | |
6370 | if (cst == NULL) { | |
1ade5cc1 | 6371 | dprintf("cannot recognize container type %s\n", |
ca9de185 LM |
6372 | devname); |
6373 | tmpdev->used = 2; | |
6374 | } else if (tst->ss != st->ss) { | |
1ade5cc1 | 6375 | dprintf("non-imsm container - ignore it: %s\n", |
ca9de185 LM |
6376 | devname); |
6377 | tmpdev->used = 2; | |
6378 | } else if (!tst->ss->load_container || | |
6379 | tst->ss->load_container(tst, dfd, NULL)) | |
6380 | tmpdev->used = 2; | |
6381 | else { | |
6382 | tmpdev->container = 1; | |
6383 | } | |
6384 | if (cst) | |
6385 | cst->ss->free_super(cst); | |
6386 | } else { | |
6387 | tmpdev->st_rdev = stb.st_rdev; | |
6388 | if (tst->ss->load_super(tst,dfd, NULL)) { | |
1ade5cc1 | 6389 | dprintf("no RAID superblock on %s\n", |
ca9de185 LM |
6390 | devname); |
6391 | tmpdev->used = 2; | |
6392 | } else if (tst->ss->compare_super == NULL) { | |
1ade5cc1 | 6393 | dprintf("Cannot assemble %s metadata on %s\n", |
ca9de185 LM |
6394 | tst->ss->name, devname); |
6395 | tmpdev->used = 2; | |
6396 | } | |
6397 | } | |
6398 | if (dfd >= 0) | |
6399 | close(dfd); | |
6400 | if (tmpdev->used == 2 || tmpdev->used == 4) { | |
6401 | /* Ignore unrecognised devices during auto-assembly */ | |
6402 | goto loop; | |
6403 | } | |
6404 | else { | |
6405 | struct mdinfo info; | |
6406 | tst->ss->getinfo_super(tst, &info, NULL); | |
6407 | ||
6408 | if (st->minor_version == -1) | |
6409 | st->minor_version = tst->minor_version; | |
6410 | ||
6411 | if (memcmp(info.uuid, uuid_zero, | |
6412 | sizeof(int[4])) == 0) { | |
6413 | /* this is a floating spare. It cannot define | |
6414 | * an array unless there are no more arrays of | |
6415 | * this type to be found. It can be included | |
6416 | * in an array of this type though. | |
6417 | */ | |
6418 | tmpdev->used = 3; | |
6419 | goto loop; | |
6420 | } | |
6421 | ||
6422 | if (st->ss != tst->ss || | |
6423 | st->minor_version != tst->minor_version || | |
6424 | st->ss->compare_super(st, tst) != 0) { | |
6425 | /* Some mismatch. If exactly one array matches this host, | |
6426 | * we can resolve on that one. | |
6427 | * Or, if we are auto assembling, we just ignore the second | |
6428 | * for now. | |
6429 | */ | |
1ade5cc1 | 6430 | dprintf("superblock on %s doesn't match others - assembly aborted\n", |
ca9de185 LM |
6431 | devname); |
6432 | goto loop; | |
6433 | } | |
6434 | tmpdev->used = 1; | |
6435 | *found = 1; | |
6436 | dprintf("found: devname: %s\n", devname); | |
6437 | } | |
6438 | loop: | |
6439 | if (tst) | |
6440 | tst->ss->free_super(tst); | |
6441 | } | |
6442 | if (*found != 0) { | |
6443 | int err; | |
6444 | if ((err = load_super_imsm_all(st, -1, &st->sb, NULL, devlist, 0)) == 0) { | |
6445 | struct mdinfo *iter, *head = st->ss->container_content(st, NULL); | |
6446 | for (iter = head; iter; iter = iter->next) { | |
6447 | dprintf("content->text_version: %s vol\n", | |
6448 | iter->text_version); | |
6449 | if (iter->array.state & (1<<MD_SB_BLOCK_VOLUME)) { | |
6450 | /* do not assemble arrays with unsupported | |
6451 | configurations */ | |
1ade5cc1 | 6452 | dprintf("Cannot activate member %s.\n", |
ca9de185 LM |
6453 | iter->text_version); |
6454 | } else | |
6455 | count++; | |
6456 | } | |
6457 | sysfs_free(head); | |
6458 | ||
6459 | } else { | |
1ade5cc1 | 6460 | dprintf("No valid super block on device list: err: %d %p\n", |
ca9de185 LM |
6461 | err, st->sb); |
6462 | } | |
6463 | } else { | |
1ade5cc1 | 6464 | dprintf("no more devices to examine\n"); |
ca9de185 LM |
6465 | } |
6466 | ||
6467 | for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) { | |
089f9d79 | 6468 | if (tmpdev->used == 1 && tmpdev->found) { |
ca9de185 LM |
6469 | if (count) { |
6470 | if (count < tmpdev->found) | |
6471 | count = 0; | |
6472 | else | |
6473 | count -= tmpdev->found; | |
6474 | } | |
6475 | } | |
6476 | if (tmpdev->used == 1) | |
6477 | tmpdev->used = 4; | |
6478 | } | |
6479 | err_1: | |
6480 | if (st) | |
6481 | st->ss->free_super(st); | |
6482 | return count; | |
6483 | } | |
6484 | ||
ca9de185 | 6485 | static int |
72a45777 | 6486 | count_volumes(struct intel_hba *hba, int dpa, int verbose) |
ca9de185 | 6487 | { |
72a45777 | 6488 | struct sys_dev *idev, *intel_devices = find_intel_devices(); |
ca9de185 | 6489 | int count = 0; |
72a45777 PB |
6490 | const struct orom_entry *entry; |
6491 | struct devid_list *dv, *devid_list; | |
ca9de185 | 6492 | |
72a45777 | 6493 | if (!hba || !hba->path) |
ca9de185 LM |
6494 | return 0; |
6495 | ||
72a45777 PB |
6496 | for (idev = intel_devices; idev; idev = idev->next) { |
6497 | if (strstr(idev->path, hba->path)) | |
6498 | break; | |
6499 | } | |
6500 | ||
6501 | if (!idev || !idev->dev_id) | |
ca9de185 | 6502 | return 0; |
72a45777 PB |
6503 | |
6504 | entry = get_orom_entry_by_device_id(idev->dev_id); | |
6505 | ||
6506 | if (!entry || !entry->devid_list) | |
6507 | return 0; | |
6508 | ||
6509 | devid_list = entry->devid_list; | |
6510 | for (dv = devid_list; dv; dv = dv->next) { | |
594dc1b8 | 6511 | struct md_list *devlist; |
72a45777 PB |
6512 | struct sys_dev *device = device_by_id(dv->devid); |
6513 | char *hba_path; | |
6514 | int found = 0; | |
6515 | ||
6516 | if (device) | |
6517 | hba_path = device->path; | |
6518 | else | |
6519 | return 0; | |
6520 | ||
6521 | devlist = get_devices(hba_path); | |
6522 | /* if no intel devices return zero volumes */ | |
6523 | if (devlist == NULL) | |
6524 | return 0; | |
6525 | ||
6526 | count += active_arrays_by_format("imsm", hba_path, &devlist, dpa, verbose); | |
6527 | dprintf("path: %s active arrays: %d\n", hba_path, count); | |
6528 | if (devlist == NULL) | |
6529 | return 0; | |
6530 | do { | |
6531 | found = 0; | |
6532 | count += count_volumes_list(devlist, | |
6533 | NULL, | |
6534 | verbose, | |
6535 | &found); | |
6536 | dprintf("found %d count: %d\n", found, count); | |
6537 | } while (found); | |
6538 | ||
6539 | dprintf("path: %s total number of volumes: %d\n", hba_path, count); | |
6540 | ||
6541 | while (devlist) { | |
6542 | struct md_list *dv = devlist; | |
6543 | devlist = devlist->next; | |
6544 | free(dv->devname); | |
6545 | free(dv); | |
6546 | } | |
ca9de185 LM |
6547 | } |
6548 | return count; | |
6549 | } | |
6550 | ||
cd9d1ac7 DW |
6551 | static int imsm_default_chunk(const struct imsm_orom *orom) |
6552 | { | |
6553 | /* up to 512 if the plaform supports it, otherwise the platform max. | |
6554 | * 128 if no platform detected | |
6555 | */ | |
6556 | int fs = max(7, orom ? fls(orom->sss) : 0); | |
6557 | ||
6558 | return min(512, (1 << fs)); | |
6559 | } | |
73408129 | 6560 | |
6592ce37 DW |
6561 | static int |
6562 | validate_geometry_imsm_orom(struct intel_super *super, int level, int layout, | |
2cc699af | 6563 | int raiddisks, int *chunk, unsigned long long size, int verbose) |
6592ce37 | 6564 | { |
660260d0 DW |
6565 | /* check/set platform and metadata limits/defaults */ |
6566 | if (super->orom && raiddisks > super->orom->dpa) { | |
676e87a8 | 6567 | pr_vrb("platform supports a maximum of %d disks per array\n", |
660260d0 | 6568 | super->orom->dpa); |
73408129 LM |
6569 | return 0; |
6570 | } | |
6571 | ||
5d500228 | 6572 | /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */ |
660260d0 | 6573 | if (!is_raid_level_supported(super->orom, level, raiddisks)) { |
676e87a8 | 6574 | pr_vrb("platform does not support raid%d with %d disk%s\n", |
6592ce37 DW |
6575 | level, raiddisks, raiddisks > 1 ? "s" : ""); |
6576 | return 0; | |
6577 | } | |
cd9d1ac7 | 6578 | |
7ccc4cc4 | 6579 | if (*chunk == 0 || *chunk == UnSet) |
cd9d1ac7 DW |
6580 | *chunk = imsm_default_chunk(super->orom); |
6581 | ||
7ccc4cc4 | 6582 | if (super->orom && !imsm_orom_has_chunk(super->orom, *chunk)) { |
676e87a8 | 6583 | pr_vrb("platform does not support a chunk size of: %d\n", *chunk); |
cd9d1ac7 | 6584 | return 0; |
6592ce37 | 6585 | } |
cd9d1ac7 | 6586 | |
6592ce37 DW |
6587 | if (layout != imsm_level_to_layout(level)) { |
6588 | if (level == 5) | |
676e87a8 | 6589 | pr_vrb("imsm raid 5 only supports the left-asymmetric layout\n"); |
6592ce37 | 6590 | else if (level == 10) |
676e87a8 | 6591 | pr_vrb("imsm raid 10 only supports the n2 layout\n"); |
6592ce37 | 6592 | else |
676e87a8 | 6593 | pr_vrb("imsm unknown layout %#x for this raid level %d\n", |
6592ce37 DW |
6594 | layout, level); |
6595 | return 0; | |
6596 | } | |
2cc699af | 6597 | |
7ccc4cc4 | 6598 | if (super->orom && (super->orom->attr & IMSM_OROM_ATTR_2TB) == 0 && |
2cc699af | 6599 | (calc_array_size(level, raiddisks, layout, *chunk, size) >> 32) > 0) { |
676e87a8 | 6600 | pr_vrb("platform does not support a volume size over 2TB\n"); |
2cc699af CA |
6601 | return 0; |
6602 | } | |
614902f6 | 6603 | |
6592ce37 DW |
6604 | return 1; |
6605 | } | |
6606 | ||
1011e834 | 6607 | /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd |
c2c087e6 DW |
6608 | * FIX ME add ahci details |
6609 | */ | |
8b353278 | 6610 | static int validate_geometry_imsm_volume(struct supertype *st, int level, |
c21e737b | 6611 | int layout, int raiddisks, int *chunk, |
af4348dd N |
6612 | unsigned long long size, |
6613 | unsigned long long data_offset, | |
6614 | char *dev, | |
2c514b71 NB |
6615 | unsigned long long *freesize, |
6616 | int verbose) | |
cdddbdbc | 6617 | { |
c2c087e6 DW |
6618 | struct stat stb; |
6619 | struct intel_super *super = st->sb; | |
b2916f25 | 6620 | struct imsm_super *mpb; |
c2c087e6 DW |
6621 | struct dl *dl; |
6622 | unsigned long long pos = 0; | |
6623 | unsigned long long maxsize; | |
6624 | struct extent *e; | |
6625 | int i; | |
cdddbdbc | 6626 | |
88c32bb1 DW |
6627 | /* We must have the container info already read in. */ |
6628 | if (!super) | |
c2c087e6 DW |
6629 | return 0; |
6630 | ||
b2916f25 JS |
6631 | mpb = super->anchor; |
6632 | ||
2cc699af | 6633 | if (!validate_geometry_imsm_orom(super, level, layout, raiddisks, chunk, size, verbose)) { |
7a862a02 | 6634 | pr_err("RAID gemetry validation failed. Cannot proceed with the action(s).\n"); |
c2c087e6 | 6635 | return 0; |
d54559f0 | 6636 | } |
c2c087e6 DW |
6637 | if (!dev) { |
6638 | /* General test: make sure there is space for | |
2da8544a DW |
6639 | * 'raiddisks' device extents of size 'size' at a given |
6640 | * offset | |
c2c087e6 | 6641 | */ |
e46273eb | 6642 | unsigned long long minsize = size; |
b7528a20 | 6643 | unsigned long long start_offset = MaxSector; |
c2c087e6 DW |
6644 | int dcnt = 0; |
6645 | if (minsize == 0) | |
6646 | minsize = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS; | |
6647 | for (dl = super->disks; dl ; dl = dl->next) { | |
6648 | int found = 0; | |
6649 | ||
bf5a934a | 6650 | pos = 0; |
c2c087e6 DW |
6651 | i = 0; |
6652 | e = get_extents(super, dl); | |
6653 | if (!e) continue; | |
6654 | do { | |
6655 | unsigned long long esize; | |
6656 | esize = e[i].start - pos; | |
6657 | if (esize >= minsize) | |
6658 | found = 1; | |
b7528a20 | 6659 | if (found && start_offset == MaxSector) { |
2da8544a DW |
6660 | start_offset = pos; |
6661 | break; | |
6662 | } else if (found && pos != start_offset) { | |
6663 | found = 0; | |
6664 | break; | |
6665 | } | |
c2c087e6 DW |
6666 | pos = e[i].start + e[i].size; |
6667 | i++; | |
6668 | } while (e[i-1].size); | |
6669 | if (found) | |
6670 | dcnt++; | |
6671 | free(e); | |
6672 | } | |
6673 | if (dcnt < raiddisks) { | |
2c514b71 | 6674 | if (verbose) |
7a862a02 | 6675 | pr_err("imsm: Not enough devices with space for this array (%d < %d)\n", |
2c514b71 | 6676 | dcnt, raiddisks); |
c2c087e6 DW |
6677 | return 0; |
6678 | } | |
6679 | return 1; | |
6680 | } | |
0dcecb2e | 6681 | |
c2c087e6 DW |
6682 | /* This device must be a member of the set */ |
6683 | if (stat(dev, &stb) < 0) | |
6684 | return 0; | |
6685 | if ((S_IFMT & stb.st_mode) != S_IFBLK) | |
6686 | return 0; | |
6687 | for (dl = super->disks ; dl ; dl = dl->next) { | |
f21e18ca N |
6688 | if (dl->major == (int)major(stb.st_rdev) && |
6689 | dl->minor == (int)minor(stb.st_rdev)) | |
c2c087e6 DW |
6690 | break; |
6691 | } | |
6692 | if (!dl) { | |
2c514b71 | 6693 | if (verbose) |
7a862a02 | 6694 | pr_err("%s is not in the same imsm set\n", dev); |
c2c087e6 | 6695 | return 0; |
a20d2ba5 DW |
6696 | } else if (super->orom && dl->index < 0 && mpb->num_raid_devs) { |
6697 | /* If a volume is present then the current creation attempt | |
6698 | * cannot incorporate new spares because the orom may not | |
6699 | * understand this configuration (all member disks must be | |
6700 | * members of each array in the container). | |
6701 | */ | |
7a862a02 N |
6702 | pr_err("%s is a spare and a volume is already defined for this container\n", dev); |
6703 | pr_err("The option-rom requires all member disks to be a member of all volumes\n"); | |
a20d2ba5 | 6704 | return 0; |
5fe62b94 WD |
6705 | } else if (super->orom && mpb->num_raid_devs > 0 && |
6706 | mpb->num_disks != raiddisks) { | |
7a862a02 | 6707 | pr_err("The option-rom requires all member disks to be a member of all volumes\n"); |
5fe62b94 | 6708 | return 0; |
c2c087e6 | 6709 | } |
0dcecb2e DW |
6710 | |
6711 | /* retrieve the largest free space block */ | |
c2c087e6 DW |
6712 | e = get_extents(super, dl); |
6713 | maxsize = 0; | |
6714 | i = 0; | |
0dcecb2e DW |
6715 | if (e) { |
6716 | do { | |
6717 | unsigned long long esize; | |
6718 | ||
6719 | esize = e[i].start - pos; | |
6720 | if (esize >= maxsize) | |
6721 | maxsize = esize; | |
6722 | pos = e[i].start + e[i].size; | |
6723 | i++; | |
6724 | } while (e[i-1].size); | |
6725 | dl->e = e; | |
6726 | dl->extent_cnt = i; | |
6727 | } else { | |
6728 | if (verbose) | |
e7b84f9d | 6729 | pr_err("unable to determine free space for: %s\n", |
0dcecb2e DW |
6730 | dev); |
6731 | return 0; | |
6732 | } | |
6733 | if (maxsize < size) { | |
6734 | if (verbose) | |
e7b84f9d | 6735 | pr_err("%s not enough space (%llu < %llu)\n", |
0dcecb2e DW |
6736 | dev, maxsize, size); |
6737 | return 0; | |
6738 | } | |
6739 | ||
6740 | /* count total number of extents for merge */ | |
6741 | i = 0; | |
6742 | for (dl = super->disks; dl; dl = dl->next) | |
6743 | if (dl->e) | |
6744 | i += dl->extent_cnt; | |
6745 | ||
6746 | maxsize = merge_extents(super, i); | |
3baa56ab LO |
6747 | |
6748 | if (!check_env("IMSM_NO_PLATFORM") && | |
6749 | mpb->num_raid_devs > 0 && size && size != maxsize) { | |
7a862a02 | 6750 | pr_err("attempting to create a second volume with size less then remaining space. Aborting...\n"); |
3baa56ab LO |
6751 | return 0; |
6752 | } | |
6753 | ||
a7dd165b | 6754 | if (maxsize < size || maxsize == 0) { |
b3071342 LD |
6755 | if (verbose) { |
6756 | if (maxsize == 0) | |
7a862a02 | 6757 | pr_err("no free space left on device. Aborting...\n"); |
b3071342 | 6758 | else |
7a862a02 | 6759 | pr_err("not enough space to create volume of given size (%llu < %llu). Aborting...\n", |
b3071342 LD |
6760 | maxsize, size); |
6761 | } | |
0dcecb2e | 6762 | return 0; |
0dcecb2e DW |
6763 | } |
6764 | ||
c2c087e6 DW |
6765 | *freesize = maxsize; |
6766 | ||
ca9de185 | 6767 | if (super->orom) { |
72a45777 | 6768 | int count = count_volumes(super->hba, |
ca9de185 LM |
6769 | super->orom->dpa, verbose); |
6770 | if (super->orom->vphba <= count) { | |
676e87a8 | 6771 | pr_vrb("platform does not support more than %d raid volumes.\n", |
ca9de185 LM |
6772 | super->orom->vphba); |
6773 | return 0; | |
6774 | } | |
6775 | } | |
c2c087e6 | 6776 | return 1; |
cdddbdbc DW |
6777 | } |
6778 | ||
13bcac90 | 6779 | static int imsm_get_free_size(struct supertype *st, int raiddisks, |
efb30e7f DW |
6780 | unsigned long long size, int chunk, |
6781 | unsigned long long *freesize) | |
6782 | { | |
6783 | struct intel_super *super = st->sb; | |
6784 | struct imsm_super *mpb = super->anchor; | |
6785 | struct dl *dl; | |
6786 | int i; | |
6787 | int extent_cnt; | |
6788 | struct extent *e; | |
6789 | unsigned long long maxsize; | |
6790 | unsigned long long minsize; | |
6791 | int cnt; | |
6792 | int used; | |
6793 | ||
6794 | /* find the largest common start free region of the possible disks */ | |
6795 | used = 0; | |
6796 | extent_cnt = 0; | |
6797 | cnt = 0; | |
6798 | for (dl = super->disks; dl; dl = dl->next) { | |
6799 | dl->raiddisk = -1; | |
6800 | ||
6801 | if (dl->index >= 0) | |
6802 | used++; | |
6803 | ||
6804 | /* don't activate new spares if we are orom constrained | |
6805 | * and there is already a volume active in the container | |
6806 | */ | |
6807 | if (super->orom && dl->index < 0 && mpb->num_raid_devs) | |
6808 | continue; | |
6809 | ||
6810 | e = get_extents(super, dl); | |
6811 | if (!e) | |
6812 | continue; | |
6813 | for (i = 1; e[i-1].size; i++) | |
6814 | ; | |
6815 | dl->e = e; | |
6816 | dl->extent_cnt = i; | |
6817 | extent_cnt += i; | |
6818 | cnt++; | |
6819 | } | |
6820 | ||
6821 | maxsize = merge_extents(super, extent_cnt); | |
6822 | minsize = size; | |
6823 | if (size == 0) | |
612e59d8 CA |
6824 | /* chunk is in K */ |
6825 | minsize = chunk * 2; | |
efb30e7f DW |
6826 | |
6827 | if (cnt < raiddisks || | |
6828 | (super->orom && used && used != raiddisks) || | |
a7dd165b DW |
6829 | maxsize < minsize || |
6830 | maxsize == 0) { | |
e7b84f9d | 6831 | pr_err("not enough devices with space to create array.\n"); |
efb30e7f DW |
6832 | return 0; /* No enough free spaces large enough */ |
6833 | } | |
6834 | ||
6835 | if (size == 0) { | |
6836 | size = maxsize; | |
6837 | if (chunk) { | |
612e59d8 CA |
6838 | size /= 2 * chunk; |
6839 | size *= 2 * chunk; | |
efb30e7f | 6840 | } |
f878b242 LM |
6841 | maxsize = size; |
6842 | } | |
6843 | if (!check_env("IMSM_NO_PLATFORM") && | |
6844 | mpb->num_raid_devs > 0 && size && size != maxsize) { | |
7a862a02 | 6845 | pr_err("attempting to create a second volume with size less then remaining space. Aborting...\n"); |
f878b242 | 6846 | return 0; |
efb30e7f | 6847 | } |
efb30e7f DW |
6848 | cnt = 0; |
6849 | for (dl = super->disks; dl; dl = dl->next) | |
6850 | if (dl->e) | |
6851 | dl->raiddisk = cnt++; | |
6852 | ||
6853 | *freesize = size; | |
6854 | ||
13bcac90 AK |
6855 | dprintf("imsm: imsm_get_free_size() returns : %llu\n", size); |
6856 | ||
efb30e7f DW |
6857 | return 1; |
6858 | } | |
6859 | ||
13bcac90 AK |
6860 | static int reserve_space(struct supertype *st, int raiddisks, |
6861 | unsigned long long size, int chunk, | |
6862 | unsigned long long *freesize) | |
6863 | { | |
6864 | struct intel_super *super = st->sb; | |
6865 | struct dl *dl; | |
6866 | int cnt; | |
6867 | int rv = 0; | |
6868 | ||
6869 | rv = imsm_get_free_size(st, raiddisks, size, chunk, freesize); | |
6870 | if (rv) { | |
6871 | cnt = 0; | |
6872 | for (dl = super->disks; dl; dl = dl->next) | |
6873 | if (dl->e) | |
6874 | dl->raiddisk = cnt++; | |
6875 | rv = 1; | |
6876 | } | |
6877 | ||
6878 | return rv; | |
6879 | } | |
6880 | ||
bf5a934a | 6881 | static int validate_geometry_imsm(struct supertype *st, int level, int layout, |
c21e737b | 6882 | int raiddisks, int *chunk, unsigned long long size, |
af4348dd | 6883 | unsigned long long data_offset, |
bf5a934a DW |
6884 | char *dev, unsigned long long *freesize, |
6885 | int verbose) | |
6886 | { | |
6887 | int fd, cfd; | |
6888 | struct mdinfo *sra; | |
20cbe8d2 | 6889 | int is_member = 0; |
bf5a934a | 6890 | |
d54559f0 LM |
6891 | /* load capability |
6892 | * if given unused devices create a container | |
bf5a934a DW |
6893 | * if given given devices in a container create a member volume |
6894 | */ | |
6895 | if (level == LEVEL_CONTAINER) { | |
6896 | /* Must be a fresh device to add to a container */ | |
6897 | return validate_geometry_imsm_container(st, level, layout, | |
c21e737b | 6898 | raiddisks, |
7ccc4cc4 | 6899 | *chunk, |
af4348dd | 6900 | size, data_offset, |
bf5a934a DW |
6901 | dev, freesize, |
6902 | verbose); | |
6903 | } | |
9587c373 | 6904 | |
8592f29d | 6905 | if (!dev) { |
e91a3bad | 6906 | if (st->sb) { |
ca9de185 | 6907 | struct intel_super *super = st->sb; |
e91a3bad | 6908 | if (!validate_geometry_imsm_orom(st->sb, level, layout, |
2cc699af | 6909 | raiddisks, chunk, size, |
e91a3bad LM |
6910 | verbose)) |
6911 | return 0; | |
efb30e7f DW |
6912 | /* we are being asked to automatically layout a |
6913 | * new volume based on the current contents of | |
6914 | * the container. If the the parameters can be | |
6915 | * satisfied reserve_space will record the disks, | |
6916 | * start offset, and size of the volume to be | |
6917 | * created. add_to_super and getinfo_super | |
6918 | * detect when autolayout is in progress. | |
6919 | */ | |
ca9de185 LM |
6920 | /* assuming that freesize is always given when array is |
6921 | created */ | |
6922 | if (super->orom && freesize) { | |
6923 | int count; | |
72a45777 | 6924 | count = count_volumes(super->hba, |
ca9de185 LM |
6925 | super->orom->dpa, verbose); |
6926 | if (super->orom->vphba <= count) { | |
676e87a8 | 6927 | pr_vrb("platform does not support more than %d raid volumes.\n", |
ca9de185 LM |
6928 | super->orom->vphba); |
6929 | return 0; | |
6930 | } | |
6931 | } | |
e91a3bad LM |
6932 | if (freesize) |
6933 | return reserve_space(st, raiddisks, size, | |
7ccc4cc4 | 6934 | *chunk, freesize); |
8592f29d N |
6935 | } |
6936 | return 1; | |
6937 | } | |
bf5a934a DW |
6938 | if (st->sb) { |
6939 | /* creating in a given container */ | |
6940 | return validate_geometry_imsm_volume(st, level, layout, | |
6941 | raiddisks, chunk, size, | |
af4348dd | 6942 | data_offset, |
bf5a934a DW |
6943 | dev, freesize, verbose); |
6944 | } | |
6945 | ||
bf5a934a DW |
6946 | /* This device needs to be a device in an 'imsm' container */ |
6947 | fd = open(dev, O_RDONLY|O_EXCL, 0); | |
6948 | if (fd >= 0) { | |
6949 | if (verbose) | |
e7b84f9d N |
6950 | pr_err("Cannot create this array on device %s\n", |
6951 | dev); | |
bf5a934a DW |
6952 | close(fd); |
6953 | return 0; | |
6954 | } | |
6955 | if (errno != EBUSY || (fd = open(dev, O_RDONLY, 0)) < 0) { | |
6956 | if (verbose) | |
e7b84f9d | 6957 | pr_err("Cannot open %s: %s\n", |
bf5a934a DW |
6958 | dev, strerror(errno)); |
6959 | return 0; | |
6960 | } | |
6961 | /* Well, it is in use by someone, maybe an 'imsm' container. */ | |
6962 | cfd = open_container(fd); | |
20cbe8d2 | 6963 | close(fd); |
bf5a934a | 6964 | if (cfd < 0) { |
bf5a934a | 6965 | if (verbose) |
e7b84f9d | 6966 | pr_err("Cannot use %s: It is busy\n", |
bf5a934a DW |
6967 | dev); |
6968 | return 0; | |
6969 | } | |
4dd2df09 | 6970 | sra = sysfs_read(cfd, NULL, GET_VERSION); |
bf5a934a | 6971 | if (sra && sra->array.major_version == -1 && |
20cbe8d2 AW |
6972 | strcmp(sra->text_version, "imsm") == 0) |
6973 | is_member = 1; | |
6974 | sysfs_free(sra); | |
6975 | if (is_member) { | |
bf5a934a DW |
6976 | /* This is a member of a imsm container. Load the container |
6977 | * and try to create a volume | |
6978 | */ | |
6979 | struct intel_super *super; | |
6980 | ||
ec50f7b6 | 6981 | if (load_super_imsm_all(st, cfd, (void **) &super, NULL, NULL, 1) == 0) { |
bf5a934a | 6982 | st->sb = super; |
4dd2df09 | 6983 | strcpy(st->container_devnm, fd2devnm(cfd)); |
bf5a934a DW |
6984 | close(cfd); |
6985 | return validate_geometry_imsm_volume(st, level, layout, | |
6986 | raiddisks, chunk, | |
af4348dd | 6987 | size, data_offset, dev, |
ecbd9e81 N |
6988 | freesize, 1) |
6989 | ? 1 : -1; | |
bf5a934a | 6990 | } |
20cbe8d2 | 6991 | } |
bf5a934a | 6992 | |
20cbe8d2 | 6993 | if (verbose) |
e7b84f9d | 6994 | pr_err("failed container membership check\n"); |
20cbe8d2 AW |
6995 | |
6996 | close(cfd); | |
6997 | return 0; | |
bf5a934a | 6998 | } |
0bd16cf2 | 6999 | |
30f58b22 | 7000 | static void default_geometry_imsm(struct supertype *st, int *level, int *layout, int *chunk) |
0bd16cf2 DJ |
7001 | { |
7002 | struct intel_super *super = st->sb; | |
7003 | ||
30f58b22 DW |
7004 | if (level && *level == UnSet) |
7005 | *level = LEVEL_CONTAINER; | |
7006 | ||
7007 | if (level && layout && *layout == UnSet) | |
7008 | *layout = imsm_level_to_layout(*level); | |
0bd16cf2 | 7009 | |
cd9d1ac7 DW |
7010 | if (chunk && (*chunk == UnSet || *chunk == 0)) |
7011 | *chunk = imsm_default_chunk(super->orom); | |
0bd16cf2 DJ |
7012 | } |
7013 | ||
33414a01 DW |
7014 | static void handle_missing(struct intel_super *super, struct imsm_dev *dev); |
7015 | ||
7016 | static int kill_subarray_imsm(struct supertype *st) | |
7017 | { | |
7018 | /* remove the subarray currently referenced by ->current_vol */ | |
7019 | __u8 i; | |
7020 | struct intel_dev **dp; | |
7021 | struct intel_super *super = st->sb; | |
7022 | __u8 current_vol = super->current_vol; | |
7023 | struct imsm_super *mpb = super->anchor; | |
7024 | ||
7025 | if (super->current_vol < 0) | |
7026 | return 2; | |
7027 | super->current_vol = -1; /* invalidate subarray cursor */ | |
7028 | ||
7029 | /* block deletions that would change the uuid of active subarrays | |
7030 | * | |
7031 | * FIXME when immutable ids are available, but note that we'll | |
7032 | * also need to fixup the invalidated/active subarray indexes in | |
7033 | * mdstat | |
7034 | */ | |
7035 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
7036 | char subarray[4]; | |
7037 | ||
7038 | if (i < current_vol) | |
7039 | continue; | |
7040 | sprintf(subarray, "%u", i); | |
4dd2df09 | 7041 | if (is_subarray_active(subarray, st->devnm)) { |
e7b84f9d N |
7042 | pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n", |
7043 | current_vol, i); | |
33414a01 DW |
7044 | |
7045 | return 2; | |
7046 | } | |
7047 | } | |
7048 | ||
7049 | if (st->update_tail) { | |
503975b9 | 7050 | struct imsm_update_kill_array *u = xmalloc(sizeof(*u)); |
33414a01 | 7051 | |
33414a01 DW |
7052 | u->type = update_kill_array; |
7053 | u->dev_idx = current_vol; | |
7054 | append_metadata_update(st, u, sizeof(*u)); | |
7055 | ||
7056 | return 0; | |
7057 | } | |
7058 | ||
7059 | for (dp = &super->devlist; *dp;) | |
7060 | if ((*dp)->index == current_vol) { | |
7061 | *dp = (*dp)->next; | |
7062 | } else { | |
7063 | handle_missing(super, (*dp)->dev); | |
7064 | if ((*dp)->index > current_vol) | |
7065 | (*dp)->index--; | |
7066 | dp = &(*dp)->next; | |
7067 | } | |
7068 | ||
7069 | /* no more raid devices, all active components are now spares, | |
7070 | * but of course failed are still failed | |
7071 | */ | |
7072 | if (--mpb->num_raid_devs == 0) { | |
7073 | struct dl *d; | |
7074 | ||
7075 | for (d = super->disks; d; d = d->next) | |
a8619d23 AK |
7076 | if (d->index > -2) |
7077 | mark_spare(d); | |
33414a01 DW |
7078 | } |
7079 | ||
7080 | super->updates_pending++; | |
7081 | ||
7082 | return 0; | |
7083 | } | |
aa534678 | 7084 | |
a951a4f7 | 7085 | static int update_subarray_imsm(struct supertype *st, char *subarray, |
fa56eddb | 7086 | char *update, struct mddev_ident *ident) |
aa534678 DW |
7087 | { |
7088 | /* update the subarray currently referenced by ->current_vol */ | |
7089 | struct intel_super *super = st->sb; | |
7090 | struct imsm_super *mpb = super->anchor; | |
7091 | ||
aa534678 DW |
7092 | if (strcmp(update, "name") == 0) { |
7093 | char *name = ident->name; | |
a951a4f7 N |
7094 | char *ep; |
7095 | int vol; | |
aa534678 | 7096 | |
4dd2df09 | 7097 | if (is_subarray_active(subarray, st->devnm)) { |
e7b84f9d | 7098 | pr_err("Unable to update name of active subarray\n"); |
aa534678 DW |
7099 | return 2; |
7100 | } | |
7101 | ||
7102 | if (!check_name(super, name, 0)) | |
7103 | return 2; | |
7104 | ||
a951a4f7 N |
7105 | vol = strtoul(subarray, &ep, 10); |
7106 | if (*ep != '\0' || vol >= super->anchor->num_raid_devs) | |
7107 | return 2; | |
7108 | ||
aa534678 | 7109 | if (st->update_tail) { |
503975b9 | 7110 | struct imsm_update_rename_array *u = xmalloc(sizeof(*u)); |
aa534678 | 7111 | |
aa534678 | 7112 | u->type = update_rename_array; |
a951a4f7 | 7113 | u->dev_idx = vol; |
aa534678 DW |
7114 | snprintf((char *) u->name, MAX_RAID_SERIAL_LEN, "%s", name); |
7115 | append_metadata_update(st, u, sizeof(*u)); | |
7116 | } else { | |
7117 | struct imsm_dev *dev; | |
7118 | int i; | |
7119 | ||
a951a4f7 | 7120 | dev = get_imsm_dev(super, vol); |
aa534678 DW |
7121 | snprintf((char *) dev->volume, MAX_RAID_SERIAL_LEN, "%s", name); |
7122 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
7123 | dev = get_imsm_dev(super, i); | |
7124 | handle_missing(super, dev); | |
7125 | } | |
7126 | super->updates_pending++; | |
7127 | } | |
7128 | } else | |
7129 | return 2; | |
7130 | ||
7131 | return 0; | |
7132 | } | |
d1e02575 | 7133 | #endif /* MDASSEMBLE */ |
bf5a934a | 7134 | |
28bce06f AK |
7135 | static int is_gen_migration(struct imsm_dev *dev) |
7136 | { | |
7534230b AK |
7137 | if (dev == NULL) |
7138 | return 0; | |
7139 | ||
28bce06f AK |
7140 | if (!dev->vol.migr_state) |
7141 | return 0; | |
7142 | ||
7143 | if (migr_type(dev) == MIGR_GEN_MIGR) | |
7144 | return 1; | |
7145 | ||
7146 | return 0; | |
7147 | } | |
7148 | ||
1e5c6983 DW |
7149 | static int is_rebuilding(struct imsm_dev *dev) |
7150 | { | |
7151 | struct imsm_map *migr_map; | |
7152 | ||
7153 | if (!dev->vol.migr_state) | |
7154 | return 0; | |
7155 | ||
7156 | if (migr_type(dev) != MIGR_REBUILD) | |
7157 | return 0; | |
7158 | ||
238c0a71 | 7159 | migr_map = get_imsm_map(dev, MAP_1); |
1e5c6983 DW |
7160 | |
7161 | if (migr_map->map_state == IMSM_T_STATE_DEGRADED) | |
7162 | return 1; | |
7163 | else | |
7164 | return 0; | |
7165 | } | |
7166 | ||
b4ab44d8 | 7167 | #ifndef MDASSEMBLE |
6ce1fbf1 AK |
7168 | static int is_initializing(struct imsm_dev *dev) |
7169 | { | |
7170 | struct imsm_map *migr_map; | |
7171 | ||
7172 | if (!dev->vol.migr_state) | |
7173 | return 0; | |
7174 | ||
7175 | if (migr_type(dev) != MIGR_INIT) | |
7176 | return 0; | |
7177 | ||
238c0a71 | 7178 | migr_map = get_imsm_map(dev, MAP_1); |
6ce1fbf1 AK |
7179 | |
7180 | if (migr_map->map_state == IMSM_T_STATE_UNINITIALIZED) | |
7181 | return 1; | |
7182 | ||
7183 | return 0; | |
6ce1fbf1 | 7184 | } |
b4ab44d8 | 7185 | #endif |
6ce1fbf1 | 7186 | |
c47b0ff6 AK |
7187 | static void update_recovery_start(struct intel_super *super, |
7188 | struct imsm_dev *dev, | |
7189 | struct mdinfo *array) | |
1e5c6983 DW |
7190 | { |
7191 | struct mdinfo *rebuild = NULL; | |
7192 | struct mdinfo *d; | |
7193 | __u32 units; | |
7194 | ||
7195 | if (!is_rebuilding(dev)) | |
7196 | return; | |
7197 | ||
7198 | /* Find the rebuild target, but punt on the dual rebuild case */ | |
7199 | for (d = array->devs; d; d = d->next) | |
7200 | if (d->recovery_start == 0) { | |
7201 | if (rebuild) | |
7202 | return; | |
7203 | rebuild = d; | |
7204 | } | |
7205 | ||
4363fd80 DW |
7206 | if (!rebuild) { |
7207 | /* (?) none of the disks are marked with | |
7208 | * IMSM_ORD_REBUILD, so assume they are missing and the | |
7209 | * disk_ord_tbl was not correctly updated | |
7210 | */ | |
1ade5cc1 | 7211 | dprintf("failed to locate out-of-sync disk\n"); |
4363fd80 DW |
7212 | return; |
7213 | } | |
7214 | ||
1e5c6983 | 7215 | units = __le32_to_cpu(dev->vol.curr_migr_unit); |
c47b0ff6 | 7216 | rebuild->recovery_start = units * blocks_per_migr_unit(super, dev); |
1e5c6983 DW |
7217 | } |
7218 | ||
9e2d750d | 7219 | #ifndef MDASSEMBLE |
276d77db | 7220 | static int recover_backup_imsm(struct supertype *st, struct mdinfo *info); |
9e2d750d | 7221 | #endif |
1e5c6983 | 7222 | |
00bbdbda | 7223 | static struct mdinfo *container_content_imsm(struct supertype *st, char *subarray) |
cdddbdbc | 7224 | { |
4f5bc454 DW |
7225 | /* Given a container loaded by load_super_imsm_all, |
7226 | * extract information about all the arrays into | |
7227 | * an mdinfo tree. | |
00bbdbda | 7228 | * If 'subarray' is given, just extract info about that array. |
4f5bc454 DW |
7229 | * |
7230 | * For each imsm_dev create an mdinfo, fill it in, | |
7231 | * then look for matching devices in super->disks | |
7232 | * and create appropriate device mdinfo. | |
7233 | */ | |
7234 | struct intel_super *super = st->sb; | |
949c47a0 | 7235 | struct imsm_super *mpb = super->anchor; |
4f5bc454 | 7236 | struct mdinfo *rest = NULL; |
00bbdbda | 7237 | unsigned int i; |
81219e70 | 7238 | int sb_errors = 0; |
abef11a3 AK |
7239 | struct dl *d; |
7240 | int spare_disks = 0; | |
cdddbdbc | 7241 | |
19482bcc AK |
7242 | /* do not assemble arrays when not all attributes are supported */ |
7243 | if (imsm_check_attributes(mpb->attributes) == 0) { | |
81219e70 | 7244 | sb_errors = 1; |
7a862a02 | 7245 | pr_err("Unsupported attributes in IMSM metadata.Arrays activation is blocked.\n"); |
19482bcc AK |
7246 | } |
7247 | ||
abef11a3 AK |
7248 | /* count spare devices, not used in maps |
7249 | */ | |
7250 | for (d = super->disks; d; d = d->next) | |
7251 | if (d->index == -1) | |
7252 | spare_disks++; | |
7253 | ||
4f5bc454 | 7254 | for (i = 0; i < mpb->num_raid_devs; i++) { |
00bbdbda N |
7255 | struct imsm_dev *dev; |
7256 | struct imsm_map *map; | |
86e3692b | 7257 | struct imsm_map *map2; |
4f5bc454 | 7258 | struct mdinfo *this; |
a6482415 N |
7259 | int slot; |
7260 | #ifndef MDASSEMBLE | |
7261 | int chunk; | |
7262 | #endif | |
00bbdbda N |
7263 | char *ep; |
7264 | ||
7265 | if (subarray && | |
7266 | (i != strtoul(subarray, &ep, 10) || *ep != '\0')) | |
7267 | continue; | |
7268 | ||
7269 | dev = get_imsm_dev(super, i); | |
238c0a71 AK |
7270 | map = get_imsm_map(dev, MAP_0); |
7271 | map2 = get_imsm_map(dev, MAP_1); | |
4f5bc454 | 7272 | |
1ce0101c DW |
7273 | /* do not publish arrays that are in the middle of an |
7274 | * unsupported migration | |
7275 | */ | |
7276 | if (dev->vol.migr_state && | |
28bce06f | 7277 | (migr_type(dev) == MIGR_STATE_CHANGE)) { |
7a862a02 | 7278 | pr_err("cannot assemble volume '%.16s': unsupported migration in progress\n", |
1ce0101c DW |
7279 | dev->volume); |
7280 | continue; | |
7281 | } | |
2db86302 LM |
7282 | /* do not publish arrays that are not support by controller's |
7283 | * OROM/EFI | |
7284 | */ | |
1ce0101c | 7285 | |
503975b9 | 7286 | this = xmalloc(sizeof(*this)); |
4f5bc454 | 7287 | |
301406c9 | 7288 | super->current_vol = i; |
a5d85af7 | 7289 | getinfo_super_imsm_volume(st, this, NULL); |
9894ec0d | 7290 | this->next = rest; |
81219e70 | 7291 | #ifndef MDASSEMBLE |
a6482415 | 7292 | chunk = __le16_to_cpu(map->blocks_per_strip) >> 1; |
81219e70 LM |
7293 | /* mdadm does not support all metadata features- set the bit in all arrays state */ |
7294 | if (!validate_geometry_imsm_orom(super, | |
7295 | get_imsm_raid_level(map), /* RAID level */ | |
7296 | imsm_level_to_layout(get_imsm_raid_level(map)), | |
7297 | map->num_members, /* raid disks */ | |
2cc699af | 7298 | &chunk, join_u32(dev->size_low, dev->size_high), |
81219e70 | 7299 | 1 /* verbose */)) { |
7a862a02 | 7300 | pr_err("IMSM RAID geometry validation failed. Array %s activation is blocked.\n", |
81219e70 LM |
7301 | dev->volume); |
7302 | this->array.state |= | |
7303 | (1<<MD_SB_BLOCK_CONTAINER_RESHAPE) | | |
7304 | (1<<MD_SB_BLOCK_VOLUME); | |
7305 | } | |
7306 | #endif | |
7307 | ||
7308 | /* if array has bad blocks, set suitable bit in all arrays state */ | |
7309 | if (sb_errors) | |
7310 | this->array.state |= | |
7311 | (1<<MD_SB_BLOCK_CONTAINER_RESHAPE) | | |
7312 | (1<<MD_SB_BLOCK_VOLUME); | |
7313 | ||
4f5bc454 | 7314 | for (slot = 0 ; slot < map->num_members; slot++) { |
1e5c6983 | 7315 | unsigned long long recovery_start; |
4f5bc454 DW |
7316 | struct mdinfo *info_d; |
7317 | struct dl *d; | |
7318 | int idx; | |
9a1608e5 | 7319 | int skip; |
7eef0453 | 7320 | __u32 ord; |
4f5bc454 | 7321 | |
9a1608e5 | 7322 | skip = 0; |
238c0a71 AK |
7323 | idx = get_imsm_disk_idx(dev, slot, MAP_0); |
7324 | ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X); | |
4f5bc454 DW |
7325 | for (d = super->disks; d ; d = d->next) |
7326 | if (d->index == idx) | |
0fbd635c | 7327 | break; |
4f5bc454 | 7328 | |
1e5c6983 | 7329 | recovery_start = MaxSector; |
4f5bc454 | 7330 | if (d == NULL) |
9a1608e5 | 7331 | skip = 1; |
25ed7e59 | 7332 | if (d && is_failed(&d->disk)) |
9a1608e5 | 7333 | skip = 1; |
7eef0453 | 7334 | if (ord & IMSM_ORD_REBUILD) |
1e5c6983 | 7335 | recovery_start = 0; |
9a1608e5 | 7336 | |
1011e834 | 7337 | /* |
9a1608e5 | 7338 | * if we skip some disks the array will be assmebled degraded; |
1e5c6983 DW |
7339 | * reset resync start to avoid a dirty-degraded |
7340 | * situation when performing the intial sync | |
9a1608e5 DW |
7341 | * |
7342 | * FIXME handle dirty degraded | |
7343 | */ | |
1e5c6983 | 7344 | if ((skip || recovery_start == 0) && !dev->vol.dirty) |
b7528a20 | 7345 | this->resync_start = MaxSector; |
9a1608e5 DW |
7346 | if (skip) |
7347 | continue; | |
4f5bc454 | 7348 | |
503975b9 | 7349 | info_d = xcalloc(1, sizeof(*info_d)); |
4f5bc454 DW |
7350 | info_d->next = this->devs; |
7351 | this->devs = info_d; | |
7352 | ||
4f5bc454 DW |
7353 | info_d->disk.number = d->index; |
7354 | info_d->disk.major = d->major; | |
7355 | info_d->disk.minor = d->minor; | |
7356 | info_d->disk.raid_disk = slot; | |
1e5c6983 | 7357 | info_d->recovery_start = recovery_start; |
86e3692b AK |
7358 | if (map2) { |
7359 | if (slot < map2->num_members) | |
7360 | info_d->disk.state = (1 << MD_DISK_ACTIVE); | |
04c3c514 AK |
7361 | else |
7362 | this->array.spare_disks++; | |
86e3692b AK |
7363 | } else { |
7364 | if (slot < map->num_members) | |
7365 | info_d->disk.state = (1 << MD_DISK_ACTIVE); | |
04c3c514 AK |
7366 | else |
7367 | this->array.spare_disks++; | |
86e3692b | 7368 | } |
1e5c6983 DW |
7369 | if (info_d->recovery_start == MaxSector) |
7370 | this->array.working_disks++; | |
4f5bc454 DW |
7371 | |
7372 | info_d->events = __le32_to_cpu(mpb->generation_num); | |
5551b113 | 7373 | info_d->data_offset = pba_of_lba0(map); |
06fb291a PB |
7374 | |
7375 | if (map->raid_level == 5) { | |
7376 | info_d->component_size = | |
7377 | num_data_stripes(map) * | |
7378 | map->blocks_per_strip; | |
7379 | } else { | |
7380 | info_d->component_size = blocks_per_member(map); | |
7381 | } | |
b12796be TM |
7382 | |
7383 | info_d->bb.supported = 0; | |
7384 | get_volume_badblocks(super->bbm_log, ord_to_idx(ord), | |
7385 | info_d->data_offset, | |
7386 | info_d->component_size, | |
7387 | &info_d->bb); | |
4f5bc454 | 7388 | } |
1e5c6983 | 7389 | /* now that the disk list is up-to-date fixup recovery_start */ |
c47b0ff6 | 7390 | update_recovery_start(super, dev, this); |
abef11a3 | 7391 | this->array.spare_disks += spare_disks; |
276d77db | 7392 | |
9e2d750d | 7393 | #ifndef MDASSEMBLE |
276d77db AK |
7394 | /* check for reshape */ |
7395 | if (this->reshape_active == 1) | |
7396 | recover_backup_imsm(st, this); | |
9e2d750d | 7397 | #endif |
9a1608e5 | 7398 | rest = this; |
4f5bc454 DW |
7399 | } |
7400 | ||
7401 | return rest; | |
cdddbdbc DW |
7402 | } |
7403 | ||
3b451610 AK |
7404 | static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev, |
7405 | int failed, int look_in_map) | |
c2a1e7da | 7406 | { |
3b451610 AK |
7407 | struct imsm_map *map; |
7408 | ||
7409 | map = get_imsm_map(dev, look_in_map); | |
c2a1e7da DW |
7410 | |
7411 | if (!failed) | |
1011e834 | 7412 | return map->map_state == IMSM_T_STATE_UNINITIALIZED ? |
3393c6af | 7413 | IMSM_T_STATE_UNINITIALIZED : IMSM_T_STATE_NORMAL; |
c2a1e7da DW |
7414 | |
7415 | switch (get_imsm_raid_level(map)) { | |
7416 | case 0: | |
7417 | return IMSM_T_STATE_FAILED; | |
7418 | break; | |
7419 | case 1: | |
7420 | if (failed < map->num_members) | |
7421 | return IMSM_T_STATE_DEGRADED; | |
7422 | else | |
7423 | return IMSM_T_STATE_FAILED; | |
7424 | break; | |
7425 | case 10: | |
7426 | { | |
7427 | /** | |
c92a2527 DW |
7428 | * check to see if any mirrors have failed, otherwise we |
7429 | * are degraded. Even numbered slots are mirrored on | |
7430 | * slot+1 | |
c2a1e7da | 7431 | */ |
c2a1e7da | 7432 | int i; |
d9b420a5 N |
7433 | /* gcc -Os complains that this is unused */ |
7434 | int insync = insync; | |
c2a1e7da DW |
7435 | |
7436 | for (i = 0; i < map->num_members; i++) { | |
238c0a71 | 7437 | __u32 ord = get_imsm_ord_tbl_ent(dev, i, MAP_X); |
c92a2527 DW |
7438 | int idx = ord_to_idx(ord); |
7439 | struct imsm_disk *disk; | |
c2a1e7da | 7440 | |
c92a2527 | 7441 | /* reset the potential in-sync count on even-numbered |
1011e834 | 7442 | * slots. num_copies is always 2 for imsm raid10 |
c92a2527 DW |
7443 | */ |
7444 | if ((i & 1) == 0) | |
7445 | insync = 2; | |
c2a1e7da | 7446 | |
c92a2527 | 7447 | disk = get_imsm_disk(super, idx); |
25ed7e59 | 7448 | if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD) |
c92a2527 | 7449 | insync--; |
c2a1e7da | 7450 | |
c92a2527 DW |
7451 | /* no in-sync disks left in this mirror the |
7452 | * array has failed | |
7453 | */ | |
7454 | if (insync == 0) | |
7455 | return IMSM_T_STATE_FAILED; | |
c2a1e7da DW |
7456 | } |
7457 | ||
7458 | return IMSM_T_STATE_DEGRADED; | |
7459 | } | |
7460 | case 5: | |
7461 | if (failed < 2) | |
7462 | return IMSM_T_STATE_DEGRADED; | |
7463 | else | |
7464 | return IMSM_T_STATE_FAILED; | |
7465 | break; | |
7466 | default: | |
7467 | break; | |
7468 | } | |
7469 | ||
7470 | return map->map_state; | |
7471 | } | |
7472 | ||
3b451610 AK |
7473 | static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev, |
7474 | int look_in_map) | |
c2a1e7da DW |
7475 | { |
7476 | int i; | |
7477 | int failed = 0; | |
7478 | struct imsm_disk *disk; | |
d5985138 AK |
7479 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
7480 | struct imsm_map *prev = get_imsm_map(dev, MAP_1); | |
68fe4598 | 7481 | struct imsm_map *map_for_loop; |
0556e1a2 DW |
7482 | __u32 ord; |
7483 | int idx; | |
d5985138 | 7484 | int idx_1; |
c2a1e7da | 7485 | |
0556e1a2 DW |
7486 | /* at the beginning of migration we set IMSM_ORD_REBUILD on |
7487 | * disks that are being rebuilt. New failures are recorded to | |
7488 | * map[0]. So we look through all the disks we started with and | |
7489 | * see if any failures are still present, or if any new ones | |
7490 | * have arrived | |
0556e1a2 | 7491 | */ |
d5985138 AK |
7492 | map_for_loop = map; |
7493 | if (prev && (map->num_members < prev->num_members)) | |
7494 | map_for_loop = prev; | |
68fe4598 LD |
7495 | |
7496 | for (i = 0; i < map_for_loop->num_members; i++) { | |
d5985138 | 7497 | idx_1 = -255; |
238c0a71 AK |
7498 | /* when MAP_X is passed both maps failures are counted |
7499 | */ | |
d5985138 | 7500 | if (prev && |
089f9d79 JS |
7501 | (look_in_map == MAP_1 || look_in_map == MAP_X) && |
7502 | i < prev->num_members) { | |
d5985138 AK |
7503 | ord = __le32_to_cpu(prev->disk_ord_tbl[i]); |
7504 | idx_1 = ord_to_idx(ord); | |
c2a1e7da | 7505 | |
d5985138 AK |
7506 | disk = get_imsm_disk(super, idx_1); |
7507 | if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD) | |
7508 | failed++; | |
7509 | } | |
089f9d79 JS |
7510 | if ((look_in_map == MAP_0 || look_in_map == MAP_X) && |
7511 | i < map->num_members) { | |
d5985138 AK |
7512 | ord = __le32_to_cpu(map->disk_ord_tbl[i]); |
7513 | idx = ord_to_idx(ord); | |
7514 | ||
7515 | if (idx != idx_1) { | |
7516 | disk = get_imsm_disk(super, idx); | |
7517 | if (!disk || is_failed(disk) || | |
7518 | ord & IMSM_ORD_REBUILD) | |
7519 | failed++; | |
7520 | } | |
7521 | } | |
c2a1e7da DW |
7522 | } |
7523 | ||
7524 | return failed; | |
845dea95 NB |
7525 | } |
7526 | ||
97b4d0e9 DW |
7527 | #ifndef MDASSEMBLE |
7528 | static int imsm_open_new(struct supertype *c, struct active_array *a, | |
7529 | char *inst) | |
7530 | { | |
7531 | struct intel_super *super = c->sb; | |
7532 | struct imsm_super *mpb = super->anchor; | |
bbab0940 | 7533 | struct imsm_update_prealloc_bb_mem u; |
9587c373 | 7534 | |
97b4d0e9 | 7535 | if (atoi(inst) >= mpb->num_raid_devs) { |
1ade5cc1 | 7536 | pr_err("subarry index %d, out of range\n", atoi(inst)); |
97b4d0e9 DW |
7537 | return -ENODEV; |
7538 | } | |
7539 | ||
7540 | dprintf("imsm: open_new %s\n", inst); | |
7541 | a->info.container_member = atoi(inst); | |
bbab0940 TM |
7542 | |
7543 | u.type = update_prealloc_badblocks_mem; | |
7544 | imsm_update_metadata_locally(c, &u, sizeof(u)); | |
7545 | ||
97b4d0e9 DW |
7546 | return 0; |
7547 | } | |
7548 | ||
0c046afd DW |
7549 | static int is_resyncing(struct imsm_dev *dev) |
7550 | { | |
7551 | struct imsm_map *migr_map; | |
7552 | ||
7553 | if (!dev->vol.migr_state) | |
7554 | return 0; | |
7555 | ||
1484e727 DW |
7556 | if (migr_type(dev) == MIGR_INIT || |
7557 | migr_type(dev) == MIGR_REPAIR) | |
0c046afd DW |
7558 | return 1; |
7559 | ||
4c9bc37b AK |
7560 | if (migr_type(dev) == MIGR_GEN_MIGR) |
7561 | return 0; | |
7562 | ||
238c0a71 | 7563 | migr_map = get_imsm_map(dev, MAP_1); |
0c046afd | 7564 | |
089f9d79 JS |
7565 | if (migr_map->map_state == IMSM_T_STATE_NORMAL && |
7566 | dev->vol.migr_type != MIGR_GEN_MIGR) | |
0c046afd DW |
7567 | return 1; |
7568 | else | |
7569 | return 0; | |
7570 | } | |
7571 | ||
0556e1a2 | 7572 | /* return true if we recorded new information */ |
4c9e8c1e TM |
7573 | static int mark_failure(struct intel_super *super, |
7574 | struct imsm_dev *dev, struct imsm_disk *disk, int idx) | |
47ee5a45 | 7575 | { |
0556e1a2 DW |
7576 | __u32 ord; |
7577 | int slot; | |
7578 | struct imsm_map *map; | |
86c54047 DW |
7579 | char buf[MAX_RAID_SERIAL_LEN+3]; |
7580 | unsigned int len, shift = 0; | |
0556e1a2 DW |
7581 | |
7582 | /* new failures are always set in map[0] */ | |
238c0a71 | 7583 | map = get_imsm_map(dev, MAP_0); |
0556e1a2 DW |
7584 | |
7585 | slot = get_imsm_disk_slot(map, idx); | |
7586 | if (slot < 0) | |
7587 | return 0; | |
7588 | ||
7589 | ord = __le32_to_cpu(map->disk_ord_tbl[slot]); | |
25ed7e59 | 7590 | if (is_failed(disk) && (ord & IMSM_ORD_REBUILD)) |
0556e1a2 DW |
7591 | return 0; |
7592 | ||
7d0c5e24 LD |
7593 | memcpy(buf, disk->serial, MAX_RAID_SERIAL_LEN); |
7594 | buf[MAX_RAID_SERIAL_LEN] = '\000'; | |
7595 | strcat(buf, ":0"); | |
86c54047 DW |
7596 | if ((len = strlen(buf)) >= MAX_RAID_SERIAL_LEN) |
7597 | shift = len - MAX_RAID_SERIAL_LEN + 1; | |
7598 | strncpy((char *)disk->serial, &buf[shift], MAX_RAID_SERIAL_LEN); | |
7599 | ||
f2f27e63 | 7600 | disk->status |= FAILED_DISK; |
0556e1a2 | 7601 | set_imsm_ord_tbl_ent(map, slot, idx | IMSM_ORD_REBUILD); |
17788994 AK |
7602 | /* mark failures in second map if second map exists and this disk |
7603 | * in this slot. | |
7604 | * This is valid for migration, initialization and rebuild | |
7605 | */ | |
7606 | if (dev->vol.migr_state) { | |
238c0a71 | 7607 | struct imsm_map *map2 = get_imsm_map(dev, MAP_1); |
0a108d63 AK |
7608 | int slot2 = get_imsm_disk_slot(map2, idx); |
7609 | ||
089f9d79 | 7610 | if (slot2 < map2->num_members && slot2 >= 0) |
0a108d63 | 7611 | set_imsm_ord_tbl_ent(map2, slot2, |
1ace8403 AK |
7612 | idx | IMSM_ORD_REBUILD); |
7613 | } | |
f21e18ca | 7614 | if (map->failed_disk_num == 0xff) |
0556e1a2 | 7615 | map->failed_disk_num = slot; |
4c9e8c1e TM |
7616 | |
7617 | clear_disk_badblocks(super->bbm_log, ord_to_idx(ord)); | |
7618 | ||
0556e1a2 DW |
7619 | return 1; |
7620 | } | |
7621 | ||
4c9e8c1e TM |
7622 | static void mark_missing(struct intel_super *super, |
7623 | struct imsm_dev *dev, struct imsm_disk *disk, int idx) | |
0556e1a2 | 7624 | { |
4c9e8c1e | 7625 | mark_failure(super, dev, disk, idx); |
0556e1a2 DW |
7626 | |
7627 | if (disk->scsi_id == __cpu_to_le32(~(__u32)0)) | |
7628 | return; | |
7629 | ||
47ee5a45 DW |
7630 | disk->scsi_id = __cpu_to_le32(~(__u32)0); |
7631 | memmove(&disk->serial[0], &disk->serial[1], MAX_RAID_SERIAL_LEN - 1); | |
7632 | } | |
7633 | ||
33414a01 DW |
7634 | static void handle_missing(struct intel_super *super, struct imsm_dev *dev) |
7635 | { | |
33414a01 | 7636 | struct dl *dl; |
33414a01 DW |
7637 | |
7638 | if (!super->missing) | |
7639 | return; | |
33414a01 | 7640 | |
79b68f1b PC |
7641 | /* When orom adds replacement for missing disk it does |
7642 | * not remove entry of missing disk, but just updates map with | |
7643 | * new added disk. So it is not enough just to test if there is | |
7644 | * any missing disk, we have to look if there are any failed disks | |
7645 | * in map to stop migration */ | |
7646 | ||
33414a01 | 7647 | dprintf("imsm: mark missing\n"); |
3d59f0c0 AK |
7648 | /* end process for initialization and rebuild only |
7649 | */ | |
7650 | if (is_gen_migration(dev) == 0) { | |
7651 | __u8 map_state; | |
7652 | int failed; | |
7653 | ||
7654 | failed = imsm_count_failed(super, dev, MAP_0); | |
7655 | map_state = imsm_check_degraded(super, dev, failed, MAP_0); | |
7656 | ||
79b68f1b PC |
7657 | if (failed) |
7658 | end_migration(dev, super, map_state); | |
3d59f0c0 | 7659 | } |
33414a01 | 7660 | for (dl = super->missing; dl; dl = dl->next) |
4c9e8c1e | 7661 | mark_missing(super, dev, &dl->disk, dl->index); |
33414a01 DW |
7662 | super->updates_pending++; |
7663 | } | |
7664 | ||
f3871fdc AK |
7665 | static unsigned long long imsm_set_array_size(struct imsm_dev *dev, |
7666 | long long new_size) | |
70bdf0dc | 7667 | { |
238c0a71 | 7668 | int used_disks = imsm_num_data_members(dev, MAP_0); |
70bdf0dc AK |
7669 | unsigned long long array_blocks; |
7670 | struct imsm_map *map; | |
7671 | ||
7672 | if (used_disks == 0) { | |
7673 | /* when problems occures | |
7674 | * return current array_blocks value | |
7675 | */ | |
7676 | array_blocks = __le32_to_cpu(dev->size_high); | |
7677 | array_blocks = array_blocks << 32; | |
7678 | array_blocks += __le32_to_cpu(dev->size_low); | |
7679 | ||
7680 | return array_blocks; | |
7681 | } | |
7682 | ||
7683 | /* set array size in metadata | |
7684 | */ | |
f3871fdc AK |
7685 | if (new_size <= 0) { |
7686 | /* OLCE size change is caused by added disks | |
7687 | */ | |
7688 | map = get_imsm_map(dev, MAP_0); | |
7689 | array_blocks = blocks_per_member(map) * used_disks; | |
7690 | } else { | |
7691 | /* Online Volume Size Change | |
7692 | * Using available free space | |
7693 | */ | |
7694 | array_blocks = new_size; | |
7695 | } | |
70bdf0dc AK |
7696 | |
7697 | /* round array size down to closest MB | |
7698 | */ | |
7699 | array_blocks = (array_blocks >> SECT_PER_MB_SHIFT) << SECT_PER_MB_SHIFT; | |
7700 | dev->size_low = __cpu_to_le32((__u32)array_blocks); | |
7701 | dev->size_high = __cpu_to_le32((__u32)(array_blocks >> 32)); | |
7702 | ||
7703 | return array_blocks; | |
7704 | } | |
7705 | ||
28bce06f AK |
7706 | static void imsm_set_disk(struct active_array *a, int n, int state); |
7707 | ||
0e2d1a4e AK |
7708 | static void imsm_progress_container_reshape(struct intel_super *super) |
7709 | { | |
7710 | /* if no device has a migr_state, but some device has a | |
7711 | * different number of members than the previous device, start | |
7712 | * changing the number of devices in this device to match | |
7713 | * previous. | |
7714 | */ | |
7715 | struct imsm_super *mpb = super->anchor; | |
7716 | int prev_disks = -1; | |
7717 | int i; | |
1dfaa380 | 7718 | int copy_map_size; |
0e2d1a4e AK |
7719 | |
7720 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
7721 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
238c0a71 | 7722 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
0e2d1a4e AK |
7723 | struct imsm_map *map2; |
7724 | int prev_num_members; | |
0e2d1a4e AK |
7725 | |
7726 | if (dev->vol.migr_state) | |
7727 | return; | |
7728 | ||
7729 | if (prev_disks == -1) | |
7730 | prev_disks = map->num_members; | |
7731 | if (prev_disks == map->num_members) | |
7732 | continue; | |
7733 | ||
7734 | /* OK, this array needs to enter reshape mode. | |
7735 | * i.e it needs a migr_state | |
7736 | */ | |
7737 | ||
1dfaa380 | 7738 | copy_map_size = sizeof_imsm_map(map); |
0e2d1a4e AK |
7739 | prev_num_members = map->num_members; |
7740 | map->num_members = prev_disks; | |
7741 | dev->vol.migr_state = 1; | |
7742 | dev->vol.curr_migr_unit = 0; | |
ea672ee1 | 7743 | set_migr_type(dev, MIGR_GEN_MIGR); |
0e2d1a4e AK |
7744 | for (i = prev_num_members; |
7745 | i < map->num_members; i++) | |
7746 | set_imsm_ord_tbl_ent(map, i, i); | |
238c0a71 | 7747 | map2 = get_imsm_map(dev, MAP_1); |
0e2d1a4e | 7748 | /* Copy the current map */ |
1dfaa380 | 7749 | memcpy(map2, map, copy_map_size); |
0e2d1a4e AK |
7750 | map2->num_members = prev_num_members; |
7751 | ||
f3871fdc | 7752 | imsm_set_array_size(dev, -1); |
51d83f5d | 7753 | super->clean_migration_record_by_mdmon = 1; |
0e2d1a4e AK |
7754 | super->updates_pending++; |
7755 | } | |
7756 | } | |
7757 | ||
aad6f216 | 7758 | /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild |
0c046afd DW |
7759 | * states are handled in imsm_set_disk() with one exception, when a |
7760 | * resync is stopped due to a new failure this routine will set the | |
7761 | * 'degraded' state for the array. | |
7762 | */ | |
01f157d7 | 7763 | static int imsm_set_array_state(struct active_array *a, int consistent) |
a862209d DW |
7764 | { |
7765 | int inst = a->info.container_member; | |
7766 | struct intel_super *super = a->container->sb; | |
949c47a0 | 7767 | struct imsm_dev *dev = get_imsm_dev(super, inst); |
238c0a71 | 7768 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
3b451610 AK |
7769 | int failed = imsm_count_failed(super, dev, MAP_0); |
7770 | __u8 map_state = imsm_check_degraded(super, dev, failed, MAP_0); | |
1e5c6983 | 7771 | __u32 blocks_per_unit; |
a862209d | 7772 | |
1af97990 AK |
7773 | if (dev->vol.migr_state && |
7774 | dev->vol.migr_type == MIGR_GEN_MIGR) { | |
7775 | /* array state change is blocked due to reshape action | |
aad6f216 N |
7776 | * We might need to |
7777 | * - abort the reshape (if last_checkpoint is 0 and action!= reshape) | |
7778 | * - finish the reshape (if last_checkpoint is big and action != reshape) | |
7779 | * - update curr_migr_unit | |
1af97990 | 7780 | */ |
aad6f216 N |
7781 | if (a->curr_action == reshape) { |
7782 | /* still reshaping, maybe update curr_migr_unit */ | |
633b5610 | 7783 | goto mark_checkpoint; |
aad6f216 N |
7784 | } else { |
7785 | if (a->last_checkpoint == 0 && a->prev_action == reshape) { | |
7786 | /* for some reason we aborted the reshape. | |
b66e591b AK |
7787 | * |
7788 | * disable automatic metadata rollback | |
7789 | * user action is required to recover process | |
aad6f216 | 7790 | */ |
b66e591b | 7791 | if (0) { |
238c0a71 AK |
7792 | struct imsm_map *map2 = |
7793 | get_imsm_map(dev, MAP_1); | |
7794 | dev->vol.migr_state = 0; | |
7795 | set_migr_type(dev, 0); | |
7796 | dev->vol.curr_migr_unit = 0; | |
7797 | memcpy(map, map2, | |
7798 | sizeof_imsm_map(map2)); | |
7799 | super->updates_pending++; | |
b66e591b | 7800 | } |
aad6f216 N |
7801 | } |
7802 | if (a->last_checkpoint >= a->info.component_size) { | |
7803 | unsigned long long array_blocks; | |
7804 | int used_disks; | |
e154ced3 | 7805 | struct mdinfo *mdi; |
aad6f216 | 7806 | |
238c0a71 | 7807 | used_disks = imsm_num_data_members(dev, MAP_0); |
d55adef9 AK |
7808 | if (used_disks > 0) { |
7809 | array_blocks = | |
5551b113 | 7810 | blocks_per_member(map) * |
d55adef9 AK |
7811 | used_disks; |
7812 | /* round array size down to closest MB | |
7813 | */ | |
7814 | array_blocks = (array_blocks | |
7815 | >> SECT_PER_MB_SHIFT) | |
7816 | << SECT_PER_MB_SHIFT; | |
d55adef9 AK |
7817 | a->info.custom_array_size = array_blocks; |
7818 | /* encourage manager to update array | |
7819 | * size | |
7820 | */ | |
e154ced3 | 7821 | |
d55adef9 | 7822 | a->check_reshape = 1; |
633b5610 | 7823 | } |
e154ced3 AK |
7824 | /* finalize online capacity expansion/reshape */ |
7825 | for (mdi = a->info.devs; mdi; mdi = mdi->next) | |
7826 | imsm_set_disk(a, | |
7827 | mdi->disk.raid_disk, | |
7828 | mdi->curr_state); | |
7829 | ||
0e2d1a4e | 7830 | imsm_progress_container_reshape(super); |
e154ced3 | 7831 | } |
aad6f216 | 7832 | } |
1af97990 AK |
7833 | } |
7834 | ||
47ee5a45 | 7835 | /* before we activate this array handle any missing disks */ |
33414a01 DW |
7836 | if (consistent == 2) |
7837 | handle_missing(super, dev); | |
1e5c6983 | 7838 | |
0c046afd | 7839 | if (consistent == 2 && |
b7941fd6 | 7840 | (!is_resync_complete(&a->info) || |
0c046afd DW |
7841 | map_state != IMSM_T_STATE_NORMAL || |
7842 | dev->vol.migr_state)) | |
01f157d7 | 7843 | consistent = 0; |
272906ef | 7844 | |
b7941fd6 | 7845 | if (is_resync_complete(&a->info)) { |
0c046afd | 7846 | /* complete intialization / resync, |
0556e1a2 DW |
7847 | * recovery and interrupted recovery is completed in |
7848 | * ->set_disk | |
0c046afd DW |
7849 | */ |
7850 | if (is_resyncing(dev)) { | |
7851 | dprintf("imsm: mark resync done\n"); | |
809da78e | 7852 | end_migration(dev, super, map_state); |
115c3803 | 7853 | super->updates_pending++; |
484240d8 | 7854 | a->last_checkpoint = 0; |
115c3803 | 7855 | } |
b9172665 AK |
7856 | } else if ((!is_resyncing(dev) && !failed) && |
7857 | (imsm_reshape_blocks_arrays_changes(super) == 0)) { | |
0c046afd | 7858 | /* mark the start of the init process if nothing is failed */ |
b7941fd6 | 7859 | dprintf("imsm: mark resync start\n"); |
1484e727 | 7860 | if (map->map_state == IMSM_T_STATE_UNINITIALIZED) |
8e59f3d8 | 7861 | migrate(dev, super, IMSM_T_STATE_NORMAL, MIGR_INIT); |
1484e727 | 7862 | else |
8e59f3d8 | 7863 | migrate(dev, super, IMSM_T_STATE_NORMAL, MIGR_REPAIR); |
3393c6af | 7864 | super->updates_pending++; |
115c3803 | 7865 | } |
a862209d | 7866 | |
633b5610 | 7867 | mark_checkpoint: |
5b83bacf AK |
7868 | /* skip checkpointing for general migration, |
7869 | * it is controlled in mdadm | |
7870 | */ | |
7871 | if (is_gen_migration(dev)) | |
7872 | goto skip_mark_checkpoint; | |
7873 | ||
1e5c6983 | 7874 | /* check if we can update curr_migr_unit from resync_start, recovery_start */ |
c47b0ff6 | 7875 | blocks_per_unit = blocks_per_migr_unit(super, dev); |
4f0a7acc | 7876 | if (blocks_per_unit) { |
1e5c6983 DW |
7877 | __u32 units32; |
7878 | __u64 units; | |
7879 | ||
4f0a7acc | 7880 | units = a->last_checkpoint / blocks_per_unit; |
1e5c6983 DW |
7881 | units32 = units; |
7882 | ||
7883 | /* check that we did not overflow 32-bits, and that | |
7884 | * curr_migr_unit needs updating | |
7885 | */ | |
7886 | if (units32 == units && | |
bfd80a56 | 7887 | units32 != 0 && |
1e5c6983 DW |
7888 | __le32_to_cpu(dev->vol.curr_migr_unit) != units32) { |
7889 | dprintf("imsm: mark checkpoint (%u)\n", units32); | |
7890 | dev->vol.curr_migr_unit = __cpu_to_le32(units32); | |
7891 | super->updates_pending++; | |
7892 | } | |
7893 | } | |
f8f603f1 | 7894 | |
5b83bacf | 7895 | skip_mark_checkpoint: |
3393c6af | 7896 | /* mark dirty / clean */ |
0c046afd | 7897 | if (dev->vol.dirty != !consistent) { |
b7941fd6 | 7898 | dprintf("imsm: mark '%s'\n", consistent ? "clean" : "dirty"); |
0c046afd DW |
7899 | if (consistent) |
7900 | dev->vol.dirty = 0; | |
7901 | else | |
7902 | dev->vol.dirty = 1; | |
a862209d DW |
7903 | super->updates_pending++; |
7904 | } | |
28bce06f | 7905 | |
01f157d7 | 7906 | return consistent; |
a862209d DW |
7907 | } |
7908 | ||
6f50473f TM |
7909 | static int imsm_disk_slot_to_ord(struct active_array *a, int slot) |
7910 | { | |
7911 | int inst = a->info.container_member; | |
7912 | struct intel_super *super = a->container->sb; | |
7913 | struct imsm_dev *dev = get_imsm_dev(super, inst); | |
7914 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
7915 | ||
7916 | if (slot > map->num_members) { | |
7917 | pr_err("imsm: imsm_disk_slot_to_ord %d out of range 0..%d\n", | |
7918 | slot, map->num_members - 1); | |
7919 | return -1; | |
7920 | } | |
7921 | ||
7922 | if (slot < 0) | |
7923 | return -1; | |
7924 | ||
7925 | return get_imsm_ord_tbl_ent(dev, slot, MAP_0); | |
7926 | } | |
7927 | ||
8d45d196 | 7928 | static void imsm_set_disk(struct active_array *a, int n, int state) |
845dea95 | 7929 | { |
8d45d196 DW |
7930 | int inst = a->info.container_member; |
7931 | struct intel_super *super = a->container->sb; | |
949c47a0 | 7932 | struct imsm_dev *dev = get_imsm_dev(super, inst); |
238c0a71 | 7933 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
8d45d196 | 7934 | struct imsm_disk *disk; |
7ce05701 LD |
7935 | struct mdinfo *mdi; |
7936 | int recovery_not_finished = 0; | |
0c046afd | 7937 | int failed; |
6f50473f | 7938 | int ord; |
0c046afd | 7939 | __u8 map_state; |
8d45d196 | 7940 | |
6f50473f TM |
7941 | ord = imsm_disk_slot_to_ord(a, n); |
7942 | if (ord < 0) | |
8d45d196 DW |
7943 | return; |
7944 | ||
4e6e574a | 7945 | dprintf("imsm: set_disk %d:%x\n", n, state); |
b10b37b8 | 7946 | disk = get_imsm_disk(super, ord_to_idx(ord)); |
8d45d196 | 7947 | |
5802a811 | 7948 | /* check for new failures */ |
0556e1a2 | 7949 | if (state & DS_FAULTY) { |
4c9e8c1e | 7950 | if (mark_failure(super, dev, disk, ord_to_idx(ord))) |
0556e1a2 | 7951 | super->updates_pending++; |
8d45d196 | 7952 | } |
47ee5a45 | 7953 | |
19859edc | 7954 | /* check if in_sync */ |
0556e1a2 | 7955 | if (state & DS_INSYNC && ord & IMSM_ORD_REBUILD && is_rebuilding(dev)) { |
238c0a71 | 7956 | struct imsm_map *migr_map = get_imsm_map(dev, MAP_1); |
b10b37b8 DW |
7957 | |
7958 | set_imsm_ord_tbl_ent(migr_map, n, ord_to_idx(ord)); | |
19859edc DW |
7959 | super->updates_pending++; |
7960 | } | |
8d45d196 | 7961 | |
3b451610 AK |
7962 | failed = imsm_count_failed(super, dev, MAP_0); |
7963 | map_state = imsm_check_degraded(super, dev, failed, MAP_0); | |
5802a811 | 7964 | |
0c046afd | 7965 | /* check if recovery complete, newly degraded, or failed */ |
94002678 AK |
7966 | dprintf("imsm: Detected transition to state "); |
7967 | switch (map_state) { | |
7968 | case IMSM_T_STATE_NORMAL: /* transition to normal state */ | |
7969 | dprintf("normal: "); | |
7970 | if (is_rebuilding(dev)) { | |
1ade5cc1 | 7971 | dprintf_cont("while rebuilding"); |
7ce05701 LD |
7972 | /* check if recovery is really finished */ |
7973 | for (mdi = a->info.devs; mdi ; mdi = mdi->next) | |
7974 | if (mdi->recovery_start != MaxSector) { | |
7975 | recovery_not_finished = 1; | |
7976 | break; | |
7977 | } | |
7978 | if (recovery_not_finished) { | |
1ade5cc1 N |
7979 | dprintf_cont("\n"); |
7980 | dprintf("Rebuild has not finished yet, state not changed"); | |
7ce05701 LD |
7981 | if (a->last_checkpoint < mdi->recovery_start) { |
7982 | a->last_checkpoint = mdi->recovery_start; | |
7983 | super->updates_pending++; | |
7984 | } | |
7985 | break; | |
7986 | } | |
94002678 | 7987 | end_migration(dev, super, map_state); |
238c0a71 | 7988 | map = get_imsm_map(dev, MAP_0); |
94002678 AK |
7989 | map->failed_disk_num = ~0; |
7990 | super->updates_pending++; | |
7991 | a->last_checkpoint = 0; | |
7992 | break; | |
7993 | } | |
7994 | if (is_gen_migration(dev)) { | |
1ade5cc1 | 7995 | dprintf_cont("while general migration"); |
bf2f0071 | 7996 | if (a->last_checkpoint >= a->info.component_size) |
809da78e | 7997 | end_migration(dev, super, map_state); |
94002678 AK |
7998 | else |
7999 | map->map_state = map_state; | |
238c0a71 | 8000 | map = get_imsm_map(dev, MAP_0); |
28bce06f | 8001 | map->failed_disk_num = ~0; |
94002678 | 8002 | super->updates_pending++; |
bf2f0071 | 8003 | break; |
94002678 AK |
8004 | } |
8005 | break; | |
8006 | case IMSM_T_STATE_DEGRADED: /* transition to degraded state */ | |
1ade5cc1 | 8007 | dprintf_cont("degraded: "); |
089f9d79 | 8008 | if (map->map_state != map_state && !dev->vol.migr_state) { |
1ade5cc1 | 8009 | dprintf_cont("mark degraded"); |
94002678 AK |
8010 | map->map_state = map_state; |
8011 | super->updates_pending++; | |
8012 | a->last_checkpoint = 0; | |
8013 | break; | |
8014 | } | |
8015 | if (is_rebuilding(dev)) { | |
1ade5cc1 | 8016 | dprintf_cont("while rebuilding."); |
94002678 | 8017 | if (map->map_state != map_state) { |
1ade5cc1 | 8018 | dprintf_cont(" Map state change"); |
94002678 AK |
8019 | end_migration(dev, super, map_state); |
8020 | super->updates_pending++; | |
8021 | } | |
8022 | break; | |
8023 | } | |
8024 | if (is_gen_migration(dev)) { | |
1ade5cc1 | 8025 | dprintf_cont("while general migration"); |
bf2f0071 | 8026 | if (a->last_checkpoint >= a->info.component_size) |
809da78e | 8027 | end_migration(dev, super, map_state); |
94002678 AK |
8028 | else { |
8029 | map->map_state = map_state; | |
3b451610 | 8030 | manage_second_map(super, dev); |
94002678 AK |
8031 | } |
8032 | super->updates_pending++; | |
bf2f0071 | 8033 | break; |
28bce06f | 8034 | } |
6ce1fbf1 | 8035 | if (is_initializing(dev)) { |
1ade5cc1 | 8036 | dprintf_cont("while initialization."); |
6ce1fbf1 AK |
8037 | map->map_state = map_state; |
8038 | super->updates_pending++; | |
8039 | break; | |
8040 | } | |
94002678 AK |
8041 | break; |
8042 | case IMSM_T_STATE_FAILED: /* transition to failed state */ | |
1ade5cc1 | 8043 | dprintf_cont("failed: "); |
94002678 | 8044 | if (is_gen_migration(dev)) { |
1ade5cc1 | 8045 | dprintf_cont("while general migration"); |
94002678 AK |
8046 | map->map_state = map_state; |
8047 | super->updates_pending++; | |
8048 | break; | |
8049 | } | |
8050 | if (map->map_state != map_state) { | |
1ade5cc1 | 8051 | dprintf_cont("mark failed"); |
94002678 AK |
8052 | end_migration(dev, super, map_state); |
8053 | super->updates_pending++; | |
8054 | a->last_checkpoint = 0; | |
8055 | break; | |
8056 | } | |
8057 | break; | |
8058 | default: | |
1ade5cc1 | 8059 | dprintf_cont("state %i\n", map_state); |
5802a811 | 8060 | } |
1ade5cc1 | 8061 | dprintf_cont("\n"); |
845dea95 NB |
8062 | } |
8063 | ||
f796af5d | 8064 | static int store_imsm_mpb(int fd, struct imsm_super *mpb) |
c2a1e7da | 8065 | { |
f796af5d | 8066 | void *buf = mpb; |
c2a1e7da DW |
8067 | __u32 mpb_size = __le32_to_cpu(mpb->mpb_size); |
8068 | unsigned long long dsize; | |
8069 | unsigned long long sectors; | |
f36a9ecd | 8070 | unsigned int sector_size; |
c2a1e7da | 8071 | |
f36a9ecd | 8072 | get_dev_sector_size(fd, NULL, §or_size); |
c2a1e7da DW |
8073 | get_dev_size(fd, NULL, &dsize); |
8074 | ||
f36a9ecd | 8075 | if (mpb_size > sector_size) { |
272f648f | 8076 | /* -1 to account for anchor */ |
f36a9ecd | 8077 | sectors = mpb_sectors(mpb, sector_size) - 1; |
c2a1e7da | 8078 | |
272f648f | 8079 | /* write the extended mpb to the sectors preceeding the anchor */ |
f36a9ecd PB |
8080 | if (lseek64(fd, dsize - (sector_size * (2 + sectors)), |
8081 | SEEK_SET) < 0) | |
272f648f | 8082 | return 1; |
c2a1e7da | 8083 | |
f36a9ecd PB |
8084 | if ((unsigned long long)write(fd, buf + sector_size, |
8085 | sector_size * sectors) != sector_size * sectors) | |
272f648f DW |
8086 | return 1; |
8087 | } | |
c2a1e7da | 8088 | |
272f648f | 8089 | /* first block is stored on second to last sector of the disk */ |
f36a9ecd | 8090 | if (lseek64(fd, dsize - (sector_size * 2), SEEK_SET) < 0) |
c2a1e7da DW |
8091 | return 1; |
8092 | ||
f36a9ecd | 8093 | if (write(fd, buf, sector_size) != sector_size) |
c2a1e7da DW |
8094 | return 1; |
8095 | ||
c2a1e7da DW |
8096 | return 0; |
8097 | } | |
8098 | ||
2e735d19 | 8099 | static void imsm_sync_metadata(struct supertype *container) |
845dea95 | 8100 | { |
2e735d19 | 8101 | struct intel_super *super = container->sb; |
c2a1e7da | 8102 | |
1a64be56 | 8103 | dprintf("sync metadata: %d\n", super->updates_pending); |
c2a1e7da DW |
8104 | if (!super->updates_pending) |
8105 | return; | |
8106 | ||
36988a3d | 8107 | write_super_imsm(container, 0); |
c2a1e7da DW |
8108 | |
8109 | super->updates_pending = 0; | |
845dea95 NB |
8110 | } |
8111 | ||
272906ef DW |
8112 | static struct dl *imsm_readd(struct intel_super *super, int idx, struct active_array *a) |
8113 | { | |
8114 | struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member); | |
238c0a71 | 8115 | int i = get_imsm_disk_idx(dev, idx, MAP_X); |
272906ef DW |
8116 | struct dl *dl; |
8117 | ||
8118 | for (dl = super->disks; dl; dl = dl->next) | |
8119 | if (dl->index == i) | |
8120 | break; | |
8121 | ||
25ed7e59 | 8122 | if (dl && is_failed(&dl->disk)) |
272906ef DW |
8123 | dl = NULL; |
8124 | ||
8125 | if (dl) | |
1ade5cc1 | 8126 | dprintf("found %x:%x\n", dl->major, dl->minor); |
272906ef DW |
8127 | |
8128 | return dl; | |
8129 | } | |
8130 | ||
a20d2ba5 | 8131 | static struct dl *imsm_add_spare(struct intel_super *super, int slot, |
8ba77d32 AK |
8132 | struct active_array *a, int activate_new, |
8133 | struct mdinfo *additional_test_list) | |
272906ef DW |
8134 | { |
8135 | struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member); | |
238c0a71 | 8136 | int idx = get_imsm_disk_idx(dev, slot, MAP_X); |
a20d2ba5 DW |
8137 | struct imsm_super *mpb = super->anchor; |
8138 | struct imsm_map *map; | |
272906ef DW |
8139 | unsigned long long pos; |
8140 | struct mdinfo *d; | |
8141 | struct extent *ex; | |
a20d2ba5 | 8142 | int i, j; |
272906ef | 8143 | int found; |
569cc43f DW |
8144 | __u32 array_start = 0; |
8145 | __u32 array_end = 0; | |
272906ef | 8146 | struct dl *dl; |
6c932028 | 8147 | struct mdinfo *test_list; |
272906ef DW |
8148 | |
8149 | for (dl = super->disks; dl; dl = dl->next) { | |
8150 | /* If in this array, skip */ | |
8151 | for (d = a->info.devs ; d ; d = d->next) | |
e553d2a4 DW |
8152 | if (d->state_fd >= 0 && |
8153 | d->disk.major == dl->major && | |
272906ef | 8154 | d->disk.minor == dl->minor) { |
8ba77d32 AK |
8155 | dprintf("%x:%x already in array\n", |
8156 | dl->major, dl->minor); | |
272906ef DW |
8157 | break; |
8158 | } | |
8159 | if (d) | |
8160 | continue; | |
6c932028 AK |
8161 | test_list = additional_test_list; |
8162 | while (test_list) { | |
8163 | if (test_list->disk.major == dl->major && | |
8164 | test_list->disk.minor == dl->minor) { | |
8ba77d32 AK |
8165 | dprintf("%x:%x already in additional test list\n", |
8166 | dl->major, dl->minor); | |
8167 | break; | |
8168 | } | |
6c932028 | 8169 | test_list = test_list->next; |
8ba77d32 | 8170 | } |
6c932028 | 8171 | if (test_list) |
8ba77d32 | 8172 | continue; |
272906ef | 8173 | |
e553d2a4 | 8174 | /* skip in use or failed drives */ |
25ed7e59 | 8175 | if (is_failed(&dl->disk) || idx == dl->index || |
df474657 DW |
8176 | dl->index == -2) { |
8177 | dprintf("%x:%x status (failed: %d index: %d)\n", | |
25ed7e59 | 8178 | dl->major, dl->minor, is_failed(&dl->disk), idx); |
9a1608e5 DW |
8179 | continue; |
8180 | } | |
8181 | ||
a20d2ba5 DW |
8182 | /* skip pure spares when we are looking for partially |
8183 | * assimilated drives | |
8184 | */ | |
8185 | if (dl->index == -1 && !activate_new) | |
8186 | continue; | |
8187 | ||
272906ef | 8188 | /* Does this unused device have the requisite free space? |
a20d2ba5 | 8189 | * It needs to be able to cover all member volumes |
272906ef DW |
8190 | */ |
8191 | ex = get_extents(super, dl); | |
8192 | if (!ex) { | |
8193 | dprintf("cannot get extents\n"); | |
8194 | continue; | |
8195 | } | |
a20d2ba5 DW |
8196 | for (i = 0; i < mpb->num_raid_devs; i++) { |
8197 | dev = get_imsm_dev(super, i); | |
238c0a71 | 8198 | map = get_imsm_map(dev, MAP_0); |
272906ef | 8199 | |
a20d2ba5 DW |
8200 | /* check if this disk is already a member of |
8201 | * this array | |
272906ef | 8202 | */ |
620b1713 | 8203 | if (get_imsm_disk_slot(map, dl->index) >= 0) |
a20d2ba5 DW |
8204 | continue; |
8205 | ||
8206 | found = 0; | |
8207 | j = 0; | |
8208 | pos = 0; | |
5551b113 | 8209 | array_start = pba_of_lba0(map); |
329c8278 | 8210 | array_end = array_start + |
5551b113 | 8211 | blocks_per_member(map) - 1; |
a20d2ba5 DW |
8212 | |
8213 | do { | |
8214 | /* check that we can start at pba_of_lba0 with | |
8215 | * blocks_per_member of space | |
8216 | */ | |
329c8278 | 8217 | if (array_start >= pos && array_end < ex[j].start) { |
a20d2ba5 DW |
8218 | found = 1; |
8219 | break; | |
8220 | } | |
8221 | pos = ex[j].start + ex[j].size; | |
8222 | j++; | |
8223 | } while (ex[j-1].size); | |
8224 | ||
8225 | if (!found) | |
272906ef | 8226 | break; |
a20d2ba5 | 8227 | } |
272906ef DW |
8228 | |
8229 | free(ex); | |
a20d2ba5 | 8230 | if (i < mpb->num_raid_devs) { |
329c8278 DW |
8231 | dprintf("%x:%x does not have %u to %u available\n", |
8232 | dl->major, dl->minor, array_start, array_end); | |
272906ef DW |
8233 | /* No room */ |
8234 | continue; | |
a20d2ba5 DW |
8235 | } |
8236 | return dl; | |
272906ef DW |
8237 | } |
8238 | ||
8239 | return dl; | |
8240 | } | |
8241 | ||
95d07a2c LM |
8242 | static int imsm_rebuild_allowed(struct supertype *cont, int dev_idx, int failed) |
8243 | { | |
8244 | struct imsm_dev *dev2; | |
8245 | struct imsm_map *map; | |
8246 | struct dl *idisk; | |
8247 | int slot; | |
8248 | int idx; | |
8249 | __u8 state; | |
8250 | ||
8251 | dev2 = get_imsm_dev(cont->sb, dev_idx); | |
8252 | if (dev2) { | |
238c0a71 | 8253 | state = imsm_check_degraded(cont->sb, dev2, failed, MAP_0); |
95d07a2c | 8254 | if (state == IMSM_T_STATE_FAILED) { |
238c0a71 | 8255 | map = get_imsm_map(dev2, MAP_0); |
95d07a2c LM |
8256 | if (!map) |
8257 | return 1; | |
8258 | for (slot = 0; slot < map->num_members; slot++) { | |
8259 | /* | |
8260 | * Check if failed disks are deleted from intel | |
8261 | * disk list or are marked to be deleted | |
8262 | */ | |
238c0a71 | 8263 | idx = get_imsm_disk_idx(dev2, slot, MAP_X); |
95d07a2c LM |
8264 | idisk = get_imsm_dl_disk(cont->sb, idx); |
8265 | /* | |
8266 | * Do not rebuild the array if failed disks | |
8267 | * from failed sub-array are not removed from | |
8268 | * container. | |
8269 | */ | |
8270 | if (idisk && | |
8271 | is_failed(&idisk->disk) && | |
8272 | (idisk->action != DISK_REMOVE)) | |
8273 | return 0; | |
8274 | } | |
8275 | } | |
8276 | } | |
8277 | return 1; | |
8278 | } | |
8279 | ||
88758e9d DW |
8280 | static struct mdinfo *imsm_activate_spare(struct active_array *a, |
8281 | struct metadata_update **updates) | |
8282 | { | |
8283 | /** | |
d23fe947 DW |
8284 | * Find a device with unused free space and use it to replace a |
8285 | * failed/vacant region in an array. We replace failed regions one a | |
8286 | * array at a time. The result is that a new spare disk will be added | |
8287 | * to the first failed array and after the monitor has finished | |
8288 | * propagating failures the remainder will be consumed. | |
88758e9d | 8289 | * |
d23fe947 DW |
8290 | * FIXME add a capability for mdmon to request spares from another |
8291 | * container. | |
88758e9d DW |
8292 | */ |
8293 | ||
8294 | struct intel_super *super = a->container->sb; | |
88758e9d | 8295 | int inst = a->info.container_member; |
949c47a0 | 8296 | struct imsm_dev *dev = get_imsm_dev(super, inst); |
238c0a71 | 8297 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
88758e9d DW |
8298 | int failed = a->info.array.raid_disks; |
8299 | struct mdinfo *rv = NULL; | |
8300 | struct mdinfo *d; | |
8301 | struct mdinfo *di; | |
8302 | struct metadata_update *mu; | |
8303 | struct dl *dl; | |
8304 | struct imsm_update_activate_spare *u; | |
8305 | int num_spares = 0; | |
8306 | int i; | |
95d07a2c | 8307 | int allowed; |
88758e9d DW |
8308 | |
8309 | for (d = a->info.devs ; d ; d = d->next) { | |
8310 | if ((d->curr_state & DS_FAULTY) && | |
8311 | d->state_fd >= 0) | |
8312 | /* wait for Removal to happen */ | |
8313 | return NULL; | |
8314 | if (d->state_fd >= 0) | |
8315 | failed--; | |
8316 | } | |
8317 | ||
8318 | dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n", | |
8319 | inst, failed, a->info.array.raid_disks, a->info.array.level); | |
1af97990 | 8320 | |
e2962bfc AK |
8321 | if (imsm_reshape_blocks_arrays_changes(super)) |
8322 | return NULL; | |
1af97990 | 8323 | |
fc8ca064 AK |
8324 | /* Cannot activate another spare if rebuild is in progress already |
8325 | */ | |
8326 | if (is_rebuilding(dev)) { | |
7a862a02 | 8327 | dprintf("imsm: No spare activation allowed. Rebuild in progress already.\n"); |
fc8ca064 AK |
8328 | return NULL; |
8329 | } | |
8330 | ||
89c67882 AK |
8331 | if (a->info.array.level == 4) |
8332 | /* No repair for takeovered array | |
8333 | * imsm doesn't support raid4 | |
8334 | */ | |
8335 | return NULL; | |
8336 | ||
3b451610 AK |
8337 | if (imsm_check_degraded(super, dev, failed, MAP_0) != |
8338 | IMSM_T_STATE_DEGRADED) | |
88758e9d DW |
8339 | return NULL; |
8340 | ||
83ca7d45 AP |
8341 | if (get_imsm_map(dev, MAP_0)->map_state == IMSM_T_STATE_UNINITIALIZED) { |
8342 | dprintf("imsm: No spare activation allowed. Volume is not initialized.\n"); | |
8343 | return NULL; | |
8344 | } | |
8345 | ||
95d07a2c LM |
8346 | /* |
8347 | * If there are any failed disks check state of the other volume. | |
8348 | * Block rebuild if the another one is failed until failed disks | |
8349 | * are removed from container. | |
8350 | */ | |
8351 | if (failed) { | |
7a862a02 | 8352 | dprintf("found failed disks in %.*s, check if there anotherfailed sub-array.\n", |
c4acd1e5 | 8353 | MAX_RAID_SERIAL_LEN, dev->volume); |
95d07a2c LM |
8354 | /* check if states of the other volumes allow for rebuild */ |
8355 | for (i = 0; i < super->anchor->num_raid_devs; i++) { | |
8356 | if (i != inst) { | |
8357 | allowed = imsm_rebuild_allowed(a->container, | |
8358 | i, failed); | |
8359 | if (!allowed) | |
8360 | return NULL; | |
8361 | } | |
8362 | } | |
8363 | } | |
8364 | ||
88758e9d | 8365 | /* For each slot, if it is not working, find a spare */ |
88758e9d DW |
8366 | for (i = 0; i < a->info.array.raid_disks; i++) { |
8367 | for (d = a->info.devs ; d ; d = d->next) | |
8368 | if (d->disk.raid_disk == i) | |
8369 | break; | |
8370 | dprintf("found %d: %p %x\n", i, d, d?d->curr_state:0); | |
8371 | if (d && (d->state_fd >= 0)) | |
8372 | continue; | |
8373 | ||
272906ef | 8374 | /* |
a20d2ba5 DW |
8375 | * OK, this device needs recovery. Try to re-add the |
8376 | * previous occupant of this slot, if this fails see if | |
8377 | * we can continue the assimilation of a spare that was | |
8378 | * partially assimilated, finally try to activate a new | |
8379 | * spare. | |
272906ef DW |
8380 | */ |
8381 | dl = imsm_readd(super, i, a); | |
8382 | if (!dl) | |
b303fe21 | 8383 | dl = imsm_add_spare(super, i, a, 0, rv); |
a20d2ba5 | 8384 | if (!dl) |
b303fe21 | 8385 | dl = imsm_add_spare(super, i, a, 1, rv); |
272906ef DW |
8386 | if (!dl) |
8387 | continue; | |
1011e834 | 8388 | |
272906ef | 8389 | /* found a usable disk with enough space */ |
503975b9 | 8390 | di = xcalloc(1, sizeof(*di)); |
272906ef DW |
8391 | |
8392 | /* dl->index will be -1 in the case we are activating a | |
8393 | * pristine spare. imsm_process_update() will create a | |
8394 | * new index in this case. Once a disk is found to be | |
8395 | * failed in all member arrays it is kicked from the | |
8396 | * metadata | |
8397 | */ | |
8398 | di->disk.number = dl->index; | |
d23fe947 | 8399 | |
272906ef DW |
8400 | /* (ab)use di->devs to store a pointer to the device |
8401 | * we chose | |
8402 | */ | |
8403 | di->devs = (struct mdinfo *) dl; | |
8404 | ||
8405 | di->disk.raid_disk = i; | |
8406 | di->disk.major = dl->major; | |
8407 | di->disk.minor = dl->minor; | |
8408 | di->disk.state = 0; | |
d23534e4 | 8409 | di->recovery_start = 0; |
5551b113 | 8410 | di->data_offset = pba_of_lba0(map); |
272906ef DW |
8411 | di->component_size = a->info.component_size; |
8412 | di->container_member = inst; | |
b12796be | 8413 | di->bb.supported = 0; |
148acb7b | 8414 | super->random = random32(); |
272906ef DW |
8415 | di->next = rv; |
8416 | rv = di; | |
8417 | num_spares++; | |
8418 | dprintf("%x:%x to be %d at %llu\n", dl->major, dl->minor, | |
8419 | i, di->data_offset); | |
88758e9d DW |
8420 | } |
8421 | ||
8422 | if (!rv) | |
8423 | /* No spares found */ | |
8424 | return rv; | |
8425 | /* Now 'rv' has a list of devices to return. | |
8426 | * Create a metadata_update record to update the | |
8427 | * disk_ord_tbl for the array | |
8428 | */ | |
503975b9 | 8429 | mu = xmalloc(sizeof(*mu)); |
1011e834 | 8430 | mu->buf = xcalloc(num_spares, |
503975b9 | 8431 | sizeof(struct imsm_update_activate_spare)); |
88758e9d | 8432 | mu->space = NULL; |
cb23f1f4 | 8433 | mu->space_list = NULL; |
88758e9d DW |
8434 | mu->len = sizeof(struct imsm_update_activate_spare) * num_spares; |
8435 | mu->next = *updates; | |
8436 | u = (struct imsm_update_activate_spare *) mu->buf; | |
8437 | ||
8438 | for (di = rv ; di ; di = di->next) { | |
8439 | u->type = update_activate_spare; | |
d23fe947 DW |
8440 | u->dl = (struct dl *) di->devs; |
8441 | di->devs = NULL; | |
88758e9d DW |
8442 | u->slot = di->disk.raid_disk; |
8443 | u->array = inst; | |
8444 | u->next = u + 1; | |
8445 | u++; | |
8446 | } | |
8447 | (u-1)->next = NULL; | |
8448 | *updates = mu; | |
8449 | ||
8450 | return rv; | |
8451 | } | |
8452 | ||
54c2c1ea | 8453 | static int disks_overlap(struct intel_super *super, int idx, struct imsm_update_create_array *u) |
8273f55e | 8454 | { |
54c2c1ea | 8455 | struct imsm_dev *dev = get_imsm_dev(super, idx); |
238c0a71 AK |
8456 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
8457 | struct imsm_map *new_map = get_imsm_map(&u->dev, MAP_0); | |
54c2c1ea DW |
8458 | struct disk_info *inf = get_disk_info(u); |
8459 | struct imsm_disk *disk; | |
8273f55e DW |
8460 | int i; |
8461 | int j; | |
8273f55e | 8462 | |
54c2c1ea | 8463 | for (i = 0; i < map->num_members; i++) { |
238c0a71 | 8464 | disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i, MAP_X)); |
54c2c1ea DW |
8465 | for (j = 0; j < new_map->num_members; j++) |
8466 | if (serialcmp(disk->serial, inf[j].serial) == 0) | |
8273f55e DW |
8467 | return 1; |
8468 | } | |
8469 | ||
8470 | return 0; | |
8471 | } | |
8472 | ||
1a64be56 LM |
8473 | static struct dl *get_disk_super(struct intel_super *super, int major, int minor) |
8474 | { | |
594dc1b8 JS |
8475 | struct dl *dl; |
8476 | ||
1a64be56 | 8477 | for (dl = super->disks; dl; dl = dl->next) |
089f9d79 | 8478 | if (dl->major == major && dl->minor == minor) |
1a64be56 LM |
8479 | return dl; |
8480 | return NULL; | |
8481 | } | |
8482 | ||
8483 | static int remove_disk_super(struct intel_super *super, int major, int minor) | |
8484 | { | |
594dc1b8 | 8485 | struct dl *prev; |
1a64be56 LM |
8486 | struct dl *dl; |
8487 | ||
8488 | prev = NULL; | |
8489 | for (dl = super->disks; dl; dl = dl->next) { | |
089f9d79 | 8490 | if (dl->major == major && dl->minor == minor) { |
1a64be56 LM |
8491 | /* remove */ |
8492 | if (prev) | |
8493 | prev->next = dl->next; | |
8494 | else | |
8495 | super->disks = dl->next; | |
8496 | dl->next = NULL; | |
8497 | __free_imsm_disk(dl); | |
1ade5cc1 | 8498 | dprintf("removed %x:%x\n", major, minor); |
1a64be56 LM |
8499 | break; |
8500 | } | |
8501 | prev = dl; | |
8502 | } | |
8503 | return 0; | |
8504 | } | |
8505 | ||
f21e18ca | 8506 | static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index); |
ae6aad82 | 8507 | |
1a64be56 LM |
8508 | static int add_remove_disk_update(struct intel_super *super) |
8509 | { | |
8510 | int check_degraded = 0; | |
594dc1b8 JS |
8511 | struct dl *disk; |
8512 | ||
1a64be56 LM |
8513 | /* add/remove some spares to/from the metadata/contrainer */ |
8514 | while (super->disk_mgmt_list) { | |
8515 | struct dl *disk_cfg; | |
8516 | ||
8517 | disk_cfg = super->disk_mgmt_list; | |
8518 | super->disk_mgmt_list = disk_cfg->next; | |
8519 | disk_cfg->next = NULL; | |
8520 | ||
8521 | if (disk_cfg->action == DISK_ADD) { | |
8522 | disk_cfg->next = super->disks; | |
8523 | super->disks = disk_cfg; | |
8524 | check_degraded = 1; | |
1ade5cc1 N |
8525 | dprintf("added %x:%x\n", |
8526 | disk_cfg->major, disk_cfg->minor); | |
1a64be56 LM |
8527 | } else if (disk_cfg->action == DISK_REMOVE) { |
8528 | dprintf("Disk remove action processed: %x.%x\n", | |
8529 | disk_cfg->major, disk_cfg->minor); | |
8530 | disk = get_disk_super(super, | |
8531 | disk_cfg->major, | |
8532 | disk_cfg->minor); | |
8533 | if (disk) { | |
8534 | /* store action status */ | |
8535 | disk->action = DISK_REMOVE; | |
8536 | /* remove spare disks only */ | |
8537 | if (disk->index == -1) { | |
8538 | remove_disk_super(super, | |
8539 | disk_cfg->major, | |
8540 | disk_cfg->minor); | |
8541 | } | |
8542 | } | |
8543 | /* release allocate disk structure */ | |
8544 | __free_imsm_disk(disk_cfg); | |
8545 | } | |
8546 | } | |
8547 | return check_degraded; | |
8548 | } | |
8549 | ||
a29911da PC |
8550 | static int apply_reshape_migration_update(struct imsm_update_reshape_migration *u, |
8551 | struct intel_super *super, | |
8552 | void ***space_list) | |
8553 | { | |
8554 | struct intel_dev *id; | |
8555 | void **tofree = NULL; | |
8556 | int ret_val = 0; | |
8557 | ||
1ade5cc1 | 8558 | dprintf("(enter)\n"); |
089f9d79 | 8559 | if (u->subdev < 0 || u->subdev > 1) { |
a29911da PC |
8560 | dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev); |
8561 | return ret_val; | |
8562 | } | |
089f9d79 | 8563 | if (space_list == NULL || *space_list == NULL) { |
a29911da PC |
8564 | dprintf("imsm: Error: Memory is not allocated\n"); |
8565 | return ret_val; | |
8566 | } | |
8567 | ||
8568 | for (id = super->devlist ; id; id = id->next) { | |
8569 | if (id->index == (unsigned)u->subdev) { | |
8570 | struct imsm_dev *dev = get_imsm_dev(super, u->subdev); | |
8571 | struct imsm_map *map; | |
8572 | struct imsm_dev *new_dev = | |
8573 | (struct imsm_dev *)*space_list; | |
238c0a71 | 8574 | struct imsm_map *migr_map = get_imsm_map(dev, MAP_1); |
a29911da PC |
8575 | int to_state; |
8576 | struct dl *new_disk; | |
8577 | ||
8578 | if (new_dev == NULL) | |
8579 | return ret_val; | |
8580 | *space_list = **space_list; | |
8581 | memcpy(new_dev, dev, sizeof_imsm_dev(dev, 0)); | |
238c0a71 | 8582 | map = get_imsm_map(new_dev, MAP_0); |
a29911da PC |
8583 | if (migr_map) { |
8584 | dprintf("imsm: Error: migration in progress"); | |
8585 | return ret_val; | |
8586 | } | |
8587 | ||
8588 | to_state = map->map_state; | |
8589 | if ((u->new_level == 5) && (map->raid_level == 0)) { | |
8590 | map->num_members++; | |
8591 | /* this should not happen */ | |
8592 | if (u->new_disks[0] < 0) { | |
8593 | map->failed_disk_num = | |
8594 | map->num_members - 1; | |
8595 | to_state = IMSM_T_STATE_DEGRADED; | |
8596 | } else | |
8597 | to_state = IMSM_T_STATE_NORMAL; | |
8598 | } | |
8e59f3d8 | 8599 | migrate(new_dev, super, to_state, MIGR_GEN_MIGR); |
a29911da PC |
8600 | if (u->new_level > -1) |
8601 | map->raid_level = u->new_level; | |
238c0a71 | 8602 | migr_map = get_imsm_map(new_dev, MAP_1); |
a29911da PC |
8603 | if ((u->new_level == 5) && |
8604 | (migr_map->raid_level == 0)) { | |
8605 | int ord = map->num_members - 1; | |
8606 | migr_map->num_members--; | |
8607 | if (u->new_disks[0] < 0) | |
8608 | ord |= IMSM_ORD_REBUILD; | |
8609 | set_imsm_ord_tbl_ent(map, | |
8610 | map->num_members - 1, | |
8611 | ord); | |
8612 | } | |
8613 | id->dev = new_dev; | |
8614 | tofree = (void **)dev; | |
8615 | ||
4bba0439 PC |
8616 | /* update chunk size |
8617 | */ | |
06fb291a PB |
8618 | if (u->new_chunksize > 0) { |
8619 | unsigned long long num_data_stripes; | |
8620 | int used_disks = | |
8621 | imsm_num_data_members(dev, MAP_0); | |
8622 | ||
8623 | if (used_disks == 0) | |
8624 | return ret_val; | |
8625 | ||
4bba0439 PC |
8626 | map->blocks_per_strip = |
8627 | __cpu_to_le16(u->new_chunksize * 2); | |
06fb291a PB |
8628 | num_data_stripes = |
8629 | (join_u32(dev->size_low, dev->size_high) | |
8630 | / used_disks); | |
8631 | num_data_stripes /= map->blocks_per_strip; | |
8632 | num_data_stripes /= map->num_domains; | |
8633 | set_num_data_stripes(map, num_data_stripes); | |
8634 | } | |
4bba0439 | 8635 | |
a29911da PC |
8636 | /* add disk |
8637 | */ | |
089f9d79 JS |
8638 | if (u->new_level != 5 || migr_map->raid_level != 0 || |
8639 | migr_map->raid_level == map->raid_level) | |
a29911da PC |
8640 | goto skip_disk_add; |
8641 | ||
8642 | if (u->new_disks[0] >= 0) { | |
8643 | /* use passes spare | |
8644 | */ | |
8645 | new_disk = get_disk_super(super, | |
8646 | major(u->new_disks[0]), | |
8647 | minor(u->new_disks[0])); | |
7a862a02 | 8648 | dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n", |
a29911da PC |
8649 | major(u->new_disks[0]), |
8650 | minor(u->new_disks[0]), | |
8651 | new_disk, new_disk->index); | |
8652 | if (new_disk == NULL) | |
8653 | goto error_disk_add; | |
8654 | ||
8655 | new_disk->index = map->num_members - 1; | |
8656 | /* slot to fill in autolayout | |
8657 | */ | |
8658 | new_disk->raiddisk = new_disk->index; | |
8659 | new_disk->disk.status |= CONFIGURED_DISK; | |
8660 | new_disk->disk.status &= ~SPARE_DISK; | |
8661 | } else | |
8662 | goto error_disk_add; | |
8663 | ||
8664 | skip_disk_add: | |
8665 | *tofree = *space_list; | |
8666 | /* calculate new size | |
8667 | */ | |
f3871fdc | 8668 | imsm_set_array_size(new_dev, -1); |
a29911da PC |
8669 | |
8670 | ret_val = 1; | |
8671 | } | |
8672 | } | |
8673 | ||
8674 | if (tofree) | |
8675 | *space_list = tofree; | |
8676 | return ret_val; | |
8677 | ||
8678 | error_disk_add: | |
8679 | dprintf("Error: imsm: Cannot find disk.\n"); | |
8680 | return ret_val; | |
8681 | } | |
8682 | ||
f3871fdc AK |
8683 | static int apply_size_change_update(struct imsm_update_size_change *u, |
8684 | struct intel_super *super) | |
8685 | { | |
8686 | struct intel_dev *id; | |
8687 | int ret_val = 0; | |
8688 | ||
1ade5cc1 | 8689 | dprintf("(enter)\n"); |
089f9d79 | 8690 | if (u->subdev < 0 || u->subdev > 1) { |
f3871fdc AK |
8691 | dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev); |
8692 | return ret_val; | |
8693 | } | |
8694 | ||
8695 | for (id = super->devlist ; id; id = id->next) { | |
8696 | if (id->index == (unsigned)u->subdev) { | |
8697 | struct imsm_dev *dev = get_imsm_dev(super, u->subdev); | |
8698 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
8699 | int used_disks = imsm_num_data_members(dev, MAP_0); | |
8700 | unsigned long long blocks_per_member; | |
06fb291a | 8701 | unsigned long long num_data_stripes; |
f3871fdc AK |
8702 | |
8703 | /* calculate new size | |
8704 | */ | |
8705 | blocks_per_member = u->new_size / used_disks; | |
06fb291a PB |
8706 | num_data_stripes = blocks_per_member / |
8707 | map->blocks_per_strip; | |
8708 | num_data_stripes /= map->num_domains; | |
8709 | dprintf("(size: %llu, blocks per member: %llu, num_data_stipes: %llu)\n", | |
8710 | u->new_size, blocks_per_member, | |
8711 | num_data_stripes); | |
f3871fdc | 8712 | set_blocks_per_member(map, blocks_per_member); |
06fb291a | 8713 | set_num_data_stripes(map, num_data_stripes); |
f3871fdc AK |
8714 | imsm_set_array_size(dev, u->new_size); |
8715 | ||
8716 | ret_val = 1; | |
8717 | break; | |
8718 | } | |
8719 | } | |
8720 | ||
8721 | return ret_val; | |
8722 | } | |
8723 | ||
061d7da3 | 8724 | static int apply_update_activate_spare(struct imsm_update_activate_spare *u, |
ca9de185 | 8725 | struct intel_super *super, |
061d7da3 LO |
8726 | struct active_array *active_array) |
8727 | { | |
8728 | struct imsm_super *mpb = super->anchor; | |
8729 | struct imsm_dev *dev = get_imsm_dev(super, u->array); | |
238c0a71 | 8730 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
061d7da3 LO |
8731 | struct imsm_map *migr_map; |
8732 | struct active_array *a; | |
8733 | struct imsm_disk *disk; | |
8734 | __u8 to_state; | |
8735 | struct dl *dl; | |
8736 | unsigned int found; | |
8737 | int failed; | |
5961eeec | 8738 | int victim; |
061d7da3 | 8739 | int i; |
5961eeec | 8740 | int second_map_created = 0; |
061d7da3 | 8741 | |
5961eeec | 8742 | for (; u; u = u->next) { |
238c0a71 | 8743 | victim = get_imsm_disk_idx(dev, u->slot, MAP_X); |
061d7da3 | 8744 | |
5961eeec | 8745 | if (victim < 0) |
8746 | return 0; | |
061d7da3 | 8747 | |
5961eeec | 8748 | for (dl = super->disks; dl; dl = dl->next) |
8749 | if (dl == u->dl) | |
8750 | break; | |
061d7da3 | 8751 | |
5961eeec | 8752 | if (!dl) { |
7a862a02 | 8753 | pr_err("error: imsm_activate_spare passed an unknown disk (index: %d)\n", |
5961eeec | 8754 | u->dl->index); |
8755 | return 0; | |
8756 | } | |
061d7da3 | 8757 | |
5961eeec | 8758 | /* count failures (excluding rebuilds and the victim) |
8759 | * to determine map[0] state | |
8760 | */ | |
8761 | failed = 0; | |
8762 | for (i = 0; i < map->num_members; i++) { | |
8763 | if (i == u->slot) | |
8764 | continue; | |
8765 | disk = get_imsm_disk(super, | |
238c0a71 | 8766 | get_imsm_disk_idx(dev, i, MAP_X)); |
5961eeec | 8767 | if (!disk || is_failed(disk)) |
8768 | failed++; | |
8769 | } | |
061d7da3 | 8770 | |
5961eeec | 8771 | /* adding a pristine spare, assign a new index */ |
8772 | if (dl->index < 0) { | |
8773 | dl->index = super->anchor->num_disks; | |
8774 | super->anchor->num_disks++; | |
8775 | } | |
8776 | disk = &dl->disk; | |
8777 | disk->status |= CONFIGURED_DISK; | |
8778 | disk->status &= ~SPARE_DISK; | |
8779 | ||
8780 | /* mark rebuild */ | |
238c0a71 | 8781 | to_state = imsm_check_degraded(super, dev, failed, MAP_0); |
5961eeec | 8782 | if (!second_map_created) { |
8783 | second_map_created = 1; | |
8784 | map->map_state = IMSM_T_STATE_DEGRADED; | |
8785 | migrate(dev, super, to_state, MIGR_REBUILD); | |
8786 | } else | |
8787 | map->map_state = to_state; | |
238c0a71 | 8788 | migr_map = get_imsm_map(dev, MAP_1); |
5961eeec | 8789 | set_imsm_ord_tbl_ent(map, u->slot, dl->index); |
8790 | set_imsm_ord_tbl_ent(migr_map, u->slot, | |
8791 | dl->index | IMSM_ORD_REBUILD); | |
8792 | ||
8793 | /* update the family_num to mark a new container | |
8794 | * generation, being careful to record the existing | |
8795 | * family_num in orig_family_num to clean up after | |
8796 | * earlier mdadm versions that neglected to set it. | |
8797 | */ | |
8798 | if (mpb->orig_family_num == 0) | |
8799 | mpb->orig_family_num = mpb->family_num; | |
8800 | mpb->family_num += super->random; | |
8801 | ||
8802 | /* count arrays using the victim in the metadata */ | |
8803 | found = 0; | |
8804 | for (a = active_array; a ; a = a->next) { | |
8805 | dev = get_imsm_dev(super, a->info.container_member); | |
238c0a71 | 8806 | map = get_imsm_map(dev, MAP_0); |
061d7da3 | 8807 | |
5961eeec | 8808 | if (get_imsm_disk_slot(map, victim) >= 0) |
8809 | found++; | |
8810 | } | |
061d7da3 | 8811 | |
5961eeec | 8812 | /* delete the victim if it is no longer being |
8813 | * utilized anywhere | |
061d7da3 | 8814 | */ |
5961eeec | 8815 | if (!found) { |
8816 | struct dl **dlp; | |
061d7da3 | 8817 | |
5961eeec | 8818 | /* We know that 'manager' isn't touching anything, |
8819 | * so it is safe to delete | |
8820 | */ | |
8821 | for (dlp = &super->disks; *dlp; dlp = &(*dlp)->next) | |
061d7da3 LO |
8822 | if ((*dlp)->index == victim) |
8823 | break; | |
5961eeec | 8824 | |
8825 | /* victim may be on the missing list */ | |
8826 | if (!*dlp) | |
8827 | for (dlp = &super->missing; *dlp; | |
8828 | dlp = &(*dlp)->next) | |
8829 | if ((*dlp)->index == victim) | |
8830 | break; | |
8831 | imsm_delete(super, dlp, victim); | |
8832 | } | |
061d7da3 LO |
8833 | } |
8834 | ||
8835 | return 1; | |
8836 | } | |
a29911da | 8837 | |
2e5dc010 N |
8838 | static int apply_reshape_container_disks_update(struct imsm_update_reshape *u, |
8839 | struct intel_super *super, | |
8840 | void ***space_list) | |
8841 | { | |
8842 | struct dl *new_disk; | |
8843 | struct intel_dev *id; | |
8844 | int i; | |
8845 | int delta_disks = u->new_raid_disks - u->old_raid_disks; | |
ee4beede | 8846 | int disk_count = u->old_raid_disks; |
2e5dc010 N |
8847 | void **tofree = NULL; |
8848 | int devices_to_reshape = 1; | |
8849 | struct imsm_super *mpb = super->anchor; | |
8850 | int ret_val = 0; | |
d098291a | 8851 | unsigned int dev_id; |
2e5dc010 | 8852 | |
1ade5cc1 | 8853 | dprintf("(enter)\n"); |
2e5dc010 N |
8854 | |
8855 | /* enable spares to use in array */ | |
8856 | for (i = 0; i < delta_disks; i++) { | |
8857 | new_disk = get_disk_super(super, | |
8858 | major(u->new_disks[i]), | |
8859 | minor(u->new_disks[i])); | |
7a862a02 | 8860 | dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n", |
2e5dc010 N |
8861 | major(u->new_disks[i]), minor(u->new_disks[i]), |
8862 | new_disk, new_disk->index); | |
089f9d79 JS |
8863 | if (new_disk == NULL || |
8864 | (new_disk->index >= 0 && | |
8865 | new_disk->index < u->old_raid_disks)) | |
2e5dc010 | 8866 | goto update_reshape_exit; |
ee4beede | 8867 | new_disk->index = disk_count++; |
2e5dc010 N |
8868 | /* slot to fill in autolayout |
8869 | */ | |
8870 | new_disk->raiddisk = new_disk->index; | |
8871 | new_disk->disk.status |= | |
8872 | CONFIGURED_DISK; | |
8873 | new_disk->disk.status &= ~SPARE_DISK; | |
8874 | } | |
8875 | ||
ed7333bd AK |
8876 | dprintf("imsm: volume set mpb->num_raid_devs = %i\n", |
8877 | mpb->num_raid_devs); | |
2e5dc010 N |
8878 | /* manage changes in volume |
8879 | */ | |
d098291a | 8880 | for (dev_id = 0; dev_id < mpb->num_raid_devs; dev_id++) { |
2e5dc010 N |
8881 | void **sp = *space_list; |
8882 | struct imsm_dev *newdev; | |
8883 | struct imsm_map *newmap, *oldmap; | |
8884 | ||
d098291a AK |
8885 | for (id = super->devlist ; id; id = id->next) { |
8886 | if (id->index == dev_id) | |
8887 | break; | |
8888 | } | |
8889 | if (id == NULL) | |
8890 | break; | |
2e5dc010 N |
8891 | if (!sp) |
8892 | continue; | |
8893 | *space_list = *sp; | |
8894 | newdev = (void*)sp; | |
8895 | /* Copy the dev, but not (all of) the map */ | |
8896 | memcpy(newdev, id->dev, sizeof(*newdev)); | |
238c0a71 AK |
8897 | oldmap = get_imsm_map(id->dev, MAP_0); |
8898 | newmap = get_imsm_map(newdev, MAP_0); | |
2e5dc010 N |
8899 | /* Copy the current map */ |
8900 | memcpy(newmap, oldmap, sizeof_imsm_map(oldmap)); | |
8901 | /* update one device only | |
8902 | */ | |
8903 | if (devices_to_reshape) { | |
ed7333bd AK |
8904 | dprintf("imsm: modifying subdev: %i\n", |
8905 | id->index); | |
2e5dc010 N |
8906 | devices_to_reshape--; |
8907 | newdev->vol.migr_state = 1; | |
8908 | newdev->vol.curr_migr_unit = 0; | |
ea672ee1 | 8909 | set_migr_type(newdev, MIGR_GEN_MIGR); |
2e5dc010 N |
8910 | newmap->num_members = u->new_raid_disks; |
8911 | for (i = 0; i < delta_disks; i++) { | |
8912 | set_imsm_ord_tbl_ent(newmap, | |
8913 | u->old_raid_disks + i, | |
8914 | u->old_raid_disks + i); | |
8915 | } | |
8916 | /* New map is correct, now need to save old map | |
8917 | */ | |
238c0a71 | 8918 | newmap = get_imsm_map(newdev, MAP_1); |
2e5dc010 N |
8919 | memcpy(newmap, oldmap, sizeof_imsm_map(oldmap)); |
8920 | ||
f3871fdc | 8921 | imsm_set_array_size(newdev, -1); |
2e5dc010 N |
8922 | } |
8923 | ||
8924 | sp = (void **)id->dev; | |
8925 | id->dev = newdev; | |
8926 | *sp = tofree; | |
8927 | tofree = sp; | |
8e59f3d8 AK |
8928 | |
8929 | /* Clear migration record */ | |
8930 | memset(super->migr_rec, 0, sizeof(struct migr_record)); | |
2e5dc010 | 8931 | } |
819bc634 AK |
8932 | if (tofree) |
8933 | *space_list = tofree; | |
2e5dc010 N |
8934 | ret_val = 1; |
8935 | ||
8936 | update_reshape_exit: | |
8937 | ||
8938 | return ret_val; | |
8939 | } | |
8940 | ||
bb025c2f | 8941 | static int apply_takeover_update(struct imsm_update_takeover *u, |
8ca6df95 KW |
8942 | struct intel_super *super, |
8943 | void ***space_list) | |
bb025c2f KW |
8944 | { |
8945 | struct imsm_dev *dev = NULL; | |
8ca6df95 KW |
8946 | struct intel_dev *dv; |
8947 | struct imsm_dev *dev_new; | |
bb025c2f KW |
8948 | struct imsm_map *map; |
8949 | struct dl *dm, *du; | |
8ca6df95 | 8950 | int i; |
bb025c2f KW |
8951 | |
8952 | for (dv = super->devlist; dv; dv = dv->next) | |
8953 | if (dv->index == (unsigned int)u->subarray) { | |
8954 | dev = dv->dev; | |
8955 | break; | |
8956 | } | |
8957 | ||
8958 | if (dev == NULL) | |
8959 | return 0; | |
8960 | ||
238c0a71 | 8961 | map = get_imsm_map(dev, MAP_0); |
bb025c2f KW |
8962 | |
8963 | if (u->direction == R10_TO_R0) { | |
06fb291a PB |
8964 | unsigned long long num_data_stripes; |
8965 | ||
8966 | map->num_domains = 1; | |
8967 | num_data_stripes = blocks_per_member(map); | |
8968 | num_data_stripes /= map->blocks_per_strip; | |
8969 | num_data_stripes /= map->num_domains; | |
8970 | set_num_data_stripes(map, num_data_stripes); | |
8971 | ||
43d5ec18 | 8972 | /* Number of failed disks must be half of initial disk number */ |
3b451610 AK |
8973 | if (imsm_count_failed(super, dev, MAP_0) != |
8974 | (map->num_members / 2)) | |
43d5ec18 KW |
8975 | return 0; |
8976 | ||
bb025c2f KW |
8977 | /* iterate through devices to mark removed disks as spare */ |
8978 | for (dm = super->disks; dm; dm = dm->next) { | |
8979 | if (dm->disk.status & FAILED_DISK) { | |
8980 | int idx = dm->index; | |
8981 | /* update indexes on the disk list */ | |
8982 | /* FIXME this loop-with-the-loop looks wrong, I'm not convinced | |
8983 | the index values will end up being correct.... NB */ | |
8984 | for (du = super->disks; du; du = du->next) | |
8985 | if (du->index > idx) | |
8986 | du->index--; | |
8987 | /* mark as spare disk */ | |
a8619d23 | 8988 | mark_spare(dm); |
bb025c2f KW |
8989 | } |
8990 | } | |
bb025c2f KW |
8991 | /* update map */ |
8992 | map->num_members = map->num_members / 2; | |
8993 | map->map_state = IMSM_T_STATE_NORMAL; | |
8994 | map->num_domains = 1; | |
8995 | map->raid_level = 0; | |
8996 | map->failed_disk_num = -1; | |
8997 | } | |
8998 | ||
8ca6df95 KW |
8999 | if (u->direction == R0_TO_R10) { |
9000 | void **space; | |
9001 | /* update slots in current disk list */ | |
9002 | for (dm = super->disks; dm; dm = dm->next) { | |
9003 | if (dm->index >= 0) | |
9004 | dm->index *= 2; | |
9005 | } | |
9006 | /* create new *missing* disks */ | |
9007 | for (i = 0; i < map->num_members; i++) { | |
9008 | space = *space_list; | |
9009 | if (!space) | |
9010 | continue; | |
9011 | *space_list = *space; | |
9012 | du = (void *)space; | |
9013 | memcpy(du, super->disks, sizeof(*du)); | |
8ca6df95 KW |
9014 | du->fd = -1; |
9015 | du->minor = 0; | |
9016 | du->major = 0; | |
9017 | du->index = (i * 2) + 1; | |
9018 | sprintf((char *)du->disk.serial, | |
9019 | " MISSING_%d", du->index); | |
9020 | sprintf((char *)du->serial, | |
9021 | "MISSING_%d", du->index); | |
9022 | du->next = super->missing; | |
9023 | super->missing = du; | |
9024 | } | |
9025 | /* create new dev and map */ | |
9026 | space = *space_list; | |
9027 | if (!space) | |
9028 | return 0; | |
9029 | *space_list = *space; | |
9030 | dev_new = (void *)space; | |
9031 | memcpy(dev_new, dev, sizeof(*dev)); | |
9032 | /* update new map */ | |
238c0a71 | 9033 | map = get_imsm_map(dev_new, MAP_0); |
8ca6df95 | 9034 | map->num_members = map->num_members * 2; |
1a2487c2 | 9035 | map->map_state = IMSM_T_STATE_DEGRADED; |
8ca6df95 KW |
9036 | map->num_domains = 2; |
9037 | map->raid_level = 1; | |
9038 | /* replace dev<->dev_new */ | |
9039 | dv->dev = dev_new; | |
9040 | } | |
bb025c2f KW |
9041 | /* update disk order table */ |
9042 | for (du = super->disks; du; du = du->next) | |
9043 | if (du->index >= 0) | |
9044 | set_imsm_ord_tbl_ent(map, du->index, du->index); | |
8ca6df95 | 9045 | for (du = super->missing; du; du = du->next) |
1a2487c2 KW |
9046 | if (du->index >= 0) { |
9047 | set_imsm_ord_tbl_ent(map, du->index, du->index); | |
4c9e8c1e | 9048 | mark_missing(super, dv->dev, &du->disk, du->index); |
1a2487c2 | 9049 | } |
bb025c2f KW |
9050 | |
9051 | return 1; | |
9052 | } | |
9053 | ||
e8319a19 DW |
9054 | static void imsm_process_update(struct supertype *st, |
9055 | struct metadata_update *update) | |
9056 | { | |
9057 | /** | |
9058 | * crack open the metadata_update envelope to find the update record | |
9059 | * update can be one of: | |
d195167d AK |
9060 | * update_reshape_container_disks - all the arrays in the container |
9061 | * are being reshaped to have more devices. We need to mark | |
9062 | * the arrays for general migration and convert selected spares | |
9063 | * into active devices. | |
9064 | * update_activate_spare - a spare device has replaced a failed | |
1011e834 N |
9065 | * device in an array, update the disk_ord_tbl. If this disk is |
9066 | * present in all member arrays then also clear the SPARE_DISK | |
9067 | * flag | |
d195167d AK |
9068 | * update_create_array |
9069 | * update_kill_array | |
9070 | * update_rename_array | |
9071 | * update_add_remove_disk | |
e8319a19 DW |
9072 | */ |
9073 | struct intel_super *super = st->sb; | |
4d7b1503 | 9074 | struct imsm_super *mpb; |
e8319a19 DW |
9075 | enum imsm_update_type type = *(enum imsm_update_type *) update->buf; |
9076 | ||
4d7b1503 DW |
9077 | /* update requires a larger buf but the allocation failed */ |
9078 | if (super->next_len && !super->next_buf) { | |
9079 | super->next_len = 0; | |
9080 | return; | |
9081 | } | |
9082 | ||
9083 | if (super->next_buf) { | |
9084 | memcpy(super->next_buf, super->buf, super->len); | |
9085 | free(super->buf); | |
9086 | super->len = super->next_len; | |
9087 | super->buf = super->next_buf; | |
9088 | ||
9089 | super->next_len = 0; | |
9090 | super->next_buf = NULL; | |
9091 | } | |
9092 | ||
9093 | mpb = super->anchor; | |
9094 | ||
e8319a19 | 9095 | switch (type) { |
0ec5d470 AK |
9096 | case update_general_migration_checkpoint: { |
9097 | struct intel_dev *id; | |
9098 | struct imsm_update_general_migration_checkpoint *u = | |
9099 | (void *)update->buf; | |
9100 | ||
1ade5cc1 | 9101 | dprintf("called for update_general_migration_checkpoint\n"); |
0ec5d470 AK |
9102 | |
9103 | /* find device under general migration */ | |
9104 | for (id = super->devlist ; id; id = id->next) { | |
9105 | if (is_gen_migration(id->dev)) { | |
9106 | id->dev->vol.curr_migr_unit = | |
9107 | __cpu_to_le32(u->curr_migr_unit); | |
9108 | super->updates_pending++; | |
9109 | } | |
9110 | } | |
9111 | break; | |
9112 | } | |
bb025c2f KW |
9113 | case update_takeover: { |
9114 | struct imsm_update_takeover *u = (void *)update->buf; | |
1a2487c2 KW |
9115 | if (apply_takeover_update(u, super, &update->space_list)) { |
9116 | imsm_update_version_info(super); | |
bb025c2f | 9117 | super->updates_pending++; |
1a2487c2 | 9118 | } |
bb025c2f KW |
9119 | break; |
9120 | } | |
9121 | ||
78b10e66 | 9122 | case update_reshape_container_disks: { |
d195167d | 9123 | struct imsm_update_reshape *u = (void *)update->buf; |
2e5dc010 N |
9124 | if (apply_reshape_container_disks_update( |
9125 | u, super, &update->space_list)) | |
9126 | super->updates_pending++; | |
78b10e66 N |
9127 | break; |
9128 | } | |
48c5303a | 9129 | case update_reshape_migration: { |
a29911da PC |
9130 | struct imsm_update_reshape_migration *u = (void *)update->buf; |
9131 | if (apply_reshape_migration_update( | |
9132 | u, super, &update->space_list)) | |
9133 | super->updates_pending++; | |
48c5303a PC |
9134 | break; |
9135 | } | |
f3871fdc AK |
9136 | case update_size_change: { |
9137 | struct imsm_update_size_change *u = (void *)update->buf; | |
9138 | if (apply_size_change_update(u, super)) | |
9139 | super->updates_pending++; | |
9140 | break; | |
9141 | } | |
e8319a19 | 9142 | case update_activate_spare: { |
1011e834 | 9143 | struct imsm_update_activate_spare *u = (void *) update->buf; |
061d7da3 LO |
9144 | if (apply_update_activate_spare(u, super, st->arrays)) |
9145 | super->updates_pending++; | |
8273f55e DW |
9146 | break; |
9147 | } | |
9148 | case update_create_array: { | |
9149 | /* someone wants to create a new array, we need to be aware of | |
9150 | * a few races/collisions: | |
9151 | * 1/ 'Create' called by two separate instances of mdadm | |
9152 | * 2/ 'Create' versus 'activate_spare': mdadm has chosen | |
9153 | * devices that have since been assimilated via | |
9154 | * activate_spare. | |
9155 | * In the event this update can not be carried out mdadm will | |
9156 | * (FIX ME) notice that its update did not take hold. | |
9157 | */ | |
9158 | struct imsm_update_create_array *u = (void *) update->buf; | |
ba2de7ba | 9159 | struct intel_dev *dv; |
8273f55e DW |
9160 | struct imsm_dev *dev; |
9161 | struct imsm_map *map, *new_map; | |
9162 | unsigned long long start, end; | |
9163 | unsigned long long new_start, new_end; | |
9164 | int i; | |
54c2c1ea DW |
9165 | struct disk_info *inf; |
9166 | struct dl *dl; | |
8273f55e DW |
9167 | |
9168 | /* handle racing creates: first come first serve */ | |
9169 | if (u->dev_idx < mpb->num_raid_devs) { | |
1ade5cc1 | 9170 | dprintf("subarray %d already defined\n", u->dev_idx); |
ba2de7ba | 9171 | goto create_error; |
8273f55e DW |
9172 | } |
9173 | ||
9174 | /* check update is next in sequence */ | |
9175 | if (u->dev_idx != mpb->num_raid_devs) { | |
1ade5cc1 N |
9176 | dprintf("can not create array %d expected index %d\n", |
9177 | u->dev_idx, mpb->num_raid_devs); | |
ba2de7ba | 9178 | goto create_error; |
8273f55e DW |
9179 | } |
9180 | ||
238c0a71 | 9181 | new_map = get_imsm_map(&u->dev, MAP_0); |
5551b113 CA |
9182 | new_start = pba_of_lba0(new_map); |
9183 | new_end = new_start + blocks_per_member(new_map); | |
54c2c1ea | 9184 | inf = get_disk_info(u); |
8273f55e DW |
9185 | |
9186 | /* handle activate_spare versus create race: | |
9187 | * check to make sure that overlapping arrays do not include | |
9188 | * overalpping disks | |
9189 | */ | |
9190 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
949c47a0 | 9191 | dev = get_imsm_dev(super, i); |
238c0a71 | 9192 | map = get_imsm_map(dev, MAP_0); |
5551b113 CA |
9193 | start = pba_of_lba0(map); |
9194 | end = start + blocks_per_member(map); | |
8273f55e DW |
9195 | if ((new_start >= start && new_start <= end) || |
9196 | (start >= new_start && start <= new_end)) | |
54c2c1ea DW |
9197 | /* overlap */; |
9198 | else | |
9199 | continue; | |
9200 | ||
9201 | if (disks_overlap(super, i, u)) { | |
1ade5cc1 | 9202 | dprintf("arrays overlap\n"); |
ba2de7ba | 9203 | goto create_error; |
8273f55e DW |
9204 | } |
9205 | } | |
8273f55e | 9206 | |
949c47a0 DW |
9207 | /* check that prepare update was successful */ |
9208 | if (!update->space) { | |
1ade5cc1 | 9209 | dprintf("prepare update failed\n"); |
ba2de7ba | 9210 | goto create_error; |
949c47a0 DW |
9211 | } |
9212 | ||
54c2c1ea DW |
9213 | /* check that all disks are still active before committing |
9214 | * changes. FIXME: could we instead handle this by creating a | |
9215 | * degraded array? That's probably not what the user expects, | |
9216 | * so better to drop this update on the floor. | |
9217 | */ | |
9218 | for (i = 0; i < new_map->num_members; i++) { | |
9219 | dl = serial_to_dl(inf[i].serial, super); | |
9220 | if (!dl) { | |
1ade5cc1 | 9221 | dprintf("disk disappeared\n"); |
ba2de7ba | 9222 | goto create_error; |
54c2c1ea | 9223 | } |
949c47a0 DW |
9224 | } |
9225 | ||
8273f55e | 9226 | super->updates_pending++; |
54c2c1ea DW |
9227 | |
9228 | /* convert spares to members and fixup ord_tbl */ | |
9229 | for (i = 0; i < new_map->num_members; i++) { | |
9230 | dl = serial_to_dl(inf[i].serial, super); | |
9231 | if (dl->index == -1) { | |
9232 | dl->index = mpb->num_disks; | |
9233 | mpb->num_disks++; | |
9234 | dl->disk.status |= CONFIGURED_DISK; | |
9235 | dl->disk.status &= ~SPARE_DISK; | |
9236 | } | |
9237 | set_imsm_ord_tbl_ent(new_map, i, dl->index); | |
9238 | } | |
9239 | ||
ba2de7ba DW |
9240 | dv = update->space; |
9241 | dev = dv->dev; | |
949c47a0 DW |
9242 | update->space = NULL; |
9243 | imsm_copy_dev(dev, &u->dev); | |
ba2de7ba DW |
9244 | dv->index = u->dev_idx; |
9245 | dv->next = super->devlist; | |
9246 | super->devlist = dv; | |
8273f55e | 9247 | mpb->num_raid_devs++; |
8273f55e | 9248 | |
4d1313e9 | 9249 | imsm_update_version_info(super); |
8273f55e | 9250 | break; |
ba2de7ba DW |
9251 | create_error: |
9252 | /* mdmon knows how to release update->space, but not | |
9253 | * ((struct intel_dev *) update->space)->dev | |
9254 | */ | |
9255 | if (update->space) { | |
9256 | dv = update->space; | |
9257 | free(dv->dev); | |
9258 | } | |
8273f55e | 9259 | break; |
e8319a19 | 9260 | } |
33414a01 DW |
9261 | case update_kill_array: { |
9262 | struct imsm_update_kill_array *u = (void *) update->buf; | |
9263 | int victim = u->dev_idx; | |
9264 | struct active_array *a; | |
9265 | struct intel_dev **dp; | |
9266 | struct imsm_dev *dev; | |
9267 | ||
9268 | /* sanity check that we are not affecting the uuid of | |
9269 | * active arrays, or deleting an active array | |
9270 | * | |
9271 | * FIXME when immutable ids are available, but note that | |
9272 | * we'll also need to fixup the invalidated/active | |
9273 | * subarray indexes in mdstat | |
9274 | */ | |
9275 | for (a = st->arrays; a; a = a->next) | |
9276 | if (a->info.container_member >= victim) | |
9277 | break; | |
9278 | /* by definition if mdmon is running at least one array | |
9279 | * is active in the container, so checking | |
9280 | * mpb->num_raid_devs is just extra paranoia | |
9281 | */ | |
9282 | dev = get_imsm_dev(super, victim); | |
9283 | if (a || !dev || mpb->num_raid_devs == 1) { | |
9284 | dprintf("failed to delete subarray-%d\n", victim); | |
9285 | break; | |
9286 | } | |
9287 | ||
9288 | for (dp = &super->devlist; *dp;) | |
f21e18ca | 9289 | if ((*dp)->index == (unsigned)super->current_vol) { |
33414a01 DW |
9290 | *dp = (*dp)->next; |
9291 | } else { | |
f21e18ca | 9292 | if ((*dp)->index > (unsigned)victim) |
33414a01 DW |
9293 | (*dp)->index--; |
9294 | dp = &(*dp)->next; | |
9295 | } | |
9296 | mpb->num_raid_devs--; | |
9297 | super->updates_pending++; | |
9298 | break; | |
9299 | } | |
aa534678 DW |
9300 | case update_rename_array: { |
9301 | struct imsm_update_rename_array *u = (void *) update->buf; | |
9302 | char name[MAX_RAID_SERIAL_LEN+1]; | |
9303 | int target = u->dev_idx; | |
9304 | struct active_array *a; | |
9305 | struct imsm_dev *dev; | |
9306 | ||
9307 | /* sanity check that we are not affecting the uuid of | |
9308 | * an active array | |
9309 | */ | |
9310 | snprintf(name, MAX_RAID_SERIAL_LEN, "%s", (char *) u->name); | |
9311 | name[MAX_RAID_SERIAL_LEN] = '\0'; | |
9312 | for (a = st->arrays; a; a = a->next) | |
9313 | if (a->info.container_member == target) | |
9314 | break; | |
9315 | dev = get_imsm_dev(super, u->dev_idx); | |
9316 | if (a || !dev || !check_name(super, name, 1)) { | |
9317 | dprintf("failed to rename subarray-%d\n", target); | |
9318 | break; | |
9319 | } | |
9320 | ||
cdbe98cd | 9321 | snprintf((char *) dev->volume, MAX_RAID_SERIAL_LEN, "%s", name); |
aa534678 DW |
9322 | super->updates_pending++; |
9323 | break; | |
9324 | } | |
1a64be56 | 9325 | case update_add_remove_disk: { |
43dad3d6 | 9326 | /* we may be able to repair some arrays if disks are |
095b8088 | 9327 | * being added, check the status of add_remove_disk |
1a64be56 LM |
9328 | * if discs has been added. |
9329 | */ | |
9330 | if (add_remove_disk_update(super)) { | |
43dad3d6 | 9331 | struct active_array *a; |
072b727f DW |
9332 | |
9333 | super->updates_pending++; | |
1a64be56 | 9334 | for (a = st->arrays; a; a = a->next) |
43dad3d6 DW |
9335 | a->check_degraded = 1; |
9336 | } | |
43dad3d6 | 9337 | break; |
e8319a19 | 9338 | } |
bbab0940 TM |
9339 | case update_prealloc_badblocks_mem: |
9340 | break; | |
1a64be56 | 9341 | default: |
7a862a02 | 9342 | pr_err("error: unsuported process update type:(type: %d)\n", type); |
1a64be56 | 9343 | } |
e8319a19 | 9344 | } |
88758e9d | 9345 | |
bc0b9d34 PC |
9346 | static struct mdinfo *get_spares_for_grow(struct supertype *st); |
9347 | ||
5fe6f031 N |
9348 | static int imsm_prepare_update(struct supertype *st, |
9349 | struct metadata_update *update) | |
8273f55e | 9350 | { |
949c47a0 | 9351 | /** |
4d7b1503 DW |
9352 | * Allocate space to hold new disk entries, raid-device entries or a new |
9353 | * mpb if necessary. The manager synchronously waits for updates to | |
9354 | * complete in the monitor, so new mpb buffers allocated here can be | |
9355 | * integrated by the monitor thread without worrying about live pointers | |
9356 | * in the manager thread. | |
8273f55e | 9357 | */ |
095b8088 | 9358 | enum imsm_update_type type; |
4d7b1503 | 9359 | struct intel_super *super = st->sb; |
f36a9ecd | 9360 | unsigned int sector_size = super->sector_size; |
4d7b1503 DW |
9361 | struct imsm_super *mpb = super->anchor; |
9362 | size_t buf_len; | |
9363 | size_t len = 0; | |
949c47a0 | 9364 | |
095b8088 N |
9365 | if (update->len < (int)sizeof(type)) |
9366 | return 0; | |
9367 | ||
9368 | type = *(enum imsm_update_type *) update->buf; | |
9369 | ||
949c47a0 | 9370 | switch (type) { |
0ec5d470 | 9371 | case update_general_migration_checkpoint: |
095b8088 N |
9372 | if (update->len < (int)sizeof(struct imsm_update_general_migration_checkpoint)) |
9373 | return 0; | |
1ade5cc1 | 9374 | dprintf("called for update_general_migration_checkpoint\n"); |
0ec5d470 | 9375 | break; |
abedf5fc KW |
9376 | case update_takeover: { |
9377 | struct imsm_update_takeover *u = (void *)update->buf; | |
095b8088 N |
9378 | if (update->len < (int)sizeof(*u)) |
9379 | return 0; | |
abedf5fc KW |
9380 | if (u->direction == R0_TO_R10) { |
9381 | void **tail = (void **)&update->space_list; | |
9382 | struct imsm_dev *dev = get_imsm_dev(super, u->subarray); | |
238c0a71 | 9383 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
abedf5fc KW |
9384 | int num_members = map->num_members; |
9385 | void *space; | |
9386 | int size, i; | |
abedf5fc KW |
9387 | /* allocate memory for added disks */ |
9388 | for (i = 0; i < num_members; i++) { | |
9389 | size = sizeof(struct dl); | |
503975b9 | 9390 | space = xmalloc(size); |
abedf5fc KW |
9391 | *tail = space; |
9392 | tail = space; | |
9393 | *tail = NULL; | |
9394 | } | |
9395 | /* allocate memory for new device */ | |
9396 | size = sizeof_imsm_dev(super->devlist->dev, 0) + | |
9397 | (num_members * sizeof(__u32)); | |
503975b9 N |
9398 | space = xmalloc(size); |
9399 | *tail = space; | |
9400 | tail = space; | |
9401 | *tail = NULL; | |
9402 | len = disks_to_mpb_size(num_members * 2); | |
abedf5fc KW |
9403 | } |
9404 | ||
9405 | break; | |
9406 | } | |
78b10e66 | 9407 | case update_reshape_container_disks: { |
d195167d AK |
9408 | /* Every raid device in the container is about to |
9409 | * gain some more devices, and we will enter a | |
9410 | * reconfiguration. | |
9411 | * So each 'imsm_map' will be bigger, and the imsm_vol | |
9412 | * will now hold 2 of them. | |
9413 | * Thus we need new 'struct imsm_dev' allocations sized | |
9414 | * as sizeof_imsm_dev but with more devices in both maps. | |
9415 | */ | |
9416 | struct imsm_update_reshape *u = (void *)update->buf; | |
9417 | struct intel_dev *dl; | |
9418 | void **space_tail = (void**)&update->space_list; | |
9419 | ||
095b8088 N |
9420 | if (update->len < (int)sizeof(*u)) |
9421 | return 0; | |
9422 | ||
1ade5cc1 | 9423 | dprintf("for update_reshape\n"); |
d195167d AK |
9424 | |
9425 | for (dl = super->devlist; dl; dl = dl->next) { | |
9426 | int size = sizeof_imsm_dev(dl->dev, 1); | |
9427 | void *s; | |
d677e0b8 AK |
9428 | if (u->new_raid_disks > u->old_raid_disks) |
9429 | size += sizeof(__u32)*2* | |
9430 | (u->new_raid_disks - u->old_raid_disks); | |
503975b9 | 9431 | s = xmalloc(size); |
d195167d AK |
9432 | *space_tail = s; |
9433 | space_tail = s; | |
9434 | *space_tail = NULL; | |
9435 | } | |
9436 | ||
9437 | len = disks_to_mpb_size(u->new_raid_disks); | |
9438 | dprintf("New anchor length is %llu\n", (unsigned long long)len); | |
78b10e66 N |
9439 | break; |
9440 | } | |
48c5303a | 9441 | case update_reshape_migration: { |
bc0b9d34 PC |
9442 | /* for migration level 0->5 we need to add disks |
9443 | * so the same as for container operation we will copy | |
9444 | * device to the bigger location. | |
9445 | * in memory prepared device and new disk area are prepared | |
9446 | * for usage in process update | |
9447 | */ | |
9448 | struct imsm_update_reshape_migration *u = (void *)update->buf; | |
9449 | struct intel_dev *id; | |
9450 | void **space_tail = (void **)&update->space_list; | |
9451 | int size; | |
9452 | void *s; | |
9453 | int current_level = -1; | |
9454 | ||
095b8088 N |
9455 | if (update->len < (int)sizeof(*u)) |
9456 | return 0; | |
9457 | ||
1ade5cc1 | 9458 | dprintf("for update_reshape\n"); |
bc0b9d34 PC |
9459 | |
9460 | /* add space for bigger array in update | |
9461 | */ | |
9462 | for (id = super->devlist; id; id = id->next) { | |
9463 | if (id->index == (unsigned)u->subdev) { | |
9464 | size = sizeof_imsm_dev(id->dev, 1); | |
9465 | if (u->new_raid_disks > u->old_raid_disks) | |
9466 | size += sizeof(__u32)*2* | |
9467 | (u->new_raid_disks - u->old_raid_disks); | |
503975b9 | 9468 | s = xmalloc(size); |
bc0b9d34 PC |
9469 | *space_tail = s; |
9470 | space_tail = s; | |
9471 | *space_tail = NULL; | |
9472 | break; | |
9473 | } | |
9474 | } | |
9475 | if (update->space_list == NULL) | |
9476 | break; | |
9477 | ||
9478 | /* add space for disk in update | |
9479 | */ | |
9480 | size = sizeof(struct dl); | |
503975b9 | 9481 | s = xmalloc(size); |
bc0b9d34 PC |
9482 | *space_tail = s; |
9483 | space_tail = s; | |
9484 | *space_tail = NULL; | |
9485 | ||
9486 | /* add spare device to update | |
9487 | */ | |
9488 | for (id = super->devlist ; id; id = id->next) | |
9489 | if (id->index == (unsigned)u->subdev) { | |
9490 | struct imsm_dev *dev; | |
9491 | struct imsm_map *map; | |
9492 | ||
9493 | dev = get_imsm_dev(super, u->subdev); | |
238c0a71 | 9494 | map = get_imsm_map(dev, MAP_0); |
bc0b9d34 PC |
9495 | current_level = map->raid_level; |
9496 | break; | |
9497 | } | |
089f9d79 | 9498 | if (u->new_level == 5 && u->new_level != current_level) { |
bc0b9d34 PC |
9499 | struct mdinfo *spares; |
9500 | ||
9501 | spares = get_spares_for_grow(st); | |
9502 | if (spares) { | |
9503 | struct dl *dl; | |
9504 | struct mdinfo *dev; | |
9505 | ||
9506 | dev = spares->devs; | |
9507 | if (dev) { | |
9508 | u->new_disks[0] = | |
9509 | makedev(dev->disk.major, | |
9510 | dev->disk.minor); | |
9511 | dl = get_disk_super(super, | |
9512 | dev->disk.major, | |
9513 | dev->disk.minor); | |
9514 | dl->index = u->old_raid_disks; | |
9515 | dev = dev->next; | |
9516 | } | |
9517 | sysfs_free(spares); | |
9518 | } | |
9519 | } | |
9520 | len = disks_to_mpb_size(u->new_raid_disks); | |
9521 | dprintf("New anchor length is %llu\n", (unsigned long long)len); | |
48c5303a PC |
9522 | break; |
9523 | } | |
f3871fdc | 9524 | case update_size_change: { |
095b8088 N |
9525 | if (update->len < (int)sizeof(struct imsm_update_size_change)) |
9526 | return 0; | |
9527 | break; | |
9528 | } | |
9529 | case update_activate_spare: { | |
9530 | if (update->len < (int)sizeof(struct imsm_update_activate_spare)) | |
9531 | return 0; | |
f3871fdc AK |
9532 | break; |
9533 | } | |
949c47a0 DW |
9534 | case update_create_array: { |
9535 | struct imsm_update_create_array *u = (void *) update->buf; | |
ba2de7ba | 9536 | struct intel_dev *dv; |
54c2c1ea | 9537 | struct imsm_dev *dev = &u->dev; |
238c0a71 | 9538 | struct imsm_map *map = get_imsm_map(dev, MAP_0); |
54c2c1ea DW |
9539 | struct dl *dl; |
9540 | struct disk_info *inf; | |
9541 | int i; | |
9542 | int activate = 0; | |
949c47a0 | 9543 | |
095b8088 N |
9544 | if (update->len < (int)sizeof(*u)) |
9545 | return 0; | |
9546 | ||
54c2c1ea DW |
9547 | inf = get_disk_info(u); |
9548 | len = sizeof_imsm_dev(dev, 1); | |
ba2de7ba | 9549 | /* allocate a new super->devlist entry */ |
503975b9 N |
9550 | dv = xmalloc(sizeof(*dv)); |
9551 | dv->dev = xmalloc(len); | |
9552 | update->space = dv; | |
949c47a0 | 9553 | |
54c2c1ea DW |
9554 | /* count how many spares will be converted to members */ |
9555 | for (i = 0; i < map->num_members; i++) { | |
9556 | dl = serial_to_dl(inf[i].serial, super); | |
9557 | if (!dl) { | |
9558 | /* hmm maybe it failed?, nothing we can do about | |
9559 | * it here | |
9560 | */ | |
9561 | continue; | |
9562 | } | |
9563 | if (count_memberships(dl, super) == 0) | |
9564 | activate++; | |
9565 | } | |
9566 | len += activate * sizeof(struct imsm_disk); | |
949c47a0 | 9567 | break; |
095b8088 N |
9568 | } |
9569 | case update_kill_array: { | |
9570 | if (update->len < (int)sizeof(struct imsm_update_kill_array)) | |
9571 | return 0; | |
949c47a0 DW |
9572 | break; |
9573 | } | |
095b8088 N |
9574 | case update_rename_array: { |
9575 | if (update->len < (int)sizeof(struct imsm_update_rename_array)) | |
9576 | return 0; | |
9577 | break; | |
9578 | } | |
9579 | case update_add_remove_disk: | |
9580 | /* no update->len needed */ | |
9581 | break; | |
bbab0940 TM |
9582 | case update_prealloc_badblocks_mem: |
9583 | super->extra_space += sizeof(struct bbm_log) - | |
9584 | get_imsm_bbm_log_size(super->bbm_log); | |
9585 | break; | |
095b8088 N |
9586 | default: |
9587 | return 0; | |
949c47a0 | 9588 | } |
8273f55e | 9589 | |
4d7b1503 DW |
9590 | /* check if we need a larger metadata buffer */ |
9591 | if (super->next_buf) | |
9592 | buf_len = super->next_len; | |
9593 | else | |
9594 | buf_len = super->len; | |
9595 | ||
bbab0940 | 9596 | if (__le32_to_cpu(mpb->mpb_size) + super->extra_space + len > buf_len) { |
4d7b1503 DW |
9597 | /* ok we need a larger buf than what is currently allocated |
9598 | * if this allocation fails process_update will notice that | |
9599 | * ->next_len is set and ->next_buf is NULL | |
9600 | */ | |
bbab0940 TM |
9601 | buf_len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + |
9602 | super->extra_space + len, sector_size); | |
4d7b1503 DW |
9603 | if (super->next_buf) |
9604 | free(super->next_buf); | |
9605 | ||
9606 | super->next_len = buf_len; | |
f36a9ecd | 9607 | if (posix_memalign(&super->next_buf, sector_size, buf_len) == 0) |
1f45a8ad DW |
9608 | memset(super->next_buf, 0, buf_len); |
9609 | else | |
4d7b1503 DW |
9610 | super->next_buf = NULL; |
9611 | } | |
5fe6f031 | 9612 | return 1; |
8273f55e DW |
9613 | } |
9614 | ||
ae6aad82 | 9615 | /* must be called while manager is quiesced */ |
f21e18ca | 9616 | static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index) |
ae6aad82 DW |
9617 | { |
9618 | struct imsm_super *mpb = super->anchor; | |
ae6aad82 DW |
9619 | struct dl *iter; |
9620 | struct imsm_dev *dev; | |
9621 | struct imsm_map *map; | |
4c9e8c1e | 9622 | unsigned int i, j, num_members; |
24565c9a | 9623 | __u32 ord; |
4c9e8c1e | 9624 | struct bbm_log *log = super->bbm_log; |
ae6aad82 | 9625 | |
1ade5cc1 | 9626 | dprintf("deleting device[%d] from imsm_super\n", index); |
ae6aad82 DW |
9627 | |
9628 | /* shift all indexes down one */ | |
9629 | for (iter = super->disks; iter; iter = iter->next) | |
f21e18ca | 9630 | if (iter->index > (int)index) |
ae6aad82 | 9631 | iter->index--; |
47ee5a45 | 9632 | for (iter = super->missing; iter; iter = iter->next) |
f21e18ca | 9633 | if (iter->index > (int)index) |
47ee5a45 | 9634 | iter->index--; |
ae6aad82 DW |
9635 | |
9636 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
9637 | dev = get_imsm_dev(super, i); | |
238c0a71 | 9638 | map = get_imsm_map(dev, MAP_0); |
24565c9a DW |
9639 | num_members = map->num_members; |
9640 | for (j = 0; j < num_members; j++) { | |
9641 | /* update ord entries being careful not to propagate | |
9642 | * ord-flags to the first map | |
9643 | */ | |
238c0a71 | 9644 | ord = get_imsm_ord_tbl_ent(dev, j, MAP_X); |
ae6aad82 | 9645 | |
24565c9a DW |
9646 | if (ord_to_idx(ord) <= index) |
9647 | continue; | |
ae6aad82 | 9648 | |
238c0a71 | 9649 | map = get_imsm_map(dev, MAP_0); |
24565c9a | 9650 | set_imsm_ord_tbl_ent(map, j, ord_to_idx(ord - 1)); |
238c0a71 | 9651 | map = get_imsm_map(dev, MAP_1); |
24565c9a DW |
9652 | if (map) |
9653 | set_imsm_ord_tbl_ent(map, j, ord - 1); | |
ae6aad82 DW |
9654 | } |
9655 | } | |
9656 | ||
4c9e8c1e TM |
9657 | for (i = 0; i < log->entry_count; i++) { |
9658 | struct bbm_log_entry *entry = &log->marked_block_entries[i]; | |
9659 | ||
9660 | if (entry->disk_ordinal <= index) | |
9661 | continue; | |
9662 | entry->disk_ordinal--; | |
9663 | } | |
9664 | ||
ae6aad82 DW |
9665 | mpb->num_disks--; |
9666 | super->updates_pending++; | |
24565c9a DW |
9667 | if (*dlp) { |
9668 | struct dl *dl = *dlp; | |
9669 | ||
9670 | *dlp = (*dlp)->next; | |
9671 | __free_imsm_disk(dl); | |
9672 | } | |
ae6aad82 | 9673 | } |
9e2d750d | 9674 | #endif /* MDASSEMBLE */ |
9a717282 AK |
9675 | |
9676 | static void close_targets(int *targets, int new_disks) | |
9677 | { | |
9678 | int i; | |
9679 | ||
9680 | if (!targets) | |
9681 | return; | |
9682 | ||
9683 | for (i = 0; i < new_disks; i++) { | |
9684 | if (targets[i] >= 0) { | |
9685 | close(targets[i]); | |
9686 | targets[i] = -1; | |
9687 | } | |
9688 | } | |
9689 | } | |
9690 | ||
9691 | static int imsm_get_allowed_degradation(int level, int raid_disks, | |
9692 | struct intel_super *super, | |
9693 | struct imsm_dev *dev) | |
9694 | { | |
9695 | switch (level) { | |
bf5cf7c7 | 9696 | case 1: |
9a717282 AK |
9697 | case 10:{ |
9698 | int ret_val = 0; | |
9699 | struct imsm_map *map; | |
9700 | int i; | |
9701 | ||
9702 | ret_val = raid_disks/2; | |
9703 | /* check map if all disks pairs not failed | |
9704 | * in both maps | |
9705 | */ | |
238c0a71 | 9706 | map = get_imsm_map(dev, MAP_0); |
9a717282 AK |
9707 | for (i = 0; i < ret_val; i++) { |
9708 | int degradation = 0; | |
9709 | if (get_imsm_disk(super, i) == NULL) | |
9710 | degradation++; | |
9711 | if (get_imsm_disk(super, i + 1) == NULL) | |
9712 | degradation++; | |
9713 | if (degradation == 2) | |
9714 | return 0; | |
9715 | } | |
238c0a71 | 9716 | map = get_imsm_map(dev, MAP_1); |
9a717282 AK |
9717 | /* if there is no second map |
9718 | * result can be returned | |
9719 | */ | |
9720 | if (map == NULL) | |
9721 | return ret_val; | |
9722 | /* check degradation in second map | |
9723 | */ | |
9724 | for (i = 0; i < ret_val; i++) { | |
9725 | int degradation = 0; | |
9726 | if (get_imsm_disk(super, i) == NULL) | |
9727 | degradation++; | |
9728 | if (get_imsm_disk(super, i + 1) == NULL) | |
9729 | degradation++; | |
9730 | if (degradation == 2) | |
9731 | return 0; | |
9732 | } | |
9733 | return ret_val; | |
9734 | } | |
9735 | case 5: | |
9736 | return 1; | |
9737 | case 6: | |
9738 | return 2; | |
9739 | default: | |
9740 | return 0; | |
9741 | } | |
9742 | } | |
9743 | ||
687629c2 AK |
9744 | /******************************************************************************* |
9745 | * Function: open_backup_targets | |
9746 | * Description: Function opens file descriptors for all devices given in | |
9747 | * info->devs | |
9748 | * Parameters: | |
9749 | * info : general array info | |
9750 | * raid_disks : number of disks | |
9751 | * raid_fds : table of device's file descriptors | |
9a717282 AK |
9752 | * super : intel super for raid10 degradation check |
9753 | * dev : intel device for raid10 degradation check | |
687629c2 AK |
9754 | * Returns: |
9755 | * 0 : success | |
9756 | * -1 : fail | |
9757 | ******************************************************************************/ | |
9a717282 AK |
9758 | int open_backup_targets(struct mdinfo *info, int raid_disks, int *raid_fds, |
9759 | struct intel_super *super, struct imsm_dev *dev) | |
687629c2 AK |
9760 | { |
9761 | struct mdinfo *sd; | |
f627f5ad | 9762 | int i; |
9a717282 | 9763 | int opened = 0; |
f627f5ad AK |
9764 | |
9765 | for (i = 0; i < raid_disks; i++) | |
9766 | raid_fds[i] = -1; | |
687629c2 AK |
9767 | |
9768 | for (sd = info->devs ; sd ; sd = sd->next) { | |
9769 | char *dn; | |
9770 | ||
9771 | if (sd->disk.state & (1<<MD_DISK_FAULTY)) { | |
9772 | dprintf("disk is faulty!!\n"); | |
9773 | continue; | |
9774 | } | |
9775 | ||
089f9d79 | 9776 | if (sd->disk.raid_disk >= raid_disks || sd->disk.raid_disk < 0) |
687629c2 AK |
9777 | continue; |
9778 | ||
9779 | dn = map_dev(sd->disk.major, | |
9780 | sd->disk.minor, 1); | |
9781 | raid_fds[sd->disk.raid_disk] = dev_open(dn, O_RDWR); | |
9782 | if (raid_fds[sd->disk.raid_disk] < 0) { | |
e12b3daa | 9783 | pr_err("cannot open component\n"); |
9a717282 | 9784 | continue; |
687629c2 | 9785 | } |
9a717282 AK |
9786 | opened++; |
9787 | } | |
9788 | /* check if maximum array degradation level is not exceeded | |
9789 | */ | |
9790 | if ((raid_disks - opened) > | |
089f9d79 JS |
9791 | imsm_get_allowed_degradation(info->new_level, raid_disks, |
9792 | super, dev)) { | |
e12b3daa | 9793 | pr_err("Not enough disks can be opened.\n"); |
9a717282 AK |
9794 | close_targets(raid_fds, raid_disks); |
9795 | return -2; | |
687629c2 AK |
9796 | } |
9797 | return 0; | |
9798 | } | |
9799 | ||
d31ad643 PB |
9800 | /******************************************************************************* |
9801 | * Function: validate_container_imsm | |
9802 | * Description: This routine validates container after assemble, | |
9803 | * eg. if devices in container are under the same controller. | |
9804 | * | |
9805 | * Parameters: | |
9806 | * info : linked list with info about devices used in array | |
9807 | * Returns: | |
9808 | * 1 : HBA mismatch | |
9809 | * 0 : Success | |
9810 | ******************************************************************************/ | |
9811 | int validate_container_imsm(struct mdinfo *info) | |
9812 | { | |
6b781d33 AP |
9813 | if (check_env("IMSM_NO_PLATFORM")) |
9814 | return 0; | |
d31ad643 | 9815 | |
6b781d33 AP |
9816 | struct sys_dev *idev; |
9817 | struct sys_dev *hba = NULL; | |
9818 | struct sys_dev *intel_devices = find_intel_devices(); | |
9819 | char *dev_path = devt_to_devpath(makedev(info->disk.major, | |
9820 | info->disk.minor)); | |
9821 | ||
9822 | for (idev = intel_devices; idev; idev = idev->next) { | |
9823 | if (dev_path && strstr(dev_path, idev->path)) { | |
9824 | hba = idev; | |
9825 | break; | |
d31ad643 | 9826 | } |
6b781d33 AP |
9827 | } |
9828 | if (dev_path) | |
d31ad643 PB |
9829 | free(dev_path); |
9830 | ||
6b781d33 AP |
9831 | if (!hba) { |
9832 | pr_err("WARNING - Cannot detect HBA for device %s!\n", | |
9833 | devid2kname(makedev(info->disk.major, info->disk.minor))); | |
9834 | return 1; | |
9835 | } | |
9836 | ||
9837 | const struct imsm_orom *orom = get_orom_by_device_id(hba->dev_id); | |
9838 | struct mdinfo *dev; | |
9839 | ||
9840 | for (dev = info->next; dev; dev = dev->next) { | |
9841 | dev_path = devt_to_devpath(makedev(dev->disk.major, dev->disk.minor)); | |
9842 | ||
9843 | struct sys_dev *hba2 = NULL; | |
9844 | for (idev = intel_devices; idev; idev = idev->next) { | |
9845 | if (dev_path && strstr(dev_path, idev->path)) { | |
9846 | hba2 = idev; | |
9847 | break; | |
d31ad643 PB |
9848 | } |
9849 | } | |
6b781d33 AP |
9850 | if (dev_path) |
9851 | free(dev_path); | |
9852 | ||
9853 | const struct imsm_orom *orom2 = hba2 == NULL ? NULL : | |
9854 | get_orom_by_device_id(hba2->dev_id); | |
9855 | ||
9856 | if (hba2 && hba->type != hba2->type) { | |
9857 | pr_err("WARNING - HBAs of devices do not match %s != %s\n", | |
9858 | get_sys_dev_type(hba->type), get_sys_dev_type(hba2->type)); | |
9859 | return 1; | |
9860 | } | |
9861 | ||
07cb1e57 | 9862 | if (orom != orom2) { |
6b781d33 AP |
9863 | pr_err("WARNING - IMSM container assembled with disks under different HBAs!\n" |
9864 | " This operation is not supported and can lead to data loss.\n"); | |
9865 | return 1; | |
9866 | } | |
9867 | ||
9868 | if (!orom) { | |
9869 | pr_err("WARNING - IMSM container assembled with disks under HBAs without IMSM platform support!\n" | |
9870 | " This operation is not supported and can lead to data loss.\n"); | |
9871 | return 1; | |
9872 | } | |
d31ad643 | 9873 | } |
6b781d33 | 9874 | |
d31ad643 PB |
9875 | return 0; |
9876 | } | |
9e2d750d | 9877 | #ifndef MDASSEMBLE |
6f50473f TM |
9878 | /******************************************************************************* |
9879 | * Function: imsm_record_badblock | |
9880 | * Description: This routine stores new bad block record in BBM log | |
9881 | * | |
9882 | * Parameters: | |
9883 | * a : array containing a bad block | |
9884 | * slot : disk number containing a bad block | |
9885 | * sector : bad block sector | |
9886 | * length : bad block sectors range | |
9887 | * Returns: | |
9888 | * 1 : Success | |
9889 | * 0 : Error | |
9890 | ******************************************************************************/ | |
9891 | static int imsm_record_badblock(struct active_array *a, int slot, | |
9892 | unsigned long long sector, int length) | |
9893 | { | |
9894 | struct intel_super *super = a->container->sb; | |
9895 | int ord; | |
9896 | int ret; | |
9897 | ||
9898 | ord = imsm_disk_slot_to_ord(a, slot); | |
9899 | if (ord < 0) | |
9900 | return 0; | |
9901 | ||
9902 | ret = record_new_badblock(super->bbm_log, ord_to_idx(ord), sector, | |
9903 | length); | |
9904 | if (ret) | |
9905 | super->updates_pending++; | |
9906 | ||
9907 | return ret; | |
9908 | } | |
c07a5a4f TM |
9909 | /******************************************************************************* |
9910 | * Function: imsm_clear_badblock | |
9911 | * Description: This routine clears bad block record from BBM log | |
9912 | * | |
9913 | * Parameters: | |
9914 | * a : array containing a bad block | |
9915 | * slot : disk number containing a bad block | |
9916 | * sector : bad block sector | |
9917 | * length : bad block sectors range | |
9918 | * Returns: | |
9919 | * 1 : Success | |
9920 | * 0 : Error | |
9921 | ******************************************************************************/ | |
9922 | static int imsm_clear_badblock(struct active_array *a, int slot, | |
9923 | unsigned long long sector, int length) | |
9924 | { | |
9925 | struct intel_super *super = a->container->sb; | |
9926 | int ord; | |
9927 | int ret; | |
9928 | ||
9929 | ord = imsm_disk_slot_to_ord(a, slot); | |
9930 | if (ord < 0) | |
9931 | return 0; | |
9932 | ||
9933 | ret = clear_badblock(super->bbm_log, ord_to_idx(ord), sector, length); | |
9934 | if (ret) | |
9935 | super->updates_pending++; | |
9936 | ||
9937 | return ret; | |
9938 | } | |
928f1424 TM |
9939 | /******************************************************************************* |
9940 | * Function: imsm_get_badblocks | |
9941 | * Description: This routine get list of bad blocks for an array | |
9942 | * | |
9943 | * Parameters: | |
9944 | * a : array | |
9945 | * slot : disk number | |
9946 | * Returns: | |
9947 | * bb : structure containing bad blocks | |
9948 | * NULL : error | |
9949 | ******************************************************************************/ | |
9950 | static struct md_bb *imsm_get_badblocks(struct active_array *a, int slot) | |
9951 | { | |
9952 | int inst = a->info.container_member; | |
9953 | struct intel_super *super = a->container->sb; | |
9954 | struct imsm_dev *dev = get_imsm_dev(super, inst); | |
9955 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
9956 | int ord; | |
9957 | ||
9958 | ord = imsm_disk_slot_to_ord(a, slot); | |
9959 | if (ord < 0) | |
9960 | return NULL; | |
9961 | ||
9962 | get_volume_badblocks(super->bbm_log, ord_to_idx(ord), pba_of_lba0(map), | |
9963 | blocks_per_member(map), &super->bb); | |
9964 | ||
9965 | return &super->bb; | |
9966 | } | |
27156a57 TM |
9967 | /******************************************************************************* |
9968 | * Function: examine_badblocks_imsm | |
9969 | * Description: Prints list of bad blocks on a disk to the standard output | |
9970 | * | |
9971 | * Parameters: | |
9972 | * st : metadata handler | |
9973 | * fd : open file descriptor for device | |
9974 | * devname : device name | |
9975 | * Returns: | |
9976 | * 0 : Success | |
9977 | * 1 : Error | |
9978 | ******************************************************************************/ | |
9979 | static int examine_badblocks_imsm(struct supertype *st, int fd, char *devname) | |
9980 | { | |
9981 | struct intel_super *super = st->sb; | |
9982 | struct bbm_log *log = super->bbm_log; | |
9983 | struct dl *d = NULL; | |
9984 | int any = 0; | |
9985 | ||
9986 | for (d = super->disks; d ; d = d->next) { | |
9987 | if (strcmp(d->devname, devname) == 0) | |
9988 | break; | |
9989 | } | |
9990 | ||
9991 | if ((d == NULL) || (d->index < 0)) { /* serial mismatch probably */ | |
9992 | pr_err("%s doesn't appear to be part of a raid array\n", | |
9993 | devname); | |
9994 | return 1; | |
9995 | } | |
9996 | ||
9997 | if (log != NULL) { | |
9998 | unsigned int i; | |
9999 | struct bbm_log_entry *entry = &log->marked_block_entries[0]; | |
10000 | ||
10001 | for (i = 0; i < log->entry_count; i++) { | |
10002 | if (entry[i].disk_ordinal == d->index) { | |
10003 | unsigned long long sector = __le48_to_cpu( | |
10004 | &entry[i].defective_block_start); | |
10005 | int cnt = entry[i].marked_count + 1; | |
10006 | ||
10007 | if (!any) { | |
10008 | printf("Bad-blocks on %s:\n", devname); | |
10009 | any = 1; | |
10010 | } | |
10011 | ||
10012 | printf("%20llu for %d sectors\n", sector, cnt); | |
10013 | } | |
10014 | } | |
10015 | } | |
10016 | ||
10017 | if (!any) | |
10018 | printf("No bad-blocks list configured on %s\n", devname); | |
10019 | ||
10020 | return 0; | |
10021 | } | |
687629c2 AK |
10022 | /******************************************************************************* |
10023 | * Function: init_migr_record_imsm | |
10024 | * Description: Function inits imsm migration record | |
10025 | * Parameters: | |
10026 | * super : imsm internal array info | |
10027 | * dev : device under migration | |
10028 | * info : general array info to find the smallest device | |
10029 | * Returns: | |
10030 | * none | |
10031 | ******************************************************************************/ | |
10032 | void init_migr_record_imsm(struct supertype *st, struct imsm_dev *dev, | |
10033 | struct mdinfo *info) | |
10034 | { | |
10035 | struct intel_super *super = st->sb; | |
10036 | struct migr_record *migr_rec = super->migr_rec; | |
10037 | int new_data_disks; | |
10038 | unsigned long long dsize, dev_sectors; | |
10039 | long long unsigned min_dev_sectors = -1LLU; | |
10040 | struct mdinfo *sd; | |
10041 | char nm[30]; | |
10042 | int fd; | |
238c0a71 AK |
10043 | struct imsm_map *map_dest = get_imsm_map(dev, MAP_0); |
10044 | struct imsm_map *map_src = get_imsm_map(dev, MAP_1); | |
687629c2 | 10045 | unsigned long long num_migr_units; |
3ef4403c | 10046 | unsigned long long array_blocks; |
687629c2 AK |
10047 | |
10048 | memset(migr_rec, 0, sizeof(struct migr_record)); | |
10049 | migr_rec->family_num = __cpu_to_le32(super->anchor->family_num); | |
10050 | ||
10051 | /* only ascending reshape supported now */ | |
10052 | migr_rec->ascending_migr = __cpu_to_le32(1); | |
10053 | ||
10054 | migr_rec->dest_depth_per_unit = GEN_MIGR_AREA_SIZE / | |
10055 | max(map_dest->blocks_per_strip, map_src->blocks_per_strip); | |
e1742195 AK |
10056 | migr_rec->dest_depth_per_unit *= |
10057 | max(map_dest->blocks_per_strip, map_src->blocks_per_strip); | |
238c0a71 | 10058 | new_data_disks = imsm_num_data_members(dev, MAP_0); |
687629c2 AK |
10059 | migr_rec->blocks_per_unit = |
10060 | __cpu_to_le32(migr_rec->dest_depth_per_unit * new_data_disks); | |
10061 | migr_rec->dest_depth_per_unit = | |
10062 | __cpu_to_le32(migr_rec->dest_depth_per_unit); | |
3ef4403c | 10063 | array_blocks = info->component_size * new_data_disks; |
687629c2 AK |
10064 | num_migr_units = |
10065 | array_blocks / __le32_to_cpu(migr_rec->blocks_per_unit); | |
10066 | ||
10067 | if (array_blocks % __le32_to_cpu(migr_rec->blocks_per_unit)) | |
10068 | num_migr_units++; | |
10069 | migr_rec->num_migr_units = __cpu_to_le32(num_migr_units); | |
10070 | ||
10071 | migr_rec->post_migr_vol_cap = dev->size_low; | |
10072 | migr_rec->post_migr_vol_cap_hi = dev->size_high; | |
10073 | ||
687629c2 AK |
10074 | /* Find the smallest dev */ |
10075 | for (sd = info->devs ; sd ; sd = sd->next) { | |
10076 | sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor); | |
10077 | fd = dev_open(nm, O_RDONLY); | |
10078 | if (fd < 0) | |
10079 | continue; | |
10080 | get_dev_size(fd, NULL, &dsize); | |
10081 | dev_sectors = dsize / 512; | |
10082 | if (dev_sectors < min_dev_sectors) | |
10083 | min_dev_sectors = dev_sectors; | |
10084 | close(fd); | |
10085 | } | |
10086 | migr_rec->ckpt_area_pba = __cpu_to_le32(min_dev_sectors - | |
10087 | RAID_DISK_RESERVED_BLOCKS_IMSM_HI); | |
10088 | ||
10089 | write_imsm_migr_rec(st); | |
10090 | ||
10091 | return; | |
10092 | } | |
10093 | ||
10094 | /******************************************************************************* | |
10095 | * Function: save_backup_imsm | |
10096 | * Description: Function saves critical data stripes to Migration Copy Area | |
10097 | * and updates the current migration unit status. | |
10098 | * Use restore_stripes() to form a destination stripe, | |
10099 | * and to write it to the Copy Area. | |
10100 | * Parameters: | |
10101 | * st : supertype information | |
aea93171 | 10102 | * dev : imsm device that backup is saved for |
687629c2 AK |
10103 | * info : general array info |
10104 | * buf : input buffer | |
687629c2 AK |
10105 | * length : length of data to backup (blocks_per_unit) |
10106 | * Returns: | |
10107 | * 0 : success | |
10108 | *, -1 : fail | |
10109 | ******************************************************************************/ | |
10110 | int save_backup_imsm(struct supertype *st, | |
10111 | struct imsm_dev *dev, | |
10112 | struct mdinfo *info, | |
10113 | void *buf, | |
687629c2 AK |
10114 | int length) |
10115 | { | |
10116 | int rv = -1; | |
10117 | struct intel_super *super = st->sb; | |
594dc1b8 JS |
10118 | unsigned long long *target_offsets; |
10119 | int *targets; | |
687629c2 | 10120 | int i; |
238c0a71 | 10121 | struct imsm_map *map_dest = get_imsm_map(dev, MAP_0); |
687629c2 | 10122 | int new_disks = map_dest->num_members; |
ab724b98 AK |
10123 | int dest_layout = 0; |
10124 | int dest_chunk; | |
d1877f69 | 10125 | unsigned long long start; |
238c0a71 | 10126 | int data_disks = imsm_num_data_members(dev, MAP_0); |
687629c2 | 10127 | |
503975b9 | 10128 | targets = xmalloc(new_disks * sizeof(int)); |
687629c2 | 10129 | |
7e45b550 AK |
10130 | for (i = 0; i < new_disks; i++) |
10131 | targets[i] = -1; | |
10132 | ||
503975b9 | 10133 | target_offsets = xcalloc(new_disks, sizeof(unsigned long long)); |
687629c2 | 10134 | |
d1877f69 | 10135 | start = info->reshape_progress * 512; |
687629c2 | 10136 | for (i = 0; i < new_disks; i++) { |
687629c2 AK |
10137 | target_offsets[i] = (unsigned long long) |
10138 | __le32_to_cpu(super->migr_rec->ckpt_area_pba) * 512; | |
d1877f69 AK |
10139 | /* move back copy area adderss, it will be moved forward |
10140 | * in restore_stripes() using start input variable | |
10141 | */ | |
10142 | target_offsets[i] -= start/data_disks; | |
687629c2 AK |
10143 | } |
10144 | ||
9a717282 AK |
10145 | if (open_backup_targets(info, new_disks, targets, |
10146 | super, dev)) | |
687629c2 AK |
10147 | goto abort; |
10148 | ||
68eb8bc6 | 10149 | dest_layout = imsm_level_to_layout(map_dest->raid_level); |
ab724b98 AK |
10150 | dest_chunk = __le16_to_cpu(map_dest->blocks_per_strip) * 512; |
10151 | ||
687629c2 AK |
10152 | if (restore_stripes(targets, /* list of dest devices */ |
10153 | target_offsets, /* migration record offsets */ | |
10154 | new_disks, | |
ab724b98 AK |
10155 | dest_chunk, |
10156 | map_dest->raid_level, | |
10157 | dest_layout, | |
10158 | -1, /* source backup file descriptor */ | |
10159 | 0, /* input buf offset | |
10160 | * always 0 buf is already offseted */ | |
d1877f69 | 10161 | start, |
687629c2 AK |
10162 | length, |
10163 | buf) != 0) { | |
e7b84f9d | 10164 | pr_err("Error restoring stripes\n"); |
687629c2 AK |
10165 | goto abort; |
10166 | } | |
10167 | ||
10168 | rv = 0; | |
10169 | ||
10170 | abort: | |
10171 | if (targets) { | |
9a717282 | 10172 | close_targets(targets, new_disks); |
687629c2 AK |
10173 | free(targets); |
10174 | } | |
10175 | free(target_offsets); | |
10176 | ||
10177 | return rv; | |
10178 | } | |
10179 | ||
10180 | /******************************************************************************* | |
10181 | * Function: save_checkpoint_imsm | |
10182 | * Description: Function called for current unit status update | |
10183 | * in the migration record. It writes it to disk. | |
10184 | * Parameters: | |
10185 | * super : imsm internal array info | |
10186 | * info : general array info | |
10187 | * Returns: | |
10188 | * 0: success | |
10189 | * 1: failure | |
0228d92c AK |
10190 | * 2: failure, means no valid migration record |
10191 | * / no general migration in progress / | |
687629c2 AK |
10192 | ******************************************************************************/ |
10193 | int save_checkpoint_imsm(struct supertype *st, struct mdinfo *info, int state) | |
10194 | { | |
10195 | struct intel_super *super = st->sb; | |
f8b72ef5 AK |
10196 | unsigned long long blocks_per_unit; |
10197 | unsigned long long curr_migr_unit; | |
10198 | ||
2e062e82 | 10199 | if (load_imsm_migr_rec(super, info) != 0) { |
7a862a02 | 10200 | dprintf("imsm: ERROR: Cannot read migration record for checkpoint save.\n"); |
2e062e82 AK |
10201 | return 1; |
10202 | } | |
10203 | ||
f8b72ef5 AK |
10204 | blocks_per_unit = __le32_to_cpu(super->migr_rec->blocks_per_unit); |
10205 | if (blocks_per_unit == 0) { | |
0228d92c AK |
10206 | dprintf("imsm: no migration in progress.\n"); |
10207 | return 2; | |
687629c2 | 10208 | } |
f8b72ef5 AK |
10209 | curr_migr_unit = info->reshape_progress / blocks_per_unit; |
10210 | /* check if array is alligned to copy area | |
10211 | * if it is not alligned, add one to current migration unit value | |
10212 | * this can happend on array reshape finish only | |
10213 | */ | |
10214 | if (info->reshape_progress % blocks_per_unit) | |
10215 | curr_migr_unit++; | |
687629c2 AK |
10216 | |
10217 | super->migr_rec->curr_migr_unit = | |
f8b72ef5 | 10218 | __cpu_to_le32(curr_migr_unit); |
687629c2 AK |
10219 | super->migr_rec->rec_status = __cpu_to_le32(state); |
10220 | super->migr_rec->dest_1st_member_lba = | |
f8b72ef5 AK |
10221 | __cpu_to_le32(curr_migr_unit * |
10222 | __le32_to_cpu(super->migr_rec->dest_depth_per_unit)); | |
687629c2 | 10223 | if (write_imsm_migr_rec(st) < 0) { |
7a862a02 | 10224 | dprintf("imsm: Cannot write migration record outside backup area\n"); |
687629c2 AK |
10225 | return 1; |
10226 | } | |
10227 | ||
10228 | return 0; | |
10229 | } | |
10230 | ||
276d77db AK |
10231 | /******************************************************************************* |
10232 | * Function: recover_backup_imsm | |
10233 | * Description: Function recovers critical data from the Migration Copy Area | |
10234 | * while assembling an array. | |
10235 | * Parameters: | |
10236 | * super : imsm internal array info | |
10237 | * info : general array info | |
10238 | * Returns: | |
10239 | * 0 : success (or there is no data to recover) | |
10240 | * 1 : fail | |
10241 | ******************************************************************************/ | |
10242 | int recover_backup_imsm(struct supertype *st, struct mdinfo *info) | |
10243 | { | |
10244 | struct intel_super *super = st->sb; | |
10245 | struct migr_record *migr_rec = super->migr_rec; | |
594dc1b8 | 10246 | struct imsm_map *map_dest; |
276d77db AK |
10247 | struct intel_dev *id = NULL; |
10248 | unsigned long long read_offset; | |
10249 | unsigned long long write_offset; | |
10250 | unsigned unit_len; | |
10251 | int *targets = NULL; | |
10252 | int new_disks, i, err; | |
10253 | char *buf = NULL; | |
10254 | int retval = 1; | |
f36a9ecd | 10255 | unsigned int sector_size = super->sector_size; |
276d77db AK |
10256 | unsigned long curr_migr_unit = __le32_to_cpu(migr_rec->curr_migr_unit); |
10257 | unsigned long num_migr_units = __le32_to_cpu(migr_rec->num_migr_units); | |
276d77db | 10258 | char buffer[20]; |
6c3560c0 | 10259 | int skipped_disks = 0; |
276d77db AK |
10260 | |
10261 | err = sysfs_get_str(info, NULL, "array_state", (char *)buffer, 20); | |
10262 | if (err < 1) | |
10263 | return 1; | |
10264 | ||
10265 | /* recover data only during assemblation */ | |
10266 | if (strncmp(buffer, "inactive", 8) != 0) | |
10267 | return 0; | |
10268 | /* no data to recover */ | |
10269 | if (__le32_to_cpu(migr_rec->rec_status) == UNIT_SRC_NORMAL) | |
10270 | return 0; | |
10271 | if (curr_migr_unit >= num_migr_units) | |
10272 | return 1; | |
10273 | ||
10274 | /* find device during reshape */ | |
10275 | for (id = super->devlist; id; id = id->next) | |
10276 | if (is_gen_migration(id->dev)) | |
10277 | break; | |
10278 | if (id == NULL) | |
10279 | return 1; | |
10280 | ||
238c0a71 | 10281 | map_dest = get_imsm_map(id->dev, MAP_0); |
276d77db AK |
10282 | new_disks = map_dest->num_members; |
10283 | ||
10284 | read_offset = (unsigned long long) | |
10285 | __le32_to_cpu(migr_rec->ckpt_area_pba) * 512; | |
10286 | ||
10287 | write_offset = ((unsigned long long) | |
10288 | __le32_to_cpu(migr_rec->dest_1st_member_lba) + | |
5551b113 | 10289 | pba_of_lba0(map_dest)) * 512; |
276d77db AK |
10290 | |
10291 | unit_len = __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512; | |
f36a9ecd | 10292 | if (posix_memalign((void **)&buf, sector_size, unit_len) != 0) |
276d77db | 10293 | goto abort; |
503975b9 | 10294 | targets = xcalloc(new_disks, sizeof(int)); |
276d77db | 10295 | |
9a717282 | 10296 | if (open_backup_targets(info, new_disks, targets, super, id->dev)) { |
e7b84f9d | 10297 | pr_err("Cannot open some devices belonging to array.\n"); |
f627f5ad AK |
10298 | goto abort; |
10299 | } | |
276d77db AK |
10300 | |
10301 | for (i = 0; i < new_disks; i++) { | |
6c3560c0 AK |
10302 | if (targets[i] < 0) { |
10303 | skipped_disks++; | |
10304 | continue; | |
10305 | } | |
276d77db | 10306 | if (lseek64(targets[i], read_offset, SEEK_SET) < 0) { |
e7b84f9d N |
10307 | pr_err("Cannot seek to block: %s\n", |
10308 | strerror(errno)); | |
137debce AK |
10309 | skipped_disks++; |
10310 | continue; | |
276d77db | 10311 | } |
9ec11d1a | 10312 | if ((unsigned)read(targets[i], buf, unit_len) != unit_len) { |
e7b84f9d N |
10313 | pr_err("Cannot read copy area block: %s\n", |
10314 | strerror(errno)); | |
137debce AK |
10315 | skipped_disks++; |
10316 | continue; | |
276d77db AK |
10317 | } |
10318 | if (lseek64(targets[i], write_offset, SEEK_SET) < 0) { | |
e7b84f9d N |
10319 | pr_err("Cannot seek to block: %s\n", |
10320 | strerror(errno)); | |
137debce AK |
10321 | skipped_disks++; |
10322 | continue; | |
276d77db | 10323 | } |
9ec11d1a | 10324 | if ((unsigned)write(targets[i], buf, unit_len) != unit_len) { |
e7b84f9d N |
10325 | pr_err("Cannot restore block: %s\n", |
10326 | strerror(errno)); | |
137debce AK |
10327 | skipped_disks++; |
10328 | continue; | |
276d77db AK |
10329 | } |
10330 | } | |
10331 | ||
137debce AK |
10332 | if (skipped_disks > imsm_get_allowed_degradation(info->new_level, |
10333 | new_disks, | |
10334 | super, | |
10335 | id->dev)) { | |
7a862a02 | 10336 | pr_err("Cannot restore data from backup. Too many failed disks\n"); |
6c3560c0 AK |
10337 | goto abort; |
10338 | } | |
10339 | ||
befb629b AK |
10340 | if (save_checkpoint_imsm(st, info, UNIT_SRC_NORMAL)) { |
10341 | /* ignore error == 2, this can mean end of reshape here | |
10342 | */ | |
7a862a02 | 10343 | dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL) during restart\n"); |
befb629b | 10344 | } else |
276d77db | 10345 | retval = 0; |
276d77db AK |
10346 | |
10347 | abort: | |
10348 | if (targets) { | |
10349 | for (i = 0; i < new_disks; i++) | |
10350 | if (targets[i]) | |
10351 | close(targets[i]); | |
10352 | free(targets); | |
10353 | } | |
10354 | free(buf); | |
10355 | return retval; | |
10356 | } | |
10357 | ||
2cda7640 ML |
10358 | static char disk_by_path[] = "/dev/disk/by-path/"; |
10359 | ||
10360 | static const char *imsm_get_disk_controller_domain(const char *path) | |
10361 | { | |
2cda7640 | 10362 | char disk_path[PATH_MAX]; |
96234762 LM |
10363 | char *drv=NULL; |
10364 | struct stat st; | |
2cda7640 | 10365 | |
6d8d290a | 10366 | strcpy(disk_path, disk_by_path); |
96234762 LM |
10367 | strncat(disk_path, path, PATH_MAX - strlen(disk_path) - 1); |
10368 | if (stat(disk_path, &st) == 0) { | |
10369 | struct sys_dev* hba; | |
594dc1b8 | 10370 | char *path; |
96234762 LM |
10371 | |
10372 | path = devt_to_devpath(st.st_rdev); | |
10373 | if (path == NULL) | |
10374 | return "unknown"; | |
10375 | hba = find_disk_attached_hba(-1, path); | |
10376 | if (hba && hba->type == SYS_DEV_SAS) | |
10377 | drv = "isci"; | |
10378 | else if (hba && hba->type == SYS_DEV_SATA) | |
10379 | drv = "ahci"; | |
1011e834 | 10380 | else |
96234762 LM |
10381 | drv = "unknown"; |
10382 | dprintf("path: %s hba: %s attached: %s\n", | |
10383 | path, (hba) ? hba->path : "NULL", drv); | |
10384 | free(path); | |
2cda7640 | 10385 | } |
96234762 | 10386 | return drv; |
2cda7640 ML |
10387 | } |
10388 | ||
4dd2df09 | 10389 | static char *imsm_find_array_devnm_by_subdev(int subdev, char *container) |
78b10e66 | 10390 | { |
4dd2df09 | 10391 | static char devnm[32]; |
78b10e66 N |
10392 | char subdev_name[20]; |
10393 | struct mdstat_ent *mdstat; | |
10394 | ||
10395 | sprintf(subdev_name, "%d", subdev); | |
10396 | mdstat = mdstat_by_subdev(subdev_name, container); | |
10397 | if (!mdstat) | |
4dd2df09 | 10398 | return NULL; |
78b10e66 | 10399 | |
4dd2df09 | 10400 | strcpy(devnm, mdstat->devnm); |
78b10e66 | 10401 | free_mdstat(mdstat); |
4dd2df09 | 10402 | return devnm; |
78b10e66 N |
10403 | } |
10404 | ||
10405 | static int imsm_reshape_is_allowed_on_container(struct supertype *st, | |
10406 | struct geo_params *geo, | |
fbf3d202 AK |
10407 | int *old_raid_disks, |
10408 | int direction) | |
78b10e66 | 10409 | { |
694575e7 KW |
10410 | /* currently we only support increasing the number of devices |
10411 | * for a container. This increases the number of device for each | |
10412 | * member array. They must all be RAID0 or RAID5. | |
10413 | */ | |
78b10e66 N |
10414 | int ret_val = 0; |
10415 | struct mdinfo *info, *member; | |
10416 | int devices_that_can_grow = 0; | |
10417 | ||
7a862a02 | 10418 | dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): st->devnm = (%s)\n", st->devnm); |
78b10e66 | 10419 | |
d04f65f4 | 10420 | if (geo->size > 0 || |
78b10e66 N |
10421 | geo->level != UnSet || |
10422 | geo->layout != UnSet || | |
10423 | geo->chunksize != 0 || | |
10424 | geo->raid_disks == UnSet) { | |
7a862a02 | 10425 | dprintf("imsm: Container operation is allowed for raid disks number change only.\n"); |
78b10e66 N |
10426 | return ret_val; |
10427 | } | |
10428 | ||
fbf3d202 | 10429 | if (direction == ROLLBACK_METADATA_CHANGES) { |
7a862a02 | 10430 | dprintf("imsm: Metadata changes rollback is not supported for container operation.\n"); |
fbf3d202 AK |
10431 | return ret_val; |
10432 | } | |
10433 | ||
78b10e66 N |
10434 | info = container_content_imsm(st, NULL); |
10435 | for (member = info; member; member = member->next) { | |
4dd2df09 | 10436 | char *result; |
78b10e66 N |
10437 | |
10438 | dprintf("imsm: checking device_num: %i\n", | |
10439 | member->container_member); | |
10440 | ||
d7d205bd | 10441 | if (geo->raid_disks <= member->array.raid_disks) { |
78b10e66 N |
10442 | /* we work on container for Online Capacity Expansion |
10443 | * only so raid_disks has to grow | |
10444 | */ | |
7a862a02 | 10445 | dprintf("imsm: for container operation raid disks increase is required\n"); |
78b10e66 N |
10446 | break; |
10447 | } | |
10448 | ||
089f9d79 | 10449 | if (info->array.level != 0 && info->array.level != 5) { |
78b10e66 N |
10450 | /* we cannot use this container with other raid level |
10451 | */ | |
7a862a02 | 10452 | dprintf("imsm: for container operation wrong raid level (%i) detected\n", |
78b10e66 N |
10453 | info->array.level); |
10454 | break; | |
10455 | } else { | |
10456 | /* check for platform support | |
10457 | * for this raid level configuration | |
10458 | */ | |
10459 | struct intel_super *super = st->sb; | |
10460 | if (!is_raid_level_supported(super->orom, | |
10461 | member->array.level, | |
10462 | geo->raid_disks)) { | |
7a862a02 | 10463 | dprintf("platform does not support raid%d with %d disk%s\n", |
78b10e66 N |
10464 | info->array.level, |
10465 | geo->raid_disks, | |
10466 | geo->raid_disks > 1 ? "s" : ""); | |
10467 | break; | |
10468 | } | |
2a4a08e7 AK |
10469 | /* check if component size is aligned to chunk size |
10470 | */ | |
10471 | if (info->component_size % | |
10472 | (info->array.chunk_size/512)) { | |
7a862a02 | 10473 | dprintf("Component size is not aligned to chunk size\n"); |
2a4a08e7 AK |
10474 | break; |
10475 | } | |
78b10e66 N |
10476 | } |
10477 | ||
10478 | if (*old_raid_disks && | |
10479 | info->array.raid_disks != *old_raid_disks) | |
10480 | break; | |
10481 | *old_raid_disks = info->array.raid_disks; | |
10482 | ||
10483 | /* All raid5 and raid0 volumes in container | |
10484 | * have to be ready for Online Capacity Expansion | |
10485 | * so they need to be assembled. We have already | |
10486 | * checked that no recovery etc is happening. | |
10487 | */ | |
4dd2df09 N |
10488 | result = imsm_find_array_devnm_by_subdev(member->container_member, |
10489 | st->container_devnm); | |
10490 | if (result == NULL) { | |
78b10e66 N |
10491 | dprintf("imsm: cannot find array\n"); |
10492 | break; | |
10493 | } | |
10494 | devices_that_can_grow++; | |
10495 | } | |
10496 | sysfs_free(info); | |
10497 | if (!member && devices_that_can_grow) | |
10498 | ret_val = 1; | |
10499 | ||
10500 | if (ret_val) | |
1ade5cc1 | 10501 | dprintf("Container operation allowed\n"); |
78b10e66 | 10502 | else |
1ade5cc1 | 10503 | dprintf("Error: %i\n", ret_val); |
78b10e66 N |
10504 | |
10505 | return ret_val; | |
10506 | } | |
10507 | ||
10508 | /* Function: get_spares_for_grow | |
10509 | * Description: Allocates memory and creates list of spare devices | |
1011e834 | 10510 | * avaliable in container. Checks if spare drive size is acceptable. |
78b10e66 N |
10511 | * Parameters: Pointer to the supertype structure |
10512 | * Returns: Pointer to the list of spare devices (mdinfo structure) on success, | |
1011e834 | 10513 | * NULL if fail |
78b10e66 N |
10514 | */ |
10515 | static struct mdinfo *get_spares_for_grow(struct supertype *st) | |
10516 | { | |
78b10e66 | 10517 | unsigned long long min_size = min_acceptable_spare_size_imsm(st); |
326727d9 | 10518 | return container_choose_spares(st, min_size, NULL, NULL, NULL, 0); |
78b10e66 N |
10519 | } |
10520 | ||
10521 | /****************************************************************************** | |
10522 | * function: imsm_create_metadata_update_for_reshape | |
10523 | * Function creates update for whole IMSM container. | |
10524 | * | |
10525 | ******************************************************************************/ | |
10526 | static int imsm_create_metadata_update_for_reshape( | |
10527 | struct supertype *st, | |
10528 | struct geo_params *geo, | |
10529 | int old_raid_disks, | |
10530 | struct imsm_update_reshape **updatep) | |
10531 | { | |
10532 | struct intel_super *super = st->sb; | |
10533 | struct imsm_super *mpb = super->anchor; | |
594dc1b8 JS |
10534 | int update_memory_size; |
10535 | struct imsm_update_reshape *u; | |
10536 | struct mdinfo *spares; | |
78b10e66 | 10537 | int i; |
594dc1b8 | 10538 | int delta_disks; |
bbd24d86 | 10539 | struct mdinfo *dev; |
78b10e66 | 10540 | |
1ade5cc1 | 10541 | dprintf("(enter) raid_disks = %i\n", geo->raid_disks); |
78b10e66 N |
10542 | |
10543 | delta_disks = geo->raid_disks - old_raid_disks; | |
10544 | ||
10545 | /* size of all update data without anchor */ | |
10546 | update_memory_size = sizeof(struct imsm_update_reshape); | |
10547 | ||
10548 | /* now add space for spare disks that we need to add. */ | |
10549 | update_memory_size += sizeof(u->new_disks[0]) * (delta_disks - 1); | |
10550 | ||
503975b9 | 10551 | u = xcalloc(1, update_memory_size); |
78b10e66 N |
10552 | u->type = update_reshape_container_disks; |
10553 | u->old_raid_disks = old_raid_disks; | |
10554 | u->new_raid_disks = geo->raid_disks; | |
10555 | ||
10556 | /* now get spare disks list | |
10557 | */ | |
10558 | spares = get_spares_for_grow(st); | |
10559 | ||
10560 | if (spares == NULL | |
10561 | || delta_disks > spares->array.spare_disks) { | |
7a862a02 | 10562 | pr_err("imsm: ERROR: Cannot get spare devices for %s.\n", geo->dev_name); |
e4c72d1d | 10563 | i = -1; |
78b10e66 N |
10564 | goto abort; |
10565 | } | |
10566 | ||
10567 | /* we have got spares | |
10568 | * update disk list in imsm_disk list table in anchor | |
10569 | */ | |
10570 | dprintf("imsm: %i spares are available.\n\n", | |
10571 | spares->array.spare_disks); | |
10572 | ||
bbd24d86 | 10573 | dev = spares->devs; |
78b10e66 | 10574 | for (i = 0; i < delta_disks; i++) { |
78b10e66 N |
10575 | struct dl *dl; |
10576 | ||
bbd24d86 AK |
10577 | if (dev == NULL) |
10578 | break; | |
78b10e66 N |
10579 | u->new_disks[i] = makedev(dev->disk.major, |
10580 | dev->disk.minor); | |
10581 | dl = get_disk_super(super, dev->disk.major, dev->disk.minor); | |
ee4beede AK |
10582 | dl->index = mpb->num_disks; |
10583 | mpb->num_disks++; | |
bbd24d86 | 10584 | dev = dev->next; |
78b10e66 | 10585 | } |
78b10e66 N |
10586 | |
10587 | abort: | |
10588 | /* free spares | |
10589 | */ | |
10590 | sysfs_free(spares); | |
10591 | ||
d677e0b8 | 10592 | dprintf("imsm: reshape update preparation :"); |
78b10e66 | 10593 | if (i == delta_disks) { |
1ade5cc1 | 10594 | dprintf_cont(" OK\n"); |
78b10e66 N |
10595 | *updatep = u; |
10596 | return update_memory_size; | |
10597 | } | |
10598 | free(u); | |
1ade5cc1 | 10599 | dprintf_cont(" Error\n"); |
78b10e66 N |
10600 | |
10601 | return 0; | |
10602 | } | |
10603 | ||
f3871fdc AK |
10604 | /****************************************************************************** |
10605 | * function: imsm_create_metadata_update_for_size_change() | |
10606 | * Creates update for IMSM array for array size change. | |
10607 | * | |
10608 | ******************************************************************************/ | |
10609 | static int imsm_create_metadata_update_for_size_change( | |
10610 | struct supertype *st, | |
10611 | struct geo_params *geo, | |
10612 | struct imsm_update_size_change **updatep) | |
10613 | { | |
10614 | struct intel_super *super = st->sb; | |
594dc1b8 JS |
10615 | int update_memory_size; |
10616 | struct imsm_update_size_change *u; | |
f3871fdc | 10617 | |
1ade5cc1 | 10618 | dprintf("(enter) New size = %llu\n", geo->size); |
f3871fdc AK |
10619 | |
10620 | /* size of all update data without anchor */ | |
10621 | update_memory_size = sizeof(struct imsm_update_size_change); | |
10622 | ||
503975b9 | 10623 | u = xcalloc(1, update_memory_size); |
f3871fdc AK |
10624 | u->type = update_size_change; |
10625 | u->subdev = super->current_vol; | |
10626 | u->new_size = geo->size; | |
10627 | ||
10628 | dprintf("imsm: reshape update preparation : OK\n"); | |
10629 | *updatep = u; | |
10630 | ||
10631 | return update_memory_size; | |
10632 | } | |
10633 | ||
48c5303a PC |
10634 | /****************************************************************************** |
10635 | * function: imsm_create_metadata_update_for_migration() | |
10636 | * Creates update for IMSM array. | |
10637 | * | |
10638 | ******************************************************************************/ | |
10639 | static int imsm_create_metadata_update_for_migration( | |
10640 | struct supertype *st, | |
10641 | struct geo_params *geo, | |
10642 | struct imsm_update_reshape_migration **updatep) | |
10643 | { | |
10644 | struct intel_super *super = st->sb; | |
594dc1b8 JS |
10645 | int update_memory_size; |
10646 | struct imsm_update_reshape_migration *u; | |
48c5303a PC |
10647 | struct imsm_dev *dev; |
10648 | int previous_level = -1; | |
10649 | ||
1ade5cc1 | 10650 | dprintf("(enter) New Level = %i\n", geo->level); |
48c5303a PC |
10651 | |
10652 | /* size of all update data without anchor */ | |
10653 | update_memory_size = sizeof(struct imsm_update_reshape_migration); | |
10654 | ||
503975b9 | 10655 | u = xcalloc(1, update_memory_size); |
48c5303a PC |
10656 | u->type = update_reshape_migration; |
10657 | u->subdev = super->current_vol; | |
10658 | u->new_level = geo->level; | |
10659 | u->new_layout = geo->layout; | |
10660 | u->new_raid_disks = u->old_raid_disks = geo->raid_disks; | |
10661 | u->new_disks[0] = -1; | |
4bba0439 | 10662 | u->new_chunksize = -1; |
48c5303a PC |
10663 | |
10664 | dev = get_imsm_dev(super, u->subdev); | |
10665 | if (dev) { | |
10666 | struct imsm_map *map; | |
10667 | ||
238c0a71 | 10668 | map = get_imsm_map(dev, MAP_0); |
4bba0439 PC |
10669 | if (map) { |
10670 | int current_chunk_size = | |
10671 | __le16_to_cpu(map->blocks_per_strip) / 2; | |
10672 | ||
10673 | if (geo->chunksize != current_chunk_size) { | |
10674 | u->new_chunksize = geo->chunksize / 1024; | |
7a862a02 | 10675 | dprintf("imsm: chunk size change from %i to %i\n", |
4bba0439 PC |
10676 | current_chunk_size, u->new_chunksize); |
10677 | } | |
48c5303a | 10678 | previous_level = map->raid_level; |
4bba0439 | 10679 | } |
48c5303a | 10680 | } |
089f9d79 | 10681 | if (geo->level == 5 && previous_level == 0) { |
48c5303a PC |
10682 | struct mdinfo *spares = NULL; |
10683 | ||
10684 | u->new_raid_disks++; | |
10685 | spares = get_spares_for_grow(st); | |
089f9d79 | 10686 | if (spares == NULL || spares->array.spare_disks < 1) { |
48c5303a PC |
10687 | free(u); |
10688 | sysfs_free(spares); | |
10689 | update_memory_size = 0; | |
7a862a02 | 10690 | dprintf("error: cannot get spare device for requested migration"); |
48c5303a PC |
10691 | return 0; |
10692 | } | |
10693 | sysfs_free(spares); | |
10694 | } | |
10695 | dprintf("imsm: reshape update preparation : OK\n"); | |
10696 | *updatep = u; | |
10697 | ||
10698 | return update_memory_size; | |
10699 | } | |
10700 | ||
8dd70bce AK |
10701 | static void imsm_update_metadata_locally(struct supertype *st, |
10702 | void *buf, int len) | |
10703 | { | |
10704 | struct metadata_update mu; | |
10705 | ||
10706 | mu.buf = buf; | |
10707 | mu.len = len; | |
10708 | mu.space = NULL; | |
10709 | mu.space_list = NULL; | |
10710 | mu.next = NULL; | |
5fe6f031 N |
10711 | if (imsm_prepare_update(st, &mu)) |
10712 | imsm_process_update(st, &mu); | |
8dd70bce AK |
10713 | |
10714 | while (mu.space_list) { | |
10715 | void **space = mu.space_list; | |
10716 | mu.space_list = *space; | |
10717 | free(space); | |
10718 | } | |
10719 | } | |
78b10e66 | 10720 | |
471bceb6 | 10721 | /*************************************************************************** |
694575e7 | 10722 | * Function: imsm_analyze_change |
471bceb6 | 10723 | * Description: Function analyze change for single volume |
1011e834 | 10724 | * and validate if transition is supported |
fbf3d202 AK |
10725 | * Parameters: Geometry parameters, supertype structure, |
10726 | * metadata change direction (apply/rollback) | |
694575e7 | 10727 | * Returns: Operation type code on success, -1 if fail |
471bceb6 KW |
10728 | ****************************************************************************/ |
10729 | enum imsm_reshape_type imsm_analyze_change(struct supertype *st, | |
fbf3d202 AK |
10730 | struct geo_params *geo, |
10731 | int direction) | |
694575e7 | 10732 | { |
471bceb6 KW |
10733 | struct mdinfo info; |
10734 | int change = -1; | |
10735 | int check_devs = 0; | |
c21e737b | 10736 | int chunk; |
67a2db32 AK |
10737 | /* number of added/removed disks in operation result */ |
10738 | int devNumChange = 0; | |
10739 | /* imsm compatible layout value for array geometry verification */ | |
10740 | int imsm_layout = -1; | |
7abc9871 AK |
10741 | int data_disks; |
10742 | struct imsm_dev *dev; | |
10743 | struct intel_super *super; | |
d04f65f4 | 10744 | unsigned long long current_size; |
65d38cca | 10745 | unsigned long long free_size; |
d04f65f4 | 10746 | unsigned long long max_size; |
65d38cca | 10747 | int rv; |
471bceb6 KW |
10748 | |
10749 | getinfo_super_imsm_volume(st, &info, NULL); | |
089f9d79 JS |
10750 | if (geo->level != info.array.level && geo->level >= 0 && |
10751 | geo->level != UnSet) { | |
471bceb6 KW |
10752 | switch (info.array.level) { |
10753 | case 0: | |
10754 | if (geo->level == 5) { | |
b5347799 | 10755 | change = CH_MIGRATION; |
e13ce846 | 10756 | if (geo->layout != ALGORITHM_LEFT_ASYMMETRIC) { |
7a862a02 | 10757 | pr_err("Error. Requested Layout not supported (left-asymmetric layout is supported only)!\n"); |
e13ce846 AK |
10758 | change = -1; |
10759 | goto analyse_change_exit; | |
10760 | } | |
67a2db32 | 10761 | imsm_layout = geo->layout; |
471bceb6 | 10762 | check_devs = 1; |
e91a3bad LM |
10763 | devNumChange = 1; /* parity disk added */ |
10764 | } else if (geo->level == 10) { | |
471bceb6 KW |
10765 | change = CH_TAKEOVER; |
10766 | check_devs = 1; | |
e91a3bad | 10767 | devNumChange = 2; /* two mirrors added */ |
67a2db32 | 10768 | imsm_layout = 0x102; /* imsm supported layout */ |
471bceb6 | 10769 | } |
dfe77a9e KW |
10770 | break; |
10771 | case 1: | |
471bceb6 KW |
10772 | case 10: |
10773 | if (geo->level == 0) { | |
10774 | change = CH_TAKEOVER; | |
10775 | check_devs = 1; | |
e91a3bad | 10776 | devNumChange = -(geo->raid_disks/2); |
67a2db32 | 10777 | imsm_layout = 0; /* imsm raid0 layout */ |
471bceb6 KW |
10778 | } |
10779 | break; | |
10780 | } | |
10781 | if (change == -1) { | |
7a862a02 | 10782 | pr_err("Error. Level Migration from %d to %d not supported!\n", |
e7b84f9d | 10783 | info.array.level, geo->level); |
471bceb6 KW |
10784 | goto analyse_change_exit; |
10785 | } | |
10786 | } else | |
10787 | geo->level = info.array.level; | |
10788 | ||
089f9d79 JS |
10789 | if (geo->layout != info.array.layout && |
10790 | (geo->layout != UnSet && geo->layout != -1)) { | |
b5347799 | 10791 | change = CH_MIGRATION; |
089f9d79 JS |
10792 | if (info.array.layout == 0 && info.array.level == 5 && |
10793 | geo->layout == 5) { | |
471bceb6 | 10794 | /* reshape 5 -> 4 */ |
089f9d79 JS |
10795 | } else if (info.array.layout == 5 && info.array.level == 5 && |
10796 | geo->layout == 0) { | |
471bceb6 KW |
10797 | /* reshape 4 -> 5 */ |
10798 | geo->layout = 0; | |
10799 | geo->level = 5; | |
10800 | } else { | |
7a862a02 | 10801 | pr_err("Error. Layout Migration from %d to %d not supported!\n", |
e7b84f9d | 10802 | info.array.layout, geo->layout); |
471bceb6 KW |
10803 | change = -1; |
10804 | goto analyse_change_exit; | |
10805 | } | |
67a2db32 | 10806 | } else { |
471bceb6 | 10807 | geo->layout = info.array.layout; |
67a2db32 AK |
10808 | if (imsm_layout == -1) |
10809 | imsm_layout = info.array.layout; | |
10810 | } | |
471bceb6 | 10811 | |
089f9d79 JS |
10812 | if (geo->chunksize > 0 && geo->chunksize != UnSet && |
10813 | geo->chunksize != info.array.chunk_size) { | |
2d2b0eb7 MD |
10814 | if (info.array.level == 10) { |
10815 | pr_err("Error. Chunk size change for RAID 10 is not supported.\n"); | |
10816 | change = -1; | |
10817 | goto analyse_change_exit; | |
1e9b2c3f PB |
10818 | } else if (info.component_size % (geo->chunksize/512)) { |
10819 | pr_err("New chunk size (%dK) does not evenly divide device size (%lluk). Aborting...\n", | |
10820 | geo->chunksize/1024, info.component_size/2); | |
10821 | change = -1; | |
10822 | goto analyse_change_exit; | |
2d2b0eb7 | 10823 | } |
b5347799 | 10824 | change = CH_MIGRATION; |
2d2b0eb7 | 10825 | } else { |
471bceb6 | 10826 | geo->chunksize = info.array.chunk_size; |
2d2b0eb7 | 10827 | } |
471bceb6 | 10828 | |
c21e737b | 10829 | chunk = geo->chunksize / 1024; |
7abc9871 AK |
10830 | |
10831 | super = st->sb; | |
10832 | dev = get_imsm_dev(super, super->current_vol); | |
10833 | data_disks = imsm_num_data_members(dev , MAP_0); | |
c41e00b2 | 10834 | /* compute current size per disk member |
7abc9871 | 10835 | */ |
c41e00b2 AK |
10836 | current_size = info.custom_array_size / data_disks; |
10837 | ||
089f9d79 | 10838 | if (geo->size > 0 && geo->size != MAX_SIZE) { |
c41e00b2 AK |
10839 | /* align component size |
10840 | */ | |
10841 | geo->size = imsm_component_size_aligment_check( | |
10842 | get_imsm_raid_level(dev->vol.map), | |
f36a9ecd | 10843 | chunk * 1024, super->sector_size, |
c41e00b2 | 10844 | geo->size * 2); |
65d0b4ce | 10845 | if (geo->size == 0) { |
7a862a02 | 10846 | pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is 0).\n", |
65d0b4ce LD |
10847 | current_size); |
10848 | goto analyse_change_exit; | |
10849 | } | |
c41e00b2 | 10850 | } |
7abc9871 | 10851 | |
089f9d79 | 10852 | if (current_size != geo->size && geo->size > 0) { |
7abc9871 | 10853 | if (change != -1) { |
7a862a02 | 10854 | pr_err("Error. Size change should be the only one at a time.\n"); |
7abc9871 AK |
10855 | change = -1; |
10856 | goto analyse_change_exit; | |
10857 | } | |
10858 | if ((super->current_vol + 1) != super->anchor->num_raid_devs) { | |
7a862a02 | 10859 | pr_err("Error. The last volume in container can be expanded only (%i/%s).\n", |
4dd2df09 | 10860 | super->current_vol, st->devnm); |
7abc9871 AK |
10861 | goto analyse_change_exit; |
10862 | } | |
65d38cca LD |
10863 | /* check the maximum available size |
10864 | */ | |
10865 | rv = imsm_get_free_size(st, dev->vol.map->num_members, | |
10866 | 0, chunk, &free_size); | |
10867 | if (rv == 0) | |
10868 | /* Cannot find maximum available space | |
10869 | */ | |
10870 | max_size = 0; | |
10871 | else { | |
10872 | max_size = free_size + current_size; | |
10873 | /* align component size | |
10874 | */ | |
10875 | max_size = imsm_component_size_aligment_check( | |
10876 | get_imsm_raid_level(dev->vol.map), | |
f36a9ecd | 10877 | chunk * 1024, super->sector_size, |
65d38cca LD |
10878 | max_size); |
10879 | } | |
d04f65f4 | 10880 | if (geo->size == MAX_SIZE) { |
b130333f AK |
10881 | /* requested size change to the maximum available size |
10882 | */ | |
65d38cca | 10883 | if (max_size == 0) { |
7a862a02 | 10884 | pr_err("Error. Cannot find maximum available space.\n"); |
b130333f AK |
10885 | change = -1; |
10886 | goto analyse_change_exit; | |
65d38cca LD |
10887 | } else |
10888 | geo->size = max_size; | |
c41e00b2 | 10889 | } |
b130333f | 10890 | |
681b7ae2 | 10891 | if (direction == ROLLBACK_METADATA_CHANGES) { |
fbf3d202 AK |
10892 | /* accept size for rollback only |
10893 | */ | |
10894 | } else { | |
10895 | /* round size due to metadata compatibility | |
10896 | */ | |
10897 | geo->size = (geo->size >> SECT_PER_MB_SHIFT) | |
10898 | << SECT_PER_MB_SHIFT; | |
10899 | dprintf("Prepare update for size change to %llu\n", | |
10900 | geo->size ); | |
10901 | if (current_size >= geo->size) { | |
7a862a02 | 10902 | pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is %llu).\n", |
e7b84f9d | 10903 | current_size, geo->size); |
fbf3d202 AK |
10904 | goto analyse_change_exit; |
10905 | } | |
65d38cca | 10906 | if (max_size && geo->size > max_size) { |
7a862a02 | 10907 | pr_err("Error. Requested size is larger than maximum available size (maximum available size is %llu, requested size /rounded/ is %llu).\n", |
e7b84f9d | 10908 | max_size, geo->size); |
65d38cca LD |
10909 | goto analyse_change_exit; |
10910 | } | |
7abc9871 AK |
10911 | } |
10912 | geo->size *= data_disks; | |
10913 | geo->raid_disks = dev->vol.map->num_members; | |
10914 | change = CH_ARRAY_SIZE; | |
10915 | } | |
471bceb6 KW |
10916 | if (!validate_geometry_imsm(st, |
10917 | geo->level, | |
67a2db32 | 10918 | imsm_layout, |
e91a3bad | 10919 | geo->raid_disks + devNumChange, |
c21e737b | 10920 | &chunk, |
af4348dd | 10921 | geo->size, INVALID_SECTORS, |
471bceb6 KW |
10922 | 0, 0, 1)) |
10923 | change = -1; | |
10924 | ||
10925 | if (check_devs) { | |
10926 | struct intel_super *super = st->sb; | |
10927 | struct imsm_super *mpb = super->anchor; | |
10928 | ||
10929 | if (mpb->num_raid_devs > 1) { | |
7a862a02 | 10930 | pr_err("Error. Cannot perform operation on %s- for this operation it MUST be single array in container\n", |
e7b84f9d | 10931 | geo->dev_name); |
471bceb6 KW |
10932 | change = -1; |
10933 | } | |
10934 | } | |
10935 | ||
10936 | analyse_change_exit: | |
089f9d79 JS |
10937 | if (direction == ROLLBACK_METADATA_CHANGES && |
10938 | (change == CH_MIGRATION || change == CH_TAKEOVER)) { | |
7a862a02 | 10939 | dprintf("imsm: Metadata changes rollback is not supported for migration and takeover operations.\n"); |
fbf3d202 AK |
10940 | change = -1; |
10941 | } | |
471bceb6 | 10942 | return change; |
694575e7 KW |
10943 | } |
10944 | ||
bb025c2f KW |
10945 | int imsm_takeover(struct supertype *st, struct geo_params *geo) |
10946 | { | |
10947 | struct intel_super *super = st->sb; | |
10948 | struct imsm_update_takeover *u; | |
10949 | ||
503975b9 | 10950 | u = xmalloc(sizeof(struct imsm_update_takeover)); |
bb025c2f KW |
10951 | |
10952 | u->type = update_takeover; | |
10953 | u->subarray = super->current_vol; | |
10954 | ||
10955 | /* 10->0 transition */ | |
10956 | if (geo->level == 0) | |
10957 | u->direction = R10_TO_R0; | |
10958 | ||
0529c688 KW |
10959 | /* 0->10 transition */ |
10960 | if (geo->level == 10) | |
10961 | u->direction = R0_TO_R10; | |
10962 | ||
bb025c2f KW |
10963 | /* update metadata locally */ |
10964 | imsm_update_metadata_locally(st, u, | |
10965 | sizeof(struct imsm_update_takeover)); | |
10966 | /* and possibly remotely */ | |
10967 | if (st->update_tail) | |
10968 | append_metadata_update(st, u, | |
10969 | sizeof(struct imsm_update_takeover)); | |
10970 | else | |
10971 | free(u); | |
10972 | ||
10973 | return 0; | |
10974 | } | |
10975 | ||
d04f65f4 N |
10976 | static int imsm_reshape_super(struct supertype *st, unsigned long long size, |
10977 | int level, | |
78b10e66 | 10978 | int layout, int chunksize, int raid_disks, |
41784c88 | 10979 | int delta_disks, char *backup, char *dev, |
016e00f5 | 10980 | int direction, int verbose) |
78b10e66 | 10981 | { |
78b10e66 N |
10982 | int ret_val = 1; |
10983 | struct geo_params geo; | |
10984 | ||
1ade5cc1 | 10985 | dprintf("(enter)\n"); |
78b10e66 | 10986 | |
71204a50 | 10987 | memset(&geo, 0, sizeof(struct geo_params)); |
78b10e66 N |
10988 | |
10989 | geo.dev_name = dev; | |
4dd2df09 | 10990 | strcpy(geo.devnm, st->devnm); |
78b10e66 N |
10991 | geo.size = size; |
10992 | geo.level = level; | |
10993 | geo.layout = layout; | |
10994 | geo.chunksize = chunksize; | |
10995 | geo.raid_disks = raid_disks; | |
41784c88 AK |
10996 | if (delta_disks != UnSet) |
10997 | geo.raid_disks += delta_disks; | |
78b10e66 | 10998 | |
1ade5cc1 N |
10999 | dprintf("for level : %i\n", geo.level); |
11000 | dprintf("for raid_disks : %i\n", geo.raid_disks); | |
78b10e66 N |
11001 | |
11002 | if (experimental() == 0) | |
11003 | return ret_val; | |
11004 | ||
4dd2df09 | 11005 | if (strcmp(st->container_devnm, st->devnm) == 0) { |
694575e7 KW |
11006 | /* On container level we can only increase number of devices. */ |
11007 | dprintf("imsm: info: Container operation\n"); | |
78b10e66 | 11008 | int old_raid_disks = 0; |
6dc0be30 | 11009 | |
78b10e66 | 11010 | if (imsm_reshape_is_allowed_on_container( |
fbf3d202 | 11011 | st, &geo, &old_raid_disks, direction)) { |
78b10e66 N |
11012 | struct imsm_update_reshape *u = NULL; |
11013 | int len; | |
11014 | ||
11015 | len = imsm_create_metadata_update_for_reshape( | |
11016 | st, &geo, old_raid_disks, &u); | |
11017 | ||
ed08d51c AK |
11018 | if (len <= 0) { |
11019 | dprintf("imsm: Cannot prepare update\n"); | |
11020 | goto exit_imsm_reshape_super; | |
11021 | } | |
11022 | ||
8dd70bce AK |
11023 | ret_val = 0; |
11024 | /* update metadata locally */ | |
11025 | imsm_update_metadata_locally(st, u, len); | |
11026 | /* and possibly remotely */ | |
11027 | if (st->update_tail) | |
11028 | append_metadata_update(st, u, len); | |
11029 | else | |
ed08d51c | 11030 | free(u); |
8dd70bce | 11031 | |
694575e7 | 11032 | } else { |
7a862a02 | 11033 | pr_err("(imsm) Operation is not allowed on this container\n"); |
694575e7 KW |
11034 | } |
11035 | } else { | |
11036 | /* On volume level we support following operations | |
471bceb6 KW |
11037 | * - takeover: raid10 -> raid0; raid0 -> raid10 |
11038 | * - chunk size migration | |
11039 | * - migration: raid5 -> raid0; raid0 -> raid5 | |
11040 | */ | |
11041 | struct intel_super *super = st->sb; | |
11042 | struct intel_dev *dev = super->devlist; | |
4dd2df09 | 11043 | int change; |
694575e7 | 11044 | dprintf("imsm: info: Volume operation\n"); |
471bceb6 KW |
11045 | /* find requested device */ |
11046 | while (dev) { | |
1011e834 | 11047 | char *devnm = |
4dd2df09 N |
11048 | imsm_find_array_devnm_by_subdev( |
11049 | dev->index, st->container_devnm); | |
11050 | if (devnm && strcmp(devnm, geo.devnm) == 0) | |
471bceb6 KW |
11051 | break; |
11052 | dev = dev->next; | |
11053 | } | |
11054 | if (dev == NULL) { | |
4dd2df09 N |
11055 | pr_err("Cannot find %s (%s) subarray\n", |
11056 | geo.dev_name, geo.devnm); | |
471bceb6 KW |
11057 | goto exit_imsm_reshape_super; |
11058 | } | |
11059 | super->current_vol = dev->index; | |
fbf3d202 | 11060 | change = imsm_analyze_change(st, &geo, direction); |
694575e7 | 11061 | switch (change) { |
471bceb6 | 11062 | case CH_TAKEOVER: |
bb025c2f | 11063 | ret_val = imsm_takeover(st, &geo); |
694575e7 | 11064 | break; |
48c5303a PC |
11065 | case CH_MIGRATION: { |
11066 | struct imsm_update_reshape_migration *u = NULL; | |
11067 | int len = | |
11068 | imsm_create_metadata_update_for_migration( | |
11069 | st, &geo, &u); | |
11070 | if (len < 1) { | |
7a862a02 | 11071 | dprintf("imsm: Cannot prepare update\n"); |
48c5303a PC |
11072 | break; |
11073 | } | |
471bceb6 | 11074 | ret_val = 0; |
48c5303a PC |
11075 | /* update metadata locally */ |
11076 | imsm_update_metadata_locally(st, u, len); | |
11077 | /* and possibly remotely */ | |
11078 | if (st->update_tail) | |
11079 | append_metadata_update(st, u, len); | |
11080 | else | |
11081 | free(u); | |
11082 | } | |
11083 | break; | |
7abc9871 | 11084 | case CH_ARRAY_SIZE: { |
f3871fdc AK |
11085 | struct imsm_update_size_change *u = NULL; |
11086 | int len = | |
11087 | imsm_create_metadata_update_for_size_change( | |
11088 | st, &geo, &u); | |
11089 | if (len < 1) { | |
7a862a02 | 11090 | dprintf("imsm: Cannot prepare update\n"); |
f3871fdc AK |
11091 | break; |
11092 | } | |
11093 | ret_val = 0; | |
11094 | /* update metadata locally */ | |
11095 | imsm_update_metadata_locally(st, u, len); | |
11096 | /* and possibly remotely */ | |
11097 | if (st->update_tail) | |
11098 | append_metadata_update(st, u, len); | |
11099 | else | |
11100 | free(u); | |
7abc9871 AK |
11101 | } |
11102 | break; | |
471bceb6 KW |
11103 | default: |
11104 | ret_val = 1; | |
694575e7 | 11105 | } |
694575e7 | 11106 | } |
78b10e66 | 11107 | |
ed08d51c | 11108 | exit_imsm_reshape_super: |
78b10e66 N |
11109 | dprintf("imsm: reshape_super Exit code = %i\n", ret_val); |
11110 | return ret_val; | |
11111 | } | |
2cda7640 | 11112 | |
0febb20c AO |
11113 | #define COMPLETED_OK 0 |
11114 | #define COMPLETED_NONE 1 | |
11115 | #define COMPLETED_DELAYED 2 | |
11116 | ||
11117 | static int read_completed(int fd, unsigned long long *val) | |
11118 | { | |
11119 | int ret; | |
11120 | char buf[50]; | |
11121 | ||
11122 | ret = sysfs_fd_get_str(fd, buf, 50); | |
11123 | if (ret < 0) | |
11124 | return ret; | |
11125 | ||
11126 | ret = COMPLETED_OK; | |
11127 | if (strncmp(buf, "none", 4) == 0) { | |
11128 | ret = COMPLETED_NONE; | |
11129 | } else if (strncmp(buf, "delayed", 7) == 0) { | |
11130 | ret = COMPLETED_DELAYED; | |
11131 | } else { | |
11132 | char *ep; | |
11133 | *val = strtoull(buf, &ep, 0); | |
11134 | if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' ')) | |
11135 | ret = -1; | |
11136 | } | |
11137 | return ret; | |
11138 | } | |
11139 | ||
eee67a47 AK |
11140 | /******************************************************************************* |
11141 | * Function: wait_for_reshape_imsm | |
11142 | * Description: Function writes new sync_max value and waits until | |
11143 | * reshape process reach new position | |
11144 | * Parameters: | |
11145 | * sra : general array info | |
eee67a47 AK |
11146 | * ndata : number of disks in new array's layout |
11147 | * Returns: | |
11148 | * 0 : success, | |
11149 | * 1 : there is no reshape in progress, | |
11150 | * -1 : fail | |
11151 | ******************************************************************************/ | |
ae9f01f8 | 11152 | int wait_for_reshape_imsm(struct mdinfo *sra, int ndata) |
eee67a47 | 11153 | { |
85ca499c | 11154 | int fd = sysfs_get_fd(sra, NULL, "sync_completed"); |
df2647fa | 11155 | int retry = 3; |
eee67a47 | 11156 | unsigned long long completed; |
ae9f01f8 AK |
11157 | /* to_complete : new sync_max position */ |
11158 | unsigned long long to_complete = sra->reshape_progress; | |
11159 | unsigned long long position_to_set = to_complete / ndata; | |
eee67a47 | 11160 | |
ae9f01f8 | 11161 | if (fd < 0) { |
1ade5cc1 | 11162 | dprintf("cannot open reshape_position\n"); |
eee67a47 | 11163 | return 1; |
ae9f01f8 | 11164 | } |
eee67a47 | 11165 | |
df2647fa PB |
11166 | do { |
11167 | if (sysfs_fd_get_ll(fd, &completed) < 0) { | |
11168 | if (!retry) { | |
11169 | dprintf("cannot read reshape_position (no reshape in progres)\n"); | |
11170 | close(fd); | |
11171 | return 1; | |
11172 | } | |
11173 | usleep(30000); | |
11174 | } else | |
11175 | break; | |
11176 | } while (retry--); | |
eee67a47 | 11177 | |
85ca499c | 11178 | if (completed > position_to_set) { |
1ade5cc1 | 11179 | dprintf("wrong next position to set %llu (%llu)\n", |
85ca499c | 11180 | to_complete, position_to_set); |
ae9f01f8 AK |
11181 | close(fd); |
11182 | return -1; | |
11183 | } | |
11184 | dprintf("Position set: %llu\n", position_to_set); | |
11185 | if (sysfs_set_num(sra, NULL, "sync_max", | |
11186 | position_to_set) != 0) { | |
1ade5cc1 | 11187 | dprintf("cannot set reshape position to %llu\n", |
ae9f01f8 AK |
11188 | position_to_set); |
11189 | close(fd); | |
11190 | return -1; | |
eee67a47 AK |
11191 | } |
11192 | ||
eee67a47 | 11193 | do { |
0febb20c | 11194 | int rc; |
eee67a47 | 11195 | char action[20]; |
5ff3a780 | 11196 | int timeout = 3000; |
0febb20c | 11197 | |
5ff3a780 | 11198 | sysfs_wait(fd, &timeout); |
a47e44fb AK |
11199 | if (sysfs_get_str(sra, NULL, "sync_action", |
11200 | action, 20) > 0 && | |
d7d3809a | 11201 | strncmp(action, "reshape", 7) != 0) { |
b2be2b62 AO |
11202 | if (strncmp(action, "idle", 4) == 0) |
11203 | break; | |
d7d3809a AP |
11204 | close(fd); |
11205 | return -1; | |
11206 | } | |
0febb20c AO |
11207 | |
11208 | rc = read_completed(fd, &completed); | |
11209 | if (rc < 0) { | |
1ade5cc1 | 11210 | dprintf("cannot read reshape_position (in loop)\n"); |
eee67a47 AK |
11211 | close(fd); |
11212 | return 1; | |
0febb20c AO |
11213 | } else if (rc == COMPLETED_NONE) |
11214 | break; | |
85ca499c | 11215 | } while (completed < position_to_set); |
b2be2b62 | 11216 | |
eee67a47 AK |
11217 | close(fd); |
11218 | return 0; | |
eee67a47 AK |
11219 | } |
11220 | ||
b915c95f AK |
11221 | /******************************************************************************* |
11222 | * Function: check_degradation_change | |
11223 | * Description: Check that array hasn't become failed. | |
11224 | * Parameters: | |
11225 | * info : for sysfs access | |
11226 | * sources : source disks descriptors | |
11227 | * degraded: previous degradation level | |
11228 | * Returns: | |
11229 | * degradation level | |
11230 | ******************************************************************************/ | |
11231 | int check_degradation_change(struct mdinfo *info, | |
11232 | int *sources, | |
11233 | int degraded) | |
11234 | { | |
11235 | unsigned long long new_degraded; | |
e1993023 LD |
11236 | int rv; |
11237 | ||
11238 | rv = sysfs_get_ll(info, NULL, "degraded", &new_degraded); | |
089f9d79 | 11239 | if (rv == -1 || (new_degraded != (unsigned long long)degraded)) { |
b915c95f AK |
11240 | /* check each device to ensure it is still working */ |
11241 | struct mdinfo *sd; | |
11242 | new_degraded = 0; | |
11243 | for (sd = info->devs ; sd ; sd = sd->next) { | |
11244 | if (sd->disk.state & (1<<MD_DISK_FAULTY)) | |
11245 | continue; | |
11246 | if (sd->disk.state & (1<<MD_DISK_SYNC)) { | |
cf52eff5 TM |
11247 | char sbuf[100]; |
11248 | ||
b915c95f | 11249 | if (sysfs_get_str(info, |
cf52eff5 | 11250 | sd, "state", sbuf, sizeof(sbuf)) < 0 || |
b915c95f AK |
11251 | strstr(sbuf, "faulty") || |
11252 | strstr(sbuf, "in_sync") == NULL) { | |
11253 | /* this device is dead */ | |
11254 | sd->disk.state = (1<<MD_DISK_FAULTY); | |
11255 | if (sd->disk.raid_disk >= 0 && | |
11256 | sources[sd->disk.raid_disk] >= 0) { | |
11257 | close(sources[ | |
11258 | sd->disk.raid_disk]); | |
11259 | sources[sd->disk.raid_disk] = | |
11260 | -1; | |
11261 | } | |
11262 | new_degraded++; | |
11263 | } | |
11264 | } | |
11265 | } | |
11266 | } | |
11267 | ||
11268 | return new_degraded; | |
11269 | } | |
11270 | ||
10f22854 AK |
11271 | /******************************************************************************* |
11272 | * Function: imsm_manage_reshape | |
11273 | * Description: Function finds array under reshape and it manages reshape | |
11274 | * process. It creates stripes backups (if required) and sets | |
942e1cdb | 11275 | * checkpoints. |
10f22854 AK |
11276 | * Parameters: |
11277 | * afd : Backup handle (nattive) - not used | |
11278 | * sra : general array info | |
11279 | * reshape : reshape parameters - not used | |
11280 | * st : supertype structure | |
11281 | * blocks : size of critical section [blocks] | |
11282 | * fds : table of source device descriptor | |
11283 | * offsets : start of array (offest per devices) | |
11284 | * dests : not used | |
11285 | * destfd : table of destination device descriptor | |
11286 | * destoffsets : table of destination offsets (per device) | |
11287 | * Returns: | |
11288 | * 1 : success, reshape is done | |
11289 | * 0 : fail | |
11290 | ******************************************************************************/ | |
999b4972 N |
11291 | static int imsm_manage_reshape( |
11292 | int afd, struct mdinfo *sra, struct reshape *reshape, | |
10f22854 | 11293 | struct supertype *st, unsigned long backup_blocks, |
999b4972 N |
11294 | int *fds, unsigned long long *offsets, |
11295 | int dests, int *destfd, unsigned long long *destoffsets) | |
11296 | { | |
10f22854 AK |
11297 | int ret_val = 0; |
11298 | struct intel_super *super = st->sb; | |
594dc1b8 | 11299 | struct intel_dev *dv; |
de44e46f | 11300 | unsigned int sector_size = super->sector_size; |
10f22854 | 11301 | struct imsm_dev *dev = NULL; |
a6b6d984 | 11302 | struct imsm_map *map_src; |
10f22854 AK |
11303 | int migr_vol_qan = 0; |
11304 | int ndata, odata; /* [bytes] */ | |
11305 | int chunk; /* [bytes] */ | |
11306 | struct migr_record *migr_rec; | |
11307 | char *buf = NULL; | |
11308 | unsigned int buf_size; /* [bytes] */ | |
11309 | unsigned long long max_position; /* array size [bytes] */ | |
11310 | unsigned long long next_step; /* [blocks]/[bytes] */ | |
11311 | unsigned long long old_data_stripe_length; | |
10f22854 AK |
11312 | unsigned long long start_src; /* [bytes] */ |
11313 | unsigned long long start; /* [bytes] */ | |
11314 | unsigned long long start_buf_shift; /* [bytes] */ | |
b915c95f | 11315 | int degraded = 0; |
ab724b98 | 11316 | int source_layout = 0; |
10f22854 | 11317 | |
79a16a9b JS |
11318 | if (!sra) |
11319 | return ret_val; | |
11320 | ||
11321 | if (!fds || !offsets) | |
10f22854 AK |
11322 | goto abort; |
11323 | ||
11324 | /* Find volume during the reshape */ | |
11325 | for (dv = super->devlist; dv; dv = dv->next) { | |
11326 | if (dv->dev->vol.migr_type == MIGR_GEN_MIGR | |
11327 | && dv->dev->vol.migr_state == 1) { | |
11328 | dev = dv->dev; | |
11329 | migr_vol_qan++; | |
11330 | } | |
11331 | } | |
11332 | /* Only one volume can migrate at the same time */ | |
11333 | if (migr_vol_qan != 1) { | |
676e87a8 | 11334 | pr_err("%s", migr_vol_qan ? |
10f22854 AK |
11335 | "Number of migrating volumes greater than 1\n" : |
11336 | "There is no volume during migrationg\n"); | |
11337 | goto abort; | |
11338 | } | |
11339 | ||
238c0a71 | 11340 | map_src = get_imsm_map(dev, MAP_1); |
10f22854 AK |
11341 | if (map_src == NULL) |
11342 | goto abort; | |
10f22854 | 11343 | |
238c0a71 AK |
11344 | ndata = imsm_num_data_members(dev, MAP_0); |
11345 | odata = imsm_num_data_members(dev, MAP_1); | |
10f22854 | 11346 | |
7b1ab482 | 11347 | chunk = __le16_to_cpu(map_src->blocks_per_strip) * 512; |
10f22854 AK |
11348 | old_data_stripe_length = odata * chunk; |
11349 | ||
11350 | migr_rec = super->migr_rec; | |
11351 | ||
10f22854 AK |
11352 | /* initialize migration record for start condition */ |
11353 | if (sra->reshape_progress == 0) | |
11354 | init_migr_record_imsm(st, dev, sra); | |
b2c59438 AK |
11355 | else { |
11356 | if (__le32_to_cpu(migr_rec->rec_status) != UNIT_SRC_NORMAL) { | |
7a862a02 | 11357 | dprintf("imsm: cannot restart migration when data are present in copy area.\n"); |
b2c59438 AK |
11358 | goto abort; |
11359 | } | |
6a75c8ca AK |
11360 | /* Save checkpoint to update migration record for current |
11361 | * reshape position (in md). It can be farther than current | |
11362 | * reshape position in metadata. | |
11363 | */ | |
11364 | if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL) == 1) { | |
11365 | /* ignore error == 2, this can mean end of reshape here | |
11366 | */ | |
7a862a02 | 11367 | dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL, initial save)\n"); |
6a75c8ca AK |
11368 | goto abort; |
11369 | } | |
b2c59438 | 11370 | } |
10f22854 AK |
11371 | |
11372 | /* size for data */ | |
11373 | buf_size = __le32_to_cpu(migr_rec->blocks_per_unit) * 512; | |
11374 | /* extend buffer size for parity disk */ | |
11375 | buf_size += __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512; | |
11376 | /* add space for stripe aligment */ | |
11377 | buf_size += old_data_stripe_length; | |
de44e46f PB |
11378 | if (posix_memalign((void **)&buf, MAX_SECTOR_SIZE, buf_size)) { |
11379 | dprintf("imsm: Cannot allocate checkpoint buffer\n"); | |
10f22854 AK |
11380 | goto abort; |
11381 | } | |
11382 | ||
3ef4403c | 11383 | max_position = sra->component_size * ndata; |
68eb8bc6 | 11384 | source_layout = imsm_level_to_layout(map_src->raid_level); |
10f22854 AK |
11385 | |
11386 | while (__le32_to_cpu(migr_rec->curr_migr_unit) < | |
11387 | __le32_to_cpu(migr_rec->num_migr_units)) { | |
11388 | /* current reshape position [blocks] */ | |
11389 | unsigned long long current_position = | |
11390 | __le32_to_cpu(migr_rec->blocks_per_unit) | |
11391 | * __le32_to_cpu(migr_rec->curr_migr_unit); | |
11392 | unsigned long long border; | |
11393 | ||
b915c95f AK |
11394 | /* Check that array hasn't become failed. |
11395 | */ | |
11396 | degraded = check_degradation_change(sra, fds, degraded); | |
11397 | if (degraded > 1) { | |
7a862a02 | 11398 | dprintf("imsm: Abort reshape due to degradation level (%i)\n", degraded); |
b915c95f AK |
11399 | goto abort; |
11400 | } | |
11401 | ||
10f22854 AK |
11402 | next_step = __le32_to_cpu(migr_rec->blocks_per_unit); |
11403 | ||
11404 | if ((current_position + next_step) > max_position) | |
11405 | next_step = max_position - current_position; | |
11406 | ||
92144abf | 11407 | start = current_position * 512; |
10f22854 | 11408 | |
942e1cdb | 11409 | /* align reading start to old geometry */ |
10f22854 AK |
11410 | start_buf_shift = start % old_data_stripe_length; |
11411 | start_src = start - start_buf_shift; | |
11412 | ||
11413 | border = (start_src / odata) - (start / ndata); | |
11414 | border /= 512; | |
11415 | if (border <= __le32_to_cpu(migr_rec->dest_depth_per_unit)) { | |
11416 | /* save critical stripes to buf | |
11417 | * start - start address of current unit | |
11418 | * to backup [bytes] | |
11419 | * start_src - start address of current unit | |
11420 | * to backup alligned to source array | |
11421 | * [bytes] | |
11422 | */ | |
594dc1b8 | 11423 | unsigned long long next_step_filler; |
10f22854 AK |
11424 | unsigned long long copy_length = next_step * 512; |
11425 | ||
11426 | /* allign copy area length to stripe in old geometry */ | |
11427 | next_step_filler = ((copy_length + start_buf_shift) | |
11428 | % old_data_stripe_length); | |
11429 | if (next_step_filler) | |
11430 | next_step_filler = (old_data_stripe_length | |
11431 | - next_step_filler); | |
7a862a02 | 11432 | 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 |
11433 | start, start_src, copy_length, |
11434 | start_buf_shift, next_step_filler); | |
11435 | ||
11436 | if (save_stripes(fds, offsets, map_src->num_members, | |
ab724b98 AK |
11437 | chunk, map_src->raid_level, |
11438 | source_layout, 0, NULL, start_src, | |
10f22854 AK |
11439 | copy_length + |
11440 | next_step_filler + start_buf_shift, | |
11441 | buf)) { | |
7a862a02 | 11442 | dprintf("imsm: Cannot save stripes to buffer\n"); |
10f22854 AK |
11443 | goto abort; |
11444 | } | |
11445 | /* Convert data to destination format and store it | |
11446 | * in backup general migration area | |
11447 | */ | |
11448 | if (save_backup_imsm(st, dev, sra, | |
aea93171 | 11449 | buf + start_buf_shift, copy_length)) { |
7a862a02 | 11450 | dprintf("imsm: Cannot save stripes to target devices\n"); |
10f22854 AK |
11451 | goto abort; |
11452 | } | |
11453 | if (save_checkpoint_imsm(st, sra, | |
11454 | UNIT_SRC_IN_CP_AREA)) { | |
7a862a02 | 11455 | dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_IN_CP_AREA)\n"); |
10f22854 AK |
11456 | goto abort; |
11457 | } | |
8016a6d4 AK |
11458 | } else { |
11459 | /* set next step to use whole border area */ | |
11460 | border /= next_step; | |
11461 | if (border > 1) | |
11462 | next_step *= border; | |
10f22854 AK |
11463 | } |
11464 | /* When data backed up, checkpoint stored, | |
11465 | * kick the kernel to reshape unit of data | |
11466 | */ | |
11467 | next_step = next_step + sra->reshape_progress; | |
8016a6d4 AK |
11468 | /* limit next step to array max position */ |
11469 | if (next_step > max_position) | |
11470 | next_step = max_position; | |
10f22854 AK |
11471 | sysfs_set_num(sra, NULL, "suspend_lo", sra->reshape_progress); |
11472 | sysfs_set_num(sra, NULL, "suspend_hi", next_step); | |
ae9f01f8 | 11473 | sra->reshape_progress = next_step; |
10f22854 AK |
11474 | |
11475 | /* wait until reshape finish */ | |
c85338c6 | 11476 | if (wait_for_reshape_imsm(sra, ndata)) { |
c47b0ff6 AK |
11477 | dprintf("wait_for_reshape_imsm returned error!\n"); |
11478 | goto abort; | |
11479 | } | |
84d11e6c N |
11480 | if (sigterm) |
11481 | goto abort; | |
10f22854 | 11482 | |
0228d92c AK |
11483 | if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL) == 1) { |
11484 | /* ignore error == 2, this can mean end of reshape here | |
11485 | */ | |
7a862a02 | 11486 | dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL)\n"); |
10f22854 AK |
11487 | goto abort; |
11488 | } | |
11489 | ||
11490 | } | |
11491 | ||
71e5411e PB |
11492 | /* clear migr_rec on disks after successful migration */ |
11493 | struct dl *d; | |
11494 | ||
de44e46f | 11495 | memset(super->migr_rec_buf, 0, MIGR_REC_BUF_SECTORS*sector_size); |
71e5411e PB |
11496 | for (d = super->disks; d; d = d->next) { |
11497 | if (d->index < 0 || is_failed(&d->disk)) | |
11498 | continue; | |
11499 | unsigned long long dsize; | |
11500 | ||
11501 | get_dev_size(d->fd, NULL, &dsize); | |
de44e46f | 11502 | if (lseek64(d->fd, dsize - MIGR_REC_SECTOR_POSITION*sector_size, |
71e5411e PB |
11503 | SEEK_SET) >= 0) { |
11504 | if (write(d->fd, super->migr_rec_buf, | |
de44e46f PB |
11505 | MIGR_REC_BUF_SECTORS*sector_size) != |
11506 | MIGR_REC_BUF_SECTORS*sector_size) | |
71e5411e PB |
11507 | perror("Write migr_rec failed"); |
11508 | } | |
11509 | } | |
11510 | ||
10f22854 AK |
11511 | /* return '1' if done */ |
11512 | ret_val = 1; | |
11513 | abort: | |
11514 | free(buf); | |
942e1cdb N |
11515 | /* See Grow.c: abort_reshape() for further explanation */ |
11516 | sysfs_set_num(sra, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL); | |
11517 | sysfs_set_num(sra, NULL, "suspend_hi", 0); | |
11518 | sysfs_set_num(sra, NULL, "suspend_lo", 0); | |
10f22854 AK |
11519 | |
11520 | return ret_val; | |
999b4972 | 11521 | } |
0c21b485 | 11522 | |
71204a50 | 11523 | #endif /* MDASSEMBLE */ |
999b4972 | 11524 | |
cdddbdbc DW |
11525 | struct superswitch super_imsm = { |
11526 | #ifndef MDASSEMBLE | |
11527 | .examine_super = examine_super_imsm, | |
11528 | .brief_examine_super = brief_examine_super_imsm, | |
4737ae25 | 11529 | .brief_examine_subarrays = brief_examine_subarrays_imsm, |
9d84c8ea | 11530 | .export_examine_super = export_examine_super_imsm, |
cdddbdbc DW |
11531 | .detail_super = detail_super_imsm, |
11532 | .brief_detail_super = brief_detail_super_imsm, | |
bf5a934a | 11533 | .write_init_super = write_init_super_imsm, |
0e600426 N |
11534 | .validate_geometry = validate_geometry_imsm, |
11535 | .add_to_super = add_to_super_imsm, | |
1a64be56 | 11536 | .remove_from_super = remove_from_super_imsm, |
d665cc31 | 11537 | .detail_platform = detail_platform_imsm, |
e50cf220 | 11538 | .export_detail_platform = export_detail_platform_imsm, |
33414a01 | 11539 | .kill_subarray = kill_subarray_imsm, |
aa534678 | 11540 | .update_subarray = update_subarray_imsm, |
2b959fbf | 11541 | .load_container = load_container_imsm, |
71204a50 N |
11542 | .default_geometry = default_geometry_imsm, |
11543 | .get_disk_controller_domain = imsm_get_disk_controller_domain, | |
11544 | .reshape_super = imsm_reshape_super, | |
11545 | .manage_reshape = imsm_manage_reshape, | |
9e2d750d | 11546 | .recover_backup = recover_backup_imsm, |
74db60b0 | 11547 | .copy_metadata = copy_metadata_imsm, |
27156a57 | 11548 | .examine_badblocks = examine_badblocks_imsm, |
cdddbdbc DW |
11549 | #endif |
11550 | .match_home = match_home_imsm, | |
11551 | .uuid_from_super= uuid_from_super_imsm, | |
11552 | .getinfo_super = getinfo_super_imsm, | |
5c4cd5da | 11553 | .getinfo_super_disks = getinfo_super_disks_imsm, |
cdddbdbc DW |
11554 | .update_super = update_super_imsm, |
11555 | ||
11556 | .avail_size = avail_size_imsm, | |
80e7f8c3 | 11557 | .min_acceptable_spare_size = min_acceptable_spare_size_imsm, |
cdddbdbc DW |
11558 | |
11559 | .compare_super = compare_super_imsm, | |
11560 | ||
11561 | .load_super = load_super_imsm, | |
bf5a934a | 11562 | .init_super = init_super_imsm, |
e683ca88 | 11563 | .store_super = store_super_imsm, |
cdddbdbc DW |
11564 | .free_super = free_super_imsm, |
11565 | .match_metadata_desc = match_metadata_desc_imsm, | |
bf5a934a | 11566 | .container_content = container_content_imsm, |
0c21b485 | 11567 | .validate_container = validate_container_imsm, |
cdddbdbc | 11568 | |
cdddbdbc | 11569 | .external = 1, |
4cce4069 | 11570 | .name = "imsm", |
845dea95 | 11571 | |
0e600426 | 11572 | #ifndef MDASSEMBLE |
845dea95 NB |
11573 | /* for mdmon */ |
11574 | .open_new = imsm_open_new, | |
ed9d66aa | 11575 | .set_array_state= imsm_set_array_state, |
845dea95 NB |
11576 | .set_disk = imsm_set_disk, |
11577 | .sync_metadata = imsm_sync_metadata, | |
88758e9d | 11578 | .activate_spare = imsm_activate_spare, |
e8319a19 | 11579 | .process_update = imsm_process_update, |
8273f55e | 11580 | .prepare_update = imsm_prepare_update, |
6f50473f | 11581 | .record_bad_block = imsm_record_badblock, |
c07a5a4f | 11582 | .clear_bad_block = imsm_clear_badblock, |
928f1424 | 11583 | .get_bad_blocks = imsm_get_badblocks, |
0e600426 | 11584 | #endif /* MDASSEMBLE */ |
cdddbdbc | 11585 | }; |