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