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