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