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