]>
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 | |
cdddbdbc DW |
56 | |
57 | /* Disk configuration info. */ | |
58 | #define IMSM_MAX_DEVICES 255 | |
59 | struct imsm_disk { | |
60 | __u8 serial[MAX_RAID_SERIAL_LEN];/* 0xD8 - 0xE7 ascii serial number */ | |
61 | __u32 total_blocks; /* 0xE8 - 0xEB total blocks */ | |
62 | __u32 scsi_id; /* 0xEC - 0xEF scsi ID */ | |
f2f27e63 DW |
63 | #define SPARE_DISK __cpu_to_le32(0x01) /* Spare */ |
64 | #define CONFIGURED_DISK __cpu_to_le32(0x02) /* Member of some RaidDev */ | |
65 | #define FAILED_DISK __cpu_to_le32(0x04) /* Permanent failure */ | |
66 | #define USABLE_DISK __cpu_to_le32(0x08) /* Fully usable unless FAILED_DISK is set */ | |
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 */ | |
91 | __u8 reserved[1]; | |
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 | |
cdddbdbc DW |
108 | __u8 migr_type; /* Initializing, Rebuilding, ... */ |
109 | __u8 dirty; | |
fe7ed8cb DW |
110 | __u8 fs_state; /* fast-sync state for CnG (0xff == disabled) */ |
111 | __u16 verify_errors; /* number of mismatches */ | |
112 | __u16 bad_blocks; /* number of bad blocks during verify */ | |
113 | __u32 filler[4]; | |
cdddbdbc DW |
114 | struct imsm_map map[1]; |
115 | /* here comes another one if migr_state */ | |
116 | } __attribute__ ((packed)); | |
117 | ||
118 | struct imsm_dev { | |
fe7ed8cb | 119 | __u8 volume[MAX_RAID_SERIAL_LEN]; |
cdddbdbc DW |
120 | __u32 size_low; |
121 | __u32 size_high; | |
fe7ed8cb DW |
122 | #define DEV_BOOTABLE __cpu_to_le32(0x01) |
123 | #define DEV_BOOT_DEVICE __cpu_to_le32(0x02) | |
124 | #define DEV_READ_COALESCING __cpu_to_le32(0x04) | |
125 | #define DEV_WRITE_COALESCING __cpu_to_le32(0x08) | |
126 | #define DEV_LAST_SHUTDOWN_DIRTY __cpu_to_le32(0x10) | |
127 | #define DEV_HIDDEN_AT_BOOT __cpu_to_le32(0x20) | |
128 | #define DEV_CURRENTLY_HIDDEN __cpu_to_le32(0x40) | |
129 | #define DEV_VERIFY_AND_FIX __cpu_to_le32(0x80) | |
130 | #define DEV_MAP_STATE_UNINIT __cpu_to_le32(0x100) | |
131 | #define DEV_NO_AUTO_RECOVERY __cpu_to_le32(0x200) | |
132 | #define DEV_CLONE_N_GO __cpu_to_le32(0x400) | |
133 | #define DEV_CLONE_MAN_SYNC __cpu_to_le32(0x800) | |
134 | #define DEV_CNG_MASTER_DISK_NUM __cpu_to_le32(0x1000) | |
cdddbdbc DW |
135 | __u32 status; /* Persistent RaidDev status */ |
136 | __u32 reserved_blocks; /* Reserved blocks at beginning of volume */ | |
fe7ed8cb DW |
137 | __u8 migr_priority; |
138 | __u8 num_sub_vols; | |
139 | __u8 tid; | |
140 | __u8 cng_master_disk; | |
141 | __u16 cache_policy; | |
142 | __u8 cng_state; | |
143 | __u8 cng_sub_state; | |
144 | #define IMSM_DEV_FILLERS 10 | |
cdddbdbc DW |
145 | __u32 filler[IMSM_DEV_FILLERS]; |
146 | struct imsm_vol vol; | |
147 | } __attribute__ ((packed)); | |
148 | ||
149 | struct imsm_super { | |
150 | __u8 sig[MAX_SIGNATURE_LENGTH]; /* 0x00 - 0x1F */ | |
151 | __u32 check_sum; /* 0x20 - 0x23 MPB Checksum */ | |
152 | __u32 mpb_size; /* 0x24 - 0x27 Size of MPB */ | |
153 | __u32 family_num; /* 0x28 - 0x2B Checksum from first time this config was written */ | |
154 | __u32 generation_num; /* 0x2C - 0x2F Incremented each time this array's MPB is written */ | |
604b746f JD |
155 | __u32 error_log_size; /* 0x30 - 0x33 in bytes */ |
156 | __u32 attributes; /* 0x34 - 0x37 */ | |
cdddbdbc DW |
157 | __u8 num_disks; /* 0x38 Number of configured disks */ |
158 | __u8 num_raid_devs; /* 0x39 Number of configured volumes */ | |
604b746f JD |
159 | __u8 error_log_pos; /* 0x3A */ |
160 | __u8 fill[1]; /* 0x3B */ | |
161 | __u32 cache_size; /* 0x3c - 0x40 in mb */ | |
162 | __u32 orig_family_num; /* 0x40 - 0x43 original family num */ | |
163 | __u32 pwr_cycle_count; /* 0x44 - 0x47 simulated power cycle count for array */ | |
164 | __u32 bbm_log_size; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */ | |
165 | #define IMSM_FILLERS 35 | |
166 | __u32 filler[IMSM_FILLERS]; /* 0x4C - 0xD7 RAID_MPB_FILLERS */ | |
cdddbdbc DW |
167 | struct imsm_disk disk[1]; /* 0xD8 diskTbl[numDisks] */ |
168 | /* here comes imsm_dev[num_raid_devs] */ | |
604b746f | 169 | /* here comes BBM logs */ |
cdddbdbc DW |
170 | } __attribute__ ((packed)); |
171 | ||
604b746f JD |
172 | #define BBM_LOG_MAX_ENTRIES 254 |
173 | ||
174 | struct bbm_log_entry { | |
175 | __u64 defective_block_start; | |
176 | #define UNREADABLE 0xFFFFFFFF | |
177 | __u32 spare_block_offset; | |
178 | __u16 remapped_marked_count; | |
179 | __u16 disk_ordinal; | |
180 | } __attribute__ ((__packed__)); | |
181 | ||
182 | struct bbm_log { | |
183 | __u32 signature; /* 0xABADB10C */ | |
184 | __u32 entry_count; | |
185 | __u32 reserved_spare_block_count; /* 0 */ | |
186 | __u32 reserved; /* 0xFFFF */ | |
187 | __u64 first_spare_lba; | |
188 | struct bbm_log_entry mapped_block_entries[BBM_LOG_MAX_ENTRIES]; | |
189 | } __attribute__ ((__packed__)); | |
190 | ||
191 | ||
cdddbdbc DW |
192 | #ifndef MDASSEMBLE |
193 | static char *map_state_str[] = { "normal", "uninitialized", "degraded", "failed" }; | |
194 | #endif | |
195 | ||
87eb16df | 196 | static unsigned int sector_count(__u32 bytes) |
cdddbdbc | 197 | { |
87eb16df DW |
198 | return ((bytes + (512-1)) & (~(512-1))) / 512; |
199 | } | |
cdddbdbc | 200 | |
87eb16df DW |
201 | static unsigned int mpb_sectors(struct imsm_super *mpb) |
202 | { | |
203 | return sector_count(__le32_to_cpu(mpb->mpb_size)); | |
cdddbdbc DW |
204 | } |
205 | ||
ba2de7ba DW |
206 | struct intel_dev { |
207 | struct imsm_dev *dev; | |
208 | struct intel_dev *next; | |
209 | int index; | |
210 | }; | |
211 | ||
cdddbdbc DW |
212 | /* internal representation of IMSM metadata */ |
213 | struct intel_super { | |
214 | union { | |
949c47a0 DW |
215 | void *buf; /* O_DIRECT buffer for reading/writing metadata */ |
216 | struct imsm_super *anchor; /* immovable parameters */ | |
cdddbdbc | 217 | }; |
949c47a0 | 218 | size_t len; /* size of the 'buf' allocation */ |
4d7b1503 DW |
219 | void *next_buf; /* for realloc'ing buf from the manager */ |
220 | size_t next_len; | |
c2c087e6 DW |
221 | int updates_pending; /* count of pending updates for mdmon */ |
222 | int creating_imsm; /* flag to indicate container creation */ | |
bf5a934a | 223 | int current_vol; /* index of raid device undergoing creation */ |
0dcecb2e | 224 | __u32 create_offset; /* common start for 'current_vol' */ |
ba2de7ba | 225 | struct intel_dev *devlist; |
cdddbdbc DW |
226 | struct dl { |
227 | struct dl *next; | |
228 | int index; | |
229 | __u8 serial[MAX_RAID_SERIAL_LEN]; | |
230 | int major, minor; | |
231 | char *devname; | |
b9f594fe | 232 | struct imsm_disk disk; |
cdddbdbc | 233 | int fd; |
0dcecb2e DW |
234 | int extent_cnt; |
235 | struct extent *e; /* for determining freespace @ create */ | |
efb30e7f | 236 | int raiddisk; /* slot to fill in autolayout */ |
cdddbdbc | 237 | } *disks; |
43dad3d6 | 238 | struct dl *add; /* list of disks to add while mdmon active */ |
47ee5a45 | 239 | struct dl *missing; /* disks removed while we weren't looking */ |
43dad3d6 | 240 | struct bbm_log *bbm_log; |
88c32bb1 DW |
241 | const char *hba; /* device path of the raid controller for this metadata */ |
242 | const struct imsm_orom *orom; /* platform firmware support */ | |
cdddbdbc DW |
243 | }; |
244 | ||
c2c087e6 DW |
245 | struct extent { |
246 | unsigned long long start, size; | |
247 | }; | |
248 | ||
88758e9d DW |
249 | /* definition of messages passed to imsm_process_update */ |
250 | enum imsm_update_type { | |
251 | update_activate_spare, | |
8273f55e | 252 | update_create_array, |
43dad3d6 | 253 | update_add_disk, |
88758e9d DW |
254 | }; |
255 | ||
256 | struct imsm_update_activate_spare { | |
257 | enum imsm_update_type type; | |
d23fe947 | 258 | struct dl *dl; |
88758e9d DW |
259 | int slot; |
260 | int array; | |
261 | struct imsm_update_activate_spare *next; | |
262 | }; | |
263 | ||
54c2c1ea DW |
264 | struct disk_info { |
265 | __u8 serial[MAX_RAID_SERIAL_LEN]; | |
266 | }; | |
267 | ||
8273f55e DW |
268 | struct imsm_update_create_array { |
269 | enum imsm_update_type type; | |
8273f55e | 270 | int dev_idx; |
6a3e913e | 271 | struct imsm_dev dev; |
8273f55e DW |
272 | }; |
273 | ||
43dad3d6 DW |
274 | struct imsm_update_add_disk { |
275 | enum imsm_update_type type; | |
276 | }; | |
277 | ||
cdddbdbc DW |
278 | static struct supertype *match_metadata_desc_imsm(char *arg) |
279 | { | |
280 | struct supertype *st; | |
281 | ||
282 | if (strcmp(arg, "imsm") != 0 && | |
283 | strcmp(arg, "default") != 0 | |
284 | ) | |
285 | return NULL; | |
286 | ||
287 | st = malloc(sizeof(*st)); | |
ef609477 | 288 | memset(st, 0, sizeof(*st)); |
cdddbdbc DW |
289 | st->ss = &super_imsm; |
290 | st->max_devs = IMSM_MAX_DEVICES; | |
291 | st->minor_version = 0; | |
292 | st->sb = NULL; | |
293 | return st; | |
294 | } | |
295 | ||
0e600426 | 296 | #ifndef MDASSEMBLE |
cdddbdbc DW |
297 | static __u8 *get_imsm_version(struct imsm_super *mpb) |
298 | { | |
299 | return &mpb->sig[MPB_SIG_LEN]; | |
300 | } | |
0e600426 | 301 | #endif |
cdddbdbc | 302 | |
949c47a0 DW |
303 | /* retrieve a disk directly from the anchor when the anchor is known to be |
304 | * up-to-date, currently only at load time | |
305 | */ | |
306 | static struct imsm_disk *__get_imsm_disk(struct imsm_super *mpb, __u8 index) | |
cdddbdbc | 307 | { |
949c47a0 | 308 | if (index >= mpb->num_disks) |
cdddbdbc DW |
309 | return NULL; |
310 | return &mpb->disk[index]; | |
311 | } | |
312 | ||
0e600426 | 313 | #ifndef MDASSEMBLE |
b9f594fe | 314 | /* retrieve a disk from the parsed metadata */ |
949c47a0 DW |
315 | static struct imsm_disk *get_imsm_disk(struct intel_super *super, __u8 index) |
316 | { | |
b9f594fe DW |
317 | struct dl *d; |
318 | ||
319 | for (d = super->disks; d; d = d->next) | |
320 | if (d->index == index) | |
321 | return &d->disk; | |
322 | ||
323 | return NULL; | |
949c47a0 | 324 | } |
0e600426 | 325 | #endif |
949c47a0 DW |
326 | |
327 | /* generate a checksum directly from the anchor when the anchor is known to be | |
328 | * up-to-date, currently only at load or write_super after coalescing | |
329 | */ | |
330 | static __u32 __gen_imsm_checksum(struct imsm_super *mpb) | |
cdddbdbc DW |
331 | { |
332 | __u32 end = mpb->mpb_size / sizeof(end); | |
333 | __u32 *p = (__u32 *) mpb; | |
334 | __u32 sum = 0; | |
335 | ||
97f734fd N |
336 | while (end--) { |
337 | sum += __le32_to_cpu(*p); | |
338 | p++; | |
339 | } | |
cdddbdbc DW |
340 | |
341 | return sum - __le32_to_cpu(mpb->check_sum); | |
342 | } | |
343 | ||
a965f303 DW |
344 | static size_t sizeof_imsm_map(struct imsm_map *map) |
345 | { | |
346 | return sizeof(struct imsm_map) + sizeof(__u32) * (map->num_members - 1); | |
347 | } | |
348 | ||
349 | struct imsm_map *get_imsm_map(struct imsm_dev *dev, int second_map) | |
cdddbdbc | 350 | { |
a965f303 DW |
351 | struct imsm_map *map = &dev->vol.map[0]; |
352 | ||
353 | if (second_map && !dev->vol.migr_state) | |
354 | return NULL; | |
355 | else if (second_map) { | |
356 | void *ptr = map; | |
357 | ||
358 | return ptr + sizeof_imsm_map(map); | |
359 | } else | |
360 | return map; | |
361 | ||
362 | } | |
cdddbdbc | 363 | |
3393c6af DW |
364 | /* return the size of the device. |
365 | * migr_state increases the returned size if map[0] were to be duplicated | |
366 | */ | |
367 | static size_t sizeof_imsm_dev(struct imsm_dev *dev, int migr_state) | |
a965f303 DW |
368 | { |
369 | size_t size = sizeof(*dev) - sizeof(struct imsm_map) + | |
370 | sizeof_imsm_map(get_imsm_map(dev, 0)); | |
cdddbdbc DW |
371 | |
372 | /* migrating means an additional map */ | |
a965f303 DW |
373 | if (dev->vol.migr_state) |
374 | size += sizeof_imsm_map(get_imsm_map(dev, 1)); | |
3393c6af DW |
375 | else if (migr_state) |
376 | size += sizeof_imsm_map(get_imsm_map(dev, 0)); | |
cdddbdbc DW |
377 | |
378 | return size; | |
379 | } | |
380 | ||
54c2c1ea DW |
381 | #ifndef MDASSEMBLE |
382 | /* retrieve disk serial number list from a metadata update */ | |
383 | static struct disk_info *get_disk_info(struct imsm_update_create_array *update) | |
384 | { | |
385 | void *u = update; | |
386 | struct disk_info *inf; | |
387 | ||
388 | inf = u + sizeof(*update) - sizeof(struct imsm_dev) + | |
389 | sizeof_imsm_dev(&update->dev, 0); | |
390 | ||
391 | return inf; | |
392 | } | |
393 | #endif | |
394 | ||
949c47a0 | 395 | static struct imsm_dev *__get_imsm_dev(struct imsm_super *mpb, __u8 index) |
cdddbdbc DW |
396 | { |
397 | int offset; | |
398 | int i; | |
399 | void *_mpb = mpb; | |
400 | ||
949c47a0 | 401 | if (index >= mpb->num_raid_devs) |
cdddbdbc DW |
402 | return NULL; |
403 | ||
404 | /* devices start after all disks */ | |
405 | offset = ((void *) &mpb->disk[mpb->num_disks]) - _mpb; | |
406 | ||
407 | for (i = 0; i <= index; i++) | |
408 | if (i == index) | |
409 | return _mpb + offset; | |
410 | else | |
3393c6af | 411 | offset += sizeof_imsm_dev(_mpb + offset, 0); |
cdddbdbc DW |
412 | |
413 | return NULL; | |
414 | } | |
415 | ||
949c47a0 DW |
416 | static struct imsm_dev *get_imsm_dev(struct intel_super *super, __u8 index) |
417 | { | |
ba2de7ba DW |
418 | struct intel_dev *dv; |
419 | ||
949c47a0 DW |
420 | if (index >= super->anchor->num_raid_devs) |
421 | return NULL; | |
ba2de7ba DW |
422 | for (dv = super->devlist; dv; dv = dv->next) |
423 | if (dv->index == index) | |
424 | return dv->dev; | |
425 | return NULL; | |
949c47a0 DW |
426 | } |
427 | ||
7eef0453 DW |
428 | static __u32 get_imsm_ord_tbl_ent(struct imsm_dev *dev, int slot) |
429 | { | |
430 | struct imsm_map *map; | |
431 | ||
432 | if (dev->vol.migr_state) | |
7eef0453 | 433 | map = get_imsm_map(dev, 1); |
fb9bf0d3 DW |
434 | else |
435 | map = get_imsm_map(dev, 0); | |
7eef0453 | 436 | |
ff077194 DW |
437 | /* top byte identifies disk under rebuild */ |
438 | return __le32_to_cpu(map->disk_ord_tbl[slot]); | |
439 | } | |
440 | ||
441 | #define ord_to_idx(ord) (((ord) << 8) >> 8) | |
442 | static __u32 get_imsm_disk_idx(struct imsm_dev *dev, int slot) | |
443 | { | |
444 | __u32 ord = get_imsm_ord_tbl_ent(dev, slot); | |
445 | ||
446 | return ord_to_idx(ord); | |
7eef0453 DW |
447 | } |
448 | ||
be73972f DW |
449 | static void set_imsm_ord_tbl_ent(struct imsm_map *map, int slot, __u32 ord) |
450 | { | |
451 | map->disk_ord_tbl[slot] = __cpu_to_le32(ord); | |
452 | } | |
453 | ||
620b1713 DW |
454 | static int get_imsm_disk_slot(struct imsm_map *map, int idx) |
455 | { | |
456 | int slot; | |
457 | __u32 ord; | |
458 | ||
459 | for (slot = 0; slot < map->num_members; slot++) { | |
460 | ord = __le32_to_cpu(map->disk_ord_tbl[slot]); | |
461 | if (ord_to_idx(ord) == idx) | |
462 | return slot; | |
463 | } | |
464 | ||
465 | return -1; | |
466 | } | |
467 | ||
cdddbdbc DW |
468 | static int get_imsm_raid_level(struct imsm_map *map) |
469 | { | |
470 | if (map->raid_level == 1) { | |
471 | if (map->num_members == 2) | |
472 | return 1; | |
473 | else | |
474 | return 10; | |
475 | } | |
476 | ||
477 | return map->raid_level; | |
478 | } | |
479 | ||
c2c087e6 DW |
480 | static int cmp_extent(const void *av, const void *bv) |
481 | { | |
482 | const struct extent *a = av; | |
483 | const struct extent *b = bv; | |
484 | if (a->start < b->start) | |
485 | return -1; | |
486 | if (a->start > b->start) | |
487 | return 1; | |
488 | return 0; | |
489 | } | |
490 | ||
0dcecb2e | 491 | static int count_memberships(struct dl *dl, struct intel_super *super) |
c2c087e6 | 492 | { |
c2c087e6 | 493 | int memberships = 0; |
620b1713 | 494 | int i; |
c2c087e6 | 495 | |
949c47a0 DW |
496 | for (i = 0; i < super->anchor->num_raid_devs; i++) { |
497 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
a965f303 | 498 | struct imsm_map *map = get_imsm_map(dev, 0); |
c2c087e6 | 499 | |
620b1713 DW |
500 | if (get_imsm_disk_slot(map, dl->index) >= 0) |
501 | memberships++; | |
c2c087e6 | 502 | } |
0dcecb2e DW |
503 | |
504 | return memberships; | |
505 | } | |
506 | ||
507 | static struct extent *get_extents(struct intel_super *super, struct dl *dl) | |
508 | { | |
509 | /* find a list of used extents on the given physical device */ | |
510 | struct extent *rv, *e; | |
620b1713 | 511 | int i; |
0dcecb2e DW |
512 | int memberships = count_memberships(dl, super); |
513 | __u32 reservation = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS; | |
514 | ||
c2c087e6 DW |
515 | rv = malloc(sizeof(struct extent) * (memberships + 1)); |
516 | if (!rv) | |
517 | return NULL; | |
518 | e = rv; | |
519 | ||
949c47a0 DW |
520 | for (i = 0; i < super->anchor->num_raid_devs; i++) { |
521 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
a965f303 | 522 | struct imsm_map *map = get_imsm_map(dev, 0); |
c2c087e6 | 523 | |
620b1713 DW |
524 | if (get_imsm_disk_slot(map, dl->index) >= 0) { |
525 | e->start = __le32_to_cpu(map->pba_of_lba0); | |
526 | e->size = __le32_to_cpu(map->blocks_per_member); | |
527 | e++; | |
c2c087e6 DW |
528 | } |
529 | } | |
530 | qsort(rv, memberships, sizeof(*rv), cmp_extent); | |
531 | ||
14e8215b DW |
532 | /* determine the start of the metadata |
533 | * when no raid devices are defined use the default | |
534 | * ...otherwise allow the metadata to truncate the value | |
535 | * as is the case with older versions of imsm | |
536 | */ | |
537 | if (memberships) { | |
538 | struct extent *last = &rv[memberships - 1]; | |
539 | __u32 remainder; | |
540 | ||
541 | remainder = __le32_to_cpu(dl->disk.total_blocks) - | |
542 | (last->start + last->size); | |
dda5855f DW |
543 | /* round down to 1k block to satisfy precision of the kernel |
544 | * 'size' interface | |
545 | */ | |
546 | remainder &= ~1UL; | |
547 | /* make sure remainder is still sane */ | |
548 | if (remainder < ROUND_UP(super->len, 512) >> 9) | |
549 | remainder = ROUND_UP(super->len, 512) >> 9; | |
14e8215b DW |
550 | if (reservation > remainder) |
551 | reservation = remainder; | |
552 | } | |
553 | e->start = __le32_to_cpu(dl->disk.total_blocks) - reservation; | |
c2c087e6 DW |
554 | e->size = 0; |
555 | return rv; | |
556 | } | |
557 | ||
14e8215b DW |
558 | /* try to determine how much space is reserved for metadata from |
559 | * the last get_extents() entry, otherwise fallback to the | |
560 | * default | |
561 | */ | |
562 | static __u32 imsm_reserved_sectors(struct intel_super *super, struct dl *dl) | |
563 | { | |
564 | struct extent *e; | |
565 | int i; | |
566 | __u32 rv; | |
567 | ||
568 | /* for spares just return a minimal reservation which will grow | |
569 | * once the spare is picked up by an array | |
570 | */ | |
571 | if (dl->index == -1) | |
572 | return MPB_SECTOR_CNT; | |
573 | ||
574 | e = get_extents(super, dl); | |
575 | if (!e) | |
576 | return MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS; | |
577 | ||
578 | /* scroll to last entry */ | |
579 | for (i = 0; e[i].size; i++) | |
580 | continue; | |
581 | ||
582 | rv = __le32_to_cpu(dl->disk.total_blocks) - e[i].start; | |
583 | ||
584 | free(e); | |
585 | ||
586 | return rv; | |
587 | } | |
588 | ||
589 | #ifndef MDASSEMBLE | |
44470971 | 590 | static void print_imsm_dev(struct imsm_dev *dev, char *uuid, int disk_idx) |
cdddbdbc DW |
591 | { |
592 | __u64 sz; | |
593 | int slot; | |
a965f303 | 594 | struct imsm_map *map = get_imsm_map(dev, 0); |
b10b37b8 | 595 | __u32 ord; |
cdddbdbc DW |
596 | |
597 | printf("\n"); | |
1e7bc0ed | 598 | printf("[%.16s]:\n", dev->volume); |
44470971 | 599 | printf(" UUID : %s\n", uuid); |
cdddbdbc DW |
600 | printf(" RAID Level : %d\n", get_imsm_raid_level(map)); |
601 | printf(" Members : %d\n", map->num_members); | |
620b1713 DW |
602 | slot = get_imsm_disk_slot(map, disk_idx); |
603 | if (slot >= 0) { | |
b10b37b8 DW |
604 | ord = get_imsm_ord_tbl_ent(dev, slot); |
605 | printf(" This Slot : %d%s\n", slot, | |
606 | ord & IMSM_ORD_REBUILD ? " (out-of-sync)" : ""); | |
607 | } else | |
cdddbdbc DW |
608 | printf(" This Slot : ?\n"); |
609 | sz = __le32_to_cpu(dev->size_high); | |
610 | sz <<= 32; | |
611 | sz += __le32_to_cpu(dev->size_low); | |
612 | printf(" Array Size : %llu%s\n", (unsigned long long)sz, | |
613 | human_size(sz * 512)); | |
614 | sz = __le32_to_cpu(map->blocks_per_member); | |
615 | printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz, | |
616 | human_size(sz * 512)); | |
617 | printf(" Sector Offset : %u\n", | |
618 | __le32_to_cpu(map->pba_of_lba0)); | |
619 | printf(" Num Stripes : %u\n", | |
620 | __le32_to_cpu(map->num_data_stripes)); | |
621 | printf(" Chunk Size : %u KiB\n", | |
622 | __le16_to_cpu(map->blocks_per_strip) / 2); | |
623 | printf(" Reserved : %d\n", __le32_to_cpu(dev->reserved_blocks)); | |
3393c6af DW |
624 | printf(" Migrate State : %s", dev->vol.migr_state ? "migrating" : "idle"); |
625 | if (dev->vol.migr_state) | |
626 | printf(": %s", dev->vol.migr_type ? "rebuilding" : "initializing"); | |
627 | printf("\n"); | |
628 | printf(" Map State : %s", map_state_str[map->map_state]); | |
629 | if (dev->vol.migr_state) { | |
630 | struct imsm_map *map = get_imsm_map(dev, 1); | |
b10b37b8 | 631 | printf(" <-- %s", map_state_str[map->map_state]); |
3393c6af DW |
632 | } |
633 | printf("\n"); | |
cdddbdbc | 634 | printf(" Dirty State : %s\n", dev->vol.dirty ? "dirty" : "clean"); |
cdddbdbc DW |
635 | } |
636 | ||
14e8215b | 637 | static void print_imsm_disk(struct imsm_super *mpb, int index, __u32 reserved) |
cdddbdbc | 638 | { |
949c47a0 | 639 | struct imsm_disk *disk = __get_imsm_disk(mpb, index); |
1f24f035 | 640 | char str[MAX_RAID_SERIAL_LEN + 1]; |
cdddbdbc DW |
641 | __u32 s; |
642 | __u64 sz; | |
643 | ||
e9d82038 DW |
644 | if (index < 0) |
645 | return; | |
646 | ||
cdddbdbc | 647 | printf("\n"); |
1f24f035 | 648 | snprintf(str, MAX_RAID_SERIAL_LEN + 1, "%s", disk->serial); |
cdddbdbc | 649 | printf(" Disk%02d Serial : %s\n", index, str); |
f2f27e63 | 650 | s = disk->status; |
cdddbdbc DW |
651 | printf(" State :%s%s%s%s\n", s&SPARE_DISK ? " spare" : "", |
652 | s&CONFIGURED_DISK ? " active" : "", | |
653 | s&FAILED_DISK ? " failed" : "", | |
654 | s&USABLE_DISK ? " usable" : ""); | |
655 | printf(" Id : %08x\n", __le32_to_cpu(disk->scsi_id)); | |
14e8215b | 656 | sz = __le32_to_cpu(disk->total_blocks) - reserved; |
cdddbdbc DW |
657 | printf(" Usable Size : %llu%s\n", (unsigned long long)sz, |
658 | human_size(sz * 512)); | |
659 | } | |
660 | ||
44470971 DW |
661 | static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info); |
662 | ||
cdddbdbc DW |
663 | static void examine_super_imsm(struct supertype *st, char *homehost) |
664 | { | |
665 | struct intel_super *super = st->sb; | |
949c47a0 | 666 | struct imsm_super *mpb = super->anchor; |
cdddbdbc DW |
667 | char str[MAX_SIGNATURE_LENGTH]; |
668 | int i; | |
27fd6274 DW |
669 | struct mdinfo info; |
670 | char nbuf[64]; | |
cdddbdbc | 671 | __u32 sum; |
14e8215b | 672 | __u32 reserved = imsm_reserved_sectors(super, super->disks); |
cdddbdbc | 673 | |
27fd6274 | 674 | |
cdddbdbc DW |
675 | snprintf(str, MPB_SIG_LEN, "%s", mpb->sig); |
676 | printf(" Magic : %s\n", str); | |
677 | snprintf(str, strlen(MPB_VERSION_RAID0), "%s", get_imsm_version(mpb)); | |
678 | printf(" Version : %s\n", get_imsm_version(mpb)); | |
679 | printf(" Family : %08x\n", __le32_to_cpu(mpb->family_num)); | |
680 | printf(" Generation : %08x\n", __le32_to_cpu(mpb->generation_num)); | |
27fd6274 DW |
681 | getinfo_super_imsm(st, &info); |
682 | fname_from_uuid(st, &info, nbuf,'-'); | |
683 | printf(" UUID : %s\n", nbuf + 5); | |
cdddbdbc DW |
684 | sum = __le32_to_cpu(mpb->check_sum); |
685 | printf(" Checksum : %08x %s\n", sum, | |
949c47a0 | 686 | __gen_imsm_checksum(mpb) == sum ? "correct" : "incorrect"); |
87eb16df | 687 | printf(" MPB Sectors : %d\n", mpb_sectors(mpb)); |
cdddbdbc DW |
688 | printf(" Disks : %d\n", mpb->num_disks); |
689 | printf(" RAID Devices : %d\n", mpb->num_raid_devs); | |
14e8215b | 690 | print_imsm_disk(mpb, super->disks->index, reserved); |
604b746f JD |
691 | if (super->bbm_log) { |
692 | struct bbm_log *log = super->bbm_log; | |
693 | ||
694 | printf("\n"); | |
695 | printf("Bad Block Management Log:\n"); | |
696 | printf(" Log Size : %d\n", __le32_to_cpu(mpb->bbm_log_size)); | |
697 | printf(" Signature : %x\n", __le32_to_cpu(log->signature)); | |
698 | printf(" Entry Count : %d\n", __le32_to_cpu(log->entry_count)); | |
699 | printf(" Spare Blocks : %d\n", __le32_to_cpu(log->reserved_spare_block_count)); | |
700 | printf(" First Spare : %llx\n", __le64_to_cpu(log->first_spare_lba)); | |
701 | } | |
44470971 DW |
702 | for (i = 0; i < mpb->num_raid_devs; i++) { |
703 | struct mdinfo info; | |
704 | struct imsm_dev *dev = __get_imsm_dev(mpb, i); | |
705 | ||
706 | super->current_vol = i; | |
707 | getinfo_super_imsm(st, &info); | |
708 | fname_from_uuid(st, &info, nbuf, '-'); | |
709 | print_imsm_dev(dev, nbuf + 5, super->disks->index); | |
710 | } | |
cdddbdbc DW |
711 | for (i = 0; i < mpb->num_disks; i++) { |
712 | if (i == super->disks->index) | |
713 | continue; | |
14e8215b | 714 | print_imsm_disk(mpb, i, reserved); |
cdddbdbc DW |
715 | } |
716 | } | |
717 | ||
718 | static void brief_examine_super_imsm(struct supertype *st) | |
719 | { | |
27fd6274 | 720 | /* We just write a generic IMSM ARRAY entry */ |
ff54de6e N |
721 | struct mdinfo info; |
722 | char nbuf[64]; | |
cf8de691 | 723 | char nbuf1[64]; |
1e7bc0ed DW |
724 | struct intel_super *super = st->sb; |
725 | int i; | |
726 | ||
727 | if (!super->anchor->num_raid_devs) | |
728 | return; | |
ff54de6e N |
729 | |
730 | getinfo_super_imsm(st, &info); | |
731 | fname_from_uuid(st, &info, nbuf,'-'); | |
cf8de691 | 732 | printf("ARRAY metadata=imsm auto=md UUID=%s\n", nbuf + 5); |
1e7bc0ed DW |
733 | for (i = 0; i < super->anchor->num_raid_devs; i++) { |
734 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
735 | ||
736 | super->current_vol = i; | |
737 | getinfo_super_imsm(st, &info); | |
cf8de691 | 738 | fname_from_uuid(st, &info, nbuf1,'-'); |
44470971 DW |
739 | printf("ARRAY /dev/md/%.16s container=%s\n" |
740 | " member=%d auto=mdp UUID=%s\n", | |
cf8de691 | 741 | dev->volume, nbuf + 5, i, nbuf1 + 5); |
1e7bc0ed | 742 | } |
cdddbdbc DW |
743 | } |
744 | ||
745 | static void detail_super_imsm(struct supertype *st, char *homehost) | |
746 | { | |
3ebe00a1 DW |
747 | struct mdinfo info; |
748 | char nbuf[64]; | |
749 | ||
750 | getinfo_super_imsm(st, &info); | |
751 | fname_from_uuid(st, &info, nbuf,'-'); | |
752 | printf("\n UUID : %s\n", nbuf + 5); | |
cdddbdbc DW |
753 | } |
754 | ||
755 | static void brief_detail_super_imsm(struct supertype *st) | |
756 | { | |
ff54de6e N |
757 | struct mdinfo info; |
758 | char nbuf[64]; | |
759 | getinfo_super_imsm(st, &info); | |
760 | fname_from_uuid(st, &info, nbuf,'-'); | |
761 | printf(" UUID=%s", nbuf + 5); | |
cdddbdbc | 762 | } |
d665cc31 DW |
763 | |
764 | static int imsm_read_serial(int fd, char *devname, __u8 *serial); | |
765 | static void fd2devname(int fd, char *name); | |
766 | ||
767 | static int imsm_enumerate_ports(const char *hba_path, int port_count, int host_base, int verbose) | |
768 | { | |
769 | /* dump an unsorted list of devices attached to ahci, as well as | |
770 | * non-connected ports | |
771 | */ | |
772 | int hba_len = strlen(hba_path) + 1; | |
773 | struct dirent *ent; | |
774 | DIR *dir; | |
775 | char *path = NULL; | |
776 | int err = 0; | |
777 | unsigned long port_mask = (1 << port_count) - 1; | |
778 | ||
779 | if (port_count > sizeof(port_mask) * 8) { | |
780 | if (verbose) | |
781 | fprintf(stderr, Name ": port_count %d out of range\n", port_count); | |
782 | return 2; | |
783 | } | |
784 | ||
785 | /* scroll through /sys/dev/block looking for devices attached to | |
786 | * this hba | |
787 | */ | |
788 | dir = opendir("/sys/dev/block"); | |
789 | for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) { | |
790 | int fd; | |
791 | char model[64]; | |
792 | char vendor[64]; | |
793 | char buf[1024]; | |
794 | int major, minor; | |
795 | char *device; | |
796 | char *c; | |
797 | int port; | |
798 | int type; | |
799 | ||
800 | if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2) | |
801 | continue; | |
802 | path = devt_to_devpath(makedev(major, minor)); | |
803 | if (!path) | |
804 | continue; | |
805 | if (!path_attached_to_hba(path, hba_path)) { | |
806 | free(path); | |
807 | path = NULL; | |
808 | continue; | |
809 | } | |
810 | ||
811 | /* retrieve the scsi device type */ | |
812 | if (asprintf(&device, "/sys/dev/block/%d:%d/device/xxxxxxx", major, minor) < 0) { | |
813 | if (verbose) | |
814 | fprintf(stderr, Name ": failed to allocate 'device'\n"); | |
815 | err = 2; | |
816 | break; | |
817 | } | |
818 | sprintf(device, "/sys/dev/block/%d:%d/device/type", major, minor); | |
819 | if (load_sys(device, buf) != 0) { | |
820 | if (verbose) | |
821 | fprintf(stderr, Name ": failed to read device type for %s\n", | |
822 | path); | |
823 | err = 2; | |
824 | free(device); | |
825 | break; | |
826 | } | |
827 | type = strtoul(buf, NULL, 10); | |
828 | ||
829 | /* if it's not a disk print the vendor and model */ | |
830 | if (!(type == 0 || type == 7 || type == 14)) { | |
831 | vendor[0] = '\0'; | |
832 | model[0] = '\0'; | |
833 | sprintf(device, "/sys/dev/block/%d:%d/device/vendor", major, minor); | |
834 | if (load_sys(device, buf) == 0) { | |
835 | strncpy(vendor, buf, sizeof(vendor)); | |
836 | vendor[sizeof(vendor) - 1] = '\0'; | |
837 | c = (char *) &vendor[sizeof(vendor) - 1]; | |
838 | while (isspace(*c) || *c == '\0') | |
839 | *c-- = '\0'; | |
840 | ||
841 | } | |
842 | sprintf(device, "/sys/dev/block/%d:%d/device/model", major, minor); | |
843 | if (load_sys(device, buf) == 0) { | |
844 | strncpy(model, buf, sizeof(model)); | |
845 | model[sizeof(model) - 1] = '\0'; | |
846 | c = (char *) &model[sizeof(model) - 1]; | |
847 | while (isspace(*c) || *c == '\0') | |
848 | *c-- = '\0'; | |
849 | } | |
850 | ||
851 | if (vendor[0] && model[0]) | |
852 | sprintf(buf, "%.64s %.64s", vendor, model); | |
853 | else | |
854 | switch (type) { /* numbers from hald/linux/device.c */ | |
855 | case 1: sprintf(buf, "tape"); break; | |
856 | case 2: sprintf(buf, "printer"); break; | |
857 | case 3: sprintf(buf, "processor"); break; | |
858 | case 4: | |
859 | case 5: sprintf(buf, "cdrom"); break; | |
860 | case 6: sprintf(buf, "scanner"); break; | |
861 | case 8: sprintf(buf, "media_changer"); break; | |
862 | case 9: sprintf(buf, "comm"); break; | |
863 | case 12: sprintf(buf, "raid"); break; | |
864 | default: sprintf(buf, "unknown"); | |
865 | } | |
866 | } else | |
867 | buf[0] = '\0'; | |
868 | free(device); | |
869 | ||
870 | /* chop device path to 'host%d' and calculate the port number */ | |
871 | c = strchr(&path[hba_len], '/'); | |
872 | *c = '\0'; | |
873 | if (sscanf(&path[hba_len], "host%d", &port) == 1) | |
874 | port -= host_base; | |
875 | else { | |
876 | if (verbose) { | |
877 | *c = '/'; /* repair the full string */ | |
878 | fprintf(stderr, Name ": failed to determine port number for %s\n", | |
879 | path); | |
880 | } | |
881 | err = 2; | |
882 | break; | |
883 | } | |
884 | ||
885 | /* mark this port as used */ | |
886 | port_mask &= ~(1 << port); | |
887 | ||
888 | /* print out the device information */ | |
889 | if (buf[0]) { | |
890 | printf(" Port%d : - non-disk device (%s) -\n", port, buf); | |
891 | continue; | |
892 | } | |
893 | ||
894 | fd = dev_open(ent->d_name, O_RDONLY); | |
895 | if (fd < 0) | |
896 | printf(" Port%d : - disk info unavailable -\n", port); | |
897 | else { | |
898 | fd2devname(fd, buf); | |
899 | printf(" Port%d : %s", port, buf); | |
900 | if (imsm_read_serial(fd, NULL, (__u8 *) buf) == 0) | |
901 | printf(" (%s)\n", buf); | |
902 | else | |
903 | printf("()\n"); | |
904 | } | |
905 | close(fd); | |
906 | free(path); | |
907 | path = NULL; | |
908 | } | |
909 | if (path) | |
910 | free(path); | |
911 | if (dir) | |
912 | closedir(dir); | |
913 | if (err == 0) { | |
914 | int i; | |
915 | ||
916 | for (i = 0; i < port_count; i++) | |
917 | if (port_mask & (1 << i)) | |
918 | printf(" Port%d : - no device attached -\n", i); | |
919 | } | |
920 | ||
921 | return err; | |
922 | } | |
923 | ||
5615172f | 924 | static int detail_platform_imsm(int verbose, int enumerate_only) |
d665cc31 DW |
925 | { |
926 | /* There are two components to imsm platform support, the ahci SATA | |
927 | * controller and the option-rom. To find the SATA controller we | |
928 | * simply look in /sys/bus/pci/drivers/ahci to see if an ahci | |
929 | * controller with the Intel vendor id is present. This approach | |
930 | * allows mdadm to leverage the kernel's ahci detection logic, with the | |
931 | * caveat that if ahci.ko is not loaded mdadm will not be able to | |
932 | * detect platform raid capabilities. The option-rom resides in a | |
933 | * platform "Adapter ROM". We scan for its signature to retrieve the | |
934 | * platform capabilities. If raid support is disabled in the BIOS the | |
935 | * option-rom capability structure will not be available. | |
936 | */ | |
937 | const struct imsm_orom *orom; | |
938 | struct sys_dev *list, *hba; | |
939 | DIR *dir; | |
940 | struct dirent *ent; | |
941 | const char *hba_path; | |
942 | int host_base = 0; | |
943 | int port_count = 0; | |
944 | ||
5615172f DW |
945 | if (enumerate_only) { |
946 | if (check_env("IMSM_NO_PLATFORM") || find_imsm_orom()) | |
947 | return 0; | |
948 | return 2; | |
949 | } | |
950 | ||
d665cc31 DW |
951 | list = find_driver_devices("pci", "ahci"); |
952 | for (hba = list; hba; hba = hba->next) | |
953 | if (devpath_to_vendor(hba->path) == 0x8086) | |
954 | break; | |
955 | ||
956 | if (!hba) { | |
957 | if (verbose) | |
958 | fprintf(stderr, Name ": unable to find active ahci controller\n"); | |
959 | free_sys_dev(&list); | |
960 | return 2; | |
961 | } else if (verbose) | |
962 | fprintf(stderr, Name ": found Intel SATA AHCI Controller\n"); | |
963 | hba_path = hba->path; | |
964 | hba->path = NULL; | |
965 | free_sys_dev(&list); | |
966 | ||
967 | orom = find_imsm_orom(); | |
968 | if (!orom) { | |
969 | if (verbose) | |
970 | fprintf(stderr, Name ": imsm option-rom not found\n"); | |
971 | return 2; | |
972 | } | |
973 | ||
974 | printf(" Platform : Intel(R) Matrix Storage Manager\n"); | |
975 | printf(" Version : %d.%d.%d.%d\n", orom->major_ver, orom->minor_ver, | |
976 | orom->hotfix_ver, orom->build); | |
977 | printf(" RAID Levels :%s%s%s%s%s\n", | |
978 | imsm_orom_has_raid0(orom) ? " raid0" : "", | |
979 | imsm_orom_has_raid1(orom) ? " raid1" : "", | |
980 | imsm_orom_has_raid1e(orom) ? " raid1e" : "", | |
981 | imsm_orom_has_raid10(orom) ? " raid10" : "", | |
982 | imsm_orom_has_raid5(orom) ? " raid5" : ""); | |
983 | printf(" Max Disks : %d\n", orom->tds); | |
984 | printf(" Max Volumes : %d\n", orom->vpa); | |
985 | printf(" I/O Controller : %s\n", hba_path); | |
986 | ||
987 | /* find the smallest scsi host number to determine a port number base */ | |
988 | dir = opendir(hba_path); | |
989 | for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) { | |
990 | int host; | |
991 | ||
992 | if (sscanf(ent->d_name, "host%d", &host) != 1) | |
993 | continue; | |
994 | if (port_count == 0) | |
995 | host_base = host; | |
996 | else if (host < host_base) | |
997 | host_base = host; | |
998 | ||
999 | if (host + 1 > port_count + host_base) | |
1000 | port_count = host + 1 - host_base; | |
1001 | ||
1002 | } | |
1003 | if (dir) | |
1004 | closedir(dir); | |
1005 | ||
1006 | if (!port_count || imsm_enumerate_ports(hba_path, port_count, | |
1007 | host_base, verbose) != 0) { | |
1008 | if (verbose) | |
1009 | fprintf(stderr, Name ": failed to enumerate ports\n"); | |
1010 | return 2; | |
1011 | } | |
1012 | ||
1013 | return 0; | |
1014 | } | |
cdddbdbc DW |
1015 | #endif |
1016 | ||
1017 | static int match_home_imsm(struct supertype *st, char *homehost) | |
1018 | { | |
5115ca67 DW |
1019 | /* the imsm metadata format does not specify any host |
1020 | * identification information. We return -1 since we can never | |
1021 | * confirm nor deny whether a given array is "meant" for this | |
1022 | * host. We rely on compare_super and the 'family_num' field to | |
1023 | * exclude member disks that do not belong, and we rely on | |
1024 | * mdadm.conf to specify the arrays that should be assembled. | |
1025 | * Auto-assembly may still pick up "foreign" arrays. | |
1026 | */ | |
cdddbdbc | 1027 | |
9362c1c8 | 1028 | return -1; |
cdddbdbc DW |
1029 | } |
1030 | ||
1031 | static void uuid_from_super_imsm(struct supertype *st, int uuid[4]) | |
1032 | { | |
51006d85 N |
1033 | /* The uuid returned here is used for: |
1034 | * uuid to put into bitmap file (Create, Grow) | |
1035 | * uuid for backup header when saving critical section (Grow) | |
1036 | * comparing uuids when re-adding a device into an array | |
1037 | * In these cases the uuid required is that of the data-array, | |
1038 | * not the device-set. | |
1039 | * uuid to recognise same set when adding a missing device back | |
1040 | * to an array. This is a uuid for the device-set. | |
1041 | * | |
1042 | * For each of these we can make do with a truncated | |
1043 | * or hashed uuid rather than the original, as long as | |
1044 | * everyone agrees. | |
1045 | * In each case the uuid required is that of the data-array, | |
1046 | * not the device-set. | |
43dad3d6 | 1047 | */ |
51006d85 N |
1048 | /* imsm does not track uuid's so we synthesis one using sha1 on |
1049 | * - The signature (Which is constant for all imsm array, but no matter) | |
1050 | * - the family_num of the container | |
1051 | * - the index number of the volume | |
1052 | * - the 'serial' number of the volume. | |
1053 | * Hopefully these are all constant. | |
1054 | */ | |
1055 | struct intel_super *super = st->sb; | |
43dad3d6 | 1056 | |
51006d85 N |
1057 | char buf[20]; |
1058 | struct sha1_ctx ctx; | |
1059 | struct imsm_dev *dev = NULL; | |
1060 | ||
1061 | sha1_init_ctx(&ctx); | |
92bd8f8d | 1062 | sha1_process_bytes(super->anchor->sig, MPB_SIG_LEN, &ctx); |
51006d85 N |
1063 | sha1_process_bytes(&super->anchor->family_num, sizeof(__u32), &ctx); |
1064 | if (super->current_vol >= 0) | |
1065 | dev = get_imsm_dev(super, super->current_vol); | |
1066 | if (dev) { | |
1067 | __u32 vol = super->current_vol; | |
1068 | sha1_process_bytes(&vol, sizeof(vol), &ctx); | |
1069 | sha1_process_bytes(dev->volume, MAX_RAID_SERIAL_LEN, &ctx); | |
1070 | } | |
1071 | sha1_finish_ctx(&ctx, buf); | |
1072 | memcpy(uuid, buf, 4*4); | |
cdddbdbc DW |
1073 | } |
1074 | ||
0d481d37 | 1075 | #if 0 |
4f5bc454 DW |
1076 | static void |
1077 | get_imsm_numerical_version(struct imsm_super *mpb, int *m, int *p) | |
cdddbdbc | 1078 | { |
cdddbdbc DW |
1079 | __u8 *v = get_imsm_version(mpb); |
1080 | __u8 *end = mpb->sig + MAX_SIGNATURE_LENGTH; | |
1081 | char major[] = { 0, 0, 0 }; | |
1082 | char minor[] = { 0 ,0, 0 }; | |
1083 | char patch[] = { 0, 0, 0 }; | |
1084 | char *ver_parse[] = { major, minor, patch }; | |
1085 | int i, j; | |
1086 | ||
1087 | i = j = 0; | |
1088 | while (*v != '\0' && v < end) { | |
1089 | if (*v != '.' && j < 2) | |
1090 | ver_parse[i][j++] = *v; | |
1091 | else { | |
1092 | i++; | |
1093 | j = 0; | |
1094 | } | |
1095 | v++; | |
1096 | } | |
1097 | ||
4f5bc454 DW |
1098 | *m = strtol(minor, NULL, 0); |
1099 | *p = strtol(patch, NULL, 0); | |
1100 | } | |
0d481d37 | 1101 | #endif |
4f5bc454 | 1102 | |
c2c087e6 DW |
1103 | static int imsm_level_to_layout(int level) |
1104 | { | |
1105 | switch (level) { | |
1106 | case 0: | |
1107 | case 1: | |
1108 | return 0; | |
1109 | case 5: | |
1110 | case 6: | |
a380c027 | 1111 | return ALGORITHM_LEFT_ASYMMETRIC; |
c2c087e6 | 1112 | case 10: |
c92a2527 | 1113 | return 0x102; |
c2c087e6 | 1114 | } |
a18a888e | 1115 | return UnSet; |
c2c087e6 DW |
1116 | } |
1117 | ||
bf5a934a DW |
1118 | static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info) |
1119 | { | |
1120 | struct intel_super *super = st->sb; | |
949c47a0 | 1121 | struct imsm_dev *dev = get_imsm_dev(super, super->current_vol); |
a965f303 | 1122 | struct imsm_map *map = get_imsm_map(dev, 0); |
efb30e7f | 1123 | struct dl *dl; |
bf5a934a | 1124 | |
efb30e7f DW |
1125 | for (dl = super->disks; dl; dl = dl->next) |
1126 | if (dl->raiddisk == info->disk.raid_disk) | |
1127 | break; | |
bf5a934a DW |
1128 | info->container_member = super->current_vol; |
1129 | info->array.raid_disks = map->num_members; | |
1130 | info->array.level = get_imsm_raid_level(map); | |
1131 | info->array.layout = imsm_level_to_layout(info->array.level); | |
1132 | info->array.md_minor = -1; | |
1133 | info->array.ctime = 0; | |
1134 | info->array.utime = 0; | |
301406c9 DW |
1135 | info->array.chunk_size = __le16_to_cpu(map->blocks_per_strip) << 9; |
1136 | info->array.state = !dev->vol.dirty; | |
1137 | ||
1138 | info->disk.major = 0; | |
1139 | info->disk.minor = 0; | |
efb30e7f DW |
1140 | if (dl) { |
1141 | info->disk.major = dl->major; | |
1142 | info->disk.minor = dl->minor; | |
1143 | } | |
bf5a934a DW |
1144 | |
1145 | info->data_offset = __le32_to_cpu(map->pba_of_lba0); | |
1146 | info->component_size = __le32_to_cpu(map->blocks_per_member); | |
301406c9 | 1147 | memset(info->uuid, 0, sizeof(info->uuid)); |
bf5a934a | 1148 | |
f8f603f1 | 1149 | if (map->map_state == IMSM_T_STATE_UNINITIALIZED || dev->vol.dirty) |
301406c9 | 1150 | info->resync_start = 0; |
f8f603f1 DW |
1151 | else if (dev->vol.migr_state) |
1152 | info->resync_start = __le32_to_cpu(dev->vol.curr_migr_unit); | |
301406c9 DW |
1153 | else |
1154 | info->resync_start = ~0ULL; | |
1155 | ||
1156 | strncpy(info->name, (char *) dev->volume, MAX_RAID_SERIAL_LEN); | |
1157 | info->name[MAX_RAID_SERIAL_LEN] = 0; | |
bf5a934a | 1158 | |
f35f2525 N |
1159 | info->array.major_version = -1; |
1160 | info->array.minor_version = -2; | |
bf5a934a DW |
1161 | sprintf(info->text_version, "/%s/%d", |
1162 | devnum2devname(st->container_dev), | |
1163 | info->container_member); | |
a67dd8cc | 1164 | info->safe_mode_delay = 4000; /* 4 secs like the Matrix driver */ |
51006d85 | 1165 | uuid_from_super_imsm(st, info->uuid); |
bf5a934a DW |
1166 | } |
1167 | ||
7a70e8aa DW |
1168 | /* check the config file to see if we can return a real uuid for this spare */ |
1169 | static void fixup_container_spare_uuid(struct mdinfo *inf) | |
1170 | { | |
1171 | struct mddev_ident_s *array_list; | |
1172 | ||
1173 | if (inf->array.level != LEVEL_CONTAINER || | |
1174 | memcmp(inf->uuid, uuid_match_any, sizeof(int[4])) != 0) | |
1175 | return; | |
1176 | ||
1177 | array_list = conf_get_ident(NULL); | |
1178 | ||
1179 | for (; array_list; array_list = array_list->next) { | |
1180 | if (array_list->uuid_set) { | |
1181 | struct supertype *_sst; /* spare supertype */ | |
1182 | struct supertype *_cst; /* container supertype */ | |
1183 | ||
1184 | _cst = array_list->st; | |
1185 | _sst = _cst->ss->match_metadata_desc(inf->text_version); | |
1186 | if (_sst) { | |
1187 | memcpy(inf->uuid, array_list->uuid, sizeof(int[4])); | |
1188 | free(_sst); | |
1189 | break; | |
1190 | } | |
1191 | } | |
1192 | } | |
1193 | } | |
bf5a934a | 1194 | |
4f5bc454 DW |
1195 | static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info) |
1196 | { | |
1197 | struct intel_super *super = st->sb; | |
4f5bc454 DW |
1198 | struct imsm_disk *disk; |
1199 | __u32 s; | |
4f5bc454 | 1200 | |
bf5a934a DW |
1201 | if (super->current_vol >= 0) { |
1202 | getinfo_super_imsm_volume(st, info); | |
1203 | return; | |
1204 | } | |
d23fe947 DW |
1205 | |
1206 | /* Set raid_disks to zero so that Assemble will always pull in valid | |
1207 | * spares | |
1208 | */ | |
1209 | info->array.raid_disks = 0; | |
cdddbdbc DW |
1210 | info->array.level = LEVEL_CONTAINER; |
1211 | info->array.layout = 0; | |
1212 | info->array.md_minor = -1; | |
c2c087e6 | 1213 | info->array.ctime = 0; /* N/A for imsm */ |
cdddbdbc DW |
1214 | info->array.utime = 0; |
1215 | info->array.chunk_size = 0; | |
1216 | ||
1217 | info->disk.major = 0; | |
1218 | info->disk.minor = 0; | |
cdddbdbc | 1219 | info->disk.raid_disk = -1; |
c2c087e6 | 1220 | info->reshape_active = 0; |
f35f2525 N |
1221 | info->array.major_version = -1; |
1222 | info->array.minor_version = -2; | |
c2c087e6 | 1223 | strcpy(info->text_version, "imsm"); |
a67dd8cc | 1224 | info->safe_mode_delay = 0; |
c2c087e6 DW |
1225 | info->disk.number = -1; |
1226 | info->disk.state = 0; | |
c5afc314 | 1227 | info->name[0] = 0; |
c2c087e6 | 1228 | |
4a04ec6c | 1229 | if (super->disks) { |
14e8215b DW |
1230 | __u32 reserved = imsm_reserved_sectors(super, super->disks); |
1231 | ||
b9f594fe | 1232 | disk = &super->disks->disk; |
14e8215b DW |
1233 | info->data_offset = __le32_to_cpu(disk->total_blocks) - reserved; |
1234 | info->component_size = reserved; | |
f2f27e63 | 1235 | s = disk->status; |
4a04ec6c | 1236 | info->disk.state = s & CONFIGURED_DISK ? (1 << MD_DISK_ACTIVE) : 0; |
df474657 DW |
1237 | /* we don't change info->disk.raid_disk here because |
1238 | * this state will be finalized in mdmon after we have | |
1239 | * found the 'most fresh' version of the metadata | |
1240 | */ | |
1241 | info->disk.state |= s & FAILED_DISK ? (1 << MD_DISK_FAULTY) : 0; | |
032e9e29 | 1242 | info->disk.state |= s & SPARE_DISK ? 0 : (1 << MD_DISK_SYNC); |
cdddbdbc | 1243 | } |
a575e2a7 DW |
1244 | |
1245 | /* only call uuid_from_super_imsm when this disk is part of a populated container, | |
1246 | * ->compare_super may have updated the 'num_raid_devs' field for spares | |
1247 | */ | |
1248 | if (info->disk.state & (1 << MD_DISK_SYNC) || super->anchor->num_raid_devs) | |
36ba7d48 | 1249 | uuid_from_super_imsm(st, info->uuid); |
7a70e8aa | 1250 | else { |
032e9e29 | 1251 | memcpy(info->uuid, uuid_match_any, sizeof(int[4])); |
7a70e8aa DW |
1252 | fixup_container_spare_uuid(info); |
1253 | } | |
cdddbdbc DW |
1254 | } |
1255 | ||
cdddbdbc DW |
1256 | static int update_super_imsm(struct supertype *st, struct mdinfo *info, |
1257 | char *update, char *devname, int verbose, | |
1258 | int uuid_set, char *homehost) | |
1259 | { | |
f352c545 DW |
1260 | /* FIXME */ |
1261 | ||
1262 | /* For 'assemble' and 'force' we need to return non-zero if any | |
1263 | * change was made. For others, the return value is ignored. | |
1264 | * Update options are: | |
1265 | * force-one : This device looks a bit old but needs to be included, | |
1266 | * update age info appropriately. | |
1267 | * assemble: clear any 'faulty' flag to allow this device to | |
1268 | * be assembled. | |
1269 | * force-array: Array is degraded but being forced, mark it clean | |
1270 | * if that will be needed to assemble it. | |
1271 | * | |
1272 | * newdev: not used ???? | |
1273 | * grow: Array has gained a new device - this is currently for | |
1274 | * linear only | |
1275 | * resync: mark as dirty so a resync will happen. | |
1276 | * name: update the name - preserving the homehost | |
1277 | * | |
1278 | * Following are not relevant for this imsm: | |
1279 | * sparc2.2 : update from old dodgey metadata | |
1280 | * super-minor: change the preferred_minor number | |
1281 | * summaries: update redundant counters. | |
1282 | * uuid: Change the uuid of the array to match watch is given | |
1283 | * homehost: update the recorded homehost | |
1284 | * _reshape_progress: record new reshape_progress position. | |
1285 | */ | |
1286 | int rv = 0; | |
1287 | //struct intel_super *super = st->sb; | |
1288 | //struct imsm_super *mpb = super->mpb; | |
1289 | ||
1290 | if (strcmp(update, "grow") == 0) { | |
1291 | } | |
1292 | if (strcmp(update, "resync") == 0) { | |
1293 | /* dev->vol.dirty = 1; */ | |
1294 | } | |
1295 | ||
1296 | /* IMSM has no concept of UUID or homehost */ | |
1297 | ||
1298 | return rv; | |
cdddbdbc DW |
1299 | } |
1300 | ||
c2c087e6 | 1301 | static size_t disks_to_mpb_size(int disks) |
cdddbdbc | 1302 | { |
c2c087e6 | 1303 | size_t size; |
cdddbdbc | 1304 | |
c2c087e6 DW |
1305 | size = sizeof(struct imsm_super); |
1306 | size += (disks - 1) * sizeof(struct imsm_disk); | |
1307 | size += 2 * sizeof(struct imsm_dev); | |
1308 | /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */ | |
1309 | size += (4 - 2) * sizeof(struct imsm_map); | |
1310 | /* 4 possible disk_ord_tbl's */ | |
1311 | size += 4 * (disks - 1) * sizeof(__u32); | |
1312 | ||
1313 | return size; | |
1314 | } | |
1315 | ||
1316 | static __u64 avail_size_imsm(struct supertype *st, __u64 devsize) | |
1317 | { | |
1318 | if (devsize < (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS)) | |
1319 | return 0; | |
1320 | ||
1321 | return devsize - (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS); | |
cdddbdbc DW |
1322 | } |
1323 | ||
ba2de7ba DW |
1324 | static void free_devlist(struct intel_super *super) |
1325 | { | |
1326 | struct intel_dev *dv; | |
1327 | ||
1328 | while (super->devlist) { | |
1329 | dv = super->devlist->next; | |
1330 | free(super->devlist->dev); | |
1331 | free(super->devlist); | |
1332 | super->devlist = dv; | |
1333 | } | |
1334 | } | |
1335 | ||
1336 | static void imsm_copy_dev(struct imsm_dev *dest, struct imsm_dev *src) | |
1337 | { | |
1338 | memcpy(dest, src, sizeof_imsm_dev(src, 0)); | |
1339 | } | |
1340 | ||
cdddbdbc DW |
1341 | static int compare_super_imsm(struct supertype *st, struct supertype *tst) |
1342 | { | |
1343 | /* | |
1344 | * return: | |
1345 | * 0 same, or first was empty, and second was copied | |
1346 | * 1 second had wrong number | |
1347 | * 2 wrong uuid | |
1348 | * 3 wrong other info | |
1349 | */ | |
1350 | struct intel_super *first = st->sb; | |
1351 | struct intel_super *sec = tst->sb; | |
1352 | ||
1353 | if (!first) { | |
1354 | st->sb = tst->sb; | |
1355 | tst->sb = NULL; | |
1356 | return 0; | |
1357 | } | |
1358 | ||
949c47a0 | 1359 | if (memcmp(first->anchor->sig, sec->anchor->sig, MAX_SIGNATURE_LENGTH) != 0) |
cdddbdbc | 1360 | return 3; |
d23fe947 DW |
1361 | |
1362 | /* if an anchor does not have num_raid_devs set then it is a free | |
1363 | * floating spare | |
1364 | */ | |
1365 | if (first->anchor->num_raid_devs > 0 && | |
1366 | sec->anchor->num_raid_devs > 0) { | |
1367 | if (first->anchor->family_num != sec->anchor->family_num) | |
1368 | return 3; | |
d23fe947 | 1369 | } |
cdddbdbc | 1370 | |
3e372e5a DW |
1371 | /* if 'first' is a spare promote it to a populated mpb with sec's |
1372 | * family number | |
1373 | */ | |
1374 | if (first->anchor->num_raid_devs == 0 && | |
1375 | sec->anchor->num_raid_devs > 0) { | |
78d30f94 | 1376 | int i; |
ba2de7ba DW |
1377 | struct intel_dev *dv; |
1378 | struct imsm_dev *dev; | |
78d30f94 DW |
1379 | |
1380 | /* we need to copy raid device info from sec if an allocation | |
1381 | * fails here we don't associate the spare | |
1382 | */ | |
1383 | for (i = 0; i < sec->anchor->num_raid_devs; i++) { | |
ba2de7ba DW |
1384 | dv = malloc(sizeof(*dv)); |
1385 | if (!dv) | |
1386 | break; | |
1387 | dev = malloc(sizeof_imsm_dev(get_imsm_dev(sec, i), 1)); | |
1388 | if (!dev) { | |
1389 | free(dv); | |
1390 | break; | |
78d30f94 | 1391 | } |
ba2de7ba DW |
1392 | dv->dev = dev; |
1393 | dv->index = i; | |
1394 | dv->next = first->devlist; | |
1395 | first->devlist = dv; | |
78d30f94 | 1396 | } |
ba2de7ba DW |
1397 | if (i <= sec->anchor->num_raid_devs) { |
1398 | /* allocation failure */ | |
1399 | free_devlist(first); | |
1400 | fprintf(stderr, "imsm: failed to associate spare\n"); | |
1401 | return 3; | |
78d30f94 | 1402 | } |
ba2de7ba DW |
1403 | for (i = 0; i < sec->anchor->num_raid_devs; i++) |
1404 | imsm_copy_dev(get_imsm_dev(first, i), get_imsm_dev(sec, i)); | |
78d30f94 | 1405 | |
3e372e5a DW |
1406 | first->anchor->num_raid_devs = sec->anchor->num_raid_devs; |
1407 | first->anchor->family_num = sec->anchor->family_num; | |
1408 | } | |
1409 | ||
cdddbdbc DW |
1410 | return 0; |
1411 | } | |
1412 | ||
0030e8d6 DW |
1413 | static void fd2devname(int fd, char *name) |
1414 | { | |
1415 | struct stat st; | |
1416 | char path[256]; | |
1417 | char dname[100]; | |
1418 | char *nm; | |
1419 | int rv; | |
1420 | ||
1421 | name[0] = '\0'; | |
1422 | if (fstat(fd, &st) != 0) | |
1423 | return; | |
1424 | sprintf(path, "/sys/dev/block/%d:%d", | |
1425 | major(st.st_rdev), minor(st.st_rdev)); | |
1426 | ||
1427 | rv = readlink(path, dname, sizeof(dname)); | |
1428 | if (rv <= 0) | |
1429 | return; | |
1430 | ||
1431 | dname[rv] = '\0'; | |
1432 | nm = strrchr(dname, '/'); | |
1433 | nm++; | |
1434 | snprintf(name, MAX_RAID_SERIAL_LEN, "/dev/%s", nm); | |
1435 | } | |
1436 | ||
1437 | ||
cdddbdbc DW |
1438 | extern int scsi_get_serial(int fd, void *buf, size_t buf_len); |
1439 | ||
1440 | static int imsm_read_serial(int fd, char *devname, | |
1441 | __u8 serial[MAX_RAID_SERIAL_LEN]) | |
1442 | { | |
1443 | unsigned char scsi_serial[255]; | |
cdddbdbc DW |
1444 | int rv; |
1445 | int rsp_len; | |
1f24f035 DW |
1446 | int len; |
1447 | char *c, *rsp_buf; | |
cdddbdbc DW |
1448 | |
1449 | memset(scsi_serial, 0, sizeof(scsi_serial)); | |
cdddbdbc | 1450 | |
f9ba0ff1 DW |
1451 | rv = scsi_get_serial(fd, scsi_serial, sizeof(scsi_serial)); |
1452 | ||
40ebbb9c | 1453 | if (rv && check_env("IMSM_DEVNAME_AS_SERIAL")) { |
f9ba0ff1 DW |
1454 | memset(serial, 0, MAX_RAID_SERIAL_LEN); |
1455 | fd2devname(fd, (char *) serial); | |
0030e8d6 DW |
1456 | return 0; |
1457 | } | |
1458 | ||
cdddbdbc DW |
1459 | if (rv != 0) { |
1460 | if (devname) | |
1461 | fprintf(stderr, | |
1462 | Name ": Failed to retrieve serial for %s\n", | |
1463 | devname); | |
1464 | return rv; | |
1465 | } | |
1466 | ||
5c3db629 | 1467 | /* trim leading whitespace */ |
cdddbdbc | 1468 | rsp_len = scsi_serial[3]; |
03cd4cc8 DW |
1469 | if (!rsp_len) { |
1470 | if (devname) | |
1471 | fprintf(stderr, | |
1472 | Name ": Failed to retrieve serial for %s\n", | |
1473 | devname); | |
1474 | return 2; | |
1475 | } | |
1f24f035 DW |
1476 | rsp_buf = (char *) &scsi_serial[4]; |
1477 | c = rsp_buf; | |
1478 | while (isspace(*c)) | |
1479 | c++; | |
5c3db629 DW |
1480 | |
1481 | /* truncate len to the end of rsp_buf if necessary */ | |
1f24f035 DW |
1482 | if (c + MAX_RAID_SERIAL_LEN > rsp_buf + rsp_len) |
1483 | len = rsp_len - (c - rsp_buf); | |
1484 | else | |
1485 | len = MAX_RAID_SERIAL_LEN; | |
5c3db629 DW |
1486 | |
1487 | /* initialize the buffer and copy rsp_buf characters */ | |
1488 | memset(serial, 0, MAX_RAID_SERIAL_LEN); | |
1f24f035 | 1489 | memcpy(serial, c, len); |
5c3db629 DW |
1490 | |
1491 | /* trim trailing whitespace starting with the last character copied */ | |
1f24f035 DW |
1492 | c = (char *) &serial[len - 1]; |
1493 | while (isspace(*c) || *c == '\0') | |
1494 | *c-- = '\0'; | |
cdddbdbc DW |
1495 | |
1496 | return 0; | |
1497 | } | |
1498 | ||
1f24f035 DW |
1499 | static int serialcmp(__u8 *s1, __u8 *s2) |
1500 | { | |
1501 | return strncmp((char *) s1, (char *) s2, MAX_RAID_SERIAL_LEN); | |
1502 | } | |
1503 | ||
1504 | static void serialcpy(__u8 *dest, __u8 *src) | |
1505 | { | |
1506 | strncpy((char *) dest, (char *) src, MAX_RAID_SERIAL_LEN); | |
1507 | } | |
1508 | ||
54c2c1ea DW |
1509 | static struct dl *serial_to_dl(__u8 *serial, struct intel_super *super) |
1510 | { | |
1511 | struct dl *dl; | |
1512 | ||
1513 | for (dl = super->disks; dl; dl = dl->next) | |
1514 | if (serialcmp(dl->serial, serial) == 0) | |
1515 | break; | |
1516 | ||
1517 | return dl; | |
1518 | } | |
1519 | ||
cdddbdbc DW |
1520 | static int |
1521 | load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd) | |
1522 | { | |
cdddbdbc DW |
1523 | struct dl *dl; |
1524 | struct stat stb; | |
cdddbdbc DW |
1525 | int rv; |
1526 | int i; | |
d23fe947 DW |
1527 | int alloc = 1; |
1528 | __u8 serial[MAX_RAID_SERIAL_LEN]; | |
1529 | ||
1530 | rv = imsm_read_serial(fd, devname, serial); | |
1531 | ||
1532 | if (rv != 0) | |
1533 | return 2; | |
1534 | ||
1535 | /* check if this is a disk we have seen before. it may be a spare in | |
1536 | * super->disks while the current anchor believes it is a raid member, | |
1537 | * check if we need to update dl->index | |
1538 | */ | |
54c2c1ea | 1539 | dl = serial_to_dl(serial, super); |
d23fe947 DW |
1540 | if (!dl) |
1541 | dl = malloc(sizeof(*dl)); | |
1542 | else | |
1543 | alloc = 0; | |
cdddbdbc | 1544 | |
b9f594fe | 1545 | if (!dl) { |
cdddbdbc DW |
1546 | if (devname) |
1547 | fprintf(stderr, | |
1548 | Name ": failed to allocate disk buffer for %s\n", | |
1549 | devname); | |
1550 | return 2; | |
1551 | } | |
cdddbdbc | 1552 | |
d23fe947 DW |
1553 | if (alloc) { |
1554 | fstat(fd, &stb); | |
1555 | dl->major = major(stb.st_rdev); | |
1556 | dl->minor = minor(stb.st_rdev); | |
1557 | dl->next = super->disks; | |
1558 | dl->fd = keep_fd ? fd : -1; | |
1559 | dl->devname = devname ? strdup(devname) : NULL; | |
1f24f035 | 1560 | serialcpy(dl->serial, serial); |
8796fdc4 | 1561 | dl->index = -2; |
0dcecb2e | 1562 | dl->e = NULL; |
d23fe947 DW |
1563 | } else if (keep_fd) { |
1564 | close(dl->fd); | |
1565 | dl->fd = fd; | |
1566 | } | |
cdddbdbc | 1567 | |
d23fe947 | 1568 | /* look up this disk's index in the current anchor */ |
949c47a0 DW |
1569 | for (i = 0; i < super->anchor->num_disks; i++) { |
1570 | struct imsm_disk *disk_iter; | |
1571 | ||
1572 | disk_iter = __get_imsm_disk(super->anchor, i); | |
cdddbdbc | 1573 | |
1f24f035 | 1574 | if (serialcmp(disk_iter->serial, dl->serial) == 0) { |
b9f594fe | 1575 | dl->disk = *disk_iter; |
d23fe947 DW |
1576 | /* only set index on disks that are a member of a |
1577 | * populated contianer, i.e. one with raid_devs | |
1578 | */ | |
f2f27e63 | 1579 | if (dl->disk.status & FAILED_DISK) |
6c386dd3 | 1580 | dl->index = -2; |
f2f27e63 | 1581 | else if (dl->disk.status & SPARE_DISK) |
d23fe947 DW |
1582 | dl->index = -1; |
1583 | else | |
1584 | dl->index = i; | |
8796fdc4 | 1585 | |
cdddbdbc | 1586 | break; |
949c47a0 | 1587 | } |
cdddbdbc DW |
1588 | } |
1589 | ||
3f6efecc DW |
1590 | /* no match, maybe a stale failed drive */ |
1591 | if (i == super->anchor->num_disks && dl->index >= 0) { | |
1592 | dl->disk = *__get_imsm_disk(super->anchor, dl->index); | |
f2f27e63 | 1593 | if (dl->disk.status & FAILED_DISK) |
3f6efecc DW |
1594 | dl->index = -2; |
1595 | } | |
1596 | ||
d23fe947 DW |
1597 | if (alloc) |
1598 | super->disks = dl; | |
6c386dd3 | 1599 | |
949c47a0 DW |
1600 | return 0; |
1601 | } | |
1602 | ||
0e600426 | 1603 | #ifndef MDASSEMBLE |
0c046afd DW |
1604 | /* When migrating map0 contains the 'destination' state while map1 |
1605 | * contains the current state. When not migrating map0 contains the | |
1606 | * current state. This routine assumes that map[0].map_state is set to | |
1607 | * the current array state before being called. | |
1608 | * | |
1609 | * Migration is indicated by one of the following states | |
1610 | * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed) | |
e3bba0e0 | 1611 | * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal |
0c046afd | 1612 | * map1state=unitialized) |
e3bba0e0 | 1613 | * 3/ Verify (Resync) (migr_state=1 migr_type=MIGR_REBUILD map0state=normal |
0c046afd | 1614 | * map1state=normal) |
e3bba0e0 | 1615 | * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal |
0c046afd DW |
1616 | * map1state=degraded) |
1617 | */ | |
0556e1a2 | 1618 | static void migrate(struct imsm_dev *dev, __u8 to_state, int migr_type) |
3393c6af | 1619 | { |
0c046afd | 1620 | struct imsm_map *dest; |
3393c6af DW |
1621 | struct imsm_map *src = get_imsm_map(dev, 0); |
1622 | ||
0c046afd | 1623 | dev->vol.migr_state = 1; |
0556e1a2 | 1624 | dev->vol.migr_type = migr_type; |
f8f603f1 | 1625 | dev->vol.curr_migr_unit = 0; |
0c046afd DW |
1626 | dest = get_imsm_map(dev, 1); |
1627 | ||
0556e1a2 | 1628 | /* duplicate and then set the target end state in map[0] */ |
3393c6af | 1629 | memcpy(dest, src, sizeof_imsm_map(src)); |
0556e1a2 DW |
1630 | if (migr_type == MIGR_REBUILD) { |
1631 | __u32 ord; | |
1632 | int i; | |
1633 | ||
1634 | for (i = 0; i < src->num_members; i++) { | |
1635 | ord = __le32_to_cpu(src->disk_ord_tbl[i]); | |
1636 | set_imsm_ord_tbl_ent(src, i, ord_to_idx(ord)); | |
1637 | } | |
1638 | } | |
1639 | ||
0c046afd | 1640 | src->map_state = to_state; |
949c47a0 | 1641 | } |
f8f603f1 DW |
1642 | |
1643 | static void end_migration(struct imsm_dev *dev, __u8 map_state) | |
1644 | { | |
1645 | struct imsm_map *map = get_imsm_map(dev, 0); | |
0556e1a2 DW |
1646 | struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state); |
1647 | int i; | |
1648 | ||
1649 | /* merge any IMSM_ORD_REBUILD bits that were not successfully | |
1650 | * completed in the last migration. | |
1651 | * | |
1652 | * FIXME add support for online capacity expansion and | |
1653 | * raid-level-migration | |
1654 | */ | |
1655 | for (i = 0; i < prev->num_members; i++) | |
1656 | map->disk_ord_tbl[i] |= prev->disk_ord_tbl[i]; | |
f8f603f1 DW |
1657 | |
1658 | dev->vol.migr_state = 0; | |
1659 | dev->vol.curr_migr_unit = 0; | |
1660 | map->map_state = map_state; | |
1661 | } | |
0e600426 | 1662 | #endif |
949c47a0 DW |
1663 | |
1664 | static int parse_raid_devices(struct intel_super *super) | |
1665 | { | |
1666 | int i; | |
1667 | struct imsm_dev *dev_new; | |
4d7b1503 DW |
1668 | size_t len, len_migr; |
1669 | size_t space_needed = 0; | |
1670 | struct imsm_super *mpb = super->anchor; | |
949c47a0 DW |
1671 | |
1672 | for (i = 0; i < super->anchor->num_raid_devs; i++) { | |
1673 | struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i); | |
ba2de7ba | 1674 | struct intel_dev *dv; |
949c47a0 | 1675 | |
4d7b1503 DW |
1676 | len = sizeof_imsm_dev(dev_iter, 0); |
1677 | len_migr = sizeof_imsm_dev(dev_iter, 1); | |
1678 | if (len_migr > len) | |
1679 | space_needed += len_migr - len; | |
1680 | ||
ba2de7ba DW |
1681 | dv = malloc(sizeof(*dv)); |
1682 | if (!dv) | |
1683 | return 1; | |
4d7b1503 | 1684 | dev_new = malloc(len_migr); |
ba2de7ba DW |
1685 | if (!dev_new) { |
1686 | free(dv); | |
949c47a0 | 1687 | return 1; |
ba2de7ba | 1688 | } |
949c47a0 | 1689 | imsm_copy_dev(dev_new, dev_iter); |
ba2de7ba DW |
1690 | dv->dev = dev_new; |
1691 | dv->index = i; | |
1692 | dv->next = super->devlist; | |
1693 | super->devlist = dv; | |
949c47a0 | 1694 | } |
cdddbdbc | 1695 | |
4d7b1503 DW |
1696 | /* ensure that super->buf is large enough when all raid devices |
1697 | * are migrating | |
1698 | */ | |
1699 | if (__le32_to_cpu(mpb->mpb_size) + space_needed > super->len) { | |
1700 | void *buf; | |
1701 | ||
1702 | len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + space_needed, 512); | |
1703 | if (posix_memalign(&buf, 512, len) != 0) | |
1704 | return 1; | |
1705 | ||
1706 | memcpy(buf, super->buf, len); | |
1707 | free(super->buf); | |
1708 | super->buf = buf; | |
1709 | super->len = len; | |
1710 | } | |
1711 | ||
cdddbdbc DW |
1712 | return 0; |
1713 | } | |
1714 | ||
604b746f JD |
1715 | /* retrieve a pointer to the bbm log which starts after all raid devices */ |
1716 | struct bbm_log *__get_imsm_bbm_log(struct imsm_super *mpb) | |
1717 | { | |
1718 | void *ptr = NULL; | |
1719 | ||
1720 | if (__le32_to_cpu(mpb->bbm_log_size)) { | |
1721 | ptr = mpb; | |
1722 | ptr += mpb->mpb_size - __le32_to_cpu(mpb->bbm_log_size); | |
1723 | } | |
1724 | ||
1725 | return ptr; | |
1726 | } | |
1727 | ||
d23fe947 | 1728 | static void __free_imsm(struct intel_super *super, int free_disks); |
9ca2c81c | 1729 | |
cdddbdbc DW |
1730 | /* load_imsm_mpb - read matrix metadata |
1731 | * allocates super->mpb to be freed by free_super | |
1732 | */ | |
1733 | static int load_imsm_mpb(int fd, struct intel_super *super, char *devname) | |
1734 | { | |
1735 | unsigned long long dsize; | |
cdddbdbc DW |
1736 | unsigned long long sectors; |
1737 | struct stat; | |
6416d527 | 1738 | struct imsm_super *anchor; |
cdddbdbc | 1739 | __u32 check_sum; |
949c47a0 | 1740 | int rc; |
cdddbdbc | 1741 | |
cdddbdbc DW |
1742 | get_dev_size(fd, NULL, &dsize); |
1743 | ||
1744 | if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0) { | |
1745 | if (devname) | |
1746 | fprintf(stderr, | |
1747 | Name ": Cannot seek to anchor block on %s: %s\n", | |
1748 | devname, strerror(errno)); | |
1749 | return 1; | |
1750 | } | |
1751 | ||
949c47a0 | 1752 | if (posix_memalign((void**)&anchor, 512, 512) != 0) { |
ad97895e DW |
1753 | if (devname) |
1754 | fprintf(stderr, | |
1755 | Name ": Failed to allocate imsm anchor buffer" | |
1756 | " on %s\n", devname); | |
1757 | return 1; | |
1758 | } | |
949c47a0 | 1759 | if (read(fd, anchor, 512) != 512) { |
cdddbdbc DW |
1760 | if (devname) |
1761 | fprintf(stderr, | |
1762 | Name ": Cannot read anchor block on %s: %s\n", | |
1763 | devname, strerror(errno)); | |
6416d527 | 1764 | free(anchor); |
cdddbdbc DW |
1765 | return 1; |
1766 | } | |
1767 | ||
6416d527 | 1768 | if (strncmp((char *) anchor->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) { |
cdddbdbc DW |
1769 | if (devname) |
1770 | fprintf(stderr, | |
1771 | Name ": no IMSM anchor on %s\n", devname); | |
6416d527 | 1772 | free(anchor); |
cdddbdbc DW |
1773 | return 2; |
1774 | } | |
1775 | ||
d23fe947 | 1776 | __free_imsm(super, 0); |
949c47a0 DW |
1777 | super->len = ROUND_UP(anchor->mpb_size, 512); |
1778 | if (posix_memalign(&super->buf, 512, super->len) != 0) { | |
cdddbdbc DW |
1779 | if (devname) |
1780 | fprintf(stderr, | |
1781 | Name ": unable to allocate %zu byte mpb buffer\n", | |
949c47a0 | 1782 | super->len); |
6416d527 | 1783 | free(anchor); |
cdddbdbc DW |
1784 | return 2; |
1785 | } | |
949c47a0 | 1786 | memcpy(super->buf, anchor, 512); |
cdddbdbc | 1787 | |
6416d527 NB |
1788 | sectors = mpb_sectors(anchor) - 1; |
1789 | free(anchor); | |
949c47a0 | 1790 | if (!sectors) { |
ecf45690 DW |
1791 | check_sum = __gen_imsm_checksum(super->anchor); |
1792 | if (check_sum != __le32_to_cpu(super->anchor->check_sum)) { | |
1793 | if (devname) | |
1794 | fprintf(stderr, | |
1795 | Name ": IMSM checksum %x != %x on %s\n", | |
1796 | check_sum, | |
1797 | __le32_to_cpu(super->anchor->check_sum), | |
1798 | devname); | |
1799 | return 2; | |
1800 | } | |
1801 | ||
949c47a0 DW |
1802 | rc = load_imsm_disk(fd, super, devname, 0); |
1803 | if (rc == 0) | |
1804 | rc = parse_raid_devices(super); | |
1805 | return rc; | |
1806 | } | |
cdddbdbc DW |
1807 | |
1808 | /* read the extended mpb */ | |
1809 | if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0) { | |
1810 | if (devname) | |
1811 | fprintf(stderr, | |
1812 | Name ": Cannot seek to extended mpb on %s: %s\n", | |
1813 | devname, strerror(errno)); | |
1814 | return 1; | |
1815 | } | |
1816 | ||
949c47a0 | 1817 | if (read(fd, super->buf + 512, super->len - 512) != super->len - 512) { |
cdddbdbc DW |
1818 | if (devname) |
1819 | fprintf(stderr, | |
1820 | Name ": Cannot read extended mpb on %s: %s\n", | |
1821 | devname, strerror(errno)); | |
1822 | return 2; | |
1823 | } | |
1824 | ||
949c47a0 DW |
1825 | check_sum = __gen_imsm_checksum(super->anchor); |
1826 | if (check_sum != __le32_to_cpu(super->anchor->check_sum)) { | |
cdddbdbc DW |
1827 | if (devname) |
1828 | fprintf(stderr, | |
1829 | Name ": IMSM checksum %x != %x on %s\n", | |
949c47a0 | 1830 | check_sum, __le32_to_cpu(super->anchor->check_sum), |
cdddbdbc | 1831 | devname); |
db575f3b | 1832 | return 3; |
cdddbdbc DW |
1833 | } |
1834 | ||
604b746f JD |
1835 | /* FIXME the BBM log is disk specific so we cannot use this global |
1836 | * buffer for all disks. Ok for now since we only look at the global | |
1837 | * bbm_log_size parameter to gate assembly | |
1838 | */ | |
1839 | super->bbm_log = __get_imsm_bbm_log(super->anchor); | |
1840 | ||
949c47a0 DW |
1841 | rc = load_imsm_disk(fd, super, devname, 0); |
1842 | if (rc == 0) | |
1843 | rc = parse_raid_devices(super); | |
4d7b1503 | 1844 | |
949c47a0 | 1845 | return rc; |
cdddbdbc DW |
1846 | } |
1847 | ||
ae6aad82 DW |
1848 | static void __free_imsm_disk(struct dl *d) |
1849 | { | |
1850 | if (d->fd >= 0) | |
1851 | close(d->fd); | |
1852 | if (d->devname) | |
1853 | free(d->devname); | |
0dcecb2e DW |
1854 | if (d->e) |
1855 | free(d->e); | |
ae6aad82 DW |
1856 | free(d); |
1857 | ||
1858 | } | |
cdddbdbc DW |
1859 | static void free_imsm_disks(struct intel_super *super) |
1860 | { | |
47ee5a45 | 1861 | struct dl *d; |
cdddbdbc | 1862 | |
47ee5a45 DW |
1863 | while (super->disks) { |
1864 | d = super->disks; | |
cdddbdbc | 1865 | super->disks = d->next; |
ae6aad82 | 1866 | __free_imsm_disk(d); |
cdddbdbc | 1867 | } |
47ee5a45 DW |
1868 | while (super->missing) { |
1869 | d = super->missing; | |
1870 | super->missing = d->next; | |
1871 | __free_imsm_disk(d); | |
1872 | } | |
1873 | ||
cdddbdbc DW |
1874 | } |
1875 | ||
9ca2c81c | 1876 | /* free all the pieces hanging off of a super pointer */ |
d23fe947 | 1877 | static void __free_imsm(struct intel_super *super, int free_disks) |
cdddbdbc | 1878 | { |
9ca2c81c | 1879 | if (super->buf) { |
949c47a0 | 1880 | free(super->buf); |
9ca2c81c DW |
1881 | super->buf = NULL; |
1882 | } | |
d23fe947 DW |
1883 | if (free_disks) |
1884 | free_imsm_disks(super); | |
ba2de7ba | 1885 | free_devlist(super); |
88c32bb1 DW |
1886 | if (super->hba) { |
1887 | free((void *) super->hba); | |
1888 | super->hba = NULL; | |
1889 | } | |
cdddbdbc DW |
1890 | } |
1891 | ||
9ca2c81c DW |
1892 | static void free_imsm(struct intel_super *super) |
1893 | { | |
d23fe947 | 1894 | __free_imsm(super, 1); |
9ca2c81c DW |
1895 | free(super); |
1896 | } | |
cdddbdbc DW |
1897 | |
1898 | static void free_super_imsm(struct supertype *st) | |
1899 | { | |
1900 | struct intel_super *super = st->sb; | |
1901 | ||
1902 | if (!super) | |
1903 | return; | |
1904 | ||
1905 | free_imsm(super); | |
1906 | st->sb = NULL; | |
1907 | } | |
1908 | ||
c2c087e6 DW |
1909 | static struct intel_super *alloc_super(int creating_imsm) |
1910 | { | |
1911 | struct intel_super *super = malloc(sizeof(*super)); | |
1912 | ||
1913 | if (super) { | |
1914 | memset(super, 0, sizeof(*super)); | |
1915 | super->creating_imsm = creating_imsm; | |
bf5a934a | 1916 | super->current_vol = -1; |
0dcecb2e | 1917 | super->create_offset = ~((__u32 ) 0); |
88c32bb1 DW |
1918 | if (!check_env("IMSM_NO_PLATFORM")) |
1919 | super->orom = find_imsm_orom(); | |
cceebc67 | 1920 | if (super->orom && !check_env("IMSM_TEST_OROM")) { |
88c32bb1 DW |
1921 | struct sys_dev *list, *ent; |
1922 | ||
1923 | /* find the first intel ahci controller */ | |
1924 | list = find_driver_devices("pci", "ahci"); | |
1925 | for (ent = list; ent; ent = ent->next) | |
1926 | if (devpath_to_vendor(ent->path) == 0x8086) | |
1927 | break; | |
1928 | if (ent) { | |
1929 | super->hba = ent->path; | |
1930 | ent->path = NULL; | |
1931 | } | |
1932 | free_sys_dev(&list); | |
1933 | } | |
c2c087e6 DW |
1934 | } |
1935 | ||
1936 | return super; | |
1937 | } | |
1938 | ||
cdddbdbc | 1939 | #ifndef MDASSEMBLE |
47ee5a45 DW |
1940 | /* find_missing - helper routine for load_super_imsm_all that identifies |
1941 | * disks that have disappeared from the system. This routine relies on | |
1942 | * the mpb being uptodate, which it is at load time. | |
1943 | */ | |
1944 | static int find_missing(struct intel_super *super) | |
1945 | { | |
1946 | int i; | |
1947 | struct imsm_super *mpb = super->anchor; | |
1948 | struct dl *dl; | |
1949 | struct imsm_disk *disk; | |
47ee5a45 DW |
1950 | |
1951 | for (i = 0; i < mpb->num_disks; i++) { | |
1952 | disk = __get_imsm_disk(mpb, i); | |
54c2c1ea | 1953 | dl = serial_to_dl(disk->serial, super); |
47ee5a45 DW |
1954 | if (dl) |
1955 | continue; | |
47ee5a45 DW |
1956 | |
1957 | dl = malloc(sizeof(*dl)); | |
1958 | if (!dl) | |
1959 | return 1; | |
1960 | dl->major = 0; | |
1961 | dl->minor = 0; | |
1962 | dl->fd = -1; | |
1963 | dl->devname = strdup("missing"); | |
1964 | dl->index = i; | |
1965 | serialcpy(dl->serial, disk->serial); | |
1966 | dl->disk = *disk; | |
689c9bf3 | 1967 | dl->e = NULL; |
47ee5a45 DW |
1968 | dl->next = super->missing; |
1969 | super->missing = dl; | |
1970 | } | |
1971 | ||
1972 | return 0; | |
1973 | } | |
1974 | ||
cdddbdbc DW |
1975 | static int load_super_imsm_all(struct supertype *st, int fd, void **sbp, |
1976 | char *devname, int keep_fd) | |
1977 | { | |
1978 | struct mdinfo *sra; | |
1979 | struct intel_super *super; | |
1980 | struct mdinfo *sd, *best = NULL; | |
1981 | __u32 bestgen = 0; | |
1982 | __u32 gen; | |
1983 | char nm[20]; | |
1984 | int dfd; | |
1985 | int rv; | |
db575f3b DW |
1986 | int devnum = fd2devnum(fd); |
1987 | int retry; | |
dab4a513 | 1988 | enum sysfs_read_flags flags; |
cdddbdbc | 1989 | |
dab4a513 DW |
1990 | flags = GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE; |
1991 | if (mdmon_running(devnum)) | |
1992 | flags |= SKIP_GONE_DEVS; | |
1993 | ||
1994 | /* check if 'fd' an opened container */ | |
1995 | sra = sysfs_read(fd, 0, flags); | |
cdddbdbc DW |
1996 | if (!sra) |
1997 | return 1; | |
1998 | ||
1999 | if (sra->array.major_version != -1 || | |
2000 | sra->array.minor_version != -2 || | |
2001 | strcmp(sra->text_version, "imsm") != 0) | |
2002 | return 1; | |
2003 | ||
c2c087e6 | 2004 | super = alloc_super(0); |
cdddbdbc DW |
2005 | if (!super) |
2006 | return 1; | |
2007 | ||
d23fe947 | 2008 | /* find the most up to date disk in this array, skipping spares */ |
cdddbdbc DW |
2009 | for (sd = sra->devs; sd; sd = sd->next) { |
2010 | sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor); | |
2011 | dfd = dev_open(nm, keep_fd ? O_RDWR : O_RDONLY); | |
be2c0e38 | 2012 | if (dfd < 0) { |
cdddbdbc DW |
2013 | free_imsm(super); |
2014 | return 2; | |
2015 | } | |
2016 | rv = load_imsm_mpb(dfd, super, NULL); | |
db575f3b DW |
2017 | |
2018 | /* retry the load if we might have raced against mdmon */ | |
2019 | if (rv == 3 && mdmon_running(devnum)) | |
2020 | for (retry = 0; retry < 3; retry++) { | |
2021 | usleep(3000); | |
2022 | rv = load_imsm_mpb(dfd, super, NULL); | |
2023 | if (rv != 3) | |
2024 | break; | |
2025 | } | |
cdddbdbc DW |
2026 | if (!keep_fd) |
2027 | close(dfd); | |
2028 | if (rv == 0) { | |
d23fe947 DW |
2029 | if (super->anchor->num_raid_devs == 0) |
2030 | gen = 0; | |
2031 | else | |
2032 | gen = __le32_to_cpu(super->anchor->generation_num); | |
cdddbdbc DW |
2033 | if (!best || gen > bestgen) { |
2034 | bestgen = gen; | |
2035 | best = sd; | |
2036 | } | |
2037 | } else { | |
2038 | free_imsm(super); | |
db575f3b | 2039 | return rv; |
cdddbdbc DW |
2040 | } |
2041 | } | |
2042 | ||
2043 | if (!best) { | |
2044 | free_imsm(super); | |
2045 | return 1; | |
2046 | } | |
2047 | ||
2048 | /* load the most up to date anchor */ | |
2049 | sprintf(nm, "%d:%d", best->disk.major, best->disk.minor); | |
2050 | dfd = dev_open(nm, O_RDONLY); | |
be2c0e38 | 2051 | if (dfd < 0) { |
cdddbdbc DW |
2052 | free_imsm(super); |
2053 | return 1; | |
2054 | } | |
2055 | rv = load_imsm_mpb(dfd, super, NULL); | |
2056 | close(dfd); | |
2057 | if (rv != 0) { | |
2058 | free_imsm(super); | |
2059 | return 2; | |
2060 | } | |
2061 | ||
d23fe947 | 2062 | /* re-parse the disk list with the current anchor */ |
cdddbdbc DW |
2063 | for (sd = sra->devs ; sd ; sd = sd->next) { |
2064 | sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor); | |
2065 | dfd = dev_open(nm, keep_fd? O_RDWR : O_RDONLY); | |
be2c0e38 | 2066 | if (dfd < 0) { |
cdddbdbc DW |
2067 | free_imsm(super); |
2068 | return 2; | |
2069 | } | |
2070 | load_imsm_disk(dfd, super, NULL, keep_fd); | |
2071 | if (!keep_fd) | |
2072 | close(dfd); | |
2073 | } | |
2074 | ||
47ee5a45 DW |
2075 | |
2076 | if (find_missing(super) != 0) { | |
2077 | free_imsm(super); | |
2078 | return 2; | |
2079 | } | |
2080 | ||
f7e7067b | 2081 | if (st->subarray[0]) { |
949c47a0 | 2082 | if (atoi(st->subarray) <= super->anchor->num_raid_devs) |
bf5a934a DW |
2083 | super->current_vol = atoi(st->subarray); |
2084 | else | |
2085 | return 1; | |
f7e7067b NB |
2086 | } |
2087 | ||
cdddbdbc | 2088 | *sbp = super; |
db575f3b | 2089 | st->container_dev = devnum; |
cdddbdbc | 2090 | if (st->ss == NULL) { |
bf5a934a | 2091 | st->ss = &super_imsm; |
cdddbdbc DW |
2092 | st->minor_version = 0; |
2093 | st->max_devs = IMSM_MAX_DEVICES; | |
2094 | } | |
352452c3 | 2095 | st->loaded_container = 1; |
cdddbdbc DW |
2096 | |
2097 | return 0; | |
2098 | } | |
2099 | #endif | |
2100 | ||
2101 | static int load_super_imsm(struct supertype *st, int fd, char *devname) | |
2102 | { | |
2103 | struct intel_super *super; | |
2104 | int rv; | |
2105 | ||
2106 | #ifndef MDASSEMBLE | |
3dbccbcf | 2107 | if (load_super_imsm_all(st, fd, &st->sb, devname, 1) == 0) |
cdddbdbc DW |
2108 | return 0; |
2109 | #endif | |
f7e7067b NB |
2110 | if (st->subarray[0]) |
2111 | return 1; /* FIXME */ | |
cdddbdbc | 2112 | |
c2c087e6 | 2113 | super = alloc_super(0); |
cdddbdbc DW |
2114 | if (!super) { |
2115 | fprintf(stderr, | |
2116 | Name ": malloc of %zu failed.\n", | |
2117 | sizeof(*super)); | |
2118 | return 1; | |
2119 | } | |
2120 | ||
2121 | rv = load_imsm_mpb(fd, super, devname); | |
2122 | ||
2123 | if (rv) { | |
2124 | if (devname) | |
2125 | fprintf(stderr, | |
2126 | Name ": Failed to load all information " | |
2127 | "sections on %s\n", devname); | |
2128 | free_imsm(super); | |
2129 | return rv; | |
2130 | } | |
2131 | ||
2132 | st->sb = super; | |
2133 | if (st->ss == NULL) { | |
2134 | st->ss = &super_imsm; | |
2135 | st->minor_version = 0; | |
2136 | st->max_devs = IMSM_MAX_DEVICES; | |
2137 | } | |
352452c3 | 2138 | st->loaded_container = 0; |
cdddbdbc DW |
2139 | |
2140 | return 0; | |
2141 | } | |
2142 | ||
ef6ffade DW |
2143 | static __u16 info_to_blocks_per_strip(mdu_array_info_t *info) |
2144 | { | |
2145 | if (info->level == 1) | |
2146 | return 128; | |
2147 | return info->chunk_size >> 9; | |
2148 | } | |
2149 | ||
2150 | static __u32 info_to_num_data_stripes(mdu_array_info_t *info) | |
2151 | { | |
2152 | __u32 num_stripes; | |
2153 | ||
2154 | num_stripes = (info->size * 2) / info_to_blocks_per_strip(info); | |
2155 | if (info->level == 1) | |
2156 | num_stripes /= 2; | |
2157 | ||
2158 | return num_stripes; | |
2159 | } | |
2160 | ||
fcfd9599 DW |
2161 | static __u32 info_to_blocks_per_member(mdu_array_info_t *info) |
2162 | { | |
4025c288 DW |
2163 | if (info->level == 1) |
2164 | return info->size * 2; | |
2165 | else | |
2166 | return (info->size * 2) & ~(info_to_blocks_per_strip(info) - 1); | |
fcfd9599 DW |
2167 | } |
2168 | ||
4d1313e9 DW |
2169 | static void imsm_update_version_info(struct intel_super *super) |
2170 | { | |
2171 | /* update the version and attributes */ | |
2172 | struct imsm_super *mpb = super->anchor; | |
2173 | char *version; | |
2174 | struct imsm_dev *dev; | |
2175 | struct imsm_map *map; | |
2176 | int i; | |
2177 | ||
2178 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
2179 | dev = get_imsm_dev(super, i); | |
2180 | map = get_imsm_map(dev, 0); | |
2181 | if (__le32_to_cpu(dev->size_high) > 0) | |
2182 | mpb->attributes |= MPB_ATTRIB_2TB; | |
2183 | ||
2184 | /* FIXME detect when an array spans a port multiplier */ | |
2185 | #if 0 | |
2186 | mpb->attributes |= MPB_ATTRIB_PM; | |
2187 | #endif | |
2188 | ||
2189 | if (mpb->num_raid_devs > 1 || | |
2190 | mpb->attributes != MPB_ATTRIB_CHECKSUM_VERIFY) { | |
2191 | version = MPB_VERSION_ATTRIBS; | |
2192 | switch (get_imsm_raid_level(map)) { | |
2193 | case 0: mpb->attributes |= MPB_ATTRIB_RAID0; break; | |
2194 | case 1: mpb->attributes |= MPB_ATTRIB_RAID1; break; | |
2195 | case 10: mpb->attributes |= MPB_ATTRIB_RAID10; break; | |
2196 | case 5: mpb->attributes |= MPB_ATTRIB_RAID5; break; | |
2197 | } | |
2198 | } else { | |
2199 | if (map->num_members >= 5) | |
2200 | version = MPB_VERSION_5OR6_DISK_ARRAY; | |
2201 | else if (dev->status == DEV_CLONE_N_GO) | |
2202 | version = MPB_VERSION_CNG; | |
2203 | else if (get_imsm_raid_level(map) == 5) | |
2204 | version = MPB_VERSION_RAID5; | |
2205 | else if (map->num_members >= 3) | |
2206 | version = MPB_VERSION_3OR4_DISK_ARRAY; | |
2207 | else if (get_imsm_raid_level(map) == 1) | |
2208 | version = MPB_VERSION_RAID1; | |
2209 | else | |
2210 | version = MPB_VERSION_RAID0; | |
2211 | } | |
2212 | strcpy(((char *) mpb->sig) + strlen(MPB_SIGNATURE), version); | |
2213 | } | |
2214 | } | |
2215 | ||
8b353278 DW |
2216 | static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info, |
2217 | unsigned long long size, char *name, | |
2218 | char *homehost, int *uuid) | |
cdddbdbc | 2219 | { |
c2c087e6 DW |
2220 | /* We are creating a volume inside a pre-existing container. |
2221 | * so st->sb is already set. | |
2222 | */ | |
2223 | struct intel_super *super = st->sb; | |
949c47a0 | 2224 | struct imsm_super *mpb = super->anchor; |
ba2de7ba | 2225 | struct intel_dev *dv; |
c2c087e6 DW |
2226 | struct imsm_dev *dev; |
2227 | struct imsm_vol *vol; | |
2228 | struct imsm_map *map; | |
2229 | int idx = mpb->num_raid_devs; | |
2230 | int i; | |
2231 | unsigned long long array_blocks; | |
2c092cad | 2232 | size_t size_old, size_new; |
cdddbdbc | 2233 | |
88c32bb1 | 2234 | if (super->orom && mpb->num_raid_devs >= super->orom->vpa) { |
c2c087e6 | 2235 | fprintf(stderr, Name": This imsm-container already has the " |
88c32bb1 | 2236 | "maximum of %d volumes\n", super->orom->vpa); |
c2c087e6 DW |
2237 | return 0; |
2238 | } | |
2239 | ||
2c092cad DW |
2240 | /* ensure the mpb is large enough for the new data */ |
2241 | size_old = __le32_to_cpu(mpb->mpb_size); | |
2242 | size_new = disks_to_mpb_size(info->nr_disks); | |
2243 | if (size_new > size_old) { | |
2244 | void *mpb_new; | |
2245 | size_t size_round = ROUND_UP(size_new, 512); | |
2246 | ||
2247 | if (posix_memalign(&mpb_new, 512, size_round) != 0) { | |
2248 | fprintf(stderr, Name": could not allocate new mpb\n"); | |
2249 | return 0; | |
2250 | } | |
2251 | memcpy(mpb_new, mpb, size_old); | |
2252 | free(mpb); | |
2253 | mpb = mpb_new; | |
949c47a0 | 2254 | super->anchor = mpb_new; |
2c092cad DW |
2255 | mpb->mpb_size = __cpu_to_le32(size_new); |
2256 | memset(mpb_new + size_old, 0, size_round - size_old); | |
2257 | } | |
bf5a934a | 2258 | super->current_vol = idx; |
d23fe947 DW |
2259 | /* when creating the first raid device in this container set num_disks |
2260 | * to zero, i.e. delete this spare and add raid member devices in | |
2261 | * add_to_super_imsm_volume() | |
2262 | */ | |
2263 | if (super->current_vol == 0) | |
2264 | mpb->num_disks = 0; | |
5a038140 DW |
2265 | |
2266 | for (i = 0; i < super->current_vol; i++) { | |
2267 | dev = get_imsm_dev(super, i); | |
2268 | if (strncmp((char *) dev->volume, name, | |
2269 | MAX_RAID_SERIAL_LEN) == 0) { | |
2270 | fprintf(stderr, Name": '%s' is already defined for this container\n", | |
2271 | name); | |
2272 | return 0; | |
2273 | } | |
2274 | } | |
2275 | ||
bf5a934a | 2276 | sprintf(st->subarray, "%d", idx); |
ba2de7ba DW |
2277 | dv = malloc(sizeof(*dv)); |
2278 | if (!dv) { | |
2279 | fprintf(stderr, Name ": failed to allocate device list entry\n"); | |
2280 | return 0; | |
2281 | } | |
949c47a0 DW |
2282 | dev = malloc(sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1)); |
2283 | if (!dev) { | |
ba2de7ba | 2284 | free(dv); |
949c47a0 DW |
2285 | fprintf(stderr, Name": could not allocate raid device\n"); |
2286 | return 0; | |
2287 | } | |
c2c087e6 | 2288 | strncpy((char *) dev->volume, name, MAX_RAID_SERIAL_LEN); |
03bcbc65 DW |
2289 | if (info->level == 1) |
2290 | array_blocks = info_to_blocks_per_member(info); | |
2291 | else | |
2292 | array_blocks = calc_array_size(info->level, info->raid_disks, | |
2293 | info->layout, info->chunk_size, | |
2294 | info->size*2); | |
c2c087e6 DW |
2295 | dev->size_low = __cpu_to_le32((__u32) array_blocks); |
2296 | dev->size_high = __cpu_to_le32((__u32) (array_blocks >> 32)); | |
2297 | dev->status = __cpu_to_le32(0); | |
2298 | dev->reserved_blocks = __cpu_to_le32(0); | |
2299 | vol = &dev->vol; | |
2300 | vol->migr_state = 0; | |
e3bba0e0 | 2301 | vol->migr_type = MIGR_INIT; |
c2c087e6 | 2302 | vol->dirty = 0; |
f8f603f1 | 2303 | vol->curr_migr_unit = 0; |
a965f303 | 2304 | map = get_imsm_map(dev, 0); |
0dcecb2e | 2305 | map->pba_of_lba0 = __cpu_to_le32(super->create_offset); |
fcfd9599 | 2306 | map->blocks_per_member = __cpu_to_le32(info_to_blocks_per_member(info)); |
ef6ffade DW |
2307 | map->blocks_per_strip = __cpu_to_le16(info_to_blocks_per_strip(info)); |
2308 | map->num_data_stripes = __cpu_to_le32(info_to_num_data_stripes(info)); | |
0556e1a2 | 2309 | map->failed_disk_num = ~0; |
c2c087e6 DW |
2310 | map->map_state = info->level ? IMSM_T_STATE_UNINITIALIZED : |
2311 | IMSM_T_STATE_NORMAL; | |
ef6ffade DW |
2312 | |
2313 | if (info->level == 1 && info->raid_disks > 2) { | |
2314 | fprintf(stderr, Name": imsm does not support more than 2 disks" | |
2315 | "in a raid1 volume\n"); | |
2316 | return 0; | |
2317 | } | |
4d1313e9 | 2318 | if (info->level == 10) { |
c2c087e6 | 2319 | map->raid_level = 1; |
4d1313e9 DW |
2320 | map->num_domains = info->raid_disks / 2; |
2321 | } else { | |
c2c087e6 | 2322 | map->raid_level = info->level; |
4d1313e9 DW |
2323 | map->num_domains = !!map->raid_level; |
2324 | } | |
ef6ffade | 2325 | |
c2c087e6 DW |
2326 | map->num_members = info->raid_disks; |
2327 | for (i = 0; i < map->num_members; i++) { | |
2328 | /* initialized in add_to_super */ | |
be73972f | 2329 | set_imsm_ord_tbl_ent(map, i, 0); |
c2c087e6 | 2330 | } |
949c47a0 | 2331 | mpb->num_raid_devs++; |
ba2de7ba DW |
2332 | |
2333 | dv->dev = dev; | |
2334 | dv->index = super->current_vol; | |
2335 | dv->next = super->devlist; | |
2336 | super->devlist = dv; | |
c2c087e6 | 2337 | |
4d1313e9 DW |
2338 | imsm_update_version_info(super); |
2339 | ||
c2c087e6 | 2340 | return 1; |
cdddbdbc DW |
2341 | } |
2342 | ||
bf5a934a DW |
2343 | static int init_super_imsm(struct supertype *st, mdu_array_info_t *info, |
2344 | unsigned long long size, char *name, | |
2345 | char *homehost, int *uuid) | |
2346 | { | |
2347 | /* This is primarily called by Create when creating a new array. | |
2348 | * We will then get add_to_super called for each component, and then | |
2349 | * write_init_super called to write it out to each device. | |
2350 | * For IMSM, Create can create on fresh devices or on a pre-existing | |
2351 | * array. | |
2352 | * To create on a pre-existing array a different method will be called. | |
2353 | * This one is just for fresh drives. | |
2354 | */ | |
2355 | struct intel_super *super; | |
2356 | struct imsm_super *mpb; | |
2357 | size_t mpb_size; | |
4d1313e9 | 2358 | char *version; |
bf5a934a DW |
2359 | |
2360 | if (!info) { | |
2361 | st->sb = NULL; | |
2362 | return 0; | |
2363 | } | |
2364 | if (st->sb) | |
2365 | return init_super_imsm_volume(st, info, size, name, homehost, | |
2366 | uuid); | |
2367 | ||
2368 | super = alloc_super(1); | |
2369 | if (!super) | |
2370 | return 0; | |
2371 | mpb_size = disks_to_mpb_size(info->nr_disks); | |
ef649044 | 2372 | if (posix_memalign(&super->buf, 512, mpb_size) != 0) { |
bf5a934a DW |
2373 | free(super); |
2374 | return 0; | |
2375 | } | |
ef649044 | 2376 | mpb = super->buf; |
bf5a934a DW |
2377 | memset(mpb, 0, mpb_size); |
2378 | ||
4d1313e9 DW |
2379 | mpb->attributes = MPB_ATTRIB_CHECKSUM_VERIFY; |
2380 | ||
2381 | version = (char *) mpb->sig; | |
2382 | strcpy(version, MPB_SIGNATURE); | |
2383 | version += strlen(MPB_SIGNATURE); | |
2384 | strcpy(version, MPB_VERSION_RAID0); | |
bf5a934a DW |
2385 | mpb->mpb_size = mpb_size; |
2386 | ||
bf5a934a DW |
2387 | st->sb = super; |
2388 | return 1; | |
2389 | } | |
2390 | ||
0e600426 | 2391 | #ifndef MDASSEMBLE |
f20c3968 | 2392 | static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk, |
bf5a934a DW |
2393 | int fd, char *devname) |
2394 | { | |
2395 | struct intel_super *super = st->sb; | |
d23fe947 | 2396 | struct imsm_super *mpb = super->anchor; |
bf5a934a DW |
2397 | struct dl *dl; |
2398 | struct imsm_dev *dev; | |
2399 | struct imsm_map *map; | |
bf5a934a | 2400 | |
949c47a0 | 2401 | dev = get_imsm_dev(super, super->current_vol); |
a965f303 | 2402 | map = get_imsm_map(dev, 0); |
bf5a934a | 2403 | |
208933a7 N |
2404 | if (! (dk->state & (1<<MD_DISK_SYNC))) { |
2405 | fprintf(stderr, Name ": %s: Cannot add spare devices to IMSM volume\n", | |
2406 | devname); | |
2407 | return 1; | |
2408 | } | |
2409 | ||
efb30e7f DW |
2410 | if (fd == -1) { |
2411 | /* we're doing autolayout so grab the pre-marked (in | |
2412 | * validate_geometry) raid_disk | |
2413 | */ | |
2414 | for (dl = super->disks; dl; dl = dl->next) | |
2415 | if (dl->raiddisk == dk->raid_disk) | |
2416 | break; | |
2417 | } else { | |
2418 | for (dl = super->disks; dl ; dl = dl->next) | |
2419 | if (dl->major == dk->major && | |
2420 | dl->minor == dk->minor) | |
2421 | break; | |
2422 | } | |
d23fe947 | 2423 | |
208933a7 N |
2424 | if (!dl) { |
2425 | fprintf(stderr, Name ": %s is not a member of the same container\n", devname); | |
f20c3968 | 2426 | return 1; |
208933a7 | 2427 | } |
bf5a934a | 2428 | |
d23fe947 DW |
2429 | /* add a pristine spare to the metadata */ |
2430 | if (dl->index < 0) { | |
2431 | dl->index = super->anchor->num_disks; | |
2432 | super->anchor->num_disks++; | |
2433 | } | |
be73972f | 2434 | set_imsm_ord_tbl_ent(map, dk->number, dl->index); |
f2f27e63 | 2435 | dl->disk.status = CONFIGURED_DISK | USABLE_DISK; |
d23fe947 DW |
2436 | |
2437 | /* if we are creating the first raid device update the family number */ | |
2438 | if (super->current_vol == 0) { | |
2439 | __u32 sum; | |
2440 | struct imsm_dev *_dev = __get_imsm_dev(mpb, 0); | |
2441 | struct imsm_disk *_disk = __get_imsm_disk(mpb, dl->index); | |
2442 | ||
2443 | *_dev = *dev; | |
2444 | *_disk = dl->disk; | |
2445 | sum = __gen_imsm_checksum(mpb); | |
2446 | mpb->family_num = __cpu_to_le32(sum); | |
2447 | } | |
f20c3968 DW |
2448 | |
2449 | return 0; | |
bf5a934a DW |
2450 | } |
2451 | ||
f20c3968 | 2452 | static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk, |
cdddbdbc DW |
2453 | int fd, char *devname) |
2454 | { | |
c2c087e6 | 2455 | struct intel_super *super = st->sb; |
c2c087e6 DW |
2456 | struct dl *dd; |
2457 | unsigned long long size; | |
f2f27e63 | 2458 | __u32 id; |
c2c087e6 DW |
2459 | int rv; |
2460 | struct stat stb; | |
2461 | ||
88c32bb1 DW |
2462 | /* if we are on an RAID enabled platform check that the disk is |
2463 | * attached to the raid controller | |
2464 | */ | |
2465 | if (super->hba && !disk_attached_to_hba(fd, super->hba)) { | |
2466 | fprintf(stderr, | |
2467 | Name ": %s is not attached to the raid controller: %s\n", | |
2468 | devname ? : "disk", super->hba); | |
2469 | return 1; | |
2470 | } | |
2471 | ||
f20c3968 DW |
2472 | if (super->current_vol >= 0) |
2473 | return add_to_super_imsm_volume(st, dk, fd, devname); | |
bf5a934a | 2474 | |
c2c087e6 DW |
2475 | fstat(fd, &stb); |
2476 | dd = malloc(sizeof(*dd)); | |
b9f594fe | 2477 | if (!dd) { |
c2c087e6 DW |
2478 | fprintf(stderr, |
2479 | Name ": malloc failed %s:%d.\n", __func__, __LINE__); | |
f20c3968 | 2480 | return 1; |
c2c087e6 DW |
2481 | } |
2482 | memset(dd, 0, sizeof(*dd)); | |
2483 | dd->major = major(stb.st_rdev); | |
2484 | dd->minor = minor(stb.st_rdev); | |
b9f594fe | 2485 | dd->index = -1; |
c2c087e6 | 2486 | dd->devname = devname ? strdup(devname) : NULL; |
c2c087e6 | 2487 | dd->fd = fd; |
689c9bf3 | 2488 | dd->e = NULL; |
c2c087e6 DW |
2489 | rv = imsm_read_serial(fd, devname, dd->serial); |
2490 | if (rv) { | |
2491 | fprintf(stderr, | |
0030e8d6 | 2492 | Name ": failed to retrieve scsi serial, aborting\n"); |
949c47a0 | 2493 | free(dd); |
0030e8d6 | 2494 | abort(); |
c2c087e6 DW |
2495 | } |
2496 | ||
c2c087e6 DW |
2497 | get_dev_size(fd, NULL, &size); |
2498 | size /= 512; | |
1f24f035 | 2499 | serialcpy(dd->disk.serial, dd->serial); |
b9f594fe | 2500 | dd->disk.total_blocks = __cpu_to_le32(size); |
f2f27e63 | 2501 | dd->disk.status = USABLE_DISK | SPARE_DISK; |
c2c087e6 | 2502 | if (sysfs_disk_to_scsi_id(fd, &id) == 0) |
b9f594fe | 2503 | dd->disk.scsi_id = __cpu_to_le32(id); |
c2c087e6 | 2504 | else |
b9f594fe | 2505 | dd->disk.scsi_id = __cpu_to_le32(0); |
43dad3d6 DW |
2506 | |
2507 | if (st->update_tail) { | |
2508 | dd->next = super->add; | |
2509 | super->add = dd; | |
2510 | } else { | |
2511 | dd->next = super->disks; | |
2512 | super->disks = dd; | |
2513 | } | |
f20c3968 DW |
2514 | |
2515 | return 0; | |
cdddbdbc DW |
2516 | } |
2517 | ||
c2c087e6 DW |
2518 | static int store_imsm_mpb(int fd, struct intel_super *super); |
2519 | ||
d23fe947 DW |
2520 | /* spare records have their own family number and do not have any defined raid |
2521 | * devices | |
2522 | */ | |
2523 | static int write_super_imsm_spares(struct intel_super *super, int doclose) | |
2524 | { | |
2525 | struct imsm_super mpb_save; | |
2526 | struct imsm_super *mpb = super->anchor; | |
2527 | __u32 sum; | |
2528 | struct dl *d; | |
2529 | ||
2530 | mpb_save = *mpb; | |
2531 | mpb->num_raid_devs = 0; | |
2532 | mpb->num_disks = 1; | |
2533 | mpb->mpb_size = sizeof(struct imsm_super); | |
2534 | mpb->generation_num = __cpu_to_le32(1UL); | |
2535 | ||
2536 | for (d = super->disks; d; d = d->next) { | |
8796fdc4 | 2537 | if (d->index != -1) |
d23fe947 DW |
2538 | continue; |
2539 | ||
2540 | mpb->disk[0] = d->disk; | |
2541 | sum = __gen_imsm_checksum(mpb); | |
2542 | mpb->family_num = __cpu_to_le32(sum); | |
2543 | sum = __gen_imsm_checksum(mpb); | |
2544 | mpb->check_sum = __cpu_to_le32(sum); | |
2545 | ||
2546 | if (store_imsm_mpb(d->fd, super)) { | |
2547 | fprintf(stderr, "%s: failed for device %d:%d %s\n", | |
2548 | __func__, d->major, d->minor, strerror(errno)); | |
2549 | *mpb = mpb_save; | |
e74255d9 | 2550 | return 1; |
d23fe947 DW |
2551 | } |
2552 | if (doclose) { | |
2553 | close(d->fd); | |
2554 | d->fd = -1; | |
2555 | } | |
2556 | } | |
2557 | ||
2558 | *mpb = mpb_save; | |
e74255d9 | 2559 | return 0; |
d23fe947 DW |
2560 | } |
2561 | ||
c2c087e6 | 2562 | static int write_super_imsm(struct intel_super *super, int doclose) |
cdddbdbc | 2563 | { |
949c47a0 | 2564 | struct imsm_super *mpb = super->anchor; |
c2c087e6 DW |
2565 | struct dl *d; |
2566 | __u32 generation; | |
2567 | __u32 sum; | |
d23fe947 | 2568 | int spares = 0; |
949c47a0 | 2569 | int i; |
a48ac0a8 | 2570 | __u32 mpb_size = sizeof(struct imsm_super) - sizeof(struct imsm_disk); |
cdddbdbc | 2571 | |
c2c087e6 DW |
2572 | /* 'generation' is incremented everytime the metadata is written */ |
2573 | generation = __le32_to_cpu(mpb->generation_num); | |
2574 | generation++; | |
2575 | mpb->generation_num = __cpu_to_le32(generation); | |
2576 | ||
1ee1e9fc | 2577 | mpb_size += sizeof(struct imsm_disk) * mpb->num_disks; |
d23fe947 | 2578 | for (d = super->disks; d; d = d->next) { |
8796fdc4 | 2579 | if (d->index == -1) |
d23fe947 | 2580 | spares++; |
1ee1e9fc | 2581 | else |
d23fe947 | 2582 | mpb->disk[d->index] = d->disk; |
d23fe947 | 2583 | } |
47ee5a45 DW |
2584 | for (d = super->missing; d; d = d->next) |
2585 | mpb->disk[d->index] = d->disk; | |
b9f594fe | 2586 | |
949c47a0 DW |
2587 | for (i = 0; i < mpb->num_raid_devs; i++) { |
2588 | struct imsm_dev *dev = __get_imsm_dev(mpb, i); | |
2589 | ||
ba2de7ba | 2590 | imsm_copy_dev(dev, get_imsm_dev(super, i)); |
a48ac0a8 | 2591 | mpb_size += sizeof_imsm_dev(dev, 0); |
949c47a0 | 2592 | } |
a48ac0a8 DW |
2593 | mpb_size += __le32_to_cpu(mpb->bbm_log_size); |
2594 | mpb->mpb_size = __cpu_to_le32(mpb_size); | |
949c47a0 | 2595 | |
c2c087e6 | 2596 | /* recalculate checksum */ |
949c47a0 | 2597 | sum = __gen_imsm_checksum(mpb); |
c2c087e6 DW |
2598 | mpb->check_sum = __cpu_to_le32(sum); |
2599 | ||
d23fe947 | 2600 | /* write the mpb for disks that compose raid devices */ |
c2c087e6 | 2601 | for (d = super->disks; d ; d = d->next) { |
d23fe947 DW |
2602 | if (d->index < 0) |
2603 | continue; | |
8796fdc4 | 2604 | if (store_imsm_mpb(d->fd, super)) |
c2c087e6 DW |
2605 | fprintf(stderr, "%s: failed for device %d:%d %s\n", |
2606 | __func__, d->major, d->minor, strerror(errno)); | |
c2c087e6 DW |
2607 | if (doclose) { |
2608 | close(d->fd); | |
2609 | d->fd = -1; | |
2610 | } | |
2611 | } | |
2612 | ||
d23fe947 DW |
2613 | if (spares) |
2614 | return write_super_imsm_spares(super, doclose); | |
2615 | ||
e74255d9 | 2616 | return 0; |
c2c087e6 DW |
2617 | } |
2618 | ||
0e600426 | 2619 | |
43dad3d6 DW |
2620 | static int create_array(struct supertype *st) |
2621 | { | |
2622 | size_t len; | |
2623 | struct imsm_update_create_array *u; | |
2624 | struct intel_super *super = st->sb; | |
2625 | struct imsm_dev *dev = get_imsm_dev(super, super->current_vol); | |
54c2c1ea DW |
2626 | struct imsm_map *map = get_imsm_map(dev, 0); |
2627 | struct disk_info *inf; | |
2628 | struct imsm_disk *disk; | |
2629 | int i; | |
2630 | int idx; | |
43dad3d6 | 2631 | |
54c2c1ea DW |
2632 | len = sizeof(*u) - sizeof(*dev) + sizeof_imsm_dev(dev, 0) + |
2633 | sizeof(*inf) * map->num_members; | |
43dad3d6 DW |
2634 | u = malloc(len); |
2635 | if (!u) { | |
2636 | fprintf(stderr, "%s: failed to allocate update buffer\n", | |
2637 | __func__); | |
2638 | return 1; | |
2639 | } | |
2640 | ||
2641 | u->type = update_create_array; | |
2642 | u->dev_idx = super->current_vol; | |
2643 | imsm_copy_dev(&u->dev, dev); | |
54c2c1ea DW |
2644 | inf = get_disk_info(u); |
2645 | for (i = 0; i < map->num_members; i++) { | |
2646 | idx = get_imsm_disk_idx(dev, i); | |
2647 | disk = get_imsm_disk(super, idx); | |
2648 | serialcpy(inf[i].serial, disk->serial); | |
2649 | } | |
43dad3d6 DW |
2650 | append_metadata_update(st, u, len); |
2651 | ||
2652 | return 0; | |
2653 | } | |
2654 | ||
7801ac20 | 2655 | static int _add_disk(struct supertype *st) |
43dad3d6 DW |
2656 | { |
2657 | struct intel_super *super = st->sb; | |
2658 | size_t len; | |
2659 | struct imsm_update_add_disk *u; | |
2660 | ||
2661 | if (!super->add) | |
2662 | return 0; | |
2663 | ||
2664 | len = sizeof(*u); | |
2665 | u = malloc(len); | |
2666 | if (!u) { | |
2667 | fprintf(stderr, "%s: failed to allocate update buffer\n", | |
2668 | __func__); | |
2669 | return 1; | |
2670 | } | |
2671 | ||
2672 | u->type = update_add_disk; | |
2673 | append_metadata_update(st, u, len); | |
2674 | ||
2675 | return 0; | |
2676 | } | |
2677 | ||
c2c087e6 DW |
2678 | static int write_init_super_imsm(struct supertype *st) |
2679 | { | |
8273f55e | 2680 | if (st->update_tail) { |
43dad3d6 DW |
2681 | /* queue the recently created array / added disk |
2682 | * as a metadata update */ | |
8273f55e | 2683 | struct intel_super *super = st->sb; |
8273f55e | 2684 | struct dl *d; |
43dad3d6 | 2685 | int rv; |
8273f55e | 2686 | |
43dad3d6 DW |
2687 | /* determine if we are creating a volume or adding a disk */ |
2688 | if (super->current_vol < 0) { | |
2689 | /* in the add disk case we are running in mdmon | |
2690 | * context, so don't close fd's | |
2691 | */ | |
7801ac20 | 2692 | return _add_disk(st); |
43dad3d6 DW |
2693 | } else |
2694 | rv = create_array(st); | |
8273f55e DW |
2695 | |
2696 | for (d = super->disks; d ; d = d->next) { | |
2697 | close(d->fd); | |
2698 | d->fd = -1; | |
2699 | } | |
2700 | ||
43dad3d6 | 2701 | return rv; |
8273f55e DW |
2702 | } else |
2703 | return write_super_imsm(st->sb, 1); | |
cdddbdbc | 2704 | } |
0e600426 | 2705 | #endif |
cdddbdbc DW |
2706 | |
2707 | static int store_zero_imsm(struct supertype *st, int fd) | |
2708 | { | |
551c80c1 | 2709 | unsigned long long dsize; |
6416d527 | 2710 | void *buf; |
551c80c1 DW |
2711 | |
2712 | get_dev_size(fd, NULL, &dsize); | |
2713 | ||
2714 | /* first block is stored on second to last sector of the disk */ | |
2715 | if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0) | |
2716 | return 1; | |
2717 | ||
ad97895e DW |
2718 | if (posix_memalign(&buf, 512, 512) != 0) |
2719 | return 1; | |
2720 | ||
eb7ea463 DW |
2721 | memset(buf, 0, 512); |
2722 | if (write(fd, buf, 512) != 512) | |
551c80c1 | 2723 | return 1; |
cdddbdbc DW |
2724 | return 0; |
2725 | } | |
2726 | ||
0e600426 N |
2727 | static int imsm_bbm_log_size(struct imsm_super *mpb) |
2728 | { | |
2729 | return __le32_to_cpu(mpb->bbm_log_size); | |
2730 | } | |
2731 | ||
2732 | #ifndef MDASSEMBLE | |
cdddbdbc DW |
2733 | static int validate_geometry_imsm_container(struct supertype *st, int level, |
2734 | int layout, int raiddisks, int chunk, | |
c2c087e6 | 2735 | unsigned long long size, char *dev, |
2c514b71 NB |
2736 | unsigned long long *freesize, |
2737 | int verbose) | |
cdddbdbc | 2738 | { |
c2c087e6 DW |
2739 | int fd; |
2740 | unsigned long long ldsize; | |
88c32bb1 | 2741 | const struct imsm_orom *orom; |
cdddbdbc | 2742 | |
c2c087e6 DW |
2743 | if (level != LEVEL_CONTAINER) |
2744 | return 0; | |
2745 | if (!dev) | |
2746 | return 1; | |
2747 | ||
88c32bb1 DW |
2748 | if (check_env("IMSM_NO_PLATFORM")) |
2749 | orom = NULL; | |
2750 | else | |
2751 | orom = find_imsm_orom(); | |
2752 | if (orom && raiddisks > orom->tds) { | |
2753 | if (verbose) | |
2754 | fprintf(stderr, Name ": %d exceeds maximum number of" | |
2755 | " platform supported disks: %d\n", | |
2756 | raiddisks, orom->tds); | |
2757 | return 0; | |
2758 | } | |
2759 | ||
c2c087e6 DW |
2760 | fd = open(dev, O_RDONLY|O_EXCL, 0); |
2761 | if (fd < 0) { | |
2c514b71 NB |
2762 | if (verbose) |
2763 | fprintf(stderr, Name ": imsm: Cannot open %s: %s\n", | |
2764 | dev, strerror(errno)); | |
c2c087e6 DW |
2765 | return 0; |
2766 | } | |
2767 | if (!get_dev_size(fd, dev, &ldsize)) { | |
2768 | close(fd); | |
2769 | return 0; | |
2770 | } | |
2771 | close(fd); | |
2772 | ||
2773 | *freesize = avail_size_imsm(st, ldsize >> 9); | |
2774 | ||
2775 | return 1; | |
cdddbdbc DW |
2776 | } |
2777 | ||
0dcecb2e DW |
2778 | static unsigned long long find_size(struct extent *e, int *idx, int num_extents) |
2779 | { | |
2780 | const unsigned long long base_start = e[*idx].start; | |
2781 | unsigned long long end = base_start + e[*idx].size; | |
2782 | int i; | |
2783 | ||
2784 | if (base_start == end) | |
2785 | return 0; | |
2786 | ||
2787 | *idx = *idx + 1; | |
2788 | for (i = *idx; i < num_extents; i++) { | |
2789 | /* extend overlapping extents */ | |
2790 | if (e[i].start >= base_start && | |
2791 | e[i].start <= end) { | |
2792 | if (e[i].size == 0) | |
2793 | return 0; | |
2794 | if (e[i].start + e[i].size > end) | |
2795 | end = e[i].start + e[i].size; | |
2796 | } else if (e[i].start > end) { | |
2797 | *idx = i; | |
2798 | break; | |
2799 | } | |
2800 | } | |
2801 | ||
2802 | return end - base_start; | |
2803 | } | |
2804 | ||
2805 | static unsigned long long merge_extents(struct intel_super *super, int sum_extents) | |
2806 | { | |
2807 | /* build a composite disk with all known extents and generate a new | |
2808 | * 'maxsize' given the "all disks in an array must share a common start | |
2809 | * offset" constraint | |
2810 | */ | |
2811 | struct extent *e = calloc(sum_extents, sizeof(*e)); | |
2812 | struct dl *dl; | |
2813 | int i, j; | |
2814 | int start_extent; | |
2815 | unsigned long long pos; | |
2816 | unsigned long long start; | |
2817 | unsigned long long maxsize; | |
2818 | unsigned long reserve; | |
2819 | ||
2820 | if (!e) | |
2821 | return ~0ULL; /* error */ | |
2822 | ||
2823 | /* coalesce and sort all extents. also, check to see if we need to | |
2824 | * reserve space between member arrays | |
2825 | */ | |
2826 | j = 0; | |
2827 | for (dl = super->disks; dl; dl = dl->next) { | |
2828 | if (!dl->e) | |
2829 | continue; | |
2830 | for (i = 0; i < dl->extent_cnt; i++) | |
2831 | e[j++] = dl->e[i]; | |
2832 | } | |
2833 | qsort(e, sum_extents, sizeof(*e), cmp_extent); | |
2834 | ||
2835 | /* merge extents */ | |
2836 | i = 0; | |
2837 | j = 0; | |
2838 | while (i < sum_extents) { | |
2839 | e[j].start = e[i].start; | |
2840 | e[j].size = find_size(e, &i, sum_extents); | |
2841 | j++; | |
2842 | if (e[j-1].size == 0) | |
2843 | break; | |
2844 | } | |
2845 | ||
2846 | pos = 0; | |
2847 | maxsize = 0; | |
2848 | start_extent = 0; | |
2849 | i = 0; | |
2850 | do { | |
2851 | unsigned long long esize; | |
2852 | ||
2853 | esize = e[i].start - pos; | |
2854 | if (esize >= maxsize) { | |
2855 | maxsize = esize; | |
2856 | start = pos; | |
2857 | start_extent = i; | |
2858 | } | |
2859 | pos = e[i].start + e[i].size; | |
2860 | i++; | |
2861 | } while (e[i-1].size); | |
2862 | free(e); | |
2863 | ||
2864 | if (start_extent > 0) | |
2865 | reserve = IMSM_RESERVED_SECTORS; /* gap between raid regions */ | |
2866 | else | |
2867 | reserve = 0; | |
2868 | ||
2869 | if (maxsize < reserve) | |
2870 | return ~0ULL; | |
2871 | ||
2872 | super->create_offset = ~((__u32) 0); | |
2873 | if (start + reserve > super->create_offset) | |
2874 | return ~0ULL; /* start overflows create_offset */ | |
2875 | super->create_offset = start + reserve; | |
2876 | ||
2877 | return maxsize - reserve; | |
2878 | } | |
2879 | ||
88c32bb1 DW |
2880 | static int is_raid_level_supported(const struct imsm_orom *orom, int level, int raiddisks) |
2881 | { | |
2882 | if (level < 0 || level == 6 || level == 4) | |
2883 | return 0; | |
2884 | ||
2885 | /* if we have an orom prevent invalid raid levels */ | |
2886 | if (orom) | |
2887 | switch (level) { | |
2888 | case 0: return imsm_orom_has_raid0(orom); | |
2889 | case 1: | |
2890 | if (raiddisks > 2) | |
2891 | return imsm_orom_has_raid1e(orom); | |
1c556e92 DW |
2892 | return imsm_orom_has_raid1(orom) && raiddisks == 2; |
2893 | case 10: return imsm_orom_has_raid10(orom) && raiddisks == 4; | |
2894 | case 5: return imsm_orom_has_raid5(orom) && raiddisks > 2; | |
88c32bb1 DW |
2895 | } |
2896 | else | |
2897 | return 1; /* not on an Intel RAID platform so anything goes */ | |
2898 | ||
2899 | return 0; | |
2900 | } | |
2901 | ||
35f81cbb | 2902 | #define pr_vrb(fmt, arg...) (void) (verbose && fprintf(stderr, Name fmt, ##arg)) |
c2c087e6 DW |
2903 | /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd |
2904 | * FIX ME add ahci details | |
2905 | */ | |
8b353278 DW |
2906 | static int validate_geometry_imsm_volume(struct supertype *st, int level, |
2907 | int layout, int raiddisks, int chunk, | |
c2c087e6 | 2908 | unsigned long long size, char *dev, |
2c514b71 NB |
2909 | unsigned long long *freesize, |
2910 | int verbose) | |
cdddbdbc | 2911 | { |
c2c087e6 DW |
2912 | struct stat stb; |
2913 | struct intel_super *super = st->sb; | |
a20d2ba5 | 2914 | struct imsm_super *mpb = super->anchor; |
c2c087e6 DW |
2915 | struct dl *dl; |
2916 | unsigned long long pos = 0; | |
2917 | unsigned long long maxsize; | |
2918 | struct extent *e; | |
2919 | int i; | |
cdddbdbc | 2920 | |
88c32bb1 DW |
2921 | /* We must have the container info already read in. */ |
2922 | if (!super) | |
c2c087e6 DW |
2923 | return 0; |
2924 | ||
88c32bb1 | 2925 | if (!is_raid_level_supported(super->orom, level, raiddisks)) { |
1c556e92 DW |
2926 | pr_vrb(": platform does not support raid%d with %d disk%s\n", |
2927 | level, raiddisks, raiddisks > 1 ? "s" : ""); | |
c2c087e6 DW |
2928 | return 0; |
2929 | } | |
78757ce8 DW |
2930 | if (super->orom && level != 1 && |
2931 | !imsm_orom_has_chunk(super->orom, chunk)) { | |
35f81cbb | 2932 | pr_vrb(": platform does not support a chunk size of: %d\n", chunk); |
c2c087e6 | 2933 | return 0; |
88c32bb1 DW |
2934 | } |
2935 | if (layout != imsm_level_to_layout(level)) { | |
2936 | if (level == 5) | |
35f81cbb | 2937 | pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n"); |
88c32bb1 | 2938 | else if (level == 10) |
35f81cbb | 2939 | pr_vrb(": imsm raid 10 only supports the n2 layout\n"); |
88c32bb1 | 2940 | else |
35f81cbb | 2941 | pr_vrb(": imsm unknown layout %#x for this raid level %d\n", |
88c32bb1 | 2942 | layout, level); |
c2c087e6 | 2943 | return 0; |
88c32bb1 | 2944 | } |
c2c087e6 DW |
2945 | |
2946 | if (!dev) { | |
2947 | /* General test: make sure there is space for | |
2da8544a DW |
2948 | * 'raiddisks' device extents of size 'size' at a given |
2949 | * offset | |
c2c087e6 | 2950 | */ |
e46273eb | 2951 | unsigned long long minsize = size; |
2da8544a | 2952 | unsigned long long start_offset = ~0ULL; |
c2c087e6 DW |
2953 | int dcnt = 0; |
2954 | if (minsize == 0) | |
2955 | minsize = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS; | |
2956 | for (dl = super->disks; dl ; dl = dl->next) { | |
2957 | int found = 0; | |
2958 | ||
bf5a934a | 2959 | pos = 0; |
c2c087e6 DW |
2960 | i = 0; |
2961 | e = get_extents(super, dl); | |
2962 | if (!e) continue; | |
2963 | do { | |
2964 | unsigned long long esize; | |
2965 | esize = e[i].start - pos; | |
2966 | if (esize >= minsize) | |
2967 | found = 1; | |
2da8544a DW |
2968 | if (found && start_offset == ~0ULL) { |
2969 | start_offset = pos; | |
2970 | break; | |
2971 | } else if (found && pos != start_offset) { | |
2972 | found = 0; | |
2973 | break; | |
2974 | } | |
c2c087e6 DW |
2975 | pos = e[i].start + e[i].size; |
2976 | i++; | |
2977 | } while (e[i-1].size); | |
2978 | if (found) | |
2979 | dcnt++; | |
2980 | free(e); | |
2981 | } | |
2982 | if (dcnt < raiddisks) { | |
2c514b71 NB |
2983 | if (verbose) |
2984 | fprintf(stderr, Name ": imsm: Not enough " | |
2985 | "devices with space for this array " | |
2986 | "(%d < %d)\n", | |
2987 | dcnt, raiddisks); | |
c2c087e6 DW |
2988 | return 0; |
2989 | } | |
2990 | return 1; | |
2991 | } | |
0dcecb2e | 2992 | |
c2c087e6 DW |
2993 | /* This device must be a member of the set */ |
2994 | if (stat(dev, &stb) < 0) | |
2995 | return 0; | |
2996 | if ((S_IFMT & stb.st_mode) != S_IFBLK) | |
2997 | return 0; | |
2998 | for (dl = super->disks ; dl ; dl = dl->next) { | |
2999 | if (dl->major == major(stb.st_rdev) && | |
3000 | dl->minor == minor(stb.st_rdev)) | |
3001 | break; | |
3002 | } | |
3003 | if (!dl) { | |
2c514b71 NB |
3004 | if (verbose) |
3005 | fprintf(stderr, Name ": %s is not in the " | |
3006 | "same imsm set\n", dev); | |
c2c087e6 | 3007 | return 0; |
a20d2ba5 DW |
3008 | } else if (super->orom && dl->index < 0 && mpb->num_raid_devs) { |
3009 | /* If a volume is present then the current creation attempt | |
3010 | * cannot incorporate new spares because the orom may not | |
3011 | * understand this configuration (all member disks must be | |
3012 | * members of each array in the container). | |
3013 | */ | |
3014 | fprintf(stderr, Name ": %s is a spare and a volume" | |
3015 | " is already defined for this container\n", dev); | |
3016 | fprintf(stderr, Name ": The option-rom requires all member" | |
3017 | " disks to be a member of all volumes\n"); | |
3018 | return 0; | |
c2c087e6 | 3019 | } |
0dcecb2e DW |
3020 | |
3021 | /* retrieve the largest free space block */ | |
c2c087e6 DW |
3022 | e = get_extents(super, dl); |
3023 | maxsize = 0; | |
3024 | i = 0; | |
0dcecb2e DW |
3025 | if (e) { |
3026 | do { | |
3027 | unsigned long long esize; | |
3028 | ||
3029 | esize = e[i].start - pos; | |
3030 | if (esize >= maxsize) | |
3031 | maxsize = esize; | |
3032 | pos = e[i].start + e[i].size; | |
3033 | i++; | |
3034 | } while (e[i-1].size); | |
3035 | dl->e = e; | |
3036 | dl->extent_cnt = i; | |
3037 | } else { | |
3038 | if (verbose) | |
3039 | fprintf(stderr, Name ": unable to determine free space for: %s\n", | |
3040 | dev); | |
3041 | return 0; | |
3042 | } | |
3043 | if (maxsize < size) { | |
3044 | if (verbose) | |
3045 | fprintf(stderr, Name ": %s not enough space (%llu < %llu)\n", | |
3046 | dev, maxsize, size); | |
3047 | return 0; | |
3048 | } | |
3049 | ||
3050 | /* count total number of extents for merge */ | |
3051 | i = 0; | |
3052 | for (dl = super->disks; dl; dl = dl->next) | |
3053 | if (dl->e) | |
3054 | i += dl->extent_cnt; | |
3055 | ||
3056 | maxsize = merge_extents(super, i); | |
3057 | if (maxsize < size) { | |
3058 | if (verbose) | |
3059 | fprintf(stderr, Name ": not enough space after merge (%llu < %llu)\n", | |
3060 | maxsize, size); | |
3061 | return 0; | |
3062 | } else if (maxsize == ~0ULL) { | |
3063 | if (verbose) | |
3064 | fprintf(stderr, Name ": failed to merge %d extents\n", i); | |
3065 | return 0; | |
3066 | } | |
3067 | ||
c2c087e6 DW |
3068 | *freesize = maxsize; |
3069 | ||
3070 | return 1; | |
cdddbdbc DW |
3071 | } |
3072 | ||
efb30e7f DW |
3073 | static int reserve_space(struct supertype *st, int raiddisks, |
3074 | unsigned long long size, int chunk, | |
3075 | unsigned long long *freesize) | |
3076 | { | |
3077 | struct intel_super *super = st->sb; | |
3078 | struct imsm_super *mpb = super->anchor; | |
3079 | struct dl *dl; | |
3080 | int i; | |
3081 | int extent_cnt; | |
3082 | struct extent *e; | |
3083 | unsigned long long maxsize; | |
3084 | unsigned long long minsize; | |
3085 | int cnt; | |
3086 | int used; | |
3087 | ||
3088 | /* find the largest common start free region of the possible disks */ | |
3089 | used = 0; | |
3090 | extent_cnt = 0; | |
3091 | cnt = 0; | |
3092 | for (dl = super->disks; dl; dl = dl->next) { | |
3093 | dl->raiddisk = -1; | |
3094 | ||
3095 | if (dl->index >= 0) | |
3096 | used++; | |
3097 | ||
3098 | /* don't activate new spares if we are orom constrained | |
3099 | * and there is already a volume active in the container | |
3100 | */ | |
3101 | if (super->orom && dl->index < 0 && mpb->num_raid_devs) | |
3102 | continue; | |
3103 | ||
3104 | e = get_extents(super, dl); | |
3105 | if (!e) | |
3106 | continue; | |
3107 | for (i = 1; e[i-1].size; i++) | |
3108 | ; | |
3109 | dl->e = e; | |
3110 | dl->extent_cnt = i; | |
3111 | extent_cnt += i; | |
3112 | cnt++; | |
3113 | } | |
3114 | ||
3115 | maxsize = merge_extents(super, extent_cnt); | |
3116 | minsize = size; | |
3117 | if (size == 0) | |
3118 | minsize = chunk; | |
3119 | ||
3120 | if (cnt < raiddisks || | |
3121 | (super->orom && used && used != raiddisks) || | |
3122 | maxsize < minsize) { | |
3123 | fprintf(stderr, Name ": not enough devices with space to create array.\n"); | |
3124 | return 0; /* No enough free spaces large enough */ | |
3125 | } | |
3126 | ||
3127 | if (size == 0) { | |
3128 | size = maxsize; | |
3129 | if (chunk) { | |
3130 | size /= chunk; | |
3131 | size *= chunk; | |
3132 | } | |
3133 | } | |
3134 | ||
3135 | cnt = 0; | |
3136 | for (dl = super->disks; dl; dl = dl->next) | |
3137 | if (dl->e) | |
3138 | dl->raiddisk = cnt++; | |
3139 | ||
3140 | *freesize = size; | |
3141 | ||
3142 | return 1; | |
3143 | } | |
3144 | ||
bf5a934a DW |
3145 | static int validate_geometry_imsm(struct supertype *st, int level, int layout, |
3146 | int raiddisks, int chunk, unsigned long long size, | |
3147 | char *dev, unsigned long long *freesize, | |
3148 | int verbose) | |
3149 | { | |
3150 | int fd, cfd; | |
3151 | struct mdinfo *sra; | |
3152 | ||
3153 | /* if given unused devices create a container | |
3154 | * if given given devices in a container create a member volume | |
3155 | */ | |
3156 | if (level == LEVEL_CONTAINER) { | |
3157 | /* Must be a fresh device to add to a container */ | |
3158 | return validate_geometry_imsm_container(st, level, layout, | |
3159 | raiddisks, chunk, size, | |
3160 | dev, freesize, | |
3161 | verbose); | |
3162 | } | |
3163 | ||
8592f29d N |
3164 | if (!dev) { |
3165 | if (st->sb && freesize) { | |
efb30e7f DW |
3166 | /* we are being asked to automatically layout a |
3167 | * new volume based on the current contents of | |
3168 | * the container. If the the parameters can be | |
3169 | * satisfied reserve_space will record the disks, | |
3170 | * start offset, and size of the volume to be | |
3171 | * created. add_to_super and getinfo_super | |
3172 | * detect when autolayout is in progress. | |
3173 | */ | |
3174 | return reserve_space(st, raiddisks, size, chunk, freesize); | |
8592f29d N |
3175 | } |
3176 | return 1; | |
3177 | } | |
bf5a934a DW |
3178 | if (st->sb) { |
3179 | /* creating in a given container */ | |
3180 | return validate_geometry_imsm_volume(st, level, layout, | |
3181 | raiddisks, chunk, size, | |
3182 | dev, freesize, verbose); | |
3183 | } | |
3184 | ||
3185 | /* limit creation to the following levels */ | |
3186 | if (!dev) | |
3187 | switch (level) { | |
3188 | case 0: | |
3189 | case 1: | |
3190 | case 10: | |
3191 | case 5: | |
3192 | break; | |
3193 | default: | |
3194 | return 1; | |
3195 | } | |
3196 | ||
3197 | /* This device needs to be a device in an 'imsm' container */ | |
3198 | fd = open(dev, O_RDONLY|O_EXCL, 0); | |
3199 | if (fd >= 0) { | |
3200 | if (verbose) | |
3201 | fprintf(stderr, | |
3202 | Name ": Cannot create this array on device %s\n", | |
3203 | dev); | |
3204 | close(fd); | |
3205 | return 0; | |
3206 | } | |
3207 | if (errno != EBUSY || (fd = open(dev, O_RDONLY, 0)) < 0) { | |
3208 | if (verbose) | |
3209 | fprintf(stderr, Name ": Cannot open %s: %s\n", | |
3210 | dev, strerror(errno)); | |
3211 | return 0; | |
3212 | } | |
3213 | /* Well, it is in use by someone, maybe an 'imsm' container. */ | |
3214 | cfd = open_container(fd); | |
3215 | if (cfd < 0) { | |
3216 | close(fd); | |
3217 | if (verbose) | |
3218 | fprintf(stderr, Name ": Cannot use %s: It is busy\n", | |
3219 | dev); | |
3220 | return 0; | |
3221 | } | |
3222 | sra = sysfs_read(cfd, 0, GET_VERSION); | |
3223 | close(fd); | |
3224 | if (sra && sra->array.major_version == -1 && | |
3225 | strcmp(sra->text_version, "imsm") == 0) { | |
3226 | /* This is a member of a imsm container. Load the container | |
3227 | * and try to create a volume | |
3228 | */ | |
3229 | struct intel_super *super; | |
3230 | ||
3231 | if (load_super_imsm_all(st, cfd, (void **) &super, NULL, 1) == 0) { | |
3232 | st->sb = super; | |
3233 | st->container_dev = fd2devnum(cfd); | |
3234 | close(cfd); | |
3235 | return validate_geometry_imsm_volume(st, level, layout, | |
3236 | raiddisks, chunk, | |
3237 | size, dev, | |
3238 | freesize, verbose); | |
3239 | } | |
3240 | close(cfd); | |
3241 | } else /* may belong to another container */ | |
3242 | return 0; | |
3243 | ||
3244 | return 1; | |
3245 | } | |
0e600426 | 3246 | #endif /* MDASSEMBLE */ |
bf5a934a | 3247 | |
cdddbdbc DW |
3248 | static struct mdinfo *container_content_imsm(struct supertype *st) |
3249 | { | |
4f5bc454 DW |
3250 | /* Given a container loaded by load_super_imsm_all, |
3251 | * extract information about all the arrays into | |
3252 | * an mdinfo tree. | |
3253 | * | |
3254 | * For each imsm_dev create an mdinfo, fill it in, | |
3255 | * then look for matching devices in super->disks | |
3256 | * and create appropriate device mdinfo. | |
3257 | */ | |
3258 | struct intel_super *super = st->sb; | |
949c47a0 | 3259 | struct imsm_super *mpb = super->anchor; |
4f5bc454 DW |
3260 | struct mdinfo *rest = NULL; |
3261 | int i; | |
cdddbdbc | 3262 | |
604b746f JD |
3263 | /* do not assemble arrays that might have bad blocks */ |
3264 | if (imsm_bbm_log_size(super->anchor)) { | |
3265 | fprintf(stderr, Name ": BBM log found in metadata. " | |
3266 | "Cannot activate array(s).\n"); | |
3267 | return NULL; | |
3268 | } | |
3269 | ||
4f5bc454 | 3270 | for (i = 0; i < mpb->num_raid_devs; i++) { |
949c47a0 | 3271 | struct imsm_dev *dev = get_imsm_dev(super, i); |
a965f303 | 3272 | struct imsm_map *map = get_imsm_map(dev, 0); |
4f5bc454 | 3273 | struct mdinfo *this; |
4f5bc454 DW |
3274 | int slot; |
3275 | ||
3276 | this = malloc(sizeof(*this)); | |
3277 | memset(this, 0, sizeof(*this)); | |
3278 | this->next = rest; | |
4f5bc454 | 3279 | |
301406c9 DW |
3280 | super->current_vol = i; |
3281 | getinfo_super_imsm_volume(st, this); | |
4f5bc454 | 3282 | for (slot = 0 ; slot < map->num_members; slot++) { |
4f5bc454 DW |
3283 | struct mdinfo *info_d; |
3284 | struct dl *d; | |
3285 | int idx; | |
9a1608e5 | 3286 | int skip; |
4f5bc454 | 3287 | __u32 s; |
7eef0453 | 3288 | __u32 ord; |
4f5bc454 | 3289 | |
9a1608e5 | 3290 | skip = 0; |
ff077194 | 3291 | idx = get_imsm_disk_idx(dev, slot); |
7eef0453 | 3292 | ord = get_imsm_ord_tbl_ent(dev, slot); |
4f5bc454 DW |
3293 | for (d = super->disks; d ; d = d->next) |
3294 | if (d->index == idx) | |
3295 | break; | |
3296 | ||
3297 | if (d == NULL) | |
9a1608e5 DW |
3298 | skip = 1; |
3299 | ||
f2f27e63 | 3300 | s = d ? d->disk.status : 0; |
9a1608e5 DW |
3301 | if (s & FAILED_DISK) |
3302 | skip = 1; | |
3303 | if (!(s & USABLE_DISK)) | |
3304 | skip = 1; | |
7eef0453 DW |
3305 | if (ord & IMSM_ORD_REBUILD) |
3306 | skip = 1; | |
9a1608e5 DW |
3307 | |
3308 | /* | |
3309 | * if we skip some disks the array will be assmebled degraded; | |
3310 | * reset resync start to avoid a dirty-degraded situation | |
3311 | * | |
3312 | * FIXME handle dirty degraded | |
3313 | */ | |
3314 | if (skip && !dev->vol.dirty) | |
3315 | this->resync_start = ~0ULL; | |
3316 | if (skip) | |
3317 | continue; | |
4f5bc454 DW |
3318 | |
3319 | info_d = malloc(sizeof(*info_d)); | |
9a1608e5 DW |
3320 | if (!info_d) { |
3321 | fprintf(stderr, Name ": failed to allocate disk" | |
3322 | " for volume %s\n", (char *) dev->volume); | |
3323 | free(this); | |
3324 | this = rest; | |
3325 | break; | |
3326 | } | |
4f5bc454 DW |
3327 | memset(info_d, 0, sizeof(*info_d)); |
3328 | info_d->next = this->devs; | |
3329 | this->devs = info_d; | |
3330 | ||
4f5bc454 DW |
3331 | info_d->disk.number = d->index; |
3332 | info_d->disk.major = d->major; | |
3333 | info_d->disk.minor = d->minor; | |
3334 | info_d->disk.raid_disk = slot; | |
4f5bc454 DW |
3335 | |
3336 | this->array.working_disks++; | |
3337 | ||
3338 | info_d->events = __le32_to_cpu(mpb->generation_num); | |
3339 | info_d->data_offset = __le32_to_cpu(map->pba_of_lba0); | |
3340 | info_d->component_size = __le32_to_cpu(map->blocks_per_member); | |
3341 | if (d->devname) | |
3342 | strcpy(info_d->name, d->devname); | |
3343 | } | |
9a1608e5 | 3344 | rest = this; |
4f5bc454 DW |
3345 | } |
3346 | ||
3347 | return rest; | |
cdddbdbc DW |
3348 | } |
3349 | ||
845dea95 | 3350 | |
0e600426 | 3351 | #ifndef MDASSEMBLE |
cba0191b NB |
3352 | static int imsm_open_new(struct supertype *c, struct active_array *a, |
3353 | char *inst) | |
845dea95 | 3354 | { |
0372d5a2 | 3355 | struct intel_super *super = c->sb; |
949c47a0 | 3356 | struct imsm_super *mpb = super->anchor; |
0372d5a2 | 3357 | |
949c47a0 | 3358 | if (atoi(inst) >= mpb->num_raid_devs) { |
0372d5a2 DW |
3359 | fprintf(stderr, "%s: subarry index %d, out of range\n", |
3360 | __func__, atoi(inst)); | |
3361 | return -ENODEV; | |
3362 | } | |
3363 | ||
4e6e574a | 3364 | dprintf("imsm: open_new %s\n", inst); |
cba0191b | 3365 | a->info.container_member = atoi(inst); |
845dea95 NB |
3366 | return 0; |
3367 | } | |
3368 | ||
fb49eef2 | 3369 | static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev, int failed) |
c2a1e7da | 3370 | { |
a965f303 | 3371 | struct imsm_map *map = get_imsm_map(dev, 0); |
c2a1e7da DW |
3372 | |
3373 | if (!failed) | |
3393c6af DW |
3374 | return map->map_state == IMSM_T_STATE_UNINITIALIZED ? |
3375 | IMSM_T_STATE_UNINITIALIZED : IMSM_T_STATE_NORMAL; | |
c2a1e7da DW |
3376 | |
3377 | switch (get_imsm_raid_level(map)) { | |
3378 | case 0: | |
3379 | return IMSM_T_STATE_FAILED; | |
3380 | break; | |
3381 | case 1: | |
3382 | if (failed < map->num_members) | |
3383 | return IMSM_T_STATE_DEGRADED; | |
3384 | else | |
3385 | return IMSM_T_STATE_FAILED; | |
3386 | break; | |
3387 | case 10: | |
3388 | { | |
3389 | /** | |
c92a2527 DW |
3390 | * check to see if any mirrors have failed, otherwise we |
3391 | * are degraded. Even numbered slots are mirrored on | |
3392 | * slot+1 | |
c2a1e7da | 3393 | */ |
c2a1e7da | 3394 | int i; |
d9b420a5 N |
3395 | /* gcc -Os complains that this is unused */ |
3396 | int insync = insync; | |
c2a1e7da DW |
3397 | |
3398 | for (i = 0; i < map->num_members; i++) { | |
c92a2527 DW |
3399 | __u32 ord = get_imsm_ord_tbl_ent(dev, i); |
3400 | int idx = ord_to_idx(ord); | |
3401 | struct imsm_disk *disk; | |
c2a1e7da | 3402 | |
c92a2527 DW |
3403 | /* reset the potential in-sync count on even-numbered |
3404 | * slots. num_copies is always 2 for imsm raid10 | |
3405 | */ | |
3406 | if ((i & 1) == 0) | |
3407 | insync = 2; | |
c2a1e7da | 3408 | |
c92a2527 | 3409 | disk = get_imsm_disk(super, idx); |
f2f27e63 | 3410 | if (!disk || disk->status & FAILED_DISK || |
c92a2527 DW |
3411 | ord & IMSM_ORD_REBUILD) |
3412 | insync--; | |
c2a1e7da | 3413 | |
c92a2527 DW |
3414 | /* no in-sync disks left in this mirror the |
3415 | * array has failed | |
3416 | */ | |
3417 | if (insync == 0) | |
3418 | return IMSM_T_STATE_FAILED; | |
c2a1e7da DW |
3419 | } |
3420 | ||
3421 | return IMSM_T_STATE_DEGRADED; | |
3422 | } | |
3423 | case 5: | |
3424 | if (failed < 2) | |
3425 | return IMSM_T_STATE_DEGRADED; | |
3426 | else | |
3427 | return IMSM_T_STATE_FAILED; | |
3428 | break; | |
3429 | default: | |
3430 | break; | |
3431 | } | |
3432 | ||
3433 | return map->map_state; | |
3434 | } | |
3435 | ||
ff077194 | 3436 | static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev) |
c2a1e7da DW |
3437 | { |
3438 | int i; | |
3439 | int failed = 0; | |
3440 | struct imsm_disk *disk; | |
ff077194 | 3441 | struct imsm_map *map = get_imsm_map(dev, 0); |
0556e1a2 DW |
3442 | struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state); |
3443 | __u32 ord; | |
3444 | int idx; | |
c2a1e7da | 3445 | |
0556e1a2 DW |
3446 | /* at the beginning of migration we set IMSM_ORD_REBUILD on |
3447 | * disks that are being rebuilt. New failures are recorded to | |
3448 | * map[0]. So we look through all the disks we started with and | |
3449 | * see if any failures are still present, or if any new ones | |
3450 | * have arrived | |
3451 | * | |
3452 | * FIXME add support for online capacity expansion and | |
3453 | * raid-level-migration | |
3454 | */ | |
3455 | for (i = 0; i < prev->num_members; i++) { | |
3456 | ord = __le32_to_cpu(prev->disk_ord_tbl[i]); | |
3457 | ord |= __le32_to_cpu(map->disk_ord_tbl[i]); | |
3458 | idx = ord_to_idx(ord); | |
c2a1e7da | 3459 | |
949c47a0 | 3460 | disk = get_imsm_disk(super, idx); |
f2f27e63 | 3461 | if (!disk || disk->status & FAILED_DISK || |
b10b37b8 | 3462 | ord & IMSM_ORD_REBUILD) |
fcb84475 | 3463 | failed++; |
c2a1e7da DW |
3464 | } |
3465 | ||
3466 | return failed; | |
845dea95 NB |
3467 | } |
3468 | ||
0c046afd DW |
3469 | static int is_resyncing(struct imsm_dev *dev) |
3470 | { | |
3471 | struct imsm_map *migr_map; | |
3472 | ||
3473 | if (!dev->vol.migr_state) | |
3474 | return 0; | |
3475 | ||
e3bba0e0 | 3476 | if (dev->vol.migr_type == MIGR_INIT) |
0c046afd DW |
3477 | return 1; |
3478 | ||
3479 | migr_map = get_imsm_map(dev, 1); | |
3480 | ||
3481 | if (migr_map->map_state == IMSM_T_STATE_NORMAL) | |
3482 | return 1; | |
3483 | else | |
3484 | return 0; | |
3485 | } | |
3486 | ||
3487 | static int is_rebuilding(struct imsm_dev *dev) | |
3488 | { | |
3489 | struct imsm_map *migr_map; | |
3490 | ||
3491 | if (!dev->vol.migr_state) | |
3492 | return 0; | |
3493 | ||
e3bba0e0 | 3494 | if (dev->vol.migr_type != MIGR_REBUILD) |
0c046afd DW |
3495 | return 0; |
3496 | ||
3497 | migr_map = get_imsm_map(dev, 1); | |
3498 | ||
3499 | if (migr_map->map_state == IMSM_T_STATE_DEGRADED) | |
3500 | return 1; | |
3501 | else | |
3502 | return 0; | |
3503 | } | |
3504 | ||
0556e1a2 DW |
3505 | /* return true if we recorded new information */ |
3506 | static int mark_failure(struct imsm_dev *dev, struct imsm_disk *disk, int idx) | |
47ee5a45 | 3507 | { |
0556e1a2 DW |
3508 | __u32 ord; |
3509 | int slot; | |
3510 | struct imsm_map *map; | |
3511 | ||
3512 | /* new failures are always set in map[0] */ | |
3513 | map = get_imsm_map(dev, 0); | |
3514 | ||
3515 | slot = get_imsm_disk_slot(map, idx); | |
3516 | if (slot < 0) | |
3517 | return 0; | |
3518 | ||
3519 | ord = __le32_to_cpu(map->disk_ord_tbl[slot]); | |
3520 | if ((disk->status & FAILED_DISK) && (ord & IMSM_ORD_REBUILD)) | |
3521 | return 0; | |
3522 | ||
f2f27e63 | 3523 | disk->status |= FAILED_DISK; |
0556e1a2 DW |
3524 | set_imsm_ord_tbl_ent(map, slot, idx | IMSM_ORD_REBUILD); |
3525 | if (map->failed_disk_num == ~0) | |
3526 | map->failed_disk_num = slot; | |
3527 | return 1; | |
3528 | } | |
3529 | ||
3530 | static void mark_missing(struct imsm_dev *dev, struct imsm_disk *disk, int idx) | |
3531 | { | |
3532 | mark_failure(dev, disk, idx); | |
3533 | ||
3534 | if (disk->scsi_id == __cpu_to_le32(~(__u32)0)) | |
3535 | return; | |
3536 | ||
47ee5a45 DW |
3537 | disk->scsi_id = __cpu_to_le32(~(__u32)0); |
3538 | memmove(&disk->serial[0], &disk->serial[1], MAX_RAID_SERIAL_LEN - 1); | |
3539 | } | |
3540 | ||
0c046afd DW |
3541 | /* Handle dirty -> clean transititions and resync. Degraded and rebuild |
3542 | * states are handled in imsm_set_disk() with one exception, when a | |
3543 | * resync is stopped due to a new failure this routine will set the | |
3544 | * 'degraded' state for the array. | |
3545 | */ | |
01f157d7 | 3546 | static int imsm_set_array_state(struct active_array *a, int consistent) |
a862209d DW |
3547 | { |
3548 | int inst = a->info.container_member; | |
3549 | struct intel_super *super = a->container->sb; | |
949c47a0 | 3550 | struct imsm_dev *dev = get_imsm_dev(super, inst); |
a965f303 | 3551 | struct imsm_map *map = get_imsm_map(dev, 0); |
0c046afd DW |
3552 | int failed = imsm_count_failed(super, dev); |
3553 | __u8 map_state = imsm_check_degraded(super, dev, failed); | |
a862209d | 3554 | |
47ee5a45 DW |
3555 | /* before we activate this array handle any missing disks */ |
3556 | if (consistent == 2 && super->missing) { | |
3557 | struct dl *dl; | |
3558 | ||
3559 | dprintf("imsm: mark missing\n"); | |
3560 | end_migration(dev, map_state); | |
3561 | for (dl = super->missing; dl; dl = dl->next) | |
0556e1a2 | 3562 | mark_missing(dev, &dl->disk, dl->index); |
47ee5a45 DW |
3563 | super->updates_pending++; |
3564 | } | |
3565 | ||
0c046afd | 3566 | if (consistent == 2 && |
593add1b | 3567 | (!is_resync_complete(a) || |
0c046afd DW |
3568 | map_state != IMSM_T_STATE_NORMAL || |
3569 | dev->vol.migr_state)) | |
01f157d7 | 3570 | consistent = 0; |
272906ef | 3571 | |
593add1b | 3572 | if (is_resync_complete(a)) { |
0c046afd | 3573 | /* complete intialization / resync, |
0556e1a2 DW |
3574 | * recovery and interrupted recovery is completed in |
3575 | * ->set_disk | |
0c046afd DW |
3576 | */ |
3577 | if (is_resyncing(dev)) { | |
3578 | dprintf("imsm: mark resync done\n"); | |
f8f603f1 | 3579 | end_migration(dev, map_state); |
115c3803 | 3580 | super->updates_pending++; |
115c3803 | 3581 | } |
0c046afd DW |
3582 | } else if (!is_resyncing(dev) && !failed) { |
3583 | /* mark the start of the init process if nothing is failed */ | |
3584 | dprintf("imsm: mark resync start (%llu)\n", a->resync_start); | |
e3bba0e0 DW |
3585 | if (map->map_state == IMSM_T_STATE_NORMAL) |
3586 | migrate(dev, IMSM_T_STATE_NORMAL, MIGR_REBUILD); | |
3587 | else | |
3588 | migrate(dev, IMSM_T_STATE_NORMAL, MIGR_INIT); | |
3393c6af | 3589 | super->updates_pending++; |
115c3803 | 3590 | } |
a862209d | 3591 | |
f8f603f1 DW |
3592 | /* check if we can update the migration checkpoint */ |
3593 | if (dev->vol.migr_state && | |
3594 | __le32_to_cpu(dev->vol.curr_migr_unit) != a->resync_start) { | |
3595 | dprintf("imsm: checkpoint migration (%llu)\n", a->resync_start); | |
3596 | dev->vol.curr_migr_unit = __cpu_to_le32(a->resync_start); | |
3597 | super->updates_pending++; | |
3598 | } | |
3599 | ||
3393c6af | 3600 | /* mark dirty / clean */ |
0c046afd | 3601 | if (dev->vol.dirty != !consistent) { |
3393c6af | 3602 | dprintf("imsm: mark '%s' (%llu)\n", |
0c046afd DW |
3603 | consistent ? "clean" : "dirty", a->resync_start); |
3604 | if (consistent) | |
3605 | dev->vol.dirty = 0; | |
3606 | else | |
3607 | dev->vol.dirty = 1; | |
a862209d DW |
3608 | super->updates_pending++; |
3609 | } | |
01f157d7 | 3610 | return consistent; |
a862209d DW |
3611 | } |
3612 | ||
8d45d196 | 3613 | static void imsm_set_disk(struct active_array *a, int n, int state) |
845dea95 | 3614 | { |
8d45d196 DW |
3615 | int inst = a->info.container_member; |
3616 | struct intel_super *super = a->container->sb; | |
949c47a0 | 3617 | struct imsm_dev *dev = get_imsm_dev(super, inst); |
a965f303 | 3618 | struct imsm_map *map = get_imsm_map(dev, 0); |
8d45d196 | 3619 | struct imsm_disk *disk; |
0c046afd | 3620 | int failed; |
b10b37b8 | 3621 | __u32 ord; |
0c046afd | 3622 | __u8 map_state; |
8d45d196 DW |
3623 | |
3624 | if (n > map->num_members) | |
3625 | fprintf(stderr, "imsm: set_disk %d out of range 0..%d\n", | |
3626 | n, map->num_members - 1); | |
3627 | ||
3628 | if (n < 0) | |
3629 | return; | |
3630 | ||
4e6e574a | 3631 | dprintf("imsm: set_disk %d:%x\n", n, state); |
8d45d196 | 3632 | |
b10b37b8 DW |
3633 | ord = get_imsm_ord_tbl_ent(dev, n); |
3634 | disk = get_imsm_disk(super, ord_to_idx(ord)); | |
8d45d196 | 3635 | |
5802a811 | 3636 | /* check for new failures */ |
0556e1a2 DW |
3637 | if (state & DS_FAULTY) { |
3638 | if (mark_failure(dev, disk, ord_to_idx(ord))) | |
3639 | super->updates_pending++; | |
8d45d196 | 3640 | } |
47ee5a45 | 3641 | |
19859edc | 3642 | /* check if in_sync */ |
0556e1a2 | 3643 | if (state & DS_INSYNC && ord & IMSM_ORD_REBUILD && is_rebuilding(dev)) { |
b10b37b8 DW |
3644 | struct imsm_map *migr_map = get_imsm_map(dev, 1); |
3645 | ||
3646 | set_imsm_ord_tbl_ent(migr_map, n, ord_to_idx(ord)); | |
19859edc DW |
3647 | super->updates_pending++; |
3648 | } | |
8d45d196 | 3649 | |
0c046afd DW |
3650 | failed = imsm_count_failed(super, dev); |
3651 | map_state = imsm_check_degraded(super, dev, failed); | |
5802a811 | 3652 | |
0c046afd DW |
3653 | /* check if recovery complete, newly degraded, or failed */ |
3654 | if (map_state == IMSM_T_STATE_NORMAL && is_rebuilding(dev)) { | |
f8f603f1 | 3655 | end_migration(dev, map_state); |
0556e1a2 DW |
3656 | map = get_imsm_map(dev, 0); |
3657 | map->failed_disk_num = ~0; | |
0c046afd DW |
3658 | super->updates_pending++; |
3659 | } else if (map_state == IMSM_T_STATE_DEGRADED && | |
3660 | map->map_state != map_state && | |
3661 | !dev->vol.migr_state) { | |
3662 | dprintf("imsm: mark degraded\n"); | |
3663 | map->map_state = map_state; | |
3664 | super->updates_pending++; | |
3665 | } else if (map_state == IMSM_T_STATE_FAILED && | |
3666 | map->map_state != map_state) { | |
3667 | dprintf("imsm: mark failed\n"); | |
f8f603f1 | 3668 | end_migration(dev, map_state); |
0c046afd | 3669 | super->updates_pending++; |
5802a811 | 3670 | } |
845dea95 NB |
3671 | } |
3672 | ||
c2a1e7da DW |
3673 | static int store_imsm_mpb(int fd, struct intel_super *super) |
3674 | { | |
949c47a0 | 3675 | struct imsm_super *mpb = super->anchor; |
c2a1e7da DW |
3676 | __u32 mpb_size = __le32_to_cpu(mpb->mpb_size); |
3677 | unsigned long long dsize; | |
3678 | unsigned long long sectors; | |
3679 | ||
3680 | get_dev_size(fd, NULL, &dsize); | |
3681 | ||
272f648f DW |
3682 | if (mpb_size > 512) { |
3683 | /* -1 to account for anchor */ | |
3684 | sectors = mpb_sectors(mpb) - 1; | |
c2a1e7da | 3685 | |
272f648f DW |
3686 | /* write the extended mpb to the sectors preceeding the anchor */ |
3687 | if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0) | |
3688 | return 1; | |
c2a1e7da | 3689 | |
99e29264 | 3690 | if (write(fd, super->buf + 512, 512 * sectors) != 512 * sectors) |
272f648f DW |
3691 | return 1; |
3692 | } | |
c2a1e7da | 3693 | |
272f648f DW |
3694 | /* first block is stored on second to last sector of the disk */ |
3695 | if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0) | |
c2a1e7da DW |
3696 | return 1; |
3697 | ||
272f648f | 3698 | if (write(fd, super->buf, 512) != 512) |
c2a1e7da DW |
3699 | return 1; |
3700 | ||
c2a1e7da DW |
3701 | return 0; |
3702 | } | |
3703 | ||
2e735d19 | 3704 | static void imsm_sync_metadata(struct supertype *container) |
845dea95 | 3705 | { |
2e735d19 | 3706 | struct intel_super *super = container->sb; |
c2a1e7da DW |
3707 | |
3708 | if (!super->updates_pending) | |
3709 | return; | |
3710 | ||
c2c087e6 | 3711 | write_super_imsm(super, 0); |
c2a1e7da DW |
3712 | |
3713 | super->updates_pending = 0; | |
845dea95 NB |
3714 | } |
3715 | ||
272906ef DW |
3716 | static struct dl *imsm_readd(struct intel_super *super, int idx, struct active_array *a) |
3717 | { | |
3718 | struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member); | |
ff077194 | 3719 | int i = get_imsm_disk_idx(dev, idx); |
272906ef DW |
3720 | struct dl *dl; |
3721 | ||
3722 | for (dl = super->disks; dl; dl = dl->next) | |
3723 | if (dl->index == i) | |
3724 | break; | |
3725 | ||
f2f27e63 | 3726 | if (dl && dl->disk.status & FAILED_DISK) |
272906ef DW |
3727 | dl = NULL; |
3728 | ||
3729 | if (dl) | |
3730 | dprintf("%s: found %x:%x\n", __func__, dl->major, dl->minor); | |
3731 | ||
3732 | return dl; | |
3733 | } | |
3734 | ||
a20d2ba5 DW |
3735 | static struct dl *imsm_add_spare(struct intel_super *super, int slot, |
3736 | struct active_array *a, int activate_new) | |
272906ef DW |
3737 | { |
3738 | struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member); | |
e553d2a4 | 3739 | int idx = get_imsm_disk_idx(dev, slot); |
a20d2ba5 DW |
3740 | struct imsm_super *mpb = super->anchor; |
3741 | struct imsm_map *map; | |
272906ef DW |
3742 | unsigned long long esize; |
3743 | unsigned long long pos; | |
3744 | struct mdinfo *d; | |
3745 | struct extent *ex; | |
a20d2ba5 | 3746 | int i, j; |
272906ef DW |
3747 | int found; |
3748 | __u32 array_start; | |
a20d2ba5 | 3749 | __u32 blocks; |
272906ef DW |
3750 | struct dl *dl; |
3751 | ||
3752 | for (dl = super->disks; dl; dl = dl->next) { | |
3753 | /* If in this array, skip */ | |
3754 | for (d = a->info.devs ; d ; d = d->next) | |
e553d2a4 DW |
3755 | if (d->state_fd >= 0 && |
3756 | d->disk.major == dl->major && | |
272906ef DW |
3757 | d->disk.minor == dl->minor) { |
3758 | dprintf("%x:%x already in array\n", dl->major, dl->minor); | |
3759 | break; | |
3760 | } | |
3761 | if (d) | |
3762 | continue; | |
3763 | ||
e553d2a4 | 3764 | /* skip in use or failed drives */ |
df474657 DW |
3765 | if (dl->disk.status & FAILED_DISK || idx == dl->index || |
3766 | dl->index == -2) { | |
3767 | dprintf("%x:%x status (failed: %d index: %d)\n", | |
3768 | dl->major, dl->minor, | |
3769 | (dl->disk.status & FAILED_DISK) == FAILED_DISK, idx); | |
9a1608e5 DW |
3770 | continue; |
3771 | } | |
3772 | ||
a20d2ba5 DW |
3773 | /* skip pure spares when we are looking for partially |
3774 | * assimilated drives | |
3775 | */ | |
3776 | if (dl->index == -1 && !activate_new) | |
3777 | continue; | |
3778 | ||
272906ef | 3779 | /* Does this unused device have the requisite free space? |
a20d2ba5 | 3780 | * It needs to be able to cover all member volumes |
272906ef DW |
3781 | */ |
3782 | ex = get_extents(super, dl); | |
3783 | if (!ex) { | |
3784 | dprintf("cannot get extents\n"); | |
3785 | continue; | |
3786 | } | |
a20d2ba5 DW |
3787 | for (i = 0; i < mpb->num_raid_devs; i++) { |
3788 | dev = get_imsm_dev(super, i); | |
3789 | map = get_imsm_map(dev, 0); | |
272906ef | 3790 | |
a20d2ba5 DW |
3791 | /* check if this disk is already a member of |
3792 | * this array | |
272906ef | 3793 | */ |
620b1713 | 3794 | if (get_imsm_disk_slot(map, dl->index) >= 0) |
a20d2ba5 DW |
3795 | continue; |
3796 | ||
3797 | found = 0; | |
3798 | j = 0; | |
3799 | pos = 0; | |
3800 | array_start = __le32_to_cpu(map->pba_of_lba0); | |
3801 | blocks = __le32_to_cpu(map->blocks_per_member); | |
3802 | ||
3803 | do { | |
3804 | /* check that we can start at pba_of_lba0 with | |
3805 | * blocks_per_member of space | |
3806 | */ | |
3807 | esize = ex[j].start - pos; | |
3808 | if (array_start >= pos && | |
3809 | array_start + blocks < ex[j].start) { | |
3810 | found = 1; | |
3811 | break; | |
3812 | } | |
3813 | pos = ex[j].start + ex[j].size; | |
3814 | j++; | |
3815 | } while (ex[j-1].size); | |
3816 | ||
3817 | if (!found) | |
272906ef | 3818 | break; |
a20d2ba5 | 3819 | } |
272906ef DW |
3820 | |
3821 | free(ex); | |
a20d2ba5 DW |
3822 | if (i < mpb->num_raid_devs) { |
3823 | dprintf("%x:%x does not have %u at %u\n", | |
272906ef | 3824 | dl->major, dl->minor, |
a20d2ba5 | 3825 | blocks, array_start); |
272906ef DW |
3826 | /* No room */ |
3827 | continue; | |
a20d2ba5 DW |
3828 | } |
3829 | return dl; | |
272906ef DW |
3830 | } |
3831 | ||
3832 | return dl; | |
3833 | } | |
3834 | ||
88758e9d DW |
3835 | static struct mdinfo *imsm_activate_spare(struct active_array *a, |
3836 | struct metadata_update **updates) | |
3837 | { | |
3838 | /** | |
d23fe947 DW |
3839 | * Find a device with unused free space and use it to replace a |
3840 | * failed/vacant region in an array. We replace failed regions one a | |
3841 | * array at a time. The result is that a new spare disk will be added | |
3842 | * to the first failed array and after the monitor has finished | |
3843 | * propagating failures the remainder will be consumed. | |
88758e9d | 3844 | * |
d23fe947 DW |
3845 | * FIXME add a capability for mdmon to request spares from another |
3846 | * container. | |
88758e9d DW |
3847 | */ |
3848 | ||
3849 | struct intel_super *super = a->container->sb; | |
88758e9d | 3850 | int inst = a->info.container_member; |
949c47a0 | 3851 | struct imsm_dev *dev = get_imsm_dev(super, inst); |
a965f303 | 3852 | struct imsm_map *map = get_imsm_map(dev, 0); |
88758e9d DW |
3853 | int failed = a->info.array.raid_disks; |
3854 | struct mdinfo *rv = NULL; | |
3855 | struct mdinfo *d; | |
3856 | struct mdinfo *di; | |
3857 | struct metadata_update *mu; | |
3858 | struct dl *dl; | |
3859 | struct imsm_update_activate_spare *u; | |
3860 | int num_spares = 0; | |
3861 | int i; | |
3862 | ||
3863 | for (d = a->info.devs ; d ; d = d->next) { | |
3864 | if ((d->curr_state & DS_FAULTY) && | |
3865 | d->state_fd >= 0) | |
3866 | /* wait for Removal to happen */ | |
3867 | return NULL; | |
3868 | if (d->state_fd >= 0) | |
3869 | failed--; | |
3870 | } | |
3871 | ||
3872 | dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n", | |
3873 | inst, failed, a->info.array.raid_disks, a->info.array.level); | |
fb49eef2 | 3874 | if (imsm_check_degraded(super, dev, failed) != IMSM_T_STATE_DEGRADED) |
88758e9d DW |
3875 | return NULL; |
3876 | ||
3877 | /* For each slot, if it is not working, find a spare */ | |
88758e9d DW |
3878 | for (i = 0; i < a->info.array.raid_disks; i++) { |
3879 | for (d = a->info.devs ; d ; d = d->next) | |
3880 | if (d->disk.raid_disk == i) | |
3881 | break; | |
3882 | dprintf("found %d: %p %x\n", i, d, d?d->curr_state:0); | |
3883 | if (d && (d->state_fd >= 0)) | |
3884 | continue; | |
3885 | ||
272906ef | 3886 | /* |
a20d2ba5 DW |
3887 | * OK, this device needs recovery. Try to re-add the |
3888 | * previous occupant of this slot, if this fails see if | |
3889 | * we can continue the assimilation of a spare that was | |
3890 | * partially assimilated, finally try to activate a new | |
3891 | * spare. | |
272906ef DW |
3892 | */ |
3893 | dl = imsm_readd(super, i, a); | |
3894 | if (!dl) | |
a20d2ba5 DW |
3895 | dl = imsm_add_spare(super, i, a, 0); |
3896 | if (!dl) | |
3897 | dl = imsm_add_spare(super, i, a, 1); | |
272906ef DW |
3898 | if (!dl) |
3899 | continue; | |
3900 | ||
3901 | /* found a usable disk with enough space */ | |
3902 | di = malloc(sizeof(*di)); | |
79244939 DW |
3903 | if (!di) |
3904 | continue; | |
272906ef DW |
3905 | memset(di, 0, sizeof(*di)); |
3906 | ||
3907 | /* dl->index will be -1 in the case we are activating a | |
3908 | * pristine spare. imsm_process_update() will create a | |
3909 | * new index in this case. Once a disk is found to be | |
3910 | * failed in all member arrays it is kicked from the | |
3911 | * metadata | |
3912 | */ | |
3913 | di->disk.number = dl->index; | |
d23fe947 | 3914 | |
272906ef DW |
3915 | /* (ab)use di->devs to store a pointer to the device |
3916 | * we chose | |
3917 | */ | |
3918 | di->devs = (struct mdinfo *) dl; | |
3919 | ||
3920 | di->disk.raid_disk = i; | |
3921 | di->disk.major = dl->major; | |
3922 | di->disk.minor = dl->minor; | |
3923 | di->disk.state = 0; | |
3924 | di->data_offset = __le32_to_cpu(map->pba_of_lba0); | |
3925 | di->component_size = a->info.component_size; | |
3926 | di->container_member = inst; | |
3927 | di->next = rv; | |
3928 | rv = di; | |
3929 | num_spares++; | |
3930 | dprintf("%x:%x to be %d at %llu\n", dl->major, dl->minor, | |
3931 | i, di->data_offset); | |
88758e9d | 3932 | |
272906ef | 3933 | break; |
88758e9d DW |
3934 | } |
3935 | ||
3936 | if (!rv) | |
3937 | /* No spares found */ | |
3938 | return rv; | |
3939 | /* Now 'rv' has a list of devices to return. | |
3940 | * Create a metadata_update record to update the | |
3941 | * disk_ord_tbl for the array | |
3942 | */ | |
3943 | mu = malloc(sizeof(*mu)); | |
79244939 DW |
3944 | if (mu) { |
3945 | mu->buf = malloc(sizeof(struct imsm_update_activate_spare) * num_spares); | |
3946 | if (mu->buf == NULL) { | |
3947 | free(mu); | |
3948 | mu = NULL; | |
3949 | } | |
3950 | } | |
3951 | if (!mu) { | |
3952 | while (rv) { | |
3953 | struct mdinfo *n = rv->next; | |
3954 | ||
3955 | free(rv); | |
3956 | rv = n; | |
3957 | } | |
3958 | return NULL; | |
3959 | } | |
3960 | ||
88758e9d DW |
3961 | mu->space = NULL; |
3962 | mu->len = sizeof(struct imsm_update_activate_spare) * num_spares; | |
3963 | mu->next = *updates; | |
3964 | u = (struct imsm_update_activate_spare *) mu->buf; | |
3965 | ||
3966 | for (di = rv ; di ; di = di->next) { | |
3967 | u->type = update_activate_spare; | |
d23fe947 DW |
3968 | u->dl = (struct dl *) di->devs; |
3969 | di->devs = NULL; | |
88758e9d DW |
3970 | u->slot = di->disk.raid_disk; |
3971 | u->array = inst; | |
3972 | u->next = u + 1; | |
3973 | u++; | |
3974 | } | |
3975 | (u-1)->next = NULL; | |
3976 | *updates = mu; | |
3977 | ||
3978 | return rv; | |
3979 | } | |
3980 | ||
54c2c1ea | 3981 | static int disks_overlap(struct intel_super *super, int idx, struct imsm_update_create_array *u) |
8273f55e | 3982 | { |
54c2c1ea DW |
3983 | struct imsm_dev *dev = get_imsm_dev(super, idx); |
3984 | struct imsm_map *map = get_imsm_map(dev, 0); | |
3985 | struct imsm_map *new_map = get_imsm_map(&u->dev, 0); | |
3986 | struct disk_info *inf = get_disk_info(u); | |
3987 | struct imsm_disk *disk; | |
8273f55e DW |
3988 | int i; |
3989 | int j; | |
8273f55e | 3990 | |
54c2c1ea DW |
3991 | for (i = 0; i < map->num_members; i++) { |
3992 | disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i)); | |
3993 | for (j = 0; j < new_map->num_members; j++) | |
3994 | if (serialcmp(disk->serial, inf[j].serial) == 0) | |
8273f55e DW |
3995 | return 1; |
3996 | } | |
3997 | ||
3998 | return 0; | |
3999 | } | |
4000 | ||
24565c9a | 4001 | static void imsm_delete(struct intel_super *super, struct dl **dlp, int index); |
ae6aad82 | 4002 | |
e8319a19 DW |
4003 | static void imsm_process_update(struct supertype *st, |
4004 | struct metadata_update *update) | |
4005 | { | |
4006 | /** | |
4007 | * crack open the metadata_update envelope to find the update record | |
4008 | * update can be one of: | |
4009 | * update_activate_spare - a spare device has replaced a failed | |
4010 | * device in an array, update the disk_ord_tbl. If this disk is | |
4011 | * present in all member arrays then also clear the SPARE_DISK | |
4012 | * flag | |
4013 | */ | |
4014 | struct intel_super *super = st->sb; | |
4d7b1503 | 4015 | struct imsm_super *mpb; |
e8319a19 DW |
4016 | enum imsm_update_type type = *(enum imsm_update_type *) update->buf; |
4017 | ||
4d7b1503 DW |
4018 | /* update requires a larger buf but the allocation failed */ |
4019 | if (super->next_len && !super->next_buf) { | |
4020 | super->next_len = 0; | |
4021 | return; | |
4022 | } | |
4023 | ||
4024 | if (super->next_buf) { | |
4025 | memcpy(super->next_buf, super->buf, super->len); | |
4026 | free(super->buf); | |
4027 | super->len = super->next_len; | |
4028 | super->buf = super->next_buf; | |
4029 | ||
4030 | super->next_len = 0; | |
4031 | super->next_buf = NULL; | |
4032 | } | |
4033 | ||
4034 | mpb = super->anchor; | |
4035 | ||
e8319a19 DW |
4036 | switch (type) { |
4037 | case update_activate_spare: { | |
4038 | struct imsm_update_activate_spare *u = (void *) update->buf; | |
949c47a0 | 4039 | struct imsm_dev *dev = get_imsm_dev(super, u->array); |
a965f303 | 4040 | struct imsm_map *map = get_imsm_map(dev, 0); |
0c046afd | 4041 | struct imsm_map *migr_map; |
e8319a19 DW |
4042 | struct active_array *a; |
4043 | struct imsm_disk *disk; | |
0c046afd | 4044 | __u8 to_state; |
e8319a19 | 4045 | struct dl *dl; |
e8319a19 | 4046 | unsigned int found; |
0c046afd DW |
4047 | int failed; |
4048 | int victim = get_imsm_disk_idx(dev, u->slot); | |
e8319a19 DW |
4049 | int i; |
4050 | ||
4051 | for (dl = super->disks; dl; dl = dl->next) | |
d23fe947 | 4052 | if (dl == u->dl) |
e8319a19 DW |
4053 | break; |
4054 | ||
4055 | if (!dl) { | |
4056 | fprintf(stderr, "error: imsm_activate_spare passed " | |
1f24f035 DW |
4057 | "an unknown disk (index: %d)\n", |
4058 | u->dl->index); | |
e8319a19 DW |
4059 | return; |
4060 | } | |
4061 | ||
4062 | super->updates_pending++; | |
4063 | ||
0c046afd DW |
4064 | /* count failures (excluding rebuilds and the victim) |
4065 | * to determine map[0] state | |
4066 | */ | |
4067 | failed = 0; | |
4068 | for (i = 0; i < map->num_members; i++) { | |
4069 | if (i == u->slot) | |
4070 | continue; | |
4071 | disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i)); | |
f2f27e63 | 4072 | if (!disk || disk->status & FAILED_DISK) |
0c046afd DW |
4073 | failed++; |
4074 | } | |
4075 | ||
d23fe947 DW |
4076 | /* adding a pristine spare, assign a new index */ |
4077 | if (dl->index < 0) { | |
4078 | dl->index = super->anchor->num_disks; | |
4079 | super->anchor->num_disks++; | |
4080 | } | |
d23fe947 | 4081 | disk = &dl->disk; |
f2f27e63 DW |
4082 | disk->status |= CONFIGURED_DISK; |
4083 | disk->status &= ~SPARE_DISK; | |
e8319a19 | 4084 | |
0c046afd DW |
4085 | /* mark rebuild */ |
4086 | to_state = imsm_check_degraded(super, dev, failed); | |
4087 | map->map_state = IMSM_T_STATE_DEGRADED; | |
e3bba0e0 | 4088 | migrate(dev, to_state, MIGR_REBUILD); |
0c046afd DW |
4089 | migr_map = get_imsm_map(dev, 1); |
4090 | set_imsm_ord_tbl_ent(map, u->slot, dl->index); | |
4091 | set_imsm_ord_tbl_ent(migr_map, u->slot, dl->index | IMSM_ORD_REBUILD); | |
4092 | ||
e8319a19 DW |
4093 | /* count arrays using the victim in the metadata */ |
4094 | found = 0; | |
4095 | for (a = st->arrays; a ; a = a->next) { | |
949c47a0 | 4096 | dev = get_imsm_dev(super, a->info.container_member); |
620b1713 DW |
4097 | map = get_imsm_map(dev, 0); |
4098 | ||
4099 | if (get_imsm_disk_slot(map, victim) >= 0) | |
4100 | found++; | |
e8319a19 DW |
4101 | } |
4102 | ||
24565c9a | 4103 | /* delete the victim if it is no longer being |
e8319a19 DW |
4104 | * utilized anywhere |
4105 | */ | |
e8319a19 | 4106 | if (!found) { |
ae6aad82 | 4107 | struct dl **dlp; |
24565c9a | 4108 | |
47ee5a45 DW |
4109 | /* We know that 'manager' isn't touching anything, |
4110 | * so it is safe to delete | |
4111 | */ | |
24565c9a | 4112 | for (dlp = &super->disks; *dlp; dlp = &(*dlp)->next) |
ae6aad82 DW |
4113 | if ((*dlp)->index == victim) |
4114 | break; | |
47ee5a45 DW |
4115 | |
4116 | /* victim may be on the missing list */ | |
4117 | if (!*dlp) | |
4118 | for (dlp = &super->missing; *dlp; dlp = &(*dlp)->next) | |
4119 | if ((*dlp)->index == victim) | |
4120 | break; | |
24565c9a | 4121 | imsm_delete(super, dlp, victim); |
e8319a19 | 4122 | } |
8273f55e DW |
4123 | break; |
4124 | } | |
4125 | case update_create_array: { | |
4126 | /* someone wants to create a new array, we need to be aware of | |
4127 | * a few races/collisions: | |
4128 | * 1/ 'Create' called by two separate instances of mdadm | |
4129 | * 2/ 'Create' versus 'activate_spare': mdadm has chosen | |
4130 | * devices that have since been assimilated via | |
4131 | * activate_spare. | |
4132 | * In the event this update can not be carried out mdadm will | |
4133 | * (FIX ME) notice that its update did not take hold. | |
4134 | */ | |
4135 | struct imsm_update_create_array *u = (void *) update->buf; | |
ba2de7ba | 4136 | struct intel_dev *dv; |
8273f55e DW |
4137 | struct imsm_dev *dev; |
4138 | struct imsm_map *map, *new_map; | |
4139 | unsigned long long start, end; | |
4140 | unsigned long long new_start, new_end; | |
4141 | int i; | |
54c2c1ea DW |
4142 | struct disk_info *inf; |
4143 | struct dl *dl; | |
8273f55e DW |
4144 | |
4145 | /* handle racing creates: first come first serve */ | |
4146 | if (u->dev_idx < mpb->num_raid_devs) { | |
4147 | dprintf("%s: subarray %d already defined\n", | |
4148 | __func__, u->dev_idx); | |
ba2de7ba | 4149 | goto create_error; |
8273f55e DW |
4150 | } |
4151 | ||
4152 | /* check update is next in sequence */ | |
4153 | if (u->dev_idx != mpb->num_raid_devs) { | |
6a3e913e DW |
4154 | dprintf("%s: can not create array %d expected index %d\n", |
4155 | __func__, u->dev_idx, mpb->num_raid_devs); | |
ba2de7ba | 4156 | goto create_error; |
8273f55e DW |
4157 | } |
4158 | ||
a965f303 | 4159 | new_map = get_imsm_map(&u->dev, 0); |
8273f55e DW |
4160 | new_start = __le32_to_cpu(new_map->pba_of_lba0); |
4161 | new_end = new_start + __le32_to_cpu(new_map->blocks_per_member); | |
54c2c1ea | 4162 | inf = get_disk_info(u); |
8273f55e DW |
4163 | |
4164 | /* handle activate_spare versus create race: | |
4165 | * check to make sure that overlapping arrays do not include | |
4166 | * overalpping disks | |
4167 | */ | |
4168 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
949c47a0 | 4169 | dev = get_imsm_dev(super, i); |
a965f303 | 4170 | map = get_imsm_map(dev, 0); |
8273f55e DW |
4171 | start = __le32_to_cpu(map->pba_of_lba0); |
4172 | end = start + __le32_to_cpu(map->blocks_per_member); | |
4173 | if ((new_start >= start && new_start <= end) || | |
4174 | (start >= new_start && start <= new_end)) | |
54c2c1ea DW |
4175 | /* overlap */; |
4176 | else | |
4177 | continue; | |
4178 | ||
4179 | if (disks_overlap(super, i, u)) { | |
8273f55e | 4180 | dprintf("%s: arrays overlap\n", __func__); |
ba2de7ba | 4181 | goto create_error; |
8273f55e DW |
4182 | } |
4183 | } | |
8273f55e | 4184 | |
949c47a0 DW |
4185 | /* check that prepare update was successful */ |
4186 | if (!update->space) { | |
4187 | dprintf("%s: prepare update failed\n", __func__); | |
ba2de7ba | 4188 | goto create_error; |
949c47a0 DW |
4189 | } |
4190 | ||
54c2c1ea DW |
4191 | /* check that all disks are still active before committing |
4192 | * changes. FIXME: could we instead handle this by creating a | |
4193 | * degraded array? That's probably not what the user expects, | |
4194 | * so better to drop this update on the floor. | |
4195 | */ | |
4196 | for (i = 0; i < new_map->num_members; i++) { | |
4197 | dl = serial_to_dl(inf[i].serial, super); | |
4198 | if (!dl) { | |
4199 | dprintf("%s: disk disappeared\n", __func__); | |
ba2de7ba | 4200 | goto create_error; |
54c2c1ea | 4201 | } |
949c47a0 DW |
4202 | } |
4203 | ||
8273f55e | 4204 | super->updates_pending++; |
54c2c1ea DW |
4205 | |
4206 | /* convert spares to members and fixup ord_tbl */ | |
4207 | for (i = 0; i < new_map->num_members; i++) { | |
4208 | dl = serial_to_dl(inf[i].serial, super); | |
4209 | if (dl->index == -1) { | |
4210 | dl->index = mpb->num_disks; | |
4211 | mpb->num_disks++; | |
4212 | dl->disk.status |= CONFIGURED_DISK; | |
4213 | dl->disk.status &= ~SPARE_DISK; | |
4214 | } | |
4215 | set_imsm_ord_tbl_ent(new_map, i, dl->index); | |
4216 | } | |
4217 | ||
ba2de7ba DW |
4218 | dv = update->space; |
4219 | dev = dv->dev; | |
949c47a0 DW |
4220 | update->space = NULL; |
4221 | imsm_copy_dev(dev, &u->dev); | |
ba2de7ba DW |
4222 | dv->index = u->dev_idx; |
4223 | dv->next = super->devlist; | |
4224 | super->devlist = dv; | |
8273f55e | 4225 | mpb->num_raid_devs++; |
8273f55e | 4226 | |
4d1313e9 | 4227 | imsm_update_version_info(super); |
8273f55e | 4228 | break; |
ba2de7ba DW |
4229 | create_error: |
4230 | /* mdmon knows how to release update->space, but not | |
4231 | * ((struct intel_dev *) update->space)->dev | |
4232 | */ | |
4233 | if (update->space) { | |
4234 | dv = update->space; | |
4235 | free(dv->dev); | |
4236 | } | |
8273f55e | 4237 | break; |
e8319a19 | 4238 | } |
43dad3d6 DW |
4239 | case update_add_disk: |
4240 | ||
4241 | /* we may be able to repair some arrays if disks are | |
4242 | * being added */ | |
4243 | if (super->add) { | |
4244 | struct active_array *a; | |
072b727f DW |
4245 | |
4246 | super->updates_pending++; | |
43dad3d6 DW |
4247 | for (a = st->arrays; a; a = a->next) |
4248 | a->check_degraded = 1; | |
4249 | } | |
e553d2a4 | 4250 | /* add some spares to the metadata */ |
43dad3d6 | 4251 | while (super->add) { |
e553d2a4 DW |
4252 | struct dl *al; |
4253 | ||
43dad3d6 DW |
4254 | al = super->add; |
4255 | super->add = al->next; | |
43dad3d6 DW |
4256 | al->next = super->disks; |
4257 | super->disks = al; | |
e553d2a4 DW |
4258 | dprintf("%s: added %x:%x\n", |
4259 | __func__, al->major, al->minor); | |
43dad3d6 DW |
4260 | } |
4261 | ||
4262 | break; | |
e8319a19 DW |
4263 | } |
4264 | } | |
88758e9d | 4265 | |
8273f55e DW |
4266 | static void imsm_prepare_update(struct supertype *st, |
4267 | struct metadata_update *update) | |
4268 | { | |
949c47a0 | 4269 | /** |
4d7b1503 DW |
4270 | * Allocate space to hold new disk entries, raid-device entries or a new |
4271 | * mpb if necessary. The manager synchronously waits for updates to | |
4272 | * complete in the monitor, so new mpb buffers allocated here can be | |
4273 | * integrated by the monitor thread without worrying about live pointers | |
4274 | * in the manager thread. | |
8273f55e | 4275 | */ |
949c47a0 | 4276 | enum imsm_update_type type = *(enum imsm_update_type *) update->buf; |
4d7b1503 DW |
4277 | struct intel_super *super = st->sb; |
4278 | struct imsm_super *mpb = super->anchor; | |
4279 | size_t buf_len; | |
4280 | size_t len = 0; | |
949c47a0 DW |
4281 | |
4282 | switch (type) { | |
4283 | case update_create_array: { | |
4284 | struct imsm_update_create_array *u = (void *) update->buf; | |
ba2de7ba | 4285 | struct intel_dev *dv; |
54c2c1ea DW |
4286 | struct imsm_dev *dev = &u->dev; |
4287 | struct imsm_map *map = get_imsm_map(dev, 0); | |
4288 | struct dl *dl; | |
4289 | struct disk_info *inf; | |
4290 | int i; | |
4291 | int activate = 0; | |
949c47a0 | 4292 | |
54c2c1ea DW |
4293 | inf = get_disk_info(u); |
4294 | len = sizeof_imsm_dev(dev, 1); | |
ba2de7ba DW |
4295 | /* allocate a new super->devlist entry */ |
4296 | dv = malloc(sizeof(*dv)); | |
4297 | if (dv) { | |
4298 | dv->dev = malloc(len); | |
4299 | if (dv->dev) | |
4300 | update->space = dv; | |
4301 | else { | |
4302 | free(dv); | |
4303 | update->space = NULL; | |
4304 | } | |
4305 | } | |
949c47a0 | 4306 | |
54c2c1ea DW |
4307 | /* count how many spares will be converted to members */ |
4308 | for (i = 0; i < map->num_members; i++) { | |
4309 | dl = serial_to_dl(inf[i].serial, super); | |
4310 | if (!dl) { | |
4311 | /* hmm maybe it failed?, nothing we can do about | |
4312 | * it here | |
4313 | */ | |
4314 | continue; | |
4315 | } | |
4316 | if (count_memberships(dl, super) == 0) | |
4317 | activate++; | |
4318 | } | |
4319 | len += activate * sizeof(struct imsm_disk); | |
949c47a0 DW |
4320 | break; |
4321 | default: | |
4322 | break; | |
4323 | } | |
4324 | } | |
8273f55e | 4325 | |
4d7b1503 DW |
4326 | /* check if we need a larger metadata buffer */ |
4327 | if (super->next_buf) | |
4328 | buf_len = super->next_len; | |
4329 | else | |
4330 | buf_len = super->len; | |
4331 | ||
4332 | if (__le32_to_cpu(mpb->mpb_size) + len > buf_len) { | |
4333 | /* ok we need a larger buf than what is currently allocated | |
4334 | * if this allocation fails process_update will notice that | |
4335 | * ->next_len is set and ->next_buf is NULL | |
4336 | */ | |
4337 | buf_len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + len, 512); | |
4338 | if (super->next_buf) | |
4339 | free(super->next_buf); | |
4340 | ||
4341 | super->next_len = buf_len; | |
c8151cbc | 4342 | if (posix_memalign(&super->next_buf, 512, buf_len) != 0) |
4d7b1503 DW |
4343 | super->next_buf = NULL; |
4344 | } | |
8273f55e DW |
4345 | } |
4346 | ||
ae6aad82 | 4347 | /* must be called while manager is quiesced */ |
24565c9a | 4348 | static void imsm_delete(struct intel_super *super, struct dl **dlp, int index) |
ae6aad82 DW |
4349 | { |
4350 | struct imsm_super *mpb = super->anchor; | |
ae6aad82 DW |
4351 | struct dl *iter; |
4352 | struct imsm_dev *dev; | |
4353 | struct imsm_map *map; | |
24565c9a DW |
4354 | int i, j, num_members; |
4355 | __u32 ord; | |
ae6aad82 | 4356 | |
24565c9a DW |
4357 | dprintf("%s: deleting device[%d] from imsm_super\n", |
4358 | __func__, index); | |
ae6aad82 DW |
4359 | |
4360 | /* shift all indexes down one */ | |
4361 | for (iter = super->disks; iter; iter = iter->next) | |
24565c9a | 4362 | if (iter->index > index) |
ae6aad82 | 4363 | iter->index--; |
47ee5a45 DW |
4364 | for (iter = super->missing; iter; iter = iter->next) |
4365 | if (iter->index > index) | |
4366 | iter->index--; | |
ae6aad82 DW |
4367 | |
4368 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
4369 | dev = get_imsm_dev(super, i); | |
4370 | map = get_imsm_map(dev, 0); | |
24565c9a DW |
4371 | num_members = map->num_members; |
4372 | for (j = 0; j < num_members; j++) { | |
4373 | /* update ord entries being careful not to propagate | |
4374 | * ord-flags to the first map | |
4375 | */ | |
4376 | ord = get_imsm_ord_tbl_ent(dev, j); | |
ae6aad82 | 4377 | |
24565c9a DW |
4378 | if (ord_to_idx(ord) <= index) |
4379 | continue; | |
ae6aad82 | 4380 | |
24565c9a DW |
4381 | map = get_imsm_map(dev, 0); |
4382 | set_imsm_ord_tbl_ent(map, j, ord_to_idx(ord - 1)); | |
4383 | map = get_imsm_map(dev, 1); | |
4384 | if (map) | |
4385 | set_imsm_ord_tbl_ent(map, j, ord - 1); | |
ae6aad82 DW |
4386 | } |
4387 | } | |
4388 | ||
4389 | mpb->num_disks--; | |
4390 | super->updates_pending++; | |
24565c9a DW |
4391 | if (*dlp) { |
4392 | struct dl *dl = *dlp; | |
4393 | ||
4394 | *dlp = (*dlp)->next; | |
4395 | __free_imsm_disk(dl); | |
4396 | } | |
ae6aad82 | 4397 | } |
0e600426 | 4398 | #endif /* MDASSEMBLE */ |
ae6aad82 | 4399 | |
cdddbdbc DW |
4400 | struct superswitch super_imsm = { |
4401 | #ifndef MDASSEMBLE | |
4402 | .examine_super = examine_super_imsm, | |
4403 | .brief_examine_super = brief_examine_super_imsm, | |
4404 | .detail_super = detail_super_imsm, | |
4405 | .brief_detail_super = brief_detail_super_imsm, | |
bf5a934a | 4406 | .write_init_super = write_init_super_imsm, |
0e600426 N |
4407 | .validate_geometry = validate_geometry_imsm, |
4408 | .add_to_super = add_to_super_imsm, | |
d665cc31 | 4409 | .detail_platform = detail_platform_imsm, |
cdddbdbc DW |
4410 | #endif |
4411 | .match_home = match_home_imsm, | |
4412 | .uuid_from_super= uuid_from_super_imsm, | |
4413 | .getinfo_super = getinfo_super_imsm, | |
4414 | .update_super = update_super_imsm, | |
4415 | ||
4416 | .avail_size = avail_size_imsm, | |
4417 | ||
4418 | .compare_super = compare_super_imsm, | |
4419 | ||
4420 | .load_super = load_super_imsm, | |
bf5a934a | 4421 | .init_super = init_super_imsm, |
cdddbdbc DW |
4422 | .store_super = store_zero_imsm, |
4423 | .free_super = free_super_imsm, | |
4424 | .match_metadata_desc = match_metadata_desc_imsm, | |
bf5a934a | 4425 | .container_content = container_content_imsm, |
a18a888e | 4426 | .default_layout = imsm_level_to_layout, |
cdddbdbc | 4427 | |
cdddbdbc | 4428 | .external = 1, |
4cce4069 | 4429 | .name = "imsm", |
845dea95 | 4430 | |
0e600426 | 4431 | #ifndef MDASSEMBLE |
845dea95 NB |
4432 | /* for mdmon */ |
4433 | .open_new = imsm_open_new, | |
4434 | .load_super = load_super_imsm, | |
ed9d66aa | 4435 | .set_array_state= imsm_set_array_state, |
845dea95 NB |
4436 | .set_disk = imsm_set_disk, |
4437 | .sync_metadata = imsm_sync_metadata, | |
88758e9d | 4438 | .activate_spare = imsm_activate_spare, |
e8319a19 | 4439 | .process_update = imsm_process_update, |
8273f55e | 4440 | .prepare_update = imsm_prepare_update, |
0e600426 | 4441 | #endif /* MDASSEMBLE */ |
cdddbdbc | 4442 | }; |