]> git.ipfire.org Git - thirdparty/mdadm.git/blob - super-intel.c
Move calls to SET_ARRAY_INFO to common helper.
[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 #define IMSM_ORD_REBUILD (1 << 24)
72 __u32 disk_ord_tbl[1]; /* disk_ord_tbl[num_members],
73 * top byte contains some flags
74 */
75 } __attribute__ ((packed));
76
77 struct imsm_vol {
78 __u32 reserved[2];
79 __u8 migr_state; /* Normal or Migrating */
80 __u8 migr_type; /* Initializing, Rebuilding, ... */
81 __u8 dirty;
82 __u8 fill[1];
83 __u32 filler[5];
84 struct imsm_map map[1];
85 /* here comes another one if migr_state */
86 } __attribute__ ((packed));
87
88 struct imsm_dev {
89 __u8 volume[MAX_RAID_SERIAL_LEN];
90 __u32 size_low;
91 __u32 size_high;
92 __u32 status; /* Persistent RaidDev status */
93 __u32 reserved_blocks; /* Reserved blocks at beginning of volume */
94 #define IMSM_DEV_FILLERS 12
95 __u32 filler[IMSM_DEV_FILLERS];
96 struct imsm_vol vol;
97 } __attribute__ ((packed));
98
99 struct imsm_super {
100 __u8 sig[MAX_SIGNATURE_LENGTH]; /* 0x00 - 0x1F */
101 __u32 check_sum; /* 0x20 - 0x23 MPB Checksum */
102 __u32 mpb_size; /* 0x24 - 0x27 Size of MPB */
103 __u32 family_num; /* 0x28 - 0x2B Checksum from first time this config was written */
104 __u32 generation_num; /* 0x2C - 0x2F Incremented each time this array's MPB is written */
105 __u32 error_log_size; /* 0x30 - 0x33 in bytes */
106 __u32 attributes; /* 0x34 - 0x37 */
107 __u8 num_disks; /* 0x38 Number of configured disks */
108 __u8 num_raid_devs; /* 0x39 Number of configured volumes */
109 __u8 error_log_pos; /* 0x3A */
110 __u8 fill[1]; /* 0x3B */
111 __u32 cache_size; /* 0x3c - 0x40 in mb */
112 __u32 orig_family_num; /* 0x40 - 0x43 original family num */
113 __u32 pwr_cycle_count; /* 0x44 - 0x47 simulated power cycle count for array */
114 __u32 bbm_log_size; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */
115 #define IMSM_FILLERS 35
116 __u32 filler[IMSM_FILLERS]; /* 0x4C - 0xD7 RAID_MPB_FILLERS */
117 struct imsm_disk disk[1]; /* 0xD8 diskTbl[numDisks] */
118 /* here comes imsm_dev[num_raid_devs] */
119 /* here comes BBM logs */
120 } __attribute__ ((packed));
121
122 #define BBM_LOG_MAX_ENTRIES 254
123
124 struct bbm_log_entry {
125 __u64 defective_block_start;
126 #define UNREADABLE 0xFFFFFFFF
127 __u32 spare_block_offset;
128 __u16 remapped_marked_count;
129 __u16 disk_ordinal;
130 } __attribute__ ((__packed__));
131
132 struct bbm_log {
133 __u32 signature; /* 0xABADB10C */
134 __u32 entry_count;
135 __u32 reserved_spare_block_count; /* 0 */
136 __u32 reserved; /* 0xFFFF */
137 __u64 first_spare_lba;
138 struct bbm_log_entry mapped_block_entries[BBM_LOG_MAX_ENTRIES];
139 } __attribute__ ((__packed__));
140
141
142 #ifndef MDASSEMBLE
143 static char *map_state_str[] = { "normal", "uninitialized", "degraded", "failed" };
144 #endif
145
146 static unsigned int sector_count(__u32 bytes)
147 {
148 return ((bytes + (512-1)) & (~(512-1))) / 512;
149 }
150
151 static unsigned int mpb_sectors(struct imsm_super *mpb)
152 {
153 return sector_count(__le32_to_cpu(mpb->mpb_size));
154 }
155
156 /* internal representation of IMSM metadata */
157 struct intel_super {
158 union {
159 void *buf; /* O_DIRECT buffer for reading/writing metadata */
160 struct imsm_super *anchor; /* immovable parameters */
161 };
162 size_t len; /* size of the 'buf' allocation */
163 void *next_buf; /* for realloc'ing buf from the manager */
164 size_t next_len;
165 int updates_pending; /* count of pending updates for mdmon */
166 int creating_imsm; /* flag to indicate container creation */
167 int current_vol; /* index of raid device undergoing creation */
168 #define IMSM_MAX_RAID_DEVS 2
169 struct imsm_dev *dev_tbl[IMSM_MAX_RAID_DEVS];
170 struct dl {
171 struct dl *next;
172 int index;
173 __u8 serial[MAX_RAID_SERIAL_LEN];
174 int major, minor;
175 char *devname;
176 struct imsm_disk disk;
177 int fd;
178 } *disks;
179 struct dl *add; /* list of disks to add while mdmon active */
180 struct bbm_log *bbm_log;
181 };
182
183 struct extent {
184 unsigned long long start, size;
185 };
186
187 /* definition of messages passed to imsm_process_update */
188 enum imsm_update_type {
189 update_activate_spare,
190 update_create_array,
191 update_add_disk,
192 };
193
194 struct imsm_update_activate_spare {
195 enum imsm_update_type type;
196 struct dl *dl;
197 int slot;
198 int array;
199 struct imsm_update_activate_spare *next;
200 };
201
202 struct imsm_update_create_array {
203 enum imsm_update_type type;
204 int dev_idx;
205 struct imsm_dev dev;
206 };
207
208 struct imsm_update_add_disk {
209 enum imsm_update_type type;
210 };
211
212 static int imsm_env_devname_as_serial(void)
213 {
214 char *val = getenv("IMSM_DEVNAME_AS_SERIAL");
215
216 if (val && atoi(val) == 1)
217 return 1;
218
219 return 0;
220 }
221
222
223 static struct supertype *match_metadata_desc_imsm(char *arg)
224 {
225 struct supertype *st;
226
227 if (strcmp(arg, "imsm") != 0 &&
228 strcmp(arg, "default") != 0
229 )
230 return NULL;
231
232 st = malloc(sizeof(*st));
233 memset(st, 0, sizeof(*st));
234 st->ss = &super_imsm;
235 st->max_devs = IMSM_MAX_DEVICES;
236 st->minor_version = 0;
237 st->sb = NULL;
238 return st;
239 }
240
241 #ifndef MDASSEMBLE
242 static __u8 *get_imsm_version(struct imsm_super *mpb)
243 {
244 return &mpb->sig[MPB_SIG_LEN];
245 }
246 #endif
247
248 /* retrieve a disk directly from the anchor when the anchor is known to be
249 * up-to-date, currently only at load time
250 */
251 static struct imsm_disk *__get_imsm_disk(struct imsm_super *mpb, __u8 index)
252 {
253 if (index >= mpb->num_disks)
254 return NULL;
255 return &mpb->disk[index];
256 }
257
258 #ifndef MDASSEMBLE
259 /* retrieve a disk from the parsed metadata */
260 static struct imsm_disk *get_imsm_disk(struct intel_super *super, __u8 index)
261 {
262 struct dl *d;
263
264 for (d = super->disks; d; d = d->next)
265 if (d->index == index)
266 return &d->disk;
267
268 return NULL;
269 }
270 #endif
271
272 /* generate a checksum directly from the anchor when the anchor is known to be
273 * up-to-date, currently only at load or write_super after coalescing
274 */
275 static __u32 __gen_imsm_checksum(struct imsm_super *mpb)
276 {
277 __u32 end = mpb->mpb_size / sizeof(end);
278 __u32 *p = (__u32 *) mpb;
279 __u32 sum = 0;
280
281 while (end--)
282 sum += __le32_to_cpu(*p++);
283
284 return sum - __le32_to_cpu(mpb->check_sum);
285 }
286
287 static size_t sizeof_imsm_map(struct imsm_map *map)
288 {
289 return sizeof(struct imsm_map) + sizeof(__u32) * (map->num_members - 1);
290 }
291
292 struct imsm_map *get_imsm_map(struct imsm_dev *dev, int second_map)
293 {
294 struct imsm_map *map = &dev->vol.map[0];
295
296 if (second_map && !dev->vol.migr_state)
297 return NULL;
298 else if (second_map) {
299 void *ptr = map;
300
301 return ptr + sizeof_imsm_map(map);
302 } else
303 return map;
304
305 }
306
307 /* return the size of the device.
308 * migr_state increases the returned size if map[0] were to be duplicated
309 */
310 static size_t sizeof_imsm_dev(struct imsm_dev *dev, int migr_state)
311 {
312 size_t size = sizeof(*dev) - sizeof(struct imsm_map) +
313 sizeof_imsm_map(get_imsm_map(dev, 0));
314
315 /* migrating means an additional map */
316 if (dev->vol.migr_state)
317 size += sizeof_imsm_map(get_imsm_map(dev, 1));
318 else if (migr_state)
319 size += sizeof_imsm_map(get_imsm_map(dev, 0));
320
321 return size;
322 }
323
324 static struct imsm_dev *__get_imsm_dev(struct imsm_super *mpb, __u8 index)
325 {
326 int offset;
327 int i;
328 void *_mpb = mpb;
329
330 if (index >= mpb->num_raid_devs)
331 return NULL;
332
333 /* devices start after all disks */
334 offset = ((void *) &mpb->disk[mpb->num_disks]) - _mpb;
335
336 for (i = 0; i <= index; i++)
337 if (i == index)
338 return _mpb + offset;
339 else
340 offset += sizeof_imsm_dev(_mpb + offset, 0);
341
342 return NULL;
343 }
344
345 static struct imsm_dev *get_imsm_dev(struct intel_super *super, __u8 index)
346 {
347 if (index >= super->anchor->num_raid_devs)
348 return NULL;
349 return super->dev_tbl[index];
350 }
351
352 static __u32 get_imsm_ord_tbl_ent(struct imsm_dev *dev, int slot)
353 {
354 struct imsm_map *map;
355
356 if (dev->vol.migr_state)
357 map = get_imsm_map(dev, 1);
358 else
359 map = get_imsm_map(dev, 0);
360
361 /* top byte identifies disk under rebuild */
362 return __le32_to_cpu(map->disk_ord_tbl[slot]);
363 }
364
365 #define ord_to_idx(ord) (((ord) << 8) >> 8)
366 static __u32 get_imsm_disk_idx(struct imsm_dev *dev, int slot)
367 {
368 __u32 ord = get_imsm_ord_tbl_ent(dev, slot);
369
370 return ord_to_idx(ord);
371 }
372
373 static void set_imsm_ord_tbl_ent(struct imsm_map *map, int slot, __u32 ord)
374 {
375 map->disk_ord_tbl[slot] = __cpu_to_le32(ord);
376 }
377
378 static int get_imsm_raid_level(struct imsm_map *map)
379 {
380 if (map->raid_level == 1) {
381 if (map->num_members == 2)
382 return 1;
383 else
384 return 10;
385 }
386
387 return map->raid_level;
388 }
389
390 #ifndef MDASSEMBLE
391 static int cmp_extent(const void *av, const void *bv)
392 {
393 const struct extent *a = av;
394 const struct extent *b = bv;
395 if (a->start < b->start)
396 return -1;
397 if (a->start > b->start)
398 return 1;
399 return 0;
400 }
401
402 static struct extent *get_extents(struct intel_super *super, struct dl *dl)
403 {
404 /* find a list of used extents on the given physical device */
405 struct extent *rv, *e;
406 int i, j;
407 int memberships = 0;
408
409 for (i = 0; i < super->anchor->num_raid_devs; i++) {
410 struct imsm_dev *dev = get_imsm_dev(super, i);
411 struct imsm_map *map = get_imsm_map(dev, 0);
412
413 for (j = 0; j < map->num_members; j++) {
414 __u32 index = get_imsm_disk_idx(dev, j);
415
416 if (index == dl->index)
417 memberships++;
418 }
419 }
420 rv = malloc(sizeof(struct extent) * (memberships + 1));
421 if (!rv)
422 return NULL;
423 e = rv;
424
425 for (i = 0; i < super->anchor->num_raid_devs; i++) {
426 struct imsm_dev *dev = get_imsm_dev(super, i);
427 struct imsm_map *map = get_imsm_map(dev, 0);
428
429 for (j = 0; j < map->num_members; j++) {
430 __u32 index = get_imsm_disk_idx(dev, j);
431
432 if (index == dl->index) {
433 e->start = __le32_to_cpu(map->pba_of_lba0);
434 e->size = __le32_to_cpu(map->blocks_per_member);
435 e++;
436 }
437 }
438 }
439 qsort(rv, memberships, sizeof(*rv), cmp_extent);
440
441 e->start = __le32_to_cpu(dl->disk.total_blocks) -
442 (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS);
443 e->size = 0;
444 return rv;
445 }
446
447 static void print_imsm_dev(struct imsm_dev *dev, int index)
448 {
449 __u64 sz;
450 int slot;
451 struct imsm_map *map = get_imsm_map(dev, 0);
452 __u32 ord;
453
454 printf("\n");
455 printf("[%s]:\n", dev->volume);
456 printf(" RAID Level : %d\n", get_imsm_raid_level(map));
457 printf(" Members : %d\n", map->num_members);
458 for (slot = 0; slot < map->num_members; slot++)
459 if (index == get_imsm_disk_idx(dev, slot))
460 break;
461 if (slot < map->num_members) {
462 ord = get_imsm_ord_tbl_ent(dev, slot);
463 printf(" This Slot : %d%s\n", slot,
464 ord & IMSM_ORD_REBUILD ? " (out-of-sync)" : "");
465 } else
466 printf(" This Slot : ?\n");
467 sz = __le32_to_cpu(dev->size_high);
468 sz <<= 32;
469 sz += __le32_to_cpu(dev->size_low);
470 printf(" Array Size : %llu%s\n", (unsigned long long)sz,
471 human_size(sz * 512));
472 sz = __le32_to_cpu(map->blocks_per_member);
473 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz,
474 human_size(sz * 512));
475 printf(" Sector Offset : %u\n",
476 __le32_to_cpu(map->pba_of_lba0));
477 printf(" Num Stripes : %u\n",
478 __le32_to_cpu(map->num_data_stripes));
479 printf(" Chunk Size : %u KiB\n",
480 __le16_to_cpu(map->blocks_per_strip) / 2);
481 printf(" Reserved : %d\n", __le32_to_cpu(dev->reserved_blocks));
482 printf(" Migrate State : %s", dev->vol.migr_state ? "migrating" : "idle");
483 if (dev->vol.migr_state)
484 printf(": %s", dev->vol.migr_type ? "rebuilding" : "initializing");
485 printf("\n");
486 printf(" Map State : %s", map_state_str[map->map_state]);
487 if (dev->vol.migr_state) {
488 struct imsm_map *map = get_imsm_map(dev, 1);
489 printf(" <-- %s", map_state_str[map->map_state]);
490 }
491 printf("\n");
492 printf(" Dirty State : %s\n", dev->vol.dirty ? "dirty" : "clean");
493 }
494
495 static void print_imsm_disk(struct imsm_super *mpb, int index)
496 {
497 struct imsm_disk *disk = __get_imsm_disk(mpb, index);
498 char str[MAX_RAID_SERIAL_LEN + 1];
499 __u32 s;
500 __u64 sz;
501
502 if (index < 0)
503 return;
504
505 printf("\n");
506 snprintf(str, MAX_RAID_SERIAL_LEN + 1, "%s", disk->serial);
507 printf(" Disk%02d Serial : %s\n", index, str);
508 s = __le32_to_cpu(disk->status);
509 printf(" State :%s%s%s%s\n", s&SPARE_DISK ? " spare" : "",
510 s&CONFIGURED_DISK ? " active" : "",
511 s&FAILED_DISK ? " failed" : "",
512 s&USABLE_DISK ? " usable" : "");
513 printf(" Id : %08x\n", __le32_to_cpu(disk->scsi_id));
514 sz = __le32_to_cpu(disk->total_blocks) -
515 (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS * mpb->num_raid_devs);
516 printf(" Usable Size : %llu%s\n", (unsigned long long)sz,
517 human_size(sz * 512));
518 }
519
520 static void examine_super_imsm(struct supertype *st, char *homehost)
521 {
522 struct intel_super *super = st->sb;
523 struct imsm_super *mpb = super->anchor;
524 char str[MAX_SIGNATURE_LENGTH];
525 int i;
526 __u32 sum;
527
528 snprintf(str, MPB_SIG_LEN, "%s", mpb->sig);
529 printf(" Magic : %s\n", str);
530 snprintf(str, strlen(MPB_VERSION_RAID0), "%s", get_imsm_version(mpb));
531 printf(" Version : %s\n", get_imsm_version(mpb));
532 printf(" Family : %08x\n", __le32_to_cpu(mpb->family_num));
533 printf(" Generation : %08x\n", __le32_to_cpu(mpb->generation_num));
534 sum = __le32_to_cpu(mpb->check_sum);
535 printf(" Checksum : %08x %s\n", sum,
536 __gen_imsm_checksum(mpb) == sum ? "correct" : "incorrect");
537 printf(" MPB Sectors : %d\n", mpb_sectors(mpb));
538 printf(" Disks : %d\n", mpb->num_disks);
539 printf(" RAID Devices : %d\n", mpb->num_raid_devs);
540 print_imsm_disk(mpb, super->disks->index);
541 if (super->bbm_log) {
542 struct bbm_log *log = super->bbm_log;
543
544 printf("\n");
545 printf("Bad Block Management Log:\n");
546 printf(" Log Size : %d\n", __le32_to_cpu(mpb->bbm_log_size));
547 printf(" Signature : %x\n", __le32_to_cpu(log->signature));
548 printf(" Entry Count : %d\n", __le32_to_cpu(log->entry_count));
549 printf(" Spare Blocks : %d\n", __le32_to_cpu(log->reserved_spare_block_count));
550 printf(" First Spare : %llx\n", __le64_to_cpu(log->first_spare_lba));
551 }
552 for (i = 0; i < mpb->num_raid_devs; i++)
553 print_imsm_dev(__get_imsm_dev(mpb, i), super->disks->index);
554 for (i = 0; i < mpb->num_disks; i++) {
555 if (i == super->disks->index)
556 continue;
557 print_imsm_disk(mpb, i);
558 }
559 }
560
561 static void brief_examine_super_imsm(struct supertype *st)
562 {
563 printf("ARRAY /dev/imsm metadata=imsm\n");
564 }
565
566 static void detail_super_imsm(struct supertype *st, char *homehost)
567 {
568 printf("%s\n", __FUNCTION__);
569 }
570
571 static void brief_detail_super_imsm(struct supertype *st)
572 {
573 printf("%s\n", __FUNCTION__);
574 }
575 #endif
576
577 static int match_home_imsm(struct supertype *st, char *homehost)
578 {
579 printf("%s\n", __FUNCTION__);
580
581 return 0;
582 }
583
584 static void uuid_from_super_imsm(struct supertype *st, int uuid[4])
585 {
586 /* imsm does not track uuid's so just make sure we never return
587 * the same value twice to break uuid matching in Manage_subdevs
588 * FIXME what about the use of uuid's with bitmap's?
589 */
590 static int dummy_id = 0;
591
592 uuid[0] = dummy_id++;
593 }
594
595 #if 0
596 static void
597 get_imsm_numerical_version(struct imsm_super *mpb, int *m, int *p)
598 {
599 __u8 *v = get_imsm_version(mpb);
600 __u8 *end = mpb->sig + MAX_SIGNATURE_LENGTH;
601 char major[] = { 0, 0, 0 };
602 char minor[] = { 0 ,0, 0 };
603 char patch[] = { 0, 0, 0 };
604 char *ver_parse[] = { major, minor, patch };
605 int i, j;
606
607 i = j = 0;
608 while (*v != '\0' && v < end) {
609 if (*v != '.' && j < 2)
610 ver_parse[i][j++] = *v;
611 else {
612 i++;
613 j = 0;
614 }
615 v++;
616 }
617
618 *m = strtol(minor, NULL, 0);
619 *p = strtol(patch, NULL, 0);
620 }
621 #endif
622
623 static int imsm_level_to_layout(int level)
624 {
625 switch (level) {
626 case 0:
627 case 1:
628 return 0;
629 case 5:
630 case 6:
631 return ALGORITHM_LEFT_ASYMMETRIC;
632 case 10:
633 return 0x102; //FIXME is this correct?
634 }
635 return -1;
636 }
637
638 static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info)
639 {
640 struct intel_super *super = st->sb;
641 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
642 struct imsm_map *map = get_imsm_map(dev, 0);
643
644 info->container_member = super->current_vol;
645 info->array.raid_disks = map->num_members;
646 info->array.level = get_imsm_raid_level(map);
647 info->array.layout = imsm_level_to_layout(info->array.level);
648 info->array.md_minor = -1;
649 info->array.ctime = 0;
650 info->array.utime = 0;
651 info->array.chunk_size = __le16_to_cpu(map->blocks_per_strip) << 9;
652 info->array.state = !dev->vol.dirty;
653
654 info->disk.major = 0;
655 info->disk.minor = 0;
656
657 info->data_offset = __le32_to_cpu(map->pba_of_lba0);
658 info->component_size = __le32_to_cpu(map->blocks_per_member);
659 memset(info->uuid, 0, sizeof(info->uuid));
660
661 if (map->map_state == IMSM_T_STATE_UNINITIALIZED ||
662 dev->vol.dirty || dev->vol.migr_state)
663 info->resync_start = 0;
664 else
665 info->resync_start = ~0ULL;
666
667 strncpy(info->name, (char *) dev->volume, MAX_RAID_SERIAL_LEN);
668 info->name[MAX_RAID_SERIAL_LEN] = 0;
669
670 info->array.major_version = -1;
671 info->array.minor_version = -2;
672 sprintf(info->text_version, "/%s/%d",
673 devnum2devname(st->container_dev),
674 info->container_member);
675 info->safe_mode_delay = 4000; /* 4 secs like the Matrix driver */
676 }
677
678
679 static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info)
680 {
681 struct intel_super *super = st->sb;
682 struct imsm_disk *disk;
683 __u32 s;
684
685 if (super->current_vol >= 0) {
686 getinfo_super_imsm_volume(st, info);
687 return;
688 }
689
690 /* Set raid_disks to zero so that Assemble will always pull in valid
691 * spares
692 */
693 info->array.raid_disks = 0;
694 info->array.level = LEVEL_CONTAINER;
695 info->array.layout = 0;
696 info->array.md_minor = -1;
697 info->array.ctime = 0; /* N/A for imsm */
698 info->array.utime = 0;
699 info->array.chunk_size = 0;
700
701 info->disk.major = 0;
702 info->disk.minor = 0;
703 info->disk.raid_disk = -1;
704 info->reshape_active = 0;
705 info->array.major_version = -1;
706 info->array.minor_version = -2;
707 strcpy(info->text_version, "imsm");
708 info->safe_mode_delay = 0;
709 info->disk.number = -1;
710 info->disk.state = 0;
711
712 if (super->disks) {
713 disk = &super->disks->disk;
714 info->disk.number = super->disks->index;
715 info->disk.raid_disk = super->disks->index;
716 info->data_offset = __le32_to_cpu(disk->total_blocks) -
717 (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS);
718 info->component_size = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
719 s = __le32_to_cpu(disk->status);
720 info->disk.state = s & CONFIGURED_DISK ? (1 << MD_DISK_ACTIVE) : 0;
721 info->disk.state |= s & FAILED_DISK ? (1 << MD_DISK_FAULTY) : 0;
722 info->disk.state |= s & USABLE_DISK ? (1 << MD_DISK_SYNC) : 0;
723 }
724 }
725
726 static int update_super_imsm(struct supertype *st, struct mdinfo *info,
727 char *update, char *devname, int verbose,
728 int uuid_set, char *homehost)
729 {
730 /* FIXME */
731
732 /* For 'assemble' and 'force' we need to return non-zero if any
733 * change was made. For others, the return value is ignored.
734 * Update options are:
735 * force-one : This device looks a bit old but needs to be included,
736 * update age info appropriately.
737 * assemble: clear any 'faulty' flag to allow this device to
738 * be assembled.
739 * force-array: Array is degraded but being forced, mark it clean
740 * if that will be needed to assemble it.
741 *
742 * newdev: not used ????
743 * grow: Array has gained a new device - this is currently for
744 * linear only
745 * resync: mark as dirty so a resync will happen.
746 * name: update the name - preserving the homehost
747 *
748 * Following are not relevant for this imsm:
749 * sparc2.2 : update from old dodgey metadata
750 * super-minor: change the preferred_minor number
751 * summaries: update redundant counters.
752 * uuid: Change the uuid of the array to match watch is given
753 * homehost: update the recorded homehost
754 * _reshape_progress: record new reshape_progress position.
755 */
756 int rv = 0;
757 //struct intel_super *super = st->sb;
758 //struct imsm_super *mpb = super->mpb;
759
760 if (strcmp(update, "grow") == 0) {
761 }
762 if (strcmp(update, "resync") == 0) {
763 /* dev->vol.dirty = 1; */
764 }
765
766 /* IMSM has no concept of UUID or homehost */
767
768 return rv;
769 }
770
771 static size_t disks_to_mpb_size(int disks)
772 {
773 size_t size;
774
775 size = sizeof(struct imsm_super);
776 size += (disks - 1) * sizeof(struct imsm_disk);
777 size += 2 * sizeof(struct imsm_dev);
778 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
779 size += (4 - 2) * sizeof(struct imsm_map);
780 /* 4 possible disk_ord_tbl's */
781 size += 4 * (disks - 1) * sizeof(__u32);
782
783 return size;
784 }
785
786 static __u64 avail_size_imsm(struct supertype *st, __u64 devsize)
787 {
788 if (devsize < (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS))
789 return 0;
790
791 return devsize - (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS);
792 }
793
794 static int compare_super_imsm(struct supertype *st, struct supertype *tst)
795 {
796 /*
797 * return:
798 * 0 same, or first was empty, and second was copied
799 * 1 second had wrong number
800 * 2 wrong uuid
801 * 3 wrong other info
802 */
803 struct intel_super *first = st->sb;
804 struct intel_super *sec = tst->sb;
805
806 if (!first) {
807 st->sb = tst->sb;
808 tst->sb = NULL;
809 return 0;
810 }
811
812 if (memcmp(first->anchor->sig, sec->anchor->sig, MAX_SIGNATURE_LENGTH) != 0)
813 return 3;
814
815 /* if an anchor does not have num_raid_devs set then it is a free
816 * floating spare
817 */
818 if (first->anchor->num_raid_devs > 0 &&
819 sec->anchor->num_raid_devs > 0) {
820 if (first->anchor->family_num != sec->anchor->family_num)
821 return 3;
822 }
823
824 /* if 'first' is a spare promote it to a populated mpb with sec's
825 * family number
826 */
827 if (first->anchor->num_raid_devs == 0 &&
828 sec->anchor->num_raid_devs > 0) {
829 first->anchor->num_raid_devs = sec->anchor->num_raid_devs;
830 first->anchor->family_num = sec->anchor->family_num;
831 }
832
833 return 0;
834 }
835
836 static void fd2devname(int fd, char *name)
837 {
838 struct stat st;
839 char path[256];
840 char dname[100];
841 char *nm;
842 int rv;
843
844 name[0] = '\0';
845 if (fstat(fd, &st) != 0)
846 return;
847 sprintf(path, "/sys/dev/block/%d:%d",
848 major(st.st_rdev), minor(st.st_rdev));
849
850 rv = readlink(path, dname, sizeof(dname));
851 if (rv <= 0)
852 return;
853
854 dname[rv] = '\0';
855 nm = strrchr(dname, '/');
856 nm++;
857 snprintf(name, MAX_RAID_SERIAL_LEN, "/dev/%s", nm);
858 }
859
860
861 extern int scsi_get_serial(int fd, void *buf, size_t buf_len);
862
863 static int imsm_read_serial(int fd, char *devname,
864 __u8 serial[MAX_RAID_SERIAL_LEN])
865 {
866 unsigned char scsi_serial[255];
867 int rv;
868 int rsp_len;
869 int len;
870 char *c, *rsp_buf;
871
872 memset(scsi_serial, 0, sizeof(scsi_serial));
873
874 rv = scsi_get_serial(fd, scsi_serial, sizeof(scsi_serial));
875
876 if (rv && imsm_env_devname_as_serial()) {
877 memset(serial, 0, MAX_RAID_SERIAL_LEN);
878 fd2devname(fd, (char *) serial);
879 return 0;
880 }
881
882 if (rv != 0) {
883 if (devname)
884 fprintf(stderr,
885 Name ": Failed to retrieve serial for %s\n",
886 devname);
887 return rv;
888 }
889
890 /* trim whitespace */
891 rsp_len = scsi_serial[3];
892 rsp_buf = (char *) &scsi_serial[4];
893 c = rsp_buf;
894 while (isspace(*c))
895 c++;
896 if (c + MAX_RAID_SERIAL_LEN > rsp_buf + rsp_len)
897 len = rsp_len - (c - rsp_buf);
898 else
899 len = MAX_RAID_SERIAL_LEN;
900 memcpy(serial, c, len);
901 c = (char *) &serial[len - 1];
902 while (isspace(*c) || *c == '\0')
903 *c-- = '\0';
904
905 return 0;
906 }
907
908 static int serialcmp(__u8 *s1, __u8 *s2)
909 {
910 return strncmp((char *) s1, (char *) s2, MAX_RAID_SERIAL_LEN);
911 }
912
913 static void serialcpy(__u8 *dest, __u8 *src)
914 {
915 strncpy((char *) dest, (char *) src, MAX_RAID_SERIAL_LEN);
916 }
917
918 static int
919 load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd)
920 {
921 struct dl *dl;
922 struct stat stb;
923 int rv;
924 int i;
925 int alloc = 1;
926 __u8 serial[MAX_RAID_SERIAL_LEN];
927
928 rv = imsm_read_serial(fd, devname, serial);
929
930 if (rv != 0)
931 return 2;
932
933 /* check if this is a disk we have seen before. it may be a spare in
934 * super->disks while the current anchor believes it is a raid member,
935 * check if we need to update dl->index
936 */
937 for (dl = super->disks; dl; dl = dl->next)
938 if (serialcmp(dl->serial, serial) == 0)
939 break;
940
941 if (!dl)
942 dl = malloc(sizeof(*dl));
943 else
944 alloc = 0;
945
946 if (!dl) {
947 if (devname)
948 fprintf(stderr,
949 Name ": failed to allocate disk buffer for %s\n",
950 devname);
951 return 2;
952 }
953
954 if (alloc) {
955 fstat(fd, &stb);
956 dl->major = major(stb.st_rdev);
957 dl->minor = minor(stb.st_rdev);
958 dl->next = super->disks;
959 dl->fd = keep_fd ? fd : -1;
960 dl->devname = devname ? strdup(devname) : NULL;
961 serialcpy(dl->serial, serial);
962 dl->index = -2;
963 } else if (keep_fd) {
964 close(dl->fd);
965 dl->fd = fd;
966 }
967
968 /* look up this disk's index in the current anchor */
969 for (i = 0; i < super->anchor->num_disks; i++) {
970 struct imsm_disk *disk_iter;
971
972 disk_iter = __get_imsm_disk(super->anchor, i);
973
974 if (serialcmp(disk_iter->serial, dl->serial) == 0) {
975 __u32 status;
976
977 dl->disk = *disk_iter;
978 status = __le32_to_cpu(dl->disk.status);
979 /* only set index on disks that are a member of a
980 * populated contianer, i.e. one with raid_devs
981 */
982 if (status & FAILED_DISK)
983 dl->index = -2;
984 else if (status & SPARE_DISK)
985 dl->index = -1;
986 else
987 dl->index = i;
988
989 break;
990 }
991 }
992
993 if (alloc)
994 super->disks = dl;
995
996 return 0;
997 }
998
999 static void imsm_copy_dev(struct imsm_dev *dest, struct imsm_dev *src)
1000 {
1001 memcpy(dest, src, sizeof_imsm_dev(src, 0));
1002 }
1003
1004 #ifndef MDASSEMBLE
1005 /* When migrating map0 contains the 'destination' state while map1
1006 * contains the current state. When not migrating map0 contains the
1007 * current state. This routine assumes that map[0].map_state is set to
1008 * the current array state before being called.
1009 *
1010 * Migration is indicated by one of the following states
1011 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
1012 * 2/ Initialize (migr_state=1 migr_type=0 map0state=normal
1013 * map1state=unitialized)
1014 * 3/ Verify (Resync) (migr_state=1 migr_type=1 map0state=normal
1015 * map1state=normal)
1016 * 4/ Rebuild (migr_state=1 migr_type=1 map0state=normal
1017 * map1state=degraded)
1018 */
1019 static void migrate(struct imsm_dev *dev, __u8 to_state, int rebuild_resync)
1020 {
1021 struct imsm_map *dest;
1022 struct imsm_map *src = get_imsm_map(dev, 0);
1023
1024 dev->vol.migr_state = 1;
1025 dev->vol.migr_type = rebuild_resync;
1026 dest = get_imsm_map(dev, 1);
1027
1028 memcpy(dest, src, sizeof_imsm_map(src));
1029 src->map_state = to_state;
1030 }
1031 #endif
1032
1033 static int parse_raid_devices(struct intel_super *super)
1034 {
1035 int i;
1036 struct imsm_dev *dev_new;
1037 size_t len, len_migr;
1038 size_t space_needed = 0;
1039 struct imsm_super *mpb = super->anchor;
1040
1041 for (i = 0; i < super->anchor->num_raid_devs; i++) {
1042 struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i);
1043
1044 len = sizeof_imsm_dev(dev_iter, 0);
1045 len_migr = sizeof_imsm_dev(dev_iter, 1);
1046 if (len_migr > len)
1047 space_needed += len_migr - len;
1048
1049 dev_new = malloc(len_migr);
1050 if (!dev_new)
1051 return 1;
1052 imsm_copy_dev(dev_new, dev_iter);
1053 super->dev_tbl[i] = dev_new;
1054 }
1055
1056 /* ensure that super->buf is large enough when all raid devices
1057 * are migrating
1058 */
1059 if (__le32_to_cpu(mpb->mpb_size) + space_needed > super->len) {
1060 void *buf;
1061
1062 len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + space_needed, 512);
1063 if (posix_memalign(&buf, 512, len) != 0)
1064 return 1;
1065
1066 memcpy(buf, super->buf, len);
1067 free(super->buf);
1068 super->buf = buf;
1069 super->len = len;
1070 }
1071
1072 return 0;
1073 }
1074
1075 /* retrieve a pointer to the bbm log which starts after all raid devices */
1076 struct bbm_log *__get_imsm_bbm_log(struct imsm_super *mpb)
1077 {
1078 void *ptr = NULL;
1079
1080 if (__le32_to_cpu(mpb->bbm_log_size)) {
1081 ptr = mpb;
1082 ptr += mpb->mpb_size - __le32_to_cpu(mpb->bbm_log_size);
1083 }
1084
1085 return ptr;
1086 }
1087
1088 static void __free_imsm(struct intel_super *super, int free_disks);
1089
1090 /* load_imsm_mpb - read matrix metadata
1091 * allocates super->mpb to be freed by free_super
1092 */
1093 static int load_imsm_mpb(int fd, struct intel_super *super, char *devname)
1094 {
1095 unsigned long long dsize;
1096 unsigned long long sectors;
1097 struct stat;
1098 struct imsm_super *anchor;
1099 __u32 check_sum;
1100 int rc;
1101
1102 get_dev_size(fd, NULL, &dsize);
1103
1104 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0) {
1105 if (devname)
1106 fprintf(stderr,
1107 Name ": Cannot seek to anchor block on %s: %s\n",
1108 devname, strerror(errno));
1109 return 1;
1110 }
1111
1112 if (posix_memalign((void**)&anchor, 512, 512) != 0) {
1113 if (devname)
1114 fprintf(stderr,
1115 Name ": Failed to allocate imsm anchor buffer"
1116 " on %s\n", devname);
1117 return 1;
1118 }
1119 if (read(fd, anchor, 512) != 512) {
1120 if (devname)
1121 fprintf(stderr,
1122 Name ": Cannot read anchor block on %s: %s\n",
1123 devname, strerror(errno));
1124 free(anchor);
1125 return 1;
1126 }
1127
1128 if (strncmp((char *) anchor->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) {
1129 if (devname)
1130 fprintf(stderr,
1131 Name ": no IMSM anchor on %s\n", devname);
1132 free(anchor);
1133 return 2;
1134 }
1135
1136 __free_imsm(super, 0);
1137 super->len = ROUND_UP(anchor->mpb_size, 512);
1138 if (posix_memalign(&super->buf, 512, super->len) != 0) {
1139 if (devname)
1140 fprintf(stderr,
1141 Name ": unable to allocate %zu byte mpb buffer\n",
1142 super->len);
1143 free(anchor);
1144 return 2;
1145 }
1146 memcpy(super->buf, anchor, 512);
1147
1148 sectors = mpb_sectors(anchor) - 1;
1149 free(anchor);
1150 if (!sectors) {
1151 rc = load_imsm_disk(fd, super, devname, 0);
1152 if (rc == 0)
1153 rc = parse_raid_devices(super);
1154 return rc;
1155 }
1156
1157 /* read the extended mpb */
1158 if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0) {
1159 if (devname)
1160 fprintf(stderr,
1161 Name ": Cannot seek to extended mpb on %s: %s\n",
1162 devname, strerror(errno));
1163 return 1;
1164 }
1165
1166 if (read(fd, super->buf + 512, super->len - 512) != super->len - 512) {
1167 if (devname)
1168 fprintf(stderr,
1169 Name ": Cannot read extended mpb on %s: %s\n",
1170 devname, strerror(errno));
1171 return 2;
1172 }
1173
1174 check_sum = __gen_imsm_checksum(super->anchor);
1175 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
1176 if (devname)
1177 fprintf(stderr,
1178 Name ": IMSM checksum %x != %x on %s\n",
1179 check_sum, __le32_to_cpu(super->anchor->check_sum),
1180 devname);
1181 return 2;
1182 }
1183
1184 /* FIXME the BBM log is disk specific so we cannot use this global
1185 * buffer for all disks. Ok for now since we only look at the global
1186 * bbm_log_size parameter to gate assembly
1187 */
1188 super->bbm_log = __get_imsm_bbm_log(super->anchor);
1189
1190 rc = load_imsm_disk(fd, super, devname, 0);
1191 if (rc == 0)
1192 rc = parse_raid_devices(super);
1193
1194 return rc;
1195 }
1196
1197 static void __free_imsm_disk(struct dl *d)
1198 {
1199 if (d->fd >= 0)
1200 close(d->fd);
1201 if (d->devname)
1202 free(d->devname);
1203 free(d);
1204
1205 }
1206 static void free_imsm_disks(struct intel_super *super)
1207 {
1208 while (super->disks) {
1209 struct dl *d = super->disks;
1210
1211 super->disks = d->next;
1212 __free_imsm_disk(d);
1213 }
1214 }
1215
1216 /* free all the pieces hanging off of a super pointer */
1217 static void __free_imsm(struct intel_super *super, int free_disks)
1218 {
1219 int i;
1220
1221 if (super->buf) {
1222 free(super->buf);
1223 super->buf = NULL;
1224 }
1225 if (free_disks)
1226 free_imsm_disks(super);
1227 for (i = 0; i < IMSM_MAX_RAID_DEVS; i++)
1228 if (super->dev_tbl[i]) {
1229 free(super->dev_tbl[i]);
1230 super->dev_tbl[i] = NULL;
1231 }
1232 }
1233
1234 static void free_imsm(struct intel_super *super)
1235 {
1236 __free_imsm(super, 1);
1237 free(super);
1238 }
1239
1240 static void free_super_imsm(struct supertype *st)
1241 {
1242 struct intel_super *super = st->sb;
1243
1244 if (!super)
1245 return;
1246
1247 free_imsm(super);
1248 st->sb = NULL;
1249 }
1250
1251 static struct intel_super *alloc_super(int creating_imsm)
1252 {
1253 struct intel_super *super = malloc(sizeof(*super));
1254
1255 if (super) {
1256 memset(super, 0, sizeof(*super));
1257 super->creating_imsm = creating_imsm;
1258 super->current_vol = -1;
1259 }
1260
1261 return super;
1262 }
1263
1264 #ifndef MDASSEMBLE
1265 static int load_super_imsm_all(struct supertype *st, int fd, void **sbp,
1266 char *devname, int keep_fd)
1267 {
1268 struct mdinfo *sra;
1269 struct intel_super *super;
1270 struct mdinfo *sd, *best = NULL;
1271 __u32 bestgen = 0;
1272 __u32 gen;
1273 char nm[20];
1274 int dfd;
1275 int rv;
1276
1277 /* check if this disk is a member of an active array */
1278 sra = sysfs_read(fd, 0, GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE);
1279 if (!sra)
1280 return 1;
1281
1282 if (sra->array.major_version != -1 ||
1283 sra->array.minor_version != -2 ||
1284 strcmp(sra->text_version, "imsm") != 0)
1285 return 1;
1286
1287 super = alloc_super(0);
1288 if (!super)
1289 return 1;
1290
1291 /* find the most up to date disk in this array, skipping spares */
1292 for (sd = sra->devs; sd; sd = sd->next) {
1293 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
1294 dfd = dev_open(nm, keep_fd ? O_RDWR : O_RDONLY);
1295 if (!dfd) {
1296 free_imsm(super);
1297 return 2;
1298 }
1299 rv = load_imsm_mpb(dfd, super, NULL);
1300 if (!keep_fd)
1301 close(dfd);
1302 if (rv == 0) {
1303 if (super->anchor->num_raid_devs == 0)
1304 gen = 0;
1305 else
1306 gen = __le32_to_cpu(super->anchor->generation_num);
1307 if (!best || gen > bestgen) {
1308 bestgen = gen;
1309 best = sd;
1310 }
1311 } else {
1312 free_imsm(super);
1313 return 2;
1314 }
1315 }
1316
1317 if (!best) {
1318 free_imsm(super);
1319 return 1;
1320 }
1321
1322 /* load the most up to date anchor */
1323 sprintf(nm, "%d:%d", best->disk.major, best->disk.minor);
1324 dfd = dev_open(nm, O_RDONLY);
1325 if (!dfd) {
1326 free_imsm(super);
1327 return 1;
1328 }
1329 rv = load_imsm_mpb(dfd, super, NULL);
1330 close(dfd);
1331 if (rv != 0) {
1332 free_imsm(super);
1333 return 2;
1334 }
1335
1336 /* re-parse the disk list with the current anchor */
1337 for (sd = sra->devs ; sd ; sd = sd->next) {
1338 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
1339 dfd = dev_open(nm, keep_fd? O_RDWR : O_RDONLY);
1340 if (!dfd) {
1341 free_imsm(super);
1342 return 2;
1343 }
1344 load_imsm_disk(dfd, super, NULL, keep_fd);
1345 if (!keep_fd)
1346 close(dfd);
1347 }
1348
1349 if (st->subarray[0]) {
1350 if (atoi(st->subarray) <= super->anchor->num_raid_devs)
1351 super->current_vol = atoi(st->subarray);
1352 else
1353 return 1;
1354 }
1355
1356 *sbp = super;
1357 st->container_dev = fd2devnum(fd);
1358 if (st->ss == NULL) {
1359 st->ss = &super_imsm;
1360 st->minor_version = 0;
1361 st->max_devs = IMSM_MAX_DEVICES;
1362 }
1363
1364 return 0;
1365 }
1366 #endif
1367
1368 static int load_super_imsm(struct supertype *st, int fd, char *devname)
1369 {
1370 struct intel_super *super;
1371 int rv;
1372
1373 #ifndef MDASSEMBLE
1374 if (load_super_imsm_all(st, fd, &st->sb, devname, 1) == 0)
1375 return 0;
1376 #endif
1377 if (st->subarray[0])
1378 return 1; /* FIXME */
1379
1380 super = alloc_super(0);
1381 if (!super) {
1382 fprintf(stderr,
1383 Name ": malloc of %zu failed.\n",
1384 sizeof(*super));
1385 return 1;
1386 }
1387
1388 rv = load_imsm_mpb(fd, super, devname);
1389
1390 if (rv) {
1391 if (devname)
1392 fprintf(stderr,
1393 Name ": Failed to load all information "
1394 "sections on %s\n", devname);
1395 free_imsm(super);
1396 return rv;
1397 }
1398
1399 st->sb = super;
1400 if (st->ss == NULL) {
1401 st->ss = &super_imsm;
1402 st->minor_version = 0;
1403 st->max_devs = IMSM_MAX_DEVICES;
1404 }
1405
1406 return 0;
1407 }
1408
1409 static __u16 info_to_blocks_per_strip(mdu_array_info_t *info)
1410 {
1411 if (info->level == 1)
1412 return 128;
1413 return info->chunk_size >> 9;
1414 }
1415
1416 static __u32 info_to_num_data_stripes(mdu_array_info_t *info)
1417 {
1418 __u32 num_stripes;
1419
1420 num_stripes = (info->size * 2) / info_to_blocks_per_strip(info);
1421 if (info->level == 1)
1422 num_stripes /= 2;
1423
1424 return num_stripes;
1425 }
1426
1427 static __u32 info_to_blocks_per_member(mdu_array_info_t *info)
1428 {
1429 return (info->size * 2) & ~(info_to_blocks_per_strip(info) - 1);
1430 }
1431
1432 static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
1433 unsigned long long size, char *name,
1434 char *homehost, int *uuid)
1435 {
1436 /* We are creating a volume inside a pre-existing container.
1437 * so st->sb is already set.
1438 */
1439 struct intel_super *super = st->sb;
1440 struct imsm_super *mpb = super->anchor;
1441 struct imsm_dev *dev;
1442 struct imsm_vol *vol;
1443 struct imsm_map *map;
1444 int idx = mpb->num_raid_devs;
1445 int i;
1446 unsigned long long array_blocks;
1447 __u32 offset = 0;
1448 size_t size_old, size_new;
1449
1450 if (mpb->num_raid_devs >= 2) {
1451 fprintf(stderr, Name": This imsm-container already has the "
1452 "maximum of 2 volumes\n");
1453 return 0;
1454 }
1455
1456 /* ensure the mpb is large enough for the new data */
1457 size_old = __le32_to_cpu(mpb->mpb_size);
1458 size_new = disks_to_mpb_size(info->nr_disks);
1459 if (size_new > size_old) {
1460 void *mpb_new;
1461 size_t size_round = ROUND_UP(size_new, 512);
1462
1463 if (posix_memalign(&mpb_new, 512, size_round) != 0) {
1464 fprintf(stderr, Name": could not allocate new mpb\n");
1465 return 0;
1466 }
1467 memcpy(mpb_new, mpb, size_old);
1468 free(mpb);
1469 mpb = mpb_new;
1470 super->anchor = mpb_new;
1471 mpb->mpb_size = __cpu_to_le32(size_new);
1472 memset(mpb_new + size_old, 0, size_round - size_old);
1473 }
1474 super->current_vol = idx;
1475 /* when creating the first raid device in this container set num_disks
1476 * to zero, i.e. delete this spare and add raid member devices in
1477 * add_to_super_imsm_volume()
1478 */
1479 if (super->current_vol == 0)
1480 mpb->num_disks = 0;
1481 sprintf(st->subarray, "%d", idx);
1482 dev = malloc(sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1));
1483 if (!dev) {
1484 fprintf(stderr, Name": could not allocate raid device\n");
1485 return 0;
1486 }
1487 strncpy((char *) dev->volume, name, MAX_RAID_SERIAL_LEN);
1488 array_blocks = calc_array_size(info->level, info->raid_disks,
1489 info->layout, info->chunk_size,
1490 info->size*2);
1491 dev->size_low = __cpu_to_le32((__u32) array_blocks);
1492 dev->size_high = __cpu_to_le32((__u32) (array_blocks >> 32));
1493 dev->status = __cpu_to_le32(0);
1494 dev->reserved_blocks = __cpu_to_le32(0);
1495 vol = &dev->vol;
1496 vol->migr_state = 0;
1497 vol->migr_type = 0;
1498 vol->dirty = 0;
1499 for (i = 0; i < idx; i++) {
1500 struct imsm_dev *prev = get_imsm_dev(super, i);
1501 struct imsm_map *pmap = get_imsm_map(prev, 0);
1502
1503 offset += __le32_to_cpu(pmap->blocks_per_member);
1504 offset += IMSM_RESERVED_SECTORS;
1505 }
1506 map = get_imsm_map(dev, 0);
1507 map->pba_of_lba0 = __cpu_to_le32(offset);
1508 map->blocks_per_member = __cpu_to_le32(info_to_blocks_per_member(info));
1509 map->blocks_per_strip = __cpu_to_le16(info_to_blocks_per_strip(info));
1510 map->num_data_stripes = __cpu_to_le32(info_to_num_data_stripes(info));
1511 map->map_state = info->level ? IMSM_T_STATE_UNINITIALIZED :
1512 IMSM_T_STATE_NORMAL;
1513
1514 if (info->level == 1 && info->raid_disks > 2) {
1515 fprintf(stderr, Name": imsm does not support more than 2 disks"
1516 "in a raid1 volume\n");
1517 return 0;
1518 }
1519 if (info->level == 10)
1520 map->raid_level = 1;
1521 else
1522 map->raid_level = info->level;
1523
1524 map->num_members = info->raid_disks;
1525 for (i = 0; i < map->num_members; i++) {
1526 /* initialized in add_to_super */
1527 set_imsm_ord_tbl_ent(map, i, 0);
1528 }
1529 mpb->num_raid_devs++;
1530 super->dev_tbl[super->current_vol] = dev;
1531
1532 return 1;
1533 }
1534
1535 static int init_super_imsm(struct supertype *st, mdu_array_info_t *info,
1536 unsigned long long size, char *name,
1537 char *homehost, int *uuid)
1538 {
1539 /* This is primarily called by Create when creating a new array.
1540 * We will then get add_to_super called for each component, and then
1541 * write_init_super called to write it out to each device.
1542 * For IMSM, Create can create on fresh devices or on a pre-existing
1543 * array.
1544 * To create on a pre-existing array a different method will be called.
1545 * This one is just for fresh drives.
1546 */
1547 struct intel_super *super;
1548 struct imsm_super *mpb;
1549 size_t mpb_size;
1550
1551 if (!info) {
1552 st->sb = NULL;
1553 return 0;
1554 }
1555 if (st->sb)
1556 return init_super_imsm_volume(st, info, size, name, homehost,
1557 uuid);
1558
1559 super = alloc_super(1);
1560 if (!super)
1561 return 0;
1562 mpb_size = disks_to_mpb_size(info->nr_disks);
1563 if (posix_memalign(&super->buf, 512, mpb_size) != 0) {
1564 free(super);
1565 return 0;
1566 }
1567 mpb = super->buf;
1568 memset(mpb, 0, mpb_size);
1569
1570 memcpy(mpb->sig, MPB_SIGNATURE, strlen(MPB_SIGNATURE));
1571 memcpy(mpb->sig + strlen(MPB_SIGNATURE), MPB_VERSION_RAID5,
1572 strlen(MPB_VERSION_RAID5));
1573 mpb->mpb_size = mpb_size;
1574
1575 st->sb = super;
1576 return 1;
1577 }
1578
1579 #ifndef MDASSEMBLE
1580 static void add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk,
1581 int fd, char *devname)
1582 {
1583 struct intel_super *super = st->sb;
1584 struct imsm_super *mpb = super->anchor;
1585 struct dl *dl;
1586 struct imsm_dev *dev;
1587 struct imsm_map *map;
1588 __u32 status;
1589
1590 dev = get_imsm_dev(super, super->current_vol);
1591 map = get_imsm_map(dev, 0);
1592
1593 for (dl = super->disks; dl ; dl = dl->next)
1594 if (dl->major == dk->major &&
1595 dl->minor == dk->minor)
1596 break;
1597
1598 if (!dl || ! (dk->state & (1<<MD_DISK_SYNC)))
1599 return;
1600
1601 /* add a pristine spare to the metadata */
1602 if (dl->index < 0) {
1603 dl->index = super->anchor->num_disks;
1604 super->anchor->num_disks++;
1605 }
1606 set_imsm_ord_tbl_ent(map, dk->number, dl->index);
1607 status = CONFIGURED_DISK | USABLE_DISK;
1608 dl->disk.status = __cpu_to_le32(status);
1609
1610 /* if we are creating the first raid device update the family number */
1611 if (super->current_vol == 0) {
1612 __u32 sum;
1613 struct imsm_dev *_dev = __get_imsm_dev(mpb, 0);
1614 struct imsm_disk *_disk = __get_imsm_disk(mpb, dl->index);
1615
1616 *_dev = *dev;
1617 *_disk = dl->disk;
1618 sum = __gen_imsm_checksum(mpb);
1619 mpb->family_num = __cpu_to_le32(sum);
1620 }
1621 }
1622
1623 static void add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
1624 int fd, char *devname)
1625 {
1626 struct intel_super *super = st->sb;
1627 struct dl *dd;
1628 unsigned long long size;
1629 __u32 status, id;
1630 int rv;
1631 struct stat stb;
1632
1633 if (super->current_vol >= 0) {
1634 add_to_super_imsm_volume(st, dk, fd, devname);
1635 return;
1636 }
1637
1638 fstat(fd, &stb);
1639 dd = malloc(sizeof(*dd));
1640 if (!dd) {
1641 fprintf(stderr,
1642 Name ": malloc failed %s:%d.\n", __func__, __LINE__);
1643 abort();
1644 }
1645 memset(dd, 0, sizeof(*dd));
1646 dd->major = major(stb.st_rdev);
1647 dd->minor = minor(stb.st_rdev);
1648 dd->index = -1;
1649 dd->devname = devname ? strdup(devname) : NULL;
1650 dd->fd = fd;
1651 rv = imsm_read_serial(fd, devname, dd->serial);
1652 if (rv) {
1653 fprintf(stderr,
1654 Name ": failed to retrieve scsi serial, aborting\n");
1655 free(dd);
1656 abort();
1657 }
1658
1659 get_dev_size(fd, NULL, &size);
1660 size /= 512;
1661 status = USABLE_DISK | SPARE_DISK;
1662 serialcpy(dd->disk.serial, dd->serial);
1663 dd->disk.total_blocks = __cpu_to_le32(size);
1664 dd->disk.status = __cpu_to_le32(status);
1665 if (sysfs_disk_to_scsi_id(fd, &id) == 0)
1666 dd->disk.scsi_id = __cpu_to_le32(id);
1667 else
1668 dd->disk.scsi_id = __cpu_to_le32(0);
1669
1670 if (st->update_tail) {
1671 dd->next = super->add;
1672 super->add = dd;
1673 } else {
1674 dd->next = super->disks;
1675 super->disks = dd;
1676 }
1677 }
1678
1679 static int store_imsm_mpb(int fd, struct intel_super *super);
1680
1681 /* spare records have their own family number and do not have any defined raid
1682 * devices
1683 */
1684 static int write_super_imsm_spares(struct intel_super *super, int doclose)
1685 {
1686 struct imsm_super mpb_save;
1687 struct imsm_super *mpb = super->anchor;
1688 __u32 sum;
1689 struct dl *d;
1690
1691 mpb_save = *mpb;
1692 mpb->num_raid_devs = 0;
1693 mpb->num_disks = 1;
1694 mpb->mpb_size = sizeof(struct imsm_super);
1695 mpb->generation_num = __cpu_to_le32(1UL);
1696
1697 for (d = super->disks; d; d = d->next) {
1698 if (d->index != -1)
1699 continue;
1700
1701 mpb->disk[0] = d->disk;
1702 sum = __gen_imsm_checksum(mpb);
1703 mpb->family_num = __cpu_to_le32(sum);
1704 sum = __gen_imsm_checksum(mpb);
1705 mpb->check_sum = __cpu_to_le32(sum);
1706
1707 if (store_imsm_mpb(d->fd, super)) {
1708 fprintf(stderr, "%s: failed for device %d:%d %s\n",
1709 __func__, d->major, d->minor, strerror(errno));
1710 *mpb = mpb_save;
1711 return 1;
1712 }
1713 if (doclose) {
1714 close(d->fd);
1715 d->fd = -1;
1716 }
1717 }
1718
1719 *mpb = mpb_save;
1720 return 0;
1721 }
1722
1723 static int write_super_imsm(struct intel_super *super, int doclose)
1724 {
1725 struct imsm_super *mpb = super->anchor;
1726 struct dl *d;
1727 __u32 generation;
1728 __u32 sum;
1729 int spares = 0;
1730 int i;
1731 __u32 mpb_size = sizeof(struct imsm_super) - sizeof(struct imsm_disk);
1732
1733 /* 'generation' is incremented everytime the metadata is written */
1734 generation = __le32_to_cpu(mpb->generation_num);
1735 generation++;
1736 mpb->generation_num = __cpu_to_le32(generation);
1737
1738 for (d = super->disks; d; d = d->next) {
1739 if (d->index == -1)
1740 spares++;
1741 else {
1742 mpb->disk[d->index] = d->disk;
1743 mpb_size += sizeof(struct imsm_disk);
1744 }
1745 }
1746
1747 for (i = 0; i < mpb->num_raid_devs; i++) {
1748 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
1749
1750 imsm_copy_dev(dev, super->dev_tbl[i]);
1751 mpb_size += sizeof_imsm_dev(dev, 0);
1752 }
1753 mpb_size += __le32_to_cpu(mpb->bbm_log_size);
1754 mpb->mpb_size = __cpu_to_le32(mpb_size);
1755
1756 /* recalculate checksum */
1757 sum = __gen_imsm_checksum(mpb);
1758 mpb->check_sum = __cpu_to_le32(sum);
1759
1760 /* write the mpb for disks that compose raid devices */
1761 for (d = super->disks; d ; d = d->next) {
1762 if (d->index < 0)
1763 continue;
1764 if (store_imsm_mpb(d->fd, super))
1765 fprintf(stderr, "%s: failed for device %d:%d %s\n",
1766 __func__, d->major, d->minor, strerror(errno));
1767 if (doclose) {
1768 close(d->fd);
1769 d->fd = -1;
1770 }
1771 }
1772
1773 if (spares)
1774 return write_super_imsm_spares(super, doclose);
1775
1776 return 0;
1777 }
1778
1779
1780 static int create_array(struct supertype *st)
1781 {
1782 size_t len;
1783 struct imsm_update_create_array *u;
1784 struct intel_super *super = st->sb;
1785 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
1786
1787 len = sizeof(*u) - sizeof(*dev) + sizeof_imsm_dev(dev, 0);
1788 u = malloc(len);
1789 if (!u) {
1790 fprintf(stderr, "%s: failed to allocate update buffer\n",
1791 __func__);
1792 return 1;
1793 }
1794
1795 u->type = update_create_array;
1796 u->dev_idx = super->current_vol;
1797 imsm_copy_dev(&u->dev, dev);
1798 append_metadata_update(st, u, len);
1799
1800 return 0;
1801 }
1802
1803 static int _add_disk(struct supertype *st)
1804 {
1805 struct intel_super *super = st->sb;
1806 size_t len;
1807 struct imsm_update_add_disk *u;
1808
1809 if (!super->add)
1810 return 0;
1811
1812 len = sizeof(*u);
1813 u = malloc(len);
1814 if (!u) {
1815 fprintf(stderr, "%s: failed to allocate update buffer\n",
1816 __func__);
1817 return 1;
1818 }
1819
1820 u->type = update_add_disk;
1821 append_metadata_update(st, u, len);
1822
1823 return 0;
1824 }
1825
1826 static int write_init_super_imsm(struct supertype *st)
1827 {
1828 if (st->update_tail) {
1829 /* queue the recently created array / added disk
1830 * as a metadata update */
1831 struct intel_super *super = st->sb;
1832 struct dl *d;
1833 int rv;
1834
1835 /* determine if we are creating a volume or adding a disk */
1836 if (super->current_vol < 0) {
1837 /* in the add disk case we are running in mdmon
1838 * context, so don't close fd's
1839 */
1840 return _add_disk(st);
1841 } else
1842 rv = create_array(st);
1843
1844 for (d = super->disks; d ; d = d->next) {
1845 close(d->fd);
1846 d->fd = -1;
1847 }
1848
1849 return rv;
1850 } else
1851 return write_super_imsm(st->sb, 1);
1852 }
1853 #endif
1854
1855 static int store_zero_imsm(struct supertype *st, int fd)
1856 {
1857 unsigned long long dsize;
1858 void *buf;
1859
1860 get_dev_size(fd, NULL, &dsize);
1861
1862 /* first block is stored on second to last sector of the disk */
1863 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0)
1864 return 1;
1865
1866 if (posix_memalign(&buf, 512, 512) != 0)
1867 return 1;
1868
1869 memset(buf, 0, 512);
1870 if (write(fd, buf, 512) != 512)
1871 return 1;
1872 return 0;
1873 }
1874
1875 static int imsm_bbm_log_size(struct imsm_super *mpb)
1876 {
1877 return __le32_to_cpu(mpb->bbm_log_size);
1878 }
1879
1880 #ifndef MDASSEMBLE
1881 static int validate_geometry_imsm_container(struct supertype *st, int level,
1882 int layout, int raiddisks, int chunk,
1883 unsigned long long size, char *dev,
1884 unsigned long long *freesize,
1885 int verbose)
1886 {
1887 int fd;
1888 unsigned long long ldsize;
1889
1890 if (level != LEVEL_CONTAINER)
1891 return 0;
1892 if (!dev)
1893 return 1;
1894
1895 fd = open(dev, O_RDONLY|O_EXCL, 0);
1896 if (fd < 0) {
1897 if (verbose)
1898 fprintf(stderr, Name ": imsm: Cannot open %s: %s\n",
1899 dev, strerror(errno));
1900 return 0;
1901 }
1902 if (!get_dev_size(fd, dev, &ldsize)) {
1903 close(fd);
1904 return 0;
1905 }
1906 close(fd);
1907
1908 *freesize = avail_size_imsm(st, ldsize >> 9);
1909
1910 return 1;
1911 }
1912
1913 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
1914 * FIX ME add ahci details
1915 */
1916 static int validate_geometry_imsm_volume(struct supertype *st, int level,
1917 int layout, int raiddisks, int chunk,
1918 unsigned long long size, char *dev,
1919 unsigned long long *freesize,
1920 int verbose)
1921 {
1922 struct stat stb;
1923 struct intel_super *super = st->sb;
1924 struct dl *dl;
1925 unsigned long long pos = 0;
1926 unsigned long long maxsize;
1927 struct extent *e;
1928 int i;
1929
1930 if (level == LEVEL_CONTAINER)
1931 return 0;
1932
1933 if (level == 1 && raiddisks > 2) {
1934 if (verbose)
1935 fprintf(stderr, Name ": imsm does not support more "
1936 "than 2 in a raid1 configuration\n");
1937 return 0;
1938 }
1939
1940 /* We must have the container info already read in. */
1941 if (!super)
1942 return 0;
1943
1944 if (!dev) {
1945 /* General test: make sure there is space for
1946 * 'raiddisks' device extents of size 'size' at a given
1947 * offset
1948 */
1949 unsigned long long minsize = size*2 /* convert to blocks */;
1950 unsigned long long start_offset = ~0ULL;
1951 int dcnt = 0;
1952 if (minsize == 0)
1953 minsize = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
1954 for (dl = super->disks; dl ; dl = dl->next) {
1955 int found = 0;
1956
1957 pos = 0;
1958 i = 0;
1959 e = get_extents(super, dl);
1960 if (!e) continue;
1961 do {
1962 unsigned long long esize;
1963 esize = e[i].start - pos;
1964 if (esize >= minsize)
1965 found = 1;
1966 if (found && start_offset == ~0ULL) {
1967 start_offset = pos;
1968 break;
1969 } else if (found && pos != start_offset) {
1970 found = 0;
1971 break;
1972 }
1973 pos = e[i].start + e[i].size;
1974 i++;
1975 } while (e[i-1].size);
1976 if (found)
1977 dcnt++;
1978 free(e);
1979 }
1980 if (dcnt < raiddisks) {
1981 if (verbose)
1982 fprintf(stderr, Name ": imsm: Not enough "
1983 "devices with space for this array "
1984 "(%d < %d)\n",
1985 dcnt, raiddisks);
1986 return 0;
1987 }
1988 return 1;
1989 }
1990 /* This device must be a member of the set */
1991 if (stat(dev, &stb) < 0)
1992 return 0;
1993 if ((S_IFMT & stb.st_mode) != S_IFBLK)
1994 return 0;
1995 for (dl = super->disks ; dl ; dl = dl->next) {
1996 if (dl->major == major(stb.st_rdev) &&
1997 dl->minor == minor(stb.st_rdev))
1998 break;
1999 }
2000 if (!dl) {
2001 if (verbose)
2002 fprintf(stderr, Name ": %s is not in the "
2003 "same imsm set\n", dev);
2004 return 0;
2005 }
2006 e = get_extents(super, dl);
2007 maxsize = 0;
2008 i = 0;
2009 if (e) do {
2010 unsigned long long esize;
2011 esize = e[i].start - pos;
2012 if (esize >= maxsize)
2013 maxsize = esize;
2014 pos = e[i].start + e[i].size;
2015 i++;
2016 } while (e[i-1].size);
2017 *freesize = maxsize;
2018
2019 return 1;
2020 }
2021
2022 static int validate_geometry_imsm(struct supertype *st, int level, int layout,
2023 int raiddisks, int chunk, unsigned long long size,
2024 char *dev, unsigned long long *freesize,
2025 int verbose)
2026 {
2027 int fd, cfd;
2028 struct mdinfo *sra;
2029
2030 /* if given unused devices create a container
2031 * if given given devices in a container create a member volume
2032 */
2033 if (level == LEVEL_CONTAINER) {
2034 /* Must be a fresh device to add to a container */
2035 return validate_geometry_imsm_container(st, level, layout,
2036 raiddisks, chunk, size,
2037 dev, freesize,
2038 verbose);
2039 }
2040
2041 if (st->sb) {
2042 /* creating in a given container */
2043 return validate_geometry_imsm_volume(st, level, layout,
2044 raiddisks, chunk, size,
2045 dev, freesize, verbose);
2046 }
2047
2048 /* limit creation to the following levels */
2049 if (!dev)
2050 switch (level) {
2051 case 0:
2052 case 1:
2053 case 10:
2054 case 5:
2055 break;
2056 default:
2057 return 1;
2058 }
2059
2060 /* This device needs to be a device in an 'imsm' container */
2061 fd = open(dev, O_RDONLY|O_EXCL, 0);
2062 if (fd >= 0) {
2063 if (verbose)
2064 fprintf(stderr,
2065 Name ": Cannot create this array on device %s\n",
2066 dev);
2067 close(fd);
2068 return 0;
2069 }
2070 if (errno != EBUSY || (fd = open(dev, O_RDONLY, 0)) < 0) {
2071 if (verbose)
2072 fprintf(stderr, Name ": Cannot open %s: %s\n",
2073 dev, strerror(errno));
2074 return 0;
2075 }
2076 /* Well, it is in use by someone, maybe an 'imsm' container. */
2077 cfd = open_container(fd);
2078 if (cfd < 0) {
2079 close(fd);
2080 if (verbose)
2081 fprintf(stderr, Name ": Cannot use %s: It is busy\n",
2082 dev);
2083 return 0;
2084 }
2085 sra = sysfs_read(cfd, 0, GET_VERSION);
2086 close(fd);
2087 if (sra && sra->array.major_version == -1 &&
2088 strcmp(sra->text_version, "imsm") == 0) {
2089 /* This is a member of a imsm container. Load the container
2090 * and try to create a volume
2091 */
2092 struct intel_super *super;
2093
2094 if (load_super_imsm_all(st, cfd, (void **) &super, NULL, 1) == 0) {
2095 st->sb = super;
2096 st->container_dev = fd2devnum(cfd);
2097 close(cfd);
2098 return validate_geometry_imsm_volume(st, level, layout,
2099 raiddisks, chunk,
2100 size, dev,
2101 freesize, verbose);
2102 }
2103 close(cfd);
2104 } else /* may belong to another container */
2105 return 0;
2106
2107 return 1;
2108 }
2109 #endif /* MDASSEMBLE */
2110
2111 static struct mdinfo *container_content_imsm(struct supertype *st)
2112 {
2113 /* Given a container loaded by load_super_imsm_all,
2114 * extract information about all the arrays into
2115 * an mdinfo tree.
2116 *
2117 * For each imsm_dev create an mdinfo, fill it in,
2118 * then look for matching devices in super->disks
2119 * and create appropriate device mdinfo.
2120 */
2121 struct intel_super *super = st->sb;
2122 struct imsm_super *mpb = super->anchor;
2123 struct mdinfo *rest = NULL;
2124 int i;
2125
2126 /* do not assemble arrays that might have bad blocks */
2127 if (imsm_bbm_log_size(super->anchor)) {
2128 fprintf(stderr, Name ": BBM log found in metadata. "
2129 "Cannot activate array(s).\n");
2130 return NULL;
2131 }
2132
2133 for (i = 0; i < mpb->num_raid_devs; i++) {
2134 struct imsm_dev *dev = get_imsm_dev(super, i);
2135 struct imsm_map *map = get_imsm_map(dev, 0);
2136 struct mdinfo *this;
2137 int slot;
2138
2139 this = malloc(sizeof(*this));
2140 memset(this, 0, sizeof(*this));
2141 this->next = rest;
2142
2143 super->current_vol = i;
2144 getinfo_super_imsm_volume(st, this);
2145 for (slot = 0 ; slot < map->num_members; slot++) {
2146 struct mdinfo *info_d;
2147 struct dl *d;
2148 int idx;
2149 int skip;
2150 __u32 s;
2151 __u32 ord;
2152
2153 skip = 0;
2154 idx = get_imsm_disk_idx(dev, slot);
2155 ord = get_imsm_ord_tbl_ent(dev, slot);
2156 for (d = super->disks; d ; d = d->next)
2157 if (d->index == idx)
2158 break;
2159
2160 if (d == NULL)
2161 skip = 1;
2162
2163 s = d ? __le32_to_cpu(d->disk.status) : 0;
2164 if (s & FAILED_DISK)
2165 skip = 1;
2166 if (!(s & USABLE_DISK))
2167 skip = 1;
2168 if (ord & IMSM_ORD_REBUILD)
2169 skip = 1;
2170
2171 /*
2172 * if we skip some disks the array will be assmebled degraded;
2173 * reset resync start to avoid a dirty-degraded situation
2174 *
2175 * FIXME handle dirty degraded
2176 */
2177 if (skip && !dev->vol.dirty)
2178 this->resync_start = ~0ULL;
2179 if (skip)
2180 continue;
2181
2182 info_d = malloc(sizeof(*info_d));
2183 if (!info_d) {
2184 fprintf(stderr, Name ": failed to allocate disk"
2185 " for volume %s\n", (char *) dev->volume);
2186 free(this);
2187 this = rest;
2188 break;
2189 }
2190 memset(info_d, 0, sizeof(*info_d));
2191 info_d->next = this->devs;
2192 this->devs = info_d;
2193
2194 info_d->disk.number = d->index;
2195 info_d->disk.major = d->major;
2196 info_d->disk.minor = d->minor;
2197 info_d->disk.raid_disk = slot;
2198
2199 this->array.working_disks++;
2200
2201 info_d->events = __le32_to_cpu(mpb->generation_num);
2202 info_d->data_offset = __le32_to_cpu(map->pba_of_lba0);
2203 info_d->component_size = __le32_to_cpu(map->blocks_per_member);
2204 if (d->devname)
2205 strcpy(info_d->name, d->devname);
2206 }
2207 rest = this;
2208 }
2209
2210 return rest;
2211 }
2212
2213
2214 #ifndef MDASSEMBLE
2215 static int imsm_open_new(struct supertype *c, struct active_array *a,
2216 char *inst)
2217 {
2218 struct intel_super *super = c->sb;
2219 struct imsm_super *mpb = super->anchor;
2220
2221 if (atoi(inst) >= mpb->num_raid_devs) {
2222 fprintf(stderr, "%s: subarry index %d, out of range\n",
2223 __func__, atoi(inst));
2224 return -ENODEV;
2225 }
2226
2227 dprintf("imsm: open_new %s\n", inst);
2228 a->info.container_member = atoi(inst);
2229 return 0;
2230 }
2231
2232 static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev, int failed)
2233 {
2234 struct imsm_map *map = get_imsm_map(dev, 0);
2235
2236 if (!failed)
2237 return map->map_state == IMSM_T_STATE_UNINITIALIZED ?
2238 IMSM_T_STATE_UNINITIALIZED : IMSM_T_STATE_NORMAL;
2239
2240 switch (get_imsm_raid_level(map)) {
2241 case 0:
2242 return IMSM_T_STATE_FAILED;
2243 break;
2244 case 1:
2245 if (failed < map->num_members)
2246 return IMSM_T_STATE_DEGRADED;
2247 else
2248 return IMSM_T_STATE_FAILED;
2249 break;
2250 case 10:
2251 {
2252 /**
2253 * check to see if any mirrors have failed,
2254 * otherwise we are degraded
2255 */
2256 int device_per_mirror = 2; /* FIXME is this always the case?
2257 * and are they always adjacent?
2258 */
2259 int r10fail = 0;
2260 int i;
2261
2262 for (i = 0; i < map->num_members; i++) {
2263 int idx = get_imsm_disk_idx(dev, i);
2264 struct imsm_disk *disk = get_imsm_disk(super, idx);
2265
2266 if (!disk)
2267 r10fail++;
2268 else if (__le32_to_cpu(disk->status) & FAILED_DISK)
2269 r10fail++;
2270
2271 if (r10fail >= device_per_mirror)
2272 return IMSM_T_STATE_FAILED;
2273
2274 /* reset 'r10fail' for next mirror set */
2275 if (!((i + 1) % device_per_mirror))
2276 r10fail = 0;
2277 }
2278
2279 return IMSM_T_STATE_DEGRADED;
2280 }
2281 case 5:
2282 if (failed < 2)
2283 return IMSM_T_STATE_DEGRADED;
2284 else
2285 return IMSM_T_STATE_FAILED;
2286 break;
2287 default:
2288 break;
2289 }
2290
2291 return map->map_state;
2292 }
2293
2294 static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev)
2295 {
2296 int i;
2297 int failed = 0;
2298 struct imsm_disk *disk;
2299 struct imsm_map *map = get_imsm_map(dev, 0);
2300
2301 for (i = 0; i < map->num_members; i++) {
2302 __u32 ord = get_imsm_ord_tbl_ent(dev, i);
2303 int idx = ord_to_idx(ord);
2304
2305 disk = get_imsm_disk(super, idx);
2306 if (!disk ||
2307 __le32_to_cpu(disk->status) & FAILED_DISK ||
2308 ord & IMSM_ORD_REBUILD)
2309 failed++;
2310 }
2311
2312 return failed;
2313 }
2314
2315 static int is_resyncing(struct imsm_dev *dev)
2316 {
2317 struct imsm_map *migr_map;
2318
2319 if (!dev->vol.migr_state)
2320 return 0;
2321
2322 if (dev->vol.migr_type == 0)
2323 return 1;
2324
2325 migr_map = get_imsm_map(dev, 1);
2326
2327 if (migr_map->map_state == IMSM_T_STATE_NORMAL)
2328 return 1;
2329 else
2330 return 0;
2331 }
2332
2333 static int is_rebuilding(struct imsm_dev *dev)
2334 {
2335 struct imsm_map *migr_map;
2336
2337 if (!dev->vol.migr_state)
2338 return 0;
2339
2340 if (dev->vol.migr_type == 0)
2341 return 0;
2342
2343 migr_map = get_imsm_map(dev, 1);
2344
2345 if (migr_map->map_state == IMSM_T_STATE_DEGRADED)
2346 return 1;
2347 else
2348 return 0;
2349 }
2350
2351 /* Handle dirty -> clean transititions and resync. Degraded and rebuild
2352 * states are handled in imsm_set_disk() with one exception, when a
2353 * resync is stopped due to a new failure this routine will set the
2354 * 'degraded' state for the array.
2355 */
2356 static int imsm_set_array_state(struct active_array *a, int consistent)
2357 {
2358 int inst = a->info.container_member;
2359 struct intel_super *super = a->container->sb;
2360 struct imsm_dev *dev = get_imsm_dev(super, inst);
2361 struct imsm_map *map = get_imsm_map(dev, 0);
2362 int failed = imsm_count_failed(super, dev);
2363 __u8 map_state = imsm_check_degraded(super, dev, failed);
2364
2365 if (consistent == 2 &&
2366 (a->resync_start != ~0ULL ||
2367 map_state != IMSM_T_STATE_NORMAL ||
2368 dev->vol.migr_state))
2369 consistent = 0;
2370
2371 if (a->resync_start == ~0ULL) {
2372 /* complete intialization / resync,
2373 * recovery is completed in ->set_disk
2374 */
2375 if (is_resyncing(dev)) {
2376 dprintf("imsm: mark resync done\n");
2377 dev->vol.migr_state = 0;
2378 map->map_state = map_state;
2379 super->updates_pending++;
2380 }
2381 } else if (!is_resyncing(dev) && !failed) {
2382 /* mark the start of the init process if nothing is failed */
2383 dprintf("imsm: mark resync start (%llu)\n", a->resync_start);
2384 map->map_state = map_state;
2385 migrate(dev, IMSM_T_STATE_NORMAL,
2386 map->map_state == IMSM_T_STATE_NORMAL);
2387 super->updates_pending++;
2388 }
2389
2390 /* mark dirty / clean */
2391 if (dev->vol.dirty != !consistent) {
2392 dprintf("imsm: mark '%s' (%llu)\n",
2393 consistent ? "clean" : "dirty", a->resync_start);
2394 if (consistent)
2395 dev->vol.dirty = 0;
2396 else
2397 dev->vol.dirty = 1;
2398 super->updates_pending++;
2399 }
2400 return consistent;
2401 }
2402
2403 static void imsm_set_disk(struct active_array *a, int n, int state)
2404 {
2405 int inst = a->info.container_member;
2406 struct intel_super *super = a->container->sb;
2407 struct imsm_dev *dev = get_imsm_dev(super, inst);
2408 struct imsm_map *map = get_imsm_map(dev, 0);
2409 struct imsm_disk *disk;
2410 int failed;
2411 __u32 status;
2412 __u32 ord;
2413 __u8 map_state;
2414
2415 if (n > map->num_members)
2416 fprintf(stderr, "imsm: set_disk %d out of range 0..%d\n",
2417 n, map->num_members - 1);
2418
2419 if (n < 0)
2420 return;
2421
2422 dprintf("imsm: set_disk %d:%x\n", n, state);
2423
2424 ord = get_imsm_ord_tbl_ent(dev, n);
2425 disk = get_imsm_disk(super, ord_to_idx(ord));
2426
2427 /* check for new failures */
2428 status = __le32_to_cpu(disk->status);
2429 if ((state & DS_FAULTY) && !(status & FAILED_DISK)) {
2430 status |= FAILED_DISK;
2431 disk->status = __cpu_to_le32(status);
2432 disk->scsi_id = __cpu_to_le32(~(__u32)0);
2433 memmove(&disk->serial[0], &disk->serial[1], MAX_RAID_SERIAL_LEN - 1);
2434 super->updates_pending++;
2435 }
2436 /* check if in_sync */
2437 if (state & DS_INSYNC && ord & IMSM_ORD_REBUILD) {
2438 struct imsm_map *migr_map = get_imsm_map(dev, 1);
2439
2440 set_imsm_ord_tbl_ent(migr_map, n, ord_to_idx(ord));
2441 super->updates_pending++;
2442 }
2443
2444 failed = imsm_count_failed(super, dev);
2445 map_state = imsm_check_degraded(super, dev, failed);
2446
2447 /* check if recovery complete, newly degraded, or failed */
2448 if (map_state == IMSM_T_STATE_NORMAL && is_rebuilding(dev)) {
2449 map->map_state = map_state;
2450 dev->vol.migr_state = 0;
2451 super->updates_pending++;
2452 } else if (map_state == IMSM_T_STATE_DEGRADED &&
2453 map->map_state != map_state &&
2454 !dev->vol.migr_state) {
2455 dprintf("imsm: mark degraded\n");
2456 map->map_state = map_state;
2457 super->updates_pending++;
2458 } else if (map_state == IMSM_T_STATE_FAILED &&
2459 map->map_state != map_state) {
2460 dprintf("imsm: mark failed\n");
2461 dev->vol.migr_state = 0;
2462 map->map_state = map_state;
2463 super->updates_pending++;
2464 }
2465 }
2466
2467 static int store_imsm_mpb(int fd, struct intel_super *super)
2468 {
2469 struct imsm_super *mpb = super->anchor;
2470 __u32 mpb_size = __le32_to_cpu(mpb->mpb_size);
2471 unsigned long long dsize;
2472 unsigned long long sectors;
2473
2474 get_dev_size(fd, NULL, &dsize);
2475
2476 if (mpb_size > 512) {
2477 /* -1 to account for anchor */
2478 sectors = mpb_sectors(mpb) - 1;
2479
2480 /* write the extended mpb to the sectors preceeding the anchor */
2481 if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0)
2482 return 1;
2483
2484 if (write(fd, super->buf + 512, 512 * sectors) != 512 * sectors)
2485 return 1;
2486 }
2487
2488 /* first block is stored on second to last sector of the disk */
2489 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0)
2490 return 1;
2491
2492 if (write(fd, super->buf, 512) != 512)
2493 return 1;
2494
2495 return 0;
2496 }
2497
2498 static void imsm_sync_metadata(struct supertype *container)
2499 {
2500 struct intel_super *super = container->sb;
2501
2502 if (!super->updates_pending)
2503 return;
2504
2505 write_super_imsm(super, 0);
2506
2507 super->updates_pending = 0;
2508 }
2509
2510 static struct dl *imsm_readd(struct intel_super *super, int idx, struct active_array *a)
2511 {
2512 struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
2513 int i = get_imsm_disk_idx(dev, idx);
2514 struct dl *dl;
2515
2516 for (dl = super->disks; dl; dl = dl->next)
2517 if (dl->index == i)
2518 break;
2519
2520 if (dl && __le32_to_cpu(dl->disk.status) & FAILED_DISK)
2521 dl = NULL;
2522
2523 if (dl)
2524 dprintf("%s: found %x:%x\n", __func__, dl->major, dl->minor);
2525
2526 return dl;
2527 }
2528
2529 static struct dl *imsm_add_spare(struct intel_super *super, int slot, struct active_array *a)
2530 {
2531 struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
2532 int idx = get_imsm_disk_idx(dev, slot);
2533 struct imsm_map *map = get_imsm_map(dev, 0);
2534 unsigned long long esize;
2535 unsigned long long pos;
2536 struct mdinfo *d;
2537 struct extent *ex;
2538 int j;
2539 int found;
2540 __u32 array_start;
2541 __u32 status;
2542 struct dl *dl;
2543
2544 for (dl = super->disks; dl; dl = dl->next) {
2545 /* If in this array, skip */
2546 for (d = a->info.devs ; d ; d = d->next)
2547 if (d->state_fd >= 0 &&
2548 d->disk.major == dl->major &&
2549 d->disk.minor == dl->minor) {
2550 dprintf("%x:%x already in array\n", dl->major, dl->minor);
2551 break;
2552 }
2553 if (d)
2554 continue;
2555
2556 /* skip in use or failed drives */
2557 status = __le32_to_cpu(dl->disk.status);
2558 if (status & FAILED_DISK || idx == dl->index) {
2559 dprintf("%x:%x status ( %s%s)\n",
2560 dl->major, dl->minor,
2561 status & FAILED_DISK ? "failed " : "",
2562 idx == dl->index ? "in use " : "");
2563 continue;
2564 }
2565
2566 /* Does this unused device have the requisite free space?
2567 * We need a->info.component_size sectors
2568 */
2569 ex = get_extents(super, dl);
2570 if (!ex) {
2571 dprintf("cannot get extents\n");
2572 continue;
2573 }
2574 found = 0;
2575 j = 0;
2576 pos = 0;
2577 array_start = __le32_to_cpu(map->pba_of_lba0);
2578
2579 do {
2580 /* check that we can start at pba_of_lba0 with
2581 * a->info.component_size of space
2582 */
2583 esize = ex[j].start - pos;
2584 if (array_start >= pos &&
2585 array_start + a->info.component_size < ex[j].start) {
2586 found = 1;
2587 break;
2588 }
2589 pos = ex[j].start + ex[j].size;
2590 j++;
2591
2592 } while (ex[j-1].size);
2593
2594 free(ex);
2595 if (!found) {
2596 dprintf("%x:%x does not have %llu at %d\n",
2597 dl->major, dl->minor,
2598 a->info.component_size,
2599 __le32_to_cpu(map->pba_of_lba0));
2600 /* No room */
2601 continue;
2602 } else
2603 break;
2604 }
2605
2606 return dl;
2607 }
2608
2609 static struct mdinfo *imsm_activate_spare(struct active_array *a,
2610 struct metadata_update **updates)
2611 {
2612 /**
2613 * Find a device with unused free space and use it to replace a
2614 * failed/vacant region in an array. We replace failed regions one a
2615 * array at a time. The result is that a new spare disk will be added
2616 * to the first failed array and after the monitor has finished
2617 * propagating failures the remainder will be consumed.
2618 *
2619 * FIXME add a capability for mdmon to request spares from another
2620 * container.
2621 */
2622
2623 struct intel_super *super = a->container->sb;
2624 int inst = a->info.container_member;
2625 struct imsm_dev *dev = get_imsm_dev(super, inst);
2626 struct imsm_map *map = get_imsm_map(dev, 0);
2627 int failed = a->info.array.raid_disks;
2628 struct mdinfo *rv = NULL;
2629 struct mdinfo *d;
2630 struct mdinfo *di;
2631 struct metadata_update *mu;
2632 struct dl *dl;
2633 struct imsm_update_activate_spare *u;
2634 int num_spares = 0;
2635 int i;
2636
2637 for (d = a->info.devs ; d ; d = d->next) {
2638 if ((d->curr_state & DS_FAULTY) &&
2639 d->state_fd >= 0)
2640 /* wait for Removal to happen */
2641 return NULL;
2642 if (d->state_fd >= 0)
2643 failed--;
2644 }
2645
2646 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
2647 inst, failed, a->info.array.raid_disks, a->info.array.level);
2648 if (imsm_check_degraded(super, dev, failed) != IMSM_T_STATE_DEGRADED)
2649 return NULL;
2650
2651 /* For each slot, if it is not working, find a spare */
2652 for (i = 0; i < a->info.array.raid_disks; i++) {
2653 for (d = a->info.devs ; d ; d = d->next)
2654 if (d->disk.raid_disk == i)
2655 break;
2656 dprintf("found %d: %p %x\n", i, d, d?d->curr_state:0);
2657 if (d && (d->state_fd >= 0))
2658 continue;
2659
2660 /*
2661 * OK, this device needs recovery. Try to re-add the previous
2662 * occupant of this slot, if this fails add a new spare
2663 */
2664 dl = imsm_readd(super, i, a);
2665 if (!dl)
2666 dl = imsm_add_spare(super, i, a);
2667 if (!dl)
2668 continue;
2669
2670 /* found a usable disk with enough space */
2671 di = malloc(sizeof(*di));
2672 memset(di, 0, sizeof(*di));
2673
2674 /* dl->index will be -1 in the case we are activating a
2675 * pristine spare. imsm_process_update() will create a
2676 * new index in this case. Once a disk is found to be
2677 * failed in all member arrays it is kicked from the
2678 * metadata
2679 */
2680 di->disk.number = dl->index;
2681
2682 /* (ab)use di->devs to store a pointer to the device
2683 * we chose
2684 */
2685 di->devs = (struct mdinfo *) dl;
2686
2687 di->disk.raid_disk = i;
2688 di->disk.major = dl->major;
2689 di->disk.minor = dl->minor;
2690 di->disk.state = 0;
2691 di->data_offset = __le32_to_cpu(map->pba_of_lba0);
2692 di->component_size = a->info.component_size;
2693 di->container_member = inst;
2694 di->next = rv;
2695 rv = di;
2696 num_spares++;
2697 dprintf("%x:%x to be %d at %llu\n", dl->major, dl->minor,
2698 i, di->data_offset);
2699
2700 break;
2701 }
2702
2703 if (!rv)
2704 /* No spares found */
2705 return rv;
2706 /* Now 'rv' has a list of devices to return.
2707 * Create a metadata_update record to update the
2708 * disk_ord_tbl for the array
2709 */
2710 mu = malloc(sizeof(*mu));
2711 mu->buf = malloc(sizeof(struct imsm_update_activate_spare) * num_spares);
2712 mu->space = NULL;
2713 mu->len = sizeof(struct imsm_update_activate_spare) * num_spares;
2714 mu->next = *updates;
2715 u = (struct imsm_update_activate_spare *) mu->buf;
2716
2717 for (di = rv ; di ; di = di->next) {
2718 u->type = update_activate_spare;
2719 u->dl = (struct dl *) di->devs;
2720 di->devs = NULL;
2721 u->slot = di->disk.raid_disk;
2722 u->array = inst;
2723 u->next = u + 1;
2724 u++;
2725 }
2726 (u-1)->next = NULL;
2727 *updates = mu;
2728
2729 return rv;
2730 }
2731
2732 static int disks_overlap(struct imsm_dev *d1, struct imsm_dev *d2)
2733 {
2734 struct imsm_map *m1 = get_imsm_map(d1, 0);
2735 struct imsm_map *m2 = get_imsm_map(d2, 0);
2736 int i;
2737 int j;
2738 int idx;
2739
2740 for (i = 0; i < m1->num_members; i++) {
2741 idx = get_imsm_disk_idx(d1, i);
2742 for (j = 0; j < m2->num_members; j++)
2743 if (idx == get_imsm_disk_idx(d2, j))
2744 return 1;
2745 }
2746
2747 return 0;
2748 }
2749
2750 static void imsm_delete(struct intel_super *super, struct dl **dlp, int index);
2751
2752 static void imsm_process_update(struct supertype *st,
2753 struct metadata_update *update)
2754 {
2755 /**
2756 * crack open the metadata_update envelope to find the update record
2757 * update can be one of:
2758 * update_activate_spare - a spare device has replaced a failed
2759 * device in an array, update the disk_ord_tbl. If this disk is
2760 * present in all member arrays then also clear the SPARE_DISK
2761 * flag
2762 */
2763 struct intel_super *super = st->sb;
2764 struct imsm_super *mpb;
2765 enum imsm_update_type type = *(enum imsm_update_type *) update->buf;
2766
2767 /* update requires a larger buf but the allocation failed */
2768 if (super->next_len && !super->next_buf) {
2769 super->next_len = 0;
2770 return;
2771 }
2772
2773 if (super->next_buf) {
2774 memcpy(super->next_buf, super->buf, super->len);
2775 free(super->buf);
2776 super->len = super->next_len;
2777 super->buf = super->next_buf;
2778
2779 super->next_len = 0;
2780 super->next_buf = NULL;
2781 }
2782
2783 mpb = super->anchor;
2784
2785 switch (type) {
2786 case update_activate_spare: {
2787 struct imsm_update_activate_spare *u = (void *) update->buf;
2788 struct imsm_dev *dev = get_imsm_dev(super, u->array);
2789 struct imsm_map *map = get_imsm_map(dev, 0);
2790 struct imsm_map *migr_map;
2791 struct active_array *a;
2792 struct imsm_disk *disk;
2793 __u32 status;
2794 __u8 to_state;
2795 struct dl *dl;
2796 unsigned int found;
2797 int failed;
2798 int victim = get_imsm_disk_idx(dev, u->slot);
2799 int i;
2800
2801 for (dl = super->disks; dl; dl = dl->next)
2802 if (dl == u->dl)
2803 break;
2804
2805 if (!dl) {
2806 fprintf(stderr, "error: imsm_activate_spare passed "
2807 "an unknown disk (index: %d)\n",
2808 u->dl->index);
2809 return;
2810 }
2811
2812 super->updates_pending++;
2813
2814 /* count failures (excluding rebuilds and the victim)
2815 * to determine map[0] state
2816 */
2817 failed = 0;
2818 for (i = 0; i < map->num_members; i++) {
2819 if (i == u->slot)
2820 continue;
2821 disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i));
2822 if (!disk ||
2823 __le32_to_cpu(disk->status) & FAILED_DISK)
2824 failed++;
2825 }
2826
2827 /* adding a pristine spare, assign a new index */
2828 if (dl->index < 0) {
2829 dl->index = super->anchor->num_disks;
2830 super->anchor->num_disks++;
2831 }
2832 disk = &dl->disk;
2833 status = __le32_to_cpu(disk->status);
2834 status |= CONFIGURED_DISK;
2835 status &= ~SPARE_DISK;
2836 disk->status = __cpu_to_le32(status);
2837
2838 /* mark rebuild */
2839 to_state = imsm_check_degraded(super, dev, failed);
2840 map->map_state = IMSM_T_STATE_DEGRADED;
2841 migrate(dev, to_state, 1);
2842 migr_map = get_imsm_map(dev, 1);
2843 set_imsm_ord_tbl_ent(map, u->slot, dl->index);
2844 set_imsm_ord_tbl_ent(migr_map, u->slot, dl->index | IMSM_ORD_REBUILD);
2845
2846 /* count arrays using the victim in the metadata */
2847 found = 0;
2848 for (a = st->arrays; a ; a = a->next) {
2849 dev = get_imsm_dev(super, a->info.container_member);
2850 for (i = 0; i < map->num_members; i++)
2851 if (victim == get_imsm_disk_idx(dev, i))
2852 found++;
2853 }
2854
2855 /* delete the victim if it is no longer being
2856 * utilized anywhere
2857 */
2858 if (!found) {
2859 struct dl **dlp;
2860
2861 for (dlp = &super->disks; *dlp; dlp = &(*dlp)->next)
2862 if ((*dlp)->index == victim)
2863 break;
2864 /* We know that 'manager' isn't touching anything,
2865 * so it is safe to:
2866 */
2867 imsm_delete(super, dlp, victim);
2868 }
2869 break;
2870 }
2871 case update_create_array: {
2872 /* someone wants to create a new array, we need to be aware of
2873 * a few races/collisions:
2874 * 1/ 'Create' called by two separate instances of mdadm
2875 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
2876 * devices that have since been assimilated via
2877 * activate_spare.
2878 * In the event this update can not be carried out mdadm will
2879 * (FIX ME) notice that its update did not take hold.
2880 */
2881 struct imsm_update_create_array *u = (void *) update->buf;
2882 struct imsm_dev *dev;
2883 struct imsm_map *map, *new_map;
2884 unsigned long long start, end;
2885 unsigned long long new_start, new_end;
2886 int i;
2887 int overlap = 0;
2888
2889 /* handle racing creates: first come first serve */
2890 if (u->dev_idx < mpb->num_raid_devs) {
2891 dprintf("%s: subarray %d already defined\n",
2892 __func__, u->dev_idx);
2893 return;
2894 }
2895
2896 /* check update is next in sequence */
2897 if (u->dev_idx != mpb->num_raid_devs) {
2898 dprintf("%s: can not create array %d expected index %d\n",
2899 __func__, u->dev_idx, mpb->num_raid_devs);
2900 return;
2901 }
2902
2903 new_map = get_imsm_map(&u->dev, 0);
2904 new_start = __le32_to_cpu(new_map->pba_of_lba0);
2905 new_end = new_start + __le32_to_cpu(new_map->blocks_per_member);
2906
2907 /* handle activate_spare versus create race:
2908 * check to make sure that overlapping arrays do not include
2909 * overalpping disks
2910 */
2911 for (i = 0; i < mpb->num_raid_devs; i++) {
2912 dev = get_imsm_dev(super, i);
2913 map = get_imsm_map(dev, 0);
2914 start = __le32_to_cpu(map->pba_of_lba0);
2915 end = start + __le32_to_cpu(map->blocks_per_member);
2916 if ((new_start >= start && new_start <= end) ||
2917 (start >= new_start && start <= new_end))
2918 overlap = 1;
2919 if (overlap && disks_overlap(dev, &u->dev)) {
2920 dprintf("%s: arrays overlap\n", __func__);
2921 return;
2922 }
2923 }
2924 /* check num_members sanity */
2925 if (new_map->num_members > mpb->num_disks) {
2926 dprintf("%s: num_disks out of range\n", __func__);
2927 return;
2928 }
2929
2930 /* check that prepare update was successful */
2931 if (!update->space) {
2932 dprintf("%s: prepare update failed\n", __func__);
2933 return;
2934 }
2935
2936 super->updates_pending++;
2937 dev = update->space;
2938 map = get_imsm_map(dev, 0);
2939 update->space = NULL;
2940 imsm_copy_dev(dev, &u->dev);
2941 map = get_imsm_map(dev, 0);
2942 super->dev_tbl[u->dev_idx] = dev;
2943 mpb->num_raid_devs++;
2944
2945 /* fix up flags */
2946 for (i = 0; i < map->num_members; i++) {
2947 struct imsm_disk *disk;
2948 __u32 status;
2949
2950 disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i));
2951 status = __le32_to_cpu(disk->status);
2952 status |= CONFIGURED_DISK;
2953 status &= ~SPARE_DISK;
2954 disk->status = __cpu_to_le32(status);
2955 }
2956 break;
2957 }
2958 case update_add_disk:
2959
2960 /* we may be able to repair some arrays if disks are
2961 * being added */
2962 if (super->add) {
2963 struct active_array *a;
2964 for (a = st->arrays; a; a = a->next)
2965 a->check_degraded = 1;
2966 }
2967 /* add some spares to the metadata */
2968 while (super->add) {
2969 struct dl *al;
2970
2971 al = super->add;
2972 super->add = al->next;
2973 al->next = super->disks;
2974 super->disks = al;
2975 dprintf("%s: added %x:%x\n",
2976 __func__, al->major, al->minor);
2977 }
2978
2979 break;
2980 }
2981 }
2982
2983 static void imsm_prepare_update(struct supertype *st,
2984 struct metadata_update *update)
2985 {
2986 /**
2987 * Allocate space to hold new disk entries, raid-device entries or a new
2988 * mpb if necessary. The manager synchronously waits for updates to
2989 * complete in the monitor, so new mpb buffers allocated here can be
2990 * integrated by the monitor thread without worrying about live pointers
2991 * in the manager thread.
2992 */
2993 enum imsm_update_type type = *(enum imsm_update_type *) update->buf;
2994 struct intel_super *super = st->sb;
2995 struct imsm_super *mpb = super->anchor;
2996 size_t buf_len;
2997 size_t len = 0;
2998
2999 switch (type) {
3000 case update_create_array: {
3001 struct imsm_update_create_array *u = (void *) update->buf;
3002
3003 len = sizeof_imsm_dev(&u->dev, 1);
3004 update->space = malloc(len);
3005 break;
3006 default:
3007 break;
3008 }
3009 }
3010
3011 /* check if we need a larger metadata buffer */
3012 if (super->next_buf)
3013 buf_len = super->next_len;
3014 else
3015 buf_len = super->len;
3016
3017 if (__le32_to_cpu(mpb->mpb_size) + len > buf_len) {
3018 /* ok we need a larger buf than what is currently allocated
3019 * if this allocation fails process_update will notice that
3020 * ->next_len is set and ->next_buf is NULL
3021 */
3022 buf_len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + len, 512);
3023 if (super->next_buf)
3024 free(super->next_buf);
3025
3026 super->next_len = buf_len;
3027 if (posix_memalign(&super->next_buf, buf_len, 512) != 0)
3028 super->next_buf = NULL;
3029 }
3030 }
3031
3032 /* must be called while manager is quiesced */
3033 static void imsm_delete(struct intel_super *super, struct dl **dlp, int index)
3034 {
3035 struct imsm_super *mpb = super->anchor;
3036 struct dl *iter;
3037 struct imsm_dev *dev;
3038 struct imsm_map *map;
3039 int i, j, num_members;
3040 __u32 ord;
3041
3042 dprintf("%s: deleting device[%d] from imsm_super\n",
3043 __func__, index);
3044
3045 /* shift all indexes down one */
3046 for (iter = super->disks; iter; iter = iter->next)
3047 if (iter->index > index)
3048 iter->index--;
3049
3050 for (i = 0; i < mpb->num_raid_devs; i++) {
3051 dev = get_imsm_dev(super, i);
3052 map = get_imsm_map(dev, 0);
3053 num_members = map->num_members;
3054 for (j = 0; j < num_members; j++) {
3055 /* update ord entries being careful not to propagate
3056 * ord-flags to the first map
3057 */
3058 ord = get_imsm_ord_tbl_ent(dev, j);
3059
3060 if (ord_to_idx(ord) <= index)
3061 continue;
3062
3063 map = get_imsm_map(dev, 0);
3064 set_imsm_ord_tbl_ent(map, j, ord_to_idx(ord - 1));
3065 map = get_imsm_map(dev, 1);
3066 if (map)
3067 set_imsm_ord_tbl_ent(map, j, ord - 1);
3068 }
3069 }
3070
3071 mpb->num_disks--;
3072 super->updates_pending++;
3073 if (*dlp) {
3074 struct dl *dl = *dlp;
3075
3076 *dlp = (*dlp)->next;
3077 __free_imsm_disk(dl);
3078 }
3079 }
3080 #endif /* MDASSEMBLE */
3081
3082 struct superswitch super_imsm = {
3083 #ifndef MDASSEMBLE
3084 .examine_super = examine_super_imsm,
3085 .brief_examine_super = brief_examine_super_imsm,
3086 .detail_super = detail_super_imsm,
3087 .brief_detail_super = brief_detail_super_imsm,
3088 .write_init_super = write_init_super_imsm,
3089 .validate_geometry = validate_geometry_imsm,
3090 .add_to_super = add_to_super_imsm,
3091 #endif
3092 .match_home = match_home_imsm,
3093 .uuid_from_super= uuid_from_super_imsm,
3094 .getinfo_super = getinfo_super_imsm,
3095 .update_super = update_super_imsm,
3096
3097 .avail_size = avail_size_imsm,
3098
3099 .compare_super = compare_super_imsm,
3100
3101 .load_super = load_super_imsm,
3102 .init_super = init_super_imsm,
3103 .store_super = store_zero_imsm,
3104 .free_super = free_super_imsm,
3105 .match_metadata_desc = match_metadata_desc_imsm,
3106 .container_content = container_content_imsm,
3107
3108 .external = 1,
3109
3110 #ifndef MDASSEMBLE
3111 /* for mdmon */
3112 .open_new = imsm_open_new,
3113 .load_super = load_super_imsm,
3114 .set_array_state= imsm_set_array_state,
3115 .set_disk = imsm_set_disk,
3116 .sync_metadata = imsm_sync_metadata,
3117 .activate_spare = imsm_activate_spare,
3118 .process_update = imsm_process_update,
3119 .prepare_update = imsm_prepare_update,
3120 #endif /* MDASSEMBLE */
3121 };