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