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