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