]>
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" |
cdddbdbc DW |
24 | #include <values.h> |
25 | #include <scsi/sg.h> | |
26 | #include <ctype.h> | |
27 | ||
28 | /* MPB == Metadata Parameter Block */ | |
29 | #define MPB_SIGNATURE "Intel Raid ISM Cfg Sig. " | |
30 | #define MPB_SIG_LEN (strlen(MPB_SIGNATURE)) | |
31 | #define MPB_VERSION_RAID0 "1.0.00" | |
32 | #define MPB_VERSION_RAID1 "1.1.00" | |
33 | #define MPB_VERSION_RAID5 "1.2.02" | |
34 | #define MAX_SIGNATURE_LENGTH 32 | |
35 | #define MAX_RAID_SERIAL_LEN 16 | |
c2c087e6 DW |
36 | #define MPB_SECTOR_CNT 418 |
37 | #define IMSM_RESERVED_SECTORS 4096 | |
cdddbdbc DW |
38 | |
39 | /* Disk configuration info. */ | |
40 | #define IMSM_MAX_DEVICES 255 | |
41 | struct imsm_disk { | |
42 | __u8 serial[MAX_RAID_SERIAL_LEN];/* 0xD8 - 0xE7 ascii serial number */ | |
43 | __u32 total_blocks; /* 0xE8 - 0xEB total blocks */ | |
44 | __u32 scsi_id; /* 0xEC - 0xEF scsi ID */ | |
45 | __u32 status; /* 0xF0 - 0xF3 */ | |
46 | #define SPARE_DISK 0x01 /* Spare */ | |
47 | #define CONFIGURED_DISK 0x02 /* Member of some RaidDev */ | |
48 | #define FAILED_DISK 0x04 /* Permanent failure */ | |
49 | #define USABLE_DISK 0x08 /* Fully usable unless FAILED_DISK is set */ | |
50 | ||
51 | #define IMSM_DISK_FILLERS 5 | |
52 | __u32 filler[IMSM_DISK_FILLERS]; /* 0xF4 - 0x107 MPB_DISK_FILLERS for future expansion */ | |
53 | }; | |
54 | ||
55 | /* RAID map configuration infos. */ | |
56 | struct imsm_map { | |
57 | __u32 pba_of_lba0; /* start address of partition */ | |
58 | __u32 blocks_per_member;/* blocks per member */ | |
59 | __u32 num_data_stripes; /* number of data stripes */ | |
60 | __u16 blocks_per_strip; | |
61 | __u8 map_state; /* Normal, Uninitialized, Degraded, Failed */ | |
62 | #define IMSM_T_STATE_NORMAL 0 | |
63 | #define IMSM_T_STATE_UNINITIALIZED 1 | |
64 | #define IMSM_T_STATE_DEGRADED 2 /* FIXME: is this correct? */ | |
65 | #define IMSM_T_STATE_FAILED 3 /* FIXME: is this correct? */ | |
66 | __u8 raid_level; | |
67 | #define IMSM_T_RAID0 0 | |
68 | #define IMSM_T_RAID1 1 | |
69 | #define IMSM_T_RAID5 5 /* since metadata version 1.2.02 ? */ | |
70 | __u8 num_members; /* number of member disks */ | |
71 | __u8 reserved[3]; | |
72 | __u32 filler[7]; /* expansion area */ | |
7eef0453 | 73 | #define IMSM_ORD_REBUILD (1 << 24) |
cdddbdbc | 74 | __u32 disk_ord_tbl[1]; /* disk_ord_tbl[num_members], |
7eef0453 DW |
75 | * top byte contains some flags |
76 | */ | |
cdddbdbc DW |
77 | } __attribute__ ((packed)); |
78 | ||
79 | struct imsm_vol { | |
f8f603f1 DW |
80 | __u32 curr_migr_unit; |
81 | __u32 reserved; | |
cdddbdbc DW |
82 | __u8 migr_state; /* Normal or Migrating */ |
83 | __u8 migr_type; /* Initializing, Rebuilding, ... */ | |
84 | __u8 dirty; | |
85 | __u8 fill[1]; | |
86 | __u32 filler[5]; | |
87 | struct imsm_map map[1]; | |
88 | /* here comes another one if migr_state */ | |
89 | } __attribute__ ((packed)); | |
90 | ||
91 | struct imsm_dev { | |
92 | __u8 volume[MAX_RAID_SERIAL_LEN]; | |
93 | __u32 size_low; | |
94 | __u32 size_high; | |
95 | __u32 status; /* Persistent RaidDev status */ | |
96 | __u32 reserved_blocks; /* Reserved blocks at beginning of volume */ | |
97 | #define IMSM_DEV_FILLERS 12 | |
98 | __u32 filler[IMSM_DEV_FILLERS]; | |
99 | struct imsm_vol vol; | |
100 | } __attribute__ ((packed)); | |
101 | ||
102 | struct imsm_super { | |
103 | __u8 sig[MAX_SIGNATURE_LENGTH]; /* 0x00 - 0x1F */ | |
104 | __u32 check_sum; /* 0x20 - 0x23 MPB Checksum */ | |
105 | __u32 mpb_size; /* 0x24 - 0x27 Size of MPB */ | |
106 | __u32 family_num; /* 0x28 - 0x2B Checksum from first time this config was written */ | |
107 | __u32 generation_num; /* 0x2C - 0x2F Incremented each time this array's MPB is written */ | |
604b746f JD |
108 | __u32 error_log_size; /* 0x30 - 0x33 in bytes */ |
109 | __u32 attributes; /* 0x34 - 0x37 */ | |
cdddbdbc DW |
110 | __u8 num_disks; /* 0x38 Number of configured disks */ |
111 | __u8 num_raid_devs; /* 0x39 Number of configured volumes */ | |
604b746f JD |
112 | __u8 error_log_pos; /* 0x3A */ |
113 | __u8 fill[1]; /* 0x3B */ | |
114 | __u32 cache_size; /* 0x3c - 0x40 in mb */ | |
115 | __u32 orig_family_num; /* 0x40 - 0x43 original family num */ | |
116 | __u32 pwr_cycle_count; /* 0x44 - 0x47 simulated power cycle count for array */ | |
117 | __u32 bbm_log_size; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */ | |
118 | #define IMSM_FILLERS 35 | |
119 | __u32 filler[IMSM_FILLERS]; /* 0x4C - 0xD7 RAID_MPB_FILLERS */ | |
cdddbdbc DW |
120 | struct imsm_disk disk[1]; /* 0xD8 diskTbl[numDisks] */ |
121 | /* here comes imsm_dev[num_raid_devs] */ | |
604b746f | 122 | /* here comes BBM logs */ |
cdddbdbc DW |
123 | } __attribute__ ((packed)); |
124 | ||
604b746f JD |
125 | #define BBM_LOG_MAX_ENTRIES 254 |
126 | ||
127 | struct bbm_log_entry { | |
128 | __u64 defective_block_start; | |
129 | #define UNREADABLE 0xFFFFFFFF | |
130 | __u32 spare_block_offset; | |
131 | __u16 remapped_marked_count; | |
132 | __u16 disk_ordinal; | |
133 | } __attribute__ ((__packed__)); | |
134 | ||
135 | struct bbm_log { | |
136 | __u32 signature; /* 0xABADB10C */ | |
137 | __u32 entry_count; | |
138 | __u32 reserved_spare_block_count; /* 0 */ | |
139 | __u32 reserved; /* 0xFFFF */ | |
140 | __u64 first_spare_lba; | |
141 | struct bbm_log_entry mapped_block_entries[BBM_LOG_MAX_ENTRIES]; | |
142 | } __attribute__ ((__packed__)); | |
143 | ||
144 | ||
cdddbdbc DW |
145 | #ifndef MDASSEMBLE |
146 | static char *map_state_str[] = { "normal", "uninitialized", "degraded", "failed" }; | |
147 | #endif | |
148 | ||
87eb16df | 149 | static unsigned int sector_count(__u32 bytes) |
cdddbdbc | 150 | { |
87eb16df DW |
151 | return ((bytes + (512-1)) & (~(512-1))) / 512; |
152 | } | |
cdddbdbc | 153 | |
87eb16df DW |
154 | static unsigned int mpb_sectors(struct imsm_super *mpb) |
155 | { | |
156 | return sector_count(__le32_to_cpu(mpb->mpb_size)); | |
cdddbdbc DW |
157 | } |
158 | ||
159 | /* internal representation of IMSM metadata */ | |
160 | struct intel_super { | |
161 | union { | |
949c47a0 DW |
162 | void *buf; /* O_DIRECT buffer for reading/writing metadata */ |
163 | struct imsm_super *anchor; /* immovable parameters */ | |
cdddbdbc | 164 | }; |
949c47a0 | 165 | size_t len; /* size of the 'buf' allocation */ |
4d7b1503 DW |
166 | void *next_buf; /* for realloc'ing buf from the manager */ |
167 | size_t next_len; | |
c2c087e6 DW |
168 | int updates_pending; /* count of pending updates for mdmon */ |
169 | int creating_imsm; /* flag to indicate container creation */ | |
bf5a934a | 170 | int current_vol; /* index of raid device undergoing creation */ |
949c47a0 DW |
171 | #define IMSM_MAX_RAID_DEVS 2 |
172 | struct imsm_dev *dev_tbl[IMSM_MAX_RAID_DEVS]; | |
cdddbdbc DW |
173 | struct dl { |
174 | struct dl *next; | |
175 | int index; | |
176 | __u8 serial[MAX_RAID_SERIAL_LEN]; | |
177 | int major, minor; | |
178 | char *devname; | |
b9f594fe | 179 | struct imsm_disk disk; |
cdddbdbc DW |
180 | int fd; |
181 | } *disks; | |
43dad3d6 | 182 | struct dl *add; /* list of disks to add while mdmon active */ |
47ee5a45 | 183 | struct dl *missing; /* disks removed while we weren't looking */ |
43dad3d6 | 184 | struct bbm_log *bbm_log; |
cdddbdbc DW |
185 | }; |
186 | ||
c2c087e6 DW |
187 | struct extent { |
188 | unsigned long long start, size; | |
189 | }; | |
190 | ||
88758e9d DW |
191 | /* definition of messages passed to imsm_process_update */ |
192 | enum imsm_update_type { | |
193 | update_activate_spare, | |
8273f55e | 194 | update_create_array, |
43dad3d6 | 195 | update_add_disk, |
88758e9d DW |
196 | }; |
197 | ||
198 | struct imsm_update_activate_spare { | |
199 | enum imsm_update_type type; | |
d23fe947 | 200 | struct dl *dl; |
88758e9d DW |
201 | int slot; |
202 | int array; | |
203 | struct imsm_update_activate_spare *next; | |
204 | }; | |
205 | ||
8273f55e DW |
206 | struct imsm_update_create_array { |
207 | enum imsm_update_type type; | |
8273f55e | 208 | int dev_idx; |
6a3e913e | 209 | struct imsm_dev dev; |
8273f55e DW |
210 | }; |
211 | ||
43dad3d6 DW |
212 | struct imsm_update_add_disk { |
213 | enum imsm_update_type type; | |
214 | }; | |
215 | ||
cdddbdbc DW |
216 | static struct supertype *match_metadata_desc_imsm(char *arg) |
217 | { | |
218 | struct supertype *st; | |
219 | ||
220 | if (strcmp(arg, "imsm") != 0 && | |
221 | strcmp(arg, "default") != 0 | |
222 | ) | |
223 | return NULL; | |
224 | ||
225 | st = malloc(sizeof(*st)); | |
ef609477 | 226 | memset(st, 0, sizeof(*st)); |
cdddbdbc DW |
227 | st->ss = &super_imsm; |
228 | st->max_devs = IMSM_MAX_DEVICES; | |
229 | st->minor_version = 0; | |
230 | st->sb = NULL; | |
231 | return st; | |
232 | } | |
233 | ||
0e600426 | 234 | #ifndef MDASSEMBLE |
cdddbdbc DW |
235 | static __u8 *get_imsm_version(struct imsm_super *mpb) |
236 | { | |
237 | return &mpb->sig[MPB_SIG_LEN]; | |
238 | } | |
0e600426 | 239 | #endif |
cdddbdbc | 240 | |
949c47a0 DW |
241 | /* retrieve a disk directly from the anchor when the anchor is known to be |
242 | * up-to-date, currently only at load time | |
243 | */ | |
244 | static struct imsm_disk *__get_imsm_disk(struct imsm_super *mpb, __u8 index) | |
cdddbdbc | 245 | { |
949c47a0 | 246 | if (index >= mpb->num_disks) |
cdddbdbc DW |
247 | return NULL; |
248 | return &mpb->disk[index]; | |
249 | } | |
250 | ||
0e600426 | 251 | #ifndef MDASSEMBLE |
b9f594fe | 252 | /* retrieve a disk from the parsed metadata */ |
949c47a0 DW |
253 | static struct imsm_disk *get_imsm_disk(struct intel_super *super, __u8 index) |
254 | { | |
b9f594fe DW |
255 | struct dl *d; |
256 | ||
257 | for (d = super->disks; d; d = d->next) | |
258 | if (d->index == index) | |
259 | return &d->disk; | |
260 | ||
261 | return NULL; | |
949c47a0 | 262 | } |
0e600426 | 263 | #endif |
949c47a0 DW |
264 | |
265 | /* generate a checksum directly from the anchor when the anchor is known to be | |
266 | * up-to-date, currently only at load or write_super after coalescing | |
267 | */ | |
268 | static __u32 __gen_imsm_checksum(struct imsm_super *mpb) | |
cdddbdbc DW |
269 | { |
270 | __u32 end = mpb->mpb_size / sizeof(end); | |
271 | __u32 *p = (__u32 *) mpb; | |
272 | __u32 sum = 0; | |
273 | ||
274 | while (end--) | |
275 | sum += __le32_to_cpu(*p++); | |
276 | ||
277 | return sum - __le32_to_cpu(mpb->check_sum); | |
278 | } | |
279 | ||
a965f303 DW |
280 | static size_t sizeof_imsm_map(struct imsm_map *map) |
281 | { | |
282 | return sizeof(struct imsm_map) + sizeof(__u32) * (map->num_members - 1); | |
283 | } | |
284 | ||
285 | struct imsm_map *get_imsm_map(struct imsm_dev *dev, int second_map) | |
cdddbdbc | 286 | { |
a965f303 DW |
287 | struct imsm_map *map = &dev->vol.map[0]; |
288 | ||
289 | if (second_map && !dev->vol.migr_state) | |
290 | return NULL; | |
291 | else if (second_map) { | |
292 | void *ptr = map; | |
293 | ||
294 | return ptr + sizeof_imsm_map(map); | |
295 | } else | |
296 | return map; | |
297 | ||
298 | } | |
cdddbdbc | 299 | |
3393c6af DW |
300 | /* return the size of the device. |
301 | * migr_state increases the returned size if map[0] were to be duplicated | |
302 | */ | |
303 | static size_t sizeof_imsm_dev(struct imsm_dev *dev, int migr_state) | |
a965f303 DW |
304 | { |
305 | size_t size = sizeof(*dev) - sizeof(struct imsm_map) + | |
306 | sizeof_imsm_map(get_imsm_map(dev, 0)); | |
cdddbdbc DW |
307 | |
308 | /* migrating means an additional map */ | |
a965f303 DW |
309 | if (dev->vol.migr_state) |
310 | size += sizeof_imsm_map(get_imsm_map(dev, 1)); | |
3393c6af DW |
311 | else if (migr_state) |
312 | size += sizeof_imsm_map(get_imsm_map(dev, 0)); | |
cdddbdbc DW |
313 | |
314 | return size; | |
315 | } | |
316 | ||
949c47a0 | 317 | static struct imsm_dev *__get_imsm_dev(struct imsm_super *mpb, __u8 index) |
cdddbdbc DW |
318 | { |
319 | int offset; | |
320 | int i; | |
321 | void *_mpb = mpb; | |
322 | ||
949c47a0 | 323 | if (index >= mpb->num_raid_devs) |
cdddbdbc DW |
324 | return NULL; |
325 | ||
326 | /* devices start after all disks */ | |
327 | offset = ((void *) &mpb->disk[mpb->num_disks]) - _mpb; | |
328 | ||
329 | for (i = 0; i <= index; i++) | |
330 | if (i == index) | |
331 | return _mpb + offset; | |
332 | else | |
3393c6af | 333 | offset += sizeof_imsm_dev(_mpb + offset, 0); |
cdddbdbc DW |
334 | |
335 | return NULL; | |
336 | } | |
337 | ||
949c47a0 DW |
338 | static struct imsm_dev *get_imsm_dev(struct intel_super *super, __u8 index) |
339 | { | |
340 | if (index >= super->anchor->num_raid_devs) | |
341 | return NULL; | |
342 | return super->dev_tbl[index]; | |
343 | } | |
344 | ||
7eef0453 DW |
345 | static __u32 get_imsm_ord_tbl_ent(struct imsm_dev *dev, int slot) |
346 | { | |
347 | struct imsm_map *map; | |
348 | ||
349 | if (dev->vol.migr_state) | |
7eef0453 | 350 | map = get_imsm_map(dev, 1); |
fb9bf0d3 DW |
351 | else |
352 | map = get_imsm_map(dev, 0); | |
7eef0453 | 353 | |
ff077194 DW |
354 | /* top byte identifies disk under rebuild */ |
355 | return __le32_to_cpu(map->disk_ord_tbl[slot]); | |
356 | } | |
357 | ||
358 | #define ord_to_idx(ord) (((ord) << 8) >> 8) | |
359 | static __u32 get_imsm_disk_idx(struct imsm_dev *dev, int slot) | |
360 | { | |
361 | __u32 ord = get_imsm_ord_tbl_ent(dev, slot); | |
362 | ||
363 | return ord_to_idx(ord); | |
7eef0453 DW |
364 | } |
365 | ||
be73972f DW |
366 | static void set_imsm_ord_tbl_ent(struct imsm_map *map, int slot, __u32 ord) |
367 | { | |
368 | map->disk_ord_tbl[slot] = __cpu_to_le32(ord); | |
369 | } | |
370 | ||
cdddbdbc DW |
371 | static int get_imsm_raid_level(struct imsm_map *map) |
372 | { | |
373 | if (map->raid_level == 1) { | |
374 | if (map->num_members == 2) | |
375 | return 1; | |
376 | else | |
377 | return 10; | |
378 | } | |
379 | ||
380 | return map->raid_level; | |
381 | } | |
382 | ||
c2c087e6 DW |
383 | static int cmp_extent(const void *av, const void *bv) |
384 | { | |
385 | const struct extent *a = av; | |
386 | const struct extent *b = bv; | |
387 | if (a->start < b->start) | |
388 | return -1; | |
389 | if (a->start > b->start) | |
390 | return 1; | |
391 | return 0; | |
392 | } | |
393 | ||
394 | static struct extent *get_extents(struct intel_super *super, struct dl *dl) | |
395 | { | |
396 | /* find a list of used extents on the given physical device */ | |
c2c087e6 DW |
397 | struct extent *rv, *e; |
398 | int i, j; | |
399 | int memberships = 0; | |
14e8215b | 400 | __u32 reservation = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS; |
c2c087e6 | 401 | |
949c47a0 DW |
402 | for (i = 0; i < super->anchor->num_raid_devs; i++) { |
403 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
a965f303 | 404 | struct imsm_map *map = get_imsm_map(dev, 0); |
c2c087e6 DW |
405 | |
406 | for (j = 0; j < map->num_members; j++) { | |
ff077194 | 407 | __u32 index = get_imsm_disk_idx(dev, j); |
c2c087e6 DW |
408 | |
409 | if (index == dl->index) | |
410 | memberships++; | |
411 | } | |
412 | } | |
413 | rv = malloc(sizeof(struct extent) * (memberships + 1)); | |
414 | if (!rv) | |
415 | return NULL; | |
416 | e = rv; | |
417 | ||
949c47a0 DW |
418 | for (i = 0; i < super->anchor->num_raid_devs; i++) { |
419 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
a965f303 | 420 | struct imsm_map *map = get_imsm_map(dev, 0); |
c2c087e6 DW |
421 | |
422 | for (j = 0; j < map->num_members; j++) { | |
ff077194 | 423 | __u32 index = get_imsm_disk_idx(dev, j); |
c2c087e6 DW |
424 | |
425 | if (index == dl->index) { | |
426 | e->start = __le32_to_cpu(map->pba_of_lba0); | |
427 | e->size = __le32_to_cpu(map->blocks_per_member); | |
428 | e++; | |
429 | } | |
430 | } | |
431 | } | |
432 | qsort(rv, memberships, sizeof(*rv), cmp_extent); | |
433 | ||
14e8215b DW |
434 | /* determine the start of the metadata |
435 | * when no raid devices are defined use the default | |
436 | * ...otherwise allow the metadata to truncate the value | |
437 | * as is the case with older versions of imsm | |
438 | */ | |
439 | if (memberships) { | |
440 | struct extent *last = &rv[memberships - 1]; | |
441 | __u32 remainder; | |
442 | ||
443 | remainder = __le32_to_cpu(dl->disk.total_blocks) - | |
444 | (last->start + last->size); | |
445 | if (reservation > remainder) | |
446 | reservation = remainder; | |
447 | } | |
448 | e->start = __le32_to_cpu(dl->disk.total_blocks) - reservation; | |
c2c087e6 DW |
449 | e->size = 0; |
450 | return rv; | |
451 | } | |
452 | ||
14e8215b DW |
453 | /* try to determine how much space is reserved for metadata from |
454 | * the last get_extents() entry, otherwise fallback to the | |
455 | * default | |
456 | */ | |
457 | static __u32 imsm_reserved_sectors(struct intel_super *super, struct dl *dl) | |
458 | { | |
459 | struct extent *e; | |
460 | int i; | |
461 | __u32 rv; | |
462 | ||
463 | /* for spares just return a minimal reservation which will grow | |
464 | * once the spare is picked up by an array | |
465 | */ | |
466 | if (dl->index == -1) | |
467 | return MPB_SECTOR_CNT; | |
468 | ||
469 | e = get_extents(super, dl); | |
470 | if (!e) | |
471 | return MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS; | |
472 | ||
473 | /* scroll to last entry */ | |
474 | for (i = 0; e[i].size; i++) | |
475 | continue; | |
476 | ||
477 | rv = __le32_to_cpu(dl->disk.total_blocks) - e[i].start; | |
478 | ||
479 | free(e); | |
480 | ||
481 | return rv; | |
482 | } | |
483 | ||
484 | #ifndef MDASSEMBLE | |
44470971 | 485 | static void print_imsm_dev(struct imsm_dev *dev, char *uuid, int disk_idx) |
cdddbdbc DW |
486 | { |
487 | __u64 sz; | |
488 | int slot; | |
a965f303 | 489 | struct imsm_map *map = get_imsm_map(dev, 0); |
b10b37b8 | 490 | __u32 ord; |
cdddbdbc DW |
491 | |
492 | printf("\n"); | |
1e7bc0ed | 493 | printf("[%.16s]:\n", dev->volume); |
44470971 | 494 | printf(" UUID : %s\n", uuid); |
cdddbdbc DW |
495 | printf(" RAID Level : %d\n", get_imsm_raid_level(map)); |
496 | printf(" Members : %d\n", map->num_members); | |
497 | for (slot = 0; slot < map->num_members; slot++) | |
44470971 | 498 | if (disk_idx== get_imsm_disk_idx(dev, slot)) |
cdddbdbc | 499 | break; |
b10b37b8 DW |
500 | if (slot < map->num_members) { |
501 | ord = get_imsm_ord_tbl_ent(dev, slot); | |
502 | printf(" This Slot : %d%s\n", slot, | |
503 | ord & IMSM_ORD_REBUILD ? " (out-of-sync)" : ""); | |
504 | } else | |
cdddbdbc DW |
505 | printf(" This Slot : ?\n"); |
506 | sz = __le32_to_cpu(dev->size_high); | |
507 | sz <<= 32; | |
508 | sz += __le32_to_cpu(dev->size_low); | |
509 | printf(" Array Size : %llu%s\n", (unsigned long long)sz, | |
510 | human_size(sz * 512)); | |
511 | sz = __le32_to_cpu(map->blocks_per_member); | |
512 | printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz, | |
513 | human_size(sz * 512)); | |
514 | printf(" Sector Offset : %u\n", | |
515 | __le32_to_cpu(map->pba_of_lba0)); | |
516 | printf(" Num Stripes : %u\n", | |
517 | __le32_to_cpu(map->num_data_stripes)); | |
518 | printf(" Chunk Size : %u KiB\n", | |
519 | __le16_to_cpu(map->blocks_per_strip) / 2); | |
520 | printf(" Reserved : %d\n", __le32_to_cpu(dev->reserved_blocks)); | |
3393c6af DW |
521 | printf(" Migrate State : %s", dev->vol.migr_state ? "migrating" : "idle"); |
522 | if (dev->vol.migr_state) | |
523 | printf(": %s", dev->vol.migr_type ? "rebuilding" : "initializing"); | |
524 | printf("\n"); | |
525 | printf(" Map State : %s", map_state_str[map->map_state]); | |
526 | if (dev->vol.migr_state) { | |
527 | struct imsm_map *map = get_imsm_map(dev, 1); | |
b10b37b8 | 528 | printf(" <-- %s", map_state_str[map->map_state]); |
3393c6af DW |
529 | } |
530 | printf("\n"); | |
cdddbdbc | 531 | printf(" Dirty State : %s\n", dev->vol.dirty ? "dirty" : "clean"); |
cdddbdbc DW |
532 | } |
533 | ||
14e8215b | 534 | static void print_imsm_disk(struct imsm_super *mpb, int index, __u32 reserved) |
cdddbdbc | 535 | { |
949c47a0 | 536 | struct imsm_disk *disk = __get_imsm_disk(mpb, index); |
1f24f035 | 537 | char str[MAX_RAID_SERIAL_LEN + 1]; |
cdddbdbc DW |
538 | __u32 s; |
539 | __u64 sz; | |
540 | ||
e9d82038 DW |
541 | if (index < 0) |
542 | return; | |
543 | ||
cdddbdbc | 544 | printf("\n"); |
1f24f035 | 545 | snprintf(str, MAX_RAID_SERIAL_LEN + 1, "%s", disk->serial); |
cdddbdbc DW |
546 | printf(" Disk%02d Serial : %s\n", index, str); |
547 | s = __le32_to_cpu(disk->status); | |
548 | printf(" State :%s%s%s%s\n", s&SPARE_DISK ? " spare" : "", | |
549 | s&CONFIGURED_DISK ? " active" : "", | |
550 | s&FAILED_DISK ? " failed" : "", | |
551 | s&USABLE_DISK ? " usable" : ""); | |
552 | printf(" Id : %08x\n", __le32_to_cpu(disk->scsi_id)); | |
14e8215b | 553 | sz = __le32_to_cpu(disk->total_blocks) - reserved; |
cdddbdbc DW |
554 | printf(" Usable Size : %llu%s\n", (unsigned long long)sz, |
555 | human_size(sz * 512)); | |
556 | } | |
557 | ||
44470971 DW |
558 | static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info); |
559 | ||
cdddbdbc DW |
560 | static void examine_super_imsm(struct supertype *st, char *homehost) |
561 | { | |
562 | struct intel_super *super = st->sb; | |
949c47a0 | 563 | struct imsm_super *mpb = super->anchor; |
cdddbdbc DW |
564 | char str[MAX_SIGNATURE_LENGTH]; |
565 | int i; | |
27fd6274 DW |
566 | struct mdinfo info; |
567 | char nbuf[64]; | |
cdddbdbc | 568 | __u32 sum; |
14e8215b | 569 | __u32 reserved = imsm_reserved_sectors(super, super->disks); |
cdddbdbc | 570 | |
27fd6274 | 571 | |
cdddbdbc DW |
572 | snprintf(str, MPB_SIG_LEN, "%s", mpb->sig); |
573 | printf(" Magic : %s\n", str); | |
574 | snprintf(str, strlen(MPB_VERSION_RAID0), "%s", get_imsm_version(mpb)); | |
575 | printf(" Version : %s\n", get_imsm_version(mpb)); | |
576 | printf(" Family : %08x\n", __le32_to_cpu(mpb->family_num)); | |
577 | printf(" Generation : %08x\n", __le32_to_cpu(mpb->generation_num)); | |
27fd6274 DW |
578 | getinfo_super_imsm(st, &info); |
579 | fname_from_uuid(st, &info, nbuf,'-'); | |
580 | printf(" UUID : %s\n", nbuf + 5); | |
cdddbdbc DW |
581 | sum = __le32_to_cpu(mpb->check_sum); |
582 | printf(" Checksum : %08x %s\n", sum, | |
949c47a0 | 583 | __gen_imsm_checksum(mpb) == sum ? "correct" : "incorrect"); |
87eb16df | 584 | printf(" MPB Sectors : %d\n", mpb_sectors(mpb)); |
cdddbdbc DW |
585 | printf(" Disks : %d\n", mpb->num_disks); |
586 | printf(" RAID Devices : %d\n", mpb->num_raid_devs); | |
14e8215b | 587 | print_imsm_disk(mpb, super->disks->index, reserved); |
604b746f JD |
588 | if (super->bbm_log) { |
589 | struct bbm_log *log = super->bbm_log; | |
590 | ||
591 | printf("\n"); | |
592 | printf("Bad Block Management Log:\n"); | |
593 | printf(" Log Size : %d\n", __le32_to_cpu(mpb->bbm_log_size)); | |
594 | printf(" Signature : %x\n", __le32_to_cpu(log->signature)); | |
595 | printf(" Entry Count : %d\n", __le32_to_cpu(log->entry_count)); | |
596 | printf(" Spare Blocks : %d\n", __le32_to_cpu(log->reserved_spare_block_count)); | |
597 | printf(" First Spare : %llx\n", __le64_to_cpu(log->first_spare_lba)); | |
598 | } | |
44470971 DW |
599 | for (i = 0; i < mpb->num_raid_devs; i++) { |
600 | struct mdinfo info; | |
601 | struct imsm_dev *dev = __get_imsm_dev(mpb, i); | |
602 | ||
603 | super->current_vol = i; | |
604 | getinfo_super_imsm(st, &info); | |
605 | fname_from_uuid(st, &info, nbuf, '-'); | |
606 | print_imsm_dev(dev, nbuf + 5, super->disks->index); | |
607 | } | |
cdddbdbc DW |
608 | for (i = 0; i < mpb->num_disks; i++) { |
609 | if (i == super->disks->index) | |
610 | continue; | |
14e8215b | 611 | print_imsm_disk(mpb, i, reserved); |
cdddbdbc DW |
612 | } |
613 | } | |
614 | ||
615 | static void brief_examine_super_imsm(struct supertype *st) | |
616 | { | |
27fd6274 | 617 | /* We just write a generic IMSM ARRAY entry */ |
ff54de6e N |
618 | struct mdinfo info; |
619 | char nbuf[64]; | |
cf8de691 | 620 | char nbuf1[64]; |
1e7bc0ed DW |
621 | struct intel_super *super = st->sb; |
622 | int i; | |
623 | ||
624 | if (!super->anchor->num_raid_devs) | |
625 | return; | |
ff54de6e N |
626 | |
627 | getinfo_super_imsm(st, &info); | |
628 | fname_from_uuid(st, &info, nbuf,'-'); | |
cf8de691 | 629 | printf("ARRAY metadata=imsm auto=md UUID=%s\n", nbuf + 5); |
1e7bc0ed DW |
630 | for (i = 0; i < super->anchor->num_raid_devs; i++) { |
631 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
632 | ||
633 | super->current_vol = i; | |
634 | getinfo_super_imsm(st, &info); | |
cf8de691 | 635 | fname_from_uuid(st, &info, nbuf1,'-'); |
44470971 DW |
636 | printf("ARRAY /dev/md/%.16s container=%s\n" |
637 | " member=%d auto=mdp UUID=%s\n", | |
cf8de691 | 638 | dev->volume, nbuf + 5, i, nbuf1 + 5); |
1e7bc0ed | 639 | } |
cdddbdbc DW |
640 | } |
641 | ||
642 | static void detail_super_imsm(struct supertype *st, char *homehost) | |
643 | { | |
3ebe00a1 DW |
644 | struct mdinfo info; |
645 | char nbuf[64]; | |
646 | ||
647 | getinfo_super_imsm(st, &info); | |
648 | fname_from_uuid(st, &info, nbuf,'-'); | |
649 | printf("\n UUID : %s\n", nbuf + 5); | |
cdddbdbc DW |
650 | } |
651 | ||
652 | static void brief_detail_super_imsm(struct supertype *st) | |
653 | { | |
ff54de6e N |
654 | struct mdinfo info; |
655 | char nbuf[64]; | |
656 | getinfo_super_imsm(st, &info); | |
657 | fname_from_uuid(st, &info, nbuf,'-'); | |
658 | printf(" UUID=%s", nbuf + 5); | |
cdddbdbc DW |
659 | } |
660 | #endif | |
661 | ||
662 | static int match_home_imsm(struct supertype *st, char *homehost) | |
663 | { | |
664 | printf("%s\n", __FUNCTION__); | |
665 | ||
9362c1c8 | 666 | return -1; |
cdddbdbc DW |
667 | } |
668 | ||
669 | static void uuid_from_super_imsm(struct supertype *st, int uuid[4]) | |
670 | { | |
51006d85 N |
671 | /* The uuid returned here is used for: |
672 | * uuid to put into bitmap file (Create, Grow) | |
673 | * uuid for backup header when saving critical section (Grow) | |
674 | * comparing uuids when re-adding a device into an array | |
675 | * In these cases the uuid required is that of the data-array, | |
676 | * not the device-set. | |
677 | * uuid to recognise same set when adding a missing device back | |
678 | * to an array. This is a uuid for the device-set. | |
679 | * | |
680 | * For each of these we can make do with a truncated | |
681 | * or hashed uuid rather than the original, as long as | |
682 | * everyone agrees. | |
683 | * In each case the uuid required is that of the data-array, | |
684 | * not the device-set. | |
43dad3d6 | 685 | */ |
51006d85 N |
686 | /* imsm does not track uuid's so we synthesis one using sha1 on |
687 | * - The signature (Which is constant for all imsm array, but no matter) | |
688 | * - the family_num of the container | |
689 | * - the index number of the volume | |
690 | * - the 'serial' number of the volume. | |
691 | * Hopefully these are all constant. | |
692 | */ | |
693 | struct intel_super *super = st->sb; | |
43dad3d6 | 694 | |
51006d85 N |
695 | char buf[20]; |
696 | struct sha1_ctx ctx; | |
697 | struct imsm_dev *dev = NULL; | |
698 | ||
699 | sha1_init_ctx(&ctx); | |
700 | sha1_process_bytes(super->anchor->sig, MAX_SIGNATURE_LENGTH, &ctx); | |
701 | sha1_process_bytes(&super->anchor->family_num, sizeof(__u32), &ctx); | |
702 | if (super->current_vol >= 0) | |
703 | dev = get_imsm_dev(super, super->current_vol); | |
704 | if (dev) { | |
705 | __u32 vol = super->current_vol; | |
706 | sha1_process_bytes(&vol, sizeof(vol), &ctx); | |
707 | sha1_process_bytes(dev->volume, MAX_RAID_SERIAL_LEN, &ctx); | |
708 | } | |
709 | sha1_finish_ctx(&ctx, buf); | |
710 | memcpy(uuid, buf, 4*4); | |
cdddbdbc DW |
711 | } |
712 | ||
0d481d37 | 713 | #if 0 |
4f5bc454 DW |
714 | static void |
715 | get_imsm_numerical_version(struct imsm_super *mpb, int *m, int *p) | |
cdddbdbc | 716 | { |
cdddbdbc DW |
717 | __u8 *v = get_imsm_version(mpb); |
718 | __u8 *end = mpb->sig + MAX_SIGNATURE_LENGTH; | |
719 | char major[] = { 0, 0, 0 }; | |
720 | char minor[] = { 0 ,0, 0 }; | |
721 | char patch[] = { 0, 0, 0 }; | |
722 | char *ver_parse[] = { major, minor, patch }; | |
723 | int i, j; | |
724 | ||
725 | i = j = 0; | |
726 | while (*v != '\0' && v < end) { | |
727 | if (*v != '.' && j < 2) | |
728 | ver_parse[i][j++] = *v; | |
729 | else { | |
730 | i++; | |
731 | j = 0; | |
732 | } | |
733 | v++; | |
734 | } | |
735 | ||
4f5bc454 DW |
736 | *m = strtol(minor, NULL, 0); |
737 | *p = strtol(patch, NULL, 0); | |
738 | } | |
0d481d37 | 739 | #endif |
4f5bc454 | 740 | |
c2c087e6 DW |
741 | static int imsm_level_to_layout(int level) |
742 | { | |
743 | switch (level) { | |
744 | case 0: | |
745 | case 1: | |
746 | return 0; | |
747 | case 5: | |
748 | case 6: | |
a380c027 | 749 | return ALGORITHM_LEFT_ASYMMETRIC; |
c2c087e6 | 750 | case 10: |
c92a2527 | 751 | return 0x102; |
c2c087e6 DW |
752 | } |
753 | return -1; | |
754 | } | |
755 | ||
bf5a934a DW |
756 | static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info) |
757 | { | |
758 | struct intel_super *super = st->sb; | |
949c47a0 | 759 | struct imsm_dev *dev = get_imsm_dev(super, super->current_vol); |
a965f303 | 760 | struct imsm_map *map = get_imsm_map(dev, 0); |
bf5a934a DW |
761 | |
762 | info->container_member = super->current_vol; | |
763 | info->array.raid_disks = map->num_members; | |
764 | info->array.level = get_imsm_raid_level(map); | |
765 | info->array.layout = imsm_level_to_layout(info->array.level); | |
766 | info->array.md_minor = -1; | |
767 | info->array.ctime = 0; | |
768 | info->array.utime = 0; | |
301406c9 DW |
769 | info->array.chunk_size = __le16_to_cpu(map->blocks_per_strip) << 9; |
770 | info->array.state = !dev->vol.dirty; | |
771 | ||
772 | info->disk.major = 0; | |
773 | info->disk.minor = 0; | |
bf5a934a DW |
774 | |
775 | info->data_offset = __le32_to_cpu(map->pba_of_lba0); | |
776 | info->component_size = __le32_to_cpu(map->blocks_per_member); | |
301406c9 | 777 | memset(info->uuid, 0, sizeof(info->uuid)); |
bf5a934a | 778 | |
f8f603f1 | 779 | if (map->map_state == IMSM_T_STATE_UNINITIALIZED || dev->vol.dirty) |
301406c9 | 780 | info->resync_start = 0; |
f8f603f1 DW |
781 | else if (dev->vol.migr_state) |
782 | info->resync_start = __le32_to_cpu(dev->vol.curr_migr_unit); | |
301406c9 DW |
783 | else |
784 | info->resync_start = ~0ULL; | |
785 | ||
786 | strncpy(info->name, (char *) dev->volume, MAX_RAID_SERIAL_LEN); | |
787 | info->name[MAX_RAID_SERIAL_LEN] = 0; | |
bf5a934a | 788 | |
f35f2525 N |
789 | info->array.major_version = -1; |
790 | info->array.minor_version = -2; | |
bf5a934a DW |
791 | sprintf(info->text_version, "/%s/%d", |
792 | devnum2devname(st->container_dev), | |
793 | info->container_member); | |
a67dd8cc | 794 | info->safe_mode_delay = 4000; /* 4 secs like the Matrix driver */ |
51006d85 | 795 | uuid_from_super_imsm(st, info->uuid); |
bf5a934a DW |
796 | } |
797 | ||
798 | ||
4f5bc454 DW |
799 | static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info) |
800 | { | |
801 | struct intel_super *super = st->sb; | |
4f5bc454 DW |
802 | struct imsm_disk *disk; |
803 | __u32 s; | |
4f5bc454 | 804 | |
bf5a934a DW |
805 | if (super->current_vol >= 0) { |
806 | getinfo_super_imsm_volume(st, info); | |
807 | return; | |
808 | } | |
d23fe947 DW |
809 | |
810 | /* Set raid_disks to zero so that Assemble will always pull in valid | |
811 | * spares | |
812 | */ | |
813 | info->array.raid_disks = 0; | |
cdddbdbc DW |
814 | info->array.level = LEVEL_CONTAINER; |
815 | info->array.layout = 0; | |
816 | info->array.md_minor = -1; | |
c2c087e6 | 817 | info->array.ctime = 0; /* N/A for imsm */ |
cdddbdbc DW |
818 | info->array.utime = 0; |
819 | info->array.chunk_size = 0; | |
820 | ||
821 | info->disk.major = 0; | |
822 | info->disk.minor = 0; | |
cdddbdbc | 823 | info->disk.raid_disk = -1; |
c2c087e6 | 824 | info->reshape_active = 0; |
f35f2525 N |
825 | info->array.major_version = -1; |
826 | info->array.minor_version = -2; | |
c2c087e6 | 827 | strcpy(info->text_version, "imsm"); |
a67dd8cc | 828 | info->safe_mode_delay = 0; |
c2c087e6 DW |
829 | info->disk.number = -1; |
830 | info->disk.state = 0; | |
c5afc314 | 831 | info->name[0] = 0; |
c2c087e6 | 832 | |
4a04ec6c | 833 | if (super->disks) { |
14e8215b DW |
834 | __u32 reserved = imsm_reserved_sectors(super, super->disks); |
835 | ||
b9f594fe | 836 | disk = &super->disks->disk; |
14e8215b DW |
837 | info->data_offset = __le32_to_cpu(disk->total_blocks) - reserved; |
838 | info->component_size = reserved; | |
4a04ec6c DW |
839 | s = __le32_to_cpu(disk->status); |
840 | info->disk.state = s & CONFIGURED_DISK ? (1 << MD_DISK_ACTIVE) : 0; | |
841 | info->disk.state |= s & FAILED_DISK ? (1 << MD_DISK_FAULTY) : 0; | |
032e9e29 | 842 | info->disk.state |= s & SPARE_DISK ? 0 : (1 << MD_DISK_SYNC); |
cdddbdbc | 843 | } |
a575e2a7 DW |
844 | |
845 | /* only call uuid_from_super_imsm when this disk is part of a populated container, | |
846 | * ->compare_super may have updated the 'num_raid_devs' field for spares | |
847 | */ | |
848 | if (info->disk.state & (1 << MD_DISK_SYNC) || super->anchor->num_raid_devs) | |
36ba7d48 | 849 | uuid_from_super_imsm(st, info->uuid); |
032e9e29 DW |
850 | else |
851 | memcpy(info->uuid, uuid_match_any, sizeof(int[4])); | |
cdddbdbc DW |
852 | } |
853 | ||
cdddbdbc DW |
854 | static int update_super_imsm(struct supertype *st, struct mdinfo *info, |
855 | char *update, char *devname, int verbose, | |
856 | int uuid_set, char *homehost) | |
857 | { | |
f352c545 DW |
858 | /* FIXME */ |
859 | ||
860 | /* For 'assemble' and 'force' we need to return non-zero if any | |
861 | * change was made. For others, the return value is ignored. | |
862 | * Update options are: | |
863 | * force-one : This device looks a bit old but needs to be included, | |
864 | * update age info appropriately. | |
865 | * assemble: clear any 'faulty' flag to allow this device to | |
866 | * be assembled. | |
867 | * force-array: Array is degraded but being forced, mark it clean | |
868 | * if that will be needed to assemble it. | |
869 | * | |
870 | * newdev: not used ???? | |
871 | * grow: Array has gained a new device - this is currently for | |
872 | * linear only | |
873 | * resync: mark as dirty so a resync will happen. | |
874 | * name: update the name - preserving the homehost | |
875 | * | |
876 | * Following are not relevant for this imsm: | |
877 | * sparc2.2 : update from old dodgey metadata | |
878 | * super-minor: change the preferred_minor number | |
879 | * summaries: update redundant counters. | |
880 | * uuid: Change the uuid of the array to match watch is given | |
881 | * homehost: update the recorded homehost | |
882 | * _reshape_progress: record new reshape_progress position. | |
883 | */ | |
884 | int rv = 0; | |
885 | //struct intel_super *super = st->sb; | |
886 | //struct imsm_super *mpb = super->mpb; | |
887 | ||
888 | if (strcmp(update, "grow") == 0) { | |
889 | } | |
890 | if (strcmp(update, "resync") == 0) { | |
891 | /* dev->vol.dirty = 1; */ | |
892 | } | |
893 | ||
894 | /* IMSM has no concept of UUID or homehost */ | |
895 | ||
896 | return rv; | |
cdddbdbc DW |
897 | } |
898 | ||
c2c087e6 | 899 | static size_t disks_to_mpb_size(int disks) |
cdddbdbc | 900 | { |
c2c087e6 | 901 | size_t size; |
cdddbdbc | 902 | |
c2c087e6 DW |
903 | size = sizeof(struct imsm_super); |
904 | size += (disks - 1) * sizeof(struct imsm_disk); | |
905 | size += 2 * sizeof(struct imsm_dev); | |
906 | /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */ | |
907 | size += (4 - 2) * sizeof(struct imsm_map); | |
908 | /* 4 possible disk_ord_tbl's */ | |
909 | size += 4 * (disks - 1) * sizeof(__u32); | |
910 | ||
911 | return size; | |
912 | } | |
913 | ||
914 | static __u64 avail_size_imsm(struct supertype *st, __u64 devsize) | |
915 | { | |
916 | if (devsize < (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS)) | |
917 | return 0; | |
918 | ||
919 | return devsize - (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS); | |
cdddbdbc DW |
920 | } |
921 | ||
922 | static int compare_super_imsm(struct supertype *st, struct supertype *tst) | |
923 | { | |
924 | /* | |
925 | * return: | |
926 | * 0 same, or first was empty, and second was copied | |
927 | * 1 second had wrong number | |
928 | * 2 wrong uuid | |
929 | * 3 wrong other info | |
930 | */ | |
931 | struct intel_super *first = st->sb; | |
932 | struct intel_super *sec = tst->sb; | |
933 | ||
934 | if (!first) { | |
935 | st->sb = tst->sb; | |
936 | tst->sb = NULL; | |
937 | return 0; | |
938 | } | |
939 | ||
949c47a0 | 940 | if (memcmp(first->anchor->sig, sec->anchor->sig, MAX_SIGNATURE_LENGTH) != 0) |
cdddbdbc | 941 | return 3; |
d23fe947 DW |
942 | |
943 | /* if an anchor does not have num_raid_devs set then it is a free | |
944 | * floating spare | |
945 | */ | |
946 | if (first->anchor->num_raid_devs > 0 && | |
947 | sec->anchor->num_raid_devs > 0) { | |
948 | if (first->anchor->family_num != sec->anchor->family_num) | |
949 | return 3; | |
d23fe947 | 950 | } |
cdddbdbc | 951 | |
3e372e5a DW |
952 | /* if 'first' is a spare promote it to a populated mpb with sec's |
953 | * family number | |
954 | */ | |
955 | if (first->anchor->num_raid_devs == 0 && | |
956 | sec->anchor->num_raid_devs > 0) { | |
78d30f94 DW |
957 | int i; |
958 | ||
959 | /* we need to copy raid device info from sec if an allocation | |
960 | * fails here we don't associate the spare | |
961 | */ | |
962 | for (i = 0; i < sec->anchor->num_raid_devs; i++) { | |
963 | first->dev_tbl[i] = malloc(sizeof(struct imsm_dev)); | |
964 | if (!first->dev_tbl) { | |
965 | while (--i >= 0) { | |
966 | free(first->dev_tbl[i]); | |
967 | first->dev_tbl[i] = NULL; | |
968 | } | |
969 | fprintf(stderr, "imsm: failed to associate spare\n"); | |
970 | return 3; | |
971 | } | |
972 | *first->dev_tbl[i] = *sec->dev_tbl[i]; | |
973 | } | |
974 | ||
3e372e5a DW |
975 | first->anchor->num_raid_devs = sec->anchor->num_raid_devs; |
976 | first->anchor->family_num = sec->anchor->family_num; | |
977 | } | |
978 | ||
cdddbdbc DW |
979 | return 0; |
980 | } | |
981 | ||
0030e8d6 DW |
982 | static void fd2devname(int fd, char *name) |
983 | { | |
984 | struct stat st; | |
985 | char path[256]; | |
986 | char dname[100]; | |
987 | char *nm; | |
988 | int rv; | |
989 | ||
990 | name[0] = '\0'; | |
991 | if (fstat(fd, &st) != 0) | |
992 | return; | |
993 | sprintf(path, "/sys/dev/block/%d:%d", | |
994 | major(st.st_rdev), minor(st.st_rdev)); | |
995 | ||
996 | rv = readlink(path, dname, sizeof(dname)); | |
997 | if (rv <= 0) | |
998 | return; | |
999 | ||
1000 | dname[rv] = '\0'; | |
1001 | nm = strrchr(dname, '/'); | |
1002 | nm++; | |
1003 | snprintf(name, MAX_RAID_SERIAL_LEN, "/dev/%s", nm); | |
1004 | } | |
1005 | ||
1006 | ||
cdddbdbc DW |
1007 | extern int scsi_get_serial(int fd, void *buf, size_t buf_len); |
1008 | ||
1009 | static int imsm_read_serial(int fd, char *devname, | |
1010 | __u8 serial[MAX_RAID_SERIAL_LEN]) | |
1011 | { | |
1012 | unsigned char scsi_serial[255]; | |
cdddbdbc DW |
1013 | int rv; |
1014 | int rsp_len; | |
1f24f035 DW |
1015 | int len; |
1016 | char *c, *rsp_buf; | |
cdddbdbc DW |
1017 | |
1018 | memset(scsi_serial, 0, sizeof(scsi_serial)); | |
cdddbdbc | 1019 | |
f9ba0ff1 DW |
1020 | rv = scsi_get_serial(fd, scsi_serial, sizeof(scsi_serial)); |
1021 | ||
40ebbb9c | 1022 | if (rv && check_env("IMSM_DEVNAME_AS_SERIAL")) { |
f9ba0ff1 DW |
1023 | memset(serial, 0, MAX_RAID_SERIAL_LEN); |
1024 | fd2devname(fd, (char *) serial); | |
0030e8d6 DW |
1025 | return 0; |
1026 | } | |
1027 | ||
cdddbdbc DW |
1028 | if (rv != 0) { |
1029 | if (devname) | |
1030 | fprintf(stderr, | |
1031 | Name ": Failed to retrieve serial for %s\n", | |
1032 | devname); | |
1033 | return rv; | |
1034 | } | |
1035 | ||
5c3db629 | 1036 | /* trim leading whitespace */ |
cdddbdbc | 1037 | rsp_len = scsi_serial[3]; |
1f24f035 DW |
1038 | rsp_buf = (char *) &scsi_serial[4]; |
1039 | c = rsp_buf; | |
1040 | while (isspace(*c)) | |
1041 | c++; | |
5c3db629 DW |
1042 | |
1043 | /* truncate len to the end of rsp_buf if necessary */ | |
1f24f035 DW |
1044 | if (c + MAX_RAID_SERIAL_LEN > rsp_buf + rsp_len) |
1045 | len = rsp_len - (c - rsp_buf); | |
1046 | else | |
1047 | len = MAX_RAID_SERIAL_LEN; | |
5c3db629 DW |
1048 | |
1049 | /* initialize the buffer and copy rsp_buf characters */ | |
1050 | memset(serial, 0, MAX_RAID_SERIAL_LEN); | |
1f24f035 | 1051 | memcpy(serial, c, len); |
5c3db629 DW |
1052 | |
1053 | /* trim trailing whitespace starting with the last character copied */ | |
1f24f035 DW |
1054 | c = (char *) &serial[len - 1]; |
1055 | while (isspace(*c) || *c == '\0') | |
1056 | *c-- = '\0'; | |
cdddbdbc DW |
1057 | |
1058 | return 0; | |
1059 | } | |
1060 | ||
1f24f035 DW |
1061 | static int serialcmp(__u8 *s1, __u8 *s2) |
1062 | { | |
1063 | return strncmp((char *) s1, (char *) s2, MAX_RAID_SERIAL_LEN); | |
1064 | } | |
1065 | ||
1066 | static void serialcpy(__u8 *dest, __u8 *src) | |
1067 | { | |
1068 | strncpy((char *) dest, (char *) src, MAX_RAID_SERIAL_LEN); | |
1069 | } | |
1070 | ||
cdddbdbc DW |
1071 | static int |
1072 | load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd) | |
1073 | { | |
cdddbdbc DW |
1074 | struct dl *dl; |
1075 | struct stat stb; | |
cdddbdbc DW |
1076 | int rv; |
1077 | int i; | |
d23fe947 DW |
1078 | int alloc = 1; |
1079 | __u8 serial[MAX_RAID_SERIAL_LEN]; | |
1080 | ||
1081 | rv = imsm_read_serial(fd, devname, serial); | |
1082 | ||
1083 | if (rv != 0) | |
1084 | return 2; | |
1085 | ||
1086 | /* check if this is a disk we have seen before. it may be a spare in | |
1087 | * super->disks while the current anchor believes it is a raid member, | |
1088 | * check if we need to update dl->index | |
1089 | */ | |
1090 | for (dl = super->disks; dl; dl = dl->next) | |
1f24f035 | 1091 | if (serialcmp(dl->serial, serial) == 0) |
d23fe947 DW |
1092 | break; |
1093 | ||
1094 | if (!dl) | |
1095 | dl = malloc(sizeof(*dl)); | |
1096 | else | |
1097 | alloc = 0; | |
cdddbdbc | 1098 | |
b9f594fe | 1099 | if (!dl) { |
cdddbdbc DW |
1100 | if (devname) |
1101 | fprintf(stderr, | |
1102 | Name ": failed to allocate disk buffer for %s\n", | |
1103 | devname); | |
1104 | return 2; | |
1105 | } | |
cdddbdbc | 1106 | |
d23fe947 DW |
1107 | if (alloc) { |
1108 | fstat(fd, &stb); | |
1109 | dl->major = major(stb.st_rdev); | |
1110 | dl->minor = minor(stb.st_rdev); | |
1111 | dl->next = super->disks; | |
1112 | dl->fd = keep_fd ? fd : -1; | |
1113 | dl->devname = devname ? strdup(devname) : NULL; | |
1f24f035 | 1114 | serialcpy(dl->serial, serial); |
8796fdc4 | 1115 | dl->index = -2; |
d23fe947 DW |
1116 | } else if (keep_fd) { |
1117 | close(dl->fd); | |
1118 | dl->fd = fd; | |
1119 | } | |
cdddbdbc | 1120 | |
d23fe947 | 1121 | /* look up this disk's index in the current anchor */ |
949c47a0 DW |
1122 | for (i = 0; i < super->anchor->num_disks; i++) { |
1123 | struct imsm_disk *disk_iter; | |
1124 | ||
1125 | disk_iter = __get_imsm_disk(super->anchor, i); | |
cdddbdbc | 1126 | |
1f24f035 | 1127 | if (serialcmp(disk_iter->serial, dl->serial) == 0) { |
d23fe947 DW |
1128 | __u32 status; |
1129 | ||
b9f594fe | 1130 | dl->disk = *disk_iter; |
d23fe947 DW |
1131 | status = __le32_to_cpu(dl->disk.status); |
1132 | /* only set index on disks that are a member of a | |
1133 | * populated contianer, i.e. one with raid_devs | |
1134 | */ | |
6c386dd3 DW |
1135 | if (status & FAILED_DISK) |
1136 | dl->index = -2; | |
1137 | else if (status & SPARE_DISK) | |
d23fe947 DW |
1138 | dl->index = -1; |
1139 | else | |
1140 | dl->index = i; | |
8796fdc4 | 1141 | |
cdddbdbc | 1142 | break; |
949c47a0 | 1143 | } |
cdddbdbc DW |
1144 | } |
1145 | ||
3f6efecc DW |
1146 | /* no match, maybe a stale failed drive */ |
1147 | if (i == super->anchor->num_disks && dl->index >= 0) { | |
1148 | dl->disk = *__get_imsm_disk(super->anchor, dl->index); | |
1149 | if (__le32_to_cpu(dl->disk.status) & FAILED_DISK) | |
1150 | dl->index = -2; | |
1151 | } | |
1152 | ||
d23fe947 DW |
1153 | if (alloc) |
1154 | super->disks = dl; | |
6c386dd3 | 1155 | |
949c47a0 DW |
1156 | return 0; |
1157 | } | |
1158 | ||
1159 | static void imsm_copy_dev(struct imsm_dev *dest, struct imsm_dev *src) | |
1160 | { | |
3393c6af DW |
1161 | memcpy(dest, src, sizeof_imsm_dev(src, 0)); |
1162 | } | |
1163 | ||
0e600426 | 1164 | #ifndef MDASSEMBLE |
0c046afd DW |
1165 | /* When migrating map0 contains the 'destination' state while map1 |
1166 | * contains the current state. When not migrating map0 contains the | |
1167 | * current state. This routine assumes that map[0].map_state is set to | |
1168 | * the current array state before being called. | |
1169 | * | |
1170 | * Migration is indicated by one of the following states | |
1171 | * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed) | |
1172 | * 2/ Initialize (migr_state=1 migr_type=0 map0state=normal | |
1173 | * map1state=unitialized) | |
1174 | * 3/ Verify (Resync) (migr_state=1 migr_type=1 map0state=normal | |
1175 | * map1state=normal) | |
1176 | * 4/ Rebuild (migr_state=1 migr_type=1 map0state=normal | |
1177 | * map1state=degraded) | |
1178 | */ | |
1179 | static void migrate(struct imsm_dev *dev, __u8 to_state, int rebuild_resync) | |
3393c6af | 1180 | { |
0c046afd | 1181 | struct imsm_map *dest; |
3393c6af DW |
1182 | struct imsm_map *src = get_imsm_map(dev, 0); |
1183 | ||
0c046afd DW |
1184 | dev->vol.migr_state = 1; |
1185 | dev->vol.migr_type = rebuild_resync; | |
f8f603f1 | 1186 | dev->vol.curr_migr_unit = 0; |
0c046afd DW |
1187 | dest = get_imsm_map(dev, 1); |
1188 | ||
3393c6af | 1189 | memcpy(dest, src, sizeof_imsm_map(src)); |
0c046afd | 1190 | src->map_state = to_state; |
949c47a0 | 1191 | } |
f8f603f1 DW |
1192 | |
1193 | static void end_migration(struct imsm_dev *dev, __u8 map_state) | |
1194 | { | |
1195 | struct imsm_map *map = get_imsm_map(dev, 0); | |
1196 | ||
1197 | dev->vol.migr_state = 0; | |
1198 | dev->vol.curr_migr_unit = 0; | |
1199 | map->map_state = map_state; | |
1200 | } | |
0e600426 | 1201 | #endif |
949c47a0 DW |
1202 | |
1203 | static int parse_raid_devices(struct intel_super *super) | |
1204 | { | |
1205 | int i; | |
1206 | struct imsm_dev *dev_new; | |
4d7b1503 DW |
1207 | size_t len, len_migr; |
1208 | size_t space_needed = 0; | |
1209 | struct imsm_super *mpb = super->anchor; | |
949c47a0 DW |
1210 | |
1211 | for (i = 0; i < super->anchor->num_raid_devs; i++) { | |
1212 | struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i); | |
1213 | ||
4d7b1503 DW |
1214 | len = sizeof_imsm_dev(dev_iter, 0); |
1215 | len_migr = sizeof_imsm_dev(dev_iter, 1); | |
1216 | if (len_migr > len) | |
1217 | space_needed += len_migr - len; | |
1218 | ||
1219 | dev_new = malloc(len_migr); | |
949c47a0 DW |
1220 | if (!dev_new) |
1221 | return 1; | |
1222 | imsm_copy_dev(dev_new, dev_iter); | |
1223 | super->dev_tbl[i] = dev_new; | |
1224 | } | |
cdddbdbc | 1225 | |
4d7b1503 DW |
1226 | /* ensure that super->buf is large enough when all raid devices |
1227 | * are migrating | |
1228 | */ | |
1229 | if (__le32_to_cpu(mpb->mpb_size) + space_needed > super->len) { | |
1230 | void *buf; | |
1231 | ||
1232 | len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + space_needed, 512); | |
1233 | if (posix_memalign(&buf, 512, len) != 0) | |
1234 | return 1; | |
1235 | ||
1236 | memcpy(buf, super->buf, len); | |
1237 | free(super->buf); | |
1238 | super->buf = buf; | |
1239 | super->len = len; | |
1240 | } | |
1241 | ||
cdddbdbc DW |
1242 | return 0; |
1243 | } | |
1244 | ||
604b746f JD |
1245 | /* retrieve a pointer to the bbm log which starts after all raid devices */ |
1246 | struct bbm_log *__get_imsm_bbm_log(struct imsm_super *mpb) | |
1247 | { | |
1248 | void *ptr = NULL; | |
1249 | ||
1250 | if (__le32_to_cpu(mpb->bbm_log_size)) { | |
1251 | ptr = mpb; | |
1252 | ptr += mpb->mpb_size - __le32_to_cpu(mpb->bbm_log_size); | |
1253 | } | |
1254 | ||
1255 | return ptr; | |
1256 | } | |
1257 | ||
d23fe947 | 1258 | static void __free_imsm(struct intel_super *super, int free_disks); |
9ca2c81c | 1259 | |
cdddbdbc DW |
1260 | /* load_imsm_mpb - read matrix metadata |
1261 | * allocates super->mpb to be freed by free_super | |
1262 | */ | |
1263 | static int load_imsm_mpb(int fd, struct intel_super *super, char *devname) | |
1264 | { | |
1265 | unsigned long long dsize; | |
cdddbdbc DW |
1266 | unsigned long long sectors; |
1267 | struct stat; | |
6416d527 | 1268 | struct imsm_super *anchor; |
cdddbdbc | 1269 | __u32 check_sum; |
949c47a0 | 1270 | int rc; |
cdddbdbc | 1271 | |
cdddbdbc DW |
1272 | get_dev_size(fd, NULL, &dsize); |
1273 | ||
1274 | if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0) { | |
1275 | if (devname) | |
1276 | fprintf(stderr, | |
1277 | Name ": Cannot seek to anchor block on %s: %s\n", | |
1278 | devname, strerror(errno)); | |
1279 | return 1; | |
1280 | } | |
1281 | ||
949c47a0 | 1282 | if (posix_memalign((void**)&anchor, 512, 512) != 0) { |
ad97895e DW |
1283 | if (devname) |
1284 | fprintf(stderr, | |
1285 | Name ": Failed to allocate imsm anchor buffer" | |
1286 | " on %s\n", devname); | |
1287 | return 1; | |
1288 | } | |
949c47a0 | 1289 | if (read(fd, anchor, 512) != 512) { |
cdddbdbc DW |
1290 | if (devname) |
1291 | fprintf(stderr, | |
1292 | Name ": Cannot read anchor block on %s: %s\n", | |
1293 | devname, strerror(errno)); | |
6416d527 | 1294 | free(anchor); |
cdddbdbc DW |
1295 | return 1; |
1296 | } | |
1297 | ||
6416d527 | 1298 | if (strncmp((char *) anchor->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) { |
cdddbdbc DW |
1299 | if (devname) |
1300 | fprintf(stderr, | |
1301 | Name ": no IMSM anchor on %s\n", devname); | |
6416d527 | 1302 | free(anchor); |
cdddbdbc DW |
1303 | return 2; |
1304 | } | |
1305 | ||
d23fe947 | 1306 | __free_imsm(super, 0); |
949c47a0 DW |
1307 | super->len = ROUND_UP(anchor->mpb_size, 512); |
1308 | if (posix_memalign(&super->buf, 512, super->len) != 0) { | |
cdddbdbc DW |
1309 | if (devname) |
1310 | fprintf(stderr, | |
1311 | Name ": unable to allocate %zu byte mpb buffer\n", | |
949c47a0 | 1312 | super->len); |
6416d527 | 1313 | free(anchor); |
cdddbdbc DW |
1314 | return 2; |
1315 | } | |
949c47a0 | 1316 | memcpy(super->buf, anchor, 512); |
cdddbdbc | 1317 | |
6416d527 NB |
1318 | sectors = mpb_sectors(anchor) - 1; |
1319 | free(anchor); | |
949c47a0 DW |
1320 | if (!sectors) { |
1321 | rc = load_imsm_disk(fd, super, devname, 0); | |
1322 | if (rc == 0) | |
1323 | rc = parse_raid_devices(super); | |
1324 | return rc; | |
1325 | } | |
cdddbdbc DW |
1326 | |
1327 | /* read the extended mpb */ | |
1328 | if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0) { | |
1329 | if (devname) | |
1330 | fprintf(stderr, | |
1331 | Name ": Cannot seek to extended mpb on %s: %s\n", | |
1332 | devname, strerror(errno)); | |
1333 | return 1; | |
1334 | } | |
1335 | ||
949c47a0 | 1336 | if (read(fd, super->buf + 512, super->len - 512) != super->len - 512) { |
cdddbdbc DW |
1337 | if (devname) |
1338 | fprintf(stderr, | |
1339 | Name ": Cannot read extended mpb on %s: %s\n", | |
1340 | devname, strerror(errno)); | |
1341 | return 2; | |
1342 | } | |
1343 | ||
949c47a0 DW |
1344 | check_sum = __gen_imsm_checksum(super->anchor); |
1345 | if (check_sum != __le32_to_cpu(super->anchor->check_sum)) { | |
cdddbdbc DW |
1346 | if (devname) |
1347 | fprintf(stderr, | |
1348 | Name ": IMSM checksum %x != %x on %s\n", | |
949c47a0 | 1349 | check_sum, __le32_to_cpu(super->anchor->check_sum), |
cdddbdbc DW |
1350 | devname); |
1351 | return 2; | |
1352 | } | |
1353 | ||
604b746f JD |
1354 | /* FIXME the BBM log is disk specific so we cannot use this global |
1355 | * buffer for all disks. Ok for now since we only look at the global | |
1356 | * bbm_log_size parameter to gate assembly | |
1357 | */ | |
1358 | super->bbm_log = __get_imsm_bbm_log(super->anchor); | |
1359 | ||
949c47a0 DW |
1360 | rc = load_imsm_disk(fd, super, devname, 0); |
1361 | if (rc == 0) | |
1362 | rc = parse_raid_devices(super); | |
4d7b1503 | 1363 | |
949c47a0 | 1364 | return rc; |
cdddbdbc DW |
1365 | } |
1366 | ||
ae6aad82 DW |
1367 | static void __free_imsm_disk(struct dl *d) |
1368 | { | |
1369 | if (d->fd >= 0) | |
1370 | close(d->fd); | |
1371 | if (d->devname) | |
1372 | free(d->devname); | |
1373 | free(d); | |
1374 | ||
1375 | } | |
cdddbdbc DW |
1376 | static void free_imsm_disks(struct intel_super *super) |
1377 | { | |
47ee5a45 | 1378 | struct dl *d; |
cdddbdbc | 1379 | |
47ee5a45 DW |
1380 | while (super->disks) { |
1381 | d = super->disks; | |
cdddbdbc | 1382 | super->disks = d->next; |
ae6aad82 | 1383 | __free_imsm_disk(d); |
cdddbdbc | 1384 | } |
47ee5a45 DW |
1385 | while (super->missing) { |
1386 | d = super->missing; | |
1387 | super->missing = d->next; | |
1388 | __free_imsm_disk(d); | |
1389 | } | |
1390 | ||
cdddbdbc DW |
1391 | } |
1392 | ||
9ca2c81c | 1393 | /* free all the pieces hanging off of a super pointer */ |
d23fe947 | 1394 | static void __free_imsm(struct intel_super *super, int free_disks) |
cdddbdbc | 1395 | { |
949c47a0 DW |
1396 | int i; |
1397 | ||
9ca2c81c | 1398 | if (super->buf) { |
949c47a0 | 1399 | free(super->buf); |
9ca2c81c DW |
1400 | super->buf = NULL; |
1401 | } | |
d23fe947 DW |
1402 | if (free_disks) |
1403 | free_imsm_disks(super); | |
949c47a0 | 1404 | for (i = 0; i < IMSM_MAX_RAID_DEVS; i++) |
9ca2c81c | 1405 | if (super->dev_tbl[i]) { |
949c47a0 | 1406 | free(super->dev_tbl[i]); |
9ca2c81c DW |
1407 | super->dev_tbl[i] = NULL; |
1408 | } | |
cdddbdbc DW |
1409 | } |
1410 | ||
9ca2c81c DW |
1411 | static void free_imsm(struct intel_super *super) |
1412 | { | |
d23fe947 | 1413 | __free_imsm(super, 1); |
9ca2c81c DW |
1414 | free(super); |
1415 | } | |
cdddbdbc DW |
1416 | |
1417 | static void free_super_imsm(struct supertype *st) | |
1418 | { | |
1419 | struct intel_super *super = st->sb; | |
1420 | ||
1421 | if (!super) | |
1422 | return; | |
1423 | ||
1424 | free_imsm(super); | |
1425 | st->sb = NULL; | |
1426 | } | |
1427 | ||
c2c087e6 DW |
1428 | static struct intel_super *alloc_super(int creating_imsm) |
1429 | { | |
1430 | struct intel_super *super = malloc(sizeof(*super)); | |
1431 | ||
1432 | if (super) { | |
1433 | memset(super, 0, sizeof(*super)); | |
1434 | super->creating_imsm = creating_imsm; | |
bf5a934a | 1435 | super->current_vol = -1; |
c2c087e6 DW |
1436 | } |
1437 | ||
1438 | return super; | |
1439 | } | |
1440 | ||
cdddbdbc | 1441 | #ifndef MDASSEMBLE |
47ee5a45 DW |
1442 | /* find_missing - helper routine for load_super_imsm_all that identifies |
1443 | * disks that have disappeared from the system. This routine relies on | |
1444 | * the mpb being uptodate, which it is at load time. | |
1445 | */ | |
1446 | static int find_missing(struct intel_super *super) | |
1447 | { | |
1448 | int i; | |
1449 | struct imsm_super *mpb = super->anchor; | |
1450 | struct dl *dl; | |
1451 | struct imsm_disk *disk; | |
1452 | __u32 status; | |
1453 | ||
1454 | for (i = 0; i < mpb->num_disks; i++) { | |
1455 | disk = __get_imsm_disk(mpb, i); | |
1456 | for (dl = super->disks; dl; dl = dl->next) | |
1457 | if (serialcmp(dl->disk.serial, disk->serial) == 0) | |
1458 | break; | |
1459 | if (dl) | |
1460 | continue; | |
1461 | /* ok we have a 'disk' without a live entry in | |
1462 | * super->disks | |
1463 | */ | |
1464 | status = __le32_to_cpu(disk->status); | |
1465 | if (status & FAILED_DISK || !(status & USABLE_DISK)) | |
1466 | continue; /* never mind, already marked */ | |
1467 | ||
1468 | dl = malloc(sizeof(*dl)); | |
1469 | if (!dl) | |
1470 | return 1; | |
1471 | dl->major = 0; | |
1472 | dl->minor = 0; | |
1473 | dl->fd = -1; | |
1474 | dl->devname = strdup("missing"); | |
1475 | dl->index = i; | |
1476 | serialcpy(dl->serial, disk->serial); | |
1477 | dl->disk = *disk; | |
1478 | dl->next = super->missing; | |
1479 | super->missing = dl; | |
1480 | } | |
1481 | ||
1482 | return 0; | |
1483 | } | |
1484 | ||
cdddbdbc DW |
1485 | static int load_super_imsm_all(struct supertype *st, int fd, void **sbp, |
1486 | char *devname, int keep_fd) | |
1487 | { | |
1488 | struct mdinfo *sra; | |
1489 | struct intel_super *super; | |
1490 | struct mdinfo *sd, *best = NULL; | |
1491 | __u32 bestgen = 0; | |
1492 | __u32 gen; | |
1493 | char nm[20]; | |
1494 | int dfd; | |
1495 | int rv; | |
1496 | ||
1497 | /* check if this disk is a member of an active array */ | |
1498 | sra = sysfs_read(fd, 0, GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE); | |
1499 | if (!sra) | |
1500 | return 1; | |
1501 | ||
1502 | if (sra->array.major_version != -1 || | |
1503 | sra->array.minor_version != -2 || | |
1504 | strcmp(sra->text_version, "imsm") != 0) | |
1505 | return 1; | |
1506 | ||
c2c087e6 | 1507 | super = alloc_super(0); |
cdddbdbc DW |
1508 | if (!super) |
1509 | return 1; | |
1510 | ||
d23fe947 | 1511 | /* find the most up to date disk in this array, skipping spares */ |
cdddbdbc DW |
1512 | for (sd = sra->devs; sd; sd = sd->next) { |
1513 | sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor); | |
1514 | dfd = dev_open(nm, keep_fd ? O_RDWR : O_RDONLY); | |
1515 | if (!dfd) { | |
1516 | free_imsm(super); | |
1517 | return 2; | |
1518 | } | |
1519 | rv = load_imsm_mpb(dfd, super, NULL); | |
1520 | if (!keep_fd) | |
1521 | close(dfd); | |
1522 | if (rv == 0) { | |
d23fe947 DW |
1523 | if (super->anchor->num_raid_devs == 0) |
1524 | gen = 0; | |
1525 | else | |
1526 | gen = __le32_to_cpu(super->anchor->generation_num); | |
cdddbdbc DW |
1527 | if (!best || gen > bestgen) { |
1528 | bestgen = gen; | |
1529 | best = sd; | |
1530 | } | |
1531 | } else { | |
1532 | free_imsm(super); | |
1533 | return 2; | |
1534 | } | |
1535 | } | |
1536 | ||
1537 | if (!best) { | |
1538 | free_imsm(super); | |
1539 | return 1; | |
1540 | } | |
1541 | ||
1542 | /* load the most up to date anchor */ | |
1543 | sprintf(nm, "%d:%d", best->disk.major, best->disk.minor); | |
1544 | dfd = dev_open(nm, O_RDONLY); | |
1545 | if (!dfd) { | |
1546 | free_imsm(super); | |
1547 | return 1; | |
1548 | } | |
1549 | rv = load_imsm_mpb(dfd, super, NULL); | |
1550 | close(dfd); | |
1551 | if (rv != 0) { | |
1552 | free_imsm(super); | |
1553 | return 2; | |
1554 | } | |
1555 | ||
d23fe947 | 1556 | /* re-parse the disk list with the current anchor */ |
cdddbdbc DW |
1557 | for (sd = sra->devs ; sd ; sd = sd->next) { |
1558 | sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor); | |
1559 | dfd = dev_open(nm, keep_fd? O_RDWR : O_RDONLY); | |
1560 | if (!dfd) { | |
1561 | free_imsm(super); | |
1562 | return 2; | |
1563 | } | |
1564 | load_imsm_disk(dfd, super, NULL, keep_fd); | |
1565 | if (!keep_fd) | |
1566 | close(dfd); | |
1567 | } | |
1568 | ||
47ee5a45 DW |
1569 | |
1570 | if (find_missing(super) != 0) { | |
1571 | free_imsm(super); | |
1572 | return 2; | |
1573 | } | |
1574 | ||
f7e7067b | 1575 | if (st->subarray[0]) { |
949c47a0 | 1576 | if (atoi(st->subarray) <= super->anchor->num_raid_devs) |
bf5a934a DW |
1577 | super->current_vol = atoi(st->subarray); |
1578 | else | |
1579 | return 1; | |
f7e7067b NB |
1580 | } |
1581 | ||
cdddbdbc | 1582 | *sbp = super; |
43dad3d6 | 1583 | st->container_dev = fd2devnum(fd); |
cdddbdbc | 1584 | if (st->ss == NULL) { |
bf5a934a | 1585 | st->ss = &super_imsm; |
cdddbdbc DW |
1586 | st->minor_version = 0; |
1587 | st->max_devs = IMSM_MAX_DEVICES; | |
1588 | } | |
352452c3 | 1589 | st->loaded_container = 1; |
cdddbdbc DW |
1590 | |
1591 | return 0; | |
1592 | } | |
1593 | #endif | |
1594 | ||
1595 | static int load_super_imsm(struct supertype *st, int fd, char *devname) | |
1596 | { | |
1597 | struct intel_super *super; | |
1598 | int rv; | |
1599 | ||
1600 | #ifndef MDASSEMBLE | |
3dbccbcf | 1601 | if (load_super_imsm_all(st, fd, &st->sb, devname, 1) == 0) |
cdddbdbc DW |
1602 | return 0; |
1603 | #endif | |
f7e7067b NB |
1604 | if (st->subarray[0]) |
1605 | return 1; /* FIXME */ | |
cdddbdbc | 1606 | |
c2c087e6 | 1607 | super = alloc_super(0); |
cdddbdbc DW |
1608 | if (!super) { |
1609 | fprintf(stderr, | |
1610 | Name ": malloc of %zu failed.\n", | |
1611 | sizeof(*super)); | |
1612 | return 1; | |
1613 | } | |
1614 | ||
1615 | rv = load_imsm_mpb(fd, super, devname); | |
1616 | ||
1617 | if (rv) { | |
1618 | if (devname) | |
1619 | fprintf(stderr, | |
1620 | Name ": Failed to load all information " | |
1621 | "sections on %s\n", devname); | |
1622 | free_imsm(super); | |
1623 | return rv; | |
1624 | } | |
1625 | ||
1626 | st->sb = super; | |
1627 | if (st->ss == NULL) { | |
1628 | st->ss = &super_imsm; | |
1629 | st->minor_version = 0; | |
1630 | st->max_devs = IMSM_MAX_DEVICES; | |
1631 | } | |
352452c3 | 1632 | st->loaded_container = 0; |
cdddbdbc DW |
1633 | |
1634 | return 0; | |
1635 | } | |
1636 | ||
ef6ffade DW |
1637 | static __u16 info_to_blocks_per_strip(mdu_array_info_t *info) |
1638 | { | |
1639 | if (info->level == 1) | |
1640 | return 128; | |
1641 | return info->chunk_size >> 9; | |
1642 | } | |
1643 | ||
1644 | static __u32 info_to_num_data_stripes(mdu_array_info_t *info) | |
1645 | { | |
1646 | __u32 num_stripes; | |
1647 | ||
1648 | num_stripes = (info->size * 2) / info_to_blocks_per_strip(info); | |
1649 | if (info->level == 1) | |
1650 | num_stripes /= 2; | |
1651 | ||
1652 | return num_stripes; | |
1653 | } | |
1654 | ||
fcfd9599 DW |
1655 | static __u32 info_to_blocks_per_member(mdu_array_info_t *info) |
1656 | { | |
1657 | return (info->size * 2) & ~(info_to_blocks_per_strip(info) - 1); | |
1658 | } | |
1659 | ||
8b353278 DW |
1660 | static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info, |
1661 | unsigned long long size, char *name, | |
1662 | char *homehost, int *uuid) | |
cdddbdbc | 1663 | { |
c2c087e6 DW |
1664 | /* We are creating a volume inside a pre-existing container. |
1665 | * so st->sb is already set. | |
1666 | */ | |
1667 | struct intel_super *super = st->sb; | |
949c47a0 | 1668 | struct imsm_super *mpb = super->anchor; |
c2c087e6 DW |
1669 | struct imsm_dev *dev; |
1670 | struct imsm_vol *vol; | |
1671 | struct imsm_map *map; | |
1672 | int idx = mpb->num_raid_devs; | |
1673 | int i; | |
1674 | unsigned long long array_blocks; | |
c2c087e6 | 1675 | __u32 offset = 0; |
2c092cad | 1676 | size_t size_old, size_new; |
cdddbdbc | 1677 | |
c2c087e6 DW |
1678 | if (mpb->num_raid_devs >= 2) { |
1679 | fprintf(stderr, Name": This imsm-container already has the " | |
1680 | "maximum of 2 volumes\n"); | |
1681 | return 0; | |
1682 | } | |
1683 | ||
2c092cad DW |
1684 | /* ensure the mpb is large enough for the new data */ |
1685 | size_old = __le32_to_cpu(mpb->mpb_size); | |
1686 | size_new = disks_to_mpb_size(info->nr_disks); | |
1687 | if (size_new > size_old) { | |
1688 | void *mpb_new; | |
1689 | size_t size_round = ROUND_UP(size_new, 512); | |
1690 | ||
1691 | if (posix_memalign(&mpb_new, 512, size_round) != 0) { | |
1692 | fprintf(stderr, Name": could not allocate new mpb\n"); | |
1693 | return 0; | |
1694 | } | |
1695 | memcpy(mpb_new, mpb, size_old); | |
1696 | free(mpb); | |
1697 | mpb = mpb_new; | |
949c47a0 | 1698 | super->anchor = mpb_new; |
2c092cad DW |
1699 | mpb->mpb_size = __cpu_to_le32(size_new); |
1700 | memset(mpb_new + size_old, 0, size_round - size_old); | |
1701 | } | |
bf5a934a | 1702 | super->current_vol = idx; |
d23fe947 DW |
1703 | /* when creating the first raid device in this container set num_disks |
1704 | * to zero, i.e. delete this spare and add raid member devices in | |
1705 | * add_to_super_imsm_volume() | |
1706 | */ | |
1707 | if (super->current_vol == 0) | |
1708 | mpb->num_disks = 0; | |
bf5a934a | 1709 | sprintf(st->subarray, "%d", idx); |
949c47a0 DW |
1710 | dev = malloc(sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1)); |
1711 | if (!dev) { | |
1712 | fprintf(stderr, Name": could not allocate raid device\n"); | |
1713 | return 0; | |
1714 | } | |
c2c087e6 DW |
1715 | strncpy((char *) dev->volume, name, MAX_RAID_SERIAL_LEN); |
1716 | array_blocks = calc_array_size(info->level, info->raid_disks, | |
1717 | info->layout, info->chunk_size, | |
1718 | info->size*2); | |
1719 | dev->size_low = __cpu_to_le32((__u32) array_blocks); | |
1720 | dev->size_high = __cpu_to_le32((__u32) (array_blocks >> 32)); | |
1721 | dev->status = __cpu_to_le32(0); | |
1722 | dev->reserved_blocks = __cpu_to_le32(0); | |
1723 | vol = &dev->vol; | |
1724 | vol->migr_state = 0; | |
1725 | vol->migr_type = 0; | |
1726 | vol->dirty = 0; | |
f8f603f1 | 1727 | vol->curr_migr_unit = 0; |
c2c087e6 | 1728 | for (i = 0; i < idx; i++) { |
949c47a0 | 1729 | struct imsm_dev *prev = get_imsm_dev(super, i); |
a965f303 | 1730 | struct imsm_map *pmap = get_imsm_map(prev, 0); |
c2c087e6 DW |
1731 | |
1732 | offset += __le32_to_cpu(pmap->blocks_per_member); | |
1733 | offset += IMSM_RESERVED_SECTORS; | |
1734 | } | |
a965f303 | 1735 | map = get_imsm_map(dev, 0); |
c2c087e6 | 1736 | map->pba_of_lba0 = __cpu_to_le32(offset); |
fcfd9599 | 1737 | map->blocks_per_member = __cpu_to_le32(info_to_blocks_per_member(info)); |
ef6ffade DW |
1738 | map->blocks_per_strip = __cpu_to_le16(info_to_blocks_per_strip(info)); |
1739 | map->num_data_stripes = __cpu_to_le32(info_to_num_data_stripes(info)); | |
c2c087e6 DW |
1740 | map->map_state = info->level ? IMSM_T_STATE_UNINITIALIZED : |
1741 | IMSM_T_STATE_NORMAL; | |
ef6ffade DW |
1742 | |
1743 | if (info->level == 1 && info->raid_disks > 2) { | |
1744 | fprintf(stderr, Name": imsm does not support more than 2 disks" | |
1745 | "in a raid1 volume\n"); | |
1746 | return 0; | |
1747 | } | |
c2c087e6 DW |
1748 | if (info->level == 10) |
1749 | map->raid_level = 1; | |
1750 | else | |
1751 | map->raid_level = info->level; | |
ef6ffade | 1752 | |
c2c087e6 DW |
1753 | map->num_members = info->raid_disks; |
1754 | for (i = 0; i < map->num_members; i++) { | |
1755 | /* initialized in add_to_super */ | |
be73972f | 1756 | set_imsm_ord_tbl_ent(map, i, 0); |
c2c087e6 | 1757 | } |
949c47a0 DW |
1758 | mpb->num_raid_devs++; |
1759 | super->dev_tbl[super->current_vol] = dev; | |
c2c087e6 DW |
1760 | |
1761 | return 1; | |
cdddbdbc DW |
1762 | } |
1763 | ||
bf5a934a DW |
1764 | static int init_super_imsm(struct supertype *st, mdu_array_info_t *info, |
1765 | unsigned long long size, char *name, | |
1766 | char *homehost, int *uuid) | |
1767 | { | |
1768 | /* This is primarily called by Create when creating a new array. | |
1769 | * We will then get add_to_super called for each component, and then | |
1770 | * write_init_super called to write it out to each device. | |
1771 | * For IMSM, Create can create on fresh devices or on a pre-existing | |
1772 | * array. | |
1773 | * To create on a pre-existing array a different method will be called. | |
1774 | * This one is just for fresh drives. | |
1775 | */ | |
1776 | struct intel_super *super; | |
1777 | struct imsm_super *mpb; | |
1778 | size_t mpb_size; | |
1779 | ||
1780 | if (!info) { | |
1781 | st->sb = NULL; | |
1782 | return 0; | |
1783 | } | |
1784 | if (st->sb) | |
1785 | return init_super_imsm_volume(st, info, size, name, homehost, | |
1786 | uuid); | |
1787 | ||
1788 | super = alloc_super(1); | |
1789 | if (!super) | |
1790 | return 0; | |
1791 | mpb_size = disks_to_mpb_size(info->nr_disks); | |
ef649044 | 1792 | if (posix_memalign(&super->buf, 512, mpb_size) != 0) { |
bf5a934a DW |
1793 | free(super); |
1794 | return 0; | |
1795 | } | |
ef649044 | 1796 | mpb = super->buf; |
bf5a934a DW |
1797 | memset(mpb, 0, mpb_size); |
1798 | ||
1799 | memcpy(mpb->sig, MPB_SIGNATURE, strlen(MPB_SIGNATURE)); | |
1800 | memcpy(mpb->sig + strlen(MPB_SIGNATURE), MPB_VERSION_RAID5, | |
1801 | strlen(MPB_VERSION_RAID5)); | |
1802 | mpb->mpb_size = mpb_size; | |
1803 | ||
bf5a934a DW |
1804 | st->sb = super; |
1805 | return 1; | |
1806 | } | |
1807 | ||
0e600426 | 1808 | #ifndef MDASSEMBLE |
bf5a934a DW |
1809 | static void add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk, |
1810 | int fd, char *devname) | |
1811 | { | |
1812 | struct intel_super *super = st->sb; | |
d23fe947 | 1813 | struct imsm_super *mpb = super->anchor; |
bf5a934a DW |
1814 | struct dl *dl; |
1815 | struct imsm_dev *dev; | |
1816 | struct imsm_map *map; | |
bf5a934a DW |
1817 | __u32 status; |
1818 | ||
949c47a0 | 1819 | dev = get_imsm_dev(super, super->current_vol); |
a965f303 | 1820 | map = get_imsm_map(dev, 0); |
bf5a934a DW |
1821 | |
1822 | for (dl = super->disks; dl ; dl = dl->next) | |
1823 | if (dl->major == dk->major && | |
1824 | dl->minor == dk->minor) | |
1825 | break; | |
d23fe947 | 1826 | |
bf5a934a DW |
1827 | if (!dl || ! (dk->state & (1<<MD_DISK_SYNC))) |
1828 | return; | |
1829 | ||
d23fe947 DW |
1830 | /* add a pristine spare to the metadata */ |
1831 | if (dl->index < 0) { | |
1832 | dl->index = super->anchor->num_disks; | |
1833 | super->anchor->num_disks++; | |
1834 | } | |
be73972f | 1835 | set_imsm_ord_tbl_ent(map, dk->number, dl->index); |
bf5a934a | 1836 | status = CONFIGURED_DISK | USABLE_DISK; |
d23fe947 DW |
1837 | dl->disk.status = __cpu_to_le32(status); |
1838 | ||
1839 | /* if we are creating the first raid device update the family number */ | |
1840 | if (super->current_vol == 0) { | |
1841 | __u32 sum; | |
1842 | struct imsm_dev *_dev = __get_imsm_dev(mpb, 0); | |
1843 | struct imsm_disk *_disk = __get_imsm_disk(mpb, dl->index); | |
1844 | ||
1845 | *_dev = *dev; | |
1846 | *_disk = dl->disk; | |
1847 | sum = __gen_imsm_checksum(mpb); | |
1848 | mpb->family_num = __cpu_to_le32(sum); | |
1849 | } | |
bf5a934a DW |
1850 | } |
1851 | ||
c2c087e6 | 1852 | static void add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk, |
cdddbdbc DW |
1853 | int fd, char *devname) |
1854 | { | |
c2c087e6 | 1855 | struct intel_super *super = st->sb; |
c2c087e6 DW |
1856 | struct dl *dd; |
1857 | unsigned long long size; | |
1858 | __u32 status, id; | |
1859 | int rv; | |
1860 | struct stat stb; | |
1861 | ||
bf5a934a DW |
1862 | if (super->current_vol >= 0) { |
1863 | add_to_super_imsm_volume(st, dk, fd, devname); | |
1864 | return; | |
1865 | } | |
1866 | ||
c2c087e6 DW |
1867 | fstat(fd, &stb); |
1868 | dd = malloc(sizeof(*dd)); | |
b9f594fe | 1869 | if (!dd) { |
c2c087e6 DW |
1870 | fprintf(stderr, |
1871 | Name ": malloc failed %s:%d.\n", __func__, __LINE__); | |
1872 | abort(); | |
1873 | } | |
1874 | memset(dd, 0, sizeof(*dd)); | |
1875 | dd->major = major(stb.st_rdev); | |
1876 | dd->minor = minor(stb.st_rdev); | |
b9f594fe | 1877 | dd->index = -1; |
c2c087e6 | 1878 | dd->devname = devname ? strdup(devname) : NULL; |
c2c087e6 DW |
1879 | dd->fd = fd; |
1880 | rv = imsm_read_serial(fd, devname, dd->serial); | |
1881 | if (rv) { | |
1882 | fprintf(stderr, | |
0030e8d6 | 1883 | Name ": failed to retrieve scsi serial, aborting\n"); |
949c47a0 | 1884 | free(dd); |
0030e8d6 | 1885 | abort(); |
c2c087e6 DW |
1886 | } |
1887 | ||
c2c087e6 DW |
1888 | get_dev_size(fd, NULL, &size); |
1889 | size /= 512; | |
1890 | status = USABLE_DISK | SPARE_DISK; | |
1f24f035 | 1891 | serialcpy(dd->disk.serial, dd->serial); |
b9f594fe DW |
1892 | dd->disk.total_blocks = __cpu_to_le32(size); |
1893 | dd->disk.status = __cpu_to_le32(status); | |
c2c087e6 | 1894 | if (sysfs_disk_to_scsi_id(fd, &id) == 0) |
b9f594fe | 1895 | dd->disk.scsi_id = __cpu_to_le32(id); |
c2c087e6 | 1896 | else |
b9f594fe | 1897 | dd->disk.scsi_id = __cpu_to_le32(0); |
43dad3d6 DW |
1898 | |
1899 | if (st->update_tail) { | |
1900 | dd->next = super->add; | |
1901 | super->add = dd; | |
1902 | } else { | |
1903 | dd->next = super->disks; | |
1904 | super->disks = dd; | |
1905 | } | |
cdddbdbc DW |
1906 | } |
1907 | ||
c2c087e6 DW |
1908 | static int store_imsm_mpb(int fd, struct intel_super *super); |
1909 | ||
d23fe947 DW |
1910 | /* spare records have their own family number and do not have any defined raid |
1911 | * devices | |
1912 | */ | |
1913 | static int write_super_imsm_spares(struct intel_super *super, int doclose) | |
1914 | { | |
1915 | struct imsm_super mpb_save; | |
1916 | struct imsm_super *mpb = super->anchor; | |
1917 | __u32 sum; | |
1918 | struct dl *d; | |
1919 | ||
1920 | mpb_save = *mpb; | |
1921 | mpb->num_raid_devs = 0; | |
1922 | mpb->num_disks = 1; | |
1923 | mpb->mpb_size = sizeof(struct imsm_super); | |
1924 | mpb->generation_num = __cpu_to_le32(1UL); | |
1925 | ||
1926 | for (d = super->disks; d; d = d->next) { | |
8796fdc4 | 1927 | if (d->index != -1) |
d23fe947 DW |
1928 | continue; |
1929 | ||
1930 | mpb->disk[0] = d->disk; | |
1931 | sum = __gen_imsm_checksum(mpb); | |
1932 | mpb->family_num = __cpu_to_le32(sum); | |
1933 | sum = __gen_imsm_checksum(mpb); | |
1934 | mpb->check_sum = __cpu_to_le32(sum); | |
1935 | ||
1936 | if (store_imsm_mpb(d->fd, super)) { | |
1937 | fprintf(stderr, "%s: failed for device %d:%d %s\n", | |
1938 | __func__, d->major, d->minor, strerror(errno)); | |
1939 | *mpb = mpb_save; | |
e74255d9 | 1940 | return 1; |
d23fe947 DW |
1941 | } |
1942 | if (doclose) { | |
1943 | close(d->fd); | |
1944 | d->fd = -1; | |
1945 | } | |
1946 | } | |
1947 | ||
1948 | *mpb = mpb_save; | |
e74255d9 | 1949 | return 0; |
d23fe947 DW |
1950 | } |
1951 | ||
c2c087e6 | 1952 | static int write_super_imsm(struct intel_super *super, int doclose) |
cdddbdbc | 1953 | { |
949c47a0 | 1954 | struct imsm_super *mpb = super->anchor; |
c2c087e6 DW |
1955 | struct dl *d; |
1956 | __u32 generation; | |
1957 | __u32 sum; | |
d23fe947 | 1958 | int spares = 0; |
949c47a0 | 1959 | int i; |
a48ac0a8 | 1960 | __u32 mpb_size = sizeof(struct imsm_super) - sizeof(struct imsm_disk); |
cdddbdbc | 1961 | |
c2c087e6 DW |
1962 | /* 'generation' is incremented everytime the metadata is written */ |
1963 | generation = __le32_to_cpu(mpb->generation_num); | |
1964 | generation++; | |
1965 | mpb->generation_num = __cpu_to_le32(generation); | |
1966 | ||
1ee1e9fc | 1967 | mpb_size += sizeof(struct imsm_disk) * mpb->num_disks; |
d23fe947 | 1968 | for (d = super->disks; d; d = d->next) { |
8796fdc4 | 1969 | if (d->index == -1) |
d23fe947 | 1970 | spares++; |
1ee1e9fc | 1971 | else |
d23fe947 | 1972 | mpb->disk[d->index] = d->disk; |
d23fe947 | 1973 | } |
47ee5a45 DW |
1974 | for (d = super->missing; d; d = d->next) |
1975 | mpb->disk[d->index] = d->disk; | |
b9f594fe | 1976 | |
949c47a0 DW |
1977 | for (i = 0; i < mpb->num_raid_devs; i++) { |
1978 | struct imsm_dev *dev = __get_imsm_dev(mpb, i); | |
1979 | ||
1980 | imsm_copy_dev(dev, super->dev_tbl[i]); | |
a48ac0a8 | 1981 | mpb_size += sizeof_imsm_dev(dev, 0); |
949c47a0 | 1982 | } |
a48ac0a8 DW |
1983 | mpb_size += __le32_to_cpu(mpb->bbm_log_size); |
1984 | mpb->mpb_size = __cpu_to_le32(mpb_size); | |
949c47a0 | 1985 | |
c2c087e6 | 1986 | /* recalculate checksum */ |
949c47a0 | 1987 | sum = __gen_imsm_checksum(mpb); |
c2c087e6 DW |
1988 | mpb->check_sum = __cpu_to_le32(sum); |
1989 | ||
d23fe947 | 1990 | /* write the mpb for disks that compose raid devices */ |
c2c087e6 | 1991 | for (d = super->disks; d ; d = d->next) { |
d23fe947 DW |
1992 | if (d->index < 0) |
1993 | continue; | |
8796fdc4 | 1994 | if (store_imsm_mpb(d->fd, super)) |
c2c087e6 DW |
1995 | fprintf(stderr, "%s: failed for device %d:%d %s\n", |
1996 | __func__, d->major, d->minor, strerror(errno)); | |
c2c087e6 DW |
1997 | if (doclose) { |
1998 | close(d->fd); | |
1999 | d->fd = -1; | |
2000 | } | |
2001 | } | |
2002 | ||
d23fe947 DW |
2003 | if (spares) |
2004 | return write_super_imsm_spares(super, doclose); | |
2005 | ||
e74255d9 | 2006 | return 0; |
c2c087e6 DW |
2007 | } |
2008 | ||
0e600426 | 2009 | |
43dad3d6 DW |
2010 | static int create_array(struct supertype *st) |
2011 | { | |
2012 | size_t len; | |
2013 | struct imsm_update_create_array *u; | |
2014 | struct intel_super *super = st->sb; | |
2015 | struct imsm_dev *dev = get_imsm_dev(super, super->current_vol); | |
2016 | ||
2017 | len = sizeof(*u) - sizeof(*dev) + sizeof_imsm_dev(dev, 0); | |
2018 | u = malloc(len); | |
2019 | if (!u) { | |
2020 | fprintf(stderr, "%s: failed to allocate update buffer\n", | |
2021 | __func__); | |
2022 | return 1; | |
2023 | } | |
2024 | ||
2025 | u->type = update_create_array; | |
2026 | u->dev_idx = super->current_vol; | |
2027 | imsm_copy_dev(&u->dev, dev); | |
2028 | append_metadata_update(st, u, len); | |
2029 | ||
2030 | return 0; | |
2031 | } | |
2032 | ||
7801ac20 | 2033 | static int _add_disk(struct supertype *st) |
43dad3d6 DW |
2034 | { |
2035 | struct intel_super *super = st->sb; | |
2036 | size_t len; | |
2037 | struct imsm_update_add_disk *u; | |
2038 | ||
2039 | if (!super->add) | |
2040 | return 0; | |
2041 | ||
2042 | len = sizeof(*u); | |
2043 | u = malloc(len); | |
2044 | if (!u) { | |
2045 | fprintf(stderr, "%s: failed to allocate update buffer\n", | |
2046 | __func__); | |
2047 | return 1; | |
2048 | } | |
2049 | ||
2050 | u->type = update_add_disk; | |
2051 | append_metadata_update(st, u, len); | |
2052 | ||
2053 | return 0; | |
2054 | } | |
2055 | ||
c2c087e6 DW |
2056 | static int write_init_super_imsm(struct supertype *st) |
2057 | { | |
8273f55e | 2058 | if (st->update_tail) { |
43dad3d6 DW |
2059 | /* queue the recently created array / added disk |
2060 | * as a metadata update */ | |
8273f55e | 2061 | struct intel_super *super = st->sb; |
8273f55e | 2062 | struct dl *d; |
43dad3d6 | 2063 | int rv; |
8273f55e | 2064 | |
43dad3d6 DW |
2065 | /* determine if we are creating a volume or adding a disk */ |
2066 | if (super->current_vol < 0) { | |
2067 | /* in the add disk case we are running in mdmon | |
2068 | * context, so don't close fd's | |
2069 | */ | |
7801ac20 | 2070 | return _add_disk(st); |
43dad3d6 DW |
2071 | } else |
2072 | rv = create_array(st); | |
8273f55e DW |
2073 | |
2074 | for (d = super->disks; d ; d = d->next) { | |
2075 | close(d->fd); | |
2076 | d->fd = -1; | |
2077 | } | |
2078 | ||
43dad3d6 | 2079 | return rv; |
8273f55e DW |
2080 | } else |
2081 | return write_super_imsm(st->sb, 1); | |
cdddbdbc | 2082 | } |
0e600426 | 2083 | #endif |
cdddbdbc DW |
2084 | |
2085 | static int store_zero_imsm(struct supertype *st, int fd) | |
2086 | { | |
551c80c1 | 2087 | unsigned long long dsize; |
6416d527 | 2088 | void *buf; |
551c80c1 DW |
2089 | |
2090 | get_dev_size(fd, NULL, &dsize); | |
2091 | ||
2092 | /* first block is stored on second to last sector of the disk */ | |
2093 | if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0) | |
2094 | return 1; | |
2095 | ||
ad97895e DW |
2096 | if (posix_memalign(&buf, 512, 512) != 0) |
2097 | return 1; | |
2098 | ||
eb7ea463 DW |
2099 | memset(buf, 0, 512); |
2100 | if (write(fd, buf, 512) != 512) | |
551c80c1 | 2101 | return 1; |
cdddbdbc DW |
2102 | return 0; |
2103 | } | |
2104 | ||
0e600426 N |
2105 | static int imsm_bbm_log_size(struct imsm_super *mpb) |
2106 | { | |
2107 | return __le32_to_cpu(mpb->bbm_log_size); | |
2108 | } | |
2109 | ||
2110 | #ifndef MDASSEMBLE | |
cdddbdbc DW |
2111 | static int validate_geometry_imsm_container(struct supertype *st, int level, |
2112 | int layout, int raiddisks, int chunk, | |
c2c087e6 | 2113 | unsigned long long size, char *dev, |
2c514b71 NB |
2114 | unsigned long long *freesize, |
2115 | int verbose) | |
cdddbdbc | 2116 | { |
c2c087e6 DW |
2117 | int fd; |
2118 | unsigned long long ldsize; | |
cdddbdbc | 2119 | |
c2c087e6 DW |
2120 | if (level != LEVEL_CONTAINER) |
2121 | return 0; | |
2122 | if (!dev) | |
2123 | return 1; | |
2124 | ||
2125 | fd = open(dev, O_RDONLY|O_EXCL, 0); | |
2126 | if (fd < 0) { | |
2c514b71 NB |
2127 | if (verbose) |
2128 | fprintf(stderr, Name ": imsm: Cannot open %s: %s\n", | |
2129 | dev, strerror(errno)); | |
c2c087e6 DW |
2130 | return 0; |
2131 | } | |
2132 | if (!get_dev_size(fd, dev, &ldsize)) { | |
2133 | close(fd); | |
2134 | return 0; | |
2135 | } | |
2136 | close(fd); | |
2137 | ||
2138 | *freesize = avail_size_imsm(st, ldsize >> 9); | |
2139 | ||
2140 | return 1; | |
cdddbdbc DW |
2141 | } |
2142 | ||
c2c087e6 DW |
2143 | /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd |
2144 | * FIX ME add ahci details | |
2145 | */ | |
8b353278 DW |
2146 | static int validate_geometry_imsm_volume(struct supertype *st, int level, |
2147 | int layout, int raiddisks, int chunk, | |
c2c087e6 | 2148 | unsigned long long size, char *dev, |
2c514b71 NB |
2149 | unsigned long long *freesize, |
2150 | int verbose) | |
cdddbdbc | 2151 | { |
c2c087e6 DW |
2152 | struct stat stb; |
2153 | struct intel_super *super = st->sb; | |
2154 | struct dl *dl; | |
2155 | unsigned long long pos = 0; | |
2156 | unsigned long long maxsize; | |
2157 | struct extent *e; | |
2158 | int i; | |
cdddbdbc | 2159 | |
c2c087e6 DW |
2160 | if (level == LEVEL_CONTAINER) |
2161 | return 0; | |
2162 | ||
2163 | if (level == 1 && raiddisks > 2) { | |
2c514b71 NB |
2164 | if (verbose) |
2165 | fprintf(stderr, Name ": imsm does not support more " | |
2166 | "than 2 in a raid1 configuration\n"); | |
c2c087e6 DW |
2167 | return 0; |
2168 | } | |
2169 | ||
2170 | /* We must have the container info already read in. */ | |
2171 | if (!super) | |
2172 | return 0; | |
2173 | ||
2174 | if (!dev) { | |
2175 | /* General test: make sure there is space for | |
2da8544a DW |
2176 | * 'raiddisks' device extents of size 'size' at a given |
2177 | * offset | |
c2c087e6 DW |
2178 | */ |
2179 | unsigned long long minsize = size*2 /* convert to blocks */; | |
2da8544a | 2180 | unsigned long long start_offset = ~0ULL; |
c2c087e6 DW |
2181 | int dcnt = 0; |
2182 | if (minsize == 0) | |
2183 | minsize = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS; | |
2184 | for (dl = super->disks; dl ; dl = dl->next) { | |
2185 | int found = 0; | |
2186 | ||
bf5a934a | 2187 | pos = 0; |
c2c087e6 DW |
2188 | i = 0; |
2189 | e = get_extents(super, dl); | |
2190 | if (!e) continue; | |
2191 | do { | |
2192 | unsigned long long esize; | |
2193 | esize = e[i].start - pos; | |
2194 | if (esize >= minsize) | |
2195 | found = 1; | |
2da8544a DW |
2196 | if (found && start_offset == ~0ULL) { |
2197 | start_offset = pos; | |
2198 | break; | |
2199 | } else if (found && pos != start_offset) { | |
2200 | found = 0; | |
2201 | break; | |
2202 | } | |
c2c087e6 DW |
2203 | pos = e[i].start + e[i].size; |
2204 | i++; | |
2205 | } while (e[i-1].size); | |
2206 | if (found) | |
2207 | dcnt++; | |
2208 | free(e); | |
2209 | } | |
2210 | if (dcnt < raiddisks) { | |
2c514b71 NB |
2211 | if (verbose) |
2212 | fprintf(stderr, Name ": imsm: Not enough " | |
2213 | "devices with space for this array " | |
2214 | "(%d < %d)\n", | |
2215 | dcnt, raiddisks); | |
c2c087e6 DW |
2216 | return 0; |
2217 | } | |
2218 | return 1; | |
2219 | } | |
2220 | /* This device must be a member of the set */ | |
2221 | if (stat(dev, &stb) < 0) | |
2222 | return 0; | |
2223 | if ((S_IFMT & stb.st_mode) != S_IFBLK) | |
2224 | return 0; | |
2225 | for (dl = super->disks ; dl ; dl = dl->next) { | |
2226 | if (dl->major == major(stb.st_rdev) && | |
2227 | dl->minor == minor(stb.st_rdev)) | |
2228 | break; | |
2229 | } | |
2230 | if (!dl) { | |
2c514b71 NB |
2231 | if (verbose) |
2232 | fprintf(stderr, Name ": %s is not in the " | |
2233 | "same imsm set\n", dev); | |
c2c087e6 DW |
2234 | return 0; |
2235 | } | |
2236 | e = get_extents(super, dl); | |
2237 | maxsize = 0; | |
2238 | i = 0; | |
2239 | if (e) do { | |
2240 | unsigned long long esize; | |
2241 | esize = e[i].start - pos; | |
2242 | if (esize >= maxsize) | |
2243 | maxsize = esize; | |
2244 | pos = e[i].start + e[i].size; | |
2245 | i++; | |
2246 | } while (e[i-1].size); | |
2247 | *freesize = maxsize; | |
2248 | ||
2249 | return 1; | |
cdddbdbc DW |
2250 | } |
2251 | ||
bf5a934a DW |
2252 | static int validate_geometry_imsm(struct supertype *st, int level, int layout, |
2253 | int raiddisks, int chunk, unsigned long long size, | |
2254 | char *dev, unsigned long long *freesize, | |
2255 | int verbose) | |
2256 | { | |
2257 | int fd, cfd; | |
2258 | struct mdinfo *sra; | |
2259 | ||
2260 | /* if given unused devices create a container | |
2261 | * if given given devices in a container create a member volume | |
2262 | */ | |
2263 | if (level == LEVEL_CONTAINER) { | |
2264 | /* Must be a fresh device to add to a container */ | |
2265 | return validate_geometry_imsm_container(st, level, layout, | |
2266 | raiddisks, chunk, size, | |
2267 | dev, freesize, | |
2268 | verbose); | |
2269 | } | |
2270 | ||
2271 | if (st->sb) { | |
2272 | /* creating in a given container */ | |
2273 | return validate_geometry_imsm_volume(st, level, layout, | |
2274 | raiddisks, chunk, size, | |
2275 | dev, freesize, verbose); | |
2276 | } | |
2277 | ||
2278 | /* limit creation to the following levels */ | |
2279 | if (!dev) | |
2280 | switch (level) { | |
2281 | case 0: | |
2282 | case 1: | |
2283 | case 10: | |
2284 | case 5: | |
2285 | break; | |
2286 | default: | |
2287 | return 1; | |
2288 | } | |
2289 | ||
2290 | /* This device needs to be a device in an 'imsm' container */ | |
2291 | fd = open(dev, O_RDONLY|O_EXCL, 0); | |
2292 | if (fd >= 0) { | |
2293 | if (verbose) | |
2294 | fprintf(stderr, | |
2295 | Name ": Cannot create this array on device %s\n", | |
2296 | dev); | |
2297 | close(fd); | |
2298 | return 0; | |
2299 | } | |
2300 | if (errno != EBUSY || (fd = open(dev, O_RDONLY, 0)) < 0) { | |
2301 | if (verbose) | |
2302 | fprintf(stderr, Name ": Cannot open %s: %s\n", | |
2303 | dev, strerror(errno)); | |
2304 | return 0; | |
2305 | } | |
2306 | /* Well, it is in use by someone, maybe an 'imsm' container. */ | |
2307 | cfd = open_container(fd); | |
2308 | if (cfd < 0) { | |
2309 | close(fd); | |
2310 | if (verbose) | |
2311 | fprintf(stderr, Name ": Cannot use %s: It is busy\n", | |
2312 | dev); | |
2313 | return 0; | |
2314 | } | |
2315 | sra = sysfs_read(cfd, 0, GET_VERSION); | |
2316 | close(fd); | |
2317 | if (sra && sra->array.major_version == -1 && | |
2318 | strcmp(sra->text_version, "imsm") == 0) { | |
2319 | /* This is a member of a imsm container. Load the container | |
2320 | * and try to create a volume | |
2321 | */ | |
2322 | struct intel_super *super; | |
2323 | ||
2324 | if (load_super_imsm_all(st, cfd, (void **) &super, NULL, 1) == 0) { | |
2325 | st->sb = super; | |
2326 | st->container_dev = fd2devnum(cfd); | |
2327 | close(cfd); | |
2328 | return validate_geometry_imsm_volume(st, level, layout, | |
2329 | raiddisks, chunk, | |
2330 | size, dev, | |
2331 | freesize, verbose); | |
2332 | } | |
2333 | close(cfd); | |
2334 | } else /* may belong to another container */ | |
2335 | return 0; | |
2336 | ||
2337 | return 1; | |
2338 | } | |
0e600426 | 2339 | #endif /* MDASSEMBLE */ |
bf5a934a | 2340 | |
cdddbdbc DW |
2341 | static struct mdinfo *container_content_imsm(struct supertype *st) |
2342 | { | |
4f5bc454 DW |
2343 | /* Given a container loaded by load_super_imsm_all, |
2344 | * extract information about all the arrays into | |
2345 | * an mdinfo tree. | |
2346 | * | |
2347 | * For each imsm_dev create an mdinfo, fill it in, | |
2348 | * then look for matching devices in super->disks | |
2349 | * and create appropriate device mdinfo. | |
2350 | */ | |
2351 | struct intel_super *super = st->sb; | |
949c47a0 | 2352 | struct imsm_super *mpb = super->anchor; |
4f5bc454 DW |
2353 | struct mdinfo *rest = NULL; |
2354 | int i; | |
cdddbdbc | 2355 | |
604b746f JD |
2356 | /* do not assemble arrays that might have bad blocks */ |
2357 | if (imsm_bbm_log_size(super->anchor)) { | |
2358 | fprintf(stderr, Name ": BBM log found in metadata. " | |
2359 | "Cannot activate array(s).\n"); | |
2360 | return NULL; | |
2361 | } | |
2362 | ||
4f5bc454 | 2363 | for (i = 0; i < mpb->num_raid_devs; i++) { |
949c47a0 | 2364 | struct imsm_dev *dev = get_imsm_dev(super, i); |
a965f303 | 2365 | struct imsm_map *map = get_imsm_map(dev, 0); |
4f5bc454 | 2366 | struct mdinfo *this; |
4f5bc454 DW |
2367 | int slot; |
2368 | ||
2369 | this = malloc(sizeof(*this)); | |
2370 | memset(this, 0, sizeof(*this)); | |
2371 | this->next = rest; | |
4f5bc454 | 2372 | |
301406c9 DW |
2373 | super->current_vol = i; |
2374 | getinfo_super_imsm_volume(st, this); | |
4f5bc454 | 2375 | for (slot = 0 ; slot < map->num_members; slot++) { |
4f5bc454 DW |
2376 | struct mdinfo *info_d; |
2377 | struct dl *d; | |
2378 | int idx; | |
9a1608e5 | 2379 | int skip; |
4f5bc454 | 2380 | __u32 s; |
7eef0453 | 2381 | __u32 ord; |
4f5bc454 | 2382 | |
9a1608e5 | 2383 | skip = 0; |
ff077194 | 2384 | idx = get_imsm_disk_idx(dev, slot); |
7eef0453 | 2385 | ord = get_imsm_ord_tbl_ent(dev, slot); |
4f5bc454 DW |
2386 | for (d = super->disks; d ; d = d->next) |
2387 | if (d->index == idx) | |
2388 | break; | |
2389 | ||
2390 | if (d == NULL) | |
9a1608e5 DW |
2391 | skip = 1; |
2392 | ||
2393 | s = d ? __le32_to_cpu(d->disk.status) : 0; | |
2394 | if (s & FAILED_DISK) | |
2395 | skip = 1; | |
2396 | if (!(s & USABLE_DISK)) | |
2397 | skip = 1; | |
7eef0453 DW |
2398 | if (ord & IMSM_ORD_REBUILD) |
2399 | skip = 1; | |
9a1608e5 DW |
2400 | |
2401 | /* | |
2402 | * if we skip some disks the array will be assmebled degraded; | |
2403 | * reset resync start to avoid a dirty-degraded situation | |
2404 | * | |
2405 | * FIXME handle dirty degraded | |
2406 | */ | |
2407 | if (skip && !dev->vol.dirty) | |
2408 | this->resync_start = ~0ULL; | |
2409 | if (skip) | |
2410 | continue; | |
4f5bc454 DW |
2411 | |
2412 | info_d = malloc(sizeof(*info_d)); | |
9a1608e5 DW |
2413 | if (!info_d) { |
2414 | fprintf(stderr, Name ": failed to allocate disk" | |
2415 | " for volume %s\n", (char *) dev->volume); | |
2416 | free(this); | |
2417 | this = rest; | |
2418 | break; | |
2419 | } | |
4f5bc454 DW |
2420 | memset(info_d, 0, sizeof(*info_d)); |
2421 | info_d->next = this->devs; | |
2422 | this->devs = info_d; | |
2423 | ||
4f5bc454 DW |
2424 | info_d->disk.number = d->index; |
2425 | info_d->disk.major = d->major; | |
2426 | info_d->disk.minor = d->minor; | |
2427 | info_d->disk.raid_disk = slot; | |
4f5bc454 DW |
2428 | |
2429 | this->array.working_disks++; | |
2430 | ||
2431 | info_d->events = __le32_to_cpu(mpb->generation_num); | |
2432 | info_d->data_offset = __le32_to_cpu(map->pba_of_lba0); | |
2433 | info_d->component_size = __le32_to_cpu(map->blocks_per_member); | |
2434 | if (d->devname) | |
2435 | strcpy(info_d->name, d->devname); | |
2436 | } | |
9a1608e5 | 2437 | rest = this; |
4f5bc454 DW |
2438 | } |
2439 | ||
2440 | return rest; | |
cdddbdbc DW |
2441 | } |
2442 | ||
845dea95 | 2443 | |
0e600426 | 2444 | #ifndef MDASSEMBLE |
cba0191b NB |
2445 | static int imsm_open_new(struct supertype *c, struct active_array *a, |
2446 | char *inst) | |
845dea95 | 2447 | { |
0372d5a2 | 2448 | struct intel_super *super = c->sb; |
949c47a0 | 2449 | struct imsm_super *mpb = super->anchor; |
0372d5a2 | 2450 | |
949c47a0 | 2451 | if (atoi(inst) >= mpb->num_raid_devs) { |
0372d5a2 DW |
2452 | fprintf(stderr, "%s: subarry index %d, out of range\n", |
2453 | __func__, atoi(inst)); | |
2454 | return -ENODEV; | |
2455 | } | |
2456 | ||
4e6e574a | 2457 | dprintf("imsm: open_new %s\n", inst); |
cba0191b | 2458 | a->info.container_member = atoi(inst); |
845dea95 NB |
2459 | return 0; |
2460 | } | |
2461 | ||
fb49eef2 | 2462 | static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev, int failed) |
c2a1e7da | 2463 | { |
a965f303 | 2464 | struct imsm_map *map = get_imsm_map(dev, 0); |
c2a1e7da DW |
2465 | |
2466 | if (!failed) | |
3393c6af DW |
2467 | return map->map_state == IMSM_T_STATE_UNINITIALIZED ? |
2468 | IMSM_T_STATE_UNINITIALIZED : IMSM_T_STATE_NORMAL; | |
c2a1e7da DW |
2469 | |
2470 | switch (get_imsm_raid_level(map)) { | |
2471 | case 0: | |
2472 | return IMSM_T_STATE_FAILED; | |
2473 | break; | |
2474 | case 1: | |
2475 | if (failed < map->num_members) | |
2476 | return IMSM_T_STATE_DEGRADED; | |
2477 | else | |
2478 | return IMSM_T_STATE_FAILED; | |
2479 | break; | |
2480 | case 10: | |
2481 | { | |
2482 | /** | |
c92a2527 DW |
2483 | * check to see if any mirrors have failed, otherwise we |
2484 | * are degraded. Even numbered slots are mirrored on | |
2485 | * slot+1 | |
c2a1e7da | 2486 | */ |
c2a1e7da | 2487 | int i; |
d9b420a5 N |
2488 | /* gcc -Os complains that this is unused */ |
2489 | int insync = insync; | |
c2a1e7da DW |
2490 | |
2491 | for (i = 0; i < map->num_members; i++) { | |
c92a2527 DW |
2492 | __u32 ord = get_imsm_ord_tbl_ent(dev, i); |
2493 | int idx = ord_to_idx(ord); | |
2494 | struct imsm_disk *disk; | |
c2a1e7da | 2495 | |
c92a2527 DW |
2496 | /* reset the potential in-sync count on even-numbered |
2497 | * slots. num_copies is always 2 for imsm raid10 | |
2498 | */ | |
2499 | if ((i & 1) == 0) | |
2500 | insync = 2; | |
c2a1e7da | 2501 | |
c92a2527 DW |
2502 | disk = get_imsm_disk(super, idx); |
2503 | if (!disk || | |
2504 | __le32_to_cpu(disk->status) & FAILED_DISK || | |
2505 | ord & IMSM_ORD_REBUILD) | |
2506 | insync--; | |
c2a1e7da | 2507 | |
c92a2527 DW |
2508 | /* no in-sync disks left in this mirror the |
2509 | * array has failed | |
2510 | */ | |
2511 | if (insync == 0) | |
2512 | return IMSM_T_STATE_FAILED; | |
c2a1e7da DW |
2513 | } |
2514 | ||
2515 | return IMSM_T_STATE_DEGRADED; | |
2516 | } | |
2517 | case 5: | |
2518 | if (failed < 2) | |
2519 | return IMSM_T_STATE_DEGRADED; | |
2520 | else | |
2521 | return IMSM_T_STATE_FAILED; | |
2522 | break; | |
2523 | default: | |
2524 | break; | |
2525 | } | |
2526 | ||
2527 | return map->map_state; | |
2528 | } | |
2529 | ||
ff077194 | 2530 | static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev) |
c2a1e7da DW |
2531 | { |
2532 | int i; | |
2533 | int failed = 0; | |
2534 | struct imsm_disk *disk; | |
ff077194 | 2535 | struct imsm_map *map = get_imsm_map(dev, 0); |
c2a1e7da DW |
2536 | |
2537 | for (i = 0; i < map->num_members; i++) { | |
b10b37b8 DW |
2538 | __u32 ord = get_imsm_ord_tbl_ent(dev, i); |
2539 | int idx = ord_to_idx(ord); | |
c2a1e7da | 2540 | |
949c47a0 | 2541 | disk = get_imsm_disk(super, idx); |
b10b37b8 DW |
2542 | if (!disk || |
2543 | __le32_to_cpu(disk->status) & FAILED_DISK || | |
2544 | ord & IMSM_ORD_REBUILD) | |
fcb84475 | 2545 | failed++; |
c2a1e7da DW |
2546 | } |
2547 | ||
2548 | return failed; | |
845dea95 NB |
2549 | } |
2550 | ||
0c046afd DW |
2551 | static int is_resyncing(struct imsm_dev *dev) |
2552 | { | |
2553 | struct imsm_map *migr_map; | |
2554 | ||
2555 | if (!dev->vol.migr_state) | |
2556 | return 0; | |
2557 | ||
2558 | if (dev->vol.migr_type == 0) | |
2559 | return 1; | |
2560 | ||
2561 | migr_map = get_imsm_map(dev, 1); | |
2562 | ||
2563 | if (migr_map->map_state == IMSM_T_STATE_NORMAL) | |
2564 | return 1; | |
2565 | else | |
2566 | return 0; | |
2567 | } | |
2568 | ||
2569 | static int is_rebuilding(struct imsm_dev *dev) | |
2570 | { | |
2571 | struct imsm_map *migr_map; | |
2572 | ||
2573 | if (!dev->vol.migr_state) | |
2574 | return 0; | |
2575 | ||
2576 | if (dev->vol.migr_type == 0) | |
2577 | return 0; | |
2578 | ||
2579 | migr_map = get_imsm_map(dev, 1); | |
2580 | ||
2581 | if (migr_map->map_state == IMSM_T_STATE_DEGRADED) | |
2582 | return 1; | |
2583 | else | |
2584 | return 0; | |
2585 | } | |
2586 | ||
47ee5a45 DW |
2587 | static void mark_failure(struct imsm_disk *disk) |
2588 | { | |
2589 | __u32 status = __le32_to_cpu(disk->status); | |
2590 | ||
2591 | if (status & FAILED_DISK) | |
2592 | return; | |
2593 | status |= FAILED_DISK; | |
2594 | disk->status = __cpu_to_le32(status); | |
2595 | disk->scsi_id = __cpu_to_le32(~(__u32)0); | |
2596 | memmove(&disk->serial[0], &disk->serial[1], MAX_RAID_SERIAL_LEN - 1); | |
2597 | } | |
2598 | ||
0c046afd DW |
2599 | /* Handle dirty -> clean transititions and resync. Degraded and rebuild |
2600 | * states are handled in imsm_set_disk() with one exception, when a | |
2601 | * resync is stopped due to a new failure this routine will set the | |
2602 | * 'degraded' state for the array. | |
2603 | */ | |
01f157d7 | 2604 | static int imsm_set_array_state(struct active_array *a, int consistent) |
a862209d DW |
2605 | { |
2606 | int inst = a->info.container_member; | |
2607 | struct intel_super *super = a->container->sb; | |
949c47a0 | 2608 | struct imsm_dev *dev = get_imsm_dev(super, inst); |
a965f303 | 2609 | struct imsm_map *map = get_imsm_map(dev, 0); |
0c046afd DW |
2610 | int failed = imsm_count_failed(super, dev); |
2611 | __u8 map_state = imsm_check_degraded(super, dev, failed); | |
a862209d | 2612 | |
47ee5a45 DW |
2613 | /* before we activate this array handle any missing disks */ |
2614 | if (consistent == 2 && super->missing) { | |
2615 | struct dl *dl; | |
2616 | ||
2617 | dprintf("imsm: mark missing\n"); | |
2618 | end_migration(dev, map_state); | |
2619 | for (dl = super->missing; dl; dl = dl->next) | |
2620 | mark_failure(&dl->disk); | |
2621 | super->updates_pending++; | |
2622 | } | |
2623 | ||
0c046afd | 2624 | if (consistent == 2 && |
593add1b | 2625 | (!is_resync_complete(a) || |
0c046afd DW |
2626 | map_state != IMSM_T_STATE_NORMAL || |
2627 | dev->vol.migr_state)) | |
01f157d7 | 2628 | consistent = 0; |
272906ef | 2629 | |
593add1b | 2630 | if (is_resync_complete(a)) { |
0c046afd DW |
2631 | /* complete intialization / resync, |
2632 | * recovery is completed in ->set_disk | |
2633 | */ | |
2634 | if (is_resyncing(dev)) { | |
2635 | dprintf("imsm: mark resync done\n"); | |
f8f603f1 | 2636 | end_migration(dev, map_state); |
115c3803 | 2637 | super->updates_pending++; |
115c3803 | 2638 | } |
0c046afd DW |
2639 | } else if (!is_resyncing(dev) && !failed) { |
2640 | /* mark the start of the init process if nothing is failed */ | |
2641 | dprintf("imsm: mark resync start (%llu)\n", a->resync_start); | |
2642 | map->map_state = map_state; | |
2643 | migrate(dev, IMSM_T_STATE_NORMAL, | |
2644 | map->map_state == IMSM_T_STATE_NORMAL); | |
3393c6af | 2645 | super->updates_pending++; |
115c3803 | 2646 | } |
a862209d | 2647 | |
f8f603f1 DW |
2648 | /* check if we can update the migration checkpoint */ |
2649 | if (dev->vol.migr_state && | |
2650 | __le32_to_cpu(dev->vol.curr_migr_unit) != a->resync_start) { | |
2651 | dprintf("imsm: checkpoint migration (%llu)\n", a->resync_start); | |
2652 | dev->vol.curr_migr_unit = __cpu_to_le32(a->resync_start); | |
2653 | super->updates_pending++; | |
2654 | } | |
2655 | ||
3393c6af | 2656 | /* mark dirty / clean */ |
0c046afd | 2657 | if (dev->vol.dirty != !consistent) { |
3393c6af | 2658 | dprintf("imsm: mark '%s' (%llu)\n", |
0c046afd DW |
2659 | consistent ? "clean" : "dirty", a->resync_start); |
2660 | if (consistent) | |
2661 | dev->vol.dirty = 0; | |
2662 | else | |
2663 | dev->vol.dirty = 1; | |
a862209d DW |
2664 | super->updates_pending++; |
2665 | } | |
01f157d7 | 2666 | return consistent; |
a862209d DW |
2667 | } |
2668 | ||
8d45d196 | 2669 | static void imsm_set_disk(struct active_array *a, int n, int state) |
845dea95 | 2670 | { |
8d45d196 DW |
2671 | int inst = a->info.container_member; |
2672 | struct intel_super *super = a->container->sb; | |
949c47a0 | 2673 | struct imsm_dev *dev = get_imsm_dev(super, inst); |
a965f303 | 2674 | struct imsm_map *map = get_imsm_map(dev, 0); |
8d45d196 | 2675 | struct imsm_disk *disk; |
0c046afd | 2676 | int failed; |
8d45d196 | 2677 | __u32 status; |
b10b37b8 | 2678 | __u32 ord; |
0c046afd | 2679 | __u8 map_state; |
8d45d196 DW |
2680 | |
2681 | if (n > map->num_members) | |
2682 | fprintf(stderr, "imsm: set_disk %d out of range 0..%d\n", | |
2683 | n, map->num_members - 1); | |
2684 | ||
2685 | if (n < 0) | |
2686 | return; | |
2687 | ||
4e6e574a | 2688 | dprintf("imsm: set_disk %d:%x\n", n, state); |
8d45d196 | 2689 | |
b10b37b8 DW |
2690 | ord = get_imsm_ord_tbl_ent(dev, n); |
2691 | disk = get_imsm_disk(super, ord_to_idx(ord)); | |
8d45d196 | 2692 | |
5802a811 | 2693 | /* check for new failures */ |
8d45d196 DW |
2694 | status = __le32_to_cpu(disk->status); |
2695 | if ((state & DS_FAULTY) && !(status & FAILED_DISK)) { | |
47ee5a45 | 2696 | mark_failure(disk); |
5802a811 | 2697 | super->updates_pending++; |
8d45d196 | 2698 | } |
47ee5a45 | 2699 | |
19859edc | 2700 | /* check if in_sync */ |
b10b37b8 DW |
2701 | if (state & DS_INSYNC && ord & IMSM_ORD_REBUILD) { |
2702 | struct imsm_map *migr_map = get_imsm_map(dev, 1); | |
2703 | ||
2704 | set_imsm_ord_tbl_ent(migr_map, n, ord_to_idx(ord)); | |
19859edc DW |
2705 | super->updates_pending++; |
2706 | } | |
8d45d196 | 2707 | |
0c046afd DW |
2708 | failed = imsm_count_failed(super, dev); |
2709 | map_state = imsm_check_degraded(super, dev, failed); | |
5802a811 | 2710 | |
0c046afd DW |
2711 | /* check if recovery complete, newly degraded, or failed */ |
2712 | if (map_state == IMSM_T_STATE_NORMAL && is_rebuilding(dev)) { | |
f8f603f1 | 2713 | end_migration(dev, map_state); |
0c046afd DW |
2714 | super->updates_pending++; |
2715 | } else if (map_state == IMSM_T_STATE_DEGRADED && | |
2716 | map->map_state != map_state && | |
2717 | !dev->vol.migr_state) { | |
2718 | dprintf("imsm: mark degraded\n"); | |
2719 | map->map_state = map_state; | |
2720 | super->updates_pending++; | |
2721 | } else if (map_state == IMSM_T_STATE_FAILED && | |
2722 | map->map_state != map_state) { | |
2723 | dprintf("imsm: mark failed\n"); | |
f8f603f1 | 2724 | end_migration(dev, map_state); |
0c046afd | 2725 | super->updates_pending++; |
5802a811 | 2726 | } |
845dea95 NB |
2727 | } |
2728 | ||
c2a1e7da DW |
2729 | static int store_imsm_mpb(int fd, struct intel_super *super) |
2730 | { | |
949c47a0 | 2731 | struct imsm_super *mpb = super->anchor; |
c2a1e7da DW |
2732 | __u32 mpb_size = __le32_to_cpu(mpb->mpb_size); |
2733 | unsigned long long dsize; | |
2734 | unsigned long long sectors; | |
2735 | ||
2736 | get_dev_size(fd, NULL, &dsize); | |
2737 | ||
272f648f DW |
2738 | if (mpb_size > 512) { |
2739 | /* -1 to account for anchor */ | |
2740 | sectors = mpb_sectors(mpb) - 1; | |
c2a1e7da | 2741 | |
272f648f DW |
2742 | /* write the extended mpb to the sectors preceeding the anchor */ |
2743 | if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0) | |
2744 | return 1; | |
c2a1e7da | 2745 | |
99e29264 | 2746 | if (write(fd, super->buf + 512, 512 * sectors) != 512 * sectors) |
272f648f DW |
2747 | return 1; |
2748 | } | |
c2a1e7da | 2749 | |
272f648f DW |
2750 | /* first block is stored on second to last sector of the disk */ |
2751 | if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0) | |
c2a1e7da DW |
2752 | return 1; |
2753 | ||
272f648f | 2754 | if (write(fd, super->buf, 512) != 512) |
c2a1e7da DW |
2755 | return 1; |
2756 | ||
c2a1e7da DW |
2757 | return 0; |
2758 | } | |
2759 | ||
2e735d19 | 2760 | static void imsm_sync_metadata(struct supertype *container) |
845dea95 | 2761 | { |
2e735d19 | 2762 | struct intel_super *super = container->sb; |
c2a1e7da DW |
2763 | |
2764 | if (!super->updates_pending) | |
2765 | return; | |
2766 | ||
c2c087e6 | 2767 | write_super_imsm(super, 0); |
c2a1e7da DW |
2768 | |
2769 | super->updates_pending = 0; | |
845dea95 NB |
2770 | } |
2771 | ||
272906ef DW |
2772 | static struct dl *imsm_readd(struct intel_super *super, int idx, struct active_array *a) |
2773 | { | |
2774 | struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member); | |
ff077194 | 2775 | int i = get_imsm_disk_idx(dev, idx); |
272906ef DW |
2776 | struct dl *dl; |
2777 | ||
2778 | for (dl = super->disks; dl; dl = dl->next) | |
2779 | if (dl->index == i) | |
2780 | break; | |
2781 | ||
8796fdc4 | 2782 | if (dl && __le32_to_cpu(dl->disk.status) & FAILED_DISK) |
272906ef DW |
2783 | dl = NULL; |
2784 | ||
2785 | if (dl) | |
2786 | dprintf("%s: found %x:%x\n", __func__, dl->major, dl->minor); | |
2787 | ||
2788 | return dl; | |
2789 | } | |
2790 | ||
e553d2a4 | 2791 | static struct dl *imsm_add_spare(struct intel_super *super, int slot, struct active_array *a) |
272906ef DW |
2792 | { |
2793 | struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member); | |
e553d2a4 | 2794 | int idx = get_imsm_disk_idx(dev, slot); |
272906ef DW |
2795 | struct imsm_map *map = get_imsm_map(dev, 0); |
2796 | unsigned long long esize; | |
2797 | unsigned long long pos; | |
2798 | struct mdinfo *d; | |
2799 | struct extent *ex; | |
2800 | int j; | |
2801 | int found; | |
2802 | __u32 array_start; | |
9a1608e5 | 2803 | __u32 status; |
272906ef DW |
2804 | struct dl *dl; |
2805 | ||
2806 | for (dl = super->disks; dl; dl = dl->next) { | |
2807 | /* If in this array, skip */ | |
2808 | for (d = a->info.devs ; d ; d = d->next) | |
e553d2a4 DW |
2809 | if (d->state_fd >= 0 && |
2810 | d->disk.major == dl->major && | |
272906ef DW |
2811 | d->disk.minor == dl->minor) { |
2812 | dprintf("%x:%x already in array\n", dl->major, dl->minor); | |
2813 | break; | |
2814 | } | |
2815 | if (d) | |
2816 | continue; | |
2817 | ||
e553d2a4 | 2818 | /* skip in use or failed drives */ |
9a1608e5 | 2819 | status = __le32_to_cpu(dl->disk.status); |
e553d2a4 | 2820 | if (status & FAILED_DISK || idx == dl->index) { |
9a1608e5 DW |
2821 | dprintf("%x:%x status ( %s%s)\n", |
2822 | dl->major, dl->minor, | |
2823 | status & FAILED_DISK ? "failed " : "", | |
e553d2a4 | 2824 | idx == dl->index ? "in use " : ""); |
9a1608e5 DW |
2825 | continue; |
2826 | } | |
2827 | ||
272906ef DW |
2828 | /* Does this unused device have the requisite free space? |
2829 | * We need a->info.component_size sectors | |
2830 | */ | |
2831 | ex = get_extents(super, dl); | |
2832 | if (!ex) { | |
2833 | dprintf("cannot get extents\n"); | |
2834 | continue; | |
2835 | } | |
2836 | found = 0; | |
2837 | j = 0; | |
2838 | pos = 0; | |
2839 | array_start = __le32_to_cpu(map->pba_of_lba0); | |
2840 | ||
2841 | do { | |
2842 | /* check that we can start at pba_of_lba0 with | |
2843 | * a->info.component_size of space | |
2844 | */ | |
2845 | esize = ex[j].start - pos; | |
2846 | if (array_start >= pos && | |
2847 | array_start + a->info.component_size < ex[j].start) { | |
2848 | found = 1; | |
2849 | break; | |
2850 | } | |
2851 | pos = ex[j].start + ex[j].size; | |
2852 | j++; | |
2853 | ||
2854 | } while (ex[j-1].size); | |
2855 | ||
2856 | free(ex); | |
2857 | if (!found) { | |
2858 | dprintf("%x:%x does not have %llu at %d\n", | |
2859 | dl->major, dl->minor, | |
2860 | a->info.component_size, | |
2861 | __le32_to_cpu(map->pba_of_lba0)); | |
2862 | /* No room */ | |
2863 | continue; | |
2864 | } else | |
2865 | break; | |
2866 | } | |
2867 | ||
2868 | return dl; | |
2869 | } | |
2870 | ||
88758e9d DW |
2871 | static struct mdinfo *imsm_activate_spare(struct active_array *a, |
2872 | struct metadata_update **updates) | |
2873 | { | |
2874 | /** | |
d23fe947 DW |
2875 | * Find a device with unused free space and use it to replace a |
2876 | * failed/vacant region in an array. We replace failed regions one a | |
2877 | * array at a time. The result is that a new spare disk will be added | |
2878 | * to the first failed array and after the monitor has finished | |
2879 | * propagating failures the remainder will be consumed. | |
88758e9d | 2880 | * |
d23fe947 DW |
2881 | * FIXME add a capability for mdmon to request spares from another |
2882 | * container. | |
88758e9d DW |
2883 | */ |
2884 | ||
2885 | struct intel_super *super = a->container->sb; | |
88758e9d | 2886 | int inst = a->info.container_member; |
949c47a0 | 2887 | struct imsm_dev *dev = get_imsm_dev(super, inst); |
a965f303 | 2888 | struct imsm_map *map = get_imsm_map(dev, 0); |
88758e9d DW |
2889 | int failed = a->info.array.raid_disks; |
2890 | struct mdinfo *rv = NULL; | |
2891 | struct mdinfo *d; | |
2892 | struct mdinfo *di; | |
2893 | struct metadata_update *mu; | |
2894 | struct dl *dl; | |
2895 | struct imsm_update_activate_spare *u; | |
2896 | int num_spares = 0; | |
2897 | int i; | |
2898 | ||
2899 | for (d = a->info.devs ; d ; d = d->next) { | |
2900 | if ((d->curr_state & DS_FAULTY) && | |
2901 | d->state_fd >= 0) | |
2902 | /* wait for Removal to happen */ | |
2903 | return NULL; | |
2904 | if (d->state_fd >= 0) | |
2905 | failed--; | |
2906 | } | |
2907 | ||
2908 | dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n", | |
2909 | inst, failed, a->info.array.raid_disks, a->info.array.level); | |
fb49eef2 | 2910 | if (imsm_check_degraded(super, dev, failed) != IMSM_T_STATE_DEGRADED) |
88758e9d DW |
2911 | return NULL; |
2912 | ||
2913 | /* For each slot, if it is not working, find a spare */ | |
88758e9d DW |
2914 | for (i = 0; i < a->info.array.raid_disks; i++) { |
2915 | for (d = a->info.devs ; d ; d = d->next) | |
2916 | if (d->disk.raid_disk == i) | |
2917 | break; | |
2918 | dprintf("found %d: %p %x\n", i, d, d?d->curr_state:0); | |
2919 | if (d && (d->state_fd >= 0)) | |
2920 | continue; | |
2921 | ||
272906ef DW |
2922 | /* |
2923 | * OK, this device needs recovery. Try to re-add the previous | |
2924 | * occupant of this slot, if this fails add a new spare | |
2925 | */ | |
2926 | dl = imsm_readd(super, i, a); | |
2927 | if (!dl) | |
2928 | dl = imsm_add_spare(super, i, a); | |
2929 | if (!dl) | |
2930 | continue; | |
2931 | ||
2932 | /* found a usable disk with enough space */ | |
2933 | di = malloc(sizeof(*di)); | |
79244939 DW |
2934 | if (!di) |
2935 | continue; | |
272906ef DW |
2936 | memset(di, 0, sizeof(*di)); |
2937 | ||
2938 | /* dl->index will be -1 in the case we are activating a | |
2939 | * pristine spare. imsm_process_update() will create a | |
2940 | * new index in this case. Once a disk is found to be | |
2941 | * failed in all member arrays it is kicked from the | |
2942 | * metadata | |
2943 | */ | |
2944 | di->disk.number = dl->index; | |
d23fe947 | 2945 | |
272906ef DW |
2946 | /* (ab)use di->devs to store a pointer to the device |
2947 | * we chose | |
2948 | */ | |
2949 | di->devs = (struct mdinfo *) dl; | |
2950 | ||
2951 | di->disk.raid_disk = i; | |
2952 | di->disk.major = dl->major; | |
2953 | di->disk.minor = dl->minor; | |
2954 | di->disk.state = 0; | |
2955 | di->data_offset = __le32_to_cpu(map->pba_of_lba0); | |
2956 | di->component_size = a->info.component_size; | |
2957 | di->container_member = inst; | |
2958 | di->next = rv; | |
2959 | rv = di; | |
2960 | num_spares++; | |
2961 | dprintf("%x:%x to be %d at %llu\n", dl->major, dl->minor, | |
2962 | i, di->data_offset); | |
88758e9d | 2963 | |
272906ef | 2964 | break; |
88758e9d DW |
2965 | } |
2966 | ||
2967 | if (!rv) | |
2968 | /* No spares found */ | |
2969 | return rv; | |
2970 | /* Now 'rv' has a list of devices to return. | |
2971 | * Create a metadata_update record to update the | |
2972 | * disk_ord_tbl for the array | |
2973 | */ | |
2974 | mu = malloc(sizeof(*mu)); | |
79244939 DW |
2975 | if (mu) { |
2976 | mu->buf = malloc(sizeof(struct imsm_update_activate_spare) * num_spares); | |
2977 | if (mu->buf == NULL) { | |
2978 | free(mu); | |
2979 | mu = NULL; | |
2980 | } | |
2981 | } | |
2982 | if (!mu) { | |
2983 | while (rv) { | |
2984 | struct mdinfo *n = rv->next; | |
2985 | ||
2986 | free(rv); | |
2987 | rv = n; | |
2988 | } | |
2989 | return NULL; | |
2990 | } | |
2991 | ||
88758e9d DW |
2992 | mu->space = NULL; |
2993 | mu->len = sizeof(struct imsm_update_activate_spare) * num_spares; | |
2994 | mu->next = *updates; | |
2995 | u = (struct imsm_update_activate_spare *) mu->buf; | |
2996 | ||
2997 | for (di = rv ; di ; di = di->next) { | |
2998 | u->type = update_activate_spare; | |
d23fe947 DW |
2999 | u->dl = (struct dl *) di->devs; |
3000 | di->devs = NULL; | |
88758e9d DW |
3001 | u->slot = di->disk.raid_disk; |
3002 | u->array = inst; | |
3003 | u->next = u + 1; | |
3004 | u++; | |
3005 | } | |
3006 | (u-1)->next = NULL; | |
3007 | *updates = mu; | |
3008 | ||
3009 | return rv; | |
3010 | } | |
3011 | ||
ff077194 | 3012 | static int disks_overlap(struct imsm_dev *d1, struct imsm_dev *d2) |
8273f55e | 3013 | { |
ff077194 DW |
3014 | struct imsm_map *m1 = get_imsm_map(d1, 0); |
3015 | struct imsm_map *m2 = get_imsm_map(d2, 0); | |
8273f55e DW |
3016 | int i; |
3017 | int j; | |
3018 | int idx; | |
3019 | ||
3020 | for (i = 0; i < m1->num_members; i++) { | |
ff077194 | 3021 | idx = get_imsm_disk_idx(d1, i); |
8273f55e | 3022 | for (j = 0; j < m2->num_members; j++) |
ff077194 | 3023 | if (idx == get_imsm_disk_idx(d2, j)) |
8273f55e DW |
3024 | return 1; |
3025 | } | |
3026 | ||
3027 | return 0; | |
3028 | } | |
3029 | ||
24565c9a | 3030 | static void imsm_delete(struct intel_super *super, struct dl **dlp, int index); |
ae6aad82 | 3031 | |
e8319a19 DW |
3032 | static void imsm_process_update(struct supertype *st, |
3033 | struct metadata_update *update) | |
3034 | { | |
3035 | /** | |
3036 | * crack open the metadata_update envelope to find the update record | |
3037 | * update can be one of: | |
3038 | * update_activate_spare - a spare device has replaced a failed | |
3039 | * device in an array, update the disk_ord_tbl. If this disk is | |
3040 | * present in all member arrays then also clear the SPARE_DISK | |
3041 | * flag | |
3042 | */ | |
3043 | struct intel_super *super = st->sb; | |
4d7b1503 | 3044 | struct imsm_super *mpb; |
e8319a19 DW |
3045 | enum imsm_update_type type = *(enum imsm_update_type *) update->buf; |
3046 | ||
4d7b1503 DW |
3047 | /* update requires a larger buf but the allocation failed */ |
3048 | if (super->next_len && !super->next_buf) { | |
3049 | super->next_len = 0; | |
3050 | return; | |
3051 | } | |
3052 | ||
3053 | if (super->next_buf) { | |
3054 | memcpy(super->next_buf, super->buf, super->len); | |
3055 | free(super->buf); | |
3056 | super->len = super->next_len; | |
3057 | super->buf = super->next_buf; | |
3058 | ||
3059 | super->next_len = 0; | |
3060 | super->next_buf = NULL; | |
3061 | } | |
3062 | ||
3063 | mpb = super->anchor; | |
3064 | ||
e8319a19 DW |
3065 | switch (type) { |
3066 | case update_activate_spare: { | |
3067 | struct imsm_update_activate_spare *u = (void *) update->buf; | |
949c47a0 | 3068 | struct imsm_dev *dev = get_imsm_dev(super, u->array); |
a965f303 | 3069 | struct imsm_map *map = get_imsm_map(dev, 0); |
0c046afd | 3070 | struct imsm_map *migr_map; |
e8319a19 DW |
3071 | struct active_array *a; |
3072 | struct imsm_disk *disk; | |
3073 | __u32 status; | |
0c046afd | 3074 | __u8 to_state; |
e8319a19 | 3075 | struct dl *dl; |
e8319a19 | 3076 | unsigned int found; |
0c046afd DW |
3077 | int failed; |
3078 | int victim = get_imsm_disk_idx(dev, u->slot); | |
e8319a19 DW |
3079 | int i; |
3080 | ||
3081 | for (dl = super->disks; dl; dl = dl->next) | |
d23fe947 | 3082 | if (dl == u->dl) |
e8319a19 DW |
3083 | break; |
3084 | ||
3085 | if (!dl) { | |
3086 | fprintf(stderr, "error: imsm_activate_spare passed " | |
1f24f035 DW |
3087 | "an unknown disk (index: %d)\n", |
3088 | u->dl->index); | |
e8319a19 DW |
3089 | return; |
3090 | } | |
3091 | ||
3092 | super->updates_pending++; | |
3093 | ||
0c046afd DW |
3094 | /* count failures (excluding rebuilds and the victim) |
3095 | * to determine map[0] state | |
3096 | */ | |
3097 | failed = 0; | |
3098 | for (i = 0; i < map->num_members; i++) { | |
3099 | if (i == u->slot) | |
3100 | continue; | |
3101 | disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i)); | |
3102 | if (!disk || | |
3103 | __le32_to_cpu(disk->status) & FAILED_DISK) | |
3104 | failed++; | |
3105 | } | |
3106 | ||
d23fe947 DW |
3107 | /* adding a pristine spare, assign a new index */ |
3108 | if (dl->index < 0) { | |
3109 | dl->index = super->anchor->num_disks; | |
3110 | super->anchor->num_disks++; | |
3111 | } | |
d23fe947 | 3112 | disk = &dl->disk; |
e8319a19 DW |
3113 | status = __le32_to_cpu(disk->status); |
3114 | status |= CONFIGURED_DISK; | |
b10b37b8 | 3115 | status &= ~SPARE_DISK; |
e8319a19 DW |
3116 | disk->status = __cpu_to_le32(status); |
3117 | ||
0c046afd DW |
3118 | /* mark rebuild */ |
3119 | to_state = imsm_check_degraded(super, dev, failed); | |
3120 | map->map_state = IMSM_T_STATE_DEGRADED; | |
3121 | migrate(dev, to_state, 1); | |
3122 | migr_map = get_imsm_map(dev, 1); | |
3123 | set_imsm_ord_tbl_ent(map, u->slot, dl->index); | |
3124 | set_imsm_ord_tbl_ent(migr_map, u->slot, dl->index | IMSM_ORD_REBUILD); | |
3125 | ||
e8319a19 DW |
3126 | /* count arrays using the victim in the metadata */ |
3127 | found = 0; | |
3128 | for (a = st->arrays; a ; a = a->next) { | |
949c47a0 | 3129 | dev = get_imsm_dev(super, a->info.container_member); |
e8319a19 | 3130 | for (i = 0; i < map->num_members; i++) |
ff077194 | 3131 | if (victim == get_imsm_disk_idx(dev, i)) |
e8319a19 DW |
3132 | found++; |
3133 | } | |
3134 | ||
24565c9a | 3135 | /* delete the victim if it is no longer being |
e8319a19 DW |
3136 | * utilized anywhere |
3137 | */ | |
e8319a19 | 3138 | if (!found) { |
ae6aad82 | 3139 | struct dl **dlp; |
24565c9a | 3140 | |
47ee5a45 DW |
3141 | /* We know that 'manager' isn't touching anything, |
3142 | * so it is safe to delete | |
3143 | */ | |
24565c9a | 3144 | for (dlp = &super->disks; *dlp; dlp = &(*dlp)->next) |
ae6aad82 DW |
3145 | if ((*dlp)->index == victim) |
3146 | break; | |
47ee5a45 DW |
3147 | |
3148 | /* victim may be on the missing list */ | |
3149 | if (!*dlp) | |
3150 | for (dlp = &super->missing; *dlp; dlp = &(*dlp)->next) | |
3151 | if ((*dlp)->index == victim) | |
3152 | break; | |
24565c9a | 3153 | imsm_delete(super, dlp, victim); |
e8319a19 | 3154 | } |
8273f55e DW |
3155 | break; |
3156 | } | |
3157 | case update_create_array: { | |
3158 | /* someone wants to create a new array, we need to be aware of | |
3159 | * a few races/collisions: | |
3160 | * 1/ 'Create' called by two separate instances of mdadm | |
3161 | * 2/ 'Create' versus 'activate_spare': mdadm has chosen | |
3162 | * devices that have since been assimilated via | |
3163 | * activate_spare. | |
3164 | * In the event this update can not be carried out mdadm will | |
3165 | * (FIX ME) notice that its update did not take hold. | |
3166 | */ | |
3167 | struct imsm_update_create_array *u = (void *) update->buf; | |
3168 | struct imsm_dev *dev; | |
3169 | struct imsm_map *map, *new_map; | |
3170 | unsigned long long start, end; | |
3171 | unsigned long long new_start, new_end; | |
3172 | int i; | |
3173 | int overlap = 0; | |
3174 | ||
3175 | /* handle racing creates: first come first serve */ | |
3176 | if (u->dev_idx < mpb->num_raid_devs) { | |
3177 | dprintf("%s: subarray %d already defined\n", | |
3178 | __func__, u->dev_idx); | |
3179 | return; | |
3180 | } | |
3181 | ||
3182 | /* check update is next in sequence */ | |
3183 | if (u->dev_idx != mpb->num_raid_devs) { | |
6a3e913e DW |
3184 | dprintf("%s: can not create array %d expected index %d\n", |
3185 | __func__, u->dev_idx, mpb->num_raid_devs); | |
8273f55e DW |
3186 | return; |
3187 | } | |
3188 | ||
a965f303 | 3189 | new_map = get_imsm_map(&u->dev, 0); |
8273f55e DW |
3190 | new_start = __le32_to_cpu(new_map->pba_of_lba0); |
3191 | new_end = new_start + __le32_to_cpu(new_map->blocks_per_member); | |
3192 | ||
3193 | /* handle activate_spare versus create race: | |
3194 | * check to make sure that overlapping arrays do not include | |
3195 | * overalpping disks | |
3196 | */ | |
3197 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
949c47a0 | 3198 | dev = get_imsm_dev(super, i); |
a965f303 | 3199 | map = get_imsm_map(dev, 0); |
8273f55e DW |
3200 | start = __le32_to_cpu(map->pba_of_lba0); |
3201 | end = start + __le32_to_cpu(map->blocks_per_member); | |
3202 | if ((new_start >= start && new_start <= end) || | |
3203 | (start >= new_start && start <= new_end)) | |
3204 | overlap = 1; | |
ff077194 | 3205 | if (overlap && disks_overlap(dev, &u->dev)) { |
8273f55e DW |
3206 | dprintf("%s: arrays overlap\n", __func__); |
3207 | return; | |
3208 | } | |
3209 | } | |
3210 | /* check num_members sanity */ | |
3211 | if (new_map->num_members > mpb->num_disks) { | |
3212 | dprintf("%s: num_disks out of range\n", __func__); | |
3213 | return; | |
3214 | } | |
3215 | ||
949c47a0 DW |
3216 | /* check that prepare update was successful */ |
3217 | if (!update->space) { | |
3218 | dprintf("%s: prepare update failed\n", __func__); | |
3219 | return; | |
3220 | } | |
3221 | ||
8273f55e | 3222 | super->updates_pending++; |
949c47a0 | 3223 | dev = update->space; |
ff077194 | 3224 | map = get_imsm_map(dev, 0); |
949c47a0 DW |
3225 | update->space = NULL; |
3226 | imsm_copy_dev(dev, &u->dev); | |
e0783b41 | 3227 | map = get_imsm_map(dev, 0); |
949c47a0 | 3228 | super->dev_tbl[u->dev_idx] = dev; |
8273f55e | 3229 | mpb->num_raid_devs++; |
8273f55e | 3230 | |
e0783b41 | 3231 | /* fix up flags */ |
8273f55e DW |
3232 | for (i = 0; i < map->num_members; i++) { |
3233 | struct imsm_disk *disk; | |
3234 | __u32 status; | |
3235 | ||
ff077194 | 3236 | disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i)); |
8273f55e DW |
3237 | status = __le32_to_cpu(disk->status); |
3238 | status |= CONFIGURED_DISK; | |
e0783b41 | 3239 | status &= ~SPARE_DISK; |
8273f55e DW |
3240 | disk->status = __cpu_to_le32(status); |
3241 | } | |
3242 | break; | |
e8319a19 | 3243 | } |
43dad3d6 DW |
3244 | case update_add_disk: |
3245 | ||
3246 | /* we may be able to repair some arrays if disks are | |
3247 | * being added */ | |
3248 | if (super->add) { | |
3249 | struct active_array *a; | |
072b727f DW |
3250 | |
3251 | super->updates_pending++; | |
43dad3d6 DW |
3252 | for (a = st->arrays; a; a = a->next) |
3253 | a->check_degraded = 1; | |
3254 | } | |
e553d2a4 | 3255 | /* add some spares to the metadata */ |
43dad3d6 | 3256 | while (super->add) { |
e553d2a4 DW |
3257 | struct dl *al; |
3258 | ||
43dad3d6 DW |
3259 | al = super->add; |
3260 | super->add = al->next; | |
43dad3d6 DW |
3261 | al->next = super->disks; |
3262 | super->disks = al; | |
e553d2a4 DW |
3263 | dprintf("%s: added %x:%x\n", |
3264 | __func__, al->major, al->minor); | |
43dad3d6 DW |
3265 | } |
3266 | ||
3267 | break; | |
e8319a19 DW |
3268 | } |
3269 | } | |
88758e9d | 3270 | |
8273f55e DW |
3271 | static void imsm_prepare_update(struct supertype *st, |
3272 | struct metadata_update *update) | |
3273 | { | |
949c47a0 | 3274 | /** |
4d7b1503 DW |
3275 | * Allocate space to hold new disk entries, raid-device entries or a new |
3276 | * mpb if necessary. The manager synchronously waits for updates to | |
3277 | * complete in the monitor, so new mpb buffers allocated here can be | |
3278 | * integrated by the monitor thread without worrying about live pointers | |
3279 | * in the manager thread. | |
8273f55e | 3280 | */ |
949c47a0 | 3281 | enum imsm_update_type type = *(enum imsm_update_type *) update->buf; |
4d7b1503 DW |
3282 | struct intel_super *super = st->sb; |
3283 | struct imsm_super *mpb = super->anchor; | |
3284 | size_t buf_len; | |
3285 | size_t len = 0; | |
949c47a0 DW |
3286 | |
3287 | switch (type) { | |
3288 | case update_create_array: { | |
3289 | struct imsm_update_create_array *u = (void *) update->buf; | |
949c47a0 | 3290 | |
4d7b1503 | 3291 | len = sizeof_imsm_dev(&u->dev, 1); |
949c47a0 DW |
3292 | update->space = malloc(len); |
3293 | break; | |
3294 | default: | |
3295 | break; | |
3296 | } | |
3297 | } | |
8273f55e | 3298 | |
4d7b1503 DW |
3299 | /* check if we need a larger metadata buffer */ |
3300 | if (super->next_buf) | |
3301 | buf_len = super->next_len; | |
3302 | else | |
3303 | buf_len = super->len; | |
3304 | ||
3305 | if (__le32_to_cpu(mpb->mpb_size) + len > buf_len) { | |
3306 | /* ok we need a larger buf than what is currently allocated | |
3307 | * if this allocation fails process_update will notice that | |
3308 | * ->next_len is set and ->next_buf is NULL | |
3309 | */ | |
3310 | buf_len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + len, 512); | |
3311 | if (super->next_buf) | |
3312 | free(super->next_buf); | |
3313 | ||
3314 | super->next_len = buf_len; | |
3315 | if (posix_memalign(&super->next_buf, buf_len, 512) != 0) | |
3316 | super->next_buf = NULL; | |
3317 | } | |
8273f55e DW |
3318 | } |
3319 | ||
ae6aad82 | 3320 | /* must be called while manager is quiesced */ |
24565c9a | 3321 | static void imsm_delete(struct intel_super *super, struct dl **dlp, int index) |
ae6aad82 DW |
3322 | { |
3323 | struct imsm_super *mpb = super->anchor; | |
ae6aad82 DW |
3324 | struct dl *iter; |
3325 | struct imsm_dev *dev; | |
3326 | struct imsm_map *map; | |
24565c9a DW |
3327 | int i, j, num_members; |
3328 | __u32 ord; | |
ae6aad82 | 3329 | |
24565c9a DW |
3330 | dprintf("%s: deleting device[%d] from imsm_super\n", |
3331 | __func__, index); | |
ae6aad82 DW |
3332 | |
3333 | /* shift all indexes down one */ | |
3334 | for (iter = super->disks; iter; iter = iter->next) | |
24565c9a | 3335 | if (iter->index > index) |
ae6aad82 | 3336 | iter->index--; |
47ee5a45 DW |
3337 | for (iter = super->missing; iter; iter = iter->next) |
3338 | if (iter->index > index) | |
3339 | iter->index--; | |
ae6aad82 DW |
3340 | |
3341 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
3342 | dev = get_imsm_dev(super, i); | |
3343 | map = get_imsm_map(dev, 0); | |
24565c9a DW |
3344 | num_members = map->num_members; |
3345 | for (j = 0; j < num_members; j++) { | |
3346 | /* update ord entries being careful not to propagate | |
3347 | * ord-flags to the first map | |
3348 | */ | |
3349 | ord = get_imsm_ord_tbl_ent(dev, j); | |
ae6aad82 | 3350 | |
24565c9a DW |
3351 | if (ord_to_idx(ord) <= index) |
3352 | continue; | |
ae6aad82 | 3353 | |
24565c9a DW |
3354 | map = get_imsm_map(dev, 0); |
3355 | set_imsm_ord_tbl_ent(map, j, ord_to_idx(ord - 1)); | |
3356 | map = get_imsm_map(dev, 1); | |
3357 | if (map) | |
3358 | set_imsm_ord_tbl_ent(map, j, ord - 1); | |
ae6aad82 DW |
3359 | } |
3360 | } | |
3361 | ||
3362 | mpb->num_disks--; | |
3363 | super->updates_pending++; | |
24565c9a DW |
3364 | if (*dlp) { |
3365 | struct dl *dl = *dlp; | |
3366 | ||
3367 | *dlp = (*dlp)->next; | |
3368 | __free_imsm_disk(dl); | |
3369 | } | |
ae6aad82 | 3370 | } |
0e600426 | 3371 | #endif /* MDASSEMBLE */ |
ae6aad82 | 3372 | |
cdddbdbc DW |
3373 | struct superswitch super_imsm = { |
3374 | #ifndef MDASSEMBLE | |
3375 | .examine_super = examine_super_imsm, | |
3376 | .brief_examine_super = brief_examine_super_imsm, | |
3377 | .detail_super = detail_super_imsm, | |
3378 | .brief_detail_super = brief_detail_super_imsm, | |
bf5a934a | 3379 | .write_init_super = write_init_super_imsm, |
0e600426 N |
3380 | .validate_geometry = validate_geometry_imsm, |
3381 | .add_to_super = add_to_super_imsm, | |
cdddbdbc DW |
3382 | #endif |
3383 | .match_home = match_home_imsm, | |
3384 | .uuid_from_super= uuid_from_super_imsm, | |
3385 | .getinfo_super = getinfo_super_imsm, | |
3386 | .update_super = update_super_imsm, | |
3387 | ||
3388 | .avail_size = avail_size_imsm, | |
3389 | ||
3390 | .compare_super = compare_super_imsm, | |
3391 | ||
3392 | .load_super = load_super_imsm, | |
bf5a934a | 3393 | .init_super = init_super_imsm, |
cdddbdbc DW |
3394 | .store_super = store_zero_imsm, |
3395 | .free_super = free_super_imsm, | |
3396 | .match_metadata_desc = match_metadata_desc_imsm, | |
bf5a934a | 3397 | .container_content = container_content_imsm, |
cdddbdbc | 3398 | |
cdddbdbc | 3399 | .external = 1, |
845dea95 | 3400 | |
0e600426 | 3401 | #ifndef MDASSEMBLE |
845dea95 NB |
3402 | /* for mdmon */ |
3403 | .open_new = imsm_open_new, | |
3404 | .load_super = load_super_imsm, | |
ed9d66aa | 3405 | .set_array_state= imsm_set_array_state, |
845dea95 NB |
3406 | .set_disk = imsm_set_disk, |
3407 | .sync_metadata = imsm_sync_metadata, | |
88758e9d | 3408 | .activate_spare = imsm_activate_spare, |
e8319a19 | 3409 | .process_update = imsm_process_update, |
8273f55e | 3410 | .prepare_update = imsm_prepare_update, |
0e600426 | 3411 | #endif /* MDASSEMBLE */ |
cdddbdbc | 3412 | }; |