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