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