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