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