mdmon: don't fork if DEBUG
[thirdparty/mdadm.git] / super-intel.c
CommitLineData
cdddbdbc
DW
1/*
2 * mdadm - Intel(R) Matrix Storage Manager Support
3 *
4 * Copyright (C) 2002-2007 Intel Corporation
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
20#include "mdadm.h"
c2a1e7da 21#include "mdmon.h"
cdddbdbc
DW
22#include <values.h>
23#include <scsi/sg.h>
24#include <ctype.h>
25
26/* MPB == Metadata Parameter Block */
27#define MPB_SIGNATURE "Intel Raid ISM Cfg Sig. "
28#define MPB_SIG_LEN (strlen(MPB_SIGNATURE))
29#define MPB_VERSION_RAID0 "1.0.00"
30#define MPB_VERSION_RAID1 "1.1.00"
31#define MPB_VERSION_RAID5 "1.2.02"
32#define MAX_SIGNATURE_LENGTH 32
33#define MAX_RAID_SERIAL_LEN 16
c2c087e6
DW
34#define MPB_SECTOR_CNT 418
35#define IMSM_RESERVED_SECTORS 4096
cdddbdbc
DW
36
37/* Disk configuration info. */
38#define IMSM_MAX_DEVICES 255
39struct imsm_disk {
40 __u8 serial[MAX_RAID_SERIAL_LEN];/* 0xD8 - 0xE7 ascii serial number */
41 __u32 total_blocks; /* 0xE8 - 0xEB total blocks */
42 __u32 scsi_id; /* 0xEC - 0xEF scsi ID */
43 __u32 status; /* 0xF0 - 0xF3 */
44#define SPARE_DISK 0x01 /* Spare */
45#define CONFIGURED_DISK 0x02 /* Member of some RaidDev */
46#define FAILED_DISK 0x04 /* Permanent failure */
47#define USABLE_DISK 0x08 /* Fully usable unless FAILED_DISK is set */
48
49#define IMSM_DISK_FILLERS 5
50 __u32 filler[IMSM_DISK_FILLERS]; /* 0xF4 - 0x107 MPB_DISK_FILLERS for future expansion */
51};
52
53/* RAID map configuration infos. */
54struct imsm_map {
55 __u32 pba_of_lba0; /* start address of partition */
56 __u32 blocks_per_member;/* blocks per member */
57 __u32 num_data_stripes; /* number of data stripes */
58 __u16 blocks_per_strip;
59 __u8 map_state; /* Normal, Uninitialized, Degraded, Failed */
60#define IMSM_T_STATE_NORMAL 0
61#define IMSM_T_STATE_UNINITIALIZED 1
62#define IMSM_T_STATE_DEGRADED 2 /* FIXME: is this correct? */
63#define IMSM_T_STATE_FAILED 3 /* FIXME: is this correct? */
64 __u8 raid_level;
65#define IMSM_T_RAID0 0
66#define IMSM_T_RAID1 1
67#define IMSM_T_RAID5 5 /* since metadata version 1.2.02 ? */
68 __u8 num_members; /* number of member disks */
69 __u8 reserved[3];
70 __u32 filler[7]; /* expansion area */
71 __u32 disk_ord_tbl[1]; /* disk_ord_tbl[num_members],
72 top byte special */
73} __attribute__ ((packed));
74
75struct imsm_vol {
76 __u32 reserved[2];
77 __u8 migr_state; /* Normal or Migrating */
78 __u8 migr_type; /* Initializing, Rebuilding, ... */
79 __u8 dirty;
80 __u8 fill[1];
81 __u32 filler[5];
82 struct imsm_map map[1];
83 /* here comes another one if migr_state */
84} __attribute__ ((packed));
85
86struct imsm_dev {
87 __u8 volume[MAX_RAID_SERIAL_LEN];
88 __u32 size_low;
89 __u32 size_high;
90 __u32 status; /* Persistent RaidDev status */
91 __u32 reserved_blocks; /* Reserved blocks at beginning of volume */
92#define IMSM_DEV_FILLERS 12
93 __u32 filler[IMSM_DEV_FILLERS];
94 struct imsm_vol vol;
95} __attribute__ ((packed));
96
97struct imsm_super {
98 __u8 sig[MAX_SIGNATURE_LENGTH]; /* 0x00 - 0x1F */
99 __u32 check_sum; /* 0x20 - 0x23 MPB Checksum */
100 __u32 mpb_size; /* 0x24 - 0x27 Size of MPB */
101 __u32 family_num; /* 0x28 - 0x2B Checksum from first time this config was written */
102 __u32 generation_num; /* 0x2C - 0x2F Incremented each time this array's MPB is written */
103 __u32 reserved[2]; /* 0x30 - 0x37 */
104 __u8 num_disks; /* 0x38 Number of configured disks */
105 __u8 num_raid_devs; /* 0x39 Number of configured volumes */
106 __u8 fill[2]; /* 0x3A - 0x3B */
107#define IMSM_FILLERS 39
108 __u32 filler[IMSM_FILLERS]; /* 0x3C - 0xD7 RAID_MPB_FILLERS */
109 struct imsm_disk disk[1]; /* 0xD8 diskTbl[numDisks] */
110 /* here comes imsm_dev[num_raid_devs] */
111} __attribute__ ((packed));
112
113#ifndef MDASSEMBLE
114static char *map_state_str[] = { "normal", "uninitialized", "degraded", "failed" };
115#endif
116
87eb16df 117static unsigned int sector_count(__u32 bytes)
cdddbdbc 118{
87eb16df
DW
119 return ((bytes + (512-1)) & (~(512-1))) / 512;
120}
cdddbdbc 121
87eb16df
DW
122static unsigned int mpb_sectors(struct imsm_super *mpb)
123{
124 return sector_count(__le32_to_cpu(mpb->mpb_size));
cdddbdbc
DW
125}
126
127/* internal representation of IMSM metadata */
128struct intel_super {
129 union {
130 struct imsm_super *mpb;
131 void *buf;
132 };
c2c087e6
DW
133 int updates_pending; /* count of pending updates for mdmon */
134 int creating_imsm; /* flag to indicate container creation */
bf5a934a 135 int current_vol; /* index of raid device undergoing creation */
cdddbdbc
DW
136 struct dl {
137 struct dl *next;
138 int index;
139 __u8 serial[MAX_RAID_SERIAL_LEN];
140 int major, minor;
141 char *devname;
142 int fd;
143 } *disks;
144};
145
c2c087e6
DW
146struct extent {
147 unsigned long long start, size;
148};
149
88758e9d
DW
150/* definition of messages passed to imsm_process_update */
151enum imsm_update_type {
152 update_activate_spare,
8273f55e 153 update_create_array,
88758e9d
DW
154};
155
156struct imsm_update_activate_spare {
157 enum imsm_update_type type;
158 int disk_idx;
159 int slot;
160 int array;
161 struct imsm_update_activate_spare *next;
162};
163
8273f55e
DW
164struct imsm_update_create_array {
165 enum imsm_update_type type;
166 struct imsm_dev dev;
167 int dev_idx;
168};
169
0030e8d6
DW
170static int imsm_env_devname_as_serial(void)
171{
172 char *val = getenv("IMSM_DEVNAME_AS_SERIAL");
173
174 if (val && atoi(val) == 1)
175 return 1;
176
177 return 0;
178}
179
180
cdddbdbc
DW
181static struct supertype *match_metadata_desc_imsm(char *arg)
182{
183 struct supertype *st;
184
185 if (strcmp(arg, "imsm") != 0 &&
186 strcmp(arg, "default") != 0
187 )
188 return NULL;
189
190 st = malloc(sizeof(*st));
ef609477 191 memset(st, 0, sizeof(*st));
cdddbdbc
DW
192 st->ss = &super_imsm;
193 st->max_devs = IMSM_MAX_DEVICES;
194 st->minor_version = 0;
195 st->sb = NULL;
196 return st;
197}
198
cdddbdbc
DW
199static __u8 *get_imsm_version(struct imsm_super *mpb)
200{
201 return &mpb->sig[MPB_SIG_LEN];
202}
203
204static struct imsm_disk *get_imsm_disk(struct imsm_super *mpb, __u8 index)
205{
206 if (index > mpb->num_disks - 1)
207 return NULL;
208 return &mpb->disk[index];
209}
210
211static __u32 gen_imsm_checksum(struct imsm_super *mpb)
212{
213 __u32 end = mpb->mpb_size / sizeof(end);
214 __u32 *p = (__u32 *) mpb;
215 __u32 sum = 0;
216
217 while (end--)
218 sum += __le32_to_cpu(*p++);
219
220 return sum - __le32_to_cpu(mpb->check_sum);
221}
222
223static size_t sizeof_imsm_dev(struct imsm_dev *dev)
224{
225 size_t size = sizeof(*dev);
226
227 /* each map has disk_ord_tbl[num_members - 1] additional space */
228 size += sizeof(__u32) * (dev->vol.map[0].num_members - 1);
229
230 /* migrating means an additional map */
231 if (dev->vol.migr_state) {
232 size += sizeof(struct imsm_map);
233 size += sizeof(__u32) * (dev->vol.map[1].num_members - 1);
234 }
235
236 return size;
237}
238
239static struct imsm_dev *get_imsm_dev(struct imsm_super *mpb, __u8 index)
240{
241 int offset;
242 int i;
243 void *_mpb = mpb;
244
245 if (index > mpb->num_raid_devs - 1)
246 return NULL;
247
248 /* devices start after all disks */
249 offset = ((void *) &mpb->disk[mpb->num_disks]) - _mpb;
250
251 for (i = 0; i <= index; i++)
252 if (i == index)
253 return _mpb + offset;
254 else
255 offset += sizeof_imsm_dev(_mpb + offset);
256
257 return NULL;
258}
259
260static __u32 get_imsm_disk_idx(struct imsm_map *map, int slot)
261{
262 __u32 *ord_tbl = &map->disk_ord_tbl[slot];
263
264 /* top byte is 'special' */
265 return __le32_to_cpu(*ord_tbl & ~(0xff << 24));
266}
267
cdddbdbc
DW
268static int get_imsm_raid_level(struct imsm_map *map)
269{
270 if (map->raid_level == 1) {
271 if (map->num_members == 2)
272 return 1;
273 else
274 return 10;
275 }
276
277 return map->raid_level;
278}
279
c2c087e6
DW
280static int cmp_extent(const void *av, const void *bv)
281{
282 const struct extent *a = av;
283 const struct extent *b = bv;
284 if (a->start < b->start)
285 return -1;
286 if (a->start > b->start)
287 return 1;
288 return 0;
289}
290
291static struct extent *get_extents(struct intel_super *super, struct dl *dl)
292{
293 /* find a list of used extents on the given physical device */
294 struct imsm_super *mpb = super->mpb;
295 struct imsm_disk *disk;
296 struct extent *rv, *e;
297 int i, j;
298 int memberships = 0;
299
300 disk = get_imsm_disk(mpb, dl->index);
301 if (!disk)
302 return NULL;
303
304 for (i = 0; i < mpb->num_raid_devs; i++) {
305 struct imsm_dev *dev = get_imsm_dev(mpb, i);
306 struct imsm_map *map = dev->vol.map;
307
308 for (j = 0; j < map->num_members; j++) {
309 __u32 index = get_imsm_disk_idx(map, j);
310
311 if (index == dl->index)
312 memberships++;
313 }
314 }
315 rv = malloc(sizeof(struct extent) * (memberships + 1));
316 if (!rv)
317 return NULL;
318 e = rv;
319
320 for (i = 0; i < mpb->num_raid_devs; i++) {
321 struct imsm_dev *dev = get_imsm_dev(mpb, i);
322 struct imsm_map *map = dev->vol.map;
323
324 for (j = 0; j < map->num_members; j++) {
325 __u32 index = get_imsm_disk_idx(map, j);
326
327 if (index == dl->index) {
328 e->start = __le32_to_cpu(map->pba_of_lba0);
329 e->size = __le32_to_cpu(map->blocks_per_member);
330 e++;
331 }
332 }
333 }
334 qsort(rv, memberships, sizeof(*rv), cmp_extent);
335
336 e->start = __le32_to_cpu(disk->total_blocks) -
337 (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS);
338 e->size = 0;
339 return rv;
340}
341
4f5bc454 342#ifndef MDASSEMBLE
cdddbdbc
DW
343static void print_imsm_dev(struct imsm_dev *dev, int index)
344{
345 __u64 sz;
346 int slot;
347 struct imsm_map *map = dev->vol.map;
348
349 printf("\n");
350 printf("[%s]:\n", dev->volume);
351 printf(" RAID Level : %d\n", get_imsm_raid_level(map));
352 printf(" Members : %d\n", map->num_members);
353 for (slot = 0; slot < map->num_members; slot++)
354 if (index == get_imsm_disk_idx(map, slot))
355 break;
356 if (slot < map->num_members)
357 printf(" This Slot : %d\n", slot);
358 else
359 printf(" This Slot : ?\n");
360 sz = __le32_to_cpu(dev->size_high);
361 sz <<= 32;
362 sz += __le32_to_cpu(dev->size_low);
363 printf(" Array Size : %llu%s\n", (unsigned long long)sz,
364 human_size(sz * 512));
365 sz = __le32_to_cpu(map->blocks_per_member);
366 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz,
367 human_size(sz * 512));
368 printf(" Sector Offset : %u\n",
369 __le32_to_cpu(map->pba_of_lba0));
370 printf(" Num Stripes : %u\n",
371 __le32_to_cpu(map->num_data_stripes));
372 printf(" Chunk Size : %u KiB\n",
373 __le16_to_cpu(map->blocks_per_strip) / 2);
374 printf(" Reserved : %d\n", __le32_to_cpu(dev->reserved_blocks));
375 printf(" Migrate State : %s\n", dev->vol.migr_state ? "migrating" : "idle");
376 printf(" Dirty State : %s\n", dev->vol.dirty ? "dirty" : "clean");
377 printf(" Map State : %s\n", map_state_str[map->map_state]);
378}
379
380static void print_imsm_disk(struct imsm_super *mpb, int index)
381{
382 struct imsm_disk *disk = get_imsm_disk(mpb, index);
383 char str[MAX_RAID_SERIAL_LEN];
384 __u32 s;
385 __u64 sz;
386
e9d82038
DW
387 if (index < 0)
388 return;
389
cdddbdbc
DW
390 printf("\n");
391 snprintf(str, MAX_RAID_SERIAL_LEN, "%s", disk->serial);
392 printf(" Disk%02d Serial : %s\n", index, str);
393 s = __le32_to_cpu(disk->status);
394 printf(" State :%s%s%s%s\n", s&SPARE_DISK ? " spare" : "",
395 s&CONFIGURED_DISK ? " active" : "",
396 s&FAILED_DISK ? " failed" : "",
397 s&USABLE_DISK ? " usable" : "");
398 printf(" Id : %08x\n", __le32_to_cpu(disk->scsi_id));
c2c087e6
DW
399 sz = __le32_to_cpu(disk->total_blocks) -
400 (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS * mpb->num_raid_devs);
cdddbdbc
DW
401 printf(" Usable Size : %llu%s\n", (unsigned long long)sz,
402 human_size(sz * 512));
403}
404
405static void examine_super_imsm(struct supertype *st, char *homehost)
406{
407 struct intel_super *super = st->sb;
408 struct imsm_super *mpb = super->mpb;
409 char str[MAX_SIGNATURE_LENGTH];
410 int i;
411 __u32 sum;
412
413 snprintf(str, MPB_SIG_LEN, "%s", mpb->sig);
414 printf(" Magic : %s\n", str);
415 snprintf(str, strlen(MPB_VERSION_RAID0), "%s", get_imsm_version(mpb));
416 printf(" Version : %s\n", get_imsm_version(mpb));
417 printf(" Family : %08x\n", __le32_to_cpu(mpb->family_num));
418 printf(" Generation : %08x\n", __le32_to_cpu(mpb->generation_num));
419 sum = __le32_to_cpu(mpb->check_sum);
420 printf(" Checksum : %08x %s\n", sum,
421 gen_imsm_checksum(mpb) == sum ? "correct" : "incorrect");
87eb16df 422 printf(" MPB Sectors : %d\n", mpb_sectors(mpb));
cdddbdbc
DW
423 printf(" Disks : %d\n", mpb->num_disks);
424 printf(" RAID Devices : %d\n", mpb->num_raid_devs);
425 print_imsm_disk(mpb, super->disks->index);
426 for (i = 0; i < mpb->num_raid_devs; i++)
427 print_imsm_dev(get_imsm_dev(mpb, i), super->disks->index);
428 for (i = 0; i < mpb->num_disks; i++) {
429 if (i == super->disks->index)
430 continue;
431 print_imsm_disk(mpb, i);
432 }
433}
434
435static void brief_examine_super_imsm(struct supertype *st)
436{
437 struct intel_super *super = st->sb;
438 struct imsm_super *mpb = super->mpb;
439
440 printf("ARRAY /dev/imsm family=%08x metadata=external:imsm\n",
441 __le32_to_cpu(mpb->family_num));
442}
443
444static void detail_super_imsm(struct supertype *st, char *homehost)
445{
446 printf("%s\n", __FUNCTION__);
447}
448
449static void brief_detail_super_imsm(struct supertype *st)
450{
451 printf("%s\n", __FUNCTION__);
452}
453#endif
454
455static int match_home_imsm(struct supertype *st, char *homehost)
456{
457 printf("%s\n", __FUNCTION__);
458
459 return 0;
460}
461
462static void uuid_from_super_imsm(struct supertype *st, int uuid[4])
463{
464 printf("%s\n", __FUNCTION__);
465}
466
0d481d37 467#if 0
4f5bc454
DW
468static void
469get_imsm_numerical_version(struct imsm_super *mpb, int *m, int *p)
cdddbdbc 470{
cdddbdbc
DW
471 __u8 *v = get_imsm_version(mpb);
472 __u8 *end = mpb->sig + MAX_SIGNATURE_LENGTH;
473 char major[] = { 0, 0, 0 };
474 char minor[] = { 0 ,0, 0 };
475 char patch[] = { 0, 0, 0 };
476 char *ver_parse[] = { major, minor, patch };
477 int i, j;
478
479 i = j = 0;
480 while (*v != '\0' && v < end) {
481 if (*v != '.' && j < 2)
482 ver_parse[i][j++] = *v;
483 else {
484 i++;
485 j = 0;
486 }
487 v++;
488 }
489
4f5bc454
DW
490 *m = strtol(minor, NULL, 0);
491 *p = strtol(patch, NULL, 0);
492}
0d481d37 493#endif
4f5bc454 494
c2c087e6
DW
495static int imsm_level_to_layout(int level)
496{
497 switch (level) {
498 case 0:
499 case 1:
500 return 0;
501 case 5:
502 case 6:
503 return ALGORITHM_LEFT_SYMMETRIC;
504 case 10:
505 return 0x102; //FIXME is this correct?
506 }
507 return -1;
508}
509
bf5a934a
DW
510static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info)
511{
512 struct intel_super *super = st->sb;
513 struct imsm_super *mpb = super->mpb;
514 struct imsm_dev *dev = get_imsm_dev(mpb, super->current_vol);
515 struct imsm_map *map = &dev->vol.map[0];
516
517 info->container_member = super->current_vol;
518 info->array.raid_disks = map->num_members;
519 info->array.level = get_imsm_raid_level(map);
520 info->array.layout = imsm_level_to_layout(info->array.level);
521 info->array.md_minor = -1;
522 info->array.ctime = 0;
523 info->array.utime = 0;
524 info->array.chunk_size = __le16_to_cpu(map->blocks_per_strip * 512);
525
526 info->data_offset = __le32_to_cpu(map->pba_of_lba0);
527 info->component_size = __le32_to_cpu(map->blocks_per_member);
528
529 info->disk.major = 0;
530 info->disk.minor = 0;
531
532 sprintf(info->text_version, "/%s/%d",
533 devnum2devname(st->container_dev),
534 info->container_member);
535}
536
537
4f5bc454
DW
538static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info)
539{
540 struct intel_super *super = st->sb;
541 struct imsm_super *mpb = super->mpb;
542 struct imsm_disk *disk;
543 __u32 s;
4f5bc454 544
bf5a934a
DW
545 if (super->current_vol >= 0) {
546 getinfo_super_imsm_volume(st, info);
547 return;
548 }
cdddbdbc
DW
549 info->array.raid_disks = mpb->num_disks;
550 info->array.level = LEVEL_CONTAINER;
551 info->array.layout = 0;
552 info->array.md_minor = -1;
c2c087e6 553 info->array.ctime = 0; /* N/A for imsm */
cdddbdbc
DW
554 info->array.utime = 0;
555 info->array.chunk_size = 0;
556
557 info->disk.major = 0;
558 info->disk.minor = 0;
cdddbdbc 559 info->disk.raid_disk = -1;
c2c087e6
DW
560 info->reshape_active = 0;
561 strcpy(info->text_version, "imsm");
562 info->disk.number = -1;
563 info->disk.state = 0;
564
4a04ec6c 565 if (super->disks) {
bf5a934a 566 disk = get_imsm_disk(mpb, super->disks->index);
e9d82038
DW
567 if (!disk) {
568 info->disk.number = -1;
569 info->disk.raid_disk = -1;
570 return;
571 }
4a04ec6c
DW
572 info->disk.number = super->disks->index;
573 info->disk.raid_disk = super->disks->index;
bf5a934a
DW
574 info->data_offset = __le32_to_cpu(disk->total_blocks) -
575 (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS);
576 info->component_size = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
4a04ec6c
DW
577 s = __le32_to_cpu(disk->status);
578 info->disk.state = s & CONFIGURED_DISK ? (1 << MD_DISK_ACTIVE) : 0;
579 info->disk.state |= s & FAILED_DISK ? (1 << MD_DISK_FAULTY) : 0;
580 info->disk.state |= s & USABLE_DISK ? (1 << MD_DISK_SYNC) : 0;
cdddbdbc 581 }
cdddbdbc
DW
582}
583
cdddbdbc
DW
584static int update_super_imsm(struct supertype *st, struct mdinfo *info,
585 char *update, char *devname, int verbose,
586 int uuid_set, char *homehost)
587{
f352c545
DW
588 /* FIXME */
589
590 /* For 'assemble' and 'force' we need to return non-zero if any
591 * change was made. For others, the return value is ignored.
592 * Update options are:
593 * force-one : This device looks a bit old but needs to be included,
594 * update age info appropriately.
595 * assemble: clear any 'faulty' flag to allow this device to
596 * be assembled.
597 * force-array: Array is degraded but being forced, mark it clean
598 * if that will be needed to assemble it.
599 *
600 * newdev: not used ????
601 * grow: Array has gained a new device - this is currently for
602 * linear only
603 * resync: mark as dirty so a resync will happen.
604 * name: update the name - preserving the homehost
605 *
606 * Following are not relevant for this imsm:
607 * sparc2.2 : update from old dodgey metadata
608 * super-minor: change the preferred_minor number
609 * summaries: update redundant counters.
610 * uuid: Change the uuid of the array to match watch is given
611 * homehost: update the recorded homehost
612 * _reshape_progress: record new reshape_progress position.
613 */
614 int rv = 0;
615 //struct intel_super *super = st->sb;
616 //struct imsm_super *mpb = super->mpb;
617
618 if (strcmp(update, "grow") == 0) {
619 }
620 if (strcmp(update, "resync") == 0) {
621 /* dev->vol.dirty = 1; */
622 }
623
624 /* IMSM has no concept of UUID or homehost */
625
626 return rv;
cdddbdbc
DW
627}
628
c2c087e6 629static size_t disks_to_mpb_size(int disks)
cdddbdbc 630{
c2c087e6 631 size_t size;
cdddbdbc 632
c2c087e6
DW
633 size = sizeof(struct imsm_super);
634 size += (disks - 1) * sizeof(struct imsm_disk);
635 size += 2 * sizeof(struct imsm_dev);
636 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
637 size += (4 - 2) * sizeof(struct imsm_map);
638 /* 4 possible disk_ord_tbl's */
639 size += 4 * (disks - 1) * sizeof(__u32);
640
641 return size;
642}
643
644static __u64 avail_size_imsm(struct supertype *st, __u64 devsize)
645{
646 if (devsize < (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS))
647 return 0;
648
649 return devsize - (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS);
cdddbdbc
DW
650}
651
652static int compare_super_imsm(struct supertype *st, struct supertype *tst)
653{
654 /*
655 * return:
656 * 0 same, or first was empty, and second was copied
657 * 1 second had wrong number
658 * 2 wrong uuid
659 * 3 wrong other info
660 */
661 struct intel_super *first = st->sb;
662 struct intel_super *sec = tst->sb;
663
664 if (!first) {
665 st->sb = tst->sb;
666 tst->sb = NULL;
667 return 0;
668 }
669
670 if (memcmp(first->mpb->sig, sec->mpb->sig, MAX_SIGNATURE_LENGTH) != 0)
671 return 3;
672 if (first->mpb->family_num != sec->mpb->family_num)
673 return 3;
674 if (first->mpb->mpb_size != sec->mpb->mpb_size)
675 return 3;
676 if (first->mpb->check_sum != sec->mpb->check_sum)
677 return 3;
678
679 return 0;
680}
681
0030e8d6
DW
682static void fd2devname(int fd, char *name)
683{
684 struct stat st;
685 char path[256];
686 char dname[100];
687 char *nm;
688 int rv;
689
690 name[0] = '\0';
691 if (fstat(fd, &st) != 0)
692 return;
693 sprintf(path, "/sys/dev/block/%d:%d",
694 major(st.st_rdev), minor(st.st_rdev));
695
696 rv = readlink(path, dname, sizeof(dname));
697 if (rv <= 0)
698 return;
699
700 dname[rv] = '\0';
701 nm = strrchr(dname, '/');
702 nm++;
703 snprintf(name, MAX_RAID_SERIAL_LEN, "/dev/%s", nm);
704}
705
706
cdddbdbc
DW
707extern int scsi_get_serial(int fd, void *buf, size_t buf_len);
708
709static int imsm_read_serial(int fd, char *devname,
710 __u8 serial[MAX_RAID_SERIAL_LEN])
711{
712 unsigned char scsi_serial[255];
cdddbdbc
DW
713 int rv;
714 int rsp_len;
715 int i, cnt;
716
717 memset(scsi_serial, 0, sizeof(scsi_serial));
cdddbdbc 718
0030e8d6
DW
719 if (imsm_env_devname_as_serial()) {
720 char name[MAX_RAID_SERIAL_LEN];
721
722 fd2devname(fd, name);
723 strcpy((char *) serial, name);
724 return 0;
725 }
726
755c99fa 727 rv = scsi_get_serial(fd, scsi_serial, sizeof(scsi_serial));
cdddbdbc
DW
728
729 if (rv != 0) {
730 if (devname)
731 fprintf(stderr,
732 Name ": Failed to retrieve serial for %s\n",
733 devname);
734 return rv;
735 }
736
737 rsp_len = scsi_serial[3];
738 for (i = 0, cnt = 0; i < rsp_len; i++) {
739 if (!isspace(scsi_serial[4 + i]))
740 serial[cnt++] = scsi_serial[4 + i];
741 if (cnt == MAX_RAID_SERIAL_LEN)
742 break;
743 }
744
745 serial[MAX_RAID_SERIAL_LEN - 1] = '\0';
746
747 return 0;
748}
749
750static int
751load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd)
752{
753 struct imsm_super *mpb = super->mpb;
754 struct dl *dl;
755 struct stat stb;
756 struct imsm_disk *disk;
757 int rv;
758 int i;
759
760 dl = malloc(sizeof(*dl));
761 if (!dl) {
762 if (devname)
763 fprintf(stderr,
764 Name ": failed to allocate disk buffer for %s\n",
765 devname);
766 return 2;
767 }
768 memset(dl, 0, sizeof(*dl));
769
770 fstat(fd, &stb);
771 dl->major = major(stb.st_rdev);
772 dl->minor = minor(stb.st_rdev);
773 dl->next = super->disks;
774 dl->fd = keep_fd ? fd : -1;
775 dl->devname = devname ? strdup(devname) : NULL;
776 dl->index = -1;
777 super->disks = dl;
778 rv = imsm_read_serial(fd, devname, dl->serial);
779
780 if (rv != 0)
781 return 2;
782
783 /* look up this disk's index */
784 for (i = 0; i < mpb->num_disks; i++) {
785 disk = get_imsm_disk(mpb, i);
786
787 if (memcmp(disk->serial, dl->serial, MAX_RAID_SERIAL_LEN) == 0)
788 break;
789 }
790
e9d82038
DW
791 if (i > mpb->num_disks - 1) {
792 if (devname)
793 fprintf(stderr,
794 Name ": failed to match serial \'%s\' for %s\n",
795 dl->serial, devname);
796 return 0;
797 }
cdddbdbc
DW
798
799 dl->index = i;
800
801 return 0;
802}
803
804/* load_imsm_mpb - read matrix metadata
805 * allocates super->mpb to be freed by free_super
806 */
807static int load_imsm_mpb(int fd, struct intel_super *super, char *devname)
808{
809 unsigned long long dsize;
810 size_t len, mpb_size;
811 unsigned long long sectors;
812 struct stat;
6416d527 813 struct imsm_super *anchor;
cdddbdbc
DW
814 __u32 check_sum;
815
cdddbdbc
DW
816 get_dev_size(fd, NULL, &dsize);
817
818 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0) {
819 if (devname)
820 fprintf(stderr,
821 Name ": Cannot seek to anchor block on %s: %s\n",
822 devname, strerror(errno));
823 return 1;
824 }
825
6416d527 826 len = 512;
ad97895e
DW
827 if (posix_memalign((void**)&anchor, 512, len) != 0) {
828 if (devname)
829 fprintf(stderr,
830 Name ": Failed to allocate imsm anchor buffer"
831 " on %s\n", devname);
832 return 1;
833 }
6416d527 834 if (read(fd, anchor, len) != len) {
cdddbdbc
DW
835 if (devname)
836 fprintf(stderr,
837 Name ": Cannot read anchor block on %s: %s\n",
838 devname, strerror(errno));
6416d527 839 free(anchor);
cdddbdbc
DW
840 return 1;
841 }
842
6416d527 843 if (strncmp((char *) anchor->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) {
cdddbdbc
DW
844 if (devname)
845 fprintf(stderr,
846 Name ": no IMSM anchor on %s\n", devname);
6416d527 847 free(anchor);
cdddbdbc
DW
848 return 2;
849 }
850
6416d527
NB
851 mpb_size = __le32_to_cpu(anchor->mpb_size);
852 mpb_size = ROUND_UP(mpb_size, 512);
ef649044 853 if (posix_memalign(&super->buf, 512, mpb_size) != 0) {
cdddbdbc
DW
854 if (devname)
855 fprintf(stderr,
856 Name ": unable to allocate %zu byte mpb buffer\n",
857 mpb_size);
6416d527 858 free(anchor);
cdddbdbc
DW
859 return 2;
860 }
6416d527 861 memcpy(super->buf, anchor, len);
cdddbdbc 862
6416d527
NB
863 sectors = mpb_sectors(anchor) - 1;
864 free(anchor);
cdddbdbc
DW
865 if (!sectors)
866 return load_imsm_disk(fd, super, devname, 0);
867
868 /* read the extended mpb */
869 if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0) {
870 if (devname)
871 fprintf(stderr,
872 Name ": Cannot seek to extended mpb on %s: %s\n",
873 devname, strerror(errno));
874 return 1;
875 }
876
877 len = mpb_size - 512;
878 if (read(fd, super->buf + 512, len) != len) {
879 if (devname)
880 fprintf(stderr,
881 Name ": Cannot read extended mpb on %s: %s\n",
882 devname, strerror(errno));
883 return 2;
884 }
885
886 check_sum = gen_imsm_checksum(super->mpb);
887 if (check_sum != __le32_to_cpu(super->mpb->check_sum)) {
888 if (devname)
889 fprintf(stderr,
890 Name ": IMSM checksum %x != %x on %s\n",
891 check_sum, __le32_to_cpu(super->mpb->check_sum),
892 devname);
893 return 2;
894 }
895
896 return load_imsm_disk(fd, super, devname, 0);
897}
898
cdddbdbc
DW
899static void free_imsm_disks(struct intel_super *super)
900{
901 while (super->disks) {
902 struct dl *d = super->disks;
903
904 super->disks = d->next;
905 if (d->fd >= 0)
906 close(d->fd);
907 if (d->devname)
908 free(d->devname);
909 free(d);
910 }
911}
912
913static void free_imsm(struct intel_super *super)
914{
915 if (super->mpb)
916 free(super->mpb);
917 free_imsm_disks(super);
918 free(super);
919}
920
921
922static void free_super_imsm(struct supertype *st)
923{
924 struct intel_super *super = st->sb;
925
926 if (!super)
927 return;
928
929 free_imsm(super);
930 st->sb = NULL;
931}
932
c2c087e6
DW
933static struct intel_super *alloc_super(int creating_imsm)
934{
935 struct intel_super *super = malloc(sizeof(*super));
936
937 if (super) {
938 memset(super, 0, sizeof(*super));
939 super->creating_imsm = creating_imsm;
bf5a934a 940 super->current_vol = -1;
c2c087e6
DW
941 }
942
943 return super;
944}
945
cdddbdbc
DW
946#ifndef MDASSEMBLE
947static int load_super_imsm_all(struct supertype *st, int fd, void **sbp,
948 char *devname, int keep_fd)
949{
950 struct mdinfo *sra;
951 struct intel_super *super;
952 struct mdinfo *sd, *best = NULL;
953 __u32 bestgen = 0;
954 __u32 gen;
955 char nm[20];
956 int dfd;
957 int rv;
958
959 /* check if this disk is a member of an active array */
960 sra = sysfs_read(fd, 0, GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE);
961 if (!sra)
962 return 1;
963
964 if (sra->array.major_version != -1 ||
965 sra->array.minor_version != -2 ||
966 strcmp(sra->text_version, "imsm") != 0)
967 return 1;
968
c2c087e6 969 super = alloc_super(0);
cdddbdbc
DW
970 if (!super)
971 return 1;
972
973 /* find the most up to date disk in this array */
974 for (sd = sra->devs; sd; sd = sd->next) {
975 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
976 dfd = dev_open(nm, keep_fd ? O_RDWR : O_RDONLY);
977 if (!dfd) {
978 free_imsm(super);
979 return 2;
980 }
981 rv = load_imsm_mpb(dfd, super, NULL);
982 if (!keep_fd)
983 close(dfd);
984 if (rv == 0) {
985 gen = __le32_to_cpu(super->mpb->generation_num);
986 if (!best || gen > bestgen) {
987 bestgen = gen;
988 best = sd;
989 }
990 } else {
991 free_imsm(super);
992 return 2;
993 }
994 }
995
996 if (!best) {
997 free_imsm(super);
998 return 1;
999 }
1000
1001 /* load the most up to date anchor */
1002 sprintf(nm, "%d:%d", best->disk.major, best->disk.minor);
1003 dfd = dev_open(nm, O_RDONLY);
1004 if (!dfd) {
1005 free_imsm(super);
1006 return 1;
1007 }
1008 rv = load_imsm_mpb(dfd, super, NULL);
1009 close(dfd);
1010 if (rv != 0) {
1011 free_imsm(super);
1012 return 2;
1013 }
1014
1015 /* reset the disk list */
1016 free_imsm_disks(super);
1017
1018 /* populate disk list */
1019 for (sd = sra->devs ; sd ; sd = sd->next) {
1020 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
1021 dfd = dev_open(nm, keep_fd? O_RDWR : O_RDONLY);
1022 if (!dfd) {
1023 free_imsm(super);
1024 return 2;
1025 }
1026 load_imsm_disk(dfd, super, NULL, keep_fd);
1027 if (!keep_fd)
1028 close(dfd);
1029 }
1030
f7e7067b 1031 if (st->subarray[0]) {
bf5a934a
DW
1032 if (atoi(st->subarray) <= super->mpb->num_raid_devs)
1033 super->current_vol = atoi(st->subarray);
1034 else
1035 return 1;
f7e7067b
NB
1036 }
1037
cdddbdbc
DW
1038 *sbp = super;
1039 if (st->ss == NULL) {
bf5a934a 1040 st->ss = &super_imsm;
cdddbdbc
DW
1041 st->minor_version = 0;
1042 st->max_devs = IMSM_MAX_DEVICES;
f4d11639 1043 st->container_dev = fd2devnum(fd);
cdddbdbc
DW
1044 }
1045
1046 return 0;
1047}
1048#endif
1049
1050static int load_super_imsm(struct supertype *st, int fd, char *devname)
1051{
1052 struct intel_super *super;
1053 int rv;
1054
1055#ifndef MDASSEMBLE
3dbccbcf 1056 if (load_super_imsm_all(st, fd, &st->sb, devname, 1) == 0)
cdddbdbc
DW
1057 return 0;
1058#endif
f7e7067b
NB
1059 if (st->subarray[0])
1060 return 1; /* FIXME */
cdddbdbc 1061
c2c087e6 1062 super = alloc_super(0);
cdddbdbc
DW
1063 if (!super) {
1064 fprintf(stderr,
1065 Name ": malloc of %zu failed.\n",
1066 sizeof(*super));
1067 return 1;
1068 }
1069
1070 rv = load_imsm_mpb(fd, super, devname);
1071
1072 if (rv) {
1073 if (devname)
1074 fprintf(stderr,
1075 Name ": Failed to load all information "
1076 "sections on %s\n", devname);
1077 free_imsm(super);
1078 return rv;
1079 }
1080
1081 st->sb = super;
1082 if (st->ss == NULL) {
1083 st->ss = &super_imsm;
1084 st->minor_version = 0;
1085 st->max_devs = IMSM_MAX_DEVICES;
1086 }
1087
1088 return 0;
1089}
1090
ef6ffade
DW
1091static __u16 info_to_blocks_per_strip(mdu_array_info_t *info)
1092{
1093 if (info->level == 1)
1094 return 128;
1095 return info->chunk_size >> 9;
1096}
1097
1098static __u32 info_to_num_data_stripes(mdu_array_info_t *info)
1099{
1100 __u32 num_stripes;
1101
1102 num_stripes = (info->size * 2) / info_to_blocks_per_strip(info);
1103 if (info->level == 1)
1104 num_stripes /= 2;
1105
1106 return num_stripes;
1107}
1108
fcfd9599
DW
1109static __u32 info_to_blocks_per_member(mdu_array_info_t *info)
1110{
1111 return (info->size * 2) & ~(info_to_blocks_per_strip(info) - 1);
1112}
1113
8b353278
DW
1114static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
1115 unsigned long long size, char *name,
1116 char *homehost, int *uuid)
cdddbdbc 1117{
c2c087e6
DW
1118 /* We are creating a volume inside a pre-existing container.
1119 * so st->sb is already set.
1120 */
1121 struct intel_super *super = st->sb;
1122 struct imsm_super *mpb = super->mpb;
1123 struct imsm_dev *dev;
1124 struct imsm_vol *vol;
1125 struct imsm_map *map;
1126 int idx = mpb->num_raid_devs;
1127 int i;
1128 unsigned long long array_blocks;
c2c087e6 1129 __u32 offset = 0;
2c092cad 1130 size_t size_old, size_new;
cdddbdbc 1131
c2c087e6
DW
1132 if (mpb->num_raid_devs >= 2) {
1133 fprintf(stderr, Name": This imsm-container already has the "
1134 "maximum of 2 volumes\n");
1135 return 0;
1136 }
1137
2c092cad
DW
1138 /* ensure the mpb is large enough for the new data */
1139 size_old = __le32_to_cpu(mpb->mpb_size);
1140 size_new = disks_to_mpb_size(info->nr_disks);
1141 if (size_new > size_old) {
1142 void *mpb_new;
1143 size_t size_round = ROUND_UP(size_new, 512);
1144
1145 if (posix_memalign(&mpb_new, 512, size_round) != 0) {
1146 fprintf(stderr, Name": could not allocate new mpb\n");
1147 return 0;
1148 }
1149 memcpy(mpb_new, mpb, size_old);
1150 free(mpb);
1151 mpb = mpb_new;
1152 super->mpb = mpb_new;
1153 mpb->mpb_size = __cpu_to_le32(size_new);
1154 memset(mpb_new + size_old, 0, size_round - size_old);
1155 }
bf5a934a
DW
1156 super->current_vol = idx;
1157 sprintf(st->subarray, "%d", idx);
c2c087e6
DW
1158 mpb->num_raid_devs++;
1159 dev = get_imsm_dev(mpb, idx);
1160 strncpy((char *) dev->volume, name, MAX_RAID_SERIAL_LEN);
1161 array_blocks = calc_array_size(info->level, info->raid_disks,
1162 info->layout, info->chunk_size,
1163 info->size*2);
1164 dev->size_low = __cpu_to_le32((__u32) array_blocks);
1165 dev->size_high = __cpu_to_le32((__u32) (array_blocks >> 32));
1166 dev->status = __cpu_to_le32(0);
1167 dev->reserved_blocks = __cpu_to_le32(0);
1168 vol = &dev->vol;
1169 vol->migr_state = 0;
1170 vol->migr_type = 0;
1171 vol->dirty = 0;
1172 for (i = 0; i < idx; i++) {
1173 struct imsm_dev *prev = get_imsm_dev(mpb, i);
1174 struct imsm_map *pmap = &prev->vol.map[0];
1175
1176 offset += __le32_to_cpu(pmap->blocks_per_member);
1177 offset += IMSM_RESERVED_SECTORS;
1178 }
1179 map = &vol->map[0];
1180 map->pba_of_lba0 = __cpu_to_le32(offset);
fcfd9599 1181 map->blocks_per_member = __cpu_to_le32(info_to_blocks_per_member(info));
ef6ffade
DW
1182 map->blocks_per_strip = __cpu_to_le16(info_to_blocks_per_strip(info));
1183 map->num_data_stripes = __cpu_to_le32(info_to_num_data_stripes(info));
c2c087e6
DW
1184 map->map_state = info->level ? IMSM_T_STATE_UNINITIALIZED :
1185 IMSM_T_STATE_NORMAL;
ef6ffade
DW
1186
1187 if (info->level == 1 && info->raid_disks > 2) {
1188 fprintf(stderr, Name": imsm does not support more than 2 disks"
1189 "in a raid1 volume\n");
1190 return 0;
1191 }
c2c087e6
DW
1192 if (info->level == 10)
1193 map->raid_level = 1;
1194 else
1195 map->raid_level = info->level;
ef6ffade 1196
c2c087e6
DW
1197 map->num_members = info->raid_disks;
1198 for (i = 0; i < map->num_members; i++) {
1199 /* initialized in add_to_super */
1200 map->disk_ord_tbl[i] = __cpu_to_le32(0);
1201 }
1202
1203 return 1;
cdddbdbc
DW
1204}
1205
bf5a934a
DW
1206static int init_super_imsm(struct supertype *st, mdu_array_info_t *info,
1207 unsigned long long size, char *name,
1208 char *homehost, int *uuid)
1209{
1210 /* This is primarily called by Create when creating a new array.
1211 * We will then get add_to_super called for each component, and then
1212 * write_init_super called to write it out to each device.
1213 * For IMSM, Create can create on fresh devices or on a pre-existing
1214 * array.
1215 * To create on a pre-existing array a different method will be called.
1216 * This one is just for fresh drives.
1217 */
1218 struct intel_super *super;
1219 struct imsm_super *mpb;
1220 size_t mpb_size;
1221
1222 if (!info) {
1223 st->sb = NULL;
1224 return 0;
1225 }
1226 if (st->sb)
1227 return init_super_imsm_volume(st, info, size, name, homehost,
1228 uuid);
1229
1230 super = alloc_super(1);
1231 if (!super)
1232 return 0;
1233 mpb_size = disks_to_mpb_size(info->nr_disks);
ef649044 1234 if (posix_memalign(&super->buf, 512, mpb_size) != 0) {
bf5a934a
DW
1235 free(super);
1236 return 0;
1237 }
ef649044 1238 mpb = super->buf;
bf5a934a
DW
1239 memset(mpb, 0, mpb_size);
1240
1241 memcpy(mpb->sig, MPB_SIGNATURE, strlen(MPB_SIGNATURE));
1242 memcpy(mpb->sig + strlen(MPB_SIGNATURE), MPB_VERSION_RAID5,
1243 strlen(MPB_VERSION_RAID5));
1244 mpb->mpb_size = mpb_size;
1245
bf5a934a
DW
1246 st->sb = super;
1247 return 1;
1248}
1249
1250static void add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk,
1251 int fd, char *devname)
1252{
1253 struct intel_super *super = st->sb;
1254 struct imsm_super *mpb = super->mpb;
1255 struct dl *dl;
1256 struct imsm_dev *dev;
1257 struct imsm_map *map;
1258 struct imsm_disk *disk;
1259 __u32 status;
1260
1261 dev = get_imsm_dev(mpb, super->current_vol);
1262 map = &dev->vol.map[0];
1263
1264 for (dl = super->disks; dl ; dl = dl->next)
1265 if (dl->major == dk->major &&
1266 dl->minor == dk->minor)
1267 break;
1268 if (!dl || ! (dk->state & (1<<MD_DISK_SYNC)))
1269 return;
1270
1271 map->disk_ord_tbl[dk->number] = __cpu_to_le32(dl->index);
1272
1273 disk = get_imsm_disk(mpb, dl->index);
1274 status = CONFIGURED_DISK | USABLE_DISK;
1275 disk->status = __cpu_to_le32(status);
1276}
1277
c2c087e6 1278static void add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
cdddbdbc
DW
1279 int fd, char *devname)
1280{
c2c087e6
DW
1281 struct intel_super *super = st->sb;
1282 struct imsm_super *mpb = super->mpb;
1283 struct imsm_disk *disk;
1284 struct dl *dd;
1285 unsigned long long size;
1286 __u32 status, id;
1287 int rv;
1288 struct stat stb;
1289
bf5a934a
DW
1290 if (super->current_vol >= 0) {
1291 add_to_super_imsm_volume(st, dk, fd, devname);
1292 return;
1293 }
1294
c2c087e6
DW
1295 fstat(fd, &stb);
1296 dd = malloc(sizeof(*dd));
1297 if (!dd) {
1298 fprintf(stderr,
1299 Name ": malloc failed %s:%d.\n", __func__, __LINE__);
1300 abort();
1301 }
1302 memset(dd, 0, sizeof(*dd));
1303 dd->major = major(stb.st_rdev);
1304 dd->minor = minor(stb.st_rdev);
1305 dd->index = dk->number;
1306 dd->devname = devname ? strdup(devname) : NULL;
1307 dd->next = super->disks;
1308 dd->fd = fd;
1309 rv = imsm_read_serial(fd, devname, dd->serial);
1310 if (rv) {
1311 fprintf(stderr,
0030e8d6
DW
1312 Name ": failed to retrieve scsi serial, aborting\n");
1313 abort();
c2c087e6
DW
1314 }
1315
1316 if (mpb->num_disks <= dk->number)
1317 mpb->num_disks = dk->number + 1;
1318
1319 disk = get_imsm_disk(mpb, dk->number);
1320 get_dev_size(fd, NULL, &size);
1321 size /= 512;
1322 status = USABLE_DISK | SPARE_DISK;
1323 strcpy((char *) disk->serial, (char *) dd->serial);
1324 disk->total_blocks = __cpu_to_le32(size);
1325 disk->status = __cpu_to_le32(status);
1326 if (sysfs_disk_to_scsi_id(fd, &id) == 0)
1327 disk->scsi_id = __cpu_to_le32(id);
1328 else
1329 disk->scsi_id = __cpu_to_le32(0);
1330
1331 /* update the family number if we are creating a container */
1332 if (super->creating_imsm)
1333 mpb->family_num = __cpu_to_le32(gen_imsm_checksum(mpb));
1334
1335 super->disks = dd;
cdddbdbc
DW
1336}
1337
c2c087e6
DW
1338static int store_imsm_mpb(int fd, struct intel_super *super);
1339
1340static int write_super_imsm(struct intel_super *super, int doclose)
cdddbdbc 1341{
c2c087e6
DW
1342 struct imsm_super *mpb = super->mpb;
1343 struct dl *d;
1344 __u32 generation;
1345 __u32 sum;
cdddbdbc 1346
c2c087e6
DW
1347 /* 'generation' is incremented everytime the metadata is written */
1348 generation = __le32_to_cpu(mpb->generation_num);
1349 generation++;
1350 mpb->generation_num = __cpu_to_le32(generation);
1351
1352 /* recalculate checksum */
1353 sum = gen_imsm_checksum(mpb);
1354 mpb->check_sum = __cpu_to_le32(sum);
1355
1356 for (d = super->disks; d ; d = d->next) {
1357 if (store_imsm_mpb(d->fd, super)) {
1358 fprintf(stderr, "%s: failed for device %d:%d %s\n",
1359 __func__, d->major, d->minor, strerror(errno));
1360 return 0;
1361 }
1362 if (doclose) {
1363 close(d->fd);
1364 d->fd = -1;
1365 }
1366 }
1367
1368 return 1;
1369}
1370
1371static int write_init_super_imsm(struct supertype *st)
1372{
8273f55e
DW
1373 if (st->update_tail) {
1374 /* queue the recently created array as a metadata update */
1375 size_t len;
1376 struct imsm_update_create_array *u;
1377 struct intel_super *super = st->sb;
1378 struct imsm_super *mpb = super->mpb;
1379 struct imsm_dev *dev;
1380 struct imsm_map *map;
1381 struct dl *d;
1382
1383 if (super->current_vol < 0 ||
1384 !(dev = get_imsm_dev(mpb, super->current_vol))) {
1385 fprintf(stderr, "%s: could not determine sub-array\n",
1386 __func__);
1387 return 1;
1388 }
1389
1390
1391 map = &dev->vol.map[0];
1392 len = sizeof(*u) + sizeof(__u32) * (map->num_members - 1);
1393 u = malloc(len);
1394 if (!u) {
1395 fprintf(stderr, "%s: failed to allocate update buffer\n",
1396 __func__);
1397 return 1;
1398 }
1399
1400 u->type = update_create_array;
1401 u->dev_idx = super->current_vol;
1402 memcpy(&u->dev, dev, sizeof(*dev));
1403 memcpy(u->dev.vol.map[0].disk_ord_tbl, map->disk_ord_tbl,
1404 sizeof(__u32) * map->num_members);
1405 append_metadata_update(st, u, len);
1406
1407 for (d = super->disks; d ; d = d->next) {
1408 close(d->fd);
1409 d->fd = -1;
1410 }
1411
1412 return 0;
1413 } else
1414 return write_super_imsm(st->sb, 1);
cdddbdbc
DW
1415}
1416
1417static int store_zero_imsm(struct supertype *st, int fd)
1418{
551c80c1 1419 unsigned long long dsize;
6416d527 1420 void *buf;
551c80c1
DW
1421
1422 get_dev_size(fd, NULL, &dsize);
1423
1424 /* first block is stored on second to last sector of the disk */
1425 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0)
1426 return 1;
1427
ad97895e
DW
1428 if (posix_memalign(&buf, 512, 512) != 0)
1429 return 1;
1430
eb7ea463
DW
1431 memset(buf, 0, 512);
1432 if (write(fd, buf, 512) != 512)
551c80c1 1433 return 1;
cdddbdbc
DW
1434 return 0;
1435}
1436
cdddbdbc
DW
1437static int validate_geometry_imsm_container(struct supertype *st, int level,
1438 int layout, int raiddisks, int chunk,
c2c087e6 1439 unsigned long long size, char *dev,
2c514b71
NB
1440 unsigned long long *freesize,
1441 int verbose)
cdddbdbc 1442{
c2c087e6
DW
1443 int fd;
1444 unsigned long long ldsize;
cdddbdbc 1445
c2c087e6
DW
1446 if (level != LEVEL_CONTAINER)
1447 return 0;
1448 if (!dev)
1449 return 1;
1450
1451 fd = open(dev, O_RDONLY|O_EXCL, 0);
1452 if (fd < 0) {
2c514b71
NB
1453 if (verbose)
1454 fprintf(stderr, Name ": imsm: Cannot open %s: %s\n",
1455 dev, strerror(errno));
c2c087e6
DW
1456 return 0;
1457 }
1458 if (!get_dev_size(fd, dev, &ldsize)) {
1459 close(fd);
1460 return 0;
1461 }
1462 close(fd);
1463
1464 *freesize = avail_size_imsm(st, ldsize >> 9);
1465
1466 return 1;
cdddbdbc
DW
1467}
1468
c2c087e6
DW
1469/* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
1470 * FIX ME add ahci details
1471 */
8b353278
DW
1472static int validate_geometry_imsm_volume(struct supertype *st, int level,
1473 int layout, int raiddisks, int chunk,
c2c087e6 1474 unsigned long long size, char *dev,
2c514b71
NB
1475 unsigned long long *freesize,
1476 int verbose)
cdddbdbc 1477{
c2c087e6
DW
1478 struct stat stb;
1479 struct intel_super *super = st->sb;
1480 struct dl *dl;
1481 unsigned long long pos = 0;
1482 unsigned long long maxsize;
1483 struct extent *e;
1484 int i;
cdddbdbc 1485
c2c087e6
DW
1486 if (level == LEVEL_CONTAINER)
1487 return 0;
1488
1489 if (level == 1 && raiddisks > 2) {
2c514b71
NB
1490 if (verbose)
1491 fprintf(stderr, Name ": imsm does not support more "
1492 "than 2 in a raid1 configuration\n");
c2c087e6
DW
1493 return 0;
1494 }
1495
1496 /* We must have the container info already read in. */
1497 if (!super)
1498 return 0;
1499
1500 if (!dev) {
1501 /* General test: make sure there is space for
2da8544a
DW
1502 * 'raiddisks' device extents of size 'size' at a given
1503 * offset
c2c087e6
DW
1504 */
1505 unsigned long long minsize = size*2 /* convert to blocks */;
2da8544a 1506 unsigned long long start_offset = ~0ULL;
c2c087e6
DW
1507 int dcnt = 0;
1508 if (minsize == 0)
1509 minsize = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
1510 for (dl = super->disks; dl ; dl = dl->next) {
1511 int found = 0;
1512
bf5a934a 1513 pos = 0;
c2c087e6
DW
1514 i = 0;
1515 e = get_extents(super, dl);
1516 if (!e) continue;
1517 do {
1518 unsigned long long esize;
1519 esize = e[i].start - pos;
1520 if (esize >= minsize)
1521 found = 1;
2da8544a
DW
1522 if (found && start_offset == ~0ULL) {
1523 start_offset = pos;
1524 break;
1525 } else if (found && pos != start_offset) {
1526 found = 0;
1527 break;
1528 }
c2c087e6
DW
1529 pos = e[i].start + e[i].size;
1530 i++;
1531 } while (e[i-1].size);
1532 if (found)
1533 dcnt++;
1534 free(e);
1535 }
1536 if (dcnt < raiddisks) {
2c514b71
NB
1537 if (verbose)
1538 fprintf(stderr, Name ": imsm: Not enough "
1539 "devices with space for this array "
1540 "(%d < %d)\n",
1541 dcnt, raiddisks);
c2c087e6
DW
1542 return 0;
1543 }
1544 return 1;
1545 }
1546 /* This device must be a member of the set */
1547 if (stat(dev, &stb) < 0)
1548 return 0;
1549 if ((S_IFMT & stb.st_mode) != S_IFBLK)
1550 return 0;
1551 for (dl = super->disks ; dl ; dl = dl->next) {
1552 if (dl->major == major(stb.st_rdev) &&
1553 dl->minor == minor(stb.st_rdev))
1554 break;
1555 }
1556 if (!dl) {
2c514b71
NB
1557 if (verbose)
1558 fprintf(stderr, Name ": %s is not in the "
1559 "same imsm set\n", dev);
c2c087e6
DW
1560 return 0;
1561 }
1562 e = get_extents(super, dl);
1563 maxsize = 0;
1564 i = 0;
1565 if (e) do {
1566 unsigned long long esize;
1567 esize = e[i].start - pos;
1568 if (esize >= maxsize)
1569 maxsize = esize;
1570 pos = e[i].start + e[i].size;
1571 i++;
1572 } while (e[i-1].size);
1573 *freesize = maxsize;
1574
1575 return 1;
cdddbdbc
DW
1576}
1577
bf5a934a
DW
1578static int validate_geometry_imsm(struct supertype *st, int level, int layout,
1579 int raiddisks, int chunk, unsigned long long size,
1580 char *dev, unsigned long long *freesize,
1581 int verbose)
1582{
1583 int fd, cfd;
1584 struct mdinfo *sra;
1585
1586 /* if given unused devices create a container
1587 * if given given devices in a container create a member volume
1588 */
1589 if (level == LEVEL_CONTAINER) {
1590 /* Must be a fresh device to add to a container */
1591 return validate_geometry_imsm_container(st, level, layout,
1592 raiddisks, chunk, size,
1593 dev, freesize,
1594 verbose);
1595 }
1596
1597 if (st->sb) {
1598 /* creating in a given container */
1599 return validate_geometry_imsm_volume(st, level, layout,
1600 raiddisks, chunk, size,
1601 dev, freesize, verbose);
1602 }
1603
1604 /* limit creation to the following levels */
1605 if (!dev)
1606 switch (level) {
1607 case 0:
1608 case 1:
1609 case 10:
1610 case 5:
1611 break;
1612 default:
1613 return 1;
1614 }
1615
1616 /* This device needs to be a device in an 'imsm' container */
1617 fd = open(dev, O_RDONLY|O_EXCL, 0);
1618 if (fd >= 0) {
1619 if (verbose)
1620 fprintf(stderr,
1621 Name ": Cannot create this array on device %s\n",
1622 dev);
1623 close(fd);
1624 return 0;
1625 }
1626 if (errno != EBUSY || (fd = open(dev, O_RDONLY, 0)) < 0) {
1627 if (verbose)
1628 fprintf(stderr, Name ": Cannot open %s: %s\n",
1629 dev, strerror(errno));
1630 return 0;
1631 }
1632 /* Well, it is in use by someone, maybe an 'imsm' container. */
1633 cfd = open_container(fd);
1634 if (cfd < 0) {
1635 close(fd);
1636 if (verbose)
1637 fprintf(stderr, Name ": Cannot use %s: It is busy\n",
1638 dev);
1639 return 0;
1640 }
1641 sra = sysfs_read(cfd, 0, GET_VERSION);
1642 close(fd);
1643 if (sra && sra->array.major_version == -1 &&
1644 strcmp(sra->text_version, "imsm") == 0) {
1645 /* This is a member of a imsm container. Load the container
1646 * and try to create a volume
1647 */
1648 struct intel_super *super;
1649
1650 if (load_super_imsm_all(st, cfd, (void **) &super, NULL, 1) == 0) {
1651 st->sb = super;
1652 st->container_dev = fd2devnum(cfd);
1653 close(cfd);
1654 return validate_geometry_imsm_volume(st, level, layout,
1655 raiddisks, chunk,
1656 size, dev,
1657 freesize, verbose);
1658 }
1659 close(cfd);
1660 } else /* may belong to another container */
1661 return 0;
1662
1663 return 1;
1664}
1665
cdddbdbc
DW
1666static struct mdinfo *container_content_imsm(struct supertype *st)
1667{
4f5bc454
DW
1668 /* Given a container loaded by load_super_imsm_all,
1669 * extract information about all the arrays into
1670 * an mdinfo tree.
1671 *
1672 * For each imsm_dev create an mdinfo, fill it in,
1673 * then look for matching devices in super->disks
1674 * and create appropriate device mdinfo.
1675 */
1676 struct intel_super *super = st->sb;
1677 struct imsm_super *mpb = super->mpb;
1678 struct mdinfo *rest = NULL;
1679 int i;
cdddbdbc 1680
4f5bc454
DW
1681 for (i = 0; i < mpb->num_raid_devs; i++) {
1682 struct imsm_dev *dev = get_imsm_dev(mpb, i);
1683 struct imsm_vol *vol = &dev->vol;
1684 struct imsm_map *map = vol->map;
1685 struct mdinfo *this;
4f5bc454
DW
1686 int slot;
1687
1688 this = malloc(sizeof(*this));
1689 memset(this, 0, sizeof(*this));
1690 this->next = rest;
1691 rest = this;
1692
4f5bc454
DW
1693 this->array.level = get_imsm_raid_level(map);
1694 this->array.raid_disks = map->num_members;
c2c087e6 1695 this->array.layout = imsm_level_to_layout(this->array.level);
4f5bc454
DW
1696 this->array.md_minor = -1;
1697 this->array.ctime = 0;
1698 this->array.utime = 0;
1699 this->array.chunk_size = __le16_to_cpu(map->blocks_per_strip) << 9;
1700 this->array.state = !vol->dirty;
1701 this->container_member = i;
0fd5c350
DW
1702 if (map->map_state == IMSM_T_STATE_UNINITIALIZED || dev->vol.dirty)
1703 this->resync_start = 0;
1704 else
1705 this->resync_start = ~0ULL;
1706
4f5bc454
DW
1707 strncpy(this->name, (char *) dev->volume, MAX_RAID_SERIAL_LEN);
1708 this->name[MAX_RAID_SERIAL_LEN] = 0;
1709
159c3a1a
NB
1710 sprintf(this->text_version, "/%s/%d",
1711 devnum2devname(st->container_dev),
1712 this->container_member);
1713
4f5bc454
DW
1714 memset(this->uuid, 0, sizeof(this->uuid));
1715
f54e6321 1716 this->component_size = __le32_to_cpu(map->blocks_per_member);
4f5bc454
DW
1717
1718 for (slot = 0 ; slot < map->num_members; slot++) {
1719 struct imsm_disk *disk;
1720 struct mdinfo *info_d;
1721 struct dl *d;
1722 int idx;
1723 __u32 s;
1724
1725 idx = __le32_to_cpu(map->disk_ord_tbl[slot] & ~(0xff << 24));
1726 for (d = super->disks; d ; d = d->next)
1727 if (d->index == idx)
1728 break;
1729
1730 if (d == NULL)
1731 break; /* shouldn't this be continue ?? */
1732
1733 info_d = malloc(sizeof(*info_d));
1734 if (!info_d)
1735 break; /* ditto ?? */
1736 memset(info_d, 0, sizeof(*info_d));
1737 info_d->next = this->devs;
1738 this->devs = info_d;
1739
1740 disk = get_imsm_disk(mpb, idx);
1741 s = __le32_to_cpu(disk->status);
1742
1743 info_d->disk.number = d->index;
1744 info_d->disk.major = d->major;
1745 info_d->disk.minor = d->minor;
1746 info_d->disk.raid_disk = slot;
1747 info_d->disk.state = s & CONFIGURED_DISK ? (1 << MD_DISK_ACTIVE) : 0;
1748 info_d->disk.state |= s & FAILED_DISK ? (1 << MD_DISK_FAULTY) : 0;
1749 info_d->disk.state |= s & USABLE_DISK ? (1 << MD_DISK_SYNC) : 0;
1750
1751 this->array.working_disks++;
1752
1753 info_d->events = __le32_to_cpu(mpb->generation_num);
1754 info_d->data_offset = __le32_to_cpu(map->pba_of_lba0);
1755 info_d->component_size = __le32_to_cpu(map->blocks_per_member);
1756 if (d->devname)
1757 strcpy(info_d->name, d->devname);
1758 }
1759 }
1760
1761 return rest;
cdddbdbc
DW
1762}
1763
845dea95 1764
cba0191b
NB
1765static int imsm_open_new(struct supertype *c, struct active_array *a,
1766 char *inst)
845dea95 1767{
0372d5a2
DW
1768 struct intel_super *super = c->sb;
1769 struct imsm_super *mpb = super->mpb;
1770
1771 if (atoi(inst) + 1 > mpb->num_raid_devs) {
1772 fprintf(stderr, "%s: subarry index %d, out of range\n",
1773 __func__, atoi(inst));
1774 return -ENODEV;
1775 }
1776
4e6e574a 1777 dprintf("imsm: open_new %s\n", inst);
cba0191b 1778 a->info.container_member = atoi(inst);
845dea95
NB
1779 return 0;
1780}
1781
c2a1e7da
DW
1782static __u8 imsm_check_degraded(struct imsm_super *mpb, int n, int failed)
1783{
1784 struct imsm_dev *dev = get_imsm_dev(mpb, n);
1785 struct imsm_map *map = dev->vol.map;
1786
1787 if (!failed)
1788 return map->map_state;
1789
1790 switch (get_imsm_raid_level(map)) {
1791 case 0:
1792 return IMSM_T_STATE_FAILED;
1793 break;
1794 case 1:
1795 if (failed < map->num_members)
1796 return IMSM_T_STATE_DEGRADED;
1797 else
1798 return IMSM_T_STATE_FAILED;
1799 break;
1800 case 10:
1801 {
1802 /**
1803 * check to see if any mirrors have failed,
1804 * otherwise we are degraded
1805 */
1806 int device_per_mirror = 2; /* FIXME is this always the case?
1807 * and are they always adjacent?
1808 */
1809 int failed = 0;
1810 int i;
1811
1812 for (i = 0; i < map->num_members; i++) {
1813 int idx = get_imsm_disk_idx(map, i);
1814 struct imsm_disk *disk = get_imsm_disk(mpb, idx);
1815
1816 if (__le32_to_cpu(disk->status) & FAILED_DISK)
1817 failed++;
1818
1819 if (failed >= device_per_mirror)
1820 return IMSM_T_STATE_FAILED;
1821
1822 /* reset 'failed' for next mirror set */
1823 if (!((i + 1) % device_per_mirror))
1824 failed = 0;
1825 }
1826
1827 return IMSM_T_STATE_DEGRADED;
1828 }
1829 case 5:
1830 if (failed < 2)
1831 return IMSM_T_STATE_DEGRADED;
1832 else
1833 return IMSM_T_STATE_FAILED;
1834 break;
1835 default:
1836 break;
1837 }
1838
1839 return map->map_state;
1840}
1841
1842static int imsm_count_failed(struct imsm_super *mpb, struct imsm_map *map)
1843{
1844 int i;
1845 int failed = 0;
1846 struct imsm_disk *disk;
1847
1848 for (i = 0; i < map->num_members; i++) {
1849 int idx = get_imsm_disk_idx(map, i);
1850
1851 disk = get_imsm_disk(mpb, idx);
1852 if (__le32_to_cpu(disk->status) & FAILED_DISK)
1853 failed++;
1854 }
1855
1856 return failed;
845dea95
NB
1857}
1858
a862209d
DW
1859static void imsm_set_array_state(struct active_array *a, int consistent)
1860{
1861 int inst = a->info.container_member;
1862 struct intel_super *super = a->container->sb;
1863 struct imsm_dev *dev = get_imsm_dev(super->mpb, inst);
1864 struct imsm_map *map = &dev->vol.map[0];
1865 int dirty = !consistent;
1866 int failed;
1867 __u8 map_state;
1868
1869 if (a->resync_start == ~0ULL) {
1870 failed = imsm_count_failed(super->mpb, map);
1871 map_state = imsm_check_degraded(super->mpb, inst, failed);
1872 if (!failed)
1873 map_state = IMSM_T_STATE_NORMAL;
1874 if (map->map_state != map_state) {
4e6e574a 1875 dprintf("imsm: map_state %d: %d\n",
a862209d
DW
1876 inst, map_state);
1877 map->map_state = map_state;
1878 super->updates_pending++;
1879 }
1880 }
1881
1882 if (dev->vol.dirty != dirty) {
4e6e574a 1883 dprintf("imsm: mark '%s' (%llu)\n",
a862209d
DW
1884 dirty?"dirty":"clean", a->resync_start);
1885
1886 dev->vol.dirty = dirty;
1887 super->updates_pending++;
1888 }
1889}
1890
8d45d196 1891static void imsm_set_disk(struct active_array *a, int n, int state)
845dea95 1892{
8d45d196
DW
1893 int inst = a->info.container_member;
1894 struct intel_super *super = a->container->sb;
1895 struct imsm_dev *dev = get_imsm_dev(super->mpb, inst);
1896 struct imsm_map *map = dev->vol.map;
1897 struct imsm_disk *disk;
1898 __u32 status;
1899 int failed = 0;
1900 int new_failure = 0;
1901
1902 if (n > map->num_members)
1903 fprintf(stderr, "imsm: set_disk %d out of range 0..%d\n",
1904 n, map->num_members - 1);
1905
1906 if (n < 0)
1907 return;
1908
4e6e574a 1909 dprintf("imsm: set_disk %d:%x\n", n, state);
8d45d196
DW
1910
1911 disk = get_imsm_disk(super->mpb, get_imsm_disk_idx(map, n));
1912
5802a811 1913 /* check for new failures */
8d45d196
DW
1914 status = __le32_to_cpu(disk->status);
1915 if ((state & DS_FAULTY) && !(status & FAILED_DISK)) {
1916 status |= FAILED_DISK;
1917 disk->status = __cpu_to_le32(status);
1918 new_failure = 1;
5802a811 1919 super->updates_pending++;
8d45d196
DW
1920 }
1921
5802a811 1922 /* the number of failures have changed, count up 'failed' to determine
8d45d196
DW
1923 * degraded / failed status
1924 */
1925 if (new_failure && map->map_state != IMSM_T_STATE_FAILED)
1926 failed = imsm_count_failed(super->mpb, map);
1927
5802a811 1928 /* determine map_state based on failed or in_sync count */
8d45d196
DW
1929 if (failed)
1930 map->map_state = imsm_check_degraded(super->mpb, inst, failed);
5802a811
DW
1931 else if (map->map_state == IMSM_T_STATE_DEGRADED) {
1932 struct mdinfo *d;
1933 int working = 0;
8d45d196 1934
5802a811
DW
1935 for (d = a->info.devs ; d ; d = d->next)
1936 if (d->curr_state & DS_INSYNC)
1937 working++;
1938
1939 if (working == a->info.array.raid_disks) {
1940 map->map_state = IMSM_T_STATE_NORMAL;
1941 super->updates_pending++;
1942 }
1943 }
845dea95
NB
1944}
1945
c2a1e7da
DW
1946static int store_imsm_mpb(int fd, struct intel_super *super)
1947{
1948 struct imsm_super *mpb = super->mpb;
1949 __u32 mpb_size = __le32_to_cpu(mpb->mpb_size);
1950 unsigned long long dsize;
1951 unsigned long long sectors;
1952
1953 get_dev_size(fd, NULL, &dsize);
1954
272f648f
DW
1955 if (mpb_size > 512) {
1956 /* -1 to account for anchor */
1957 sectors = mpb_sectors(mpb) - 1;
c2a1e7da 1958
272f648f
DW
1959 /* write the extended mpb to the sectors preceeding the anchor */
1960 if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0)
1961 return 1;
c2a1e7da 1962
99e29264 1963 if (write(fd, super->buf + 512, 512 * sectors) != 512 * sectors)
272f648f
DW
1964 return 1;
1965 }
c2a1e7da 1966
272f648f
DW
1967 /* first block is stored on second to last sector of the disk */
1968 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0)
c2a1e7da
DW
1969 return 1;
1970
272f648f 1971 if (write(fd, super->buf, 512) != 512)
c2a1e7da
DW
1972 return 1;
1973
c2a1e7da
DW
1974 return 0;
1975}
1976
2e735d19 1977static void imsm_sync_metadata(struct supertype *container)
845dea95 1978{
2e735d19 1979 struct intel_super *super = container->sb;
c2a1e7da
DW
1980
1981 if (!super->updates_pending)
1982 return;
1983
c2c087e6 1984 write_super_imsm(super, 0);
c2a1e7da
DW
1985
1986 super->updates_pending = 0;
845dea95
NB
1987}
1988
88758e9d
DW
1989static struct mdinfo *imsm_activate_spare(struct active_array *a,
1990 struct metadata_update **updates)
1991{
1992 /**
1993 * Take a device that is marked spare in the metadata and use it to
1994 * replace a failed/vacant slot in an array. There may be a case where
1995 * a device is failed in one array but active in a second.
1996 * imsm_process_update catches this case and does not clear the SPARE_DISK
1997 * flag, allowing the second array to start using the device on failure.
1998 * SPARE_DISK is cleared when all arrays are using a device.
1999 *
2000 * FIXME: is this a valid use of SPARE_DISK?
2001 */
2002
2003 struct intel_super *super = a->container->sb;
2004 struct imsm_super *mpb = super->mpb;
2005 int inst = a->info.container_member;
2006 struct imsm_dev *dev = get_imsm_dev(mpb, inst);
2007 struct imsm_map *map = dev->vol.map;
2008 int failed = a->info.array.raid_disks;
2009 struct mdinfo *rv = NULL;
2010 struct mdinfo *d;
2011 struct mdinfo *di;
2012 struct metadata_update *mu;
2013 struct dl *dl;
2014 struct imsm_update_activate_spare *u;
2015 int num_spares = 0;
2016 int i;
2017
2018 for (d = a->info.devs ; d ; d = d->next) {
2019 if ((d->curr_state & DS_FAULTY) &&
2020 d->state_fd >= 0)
2021 /* wait for Removal to happen */
2022 return NULL;
2023 if (d->state_fd >= 0)
2024 failed--;
2025 }
2026
2027 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
2028 inst, failed, a->info.array.raid_disks, a->info.array.level);
2029 if (imsm_check_degraded(mpb, inst, failed) != IMSM_T_STATE_DEGRADED)
2030 return NULL;
2031
2032 /* For each slot, if it is not working, find a spare */
2033 dl = super->disks;
2034 for (i = 0; i < a->info.array.raid_disks; i++) {
2035 for (d = a->info.devs ; d ; d = d->next)
2036 if (d->disk.raid_disk == i)
2037 break;
2038 dprintf("found %d: %p %x\n", i, d, d?d->curr_state:0);
2039 if (d && (d->state_fd >= 0))
2040 continue;
2041
2042 /* OK, this device needs recovery. Find a spare */
2043 for ( ; dl ; dl = dl->next) {
2044 unsigned long long esize;
2045 unsigned long long pos;
2046 struct mdinfo *d2;
2047 struct extent *ex;
2048 struct imsm_disk *disk;
2049 int j;
2050 int found;
5257ce36 2051 __u32 array_start;
88758e9d
DW
2052
2053 /* If in this array, skip */
2054 for (d2 = a->info.devs ; d2 ; d2 = d2->next)
2055 if (d2->disk.major == dl->major &&
2056 d2->disk.minor == dl->minor) {
2057 dprintf("%x:%x already in array\n", dl->major, dl->minor);
2058 break;
2059 }
2060 if (d2)
2061 continue;
2062
2063 /* is this unused device marked as a spare? */
2064 disk = get_imsm_disk(mpb, dl->index);
2065 if (!(__le32_to_cpu(disk->status) & SPARE_DISK))
2066 continue;
2067
2068 /* We are allowed to use this device - is there space?
2069 * We need a->info.component_size sectors */
2070 ex = get_extents(super, dl);
2071 if (!ex) {
2072 dprintf("cannot get extents\n");
2073 continue;
2074 }
2075 found = 0;
2076 j = 0;
2077 pos = 0;
5257ce36 2078 array_start = __le32_to_cpu(map->pba_of_lba0);
88758e9d
DW
2079
2080 do {
5257ce36
DW
2081 /* check that we can start at pba_of_lba0 with
2082 * a->info.component_size of space
2083 */
88758e9d 2084 esize = ex[j].start - pos;
5257ce36
DW
2085 if (array_start >= pos &&
2086 array_start + a->info.component_size < ex[j].start) {
2087 found = 1;
88758e9d
DW
2088 break;
2089 }
5257ce36
DW
2090 pos = ex[j].start + ex[j].size;
2091 j++;
2092
2093 } while (ex[j-1].size);
88758e9d
DW
2094
2095 free(ex);
2096 if (!found) {
2097 dprintf("%x:%x does not have %llu at %d\n",
2098 dl->major, dl->minor,
2099 a->info.component_size,
2100 __le32_to_cpu(map->pba_of_lba0));
2101 /* No room */
2102 continue;
2103 }
2104
5257ce36 2105 /* found a usable disk with enough space */
88758e9d
DW
2106 di = malloc(sizeof(*di));
2107 memset(di, 0, sizeof(*di));
2108 di->disk.number = dl->index;
2109 di->disk.raid_disk = i;
2110 di->disk.major = dl->major;
2111 di->disk.minor = dl->minor;
2112 di->disk.state = 0;
5257ce36 2113 di->data_offset = array_start;
88758e9d
DW
2114 di->component_size = a->info.component_size;
2115 di->container_member = inst;
2116 di->next = rv;
2117 rv = di;
2118 num_spares++;
2119 dprintf("%x:%x to be %d at %llu\n", dl->major, dl->minor,
2120 i, pos);
2121
2122 break;
2123 }
2124 }
2125
2126 if (!rv)
2127 /* No spares found */
2128 return rv;
2129 /* Now 'rv' has a list of devices to return.
2130 * Create a metadata_update record to update the
2131 * disk_ord_tbl for the array
2132 */
2133 mu = malloc(sizeof(*mu));
2134 mu->buf = malloc(sizeof(struct imsm_update_activate_spare) * num_spares);
2135 mu->space = NULL;
2136 mu->len = sizeof(struct imsm_update_activate_spare) * num_spares;
2137 mu->next = *updates;
2138 u = (struct imsm_update_activate_spare *) mu->buf;
2139
2140 for (di = rv ; di ; di = di->next) {
2141 u->type = update_activate_spare;
2142 u->disk_idx = di->disk.number;
2143 u->slot = di->disk.raid_disk;
2144 u->array = inst;
2145 u->next = u + 1;
2146 u++;
2147 }
2148 (u-1)->next = NULL;
2149 *updates = mu;
2150
2151 return rv;
2152}
2153
e8319a19
DW
2154static int weight(unsigned int field)
2155{
2156 int weight;
2157
2158 for (weight = 0; field; weight++)
2159 field &= field - 1;
2160
2161 return weight;
2162}
2163
8273f55e
DW
2164static int disks_overlap(struct imsm_map *m1, struct imsm_map *m2)
2165{
2166 int i;
2167 int j;
2168 int idx;
2169
2170 for (i = 0; i < m1->num_members; i++) {
2171 idx = get_imsm_disk_idx(m1, i);
2172 for (j = 0; j < m2->num_members; j++)
2173 if (idx == get_imsm_disk_idx(m2, j))
2174 return 1;
2175 }
2176
2177 return 0;
2178}
2179
e8319a19
DW
2180static void imsm_process_update(struct supertype *st,
2181 struct metadata_update *update)
2182{
2183 /**
2184 * crack open the metadata_update envelope to find the update record
2185 * update can be one of:
2186 * update_activate_spare - a spare device has replaced a failed
2187 * device in an array, update the disk_ord_tbl. If this disk is
2188 * present in all member arrays then also clear the SPARE_DISK
2189 * flag
2190 */
2191 struct intel_super *super = st->sb;
2192 struct imsm_super *mpb = super->mpb;
2193 enum imsm_update_type type = *(enum imsm_update_type *) update->buf;
2194
2195 switch (type) {
2196 case update_activate_spare: {
2197 struct imsm_update_activate_spare *u = (void *) update->buf;
2198 struct imsm_dev *dev = get_imsm_dev(mpb, u->array);
2199 struct imsm_map *map = &dev->vol.map[0];
2200 struct active_array *a;
2201 struct imsm_disk *disk;
2202 __u32 status;
2203 struct dl *dl;
2204 struct mdinfo *d;
2205 unsigned int members;
2206 unsigned int found;
2207 int victim;
2208 int i;
2209
2210 for (dl = super->disks; dl; dl = dl->next)
2211 if (dl->index == u->disk_idx)
2212 break;
2213
2214 if (!dl) {
2215 fprintf(stderr, "error: imsm_activate_spare passed "
2216 "an unknown disk_idx: %d\n", u->disk_idx);
2217 return;
2218 }
2219
2220 super->updates_pending++;
2221
2222 victim = get_imsm_disk_idx(map, u->slot);
2223 map->disk_ord_tbl[u->slot] = __cpu_to_le32(u->disk_idx);
2224 disk = get_imsm_disk(mpb, u->disk_idx);
2225 status = __le32_to_cpu(disk->status);
2226 status |= CONFIGURED_DISK;
2227 disk->status = __cpu_to_le32(status);
2228
2229 /* map unique/live arrays using the spare */
2230 members = 0;
2231 found = 0;
2232 for (a = st->arrays; a; a = a->next) {
2233 int inst = a->info.container_member;
2234
2235 dev = get_imsm_dev(mpb, inst);
2236 map = &dev->vol.map[0];
2237 if (map->raid_level > 0)
2238 members |= 1 << inst;
2239 for (d = a->info.devs; d; d = d->next)
2240 if (d->disk.major == dl->major &&
2241 d->disk.minor == dl->minor)
2242 found |= 1 << inst;
2243 }
2244
2245 /* until all arrays that can absorb this disk have absorbed
2246 * this disk it can still be considered a spare
2247 */
2248 if (weight(found) >= weight(members)) {
2249 status = __le32_to_cpu(disk->status);
2250 status &= ~SPARE_DISK;
2251 disk->status = __cpu_to_le32(status);
2252 }
2253
2254 /* count arrays using the victim in the metadata */
2255 found = 0;
2256 for (a = st->arrays; a ; a = a->next) {
2257 dev = get_imsm_dev(mpb, a->info.container_member);
2258 map = &dev->vol.map[0];
2259 for (i = 0; i < map->num_members; i++)
2260 if (victim == get_imsm_disk_idx(map, i))
2261 found++;
2262 }
2263
2264 /* clear some flags if the victim is no longer being
2265 * utilized anywhere
2266 */
2267 disk = get_imsm_disk(mpb, victim);
2268 if (!found) {
2269 status = __le32_to_cpu(disk->status);
2270 status &= ~(CONFIGURED_DISK | USABLE_DISK);
2271 disk->status = __cpu_to_le32(status);
2272 }
8273f55e
DW
2273 break;
2274 }
2275 case update_create_array: {
2276 /* someone wants to create a new array, we need to be aware of
2277 * a few races/collisions:
2278 * 1/ 'Create' called by two separate instances of mdadm
2279 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
2280 * devices that have since been assimilated via
2281 * activate_spare.
2282 * In the event this update can not be carried out mdadm will
2283 * (FIX ME) notice that its update did not take hold.
2284 */
2285 struct imsm_update_create_array *u = (void *) update->buf;
2286 struct imsm_dev *dev;
2287 struct imsm_map *map, *new_map;
2288 unsigned long long start, end;
2289 unsigned long long new_start, new_end;
2290 int i;
2291 int overlap = 0;
2292
2293 /* handle racing creates: first come first serve */
2294 if (u->dev_idx < mpb->num_raid_devs) {
2295 dprintf("%s: subarray %d already defined\n",
2296 __func__, u->dev_idx);
2297 return;
2298 }
2299
2300 /* check update is next in sequence */
2301 if (u->dev_idx != mpb->num_raid_devs) {
2302 dprintf("%s: can not create arrays out of sequence\n",
2303 __func__);
2304 return;
2305 }
2306
2307 new_map = &u->dev.vol.map[0];
2308 new_start = __le32_to_cpu(new_map->pba_of_lba0);
2309 new_end = new_start + __le32_to_cpu(new_map->blocks_per_member);
2310
2311 /* handle activate_spare versus create race:
2312 * check to make sure that overlapping arrays do not include
2313 * overalpping disks
2314 */
2315 for (i = 0; i < mpb->num_raid_devs; i++) {
2316 dev = get_imsm_dev(mpb, i);
2317 map = &dev->vol.map[0];
2318 start = __le32_to_cpu(map->pba_of_lba0);
2319 end = start + __le32_to_cpu(map->blocks_per_member);
2320 if ((new_start >= start && new_start <= end) ||
2321 (start >= new_start && start <= new_end))
2322 overlap = 1;
2323 if (overlap && disks_overlap(map, new_map)) {
2324 dprintf("%s: arrays overlap\n", __func__);
2325 return;
2326 }
2327 }
2328 /* check num_members sanity */
2329 if (new_map->num_members > mpb->num_disks) {
2330 dprintf("%s: num_disks out of range\n", __func__);
2331 return;
2332 }
2333
2334 super->updates_pending++;
2335 mpb->num_raid_devs++;
2336 dev = get_imsm_dev(mpb, u->dev_idx);
2337 memcpy(dev, &u->dev, sizeof(*dev));
2338 map = &dev->vol.map[0];
2339 memcpy(map->disk_ord_tbl, new_map->disk_ord_tbl,
2340 sizeof(__u32) * new_map->num_members);
2341
2342 /* fix up flags, if arrays overlap then the drives can not be
2343 * spares
2344 */
2345 for (i = 0; i < map->num_members; i++) {
2346 struct imsm_disk *disk;
2347 __u32 status;
2348
2349 disk = get_imsm_disk(mpb, get_imsm_disk_idx(map, i));
2350 status = __le32_to_cpu(disk->status);
2351 status |= CONFIGURED_DISK;
2352 if (overlap)
2353 status &= ~SPARE_DISK;
2354 disk->status = __cpu_to_le32(status);
2355 }
2356 break;
e8319a19
DW
2357 }
2358 }
2359}
88758e9d 2360
8273f55e
DW
2361static void imsm_prepare_update(struct supertype *st,
2362 struct metadata_update *update)
2363{
2364 /* Allocate space to hold a new mpb if necessary. We currently
2365 * allocate enough to hold 2 subarrays for the given number of disks.
2366 * This may not be sufficient iff reshaping.
2367 *
2368 * FIX ME handle the reshape case.
2369 *
2370 * The monitor will be able to safely change super->mpb by arranging
2371 * for it to be freed in check_update_queue(). I.e. the monitor thread
2372 * will start using the new pointer and the manager can continue to use
2373 * the old value until check_update_queue() runs.
2374 */
2375
2376 return;
2377}
2378
cdddbdbc
DW
2379struct superswitch super_imsm = {
2380#ifndef MDASSEMBLE
2381 .examine_super = examine_super_imsm,
2382 .brief_examine_super = brief_examine_super_imsm,
2383 .detail_super = detail_super_imsm,
2384 .brief_detail_super = brief_detail_super_imsm,
bf5a934a 2385 .write_init_super = write_init_super_imsm,
cdddbdbc
DW
2386#endif
2387 .match_home = match_home_imsm,
2388 .uuid_from_super= uuid_from_super_imsm,
2389 .getinfo_super = getinfo_super_imsm,
2390 .update_super = update_super_imsm,
2391
2392 .avail_size = avail_size_imsm,
2393
2394 .compare_super = compare_super_imsm,
2395
2396 .load_super = load_super_imsm,
bf5a934a
DW
2397 .init_super = init_super_imsm,
2398 .add_to_super = add_to_super_imsm,
cdddbdbc
DW
2399 .store_super = store_zero_imsm,
2400 .free_super = free_super_imsm,
2401 .match_metadata_desc = match_metadata_desc_imsm,
bf5a934a 2402 .container_content = container_content_imsm,
cdddbdbc
DW
2403
2404 .validate_geometry = validate_geometry_imsm,
cdddbdbc 2405 .external = 1,
845dea95
NB
2406
2407/* for mdmon */
2408 .open_new = imsm_open_new,
2409 .load_super = load_super_imsm,
ed9d66aa 2410 .set_array_state= imsm_set_array_state,
845dea95
NB
2411 .set_disk = imsm_set_disk,
2412 .sync_metadata = imsm_sync_metadata,
88758e9d 2413 .activate_spare = imsm_activate_spare,
e8319a19 2414 .process_update = imsm_process_update,
8273f55e 2415 .prepare_update = imsm_prepare_update,
cdddbdbc 2416};