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