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