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