imsm: store imsm_disk entries in the super->disks list
[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 */
71 __u32 disk_ord_tbl[1]; /* disk_ord_tbl[num_members],
72 top byte special */
73} __attribute__ ((packed));
74
75struct imsm_vol {
76 __u32 reserved[2];
77 __u8 migr_state; /* Normal or Migrating */
78 __u8 migr_type; /* Initializing, Rebuilding, ... */
79 __u8 dirty;
80 __u8 fill[1];
81 __u32 filler[5];
82 struct imsm_map map[1];
83 /* here comes another one if migr_state */
84} __attribute__ ((packed));
85
86struct imsm_dev {
87 __u8 volume[MAX_RAID_SERIAL_LEN];
88 __u32 size_low;
89 __u32 size_high;
90 __u32 status; /* Persistent RaidDev status */
91 __u32 reserved_blocks; /* Reserved blocks at beginning of volume */
92#define IMSM_DEV_FILLERS 12
93 __u32 filler[IMSM_DEV_FILLERS];
94 struct imsm_vol vol;
95} __attribute__ ((packed));
96
97struct imsm_super {
98 __u8 sig[MAX_SIGNATURE_LENGTH]; /* 0x00 - 0x1F */
99 __u32 check_sum; /* 0x20 - 0x23 MPB Checksum */
100 __u32 mpb_size; /* 0x24 - 0x27 Size of MPB */
101 __u32 family_num; /* 0x28 - 0x2B Checksum from first time this config was written */
102 __u32 generation_num; /* 0x2C - 0x2F Incremented each time this array's MPB is written */
604b746f
JD
103 __u32 error_log_size; /* 0x30 - 0x33 in bytes */
104 __u32 attributes; /* 0x34 - 0x37 */
cdddbdbc
DW
105 __u8 num_disks; /* 0x38 Number of configured disks */
106 __u8 num_raid_devs; /* 0x39 Number of configured volumes */
604b746f
JD
107 __u8 error_log_pos; /* 0x3A */
108 __u8 fill[1]; /* 0x3B */
109 __u32 cache_size; /* 0x3c - 0x40 in mb */
110 __u32 orig_family_num; /* 0x40 - 0x43 original family num */
111 __u32 pwr_cycle_count; /* 0x44 - 0x47 simulated power cycle count for array */
112 __u32 bbm_log_size; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */
113#define IMSM_FILLERS 35
114 __u32 filler[IMSM_FILLERS]; /* 0x4C - 0xD7 RAID_MPB_FILLERS */
cdddbdbc
DW
115 struct imsm_disk disk[1]; /* 0xD8 diskTbl[numDisks] */
116 /* here comes imsm_dev[num_raid_devs] */
604b746f 117 /* here comes BBM logs */
cdddbdbc
DW
118} __attribute__ ((packed));
119
604b746f
JD
120#define BBM_LOG_MAX_ENTRIES 254
121
122struct bbm_log_entry {
123 __u64 defective_block_start;
124#define UNREADABLE 0xFFFFFFFF
125 __u32 spare_block_offset;
126 __u16 remapped_marked_count;
127 __u16 disk_ordinal;
128} __attribute__ ((__packed__));
129
130struct bbm_log {
131 __u32 signature; /* 0xABADB10C */
132 __u32 entry_count;
133 __u32 reserved_spare_block_count; /* 0 */
134 __u32 reserved; /* 0xFFFF */
135 __u64 first_spare_lba;
136 struct bbm_log_entry mapped_block_entries[BBM_LOG_MAX_ENTRIES];
137} __attribute__ ((__packed__));
138
139
cdddbdbc
DW
140#ifndef MDASSEMBLE
141static char *map_state_str[] = { "normal", "uninitialized", "degraded", "failed" };
142#endif
143
87eb16df 144static unsigned int sector_count(__u32 bytes)
cdddbdbc 145{
87eb16df
DW
146 return ((bytes + (512-1)) & (~(512-1))) / 512;
147}
cdddbdbc 148
87eb16df
DW
149static unsigned int mpb_sectors(struct imsm_super *mpb)
150{
151 return sector_count(__le32_to_cpu(mpb->mpb_size));
cdddbdbc
DW
152}
153
154/* internal representation of IMSM metadata */
155struct intel_super {
156 union {
949c47a0
DW
157 void *buf; /* O_DIRECT buffer for reading/writing metadata */
158 struct imsm_super *anchor; /* immovable parameters */
cdddbdbc 159 };
949c47a0 160 size_t len; /* size of the 'buf' allocation */
c2c087e6
DW
161 int updates_pending; /* count of pending updates for mdmon */
162 int creating_imsm; /* flag to indicate container creation */
bf5a934a 163 int current_vol; /* index of raid device undergoing creation */
949c47a0
DW
164 #define IMSM_MAX_RAID_DEVS 2
165 struct imsm_dev *dev_tbl[IMSM_MAX_RAID_DEVS];
cdddbdbc
DW
166 struct dl {
167 struct dl *next;
168 int index;
169 __u8 serial[MAX_RAID_SERIAL_LEN];
170 int major, minor;
171 char *devname;
b9f594fe 172 struct imsm_disk disk;
cdddbdbc
DW
173 int fd;
174 } *disks;
604b746f 175 struct bbm_log *bbm_log;
cdddbdbc
DW
176};
177
c2c087e6
DW
178struct extent {
179 unsigned long long start, size;
180};
181
88758e9d
DW
182/* definition of messages passed to imsm_process_update */
183enum imsm_update_type {
184 update_activate_spare,
8273f55e 185 update_create_array,
88758e9d
DW
186};
187
188struct imsm_update_activate_spare {
189 enum imsm_update_type type;
190 int disk_idx;
191 int slot;
192 int array;
193 struct imsm_update_activate_spare *next;
194};
195
8273f55e
DW
196struct imsm_update_create_array {
197 enum imsm_update_type type;
198 struct imsm_dev dev;
199 int dev_idx;
200};
201
0030e8d6
DW
202static int imsm_env_devname_as_serial(void)
203{
204 char *val = getenv("IMSM_DEVNAME_AS_SERIAL");
205
206 if (val && atoi(val) == 1)
207 return 1;
208
209 return 0;
210}
211
212
cdddbdbc
DW
213static struct supertype *match_metadata_desc_imsm(char *arg)
214{
215 struct supertype *st;
216
217 if (strcmp(arg, "imsm") != 0 &&
218 strcmp(arg, "default") != 0
219 )
220 return NULL;
221
222 st = malloc(sizeof(*st));
ef609477 223 memset(st, 0, sizeof(*st));
cdddbdbc
DW
224 st->ss = &super_imsm;
225 st->max_devs = IMSM_MAX_DEVICES;
226 st->minor_version = 0;
227 st->sb = NULL;
228 return st;
229}
230
cdddbdbc
DW
231static __u8 *get_imsm_version(struct imsm_super *mpb)
232{
233 return &mpb->sig[MPB_SIG_LEN];
234}
235
949c47a0
DW
236/* retrieve a disk directly from the anchor when the anchor is known to be
237 * up-to-date, currently only at load time
238 */
239static struct imsm_disk *__get_imsm_disk(struct imsm_super *mpb, __u8 index)
cdddbdbc 240{
949c47a0 241 if (index >= mpb->num_disks)
cdddbdbc
DW
242 return NULL;
243 return &mpb->disk[index];
244}
245
b9f594fe 246/* retrieve a disk from the parsed metadata */
949c47a0
DW
247static struct imsm_disk *get_imsm_disk(struct intel_super *super, __u8 index)
248{
b9f594fe
DW
249 struct dl *d;
250
251 for (d = super->disks; d; d = d->next)
252 if (d->index == index)
253 return &d->disk;
254
255 return NULL;
949c47a0
DW
256}
257
258/* generate a checksum directly from the anchor when the anchor is known to be
259 * up-to-date, currently only at load or write_super after coalescing
260 */
261static __u32 __gen_imsm_checksum(struct imsm_super *mpb)
cdddbdbc
DW
262{
263 __u32 end = mpb->mpb_size / sizeof(end);
264 __u32 *p = (__u32 *) mpb;
265 __u32 sum = 0;
266
267 while (end--)
268 sum += __le32_to_cpu(*p++);
269
270 return sum - __le32_to_cpu(mpb->check_sum);
271}
272
273static size_t sizeof_imsm_dev(struct imsm_dev *dev)
274{
275 size_t size = sizeof(*dev);
276
277 /* each map has disk_ord_tbl[num_members - 1] additional space */
278 size += sizeof(__u32) * (dev->vol.map[0].num_members - 1);
279
280 /* migrating means an additional map */
281 if (dev->vol.migr_state) {
282 size += sizeof(struct imsm_map);
283 size += sizeof(__u32) * (dev->vol.map[1].num_members - 1);
284 }
285
286 return size;
287}
288
949c47a0 289static struct imsm_dev *__get_imsm_dev(struct imsm_super *mpb, __u8 index)
cdddbdbc
DW
290{
291 int offset;
292 int i;
293 void *_mpb = mpb;
294
949c47a0 295 if (index >= mpb->num_raid_devs)
cdddbdbc
DW
296 return NULL;
297
298 /* devices start after all disks */
299 offset = ((void *) &mpb->disk[mpb->num_disks]) - _mpb;
300
301 for (i = 0; i <= index; i++)
302 if (i == index)
303 return _mpb + offset;
304 else
305 offset += sizeof_imsm_dev(_mpb + offset);
306
307 return NULL;
308}
309
949c47a0
DW
310static struct imsm_dev *get_imsm_dev(struct intel_super *super, __u8 index)
311{
312 if (index >= super->anchor->num_raid_devs)
313 return NULL;
314 return super->dev_tbl[index];
315}
316
cdddbdbc
DW
317static __u32 get_imsm_disk_idx(struct imsm_map *map, int slot)
318{
319 __u32 *ord_tbl = &map->disk_ord_tbl[slot];
320
321 /* top byte is 'special' */
322 return __le32_to_cpu(*ord_tbl & ~(0xff << 24));
323}
324
cdddbdbc
DW
325static int get_imsm_raid_level(struct imsm_map *map)
326{
327 if (map->raid_level == 1) {
328 if (map->num_members == 2)
329 return 1;
330 else
331 return 10;
332 }
333
334 return map->raid_level;
335}
336
c2c087e6
DW
337static int cmp_extent(const void *av, const void *bv)
338{
339 const struct extent *a = av;
340 const struct extent *b = bv;
341 if (a->start < b->start)
342 return -1;
343 if (a->start > b->start)
344 return 1;
345 return 0;
346}
347
348static struct extent *get_extents(struct intel_super *super, struct dl *dl)
349{
350 /* find a list of used extents on the given physical device */
c2c087e6
DW
351 struct extent *rv, *e;
352 int i, j;
353 int memberships = 0;
354
949c47a0
DW
355 for (i = 0; i < super->anchor->num_raid_devs; i++) {
356 struct imsm_dev *dev = get_imsm_dev(super, i);
c2c087e6
DW
357 struct imsm_map *map = dev->vol.map;
358
359 for (j = 0; j < map->num_members; j++) {
360 __u32 index = get_imsm_disk_idx(map, j);
361
362 if (index == dl->index)
363 memberships++;
364 }
365 }
366 rv = malloc(sizeof(struct extent) * (memberships + 1));
367 if (!rv)
368 return NULL;
369 e = rv;
370
949c47a0
DW
371 for (i = 0; i < super->anchor->num_raid_devs; i++) {
372 struct imsm_dev *dev = get_imsm_dev(super, i);
c2c087e6
DW
373 struct imsm_map *map = dev->vol.map;
374
375 for (j = 0; j < map->num_members; j++) {
376 __u32 index = get_imsm_disk_idx(map, j);
377
378 if (index == dl->index) {
379 e->start = __le32_to_cpu(map->pba_of_lba0);
380 e->size = __le32_to_cpu(map->blocks_per_member);
381 e++;
382 }
383 }
384 }
385 qsort(rv, memberships, sizeof(*rv), cmp_extent);
386
b9f594fe 387 e->start = __le32_to_cpu(dl->disk.total_blocks) -
c2c087e6
DW
388 (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS);
389 e->size = 0;
390 return rv;
391}
392
4f5bc454 393#ifndef MDASSEMBLE
cdddbdbc
DW
394static void print_imsm_dev(struct imsm_dev *dev, int index)
395{
396 __u64 sz;
397 int slot;
398 struct imsm_map *map = dev->vol.map;
399
400 printf("\n");
401 printf("[%s]:\n", dev->volume);
402 printf(" RAID Level : %d\n", get_imsm_raid_level(map));
403 printf(" Members : %d\n", map->num_members);
404 for (slot = 0; slot < map->num_members; slot++)
405 if (index == get_imsm_disk_idx(map, slot))
406 break;
407 if (slot < map->num_members)
408 printf(" This Slot : %d\n", slot);
409 else
410 printf(" This Slot : ?\n");
411 sz = __le32_to_cpu(dev->size_high);
412 sz <<= 32;
413 sz += __le32_to_cpu(dev->size_low);
414 printf(" Array Size : %llu%s\n", (unsigned long long)sz,
415 human_size(sz * 512));
416 sz = __le32_to_cpu(map->blocks_per_member);
417 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz,
418 human_size(sz * 512));
419 printf(" Sector Offset : %u\n",
420 __le32_to_cpu(map->pba_of_lba0));
421 printf(" Num Stripes : %u\n",
422 __le32_to_cpu(map->num_data_stripes));
423 printf(" Chunk Size : %u KiB\n",
424 __le16_to_cpu(map->blocks_per_strip) / 2);
425 printf(" Reserved : %d\n", __le32_to_cpu(dev->reserved_blocks));
426 printf(" Migrate State : %s\n", dev->vol.migr_state ? "migrating" : "idle");
427 printf(" Dirty State : %s\n", dev->vol.dirty ? "dirty" : "clean");
428 printf(" Map State : %s\n", map_state_str[map->map_state]);
429}
430
431static void print_imsm_disk(struct imsm_super *mpb, int index)
432{
949c47a0 433 struct imsm_disk *disk = __get_imsm_disk(mpb, index);
cdddbdbc
DW
434 char str[MAX_RAID_SERIAL_LEN];
435 __u32 s;
436 __u64 sz;
437
e9d82038
DW
438 if (index < 0)
439 return;
440
cdddbdbc
DW
441 printf("\n");
442 snprintf(str, MAX_RAID_SERIAL_LEN, "%s", disk->serial);
443 printf(" Disk%02d Serial : %s\n", index, str);
444 s = __le32_to_cpu(disk->status);
445 printf(" State :%s%s%s%s\n", s&SPARE_DISK ? " spare" : "",
446 s&CONFIGURED_DISK ? " active" : "",
447 s&FAILED_DISK ? " failed" : "",
448 s&USABLE_DISK ? " usable" : "");
449 printf(" Id : %08x\n", __le32_to_cpu(disk->scsi_id));
c2c087e6
DW
450 sz = __le32_to_cpu(disk->total_blocks) -
451 (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS * mpb->num_raid_devs);
cdddbdbc
DW
452 printf(" Usable Size : %llu%s\n", (unsigned long long)sz,
453 human_size(sz * 512));
454}
455
456static void examine_super_imsm(struct supertype *st, char *homehost)
457{
458 struct intel_super *super = st->sb;
949c47a0 459 struct imsm_super *mpb = super->anchor;
cdddbdbc
DW
460 char str[MAX_SIGNATURE_LENGTH];
461 int i;
462 __u32 sum;
463
464 snprintf(str, MPB_SIG_LEN, "%s", mpb->sig);
465 printf(" Magic : %s\n", str);
466 snprintf(str, strlen(MPB_VERSION_RAID0), "%s", get_imsm_version(mpb));
467 printf(" Version : %s\n", get_imsm_version(mpb));
468 printf(" Family : %08x\n", __le32_to_cpu(mpb->family_num));
469 printf(" Generation : %08x\n", __le32_to_cpu(mpb->generation_num));
470 sum = __le32_to_cpu(mpb->check_sum);
471 printf(" Checksum : %08x %s\n", sum,
949c47a0 472 __gen_imsm_checksum(mpb) == sum ? "correct" : "incorrect");
87eb16df 473 printf(" MPB Sectors : %d\n", mpb_sectors(mpb));
cdddbdbc
DW
474 printf(" Disks : %d\n", mpb->num_disks);
475 printf(" RAID Devices : %d\n", mpb->num_raid_devs);
476 print_imsm_disk(mpb, super->disks->index);
604b746f
JD
477 if (super->bbm_log) {
478 struct bbm_log *log = super->bbm_log;
479
480 printf("\n");
481 printf("Bad Block Management Log:\n");
482 printf(" Log Size : %d\n", __le32_to_cpu(mpb->bbm_log_size));
483 printf(" Signature : %x\n", __le32_to_cpu(log->signature));
484 printf(" Entry Count : %d\n", __le32_to_cpu(log->entry_count));
485 printf(" Spare Blocks : %d\n", __le32_to_cpu(log->reserved_spare_block_count));
486 printf(" First Spare : %llx\n", __le64_to_cpu(log->first_spare_lba));
487 }
cdddbdbc 488 for (i = 0; i < mpb->num_raid_devs; i++)
949c47a0 489 print_imsm_dev(__get_imsm_dev(mpb, i), super->disks->index);
cdddbdbc
DW
490 for (i = 0; i < mpb->num_disks; i++) {
491 if (i == super->disks->index)
492 continue;
493 print_imsm_disk(mpb, i);
494 }
495}
496
497static void brief_examine_super_imsm(struct supertype *st)
498{
499 struct intel_super *super = st->sb;
cdddbdbc
DW
500
501 printf("ARRAY /dev/imsm family=%08x metadata=external:imsm\n",
949c47a0 502 __le32_to_cpu(super->anchor->family_num));
cdddbdbc
DW
503}
504
505static void detail_super_imsm(struct supertype *st, char *homehost)
506{
507 printf("%s\n", __FUNCTION__);
508}
509
510static void brief_detail_super_imsm(struct supertype *st)
511{
512 printf("%s\n", __FUNCTION__);
513}
514#endif
515
516static int match_home_imsm(struct supertype *st, char *homehost)
517{
518 printf("%s\n", __FUNCTION__);
519
520 return 0;
521}
522
523static void uuid_from_super_imsm(struct supertype *st, int uuid[4])
524{
525 printf("%s\n", __FUNCTION__);
526}
527
0d481d37 528#if 0
4f5bc454
DW
529static void
530get_imsm_numerical_version(struct imsm_super *mpb, int *m, int *p)
cdddbdbc 531{
cdddbdbc
DW
532 __u8 *v = get_imsm_version(mpb);
533 __u8 *end = mpb->sig + MAX_SIGNATURE_LENGTH;
534 char major[] = { 0, 0, 0 };
535 char minor[] = { 0 ,0, 0 };
536 char patch[] = { 0, 0, 0 };
537 char *ver_parse[] = { major, minor, patch };
538 int i, j;
539
540 i = j = 0;
541 while (*v != '\0' && v < end) {
542 if (*v != '.' && j < 2)
543 ver_parse[i][j++] = *v;
544 else {
545 i++;
546 j = 0;
547 }
548 v++;
549 }
550
4f5bc454
DW
551 *m = strtol(minor, NULL, 0);
552 *p = strtol(patch, NULL, 0);
553}
0d481d37 554#endif
4f5bc454 555
c2c087e6
DW
556static int imsm_level_to_layout(int level)
557{
558 switch (level) {
559 case 0:
560 case 1:
561 return 0;
562 case 5:
563 case 6:
a380c027 564 return ALGORITHM_LEFT_ASYMMETRIC;
c2c087e6
DW
565 case 10:
566 return 0x102; //FIXME is this correct?
567 }
568 return -1;
569}
570
bf5a934a
DW
571static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info)
572{
573 struct intel_super *super = st->sb;
949c47a0 574 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
bf5a934a
DW
575 struct imsm_map *map = &dev->vol.map[0];
576
577 info->container_member = super->current_vol;
578 info->array.raid_disks = map->num_members;
579 info->array.level = get_imsm_raid_level(map);
580 info->array.layout = imsm_level_to_layout(info->array.level);
581 info->array.md_minor = -1;
582 info->array.ctime = 0;
583 info->array.utime = 0;
584 info->array.chunk_size = __le16_to_cpu(map->blocks_per_strip * 512);
585
586 info->data_offset = __le32_to_cpu(map->pba_of_lba0);
587 info->component_size = __le32_to_cpu(map->blocks_per_member);
588
589 info->disk.major = 0;
590 info->disk.minor = 0;
591
592 sprintf(info->text_version, "/%s/%d",
593 devnum2devname(st->container_dev),
594 info->container_member);
595}
596
597
4f5bc454
DW
598static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info)
599{
600 struct intel_super *super = st->sb;
4f5bc454
DW
601 struct imsm_disk *disk;
602 __u32 s;
4f5bc454 603
bf5a934a
DW
604 if (super->current_vol >= 0) {
605 getinfo_super_imsm_volume(st, info);
606 return;
607 }
949c47a0 608 info->array.raid_disks = super->anchor->num_disks;
cdddbdbc
DW
609 info->array.level = LEVEL_CONTAINER;
610 info->array.layout = 0;
611 info->array.md_minor = -1;
c2c087e6 612 info->array.ctime = 0; /* N/A for imsm */
cdddbdbc
DW
613 info->array.utime = 0;
614 info->array.chunk_size = 0;
615
616 info->disk.major = 0;
617 info->disk.minor = 0;
cdddbdbc 618 info->disk.raid_disk = -1;
c2c087e6
DW
619 info->reshape_active = 0;
620 strcpy(info->text_version, "imsm");
621 info->disk.number = -1;
622 info->disk.state = 0;
623
4a04ec6c 624 if (super->disks) {
b9f594fe 625 disk = &super->disks->disk;
4a04ec6c
DW
626 info->disk.number = super->disks->index;
627 info->disk.raid_disk = super->disks->index;
bf5a934a
DW
628 info->data_offset = __le32_to_cpu(disk->total_blocks) -
629 (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS);
630 info->component_size = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
4a04ec6c
DW
631 s = __le32_to_cpu(disk->status);
632 info->disk.state = s & CONFIGURED_DISK ? (1 << MD_DISK_ACTIVE) : 0;
633 info->disk.state |= s & FAILED_DISK ? (1 << MD_DISK_FAULTY) : 0;
634 info->disk.state |= s & USABLE_DISK ? (1 << MD_DISK_SYNC) : 0;
cdddbdbc 635 }
cdddbdbc
DW
636}
637
cdddbdbc
DW
638static int update_super_imsm(struct supertype *st, struct mdinfo *info,
639 char *update, char *devname, int verbose,
640 int uuid_set, char *homehost)
641{
f352c545
DW
642 /* FIXME */
643
644 /* For 'assemble' and 'force' we need to return non-zero if any
645 * change was made. For others, the return value is ignored.
646 * Update options are:
647 * force-one : This device looks a bit old but needs to be included,
648 * update age info appropriately.
649 * assemble: clear any 'faulty' flag to allow this device to
650 * be assembled.
651 * force-array: Array is degraded but being forced, mark it clean
652 * if that will be needed to assemble it.
653 *
654 * newdev: not used ????
655 * grow: Array has gained a new device - this is currently for
656 * linear only
657 * resync: mark as dirty so a resync will happen.
658 * name: update the name - preserving the homehost
659 *
660 * Following are not relevant for this imsm:
661 * sparc2.2 : update from old dodgey metadata
662 * super-minor: change the preferred_minor number
663 * summaries: update redundant counters.
664 * uuid: Change the uuid of the array to match watch is given
665 * homehost: update the recorded homehost
666 * _reshape_progress: record new reshape_progress position.
667 */
668 int rv = 0;
669 //struct intel_super *super = st->sb;
670 //struct imsm_super *mpb = super->mpb;
671
672 if (strcmp(update, "grow") == 0) {
673 }
674 if (strcmp(update, "resync") == 0) {
675 /* dev->vol.dirty = 1; */
676 }
677
678 /* IMSM has no concept of UUID or homehost */
679
680 return rv;
cdddbdbc
DW
681}
682
c2c087e6 683static size_t disks_to_mpb_size(int disks)
cdddbdbc 684{
c2c087e6 685 size_t size;
cdddbdbc 686
c2c087e6
DW
687 size = sizeof(struct imsm_super);
688 size += (disks - 1) * sizeof(struct imsm_disk);
689 size += 2 * sizeof(struct imsm_dev);
690 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
691 size += (4 - 2) * sizeof(struct imsm_map);
692 /* 4 possible disk_ord_tbl's */
693 size += 4 * (disks - 1) * sizeof(__u32);
694
695 return size;
696}
697
698static __u64 avail_size_imsm(struct supertype *st, __u64 devsize)
699{
700 if (devsize < (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS))
701 return 0;
702
703 return devsize - (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS);
cdddbdbc
DW
704}
705
706static int compare_super_imsm(struct supertype *st, struct supertype *tst)
707{
708 /*
709 * return:
710 * 0 same, or first was empty, and second was copied
711 * 1 second had wrong number
712 * 2 wrong uuid
713 * 3 wrong other info
714 */
715 struct intel_super *first = st->sb;
716 struct intel_super *sec = tst->sb;
717
718 if (!first) {
719 st->sb = tst->sb;
720 tst->sb = NULL;
721 return 0;
722 }
723
949c47a0 724 if (memcmp(first->anchor->sig, sec->anchor->sig, MAX_SIGNATURE_LENGTH) != 0)
cdddbdbc 725 return 3;
949c47a0 726 if (first->anchor->family_num != sec->anchor->family_num)
cdddbdbc 727 return 3;
949c47a0 728 if (first->anchor->mpb_size != sec->anchor->mpb_size)
cdddbdbc 729 return 3;
949c47a0 730 if (first->anchor->check_sum != sec->anchor->check_sum)
cdddbdbc
DW
731 return 3;
732
733 return 0;
734}
735
0030e8d6
DW
736static void fd2devname(int fd, char *name)
737{
738 struct stat st;
739 char path[256];
740 char dname[100];
741 char *nm;
742 int rv;
743
744 name[0] = '\0';
745 if (fstat(fd, &st) != 0)
746 return;
747 sprintf(path, "/sys/dev/block/%d:%d",
748 major(st.st_rdev), minor(st.st_rdev));
749
750 rv = readlink(path, dname, sizeof(dname));
751 if (rv <= 0)
752 return;
753
754 dname[rv] = '\0';
755 nm = strrchr(dname, '/');
756 nm++;
757 snprintf(name, MAX_RAID_SERIAL_LEN, "/dev/%s", nm);
758}
759
760
cdddbdbc
DW
761extern int scsi_get_serial(int fd, void *buf, size_t buf_len);
762
763static int imsm_read_serial(int fd, char *devname,
764 __u8 serial[MAX_RAID_SERIAL_LEN])
765{
766 unsigned char scsi_serial[255];
cdddbdbc
DW
767 int rv;
768 int rsp_len;
769 int i, cnt;
770
771 memset(scsi_serial, 0, sizeof(scsi_serial));
cdddbdbc 772
0030e8d6
DW
773 if (imsm_env_devname_as_serial()) {
774 char name[MAX_RAID_SERIAL_LEN];
775
776 fd2devname(fd, name);
777 strcpy((char *) serial, name);
778 return 0;
779 }
780
755c99fa 781 rv = scsi_get_serial(fd, scsi_serial, sizeof(scsi_serial));
cdddbdbc
DW
782
783 if (rv != 0) {
784 if (devname)
785 fprintf(stderr,
786 Name ": Failed to retrieve serial for %s\n",
787 devname);
788 return rv;
789 }
790
791 rsp_len = scsi_serial[3];
792 for (i = 0, cnt = 0; i < rsp_len; i++) {
793 if (!isspace(scsi_serial[4 + i]))
794 serial[cnt++] = scsi_serial[4 + i];
795 if (cnt == MAX_RAID_SERIAL_LEN)
796 break;
797 }
798
799 serial[MAX_RAID_SERIAL_LEN - 1] = '\0';
800
801 return 0;
802}
803
804static int
805load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd)
806{
cdddbdbc
DW
807 struct dl *dl;
808 struct stat stb;
cdddbdbc
DW
809 int rv;
810 int i;
811
812 dl = malloc(sizeof(*dl));
b9f594fe 813 if (!dl) {
cdddbdbc
DW
814 if (devname)
815 fprintf(stderr,
816 Name ": failed to allocate disk buffer for %s\n",
817 devname);
818 return 2;
819 }
820 memset(dl, 0, sizeof(*dl));
821
822 fstat(fd, &stb);
823 dl->major = major(stb.st_rdev);
824 dl->minor = minor(stb.st_rdev);
825 dl->next = super->disks;
826 dl->fd = keep_fd ? fd : -1;
827 dl->devname = devname ? strdup(devname) : NULL;
828 dl->index = -1;
829 super->disks = dl;
830 rv = imsm_read_serial(fd, devname, dl->serial);
831
832 if (rv != 0)
833 return 2;
834
835 /* look up this disk's index */
949c47a0
DW
836 for (i = 0; i < super->anchor->num_disks; i++) {
837 struct imsm_disk *disk_iter;
838
839 disk_iter = __get_imsm_disk(super->anchor, i);
cdddbdbc 840
949c47a0
DW
841 if (memcmp(disk_iter->serial, dl->serial,
842 MAX_RAID_SERIAL_LEN) == 0) {
b9f594fe 843 dl->disk = *disk_iter;
949c47a0 844 dl->index = i;
cdddbdbc 845 break;
949c47a0 846 }
cdddbdbc
DW
847 }
848
949c47a0 849 if (i == super->anchor->num_disks) {
e9d82038
DW
850 if (devname)
851 fprintf(stderr,
852 Name ": failed to match serial \'%s\' for %s\n",
853 dl->serial, devname);
854 return 0;
855 }
cdddbdbc 856
949c47a0
DW
857 return 0;
858}
859
860static void imsm_copy_dev(struct imsm_dev *dest, struct imsm_dev *src)
861{
862 int i;
863
864 *dest = *src;
865
866 for (i = 0; i < src->vol.map[0].num_members; i++)
867 dest->vol.map[0].disk_ord_tbl[i] = src->vol.map[0].disk_ord_tbl[i];
868
869 if (!src->vol.migr_state)
870 return;
871
872 dest->vol.map[1] = src->vol.map[1];
873 for (i = 0; i < src->vol.map[1].num_members; i++)
874 dest->vol.map[1].disk_ord_tbl[i] = src->vol.map[1].disk_ord_tbl[i];
875}
876
877static int parse_raid_devices(struct intel_super *super)
878{
879 int i;
880 struct imsm_dev *dev_new;
881 size_t len;
882
883 for (i = 0; i < super->anchor->num_raid_devs; i++) {
884 struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i);
885
886 len = sizeof_imsm_dev(dev_iter);
887 dev_new = malloc(len);
888 if (!dev_new)
889 return 1;
890 imsm_copy_dev(dev_new, dev_iter);
891 super->dev_tbl[i] = dev_new;
892 }
cdddbdbc
DW
893
894 return 0;
895}
896
604b746f
JD
897/* retrieve a pointer to the bbm log which starts after all raid devices */
898struct bbm_log *__get_imsm_bbm_log(struct imsm_super *mpb)
899{
900 void *ptr = NULL;
901
902 if (__le32_to_cpu(mpb->bbm_log_size)) {
903 ptr = mpb;
904 ptr += mpb->mpb_size - __le32_to_cpu(mpb->bbm_log_size);
905 }
906
907 return ptr;
908}
909
9ca2c81c
DW
910static void __free_imsm(struct intel_super *super);
911
cdddbdbc
DW
912/* load_imsm_mpb - read matrix metadata
913 * allocates super->mpb to be freed by free_super
914 */
915static int load_imsm_mpb(int fd, struct intel_super *super, char *devname)
916{
917 unsigned long long dsize;
cdddbdbc
DW
918 unsigned long long sectors;
919 struct stat;
6416d527 920 struct imsm_super *anchor;
cdddbdbc 921 __u32 check_sum;
949c47a0 922 int rc;
cdddbdbc 923
cdddbdbc
DW
924 get_dev_size(fd, NULL, &dsize);
925
926 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0) {
927 if (devname)
928 fprintf(stderr,
929 Name ": Cannot seek to anchor block on %s: %s\n",
930 devname, strerror(errno));
931 return 1;
932 }
933
949c47a0 934 if (posix_memalign((void**)&anchor, 512, 512) != 0) {
ad97895e
DW
935 if (devname)
936 fprintf(stderr,
937 Name ": Failed to allocate imsm anchor buffer"
938 " on %s\n", devname);
939 return 1;
940 }
949c47a0 941 if (read(fd, anchor, 512) != 512) {
cdddbdbc
DW
942 if (devname)
943 fprintf(stderr,
944 Name ": Cannot read anchor block on %s: %s\n",
945 devname, strerror(errno));
6416d527 946 free(anchor);
cdddbdbc
DW
947 return 1;
948 }
949
6416d527 950 if (strncmp((char *) anchor->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) {
cdddbdbc
DW
951 if (devname)
952 fprintf(stderr,
953 Name ": no IMSM anchor on %s\n", devname);
6416d527 954 free(anchor);
cdddbdbc
DW
955 return 2;
956 }
957
9ca2c81c 958 __free_imsm(super);
949c47a0
DW
959 super->len = __le32_to_cpu(anchor->mpb_size);
960 super->len = ROUND_UP(anchor->mpb_size, 512);
961 if (posix_memalign(&super->buf, 512, super->len) != 0) {
cdddbdbc
DW
962 if (devname)
963 fprintf(stderr,
964 Name ": unable to allocate %zu byte mpb buffer\n",
949c47a0 965 super->len);
6416d527 966 free(anchor);
cdddbdbc
DW
967 return 2;
968 }
949c47a0 969 memcpy(super->buf, anchor, 512);
cdddbdbc 970
6416d527
NB
971 sectors = mpb_sectors(anchor) - 1;
972 free(anchor);
949c47a0
DW
973 if (!sectors) {
974 rc = load_imsm_disk(fd, super, devname, 0);
975 if (rc == 0)
976 rc = parse_raid_devices(super);
977 return rc;
978 }
cdddbdbc
DW
979
980 /* read the extended mpb */
981 if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0) {
982 if (devname)
983 fprintf(stderr,
984 Name ": Cannot seek to extended mpb on %s: %s\n",
985 devname, strerror(errno));
986 return 1;
987 }
988
949c47a0 989 if (read(fd, super->buf + 512, super->len - 512) != super->len - 512) {
cdddbdbc
DW
990 if (devname)
991 fprintf(stderr,
992 Name ": Cannot read extended mpb on %s: %s\n",
993 devname, strerror(errno));
994 return 2;
995 }
996
949c47a0
DW
997 check_sum = __gen_imsm_checksum(super->anchor);
998 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
cdddbdbc
DW
999 if (devname)
1000 fprintf(stderr,
1001 Name ": IMSM checksum %x != %x on %s\n",
949c47a0 1002 check_sum, __le32_to_cpu(super->anchor->check_sum),
cdddbdbc
DW
1003 devname);
1004 return 2;
1005 }
1006
604b746f
JD
1007 /* FIXME the BBM log is disk specific so we cannot use this global
1008 * buffer for all disks. Ok for now since we only look at the global
1009 * bbm_log_size parameter to gate assembly
1010 */
1011 super->bbm_log = __get_imsm_bbm_log(super->anchor);
1012
949c47a0
DW
1013 rc = load_imsm_disk(fd, super, devname, 0);
1014 if (rc == 0)
1015 rc = parse_raid_devices(super);
1016 return rc;
cdddbdbc
DW
1017}
1018
cdddbdbc
DW
1019static void free_imsm_disks(struct intel_super *super)
1020{
1021 while (super->disks) {
1022 struct dl *d = super->disks;
1023
1024 super->disks = d->next;
1025 if (d->fd >= 0)
1026 close(d->fd);
1027 if (d->devname)
1028 free(d->devname);
1029 free(d);
1030 }
1031}
1032
9ca2c81c
DW
1033/* free all the pieces hanging off of a super pointer */
1034static void __free_imsm(struct intel_super *super)
cdddbdbc 1035{
949c47a0
DW
1036 int i;
1037
9ca2c81c 1038 if (super->buf) {
949c47a0 1039 free(super->buf);
9ca2c81c
DW
1040 super->buf = NULL;
1041 }
cdddbdbc 1042 free_imsm_disks(super);
949c47a0 1043 for (i = 0; i < IMSM_MAX_RAID_DEVS; i++)
9ca2c81c 1044 if (super->dev_tbl[i]) {
949c47a0 1045 free(super->dev_tbl[i]);
9ca2c81c
DW
1046 super->dev_tbl[i] = NULL;
1047 }
cdddbdbc
DW
1048}
1049
9ca2c81c
DW
1050static void free_imsm(struct intel_super *super)
1051{
1052 __free_imsm(super);
1053 free(super);
1054}
cdddbdbc
DW
1055
1056static void free_super_imsm(struct supertype *st)
1057{
1058 struct intel_super *super = st->sb;
1059
1060 if (!super)
1061 return;
1062
1063 free_imsm(super);
1064 st->sb = NULL;
1065}
1066
c2c087e6
DW
1067static struct intel_super *alloc_super(int creating_imsm)
1068{
1069 struct intel_super *super = malloc(sizeof(*super));
1070
1071 if (super) {
1072 memset(super, 0, sizeof(*super));
1073 super->creating_imsm = creating_imsm;
bf5a934a 1074 super->current_vol = -1;
c2c087e6
DW
1075 }
1076
1077 return super;
1078}
1079
cdddbdbc
DW
1080#ifndef MDASSEMBLE
1081static int load_super_imsm_all(struct supertype *st, int fd, void **sbp,
1082 char *devname, int keep_fd)
1083{
1084 struct mdinfo *sra;
1085 struct intel_super *super;
1086 struct mdinfo *sd, *best = NULL;
1087 __u32 bestgen = 0;
1088 __u32 gen;
1089 char nm[20];
1090 int dfd;
1091 int rv;
1092
1093 /* check if this disk is a member of an active array */
1094 sra = sysfs_read(fd, 0, GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE);
1095 if (!sra)
1096 return 1;
1097
1098 if (sra->array.major_version != -1 ||
1099 sra->array.minor_version != -2 ||
1100 strcmp(sra->text_version, "imsm") != 0)
1101 return 1;
1102
c2c087e6 1103 super = alloc_super(0);
cdddbdbc
DW
1104 if (!super)
1105 return 1;
1106
1107 /* find the most up to date disk in this array */
1108 for (sd = sra->devs; sd; sd = sd->next) {
1109 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
1110 dfd = dev_open(nm, keep_fd ? O_RDWR : O_RDONLY);
1111 if (!dfd) {
1112 free_imsm(super);
1113 return 2;
1114 }
1115 rv = load_imsm_mpb(dfd, super, NULL);
1116 if (!keep_fd)
1117 close(dfd);
1118 if (rv == 0) {
949c47a0 1119 gen = __le32_to_cpu(super->anchor->generation_num);
cdddbdbc
DW
1120 if (!best || gen > bestgen) {
1121 bestgen = gen;
1122 best = sd;
1123 }
1124 } else {
1125 free_imsm(super);
1126 return 2;
1127 }
1128 }
1129
1130 if (!best) {
1131 free_imsm(super);
1132 return 1;
1133 }
1134
1135 /* load the most up to date anchor */
1136 sprintf(nm, "%d:%d", best->disk.major, best->disk.minor);
1137 dfd = dev_open(nm, O_RDONLY);
1138 if (!dfd) {
1139 free_imsm(super);
1140 return 1;
1141 }
1142 rv = load_imsm_mpb(dfd, super, NULL);
1143 close(dfd);
1144 if (rv != 0) {
1145 free_imsm(super);
1146 return 2;
1147 }
1148
1149 /* reset the disk list */
1150 free_imsm_disks(super);
1151
1152 /* populate disk list */
1153 for (sd = sra->devs ; sd ; sd = sd->next) {
1154 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
1155 dfd = dev_open(nm, keep_fd? O_RDWR : O_RDONLY);
1156 if (!dfd) {
1157 free_imsm(super);
1158 return 2;
1159 }
1160 load_imsm_disk(dfd, super, NULL, keep_fd);
1161 if (!keep_fd)
1162 close(dfd);
1163 }
1164
f7e7067b 1165 if (st->subarray[0]) {
949c47a0 1166 if (atoi(st->subarray) <= super->anchor->num_raid_devs)
bf5a934a
DW
1167 super->current_vol = atoi(st->subarray);
1168 else
1169 return 1;
f7e7067b
NB
1170 }
1171
cdddbdbc
DW
1172 *sbp = super;
1173 if (st->ss == NULL) {
bf5a934a 1174 st->ss = &super_imsm;
cdddbdbc
DW
1175 st->minor_version = 0;
1176 st->max_devs = IMSM_MAX_DEVICES;
f4d11639 1177 st->container_dev = fd2devnum(fd);
cdddbdbc
DW
1178 }
1179
1180 return 0;
1181}
1182#endif
1183
1184static int load_super_imsm(struct supertype *st, int fd, char *devname)
1185{
1186 struct intel_super *super;
1187 int rv;
1188
1189#ifndef MDASSEMBLE
3dbccbcf 1190 if (load_super_imsm_all(st, fd, &st->sb, devname, 1) == 0)
cdddbdbc
DW
1191 return 0;
1192#endif
f7e7067b
NB
1193 if (st->subarray[0])
1194 return 1; /* FIXME */
cdddbdbc 1195
c2c087e6 1196 super = alloc_super(0);
cdddbdbc
DW
1197 if (!super) {
1198 fprintf(stderr,
1199 Name ": malloc of %zu failed.\n",
1200 sizeof(*super));
1201 return 1;
1202 }
1203
1204 rv = load_imsm_mpb(fd, super, devname);
1205
1206 if (rv) {
1207 if (devname)
1208 fprintf(stderr,
1209 Name ": Failed to load all information "
1210 "sections on %s\n", devname);
1211 free_imsm(super);
1212 return rv;
1213 }
1214
1215 st->sb = super;
1216 if (st->ss == NULL) {
1217 st->ss = &super_imsm;
1218 st->minor_version = 0;
1219 st->max_devs = IMSM_MAX_DEVICES;
1220 }
1221
1222 return 0;
1223}
1224
ef6ffade
DW
1225static __u16 info_to_blocks_per_strip(mdu_array_info_t *info)
1226{
1227 if (info->level == 1)
1228 return 128;
1229 return info->chunk_size >> 9;
1230}
1231
1232static __u32 info_to_num_data_stripes(mdu_array_info_t *info)
1233{
1234 __u32 num_stripes;
1235
1236 num_stripes = (info->size * 2) / info_to_blocks_per_strip(info);
1237 if (info->level == 1)
1238 num_stripes /= 2;
1239
1240 return num_stripes;
1241}
1242
fcfd9599
DW
1243static __u32 info_to_blocks_per_member(mdu_array_info_t *info)
1244{
1245 return (info->size * 2) & ~(info_to_blocks_per_strip(info) - 1);
1246}
1247
8b353278
DW
1248static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
1249 unsigned long long size, char *name,
1250 char *homehost, int *uuid)
cdddbdbc 1251{
c2c087e6
DW
1252 /* We are creating a volume inside a pre-existing container.
1253 * so st->sb is already set.
1254 */
1255 struct intel_super *super = st->sb;
949c47a0 1256 struct imsm_super *mpb = super->anchor;
c2c087e6
DW
1257 struct imsm_dev *dev;
1258 struct imsm_vol *vol;
1259 struct imsm_map *map;
1260 int idx = mpb->num_raid_devs;
1261 int i;
1262 unsigned long long array_blocks;
c2c087e6 1263 __u32 offset = 0;
2c092cad 1264 size_t size_old, size_new;
cdddbdbc 1265
c2c087e6
DW
1266 if (mpb->num_raid_devs >= 2) {
1267 fprintf(stderr, Name": This imsm-container already has the "
1268 "maximum of 2 volumes\n");
1269 return 0;
1270 }
1271
2c092cad
DW
1272 /* ensure the mpb is large enough for the new data */
1273 size_old = __le32_to_cpu(mpb->mpb_size);
1274 size_new = disks_to_mpb_size(info->nr_disks);
1275 if (size_new > size_old) {
1276 void *mpb_new;
1277 size_t size_round = ROUND_UP(size_new, 512);
1278
1279 if (posix_memalign(&mpb_new, 512, size_round) != 0) {
1280 fprintf(stderr, Name": could not allocate new mpb\n");
1281 return 0;
1282 }
1283 memcpy(mpb_new, mpb, size_old);
1284 free(mpb);
1285 mpb = mpb_new;
949c47a0 1286 super->anchor = mpb_new;
2c092cad
DW
1287 mpb->mpb_size = __cpu_to_le32(size_new);
1288 memset(mpb_new + size_old, 0, size_round - size_old);
1289 }
bf5a934a
DW
1290 super->current_vol = idx;
1291 sprintf(st->subarray, "%d", idx);
949c47a0
DW
1292 dev = malloc(sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1));
1293 if (!dev) {
1294 fprintf(stderr, Name": could not allocate raid device\n");
1295 return 0;
1296 }
c2c087e6
DW
1297 strncpy((char *) dev->volume, name, MAX_RAID_SERIAL_LEN);
1298 array_blocks = calc_array_size(info->level, info->raid_disks,
1299 info->layout, info->chunk_size,
1300 info->size*2);
1301 dev->size_low = __cpu_to_le32((__u32) array_blocks);
1302 dev->size_high = __cpu_to_le32((__u32) (array_blocks >> 32));
1303 dev->status = __cpu_to_le32(0);
1304 dev->reserved_blocks = __cpu_to_le32(0);
1305 vol = &dev->vol;
1306 vol->migr_state = 0;
1307 vol->migr_type = 0;
1308 vol->dirty = 0;
1309 for (i = 0; i < idx; i++) {
949c47a0 1310 struct imsm_dev *prev = get_imsm_dev(super, i);
c2c087e6
DW
1311 struct imsm_map *pmap = &prev->vol.map[0];
1312
1313 offset += __le32_to_cpu(pmap->blocks_per_member);
1314 offset += IMSM_RESERVED_SECTORS;
1315 }
1316 map = &vol->map[0];
1317 map->pba_of_lba0 = __cpu_to_le32(offset);
fcfd9599 1318 map->blocks_per_member = __cpu_to_le32(info_to_blocks_per_member(info));
ef6ffade
DW
1319 map->blocks_per_strip = __cpu_to_le16(info_to_blocks_per_strip(info));
1320 map->num_data_stripes = __cpu_to_le32(info_to_num_data_stripes(info));
c2c087e6
DW
1321 map->map_state = info->level ? IMSM_T_STATE_UNINITIALIZED :
1322 IMSM_T_STATE_NORMAL;
ef6ffade
DW
1323
1324 if (info->level == 1 && info->raid_disks > 2) {
1325 fprintf(stderr, Name": imsm does not support more than 2 disks"
1326 "in a raid1 volume\n");
1327 return 0;
1328 }
c2c087e6
DW
1329 if (info->level == 10)
1330 map->raid_level = 1;
1331 else
1332 map->raid_level = info->level;
ef6ffade 1333
c2c087e6
DW
1334 map->num_members = info->raid_disks;
1335 for (i = 0; i < map->num_members; i++) {
1336 /* initialized in add_to_super */
1337 map->disk_ord_tbl[i] = __cpu_to_le32(0);
1338 }
949c47a0
DW
1339 mpb->num_raid_devs++;
1340 super->dev_tbl[super->current_vol] = dev;
c2c087e6
DW
1341
1342 return 1;
cdddbdbc
DW
1343}
1344
bf5a934a
DW
1345static int init_super_imsm(struct supertype *st, mdu_array_info_t *info,
1346 unsigned long long size, char *name,
1347 char *homehost, int *uuid)
1348{
1349 /* This is primarily called by Create when creating a new array.
1350 * We will then get add_to_super called for each component, and then
1351 * write_init_super called to write it out to each device.
1352 * For IMSM, Create can create on fresh devices or on a pre-existing
1353 * array.
1354 * To create on a pre-existing array a different method will be called.
1355 * This one is just for fresh drives.
1356 */
1357 struct intel_super *super;
1358 struct imsm_super *mpb;
1359 size_t mpb_size;
1360
1361 if (!info) {
1362 st->sb = NULL;
1363 return 0;
1364 }
1365 if (st->sb)
1366 return init_super_imsm_volume(st, info, size, name, homehost,
1367 uuid);
1368
1369 super = alloc_super(1);
1370 if (!super)
1371 return 0;
1372 mpb_size = disks_to_mpb_size(info->nr_disks);
ef649044 1373 if (posix_memalign(&super->buf, 512, mpb_size) != 0) {
bf5a934a
DW
1374 free(super);
1375 return 0;
1376 }
ef649044 1377 mpb = super->buf;
bf5a934a
DW
1378 memset(mpb, 0, mpb_size);
1379
1380 memcpy(mpb->sig, MPB_SIGNATURE, strlen(MPB_SIGNATURE));
1381 memcpy(mpb->sig + strlen(MPB_SIGNATURE), MPB_VERSION_RAID5,
1382 strlen(MPB_VERSION_RAID5));
1383 mpb->mpb_size = mpb_size;
1384
bf5a934a
DW
1385 st->sb = super;
1386 return 1;
1387}
1388
1389static void add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk,
1390 int fd, char *devname)
1391{
1392 struct intel_super *super = st->sb;
bf5a934a
DW
1393 struct dl *dl;
1394 struct imsm_dev *dev;
1395 struct imsm_map *map;
1396 struct imsm_disk *disk;
1397 __u32 status;
1398
949c47a0 1399 dev = get_imsm_dev(super, super->current_vol);
bf5a934a
DW
1400 map = &dev->vol.map[0];
1401
1402 for (dl = super->disks; dl ; dl = dl->next)
1403 if (dl->major == dk->major &&
1404 dl->minor == dk->minor)
1405 break;
1406 if (!dl || ! (dk->state & (1<<MD_DISK_SYNC)))
1407 return;
1408
1409 map->disk_ord_tbl[dk->number] = __cpu_to_le32(dl->index);
1410
949c47a0 1411 disk = get_imsm_disk(super, dl->index);
bf5a934a
DW
1412 status = CONFIGURED_DISK | USABLE_DISK;
1413 disk->status = __cpu_to_le32(status);
1414}
1415
c2c087e6 1416static void add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
cdddbdbc
DW
1417 int fd, char *devname)
1418{
c2c087e6 1419 struct intel_super *super = st->sb;
949c47a0 1420 struct imsm_super *mpb = super->anchor;
c2c087e6
DW
1421 struct imsm_disk *disk;
1422 struct dl *dd;
1423 unsigned long long size;
1424 __u32 status, id;
1425 int rv;
1426 struct stat stb;
1427
bf5a934a
DW
1428 if (super->current_vol >= 0) {
1429 add_to_super_imsm_volume(st, dk, fd, devname);
1430 return;
1431 }
1432
c2c087e6
DW
1433 fstat(fd, &stb);
1434 dd = malloc(sizeof(*dd));
b9f594fe 1435 if (!dd) {
c2c087e6
DW
1436 fprintf(stderr,
1437 Name ": malloc failed %s:%d.\n", __func__, __LINE__);
1438 abort();
1439 }
1440 memset(dd, 0, sizeof(*dd));
1441 dd->major = major(stb.st_rdev);
1442 dd->minor = minor(stb.st_rdev);
b9f594fe 1443 dd->index = -1;
c2c087e6
DW
1444 dd->devname = devname ? strdup(devname) : NULL;
1445 dd->next = super->disks;
1446 dd->fd = fd;
1447 rv = imsm_read_serial(fd, devname, dd->serial);
1448 if (rv) {
1449 fprintf(stderr,
0030e8d6 1450 Name ": failed to retrieve scsi serial, aborting\n");
949c47a0 1451 free(dd);
0030e8d6 1452 abort();
c2c087e6
DW
1453 }
1454
1455 if (mpb->num_disks <= dk->number)
1456 mpb->num_disks = dk->number + 1;
1457
c2c087e6
DW
1458 get_dev_size(fd, NULL, &size);
1459 size /= 512;
1460 status = USABLE_DISK | SPARE_DISK;
b9f594fe
DW
1461 strcpy((char *) dd->disk.serial, (char *) dd->serial);
1462 dd->disk.total_blocks = __cpu_to_le32(size);
1463 dd->disk.status = __cpu_to_le32(status);
c2c087e6 1464 if (sysfs_disk_to_scsi_id(fd, &id) == 0)
b9f594fe 1465 dd->disk.scsi_id = __cpu_to_le32(id);
c2c087e6 1466 else
b9f594fe 1467 dd->disk.scsi_id = __cpu_to_le32(0);
c2c087e6
DW
1468
1469 /* update the family number if we are creating a container */
949c47a0
DW
1470 if (super->creating_imsm) {
1471 disk = __get_imsm_disk(mpb, dd->index);
b9f594fe 1472 *disk = dd->disk;
949c47a0
DW
1473 mpb->family_num = __cpu_to_le32(__gen_imsm_checksum(mpb));
1474 }
c2c087e6
DW
1475
1476 super->disks = dd;
cdddbdbc
DW
1477}
1478
c2c087e6
DW
1479static int store_imsm_mpb(int fd, struct intel_super *super);
1480
1481static int write_super_imsm(struct intel_super *super, int doclose)
cdddbdbc 1482{
949c47a0 1483 struct imsm_super *mpb = super->anchor;
c2c087e6
DW
1484 struct dl *d;
1485 __u32 generation;
1486 __u32 sum;
949c47a0 1487 int i;
cdddbdbc 1488
c2c087e6
DW
1489 /* 'generation' is incremented everytime the metadata is written */
1490 generation = __le32_to_cpu(mpb->generation_num);
1491 generation++;
1492 mpb->generation_num = __cpu_to_le32(generation);
1493
b9f594fe
DW
1494 for (d = super->disks; d; d = d->next)
1495 mpb->disk[d->index] = d->disk;
1496
949c47a0
DW
1497 for (i = 0; i < mpb->num_raid_devs; i++) {
1498 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
1499
1500 imsm_copy_dev(dev, super->dev_tbl[i]);
1501 }
1502
c2c087e6 1503 /* recalculate checksum */
949c47a0 1504 sum = __gen_imsm_checksum(mpb);
c2c087e6
DW
1505 mpb->check_sum = __cpu_to_le32(sum);
1506
1507 for (d = super->disks; d ; d = d->next) {
1508 if (store_imsm_mpb(d->fd, super)) {
1509 fprintf(stderr, "%s: failed for device %d:%d %s\n",
1510 __func__, d->major, d->minor, strerror(errno));
1511 return 0;
1512 }
1513 if (doclose) {
1514 close(d->fd);
1515 d->fd = -1;
1516 }
1517 }
1518
1519 return 1;
1520}
1521
1522static int write_init_super_imsm(struct supertype *st)
1523{
8273f55e
DW
1524 if (st->update_tail) {
1525 /* queue the recently created array as a metadata update */
1526 size_t len;
1527 struct imsm_update_create_array *u;
1528 struct intel_super *super = st->sb;
8273f55e 1529 struct imsm_dev *dev;
8273f55e
DW
1530 struct dl *d;
1531
1532 if (super->current_vol < 0 ||
949c47a0 1533 !(dev = get_imsm_dev(super, super->current_vol))) {
8273f55e
DW
1534 fprintf(stderr, "%s: could not determine sub-array\n",
1535 __func__);
1536 return 1;
1537 }
1538
1539
949c47a0 1540 len = sizeof(*u) - sizeof(*dev) + sizeof_imsm_dev(dev);
8273f55e
DW
1541 u = malloc(len);
1542 if (!u) {
1543 fprintf(stderr, "%s: failed to allocate update buffer\n",
1544 __func__);
1545 return 1;
1546 }
1547
1548 u->type = update_create_array;
1549 u->dev_idx = super->current_vol;
949c47a0 1550 imsm_copy_dev(&u->dev, dev);
8273f55e
DW
1551 append_metadata_update(st, u, len);
1552
1553 for (d = super->disks; d ; d = d->next) {
1554 close(d->fd);
1555 d->fd = -1;
1556 }
1557
1558 return 0;
1559 } else
1560 return write_super_imsm(st->sb, 1);
cdddbdbc
DW
1561}
1562
1563static int store_zero_imsm(struct supertype *st, int fd)
1564{
551c80c1 1565 unsigned long long dsize;
6416d527 1566 void *buf;
551c80c1
DW
1567
1568 get_dev_size(fd, NULL, &dsize);
1569
1570 /* first block is stored on second to last sector of the disk */
1571 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0)
1572 return 1;
1573
ad97895e
DW
1574 if (posix_memalign(&buf, 512, 512) != 0)
1575 return 1;
1576
eb7ea463
DW
1577 memset(buf, 0, 512);
1578 if (write(fd, buf, 512) != 512)
551c80c1 1579 return 1;
cdddbdbc
DW
1580 return 0;
1581}
1582
cdddbdbc
DW
1583static int validate_geometry_imsm_container(struct supertype *st, int level,
1584 int layout, int raiddisks, int chunk,
c2c087e6 1585 unsigned long long size, char *dev,
2c514b71
NB
1586 unsigned long long *freesize,
1587 int verbose)
cdddbdbc 1588{
c2c087e6
DW
1589 int fd;
1590 unsigned long long ldsize;
cdddbdbc 1591
c2c087e6
DW
1592 if (level != LEVEL_CONTAINER)
1593 return 0;
1594 if (!dev)
1595 return 1;
1596
1597 fd = open(dev, O_RDONLY|O_EXCL, 0);
1598 if (fd < 0) {
2c514b71
NB
1599 if (verbose)
1600 fprintf(stderr, Name ": imsm: Cannot open %s: %s\n",
1601 dev, strerror(errno));
c2c087e6
DW
1602 return 0;
1603 }
1604 if (!get_dev_size(fd, dev, &ldsize)) {
1605 close(fd);
1606 return 0;
1607 }
1608 close(fd);
1609
1610 *freesize = avail_size_imsm(st, ldsize >> 9);
1611
1612 return 1;
cdddbdbc
DW
1613}
1614
c2c087e6
DW
1615/* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
1616 * FIX ME add ahci details
1617 */
8b353278
DW
1618static int validate_geometry_imsm_volume(struct supertype *st, int level,
1619 int layout, int raiddisks, int chunk,
c2c087e6 1620 unsigned long long size, char *dev,
2c514b71
NB
1621 unsigned long long *freesize,
1622 int verbose)
cdddbdbc 1623{
c2c087e6
DW
1624 struct stat stb;
1625 struct intel_super *super = st->sb;
1626 struct dl *dl;
1627 unsigned long long pos = 0;
1628 unsigned long long maxsize;
1629 struct extent *e;
1630 int i;
cdddbdbc 1631
c2c087e6
DW
1632 if (level == LEVEL_CONTAINER)
1633 return 0;
1634
1635 if (level == 1 && raiddisks > 2) {
2c514b71
NB
1636 if (verbose)
1637 fprintf(stderr, Name ": imsm does not support more "
1638 "than 2 in a raid1 configuration\n");
c2c087e6
DW
1639 return 0;
1640 }
1641
1642 /* We must have the container info already read in. */
1643 if (!super)
1644 return 0;
1645
1646 if (!dev) {
1647 /* General test: make sure there is space for
2da8544a
DW
1648 * 'raiddisks' device extents of size 'size' at a given
1649 * offset
c2c087e6
DW
1650 */
1651 unsigned long long minsize = size*2 /* convert to blocks */;
2da8544a 1652 unsigned long long start_offset = ~0ULL;
c2c087e6
DW
1653 int dcnt = 0;
1654 if (minsize == 0)
1655 minsize = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
1656 for (dl = super->disks; dl ; dl = dl->next) {
1657 int found = 0;
1658
bf5a934a 1659 pos = 0;
c2c087e6
DW
1660 i = 0;
1661 e = get_extents(super, dl);
1662 if (!e) continue;
1663 do {
1664 unsigned long long esize;
1665 esize = e[i].start - pos;
1666 if (esize >= minsize)
1667 found = 1;
2da8544a
DW
1668 if (found && start_offset == ~0ULL) {
1669 start_offset = pos;
1670 break;
1671 } else if (found && pos != start_offset) {
1672 found = 0;
1673 break;
1674 }
c2c087e6
DW
1675 pos = e[i].start + e[i].size;
1676 i++;
1677 } while (e[i-1].size);
1678 if (found)
1679 dcnt++;
1680 free(e);
1681 }
1682 if (dcnt < raiddisks) {
2c514b71
NB
1683 if (verbose)
1684 fprintf(stderr, Name ": imsm: Not enough "
1685 "devices with space for this array "
1686 "(%d < %d)\n",
1687 dcnt, raiddisks);
c2c087e6
DW
1688 return 0;
1689 }
1690 return 1;
1691 }
1692 /* This device must be a member of the set */
1693 if (stat(dev, &stb) < 0)
1694 return 0;
1695 if ((S_IFMT & stb.st_mode) != S_IFBLK)
1696 return 0;
1697 for (dl = super->disks ; dl ; dl = dl->next) {
1698 if (dl->major == major(stb.st_rdev) &&
1699 dl->minor == minor(stb.st_rdev))
1700 break;
1701 }
1702 if (!dl) {
2c514b71
NB
1703 if (verbose)
1704 fprintf(stderr, Name ": %s is not in the "
1705 "same imsm set\n", dev);
c2c087e6
DW
1706 return 0;
1707 }
1708 e = get_extents(super, dl);
1709 maxsize = 0;
1710 i = 0;
1711 if (e) do {
1712 unsigned long long esize;
1713 esize = e[i].start - pos;
1714 if (esize >= maxsize)
1715 maxsize = esize;
1716 pos = e[i].start + e[i].size;
1717 i++;
1718 } while (e[i-1].size);
1719 *freesize = maxsize;
1720
1721 return 1;
cdddbdbc
DW
1722}
1723
604b746f
JD
1724int imsm_bbm_log_size(struct imsm_super *mpb)
1725{
1726 return __le32_to_cpu(mpb->bbm_log_size);
1727}
1728
bf5a934a
DW
1729static int validate_geometry_imsm(struct supertype *st, int level, int layout,
1730 int raiddisks, int chunk, unsigned long long size,
1731 char *dev, unsigned long long *freesize,
1732 int verbose)
1733{
1734 int fd, cfd;
1735 struct mdinfo *sra;
1736
1737 /* if given unused devices create a container
1738 * if given given devices in a container create a member volume
1739 */
1740 if (level == LEVEL_CONTAINER) {
1741 /* Must be a fresh device to add to a container */
1742 return validate_geometry_imsm_container(st, level, layout,
1743 raiddisks, chunk, size,
1744 dev, freesize,
1745 verbose);
1746 }
1747
1748 if (st->sb) {
1749 /* creating in a given container */
1750 return validate_geometry_imsm_volume(st, level, layout,
1751 raiddisks, chunk, size,
1752 dev, freesize, verbose);
1753 }
1754
1755 /* limit creation to the following levels */
1756 if (!dev)
1757 switch (level) {
1758 case 0:
1759 case 1:
1760 case 10:
1761 case 5:
1762 break;
1763 default:
1764 return 1;
1765 }
1766
1767 /* This device needs to be a device in an 'imsm' container */
1768 fd = open(dev, O_RDONLY|O_EXCL, 0);
1769 if (fd >= 0) {
1770 if (verbose)
1771 fprintf(stderr,
1772 Name ": Cannot create this array on device %s\n",
1773 dev);
1774 close(fd);
1775 return 0;
1776 }
1777 if (errno != EBUSY || (fd = open(dev, O_RDONLY, 0)) < 0) {
1778 if (verbose)
1779 fprintf(stderr, Name ": Cannot open %s: %s\n",
1780 dev, strerror(errno));
1781 return 0;
1782 }
1783 /* Well, it is in use by someone, maybe an 'imsm' container. */
1784 cfd = open_container(fd);
1785 if (cfd < 0) {
1786 close(fd);
1787 if (verbose)
1788 fprintf(stderr, Name ": Cannot use %s: It is busy\n",
1789 dev);
1790 return 0;
1791 }
1792 sra = sysfs_read(cfd, 0, GET_VERSION);
1793 close(fd);
1794 if (sra && sra->array.major_version == -1 &&
1795 strcmp(sra->text_version, "imsm") == 0) {
1796 /* This is a member of a imsm container. Load the container
1797 * and try to create a volume
1798 */
1799 struct intel_super *super;
1800
1801 if (load_super_imsm_all(st, cfd, (void **) &super, NULL, 1) == 0) {
1802 st->sb = super;
1803 st->container_dev = fd2devnum(cfd);
1804 close(cfd);
1805 return validate_geometry_imsm_volume(st, level, layout,
1806 raiddisks, chunk,
1807 size, dev,
1808 freesize, verbose);
1809 }
1810 close(cfd);
1811 } else /* may belong to another container */
1812 return 0;
1813
1814 return 1;
1815}
1816
cdddbdbc
DW
1817static struct mdinfo *container_content_imsm(struct supertype *st)
1818{
4f5bc454
DW
1819 /* Given a container loaded by load_super_imsm_all,
1820 * extract information about all the arrays into
1821 * an mdinfo tree.
1822 *
1823 * For each imsm_dev create an mdinfo, fill it in,
1824 * then look for matching devices in super->disks
1825 * and create appropriate device mdinfo.
1826 */
1827 struct intel_super *super = st->sb;
949c47a0 1828 struct imsm_super *mpb = super->anchor;
4f5bc454
DW
1829 struct mdinfo *rest = NULL;
1830 int i;
cdddbdbc 1831
604b746f
JD
1832 /* do not assemble arrays that might have bad blocks */
1833 if (imsm_bbm_log_size(super->anchor)) {
1834 fprintf(stderr, Name ": BBM log found in metadata. "
1835 "Cannot activate array(s).\n");
1836 return NULL;
1837 }
1838
4f5bc454 1839 for (i = 0; i < mpb->num_raid_devs; i++) {
949c47a0 1840 struct imsm_dev *dev = get_imsm_dev(super, i);
4f5bc454
DW
1841 struct imsm_vol *vol = &dev->vol;
1842 struct imsm_map *map = vol->map;
1843 struct mdinfo *this;
4f5bc454
DW
1844 int slot;
1845
1846 this = malloc(sizeof(*this));
1847 memset(this, 0, sizeof(*this));
1848 this->next = rest;
1849 rest = this;
1850
4f5bc454
DW
1851 this->array.level = get_imsm_raid_level(map);
1852 this->array.raid_disks = map->num_members;
c2c087e6 1853 this->array.layout = imsm_level_to_layout(this->array.level);
4f5bc454
DW
1854 this->array.md_minor = -1;
1855 this->array.ctime = 0;
1856 this->array.utime = 0;
1857 this->array.chunk_size = __le16_to_cpu(map->blocks_per_strip) << 9;
1858 this->array.state = !vol->dirty;
1859 this->container_member = i;
0fd5c350
DW
1860 if (map->map_state == IMSM_T_STATE_UNINITIALIZED || dev->vol.dirty)
1861 this->resync_start = 0;
1862 else
1863 this->resync_start = ~0ULL;
1864
4f5bc454
DW
1865 strncpy(this->name, (char *) dev->volume, MAX_RAID_SERIAL_LEN);
1866 this->name[MAX_RAID_SERIAL_LEN] = 0;
1867
159c3a1a
NB
1868 sprintf(this->text_version, "/%s/%d",
1869 devnum2devname(st->container_dev),
1870 this->container_member);
1871
4f5bc454
DW
1872 memset(this->uuid, 0, sizeof(this->uuid));
1873
f54e6321 1874 this->component_size = __le32_to_cpu(map->blocks_per_member);
4f5bc454
DW
1875
1876 for (slot = 0 ; slot < map->num_members; slot++) {
4f5bc454
DW
1877 struct mdinfo *info_d;
1878 struct dl *d;
1879 int idx;
1880 __u32 s;
1881
1882 idx = __le32_to_cpu(map->disk_ord_tbl[slot] & ~(0xff << 24));
1883 for (d = super->disks; d ; d = d->next)
1884 if (d->index == idx)
1885 break;
1886
1887 if (d == NULL)
1888 break; /* shouldn't this be continue ?? */
1889
1890 info_d = malloc(sizeof(*info_d));
1891 if (!info_d)
1892 break; /* ditto ?? */
1893 memset(info_d, 0, sizeof(*info_d));
1894 info_d->next = this->devs;
1895 this->devs = info_d;
1896
b9f594fe 1897 s = __le32_to_cpu(d->disk.status);
4f5bc454
DW
1898
1899 info_d->disk.number = d->index;
1900 info_d->disk.major = d->major;
1901 info_d->disk.minor = d->minor;
1902 info_d->disk.raid_disk = slot;
1903 info_d->disk.state = s & CONFIGURED_DISK ? (1 << MD_DISK_ACTIVE) : 0;
1904 info_d->disk.state |= s & FAILED_DISK ? (1 << MD_DISK_FAULTY) : 0;
1905 info_d->disk.state |= s & USABLE_DISK ? (1 << MD_DISK_SYNC) : 0;
1906
1907 this->array.working_disks++;
1908
1909 info_d->events = __le32_to_cpu(mpb->generation_num);
1910 info_d->data_offset = __le32_to_cpu(map->pba_of_lba0);
1911 info_d->component_size = __le32_to_cpu(map->blocks_per_member);
1912 if (d->devname)
1913 strcpy(info_d->name, d->devname);
1914 }
1915 }
1916
1917 return rest;
cdddbdbc
DW
1918}
1919
845dea95 1920
cba0191b
NB
1921static int imsm_open_new(struct supertype *c, struct active_array *a,
1922 char *inst)
845dea95 1923{
0372d5a2 1924 struct intel_super *super = c->sb;
949c47a0 1925 struct imsm_super *mpb = super->anchor;
0372d5a2 1926
949c47a0 1927 if (atoi(inst) >= mpb->num_raid_devs) {
0372d5a2
DW
1928 fprintf(stderr, "%s: subarry index %d, out of range\n",
1929 __func__, atoi(inst));
1930 return -ENODEV;
1931 }
1932
4e6e574a 1933 dprintf("imsm: open_new %s\n", inst);
cba0191b 1934 a->info.container_member = atoi(inst);
845dea95
NB
1935 return 0;
1936}
1937
949c47a0 1938static __u8 imsm_check_degraded(struct intel_super *super, int n, int failed)
c2a1e7da 1939{
949c47a0 1940 struct imsm_dev *dev = get_imsm_dev(super, n);
c2a1e7da
DW
1941 struct imsm_map *map = dev->vol.map;
1942
1943 if (!failed)
1944 return map->map_state;
1945
1946 switch (get_imsm_raid_level(map)) {
1947 case 0:
1948 return IMSM_T_STATE_FAILED;
1949 break;
1950 case 1:
1951 if (failed < map->num_members)
1952 return IMSM_T_STATE_DEGRADED;
1953 else
1954 return IMSM_T_STATE_FAILED;
1955 break;
1956 case 10:
1957 {
1958 /**
1959 * check to see if any mirrors have failed,
1960 * otherwise we are degraded
1961 */
1962 int device_per_mirror = 2; /* FIXME is this always the case?
1963 * and are they always adjacent?
1964 */
1965 int failed = 0;
1966 int i;
1967
1968 for (i = 0; i < map->num_members; i++) {
1969 int idx = get_imsm_disk_idx(map, i);
949c47a0 1970 struct imsm_disk *disk = get_imsm_disk(super, idx);
c2a1e7da
DW
1971
1972 if (__le32_to_cpu(disk->status) & FAILED_DISK)
1973 failed++;
1974
1975 if (failed >= device_per_mirror)
1976 return IMSM_T_STATE_FAILED;
1977
1978 /* reset 'failed' for next mirror set */
1979 if (!((i + 1) % device_per_mirror))
1980 failed = 0;
1981 }
1982
1983 return IMSM_T_STATE_DEGRADED;
1984 }
1985 case 5:
1986 if (failed < 2)
1987 return IMSM_T_STATE_DEGRADED;
1988 else
1989 return IMSM_T_STATE_FAILED;
1990 break;
1991 default:
1992 break;
1993 }
1994
1995 return map->map_state;
1996}
1997
949c47a0 1998static int imsm_count_failed(struct intel_super *super, struct imsm_map *map)
c2a1e7da
DW
1999{
2000 int i;
2001 int failed = 0;
2002 struct imsm_disk *disk;
2003
2004 for (i = 0; i < map->num_members; i++) {
2005 int idx = get_imsm_disk_idx(map, i);
2006
949c47a0 2007 disk = get_imsm_disk(super, idx);
c2a1e7da
DW
2008 if (__le32_to_cpu(disk->status) & FAILED_DISK)
2009 failed++;
2010 }
2011
2012 return failed;
845dea95
NB
2013}
2014
a862209d
DW
2015static void imsm_set_array_state(struct active_array *a, int consistent)
2016{
2017 int inst = a->info.container_member;
2018 struct intel_super *super = a->container->sb;
949c47a0 2019 struct imsm_dev *dev = get_imsm_dev(super, inst);
a862209d
DW
2020 struct imsm_map *map = &dev->vol.map[0];
2021 int dirty = !consistent;
2022 int failed;
2023 __u8 map_state;
2024
2025 if (a->resync_start == ~0ULL) {
949c47a0
DW
2026 failed = imsm_count_failed(super, map);
2027 map_state = imsm_check_degraded(super, inst, failed);
115c3803 2028 /* complete recovery or initial resync */
a862209d
DW
2029 if (!failed)
2030 map_state = IMSM_T_STATE_NORMAL;
2031 if (map->map_state != map_state) {
4e6e574a 2032 dprintf("imsm: map_state %d: %d\n",
a862209d
DW
2033 inst, map_state);
2034 map->map_state = map_state;
2035 super->updates_pending++;
2036 }
a862209d 2037
115c3803
DW
2038 /* complete resync */
2039 if (!dirty && dev->vol.dirty) {
2040 dprintf("imsm: mark 'clean'\n");
2041 dev->vol.dirty = 0;
2042 super->updates_pending++;
2043
2044 }
2045 }
a862209d 2046
115c3803
DW
2047 /* mark dirty */
2048 if (dirty && !dev->vol.dirty) {
2049 dprintf("imsm: mark 'dirty' (%llu)\n", a->resync_start);
2050 dev->vol.dirty = 1;
a862209d
DW
2051 super->updates_pending++;
2052 }
2053}
2054
8d45d196 2055static void imsm_set_disk(struct active_array *a, int n, int state)
845dea95 2056{
8d45d196
DW
2057 int inst = a->info.container_member;
2058 struct intel_super *super = a->container->sb;
949c47a0 2059 struct imsm_dev *dev = get_imsm_dev(super, inst);
8d45d196
DW
2060 struct imsm_map *map = dev->vol.map;
2061 struct imsm_disk *disk;
2062 __u32 status;
2063 int failed = 0;
2064 int new_failure = 0;
2065
2066 if (n > map->num_members)
2067 fprintf(stderr, "imsm: set_disk %d out of range 0..%d\n",
2068 n, map->num_members - 1);
2069
2070 if (n < 0)
2071 return;
2072
4e6e574a 2073 dprintf("imsm: set_disk %d:%x\n", n, state);
8d45d196 2074
949c47a0 2075 disk = get_imsm_disk(super, get_imsm_disk_idx(map, n));
8d45d196 2076
5802a811 2077 /* check for new failures */
8d45d196
DW
2078 status = __le32_to_cpu(disk->status);
2079 if ((state & DS_FAULTY) && !(status & FAILED_DISK)) {
2080 status |= FAILED_DISK;
2081 disk->status = __cpu_to_le32(status);
2082 new_failure = 1;
5802a811 2083 super->updates_pending++;
8d45d196
DW
2084 }
2085
5802a811 2086 /* the number of failures have changed, count up 'failed' to determine
8d45d196
DW
2087 * degraded / failed status
2088 */
2089 if (new_failure && map->map_state != IMSM_T_STATE_FAILED)
949c47a0 2090 failed = imsm_count_failed(super, map);
8d45d196 2091
5802a811 2092 /* determine map_state based on failed or in_sync count */
8d45d196 2093 if (failed)
949c47a0 2094 map->map_state = imsm_check_degraded(super, inst, failed);
5802a811
DW
2095 else if (map->map_state == IMSM_T_STATE_DEGRADED) {
2096 struct mdinfo *d;
2097 int working = 0;
8d45d196 2098
5802a811
DW
2099 for (d = a->info.devs ; d ; d = d->next)
2100 if (d->curr_state & DS_INSYNC)
2101 working++;
2102
2103 if (working == a->info.array.raid_disks) {
2104 map->map_state = IMSM_T_STATE_NORMAL;
2105 super->updates_pending++;
2106 }
2107 }
845dea95
NB
2108}
2109
c2a1e7da
DW
2110static int store_imsm_mpb(int fd, struct intel_super *super)
2111{
949c47a0 2112 struct imsm_super *mpb = super->anchor;
c2a1e7da
DW
2113 __u32 mpb_size = __le32_to_cpu(mpb->mpb_size);
2114 unsigned long long dsize;
2115 unsigned long long sectors;
2116
2117 get_dev_size(fd, NULL, &dsize);
2118
272f648f
DW
2119 if (mpb_size > 512) {
2120 /* -1 to account for anchor */
2121 sectors = mpb_sectors(mpb) - 1;
c2a1e7da 2122
272f648f
DW
2123 /* write the extended mpb to the sectors preceeding the anchor */
2124 if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0)
2125 return 1;
c2a1e7da 2126
99e29264 2127 if (write(fd, super->buf + 512, 512 * sectors) != 512 * sectors)
272f648f
DW
2128 return 1;
2129 }
c2a1e7da 2130
272f648f
DW
2131 /* first block is stored on second to last sector of the disk */
2132 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0)
c2a1e7da
DW
2133 return 1;
2134
272f648f 2135 if (write(fd, super->buf, 512) != 512)
c2a1e7da
DW
2136 return 1;
2137
c2a1e7da
DW
2138 return 0;
2139}
2140
2e735d19 2141static void imsm_sync_metadata(struct supertype *container)
845dea95 2142{
2e735d19 2143 struct intel_super *super = container->sb;
c2a1e7da
DW
2144
2145 if (!super->updates_pending)
2146 return;
2147
c2c087e6 2148 write_super_imsm(super, 0);
c2a1e7da
DW
2149
2150 super->updates_pending = 0;
845dea95
NB
2151}
2152
88758e9d
DW
2153static struct mdinfo *imsm_activate_spare(struct active_array *a,
2154 struct metadata_update **updates)
2155{
2156 /**
2157 * Take a device that is marked spare in the metadata and use it to
2158 * replace a failed/vacant slot in an array. There may be a case where
2159 * a device is failed in one array but active in a second.
2160 * imsm_process_update catches this case and does not clear the SPARE_DISK
2161 * flag, allowing the second array to start using the device on failure.
2162 * SPARE_DISK is cleared when all arrays are using a device.
2163 *
2164 * FIXME: is this a valid use of SPARE_DISK?
2165 */
2166
2167 struct intel_super *super = a->container->sb;
88758e9d 2168 int inst = a->info.container_member;
949c47a0 2169 struct imsm_dev *dev = get_imsm_dev(super, inst);
88758e9d
DW
2170 struct imsm_map *map = dev->vol.map;
2171 int failed = a->info.array.raid_disks;
2172 struct mdinfo *rv = NULL;
2173 struct mdinfo *d;
2174 struct mdinfo *di;
2175 struct metadata_update *mu;
2176 struct dl *dl;
2177 struct imsm_update_activate_spare *u;
2178 int num_spares = 0;
2179 int i;
2180
2181 for (d = a->info.devs ; d ; d = d->next) {
2182 if ((d->curr_state & DS_FAULTY) &&
2183 d->state_fd >= 0)
2184 /* wait for Removal to happen */
2185 return NULL;
2186 if (d->state_fd >= 0)
2187 failed--;
2188 }
2189
2190 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
2191 inst, failed, a->info.array.raid_disks, a->info.array.level);
949c47a0 2192 if (imsm_check_degraded(super, inst, failed) != IMSM_T_STATE_DEGRADED)
88758e9d
DW
2193 return NULL;
2194
2195 /* For each slot, if it is not working, find a spare */
2196 dl = super->disks;
2197 for (i = 0; i < a->info.array.raid_disks; i++) {
2198 for (d = a->info.devs ; d ; d = d->next)
2199 if (d->disk.raid_disk == i)
2200 break;
2201 dprintf("found %d: %p %x\n", i, d, d?d->curr_state:0);
2202 if (d && (d->state_fd >= 0))
2203 continue;
2204
2205 /* OK, this device needs recovery. Find a spare */
2206 for ( ; dl ; dl = dl->next) {
2207 unsigned long long esize;
2208 unsigned long long pos;
2209 struct mdinfo *d2;
2210 struct extent *ex;
2211 struct imsm_disk *disk;
2212 int j;
2213 int found;
5257ce36 2214 __u32 array_start;
88758e9d
DW
2215
2216 /* If in this array, skip */
2217 for (d2 = a->info.devs ; d2 ; d2 = d2->next)
2218 if (d2->disk.major == dl->major &&
2219 d2->disk.minor == dl->minor) {
2220 dprintf("%x:%x already in array\n", dl->major, dl->minor);
2221 break;
2222 }
2223 if (d2)
2224 continue;
2225
2226 /* is this unused device marked as a spare? */
949c47a0 2227 disk = get_imsm_disk(super, dl->index);
88758e9d
DW
2228 if (!(__le32_to_cpu(disk->status) & SPARE_DISK))
2229 continue;
2230
2231 /* We are allowed to use this device - is there space?
2232 * We need a->info.component_size sectors */
2233 ex = get_extents(super, dl);
2234 if (!ex) {
2235 dprintf("cannot get extents\n");
2236 continue;
2237 }
2238 found = 0;
2239 j = 0;
2240 pos = 0;
5257ce36 2241 array_start = __le32_to_cpu(map->pba_of_lba0);
88758e9d
DW
2242
2243 do {
5257ce36
DW
2244 /* check that we can start at pba_of_lba0 with
2245 * a->info.component_size of space
2246 */
88758e9d 2247 esize = ex[j].start - pos;
5257ce36
DW
2248 if (array_start >= pos &&
2249 array_start + a->info.component_size < ex[j].start) {
2250 found = 1;
88758e9d
DW
2251 break;
2252 }
5257ce36
DW
2253 pos = ex[j].start + ex[j].size;
2254 j++;
2255
2256 } while (ex[j-1].size);
88758e9d
DW
2257
2258 free(ex);
2259 if (!found) {
2260 dprintf("%x:%x does not have %llu at %d\n",
2261 dl->major, dl->minor,
2262 a->info.component_size,
2263 __le32_to_cpu(map->pba_of_lba0));
2264 /* No room */
2265 continue;
2266 }
2267
5257ce36 2268 /* found a usable disk with enough space */
88758e9d
DW
2269 di = malloc(sizeof(*di));
2270 memset(di, 0, sizeof(*di));
2271 di->disk.number = dl->index;
2272 di->disk.raid_disk = i;
2273 di->disk.major = dl->major;
2274 di->disk.minor = dl->minor;
2275 di->disk.state = 0;
5257ce36 2276 di->data_offset = array_start;
88758e9d
DW
2277 di->component_size = a->info.component_size;
2278 di->container_member = inst;
2279 di->next = rv;
2280 rv = di;
2281 num_spares++;
2282 dprintf("%x:%x to be %d at %llu\n", dl->major, dl->minor,
2283 i, pos);
2284
2285 break;
2286 }
2287 }
2288
2289 if (!rv)
2290 /* No spares found */
2291 return rv;
2292 /* Now 'rv' has a list of devices to return.
2293 * Create a metadata_update record to update the
2294 * disk_ord_tbl for the array
2295 */
2296 mu = malloc(sizeof(*mu));
2297 mu->buf = malloc(sizeof(struct imsm_update_activate_spare) * num_spares);
2298 mu->space = NULL;
2299 mu->len = sizeof(struct imsm_update_activate_spare) * num_spares;
2300 mu->next = *updates;
2301 u = (struct imsm_update_activate_spare *) mu->buf;
2302
2303 for (di = rv ; di ; di = di->next) {
2304 u->type = update_activate_spare;
2305 u->disk_idx = di->disk.number;
2306 u->slot = di->disk.raid_disk;
2307 u->array = inst;
2308 u->next = u + 1;
2309 u++;
2310 }
2311 (u-1)->next = NULL;
2312 *updates = mu;
2313
2314 return rv;
2315}
2316
e8319a19
DW
2317static int weight(unsigned int field)
2318{
2319 int weight;
2320
2321 for (weight = 0; field; weight++)
2322 field &= field - 1;
2323
2324 return weight;
2325}
2326
8273f55e
DW
2327static int disks_overlap(struct imsm_map *m1, struct imsm_map *m2)
2328{
2329 int i;
2330 int j;
2331 int idx;
2332
2333 for (i = 0; i < m1->num_members; i++) {
2334 idx = get_imsm_disk_idx(m1, i);
2335 for (j = 0; j < m2->num_members; j++)
2336 if (idx == get_imsm_disk_idx(m2, j))
2337 return 1;
2338 }
2339
2340 return 0;
2341}
2342
e8319a19
DW
2343static void imsm_process_update(struct supertype *st,
2344 struct metadata_update *update)
2345{
2346 /**
2347 * crack open the metadata_update envelope to find the update record
2348 * update can be one of:
2349 * update_activate_spare - a spare device has replaced a failed
2350 * device in an array, update the disk_ord_tbl. If this disk is
2351 * present in all member arrays then also clear the SPARE_DISK
2352 * flag
2353 */
2354 struct intel_super *super = st->sb;
949c47a0 2355 struct imsm_super *mpb = super->anchor;
e8319a19
DW
2356 enum imsm_update_type type = *(enum imsm_update_type *) update->buf;
2357
2358 switch (type) {
2359 case update_activate_spare: {
2360 struct imsm_update_activate_spare *u = (void *) update->buf;
949c47a0 2361 struct imsm_dev *dev = get_imsm_dev(super, u->array);
e8319a19
DW
2362 struct imsm_map *map = &dev->vol.map[0];
2363 struct active_array *a;
2364 struct imsm_disk *disk;
2365 __u32 status;
2366 struct dl *dl;
2367 struct mdinfo *d;
2368 unsigned int members;
2369 unsigned int found;
2370 int victim;
2371 int i;
2372
2373 for (dl = super->disks; dl; dl = dl->next)
2374 if (dl->index == u->disk_idx)
2375 break;
2376
2377 if (!dl) {
2378 fprintf(stderr, "error: imsm_activate_spare passed "
2379 "an unknown disk_idx: %d\n", u->disk_idx);
2380 return;
2381 }
2382
2383 super->updates_pending++;
2384
2385 victim = get_imsm_disk_idx(map, u->slot);
2386 map->disk_ord_tbl[u->slot] = __cpu_to_le32(u->disk_idx);
949c47a0 2387 disk = get_imsm_disk(super, u->disk_idx);
e8319a19
DW
2388 status = __le32_to_cpu(disk->status);
2389 status |= CONFIGURED_DISK;
2390 disk->status = __cpu_to_le32(status);
2391
2392 /* map unique/live arrays using the spare */
2393 members = 0;
2394 found = 0;
2395 for (a = st->arrays; a; a = a->next) {
2396 int inst = a->info.container_member;
2397
949c47a0 2398 dev = get_imsm_dev(super, inst);
e8319a19
DW
2399 map = &dev->vol.map[0];
2400 if (map->raid_level > 0)
2401 members |= 1 << inst;
2402 for (d = a->info.devs; d; d = d->next)
2403 if (d->disk.major == dl->major &&
2404 d->disk.minor == dl->minor)
2405 found |= 1 << inst;
2406 }
2407
2408 /* until all arrays that can absorb this disk have absorbed
2409 * this disk it can still be considered a spare
2410 */
2411 if (weight(found) >= weight(members)) {
2412 status = __le32_to_cpu(disk->status);
2413 status &= ~SPARE_DISK;
2414 disk->status = __cpu_to_le32(status);
2415 }
2416
2417 /* count arrays using the victim in the metadata */
2418 found = 0;
2419 for (a = st->arrays; a ; a = a->next) {
949c47a0 2420 dev = get_imsm_dev(super, a->info.container_member);
e8319a19
DW
2421 map = &dev->vol.map[0];
2422 for (i = 0; i < map->num_members; i++)
2423 if (victim == get_imsm_disk_idx(map, i))
2424 found++;
2425 }
2426
2427 /* clear some flags if the victim is no longer being
2428 * utilized anywhere
2429 */
949c47a0 2430 disk = get_imsm_disk(super, victim);
e8319a19
DW
2431 if (!found) {
2432 status = __le32_to_cpu(disk->status);
2433 status &= ~(CONFIGURED_DISK | USABLE_DISK);
2434 disk->status = __cpu_to_le32(status);
2435 }
8273f55e
DW
2436 break;
2437 }
2438 case update_create_array: {
2439 /* someone wants to create a new array, we need to be aware of
2440 * a few races/collisions:
2441 * 1/ 'Create' called by two separate instances of mdadm
2442 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
2443 * devices that have since been assimilated via
2444 * activate_spare.
2445 * In the event this update can not be carried out mdadm will
2446 * (FIX ME) notice that its update did not take hold.
2447 */
2448 struct imsm_update_create_array *u = (void *) update->buf;
2449 struct imsm_dev *dev;
2450 struct imsm_map *map, *new_map;
2451 unsigned long long start, end;
2452 unsigned long long new_start, new_end;
2453 int i;
2454 int overlap = 0;
2455
2456 /* handle racing creates: first come first serve */
2457 if (u->dev_idx < mpb->num_raid_devs) {
2458 dprintf("%s: subarray %d already defined\n",
2459 __func__, u->dev_idx);
2460 return;
2461 }
2462
2463 /* check update is next in sequence */
2464 if (u->dev_idx != mpb->num_raid_devs) {
2465 dprintf("%s: can not create arrays out of sequence\n",
2466 __func__);
2467 return;
2468 }
2469
2470 new_map = &u->dev.vol.map[0];
2471 new_start = __le32_to_cpu(new_map->pba_of_lba0);
2472 new_end = new_start + __le32_to_cpu(new_map->blocks_per_member);
2473
2474 /* handle activate_spare versus create race:
2475 * check to make sure that overlapping arrays do not include
2476 * overalpping disks
2477 */
2478 for (i = 0; i < mpb->num_raid_devs; i++) {
949c47a0 2479 dev = get_imsm_dev(super, i);
8273f55e
DW
2480 map = &dev->vol.map[0];
2481 start = __le32_to_cpu(map->pba_of_lba0);
2482 end = start + __le32_to_cpu(map->blocks_per_member);
2483 if ((new_start >= start && new_start <= end) ||
2484 (start >= new_start && start <= new_end))
2485 overlap = 1;
2486 if (overlap && disks_overlap(map, new_map)) {
2487 dprintf("%s: arrays overlap\n", __func__);
2488 return;
2489 }
2490 }
2491 /* check num_members sanity */
2492 if (new_map->num_members > mpb->num_disks) {
2493 dprintf("%s: num_disks out of range\n", __func__);
2494 return;
2495 }
2496
949c47a0
DW
2497 /* check that prepare update was successful */
2498 if (!update->space) {
2499 dprintf("%s: prepare update failed\n", __func__);
2500 return;
2501 }
2502
8273f55e 2503 super->updates_pending++;
949c47a0
DW
2504 dev = update->space;
2505 update->space = NULL;
2506 imsm_copy_dev(dev, &u->dev);
2507 super->dev_tbl[u->dev_idx] = dev;
8273f55e 2508 mpb->num_raid_devs++;
8273f55e
DW
2509
2510 /* fix up flags, if arrays overlap then the drives can not be
2511 * spares
2512 */
2513 for (i = 0; i < map->num_members; i++) {
2514 struct imsm_disk *disk;
2515 __u32 status;
2516
949c47a0 2517 disk = get_imsm_disk(super, get_imsm_disk_idx(map, i));
8273f55e
DW
2518 status = __le32_to_cpu(disk->status);
2519 status |= CONFIGURED_DISK;
2520 if (overlap)
2521 status &= ~SPARE_DISK;
2522 disk->status = __cpu_to_le32(status);
2523 }
2524 break;
e8319a19
DW
2525 }
2526 }
2527}
88758e9d 2528
8273f55e
DW
2529static void imsm_prepare_update(struct supertype *st,
2530 struct metadata_update *update)
2531{
949c47a0
DW
2532 /**
2533 * Allocate space to hold new disk entries, raid-device entries or a
2534 * new mpb if necessary. We currently maintain an mpb large enough to
2535 * hold 2 subarrays for the given number of disks. This may not be
2536 * sufficient when reshaping.
8273f55e
DW
2537 *
2538 * FIX ME handle the reshape case.
2539 *
2540 * The monitor will be able to safely change super->mpb by arranging
2541 * for it to be freed in check_update_queue(). I.e. the monitor thread
2542 * will start using the new pointer and the manager can continue to use
2543 * the old value until check_update_queue() runs.
2544 */
949c47a0
DW
2545 enum imsm_update_type type = *(enum imsm_update_type *) update->buf;
2546
2547 switch (type) {
2548 case update_create_array: {
2549 struct imsm_update_create_array *u = (void *) update->buf;
2550 size_t len = sizeof_imsm_dev(&u->dev);
2551
2552 update->space = malloc(len);
2553 break;
2554 default:
2555 break;
2556 }
2557 }
8273f55e
DW
2558
2559 return;
2560}
2561
cdddbdbc
DW
2562struct superswitch super_imsm = {
2563#ifndef MDASSEMBLE
2564 .examine_super = examine_super_imsm,
2565 .brief_examine_super = brief_examine_super_imsm,
2566 .detail_super = detail_super_imsm,
2567 .brief_detail_super = brief_detail_super_imsm,
bf5a934a 2568 .write_init_super = write_init_super_imsm,
cdddbdbc
DW
2569#endif
2570 .match_home = match_home_imsm,
2571 .uuid_from_super= uuid_from_super_imsm,
2572 .getinfo_super = getinfo_super_imsm,
2573 .update_super = update_super_imsm,
2574
2575 .avail_size = avail_size_imsm,
2576
2577 .compare_super = compare_super_imsm,
2578
2579 .load_super = load_super_imsm,
bf5a934a
DW
2580 .init_super = init_super_imsm,
2581 .add_to_super = add_to_super_imsm,
cdddbdbc
DW
2582 .store_super = store_zero_imsm,
2583 .free_super = free_super_imsm,
2584 .match_metadata_desc = match_metadata_desc_imsm,
bf5a934a 2585 .container_content = container_content_imsm,
cdddbdbc
DW
2586
2587 .validate_geometry = validate_geometry_imsm,
cdddbdbc 2588 .external = 1,
845dea95
NB
2589
2590/* for mdmon */
2591 .open_new = imsm_open_new,
2592 .load_super = load_super_imsm,
ed9d66aa 2593 .set_array_state= imsm_set_array_state,
845dea95
NB
2594 .set_disk = imsm_set_disk,
2595 .sync_metadata = imsm_sync_metadata,
88758e9d 2596 .activate_spare = imsm_activate_spare,
e8319a19 2597 .process_update = imsm_process_update,
8273f55e 2598 .prepare_update = imsm_prepare_update,
cdddbdbc 2599};