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