imsm: fix setting of device size for raid1
[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 1818 strncpy((char *) dev->volume, name, MAX_RAID_SERIAL_LEN);
03bcbc65
DW
1819 if (info->level == 1)
1820 array_blocks = info_to_blocks_per_member(info);
1821 else
1822 array_blocks = calc_array_size(info->level, info->raid_disks,
1823 info->layout, info->chunk_size,
1824 info->size*2);
c2c087e6
DW
1825 dev->size_low = __cpu_to_le32((__u32) array_blocks);
1826 dev->size_high = __cpu_to_le32((__u32) (array_blocks >> 32));
1827 dev->status = __cpu_to_le32(0);
1828 dev->reserved_blocks = __cpu_to_le32(0);
1829 vol = &dev->vol;
1830 vol->migr_state = 0;
e3bba0e0 1831 vol->migr_type = MIGR_INIT;
c2c087e6 1832 vol->dirty = 0;
f8f603f1 1833 vol->curr_migr_unit = 0;
c2c087e6 1834 for (i = 0; i < idx; i++) {
949c47a0 1835 struct imsm_dev *prev = get_imsm_dev(super, i);
a965f303 1836 struct imsm_map *pmap = get_imsm_map(prev, 0);
c2c087e6
DW
1837
1838 offset += __le32_to_cpu(pmap->blocks_per_member);
1839 offset += IMSM_RESERVED_SECTORS;
1840 }
a965f303 1841 map = get_imsm_map(dev, 0);
c2c087e6 1842 map->pba_of_lba0 = __cpu_to_le32(offset);
fcfd9599 1843 map->blocks_per_member = __cpu_to_le32(info_to_blocks_per_member(info));
ef6ffade
DW
1844 map->blocks_per_strip = __cpu_to_le16(info_to_blocks_per_strip(info));
1845 map->num_data_stripes = __cpu_to_le32(info_to_num_data_stripes(info));
c2c087e6
DW
1846 map->map_state = info->level ? IMSM_T_STATE_UNINITIALIZED :
1847 IMSM_T_STATE_NORMAL;
ef6ffade
DW
1848
1849 if (info->level == 1 && info->raid_disks > 2) {
1850 fprintf(stderr, Name": imsm does not support more than 2 disks"
1851 "in a raid1 volume\n");
1852 return 0;
1853 }
4d1313e9 1854 if (info->level == 10) {
c2c087e6 1855 map->raid_level = 1;
4d1313e9
DW
1856 map->num_domains = info->raid_disks / 2;
1857 } else {
c2c087e6 1858 map->raid_level = info->level;
4d1313e9
DW
1859 map->num_domains = !!map->raid_level;
1860 }
ef6ffade 1861
c2c087e6
DW
1862 map->num_members = info->raid_disks;
1863 for (i = 0; i < map->num_members; i++) {
1864 /* initialized in add_to_super */
be73972f 1865 set_imsm_ord_tbl_ent(map, i, 0);
c2c087e6 1866 }
949c47a0
DW
1867 mpb->num_raid_devs++;
1868 super->dev_tbl[super->current_vol] = dev;
c2c087e6 1869
4d1313e9
DW
1870 imsm_update_version_info(super);
1871
c2c087e6 1872 return 1;
cdddbdbc
DW
1873}
1874
bf5a934a
DW
1875static int init_super_imsm(struct supertype *st, mdu_array_info_t *info,
1876 unsigned long long size, char *name,
1877 char *homehost, int *uuid)
1878{
1879 /* This is primarily called by Create when creating a new array.
1880 * We will then get add_to_super called for each component, and then
1881 * write_init_super called to write it out to each device.
1882 * For IMSM, Create can create on fresh devices or on a pre-existing
1883 * array.
1884 * To create on a pre-existing array a different method will be called.
1885 * This one is just for fresh drives.
1886 */
1887 struct intel_super *super;
1888 struct imsm_super *mpb;
1889 size_t mpb_size;
4d1313e9 1890 char *version;
bf5a934a
DW
1891
1892 if (!info) {
1893 st->sb = NULL;
1894 return 0;
1895 }
1896 if (st->sb)
1897 return init_super_imsm_volume(st, info, size, name, homehost,
1898 uuid);
1899
1900 super = alloc_super(1);
1901 if (!super)
1902 return 0;
1903 mpb_size = disks_to_mpb_size(info->nr_disks);
ef649044 1904 if (posix_memalign(&super->buf, 512, mpb_size) != 0) {
bf5a934a
DW
1905 free(super);
1906 return 0;
1907 }
ef649044 1908 mpb = super->buf;
bf5a934a
DW
1909 memset(mpb, 0, mpb_size);
1910
4d1313e9
DW
1911 mpb->attributes = MPB_ATTRIB_CHECKSUM_VERIFY;
1912
1913 version = (char *) mpb->sig;
1914 strcpy(version, MPB_SIGNATURE);
1915 version += strlen(MPB_SIGNATURE);
1916 strcpy(version, MPB_VERSION_RAID0);
bf5a934a
DW
1917 mpb->mpb_size = mpb_size;
1918
bf5a934a
DW
1919 st->sb = super;
1920 return 1;
1921}
1922
0e600426 1923#ifndef MDASSEMBLE
f20c3968 1924static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk,
bf5a934a
DW
1925 int fd, char *devname)
1926{
1927 struct intel_super *super = st->sb;
d23fe947 1928 struct imsm_super *mpb = super->anchor;
bf5a934a
DW
1929 struct dl *dl;
1930 struct imsm_dev *dev;
1931 struct imsm_map *map;
bf5a934a 1932
949c47a0 1933 dev = get_imsm_dev(super, super->current_vol);
a965f303 1934 map = get_imsm_map(dev, 0);
bf5a934a 1935
208933a7
N
1936 if (! (dk->state & (1<<MD_DISK_SYNC))) {
1937 fprintf(stderr, Name ": %s: Cannot add spare devices to IMSM volume\n",
1938 devname);
1939 return 1;
1940 }
1941
bf5a934a
DW
1942 for (dl = super->disks; dl ; dl = dl->next)
1943 if (dl->major == dk->major &&
1944 dl->minor == dk->minor)
1945 break;
d23fe947 1946
208933a7
N
1947 if (!dl) {
1948 fprintf(stderr, Name ": %s is not a member of the same container\n", devname);
f20c3968 1949 return 1;
208933a7 1950 }
bf5a934a 1951
d23fe947
DW
1952 /* add a pristine spare to the metadata */
1953 if (dl->index < 0) {
1954 dl->index = super->anchor->num_disks;
1955 super->anchor->num_disks++;
1956 }
be73972f 1957 set_imsm_ord_tbl_ent(map, dk->number, dl->index);
f2f27e63 1958 dl->disk.status = CONFIGURED_DISK | USABLE_DISK;
d23fe947
DW
1959
1960 /* if we are creating the first raid device update the family number */
1961 if (super->current_vol == 0) {
1962 __u32 sum;
1963 struct imsm_dev *_dev = __get_imsm_dev(mpb, 0);
1964 struct imsm_disk *_disk = __get_imsm_disk(mpb, dl->index);
1965
1966 *_dev = *dev;
1967 *_disk = dl->disk;
1968 sum = __gen_imsm_checksum(mpb);
1969 mpb->family_num = __cpu_to_le32(sum);
1970 }
f20c3968
DW
1971
1972 return 0;
bf5a934a
DW
1973}
1974
f20c3968 1975static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
cdddbdbc
DW
1976 int fd, char *devname)
1977{
c2c087e6 1978 struct intel_super *super = st->sb;
c2c087e6
DW
1979 struct dl *dd;
1980 unsigned long long size;
f2f27e63 1981 __u32 id;
c2c087e6
DW
1982 int rv;
1983 struct stat stb;
1984
f20c3968
DW
1985 if (super->current_vol >= 0)
1986 return add_to_super_imsm_volume(st, dk, fd, devname);
bf5a934a 1987
c2c087e6
DW
1988 fstat(fd, &stb);
1989 dd = malloc(sizeof(*dd));
b9f594fe 1990 if (!dd) {
c2c087e6
DW
1991 fprintf(stderr,
1992 Name ": malloc failed %s:%d.\n", __func__, __LINE__);
f20c3968 1993 return 1;
c2c087e6
DW
1994 }
1995 memset(dd, 0, sizeof(*dd));
1996 dd->major = major(stb.st_rdev);
1997 dd->minor = minor(stb.st_rdev);
b9f594fe 1998 dd->index = -1;
c2c087e6 1999 dd->devname = devname ? strdup(devname) : NULL;
c2c087e6
DW
2000 dd->fd = fd;
2001 rv = imsm_read_serial(fd, devname, dd->serial);
2002 if (rv) {
2003 fprintf(stderr,
0030e8d6 2004 Name ": failed to retrieve scsi serial, aborting\n");
949c47a0 2005 free(dd);
0030e8d6 2006 abort();
c2c087e6
DW
2007 }
2008
c2c087e6
DW
2009 get_dev_size(fd, NULL, &size);
2010 size /= 512;
1f24f035 2011 serialcpy(dd->disk.serial, dd->serial);
b9f594fe 2012 dd->disk.total_blocks = __cpu_to_le32(size);
f2f27e63 2013 dd->disk.status = USABLE_DISK | SPARE_DISK;
c2c087e6 2014 if (sysfs_disk_to_scsi_id(fd, &id) == 0)
b9f594fe 2015 dd->disk.scsi_id = __cpu_to_le32(id);
c2c087e6 2016 else
b9f594fe 2017 dd->disk.scsi_id = __cpu_to_le32(0);
43dad3d6
DW
2018
2019 if (st->update_tail) {
2020 dd->next = super->add;
2021 super->add = dd;
2022 } else {
2023 dd->next = super->disks;
2024 super->disks = dd;
2025 }
f20c3968
DW
2026
2027 return 0;
cdddbdbc
DW
2028}
2029
c2c087e6
DW
2030static int store_imsm_mpb(int fd, struct intel_super *super);
2031
d23fe947
DW
2032/* spare records have their own family number and do not have any defined raid
2033 * devices
2034 */
2035static int write_super_imsm_spares(struct intel_super *super, int doclose)
2036{
2037 struct imsm_super mpb_save;
2038 struct imsm_super *mpb = super->anchor;
2039 __u32 sum;
2040 struct dl *d;
2041
2042 mpb_save = *mpb;
2043 mpb->num_raid_devs = 0;
2044 mpb->num_disks = 1;
2045 mpb->mpb_size = sizeof(struct imsm_super);
2046 mpb->generation_num = __cpu_to_le32(1UL);
2047
2048 for (d = super->disks; d; d = d->next) {
8796fdc4 2049 if (d->index != -1)
d23fe947
DW
2050 continue;
2051
2052 mpb->disk[0] = d->disk;
2053 sum = __gen_imsm_checksum(mpb);
2054 mpb->family_num = __cpu_to_le32(sum);
2055 sum = __gen_imsm_checksum(mpb);
2056 mpb->check_sum = __cpu_to_le32(sum);
2057
2058 if (store_imsm_mpb(d->fd, super)) {
2059 fprintf(stderr, "%s: failed for device %d:%d %s\n",
2060 __func__, d->major, d->minor, strerror(errno));
2061 *mpb = mpb_save;
e74255d9 2062 return 1;
d23fe947
DW
2063 }
2064 if (doclose) {
2065 close(d->fd);
2066 d->fd = -1;
2067 }
2068 }
2069
2070 *mpb = mpb_save;
e74255d9 2071 return 0;
d23fe947
DW
2072}
2073
c2c087e6 2074static int write_super_imsm(struct intel_super *super, int doclose)
cdddbdbc 2075{
949c47a0 2076 struct imsm_super *mpb = super->anchor;
c2c087e6
DW
2077 struct dl *d;
2078 __u32 generation;
2079 __u32 sum;
d23fe947 2080 int spares = 0;
949c47a0 2081 int i;
a48ac0a8 2082 __u32 mpb_size = sizeof(struct imsm_super) - sizeof(struct imsm_disk);
cdddbdbc 2083
c2c087e6
DW
2084 /* 'generation' is incremented everytime the metadata is written */
2085 generation = __le32_to_cpu(mpb->generation_num);
2086 generation++;
2087 mpb->generation_num = __cpu_to_le32(generation);
2088
1ee1e9fc 2089 mpb_size += sizeof(struct imsm_disk) * mpb->num_disks;
d23fe947 2090 for (d = super->disks; d; d = d->next) {
8796fdc4 2091 if (d->index == -1)
d23fe947 2092 spares++;
1ee1e9fc 2093 else
d23fe947 2094 mpb->disk[d->index] = d->disk;
d23fe947 2095 }
47ee5a45
DW
2096 for (d = super->missing; d; d = d->next)
2097 mpb->disk[d->index] = d->disk;
b9f594fe 2098
949c47a0
DW
2099 for (i = 0; i < mpb->num_raid_devs; i++) {
2100 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
2101
2102 imsm_copy_dev(dev, super->dev_tbl[i]);
a48ac0a8 2103 mpb_size += sizeof_imsm_dev(dev, 0);
949c47a0 2104 }
a48ac0a8
DW
2105 mpb_size += __le32_to_cpu(mpb->bbm_log_size);
2106 mpb->mpb_size = __cpu_to_le32(mpb_size);
949c47a0 2107
c2c087e6 2108 /* recalculate checksum */
949c47a0 2109 sum = __gen_imsm_checksum(mpb);
c2c087e6
DW
2110 mpb->check_sum = __cpu_to_le32(sum);
2111
d23fe947 2112 /* write the mpb for disks that compose raid devices */
c2c087e6 2113 for (d = super->disks; d ; d = d->next) {
d23fe947
DW
2114 if (d->index < 0)
2115 continue;
8796fdc4 2116 if (store_imsm_mpb(d->fd, super))
c2c087e6
DW
2117 fprintf(stderr, "%s: failed for device %d:%d %s\n",
2118 __func__, d->major, d->minor, strerror(errno));
c2c087e6
DW
2119 if (doclose) {
2120 close(d->fd);
2121 d->fd = -1;
2122 }
2123 }
2124
d23fe947
DW
2125 if (spares)
2126 return write_super_imsm_spares(super, doclose);
2127
e74255d9 2128 return 0;
c2c087e6
DW
2129}
2130
0e600426 2131
43dad3d6
DW
2132static int create_array(struct supertype *st)
2133{
2134 size_t len;
2135 struct imsm_update_create_array *u;
2136 struct intel_super *super = st->sb;
2137 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
2138
2139 len = sizeof(*u) - sizeof(*dev) + sizeof_imsm_dev(dev, 0);
2140 u = malloc(len);
2141 if (!u) {
2142 fprintf(stderr, "%s: failed to allocate update buffer\n",
2143 __func__);
2144 return 1;
2145 }
2146
2147 u->type = update_create_array;
2148 u->dev_idx = super->current_vol;
2149 imsm_copy_dev(&u->dev, dev);
2150 append_metadata_update(st, u, len);
2151
2152 return 0;
2153}
2154
7801ac20 2155static int _add_disk(struct supertype *st)
43dad3d6
DW
2156{
2157 struct intel_super *super = st->sb;
2158 size_t len;
2159 struct imsm_update_add_disk *u;
2160
2161 if (!super->add)
2162 return 0;
2163
2164 len = sizeof(*u);
2165 u = malloc(len);
2166 if (!u) {
2167 fprintf(stderr, "%s: failed to allocate update buffer\n",
2168 __func__);
2169 return 1;
2170 }
2171
2172 u->type = update_add_disk;
2173 append_metadata_update(st, u, len);
2174
2175 return 0;
2176}
2177
c2c087e6
DW
2178static int write_init_super_imsm(struct supertype *st)
2179{
8273f55e 2180 if (st->update_tail) {
43dad3d6
DW
2181 /* queue the recently created array / added disk
2182 * as a metadata update */
8273f55e 2183 struct intel_super *super = st->sb;
8273f55e 2184 struct dl *d;
43dad3d6 2185 int rv;
8273f55e 2186
43dad3d6
DW
2187 /* determine if we are creating a volume or adding a disk */
2188 if (super->current_vol < 0) {
2189 /* in the add disk case we are running in mdmon
2190 * context, so don't close fd's
2191 */
7801ac20 2192 return _add_disk(st);
43dad3d6
DW
2193 } else
2194 rv = create_array(st);
8273f55e
DW
2195
2196 for (d = super->disks; d ; d = d->next) {
2197 close(d->fd);
2198 d->fd = -1;
2199 }
2200
43dad3d6 2201 return rv;
8273f55e
DW
2202 } else
2203 return write_super_imsm(st->sb, 1);
cdddbdbc 2204}
0e600426 2205#endif
cdddbdbc
DW
2206
2207static int store_zero_imsm(struct supertype *st, int fd)
2208{
551c80c1 2209 unsigned long long dsize;
6416d527 2210 void *buf;
551c80c1
DW
2211
2212 get_dev_size(fd, NULL, &dsize);
2213
2214 /* first block is stored on second to last sector of the disk */
2215 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0)
2216 return 1;
2217
ad97895e
DW
2218 if (posix_memalign(&buf, 512, 512) != 0)
2219 return 1;
2220
eb7ea463
DW
2221 memset(buf, 0, 512);
2222 if (write(fd, buf, 512) != 512)
551c80c1 2223 return 1;
cdddbdbc
DW
2224 return 0;
2225}
2226
0e600426
N
2227static int imsm_bbm_log_size(struct imsm_super *mpb)
2228{
2229 return __le32_to_cpu(mpb->bbm_log_size);
2230}
2231
2232#ifndef MDASSEMBLE
cdddbdbc
DW
2233static int validate_geometry_imsm_container(struct supertype *st, int level,
2234 int layout, int raiddisks, int chunk,
c2c087e6 2235 unsigned long long size, char *dev,
2c514b71
NB
2236 unsigned long long *freesize,
2237 int verbose)
cdddbdbc 2238{
c2c087e6
DW
2239 int fd;
2240 unsigned long long ldsize;
cdddbdbc 2241
c2c087e6
DW
2242 if (level != LEVEL_CONTAINER)
2243 return 0;
2244 if (!dev)
2245 return 1;
2246
2247 fd = open(dev, O_RDONLY|O_EXCL, 0);
2248 if (fd < 0) {
2c514b71
NB
2249 if (verbose)
2250 fprintf(stderr, Name ": imsm: Cannot open %s: %s\n",
2251 dev, strerror(errno));
c2c087e6
DW
2252 return 0;
2253 }
2254 if (!get_dev_size(fd, dev, &ldsize)) {
2255 close(fd);
2256 return 0;
2257 }
2258 close(fd);
2259
2260 *freesize = avail_size_imsm(st, ldsize >> 9);
2261
2262 return 1;
cdddbdbc
DW
2263}
2264
c2c087e6
DW
2265/* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
2266 * FIX ME add ahci details
2267 */
8b353278
DW
2268static int validate_geometry_imsm_volume(struct supertype *st, int level,
2269 int layout, int raiddisks, int chunk,
c2c087e6 2270 unsigned long long size, char *dev,
2c514b71
NB
2271 unsigned long long *freesize,
2272 int verbose)
cdddbdbc 2273{
c2c087e6
DW
2274 struct stat stb;
2275 struct intel_super *super = st->sb;
2276 struct dl *dl;
2277 unsigned long long pos = 0;
2278 unsigned long long maxsize;
2279 struct extent *e;
2280 int i;
cdddbdbc 2281
c2c087e6
DW
2282 if (level == LEVEL_CONTAINER)
2283 return 0;
2284
2285 if (level == 1 && raiddisks > 2) {
2c514b71
NB
2286 if (verbose)
2287 fprintf(stderr, Name ": imsm does not support more "
2288 "than 2 in a raid1 configuration\n");
c2c087e6
DW
2289 return 0;
2290 }
2291
2292 /* We must have the container info already read in. */
2293 if (!super)
2294 return 0;
2295
2296 if (!dev) {
2297 /* General test: make sure there is space for
2da8544a
DW
2298 * 'raiddisks' device extents of size 'size' at a given
2299 * offset
c2c087e6
DW
2300 */
2301 unsigned long long minsize = size*2 /* convert to blocks */;
2da8544a 2302 unsigned long long start_offset = ~0ULL;
c2c087e6
DW
2303 int dcnt = 0;
2304 if (minsize == 0)
2305 minsize = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
2306 for (dl = super->disks; dl ; dl = dl->next) {
2307 int found = 0;
2308
bf5a934a 2309 pos = 0;
c2c087e6
DW
2310 i = 0;
2311 e = get_extents(super, dl);
2312 if (!e) continue;
2313 do {
2314 unsigned long long esize;
2315 esize = e[i].start - pos;
2316 if (esize >= minsize)
2317 found = 1;
2da8544a
DW
2318 if (found && start_offset == ~0ULL) {
2319 start_offset = pos;
2320 break;
2321 } else if (found && pos != start_offset) {
2322 found = 0;
2323 break;
2324 }
c2c087e6
DW
2325 pos = e[i].start + e[i].size;
2326 i++;
2327 } while (e[i-1].size);
2328 if (found)
2329 dcnt++;
2330 free(e);
2331 }
2332 if (dcnt < raiddisks) {
2c514b71
NB
2333 if (verbose)
2334 fprintf(stderr, Name ": imsm: Not enough "
2335 "devices with space for this array "
2336 "(%d < %d)\n",
2337 dcnt, raiddisks);
c2c087e6
DW
2338 return 0;
2339 }
2340 return 1;
2341 }
2342 /* This device must be a member of the set */
2343 if (stat(dev, &stb) < 0)
2344 return 0;
2345 if ((S_IFMT & stb.st_mode) != S_IFBLK)
2346 return 0;
2347 for (dl = super->disks ; dl ; dl = dl->next) {
2348 if (dl->major == major(stb.st_rdev) &&
2349 dl->minor == minor(stb.st_rdev))
2350 break;
2351 }
2352 if (!dl) {
2c514b71
NB
2353 if (verbose)
2354 fprintf(stderr, Name ": %s is not in the "
2355 "same imsm set\n", dev);
c2c087e6
DW
2356 return 0;
2357 }
2358 e = get_extents(super, dl);
2359 maxsize = 0;
2360 i = 0;
2361 if (e) do {
2362 unsigned long long esize;
2363 esize = e[i].start - pos;
2364 if (esize >= maxsize)
2365 maxsize = esize;
2366 pos = e[i].start + e[i].size;
2367 i++;
2368 } while (e[i-1].size);
2369 *freesize = maxsize;
2370
2371 return 1;
cdddbdbc
DW
2372}
2373
bf5a934a
DW
2374static int validate_geometry_imsm(struct supertype *st, int level, int layout,
2375 int raiddisks, int chunk, unsigned long long size,
2376 char *dev, unsigned long long *freesize,
2377 int verbose)
2378{
2379 int fd, cfd;
2380 struct mdinfo *sra;
2381
2382 /* if given unused devices create a container
2383 * if given given devices in a container create a member volume
2384 */
2385 if (level == LEVEL_CONTAINER) {
2386 /* Must be a fresh device to add to a container */
2387 return validate_geometry_imsm_container(st, level, layout,
2388 raiddisks, chunk, size,
2389 dev, freesize,
2390 verbose);
2391 }
2392
2393 if (st->sb) {
2394 /* creating in a given container */
2395 return validate_geometry_imsm_volume(st, level, layout,
2396 raiddisks, chunk, size,
2397 dev, freesize, verbose);
2398 }
2399
2400 /* limit creation to the following levels */
2401 if (!dev)
2402 switch (level) {
2403 case 0:
2404 case 1:
2405 case 10:
2406 case 5:
2407 break;
2408 default:
2409 return 1;
2410 }
2411
2412 /* This device needs to be a device in an 'imsm' container */
2413 fd = open(dev, O_RDONLY|O_EXCL, 0);
2414 if (fd >= 0) {
2415 if (verbose)
2416 fprintf(stderr,
2417 Name ": Cannot create this array on device %s\n",
2418 dev);
2419 close(fd);
2420 return 0;
2421 }
2422 if (errno != EBUSY || (fd = open(dev, O_RDONLY, 0)) < 0) {
2423 if (verbose)
2424 fprintf(stderr, Name ": Cannot open %s: %s\n",
2425 dev, strerror(errno));
2426 return 0;
2427 }
2428 /* Well, it is in use by someone, maybe an 'imsm' container. */
2429 cfd = open_container(fd);
2430 if (cfd < 0) {
2431 close(fd);
2432 if (verbose)
2433 fprintf(stderr, Name ": Cannot use %s: It is busy\n",
2434 dev);
2435 return 0;
2436 }
2437 sra = sysfs_read(cfd, 0, GET_VERSION);
2438 close(fd);
2439 if (sra && sra->array.major_version == -1 &&
2440 strcmp(sra->text_version, "imsm") == 0) {
2441 /* This is a member of a imsm container. Load the container
2442 * and try to create a volume
2443 */
2444 struct intel_super *super;
2445
2446 if (load_super_imsm_all(st, cfd, (void **) &super, NULL, 1) == 0) {
2447 st->sb = super;
2448 st->container_dev = fd2devnum(cfd);
2449 close(cfd);
2450 return validate_geometry_imsm_volume(st, level, layout,
2451 raiddisks, chunk,
2452 size, dev,
2453 freesize, verbose);
2454 }
2455 close(cfd);
2456 } else /* may belong to another container */
2457 return 0;
2458
2459 return 1;
2460}
0e600426 2461#endif /* MDASSEMBLE */
bf5a934a 2462
cdddbdbc
DW
2463static struct mdinfo *container_content_imsm(struct supertype *st)
2464{
4f5bc454
DW
2465 /* Given a container loaded by load_super_imsm_all,
2466 * extract information about all the arrays into
2467 * an mdinfo tree.
2468 *
2469 * For each imsm_dev create an mdinfo, fill it in,
2470 * then look for matching devices in super->disks
2471 * and create appropriate device mdinfo.
2472 */
2473 struct intel_super *super = st->sb;
949c47a0 2474 struct imsm_super *mpb = super->anchor;
4f5bc454
DW
2475 struct mdinfo *rest = NULL;
2476 int i;
cdddbdbc 2477
604b746f
JD
2478 /* do not assemble arrays that might have bad blocks */
2479 if (imsm_bbm_log_size(super->anchor)) {
2480 fprintf(stderr, Name ": BBM log found in metadata. "
2481 "Cannot activate array(s).\n");
2482 return NULL;
2483 }
2484
4f5bc454 2485 for (i = 0; i < mpb->num_raid_devs; i++) {
949c47a0 2486 struct imsm_dev *dev = get_imsm_dev(super, i);
a965f303 2487 struct imsm_map *map = get_imsm_map(dev, 0);
4f5bc454 2488 struct mdinfo *this;
4f5bc454
DW
2489 int slot;
2490
2491 this = malloc(sizeof(*this));
2492 memset(this, 0, sizeof(*this));
2493 this->next = rest;
4f5bc454 2494
301406c9
DW
2495 super->current_vol = i;
2496 getinfo_super_imsm_volume(st, this);
4f5bc454 2497 for (slot = 0 ; slot < map->num_members; slot++) {
4f5bc454
DW
2498 struct mdinfo *info_d;
2499 struct dl *d;
2500 int idx;
9a1608e5 2501 int skip;
4f5bc454 2502 __u32 s;
7eef0453 2503 __u32 ord;
4f5bc454 2504
9a1608e5 2505 skip = 0;
ff077194 2506 idx = get_imsm_disk_idx(dev, slot);
7eef0453 2507 ord = get_imsm_ord_tbl_ent(dev, slot);
4f5bc454
DW
2508 for (d = super->disks; d ; d = d->next)
2509 if (d->index == idx)
2510 break;
2511
2512 if (d == NULL)
9a1608e5
DW
2513 skip = 1;
2514
f2f27e63 2515 s = d ? d->disk.status : 0;
9a1608e5
DW
2516 if (s & FAILED_DISK)
2517 skip = 1;
2518 if (!(s & USABLE_DISK))
2519 skip = 1;
7eef0453
DW
2520 if (ord & IMSM_ORD_REBUILD)
2521 skip = 1;
9a1608e5
DW
2522
2523 /*
2524 * if we skip some disks the array will be assmebled degraded;
2525 * reset resync start to avoid a dirty-degraded situation
2526 *
2527 * FIXME handle dirty degraded
2528 */
2529 if (skip && !dev->vol.dirty)
2530 this->resync_start = ~0ULL;
2531 if (skip)
2532 continue;
4f5bc454
DW
2533
2534 info_d = malloc(sizeof(*info_d));
9a1608e5
DW
2535 if (!info_d) {
2536 fprintf(stderr, Name ": failed to allocate disk"
2537 " for volume %s\n", (char *) dev->volume);
2538 free(this);
2539 this = rest;
2540 break;
2541 }
4f5bc454
DW
2542 memset(info_d, 0, sizeof(*info_d));
2543 info_d->next = this->devs;
2544 this->devs = info_d;
2545
4f5bc454
DW
2546 info_d->disk.number = d->index;
2547 info_d->disk.major = d->major;
2548 info_d->disk.minor = d->minor;
2549 info_d->disk.raid_disk = slot;
4f5bc454
DW
2550
2551 this->array.working_disks++;
2552
2553 info_d->events = __le32_to_cpu(mpb->generation_num);
2554 info_d->data_offset = __le32_to_cpu(map->pba_of_lba0);
2555 info_d->component_size = __le32_to_cpu(map->blocks_per_member);
2556 if (d->devname)
2557 strcpy(info_d->name, d->devname);
2558 }
9a1608e5 2559 rest = this;
4f5bc454
DW
2560 }
2561
2562 return rest;
cdddbdbc
DW
2563}
2564
845dea95 2565
0e600426 2566#ifndef MDASSEMBLE
cba0191b
NB
2567static int imsm_open_new(struct supertype *c, struct active_array *a,
2568 char *inst)
845dea95 2569{
0372d5a2 2570 struct intel_super *super = c->sb;
949c47a0 2571 struct imsm_super *mpb = super->anchor;
0372d5a2 2572
949c47a0 2573 if (atoi(inst) >= mpb->num_raid_devs) {
0372d5a2
DW
2574 fprintf(stderr, "%s: subarry index %d, out of range\n",
2575 __func__, atoi(inst));
2576 return -ENODEV;
2577 }
2578
4e6e574a 2579 dprintf("imsm: open_new %s\n", inst);
cba0191b 2580 a->info.container_member = atoi(inst);
845dea95
NB
2581 return 0;
2582}
2583
fb49eef2 2584static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev, int failed)
c2a1e7da 2585{
a965f303 2586 struct imsm_map *map = get_imsm_map(dev, 0);
c2a1e7da
DW
2587
2588 if (!failed)
3393c6af
DW
2589 return map->map_state == IMSM_T_STATE_UNINITIALIZED ?
2590 IMSM_T_STATE_UNINITIALIZED : IMSM_T_STATE_NORMAL;
c2a1e7da
DW
2591
2592 switch (get_imsm_raid_level(map)) {
2593 case 0:
2594 return IMSM_T_STATE_FAILED;
2595 break;
2596 case 1:
2597 if (failed < map->num_members)
2598 return IMSM_T_STATE_DEGRADED;
2599 else
2600 return IMSM_T_STATE_FAILED;
2601 break;
2602 case 10:
2603 {
2604 /**
c92a2527
DW
2605 * check to see if any mirrors have failed, otherwise we
2606 * are degraded. Even numbered slots are mirrored on
2607 * slot+1
c2a1e7da 2608 */
c2a1e7da 2609 int i;
d9b420a5
N
2610 /* gcc -Os complains that this is unused */
2611 int insync = insync;
c2a1e7da
DW
2612
2613 for (i = 0; i < map->num_members; i++) {
c92a2527
DW
2614 __u32 ord = get_imsm_ord_tbl_ent(dev, i);
2615 int idx = ord_to_idx(ord);
2616 struct imsm_disk *disk;
c2a1e7da 2617
c92a2527
DW
2618 /* reset the potential in-sync count on even-numbered
2619 * slots. num_copies is always 2 for imsm raid10
2620 */
2621 if ((i & 1) == 0)
2622 insync = 2;
c2a1e7da 2623
c92a2527 2624 disk = get_imsm_disk(super, idx);
f2f27e63 2625 if (!disk || disk->status & FAILED_DISK ||
c92a2527
DW
2626 ord & IMSM_ORD_REBUILD)
2627 insync--;
c2a1e7da 2628
c92a2527
DW
2629 /* no in-sync disks left in this mirror the
2630 * array has failed
2631 */
2632 if (insync == 0)
2633 return IMSM_T_STATE_FAILED;
c2a1e7da
DW
2634 }
2635
2636 return IMSM_T_STATE_DEGRADED;
2637 }
2638 case 5:
2639 if (failed < 2)
2640 return IMSM_T_STATE_DEGRADED;
2641 else
2642 return IMSM_T_STATE_FAILED;
2643 break;
2644 default:
2645 break;
2646 }
2647
2648 return map->map_state;
2649}
2650
ff077194 2651static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev)
c2a1e7da
DW
2652{
2653 int i;
2654 int failed = 0;
2655 struct imsm_disk *disk;
ff077194 2656 struct imsm_map *map = get_imsm_map(dev, 0);
c2a1e7da
DW
2657
2658 for (i = 0; i < map->num_members; i++) {
b10b37b8
DW
2659 __u32 ord = get_imsm_ord_tbl_ent(dev, i);
2660 int idx = ord_to_idx(ord);
c2a1e7da 2661
949c47a0 2662 disk = get_imsm_disk(super, idx);
f2f27e63 2663 if (!disk || disk->status & FAILED_DISK ||
b10b37b8 2664 ord & IMSM_ORD_REBUILD)
fcb84475 2665 failed++;
c2a1e7da
DW
2666 }
2667
2668 return failed;
845dea95
NB
2669}
2670
0c046afd
DW
2671static int is_resyncing(struct imsm_dev *dev)
2672{
2673 struct imsm_map *migr_map;
2674
2675 if (!dev->vol.migr_state)
2676 return 0;
2677
e3bba0e0 2678 if (dev->vol.migr_type == MIGR_INIT)
0c046afd
DW
2679 return 1;
2680
2681 migr_map = get_imsm_map(dev, 1);
2682
2683 if (migr_map->map_state == IMSM_T_STATE_NORMAL)
2684 return 1;
2685 else
2686 return 0;
2687}
2688
2689static int is_rebuilding(struct imsm_dev *dev)
2690{
2691 struct imsm_map *migr_map;
2692
2693 if (!dev->vol.migr_state)
2694 return 0;
2695
e3bba0e0 2696 if (dev->vol.migr_type != MIGR_REBUILD)
0c046afd
DW
2697 return 0;
2698
2699 migr_map = get_imsm_map(dev, 1);
2700
2701 if (migr_map->map_state == IMSM_T_STATE_DEGRADED)
2702 return 1;
2703 else
2704 return 0;
2705}
2706
47ee5a45
DW
2707static void mark_failure(struct imsm_disk *disk)
2708{
f2f27e63 2709 if (disk->status & FAILED_DISK)
47ee5a45 2710 return;
f2f27e63 2711 disk->status |= FAILED_DISK;
47ee5a45
DW
2712 disk->scsi_id = __cpu_to_le32(~(__u32)0);
2713 memmove(&disk->serial[0], &disk->serial[1], MAX_RAID_SERIAL_LEN - 1);
2714}
2715
0c046afd
DW
2716/* Handle dirty -> clean transititions and resync. Degraded and rebuild
2717 * states are handled in imsm_set_disk() with one exception, when a
2718 * resync is stopped due to a new failure this routine will set the
2719 * 'degraded' state for the array.
2720 */
01f157d7 2721static int imsm_set_array_state(struct active_array *a, int consistent)
a862209d
DW
2722{
2723 int inst = a->info.container_member;
2724 struct intel_super *super = a->container->sb;
949c47a0 2725 struct imsm_dev *dev = get_imsm_dev(super, inst);
a965f303 2726 struct imsm_map *map = get_imsm_map(dev, 0);
0c046afd
DW
2727 int failed = imsm_count_failed(super, dev);
2728 __u8 map_state = imsm_check_degraded(super, dev, failed);
a862209d 2729
47ee5a45
DW
2730 /* before we activate this array handle any missing disks */
2731 if (consistent == 2 && super->missing) {
2732 struct dl *dl;
2733
2734 dprintf("imsm: mark missing\n");
2735 end_migration(dev, map_state);
2736 for (dl = super->missing; dl; dl = dl->next)
2737 mark_failure(&dl->disk);
2738 super->updates_pending++;
2739 }
2740
0c046afd 2741 if (consistent == 2 &&
593add1b 2742 (!is_resync_complete(a) ||
0c046afd
DW
2743 map_state != IMSM_T_STATE_NORMAL ||
2744 dev->vol.migr_state))
01f157d7 2745 consistent = 0;
272906ef 2746
593add1b 2747 if (is_resync_complete(a)) {
0c046afd
DW
2748 /* complete intialization / resync,
2749 * recovery is completed in ->set_disk
2750 */
2751 if (is_resyncing(dev)) {
2752 dprintf("imsm: mark resync done\n");
f8f603f1 2753 end_migration(dev, map_state);
115c3803 2754 super->updates_pending++;
115c3803 2755 }
0c046afd
DW
2756 } else if (!is_resyncing(dev) && !failed) {
2757 /* mark the start of the init process if nothing is failed */
2758 dprintf("imsm: mark resync start (%llu)\n", a->resync_start);
e3bba0e0
DW
2759 if (map->map_state == IMSM_T_STATE_NORMAL)
2760 migrate(dev, IMSM_T_STATE_NORMAL, MIGR_REBUILD);
2761 else
2762 migrate(dev, IMSM_T_STATE_NORMAL, MIGR_INIT);
3393c6af 2763 super->updates_pending++;
115c3803 2764 }
a862209d 2765
f8f603f1
DW
2766 /* check if we can update the migration checkpoint */
2767 if (dev->vol.migr_state &&
2768 __le32_to_cpu(dev->vol.curr_migr_unit) != a->resync_start) {
2769 dprintf("imsm: checkpoint migration (%llu)\n", a->resync_start);
2770 dev->vol.curr_migr_unit = __cpu_to_le32(a->resync_start);
2771 super->updates_pending++;
2772 }
2773
3393c6af 2774 /* mark dirty / clean */
0c046afd 2775 if (dev->vol.dirty != !consistent) {
3393c6af 2776 dprintf("imsm: mark '%s' (%llu)\n",
0c046afd
DW
2777 consistent ? "clean" : "dirty", a->resync_start);
2778 if (consistent)
2779 dev->vol.dirty = 0;
2780 else
2781 dev->vol.dirty = 1;
a862209d
DW
2782 super->updates_pending++;
2783 }
01f157d7 2784 return consistent;
a862209d
DW
2785}
2786
8d45d196 2787static void imsm_set_disk(struct active_array *a, int n, int state)
845dea95 2788{
8d45d196
DW
2789 int inst = a->info.container_member;
2790 struct intel_super *super = a->container->sb;
949c47a0 2791 struct imsm_dev *dev = get_imsm_dev(super, inst);
a965f303 2792 struct imsm_map *map = get_imsm_map(dev, 0);
8d45d196 2793 struct imsm_disk *disk;
0c046afd 2794 int failed;
b10b37b8 2795 __u32 ord;
0c046afd 2796 __u8 map_state;
8d45d196
DW
2797
2798 if (n > map->num_members)
2799 fprintf(stderr, "imsm: set_disk %d out of range 0..%d\n",
2800 n, map->num_members - 1);
2801
2802 if (n < 0)
2803 return;
2804
4e6e574a 2805 dprintf("imsm: set_disk %d:%x\n", n, state);
8d45d196 2806
b10b37b8
DW
2807 ord = get_imsm_ord_tbl_ent(dev, n);
2808 disk = get_imsm_disk(super, ord_to_idx(ord));
8d45d196 2809
5802a811 2810 /* check for new failures */
f2f27e63 2811 if ((state & DS_FAULTY) && !(disk->status & FAILED_DISK)) {
47ee5a45 2812 mark_failure(disk);
5802a811 2813 super->updates_pending++;
8d45d196 2814 }
47ee5a45 2815
19859edc 2816 /* check if in_sync */
b10b37b8
DW
2817 if (state & DS_INSYNC && ord & IMSM_ORD_REBUILD) {
2818 struct imsm_map *migr_map = get_imsm_map(dev, 1);
2819
2820 set_imsm_ord_tbl_ent(migr_map, n, ord_to_idx(ord));
19859edc
DW
2821 super->updates_pending++;
2822 }
8d45d196 2823
0c046afd
DW
2824 failed = imsm_count_failed(super, dev);
2825 map_state = imsm_check_degraded(super, dev, failed);
5802a811 2826
0c046afd
DW
2827 /* check if recovery complete, newly degraded, or failed */
2828 if (map_state == IMSM_T_STATE_NORMAL && is_rebuilding(dev)) {
f8f603f1 2829 end_migration(dev, map_state);
0c046afd
DW
2830 super->updates_pending++;
2831 } else if (map_state == IMSM_T_STATE_DEGRADED &&
2832 map->map_state != map_state &&
2833 !dev->vol.migr_state) {
2834 dprintf("imsm: mark degraded\n");
2835 map->map_state = map_state;
2836 super->updates_pending++;
2837 } else if (map_state == IMSM_T_STATE_FAILED &&
2838 map->map_state != map_state) {
2839 dprintf("imsm: mark failed\n");
f8f603f1 2840 end_migration(dev, map_state);
0c046afd 2841 super->updates_pending++;
5802a811 2842 }
845dea95
NB
2843}
2844
c2a1e7da
DW
2845static int store_imsm_mpb(int fd, struct intel_super *super)
2846{
949c47a0 2847 struct imsm_super *mpb = super->anchor;
c2a1e7da
DW
2848 __u32 mpb_size = __le32_to_cpu(mpb->mpb_size);
2849 unsigned long long dsize;
2850 unsigned long long sectors;
2851
2852 get_dev_size(fd, NULL, &dsize);
2853
272f648f
DW
2854 if (mpb_size > 512) {
2855 /* -1 to account for anchor */
2856 sectors = mpb_sectors(mpb) - 1;
c2a1e7da 2857
272f648f
DW
2858 /* write the extended mpb to the sectors preceeding the anchor */
2859 if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0)
2860 return 1;
c2a1e7da 2861
99e29264 2862 if (write(fd, super->buf + 512, 512 * sectors) != 512 * sectors)
272f648f
DW
2863 return 1;
2864 }
c2a1e7da 2865
272f648f
DW
2866 /* first block is stored on second to last sector of the disk */
2867 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0)
c2a1e7da
DW
2868 return 1;
2869
272f648f 2870 if (write(fd, super->buf, 512) != 512)
c2a1e7da
DW
2871 return 1;
2872
c2a1e7da
DW
2873 return 0;
2874}
2875
2e735d19 2876static void imsm_sync_metadata(struct supertype *container)
845dea95 2877{
2e735d19 2878 struct intel_super *super = container->sb;
c2a1e7da
DW
2879
2880 if (!super->updates_pending)
2881 return;
2882
c2c087e6 2883 write_super_imsm(super, 0);
c2a1e7da
DW
2884
2885 super->updates_pending = 0;
845dea95
NB
2886}
2887
272906ef
DW
2888static struct dl *imsm_readd(struct intel_super *super, int idx, struct active_array *a)
2889{
2890 struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
ff077194 2891 int i = get_imsm_disk_idx(dev, idx);
272906ef
DW
2892 struct dl *dl;
2893
2894 for (dl = super->disks; dl; dl = dl->next)
2895 if (dl->index == i)
2896 break;
2897
f2f27e63 2898 if (dl && dl->disk.status & FAILED_DISK)
272906ef
DW
2899 dl = NULL;
2900
2901 if (dl)
2902 dprintf("%s: found %x:%x\n", __func__, dl->major, dl->minor);
2903
2904 return dl;
2905}
2906
e553d2a4 2907static struct dl *imsm_add_spare(struct intel_super *super, int slot, struct active_array *a)
272906ef
DW
2908{
2909 struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
e553d2a4 2910 int idx = get_imsm_disk_idx(dev, slot);
272906ef
DW
2911 struct imsm_map *map = get_imsm_map(dev, 0);
2912 unsigned long long esize;
2913 unsigned long long pos;
2914 struct mdinfo *d;
2915 struct extent *ex;
2916 int j;
2917 int found;
2918 __u32 array_start;
2919 struct dl *dl;
2920
2921 for (dl = super->disks; dl; dl = dl->next) {
2922 /* If in this array, skip */
2923 for (d = a->info.devs ; d ; d = d->next)
e553d2a4
DW
2924 if (d->state_fd >= 0 &&
2925 d->disk.major == dl->major &&
272906ef
DW
2926 d->disk.minor == dl->minor) {
2927 dprintf("%x:%x already in array\n", dl->major, dl->minor);
2928 break;
2929 }
2930 if (d)
2931 continue;
2932
e553d2a4 2933 /* skip in use or failed drives */
f2f27e63 2934 if (dl->disk.status & FAILED_DISK || idx == dl->index) {
9a1608e5
DW
2935 dprintf("%x:%x status ( %s%s)\n",
2936 dl->major, dl->minor,
f2f27e63 2937 dl->disk.status & FAILED_DISK ? "failed " : "",
e553d2a4 2938 idx == dl->index ? "in use " : "");
9a1608e5
DW
2939 continue;
2940 }
2941
272906ef
DW
2942 /* Does this unused device have the requisite free space?
2943 * We need a->info.component_size sectors
2944 */
2945 ex = get_extents(super, dl);
2946 if (!ex) {
2947 dprintf("cannot get extents\n");
2948 continue;
2949 }
2950 found = 0;
2951 j = 0;
2952 pos = 0;
2953 array_start = __le32_to_cpu(map->pba_of_lba0);
2954
2955 do {
2956 /* check that we can start at pba_of_lba0 with
2957 * a->info.component_size of space
2958 */
2959 esize = ex[j].start - pos;
2960 if (array_start >= pos &&
2961 array_start + a->info.component_size < ex[j].start) {
2962 found = 1;
2963 break;
2964 }
2965 pos = ex[j].start + ex[j].size;
2966 j++;
2967
2968 } while (ex[j-1].size);
2969
2970 free(ex);
2971 if (!found) {
2972 dprintf("%x:%x does not have %llu at %d\n",
2973 dl->major, dl->minor,
2974 a->info.component_size,
2975 __le32_to_cpu(map->pba_of_lba0));
2976 /* No room */
2977 continue;
2978 } else
2979 break;
2980 }
2981
2982 return dl;
2983}
2984
88758e9d
DW
2985static struct mdinfo *imsm_activate_spare(struct active_array *a,
2986 struct metadata_update **updates)
2987{
2988 /**
d23fe947
DW
2989 * Find a device with unused free space and use it to replace a
2990 * failed/vacant region in an array. We replace failed regions one a
2991 * array at a time. The result is that a new spare disk will be added
2992 * to the first failed array and after the monitor has finished
2993 * propagating failures the remainder will be consumed.
88758e9d 2994 *
d23fe947
DW
2995 * FIXME add a capability for mdmon to request spares from another
2996 * container.
88758e9d
DW
2997 */
2998
2999 struct intel_super *super = a->container->sb;
88758e9d 3000 int inst = a->info.container_member;
949c47a0 3001 struct imsm_dev *dev = get_imsm_dev(super, inst);
a965f303 3002 struct imsm_map *map = get_imsm_map(dev, 0);
88758e9d
DW
3003 int failed = a->info.array.raid_disks;
3004 struct mdinfo *rv = NULL;
3005 struct mdinfo *d;
3006 struct mdinfo *di;
3007 struct metadata_update *mu;
3008 struct dl *dl;
3009 struct imsm_update_activate_spare *u;
3010 int num_spares = 0;
3011 int i;
3012
3013 for (d = a->info.devs ; d ; d = d->next) {
3014 if ((d->curr_state & DS_FAULTY) &&
3015 d->state_fd >= 0)
3016 /* wait for Removal to happen */
3017 return NULL;
3018 if (d->state_fd >= 0)
3019 failed--;
3020 }
3021
3022 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
3023 inst, failed, a->info.array.raid_disks, a->info.array.level);
fb49eef2 3024 if (imsm_check_degraded(super, dev, failed) != IMSM_T_STATE_DEGRADED)
88758e9d
DW
3025 return NULL;
3026
3027 /* For each slot, if it is not working, find a spare */
88758e9d
DW
3028 for (i = 0; i < a->info.array.raid_disks; i++) {
3029 for (d = a->info.devs ; d ; d = d->next)
3030 if (d->disk.raid_disk == i)
3031 break;
3032 dprintf("found %d: %p %x\n", i, d, d?d->curr_state:0);
3033 if (d && (d->state_fd >= 0))
3034 continue;
3035
272906ef
DW
3036 /*
3037 * OK, this device needs recovery. Try to re-add the previous
3038 * occupant of this slot, if this fails add a new spare
3039 */
3040 dl = imsm_readd(super, i, a);
3041 if (!dl)
3042 dl = imsm_add_spare(super, i, a);
3043 if (!dl)
3044 continue;
3045
3046 /* found a usable disk with enough space */
3047 di = malloc(sizeof(*di));
79244939
DW
3048 if (!di)
3049 continue;
272906ef
DW
3050 memset(di, 0, sizeof(*di));
3051
3052 /* dl->index will be -1 in the case we are activating a
3053 * pristine spare. imsm_process_update() will create a
3054 * new index in this case. Once a disk is found to be
3055 * failed in all member arrays it is kicked from the
3056 * metadata
3057 */
3058 di->disk.number = dl->index;
d23fe947 3059
272906ef
DW
3060 /* (ab)use di->devs to store a pointer to the device
3061 * we chose
3062 */
3063 di->devs = (struct mdinfo *) dl;
3064
3065 di->disk.raid_disk = i;
3066 di->disk.major = dl->major;
3067 di->disk.minor = dl->minor;
3068 di->disk.state = 0;
3069 di->data_offset = __le32_to_cpu(map->pba_of_lba0);
3070 di->component_size = a->info.component_size;
3071 di->container_member = inst;
3072 di->next = rv;
3073 rv = di;
3074 num_spares++;
3075 dprintf("%x:%x to be %d at %llu\n", dl->major, dl->minor,
3076 i, di->data_offset);
88758e9d 3077
272906ef 3078 break;
88758e9d
DW
3079 }
3080
3081 if (!rv)
3082 /* No spares found */
3083 return rv;
3084 /* Now 'rv' has a list of devices to return.
3085 * Create a metadata_update record to update the
3086 * disk_ord_tbl for the array
3087 */
3088 mu = malloc(sizeof(*mu));
79244939
DW
3089 if (mu) {
3090 mu->buf = malloc(sizeof(struct imsm_update_activate_spare) * num_spares);
3091 if (mu->buf == NULL) {
3092 free(mu);
3093 mu = NULL;
3094 }
3095 }
3096 if (!mu) {
3097 while (rv) {
3098 struct mdinfo *n = rv->next;
3099
3100 free(rv);
3101 rv = n;
3102 }
3103 return NULL;
3104 }
3105
88758e9d
DW
3106 mu->space = NULL;
3107 mu->len = sizeof(struct imsm_update_activate_spare) * num_spares;
3108 mu->next = *updates;
3109 u = (struct imsm_update_activate_spare *) mu->buf;
3110
3111 for (di = rv ; di ; di = di->next) {
3112 u->type = update_activate_spare;
d23fe947
DW
3113 u->dl = (struct dl *) di->devs;
3114 di->devs = NULL;
88758e9d
DW
3115 u->slot = di->disk.raid_disk;
3116 u->array = inst;
3117 u->next = u + 1;
3118 u++;
3119 }
3120 (u-1)->next = NULL;
3121 *updates = mu;
3122
3123 return rv;
3124}
3125
ff077194 3126static int disks_overlap(struct imsm_dev *d1, struct imsm_dev *d2)
8273f55e 3127{
ff077194
DW
3128 struct imsm_map *m1 = get_imsm_map(d1, 0);
3129 struct imsm_map *m2 = get_imsm_map(d2, 0);
8273f55e
DW
3130 int i;
3131 int j;
3132 int idx;
3133
3134 for (i = 0; i < m1->num_members; i++) {
ff077194 3135 idx = get_imsm_disk_idx(d1, i);
8273f55e 3136 for (j = 0; j < m2->num_members; j++)
ff077194 3137 if (idx == get_imsm_disk_idx(d2, j))
8273f55e
DW
3138 return 1;
3139 }
3140
3141 return 0;
3142}
3143
24565c9a 3144static void imsm_delete(struct intel_super *super, struct dl **dlp, int index);
ae6aad82 3145
e8319a19
DW
3146static void imsm_process_update(struct supertype *st,
3147 struct metadata_update *update)
3148{
3149 /**
3150 * crack open the metadata_update envelope to find the update record
3151 * update can be one of:
3152 * update_activate_spare - a spare device has replaced a failed
3153 * device in an array, update the disk_ord_tbl. If this disk is
3154 * present in all member arrays then also clear the SPARE_DISK
3155 * flag
3156 */
3157 struct intel_super *super = st->sb;
4d7b1503 3158 struct imsm_super *mpb;
e8319a19
DW
3159 enum imsm_update_type type = *(enum imsm_update_type *) update->buf;
3160
4d7b1503
DW
3161 /* update requires a larger buf but the allocation failed */
3162 if (super->next_len && !super->next_buf) {
3163 super->next_len = 0;
3164 return;
3165 }
3166
3167 if (super->next_buf) {
3168 memcpy(super->next_buf, super->buf, super->len);
3169 free(super->buf);
3170 super->len = super->next_len;
3171 super->buf = super->next_buf;
3172
3173 super->next_len = 0;
3174 super->next_buf = NULL;
3175 }
3176
3177 mpb = super->anchor;
3178
e8319a19
DW
3179 switch (type) {
3180 case update_activate_spare: {
3181 struct imsm_update_activate_spare *u = (void *) update->buf;
949c47a0 3182 struct imsm_dev *dev = get_imsm_dev(super, u->array);
a965f303 3183 struct imsm_map *map = get_imsm_map(dev, 0);
0c046afd 3184 struct imsm_map *migr_map;
e8319a19
DW
3185 struct active_array *a;
3186 struct imsm_disk *disk;
0c046afd 3187 __u8 to_state;
e8319a19 3188 struct dl *dl;
e8319a19 3189 unsigned int found;
0c046afd
DW
3190 int failed;
3191 int victim = get_imsm_disk_idx(dev, u->slot);
e8319a19
DW
3192 int i;
3193
3194 for (dl = super->disks; dl; dl = dl->next)
d23fe947 3195 if (dl == u->dl)
e8319a19
DW
3196 break;
3197
3198 if (!dl) {
3199 fprintf(stderr, "error: imsm_activate_spare passed "
1f24f035
DW
3200 "an unknown disk (index: %d)\n",
3201 u->dl->index);
e8319a19
DW
3202 return;
3203 }
3204
3205 super->updates_pending++;
3206
0c046afd
DW
3207 /* count failures (excluding rebuilds and the victim)
3208 * to determine map[0] state
3209 */
3210 failed = 0;
3211 for (i = 0; i < map->num_members; i++) {
3212 if (i == u->slot)
3213 continue;
3214 disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i));
f2f27e63 3215 if (!disk || disk->status & FAILED_DISK)
0c046afd
DW
3216 failed++;
3217 }
3218
d23fe947
DW
3219 /* adding a pristine spare, assign a new index */
3220 if (dl->index < 0) {
3221 dl->index = super->anchor->num_disks;
3222 super->anchor->num_disks++;
3223 }
d23fe947 3224 disk = &dl->disk;
f2f27e63
DW
3225 disk->status |= CONFIGURED_DISK;
3226 disk->status &= ~SPARE_DISK;
e8319a19 3227
0c046afd
DW
3228 /* mark rebuild */
3229 to_state = imsm_check_degraded(super, dev, failed);
3230 map->map_state = IMSM_T_STATE_DEGRADED;
e3bba0e0 3231 migrate(dev, to_state, MIGR_REBUILD);
0c046afd
DW
3232 migr_map = get_imsm_map(dev, 1);
3233 set_imsm_ord_tbl_ent(map, u->slot, dl->index);
3234 set_imsm_ord_tbl_ent(migr_map, u->slot, dl->index | IMSM_ORD_REBUILD);
3235
e8319a19
DW
3236 /* count arrays using the victim in the metadata */
3237 found = 0;
3238 for (a = st->arrays; a ; a = a->next) {
949c47a0 3239 dev = get_imsm_dev(super, a->info.container_member);
e8319a19 3240 for (i = 0; i < map->num_members; i++)
ff077194 3241 if (victim == get_imsm_disk_idx(dev, i))
e8319a19
DW
3242 found++;
3243 }
3244
24565c9a 3245 /* delete the victim if it is no longer being
e8319a19
DW
3246 * utilized anywhere
3247 */
e8319a19 3248 if (!found) {
ae6aad82 3249 struct dl **dlp;
24565c9a 3250
47ee5a45
DW
3251 /* We know that 'manager' isn't touching anything,
3252 * so it is safe to delete
3253 */
24565c9a 3254 for (dlp = &super->disks; *dlp; dlp = &(*dlp)->next)
ae6aad82
DW
3255 if ((*dlp)->index == victim)
3256 break;
47ee5a45
DW
3257
3258 /* victim may be on the missing list */
3259 if (!*dlp)
3260 for (dlp = &super->missing; *dlp; dlp = &(*dlp)->next)
3261 if ((*dlp)->index == victim)
3262 break;
24565c9a 3263 imsm_delete(super, dlp, victim);
e8319a19 3264 }
8273f55e
DW
3265 break;
3266 }
3267 case update_create_array: {
3268 /* someone wants to create a new array, we need to be aware of
3269 * a few races/collisions:
3270 * 1/ 'Create' called by two separate instances of mdadm
3271 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
3272 * devices that have since been assimilated via
3273 * activate_spare.
3274 * In the event this update can not be carried out mdadm will
3275 * (FIX ME) notice that its update did not take hold.
3276 */
3277 struct imsm_update_create_array *u = (void *) update->buf;
3278 struct imsm_dev *dev;
3279 struct imsm_map *map, *new_map;
3280 unsigned long long start, end;
3281 unsigned long long new_start, new_end;
3282 int i;
3283 int overlap = 0;
3284
3285 /* handle racing creates: first come first serve */
3286 if (u->dev_idx < mpb->num_raid_devs) {
3287 dprintf("%s: subarray %d already defined\n",
3288 __func__, u->dev_idx);
3289 return;
3290 }
3291
3292 /* check update is next in sequence */
3293 if (u->dev_idx != mpb->num_raid_devs) {
6a3e913e
DW
3294 dprintf("%s: can not create array %d expected index %d\n",
3295 __func__, u->dev_idx, mpb->num_raid_devs);
8273f55e
DW
3296 return;
3297 }
3298
a965f303 3299 new_map = get_imsm_map(&u->dev, 0);
8273f55e
DW
3300 new_start = __le32_to_cpu(new_map->pba_of_lba0);
3301 new_end = new_start + __le32_to_cpu(new_map->blocks_per_member);
3302
3303 /* handle activate_spare versus create race:
3304 * check to make sure that overlapping arrays do not include
3305 * overalpping disks
3306 */
3307 for (i = 0; i < mpb->num_raid_devs; i++) {
949c47a0 3308 dev = get_imsm_dev(super, i);
a965f303 3309 map = get_imsm_map(dev, 0);
8273f55e
DW
3310 start = __le32_to_cpu(map->pba_of_lba0);
3311 end = start + __le32_to_cpu(map->blocks_per_member);
3312 if ((new_start >= start && new_start <= end) ||
3313 (start >= new_start && start <= new_end))
3314 overlap = 1;
ff077194 3315 if (overlap && disks_overlap(dev, &u->dev)) {
8273f55e
DW
3316 dprintf("%s: arrays overlap\n", __func__);
3317 return;
3318 }
3319 }
3320 /* check num_members sanity */
3321 if (new_map->num_members > mpb->num_disks) {
3322 dprintf("%s: num_disks out of range\n", __func__);
3323 return;
3324 }
3325
949c47a0
DW
3326 /* check that prepare update was successful */
3327 if (!update->space) {
3328 dprintf("%s: prepare update failed\n", __func__);
3329 return;
3330 }
3331
8273f55e 3332 super->updates_pending++;
949c47a0 3333 dev = update->space;
ff077194 3334 map = get_imsm_map(dev, 0);
949c47a0
DW
3335 update->space = NULL;
3336 imsm_copy_dev(dev, &u->dev);
e0783b41 3337 map = get_imsm_map(dev, 0);
949c47a0 3338 super->dev_tbl[u->dev_idx] = dev;
8273f55e 3339 mpb->num_raid_devs++;
8273f55e 3340
e0783b41 3341 /* fix up flags */
8273f55e
DW
3342 for (i = 0; i < map->num_members; i++) {
3343 struct imsm_disk *disk;
8273f55e 3344
ff077194 3345 disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i));
f2f27e63
DW
3346 disk->status |= CONFIGURED_DISK;
3347 disk->status &= ~SPARE_DISK;
8273f55e 3348 }
4d1313e9
DW
3349
3350 imsm_update_version_info(super);
3351
8273f55e 3352 break;
e8319a19 3353 }
43dad3d6
DW
3354 case update_add_disk:
3355
3356 /* we may be able to repair some arrays if disks are
3357 * being added */
3358 if (super->add) {
3359 struct active_array *a;
072b727f
DW
3360
3361 super->updates_pending++;
43dad3d6
DW
3362 for (a = st->arrays; a; a = a->next)
3363 a->check_degraded = 1;
3364 }
e553d2a4 3365 /* add some spares to the metadata */
43dad3d6 3366 while (super->add) {
e553d2a4
DW
3367 struct dl *al;
3368
43dad3d6
DW
3369 al = super->add;
3370 super->add = al->next;
43dad3d6
DW
3371 al->next = super->disks;
3372 super->disks = al;
e553d2a4
DW
3373 dprintf("%s: added %x:%x\n",
3374 __func__, al->major, al->minor);
43dad3d6
DW
3375 }
3376
3377 break;
e8319a19
DW
3378 }
3379}
88758e9d 3380
8273f55e
DW
3381static void imsm_prepare_update(struct supertype *st,
3382 struct metadata_update *update)
3383{
949c47a0 3384 /**
4d7b1503
DW
3385 * Allocate space to hold new disk entries, raid-device entries or a new
3386 * mpb if necessary. The manager synchronously waits for updates to
3387 * complete in the monitor, so new mpb buffers allocated here can be
3388 * integrated by the monitor thread without worrying about live pointers
3389 * in the manager thread.
8273f55e 3390 */
949c47a0 3391 enum imsm_update_type type = *(enum imsm_update_type *) update->buf;
4d7b1503
DW
3392 struct intel_super *super = st->sb;
3393 struct imsm_super *mpb = super->anchor;
3394 size_t buf_len;
3395 size_t len = 0;
949c47a0
DW
3396
3397 switch (type) {
3398 case update_create_array: {
3399 struct imsm_update_create_array *u = (void *) update->buf;
949c47a0 3400
4d7b1503 3401 len = sizeof_imsm_dev(&u->dev, 1);
949c47a0
DW
3402 update->space = malloc(len);
3403 break;
3404 default:
3405 break;
3406 }
3407 }
8273f55e 3408
4d7b1503
DW
3409 /* check if we need a larger metadata buffer */
3410 if (super->next_buf)
3411 buf_len = super->next_len;
3412 else
3413 buf_len = super->len;
3414
3415 if (__le32_to_cpu(mpb->mpb_size) + len > buf_len) {
3416 /* ok we need a larger buf than what is currently allocated
3417 * if this allocation fails process_update will notice that
3418 * ->next_len is set and ->next_buf is NULL
3419 */
3420 buf_len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + len, 512);
3421 if (super->next_buf)
3422 free(super->next_buf);
3423
3424 super->next_len = buf_len;
3425 if (posix_memalign(&super->next_buf, buf_len, 512) != 0)
3426 super->next_buf = NULL;
3427 }
8273f55e
DW
3428}
3429
ae6aad82 3430/* must be called while manager is quiesced */
24565c9a 3431static void imsm_delete(struct intel_super *super, struct dl **dlp, int index)
ae6aad82
DW
3432{
3433 struct imsm_super *mpb = super->anchor;
ae6aad82
DW
3434 struct dl *iter;
3435 struct imsm_dev *dev;
3436 struct imsm_map *map;
24565c9a
DW
3437 int i, j, num_members;
3438 __u32 ord;
ae6aad82 3439
24565c9a
DW
3440 dprintf("%s: deleting device[%d] from imsm_super\n",
3441 __func__, index);
ae6aad82
DW
3442
3443 /* shift all indexes down one */
3444 for (iter = super->disks; iter; iter = iter->next)
24565c9a 3445 if (iter->index > index)
ae6aad82 3446 iter->index--;
47ee5a45
DW
3447 for (iter = super->missing; iter; iter = iter->next)
3448 if (iter->index > index)
3449 iter->index--;
ae6aad82
DW
3450
3451 for (i = 0; i < mpb->num_raid_devs; i++) {
3452 dev = get_imsm_dev(super, i);
3453 map = get_imsm_map(dev, 0);
24565c9a
DW
3454 num_members = map->num_members;
3455 for (j = 0; j < num_members; j++) {
3456 /* update ord entries being careful not to propagate
3457 * ord-flags to the first map
3458 */
3459 ord = get_imsm_ord_tbl_ent(dev, j);
ae6aad82 3460
24565c9a
DW
3461 if (ord_to_idx(ord) <= index)
3462 continue;
ae6aad82 3463
24565c9a
DW
3464 map = get_imsm_map(dev, 0);
3465 set_imsm_ord_tbl_ent(map, j, ord_to_idx(ord - 1));
3466 map = get_imsm_map(dev, 1);
3467 if (map)
3468 set_imsm_ord_tbl_ent(map, j, ord - 1);
ae6aad82
DW
3469 }
3470 }
3471
3472