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