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