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