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