]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * mdadm - Intel(R) Matrix Storage Manager Support | |
3 | * | |
4 | * Copyright (C) 2002-2008 Intel Corporation | |
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 | ||
20 | #define HAVE_STDINT_H 1 | |
21 | #include "mdadm.h" | |
22 | #include "mdmon.h" | |
23 | #include "sha1.h" | |
24 | #include "platform-intel.h" | |
25 | #include <values.h> | |
26 | #include <scsi/sg.h> | |
27 | #include <ctype.h> | |
28 | #include <dirent.h> | |
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" | |
35 | #define MPB_VERSION_MANY_VOLUMES_PER_ARRAY "1.2.00" | |
36 | #define MPB_VERSION_3OR4_DISK_ARRAY "1.2.01" | |
37 | #define MPB_VERSION_RAID5 "1.2.02" | |
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" | |
41 | #define MAX_SIGNATURE_LENGTH 32 | |
42 | #define MAX_RAID_SERIAL_LEN 16 | |
43 | ||
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 | */ | |
77 | #define MPB_ATTRIB_SUPPORTED (MPB_ATTRIB_CHECKSUM_VERIFY | \ | |
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 | \ | |
84 | MPB_ATTRIB_EXP_STRIPE_SIZE | \ | |
85 | MPB_ATTRIB_BBM) | |
86 | ||
87 | /* Define attributes that are unused but not harmful */ | |
88 | #define MPB_ATTRIB_IGNORED (MPB_ATTRIB_NEVER_USE) | |
89 | ||
90 | #define MPB_SECTOR_CNT 2210 | |
91 | #define IMSM_RESERVED_SECTORS 4096 | |
92 | #define NUM_BLOCKS_DIRTY_STRIPE_REGION 2056 | |
93 | #define SECT_PER_MB_SHIFT 11 | |
94 | #define MAX_SECTOR_SIZE 4096 | |
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 */ | |
100 | __u32 total_blocks_lo; /* 0xE8 - 0xEB total blocks lo */ | |
101 | __u32 scsi_id; /* 0xEC - 0xEF scsi ID */ | |
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 */ | |
105 | __u32 status; /* 0xF0 - 0xF3 */ | |
106 | __u32 owner_cfg_num; /* which config 0,1,2... owns this disk */ | |
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 */ | |
110 | }; | |
111 | ||
112 | /* map selector for map managment | |
113 | */ | |
114 | #define MAP_0 0 | |
115 | #define MAP_1 1 | |
116 | #define MAP_X -1 | |
117 | ||
118 | /* RAID map configuration infos. */ | |
119 | struct imsm_map { | |
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 */ | |
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 | |
127 | #define IMSM_T_STATE_DEGRADED 2 | |
128 | #define IMSM_T_STATE_FAILED 3 | |
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 */ | |
134 | __u8 num_domains; /* number of parity domains */ | |
135 | __u8 failed_disk_num; /* valid only when state is degraded */ | |
136 | __u8 ddf; | |
137 | __u32 pba_of_lba0_hi; | |
138 | __u32 blocks_per_member_hi; | |
139 | __u32 num_data_stripes_hi; | |
140 | __u32 filler[4]; /* expansion area */ | |
141 | #define IMSM_ORD_REBUILD (1 << 24) | |
142 | __u32 disk_ord_tbl[1]; /* disk_ord_tbl[num_members], | |
143 | * top byte contains some flags | |
144 | */ | |
145 | } __attribute__ ((packed)); | |
146 | ||
147 | struct imsm_vol { | |
148 | __u32 curr_migr_unit; | |
149 | __u32 checkpoint_id; /* id to access curr_migr_unit */ | |
150 | __u8 migr_state; /* Normal or Migrating */ | |
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 | |
156 | #define MIGR_REPAIR 5 | |
157 | __u8 migr_type; /* Initializing, Rebuilding, ... */ | |
158 | __u8 dirty; | |
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]; | |
163 | struct imsm_map map[1]; | |
164 | /* here comes another one if migr_state */ | |
165 | } __attribute__ ((packed)); | |
166 | ||
167 | struct imsm_dev { | |
168 | __u8 volume[MAX_RAID_SERIAL_LEN]; | |
169 | __u32 size_low; | |
170 | __u32 size_high; | |
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) | |
184 | __u32 status; /* Persistent RaidDev status */ | |
185 | __u32 reserved_blocks; /* Reserved blocks at beginning of volume */ | |
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 | |
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 */ | |
204 | __u32 error_log_size; /* 0x30 - 0x33 in bytes */ | |
205 | __u32 attributes; /* 0x34 - 0x37 */ | |
206 | __u8 num_disks; /* 0x38 Number of configured disks */ | |
207 | __u8 num_raid_devs; /* 0x39 Number of configured volumes */ | |
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 */ | |
216 | struct imsm_disk disk[1]; /* 0xD8 diskTbl[numDisks] */ | |
217 | /* here comes imsm_dev[num_raid_devs] */ | |
218 | /* here comes BBM logs */ | |
219 | } __attribute__ ((packed)); | |
220 | ||
221 | #define BBM_LOG_MAX_ENTRIES 254 | |
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__)); | |
229 | ||
230 | struct bbm_log_entry { | |
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; | |
234 | } __attribute__ ((__packed__)); | |
235 | ||
236 | struct bbm_log { | |
237 | __u32 signature; /* 0xABADB10C */ | |
238 | __u32 entry_count; | |
239 | struct bbm_log_entry marked_block_entries[BBM_LOG_MAX_ENTRIES]; | |
240 | } __attribute__ ((__packed__)); | |
241 | ||
242 | #ifndef MDASSEMBLE | |
243 | static char *map_state_str[] = { "normal", "uninitialized", "degraded", "failed" }; | |
244 | #endif | |
245 | ||
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 | ||
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 | |
253 | */ | |
254 | ||
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 | ||
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 | ||
300 | #define pr_vrb(fmt, arg...) (void) (verbose && pr_err(fmt, ##arg)) | |
301 | ||
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 | ||
325 | static unsigned int sector_count(__u32 bytes, unsigned int sector_size) | |
326 | { | |
327 | return ROUND_UP(bytes, sector_size) / sector_size; | |
328 | } | |
329 | ||
330 | static unsigned int mpb_sectors(struct imsm_super *mpb, | |
331 | unsigned int sector_size) | |
332 | { | |
333 | return sector_count(__le32_to_cpu(mpb->mpb_size), sector_size); | |
334 | } | |
335 | ||
336 | struct intel_dev { | |
337 | struct imsm_dev *dev; | |
338 | struct intel_dev *next; | |
339 | unsigned index; | |
340 | }; | |
341 | ||
342 | struct intel_hba { | |
343 | enum sys_dev_type type; | |
344 | char *path; | |
345 | char *pci_id; | |
346 | struct intel_hba *next; | |
347 | }; | |
348 | ||
349 | enum action { | |
350 | DISK_REMOVE = 1, | |
351 | DISK_ADD | |
352 | }; | |
353 | /* internal representation of IMSM metadata */ | |
354 | struct intel_super { | |
355 | union { | |
356 | void *buf; /* O_DIRECT buffer for reading/writing metadata */ | |
357 | struct imsm_super *anchor; /* immovable parameters */ | |
358 | }; | |
359 | union { | |
360 | void *migr_rec_buf; /* buffer for I/O operations */ | |
361 | struct migr_record *migr_rec; /* migration record */ | |
362 | }; | |
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 */ | |
366 | size_t len; /* size of the 'buf' allocation */ | |
367 | size_t extra_space; /* extra space in 'buf' that is not used yet */ | |
368 | void *next_buf; /* for realloc'ing buf from the manager */ | |
369 | size_t next_len; | |
370 | int updates_pending; /* count of pending updates for mdmon */ | |
371 | int current_vol; /* index of raid device undergoing creation */ | |
372 | unsigned long long create_offset; /* common start for 'current_vol' */ | |
373 | __u32 random; /* random data for seeding new family numbers */ | |
374 | struct intel_dev *devlist; | |
375 | unsigned int sector_size; /* sector size of used member drives */ | |
376 | struct dl { | |
377 | struct dl *next; | |
378 | int index; | |
379 | __u8 serial[MAX_RAID_SERIAL_LEN]; | |
380 | int major, minor; | |
381 | char *devname; | |
382 | struct imsm_disk disk; | |
383 | int fd; | |
384 | int extent_cnt; | |
385 | struct extent *e; /* for determining freespace @ create */ | |
386 | int raiddisk; /* slot to fill in autolayout */ | |
387 | enum action action; | |
388 | } *disks, *current_disk; | |
389 | struct dl *disk_mgmt_list; /* list of disks to add/remove while mdmon | |
390 | active */ | |
391 | struct dl *missing; /* disks removed while we weren't looking */ | |
392 | struct bbm_log *bbm_log; | |
393 | struct intel_hba *hba; /* device path of the raid controller for this metadata */ | |
394 | const struct imsm_orom *orom; /* platform firmware support */ | |
395 | struct intel_super *next; /* (temp) list for disambiguating family_num */ | |
396 | struct md_bb bb; /* memory for get_bad_blocks call */ | |
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; | |
404 | }; | |
405 | ||
406 | struct extent { | |
407 | unsigned long long start, size; | |
408 | }; | |
409 | ||
410 | /* definitions of reshape process types */ | |
411 | enum imsm_reshape_type { | |
412 | CH_TAKEOVER, | |
413 | CH_MIGRATION, | |
414 | CH_ARRAY_SIZE, | |
415 | }; | |
416 | ||
417 | /* definition of messages passed to imsm_process_update */ | |
418 | enum imsm_update_type { | |
419 | update_activate_spare, | |
420 | update_create_array, | |
421 | update_kill_array, | |
422 | update_rename_array, | |
423 | update_add_remove_disk, | |
424 | update_reshape_container_disks, | |
425 | update_reshape_migration, | |
426 | update_takeover, | |
427 | update_general_migration_checkpoint, | |
428 | update_size_change, | |
429 | update_prealloc_badblocks_mem, | |
430 | }; | |
431 | ||
432 | struct imsm_update_activate_spare { | |
433 | enum imsm_update_type type; | |
434 | struct dl *dl; | |
435 | int slot; | |
436 | int array; | |
437 | struct imsm_update_activate_spare *next; | |
438 | }; | |
439 | ||
440 | struct geo_params { | |
441 | char devnm[32]; | |
442 | char *dev_name; | |
443 | unsigned long long size; | |
444 | int level; | |
445 | int layout; | |
446 | int chunksize; | |
447 | int raid_disks; | |
448 | }; | |
449 | ||
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 | }; | |
459 | ||
460 | struct imsm_update_reshape { | |
461 | enum imsm_update_type type; | |
462 | int old_raid_disks; | |
463 | int new_raid_disks; | |
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; | |
477 | int new_chunksize; | |
478 | ||
479 | int new_disks[1]; /* new_raid_disks - old_raid_disks makedev number */ | |
480 | }; | |
481 | ||
482 | struct imsm_update_size_change { | |
483 | enum imsm_update_type type; | |
484 | int subdev; | |
485 | long long new_size; | |
486 | }; | |
487 | ||
488 | struct imsm_update_general_migration_checkpoint { | |
489 | enum imsm_update_type type; | |
490 | __u32 curr_migr_unit; | |
491 | }; | |
492 | ||
493 | struct disk_info { | |
494 | __u8 serial[MAX_RAID_SERIAL_LEN]; | |
495 | }; | |
496 | ||
497 | struct imsm_update_create_array { | |
498 | enum imsm_update_type type; | |
499 | int dev_idx; | |
500 | struct imsm_dev dev; | |
501 | }; | |
502 | ||
503 | struct imsm_update_kill_array { | |
504 | enum imsm_update_type type; | |
505 | int dev_idx; | |
506 | }; | |
507 | ||
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 | ||
514 | struct imsm_update_add_remove_disk { | |
515 | enum imsm_update_type type; | |
516 | }; | |
517 | ||
518 | struct imsm_update_prealloc_bb_mem { | |
519 | enum imsm_update_type type; | |
520 | }; | |
521 | ||
522 | static const char *_sys_dev_type[] = { | |
523 | [SYS_DEV_UNKNOWN] = "Unknown", | |
524 | [SYS_DEV_SAS] = "SAS", | |
525 | [SYS_DEV_SATA] = "SATA", | |
526 | [SYS_DEV_NVME] = "NVMe", | |
527 | [SYS_DEV_VMD] = "VMD" | |
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 | { | |
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 | ||
548 | return result; | |
549 | } | |
550 | ||
551 | static struct intel_hba * find_intel_hba(struct intel_hba *hba, struct sys_dev *device) | |
552 | { | |
553 | struct intel_hba *result; | |
554 | ||
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 | ||
562 | static int attach_hba_to_super(struct intel_super *super, struct sys_dev *device) | |
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; | |
574 | } | |
575 | ||
576 | hba = super->hba; | |
577 | /* Intel metadata allows for all disks attached to the same type HBA. | |
578 | * Do not support HBA types mixing | |
579 | */ | |
580 | if (device->type != hba->type) | |
581 | return 2; | |
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; | |
594 | } | |
595 | ||
596 | static struct sys_dev* find_disk_attached_hba(int fd, const char *devname) | |
597 | { | |
598 | struct sys_dev *list, *elem; | |
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 | ||
609 | if (!disk_path) | |
610 | return 0; | |
611 | ||
612 | for (elem = list; elem; elem = elem->next) | |
613 | if (path_attached_to_hba(disk_path, elem->path)) | |
614 | return elem; | |
615 | ||
616 | if (disk_path != devname) | |
617 | free(disk_path); | |
618 | ||
619 | return NULL; | |
620 | } | |
621 | ||
622 | static int find_intel_hba_capability(int fd, struct intel_super *super, | |
623 | char *devname); | |
624 | ||
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 | ||
634 | st = xcalloc(1, sizeof(*st)); | |
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 | ||
642 | #ifndef MDASSEMBLE | |
643 | static __u8 *get_imsm_version(struct imsm_super *mpb) | |
644 | { | |
645 | return &mpb->sig[MPB_SIG_LEN]; | |
646 | } | |
647 | #endif | |
648 | ||
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) | |
653 | { | |
654 | if (index >= mpb->num_disks) | |
655 | return NULL; | |
656 | return &mpb->disk[index]; | |
657 | } | |
658 | ||
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) | |
663 | { | |
664 | struct dl *d; | |
665 | ||
666 | for (d = super->disks; d; d = d->next) | |
667 | if (d->index == index) | |
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 | ||
681 | return NULL; | |
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) | |
688 | { | |
689 | __u32 end = mpb->mpb_size / sizeof(end); | |
690 | __u32 *p = (__u32 *) mpb; | |
691 | __u32 sum = 0; | |
692 | ||
693 | while (end--) { | |
694 | sum += __le32_to_cpu(*p); | |
695 | p++; | |
696 | } | |
697 | ||
698 | return sum - __le32_to_cpu(mpb->check_sum); | |
699 | } | |
700 | ||
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) | |
707 | { | |
708 | /* A device can have 2 maps if it is in the middle of a migration. | |
709 | * If second_map is: | |
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 | |
713 | */ | |
714 | struct imsm_map *map = &dev->vol.map[0]; | |
715 | struct imsm_map *map2 = NULL; | |
716 | ||
717 | if (dev->vol.migr_state) | |
718 | map2 = (void *)map + sizeof_imsm_map(map); | |
719 | ||
720 | switch (second_map) { | |
721 | case MAP_0: | |
722 | break; | |
723 | case MAP_1: | |
724 | map = map2; | |
725 | break; | |
726 | case MAP_X: | |
727 | if (map2) | |
728 | map = map2; | |
729 | break; | |
730 | default: | |
731 | map = NULL; | |
732 | } | |
733 | return map; | |
734 | ||
735 | } | |
736 | ||
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) | |
741 | { | |
742 | size_t size = sizeof(*dev) - sizeof(struct imsm_map) + | |
743 | sizeof_imsm_map(get_imsm_map(dev, MAP_0)); | |
744 | ||
745 | /* migrating means an additional map */ | |
746 | if (dev->vol.migr_state) | |
747 | size += sizeof_imsm_map(get_imsm_map(dev, MAP_1)); | |
748 | else if (migr_state) | |
749 | size += sizeof_imsm_map(get_imsm_map(dev, MAP_0)); | |
750 | ||
751 | return size; | |
752 | } | |
753 | ||
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 | ||
768 | static struct imsm_dev *__get_imsm_dev(struct imsm_super *mpb, __u8 index) | |
769 | { | |
770 | int offset; | |
771 | int i; | |
772 | void *_mpb = mpb; | |
773 | ||
774 | if (index >= mpb->num_raid_devs) | |
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 | |
784 | offset += sizeof_imsm_dev(_mpb + offset, 0); | |
785 | ||
786 | return NULL; | |
787 | } | |
788 | ||
789 | static struct imsm_dev *get_imsm_dev(struct intel_super *super, __u8 index) | |
790 | { | |
791 | struct intel_dev *dv; | |
792 | ||
793 | if (index >= super->anchor->num_raid_devs) | |
794 | return NULL; | |
795 | for (dv = super->devlist; dv; dv = dv->next) | |
796 | if (dv->index == index) | |
797 | return dv->dev; | |
798 | return NULL; | |
799 | } | |
800 | ||
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 | } | |
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 | } | |
908 | ||
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 | ||
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 | } | |
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 | ||
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 | ||
1039 | /* | |
1040 | * for second_map: | |
1041 | * == MAP_0 get first map | |
1042 | * == MAP_1 get second map | |
1043 | * == MAP_X than get map according to the current migr_state | |
1044 | */ | |
1045 | static __u32 get_imsm_ord_tbl_ent(struct imsm_dev *dev, | |
1046 | int slot, | |
1047 | int second_map) | |
1048 | { | |
1049 | struct imsm_map *map; | |
1050 | ||
1051 | map = get_imsm_map(dev, second_map); | |
1052 | ||
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) | |
1058 | static __u32 get_imsm_disk_idx(struct imsm_dev *dev, int slot, int second_map) | |
1059 | { | |
1060 | __u32 ord = get_imsm_ord_tbl_ent(dev, slot, second_map); | |
1061 | ||
1062 | return ord_to_idx(ord); | |
1063 | } | |
1064 | ||
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 | ||
1070 | static int get_imsm_disk_slot(struct imsm_map *map, unsigned idx) | |
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 | ||
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 | ||
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 | ||
1107 | static int count_memberships(struct dl *dl, struct intel_super *super) | |
1108 | { | |
1109 | int memberships = 0; | |
1110 | int i; | |
1111 | ||
1112 | for (i = 0; i < super->anchor->num_raid_devs; i++) { | |
1113 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
1114 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
1115 | ||
1116 | if (get_imsm_disk_slot(map, dl->index) >= 0) | |
1117 | memberships++; | |
1118 | } | |
1119 | ||
1120 | return memberships; | |
1121 | } | |
1122 | ||
1123 | static __u32 imsm_min_reserved_sectors(struct intel_super *super); | |
1124 | ||
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 | ||
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; | |
1192 | int i; | |
1193 | int memberships = count_memberships(dl, super); | |
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) | |
1201 | reservation = imsm_min_reserved_sectors(super); | |
1202 | else | |
1203 | reservation = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS; | |
1204 | ||
1205 | rv = xcalloc(sizeof(struct extent), (memberships + 1)); | |
1206 | e = rv; | |
1207 | ||
1208 | for (i = 0; i < super->anchor->num_raid_devs; i++) { | |
1209 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
1210 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
1211 | ||
1212 | if (get_imsm_disk_slot(map, dl->index) >= 0) { | |
1213 | e->start = pba_of_lba0(map); | |
1214 | e->size = blocks_per_member(map); | |
1215 | e++; | |
1216 | } | |
1217 | } | |
1218 | qsort(rv, memberships, sizeof(*rv), cmp_extent); | |
1219 | ||
1220 | /* determine the start of the metadata | |
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]; | |
1227 | unsigned long long remainder; | |
1228 | ||
1229 | remainder = total_blocks(&dl->disk) - (last->start + last->size); | |
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 */ | |
1235 | if (remainder < (unsigned)ROUND_UP(super->len, 512) >> 9) | |
1236 | remainder = ROUND_UP(super->len, 512) >> 9; | |
1237 | if (reservation > remainder) | |
1238 | reservation = remainder; | |
1239 | } | |
1240 | e->start = total_blocks(&dl->disk) - reservation; | |
1241 | e->size = 0; | |
1242 | return rv; | |
1243 | } | |
1244 | ||
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 | ||
1269 | rv = total_blocks(&dl->disk) - e[i].start; | |
1270 | ||
1271 | free(e); | |
1272 | ||
1273 | return rv; | |
1274 | } | |
1275 | ||
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 | ||
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; | |
1299 | unsigned long long min_active; | |
1300 | __u32 remainder; | |
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; | |
1311 | unsigned long long blocks = total_blocks(&dl->disk); | |
1312 | if (blocks < min_active || min_active == 0) { | |
1313 | dl_min = dl; | |
1314 | min_active = blocks; | |
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 | ||
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); | |
1364 | ||
1365 | /* add the amount of space needed for metadata */ | |
1366 | rv = rv + imsm_min_reserved_sectors(super); | |
1367 | ||
1368 | return rv * 512; | |
1369 | } | |
1370 | ||
1371 | static int is_gen_migration(struct imsm_dev *dev); | |
1372 | ||
1373 | #define IMSM_4K_DIV 8 | |
1374 | ||
1375 | #ifndef MDASSEMBLE | |
1376 | static __u64 blocks_per_migr_unit(struct intel_super *super, | |
1377 | struct imsm_dev *dev); | |
1378 | ||
1379 | static void print_imsm_dev(struct intel_super *super, | |
1380 | struct imsm_dev *dev, | |
1381 | char *uuid, | |
1382 | int disk_idx) | |
1383 | { | |
1384 | __u64 sz; | |
1385 | int slot, i; | |
1386 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
1387 | struct imsm_map *map2 = get_imsm_map(dev, MAP_1); | |
1388 | __u32 ord; | |
1389 | ||
1390 | printf("\n"); | |
1391 | printf("[%.16s]:\n", dev->volume); | |
1392 | printf(" UUID : %s\n", uuid); | |
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"); | |
1401 | printf(" Slots : ["); | |
1402 | for (i = 0; i < map->num_members; i++) { | |
1403 | ord = get_imsm_ord_tbl_ent(dev, i, MAP_0); | |
1404 | printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U"); | |
1405 | } | |
1406 | printf("]"); | |
1407 | if (map2) { | |
1408 | printf(" <-- ["); | |
1409 | for (i = 0; i < map2->num_members; i++) { | |
1410 | ord = get_imsm_ord_tbl_ent(dev, i, MAP_1); | |
1411 | printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U"); | |
1412 | } | |
1413 | printf("]"); | |
1414 | } | |
1415 | printf("\n"); | |
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"); | |
1422 | slot = get_imsm_disk_slot(map, disk_idx); | |
1423 | if (slot >= 0) { | |
1424 | ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X); | |
1425 | printf(" This Slot : %d%s\n", slot, | |
1426 | ord & IMSM_ORD_REBUILD ? " (out-of-sync)" : ""); | |
1427 | } else | |
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)); | |
1434 | sz = blocks_per_member(map); | |
1435 | printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz, | |
1436 | human_size(sz * 512)); | |
1437 | printf(" Sector Offset : %llu\n", | |
1438 | pba_of_lba0(map)); | |
1439 | printf(" Num Stripes : %llu\n", | |
1440 | num_data_stripes(map)); | |
1441 | printf(" Chunk Size : %u KiB", | |
1442 | __le16_to_cpu(map->blocks_per_strip) / 2); | |
1443 | if (map2) | |
1444 | printf(" <-- %u KiB", | |
1445 | __le16_to_cpu(map2->blocks_per_strip) / 2); | |
1446 | printf("\n"); | |
1447 | printf(" Reserved : %d\n", __le32_to_cpu(dev->reserved_blocks)); | |
1448 | printf(" Migrate State : "); | |
1449 | if (dev->vol.migr_state) { | |
1450 | if (migr_type(dev) == MIGR_INIT) | |
1451 | printf("initialize\n"); | |
1452 | else if (migr_type(dev) == MIGR_REBUILD) | |
1453 | printf("rebuild\n"); | |
1454 | else if (migr_type(dev) == MIGR_VERIFY) | |
1455 | printf("check\n"); | |
1456 | else if (migr_type(dev) == MIGR_GEN_MIGR) | |
1457 | printf("general migration\n"); | |
1458 | else if (migr_type(dev) == MIGR_STATE_CHANGE) | |
1459 | printf("state change\n"); | |
1460 | else if (migr_type(dev) == MIGR_REPAIR) | |
1461 | printf("repair\n"); | |
1462 | else | |
1463 | printf("<unknown:%d>\n", migr_type(dev)); | |
1464 | } else | |
1465 | printf("idle\n"); | |
1466 | printf(" Map State : %s", map_state_str[map->map_state]); | |
1467 | if (dev->vol.migr_state) { | |
1468 | struct imsm_map *map = get_imsm_map(dev, MAP_1); | |
1469 | ||
1470 | printf(" <-- %s", map_state_str[map->map_state]); | |
1471 | printf("\n Checkpoint : %u ", | |
1472 | __le32_to_cpu(dev->vol.curr_migr_unit)); | |
1473 | if (is_gen_migration(dev) && (slot > 1 || slot < 0)) | |
1474 | printf("(N/A)"); | |
1475 | else | |
1476 | printf("(%llu)", (unsigned long long) | |
1477 | blocks_per_migr_unit(super, dev)); | |
1478 | } | |
1479 | printf("\n"); | |
1480 | printf(" Dirty State : %s\n", dev->vol.dirty ? "dirty" : "clean"); | |
1481 | } | |
1482 | ||
1483 | static void print_imsm_disk(struct imsm_disk *disk, | |
1484 | int index, | |
1485 | __u32 reserved, | |
1486 | unsigned int sector_size) { | |
1487 | char str[MAX_RAID_SERIAL_LEN + 1]; | |
1488 | __u64 sz; | |
1489 | ||
1490 | if (index < -1 || !disk) | |
1491 | return; | |
1492 | ||
1493 | printf("\n"); | |
1494 | snprintf(str, MAX_RAID_SERIAL_LEN + 1, "%s", disk->serial); | |
1495 | if (index >= 0) | |
1496 | printf(" Disk%02d Serial : %s\n", index, str); | |
1497 | else | |
1498 | printf(" Disk Serial : %s\n", str); | |
1499 | printf(" State :%s%s%s\n", is_spare(disk) ? " spare" : "", | |
1500 | is_configured(disk) ? " active" : "", | |
1501 | is_failed(disk) ? " failed" : ""); | |
1502 | printf(" Id : %08x\n", __le32_to_cpu(disk->scsi_id)); | |
1503 | sz = total_blocks(disk) - reserved; | |
1504 | printf(" Usable Size : %llu%s\n", | |
1505 | (unsigned long long)sz * 512 / sector_size, | |
1506 | human_size(sz * 512)); | |
1507 | } | |
1508 | ||
1509 | void convert_to_4k_imsm_migr_rec(struct intel_super *super) | |
1510 | { | |
1511 | struct migr_record *migr_rec = super->migr_rec; | |
1512 | ||
1513 | migr_rec->blocks_per_unit /= IMSM_4K_DIV; | |
1514 | migr_rec->ckpt_area_pba /= IMSM_4K_DIV; | |
1515 | migr_rec->dest_1st_member_lba /= IMSM_4K_DIV; | |
1516 | migr_rec->dest_depth_per_unit /= IMSM_4K_DIV; | |
1517 | split_ull((join_u32(migr_rec->post_migr_vol_cap, | |
1518 | migr_rec->post_migr_vol_cap_hi) / IMSM_4K_DIV), | |
1519 | &migr_rec->post_migr_vol_cap, &migr_rec->post_migr_vol_cap_hi); | |
1520 | } | |
1521 | ||
1522 | void convert_to_4k_imsm_disk(struct imsm_disk *disk) | |
1523 | { | |
1524 | set_total_blocks(disk, (total_blocks(disk)/IMSM_4K_DIV)); | |
1525 | } | |
1526 | ||
1527 | void convert_to_4k(struct intel_super *super) | |
1528 | { | |
1529 | struct imsm_super *mpb = super->anchor; | |
1530 | struct imsm_disk *disk; | |
1531 | int i; | |
1532 | __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size); | |
1533 | ||
1534 | for (i = 0; i < mpb->num_disks ; i++) { | |
1535 | disk = __get_imsm_disk(mpb, i); | |
1536 | /* disk */ | |
1537 | convert_to_4k_imsm_disk(disk); | |
1538 | } | |
1539 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
1540 | struct imsm_dev *dev = __get_imsm_dev(mpb, i); | |
1541 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
1542 | /* dev */ | |
1543 | split_ull((join_u32(dev->size_low, dev->size_high)/IMSM_4K_DIV), | |
1544 | &dev->size_low, &dev->size_high); | |
1545 | dev->vol.curr_migr_unit /= IMSM_4K_DIV; | |
1546 | ||
1547 | /* map0 */ | |
1548 | set_blocks_per_member(map, blocks_per_member(map)/IMSM_4K_DIV); | |
1549 | map->blocks_per_strip /= IMSM_4K_DIV; | |
1550 | set_pba_of_lba0(map, pba_of_lba0(map)/IMSM_4K_DIV); | |
1551 | ||
1552 | if (dev->vol.migr_state) { | |
1553 | /* map1 */ | |
1554 | map = get_imsm_map(dev, MAP_1); | |
1555 | set_blocks_per_member(map, | |
1556 | blocks_per_member(map)/IMSM_4K_DIV); | |
1557 | map->blocks_per_strip /= IMSM_4K_DIV; | |
1558 | set_pba_of_lba0(map, pba_of_lba0(map)/IMSM_4K_DIV); | |
1559 | } | |
1560 | } | |
1561 | if (bbm_log_size) { | |
1562 | struct bbm_log *log = (void *)mpb + | |
1563 | __le32_to_cpu(mpb->mpb_size) - bbm_log_size; | |
1564 | __u32 i; | |
1565 | ||
1566 | for (i = 0; i < log->entry_count; i++) { | |
1567 | struct bbm_log_entry *entry = | |
1568 | &log->marked_block_entries[i]; | |
1569 | ||
1570 | __u8 count = entry->marked_count + 1; | |
1571 | unsigned long long sector = | |
1572 | __le48_to_cpu(&entry->defective_block_start); | |
1573 | ||
1574 | entry->defective_block_start = | |
1575 | __cpu_to_le48(sector/IMSM_4K_DIV); | |
1576 | entry->marked_count = max(count/IMSM_4K_DIV, 1) - 1; | |
1577 | } | |
1578 | } | |
1579 | ||
1580 | mpb->check_sum = __gen_imsm_checksum(mpb); | |
1581 | } | |
1582 | ||
1583 | void examine_migr_rec_imsm(struct intel_super *super) | |
1584 | { | |
1585 | struct migr_record *migr_rec = super->migr_rec; | |
1586 | struct imsm_super *mpb = super->anchor; | |
1587 | int i; | |
1588 | ||
1589 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
1590 | struct imsm_dev *dev = __get_imsm_dev(mpb, i); | |
1591 | struct imsm_map *map; | |
1592 | int slot = -1; | |
1593 | ||
1594 | if (is_gen_migration(dev) == 0) | |
1595 | continue; | |
1596 | ||
1597 | printf("\nMigration Record Information:"); | |
1598 | ||
1599 | /* first map under migration */ | |
1600 | map = get_imsm_map(dev, MAP_0); | |
1601 | if (map) | |
1602 | slot = get_imsm_disk_slot(map, super->disks->index); | |
1603 | if (map == NULL || slot > 1 || slot < 0) { | |
1604 | printf(" Empty\n "); | |
1605 | printf("Examine one of first two disks in array\n"); | |
1606 | break; | |
1607 | } | |
1608 | printf("\n Status : "); | |
1609 | if (__le32_to_cpu(migr_rec->rec_status) == UNIT_SRC_NORMAL) | |
1610 | printf("Normal\n"); | |
1611 | else | |
1612 | printf("Contains Data\n"); | |
1613 | printf(" Current Unit : %u\n", | |
1614 | __le32_to_cpu(migr_rec->curr_migr_unit)); | |
1615 | printf(" Family : %u\n", | |
1616 | __le32_to_cpu(migr_rec->family_num)); | |
1617 | printf(" Ascending : %u\n", | |
1618 | __le32_to_cpu(migr_rec->ascending_migr)); | |
1619 | printf(" Blocks Per Unit : %u\n", | |
1620 | __le32_to_cpu(migr_rec->blocks_per_unit)); | |
1621 | printf(" Dest. Depth Per Unit : %u\n", | |
1622 | __le32_to_cpu(migr_rec->dest_depth_per_unit)); | |
1623 | printf(" Checkpoint Area pba : %u\n", | |
1624 | __le32_to_cpu(migr_rec->ckpt_area_pba)); | |
1625 | printf(" First member lba : %u\n", | |
1626 | __le32_to_cpu(migr_rec->dest_1st_member_lba)); | |
1627 | printf(" Total Number of Units : %u\n", | |
1628 | __le32_to_cpu(migr_rec->num_migr_units)); | |
1629 | printf(" Size of volume : %u\n", | |
1630 | __le32_to_cpu(migr_rec->post_migr_vol_cap)); | |
1631 | printf(" Expansion space for LBA64 : %u\n", | |
1632 | __le32_to_cpu(migr_rec->post_migr_vol_cap_hi)); | |
1633 | printf(" Record was read from : %u\n", | |
1634 | __le32_to_cpu(migr_rec->ckpt_read_disk_num)); | |
1635 | ||
1636 | break; | |
1637 | } | |
1638 | } | |
1639 | #endif /* MDASSEMBLE */ | |
1640 | ||
1641 | void convert_from_4k_imsm_migr_rec(struct intel_super *super) | |
1642 | { | |
1643 | struct migr_record *migr_rec = super->migr_rec; | |
1644 | ||
1645 | migr_rec->blocks_per_unit *= IMSM_4K_DIV; | |
1646 | migr_rec->ckpt_area_pba *= IMSM_4K_DIV; | |
1647 | migr_rec->dest_1st_member_lba *= IMSM_4K_DIV; | |
1648 | migr_rec->dest_depth_per_unit *= IMSM_4K_DIV; | |
1649 | split_ull((join_u32(migr_rec->post_migr_vol_cap, | |
1650 | migr_rec->post_migr_vol_cap_hi) * IMSM_4K_DIV), | |
1651 | &migr_rec->post_migr_vol_cap, | |
1652 | &migr_rec->post_migr_vol_cap_hi); | |
1653 | } | |
1654 | ||
1655 | void convert_from_4k(struct intel_super *super) | |
1656 | { | |
1657 | struct imsm_super *mpb = super->anchor; | |
1658 | struct imsm_disk *disk; | |
1659 | int i; | |
1660 | __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size); | |
1661 | ||
1662 | for (i = 0; i < mpb->num_disks ; i++) { | |
1663 | disk = __get_imsm_disk(mpb, i); | |
1664 | /* disk */ | |
1665 | set_total_blocks(disk, (total_blocks(disk)*IMSM_4K_DIV)); | |
1666 | } | |
1667 | ||
1668 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
1669 | struct imsm_dev *dev = __get_imsm_dev(mpb, i); | |
1670 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
1671 | /* dev */ | |
1672 | split_ull((join_u32(dev->size_low, dev->size_high)*IMSM_4K_DIV), | |
1673 | &dev->size_low, &dev->size_high); | |
1674 | dev->vol.curr_migr_unit *= IMSM_4K_DIV; | |
1675 | ||
1676 | /* map0 */ | |
1677 | set_blocks_per_member(map, blocks_per_member(map)*IMSM_4K_DIV); | |
1678 | map->blocks_per_strip *= IMSM_4K_DIV; | |
1679 | set_pba_of_lba0(map, pba_of_lba0(map)*IMSM_4K_DIV); | |
1680 | ||
1681 | if (dev->vol.migr_state) { | |
1682 | /* map1 */ | |
1683 | map = get_imsm_map(dev, MAP_1); | |
1684 | set_blocks_per_member(map, | |
1685 | blocks_per_member(map)*IMSM_4K_DIV); | |
1686 | map->blocks_per_strip *= IMSM_4K_DIV; | |
1687 | set_pba_of_lba0(map, pba_of_lba0(map)*IMSM_4K_DIV); | |
1688 | } | |
1689 | } | |
1690 | if (bbm_log_size) { | |
1691 | struct bbm_log *log = (void *)mpb + | |
1692 | __le32_to_cpu(mpb->mpb_size) - bbm_log_size; | |
1693 | __u32 i; | |
1694 | ||
1695 | for (i = 0; i < log->entry_count; i++) { | |
1696 | struct bbm_log_entry *entry = | |
1697 | &log->marked_block_entries[i]; | |
1698 | ||
1699 | __u8 count = entry->marked_count + 1; | |
1700 | unsigned long long sector = | |
1701 | __le48_to_cpu(&entry->defective_block_start); | |
1702 | ||
1703 | entry->defective_block_start = | |
1704 | __cpu_to_le48(sector*IMSM_4K_DIV); | |
1705 | entry->marked_count = count*IMSM_4K_DIV - 1; | |
1706 | } | |
1707 | } | |
1708 | ||
1709 | mpb->check_sum = __gen_imsm_checksum(mpb); | |
1710 | } | |
1711 | ||
1712 | /******************************************************************************* | |
1713 | * function: imsm_check_attributes | |
1714 | * Description: Function checks if features represented by attributes flags | |
1715 | * are supported by mdadm. | |
1716 | * Parameters: | |
1717 | * attributes - Attributes read from metadata | |
1718 | * Returns: | |
1719 | * 0 - passed attributes contains unsupported features flags | |
1720 | * 1 - all features are supported | |
1721 | ******************************************************************************/ | |
1722 | static int imsm_check_attributes(__u32 attributes) | |
1723 | { | |
1724 | int ret_val = 1; | |
1725 | __u32 not_supported = MPB_ATTRIB_SUPPORTED^0xffffffff; | |
1726 | ||
1727 | not_supported &= ~MPB_ATTRIB_IGNORED; | |
1728 | ||
1729 | not_supported &= attributes; | |
1730 | if (not_supported) { | |
1731 | pr_err("(IMSM): Unsupported attributes : %x\n", | |
1732 | (unsigned)__le32_to_cpu(not_supported)); | |
1733 | if (not_supported & MPB_ATTRIB_CHECKSUM_VERIFY) { | |
1734 | dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n"); | |
1735 | not_supported ^= MPB_ATTRIB_CHECKSUM_VERIFY; | |
1736 | } | |
1737 | if (not_supported & MPB_ATTRIB_2TB) { | |
1738 | dprintf("\t\tMPB_ATTRIB_2TB\n"); | |
1739 | not_supported ^= MPB_ATTRIB_2TB; | |
1740 | } | |
1741 | if (not_supported & MPB_ATTRIB_RAID0) { | |
1742 | dprintf("\t\tMPB_ATTRIB_RAID0\n"); | |
1743 | not_supported ^= MPB_ATTRIB_RAID0; | |
1744 | } | |
1745 | if (not_supported & MPB_ATTRIB_RAID1) { | |
1746 | dprintf("\t\tMPB_ATTRIB_RAID1\n"); | |
1747 | not_supported ^= MPB_ATTRIB_RAID1; | |
1748 | } | |
1749 | if (not_supported & MPB_ATTRIB_RAID10) { | |
1750 | dprintf("\t\tMPB_ATTRIB_RAID10\n"); | |
1751 | not_supported ^= MPB_ATTRIB_RAID10; | |
1752 | } | |
1753 | if (not_supported & MPB_ATTRIB_RAID1E) { | |
1754 | dprintf("\t\tMPB_ATTRIB_RAID1E\n"); | |
1755 | not_supported ^= MPB_ATTRIB_RAID1E; | |
1756 | } | |
1757 | if (not_supported & MPB_ATTRIB_RAID5) { | |
1758 | dprintf("\t\tMPB_ATTRIB_RAID5\n"); | |
1759 | not_supported ^= MPB_ATTRIB_RAID5; | |
1760 | } | |
1761 | if (not_supported & MPB_ATTRIB_RAIDCNG) { | |
1762 | dprintf("\t\tMPB_ATTRIB_RAIDCNG\n"); | |
1763 | not_supported ^= MPB_ATTRIB_RAIDCNG; | |
1764 | } | |
1765 | if (not_supported & MPB_ATTRIB_BBM) { | |
1766 | dprintf("\t\tMPB_ATTRIB_BBM\n"); | |
1767 | not_supported ^= MPB_ATTRIB_BBM; | |
1768 | } | |
1769 | if (not_supported & MPB_ATTRIB_CHECKSUM_VERIFY) { | |
1770 | dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n"); | |
1771 | not_supported ^= MPB_ATTRIB_CHECKSUM_VERIFY; | |
1772 | } | |
1773 | if (not_supported & MPB_ATTRIB_EXP_STRIPE_SIZE) { | |
1774 | dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n"); | |
1775 | not_supported ^= MPB_ATTRIB_EXP_STRIPE_SIZE; | |
1776 | } | |
1777 | if (not_supported & MPB_ATTRIB_2TB_DISK) { | |
1778 | dprintf("\t\tMPB_ATTRIB_2TB_DISK\n"); | |
1779 | not_supported ^= MPB_ATTRIB_2TB_DISK; | |
1780 | } | |
1781 | if (not_supported & MPB_ATTRIB_NEVER_USE2) { | |
1782 | dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n"); | |
1783 | not_supported ^= MPB_ATTRIB_NEVER_USE2; | |
1784 | } | |
1785 | if (not_supported & MPB_ATTRIB_NEVER_USE) { | |
1786 | dprintf("\t\tMPB_ATTRIB_NEVER_USE\n"); | |
1787 | not_supported ^= MPB_ATTRIB_NEVER_USE; | |
1788 | } | |
1789 | ||
1790 | if (not_supported) | |
1791 | dprintf("(IMSM): Unknown attributes : %x\n", not_supported); | |
1792 | ||
1793 | ret_val = 0; | |
1794 | } | |
1795 | ||
1796 | return ret_val; | |
1797 | } | |
1798 | ||
1799 | #ifndef MDASSEMBLE | |
1800 | static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map); | |
1801 | ||
1802 | static void examine_super_imsm(struct supertype *st, char *homehost) | |
1803 | { | |
1804 | struct intel_super *super = st->sb; | |
1805 | struct imsm_super *mpb = super->anchor; | |
1806 | char str[MAX_SIGNATURE_LENGTH]; | |
1807 | int i; | |
1808 | struct mdinfo info; | |
1809 | char nbuf[64]; | |
1810 | __u32 sum; | |
1811 | __u32 reserved = imsm_reserved_sectors(super, super->disks); | |
1812 | struct dl *dl; | |
1813 | ||
1814 | strncpy(str, (char *)mpb->sig, MPB_SIG_LEN); | |
1815 | str[MPB_SIG_LEN-1] = '\0'; | |
1816 | printf(" Magic : %s\n", str); | |
1817 | snprintf(str, strlen(MPB_VERSION_RAID0), "%s", get_imsm_version(mpb)); | |
1818 | printf(" Version : %s\n", get_imsm_version(mpb)); | |
1819 | printf(" Orig Family : %08x\n", __le32_to_cpu(mpb->orig_family_num)); | |
1820 | printf(" Family : %08x\n", __le32_to_cpu(mpb->family_num)); | |
1821 | printf(" Generation : %08x\n", __le32_to_cpu(mpb->generation_num)); | |
1822 | printf(" Attributes : "); | |
1823 | if (imsm_check_attributes(mpb->attributes)) | |
1824 | printf("All supported\n"); | |
1825 | else | |
1826 | printf("not supported\n"); | |
1827 | getinfo_super_imsm(st, &info, NULL); | |
1828 | fname_from_uuid(st, &info, nbuf, ':'); | |
1829 | printf(" UUID : %s\n", nbuf + 5); | |
1830 | sum = __le32_to_cpu(mpb->check_sum); | |
1831 | printf(" Checksum : %08x %s\n", sum, | |
1832 | __gen_imsm_checksum(mpb) == sum ? "correct" : "incorrect"); | |
1833 | printf(" MPB Sectors : %d\n", mpb_sectors(mpb, super->sector_size)); | |
1834 | printf(" Disks : %d\n", mpb->num_disks); | |
1835 | printf(" RAID Devices : %d\n", mpb->num_raid_devs); | |
1836 | print_imsm_disk(__get_imsm_disk(mpb, super->disks->index), | |
1837 | super->disks->index, reserved, super->sector_size); | |
1838 | if (get_imsm_bbm_log_size(super->bbm_log)) { | |
1839 | struct bbm_log *log = super->bbm_log; | |
1840 | ||
1841 | printf("\n"); | |
1842 | printf("Bad Block Management Log:\n"); | |
1843 | printf(" Log Size : %d\n", __le32_to_cpu(mpb->bbm_log_size)); | |
1844 | printf(" Signature : %x\n", __le32_to_cpu(log->signature)); | |
1845 | printf(" Entry Count : %d\n", __le32_to_cpu(log->entry_count)); | |
1846 | } | |
1847 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
1848 | struct mdinfo info; | |
1849 | struct imsm_dev *dev = __get_imsm_dev(mpb, i); | |
1850 | ||
1851 | super->current_vol = i; | |
1852 | getinfo_super_imsm(st, &info, NULL); | |
1853 | fname_from_uuid(st, &info, nbuf, ':'); | |
1854 | print_imsm_dev(super, dev, nbuf + 5, super->disks->index); | |
1855 | } | |
1856 | for (i = 0; i < mpb->num_disks; i++) { | |
1857 | if (i == super->disks->index) | |
1858 | continue; | |
1859 | print_imsm_disk(__get_imsm_disk(mpb, i), i, reserved, | |
1860 | super->sector_size); | |
1861 | } | |
1862 | ||
1863 | for (dl = super->disks; dl; dl = dl->next) | |
1864 | if (dl->index == -1) | |
1865 | print_imsm_disk(&dl->disk, -1, reserved, | |
1866 | super->sector_size); | |
1867 | ||
1868 | examine_migr_rec_imsm(super); | |
1869 | } | |
1870 | ||
1871 | static void brief_examine_super_imsm(struct supertype *st, int verbose) | |
1872 | { | |
1873 | /* We just write a generic IMSM ARRAY entry */ | |
1874 | struct mdinfo info; | |
1875 | char nbuf[64]; | |
1876 | struct intel_super *super = st->sb; | |
1877 | ||
1878 | if (!super->anchor->num_raid_devs) { | |
1879 | printf("ARRAY metadata=imsm\n"); | |
1880 | return; | |
1881 | } | |
1882 | ||
1883 | getinfo_super_imsm(st, &info, NULL); | |
1884 | fname_from_uuid(st, &info, nbuf, ':'); | |
1885 | printf("ARRAY metadata=imsm UUID=%s\n", nbuf + 5); | |
1886 | } | |
1887 | ||
1888 | static void brief_examine_subarrays_imsm(struct supertype *st, int verbose) | |
1889 | { | |
1890 | /* We just write a generic IMSM ARRAY entry */ | |
1891 | struct mdinfo info; | |
1892 | char nbuf[64]; | |
1893 | char nbuf1[64]; | |
1894 | struct intel_super *super = st->sb; | |
1895 | int i; | |
1896 | ||
1897 | if (!super->anchor->num_raid_devs) | |
1898 | return; | |
1899 | ||
1900 | getinfo_super_imsm(st, &info, NULL); | |
1901 | fname_from_uuid(st, &info, nbuf, ':'); | |
1902 | for (i = 0; i < super->anchor->num_raid_devs; i++) { | |
1903 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
1904 | ||
1905 | super->current_vol = i; | |
1906 | getinfo_super_imsm(st, &info, NULL); | |
1907 | fname_from_uuid(st, &info, nbuf1, ':'); | |
1908 | printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n", | |
1909 | dev->volume, nbuf + 5, i, nbuf1 + 5); | |
1910 | } | |
1911 | } | |
1912 | ||
1913 | static void export_examine_super_imsm(struct supertype *st) | |
1914 | { | |
1915 | struct intel_super *super = st->sb; | |
1916 | struct imsm_super *mpb = super->anchor; | |
1917 | struct mdinfo info; | |
1918 | char nbuf[64]; | |
1919 | ||
1920 | getinfo_super_imsm(st, &info, NULL); | |
1921 | fname_from_uuid(st, &info, nbuf, ':'); | |
1922 | printf("MD_METADATA=imsm\n"); | |
1923 | printf("MD_LEVEL=container\n"); | |
1924 | printf("MD_UUID=%s\n", nbuf+5); | |
1925 | printf("MD_DEVICES=%u\n", mpb->num_disks); | |
1926 | } | |
1927 | ||
1928 | static int copy_metadata_imsm(struct supertype *st, int from, int to) | |
1929 | { | |
1930 | /* The second last sector of the device contains | |
1931 | * the "struct imsm_super" metadata. | |
1932 | * This contains mpb_size which is the size in bytes of the | |
1933 | * extended metadata. This is located immediately before | |
1934 | * the imsm_super. | |
1935 | * We want to read all that, plus the last sector which | |
1936 | * may contain a migration record, and write it all | |
1937 | * to the target. | |
1938 | */ | |
1939 | void *buf; | |
1940 | unsigned long long dsize, offset; | |
1941 | int sectors; | |
1942 | struct imsm_super *sb; | |
1943 | struct intel_super *super = st->sb; | |
1944 | unsigned int sector_size = super->sector_size; | |
1945 | unsigned int written = 0; | |
1946 | ||
1947 | if (posix_memalign(&buf, MAX_SECTOR_SIZE, MAX_SECTOR_SIZE) != 0) | |
1948 | return 1; | |
1949 | ||
1950 | if (!get_dev_size(from, NULL, &dsize)) | |
1951 | goto err; | |
1952 | ||
1953 | if (lseek64(from, dsize-(2*sector_size), 0) < 0) | |
1954 | goto err; | |
1955 | if ((unsigned int)read(from, buf, sector_size) != sector_size) | |
1956 | goto err; | |
1957 | sb = buf; | |
1958 | if (strncmp((char*)sb->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) | |
1959 | goto err; | |
1960 | ||
1961 | sectors = mpb_sectors(sb, sector_size) + 2; | |
1962 | offset = dsize - sectors * sector_size; | |
1963 | if (lseek64(from, offset, 0) < 0 || | |
1964 | lseek64(to, offset, 0) < 0) | |
1965 | goto err; | |
1966 | while (written < sectors * sector_size) { | |
1967 | int n = sectors*sector_size - written; | |
1968 | if (n > 4096) | |
1969 | n = 4096; | |
1970 | if (read(from, buf, n) != n) | |
1971 | goto err; | |
1972 | if (write(to, buf, n) != n) | |
1973 | goto err; | |
1974 | written += n; | |
1975 | } | |
1976 | free(buf); | |
1977 | return 0; | |
1978 | err: | |
1979 | free(buf); | |
1980 | return 1; | |
1981 | } | |
1982 | ||
1983 | static void detail_super_imsm(struct supertype *st, char *homehost) | |
1984 | { | |
1985 | struct mdinfo info; | |
1986 | char nbuf[64]; | |
1987 | ||
1988 | getinfo_super_imsm(st, &info, NULL); | |
1989 | fname_from_uuid(st, &info, nbuf, ':'); | |
1990 | printf("\n UUID : %s\n", nbuf + 5); | |
1991 | } | |
1992 | ||
1993 | static void brief_detail_super_imsm(struct supertype *st) | |
1994 | { | |
1995 | struct mdinfo info; | |
1996 | char nbuf[64]; | |
1997 | getinfo_super_imsm(st, &info, NULL); | |
1998 | fname_from_uuid(st, &info, nbuf, ':'); | |
1999 | printf(" UUID=%s", nbuf + 5); | |
2000 | } | |
2001 | ||
2002 | static int imsm_read_serial(int fd, char *devname, __u8 *serial); | |
2003 | static void fd2devname(int fd, char *name); | |
2004 | ||
2005 | static int ahci_enumerate_ports(const char *hba_path, int port_count, int host_base, int verbose) | |
2006 | { | |
2007 | /* dump an unsorted list of devices attached to AHCI Intel storage | |
2008 | * controller, as well as non-connected ports | |
2009 | */ | |
2010 | int hba_len = strlen(hba_path) + 1; | |
2011 | struct dirent *ent; | |
2012 | DIR *dir; | |
2013 | char *path = NULL; | |
2014 | int err = 0; | |
2015 | unsigned long port_mask = (1 << port_count) - 1; | |
2016 | ||
2017 | if (port_count > (int)sizeof(port_mask) * 8) { | |
2018 | if (verbose > 0) | |
2019 | pr_err("port_count %d out of range\n", port_count); | |
2020 | return 2; | |
2021 | } | |
2022 | ||
2023 | /* scroll through /sys/dev/block looking for devices attached to | |
2024 | * this hba | |
2025 | */ | |
2026 | dir = opendir("/sys/dev/block"); | |
2027 | if (!dir) | |
2028 | return 1; | |
2029 | ||
2030 | for (ent = readdir(dir); ent; ent = readdir(dir)) { | |
2031 | int fd; | |
2032 | char model[64]; | |
2033 | char vendor[64]; | |
2034 | char buf[1024]; | |
2035 | int major, minor; | |
2036 | char *device; | |
2037 | char *c; | |
2038 | int port; | |
2039 | int type; | |
2040 | ||
2041 | if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2) | |
2042 | continue; | |
2043 | path = devt_to_devpath(makedev(major, minor)); | |
2044 | if (!path) | |
2045 | continue; | |
2046 | if (!path_attached_to_hba(path, hba_path)) { | |
2047 | free(path); | |
2048 | path = NULL; | |
2049 | continue; | |
2050 | } | |
2051 | ||
2052 | /* retrieve the scsi device type */ | |
2053 | if (asprintf(&device, "/sys/dev/block/%d:%d/device/xxxxxxx", major, minor) < 0) { | |
2054 | if (verbose > 0) | |
2055 | pr_err("failed to allocate 'device'\n"); | |
2056 | err = 2; | |
2057 | break; | |
2058 | } | |
2059 | sprintf(device, "/sys/dev/block/%d:%d/device/type", major, minor); | |
2060 | if (load_sys(device, buf, sizeof(buf)) != 0) { | |
2061 | if (verbose > 0) | |
2062 | pr_err("failed to read device type for %s\n", | |
2063 | path); | |
2064 | err = 2; | |
2065 | free(device); | |
2066 | break; | |
2067 | } | |
2068 | type = strtoul(buf, NULL, 10); | |
2069 | ||
2070 | /* if it's not a disk print the vendor and model */ | |
2071 | if (!(type == 0 || type == 7 || type == 14)) { | |
2072 | vendor[0] = '\0'; | |
2073 | model[0] = '\0'; | |
2074 | sprintf(device, "/sys/dev/block/%d:%d/device/vendor", major, minor); | |
2075 | if (load_sys(device, buf, sizeof(buf)) == 0) { | |
2076 | strncpy(vendor, buf, sizeof(vendor)); | |
2077 | vendor[sizeof(vendor) - 1] = '\0'; | |
2078 | c = (char *) &vendor[sizeof(vendor) - 1]; | |
2079 | while (isspace(*c) || *c == '\0') | |
2080 | *c-- = '\0'; | |
2081 | ||
2082 | } | |
2083 | sprintf(device, "/sys/dev/block/%d:%d/device/model", major, minor); | |
2084 | if (load_sys(device, buf, sizeof(buf)) == 0) { | |
2085 | strncpy(model, buf, sizeof(model)); | |
2086 | model[sizeof(model) - 1] = '\0'; | |
2087 | c = (char *) &model[sizeof(model) - 1]; | |
2088 | while (isspace(*c) || *c == '\0') | |
2089 | *c-- = '\0'; | |
2090 | } | |
2091 | ||
2092 | if (vendor[0] && model[0]) | |
2093 | sprintf(buf, "%.64s %.64s", vendor, model); | |
2094 | else | |
2095 | switch (type) { /* numbers from hald/linux/device.c */ | |
2096 | case 1: sprintf(buf, "tape"); break; | |
2097 | case 2: sprintf(buf, "printer"); break; | |
2098 | case 3: sprintf(buf, "processor"); break; | |
2099 | case 4: | |
2100 | case 5: sprintf(buf, "cdrom"); break; | |
2101 | case 6: sprintf(buf, "scanner"); break; | |
2102 | case 8: sprintf(buf, "media_changer"); break; | |
2103 | case 9: sprintf(buf, "comm"); break; | |
2104 | case 12: sprintf(buf, "raid"); break; | |
2105 | default: sprintf(buf, "unknown"); | |
2106 | } | |
2107 | } else | |
2108 | buf[0] = '\0'; | |
2109 | free(device); | |
2110 | ||
2111 | /* chop device path to 'host%d' and calculate the port number */ | |
2112 | c = strchr(&path[hba_len], '/'); | |
2113 | if (!c) { | |
2114 | if (verbose > 0) | |
2115 | pr_err("%s - invalid path name\n", path + hba_len); | |
2116 | err = 2; | |
2117 | break; | |
2118 | } | |
2119 | *c = '\0'; | |
2120 | if ((sscanf(&path[hba_len], "ata%d", &port) == 1) || | |
2121 | ((sscanf(&path[hba_len], "host%d", &port) == 1))) | |
2122 | port -= host_base; | |
2123 | else { | |
2124 | if (verbose > 0) { | |
2125 | *c = '/'; /* repair the full string */ | |
2126 | pr_err("failed to determine port number for %s\n", | |
2127 | path); | |
2128 | } | |
2129 | err = 2; | |
2130 | break; | |
2131 | } | |
2132 | ||
2133 | /* mark this port as used */ | |
2134 | port_mask &= ~(1 << port); | |
2135 | ||
2136 | /* print out the device information */ | |
2137 | if (buf[0]) { | |
2138 | printf(" Port%d : - non-disk device (%s) -\n", port, buf); | |
2139 | continue; | |
2140 | } | |
2141 | ||
2142 | fd = dev_open(ent->d_name, O_RDONLY); | |
2143 | if (fd < 0) | |
2144 | printf(" Port%d : - disk info unavailable -\n", port); | |
2145 | else { | |
2146 | fd2devname(fd, buf); | |
2147 | printf(" Port%d : %s", port, buf); | |
2148 | if (imsm_read_serial(fd, NULL, (__u8 *) buf) == 0) | |
2149 | printf(" (%.*s)\n", MAX_RAID_SERIAL_LEN, buf); | |
2150 | else | |
2151 | printf(" ()\n"); | |
2152 | close(fd); | |
2153 | } | |
2154 | free(path); | |
2155 | path = NULL; | |
2156 | } | |
2157 | if (path) | |
2158 | free(path); | |
2159 | if (dir) | |
2160 | closedir(dir); | |
2161 | if (err == 0) { | |
2162 | int i; | |
2163 | ||
2164 | for (i = 0; i < port_count; i++) | |
2165 | if (port_mask & (1 << i)) | |
2166 | printf(" Port%d : - no device attached -\n", i); | |
2167 | } | |
2168 | ||
2169 | return err; | |
2170 | } | |
2171 | ||
2172 | static int print_vmd_attached_devs(struct sys_dev *hba) | |
2173 | { | |
2174 | struct dirent *ent; | |
2175 | DIR *dir; | |
2176 | char path[292]; | |
2177 | char link[256]; | |
2178 | char *c, *rp; | |
2179 | ||
2180 | if (hba->type != SYS_DEV_VMD) | |
2181 | return 1; | |
2182 | ||
2183 | /* scroll through /sys/dev/block looking for devices attached to | |
2184 | * this hba | |
2185 | */ | |
2186 | dir = opendir("/sys/bus/pci/drivers/nvme"); | |
2187 | if (!dir) | |
2188 | return 1; | |
2189 | ||
2190 | for (ent = readdir(dir); ent; ent = readdir(dir)) { | |
2191 | int n; | |
2192 | ||
2193 | /* is 'ent' a device? check that the 'subsystem' link exists and | |
2194 | * that its target matches 'bus' | |
2195 | */ | |
2196 | sprintf(path, "/sys/bus/pci/drivers/nvme/%s/subsystem", | |
2197 | ent->d_name); | |
2198 | n = readlink(path, link, sizeof(link)); | |
2199 | if (n < 0 || n >= (int)sizeof(link)) | |
2200 | continue; | |
2201 | link[n] = '\0'; | |
2202 | c = strrchr(link, '/'); | |
2203 | if (!c) | |
2204 | continue; | |
2205 | if (strncmp("pci", c+1, strlen("pci")) != 0) | |
2206 | continue; | |
2207 | ||
2208 | sprintf(path, "/sys/bus/pci/drivers/nvme/%s", ent->d_name); | |
2209 | ||
2210 | rp = realpath(path, NULL); | |
2211 | if (!rp) | |
2212 | continue; | |
2213 | ||
2214 | if (path_attached_to_hba(rp, hba->path)) { | |
2215 | printf(" NVMe under VMD : %s\n", rp); | |
2216 | } | |
2217 | free(rp); | |
2218 | } | |
2219 | ||
2220 | closedir(dir); | |
2221 | return 0; | |
2222 | } | |
2223 | ||
2224 | static void print_found_intel_controllers(struct sys_dev *elem) | |
2225 | { | |
2226 | for (; elem; elem = elem->next) { | |
2227 | pr_err("found Intel(R) "); | |
2228 | if (elem->type == SYS_DEV_SATA) | |
2229 | fprintf(stderr, "SATA "); | |
2230 | else if (elem->type == SYS_DEV_SAS) | |
2231 | fprintf(stderr, "SAS "); | |
2232 | else if (elem->type == SYS_DEV_NVME) | |
2233 | fprintf(stderr, "NVMe "); | |
2234 | ||
2235 | if (elem->type == SYS_DEV_VMD) | |
2236 | fprintf(stderr, "VMD domain"); | |
2237 | else | |
2238 | fprintf(stderr, "RAID controller"); | |
2239 | ||
2240 | if (elem->pci_id) | |
2241 | fprintf(stderr, " at %s", elem->pci_id); | |
2242 | fprintf(stderr, ".\n"); | |
2243 | } | |
2244 | fflush(stderr); | |
2245 | } | |
2246 | ||
2247 | static int ahci_get_port_count(const char *hba_path, int *port_count) | |
2248 | { | |
2249 | struct dirent *ent; | |
2250 | DIR *dir; | |
2251 | int host_base = -1; | |
2252 | ||
2253 | *port_count = 0; | |
2254 | if ((dir = opendir(hba_path)) == NULL) | |
2255 | return -1; | |
2256 | ||
2257 | for (ent = readdir(dir); ent; ent = readdir(dir)) { | |
2258 | int host; | |
2259 | ||
2260 | if ((sscanf(ent->d_name, "ata%d", &host) != 1) && | |
2261 | ((sscanf(ent->d_name, "host%d", &host) != 1))) | |
2262 | continue; | |
2263 | if (*port_count == 0) | |
2264 | host_base = host; | |
2265 | else if (host < host_base) | |
2266 | host_base = host; | |
2267 | ||
2268 | if (host + 1 > *port_count + host_base) | |
2269 | *port_count = host + 1 - host_base; | |
2270 | } | |
2271 | closedir(dir); | |
2272 | return host_base; | |
2273 | } | |
2274 | ||
2275 | static void print_imsm_capability(const struct imsm_orom *orom) | |
2276 | { | |
2277 | printf(" Platform : Intel(R) "); | |
2278 | if (orom->capabilities == 0 && orom->driver_features == 0) | |
2279 | printf("Matrix Storage Manager\n"); | |
2280 | else | |
2281 | printf("Rapid Storage Technology%s\n", | |
2282 | imsm_orom_is_enterprise(orom) ? " enterprise" : ""); | |
2283 | if (orom->major_ver || orom->minor_ver || orom->hotfix_ver || orom->build) | |
2284 | printf(" Version : %d.%d.%d.%d\n", orom->major_ver, | |
2285 | orom->minor_ver, orom->hotfix_ver, orom->build); | |
2286 | printf(" RAID Levels :%s%s%s%s%s\n", | |
2287 | imsm_orom_has_raid0(orom) ? " raid0" : "", | |
2288 | imsm_orom_has_raid1(orom) ? " raid1" : "", | |
2289 | imsm_orom_has_raid1e(orom) ? " raid1e" : "", | |
2290 | imsm_orom_has_raid10(orom) ? " raid10" : "", | |
2291 | imsm_orom_has_raid5(orom) ? " raid5" : ""); | |
2292 | printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n", | |
2293 | imsm_orom_has_chunk(orom, 2) ? " 2k" : "", | |
2294 | imsm_orom_has_chunk(orom, 4) ? " 4k" : "", | |
2295 | imsm_orom_has_chunk(orom, 8) ? " 8k" : "", | |
2296 | imsm_orom_has_chunk(orom, 16) ? " 16k" : "", | |
2297 | imsm_orom_has_chunk(orom, 32) ? " 32k" : "", | |
2298 | imsm_orom_has_chunk(orom, 64) ? " 64k" : "", | |
2299 | imsm_orom_has_chunk(orom, 128) ? " 128k" : "", | |
2300 | imsm_orom_has_chunk(orom, 256) ? " 256k" : "", | |
2301 | imsm_orom_has_chunk(orom, 512) ? " 512k" : "", | |
2302 | imsm_orom_has_chunk(orom, 1024*1) ? " 1M" : "", | |
2303 | imsm_orom_has_chunk(orom, 1024*2) ? " 2M" : "", | |
2304 | imsm_orom_has_chunk(orom, 1024*4) ? " 4M" : "", | |
2305 | imsm_orom_has_chunk(orom, 1024*8) ? " 8M" : "", | |
2306 | imsm_orom_has_chunk(orom, 1024*16) ? " 16M" : "", | |
2307 | imsm_orom_has_chunk(orom, 1024*32) ? " 32M" : "", | |
2308 | imsm_orom_has_chunk(orom, 1024*64) ? " 64M" : ""); | |
2309 | printf(" 2TB volumes :%s supported\n", | |
2310 | (orom->attr & IMSM_OROM_ATTR_2TB)?"":" not"); | |
2311 | printf(" 2TB disks :%s supported\n", | |
2312 | (orom->attr & IMSM_OROM_ATTR_2TB_DISK)?"":" not"); | |
2313 | printf(" Max Disks : %d\n", orom->tds); | |
2314 | printf(" Max Volumes : %d per array, %d per %s\n", | |
2315 | orom->vpa, orom->vphba, | |
2316 | imsm_orom_is_nvme(orom) ? "platform" : "controller"); | |
2317 | return; | |
2318 | } | |
2319 | ||
2320 | static void print_imsm_capability_export(const struct imsm_orom *orom) | |
2321 | { | |
2322 | printf("MD_FIRMWARE_TYPE=imsm\n"); | |
2323 | if (orom->major_ver || orom->minor_ver || orom->hotfix_ver || orom->build) | |
2324 | printf("IMSM_VERSION=%d.%d.%d.%d\n", orom->major_ver, orom->minor_ver, | |
2325 | orom->hotfix_ver, orom->build); | |
2326 | printf("IMSM_SUPPORTED_RAID_LEVELS=%s%s%s%s%s\n", | |
2327 | imsm_orom_has_raid0(orom) ? "raid0 " : "", | |
2328 | imsm_orom_has_raid1(orom) ? "raid1 " : "", | |
2329 | imsm_orom_has_raid1e(orom) ? "raid1e " : "", | |
2330 | imsm_orom_has_raid5(orom) ? "raid10 " : "", | |
2331 | imsm_orom_has_raid10(orom) ? "raid5 " : ""); | |
2332 | printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n", | |
2333 | imsm_orom_has_chunk(orom, 2) ? "2k " : "", | |
2334 | imsm_orom_has_chunk(orom, 4) ? "4k " : "", | |
2335 | imsm_orom_has_chunk(orom, 8) ? "8k " : "", | |
2336 | imsm_orom_has_chunk(orom, 16) ? "16k " : "", | |
2337 | imsm_orom_has_chunk(orom, 32) ? "32k " : "", | |
2338 | imsm_orom_has_chunk(orom, 64) ? "64k " : "", | |
2339 | imsm_orom_has_chunk(orom, 128) ? "128k " : "", | |
2340 | imsm_orom_has_chunk(orom, 256) ? "256k " : "", | |
2341 | imsm_orom_has_chunk(orom, 512) ? "512k " : "", | |
2342 | imsm_orom_has_chunk(orom, 1024*1) ? "1M " : "", | |
2343 | imsm_orom_has_chunk(orom, 1024*2) ? "2M " : "", | |
2344 | imsm_orom_has_chunk(orom, 1024*4) ? "4M " : "", | |
2345 | imsm_orom_has_chunk(orom, 1024*8) ? "8M " : "", | |
2346 | imsm_orom_has_chunk(orom, 1024*16) ? "16M " : "", | |
2347 | imsm_orom_has_chunk(orom, 1024*32) ? "32M " : "", | |
2348 | imsm_orom_has_chunk(orom, 1024*64) ? "64M " : ""); | |
2349 | printf("IMSM_2TB_VOLUMES=%s\n",(orom->attr & IMSM_OROM_ATTR_2TB) ? "yes" : "no"); | |
2350 | printf("IMSM_2TB_DISKS=%s\n",(orom->attr & IMSM_OROM_ATTR_2TB_DISK) ? "yes" : "no"); | |
2351 | printf("IMSM_MAX_DISKS=%d\n",orom->tds); | |
2352 | printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom->vpa); | |
2353 | printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom->vphba); | |
2354 | } | |
2355 | ||
2356 | static int detail_platform_imsm(int verbose, int enumerate_only, char *controller_path) | |
2357 | { | |
2358 | /* There are two components to imsm platform support, the ahci SATA | |
2359 | * controller and the option-rom. To find the SATA controller we | |
2360 | * simply look in /sys/bus/pci/drivers/ahci to see if an ahci | |
2361 | * controller with the Intel vendor id is present. This approach | |
2362 | * allows mdadm to leverage the kernel's ahci detection logic, with the | |
2363 | * caveat that if ahci.ko is not loaded mdadm will not be able to | |
2364 | * detect platform raid capabilities. The option-rom resides in a | |
2365 | * platform "Adapter ROM". We scan for its signature to retrieve the | |
2366 | * platform capabilities. If raid support is disabled in the BIOS the | |
2367 | * option-rom capability structure will not be available. | |
2368 | */ | |
2369 | struct sys_dev *list, *hba; | |
2370 | int host_base = 0; | |
2371 | int port_count = 0; | |
2372 | int result=1; | |
2373 | ||
2374 | if (enumerate_only) { | |
2375 | if (check_env("IMSM_NO_PLATFORM")) | |
2376 | return 0; | |
2377 | list = find_intel_devices(); | |
2378 | if (!list) | |
2379 | return 2; | |
2380 | for (hba = list; hba; hba = hba->next) { | |
2381 | if (find_imsm_capability(hba)) { | |
2382 | result = 0; | |
2383 | break; | |
2384 | } | |
2385 | else | |
2386 | result = 2; | |
2387 | } | |
2388 | return result; | |
2389 | } | |
2390 | ||
2391 | list = find_intel_devices(); | |
2392 | if (!list) { | |
2393 | if (verbose > 0) | |
2394 | pr_err("no active Intel(R) RAID controller found.\n"); | |
2395 | return 2; | |
2396 | } else if (verbose > 0) | |
2397 | print_found_intel_controllers(list); | |
2398 | ||
2399 | for (hba = list; hba; hba = hba->next) { | |
2400 | if (controller_path && (compare_paths(hba->path, controller_path) != 0)) | |
2401 | continue; | |
2402 | if (!find_imsm_capability(hba)) { | |
2403 | char buf[PATH_MAX]; | |
2404 | pr_err("imsm capabilities not found for controller: %s (type %s)\n", | |
2405 | hba->type == SYS_DEV_VMD ? vmd_domain_to_controller(hba, buf) : hba->path, | |
2406 | get_sys_dev_type(hba->type)); | |
2407 | continue; | |
2408 | } | |
2409 | result = 0; | |
2410 | } | |
2411 | ||
2412 | if (controller_path && result == 1) { | |
2413 | pr_err("no active Intel(R) RAID controller found under %s\n", | |
2414 | controller_path); | |
2415 | return result; | |
2416 | } | |
2417 | ||
2418 | const struct orom_entry *entry; | |
2419 | ||
2420 | for (entry = orom_entries; entry; entry = entry->next) { | |
2421 | if (entry->type == SYS_DEV_VMD) { | |
2422 | print_imsm_capability(&entry->orom); | |
2423 | printf(" 3rd party NVMe :%s supported\n", | |
2424 | imsm_orom_has_tpv_support(&entry->orom)?"":" not"); | |
2425 | for (hba = list; hba; hba = hba->next) { | |
2426 | if (hba->type == SYS_DEV_VMD) { | |
2427 | char buf[PATH_MAX]; | |
2428 | printf(" I/O Controller : %s (%s)\n", | |
2429 | vmd_domain_to_controller(hba, buf), get_sys_dev_type(hba->type)); | |
2430 | if (print_vmd_attached_devs(hba)) { | |
2431 | if (verbose > 0) | |
2432 | pr_err("failed to get devices attached to VMD domain.\n"); | |
2433 | result |= 2; | |
2434 | } | |
2435 | } | |
2436 | } | |
2437 | printf("\n"); | |
2438 | continue; | |
2439 | } | |
2440 | ||
2441 | print_imsm_capability(&entry->orom); | |
2442 | if (entry->type == SYS_DEV_NVME) { | |
2443 | for (hba = list; hba; hba = hba->next) { | |
2444 | if (hba->type == SYS_DEV_NVME) | |
2445 | printf(" NVMe Device : %s\n", hba->path); | |
2446 | } | |
2447 | printf("\n"); | |
2448 | continue; | |
2449 | } | |
2450 | ||
2451 | struct devid_list *devid; | |
2452 | for (devid = entry->devid_list; devid; devid = devid->next) { | |
2453 | hba = device_by_id(devid->devid); | |
2454 | if (!hba) | |
2455 | continue; | |
2456 | ||
2457 | printf(" I/O Controller : %s (%s)\n", | |
2458 | hba->path, get_sys_dev_type(hba->type)); | |
2459 | if (hba->type == SYS_DEV_SATA) { | |
2460 | host_base = ahci_get_port_count(hba->path, &port_count); | |
2461 | if (ahci_enumerate_ports(hba->path, port_count, host_base, verbose)) { | |
2462 | if (verbose > 0) | |
2463 | pr_err("failed to enumerate ports on SATA controller at %s.\n", hba->pci_id); | |
2464 | result |= 2; | |
2465 | } | |
2466 | } | |
2467 | } | |
2468 | printf("\n"); | |
2469 | } | |
2470 | ||
2471 | return result; | |
2472 | } | |
2473 | ||
2474 | static int export_detail_platform_imsm(int verbose, char *controller_path) | |
2475 | { | |
2476 | struct sys_dev *list, *hba; | |
2477 | int result=1; | |
2478 | ||
2479 | list = find_intel_devices(); | |
2480 | if (!list) { | |
2481 | if (verbose > 0) | |
2482 | pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n"); | |
2483 | result = 2; | |
2484 | return result; | |
2485 | } | |
2486 | ||
2487 | for (hba = list; hba; hba = hba->next) { | |
2488 | if (controller_path && (compare_paths(hba->path,controller_path) != 0)) | |
2489 | continue; | |
2490 | if (!find_imsm_capability(hba) && verbose > 0) { | |
2491 | char buf[PATH_MAX]; | |
2492 | pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n", | |
2493 | hba->type == SYS_DEV_VMD ? vmd_domain_to_controller(hba, buf) : hba->path); | |
2494 | } | |
2495 | else | |
2496 | result = 0; | |
2497 | } | |
2498 | ||
2499 | const struct orom_entry *entry; | |
2500 | ||
2501 | for (entry = orom_entries; entry; entry = entry->next) { | |
2502 | if (entry->type == SYS_DEV_VMD) { | |
2503 | for (hba = list; hba; hba = hba->next) | |
2504 | print_imsm_capability_export(&entry->orom); | |
2505 | continue; | |
2506 | } | |
2507 | print_imsm_capability_export(&entry->orom); | |
2508 | } | |
2509 | ||
2510 | return result; | |
2511 | } | |
2512 | ||
2513 | #endif | |
2514 | ||
2515 | static int match_home_imsm(struct supertype *st, char *homehost) | |
2516 | { | |
2517 | /* the imsm metadata format does not specify any host | |
2518 | * identification information. We return -1 since we can never | |
2519 | * confirm nor deny whether a given array is "meant" for this | |
2520 | * host. We rely on compare_super and the 'family_num' fields to | |
2521 | * exclude member disks that do not belong, and we rely on | |
2522 | * mdadm.conf to specify the arrays that should be assembled. | |
2523 | * Auto-assembly may still pick up "foreign" arrays. | |
2524 | */ | |
2525 | ||
2526 | return -1; | |
2527 | } | |
2528 | ||
2529 | static void uuid_from_super_imsm(struct supertype *st, int uuid[4]) | |
2530 | { | |
2531 | /* The uuid returned here is used for: | |
2532 | * uuid to put into bitmap file (Create, Grow) | |
2533 | * uuid for backup header when saving critical section (Grow) | |
2534 | * comparing uuids when re-adding a device into an array | |
2535 | * In these cases the uuid required is that of the data-array, | |
2536 | * not the device-set. | |
2537 | * uuid to recognise same set when adding a missing device back | |
2538 | * to an array. This is a uuid for the device-set. | |
2539 | * | |
2540 | * For each of these we can make do with a truncated | |
2541 | * or hashed uuid rather than the original, as long as | |
2542 | * everyone agrees. | |
2543 | * In each case the uuid required is that of the data-array, | |
2544 | * not the device-set. | |
2545 | */ | |
2546 | /* imsm does not track uuid's so we synthesis one using sha1 on | |
2547 | * - The signature (Which is constant for all imsm array, but no matter) | |
2548 | * - the orig_family_num of the container | |
2549 | * - the index number of the volume | |
2550 | * - the 'serial' number of the volume. | |
2551 | * Hopefully these are all constant. | |
2552 | */ | |
2553 | struct intel_super *super = st->sb; | |
2554 | ||
2555 | char buf[20]; | |
2556 | struct sha1_ctx ctx; | |
2557 | struct imsm_dev *dev = NULL; | |
2558 | __u32 family_num; | |
2559 | ||
2560 | /* some mdadm versions failed to set ->orig_family_num, in which | |
2561 | * case fall back to ->family_num. orig_family_num will be | |
2562 | * fixed up with the first metadata update. | |
2563 | */ | |
2564 | family_num = super->anchor->orig_family_num; | |
2565 | if (family_num == 0) | |
2566 | family_num = super->anchor->family_num; | |
2567 | sha1_init_ctx(&ctx); | |
2568 | sha1_process_bytes(super->anchor->sig, MPB_SIG_LEN, &ctx); | |
2569 | sha1_process_bytes(&family_num, sizeof(__u32), &ctx); | |
2570 | if (super->current_vol >= 0) | |
2571 | dev = get_imsm_dev(super, super->current_vol); | |
2572 | if (dev) { | |
2573 | __u32 vol = super->current_vol; | |
2574 | sha1_process_bytes(&vol, sizeof(vol), &ctx); | |
2575 | sha1_process_bytes(dev->volume, MAX_RAID_SERIAL_LEN, &ctx); | |
2576 | } | |
2577 | sha1_finish_ctx(&ctx, buf); | |
2578 | memcpy(uuid, buf, 4*4); | |
2579 | } | |
2580 | ||
2581 | #if 0 | |
2582 | static void | |
2583 | get_imsm_numerical_version(struct imsm_super *mpb, int *m, int *p) | |
2584 | { | |
2585 | __u8 *v = get_imsm_version(mpb); | |
2586 | __u8 *end = mpb->sig + MAX_SIGNATURE_LENGTH; | |
2587 | char major[] = { 0, 0, 0 }; | |
2588 | char minor[] = { 0 ,0, 0 }; | |
2589 | char patch[] = { 0, 0, 0 }; | |
2590 | char *ver_parse[] = { major, minor, patch }; | |
2591 | int i, j; | |
2592 | ||
2593 | i = j = 0; | |
2594 | while (*v != '\0' && v < end) { | |
2595 | if (*v != '.' && j < 2) | |
2596 | ver_parse[i][j++] = *v; | |
2597 | else { | |
2598 | i++; | |
2599 | j = 0; | |
2600 | } | |
2601 | v++; | |
2602 | } | |
2603 | ||
2604 | *m = strtol(minor, NULL, 0); | |
2605 | *p = strtol(patch, NULL, 0); | |
2606 | } | |
2607 | #endif | |
2608 | ||
2609 | static __u32 migr_strip_blocks_resync(struct imsm_dev *dev) | |
2610 | { | |
2611 | /* migr_strip_size when repairing or initializing parity */ | |
2612 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
2613 | __u32 chunk = __le32_to_cpu(map->blocks_per_strip); | |
2614 | ||
2615 | switch (get_imsm_raid_level(map)) { | |
2616 | case 5: | |
2617 | case 10: | |
2618 | return chunk; | |
2619 | default: | |
2620 | return 128*1024 >> 9; | |
2621 | } | |
2622 | } | |
2623 | ||
2624 | static __u32 migr_strip_blocks_rebuild(struct imsm_dev *dev) | |
2625 | { | |
2626 | /* migr_strip_size when rebuilding a degraded disk, no idea why | |
2627 | * this is different than migr_strip_size_resync(), but it's good | |
2628 | * to be compatible | |
2629 | */ | |
2630 | struct imsm_map *map = get_imsm_map(dev, MAP_1); | |
2631 | __u32 chunk = __le32_to_cpu(map->blocks_per_strip); | |
2632 | ||
2633 | switch (get_imsm_raid_level(map)) { | |
2634 | case 1: | |
2635 | case 10: | |
2636 | if (map->num_members % map->num_domains == 0) | |
2637 | return 128*1024 >> 9; | |
2638 | else | |
2639 | return chunk; | |
2640 | case 5: | |
2641 | return max((__u32) 64*1024 >> 9, chunk); | |
2642 | default: | |
2643 | return 128*1024 >> 9; | |
2644 | } | |
2645 | } | |
2646 | ||
2647 | static __u32 num_stripes_per_unit_resync(struct imsm_dev *dev) | |
2648 | { | |
2649 | struct imsm_map *lo = get_imsm_map(dev, MAP_0); | |
2650 | struct imsm_map *hi = get_imsm_map(dev, MAP_1); | |
2651 | __u32 lo_chunk = __le32_to_cpu(lo->blocks_per_strip); | |
2652 | __u32 hi_chunk = __le32_to_cpu(hi->blocks_per_strip); | |
2653 | ||
2654 | return max((__u32) 1, hi_chunk / lo_chunk); | |
2655 | } | |
2656 | ||
2657 | static __u32 num_stripes_per_unit_rebuild(struct imsm_dev *dev) | |
2658 | { | |
2659 | struct imsm_map *lo = get_imsm_map(dev, MAP_0); | |
2660 | int level = get_imsm_raid_level(lo); | |
2661 | ||
2662 | if (level == 1 || level == 10) { | |
2663 | struct imsm_map *hi = get_imsm_map(dev, MAP_1); | |
2664 | ||
2665 | return hi->num_domains; | |
2666 | } else | |
2667 | return num_stripes_per_unit_resync(dev); | |
2668 | } | |
2669 | ||
2670 | static __u8 imsm_num_data_members(struct imsm_dev *dev, int second_map) | |
2671 | { | |
2672 | /* named 'imsm_' because raid0, raid1 and raid10 | |
2673 | * counter-intuitively have the same number of data disks | |
2674 | */ | |
2675 | struct imsm_map *map = get_imsm_map(dev, second_map); | |
2676 | ||
2677 | switch (get_imsm_raid_level(map)) { | |
2678 | case 0: | |
2679 | return map->num_members; | |
2680 | break; | |
2681 | case 1: | |
2682 | case 10: | |
2683 | return map->num_members/2; | |
2684 | case 5: | |
2685 | return map->num_members - 1; | |
2686 | default: | |
2687 | dprintf("unsupported raid level\n"); | |
2688 | return 0; | |
2689 | } | |
2690 | } | |
2691 | ||
2692 | static __u32 parity_segment_depth(struct imsm_dev *dev) | |
2693 | { | |
2694 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
2695 | __u32 chunk = __le32_to_cpu(map->blocks_per_strip); | |
2696 | ||
2697 | switch(get_imsm_raid_level(map)) { | |
2698 | case 1: | |
2699 | case 10: | |
2700 | return chunk * map->num_domains; | |
2701 | case 5: | |
2702 | return chunk * map->num_members; | |
2703 | default: | |
2704 | return chunk; | |
2705 | } | |
2706 | } | |
2707 | ||
2708 | static __u32 map_migr_block(struct imsm_dev *dev, __u32 block) | |
2709 | { | |
2710 | struct imsm_map *map = get_imsm_map(dev, MAP_1); | |
2711 | __u32 chunk = __le32_to_cpu(map->blocks_per_strip); | |
2712 | __u32 strip = block / chunk; | |
2713 | ||
2714 | switch (get_imsm_raid_level(map)) { | |
2715 | case 1: | |
2716 | case 10: { | |
2717 | __u32 vol_strip = (strip * map->num_domains) + 1; | |
2718 | __u32 vol_stripe = vol_strip / map->num_members; | |
2719 | ||
2720 | return vol_stripe * chunk + block % chunk; | |
2721 | } case 5: { | |
2722 | __u32 stripe = strip / (map->num_members - 1); | |
2723 | ||
2724 | return stripe * chunk + block % chunk; | |
2725 | } | |
2726 | default: | |
2727 | return 0; | |
2728 | } | |
2729 | } | |
2730 | ||
2731 | static __u64 blocks_per_migr_unit(struct intel_super *super, | |
2732 | struct imsm_dev *dev) | |
2733 | { | |
2734 | /* calculate the conversion factor between per member 'blocks' | |
2735 | * (md/{resync,rebuild}_start) and imsm migration units, return | |
2736 | * 0 for the 'not migrating' and 'unsupported migration' cases | |
2737 | */ | |
2738 | if (!dev->vol.migr_state) | |
2739 | return 0; | |
2740 | ||
2741 | switch (migr_type(dev)) { | |
2742 | case MIGR_GEN_MIGR: { | |
2743 | struct migr_record *migr_rec = super->migr_rec; | |
2744 | return __le32_to_cpu(migr_rec->blocks_per_unit); | |
2745 | } | |
2746 | case MIGR_VERIFY: | |
2747 | case MIGR_REPAIR: | |
2748 | case MIGR_INIT: { | |
2749 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
2750 | __u32 stripes_per_unit; | |
2751 | __u32 blocks_per_unit; | |
2752 | __u32 parity_depth; | |
2753 | __u32 migr_chunk; | |
2754 | __u32 block_map; | |
2755 | __u32 block_rel; | |
2756 | __u32 segment; | |
2757 | __u32 stripe; | |
2758 | __u8 disks; | |
2759 | ||
2760 | /* yes, this is really the translation of migr_units to | |
2761 | * per-member blocks in the 'resync' case | |
2762 | */ | |
2763 | stripes_per_unit = num_stripes_per_unit_resync(dev); | |
2764 | migr_chunk = migr_strip_blocks_resync(dev); | |
2765 | disks = imsm_num_data_members(dev, MAP_0); | |
2766 | blocks_per_unit = stripes_per_unit * migr_chunk * disks; | |
2767 | stripe = __le16_to_cpu(map->blocks_per_strip) * disks; | |
2768 | segment = blocks_per_unit / stripe; | |
2769 | block_rel = blocks_per_unit - segment * stripe; | |
2770 | parity_depth = parity_segment_depth(dev); | |
2771 | block_map = map_migr_block(dev, block_rel); | |
2772 | return block_map + parity_depth * segment; | |
2773 | } | |
2774 | case MIGR_REBUILD: { | |
2775 | __u32 stripes_per_unit; | |
2776 | __u32 migr_chunk; | |
2777 | ||
2778 | stripes_per_unit = num_stripes_per_unit_rebuild(dev); | |
2779 | migr_chunk = migr_strip_blocks_rebuild(dev); | |
2780 | return migr_chunk * stripes_per_unit; | |
2781 | } | |
2782 | case MIGR_STATE_CHANGE: | |
2783 | default: | |
2784 | return 0; | |
2785 | } | |
2786 | } | |
2787 | ||
2788 | static int imsm_level_to_layout(int level) | |
2789 | { | |
2790 | switch (level) { | |
2791 | case 0: | |
2792 | case 1: | |
2793 | return 0; | |
2794 | case 5: | |
2795 | case 6: | |
2796 | return ALGORITHM_LEFT_ASYMMETRIC; | |
2797 | case 10: | |
2798 | return 0x102; | |
2799 | } | |
2800 | return UnSet; | |
2801 | } | |
2802 | ||
2803 | /******************************************************************************* | |
2804 | * Function: read_imsm_migr_rec | |
2805 | * Description: Function reads imsm migration record from last sector of disk | |
2806 | * Parameters: | |
2807 | * fd : disk descriptor | |
2808 | * super : metadata info | |
2809 | * Returns: | |
2810 | * 0 : success, | |
2811 | * -1 : fail | |
2812 | ******************************************************************************/ | |
2813 | static int read_imsm_migr_rec(int fd, struct intel_super *super) | |
2814 | { | |
2815 | int ret_val = -1; | |
2816 | unsigned int sector_size = super->sector_size; | |
2817 | unsigned long long dsize; | |
2818 | ||
2819 | get_dev_size(fd, NULL, &dsize); | |
2820 | if (lseek64(fd, dsize - (sector_size*MIGR_REC_SECTOR_POSITION), | |
2821 | SEEK_SET) < 0) { | |
2822 | pr_err("Cannot seek to anchor block: %s\n", | |
2823 | strerror(errno)); | |
2824 | goto out; | |
2825 | } | |
2826 | if ((unsigned int)read(fd, super->migr_rec_buf, | |
2827 | MIGR_REC_BUF_SECTORS*sector_size) != | |
2828 | MIGR_REC_BUF_SECTORS*sector_size) { | |
2829 | pr_err("Cannot read migr record block: %s\n", | |
2830 | strerror(errno)); | |
2831 | goto out; | |
2832 | } | |
2833 | ret_val = 0; | |
2834 | if (sector_size == 4096) | |
2835 | convert_from_4k_imsm_migr_rec(super); | |
2836 | ||
2837 | out: | |
2838 | return ret_val; | |
2839 | } | |
2840 | ||
2841 | static struct imsm_dev *imsm_get_device_during_migration( | |
2842 | struct intel_super *super) | |
2843 | { | |
2844 | ||
2845 | struct intel_dev *dv; | |
2846 | ||
2847 | for (dv = super->devlist; dv; dv = dv->next) { | |
2848 | if (is_gen_migration(dv->dev)) | |
2849 | return dv->dev; | |
2850 | } | |
2851 | return NULL; | |
2852 | } | |
2853 | ||
2854 | /******************************************************************************* | |
2855 | * Function: load_imsm_migr_rec | |
2856 | * Description: Function reads imsm migration record (it is stored at the last | |
2857 | * sector of disk) | |
2858 | * Parameters: | |
2859 | * super : imsm internal array info | |
2860 | * info : general array info | |
2861 | * Returns: | |
2862 | * 0 : success | |
2863 | * -1 : fail | |
2864 | * -2 : no migration in progress | |
2865 | ******************************************************************************/ | |
2866 | static int load_imsm_migr_rec(struct intel_super *super, struct mdinfo *info) | |
2867 | { | |
2868 | struct mdinfo *sd; | |
2869 | struct dl *dl; | |
2870 | char nm[30]; | |
2871 | int retval = -1; | |
2872 | int fd = -1; | |
2873 | struct imsm_dev *dev; | |
2874 | struct imsm_map *map; | |
2875 | int slot = -1; | |
2876 | ||
2877 | /* find map under migration */ | |
2878 | dev = imsm_get_device_during_migration(super); | |
2879 | /* nothing to load,no migration in progress? | |
2880 | */ | |
2881 | if (dev == NULL) | |
2882 | return -2; | |
2883 | ||
2884 | if (info) { | |
2885 | for (sd = info->devs ; sd ; sd = sd->next) { | |
2886 | /* read only from one of the first two slots */ | |
2887 | if ((sd->disk.raid_disk < 0) || | |
2888 | (sd->disk.raid_disk > 1)) | |
2889 | continue; | |
2890 | ||
2891 | sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor); | |
2892 | fd = dev_open(nm, O_RDONLY); | |
2893 | if (fd >= 0) | |
2894 | break; | |
2895 | } | |
2896 | } | |
2897 | if (fd < 0) { | |
2898 | map = get_imsm_map(dev, MAP_0); | |
2899 | for (dl = super->disks; dl; dl = dl->next) { | |
2900 | /* skip spare and failed disks | |
2901 | */ | |
2902 | if (dl->index < 0) | |
2903 | continue; | |
2904 | /* read only from one of the first two slots */ | |
2905 | if (map) | |
2906 | slot = get_imsm_disk_slot(map, dl->index); | |
2907 | if (map == NULL || slot > 1 || slot < 0) | |
2908 | continue; | |
2909 | sprintf(nm, "%d:%d", dl->major, dl->minor); | |
2910 | fd = dev_open(nm, O_RDONLY); | |
2911 | if (fd >= 0) | |
2912 | break; | |
2913 | } | |
2914 | } | |
2915 | if (fd < 0) | |
2916 | goto out; | |
2917 | retval = read_imsm_migr_rec(fd, super); | |
2918 | ||
2919 | out: | |
2920 | if (fd >= 0) | |
2921 | close(fd); | |
2922 | return retval; | |
2923 | } | |
2924 | ||
2925 | #ifndef MDASSEMBLE | |
2926 | /******************************************************************************* | |
2927 | * function: imsm_create_metadata_checkpoint_update | |
2928 | * Description: It creates update for checkpoint change. | |
2929 | * Parameters: | |
2930 | * super : imsm internal array info | |
2931 | * u : pointer to prepared update | |
2932 | * Returns: | |
2933 | * Uptate length. | |
2934 | * If length is equal to 0, input pointer u contains no update | |
2935 | ******************************************************************************/ | |
2936 | static int imsm_create_metadata_checkpoint_update( | |
2937 | struct intel_super *super, | |
2938 | struct imsm_update_general_migration_checkpoint **u) | |
2939 | { | |
2940 | ||
2941 | int update_memory_size = 0; | |
2942 | ||
2943 | dprintf("(enter)\n"); | |
2944 | ||
2945 | if (u == NULL) | |
2946 | return 0; | |
2947 | *u = NULL; | |
2948 | ||
2949 | /* size of all update data without anchor */ | |
2950 | update_memory_size = | |
2951 | sizeof(struct imsm_update_general_migration_checkpoint); | |
2952 | ||
2953 | *u = xcalloc(1, update_memory_size); | |
2954 | if (*u == NULL) { | |
2955 | dprintf("error: cannot get memory\n"); | |
2956 | return 0; | |
2957 | } | |
2958 | (*u)->type = update_general_migration_checkpoint; | |
2959 | (*u)->curr_migr_unit = __le32_to_cpu(super->migr_rec->curr_migr_unit); | |
2960 | dprintf("prepared for %u\n", (*u)->curr_migr_unit); | |
2961 | ||
2962 | return update_memory_size; | |
2963 | } | |
2964 | ||
2965 | static void imsm_update_metadata_locally(struct supertype *st, | |
2966 | void *buf, int len); | |
2967 | ||
2968 | /******************************************************************************* | |
2969 | * Function: write_imsm_migr_rec | |
2970 | * Description: Function writes imsm migration record | |
2971 | * (at the last sector of disk) | |
2972 | * Parameters: | |
2973 | * super : imsm internal array info | |
2974 | * Returns: | |
2975 | * 0 : success | |
2976 | * -1 : if fail | |
2977 | ******************************************************************************/ | |
2978 | static int write_imsm_migr_rec(struct supertype *st) | |
2979 | { | |
2980 | struct intel_super *super = st->sb; | |
2981 | unsigned int sector_size = super->sector_size; | |
2982 | unsigned long long dsize; | |
2983 | char nm[30]; | |
2984 | int fd = -1; | |
2985 | int retval = -1; | |
2986 | struct dl *sd; | |
2987 | int len; | |
2988 | struct imsm_update_general_migration_checkpoint *u; | |
2989 | struct imsm_dev *dev; | |
2990 | struct imsm_map *map; | |
2991 | ||
2992 | /* find map under migration */ | |
2993 | dev = imsm_get_device_during_migration(super); | |
2994 | /* if no migration, write buffer anyway to clear migr_record | |
2995 | * on disk based on first available device | |
2996 | */ | |
2997 | if (dev == NULL) | |
2998 | dev = get_imsm_dev(super, super->current_vol < 0 ? 0 : | |
2999 | super->current_vol); | |
3000 | ||
3001 | map = get_imsm_map(dev, MAP_0); | |
3002 | ||
3003 | if (sector_size == 4096) | |
3004 | convert_to_4k_imsm_migr_rec(super); | |
3005 | for (sd = super->disks ; sd ; sd = sd->next) { | |
3006 | int slot = -1; | |
3007 | ||
3008 | /* skip failed and spare devices */ | |
3009 | if (sd->index < 0) | |
3010 | continue; | |
3011 | /* write to 2 first slots only */ | |
3012 | if (map) | |
3013 | slot = get_imsm_disk_slot(map, sd->index); | |
3014 | if (map == NULL || slot > 1 || slot < 0) | |
3015 | continue; | |
3016 | ||
3017 | sprintf(nm, "%d:%d", sd->major, sd->minor); | |
3018 | fd = dev_open(nm, O_RDWR); | |
3019 | if (fd < 0) | |
3020 | continue; | |
3021 | get_dev_size(fd, NULL, &dsize); | |
3022 | if (lseek64(fd, dsize - (MIGR_REC_SECTOR_POSITION*sector_size), | |
3023 | SEEK_SET) < 0) { | |
3024 | pr_err("Cannot seek to anchor block: %s\n", | |
3025 | strerror(errno)); | |
3026 | goto out; | |
3027 | } | |
3028 | if ((unsigned int)write(fd, super->migr_rec_buf, | |
3029 | MIGR_REC_BUF_SECTORS*sector_size) != | |
3030 | MIGR_REC_BUF_SECTORS*sector_size) { | |
3031 | pr_err("Cannot write migr record block: %s\n", | |
3032 | strerror(errno)); | |
3033 | goto out; | |
3034 | } | |
3035 | close(fd); | |
3036 | fd = -1; | |
3037 | } | |
3038 | if (sector_size == 4096) | |
3039 | convert_from_4k_imsm_migr_rec(super); | |
3040 | /* update checkpoint information in metadata */ | |
3041 | len = imsm_create_metadata_checkpoint_update(super, &u); | |
3042 | if (len <= 0) { | |
3043 | dprintf("imsm: Cannot prepare update\n"); | |
3044 | goto out; | |
3045 | } | |
3046 | /* update metadata locally */ | |
3047 | imsm_update_metadata_locally(st, u, len); | |
3048 | /* and possibly remotely */ | |
3049 | if (st->update_tail) { | |
3050 | append_metadata_update(st, u, len); | |
3051 | /* during reshape we do all work inside metadata handler | |
3052 | * manage_reshape(), so metadata update has to be triggered | |
3053 | * insida it | |
3054 | */ | |
3055 | flush_metadata_updates(st); | |
3056 | st->update_tail = &st->updates; | |
3057 | } else | |
3058 | free(u); | |
3059 | ||
3060 | retval = 0; | |
3061 | out: | |
3062 | if (fd >= 0) | |
3063 | close(fd); | |
3064 | return retval; | |
3065 | } | |
3066 | #endif /* MDASSEMBLE */ | |
3067 | ||
3068 | /* spare/missing disks activations are not allowe when | |
3069 | * array/container performs reshape operation, because | |
3070 | * all arrays in container works on the same disks set | |
3071 | */ | |
3072 | int imsm_reshape_blocks_arrays_changes(struct intel_super *super) | |
3073 | { | |
3074 | int rv = 0; | |
3075 | struct intel_dev *i_dev; | |
3076 | struct imsm_dev *dev; | |
3077 | ||
3078 | /* check whole container | |
3079 | */ | |
3080 | for (i_dev = super->devlist; i_dev; i_dev = i_dev->next) { | |
3081 | dev = i_dev->dev; | |
3082 | if (is_gen_migration(dev)) { | |
3083 | /* No repair during any migration in container | |
3084 | */ | |
3085 | rv = 1; | |
3086 | break; | |
3087 | } | |
3088 | } | |
3089 | return rv; | |
3090 | } | |
3091 | static unsigned long long imsm_component_size_aligment_check(int level, | |
3092 | int chunk_size, | |
3093 | unsigned int sector_size, | |
3094 | unsigned long long component_size) | |
3095 | { | |
3096 | unsigned int component_size_alligment; | |
3097 | ||
3098 | /* check component size aligment | |
3099 | */ | |
3100 | component_size_alligment = component_size % (chunk_size/sector_size); | |
3101 | ||
3102 | dprintf("(Level: %i, chunk_size = %i, component_size = %llu), component_size_alligment = %u\n", | |
3103 | level, chunk_size, component_size, | |
3104 | component_size_alligment); | |
3105 | ||
3106 | if (component_size_alligment && (level != 1) && (level != UnSet)) { | |
3107 | dprintf("imsm: reported component size alligned from %llu ", | |
3108 | component_size); | |
3109 | component_size -= component_size_alligment; | |
3110 | dprintf_cont("to %llu (%i).\n", | |
3111 | component_size, component_size_alligment); | |
3112 | } | |
3113 | ||
3114 | return component_size; | |
3115 | } | |
3116 | ||
3117 | static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info, char *dmap) | |
3118 | { | |
3119 | struct intel_super *super = st->sb; | |
3120 | struct migr_record *migr_rec = super->migr_rec; | |
3121 | struct imsm_dev *dev = get_imsm_dev(super, super->current_vol); | |
3122 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
3123 | struct imsm_map *prev_map = get_imsm_map(dev, MAP_1); | |
3124 | struct imsm_map *map_to_analyse = map; | |
3125 | struct dl *dl; | |
3126 | int map_disks = info->array.raid_disks; | |
3127 | ||
3128 | memset(info, 0, sizeof(*info)); | |
3129 | if (prev_map) | |
3130 | map_to_analyse = prev_map; | |
3131 | ||
3132 | dl = super->current_disk; | |
3133 | ||
3134 | info->container_member = super->current_vol; | |
3135 | info->array.raid_disks = map->num_members; | |
3136 | info->array.level = get_imsm_raid_level(map_to_analyse); | |
3137 | info->array.layout = imsm_level_to_layout(info->array.level); | |
3138 | info->array.md_minor = -1; | |
3139 | info->array.ctime = 0; | |
3140 | info->array.utime = 0; | |
3141 | info->array.chunk_size = | |
3142 | __le16_to_cpu(map_to_analyse->blocks_per_strip) << 9; | |
3143 | info->array.state = !dev->vol.dirty; | |
3144 | info->custom_array_size = __le32_to_cpu(dev->size_high); | |
3145 | info->custom_array_size <<= 32; | |
3146 | info->custom_array_size |= __le32_to_cpu(dev->size_low); | |
3147 | info->recovery_blocked = imsm_reshape_blocks_arrays_changes(st->sb); | |
3148 | ||
3149 | if (is_gen_migration(dev)) { | |
3150 | info->reshape_active = 1; | |
3151 | info->new_level = get_imsm_raid_level(map); | |
3152 | info->new_layout = imsm_level_to_layout(info->new_level); | |
3153 | info->new_chunk = __le16_to_cpu(map->blocks_per_strip) << 9; | |
3154 | info->delta_disks = map->num_members - prev_map->num_members; | |
3155 | if (info->delta_disks) { | |
3156 | /* this needs to be applied to every array | |
3157 | * in the container. | |
3158 | */ | |
3159 | info->reshape_active = CONTAINER_RESHAPE; | |
3160 | } | |
3161 | /* We shape information that we give to md might have to be | |
3162 | * modify to cope with md's requirement for reshaping arrays. | |
3163 | * For example, when reshaping a RAID0, md requires it to be | |
3164 | * presented as a degraded RAID4. | |
3165 | * Also if a RAID0 is migrating to a RAID5 we need to specify | |
3166 | * the array as already being RAID5, but the 'before' layout | |
3167 | * is a RAID4-like layout. | |
3168 | */ | |
3169 | switch (info->array.level) { | |
3170 | case 0: | |
3171 | switch(info->new_level) { | |
3172 | case 0: | |
3173 | /* conversion is happening as RAID4 */ | |
3174 | info->array.level = 4; | |
3175 | info->array.raid_disks += 1; | |
3176 | break; | |
3177 | case 5: | |
3178 | /* conversion is happening as RAID5 */ | |
3179 | info->array.level = 5; | |
3180 | info->array.layout = ALGORITHM_PARITY_N; | |
3181 | info->delta_disks -= 1; | |
3182 | break; | |
3183 | default: | |
3184 | /* FIXME error message */ | |
3185 | info->array.level = UnSet; | |
3186 | break; | |
3187 | } | |
3188 | break; | |
3189 | } | |
3190 | } else { | |
3191 | info->new_level = UnSet; | |
3192 | info->new_layout = UnSet; | |
3193 | info->new_chunk = info->array.chunk_size; | |
3194 | info->delta_disks = 0; | |
3195 | } | |
3196 | ||
3197 | if (dl) { | |
3198 | info->disk.major = dl->major; | |
3199 | info->disk.minor = dl->minor; | |
3200 | info->disk.number = dl->index; | |
3201 | info->disk.raid_disk = get_imsm_disk_slot(map_to_analyse, | |
3202 | dl->index); | |
3203 | } | |
3204 | ||
3205 | info->data_offset = pba_of_lba0(map_to_analyse); | |
3206 | ||
3207 | if (info->array.level == 5) { | |
3208 | info->component_size = num_data_stripes(map_to_analyse) * | |
3209 | map_to_analyse->blocks_per_strip; | |
3210 | } else { | |
3211 | info->component_size = blocks_per_member(map_to_analyse); | |
3212 | } | |
3213 | ||
3214 | info->component_size = imsm_component_size_aligment_check( | |
3215 | info->array.level, | |
3216 | info->array.chunk_size, | |
3217 | super->sector_size, | |
3218 | info->component_size); | |
3219 | info->bb.supported = 1; | |
3220 | ||
3221 | memset(info->uuid, 0, sizeof(info->uuid)); | |
3222 | info->recovery_start = MaxSector; | |
3223 | ||
3224 | info->reshape_progress = 0; | |
3225 | info->resync_start = MaxSector; | |
3226 | if ((map_to_analyse->map_state == IMSM_T_STATE_UNINITIALIZED || | |
3227 | dev->vol.dirty) && | |
3228 | imsm_reshape_blocks_arrays_changes(super) == 0) { | |
3229 | info->resync_start = 0; | |
3230 | } | |
3231 | if (dev->vol.migr_state) { | |
3232 | switch (migr_type(dev)) { | |
3233 | case MIGR_REPAIR: | |
3234 | case MIGR_INIT: { | |
3235 | __u64 blocks_per_unit = blocks_per_migr_unit(super, | |
3236 | dev); | |
3237 | __u64 units = __le32_to_cpu(dev->vol.curr_migr_unit); | |
3238 | ||
3239 | info->resync_start = blocks_per_unit * units; | |
3240 | break; | |
3241 | } | |
3242 | case MIGR_GEN_MIGR: { | |
3243 | __u64 blocks_per_unit = blocks_per_migr_unit(super, | |
3244 | dev); | |
3245 | __u64 units = __le32_to_cpu(migr_rec->curr_migr_unit); | |
3246 | unsigned long long array_blocks; | |
3247 | int used_disks; | |
3248 | ||
3249 | if (__le32_to_cpu(migr_rec->ascending_migr) && | |
3250 | (units < | |
3251 | (__le32_to_cpu(migr_rec->num_migr_units)-1)) && | |
3252 | (super->migr_rec->rec_status == | |
3253 | __cpu_to_le32(UNIT_SRC_IN_CP_AREA))) | |
3254 | units++; | |
3255 | ||
3256 | info->reshape_progress = blocks_per_unit * units; | |
3257 | ||
3258 | dprintf("IMSM: General Migration checkpoint : %llu (%llu) -> read reshape progress : %llu\n", | |
3259 | (unsigned long long)units, | |
3260 | (unsigned long long)blocks_per_unit, | |
3261 | info->reshape_progress); | |
3262 | ||
3263 | used_disks = imsm_num_data_members(dev, MAP_1); | |
3264 | if (used_disks > 0) { | |
3265 | array_blocks = blocks_per_member(map) * | |
3266 | used_disks; | |
3267 | /* round array size down to closest MB | |
3268 | */ | |
3269 | info->custom_array_size = (array_blocks | |
3270 | >> SECT_PER_MB_SHIFT) | |
3271 | << SECT_PER_MB_SHIFT; | |
3272 | } | |
3273 | } | |
3274 | case MIGR_VERIFY: | |
3275 | /* we could emulate the checkpointing of | |
3276 | * 'sync_action=check' migrations, but for now | |
3277 | * we just immediately complete them | |
3278 | */ | |
3279 | case MIGR_REBUILD: | |
3280 | /* this is handled by container_content_imsm() */ | |
3281 | case MIGR_STATE_CHANGE: | |
3282 | /* FIXME handle other migrations */ | |
3283 | default: | |
3284 | /* we are not dirty, so... */ | |
3285 | info->resync_start = MaxSector; | |
3286 | } | |
3287 | } | |
3288 | ||
3289 | strncpy(info->name, (char *) dev->volume, MAX_RAID_SERIAL_LEN); | |
3290 | info->name[MAX_RAID_SERIAL_LEN] = 0; | |
3291 | ||
3292 | info->array.major_version = -1; | |
3293 | info->array.minor_version = -2; | |
3294 | sprintf(info->text_version, "/%s/%d", st->container_devnm, info->container_member); | |
3295 | info->safe_mode_delay = 4000; /* 4 secs like the Matrix driver */ | |
3296 | uuid_from_super_imsm(st, info->uuid); | |
3297 | ||
3298 | if (dmap) { | |
3299 | int i, j; | |
3300 | for (i=0; i<map_disks; i++) { | |
3301 | dmap[i] = 0; | |
3302 | if (i < info->array.raid_disks) { | |
3303 | struct imsm_disk *dsk; | |
3304 | j = get_imsm_disk_idx(dev, i, MAP_X); | |
3305 | dsk = get_imsm_disk(super, j); | |
3306 | if (dsk && (dsk->status & CONFIGURED_DISK)) | |
3307 | dmap[i] = 1; | |
3308 | } | |
3309 | } | |
3310 | } | |
3311 | } | |
3312 | ||
3313 | static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev, | |
3314 | int failed, int look_in_map); | |
3315 | ||
3316 | static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev, | |
3317 | int look_in_map); | |
3318 | ||
3319 | #ifndef MDASSEMBLE | |
3320 | static void manage_second_map(struct intel_super *super, struct imsm_dev *dev) | |
3321 | { | |
3322 | if (is_gen_migration(dev)) { | |
3323 | int failed; | |
3324 | __u8 map_state; | |
3325 | struct imsm_map *map2 = get_imsm_map(dev, MAP_1); | |
3326 | ||
3327 | failed = imsm_count_failed(super, dev, MAP_1); | |
3328 | map_state = imsm_check_degraded(super, dev, failed, MAP_1); | |
3329 | if (map2->map_state != map_state) { | |
3330 | map2->map_state = map_state; | |
3331 | super->updates_pending++; | |
3332 | } | |
3333 | } | |
3334 | } | |
3335 | #endif | |
3336 | ||
3337 | static struct imsm_disk *get_imsm_missing(struct intel_super *super, __u8 index) | |
3338 | { | |
3339 | struct dl *d; | |
3340 | ||
3341 | for (d = super->missing; d; d = d->next) | |
3342 | if (d->index == index) | |
3343 | return &d->disk; | |
3344 | return NULL; | |
3345 | } | |
3346 | ||
3347 | static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map) | |
3348 | { | |
3349 | struct intel_super *super = st->sb; | |
3350 | struct imsm_disk *disk; | |
3351 | int map_disks = info->array.raid_disks; | |
3352 | int max_enough = -1; | |
3353 | int i; | |
3354 | struct imsm_super *mpb; | |
3355 | ||
3356 | if (super->current_vol >= 0) { | |
3357 | getinfo_super_imsm_volume(st, info, map); | |
3358 | return; | |
3359 | } | |
3360 | memset(info, 0, sizeof(*info)); | |
3361 | ||
3362 | /* Set raid_disks to zero so that Assemble will always pull in valid | |
3363 | * spares | |
3364 | */ | |
3365 | info->array.raid_disks = 0; | |
3366 | info->array.level = LEVEL_CONTAINER; | |
3367 | info->array.layout = 0; | |
3368 | info->array.md_minor = -1; | |
3369 | info->array.ctime = 0; /* N/A for imsm */ | |
3370 | info->array.utime = 0; | |
3371 | info->array.chunk_size = 0; | |
3372 | ||
3373 | info->disk.major = 0; | |
3374 | info->disk.minor = 0; | |
3375 | info->disk.raid_disk = -1; | |
3376 | info->reshape_active = 0; | |
3377 | info->array.major_version = -1; | |
3378 | info->array.minor_version = -2; | |
3379 | strcpy(info->text_version, "imsm"); | |
3380 | info->safe_mode_delay = 0; | |
3381 | info->disk.number = -1; | |
3382 | info->disk.state = 0; | |
3383 | info->name[0] = 0; | |
3384 | info->recovery_start = MaxSector; | |
3385 | info->recovery_blocked = imsm_reshape_blocks_arrays_changes(st->sb); | |
3386 | info->bb.supported = 1; | |
3387 | ||
3388 | /* do we have the all the insync disks that we expect? */ | |
3389 | mpb = super->anchor; | |
3390 | info->events = __le32_to_cpu(mpb->generation_num); | |
3391 | ||
3392 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
3393 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
3394 | int failed, enough, j, missing = 0; | |
3395 | struct imsm_map *map; | |
3396 | __u8 state; | |
3397 | ||
3398 | failed = imsm_count_failed(super, dev, MAP_0); | |
3399 | state = imsm_check_degraded(super, dev, failed, MAP_0); | |
3400 | map = get_imsm_map(dev, MAP_0); | |
3401 | ||
3402 | /* any newly missing disks? | |
3403 | * (catches single-degraded vs double-degraded) | |
3404 | */ | |
3405 | for (j = 0; j < map->num_members; j++) { | |
3406 | __u32 ord = get_imsm_ord_tbl_ent(dev, j, MAP_0); | |
3407 | __u32 idx = ord_to_idx(ord); | |
3408 | ||
3409 | if (!(ord & IMSM_ORD_REBUILD) && | |
3410 | get_imsm_missing(super, idx)) { | |
3411 | missing = 1; | |
3412 | break; | |
3413 | } | |
3414 | } | |
3415 | ||
3416 | if (state == IMSM_T_STATE_FAILED) | |
3417 | enough = -1; | |
3418 | else if (state == IMSM_T_STATE_DEGRADED && | |
3419 | (state != map->map_state || missing)) | |
3420 | enough = 0; | |
3421 | else /* we're normal, or already degraded */ | |
3422 | enough = 1; | |
3423 | if (is_gen_migration(dev) && missing) { | |
3424 | /* during general migration we need all disks | |
3425 | * that process is running on. | |
3426 | * No new missing disk is allowed. | |
3427 | */ | |
3428 | max_enough = -1; | |
3429 | enough = -1; | |
3430 | /* no more checks necessary | |
3431 | */ | |
3432 | break; | |
3433 | } | |
3434 | /* in the missing/failed disk case check to see | |
3435 | * if at least one array is runnable | |
3436 | */ | |
3437 | max_enough = max(max_enough, enough); | |
3438 | } | |
3439 | dprintf("enough: %d\n", max_enough); | |
3440 | info->container_enough = max_enough; | |
3441 | ||
3442 | if (super->disks) { | |
3443 | __u32 reserved = imsm_reserved_sectors(super, super->disks); | |
3444 | ||
3445 | disk = &super->disks->disk; | |
3446 | info->data_offset = total_blocks(&super->disks->disk) - reserved; | |
3447 | info->component_size = reserved; | |
3448 | info->disk.state = is_configured(disk) ? (1 << MD_DISK_ACTIVE) : 0; | |
3449 | /* we don't change info->disk.raid_disk here because | |
3450 | * this state will be finalized in mdmon after we have | |
3451 | * found the 'most fresh' version of the metadata | |
3452 | */ | |
3453 | info->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0; | |
3454 | info->disk.state |= is_spare(disk) ? 0 : (1 << MD_DISK_SYNC); | |
3455 | } | |
3456 | ||
3457 | /* only call uuid_from_super_imsm when this disk is part of a populated container, | |
3458 | * ->compare_super may have updated the 'num_raid_devs' field for spares | |
3459 | */ | |
3460 | if (info->disk.state & (1 << MD_DISK_SYNC) || super->anchor->num_raid_devs) | |
3461 | uuid_from_super_imsm(st, info->uuid); | |
3462 | else | |
3463 | memcpy(info->uuid, uuid_zero, sizeof(uuid_zero)); | |
3464 | ||
3465 | /* I don't know how to compute 'map' on imsm, so use safe default */ | |
3466 | if (map) { | |
3467 | int i; | |
3468 | for (i = 0; i < map_disks; i++) | |
3469 | map[i] = 1; | |
3470 | } | |
3471 | ||
3472 | } | |
3473 | ||
3474 | /* allocates memory and fills disk in mdinfo structure | |
3475 | * for each disk in array */ | |
3476 | struct mdinfo *getinfo_super_disks_imsm(struct supertype *st) | |
3477 | { | |
3478 | struct mdinfo *mddev; | |
3479 | struct intel_super *super = st->sb; | |
3480 | struct imsm_disk *disk; | |
3481 | int count = 0; | |
3482 | struct dl *dl; | |
3483 | if (!super || !super->disks) | |
3484 | return NULL; | |
3485 | dl = super->disks; | |
3486 | mddev = xcalloc(1, sizeof(*mddev)); | |
3487 | while (dl) { | |
3488 | struct mdinfo *tmp; | |
3489 | disk = &dl->disk; | |
3490 | tmp = xcalloc(1, sizeof(*tmp)); | |
3491 | if (mddev->devs) | |
3492 | tmp->next = mddev->devs; | |
3493 | mddev->devs = tmp; | |
3494 | tmp->disk.number = count++; | |
3495 | tmp->disk.major = dl->major; | |
3496 | tmp->disk.minor = dl->minor; | |
3497 | tmp->disk.state = is_configured(disk) ? | |
3498 | (1 << MD_DISK_ACTIVE) : 0; | |
3499 | tmp->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0; | |
3500 | tmp->disk.state |= is_spare(disk) ? 0 : (1 << MD_DISK_SYNC); | |
3501 | tmp->disk.raid_disk = -1; | |
3502 | dl = dl->next; | |
3503 | } | |
3504 | return mddev; | |
3505 | } | |
3506 | ||
3507 | static int update_super_imsm(struct supertype *st, struct mdinfo *info, | |
3508 | char *update, char *devname, int verbose, | |
3509 | int uuid_set, char *homehost) | |
3510 | { | |
3511 | /* For 'assemble' and 'force' we need to return non-zero if any | |
3512 | * change was made. For others, the return value is ignored. | |
3513 | * Update options are: | |
3514 | * force-one : This device looks a bit old but needs to be included, | |
3515 | * update age info appropriately. | |
3516 | * assemble: clear any 'faulty' flag to allow this device to | |
3517 | * be assembled. | |
3518 | * force-array: Array is degraded but being forced, mark it clean | |
3519 | * if that will be needed to assemble it. | |
3520 | * | |
3521 | * newdev: not used ???? | |
3522 | * grow: Array has gained a new device - this is currently for | |
3523 | * linear only | |
3524 | * resync: mark as dirty so a resync will happen. | |
3525 | * name: update the name - preserving the homehost | |
3526 | * uuid: Change the uuid of the array to match watch is given | |
3527 | * | |
3528 | * Following are not relevant for this imsm: | |
3529 | * sparc2.2 : update from old dodgey metadata | |
3530 | * super-minor: change the preferred_minor number | |
3531 | * summaries: update redundant counters. | |
3532 | * homehost: update the recorded homehost | |
3533 | * _reshape_progress: record new reshape_progress position. | |
3534 | */ | |
3535 | int rv = 1; | |
3536 | struct intel_super *super = st->sb; | |
3537 | struct imsm_super *mpb; | |
3538 | ||
3539 | /* we can only update container info */ | |
3540 | if (!super || super->current_vol >= 0 || !super->anchor) | |
3541 | return 1; | |
3542 | ||
3543 | mpb = super->anchor; | |
3544 | ||
3545 | if (strcmp(update, "uuid") == 0) { | |
3546 | /* We take this to mean that the family_num should be updated. | |
3547 | * However that is much smaller than the uuid so we cannot really | |
3548 | * allow an explicit uuid to be given. And it is hard to reliably | |
3549 | * know if one was. | |
3550 | * So if !uuid_set we know the current uuid is random and just used | |
3551 | * the first 'int' and copy it to the other 3 positions. | |
3552 | * Otherwise we require the 4 'int's to be the same as would be the | |
3553 | * case if we are using a random uuid. So an explicit uuid will be | |
3554 | * accepted as long as all for ints are the same... which shouldn't hurt | |
3555 | */ | |
3556 | if (!uuid_set) { | |
3557 | info->uuid[1] = info->uuid[2] = info->uuid[3] = info->uuid[0]; | |
3558 | rv = 0; | |
3559 | } else { | |
3560 | if (info->uuid[0] != info->uuid[1] || | |
3561 | info->uuid[1] != info->uuid[2] || | |
3562 | info->uuid[2] != info->uuid[3]) | |
3563 | rv = -1; | |
3564 | else | |
3565 | rv = 0; | |
3566 | } | |
3567 | if (rv == 0) | |
3568 | mpb->orig_family_num = info->uuid[0]; | |
3569 | } else if (strcmp(update, "assemble") == 0) | |
3570 | rv = 0; | |
3571 | else | |
3572 | rv = -1; | |
3573 | ||
3574 | /* successful update? recompute checksum */ | |
3575 | if (rv == 0) | |
3576 | mpb->check_sum = __le32_to_cpu(__gen_imsm_checksum(mpb)); | |
3577 | ||
3578 | return rv; | |
3579 | } | |
3580 | ||
3581 | static size_t disks_to_mpb_size(int disks) | |
3582 | { | |
3583 | size_t size; | |
3584 | ||
3585 | size = sizeof(struct imsm_super); | |
3586 | size += (disks - 1) * sizeof(struct imsm_disk); | |
3587 | size += 2 * sizeof(struct imsm_dev); | |
3588 | /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */ | |
3589 | size += (4 - 2) * sizeof(struct imsm_map); | |
3590 | /* 4 possible disk_ord_tbl's */ | |
3591 | size += 4 * (disks - 1) * sizeof(__u32); | |
3592 | /* maximum bbm log */ | |
3593 | size += sizeof(struct bbm_log); | |
3594 | ||
3595 | return size; | |
3596 | } | |
3597 | ||
3598 | static __u64 avail_size_imsm(struct supertype *st, __u64 devsize, | |
3599 | unsigned long long data_offset) | |
3600 | { | |
3601 | if (devsize < (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS)) | |
3602 | return 0; | |
3603 | ||
3604 | return devsize - (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS); | |
3605 | } | |
3606 | ||
3607 | static void free_devlist(struct intel_super *super) | |
3608 | { | |
3609 | struct intel_dev *dv; | |
3610 | ||
3611 | while (super->devlist) { | |
3612 | dv = super->devlist->next; | |
3613 | free(super->devlist->dev); | |
3614 | free(super->devlist); | |
3615 | super->devlist = dv; | |
3616 | } | |
3617 | } | |
3618 | ||
3619 | static void imsm_copy_dev(struct imsm_dev *dest, struct imsm_dev *src) | |
3620 | { | |
3621 | memcpy(dest, src, sizeof_imsm_dev(src, 0)); | |
3622 | } | |
3623 | ||
3624 | static int compare_super_imsm(struct supertype *st, struct supertype *tst) | |
3625 | { | |
3626 | /* | |
3627 | * return: | |
3628 | * 0 same, or first was empty, and second was copied | |
3629 | * 1 second had wrong number | |
3630 | * 2 wrong uuid | |
3631 | * 3 wrong other info | |
3632 | */ | |
3633 | struct intel_super *first = st->sb; | |
3634 | struct intel_super *sec = tst->sb; | |
3635 | ||
3636 | if (!first) { | |
3637 | st->sb = tst->sb; | |
3638 | tst->sb = NULL; | |
3639 | return 0; | |
3640 | } | |
3641 | /* in platform dependent environment test if the disks | |
3642 | * use the same Intel hba | |
3643 | * If not on Intel hba at all, allow anything. | |
3644 | */ | |
3645 | if (!check_env("IMSM_NO_PLATFORM") && first->hba && sec->hba) { | |
3646 | if (first->hba->type != sec->hba->type) { | |
3647 | fprintf(stderr, | |
3648 | "HBAs of devices do not match %s != %s\n", | |
3649 | get_sys_dev_type(first->hba->type), | |
3650 | get_sys_dev_type(sec->hba->type)); | |
3651 | return 3; | |
3652 | } | |
3653 | if (first->orom != sec->orom) { | |
3654 | fprintf(stderr, | |
3655 | "HBAs of devices do not match %s != %s\n", | |
3656 | first->hba->pci_id, sec->hba->pci_id); | |
3657 | return 3; | |
3658 | } | |
3659 | } | |
3660 | ||
3661 | /* if an anchor does not have num_raid_devs set then it is a free | |
3662 | * floating spare | |
3663 | */ | |
3664 | if (first->anchor->num_raid_devs > 0 && | |
3665 | sec->anchor->num_raid_devs > 0) { | |
3666 | /* Determine if these disks might ever have been | |
3667 | * related. Further disambiguation can only take place | |
3668 | * in load_super_imsm_all | |
3669 | */ | |
3670 | __u32 first_family = first->anchor->orig_family_num; | |
3671 | __u32 sec_family = sec->anchor->orig_family_num; | |
3672 | ||
3673 | if (memcmp(first->anchor->sig, sec->anchor->sig, | |
3674 | MAX_SIGNATURE_LENGTH) != 0) | |
3675 | return 3; | |
3676 | ||
3677 | if (first_family == 0) | |
3678 | first_family = first->anchor->family_num; | |
3679 | if (sec_family == 0) | |
3680 | sec_family = sec->anchor->family_num; | |
3681 | ||
3682 | if (first_family != sec_family) | |
3683 | return 3; | |
3684 | ||
3685 | } | |
3686 | ||
3687 | /* if 'first' is a spare promote it to a populated mpb with sec's | |
3688 | * family number | |
3689 | */ | |
3690 | if (first->anchor->num_raid_devs == 0 && | |
3691 | sec->anchor->num_raid_devs > 0) { | |
3692 | int i; | |
3693 | struct intel_dev *dv; | |
3694 | struct imsm_dev *dev; | |
3695 | ||
3696 | /* we need to copy raid device info from sec if an allocation | |
3697 | * fails here we don't associate the spare | |
3698 | */ | |
3699 | for (i = 0; i < sec->anchor->num_raid_devs; i++) { | |
3700 | dv = xmalloc(sizeof(*dv)); | |
3701 | dev = xmalloc(sizeof_imsm_dev(get_imsm_dev(sec, i), 1)); | |
3702 | dv->dev = dev; | |
3703 | dv->index = i; | |
3704 | dv->next = first->devlist; | |
3705 | first->devlist = dv; | |
3706 | } | |
3707 | if (i < sec->anchor->num_raid_devs) { | |
3708 | /* allocation failure */ | |
3709 | free_devlist(first); | |
3710 | pr_err("imsm: failed to associate spare\n"); | |
3711 | return 3; | |
3712 | } | |
3713 | first->anchor->num_raid_devs = sec->anchor->num_raid_devs; | |
3714 | first->anchor->orig_family_num = sec->anchor->orig_family_num; | |
3715 | first->anchor->family_num = sec->anchor->family_num; | |
3716 | memcpy(first->anchor->sig, sec->anchor->sig, MAX_SIGNATURE_LENGTH); | |
3717 | for (i = 0; i < sec->anchor->num_raid_devs; i++) | |
3718 | imsm_copy_dev(get_imsm_dev(first, i), get_imsm_dev(sec, i)); | |
3719 | } | |
3720 | ||
3721 | return 0; | |
3722 | } | |
3723 | ||
3724 | static void fd2devname(int fd, char *name) | |
3725 | { | |
3726 | struct stat st; | |
3727 | char path[256]; | |
3728 | char dname[PATH_MAX]; | |
3729 | char *nm; | |
3730 | int rv; | |
3731 | ||
3732 | name[0] = '\0'; | |
3733 | if (fstat(fd, &st) != 0) | |
3734 | return; | |
3735 | sprintf(path, "/sys/dev/block/%d:%d", | |
3736 | major(st.st_rdev), minor(st.st_rdev)); | |
3737 | ||
3738 | rv = readlink(path, dname, sizeof(dname)-1); | |
3739 | if (rv <= 0) | |
3740 | return; | |
3741 | ||
3742 | dname[rv] = '\0'; | |
3743 | nm = strrchr(dname, '/'); | |
3744 | if (nm) { | |
3745 | nm++; | |
3746 | snprintf(name, MAX_RAID_SERIAL_LEN, "/dev/%s", nm); | |
3747 | } | |
3748 | } | |
3749 | ||
3750 | static int nvme_get_serial(int fd, void *buf, size_t buf_len) | |
3751 | { | |
3752 | char path[60]; | |
3753 | char *name = fd2kname(fd); | |
3754 | ||
3755 | if (!name) | |
3756 | return 1; | |
3757 | ||
3758 | if (strncmp(name, "nvme", 4) != 0) | |
3759 | return 1; | |
3760 | ||
3761 | snprintf(path, sizeof(path) - 1, "/sys/block/%s/device/serial", name); | |
3762 | ||
3763 | return load_sys(path, buf, buf_len); | |
3764 | } | |
3765 | ||
3766 | extern int scsi_get_serial(int fd, void *buf, size_t buf_len); | |
3767 | ||
3768 | static int imsm_read_serial(int fd, char *devname, | |
3769 | __u8 serial[MAX_RAID_SERIAL_LEN]) | |
3770 | { | |
3771 | char buf[50]; | |
3772 | int rv; | |
3773 | int len; | |
3774 | char *dest; | |
3775 | char *src; | |
3776 | unsigned int i; | |
3777 | ||
3778 | memset(buf, 0, sizeof(buf)); | |
3779 | ||
3780 | rv = nvme_get_serial(fd, buf, sizeof(buf)); | |
3781 | ||
3782 | if (rv) | |
3783 | rv = scsi_get_serial(fd, buf, sizeof(buf)); | |
3784 | ||
3785 | if (rv && check_env("IMSM_DEVNAME_AS_SERIAL")) { | |
3786 | memset(serial, 0, MAX_RAID_SERIAL_LEN); | |
3787 | fd2devname(fd, (char *) serial); | |
3788 | return 0; | |
3789 | } | |
3790 | ||
3791 | if (rv != 0) { | |
3792 | if (devname) | |
3793 | pr_err("Failed to retrieve serial for %s\n", | |
3794 | devname); | |
3795 | return rv; | |
3796 | } | |
3797 | ||
3798 | /* trim all whitespace and non-printable characters and convert | |
3799 | * ':' to ';' | |
3800 | */ | |
3801 | for (i = 0, dest = buf; i < sizeof(buf) && buf[i]; i++) { | |
3802 | src = &buf[i]; | |
3803 | if (*src > 0x20) { | |
3804 | /* ':' is reserved for use in placeholder serial | |
3805 | * numbers for missing disks | |
3806 | */ | |
3807 | if (*src == ':') | |
3808 | *dest++ = ';'; | |
3809 | else | |
3810 | *dest++ = *src; | |
3811 | } | |
3812 | } | |
3813 | len = dest - buf; | |
3814 | dest = buf; | |
3815 | ||
3816 | /* truncate leading characters */ | |
3817 | if (len > MAX_RAID_SERIAL_LEN) { | |
3818 | dest += len - MAX_RAID_SERIAL_LEN; | |
3819 | len = MAX_RAID_SERIAL_LEN; | |
3820 | } | |
3821 | ||
3822 | memset(serial, 0, MAX_RAID_SERIAL_LEN); | |
3823 | memcpy(serial, dest, len); | |
3824 | ||
3825 | return 0; | |
3826 | } | |
3827 | ||
3828 | static int serialcmp(__u8 *s1, __u8 *s2) | |
3829 | { | |
3830 | return strncmp((char *) s1, (char *) s2, MAX_RAID_SERIAL_LEN); | |
3831 | } | |
3832 | ||
3833 | static void serialcpy(__u8 *dest, __u8 *src) | |
3834 | { | |
3835 | strncpy((char *) dest, (char *) src, MAX_RAID_SERIAL_LEN); | |
3836 | } | |
3837 | ||
3838 | static struct dl *serial_to_dl(__u8 *serial, struct intel_super *super) | |
3839 | { | |
3840 | struct dl *dl; | |
3841 | ||
3842 | for (dl = super->disks; dl; dl = dl->next) | |
3843 | if (serialcmp(dl->serial, serial) == 0) | |
3844 | break; | |
3845 | ||
3846 | return dl; | |
3847 | } | |
3848 | ||
3849 | static struct imsm_disk * | |
3850 | __serial_to_disk(__u8 *serial, struct imsm_super *mpb, int *idx) | |
3851 | { | |
3852 | int i; | |
3853 | ||
3854 | for (i = 0; i < mpb->num_disks; i++) { | |
3855 | struct imsm_disk *disk = __get_imsm_disk(mpb, i); | |
3856 | ||
3857 | if (serialcmp(disk->serial, serial) == 0) { | |
3858 | if (idx) | |
3859 | *idx = i; | |
3860 | return disk; | |
3861 | } | |
3862 | } | |
3863 | ||
3864 | return NULL; | |
3865 | } | |
3866 | ||
3867 | static int | |
3868 | load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd) | |
3869 | { | |
3870 | struct imsm_disk *disk; | |
3871 | struct dl *dl; | |
3872 | struct stat stb; | |
3873 | int rv; | |
3874 | char name[40]; | |
3875 | __u8 serial[MAX_RAID_SERIAL_LEN]; | |
3876 | ||
3877 | rv = imsm_read_serial(fd, devname, serial); | |
3878 | ||
3879 | if (rv != 0) | |
3880 | return 2; | |
3881 | ||
3882 | dl = xcalloc(1, sizeof(*dl)); | |
3883 | ||
3884 | fstat(fd, &stb); | |
3885 | dl->major = major(stb.st_rdev); | |
3886 | dl->minor = minor(stb.st_rdev); | |
3887 | dl->next = super->disks; | |
3888 | dl->fd = keep_fd ? fd : -1; | |
3889 | assert(super->disks == NULL); | |
3890 | super->disks = dl; | |
3891 | serialcpy(dl->serial, serial); | |
3892 | dl->index = -2; | |
3893 | dl->e = NULL; | |
3894 | fd2devname(fd, name); | |
3895 | if (devname) | |
3896 | dl->devname = xstrdup(devname); | |
3897 | else | |
3898 | dl->devname = xstrdup(name); | |
3899 | ||
3900 | /* look up this disk's index in the current anchor */ | |
3901 | disk = __serial_to_disk(dl->serial, super->anchor, &dl->index); | |
3902 | if (disk) { | |
3903 | dl->disk = *disk; | |
3904 | /* only set index on disks that are a member of a | |
3905 | * populated contianer, i.e. one with raid_devs | |
3906 | */ | |
3907 | if (is_failed(&dl->disk)) | |
3908 | dl->index = -2; | |
3909 | else if (is_spare(&dl->disk)) | |
3910 | dl->index = -1; | |
3911 | } | |
3912 | ||
3913 | return 0; | |
3914 | } | |
3915 | ||
3916 | #ifndef MDASSEMBLE | |
3917 | /* When migrating map0 contains the 'destination' state while map1 | |
3918 | * contains the current state. When not migrating map0 contains the | |
3919 | * current state. This routine assumes that map[0].map_state is set to | |
3920 | * the current array state before being called. | |
3921 | * | |
3922 | * Migration is indicated by one of the following states | |
3923 | * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed) | |
3924 | * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal | |
3925 | * map1state=unitialized) | |
3926 | * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal | |
3927 | * map1state=normal) | |
3928 | * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal | |
3929 | * map1state=degraded) | |
3930 | * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal | |
3931 | * map1state=normal) | |
3932 | */ | |
3933 | static void migrate(struct imsm_dev *dev, struct intel_super *super, | |
3934 | __u8 to_state, int migr_type) | |
3935 | { | |
3936 | struct imsm_map *dest; | |
3937 | struct imsm_map *src = get_imsm_map(dev, MAP_0); | |
3938 | ||
3939 | dev->vol.migr_state = 1; | |
3940 | set_migr_type(dev, migr_type); | |
3941 | dev->vol.curr_migr_unit = 0; | |
3942 | dest = get_imsm_map(dev, MAP_1); | |
3943 | ||
3944 | /* duplicate and then set the target end state in map[0] */ | |
3945 | memcpy(dest, src, sizeof_imsm_map(src)); | |
3946 | if (migr_type == MIGR_REBUILD || migr_type == MIGR_GEN_MIGR) { | |
3947 | __u32 ord; | |
3948 | int i; | |
3949 | ||
3950 | for (i = 0; i < src->num_members; i++) { | |
3951 | ord = __le32_to_cpu(src->disk_ord_tbl[i]); | |
3952 | set_imsm_ord_tbl_ent(src, i, ord_to_idx(ord)); | |
3953 | } | |
3954 | } | |
3955 | ||
3956 | if (migr_type == MIGR_GEN_MIGR) | |
3957 | /* Clear migration record */ | |
3958 | memset(super->migr_rec, 0, sizeof(struct migr_record)); | |
3959 | ||
3960 | src->map_state = to_state; | |
3961 | } | |
3962 | ||
3963 | static void end_migration(struct imsm_dev *dev, struct intel_super *super, | |
3964 | __u8 map_state) | |
3965 | { | |
3966 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
3967 | struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state == 0 ? | |
3968 | MAP_0 : MAP_1); | |
3969 | int i, j; | |
3970 | ||
3971 | /* merge any IMSM_ORD_REBUILD bits that were not successfully | |
3972 | * completed in the last migration. | |
3973 | * | |
3974 | * FIXME add support for raid-level-migration | |
3975 | */ | |
3976 | if (map_state != map->map_state && (is_gen_migration(dev) == 0) && | |
3977 | prev->map_state != IMSM_T_STATE_UNINITIALIZED) { | |
3978 | /* when final map state is other than expected | |
3979 | * merge maps (not for migration) | |
3980 | */ | |
3981 | int failed; | |
3982 | ||
3983 | for (i = 0; i < prev->num_members; i++) | |
3984 | for (j = 0; j < map->num_members; j++) | |
3985 | /* during online capacity expansion | |
3986 | * disks position can be changed | |
3987 | * if takeover is used | |
3988 | */ | |
3989 | if (ord_to_idx(map->disk_ord_tbl[j]) == | |
3990 | ord_to_idx(prev->disk_ord_tbl[i])) { | |
3991 | map->disk_ord_tbl[j] |= | |
3992 | prev->disk_ord_tbl[i]; | |
3993 | break; | |
3994 | } | |
3995 | failed = imsm_count_failed(super, dev, MAP_0); | |
3996 | map_state = imsm_check_degraded(super, dev, failed, MAP_0); | |
3997 | } | |
3998 | ||
3999 | dev->vol.migr_state = 0; | |
4000 | set_migr_type(dev, 0); | |
4001 | dev->vol.curr_migr_unit = 0; | |
4002 | map->map_state = map_state; | |
4003 | } | |
4004 | #endif | |
4005 | ||
4006 | static int parse_raid_devices(struct intel_super *super) | |
4007 | { | |
4008 | int i; | |
4009 | struct imsm_dev *dev_new; | |
4010 | size_t len, len_migr; | |
4011 | size_t max_len = 0; | |
4012 | size_t space_needed = 0; | |
4013 | struct imsm_super *mpb = super->anchor; | |
4014 | ||
4015 | for (i = 0; i < super->anchor->num_raid_devs; i++) { | |
4016 | struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i); | |
4017 | struct intel_dev *dv; | |
4018 | ||
4019 | len = sizeof_imsm_dev(dev_iter, 0); | |
4020 | len_migr = sizeof_imsm_dev(dev_iter, 1); | |
4021 | if (len_migr > len) | |
4022 | space_needed += len_migr - len; | |
4023 | ||
4024 | dv = xmalloc(sizeof(*dv)); | |
4025 | if (max_len < len_migr) | |
4026 | max_len = len_migr; | |
4027 | if (max_len > len_migr) | |
4028 | space_needed += max_len - len_migr; | |
4029 | dev_new = xmalloc(max_len); | |
4030 | imsm_copy_dev(dev_new, dev_iter); | |
4031 | dv->dev = dev_new; | |
4032 | dv->index = i; | |
4033 | dv->next = super->devlist; | |
4034 | super->devlist = dv; | |
4035 | } | |
4036 | ||
4037 | /* ensure that super->buf is large enough when all raid devices | |
4038 | * are migrating | |
4039 | */ | |
4040 | if (__le32_to_cpu(mpb->mpb_size) + space_needed > super->len) { | |
4041 | void *buf; | |
4042 | ||
4043 | len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + space_needed, | |
4044 | super->sector_size); | |
4045 | if (posix_memalign(&buf, MAX_SECTOR_SIZE, len) != 0) | |
4046 | return 1; | |
4047 | ||
4048 | memcpy(buf, super->buf, super->len); | |
4049 | memset(buf + super->len, 0, len - super->len); | |
4050 | free(super->buf); | |
4051 | super->buf = buf; | |
4052 | super->len = len; | |
4053 | } | |
4054 | ||
4055 | super->extra_space += space_needed; | |
4056 | ||
4057 | return 0; | |
4058 | } | |
4059 | ||
4060 | /******************************************************************************* | |
4061 | * Function: check_mpb_migr_compatibility | |
4062 | * Description: Function checks for unsupported migration features: | |
4063 | * - migration optimization area (pba_of_lba0) | |
4064 | * - descending reshape (ascending_migr) | |
4065 | * Parameters: | |
4066 | * super : imsm metadata information | |
4067 | * Returns: | |
4068 | * 0 : migration is compatible | |
4069 | * -1 : migration is not compatible | |
4070 | ******************************************************************************/ | |
4071 | int check_mpb_migr_compatibility(struct intel_super *super) | |
4072 | { | |
4073 | struct imsm_map *map0, *map1; | |
4074 | struct migr_record *migr_rec = super->migr_rec; | |
4075 | int i; | |
4076 | ||
4077 | for (i = 0; i < super->anchor->num_raid_devs; i++) { | |
4078 | struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i); | |
4079 | ||
4080 | if (dev_iter && | |
4081 | dev_iter->vol.migr_state == 1 && | |
4082 | dev_iter->vol.migr_type == MIGR_GEN_MIGR) { | |
4083 | /* This device is migrating */ | |
4084 | map0 = get_imsm_map(dev_iter, MAP_0); | |
4085 | map1 = get_imsm_map(dev_iter, MAP_1); | |
4086 | if (pba_of_lba0(map0) != pba_of_lba0(map1)) | |
4087 | /* migration optimization area was used */ | |
4088 | return -1; | |
4089 | if (migr_rec->ascending_migr == 0 | |
4090 | && migr_rec->dest_depth_per_unit > 0) | |
4091 | /* descending reshape not supported yet */ | |
4092 | return -1; | |
4093 | } | |
4094 | } | |
4095 | return 0; | |
4096 | } | |
4097 | ||
4098 | static void __free_imsm(struct intel_super *super, int free_disks); | |
4099 | ||
4100 | /* load_imsm_mpb - read matrix metadata | |
4101 | * allocates super->mpb to be freed by free_imsm | |
4102 | */ | |
4103 | static int load_imsm_mpb(int fd, struct intel_super *super, char *devname) | |
4104 | { | |
4105 | unsigned long long dsize; | |
4106 | unsigned long long sectors; | |
4107 | unsigned int sector_size = super->sector_size; | |
4108 | struct stat; | |
4109 | struct imsm_super *anchor; | |
4110 | __u32 check_sum; | |
4111 | ||
4112 | get_dev_size(fd, NULL, &dsize); | |
4113 | if (dsize < 2*sector_size) { | |
4114 | if (devname) | |
4115 | pr_err("%s: device to small for imsm\n", | |
4116 | devname); | |
4117 | return 1; | |
4118 | } | |
4119 | ||
4120 | if (lseek64(fd, dsize - (sector_size * 2), SEEK_SET) < 0) { | |
4121 | if (devname) | |
4122 | pr_err("Cannot seek to anchor block on %s: %s\n", | |
4123 | devname, strerror(errno)); | |
4124 | return 1; | |
4125 | } | |
4126 | ||
4127 | if (posix_memalign((void **)&anchor, sector_size, sector_size) != 0) { | |
4128 | if (devname) | |
4129 | pr_err("Failed to allocate imsm anchor buffer on %s\n", devname); | |
4130 | return 1; | |
4131 | } | |
4132 | if ((unsigned int)read(fd, anchor, sector_size) != sector_size) { | |
4133 | if (devname) | |
4134 | pr_err("Cannot read anchor block on %s: %s\n", | |
4135 | devname, strerror(errno)); | |
4136 | free(anchor); | |
4137 | return 1; | |
4138 | } | |
4139 | ||
4140 | if (strncmp((char *) anchor->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) { | |
4141 | if (devname) | |
4142 | pr_err("no IMSM anchor on %s\n", devname); | |
4143 | free(anchor); | |
4144 | return 2; | |
4145 | } | |
4146 | ||
4147 | __free_imsm(super, 0); | |
4148 | /* reload capability and hba */ | |
4149 | ||
4150 | /* capability and hba must be updated with new super allocation */ | |
4151 | find_intel_hba_capability(fd, super, devname); | |
4152 | super->len = ROUND_UP(anchor->mpb_size, sector_size); | |
4153 | if (posix_memalign(&super->buf, MAX_SECTOR_SIZE, super->len) != 0) { | |
4154 | if (devname) | |
4155 | pr_err("unable to allocate %zu byte mpb buffer\n", | |
4156 | super->len); | |
4157 | free(anchor); | |
4158 | return 2; | |
4159 | } | |
4160 | memcpy(super->buf, anchor, sector_size); | |
4161 | ||
4162 | sectors = mpb_sectors(anchor, sector_size) - 1; | |
4163 | free(anchor); | |
4164 | ||
4165 | if (posix_memalign(&super->migr_rec_buf, sector_size, | |
4166 | MIGR_REC_BUF_SECTORS*sector_size) != 0) { | |
4167 | pr_err("could not allocate migr_rec buffer\n"); | |
4168 | free(super->buf); | |
4169 | return 2; | |
4170 | } | |
4171 | super->clean_migration_record_by_mdmon = 0; | |
4172 | ||
4173 | if (!sectors) { | |
4174 | check_sum = __gen_imsm_checksum(super->anchor); | |
4175 | if (check_sum != __le32_to_cpu(super->anchor->check_sum)) { | |
4176 | if (devname) | |
4177 | pr_err("IMSM checksum %x != %x on %s\n", | |
4178 | check_sum, | |
4179 | __le32_to_cpu(super->anchor->check_sum), | |
4180 | devname); | |
4181 | return 2; | |
4182 | } | |
4183 | ||
4184 | return 0; | |
4185 | } | |
4186 | ||
4187 | /* read the extended mpb */ | |
4188 | if (lseek64(fd, dsize - (sector_size * (2 + sectors)), SEEK_SET) < 0) { | |
4189 | if (devname) | |
4190 | pr_err("Cannot seek to extended mpb on %s: %s\n", | |
4191 | devname, strerror(errno)); | |
4192 | return 1; | |
4193 | } | |
4194 | ||
4195 | if ((unsigned int)read(fd, super->buf + sector_size, | |
4196 | super->len - sector_size) != super->len - sector_size) { | |
4197 | if (devname) | |
4198 | pr_err("Cannot read extended mpb on %s: %s\n", | |
4199 | devname, strerror(errno)); | |
4200 | return 2; | |
4201 | } | |
4202 | ||
4203 | check_sum = __gen_imsm_checksum(super->anchor); | |
4204 | if (check_sum != __le32_to_cpu(super->anchor->check_sum)) { | |
4205 | if (devname) | |
4206 | pr_err("IMSM checksum %x != %x on %s\n", | |
4207 | check_sum, __le32_to_cpu(super->anchor->check_sum), | |
4208 | devname); | |
4209 | return 3; | |
4210 | } | |
4211 | ||
4212 | return 0; | |
4213 | } | |
4214 | ||
4215 | static int read_imsm_migr_rec(int fd, struct intel_super *super); | |
4216 | ||
4217 | /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */ | |
4218 | static void clear_hi(struct intel_super *super) | |
4219 | { | |
4220 | struct imsm_super *mpb = super->anchor; | |
4221 | int i, n; | |
4222 | if (mpb->attributes & MPB_ATTRIB_2TB_DISK) | |
4223 | return; | |
4224 | for (i = 0; i < mpb->num_disks; ++i) { | |
4225 | struct imsm_disk *disk = &mpb->disk[i]; | |
4226 | disk->total_blocks_hi = 0; | |
4227 | } | |
4228 | for (i = 0; i < mpb->num_raid_devs; ++i) { | |
4229 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
4230 | if (!dev) | |
4231 | return; | |
4232 | for (n = 0; n < 2; ++n) { | |
4233 | struct imsm_map *map = get_imsm_map(dev, n); | |
4234 | if (!map) | |
4235 | continue; | |
4236 | map->pba_of_lba0_hi = 0; | |
4237 | map->blocks_per_member_hi = 0; | |
4238 | map->num_data_stripes_hi = 0; | |
4239 | } | |
4240 | } | |
4241 | } | |
4242 | ||
4243 | static int | |
4244 | load_and_parse_mpb(int fd, struct intel_super *super, char *devname, int keep_fd) | |
4245 | { | |
4246 | int err; | |
4247 | ||
4248 | err = load_imsm_mpb(fd, super, devname); | |
4249 | if (err) | |
4250 | return err; | |
4251 | if (super->sector_size == 4096) | |
4252 | convert_from_4k(super); | |
4253 | err = load_imsm_disk(fd, super, devname, keep_fd); | |
4254 | if (err) | |
4255 | return err; | |
4256 | err = parse_raid_devices(super); | |
4257 | if (err) | |
4258 | return err; | |
4259 | err = load_bbm_log(super); | |
4260 | clear_hi(super); | |
4261 | return err; | |
4262 | } | |
4263 | ||
4264 | static void __free_imsm_disk(struct dl *d) | |
4265 | { | |
4266 | if (d->fd >= 0) | |
4267 | close(d->fd); | |
4268 | if (d->devname) | |
4269 | free(d->devname); | |
4270 | if (d->e) | |
4271 | free(d->e); | |
4272 | free(d); | |
4273 | ||
4274 | } | |
4275 | ||
4276 | static void free_imsm_disks(struct intel_super *super) | |
4277 | { | |
4278 | struct dl *d; | |
4279 | ||
4280 | while (super->disks) { | |
4281 | d = super->disks; | |
4282 | super->disks = d->next; | |
4283 | __free_imsm_disk(d); | |
4284 | } | |
4285 | while (super->disk_mgmt_list) { | |
4286 | d = super->disk_mgmt_list; | |
4287 | super->disk_mgmt_list = d->next; | |
4288 | __free_imsm_disk(d); | |
4289 | } | |
4290 | while (super->missing) { | |
4291 | d = super->missing; | |
4292 | super->missing = d->next; | |
4293 | __free_imsm_disk(d); | |
4294 | } | |
4295 | ||
4296 | } | |
4297 | ||
4298 | /* free all the pieces hanging off of a super pointer */ | |
4299 | static void __free_imsm(struct intel_super *super, int free_disks) | |
4300 | { | |
4301 | struct intel_hba *elem, *next; | |
4302 | ||
4303 | if (super->buf) { | |
4304 | free(super->buf); | |
4305 | super->buf = NULL; | |
4306 | } | |
4307 | /* unlink capability description */ | |
4308 | super->orom = NULL; | |
4309 | if (super->migr_rec_buf) { | |
4310 | free(super->migr_rec_buf); | |
4311 | super->migr_rec_buf = NULL; | |
4312 | } | |
4313 | if (free_disks) | |
4314 | free_imsm_disks(super); | |
4315 | free_devlist(super); | |
4316 | elem = super->hba; | |
4317 | while (elem) { | |
4318 | if (elem->path) | |
4319 | free((void *)elem->path); | |
4320 | next = elem->next; | |
4321 | free(elem); | |
4322 | elem = next; | |
4323 | } | |
4324 | if (super->bbm_log) | |
4325 | free(super->bbm_log); | |
4326 | super->hba = NULL; | |
4327 | } | |
4328 | ||
4329 | static void free_imsm(struct intel_super *super) | |
4330 | { | |
4331 | __free_imsm(super, 1); | |
4332 | free(super->bb.entries); | |
4333 | free(super); | |
4334 | } | |
4335 | ||
4336 | static void free_super_imsm(struct supertype *st) | |
4337 | { | |
4338 | struct intel_super *super = st->sb; | |
4339 | ||
4340 | if (!super) | |
4341 | return; | |
4342 | ||
4343 | free_imsm(super); | |
4344 | st->sb = NULL; | |
4345 | } | |
4346 | ||
4347 | static struct intel_super *alloc_super(void) | |
4348 | { | |
4349 | struct intel_super *super = xcalloc(1, sizeof(*super)); | |
4350 | ||
4351 | super->current_vol = -1; | |
4352 | super->create_offset = ~((unsigned long long) 0); | |
4353 | ||
4354 | super->bb.entries = xmalloc(BBM_LOG_MAX_ENTRIES * | |
4355 | sizeof(struct md_bb_entry)); | |
4356 | if (!super->bb.entries) { | |
4357 | free(super); | |
4358 | return NULL; | |
4359 | } | |
4360 | ||
4361 | return super; | |
4362 | } | |
4363 | ||
4364 | /* | |
4365 | * find and allocate hba and OROM/EFI based on valid fd of RAID component device | |
4366 | */ | |
4367 | static int find_intel_hba_capability(int fd, struct intel_super *super, char *devname) | |
4368 | { | |
4369 | struct sys_dev *hba_name; | |
4370 | int rv = 0; | |
4371 | ||
4372 | if (fd < 0 || check_env("IMSM_NO_PLATFORM")) { | |
4373 | super->orom = NULL; | |
4374 | super->hba = NULL; | |
4375 | return 0; | |
4376 | } | |
4377 | hba_name = find_disk_attached_hba(fd, NULL); | |
4378 | if (!hba_name) { | |
4379 | if (devname) | |
4380 | pr_err("%s is not attached to Intel(R) RAID controller.\n", | |
4381 | devname); | |
4382 | return 1; | |
4383 | } | |
4384 | rv = attach_hba_to_super(super, hba_name); | |
4385 | if (rv == 2) { | |
4386 | if (devname) { | |
4387 | struct intel_hba *hba = super->hba; | |
4388 | ||
4389 | pr_err("%s is attached to Intel(R) %s %s (%s),\n" | |
4390 | " but the container is assigned to Intel(R) %s %s (", | |
4391 | devname, | |
4392 | get_sys_dev_type(hba_name->type), | |
4393 | hba_name->type == SYS_DEV_VMD ? "domain" : "RAID controller", | |
4394 | hba_name->pci_id ? : "Err!", | |
4395 | get_sys_dev_type(super->hba->type), | |
4396 | hba->type == SYS_DEV_VMD ? "domain" : "RAID controller"); | |
4397 | ||
4398 | while (hba) { | |
4399 | fprintf(stderr, "%s", hba->pci_id ? : "Err!"); | |
4400 | if (hba->next) | |
4401 | fprintf(stderr, ", "); | |
4402 | hba = hba->next; | |
4403 | } | |
4404 | fprintf(stderr, ").\n" | |
4405 | " Mixing devices attached to different %s is not allowed.\n", | |
4406 | hba_name->type == SYS_DEV_VMD ? "VMD domains" : "controllers"); | |
4407 | } | |
4408 | return 2; | |
4409 | } | |
4410 | super->orom = find_imsm_capability(hba_name); | |
4411 | if (!super->orom) | |
4412 | return 3; | |
4413 | ||
4414 | return 0; | |
4415 | } | |
4416 | ||
4417 | /* find_missing - helper routine for load_super_imsm_all that identifies | |
4418 | * disks that have disappeared from the system. This routine relies on | |
4419 | * the mpb being uptodate, which it is at load time. | |
4420 | */ | |
4421 | static int find_missing(struct intel_super *super) | |
4422 | { | |
4423 | int i; | |
4424 | struct imsm_super *mpb = super->anchor; | |
4425 | struct dl *dl; | |
4426 | struct imsm_disk *disk; | |
4427 | ||
4428 | for (i = 0; i < mpb->num_disks; i++) { | |
4429 | disk = __get_imsm_disk(mpb, i); | |
4430 | dl = serial_to_dl(disk->serial, super); | |
4431 | if (dl) | |
4432 | continue; | |
4433 | ||
4434 | dl = xmalloc(sizeof(*dl)); | |
4435 | dl->major = 0; | |
4436 | dl->minor = 0; | |
4437 | dl->fd = -1; | |
4438 | dl->devname = xstrdup("missing"); | |
4439 | dl->index = i; | |
4440 | serialcpy(dl->serial, disk->serial); | |
4441 | dl->disk = *disk; | |
4442 | dl->e = NULL; | |
4443 | dl->next = super->missing; | |
4444 | super->missing = dl; | |
4445 | } | |
4446 | ||
4447 | return 0; | |
4448 | } | |
4449 | ||
4450 | #ifndef MDASSEMBLE | |
4451 | static struct intel_disk *disk_list_get(__u8 *serial, struct intel_disk *disk_list) | |
4452 | { | |
4453 | struct intel_disk *idisk = disk_list; | |
4454 | ||
4455 | while (idisk) { | |
4456 | if (serialcmp(idisk->disk.serial, serial) == 0) | |
4457 | break; | |
4458 | idisk = idisk->next; | |
4459 | } | |
4460 | ||
4461 | return idisk; | |
4462 | } | |
4463 | ||
4464 | static int __prep_thunderdome(struct intel_super **table, int tbl_size, | |
4465 | struct intel_super *super, | |
4466 | struct intel_disk **disk_list) | |
4467 | { | |
4468 | struct imsm_disk *d = &super->disks->disk; | |
4469 | struct imsm_super *mpb = super->anchor; | |
4470 | int i, j; | |
4471 | ||
4472 | for (i = 0; i < tbl_size; i++) { | |
4473 | struct imsm_super *tbl_mpb = table[i]->anchor; | |
4474 | struct imsm_disk *tbl_d = &table[i]->disks->disk; | |
4475 | ||
4476 | if (tbl_mpb->family_num == mpb->family_num) { | |
4477 | if (tbl_mpb->check_sum == mpb->check_sum) { | |
4478 | dprintf("mpb from %d:%d matches %d:%d\n", | |
4479 | super->disks->major, | |
4480 | super->disks->minor, | |
4481 | table[i]->disks->major, | |
4482 | table[i]->disks->minor); | |
4483 | break; | |
4484 | } | |
4485 | ||
4486 | if (((is_configured(d) && !is_configured(tbl_d)) || | |
4487 | is_configured(d) == is_configured(tbl_d)) && | |
4488 | tbl_mpb->generation_num < mpb->generation_num) { | |
4489 | /* current version of the mpb is a | |
4490 | * better candidate than the one in | |
4491 | * super_table, but copy over "cross | |
4492 | * generational" status | |
4493 | */ | |
4494 | struct intel_disk *idisk; | |
4495 | ||
4496 | dprintf("mpb from %d:%d replaces %d:%d\n", | |
4497 | super->disks->major, | |
4498 | super->disks->minor, | |
4499 | table[i]->disks->major, | |
4500 | table[i]->disks->minor); | |
4501 | ||
4502 | idisk = disk_list_get(tbl_d->serial, *disk_list); | |
4503 | if (idisk && is_failed(&idisk->disk)) | |
4504 | tbl_d->status |= FAILED_DISK; | |
4505 | break; | |
4506 | } else { | |
4507 | struct intel_disk *idisk; | |
4508 | struct imsm_disk *disk; | |
4509 | ||
4510 | /* tbl_mpb is more up to date, but copy | |
4511 | * over cross generational status before | |
4512 | * returning | |
4513 | */ | |
4514 | disk = __serial_to_disk(d->serial, mpb, NULL); | |
4515 | if (disk && is_failed(disk)) | |
4516 | d->status |= FAILED_DISK; | |
4517 | ||
4518 | idisk = disk_list_get(d->serial, *disk_list); | |
4519 | if (idisk) { | |
4520 | idisk->owner = i; | |
4521 | if (disk && is_configured(disk)) | |
4522 | idisk->disk.status |= CONFIGURED_DISK; | |
4523 | } | |
4524 | ||
4525 | dprintf("mpb from %d:%d prefer %d:%d\n", | |
4526 | super->disks->major, | |
4527 | super->disks->minor, | |
4528 | table[i]->disks->major, | |
4529 | table[i]->disks->minor); | |
4530 | ||
4531 | return tbl_size; | |
4532 | } | |
4533 | } | |
4534 | } | |
4535 | ||
4536 | if (i >= tbl_size) | |
4537 | table[tbl_size++] = super; | |
4538 | else | |
4539 | table[i] = super; | |
4540 | ||
4541 | /* update/extend the merged list of imsm_disk records */ | |
4542 | for (j = 0; j < mpb->num_disks; j++) { | |
4543 | struct imsm_disk *disk = __get_imsm_disk(mpb, j); | |
4544 | struct intel_disk *idisk; | |
4545 | ||
4546 | idisk = disk_list_get(disk->serial, *disk_list); | |
4547 | if (idisk) { | |
4548 | idisk->disk.status |= disk->status; | |
4549 | if (is_configured(&idisk->disk) || | |
4550 | is_failed(&idisk->disk)) | |
4551 | idisk->disk.status &= ~(SPARE_DISK); | |
4552 | } else { | |
4553 | idisk = xcalloc(1, sizeof(*idisk)); | |
4554 | idisk->owner = IMSM_UNKNOWN_OWNER; | |
4555 | idisk->disk = *disk; | |
4556 | idisk->next = *disk_list; | |
4557 | *disk_list = idisk; | |
4558 | } | |
4559 | ||
4560 | if (serialcmp(idisk->disk.serial, d->serial) == 0) | |
4561 | idisk->owner = i; | |
4562 | } | |
4563 | ||
4564 | return tbl_size; | |
4565 | } | |
4566 | ||
4567 | static struct intel_super * | |
4568 | validate_members(struct intel_super *super, struct intel_disk *disk_list, | |
4569 | const int owner) | |
4570 | { | |
4571 | struct imsm_super *mpb = super->anchor; | |
4572 | int ok_count = 0; | |
4573 | int i; | |
4574 | ||
4575 | for (i = 0; i < mpb->num_disks; i++) { | |
4576 | struct imsm_disk *disk = __get_imsm_disk(mpb, i); | |
4577 | struct intel_disk *idisk; | |
4578 | ||
4579 | idisk = disk_list_get(disk->serial, disk_list); | |
4580 | if (idisk) { | |
4581 | if (idisk->owner == owner || | |
4582 | idisk->owner == IMSM_UNKNOWN_OWNER) | |
4583 | ok_count++; | |
4584 | else | |
4585 | dprintf("'%.16s' owner %d != %d\n", | |
4586 | disk->serial, idisk->owner, | |
4587 | owner); | |
4588 | } else { | |
4589 | dprintf("unknown disk %x [%d]: %.16s\n", | |
4590 | __le32_to_cpu(mpb->family_num), i, | |
4591 | disk->serial); | |
4592 | break; | |
4593 | } | |
4594 | } | |
4595 | ||
4596 | if (ok_count == mpb->num_disks) | |
4597 | return super; | |
4598 | return NULL; | |
4599 | } | |
4600 | ||
4601 | static void show_conflicts(__u32 family_num, struct intel_super *super_list) | |
4602 | { | |
4603 | struct intel_super *s; | |
4604 | ||
4605 | for (s = super_list; s; s = s->next) { | |
4606 | if (family_num != s->anchor->family_num) | |
4607 | continue; | |
4608 | pr_err("Conflict, offlining family %#x on '%s'\n", | |
4609 | __le32_to_cpu(family_num), s->disks->devname); | |
4610 | } | |
4611 | } | |
4612 | ||
4613 | static struct intel_super * | |
4614 | imsm_thunderdome(struct intel_super **super_list, int len) | |
4615 | { | |
4616 | struct intel_super *super_table[len]; | |
4617 | struct intel_disk *disk_list = NULL; | |
4618 | struct intel_super *champion, *spare; | |
4619 | struct intel_super *s, **del; | |
4620 | int tbl_size = 0; | |
4621 | int conflict; | |
4622 | int i; | |
4623 | ||
4624 | memset(super_table, 0, sizeof(super_table)); | |
4625 | for (s = *super_list; s; s = s->next) | |
4626 | tbl_size = __prep_thunderdome(super_table, tbl_size, s, &disk_list); | |
4627 | ||
4628 | for (i = 0; i < tbl_size; i++) { | |
4629 | struct imsm_disk *d; | |
4630 | struct intel_disk *idisk; | |
4631 | struct imsm_super *mpb = super_table[i]->anchor; | |
4632 | ||
4633 | s = super_table[i]; | |
4634 | d = &s->disks->disk; | |
4635 | ||
4636 | /* 'd' must appear in merged disk list for its | |
4637 | * configuration to be valid | |
4638 | */ | |
4639 | idisk = disk_list_get(d->serial, disk_list); | |
4640 | if (idisk && idisk->owner == i) | |
4641 | s = validate_members(s, disk_list, i); | |
4642 | else | |
4643 | s = NULL; | |
4644 | ||
4645 | if (!s) | |
4646 | dprintf("marking family: %#x from %d:%d offline\n", | |
4647 | mpb->family_num, | |
4648 | super_table[i]->disks->major, | |
4649 | super_table[i]->disks->minor); | |
4650 | super_table[i] = s; | |
4651 | } | |
4652 | ||
4653 | /* This is where the mdadm implementation differs from the Windows | |
4654 | * driver which has no strict concept of a container. We can only | |
4655 | * assemble one family from a container, so when returning a prodigal | |
4656 | * array member to this system the code will not be able to disambiguate | |
4657 | * the container contents that should be assembled ("foreign" versus | |
4658 | * "local"). It requires user intervention to set the orig_family_num | |
4659 | * to a new value to establish a new container. The Windows driver in | |
4660 | * this situation fixes up the volume name in place and manages the | |
4661 | * foreign array as an independent entity. | |
4662 | */ | |
4663 | s = NULL; | |
4664 | spare = NULL; | |
4665 | conflict = 0; | |
4666 | for (i = 0; i < tbl_size; i++) { | |
4667 | struct intel_super *tbl_ent = super_table[i]; | |
4668 | int is_spare = 0; | |
4669 | ||
4670 | if (!tbl_ent) | |
4671 | continue; | |
4672 | ||
4673 | if (tbl_ent->anchor->num_raid_devs == 0) { | |
4674 | spare = tbl_ent; | |
4675 | is_spare = 1; | |
4676 | } | |
4677 | ||
4678 | if (s && !is_spare) { | |
4679 | show_conflicts(tbl_ent->anchor->family_num, *super_list); | |
4680 | conflict++; | |
4681 | } else if (!s && !is_spare) | |
4682 | s = tbl_ent; | |
4683 | } | |
4684 | ||
4685 | if (!s) | |
4686 | s = spare; | |
4687 | if (!s) { | |
4688 | champion = NULL; | |
4689 | goto out; | |
4690 | } | |
4691 | champion = s; | |
4692 | ||
4693 | if (conflict) | |
4694 | pr_err("Chose family %#x on '%s', assemble conflicts to new container with '--update=uuid'\n", | |
4695 | __le32_to_cpu(s->anchor->family_num), s->disks->devname); | |
4696 | ||
4697 | /* collect all dl's onto 'champion', and update them to | |
4698 | * champion's version of the status | |
4699 | */ | |
4700 | for (s = *super_list; s; s = s->next) { | |
4701 | struct imsm_super *mpb = champion->anchor; | |
4702 | struct dl *dl = s->disks; | |
4703 | ||
4704 | if (s == champion) | |
4705 | continue; | |
4706 | ||
4707 | mpb->attributes |= s->anchor->attributes & MPB_ATTRIB_2TB_DISK; | |
4708 | ||
4709 | for (i = 0; i < mpb->num_disks; i++) { | |
4710 | struct imsm_disk *disk; | |
4711 | ||
4712 | disk = __serial_to_disk(dl->serial, mpb, &dl->index); | |
4713 | if (disk) { | |
4714 | dl->disk = *disk; | |
4715 | /* only set index on disks that are a member of | |
4716 | * a populated contianer, i.e. one with | |
4717 | * raid_devs | |
4718 | */ | |
4719 | if (is_failed(&dl->disk)) | |
4720 | dl->index = -2; | |
4721 | else if (is_spare(&dl->disk)) | |
4722 | dl->index = -1; | |
4723 | break; | |
4724 | } | |
4725 | } | |
4726 | ||
4727 | if (i >= mpb->num_disks) { | |
4728 | struct intel_disk *idisk; | |
4729 | ||
4730 | idisk = disk_list_get(dl->serial, disk_list); | |
4731 | if (idisk && is_spare(&idisk->disk) && | |
4732 | !is_failed(&idisk->disk) && !is_configured(&idisk->disk)) | |
4733 | dl->index = -1; | |
4734 | else { | |
4735 | dl->index = -2; | |
4736 | continue; | |
4737 | } | |
4738 | } | |
4739 | ||
4740 | dl->next = champion->disks; | |
4741 | champion->disks = dl; | |
4742 | s->disks = NULL; | |
4743 | } | |
4744 | ||
4745 | /* delete 'champion' from super_list */ | |
4746 | for (del = super_list; *del; ) { | |
4747 | if (*del == champion) { | |
4748 | *del = (*del)->next; | |
4749 | break; | |
4750 | } else | |
4751 | del = &(*del)->next; | |
4752 | } | |
4753 | champion->next = NULL; | |
4754 | ||
4755 | out: | |
4756 | while (disk_list) { | |
4757 | struct intel_disk *idisk = disk_list; | |
4758 | ||
4759 | disk_list = disk_list->next; | |
4760 | free(idisk); | |
4761 | } | |
4762 | ||
4763 | return champion; | |
4764 | } | |
4765 | ||
4766 | static int | |
4767 | get_sra_super_block(int fd, struct intel_super **super_list, char *devname, int *max, int keep_fd); | |
4768 | static int get_super_block(struct intel_super **super_list, char *devnm, char *devname, | |
4769 | int major, int minor, int keep_fd); | |
4770 | static int | |
4771 | get_devlist_super_block(struct md_list *devlist, struct intel_super **super_list, | |
4772 | int *max, int keep_fd); | |
4773 | ||
4774 | static int load_super_imsm_all(struct supertype *st, int fd, void **sbp, | |
4775 | char *devname, struct md_list *devlist, | |
4776 | int keep_fd) | |
4777 | { | |
4778 | struct intel_super *super_list = NULL; | |
4779 | struct intel_super *super = NULL; | |
4780 | int err = 0; | |
4781 | int i = 0; | |
4782 | ||
4783 | if (fd >= 0) | |
4784 | /* 'fd' is an opened container */ | |
4785 | err = get_sra_super_block(fd, &super_list, devname, &i, keep_fd); | |
4786 | else | |
4787 | /* get super block from devlist devices */ | |
4788 | err = get_devlist_super_block(devlist, &super_list, &i, keep_fd); | |
4789 | if (err) | |
4790 | goto error; | |
4791 | /* all mpbs enter, maybe one leaves */ | |
4792 | super = imsm_thunderdome(&super_list, i); | |
4793 | if (!super) { | |
4794 | err = 1; | |
4795 | goto error; | |
4796 | } | |
4797 | ||
4798 | if (find_missing(super) != 0) { | |
4799 | free_imsm(super); | |
4800 | err = 2; | |
4801 | goto error; | |
4802 | } | |
4803 | ||
4804 | /* load migration record */ | |
4805 | err = load_imsm_migr_rec(super, NULL); | |
4806 | if (err == -1) { | |
4807 | /* migration is in progress, | |
4808 | * but migr_rec cannot be loaded, | |
4809 | */ | |
4810 | err = 4; | |
4811 | goto error; | |
4812 | } | |
4813 | ||
4814 | /* Check migration compatibility */ | |
4815 | if (err == 0 && check_mpb_migr_compatibility(super) != 0) { | |
4816 | pr_err("Unsupported migration detected"); | |
4817 | if (devname) | |
4818 | fprintf(stderr, " on %s\n", devname); | |
4819 | else | |
4820 | fprintf(stderr, " (IMSM).\n"); | |
4821 | ||
4822 | err = 5; | |
4823 | goto error; | |
4824 | } | |
4825 | ||
4826 | err = 0; | |
4827 | ||
4828 | error: | |
4829 | while (super_list) { | |
4830 | struct intel_super *s = super_list; | |
4831 | ||
4832 | super_list = super_list->next; | |
4833 | free_imsm(s); | |
4834 | } | |
4835 | ||
4836 | if (err) | |
4837 | return err; | |
4838 | ||
4839 | *sbp = super; | |
4840 | if (fd >= 0) | |
4841 | strcpy(st->container_devnm, fd2devnm(fd)); | |
4842 | else | |
4843 | st->container_devnm[0] = 0; | |
4844 | if (err == 0 && st->ss == NULL) { | |
4845 | st->ss = &super_imsm; | |
4846 | st->minor_version = 0; | |
4847 | st->max_devs = IMSM_MAX_DEVICES; | |
4848 | } | |
4849 | return 0; | |
4850 | } | |
4851 | ||
4852 | static int | |
4853 | get_devlist_super_block(struct md_list *devlist, struct intel_super **super_list, | |
4854 | int *max, int keep_fd) | |
4855 | { | |
4856 | struct md_list *tmpdev; | |
4857 | int err = 0; | |
4858 | int i = 0; | |
4859 | ||
4860 | for (i = 0, tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) { | |
4861 | if (tmpdev->used != 1) | |
4862 | continue; | |
4863 | if (tmpdev->container == 1) { | |
4864 | int lmax = 0; | |
4865 | int fd = dev_open(tmpdev->devname, O_RDONLY|O_EXCL); | |
4866 | if (fd < 0) { | |
4867 | pr_err("cannot open device %s: %s\n", | |
4868 | tmpdev->devname, strerror(errno)); | |
4869 | err = 8; | |
4870 | goto error; | |
4871 | } | |
4872 | err = get_sra_super_block(fd, super_list, | |
4873 | tmpdev->devname, &lmax, | |
4874 | keep_fd); | |
4875 | i += lmax; | |
4876 | close(fd); | |
4877 | if (err) { | |
4878 | err = 7; | |
4879 | goto error; | |
4880 | } | |
4881 | } else { | |
4882 | int major = major(tmpdev->st_rdev); | |
4883 | int minor = minor(tmpdev->st_rdev); | |
4884 | err = get_super_block(super_list, | |
4885 | NULL, | |
4886 | tmpdev->devname, | |
4887 | major, minor, | |
4888 | keep_fd); | |
4889 | i++; | |
4890 | if (err) { | |
4891 | err = 6; | |
4892 | goto error; | |
4893 | } | |
4894 | } | |
4895 | } | |
4896 | error: | |
4897 | *max = i; | |
4898 | return err; | |
4899 | } | |
4900 | ||
4901 | static int get_super_block(struct intel_super **super_list, char *devnm, char *devname, | |
4902 | int major, int minor, int keep_fd) | |
4903 | { | |
4904 | struct intel_super *s; | |
4905 | char nm[32]; | |
4906 | int dfd = -1; | |
4907 | int err = 0; | |
4908 | int retry; | |
4909 | ||
4910 | s = alloc_super(); | |
4911 | if (!s) { | |
4912 | err = 1; | |
4913 | goto error; | |
4914 | } | |
4915 | ||
4916 | sprintf(nm, "%d:%d", major, minor); | |
4917 | dfd = dev_open(nm, O_RDWR); | |
4918 | if (dfd < 0) { | |
4919 | err = 2; | |
4920 | goto error; | |
4921 | } | |
4922 | ||
4923 | get_dev_sector_size(dfd, NULL, &s->sector_size); | |
4924 | find_intel_hba_capability(dfd, s, devname); | |
4925 | err = load_and_parse_mpb(dfd, s, NULL, keep_fd); | |
4926 | ||
4927 | /* retry the load if we might have raced against mdmon */ | |
4928 | if (err == 3 && devnm && mdmon_running(devnm)) | |
4929 | for (retry = 0; retry < 3; retry++) { | |
4930 | usleep(3000); | |
4931 | err = load_and_parse_mpb(dfd, s, NULL, keep_fd); | |
4932 | if (err != 3) | |
4933 | break; | |
4934 | } | |
4935 | error: | |
4936 | if (!err) { | |
4937 | s->next = *super_list; | |
4938 | *super_list = s; | |
4939 | } else { | |
4940 | if (s) | |
4941 | free_imsm(s); | |
4942 | if (dfd >= 0) | |
4943 | close(dfd); | |
4944 | } | |
4945 | if (dfd >= 0 && !keep_fd) | |
4946 | close(dfd); | |
4947 | return err; | |
4948 | ||
4949 | } | |
4950 | ||
4951 | static int | |
4952 | get_sra_super_block(int fd, struct intel_super **super_list, char *devname, int *max, int keep_fd) | |
4953 | { | |
4954 | struct mdinfo *sra; | |
4955 | char *devnm; | |
4956 | struct mdinfo *sd; | |
4957 | int err = 0; | |
4958 | int i = 0; | |
4959 | sra = sysfs_read(fd, NULL, GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE); | |
4960 | if (!sra) | |
4961 | return 1; | |
4962 | ||
4963 | if (sra->array.major_version != -1 || | |
4964 | sra->array.minor_version != -2 || | |
4965 | strcmp(sra->text_version, "imsm") != 0) { | |
4966 | err = 1; | |
4967 | goto error; | |
4968 | } | |
4969 | /* load all mpbs */ | |
4970 | devnm = fd2devnm(fd); | |
4971 | for (sd = sra->devs, i = 0; sd; sd = sd->next, i++) { | |
4972 | if (get_super_block(super_list, devnm, devname, | |
4973 | sd->disk.major, sd->disk.minor, keep_fd) != 0) { | |
4974 | err = 7; | |
4975 | goto error; | |
4976 | } | |
4977 | } | |
4978 | error: | |
4979 | sysfs_free(sra); | |
4980 | *max = i; | |
4981 | return err; | |
4982 | } | |
4983 | ||
4984 | static int load_container_imsm(struct supertype *st, int fd, char *devname) | |
4985 | { | |
4986 | return load_super_imsm_all(st, fd, &st->sb, devname, NULL, 1); | |
4987 | } | |
4988 | #endif | |
4989 | ||
4990 | static int load_super_imsm(struct supertype *st, int fd, char *devname) | |
4991 | { | |
4992 | struct intel_super *super; | |
4993 | int rv; | |
4994 | int retry; | |
4995 | ||
4996 | if (test_partition(fd)) | |
4997 | /* IMSM not allowed on partitions */ | |
4998 | return 1; | |
4999 | ||
5000 | free_super_imsm(st); | |
5001 | ||
5002 | super = alloc_super(); | |
5003 | get_dev_sector_size(fd, NULL, &super->sector_size); | |
5004 | if (!super) | |
5005 | return 1; | |
5006 | /* Load hba and capabilities if they exist. | |
5007 | * But do not preclude loading metadata in case capabilities or hba are | |
5008 | * non-compliant and ignore_hw_compat is set. | |
5009 | */ | |
5010 | rv = find_intel_hba_capability(fd, super, devname); | |
5011 | /* no orom/efi or non-intel hba of the disk */ | |
5012 | if (rv != 0 && st->ignore_hw_compat == 0) { | |
5013 | if (devname) | |
5014 | pr_err("No OROM/EFI properties for %s\n", devname); | |
5015 | free_imsm(super); | |
5016 | return 2; | |
5017 | } | |
5018 | rv = load_and_parse_mpb(fd, super, devname, 0); | |
5019 | ||
5020 | /* retry the load if we might have raced against mdmon */ | |
5021 | if (rv == 3) { | |
5022 | struct mdstat_ent *mdstat = NULL; | |
5023 | char *name = fd2kname(fd); | |
5024 | ||
5025 | if (name) | |
5026 | mdstat = mdstat_by_component(name); | |
5027 | ||
5028 | if (mdstat && mdmon_running(mdstat->devnm) && getpid() != mdmon_pid(mdstat->devnm)) { | |
5029 | for (retry = 0; retry < 3; retry++) { | |
5030 | usleep(3000); | |
5031 | rv = load_and_parse_mpb(fd, super, devname, 0); | |
5032 | if (rv != 3) | |
5033 | break; | |
5034 | } | |
5035 | } | |
5036 | ||
5037 | free_mdstat(mdstat); | |
5038 | } | |
5039 | ||
5040 | if (rv) { | |
5041 | if (devname) | |
5042 | pr_err("Failed to load all information sections on %s\n", devname); | |
5043 | free_imsm(super); | |
5044 | return rv; | |
5045 | } | |
5046 | ||
5047 | st->sb = super; | |
5048 | if (st->ss == NULL) { | |
5049 | st->ss = &super_imsm; | |
5050 | st->minor_version = 0; | |
5051 | st->max_devs = IMSM_MAX_DEVICES; | |
5052 | } | |
5053 | ||
5054 | /* load migration record */ | |
5055 | if (load_imsm_migr_rec(super, NULL) == 0) { | |
5056 | /* Check for unsupported migration features */ | |
5057 | if (check_mpb_migr_compatibility(super) != 0) { | |
5058 | pr_err("Unsupported migration detected"); | |
5059 | if (devname) | |
5060 | fprintf(stderr, " on %s\n", devname); | |
5061 | else | |
5062 | fprintf(stderr, " (IMSM).\n"); | |
5063 | return 3; | |
5064 | } | |
5065 | } | |
5066 | ||
5067 | return 0; | |
5068 | } | |
5069 | ||
5070 | static __u16 info_to_blocks_per_strip(mdu_array_info_t *info) | |
5071 | { | |
5072 | if (info->level == 1) | |
5073 | return 128; | |
5074 | return info->chunk_size >> 9; | |
5075 | } | |
5076 | ||
5077 | static unsigned long long info_to_blocks_per_member(mdu_array_info_t *info, | |
5078 | unsigned long long size) | |
5079 | { | |
5080 | if (info->level == 1) | |
5081 | return size * 2; | |
5082 | else | |
5083 | return (size * 2) & ~(info_to_blocks_per_strip(info) - 1); | |
5084 | } | |
5085 | ||
5086 | static void imsm_update_version_info(struct intel_super *super) | |
5087 | { | |
5088 | /* update the version and attributes */ | |
5089 | struct imsm_super *mpb = super->anchor; | |
5090 | char *version; | |
5091 | struct imsm_dev *dev; | |
5092 | struct imsm_map *map; | |
5093 | int i; | |
5094 | ||
5095 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
5096 | dev = get_imsm_dev(super, i); | |
5097 | map = get_imsm_map(dev, MAP_0); | |
5098 | if (__le32_to_cpu(dev->size_high) > 0) | |
5099 | mpb->attributes |= MPB_ATTRIB_2TB; | |
5100 | ||
5101 | /* FIXME detect when an array spans a port multiplier */ | |
5102 | #if 0 | |
5103 | mpb->attributes |= MPB_ATTRIB_PM; | |
5104 | #endif | |
5105 | ||
5106 | if (mpb->num_raid_devs > 1 || | |
5107 | mpb->attributes != MPB_ATTRIB_CHECKSUM_VERIFY) { | |
5108 | version = MPB_VERSION_ATTRIBS; | |
5109 | switch (get_imsm_raid_level(map)) { | |
5110 | case 0: mpb->attributes |= MPB_ATTRIB_RAID0; break; | |
5111 | case 1: mpb->attributes |= MPB_ATTRIB_RAID1; break; | |
5112 | case 10: mpb->attributes |= MPB_ATTRIB_RAID10; break; | |
5113 | case 5: mpb->attributes |= MPB_ATTRIB_RAID5; break; | |
5114 | } | |
5115 | } else { | |
5116 | if (map->num_members >= 5) | |
5117 | version = MPB_VERSION_5OR6_DISK_ARRAY; | |
5118 | else if (dev->status == DEV_CLONE_N_GO) | |
5119 | version = MPB_VERSION_CNG; | |
5120 | else if (get_imsm_raid_level(map) == 5) | |
5121 | version = MPB_VERSION_RAID5; | |
5122 | else if (map->num_members >= 3) | |
5123 | version = MPB_VERSION_3OR4_DISK_ARRAY; | |
5124 | else if (get_imsm_raid_level(map) == 1) | |
5125 | version = MPB_VERSION_RAID1; | |
5126 | else | |
5127 | version = MPB_VERSION_RAID0; | |
5128 | } | |
5129 | strcpy(((char *) mpb->sig) + strlen(MPB_SIGNATURE), version); | |
5130 | } | |
5131 | } | |
5132 | ||
5133 | static int check_name(struct intel_super *super, char *name, int quiet) | |
5134 | { | |
5135 | struct imsm_super *mpb = super->anchor; | |
5136 | char *reason = NULL; | |
5137 | int i; | |
5138 | ||
5139 | if (strlen(name) > MAX_RAID_SERIAL_LEN) | |
5140 | reason = "must be 16 characters or less"; | |
5141 | ||
5142 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
5143 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
5144 | ||
5145 | if (strncmp((char *) dev->volume, name, MAX_RAID_SERIAL_LEN) == 0) { | |
5146 | reason = "already exists"; | |
5147 | break; | |
5148 | } | |
5149 | } | |
5150 | ||
5151 | if (reason && !quiet) | |
5152 | pr_err("imsm volume name %s\n", reason); | |
5153 | ||
5154 | return !reason; | |
5155 | } | |
5156 | ||
5157 | static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info, | |
5158 | struct shape *s, char *name, | |
5159 | char *homehost, int *uuid, | |
5160 | long long data_offset) | |
5161 | { | |
5162 | /* We are creating a volume inside a pre-existing container. | |
5163 | * so st->sb is already set. | |
5164 | */ | |
5165 | struct intel_super *super = st->sb; | |
5166 | unsigned int sector_size = super->sector_size; | |
5167 | struct imsm_super *mpb = super->anchor; | |
5168 | struct intel_dev *dv; | |
5169 | struct imsm_dev *dev; | |
5170 | struct imsm_vol *vol; | |
5171 | struct imsm_map *map; | |
5172 | int idx = mpb->num_raid_devs; | |
5173 | int i; | |
5174 | unsigned long long array_blocks; | |
5175 | size_t size_old, size_new; | |
5176 | unsigned long long num_data_stripes; | |
5177 | ||
5178 | if (super->orom && mpb->num_raid_devs >= super->orom->vpa) { | |
5179 | pr_err("This imsm-container already has the maximum of %d volumes\n", super->orom->vpa); | |
5180 | return 0; | |
5181 | } | |
5182 | ||
5183 | /* ensure the mpb is large enough for the new data */ | |
5184 | size_old = __le32_to_cpu(mpb->mpb_size); | |
5185 | size_new = disks_to_mpb_size(info->nr_disks); | |
5186 | if (size_new > size_old) { | |
5187 | void *mpb_new; | |
5188 | size_t size_round = ROUND_UP(size_new, sector_size); | |
5189 | ||
5190 | if (posix_memalign(&mpb_new, sector_size, size_round) != 0) { | |
5191 | pr_err("could not allocate new mpb\n"); | |
5192 | return 0; | |
5193 | } | |
5194 | if (posix_memalign(&super->migr_rec_buf, sector_size, | |
5195 | MIGR_REC_BUF_SECTORS*sector_size) != 0) { | |
5196 | pr_err("could not allocate migr_rec buffer\n"); | |
5197 | free(super->buf); | |
5198 | free(super); | |
5199 | free(mpb_new); | |
5200 | return 0; | |
5201 | } | |
5202 | memcpy(mpb_new, mpb, size_old); | |
5203 | free(mpb); | |
5204 | mpb = mpb_new; | |
5205 | super->anchor = mpb_new; | |
5206 | mpb->mpb_size = __cpu_to_le32(size_new); | |
5207 | memset(mpb_new + size_old, 0, size_round - size_old); | |
5208 | super->len = size_round; | |
5209 | } | |
5210 | super->current_vol = idx; | |
5211 | ||
5212 | /* handle 'failed_disks' by either: | |
5213 | * a) create dummy disk entries in the table if this the first | |
5214 | * volume in the array. We add them here as this is the only | |
5215 | * opportunity to add them. add_to_super_imsm_volume() | |
5216 | * handles the non-failed disks and continues incrementing | |
5217 | * mpb->num_disks. | |
5218 | * b) validate that 'failed_disks' matches the current number | |
5219 | * of missing disks if the container is populated | |
5220 | */ | |
5221 | if (super->current_vol == 0) { | |
5222 | mpb->num_disks = 0; | |
5223 | for (i = 0; i < info->failed_disks; i++) { | |
5224 | struct imsm_disk *disk; | |
5225 | ||
5226 | mpb->num_disks++; | |
5227 | disk = __get_imsm_disk(mpb, i); | |
5228 | disk->status = CONFIGURED_DISK | FAILED_DISK; | |
5229 | disk->scsi_id = __cpu_to_le32(~(__u32)0); | |
5230 | snprintf((char *) disk->serial, MAX_RAID_SERIAL_LEN, | |
5231 | "missing:%d", (__u8)i); | |
5232 | } | |
5233 | find_missing(super); | |
5234 | } else { | |
5235 | int missing = 0; | |
5236 | struct dl *d; | |
5237 | ||
5238 | for (d = super->missing; d; d = d->next) | |
5239 | missing++; | |
5240 | if (info->failed_disks > missing) { | |
5241 | pr_err("unable to add 'missing' disk to container\n"); | |
5242 | return 0; | |
5243 | } | |
5244 | } | |
5245 | ||
5246 | if (!check_name(super, name, 0)) | |
5247 | return 0; | |
5248 | dv = xmalloc(sizeof(*dv)); | |
5249 | dev = xcalloc(1, sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1)); | |
5250 | strncpy((char *) dev->volume, name, MAX_RAID_SERIAL_LEN); | |
5251 | array_blocks = calc_array_size(info->level, info->raid_disks, | |
5252 | info->layout, info->chunk_size, | |
5253 | s->size * 2); | |
5254 | /* round array size down to closest MB */ | |
5255 | array_blocks = (array_blocks >> SECT_PER_MB_SHIFT) << SECT_PER_MB_SHIFT; | |
5256 | ||
5257 | dev->size_low = __cpu_to_le32((__u32) array_blocks); | |
5258 | dev->size_high = __cpu_to_le32((__u32) (array_blocks >> 32)); | |
5259 | dev->status = (DEV_READ_COALESCING | DEV_WRITE_COALESCING); | |
5260 | vol = &dev->vol; | |
5261 | vol->migr_state = 0; | |
5262 | set_migr_type(dev, MIGR_INIT); | |
5263 | vol->dirty = !info->state; | |
5264 | vol->curr_migr_unit = 0; | |
5265 | map = get_imsm_map(dev, MAP_0); | |
5266 | set_pba_of_lba0(map, super->create_offset); | |
5267 | set_blocks_per_member(map, info_to_blocks_per_member(info, s->size)); | |
5268 | map->blocks_per_strip = __cpu_to_le16(info_to_blocks_per_strip(info)); | |
5269 | map->failed_disk_num = ~0; | |
5270 | if (info->level > 0) | |
5271 | map->map_state = (info->state ? IMSM_T_STATE_NORMAL | |
5272 | : IMSM_T_STATE_UNINITIALIZED); | |
5273 | else | |
5274 | map->map_state = info->failed_disks ? IMSM_T_STATE_FAILED : | |
5275 | IMSM_T_STATE_NORMAL; | |
5276 | map->ddf = 1; | |
5277 | ||
5278 | if (info->level == 1 && info->raid_disks > 2) { | |
5279 | free(dev); | |
5280 | free(dv); | |
5281 | pr_err("imsm does not support more than 2 disksin a raid1 volume\n"); | |
5282 | return 0; | |
5283 | } | |
5284 | ||
5285 | map->raid_level = info->level; | |
5286 | if (info->level == 10) { | |
5287 | map->raid_level = 1; | |
5288 | map->num_domains = info->raid_disks / 2; | |
5289 | } else if (info->level == 1) | |
5290 | map->num_domains = info->raid_disks; | |
5291 | else | |
5292 | map->num_domains = 1; | |
5293 | ||
5294 | /* info->size is only int so use the 'size' parameter instead */ | |
5295 | num_data_stripes = (s->size * 2) / info_to_blocks_per_strip(info); | |
5296 | num_data_stripes /= map->num_domains; | |
5297 | set_num_data_stripes(map, num_data_stripes); | |
5298 | ||
5299 | map->num_members = info->raid_disks; | |
5300 | for (i = 0; i < map->num_members; i++) { | |
5301 | /* initialized in add_to_super */ | |
5302 | set_imsm_ord_tbl_ent(map, i, IMSM_ORD_REBUILD); | |
5303 | } | |
5304 | mpb->num_raid_devs++; | |
5305 | ||
5306 | dv->dev = dev; | |
5307 | dv->index = super->current_vol; | |
5308 | dv->next = super->devlist; | |
5309 | super->devlist = dv; | |
5310 | ||
5311 | imsm_update_version_info(super); | |
5312 | ||
5313 | return 1; | |
5314 | } | |
5315 | ||
5316 | static int init_super_imsm(struct supertype *st, mdu_array_info_t *info, | |
5317 | struct shape *s, char *name, | |
5318 | char *homehost, int *uuid, | |
5319 | unsigned long long data_offset) | |
5320 | { | |
5321 | /* This is primarily called by Create when creating a new array. | |
5322 | * We will then get add_to_super called for each component, and then | |
5323 | * write_init_super called to write it out to each device. | |
5324 | * For IMSM, Create can create on fresh devices or on a pre-existing | |
5325 | * array. | |
5326 | * To create on a pre-existing array a different method will be called. | |
5327 | * This one is just for fresh drives. | |
5328 | */ | |
5329 | struct intel_super *super; | |
5330 | struct imsm_super *mpb; | |
5331 | size_t mpb_size; | |
5332 | char *version; | |
5333 | ||
5334 | if (data_offset != INVALID_SECTORS) { | |
5335 | pr_err("data-offset not supported by imsm\n"); | |
5336 | return 0; | |
5337 | } | |
5338 | ||
5339 | if (st->sb) | |
5340 | return init_super_imsm_volume(st, info, s, name, homehost, uuid, | |
5341 | data_offset); | |
5342 | ||
5343 | if (info) | |
5344 | mpb_size = disks_to_mpb_size(info->nr_disks); | |
5345 | else | |
5346 | mpb_size = MAX_SECTOR_SIZE; | |
5347 | ||
5348 | super = alloc_super(); | |
5349 | if (super && | |
5350 | posix_memalign(&super->buf, MAX_SECTOR_SIZE, mpb_size) != 0) { | |
5351 | free_imsm(super); | |
5352 | super = NULL; | |
5353 | } | |
5354 | if (!super) { | |
5355 | pr_err("could not allocate superblock\n"); | |
5356 | return 0; | |
5357 | } | |
5358 | if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE, | |
5359 | MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE) != 0) { | |
5360 | pr_err("could not allocate migr_rec buffer\n"); | |
5361 | free(super->buf); | |
5362 | free_imsm(super); | |
5363 | return 0; | |
5364 | } | |
5365 | memset(super->buf, 0, mpb_size); | |
5366 | mpb = super->buf; | |
5367 | mpb->mpb_size = __cpu_to_le32(mpb_size); | |
5368 | st->sb = super; | |
5369 | ||
5370 | if (info == NULL) { | |
5371 | /* zeroing superblock */ | |
5372 | return 0; | |
5373 | } | |
5374 | ||
5375 | mpb->attributes = MPB_ATTRIB_CHECKSUM_VERIFY; | |
5376 | ||
5377 | version = (char *) mpb->sig; | |
5378 | strcpy(version, MPB_SIGNATURE); | |
5379 | version += strlen(MPB_SIGNATURE); | |
5380 | strcpy(version, MPB_VERSION_RAID0); | |
5381 | ||
5382 | return 1; | |
5383 | } | |
5384 | ||
5385 | #ifndef MDASSEMBLE | |
5386 | static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk, | |
5387 | int fd, char *devname) | |
5388 | { | |
5389 | struct intel_super *super = st->sb; | |
5390 | struct imsm_super *mpb = super->anchor; | |
5391 | struct imsm_disk *_disk; | |
5392 | struct imsm_dev *dev; | |
5393 | struct imsm_map *map; | |
5394 | struct dl *dl, *df; | |
5395 | int slot; | |
5396 | ||
5397 | dev = get_imsm_dev(super, super->current_vol); | |
5398 | map = get_imsm_map(dev, MAP_0); | |
5399 | ||
5400 | if (! (dk->state & (1<<MD_DISK_SYNC))) { | |
5401 | pr_err("%s: Cannot add spare devices to IMSM volume\n", | |
5402 | devname); | |
5403 | return 1; | |
5404 | } | |
5405 | ||
5406 | if (fd == -1) { | |
5407 | /* we're doing autolayout so grab the pre-marked (in | |
5408 | * validate_geometry) raid_disk | |
5409 | */ | |
5410 | for (dl = super->disks; dl; dl = dl->next) | |
5411 | if (dl->raiddisk == dk->raid_disk) | |
5412 | break; | |
5413 | } else { | |
5414 | for (dl = super->disks; dl ; dl = dl->next) | |
5415 | if (dl->major == dk->major && | |
5416 | dl->minor == dk->minor) | |
5417 | break; | |
5418 | } | |
5419 | ||
5420 | if (!dl) { | |
5421 | pr_err("%s is not a member of the same container\n", devname); | |
5422 | return 1; | |
5423 | } | |
5424 | ||
5425 | /* add a pristine spare to the metadata */ | |
5426 | if (dl->index < 0) { | |
5427 | dl->index = super->anchor->num_disks; | |
5428 | super->anchor->num_disks++; | |
5429 | } | |
5430 | /* Check the device has not already been added */ | |
5431 | slot = get_imsm_disk_slot(map, dl->index); | |
5432 | if (slot >= 0 && | |
5433 | (get_imsm_ord_tbl_ent(dev, slot, MAP_X) & IMSM_ORD_REBUILD) == 0) { | |
5434 | pr_err("%s has been included in this array twice\n", | |
5435 | devname); | |
5436 | return 1; | |
5437 | } | |
5438 | set_imsm_ord_tbl_ent(map, dk->raid_disk, dl->index); | |
5439 | dl->disk.status = CONFIGURED_DISK; | |
5440 | ||
5441 | /* update size of 'missing' disks to be at least as large as the | |
5442 | * largest acitve member (we only have dummy missing disks when | |
5443 | * creating the first volume) | |
5444 | */ | |
5445 | if (super->current_vol == 0) { | |
5446 | for (df = super->missing; df; df = df->next) { | |
5447 | if (total_blocks(&dl->disk) > total_blocks(&df->disk)) | |
5448 | set_total_blocks(&df->disk, total_blocks(&dl->disk)); | |
5449 | _disk = __get_imsm_disk(mpb, df->index); | |
5450 | *_disk = df->disk; | |
5451 | } | |
5452 | } | |
5453 | ||
5454 | /* refresh unset/failed slots to point to valid 'missing' entries */ | |
5455 | for (df = super->missing; df; df = df->next) | |
5456 | for (slot = 0; slot < mpb->num_disks; slot++) { | |
5457 | __u32 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X); | |
5458 | ||
5459 | if ((ord & IMSM_ORD_REBUILD) == 0) | |
5460 | continue; | |
5461 | set_imsm_ord_tbl_ent(map, slot, df->index | IMSM_ORD_REBUILD); | |
5462 | if (is_gen_migration(dev)) { | |
5463 | struct imsm_map *map2 = get_imsm_map(dev, | |
5464 | MAP_1); | |
5465 | int slot2 = get_imsm_disk_slot(map2, df->index); | |
5466 | if (slot2 < map2->num_members && slot2 >= 0) { | |
5467 | __u32 ord2 = get_imsm_ord_tbl_ent(dev, | |
5468 | slot2, | |
5469 | MAP_1); | |
5470 | if ((unsigned)df->index == | |
5471 | ord_to_idx(ord2)) | |
5472 | set_imsm_ord_tbl_ent(map2, | |
5473 | slot2, | |
5474 | df->index | | |
5475 | IMSM_ORD_REBUILD); | |
5476 | } | |
5477 | } | |
5478 | dprintf("set slot:%d to missing disk:%d\n", slot, df->index); | |
5479 | break; | |
5480 | } | |
5481 | ||
5482 | /* if we are creating the first raid device update the family number */ | |
5483 | if (super->current_vol == 0) { | |
5484 | __u32 sum; | |
5485 | struct imsm_dev *_dev = __get_imsm_dev(mpb, 0); | |
5486 | ||
5487 | _disk = __get_imsm_disk(mpb, dl->index); | |
5488 | if (!_dev || !_disk) { | |
5489 | pr_err("BUG mpb setup error\n"); | |
5490 | return 1; | |
5491 | } | |
5492 | *_dev = *dev; | |
5493 | *_disk = dl->disk; | |
5494 | sum = random32(); | |
5495 | sum += __gen_imsm_checksum(mpb); | |
5496 | mpb->family_num = __cpu_to_le32(sum); | |
5497 | mpb->orig_family_num = mpb->family_num; | |
5498 | } | |
5499 | super->current_disk = dl; | |
5500 | return 0; | |
5501 | } | |
5502 | ||
5503 | /* mark_spare() | |
5504 | * Function marks disk as spare and restores disk serial | |
5505 | * in case it was previously marked as failed by takeover operation | |
5506 | * reruns: | |
5507 | * -1 : critical error | |
5508 | * 0 : disk is marked as spare but serial is not set | |
5509 | * 1 : success | |
5510 | */ | |
5511 | int mark_spare(struct dl *disk) | |
5512 | { | |
5513 | __u8 serial[MAX_RAID_SERIAL_LEN]; | |
5514 | int ret_val = -1; | |
5515 | ||
5516 | if (!disk) | |
5517 | return ret_val; | |
5518 | ||
5519 | ret_val = 0; | |
5520 | if (!imsm_read_serial(disk->fd, NULL, serial)) { | |
5521 | /* Restore disk serial number, because takeover marks disk | |
5522 | * as failed and adds to serial ':0' before it becomes | |
5523 | * a spare disk. | |
5524 | */ | |
5525 | serialcpy(disk->serial, serial); | |
5526 | serialcpy(disk->disk.serial, serial); | |
5527 | ret_val = 1; | |
5528 | } | |
5529 | disk->disk.status = SPARE_DISK; | |
5530 | disk->index = -1; | |
5531 | ||
5532 | return ret_val; | |
5533 | } | |
5534 | ||
5535 | static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk, | |
5536 | int fd, char *devname, | |
5537 | unsigned long long data_offset) | |
5538 | { | |
5539 | struct intel_super *super = st->sb; | |
5540 | struct dl *dd; | |
5541 | unsigned long long size; | |
5542 | unsigned int member_sector_size; | |
5543 | __u32 id; | |
5544 | int rv; | |
5545 | struct stat stb; | |
5546 | ||
5547 | /* If we are on an RAID enabled platform check that the disk is | |
5548 | * attached to the raid controller. | |
5549 | * We do not need to test disks attachment for container based additions, | |
5550 | * they shall be already tested when container was created/assembled. | |
5551 | */ | |
5552 | rv = find_intel_hba_capability(fd, super, devname); | |
5553 | /* no orom/efi or non-intel hba of the disk */ | |
5554 | if (rv != 0) { | |
5555 | dprintf("capability: %p fd: %d ret: %d\n", | |
5556 | super->orom, fd, rv); | |
5557 | return 1; | |
5558 | } | |
5559 | ||
5560 | if (super->current_vol >= 0) | |
5561 | return add_to_super_imsm_volume(st, dk, fd, devname); | |
5562 | ||
5563 | fstat(fd, &stb); | |
5564 | dd = xcalloc(sizeof(*dd), 1); | |
5565 | dd->major = major(stb.st_rdev); | |
5566 | dd->minor = minor(stb.st_rdev); | |
5567 | dd->devname = devname ? xstrdup(devname) : NULL; | |
5568 | dd->fd = fd; | |
5569 | dd->e = NULL; | |
5570 | dd->action = DISK_ADD; | |
5571 | rv = imsm_read_serial(fd, devname, dd->serial); | |
5572 | if (rv) { | |
5573 | pr_err("failed to retrieve scsi serial, aborting\n"); | |
5574 | if (dd->devname) | |
5575 | free(dd->devname); | |
5576 | free(dd); | |
5577 | abort(); | |
5578 | } | |
5579 | if (super->hba && ((super->hba->type == SYS_DEV_NVME) || | |
5580 | (super->hba->type == SYS_DEV_VMD))) { | |
5581 | int i; | |
5582 | char *devpath = diskfd_to_devpath(fd); | |
5583 | char controller_path[PATH_MAX]; | |
5584 | ||
5585 | if (!devpath) { | |
5586 | pr_err("failed to get devpath, aborting\n"); | |
5587 | if (dd->devname) | |
5588 | free(dd->devname); | |
5589 | free(dd); | |
5590 | return 1; | |
5591 | } | |
5592 | ||
5593 | snprintf(controller_path, PATH_MAX-1, "%s/device", devpath); | |
5594 | free(devpath); | |
5595 | ||
5596 | if (devpath_to_vendor(controller_path) == 0x8086) { | |
5597 | /* | |
5598 | * If Intel's NVMe drive has serial ended with | |
5599 | * "-A","-B","-1" or "-2" it means that this is "x8" | |
5600 | * device (double drive on single PCIe card). | |
5601 | * User should be warned about potential data loss. | |
5602 | */ | |
5603 | for (i = MAX_RAID_SERIAL_LEN-1; i > 0; i--) { | |
5604 | /* Skip empty character at the end */ | |
5605 | if (dd->serial[i] == 0) | |
5606 | continue; | |
5607 | ||
5608 | if (((dd->serial[i] == 'A') || | |
5609 | (dd->serial[i] == 'B') || | |
5610 | (dd->serial[i] == '1') || | |
5611 | (dd->serial[i] == '2')) && | |
5612 | (dd->serial[i-1] == '-')) | |
5613 | pr_err("\tThe action you are about to take may put your data at risk.\n" | |
5614 | "\tPlease note that x8 devices may consist of two separate x4 devices " | |
5615 | "located on a single PCIe port.\n" | |
5616 | "\tRAID 0 is the only supported configuration for this type of x8 device.\n"); | |
5617 | break; | |
5618 | } | |
5619 | } else if (super->hba->type == SYS_DEV_VMD && super->orom && | |
5620 | !imsm_orom_has_tpv_support(super->orom)) { | |
5621 | pr_err("\tPlatform configuration does not support non-Intel NVMe drives.\n" | |
5622 | "\tPlease refer to Intel(R) RSTe user guide.\n"); | |
5623 | free(dd->devname); | |
5624 | free(dd); | |
5625 | return 1; | |
5626 | } | |
5627 | } | |
5628 | ||
5629 | get_dev_size(fd, NULL, &size); | |
5630 | get_dev_sector_size(fd, NULL, &member_sector_size); | |
5631 | ||
5632 | if (super->sector_size == 0) { | |
5633 | /* this a first device, so sector_size is not set yet */ | |
5634 | super->sector_size = member_sector_size; | |
5635 | } else if (member_sector_size != super->sector_size) { | |
5636 | pr_err("Mixing between different sector size is forbidden, aborting...\n"); | |
5637 | if (dd->devname) | |
5638 | free(dd->devname); | |
5639 | free(dd); | |
5640 | return 1; | |
5641 | } | |
5642 | ||
5643 | /* clear migr_rec when adding disk to container */ | |
5644 | memset(super->migr_rec_buf, 0, MIGR_REC_BUF_SECTORS*super->sector_size); | |
5645 | if (lseek64(fd, size - MIGR_REC_SECTOR_POSITION*super->sector_size, | |
5646 | SEEK_SET) >= 0) { | |
5647 | if ((unsigned int)write(fd, super->migr_rec_buf, | |
5648 | MIGR_REC_BUF_SECTORS*super->sector_size) != | |
5649 | MIGR_REC_BUF_SECTORS*super->sector_size) | |
5650 | perror("Write migr_rec failed"); | |
5651 | } | |
5652 | ||
5653 | size /= 512; | |
5654 | serialcpy(dd->disk.serial, dd->serial); | |
5655 | set_total_blocks(&dd->disk, size); | |
5656 | if (__le32_to_cpu(dd->disk.total_blocks_hi) > 0) { | |
5657 | struct imsm_super *mpb = super->anchor; | |
5658 | mpb->attributes |= MPB_ATTRIB_2TB_DISK; | |
5659 | } | |
5660 | mark_spare(dd); | |
5661 | if (sysfs_disk_to_scsi_id(fd, &id) == 0) | |
5662 | dd->disk.scsi_id = __cpu_to_le32(id); | |
5663 | else | |
5664 | dd->disk.scsi_id = __cpu_to_le32(0); | |
5665 | ||
5666 | if (st->update_tail) { | |
5667 | dd->next = super->disk_mgmt_list; | |
5668 | super->disk_mgmt_list = dd; | |
5669 | } else { | |
5670 | dd->next = super->disks; | |
5671 | super->disks = dd; | |
5672 | super->updates_pending++; | |
5673 | } | |
5674 | ||
5675 | return 0; | |
5676 | } | |
5677 | ||
5678 | static int remove_from_super_imsm(struct supertype *st, mdu_disk_info_t *dk) | |
5679 | { | |
5680 | struct intel_super *super = st->sb; | |
5681 | struct dl *dd; | |
5682 | ||
5683 | /* remove from super works only in mdmon - for communication | |
5684 | * manager - monitor. Check if communication memory buffer | |
5685 | * is prepared. | |
5686 | */ | |
5687 | if (!st->update_tail) { | |
5688 | pr_err("shall be used in mdmon context only\n"); | |
5689 | return 1; | |
5690 | } | |
5691 | dd = xcalloc(1, sizeof(*dd)); | |
5692 | dd->major = dk->major; | |
5693 | dd->minor = dk->minor; | |
5694 | dd->fd = -1; | |
5695 | mark_spare(dd); | |
5696 | dd->action = DISK_REMOVE; | |
5697 | ||
5698 | dd->next = super->disk_mgmt_list; | |
5699 | super->disk_mgmt_list = dd; | |
5700 | ||
5701 | return 0; | |
5702 | } | |
5703 | ||
5704 | static int store_imsm_mpb(int fd, struct imsm_super *mpb); | |
5705 | ||
5706 | static union { | |
5707 | char buf[MAX_SECTOR_SIZE]; | |
5708 | struct imsm_super anchor; | |
5709 | } spare_record __attribute__ ((aligned(MAX_SECTOR_SIZE))); | |
5710 | ||
5711 | /* spare records have their own family number and do not have any defined raid | |
5712 | * devices | |
5713 | */ | |
5714 | static int write_super_imsm_spares(struct intel_super *super, int doclose) | |
5715 | { | |
5716 | struct imsm_super *mpb = super->anchor; | |
5717 | struct imsm_super *spare = &spare_record.anchor; | |
5718 | __u32 sum; | |
5719 | struct dl *d; | |
5720 | ||
5721 | spare->mpb_size = __cpu_to_le32(sizeof(struct imsm_super)); | |
5722 | spare->generation_num = __cpu_to_le32(1UL); | |
5723 | spare->attributes = MPB_ATTRIB_CHECKSUM_VERIFY; | |
5724 | spare->num_disks = 1; | |
5725 | spare->num_raid_devs = 0; | |
5726 | spare->cache_size = mpb->cache_size; | |
5727 | spare->pwr_cycle_count = __cpu_to_le32(1); | |
5728 | ||
5729 | snprintf((char *) spare->sig, MAX_SIGNATURE_LENGTH, | |
5730 | MPB_SIGNATURE MPB_VERSION_RAID0); | |
5731 | ||
5732 | for (d = super->disks; d; d = d->next) { | |
5733 | if (d->index != -1) | |
5734 | continue; | |
5735 | ||
5736 | spare->disk[0] = d->disk; | |
5737 | if (__le32_to_cpu(d->disk.total_blocks_hi) > 0) | |
5738 | spare->attributes |= MPB_ATTRIB_2TB_DISK; | |
5739 | ||
5740 | if (super->sector_size == 4096) | |
5741 | convert_to_4k_imsm_disk(&spare->disk[0]); | |
5742 | ||
5743 | sum = __gen_imsm_checksum(spare); | |
5744 | spare->family_num = __cpu_to_le32(sum); | |
5745 | spare->orig_family_num = 0; | |
5746 | sum = __gen_imsm_checksum(spare); | |
5747 | spare->check_sum = __cpu_to_le32(sum); | |
5748 | ||
5749 | if (store_imsm_mpb(d->fd, spare)) { | |
5750 | pr_err("failed for device %d:%d %s\n", | |
5751 | d->major, d->minor, strerror(errno)); | |
5752 | return 1; | |
5753 | } | |
5754 | if (doclose) { | |
5755 | close(d->fd); | |
5756 | d->fd = -1; | |
5757 | } | |
5758 | } | |
5759 | ||
5760 | return 0; | |
5761 | } | |
5762 | ||
5763 | static int write_super_imsm(struct supertype *st, int doclose) | |
5764 | { | |
5765 | struct intel_super *super = st->sb; | |
5766 | unsigned int sector_size = super->sector_size; | |
5767 | struct imsm_super *mpb = super->anchor; | |
5768 | struct dl *d; | |
5769 | __u32 generation; | |
5770 | __u32 sum; | |
5771 | int spares = 0; | |
5772 | int i; | |
5773 | __u32 mpb_size = sizeof(struct imsm_super) - sizeof(struct imsm_disk); | |
5774 | int num_disks = 0; | |
5775 | int clear_migration_record = 1; | |
5776 | __u32 bbm_log_size; | |
5777 | ||
5778 | /* 'generation' is incremented everytime the metadata is written */ | |
5779 | generation = __le32_to_cpu(mpb->generation_num); | |
5780 | generation++; | |
5781 | mpb->generation_num = __cpu_to_le32(generation); | |
5782 | ||
5783 | /* fix up cases where previous mdadm releases failed to set | |
5784 | * orig_family_num | |
5785 | */ | |
5786 | if (mpb->orig_family_num == 0) | |
5787 | mpb->orig_family_num = mpb->family_num; | |
5788 | ||
5789 | for (d = super->disks; d; d = d->next) { | |
5790 | if (d->index == -1) | |
5791 | spares++; | |
5792 | else { | |
5793 | mpb->disk[d->index] = d->disk; | |
5794 | num_disks++; | |
5795 | } | |
5796 | } | |
5797 | for (d = super->missing; d; d = d->next) { | |
5798 | mpb->disk[d->index] = d->disk; | |
5799 | num_disks++; | |
5800 | } | |
5801 | mpb->num_disks = num_disks; | |
5802 | mpb_size += sizeof(struct imsm_disk) * mpb->num_disks; | |
5803 | ||
5804 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
5805 | struct imsm_dev *dev = __get_imsm_dev(mpb, i); | |
5806 | struct imsm_dev *dev2 = get_imsm_dev(super, i); | |
5807 | if (dev && dev2) { | |
5808 | imsm_copy_dev(dev, dev2); | |
5809 | mpb_size += sizeof_imsm_dev(dev, 0); | |
5810 | } | |
5811 | if (is_gen_migration(dev2)) | |
5812 | clear_migration_record = 0; | |
5813 | } | |
5814 | ||
5815 | bbm_log_size = get_imsm_bbm_log_size(super->bbm_log); | |
5816 | ||
5817 | if (bbm_log_size) { | |
5818 | memcpy((void *)mpb + mpb_size, super->bbm_log, bbm_log_size); | |
5819 | mpb->attributes |= MPB_ATTRIB_BBM; | |
5820 | } else | |
5821 | mpb->attributes &= ~MPB_ATTRIB_BBM; | |
5822 | ||
5823 | super->anchor->bbm_log_size = __cpu_to_le32(bbm_log_size); | |
5824 | mpb_size += bbm_log_size; | |
5825 | mpb->mpb_size = __cpu_to_le32(mpb_size); | |
5826 | ||
5827 | #ifdef DEBUG | |
5828 | assert(super->len == 0 || mpb_size <= super->len); | |
5829 | #endif | |
5830 | ||
5831 | /* recalculate checksum */ | |
5832 | sum = __gen_imsm_checksum(mpb); | |
5833 | mpb->check_sum = __cpu_to_le32(sum); | |
5834 | ||
5835 | if (super->clean_migration_record_by_mdmon) { | |
5836 | clear_migration_record = 1; | |
5837 | super->clean_migration_record_by_mdmon = 0; | |
5838 | } | |
5839 | if (clear_migration_record) | |
5840 | memset(super->migr_rec_buf, 0, | |
5841 | MIGR_REC_BUF_SECTORS*sector_size); | |
5842 | ||
5843 | if (sector_size == 4096) | |
5844 | convert_to_4k(super); | |
5845 | ||
5846 | /* write the mpb for disks that compose raid devices */ | |
5847 | for (d = super->disks; d ; d = d->next) { | |
5848 | if (d->index < 0 || is_failed(&d->disk)) | |
5849 | continue; | |
5850 | ||
5851 | if (clear_migration_record) { | |
5852 | unsigned long long dsize; | |
5853 | ||
5854 | get_dev_size(d->fd, NULL, &dsize); | |
5855 | if (lseek64(d->fd, dsize - sector_size, | |
5856 | SEEK_SET) >= 0) { | |
5857 | if ((unsigned int)write(d->fd, | |
5858 | super->migr_rec_buf, | |
5859 | MIGR_REC_BUF_SECTORS*sector_size) != | |
5860 | MIGR_REC_BUF_SECTORS*sector_size) | |
5861 | perror("Write migr_rec failed"); | |
5862 | } | |
5863 | } | |
5864 | ||
5865 | if (store_imsm_mpb(d->fd, mpb)) | |
5866 | fprintf(stderr, | |
5867 | "failed for device %d:%d (fd: %d)%s\n", | |
5868 | d->major, d->minor, | |
5869 | d->fd, strerror(errno)); | |
5870 | ||
5871 | if (doclose) { | |
5872 | close(d->fd); | |
5873 | d->fd = -1; | |
5874 | } | |
5875 | } | |
5876 | ||
5877 | if (spares) | |
5878 | return write_super_imsm_spares(super, doclose); | |
5879 | ||
5880 | return 0; | |
5881 | } | |
5882 | ||
5883 | static int create_array(struct supertype *st, int dev_idx) | |
5884 | { | |
5885 | size_t len; | |
5886 | struct imsm_update_create_array *u; | |
5887 | struct intel_super *super = st->sb; | |
5888 | struct imsm_dev *dev = get_imsm_dev(super, dev_idx); | |
5889 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
5890 | struct disk_info *inf; | |
5891 | struct imsm_disk *disk; | |
5892 | int i; | |
5893 | ||
5894 | len = sizeof(*u) - sizeof(*dev) + sizeof_imsm_dev(dev, 0) + | |
5895 | sizeof(*inf) * map->num_members; | |
5896 | u = xmalloc(len); | |
5897 | u->type = update_create_array; | |
5898 | u->dev_idx = dev_idx; | |
5899 | imsm_copy_dev(&u->dev, dev); | |
5900 | inf = get_disk_info(u); | |
5901 | for (i = 0; i < map->num_members; i++) { | |
5902 | int idx = get_imsm_disk_idx(dev, i, MAP_X); | |
5903 | ||
5904 | disk = get_imsm_disk(super, idx); | |
5905 | if (!disk) | |
5906 | disk = get_imsm_missing(super, idx); | |
5907 | serialcpy(inf[i].serial, disk->serial); | |
5908 | } | |
5909 | append_metadata_update(st, u, len); | |
5910 | ||
5911 | return 0; | |
5912 | } | |
5913 | ||
5914 | static int mgmt_disk(struct supertype *st) | |
5915 | { | |
5916 | struct intel_super *super = st->sb; | |
5917 | size_t len; | |
5918 | struct imsm_update_add_remove_disk *u; | |
5919 | ||
5920 | if (!super->disk_mgmt_list) | |
5921 | return 0; | |
5922 | ||
5923 | len = sizeof(*u); | |
5924 | u = xmalloc(len); | |
5925 | u->type = update_add_remove_disk; | |
5926 | append_metadata_update(st, u, len); | |
5927 | ||
5928 | return 0; | |
5929 | } | |
5930 | ||
5931 | static int write_init_super_imsm(struct supertype *st) | |
5932 | { | |
5933 | struct intel_super *super = st->sb; | |
5934 | int current_vol = super->current_vol; | |
5935 | ||
5936 | /* we are done with current_vol reset it to point st at the container */ | |
5937 | super->current_vol = -1; | |
5938 | ||
5939 | if (st->update_tail) { | |
5940 | /* queue the recently created array / added disk | |
5941 | * as a metadata update */ | |
5942 | int rv; | |
5943 | ||
5944 | /* determine if we are creating a volume or adding a disk */ | |
5945 | if (current_vol < 0) { | |
5946 | /* in the mgmt (add/remove) disk case we are running | |
5947 | * in mdmon context, so don't close fd's | |
5948 | */ | |
5949 | return mgmt_disk(st); | |
5950 | } else | |
5951 | rv = create_array(st, current_vol); | |
5952 | ||
5953 | return rv; | |
5954 | } else { | |
5955 | struct dl *d; | |
5956 | for (d = super->disks; d; d = d->next) | |
5957 | Kill(d->devname, NULL, 0, -1, 1); | |
5958 | return write_super_imsm(st, 1); | |
5959 | } | |
5960 | } | |
5961 | #endif | |
5962 | ||
5963 | static int store_super_imsm(struct supertype *st, int fd) | |
5964 | { | |
5965 | struct intel_super *super = st->sb; | |
5966 | struct imsm_super *mpb = super ? super->anchor : NULL; | |
5967 | ||
5968 | if (!mpb) | |
5969 | return 1; | |
5970 | ||
5971 | #ifndef MDASSEMBLE | |
5972 | if (super->sector_size == 4096) | |
5973 | convert_to_4k(super); | |
5974 | return store_imsm_mpb(fd, mpb); | |
5975 | #else | |
5976 | return 1; | |
5977 | #endif | |
5978 | } | |
5979 | ||
5980 | #ifndef MDASSEMBLE | |
5981 | static int validate_geometry_imsm_container(struct supertype *st, int level, | |
5982 | int layout, int raiddisks, int chunk, | |
5983 | unsigned long long size, | |
5984 | unsigned long long data_offset, | |
5985 | char *dev, | |
5986 | unsigned long long *freesize, | |
5987 | int verbose) | |
5988 | { | |
5989 | int fd; | |
5990 | unsigned long long ldsize; | |
5991 | struct intel_super *super; | |
5992 | int rv = 0; | |
5993 | ||
5994 | if (level != LEVEL_CONTAINER) | |
5995 | return 0; | |
5996 | if (!dev) | |
5997 | return 1; | |
5998 | ||
5999 | fd = open(dev, O_RDONLY|O_EXCL, 0); | |
6000 | if (fd < 0) { | |
6001 | if (verbose > 0) | |
6002 | pr_err("imsm: Cannot open %s: %s\n", | |
6003 | dev, strerror(errno)); | |
6004 | return 0; | |
6005 | } | |
6006 | if (!get_dev_size(fd, dev, &ldsize)) { | |
6007 | close(fd); | |
6008 | return 0; | |
6009 | } | |
6010 | ||
6011 | /* capabilities retrieve could be possible | |
6012 | * note that there is no fd for the disks in array. | |
6013 | */ | |
6014 | super = alloc_super(); | |
6015 | if (!super) { | |
6016 | close(fd); | |
6017 | return 0; | |
6018 | } | |
6019 | if (!get_dev_sector_size(fd, NULL, &super->sector_size)) { | |
6020 | close(fd); | |
6021 | free_imsm(super); | |
6022 | return 0; | |
6023 | } | |
6024 | ||
6025 | rv = find_intel_hba_capability(fd, super, verbose > 0 ? dev : NULL); | |
6026 | if (rv != 0) { | |
6027 | #if DEBUG | |
6028 | char str[256]; | |
6029 | fd2devname(fd, str); | |
6030 | dprintf("fd: %d %s orom: %p rv: %d raiddisk: %d\n", | |
6031 | fd, str, super->orom, rv, raiddisks); | |
6032 | #endif | |
6033 | /* no orom/efi or non-intel hba of the disk */ | |
6034 | close(fd); | |
6035 | free_imsm(super); | |
6036 | return 0; | |
6037 | } | |
6038 | close(fd); | |
6039 | if (super->orom) { | |
6040 | if (raiddisks > super->orom->tds) { | |
6041 | if (verbose) | |
6042 | pr_err("%d exceeds maximum number of platform supported disks: %d\n", | |
6043 | raiddisks, super->orom->tds); | |
6044 | free_imsm(super); | |
6045 | return 0; | |
6046 | } | |
6047 | if ((super->orom->attr & IMSM_OROM_ATTR_2TB_DISK) == 0 && | |
6048 | (ldsize >> 9) >> 32 > 0) { | |
6049 | if (verbose) | |
6050 | pr_err("%s exceeds maximum platform supported size\n", dev); | |
6051 | free_imsm(super); | |
6052 | return 0; | |
6053 | } | |
6054 | } | |
6055 | ||
6056 | *freesize = avail_size_imsm(st, ldsize >> 9, data_offset); | |
6057 | free_imsm(super); | |
6058 | ||
6059 | return 1; | |
6060 | } | |
6061 | ||
6062 | static unsigned long long find_size(struct extent *e, int *idx, int num_extents) | |
6063 | { | |
6064 | const unsigned long long base_start = e[*idx].start; | |
6065 | unsigned long long end = base_start + e[*idx].size; | |
6066 | int i; | |
6067 | ||
6068 | if (base_start == end) | |
6069 | return 0; | |
6070 | ||
6071 | *idx = *idx + 1; | |
6072 | for (i = *idx; i < num_extents; i++) { | |
6073 | /* extend overlapping extents */ | |
6074 | if (e[i].start >= base_start && | |
6075 | e[i].start <= end) { | |
6076 | if (e[i].size == 0) | |
6077 | return 0; | |
6078 | if (e[i].start + e[i].size > end) | |
6079 | end = e[i].start + e[i].size; | |
6080 | } else if (e[i].start > end) { | |
6081 | *idx = i; | |
6082 | break; | |
6083 | } | |
6084 | } | |
6085 | ||
6086 | return end - base_start; | |
6087 | } | |
6088 | ||
6089 | static unsigned long long merge_extents(struct intel_super *super, int sum_extents) | |
6090 | { | |
6091 | /* build a composite disk with all known extents and generate a new | |
6092 | * 'maxsize' given the "all disks in an array must share a common start | |
6093 | * offset" constraint | |
6094 | */ | |
6095 | struct extent *e = xcalloc(sum_extents, sizeof(*e)); | |
6096 | struct dl *dl; | |
6097 | int i, j; | |
6098 | int start_extent; | |
6099 | unsigned long long pos; | |
6100 | unsigned long long start = 0; | |
6101 | unsigned long long maxsize; | |
6102 | unsigned long reserve; | |
6103 | ||
6104 | /* coalesce and sort all extents. also, check to see if we need to | |
6105 | * reserve space between member arrays | |
6106 | */ | |
6107 | j = 0; | |
6108 | for (dl = super->disks; dl; dl = dl->next) { | |
6109 | if (!dl->e) | |
6110 | continue; | |
6111 | for (i = 0; i < dl->extent_cnt; i++) | |
6112 | e[j++] = dl->e[i]; | |
6113 | } | |
6114 | qsort(e, sum_extents, sizeof(*e), cmp_extent); | |
6115 | ||
6116 | /* merge extents */ | |
6117 | i = 0; | |
6118 | j = 0; | |
6119 | while (i < sum_extents) { | |
6120 | e[j].start = e[i].start; | |
6121 | e[j].size = find_size(e, &i, sum_extents); | |
6122 | j++; | |
6123 | if (e[j-1].size == 0) | |
6124 | break; | |
6125 | } | |
6126 | ||
6127 | pos = 0; | |
6128 | maxsize = 0; | |
6129 | start_extent = 0; | |
6130 | i = 0; | |
6131 | do { | |
6132 | unsigned long long esize; | |
6133 | ||
6134 | esize = e[i].start - pos; | |
6135 | if (esize >= maxsize) { | |
6136 | maxsize = esize; | |
6137 | start = pos; | |
6138 | start_extent = i; | |
6139 | } | |
6140 | pos = e[i].start + e[i].size; | |
6141 | i++; | |
6142 | } while (e[i-1].size); | |
6143 | free(e); | |
6144 | ||
6145 | if (maxsize == 0) | |
6146 | return 0; | |
6147 | ||
6148 | /* FIXME assumes volume at offset 0 is the first volume in a | |
6149 | * container | |
6150 | */ | |
6151 | if (start_extent > 0) | |
6152 | reserve = IMSM_RESERVED_SECTORS; /* gap between raid regions */ | |
6153 | else | |
6154 | reserve = 0; | |
6155 | ||
6156 | if (maxsize < reserve) | |
6157 | return 0; | |
6158 | ||
6159 | super->create_offset = ~((unsigned long long) 0); | |
6160 | if (start + reserve > super->create_offset) | |
6161 | return 0; /* start overflows create_offset */ | |
6162 | super->create_offset = start + reserve; | |
6163 | ||
6164 | return maxsize - reserve; | |
6165 | } | |
6166 | ||
6167 | static int is_raid_level_supported(const struct imsm_orom *orom, int level, int raiddisks) | |
6168 | { | |
6169 | if (level < 0 || level == 6 || level == 4) | |
6170 | return 0; | |
6171 | ||
6172 | /* if we have an orom prevent invalid raid levels */ | |
6173 | if (orom) | |
6174 | switch (level) { | |
6175 | case 0: return imsm_orom_has_raid0(orom); | |
6176 | case 1: | |
6177 | if (raiddisks > 2) | |
6178 | return imsm_orom_has_raid1e(orom); | |
6179 | return imsm_orom_has_raid1(orom) && raiddisks == 2; | |
6180 | case 10: return imsm_orom_has_raid10(orom) && raiddisks == 4; | |
6181 | case 5: return imsm_orom_has_raid5(orom) && raiddisks > 2; | |
6182 | } | |
6183 | else | |
6184 | return 1; /* not on an Intel RAID platform so anything goes */ | |
6185 | ||
6186 | return 0; | |
6187 | } | |
6188 | ||
6189 | static int | |
6190 | active_arrays_by_format(char *name, char* hba, struct md_list **devlist, | |
6191 | int dpa, int verbose) | |
6192 | { | |
6193 | struct mdstat_ent *mdstat = mdstat_read(0, 0); | |
6194 | struct mdstat_ent *memb; | |
6195 | int count = 0; | |
6196 | int num = 0; | |
6197 | struct md_list *dv; | |
6198 | int found; | |
6199 | ||
6200 | for (memb = mdstat ; memb ; memb = memb->next) { | |
6201 | if (memb->metadata_version && | |
6202 | (strncmp(memb->metadata_version, "external:", 9) == 0) && | |
6203 | (strcmp(&memb->metadata_version[9], name) == 0) && | |
6204 | !is_subarray(memb->metadata_version+9) && | |
6205 | memb->members) { | |
6206 | struct dev_member *dev = memb->members; | |
6207 | int fd = -1; | |
6208 | while(dev && (fd < 0)) { | |
6209 | char *path = xmalloc(strlen(dev->name) + strlen("/dev/") + 1); | |
6210 | num = sprintf(path, "%s%s", "/dev/", dev->name); | |
6211 | if (num > 0) | |
6212 | fd = open(path, O_RDONLY, 0); | |
6213 | if (num <= 0 || fd < 0) { | |
6214 | pr_vrb("Cannot open %s: %s\n", | |
6215 | dev->name, strerror(errno)); | |
6216 | } | |
6217 | free(path); | |
6218 | dev = dev->next; | |
6219 | } | |
6220 | found = 0; | |
6221 | if (fd >= 0 && disk_attached_to_hba(fd, hba)) { | |
6222 | struct mdstat_ent *vol; | |
6223 | for (vol = mdstat ; vol ; vol = vol->next) { | |
6224 | if (vol->active > 0 && | |
6225 | vol->metadata_version && | |
6226 | is_container_member(vol, memb->devnm)) { | |
6227 | found++; | |
6228 | count++; | |
6229 | } | |
6230 | } | |
6231 | if (*devlist && (found < dpa)) { | |
6232 | dv = xcalloc(1, sizeof(*dv)); | |
6233 | dv->devname = xmalloc(strlen(memb->devnm) + strlen("/dev/") + 1); | |
6234 | sprintf(dv->devname, "%s%s", "/dev/", memb->devnm); | |
6235 | dv->found = found; | |
6236 | dv->used = 0; | |
6237 | dv->next = *devlist; | |
6238 | *devlist = dv; | |
6239 | } | |
6240 | } | |
6241 | if (fd >= 0) | |
6242 | close(fd); | |
6243 | } | |
6244 | } | |
6245 | free_mdstat(mdstat); | |
6246 | return count; | |
6247 | } | |
6248 | ||
6249 | #ifdef DEBUG_LOOP | |
6250 | static struct md_list* | |
6251 | get_loop_devices(void) | |
6252 | { | |
6253 | int i; | |
6254 | struct md_list *devlist = NULL; | |
6255 | struct md_list *dv; | |
6256 | ||
6257 | for(i = 0; i < 12; i++) { | |
6258 | dv = xcalloc(1, sizeof(*dv)); | |
6259 | dv->devname = xmalloc(40); | |
6260 | sprintf(dv->devname, "/dev/loop%d", i); | |
6261 | dv->next = devlist; | |
6262 | devlist = dv; | |
6263 | } | |
6264 | return devlist; | |
6265 | } | |
6266 | #endif | |
6267 | ||
6268 | static struct md_list* | |
6269 | get_devices(const char *hba_path) | |
6270 | { | |
6271 | struct md_list *devlist = NULL; | |
6272 | struct md_list *dv; | |
6273 | struct dirent *ent; | |
6274 | DIR *dir; | |
6275 | int err = 0; | |
6276 | ||
6277 | #if DEBUG_LOOP | |
6278 | devlist = get_loop_devices(); | |
6279 | return devlist; | |
6280 | #endif | |
6281 | /* scroll through /sys/dev/block looking for devices attached to | |
6282 | * this hba | |
6283 | */ | |
6284 | dir = opendir("/sys/dev/block"); | |
6285 | for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) { | |
6286 | int fd; | |
6287 | char buf[1024]; | |
6288 | int major, minor; | |
6289 | char *path = NULL; | |
6290 | if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2) | |
6291 | continue; | |
6292 | path = devt_to_devpath(makedev(major, minor)); | |
6293 | if (!path) | |
6294 | continue; | |
6295 | if (!path_attached_to_hba(path, hba_path)) { | |
6296 | free(path); | |
6297 | path = NULL; | |
6298 | continue; | |
6299 | } | |
6300 | free(path); | |
6301 | path = NULL; | |
6302 | fd = dev_open(ent->d_name, O_RDONLY); | |
6303 | if (fd >= 0) { | |
6304 | fd2devname(fd, buf); | |
6305 | close(fd); | |
6306 | } else { | |
6307 | pr_err("cannot open device: %s\n", | |
6308 | ent->d_name); | |
6309 | continue; | |
6310 | } | |
6311 | ||
6312 | dv = xcalloc(1, sizeof(*dv)); | |
6313 | dv->devname = xstrdup(buf); | |
6314 | dv->next = devlist; | |
6315 | devlist = dv; | |
6316 | } | |
6317 | if (err) { | |
6318 | while(devlist) { | |
6319 | dv = devlist; | |
6320 | devlist = devlist->next; | |
6321 | free(dv->devname); | |
6322 | free(dv); | |
6323 | } | |
6324 | } | |
6325 | closedir(dir); | |
6326 | return devlist; | |
6327 | } | |
6328 | ||
6329 | static int | |
6330 | count_volumes_list(struct md_list *devlist, char *homehost, | |
6331 | int verbose, int *found) | |
6332 | { | |
6333 | struct md_list *tmpdev; | |
6334 | int count = 0; | |
6335 | struct supertype *st; | |
6336 | ||
6337 | /* first walk the list of devices to find a consistent set | |
6338 | * that match the criterea, if that is possible. | |
6339 | * We flag the ones we like with 'used'. | |
6340 | */ | |
6341 | *found = 0; | |
6342 | st = match_metadata_desc_imsm("imsm"); | |
6343 | if (st == NULL) { | |
6344 | pr_vrb("cannot allocate memory for imsm supertype\n"); | |
6345 | return 0; | |
6346 | } | |
6347 | ||
6348 | for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) { | |
6349 | char *devname = tmpdev->devname; | |
6350 | struct stat stb; | |
6351 | struct supertype *tst; | |
6352 | int dfd; | |
6353 | if (tmpdev->used > 1) | |
6354 | continue; | |
6355 | tst = dup_super(st); | |
6356 | if (tst == NULL) { | |
6357 | pr_vrb("cannot allocate memory for imsm supertype\n"); | |
6358 | goto err_1; | |
6359 | } | |
6360 | tmpdev->container = 0; | |
6361 | dfd = dev_open(devname, O_RDONLY|O_EXCL); | |
6362 | if (dfd < 0) { | |
6363 | dprintf("cannot open device %s: %s\n", | |
6364 | devname, strerror(errno)); | |
6365 | tmpdev->used = 2; | |
6366 | } else if (fstat(dfd, &stb)< 0) { | |
6367 | /* Impossible! */ | |
6368 | dprintf("fstat failed for %s: %s\n", | |
6369 | devname, strerror(errno)); | |
6370 | tmpdev->used = 2; | |
6371 | } else if ((stb.st_mode & S_IFMT) != S_IFBLK) { | |
6372 | dprintf("%s is not a block device.\n", | |
6373 | devname); | |
6374 | tmpdev->used = 2; | |
6375 | } else if (must_be_container(dfd)) { | |
6376 | struct supertype *cst; | |
6377 | cst = super_by_fd(dfd, NULL); | |
6378 | if (cst == NULL) { | |
6379 | dprintf("cannot recognize container type %s\n", | |
6380 | devname); | |
6381 | tmpdev->used = 2; | |
6382 | } else if (tst->ss != st->ss) { | |
6383 | dprintf("non-imsm container - ignore it: %s\n", | |
6384 | devname); | |
6385 | tmpdev->used = 2; | |
6386 | } else if (!tst->ss->load_container || | |
6387 | tst->ss->load_container(tst, dfd, NULL)) | |
6388 | tmpdev->used = 2; | |
6389 | else { | |
6390 | tmpdev->container = 1; | |
6391 | } | |
6392 | if (cst) | |
6393 | cst->ss->free_super(cst); | |
6394 | } else { | |
6395 | tmpdev->st_rdev = stb.st_rdev; | |
6396 | if (tst->ss->load_super(tst,dfd, NULL)) { | |
6397 | dprintf("no RAID superblock on %s\n", | |
6398 | devname); | |
6399 | tmpdev->used = 2; | |
6400 | } else if (tst->ss->compare_super == NULL) { | |
6401 | dprintf("Cannot assemble %s metadata on %s\n", | |
6402 | tst->ss->name, devname); | |
6403 | tmpdev->used = 2; | |
6404 | } | |
6405 | } | |
6406 | if (dfd >= 0) | |
6407 | close(dfd); | |
6408 | if (tmpdev->used == 2 || tmpdev->used == 4) { | |
6409 | /* Ignore unrecognised devices during auto-assembly */ | |
6410 | goto loop; | |
6411 | } | |
6412 | else { | |
6413 | struct mdinfo info; | |
6414 | tst->ss->getinfo_super(tst, &info, NULL); | |
6415 | ||
6416 | if (st->minor_version == -1) | |
6417 | st->minor_version = tst->minor_version; | |
6418 | ||
6419 | if (memcmp(info.uuid, uuid_zero, | |
6420 | sizeof(int[4])) == 0) { | |
6421 | /* this is a floating spare. It cannot define | |
6422 | * an array unless there are no more arrays of | |
6423 | * this type to be found. It can be included | |
6424 | * in an array of this type though. | |
6425 | */ | |
6426 | tmpdev->used = 3; | |
6427 | goto loop; | |
6428 | } | |
6429 | ||
6430 | if (st->ss != tst->ss || | |
6431 | st->minor_version != tst->minor_version || | |
6432 | st->ss->compare_super(st, tst) != 0) { | |
6433 | /* Some mismatch. If exactly one array matches this host, | |
6434 | * we can resolve on that one. | |
6435 | * Or, if we are auto assembling, we just ignore the second | |
6436 | * for now. | |
6437 | */ | |
6438 | dprintf("superblock on %s doesn't match others - assembly aborted\n", | |
6439 | devname); | |
6440 | goto loop; | |
6441 | } | |
6442 | tmpdev->used = 1; | |
6443 | *found = 1; | |
6444 | dprintf("found: devname: %s\n", devname); | |
6445 | } | |
6446 | loop: | |
6447 | if (tst) | |
6448 | tst->ss->free_super(tst); | |
6449 | } | |
6450 | if (*found != 0) { | |
6451 | int err; | |
6452 | if ((err = load_super_imsm_all(st, -1, &st->sb, NULL, devlist, 0)) == 0) { | |
6453 | struct mdinfo *iter, *head = st->ss->container_content(st, NULL); | |
6454 | for (iter = head; iter; iter = iter->next) { | |
6455 | dprintf("content->text_version: %s vol\n", | |
6456 | iter->text_version); | |
6457 | if (iter->array.state & (1<<MD_SB_BLOCK_VOLUME)) { | |
6458 | /* do not assemble arrays with unsupported | |
6459 | configurations */ | |
6460 | dprintf("Cannot activate member %s.\n", | |
6461 | iter->text_version); | |
6462 | } else | |
6463 | count++; | |
6464 | } | |
6465 | sysfs_free(head); | |
6466 | ||
6467 | } else { | |
6468 | dprintf("No valid super block on device list: err: %d %p\n", | |
6469 | err, st->sb); | |
6470 | } | |
6471 | } else { | |
6472 | dprintf("no more devices to examine\n"); | |
6473 | } | |
6474 | ||
6475 | for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) { | |
6476 | if (tmpdev->used == 1 && tmpdev->found) { | |
6477 | if (count) { | |
6478 | if (count < tmpdev->found) | |
6479 | count = 0; | |
6480 | else | |
6481 | count -= tmpdev->found; | |
6482 | } | |
6483 | } | |
6484 | if (tmpdev->used == 1) | |
6485 | tmpdev->used = 4; | |
6486 | } | |
6487 | err_1: | |
6488 | if (st) | |
6489 | st->ss->free_super(st); | |
6490 | return count; | |
6491 | } | |
6492 | ||
6493 | static int __count_volumes(char *hba_path, int dpa, int verbose, | |
6494 | int cmp_hba_path) | |
6495 | { | |
6496 | struct sys_dev *idev, *intel_devices = find_intel_devices(); | |
6497 | int count = 0; | |
6498 | const struct orom_entry *entry; | |
6499 | struct devid_list *dv, *devid_list; | |
6500 | ||
6501 | if (!hba_path) | |
6502 | return 0; | |
6503 | ||
6504 | for (idev = intel_devices; idev; idev = idev->next) { | |
6505 | if (strstr(idev->path, hba_path)) | |
6506 | break; | |
6507 | } | |
6508 | ||
6509 | if (!idev || !idev->dev_id) | |
6510 | return 0; | |
6511 | ||
6512 | entry = get_orom_entry_by_device_id(idev->dev_id); | |
6513 | ||
6514 | if (!entry || !entry->devid_list) | |
6515 | return 0; | |
6516 | ||
6517 | devid_list = entry->devid_list; | |
6518 | for (dv = devid_list; dv; dv = dv->next) { | |
6519 | struct md_list *devlist; | |
6520 | struct sys_dev *device = NULL; | |
6521 | char *hpath; | |
6522 | int found = 0; | |
6523 | ||
6524 | if (cmp_hba_path) | |
6525 | device = device_by_id_and_path(dv->devid, hba_path); | |
6526 | else | |
6527 | device = device_by_id(dv->devid); | |
6528 | ||
6529 | if (device) | |
6530 | hpath = device->path; | |
6531 | else | |
6532 | return 0; | |
6533 | ||
6534 | devlist = get_devices(hpath); | |
6535 | /* if no intel devices return zero volumes */ | |
6536 | if (devlist == NULL) | |
6537 | return 0; | |
6538 | ||
6539 | count += active_arrays_by_format("imsm", hpath, &devlist, dpa, | |
6540 | verbose); | |
6541 | dprintf("path: %s active arrays: %d\n", hpath, count); | |
6542 | if (devlist == NULL) | |
6543 | return 0; | |
6544 | do { | |
6545 | found = 0; | |
6546 | count += count_volumes_list(devlist, | |
6547 | NULL, | |
6548 | verbose, | |
6549 | &found); | |
6550 | dprintf("found %d count: %d\n", found, count); | |
6551 | } while (found); | |
6552 | ||
6553 | dprintf("path: %s total number of volumes: %d\n", hpath, count); | |
6554 | ||
6555 | while (devlist) { | |
6556 | struct md_list *dv = devlist; | |
6557 | devlist = devlist->next; | |
6558 | free(dv->devname); | |
6559 | free(dv); | |
6560 | } | |
6561 | } | |
6562 | return count; | |
6563 | } | |
6564 | ||
6565 | static int count_volumes(struct intel_hba *hba, int dpa, int verbose) | |
6566 | { | |
6567 | if (!hba) | |
6568 | return 0; | |
6569 | if (hba->type == SYS_DEV_VMD) { | |
6570 | struct sys_dev *dev; | |
6571 | int count = 0; | |
6572 | ||
6573 | for (dev = find_intel_devices(); dev; dev = dev->next) { | |
6574 | if (dev->type == SYS_DEV_VMD) | |
6575 | count += __count_volumes(dev->path, dpa, | |
6576 | verbose, 1); | |
6577 | } | |
6578 | return count; | |
6579 | } | |
6580 | return __count_volumes(hba->path, dpa, verbose, 0); | |
6581 | } | |
6582 | ||
6583 | static int imsm_default_chunk(const struct imsm_orom *orom) | |
6584 | { | |
6585 | /* up to 512 if the plaform supports it, otherwise the platform max. | |
6586 | * 128 if no platform detected | |
6587 | */ | |
6588 | int fs = max(7, orom ? fls(orom->sss) : 0); | |
6589 | ||
6590 | return min(512, (1 << fs)); | |
6591 | } | |
6592 | ||
6593 | static int | |
6594 | validate_geometry_imsm_orom(struct intel_super *super, int level, int layout, | |
6595 | int raiddisks, int *chunk, unsigned long long size, int verbose) | |
6596 | { | |
6597 | /* check/set platform and metadata limits/defaults */ | |
6598 | if (super->orom && raiddisks > super->orom->dpa) { | |
6599 | pr_vrb("platform supports a maximum of %d disks per array\n", | |
6600 | super->orom->dpa); | |
6601 | return 0; | |
6602 | } | |
6603 | ||
6604 | /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */ | |
6605 | if (!is_raid_level_supported(super->orom, level, raiddisks)) { | |
6606 | pr_vrb("platform does not support raid%d with %d disk%s\n", | |
6607 | level, raiddisks, raiddisks > 1 ? "s" : ""); | |
6608 | return 0; | |
6609 | } | |
6610 | ||
6611 | if (*chunk == 0 || *chunk == UnSet) | |
6612 | *chunk = imsm_default_chunk(super->orom); | |
6613 | ||
6614 | if (super->orom && !imsm_orom_has_chunk(super->orom, *chunk)) { | |
6615 | pr_vrb("platform does not support a chunk size of: %d\n", *chunk); | |
6616 | return 0; | |
6617 | } | |
6618 | ||
6619 | if (layout != imsm_level_to_layout(level)) { | |
6620 | if (level == 5) | |
6621 | pr_vrb("imsm raid 5 only supports the left-asymmetric layout\n"); | |
6622 | else if (level == 10) | |
6623 | pr_vrb("imsm raid 10 only supports the n2 layout\n"); | |
6624 | else | |
6625 | pr_vrb("imsm unknown layout %#x for this raid level %d\n", | |
6626 | layout, level); | |
6627 | return 0; | |
6628 | } | |
6629 | ||
6630 | if (super->orom && (super->orom->attr & IMSM_OROM_ATTR_2TB) == 0 && | |
6631 | (calc_array_size(level, raiddisks, layout, *chunk, size) >> 32) > 0) { | |
6632 | pr_vrb("platform does not support a volume size over 2TB\n"); | |
6633 | return 0; | |
6634 | } | |
6635 | ||
6636 | return 1; | |
6637 | } | |
6638 | ||
6639 | /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd | |
6640 | * FIX ME add ahci details | |
6641 | */ | |
6642 | static int validate_geometry_imsm_volume(struct supertype *st, int level, | |
6643 | int layout, int raiddisks, int *chunk, | |
6644 | unsigned long long size, | |
6645 | unsigned long long data_offset, | |
6646 | char *dev, | |
6647 | unsigned long long *freesize, | |
6648 | int verbose) | |
6649 | { | |
6650 | struct stat stb; | |
6651 | struct intel_super *super = st->sb; | |
6652 | struct imsm_super *mpb; | |
6653 | struct dl *dl; | |
6654 | unsigned long long pos = 0; | |
6655 | unsigned long long maxsize; | |
6656 | struct extent *e; | |
6657 | int i; | |
6658 | ||
6659 | /* We must have the container info already read in. */ | |
6660 | if (!super) | |
6661 | return 0; | |
6662 | ||
6663 | mpb = super->anchor; | |
6664 | ||
6665 | if (!validate_geometry_imsm_orom(super, level, layout, raiddisks, chunk, size, verbose)) { | |
6666 | pr_err("RAID gemetry validation failed. Cannot proceed with the action(s).\n"); | |
6667 | return 0; | |
6668 | } | |
6669 | if (!dev) { | |
6670 | /* General test: make sure there is space for | |
6671 | * 'raiddisks' device extents of size 'size' at a given | |
6672 | * offset | |
6673 | */ | |
6674 | unsigned long long minsize = size; | |
6675 | unsigned long long start_offset = MaxSector; | |
6676 | int dcnt = 0; | |
6677 | if (minsize == 0) | |
6678 | minsize = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS; | |
6679 | for (dl = super->disks; dl ; dl = dl->next) { | |
6680 | int found = 0; | |
6681 | ||
6682 | pos = 0; | |
6683 | i = 0; | |
6684 | e = get_extents(super, dl); | |
6685 | if (!e) continue; | |
6686 | do { | |
6687 | unsigned long long esize; | |
6688 | esize = e[i].start - pos; | |
6689 | if (esize >= minsize) | |
6690 | found = 1; | |
6691 | if (found && start_offset == MaxSector) { | |
6692 | start_offset = pos; | |
6693 | break; | |
6694 | } else if (found && pos != start_offset) { | |
6695 | found = 0; | |
6696 | break; | |
6697 | } | |
6698 | pos = e[i].start + e[i].size; | |
6699 | i++; | |
6700 | } while (e[i-1].size); | |
6701 | if (found) | |
6702 | dcnt++; | |
6703 | free(e); | |
6704 | } | |
6705 | if (dcnt < raiddisks) { | |
6706 | if (verbose) | |
6707 | pr_err("imsm: Not enough devices with space for this array (%d < %d)\n", | |
6708 | dcnt, raiddisks); | |
6709 | return 0; | |
6710 | } | |
6711 | return 1; | |
6712 | } | |
6713 | ||
6714 | /* This device must be a member of the set */ | |
6715 | if (stat(dev, &stb) < 0) | |
6716 | return 0; | |
6717 | if ((S_IFMT & stb.st_mode) != S_IFBLK) | |
6718 | return 0; | |
6719 | for (dl = super->disks ; dl ; dl = dl->next) { | |
6720 | if (dl->major == (int)major(stb.st_rdev) && | |
6721 | dl->minor == (int)minor(stb.st_rdev)) | |
6722 | break; | |
6723 | } | |
6724 | if (!dl) { | |
6725 | if (verbose) | |
6726 | pr_err("%s is not in the same imsm set\n", dev); | |
6727 | return 0; | |
6728 | } else if (super->orom && dl->index < 0 && mpb->num_raid_devs) { | |
6729 | /* If a volume is present then the current creation attempt | |
6730 | * cannot incorporate new spares because the orom may not | |
6731 | * understand this configuration (all member disks must be | |
6732 | * members of each array in the container). | |
6733 | */ | |
6734 | pr_err("%s is a spare and a volume is already defined for this container\n", dev); | |
6735 | pr_err("The option-rom requires all member disks to be a member of all volumes\n"); | |
6736 | return 0; | |
6737 | } else if (super->orom && mpb->num_raid_devs > 0 && | |
6738 | mpb->num_disks != raiddisks) { | |
6739 | pr_err("The option-rom requires all member disks to be a member of all volumes\n"); | |
6740 | return 0; | |
6741 | } | |
6742 | ||
6743 | /* retrieve the largest free space block */ | |
6744 | e = get_extents(super, dl); | |
6745 | maxsize = 0; | |
6746 | i = 0; | |
6747 | if (e) { | |
6748 | do { | |
6749 | unsigned long long esize; | |
6750 | ||
6751 | esize = e[i].start - pos; | |
6752 | if (esize >= maxsize) | |
6753 | maxsize = esize; | |
6754 | pos = e[i].start + e[i].size; | |
6755 | i++; | |
6756 | } while (e[i-1].size); | |
6757 | dl->e = e; | |
6758 | dl->extent_cnt = i; | |
6759 | } else { | |
6760 | if (verbose) | |
6761 | pr_err("unable to determine free space for: %s\n", | |
6762 | dev); | |
6763 | return 0; | |
6764 | } | |
6765 | if (maxsize < size) { | |
6766 | if (verbose) | |
6767 | pr_err("%s not enough space (%llu < %llu)\n", | |
6768 | dev, maxsize, size); | |
6769 | return 0; | |
6770 | } | |
6771 | ||
6772 | /* count total number of extents for merge */ | |
6773 | i = 0; | |
6774 | for (dl = super->disks; dl; dl = dl->next) | |
6775 | if (dl->e) | |
6776 | i += dl->extent_cnt; | |
6777 | ||
6778 | maxsize = merge_extents(super, i); | |
6779 | ||
6780 | if (!check_env("IMSM_NO_PLATFORM") && | |
6781 | mpb->num_raid_devs > 0 && size && size != maxsize) { | |
6782 | pr_err("attempting to create a second volume with size less then remaining space. Aborting...\n"); | |
6783 | return 0; | |
6784 | } | |
6785 | ||
6786 | if (maxsize < size || maxsize == 0) { | |
6787 | if (verbose) { | |
6788 | if (maxsize == 0) | |
6789 | pr_err("no free space left on device. Aborting...\n"); | |
6790 | else | |
6791 | pr_err("not enough space to create volume of given size (%llu < %llu). Aborting...\n", | |
6792 | maxsize, size); | |
6793 | } | |
6794 | return 0; | |
6795 | } | |
6796 | ||
6797 | *freesize = maxsize; | |
6798 | ||
6799 | if (super->orom) { | |
6800 | int count = count_volumes(super->hba, | |
6801 | super->orom->dpa, verbose); | |
6802 | if (super->orom->vphba <= count) { | |
6803 | pr_vrb("platform does not support more than %d raid volumes.\n", | |
6804 | super->orom->vphba); | |
6805 | return 0; | |
6806 | } | |
6807 | } | |
6808 | return 1; | |
6809 | } | |
6810 | ||
6811 | static int imsm_get_free_size(struct supertype *st, int raiddisks, | |
6812 | unsigned long long size, int chunk, | |
6813 | unsigned long long *freesize) | |
6814 | { | |
6815 | struct intel_super *super = st->sb; | |
6816 | struct imsm_super *mpb = super->anchor; | |
6817 | struct dl *dl; | |
6818 | int i; | |
6819 | int extent_cnt; | |
6820 | struct extent *e; | |
6821 | unsigned long long maxsize; | |
6822 | unsigned long long minsize; | |
6823 | int cnt; | |
6824 | int used; | |
6825 | ||
6826 | /* find the largest common start free region of the possible disks */ | |
6827 | used = 0; | |
6828 | extent_cnt = 0; | |
6829 | cnt = 0; | |
6830 | for (dl = super->disks; dl; dl = dl->next) { | |
6831 | dl->raiddisk = -1; | |
6832 | ||
6833 | if (dl->index >= 0) | |
6834 | used++; | |
6835 | ||
6836 | /* don't activate new spares if we are orom constrained | |
6837 | * and there is already a volume active in the container | |
6838 | */ | |
6839 | if (super->orom && dl->index < 0 && mpb->num_raid_devs) | |
6840 | continue; | |
6841 | ||
6842 | e = get_extents(super, dl); | |
6843 | if (!e) | |
6844 | continue; | |
6845 | for (i = 1; e[i-1].size; i++) | |
6846 | ; | |
6847 | dl->e = e; | |
6848 | dl->extent_cnt = i; | |
6849 | extent_cnt += i; | |
6850 | cnt++; | |
6851 | } | |
6852 | ||
6853 | maxsize = merge_extents(super, extent_cnt); | |
6854 | minsize = size; | |
6855 | if (size == 0) | |
6856 | /* chunk is in K */ | |
6857 | minsize = chunk * 2; | |
6858 | ||
6859 | if (cnt < raiddisks || | |
6860 | (super->orom && used && used != raiddisks) || | |
6861 | maxsize < minsize || | |
6862 | maxsize == 0) { | |
6863 | pr_err("not enough devices with space to create array.\n"); | |
6864 | return 0; /* No enough free spaces large enough */ | |
6865 | } | |
6866 | ||
6867 | if (size == 0) { | |
6868 | size = maxsize; | |
6869 | if (chunk) { | |
6870 | size /= 2 * chunk; | |
6871 | size *= 2 * chunk; | |
6872 | } | |
6873 | maxsize = size; | |
6874 | } | |
6875 | if (!check_env("IMSM_NO_PLATFORM") && | |
6876 | mpb->num_raid_devs > 0 && size && size != maxsize) { | |
6877 | pr_err("attempting to create a second volume with size less then remaining space. Aborting...\n"); | |
6878 | return 0; | |
6879 | } | |
6880 | cnt = 0; | |
6881 | for (dl = super->disks; dl; dl = dl->next) | |
6882 | if (dl->e) | |
6883 | dl->raiddisk = cnt++; | |
6884 | ||
6885 | *freesize = size; | |
6886 | ||
6887 | dprintf("imsm: imsm_get_free_size() returns : %llu\n", size); | |
6888 | ||
6889 | return 1; | |
6890 | } | |
6891 | ||
6892 | static int reserve_space(struct supertype *st, int raiddisks, | |
6893 | unsigned long long size, int chunk, | |
6894 | unsigned long long *freesize) | |
6895 | { | |
6896 | struct intel_super *super = st->sb; | |
6897 | struct dl *dl; | |
6898 | int cnt; | |
6899 | int rv = 0; | |
6900 | ||
6901 | rv = imsm_get_free_size(st, raiddisks, size, chunk, freesize); | |
6902 | if (rv) { | |
6903 | cnt = 0; | |
6904 | for (dl = super->disks; dl; dl = dl->next) | |
6905 | if (dl->e) | |
6906 | dl->raiddisk = cnt++; | |
6907 | rv = 1; | |
6908 | } | |
6909 | ||
6910 | return rv; | |
6911 | } | |
6912 | ||
6913 | static int validate_geometry_imsm(struct supertype *st, int level, int layout, | |
6914 | int raiddisks, int *chunk, unsigned long long size, | |
6915 | unsigned long long data_offset, | |
6916 | char *dev, unsigned long long *freesize, | |
6917 | int consistency_policy, int verbose) | |
6918 | { | |
6919 | int fd, cfd; | |
6920 | struct mdinfo *sra; | |
6921 | int is_member = 0; | |
6922 | ||
6923 | /* load capability | |
6924 | * if given unused devices create a container | |
6925 | * if given given devices in a container create a member volume | |
6926 | */ | |
6927 | if (level == LEVEL_CONTAINER) { | |
6928 | /* Must be a fresh device to add to a container */ | |
6929 | return validate_geometry_imsm_container(st, level, layout, | |
6930 | raiddisks, | |
6931 | *chunk, | |
6932 | size, data_offset, | |
6933 | dev, freesize, | |
6934 | verbose); | |
6935 | } | |
6936 | ||
6937 | if (!dev) { | |
6938 | if (st->sb) { | |
6939 | struct intel_super *super = st->sb; | |
6940 | if (!validate_geometry_imsm_orom(st->sb, level, layout, | |
6941 | raiddisks, chunk, size, | |
6942 | verbose)) | |
6943 | return 0; | |
6944 | /* we are being asked to automatically layout a | |
6945 | * new volume based on the current contents of | |
6946 | * the container. If the the parameters can be | |
6947 | * satisfied reserve_space will record the disks, | |
6948 | * start offset, and size of the volume to be | |
6949 | * created. add_to_super and getinfo_super | |
6950 | * detect when autolayout is in progress. | |
6951 | */ | |
6952 | /* assuming that freesize is always given when array is | |
6953 | created */ | |
6954 | if (super->orom && freesize) { | |
6955 | int count; | |
6956 | count = count_volumes(super->hba, | |
6957 | super->orom->dpa, verbose); | |
6958 | if (super->orom->vphba <= count) { | |
6959 | pr_vrb("platform does not support more than %d raid volumes.\n", | |
6960 | super->orom->vphba); | |
6961 | return 0; | |
6962 | } | |
6963 | } | |
6964 | if (freesize) | |
6965 | return reserve_space(st, raiddisks, size, | |
6966 | *chunk, freesize); | |
6967 | } | |
6968 | return 1; | |
6969 | } | |
6970 | if (st->sb) { | |
6971 | /* creating in a given container */ | |
6972 | return validate_geometry_imsm_volume(st, level, layout, | |
6973 | raiddisks, chunk, size, | |
6974 | data_offset, | |
6975 | dev, freesize, verbose); | |
6976 | } | |
6977 | ||
6978 | /* This device needs to be a device in an 'imsm' container */ | |
6979 | fd = open(dev, O_RDONLY|O_EXCL, 0); | |
6980 | if (fd >= 0) { | |
6981 | if (verbose) | |
6982 | pr_err("Cannot create this array on device %s\n", | |
6983 | dev); | |
6984 | close(fd); | |
6985 | return 0; | |
6986 | } | |
6987 | if (errno != EBUSY || (fd = open(dev, O_RDONLY, 0)) < 0) { | |
6988 | if (verbose) | |
6989 | pr_err("Cannot open %s: %s\n", | |
6990 | dev, strerror(errno)); | |
6991 | return 0; | |
6992 | } | |
6993 | /* Well, it is in use by someone, maybe an 'imsm' container. */ | |
6994 | cfd = open_container(fd); | |
6995 | close(fd); | |
6996 | if (cfd < 0) { | |
6997 | if (verbose) | |
6998 | pr_err("Cannot use %s: It is busy\n", | |
6999 | dev); | |
7000 | return 0; | |
7001 | } | |
7002 | sra = sysfs_read(cfd, NULL, GET_VERSION); | |
7003 | if (sra && sra->array.major_version == -1 && | |
7004 | strcmp(sra->text_version, "imsm") == 0) | |
7005 | is_member = 1; | |
7006 | sysfs_free(sra); | |
7007 | if (is_member) { | |
7008 | /* This is a member of a imsm container. Load the container | |
7009 | * and try to create a volume | |
7010 | */ | |
7011 | struct intel_super *super; | |
7012 | ||
7013 | if (load_super_imsm_all(st, cfd, (void **) &super, NULL, NULL, 1) == 0) { | |
7014 | st->sb = super; | |
7015 | strcpy(st->container_devnm, fd2devnm(cfd)); | |
7016 | close(cfd); | |
7017 | return validate_geometry_imsm_volume(st, level, layout, | |
7018 | raiddisks, chunk, | |
7019 | size, data_offset, dev, | |
7020 | freesize, 1) | |
7021 | ? 1 : -1; | |
7022 | } | |
7023 | } | |
7024 | ||
7025 | if (verbose) | |
7026 | pr_err("failed container membership check\n"); | |
7027 | ||
7028 | close(cfd); | |
7029 | return 0; | |
7030 | } | |
7031 | ||
7032 | static void default_geometry_imsm(struct supertype *st, int *level, int *layout, int *chunk) | |
7033 | { | |
7034 | struct intel_super *super = st->sb; | |
7035 | ||
7036 | if (level && *level == UnSet) | |
7037 | *level = LEVEL_CONTAINER; | |
7038 | ||
7039 | if (level && layout && *layout == UnSet) | |
7040 | *layout = imsm_level_to_layout(*level); | |
7041 | ||
7042 | if (chunk && (*chunk == UnSet || *chunk == 0)) | |
7043 | *chunk = imsm_default_chunk(super->orom); | |
7044 | } | |
7045 | ||
7046 | static void handle_missing(struct intel_super *super, struct imsm_dev *dev); | |
7047 | ||
7048 | static int kill_subarray_imsm(struct supertype *st) | |
7049 | { | |
7050 | /* remove the subarray currently referenced by ->current_vol */ | |
7051 | __u8 i; | |
7052 | struct intel_dev **dp; | |
7053 | struct intel_super *super = st->sb; | |
7054 | __u8 current_vol = super->current_vol; | |
7055 | struct imsm_super *mpb = super->anchor; | |
7056 | ||
7057 | if (super->current_vol < 0) | |
7058 | return 2; | |
7059 | super->current_vol = -1; /* invalidate subarray cursor */ | |
7060 | ||
7061 | /* block deletions that would change the uuid of active subarrays | |
7062 | * | |
7063 | * FIXME when immutable ids are available, but note that we'll | |
7064 | * also need to fixup the invalidated/active subarray indexes in | |
7065 | * mdstat | |
7066 | */ | |
7067 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
7068 | char subarray[4]; | |
7069 | ||
7070 | if (i < current_vol) | |
7071 | continue; | |
7072 | sprintf(subarray, "%u", i); | |
7073 | if (is_subarray_active(subarray, st->devnm)) { | |
7074 | pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n", | |
7075 | current_vol, i); | |
7076 | ||
7077 | return 2; | |
7078 | } | |
7079 | } | |
7080 | ||
7081 | if (st->update_tail) { | |
7082 | struct imsm_update_kill_array *u = xmalloc(sizeof(*u)); | |
7083 | ||
7084 | u->type = update_kill_array; | |
7085 | u->dev_idx = current_vol; | |
7086 | append_metadata_update(st, u, sizeof(*u)); | |
7087 | ||
7088 | return 0; | |
7089 | } | |
7090 | ||
7091 | for (dp = &super->devlist; *dp;) | |
7092 | if ((*dp)->index == current_vol) { | |
7093 | *dp = (*dp)->next; | |
7094 | } else { | |
7095 | handle_missing(super, (*dp)->dev); | |
7096 | if ((*dp)->index > current_vol) | |
7097 | (*dp)->index--; | |
7098 | dp = &(*dp)->next; | |
7099 | } | |
7100 | ||
7101 | /* no more raid devices, all active components are now spares, | |
7102 | * but of course failed are still failed | |
7103 | */ | |
7104 | if (--mpb->num_raid_devs == 0) { | |
7105 | struct dl *d; | |
7106 | ||
7107 | for (d = super->disks; d; d = d->next) | |
7108 | if (d->index > -2) | |
7109 | mark_spare(d); | |
7110 | } | |
7111 | ||
7112 | super->updates_pending++; | |
7113 | ||
7114 | return 0; | |
7115 | } | |
7116 | ||
7117 | static int update_subarray_imsm(struct supertype *st, char *subarray, | |
7118 | char *update, struct mddev_ident *ident) | |
7119 | { | |
7120 | /* update the subarray currently referenced by ->current_vol */ | |
7121 | struct intel_super *super = st->sb; | |
7122 | struct imsm_super *mpb = super->anchor; | |
7123 | ||
7124 | if (strcmp(update, "name") == 0) { | |
7125 | char *name = ident->name; | |
7126 | char *ep; | |
7127 | int vol; | |
7128 | ||
7129 | if (is_subarray_active(subarray, st->devnm)) { | |
7130 | pr_err("Unable to update name of active subarray\n"); | |
7131 | return 2; | |
7132 | } | |
7133 | ||
7134 | if (!check_name(super, name, 0)) | |
7135 | return 2; | |
7136 | ||
7137 | vol = strtoul(subarray, &ep, 10); | |
7138 | if (*ep != '\0' || vol >= super->anchor->num_raid_devs) | |
7139 | return 2; | |
7140 | ||
7141 | if (st->update_tail) { | |
7142 | struct imsm_update_rename_array *u = xmalloc(sizeof(*u)); | |
7143 | ||
7144 | u->type = update_rename_array; | |
7145 | u->dev_idx = vol; | |
7146 | strncpy((char *) u->name, name, MAX_RAID_SERIAL_LEN); | |
7147 | u->name[MAX_RAID_SERIAL_LEN-1] = '\0'; | |
7148 | append_metadata_update(st, u, sizeof(*u)); | |
7149 | } else { | |
7150 | struct imsm_dev *dev; | |
7151 | int i; | |
7152 | ||
7153 | dev = get_imsm_dev(super, vol); | |
7154 | strncpy((char *) dev->volume, name, MAX_RAID_SERIAL_LEN); | |
7155 | dev->volume[MAX_RAID_SERIAL_LEN-1] = '\0'; | |
7156 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
7157 | dev = get_imsm_dev(super, i); | |
7158 | handle_missing(super, dev); | |
7159 | } | |
7160 | super->updates_pending++; | |
7161 | } | |
7162 | } else | |
7163 | return 2; | |
7164 | ||
7165 | return 0; | |
7166 | } | |
7167 | #endif /* MDASSEMBLE */ | |
7168 | ||
7169 | static int is_gen_migration(struct imsm_dev *dev) | |
7170 | { | |
7171 | if (dev == NULL) | |
7172 | return 0; | |
7173 | ||
7174 | if (!dev->vol.migr_state) | |
7175 | return 0; | |
7176 | ||
7177 | if (migr_type(dev) == MIGR_GEN_MIGR) | |
7178 | return 1; | |
7179 | ||
7180 | return 0; | |
7181 | } | |
7182 | ||
7183 | static int is_rebuilding(struct imsm_dev *dev) | |
7184 | { | |
7185 | struct imsm_map *migr_map; | |
7186 | ||
7187 | if (!dev->vol.migr_state) | |
7188 | return 0; | |
7189 | ||
7190 | if (migr_type(dev) != MIGR_REBUILD) | |
7191 | return 0; | |
7192 | ||
7193 | migr_map = get_imsm_map(dev, MAP_1); | |
7194 | ||
7195 | if (migr_map->map_state == IMSM_T_STATE_DEGRADED) | |
7196 | return 1; | |
7197 | else | |
7198 | return 0; | |
7199 | } | |
7200 | ||
7201 | #ifndef MDASSEMBLE | |
7202 | static int is_initializing(struct imsm_dev *dev) | |
7203 | { | |
7204 | struct imsm_map *migr_map; | |
7205 | ||
7206 | if (!dev->vol.migr_state) | |
7207 | return 0; | |
7208 | ||
7209 | if (migr_type(dev) != MIGR_INIT) | |
7210 | return 0; | |
7211 | ||
7212 | migr_map = get_imsm_map(dev, MAP_1); | |
7213 | ||
7214 | if (migr_map->map_state == IMSM_T_STATE_UNINITIALIZED) | |
7215 | return 1; | |
7216 | ||
7217 | return 0; | |
7218 | } | |
7219 | #endif | |
7220 | ||
7221 | static void update_recovery_start(struct intel_super *super, | |
7222 | struct imsm_dev *dev, | |
7223 | struct mdinfo *array) | |
7224 | { | |
7225 | struct mdinfo *rebuild = NULL; | |
7226 | struct mdinfo *d; | |
7227 | __u32 units; | |
7228 | ||
7229 | if (!is_rebuilding(dev)) | |
7230 | return; | |
7231 | ||
7232 | /* Find the rebuild target, but punt on the dual rebuild case */ | |
7233 | for (d = array->devs; d; d = d->next) | |
7234 | if (d->recovery_start == 0) { | |
7235 | if (rebuild) | |
7236 | return; | |
7237 | rebuild = d; | |
7238 | } | |
7239 | ||
7240 | if (!rebuild) { | |
7241 | /* (?) none of the disks are marked with | |
7242 | * IMSM_ORD_REBUILD, so assume they are missing and the | |
7243 | * disk_ord_tbl was not correctly updated | |
7244 | */ | |
7245 | dprintf("failed to locate out-of-sync disk\n"); | |
7246 | return; | |
7247 | } | |
7248 | ||
7249 | units = __le32_to_cpu(dev->vol.curr_migr_unit); | |
7250 | rebuild->recovery_start = units * blocks_per_migr_unit(super, dev); | |
7251 | } | |
7252 | ||
7253 | #ifndef MDASSEMBLE | |
7254 | static int recover_backup_imsm(struct supertype *st, struct mdinfo *info); | |
7255 | #endif | |
7256 | ||
7257 | static struct mdinfo *container_content_imsm(struct supertype *st, char *subarray) | |
7258 | { | |
7259 | /* Given a container loaded by load_super_imsm_all, | |
7260 | * extract information about all the arrays into | |
7261 | * an mdinfo tree. | |
7262 | * If 'subarray' is given, just extract info about that array. | |
7263 | * | |
7264 | * For each imsm_dev create an mdinfo, fill it in, | |
7265 | * then look for matching devices in super->disks | |
7266 | * and create appropriate device mdinfo. | |
7267 | */ | |
7268 | struct intel_super *super = st->sb; | |
7269 | struct imsm_super *mpb = super->anchor; | |
7270 | struct mdinfo *rest = NULL; | |
7271 | unsigned int i; | |
7272 | int sb_errors = 0; | |
7273 | struct dl *d; | |
7274 | int spare_disks = 0; | |
7275 | ||
7276 | /* do not assemble arrays when not all attributes are supported */ | |
7277 | if (imsm_check_attributes(mpb->attributes) == 0) { | |
7278 | sb_errors = 1; | |
7279 | pr_err("Unsupported attributes in IMSM metadata.Arrays activation is blocked.\n"); | |
7280 | } | |
7281 | ||
7282 | /* count spare devices, not used in maps | |
7283 | */ | |
7284 | for (d = super->disks; d; d = d->next) | |
7285 | if (d->index == -1) | |
7286 | spare_disks++; | |
7287 | ||
7288 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
7289 | struct imsm_dev *dev; | |
7290 | struct imsm_map *map; | |
7291 | struct imsm_map *map2; | |
7292 | struct mdinfo *this; | |
7293 | int slot; | |
7294 | #ifndef MDASSEMBLE | |
7295 | int chunk; | |
7296 | #endif | |
7297 | char *ep; | |
7298 | ||
7299 | if (subarray && | |
7300 | (i != strtoul(subarray, &ep, 10) || *ep != '\0')) | |
7301 | continue; | |
7302 | ||
7303 | dev = get_imsm_dev(super, i); | |
7304 | map = get_imsm_map(dev, MAP_0); | |
7305 | map2 = get_imsm_map(dev, MAP_1); | |
7306 | ||
7307 | /* do not publish arrays that are in the middle of an | |
7308 | * unsupported migration | |
7309 | */ | |
7310 | if (dev->vol.migr_state && | |
7311 | (migr_type(dev) == MIGR_STATE_CHANGE)) { | |
7312 | pr_err("cannot assemble volume '%.16s': unsupported migration in progress\n", | |
7313 | dev->volume); | |
7314 | continue; | |
7315 | } | |
7316 | /* do not publish arrays that are not support by controller's | |
7317 | * OROM/EFI | |
7318 | */ | |
7319 | ||
7320 | this = xmalloc(sizeof(*this)); | |
7321 | ||
7322 | super->current_vol = i; | |
7323 | getinfo_super_imsm_volume(st, this, NULL); | |
7324 | this->next = rest; | |
7325 | #ifndef MDASSEMBLE | |
7326 | chunk = __le16_to_cpu(map->blocks_per_strip) >> 1; | |
7327 | /* mdadm does not support all metadata features- set the bit in all arrays state */ | |
7328 | if (!validate_geometry_imsm_orom(super, | |
7329 | get_imsm_raid_level(map), /* RAID level */ | |
7330 | imsm_level_to_layout(get_imsm_raid_level(map)), | |
7331 | map->num_members, /* raid disks */ | |
7332 | &chunk, join_u32(dev->size_low, dev->size_high), | |
7333 | 1 /* verbose */)) { | |
7334 | pr_err("IMSM RAID geometry validation failed. Array %s activation is blocked.\n", | |
7335 | dev->volume); | |
7336 | this->array.state |= | |
7337 | (1<<MD_SB_BLOCK_CONTAINER_RESHAPE) | | |
7338 | (1<<MD_SB_BLOCK_VOLUME); | |
7339 | } | |
7340 | #endif | |
7341 | ||
7342 | /* if array has bad blocks, set suitable bit in all arrays state */ | |
7343 | if (sb_errors) | |
7344 | this->array.state |= | |
7345 | (1<<MD_SB_BLOCK_CONTAINER_RESHAPE) | | |
7346 | (1<<MD_SB_BLOCK_VOLUME); | |
7347 | ||
7348 | for (slot = 0 ; slot < map->num_members; slot++) { | |
7349 | unsigned long long recovery_start; | |
7350 | struct mdinfo *info_d; | |
7351 | struct dl *d; | |
7352 | int idx; | |
7353 | int skip; | |
7354 | __u32 ord; | |
7355 | ||
7356 | skip = 0; | |
7357 | idx = get_imsm_disk_idx(dev, slot, MAP_0); | |
7358 | ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X); | |
7359 | for (d = super->disks; d ; d = d->next) | |
7360 | if (d->index == idx) | |
7361 | break; | |
7362 | ||
7363 | recovery_start = MaxSector; | |
7364 | if (d == NULL) | |
7365 | skip = 1; | |
7366 | if (d && is_failed(&d->disk)) | |
7367 | skip = 1; | |
7368 | if (ord & IMSM_ORD_REBUILD) | |
7369 | recovery_start = 0; | |
7370 | ||
7371 | /* | |
7372 | * if we skip some disks the array will be assmebled degraded; | |
7373 | * reset resync start to avoid a dirty-degraded | |
7374 | * situation when performing the intial sync | |
7375 | * | |
7376 | * FIXME handle dirty degraded | |
7377 | */ | |
7378 | if ((skip || recovery_start == 0) && !dev->vol.dirty) | |
7379 | this->resync_start = MaxSector; | |
7380 | if (skip) | |
7381 | continue; | |
7382 | ||
7383 | info_d = xcalloc(1, sizeof(*info_d)); | |
7384 | info_d->next = this->devs; | |
7385 | this->devs = info_d; | |
7386 | ||
7387 | info_d->disk.number = d->index; | |
7388 | info_d->disk.major = d->major; | |
7389 | info_d->disk.minor = d->minor; | |
7390 | info_d->disk.raid_disk = slot; | |
7391 | info_d->recovery_start = recovery_start; | |
7392 | if (map2) { | |
7393 | if (slot < map2->num_members) | |
7394 | info_d->disk.state = (1 << MD_DISK_ACTIVE); | |
7395 | else | |
7396 | this->array.spare_disks++; | |
7397 | } else { | |
7398 | if (slot < map->num_members) | |
7399 | info_d->disk.state = (1 << MD_DISK_ACTIVE); | |
7400 | else | |
7401 | this->array.spare_disks++; | |
7402 | } | |
7403 | if (info_d->recovery_start == MaxSector) | |
7404 | this->array.working_disks++; | |
7405 | ||
7406 | info_d->events = __le32_to_cpu(mpb->generation_num); | |
7407 | info_d->data_offset = pba_of_lba0(map); | |
7408 | ||
7409 | if (map->raid_level == 5) { | |
7410 | info_d->component_size = | |
7411 | num_data_stripes(map) * | |
7412 | map->blocks_per_strip; | |
7413 | } else { | |
7414 | info_d->component_size = blocks_per_member(map); | |
7415 | } | |
7416 | ||
7417 | info_d->bb.supported = 1; | |
7418 | get_volume_badblocks(super->bbm_log, ord_to_idx(ord), | |
7419 | info_d->data_offset, | |
7420 | info_d->component_size, | |
7421 | &info_d->bb); | |
7422 | } | |
7423 | /* now that the disk list is up-to-date fixup recovery_start */ | |
7424 | update_recovery_start(super, dev, this); | |
7425 | this->array.spare_disks += spare_disks; | |
7426 | ||
7427 | #ifndef MDASSEMBLE | |
7428 | /* check for reshape */ | |
7429 | if (this->reshape_active == 1) | |
7430 | recover_backup_imsm(st, this); | |
7431 | #endif | |
7432 | rest = this; | |
7433 | } | |
7434 | ||
7435 | return rest; | |
7436 | } | |
7437 | ||
7438 | static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev, | |
7439 | int failed, int look_in_map) | |
7440 | { | |
7441 | struct imsm_map *map; | |
7442 | ||
7443 | map = get_imsm_map(dev, look_in_map); | |
7444 | ||
7445 | if (!failed) | |
7446 | return map->map_state == IMSM_T_STATE_UNINITIALIZED ? | |
7447 | IMSM_T_STATE_UNINITIALIZED : IMSM_T_STATE_NORMAL; | |
7448 | ||
7449 | switch (get_imsm_raid_level(map)) { | |
7450 | case 0: | |
7451 | return IMSM_T_STATE_FAILED; | |
7452 | break; | |
7453 | case 1: | |
7454 | if (failed < map->num_members) | |
7455 | return IMSM_T_STATE_DEGRADED; | |
7456 | else | |
7457 | return IMSM_T_STATE_FAILED; | |
7458 | break; | |
7459 | case 10: | |
7460 | { | |
7461 | /** | |
7462 | * check to see if any mirrors have failed, otherwise we | |
7463 | * are degraded. Even numbered slots are mirrored on | |
7464 | * slot+1 | |
7465 | */ | |
7466 | int i; | |
7467 | /* gcc -Os complains that this is unused */ | |
7468 | int insync = insync; | |
7469 | ||
7470 | for (i = 0; i < map->num_members; i++) { | |
7471 | __u32 ord = get_imsm_ord_tbl_ent(dev, i, MAP_X); | |
7472 | int idx = ord_to_idx(ord); | |
7473 | struct imsm_disk *disk; | |
7474 | ||
7475 | /* reset the potential in-sync count on even-numbered | |
7476 | * slots. num_copies is always 2 for imsm raid10 | |
7477 | */ | |
7478 | if ((i & 1) == 0) | |
7479 | insync = 2; | |
7480 | ||
7481 | disk = get_imsm_disk(super, idx); | |
7482 | if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD) | |
7483 | insync--; | |
7484 | ||
7485 | /* no in-sync disks left in this mirror the | |
7486 | * array has failed | |
7487 | */ | |
7488 | if (insync == 0) | |
7489 | return IMSM_T_STATE_FAILED; | |
7490 | } | |
7491 | ||
7492 | return IMSM_T_STATE_DEGRADED; | |
7493 | } | |
7494 | case 5: | |
7495 | if (failed < 2) | |
7496 | return IMSM_T_STATE_DEGRADED; | |
7497 | else | |
7498 | return IMSM_T_STATE_FAILED; | |
7499 | break; | |
7500 | default: | |
7501 | break; | |
7502 | } | |
7503 | ||
7504 | return map->map_state; | |
7505 | } | |
7506 | ||
7507 | static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev, | |
7508 | int look_in_map) | |
7509 | { | |
7510 | int i; | |
7511 | int failed = 0; | |
7512 | struct imsm_disk *disk; | |
7513 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
7514 | struct imsm_map *prev = get_imsm_map(dev, MAP_1); | |
7515 | struct imsm_map *map_for_loop; | |
7516 | __u32 ord; | |
7517 | int idx; | |
7518 | int idx_1; | |
7519 | ||
7520 | /* at the beginning of migration we set IMSM_ORD_REBUILD on | |
7521 | * disks that are being rebuilt. New failures are recorded to | |
7522 | * map[0]. So we look through all the disks we started with and | |
7523 | * see if any failures are still present, or if any new ones | |
7524 | * have arrived | |
7525 | */ | |
7526 | map_for_loop = map; | |
7527 | if (prev && (map->num_members < prev->num_members)) | |
7528 | map_for_loop = prev; | |
7529 | ||
7530 | for (i = 0; i < map_for_loop->num_members; i++) { | |
7531 | idx_1 = -255; | |
7532 | /* when MAP_X is passed both maps failures are counted | |
7533 | */ | |
7534 | if (prev && | |
7535 | (look_in_map == MAP_1 || look_in_map == MAP_X) && | |
7536 | i < prev->num_members) { | |
7537 | ord = __le32_to_cpu(prev->disk_ord_tbl[i]); | |
7538 | idx_1 = ord_to_idx(ord); | |
7539 | ||
7540 | disk = get_imsm_disk(super, idx_1); | |
7541 | if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD) | |
7542 | failed++; | |
7543 | } | |
7544 | if ((look_in_map == MAP_0 || look_in_map == MAP_X) && | |
7545 | i < map->num_members) { | |
7546 | ord = __le32_to_cpu(map->disk_ord_tbl[i]); | |
7547 | idx = ord_to_idx(ord); | |
7548 | ||
7549 | if (idx != idx_1) { | |
7550 | disk = get_imsm_disk(super, idx); | |
7551 | if (!disk || is_failed(disk) || | |
7552 | ord & IMSM_ORD_REBUILD) | |
7553 | failed++; | |
7554 | } | |
7555 | } | |
7556 | } | |
7557 | ||
7558 | return failed; | |
7559 | } | |
7560 | ||
7561 | #ifndef MDASSEMBLE | |
7562 | static int imsm_open_new(struct supertype *c, struct active_array *a, | |
7563 | char *inst) | |
7564 | { | |
7565 | struct intel_super *super = c->sb; | |
7566 | struct imsm_super *mpb = super->anchor; | |
7567 | struct imsm_update_prealloc_bb_mem u; | |
7568 | ||
7569 | if (atoi(inst) >= mpb->num_raid_devs) { | |
7570 | pr_err("subarry index %d, out of range\n", atoi(inst)); | |
7571 | return -ENODEV; | |
7572 | } | |
7573 | ||
7574 | dprintf("imsm: open_new %s\n", inst); | |
7575 | a->info.container_member = atoi(inst); | |
7576 | ||
7577 | u.type = update_prealloc_badblocks_mem; | |
7578 | imsm_update_metadata_locally(c, &u, sizeof(u)); | |
7579 | ||
7580 | return 0; | |
7581 | } | |
7582 | ||
7583 | static int is_resyncing(struct imsm_dev *dev) | |
7584 | { | |
7585 | struct imsm_map *migr_map; | |
7586 | ||
7587 | if (!dev->vol.migr_state) | |
7588 | return 0; | |
7589 | ||
7590 | if (migr_type(dev) == MIGR_INIT || | |
7591 | migr_type(dev) == MIGR_REPAIR) | |
7592 | return 1; | |
7593 | ||
7594 | if (migr_type(dev) == MIGR_GEN_MIGR) | |
7595 | return 0; | |
7596 | ||
7597 | migr_map = get_imsm_map(dev, MAP_1); | |
7598 | ||
7599 | if (migr_map->map_state == IMSM_T_STATE_NORMAL && | |
7600 | dev->vol.migr_type != MIGR_GEN_MIGR) | |
7601 | return 1; | |
7602 | else | |
7603 | return 0; | |
7604 | } | |
7605 | ||
7606 | /* return true if we recorded new information */ | |
7607 | static int mark_failure(struct intel_super *super, | |
7608 | struct imsm_dev *dev, struct imsm_disk *disk, int idx) | |
7609 | { | |
7610 | __u32 ord; | |
7611 | int slot; | |
7612 | struct imsm_map *map; | |
7613 | char buf[MAX_RAID_SERIAL_LEN+3]; | |
7614 | unsigned int len, shift = 0; | |
7615 | ||
7616 | /* new failures are always set in map[0] */ | |
7617 | map = get_imsm_map(dev, MAP_0); | |
7618 | ||
7619 | slot = get_imsm_disk_slot(map, idx); | |
7620 | if (slot < 0) | |
7621 | return 0; | |
7622 | ||
7623 | ord = __le32_to_cpu(map->disk_ord_tbl[slot]); | |
7624 | if (is_failed(disk) && (ord & IMSM_ORD_REBUILD)) | |
7625 | return 0; | |
7626 | ||
7627 | memcpy(buf, disk->serial, MAX_RAID_SERIAL_LEN); | |
7628 | buf[MAX_RAID_SERIAL_LEN] = '\000'; | |
7629 | strcat(buf, ":0"); | |
7630 | if ((len = strlen(buf)) >= MAX_RAID_SERIAL_LEN) | |
7631 | shift = len - MAX_RAID_SERIAL_LEN + 1; | |
7632 | strncpy((char *)disk->serial, &buf[shift], MAX_RAID_SERIAL_LEN); | |
7633 | ||
7634 | disk->status |= FAILED_DISK; | |
7635 | set_imsm_ord_tbl_ent(map, slot, idx | IMSM_ORD_REBUILD); | |
7636 | /* mark failures in second map if second map exists and this disk | |
7637 | * in this slot. | |
7638 | * This is valid for migration, initialization and rebuild | |
7639 | */ | |
7640 | if (dev->vol.migr_state) { | |
7641 | struct imsm_map *map2 = get_imsm_map(dev, MAP_1); | |
7642 | int slot2 = get_imsm_disk_slot(map2, idx); | |
7643 | ||
7644 | if (slot2 < map2->num_members && slot2 >= 0) | |
7645 | set_imsm_ord_tbl_ent(map2, slot2, | |
7646 | idx | IMSM_ORD_REBUILD); | |
7647 | } | |
7648 | if (map->failed_disk_num == 0xff) | |
7649 | map->failed_disk_num = slot; | |
7650 | ||
7651 | clear_disk_badblocks(super->bbm_log, ord_to_idx(ord)); | |
7652 | ||
7653 | return 1; | |
7654 | } | |
7655 | ||
7656 | static void mark_missing(struct intel_super *super, | |
7657 | struct imsm_dev *dev, struct imsm_disk *disk, int idx) | |
7658 | { | |
7659 | mark_failure(super, dev, disk, idx); | |
7660 | ||
7661 | if (disk->scsi_id == __cpu_to_le32(~(__u32)0)) | |
7662 | return; | |
7663 | ||
7664 | disk->scsi_id = __cpu_to_le32(~(__u32)0); | |
7665 | memmove(&disk->serial[0], &disk->serial[1], MAX_RAID_SERIAL_LEN - 1); | |
7666 | } | |
7667 | ||
7668 | static void handle_missing(struct intel_super *super, struct imsm_dev *dev) | |
7669 | { | |
7670 | struct dl *dl; | |
7671 | ||
7672 | if (!super->missing) | |
7673 | return; | |
7674 | ||
7675 | /* When orom adds replacement for missing disk it does | |
7676 | * not remove entry of missing disk, but just updates map with | |
7677 | * new added disk. So it is not enough just to test if there is | |
7678 | * any missing disk, we have to look if there are any failed disks | |
7679 | * in map to stop migration */ | |
7680 | ||
7681 | dprintf("imsm: mark missing\n"); | |
7682 | /* end process for initialization and rebuild only | |
7683 | */ | |
7684 | if (is_gen_migration(dev) == 0) { | |
7685 | __u8 map_state; | |
7686 | int failed; | |
7687 | ||
7688 | failed = imsm_count_failed(super, dev, MAP_0); | |
7689 | map_state = imsm_check_degraded(super, dev, failed, MAP_0); | |
7690 | ||
7691 | if (failed) | |
7692 | end_migration(dev, super, map_state); | |
7693 | } | |
7694 | for (dl = super->missing; dl; dl = dl->next) | |
7695 | mark_missing(super, dev, &dl->disk, dl->index); | |
7696 | super->updates_pending++; | |
7697 | } | |
7698 | ||
7699 | static unsigned long long imsm_set_array_size(struct imsm_dev *dev, | |
7700 | long long new_size) | |
7701 | { | |
7702 | int used_disks = imsm_num_data_members(dev, MAP_0); | |
7703 | unsigned long long array_blocks; | |
7704 | struct imsm_map *map; | |
7705 | ||
7706 | if (used_disks == 0) { | |
7707 | /* when problems occures | |
7708 | * return current array_blocks value | |
7709 | */ | |
7710 | array_blocks = __le32_to_cpu(dev->size_high); | |
7711 | array_blocks = array_blocks << 32; | |
7712 | array_blocks += __le32_to_cpu(dev->size_low); | |
7713 | ||
7714 | return array_blocks; | |
7715 | } | |
7716 | ||
7717 | /* set array size in metadata | |
7718 | */ | |
7719 | if (new_size <= 0) { | |
7720 | /* OLCE size change is caused by added disks | |
7721 | */ | |
7722 | map = get_imsm_map(dev, MAP_0); | |
7723 | array_blocks = blocks_per_member(map) * used_disks; | |
7724 | } else { | |
7725 | /* Online Volume Size Change | |
7726 | * Using available free space | |
7727 | */ | |
7728 | array_blocks = new_size; | |
7729 | } | |
7730 | ||
7731 | /* round array size down to closest MB | |
7732 | */ | |
7733 | array_blocks = (array_blocks >> SECT_PER_MB_SHIFT) << SECT_PER_MB_SHIFT; | |
7734 | dev->size_low = __cpu_to_le32((__u32)array_blocks); | |
7735 | dev->size_high = __cpu_to_le32((__u32)(array_blocks >> 32)); | |
7736 | ||
7737 | return array_blocks; | |
7738 | } | |
7739 | ||
7740 | static void imsm_set_disk(struct active_array *a, int n, int state); | |
7741 | ||
7742 | static void imsm_progress_container_reshape(struct intel_super *super) | |
7743 | { | |
7744 | /* if no device has a migr_state, but some device has a | |
7745 | * different number of members than the previous device, start | |
7746 | * changing the number of devices in this device to match | |
7747 | * previous. | |
7748 | */ | |
7749 | struct imsm_super *mpb = super->anchor; | |
7750 | int prev_disks = -1; | |
7751 | int i; | |
7752 | int copy_map_size; | |
7753 | ||
7754 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
7755 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
7756 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
7757 | struct imsm_map *map2; | |
7758 | int prev_num_members; | |
7759 | ||
7760 | if (dev->vol.migr_state) | |
7761 | return; | |
7762 | ||
7763 | if (prev_disks == -1) | |
7764 | prev_disks = map->num_members; | |
7765 | if (prev_disks == map->num_members) | |
7766 | continue; | |
7767 | ||
7768 | /* OK, this array needs to enter reshape mode. | |
7769 | * i.e it needs a migr_state | |
7770 | */ | |
7771 | ||
7772 | copy_map_size = sizeof_imsm_map(map); | |
7773 | prev_num_members = map->num_members; | |
7774 | map->num_members = prev_disks; | |
7775 | dev->vol.migr_state = 1; | |
7776 | dev->vol.curr_migr_unit = 0; | |
7777 | set_migr_type(dev, MIGR_GEN_MIGR); | |
7778 | for (i = prev_num_members; | |
7779 | i < map->num_members; i++) | |
7780 | set_imsm_ord_tbl_ent(map, i, i); | |
7781 | map2 = get_imsm_map(dev, MAP_1); | |
7782 | /* Copy the current map */ | |
7783 | memcpy(map2, map, copy_map_size); | |
7784 | map2->num_members = prev_num_members; | |
7785 | ||
7786 | imsm_set_array_size(dev, -1); | |
7787 | super->clean_migration_record_by_mdmon = 1; | |
7788 | super->updates_pending++; | |
7789 | } | |
7790 | } | |
7791 | ||
7792 | /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild | |
7793 | * states are handled in imsm_set_disk() with one exception, when a | |
7794 | * resync is stopped due to a new failure this routine will set the | |
7795 | * 'degraded' state for the array. | |
7796 | */ | |
7797 | static int imsm_set_array_state(struct active_array *a, int consistent) | |
7798 | { | |
7799 | int inst = a->info.container_member; | |
7800 | struct intel_super *super = a->container->sb; | |
7801 | struct imsm_dev *dev = get_imsm_dev(super, inst); | |
7802 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
7803 | int failed = imsm_count_failed(super, dev, MAP_0); | |
7804 | __u8 map_state = imsm_check_degraded(super, dev, failed, MAP_0); | |
7805 | __u32 blocks_per_unit; | |
7806 | ||
7807 | if (dev->vol.migr_state && | |
7808 | dev->vol.migr_type == MIGR_GEN_MIGR) { | |
7809 | /* array state change is blocked due to reshape action | |
7810 | * We might need to | |
7811 | * - abort the reshape (if last_checkpoint is 0 and action!= reshape) | |
7812 | * - finish the reshape (if last_checkpoint is big and action != reshape) | |
7813 | * - update curr_migr_unit | |
7814 | */ | |
7815 | if (a->curr_action == reshape) { | |
7816 | /* still reshaping, maybe update curr_migr_unit */ | |
7817 | goto mark_checkpoint; | |
7818 | } else { | |
7819 | if (a->last_checkpoint == 0 && a->prev_action == reshape) { | |
7820 | /* for some reason we aborted the reshape. | |
7821 | * | |
7822 | * disable automatic metadata rollback | |
7823 | * user action is required to recover process | |
7824 | */ | |
7825 | if (0) { | |
7826 | struct imsm_map *map2 = | |
7827 | get_imsm_map(dev, MAP_1); | |
7828 | dev->vol.migr_state = 0; | |
7829 | set_migr_type(dev, 0); | |
7830 | dev->vol.curr_migr_unit = 0; | |
7831 | memcpy(map, map2, | |
7832 | sizeof_imsm_map(map2)); | |
7833 | super->updates_pending++; | |
7834 | } | |
7835 | } | |
7836 | if (a->last_checkpoint >= a->info.component_size) { | |
7837 | unsigned long long array_blocks; | |
7838 | int used_disks; | |
7839 | struct mdinfo *mdi; | |
7840 | ||
7841 | used_disks = imsm_num_data_members(dev, MAP_0); | |
7842 | if (used_disks > 0) { | |
7843 | array_blocks = | |
7844 | blocks_per_member(map) * | |
7845 | used_disks; | |
7846 | /* round array size down to closest MB | |
7847 | */ | |
7848 | array_blocks = (array_blocks | |
7849 | >> SECT_PER_MB_SHIFT) | |
7850 | << SECT_PER_MB_SHIFT; | |
7851 | a->info.custom_array_size = array_blocks; | |
7852 | /* encourage manager to update array | |
7853 | * size | |
7854 | */ | |
7855 | ||
7856 | a->check_reshape = 1; | |
7857 | } | |
7858 | /* finalize online capacity expansion/reshape */ | |
7859 | for (mdi = a->info.devs; mdi; mdi = mdi->next) | |
7860 | imsm_set_disk(a, | |
7861 | mdi->disk.raid_disk, | |
7862 | mdi->curr_state); | |
7863 | ||
7864 | imsm_progress_container_reshape(super); | |
7865 | } | |
7866 | } | |
7867 | } | |
7868 | ||
7869 | /* before we activate this array handle any missing disks */ | |
7870 | if (consistent == 2) | |
7871 | handle_missing(super, dev); | |
7872 | ||
7873 | if (consistent == 2 && | |
7874 | (!is_resync_complete(&a->info) || | |
7875 | map_state != IMSM_T_STATE_NORMAL || | |
7876 | dev->vol.migr_state)) | |
7877 | consistent = 0; | |
7878 | ||
7879 | if (is_resync_complete(&a->info)) { | |
7880 | /* complete intialization / resync, | |
7881 | * recovery and interrupted recovery is completed in | |
7882 | * ->set_disk | |
7883 | */ | |
7884 | if (is_resyncing(dev)) { | |
7885 | dprintf("imsm: mark resync done\n"); | |
7886 | end_migration(dev, super, map_state); | |
7887 | super->updates_pending++; | |
7888 | a->last_checkpoint = 0; | |
7889 | } | |
7890 | } else if ((!is_resyncing(dev) && !failed) && | |
7891 | (imsm_reshape_blocks_arrays_changes(super) == 0)) { | |
7892 | /* mark the start of the init process if nothing is failed */ | |
7893 | dprintf("imsm: mark resync start\n"); | |
7894 | if (map->map_state == IMSM_T_STATE_UNINITIALIZED) | |
7895 | migrate(dev, super, IMSM_T_STATE_NORMAL, MIGR_INIT); | |
7896 | else | |
7897 | migrate(dev, super, IMSM_T_STATE_NORMAL, MIGR_REPAIR); | |
7898 | super->updates_pending++; | |
7899 | } | |
7900 | ||
7901 | mark_checkpoint: | |
7902 | /* skip checkpointing for general migration, | |
7903 | * it is controlled in mdadm | |
7904 | */ | |
7905 | if (is_gen_migration(dev)) | |
7906 | goto skip_mark_checkpoint; | |
7907 | ||
7908 | /* check if we can update curr_migr_unit from resync_start, recovery_start */ | |
7909 | blocks_per_unit = blocks_per_migr_unit(super, dev); | |
7910 | if (blocks_per_unit) { | |
7911 | __u32 units32; | |
7912 | __u64 units; | |
7913 | ||
7914 | units = a->last_checkpoint / blocks_per_unit; | |
7915 | units32 = units; | |
7916 | ||
7917 | /* check that we did not overflow 32-bits, and that | |
7918 | * curr_migr_unit needs updating | |
7919 | */ | |
7920 | if (units32 == units && | |
7921 | units32 != 0 && | |
7922 | __le32_to_cpu(dev->vol.curr_migr_unit) != units32) { | |
7923 | dprintf("imsm: mark checkpoint (%u)\n", units32); | |
7924 | dev->vol.curr_migr_unit = __cpu_to_le32(units32); | |
7925 | super->updates_pending++; | |
7926 | } | |
7927 | } | |
7928 | ||
7929 | skip_mark_checkpoint: | |
7930 | /* mark dirty / clean */ | |
7931 | if (dev->vol.dirty != !consistent) { | |
7932 | dprintf("imsm: mark '%s'\n", consistent ? "clean" : "dirty"); | |
7933 | if (consistent) | |
7934 | dev->vol.dirty = 0; | |
7935 | else | |
7936 | dev->vol.dirty = 1; | |
7937 | super->updates_pending++; | |
7938 | } | |
7939 | ||
7940 | return consistent; | |
7941 | } | |
7942 | ||
7943 | static int imsm_disk_slot_to_ord(struct active_array *a, int slot) | |
7944 | { | |
7945 | int inst = a->info.container_member; | |
7946 | struct intel_super *super = a->container->sb; | |
7947 | struct imsm_dev *dev = get_imsm_dev(super, inst); | |
7948 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
7949 | ||
7950 | if (slot > map->num_members) { | |
7951 | pr_err("imsm: imsm_disk_slot_to_ord %d out of range 0..%d\n", | |
7952 | slot, map->num_members - 1); | |
7953 | return -1; | |
7954 | } | |
7955 | ||
7956 | if (slot < 0) | |
7957 | return -1; | |
7958 | ||
7959 | return get_imsm_ord_tbl_ent(dev, slot, MAP_0); | |
7960 | } | |
7961 | ||
7962 | static void imsm_set_disk(struct active_array *a, int n, int state) | |
7963 | { | |
7964 | int inst = a->info.container_member; | |
7965 | struct intel_super *super = a->container->sb; | |
7966 | struct imsm_dev *dev = get_imsm_dev(super, inst); | |
7967 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
7968 | struct imsm_disk *disk; | |
7969 | struct mdinfo *mdi; | |
7970 | int recovery_not_finished = 0; | |
7971 | int failed; | |
7972 | int ord; | |
7973 | __u8 map_state; | |
7974 | ||
7975 | ord = imsm_disk_slot_to_ord(a, n); | |
7976 | if (ord < 0) | |
7977 | return; | |
7978 | ||
7979 | dprintf("imsm: set_disk %d:%x\n", n, state); | |
7980 | disk = get_imsm_disk(super, ord_to_idx(ord)); | |
7981 | ||
7982 | /* check for new failures */ | |
7983 | if (state & DS_FAULTY) { | |
7984 | if (mark_failure(super, dev, disk, ord_to_idx(ord))) | |
7985 | super->updates_pending++; | |
7986 | } | |
7987 | ||
7988 | /* check if in_sync */ | |
7989 | if (state & DS_INSYNC && ord & IMSM_ORD_REBUILD && is_rebuilding(dev)) { | |
7990 | struct imsm_map *migr_map = get_imsm_map(dev, MAP_1); | |
7991 | ||
7992 | set_imsm_ord_tbl_ent(migr_map, n, ord_to_idx(ord)); | |
7993 | super->updates_pending++; | |
7994 | } | |
7995 | ||
7996 | failed = imsm_count_failed(super, dev, MAP_0); | |
7997 | map_state = imsm_check_degraded(super, dev, failed, MAP_0); | |
7998 | ||
7999 | /* check if recovery complete, newly degraded, or failed */ | |
8000 | dprintf("imsm: Detected transition to state "); | |
8001 | switch (map_state) { | |
8002 | case IMSM_T_STATE_NORMAL: /* transition to normal state */ | |
8003 | dprintf("normal: "); | |
8004 | if (is_rebuilding(dev)) { | |
8005 | dprintf_cont("while rebuilding"); | |
8006 | /* check if recovery is really finished */ | |
8007 | for (mdi = a->info.devs; mdi ; mdi = mdi->next) | |
8008 | if (mdi->recovery_start != MaxSector) { | |
8009 | recovery_not_finished = 1; | |
8010 | break; | |
8011 | } | |
8012 | if (recovery_not_finished) { | |
8013 | dprintf_cont("\n"); | |
8014 | dprintf("Rebuild has not finished yet, state not changed"); | |
8015 | if (a->last_checkpoint < mdi->recovery_start) { | |
8016 | a->last_checkpoint = mdi->recovery_start; | |
8017 | super->updates_pending++; | |
8018 | } | |
8019 | break; | |
8020 | } | |
8021 | end_migration(dev, super, map_state); | |
8022 | map = get_imsm_map(dev, MAP_0); | |
8023 | map->failed_disk_num = ~0; | |
8024 | super->updates_pending++; | |
8025 | a->last_checkpoint = 0; | |
8026 | break; | |
8027 | } | |
8028 | if (is_gen_migration(dev)) { | |
8029 | dprintf_cont("while general migration"); | |
8030 | if (a->last_checkpoint >= a->info.component_size) | |
8031 | end_migration(dev, super, map_state); | |
8032 | else | |
8033 | map->map_state = map_state; | |
8034 | map = get_imsm_map(dev, MAP_0); | |
8035 | map->failed_disk_num = ~0; | |
8036 | super->updates_pending++; | |
8037 | break; | |
8038 | } | |
8039 | break; | |
8040 | case IMSM_T_STATE_DEGRADED: /* transition to degraded state */ | |
8041 | dprintf_cont("degraded: "); | |
8042 | if (map->map_state != map_state && !dev->vol.migr_state) { | |
8043 | dprintf_cont("mark degraded"); | |
8044 | map->map_state = map_state; | |
8045 | super->updates_pending++; | |
8046 | a->last_checkpoint = 0; | |
8047 | break; | |
8048 | } | |
8049 | if (is_rebuilding(dev)) { | |
8050 | dprintf_cont("while rebuilding."); | |
8051 | if (map->map_state != map_state) { | |
8052 | dprintf_cont(" Map state change"); | |
8053 | end_migration(dev, super, map_state); | |
8054 | super->updates_pending++; | |
8055 | } | |
8056 | break; | |
8057 | } | |
8058 | if (is_gen_migration(dev)) { | |
8059 | dprintf_cont("while general migration"); | |
8060 | if (a->last_checkpoint >= a->info.component_size) | |
8061 | end_migration(dev, super, map_state); | |
8062 | else { | |
8063 | map->map_state = map_state; | |
8064 | manage_second_map(super, dev); | |
8065 | } | |
8066 | super->updates_pending++; | |
8067 | break; | |
8068 | } | |
8069 | if (is_initializing(dev)) { | |
8070 | dprintf_cont("while initialization."); | |
8071 | map->map_state = map_state; | |
8072 | super->updates_pending++; | |
8073 | break; | |
8074 | } | |
8075 | break; | |
8076 | case IMSM_T_STATE_FAILED: /* transition to failed state */ | |
8077 | dprintf_cont("failed: "); | |
8078 | if (is_gen_migration(dev)) { | |
8079 | dprintf_cont("while general migration"); | |
8080 | map->map_state = map_state; | |
8081 | super->updates_pending++; | |
8082 | break; | |
8083 | } | |
8084 | if (map->map_state != map_state) { | |
8085 | dprintf_cont("mark failed"); | |
8086 | end_migration(dev, super, map_state); | |
8087 | super->updates_pending++; | |
8088 | a->last_checkpoint = 0; | |
8089 | break; | |
8090 | } | |
8091 | break; | |
8092 | default: | |
8093 | dprintf_cont("state %i\n", map_state); | |
8094 | } | |
8095 | dprintf_cont("\n"); | |
8096 | } | |
8097 | ||
8098 | static int store_imsm_mpb(int fd, struct imsm_super *mpb) | |
8099 | { | |
8100 | void *buf = mpb; | |
8101 | __u32 mpb_size = __le32_to_cpu(mpb->mpb_size); | |
8102 | unsigned long long dsize; | |
8103 | unsigned long long sectors; | |
8104 | unsigned int sector_size; | |
8105 | ||
8106 | get_dev_sector_size(fd, NULL, §or_size); | |
8107 | get_dev_size(fd, NULL, &dsize); | |
8108 | ||
8109 | if (mpb_size > sector_size) { | |
8110 | /* -1 to account for anchor */ | |
8111 | sectors = mpb_sectors(mpb, sector_size) - 1; | |
8112 | ||
8113 | /* write the extended mpb to the sectors preceeding the anchor */ | |
8114 | if (lseek64(fd, dsize - (sector_size * (2 + sectors)), | |
8115 | SEEK_SET) < 0) | |
8116 | return 1; | |
8117 | ||
8118 | if ((unsigned long long)write(fd, buf + sector_size, | |
8119 | sector_size * sectors) != sector_size * sectors) | |
8120 | return 1; | |
8121 | } | |
8122 | ||
8123 | /* first block is stored on second to last sector of the disk */ | |
8124 | if (lseek64(fd, dsize - (sector_size * 2), SEEK_SET) < 0) | |
8125 | return 1; | |
8126 | ||
8127 | if ((unsigned int)write(fd, buf, sector_size) != sector_size) | |
8128 | return 1; | |
8129 | ||
8130 | return 0; | |
8131 | } | |
8132 | ||
8133 | static void imsm_sync_metadata(struct supertype *container) | |
8134 | { | |
8135 | struct intel_super *super = container->sb; | |
8136 | ||
8137 | dprintf("sync metadata: %d\n", super->updates_pending); | |
8138 | if (!super->updates_pending) | |
8139 | return; | |
8140 | ||
8141 | write_super_imsm(container, 0); | |
8142 | ||
8143 | super->updates_pending = 0; | |
8144 | } | |
8145 | ||
8146 | static struct dl *imsm_readd(struct intel_super *super, int idx, struct active_array *a) | |
8147 | { | |
8148 | struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member); | |
8149 | int i = get_imsm_disk_idx(dev, idx, MAP_X); | |
8150 | struct dl *dl; | |
8151 | ||
8152 | for (dl = super->disks; dl; dl = dl->next) | |
8153 | if (dl->index == i) | |
8154 | break; | |
8155 | ||
8156 | if (dl && is_failed(&dl->disk)) | |
8157 | dl = NULL; | |
8158 | ||
8159 | if (dl) | |
8160 | dprintf("found %x:%x\n", dl->major, dl->minor); | |
8161 | ||
8162 | return dl; | |
8163 | } | |
8164 | ||
8165 | static struct dl *imsm_add_spare(struct intel_super *super, int slot, | |
8166 | struct active_array *a, int activate_new, | |
8167 | struct mdinfo *additional_test_list) | |
8168 | { | |
8169 | struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member); | |
8170 | int idx = get_imsm_disk_idx(dev, slot, MAP_X); | |
8171 | struct imsm_super *mpb = super->anchor; | |
8172 | struct imsm_map *map; | |
8173 | unsigned long long pos; | |
8174 | struct mdinfo *d; | |
8175 | struct extent *ex; | |
8176 | int i, j; | |
8177 | int found; | |
8178 | __u32 array_start = 0; | |
8179 | __u32 array_end = 0; | |
8180 | struct dl *dl; | |
8181 | struct mdinfo *test_list; | |
8182 | ||
8183 | for (dl = super->disks; dl; dl = dl->next) { | |
8184 | /* If in this array, skip */ | |
8185 | for (d = a->info.devs ; d ; d = d->next) | |
8186 | if (d->state_fd >= 0 && | |
8187 | d->disk.major == dl->major && | |
8188 | d->disk.minor == dl->minor) { | |
8189 | dprintf("%x:%x already in array\n", | |
8190 | dl->major, dl->minor); | |
8191 | break; | |
8192 | } | |
8193 | if (d) | |
8194 | continue; | |
8195 | test_list = additional_test_list; | |
8196 | while (test_list) { | |
8197 | if (test_list->disk.major == dl->major && | |
8198 | test_list->disk.minor == dl->minor) { | |
8199 | dprintf("%x:%x already in additional test list\n", | |
8200 | dl->major, dl->minor); | |
8201 | break; | |
8202 | } | |
8203 | test_list = test_list->next; | |
8204 | } | |
8205 | if (test_list) | |
8206 | continue; | |
8207 | ||
8208 | /* skip in use or failed drives */ | |
8209 | if (is_failed(&dl->disk) || idx == dl->index || | |
8210 | dl->index == -2) { | |
8211 | dprintf("%x:%x status (failed: %d index: %d)\n", | |
8212 | dl->major, dl->minor, is_failed(&dl->disk), idx); | |
8213 | continue; | |
8214 | } | |
8215 | ||
8216 | /* skip pure spares when we are looking for partially | |
8217 | * assimilated drives | |
8218 | */ | |
8219 | if (dl->index == -1 && !activate_new) | |
8220 | continue; | |
8221 | ||
8222 | /* Does this unused device have the requisite free space? | |
8223 | * It needs to be able to cover all member volumes | |
8224 | */ | |
8225 | ex = get_extents(super, dl); | |
8226 | if (!ex) { | |
8227 | dprintf("cannot get extents\n"); | |
8228 | continue; | |
8229 | } | |
8230 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
8231 | dev = get_imsm_dev(super, i); | |
8232 | map = get_imsm_map(dev, MAP_0); | |
8233 | ||
8234 | /* check if this disk is already a member of | |
8235 | * this array | |
8236 | */ | |
8237 | if (get_imsm_disk_slot(map, dl->index) >= 0) | |
8238 | continue; | |
8239 | ||
8240 | found = 0; | |
8241 | j = 0; | |
8242 | pos = 0; | |
8243 | array_start = pba_of_lba0(map); | |
8244 | array_end = array_start + | |
8245 | blocks_per_member(map) - 1; | |
8246 | ||
8247 | do { | |
8248 | /* check that we can start at pba_of_lba0 with | |
8249 | * blocks_per_member of space | |
8250 | */ | |
8251 | if (array_start >= pos && array_end < ex[j].start) { | |
8252 | found = 1; | |
8253 | break; | |
8254 | } | |
8255 | pos = ex[j].start + ex[j].size; | |
8256 | j++; | |
8257 | } while (ex[j-1].size); | |
8258 | ||
8259 | if (!found) | |
8260 | break; | |
8261 | } | |
8262 | ||
8263 | free(ex); | |
8264 | if (i < mpb->num_raid_devs) { | |
8265 | dprintf("%x:%x does not have %u to %u available\n", | |
8266 | dl->major, dl->minor, array_start, array_end); | |
8267 | /* No room */ | |
8268 | continue; | |
8269 | } | |
8270 | return dl; | |
8271 | } | |
8272 | ||
8273 | return dl; | |
8274 | } | |
8275 | ||
8276 | static int imsm_rebuild_allowed(struct supertype *cont, int dev_idx, int failed) | |
8277 | { | |
8278 | struct imsm_dev *dev2; | |
8279 | struct imsm_map *map; | |
8280 | struct dl *idisk; | |
8281 | int slot; | |
8282 | int idx; | |
8283 | __u8 state; | |
8284 | ||
8285 | dev2 = get_imsm_dev(cont->sb, dev_idx); | |
8286 | if (dev2) { | |
8287 | state = imsm_check_degraded(cont->sb, dev2, failed, MAP_0); | |
8288 | if (state == IMSM_T_STATE_FAILED) { | |
8289 | map = get_imsm_map(dev2, MAP_0); | |
8290 | if (!map) | |
8291 | return 1; | |
8292 | for (slot = 0; slot < map->num_members; slot++) { | |
8293 | /* | |
8294 | * Check if failed disks are deleted from intel | |
8295 | * disk list or are marked to be deleted | |
8296 | */ | |
8297 | idx = get_imsm_disk_idx(dev2, slot, MAP_X); | |
8298 | idisk = get_imsm_dl_disk(cont->sb, idx); | |
8299 | /* | |
8300 | * Do not rebuild the array if failed disks | |
8301 | * from failed sub-array are not removed from | |
8302 | * container. | |
8303 | */ | |
8304 | if (idisk && | |
8305 | is_failed(&idisk->disk) && | |
8306 | (idisk->action != DISK_REMOVE)) | |
8307 | return 0; | |
8308 | } | |
8309 | } | |
8310 | } | |
8311 | return 1; | |
8312 | } | |
8313 | ||
8314 | static struct mdinfo *imsm_activate_spare(struct active_array *a, | |
8315 | struct metadata_update **updates) | |
8316 | { | |
8317 | /** | |
8318 | * Find a device with unused free space and use it to replace a | |
8319 | * failed/vacant region in an array. We replace failed regions one a | |
8320 | * array at a time. The result is that a new spare disk will be added | |
8321 | * to the first failed array and after the monitor has finished | |
8322 | * propagating failures the remainder will be consumed. | |
8323 | * | |
8324 | * FIXME add a capability for mdmon to request spares from another | |
8325 | * container. | |
8326 | */ | |
8327 | ||
8328 | struct intel_super *super = a->container->sb; | |
8329 | int inst = a->info.container_member; | |
8330 | struct imsm_dev *dev = get_imsm_dev(super, inst); | |
8331 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
8332 | int failed = a->info.array.raid_disks; | |
8333 | struct mdinfo *rv = NULL; | |
8334 | struct mdinfo *d; | |
8335 | struct mdinfo *di; | |
8336 | struct metadata_update *mu; | |
8337 | struct dl *dl; | |
8338 | struct imsm_update_activate_spare *u; | |
8339 | int num_spares = 0; | |
8340 | int i; | |
8341 | int allowed; | |
8342 | ||
8343 | for (d = a->info.devs ; d ; d = d->next) { | |
8344 | if ((d->curr_state & DS_FAULTY) && | |
8345 | d->state_fd >= 0) | |
8346 | /* wait for Removal to happen */ | |
8347 | return NULL; | |
8348 | if (d->state_fd >= 0) | |
8349 | failed--; | |
8350 | } | |
8351 | ||
8352 | dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n", | |
8353 | inst, failed, a->info.array.raid_disks, a->info.array.level); | |
8354 | ||
8355 | if (imsm_reshape_blocks_arrays_changes(super)) | |
8356 | return NULL; | |
8357 | ||
8358 | /* Cannot activate another spare if rebuild is in progress already | |
8359 | */ | |
8360 | if (is_rebuilding(dev)) { | |
8361 | dprintf("imsm: No spare activation allowed. Rebuild in progress already.\n"); | |
8362 | return NULL; | |
8363 | } | |
8364 | ||
8365 | if (a->info.array.level == 4) | |
8366 | /* No repair for takeovered array | |
8367 | * imsm doesn't support raid4 | |
8368 | */ | |
8369 | return NULL; | |
8370 | ||
8371 | if (imsm_check_degraded(super, dev, failed, MAP_0) != | |
8372 | IMSM_T_STATE_DEGRADED) | |
8373 | return NULL; | |
8374 | ||
8375 | if (get_imsm_map(dev, MAP_0)->map_state == IMSM_T_STATE_UNINITIALIZED) { | |
8376 | dprintf("imsm: No spare activation allowed. Volume is not initialized.\n"); | |
8377 | return NULL; | |
8378 | } | |
8379 | ||
8380 | /* | |
8381 | * If there are any failed disks check state of the other volume. | |
8382 | * Block rebuild if the another one is failed until failed disks | |
8383 | * are removed from container. | |
8384 | */ | |
8385 | if (failed) { | |
8386 | dprintf("found failed disks in %.*s, check if there anotherfailed sub-array.\n", | |
8387 | MAX_RAID_SERIAL_LEN, dev->volume); | |
8388 | /* check if states of the other volumes allow for rebuild */ | |
8389 | for (i = 0; i < super->anchor->num_raid_devs; i++) { | |
8390 | if (i != inst) { | |
8391 | allowed = imsm_rebuild_allowed(a->container, | |
8392 | i, failed); | |
8393 | if (!allowed) | |
8394 | return NULL; | |
8395 | } | |
8396 | } | |
8397 | } | |
8398 | ||
8399 | /* For each slot, if it is not working, find a spare */ | |
8400 | for (i = 0; i < a->info.array.raid_disks; i++) { | |
8401 | for (d = a->info.devs ; d ; d = d->next) | |
8402 | if (d->disk.raid_disk == i) | |
8403 | break; | |
8404 | dprintf("found %d: %p %x\n", i, d, d?d->curr_state:0); | |
8405 | if (d && (d->state_fd >= 0)) | |
8406 | continue; | |
8407 | ||
8408 | /* | |
8409 | * OK, this device needs recovery. Try to re-add the | |
8410 | * previous occupant of this slot, if this fails see if | |
8411 | * we can continue the assimilation of a spare that was | |
8412 | * partially assimilated, finally try to activate a new | |
8413 | * spare. | |
8414 | */ | |
8415 | dl = imsm_readd(super, i, a); | |
8416 | if (!dl) | |
8417 | dl = imsm_add_spare(super, i, a, 0, rv); | |
8418 | if (!dl) | |
8419 | dl = imsm_add_spare(super, i, a, 1, rv); | |
8420 | if (!dl) | |
8421 | continue; | |
8422 | ||
8423 | /* found a usable disk with enough space */ | |
8424 | di = xcalloc(1, sizeof(*di)); | |
8425 | ||
8426 | /* dl->index will be -1 in the case we are activating a | |
8427 | * pristine spare. imsm_process_update() will create a | |
8428 | * new index in this case. Once a disk is found to be | |
8429 | * failed in all member arrays it is kicked from the | |
8430 | * metadata | |
8431 | */ | |
8432 | di->disk.number = dl->index; | |
8433 | ||
8434 | /* (ab)use di->devs to store a pointer to the device | |
8435 | * we chose | |
8436 | */ | |
8437 | di->devs = (struct mdinfo *) dl; | |
8438 | ||
8439 | di->disk.raid_disk = i; | |
8440 | di->disk.major = dl->major; | |
8441 | di->disk.minor = dl->minor; | |
8442 | di->disk.state = 0; | |
8443 | di->recovery_start = 0; | |
8444 | di->data_offset = pba_of_lba0(map); | |
8445 | di->component_size = a->info.component_size; | |
8446 | di->container_member = inst; | |
8447 | di->bb.supported = 1; | |
8448 | super->random = random32(); | |
8449 | di->next = rv; | |
8450 | rv = di; | |
8451 | num_spares++; | |
8452 | dprintf("%x:%x to be %d at %llu\n", dl->major, dl->minor, | |
8453 | i, di->data_offset); | |
8454 | } | |
8455 | ||
8456 | if (!rv) | |
8457 | /* No spares found */ | |
8458 | return rv; | |
8459 | /* Now 'rv' has a list of devices to return. | |
8460 | * Create a metadata_update record to update the | |
8461 | * disk_ord_tbl for the array | |
8462 | */ | |
8463 | mu = xmalloc(sizeof(*mu)); | |
8464 | mu->buf = xcalloc(num_spares, | |
8465 | sizeof(struct imsm_update_activate_spare)); | |
8466 | mu->space = NULL; | |
8467 | mu->space_list = NULL; | |
8468 | mu->len = sizeof(struct imsm_update_activate_spare) * num_spares; | |
8469 | mu->next = *updates; | |
8470 | u = (struct imsm_update_activate_spare *) mu->buf; | |
8471 | ||
8472 | for (di = rv ; di ; di = di->next) { | |
8473 | u->type = update_activate_spare; | |
8474 | u->dl = (struct dl *) di->devs; | |
8475 | di->devs = NULL; | |
8476 | u->slot = di->disk.raid_disk; | |
8477 | u->array = inst; | |
8478 | u->next = u + 1; | |
8479 | u++; | |
8480 | } | |
8481 | (u-1)->next = NULL; | |
8482 | *updates = mu; | |
8483 | ||
8484 | return rv; | |
8485 | } | |
8486 | ||
8487 | static int disks_overlap(struct intel_super *super, int idx, struct imsm_update_create_array *u) | |
8488 | { | |
8489 | struct imsm_dev *dev = get_imsm_dev(super, idx); | |
8490 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
8491 | struct imsm_map *new_map = get_imsm_map(&u->dev, MAP_0); | |
8492 | struct disk_info *inf = get_disk_info(u); | |
8493 | struct imsm_disk *disk; | |
8494 | int i; | |
8495 | int j; | |
8496 | ||
8497 | for (i = 0; i < map->num_members; i++) { | |
8498 | disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i, MAP_X)); | |
8499 | for (j = 0; j < new_map->num_members; j++) | |
8500 | if (serialcmp(disk->serial, inf[j].serial) == 0) | |
8501 | return 1; | |
8502 | } | |
8503 | ||
8504 | return 0; | |
8505 | } | |
8506 | ||
8507 | static struct dl *get_disk_super(struct intel_super *super, int major, int minor) | |
8508 | { | |
8509 | struct dl *dl; | |
8510 | ||
8511 | for (dl = super->disks; dl; dl = dl->next) | |
8512 | if (dl->major == major && dl->minor == minor) | |
8513 | return dl; | |
8514 | return NULL; | |
8515 | } | |
8516 | ||
8517 | static int remove_disk_super(struct intel_super *super, int major, int minor) | |
8518 | { | |
8519 | struct dl *prev; | |
8520 | struct dl *dl; | |
8521 | ||
8522 | prev = NULL; | |
8523 | for (dl = super->disks; dl; dl = dl->next) { | |
8524 | if (dl->major == major && dl->minor == minor) { | |
8525 | /* remove */ | |
8526 | if (prev) | |
8527 | prev->next = dl->next; | |
8528 | else | |
8529 | super->disks = dl->next; | |
8530 | dl->next = NULL; | |
8531 | __free_imsm_disk(dl); | |
8532 | dprintf("removed %x:%x\n", major, minor); | |
8533 | break; | |
8534 | } | |
8535 | prev = dl; | |
8536 | } | |
8537 | return 0; | |
8538 | } | |
8539 | ||
8540 | static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index); | |
8541 | ||
8542 | static int add_remove_disk_update(struct intel_super *super) | |
8543 | { | |
8544 | int check_degraded = 0; | |
8545 | struct dl *disk; | |
8546 | ||
8547 | /* add/remove some spares to/from the metadata/contrainer */ | |
8548 | while (super->disk_mgmt_list) { | |
8549 | struct dl *disk_cfg; | |
8550 | ||
8551 | disk_cfg = super->disk_mgmt_list; | |
8552 | super->disk_mgmt_list = disk_cfg->next; | |
8553 | disk_cfg->next = NULL; | |
8554 | ||
8555 | if (disk_cfg->action == DISK_ADD) { | |
8556 | disk_cfg->next = super->disks; | |
8557 | super->disks = disk_cfg; | |
8558 | check_degraded = 1; | |
8559 | dprintf("added %x:%x\n", | |
8560 | disk_cfg->major, disk_cfg->minor); | |
8561 | } else if (disk_cfg->action == DISK_REMOVE) { | |
8562 | dprintf("Disk remove action processed: %x.%x\n", | |
8563 | disk_cfg->major, disk_cfg->minor); | |
8564 | disk = get_disk_super(super, | |
8565 | disk_cfg->major, | |
8566 | disk_cfg->minor); | |
8567 | if (disk) { | |
8568 | /* store action status */ | |
8569 | disk->action = DISK_REMOVE; | |
8570 | /* remove spare disks only */ | |
8571 | if (disk->index == -1) { | |
8572 | remove_disk_super(super, | |
8573 | disk_cfg->major, | |
8574 | disk_cfg->minor); | |
8575 | } | |
8576 | } | |
8577 | /* release allocate disk structure */ | |
8578 | __free_imsm_disk(disk_cfg); | |
8579 | } | |
8580 | } | |
8581 | return check_degraded; | |
8582 | } | |
8583 | ||
8584 | static int apply_reshape_migration_update(struct imsm_update_reshape_migration *u, | |
8585 | struct intel_super *super, | |
8586 | void ***space_list) | |
8587 | { | |
8588 | struct intel_dev *id; | |
8589 | void **tofree = NULL; | |
8590 | int ret_val = 0; | |
8591 | ||
8592 | dprintf("(enter)\n"); | |
8593 | if (u->subdev < 0 || u->subdev > 1) { | |
8594 | dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev); | |
8595 | return ret_val; | |
8596 | } | |
8597 | if (space_list == NULL || *space_list == NULL) { | |
8598 | dprintf("imsm: Error: Memory is not allocated\n"); | |
8599 | return ret_val; | |
8600 | } | |
8601 | ||
8602 | for (id = super->devlist ; id; id = id->next) { | |
8603 | if (id->index == (unsigned)u->subdev) { | |
8604 | struct imsm_dev *dev = get_imsm_dev(super, u->subdev); | |
8605 | struct imsm_map *map; | |
8606 | struct imsm_dev *new_dev = | |
8607 | (struct imsm_dev *)*space_list; | |
8608 | struct imsm_map *migr_map = get_imsm_map(dev, MAP_1); | |
8609 | int to_state; | |
8610 | struct dl *new_disk; | |
8611 | ||
8612 | if (new_dev == NULL) | |
8613 | return ret_val; | |
8614 | *space_list = **space_list; | |
8615 | memcpy(new_dev, dev, sizeof_imsm_dev(dev, 0)); | |
8616 | map = get_imsm_map(new_dev, MAP_0); | |
8617 | if (migr_map) { | |
8618 | dprintf("imsm: Error: migration in progress"); | |
8619 | return ret_val; | |
8620 | } | |
8621 | ||
8622 | to_state = map->map_state; | |
8623 | if ((u->new_level == 5) && (map->raid_level == 0)) { | |
8624 | map->num_members++; | |
8625 | /* this should not happen */ | |
8626 | if (u->new_disks[0] < 0) { | |
8627 | map->failed_disk_num = | |
8628 | map->num_members - 1; | |
8629 | to_state = IMSM_T_STATE_DEGRADED; | |
8630 | } else | |
8631 | to_state = IMSM_T_STATE_NORMAL; | |
8632 | } | |
8633 | migrate(new_dev, super, to_state, MIGR_GEN_MIGR); | |
8634 | if (u->new_level > -1) | |
8635 | map->raid_level = u->new_level; | |
8636 | migr_map = get_imsm_map(new_dev, MAP_1); | |
8637 | if ((u->new_level == 5) && | |
8638 | (migr_map->raid_level == 0)) { | |
8639 | int ord = map->num_members - 1; | |
8640 | migr_map->num_members--; | |
8641 | if (u->new_disks[0] < 0) | |
8642 | ord |= IMSM_ORD_REBUILD; | |
8643 | set_imsm_ord_tbl_ent(map, | |
8644 | map->num_members - 1, | |
8645 | ord); | |
8646 | } | |
8647 | id->dev = new_dev; | |
8648 | tofree = (void **)dev; | |
8649 | ||
8650 | /* update chunk size | |
8651 | */ | |
8652 | if (u->new_chunksize > 0) { | |
8653 | unsigned long long num_data_stripes; | |
8654 | int used_disks = | |
8655 | imsm_num_data_members(dev, MAP_0); | |
8656 | ||
8657 | if (used_disks == 0) | |
8658 | return ret_val; | |
8659 | ||
8660 | map->blocks_per_strip = | |
8661 | __cpu_to_le16(u->new_chunksize * 2); | |
8662 | num_data_stripes = | |
8663 | (join_u32(dev->size_low, dev->size_high) | |
8664 | / used_disks); | |
8665 | num_data_stripes /= map->blocks_per_strip; | |
8666 | num_data_stripes /= map->num_domains; | |
8667 | set_num_data_stripes(map, num_data_stripes); | |
8668 | } | |
8669 | ||
8670 | /* add disk | |
8671 | */ | |
8672 | if (u->new_level != 5 || migr_map->raid_level != 0 || | |
8673 | migr_map->raid_level == map->raid_level) | |
8674 | goto skip_disk_add; | |
8675 | ||
8676 | if (u->new_disks[0] >= 0) { | |
8677 | /* use passes spare | |
8678 | */ | |
8679 | new_disk = get_disk_super(super, | |
8680 | major(u->new_disks[0]), | |
8681 | minor(u->new_disks[0])); | |
8682 | dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n", | |
8683 | major(u->new_disks[0]), | |
8684 | minor(u->new_disks[0]), | |
8685 | new_disk, new_disk->index); | |
8686 | if (new_disk == NULL) | |
8687 | goto error_disk_add; | |
8688 | ||
8689 | new_disk->index = map->num_members - 1; | |
8690 | /* slot to fill in autolayout | |
8691 | */ | |
8692 | new_disk->raiddisk = new_disk->index; | |
8693 | new_disk->disk.status |= CONFIGURED_DISK; | |
8694 | new_disk->disk.status &= ~SPARE_DISK; | |
8695 | } else | |
8696 | goto error_disk_add; | |
8697 | ||
8698 | skip_disk_add: | |
8699 | *tofree = *space_list; | |
8700 | /* calculate new size | |
8701 | */ | |
8702 | imsm_set_array_size(new_dev, -1); | |
8703 | ||
8704 | ret_val = 1; | |
8705 | } | |
8706 | } | |
8707 | ||
8708 | if (tofree) | |
8709 | *space_list = tofree; | |
8710 | return ret_val; | |
8711 | ||
8712 | error_disk_add: | |
8713 | dprintf("Error: imsm: Cannot find disk.\n"); | |
8714 | return ret_val; | |
8715 | } | |
8716 | ||
8717 | static int apply_size_change_update(struct imsm_update_size_change *u, | |
8718 | struct intel_super *super) | |
8719 | { | |
8720 | struct intel_dev *id; | |
8721 | int ret_val = 0; | |
8722 | ||
8723 | dprintf("(enter)\n"); | |
8724 | if (u->subdev < 0 || u->subdev > 1) { | |
8725 | dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev); | |
8726 | return ret_val; | |
8727 | } | |
8728 | ||
8729 | for (id = super->devlist ; id; id = id->next) { | |
8730 | if (id->index == (unsigned)u->subdev) { | |
8731 | struct imsm_dev *dev = get_imsm_dev(super, u->subdev); | |
8732 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
8733 | int used_disks = imsm_num_data_members(dev, MAP_0); | |
8734 | unsigned long long blocks_per_member; | |
8735 | unsigned long long num_data_stripes; | |
8736 | ||
8737 | /* calculate new size | |
8738 | */ | |
8739 | blocks_per_member = u->new_size / used_disks; | |
8740 | num_data_stripes = blocks_per_member / | |
8741 | map->blocks_per_strip; | |
8742 | num_data_stripes /= map->num_domains; | |
8743 | dprintf("(size: %llu, blocks per member: %llu, num_data_stipes: %llu)\n", | |
8744 | u->new_size, blocks_per_member, | |
8745 | num_data_stripes); | |
8746 | set_blocks_per_member(map, blocks_per_member); | |
8747 | set_num_data_stripes(map, num_data_stripes); | |
8748 | imsm_set_array_size(dev, u->new_size); | |
8749 | ||
8750 | ret_val = 1; | |
8751 | break; | |
8752 | } | |
8753 | } | |
8754 | ||
8755 | return ret_val; | |
8756 | } | |
8757 | ||
8758 | static int apply_update_activate_spare(struct imsm_update_activate_spare *u, | |
8759 | struct intel_super *super, | |
8760 | struct active_array *active_array) | |
8761 | { | |
8762 | struct imsm_super *mpb = super->anchor; | |
8763 | struct imsm_dev *dev = get_imsm_dev(super, u->array); | |
8764 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
8765 | struct imsm_map *migr_map; | |
8766 | struct active_array *a; | |
8767 | struct imsm_disk *disk; | |
8768 | __u8 to_state; | |
8769 | struct dl *dl; | |
8770 | unsigned int found; | |
8771 | int failed; | |
8772 | int victim; | |
8773 | int i; | |
8774 | int second_map_created = 0; | |
8775 | ||
8776 | for (; u; u = u->next) { | |
8777 | victim = get_imsm_disk_idx(dev, u->slot, MAP_X); | |
8778 | ||
8779 | if (victim < 0) | |
8780 | return 0; | |
8781 | ||
8782 | for (dl = super->disks; dl; dl = dl->next) | |
8783 | if (dl == u->dl) | |
8784 | break; | |
8785 | ||
8786 | if (!dl) { | |
8787 | pr_err("error: imsm_activate_spare passed an unknown disk (index: %d)\n", | |
8788 | u->dl->index); | |
8789 | return 0; | |
8790 | } | |
8791 | ||
8792 | /* count failures (excluding rebuilds and the victim) | |
8793 | * to determine map[0] state | |
8794 | */ | |
8795 | failed = 0; | |
8796 | for (i = 0; i < map->num_members; i++) { | |
8797 | if (i == u->slot) | |
8798 | continue; | |
8799 | disk = get_imsm_disk(super, | |
8800 | get_imsm_disk_idx(dev, i, MAP_X)); | |
8801 | if (!disk || is_failed(disk)) | |
8802 | failed++; | |
8803 | } | |
8804 | ||
8805 | /* adding a pristine spare, assign a new index */ | |
8806 | if (dl->index < 0) { | |
8807 | dl->index = super->anchor->num_disks; | |
8808 | super->anchor->num_disks++; | |
8809 | } | |
8810 | disk = &dl->disk; | |
8811 | disk->status |= CONFIGURED_DISK; | |
8812 | disk->status &= ~SPARE_DISK; | |
8813 | ||
8814 | /* mark rebuild */ | |
8815 | to_state = imsm_check_degraded(super, dev, failed, MAP_0); | |
8816 | if (!second_map_created) { | |
8817 | second_map_created = 1; | |
8818 | map->map_state = IMSM_T_STATE_DEGRADED; | |
8819 | migrate(dev, super, to_state, MIGR_REBUILD); | |
8820 | } else | |
8821 | map->map_state = to_state; | |
8822 | migr_map = get_imsm_map(dev, MAP_1); | |
8823 | set_imsm_ord_tbl_ent(map, u->slot, dl->index); | |
8824 | set_imsm_ord_tbl_ent(migr_map, u->slot, | |
8825 | dl->index | IMSM_ORD_REBUILD); | |
8826 | ||
8827 | /* update the family_num to mark a new container | |
8828 | * generation, being careful to record the existing | |
8829 | * family_num in orig_family_num to clean up after | |
8830 | * earlier mdadm versions that neglected to set it. | |
8831 | */ | |
8832 | if (mpb->orig_family_num == 0) | |
8833 | mpb->orig_family_num = mpb->family_num; | |
8834 | mpb->family_num += super->random; | |
8835 | ||
8836 | /* count arrays using the victim in the metadata */ | |
8837 | found = 0; | |
8838 | for (a = active_array; a ; a = a->next) { | |
8839 | dev = get_imsm_dev(super, a->info.container_member); | |
8840 | map = get_imsm_map(dev, MAP_0); | |
8841 | ||
8842 | if (get_imsm_disk_slot(map, victim) >= 0) | |
8843 | found++; | |
8844 | } | |
8845 | ||
8846 | /* delete the victim if it is no longer being | |
8847 | * utilized anywhere | |
8848 | */ | |
8849 | if (!found) { | |
8850 | struct dl **dlp; | |
8851 | ||
8852 | /* We know that 'manager' isn't touching anything, | |
8853 | * so it is safe to delete | |
8854 | */ | |
8855 | for (dlp = &super->disks; *dlp; dlp = &(*dlp)->next) | |
8856 | if ((*dlp)->index == victim) | |
8857 | break; | |
8858 | ||
8859 | /* victim may be on the missing list */ | |
8860 | if (!*dlp) | |
8861 | for (dlp = &super->missing; *dlp; | |
8862 | dlp = &(*dlp)->next) | |
8863 | if ((*dlp)->index == victim) | |
8864 | break; | |
8865 | imsm_delete(super, dlp, victim); | |
8866 | } | |
8867 | } | |
8868 | ||
8869 | return 1; | |
8870 | } | |
8871 | ||
8872 | static int apply_reshape_container_disks_update(struct imsm_update_reshape *u, | |
8873 | struct intel_super *super, | |
8874 | void ***space_list) | |
8875 | { | |
8876 | struct dl *new_disk; | |
8877 | struct intel_dev *id; | |
8878 | int i; | |
8879 | int delta_disks = u->new_raid_disks - u->old_raid_disks; | |
8880 | int disk_count = u->old_raid_disks; | |
8881 | void **tofree = NULL; | |
8882 | int devices_to_reshape = 1; | |
8883 | struct imsm_super *mpb = super->anchor; | |
8884 | int ret_val = 0; | |
8885 | unsigned int dev_id; | |
8886 | ||
8887 | dprintf("(enter)\n"); | |
8888 | ||
8889 | /* enable spares to use in array */ | |
8890 | for (i = 0; i < delta_disks; i++) { | |
8891 | new_disk = get_disk_super(super, | |
8892 | major(u->new_disks[i]), | |
8893 | minor(u->new_disks[i])); | |
8894 | dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n", | |
8895 | major(u->new_disks[i]), minor(u->new_disks[i]), | |
8896 | new_disk, new_disk->index); | |
8897 | if (new_disk == NULL || | |
8898 | (new_disk->index >= 0 && | |
8899 | new_disk->index < u->old_raid_disks)) | |
8900 | goto update_reshape_exit; | |
8901 | new_disk->index = disk_count++; | |
8902 | /* slot to fill in autolayout | |
8903 | */ | |
8904 | new_disk->raiddisk = new_disk->index; | |
8905 | new_disk->disk.status |= | |
8906 | CONFIGURED_DISK; | |
8907 | new_disk->disk.status &= ~SPARE_DISK; | |
8908 | } | |
8909 | ||
8910 | dprintf("imsm: volume set mpb->num_raid_devs = %i\n", | |
8911 | mpb->num_raid_devs); | |
8912 | /* manage changes in volume | |
8913 | */ | |
8914 | for (dev_id = 0; dev_id < mpb->num_raid_devs; dev_id++) { | |
8915 | void **sp = *space_list; | |
8916 | struct imsm_dev *newdev; | |
8917 | struct imsm_map *newmap, *oldmap; | |
8918 | ||
8919 | for (id = super->devlist ; id; id = id->next) { | |
8920 | if (id->index == dev_id) | |
8921 | break; | |
8922 | } | |
8923 | if (id == NULL) | |
8924 | break; | |
8925 | if (!sp) | |
8926 | continue; | |
8927 | *space_list = *sp; | |
8928 | newdev = (void*)sp; | |
8929 | /* Copy the dev, but not (all of) the map */ | |
8930 | memcpy(newdev, id->dev, sizeof(*newdev)); | |
8931 | oldmap = get_imsm_map(id->dev, MAP_0); | |
8932 | newmap = get_imsm_map(newdev, MAP_0); | |
8933 | /* Copy the current map */ | |
8934 | memcpy(newmap, oldmap, sizeof_imsm_map(oldmap)); | |
8935 | /* update one device only | |
8936 | */ | |
8937 | if (devices_to_reshape) { | |
8938 | dprintf("imsm: modifying subdev: %i\n", | |
8939 | id->index); | |
8940 | devices_to_reshape--; | |
8941 | newdev->vol.migr_state = 1; | |
8942 | newdev->vol.curr_migr_unit = 0; | |
8943 | set_migr_type(newdev, MIGR_GEN_MIGR); | |
8944 | newmap->num_members = u->new_raid_disks; | |
8945 | for (i = 0; i < delta_disks; i++) { | |
8946 | set_imsm_ord_tbl_ent(newmap, | |
8947 | u->old_raid_disks + i, | |
8948 | u->old_raid_disks + i); | |
8949 | } | |
8950 | /* New map is correct, now need to save old map | |
8951 | */ | |
8952 | newmap = get_imsm_map(newdev, MAP_1); | |
8953 | memcpy(newmap, oldmap, sizeof_imsm_map(oldmap)); | |
8954 | ||
8955 | imsm_set_array_size(newdev, -1); | |
8956 | } | |
8957 | ||
8958 | sp = (void **)id->dev; | |
8959 | id->dev = newdev; | |
8960 | *sp = tofree; | |
8961 | tofree = sp; | |
8962 | ||
8963 | /* Clear migration record */ | |
8964 | memset(super->migr_rec, 0, sizeof(struct migr_record)); | |
8965 | } | |
8966 | if (tofree) | |
8967 | *space_list = tofree; | |
8968 | ret_val = 1; | |
8969 | ||
8970 | update_reshape_exit: | |
8971 | ||
8972 | return ret_val; | |
8973 | } | |
8974 | ||
8975 | static int apply_takeover_update(struct imsm_update_takeover *u, | |
8976 | struct intel_super *super, | |
8977 | void ***space_list) | |
8978 | { | |
8979 | struct imsm_dev *dev = NULL; | |
8980 | struct intel_dev *dv; | |
8981 | struct imsm_dev *dev_new; | |
8982 | struct imsm_map *map; | |
8983 | struct dl *dm, *du; | |
8984 | int i; | |
8985 | ||
8986 | for (dv = super->devlist; dv; dv = dv->next) | |
8987 | if (dv->index == (unsigned int)u->subarray) { | |
8988 | dev = dv->dev; | |
8989 | break; | |
8990 | } | |
8991 | ||
8992 | if (dev == NULL) | |
8993 | return 0; | |
8994 | ||
8995 | map = get_imsm_map(dev, MAP_0); | |
8996 | ||
8997 | if (u->direction == R10_TO_R0) { | |
8998 | unsigned long long num_data_stripes; | |
8999 | ||
9000 | map->num_domains = 1; | |
9001 | num_data_stripes = blocks_per_member(map); | |
9002 | num_data_stripes /= map->blocks_per_strip; | |
9003 | num_data_stripes /= map->num_domains; | |
9004 | set_num_data_stripes(map, num_data_stripes); | |
9005 | ||
9006 | /* Number of failed disks must be half of initial disk number */ | |
9007 | if (imsm_count_failed(super, dev, MAP_0) != | |
9008 | (map->num_members / 2)) | |
9009 | return 0; | |
9010 | ||
9011 | /* iterate through devices to mark removed disks as spare */ | |
9012 | for (dm = super->disks; dm; dm = dm->next) { | |
9013 | if (dm->disk.status & FAILED_DISK) { | |
9014 | int idx = dm->index; | |
9015 | /* update indexes on the disk list */ | |
9016 | /* FIXME this loop-with-the-loop looks wrong, I'm not convinced | |
9017 | the index values will end up being correct.... NB */ | |
9018 | for (du = super->disks; du; du = du->next) | |
9019 | if (du->index > idx) | |
9020 | du->index--; | |
9021 | /* mark as spare disk */ | |
9022 | mark_spare(dm); | |
9023 | } | |
9024 | } | |
9025 | /* update map */ | |
9026 | map->num_members = map->num_members / 2; | |
9027 | map->map_state = IMSM_T_STATE_NORMAL; | |
9028 | map->num_domains = 1; | |
9029 | map->raid_level = 0; | |
9030 | map->failed_disk_num = -1; | |
9031 | } | |
9032 | ||
9033 | if (u->direction == R0_TO_R10) { | |
9034 | void **space; | |
9035 | /* update slots in current disk list */ | |
9036 | for (dm = super->disks; dm; dm = dm->next) { | |
9037 | if (dm->index >= 0) | |
9038 | dm->index *= 2; | |
9039 | } | |
9040 | /* create new *missing* disks */ | |
9041 | for (i = 0; i < map->num_members; i++) { | |
9042 | space = *space_list; | |
9043 | if (!space) | |
9044 | continue; | |
9045 | *space_list = *space; | |
9046 | du = (void *)space; | |
9047 | memcpy(du, super->disks, sizeof(*du)); | |
9048 | du->fd = -1; | |
9049 | du->minor = 0; | |
9050 | du->major = 0; | |
9051 | du->index = (i * 2) + 1; | |
9052 | sprintf((char *)du->disk.serial, | |
9053 | " MISSING_%d", du->index); | |
9054 | sprintf((char *)du->serial, | |
9055 | "MISSING_%d", du->index); | |
9056 | du->next = super->missing; | |
9057 | super->missing = du; | |
9058 | } | |
9059 | /* create new dev and map */ | |
9060 | space = *space_list; | |
9061 | if (!space) | |
9062 | return 0; | |
9063 | *space_list = *space; | |
9064 | dev_new = (void *)space; | |
9065 | memcpy(dev_new, dev, sizeof(*dev)); | |
9066 | /* update new map */ | |
9067 | map = get_imsm_map(dev_new, MAP_0); | |
9068 | map->num_members = map->num_members * 2; | |
9069 | map->map_state = IMSM_T_STATE_DEGRADED; | |
9070 | map->num_domains = 2; | |
9071 | map->raid_level = 1; | |
9072 | /* replace dev<->dev_new */ | |
9073 | dv->dev = dev_new; | |
9074 | } | |
9075 | /* update disk order table */ | |
9076 | for (du = super->disks; du; du = du->next) | |
9077 | if (du->index >= 0) | |
9078 | set_imsm_ord_tbl_ent(map, du->index, du->index); | |
9079 | for (du = super->missing; du; du = du->next) | |
9080 | if (du->index >= 0) { | |
9081 | set_imsm_ord_tbl_ent(map, du->index, du->index); | |
9082 | mark_missing(super, dv->dev, &du->disk, du->index); | |
9083 | } | |
9084 | ||
9085 | return 1; | |
9086 | } | |
9087 | ||
9088 | static void imsm_process_update(struct supertype *st, | |
9089 | struct metadata_update *update) | |
9090 | { | |
9091 | /** | |
9092 | * crack open the metadata_update envelope to find the update record | |
9093 | * update can be one of: | |
9094 | * update_reshape_container_disks - all the arrays in the container | |
9095 | * are being reshaped to have more devices. We need to mark | |
9096 | * the arrays for general migration and convert selected spares | |
9097 | * into active devices. | |
9098 | * update_activate_spare - a spare device has replaced a failed | |
9099 | * device in an array, update the disk_ord_tbl. If this disk is | |
9100 | * present in all member arrays then also clear the SPARE_DISK | |
9101 | * flag | |
9102 | * update_create_array | |
9103 | * update_kill_array | |
9104 | * update_rename_array | |
9105 | * update_add_remove_disk | |
9106 | */ | |
9107 | struct intel_super *super = st->sb; | |
9108 | struct imsm_super *mpb; | |
9109 | enum imsm_update_type type = *(enum imsm_update_type *) update->buf; | |
9110 | ||
9111 | /* update requires a larger buf but the allocation failed */ | |
9112 | if (super->next_len && !super->next_buf) { | |
9113 | super->next_len = 0; | |
9114 | return; | |
9115 | } | |
9116 | ||
9117 | if (super->next_buf) { | |
9118 | memcpy(super->next_buf, super->buf, super->len); | |
9119 | free(super->buf); | |
9120 | super->len = super->next_len; | |
9121 | super->buf = super->next_buf; | |
9122 | ||
9123 | super->next_len = 0; | |
9124 | super->next_buf = NULL; | |
9125 | } | |
9126 | ||
9127 | mpb = super->anchor; | |
9128 | ||
9129 | switch (type) { | |
9130 | case update_general_migration_checkpoint: { | |
9131 | struct intel_dev *id; | |
9132 | struct imsm_update_general_migration_checkpoint *u = | |
9133 | (void *)update->buf; | |
9134 | ||
9135 | dprintf("called for update_general_migration_checkpoint\n"); | |
9136 | ||
9137 | /* find device under general migration */ | |
9138 | for (id = super->devlist ; id; id = id->next) { | |
9139 | if (is_gen_migration(id->dev)) { | |
9140 | id->dev->vol.curr_migr_unit = | |
9141 | __cpu_to_le32(u->curr_migr_unit); | |
9142 | super->updates_pending++; | |
9143 | } | |
9144 | } | |
9145 | break; | |
9146 | } | |
9147 | case update_takeover: { | |
9148 | struct imsm_update_takeover *u = (void *)update->buf; | |
9149 | if (apply_takeover_update(u, super, &update->space_list)) { | |
9150 | imsm_update_version_info(super); | |
9151 | super->updates_pending++; | |
9152 | } | |
9153 | break; | |
9154 | } | |
9155 | ||
9156 | case update_reshape_container_disks: { | |
9157 | struct imsm_update_reshape *u = (void *)update->buf; | |
9158 | if (apply_reshape_container_disks_update( | |
9159 | u, super, &update->space_list)) | |
9160 | super->updates_pending++; | |
9161 | break; | |
9162 | } | |
9163 | case update_reshape_migration: { | |
9164 | struct imsm_update_reshape_migration *u = (void *)update->buf; | |
9165 | if (apply_reshape_migration_update( | |
9166 | u, super, &update->space_list)) | |
9167 | super->updates_pending++; | |
9168 | break; | |
9169 | } | |
9170 | case update_size_change: { | |
9171 | struct imsm_update_size_change *u = (void *)update->buf; | |
9172 | if (apply_size_change_update(u, super)) | |
9173 | super->updates_pending++; | |
9174 | break; | |
9175 | } | |
9176 | case update_activate_spare: { | |
9177 | struct imsm_update_activate_spare *u = (void *) update->buf; | |
9178 | if (apply_update_activate_spare(u, super, st->arrays)) | |
9179 | super->updates_pending++; | |
9180 | break; | |
9181 | } | |
9182 | case update_create_array: { | |
9183 | /* someone wants to create a new array, we need to be aware of | |
9184 | * a few races/collisions: | |
9185 | * 1/ 'Create' called by two separate instances of mdadm | |
9186 | * 2/ 'Create' versus 'activate_spare': mdadm has chosen | |
9187 | * devices that have since been assimilated via | |
9188 | * activate_spare. | |
9189 | * In the event this update can not be carried out mdadm will | |
9190 | * (FIX ME) notice that its update did not take hold. | |
9191 | */ | |
9192 | struct imsm_update_create_array *u = (void *) update->buf; | |
9193 | struct intel_dev *dv; | |
9194 | struct imsm_dev *dev; | |
9195 | struct imsm_map *map, *new_map; | |
9196 | unsigned long long start, end; | |
9197 | unsigned long long new_start, new_end; | |
9198 | int i; | |
9199 | struct disk_info *inf; | |
9200 | struct dl *dl; | |
9201 | ||
9202 | /* handle racing creates: first come first serve */ | |
9203 | if (u->dev_idx < mpb->num_raid_devs) { | |
9204 | dprintf("subarray %d already defined\n", u->dev_idx); | |
9205 | goto create_error; | |
9206 | } | |
9207 | ||
9208 | /* check update is next in sequence */ | |
9209 | if (u->dev_idx != mpb->num_raid_devs) { | |
9210 | dprintf("can not create array %d expected index %d\n", | |
9211 | u->dev_idx, mpb->num_raid_devs); | |
9212 | goto create_error; | |
9213 | } | |
9214 | ||
9215 | new_map = get_imsm_map(&u->dev, MAP_0); | |
9216 | new_start = pba_of_lba0(new_map); | |
9217 | new_end = new_start + blocks_per_member(new_map); | |
9218 | inf = get_disk_info(u); | |
9219 | ||
9220 | /* handle activate_spare versus create race: | |
9221 | * check to make sure that overlapping arrays do not include | |
9222 | * overalpping disks | |
9223 | */ | |
9224 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
9225 | dev = get_imsm_dev(super, i); | |
9226 | map = get_imsm_map(dev, MAP_0); | |
9227 | start = pba_of_lba0(map); | |
9228 | end = start + blocks_per_member(map); | |
9229 | if ((new_start >= start && new_start <= end) || | |
9230 | (start >= new_start && start <= new_end)) | |
9231 | /* overlap */; | |
9232 | else | |
9233 | continue; | |
9234 | ||
9235 | if (disks_overlap(super, i, u)) { | |
9236 | dprintf("arrays overlap\n"); | |
9237 | goto create_error; | |
9238 | } | |
9239 | } | |
9240 | ||
9241 | /* check that prepare update was successful */ | |
9242 | if (!update->space) { | |
9243 | dprintf("prepare update failed\n"); | |
9244 | goto create_error; | |
9245 | } | |
9246 | ||
9247 | /* check that all disks are still active before committing | |
9248 | * changes. FIXME: could we instead handle this by creating a | |
9249 | * degraded array? That's probably not what the user expects, | |
9250 | * so better to drop this update on the floor. | |
9251 | */ | |
9252 | for (i = 0; i < new_map->num_members; i++) { | |
9253 | dl = serial_to_dl(inf[i].serial, super); | |
9254 | if (!dl) { | |
9255 | dprintf("disk disappeared\n"); | |
9256 | goto create_error; | |
9257 | } | |
9258 | } | |
9259 | ||
9260 | super->updates_pending++; | |
9261 | ||
9262 | /* convert spares to members and fixup ord_tbl */ | |
9263 | for (i = 0; i < new_map->num_members; i++) { | |
9264 | dl = serial_to_dl(inf[i].serial, super); | |
9265 | if (dl->index == -1) { | |
9266 | dl->index = mpb->num_disks; | |
9267 | mpb->num_disks++; | |
9268 | dl->disk.status |= CONFIGURED_DISK; | |
9269 | dl->disk.status &= ~SPARE_DISK; | |
9270 | } | |
9271 | set_imsm_ord_tbl_ent(new_map, i, dl->index); | |
9272 | } | |
9273 | ||
9274 | dv = update->space; | |
9275 | dev = dv->dev; | |
9276 | update->space = NULL; | |
9277 | imsm_copy_dev(dev, &u->dev); | |
9278 | dv->index = u->dev_idx; | |
9279 | dv->next = super->devlist; | |
9280 | super->devlist = dv; | |
9281 | mpb->num_raid_devs++; | |
9282 | ||
9283 | imsm_update_version_info(super); | |
9284 | break; | |
9285 | create_error: | |
9286 | /* mdmon knows how to release update->space, but not | |
9287 | * ((struct intel_dev *) update->space)->dev | |
9288 | */ | |
9289 | if (update->space) { | |
9290 | dv = update->space; | |
9291 | free(dv->dev); | |
9292 | } | |
9293 | break; | |
9294 | } | |
9295 | case update_kill_array: { | |
9296 | struct imsm_update_kill_array *u = (void *) update->buf; | |
9297 | int victim = u->dev_idx; | |
9298 | struct active_array *a; | |
9299 | struct intel_dev **dp; | |
9300 | struct imsm_dev *dev; | |
9301 | ||
9302 | /* sanity check that we are not affecting the uuid of | |
9303 | * active arrays, or deleting an active array | |
9304 | * | |
9305 | * FIXME when immutable ids are available, but note that | |
9306 | * we'll also need to fixup the invalidated/active | |
9307 | * subarray indexes in mdstat | |
9308 | */ | |
9309 | for (a = st->arrays; a; a = a->next) | |
9310 | if (a->info.container_member >= victim) | |
9311 | break; | |
9312 | /* by definition if mdmon is running at least one array | |
9313 | * is active in the container, so checking | |
9314 | * mpb->num_raid_devs is just extra paranoia | |
9315 | */ | |
9316 | dev = get_imsm_dev(super, victim); | |
9317 | if (a || !dev || mpb->num_raid_devs == 1) { | |
9318 | dprintf("failed to delete subarray-%d\n", victim); | |
9319 | break; | |
9320 | } | |
9321 | ||
9322 | for (dp = &super->devlist; *dp;) | |
9323 | if ((*dp)->index == (unsigned)super->current_vol) { | |
9324 | *dp = (*dp)->next; | |
9325 | } else { | |
9326 | if ((*dp)->index > (unsigned)victim) | |
9327 | (*dp)->index--; | |
9328 | dp = &(*dp)->next; | |
9329 | } | |
9330 | mpb->num_raid_devs--; | |
9331 | super->updates_pending++; | |
9332 | break; | |
9333 | } | |
9334 | case update_rename_array: { | |
9335 | struct imsm_update_rename_array *u = (void *) update->buf; | |
9336 | char name[MAX_RAID_SERIAL_LEN+1]; | |
9337 | int target = u->dev_idx; | |
9338 | struct active_array *a; | |
9339 | struct imsm_dev *dev; | |
9340 | ||
9341 | /* sanity check that we are not affecting the uuid of | |
9342 | * an active array | |
9343 | */ | |
9344 | snprintf(name, MAX_RAID_SERIAL_LEN, "%s", (char *) u->name); | |
9345 | name[MAX_RAID_SERIAL_LEN] = '\0'; | |
9346 | for (a = st->arrays; a; a = a->next) | |
9347 | if (a->info.container_member == target) | |
9348 | break; | |
9349 | dev = get_imsm_dev(super, u->dev_idx); | |
9350 | if (a || !dev || !check_name(super, name, 1)) { | |
9351 | dprintf("failed to rename subarray-%d\n", target); | |
9352 | break; | |
9353 | } | |
9354 | ||
9355 | snprintf((char *) dev->volume, MAX_RAID_SERIAL_LEN, "%s", name); | |
9356 | super->updates_pending++; | |
9357 | break; | |
9358 | } | |
9359 | case update_add_remove_disk: { | |
9360 | /* we may be able to repair some arrays if disks are | |
9361 | * being added, check the status of add_remove_disk | |
9362 | * if discs has been added. | |
9363 | */ | |
9364 | if (add_remove_disk_update(super)) { | |
9365 | struct active_array *a; | |
9366 | ||
9367 | super->updates_pending++; | |
9368 | for (a = st->arrays; a; a = a->next) | |
9369 | a->check_degraded = 1; | |
9370 | } | |
9371 | break; | |
9372 | } | |
9373 | case update_prealloc_badblocks_mem: | |
9374 | break; | |
9375 | default: | |
9376 | pr_err("error: unsuported process update type:(type: %d)\n", type); | |
9377 | } | |
9378 | } | |
9379 | ||
9380 | static struct mdinfo *get_spares_for_grow(struct supertype *st); | |
9381 | ||
9382 | static int imsm_prepare_update(struct supertype *st, | |
9383 | struct metadata_update *update) | |
9384 | { | |
9385 | /** | |
9386 | * Allocate space to hold new disk entries, raid-device entries or a new | |
9387 | * mpb if necessary. The manager synchronously waits for updates to | |
9388 | * complete in the monitor, so new mpb buffers allocated here can be | |
9389 | * integrated by the monitor thread without worrying about live pointers | |
9390 | * in the manager thread. | |
9391 | */ | |
9392 | enum imsm_update_type type; | |
9393 | struct intel_super *super = st->sb; | |
9394 | unsigned int sector_size = super->sector_size; | |
9395 | struct imsm_super *mpb = super->anchor; | |
9396 | size_t buf_len; | |
9397 | size_t len = 0; | |
9398 | ||
9399 | if (update->len < (int)sizeof(type)) | |
9400 | return 0; | |
9401 | ||
9402 | type = *(enum imsm_update_type *) update->buf; | |
9403 | ||
9404 | switch (type) { | |
9405 | case update_general_migration_checkpoint: | |
9406 | if (update->len < (int)sizeof(struct imsm_update_general_migration_checkpoint)) | |
9407 | return 0; | |
9408 | dprintf("called for update_general_migration_checkpoint\n"); | |
9409 | break; | |
9410 | case update_takeover: { | |
9411 | struct imsm_update_takeover *u = (void *)update->buf; | |
9412 | if (update->len < (int)sizeof(*u)) | |
9413 | return 0; | |
9414 | if (u->direction == R0_TO_R10) { | |
9415 | void **tail = (void **)&update->space_list; | |
9416 | struct imsm_dev *dev = get_imsm_dev(super, u->subarray); | |
9417 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
9418 | int num_members = map->num_members; | |
9419 | void *space; | |
9420 | int size, i; | |
9421 | /* allocate memory for added disks */ | |
9422 | for (i = 0; i < num_members; i++) { | |
9423 | size = sizeof(struct dl); | |
9424 | space = xmalloc(size); | |
9425 | *tail = space; | |
9426 | tail = space; | |
9427 | *tail = NULL; | |
9428 | } | |
9429 | /* allocate memory for new device */ | |
9430 | size = sizeof_imsm_dev(super->devlist->dev, 0) + | |
9431 | (num_members * sizeof(__u32)); | |
9432 | space = xmalloc(size); | |
9433 | *tail = space; | |
9434 | tail = space; | |
9435 | *tail = NULL; | |
9436 | len = disks_to_mpb_size(num_members * 2); | |
9437 | } | |
9438 | ||
9439 | break; | |
9440 | } | |
9441 | case update_reshape_container_disks: { | |
9442 | /* Every raid device in the container is about to | |
9443 | * gain some more devices, and we will enter a | |
9444 | * reconfiguration. | |
9445 | * So each 'imsm_map' will be bigger, and the imsm_vol | |
9446 | * will now hold 2 of them. | |
9447 | * Thus we need new 'struct imsm_dev' allocations sized | |
9448 | * as sizeof_imsm_dev but with more devices in both maps. | |
9449 | */ | |
9450 | struct imsm_update_reshape *u = (void *)update->buf; | |
9451 | struct intel_dev *dl; | |
9452 | void **space_tail = (void**)&update->space_list; | |
9453 | ||
9454 | if (update->len < (int)sizeof(*u)) | |
9455 | return 0; | |
9456 | ||
9457 | dprintf("for update_reshape\n"); | |
9458 | ||
9459 | for (dl = super->devlist; dl; dl = dl->next) { | |
9460 | int size = sizeof_imsm_dev(dl->dev, 1); | |
9461 | void *s; | |
9462 | if (u->new_raid_disks > u->old_raid_disks) | |
9463 | size += sizeof(__u32)*2* | |
9464 | (u->new_raid_disks - u->old_raid_disks); | |
9465 | s = xmalloc(size); | |
9466 | *space_tail = s; | |
9467 | space_tail = s; | |
9468 | *space_tail = NULL; | |
9469 | } | |
9470 | ||
9471 | len = disks_to_mpb_size(u->new_raid_disks); | |
9472 | dprintf("New anchor length is %llu\n", (unsigned long long)len); | |
9473 | break; | |
9474 | } | |
9475 | case update_reshape_migration: { | |
9476 | /* for migration level 0->5 we need to add disks | |
9477 | * so the same as for container operation we will copy | |
9478 | * device to the bigger location. | |
9479 | * in memory prepared device and new disk area are prepared | |
9480 | * for usage in process update | |
9481 | */ | |
9482 | struct imsm_update_reshape_migration *u = (void *)update->buf; | |
9483 | struct intel_dev *id; | |
9484 | void **space_tail = (void **)&update->space_list; | |
9485 | int size; | |
9486 | void *s; | |
9487 | int current_level = -1; | |
9488 | ||
9489 | if (update->len < (int)sizeof(*u)) | |
9490 | return 0; | |
9491 | ||
9492 | dprintf("for update_reshape\n"); | |
9493 | ||
9494 | /* add space for bigger array in update | |
9495 | */ | |
9496 | for (id = super->devlist; id; id = id->next) { | |
9497 | if (id->index == (unsigned)u->subdev) { | |
9498 | size = sizeof_imsm_dev(id->dev, 1); | |
9499 | if (u->new_raid_disks > u->old_raid_disks) | |
9500 | size += sizeof(__u32)*2* | |
9501 | (u->new_raid_disks - u->old_raid_disks); | |
9502 | s = xmalloc(size); | |
9503 | *space_tail = s; | |
9504 | space_tail = s; | |
9505 | *space_tail = NULL; | |
9506 | break; | |
9507 | } | |
9508 | } | |
9509 | if (update->space_list == NULL) | |
9510 | break; | |
9511 | ||
9512 | /* add space for disk in update | |
9513 | */ | |
9514 | size = sizeof(struct dl); | |
9515 | s = xmalloc(size); | |
9516 | *space_tail = s; | |
9517 | space_tail = s; | |
9518 | *space_tail = NULL; | |
9519 | ||
9520 | /* add spare device to update | |
9521 | */ | |
9522 | for (id = super->devlist ; id; id = id->next) | |
9523 | if (id->index == (unsigned)u->subdev) { | |
9524 | struct imsm_dev *dev; | |
9525 | struct imsm_map *map; | |
9526 | ||
9527 | dev = get_imsm_dev(super, u->subdev); | |
9528 | map = get_imsm_map(dev, MAP_0); | |
9529 | current_level = map->raid_level; | |
9530 | break; | |
9531 | } | |
9532 | if (u->new_level == 5 && u->new_level != current_level) { | |
9533 | struct mdinfo *spares; | |
9534 | ||
9535 | spares = get_spares_for_grow(st); | |
9536 | if (spares) { | |
9537 | struct dl *dl; | |
9538 | struct mdinfo *dev; | |
9539 | ||
9540 | dev = spares->devs; | |
9541 | if (dev) { | |
9542 | u->new_disks[0] = | |
9543 | makedev(dev->disk.major, | |
9544 | dev->disk.minor); | |
9545 | dl = get_disk_super(super, | |
9546 | dev->disk.major, | |
9547 | dev->disk.minor); | |
9548 | dl->index = u->old_raid_disks; | |
9549 | dev = dev->next; | |
9550 | } | |
9551 | sysfs_free(spares); | |
9552 | } | |
9553 | } | |
9554 | len = disks_to_mpb_size(u->new_raid_disks); | |
9555 | dprintf("New anchor length is %llu\n", (unsigned long long)len); | |
9556 | break; | |
9557 | } | |
9558 | case update_size_change: { | |
9559 | if (update->len < (int)sizeof(struct imsm_update_size_change)) | |
9560 | return 0; | |
9561 | break; | |
9562 | } | |
9563 | case update_activate_spare: { | |
9564 | if (update->len < (int)sizeof(struct imsm_update_activate_spare)) | |
9565 | return 0; | |
9566 | break; | |
9567 | } | |
9568 | case update_create_array: { | |
9569 | struct imsm_update_create_array *u = (void *) update->buf; | |
9570 | struct intel_dev *dv; | |
9571 | struct imsm_dev *dev = &u->dev; | |
9572 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
9573 | struct dl *dl; | |
9574 | struct disk_info *inf; | |
9575 | int i; | |
9576 | int activate = 0; | |
9577 | ||
9578 | if (update->len < (int)sizeof(*u)) | |
9579 | return 0; | |
9580 | ||
9581 | inf = get_disk_info(u); | |
9582 | len = sizeof_imsm_dev(dev, 1); | |
9583 | /* allocate a new super->devlist entry */ | |
9584 | dv = xmalloc(sizeof(*dv)); | |
9585 | dv->dev = xmalloc(len); | |
9586 | update->space = dv; | |
9587 | ||
9588 | /* count how many spares will be converted to members */ | |
9589 | for (i = 0; i < map->num_members; i++) { | |
9590 | dl = serial_to_dl(inf[i].serial, super); | |
9591 | if (!dl) { | |
9592 | /* hmm maybe it failed?, nothing we can do about | |
9593 | * it here | |
9594 | */ | |
9595 | continue; | |
9596 | } | |
9597 | if (count_memberships(dl, super) == 0) | |
9598 | activate++; | |
9599 | } | |
9600 | len += activate * sizeof(struct imsm_disk); | |
9601 | break; | |
9602 | } | |
9603 | case update_kill_array: { | |
9604 | if (update->len < (int)sizeof(struct imsm_update_kill_array)) | |
9605 | return 0; | |
9606 | break; | |
9607 | } | |
9608 | case update_rename_array: { | |
9609 | if (update->len < (int)sizeof(struct imsm_update_rename_array)) | |
9610 | return 0; | |
9611 | break; | |
9612 | } | |
9613 | case update_add_remove_disk: | |
9614 | /* no update->len needed */ | |
9615 | break; | |
9616 | case update_prealloc_badblocks_mem: | |
9617 | super->extra_space += sizeof(struct bbm_log) - | |
9618 | get_imsm_bbm_log_size(super->bbm_log); | |
9619 | break; | |
9620 | default: | |
9621 | return 0; | |
9622 | } | |
9623 | ||
9624 | /* check if we need a larger metadata buffer */ | |
9625 | if (super->next_buf) | |
9626 | buf_len = super->next_len; | |
9627 | else | |
9628 | buf_len = super->len; | |
9629 | ||
9630 | if (__le32_to_cpu(mpb->mpb_size) + super->extra_space + len > buf_len) { | |
9631 | /* ok we need a larger buf than what is currently allocated | |
9632 | * if this allocation fails process_update will notice that | |
9633 | * ->next_len is set and ->next_buf is NULL | |
9634 | */ | |
9635 | buf_len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + | |
9636 | super->extra_space + len, sector_size); | |
9637 | if (super->next_buf) | |
9638 | free(super->next_buf); | |
9639 | ||
9640 | super->next_len = buf_len; | |
9641 | if (posix_memalign(&super->next_buf, sector_size, buf_len) == 0) | |
9642 | memset(super->next_buf, 0, buf_len); | |
9643 | else | |
9644 | super->next_buf = NULL; | |
9645 | } | |
9646 | return 1; | |
9647 | } | |
9648 | ||
9649 | /* must be called while manager is quiesced */ | |
9650 | static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index) | |
9651 | { | |
9652 | struct imsm_super *mpb = super->anchor; | |
9653 | struct dl *iter; | |
9654 | struct imsm_dev *dev; | |
9655 | struct imsm_map *map; | |
9656 | unsigned int i, j, num_members; | |
9657 | __u32 ord; | |
9658 | struct bbm_log *log = super->bbm_log; | |
9659 | ||
9660 | dprintf("deleting device[%d] from imsm_super\n", index); | |
9661 | ||
9662 | /* shift all indexes down one */ | |
9663 | for (iter = super->disks; iter; iter = iter->next) | |
9664 | if (iter->index > (int)index) | |
9665 | iter->index--; | |
9666 | for (iter = super->missing; iter; iter = iter->next) | |
9667 | if (iter->index > (int)index) | |
9668 | iter->index--; | |
9669 | ||
9670 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
9671 | dev = get_imsm_dev(super, i); | |
9672 | map = get_imsm_map(dev, MAP_0); | |
9673 | num_members = map->num_members; | |
9674 | for (j = 0; j < num_members; j++) { | |
9675 | /* update ord entries being careful not to propagate | |
9676 | * ord-flags to the first map | |
9677 | */ | |
9678 | ord = get_imsm_ord_tbl_ent(dev, j, MAP_X); | |
9679 | ||
9680 | if (ord_to_idx(ord) <= index) | |
9681 | continue; | |
9682 | ||
9683 | map = get_imsm_map(dev, MAP_0); | |
9684 | set_imsm_ord_tbl_ent(map, j, ord_to_idx(ord - 1)); | |
9685 | map = get_imsm_map(dev, MAP_1); | |
9686 | if (map) | |
9687 | set_imsm_ord_tbl_ent(map, j, ord - 1); | |
9688 | } | |
9689 | } | |
9690 | ||
9691 | for (i = 0; i < log->entry_count; i++) { | |
9692 | struct bbm_log_entry *entry = &log->marked_block_entries[i]; | |
9693 | ||
9694 | if (entry->disk_ordinal <= index) | |
9695 | continue; | |
9696 | entry->disk_ordinal--; | |
9697 | } | |
9698 | ||
9699 | mpb->num_disks--; | |
9700 | super->updates_pending++; | |
9701 | if (*dlp) { | |
9702 | struct dl *dl = *dlp; | |
9703 | ||
9704 | *dlp = (*dlp)->next; | |
9705 | __free_imsm_disk(dl); | |
9706 | } | |
9707 | } | |
9708 | #endif /* MDASSEMBLE */ | |
9709 | ||
9710 | static void close_targets(int *targets, int new_disks) | |
9711 | { | |
9712 | int i; | |
9713 | ||
9714 | if (!targets) | |
9715 | return; | |
9716 | ||
9717 | for (i = 0; i < new_disks; i++) { | |
9718 | if (targets[i] >= 0) { | |
9719 | close(targets[i]); | |
9720 | targets[i] = -1; | |
9721 | } | |
9722 | } | |
9723 | } | |
9724 | ||
9725 | static int imsm_get_allowed_degradation(int level, int raid_disks, | |
9726 | struct intel_super *super, | |
9727 | struct imsm_dev *dev) | |
9728 | { | |
9729 | switch (level) { | |
9730 | case 1: | |
9731 | case 10:{ | |
9732 | int ret_val = 0; | |
9733 | struct imsm_map *map; | |
9734 | int i; | |
9735 | ||
9736 | ret_val = raid_disks/2; | |
9737 | /* check map if all disks pairs not failed | |
9738 | * in both maps | |
9739 | */ | |
9740 | map = get_imsm_map(dev, MAP_0); | |
9741 | for (i = 0; i < ret_val; i++) { | |
9742 | int degradation = 0; | |
9743 | if (get_imsm_disk(super, i) == NULL) | |
9744 | degradation++; | |
9745 | if (get_imsm_disk(super, i + 1) == NULL) | |
9746 | degradation++; | |
9747 | if (degradation == 2) | |
9748 | return 0; | |
9749 | } | |
9750 | map = get_imsm_map(dev, MAP_1); | |
9751 | /* if there is no second map | |
9752 | * result can be returned | |
9753 | */ | |
9754 | if (map == NULL) | |
9755 | return ret_val; | |
9756 | /* check degradation in second map | |
9757 | */ | |
9758 | for (i = 0; i < ret_val; i++) { | |
9759 | int degradation = 0; | |
9760 | if (get_imsm_disk(super, i) == NULL) | |
9761 | degradation++; | |
9762 | if (get_imsm_disk(super, i + 1) == NULL) | |
9763 | degradation++; | |
9764 | if (degradation == 2) | |
9765 | return 0; | |
9766 | } | |
9767 | return ret_val; | |
9768 | } | |
9769 | case 5: | |
9770 | return 1; | |
9771 | case 6: | |
9772 | return 2; | |
9773 | default: | |
9774 | return 0; | |
9775 | } | |
9776 | } | |
9777 | ||
9778 | /******************************************************************************* | |
9779 | * Function: open_backup_targets | |
9780 | * Description: Function opens file descriptors for all devices given in | |
9781 | * info->devs | |
9782 | * Parameters: | |
9783 | * info : general array info | |
9784 | * raid_disks : number of disks | |
9785 | * raid_fds : table of device's file descriptors | |
9786 | * super : intel super for raid10 degradation check | |
9787 | * dev : intel device for raid10 degradation check | |
9788 | * Returns: | |
9789 | * 0 : success | |
9790 | * -1 : fail | |
9791 | ******************************************************************************/ | |
9792 | int open_backup_targets(struct mdinfo *info, int raid_disks, int *raid_fds, | |
9793 | struct intel_super *super, struct imsm_dev *dev) | |
9794 | { | |
9795 | struct mdinfo *sd; | |
9796 | int i; | |
9797 | int opened = 0; | |
9798 | ||
9799 | for (i = 0; i < raid_disks; i++) | |
9800 | raid_fds[i] = -1; | |
9801 | ||
9802 | for (sd = info->devs ; sd ; sd = sd->next) { | |
9803 | char *dn; | |
9804 | ||
9805 | if (sd->disk.state & (1<<MD_DISK_FAULTY)) { | |
9806 | dprintf("disk is faulty!!\n"); | |
9807 | continue; | |
9808 | } | |
9809 | ||
9810 | if (sd->disk.raid_disk >= raid_disks || sd->disk.raid_disk < 0) | |
9811 | continue; | |
9812 | ||
9813 | dn = map_dev(sd->disk.major, | |
9814 | sd->disk.minor, 1); | |
9815 | raid_fds[sd->disk.raid_disk] = dev_open(dn, O_RDWR); | |
9816 | if (raid_fds[sd->disk.raid_disk] < 0) { | |
9817 | pr_err("cannot open component\n"); | |
9818 | continue; | |
9819 | } | |
9820 | opened++; | |
9821 | } | |
9822 | /* check if maximum array degradation level is not exceeded | |
9823 | */ | |
9824 | if ((raid_disks - opened) > | |
9825 | imsm_get_allowed_degradation(info->new_level, raid_disks, | |
9826 | super, dev)) { | |
9827 | pr_err("Not enough disks can be opened.\n"); | |
9828 | close_targets(raid_fds, raid_disks); | |
9829 | return -2; | |
9830 | } | |
9831 | return 0; | |
9832 | } | |
9833 | ||
9834 | /******************************************************************************* | |
9835 | * Function: validate_container_imsm | |
9836 | * Description: This routine validates container after assemble, | |
9837 | * eg. if devices in container are under the same controller. | |
9838 | * | |
9839 | * Parameters: | |
9840 | * info : linked list with info about devices used in array | |
9841 | * Returns: | |
9842 | * 1 : HBA mismatch | |
9843 | * 0 : Success | |
9844 | ******************************************************************************/ | |
9845 | int validate_container_imsm(struct mdinfo *info) | |
9846 | { | |
9847 | if (check_env("IMSM_NO_PLATFORM")) | |
9848 | return 0; | |
9849 | ||
9850 | struct sys_dev *idev; | |
9851 | struct sys_dev *hba = NULL; | |
9852 | struct sys_dev *intel_devices = find_intel_devices(); | |
9853 | char *dev_path = devt_to_devpath(makedev(info->disk.major, | |
9854 | info->disk.minor)); | |
9855 | ||
9856 | for (idev = intel_devices; idev; idev = idev->next) { | |
9857 | if (dev_path && strstr(dev_path, idev->path)) { | |
9858 | hba = idev; | |
9859 | break; | |
9860 | } | |
9861 | } | |
9862 | if (dev_path) | |
9863 | free(dev_path); | |
9864 | ||
9865 | if (!hba) { | |
9866 | pr_err("WARNING - Cannot detect HBA for device %s!\n", | |
9867 | devid2kname(makedev(info->disk.major, info->disk.minor))); | |
9868 | return 1; | |
9869 | } | |
9870 | ||
9871 | const struct imsm_orom *orom = get_orom_by_device_id(hba->dev_id); | |
9872 | struct mdinfo *dev; | |
9873 | ||
9874 | for (dev = info->next; dev; dev = dev->next) { | |
9875 | dev_path = devt_to_devpath(makedev(dev->disk.major, dev->disk.minor)); | |
9876 | ||
9877 | struct sys_dev *hba2 = NULL; | |
9878 | for (idev = intel_devices; idev; idev = idev->next) { | |
9879 | if (dev_path && strstr(dev_path, idev->path)) { | |
9880 | hba2 = idev; | |
9881 | break; | |
9882 | } | |
9883 | } | |
9884 | if (dev_path) | |
9885 | free(dev_path); | |
9886 | ||
9887 | const struct imsm_orom *orom2 = hba2 == NULL ? NULL : | |
9888 | get_orom_by_device_id(hba2->dev_id); | |
9889 | ||
9890 | if (hba2 && hba->type != hba2->type) { | |
9891 | pr_err("WARNING - HBAs of devices do not match %s != %s\n", | |
9892 | get_sys_dev_type(hba->type), get_sys_dev_type(hba2->type)); | |
9893 | return 1; | |
9894 | } | |
9895 | ||
9896 | if (orom != orom2) { | |
9897 | pr_err("WARNING - IMSM container assembled with disks under different HBAs!\n" | |
9898 | " This operation is not supported and can lead to data loss.\n"); | |
9899 | return 1; | |
9900 | } | |
9901 | ||
9902 | if (!orom) { | |
9903 | pr_err("WARNING - IMSM container assembled with disks under HBAs without IMSM platform support!\n" | |
9904 | " This operation is not supported and can lead to data loss.\n"); | |
9905 | return 1; | |
9906 | } | |
9907 | } | |
9908 | ||
9909 | return 0; | |
9910 | } | |
9911 | #ifndef MDASSEMBLE | |
9912 | /******************************************************************************* | |
9913 | * Function: imsm_record_badblock | |
9914 | * Description: This routine stores new bad block record in BBM log | |
9915 | * | |
9916 | * Parameters: | |
9917 | * a : array containing a bad block | |
9918 | * slot : disk number containing a bad block | |
9919 | * sector : bad block sector | |
9920 | * length : bad block sectors range | |
9921 | * Returns: | |
9922 | * 1 : Success | |
9923 | * 0 : Error | |
9924 | ******************************************************************************/ | |
9925 | static int imsm_record_badblock(struct active_array *a, int slot, | |
9926 | unsigned long long sector, int length) | |
9927 | { | |
9928 | struct intel_super *super = a->container->sb; | |
9929 | int ord; | |
9930 | int ret; | |
9931 | ||
9932 | ord = imsm_disk_slot_to_ord(a, slot); | |
9933 | if (ord < 0) | |
9934 | return 0; | |
9935 | ||
9936 | ret = record_new_badblock(super->bbm_log, ord_to_idx(ord), sector, | |
9937 | length); | |
9938 | if (ret) | |
9939 | super->updates_pending++; | |
9940 | ||
9941 | return ret; | |
9942 | } | |
9943 | /******************************************************************************* | |
9944 | * Function: imsm_clear_badblock | |
9945 | * Description: This routine clears bad block record from BBM log | |
9946 | * | |
9947 | * Parameters: | |
9948 | * a : array containing a bad block | |
9949 | * slot : disk number containing a bad block | |
9950 | * sector : bad block sector | |
9951 | * length : bad block sectors range | |
9952 | * Returns: | |
9953 | * 1 : Success | |
9954 | * 0 : Error | |
9955 | ******************************************************************************/ | |
9956 | static int imsm_clear_badblock(struct active_array *a, int slot, | |
9957 | unsigned long long sector, int length) | |
9958 | { | |
9959 | struct intel_super *super = a->container->sb; | |
9960 | int ord; | |
9961 | int ret; | |
9962 | ||
9963 | ord = imsm_disk_slot_to_ord(a, slot); | |
9964 | if (ord < 0) | |
9965 | return 0; | |
9966 | ||
9967 | ret = clear_badblock(super->bbm_log, ord_to_idx(ord), sector, length); | |
9968 | if (ret) | |
9969 | super->updates_pending++; | |
9970 | ||
9971 | return ret; | |
9972 | } | |
9973 | /******************************************************************************* | |
9974 | * Function: imsm_get_badblocks | |
9975 | * Description: This routine get list of bad blocks for an array | |
9976 | * | |
9977 | * Parameters: | |
9978 | * a : array | |
9979 | * slot : disk number | |
9980 | * Returns: | |
9981 | * bb : structure containing bad blocks | |
9982 | * NULL : error | |
9983 | ******************************************************************************/ | |
9984 | static struct md_bb *imsm_get_badblocks(struct active_array *a, int slot) | |
9985 | { | |
9986 | int inst = a->info.container_member; | |
9987 | struct intel_super *super = a->container->sb; | |
9988 | struct imsm_dev *dev = get_imsm_dev(super, inst); | |
9989 | struct imsm_map *map = get_imsm_map(dev, MAP_0); | |
9990 | int ord; | |
9991 | ||
9992 | ord = imsm_disk_slot_to_ord(a, slot); | |
9993 | if (ord < 0) | |
9994 | return NULL; | |
9995 | ||
9996 | get_volume_badblocks(super->bbm_log, ord_to_idx(ord), pba_of_lba0(map), | |
9997 | blocks_per_member(map), &super->bb); | |
9998 | ||
9999 | return &super->bb; | |
10000 | } | |
10001 | /******************************************************************************* | |
10002 | * Function: examine_badblocks_imsm | |
10003 | * Description: Prints list of bad blocks on a disk to the standard output | |
10004 | * | |
10005 | * Parameters: | |
10006 | * st : metadata handler | |
10007 | * fd : open file descriptor for device | |
10008 | * devname : device name | |
10009 | * Returns: | |
10010 | * 0 : Success | |
10011 | * 1 : Error | |
10012 | ******************************************************************************/ | |
10013 | static int examine_badblocks_imsm(struct supertype *st, int fd, char *devname) | |
10014 | { | |
10015 | struct intel_super *super = st->sb; | |
10016 | struct bbm_log *log = super->bbm_log; | |
10017 | struct dl *d = NULL; | |
10018 | int any = 0; | |
10019 | ||
10020 | for (d = super->disks; d ; d = d->next) { | |
10021 | if (strcmp(d->devname, devname) == 0) | |
10022 | break; | |
10023 | } | |
10024 | ||
10025 | if ((d == NULL) || (d->index < 0)) { /* serial mismatch probably */ | |
10026 | pr_err("%s doesn't appear to be part of a raid array\n", | |
10027 | devname); | |
10028 | return 1; | |
10029 | } | |
10030 | ||
10031 | if (log != NULL) { | |
10032 | unsigned int i; | |
10033 | struct bbm_log_entry *entry = &log->marked_block_entries[0]; | |
10034 | ||
10035 | for (i = 0; i < log->entry_count; i++) { | |
10036 | if (entry[i].disk_ordinal == d->index) { | |
10037 | unsigned long long sector = __le48_to_cpu( | |
10038 | &entry[i].defective_block_start); | |
10039 | int cnt = entry[i].marked_count + 1; | |
10040 | ||
10041 | if (!any) { | |
10042 | printf("Bad-blocks on %s:\n", devname); | |
10043 | any = 1; | |
10044 | } | |
10045 | ||
10046 | printf("%20llu for %d sectors\n", sector, cnt); | |
10047 | } | |
10048 | } | |
10049 | } | |
10050 | ||
10051 | if (!any) | |
10052 | printf("No bad-blocks list configured on %s\n", devname); | |
10053 | ||
10054 | return 0; | |
10055 | } | |
10056 | /******************************************************************************* | |
10057 | * Function: init_migr_record_imsm | |
10058 | * Description: Function inits imsm migration record | |
10059 | * Parameters: | |
10060 | * super : imsm internal array info | |
10061 | * dev : device under migration | |
10062 | * info : general array info to find the smallest device | |
10063 | * Returns: | |
10064 | * none | |
10065 | ******************************************************************************/ | |
10066 | void init_migr_record_imsm(struct supertype *st, struct imsm_dev *dev, | |
10067 | struct mdinfo *info) | |
10068 | { | |
10069 | struct intel_super *super = st->sb; | |
10070 | struct migr_record *migr_rec = super->migr_rec; | |
10071 | int new_data_disks; | |
10072 | unsigned long long dsize, dev_sectors; | |
10073 | long long unsigned min_dev_sectors = -1LLU; | |
10074 | struct mdinfo *sd; | |
10075 | char nm[30]; | |
10076 | int fd; | |
10077 | struct imsm_map *map_dest = get_imsm_map(dev, MAP_0); | |
10078 | struct imsm_map *map_src = get_imsm_map(dev, MAP_1); | |
10079 | unsigned long long num_migr_units; | |
10080 | unsigned long long array_blocks; | |
10081 | ||
10082 | memset(migr_rec, 0, sizeof(struct migr_record)); | |
10083 | migr_rec->family_num = __cpu_to_le32(super->anchor->family_num); | |
10084 | ||
10085 | /* only ascending reshape supported now */ | |
10086 | migr_rec->ascending_migr = __cpu_to_le32(1); | |
10087 | ||
10088 | migr_rec->dest_depth_per_unit = GEN_MIGR_AREA_SIZE / | |
10089 | max(map_dest->blocks_per_strip, map_src->blocks_per_strip); | |
10090 | migr_rec->dest_depth_per_unit *= | |
10091 | max(map_dest->blocks_per_strip, map_src->blocks_per_strip); | |
10092 | new_data_disks = imsm_num_data_members(dev, MAP_0); | |
10093 | migr_rec->blocks_per_unit = | |
10094 | __cpu_to_le32(migr_rec->dest_depth_per_unit * new_data_disks); | |
10095 | migr_rec->dest_depth_per_unit = | |
10096 | __cpu_to_le32(migr_rec->dest_depth_per_unit); | |
10097 | array_blocks = info->component_size * new_data_disks; | |
10098 | num_migr_units = | |
10099 | array_blocks / __le32_to_cpu(migr_rec->blocks_per_unit); | |
10100 | ||
10101 | if (array_blocks % __le32_to_cpu(migr_rec->blocks_per_unit)) | |
10102 | num_migr_units++; | |
10103 | migr_rec->num_migr_units = __cpu_to_le32(num_migr_units); | |
10104 | ||
10105 | migr_rec->post_migr_vol_cap = dev->size_low; | |
10106 | migr_rec->post_migr_vol_cap_hi = dev->size_high; | |
10107 | ||
10108 | /* Find the smallest dev */ | |
10109 | for (sd = info->devs ; sd ; sd = sd->next) { | |
10110 | sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor); | |
10111 | fd = dev_open(nm, O_RDONLY); | |
10112 | if (fd < 0) | |
10113 | continue; | |
10114 | get_dev_size(fd, NULL, &dsize); | |
10115 | dev_sectors = dsize / 512; | |
10116 | if (dev_sectors < min_dev_sectors) | |
10117 | min_dev_sectors = dev_sectors; | |
10118 | close(fd); | |
10119 | } | |
10120 | migr_rec->ckpt_area_pba = __cpu_to_le32(min_dev_sectors - | |
10121 | RAID_DISK_RESERVED_BLOCKS_IMSM_HI); | |
10122 | ||
10123 | write_imsm_migr_rec(st); | |
10124 | ||
10125 | return; | |
10126 | } | |
10127 | ||
10128 | /******************************************************************************* | |
10129 | * Function: save_backup_imsm | |
10130 | * Description: Function saves critical data stripes to Migration Copy Area | |
10131 | * and updates the current migration unit status. | |
10132 | * Use restore_stripes() to form a destination stripe, | |
10133 | * and to write it to the Copy Area. | |
10134 | * Parameters: | |
10135 | * st : supertype information | |
10136 | * dev : imsm device that backup is saved for | |
10137 | * info : general array info | |
10138 | * buf : input buffer | |
10139 | * length : length of data to backup (blocks_per_unit) | |
10140 | * Returns: | |
10141 | * 0 : success | |
10142 | *, -1 : fail | |
10143 | ******************************************************************************/ | |
10144 | int save_backup_imsm(struct supertype *st, | |
10145 | struct imsm_dev *dev, | |
10146 | struct mdinfo *info, | |
10147 | void *buf, | |
10148 | int length) | |
10149 | { | |
10150 | int rv = -1; | |
10151 | struct intel_super *super = st->sb; | |
10152 | unsigned long long *target_offsets; | |
10153 | int *targets; | |
10154 | int i; | |
10155 | struct imsm_map *map_dest = get_imsm_map(dev, MAP_0); | |
10156 | int new_disks = map_dest->num_members; | |
10157 | int dest_layout = 0; | |
10158 | int dest_chunk; | |
10159 | unsigned long long start; | |
10160 | int data_disks = imsm_num_data_members(dev, MAP_0); | |
10161 | ||
10162 | targets = xmalloc(new_disks * sizeof(int)); | |
10163 | ||
10164 | for (i = 0; i < new_disks; i++) | |
10165 | targets[i] = -1; | |
10166 | ||
10167 | target_offsets = xcalloc(new_disks, sizeof(unsigned long long)); | |
10168 | ||
10169 | start = info->reshape_progress * 512; | |
10170 | for (i = 0; i < new_disks; i++) { | |
10171 | target_offsets[i] = (unsigned long long) | |
10172 | __le32_to_cpu(super->migr_rec->ckpt_area_pba) * 512; | |
10173 | /* move back copy area adderss, it will be moved forward | |
10174 | * in restore_stripes() using start input variable | |
10175 | */ | |
10176 | target_offsets[i] -= start/data_disks; | |
10177 | } | |
10178 | ||
10179 | if (open_backup_targets(info, new_disks, targets, | |
10180 | super, dev)) | |
10181 | goto abort; | |
10182 | ||
10183 | dest_layout = imsm_level_to_layout(map_dest->raid_level); | |
10184 | dest_chunk = __le16_to_cpu(map_dest->blocks_per_strip) * 512; | |
10185 | ||
10186 | if (restore_stripes(targets, /* list of dest devices */ | |
10187 | target_offsets, /* migration record offsets */ | |
10188 | new_disks, | |
10189 | dest_chunk, | |
10190 | map_dest->raid_level, | |
10191 | dest_layout, | |
10192 | -1, /* source backup file descriptor */ | |
10193 | 0, /* input buf offset | |
10194 | * always 0 buf is already offseted */ | |
10195 | start, | |
10196 | length, | |
10197 | buf) != 0) { | |
10198 | pr_err("Error restoring stripes\n"); | |
10199 | goto abort; | |
10200 | } | |
10201 | ||
10202 | rv = 0; | |
10203 | ||
10204 | abort: | |
10205 | if (targets) { | |
10206 | close_targets(targets, new_disks); | |
10207 | free(targets); | |
10208 | } | |
10209 | free(target_offsets); | |
10210 | ||
10211 | return rv; | |
10212 | } | |
10213 | ||
10214 | /******************************************************************************* | |
10215 | * Function: save_checkpoint_imsm | |
10216 | * Description: Function called for current unit status update | |
10217 | * in the migration record. It writes it to disk. | |
10218 | * Parameters: | |
10219 | * super : imsm internal array info | |
10220 | * info : general array info | |
10221 | * Returns: | |
10222 | * 0: success | |
10223 | * 1: failure | |
10224 | * 2: failure, means no valid migration record | |
10225 | * / no general migration in progress / | |
10226 | ******************************************************************************/ | |
10227 | int save_checkpoint_imsm(struct supertype *st, struct mdinfo *info, int state) | |
10228 | { | |
10229 | struct intel_super *super = st->sb; | |
10230 | unsigned long long blocks_per_unit; | |
10231 | unsigned long long curr_migr_unit; | |
10232 | ||
10233 | if (load_imsm_migr_rec(super, info) != 0) { | |
10234 | dprintf("imsm: ERROR: Cannot read migration record for checkpoint save.\n"); | |
10235 | return 1; | |
10236 | } | |
10237 | ||
10238 | blocks_per_unit = __le32_to_cpu(super->migr_rec->blocks_per_unit); | |
10239 | if (blocks_per_unit == 0) { | |
10240 | dprintf("imsm: no migration in progress.\n"); | |
10241 | return 2; | |
10242 | } | |
10243 | curr_migr_unit = info->reshape_progress / blocks_per_unit; | |
10244 | /* check if array is alligned to copy area | |
10245 | * if it is not alligned, add one to current migration unit value | |
10246 | * this can happend on array reshape finish only | |
10247 | */ | |
10248 | if (info->reshape_progress % blocks_per_unit) | |
10249 | curr_migr_unit++; | |
10250 | ||
10251 | super->migr_rec->curr_migr_unit = | |
10252 | __cpu_to_le32(curr_migr_unit); | |
10253 | super->migr_rec->rec_status = __cpu_to_le32(state); | |
10254 | super->migr_rec->dest_1st_member_lba = | |
10255 | __cpu_to_le32(curr_migr_unit * | |
10256 | __le32_to_cpu(super->migr_rec->dest_depth_per_unit)); | |
10257 | if (write_imsm_migr_rec(st) < 0) { | |
10258 | dprintf("imsm: Cannot write migration record outside backup area\n"); | |
10259 | return 1; | |
10260 | } | |
10261 | ||
10262 | return 0; | |
10263 | } | |
10264 | ||
10265 | /******************************************************************************* | |
10266 | * Function: recover_backup_imsm | |
10267 | * Description: Function recovers critical data from the Migration Copy Area | |
10268 | * while assembling an array. | |
10269 | * Parameters: | |
10270 | * super : imsm internal array info | |
10271 | * info : general array info | |
10272 | * Returns: | |
10273 | * 0 : success (or there is no data to recover) | |
10274 | * 1 : fail | |
10275 | ******************************************************************************/ | |
10276 | int recover_backup_imsm(struct supertype *st, struct mdinfo *info) | |
10277 | { | |
10278 | struct intel_super *super = st->sb; | |
10279 | struct migr_record *migr_rec = super->migr_rec; | |
10280 | struct imsm_map *map_dest; | |
10281 | struct intel_dev *id = NULL; | |
10282 | unsigned long long read_offset; | |
10283 | unsigned long long write_offset; | |
10284 | unsigned unit_len; | |
10285 | int *targets = NULL; | |
10286 | int new_disks, i, err; | |
10287 | char *buf = NULL; | |
10288 | int retval = 1; | |
10289 | unsigned int sector_size = super->sector_size; | |
10290 | unsigned long curr_migr_unit = __le32_to_cpu(migr_rec->curr_migr_unit); | |
10291 | unsigned long num_migr_units = __le32_to_cpu(migr_rec->num_migr_units); | |
10292 | char buffer[20]; | |
10293 | int skipped_disks = 0; | |
10294 | ||
10295 | err = sysfs_get_str(info, NULL, "array_state", (char *)buffer, 20); | |
10296 | if (err < 1) | |
10297 | return 1; | |
10298 | ||
10299 | /* recover data only during assemblation */ | |
10300 | if (strncmp(buffer, "inactive", 8) != 0) | |
10301 | return 0; | |
10302 | /* no data to recover */ | |
10303 | if (__le32_to_cpu(migr_rec->rec_status) == UNIT_SRC_NORMAL) | |
10304 | return 0; | |
10305 | if (curr_migr_unit >= num_migr_units) | |
10306 | return 1; | |
10307 | ||
10308 | /* find device during reshape */ | |
10309 | for (id = super->devlist; id; id = id->next) | |
10310 | if (is_gen_migration(id->dev)) | |
10311 | break; | |
10312 | if (id == NULL) | |
10313 | return 1; | |
10314 | ||
10315 | map_dest = get_imsm_map(id->dev, MAP_0); | |
10316 | new_disks = map_dest->num_members; | |
10317 | ||
10318 | read_offset = (unsigned long long) | |
10319 | __le32_to_cpu(migr_rec->ckpt_area_pba) * 512; | |
10320 | ||
10321 | write_offset = ((unsigned long long) | |
10322 | __le32_to_cpu(migr_rec->dest_1st_member_lba) + | |
10323 | pba_of_lba0(map_dest)) * 512; | |
10324 | ||
10325 | unit_len = __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512; | |
10326 | if (posix_memalign((void **)&buf, sector_size, unit_len) != 0) | |
10327 | goto abort; | |
10328 | targets = xcalloc(new_disks, sizeof(int)); | |
10329 | ||
10330 | if (open_backup_targets(info, new_disks, targets, super, id->dev)) { | |
10331 | pr_err("Cannot open some devices belonging to array.\n"); | |
10332 | goto abort; | |
10333 | } | |
10334 | ||
10335 | for (i = 0; i < new_disks; i++) { | |
10336 | if (targets[i] < 0) { | |
10337 | skipped_disks++; | |
10338 | continue; | |
10339 | } | |
10340 | if (lseek64(targets[i], read_offset, SEEK_SET) < 0) { | |
10341 | pr_err("Cannot seek to block: %s\n", | |
10342 | strerror(errno)); | |
10343 | skipped_disks++; | |
10344 | continue; | |
10345 | } | |
10346 | if ((unsigned)read(targets[i], buf, unit_len) != unit_len) { | |
10347 | pr_err("Cannot read copy area block: %s\n", | |
10348 | strerror(errno)); | |
10349 | skipped_disks++; | |
10350 | continue; | |
10351 | } | |
10352 | if (lseek64(targets[i], write_offset, SEEK_SET) < 0) { | |
10353 | pr_err("Cannot seek to block: %s\n", | |
10354 | strerror(errno)); | |
10355 | skipped_disks++; | |
10356 | continue; | |
10357 | } | |
10358 | if ((unsigned)write(targets[i], buf, unit_len) != unit_len) { | |
10359 | pr_err("Cannot restore block: %s\n", | |
10360 | strerror(errno)); | |
10361 | skipped_disks++; | |
10362 | continue; | |
10363 | } | |
10364 | } | |
10365 | ||
10366 | if (skipped_disks > imsm_get_allowed_degradation(info->new_level, | |
10367 | new_disks, | |
10368 | super, | |
10369 | id->dev)) { | |
10370 | pr_err("Cannot restore data from backup. Too many failed disks\n"); | |
10371 | goto abort; | |
10372 | } | |
10373 | ||
10374 | if (save_checkpoint_imsm(st, info, UNIT_SRC_NORMAL)) { | |
10375 | /* ignore error == 2, this can mean end of reshape here | |
10376 | */ | |
10377 | dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL) during restart\n"); | |
10378 | } else | |
10379 | retval = 0; | |
10380 | ||
10381 | abort: | |
10382 | if (targets) { | |
10383 | for (i = 0; i < new_disks; i++) | |
10384 | if (targets[i]) | |
10385 | close(targets[i]); | |
10386 | free(targets); | |
10387 | } | |
10388 | free(buf); | |
10389 | return retval; | |
10390 | } | |
10391 | ||
10392 | static char disk_by_path[] = "/dev/disk/by-path/"; | |
10393 | ||
10394 | static const char *imsm_get_disk_controller_domain(const char *path) | |
10395 | { | |
10396 | char disk_path[PATH_MAX]; | |
10397 | char *drv=NULL; | |
10398 | struct stat st; | |
10399 | ||
10400 | strcpy(disk_path, disk_by_path); | |
10401 | strncat(disk_path, path, PATH_MAX - strlen(disk_path) - 1); | |
10402 | if (stat(disk_path, &st) == 0) { | |
10403 | struct sys_dev* hba; | |
10404 | char *path; | |
10405 | ||
10406 | path = devt_to_devpath(st.st_rdev); | |
10407 | if (path == NULL) | |
10408 | return "unknown"; | |
10409 | hba = find_disk_attached_hba(-1, path); | |
10410 | if (hba && hba->type == SYS_DEV_SAS) | |
10411 | drv = "isci"; | |
10412 | else if (hba && hba->type == SYS_DEV_SATA) | |
10413 | drv = "ahci"; | |
10414 | else | |
10415 | drv = "unknown"; | |
10416 | dprintf("path: %s hba: %s attached: %s\n", | |
10417 | path, (hba) ? hba->path : "NULL", drv); | |
10418 | free(path); | |
10419 | } | |
10420 | return drv; | |
10421 | } | |
10422 | ||
10423 | static char *imsm_find_array_devnm_by_subdev(int subdev, char *container) | |
10424 | { | |
10425 | static char devnm[32]; | |
10426 | char subdev_name[20]; | |
10427 | struct mdstat_ent *mdstat; | |
10428 | ||
10429 | sprintf(subdev_name, "%d", subdev); | |
10430 | mdstat = mdstat_by_subdev(subdev_name, container); | |
10431 | if (!mdstat) | |
10432 | return NULL; | |
10433 | ||
10434 | strcpy(devnm, mdstat->devnm); | |
10435 | free_mdstat(mdstat); | |
10436 | return devnm; | |
10437 | } | |
10438 | ||
10439 | static int imsm_reshape_is_allowed_on_container(struct supertype *st, | |
10440 | struct geo_params *geo, | |
10441 | int *old_raid_disks, | |
10442 | int direction) | |
10443 | { | |
10444 | /* currently we only support increasing the number of devices | |
10445 | * for a container. This increases the number of device for each | |
10446 | * member array. They must all be RAID0 or RAID5. | |
10447 | */ | |
10448 | int ret_val = 0; | |
10449 | struct mdinfo *info, *member; | |
10450 | int devices_that_can_grow = 0; | |
10451 | ||
10452 | dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): st->devnm = (%s)\n", st->devnm); | |
10453 | ||
10454 | if (geo->size > 0 || | |
10455 | geo->level != UnSet || | |
10456 | geo->layout != UnSet || | |
10457 | geo->chunksize != 0 || | |
10458 | geo->raid_disks == UnSet) { | |
10459 | dprintf("imsm: Container operation is allowed for raid disks number change only.\n"); | |
10460 | return ret_val; | |
10461 | } | |
10462 | ||
10463 | if (direction == ROLLBACK_METADATA_CHANGES) { | |
10464 | dprintf("imsm: Metadata changes rollback is not supported for container operation.\n"); | |
10465 | return ret_val; | |
10466 | } | |
10467 | ||
10468 | info = container_content_imsm(st, NULL); | |
10469 | for (member = info; member; member = member->next) { | |
10470 | char *result; | |
10471 | ||
10472 | dprintf("imsm: checking device_num: %i\n", | |
10473 | member->container_member); | |
10474 | ||
10475 | if (geo->raid_disks <= member->array.raid_disks) { | |
10476 | /* we work on container for Online Capacity Expansion | |
10477 | * only so raid_disks has to grow | |
10478 | */ | |
10479 | dprintf("imsm: for container operation raid disks increase is required\n"); | |
10480 | break; | |
10481 | } | |
10482 | ||
10483 | if (info->array.level != 0 && info->array.level != 5) { | |
10484 | /* we cannot use this container with other raid level | |
10485 | */ | |
10486 | dprintf("imsm: for container operation wrong raid level (%i) detected\n", | |
10487 | info->array.level); | |
10488 | break; | |
10489 | } else { | |
10490 | /* check for platform support | |
10491 | * for this raid level configuration | |
10492 | */ | |
10493 | struct intel_super *super = st->sb; | |
10494 | if (!is_raid_level_supported(super->orom, | |
10495 | member->array.level, | |
10496 | geo->raid_disks)) { | |
10497 | dprintf("platform does not support raid%d with %d disk%s\n", | |
10498 | info->array.level, | |
10499 | geo->raid_disks, | |
10500 | geo->raid_disks > 1 ? "s" : ""); | |
10501 | break; | |
10502 | } | |
10503 | /* check if component size is aligned to chunk size | |
10504 | */ | |
10505 | if (info->component_size % | |
10506 | (info->array.chunk_size/512)) { | |
10507 | dprintf("Component size is not aligned to chunk size\n"); | |
10508 | break; | |
10509 | } | |
10510 | } | |
10511 | ||
10512 | if (*old_raid_disks && | |
10513 | info->array.raid_disks != *old_raid_disks) | |
10514 | break; | |
10515 | *old_raid_disks = info->array.raid_disks; | |
10516 | ||
10517 | /* All raid5 and raid0 volumes in container | |
10518 | * have to be ready for Online Capacity Expansion | |
10519 | * so they need to be assembled. We have already | |
10520 | * checked that no recovery etc is happening. | |
10521 | */ | |
10522 | result = imsm_find_array_devnm_by_subdev(member->container_member, | |
10523 | st->container_devnm); | |
10524 | if (result == NULL) { | |
10525 | dprintf("imsm: cannot find array\n"); | |
10526 | break; | |
10527 | } | |
10528 | devices_that_can_grow++; | |
10529 | } | |
10530 | sysfs_free(info); | |
10531 | if (!member && devices_that_can_grow) | |
10532 | ret_val = 1; | |
10533 | ||
10534 | if (ret_val) | |
10535 | dprintf("Container operation allowed\n"); | |
10536 | else | |
10537 | dprintf("Error: %i\n", ret_val); | |
10538 | ||
10539 | return ret_val; | |
10540 | } | |
10541 | ||
10542 | /* Function: get_spares_for_grow | |
10543 | * Description: Allocates memory and creates list of spare devices | |
10544 | * avaliable in container. Checks if spare drive size is acceptable. | |
10545 | * Parameters: Pointer to the supertype structure | |
10546 | * Returns: Pointer to the list of spare devices (mdinfo structure) on success, | |
10547 | * NULL if fail | |
10548 | */ | |
10549 | static struct mdinfo *get_spares_for_grow(struct supertype *st) | |
10550 | { | |
10551 | unsigned long long min_size = min_acceptable_spare_size_imsm(st); | |
10552 | return container_choose_spares(st, min_size, NULL, NULL, NULL, 0); | |
10553 | } | |
10554 | ||
10555 | /****************************************************************************** | |
10556 | * function: imsm_create_metadata_update_for_reshape | |
10557 | * Function creates update for whole IMSM container. | |
10558 | * | |
10559 | ******************************************************************************/ | |
10560 | static int imsm_create_metadata_update_for_reshape( | |
10561 | struct supertype *st, | |
10562 | struct geo_params *geo, | |
10563 | int old_raid_disks, | |
10564 | struct imsm_update_reshape **updatep) | |
10565 | { | |
10566 | struct intel_super *super = st->sb; | |
10567 | struct imsm_super *mpb = super->anchor; | |
10568 | int update_memory_size; | |
10569 | struct imsm_update_reshape *u; | |
10570 | struct mdinfo *spares; | |
10571 | int i; | |
10572 | int delta_disks; | |
10573 | struct mdinfo *dev; | |
10574 | ||
10575 | dprintf("(enter) raid_disks = %i\n", geo->raid_disks); | |
10576 | ||
10577 | delta_disks = geo->raid_disks - old_raid_disks; | |
10578 | ||
10579 | /* size of all update data without anchor */ | |
10580 | update_memory_size = sizeof(struct imsm_update_reshape); | |
10581 | ||
10582 | /* now add space for spare disks that we need to add. */ | |
10583 | update_memory_size += sizeof(u->new_disks[0]) * (delta_disks - 1); | |
10584 | ||
10585 | u = xcalloc(1, update_memory_size); | |
10586 | u->type = update_reshape_container_disks; | |
10587 | u->old_raid_disks = old_raid_disks; | |
10588 | u->new_raid_disks = geo->raid_disks; | |
10589 | ||
10590 | /* now get spare disks list | |
10591 | */ | |
10592 | spares = get_spares_for_grow(st); | |
10593 | ||
10594 | if (spares == NULL | |
10595 | || delta_disks > spares->array.spare_disks) { | |
10596 | pr_err("imsm: ERROR: Cannot get spare devices for %s.\n", geo->dev_name); | |
10597 | i = -1; | |
10598 | goto abort; | |
10599 | } | |
10600 | ||
10601 | /* we have got spares | |
10602 | * update disk list in imsm_disk list table in anchor | |
10603 | */ | |
10604 | dprintf("imsm: %i spares are available.\n\n", | |
10605 | spares->array.spare_disks); | |
10606 | ||
10607 | dev = spares->devs; | |
10608 | for (i = 0; i < delta_disks; i++) { | |
10609 | struct dl *dl; | |
10610 | ||
10611 | if (dev == NULL) | |
10612 | break; | |
10613 | u->new_disks[i] = makedev(dev->disk.major, | |
10614 | dev->disk.minor); | |
10615 | dl = get_disk_super(super, dev->disk.major, dev->disk.minor); | |
10616 | dl->index = mpb->num_disks; | |
10617 | mpb->num_disks++; | |
10618 | dev = dev->next; | |
10619 | } | |
10620 | ||
10621 | abort: | |
10622 | /* free spares | |
10623 | */ | |
10624 | sysfs_free(spares); | |
10625 | ||
10626 | dprintf("imsm: reshape update preparation :"); | |
10627 | if (i == delta_disks) { | |
10628 | dprintf_cont(" OK\n"); | |
10629 | *updatep = u; | |
10630 | return update_memory_size; | |
10631 | } | |
10632 | free(u); | |
10633 | dprintf_cont(" Error\n"); | |
10634 | ||
10635 | return 0; | |
10636 | } | |
10637 | ||
10638 | /****************************************************************************** | |
10639 | * function: imsm_create_metadata_update_for_size_change() | |
10640 | * Creates update for IMSM array for array size change. | |
10641 | * | |
10642 | ******************************************************************************/ | |
10643 | static int imsm_create_metadata_update_for_size_change( | |
10644 | struct supertype *st, | |
10645 | struct geo_params *geo, | |
10646 | struct imsm_update_size_change **updatep) | |
10647 | { | |
10648 | struct intel_super *super = st->sb; | |
10649 | int update_memory_size; | |
10650 | struct imsm_update_size_change *u; | |
10651 | ||
10652 | dprintf("(enter) New size = %llu\n", geo->size); | |
10653 | ||
10654 | /* size of all update data without anchor */ | |
10655 | update_memory_size = sizeof(struct imsm_update_size_change); | |
10656 | ||
10657 | u = xcalloc(1, update_memory_size); | |
10658 | u->type = update_size_change; | |
10659 | u->subdev = super->current_vol; | |
10660 | u->new_size = geo->size; | |
10661 | ||
10662 | dprintf("imsm: reshape update preparation : OK\n"); | |
10663 | *updatep = u; | |
10664 | ||
10665 | return update_memory_size; | |
10666 | } | |
10667 | ||
10668 | /****************************************************************************** | |
10669 | * function: imsm_create_metadata_update_for_migration() | |
10670 | * Creates update for IMSM array. | |
10671 | * | |
10672 | ******************************************************************************/ | |
10673 | static int imsm_create_metadata_update_for_migration( | |
10674 | struct supertype *st, | |
10675 | struct geo_params *geo, | |
10676 | struct imsm_update_reshape_migration **updatep) | |
10677 | { | |
10678 | struct intel_super *super = st->sb; | |
10679 | int update_memory_size; | |
10680 | struct imsm_update_reshape_migration *u; | |
10681 | struct imsm_dev *dev; | |
10682 | int previous_level = -1; | |
10683 | ||
10684 | dprintf("(enter) New Level = %i\n", geo->level); | |
10685 | ||
10686 | /* size of all update data without anchor */ | |
10687 | update_memory_size = sizeof(struct imsm_update_reshape_migration); | |
10688 | ||
10689 | u = xcalloc(1, update_memory_size); | |
10690 | u->type = update_reshape_migration; | |
10691 | u->subdev = super->current_vol; | |
10692 | u->new_level = geo->level; | |
10693 | u->new_layout = geo->layout; | |
10694 | u->new_raid_disks = u->old_raid_disks = geo->raid_disks; | |
10695 | u->new_disks[0] = -1; | |
10696 | u->new_chunksize = -1; | |
10697 | ||
10698 | dev = get_imsm_dev(super, u->subdev); | |
10699 | if (dev) { | |
10700 | struct imsm_map *map; | |
10701 | ||
10702 | map = get_imsm_map(dev, MAP_0); | |
10703 | if (map) { | |
10704 | int current_chunk_size = | |
10705 | __le16_to_cpu(map->blocks_per_strip) / 2; | |
10706 | ||
10707 | if (geo->chunksize != current_chunk_size) { | |
10708 | u->new_chunksize = geo->chunksize / 1024; | |
10709 | dprintf("imsm: chunk size change from %i to %i\n", | |
10710 | current_chunk_size, u->new_chunksize); | |
10711 | } | |
10712 | previous_level = map->raid_level; | |
10713 | } | |
10714 | } | |
10715 | if (geo->level == 5 && previous_level == 0) { | |
10716 | struct mdinfo *spares = NULL; | |
10717 | ||
10718 | u->new_raid_disks++; | |
10719 | spares = get_spares_for_grow(st); | |
10720 | if (spares == NULL || spares->array.spare_disks < 1) { | |
10721 | free(u); | |
10722 | sysfs_free(spares); | |
10723 | update_memory_size = 0; | |
10724 | pr_err("cannot get spare device for requested migration\n"); | |
10725 | return 0; | |
10726 | } | |
10727 | sysfs_free(spares); | |
10728 | } | |
10729 | dprintf("imsm: reshape update preparation : OK\n"); | |
10730 | *updatep = u; | |
10731 | ||
10732 | return update_memory_size; | |
10733 | } | |
10734 | ||
10735 | static void imsm_update_metadata_locally(struct supertype *st, | |
10736 | void *buf, int len) | |
10737 | { | |
10738 | struct metadata_update mu; | |
10739 | ||
10740 | mu.buf = buf; | |
10741 | mu.len = len; | |
10742 | mu.space = NULL; | |
10743 | mu.space_list = NULL; | |
10744 | mu.next = NULL; | |
10745 | if (imsm_prepare_update(st, &mu)) | |
10746 | imsm_process_update(st, &mu); | |
10747 | ||
10748 | while (mu.space_list) { | |
10749 | void **space = mu.space_list; | |
10750 | mu.space_list = *space; | |
10751 | free(space); | |
10752 | } | |
10753 | } | |
10754 | ||
10755 | /*************************************************************************** | |
10756 | * Function: imsm_analyze_change | |
10757 | * Description: Function analyze change for single volume | |
10758 | * and validate if transition is supported | |
10759 | * Parameters: Geometry parameters, supertype structure, | |
10760 | * metadata change direction (apply/rollback) | |
10761 | * Returns: Operation type code on success, -1 if fail | |
10762 | ****************************************************************************/ | |
10763 | enum imsm_reshape_type imsm_analyze_change(struct supertype *st, | |
10764 | struct geo_params *geo, | |
10765 | int direction) | |
10766 | { | |
10767 | struct mdinfo info; | |
10768 | int change = -1; | |
10769 | int check_devs = 0; | |
10770 | int chunk; | |
10771 | /* number of added/removed disks in operation result */ | |
10772 | int devNumChange = 0; | |
10773 | /* imsm compatible layout value for array geometry verification */ | |
10774 | int imsm_layout = -1; | |
10775 | int data_disks; | |
10776 | struct imsm_dev *dev; | |
10777 | struct intel_super *super; | |
10778 | unsigned long long current_size; | |
10779 | unsigned long long free_size; | |
10780 | unsigned long long max_size; | |
10781 | int rv; | |
10782 | ||
10783 | getinfo_super_imsm_volume(st, &info, NULL); | |
10784 | if (geo->level != info.array.level && geo->level >= 0 && | |
10785 | geo->level != UnSet) { | |
10786 | switch (info.array.level) { | |
10787 | case 0: | |
10788 | if (geo->level == 5) { | |
10789 | change = CH_MIGRATION; | |
10790 | if (geo->layout != ALGORITHM_LEFT_ASYMMETRIC) { | |
10791 | pr_err("Error. Requested Layout not supported (left-asymmetric layout is supported only)!\n"); | |
10792 | change = -1; | |
10793 | goto analyse_change_exit; | |
10794 | } | |
10795 | imsm_layout = geo->layout; | |
10796 | check_devs = 1; | |
10797 | devNumChange = 1; /* parity disk added */ | |
10798 | } else if (geo->level == 10) { | |
10799 | change = CH_TAKEOVER; | |
10800 | check_devs = 1; | |
10801 | devNumChange = 2; /* two mirrors added */ | |
10802 | imsm_layout = 0x102; /* imsm supported layout */ | |
10803 | } | |
10804 | break; | |
10805 | case 1: | |
10806 | case 10: | |
10807 | if (geo->level == 0) { | |
10808 | change = CH_TAKEOVER; | |
10809 | check_devs = 1; | |
10810 | devNumChange = -(geo->raid_disks/2); | |
10811 | imsm_layout = 0; /* imsm raid0 layout */ | |
10812 | } | |
10813 | break; | |
10814 | } | |
10815 | if (change == -1) { | |
10816 | pr_err("Error. Level Migration from %d to %d not supported!\n", | |
10817 | info.array.level, geo->level); | |
10818 | goto analyse_change_exit; | |
10819 | } | |
10820 | } else | |
10821 | geo->level = info.array.level; | |
10822 | ||
10823 | if (geo->layout != info.array.layout && | |
10824 | (geo->layout != UnSet && geo->layout != -1)) { | |
10825 | change = CH_MIGRATION; | |
10826 | if (info.array.layout == 0 && info.array.level == 5 && | |
10827 | geo->layout == 5) { | |
10828 | /* reshape 5 -> 4 */ | |
10829 | } else if (info.array.layout == 5 && info.array.level == 5 && | |
10830 | geo->layout == 0) { | |
10831 | /* reshape 4 -> 5 */ | |
10832 | geo->layout = 0; | |
10833 | geo->level = 5; | |
10834 | } else { | |
10835 | pr_err("Error. Layout Migration from %d to %d not supported!\n", | |
10836 | info.array.layout, geo->layout); | |
10837 | change = -1; | |
10838 | goto analyse_change_exit; | |
10839 | } | |
10840 | } else { | |
10841 | geo->layout = info.array.layout; | |
10842 | if (imsm_layout == -1) | |
10843 | imsm_layout = info.array.layout; | |
10844 | } | |
10845 | ||
10846 | if (geo->chunksize > 0 && geo->chunksize != UnSet && | |
10847 | geo->chunksize != info.array.chunk_size) { | |
10848 | if (info.array.level == 10) { | |
10849 | pr_err("Error. Chunk size change for RAID 10 is not supported.\n"); | |
10850 | change = -1; | |
10851 | goto analyse_change_exit; | |
10852 | } else if (info.component_size % (geo->chunksize/512)) { | |
10853 | pr_err("New chunk size (%dK) does not evenly divide device size (%lluk). Aborting...\n", | |
10854 | geo->chunksize/1024, info.component_size/2); | |
10855 | change = -1; | |
10856 | goto analyse_change_exit; | |
10857 | } | |
10858 | change = CH_MIGRATION; | |
10859 | } else { | |
10860 | geo->chunksize = info.array.chunk_size; | |
10861 | } | |
10862 | ||
10863 | chunk = geo->chunksize / 1024; | |
10864 | ||
10865 | super = st->sb; | |
10866 | dev = get_imsm_dev(super, super->current_vol); | |
10867 | data_disks = imsm_num_data_members(dev , MAP_0); | |
10868 | /* compute current size per disk member | |
10869 | */ | |
10870 | current_size = info.custom_array_size / data_disks; | |
10871 | ||
10872 | if (geo->size > 0 && geo->size != MAX_SIZE) { | |
10873 | /* align component size | |
10874 | */ | |
10875 | geo->size = imsm_component_size_aligment_check( | |
10876 | get_imsm_raid_level(dev->vol.map), | |
10877 | chunk * 1024, super->sector_size, | |
10878 | geo->size * 2); | |
10879 | if (geo->size == 0) { | |
10880 | pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is 0).\n", | |
10881 | current_size); | |
10882 | goto analyse_change_exit; | |
10883 | } | |
10884 | } | |
10885 | ||
10886 | if (current_size != geo->size && geo->size > 0) { | |
10887 | if (change != -1) { | |
10888 | pr_err("Error. Size change should be the only one at a time.\n"); | |
10889 | change = -1; | |
10890 | goto analyse_change_exit; | |
10891 | } | |
10892 | if ((super->current_vol + 1) != super->anchor->num_raid_devs) { | |
10893 | pr_err("Error. The last volume in container can be expanded only (%i/%s).\n", | |
10894 | super->current_vol, st->devnm); | |
10895 | goto analyse_change_exit; | |
10896 | } | |
10897 | /* check the maximum available size | |
10898 | */ | |
10899 | rv = imsm_get_free_size(st, dev->vol.map->num_members, | |
10900 | 0, chunk, &free_size); | |
10901 | if (rv == 0) | |
10902 | /* Cannot find maximum available space | |
10903 | */ | |
10904 | max_size = 0; | |
10905 | else { | |
10906 | max_size = free_size + current_size; | |
10907 | /* align component size | |
10908 | */ | |
10909 | max_size = imsm_component_size_aligment_check( | |
10910 | get_imsm_raid_level(dev->vol.map), | |
10911 | chunk * 1024, super->sector_size, | |
10912 | max_size); | |
10913 | } | |
10914 | if (geo->size == MAX_SIZE) { | |
10915 | /* requested size change to the maximum available size | |
10916 | */ | |
10917 | if (max_size == 0) { | |
10918 | pr_err("Error. Cannot find maximum available space.\n"); | |
10919 | change = -1; | |
10920 | goto analyse_change_exit; | |
10921 | } else | |
10922 | geo->size = max_size; | |
10923 | } | |
10924 | ||
10925 | if (direction == ROLLBACK_METADATA_CHANGES) { | |
10926 | /* accept size for rollback only | |
10927 | */ | |
10928 | } else { | |
10929 | /* round size due to metadata compatibility | |
10930 | */ | |
10931 | geo->size = (geo->size >> SECT_PER_MB_SHIFT) | |
10932 | << SECT_PER_MB_SHIFT; | |
10933 | dprintf("Prepare update for size change to %llu\n", | |
10934 | geo->size ); | |
10935 | if (current_size >= geo->size) { | |
10936 | pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is %llu).\n", | |
10937 | current_size, geo->size); | |
10938 | goto analyse_change_exit; | |
10939 | } | |
10940 | if (max_size && geo->size > max_size) { | |
10941 | pr_err("Error. Requested size is larger than maximum available size (maximum available size is %llu, requested size /rounded/ is %llu).\n", | |
10942 | max_size, geo->size); | |
10943 | goto analyse_change_exit; | |
10944 | } | |
10945 | } | |
10946 | geo->size *= data_disks; | |
10947 | geo->raid_disks = dev->vol.map->num_members; | |
10948 | change = CH_ARRAY_SIZE; | |
10949 | } | |
10950 | if (!validate_geometry_imsm(st, | |
10951 | geo->level, | |
10952 | imsm_layout, | |
10953 | geo->raid_disks + devNumChange, | |
10954 | &chunk, | |
10955 | geo->size, INVALID_SECTORS, | |
10956 | 0, 0, info.consistency_policy, 1)) | |
10957 | change = -1; | |
10958 | ||
10959 | if (check_devs) { | |
10960 | struct intel_super *super = st->sb; | |
10961 | struct imsm_super *mpb = super->anchor; | |
10962 | ||
10963 | if (mpb->num_raid_devs > 1) { | |
10964 | pr_err("Error. Cannot perform operation on %s- for this operation it MUST be single array in container\n", | |
10965 | geo->dev_name); | |
10966 | change = -1; | |
10967 | } | |
10968 | } | |
10969 | ||
10970 | analyse_change_exit: | |
10971 | if (direction == ROLLBACK_METADATA_CHANGES && | |
10972 | (change == CH_MIGRATION || change == CH_TAKEOVER)) { | |
10973 | dprintf("imsm: Metadata changes rollback is not supported for migration and takeover operations.\n"); | |
10974 | change = -1; | |
10975 | } | |
10976 | return change; | |
10977 | } | |
10978 | ||
10979 | int imsm_takeover(struct supertype *st, struct geo_params *geo) | |
10980 | { | |
10981 | struct intel_super *super = st->sb; | |
10982 | struct imsm_update_takeover *u; | |
10983 | ||
10984 | u = xmalloc(sizeof(struct imsm_update_takeover)); | |
10985 | ||
10986 | u->type = update_takeover; | |
10987 | u->subarray = super->current_vol; | |
10988 | ||
10989 | /* 10->0 transition */ | |
10990 | if (geo->level == 0) | |
10991 | u->direction = R10_TO_R0; | |
10992 | ||
10993 | /* 0->10 transition */ | |
10994 | if (geo->level == 10) | |
10995 | u->direction = R0_TO_R10; | |
10996 | ||
10997 | /* update metadata locally */ | |
10998 | imsm_update_metadata_locally(st, u, | |
10999 | sizeof(struct imsm_update_takeover)); | |
11000 | /* and possibly remotely */ | |
11001 | if (st->update_tail) | |
11002 | append_metadata_update(st, u, | |
11003 | sizeof(struct imsm_update_takeover)); | |
11004 | else | |
11005 | free(u); | |
11006 | ||
11007 | return 0; | |
11008 | } | |
11009 | ||
11010 | static int imsm_reshape_super(struct supertype *st, unsigned long long size, | |
11011 | int level, | |
11012 | int layout, int chunksize, int raid_disks, | |
11013 | int delta_disks, char *backup, char *dev, | |
11014 | int direction, int verbose) | |
11015 | { | |
11016 | int ret_val = 1; | |
11017 | struct geo_params geo; | |
11018 | ||
11019 | dprintf("(enter)\n"); | |
11020 | ||
11021 | memset(&geo, 0, sizeof(struct geo_params)); | |
11022 | ||
11023 | geo.dev_name = dev; | |
11024 | strcpy(geo.devnm, st->devnm); | |
11025 | geo.size = size; | |
11026 | geo.level = level; | |
11027 | geo.layout = layout; | |
11028 | geo.chunksize = chunksize; | |
11029 | geo.raid_disks = raid_disks; | |
11030 | if (delta_disks != UnSet) | |
11031 | geo.raid_disks += delta_disks; | |
11032 | ||
11033 | dprintf("for level : %i\n", geo.level); | |
11034 | dprintf("for raid_disks : %i\n", geo.raid_disks); | |
11035 | ||
11036 | if (experimental() == 0) | |
11037 | return ret_val; | |
11038 | ||
11039 | if (strcmp(st->container_devnm, st->devnm) == 0) { | |
11040 | /* On container level we can only increase number of devices. */ | |
11041 | dprintf("imsm: info: Container operation\n"); | |
11042 | int old_raid_disks = 0; | |
11043 | ||
11044 | if (imsm_reshape_is_allowed_on_container( | |
11045 | st, &geo, &old_raid_disks, direction)) { | |
11046 | struct imsm_update_reshape *u = NULL; | |
11047 | int len; | |
11048 | ||
11049 | len = imsm_create_metadata_update_for_reshape( | |
11050 | st, &geo, old_raid_disks, &u); | |
11051 | ||
11052 | if (len <= 0) { | |
11053 | dprintf("imsm: Cannot prepare update\n"); | |
11054 | goto exit_imsm_reshape_super; | |
11055 | } | |
11056 | ||
11057 | ret_val = 0; | |
11058 | /* update metadata locally */ | |
11059 | imsm_update_metadata_locally(st, u, len); | |
11060 | /* and possibly remotely */ | |
11061 | if (st->update_tail) | |
11062 | append_metadata_update(st, u, len); | |
11063 | else | |
11064 | free(u); | |
11065 | ||
11066 | } else { | |
11067 | pr_err("(imsm) Operation is not allowed on this container\n"); | |
11068 | } | |
11069 | } else { | |
11070 | /* On volume level we support following operations | |
11071 | * - takeover: raid10 -> raid0; raid0 -> raid10 | |
11072 | * - chunk size migration | |
11073 | * - migration: raid5 -> raid0; raid0 -> raid5 | |
11074 | */ | |
11075 | struct intel_super *super = st->sb; | |
11076 | struct intel_dev *dev = super->devlist; | |
11077 | int change; | |
11078 | dprintf("imsm: info: Volume operation\n"); | |
11079 | /* find requested device */ | |
11080 | while (dev) { | |
11081 | char *devnm = | |
11082 | imsm_find_array_devnm_by_subdev( | |
11083 | dev->index, st->container_devnm); | |
11084 | if (devnm && strcmp(devnm, geo.devnm) == 0) | |
11085 | break; | |
11086 | dev = dev->next; | |
11087 | } | |
11088 | if (dev == NULL) { | |
11089 | pr_err("Cannot find %s (%s) subarray\n", | |
11090 | geo.dev_name, geo.devnm); | |
11091 | goto exit_imsm_reshape_super; | |
11092 | } | |
11093 | super->current_vol = dev->index; | |
11094 | change = imsm_analyze_change(st, &geo, direction); | |
11095 | switch (change) { | |
11096 | case CH_TAKEOVER: | |
11097 | ret_val = imsm_takeover(st, &geo); | |
11098 | break; | |
11099 | case CH_MIGRATION: { | |
11100 | struct imsm_update_reshape_migration *u = NULL; | |
11101 | int len = | |
11102 | imsm_create_metadata_update_for_migration( | |
11103 | st, &geo, &u); | |
11104 | if (len < 1) { | |
11105 | dprintf("imsm: Cannot prepare update\n"); | |
11106 | break; | |
11107 | } | |
11108 | ret_val = 0; | |
11109 | /* update metadata locally */ | |
11110 | imsm_update_metadata_locally(st, u, len); | |
11111 | /* and possibly remotely */ | |
11112 | if (st->update_tail) | |
11113 | append_metadata_update(st, u, len); | |
11114 | else | |
11115 | free(u); | |
11116 | } | |
11117 | break; | |
11118 | case CH_ARRAY_SIZE: { | |
11119 | struct imsm_update_size_change *u = NULL; | |
11120 | int len = | |
11121 | imsm_create_metadata_update_for_size_change( | |
11122 | st, &geo, &u); | |
11123 | if (len < 1) { | |
11124 | dprintf("imsm: Cannot prepare update\n"); | |
11125 | break; | |
11126 | } | |
11127 | ret_val = 0; | |
11128 | /* update metadata locally */ | |
11129 | imsm_update_metadata_locally(st, u, len); | |
11130 | /* and possibly remotely */ | |
11131 | if (st->update_tail) | |
11132 | append_metadata_update(st, u, len); | |
11133 | else | |
11134 | free(u); | |
11135 | } | |
11136 | break; | |
11137 | default: | |
11138 | ret_val = 1; | |
11139 | } | |
11140 | } | |
11141 | ||
11142 | exit_imsm_reshape_super: | |
11143 | dprintf("imsm: reshape_super Exit code = %i\n", ret_val); | |
11144 | return ret_val; | |
11145 | } | |
11146 | ||
11147 | #define COMPLETED_OK 0 | |
11148 | #define COMPLETED_NONE 1 | |
11149 | #define COMPLETED_DELAYED 2 | |
11150 | ||
11151 | static int read_completed(int fd, unsigned long long *val) | |
11152 | { | |
11153 | int ret; | |
11154 | char buf[50]; | |
11155 | ||
11156 | ret = sysfs_fd_get_str(fd, buf, 50); | |
11157 | if (ret < 0) | |
11158 | return ret; | |
11159 | ||
11160 | ret = COMPLETED_OK; | |
11161 | if (strncmp(buf, "none", 4) == 0) { | |
11162 | ret = COMPLETED_NONE; | |
11163 | } else if (strncmp(buf, "delayed", 7) == 0) { | |
11164 | ret = COMPLETED_DELAYED; | |
11165 | } else { | |
11166 | char *ep; | |
11167 | *val = strtoull(buf, &ep, 0); | |
11168 | if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' ')) | |
11169 | ret = -1; | |
11170 | } | |
11171 | return ret; | |
11172 | } | |
11173 | ||
11174 | /******************************************************************************* | |
11175 | * Function: wait_for_reshape_imsm | |
11176 | * Description: Function writes new sync_max value and waits until | |
11177 | * reshape process reach new position | |
11178 | * Parameters: | |
11179 | * sra : general array info | |
11180 | * ndata : number of disks in new array's layout | |
11181 | * Returns: | |
11182 | * 0 : success, | |
11183 | * 1 : there is no reshape in progress, | |
11184 | * -1 : fail | |
11185 | ******************************************************************************/ | |
11186 | int wait_for_reshape_imsm(struct mdinfo *sra, int ndata) | |
11187 | { | |
11188 | int fd = sysfs_get_fd(sra, NULL, "sync_completed"); | |
11189 | int retry = 3; | |
11190 | unsigned long long completed; | |
11191 | /* to_complete : new sync_max position */ | |
11192 | unsigned long long to_complete = sra->reshape_progress; | |
11193 | unsigned long long position_to_set = to_complete / ndata; | |
11194 | ||
11195 | if (fd < 0) { | |
11196 | dprintf("cannot open reshape_position\n"); | |
11197 | return 1; | |
11198 | } | |
11199 | ||
11200 | do { | |
11201 | if (sysfs_fd_get_ll(fd, &completed) < 0) { | |
11202 | if (!retry) { | |
11203 | dprintf("cannot read reshape_position (no reshape in progres)\n"); | |
11204 | close(fd); | |
11205 | return 1; | |
11206 | } | |
11207 | usleep(30000); | |
11208 | } else | |
11209 | break; | |
11210 | } while (retry--); | |
11211 | ||
11212 | if (completed > position_to_set) { | |
11213 | dprintf("wrong next position to set %llu (%llu)\n", | |
11214 | to_complete, position_to_set); | |
11215 | close(fd); | |
11216 | return -1; | |
11217 | } | |
11218 | dprintf("Position set: %llu\n", position_to_set); | |
11219 | if (sysfs_set_num(sra, NULL, "sync_max", | |
11220 | position_to_set) != 0) { | |
11221 | dprintf("cannot set reshape position to %llu\n", | |
11222 | position_to_set); | |
11223 | close(fd); | |
11224 | return -1; | |
11225 | } | |
11226 | ||
11227 | do { | |
11228 | int rc; | |
11229 | char action[20]; | |
11230 | int timeout = 3000; | |
11231 | ||
11232 | sysfs_wait(fd, &timeout); | |
11233 | if (sysfs_get_str(sra, NULL, "sync_action", | |
11234 | action, 20) > 0 && | |
11235 | strncmp(action, "reshape", 7) != 0) { | |
11236 | if (strncmp(action, "idle", 4) == 0) | |
11237 | break; | |
11238 | close(fd); | |
11239 | return -1; | |
11240 | } | |
11241 | ||
11242 | rc = read_completed(fd, &completed); | |
11243 | if (rc < 0) { | |
11244 | dprintf("cannot read reshape_position (in loop)\n"); | |
11245 | close(fd); | |
11246 | return 1; | |
11247 | } else if (rc == COMPLETED_NONE) | |
11248 | break; | |
11249 | } while (completed < position_to_set); | |
11250 | ||
11251 | close(fd); | |
11252 | return 0; | |
11253 | } | |
11254 | ||
11255 | /******************************************************************************* | |
11256 | * Function: check_degradation_change | |
11257 | * Description: Check that array hasn't become failed. | |
11258 | * Parameters: | |
11259 | * info : for sysfs access | |
11260 | * sources : source disks descriptors | |
11261 | * degraded: previous degradation level | |
11262 | * Returns: | |
11263 | * degradation level | |
11264 | ******************************************************************************/ | |
11265 | int check_degradation_change(struct mdinfo *info, | |
11266 | int *sources, | |
11267 | int degraded) | |
11268 | { | |
11269 | unsigned long long new_degraded; | |
11270 | int rv; | |
11271 | ||
11272 | rv = sysfs_get_ll(info, NULL, "degraded", &new_degraded); | |
11273 | if (rv == -1 || (new_degraded != (unsigned long long)degraded)) { | |
11274 | /* check each device to ensure it is still working */ | |
11275 | struct mdinfo *sd; | |
11276 | new_degraded = 0; | |
11277 | for (sd = info->devs ; sd ; sd = sd->next) { | |
11278 | if (sd->disk.state & (1<<MD_DISK_FAULTY)) | |
11279 | continue; | |
11280 | if (sd->disk.state & (1<<MD_DISK_SYNC)) { | |
11281 | char sbuf[100]; | |
11282 | ||
11283 | if (sysfs_get_str(info, | |
11284 | sd, "state", sbuf, sizeof(sbuf)) < 0 || | |
11285 | strstr(sbuf, "faulty") || | |
11286 | strstr(sbuf, "in_sync") == NULL) { | |
11287 | /* this device is dead */ | |
11288 | sd->disk.state = (1<<MD_DISK_FAULTY); | |
11289 | if (sd->disk.raid_disk >= 0 && | |
11290 | sources[sd->disk.raid_disk] >= 0) { | |
11291 | close(sources[ | |
11292 | sd->disk.raid_disk]); | |
11293 | sources[sd->disk.raid_disk] = | |
11294 | -1; | |
11295 | } | |
11296 | new_degraded++; | |
11297 | } | |
11298 | } | |
11299 | } | |
11300 | } | |
11301 | ||
11302 | return new_degraded; | |
11303 | } | |
11304 | ||
11305 | /******************************************************************************* | |
11306 | * Function: imsm_manage_reshape | |
11307 | * Description: Function finds array under reshape and it manages reshape | |
11308 | * process. It creates stripes backups (if required) and sets | |
11309 | * checkpoints. | |
11310 | * Parameters: | |
11311 | * afd : Backup handle (nattive) - not used | |
11312 | * sra : general array info | |
11313 | * reshape : reshape parameters - not used | |
11314 | * st : supertype structure | |
11315 | * blocks : size of critical section [blocks] | |
11316 | * fds : table of source device descriptor | |
11317 | * offsets : start of array (offest per devices) | |
11318 | * dests : not used | |
11319 | * destfd : table of destination device descriptor | |
11320 | * destoffsets : table of destination offsets (per device) | |
11321 | * Returns: | |
11322 | * 1 : success, reshape is done | |
11323 | * 0 : fail | |
11324 | ******************************************************************************/ | |
11325 | static int imsm_manage_reshape( | |
11326 | int afd, struct mdinfo *sra, struct reshape *reshape, | |
11327 | struct supertype *st, unsigned long backup_blocks, | |
11328 | int *fds, unsigned long long *offsets, | |
11329 | int dests, int *destfd, unsigned long long *destoffsets) | |
11330 | { | |
11331 | int ret_val = 0; | |
11332 | struct intel_super *super = st->sb; | |
11333 | struct intel_dev *dv; | |
11334 | unsigned int sector_size = super->sector_size; | |
11335 | struct imsm_dev *dev = NULL; | |
11336 | struct imsm_map *map_src; | |
11337 | int migr_vol_qan = 0; | |
11338 | int ndata, odata; /* [bytes] */ | |
11339 | int chunk; /* [bytes] */ | |
11340 | struct migr_record *migr_rec; | |
11341 | char *buf = NULL; | |
11342 | unsigned int buf_size; /* [bytes] */ | |
11343 | unsigned long long max_position; /* array size [bytes] */ | |
11344 | unsigned long long next_step; /* [blocks]/[bytes] */ | |
11345 | unsigned long long old_data_stripe_length; | |
11346 | unsigned long long start_src; /* [bytes] */ | |
11347 | unsigned long long start; /* [bytes] */ | |
11348 | unsigned long long start_buf_shift; /* [bytes] */ | |
11349 | int degraded = 0; | |
11350 | int source_layout = 0; | |
11351 | ||
11352 | if (!sra) | |
11353 | return ret_val; | |
11354 | ||
11355 | if (!fds || !offsets) | |
11356 | goto abort; | |
11357 | ||
11358 | /* Find volume during the reshape */ | |
11359 | for (dv = super->devlist; dv; dv = dv->next) { | |
11360 | if (dv->dev->vol.migr_type == MIGR_GEN_MIGR | |
11361 | && dv->dev->vol.migr_state == 1) { | |
11362 | dev = dv->dev; | |
11363 | migr_vol_qan++; | |
11364 | } | |
11365 | } | |
11366 | /* Only one volume can migrate at the same time */ | |
11367 | if (migr_vol_qan != 1) { | |
11368 | pr_err("%s", migr_vol_qan ? | |
11369 | "Number of migrating volumes greater than 1\n" : | |
11370 | "There is no volume during migrationg\n"); | |
11371 | goto abort; | |
11372 | } | |
11373 | ||
11374 | map_src = get_imsm_map(dev, MAP_1); | |
11375 | if (map_src == NULL) | |
11376 | goto abort; | |
11377 | ||
11378 | ndata = imsm_num_data_members(dev, MAP_0); | |
11379 | odata = imsm_num_data_members(dev, MAP_1); | |
11380 | ||
11381 | chunk = __le16_to_cpu(map_src->blocks_per_strip) * 512; | |
11382 | old_data_stripe_length = odata * chunk; | |
11383 | ||
11384 | migr_rec = super->migr_rec; | |
11385 | ||
11386 | /* initialize migration record for start condition */ | |
11387 | if (sra->reshape_progress == 0) | |
11388 | init_migr_record_imsm(st, dev, sra); | |
11389 | else { | |
11390 | if (__le32_to_cpu(migr_rec->rec_status) != UNIT_SRC_NORMAL) { | |
11391 | dprintf("imsm: cannot restart migration when data are present in copy area.\n"); | |
11392 | goto abort; | |
11393 | } | |
11394 | /* Save checkpoint to update migration record for current | |
11395 | * reshape position (in md). It can be farther than current | |
11396 | * reshape position in metadata. | |
11397 | */ | |
11398 | if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL) == 1) { | |
11399 | /* ignore error == 2, this can mean end of reshape here | |
11400 | */ | |
11401 | dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL, initial save)\n"); | |
11402 | goto abort; | |
11403 | } | |
11404 | } | |
11405 | ||
11406 | /* size for data */ | |
11407 | buf_size = __le32_to_cpu(migr_rec->blocks_per_unit) * 512; | |
11408 | /* extend buffer size for parity disk */ | |
11409 | buf_size += __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512; | |
11410 | /* add space for stripe aligment */ | |
11411 | buf_size += old_data_stripe_length; | |
11412 | if (posix_memalign((void **)&buf, MAX_SECTOR_SIZE, buf_size)) { | |
11413 | dprintf("imsm: Cannot allocate checkpoint buffer\n"); | |
11414 | goto abort; | |
11415 | } | |
11416 | ||
11417 | max_position = sra->component_size * ndata; | |
11418 | source_layout = imsm_level_to_layout(map_src->raid_level); | |
11419 | ||
11420 | while (__le32_to_cpu(migr_rec->curr_migr_unit) < | |
11421 | __le32_to_cpu(migr_rec->num_migr_units)) { | |
11422 | /* current reshape position [blocks] */ | |
11423 | unsigned long long current_position = | |
11424 | __le32_to_cpu(migr_rec->blocks_per_unit) | |
11425 | * __le32_to_cpu(migr_rec->curr_migr_unit); | |
11426 | unsigned long long border; | |
11427 | ||
11428 | /* Check that array hasn't become failed. | |
11429 | */ | |
11430 | degraded = check_degradation_change(sra, fds, degraded); | |
11431 | if (degraded > 1) { | |
11432 | dprintf("imsm: Abort reshape due to degradation level (%i)\n", degraded); | |
11433 | goto abort; | |
11434 | } | |
11435 | ||
11436 | next_step = __le32_to_cpu(migr_rec->blocks_per_unit); | |
11437 | ||
11438 | if ((current_position + next_step) > max_position) | |
11439 | next_step = max_position - current_position; | |
11440 | ||
11441 | start = current_position * 512; | |
11442 | ||
11443 | /* align reading start to old geometry */ | |
11444 | start_buf_shift = start % old_data_stripe_length; | |
11445 | start_src = start - start_buf_shift; | |
11446 | ||
11447 | border = (start_src / odata) - (start / ndata); | |
11448 | border /= 512; | |
11449 | if (border <= __le32_to_cpu(migr_rec->dest_depth_per_unit)) { | |
11450 | /* save critical stripes to buf | |
11451 | * start - start address of current unit | |
11452 | * to backup [bytes] | |
11453 | * start_src - start address of current unit | |
11454 | * to backup alligned to source array | |
11455 | * [bytes] | |
11456 | */ | |
11457 | unsigned long long next_step_filler; | |
11458 | unsigned long long copy_length = next_step * 512; | |
11459 | ||
11460 | /* allign copy area length to stripe in old geometry */ | |
11461 | next_step_filler = ((copy_length + start_buf_shift) | |
11462 | % old_data_stripe_length); | |
11463 | if (next_step_filler) | |
11464 | next_step_filler = (old_data_stripe_length | |
11465 | - next_step_filler); | |
11466 | dprintf("save_stripes() parameters: start = %llu,\tstart_src = %llu,\tnext_step*512 = %llu,\tstart_in_buf_shift = %llu,\tnext_step_filler = %llu\n", | |
11467 | start, start_src, copy_length, | |
11468 | start_buf_shift, next_step_filler); | |
11469 | ||
11470 | if (save_stripes(fds, offsets, map_src->num_members, | |
11471 | chunk, map_src->raid_level, | |
11472 | source_layout, 0, NULL, start_src, | |
11473 | copy_length + | |
11474 | next_step_filler + start_buf_shift, | |
11475 | buf)) { | |
11476 | dprintf("imsm: Cannot save stripes to buffer\n"); | |
11477 | goto abort; | |
11478 | } | |
11479 | /* Convert data to destination format and store it | |
11480 | * in backup general migration area | |
11481 | */ | |
11482 | if (save_backup_imsm(st, dev, sra, | |
11483 | buf + start_buf_shift, copy_length)) { | |
11484 | dprintf("imsm: Cannot save stripes to target devices\n"); | |
11485 | goto abort; | |
11486 | } | |
11487 | if (save_checkpoint_imsm(st, sra, | |
11488 | UNIT_SRC_IN_CP_AREA)) { | |
11489 | dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_IN_CP_AREA)\n"); | |
11490 | goto abort; | |
11491 | } | |
11492 | } else { | |
11493 | /* set next step to use whole border area */ | |
11494 | border /= next_step; | |
11495 | if (border > 1) | |
11496 | next_step *= border; | |
11497 | } | |
11498 | /* When data backed up, checkpoint stored, | |
11499 | * kick the kernel to reshape unit of data | |
11500 | */ | |
11501 | next_step = next_step + sra->reshape_progress; | |
11502 | /* limit next step to array max position */ | |
11503 | if (next_step > max_position) | |
11504 | next_step = max_position; | |
11505 | sysfs_set_num(sra, NULL, "suspend_lo", sra->reshape_progress); | |
11506 | sysfs_set_num(sra, NULL, "suspend_hi", next_step); | |
11507 | sra->reshape_progress = next_step; | |
11508 | ||
11509 | /* wait until reshape finish */ | |
11510 | if (wait_for_reshape_imsm(sra, ndata)) { | |
11511 | dprintf("wait_for_reshape_imsm returned error!\n"); | |
11512 | goto abort; | |
11513 | } | |
11514 | if (sigterm) | |
11515 | goto abort; | |
11516 | ||
11517 | if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL) == 1) { | |
11518 | /* ignore error == 2, this can mean end of reshape here | |
11519 | */ | |
11520 | dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL)\n"); | |
11521 | goto abort; | |
11522 | } | |
11523 | ||
11524 | } | |
11525 | ||
11526 | /* clear migr_rec on disks after successful migration */ | |
11527 | struct dl *d; | |
11528 | ||
11529 | memset(super->migr_rec_buf, 0, MIGR_REC_BUF_SECTORS*sector_size); | |
11530 | for (d = super->disks; d; d = d->next) { | |
11531 | if (d->index < 0 || is_failed(&d->disk)) | |
11532 | continue; | |
11533 | unsigned long long dsize; | |
11534 | ||
11535 | get_dev_size(d->fd, NULL, &dsize); | |
11536 | if (lseek64(d->fd, dsize - MIGR_REC_SECTOR_POSITION*sector_size, | |
11537 | SEEK_SET) >= 0) { | |
11538 | if ((unsigned int)write(d->fd, super->migr_rec_buf, | |
11539 | MIGR_REC_BUF_SECTORS*sector_size) != | |
11540 | MIGR_REC_BUF_SECTORS*sector_size) | |
11541 | perror("Write migr_rec failed"); | |
11542 | } | |
11543 | } | |
11544 | ||
11545 | /* return '1' if done */ | |
11546 | ret_val = 1; | |
11547 | abort: | |
11548 | free(buf); | |
11549 | /* See Grow.c: abort_reshape() for further explanation */ | |
11550 | sysfs_set_num(sra, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL); | |
11551 | sysfs_set_num(sra, NULL, "suspend_hi", 0); | |
11552 | sysfs_set_num(sra, NULL, "suspend_lo", 0); | |
11553 | ||
11554 | return ret_val; | |
11555 | } | |
11556 | ||
11557 | #endif /* MDASSEMBLE */ | |
11558 | ||
11559 | struct superswitch super_imsm = { | |
11560 | #ifndef MDASSEMBLE | |
11561 | .examine_super = examine_super_imsm, | |
11562 | .brief_examine_super = brief_examine_super_imsm, | |
11563 | .brief_examine_subarrays = brief_examine_subarrays_imsm, | |
11564 | .export_examine_super = export_examine_super_imsm, | |
11565 | .detail_super = detail_super_imsm, | |
11566 | .brief_detail_super = brief_detail_super_imsm, | |
11567 | .write_init_super = write_init_super_imsm, | |
11568 | .validate_geometry = validate_geometry_imsm, | |
11569 | .add_to_super = add_to_super_imsm, | |
11570 | .remove_from_super = remove_from_super_imsm, | |
11571 | .detail_platform = detail_platform_imsm, | |
11572 | .export_detail_platform = export_detail_platform_imsm, | |
11573 | .kill_subarray = kill_subarray_imsm, | |
11574 | .update_subarray = update_subarray_imsm, | |
11575 | .load_container = load_container_imsm, | |
11576 | .default_geometry = default_geometry_imsm, | |
11577 | .get_disk_controller_domain = imsm_get_disk_controller_domain, | |
11578 | .reshape_super = imsm_reshape_super, | |
11579 | .manage_reshape = imsm_manage_reshape, | |
11580 | .recover_backup = recover_backup_imsm, | |
11581 | .copy_metadata = copy_metadata_imsm, | |
11582 | .examine_badblocks = examine_badblocks_imsm, | |
11583 | #endif | |
11584 | .match_home = match_home_imsm, | |
11585 | .uuid_from_super= uuid_from_super_imsm, | |
11586 | .getinfo_super = getinfo_super_imsm, | |
11587 | .getinfo_super_disks = getinfo_super_disks_imsm, | |
11588 | .update_super = update_super_imsm, | |
11589 | ||
11590 | .avail_size = avail_size_imsm, | |
11591 | .min_acceptable_spare_size = min_acceptable_spare_size_imsm, | |
11592 | ||
11593 | .compare_super = compare_super_imsm, | |
11594 | ||
11595 | .load_super = load_super_imsm, | |
11596 | .init_super = init_super_imsm, | |
11597 | .store_super = store_super_imsm, | |
11598 | .free_super = free_super_imsm, | |
11599 | .match_metadata_desc = match_metadata_desc_imsm, | |
11600 | .container_content = container_content_imsm, | |
11601 | .validate_container = validate_container_imsm, | |
11602 | ||
11603 | .external = 1, | |
11604 | .name = "imsm", | |
11605 | ||
11606 | #ifndef MDASSEMBLE | |
11607 | /* for mdmon */ | |
11608 | .open_new = imsm_open_new, | |
11609 | .set_array_state= imsm_set_array_state, | |
11610 | .set_disk = imsm_set_disk, | |
11611 | .sync_metadata = imsm_sync_metadata, | |
11612 | .activate_spare = imsm_activate_spare, | |
11613 | .process_update = imsm_process_update, | |
11614 | .prepare_update = imsm_prepare_update, | |
11615 | .record_bad_block = imsm_record_badblock, | |
11616 | .clear_bad_block = imsm_clear_badblock, | |
11617 | .get_bad_blocks = imsm_get_badblocks, | |
11618 | #endif /* MDASSEMBLE */ | |
11619 | }; |