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