]>
Commit | Line | Data |
---|---|---|
cdddbdbc DW |
1 | /* |
2 | * mdadm - Intel(R) Matrix Storage Manager Support | |
3 | * | |
a54d5262 | 4 | * Copyright (C) 2002-2008 Intel Corporation |
cdddbdbc DW |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify it | |
7 | * under the terms and conditions of the GNU General Public License, | |
8 | * version 2, as published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope it will be useful, but WITHOUT | |
11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
13 | * more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along with | |
16 | * this program; if not, write to the Free Software Foundation, Inc., | |
17 | * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. | |
18 | */ | |
19 | ||
51006d85 | 20 | #define HAVE_STDINT_H 1 |
cdddbdbc | 21 | #include "mdadm.h" |
c2a1e7da | 22 | #include "mdmon.h" |
51006d85 | 23 | #include "sha1.h" |
88c32bb1 | 24 | #include "platform-intel.h" |
cdddbdbc DW |
25 | #include <values.h> |
26 | #include <scsi/sg.h> | |
27 | #include <ctype.h> | |
d665cc31 | 28 | #include <dirent.h> |
cdddbdbc DW |
29 | |
30 | /* MPB == Metadata Parameter Block */ | |
31 | #define MPB_SIGNATURE "Intel Raid ISM Cfg Sig. " | |
32 | #define MPB_SIG_LEN (strlen(MPB_SIGNATURE)) | |
33 | #define MPB_VERSION_RAID0 "1.0.00" | |
34 | #define MPB_VERSION_RAID1 "1.1.00" | |
fe7ed8cb DW |
35 | #define MPB_VERSION_MANY_VOLUMES_PER_ARRAY "1.2.00" |
36 | #define MPB_VERSION_3OR4_DISK_ARRAY "1.2.01" | |
cdddbdbc | 37 | #define MPB_VERSION_RAID5 "1.2.02" |
fe7ed8cb DW |
38 | #define MPB_VERSION_5OR6_DISK_ARRAY "1.2.04" |
39 | #define MPB_VERSION_CNG "1.2.06" | |
40 | #define MPB_VERSION_ATTRIBS "1.3.00" | |
cdddbdbc DW |
41 | #define MAX_SIGNATURE_LENGTH 32 |
42 | #define MAX_RAID_SERIAL_LEN 16 | |
fe7ed8cb DW |
43 | |
44 | #define MPB_ATTRIB_CHECKSUM_VERIFY __cpu_to_le32(0x80000000) | |
45 | #define MPB_ATTRIB_PM __cpu_to_le32(0x40000000) | |
46 | #define MPB_ATTRIB_2TB __cpu_to_le32(0x20000000) | |
47 | #define MPB_ATTRIB_RAID0 __cpu_to_le32(0x00000001) | |
48 | #define MPB_ATTRIB_RAID1 __cpu_to_le32(0x00000002) | |
49 | #define MPB_ATTRIB_RAID10 __cpu_to_le32(0x00000004) | |
50 | #define MPB_ATTRIB_RAID1E __cpu_to_le32(0x00000008) | |
51 | #define MPB_ATTRIB_RAID5 __cpu_to_le32(0x00000010) | |
52 | #define MPB_ATTRIB_RAIDCNG __cpu_to_le32(0x00000020) | |
53 | ||
c2c087e6 DW |
54 | #define MPB_SECTOR_CNT 418 |
55 | #define IMSM_RESERVED_SECTORS 4096 | |
979d38be | 56 | #define SECT_PER_MB_SHIFT 11 |
cdddbdbc DW |
57 | |
58 | /* Disk configuration info. */ | |
59 | #define IMSM_MAX_DEVICES 255 | |
60 | struct imsm_disk { | |
61 | __u8 serial[MAX_RAID_SERIAL_LEN];/* 0xD8 - 0xE7 ascii serial number */ | |
62 | __u32 total_blocks; /* 0xE8 - 0xEB total blocks */ | |
63 | __u32 scsi_id; /* 0xEC - 0xEF scsi ID */ | |
f2f27e63 DW |
64 | #define SPARE_DISK __cpu_to_le32(0x01) /* Spare */ |
65 | #define CONFIGURED_DISK __cpu_to_le32(0x02) /* Member of some RaidDev */ | |
66 | #define FAILED_DISK __cpu_to_le32(0x04) /* Permanent failure */ | |
cdddbdbc | 67 | __u32 status; /* 0xF0 - 0xF3 */ |
fe7ed8cb DW |
68 | __u32 owner_cfg_num; /* which config 0,1,2... owns this disk */ |
69 | #define IMSM_DISK_FILLERS 4 | |
cdddbdbc DW |
70 | __u32 filler[IMSM_DISK_FILLERS]; /* 0xF4 - 0x107 MPB_DISK_FILLERS for future expansion */ |
71 | }; | |
72 | ||
73 | /* RAID map configuration infos. */ | |
74 | struct imsm_map { | |
75 | __u32 pba_of_lba0; /* start address of partition */ | |
76 | __u32 blocks_per_member;/* blocks per member */ | |
77 | __u32 num_data_stripes; /* number of data stripes */ | |
78 | __u16 blocks_per_strip; | |
79 | __u8 map_state; /* Normal, Uninitialized, Degraded, Failed */ | |
80 | #define IMSM_T_STATE_NORMAL 0 | |
81 | #define IMSM_T_STATE_UNINITIALIZED 1 | |
e3bba0e0 DW |
82 | #define IMSM_T_STATE_DEGRADED 2 |
83 | #define IMSM_T_STATE_FAILED 3 | |
cdddbdbc DW |
84 | __u8 raid_level; |
85 | #define IMSM_T_RAID0 0 | |
86 | #define IMSM_T_RAID1 1 | |
87 | #define IMSM_T_RAID5 5 /* since metadata version 1.2.02 ? */ | |
88 | __u8 num_members; /* number of member disks */ | |
fe7ed8cb DW |
89 | __u8 num_domains; /* number of parity domains */ |
90 | __u8 failed_disk_num; /* valid only when state is degraded */ | |
252d23c0 | 91 | __u8 ddf; |
cdddbdbc | 92 | __u32 filler[7]; /* expansion area */ |
7eef0453 | 93 | #define IMSM_ORD_REBUILD (1 << 24) |
cdddbdbc | 94 | __u32 disk_ord_tbl[1]; /* disk_ord_tbl[num_members], |
7eef0453 DW |
95 | * top byte contains some flags |
96 | */ | |
cdddbdbc DW |
97 | } __attribute__ ((packed)); |
98 | ||
99 | struct imsm_vol { | |
f8f603f1 | 100 | __u32 curr_migr_unit; |
fe7ed8cb | 101 | __u32 checkpoint_id; /* id to access curr_migr_unit */ |
cdddbdbc | 102 | __u8 migr_state; /* Normal or Migrating */ |
e3bba0e0 DW |
103 | #define MIGR_INIT 0 |
104 | #define MIGR_REBUILD 1 | |
105 | #define MIGR_VERIFY 2 /* analagous to echo check > sync_action */ | |
106 | #define MIGR_GEN_MIGR 3 | |
107 | #define MIGR_STATE_CHANGE 4 | |
1484e727 | 108 | #define MIGR_REPAIR 5 |
cdddbdbc DW |
109 | __u8 migr_type; /* Initializing, Rebuilding, ... */ |
110 | __u8 dirty; | |
fe7ed8cb DW |
111 | __u8 fs_state; /* fast-sync state for CnG (0xff == disabled) */ |
112 | __u16 verify_errors; /* number of mismatches */ | |
113 | __u16 bad_blocks; /* number of bad blocks during verify */ | |
114 | __u32 filler[4]; | |
cdddbdbc DW |
115 | struct imsm_map map[1]; |
116 | /* here comes another one if migr_state */ | |
117 | } __attribute__ ((packed)); | |
118 | ||
119 | struct imsm_dev { | |
fe7ed8cb | 120 | __u8 volume[MAX_RAID_SERIAL_LEN]; |
cdddbdbc DW |
121 | __u32 size_low; |
122 | __u32 size_high; | |
fe7ed8cb DW |
123 | #define DEV_BOOTABLE __cpu_to_le32(0x01) |
124 | #define DEV_BOOT_DEVICE __cpu_to_le32(0x02) | |
125 | #define DEV_READ_COALESCING __cpu_to_le32(0x04) | |
126 | #define DEV_WRITE_COALESCING __cpu_to_le32(0x08) | |
127 | #define DEV_LAST_SHUTDOWN_DIRTY __cpu_to_le32(0x10) | |
128 | #define DEV_HIDDEN_AT_BOOT __cpu_to_le32(0x20) | |
129 | #define DEV_CURRENTLY_HIDDEN __cpu_to_le32(0x40) | |
130 | #define DEV_VERIFY_AND_FIX __cpu_to_le32(0x80) | |
131 | #define DEV_MAP_STATE_UNINIT __cpu_to_le32(0x100) | |
132 | #define DEV_NO_AUTO_RECOVERY __cpu_to_le32(0x200) | |
133 | #define DEV_CLONE_N_GO __cpu_to_le32(0x400) | |
134 | #define DEV_CLONE_MAN_SYNC __cpu_to_le32(0x800) | |
135 | #define DEV_CNG_MASTER_DISK_NUM __cpu_to_le32(0x1000) | |
cdddbdbc DW |
136 | __u32 status; /* Persistent RaidDev status */ |
137 | __u32 reserved_blocks; /* Reserved blocks at beginning of volume */ | |
fe7ed8cb DW |
138 | __u8 migr_priority; |
139 | __u8 num_sub_vols; | |
140 | __u8 tid; | |
141 | __u8 cng_master_disk; | |
142 | __u16 cache_policy; | |
143 | __u8 cng_state; | |
144 | __u8 cng_sub_state; | |
145 | #define IMSM_DEV_FILLERS 10 | |
cdddbdbc DW |
146 | __u32 filler[IMSM_DEV_FILLERS]; |
147 | struct imsm_vol vol; | |
148 | } __attribute__ ((packed)); | |
149 | ||
150 | struct imsm_super { | |
151 | __u8 sig[MAX_SIGNATURE_LENGTH]; /* 0x00 - 0x1F */ | |
152 | __u32 check_sum; /* 0x20 - 0x23 MPB Checksum */ | |
153 | __u32 mpb_size; /* 0x24 - 0x27 Size of MPB */ | |
154 | __u32 family_num; /* 0x28 - 0x2B Checksum from first time this config was written */ | |
155 | __u32 generation_num; /* 0x2C - 0x2F Incremented each time this array's MPB is written */ | |
604b746f JD |
156 | __u32 error_log_size; /* 0x30 - 0x33 in bytes */ |
157 | __u32 attributes; /* 0x34 - 0x37 */ | |
cdddbdbc DW |
158 | __u8 num_disks; /* 0x38 Number of configured disks */ |
159 | __u8 num_raid_devs; /* 0x39 Number of configured volumes */ | |
604b746f JD |
160 | __u8 error_log_pos; /* 0x3A */ |
161 | __u8 fill[1]; /* 0x3B */ | |
162 | __u32 cache_size; /* 0x3c - 0x40 in mb */ | |
163 | __u32 orig_family_num; /* 0x40 - 0x43 original family num */ | |
164 | __u32 pwr_cycle_count; /* 0x44 - 0x47 simulated power cycle count for array */ | |
165 | __u32 bbm_log_size; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */ | |
166 | #define IMSM_FILLERS 35 | |
167 | __u32 filler[IMSM_FILLERS]; /* 0x4C - 0xD7 RAID_MPB_FILLERS */ | |
cdddbdbc DW |
168 | struct imsm_disk disk[1]; /* 0xD8 diskTbl[numDisks] */ |
169 | /* here comes imsm_dev[num_raid_devs] */ | |
604b746f | 170 | /* here comes BBM logs */ |
cdddbdbc DW |
171 | } __attribute__ ((packed)); |
172 | ||
604b746f JD |
173 | #define BBM_LOG_MAX_ENTRIES 254 |
174 | ||
175 | struct bbm_log_entry { | |
176 | __u64 defective_block_start; | |
177 | #define UNREADABLE 0xFFFFFFFF | |
178 | __u32 spare_block_offset; | |
179 | __u16 remapped_marked_count; | |
180 | __u16 disk_ordinal; | |
181 | } __attribute__ ((__packed__)); | |
182 | ||
183 | struct bbm_log { | |
184 | __u32 signature; /* 0xABADB10C */ | |
185 | __u32 entry_count; | |
186 | __u32 reserved_spare_block_count; /* 0 */ | |
187 | __u32 reserved; /* 0xFFFF */ | |
188 | __u64 first_spare_lba; | |
189 | struct bbm_log_entry mapped_block_entries[BBM_LOG_MAX_ENTRIES]; | |
190 | } __attribute__ ((__packed__)); | |
191 | ||
192 | ||
cdddbdbc DW |
193 | #ifndef MDASSEMBLE |
194 | static char *map_state_str[] = { "normal", "uninitialized", "degraded", "failed" }; | |
195 | #endif | |
196 | ||
1484e727 DW |
197 | static __u8 migr_type(struct imsm_dev *dev) |
198 | { | |
199 | if (dev->vol.migr_type == MIGR_VERIFY && | |
200 | dev->status & DEV_VERIFY_AND_FIX) | |
201 | return MIGR_REPAIR; | |
202 | else | |
203 | return dev->vol.migr_type; | |
204 | } | |
205 | ||
206 | static void set_migr_type(struct imsm_dev *dev, __u8 migr_type) | |
207 | { | |
208 | /* for compatibility with older oroms convert MIGR_REPAIR, into | |
209 | * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status | |
210 | */ | |
211 | if (migr_type == MIGR_REPAIR) { | |
212 | dev->vol.migr_type = MIGR_VERIFY; | |
213 | dev->status |= DEV_VERIFY_AND_FIX; | |
214 | } else { | |
215 | dev->vol.migr_type = migr_type; | |
216 | dev->status &= ~DEV_VERIFY_AND_FIX; | |
217 | } | |
218 | } | |
219 | ||
87eb16df | 220 | static unsigned int sector_count(__u32 bytes) |
cdddbdbc | 221 | { |
87eb16df DW |
222 | return ((bytes + (512-1)) & (~(512-1))) / 512; |
223 | } | |
cdddbdbc | 224 | |
87eb16df DW |
225 | static unsigned int mpb_sectors(struct imsm_super *mpb) |
226 | { | |
227 | return sector_count(__le32_to_cpu(mpb->mpb_size)); | |
cdddbdbc DW |
228 | } |
229 | ||
ba2de7ba DW |
230 | struct intel_dev { |
231 | struct imsm_dev *dev; | |
232 | struct intel_dev *next; | |
f21e18ca | 233 | unsigned index; |
ba2de7ba DW |
234 | }; |
235 | ||
88654014 LM |
236 | struct intel_hba { |
237 | enum sys_dev_type type; | |
238 | char *path; | |
239 | char *pci_id; | |
240 | struct intel_hba *next; | |
241 | }; | |
242 | ||
1a64be56 LM |
243 | enum action { |
244 | DISK_REMOVE = 1, | |
245 | DISK_ADD | |
246 | }; | |
cdddbdbc DW |
247 | /* internal representation of IMSM metadata */ |
248 | struct intel_super { | |
249 | union { | |
949c47a0 DW |
250 | void *buf; /* O_DIRECT buffer for reading/writing metadata */ |
251 | struct imsm_super *anchor; /* immovable parameters */ | |
cdddbdbc | 252 | }; |
949c47a0 | 253 | size_t len; /* size of the 'buf' allocation */ |
4d7b1503 DW |
254 | void *next_buf; /* for realloc'ing buf from the manager */ |
255 | size_t next_len; | |
c2c087e6 | 256 | int updates_pending; /* count of pending updates for mdmon */ |
bf5a934a | 257 | int current_vol; /* index of raid device undergoing creation */ |
0dcecb2e | 258 | __u32 create_offset; /* common start for 'current_vol' */ |
148acb7b | 259 | __u32 random; /* random data for seeding new family numbers */ |
ba2de7ba | 260 | struct intel_dev *devlist; |
cdddbdbc DW |
261 | struct dl { |
262 | struct dl *next; | |
263 | int index; | |
264 | __u8 serial[MAX_RAID_SERIAL_LEN]; | |
265 | int major, minor; | |
266 | char *devname; | |
b9f594fe | 267 | struct imsm_disk disk; |
cdddbdbc | 268 | int fd; |
0dcecb2e DW |
269 | int extent_cnt; |
270 | struct extent *e; /* for determining freespace @ create */ | |
efb30e7f | 271 | int raiddisk; /* slot to fill in autolayout */ |
1a64be56 | 272 | enum action action; |
cdddbdbc | 273 | } *disks; |
1a64be56 LM |
274 | struct dl *disk_mgmt_list; /* list of disks to add/remove while mdmon |
275 | active */ | |
47ee5a45 | 276 | struct dl *missing; /* disks removed while we weren't looking */ |
43dad3d6 | 277 | struct bbm_log *bbm_log; |
88654014 | 278 | struct intel_hba *hba; /* device path of the raid controller for this metadata */ |
88c32bb1 | 279 | const struct imsm_orom *orom; /* platform firmware support */ |
a2b97981 DW |
280 | struct intel_super *next; /* (temp) list for disambiguating family_num */ |
281 | }; | |
282 | ||
283 | struct intel_disk { | |
284 | struct imsm_disk disk; | |
285 | #define IMSM_UNKNOWN_OWNER (-1) | |
286 | int owner; | |
287 | struct intel_disk *next; | |
cdddbdbc DW |
288 | }; |
289 | ||
c2c087e6 DW |
290 | struct extent { |
291 | unsigned long long start, size; | |
292 | }; | |
293 | ||
694575e7 KW |
294 | /* definitions of reshape process types */ |
295 | enum imsm_reshape_type { | |
296 | CH_TAKEOVER, | |
b5347799 | 297 | CH_MIGRATION, |
694575e7 KW |
298 | }; |
299 | ||
88758e9d DW |
300 | /* definition of messages passed to imsm_process_update */ |
301 | enum imsm_update_type { | |
302 | update_activate_spare, | |
8273f55e | 303 | update_create_array, |
33414a01 | 304 | update_kill_array, |
aa534678 | 305 | update_rename_array, |
1a64be56 | 306 | update_add_remove_disk, |
78b10e66 | 307 | update_reshape_container_disks, |
bb025c2f | 308 | update_takeover |
88758e9d DW |
309 | }; |
310 | ||
311 | struct imsm_update_activate_spare { | |
312 | enum imsm_update_type type; | |
d23fe947 | 313 | struct dl *dl; |
88758e9d DW |
314 | int slot; |
315 | int array; | |
316 | struct imsm_update_activate_spare *next; | |
317 | }; | |
318 | ||
78b10e66 N |
319 | struct geo_params { |
320 | int dev_id; | |
321 | char *dev_name; | |
322 | long long size; | |
323 | int level; | |
324 | int layout; | |
325 | int chunksize; | |
326 | int raid_disks; | |
327 | }; | |
328 | ||
bb025c2f KW |
329 | enum takeover_direction { |
330 | R10_TO_R0, | |
331 | R0_TO_R10 | |
332 | }; | |
333 | struct imsm_update_takeover { | |
334 | enum imsm_update_type type; | |
335 | int subarray; | |
336 | enum takeover_direction direction; | |
337 | }; | |
78b10e66 N |
338 | |
339 | struct imsm_update_reshape { | |
340 | enum imsm_update_type type; | |
341 | int old_raid_disks; | |
342 | int new_raid_disks; | |
d195167d | 343 | int new_disks[1]; /* new_raid_disks - old_raid_disks makedev number */ |
78b10e66 N |
344 | }; |
345 | ||
54c2c1ea DW |
346 | struct disk_info { |
347 | __u8 serial[MAX_RAID_SERIAL_LEN]; | |
348 | }; | |
349 | ||
8273f55e DW |
350 | struct imsm_update_create_array { |
351 | enum imsm_update_type type; | |
8273f55e | 352 | int dev_idx; |
6a3e913e | 353 | struct imsm_dev dev; |
8273f55e DW |
354 | }; |
355 | ||
33414a01 DW |
356 | struct imsm_update_kill_array { |
357 | enum imsm_update_type type; | |
358 | int dev_idx; | |
359 | }; | |
360 | ||
aa534678 DW |
361 | struct imsm_update_rename_array { |
362 | enum imsm_update_type type; | |
363 | __u8 name[MAX_RAID_SERIAL_LEN]; | |
364 | int dev_idx; | |
365 | }; | |
366 | ||
1a64be56 | 367 | struct imsm_update_add_remove_disk { |
43dad3d6 DW |
368 | enum imsm_update_type type; |
369 | }; | |
370 | ||
88654014 LM |
371 | |
372 | static const char *_sys_dev_type[] = { | |
373 | [SYS_DEV_UNKNOWN] = "Unknown", | |
374 | [SYS_DEV_SAS] = "SAS", | |
375 | [SYS_DEV_SATA] = "SATA" | |
376 | }; | |
377 | ||
378 | const char *get_sys_dev_type(enum sys_dev_type type) | |
379 | { | |
380 | if (type >= SYS_DEV_MAX) | |
381 | type = SYS_DEV_UNKNOWN; | |
382 | ||
383 | return _sys_dev_type[type]; | |
384 | } | |
385 | ||
71204a50 | 386 | #ifndef MDASSEMBLE |
88654014 LM |
387 | static struct intel_hba * alloc_intel_hba(struct sys_dev *device) |
388 | { | |
389 | struct intel_hba *result = malloc(sizeof(*result)); | |
390 | if (result) { | |
391 | result->type = device->type; | |
392 | result->path = strdup(device->path); | |
393 | result->next = NULL; | |
394 | if (result->path && (result->pci_id = strrchr(result->path, '/')) != NULL) | |
395 | result->pci_id++; | |
396 | } | |
397 | return result; | |
398 | } | |
399 | ||
400 | static struct intel_hba * find_intel_hba(struct intel_hba *hba, struct sys_dev *device) | |
401 | { | |
402 | struct intel_hba *result=NULL; | |
403 | for (result = hba; result; result = result->next) { | |
404 | if (result->type == device->type && strcmp(result->path, device->path) == 0) | |
405 | break; | |
406 | } | |
407 | return result; | |
408 | } | |
409 | ||
410 | ||
b4cf4cba | 411 | static int attach_hba_to_super(struct intel_super *super, struct sys_dev *device) |
88654014 LM |
412 | { |
413 | struct intel_hba *hba; | |
414 | ||
415 | /* check if disk attached to Intel HBA */ | |
416 | hba = find_intel_hba(super->hba, device); | |
417 | if (hba != NULL) | |
418 | return 1; | |
419 | /* Check if HBA is already attached to super */ | |
420 | if (super->hba == NULL) { | |
421 | super->hba = alloc_intel_hba(device); | |
422 | return 1; | |
423 | } | |
424 | ||
425 | hba = super->hba; | |
426 | /* Intel metadata allows for all disks attached to the same type HBA. | |
427 | * Do not sypport odf HBA types mixing | |
428 | */ | |
429 | if (device->type != hba->type) | |
430 | return 2; | |
431 | ||
432 | while (hba->next) | |
433 | hba = hba->next; | |
434 | ||
435 | hba->next = alloc_intel_hba(device); | |
436 | return 1; | |
437 | } | |
438 | ||
439 | static struct sys_dev* find_disk_attached_hba(int fd, const char *devname) | |
440 | { | |
441 | struct sys_dev *list, *elem, *prev; | |
442 | char *disk_path; | |
443 | ||
444 | if ((list = find_intel_devices()) == NULL) | |
445 | return 0; | |
446 | ||
447 | if (fd < 0) | |
448 | disk_path = (char *) devname; | |
449 | else | |
450 | disk_path = diskfd_to_devpath(fd); | |
451 | ||
452 | if (!disk_path) { | |
453 | free_sys_dev(&list); | |
454 | return 0; | |
455 | } | |
456 | ||
457 | for (prev = NULL, elem = list; elem; prev = elem, elem = elem->next) { | |
458 | if (path_attached_to_hba(disk_path, elem->path)) { | |
459 | if (prev == NULL) | |
460 | list = list->next; | |
461 | else | |
462 | prev->next = elem->next; | |
463 | elem->next = NULL; | |
464 | if (disk_path != devname) | |
465 | free(disk_path); | |
466 | free_sys_dev(&list); | |
467 | return elem; | |
468 | } | |
469 | } | |
470 | if (disk_path != devname) | |
471 | free(disk_path); | |
472 | free_sys_dev(&list); | |
473 | ||
474 | return NULL; | |
475 | } | |
71204a50 | 476 | #endif /* MDASSEMBLE */ |
88654014 LM |
477 | |
478 | ||
d424212e N |
479 | static int find_intel_hba_capability(int fd, struct intel_super *super, |
480 | char *devname); | |
f2f5c343 | 481 | |
cdddbdbc DW |
482 | static struct supertype *match_metadata_desc_imsm(char *arg) |
483 | { | |
484 | struct supertype *st; | |
485 | ||
486 | if (strcmp(arg, "imsm") != 0 && | |
487 | strcmp(arg, "default") != 0 | |
488 | ) | |
489 | return NULL; | |
490 | ||
491 | st = malloc(sizeof(*st)); | |
4e9d2186 AW |
492 | if (!st) |
493 | return NULL; | |
ef609477 | 494 | memset(st, 0, sizeof(*st)); |
d1d599ea | 495 | st->container_dev = NoMdDev; |
cdddbdbc DW |
496 | st->ss = &super_imsm; |
497 | st->max_devs = IMSM_MAX_DEVICES; | |
498 | st->minor_version = 0; | |
499 | st->sb = NULL; | |
500 | return st; | |
501 | } | |
502 | ||
0e600426 | 503 | #ifndef MDASSEMBLE |
cdddbdbc DW |
504 | static __u8 *get_imsm_version(struct imsm_super *mpb) |
505 | { | |
506 | return &mpb->sig[MPB_SIG_LEN]; | |
507 | } | |
0e600426 | 508 | #endif |
cdddbdbc | 509 | |
949c47a0 DW |
510 | /* retrieve a disk directly from the anchor when the anchor is known to be |
511 | * up-to-date, currently only at load time | |
512 | */ | |
513 | static struct imsm_disk *__get_imsm_disk(struct imsm_super *mpb, __u8 index) | |
cdddbdbc | 514 | { |
949c47a0 | 515 | if (index >= mpb->num_disks) |
cdddbdbc DW |
516 | return NULL; |
517 | return &mpb->disk[index]; | |
518 | } | |
519 | ||
95d07a2c LM |
520 | /* retrieve the disk description based on a index of the disk |
521 | * in the sub-array | |
522 | */ | |
523 | static struct dl *get_imsm_dl_disk(struct intel_super *super, __u8 index) | |
949c47a0 | 524 | { |
b9f594fe DW |
525 | struct dl *d; |
526 | ||
527 | for (d = super->disks; d; d = d->next) | |
528 | if (d->index == index) | |
95d07a2c LM |
529 | return d; |
530 | ||
531 | return NULL; | |
532 | } | |
533 | /* retrieve a disk from the parsed metadata */ | |
534 | static struct imsm_disk *get_imsm_disk(struct intel_super *super, __u8 index) | |
535 | { | |
536 | struct dl *dl; | |
537 | ||
538 | dl = get_imsm_dl_disk(super, index); | |
539 | if (dl) | |
540 | return &dl->disk; | |
541 | ||
b9f594fe | 542 | return NULL; |
949c47a0 DW |
543 | } |
544 | ||
545 | /* generate a checksum directly from the anchor when the anchor is known to be | |
546 | * up-to-date, currently only at load or write_super after coalescing | |
547 | */ | |
548 | static __u32 __gen_imsm_checksum(struct imsm_super *mpb) | |
cdddbdbc DW |
549 | { |
550 | __u32 end = mpb->mpb_size / sizeof(end); | |
551 | __u32 *p = (__u32 *) mpb; | |
552 | __u32 sum = 0; | |
553 | ||
97f734fd N |
554 | while (end--) { |
555 | sum += __le32_to_cpu(*p); | |
556 | p++; | |
557 | } | |
cdddbdbc DW |
558 | |
559 | return sum - __le32_to_cpu(mpb->check_sum); | |
560 | } | |
561 | ||
a965f303 DW |
562 | static size_t sizeof_imsm_map(struct imsm_map *map) |
563 | { | |
564 | return sizeof(struct imsm_map) + sizeof(__u32) * (map->num_members - 1); | |
565 | } | |
566 | ||
567 | struct imsm_map *get_imsm_map(struct imsm_dev *dev, int second_map) | |
cdddbdbc | 568 | { |
5e7b0330 AK |
569 | /* A device can have 2 maps if it is in the middle of a migration. |
570 | * If second_map is: | |
571 | * 0 - we return the first map | |
572 | * 1 - we return the second map if it exists, else NULL | |
573 | * -1 - we return the second map if it exists, else the first | |
574 | */ | |
a965f303 DW |
575 | struct imsm_map *map = &dev->vol.map[0]; |
576 | ||
5e7b0330 | 577 | if (second_map == 1 && !dev->vol.migr_state) |
a965f303 | 578 | return NULL; |
5e7b0330 AK |
579 | else if (second_map == 1 || |
580 | (second_map < 0 && dev->vol.migr_state)) { | |
a965f303 DW |
581 | void *ptr = map; |
582 | ||
583 | return ptr + sizeof_imsm_map(map); | |
584 | } else | |
585 | return map; | |
5e7b0330 | 586 | |
a965f303 | 587 | } |
cdddbdbc | 588 | |
3393c6af DW |
589 | /* return the size of the device. |
590 | * migr_state increases the returned size if map[0] were to be duplicated | |
591 | */ | |
592 | static size_t sizeof_imsm_dev(struct imsm_dev *dev, int migr_state) | |
a965f303 DW |
593 | { |
594 | size_t size = sizeof(*dev) - sizeof(struct imsm_map) + | |
595 | sizeof_imsm_map(get_imsm_map(dev, 0)); | |
cdddbdbc DW |
596 | |
597 | /* migrating means an additional map */ | |
a965f303 DW |
598 | if (dev->vol.migr_state) |
599 | size += sizeof_imsm_map(get_imsm_map(dev, 1)); | |
3393c6af DW |
600 | else if (migr_state) |
601 | size += sizeof_imsm_map(get_imsm_map(dev, 0)); | |
cdddbdbc DW |
602 | |
603 | return size; | |
604 | } | |
605 | ||
54c2c1ea DW |
606 | #ifndef MDASSEMBLE |
607 | /* retrieve disk serial number list from a metadata update */ | |
608 | static struct disk_info *get_disk_info(struct imsm_update_create_array *update) | |
609 | { | |
610 | void *u = update; | |
611 | struct disk_info *inf; | |
612 | ||
613 | inf = u + sizeof(*update) - sizeof(struct imsm_dev) + | |
614 | sizeof_imsm_dev(&update->dev, 0); | |
615 | ||
616 | return inf; | |
617 | } | |
618 | #endif | |
619 | ||
949c47a0 | 620 | static struct imsm_dev *__get_imsm_dev(struct imsm_super *mpb, __u8 index) |
cdddbdbc DW |
621 | { |
622 | int offset; | |
623 | int i; | |
624 | void *_mpb = mpb; | |
625 | ||
949c47a0 | 626 | if (index >= mpb->num_raid_devs) |
cdddbdbc DW |
627 | return NULL; |
628 | ||
629 | /* devices start after all disks */ | |
630 | offset = ((void *) &mpb->disk[mpb->num_disks]) - _mpb; | |
631 | ||
632 | for (i = 0; i <= index; i++) | |
633 | if (i == index) | |
634 | return _mpb + offset; | |
635 | else | |
3393c6af | 636 | offset += sizeof_imsm_dev(_mpb + offset, 0); |
cdddbdbc DW |
637 | |
638 | return NULL; | |
639 | } | |
640 | ||
949c47a0 DW |
641 | static struct imsm_dev *get_imsm_dev(struct intel_super *super, __u8 index) |
642 | { | |
ba2de7ba DW |
643 | struct intel_dev *dv; |
644 | ||
949c47a0 DW |
645 | if (index >= super->anchor->num_raid_devs) |
646 | return NULL; | |
ba2de7ba DW |
647 | for (dv = super->devlist; dv; dv = dv->next) |
648 | if (dv->index == index) | |
649 | return dv->dev; | |
650 | return NULL; | |
949c47a0 DW |
651 | } |
652 | ||
98130f40 AK |
653 | /* |
654 | * for second_map: | |
655 | * == 0 get first map | |
656 | * == 1 get second map | |
657 | * == -1 than get map according to the current migr_state | |
658 | */ | |
659 | static __u32 get_imsm_ord_tbl_ent(struct imsm_dev *dev, | |
660 | int slot, | |
661 | int second_map) | |
7eef0453 DW |
662 | { |
663 | struct imsm_map *map; | |
664 | ||
5e7b0330 | 665 | map = get_imsm_map(dev, second_map); |
7eef0453 | 666 | |
ff077194 DW |
667 | /* top byte identifies disk under rebuild */ |
668 | return __le32_to_cpu(map->disk_ord_tbl[slot]); | |
669 | } | |
670 | ||
671 | #define ord_to_idx(ord) (((ord) << 8) >> 8) | |
98130f40 | 672 | static __u32 get_imsm_disk_idx(struct imsm_dev *dev, int slot, int second_map) |
ff077194 | 673 | { |
98130f40 | 674 | __u32 ord = get_imsm_ord_tbl_ent(dev, slot, second_map); |
ff077194 DW |
675 | |
676 | return ord_to_idx(ord); | |
7eef0453 DW |
677 | } |
678 | ||
be73972f DW |
679 | static void set_imsm_ord_tbl_ent(struct imsm_map *map, int slot, __u32 ord) |
680 | { | |
681 | map->disk_ord_tbl[slot] = __cpu_to_le32(ord); | |
682 | } | |
683 | ||
f21e18ca | 684 | static int get_imsm_disk_slot(struct imsm_map *map, unsigned idx) |
620b1713 DW |
685 | { |
686 | int slot; | |
687 | __u32 ord; | |
688 | ||
689 | for (slot = 0; slot < map->num_members; slot++) { | |
690 | ord = __le32_to_cpu(map->disk_ord_tbl[slot]); | |
691 | if (ord_to_idx(ord) == idx) | |
692 | return slot; | |
693 | } | |
694 | ||
695 | return -1; | |
696 | } | |
697 | ||
cdddbdbc DW |
698 | static int get_imsm_raid_level(struct imsm_map *map) |
699 | { | |
700 | if (map->raid_level == 1) { | |
701 | if (map->num_members == 2) | |
702 | return 1; | |
703 | else | |
704 | return 10; | |
705 | } | |
706 | ||
707 | return map->raid_level; | |
708 | } | |
709 | ||
c2c087e6 DW |
710 | static int cmp_extent(const void *av, const void *bv) |
711 | { | |
712 | const struct extent *a = av; | |
713 | const struct extent *b = bv; | |
714 | if (a->start < b->start) | |
715 | return -1; | |
716 | if (a->start > b->start) | |
717 | return 1; | |
718 | return 0; | |
719 | } | |
720 | ||
0dcecb2e | 721 | static int count_memberships(struct dl *dl, struct intel_super *super) |
c2c087e6 | 722 | { |
c2c087e6 | 723 | int memberships = 0; |
620b1713 | 724 | int i; |
c2c087e6 | 725 | |
949c47a0 DW |
726 | for (i = 0; i < super->anchor->num_raid_devs; i++) { |
727 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
a965f303 | 728 | struct imsm_map *map = get_imsm_map(dev, 0); |
c2c087e6 | 729 | |
620b1713 DW |
730 | if (get_imsm_disk_slot(map, dl->index) >= 0) |
731 | memberships++; | |
c2c087e6 | 732 | } |
0dcecb2e DW |
733 | |
734 | return memberships; | |
735 | } | |
736 | ||
737 | static struct extent *get_extents(struct intel_super *super, struct dl *dl) | |
738 | { | |
739 | /* find a list of used extents on the given physical device */ | |
740 | struct extent *rv, *e; | |
620b1713 | 741 | int i; |
0dcecb2e DW |
742 | int memberships = count_memberships(dl, super); |
743 | __u32 reservation = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS; | |
744 | ||
c2c087e6 DW |
745 | rv = malloc(sizeof(struct extent) * (memberships + 1)); |
746 | if (!rv) | |
747 | return NULL; | |
748 | e = rv; | |
749 | ||
949c47a0 DW |
750 | for (i = 0; i < super->anchor->num_raid_devs; i++) { |
751 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
a965f303 | 752 | struct imsm_map *map = get_imsm_map(dev, 0); |
c2c087e6 | 753 | |
620b1713 DW |
754 | if (get_imsm_disk_slot(map, dl->index) >= 0) { |
755 | e->start = __le32_to_cpu(map->pba_of_lba0); | |
756 | e->size = __le32_to_cpu(map->blocks_per_member); | |
757 | e++; | |
c2c087e6 DW |
758 | } |
759 | } | |
760 | qsort(rv, memberships, sizeof(*rv), cmp_extent); | |
761 | ||
14e8215b DW |
762 | /* determine the start of the metadata |
763 | * when no raid devices are defined use the default | |
764 | * ...otherwise allow the metadata to truncate the value | |
765 | * as is the case with older versions of imsm | |
766 | */ | |
767 | if (memberships) { | |
768 | struct extent *last = &rv[memberships - 1]; | |
769 | __u32 remainder; | |
770 | ||
771 | remainder = __le32_to_cpu(dl->disk.total_blocks) - | |
772 | (last->start + last->size); | |
dda5855f DW |
773 | /* round down to 1k block to satisfy precision of the kernel |
774 | * 'size' interface | |
775 | */ | |
776 | remainder &= ~1UL; | |
777 | /* make sure remainder is still sane */ | |
f21e18ca | 778 | if (remainder < (unsigned)ROUND_UP(super->len, 512) >> 9) |
dda5855f | 779 | remainder = ROUND_UP(super->len, 512) >> 9; |
14e8215b DW |
780 | if (reservation > remainder) |
781 | reservation = remainder; | |
782 | } | |
783 | e->start = __le32_to_cpu(dl->disk.total_blocks) - reservation; | |
c2c087e6 DW |
784 | e->size = 0; |
785 | return rv; | |
786 | } | |
787 | ||
14e8215b DW |
788 | /* try to determine how much space is reserved for metadata from |
789 | * the last get_extents() entry, otherwise fallback to the | |
790 | * default | |
791 | */ | |
792 | static __u32 imsm_reserved_sectors(struct intel_super *super, struct dl *dl) | |
793 | { | |
794 | struct extent *e; | |
795 | int i; | |
796 | __u32 rv; | |
797 | ||
798 | /* for spares just return a minimal reservation which will grow | |
799 | * once the spare is picked up by an array | |
800 | */ | |
801 | if (dl->index == -1) | |
802 | return MPB_SECTOR_CNT; | |
803 | ||
804 | e = get_extents(super, dl); | |
805 | if (!e) | |
806 | return MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS; | |
807 | ||
808 | /* scroll to last entry */ | |
809 | for (i = 0; e[i].size; i++) | |
810 | continue; | |
811 | ||
812 | rv = __le32_to_cpu(dl->disk.total_blocks) - e[i].start; | |
813 | ||
814 | free(e); | |
815 | ||
816 | return rv; | |
817 | } | |
818 | ||
25ed7e59 DW |
819 | static int is_spare(struct imsm_disk *disk) |
820 | { | |
821 | return (disk->status & SPARE_DISK) == SPARE_DISK; | |
822 | } | |
823 | ||
824 | static int is_configured(struct imsm_disk *disk) | |
825 | { | |
826 | return (disk->status & CONFIGURED_DISK) == CONFIGURED_DISK; | |
827 | } | |
828 | ||
829 | static int is_failed(struct imsm_disk *disk) | |
830 | { | |
831 | return (disk->status & FAILED_DISK) == FAILED_DISK; | |
832 | } | |
833 | ||
80e7f8c3 AC |
834 | /* Return minimum size of a spare that can be used in this array*/ |
835 | static unsigned long long min_acceptable_spare_size_imsm(struct supertype *st) | |
836 | { | |
837 | struct intel_super *super = st->sb; | |
838 | struct dl *dl; | |
839 | struct extent *e; | |
840 | int i; | |
841 | unsigned long long rv = 0; | |
842 | ||
843 | if (!super) | |
844 | return rv; | |
845 | /* find first active disk in array */ | |
846 | dl = super->disks; | |
847 | while (dl && (is_failed(&dl->disk) || dl->index == -1)) | |
848 | dl = dl->next; | |
849 | if (!dl) | |
850 | return rv; | |
851 | /* find last lba used by subarrays */ | |
852 | e = get_extents(super, dl); | |
853 | if (!e) | |
854 | return rv; | |
855 | for (i = 0; e[i].size; i++) | |
856 | continue; | |
857 | if (i > 0) | |
858 | rv = e[i-1].start + e[i-1].size; | |
859 | free(e); | |
860 | /* add the amount of space needed for metadata */ | |
861 | rv = rv + MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS; | |
862 | return rv * 512; | |
863 | } | |
864 | ||
1799c9e8 | 865 | #ifndef MDASSEMBLE |
1e5c6983 DW |
866 | static __u64 blocks_per_migr_unit(struct imsm_dev *dev); |
867 | ||
44470971 | 868 | static void print_imsm_dev(struct imsm_dev *dev, char *uuid, int disk_idx) |
cdddbdbc DW |
869 | { |
870 | __u64 sz; | |
0d80bb2f | 871 | int slot, i; |
a965f303 | 872 | struct imsm_map *map = get_imsm_map(dev, 0); |
dd8bcb3b | 873 | struct imsm_map *map2 = get_imsm_map(dev, 1); |
b10b37b8 | 874 | __u32 ord; |
cdddbdbc DW |
875 | |
876 | printf("\n"); | |
1e7bc0ed | 877 | printf("[%.16s]:\n", dev->volume); |
44470971 | 878 | printf(" UUID : %s\n", uuid); |
dd8bcb3b AK |
879 | printf(" RAID Level : %d", get_imsm_raid_level(map)); |
880 | if (map2) | |
881 | printf(" <-- %d", get_imsm_raid_level(map2)); | |
882 | printf("\n"); | |
883 | printf(" Members : %d", map->num_members); | |
884 | if (map2) | |
885 | printf(" <-- %d", map2->num_members); | |
886 | printf("\n"); | |
0d80bb2f DW |
887 | printf(" Slots : ["); |
888 | for (i = 0; i < map->num_members; i++) { | |
dd8bcb3b | 889 | ord = get_imsm_ord_tbl_ent(dev, i, 0); |
0d80bb2f DW |
890 | printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U"); |
891 | } | |
dd8bcb3b AK |
892 | printf("]"); |
893 | if (map2) { | |
894 | printf(" <-- ["); | |
895 | for (i = 0; i < map2->num_members; i++) { | |
896 | ord = get_imsm_ord_tbl_ent(dev, i, 1); | |
897 | printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U"); | |
898 | } | |
899 | printf("]"); | |
900 | } | |
901 | printf("\n"); | |
7095bccb AK |
902 | printf(" Failed disk : "); |
903 | if (map->failed_disk_num == 0xff) | |
904 | printf("none"); | |
905 | else | |
906 | printf("%i", map->failed_disk_num); | |
907 | printf("\n"); | |
620b1713 DW |
908 | slot = get_imsm_disk_slot(map, disk_idx); |
909 | if (slot >= 0) { | |
98130f40 | 910 | ord = get_imsm_ord_tbl_ent(dev, slot, -1); |
b10b37b8 DW |
911 | printf(" This Slot : %d%s\n", slot, |
912 | ord & IMSM_ORD_REBUILD ? " (out-of-sync)" : ""); | |
913 | } else | |
cdddbdbc DW |
914 | printf(" This Slot : ?\n"); |
915 | sz = __le32_to_cpu(dev->size_high); | |
916 | sz <<= 32; | |
917 | sz += __le32_to_cpu(dev->size_low); | |
918 | printf(" Array Size : %llu%s\n", (unsigned long long)sz, | |
919 | human_size(sz * 512)); | |
920 | sz = __le32_to_cpu(map->blocks_per_member); | |
921 | printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz, | |
922 | human_size(sz * 512)); | |
923 | printf(" Sector Offset : %u\n", | |
924 | __le32_to_cpu(map->pba_of_lba0)); | |
925 | printf(" Num Stripes : %u\n", | |
926 | __le32_to_cpu(map->num_data_stripes)); | |
dd8bcb3b | 927 | printf(" Chunk Size : %u KiB", |
cdddbdbc | 928 | __le16_to_cpu(map->blocks_per_strip) / 2); |
dd8bcb3b AK |
929 | if (map2) |
930 | printf(" <-- %u KiB", | |
931 | __le16_to_cpu(map2->blocks_per_strip) / 2); | |
932 | printf("\n"); | |
cdddbdbc | 933 | printf(" Reserved : %d\n", __le32_to_cpu(dev->reserved_blocks)); |
8655a7b1 | 934 | printf(" Migrate State : "); |
1484e727 DW |
935 | if (dev->vol.migr_state) { |
936 | if (migr_type(dev) == MIGR_INIT) | |
8655a7b1 | 937 | printf("initialize\n"); |
1484e727 | 938 | else if (migr_type(dev) == MIGR_REBUILD) |
8655a7b1 | 939 | printf("rebuild\n"); |
1484e727 | 940 | else if (migr_type(dev) == MIGR_VERIFY) |
8655a7b1 | 941 | printf("check\n"); |
1484e727 | 942 | else if (migr_type(dev) == MIGR_GEN_MIGR) |
8655a7b1 | 943 | printf("general migration\n"); |
1484e727 | 944 | else if (migr_type(dev) == MIGR_STATE_CHANGE) |
8655a7b1 | 945 | printf("state change\n"); |
1484e727 | 946 | else if (migr_type(dev) == MIGR_REPAIR) |
8655a7b1 | 947 | printf("repair\n"); |
1484e727 | 948 | else |
8655a7b1 DW |
949 | printf("<unknown:%d>\n", migr_type(dev)); |
950 | } else | |
951 | printf("idle\n"); | |
3393c6af DW |
952 | printf(" Map State : %s", map_state_str[map->map_state]); |
953 | if (dev->vol.migr_state) { | |
954 | struct imsm_map *map = get_imsm_map(dev, 1); | |
1e5c6983 | 955 | |
b10b37b8 | 956 | printf(" <-- %s", map_state_str[map->map_state]); |
1e5c6983 DW |
957 | printf("\n Checkpoint : %u (%llu)", |
958 | __le32_to_cpu(dev->vol.curr_migr_unit), | |
94fcb80a | 959 | (unsigned long long)blocks_per_migr_unit(dev)); |
3393c6af DW |
960 | } |
961 | printf("\n"); | |
cdddbdbc | 962 | printf(" Dirty State : %s\n", dev->vol.dirty ? "dirty" : "clean"); |
cdddbdbc DW |
963 | } |
964 | ||
14e8215b | 965 | static void print_imsm_disk(struct imsm_super *mpb, int index, __u32 reserved) |
cdddbdbc | 966 | { |
949c47a0 | 967 | struct imsm_disk *disk = __get_imsm_disk(mpb, index); |
1f24f035 | 968 | char str[MAX_RAID_SERIAL_LEN + 1]; |
cdddbdbc DW |
969 | __u64 sz; |
970 | ||
d362da3d | 971 | if (index < 0 || !disk) |
e9d82038 DW |
972 | return; |
973 | ||
cdddbdbc | 974 | printf("\n"); |
1f24f035 | 975 | snprintf(str, MAX_RAID_SERIAL_LEN + 1, "%s", disk->serial); |
cdddbdbc | 976 | printf(" Disk%02d Serial : %s\n", index, str); |
25ed7e59 DW |
977 | printf(" State :%s%s%s\n", is_spare(disk) ? " spare" : "", |
978 | is_configured(disk) ? " active" : "", | |
979 | is_failed(disk) ? " failed" : ""); | |
cdddbdbc | 980 | printf(" Id : %08x\n", __le32_to_cpu(disk->scsi_id)); |
14e8215b | 981 | sz = __le32_to_cpu(disk->total_blocks) - reserved; |
cdddbdbc DW |
982 | printf(" Usable Size : %llu%s\n", (unsigned long long)sz, |
983 | human_size(sz * 512)); | |
984 | } | |
985 | ||
a5d85af7 | 986 | static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map); |
44470971 | 987 | |
cdddbdbc DW |
988 | static void examine_super_imsm(struct supertype *st, char *homehost) |
989 | { | |
990 | struct intel_super *super = st->sb; | |
949c47a0 | 991 | struct imsm_super *mpb = super->anchor; |
cdddbdbc DW |
992 | char str[MAX_SIGNATURE_LENGTH]; |
993 | int i; | |
27fd6274 DW |
994 | struct mdinfo info; |
995 | char nbuf[64]; | |
cdddbdbc | 996 | __u32 sum; |
14e8215b | 997 | __u32 reserved = imsm_reserved_sectors(super, super->disks); |
94827db3 | 998 | struct dl *dl; |
27fd6274 | 999 | |
cdddbdbc DW |
1000 | snprintf(str, MPB_SIG_LEN, "%s", mpb->sig); |
1001 | printf(" Magic : %s\n", str); | |
1002 | snprintf(str, strlen(MPB_VERSION_RAID0), "%s", get_imsm_version(mpb)); | |
1003 | printf(" Version : %s\n", get_imsm_version(mpb)); | |
148acb7b | 1004 | printf(" Orig Family : %08x\n", __le32_to_cpu(mpb->orig_family_num)); |
cdddbdbc DW |
1005 | printf(" Family : %08x\n", __le32_to_cpu(mpb->family_num)); |
1006 | printf(" Generation : %08x\n", __le32_to_cpu(mpb->generation_num)); | |
a5d85af7 | 1007 | getinfo_super_imsm(st, &info, NULL); |
ae2bfd4e | 1008 | fname_from_uuid(st, &info, nbuf, ':'); |
27fd6274 | 1009 | printf(" UUID : %s\n", nbuf + 5); |
cdddbdbc DW |
1010 | sum = __le32_to_cpu(mpb->check_sum); |
1011 | printf(" Checksum : %08x %s\n", sum, | |
949c47a0 | 1012 | __gen_imsm_checksum(mpb) == sum ? "correct" : "incorrect"); |
87eb16df | 1013 | printf(" MPB Sectors : %d\n", mpb_sectors(mpb)); |
cdddbdbc DW |
1014 | printf(" Disks : %d\n", mpb->num_disks); |
1015 | printf(" RAID Devices : %d\n", mpb->num_raid_devs); | |
14e8215b | 1016 | print_imsm_disk(mpb, super->disks->index, reserved); |
604b746f JD |
1017 | if (super->bbm_log) { |
1018 | struct bbm_log *log = super->bbm_log; | |
1019 | ||
1020 | printf("\n"); | |
1021 | printf("Bad Block Management Log:\n"); | |
1022 | printf(" Log Size : %d\n", __le32_to_cpu(mpb->bbm_log_size)); | |
1023 | printf(" Signature : %x\n", __le32_to_cpu(log->signature)); | |
1024 | printf(" Entry Count : %d\n", __le32_to_cpu(log->entry_count)); | |
1025 | printf(" Spare Blocks : %d\n", __le32_to_cpu(log->reserved_spare_block_count)); | |
13a3b65d N |
1026 | printf(" First Spare : %llx\n", |
1027 | (unsigned long long) __le64_to_cpu(log->first_spare_lba)); | |
604b746f | 1028 | } |
44470971 DW |
1029 | for (i = 0; i < mpb->num_raid_devs; i++) { |
1030 | struct mdinfo info; | |
1031 | struct imsm_dev *dev = __get_imsm_dev(mpb, i); | |
1032 | ||
1033 | super->current_vol = i; | |
a5d85af7 | 1034 | getinfo_super_imsm(st, &info, NULL); |
ae2bfd4e | 1035 | fname_from_uuid(st, &info, nbuf, ':'); |
44470971 DW |
1036 | print_imsm_dev(dev, nbuf + 5, super->disks->index); |
1037 | } | |
cdddbdbc DW |
1038 | for (i = 0; i < mpb->num_disks; i++) { |
1039 | if (i == super->disks->index) | |
1040 | continue; | |
14e8215b | 1041 | print_imsm_disk(mpb, i, reserved); |
cdddbdbc | 1042 | } |
94827db3 N |
1043 | for (dl = super->disks ; dl; dl = dl->next) { |
1044 | struct imsm_disk *disk; | |
1045 | char str[MAX_RAID_SERIAL_LEN + 1]; | |
1046 | __u64 sz; | |
1047 | ||
1048 | if (dl->index >= 0) | |
1049 | continue; | |
1050 | ||
1051 | disk = &dl->disk; | |
1052 | printf("\n"); | |
1053 | snprintf(str, MAX_RAID_SERIAL_LEN + 1, "%s", disk->serial); | |
1054 | printf(" Disk Serial : %s\n", str); | |
1055 | printf(" State :%s%s%s\n", is_spare(disk) ? " spare" : "", | |
1056 | is_configured(disk) ? " active" : "", | |
1057 | is_failed(disk) ? " failed" : ""); | |
1058 | printf(" Id : %08x\n", __le32_to_cpu(disk->scsi_id)); | |
1059 | sz = __le32_to_cpu(disk->total_blocks) - reserved; | |
1060 | printf(" Usable Size : %llu%s\n", (unsigned long long)sz, | |
1061 | human_size(sz * 512)); | |
1062 | } | |
cdddbdbc DW |
1063 | } |
1064 | ||
061f2c6a | 1065 | static void brief_examine_super_imsm(struct supertype *st, int verbose) |
cdddbdbc | 1066 | { |
27fd6274 | 1067 | /* We just write a generic IMSM ARRAY entry */ |
ff54de6e N |
1068 | struct mdinfo info; |
1069 | char nbuf[64]; | |
1e7bc0ed | 1070 | struct intel_super *super = st->sb; |
1e7bc0ed | 1071 | |
0d5a423f DW |
1072 | if (!super->anchor->num_raid_devs) { |
1073 | printf("ARRAY metadata=imsm\n"); | |
1e7bc0ed | 1074 | return; |
0d5a423f | 1075 | } |
ff54de6e | 1076 | |
a5d85af7 | 1077 | getinfo_super_imsm(st, &info, NULL); |
4737ae25 N |
1078 | fname_from_uuid(st, &info, nbuf, ':'); |
1079 | printf("ARRAY metadata=imsm UUID=%s\n", nbuf + 5); | |
1080 | } | |
1081 | ||
1082 | static void brief_examine_subarrays_imsm(struct supertype *st, int verbose) | |
1083 | { | |
1084 | /* We just write a generic IMSM ARRAY entry */ | |
1085 | struct mdinfo info; | |
1086 | char nbuf[64]; | |
1087 | char nbuf1[64]; | |
1088 | struct intel_super *super = st->sb; | |
1089 | int i; | |
1090 | ||
1091 | if (!super->anchor->num_raid_devs) | |
1092 | return; | |
1093 | ||
a5d85af7 | 1094 | getinfo_super_imsm(st, &info, NULL); |
ae2bfd4e | 1095 | fname_from_uuid(st, &info, nbuf, ':'); |
1e7bc0ed DW |
1096 | for (i = 0; i < super->anchor->num_raid_devs; i++) { |
1097 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
1098 | ||
1099 | super->current_vol = i; | |
a5d85af7 | 1100 | getinfo_super_imsm(st, &info, NULL); |
ae2bfd4e | 1101 | fname_from_uuid(st, &info, nbuf1, ':'); |
1124b3cf | 1102 | printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n", |
cf8de691 | 1103 | dev->volume, nbuf + 5, i, nbuf1 + 5); |
1e7bc0ed | 1104 | } |
cdddbdbc DW |
1105 | } |
1106 | ||
9d84c8ea DW |
1107 | static void export_examine_super_imsm(struct supertype *st) |
1108 | { | |
1109 | struct intel_super *super = st->sb; | |
1110 | struct imsm_super *mpb = super->anchor; | |
1111 | struct mdinfo info; | |
1112 | char nbuf[64]; | |
1113 | ||
a5d85af7 | 1114 | getinfo_super_imsm(st, &info, NULL); |
9d84c8ea DW |
1115 | fname_from_uuid(st, &info, nbuf, ':'); |
1116 | printf("MD_METADATA=imsm\n"); | |
1117 | printf("MD_LEVEL=container\n"); | |
1118 | printf("MD_UUID=%s\n", nbuf+5); | |
1119 | printf("MD_DEVICES=%u\n", mpb->num_disks); | |
1120 | } | |
1121 | ||
cdddbdbc DW |
1122 | static void detail_super_imsm(struct supertype *st, char *homehost) |
1123 | { | |
3ebe00a1 DW |
1124 | struct mdinfo info; |
1125 | char nbuf[64]; | |
1126 | ||
a5d85af7 | 1127 | getinfo_super_imsm(st, &info, NULL); |
ae2bfd4e | 1128 | fname_from_uuid(st, &info, nbuf, ':'); |
3ebe00a1 | 1129 | printf("\n UUID : %s\n", nbuf + 5); |
cdddbdbc DW |
1130 | } |
1131 | ||
1132 | static void brief_detail_super_imsm(struct supertype *st) | |
1133 | { | |
ff54de6e N |
1134 | struct mdinfo info; |
1135 | char nbuf[64]; | |
a5d85af7 | 1136 | getinfo_super_imsm(st, &info, NULL); |
ae2bfd4e | 1137 | fname_from_uuid(st, &info, nbuf, ':'); |
ff54de6e | 1138 | printf(" UUID=%s", nbuf + 5); |
cdddbdbc | 1139 | } |
d665cc31 DW |
1140 | |
1141 | static int imsm_read_serial(int fd, char *devname, __u8 *serial); | |
1142 | static void fd2devname(int fd, char *name); | |
1143 | ||
120dc887 | 1144 | static int ahci_enumerate_ports(const char *hba_path, int port_count, int host_base, int verbose) |
d665cc31 | 1145 | { |
120dc887 LM |
1146 | /* dump an unsorted list of devices attached to AHCI Intel storage |
1147 | * controller, as well as non-connected ports | |
d665cc31 DW |
1148 | */ |
1149 | int hba_len = strlen(hba_path) + 1; | |
1150 | struct dirent *ent; | |
1151 | DIR *dir; | |
1152 | char *path = NULL; | |
1153 | int err = 0; | |
1154 | unsigned long port_mask = (1 << port_count) - 1; | |
1155 | ||
f21e18ca | 1156 | if (port_count > (int)sizeof(port_mask) * 8) { |
d665cc31 DW |
1157 | if (verbose) |
1158 | fprintf(stderr, Name ": port_count %d out of range\n", port_count); | |
1159 | return 2; | |
1160 | } | |
1161 | ||
1162 | /* scroll through /sys/dev/block looking for devices attached to | |
1163 | * this hba | |
1164 | */ | |
1165 | dir = opendir("/sys/dev/block"); | |
1166 | for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) { | |
1167 | int fd; | |
1168 | char model[64]; | |
1169 | char vendor[64]; | |
1170 | char buf[1024]; | |
1171 | int major, minor; | |
1172 | char *device; | |
1173 | char *c; | |
1174 | int port; | |
1175 | int type; | |
1176 | ||
1177 | if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2) | |
1178 | continue; | |
1179 | path = devt_to_devpath(makedev(major, minor)); | |
1180 | if (!path) | |
1181 | continue; | |
1182 | if (!path_attached_to_hba(path, hba_path)) { | |
1183 | free(path); | |
1184 | path = NULL; | |
1185 | continue; | |
1186 | } | |
1187 | ||
1188 | /* retrieve the scsi device type */ | |
1189 | if (asprintf(&device, "/sys/dev/block/%d:%d/device/xxxxxxx", major, minor) < 0) { | |
1190 | if (verbose) | |
1191 | fprintf(stderr, Name ": failed to allocate 'device'\n"); | |
1192 | err = 2; | |
1193 | break; | |
1194 | } | |
1195 | sprintf(device, "/sys/dev/block/%d:%d/device/type", major, minor); | |
1196 | if (load_sys(device, buf) != 0) { | |
1197 | if (verbose) | |
1198 | fprintf(stderr, Name ": failed to read device type for %s\n", | |
1199 | path); | |
1200 | err = 2; | |
1201 | free(device); | |
1202 | break; | |
1203 | } | |
1204 | type = strtoul(buf, NULL, 10); | |
1205 | ||
1206 | /* if it's not a disk print the vendor and model */ | |
1207 | if (!(type == 0 || type == 7 || type == 14)) { | |
1208 | vendor[0] = '\0'; | |
1209 | model[0] = '\0'; | |
1210 | sprintf(device, "/sys/dev/block/%d:%d/device/vendor", major, minor); | |
1211 | if (load_sys(device, buf) == 0) { | |
1212 | strncpy(vendor, buf, sizeof(vendor)); | |
1213 | vendor[sizeof(vendor) - 1] = '\0'; | |
1214 | c = (char *) &vendor[sizeof(vendor) - 1]; | |
1215 | while (isspace(*c) || *c == '\0') | |
1216 | *c-- = '\0'; | |
1217 | ||
1218 | } | |
1219 | sprintf(device, "/sys/dev/block/%d:%d/device/model", major, minor); | |
1220 | if (load_sys(device, buf) == 0) { | |
1221 | strncpy(model, buf, sizeof(model)); | |
1222 | model[sizeof(model) - 1] = '\0'; | |
1223 | c = (char *) &model[sizeof(model) - 1]; | |
1224 | while (isspace(*c) || *c == '\0') | |
1225 | *c-- = '\0'; | |
1226 | } | |
1227 | ||
1228 | if (vendor[0] && model[0]) | |
1229 | sprintf(buf, "%.64s %.64s", vendor, model); | |
1230 | else | |
1231 | switch (type) { /* numbers from hald/linux/device.c */ | |
1232 | case 1: sprintf(buf, "tape"); break; | |
1233 | case 2: sprintf(buf, "printer"); break; | |
1234 | case 3: sprintf(buf, "processor"); break; | |
1235 | case 4: | |
1236 | case 5: sprintf(buf, "cdrom"); break; | |
1237 | case 6: sprintf(buf, "scanner"); break; | |
1238 | case 8: sprintf(buf, "media_changer"); break; | |
1239 | case 9: sprintf(buf, "comm"); break; | |
1240 | case 12: sprintf(buf, "raid"); break; | |
1241 | default: sprintf(buf, "unknown"); | |
1242 | } | |
1243 | } else | |
1244 | buf[0] = '\0'; | |
1245 | free(device); | |
1246 | ||
1247 | /* chop device path to 'host%d' and calculate the port number */ | |
1248 | c = strchr(&path[hba_len], '/'); | |
4e5e717d AW |
1249 | if (!c) { |
1250 | if (verbose) | |
1251 | fprintf(stderr, Name ": %s - invalid path name\n", path + hba_len); | |
1252 | err = 2; | |
1253 | break; | |
1254 | } | |
d665cc31 DW |
1255 | *c = '\0'; |
1256 | if (sscanf(&path[hba_len], "host%d", &port) == 1) | |
1257 | port -= host_base; | |
1258 | else { | |
1259 | if (verbose) { | |
1260 | *c = '/'; /* repair the full string */ | |
1261 | fprintf(stderr, Name ": failed to determine port number for %s\n", | |
1262 | path); | |
1263 | } | |
1264 | err = 2; | |
1265 | break; | |
1266 | } | |
1267 | ||
1268 | /* mark this port as used */ | |
1269 | port_mask &= ~(1 << port); | |
1270 | ||
1271 | /* print out the device information */ | |
1272 | if (buf[0]) { | |
1273 | printf(" Port%d : - non-disk device (%s) -\n", port, buf); | |
1274 | continue; | |
1275 | } | |
1276 | ||
1277 | fd = dev_open(ent->d_name, O_RDONLY); | |
1278 | if (fd < 0) | |
1279 | printf(" Port%d : - disk info unavailable -\n", port); | |
1280 | else { | |
1281 | fd2devname(fd, buf); | |
1282 | printf(" Port%d : %s", port, buf); | |
1283 | if (imsm_read_serial(fd, NULL, (__u8 *) buf) == 0) | |
1284 | printf(" (%s)\n", buf); | |
1285 | else | |
1286 | printf("()\n"); | |
1287 | } | |
1288 | close(fd); | |
1289 | free(path); | |
1290 | path = NULL; | |
1291 | } | |
1292 | if (path) | |
1293 | free(path); | |
1294 | if (dir) | |
1295 | closedir(dir); | |
1296 | if (err == 0) { | |
1297 | int i; | |
1298 | ||
1299 | for (i = 0; i < port_count; i++) | |
1300 | if (port_mask & (1 << i)) | |
1301 | printf(" Port%d : - no device attached -\n", i); | |
1302 | } | |
1303 | ||
1304 | return err; | |
1305 | } | |
1306 | ||
120dc887 | 1307 | |
155cbb4c | 1308 | |
120dc887 LM |
1309 | static void print_found_intel_controllers(struct sys_dev *elem) |
1310 | { | |
1311 | for (; elem; elem = elem->next) { | |
1312 | fprintf(stderr, Name ": found Intel(R) "); | |
1313 | if (elem->type == SYS_DEV_SATA) | |
1314 | fprintf(stderr, "SATA "); | |
155cbb4c LM |
1315 | else if (elem->type == SYS_DEV_SAS) |
1316 | fprintf(stderr, "SAS "); | |
120dc887 LM |
1317 | fprintf(stderr, "RAID controller"); |
1318 | if (elem->pci_id) | |
1319 | fprintf(stderr, " at %s", elem->pci_id); | |
1320 | fprintf(stderr, ".\n"); | |
1321 | } | |
1322 | fflush(stderr); | |
1323 | } | |
1324 | ||
120dc887 LM |
1325 | static int ahci_get_port_count(const char *hba_path, int *port_count) |
1326 | { | |
1327 | struct dirent *ent; | |
1328 | DIR *dir; | |
1329 | int host_base = -1; | |
1330 | ||
1331 | *port_count = 0; | |
1332 | if ((dir = opendir(hba_path)) == NULL) | |
1333 | return -1; | |
1334 | ||
1335 | for (ent = readdir(dir); ent; ent = readdir(dir)) { | |
1336 | int host; | |
1337 | ||
1338 | if (sscanf(ent->d_name, "host%d", &host) != 1) | |
1339 | continue; | |
1340 | if (*port_count == 0) | |
1341 | host_base = host; | |
1342 | else if (host < host_base) | |
1343 | host_base = host; | |
1344 | ||
1345 | if (host + 1 > *port_count + host_base) | |
1346 | *port_count = host + 1 - host_base; | |
1347 | } | |
1348 | closedir(dir); | |
1349 | return host_base; | |
1350 | } | |
1351 | ||
a891a3c2 LM |
1352 | static void print_imsm_capability(const struct imsm_orom *orom) |
1353 | { | |
1354 | printf(" Platform : Intel(R) Matrix Storage Manager\n"); | |
1355 | printf(" Version : %d.%d.%d.%d\n", orom->major_ver, orom->minor_ver, | |
1356 | orom->hotfix_ver, orom->build); | |
1357 | printf(" RAID Levels :%s%s%s%s%s\n", | |
1358 | imsm_orom_has_raid0(orom) ? " raid0" : "", | |
1359 | imsm_orom_has_raid1(orom) ? " raid1" : "", | |
1360 | imsm_orom_has_raid1e(orom) ? " raid1e" : "", | |
1361 | imsm_orom_has_raid10(orom) ? " raid10" : "", | |
1362 | imsm_orom_has_raid5(orom) ? " raid5" : ""); | |
1363 | printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n", | |
1364 | imsm_orom_has_chunk(orom, 2) ? " 2k" : "", | |
1365 | imsm_orom_has_chunk(orom, 4) ? " 4k" : "", | |
1366 | imsm_orom_has_chunk(orom, 8) ? " 8k" : "", | |
1367 | imsm_orom_has_chunk(orom, 16) ? " 16k" : "", | |
1368 | imsm_orom_has_chunk(orom, 32) ? " 32k" : "", | |
1369 | imsm_orom_has_chunk(orom, 64) ? " 64k" : "", | |
1370 | imsm_orom_has_chunk(orom, 128) ? " 128k" : "", | |
1371 | imsm_orom_has_chunk(orom, 256) ? " 256k" : "", | |
1372 | imsm_orom_has_chunk(orom, 512) ? " 512k" : "", | |
1373 | imsm_orom_has_chunk(orom, 1024*1) ? " 1M" : "", | |
1374 | imsm_orom_has_chunk(orom, 1024*2) ? " 2M" : "", | |
1375 | imsm_orom_has_chunk(orom, 1024*4) ? " 4M" : "", | |
1376 | imsm_orom_has_chunk(orom, 1024*8) ? " 8M" : "", | |
1377 | imsm_orom_has_chunk(orom, 1024*16) ? " 16M" : "", | |
1378 | imsm_orom_has_chunk(orom, 1024*32) ? " 32M" : "", | |
1379 | imsm_orom_has_chunk(orom, 1024*64) ? " 64M" : ""); | |
1380 | printf(" Max Disks : %d\n", orom->tds); | |
1381 | printf(" Max Volumes : %d\n", orom->vpa); | |
1382 | return; | |
1383 | } | |
1384 | ||
5615172f | 1385 | static int detail_platform_imsm(int verbose, int enumerate_only) |
d665cc31 DW |
1386 | { |
1387 | /* There are two components to imsm platform support, the ahci SATA | |
1388 | * controller and the option-rom. To find the SATA controller we | |
1389 | * simply look in /sys/bus/pci/drivers/ahci to see if an ahci | |
1390 | * controller with the Intel vendor id is present. This approach | |
1391 | * allows mdadm to leverage the kernel's ahci detection logic, with the | |
1392 | * caveat that if ahci.ko is not loaded mdadm will not be able to | |
1393 | * detect platform raid capabilities. The option-rom resides in a | |
1394 | * platform "Adapter ROM". We scan for its signature to retrieve the | |
1395 | * platform capabilities. If raid support is disabled in the BIOS the | |
1396 | * option-rom capability structure will not be available. | |
1397 | */ | |
1398 | const struct imsm_orom *orom; | |
1399 | struct sys_dev *list, *hba; | |
d665cc31 DW |
1400 | int host_base = 0; |
1401 | int port_count = 0; | |
120dc887 | 1402 | int result=0; |
d665cc31 | 1403 | |
5615172f | 1404 | if (enumerate_only) { |
a891a3c2 | 1405 | if (check_env("IMSM_NO_PLATFORM")) |
5615172f | 1406 | return 0; |
a891a3c2 LM |
1407 | list = find_intel_devices(); |
1408 | if (!list) | |
1409 | return 2; | |
1410 | for (hba = list; hba; hba = hba->next) { | |
1411 | orom = find_imsm_capability(hba->type); | |
1412 | if (!orom) { | |
1413 | result = 2; | |
1414 | break; | |
1415 | } | |
1416 | } | |
1417 | free_sys_dev(&list); | |
1418 | return result; | |
5615172f DW |
1419 | } |
1420 | ||
155cbb4c LM |
1421 | list = find_intel_devices(); |
1422 | if (!list) { | |
d665cc31 | 1423 | if (verbose) |
155cbb4c LM |
1424 | fprintf(stderr, Name ": no active Intel(R) RAID " |
1425 | "controller found.\n"); | |
d665cc31 DW |
1426 | free_sys_dev(&list); |
1427 | return 2; | |
1428 | } else if (verbose) | |
155cbb4c | 1429 | print_found_intel_controllers(list); |
d665cc31 | 1430 | |
a891a3c2 LM |
1431 | for (hba = list; hba; hba = hba->next) { |
1432 | orom = find_imsm_capability(hba->type); | |
1433 | if (!orom) | |
1434 | fprintf(stderr, Name ": imsm capabilities not found for controller: %s (type %s)\n", | |
1435 | hba->path, get_sys_dev_type(hba->type)); | |
1436 | else | |
1437 | print_imsm_capability(orom); | |
d665cc31 DW |
1438 | } |
1439 | ||
120dc887 LM |
1440 | for (hba = list; hba; hba = hba->next) { |
1441 | printf(" I/O Controller : %s (%s)\n", | |
1442 | hba->path, get_sys_dev_type(hba->type)); | |
d665cc31 | 1443 | |
120dc887 LM |
1444 | if (hba->type == SYS_DEV_SATA) { |
1445 | host_base = ahci_get_port_count(hba->path, &port_count); | |
1446 | if (ahci_enumerate_ports(hba->path, port_count, host_base, verbose)) { | |
1447 | if (verbose) | |
1448 | fprintf(stderr, Name ": failed to enumerate " | |
1449 | "ports on SATA controller at %s.", hba->pci_id); | |
1450 | result |= 2; | |
1451 | } | |
1452 | } | |
d665cc31 | 1453 | } |
155cbb4c | 1454 | |
120dc887 LM |
1455 | free_sys_dev(&list); |
1456 | return result; | |
d665cc31 | 1457 | } |
cdddbdbc DW |
1458 | #endif |
1459 | ||
1460 | static int match_home_imsm(struct supertype *st, char *homehost) | |
1461 | { | |
5115ca67 DW |
1462 | /* the imsm metadata format does not specify any host |
1463 | * identification information. We return -1 since we can never | |
1464 | * confirm nor deny whether a given array is "meant" for this | |
148acb7b | 1465 | * host. We rely on compare_super and the 'family_num' fields to |
5115ca67 DW |
1466 | * exclude member disks that do not belong, and we rely on |
1467 | * mdadm.conf to specify the arrays that should be assembled. | |
1468 | * Auto-assembly may still pick up "foreign" arrays. | |
1469 | */ | |
cdddbdbc | 1470 | |
9362c1c8 | 1471 | return -1; |
cdddbdbc DW |
1472 | } |
1473 | ||
1474 | static void uuid_from_super_imsm(struct supertype *st, int uuid[4]) | |
1475 | { | |
51006d85 N |
1476 | /* The uuid returned here is used for: |
1477 | * uuid to put into bitmap file (Create, Grow) | |
1478 | * uuid for backup header when saving critical section (Grow) | |
1479 | * comparing uuids when re-adding a device into an array | |
1480 | * In these cases the uuid required is that of the data-array, | |
1481 | * not the device-set. | |
1482 | * uuid to recognise same set when adding a missing device back | |
1483 | * to an array. This is a uuid for the device-set. | |
1484 | * | |
1485 | * For each of these we can make do with a truncated | |
1486 | * or hashed uuid rather than the original, as long as | |
1487 | * everyone agrees. | |
1488 | * In each case the uuid required is that of the data-array, | |
1489 | * not the device-set. | |
43dad3d6 | 1490 | */ |
51006d85 N |
1491 | /* imsm does not track uuid's so we synthesis one using sha1 on |
1492 | * - The signature (Which is constant for all imsm array, but no matter) | |
148acb7b | 1493 | * - the orig_family_num of the container |
51006d85 N |
1494 | * - the index number of the volume |
1495 | * - the 'serial' number of the volume. | |
1496 | * Hopefully these are all constant. | |
1497 | */ | |
1498 | struct intel_super *super = st->sb; | |
43dad3d6 | 1499 | |
51006d85 N |
1500 | char buf[20]; |
1501 | struct sha1_ctx ctx; | |
1502 | struct imsm_dev *dev = NULL; | |
148acb7b | 1503 | __u32 family_num; |
51006d85 | 1504 | |
148acb7b DW |
1505 | /* some mdadm versions failed to set ->orig_family_num, in which |
1506 | * case fall back to ->family_num. orig_family_num will be | |
1507 | * fixed up with the first metadata update. | |
1508 | */ | |
1509 | family_num = super->anchor->orig_family_num; | |
1510 | if (family_num == 0) | |
1511 | family_num = super->anchor->family_num; | |
51006d85 | 1512 | sha1_init_ctx(&ctx); |
92bd8f8d | 1513 | sha1_process_bytes(super->anchor->sig, MPB_SIG_LEN, &ctx); |
148acb7b | 1514 | sha1_process_bytes(&family_num, sizeof(__u32), &ctx); |
51006d85 N |
1515 | if (super->current_vol >= 0) |
1516 | dev = get_imsm_dev(super, super->current_vol); | |
1517 | if (dev) { | |
1518 | __u32 vol = super->current_vol; | |
1519 | sha1_process_bytes(&vol, sizeof(vol), &ctx); | |
1520 | sha1_process_bytes(dev->volume, MAX_RAID_SERIAL_LEN, &ctx); | |
1521 | } | |
1522 | sha1_finish_ctx(&ctx, buf); | |
1523 | memcpy(uuid, buf, 4*4); | |
cdddbdbc DW |
1524 | } |
1525 | ||
0d481d37 | 1526 | #if 0 |
4f5bc454 DW |
1527 | static void |
1528 | get_imsm_numerical_version(struct imsm_super *mpb, int *m, int *p) | |
cdddbdbc | 1529 | { |
cdddbdbc DW |
1530 | __u8 *v = get_imsm_version(mpb); |
1531 | __u8 *end = mpb->sig + MAX_SIGNATURE_LENGTH; | |
1532 | char major[] = { 0, 0, 0 }; | |
1533 | char minor[] = { 0 ,0, 0 }; | |
1534 | char patch[] = { 0, 0, 0 }; | |
1535 | char *ver_parse[] = { major, minor, patch }; | |
1536 | int i, j; | |
1537 | ||
1538 | i = j = 0; | |
1539 | while (*v != '\0' && v < end) { | |
1540 | if (*v != '.' && j < 2) | |
1541 | ver_parse[i][j++] = *v; | |
1542 | else { | |
1543 | i++; | |
1544 | j = 0; | |
1545 | } | |
1546 | v++; | |
1547 | } | |
1548 | ||
4f5bc454 DW |
1549 | *m = strtol(minor, NULL, 0); |
1550 | *p = strtol(patch, NULL, 0); | |
1551 | } | |
0d481d37 | 1552 | #endif |
4f5bc454 | 1553 | |
1e5c6983 DW |
1554 | static __u32 migr_strip_blocks_resync(struct imsm_dev *dev) |
1555 | { | |
1556 | /* migr_strip_size when repairing or initializing parity */ | |
1557 | struct imsm_map *map = get_imsm_map(dev, 0); | |
1558 | __u32 chunk = __le32_to_cpu(map->blocks_per_strip); | |
1559 | ||
1560 | switch (get_imsm_raid_level(map)) { | |
1561 | case 5: | |
1562 | case 10: | |
1563 | return chunk; | |
1564 | default: | |
1565 | return 128*1024 >> 9; | |
1566 | } | |
1567 | } | |
1568 | ||
1569 | static __u32 migr_strip_blocks_rebuild(struct imsm_dev *dev) | |
1570 | { | |
1571 | /* migr_strip_size when rebuilding a degraded disk, no idea why | |
1572 | * this is different than migr_strip_size_resync(), but it's good | |
1573 | * to be compatible | |
1574 | */ | |
1575 | struct imsm_map *map = get_imsm_map(dev, 1); | |
1576 | __u32 chunk = __le32_to_cpu(map->blocks_per_strip); | |
1577 | ||
1578 | switch (get_imsm_raid_level(map)) { | |
1579 | case 1: | |
1580 | case 10: | |
1581 | if (map->num_members % map->num_domains == 0) | |
1582 | return 128*1024 >> 9; | |
1583 | else | |
1584 | return chunk; | |
1585 | case 5: | |
1586 | return max((__u32) 64*1024 >> 9, chunk); | |
1587 | default: | |
1588 | return 128*1024 >> 9; | |
1589 | } | |
1590 | } | |
1591 | ||
1592 | static __u32 num_stripes_per_unit_resync(struct imsm_dev *dev) | |
1593 | { | |
1594 | struct imsm_map *lo = get_imsm_map(dev, 0); | |
1595 | struct imsm_map *hi = get_imsm_map(dev, 1); | |
1596 | __u32 lo_chunk = __le32_to_cpu(lo->blocks_per_strip); | |
1597 | __u32 hi_chunk = __le32_to_cpu(hi->blocks_per_strip); | |
1598 | ||
1599 | return max((__u32) 1, hi_chunk / lo_chunk); | |
1600 | } | |
1601 | ||
1602 | static __u32 num_stripes_per_unit_rebuild(struct imsm_dev *dev) | |
1603 | { | |
1604 | struct imsm_map *lo = get_imsm_map(dev, 0); | |
1605 | int level = get_imsm_raid_level(lo); | |
1606 | ||
1607 | if (level == 1 || level == 10) { | |
1608 | struct imsm_map *hi = get_imsm_map(dev, 1); | |
1609 | ||
1610 | return hi->num_domains; | |
1611 | } else | |
1612 | return num_stripes_per_unit_resync(dev); | |
1613 | } | |
1614 | ||
98130f40 | 1615 | static __u8 imsm_num_data_members(struct imsm_dev *dev, int second_map) |
1e5c6983 DW |
1616 | { |
1617 | /* named 'imsm_' because raid0, raid1 and raid10 | |
1618 | * counter-intuitively have the same number of data disks | |
1619 | */ | |
98130f40 | 1620 | struct imsm_map *map = get_imsm_map(dev, second_map); |
1e5c6983 DW |
1621 | |
1622 | switch (get_imsm_raid_level(map)) { | |
1623 | case 0: | |
1624 | case 1: | |
1625 | case 10: | |
1626 | return map->num_members; | |
1627 | case 5: | |
1628 | return map->num_members - 1; | |
1629 | default: | |
1630 | dprintf("%s: unsupported raid level\n", __func__); | |
1631 | return 0; | |
1632 | } | |
1633 | } | |
1634 | ||
1635 | static __u32 parity_segment_depth(struct imsm_dev *dev) | |
1636 | { | |
1637 | struct imsm_map *map = get_imsm_map(dev, 0); | |
1638 | __u32 chunk = __le32_to_cpu(map->blocks_per_strip); | |
1639 | ||
1640 | switch(get_imsm_raid_level(map)) { | |
1641 | case 1: | |
1642 | case 10: | |
1643 | return chunk * map->num_domains; | |
1644 | case 5: | |
1645 | return chunk * map->num_members; | |
1646 | default: | |
1647 | return chunk; | |
1648 | } | |
1649 | } | |
1650 | ||
1651 | static __u32 map_migr_block(struct imsm_dev *dev, __u32 block) | |
1652 | { | |
1653 | struct imsm_map *map = get_imsm_map(dev, 1); | |
1654 | __u32 chunk = __le32_to_cpu(map->blocks_per_strip); | |
1655 | __u32 strip = block / chunk; | |
1656 | ||
1657 | switch (get_imsm_raid_level(map)) { | |
1658 | case 1: | |
1659 | case 10: { | |
1660 | __u32 vol_strip = (strip * map->num_domains) + 1; | |
1661 | __u32 vol_stripe = vol_strip / map->num_members; | |
1662 | ||
1663 | return vol_stripe * chunk + block % chunk; | |
1664 | } case 5: { | |
1665 | __u32 stripe = strip / (map->num_members - 1); | |
1666 | ||
1667 | return stripe * chunk + block % chunk; | |
1668 | } | |
1669 | default: | |
1670 | return 0; | |
1671 | } | |
1672 | } | |
1673 | ||
1674 | static __u64 blocks_per_migr_unit(struct imsm_dev *dev) | |
1675 | { | |
1676 | /* calculate the conversion factor between per member 'blocks' | |
1677 | * (md/{resync,rebuild}_start) and imsm migration units, return | |
1678 | * 0 for the 'not migrating' and 'unsupported migration' cases | |
1679 | */ | |
1680 | if (!dev->vol.migr_state) | |
1681 | return 0; | |
1682 | ||
1683 | switch (migr_type(dev)) { | |
6345120e | 1684 | case MIGR_GEN_MIGR: |
1e5c6983 DW |
1685 | case MIGR_VERIFY: |
1686 | case MIGR_REPAIR: | |
1687 | case MIGR_INIT: { | |
1688 | struct imsm_map *map = get_imsm_map(dev, 0); | |
1689 | __u32 stripes_per_unit; | |
1690 | __u32 blocks_per_unit; | |
1691 | __u32 parity_depth; | |
1692 | __u32 migr_chunk; | |
1693 | __u32 block_map; | |
1694 | __u32 block_rel; | |
1695 | __u32 segment; | |
1696 | __u32 stripe; | |
1697 | __u8 disks; | |
1698 | ||
1699 | /* yes, this is really the translation of migr_units to | |
1700 | * per-member blocks in the 'resync' case | |
1701 | */ | |
1702 | stripes_per_unit = num_stripes_per_unit_resync(dev); | |
1703 | migr_chunk = migr_strip_blocks_resync(dev); | |
98130f40 | 1704 | disks = imsm_num_data_members(dev, 0); |
1e5c6983 DW |
1705 | blocks_per_unit = stripes_per_unit * migr_chunk * disks; |
1706 | stripe = __le32_to_cpu(map->blocks_per_strip) * disks; | |
1707 | segment = blocks_per_unit / stripe; | |
1708 | block_rel = blocks_per_unit - segment * stripe; | |
1709 | parity_depth = parity_segment_depth(dev); | |
1710 | block_map = map_migr_block(dev, block_rel); | |
1711 | return block_map + parity_depth * segment; | |
1712 | } | |
1713 | case MIGR_REBUILD: { | |
1714 | __u32 stripes_per_unit; | |
1715 | __u32 migr_chunk; | |
1716 | ||
1717 | stripes_per_unit = num_stripes_per_unit_rebuild(dev); | |
1718 | migr_chunk = migr_strip_blocks_rebuild(dev); | |
1719 | return migr_chunk * stripes_per_unit; | |
1720 | } | |
1e5c6983 DW |
1721 | case MIGR_STATE_CHANGE: |
1722 | default: | |
1723 | return 0; | |
1724 | } | |
1725 | } | |
1726 | ||
c2c087e6 DW |
1727 | static int imsm_level_to_layout(int level) |
1728 | { | |
1729 | switch (level) { | |
1730 | case 0: | |
1731 | case 1: | |
1732 | return 0; | |
1733 | case 5: | |
1734 | case 6: | |
a380c027 | 1735 | return ALGORITHM_LEFT_ASYMMETRIC; |
c2c087e6 | 1736 | case 10: |
c92a2527 | 1737 | return 0x102; |
c2c087e6 | 1738 | } |
a18a888e | 1739 | return UnSet; |
c2c087e6 DW |
1740 | } |
1741 | ||
a5d85af7 | 1742 | static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info, char *dmap) |
bf5a934a DW |
1743 | { |
1744 | struct intel_super *super = st->sb; | |
949c47a0 | 1745 | struct imsm_dev *dev = get_imsm_dev(super, super->current_vol); |
a965f303 | 1746 | struct imsm_map *map = get_imsm_map(dev, 0); |
81ac8b4d | 1747 | struct imsm_map *prev_map = get_imsm_map(dev, 1); |
b335e593 | 1748 | struct imsm_map *map_to_analyse = map; |
efb30e7f | 1749 | struct dl *dl; |
e207da2f | 1750 | char *devname; |
a5d85af7 | 1751 | int map_disks = info->array.raid_disks; |
bf5a934a | 1752 | |
b335e593 AK |
1753 | if (prev_map) |
1754 | map_to_analyse = prev_map; | |
1755 | ||
efb30e7f DW |
1756 | for (dl = super->disks; dl; dl = dl->next) |
1757 | if (dl->raiddisk == info->disk.raid_disk) | |
1758 | break; | |
bf5a934a | 1759 | info->container_member = super->current_vol; |
b335e593 AK |
1760 | info->array.raid_disks = map_to_analyse->num_members; |
1761 | info->array.level = get_imsm_raid_level(map_to_analyse); | |
bf5a934a DW |
1762 | info->array.layout = imsm_level_to_layout(info->array.level); |
1763 | info->array.md_minor = -1; | |
1764 | info->array.ctime = 0; | |
1765 | info->array.utime = 0; | |
b335e593 AK |
1766 | info->array.chunk_size = |
1767 | __le16_to_cpu(map_to_analyse->blocks_per_strip) << 9; | |
301406c9 | 1768 | info->array.state = !dev->vol.dirty; |
da9b4a62 DW |
1769 | info->custom_array_size = __le32_to_cpu(dev->size_high); |
1770 | info->custom_array_size <<= 32; | |
1771 | info->custom_array_size |= __le32_to_cpu(dev->size_low); | |
3f83228a N |
1772 | if (prev_map && map->map_state == prev_map->map_state) { |
1773 | info->reshape_active = 1; | |
b335e593 AK |
1774 | info->new_level = get_imsm_raid_level(map); |
1775 | info->new_layout = imsm_level_to_layout(info->new_level); | |
1776 | info->new_chunk = __le16_to_cpu(map->blocks_per_strip) << 9; | |
3f83228a | 1777 | info->delta_disks = map->num_members - prev_map->num_members; |
493f5dd6 N |
1778 | if (info->delta_disks) { |
1779 | /* this needs to be applied to every array | |
1780 | * in the container. | |
1781 | */ | |
1782 | info->reshape_active = 2; | |
1783 | } | |
3f83228a N |
1784 | /* We shape information that we give to md might have to be |
1785 | * modify to cope with md's requirement for reshaping arrays. | |
1786 | * For example, when reshaping a RAID0, md requires it to be | |
1787 | * presented as a degraded RAID4. | |
1788 | * Also if a RAID0 is migrating to a RAID5 we need to specify | |
1789 | * the array as already being RAID5, but the 'before' layout | |
1790 | * is a RAID4-like layout. | |
1791 | */ | |
1792 | switch (info->array.level) { | |
1793 | case 0: | |
1794 | switch(info->new_level) { | |
1795 | case 0: | |
1796 | /* conversion is happening as RAID4 */ | |
1797 | info->array.level = 4; | |
1798 | info->array.raid_disks += 1; | |
1799 | break; | |
1800 | case 5: | |
1801 | /* conversion is happening as RAID5 */ | |
1802 | info->array.level = 5; | |
1803 | info->array.layout = ALGORITHM_PARITY_N; | |
1804 | info->array.raid_disks += 1; | |
1805 | info->delta_disks -= 1; | |
1806 | break; | |
1807 | default: | |
1808 | /* FIXME error message */ | |
1809 | info->array.level = UnSet; | |
1810 | break; | |
1811 | } | |
1812 | break; | |
1813 | } | |
b335e593 AK |
1814 | } else { |
1815 | info->new_level = UnSet; | |
1816 | info->new_layout = UnSet; | |
1817 | info->new_chunk = info->array.chunk_size; | |
3f83228a | 1818 | info->delta_disks = 0; |
b335e593 | 1819 | } |
301406c9 DW |
1820 | info->disk.major = 0; |
1821 | info->disk.minor = 0; | |
efb30e7f DW |
1822 | if (dl) { |
1823 | info->disk.major = dl->major; | |
1824 | info->disk.minor = dl->minor; | |
1825 | } | |
bf5a934a | 1826 | |
b335e593 AK |
1827 | info->data_offset = __le32_to_cpu(map_to_analyse->pba_of_lba0); |
1828 | info->component_size = | |
1829 | __le32_to_cpu(map_to_analyse->blocks_per_member); | |
301406c9 | 1830 | memset(info->uuid, 0, sizeof(info->uuid)); |
921d9e16 | 1831 | info->recovery_start = MaxSector; |
bf5a934a | 1832 | |
d2e6d5d6 | 1833 | info->reshape_progress = 0; |
b6796ce1 | 1834 | info->resync_start = MaxSector; |
b335e593 AK |
1835 | if (map_to_analyse->map_state == IMSM_T_STATE_UNINITIALIZED || |
1836 | dev->vol.dirty) { | |
301406c9 | 1837 | info->resync_start = 0; |
b6796ce1 AK |
1838 | } |
1839 | if (dev->vol.migr_state) { | |
1e5c6983 DW |
1840 | switch (migr_type(dev)) { |
1841 | case MIGR_REPAIR: | |
1842 | case MIGR_INIT: { | |
1843 | __u64 blocks_per_unit = blocks_per_migr_unit(dev); | |
1844 | __u64 units = __le32_to_cpu(dev->vol.curr_migr_unit); | |
1845 | ||
1846 | info->resync_start = blocks_per_unit * units; | |
1847 | break; | |
1848 | } | |
d2e6d5d6 AK |
1849 | case MIGR_GEN_MIGR: { |
1850 | __u64 blocks_per_unit = blocks_per_migr_unit(dev); | |
1851 | __u64 units = __le32_to_cpu(dev->vol.curr_migr_unit); | |
04fa9523 AK |
1852 | unsigned long long array_blocks; |
1853 | int used_disks; | |
d2e6d5d6 AK |
1854 | |
1855 | info->reshape_progress = blocks_per_unit * units; | |
6289d1e0 AK |
1856 | |
1857 | /* checkpoint is written per disks unit | |
1858 | * recalculate it to reshape position | |
1859 | */ | |
1860 | used_disks = imsm_num_data_members(dev, 0); | |
1861 | info->reshape_progress *= used_disks; | |
d2e6d5d6 AK |
1862 | dprintf("IMSM: General Migration checkpoint : %llu " |
1863 | "(%llu) -> read reshape progress : %llu\n", | |
1864 | units, blocks_per_unit, info->reshape_progress); | |
75156c46 AK |
1865 | |
1866 | used_disks = imsm_num_data_members(dev, 1); | |
1867 | if (used_disks > 0) { | |
1868 | array_blocks = map->blocks_per_member * | |
1869 | used_disks; | |
1870 | /* round array size down to closest MB | |
1871 | */ | |
1872 | info->custom_array_size = (array_blocks | |
1873 | >> SECT_PER_MB_SHIFT) | |
1874 | << SECT_PER_MB_SHIFT; | |
1875 | } | |
d2e6d5d6 | 1876 | } |
1e5c6983 DW |
1877 | case MIGR_VERIFY: |
1878 | /* we could emulate the checkpointing of | |
1879 | * 'sync_action=check' migrations, but for now | |
1880 | * we just immediately complete them | |
1881 | */ | |
1882 | case MIGR_REBUILD: | |
1883 | /* this is handled by container_content_imsm() */ | |
1e5c6983 DW |
1884 | case MIGR_STATE_CHANGE: |
1885 | /* FIXME handle other migrations */ | |
1886 | default: | |
1887 | /* we are not dirty, so... */ | |
1888 | info->resync_start = MaxSector; | |
1889 | } | |
b6796ce1 | 1890 | } |
301406c9 DW |
1891 | |
1892 | strncpy(info->name, (char *) dev->volume, MAX_RAID_SERIAL_LEN); | |
1893 | info->name[MAX_RAID_SERIAL_LEN] = 0; | |
bf5a934a | 1894 | |
f35f2525 N |
1895 | info->array.major_version = -1; |
1896 | info->array.minor_version = -2; | |
e207da2f AW |
1897 | devname = devnum2devname(st->container_dev); |
1898 | *info->text_version = '\0'; | |
1899 | if (devname) | |
1900 | sprintf(info->text_version, "/%s/%d", devname, info->container_member); | |
1901 | free(devname); | |
a67dd8cc | 1902 | info->safe_mode_delay = 4000; /* 4 secs like the Matrix driver */ |
51006d85 | 1903 | uuid_from_super_imsm(st, info->uuid); |
a5d85af7 N |
1904 | |
1905 | if (dmap) { | |
1906 | int i, j; | |
1907 | for (i=0; i<map_disks; i++) { | |
1908 | dmap[i] = 0; | |
1909 | if (i < info->array.raid_disks) { | |
1910 | struct imsm_disk *dsk; | |
98130f40 | 1911 | j = get_imsm_disk_idx(dev, i, -1); |
a5d85af7 N |
1912 | dsk = get_imsm_disk(super, j); |
1913 | if (dsk && (dsk->status & CONFIGURED_DISK)) | |
1914 | dmap[i] = 1; | |
1915 | } | |
1916 | } | |
1917 | } | |
81ac8b4d | 1918 | } |
bf5a934a | 1919 | |
97b4d0e9 DW |
1920 | static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev, int failed); |
1921 | static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev); | |
1922 | ||
1923 | static struct imsm_disk *get_imsm_missing(struct intel_super *super, __u8 index) | |
1924 | { | |
1925 | struct dl *d; | |
1926 | ||
1927 | for (d = super->missing; d; d = d->next) | |
1928 | if (d->index == index) | |
1929 | return &d->disk; | |
1930 | return NULL; | |
1931 | } | |
1932 | ||
a5d85af7 | 1933 | static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map) |
4f5bc454 DW |
1934 | { |
1935 | struct intel_super *super = st->sb; | |
4f5bc454 | 1936 | struct imsm_disk *disk; |
a5d85af7 | 1937 | int map_disks = info->array.raid_disks; |
ab3cb6b3 N |
1938 | int max_enough = -1; |
1939 | int i; | |
1940 | struct imsm_super *mpb; | |
4f5bc454 | 1941 | |
bf5a934a | 1942 | if (super->current_vol >= 0) { |
a5d85af7 | 1943 | getinfo_super_imsm_volume(st, info, map); |
bf5a934a DW |
1944 | return; |
1945 | } | |
d23fe947 DW |
1946 | |
1947 | /* Set raid_disks to zero so that Assemble will always pull in valid | |
1948 | * spares | |
1949 | */ | |
1950 | info->array.raid_disks = 0; | |
cdddbdbc DW |
1951 | info->array.level = LEVEL_CONTAINER; |
1952 | info->array.layout = 0; | |
1953 | info->array.md_minor = -1; | |
c2c087e6 | 1954 | info->array.ctime = 0; /* N/A for imsm */ |
cdddbdbc DW |
1955 | info->array.utime = 0; |
1956 | info->array.chunk_size = 0; | |
1957 | ||
1958 | info->disk.major = 0; | |
1959 | info->disk.minor = 0; | |
cdddbdbc | 1960 | info->disk.raid_disk = -1; |
c2c087e6 | 1961 | info->reshape_active = 0; |
f35f2525 N |
1962 | info->array.major_version = -1; |
1963 | info->array.minor_version = -2; | |
c2c087e6 | 1964 | strcpy(info->text_version, "imsm"); |
a67dd8cc | 1965 | info->safe_mode_delay = 0; |
c2c087e6 DW |
1966 | info->disk.number = -1; |
1967 | info->disk.state = 0; | |
c5afc314 | 1968 | info->name[0] = 0; |
921d9e16 | 1969 | info->recovery_start = MaxSector; |
c2c087e6 | 1970 | |
97b4d0e9 | 1971 | /* do we have the all the insync disks that we expect? */ |
ab3cb6b3 | 1972 | mpb = super->anchor; |
97b4d0e9 | 1973 | |
ab3cb6b3 N |
1974 | for (i = 0; i < mpb->num_raid_devs; i++) { |
1975 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
1976 | int failed, enough, j, missing = 0; | |
1977 | struct imsm_map *map; | |
1978 | __u8 state; | |
97b4d0e9 | 1979 | |
ab3cb6b3 N |
1980 | failed = imsm_count_failed(super, dev); |
1981 | state = imsm_check_degraded(super, dev, failed); | |
1982 | map = get_imsm_map(dev, dev->vol.migr_state); | |
1983 | ||
1984 | /* any newly missing disks? | |
1985 | * (catches single-degraded vs double-degraded) | |
1986 | */ | |
1987 | for (j = 0; j < map->num_members; j++) { | |
98130f40 | 1988 | __u32 ord = get_imsm_ord_tbl_ent(dev, i, -1); |
ab3cb6b3 N |
1989 | __u32 idx = ord_to_idx(ord); |
1990 | ||
1991 | if (!(ord & IMSM_ORD_REBUILD) && | |
1992 | get_imsm_missing(super, idx)) { | |
1993 | missing = 1; | |
1994 | break; | |
1995 | } | |
97b4d0e9 | 1996 | } |
ab3cb6b3 N |
1997 | |
1998 | if (state == IMSM_T_STATE_FAILED) | |
1999 | enough = -1; | |
2000 | else if (state == IMSM_T_STATE_DEGRADED && | |
2001 | (state != map->map_state || missing)) | |
2002 | enough = 0; | |
2003 | else /* we're normal, or already degraded */ | |
2004 | enough = 1; | |
2005 | ||
2006 | /* in the missing/failed disk case check to see | |
2007 | * if at least one array is runnable | |
2008 | */ | |
2009 | max_enough = max(max_enough, enough); | |
2010 | } | |
2011 | dprintf("%s: enough: %d\n", __func__, max_enough); | |
2012 | info->container_enough = max_enough; | |
97b4d0e9 | 2013 | |
4a04ec6c | 2014 | if (super->disks) { |
14e8215b DW |
2015 | __u32 reserved = imsm_reserved_sectors(super, super->disks); |
2016 | ||
b9f594fe | 2017 | disk = &super->disks->disk; |
14e8215b DW |
2018 | info->data_offset = __le32_to_cpu(disk->total_blocks) - reserved; |
2019 | info->component_size = reserved; | |
25ed7e59 | 2020 | info->disk.state = is_configured(disk) ? (1 << MD_DISK_ACTIVE) : 0; |
df474657 DW |
2021 | /* we don't change info->disk.raid_disk here because |
2022 | * this state will be finalized in mdmon after we have | |
2023 | * found the 'most fresh' version of the metadata | |
2024 | */ | |
25ed7e59 DW |
2025 | info->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0; |
2026 | info->disk.state |= is_spare(disk) ? 0 : (1 << MD_DISK_SYNC); | |
cdddbdbc | 2027 | } |
a575e2a7 DW |
2028 | |
2029 | /* only call uuid_from_super_imsm when this disk is part of a populated container, | |
2030 | * ->compare_super may have updated the 'num_raid_devs' field for spares | |
2031 | */ | |
2032 | if (info->disk.state & (1 << MD_DISK_SYNC) || super->anchor->num_raid_devs) | |
36ba7d48 | 2033 | uuid_from_super_imsm(st, info->uuid); |
22e263f6 AC |
2034 | else |
2035 | memcpy(info->uuid, uuid_zero, sizeof(uuid_zero)); | |
a5d85af7 N |
2036 | |
2037 | /* I don't know how to compute 'map' on imsm, so use safe default */ | |
2038 | if (map) { | |
2039 | int i; | |
2040 | for (i = 0; i < map_disks; i++) | |
2041 | map[i] = 1; | |
2042 | } | |
2043 | ||
cdddbdbc DW |
2044 | } |
2045 | ||
5c4cd5da AC |
2046 | /* allocates memory and fills disk in mdinfo structure |
2047 | * for each disk in array */ | |
2048 | struct mdinfo *getinfo_super_disks_imsm(struct supertype *st) | |
2049 | { | |
2050 | struct mdinfo *mddev = NULL; | |
2051 | struct intel_super *super = st->sb; | |
2052 | struct imsm_disk *disk; | |
2053 | int count = 0; | |
2054 | struct dl *dl; | |
2055 | if (!super || !super->disks) | |
2056 | return NULL; | |
2057 | dl = super->disks; | |
2058 | mddev = malloc(sizeof(*mddev)); | |
2059 | if (!mddev) { | |
2060 | fprintf(stderr, Name ": Failed to allocate memory.\n"); | |
2061 | return NULL; | |
2062 | } | |
2063 | memset(mddev, 0, sizeof(*mddev)); | |
2064 | while (dl) { | |
2065 | struct mdinfo *tmp; | |
2066 | disk = &dl->disk; | |
2067 | tmp = malloc(sizeof(*tmp)); | |
2068 | if (!tmp) { | |
2069 | fprintf(stderr, Name ": Failed to allocate memory.\n"); | |
2070 | if (mddev) | |
2071 | sysfs_free(mddev); | |
2072 | return NULL; | |
2073 | } | |
2074 | memset(tmp, 0, sizeof(*tmp)); | |
2075 | if (mddev->devs) | |
2076 | tmp->next = mddev->devs; | |
2077 | mddev->devs = tmp; | |
2078 | tmp->disk.number = count++; | |
2079 | tmp->disk.major = dl->major; | |
2080 | tmp->disk.minor = dl->minor; | |
2081 | tmp->disk.state = is_configured(disk) ? | |
2082 | (1 << MD_DISK_ACTIVE) : 0; | |
2083 | tmp->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0; | |
2084 | tmp->disk.state |= is_spare(disk) ? 0 : (1 << MD_DISK_SYNC); | |
2085 | tmp->disk.raid_disk = -1; | |
2086 | dl = dl->next; | |
2087 | } | |
2088 | return mddev; | |
2089 | } | |
2090 | ||
cdddbdbc DW |
2091 | static int update_super_imsm(struct supertype *st, struct mdinfo *info, |
2092 | char *update, char *devname, int verbose, | |
2093 | int uuid_set, char *homehost) | |
2094 | { | |
f352c545 DW |
2095 | /* For 'assemble' and 'force' we need to return non-zero if any |
2096 | * change was made. For others, the return value is ignored. | |
2097 | * Update options are: | |
2098 | * force-one : This device looks a bit old but needs to be included, | |
2099 | * update age info appropriately. | |
2100 | * assemble: clear any 'faulty' flag to allow this device to | |
2101 | * be assembled. | |
2102 | * force-array: Array is degraded but being forced, mark it clean | |
2103 | * if that will be needed to assemble it. | |
2104 | * | |
2105 | * newdev: not used ???? | |
2106 | * grow: Array has gained a new device - this is currently for | |
2107 | * linear only | |
2108 | * resync: mark as dirty so a resync will happen. | |
2109 | * name: update the name - preserving the homehost | |
6e46bf34 | 2110 | * uuid: Change the uuid of the array to match watch is given |
f352c545 DW |
2111 | * |
2112 | * Following are not relevant for this imsm: | |
2113 | * sparc2.2 : update from old dodgey metadata | |
2114 | * super-minor: change the preferred_minor number | |
2115 | * summaries: update redundant counters. | |
f352c545 DW |
2116 | * homehost: update the recorded homehost |
2117 | * _reshape_progress: record new reshape_progress position. | |
2118 | */ | |
6e46bf34 DW |
2119 | int rv = 1; |
2120 | struct intel_super *super = st->sb; | |
2121 | struct imsm_super *mpb; | |
f352c545 | 2122 | |
6e46bf34 DW |
2123 | /* we can only update container info */ |
2124 | if (!super || super->current_vol >= 0 || !super->anchor) | |
2125 | return 1; | |
2126 | ||
2127 | mpb = super->anchor; | |
2128 | ||
2129 | if (strcmp(update, "uuid") == 0 && uuid_set && !info->update_private) | |
1e2b2765 | 2130 | rv = -1; |
6e46bf34 DW |
2131 | else if (strcmp(update, "uuid") == 0 && uuid_set && info->update_private) { |
2132 | mpb->orig_family_num = *((__u32 *) info->update_private); | |
2133 | rv = 0; | |
2134 | } else if (strcmp(update, "uuid") == 0) { | |
2135 | __u32 *new_family = malloc(sizeof(*new_family)); | |
2136 | ||
2137 | /* update orig_family_number with the incoming random | |
2138 | * data, report the new effective uuid, and store the | |
2139 | * new orig_family_num for future updates. | |
2140 | */ | |
2141 | if (new_family) { | |
2142 | memcpy(&mpb->orig_family_num, info->uuid, sizeof(__u32)); | |
2143 | uuid_from_super_imsm(st, info->uuid); | |
2144 | *new_family = mpb->orig_family_num; | |
2145 | info->update_private = new_family; | |
2146 | rv = 0; | |
2147 | } | |
2148 | } else if (strcmp(update, "assemble") == 0) | |
2149 | rv = 0; | |
2150 | else | |
1e2b2765 | 2151 | rv = -1; |
f352c545 | 2152 | |
6e46bf34 DW |
2153 | /* successful update? recompute checksum */ |
2154 | if (rv == 0) | |
2155 | mpb->check_sum = __le32_to_cpu(__gen_imsm_checksum(mpb)); | |
f352c545 DW |
2156 | |
2157 | return rv; | |
cdddbdbc DW |
2158 | } |
2159 | ||
c2c087e6 | 2160 | static size_t disks_to_mpb_size(int disks) |
cdddbdbc | 2161 | { |
c2c087e6 | 2162 | size_t size; |
cdddbdbc | 2163 | |
c2c087e6 DW |
2164 | size = sizeof(struct imsm_super); |
2165 | size += (disks - 1) * sizeof(struct imsm_disk); | |
2166 | size += 2 * sizeof(struct imsm_dev); | |
2167 | /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */ | |
2168 | size += (4 - 2) * sizeof(struct imsm_map); | |
2169 | /* 4 possible disk_ord_tbl's */ | |
2170 | size += 4 * (disks - 1) * sizeof(__u32); | |
2171 | ||
2172 | return size; | |
2173 | } | |
2174 | ||
2175 | static __u64 avail_size_imsm(struct supertype *st, __u64 devsize) | |
2176 | { | |
2177 | if (devsize < (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS)) | |
2178 | return 0; | |
2179 | ||
2180 | return devsize - (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS); | |
cdddbdbc DW |
2181 | } |
2182 | ||
ba2de7ba DW |
2183 | static void free_devlist(struct intel_super *super) |
2184 | { | |
2185 | struct intel_dev *dv; | |
2186 | ||
2187 | while (super->devlist) { | |
2188 | dv = super->devlist->next; | |
2189 | free(super->devlist->dev); | |
2190 | free(super->devlist); | |
2191 | super->devlist = dv; | |
2192 | } | |
2193 | } | |
2194 | ||
2195 | static void imsm_copy_dev(struct imsm_dev *dest, struct imsm_dev *src) | |
2196 | { | |
2197 | memcpy(dest, src, sizeof_imsm_dev(src, 0)); | |
2198 | } | |
2199 | ||
cdddbdbc DW |
2200 | static int compare_super_imsm(struct supertype *st, struct supertype *tst) |
2201 | { | |
2202 | /* | |
2203 | * return: | |
2204 | * 0 same, or first was empty, and second was copied | |
2205 | * 1 second had wrong number | |
2206 | * 2 wrong uuid | |
2207 | * 3 wrong other info | |
2208 | */ | |
2209 | struct intel_super *first = st->sb; | |
2210 | struct intel_super *sec = tst->sb; | |
2211 | ||
2212 | if (!first) { | |
2213 | st->sb = tst->sb; | |
2214 | tst->sb = NULL; | |
2215 | return 0; | |
2216 | } | |
8603ea6f LM |
2217 | /* in platform dependent environment test if the disks |
2218 | * use the same Intel hba | |
2219 | */ | |
2220 | if (!check_env("IMSM_NO_PLATFORM")) { | |
ea2bc72b LM |
2221 | if (!first->hba || !sec->hba || |
2222 | (first->hba->type != sec->hba->type)) { | |
8603ea6f LM |
2223 | fprintf(stderr, |
2224 | "HBAs of devices does not match %s != %s\n", | |
ea2bc72b LM |
2225 | first->hba ? get_sys_dev_type(first->hba->type) : NULL, |
2226 | sec->hba ? get_sys_dev_type(sec->hba->type) : NULL); | |
8603ea6f LM |
2227 | return 3; |
2228 | } | |
2229 | } | |
cdddbdbc | 2230 | |
d23fe947 DW |
2231 | /* if an anchor does not have num_raid_devs set then it is a free |
2232 | * floating spare | |
2233 | */ | |
2234 | if (first->anchor->num_raid_devs > 0 && | |
2235 | sec->anchor->num_raid_devs > 0) { | |
a2b97981 DW |
2236 | /* Determine if these disks might ever have been |
2237 | * related. Further disambiguation can only take place | |
2238 | * in load_super_imsm_all | |
2239 | */ | |
2240 | __u32 first_family = first->anchor->orig_family_num; | |
2241 | __u32 sec_family = sec->anchor->orig_family_num; | |
2242 | ||
f796af5d DW |
2243 | if (memcmp(first->anchor->sig, sec->anchor->sig, |
2244 | MAX_SIGNATURE_LENGTH) != 0) | |
2245 | return 3; | |
2246 | ||
a2b97981 DW |
2247 | if (first_family == 0) |
2248 | first_family = first->anchor->family_num; | |
2249 | if (sec_family == 0) | |
2250 | sec_family = sec->anchor->family_num; | |
2251 | ||
2252 | if (first_family != sec_family) | |
d23fe947 | 2253 | return 3; |
f796af5d | 2254 | |
d23fe947 | 2255 | } |
cdddbdbc | 2256 | |
f796af5d | 2257 | |
3e372e5a DW |
2258 | /* if 'first' is a spare promote it to a populated mpb with sec's |
2259 | * family number | |
2260 | */ | |
2261 | if (first->anchor->num_raid_devs == 0 && | |
2262 | sec->anchor->num_raid_devs > 0) { | |
78d30f94 | 2263 | int i; |
ba2de7ba DW |
2264 | struct intel_dev *dv; |
2265 | struct imsm_dev *dev; | |
78d30f94 DW |
2266 | |
2267 | /* we need to copy raid device info from sec if an allocation | |
2268 | * fails here we don't associate the spare | |
2269 | */ | |
2270 | for (i = 0; i < sec->anchor->num_raid_devs; i++) { | |
ba2de7ba DW |
2271 | dv = malloc(sizeof(*dv)); |
2272 | if (!dv) | |
2273 | break; | |
2274 | dev = malloc(sizeof_imsm_dev(get_imsm_dev(sec, i), 1)); | |
2275 | if (!dev) { | |
2276 | free(dv); | |
2277 | break; | |
78d30f94 | 2278 | } |
ba2de7ba DW |
2279 | dv->dev = dev; |
2280 | dv->index = i; | |
2281 | dv->next = first->devlist; | |
2282 | first->devlist = dv; | |
78d30f94 | 2283 | } |
709743c5 | 2284 | if (i < sec->anchor->num_raid_devs) { |
ba2de7ba DW |
2285 | /* allocation failure */ |
2286 | free_devlist(first); | |
2287 | fprintf(stderr, "imsm: failed to associate spare\n"); | |
2288 | return 3; | |
78d30f94 | 2289 | } |
3e372e5a | 2290 | first->anchor->num_raid_devs = sec->anchor->num_raid_devs; |
148acb7b | 2291 | first->anchor->orig_family_num = sec->anchor->orig_family_num; |
3e372e5a | 2292 | first->anchor->family_num = sec->anchor->family_num; |
ac6449be | 2293 | memcpy(first->anchor->sig, sec->anchor->sig, MAX_SIGNATURE_LENGTH); |
709743c5 DW |
2294 | for (i = 0; i < sec->anchor->num_raid_devs; i++) |
2295 | imsm_copy_dev(get_imsm_dev(first, i), get_imsm_dev(sec, i)); | |
3e372e5a DW |
2296 | } |
2297 | ||
cdddbdbc DW |
2298 | return 0; |
2299 | } | |
2300 | ||
0030e8d6 DW |
2301 | static void fd2devname(int fd, char *name) |
2302 | { | |
2303 | struct stat st; | |
2304 | char path[256]; | |
33a6535d | 2305 | char dname[PATH_MAX]; |
0030e8d6 DW |
2306 | char *nm; |
2307 | int rv; | |
2308 | ||
2309 | name[0] = '\0'; | |
2310 | if (fstat(fd, &st) != 0) | |
2311 | return; | |
2312 | sprintf(path, "/sys/dev/block/%d:%d", | |
2313 | major(st.st_rdev), minor(st.st_rdev)); | |
2314 | ||
2315 | rv = readlink(path, dname, sizeof(dname)); | |
2316 | if (rv <= 0) | |
2317 | return; | |
2318 | ||
2319 | dname[rv] = '\0'; | |
2320 | nm = strrchr(dname, '/'); | |
2321 | nm++; | |
2322 | snprintf(name, MAX_RAID_SERIAL_LEN, "/dev/%s", nm); | |
2323 | } | |
2324 | ||
cdddbdbc DW |
2325 | extern int scsi_get_serial(int fd, void *buf, size_t buf_len); |
2326 | ||
2327 | static int imsm_read_serial(int fd, char *devname, | |
2328 | __u8 serial[MAX_RAID_SERIAL_LEN]) | |
2329 | { | |
2330 | unsigned char scsi_serial[255]; | |
cdddbdbc DW |
2331 | int rv; |
2332 | int rsp_len; | |
1f24f035 | 2333 | int len; |
316e2bf4 DW |
2334 | char *dest; |
2335 | char *src; | |
2336 | char *rsp_buf; | |
2337 | int i; | |
cdddbdbc DW |
2338 | |
2339 | memset(scsi_serial, 0, sizeof(scsi_serial)); | |
cdddbdbc | 2340 | |
f9ba0ff1 DW |
2341 | rv = scsi_get_serial(fd, scsi_serial, sizeof(scsi_serial)); |
2342 | ||
40ebbb9c | 2343 | if (rv && check_env("IMSM_DEVNAME_AS_SERIAL")) { |
f9ba0ff1 DW |
2344 | memset(serial, 0, MAX_RAID_SERIAL_LEN); |
2345 | fd2devname(fd, (char *) serial); | |
0030e8d6 DW |
2346 | return 0; |
2347 | } | |
2348 | ||
cdddbdbc DW |
2349 | if (rv != 0) { |
2350 | if (devname) | |
2351 | fprintf(stderr, | |
2352 | Name ": Failed to retrieve serial for %s\n", | |
2353 | devname); | |
2354 | return rv; | |
2355 | } | |
2356 | ||
2357 | rsp_len = scsi_serial[3]; | |
03cd4cc8 DW |
2358 | if (!rsp_len) { |
2359 | if (devname) | |
2360 | fprintf(stderr, | |
2361 | Name ": Failed to retrieve serial for %s\n", | |
2362 | devname); | |
2363 | return 2; | |
2364 | } | |
1f24f035 | 2365 | rsp_buf = (char *) &scsi_serial[4]; |
5c3db629 | 2366 | |
316e2bf4 DW |
2367 | /* trim all whitespace and non-printable characters and convert |
2368 | * ':' to ';' | |
2369 | */ | |
2370 | for (i = 0, dest = rsp_buf; i < rsp_len; i++) { | |
2371 | src = &rsp_buf[i]; | |
2372 | if (*src > 0x20) { | |
2373 | /* ':' is reserved for use in placeholder serial | |
2374 | * numbers for missing disks | |
2375 | */ | |
2376 | if (*src == ':') | |
2377 | *dest++ = ';'; | |
2378 | else | |
2379 | *dest++ = *src; | |
2380 | } | |
2381 | } | |
2382 | len = dest - rsp_buf; | |
2383 | dest = rsp_buf; | |
2384 | ||
2385 | /* truncate leading characters */ | |
2386 | if (len > MAX_RAID_SERIAL_LEN) { | |
2387 | dest += len - MAX_RAID_SERIAL_LEN; | |
1f24f035 | 2388 | len = MAX_RAID_SERIAL_LEN; |
316e2bf4 | 2389 | } |
5c3db629 | 2390 | |
5c3db629 | 2391 | memset(serial, 0, MAX_RAID_SERIAL_LEN); |
316e2bf4 | 2392 | memcpy(serial, dest, len); |
cdddbdbc DW |
2393 | |
2394 | return 0; | |
2395 | } | |
2396 | ||
1f24f035 DW |
2397 | static int serialcmp(__u8 *s1, __u8 *s2) |
2398 | { | |
2399 | return strncmp((char *) s1, (char *) s2, MAX_RAID_SERIAL_LEN); | |
2400 | } | |
2401 | ||
2402 | static void serialcpy(__u8 *dest, __u8 *src) | |
2403 | { | |
2404 | strncpy((char *) dest, (char *) src, MAX_RAID_SERIAL_LEN); | |
2405 | } | |
2406 | ||
1799c9e8 | 2407 | #ifndef MDASSEMBLE |
54c2c1ea DW |
2408 | static struct dl *serial_to_dl(__u8 *serial, struct intel_super *super) |
2409 | { | |
2410 | struct dl *dl; | |
2411 | ||
2412 | for (dl = super->disks; dl; dl = dl->next) | |
2413 | if (serialcmp(dl->serial, serial) == 0) | |
2414 | break; | |
2415 | ||
2416 | return dl; | |
2417 | } | |
1799c9e8 | 2418 | #endif |
54c2c1ea | 2419 | |
a2b97981 DW |
2420 | static struct imsm_disk * |
2421 | __serial_to_disk(__u8 *serial, struct imsm_super *mpb, int *idx) | |
2422 | { | |
2423 | int i; | |
2424 | ||
2425 | for (i = 0; i < mpb->num_disks; i++) { | |
2426 | struct imsm_disk *disk = __get_imsm_disk(mpb, i); | |
2427 | ||
2428 | if (serialcmp(disk->serial, serial) == 0) { | |
2429 | if (idx) | |
2430 | *idx = i; | |
2431 | return disk; | |
2432 | } | |
2433 | } | |
2434 | ||
2435 | return NULL; | |
2436 | } | |
2437 | ||
cdddbdbc DW |
2438 | static int |
2439 | load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd) | |
2440 | { | |
a2b97981 | 2441 | struct imsm_disk *disk; |
cdddbdbc DW |
2442 | struct dl *dl; |
2443 | struct stat stb; | |
cdddbdbc | 2444 | int rv; |
a2b97981 | 2445 | char name[40]; |
d23fe947 DW |
2446 | __u8 serial[MAX_RAID_SERIAL_LEN]; |
2447 | ||
2448 | rv = imsm_read_serial(fd, devname, serial); | |
2449 | ||
2450 | if (rv != 0) | |
2451 | return 2; | |
2452 | ||
a2b97981 | 2453 | dl = calloc(1, sizeof(*dl)); |
b9f594fe | 2454 | if (!dl) { |
cdddbdbc DW |
2455 | if (devname) |
2456 | fprintf(stderr, | |
2457 | Name ": failed to allocate disk buffer for %s\n", | |
2458 | devname); | |
2459 | return 2; | |
2460 | } | |
cdddbdbc | 2461 | |
a2b97981 DW |
2462 | fstat(fd, &stb); |
2463 | dl->major = major(stb.st_rdev); | |
2464 | dl->minor = minor(stb.st_rdev); | |
2465 | dl->next = super->disks; | |
2466 | dl->fd = keep_fd ? fd : -1; | |
2467 | assert(super->disks == NULL); | |
2468 | super->disks = dl; | |
2469 | serialcpy(dl->serial, serial); | |
2470 | dl->index = -2; | |
2471 | dl->e = NULL; | |
2472 | fd2devname(fd, name); | |
2473 | if (devname) | |
2474 | dl->devname = strdup(devname); | |
2475 | else | |
2476 | dl->devname = strdup(name); | |
cdddbdbc | 2477 | |
d23fe947 | 2478 | /* look up this disk's index in the current anchor */ |
a2b97981 DW |
2479 | disk = __serial_to_disk(dl->serial, super->anchor, &dl->index); |
2480 | if (disk) { | |
2481 | dl->disk = *disk; | |
2482 | /* only set index on disks that are a member of a | |
2483 | * populated contianer, i.e. one with raid_devs | |
2484 | */ | |
2485 | if (is_failed(&dl->disk)) | |
3f6efecc | 2486 | dl->index = -2; |
a2b97981 DW |
2487 | else if (is_spare(&dl->disk)) |
2488 | dl->index = -1; | |
3f6efecc DW |
2489 | } |
2490 | ||
949c47a0 DW |
2491 | return 0; |
2492 | } | |
2493 | ||
0e600426 | 2494 | #ifndef MDASSEMBLE |
0c046afd DW |
2495 | /* When migrating map0 contains the 'destination' state while map1 |
2496 | * contains the current state. When not migrating map0 contains the | |
2497 | * current state. This routine assumes that map[0].map_state is set to | |
2498 | * the current array state before being called. | |
2499 | * | |
2500 | * Migration is indicated by one of the following states | |
2501 | * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed) | |
e3bba0e0 | 2502 | * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal |
0c046afd | 2503 | * map1state=unitialized) |
1484e727 | 2504 | * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal |
0c046afd | 2505 | * map1state=normal) |
e3bba0e0 | 2506 | * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal |
0c046afd DW |
2507 | * map1state=degraded) |
2508 | */ | |
0556e1a2 | 2509 | static void migrate(struct imsm_dev *dev, __u8 to_state, int migr_type) |
3393c6af | 2510 | { |
0c046afd | 2511 | struct imsm_map *dest; |
3393c6af DW |
2512 | struct imsm_map *src = get_imsm_map(dev, 0); |
2513 | ||
0c046afd | 2514 | dev->vol.migr_state = 1; |
1484e727 | 2515 | set_migr_type(dev, migr_type); |
f8f603f1 | 2516 | dev->vol.curr_migr_unit = 0; |
0c046afd DW |
2517 | dest = get_imsm_map(dev, 1); |
2518 | ||
0556e1a2 | 2519 | /* duplicate and then set the target end state in map[0] */ |
3393c6af | 2520 | memcpy(dest, src, sizeof_imsm_map(src)); |
28bce06f AK |
2521 | if ((migr_type == MIGR_REBUILD) || |
2522 | (migr_type == MIGR_GEN_MIGR)) { | |
0556e1a2 DW |
2523 | __u32 ord; |
2524 | int i; | |
2525 | ||
2526 | for (i = 0; i < src->num_members; i++) { | |
2527 | ord = __le32_to_cpu(src->disk_ord_tbl[i]); | |
2528 | set_imsm_ord_tbl_ent(src, i, ord_to_idx(ord)); | |
2529 | } | |
2530 | } | |
2531 | ||
0c046afd | 2532 | src->map_state = to_state; |
949c47a0 | 2533 | } |
f8f603f1 DW |
2534 | |
2535 | static void end_migration(struct imsm_dev *dev, __u8 map_state) | |
2536 | { | |
2537 | struct imsm_map *map = get_imsm_map(dev, 0); | |
0556e1a2 | 2538 | struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state); |
28bce06f | 2539 | int i, j; |
0556e1a2 DW |
2540 | |
2541 | /* merge any IMSM_ORD_REBUILD bits that were not successfully | |
2542 | * completed in the last migration. | |
2543 | * | |
28bce06f | 2544 | * FIXME add support for raid-level-migration |
0556e1a2 DW |
2545 | */ |
2546 | for (i = 0; i < prev->num_members; i++) | |
28bce06f AK |
2547 | for (j = 0; j < map->num_members; j++) |
2548 | /* during online capacity expansion | |
2549 | * disks position can be changed if takeover is used | |
2550 | */ | |
2551 | if (ord_to_idx(map->disk_ord_tbl[j]) == | |
2552 | ord_to_idx(prev->disk_ord_tbl[i])) { | |
2553 | map->disk_ord_tbl[j] |= prev->disk_ord_tbl[i]; | |
2554 | break; | |
2555 | } | |
f8f603f1 DW |
2556 | |
2557 | dev->vol.migr_state = 0; | |
28bce06f | 2558 | dev->vol.migr_type = 0; |
f8f603f1 DW |
2559 | dev->vol.curr_migr_unit = 0; |
2560 | map->map_state = map_state; | |
2561 | } | |
0e600426 | 2562 | #endif |
949c47a0 DW |
2563 | |
2564 | static int parse_raid_devices(struct intel_super *super) | |
2565 | { | |
2566 | int i; | |
2567 | struct imsm_dev *dev_new; | |
4d7b1503 | 2568 | size_t len, len_migr; |
401d313b | 2569 | size_t max_len = 0; |
4d7b1503 DW |
2570 | size_t space_needed = 0; |
2571 | struct imsm_super *mpb = super->anchor; | |
949c47a0 DW |
2572 | |
2573 | for (i = 0; i < super->anchor->num_raid_devs; i++) { | |
2574 | struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i); | |
ba2de7ba | 2575 | struct intel_dev *dv; |
949c47a0 | 2576 | |
4d7b1503 DW |
2577 | len = sizeof_imsm_dev(dev_iter, 0); |
2578 | len_migr = sizeof_imsm_dev(dev_iter, 1); | |
2579 | if (len_migr > len) | |
2580 | space_needed += len_migr - len; | |
2581 | ||
ba2de7ba DW |
2582 | dv = malloc(sizeof(*dv)); |
2583 | if (!dv) | |
2584 | return 1; | |
401d313b AK |
2585 | if (max_len < len_migr) |
2586 | max_len = len_migr; | |
2587 | if (max_len > len_migr) | |
2588 | space_needed += max_len - len_migr; | |
2589 | dev_new = malloc(max_len); | |
ba2de7ba DW |
2590 | if (!dev_new) { |
2591 | free(dv); | |
949c47a0 | 2592 | return 1; |
ba2de7ba | 2593 | } |
949c47a0 | 2594 | imsm_copy_dev(dev_new, dev_iter); |
ba2de7ba DW |
2595 | dv->dev = dev_new; |
2596 | dv->index = i; | |
2597 | dv->next = super->devlist; | |
2598 | super->devlist = dv; | |
949c47a0 | 2599 | } |
cdddbdbc | 2600 | |
4d7b1503 DW |
2601 | /* ensure that super->buf is large enough when all raid devices |
2602 | * are migrating | |
2603 | */ | |
2604 | if (__le32_to_cpu(mpb->mpb_size) + space_needed > super->len) { | |
2605 | void *buf; | |
2606 | ||
2607 | len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + space_needed, 512); | |
2608 | if (posix_memalign(&buf, 512, len) != 0) | |
2609 | return 1; | |
2610 | ||
1f45a8ad DW |
2611 | memcpy(buf, super->buf, super->len); |
2612 | memset(buf + super->len, 0, len - super->len); | |
4d7b1503 DW |
2613 | free(super->buf); |
2614 | super->buf = buf; | |
2615 | super->len = len; | |
2616 | } | |
2617 | ||
cdddbdbc DW |
2618 | return 0; |
2619 | } | |
2620 | ||
604b746f JD |
2621 | /* retrieve a pointer to the bbm log which starts after all raid devices */ |
2622 | struct bbm_log *__get_imsm_bbm_log(struct imsm_super *mpb) | |
2623 | { | |
2624 | void *ptr = NULL; | |
2625 | ||
2626 | if (__le32_to_cpu(mpb->bbm_log_size)) { | |
2627 | ptr = mpb; | |
2628 | ptr += mpb->mpb_size - __le32_to_cpu(mpb->bbm_log_size); | |
2629 | } | |
2630 | ||
2631 | return ptr; | |
2632 | } | |
2633 | ||
d23fe947 | 2634 | static void __free_imsm(struct intel_super *super, int free_disks); |
9ca2c81c | 2635 | |
cdddbdbc | 2636 | /* load_imsm_mpb - read matrix metadata |
f2f5c343 | 2637 | * allocates super->mpb to be freed by free_imsm |
cdddbdbc DW |
2638 | */ |
2639 | static int load_imsm_mpb(int fd, struct intel_super *super, char *devname) | |
2640 | { | |
2641 | unsigned long long dsize; | |
cdddbdbc DW |
2642 | unsigned long long sectors; |
2643 | struct stat; | |
6416d527 | 2644 | struct imsm_super *anchor; |
cdddbdbc DW |
2645 | __u32 check_sum; |
2646 | ||
cdddbdbc | 2647 | get_dev_size(fd, NULL, &dsize); |
64436f06 N |
2648 | if (dsize < 1024) { |
2649 | if (devname) | |
2650 | fprintf(stderr, | |
2651 | Name ": %s: device to small for imsm\n", | |
2652 | devname); | |
2653 | return 1; | |
2654 | } | |
cdddbdbc DW |
2655 | |
2656 | if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0) { | |
2657 | if (devname) | |
2658 | fprintf(stderr, | |
2659 | Name ": Cannot seek to anchor block on %s: %s\n", | |
2660 | devname, strerror(errno)); | |
2661 | return 1; | |
2662 | } | |
2663 | ||
949c47a0 | 2664 | if (posix_memalign((void**)&anchor, 512, 512) != 0) { |
ad97895e DW |
2665 | if (devname) |
2666 | fprintf(stderr, | |
2667 | Name ": Failed to allocate imsm anchor buffer" | |
2668 | " on %s\n", devname); | |
2669 | return 1; | |
2670 | } | |
949c47a0 | 2671 | if (read(fd, anchor, 512) != 512) { |
cdddbdbc DW |
2672 | if (devname) |
2673 | fprintf(stderr, | |
2674 | Name ": Cannot read anchor block on %s: %s\n", | |
2675 | devname, strerror(errno)); | |
6416d527 | 2676 | free(anchor); |
cdddbdbc DW |
2677 | return 1; |
2678 | } | |
2679 | ||
6416d527 | 2680 | if (strncmp((char *) anchor->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) { |
cdddbdbc DW |
2681 | if (devname) |
2682 | fprintf(stderr, | |
2683 | Name ": no IMSM anchor on %s\n", devname); | |
6416d527 | 2684 | free(anchor); |
cdddbdbc DW |
2685 | return 2; |
2686 | } | |
2687 | ||
d23fe947 | 2688 | __free_imsm(super, 0); |
f2f5c343 LM |
2689 | /* reload capability and hba */ |
2690 | ||
2691 | /* capability and hba must be updated with new super allocation */ | |
d424212e | 2692 | find_intel_hba_capability(fd, super, devname); |
949c47a0 DW |
2693 | super->len = ROUND_UP(anchor->mpb_size, 512); |
2694 | if (posix_memalign(&super->buf, 512, super->len) != 0) { | |
cdddbdbc DW |
2695 | if (devname) |
2696 | fprintf(stderr, | |
2697 | Name ": unable to allocate %zu byte mpb buffer\n", | |
949c47a0 | 2698 | super->len); |
6416d527 | 2699 | free(anchor); |
cdddbdbc DW |
2700 | return 2; |
2701 | } | |
949c47a0 | 2702 | memcpy(super->buf, anchor, 512); |
cdddbdbc | 2703 | |
6416d527 NB |
2704 | sectors = mpb_sectors(anchor) - 1; |
2705 | free(anchor); | |
949c47a0 | 2706 | if (!sectors) { |
ecf45690 DW |
2707 | check_sum = __gen_imsm_checksum(super->anchor); |
2708 | if (check_sum != __le32_to_cpu(super->anchor->check_sum)) { | |
2709 | if (devname) | |
2710 | fprintf(stderr, | |
2711 | Name ": IMSM checksum %x != %x on %s\n", | |
2712 | check_sum, | |
2713 | __le32_to_cpu(super->anchor->check_sum), | |
2714 | devname); | |
2715 | return 2; | |
2716 | } | |
2717 | ||
a2b97981 | 2718 | return 0; |
949c47a0 | 2719 | } |
cdddbdbc DW |
2720 | |
2721 | /* read the extended mpb */ | |
2722 | if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0) { | |
2723 | if (devname) | |
2724 | fprintf(stderr, | |
2725 | Name ": Cannot seek to extended mpb on %s: %s\n", | |
2726 | devname, strerror(errno)); | |
2727 | return 1; | |
2728 | } | |
2729 | ||
f21e18ca | 2730 | if ((unsigned)read(fd, super->buf + 512, super->len - 512) != super->len - 512) { |
cdddbdbc DW |
2731 | if (devname) |
2732 | fprintf(stderr, | |
2733 | Name ": Cannot read extended mpb on %s: %s\n", | |
2734 | devname, strerror(errno)); | |
2735 | return 2; | |
2736 | } | |
2737 | ||
949c47a0 DW |
2738 | check_sum = __gen_imsm_checksum(super->anchor); |
2739 | if (check_sum != __le32_to_cpu(super->anchor->check_sum)) { | |
cdddbdbc DW |
2740 | if (devname) |
2741 | fprintf(stderr, | |
2742 | Name ": IMSM checksum %x != %x on %s\n", | |
949c47a0 | 2743 | check_sum, __le32_to_cpu(super->anchor->check_sum), |
cdddbdbc | 2744 | devname); |
db575f3b | 2745 | return 3; |
cdddbdbc DW |
2746 | } |
2747 | ||
604b746f JD |
2748 | /* FIXME the BBM log is disk specific so we cannot use this global |
2749 | * buffer for all disks. Ok for now since we only look at the global | |
2750 | * bbm_log_size parameter to gate assembly | |
2751 | */ | |
2752 | super->bbm_log = __get_imsm_bbm_log(super->anchor); | |
2753 | ||
a2b97981 DW |
2754 | return 0; |
2755 | } | |
2756 | ||
2757 | static int | |
2758 | load_and_parse_mpb(int fd, struct intel_super *super, char *devname, int keep_fd) | |
2759 | { | |
2760 | int err; | |
2761 | ||
2762 | err = load_imsm_mpb(fd, super, devname); | |
2763 | if (err) | |
2764 | return err; | |
2765 | err = load_imsm_disk(fd, super, devname, keep_fd); | |
2766 | if (err) | |
2767 | return err; | |
2768 | err = parse_raid_devices(super); | |
4d7b1503 | 2769 | |
a2b97981 | 2770 | return err; |
cdddbdbc DW |
2771 | } |
2772 | ||
ae6aad82 DW |
2773 | static void __free_imsm_disk(struct dl *d) |
2774 | { | |
2775 | if (d->fd >= 0) | |
2776 | close(d->fd); | |
2777 | if (d->devname) | |
2778 | free(d->devname); | |
0dcecb2e DW |
2779 | if (d->e) |
2780 | free(d->e); | |
ae6aad82 DW |
2781 | free(d); |
2782 | ||
2783 | } | |
1a64be56 | 2784 | |
cdddbdbc DW |
2785 | static void free_imsm_disks(struct intel_super *super) |
2786 | { | |
47ee5a45 | 2787 | struct dl *d; |
cdddbdbc | 2788 | |
47ee5a45 DW |
2789 | while (super->disks) { |
2790 | d = super->disks; | |
cdddbdbc | 2791 | super->disks = d->next; |
ae6aad82 | 2792 | __free_imsm_disk(d); |
cdddbdbc | 2793 | } |
cb82edca AK |
2794 | while (super->disk_mgmt_list) { |
2795 | d = super->disk_mgmt_list; | |
2796 | super->disk_mgmt_list = d->next; | |
2797 | __free_imsm_disk(d); | |
2798 | } | |
47ee5a45 DW |
2799 | while (super->missing) { |
2800 | d = super->missing; | |
2801 | super->missing = d->next; | |
2802 | __free_imsm_disk(d); | |
2803 | } | |
2804 | ||
cdddbdbc DW |
2805 | } |
2806 | ||
9ca2c81c | 2807 | /* free all the pieces hanging off of a super pointer */ |
d23fe947 | 2808 | static void __free_imsm(struct intel_super *super, int free_disks) |
cdddbdbc | 2809 | { |
88654014 LM |
2810 | struct intel_hba *elem, *next; |
2811 | ||
9ca2c81c | 2812 | if (super->buf) { |
949c47a0 | 2813 | free(super->buf); |
9ca2c81c DW |
2814 | super->buf = NULL; |
2815 | } | |
f2f5c343 LM |
2816 | /* unlink capability description */ |
2817 | super->orom = NULL; | |
d23fe947 DW |
2818 | if (free_disks) |
2819 | free_imsm_disks(super); | |
ba2de7ba | 2820 | free_devlist(super); |
88654014 LM |
2821 | elem = super->hba; |
2822 | while (elem) { | |
2823 | if (elem->path) | |
2824 | free((void *)elem->path); | |
2825 | next = elem->next; | |
2826 | free(elem); | |
2827 | elem = next; | |
88c32bb1 | 2828 | } |
88654014 | 2829 | super->hba = NULL; |
cdddbdbc DW |
2830 | } |
2831 | ||
9ca2c81c DW |
2832 | static void free_imsm(struct intel_super *super) |
2833 | { | |
d23fe947 | 2834 | __free_imsm(super, 1); |
9ca2c81c DW |
2835 | free(super); |
2836 | } | |
cdddbdbc DW |
2837 | |
2838 | static void free_super_imsm(struct supertype *st) | |
2839 | { | |
2840 | struct intel_super *super = st->sb; | |
2841 | ||
2842 | if (!super) | |
2843 | return; | |
2844 | ||
2845 | free_imsm(super); | |
2846 | st->sb = NULL; | |
2847 | } | |
2848 | ||
49133e57 | 2849 | static struct intel_super *alloc_super(void) |
c2c087e6 DW |
2850 | { |
2851 | struct intel_super *super = malloc(sizeof(*super)); | |
2852 | ||
2853 | if (super) { | |
2854 | memset(super, 0, sizeof(*super)); | |
bf5a934a | 2855 | super->current_vol = -1; |
0dcecb2e | 2856 | super->create_offset = ~((__u32 ) 0); |
c2c087e6 | 2857 | } |
c2c087e6 DW |
2858 | return super; |
2859 | } | |
2860 | ||
f0f5a016 LM |
2861 | /* |
2862 | * find and allocate hba and OROM/EFI based on valid fd of RAID component device | |
2863 | */ | |
d424212e | 2864 | static int find_intel_hba_capability(int fd, struct intel_super *super, char *devname) |
f0f5a016 LM |
2865 | { |
2866 | struct sys_dev *hba_name; | |
2867 | int rv = 0; | |
2868 | ||
2869 | if ((fd < 0) || check_env("IMSM_NO_PLATFORM")) { | |
f2f5c343 | 2870 | super->orom = NULL; |
f0f5a016 LM |
2871 | super->hba = NULL; |
2872 | return 0; | |
2873 | } | |
2874 | hba_name = find_disk_attached_hba(fd, NULL); | |
2875 | if (!hba_name) { | |
d424212e | 2876 | if (devname) |
f0f5a016 LM |
2877 | fprintf(stderr, |
2878 | Name ": %s is not attached to Intel(R) RAID controller.\n", | |
d424212e | 2879 | devname); |
f0f5a016 LM |
2880 | return 1; |
2881 | } | |
2882 | rv = attach_hba_to_super(super, hba_name); | |
2883 | if (rv == 2) { | |
d424212e N |
2884 | if (devname) { |
2885 | struct intel_hba *hba = super->hba; | |
f0f5a016 | 2886 | |
f0f5a016 LM |
2887 | fprintf(stderr, Name ": %s is attached to Intel(R) %s RAID " |
2888 | "controller (%s),\n" | |
2889 | " but the container is assigned to Intel(R) " | |
2890 | "%s RAID controller (", | |
d424212e | 2891 | devname, |
f0f5a016 LM |
2892 | hba_name->path, |
2893 | hba_name->pci_id ? : "Err!", | |
2894 | get_sys_dev_type(hba_name->type)); | |
2895 | ||
f0f5a016 LM |
2896 | while (hba) { |
2897 | fprintf(stderr, "%s", hba->pci_id ? : "Err!"); | |
2898 | if (hba->next) | |
2899 | fprintf(stderr, ", "); | |
2900 | hba = hba->next; | |
2901 | } | |
2902 | ||
2903 | fprintf(stderr, ").\n" | |
2904 | " Mixing devices attached to different controllers " | |
2905 | "is not allowed.\n"); | |
2906 | } | |
2907 | free_sys_dev(&hba_name); | |
2908 | return 2; | |
2909 | } | |
f2f5c343 | 2910 | super->orom = find_imsm_capability(hba_name->type); |
f0f5a016 | 2911 | free_sys_dev(&hba_name); |
f2f5c343 LM |
2912 | if (!super->orom) |
2913 | return 3; | |
f0f5a016 LM |
2914 | return 0; |
2915 | } | |
2916 | ||
cdddbdbc | 2917 | #ifndef MDASSEMBLE |
47ee5a45 DW |
2918 | /* find_missing - helper routine for load_super_imsm_all that identifies |
2919 | * disks that have disappeared from the system. This routine relies on | |
2920 | * the mpb being uptodate, which it is at load time. | |
2921 | */ | |
2922 | static int find_missing(struct intel_super *super) | |
2923 | { | |
2924 | int i; | |
2925 | struct imsm_super *mpb = super->anchor; | |
2926 | struct dl *dl; | |
2927 | struct imsm_disk *disk; | |
47ee5a45 DW |
2928 | |
2929 | for (i = 0; i < mpb->num_disks; i++) { | |
2930 | disk = __get_imsm_disk(mpb, i); | |
54c2c1ea | 2931 | dl = serial_to_dl(disk->serial, super); |
47ee5a45 DW |
2932 | if (dl) |
2933 | continue; | |
47ee5a45 DW |
2934 | |
2935 | dl = malloc(sizeof(*dl)); | |
2936 | if (!dl) | |
2937 | return 1; | |
2938 | dl->major = 0; | |
2939 | dl->minor = 0; | |
2940 | dl->fd = -1; | |
2941 | dl->devname = strdup("missing"); | |
2942 | dl->index = i; | |
2943 | serialcpy(dl->serial, disk->serial); | |
2944 | dl->disk = *disk; | |
689c9bf3 | 2945 | dl->e = NULL; |
47ee5a45 DW |
2946 | dl->next = super->missing; |
2947 | super->missing = dl; | |
2948 | } | |
2949 | ||
2950 | return 0; | |
2951 | } | |
2952 | ||
a2b97981 DW |
2953 | static struct intel_disk *disk_list_get(__u8 *serial, struct intel_disk *disk_list) |
2954 | { | |
2955 | struct intel_disk *idisk = disk_list; | |
2956 | ||
2957 | while (idisk) { | |
2958 | if (serialcmp(idisk->disk.serial, serial) == 0) | |
2959 | break; | |
2960 | idisk = idisk->next; | |
2961 | } | |
2962 | ||
2963 | return idisk; | |
2964 | } | |
2965 | ||
2966 | static int __prep_thunderdome(struct intel_super **table, int tbl_size, | |
2967 | struct intel_super *super, | |
2968 | struct intel_disk **disk_list) | |
2969 | { | |
2970 | struct imsm_disk *d = &super->disks->disk; | |
2971 | struct imsm_super *mpb = super->anchor; | |
2972 | int i, j; | |
2973 | ||
2974 | for (i = 0; i < tbl_size; i++) { | |
2975 | struct imsm_super *tbl_mpb = table[i]->anchor; | |
2976 | struct imsm_disk *tbl_d = &table[i]->disks->disk; | |
2977 | ||
2978 | if (tbl_mpb->family_num == mpb->family_num) { | |
2979 | if (tbl_mpb->check_sum == mpb->check_sum) { | |
2980 | dprintf("%s: mpb from %d:%d matches %d:%d\n", | |
2981 | __func__, super->disks->major, | |
2982 | super->disks->minor, | |
2983 | table[i]->disks->major, | |
2984 | table[i]->disks->minor); | |
2985 | break; | |
2986 | } | |
2987 | ||
2988 | if (((is_configured(d) && !is_configured(tbl_d)) || | |
2989 | is_configured(d) == is_configured(tbl_d)) && | |
2990 | tbl_mpb->generation_num < mpb->generation_num) { | |
2991 | /* current version of the mpb is a | |
2992 | * better candidate than the one in | |
2993 | * super_table, but copy over "cross | |
2994 | * generational" status | |
2995 | */ | |
2996 | struct intel_disk *idisk; | |
2997 | ||
2998 | dprintf("%s: mpb from %d:%d replaces %d:%d\n", | |
2999 | __func__, super->disks->major, | |
3000 | super->disks->minor, | |
3001 | table[i]->disks->major, | |
3002 | table[i]->disks->minor); | |
3003 | ||
3004 | idisk = disk_list_get(tbl_d->serial, *disk_list); | |
3005 | if (idisk && is_failed(&idisk->disk)) | |
3006 | tbl_d->status |= FAILED_DISK; | |
3007 | break; | |
3008 | } else { | |
3009 | struct intel_disk *idisk; | |
3010 | struct imsm_disk *disk; | |
3011 | ||
3012 | /* tbl_mpb is more up to date, but copy | |
3013 | * over cross generational status before | |
3014 | * returning | |
3015 | */ | |
3016 | disk = __serial_to_disk(d->serial, mpb, NULL); | |
3017 | if (disk && is_failed(disk)) | |
3018 | d->status |= FAILED_DISK; | |
3019 | ||
3020 | idisk = disk_list_get(d->serial, *disk_list); | |
3021 | if (idisk) { | |
3022 | idisk->owner = i; | |
3023 | if (disk && is_configured(disk)) | |
3024 | idisk->disk.status |= CONFIGURED_DISK; | |
3025 | } | |
3026 | ||
3027 | dprintf("%s: mpb from %d:%d prefer %d:%d\n", | |
3028 | __func__, super->disks->major, | |
3029 | super->disks->minor, | |
3030 | table[i]->disks->major, | |
3031 | table[i]->disks->minor); | |
3032 | ||
3033 | return tbl_size; | |
3034 | } | |
3035 | } | |
3036 | } | |
3037 | ||
3038 | if (i >= tbl_size) | |
3039 | table[tbl_size++] = super; | |
3040 | else | |
3041 | table[i] = super; | |
3042 | ||
3043 | /* update/extend the merged list of imsm_disk records */ | |
3044 | for (j = 0; j < mpb->num_disks; j++) { | |
3045 | struct imsm_disk *disk = __get_imsm_disk(mpb, j); | |
3046 | struct intel_disk *idisk; | |
3047 | ||
3048 | idisk = disk_list_get(disk->serial, *disk_list); | |
3049 | if (idisk) { | |
3050 | idisk->disk.status |= disk->status; | |
3051 | if (is_configured(&idisk->disk) || | |
3052 | is_failed(&idisk->disk)) | |
3053 | idisk->disk.status &= ~(SPARE_DISK); | |
3054 | } else { | |
3055 | idisk = calloc(1, sizeof(*idisk)); | |
3056 | if (!idisk) | |
3057 | return -1; | |
3058 | idisk->owner = IMSM_UNKNOWN_OWNER; | |
3059 | idisk->disk = *disk; | |
3060 | idisk->next = *disk_list; | |
3061 | *disk_list = idisk; | |
3062 | } | |
3063 | ||
3064 | if (serialcmp(idisk->disk.serial, d->serial) == 0) | |
3065 | idisk->owner = i; | |
3066 | } | |
3067 | ||
3068 | return tbl_size; | |
3069 | } | |
3070 | ||
3071 | static struct intel_super * | |
3072 | validate_members(struct intel_super *super, struct intel_disk *disk_list, | |
3073 | const int owner) | |
3074 | { | |
3075 | struct imsm_super *mpb = super->anchor; | |
3076 | int ok_count = 0; | |
3077 | int i; | |
3078 | ||
3079 | for (i = 0; i < mpb->num_disks; i++) { | |
3080 | struct imsm_disk *disk = __get_imsm_disk(mpb, i); | |
3081 | struct intel_disk *idisk; | |
3082 | ||
3083 | idisk = disk_list_get(disk->serial, disk_list); | |
3084 | if (idisk) { | |
3085 | if (idisk->owner == owner || | |
3086 | idisk->owner == IMSM_UNKNOWN_OWNER) | |
3087 | ok_count++; | |
3088 | else | |
3089 | dprintf("%s: '%.16s' owner %d != %d\n", | |
3090 | __func__, disk->serial, idisk->owner, | |
3091 | owner); | |
3092 | } else { | |
3093 | dprintf("%s: unknown disk %x [%d]: %.16s\n", | |
3094 | __func__, __le32_to_cpu(mpb->family_num), i, | |
3095 | disk->serial); | |
3096 | break; | |
3097 | } | |
3098 | } | |
3099 | ||
3100 | if (ok_count == mpb->num_disks) | |
3101 | return super; | |
3102 | return NULL; | |
3103 | } | |
3104 | ||
3105 | static void show_conflicts(__u32 family_num, struct intel_super *super_list) | |
3106 | { | |
3107 | struct intel_super *s; | |
3108 | ||
3109 | for (s = super_list; s; s = s->next) { | |
3110 | if (family_num != s->anchor->family_num) | |
3111 | continue; | |
3112 | fprintf(stderr, "Conflict, offlining family %#x on '%s'\n", | |
3113 | __le32_to_cpu(family_num), s->disks->devname); | |
3114 | } | |
3115 | } | |
3116 | ||
3117 | static struct intel_super * | |
3118 | imsm_thunderdome(struct intel_super **super_list, int len) | |
3119 | { | |
3120 | struct intel_super *super_table[len]; | |
3121 | struct intel_disk *disk_list = NULL; | |
3122 | struct intel_super *champion, *spare; | |
3123 | struct intel_super *s, **del; | |
3124 | int tbl_size = 0; | |
3125 | int conflict; | |
3126 | int i; | |
3127 | ||
3128 | memset(super_table, 0, sizeof(super_table)); | |
3129 | for (s = *super_list; s; s = s->next) | |
3130 | tbl_size = __prep_thunderdome(super_table, tbl_size, s, &disk_list); | |
3131 | ||
3132 | for (i = 0; i < tbl_size; i++) { | |
3133 | struct imsm_disk *d; | |
3134 | struct intel_disk *idisk; | |
3135 | struct imsm_super *mpb = super_table[i]->anchor; | |
3136 | ||
3137 | s = super_table[i]; | |
3138 | d = &s->disks->disk; | |
3139 | ||
3140 | /* 'd' must appear in merged disk list for its | |
3141 | * configuration to be valid | |
3142 | */ | |
3143 | idisk = disk_list_get(d->serial, disk_list); | |
3144 | if (idisk && idisk->owner == i) | |
3145 | s = validate_members(s, disk_list, i); | |
3146 | else | |
3147 | s = NULL; | |
3148 | ||
3149 | if (!s) | |
3150 | dprintf("%s: marking family: %#x from %d:%d offline\n", | |
3151 | __func__, mpb->family_num, | |
3152 | super_table[i]->disks->major, | |
3153 | super_table[i]->disks->minor); | |
3154 | super_table[i] = s; | |
3155 | } | |
3156 | ||
3157 | /* This is where the mdadm implementation differs from the Windows | |
3158 | * driver which has no strict concept of a container. We can only | |
3159 | * assemble one family from a container, so when returning a prodigal | |
3160 | * array member to this system the code will not be able to disambiguate | |
3161 | * the container contents that should be assembled ("foreign" versus | |
3162 | * "local"). It requires user intervention to set the orig_family_num | |
3163 | * to a new value to establish a new container. The Windows driver in | |
3164 | * this situation fixes up the volume name in place and manages the | |
3165 | * foreign array as an independent entity. | |
3166 | */ | |
3167 | s = NULL; | |
3168 | spare = NULL; | |
3169 | conflict = 0; | |
3170 | for (i = 0; i < tbl_size; i++) { | |
3171 | struct intel_super *tbl_ent = super_table[i]; | |
3172 | int is_spare = 0; | |
3173 | ||
3174 | if (!tbl_ent) | |
3175 | continue; | |
3176 | ||
3177 | if (tbl_ent->anchor->num_raid_devs == 0) { | |
3178 | spare = tbl_ent; | |
3179 | is_spare = 1; | |
3180 | } | |
3181 | ||
3182 | if (s && !is_spare) { | |
3183 | show_conflicts(tbl_ent->anchor->family_num, *super_list); | |
3184 | conflict++; | |
3185 | } else if (!s && !is_spare) | |
3186 | s = tbl_ent; | |
3187 | } | |
3188 | ||
3189 | if (!s) | |
3190 | s = spare; | |
3191 | if (!s) { | |
3192 | champion = NULL; | |
3193 | goto out; | |
3194 | } | |
3195 | champion = s; | |
3196 | ||
3197 | if (conflict) | |
3198 | fprintf(stderr, "Chose family %#x on '%s', " | |
3199 | "assemble conflicts to new container with '--update=uuid'\n", | |
3200 | __le32_to_cpu(s->anchor->family_num), s->disks->devname); | |
3201 | ||
3202 | /* collect all dl's onto 'champion', and update them to | |
3203 | * champion's version of the status | |
3204 | */ | |
3205 | for (s = *super_list; s; s = s->next) { | |
3206 | struct imsm_super *mpb = champion->anchor; | |
3207 | struct dl *dl = s->disks; | |
3208 | ||
3209 | if (s == champion) | |
3210 | continue; | |
3211 | ||
3212 | for (i = 0; i < mpb->num_disks; i++) { | |
3213 | struct imsm_disk *disk; | |
3214 | ||
3215 | disk = __serial_to_disk(dl->serial, mpb, &dl->index); | |
3216 | if (disk) { | |
3217 | dl->disk = *disk; | |
3218 | /* only set index on disks that are a member of | |
3219 | * a populated contianer, i.e. one with | |
3220 | * raid_devs | |
3221 | */ | |
3222 | if (is_failed(&dl->disk)) | |
3223 | dl->index = -2; | |
3224 | else if (is_spare(&dl->disk)) | |
3225 | dl->index = -1; | |
3226 | break; | |
3227 | } | |
3228 | } | |
3229 | ||
3230 | if (i >= mpb->num_disks) { | |
3231 | struct intel_disk *idisk; | |
3232 | ||
3233 | idisk = disk_list_get(dl->serial, disk_list); | |
ecf408e9 | 3234 | if (idisk && is_spare(&idisk->disk) && |
a2b97981 DW |
3235 | !is_failed(&idisk->disk) && !is_configured(&idisk->disk)) |
3236 | dl->index = -1; | |
3237 | else { | |
3238 | dl->index = -2; | |
3239 | continue; | |
3240 | } | |
3241 | } | |
3242 | ||
3243 | dl->next = champion->disks; | |
3244 | champion->disks = dl; | |
3245 | s->disks = NULL; | |
3246 | } | |
3247 | ||
3248 | /* delete 'champion' from super_list */ | |
3249 | for (del = super_list; *del; ) { | |
3250 | if (*del == champion) { | |
3251 | *del = (*del)->next; | |
3252 | break; | |
3253 | } else | |
3254 | del = &(*del)->next; | |
3255 | } | |
3256 | champion->next = NULL; | |
3257 | ||
3258 | out: | |
3259 | while (disk_list) { | |
3260 | struct intel_disk *idisk = disk_list; | |
3261 | ||
3262 | disk_list = disk_list->next; | |
3263 | free(idisk); | |
3264 | } | |
3265 | ||
3266 | return champion; | |
3267 | } | |
3268 | ||
cdddbdbc | 3269 | static int load_super_imsm_all(struct supertype *st, int fd, void **sbp, |
e1902a7b | 3270 | char *devname) |
cdddbdbc DW |
3271 | { |
3272 | struct mdinfo *sra; | |
a2b97981 DW |
3273 | struct intel_super *super_list = NULL; |
3274 | struct intel_super *super = NULL; | |
db575f3b | 3275 | int devnum = fd2devnum(fd); |
a2b97981 | 3276 | struct mdinfo *sd; |
db575f3b | 3277 | int retry; |
a2b97981 DW |
3278 | int err = 0; |
3279 | int i; | |
dab4a513 DW |
3280 | |
3281 | /* check if 'fd' an opened container */ | |
b526e52d | 3282 | sra = sysfs_read(fd, 0, GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE); |
cdddbdbc DW |
3283 | if (!sra) |
3284 | return 1; | |
3285 | ||
3286 | if (sra->array.major_version != -1 || | |
3287 | sra->array.minor_version != -2 || | |
1602d52c AW |
3288 | strcmp(sra->text_version, "imsm") != 0) { |
3289 | err = 1; | |
3290 | goto error; | |
3291 | } | |
a2b97981 DW |
3292 | /* load all mpbs */ |
3293 | for (sd = sra->devs, i = 0; sd; sd = sd->next, i++) { | |
49133e57 | 3294 | struct intel_super *s = alloc_super(); |
7a6ecd55 | 3295 | char nm[32]; |
a2b97981 | 3296 | int dfd; |
f2f5c343 | 3297 | int rv; |
a2b97981 DW |
3298 | |
3299 | err = 1; | |
3300 | if (!s) | |
3301 | goto error; | |
3302 | s->next = super_list; | |
3303 | super_list = s; | |
cdddbdbc | 3304 | |
a2b97981 | 3305 | err = 2; |
cdddbdbc | 3306 | sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor); |
e1902a7b | 3307 | dfd = dev_open(nm, O_RDWR); |
a2b97981 DW |
3308 | if (dfd < 0) |
3309 | goto error; | |
3310 | ||
d424212e | 3311 | rv = find_intel_hba_capability(dfd, s, devname); |
f2f5c343 LM |
3312 | /* no orom/efi or non-intel hba of the disk */ |
3313 | if (rv != 0) | |
3314 | goto error; | |
3315 | ||
e1902a7b | 3316 | err = load_and_parse_mpb(dfd, s, NULL, 1); |
db575f3b DW |
3317 | |
3318 | /* retry the load if we might have raced against mdmon */ | |
a2b97981 | 3319 | if (err == 3 && mdmon_running(devnum)) |
db575f3b DW |
3320 | for (retry = 0; retry < 3; retry++) { |
3321 | usleep(3000); | |
e1902a7b | 3322 | err = load_and_parse_mpb(dfd, s, NULL, 1); |
a2b97981 | 3323 | if (err != 3) |
db575f3b DW |
3324 | break; |
3325 | } | |
a2b97981 DW |
3326 | if (err) |
3327 | goto error; | |
cdddbdbc DW |
3328 | } |
3329 | ||
a2b97981 DW |
3330 | /* all mpbs enter, maybe one leaves */ |
3331 | super = imsm_thunderdome(&super_list, i); | |
3332 | if (!super) { | |
3333 | err = 1; | |
3334 | goto error; | |
cdddbdbc DW |
3335 | } |
3336 | ||
47ee5a45 DW |
3337 | if (find_missing(super) != 0) { |
3338 | free_imsm(super); | |
a2b97981 DW |
3339 | err = 2; |
3340 | goto error; | |
47ee5a45 | 3341 | } |
a2b97981 DW |
3342 | err = 0; |
3343 | ||
3344 | error: | |
3345 | while (super_list) { | |
3346 | struct intel_super *s = super_list; | |
3347 | ||
3348 | super_list = super_list->next; | |
3349 | free_imsm(s); | |
3350 | } | |
1602d52c | 3351 | sysfs_free(sra); |
a2b97981 DW |
3352 | |
3353 | if (err) | |
3354 | return err; | |
f7e7067b | 3355 | |
cdddbdbc | 3356 | *sbp = super; |
db575f3b | 3357 | st->container_dev = devnum; |
a2b97981 | 3358 | if (err == 0 && st->ss == NULL) { |
bf5a934a | 3359 | st->ss = &super_imsm; |
cdddbdbc DW |
3360 | st->minor_version = 0; |
3361 | st->max_devs = IMSM_MAX_DEVICES; | |
3362 | } | |
cdddbdbc DW |
3363 | return 0; |
3364 | } | |
2b959fbf N |
3365 | |
3366 | static int load_container_imsm(struct supertype *st, int fd, char *devname) | |
3367 | { | |
3368 | return load_super_imsm_all(st, fd, &st->sb, devname); | |
3369 | } | |
cdddbdbc DW |
3370 | #endif |
3371 | ||
3372 | static int load_super_imsm(struct supertype *st, int fd, char *devname) | |
3373 | { | |
3374 | struct intel_super *super; | |
3375 | int rv; | |
3376 | ||
691c6ee1 N |
3377 | if (test_partition(fd)) |
3378 | /* IMSM not allowed on partitions */ | |
3379 | return 1; | |
3380 | ||
37424f13 DW |
3381 | free_super_imsm(st); |
3382 | ||
49133e57 | 3383 | super = alloc_super(); |
cdddbdbc DW |
3384 | if (!super) { |
3385 | fprintf(stderr, | |
3386 | Name ": malloc of %zu failed.\n", | |
3387 | sizeof(*super)); | |
3388 | return 1; | |
3389 | } | |
ea2bc72b LM |
3390 | /* Load hba and capabilities if they exist. |
3391 | * But do not preclude loading metadata in case capabilities or hba are | |
3392 | * non-compliant and ignore_hw_compat is set. | |
3393 | */ | |
d424212e | 3394 | rv = find_intel_hba_capability(fd, super, devname); |
f2f5c343 | 3395 | /* no orom/efi or non-intel hba of the disk */ |
ea2bc72b | 3396 | if ((rv != 0) && (st->ignore_hw_compat == 0)) { |
f2f5c343 LM |
3397 | if (devname) |
3398 | fprintf(stderr, | |
3399 | Name ": No OROM/EFI properties for %s\n", devname); | |
3400 | free_imsm(super); | |
3401 | return 2; | |
3402 | } | |
a2b97981 | 3403 | rv = load_and_parse_mpb(fd, super, devname, 0); |
cdddbdbc DW |
3404 | |
3405 | if (rv) { | |
3406 | if (devname) | |
3407 | fprintf(stderr, | |
3408 | Name ": Failed to load all information " | |
3409 | "sections on %s\n", devname); | |
3410 | free_imsm(super); | |
3411 | return rv; | |
3412 | } | |
3413 | ||
3414 | st->sb = super; | |
3415 | if (st->ss == NULL) { | |
3416 | st->ss = &super_imsm; | |
3417 | st->minor_version = 0; | |
3418 | st->max_devs = IMSM_MAX_DEVICES; | |
3419 | } | |
cdddbdbc DW |
3420 | return 0; |
3421 | } | |
3422 | ||
ef6ffade DW |
3423 | static __u16 info_to_blocks_per_strip(mdu_array_info_t *info) |
3424 | { | |
3425 | if (info->level == 1) | |
3426 | return 128; | |
3427 | return info->chunk_size >> 9; | |
3428 | } | |
3429 | ||
ff596308 | 3430 | static __u32 info_to_num_data_stripes(mdu_array_info_t *info, int num_domains) |
ef6ffade DW |
3431 | { |
3432 | __u32 num_stripes; | |
3433 | ||
3434 | num_stripes = (info->size * 2) / info_to_blocks_per_strip(info); | |
ff596308 | 3435 | num_stripes /= num_domains; |
ef6ffade DW |
3436 | |
3437 | return num_stripes; | |
3438 | } | |
3439 | ||
fcfd9599 DW |
3440 | static __u32 info_to_blocks_per_member(mdu_array_info_t *info) |
3441 | { | |
4025c288 DW |
3442 | if (info->level == 1) |
3443 | return info->size * 2; | |
3444 | else | |
3445 | return (info->size * 2) & ~(info_to_blocks_per_strip(info) - 1); | |
fcfd9599 DW |
3446 | } |
3447 | ||
4d1313e9 DW |
3448 | static void imsm_update_version_info(struct intel_super *super) |
3449 | { | |
3450 | /* update the version and attributes */ | |
3451 | struct imsm_super *mpb = super->anchor; | |
3452 | char *version; | |
3453 | struct imsm_dev *dev; | |
3454 | struct imsm_map *map; | |
3455 | int i; | |
3456 | ||
3457 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
3458 | dev = get_imsm_dev(super, i); | |
3459 | map = get_imsm_map(dev, 0); | |
3460 | if (__le32_to_cpu(dev->size_high) > 0) | |
3461 | mpb->attributes |= MPB_ATTRIB_2TB; | |
3462 | ||
3463 | /* FIXME detect when an array spans a port multiplier */ | |
3464 | #if 0 | |
3465 | mpb->attributes |= MPB_ATTRIB_PM; | |
3466 | #endif | |
3467 | ||
3468 | if (mpb->num_raid_devs > 1 || | |
3469 | mpb->attributes != MPB_ATTRIB_CHECKSUM_VERIFY) { | |
3470 | version = MPB_VERSION_ATTRIBS; | |
3471 | switch (get_imsm_raid_level(map)) { | |
3472 | case 0: mpb->attributes |= MPB_ATTRIB_RAID0; break; | |
3473 | case 1: mpb->attributes |= MPB_ATTRIB_RAID1; break; | |
3474 | case 10: mpb->attributes |= MPB_ATTRIB_RAID10; break; | |
3475 | case 5: mpb->attributes |= MPB_ATTRIB_RAID5; break; | |
3476 | } | |
3477 | } else { | |
3478 | if (map->num_members >= 5) | |
3479 | version = MPB_VERSION_5OR6_DISK_ARRAY; | |
3480 | else if (dev->status == DEV_CLONE_N_GO) | |
3481 | version = MPB_VERSION_CNG; | |
3482 | else if (get_imsm_raid_level(map) == 5) | |
3483 | version = MPB_VERSION_RAID5; | |
3484 | else if (map->num_members >= 3) | |
3485 | version = MPB_VERSION_3OR4_DISK_ARRAY; | |
3486 | else if (get_imsm_raid_level(map) == 1) | |
3487 | version = MPB_VERSION_RAID1; | |
3488 | else | |
3489 | version = MPB_VERSION_RAID0; | |
3490 | } | |
3491 | strcpy(((char *) mpb->sig) + strlen(MPB_SIGNATURE), version); | |
3492 | } | |
3493 | } | |
3494 | ||
aa534678 DW |
3495 | static int check_name(struct intel_super *super, char *name, int quiet) |
3496 | { | |
3497 | struct imsm_super *mpb = super->anchor; | |
3498 | char *reason = NULL; | |
3499 | int i; | |
3500 | ||
3501 | if (strlen(name) > MAX_RAID_SERIAL_LEN) | |
3502 | reason = "must be 16 characters or less"; | |
3503 | ||
3504 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
3505 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
3506 | ||
3507 | if (strncmp((char *) dev->volume, name, MAX_RAID_SERIAL_LEN) == 0) { | |
3508 | reason = "already exists"; | |
3509 | break; | |
3510 | } | |
3511 | } | |
3512 | ||
3513 | if (reason && !quiet) | |
3514 | fprintf(stderr, Name ": imsm volume name %s\n", reason); | |
3515 | ||
3516 | return !reason; | |
3517 | } | |
3518 | ||
8b353278 DW |
3519 | static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info, |
3520 | unsigned long long size, char *name, | |
3521 | char *homehost, int *uuid) | |
cdddbdbc | 3522 | { |
c2c087e6 DW |
3523 | /* We are creating a volume inside a pre-existing container. |
3524 | * so st->sb is already set. | |
3525 | */ | |
3526 | struct intel_super *super = st->sb; | |
949c47a0 | 3527 | struct imsm_super *mpb = super->anchor; |
ba2de7ba | 3528 | struct intel_dev *dv; |
c2c087e6 DW |
3529 | struct imsm_dev *dev; |
3530 | struct imsm_vol *vol; | |
3531 | struct imsm_map *map; | |
3532 | int idx = mpb->num_raid_devs; | |
3533 | int i; | |
3534 | unsigned long long array_blocks; | |
2c092cad | 3535 | size_t size_old, size_new; |
ff596308 | 3536 | __u32 num_data_stripes; |
cdddbdbc | 3537 | |
88c32bb1 | 3538 | if (super->orom && mpb->num_raid_devs >= super->orom->vpa) { |
c2c087e6 | 3539 | fprintf(stderr, Name": This imsm-container already has the " |
88c32bb1 | 3540 | "maximum of %d volumes\n", super->orom->vpa); |
c2c087e6 DW |
3541 | return 0; |
3542 | } | |
3543 | ||
2c092cad DW |
3544 | /* ensure the mpb is large enough for the new data */ |
3545 | size_old = __le32_to_cpu(mpb->mpb_size); | |
3546 | size_new = disks_to_mpb_size(info->nr_disks); | |
3547 | if (size_new > size_old) { | |
3548 | void *mpb_new; | |
3549 | size_t size_round = ROUND_UP(size_new, 512); | |
3550 | ||
3551 | if (posix_memalign(&mpb_new, 512, size_round) != 0) { | |
3552 | fprintf(stderr, Name": could not allocate new mpb\n"); | |
3553 | return 0; | |
3554 | } | |
3555 | memcpy(mpb_new, mpb, size_old); | |
3556 | free(mpb); | |
3557 | mpb = mpb_new; | |
949c47a0 | 3558 | super->anchor = mpb_new; |
2c092cad DW |
3559 | mpb->mpb_size = __cpu_to_le32(size_new); |
3560 | memset(mpb_new + size_old, 0, size_round - size_old); | |
3561 | } | |
bf5a934a | 3562 | super->current_vol = idx; |
d23fe947 DW |
3563 | /* when creating the first raid device in this container set num_disks |
3564 | * to zero, i.e. delete this spare and add raid member devices in | |
3565 | * add_to_super_imsm_volume() | |
3566 | */ | |
3567 | if (super->current_vol == 0) | |
3568 | mpb->num_disks = 0; | |
5a038140 | 3569 | |
aa534678 DW |
3570 | if (!check_name(super, name, 0)) |
3571 | return 0; | |
ba2de7ba DW |
3572 | dv = malloc(sizeof(*dv)); |
3573 | if (!dv) { | |
3574 | fprintf(stderr, Name ": failed to allocate device list entry\n"); | |
3575 | return 0; | |
3576 | } | |
1a2487c2 | 3577 | dev = calloc(1, sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1)); |
949c47a0 | 3578 | if (!dev) { |
ba2de7ba | 3579 | free(dv); |
949c47a0 DW |
3580 | fprintf(stderr, Name": could not allocate raid device\n"); |
3581 | return 0; | |
3582 | } | |
1a2487c2 | 3583 | |
c2c087e6 | 3584 | strncpy((char *) dev->volume, name, MAX_RAID_SERIAL_LEN); |
03bcbc65 DW |
3585 | if (info->level == 1) |
3586 | array_blocks = info_to_blocks_per_member(info); | |
3587 | else | |
3588 | array_blocks = calc_array_size(info->level, info->raid_disks, | |
3589 | info->layout, info->chunk_size, | |
3590 | info->size*2); | |
979d38be DW |
3591 | /* round array size down to closest MB */ |
3592 | array_blocks = (array_blocks >> SECT_PER_MB_SHIFT) << SECT_PER_MB_SHIFT; | |
3593 | ||
c2c087e6 DW |
3594 | dev->size_low = __cpu_to_le32((__u32) array_blocks); |
3595 | dev->size_high = __cpu_to_le32((__u32) (array_blocks >> 32)); | |
1a2487c2 | 3596 | dev->status = (DEV_READ_COALESCING | DEV_WRITE_COALESCING); |
c2c087e6 DW |
3597 | vol = &dev->vol; |
3598 | vol->migr_state = 0; | |
1484e727 | 3599 | set_migr_type(dev, MIGR_INIT); |
c2c087e6 | 3600 | vol->dirty = 0; |
f8f603f1 | 3601 | vol->curr_migr_unit = 0; |
a965f303 | 3602 | map = get_imsm_map(dev, 0); |
0dcecb2e | 3603 | map->pba_of_lba0 = __cpu_to_le32(super->create_offset); |
fcfd9599 | 3604 | map->blocks_per_member = __cpu_to_le32(info_to_blocks_per_member(info)); |
ef6ffade | 3605 | map->blocks_per_strip = __cpu_to_le16(info_to_blocks_per_strip(info)); |
0556e1a2 | 3606 | map->failed_disk_num = ~0; |
c2c087e6 DW |
3607 | map->map_state = info->level ? IMSM_T_STATE_UNINITIALIZED : |
3608 | IMSM_T_STATE_NORMAL; | |
252d23c0 | 3609 | map->ddf = 1; |
ef6ffade DW |
3610 | |
3611 | if (info->level == 1 && info->raid_disks > 2) { | |
38950822 AW |
3612 | free(dev); |
3613 | free(dv); | |
ef6ffade DW |
3614 | fprintf(stderr, Name": imsm does not support more than 2 disks" |
3615 | "in a raid1 volume\n"); | |
3616 | return 0; | |
3617 | } | |
81062a36 DW |
3618 | |
3619 | map->raid_level = info->level; | |
4d1313e9 | 3620 | if (info->level == 10) { |
c2c087e6 | 3621 | map->raid_level = 1; |
4d1313e9 | 3622 | map->num_domains = info->raid_disks / 2; |
81062a36 DW |
3623 | } else if (info->level == 1) |
3624 | map->num_domains = info->raid_disks; | |
3625 | else | |
ff596308 | 3626 | map->num_domains = 1; |
81062a36 | 3627 | |
ff596308 DW |
3628 | num_data_stripes = info_to_num_data_stripes(info, map->num_domains); |
3629 | map->num_data_stripes = __cpu_to_le32(num_data_stripes); | |
ef6ffade | 3630 | |
c2c087e6 DW |
3631 | map->num_members = info->raid_disks; |
3632 | for (i = 0; i < map->num_members; i++) { | |
3633 | /* initialized in add_to_super */ | |
4eb26970 | 3634 | set_imsm_ord_tbl_ent(map, i, IMSM_ORD_REBUILD); |
c2c087e6 | 3635 | } |
949c47a0 | 3636 | mpb->num_raid_devs++; |
ba2de7ba DW |
3637 | |
3638 | dv->dev = dev; | |
3639 | dv->index = super->current_vol; | |
3640 | dv->next = super->devlist; | |
3641 | super->devlist = dv; | |
c2c087e6 | 3642 | |
4d1313e9 DW |
3643 | imsm_update_version_info(super); |
3644 | ||
c2c087e6 | 3645 | return 1; |
cdddbdbc DW |
3646 | } |
3647 | ||
bf5a934a DW |
3648 | static int init_super_imsm(struct supertype *st, mdu_array_info_t *info, |
3649 | unsigned long long size, char *name, | |
3650 | char *homehost, int *uuid) | |
3651 | { | |
3652 | /* This is primarily called by Create when creating a new array. | |
3653 | * We will then get add_to_super called for each component, and then | |
3654 | * write_init_super called to write it out to each device. | |
3655 | * For IMSM, Create can create on fresh devices or on a pre-existing | |
3656 | * array. | |
3657 | * To create on a pre-existing array a different method will be called. | |
3658 | * This one is just for fresh drives. | |
3659 | */ | |
3660 | struct intel_super *super; | |
3661 | struct imsm_super *mpb; | |
3662 | size_t mpb_size; | |
4d1313e9 | 3663 | char *version; |
bf5a934a | 3664 | |
bf5a934a | 3665 | if (st->sb) |
e683ca88 DW |
3666 | return init_super_imsm_volume(st, info, size, name, homehost, uuid); |
3667 | ||
3668 | if (info) | |
3669 | mpb_size = disks_to_mpb_size(info->nr_disks); | |
3670 | else | |
3671 | mpb_size = 512; | |
bf5a934a | 3672 | |
49133e57 | 3673 | super = alloc_super(); |
e683ca88 | 3674 | if (super && posix_memalign(&super->buf, 512, mpb_size) != 0) { |
bf5a934a | 3675 | free(super); |
e683ca88 DW |
3676 | super = NULL; |
3677 | } | |
3678 | if (!super) { | |
3679 | fprintf(stderr, Name | |
3680 | ": %s could not allocate superblock\n", __func__); | |
bf5a934a DW |
3681 | return 0; |
3682 | } | |
e683ca88 | 3683 | memset(super->buf, 0, mpb_size); |
ef649044 | 3684 | mpb = super->buf; |
e683ca88 DW |
3685 | mpb->mpb_size = __cpu_to_le32(mpb_size); |
3686 | st->sb = super; | |
3687 | ||
3688 | if (info == NULL) { | |
3689 | /* zeroing superblock */ | |
3690 | return 0; | |
3691 | } | |
bf5a934a | 3692 | |
4d1313e9 DW |
3693 | mpb->attributes = MPB_ATTRIB_CHECKSUM_VERIFY; |
3694 | ||
3695 | version = (char *) mpb->sig; | |
3696 | strcpy(version, MPB_SIGNATURE); | |
3697 | version += strlen(MPB_SIGNATURE); | |
3698 | strcpy(version, MPB_VERSION_RAID0); | |
bf5a934a | 3699 | |
bf5a934a DW |
3700 | return 1; |
3701 | } | |
3702 | ||
0e600426 | 3703 | #ifndef MDASSEMBLE |
f20c3968 | 3704 | static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk, |
bf5a934a DW |
3705 | int fd, char *devname) |
3706 | { | |
3707 | struct intel_super *super = st->sb; | |
d23fe947 | 3708 | struct imsm_super *mpb = super->anchor; |
bf5a934a DW |
3709 | struct dl *dl; |
3710 | struct imsm_dev *dev; | |
3711 | struct imsm_map *map; | |
4eb26970 | 3712 | int slot; |
bf5a934a | 3713 | |
949c47a0 | 3714 | dev = get_imsm_dev(super, super->current_vol); |
a965f303 | 3715 | map = get_imsm_map(dev, 0); |
bf5a934a | 3716 | |
208933a7 N |
3717 | if (! (dk->state & (1<<MD_DISK_SYNC))) { |
3718 | fprintf(stderr, Name ": %s: Cannot add spare devices to IMSM volume\n", | |
3719 | devname); | |
3720 | return 1; | |
3721 | } | |
3722 | ||
efb30e7f DW |
3723 | if (fd == -1) { |
3724 | /* we're doing autolayout so grab the pre-marked (in | |
3725 | * validate_geometry) raid_disk | |
3726 | */ | |
3727 | for (dl = super->disks; dl; dl = dl->next) | |
3728 | if (dl->raiddisk == dk->raid_disk) | |
3729 | break; | |
3730 | } else { | |
3731 | for (dl = super->disks; dl ; dl = dl->next) | |
3732 | if (dl->major == dk->major && | |
3733 | dl->minor == dk->minor) | |
3734 | break; | |
3735 | } | |
d23fe947 | 3736 | |
208933a7 N |
3737 | if (!dl) { |
3738 | fprintf(stderr, Name ": %s is not a member of the same container\n", devname); | |
f20c3968 | 3739 | return 1; |
208933a7 | 3740 | } |
bf5a934a | 3741 | |
d23fe947 DW |
3742 | /* add a pristine spare to the metadata */ |
3743 | if (dl->index < 0) { | |
3744 | dl->index = super->anchor->num_disks; | |
3745 | super->anchor->num_disks++; | |
3746 | } | |
4eb26970 DW |
3747 | /* Check the device has not already been added */ |
3748 | slot = get_imsm_disk_slot(map, dl->index); | |
3749 | if (slot >= 0 && | |
98130f40 | 3750 | (get_imsm_ord_tbl_ent(dev, slot, -1) & IMSM_ORD_REBUILD) == 0) { |
4eb26970 DW |
3751 | fprintf(stderr, Name ": %s has been included in this array twice\n", |
3752 | devname); | |
3753 | return 1; | |
3754 | } | |
be73972f | 3755 | set_imsm_ord_tbl_ent(map, dk->number, dl->index); |
ee5aad5a | 3756 | dl->disk.status = CONFIGURED_DISK; |
d23fe947 DW |
3757 | |
3758 | /* if we are creating the first raid device update the family number */ | |
3759 | if (super->current_vol == 0) { | |
3760 | __u32 sum; | |
3761 | struct imsm_dev *_dev = __get_imsm_dev(mpb, 0); | |
3762 | struct imsm_disk *_disk = __get_imsm_disk(mpb, dl->index); | |
3763 | ||
791b666a AW |
3764 | if (!_dev || !_disk) { |
3765 | fprintf(stderr, Name ": BUG mpb setup error\n"); | |
3766 | return 1; | |
3767 | } | |
d23fe947 DW |
3768 | *_dev = *dev; |
3769 | *_disk = dl->disk; | |
148acb7b DW |
3770 | sum = random32(); |
3771 | sum += __gen_imsm_checksum(mpb); | |
d23fe947 | 3772 | mpb->family_num = __cpu_to_le32(sum); |
148acb7b | 3773 | mpb->orig_family_num = mpb->family_num; |
d23fe947 | 3774 | } |
f20c3968 DW |
3775 | |
3776 | return 0; | |
bf5a934a DW |
3777 | } |
3778 | ||
88654014 | 3779 | |
f20c3968 | 3780 | static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk, |
88654014 | 3781 | int fd, char *devname) |
cdddbdbc | 3782 | { |
c2c087e6 | 3783 | struct intel_super *super = st->sb; |
c2c087e6 DW |
3784 | struct dl *dd; |
3785 | unsigned long long size; | |
f2f27e63 | 3786 | __u32 id; |
c2c087e6 DW |
3787 | int rv; |
3788 | struct stat stb; | |
3789 | ||
88654014 LM |
3790 | /* If we are on an RAID enabled platform check that the disk is |
3791 | * attached to the raid controller. | |
3792 | * We do not need to test disks attachment for container based additions, | |
3793 | * they shall be already tested when container was created/assembled. | |
88c32bb1 | 3794 | */ |
d424212e | 3795 | rv = find_intel_hba_capability(fd, super, devname); |
f2f5c343 | 3796 | /* no orom/efi or non-intel hba of the disk */ |
f0f5a016 LM |
3797 | if (rv != 0) { |
3798 | dprintf("capability: %p fd: %d ret: %d\n", | |
3799 | super->orom, fd, rv); | |
3800 | return 1; | |
88c32bb1 DW |
3801 | } |
3802 | ||
f20c3968 DW |
3803 | if (super->current_vol >= 0) |
3804 | return add_to_super_imsm_volume(st, dk, fd, devname); | |
bf5a934a | 3805 | |
c2c087e6 DW |
3806 | fstat(fd, &stb); |
3807 | dd = malloc(sizeof(*dd)); | |
b9f594fe | 3808 | if (!dd) { |
c2c087e6 DW |
3809 | fprintf(stderr, |
3810 | Name ": malloc failed %s:%d.\n", __func__, __LINE__); | |
f20c3968 | 3811 | return 1; |
c2c087e6 DW |
3812 | } |
3813 | memset(dd, 0, sizeof(*dd)); | |
3814 | dd->major = major(stb.st_rdev); | |
3815 | dd->minor = minor(stb.st_rdev); | |
b9f594fe | 3816 | dd->index = -1; |
c2c087e6 | 3817 | dd->devname = devname ? strdup(devname) : NULL; |
c2c087e6 | 3818 | dd->fd = fd; |
689c9bf3 | 3819 | dd->e = NULL; |
1a64be56 | 3820 | dd->action = DISK_ADD; |
c2c087e6 | 3821 | rv = imsm_read_serial(fd, devname, dd->serial); |
32ba9157 | 3822 | if (rv) { |
c2c087e6 | 3823 | fprintf(stderr, |
0030e8d6 | 3824 | Name ": failed to retrieve scsi serial, aborting\n"); |
949c47a0 | 3825 | free(dd); |
0030e8d6 | 3826 | abort(); |
c2c087e6 DW |
3827 | } |
3828 | ||
c2c087e6 DW |
3829 | get_dev_size(fd, NULL, &size); |
3830 | size /= 512; | |
1f24f035 | 3831 | serialcpy(dd->disk.serial, dd->serial); |
b9f594fe | 3832 | dd->disk.total_blocks = __cpu_to_le32(size); |
ee5aad5a | 3833 | dd->disk.status = SPARE_DISK; |
c2c087e6 | 3834 | if (sysfs_disk_to_scsi_id(fd, &id) == 0) |
b9f594fe | 3835 | dd->disk.scsi_id = __cpu_to_le32(id); |
c2c087e6 | 3836 | else |
b9f594fe | 3837 | dd->disk.scsi_id = __cpu_to_le32(0); |
43dad3d6 DW |
3838 | |
3839 | if (st->update_tail) { | |
1a64be56 LM |
3840 | dd->next = super->disk_mgmt_list; |
3841 | super->disk_mgmt_list = dd; | |
43dad3d6 DW |
3842 | } else { |
3843 | dd->next = super->disks; | |
3844 | super->disks = dd; | |
ceaf0ee1 | 3845 | super->updates_pending++; |
43dad3d6 | 3846 | } |
f20c3968 DW |
3847 | |
3848 | return 0; | |
cdddbdbc DW |
3849 | } |
3850 | ||
1a64be56 LM |
3851 | |
3852 | static int remove_from_super_imsm(struct supertype *st, mdu_disk_info_t *dk) | |
3853 | { | |
3854 | struct intel_super *super = st->sb; | |
3855 | struct dl *dd; | |
3856 | ||
3857 | /* remove from super works only in mdmon - for communication | |
3858 | * manager - monitor. Check if communication memory buffer | |
3859 | * is prepared. | |
3860 | */ | |
3861 | if (!st->update_tail) { | |
3862 | fprintf(stderr, | |
3863 | Name ": %s shall be used in mdmon context only" | |
3864 | "(line %d).\n", __func__, __LINE__); | |
3865 | return 1; | |
3866 | } | |
3867 | dd = malloc(sizeof(*dd)); | |
3868 | if (!dd) { | |
3869 | fprintf(stderr, | |
3870 | Name ": malloc failed %s:%d.\n", __func__, __LINE__); | |
3871 | return 1; | |
3872 | } | |
3873 | memset(dd, 0, sizeof(*dd)); | |
3874 | dd->major = dk->major; | |
3875 | dd->minor = dk->minor; | |
3876 | dd->index = -1; | |
3877 | dd->fd = -1; | |
3878 | dd->disk.status = SPARE_DISK; | |
3879 | dd->action = DISK_REMOVE; | |
3880 | ||
3881 | dd->next = super->disk_mgmt_list; | |
3882 | super->disk_mgmt_list = dd; | |
3883 | ||
3884 | ||
3885 | return 0; | |
3886 | } | |
3887 | ||
f796af5d DW |
3888 | static int store_imsm_mpb(int fd, struct imsm_super *mpb); |
3889 | ||
3890 | static union { | |
3891 | char buf[512]; | |
3892 | struct imsm_super anchor; | |
3893 | } spare_record __attribute__ ((aligned(512))); | |
c2c087e6 | 3894 | |
d23fe947 DW |
3895 | /* spare records have their own family number and do not have any defined raid |
3896 | * devices | |
3897 | */ | |
3898 | static int write_super_imsm_spares(struct intel_super *super, int doclose) | |
3899 | { | |
d23fe947 | 3900 | struct imsm_super *mpb = super->anchor; |
f796af5d | 3901 | struct imsm_super *spare = &spare_record.anchor; |
d23fe947 DW |
3902 | __u32 sum; |
3903 | struct dl *d; | |
3904 | ||
f796af5d DW |
3905 | spare->mpb_size = __cpu_to_le32(sizeof(struct imsm_super)), |
3906 | spare->generation_num = __cpu_to_le32(1UL), | |
3907 | spare->attributes = MPB_ATTRIB_CHECKSUM_VERIFY; | |
3908 | spare->num_disks = 1, | |
3909 | spare->num_raid_devs = 0, | |
3910 | spare->cache_size = mpb->cache_size, | |
3911 | spare->pwr_cycle_count = __cpu_to_le32(1), | |
3912 | ||
3913 | snprintf((char *) spare->sig, MAX_SIGNATURE_LENGTH, | |
3914 | MPB_SIGNATURE MPB_VERSION_RAID0); | |
d23fe947 DW |
3915 | |
3916 | for (d = super->disks; d; d = d->next) { | |
8796fdc4 | 3917 | if (d->index != -1) |
d23fe947 DW |
3918 | continue; |
3919 | ||
f796af5d DW |
3920 | spare->disk[0] = d->disk; |
3921 | sum = __gen_imsm_checksum(spare); | |
3922 | spare->family_num = __cpu_to_le32(sum); | |
3923 | spare->orig_family_num = 0; | |
3924 | sum = __gen_imsm_checksum(spare); | |
3925 | spare->check_sum = __cpu_to_le32(sum); | |
d23fe947 | 3926 | |
f796af5d | 3927 | if (store_imsm_mpb(d->fd, spare)) { |
d23fe947 DW |
3928 | fprintf(stderr, "%s: failed for device %d:%d %s\n", |
3929 | __func__, d->major, d->minor, strerror(errno)); | |
e74255d9 | 3930 | return 1; |
d23fe947 DW |
3931 | } |
3932 | if (doclose) { | |
3933 | close(d->fd); | |
3934 | d->fd = -1; | |
3935 | } | |
3936 | } | |
3937 | ||
e74255d9 | 3938 | return 0; |
d23fe947 DW |
3939 | } |
3940 | ||
36988a3d | 3941 | static int write_super_imsm(struct supertype *st, int doclose) |
cdddbdbc | 3942 | { |
36988a3d | 3943 | struct intel_super *super = st->sb; |
949c47a0 | 3944 | struct imsm_super *mpb = super->anchor; |
c2c087e6 DW |
3945 | struct dl *d; |
3946 | __u32 generation; | |
3947 | __u32 sum; | |
d23fe947 | 3948 | int spares = 0; |
949c47a0 | 3949 | int i; |
a48ac0a8 | 3950 | __u32 mpb_size = sizeof(struct imsm_super) - sizeof(struct imsm_disk); |
36988a3d | 3951 | int num_disks = 0; |
cdddbdbc | 3952 | |
c2c087e6 DW |
3953 | /* 'generation' is incremented everytime the metadata is written */ |
3954 | generation = __le32_to_cpu(mpb->generation_num); | |
3955 | generation++; | |
3956 | mpb->generation_num = __cpu_to_le32(generation); | |
3957 | ||
148acb7b DW |
3958 | /* fix up cases where previous mdadm releases failed to set |
3959 | * orig_family_num | |
3960 | */ | |
3961 | if (mpb->orig_family_num == 0) | |
3962 | mpb->orig_family_num = mpb->family_num; | |
3963 | ||
d23fe947 | 3964 | for (d = super->disks; d; d = d->next) { |
8796fdc4 | 3965 | if (d->index == -1) |
d23fe947 | 3966 | spares++; |
36988a3d | 3967 | else { |
d23fe947 | 3968 | mpb->disk[d->index] = d->disk; |
36988a3d AK |
3969 | num_disks++; |
3970 | } | |
d23fe947 | 3971 | } |
36988a3d | 3972 | for (d = super->missing; d; d = d->next) { |
47ee5a45 | 3973 | mpb->disk[d->index] = d->disk; |
36988a3d AK |
3974 | num_disks++; |
3975 | } | |
3976 | mpb->num_disks = num_disks; | |
3977 | mpb_size += sizeof(struct imsm_disk) * mpb->num_disks; | |
b9f594fe | 3978 | |
949c47a0 DW |
3979 | for (i = 0; i < mpb->num_raid_devs; i++) { |
3980 | struct imsm_dev *dev = __get_imsm_dev(mpb, i); | |
36988a3d AK |
3981 | struct imsm_dev *dev2 = get_imsm_dev(super, i); |
3982 | if (dev && dev2) { | |
3983 | imsm_copy_dev(dev, dev2); | |
3984 | mpb_size += sizeof_imsm_dev(dev, 0); | |
3985 | } | |
949c47a0 | 3986 | } |
a48ac0a8 DW |
3987 | mpb_size += __le32_to_cpu(mpb->bbm_log_size); |
3988 | mpb->mpb_size = __cpu_to_le32(mpb_size); | |
949c47a0 | 3989 | |
c2c087e6 | 3990 | /* recalculate checksum */ |
949c47a0 | 3991 | sum = __gen_imsm_checksum(mpb); |
c2c087e6 DW |
3992 | mpb->check_sum = __cpu_to_le32(sum); |
3993 | ||
d23fe947 | 3994 | /* write the mpb for disks that compose raid devices */ |
c2c087e6 | 3995 | for (d = super->disks; d ; d = d->next) { |
d23fe947 DW |
3996 | if (d->index < 0) |
3997 | continue; | |
f796af5d | 3998 | if (store_imsm_mpb(d->fd, mpb)) |
c2c087e6 DW |
3999 | fprintf(stderr, "%s: failed for device %d:%d %s\n", |
4000 | __func__, d->major, d->minor, strerror(errno)); | |
c2c087e6 DW |
4001 | if (doclose) { |
4002 | close(d->fd); | |
4003 | d->fd = -1; | |
4004 | } | |
4005 | } | |
4006 | ||
d23fe947 DW |
4007 | if (spares) |
4008 | return write_super_imsm_spares(super, doclose); | |
4009 | ||
e74255d9 | 4010 | return 0; |
c2c087e6 DW |
4011 | } |
4012 | ||
0e600426 | 4013 | |
9b1fb677 | 4014 | static int create_array(struct supertype *st, int dev_idx) |
43dad3d6 DW |
4015 | { |
4016 | size_t len; | |
4017 | struct imsm_update_create_array *u; | |
4018 | struct intel_super *super = st->sb; | |
9b1fb677 | 4019 | struct imsm_dev *dev = get_imsm_dev(super, dev_idx); |
54c2c1ea DW |
4020 | struct imsm_map *map = get_imsm_map(dev, 0); |
4021 | struct disk_info *inf; | |
4022 | struct imsm_disk *disk; | |
4023 | int i; | |
43dad3d6 | 4024 | |
54c2c1ea DW |
4025 | len = sizeof(*u) - sizeof(*dev) + sizeof_imsm_dev(dev, 0) + |
4026 | sizeof(*inf) * map->num_members; | |
43dad3d6 DW |
4027 | u = malloc(len); |
4028 | if (!u) { | |
4029 | fprintf(stderr, "%s: failed to allocate update buffer\n", | |
4030 | __func__); | |
4031 | return 1; | |
4032 | } | |
4033 | ||
4034 | u->type = update_create_array; | |
9b1fb677 | 4035 | u->dev_idx = dev_idx; |
43dad3d6 | 4036 | imsm_copy_dev(&u->dev, dev); |
54c2c1ea DW |
4037 | inf = get_disk_info(u); |
4038 | for (i = 0; i < map->num_members; i++) { | |
98130f40 | 4039 | int idx = get_imsm_disk_idx(dev, i, -1); |
9b1fb677 | 4040 | |
54c2c1ea DW |
4041 | disk = get_imsm_disk(super, idx); |
4042 | serialcpy(inf[i].serial, disk->serial); | |
4043 | } | |
43dad3d6 DW |
4044 | append_metadata_update(st, u, len); |
4045 | ||
4046 | return 0; | |
4047 | } | |
4048 | ||
1a64be56 | 4049 | static int mgmt_disk(struct supertype *st) |
43dad3d6 DW |
4050 | { |
4051 | struct intel_super *super = st->sb; | |
4052 | size_t len; | |
1a64be56 | 4053 | struct imsm_update_add_remove_disk *u; |
43dad3d6 | 4054 | |
1a64be56 | 4055 | if (!super->disk_mgmt_list) |
43dad3d6 DW |
4056 | return 0; |
4057 | ||
4058 | len = sizeof(*u); | |
4059 | u = malloc(len); | |
4060 | if (!u) { | |
4061 | fprintf(stderr, "%s: failed to allocate update buffer\n", | |
4062 | __func__); | |
4063 | return 1; | |
4064 | } | |
4065 | ||
1a64be56 | 4066 | u->type = update_add_remove_disk; |
43dad3d6 DW |
4067 | append_metadata_update(st, u, len); |
4068 | ||
4069 | return 0; | |
4070 | } | |
4071 | ||
c2c087e6 DW |
4072 | static int write_init_super_imsm(struct supertype *st) |
4073 | { | |
9b1fb677 DW |
4074 | struct intel_super *super = st->sb; |
4075 | int current_vol = super->current_vol; | |
4076 | ||
4077 | /* we are done with current_vol reset it to point st at the container */ | |
4078 | super->current_vol = -1; | |
4079 | ||
8273f55e | 4080 | if (st->update_tail) { |
43dad3d6 DW |
4081 | /* queue the recently created array / added disk |
4082 | * as a metadata update */ | |
43dad3d6 | 4083 | int rv; |
8273f55e | 4084 | |
43dad3d6 | 4085 | /* determine if we are creating a volume or adding a disk */ |
9b1fb677 | 4086 | if (current_vol < 0) { |
1a64be56 LM |
4087 | /* in the mgmt (add/remove) disk case we are running |
4088 | * in mdmon context, so don't close fd's | |
43dad3d6 | 4089 | */ |
1a64be56 | 4090 | return mgmt_disk(st); |
43dad3d6 | 4091 | } else |
9b1fb677 | 4092 | rv = create_array(st, current_vol); |
8273f55e | 4093 | |
43dad3d6 | 4094 | return rv; |
d682f344 N |
4095 | } else { |
4096 | struct dl *d; | |
4097 | for (d = super->disks; d; d = d->next) | |
4098 | Kill(d->devname, NULL, 0, 1, 1); | |
36988a3d | 4099 | return write_super_imsm(st, 1); |
d682f344 | 4100 | } |
cdddbdbc | 4101 | } |
0e600426 | 4102 | #endif |
cdddbdbc | 4103 | |
e683ca88 | 4104 | static int store_super_imsm(struct supertype *st, int fd) |
cdddbdbc | 4105 | { |
e683ca88 DW |
4106 | struct intel_super *super = st->sb; |
4107 | struct imsm_super *mpb = super ? super->anchor : NULL; | |
551c80c1 | 4108 | |
e683ca88 | 4109 | if (!mpb) |
ad97895e DW |
4110 | return 1; |
4111 | ||
1799c9e8 | 4112 | #ifndef MDASSEMBLE |
e683ca88 | 4113 | return store_imsm_mpb(fd, mpb); |
1799c9e8 N |
4114 | #else |
4115 | return 1; | |
4116 | #endif | |
cdddbdbc DW |
4117 | } |
4118 | ||
0e600426 N |
4119 | static int imsm_bbm_log_size(struct imsm_super *mpb) |
4120 | { | |
4121 | return __le32_to_cpu(mpb->bbm_log_size); | |
4122 | } | |
4123 | ||
4124 | #ifndef MDASSEMBLE | |
cdddbdbc DW |
4125 | static int validate_geometry_imsm_container(struct supertype *st, int level, |
4126 | int layout, int raiddisks, int chunk, | |
c2c087e6 | 4127 | unsigned long long size, char *dev, |
2c514b71 NB |
4128 | unsigned long long *freesize, |
4129 | int verbose) | |
cdddbdbc | 4130 | { |
c2c087e6 DW |
4131 | int fd; |
4132 | unsigned long long ldsize; | |
f2f5c343 LM |
4133 | struct intel_super *super=NULL; |
4134 | int rv = 0; | |
cdddbdbc | 4135 | |
c2c087e6 DW |
4136 | if (level != LEVEL_CONTAINER) |
4137 | return 0; | |
4138 | if (!dev) | |
4139 | return 1; | |
4140 | ||
4141 | fd = open(dev, O_RDONLY|O_EXCL, 0); | |
4142 | if (fd < 0) { | |
2c514b71 NB |
4143 | if (verbose) |
4144 | fprintf(stderr, Name ": imsm: Cannot open %s: %s\n", | |
4145 | dev, strerror(errno)); | |
c2c087e6 DW |
4146 | return 0; |
4147 | } | |
4148 | if (!get_dev_size(fd, dev, &ldsize)) { | |
4149 | close(fd); | |
4150 | return 0; | |
4151 | } | |
f2f5c343 LM |
4152 | |
4153 | /* capabilities retrieve could be possible | |
4154 | * note that there is no fd for the disks in array. | |
4155 | */ | |
4156 | super = alloc_super(); | |
4157 | if (!super) { | |
4158 | fprintf(stderr, | |
4159 | Name ": malloc of %zu failed.\n", | |
4160 | sizeof(*super)); | |
4161 | close(fd); | |
4162 | return 0; | |
4163 | } | |
4164 | ||
d424212e | 4165 | rv = find_intel_hba_capability(fd, super, verbose ? dev : NULL); |
f2f5c343 LM |
4166 | if (rv != 0) { |
4167 | #if DEBUG | |
4168 | char str[256]; | |
4169 | fd2devname(fd, str); | |
4170 | dprintf("validate_geometry_imsm_container: fd: %d %s orom: %p rv: %d raiddisk: %d\n", | |
4171 | fd, str, super->orom, rv, raiddisks); | |
4172 | #endif | |
4173 | /* no orom/efi or non-intel hba of the disk */ | |
4174 | close(fd); | |
4175 | free_imsm(super); | |
4176 | return 0; | |
4177 | } | |
c2c087e6 | 4178 | close(fd); |
f2f5c343 LM |
4179 | if (super->orom && raiddisks > super->orom->tds) { |
4180 | if (verbose) | |
4181 | fprintf(stderr, Name ": %d exceeds maximum number of" | |
4182 | " platform supported disks: %d\n", | |
4183 | raiddisks, super->orom->tds); | |
4184 | ||
4185 | free_imsm(super); | |
4186 | return 0; | |
4187 | } | |
c2c087e6 DW |
4188 | |
4189 | *freesize = avail_size_imsm(st, ldsize >> 9); | |
f2f5c343 | 4190 | free_imsm(super); |
c2c087e6 DW |
4191 | |
4192 | return 1; | |
cdddbdbc DW |
4193 | } |
4194 | ||
0dcecb2e DW |
4195 | static unsigned long long find_size(struct extent *e, int *idx, int num_extents) |
4196 | { | |
4197 | const unsigned long long base_start = e[*idx].start; | |
4198 | unsigned long long end = base_start + e[*idx].size; | |
4199 | int i; | |
4200 | ||
4201 | if (base_start == end) | |
4202 | return 0; | |
4203 | ||
4204 | *idx = *idx + 1; | |
4205 | for (i = *idx; i < num_extents; i++) { | |
4206 | /* extend overlapping extents */ | |
4207 | if (e[i].start >= base_start && | |
4208 | e[i].start <= end) { | |
4209 | if (e[i].size == 0) | |
4210 | return 0; | |
4211 | if (e[i].start + e[i].size > end) | |
4212 | end = e[i].start + e[i].size; | |
4213 | } else if (e[i].start > end) { | |
4214 | *idx = i; | |
4215 | break; | |
4216 | } | |
4217 | } | |
4218 | ||
4219 | return end - base_start; | |
4220 | } | |
4221 | ||
4222 | static unsigned long long merge_extents(struct intel_super *super, int sum_extents) | |
4223 | { | |
4224 | /* build a composite disk with all known extents and generate a new | |
4225 | * 'maxsize' given the "all disks in an array must share a common start | |
4226 | * offset" constraint | |
4227 | */ | |
4228 | struct extent *e = calloc(sum_extents, sizeof(*e)); | |
4229 | struct dl *dl; | |
4230 | int i, j; | |
4231 | int start_extent; | |
4232 | unsigned long long pos; | |
b9d77223 | 4233 | unsigned long long start = 0; |
0dcecb2e DW |
4234 | unsigned long long maxsize; |
4235 | unsigned long reserve; | |
4236 | ||
4237 | if (!e) | |
a7dd165b | 4238 | return 0; |
0dcecb2e DW |
4239 | |
4240 | /* coalesce and sort all extents. also, check to see if we need to | |
4241 | * reserve space between member arrays | |
4242 | */ | |
4243 | j = 0; | |
4244 | for (dl = super->disks; dl; dl = dl->next) { | |
4245 | if (!dl->e) | |
4246 | continue; | |
4247 | for (i = 0; i < dl->extent_cnt; i++) | |
4248 | e[j++] = dl->e[i]; | |
4249 | } | |
4250 | qsort(e, sum_extents, sizeof(*e), cmp_extent); | |
4251 | ||
4252 | /* merge extents */ | |
4253 | i = 0; | |
4254 | j = 0; | |
4255 | while (i < sum_extents) { | |
4256 | e[j].start = e[i].start; | |
4257 | e[j].size = find_size(e, &i, sum_extents); | |
4258 | j++; | |
4259 | if (e[j-1].size == 0) | |
4260 | break; | |
4261 | } | |
4262 | ||
4263 | pos = 0; | |
4264 | maxsize = 0; | |
4265 | start_extent = 0; | |
4266 | i = 0; | |
4267 | do { | |
4268 | unsigned long long esize; | |
4269 | ||
4270 | esize = e[i].start - pos; | |
4271 | if (esize >= maxsize) { | |
4272 | maxsize = esize; | |
4273 | start = pos; | |
4274 | start_extent = i; | |
4275 | } | |
4276 | pos = e[i].start + e[i].size; | |
4277 | i++; | |
4278 | } while (e[i-1].size); | |
4279 | free(e); | |
4280 | ||
a7dd165b DW |
4281 | if (maxsize == 0) |
4282 | return 0; | |
4283 | ||
4284 | /* FIXME assumes volume at offset 0 is the first volume in a | |
4285 | * container | |
4286 | */ | |
0dcecb2e DW |
4287 | if (start_extent > 0) |
4288 | reserve = IMSM_RESERVED_SECTORS; /* gap between raid regions */ | |
4289 | else | |
4290 | reserve = 0; | |
4291 | ||
4292 | if (maxsize < reserve) | |
a7dd165b | 4293 | return 0; |
0dcecb2e DW |
4294 | |
4295 | super->create_offset = ~((__u32) 0); | |
4296 | if (start + reserve > super->create_offset) | |
a7dd165b | 4297 | return 0; /* start overflows create_offset */ |
0dcecb2e DW |
4298 | super->create_offset = start + reserve; |
4299 | ||
4300 | return maxsize - reserve; | |
4301 | } | |
4302 | ||
88c32bb1 DW |
4303 | static int is_raid_level_supported(const struct imsm_orom *orom, int level, int raiddisks) |
4304 | { | |
4305 | if (level < 0 || level == 6 || level == 4) | |
4306 | return 0; | |
4307 | ||
4308 | /* if we have an orom prevent invalid raid levels */ | |
4309 | if (orom) | |
4310 | switch (level) { | |
4311 | case 0: return imsm_orom_has_raid0(orom); | |
4312 | case 1: | |
4313 | if (raiddisks > 2) | |
4314 | return imsm_orom_has_raid1e(orom); | |
1c556e92 DW |
4315 | return imsm_orom_has_raid1(orom) && raiddisks == 2; |
4316 | case 10: return imsm_orom_has_raid10(orom) && raiddisks == 4; | |
4317 | case 5: return imsm_orom_has_raid5(orom) && raiddisks > 2; | |
88c32bb1 DW |
4318 | } |
4319 | else | |
4320 | return 1; /* not on an Intel RAID platform so anything goes */ | |
4321 | ||
4322 | return 0; | |
4323 | } | |
4324 | ||
73408129 | 4325 | |
35f81cbb | 4326 | #define pr_vrb(fmt, arg...) (void) (verbose && fprintf(stderr, Name fmt, ##arg)) |
73408129 LM |
4327 | /* |
4328 | * validate volume parameters with OROM/EFI capabilities | |
4329 | */ | |
6592ce37 DW |
4330 | static int |
4331 | validate_geometry_imsm_orom(struct intel_super *super, int level, int layout, | |
c21e737b | 4332 | int raiddisks, int *chunk, int verbose) |
6592ce37 | 4333 | { |
73408129 LM |
4334 | #if DEBUG |
4335 | verbose = 1; | |
4336 | #endif | |
4337 | /* validate container capabilities */ | |
4338 | if (super->orom && raiddisks > super->orom->tds) { | |
4339 | if (verbose) | |
4340 | fprintf(stderr, Name ": %d exceeds maximum number of" | |
4341 | " platform supported disks: %d\n", | |
4342 | raiddisks, super->orom->tds); | |
4343 | return 0; | |
4344 | } | |
4345 | ||
4346 | /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */ | |
4347 | if (super->orom && (!is_raid_level_supported(super->orom, level, | |
4348 | raiddisks))) { | |
6592ce37 DW |
4349 | pr_vrb(": platform does not support raid%d with %d disk%s\n", |
4350 | level, raiddisks, raiddisks > 1 ? "s" : ""); | |
4351 | return 0; | |
4352 | } | |
c21e737b CA |
4353 | if (super->orom && level != 1) { |
4354 | if (chunk && (*chunk == 0 || *chunk == UnSet)) | |
4355 | *chunk = imsm_orom_default_chunk(super->orom); | |
4356 | else if (chunk && !imsm_orom_has_chunk(super->orom, *chunk)) { | |
4357 | pr_vrb(": platform does not support a chunk size of: " | |
4358 | "%d\n", *chunk); | |
4359 | return 0; | |
4360 | } | |
6592ce37 DW |
4361 | } |
4362 | if (layout != imsm_level_to_layout(level)) { | |
4363 | if (level == 5) | |
4364 | pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n"); | |
4365 | else if (level == 10) | |
4366 | pr_vrb(": imsm raid 10 only supports the n2 layout\n"); | |
4367 | else | |
4368 | pr_vrb(": imsm unknown layout %#x for this raid level %d\n", | |
4369 | layout, level); | |
4370 | return 0; | |
4371 | } | |
6592ce37 DW |
4372 | return 1; |
4373 | } | |
4374 | ||
c2c087e6 DW |
4375 | /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd |
4376 | * FIX ME add ahci details | |
4377 | */ | |
8b353278 | 4378 | static int validate_geometry_imsm_volume(struct supertype *st, int level, |
c21e737b | 4379 | int layout, int raiddisks, int *chunk, |
c2c087e6 | 4380 | unsigned long long size, char *dev, |
2c514b71 NB |
4381 | unsigned long long *freesize, |
4382 | int verbose) | |
cdddbdbc | 4383 | { |
c2c087e6 DW |
4384 | struct stat stb; |
4385 | struct intel_super *super = st->sb; | |
a20d2ba5 | 4386 | struct imsm_super *mpb = super->anchor; |
c2c087e6 DW |
4387 | struct dl *dl; |
4388 | unsigned long long pos = 0; | |
4389 | unsigned long long maxsize; | |
4390 | struct extent *e; | |
4391 | int i; | |
cdddbdbc | 4392 | |
88c32bb1 DW |
4393 | /* We must have the container info already read in. */ |
4394 | if (!super) | |
c2c087e6 DW |
4395 | return 0; |
4396 | ||
d54559f0 LM |
4397 | if (!validate_geometry_imsm_orom(super, level, layout, raiddisks, chunk, verbose)) { |
4398 | fprintf(stderr, Name ": RAID gemetry validation failed. " | |
4399 | "Cannot proceed with the action(s).\n"); | |
c2c087e6 | 4400 | return 0; |
d54559f0 | 4401 | } |
c2c087e6 DW |
4402 | if (!dev) { |
4403 | /* General test: make sure there is space for | |
2da8544a DW |
4404 | * 'raiddisks' device extents of size 'size' at a given |
4405 | * offset | |
c2c087e6 | 4406 | */ |
e46273eb | 4407 | unsigned long long minsize = size; |
b7528a20 | 4408 | unsigned long long start_offset = MaxSector; |
c2c087e6 DW |
4409 | int dcnt = 0; |
4410 | if (minsize == 0) | |
4411 | minsize = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS; | |
4412 | for (dl = super->disks; dl ; dl = dl->next) { | |
4413 | int found = 0; | |
4414 | ||
bf5a934a | 4415 | pos = 0; |
c2c087e6 DW |
4416 | i = 0; |
4417 | e = get_extents(super, dl); | |
4418 | if (!e) continue; | |
4419 | do { | |
4420 | unsigned long long esize; | |
4421 | esize = e[i].start - pos; | |
4422 | if (esize >= minsize) | |
4423 | found = 1; | |
b7528a20 | 4424 | if (found && start_offset == MaxSector) { |
2da8544a DW |
4425 | start_offset = pos; |
4426 | break; | |
4427 | } else if (found && pos != start_offset) { | |
4428 | found = 0; | |
4429 | break; | |
4430 | } | |
c2c087e6 DW |
4431 | pos = e[i].start + e[i].size; |
4432 | i++; | |
4433 | } while (e[i-1].size); | |
4434 | if (found) | |
4435 | dcnt++; | |
4436 | free(e); | |
4437 | } | |
4438 | if (dcnt < raiddisks) { | |
2c514b71 NB |
4439 | if (verbose) |
4440 | fprintf(stderr, Name ": imsm: Not enough " | |
4441 | "devices with space for this array " | |
4442 | "(%d < %d)\n", | |
4443 | dcnt, raiddisks); | |
c2c087e6 DW |
4444 | return 0; |
4445 | } | |
4446 | return 1; | |
4447 | } | |
0dcecb2e | 4448 | |
c2c087e6 DW |
4449 | /* This device must be a member of the set */ |
4450 | if (stat(dev, &stb) < 0) | |
4451 | return 0; | |
4452 | if ((S_IFMT & stb.st_mode) != S_IFBLK) | |
4453 | return 0; | |
4454 | for (dl = super->disks ; dl ; dl = dl->next) { | |
f21e18ca N |
4455 | if (dl->major == (int)major(stb.st_rdev) && |
4456 | dl->minor == (int)minor(stb.st_rdev)) | |
c2c087e6 DW |
4457 | break; |
4458 | } | |
4459 | if (!dl) { | |
2c514b71 NB |
4460 | if (verbose) |
4461 | fprintf(stderr, Name ": %s is not in the " | |
4462 | "same imsm set\n", dev); | |
c2c087e6 | 4463 | return 0; |
a20d2ba5 DW |
4464 | } else if (super->orom && dl->index < 0 && mpb->num_raid_devs) { |
4465 | /* If a volume is present then the current creation attempt | |
4466 | * cannot incorporate new spares because the orom may not | |
4467 | * understand this configuration (all member disks must be | |
4468 | * members of each array in the container). | |
4469 | */ | |
4470 | fprintf(stderr, Name ": %s is a spare and a volume" | |
4471 | " is already defined for this container\n", dev); | |
4472 | fprintf(stderr, Name ": The option-rom requires all member" | |
4473 | " disks to be a member of all volumes\n"); | |
4474 | return 0; | |
c2c087e6 | 4475 | } |
0dcecb2e DW |
4476 | |
4477 | /* retrieve the largest free space block */ | |
c2c087e6 DW |
4478 | e = get_extents(super, dl); |
4479 | maxsize = 0; | |
4480 | i = 0; | |
0dcecb2e DW |
4481 | if (e) { |
4482 | do { | |
4483 | unsigned long long esize; | |
4484 | ||
4485 | esize = e[i].start - pos; | |
4486 | if (esize >= maxsize) | |
4487 | maxsize = esize; | |
4488 | pos = e[i].start + e[i].size; | |
4489 | i++; | |
4490 | } while (e[i-1].size); | |
4491 | dl->e = e; | |
4492 | dl->extent_cnt = i; | |
4493 | } else { | |
4494 | if (verbose) | |
4495 | fprintf(stderr, Name ": unable to determine free space for: %s\n", | |
4496 | dev); | |
4497 | return 0; | |
4498 | } | |
4499 | if (maxsize < size) { | |
4500 | if (verbose) | |
4501 | fprintf(stderr, Name ": %s not enough space (%llu < %llu)\n", | |
4502 | dev, maxsize, size); | |
4503 | return 0; | |
4504 | } | |
4505 | ||
4506 | /* count total number of extents for merge */ | |
4507 | i = 0; | |
4508 | for (dl = super->disks; dl; dl = dl->next) | |
4509 | if (dl->e) | |
4510 | i += dl->extent_cnt; | |
4511 | ||
4512 | maxsize = merge_extents(super, i); | |
a7dd165b | 4513 | if (maxsize < size || maxsize == 0) { |
0dcecb2e DW |
4514 | if (verbose) |
4515 | fprintf(stderr, Name ": not enough space after merge (%llu < %llu)\n", | |
4516 | maxsize, size); | |
4517 | return 0; | |
0dcecb2e DW |
4518 | } |
4519 | ||
c2c087e6 DW |
4520 | *freesize = maxsize; |
4521 | ||
4522 | return 1; | |
cdddbdbc DW |
4523 | } |
4524 | ||
efb30e7f DW |
4525 | static int reserve_space(struct supertype *st, int raiddisks, |
4526 | unsigned long long size, int chunk, | |
4527 | unsigned long long *freesize) | |
4528 | { | |
4529 | struct intel_super *super = st->sb; | |
4530 | struct imsm_super *mpb = super->anchor; | |
4531 | struct dl *dl; | |
4532 | int i; | |
4533 | int extent_cnt; | |
4534 | struct extent *e; | |
4535 | unsigned long long maxsize; | |
4536 | unsigned long long minsize; | |
4537 | int cnt; | |
4538 | int used; | |
4539 | ||
4540 | /* find the largest common start free region of the possible disks */ | |
4541 | used = 0; | |
4542 | extent_cnt = 0; | |
4543 | cnt = 0; | |
4544 | for (dl = super->disks; dl; dl = dl->next) { | |
4545 | dl->raiddisk = -1; | |
4546 | ||
4547 | if (dl->index >= 0) | |
4548 | used++; | |
4549 | ||
4550 | /* don't activate new spares if we are orom constrained | |
4551 | * and there is already a volume active in the container | |
4552 | */ | |
4553 | if (super->orom && dl->index < 0 && mpb->num_raid_devs) | |
4554 | continue; | |
4555 | ||
4556 | e = get_extents(super, dl); | |
4557 | if (!e) | |
4558 | continue; | |
4559 | for (i = 1; e[i-1].size; i++) | |
4560 | ; | |
4561 | dl->e = e; | |
4562 | dl->extent_cnt = i; | |
4563 | extent_cnt += i; | |
4564 | cnt++; | |
4565 | } | |
4566 | ||
4567 | maxsize = merge_extents(super, extent_cnt); | |
4568 | minsize = size; | |
4569 | if (size == 0) | |
612e59d8 CA |
4570 | /* chunk is in K */ |
4571 | minsize = chunk * 2; | |
efb30e7f DW |
4572 | |
4573 | if (cnt < raiddisks || | |
4574 | (super->orom && used && used != raiddisks) || | |
a7dd165b DW |
4575 | maxsize < minsize || |
4576 | maxsize == 0) { | |
efb30e7f DW |
4577 | fprintf(stderr, Name ": not enough devices with space to create array.\n"); |
4578 | return 0; /* No enough free spaces large enough */ | |
4579 | } | |
4580 | ||
4581 | if (size == 0) { | |
4582 | size = maxsize; | |
4583 | if (chunk) { | |
612e59d8 CA |
4584 | size /= 2 * chunk; |
4585 | size *= 2 * chunk; | |
efb30e7f DW |
4586 | } |
4587 | } | |
4588 | ||
4589 | cnt = 0; | |
4590 | for (dl = super->disks; dl; dl = dl->next) | |
4591 | if (dl->e) | |
4592 | dl->raiddisk = cnt++; | |
4593 | ||
4594 | *freesize = size; | |
4595 | ||
4596 | return 1; | |
4597 | } | |
4598 | ||
bf5a934a | 4599 | static int validate_geometry_imsm(struct supertype *st, int level, int layout, |
c21e737b | 4600 | int raiddisks, int *chunk, unsigned long long size, |
bf5a934a DW |
4601 | char *dev, unsigned long long *freesize, |
4602 | int verbose) | |
4603 | { | |
4604 | int fd, cfd; | |
4605 | struct mdinfo *sra; | |
20cbe8d2 | 4606 | int is_member = 0; |
bf5a934a | 4607 | |
d54559f0 LM |
4608 | /* load capability |
4609 | * if given unused devices create a container | |
bf5a934a DW |
4610 | * if given given devices in a container create a member volume |
4611 | */ | |
4612 | if (level == LEVEL_CONTAINER) { | |
4613 | /* Must be a fresh device to add to a container */ | |
4614 | return validate_geometry_imsm_container(st, level, layout, | |
c21e737b CA |
4615 | raiddisks, |
4616 | chunk?*chunk:0, size, | |
bf5a934a DW |
4617 | dev, freesize, |
4618 | verbose); | |
4619 | } | |
4620 | ||
8592f29d N |
4621 | if (!dev) { |
4622 | if (st->sb && freesize) { | |
efb30e7f DW |
4623 | /* we are being asked to automatically layout a |
4624 | * new volume based on the current contents of | |
4625 | * the container. If the the parameters can be | |
4626 | * satisfied reserve_space will record the disks, | |
4627 | * start offset, and size of the volume to be | |
4628 | * created. add_to_super and getinfo_super | |
4629 | * detect when autolayout is in progress. | |
4630 | */ | |
6592ce37 DW |
4631 | if (!validate_geometry_imsm_orom(st->sb, level, layout, |
4632 | raiddisks, chunk, | |
4633 | verbose)) | |
4634 | return 0; | |
c21e737b CA |
4635 | return reserve_space(st, raiddisks, size, |
4636 | chunk?*chunk:0, freesize); | |
8592f29d N |
4637 | } |
4638 | return 1; | |
4639 | } | |
bf5a934a DW |
4640 | if (st->sb) { |
4641 | /* creating in a given container */ | |
4642 | return validate_geometry_imsm_volume(st, level, layout, | |
4643 | raiddisks, chunk, size, | |
4644 | dev, freesize, verbose); | |
4645 | } | |
4646 | ||
bf5a934a DW |
4647 | /* This device needs to be a device in an 'imsm' container */ |
4648 | fd = open(dev, O_RDONLY|O_EXCL, 0); | |
4649 | if (fd >= 0) { | |
4650 | if (verbose) | |
4651 | fprintf(stderr, | |
4652 | Name ": Cannot create this array on device %s\n", | |
4653 | dev); | |
4654 | close(fd); | |
4655 | return 0; | |
4656 | } | |
4657 | if (errno != EBUSY || (fd = open(dev, O_RDONLY, 0)) < 0) { | |
4658 | if (verbose) | |
4659 | fprintf(stderr, Name ": Cannot open %s: %s\n", | |
4660 | dev, strerror(errno)); | |
4661 | return 0; | |
4662 | } | |
4663 | /* Well, it is in use by someone, maybe an 'imsm' container. */ | |
4664 | cfd = open_container(fd); | |
20cbe8d2 | 4665 | close(fd); |
bf5a934a | 4666 | if (cfd < 0) { |
bf5a934a DW |
4667 | if (verbose) |
4668 | fprintf(stderr, Name ": Cannot use %s: It is busy\n", | |
4669 | dev); | |
4670 | return 0; | |
4671 | } | |
4672 | sra = sysfs_read(cfd, 0, GET_VERSION); | |
bf5a934a | 4673 | if (sra && sra->array.major_version == -1 && |
20cbe8d2 AW |
4674 | strcmp(sra->text_version, "imsm") == 0) |
4675 | is_member = 1; | |
4676 | sysfs_free(sra); | |
4677 | if (is_member) { | |
bf5a934a DW |
4678 | /* This is a member of a imsm container. Load the container |
4679 | * and try to create a volume | |
4680 | */ | |
4681 | struct intel_super *super; | |
4682 | ||
e1902a7b | 4683 | if (load_super_imsm_all(st, cfd, (void **) &super, NULL) == 0) { |
bf5a934a DW |
4684 | st->sb = super; |
4685 | st->container_dev = fd2devnum(cfd); | |
4686 | close(cfd); | |
4687 | return validate_geometry_imsm_volume(st, level, layout, | |
4688 | raiddisks, chunk, | |
4689 | size, dev, | |
4690 | freesize, verbose); | |
4691 | } | |
20cbe8d2 | 4692 | } |
bf5a934a | 4693 | |
20cbe8d2 AW |
4694 | if (verbose) |
4695 | fprintf(stderr, Name ": failed container membership check\n"); | |
4696 | ||
4697 | close(cfd); | |
4698 | return 0; | |
bf5a934a | 4699 | } |
0bd16cf2 | 4700 | |
30f58b22 | 4701 | static void default_geometry_imsm(struct supertype *st, int *level, int *layout, int *chunk) |
0bd16cf2 DJ |
4702 | { |
4703 | struct intel_super *super = st->sb; | |
4704 | ||
30f58b22 DW |
4705 | if (level && *level == UnSet) |
4706 | *level = LEVEL_CONTAINER; | |
4707 | ||
4708 | if (level && layout && *layout == UnSet) | |
4709 | *layout = imsm_level_to_layout(*level); | |
0bd16cf2 | 4710 | |
1d54f286 N |
4711 | if (chunk && (*chunk == UnSet || *chunk == 0) && |
4712 | super && super->orom) | |
30f58b22 | 4713 | *chunk = imsm_orom_default_chunk(super->orom); |
0bd16cf2 DJ |
4714 | } |
4715 | ||
33414a01 DW |
4716 | static void handle_missing(struct intel_super *super, struct imsm_dev *dev); |
4717 | ||
4718 | static int kill_subarray_imsm(struct supertype *st) | |
4719 | { | |
4720 | /* remove the subarray currently referenced by ->current_vol */ | |
4721 | __u8 i; | |
4722 | struct intel_dev **dp; | |
4723 | struct intel_super *super = st->sb; | |
4724 | __u8 current_vol = super->current_vol; | |
4725 | struct imsm_super *mpb = super->anchor; | |
4726 | ||
4727 | if (super->current_vol < 0) | |
4728 | return 2; | |
4729 | super->current_vol = -1; /* invalidate subarray cursor */ | |
4730 | ||
4731 | /* block deletions that would change the uuid of active subarrays | |
4732 | * | |
4733 | * FIXME when immutable ids are available, but note that we'll | |
4734 | * also need to fixup the invalidated/active subarray indexes in | |
4735 | * mdstat | |
4736 | */ | |
4737 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
4738 | char subarray[4]; | |
4739 | ||
4740 | if (i < current_vol) | |
4741 | continue; | |
4742 | sprintf(subarray, "%u", i); | |
4743 | if (is_subarray_active(subarray, st->devname)) { | |
4744 | fprintf(stderr, | |
4745 | Name ": deleting subarray-%d would change the UUID of active subarray-%d, aborting\n", | |
4746 | current_vol, i); | |
4747 | ||
4748 | return 2; | |
4749 | } | |
4750 | } | |
4751 | ||
4752 | if (st->update_tail) { | |
4753 | struct imsm_update_kill_array *u = malloc(sizeof(*u)); | |
4754 | ||
4755 | if (!u) | |
4756 | return 2; | |
4757 | u->type = update_kill_array; | |
4758 | u->dev_idx = current_vol; | |
4759 | append_metadata_update(st, u, sizeof(*u)); | |
4760 | ||
4761 | return 0; | |
4762 | } | |
4763 | ||
4764 | for (dp = &super->devlist; *dp;) | |
4765 | if ((*dp)->index == current_vol) { | |
4766 | *dp = (*dp)->next; | |
4767 | } else { | |
4768 | handle_missing(super, (*dp)->dev); | |
4769 | if ((*dp)->index > current_vol) | |
4770 | (*dp)->index--; | |
4771 | dp = &(*dp)->next; | |
4772 | } | |
4773 | ||
4774 | /* no more raid devices, all active components are now spares, | |
4775 | * but of course failed are still failed | |
4776 | */ | |
4777 | if (--mpb->num_raid_devs == 0) { | |
4778 | struct dl *d; | |
4779 | ||
4780 | for (d = super->disks; d; d = d->next) | |
4781 | if (d->index > -2) { | |
4782 | d->index = -1; | |
4783 | d->disk.status = SPARE_DISK; | |
4784 | } | |
4785 | } | |
4786 | ||
4787 | super->updates_pending++; | |
4788 | ||
4789 | return 0; | |
4790 | } | |
aa534678 | 4791 | |
a951a4f7 | 4792 | static int update_subarray_imsm(struct supertype *st, char *subarray, |
fa56eddb | 4793 | char *update, struct mddev_ident *ident) |
aa534678 DW |
4794 | { |
4795 | /* update the subarray currently referenced by ->current_vol */ | |
4796 | struct intel_super *super = st->sb; | |
4797 | struct imsm_super *mpb = super->anchor; | |
4798 | ||
aa534678 DW |
4799 | if (strcmp(update, "name") == 0) { |
4800 | char *name = ident->name; | |
a951a4f7 N |
4801 | char *ep; |
4802 | int vol; | |
aa534678 | 4803 | |
a951a4f7 | 4804 | if (is_subarray_active(subarray, st->devname)) { |
aa534678 DW |
4805 | fprintf(stderr, |
4806 | Name ": Unable to update name of active subarray\n"); | |
4807 | return 2; | |
4808 | } | |
4809 | ||
4810 | if (!check_name(super, name, 0)) | |
4811 | return 2; | |
4812 | ||
a951a4f7 N |
4813 | vol = strtoul(subarray, &ep, 10); |
4814 | if (*ep != '\0' || vol >= super->anchor->num_raid_devs) | |
4815 | return 2; | |
4816 | ||
aa534678 DW |
4817 | if (st->update_tail) { |
4818 | struct imsm_update_rename_array *u = malloc(sizeof(*u)); | |
4819 | ||
4820 | if (!u) | |
4821 | return 2; | |
4822 | u->type = update_rename_array; | |
a951a4f7 | 4823 | u->dev_idx = vol; |
aa534678 DW |
4824 | snprintf((char *) u->name, MAX_RAID_SERIAL_LEN, "%s", name); |
4825 | append_metadata_update(st, u, sizeof(*u)); | |
4826 | } else { | |
4827 | struct imsm_dev *dev; | |
4828 | int i; | |
4829 | ||
a951a4f7 | 4830 | dev = get_imsm_dev(super, vol); |
aa534678 DW |
4831 | snprintf((char *) dev->volume, MAX_RAID_SERIAL_LEN, "%s", name); |
4832 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
4833 | dev = get_imsm_dev(super, i); | |
4834 | handle_missing(super, dev); | |
4835 | } | |
4836 | super->updates_pending++; | |
4837 | } | |
4838 | } else | |
4839 | return 2; | |
4840 | ||
4841 | return 0; | |
4842 | } | |
bf5a934a | 4843 | |
28bce06f AK |
4844 | static int is_gen_migration(struct imsm_dev *dev) |
4845 | { | |
4846 | if (!dev->vol.migr_state) | |
4847 | return 0; | |
4848 | ||
4849 | if (migr_type(dev) == MIGR_GEN_MIGR) | |
4850 | return 1; | |
4851 | ||
4852 | return 0; | |
4853 | } | |
71204a50 | 4854 | #endif /* MDASSEMBLE */ |
28bce06f | 4855 | |
1e5c6983 DW |
4856 | static int is_rebuilding(struct imsm_dev *dev) |
4857 | { | |
4858 | struct imsm_map *migr_map; | |
4859 | ||
4860 | if (!dev->vol.migr_state) | |
4861 | return 0; | |
4862 | ||
4863 | if (migr_type(dev) != MIGR_REBUILD) | |
4864 | return 0; | |
4865 | ||
4866 | migr_map = get_imsm_map(dev, 1); | |
4867 | ||
4868 | if (migr_map->map_state == IMSM_T_STATE_DEGRADED) | |
4869 | return 1; | |
4870 | else | |
4871 | return 0; | |
4872 | } | |
4873 | ||
4874 | static void update_recovery_start(struct imsm_dev *dev, struct mdinfo *array) | |
4875 | { | |
4876 | struct mdinfo *rebuild = NULL; | |
4877 | struct mdinfo *d; | |
4878 | __u32 units; | |
4879 | ||
4880 | if (!is_rebuilding(dev)) | |
4881 | return; | |
4882 | ||
4883 | /* Find the rebuild target, but punt on the dual rebuild case */ | |
4884 | for (d = array->devs; d; d = d->next) | |
4885 | if (d->recovery_start == 0) { | |
4886 | if (rebuild) | |
4887 | return; | |
4888 | rebuild = d; | |
4889 | } | |
4890 | ||
4363fd80 DW |
4891 | if (!rebuild) { |
4892 | /* (?) none of the disks are marked with | |
4893 | * IMSM_ORD_REBUILD, so assume they are missing and the | |
4894 | * disk_ord_tbl was not correctly updated | |
4895 | */ | |
4896 | dprintf("%s: failed to locate out-of-sync disk\n", __func__); | |
4897 | return; | |
4898 | } | |
4899 | ||
1e5c6983 DW |
4900 | units = __le32_to_cpu(dev->vol.curr_migr_unit); |
4901 | rebuild->recovery_start = units * blocks_per_migr_unit(dev); | |
4902 | } | |
4903 | ||
4904 | ||
00bbdbda | 4905 | static struct mdinfo *container_content_imsm(struct supertype *st, char *subarray) |
cdddbdbc | 4906 | { |
4f5bc454 DW |
4907 | /* Given a container loaded by load_super_imsm_all, |
4908 | * extract information about all the arrays into | |
4909 | * an mdinfo tree. | |
00bbdbda | 4910 | * If 'subarray' is given, just extract info about that array. |
4f5bc454 DW |
4911 | * |
4912 | * For each imsm_dev create an mdinfo, fill it in, | |
4913 | * then look for matching devices in super->disks | |
4914 | * and create appropriate device mdinfo. | |
4915 | */ | |
4916 | struct intel_super *super = st->sb; | |
949c47a0 | 4917 | struct imsm_super *mpb = super->anchor; |
4f5bc454 | 4918 | struct mdinfo *rest = NULL; |
00bbdbda | 4919 | unsigned int i; |
a06d022d | 4920 | int bbm_errors = 0; |
abef11a3 AK |
4921 | struct dl *d; |
4922 | int spare_disks = 0; | |
cdddbdbc | 4923 | |
a06d022d KW |
4924 | /* check for bad blocks */ |
4925 | if (imsm_bbm_log_size(super->anchor)) | |
4926 | bbm_errors = 1; | |
604b746f | 4927 | |
abef11a3 AK |
4928 | /* count spare devices, not used in maps |
4929 | */ | |
4930 | for (d = super->disks; d; d = d->next) | |
4931 | if (d->index == -1) | |
4932 | spare_disks++; | |
4933 | ||
4f5bc454 | 4934 | for (i = 0; i < mpb->num_raid_devs; i++) { |
00bbdbda N |
4935 | struct imsm_dev *dev; |
4936 | struct imsm_map *map; | |
86e3692b | 4937 | struct imsm_map *map2; |
4f5bc454 | 4938 | struct mdinfo *this; |
2db86302 | 4939 | int slot, chunk; |
00bbdbda N |
4940 | char *ep; |
4941 | ||
4942 | if (subarray && | |
4943 | (i != strtoul(subarray, &ep, 10) || *ep != '\0')) | |
4944 | continue; | |
4945 | ||
4946 | dev = get_imsm_dev(super, i); | |
4947 | map = get_imsm_map(dev, 0); | |
86e3692b | 4948 | map2 = get_imsm_map(dev, 1); |
4f5bc454 | 4949 | |
1ce0101c DW |
4950 | /* do not publish arrays that are in the middle of an |
4951 | * unsupported migration | |
4952 | */ | |
4953 | if (dev->vol.migr_state && | |
28bce06f | 4954 | (migr_type(dev) == MIGR_STATE_CHANGE)) { |
1ce0101c DW |
4955 | fprintf(stderr, Name ": cannot assemble volume '%.16s':" |
4956 | " unsupported migration in progress\n", | |
4957 | dev->volume); | |
4958 | continue; | |
4959 | } | |
2db86302 LM |
4960 | /* do not publish arrays that are not support by controller's |
4961 | * OROM/EFI | |
4962 | */ | |
1ce0101c | 4963 | |
2db86302 LM |
4964 | chunk = __le16_to_cpu(map->blocks_per_strip) >> 1; |
4965 | if (!validate_geometry_imsm_orom(super, | |
4966 | get_imsm_raid_level(map), /* RAID level */ | |
4967 | imsm_level_to_layout(get_imsm_raid_level(map)), | |
4968 | map->num_members, /* raid disks */ | |
4969 | &chunk, | |
4970 | 1 /* verbose */)) { | |
4971 | fprintf(stderr, Name ": RAID gemetry validation failed. " | |
4972 | "Cannot proceed with the action(s).\n"); | |
4973 | continue; | |
4974 | } | |
4f5bc454 | 4975 | this = malloc(sizeof(*this)); |
0fbd635c | 4976 | if (!this) { |
cf1be220 | 4977 | fprintf(stderr, Name ": failed to allocate %zu bytes\n", |
0fbd635c AW |
4978 | sizeof(*this)); |
4979 | break; | |
4980 | } | |
4f5bc454 DW |
4981 | memset(this, 0, sizeof(*this)); |
4982 | this->next = rest; | |
4f5bc454 | 4983 | |
301406c9 | 4984 | super->current_vol = i; |
a5d85af7 | 4985 | getinfo_super_imsm_volume(st, this, NULL); |
4f5bc454 | 4986 | for (slot = 0 ; slot < map->num_members; slot++) { |
1e5c6983 | 4987 | unsigned long long recovery_start; |
4f5bc454 DW |
4988 | struct mdinfo *info_d; |
4989 | struct dl *d; | |
4990 | int idx; | |
9a1608e5 | 4991 | int skip; |
7eef0453 | 4992 | __u32 ord; |
4f5bc454 | 4993 | |
9a1608e5 | 4994 | skip = 0; |
98130f40 | 4995 | idx = get_imsm_disk_idx(dev, slot, 0); |
196b0d44 | 4996 | ord = get_imsm_ord_tbl_ent(dev, slot, -1); |
4f5bc454 DW |
4997 | for (d = super->disks; d ; d = d->next) |
4998 | if (d->index == idx) | |
0fbd635c | 4999 | break; |
4f5bc454 | 5000 | |
1e5c6983 | 5001 | recovery_start = MaxSector; |
4f5bc454 | 5002 | if (d == NULL) |
9a1608e5 | 5003 | skip = 1; |
25ed7e59 | 5004 | if (d && is_failed(&d->disk)) |
9a1608e5 | 5005 | skip = 1; |
7eef0453 | 5006 | if (ord & IMSM_ORD_REBUILD) |
1e5c6983 | 5007 | recovery_start = 0; |
9a1608e5 DW |
5008 | |
5009 | /* | |
5010 | * if we skip some disks the array will be assmebled degraded; | |
1e5c6983 DW |
5011 | * reset resync start to avoid a dirty-degraded |
5012 | * situation when performing the intial sync | |
9a1608e5 DW |
5013 | * |
5014 | * FIXME handle dirty degraded | |
5015 | */ | |
1e5c6983 | 5016 | if ((skip || recovery_start == 0) && !dev->vol.dirty) |
b7528a20 | 5017 | this->resync_start = MaxSector; |
9a1608e5 DW |
5018 | if (skip) |
5019 | continue; | |
4f5bc454 | 5020 | |
1e5c6983 | 5021 | info_d = calloc(1, sizeof(*info_d)); |
9a1608e5 DW |
5022 | if (!info_d) { |
5023 | fprintf(stderr, Name ": failed to allocate disk" | |
1ce0101c | 5024 | " for volume %.16s\n", dev->volume); |
1e5c6983 DW |
5025 | info_d = this->devs; |
5026 | while (info_d) { | |
5027 | struct mdinfo *d = info_d->next; | |
5028 | ||
5029 | free(info_d); | |
5030 | info_d = d; | |
5031 | } | |
9a1608e5 DW |
5032 | free(this); |
5033 | this = rest; | |
5034 | break; | |
5035 | } | |
4f5bc454 DW |
5036 | info_d->next = this->devs; |
5037 | this->devs = info_d; | |
5038 | ||
4f5bc454 DW |
5039 | info_d->disk.number = d->index; |
5040 | info_d->disk.major = d->major; | |
5041 | info_d->disk.minor = d->minor; | |
5042 | info_d->disk.raid_disk = slot; | |
1e5c6983 | 5043 | info_d->recovery_start = recovery_start; |
86e3692b AK |
5044 | if (map2) { |
5045 | if (slot < map2->num_members) | |
5046 | info_d->disk.state = (1 << MD_DISK_ACTIVE); | |
04c3c514 AK |
5047 | else |
5048 | this->array.spare_disks++; | |
86e3692b AK |
5049 | } else { |
5050 | if (slot < map->num_members) | |
5051 | info_d->disk.state = (1 << MD_DISK_ACTIVE); | |
04c3c514 AK |
5052 | else |
5053 | this->array.spare_disks++; | |
86e3692b | 5054 | } |
1e5c6983 DW |
5055 | if (info_d->recovery_start == MaxSector) |
5056 | this->array.working_disks++; | |
4f5bc454 DW |
5057 | |
5058 | info_d->events = __le32_to_cpu(mpb->generation_num); | |
5059 | info_d->data_offset = __le32_to_cpu(map->pba_of_lba0); | |
5060 | info_d->component_size = __le32_to_cpu(map->blocks_per_member); | |
4f5bc454 | 5061 | } |
1e5c6983 DW |
5062 | /* now that the disk list is up-to-date fixup recovery_start */ |
5063 | update_recovery_start(dev, this); | |
abef11a3 | 5064 | this->array.spare_disks += spare_disks; |
9a1608e5 | 5065 | rest = this; |
4f5bc454 DW |
5066 | } |
5067 | ||
a06d022d KW |
5068 | /* if array has bad blocks, set suitable bit in array status */ |
5069 | if (bbm_errors) | |
5070 | rest->array.state |= (1<<MD_SB_BBM_ERRORS); | |
5071 | ||
4f5bc454 | 5072 | return rest; |
cdddbdbc DW |
5073 | } |
5074 | ||
845dea95 | 5075 | |
fb49eef2 | 5076 | static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev, int failed) |
c2a1e7da | 5077 | { |
a965f303 | 5078 | struct imsm_map *map = get_imsm_map(dev, 0); |
c2a1e7da DW |
5079 | |
5080 | if (!failed) | |
3393c6af DW |
5081 | return map->map_state == IMSM_T_STATE_UNINITIALIZED ? |
5082 | IMSM_T_STATE_UNINITIALIZED : IMSM_T_STATE_NORMAL; | |
c2a1e7da DW |
5083 | |
5084 | switch (get_imsm_raid_level(map)) { | |
5085 | case 0: | |
5086 | return IMSM_T_STATE_FAILED; | |
5087 | break; | |
5088 | case 1: | |
5089 | if (failed < map->num_members) | |
5090 | return IMSM_T_STATE_DEGRADED; | |
5091 | else | |
5092 | return IMSM_T_STATE_FAILED; | |
5093 | break; | |
5094 | case 10: | |
5095 | { | |
5096 | /** | |
c92a2527 DW |
5097 | * check to see if any mirrors have failed, otherwise we |
5098 | * are degraded. Even numbered slots are mirrored on | |
5099 | * slot+1 | |
c2a1e7da | 5100 | */ |
c2a1e7da | 5101 | int i; |
d9b420a5 N |
5102 | /* gcc -Os complains that this is unused */ |
5103 | int insync = insync; | |
c2a1e7da DW |
5104 | |
5105 | for (i = 0; i < map->num_members; i++) { | |
98130f40 | 5106 | __u32 ord = get_imsm_ord_tbl_ent(dev, i, -1); |
c92a2527 DW |
5107 | int idx = ord_to_idx(ord); |
5108 | struct imsm_disk *disk; | |
c2a1e7da | 5109 | |
c92a2527 DW |
5110 | /* reset the potential in-sync count on even-numbered |
5111 | * slots. num_copies is always 2 for imsm raid10 | |
5112 | */ | |
5113 | if ((i & 1) == 0) | |
5114 | insync = 2; | |
c2a1e7da | 5115 | |
c92a2527 | 5116 | disk = get_imsm_disk(super, idx); |
25ed7e59 | 5117 | if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD) |
c92a2527 | 5118 | insync--; |
c2a1e7da | 5119 | |
c92a2527 DW |
5120 | /* no in-sync disks left in this mirror the |
5121 | * array has failed | |
5122 | */ | |
5123 | if (insync == 0) | |
5124 | return IMSM_T_STATE_FAILED; | |
c2a1e7da DW |
5125 | } |
5126 | ||
5127 | return IMSM_T_STATE_DEGRADED; | |
5128 | } | |
5129 | case 5: | |
5130 | if (failed < 2) | |
5131 | return IMSM_T_STATE_DEGRADED; | |
5132 | else | |
5133 | return IMSM_T_STATE_FAILED; | |
5134 | break; | |
5135 | default: | |
5136 | break; | |
5137 | } | |
5138 | ||
5139 | return map->map_state; | |
5140 | } | |
5141 | ||
ff077194 | 5142 | static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev) |
c2a1e7da DW |
5143 | { |
5144 | int i; | |
5145 | int failed = 0; | |
5146 | struct imsm_disk *disk; | |
ff077194 | 5147 | struct imsm_map *map = get_imsm_map(dev, 0); |
0556e1a2 DW |
5148 | struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state); |
5149 | __u32 ord; | |
5150 | int idx; | |
c2a1e7da | 5151 | |
0556e1a2 DW |
5152 | /* at the beginning of migration we set IMSM_ORD_REBUILD on |
5153 | * disks that are being rebuilt. New failures are recorded to | |
5154 | * map[0]. So we look through all the disks we started with and | |
5155 | * see if any failures are still present, or if any new ones | |
5156 | * have arrived | |
5157 | * | |
5158 | * FIXME add support for online capacity expansion and | |
5159 | * raid-level-migration | |
5160 | */ | |
5161 | for (i = 0; i < prev->num_members; i++) { | |
5162 | ord = __le32_to_cpu(prev->disk_ord_tbl[i]); | |
5163 | ord |= __le32_to_cpu(map->disk_ord_tbl[i]); | |
5164 | idx = ord_to_idx(ord); | |
c2a1e7da | 5165 | |
949c47a0 | 5166 | disk = get_imsm_disk(super, idx); |
25ed7e59 | 5167 | if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD) |
fcb84475 | 5168 | failed++; |
c2a1e7da DW |
5169 | } |
5170 | ||
5171 | return failed; | |
845dea95 NB |
5172 | } |
5173 | ||
97b4d0e9 DW |
5174 | #ifndef MDASSEMBLE |
5175 | static int imsm_open_new(struct supertype *c, struct active_array *a, | |
5176 | char *inst) | |
5177 | { | |
5178 | struct intel_super *super = c->sb; | |
5179 | struct imsm_super *mpb = super->anchor; | |
5180 | ||
5181 | if (atoi(inst) >= mpb->num_raid_devs) { | |
5182 | fprintf(stderr, "%s: subarry index %d, out of range\n", | |
5183 | __func__, atoi(inst)); | |
5184 | return -ENODEV; | |
5185 | } | |
5186 | ||
5187 | dprintf("imsm: open_new %s\n", inst); | |
5188 | a->info.container_member = atoi(inst); | |
5189 | return 0; | |
5190 | } | |
5191 | ||
0c046afd DW |
5192 | static int is_resyncing(struct imsm_dev *dev) |
5193 | { | |
5194 | struct imsm_map *migr_map; | |
5195 | ||
5196 | if (!dev->vol.migr_state) | |
5197 | return 0; | |
5198 | ||
1484e727 DW |
5199 | if (migr_type(dev) == MIGR_INIT || |
5200 | migr_type(dev) == MIGR_REPAIR) | |
0c046afd DW |
5201 | return 1; |
5202 | ||
4c9bc37b AK |
5203 | if (migr_type(dev) == MIGR_GEN_MIGR) |
5204 | return 0; | |
5205 | ||
0c046afd DW |
5206 | migr_map = get_imsm_map(dev, 1); |
5207 | ||
4c9bc37b AK |
5208 | if ((migr_map->map_state == IMSM_T_STATE_NORMAL) && |
5209 | (dev->vol.migr_type != MIGR_GEN_MIGR)) | |
0c046afd DW |
5210 | return 1; |
5211 | else | |
5212 | return 0; | |
5213 | } | |
5214 | ||
0556e1a2 DW |
5215 | /* return true if we recorded new information */ |
5216 | static int mark_failure(struct imsm_dev *dev, struct imsm_disk *disk, int idx) | |
47ee5a45 | 5217 | { |
0556e1a2 DW |
5218 | __u32 ord; |
5219 | int slot; | |
5220 | struct imsm_map *map; | |
1a2487c2 KW |
5221 | char buf[MAX_RAID_SERIAL_LEN+3]; |
5222 | unsigned int len, shift = 0; | |
0556e1a2 DW |
5223 | |
5224 | /* new failures are always set in map[0] */ | |
5225 | map = get_imsm_map(dev, 0); | |
5226 | ||
5227 | slot = get_imsm_disk_slot(map, idx); | |
5228 | if (slot < 0) | |
5229 | return 0; | |
5230 | ||
5231 | ord = __le32_to_cpu(map->disk_ord_tbl[slot]); | |
25ed7e59 | 5232 | if (is_failed(disk) && (ord & IMSM_ORD_REBUILD)) |
0556e1a2 DW |
5233 | return 0; |
5234 | ||
1a2487c2 KW |
5235 | sprintf(buf, "%s:0", disk->serial); |
5236 | if ((len = strlen(buf)) >= MAX_RAID_SERIAL_LEN) | |
5237 | shift = len - MAX_RAID_SERIAL_LEN + 1; | |
5238 | strncpy((char *)disk->serial, &buf[shift], MAX_RAID_SERIAL_LEN); | |
5239 | ||
f2f27e63 | 5240 | disk->status |= FAILED_DISK; |
0556e1a2 | 5241 | set_imsm_ord_tbl_ent(map, slot, idx | IMSM_ORD_REBUILD); |
f21e18ca | 5242 | if (map->failed_disk_num == 0xff) |
0556e1a2 DW |
5243 | map->failed_disk_num = slot; |
5244 | return 1; | |
5245 | } | |
5246 | ||
5247 | static void mark_missing(struct imsm_dev *dev, struct imsm_disk *disk, int idx) | |
5248 | { | |
5249 | mark_failure(dev, disk, idx); | |
5250 | ||
5251 | if (disk->scsi_id == __cpu_to_le32(~(__u32)0)) | |
5252 | return; | |
5253 | ||
47ee5a45 DW |
5254 | disk->scsi_id = __cpu_to_le32(~(__u32)0); |
5255 | memmove(&disk->serial[0], &disk->serial[1], MAX_RAID_SERIAL_LEN - 1); | |
5256 | } | |
5257 | ||
33414a01 DW |
5258 | static void handle_missing(struct intel_super *super, struct imsm_dev *dev) |
5259 | { | |
5260 | __u8 map_state; | |
5261 | struct dl *dl; | |
5262 | int failed; | |
5263 | ||
5264 | if (!super->missing) | |
5265 | return; | |
5266 | failed = imsm_count_failed(super, dev); | |
5267 | map_state = imsm_check_degraded(super, dev, failed); | |
5268 | ||
5269 | dprintf("imsm: mark missing\n"); | |
5270 | end_migration(dev, map_state); | |
5271 | for (dl = super->missing; dl; dl = dl->next) | |
5272 | mark_missing(dev, &dl->disk, dl->index); | |
5273 | super->updates_pending++; | |
5274 | } | |
5275 | ||
70bdf0dc AK |
5276 | static unsigned long long imsm_set_array_size(struct imsm_dev *dev) |
5277 | { | |
5278 | int used_disks = imsm_num_data_members(dev, 0); | |
5279 | unsigned long long array_blocks; | |
5280 | struct imsm_map *map; | |
5281 | ||
5282 | if (used_disks == 0) { | |
5283 | /* when problems occures | |
5284 | * return current array_blocks value | |
5285 | */ | |
5286 | array_blocks = __le32_to_cpu(dev->size_high); | |
5287 | array_blocks = array_blocks << 32; | |
5288 | array_blocks += __le32_to_cpu(dev->size_low); | |
5289 | ||
5290 | return array_blocks; | |
5291 | } | |
5292 | ||
5293 | /* set array size in metadata | |
5294 | */ | |
5295 | map = get_imsm_map(dev, 0); | |
5296 | array_blocks = map->blocks_per_member * used_disks; | |
5297 | ||
5298 | /* round array size down to closest MB | |
5299 | */ | |
5300 | array_blocks = (array_blocks >> SECT_PER_MB_SHIFT) << SECT_PER_MB_SHIFT; | |
5301 | dev->size_low = __cpu_to_le32((__u32)array_blocks); | |
5302 | dev->size_high = __cpu_to_le32((__u32)(array_blocks >> 32)); | |
5303 | ||
5304 | return array_blocks; | |
5305 | } | |
5306 | ||
28bce06f AK |
5307 | static void imsm_set_disk(struct active_array *a, int n, int state); |
5308 | ||
0e2d1a4e AK |
5309 | static void imsm_progress_container_reshape(struct intel_super *super) |
5310 | { | |
5311 | /* if no device has a migr_state, but some device has a | |
5312 | * different number of members than the previous device, start | |
5313 | * changing the number of devices in this device to match | |
5314 | * previous. | |
5315 | */ | |
5316 | struct imsm_super *mpb = super->anchor; | |
5317 | int prev_disks = -1; | |
5318 | int i; | |
1dfaa380 | 5319 | int copy_map_size; |
0e2d1a4e AK |
5320 | |
5321 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
5322 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
5323 | struct imsm_map *map = get_imsm_map(dev, 0); | |
5324 | struct imsm_map *map2; | |
5325 | int prev_num_members; | |
0e2d1a4e AK |
5326 | |
5327 | if (dev->vol.migr_state) | |
5328 | return; | |
5329 | ||
5330 | if (prev_disks == -1) | |
5331 | prev_disks = map->num_members; | |
5332 | if (prev_disks == map->num_members) | |
5333 | continue; | |
5334 | ||
5335 | /* OK, this array needs to enter reshape mode. | |
5336 | * i.e it needs a migr_state | |
5337 | */ | |
5338 | ||
1dfaa380 | 5339 | copy_map_size = sizeof_imsm_map(map); |
0e2d1a4e AK |
5340 | prev_num_members = map->num_members; |
5341 | map->num_members = prev_disks; | |
5342 | dev->vol.migr_state = 1; | |
5343 | dev->vol.curr_migr_unit = 0; | |
5344 | dev->vol.migr_type = MIGR_GEN_MIGR; | |
5345 | for (i = prev_num_members; | |
5346 | i < map->num_members; i++) | |
5347 | set_imsm_ord_tbl_ent(map, i, i); | |
5348 | map2 = get_imsm_map(dev, 1); | |
5349 | /* Copy the current map */ | |
1dfaa380 | 5350 | memcpy(map2, map, copy_map_size); |
0e2d1a4e AK |
5351 | map2->num_members = prev_num_members; |
5352 | ||
70bdf0dc | 5353 | imsm_set_array_size(dev); |
0e2d1a4e AK |
5354 | super->updates_pending++; |
5355 | } | |
5356 | } | |
5357 | ||
aad6f216 | 5358 | /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild |
0c046afd DW |
5359 | * states are handled in imsm_set_disk() with one exception, when a |
5360 | * resync is stopped due to a new failure this routine will set the | |
5361 | * 'degraded' state for the array. | |
5362 | */ | |
01f157d7 | 5363 | static int imsm_set_array_state(struct active_array *a, int consistent) |
a862209d DW |
5364 | { |
5365 | int inst = a->info.container_member; | |
5366 | struct intel_super *super = a->container->sb; | |
949c47a0 | 5367 | struct imsm_dev *dev = get_imsm_dev(super, inst); |
a965f303 | 5368 | struct imsm_map *map = get_imsm_map(dev, 0); |
0c046afd DW |
5369 | int failed = imsm_count_failed(super, dev); |
5370 | __u8 map_state = imsm_check_degraded(super, dev, failed); | |
1e5c6983 | 5371 | __u32 blocks_per_unit; |
a862209d | 5372 | |
1af97990 AK |
5373 | if (dev->vol.migr_state && |
5374 | dev->vol.migr_type == MIGR_GEN_MIGR) { | |
5375 | /* array state change is blocked due to reshape action | |
aad6f216 N |
5376 | * We might need to |
5377 | * - abort the reshape (if last_checkpoint is 0 and action!= reshape) | |
5378 | * - finish the reshape (if last_checkpoint is big and action != reshape) | |
5379 | * - update curr_migr_unit | |
1af97990 | 5380 | */ |
aad6f216 N |
5381 | if (a->curr_action == reshape) { |
5382 | /* still reshaping, maybe update curr_migr_unit */ | |
633b5610 | 5383 | goto mark_checkpoint; |
aad6f216 N |
5384 | } else { |
5385 | if (a->last_checkpoint == 0 && a->prev_action == reshape) { | |
5386 | /* for some reason we aborted the reshape. | |
5387 | * Better clean up | |
5388 | */ | |
5389 | struct imsm_map *map2 = get_imsm_map(dev, 1); | |
5390 | dev->vol.migr_state = 0; | |
5391 | dev->vol.migr_type = 0; | |
5392 | dev->vol.curr_migr_unit = 0; | |
5393 | memcpy(map, map2, sizeof_imsm_map(map2)); | |
5394 | super->updates_pending++; | |
5395 | } | |
5396 | if (a->last_checkpoint >= a->info.component_size) { | |
5397 | unsigned long long array_blocks; | |
5398 | int used_disks; | |
e154ced3 | 5399 | struct mdinfo *mdi; |
aad6f216 | 5400 | |
9653001d | 5401 | used_disks = imsm_num_data_members(dev, 0); |
d55adef9 AK |
5402 | if (used_disks > 0) { |
5403 | array_blocks = | |
5404 | map->blocks_per_member * | |
5405 | used_disks; | |
5406 | /* round array size down to closest MB | |
5407 | */ | |
5408 | array_blocks = (array_blocks | |
5409 | >> SECT_PER_MB_SHIFT) | |
5410 | << SECT_PER_MB_SHIFT; | |
d55adef9 AK |
5411 | a->info.custom_array_size = array_blocks; |
5412 | /* encourage manager to update array | |
5413 | * size | |
5414 | */ | |
e154ced3 | 5415 | |
d55adef9 | 5416 | a->check_reshape = 1; |
633b5610 | 5417 | } |
e154ced3 AK |
5418 | /* finalize online capacity expansion/reshape */ |
5419 | for (mdi = a->info.devs; mdi; mdi = mdi->next) | |
5420 | imsm_set_disk(a, | |
5421 | mdi->disk.raid_disk, | |
5422 | mdi->curr_state); | |
5423 | ||
0e2d1a4e | 5424 | imsm_progress_container_reshape(super); |
e154ced3 | 5425 | } |
aad6f216 | 5426 | } |
1af97990 AK |
5427 | } |
5428 | ||
47ee5a45 | 5429 | /* before we activate this array handle any missing disks */ |
33414a01 DW |
5430 | if (consistent == 2) |
5431 | handle_missing(super, dev); | |
1e5c6983 | 5432 | |
0c046afd | 5433 | if (consistent == 2 && |
b7941fd6 | 5434 | (!is_resync_complete(&a->info) || |
0c046afd DW |
5435 | map_state != IMSM_T_STATE_NORMAL || |
5436 | dev->vol.migr_state)) | |
01f157d7 | 5437 | consistent = 0; |
272906ef | 5438 | |
b7941fd6 | 5439 | if (is_resync_complete(&a->info)) { |
0c046afd | 5440 | /* complete intialization / resync, |
0556e1a2 DW |
5441 | * recovery and interrupted recovery is completed in |
5442 | * ->set_disk | |
0c046afd DW |
5443 | */ |
5444 | if (is_resyncing(dev)) { | |
5445 | dprintf("imsm: mark resync done\n"); | |
f8f603f1 | 5446 | end_migration(dev, map_state); |
115c3803 | 5447 | super->updates_pending++; |
484240d8 | 5448 | a->last_checkpoint = 0; |
115c3803 | 5449 | } |
0c046afd DW |
5450 | } else if (!is_resyncing(dev) && !failed) { |
5451 | /* mark the start of the init process if nothing is failed */ | |
b7941fd6 | 5452 | dprintf("imsm: mark resync start\n"); |
1484e727 | 5453 | if (map->map_state == IMSM_T_STATE_UNINITIALIZED) |
e3bba0e0 | 5454 | migrate(dev, IMSM_T_STATE_NORMAL, MIGR_INIT); |
1484e727 DW |
5455 | else |
5456 | migrate(dev, IMSM_T_STATE_NORMAL, MIGR_REPAIR); | |
3393c6af | 5457 | super->updates_pending++; |
115c3803 | 5458 | } |
a862209d | 5459 | |
633b5610 | 5460 | mark_checkpoint: |
1e5c6983 DW |
5461 | /* check if we can update curr_migr_unit from resync_start, recovery_start */ |
5462 | blocks_per_unit = blocks_per_migr_unit(dev); | |
4f0a7acc | 5463 | if (blocks_per_unit) { |
1e5c6983 DW |
5464 | __u32 units32; |
5465 | __u64 units; | |
5466 | ||
4f0a7acc | 5467 | units = a->last_checkpoint / blocks_per_unit; |
1e5c6983 DW |
5468 | units32 = units; |
5469 | ||
5470 | /* check that we did not overflow 32-bits, and that | |
5471 | * curr_migr_unit needs updating | |
5472 | */ | |
5473 | if (units32 == units && | |
5474 | __le32_to_cpu(dev->vol.curr_migr_unit) != units32) { | |
5475 | dprintf("imsm: mark checkpoint (%u)\n", units32); | |
5476 | dev->vol.curr_migr_unit = __cpu_to_le32(units32); | |
5477 | super->updates_pending++; | |
5478 | } | |
5479 | } | |
f8f603f1 | 5480 | |
3393c6af | 5481 | /* mark dirty / clean */ |
0c046afd | 5482 | if (dev->vol.dirty != !consistent) { |
b7941fd6 | 5483 | dprintf("imsm: mark '%s'\n", consistent ? "clean" : "dirty"); |
0c046afd DW |
5484 | if (consistent) |
5485 | dev->vol.dirty = 0; | |
5486 | else | |
5487 | dev->vol.dirty = 1; | |
a862209d DW |
5488 | super->updates_pending++; |
5489 | } | |
28bce06f | 5490 | |
01f157d7 | 5491 | return consistent; |
a862209d DW |
5492 | } |
5493 | ||
8d45d196 | 5494 | static void imsm_set_disk(struct active_array *a, int n, int state) |
845dea95 | 5495 | { |
8d45d196 DW |
5496 | int inst = a->info.container_member; |
5497 | struct intel_super *super = a->container->sb; | |
949c47a0 | 5498 | struct imsm_dev *dev = get_imsm_dev(super, inst); |
a965f303 | 5499 | struct imsm_map *map = get_imsm_map(dev, 0); |
8d45d196 | 5500 | struct imsm_disk *disk; |
0c046afd | 5501 | int failed; |
b10b37b8 | 5502 | __u32 ord; |
0c046afd | 5503 | __u8 map_state; |
8d45d196 DW |
5504 | |
5505 | if (n > map->num_members) | |
5506 | fprintf(stderr, "imsm: set_disk %d out of range 0..%d\n", | |
5507 | n, map->num_members - 1); | |
5508 | ||
5509 | if (n < 0) | |
5510 | return; | |
5511 | ||
4e6e574a | 5512 | dprintf("imsm: set_disk %d:%x\n", n, state); |
8d45d196 | 5513 | |
98130f40 | 5514 | ord = get_imsm_ord_tbl_ent(dev, n, -1); |
b10b37b8 | 5515 | disk = get_imsm_disk(super, ord_to_idx(ord)); |
8d45d196 | 5516 | |
5802a811 | 5517 | /* check for new failures */ |
0556e1a2 DW |
5518 | if (state & DS_FAULTY) { |
5519 | if (mark_failure(dev, disk, ord_to_idx(ord))) | |
5520 | super->updates_pending++; | |
8d45d196 | 5521 | } |
47ee5a45 | 5522 | |
19859edc | 5523 | /* check if in_sync */ |
0556e1a2 | 5524 | if (state & DS_INSYNC && ord & IMSM_ORD_REBUILD && is_rebuilding(dev)) { |
b10b37b8 DW |
5525 | struct imsm_map *migr_map = get_imsm_map(dev, 1); |
5526 | ||
5527 | set_imsm_ord_tbl_ent(migr_map, n, ord_to_idx(ord)); | |
19859edc DW |
5528 | super->updates_pending++; |
5529 | } | |
8d45d196 | 5530 | |
0c046afd DW |
5531 | failed = imsm_count_failed(super, dev); |
5532 | map_state = imsm_check_degraded(super, dev, failed); | |
5802a811 | 5533 | |
0c046afd DW |
5534 | /* check if recovery complete, newly degraded, or failed */ |
5535 | if (map_state == IMSM_T_STATE_NORMAL && is_rebuilding(dev)) { | |
f8f603f1 | 5536 | end_migration(dev, map_state); |
0556e1a2 DW |
5537 | map = get_imsm_map(dev, 0); |
5538 | map->failed_disk_num = ~0; | |
0c046afd | 5539 | super->updates_pending++; |
484240d8 | 5540 | a->last_checkpoint = 0; |
0c046afd DW |
5541 | } else if (map_state == IMSM_T_STATE_DEGRADED && |
5542 | map->map_state != map_state && | |
5543 | !dev->vol.migr_state) { | |
5544 | dprintf("imsm: mark degraded\n"); | |
5545 | map->map_state = map_state; | |
5546 | super->updates_pending++; | |
484240d8 | 5547 | a->last_checkpoint = 0; |
0c046afd DW |
5548 | } else if (map_state == IMSM_T_STATE_FAILED && |
5549 | map->map_state != map_state) { | |
5550 | dprintf("imsm: mark failed\n"); | |
f8f603f1 | 5551 | end_migration(dev, map_state); |
0c046afd | 5552 | super->updates_pending++; |
484240d8 | 5553 | a->last_checkpoint = 0; |
28bce06f AK |
5554 | } else if (is_gen_migration(dev)) { |
5555 | dprintf("imsm: Detected General Migration in state: "); | |
5556 | if (map_state == IMSM_T_STATE_NORMAL) { | |
5557 | end_migration(dev, map_state); | |
5558 | map = get_imsm_map(dev, 0); | |
5559 | map->failed_disk_num = ~0; | |
5560 | dprintf("normal\n"); | |
5561 | } else { | |
5562 | if (map_state == IMSM_T_STATE_DEGRADED) { | |
5563 | printf("degraded\n"); | |
5564 | end_migration(dev, map_state); | |
5565 | } else { | |
5566 | dprintf("failed\n"); | |
5567 | } | |
5568 | map->map_state = map_state; | |
5569 | } | |
5570 | super->updates_pending++; | |
5802a811 | 5571 | } |
845dea95 NB |
5572 | } |
5573 | ||
f796af5d | 5574 | static int store_imsm_mpb(int fd, struct imsm_super *mpb) |
c2a1e7da | 5575 | { |
f796af5d | 5576 | void *buf = mpb; |
c2a1e7da DW |
5577 | __u32 mpb_size = __le32_to_cpu(mpb->mpb_size); |
5578 | unsigned long long dsize; | |
5579 | unsigned long long sectors; | |
5580 | ||
5581 | get_dev_size(fd, NULL, &dsize); | |
5582 | ||
272f648f DW |
5583 | if (mpb_size > 512) { |
5584 | /* -1 to account for anchor */ | |
5585 | sectors = mpb_sectors(mpb) - 1; | |
c2a1e7da | 5586 | |
272f648f DW |
5587 | /* write the extended mpb to the sectors preceeding the anchor */ |
5588 | if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0) | |
5589 | return 1; | |
c2a1e7da | 5590 | |
f21e18ca N |
5591 | if ((unsigned long long)write(fd, buf + 512, 512 * sectors) |
5592 | != 512 * sectors) | |
272f648f DW |
5593 | return 1; |
5594 | } | |
c2a1e7da | 5595 | |
272f648f DW |
5596 | /* first block is stored on second to last sector of the disk */ |
5597 | if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0) | |
c2a1e7da DW |
5598 | return 1; |
5599 | ||
f796af5d | 5600 | if (write(fd, buf, 512) != 512) |
c2a1e7da DW |
5601 | return 1; |
5602 | ||
c2a1e7da DW |
5603 | return 0; |
5604 | } | |
5605 | ||
2e735d19 | 5606 | static void imsm_sync_metadata(struct supertype *container) |
845dea95 | 5607 | { |
2e735d19 | 5608 | struct intel_super *super = container->sb; |
c2a1e7da | 5609 | |
1a64be56 | 5610 | dprintf("sync metadata: %d\n", super->updates_pending); |
c2a1e7da DW |
5611 | if (!super->updates_pending) |
5612 | return; | |
5613 | ||
36988a3d | 5614 | write_super_imsm(container, 0); |
c2a1e7da DW |
5615 | |
5616 | super->updates_pending = 0; | |
845dea95 NB |
5617 | } |
5618 | ||
272906ef DW |
5619 | static struct dl *imsm_readd(struct intel_super *super, int idx, struct active_array *a) |
5620 | { | |
5621 | struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member); | |
98130f40 | 5622 | int i = get_imsm_disk_idx(dev, idx, -1); |
272906ef DW |
5623 | struct dl *dl; |
5624 | ||
5625 | for (dl = super->disks; dl; dl = dl->next) | |
5626 | if (dl->index == i) | |
5627 | break; | |
5628 | ||
25ed7e59 | 5629 | if (dl && is_failed(&dl->disk)) |
272906ef DW |
5630 | dl = NULL; |
5631 | ||
5632 | if (dl) | |
5633 | dprintf("%s: found %x:%x\n", __func__, dl->major, dl->minor); | |
5634 | ||
5635 | return dl; | |
5636 | } | |
5637 | ||
a20d2ba5 | 5638 | static struct dl *imsm_add_spare(struct intel_super *super, int slot, |
8ba77d32 AK |
5639 | struct active_array *a, int activate_new, |
5640 | struct mdinfo *additional_test_list) | |
272906ef DW |
5641 | { |
5642 | struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member); | |
98130f40 | 5643 | int idx = get_imsm_disk_idx(dev, slot, -1); |
a20d2ba5 DW |
5644 | struct imsm_super *mpb = super->anchor; |
5645 | struct imsm_map *map; | |
272906ef DW |
5646 | unsigned long long pos; |
5647 | struct mdinfo *d; | |
5648 | struct extent *ex; | |
a20d2ba5 | 5649 | int i, j; |
272906ef | 5650 | int found; |
569cc43f DW |
5651 | __u32 array_start = 0; |
5652 | __u32 array_end = 0; | |
272906ef | 5653 | struct dl *dl; |
6c932028 | 5654 | struct mdinfo *test_list; |
272906ef DW |
5655 | |
5656 | for (dl = super->disks; dl; dl = dl->next) { | |
5657 | /* If in this array, skip */ | |
5658 | for (d = a->info.devs ; d ; d = d->next) | |
e553d2a4 DW |
5659 | if (d->state_fd >= 0 && |
5660 | d->disk.major == dl->major && | |
272906ef | 5661 | d->disk.minor == dl->minor) { |
8ba77d32 AK |
5662 | dprintf("%x:%x already in array\n", |
5663 | dl->major, dl->minor); | |
272906ef DW |
5664 | break; |
5665 | } | |
5666 | if (d) | |
5667 | continue; | |
6c932028 AK |
5668 | test_list = additional_test_list; |
5669 | while (test_list) { | |
5670 | if (test_list->disk.major == dl->major && | |
5671 | test_list->disk.minor == dl->minor) { | |
8ba77d32 AK |
5672 | dprintf("%x:%x already in additional test list\n", |
5673 | dl->major, dl->minor); | |
5674 | break; | |
5675 | } | |
6c932028 | 5676 | test_list = test_list->next; |
8ba77d32 | 5677 | } |
6c932028 | 5678 | if (test_list) |
8ba77d32 | 5679 | continue; |
272906ef | 5680 | |
e553d2a4 | 5681 | /* skip in use or failed drives */ |
25ed7e59 | 5682 | if (is_failed(&dl->disk) || idx == dl->index || |
df474657 DW |
5683 | dl->index == -2) { |
5684 | dprintf("%x:%x status (failed: %d index: %d)\n", | |
25ed7e59 | 5685 | dl->major, dl->minor, is_failed(&dl->disk), idx); |
9a1608e5 DW |
5686 | continue; |
5687 | } | |
5688 | ||
a20d2ba5 DW |
5689 | /* skip pure spares when we are looking for partially |
5690 | * assimilated drives | |
5691 | */ | |
5692 | if (dl->index == -1 && !activate_new) | |
5693 | continue; | |
5694 | ||
272906ef | 5695 | /* Does this unused device have the requisite free space? |
a20d2ba5 | 5696 | * It needs to be able to cover all member volumes |
272906ef DW |
5697 | */ |
5698 | ex = get_extents(super, dl); | |
5699 | if (!ex) { | |
5700 | dprintf("cannot get extents\n"); | |
5701 | continue; | |
5702 | } | |
a20d2ba5 DW |
5703 | for (i = 0; i < mpb->num_raid_devs; i++) { |
5704 | dev = get_imsm_dev(super, i); | |
5705 | map = get_imsm_map(dev, 0); | |
272906ef | 5706 | |
a20d2ba5 DW |
5707 | /* check if this disk is already a member of |
5708 | * this array | |
272906ef | 5709 | */ |
620b1713 | 5710 | if (get_imsm_disk_slot(map, dl->index) >= 0) |
a20d2ba5 DW |
5711 | continue; |
5712 | ||
5713 | found = 0; | |
5714 | j = 0; | |
5715 | pos = 0; | |
5716 | array_start = __le32_to_cpu(map->pba_of_lba0); | |
329c8278 DW |
5717 | array_end = array_start + |
5718 | __le32_to_cpu(map->blocks_per_member) - 1; | |
a20d2ba5 DW |
5719 | |
5720 | do { | |
5721 | /* check that we can start at pba_of_lba0 with | |
5722 | * blocks_per_member of space | |
5723 | */ | |
329c8278 | 5724 | if (array_start >= pos && array_end < ex[j].start) { |
a20d2ba5 DW |
5725 | found = 1; |
5726 | break; | |
5727 | } | |
5728 | pos = ex[j].start + ex[j].size; | |
5729 | j++; | |
5730 | } while (ex[j-1].size); | |
5731 | ||
5732 | if (!found) | |
272906ef | 5733 | break; |
a20d2ba5 | 5734 | } |
272906ef DW |
5735 | |
5736 | free(ex); | |
a20d2ba5 | 5737 | if (i < mpb->num_raid_devs) { |
329c8278 DW |
5738 | dprintf("%x:%x does not have %u to %u available\n", |
5739 | dl->major, dl->minor, array_start, array_end); | |
272906ef DW |
5740 | /* No room */ |
5741 | continue; | |
a20d2ba5 DW |
5742 | } |
5743 | return dl; | |
272906ef DW |
5744 | } |
5745 | ||
5746 | return dl; | |
5747 | } | |
5748 | ||
95d07a2c LM |
5749 | |
5750 | static int imsm_rebuild_allowed(struct supertype *cont, int dev_idx, int failed) | |
5751 | { | |
5752 | struct imsm_dev *dev2; | |
5753 | struct imsm_map *map; | |
5754 | struct dl *idisk; | |
5755 | int slot; | |
5756 | int idx; | |
5757 | __u8 state; | |
5758 | ||
5759 | dev2 = get_imsm_dev(cont->sb, dev_idx); | |
5760 | if (dev2) { | |
5761 | state = imsm_check_degraded(cont->sb, dev2, failed); | |
5762 | if (state == IMSM_T_STATE_FAILED) { | |
5763 | map = get_imsm_map(dev2, 0); | |
5764 | if (!map) | |
5765 | return 1; | |
5766 | for (slot = 0; slot < map->num_members; slot++) { | |
5767 | /* | |
5768 | * Check if failed disks are deleted from intel | |
5769 | * disk list or are marked to be deleted | |
5770 | */ | |
98130f40 | 5771 | idx = get_imsm_disk_idx(dev2, slot, -1); |
95d07a2c LM |
5772 | idisk = get_imsm_dl_disk(cont->sb, idx); |
5773 | /* | |
5774 | * Do not rebuild the array if failed disks | |
5775 | * from failed sub-array are not removed from | |
5776 | * container. | |
5777 | */ | |
5778 | if (idisk && | |
5779 | is_failed(&idisk->disk) && | |
5780 | (idisk->action != DISK_REMOVE)) | |
5781 | return 0; | |
5782 | } | |
5783 | } | |
5784 | } | |
5785 | return 1; | |
5786 | } | |
5787 | ||
88758e9d DW |
5788 | static struct mdinfo *imsm_activate_spare(struct active_array *a, |
5789 | struct metadata_update **updates) | |
5790 | { | |
5791 | /** | |
d23fe947 DW |
5792 | * Find a device with unused free space and use it to replace a |
5793 | * failed/vacant region in an array. We replace failed regions one a | |
5794 | * array at a time. The result is that a new spare disk will be added | |
5795 | * to the first failed array and after the monitor has finished | |
5796 | * propagating failures the remainder will be consumed. | |
88758e9d | 5797 | * |
d23fe947 DW |
5798 | * FIXME add a capability for mdmon to request spares from another |
5799 | * container. | |
88758e9d DW |
5800 | */ |
5801 | ||
5802 | struct intel_super *super = a->container->sb; | |
88758e9d | 5803 | int inst = a->info.container_member; |
949c47a0 | 5804 | struct imsm_dev *dev = get_imsm_dev(super, inst); |
a965f303 | 5805 | struct imsm_map *map = get_imsm_map(dev, 0); |
88758e9d DW |
5806 | int failed = a->info.array.raid_disks; |
5807 | struct mdinfo *rv = NULL; | |
5808 | struct mdinfo *d; | |
5809 | struct mdinfo *di; | |
5810 | struct metadata_update *mu; | |
5811 | struct dl *dl; | |
5812 | struct imsm_update_activate_spare *u; | |
5813 | int num_spares = 0; | |
5814 | int i; | |
95d07a2c | 5815 | int allowed; |
88758e9d DW |
5816 | |
5817 | for (d = a->info.devs ; d ; d = d->next) { | |
5818 | if ((d->curr_state & DS_FAULTY) && | |
5819 | d->state_fd >= 0) | |
5820 | /* wait for Removal to happen */ | |
5821 | return NULL; | |
5822 | if (d->state_fd >= 0) | |
5823 | failed--; | |
5824 | } | |
5825 | ||
5826 | dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n", | |
5827 | inst, failed, a->info.array.raid_disks, a->info.array.level); | |
1af97990 AK |
5828 | |
5829 | if (dev->vol.migr_state && | |
5830 | dev->vol.migr_type == MIGR_GEN_MIGR) | |
5831 | /* No repair during migration */ | |
5832 | return NULL; | |
5833 | ||
89c67882 AK |
5834 | if (a->info.array.level == 4) |
5835 | /* No repair for takeovered array | |
5836 | * imsm doesn't support raid4 | |
5837 | */ | |
5838 | return NULL; | |
5839 | ||
fb49eef2 | 5840 | if (imsm_check_degraded(super, dev, failed) != IMSM_T_STATE_DEGRADED) |
88758e9d DW |
5841 | return NULL; |
5842 | ||
95d07a2c LM |
5843 | /* |
5844 | * If there are any failed disks check state of the other volume. | |
5845 | * Block rebuild if the another one is failed until failed disks | |
5846 | * are removed from container. | |
5847 | */ | |
5848 | if (failed) { | |
5849 | dprintf("found failed disks in %s, check if there another" | |
5850 | "failed sub-array.\n", | |
5851 | dev->volume); | |
5852 | /* check if states of the other volumes allow for rebuild */ | |
5853 | for (i = 0; i < super->anchor->num_raid_devs; i++) { | |
5854 | if (i != inst) { | |
5855 | allowed = imsm_rebuild_allowed(a->container, | |
5856 | i, failed); | |
5857 | if (!allowed) | |
5858 | return NULL; | |
5859 | } | |
5860 | } | |
5861 | } | |
5862 | ||
88758e9d | 5863 | /* For each slot, if it is not working, find a spare */ |
88758e9d DW |
5864 | for (i = 0; i < a->info.array.raid_disks; i++) { |
5865 | for (d = a->info.devs ; d ; d = d->next) | |
5866 | if (d->disk.raid_disk == i) | |
5867 | break; | |
5868 | dprintf("found %d: %p %x\n", i, d, d?d->curr_state:0); | |
5869 | if (d && (d->state_fd >= 0)) | |
5870 | continue; | |
5871 | ||
272906ef | 5872 | /* |
a20d2ba5 DW |
5873 | * OK, this device needs recovery. Try to re-add the |
5874 | * previous occupant of this slot, if this fails see if | |
5875 | * we can continue the assimilation of a spare that was | |
5876 | * partially assimilated, finally try to activate a new | |
5877 | * spare. | |
272906ef DW |
5878 | */ |
5879 | dl = imsm_readd(super, i, a); | |
5880 | if (!dl) | |
8ba77d32 | 5881 | dl = imsm_add_spare(super, i, a, 0, NULL); |
a20d2ba5 | 5882 | if (!dl) |
8ba77d32 | 5883 | dl = imsm_add_spare(super, i, a, 1, NULL); |
272906ef DW |
5884 | if (!dl) |
5885 | continue; | |
5886 | ||
5887 | /* found a usable disk with enough space */ | |
5888 | di = malloc(sizeof(*di)); | |
79244939 DW |
5889 | if (!di) |
5890 | continue; | |
272906ef DW |
5891 | memset(di, 0, sizeof(*di)); |
5892 | ||
5893 | /* dl->index will be -1 in the case we are activating a | |
5894 | * pristine spare. imsm_process_update() will create a | |
5895 | * new index in this case. Once a disk is found to be | |
5896 | * failed in all member arrays it is kicked from the | |
5897 | * metadata | |
5898 | */ | |
5899 | di->disk.number = dl->index; | |
d23fe947 | 5900 | |
272906ef DW |
5901 | /* (ab)use di->devs to store a pointer to the device |
5902 | * we chose | |
5903 | */ | |
5904 | di->devs = (struct mdinfo *) dl; | |
5905 | ||
5906 | di->disk.raid_disk = i; | |
5907 | di->disk.major = dl->major; | |
5908 | di->disk.minor = dl->minor; | |
5909 | di->disk.state = 0; | |
d23534e4 | 5910 | di->recovery_start = 0; |
272906ef DW |
5911 | di->data_offset = __le32_to_cpu(map->pba_of_lba0); |
5912 | di->component_size = a->info.component_size; | |
5913 | di->container_member = inst; | |
148acb7b | 5914 | super->random = random32(); |
272906ef DW |
5915 | di->next = rv; |
5916 | rv = di; | |
5917 | num_spares++; | |
5918 | dprintf("%x:%x to be %d at %llu\n", dl->major, dl->minor, | |
5919 | i, di->data_offset); | |
88758e9d | 5920 | |
272906ef | 5921 | break; |
88758e9d DW |
5922 | } |
5923 | ||
5924 | if (!rv) | |
5925 | /* No spares found */ | |
5926 | return rv; | |
5927 | /* Now 'rv' has a list of devices to return. | |
5928 | * Create a metadata_update record to update the | |
5929 | * disk_ord_tbl for the array | |
5930 | */ | |
5931 | mu = malloc(sizeof(*mu)); | |
79244939 DW |
5932 | if (mu) { |
5933 | mu->buf = malloc(sizeof(struct imsm_update_activate_spare) * num_spares); | |
5934 | if (mu->buf == NULL) { | |
5935 | free(mu); | |
5936 | mu = NULL; | |
5937 | } | |
5938 | } | |
5939 | if (!mu) { | |
5940 | while (rv) { | |
5941 | struct mdinfo *n = rv->next; | |
5942 | ||
5943 | free(rv); | |
5944 | rv = n; | |
5945 | } | |
5946 | return NULL; | |
5947 | } | |
5948 | ||
88758e9d | 5949 | mu->space = NULL; |
cb23f1f4 | 5950 | mu->space_list = NULL; |
88758e9d DW |
5951 | mu->len = sizeof(struct imsm_update_activate_spare) * num_spares; |
5952 | mu->next = *updates; | |
5953 | u = (struct imsm_update_activate_spare *) mu->buf; | |
5954 | ||
5955 | for (di = rv ; di ; di = di->next) { | |
5956 | u->type = update_activate_spare; | |
d23fe947 DW |
5957 | u->dl = (struct dl *) di->devs; |
5958 | di->devs = NULL; | |
88758e9d DW |
5959 | u->slot = di->disk.raid_disk; |
5960 | u->array = inst; | |
5961 | u->next = u + 1; | |
5962 | u++; | |
5963 | } | |
5964 | (u-1)->next = NULL; | |
5965 | *updates = mu; | |
5966 | ||
5967 | return rv; | |
5968 | } | |
5969 | ||
54c2c1ea | 5970 | static int disks_overlap(struct intel_super *super, int idx, struct imsm_update_create_array *u) |
8273f55e | 5971 | { |
54c2c1ea DW |
5972 | struct imsm_dev *dev = get_imsm_dev(super, idx); |
5973 | struct imsm_map *map = get_imsm_map(dev, 0); | |
5974 | struct imsm_map *new_map = get_imsm_map(&u->dev, 0); | |
5975 | struct disk_info *inf = get_disk_info(u); | |
5976 | struct imsm_disk *disk; | |
8273f55e DW |
5977 | int i; |
5978 | int j; | |
8273f55e | 5979 | |
54c2c1ea | 5980 | for (i = 0; i < map->num_members; i++) { |
98130f40 | 5981 | disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i, -1)); |
54c2c1ea DW |
5982 | for (j = 0; j < new_map->num_members; j++) |
5983 | if (serialcmp(disk->serial, inf[j].serial) == 0) | |
8273f55e DW |
5984 | return 1; |
5985 | } | |
5986 | ||
5987 | return 0; | |
5988 | } | |
5989 | ||
1a64be56 LM |
5990 | |
5991 | static struct dl *get_disk_super(struct intel_super *super, int major, int minor) | |
5992 | { | |
5993 | struct dl *dl = NULL; | |
5994 | for (dl = super->disks; dl; dl = dl->next) | |
5995 | if ((dl->major == major) && (dl->minor == minor)) | |
5996 | return dl; | |
5997 | return NULL; | |
5998 | } | |
5999 | ||
6000 | static int remove_disk_super(struct intel_super *super, int major, int minor) | |
6001 | { | |
6002 | struct dl *prev = NULL; | |
6003 | struct dl *dl; | |
6004 | ||
6005 | prev = NULL; | |
6006 | for (dl = super->disks; dl; dl = dl->next) { | |
6007 | if ((dl->major == major) && (dl->minor == minor)) { | |
6008 | /* remove */ | |
6009 | if (prev) | |
6010 | prev->next = dl->next; | |
6011 | else | |
6012 | super->disks = dl->next; | |
6013 | dl->next = NULL; | |
6014 | __free_imsm_disk(dl); | |
6015 | dprintf("%s: removed %x:%x\n", | |
6016 | __func__, major, minor); | |
6017 | break; | |
6018 | } | |
6019 | prev = dl; | |
6020 | } | |
6021 | return 0; | |
6022 | } | |
6023 | ||
f21e18ca | 6024 | static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index); |
ae6aad82 | 6025 | |
1a64be56 LM |
6026 | static int add_remove_disk_update(struct intel_super *super) |
6027 | { | |
6028 | int check_degraded = 0; | |
6029 | struct dl *disk = NULL; | |
6030 | /* add/remove some spares to/from the metadata/contrainer */ | |
6031 | while (super->disk_mgmt_list) { | |
6032 | struct dl *disk_cfg; | |
6033 | ||
6034 | disk_cfg = super->disk_mgmt_list; | |
6035 | super->disk_mgmt_list = disk_cfg->next; | |
6036 | disk_cfg->next = NULL; | |
6037 | ||
6038 | if (disk_cfg->action == DISK_ADD) { | |
6039 | disk_cfg->next = super->disks; | |
6040 | super->disks = disk_cfg; | |
6041 | check_degraded = 1; | |
6042 | dprintf("%s: added %x:%x\n", | |
6043 | __func__, disk_cfg->major, | |
6044 | disk_cfg->minor); | |
6045 | } else if (disk_cfg->action == DISK_REMOVE) { | |
6046 | dprintf("Disk remove action processed: %x.%x\n", | |
6047 | disk_cfg->major, disk_cfg->minor); | |
6048 | disk = get_disk_super(super, | |
6049 | disk_cfg->major, | |
6050 | disk_cfg->minor); | |
6051 | if (disk) { | |
6052 | /* store action status */ | |
6053 | disk->action = DISK_REMOVE; | |
6054 | /* remove spare disks only */ | |
6055 | if (disk->index == -1) { | |
6056 | remove_disk_super(super, | |
6057 | disk_cfg->major, | |
6058 | disk_cfg->minor); | |
6059 | } | |
6060 | } | |
6061 | /* release allocate disk structure */ | |
6062 | __free_imsm_disk(disk_cfg); | |
6063 | } | |
6064 | } | |
6065 | return check_degraded; | |
6066 | } | |
6067 | ||
2e5dc010 N |
6068 | static int apply_reshape_container_disks_update(struct imsm_update_reshape *u, |
6069 | struct intel_super *super, | |
6070 | void ***space_list) | |
6071 | { | |
6072 | struct dl *new_disk; | |
6073 | struct intel_dev *id; | |
6074 | int i; | |
6075 | int delta_disks = u->new_raid_disks - u->old_raid_disks; | |
ee4beede | 6076 | int disk_count = u->old_raid_disks; |
2e5dc010 N |
6077 | void **tofree = NULL; |
6078 | int devices_to_reshape = 1; | |
6079 | struct imsm_super *mpb = super->anchor; | |
6080 | int ret_val = 0; | |
d098291a | 6081 | unsigned int dev_id; |
2e5dc010 | 6082 | |
ed7333bd | 6083 | dprintf("imsm: apply_reshape_container_disks_update()\n"); |
2e5dc010 N |
6084 | |
6085 | /* enable spares to use in array */ | |
6086 | for (i = 0; i < delta_disks; i++) { | |
6087 | new_disk = get_disk_super(super, | |
6088 | major(u->new_disks[i]), | |
6089 | minor(u->new_disks[i])); | |
ed7333bd AK |
6090 | dprintf("imsm: new disk for reshape is: %i:%i " |
6091 | "(%p, index = %i)\n", | |
2e5dc010 N |
6092 | major(u->new_disks[i]), minor(u->new_disks[i]), |
6093 | new_disk, new_disk->index); | |
6094 | if ((new_disk == NULL) || | |
6095 | ((new_disk->index >= 0) && | |
6096 | (new_disk->index < u->old_raid_disks))) | |
6097 | goto update_reshape_exit; | |
ee4beede | 6098 | new_disk->index = disk_count++; |
2e5dc010 N |
6099 | /* slot to fill in autolayout |
6100 | */ | |
6101 | new_disk->raiddisk = new_disk->index; | |
6102 | new_disk->disk.status |= | |
6103 | CONFIGURED_DISK; | |
6104 | new_disk->disk.status &= ~SPARE_DISK; | |
6105 | } | |
6106 | ||
ed7333bd AK |
6107 | dprintf("imsm: volume set mpb->num_raid_devs = %i\n", |
6108 | mpb->num_raid_devs); | |
2e5dc010 N |
6109 | /* manage changes in volume |
6110 | */ | |
d098291a | 6111 | for (dev_id = 0; dev_id < mpb->num_raid_devs; dev_id++) { |
2e5dc010 N |
6112 | void **sp = *space_list; |
6113 | struct imsm_dev *newdev; | |
6114 | struct imsm_map *newmap, *oldmap; | |
6115 | ||
d098291a AK |
6116 | for (id = super->devlist ; id; id = id->next) { |
6117 | if (id->index == dev_id) | |
6118 | break; | |
6119 | } | |
6120 | if (id == NULL) | |
6121 | break; | |
2e5dc010 N |
6122 | if (!sp) |
6123 | continue; | |
6124 | *space_list = *sp; | |
6125 | newdev = (void*)sp; | |
6126 | /* Copy the dev, but not (all of) the map */ | |
6127 | memcpy(newdev, id->dev, sizeof(*newdev)); | |
6128 | oldmap = get_imsm_map(id->dev, 0); | |
6129 | newmap = get_imsm_map(newdev, 0); | |
6130 | /* Copy the current map */ | |
6131 | memcpy(newmap, oldmap, sizeof_imsm_map(oldmap)); | |
6132 | /* update one device only | |
6133 | */ | |
6134 | if (devices_to_reshape) { | |
ed7333bd AK |
6135 | dprintf("imsm: modifying subdev: %i\n", |
6136 | id->index); | |
2e5dc010 N |
6137 | devices_to_reshape--; |
6138 | newdev->vol.migr_state = 1; | |
6139 | newdev->vol.curr_migr_unit = 0; | |
6140 | newdev->vol.migr_type = MIGR_GEN_MIGR; | |
6141 | newmap->num_members = u->new_raid_disks; | |
6142 | for (i = 0; i < delta_disks; i++) { | |
6143 | set_imsm_ord_tbl_ent(newmap, | |
6144 | u->old_raid_disks + i, | |
6145 | u->old_raid_disks + i); | |
6146 | } | |
6147 | /* New map is correct, now need to save old map | |
6148 | */ | |
6149 | newmap = get_imsm_map(newdev, 1); | |
6150 | memcpy(newmap, oldmap, sizeof_imsm_map(oldmap)); | |
6151 | ||
70bdf0dc | 6152 | imsm_set_array_size(newdev); |
2e5dc010 N |
6153 | } |
6154 | ||
6155 | sp = (void **)id->dev; | |
6156 | id->dev = newdev; | |
6157 | *sp = tofree; | |
6158 | tofree = sp; | |
6159 | } | |
819bc634 AK |
6160 | if (tofree) |
6161 | *space_list = tofree; | |
2e5dc010 N |
6162 | ret_val = 1; |
6163 | ||
6164 | update_reshape_exit: | |
6165 | ||
6166 | return ret_val; | |
6167 | } | |
6168 | ||
bb025c2f | 6169 | static int apply_takeover_update(struct imsm_update_takeover *u, |
8ca6df95 KW |
6170 | struct intel_super *super, |
6171 | void ***space_list) | |
bb025c2f KW |
6172 | { |
6173 | struct imsm_dev *dev = NULL; | |
8ca6df95 KW |
6174 | struct intel_dev *dv; |
6175 | struct imsm_dev *dev_new; | |
bb025c2f KW |
6176 | struct imsm_map *map; |
6177 | struct dl *dm, *du; | |
8ca6df95 | 6178 | int i; |
bb025c2f KW |
6179 | |
6180 | for (dv = super->devlist; dv; dv = dv->next) | |
6181 | if (dv->index == (unsigned int)u->subarray) { | |
6182 | dev = dv->dev; | |
6183 | break; | |
6184 | } | |
6185 | ||
6186 | if (dev == NULL) | |
6187 | return 0; | |
6188 | ||
6189 | map = get_imsm_map(dev, 0); | |
6190 | ||
6191 | if (u->direction == R10_TO_R0) { | |
43d5ec18 KW |
6192 | /* Number of failed disks must be half of initial disk number */ |
6193 | if (imsm_count_failed(super, dev) != (map->num_members / 2)) | |
6194 | return 0; | |
6195 | ||
bb025c2f KW |
6196 | /* iterate through devices to mark removed disks as spare */ |
6197 | for (dm = super->disks; dm; dm = dm->next) { | |
6198 | if (dm->disk.status & FAILED_DISK) { | |
6199 | int idx = dm->index; | |
6200 | /* update indexes on the disk list */ | |
6201 | /* FIXME this loop-with-the-loop looks wrong, I'm not convinced | |
6202 | the index values will end up being correct.... NB */ | |
6203 | for (du = super->disks; du; du = du->next) | |
6204 | if (du->index > idx) | |
6205 | du->index--; | |
6206 | /* mark as spare disk */ | |
6207 | dm->disk.status = SPARE_DISK; | |
6208 | dm->index = -1; | |
6209 | } | |
6210 | } | |
bb025c2f KW |
6211 | /* update map */ |
6212 | map->num_members = map->num_members / 2; | |
6213 | map->map_state = IMSM_T_STATE_NORMAL; | |
6214 | map->num_domains = 1; | |
6215 | map->raid_level = 0; | |
6216 | map->failed_disk_num = -1; | |
6217 | } | |
6218 | ||
8ca6df95 KW |
6219 | if (u->direction == R0_TO_R10) { |
6220 | void **space; | |
6221 | /* update slots in current disk list */ | |
6222 | for (dm = super->disks; dm; dm = dm->next) { | |
6223 | if (dm->index >= 0) | |
6224 | dm->index *= 2; | |
6225 | } | |
6226 | /* create new *missing* disks */ | |
6227 | for (i = 0; i < map->num_members; i++) { | |
6228 | space = *space_list; | |
6229 | if (!space) | |
6230 | continue; | |
6231 | *space_list = *space; | |
6232 | du = (void *)space; | |
6233 | memcpy(du, super->disks, sizeof(*du)); | |
8ca6df95 KW |
6234 | du->fd = -1; |
6235 | du->minor = 0; | |
6236 | du->major = 0; | |
6237 | du->index = (i * 2) + 1; | |
6238 | sprintf((char *)du->disk.serial, | |
6239 | " MISSING_%d", du->index); | |
6240 | sprintf((char *)du->serial, | |
6241 | "MISSING_%d", du->index); | |
6242 | du->next = super->missing; | |
6243 | super->missing = du; | |
6244 | } | |
6245 | /* create new dev and map */ | |
6246 | space = *space_list; | |
6247 | if (!space) | |
6248 | return 0; | |
6249 | *space_list = *space; | |
6250 | dev_new = (void *)space; | |
6251 | memcpy(dev_new, dev, sizeof(*dev)); | |
6252 | /* update new map */ | |
6253 | map = get_imsm_map(dev_new, 0); | |
8ca6df95 | 6254 | map->num_members = map->num_members * 2; |
1a2487c2 | 6255 | map->map_state = IMSM_T_STATE_DEGRADED; |
8ca6df95 KW |
6256 | map->num_domains = 2; |
6257 | map->raid_level = 1; | |
6258 | /* replace dev<->dev_new */ | |
6259 | dv->dev = dev_new; | |
6260 | } | |
bb025c2f KW |
6261 | /* update disk order table */ |
6262 | for (du = super->disks; du; du = du->next) | |
6263 | if (du->index >= 0) | |
6264 | set_imsm_ord_tbl_ent(map, du->index, du->index); | |
8ca6df95 | 6265 | for (du = super->missing; du; du = du->next) |
1a2487c2 KW |
6266 | if (du->index >= 0) { |
6267 | set_imsm_ord_tbl_ent(map, du->index, du->index); | |
6268 | mark_missing(dev_new, &du->disk, du->index); | |
6269 | } | |
bb025c2f KW |
6270 | |
6271 | return 1; | |
6272 | } | |
6273 | ||
e8319a19 DW |
6274 | static void imsm_process_update(struct supertype *st, |
6275 | struct metadata_update *update) | |
6276 | { | |
6277 | /** | |
6278 | * crack open the metadata_update envelope to find the update record | |
6279 | * update can be one of: | |
d195167d AK |
6280 | * update_reshape_container_disks - all the arrays in the container |
6281 | * are being reshaped to have more devices. We need to mark | |
6282 | * the arrays for general migration and convert selected spares | |
6283 | * into active devices. | |
6284 | * update_activate_spare - a spare device has replaced a failed | |
e8319a19 DW |
6285 | * device in an array, update the disk_ord_tbl. If this disk is |
6286 | * present in all member arrays then also clear the SPARE_DISK | |
6287 | * flag | |
d195167d AK |
6288 | * update_create_array |
6289 | * update_kill_array | |
6290 | * update_rename_array | |
6291 | * update_add_remove_disk | |
e8319a19 DW |
6292 | */ |
6293 | struct intel_super *super = st->sb; | |
4d7b1503 | 6294 | struct imsm_super *mpb; |
e8319a19 DW |
6295 | enum imsm_update_type type = *(enum imsm_update_type *) update->buf; |
6296 | ||
4d7b1503 DW |
6297 | /* update requires a larger buf but the allocation failed */ |
6298 | if (super->next_len && !super->next_buf) { | |
6299 | super->next_len = 0; | |
6300 | return; | |
6301 | } | |
6302 | ||
6303 | if (super->next_buf) { | |
6304 | memcpy(super->next_buf, super->buf, super->len); | |
6305 | free(super->buf); | |
6306 | super->len = super->next_len; | |
6307 | super->buf = super->next_buf; | |
6308 | ||
6309 | super->next_len = 0; | |
6310 | super->next_buf = NULL; | |
6311 | } | |
6312 | ||
6313 | mpb = super->anchor; | |
6314 | ||
e8319a19 | 6315 | switch (type) { |
bb025c2f KW |
6316 | case update_takeover: { |
6317 | struct imsm_update_takeover *u = (void *)update->buf; | |
1a2487c2 KW |
6318 | if (apply_takeover_update(u, super, &update->space_list)) { |
6319 | imsm_update_version_info(super); | |
bb025c2f | 6320 | super->updates_pending++; |
1a2487c2 | 6321 | } |
bb025c2f KW |
6322 | break; |
6323 | } | |
6324 | ||
78b10e66 | 6325 | case update_reshape_container_disks: { |
d195167d | 6326 | struct imsm_update_reshape *u = (void *)update->buf; |
2e5dc010 N |
6327 | if (apply_reshape_container_disks_update( |
6328 | u, super, &update->space_list)) | |
6329 | super->updates_pending++; | |
78b10e66 N |
6330 | break; |
6331 | } | |
e8319a19 DW |
6332 | case update_activate_spare: { |
6333 | struct imsm_update_activate_spare *u = (void *) update->buf; | |
949c47a0 | 6334 | struct imsm_dev *dev = get_imsm_dev(super, u->array); |
a965f303 | 6335 | struct imsm_map *map = get_imsm_map(dev, 0); |
0c046afd | 6336 | struct imsm_map *migr_map; |
e8319a19 DW |
6337 | struct active_array *a; |
6338 | struct imsm_disk *disk; | |
0c046afd | 6339 | __u8 to_state; |
e8319a19 | 6340 | struct dl *dl; |
e8319a19 | 6341 | unsigned int found; |
0c046afd | 6342 | int failed; |
98130f40 | 6343 | int victim = get_imsm_disk_idx(dev, u->slot, -1); |
e8319a19 DW |
6344 | int i; |
6345 | ||
6346 | for (dl = super->disks; dl; dl = dl->next) | |
d23fe947 | 6347 | if (dl == u->dl) |
e8319a19 DW |
6348 | break; |
6349 | ||
6350 | if (!dl) { | |
6351 | fprintf(stderr, "error: imsm_activate_spare passed " | |
1f24f035 DW |
6352 | "an unknown disk (index: %d)\n", |
6353 | u->dl->index); | |
e8319a19 DW |
6354 | return; |
6355 | } | |
6356 | ||
6357 | super->updates_pending++; | |
6358 | ||
0c046afd DW |
6359 | /* count failures (excluding rebuilds and the victim) |
6360 | * to determine map[0] state | |
6361 | */ | |
6362 | failed = 0; | |
6363 | for (i = 0; i < map->num_members; i++) { | |
6364 | if (i == u->slot) | |
6365 | continue; | |
98130f40 AK |
6366 | disk = get_imsm_disk(super, |
6367 | get_imsm_disk_idx(dev, i, -1)); | |
25ed7e59 | 6368 | if (!disk || is_failed(disk)) |
0c046afd DW |
6369 | failed++; |
6370 | } | |
6371 | ||
d23fe947 DW |
6372 | /* adding a pristine spare, assign a new index */ |
6373 | if (dl->index < 0) { | |
6374 | dl->index = super->anchor->num_disks; | |
6375 | super->anchor->num_disks++; | |
6376 | } | |
d23fe947 | 6377 | disk = &dl->disk; |
f2f27e63 DW |
6378 | disk->status |= CONFIGURED_DISK; |
6379 | disk->status &= ~SPARE_DISK; | |
e8319a19 | 6380 | |
0c046afd DW |
6381 | /* mark rebuild */ |
6382 | to_state = imsm_check_degraded(super, dev, failed); | |
6383 | map->map_state = IMSM_T_STATE_DEGRADED; | |
e3bba0e0 | 6384 | migrate(dev, to_state, MIGR_REBUILD); |
0c046afd DW |
6385 | migr_map = get_imsm_map(dev, 1); |
6386 | set_imsm_ord_tbl_ent(map, u->slot, dl->index); | |
6387 | set_imsm_ord_tbl_ent(migr_map, u->slot, dl->index | IMSM_ORD_REBUILD); | |
6388 | ||
148acb7b DW |
6389 | /* update the family_num to mark a new container |
6390 | * generation, being careful to record the existing | |
6391 | * family_num in orig_family_num to clean up after | |
6392 | * earlier mdadm versions that neglected to set it. | |
6393 | */ | |
6394 | if (mpb->orig_family_num == 0) | |
6395 | mpb->orig_family_num = mpb->family_num; | |
6396 | mpb->family_num += super->random; | |
6397 | ||
e8319a19 DW |
6398 | /* count arrays using the victim in the metadata */ |
6399 | found = 0; | |
6400 | for (a = st->arrays; a ; a = a->next) { | |
949c47a0 | 6401 | dev = get_imsm_dev(super, a->info.container_member); |
620b1713 DW |
6402 | map = get_imsm_map(dev, 0); |
6403 | ||
6404 | if (get_imsm_disk_slot(map, victim) >= 0) | |
6405 | found++; | |
e8319a19 DW |
6406 | } |
6407 | ||
24565c9a | 6408 | /* delete the victim if it is no longer being |
e8319a19 DW |
6409 | * utilized anywhere |
6410 | */ | |
e8319a19 | 6411 | if (!found) { |
ae6aad82 | 6412 | struct dl **dlp; |
24565c9a | 6413 | |
47ee5a45 DW |
6414 | /* We know that 'manager' isn't touching anything, |
6415 | * so it is safe to delete | |
6416 | */ | |
24565c9a | 6417 | for (dlp = &super->disks; *dlp; dlp = &(*dlp)->next) |
ae6aad82 DW |
6418 | if ((*dlp)->index == victim) |
6419 | break; | |
47ee5a45 DW |
6420 | |
6421 | /* victim may be on the missing list */ | |
6422 | if (!*dlp) | |
6423 | for (dlp = &super->missing; *dlp; dlp = &(*dlp)->next) | |
6424 | if ((*dlp)->index == victim) | |
6425 | break; | |
24565c9a | 6426 | imsm_delete(super, dlp, victim); |
e8319a19 | 6427 | } |
8273f55e DW |
6428 | break; |
6429 | } | |
6430 | case update_create_array: { | |
6431 | /* someone wants to create a new array, we need to be aware of | |
6432 | * a few races/collisions: | |
6433 | * 1/ 'Create' called by two separate instances of mdadm | |
6434 | * 2/ 'Create' versus 'activate_spare': mdadm has chosen | |
6435 | * devices that have since been assimilated via | |
6436 | * activate_spare. | |
6437 | * In the event this update can not be carried out mdadm will | |
6438 | * (FIX ME) notice that its update did not take hold. | |
6439 | */ | |
6440 | struct imsm_update_create_array *u = (void *) update->buf; | |
ba2de7ba | 6441 | struct intel_dev *dv; |
8273f55e DW |
6442 | struct imsm_dev *dev; |
6443 | struct imsm_map *map, *new_map; | |
6444 | unsigned long long start, end; | |
6445 | unsigned long long new_start, new_end; | |
6446 | int i; | |
54c2c1ea DW |
6447 | struct disk_info *inf; |
6448 | struct dl *dl; | |
8273f55e DW |
6449 | |
6450 | /* handle racing creates: first come first serve */ | |
6451 | if (u->dev_idx < mpb->num_raid_devs) { | |
6452 | dprintf("%s: subarray %d already defined\n", | |
6453 | __func__, u->dev_idx); | |
ba2de7ba | 6454 | goto create_error; |
8273f55e DW |
6455 | } |
6456 | ||
6457 | /* check update is next in sequence */ | |
6458 | if (u->dev_idx != mpb->num_raid_devs) { | |
6a3e913e DW |
6459 | dprintf("%s: can not create array %d expected index %d\n", |
6460 | __func__, u->dev_idx, mpb->num_raid_devs); | |
ba2de7ba | 6461 | goto create_error; |
8273f55e DW |
6462 | } |
6463 | ||
a965f303 | 6464 | new_map = get_imsm_map(&u->dev, 0); |
8273f55e DW |
6465 | new_start = __le32_to_cpu(new_map->pba_of_lba0); |
6466 | new_end = new_start + __le32_to_cpu(new_map->blocks_per_member); | |
54c2c1ea | 6467 | inf = get_disk_info(u); |
8273f55e DW |
6468 | |
6469 | /* handle activate_spare versus create race: | |
6470 | * check to make sure that overlapping arrays do not include | |
6471 | * overalpping disks | |
6472 | */ | |
6473 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
949c47a0 | 6474 | dev = get_imsm_dev(super, i); |
a965f303 | 6475 | map = get_imsm_map(dev, 0); |
8273f55e DW |
6476 | start = __le32_to_cpu(map->pba_of_lba0); |
6477 | end = start + __le32_to_cpu(map->blocks_per_member); | |
6478 | if ((new_start >= start && new_start <= end) || | |
6479 | (start >= new_start && start <= new_end)) | |
54c2c1ea DW |
6480 | /* overlap */; |
6481 | else | |
6482 | continue; | |
6483 | ||
6484 | if (disks_overlap(super, i, u)) { | |
8273f55e | 6485 | dprintf("%s: arrays overlap\n", __func__); |
ba2de7ba | 6486 | goto create_error; |
8273f55e DW |
6487 | } |
6488 | } | |
8273f55e | 6489 | |
949c47a0 DW |
6490 | /* check that prepare update was successful */ |
6491 | if (!update->space) { | |
6492 | dprintf("%s: prepare update failed\n", __func__); | |
ba2de7ba | 6493 | goto create_error; |
949c47a0 DW |
6494 | } |
6495 | ||
54c2c1ea DW |
6496 | /* check that all disks are still active before committing |
6497 | * changes. FIXME: could we instead handle this by creating a | |
6498 | * degraded array? That's probably not what the user expects, | |
6499 | * so better to drop this update on the floor. | |
6500 | */ | |
6501 | for (i = 0; i < new_map->num_members; i++) { | |
6502 | dl = serial_to_dl(inf[i].serial, super); | |
6503 | if (!dl) { | |
6504 | dprintf("%s: disk disappeared\n", __func__); | |
ba2de7ba | 6505 | goto create_error; |
54c2c1ea | 6506 | } |
949c47a0 DW |
6507 | } |
6508 | ||
8273f55e | 6509 | super->updates_pending++; |
54c2c1ea DW |
6510 | |
6511 | /* convert spares to members and fixup ord_tbl */ | |
6512 | for (i = 0; i < new_map->num_members; i++) { | |
6513 | dl = serial_to_dl(inf[i].serial, super); | |
6514 | if (dl->index == -1) { | |
6515 | dl->index = mpb->num_disks; | |
6516 | mpb->num_disks++; | |
6517 | dl->disk.status |= CONFIGURED_DISK; | |
6518 | dl->disk.status &= ~SPARE_DISK; | |
6519 | } | |
6520 | set_imsm_ord_tbl_ent(new_map, i, dl->index); | |
6521 | } | |
6522 | ||
ba2de7ba DW |
6523 | dv = update->space; |
6524 | dev = dv->dev; | |
949c47a0 DW |
6525 | update->space = NULL; |
6526 | imsm_copy_dev(dev, &u->dev); | |
ba2de7ba DW |
6527 | dv->index = u->dev_idx; |
6528 | dv->next = super->devlist; | |
6529 | super->devlist = dv; | |
8273f55e | 6530 | mpb->num_raid_devs++; |
8273f55e | 6531 | |
4d1313e9 | 6532 | imsm_update_version_info(super); |
8273f55e | 6533 | break; |
ba2de7ba DW |
6534 | create_error: |
6535 | /* mdmon knows how to release update->space, but not | |
6536 | * ((struct intel_dev *) update->space)->dev | |
6537 | */ | |
6538 | if (update->space) { | |
6539 | dv = update->space; | |
6540 | free(dv->dev); | |
6541 | } | |
8273f55e | 6542 | break; |
e8319a19 | 6543 | } |
33414a01 DW |
6544 | case update_kill_array: { |
6545 | struct imsm_update_kill_array *u = (void *) update->buf; | |
6546 | int victim = u->dev_idx; | |
6547 | struct active_array *a; | |
6548 | struct intel_dev **dp; | |
6549 | struct imsm_dev *dev; | |
6550 | ||
6551 | /* sanity check that we are not affecting the uuid of | |
6552 | * active arrays, or deleting an active array | |
6553 | * | |
6554 | * FIXME when immutable ids are available, but note that | |
6555 | * we'll also need to fixup the invalidated/active | |
6556 | * subarray indexes in mdstat | |
6557 | */ | |
6558 | for (a = st->arrays; a; a = a->next) | |
6559 | if (a->info.container_member >= victim) | |
6560 | break; | |
6561 | /* by definition if mdmon is running at least one array | |
6562 | * is active in the container, so checking | |
6563 | * mpb->num_raid_devs is just extra paranoia | |
6564 | */ | |
6565 | dev = get_imsm_dev(super, victim); | |
6566 | if (a || !dev || mpb->num_raid_devs == 1) { | |
6567 | dprintf("failed to delete subarray-%d\n", victim); | |
6568 | break; | |
6569 | } | |
6570 | ||
6571 | for (dp = &super->devlist; *dp;) | |
f21e18ca | 6572 | if ((*dp)->index == (unsigned)super->current_vol) { |
33414a01 DW |
6573 | *dp = (*dp)->next; |
6574 | } else { | |
f21e18ca | 6575 | if ((*dp)->index > (unsigned)victim) |
33414a01 DW |
6576 | (*dp)->index--; |
6577 | dp = &(*dp)->next; | |
6578 | } | |
6579 | mpb->num_raid_devs--; | |
6580 | super->updates_pending++; | |
6581 | break; | |
6582 | } | |
aa534678 DW |
6583 | case update_rename_array: { |
6584 | struct imsm_update_rename_array *u = (void *) update->buf; | |
6585 | char name[MAX_RAID_SERIAL_LEN+1]; | |
6586 | int target = u->dev_idx; | |
6587 | struct active_array *a; | |
6588 | struct imsm_dev *dev; | |
6589 | ||
6590 | /* sanity check that we are not affecting the uuid of | |
6591 | * an active array | |
6592 | */ | |
6593 | snprintf(name, MAX_RAID_SERIAL_LEN, "%s", (char *) u->name); | |
6594 | name[MAX_RAID_SERIAL_LEN] = '\0'; | |
6595 | for (a = st->arrays; a; a = a->next) | |
6596 | if (a->info.container_member == target) | |
6597 | break; | |
6598 | dev = get_imsm_dev(super, u->dev_idx); | |
6599 | if (a || !dev || !check_name(super, name, 1)) { | |
6600 | dprintf("failed to rename subarray-%d\n", target); | |
6601 | break; | |
6602 | } | |
6603 | ||
cdbe98cd | 6604 | snprintf((char *) dev->volume, MAX_RAID_SERIAL_LEN, "%s", name); |
aa534678 DW |
6605 | super->updates_pending++; |
6606 | break; | |
6607 | } | |
1a64be56 | 6608 | case update_add_remove_disk: { |
43dad3d6 | 6609 | /* we may be able to repair some arrays if disks are |
1a64be56 LM |
6610 | * being added, check teh status of add_remove_disk |
6611 | * if discs has been added. | |
6612 | */ | |
6613 | if (add_remove_disk_update(super)) { | |
43dad3d6 | 6614 | struct active_array *a; |
072b727f DW |
6615 | |
6616 | super->updates_pending++; | |
1a64be56 | 6617 | for (a = st->arrays; a; a = a->next) |
43dad3d6 DW |
6618 | a->check_degraded = 1; |
6619 | } | |
43dad3d6 | 6620 | break; |
e8319a19 | 6621 | } |
1a64be56 LM |
6622 | default: |
6623 | fprintf(stderr, "error: unsuported process update type:" | |
6624 | "(type: %d)\n", type); | |
6625 | } | |
e8319a19 | 6626 | } |
88758e9d | 6627 | |
8273f55e DW |
6628 | static void imsm_prepare_update(struct supertype *st, |
6629 | struct metadata_update *update) | |
6630 | { | |
949c47a0 | 6631 | /** |
4d7b1503 DW |
6632 | * Allocate space to hold new disk entries, raid-device entries or a new |
6633 | * mpb if necessary. The manager synchronously waits for updates to | |
6634 | * complete in the monitor, so new mpb buffers allocated here can be | |
6635 | * integrated by the monitor thread without worrying about live pointers | |
6636 | * in the manager thread. | |
8273f55e | 6637 | */ |
949c47a0 | 6638 | enum imsm_update_type type = *(enum imsm_update_type *) update->buf; |
4d7b1503 DW |
6639 | struct intel_super *super = st->sb; |
6640 | struct imsm_super *mpb = super->anchor; | |
6641 | size_t buf_len; | |
6642 | size_t len = 0; | |
949c47a0 DW |
6643 | |
6644 | switch (type) { | |
abedf5fc KW |
6645 | case update_takeover: { |
6646 | struct imsm_update_takeover *u = (void *)update->buf; | |
6647 | if (u->direction == R0_TO_R10) { | |
6648 | void **tail = (void **)&update->space_list; | |
6649 | struct imsm_dev *dev = get_imsm_dev(super, u->subarray); | |
6650 | struct imsm_map *map = get_imsm_map(dev, 0); | |
6651 | int num_members = map->num_members; | |
6652 | void *space; | |
6653 | int size, i; | |
6654 | int err = 0; | |
6655 | /* allocate memory for added disks */ | |
6656 | for (i = 0; i < num_members; i++) { | |
6657 | size = sizeof(struct dl); | |
6658 | space = malloc(size); | |
6659 | if (!space) { | |
6660 | err++; | |
6661 | break; | |
6662 | } | |
6663 | *tail = space; | |
6664 | tail = space; | |
6665 | *tail = NULL; | |
6666 | } | |
6667 | /* allocate memory for new device */ | |
6668 | size = sizeof_imsm_dev(super->devlist->dev, 0) + | |
6669 | (num_members * sizeof(__u32)); | |
6670 | space = malloc(size); | |
6671 | if (!space) | |
6672 | err++; | |
6673 | else { | |
6674 | *tail = space; | |
6675 | tail = space; | |
6676 | *tail = NULL; | |
6677 | } | |
6678 | if (!err) { | |
6679 | len = disks_to_mpb_size(num_members * 2); | |
6680 | } else { | |
6681 | /* if allocation didn't success, free buffer */ | |
6682 | while (update->space_list) { | |
6683 | void **sp = update->space_list; | |
6684 | update->space_list = *sp; | |
6685 | free(sp); | |
6686 | } | |
6687 | } | |
6688 | } | |
6689 | ||
6690 | break; | |
6691 | } | |
78b10e66 | 6692 | case update_reshape_container_disks: { |
d195167d AK |
6693 | /* Every raid device in the container is about to |
6694 | * gain some more devices, and we will enter a | |
6695 | * reconfiguration. | |
6696 | * So each 'imsm_map' will be bigger, and the imsm_vol | |
6697 | * will now hold 2 of them. | |
6698 | * Thus we need new 'struct imsm_dev' allocations sized | |
6699 | * as sizeof_imsm_dev but with more devices in both maps. | |
6700 | */ | |
6701 | struct imsm_update_reshape *u = (void *)update->buf; | |
6702 | struct intel_dev *dl; | |
6703 | void **space_tail = (void**)&update->space_list; | |
6704 | ||
6705 | dprintf("imsm: imsm_prepare_update() for update_reshape\n"); | |
6706 | ||
6707 | for (dl = super->devlist; dl; dl = dl->next) { | |
6708 | int size = sizeof_imsm_dev(dl->dev, 1); | |
6709 | void *s; | |
d677e0b8 AK |
6710 | if (u->new_raid_disks > u->old_raid_disks) |
6711 | size += sizeof(__u32)*2* | |
6712 | (u->new_raid_disks - u->old_raid_disks); | |
d195167d AK |
6713 | s = malloc(size); |
6714 | if (!s) | |
6715 | break; | |
6716 | *space_tail = s; | |
6717 | space_tail = s; | |
6718 | *space_tail = NULL; | |
6719 | } | |
6720 | ||
6721 | len = disks_to_mpb_size(u->new_raid_disks); | |
6722 | dprintf("New anchor length is %llu\n", (unsigned long long)len); | |
78b10e66 N |
6723 | break; |
6724 | } | |
949c47a0 DW |
6725 | case update_create_array: { |
6726 | struct imsm_update_create_array *u = (void *) update->buf; | |
ba2de7ba | 6727 | struct intel_dev *dv; |
54c2c1ea DW |
6728 | struct imsm_dev *dev = &u->dev; |
6729 | struct imsm_map *map = get_imsm_map(dev, 0); | |
6730 | struct dl *dl; | |
6731 | struct disk_info *inf; | |
6732 | int i; | |
6733 | int activate = 0; | |
949c47a0 | 6734 | |
54c2c1ea DW |
6735 | inf = get_disk_info(u); |
6736 | len = sizeof_imsm_dev(dev, 1); | |
ba2de7ba DW |
6737 | /* allocate a new super->devlist entry */ |
6738 | dv = malloc(sizeof(*dv)); | |
6739 | if (dv) { | |
6740 | dv->dev = malloc(len); | |
6741 | if (dv->dev) | |
6742 | update->space = dv; | |
6743 | else { | |
6744 | free(dv); | |
6745 | update->space = NULL; | |
6746 | } | |
6747 | } | |
949c47a0 | 6748 | |
54c2c1ea DW |
6749 | /* count how many spares will be converted to members */ |
6750 | for (i = 0; i < map->num_members; i++) { | |
6751 | dl = serial_to_dl(inf[i].serial, super); | |
6752 | if (!dl) { | |
6753 | /* hmm maybe it failed?, nothing we can do about | |
6754 | * it here | |
6755 | */ | |
6756 | continue; | |
6757 | } | |
6758 | if (count_memberships(dl, super) == 0) | |
6759 | activate++; | |
6760 | } | |
6761 | len += activate * sizeof(struct imsm_disk); | |
949c47a0 DW |
6762 | break; |
6763 | default: | |
6764 | break; | |
6765 | } | |
6766 | } | |
8273f55e | 6767 | |
4d7b1503 DW |
6768 | /* check if we need a larger metadata buffer */ |
6769 | if (super->next_buf) | |
6770 | buf_len = super->next_len; | |
6771 | else | |
6772 | buf_len = super->len; | |
6773 | ||
6774 | if (__le32_to_cpu(mpb->mpb_size) + len > buf_len) { | |
6775 | /* ok we need a larger buf than what is currently allocated | |
6776 | * if this allocation fails process_update will notice that | |
6777 | * ->next_len is set and ->next_buf is NULL | |
6778 | */ | |
6779 | buf_len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + len, 512); | |
6780 | if (super->next_buf) | |
6781 | free(super->next_buf); | |
6782 | ||
6783 | super->next_len = buf_len; | |
1f45a8ad DW |
6784 | if (posix_memalign(&super->next_buf, 512, buf_len) == 0) |
6785 | memset(super->next_buf, 0, buf_len); | |
6786 | else | |
4d7b1503 DW |
6787 | super->next_buf = NULL; |
6788 | } | |
8273f55e DW |
6789 | } |
6790 | ||
ae6aad82 | 6791 | /* must be called while manager is quiesced */ |
f21e18ca | 6792 | static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index) |
ae6aad82 DW |
6793 | { |
6794 | struct imsm_super *mpb = super->anchor; | |
ae6aad82 DW |
6795 | struct dl *iter; |
6796 | struct imsm_dev *dev; | |
6797 | struct imsm_map *map; | |
24565c9a DW |
6798 | int i, j, num_members; |
6799 | __u32 ord; | |
ae6aad82 | 6800 | |
24565c9a DW |
6801 | dprintf("%s: deleting device[%d] from imsm_super\n", |
6802 | __func__, index); | |
ae6aad82 DW |
6803 | |
6804 | /* shift all indexes down one */ | |
6805 | for (iter = super->disks; iter; iter = iter->next) | |
f21e18ca | 6806 | if (iter->index > (int)index) |
ae6aad82 | 6807 | iter->index--; |
47ee5a45 | 6808 | for (iter = super->missing; iter; iter = iter->next) |
f21e18ca | 6809 | if (iter->index > (int)index) |
47ee5a45 | 6810 | iter->index--; |
ae6aad82 DW |
6811 | |
6812 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
6813 | dev = get_imsm_dev(super, i); | |
6814 | map = get_imsm_map(dev, 0); | |
24565c9a DW |
6815 | num_members = map->num_members; |
6816 | for (j = 0; j < num_members; j++) { | |
6817 | /* update ord entries being careful not to propagate | |
6818 | * ord-flags to the first map | |
6819 | */ | |
98130f40 | 6820 | ord = get_imsm_ord_tbl_ent(dev, j, -1); |
ae6aad82 | 6821 | |
24565c9a DW |
6822 | if (ord_to_idx(ord) <= index) |
6823 | continue; | |
ae6aad82 | 6824 | |
24565c9a DW |
6825 | map = get_imsm_map(dev, 0); |
6826 | set_imsm_ord_tbl_ent(map, j, ord_to_idx(ord - 1)); | |
6827 | map = get_imsm_map(dev, 1); | |
6828 | if (map) | |
6829 | set_imsm_ord_tbl_ent(map, j, ord - 1); | |
ae6aad82 DW |
6830 | } |
6831 | } | |
6832 | ||
6833 | mpb->num_disks--; | |
6834 | super->updates_pending++; | |
24565c9a DW |
6835 | if (*dlp) { |
6836 | struct dl *dl = *dlp; | |
6837 | ||
6838 | *dlp = (*dlp)->next; | |
6839 | __free_imsm_disk(dl); | |
6840 | } | |
ae6aad82 DW |
6841 | } |
6842 | ||
2cda7640 ML |
6843 | static char disk_by_path[] = "/dev/disk/by-path/"; |
6844 | ||
6845 | static const char *imsm_get_disk_controller_domain(const char *path) | |
6846 | { | |
2cda7640 | 6847 | char disk_path[PATH_MAX]; |
96234762 LM |
6848 | char *drv=NULL; |
6849 | struct stat st; | |
2cda7640 | 6850 | |
96234762 LM |
6851 | strncpy(disk_path, disk_by_path, PATH_MAX - 1); |
6852 | strncat(disk_path, path, PATH_MAX - strlen(disk_path) - 1); | |
6853 | if (stat(disk_path, &st) == 0) { | |
6854 | struct sys_dev* hba; | |
6855 | char *path=NULL; | |
6856 | ||
6857 | path = devt_to_devpath(st.st_rdev); | |
6858 | if (path == NULL) | |
6859 | return "unknown"; | |
6860 | hba = find_disk_attached_hba(-1, path); | |
6861 | if (hba && hba->type == SYS_DEV_SAS) | |
6862 | drv = "isci"; | |
6863 | else if (hba && hba->type == SYS_DEV_SATA) | |
6864 | drv = "ahci"; | |
6865 | else | |
6866 | drv = "unknown"; | |
6867 | dprintf("path: %s hba: %s attached: %s\n", | |
6868 | path, (hba) ? hba->path : "NULL", drv); | |
6869 | free(path); | |
6870 | if (hba) | |
6871 | free_sys_dev(&hba); | |
2cda7640 | 6872 | } |
96234762 | 6873 | return drv; |
2cda7640 ML |
6874 | } |
6875 | ||
78b10e66 N |
6876 | static int imsm_find_array_minor_by_subdev(int subdev, int container, int *minor) |
6877 | { | |
6878 | char subdev_name[20]; | |
6879 | struct mdstat_ent *mdstat; | |
6880 | ||
6881 | sprintf(subdev_name, "%d", subdev); | |
6882 | mdstat = mdstat_by_subdev(subdev_name, container); | |
6883 | if (!mdstat) | |
6884 | return -1; | |
6885 | ||
6886 | *minor = mdstat->devnum; | |
6887 | free_mdstat(mdstat); | |
6888 | return 0; | |
6889 | } | |
6890 | ||
6891 | static int imsm_reshape_is_allowed_on_container(struct supertype *st, | |
6892 | struct geo_params *geo, | |
6893 | int *old_raid_disks) | |
6894 | { | |
694575e7 KW |
6895 | /* currently we only support increasing the number of devices |
6896 | * for a container. This increases the number of device for each | |
6897 | * member array. They must all be RAID0 or RAID5. | |
6898 | */ | |
78b10e66 N |
6899 | int ret_val = 0; |
6900 | struct mdinfo *info, *member; | |
6901 | int devices_that_can_grow = 0; | |
6902 | ||
6903 | dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): " | |
6904 | "st->devnum = (%i)\n", | |
6905 | st->devnum); | |
6906 | ||
6907 | if (geo->size != -1 || | |
6908 | geo->level != UnSet || | |
6909 | geo->layout != UnSet || | |
6910 | geo->chunksize != 0 || | |
6911 | geo->raid_disks == UnSet) { | |
6912 | dprintf("imsm: Container operation is allowed for " | |
6913 | "raid disks number change only.\n"); | |
6914 | return ret_val; | |
6915 | } | |
6916 | ||
6917 | info = container_content_imsm(st, NULL); | |
6918 | for (member = info; member; member = member->next) { | |
6919 | int result; | |
6920 | int minor; | |
6921 | ||
6922 | dprintf("imsm: checking device_num: %i\n", | |
6923 | member->container_member); | |
6924 | ||
d7d205bd | 6925 | if (geo->raid_disks <= member->array.raid_disks) { |
78b10e66 N |
6926 | /* we work on container for Online Capacity Expansion |
6927 | * only so raid_disks has to grow | |
6928 | */ | |
6929 | dprintf("imsm: for container operation raid disks " | |
6930 | "increase is required\n"); | |
6931 | break; | |
6932 | } | |
6933 | ||
6934 | if ((info->array.level != 0) && | |
6935 | (info->array.level != 5)) { | |
6936 | /* we cannot use this container with other raid level | |
6937 | */ | |
690aae1a | 6938 | dprintf("imsm: for container operation wrong" |
78b10e66 N |
6939 | " raid level (%i) detected\n", |
6940 | info->array.level); | |
6941 | break; | |
6942 | } else { | |
6943 | /* check for platform support | |
6944 | * for this raid level configuration | |
6945 | */ | |
6946 | struct intel_super *super = st->sb; | |
6947 | if (!is_raid_level_supported(super->orom, | |
6948 | member->array.level, | |
6949 | geo->raid_disks)) { | |
690aae1a | 6950 | dprintf("platform does not support raid%d with" |
78b10e66 N |
6951 | " %d disk%s\n", |
6952 | info->array.level, | |
6953 | geo->raid_disks, | |
6954 | geo->raid_disks > 1 ? "s" : ""); | |
6955 | break; | |
6956 | } | |
6957 | } | |
6958 | ||
6959 | if (*old_raid_disks && | |
6960 | info->array.raid_disks != *old_raid_disks) | |
6961 | break; | |
6962 | *old_raid_disks = info->array.raid_disks; | |
6963 | ||
6964 | /* All raid5 and raid0 volumes in container | |
6965 | * have to be ready for Online Capacity Expansion | |
6966 | * so they need to be assembled. We have already | |
6967 | * checked that no recovery etc is happening. | |
6968 | */ | |
6969 | result = imsm_find_array_minor_by_subdev(member->container_member, | |
6970 | st->container_dev, | |
6971 | &minor); | |
6972 | if (result < 0) { | |
6973 | dprintf("imsm: cannot find array\n"); | |
6974 | break; | |
6975 | } | |
6976 | devices_that_can_grow++; | |
6977 | } | |
6978 | sysfs_free(info); | |
6979 | if (!member && devices_that_can_grow) | |
6980 | ret_val = 1; | |
6981 | ||
6982 | if (ret_val) | |
6983 | dprintf("\tContainer operation allowed\n"); | |
6984 | else | |
6985 | dprintf("\tError: %i\n", ret_val); | |
6986 | ||
6987 | return ret_val; | |
6988 | } | |
6989 | ||
6990 | /* Function: get_spares_for_grow | |
6991 | * Description: Allocates memory and creates list of spare devices | |
6992 | * avaliable in container. Checks if spare drive size is acceptable. | |
6993 | * Parameters: Pointer to the supertype structure | |
6994 | * Returns: Pointer to the list of spare devices (mdinfo structure) on success, | |
6995 | * NULL if fail | |
6996 | */ | |
6997 | static struct mdinfo *get_spares_for_grow(struct supertype *st) | |
6998 | { | |
78b10e66 | 6999 | unsigned long long min_size = min_acceptable_spare_size_imsm(st); |
326727d9 | 7000 | return container_choose_spares(st, min_size, NULL, NULL, NULL, 0); |
78b10e66 N |
7001 | } |
7002 | ||
7003 | /****************************************************************************** | |
7004 | * function: imsm_create_metadata_update_for_reshape | |
7005 | * Function creates update for whole IMSM container. | |
7006 | * | |
7007 | ******************************************************************************/ | |
7008 | static int imsm_create_metadata_update_for_reshape( | |
7009 | struct supertype *st, | |
7010 | struct geo_params *geo, | |
7011 | int old_raid_disks, | |
7012 | struct imsm_update_reshape **updatep) | |
7013 | { | |
7014 | struct intel_super *super = st->sb; | |
7015 | struct imsm_super *mpb = super->anchor; | |
7016 | int update_memory_size = 0; | |
7017 | struct imsm_update_reshape *u = NULL; | |
7018 | struct mdinfo *spares = NULL; | |
7019 | int i; | |
7020 | int delta_disks = 0; | |
bbd24d86 | 7021 | struct mdinfo *dev; |
78b10e66 N |
7022 | |
7023 | dprintf("imsm_update_metadata_for_reshape(enter) raid_disks = %i\n", | |
7024 | geo->raid_disks); | |
7025 | ||
7026 | delta_disks = geo->raid_disks - old_raid_disks; | |
7027 | ||
7028 | /* size of all update data without anchor */ | |
7029 | update_memory_size = sizeof(struct imsm_update_reshape); | |
7030 | ||
7031 | /* now add space for spare disks that we need to add. */ | |
7032 | update_memory_size += sizeof(u->new_disks[0]) * (delta_disks - 1); | |
7033 | ||
7034 | u = calloc(1, update_memory_size); | |
7035 | if (u == NULL) { | |
7036 | dprintf("error: " | |
7037 | "cannot get memory for imsm_update_reshape update\n"); | |
7038 | return 0; | |
7039 | } | |
7040 | u->type = update_reshape_container_disks; | |
7041 | u->old_raid_disks = old_raid_disks; | |
7042 | u->new_raid_disks = geo->raid_disks; | |
7043 | ||
7044 | /* now get spare disks list | |
7045 | */ | |
7046 | spares = get_spares_for_grow(st); | |
7047 | ||
7048 | if (spares == NULL | |
7049 | || delta_disks > spares->array.spare_disks) { | |
e14e5960 KW |
7050 | fprintf(stderr, Name ": imsm: ERROR: Cannot get spare devices " |
7051 | "for %s.\n", geo->dev_name); | |
78b10e66 N |
7052 | goto abort; |
7053 | } | |
7054 | ||
7055 | /* we have got spares | |
7056 | * update disk list in imsm_disk list table in anchor | |
7057 | */ | |
7058 | dprintf("imsm: %i spares are available.\n\n", | |
7059 | spares->array.spare_disks); | |
7060 | ||
bbd24d86 | 7061 | dev = spares->devs; |
78b10e66 | 7062 | for (i = 0; i < delta_disks; i++) { |
78b10e66 N |
7063 | struct dl *dl; |
7064 | ||
bbd24d86 AK |
7065 | if (dev == NULL) |
7066 | break; | |
78b10e66 N |
7067 | u->new_disks[i] = makedev(dev->disk.major, |
7068 | dev->disk.minor); | |
7069 | dl = get_disk_super(super, dev->disk.major, dev->disk.minor); | |
ee4beede AK |
7070 | dl->index = mpb->num_disks; |
7071 | mpb->num_disks++; | |
bbd24d86 | 7072 | dev = dev->next; |
78b10e66 | 7073 | } |
78b10e66 N |
7074 | |
7075 | abort: | |
7076 | /* free spares | |
7077 | */ | |
7078 | sysfs_free(spares); | |
7079 | ||
d677e0b8 | 7080 | dprintf("imsm: reshape update preparation :"); |
78b10e66 | 7081 | if (i == delta_disks) { |
d677e0b8 | 7082 | dprintf(" OK\n"); |
78b10e66 N |
7083 | *updatep = u; |
7084 | return update_memory_size; | |
7085 | } | |
7086 | free(u); | |
d677e0b8 | 7087 | dprintf(" Error\n"); |
78b10e66 N |
7088 | |
7089 | return 0; | |
7090 | } | |
7091 | ||
8dd70bce AK |
7092 | static void imsm_update_metadata_locally(struct supertype *st, |
7093 | void *buf, int len) | |
7094 | { | |
7095 | struct metadata_update mu; | |
7096 | ||
7097 | mu.buf = buf; | |
7098 | mu.len = len; | |
7099 | mu.space = NULL; | |
7100 | mu.space_list = NULL; | |
7101 | mu.next = NULL; | |
7102 | imsm_prepare_update(st, &mu); | |
7103 | imsm_process_update(st, &mu); | |
7104 | ||
7105 | while (mu.space_list) { | |
7106 | void **space = mu.space_list; | |
7107 | mu.space_list = *space; | |
7108 | free(space); | |
7109 | } | |
7110 | } | |
78b10e66 | 7111 | |
471bceb6 | 7112 | /*************************************************************************** |
694575e7 | 7113 | * Function: imsm_analyze_change |
471bceb6 KW |
7114 | * Description: Function analyze change for single volume |
7115 | * and validate if transition is supported | |
694575e7 KW |
7116 | * Parameters: Geometry parameters, supertype structure |
7117 | * Returns: Operation type code on success, -1 if fail | |
471bceb6 KW |
7118 | ****************************************************************************/ |
7119 | enum imsm_reshape_type imsm_analyze_change(struct supertype *st, | |
7120 | struct geo_params *geo) | |
694575e7 | 7121 | { |
471bceb6 KW |
7122 | struct mdinfo info; |
7123 | int change = -1; | |
7124 | int check_devs = 0; | |
c21e737b | 7125 | int chunk; |
471bceb6 KW |
7126 | |
7127 | getinfo_super_imsm_volume(st, &info, NULL); | |
7128 | ||
7129 | if ((geo->level != info.array.level) && | |
7130 | (geo->level >= 0) && | |
7131 | (geo->level != UnSet)) { | |
7132 | switch (info.array.level) { | |
7133 | case 0: | |
7134 | if (geo->level == 5) { | |
b5347799 | 7135 | change = CH_MIGRATION; |
471bceb6 KW |
7136 | check_devs = 1; |
7137 | } | |
7138 | if (geo->level == 10) { | |
7139 | change = CH_TAKEOVER; | |
7140 | check_devs = 1; | |
7141 | } | |
dfe77a9e KW |
7142 | break; |
7143 | case 1: | |
7144 | if (geo->level == 0) { | |
7145 | change = CH_TAKEOVER; | |
7146 | check_devs = 1; | |
7147 | } | |
471bceb6 KW |
7148 | break; |
7149 | case 5: | |
41bf155e | 7150 | if (geo->level == 0) |
b5347799 | 7151 | change = CH_MIGRATION; |
471bceb6 KW |
7152 | break; |
7153 | case 10: | |
7154 | if (geo->level == 0) { | |
7155 | change = CH_TAKEOVER; | |
7156 | check_devs = 1; | |
7157 | } | |
7158 | break; | |
7159 | } | |
7160 | if (change == -1) { | |
7161 | fprintf(stderr, | |
7162 | Name " Error. Level Migration from %d to %d " | |
7163 | "not supported!\n", | |
7164 | info.array.level, geo->level); | |
7165 | goto analyse_change_exit; | |
7166 | } | |
7167 | } else | |
7168 | geo->level = info.array.level; | |
7169 | ||
7170 | if ((geo->layout != info.array.layout) | |
7171 | && ((geo->layout != UnSet) && (geo->layout != -1))) { | |
b5347799 | 7172 | change = CH_MIGRATION; |
471bceb6 KW |
7173 | if ((info.array.layout == 0) |
7174 | && (info.array.level == 5) | |
7175 | && (geo->layout == 5)) { | |
7176 | /* reshape 5 -> 4 */ | |
7177 | } else if ((info.array.layout == 5) | |
7178 | && (info.array.level == 5) | |
7179 | && (geo->layout == 0)) { | |
7180 | /* reshape 4 -> 5 */ | |
7181 | geo->layout = 0; | |
7182 | geo->level = 5; | |
7183 | } else { | |
7184 | fprintf(stderr, | |
7185 | Name " Error. Layout Migration from %d to %d " | |
7186 | "not supported!\n", | |
7187 | info.array.layout, geo->layout); | |
7188 | change = -1; | |
7189 | goto analyse_change_exit; | |
7190 | } | |
7191 | } else | |
7192 | geo->layout = info.array.layout; | |
7193 | ||
7194 | if ((geo->chunksize > 0) && (geo->chunksize != UnSet) | |
7195 | && (geo->chunksize != info.array.chunk_size)) | |
b5347799 | 7196 | change = CH_MIGRATION; |
471bceb6 KW |
7197 | else |
7198 | geo->chunksize = info.array.chunk_size; | |
7199 | ||
c21e737b | 7200 | chunk = geo->chunksize / 1024; |
471bceb6 KW |
7201 | if (!validate_geometry_imsm(st, |
7202 | geo->level, | |
7203 | geo->layout, | |
7204 | geo->raid_disks, | |
c21e737b | 7205 | &chunk, |
471bceb6 KW |
7206 | geo->size, |
7207 | 0, 0, 1)) | |
7208 | change = -1; | |
7209 | ||
7210 | if (check_devs) { | |
7211 | struct intel_super *super = st->sb; | |
7212 | struct imsm_super *mpb = super->anchor; | |
7213 | ||
7214 | if (mpb->num_raid_devs > 1) { | |
7215 | fprintf(stderr, | |
7216 | Name " Error. Cannot perform operation on %s" | |
7217 | "- for this operation it MUST be single " | |
7218 | "array in container\n", | |
7219 | geo->dev_name); | |
7220 | change = -1; | |
7221 | } | |
7222 | } | |
7223 | ||
7224 | analyse_change_exit: | |
7225 | ||
7226 | return change; | |
694575e7 KW |
7227 | } |
7228 | ||
bb025c2f KW |
7229 | int imsm_takeover(struct supertype *st, struct geo_params *geo) |
7230 | { | |
7231 | struct intel_super *super = st->sb; | |
7232 | struct imsm_update_takeover *u; | |
7233 | ||
7234 | u = malloc(sizeof(struct imsm_update_takeover)); | |
7235 | if (u == NULL) | |
7236 | return 1; | |
7237 | ||
7238 | u->type = update_takeover; | |
7239 | u->subarray = super->current_vol; | |
7240 | ||
7241 | /* 10->0 transition */ | |
7242 | if (geo->level == 0) | |
7243 | u->direction = R10_TO_R0; | |
7244 | ||
0529c688 KW |
7245 | /* 0->10 transition */ |
7246 | if (geo->level == 10) | |
7247 | u->direction = R0_TO_R10; | |
7248 | ||
bb025c2f KW |
7249 | /* update metadata locally */ |
7250 | imsm_update_metadata_locally(st, u, | |
7251 | sizeof(struct imsm_update_takeover)); | |
7252 | /* and possibly remotely */ | |
7253 | if (st->update_tail) | |
7254 | append_metadata_update(st, u, | |
7255 | sizeof(struct imsm_update_takeover)); | |
7256 | else | |
7257 | free(u); | |
7258 | ||
7259 | return 0; | |
7260 | } | |
7261 | ||
78b10e66 N |
7262 | static int imsm_reshape_super(struct supertype *st, long long size, int level, |
7263 | int layout, int chunksize, int raid_disks, | |
41784c88 AK |
7264 | int delta_disks, char *backup, char *dev, |
7265 | int verbose) | |
78b10e66 | 7266 | { |
78b10e66 N |
7267 | int ret_val = 1; |
7268 | struct geo_params geo; | |
7269 | ||
7270 | dprintf("imsm: reshape_super called.\n"); | |
7271 | ||
71204a50 | 7272 | memset(&geo, 0, sizeof(struct geo_params)); |
78b10e66 N |
7273 | |
7274 | geo.dev_name = dev; | |
694575e7 | 7275 | geo.dev_id = st->devnum; |
78b10e66 N |
7276 | geo.size = size; |
7277 | geo.level = level; | |
7278 | geo.layout = layout; | |
7279 | geo.chunksize = chunksize; | |
7280 | geo.raid_disks = raid_disks; | |
41784c88 AK |
7281 | if (delta_disks != UnSet) |
7282 | geo.raid_disks += delta_disks; | |
78b10e66 N |
7283 | |
7284 | dprintf("\tfor level : %i\n", geo.level); | |
7285 | dprintf("\tfor raid_disks : %i\n", geo.raid_disks); | |
7286 | ||
7287 | if (experimental() == 0) | |
7288 | return ret_val; | |
7289 | ||
78b10e66 | 7290 | if (st->container_dev == st->devnum) { |
694575e7 KW |
7291 | /* On container level we can only increase number of devices. */ |
7292 | dprintf("imsm: info: Container operation\n"); | |
78b10e66 N |
7293 | int old_raid_disks = 0; |
7294 | if (imsm_reshape_is_allowed_on_container( | |
7295 | st, &geo, &old_raid_disks)) { | |
7296 | struct imsm_update_reshape *u = NULL; | |
7297 | int len; | |
7298 | ||
7299 | len = imsm_create_metadata_update_for_reshape( | |
7300 | st, &geo, old_raid_disks, &u); | |
7301 | ||
ed08d51c AK |
7302 | if (len <= 0) { |
7303 | dprintf("imsm: Cannot prepare update\n"); | |
7304 | goto exit_imsm_reshape_super; | |
7305 | } | |
7306 | ||
8dd70bce AK |
7307 | ret_val = 0; |
7308 | /* update metadata locally */ | |
7309 | imsm_update_metadata_locally(st, u, len); | |
7310 | /* and possibly remotely */ | |
7311 | if (st->update_tail) | |
7312 | append_metadata_update(st, u, len); | |
7313 | else | |
ed08d51c | 7314 | free(u); |
8dd70bce | 7315 | |
694575e7 | 7316 | } else { |
e7ff7e40 AK |
7317 | fprintf(stderr, Name ": (imsm) Operation " |
7318 | "is not allowed on this container\n"); | |
694575e7 KW |
7319 | } |
7320 | } else { | |
7321 | /* On volume level we support following operations | |
471bceb6 KW |
7322 | * - takeover: raid10 -> raid0; raid0 -> raid10 |
7323 | * - chunk size migration | |
7324 | * - migration: raid5 -> raid0; raid0 -> raid5 | |
7325 | */ | |
7326 | struct intel_super *super = st->sb; | |
7327 | struct intel_dev *dev = super->devlist; | |
7328 | int change, devnum; | |
694575e7 | 7329 | dprintf("imsm: info: Volume operation\n"); |
471bceb6 KW |
7330 | /* find requested device */ |
7331 | while (dev) { | |
7332 | imsm_find_array_minor_by_subdev(dev->index, st->container_dev, &devnum); | |
7333 | if (devnum == geo.dev_id) | |
7334 | break; | |
7335 | dev = dev->next; | |
7336 | } | |
7337 | if (dev == NULL) { | |
7338 | fprintf(stderr, Name " Cannot find %s (%i) subarray\n", | |
7339 | geo.dev_name, geo.dev_id); | |
7340 | goto exit_imsm_reshape_super; | |
7341 | } | |
7342 | super->current_vol = dev->index; | |
694575e7 KW |
7343 | change = imsm_analyze_change(st, &geo); |
7344 | switch (change) { | |
471bceb6 | 7345 | case CH_TAKEOVER: |
bb025c2f | 7346 | ret_val = imsm_takeover(st, &geo); |
694575e7 | 7347 | break; |
b5347799 | 7348 | case CH_MIGRATION: |
471bceb6 | 7349 | ret_val = 0; |
694575e7 | 7350 | break; |
471bceb6 KW |
7351 | default: |
7352 | ret_val = 1; | |
694575e7 | 7353 | } |
694575e7 | 7354 | } |
78b10e66 | 7355 | |
ed08d51c | 7356 | exit_imsm_reshape_super: |
78b10e66 N |
7357 | dprintf("imsm: reshape_super Exit code = %i\n", ret_val); |
7358 | return ret_val; | |
7359 | } | |
2cda7640 | 7360 | |
999b4972 N |
7361 | static int imsm_manage_reshape( |
7362 | int afd, struct mdinfo *sra, struct reshape *reshape, | |
7363 | struct supertype *st, unsigned long stripes, | |
7364 | int *fds, unsigned long long *offsets, | |
7365 | int dests, int *destfd, unsigned long long *destoffsets) | |
7366 | { | |
7367 | /* Just use child_monitor for now */ | |
7368 | return child_monitor( | |
7369 | afd, sra, reshape, st, stripes, | |
7370 | fds, offsets, dests, destfd, destoffsets); | |
7371 | } | |
71204a50 | 7372 | #endif /* MDASSEMBLE */ |
999b4972 | 7373 | |
cdddbdbc DW |
7374 | struct superswitch super_imsm = { |
7375 | #ifndef MDASSEMBLE | |
7376 | .examine_super = examine_super_imsm, | |
7377 | .brief_examine_super = brief_examine_super_imsm, | |
4737ae25 | 7378 | .brief_examine_subarrays = brief_examine_subarrays_imsm, |
9d84c8ea | 7379 | .export_examine_super = export_examine_super_imsm, |
cdddbdbc DW |
7380 | .detail_super = detail_super_imsm, |
7381 | .brief_detail_super = brief_detail_super_imsm, | |
bf5a934a | 7382 | .write_init_super = write_init_super_imsm, |
0e600426 N |
7383 | .validate_geometry = validate_geometry_imsm, |
7384 | .add_to_super = add_to_super_imsm, | |
1a64be56 | 7385 | .remove_from_super = remove_from_super_imsm, |
d665cc31 | 7386 | .detail_platform = detail_platform_imsm, |
33414a01 | 7387 | .kill_subarray = kill_subarray_imsm, |
aa534678 | 7388 | .update_subarray = update_subarray_imsm, |
2b959fbf | 7389 | .load_container = load_container_imsm, |
71204a50 N |
7390 | .default_geometry = default_geometry_imsm, |
7391 | .get_disk_controller_domain = imsm_get_disk_controller_domain, | |
7392 | .reshape_super = imsm_reshape_super, | |
7393 | .manage_reshape = imsm_manage_reshape, | |
cdddbdbc DW |
7394 | #endif |
7395 | .match_home = match_home_imsm, | |
7396 | .uuid_from_super= uuid_from_super_imsm, | |
7397 | .getinfo_super = getinfo_super_imsm, | |
5c4cd5da | 7398 | .getinfo_super_disks = getinfo_super_disks_imsm, |
cdddbdbc DW |
7399 | .update_super = update_super_imsm, |
7400 | ||
7401 | .avail_size = avail_size_imsm, | |
80e7f8c3 | 7402 | .min_acceptable_spare_size = min_acceptable_spare_size_imsm, |
cdddbdbc DW |
7403 | |
7404 | .compare_super = compare_super_imsm, | |
7405 | ||
7406 | .load_super = load_super_imsm, | |
bf5a934a | 7407 | .init_super = init_super_imsm, |
e683ca88 | 7408 | .store_super = store_super_imsm, |
cdddbdbc DW |
7409 | .free_super = free_super_imsm, |
7410 | .match_metadata_desc = match_metadata_desc_imsm, | |
bf5a934a | 7411 | .container_content = container_content_imsm, |
cdddbdbc | 7412 | |
cdddbdbc | 7413 | .external = 1, |
4cce4069 | 7414 | .name = "imsm", |
845dea95 | 7415 | |
0e600426 | 7416 | #ifndef MDASSEMBLE |
845dea95 NB |
7417 | /* for mdmon */ |
7418 | .open_new = imsm_open_new, | |
ed9d66aa | 7419 | .set_array_state= imsm_set_array_state, |
845dea95 NB |
7420 | .set_disk = imsm_set_disk, |
7421 | .sync_metadata = imsm_sync_metadata, | |
88758e9d | 7422 | .activate_spare = imsm_activate_spare, |
e8319a19 | 7423 | .process_update = imsm_process_update, |
8273f55e | 7424 | .prepare_update = imsm_prepare_update, |
0e600426 | 7425 | #endif /* MDASSEMBLE */ |
cdddbdbc | 7426 | }; |