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