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