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