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