imsm: fix store_imsm_mpb() alignment for O_DIRECT
[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
0063ecba
NB
127static struct superswitch super_imsm_volume;
128
cdddbdbc
DW
129/* internal representation of IMSM metadata */
130struct intel_super {
131 union {
132 struct imsm_super *mpb;
133 void *buf;
134 };
c2c087e6
DW
135 int updates_pending; /* count of pending updates for mdmon */
136 int creating_imsm; /* flag to indicate container creation */
137 int creating_dev; /* index of raid device undergoing creation */
cdddbdbc
DW
138 struct dl {
139 struct dl *next;
140 int index;
141 __u8 serial[MAX_RAID_SERIAL_LEN];
142 int major, minor;
143 char *devname;
144 int fd;
145 } *disks;
146};
147
c2c087e6
DW
148struct extent {
149 unsigned long long start, size;
150};
151
cdddbdbc
DW
152static struct supertype *match_metadata_desc_imsm(char *arg)
153{
154 struct supertype *st;
155
156 if (strcmp(arg, "imsm") != 0 &&
157 strcmp(arg, "default") != 0
158 )
159 return NULL;
160
161 st = malloc(sizeof(*st));
ef609477 162 memset(st, 0, sizeof(*st));
cdddbdbc
DW
163 st->ss = &super_imsm;
164 st->max_devs = IMSM_MAX_DEVICES;
165 st->minor_version = 0;
166 st->sb = NULL;
167 return st;
168}
169
8b353278 170static struct supertype *match_metadata_desc_imsm_volume(char *arg)
cdddbdbc
DW
171{
172 struct supertype *st;
173
8b353278 174 if (strcmp(arg, "imsm/volume") != 0 &&
cdddbdbc
DW
175 strcmp(arg, "raid") != 0 &&
176 strcmp(arg, "default") != 0
177 )
178 return NULL;
179
180 st = malloc(sizeof(*st));
ef609477 181 memset(st, 0, sizeof(*st));
8b353278 182 st->ss = &super_imsm_volume;
cdddbdbc
DW
183 st->max_devs = IMSM_MAX_DEVICES;
184 st->minor_version = 0;
185 st->sb = NULL;
186 return st;
187}
188
189static __u8 *get_imsm_version(struct imsm_super *mpb)
190{
191 return &mpb->sig[MPB_SIG_LEN];
192}
193
194static struct imsm_disk *get_imsm_disk(struct imsm_super *mpb, __u8 index)
195{
196 if (index > mpb->num_disks - 1)
197 return NULL;
198 return &mpb->disk[index];
199}
200
201static __u32 gen_imsm_checksum(struct imsm_super *mpb)
202{
203 __u32 end = mpb->mpb_size / sizeof(end);
204 __u32 *p = (__u32 *) mpb;
205 __u32 sum = 0;
206
207 while (end--)
208 sum += __le32_to_cpu(*p++);
209
210 return sum - __le32_to_cpu(mpb->check_sum);
211}
212
213static size_t sizeof_imsm_dev(struct imsm_dev *dev)
214{
215 size_t size = sizeof(*dev);
216
217 /* each map has disk_ord_tbl[num_members - 1] additional space */
218 size += sizeof(__u32) * (dev->vol.map[0].num_members - 1);
219
220 /* migrating means an additional map */
221 if (dev->vol.migr_state) {
222 size += sizeof(struct imsm_map);
223 size += sizeof(__u32) * (dev->vol.map[1].num_members - 1);
224 }
225
226 return size;
227}
228
229static struct imsm_dev *get_imsm_dev(struct imsm_super *mpb, __u8 index)
230{
231 int offset;
232 int i;
233 void *_mpb = mpb;
234
235 if (index > mpb->num_raid_devs - 1)
236 return NULL;
237
238 /* devices start after all disks */
239 offset = ((void *) &mpb->disk[mpb->num_disks]) - _mpb;
240
241 for (i = 0; i <= index; i++)
242 if (i == index)
243 return _mpb + offset;
244 else
245 offset += sizeof_imsm_dev(_mpb + offset);
246
247 return NULL;
248}
249
250static __u32 get_imsm_disk_idx(struct imsm_map *map, int slot)
251{
252 __u32 *ord_tbl = &map->disk_ord_tbl[slot];
253
254 /* top byte is 'special' */
255 return __le32_to_cpu(*ord_tbl & ~(0xff << 24));
256}
257
cdddbdbc
DW
258static int get_imsm_raid_level(struct imsm_map *map)
259{
260 if (map->raid_level == 1) {
261 if (map->num_members == 2)
262 return 1;
263 else
264 return 10;
265 }
266
267 return map->raid_level;
268}
269
c2c087e6
DW
270static int cmp_extent(const void *av, const void *bv)
271{
272 const struct extent *a = av;
273 const struct extent *b = bv;
274 if (a->start < b->start)
275 return -1;
276 if (a->start > b->start)
277 return 1;
278 return 0;
279}
280
281static struct extent *get_extents(struct intel_super *super, struct dl *dl)
282{
283 /* find a list of used extents on the given physical device */
284 struct imsm_super *mpb = super->mpb;
285 struct imsm_disk *disk;
286 struct extent *rv, *e;
287 int i, j;
288 int memberships = 0;
289
290 disk = get_imsm_disk(mpb, dl->index);
291 if (!disk)
292 return NULL;
293
294 for (i = 0; i < mpb->num_raid_devs; i++) {
295 struct imsm_dev *dev = get_imsm_dev(mpb, i);
296 struct imsm_map *map = dev->vol.map;
297
298 for (j = 0; j < map->num_members; j++) {
299 __u32 index = get_imsm_disk_idx(map, j);
300
301 if (index == dl->index)
302 memberships++;
303 }
304 }
305 rv = malloc(sizeof(struct extent) * (memberships + 1));
306 if (!rv)
307 return NULL;
308 e = rv;
309
310 for (i = 0; i < mpb->num_raid_devs; i++) {
311 struct imsm_dev *dev = get_imsm_dev(mpb, i);
312 struct imsm_map *map = dev->vol.map;
313
314 for (j = 0; j < map->num_members; j++) {
315 __u32 index = get_imsm_disk_idx(map, j);
316
317 if (index == dl->index) {
318 e->start = __le32_to_cpu(map->pba_of_lba0);
319 e->size = __le32_to_cpu(map->blocks_per_member);
320 e++;
321 }
322 }
323 }
324 qsort(rv, memberships, sizeof(*rv), cmp_extent);
325
326 e->start = __le32_to_cpu(disk->total_blocks) -
327 (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS);
328 e->size = 0;
329 return rv;
330}
331
4f5bc454 332#ifndef MDASSEMBLE
cdddbdbc
DW
333static void print_imsm_dev(struct imsm_dev *dev, int index)
334{
335 __u64 sz;
336 int slot;
337 struct imsm_map *map = dev->vol.map;
338
339 printf("\n");
340 printf("[%s]:\n", dev->volume);
341 printf(" RAID Level : %d\n", get_imsm_raid_level(map));
342 printf(" Members : %d\n", map->num_members);
343 for (slot = 0; slot < map->num_members; slot++)
344 if (index == get_imsm_disk_idx(map, slot))
345 break;
346 if (slot < map->num_members)
347 printf(" This Slot : %d\n", slot);
348 else
349 printf(" This Slot : ?\n");
350 sz = __le32_to_cpu(dev->size_high);
351 sz <<= 32;
352 sz += __le32_to_cpu(dev->size_low);
353 printf(" Array Size : %llu%s\n", (unsigned long long)sz,
354 human_size(sz * 512));
355 sz = __le32_to_cpu(map->blocks_per_member);
356 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz,
357 human_size(sz * 512));
358 printf(" Sector Offset : %u\n",
359 __le32_to_cpu(map->pba_of_lba0));
360 printf(" Num Stripes : %u\n",
361 __le32_to_cpu(map->num_data_stripes));
362 printf(" Chunk Size : %u KiB\n",
363 __le16_to_cpu(map->blocks_per_strip) / 2);
364 printf(" Reserved : %d\n", __le32_to_cpu(dev->reserved_blocks));
365 printf(" Migrate State : %s\n", dev->vol.migr_state ? "migrating" : "idle");
366 printf(" Dirty State : %s\n", dev->vol.dirty ? "dirty" : "clean");
367 printf(" Map State : %s\n", map_state_str[map->map_state]);
368}
369
370static void print_imsm_disk(struct imsm_super *mpb, int index)
371{
372 struct imsm_disk *disk = get_imsm_disk(mpb, index);
373 char str[MAX_RAID_SERIAL_LEN];
374 __u32 s;
375 __u64 sz;
376
377 printf("\n");
378 snprintf(str, MAX_RAID_SERIAL_LEN, "%s", disk->serial);
379 printf(" Disk%02d Serial : %s\n", index, str);
380 s = __le32_to_cpu(disk->status);
381 printf(" State :%s%s%s%s\n", s&SPARE_DISK ? " spare" : "",
382 s&CONFIGURED_DISK ? " active" : "",
383 s&FAILED_DISK ? " failed" : "",
384 s&USABLE_DISK ? " usable" : "");
385 printf(" Id : %08x\n", __le32_to_cpu(disk->scsi_id));
c2c087e6
DW
386 sz = __le32_to_cpu(disk->total_blocks) -
387 (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS * mpb->num_raid_devs);
cdddbdbc
DW
388 printf(" Usable Size : %llu%s\n", (unsigned long long)sz,
389 human_size(sz * 512));
390}
391
392static void examine_super_imsm(struct supertype *st, char *homehost)
393{
394 struct intel_super *super = st->sb;
395 struct imsm_super *mpb = super->mpb;
396 char str[MAX_SIGNATURE_LENGTH];
397 int i;
398 __u32 sum;
399
400 snprintf(str, MPB_SIG_LEN, "%s", mpb->sig);
401 printf(" Magic : %s\n", str);
402 snprintf(str, strlen(MPB_VERSION_RAID0), "%s", get_imsm_version(mpb));
403 printf(" Version : %s\n", get_imsm_version(mpb));
404 printf(" Family : %08x\n", __le32_to_cpu(mpb->family_num));
405 printf(" Generation : %08x\n", __le32_to_cpu(mpb->generation_num));
406 sum = __le32_to_cpu(mpb->check_sum);
407 printf(" Checksum : %08x %s\n", sum,
408 gen_imsm_checksum(mpb) == sum ? "correct" : "incorrect");
87eb16df 409 printf(" MPB Sectors : %d\n", mpb_sectors(mpb));
cdddbdbc
DW
410 printf(" Disks : %d\n", mpb->num_disks);
411 printf(" RAID Devices : %d\n", mpb->num_raid_devs);
412 print_imsm_disk(mpb, super->disks->index);
413 for (i = 0; i < mpb->num_raid_devs; i++)
414 print_imsm_dev(get_imsm_dev(mpb, i), super->disks->index);
415 for (i = 0; i < mpb->num_disks; i++) {
416 if (i == super->disks->index)
417 continue;
418 print_imsm_disk(mpb, i);
419 }
420}
421
422static void brief_examine_super_imsm(struct supertype *st)
423{
424 struct intel_super *super = st->sb;
425 struct imsm_super *mpb = super->mpb;
426
427 printf("ARRAY /dev/imsm family=%08x metadata=external:imsm\n",
428 __le32_to_cpu(mpb->family_num));
429}
430
431static void detail_super_imsm(struct supertype *st, char *homehost)
432{
433 printf("%s\n", __FUNCTION__);
434}
435
436static void brief_detail_super_imsm(struct supertype *st)
437{
438 printf("%s\n", __FUNCTION__);
439}
440#endif
441
442static int match_home_imsm(struct supertype *st, char *homehost)
443{
444 printf("%s\n", __FUNCTION__);
445
446 return 0;
447}
448
449static void uuid_from_super_imsm(struct supertype *st, int uuid[4])
450{
451 printf("%s\n", __FUNCTION__);
452}
453
0d481d37 454#if 0
4f5bc454
DW
455static void
456get_imsm_numerical_version(struct imsm_super *mpb, int *m, int *p)
cdddbdbc 457{
cdddbdbc
DW
458 __u8 *v = get_imsm_version(mpb);
459 __u8 *end = mpb->sig + MAX_SIGNATURE_LENGTH;
460 char major[] = { 0, 0, 0 };
461 char minor[] = { 0 ,0, 0 };
462 char patch[] = { 0, 0, 0 };
463 char *ver_parse[] = { major, minor, patch };
464 int i, j;
465
466 i = j = 0;
467 while (*v != '\0' && v < end) {
468 if (*v != '.' && j < 2)
469 ver_parse[i][j++] = *v;
470 else {
471 i++;
472 j = 0;
473 }
474 v++;
475 }
476
4f5bc454
DW
477 *m = strtol(minor, NULL, 0);
478 *p = strtol(patch, NULL, 0);
479}
0d481d37 480#endif
4f5bc454 481
c2c087e6
DW
482static int imsm_level_to_layout(int level)
483{
484 switch (level) {
485 case 0:
486 case 1:
487 return 0;
488 case 5:
489 case 6:
490 return ALGORITHM_LEFT_SYMMETRIC;
491 case 10:
492 return 0x102; //FIXME is this correct?
493 }
494 return -1;
495}
496
4f5bc454
DW
497static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info)
498{
499 struct intel_super *super = st->sb;
500 struct imsm_super *mpb = super->mpb;
501 struct imsm_disk *disk;
d2ca6449 502 int sect = mpb_sectors(mpb);
4f5bc454 503 __u32 s;
4f5bc454 504
cdddbdbc
DW
505 info->array.raid_disks = mpb->num_disks;
506 info->array.level = LEVEL_CONTAINER;
507 info->array.layout = 0;
508 info->array.md_minor = -1;
c2c087e6 509 info->array.ctime = 0; /* N/A for imsm */
cdddbdbc
DW
510 info->array.utime = 0;
511 info->array.chunk_size = 0;
512
513 info->disk.major = 0;
514 info->disk.minor = 0;
cdddbdbc 515 info->disk.raid_disk = -1;
c2c087e6
DW
516 info->reshape_active = 0;
517 strcpy(info->text_version, "imsm");
518 info->disk.number = -1;
519 info->disk.state = 0;
520
d2ca6449
NB
521 info->data_offset = __le32_to_cpu(get_imsm_disk(super->mpb,
522 super->disks->index)
523 ->total_blocks) - (2 + sect - 1);
524 info->component_size = sect;
525
4a04ec6c
DW
526 if (super->disks) {
527 info->disk.number = super->disks->index;
528 info->disk.raid_disk = super->disks->index;
529 disk = get_imsm_disk(mpb, super->disks->index);
530 s = __le32_to_cpu(disk->status);
531 info->disk.state = s & CONFIGURED_DISK ? (1 << MD_DISK_ACTIVE) : 0;
532 info->disk.state |= s & FAILED_DISK ? (1 << MD_DISK_FAULTY) : 0;
533 info->disk.state |= s & USABLE_DISK ? (1 << MD_DISK_SYNC) : 0;
cdddbdbc 534 }
cdddbdbc
DW
535}
536
8b353278 537static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info)
cdddbdbc 538{
c2c087e6
DW
539 struct intel_super *super = st->sb;
540 struct imsm_super *mpb = super->mpb;
541 struct imsm_dev *dev = get_imsm_dev(mpb, info->container_member);
542 struct imsm_map *map = &dev->vol.map[0];
543
c2c087e6
DW
544 info->array.raid_disks = map->num_members;
545 info->array.level = get_imsm_raid_level(map);
546 info->array.layout = imsm_level_to_layout(info->array.level);
547 info->array.md_minor = -1;
548 info->array.ctime = 0;
549 info->array.utime = 0;
550 info->array.chunk_size = __le16_to_cpu(map->blocks_per_strip * 512);
551
552 info->data_offset = __le32_to_cpu(map->pba_of_lba0);
553 info->component_size = __le32_to_cpu(map->blocks_per_member);
554
555 info->disk.major = 0;
556 info->disk.minor = 0;
159c3a1a
NB
557
558 sprintf(info->text_version, "/%s/%d",
559 devnum2devname(st->container_dev),
c2c087e6 560 info->container_member);
cdddbdbc
DW
561}
562
563static int update_super_imsm(struct supertype *st, struct mdinfo *info,
564 char *update, char *devname, int verbose,
565 int uuid_set, char *homehost)
566{
f352c545
DW
567 /* FIXME */
568
569 /* For 'assemble' and 'force' we need to return non-zero if any
570 * change was made. For others, the return value is ignored.
571 * Update options are:
572 * force-one : This device looks a bit old but needs to be included,
573 * update age info appropriately.
574 * assemble: clear any 'faulty' flag to allow this device to
575 * be assembled.
576 * force-array: Array is degraded but being forced, mark it clean
577 * if that will be needed to assemble it.
578 *
579 * newdev: not used ????
580 * grow: Array has gained a new device - this is currently for
581 * linear only
582 * resync: mark as dirty so a resync will happen.
583 * name: update the name - preserving the homehost
584 *
585 * Following are not relevant for this imsm:
586 * sparc2.2 : update from old dodgey metadata
587 * super-minor: change the preferred_minor number
588 * summaries: update redundant counters.
589 * uuid: Change the uuid of the array to match watch is given
590 * homehost: update the recorded homehost
591 * _reshape_progress: record new reshape_progress position.
592 */
593 int rv = 0;
594 //struct intel_super *super = st->sb;
595 //struct imsm_super *mpb = super->mpb;
596
597 if (strcmp(update, "grow") == 0) {
598 }
599 if (strcmp(update, "resync") == 0) {
600 /* dev->vol.dirty = 1; */
601 }
602
603 /* IMSM has no concept of UUID or homehost */
604
605 return rv;
cdddbdbc
DW
606}
607
c2c087e6 608static size_t disks_to_mpb_size(int disks)
cdddbdbc 609{
c2c087e6 610 size_t size;
cdddbdbc 611
c2c087e6
DW
612 size = sizeof(struct imsm_super);
613 size += (disks - 1) * sizeof(struct imsm_disk);
614 size += 2 * sizeof(struct imsm_dev);
615 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
616 size += (4 - 2) * sizeof(struct imsm_map);
617 /* 4 possible disk_ord_tbl's */
618 size += 4 * (disks - 1) * sizeof(__u32);
619
620 return size;
621}
622
623static __u64 avail_size_imsm(struct supertype *st, __u64 devsize)
624{
625 if (devsize < (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS))
626 return 0;
627
628 return devsize - (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS);
cdddbdbc
DW
629}
630
631static int compare_super_imsm(struct supertype *st, struct supertype *tst)
632{
633 /*
634 * return:
635 * 0 same, or first was empty, and second was copied
636 * 1 second had wrong number
637 * 2 wrong uuid
638 * 3 wrong other info
639 */
640 struct intel_super *first = st->sb;
641 struct intel_super *sec = tst->sb;
642
643 if (!first) {
644 st->sb = tst->sb;
645 tst->sb = NULL;
646 return 0;
647 }
648
649 if (memcmp(first->mpb->sig, sec->mpb->sig, MAX_SIGNATURE_LENGTH) != 0)
650 return 3;
651 if (first->mpb->family_num != sec->mpb->family_num)
652 return 3;
653 if (first->mpb->mpb_size != sec->mpb->mpb_size)
654 return 3;
655 if (first->mpb->check_sum != sec->mpb->check_sum)
656 return 3;
657
658 return 0;
659}
660
661extern int scsi_get_serial(int fd, void *buf, size_t buf_len);
662
663static int imsm_read_serial(int fd, char *devname,
664 __u8 serial[MAX_RAID_SERIAL_LEN])
665{
666 unsigned char scsi_serial[255];
cdddbdbc 667 int sg_fd;
cdddbdbc
DW
668 int rv;
669 int rsp_len;
670 int i, cnt;
671
672 memset(scsi_serial, 0, sizeof(scsi_serial));
cdddbdbc 673
90c8b707 674 sg_fd = sysfs_disk_to_sg(fd);
cdddbdbc
DW
675 if (sg_fd < 0) {
676 if (devname)
677 fprintf(stderr,
90c8b707
DW
678 Name ": Failed to open sg interface for %s: %s\n",
679 devname, strerror(errno));
cdddbdbc
DW
680 return 1;
681 }
682
683 rv = scsi_get_serial(sg_fd, scsi_serial, sizeof(scsi_serial));
684 close(sg_fd);
685
686 if (rv != 0) {
687 if (devname)
688 fprintf(stderr,
689 Name ": Failed to retrieve serial for %s\n",
690 devname);
691 return rv;
692 }
693
694 rsp_len = scsi_serial[3];
695 for (i = 0, cnt = 0; i < rsp_len; i++) {
696 if (!isspace(scsi_serial[4 + i]))
697 serial[cnt++] = scsi_serial[4 + i];
698 if (cnt == MAX_RAID_SERIAL_LEN)
699 break;
700 }
701
702 serial[MAX_RAID_SERIAL_LEN - 1] = '\0';
703
704 return 0;
705}
706
707static int
708load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd)
709{
710 struct imsm_super *mpb = super->mpb;
711 struct dl *dl;
712 struct stat stb;
713 struct imsm_disk *disk;
714 int rv;
715 int i;
716
717 dl = malloc(sizeof(*dl));
718 if (!dl) {
719 if (devname)
720 fprintf(stderr,
721 Name ": failed to allocate disk buffer for %s\n",
722 devname);
723 return 2;
724 }
725 memset(dl, 0, sizeof(*dl));
726
727 fstat(fd, &stb);
728 dl->major = major(stb.st_rdev);
729 dl->minor = minor(stb.st_rdev);
730 dl->next = super->disks;
731 dl->fd = keep_fd ? fd : -1;
732 dl->devname = devname ? strdup(devname) : NULL;
733 dl->index = -1;
734 super->disks = dl;
735 rv = imsm_read_serial(fd, devname, dl->serial);
736
737 if (rv != 0)
738 return 2;
739
740 /* look up this disk's index */
741 for (i = 0; i < mpb->num_disks; i++) {
742 disk = get_imsm_disk(mpb, i);
743
744 if (memcmp(disk->serial, dl->serial, MAX_RAID_SERIAL_LEN) == 0)
745 break;
746 }
747
748 if (i > mpb->num_disks)
749 return 2;
750
751 dl->index = i;
752
753 return 0;
754}
755
756/* load_imsm_mpb - read matrix metadata
757 * allocates super->mpb to be freed by free_super
758 */
759static int load_imsm_mpb(int fd, struct intel_super *super, char *devname)
760{
761 unsigned long long dsize;
762 size_t len, mpb_size;
763 unsigned long long sectors;
764 struct stat;
6416d527 765 struct imsm_super *anchor;
cdddbdbc
DW
766 __u32 check_sum;
767
768 memset(super, 0, sizeof(*super));
769 get_dev_size(fd, NULL, &dsize);
770
771 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0) {
772 if (devname)
773 fprintf(stderr,
774 Name ": Cannot seek to anchor block on %s: %s\n",
775 devname, strerror(errno));
776 return 1;
777 }
778
6416d527
NB
779 len = 512;
780 posix_memalign((void**)&anchor, 512, len);
781 if (read(fd, anchor, len) != len) {
cdddbdbc
DW
782 if (devname)
783 fprintf(stderr,
784 Name ": Cannot read anchor block on %s: %s\n",
785 devname, strerror(errno));
6416d527 786 free(anchor);
cdddbdbc
DW
787 return 1;
788 }
789
6416d527 790 if (strncmp((char *) anchor->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) {
cdddbdbc
DW
791 if (devname)
792 fprintf(stderr,
793 Name ": no IMSM anchor on %s\n", devname);
6416d527 794 free(anchor);
cdddbdbc
DW
795 return 2;
796 }
797
6416d527
NB
798 mpb_size = __le32_to_cpu(anchor->mpb_size);
799 mpb_size = ROUND_UP(mpb_size, 512);
800 posix_memalign((void**)&super->mpb, 512, mpb_size);
cdddbdbc
DW
801 if (!super->mpb) {
802 if (devname)
803 fprintf(stderr,
804 Name ": unable to allocate %zu byte mpb buffer\n",
805 mpb_size);
6416d527 806 free(anchor);
cdddbdbc
DW
807 return 2;
808 }
6416d527 809 memcpy(super->buf, anchor, len);
cdddbdbc 810
6416d527
NB
811 sectors = mpb_sectors(anchor) - 1;
812 free(anchor);
cdddbdbc
DW
813 if (!sectors)
814 return load_imsm_disk(fd, super, devname, 0);
815
816 /* read the extended mpb */
817 if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0) {
818 if (devname)
819 fprintf(stderr,
820 Name ": Cannot seek to extended mpb on %s: %s\n",
821 devname, strerror(errno));
822 return 1;
823 }
824
825 len = mpb_size - 512;
826 if (read(fd, super->buf + 512, len) != len) {
827 if (devname)
828 fprintf(stderr,
829 Name ": Cannot read extended mpb on %s: %s\n",
830 devname, strerror(errno));
831 return 2;
832 }
833
834 check_sum = gen_imsm_checksum(super->mpb);
835 if (check_sum != __le32_to_cpu(super->mpb->check_sum)) {
836 if (devname)
837 fprintf(stderr,
838 Name ": IMSM checksum %x != %x on %s\n",
839 check_sum, __le32_to_cpu(super->mpb->check_sum),
840 devname);
841 return 2;
842 }
843
844 return load_imsm_disk(fd, super, devname, 0);
845}
846
847struct superswitch super_imsm_container;
848
849static void free_imsm_disks(struct intel_super *super)
850{
851 while (super->disks) {
852 struct dl *d = super->disks;
853
854 super->disks = d->next;
855 if (d->fd >= 0)
856 close(d->fd);
857 if (d->devname)
858 free(d->devname);
859 free(d);
860 }
861}
862
863static void free_imsm(struct intel_super *super)
864{
865 if (super->mpb)
866 free(super->mpb);
867 free_imsm_disks(super);
868 free(super);
869}
870
871
872static void free_super_imsm(struct supertype *st)
873{
874 struct intel_super *super = st->sb;
875
876 if (!super)
877 return;
878
879 free_imsm(super);
880 st->sb = NULL;
881}
882
c2c087e6
DW
883static struct intel_super *alloc_super(int creating_imsm)
884{
885 struct intel_super *super = malloc(sizeof(*super));
886
887 if (super) {
888 memset(super, 0, sizeof(*super));
889 super->creating_imsm = creating_imsm;
890 super->creating_dev = -1;
891 }
892
893 return super;
894}
895
cdddbdbc
DW
896#ifndef MDASSEMBLE
897static int load_super_imsm_all(struct supertype *st, int fd, void **sbp,
898 char *devname, int keep_fd)
899{
900 struct mdinfo *sra;
901 struct intel_super *super;
902 struct mdinfo *sd, *best = NULL;
903 __u32 bestgen = 0;
904 __u32 gen;
905 char nm[20];
906 int dfd;
907 int rv;
908
909 /* check if this disk is a member of an active array */
910 sra = sysfs_read(fd, 0, GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE);
911 if (!sra)
912 return 1;
913
914 if (sra->array.major_version != -1 ||
915 sra->array.minor_version != -2 ||
916 strcmp(sra->text_version, "imsm") != 0)
917 return 1;
918
c2c087e6 919 super = alloc_super(0);
cdddbdbc
DW
920 if (!super)
921 return 1;
922
923 /* find the most up to date disk in this array */
924 for (sd = sra->devs; sd; sd = sd->next) {
925 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
926 dfd = dev_open(nm, keep_fd ? O_RDWR : O_RDONLY);
927 if (!dfd) {
928 free_imsm(super);
929 return 2;
930 }
931 rv = load_imsm_mpb(dfd, super, NULL);
932 if (!keep_fd)
933 close(dfd);
934 if (rv == 0) {
935 gen = __le32_to_cpu(super->mpb->generation_num);
936 if (!best || gen > bestgen) {
937 bestgen = gen;
938 best = sd;
939 }
940 } else {
941 free_imsm(super);
942 return 2;
943 }
944 }
945
946 if (!best) {
947 free_imsm(super);
948 return 1;
949 }
950
951 /* load the most up to date anchor */
952 sprintf(nm, "%d:%d", best->disk.major, best->disk.minor);
953 dfd = dev_open(nm, O_RDONLY);
954 if (!dfd) {
955 free_imsm(super);
956 return 1;
957 }
958 rv = load_imsm_mpb(dfd, super, NULL);
959 close(dfd);
960 if (rv != 0) {
961 free_imsm(super);
962 return 2;
963 }
964
965 /* reset the disk list */
966 free_imsm_disks(super);
967
968 /* populate disk list */
969 for (sd = sra->devs ; sd ; sd = sd->next) {
970 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
971 dfd = dev_open(nm, keep_fd? O_RDWR : O_RDONLY);
972 if (!dfd) {
973 free_imsm(super);
974 return 2;
975 }
976 load_imsm_disk(dfd, super, NULL, keep_fd);
977 if (!keep_fd)
978 close(dfd);
979 }
980
f7e7067b
NB
981 if (st->subarray[0]) {
982 /* FIXME */
983 }
984
cdddbdbc
DW
985 *sbp = super;
986 if (st->ss == NULL) {
987 st->ss = &super_imsm_container;
988 st->minor_version = 0;
989 st->max_devs = IMSM_MAX_DEVICES;
f4d11639 990 st->container_dev = fd2devnum(fd);
cdddbdbc
DW
991 }
992
993 return 0;
994}
995#endif
996
997static int load_super_imsm(struct supertype *st, int fd, char *devname)
998{
999 struct intel_super *super;
1000 int rv;
1001
1002#ifndef MDASSEMBLE
3dbccbcf 1003 if (load_super_imsm_all(st, fd, &st->sb, devname, 1) == 0)
cdddbdbc
DW
1004 return 0;
1005#endif
f7e7067b
NB
1006 if (st->subarray[0])
1007 return 1; /* FIXME */
cdddbdbc 1008
c2c087e6 1009 super = alloc_super(0);
cdddbdbc
DW
1010 if (!super) {
1011 fprintf(stderr,
1012 Name ": malloc of %zu failed.\n",
1013 sizeof(*super));
1014 return 1;
1015 }
1016
1017 rv = load_imsm_mpb(fd, super, devname);
1018
1019 if (rv) {
1020 if (devname)
1021 fprintf(stderr,
1022 Name ": Failed to load all information "
1023 "sections on %s\n", devname);
1024 free_imsm(super);
1025 return rv;
1026 }
1027
1028 st->sb = super;
1029 if (st->ss == NULL) {
1030 st->ss = &super_imsm;
1031 st->minor_version = 0;
1032 st->max_devs = IMSM_MAX_DEVICES;
1033 }
1034
1035 return 0;
1036}
1037
1038static int init_zero_imsm(struct supertype *st, mdu_array_info_t *info,
1039 unsigned long long size, char *name,
1040 char *homehost, int *uuid)
1041{
551c80c1 1042 st->sb = NULL;
cdddbdbc
DW
1043 return 0;
1044}
1045
1046static int init_super_imsm(struct supertype *st, mdu_array_info_t *info,
1047 unsigned long long size, char *name,
1048 char *homehost, int *uuid)
1049{
c2c087e6
DW
1050 /* This is primarily called by Create when creating a new array.
1051 * We will then get add_to_super called for each component, and then
1052 * write_init_super called to write it out to each device.
1053 * For IMSM, Create can create on fresh devices or on a pre-existing
1054 * array.
1055 * To create on a pre-existing array a different method will be called.
1056 * This one is just for fresh drives.
1057 */
1058 struct intel_super *super;
1059 struct imsm_super *mpb;
1060 size_t mpb_size;
cdddbdbc 1061
c2c087e6
DW
1062 super = alloc_super(1);
1063 if (!super)
1064 return 0;
1065 mpb_size = disks_to_mpb_size(info->nr_disks);
6416d527 1066 posix_memalign((void**)&mpb, 512, mpb_size);
c2c087e6
DW
1067 if (!mpb) {
1068 free(super);
1069 return 0;
1070 }
1071 memset(mpb, 0, mpb_size);
1072
1073 memcpy(mpb->sig, MPB_SIGNATURE, strlen(MPB_SIGNATURE));
1074 memcpy(mpb->sig + strlen(MPB_SIGNATURE), MPB_VERSION_RAID5,
1075 strlen(MPB_VERSION_RAID5));
1076 mpb->mpb_size = mpb_size;
1077
1078 super->mpb = mpb;
1079 st->sb = super;
1080 return 1;
cdddbdbc
DW
1081}
1082
8b353278
DW
1083static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
1084 unsigned long long size, char *name,
1085 char *homehost, int *uuid)
cdddbdbc 1086{
c2c087e6
DW
1087 /* We are creating a volume inside a pre-existing container.
1088 * so st->sb is already set.
1089 */
1090 struct intel_super *super = st->sb;
1091 struct imsm_super *mpb = super->mpb;
1092 struct imsm_dev *dev;
1093 struct imsm_vol *vol;
1094 struct imsm_map *map;
1095 int idx = mpb->num_raid_devs;
1096 int i;
1097 unsigned long long array_blocks;
1098 unsigned long long sz;
1099 __u32 offset = 0;
cdddbdbc 1100
c2c087e6
DW
1101 if (mpb->num_raid_devs >= 2) {
1102 fprintf(stderr, Name": This imsm-container already has the "
1103 "maximum of 2 volumes\n");
1104 return 0;
1105 }
1106
1107 super->creating_dev = idx;
1108 mpb->num_raid_devs++;
1109 dev = get_imsm_dev(mpb, idx);
1110 strncpy((char *) dev->volume, name, MAX_RAID_SERIAL_LEN);
1111 array_blocks = calc_array_size(info->level, info->raid_disks,
1112 info->layout, info->chunk_size,
1113 info->size*2);
1114 dev->size_low = __cpu_to_le32((__u32) array_blocks);
1115 dev->size_high = __cpu_to_le32((__u32) (array_blocks >> 32));
1116 dev->status = __cpu_to_le32(0);
1117 dev->reserved_blocks = __cpu_to_le32(0);
1118 vol = &dev->vol;
1119 vol->migr_state = 0;
1120 vol->migr_type = 0;
1121 vol->dirty = 0;
1122 for (i = 0; i < idx; i++) {
1123 struct imsm_dev *prev = get_imsm_dev(mpb, i);
1124 struct imsm_map *pmap = &prev->vol.map[0];
1125
1126 offset += __le32_to_cpu(pmap->blocks_per_member);
1127 offset += IMSM_RESERVED_SECTORS;
1128 }
1129 map = &vol->map[0];
1130 map->pba_of_lba0 = __cpu_to_le32(offset);
1131 sz = info->size * 2;
1132 map->blocks_per_member = __cpu_to_le32(sz);
1133 map->blocks_per_strip = __cpu_to_le16(info->chunk_size >> 9);
1134 map->num_data_stripes = __cpu_to_le32(sz / (info->chunk_size >> 9));
1135 map->map_state = info->level ? IMSM_T_STATE_UNINITIALIZED :
1136 IMSM_T_STATE_NORMAL;
1137 if (info->level == 10)
1138 map->raid_level = 1;
1139 else
1140 map->raid_level = info->level;
1141 map->num_members = info->raid_disks;
1142 for (i = 0; i < map->num_members; i++) {
1143 /* initialized in add_to_super */
1144 map->disk_ord_tbl[i] = __cpu_to_le32(0);
1145 }
1146
1147 return 1;
cdddbdbc
DW
1148}
1149
c2c087e6 1150static void add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
cdddbdbc
DW
1151 int fd, char *devname)
1152{
c2c087e6
DW
1153 struct intel_super *super = st->sb;
1154 struct imsm_super *mpb = super->mpb;
1155 struct imsm_disk *disk;
1156 struct dl *dd;
1157 unsigned long long size;
1158 __u32 status, id;
1159 int rv;
1160 struct stat stb;
1161
1162 fstat(fd, &stb);
1163 dd = malloc(sizeof(*dd));
1164 if (!dd) {
1165 fprintf(stderr,
1166 Name ": malloc failed %s:%d.\n", __func__, __LINE__);
1167 abort();
1168 }
1169 memset(dd, 0, sizeof(*dd));
1170 dd->major = major(stb.st_rdev);
1171 dd->minor = minor(stb.st_rdev);
1172 dd->index = dk->number;
1173 dd->devname = devname ? strdup(devname) : NULL;
1174 dd->next = super->disks;
1175 dd->fd = fd;
1176 rv = imsm_read_serial(fd, devname, dd->serial);
1177 if (rv) {
1178 fprintf(stderr,
1179 Name ": failed to retrieve scsi serial "
1180 "using \'%s\' instead\n", devname);
1181 strcpy((char *) dd->serial, devname);
1182 }
1183
1184 if (mpb->num_disks <= dk->number)
1185 mpb->num_disks = dk->number + 1;
1186
1187 disk = get_imsm_disk(mpb, dk->number);
1188 get_dev_size(fd, NULL, &size);
1189 size /= 512;
1190 status = USABLE_DISK | SPARE_DISK;
1191 strcpy((char *) disk->serial, (char *) dd->serial);
1192 disk->total_blocks = __cpu_to_le32(size);
1193 disk->status = __cpu_to_le32(status);
1194 if (sysfs_disk_to_scsi_id(fd, &id) == 0)
1195 disk->scsi_id = __cpu_to_le32(id);
1196 else
1197 disk->scsi_id = __cpu_to_le32(0);
1198
1199 /* update the family number if we are creating a container */
1200 if (super->creating_imsm)
1201 mpb->family_num = __cpu_to_le32(gen_imsm_checksum(mpb));
1202
1203 super->disks = dd;
cdddbdbc
DW
1204}
1205
c2c087e6 1206static void add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk,
8b353278 1207 int fd, char *devname)
cdddbdbc 1208{
c2c087e6
DW
1209 struct intel_super *super = st->sb;
1210 struct imsm_super *mpb = super->mpb;
1211 struct dl *dl;
1212 struct imsm_dev *dev;
1213 struct imsm_map *map;
1214 struct imsm_disk *disk;
1215 __u32 status;
1216
1217 if (super->creating_dev == -1) {
1218 fprintf(stderr, Name ": no active raid device\n");
1219 abort();
1220 }
1221
1222 dev = get_imsm_dev(mpb, super->creating_dev);
1223 map = &dev->vol.map[0];
1224
1225 for (dl = super->disks; dl ; dl = dl->next)
1226 if (dl->major == dk->major &&
1227 dl->minor == dk->minor)
1228 break;
1229 if (!dl || ! (dk->state & (1<<MD_DISK_SYNC)))
1230 return;
1231
1232 map->disk_ord_tbl[dk->number] = __cpu_to_le32(dl->index);
1233
1234 disk = get_imsm_disk(mpb, dl->index);
1235 status = CONFIGURED_DISK | USABLE_DISK;
1236 disk->status = __cpu_to_le32(status);
cdddbdbc
DW
1237}
1238
c2c087e6
DW
1239static int store_imsm_mpb(int fd, struct intel_super *super);
1240
1241static int write_super_imsm(struct intel_super *super, int doclose)
cdddbdbc 1242{
c2c087e6
DW
1243 struct imsm_super *mpb = super->mpb;
1244 struct dl *d;
1245 __u32 generation;
1246 __u32 sum;
cdddbdbc 1247
c2c087e6
DW
1248 /* 'generation' is incremented everytime the metadata is written */
1249 generation = __le32_to_cpu(mpb->generation_num);
1250 generation++;
1251 mpb->generation_num = __cpu_to_le32(generation);
1252
1253 /* recalculate checksum */
1254 sum = gen_imsm_checksum(mpb);
1255 mpb->check_sum = __cpu_to_le32(sum);
1256
1257 for (d = super->disks; d ; d = d->next) {
1258 if (store_imsm_mpb(d->fd, super)) {
1259 fprintf(stderr, "%s: failed for device %d:%d %s\n",
1260 __func__, d->major, d->minor, strerror(errno));
1261 return 0;
1262 }
1263 if (doclose) {
1264 close(d->fd);
1265 d->fd = -1;
1266 }
1267 }
1268
1269 return 1;
1270}
1271
1272static int write_init_super_imsm(struct supertype *st)
1273{
1274 return write_super_imsm(st->sb, 1);
cdddbdbc
DW
1275}
1276
1277static int store_zero_imsm(struct supertype *st, int fd)
1278{
551c80c1 1279 unsigned long long dsize;
6416d527 1280 void *buf;
551c80c1
DW
1281
1282 get_dev_size(fd, NULL, &dsize);
1283
1284 /* first block is stored on second to last sector of the disk */
1285 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0)
1286 return 1;
1287
6416d527 1288 posix_memalign(&buf, 512, 512);
551c80c1
DW
1289 memset(buf, 0, sizeof(buf));
1290 if (write(fd, buf, sizeof(buf)) != sizeof(buf))
1291 return 1;
cdddbdbc
DW
1292 return 0;
1293}
1294
cdddbdbc
DW
1295static int validate_geometry_imsm(struct supertype *st, int level, int layout,
1296 int raiddisks, int chunk, unsigned long long size,
2c514b71
NB
1297 char *dev, unsigned long long *freesize,
1298 int verbose)
cdddbdbc 1299{
c2c087e6
DW
1300 int fd, cfd;
1301 struct mdinfo *sra;
cdddbdbc 1302
c2c087e6
DW
1303 /* if given unused devices create a container
1304 * if given given devices in a container create a member volume
1305 */
1306 if (level == LEVEL_CONTAINER) {
1307 st->ss = &super_imsm_container;
1308 if (dev) {
1309 /* validate the container, dev == NULL */
1310 int rv = st->ss->validate_geometry(st, level, layout,
1311 raiddisks, chunk,
1312 size,
2c514b71
NB
1313 NULL, freesize,
1314 verbose);
c2c087e6
DW
1315 if (rv)
1316 return rv;
1317 }
1318 return st->ss->validate_geometry(st, level, layout, raiddisks,
2c514b71
NB
1319 chunk, size, dev, freesize,
1320 verbose);
c2c087e6
DW
1321 }
1322
1323 if (st->sb) {
1324 /* creating in a given container */
1325 st->ss = &super_imsm_volume;
1326 if (dev) {
1327 int rv = st->ss->validate_geometry(st, level, layout,
1328 raiddisks, chunk,
1329 size,
2c514b71
NB
1330 NULL, freesize,
1331 verbose);
c2c087e6
DW
1332 if (rv)
1333 return rv;
1334 }
1335 return st->ss->validate_geometry(st, level, layout, raiddisks,
2c514b71
NB
1336 chunk, size, dev, freesize,
1337 verbose);
c2c087e6
DW
1338 }
1339
1340 /* limit creation to the following levels */
1341 if (!dev)
1342 switch (level) {
1343 case 0:
1344 case 1:
1345 case 10:
1346 case 5:
1347 break;
1348 default:
1349 return 1;
1350 }
1351
1352 /* This device needs to be a device in an 'imsm' container */
1353 fd = open(dev, O_RDONLY|O_EXCL, 0);
1354 if (fd >= 0) {
2c514b71
NB
1355 if (verbose)
1356 fprintf(stderr,
1357 Name ": imsm: Cannot create this array on "
1358 "device %s\n", dev);
c2c087e6
DW
1359 close(fd);
1360 return 0;
1361 }
1362 if (errno != EBUSY || (fd = open(dev, O_RDONLY, 0)) < 0) {
2c514b71
NB
1363 if (verbose)
1364 fprintf(stderr, Name ": imsm: Cannot open %s: %s\n",
1365 dev, strerror(errno));
c2c087e6
DW
1366 return 0;
1367 }
1368 /* Well, it is in use by someone, maybe an 'imsm' container. */
1369 cfd = open_container(fd);
1370 if (cfd < 0) {
1371 close(fd);
2c514b71
NB
1372 if (verbose)
1373 fprintf(stderr, Name ": imsm: Cannot use %s: %s\n",
1374 dev, strerror(EBUSY));
c2c087e6
DW
1375 return 0;
1376 }
1377 sra = sysfs_read(cfd, 0, GET_VERSION);
1378 close(fd);
1379 if (sra && sra->array.major_version == -1 &&
1380 strcmp(sra->text_version, "imsm") == 0) {
1381 /* This is a member of a imsm container. Load the container
1382 * and try to create a volume
1383 */
1384 struct intel_super *super;
1385 st->ss = &super_imsm_volume;
1386 if (load_super_imsm_all(st, cfd, (void **) &super, NULL, 1) == 0) {
1387 st->sb = super;
1388 st->container_dev = fd2devnum(cfd);
1389 close(cfd);
1390 return st->ss->validate_geometry(st, level, layout,
1391 raiddisks, chunk, size,
2c514b71
NB
1392 dev, freesize,
1393 verbose);
c2c087e6
DW
1394 }
1395 close(cfd);
1396 } else /* may belong to another container */
1397 return 0;
1398
1399 return 1;
cdddbdbc
DW
1400}
1401
1402static int validate_geometry_imsm_container(struct supertype *st, int level,
1403 int layout, int raiddisks, int chunk,
c2c087e6 1404 unsigned long long size, char *dev,
2c514b71
NB
1405 unsigned long long *freesize,
1406 int verbose)
cdddbdbc 1407{
c2c087e6
DW
1408 int fd;
1409 unsigned long long ldsize;
cdddbdbc 1410
c2c087e6
DW
1411 if (level != LEVEL_CONTAINER)
1412 return 0;
1413 if (!dev)
1414 return 1;
1415
1416 fd = open(dev, O_RDONLY|O_EXCL, 0);
1417 if (fd < 0) {
2c514b71
NB
1418 if (verbose)
1419 fprintf(stderr, Name ": imsm: Cannot open %s: %s\n",
1420 dev, strerror(errno));
c2c087e6
DW
1421 return 0;
1422 }
1423 if (!get_dev_size(fd, dev, &ldsize)) {
1424 close(fd);
1425 return 0;
1426 }
1427 close(fd);
1428
1429 *freesize = avail_size_imsm(st, ldsize >> 9);
1430
1431 return 1;
cdddbdbc
DW
1432}
1433
c2c087e6
DW
1434/* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
1435 * FIX ME add ahci details
1436 */
8b353278
DW
1437static int validate_geometry_imsm_volume(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 struct stat stb;
1444 struct intel_super *super = st->sb;
1445 struct dl *dl;
1446 unsigned long long pos = 0;
1447 unsigned long long maxsize;
1448 struct extent *e;
1449 int i;
cdddbdbc 1450
c2c087e6
DW
1451 if (level == LEVEL_CONTAINER)
1452 return 0;
1453
1454 if (level == 1 && raiddisks > 2) {
2c514b71
NB
1455 if (verbose)
1456 fprintf(stderr, Name ": imsm does not support more "
1457 "than 2 in a raid1 configuration\n");
c2c087e6
DW
1458 return 0;
1459 }
1460
1461 /* We must have the container info already read in. */
1462 if (!super)
1463 return 0;
1464
1465 if (!dev) {
1466 /* General test: make sure there is space for
1467 * 'raiddisks' device extents of size 'size'.
1468 */
1469 unsigned long long minsize = size*2 /* convert to blocks */;
1470 int dcnt = 0;
1471 if (minsize == 0)
1472 minsize = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
1473 for (dl = super->disks; dl ; dl = dl->next) {
1474 int found = 0;
1475
1476 i = 0;
1477 e = get_extents(super, dl);
1478 if (!e) continue;
1479 do {
1480 unsigned long long esize;
1481 esize = e[i].start - pos;
1482 if (esize >= minsize)
1483 found = 1;
1484 pos = e[i].start + e[i].size;
1485 i++;
1486 } while (e[i-1].size);
1487 if (found)
1488 dcnt++;
1489 free(e);
1490 }
1491 if (dcnt < raiddisks) {
2c514b71
NB
1492 if (verbose)
1493 fprintf(stderr, Name ": imsm: Not enough "
1494 "devices with space for this array "
1495 "(%d < %d)\n",
1496 dcnt, raiddisks);
c2c087e6
DW
1497 return 0;
1498 }
1499 return 1;
1500 }
1501 /* This device must be a member of the set */
1502 if (stat(dev, &stb) < 0)
1503 return 0;
1504 if ((S_IFMT & stb.st_mode) != S_IFBLK)
1505 return 0;
1506 for (dl = super->disks ; dl ; dl = dl->next) {
1507 if (dl->major == major(stb.st_rdev) &&
1508 dl->minor == minor(stb.st_rdev))
1509 break;
1510 }
1511 if (!dl) {
2c514b71
NB
1512 if (verbose)
1513 fprintf(stderr, Name ": %s is not in the "
1514 "same imsm set\n", dev);
c2c087e6
DW
1515 return 0;
1516 }
1517 e = get_extents(super, dl);
1518 maxsize = 0;
1519 i = 0;
1520 if (e) do {
1521 unsigned long long esize;
1522 esize = e[i].start - pos;
1523 if (esize >= maxsize)
1524 maxsize = esize;
1525 pos = e[i].start + e[i].size;
1526 i++;
1527 } while (e[i-1].size);
1528 *freesize = maxsize;
1529
1530 return 1;
cdddbdbc
DW
1531}
1532
cdddbdbc
DW
1533static struct mdinfo *container_content_imsm(struct supertype *st)
1534{
4f5bc454
DW
1535 /* Given a container loaded by load_super_imsm_all,
1536 * extract information about all the arrays into
1537 * an mdinfo tree.
1538 *
1539 * For each imsm_dev create an mdinfo, fill it in,
1540 * then look for matching devices in super->disks
1541 * and create appropriate device mdinfo.
1542 */
1543 struct intel_super *super = st->sb;
1544 struct imsm_super *mpb = super->mpb;
1545 struct mdinfo *rest = NULL;
1546 int i;
cdddbdbc 1547
4f5bc454
DW
1548 for (i = 0; i < mpb->num_raid_devs; i++) {
1549 struct imsm_dev *dev = get_imsm_dev(mpb, i);
1550 struct imsm_vol *vol = &dev->vol;
1551 struct imsm_map *map = vol->map;
1552 struct mdinfo *this;
1553 __u64 sz;
1554 int slot;
1555
1556 this = malloc(sizeof(*this));
1557 memset(this, 0, sizeof(*this));
1558 this->next = rest;
1559 rest = this;
1560
4f5bc454
DW
1561 this->array.level = get_imsm_raid_level(map);
1562 this->array.raid_disks = map->num_members;
c2c087e6 1563 this->array.layout = imsm_level_to_layout(this->array.level);
4f5bc454
DW
1564 this->array.md_minor = -1;
1565 this->array.ctime = 0;
1566 this->array.utime = 0;
1567 this->array.chunk_size = __le16_to_cpu(map->blocks_per_strip) << 9;
1568 this->array.state = !vol->dirty;
1569 this->container_member = i;
0fd5c350
DW
1570 if (map->map_state == IMSM_T_STATE_UNINITIALIZED || dev->vol.dirty)
1571 this->resync_start = 0;
1572 else
1573 this->resync_start = ~0ULL;
1574
4f5bc454
DW
1575 strncpy(this->name, (char *) dev->volume, MAX_RAID_SERIAL_LEN);
1576 this->name[MAX_RAID_SERIAL_LEN] = 0;
1577
159c3a1a
NB
1578 sprintf(this->text_version, "/%s/%d",
1579 devnum2devname(st->container_dev),
1580 this->container_member);
1581
4f5bc454
DW
1582 memset(this->uuid, 0, sizeof(this->uuid));
1583
1584 sz = __le32_to_cpu(dev->size_high);
1585 sz <<= 32;
1586 sz += __le32_to_cpu(dev->size_low);
1587 this->component_size = sz;
1588 this->array.size = this->component_size / 2;
1589
1590 for (slot = 0 ; slot < map->num_members; slot++) {
1591 struct imsm_disk *disk;
1592 struct mdinfo *info_d;
1593 struct dl *d;
1594 int idx;
1595 __u32 s;
1596
1597 idx = __le32_to_cpu(map->disk_ord_tbl[slot] & ~(0xff << 24));
1598 for (d = super->disks; d ; d = d->next)
1599 if (d->index == idx)
1600 break;
1601
1602 if (d == NULL)
1603 break; /* shouldn't this be continue ?? */
1604
1605 info_d = malloc(sizeof(*info_d));
1606 if (!info_d)
1607 break; /* ditto ?? */
1608 memset(info_d, 0, sizeof(*info_d));
1609 info_d->next = this->devs;
1610 this->devs = info_d;
1611
1612 disk = get_imsm_disk(mpb, idx);
1613 s = __le32_to_cpu(disk->status);
1614
1615 info_d->disk.number = d->index;
1616 info_d->disk.major = d->major;
1617 info_d->disk.minor = d->minor;
1618 info_d->disk.raid_disk = slot;
1619 info_d->disk.state = s & CONFIGURED_DISK ? (1 << MD_DISK_ACTIVE) : 0;
1620 info_d->disk.state |= s & FAILED_DISK ? (1 << MD_DISK_FAULTY) : 0;
1621 info_d->disk.state |= s & USABLE_DISK ? (1 << MD_DISK_SYNC) : 0;
1622
1623 this->array.working_disks++;
1624
1625 info_d->events = __le32_to_cpu(mpb->generation_num);
1626 info_d->data_offset = __le32_to_cpu(map->pba_of_lba0);
1627 info_d->component_size = __le32_to_cpu(map->blocks_per_member);
1628 if (d->devname)
1629 strcpy(info_d->name, d->devname);
1630 }
1631 }
1632
1633 return rest;
cdddbdbc
DW
1634}
1635
845dea95 1636
cba0191b
NB
1637static int imsm_open_new(struct supertype *c, struct active_array *a,
1638 char *inst)
845dea95 1639{
4e6e574a 1640 dprintf("imsm: open_new %s\n", inst);
cba0191b 1641 a->info.container_member = atoi(inst);
845dea95
NB
1642 return 0;
1643}
1644
c2a1e7da
DW
1645static __u8 imsm_check_degraded(struct imsm_super *mpb, int n, int failed)
1646{
1647 struct imsm_dev *dev = get_imsm_dev(mpb, n);
1648 struct imsm_map *map = dev->vol.map;
1649
1650 if (!failed)
1651 return map->map_state;
1652
1653 switch (get_imsm_raid_level(map)) {
1654 case 0:
1655 return IMSM_T_STATE_FAILED;
1656 break;
1657 case 1:
1658 if (failed < map->num_members)
1659 return IMSM_T_STATE_DEGRADED;
1660 else
1661 return IMSM_T_STATE_FAILED;
1662 break;
1663 case 10:
1664 {
1665 /**
1666 * check to see if any mirrors have failed,
1667 * otherwise we are degraded
1668 */
1669 int device_per_mirror = 2; /* FIXME is this always the case?
1670 * and are they always adjacent?
1671 */
1672 int failed = 0;
1673 int i;
1674
1675 for (i = 0; i < map->num_members; i++) {
1676 int idx = get_imsm_disk_idx(map, i);
1677 struct imsm_disk *disk = get_imsm_disk(mpb, idx);
1678
1679 if (__le32_to_cpu(disk->status) & FAILED_DISK)
1680 failed++;
1681
1682 if (failed >= device_per_mirror)
1683 return IMSM_T_STATE_FAILED;
1684
1685 /* reset 'failed' for next mirror set */
1686 if (!((i + 1) % device_per_mirror))
1687 failed = 0;
1688 }
1689
1690 return IMSM_T_STATE_DEGRADED;
1691 }
1692 case 5:
1693 if (failed < 2)
1694 return IMSM_T_STATE_DEGRADED;
1695 else
1696 return IMSM_T_STATE_FAILED;
1697 break;
1698 default:
1699 break;
1700 }
1701
1702 return map->map_state;
1703}
1704
1705static int imsm_count_failed(struct imsm_super *mpb, struct imsm_map *map)
1706{
1707 int i;
1708 int failed = 0;
1709 struct imsm_disk *disk;
1710
1711 for (i = 0; i < map->num_members; i++) {
1712 int idx = get_imsm_disk_idx(map, i);
1713
1714 disk = get_imsm_disk(mpb, idx);
1715 if (__le32_to_cpu(disk->status) & FAILED_DISK)
1716 failed++;
1717 }
1718
1719 return failed;
845dea95
NB
1720}
1721
a862209d
DW
1722static void imsm_set_array_state(struct active_array *a, int consistent)
1723{
1724 int inst = a->info.container_member;
1725 struct intel_super *super = a->container->sb;
1726 struct imsm_dev *dev = get_imsm_dev(super->mpb, inst);
1727 struct imsm_map *map = &dev->vol.map[0];
1728 int dirty = !consistent;
1729 int failed;
1730 __u8 map_state;
1731
1732 if (a->resync_start == ~0ULL) {
1733 failed = imsm_count_failed(super->mpb, map);
1734 map_state = imsm_check_degraded(super->mpb, inst, failed);
1735 if (!failed)
1736 map_state = IMSM_T_STATE_NORMAL;
1737 if (map->map_state != map_state) {
4e6e574a 1738 dprintf("imsm: map_state %d: %d\n",
a862209d
DW
1739 inst, map_state);
1740 map->map_state = map_state;
1741 super->updates_pending++;
1742 }
1743 }
1744
1745 if (dev->vol.dirty != dirty) {
4e6e574a 1746 dprintf("imsm: mark '%s' (%llu)\n",
a862209d
DW
1747 dirty?"dirty":"clean", a->resync_start);
1748
1749 dev->vol.dirty = dirty;
1750 super->updates_pending++;
1751 }
1752}
1753
8d45d196 1754static void imsm_set_disk(struct active_array *a, int n, int state)
845dea95 1755{
8d45d196
DW
1756 int inst = a->info.container_member;
1757 struct intel_super *super = a->container->sb;
1758 struct imsm_dev *dev = get_imsm_dev(super->mpb, inst);
1759 struct imsm_map *map = dev->vol.map;
1760 struct imsm_disk *disk;
1761 __u32 status;
1762 int failed = 0;
1763 int new_failure = 0;
1764
1765 if (n > map->num_members)
1766 fprintf(stderr, "imsm: set_disk %d out of range 0..%d\n",
1767 n, map->num_members - 1);
1768
1769 if (n < 0)
1770 return;
1771
4e6e574a 1772 dprintf("imsm: set_disk %d:%x\n", n, state);
8d45d196
DW
1773
1774 disk = get_imsm_disk(super->mpb, get_imsm_disk_idx(map, n));
1775
1776 /* check if we have seen this failure before */
1777 status = __le32_to_cpu(disk->status);
1778 if ((state & DS_FAULTY) && !(status & FAILED_DISK)) {
1779 status |= FAILED_DISK;
1780 disk->status = __cpu_to_le32(status);
1781 new_failure = 1;
1782 }
1783
1784 /**
1785 * the number of failures have changed, count up 'failed' to determine
1786 * degraded / failed status
1787 */
1788 if (new_failure && map->map_state != IMSM_T_STATE_FAILED)
1789 failed = imsm_count_failed(super->mpb, map);
1790
1791 if (failed)
1792 map->map_state = imsm_check_degraded(super->mpb, inst, failed);
1793
1794 if (new_failure)
1795 super->updates_pending++;
845dea95
NB
1796}
1797
c2a1e7da
DW
1798static int store_imsm_mpb(int fd, struct intel_super *super)
1799{
1800 struct imsm_super *mpb = super->mpb;
1801 __u32 mpb_size = __le32_to_cpu(mpb->mpb_size);
1802 unsigned long long dsize;
1803 unsigned long long sectors;
1804
1805 get_dev_size(fd, NULL, &dsize);
1806
272f648f
DW
1807 if (mpb_size > 512) {
1808 /* -1 to account for anchor */
1809 sectors = mpb_sectors(mpb) - 1;
c2a1e7da 1810
272f648f
DW
1811 /* write the extended mpb to the sectors preceeding the anchor */
1812 if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0)
1813 return 1;
c2a1e7da 1814
99e29264 1815 if (write(fd, super->buf + 512, 512 * sectors) != 512 * sectors)
272f648f
DW
1816 return 1;
1817 }
c2a1e7da 1818
272f648f
DW
1819 /* first block is stored on second to last sector of the disk */
1820 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0)
c2a1e7da
DW
1821 return 1;
1822
272f648f 1823 if (write(fd, super->buf, 512) != 512)
c2a1e7da
DW
1824 return 1;
1825
1826 fsync(fd);
1827
1828 return 0;
1829}
1830
2e735d19 1831static void imsm_sync_metadata(struct supertype *container)
845dea95 1832{
2e735d19 1833 struct intel_super *super = container->sb;
c2a1e7da
DW
1834
1835 if (!super->updates_pending)
1836 return;
1837
c2c087e6 1838 write_super_imsm(super, 0);
c2a1e7da
DW
1839
1840 super->updates_pending = 0;
845dea95
NB
1841}
1842
cdddbdbc
DW
1843struct superswitch super_imsm = {
1844#ifndef MDASSEMBLE
1845 .examine_super = examine_super_imsm,
1846 .brief_examine_super = brief_examine_super_imsm,
1847 .detail_super = detail_super_imsm,
1848 .brief_detail_super = brief_detail_super_imsm,
1849#endif
1850 .match_home = match_home_imsm,
1851 .uuid_from_super= uuid_from_super_imsm,
1852 .getinfo_super = getinfo_super_imsm,
1853 .update_super = update_super_imsm,
1854
1855 .avail_size = avail_size_imsm,
1856
1857 .compare_super = compare_super_imsm,
1858
1859 .load_super = load_super_imsm,
1860 .init_super = init_zero_imsm,
1861 .store_super = store_zero_imsm,
1862 .free_super = free_super_imsm,
1863 .match_metadata_desc = match_metadata_desc_imsm,
cdddbdbc
DW
1864
1865 .validate_geometry = validate_geometry_imsm,
cdddbdbc 1866 .external = 1,
845dea95
NB
1867
1868/* for mdmon */
1869 .open_new = imsm_open_new,
1870 .load_super = load_super_imsm,
ed9d66aa 1871 .set_array_state= imsm_set_array_state,
845dea95
NB
1872 .set_disk = imsm_set_disk,
1873 .sync_metadata = imsm_sync_metadata,
cdddbdbc
DW
1874};
1875
1876/* super_imsm_container is set by validate_geometry_imsm when given a
1877 * device that is not part of any array
1878 */
1879struct superswitch super_imsm_container = {
1880
1881 .validate_geometry = validate_geometry_imsm_container,
1882 .init_super = init_super_imsm,
1883 .add_to_super = add_to_super_imsm,
1884 .write_init_super = write_init_super_imsm,
1885 .getinfo_super = getinfo_super_imsm,
cdddbdbc
DW
1886 .load_super = load_super_imsm,
1887
1888#ifndef MDASSEMBLE
1889 .examine_super = examine_super_imsm,
1890 .brief_examine_super = brief_examine_super_imsm,
1891 .detail_super = detail_super_imsm,
1892 .brief_detail_super = brief_detail_super_imsm,
1893#endif
1894
1895 .free_super = free_super_imsm,
1896
1897 .container_content = container_content_imsm,
1898
cdddbdbc 1899 .external = 1,
cdddbdbc
DW
1900};
1901
0063ecba 1902static struct superswitch super_imsm_volume = {
cdddbdbc 1903 .update_super = update_super_imsm,
8b353278
DW
1904 .init_super = init_super_imsm_volume,
1905 .add_to_super = add_to_super_imsm_volume,
1906 .getinfo_super = getinfo_super_imsm_volume,
cdddbdbc
DW
1907 .write_init_super = write_init_super_imsm,
1908
1909 .load_super = load_super_imsm,
1910 .free_super = free_super_imsm,
8b353278 1911 .match_metadata_desc = match_metadata_desc_imsm_volume,
cdddbdbc
DW
1912
1913
8b353278 1914 .validate_geometry = validate_geometry_imsm_volume,
cdddbdbc 1915 .external = 2,
cdddbdbc 1916};