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