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