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