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