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