2 * mdadm - Intel(R) Matrix Storage Manager Support
4 * Copyright (C) 2002-2008 Intel Corporation
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.
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
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.
20 #define HAVE_STDINT_H 1
24 #include "platform-intel.h"
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
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)
54 #define MPB_SECTOR_CNT 418
55 #define IMSM_RESERVED_SECTORS 4096
56 #define SECT_PER_MB_SHIFT 11
58 /* Disk configuration info. */
59 #define IMSM_MAX_DEVICES 255
61 __u8 serial
[MAX_RAID_SERIAL_LEN
];/* 0xD8 - 0xE7 ascii serial number */
62 __u32 total_blocks
; /* 0xE8 - 0xEB total blocks */
63 __u32 scsi_id
; /* 0xEC - 0xEF scsi ID */
64 #define SPARE_DISK __cpu_to_le32(0x01) /* Spare */
65 #define CONFIGURED_DISK __cpu_to_le32(0x02) /* Member of some RaidDev */
66 #define FAILED_DISK __cpu_to_le32(0x04) /* Permanent failure */
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 */
73 /* RAID map configuration infos. */
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
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 */
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
97 } __attribute__ ((packed
));
100 __u32 curr_migr_unit
;
101 __u32 checkpoint_id
; /* id to access curr_migr_unit */
102 __u8 migr_state
; /* Normal or Migrating */
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 #define MIGR_REPAIR 5
109 __u8 migr_type
; /* Initializing, Rebuilding, ... */
111 __u8 fs_state
; /* fast-sync state for CnG (0xff == disabled) */
112 __u16 verify_errors
; /* number of mismatches */
113 __u16 bad_blocks
; /* number of bad blocks during verify */
115 struct imsm_map map
[1];
116 /* here comes another one if migr_state */
117 } __attribute__ ((packed
));
120 __u8 volume
[MAX_RAID_SERIAL_LEN
];
123 #define DEV_BOOTABLE __cpu_to_le32(0x01)
124 #define DEV_BOOT_DEVICE __cpu_to_le32(0x02)
125 #define DEV_READ_COALESCING __cpu_to_le32(0x04)
126 #define DEV_WRITE_COALESCING __cpu_to_le32(0x08)
127 #define DEV_LAST_SHUTDOWN_DIRTY __cpu_to_le32(0x10)
128 #define DEV_HIDDEN_AT_BOOT __cpu_to_le32(0x20)
129 #define DEV_CURRENTLY_HIDDEN __cpu_to_le32(0x40)
130 #define DEV_VERIFY_AND_FIX __cpu_to_le32(0x80)
131 #define DEV_MAP_STATE_UNINIT __cpu_to_le32(0x100)
132 #define DEV_NO_AUTO_RECOVERY __cpu_to_le32(0x200)
133 #define DEV_CLONE_N_GO __cpu_to_le32(0x400)
134 #define DEV_CLONE_MAN_SYNC __cpu_to_le32(0x800)
135 #define DEV_CNG_MASTER_DISK_NUM __cpu_to_le32(0x1000)
136 __u32 status
; /* Persistent RaidDev status */
137 __u32 reserved_blocks
; /* Reserved blocks at beginning of volume */
141 __u8 cng_master_disk
;
145 #define IMSM_DEV_FILLERS 10
146 __u32 filler
[IMSM_DEV_FILLERS
];
148 } __attribute__ ((packed
));
151 __u8 sig
[MAX_SIGNATURE_LENGTH
]; /* 0x00 - 0x1F */
152 __u32 check_sum
; /* 0x20 - 0x23 MPB Checksum */
153 __u32 mpb_size
; /* 0x24 - 0x27 Size of MPB */
154 __u32 family_num
; /* 0x28 - 0x2B Checksum from first time this config was written */
155 __u32 generation_num
; /* 0x2C - 0x2F Incremented each time this array's MPB is written */
156 __u32 error_log_size
; /* 0x30 - 0x33 in bytes */
157 __u32 attributes
; /* 0x34 - 0x37 */
158 __u8 num_disks
; /* 0x38 Number of configured disks */
159 __u8 num_raid_devs
; /* 0x39 Number of configured volumes */
160 __u8 error_log_pos
; /* 0x3A */
161 __u8 fill
[1]; /* 0x3B */
162 __u32 cache_size
; /* 0x3c - 0x40 in mb */
163 __u32 orig_family_num
; /* 0x40 - 0x43 original family num */
164 __u32 pwr_cycle_count
; /* 0x44 - 0x47 simulated power cycle count for array */
165 __u32 bbm_log_size
; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */
166 #define IMSM_FILLERS 35
167 __u32 filler
[IMSM_FILLERS
]; /* 0x4C - 0xD7 RAID_MPB_FILLERS */
168 struct imsm_disk disk
[1]; /* 0xD8 diskTbl[numDisks] */
169 /* here comes imsm_dev[num_raid_devs] */
170 /* here comes BBM logs */
171 } __attribute__ ((packed
));
173 #define BBM_LOG_MAX_ENTRIES 254
175 struct bbm_log_entry
{
176 __u64 defective_block_start
;
177 #define UNREADABLE 0xFFFFFFFF
178 __u32 spare_block_offset
;
179 __u16 remapped_marked_count
;
181 } __attribute__ ((__packed__
));
184 __u32 signature
; /* 0xABADB10C */
186 __u32 reserved_spare_block_count
; /* 0 */
187 __u32 reserved
; /* 0xFFFF */
188 __u64 first_spare_lba
;
189 struct bbm_log_entry mapped_block_entries
[BBM_LOG_MAX_ENTRIES
];
190 } __attribute__ ((__packed__
));
194 static char *map_state_str
[] = { "normal", "uninitialized", "degraded", "failed" };
197 static __u8
migr_type(struct imsm_dev
*dev
)
199 if (dev
->vol
.migr_type
== MIGR_VERIFY
&&
200 dev
->status
& DEV_VERIFY_AND_FIX
)
203 return dev
->vol
.migr_type
;
206 static void set_migr_type(struct imsm_dev
*dev
, __u8 migr_type
)
208 /* for compatibility with older oroms convert MIGR_REPAIR, into
209 * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
211 if (migr_type
== MIGR_REPAIR
) {
212 dev
->vol
.migr_type
= MIGR_VERIFY
;
213 dev
->status
|= DEV_VERIFY_AND_FIX
;
215 dev
->vol
.migr_type
= migr_type
;
216 dev
->status
&= ~DEV_VERIFY_AND_FIX
;
220 static unsigned int sector_count(__u32 bytes
)
222 return ((bytes
+ (512-1)) & (~(512-1))) / 512;
225 static unsigned int mpb_sectors(struct imsm_super
*mpb
)
227 return sector_count(__le32_to_cpu(mpb
->mpb_size
));
231 struct imsm_dev
*dev
;
232 struct intel_dev
*next
;
240 /* internal representation of IMSM metadata */
243 void *buf
; /* O_DIRECT buffer for reading/writing metadata */
244 struct imsm_super
*anchor
; /* immovable parameters */
246 size_t len
; /* size of the 'buf' allocation */
247 void *next_buf
; /* for realloc'ing buf from the manager */
249 int updates_pending
; /* count of pending updates for mdmon */
250 int current_vol
; /* index of raid device undergoing creation */
251 __u32 create_offset
; /* common start for 'current_vol' */
252 __u32 random
; /* random data for seeding new family numbers */
253 struct intel_dev
*devlist
;
257 __u8 serial
[MAX_RAID_SERIAL_LEN
];
260 struct imsm_disk disk
;
263 struct extent
*e
; /* for determining freespace @ create */
264 int raiddisk
; /* slot to fill in autolayout */
267 struct dl
*disk_mgmt_list
; /* list of disks to add/remove while mdmon
269 struct dl
*missing
; /* disks removed while we weren't looking */
270 struct bbm_log
*bbm_log
;
271 const char *hba
; /* device path of the raid controller for this metadata */
272 const struct imsm_orom
*orom
; /* platform firmware support */
273 struct intel_super
*next
; /* (temp) list for disambiguating family_num */
277 struct imsm_disk disk
;
278 #define IMSM_UNKNOWN_OWNER (-1)
280 struct intel_disk
*next
;
284 unsigned long long start
, size
;
287 /* definition of messages passed to imsm_process_update */
288 enum imsm_update_type
{
289 update_activate_spare
,
293 update_add_remove_disk
,
294 update_reshape_container_disks
,
297 struct imsm_update_activate_spare
{
298 enum imsm_update_type type
;
302 struct imsm_update_activate_spare
*next
;
316 struct imsm_update_reshape
{
317 enum imsm_update_type type
;
320 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
324 __u8 serial
[MAX_RAID_SERIAL_LEN
];
327 struct imsm_update_create_array
{
328 enum imsm_update_type type
;
333 struct imsm_update_kill_array
{
334 enum imsm_update_type type
;
338 struct imsm_update_rename_array
{
339 enum imsm_update_type type
;
340 __u8 name
[MAX_RAID_SERIAL_LEN
];
344 struct imsm_update_add_remove_disk
{
345 enum imsm_update_type type
;
348 static struct supertype
*match_metadata_desc_imsm(char *arg
)
350 struct supertype
*st
;
352 if (strcmp(arg
, "imsm") != 0 &&
353 strcmp(arg
, "default") != 0
357 st
= malloc(sizeof(*st
));
360 memset(st
, 0, sizeof(*st
));
361 st
->container_dev
= NoMdDev
;
362 st
->ss
= &super_imsm
;
363 st
->max_devs
= IMSM_MAX_DEVICES
;
364 st
->minor_version
= 0;
370 static __u8
*get_imsm_version(struct imsm_super
*mpb
)
372 return &mpb
->sig
[MPB_SIG_LEN
];
376 /* retrieve a disk directly from the anchor when the anchor is known to be
377 * up-to-date, currently only at load time
379 static struct imsm_disk
*__get_imsm_disk(struct imsm_super
*mpb
, __u8 index
)
381 if (index
>= mpb
->num_disks
)
383 return &mpb
->disk
[index
];
386 /* retrieve the disk description based on a index of the disk
389 static struct dl
*get_imsm_dl_disk(struct intel_super
*super
, __u8 index
)
393 for (d
= super
->disks
; d
; d
= d
->next
)
394 if (d
->index
== index
)
399 /* retrieve a disk from the parsed metadata */
400 static struct imsm_disk
*get_imsm_disk(struct intel_super
*super
, __u8 index
)
404 dl
= get_imsm_dl_disk(super
, index
);
411 /* generate a checksum directly from the anchor when the anchor is known to be
412 * up-to-date, currently only at load or write_super after coalescing
414 static __u32
__gen_imsm_checksum(struct imsm_super
*mpb
)
416 __u32 end
= mpb
->mpb_size
/ sizeof(end
);
417 __u32
*p
= (__u32
*) mpb
;
421 sum
+= __le32_to_cpu(*p
);
425 return sum
- __le32_to_cpu(mpb
->check_sum
);
428 static size_t sizeof_imsm_map(struct imsm_map
*map
)
430 return sizeof(struct imsm_map
) + sizeof(__u32
) * (map
->num_members
- 1);
433 struct imsm_map
*get_imsm_map(struct imsm_dev
*dev
, int second_map
)
435 struct imsm_map
*map
= &dev
->vol
.map
[0];
437 if (second_map
&& !dev
->vol
.migr_state
)
439 else if (second_map
) {
442 return ptr
+ sizeof_imsm_map(map
);
448 /* return the size of the device.
449 * migr_state increases the returned size if map[0] were to be duplicated
451 static size_t sizeof_imsm_dev(struct imsm_dev
*dev
, int migr_state
)
453 size_t size
= sizeof(*dev
) - sizeof(struct imsm_map
) +
454 sizeof_imsm_map(get_imsm_map(dev
, 0));
456 /* migrating means an additional map */
457 if (dev
->vol
.migr_state
)
458 size
+= sizeof_imsm_map(get_imsm_map(dev
, 1));
460 size
+= sizeof_imsm_map(get_imsm_map(dev
, 0));
466 /* retrieve disk serial number list from a metadata update */
467 static struct disk_info
*get_disk_info(struct imsm_update_create_array
*update
)
470 struct disk_info
*inf
;
472 inf
= u
+ sizeof(*update
) - sizeof(struct imsm_dev
) +
473 sizeof_imsm_dev(&update
->dev
, 0);
479 static struct imsm_dev
*__get_imsm_dev(struct imsm_super
*mpb
, __u8 index
)
485 if (index
>= mpb
->num_raid_devs
)
488 /* devices start after all disks */
489 offset
= ((void *) &mpb
->disk
[mpb
->num_disks
]) - _mpb
;
491 for (i
= 0; i
<= index
; i
++)
493 return _mpb
+ offset
;
495 offset
+= sizeof_imsm_dev(_mpb
+ offset
, 0);
500 static struct imsm_dev
*get_imsm_dev(struct intel_super
*super
, __u8 index
)
502 struct intel_dev
*dv
;
504 if (index
>= super
->anchor
->num_raid_devs
)
506 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
507 if (dv
->index
== index
)
515 * == 1 get second map
516 * == -1 than get map according to the current migr_state
518 static __u32
get_imsm_ord_tbl_ent(struct imsm_dev
*dev
,
522 struct imsm_map
*map
;
524 if (second_map
== -1) {
525 if (dev
->vol
.migr_state
)
526 map
= get_imsm_map(dev
, 1);
528 map
= get_imsm_map(dev
, 0);
530 map
= get_imsm_map(dev
, second_map
);
533 /* top byte identifies disk under rebuild */
534 return __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
537 #define ord_to_idx(ord) (((ord) << 8) >> 8)
538 static __u32
get_imsm_disk_idx(struct imsm_dev
*dev
, int slot
, int second_map
)
540 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, second_map
);
542 return ord_to_idx(ord
);
545 static void set_imsm_ord_tbl_ent(struct imsm_map
*map
, int slot
, __u32 ord
)
547 map
->disk_ord_tbl
[slot
] = __cpu_to_le32(ord
);
550 static int get_imsm_disk_slot(struct imsm_map
*map
, unsigned idx
)
555 for (slot
= 0; slot
< map
->num_members
; slot
++) {
556 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
557 if (ord_to_idx(ord
) == idx
)
564 static int get_imsm_raid_level(struct imsm_map
*map
)
566 if (map
->raid_level
== 1) {
567 if (map
->num_members
== 2)
573 return map
->raid_level
;
576 static int cmp_extent(const void *av
, const void *bv
)
578 const struct extent
*a
= av
;
579 const struct extent
*b
= bv
;
580 if (a
->start
< b
->start
)
582 if (a
->start
> b
->start
)
587 static int count_memberships(struct dl
*dl
, struct intel_super
*super
)
592 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
593 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
594 struct imsm_map
*map
= get_imsm_map(dev
, 0);
596 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
603 static struct extent
*get_extents(struct intel_super
*super
, struct dl
*dl
)
605 /* find a list of used extents on the given physical device */
606 struct extent
*rv
, *e
;
608 int memberships
= count_memberships(dl
, super
);
609 __u32 reservation
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
611 rv
= malloc(sizeof(struct extent
) * (memberships
+ 1));
616 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
617 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
618 struct imsm_map
*map
= get_imsm_map(dev
, 0);
620 if (get_imsm_disk_slot(map
, dl
->index
) >= 0) {
621 e
->start
= __le32_to_cpu(map
->pba_of_lba0
);
622 e
->size
= __le32_to_cpu(map
->blocks_per_member
);
626 qsort(rv
, memberships
, sizeof(*rv
), cmp_extent
);
628 /* determine the start of the metadata
629 * when no raid devices are defined use the default
630 * ...otherwise allow the metadata to truncate the value
631 * as is the case with older versions of imsm
634 struct extent
*last
= &rv
[memberships
- 1];
637 remainder
= __le32_to_cpu(dl
->disk
.total_blocks
) -
638 (last
->start
+ last
->size
);
639 /* round down to 1k block to satisfy precision of the kernel
643 /* make sure remainder is still sane */
644 if (remainder
< (unsigned)ROUND_UP(super
->len
, 512) >> 9)
645 remainder
= ROUND_UP(super
->len
, 512) >> 9;
646 if (reservation
> remainder
)
647 reservation
= remainder
;
649 e
->start
= __le32_to_cpu(dl
->disk
.total_blocks
) - reservation
;
654 /* try to determine how much space is reserved for metadata from
655 * the last get_extents() entry, otherwise fallback to the
658 static __u32
imsm_reserved_sectors(struct intel_super
*super
, struct dl
*dl
)
664 /* for spares just return a minimal reservation which will grow
665 * once the spare is picked up by an array
668 return MPB_SECTOR_CNT
;
670 e
= get_extents(super
, dl
);
672 return MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
674 /* scroll to last entry */
675 for (i
= 0; e
[i
].size
; i
++)
678 rv
= __le32_to_cpu(dl
->disk
.total_blocks
) - e
[i
].start
;
685 static int is_spare(struct imsm_disk
*disk
)
687 return (disk
->status
& SPARE_DISK
) == SPARE_DISK
;
690 static int is_configured(struct imsm_disk
*disk
)
692 return (disk
->status
& CONFIGURED_DISK
) == CONFIGURED_DISK
;
695 static int is_failed(struct imsm_disk
*disk
)
697 return (disk
->status
& FAILED_DISK
) == FAILED_DISK
;
700 /* Return minimum size of a spare that can be used in this array*/
701 static unsigned long long min_acceptable_spare_size_imsm(struct supertype
*st
)
703 struct intel_super
*super
= st
->sb
;
707 unsigned long long rv
= 0;
711 /* find first active disk in array */
713 while (dl
&& (is_failed(&dl
->disk
) || dl
->index
== -1))
717 /* find last lba used by subarrays */
718 e
= get_extents(super
, dl
);
721 for (i
= 0; e
[i
].size
; i
++)
724 rv
= e
[i
-1].start
+ e
[i
-1].size
;
726 /* add the amount of space needed for metadata */
727 rv
= rv
+ MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
732 static __u64
blocks_per_migr_unit(struct imsm_dev
*dev
);
734 static void print_imsm_dev(struct imsm_dev
*dev
, char *uuid
, int disk_idx
)
738 struct imsm_map
*map
= get_imsm_map(dev
, 0);
742 printf("[%.16s]:\n", dev
->volume
);
743 printf(" UUID : %s\n", uuid
);
744 printf(" RAID Level : %d\n", get_imsm_raid_level(map
));
745 printf(" Members : %d\n", map
->num_members
);
746 printf(" Slots : [");
747 for (i
= 0; i
< map
->num_members
; i
++) {
748 ord
= get_imsm_ord_tbl_ent(dev
, i
, -1);
749 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
752 slot
= get_imsm_disk_slot(map
, disk_idx
);
754 ord
= get_imsm_ord_tbl_ent(dev
, slot
, -1);
755 printf(" This Slot : %d%s\n", slot
,
756 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
758 printf(" This Slot : ?\n");
759 sz
= __le32_to_cpu(dev
->size_high
);
761 sz
+= __le32_to_cpu(dev
->size_low
);
762 printf(" Array Size : %llu%s\n", (unsigned long long)sz
,
763 human_size(sz
* 512));
764 sz
= __le32_to_cpu(map
->blocks_per_member
);
765 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz
,
766 human_size(sz
* 512));
767 printf(" Sector Offset : %u\n",
768 __le32_to_cpu(map
->pba_of_lba0
));
769 printf(" Num Stripes : %u\n",
770 __le32_to_cpu(map
->num_data_stripes
));
771 printf(" Chunk Size : %u KiB\n",
772 __le16_to_cpu(map
->blocks_per_strip
) / 2);
773 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
774 printf(" Migrate State : ");
775 if (dev
->vol
.migr_state
) {
776 if (migr_type(dev
) == MIGR_INIT
)
777 printf("initialize\n");
778 else if (migr_type(dev
) == MIGR_REBUILD
)
780 else if (migr_type(dev
) == MIGR_VERIFY
)
782 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
783 printf("general migration\n");
784 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
785 printf("state change\n");
786 else if (migr_type(dev
) == MIGR_REPAIR
)
789 printf("<unknown:%d>\n", migr_type(dev
));
792 printf(" Map State : %s", map_state_str
[map
->map_state
]);
793 if (dev
->vol
.migr_state
) {
794 struct imsm_map
*map
= get_imsm_map(dev
, 1);
796 printf(" <-- %s", map_state_str
[map
->map_state
]);
797 printf("\n Checkpoint : %u (%llu)",
798 __le32_to_cpu(dev
->vol
.curr_migr_unit
),
799 (unsigned long long)blocks_per_migr_unit(dev
));
802 printf(" Dirty State : %s\n", dev
->vol
.dirty
? "dirty" : "clean");
805 static void print_imsm_disk(struct imsm_super
*mpb
, int index
, __u32 reserved
)
807 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, index
);
808 char str
[MAX_RAID_SERIAL_LEN
+ 1];
811 if (index
< 0 || !disk
)
815 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
816 printf(" Disk%02d Serial : %s\n", index
, str
);
817 printf(" State :%s%s%s\n", is_spare(disk
) ? " spare" : "",
818 is_configured(disk
) ? " active" : "",
819 is_failed(disk
) ? " failed" : "");
820 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
821 sz
= __le32_to_cpu(disk
->total_blocks
) - reserved
;
822 printf(" Usable Size : %llu%s\n", (unsigned long long)sz
,
823 human_size(sz
* 512));
826 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
);
828 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
830 struct intel_super
*super
= st
->sb
;
831 struct imsm_super
*mpb
= super
->anchor
;
832 char str
[MAX_SIGNATURE_LENGTH
];
837 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
840 snprintf(str
, MPB_SIG_LEN
, "%s", mpb
->sig
);
841 printf(" Magic : %s\n", str
);
842 snprintf(str
, strlen(MPB_VERSION_RAID0
), "%s", get_imsm_version(mpb
));
843 printf(" Version : %s\n", get_imsm_version(mpb
));
844 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
845 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
846 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
847 getinfo_super_imsm(st
, &info
, NULL
);
848 fname_from_uuid(st
, &info
, nbuf
, ':');
849 printf(" UUID : %s\n", nbuf
+ 5);
850 sum
= __le32_to_cpu(mpb
->check_sum
);
851 printf(" Checksum : %08x %s\n", sum
,
852 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
853 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
));
854 printf(" Disks : %d\n", mpb
->num_disks
);
855 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
856 print_imsm_disk(mpb
, super
->disks
->index
, reserved
);
857 if (super
->bbm_log
) {
858 struct bbm_log
*log
= super
->bbm_log
;
861 printf("Bad Block Management Log:\n");
862 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
863 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
864 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
865 printf(" Spare Blocks : %d\n", __le32_to_cpu(log
->reserved_spare_block_count
));
866 printf(" First Spare : %llx\n",
867 (unsigned long long) __le64_to_cpu(log
->first_spare_lba
));
869 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
871 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
873 super
->current_vol
= i
;
874 getinfo_super_imsm(st
, &info
, NULL
);
875 fname_from_uuid(st
, &info
, nbuf
, ':');
876 print_imsm_dev(dev
, nbuf
+ 5, super
->disks
->index
);
878 for (i
= 0; i
< mpb
->num_disks
; i
++) {
879 if (i
== super
->disks
->index
)
881 print_imsm_disk(mpb
, i
, reserved
);
883 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
884 struct imsm_disk
*disk
;
885 char str
[MAX_RAID_SERIAL_LEN
+ 1];
893 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
894 printf(" Disk Serial : %s\n", str
);
895 printf(" State :%s%s%s\n", is_spare(disk
) ? " spare" : "",
896 is_configured(disk
) ? " active" : "",
897 is_failed(disk
) ? " failed" : "");
898 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
899 sz
= __le32_to_cpu(disk
->total_blocks
) - reserved
;
900 printf(" Usable Size : %llu%s\n", (unsigned long long)sz
,
901 human_size(sz
* 512));
905 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
907 /* We just write a generic IMSM ARRAY entry */
910 struct intel_super
*super
= st
->sb
;
912 if (!super
->anchor
->num_raid_devs
) {
913 printf("ARRAY metadata=imsm\n");
917 getinfo_super_imsm(st
, &info
, NULL
);
918 fname_from_uuid(st
, &info
, nbuf
, ':');
919 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
922 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
924 /* We just write a generic IMSM ARRAY entry */
928 struct intel_super
*super
= st
->sb
;
931 if (!super
->anchor
->num_raid_devs
)
934 getinfo_super_imsm(st
, &info
, NULL
);
935 fname_from_uuid(st
, &info
, nbuf
, ':');
936 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
937 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
939 super
->current_vol
= i
;
940 getinfo_super_imsm(st
, &info
, NULL
);
941 fname_from_uuid(st
, &info
, nbuf1
, ':');
942 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
943 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
947 static void export_examine_super_imsm(struct supertype
*st
)
949 struct intel_super
*super
= st
->sb
;
950 struct imsm_super
*mpb
= super
->anchor
;
954 getinfo_super_imsm(st
, &info
, NULL
);
955 fname_from_uuid(st
, &info
, nbuf
, ':');
956 printf("MD_METADATA=imsm\n");
957 printf("MD_LEVEL=container\n");
958 printf("MD_UUID=%s\n", nbuf
+5);
959 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
962 static void detail_super_imsm(struct supertype
*st
, char *homehost
)
967 getinfo_super_imsm(st
, &info
, NULL
);
968 fname_from_uuid(st
, &info
, nbuf
, ':');
969 printf("\n UUID : %s\n", nbuf
+ 5);
972 static void brief_detail_super_imsm(struct supertype
*st
)
976 getinfo_super_imsm(st
, &info
, NULL
);
977 fname_from_uuid(st
, &info
, nbuf
, ':');
978 printf(" UUID=%s", nbuf
+ 5);
981 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
);
982 static void fd2devname(int fd
, char *name
);
984 static int imsm_enumerate_ports(const char *hba_path
, int port_count
, int host_base
, int verbose
)
986 /* dump an unsorted list of devices attached to ahci, as well as
987 * non-connected ports
989 int hba_len
= strlen(hba_path
) + 1;
994 unsigned long port_mask
= (1 << port_count
) - 1;
996 if (port_count
> (int)sizeof(port_mask
) * 8) {
998 fprintf(stderr
, Name
": port_count %d out of range\n", port_count
);
1002 /* scroll through /sys/dev/block looking for devices attached to
1005 dir
= opendir("/sys/dev/block");
1006 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
1017 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
1019 path
= devt_to_devpath(makedev(major
, minor
));
1022 if (!path_attached_to_hba(path
, hba_path
)) {
1028 /* retrieve the scsi device type */
1029 if (asprintf(&device
, "/sys/dev/block/%d:%d/device/xxxxxxx", major
, minor
) < 0) {
1031 fprintf(stderr
, Name
": failed to allocate 'device'\n");
1035 sprintf(device
, "/sys/dev/block/%d:%d/device/type", major
, minor
);
1036 if (load_sys(device
, buf
) != 0) {
1038 fprintf(stderr
, Name
": failed to read device type for %s\n",
1044 type
= strtoul(buf
, NULL
, 10);
1046 /* if it's not a disk print the vendor and model */
1047 if (!(type
== 0 || type
== 7 || type
== 14)) {
1050 sprintf(device
, "/sys/dev/block/%d:%d/device/vendor", major
, minor
);
1051 if (load_sys(device
, buf
) == 0) {
1052 strncpy(vendor
, buf
, sizeof(vendor
));
1053 vendor
[sizeof(vendor
) - 1] = '\0';
1054 c
= (char *) &vendor
[sizeof(vendor
) - 1];
1055 while (isspace(*c
) || *c
== '\0')
1059 sprintf(device
, "/sys/dev/block/%d:%d/device/model", major
, minor
);
1060 if (load_sys(device
, buf
) == 0) {
1061 strncpy(model
, buf
, sizeof(model
));
1062 model
[sizeof(model
) - 1] = '\0';
1063 c
= (char *) &model
[sizeof(model
) - 1];
1064 while (isspace(*c
) || *c
== '\0')
1068 if (vendor
[0] && model
[0])
1069 sprintf(buf
, "%.64s %.64s", vendor
, model
);
1071 switch (type
) { /* numbers from hald/linux/device.c */
1072 case 1: sprintf(buf
, "tape"); break;
1073 case 2: sprintf(buf
, "printer"); break;
1074 case 3: sprintf(buf
, "processor"); break;
1076 case 5: sprintf(buf
, "cdrom"); break;
1077 case 6: sprintf(buf
, "scanner"); break;
1078 case 8: sprintf(buf
, "media_changer"); break;
1079 case 9: sprintf(buf
, "comm"); break;
1080 case 12: sprintf(buf
, "raid"); break;
1081 default: sprintf(buf
, "unknown");
1087 /* chop device path to 'host%d' and calculate the port number */
1088 c
= strchr(&path
[hba_len
], '/');
1091 fprintf(stderr
, Name
": %s - invalid path name\n", path
+ hba_len
);
1096 if (sscanf(&path
[hba_len
], "host%d", &port
) == 1)
1100 *c
= '/'; /* repair the full string */
1101 fprintf(stderr
, Name
": failed to determine port number for %s\n",
1108 /* mark this port as used */
1109 port_mask
&= ~(1 << port
);
1111 /* print out the device information */
1113 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
1117 fd
= dev_open(ent
->d_name
, O_RDONLY
);
1119 printf(" Port%d : - disk info unavailable -\n", port
);
1121 fd2devname(fd
, buf
);
1122 printf(" Port%d : %s", port
, buf
);
1123 if (imsm_read_serial(fd
, NULL
, (__u8
*) buf
) == 0)
1124 printf(" (%s)\n", buf
);
1139 for (i
= 0; i
< port_count
; i
++)
1140 if (port_mask
& (1 << i
))
1141 printf(" Port%d : - no device attached -\n", i
);
1147 static int detail_platform_imsm(int verbose
, int enumerate_only
)
1149 /* There are two components to imsm platform support, the ahci SATA
1150 * controller and the option-rom. To find the SATA controller we
1151 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
1152 * controller with the Intel vendor id is present. This approach
1153 * allows mdadm to leverage the kernel's ahci detection logic, with the
1154 * caveat that if ahci.ko is not loaded mdadm will not be able to
1155 * detect platform raid capabilities. The option-rom resides in a
1156 * platform "Adapter ROM". We scan for its signature to retrieve the
1157 * platform capabilities. If raid support is disabled in the BIOS the
1158 * option-rom capability structure will not be available.
1160 const struct imsm_orom
*orom
;
1161 struct sys_dev
*list
, *hba
;
1164 const char *hba_path
;
1168 if (enumerate_only
) {
1169 if (check_env("IMSM_NO_PLATFORM") || find_imsm_orom())
1174 list
= find_driver_devices("pci", "ahci");
1175 for (hba
= list
; hba
; hba
= hba
->next
)
1176 if (devpath_to_vendor(hba
->path
) == 0x8086)
1181 fprintf(stderr
, Name
": unable to find active ahci controller\n");
1182 free_sys_dev(&list
);
1185 fprintf(stderr
, Name
": found Intel SATA AHCI Controller\n");
1186 hba_path
= hba
->path
;
1188 free_sys_dev(&list
);
1190 orom
= find_imsm_orom();
1193 fprintf(stderr
, Name
": imsm option-rom not found\n");
1197 printf(" Platform : Intel(R) Matrix Storage Manager\n");
1198 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
1199 orom
->hotfix_ver
, orom
->build
);
1200 printf(" RAID Levels :%s%s%s%s%s\n",
1201 imsm_orom_has_raid0(orom
) ? " raid0" : "",
1202 imsm_orom_has_raid1(orom
) ? " raid1" : "",
1203 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
1204 imsm_orom_has_raid10(orom
) ? " raid10" : "",
1205 imsm_orom_has_raid5(orom
) ? " raid5" : "");
1206 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1207 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
1208 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
1209 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
1210 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
1211 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
1212 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
1213 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
1214 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
1215 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
1216 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
1217 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
1218 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
1219 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
1220 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
1221 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
1222 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
1223 printf(" Max Disks : %d\n", orom
->tds
);
1224 printf(" Max Volumes : %d\n", orom
->vpa
);
1225 printf(" I/O Controller : %s\n", hba_path
);
1227 /* find the smallest scsi host number to determine a port number base */
1228 dir
= opendir(hba_path
);
1229 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
1232 if (sscanf(ent
->d_name
, "host%d", &host
) != 1)
1234 if (port_count
== 0)
1236 else if (host
< host_base
)
1239 if (host
+ 1 > port_count
+ host_base
)
1240 port_count
= host
+ 1 - host_base
;
1246 if (!port_count
|| imsm_enumerate_ports(hba_path
, port_count
,
1247 host_base
, verbose
) != 0) {
1249 fprintf(stderr
, Name
": failed to enumerate ports\n");
1257 static int match_home_imsm(struct supertype
*st
, char *homehost
)
1259 /* the imsm metadata format does not specify any host
1260 * identification information. We return -1 since we can never
1261 * confirm nor deny whether a given array is "meant" for this
1262 * host. We rely on compare_super and the 'family_num' fields to
1263 * exclude member disks that do not belong, and we rely on
1264 * mdadm.conf to specify the arrays that should be assembled.
1265 * Auto-assembly may still pick up "foreign" arrays.
1271 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
1273 /* The uuid returned here is used for:
1274 * uuid to put into bitmap file (Create, Grow)
1275 * uuid for backup header when saving critical section (Grow)
1276 * comparing uuids when re-adding a device into an array
1277 * In these cases the uuid required is that of the data-array,
1278 * not the device-set.
1279 * uuid to recognise same set when adding a missing device back
1280 * to an array. This is a uuid for the device-set.
1282 * For each of these we can make do with a truncated
1283 * or hashed uuid rather than the original, as long as
1285 * In each case the uuid required is that of the data-array,
1286 * not the device-set.
1288 /* imsm does not track uuid's so we synthesis one using sha1 on
1289 * - The signature (Which is constant for all imsm array, but no matter)
1290 * - the orig_family_num of the container
1291 * - the index number of the volume
1292 * - the 'serial' number of the volume.
1293 * Hopefully these are all constant.
1295 struct intel_super
*super
= st
->sb
;
1298 struct sha1_ctx ctx
;
1299 struct imsm_dev
*dev
= NULL
;
1302 /* some mdadm versions failed to set ->orig_family_num, in which
1303 * case fall back to ->family_num. orig_family_num will be
1304 * fixed up with the first metadata update.
1306 family_num
= super
->anchor
->orig_family_num
;
1307 if (family_num
== 0)
1308 family_num
= super
->anchor
->family_num
;
1309 sha1_init_ctx(&ctx
);
1310 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
1311 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
1312 if (super
->current_vol
>= 0)
1313 dev
= get_imsm_dev(super
, super
->current_vol
);
1315 __u32 vol
= super
->current_vol
;
1316 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
1317 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
1319 sha1_finish_ctx(&ctx
, buf
);
1320 memcpy(uuid
, buf
, 4*4);
1325 get_imsm_numerical_version(struct imsm_super
*mpb
, int *m
, int *p
)
1327 __u8
*v
= get_imsm_version(mpb
);
1328 __u8
*end
= mpb
->sig
+ MAX_SIGNATURE_LENGTH
;
1329 char major
[] = { 0, 0, 0 };
1330 char minor
[] = { 0 ,0, 0 };
1331 char patch
[] = { 0, 0, 0 };
1332 char *ver_parse
[] = { major
, minor
, patch
};
1336 while (*v
!= '\0' && v
< end
) {
1337 if (*v
!= '.' && j
< 2)
1338 ver_parse
[i
][j
++] = *v
;
1346 *m
= strtol(minor
, NULL
, 0);
1347 *p
= strtol(patch
, NULL
, 0);
1351 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
1353 /* migr_strip_size when repairing or initializing parity */
1354 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1355 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1357 switch (get_imsm_raid_level(map
)) {
1362 return 128*1024 >> 9;
1366 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
1368 /* migr_strip_size when rebuilding a degraded disk, no idea why
1369 * this is different than migr_strip_size_resync(), but it's good
1372 struct imsm_map
*map
= get_imsm_map(dev
, 1);
1373 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1375 switch (get_imsm_raid_level(map
)) {
1378 if (map
->num_members
% map
->num_domains
== 0)
1379 return 128*1024 >> 9;
1383 return max((__u32
) 64*1024 >> 9, chunk
);
1385 return 128*1024 >> 9;
1389 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
1391 struct imsm_map
*lo
= get_imsm_map(dev
, 0);
1392 struct imsm_map
*hi
= get_imsm_map(dev
, 1);
1393 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
1394 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
1396 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
1399 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
1401 struct imsm_map
*lo
= get_imsm_map(dev
, 0);
1402 int level
= get_imsm_raid_level(lo
);
1404 if (level
== 1 || level
== 10) {
1405 struct imsm_map
*hi
= get_imsm_map(dev
, 1);
1407 return hi
->num_domains
;
1409 return num_stripes_per_unit_resync(dev
);
1412 static __u8
imsm_num_data_members(struct imsm_dev
*dev
, int second_map
)
1414 /* named 'imsm_' because raid0, raid1 and raid10
1415 * counter-intuitively have the same number of data disks
1417 struct imsm_map
*map
= get_imsm_map(dev
, second_map
);
1419 switch (get_imsm_raid_level(map
)) {
1423 return map
->num_members
;
1425 return map
->num_members
- 1;
1427 dprintf("%s: unsupported raid level\n", __func__
);
1432 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
1434 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1435 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1437 switch(get_imsm_raid_level(map
)) {
1440 return chunk
* map
->num_domains
;
1442 return chunk
* map
->num_members
;
1448 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
1450 struct imsm_map
*map
= get_imsm_map(dev
, 1);
1451 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1452 __u32 strip
= block
/ chunk
;
1454 switch (get_imsm_raid_level(map
)) {
1457 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
1458 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
1460 return vol_stripe
* chunk
+ block
% chunk
;
1462 __u32 stripe
= strip
/ (map
->num_members
- 1);
1464 return stripe
* chunk
+ block
% chunk
;
1471 static __u64
blocks_per_migr_unit(struct imsm_dev
*dev
)
1473 /* calculate the conversion factor between per member 'blocks'
1474 * (md/{resync,rebuild}_start) and imsm migration units, return
1475 * 0 for the 'not migrating' and 'unsupported migration' cases
1477 if (!dev
->vol
.migr_state
)
1480 switch (migr_type(dev
)) {
1484 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1485 __u32 stripes_per_unit
;
1486 __u32 blocks_per_unit
;
1495 /* yes, this is really the translation of migr_units to
1496 * per-member blocks in the 'resync' case
1498 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
1499 migr_chunk
= migr_strip_blocks_resync(dev
);
1500 disks
= imsm_num_data_members(dev
, 0);
1501 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
1502 stripe
= __le32_to_cpu(map
->blocks_per_strip
) * disks
;
1503 segment
= blocks_per_unit
/ stripe
;
1504 block_rel
= blocks_per_unit
- segment
* stripe
;
1505 parity_depth
= parity_segment_depth(dev
);
1506 block_map
= map_migr_block(dev
, block_rel
);
1507 return block_map
+ parity_depth
* segment
;
1509 case MIGR_REBUILD
: {
1510 __u32 stripes_per_unit
;
1513 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
1514 migr_chunk
= migr_strip_blocks_rebuild(dev
);
1515 return migr_chunk
* stripes_per_unit
;
1518 /* FIXME I need a number here */
1519 case MIGR_STATE_CHANGE
:
1525 static int imsm_level_to_layout(int level
)
1533 return ALGORITHM_LEFT_ASYMMETRIC
;
1540 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
1542 struct intel_super
*super
= st
->sb
;
1543 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
1544 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1545 struct imsm_map
*prev_map
= get_imsm_map(dev
, 1);
1548 int map_disks
= info
->array
.raid_disks
;
1550 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
1551 if (dl
->raiddisk
== info
->disk
.raid_disk
)
1553 info
->container_member
= super
->current_vol
;
1554 info
->array
.raid_disks
= map
->num_members
;
1555 info
->array
.level
= get_imsm_raid_level(map
);
1556 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
1557 info
->array
.md_minor
= -1;
1558 info
->array
.ctime
= 0;
1559 info
->array
.utime
= 0;
1560 info
->array
.chunk_size
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
1561 info
->array
.state
= !dev
->vol
.dirty
;
1562 info
->custom_array_size
= __le32_to_cpu(dev
->size_high
);
1563 info
->custom_array_size
<<= 32;
1564 info
->custom_array_size
|= __le32_to_cpu(dev
->size_low
);
1566 info
->disk
.major
= 0;
1567 info
->disk
.minor
= 0;
1569 info
->disk
.major
= dl
->major
;
1570 info
->disk
.minor
= dl
->minor
;
1573 info
->data_offset
= __le32_to_cpu(map
->pba_of_lba0
);
1574 info
->component_size
= __le32_to_cpu(map
->blocks_per_member
);
1575 memset(info
->uuid
, 0, sizeof(info
->uuid
));
1576 info
->recovery_start
= MaxSector
;
1577 info
->reshape_active
= (prev_map
!= NULL
);
1578 if (info
->reshape_active
)
1579 info
->delta_disks
= map
->num_members
- prev_map
->num_members
;
1581 info
->delta_disks
= 0;
1583 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
|| dev
->vol
.dirty
) {
1584 info
->resync_start
= 0;
1585 } else if (dev
->vol
.migr_state
) {
1586 switch (migr_type(dev
)) {
1589 __u64 blocks_per_unit
= blocks_per_migr_unit(dev
);
1590 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
1592 info
->resync_start
= blocks_per_unit
* units
;
1596 /* we could emulate the checkpointing of
1597 * 'sync_action=check' migrations, but for now
1598 * we just immediately complete them
1601 /* this is handled by container_content_imsm() */
1603 case MIGR_STATE_CHANGE
:
1604 /* FIXME handle other migrations */
1606 /* we are not dirty, so... */
1607 info
->resync_start
= MaxSector
;
1610 info
->resync_start
= MaxSector
;
1612 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
1613 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
1615 info
->array
.major_version
= -1;
1616 info
->array
.minor_version
= -2;
1617 devname
= devnum2devname(st
->container_dev
);
1618 *info
->text_version
= '\0';
1620 sprintf(info
->text_version
, "/%s/%d", devname
, info
->container_member
);
1622 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
1623 uuid_from_super_imsm(st
, info
->uuid
);
1627 for (i
=0; i
<map_disks
; i
++) {
1629 if (i
< info
->array
.raid_disks
) {
1630 struct imsm_disk
*dsk
;
1631 j
= get_imsm_disk_idx(dev
, i
, -1);
1632 dsk
= get_imsm_disk(super
, j
);
1633 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
1640 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
, int failed
);
1641 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
);
1643 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
1647 for (d
= super
->missing
; d
; d
= d
->next
)
1648 if (d
->index
== index
)
1653 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
1655 struct intel_super
*super
= st
->sb
;
1656 struct imsm_disk
*disk
;
1657 int map_disks
= info
->array
.raid_disks
;
1658 int max_enough
= -1;
1660 struct imsm_super
*mpb
;
1662 if (super
->current_vol
>= 0) {
1663 getinfo_super_imsm_volume(st
, info
, map
);
1667 /* Set raid_disks to zero so that Assemble will always pull in valid
1670 info
->array
.raid_disks
= 0;
1671 info
->array
.level
= LEVEL_CONTAINER
;
1672 info
->array
.layout
= 0;
1673 info
->array
.md_minor
= -1;
1674 info
->array
.ctime
= 0; /* N/A for imsm */
1675 info
->array
.utime
= 0;
1676 info
->array
.chunk_size
= 0;
1678 info
->disk
.major
= 0;
1679 info
->disk
.minor
= 0;
1680 info
->disk
.raid_disk
= -1;
1681 info
->reshape_active
= 0;
1682 info
->array
.major_version
= -1;
1683 info
->array
.minor_version
= -2;
1684 strcpy(info
->text_version
, "imsm");
1685 info
->safe_mode_delay
= 0;
1686 info
->disk
.number
= -1;
1687 info
->disk
.state
= 0;
1689 info
->recovery_start
= MaxSector
;
1691 /* do we have the all the insync disks that we expect? */
1692 mpb
= super
->anchor
;
1694 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1695 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1696 int failed
, enough
, j
, missing
= 0;
1697 struct imsm_map
*map
;
1700 failed
= imsm_count_failed(super
, dev
);
1701 state
= imsm_check_degraded(super
, dev
, failed
);
1702 map
= get_imsm_map(dev
, dev
->vol
.migr_state
);
1704 /* any newly missing disks?
1705 * (catches single-degraded vs double-degraded)
1707 for (j
= 0; j
< map
->num_members
; j
++) {
1708 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, -1);
1709 __u32 idx
= ord_to_idx(ord
);
1711 if (!(ord
& IMSM_ORD_REBUILD
) &&
1712 get_imsm_missing(super
, idx
)) {
1718 if (state
== IMSM_T_STATE_FAILED
)
1720 else if (state
== IMSM_T_STATE_DEGRADED
&&
1721 (state
!= map
->map_state
|| missing
))
1723 else /* we're normal, or already degraded */
1726 /* in the missing/failed disk case check to see
1727 * if at least one array is runnable
1729 max_enough
= max(max_enough
, enough
);
1731 dprintf("%s: enough: %d\n", __func__
, max_enough
);
1732 info
->container_enough
= max_enough
;
1735 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
1737 disk
= &super
->disks
->disk
;
1738 info
->data_offset
= __le32_to_cpu(disk
->total_blocks
) - reserved
;
1739 info
->component_size
= reserved
;
1740 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
1741 /* we don't change info->disk.raid_disk here because
1742 * this state will be finalized in mdmon after we have
1743 * found the 'most fresh' version of the metadata
1745 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
1746 info
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
1749 /* only call uuid_from_super_imsm when this disk is part of a populated container,
1750 * ->compare_super may have updated the 'num_raid_devs' field for spares
1752 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
1753 uuid_from_super_imsm(st
, info
->uuid
);
1755 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
1757 /* I don't know how to compute 'map' on imsm, so use safe default */
1760 for (i
= 0; i
< map_disks
; i
++)
1766 /* allocates memory and fills disk in mdinfo structure
1767 * for each disk in array */
1768 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
1770 struct mdinfo
*mddev
= NULL
;
1771 struct intel_super
*super
= st
->sb
;
1772 struct imsm_disk
*disk
;
1775 if (!super
|| !super
->disks
)
1778 mddev
= malloc(sizeof(*mddev
));
1780 fprintf(stderr
, Name
": Failed to allocate memory.\n");
1783 memset(mddev
, 0, sizeof(*mddev
));
1787 tmp
= malloc(sizeof(*tmp
));
1789 fprintf(stderr
, Name
": Failed to allocate memory.\n");
1794 memset(tmp
, 0, sizeof(*tmp
));
1796 tmp
->next
= mddev
->devs
;
1798 tmp
->disk
.number
= count
++;
1799 tmp
->disk
.major
= dl
->major
;
1800 tmp
->disk
.minor
= dl
->minor
;
1801 tmp
->disk
.state
= is_configured(disk
) ?
1802 (1 << MD_DISK_ACTIVE
) : 0;
1803 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
1804 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
1805 tmp
->disk
.raid_disk
= -1;
1811 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
1812 char *update
, char *devname
, int verbose
,
1813 int uuid_set
, char *homehost
)
1815 /* For 'assemble' and 'force' we need to return non-zero if any
1816 * change was made. For others, the return value is ignored.
1817 * Update options are:
1818 * force-one : This device looks a bit old but needs to be included,
1819 * update age info appropriately.
1820 * assemble: clear any 'faulty' flag to allow this device to
1822 * force-array: Array is degraded but being forced, mark it clean
1823 * if that will be needed to assemble it.
1825 * newdev: not used ????
1826 * grow: Array has gained a new device - this is currently for
1828 * resync: mark as dirty so a resync will happen.
1829 * name: update the name - preserving the homehost
1830 * uuid: Change the uuid of the array to match watch is given
1832 * Following are not relevant for this imsm:
1833 * sparc2.2 : update from old dodgey metadata
1834 * super-minor: change the preferred_minor number
1835 * summaries: update redundant counters.
1836 * homehost: update the recorded homehost
1837 * _reshape_progress: record new reshape_progress position.
1840 struct intel_super
*super
= st
->sb
;
1841 struct imsm_super
*mpb
;
1843 /* we can only update container info */
1844 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
1847 mpb
= super
->anchor
;
1849 if (strcmp(update
, "uuid") == 0 && uuid_set
&& !info
->update_private
)
1851 else if (strcmp(update
, "uuid") == 0 && uuid_set
&& info
->update_private
) {
1852 mpb
->orig_family_num
= *((__u32
*) info
->update_private
);
1854 } else if (strcmp(update
, "uuid") == 0) {
1855 __u32
*new_family
= malloc(sizeof(*new_family
));
1857 /* update orig_family_number with the incoming random
1858 * data, report the new effective uuid, and store the
1859 * new orig_family_num for future updates.
1862 memcpy(&mpb
->orig_family_num
, info
->uuid
, sizeof(__u32
));
1863 uuid_from_super_imsm(st
, info
->uuid
);
1864 *new_family
= mpb
->orig_family_num
;
1865 info
->update_private
= new_family
;
1868 } else if (strcmp(update
, "assemble") == 0)
1873 /* successful update? recompute checksum */
1875 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
1880 static size_t disks_to_mpb_size(int disks
)
1884 size
= sizeof(struct imsm_super
);
1885 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
1886 size
+= 2 * sizeof(struct imsm_dev
);
1887 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
1888 size
+= (4 - 2) * sizeof(struct imsm_map
);
1889 /* 4 possible disk_ord_tbl's */
1890 size
+= 4 * (disks
- 1) * sizeof(__u32
);
1895 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
)
1897 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
1900 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
1903 static void free_devlist(struct intel_super
*super
)
1905 struct intel_dev
*dv
;
1907 while (super
->devlist
) {
1908 dv
= super
->devlist
->next
;
1909 free(super
->devlist
->dev
);
1910 free(super
->devlist
);
1911 super
->devlist
= dv
;
1915 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
1917 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
1920 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
)
1924 * 0 same, or first was empty, and second was copied
1925 * 1 second had wrong number
1927 * 3 wrong other info
1929 struct intel_super
*first
= st
->sb
;
1930 struct intel_super
*sec
= tst
->sb
;
1938 /* if an anchor does not have num_raid_devs set then it is a free
1941 if (first
->anchor
->num_raid_devs
> 0 &&
1942 sec
->anchor
->num_raid_devs
> 0) {
1943 /* Determine if these disks might ever have been
1944 * related. Further disambiguation can only take place
1945 * in load_super_imsm_all
1947 __u32 first_family
= first
->anchor
->orig_family_num
;
1948 __u32 sec_family
= sec
->anchor
->orig_family_num
;
1950 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
1951 MAX_SIGNATURE_LENGTH
) != 0)
1954 if (first_family
== 0)
1955 first_family
= first
->anchor
->family_num
;
1956 if (sec_family
== 0)
1957 sec_family
= sec
->anchor
->family_num
;
1959 if (first_family
!= sec_family
)
1965 /* if 'first' is a spare promote it to a populated mpb with sec's
1968 if (first
->anchor
->num_raid_devs
== 0 &&
1969 sec
->anchor
->num_raid_devs
> 0) {
1971 struct intel_dev
*dv
;
1972 struct imsm_dev
*dev
;
1974 /* we need to copy raid device info from sec if an allocation
1975 * fails here we don't associate the spare
1977 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++) {
1978 dv
= malloc(sizeof(*dv
));
1981 dev
= malloc(sizeof_imsm_dev(get_imsm_dev(sec
, i
), 1));
1988 dv
->next
= first
->devlist
;
1989 first
->devlist
= dv
;
1991 if (i
< sec
->anchor
->num_raid_devs
) {
1992 /* allocation failure */
1993 free_devlist(first
);
1994 fprintf(stderr
, "imsm: failed to associate spare\n");
1997 first
->anchor
->num_raid_devs
= sec
->anchor
->num_raid_devs
;
1998 first
->anchor
->orig_family_num
= sec
->anchor
->orig_family_num
;
1999 first
->anchor
->family_num
= sec
->anchor
->family_num
;
2000 memcpy(first
->anchor
->sig
, sec
->anchor
->sig
, MAX_SIGNATURE_LENGTH
);
2001 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++)
2002 imsm_copy_dev(get_imsm_dev(first
, i
), get_imsm_dev(sec
, i
));
2008 static void fd2devname(int fd
, char *name
)
2012 char dname
[PATH_MAX
];
2017 if (fstat(fd
, &st
) != 0)
2019 sprintf(path
, "/sys/dev/block/%d:%d",
2020 major(st
.st_rdev
), minor(st
.st_rdev
));
2022 rv
= readlink(path
, dname
, sizeof(dname
));
2027 nm
= strrchr(dname
, '/');
2029 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
2032 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
2034 static int imsm_read_serial(int fd
, char *devname
,
2035 __u8 serial
[MAX_RAID_SERIAL_LEN
])
2037 unsigned char scsi_serial
[255];
2046 memset(scsi_serial
, 0, sizeof(scsi_serial
));
2048 rv
= scsi_get_serial(fd
, scsi_serial
, sizeof(scsi_serial
));
2050 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
2051 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
2052 fd2devname(fd
, (char *) serial
);
2059 Name
": Failed to retrieve serial for %s\n",
2064 rsp_len
= scsi_serial
[3];
2068 Name
": Failed to retrieve serial for %s\n",
2072 rsp_buf
= (char *) &scsi_serial
[4];
2074 /* trim all whitespace and non-printable characters and convert
2077 for (i
= 0, dest
= rsp_buf
; i
< rsp_len
; i
++) {
2080 /* ':' is reserved for use in placeholder serial
2081 * numbers for missing disks
2089 len
= dest
- rsp_buf
;
2092 /* truncate leading characters */
2093 if (len
> MAX_RAID_SERIAL_LEN
) {
2094 dest
+= len
- MAX_RAID_SERIAL_LEN
;
2095 len
= MAX_RAID_SERIAL_LEN
;
2098 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
2099 memcpy(serial
, dest
, len
);
2104 static int serialcmp(__u8
*s1
, __u8
*s2
)
2106 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
2109 static void serialcpy(__u8
*dest
, __u8
*src
)
2111 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
2115 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
2119 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
2120 if (serialcmp(dl
->serial
, serial
) == 0)
2127 static struct imsm_disk
*
2128 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
2132 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2133 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
2135 if (serialcmp(disk
->serial
, serial
) == 0) {
2146 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
2148 struct imsm_disk
*disk
;
2153 __u8 serial
[MAX_RAID_SERIAL_LEN
];
2155 rv
= imsm_read_serial(fd
, devname
, serial
);
2160 dl
= calloc(1, sizeof(*dl
));
2164 Name
": failed to allocate disk buffer for %s\n",
2170 dl
->major
= major(stb
.st_rdev
);
2171 dl
->minor
= minor(stb
.st_rdev
);
2172 dl
->next
= super
->disks
;
2173 dl
->fd
= keep_fd
? fd
: -1;
2174 assert(super
->disks
== NULL
);
2176 serialcpy(dl
->serial
, serial
);
2179 fd2devname(fd
, name
);
2181 dl
->devname
= strdup(devname
);
2183 dl
->devname
= strdup(name
);
2185 /* look up this disk's index in the current anchor */
2186 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
2189 /* only set index on disks that are a member of a
2190 * populated contianer, i.e. one with raid_devs
2192 if (is_failed(&dl
->disk
))
2194 else if (is_spare(&dl
->disk
))
2202 /* When migrating map0 contains the 'destination' state while map1
2203 * contains the current state. When not migrating map0 contains the
2204 * current state. This routine assumes that map[0].map_state is set to
2205 * the current array state before being called.
2207 * Migration is indicated by one of the following states
2208 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
2209 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
2210 * map1state=unitialized)
2211 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
2213 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
2214 * map1state=degraded)
2216 static void migrate(struct imsm_dev
*dev
, __u8 to_state
, int migr_type
)
2218 struct imsm_map
*dest
;
2219 struct imsm_map
*src
= get_imsm_map(dev
, 0);
2221 dev
->vol
.migr_state
= 1;
2222 set_migr_type(dev
, migr_type
);
2223 dev
->vol
.curr_migr_unit
= 0;
2224 dest
= get_imsm_map(dev
, 1);
2226 /* duplicate and then set the target end state in map[0] */
2227 memcpy(dest
, src
, sizeof_imsm_map(src
));
2228 if ((migr_type
== MIGR_REBUILD
) ||
2229 (migr_type
== MIGR_GEN_MIGR
)) {
2233 for (i
= 0; i
< src
->num_members
; i
++) {
2234 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
2235 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
2239 src
->map_state
= to_state
;
2242 static void end_migration(struct imsm_dev
*dev
, __u8 map_state
)
2244 struct imsm_map
*map
= get_imsm_map(dev
, 0);
2245 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
);
2248 /* merge any IMSM_ORD_REBUILD bits that were not successfully
2249 * completed in the last migration.
2251 * FIXME add support for raid-level-migration
2253 for (i
= 0; i
< prev
->num_members
; i
++)
2254 for (j
= 0; j
< map
->num_members
; j
++)
2255 /* during online capacity expansion
2256 * disks position can be changed if takeover is used
2258 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
2259 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
2260 map
->disk_ord_tbl
[j
] |= prev
->disk_ord_tbl
[i
];
2264 dev
->vol
.migr_state
= 0;
2265 dev
->vol
.migr_type
= 0;
2266 dev
->vol
.curr_migr_unit
= 0;
2267 map
->map_state
= map_state
;
2271 static int parse_raid_devices(struct intel_super
*super
)
2274 struct imsm_dev
*dev_new
;
2275 size_t len
, len_migr
;
2276 size_t space_needed
= 0;
2277 struct imsm_super
*mpb
= super
->anchor
;
2279 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
2280 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
2281 struct intel_dev
*dv
;
2283 len
= sizeof_imsm_dev(dev_iter
, 0);
2284 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
2286 space_needed
+= len_migr
- len
;
2288 dv
= malloc(sizeof(*dv
));
2291 dev_new
= malloc(len_migr
);
2296 imsm_copy_dev(dev_new
, dev_iter
);
2299 dv
->next
= super
->devlist
;
2300 super
->devlist
= dv
;
2303 /* ensure that super->buf is large enough when all raid devices
2306 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
2309 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
, 512);
2310 if (posix_memalign(&buf
, 512, len
) != 0)
2313 memcpy(buf
, super
->buf
, super
->len
);
2314 memset(buf
+ super
->len
, 0, len
- super
->len
);
2323 /* retrieve a pointer to the bbm log which starts after all raid devices */
2324 struct bbm_log
*__get_imsm_bbm_log(struct imsm_super
*mpb
)
2328 if (__le32_to_cpu(mpb
->bbm_log_size
)) {
2330 ptr
+= mpb
->mpb_size
- __le32_to_cpu(mpb
->bbm_log_size
);
2336 static void __free_imsm(struct intel_super
*super
, int free_disks
);
2338 /* load_imsm_mpb - read matrix metadata
2339 * allocates super->mpb to be freed by free_super
2341 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
2343 unsigned long long dsize
;
2344 unsigned long long sectors
;
2346 struct imsm_super
*anchor
;
2349 get_dev_size(fd
, NULL
, &dsize
);
2353 Name
": %s: device to small for imsm\n",
2358 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0) {
2361 Name
": Cannot seek to anchor block on %s: %s\n",
2362 devname
, strerror(errno
));
2366 if (posix_memalign((void**)&anchor
, 512, 512) != 0) {
2369 Name
": Failed to allocate imsm anchor buffer"
2370 " on %s\n", devname
);
2373 if (read(fd
, anchor
, 512) != 512) {
2376 Name
": Cannot read anchor block on %s: %s\n",
2377 devname
, strerror(errno
));
2382 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
2385 Name
": no IMSM anchor on %s\n", devname
);
2390 __free_imsm(super
, 0);
2391 super
->len
= ROUND_UP(anchor
->mpb_size
, 512);
2392 if (posix_memalign(&super
->buf
, 512, super
->len
) != 0) {
2395 Name
": unable to allocate %zu byte mpb buffer\n",
2400 memcpy(super
->buf
, anchor
, 512);
2402 sectors
= mpb_sectors(anchor
) - 1;
2405 check_sum
= __gen_imsm_checksum(super
->anchor
);
2406 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
2409 Name
": IMSM checksum %x != %x on %s\n",
2411 __le32_to_cpu(super
->anchor
->check_sum
),
2419 /* read the extended mpb */
2420 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0) {
2423 Name
": Cannot seek to extended mpb on %s: %s\n",
2424 devname
, strerror(errno
));
2428 if ((unsigned)read(fd
, super
->buf
+ 512, super
->len
- 512) != super
->len
- 512) {
2431 Name
": Cannot read extended mpb on %s: %s\n",
2432 devname
, strerror(errno
));
2436 check_sum
= __gen_imsm_checksum(super
->anchor
);
2437 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
2440 Name
": IMSM checksum %x != %x on %s\n",
2441 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
2446 /* FIXME the BBM log is disk specific so we cannot use this global
2447 * buffer for all disks. Ok for now since we only look at the global
2448 * bbm_log_size parameter to gate assembly
2450 super
->bbm_log
= __get_imsm_bbm_log(super
->anchor
);
2456 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
2460 err
= load_imsm_mpb(fd
, super
, devname
);
2463 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
2466 err
= parse_raid_devices(super
);
2471 static void __free_imsm_disk(struct dl
*d
)
2483 static void free_imsm_disks(struct intel_super
*super
)
2487 while (super
->disks
) {
2489 super
->disks
= d
->next
;
2490 __free_imsm_disk(d
);
2492 while (super
->missing
) {
2494 super
->missing
= d
->next
;
2495 __free_imsm_disk(d
);
2500 /* free all the pieces hanging off of a super pointer */
2501 static void __free_imsm(struct intel_super
*super
, int free_disks
)
2508 free_imsm_disks(super
);
2509 free_devlist(super
);
2511 free((void *) super
->hba
);
2516 static void free_imsm(struct intel_super
*super
)
2518 __free_imsm(super
, 1);
2522 static void free_super_imsm(struct supertype
*st
)
2524 struct intel_super
*super
= st
->sb
;
2533 static struct intel_super
*alloc_super(void)
2535 struct intel_super
*super
= malloc(sizeof(*super
));
2538 memset(super
, 0, sizeof(*super
));
2539 super
->current_vol
= -1;
2540 super
->create_offset
= ~((__u32
) 0);
2541 if (!check_env("IMSM_NO_PLATFORM"))
2542 super
->orom
= find_imsm_orom();
2543 if (super
->orom
&& !check_env("IMSM_TEST_OROM")) {
2544 struct sys_dev
*list
, *ent
;
2546 /* find the first intel ahci controller */
2547 list
= find_driver_devices("pci", "ahci");
2548 for (ent
= list
; ent
; ent
= ent
->next
)
2549 if (devpath_to_vendor(ent
->path
) == 0x8086)
2552 super
->hba
= ent
->path
;
2555 free_sys_dev(&list
);
2563 /* find_missing - helper routine for load_super_imsm_all that identifies
2564 * disks that have disappeared from the system. This routine relies on
2565 * the mpb being uptodate, which it is at load time.
2567 static int find_missing(struct intel_super
*super
)
2570 struct imsm_super
*mpb
= super
->anchor
;
2572 struct imsm_disk
*disk
;
2574 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2575 disk
= __get_imsm_disk(mpb
, i
);
2576 dl
= serial_to_dl(disk
->serial
, super
);
2580 dl
= malloc(sizeof(*dl
));
2586 dl
->devname
= strdup("missing");
2588 serialcpy(dl
->serial
, disk
->serial
);
2591 dl
->next
= super
->missing
;
2592 super
->missing
= dl
;
2598 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
2600 struct intel_disk
*idisk
= disk_list
;
2603 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
2605 idisk
= idisk
->next
;
2611 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
2612 struct intel_super
*super
,
2613 struct intel_disk
**disk_list
)
2615 struct imsm_disk
*d
= &super
->disks
->disk
;
2616 struct imsm_super
*mpb
= super
->anchor
;
2619 for (i
= 0; i
< tbl_size
; i
++) {
2620 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
2621 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
2623 if (tbl_mpb
->family_num
== mpb
->family_num
) {
2624 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
2625 dprintf("%s: mpb from %d:%d matches %d:%d\n",
2626 __func__
, super
->disks
->major
,
2627 super
->disks
->minor
,
2628 table
[i
]->disks
->major
,
2629 table
[i
]->disks
->minor
);
2633 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
2634 is_configured(d
) == is_configured(tbl_d
)) &&
2635 tbl_mpb
->generation_num
< mpb
->generation_num
) {
2636 /* current version of the mpb is a
2637 * better candidate than the one in
2638 * super_table, but copy over "cross
2639 * generational" status
2641 struct intel_disk
*idisk
;
2643 dprintf("%s: mpb from %d:%d replaces %d:%d\n",
2644 __func__
, super
->disks
->major
,
2645 super
->disks
->minor
,
2646 table
[i
]->disks
->major
,
2647 table
[i
]->disks
->minor
);
2649 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
2650 if (idisk
&& is_failed(&idisk
->disk
))
2651 tbl_d
->status
|= FAILED_DISK
;
2654 struct intel_disk
*idisk
;
2655 struct imsm_disk
*disk
;
2657 /* tbl_mpb is more up to date, but copy
2658 * over cross generational status before
2661 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
2662 if (disk
&& is_failed(disk
))
2663 d
->status
|= FAILED_DISK
;
2665 idisk
= disk_list_get(d
->serial
, *disk_list
);
2668 if (disk
&& is_configured(disk
))
2669 idisk
->disk
.status
|= CONFIGURED_DISK
;
2672 dprintf("%s: mpb from %d:%d prefer %d:%d\n",
2673 __func__
, super
->disks
->major
,
2674 super
->disks
->minor
,
2675 table
[i
]->disks
->major
,
2676 table
[i
]->disks
->minor
);
2684 table
[tbl_size
++] = super
;
2688 /* update/extend the merged list of imsm_disk records */
2689 for (j
= 0; j
< mpb
->num_disks
; j
++) {
2690 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
2691 struct intel_disk
*idisk
;
2693 idisk
= disk_list_get(disk
->serial
, *disk_list
);
2695 idisk
->disk
.status
|= disk
->status
;
2696 if (is_configured(&idisk
->disk
) ||
2697 is_failed(&idisk
->disk
))
2698 idisk
->disk
.status
&= ~(SPARE_DISK
);
2700 idisk
= calloc(1, sizeof(*idisk
));
2703 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
2704 idisk
->disk
= *disk
;
2705 idisk
->next
= *disk_list
;
2709 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
2716 static struct intel_super
*
2717 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
2720 struct imsm_super
*mpb
= super
->anchor
;
2724 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2725 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
2726 struct intel_disk
*idisk
;
2728 idisk
= disk_list_get(disk
->serial
, disk_list
);
2730 if (idisk
->owner
== owner
||
2731 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
2734 dprintf("%s: '%.16s' owner %d != %d\n",
2735 __func__
, disk
->serial
, idisk
->owner
,
2738 dprintf("%s: unknown disk %x [%d]: %.16s\n",
2739 __func__
, __le32_to_cpu(mpb
->family_num
), i
,
2745 if (ok_count
== mpb
->num_disks
)
2750 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
2752 struct intel_super
*s
;
2754 for (s
= super_list
; s
; s
= s
->next
) {
2755 if (family_num
!= s
->anchor
->family_num
)
2757 fprintf(stderr
, "Conflict, offlining family %#x on '%s'\n",
2758 __le32_to_cpu(family_num
), s
->disks
->devname
);
2762 static struct intel_super
*
2763 imsm_thunderdome(struct intel_super
**super_list
, int len
)
2765 struct intel_super
*super_table
[len
];
2766 struct intel_disk
*disk_list
= NULL
;
2767 struct intel_super
*champion
, *spare
;
2768 struct intel_super
*s
, **del
;
2773 memset(super_table
, 0, sizeof(super_table
));
2774 for (s
= *super_list
; s
; s
= s
->next
)
2775 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
2777 for (i
= 0; i
< tbl_size
; i
++) {
2778 struct imsm_disk
*d
;
2779 struct intel_disk
*idisk
;
2780 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
2783 d
= &s
->disks
->disk
;
2785 /* 'd' must appear in merged disk list for its
2786 * configuration to be valid
2788 idisk
= disk_list_get(d
->serial
, disk_list
);
2789 if (idisk
&& idisk
->owner
== i
)
2790 s
= validate_members(s
, disk_list
, i
);
2795 dprintf("%s: marking family: %#x from %d:%d offline\n",
2796 __func__
, mpb
->family_num
,
2797 super_table
[i
]->disks
->major
,
2798 super_table
[i
]->disks
->minor
);
2802 /* This is where the mdadm implementation differs from the Windows
2803 * driver which has no strict concept of a container. We can only
2804 * assemble one family from a container, so when returning a prodigal
2805 * array member to this system the code will not be able to disambiguate
2806 * the container contents that should be assembled ("foreign" versus
2807 * "local"). It requires user intervention to set the orig_family_num
2808 * to a new value to establish a new container. The Windows driver in
2809 * this situation fixes up the volume name in place and manages the
2810 * foreign array as an independent entity.
2815 for (i
= 0; i
< tbl_size
; i
++) {
2816 struct intel_super
*tbl_ent
= super_table
[i
];
2822 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
2827 if (s
&& !is_spare
) {
2828 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
2830 } else if (!s
&& !is_spare
)
2843 fprintf(stderr
, "Chose family %#x on '%s', "
2844 "assemble conflicts to new container with '--update=uuid'\n",
2845 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
2847 /* collect all dl's onto 'champion', and update them to
2848 * champion's version of the status
2850 for (s
= *super_list
; s
; s
= s
->next
) {
2851 struct imsm_super
*mpb
= champion
->anchor
;
2852 struct dl
*dl
= s
->disks
;
2857 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2858 struct imsm_disk
*disk
;
2860 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
2863 /* only set index on disks that are a member of
2864 * a populated contianer, i.e. one with
2867 if (is_failed(&dl
->disk
))
2869 else if (is_spare(&dl
->disk
))
2875 if (i
>= mpb
->num_disks
) {
2876 struct intel_disk
*idisk
;
2878 idisk
= disk_list_get(dl
->serial
, disk_list
);
2879 if (idisk
&& is_spare(&idisk
->disk
) &&
2880 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
2888 dl
->next
= champion
->disks
;
2889 champion
->disks
= dl
;
2893 /* delete 'champion' from super_list */
2894 for (del
= super_list
; *del
; ) {
2895 if (*del
== champion
) {
2896 *del
= (*del
)->next
;
2899 del
= &(*del
)->next
;
2901 champion
->next
= NULL
;
2905 struct intel_disk
*idisk
= disk_list
;
2907 disk_list
= disk_list
->next
;
2914 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
2918 struct intel_super
*super_list
= NULL
;
2919 struct intel_super
*super
= NULL
;
2920 int devnum
= fd2devnum(fd
);
2926 /* check if 'fd' an opened container */
2927 sra
= sysfs_read(fd
, 0, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
2931 if (sra
->array
.major_version
!= -1 ||
2932 sra
->array
.minor_version
!= -2 ||
2933 strcmp(sra
->text_version
, "imsm") != 0) {
2938 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
2939 struct intel_super
*s
= alloc_super();
2946 s
->next
= super_list
;
2950 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
2951 dfd
= dev_open(nm
, O_RDWR
);
2955 err
= load_and_parse_mpb(dfd
, s
, NULL
, 1);
2957 /* retry the load if we might have raced against mdmon */
2958 if (err
== 3 && mdmon_running(devnum
))
2959 for (retry
= 0; retry
< 3; retry
++) {
2961 err
= load_and_parse_mpb(dfd
, s
, NULL
, 1);
2969 /* all mpbs enter, maybe one leaves */
2970 super
= imsm_thunderdome(&super_list
, i
);
2976 if (find_missing(super
) != 0) {
2984 while (super_list
) {
2985 struct intel_super
*s
= super_list
;
2987 super_list
= super_list
->next
;
2996 st
->container_dev
= devnum
;
2997 if (err
== 0 && st
->ss
== NULL
) {
2998 st
->ss
= &super_imsm
;
2999 st
->minor_version
= 0;
3000 st
->max_devs
= IMSM_MAX_DEVICES
;
3005 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
3007 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
);
3011 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
3013 struct intel_super
*super
;
3017 if (load_super_imsm_all(st
, fd
, &st
->sb
, devname
) == 0)
3021 if (test_partition(fd
))
3022 /* IMSM not allowed on partitions */
3025 free_super_imsm(st
);
3027 super
= alloc_super();
3030 Name
": malloc of %zu failed.\n",
3035 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
3040 Name
": Failed to load all information "
3041 "sections on %s\n", devname
);
3047 if (st
->ss
== NULL
) {
3048 st
->ss
= &super_imsm
;
3049 st
->minor_version
= 0;
3050 st
->max_devs
= IMSM_MAX_DEVICES
;
3055 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
3057 if (info
->level
== 1)
3059 return info
->chunk_size
>> 9;
3062 static __u32
info_to_num_data_stripes(mdu_array_info_t
*info
, int num_domains
)
3066 num_stripes
= (info
->size
* 2) / info_to_blocks_per_strip(info
);
3067 num_stripes
/= num_domains
;
3072 static __u32
info_to_blocks_per_member(mdu_array_info_t
*info
)
3074 if (info
->level
== 1)
3075 return info
->size
* 2;
3077 return (info
->size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
3080 static void imsm_update_version_info(struct intel_super
*super
)
3082 /* update the version and attributes */
3083 struct imsm_super
*mpb
= super
->anchor
;
3085 struct imsm_dev
*dev
;
3086 struct imsm_map
*map
;
3089 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3090 dev
= get_imsm_dev(super
, i
);
3091 map
= get_imsm_map(dev
, 0);
3092 if (__le32_to_cpu(dev
->size_high
) > 0)
3093 mpb
->attributes
|= MPB_ATTRIB_2TB
;
3095 /* FIXME detect when an array spans a port multiplier */
3097 mpb
->attributes
|= MPB_ATTRIB_PM
;
3100 if (mpb
->num_raid_devs
> 1 ||
3101 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
3102 version
= MPB_VERSION_ATTRIBS
;
3103 switch (get_imsm_raid_level(map
)) {
3104 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
3105 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
3106 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
3107 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
3110 if (map
->num_members
>= 5)
3111 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
3112 else if (dev
->status
== DEV_CLONE_N_GO
)
3113 version
= MPB_VERSION_CNG
;
3114 else if (get_imsm_raid_level(map
) == 5)
3115 version
= MPB_VERSION_RAID5
;
3116 else if (map
->num_members
>= 3)
3117 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
3118 else if (get_imsm_raid_level(map
) == 1)
3119 version
= MPB_VERSION_RAID1
;
3121 version
= MPB_VERSION_RAID0
;
3123 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
3127 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
3129 struct imsm_super
*mpb
= super
->anchor
;
3130 char *reason
= NULL
;
3133 if (strlen(name
) > MAX_RAID_SERIAL_LEN
)
3134 reason
= "must be 16 characters or less";
3136 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3137 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3139 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
3140 reason
= "already exists";
3145 if (reason
&& !quiet
)
3146 fprintf(stderr
, Name
": imsm volume name %s\n", reason
);
3151 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
3152 unsigned long long size
, char *name
,
3153 char *homehost
, int *uuid
)
3155 /* We are creating a volume inside a pre-existing container.
3156 * so st->sb is already set.
3158 struct intel_super
*super
= st
->sb
;
3159 struct imsm_super
*mpb
= super
->anchor
;
3160 struct intel_dev
*dv
;
3161 struct imsm_dev
*dev
;
3162 struct imsm_vol
*vol
;
3163 struct imsm_map
*map
;
3164 int idx
= mpb
->num_raid_devs
;
3166 unsigned long long array_blocks
;
3167 size_t size_old
, size_new
;
3168 __u32 num_data_stripes
;
3170 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
3171 fprintf(stderr
, Name
": This imsm-container already has the "
3172 "maximum of %d volumes\n", super
->orom
->vpa
);
3176 /* ensure the mpb is large enough for the new data */
3177 size_old
= __le32_to_cpu(mpb
->mpb_size
);
3178 size_new
= disks_to_mpb_size(info
->nr_disks
);
3179 if (size_new
> size_old
) {
3181 size_t size_round
= ROUND_UP(size_new
, 512);
3183 if (posix_memalign(&mpb_new
, 512, size_round
) != 0) {
3184 fprintf(stderr
, Name
": could not allocate new mpb\n");
3187 memcpy(mpb_new
, mpb
, size_old
);
3190 super
->anchor
= mpb_new
;
3191 mpb
->mpb_size
= __cpu_to_le32(size_new
);
3192 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
3194 super
->current_vol
= idx
;
3195 /* when creating the first raid device in this container set num_disks
3196 * to zero, i.e. delete this spare and add raid member devices in
3197 * add_to_super_imsm_volume()
3199 if (super
->current_vol
== 0)
3202 if (!check_name(super
, name
, 0))
3204 dv
= malloc(sizeof(*dv
));
3206 fprintf(stderr
, Name
": failed to allocate device list entry\n");
3209 dev
= malloc(sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
3212 fprintf(stderr
, Name
": could not allocate raid device\n");
3215 strncpy((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
3216 if (info
->level
== 1)
3217 array_blocks
= info_to_blocks_per_member(info
);
3219 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
3220 info
->layout
, info
->chunk_size
,
3222 /* round array size down to closest MB */
3223 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
3225 dev
->size_low
= __cpu_to_le32((__u32
) array_blocks
);
3226 dev
->size_high
= __cpu_to_le32((__u32
) (array_blocks
>> 32));
3227 dev
->status
= __cpu_to_le32(0);
3228 dev
->reserved_blocks
= __cpu_to_le32(0);
3230 vol
->migr_state
= 0;
3231 set_migr_type(dev
, MIGR_INIT
);
3233 vol
->curr_migr_unit
= 0;
3234 map
= get_imsm_map(dev
, 0);
3235 map
->pba_of_lba0
= __cpu_to_le32(super
->create_offset
);
3236 map
->blocks_per_member
= __cpu_to_le32(info_to_blocks_per_member(info
));
3237 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
3238 map
->failed_disk_num
= ~0;
3239 map
->map_state
= info
->level
? IMSM_T_STATE_UNINITIALIZED
:
3240 IMSM_T_STATE_NORMAL
;
3243 if (info
->level
== 1 && info
->raid_disks
> 2) {
3246 fprintf(stderr
, Name
": imsm does not support more than 2 disks"
3247 "in a raid1 volume\n");
3251 map
->raid_level
= info
->level
;
3252 if (info
->level
== 10) {
3253 map
->raid_level
= 1;
3254 map
->num_domains
= info
->raid_disks
/ 2;
3255 } else if (info
->level
== 1)
3256 map
->num_domains
= info
->raid_disks
;
3258 map
->num_domains
= 1;
3260 num_data_stripes
= info_to_num_data_stripes(info
, map
->num_domains
);
3261 map
->num_data_stripes
= __cpu_to_le32(num_data_stripes
);
3263 map
->num_members
= info
->raid_disks
;
3264 for (i
= 0; i
< map
->num_members
; i
++) {
3265 /* initialized in add_to_super */
3266 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
3268 mpb
->num_raid_devs
++;
3271 dv
->index
= super
->current_vol
;
3272 dv
->next
= super
->devlist
;
3273 super
->devlist
= dv
;
3275 imsm_update_version_info(super
);
3280 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
3281 unsigned long long size
, char *name
,
3282 char *homehost
, int *uuid
)
3284 /* This is primarily called by Create when creating a new array.
3285 * We will then get add_to_super called for each component, and then
3286 * write_init_super called to write it out to each device.
3287 * For IMSM, Create can create on fresh devices or on a pre-existing
3289 * To create on a pre-existing array a different method will be called.
3290 * This one is just for fresh drives.
3292 struct intel_super
*super
;
3293 struct imsm_super
*mpb
;
3298 return init_super_imsm_volume(st
, info
, size
, name
, homehost
, uuid
);
3301 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
3305 super
= alloc_super();
3306 if (super
&& posix_memalign(&super
->buf
, 512, mpb_size
) != 0) {
3311 fprintf(stderr
, Name
3312 ": %s could not allocate superblock\n", __func__
);
3315 memset(super
->buf
, 0, mpb_size
);
3317 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
3321 /* zeroing superblock */
3325 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
3327 version
= (char *) mpb
->sig
;
3328 strcpy(version
, MPB_SIGNATURE
);
3329 version
+= strlen(MPB_SIGNATURE
);
3330 strcpy(version
, MPB_VERSION_RAID0
);
3336 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
3337 int fd
, char *devname
)
3339 struct intel_super
*super
= st
->sb
;
3340 struct imsm_super
*mpb
= super
->anchor
;
3342 struct imsm_dev
*dev
;
3343 struct imsm_map
*map
;
3346 dev
= get_imsm_dev(super
, super
->current_vol
);
3347 map
= get_imsm_map(dev
, 0);
3349 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
3350 fprintf(stderr
, Name
": %s: Cannot add spare devices to IMSM volume\n",
3356 /* we're doing autolayout so grab the pre-marked (in
3357 * validate_geometry) raid_disk
3359 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3360 if (dl
->raiddisk
== dk
->raid_disk
)
3363 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3364 if (dl
->major
== dk
->major
&&
3365 dl
->minor
== dk
->minor
)
3370 fprintf(stderr
, Name
": %s is not a member of the same container\n", devname
);
3374 /* add a pristine spare to the metadata */
3375 if (dl
->index
< 0) {
3376 dl
->index
= super
->anchor
->num_disks
;
3377 super
->anchor
->num_disks
++;
3379 /* Check the device has not already been added */
3380 slot
= get_imsm_disk_slot(map
, dl
->index
);
3382 (get_imsm_ord_tbl_ent(dev
, slot
, -1) & IMSM_ORD_REBUILD
) == 0) {
3383 fprintf(stderr
, Name
": %s has been included in this array twice\n",
3387 set_imsm_ord_tbl_ent(map
, dk
->number
, dl
->index
);
3388 dl
->disk
.status
= CONFIGURED_DISK
;
3390 /* if we are creating the first raid device update the family number */
3391 if (super
->current_vol
== 0) {
3393 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
3394 struct imsm_disk
*_disk
= __get_imsm_disk(mpb
, dl
->index
);
3396 if (!_dev
|| !_disk
) {
3397 fprintf(stderr
, Name
": BUG mpb setup error\n");
3403 sum
+= __gen_imsm_checksum(mpb
);
3404 mpb
->family_num
= __cpu_to_le32(sum
);
3405 mpb
->orig_family_num
= mpb
->family_num
;
3411 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
3412 int fd
, char *devname
)
3414 struct intel_super
*super
= st
->sb
;
3416 unsigned long long size
;
3421 /* if we are on an RAID enabled platform check that the disk is
3422 * attached to the raid controller
3424 if (super
->hba
&& !disk_attached_to_hba(fd
, super
->hba
)) {
3426 Name
": %s is not attached to the raid controller: %s\n",
3427 devname
? : "disk", super
->hba
);
3431 if (super
->current_vol
>= 0)
3432 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
3435 dd
= malloc(sizeof(*dd
));
3438 Name
": malloc failed %s:%d.\n", __func__
, __LINE__
);
3441 memset(dd
, 0, sizeof(*dd
));
3442 dd
->major
= major(stb
.st_rdev
);
3443 dd
->minor
= minor(stb
.st_rdev
);
3445 dd
->devname
= devname
? strdup(devname
) : NULL
;
3448 dd
->action
= DISK_ADD
;
3449 rv
= imsm_read_serial(fd
, devname
, dd
->serial
);
3452 Name
": failed to retrieve scsi serial, aborting\n");
3457 get_dev_size(fd
, NULL
, &size
);
3459 serialcpy(dd
->disk
.serial
, dd
->serial
);
3460 dd
->disk
.total_blocks
= __cpu_to_le32(size
);
3461 dd
->disk
.status
= SPARE_DISK
;
3462 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
3463 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
3465 dd
->disk
.scsi_id
= __cpu_to_le32(0);
3467 if (st
->update_tail
) {
3468 dd
->next
= super
->disk_mgmt_list
;
3469 super
->disk_mgmt_list
= dd
;
3471 dd
->next
= super
->disks
;
3479 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
3481 struct intel_super
*super
= st
->sb
;
3484 /* remove from super works only in mdmon - for communication
3485 * manager - monitor. Check if communication memory buffer
3488 if (!st
->update_tail
) {
3490 Name
": %s shall be used in mdmon context only"
3491 "(line %d).\n", __func__
, __LINE__
);
3494 dd
= malloc(sizeof(*dd
));
3497 Name
": malloc failed %s:%d.\n", __func__
, __LINE__
);
3500 memset(dd
, 0, sizeof(*dd
));
3501 dd
->major
= dk
->major
;
3502 dd
->minor
= dk
->minor
;
3505 dd
->disk
.status
= SPARE_DISK
;
3506 dd
->action
= DISK_REMOVE
;
3508 dd
->next
= super
->disk_mgmt_list
;
3509 super
->disk_mgmt_list
= dd
;
3515 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
3519 struct imsm_super anchor
;
3520 } spare_record
__attribute__ ((aligned(512)));
3522 /* spare records have their own family number and do not have any defined raid
3525 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
3527 struct imsm_super
*mpb
= super
->anchor
;
3528 struct imsm_super
*spare
= &spare_record
.anchor
;
3532 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
)),
3533 spare
->generation_num
= __cpu_to_le32(1UL),
3534 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
3535 spare
->num_disks
= 1,
3536 spare
->num_raid_devs
= 0,
3537 spare
->cache_size
= mpb
->cache_size
,
3538 spare
->pwr_cycle_count
= __cpu_to_le32(1),
3540 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
3541 MPB_SIGNATURE MPB_VERSION_RAID0
);
3543 for (d
= super
->disks
; d
; d
= d
->next
) {
3547 spare
->disk
[0] = d
->disk
;
3548 sum
= __gen_imsm_checksum(spare
);
3549 spare
->family_num
= __cpu_to_le32(sum
);
3550 spare
->orig_family_num
= 0;
3551 sum
= __gen_imsm_checksum(spare
);
3552 spare
->check_sum
= __cpu_to_le32(sum
);
3554 if (store_imsm_mpb(d
->fd
, spare
)) {
3555 fprintf(stderr
, "%s: failed for device %d:%d %s\n",
3556 __func__
, d
->major
, d
->minor
, strerror(errno
));
3568 static int write_super_imsm(struct supertype
*st
, int doclose
)
3570 struct intel_super
*super
= st
->sb
;
3571 struct imsm_super
*mpb
= super
->anchor
;
3577 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
3580 /* 'generation' is incremented everytime the metadata is written */
3581 generation
= __le32_to_cpu(mpb
->generation_num
);
3583 mpb
->generation_num
= __cpu_to_le32(generation
);
3585 /* fix up cases where previous mdadm releases failed to set
3588 if (mpb
->orig_family_num
== 0)
3589 mpb
->orig_family_num
= mpb
->family_num
;
3591 for (d
= super
->disks
; d
; d
= d
->next
) {
3595 mpb
->disk
[d
->index
] = d
->disk
;
3599 for (d
= super
->missing
; d
; d
= d
->next
) {
3600 mpb
->disk
[d
->index
] = d
->disk
;
3603 mpb
->num_disks
= num_disks
;
3604 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
3606 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3607 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
3608 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
3610 imsm_copy_dev(dev
, dev2
);
3611 mpb_size
+= sizeof_imsm_dev(dev
, 0);
3614 mpb_size
+= __le32_to_cpu(mpb
->bbm_log_size
);
3615 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
3617 /* recalculate checksum */
3618 sum
= __gen_imsm_checksum(mpb
);
3619 mpb
->check_sum
= __cpu_to_le32(sum
);
3621 /* write the mpb for disks that compose raid devices */
3622 for (d
= super
->disks
; d
; d
= d
->next
) {
3625 if (store_imsm_mpb(d
->fd
, mpb
))
3626 fprintf(stderr
, "%s: failed for device %d:%d %s\n",
3627 __func__
, d
->major
, d
->minor
, strerror(errno
));
3635 return write_super_imsm_spares(super
, doclose
);
3641 static int create_array(struct supertype
*st
, int dev_idx
)
3644 struct imsm_update_create_array
*u
;
3645 struct intel_super
*super
= st
->sb
;
3646 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
3647 struct imsm_map
*map
= get_imsm_map(dev
, 0);
3648 struct disk_info
*inf
;
3649 struct imsm_disk
*disk
;
3652 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
3653 sizeof(*inf
) * map
->num_members
;
3656 fprintf(stderr
, "%s: failed to allocate update buffer\n",
3661 u
->type
= update_create_array
;
3662 u
->dev_idx
= dev_idx
;
3663 imsm_copy_dev(&u
->dev
, dev
);
3664 inf
= get_disk_info(u
);
3665 for (i
= 0; i
< map
->num_members
; i
++) {
3666 int idx
= get_imsm_disk_idx(dev
, i
, -1);
3668 disk
= get_imsm_disk(super
, idx
);
3669 serialcpy(inf
[i
].serial
, disk
->serial
);
3671 append_metadata_update(st
, u
, len
);
3676 static int mgmt_disk(struct supertype
*st
)
3678 struct intel_super
*super
= st
->sb
;
3680 struct imsm_update_add_remove_disk
*u
;
3682 if (!super
->disk_mgmt_list
)
3688 fprintf(stderr
, "%s: failed to allocate update buffer\n",
3693 u
->type
= update_add_remove_disk
;
3694 append_metadata_update(st
, u
, len
);
3699 static int write_init_super_imsm(struct supertype
*st
)
3701 struct intel_super
*super
= st
->sb
;
3702 int current_vol
= super
->current_vol
;
3704 /* we are done with current_vol reset it to point st at the container */
3705 super
->current_vol
= -1;
3707 if (st
->update_tail
) {
3708 /* queue the recently created array / added disk
3709 * as a metadata update */
3713 /* determine if we are creating a volume or adding a disk */
3714 if (current_vol
< 0) {
3715 /* in the mgmt (add/remove) disk case we are running
3716 * in mdmon context, so don't close fd's
3718 return mgmt_disk(st
);
3720 rv
= create_array(st
, current_vol
);
3722 for (d
= super
->disks
; d
; d
= d
->next
) {
3730 for (d
= super
->disks
; d
; d
= d
->next
)
3731 Kill(d
->devname
, NULL
, 0, 1, 1);
3732 return write_super_imsm(st
, 1);
3737 static int store_super_imsm(struct supertype
*st
, int fd
)
3739 struct intel_super
*super
= st
->sb
;
3740 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
3746 return store_imsm_mpb(fd
, mpb
);
3752 static int imsm_bbm_log_size(struct imsm_super
*mpb
)
3754 return __le32_to_cpu(mpb
->bbm_log_size
);
3758 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
3759 int layout
, int raiddisks
, int chunk
,
3760 unsigned long long size
, char *dev
,
3761 unsigned long long *freesize
,
3765 unsigned long long ldsize
;
3766 const struct imsm_orom
*orom
;
3768 if (level
!= LEVEL_CONTAINER
)
3773 if (check_env("IMSM_NO_PLATFORM"))
3776 orom
= find_imsm_orom();
3777 if (orom
&& raiddisks
> orom
->tds
) {
3779 fprintf(stderr
, Name
": %d exceeds maximum number of"
3780 " platform supported disks: %d\n",
3781 raiddisks
, orom
->tds
);
3785 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
3788 fprintf(stderr
, Name
": imsm: Cannot open %s: %s\n",
3789 dev
, strerror(errno
));
3792 if (!get_dev_size(fd
, dev
, &ldsize
)) {
3798 *freesize
= avail_size_imsm(st
, ldsize
>> 9);
3803 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
3805 const unsigned long long base_start
= e
[*idx
].start
;
3806 unsigned long long end
= base_start
+ e
[*idx
].size
;
3809 if (base_start
== end
)
3813 for (i
= *idx
; i
< num_extents
; i
++) {
3814 /* extend overlapping extents */
3815 if (e
[i
].start
>= base_start
&&
3816 e
[i
].start
<= end
) {
3819 if (e
[i
].start
+ e
[i
].size
> end
)
3820 end
= e
[i
].start
+ e
[i
].size
;
3821 } else if (e
[i
].start
> end
) {
3827 return end
- base_start
;
3830 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
3832 /* build a composite disk with all known extents and generate a new
3833 * 'maxsize' given the "all disks in an array must share a common start
3834 * offset" constraint
3836 struct extent
*e
= calloc(sum_extents
, sizeof(*e
));
3840 unsigned long long pos
;
3841 unsigned long long start
= 0;
3842 unsigned long long maxsize
;
3843 unsigned long reserve
;
3848 /* coalesce and sort all extents. also, check to see if we need to
3849 * reserve space between member arrays
3852 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
3855 for (i
= 0; i
< dl
->extent_cnt
; i
++)
3858 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
3863 while (i
< sum_extents
) {
3864 e
[j
].start
= e
[i
].start
;
3865 e
[j
].size
= find_size(e
, &i
, sum_extents
);
3867 if (e
[j
-1].size
== 0)
3876 unsigned long long esize
;
3878 esize
= e
[i
].start
- pos
;
3879 if (esize
>= maxsize
) {
3884 pos
= e
[i
].start
+ e
[i
].size
;
3886 } while (e
[i
-1].size
);
3892 /* FIXME assumes volume at offset 0 is the first volume in a
3895 if (start_extent
> 0)
3896 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
3900 if (maxsize
< reserve
)
3903 super
->create_offset
= ~((__u32
) 0);
3904 if (start
+ reserve
> super
->create_offset
)
3905 return 0; /* start overflows create_offset */
3906 super
->create_offset
= start
+ reserve
;
3908 return maxsize
- reserve
;
3911 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
3913 if (level
< 0 || level
== 6 || level
== 4)
3916 /* if we have an orom prevent invalid raid levels */
3919 case 0: return imsm_orom_has_raid0(orom
);
3922 return imsm_orom_has_raid1e(orom
);
3923 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
3924 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
3925 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
3928 return 1; /* not on an Intel RAID platform so anything goes */
3933 #define pr_vrb(fmt, arg...) (void) (verbose && fprintf(stderr, Name fmt, ##arg))
3935 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
3936 int raiddisks
, int chunk
, int verbose
)
3938 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
3939 pr_vrb(": platform does not support raid%d with %d disk%s\n",
3940 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
3943 if (super
->orom
&& level
!= 1 &&
3944 !imsm_orom_has_chunk(super
->orom
, chunk
)) {
3945 pr_vrb(": platform does not support a chunk size of: %d\n", chunk
);
3948 if (layout
!= imsm_level_to_layout(level
)) {
3950 pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n");
3951 else if (level
== 10)
3952 pr_vrb(": imsm raid 10 only supports the n2 layout\n");
3954 pr_vrb(": imsm unknown layout %#x for this raid level %d\n",
3962 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
3963 * FIX ME add ahci details
3965 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
3966 int layout
, int raiddisks
, int chunk
,
3967 unsigned long long size
, char *dev
,
3968 unsigned long long *freesize
,
3972 struct intel_super
*super
= st
->sb
;
3973 struct imsm_super
*mpb
= super
->anchor
;
3975 unsigned long long pos
= 0;
3976 unsigned long long maxsize
;
3980 /* We must have the container info already read in. */
3984 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, verbose
))
3988 /* General test: make sure there is space for
3989 * 'raiddisks' device extents of size 'size' at a given
3992 unsigned long long minsize
= size
;
3993 unsigned long long start_offset
= MaxSector
;
3996 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
3997 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
4002 e
= get_extents(super
, dl
);
4005 unsigned long long esize
;
4006 esize
= e
[i
].start
- pos
;
4007 if (esize
>= minsize
)
4009 if (found
&& start_offset
== MaxSector
) {
4012 } else if (found
&& pos
!= start_offset
) {
4016 pos
= e
[i
].start
+ e
[i
].size
;
4018 } while (e
[i
-1].size
);
4023 if (dcnt
< raiddisks
) {
4025 fprintf(stderr
, Name
": imsm: Not enough "
4026 "devices with space for this array "
4034 /* This device must be a member of the set */
4035 if (stat(dev
, &stb
) < 0)
4037 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
4039 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
4040 if (dl
->major
== (int)major(stb
.st_rdev
) &&
4041 dl
->minor
== (int)minor(stb
.st_rdev
))
4046 fprintf(stderr
, Name
": %s is not in the "
4047 "same imsm set\n", dev
);
4049 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
4050 /* If a volume is present then the current creation attempt
4051 * cannot incorporate new spares because the orom may not
4052 * understand this configuration (all member disks must be
4053 * members of each array in the container).
4055 fprintf(stderr
, Name
": %s is a spare and a volume"
4056 " is already defined for this container\n", dev
);
4057 fprintf(stderr
, Name
": The option-rom requires all member"
4058 " disks to be a member of all volumes\n");
4062 /* retrieve the largest free space block */
4063 e
= get_extents(super
, dl
);
4068 unsigned long long esize
;
4070 esize
= e
[i
].start
- pos
;
4071 if (esize
>= maxsize
)
4073 pos
= e
[i
].start
+ e
[i
].size
;
4075 } while (e
[i
-1].size
);
4080 fprintf(stderr
, Name
": unable to determine free space for: %s\n",
4084 if (maxsize
< size
) {
4086 fprintf(stderr
, Name
": %s not enough space (%llu < %llu)\n",
4087 dev
, maxsize
, size
);
4091 /* count total number of extents for merge */
4093 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4095 i
+= dl
->extent_cnt
;
4097 maxsize
= merge_extents(super
, i
);
4098 if (maxsize
< size
|| maxsize
== 0) {
4100 fprintf(stderr
, Name
": not enough space after merge (%llu < %llu)\n",
4105 *freesize
= maxsize
;
4110 static int reserve_space(struct supertype
*st
, int raiddisks
,
4111 unsigned long long size
, int chunk
,
4112 unsigned long long *freesize
)
4114 struct intel_super
*super
= st
->sb
;
4115 struct imsm_super
*mpb
= super
->anchor
;
4120 unsigned long long maxsize
;
4121 unsigned long long minsize
;
4125 /* find the largest common start free region of the possible disks */
4129 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
4135 /* don't activate new spares if we are orom constrained
4136 * and there is already a volume active in the container
4138 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
4141 e
= get_extents(super
, dl
);
4144 for (i
= 1; e
[i
-1].size
; i
++)
4152 maxsize
= merge_extents(super
, extent_cnt
);
4157 if (cnt
< raiddisks
||
4158 (super
->orom
&& used
&& used
!= raiddisks
) ||
4159 maxsize
< minsize
||
4161 fprintf(stderr
, Name
": not enough devices with space to create array.\n");
4162 return 0; /* No enough free spaces large enough */
4174 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4176 dl
->raiddisk
= cnt
++;
4183 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
4184 int raiddisks
, int chunk
, unsigned long long size
,
4185 char *dev
, unsigned long long *freesize
,
4192 /* if given unused devices create a container
4193 * if given given devices in a container create a member volume
4195 if (level
== LEVEL_CONTAINER
) {
4196 /* Must be a fresh device to add to a container */
4197 return validate_geometry_imsm_container(st
, level
, layout
,
4198 raiddisks
, chunk
, size
,
4204 if (st
->sb
&& freesize
) {
4205 /* we are being asked to automatically layout a
4206 * new volume based on the current contents of
4207 * the container. If the the parameters can be
4208 * satisfied reserve_space will record the disks,
4209 * start offset, and size of the volume to be
4210 * created. add_to_super and getinfo_super
4211 * detect when autolayout is in progress.
4213 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
4217 return reserve_space(st
, raiddisks
, size
, chunk
, freesize
);
4222 /* creating in a given container */
4223 return validate_geometry_imsm_volume(st
, level
, layout
,
4224 raiddisks
, chunk
, size
,
4225 dev
, freesize
, verbose
);
4228 /* This device needs to be a device in an 'imsm' container */
4229 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
4233 Name
": Cannot create this array on device %s\n",
4238 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
4240 fprintf(stderr
, Name
": Cannot open %s: %s\n",
4241 dev
, strerror(errno
));
4244 /* Well, it is in use by someone, maybe an 'imsm' container. */
4245 cfd
= open_container(fd
);
4249 fprintf(stderr
, Name
": Cannot use %s: It is busy\n",
4253 sra
= sysfs_read(cfd
, 0, GET_VERSION
);
4254 if (sra
&& sra
->array
.major_version
== -1 &&
4255 strcmp(sra
->text_version
, "imsm") == 0)
4259 /* This is a member of a imsm container. Load the container
4260 * and try to create a volume
4262 struct intel_super
*super
;
4264 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
) == 0) {
4266 st
->container_dev
= fd2devnum(cfd
);
4268 return validate_geometry_imsm_volume(st
, level
, layout
,
4276 fprintf(stderr
, Name
": failed container membership check\n");
4282 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
4284 struct intel_super
*super
= st
->sb
;
4286 if (level
&& *level
== UnSet
)
4287 *level
= LEVEL_CONTAINER
;
4289 if (level
&& layout
&& *layout
== UnSet
)
4290 *layout
= imsm_level_to_layout(*level
);
4292 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0) &&
4293 super
&& super
->orom
)
4294 *chunk
= imsm_orom_default_chunk(super
->orom
);
4297 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
4299 static int kill_subarray_imsm(struct supertype
*st
)
4301 /* remove the subarray currently referenced by ->current_vol */
4303 struct intel_dev
**dp
;
4304 struct intel_super
*super
= st
->sb
;
4305 __u8 current_vol
= super
->current_vol
;
4306 struct imsm_super
*mpb
= super
->anchor
;
4308 if (super
->current_vol
< 0)
4310 super
->current_vol
= -1; /* invalidate subarray cursor */
4312 /* block deletions that would change the uuid of active subarrays
4314 * FIXME when immutable ids are available, but note that we'll
4315 * also need to fixup the invalidated/active subarray indexes in
4318 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4321 if (i
< current_vol
)
4323 sprintf(subarray
, "%u", i
);
4324 if (is_subarray_active(subarray
, st
->devname
)) {
4326 Name
": deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
4333 if (st
->update_tail
) {
4334 struct imsm_update_kill_array
*u
= malloc(sizeof(*u
));
4338 u
->type
= update_kill_array
;
4339 u
->dev_idx
= current_vol
;
4340 append_metadata_update(st
, u
, sizeof(*u
));
4345 for (dp
= &super
->devlist
; *dp
;)
4346 if ((*dp
)->index
== current_vol
) {
4349 handle_missing(super
, (*dp
)->dev
);
4350 if ((*dp
)->index
> current_vol
)
4355 /* no more raid devices, all active components are now spares,
4356 * but of course failed are still failed
4358 if (--mpb
->num_raid_devs
== 0) {
4361 for (d
= super
->disks
; d
; d
= d
->next
)
4362 if (d
->index
> -2) {
4364 d
->disk
.status
= SPARE_DISK
;
4368 super
->updates_pending
++;
4373 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
4374 char *update
, struct mddev_ident
*ident
)
4376 /* update the subarray currently referenced by ->current_vol */
4377 struct intel_super
*super
= st
->sb
;
4378 struct imsm_super
*mpb
= super
->anchor
;
4380 if (strcmp(update
, "name") == 0) {
4381 char *name
= ident
->name
;
4385 if (is_subarray_active(subarray
, st
->devname
)) {
4387 Name
": Unable to update name of active subarray\n");
4391 if (!check_name(super
, name
, 0))
4394 vol
= strtoul(subarray
, &ep
, 10);
4395 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
4398 if (st
->update_tail
) {
4399 struct imsm_update_rename_array
*u
= malloc(sizeof(*u
));
4403 u
->type
= update_rename_array
;
4405 snprintf((char *) u
->name
, MAX_RAID_SERIAL_LEN
, "%s", name
);
4406 append_metadata_update(st
, u
, sizeof(*u
));
4408 struct imsm_dev
*dev
;
4411 dev
= get_imsm_dev(super
, vol
);
4412 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
4413 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4414 dev
= get_imsm_dev(super
, i
);
4415 handle_missing(super
, dev
);
4417 super
->updates_pending
++;
4424 #endif /* MDASSEMBLE */
4426 static int is_gen_migration(struct imsm_dev
*dev
)
4428 if (!dev
->vol
.migr_state
)
4431 if (migr_type(dev
) == MIGR_GEN_MIGR
)
4437 static int is_rebuilding(struct imsm_dev
*dev
)
4439 struct imsm_map
*migr_map
;
4441 if (!dev
->vol
.migr_state
)
4444 if (migr_type(dev
) != MIGR_REBUILD
)
4447 migr_map
= get_imsm_map(dev
, 1);
4449 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
4455 static void update_recovery_start(struct imsm_dev
*dev
, struct mdinfo
*array
)
4457 struct mdinfo
*rebuild
= NULL
;
4461 if (!is_rebuilding(dev
))
4464 /* Find the rebuild target, but punt on the dual rebuild case */
4465 for (d
= array
->devs
; d
; d
= d
->next
)
4466 if (d
->recovery_start
== 0) {
4473 /* (?) none of the disks are marked with
4474 * IMSM_ORD_REBUILD, so assume they are missing and the
4475 * disk_ord_tbl was not correctly updated
4477 dprintf("%s: failed to locate out-of-sync disk\n", __func__
);
4481 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
4482 rebuild
->recovery_start
= units
* blocks_per_migr_unit(dev
);
4486 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
4488 /* Given a container loaded by load_super_imsm_all,
4489 * extract information about all the arrays into
4491 * If 'subarray' is given, just extract info about that array.
4493 * For each imsm_dev create an mdinfo, fill it in,
4494 * then look for matching devices in super->disks
4495 * and create appropriate device mdinfo.
4497 struct intel_super
*super
= st
->sb
;
4498 struct imsm_super
*mpb
= super
->anchor
;
4499 struct mdinfo
*rest
= NULL
;
4503 /* check for bad blocks */
4504 if (imsm_bbm_log_size(super
->anchor
))
4507 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4508 struct imsm_dev
*dev
;
4509 struct imsm_map
*map
;
4510 struct mdinfo
*this;
4515 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
4518 dev
= get_imsm_dev(super
, i
);
4519 map
= get_imsm_map(dev
, 0);
4521 /* do not publish arrays that are in the middle of an
4522 * unsupported migration
4524 if (dev
->vol
.migr_state
&&
4525 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
4526 fprintf(stderr
, Name
": cannot assemble volume '%.16s':"
4527 " unsupported migration in progress\n",
4532 this = malloc(sizeof(*this));
4534 fprintf(stderr
, Name
": failed to allocate %zu bytes\n",
4538 memset(this, 0, sizeof(*this));
4541 super
->current_vol
= i
;
4542 getinfo_super_imsm_volume(st
, this, NULL
);
4543 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
4544 unsigned long long recovery_start
;
4545 struct mdinfo
*info_d
;
4552 idx
= get_imsm_disk_idx(dev
, slot
, 0);
4553 ord
= get_imsm_ord_tbl_ent(dev
, slot
, 0);
4554 for (d
= super
->disks
; d
; d
= d
->next
)
4555 if (d
->index
== idx
)
4558 recovery_start
= MaxSector
;
4561 if (d
&& is_failed(&d
->disk
))
4563 if (ord
& IMSM_ORD_REBUILD
)
4567 * if we skip some disks the array will be assmebled degraded;
4568 * reset resync start to avoid a dirty-degraded
4569 * situation when performing the intial sync
4571 * FIXME handle dirty degraded
4573 if ((skip
|| recovery_start
== 0) && !dev
->vol
.dirty
)
4574 this->resync_start
= MaxSector
;
4578 info_d
= calloc(1, sizeof(*info_d
));
4580 fprintf(stderr
, Name
": failed to allocate disk"
4581 " for volume %.16s\n", dev
->volume
);
4582 info_d
= this->devs
;
4584 struct mdinfo
*d
= info_d
->next
;
4593 info_d
->next
= this->devs
;
4594 this->devs
= info_d
;
4596 info_d
->disk
.number
= d
->index
;
4597 info_d
->disk
.major
= d
->major
;
4598 info_d
->disk
.minor
= d
->minor
;
4599 info_d
->disk
.raid_disk
= slot
;
4600 info_d
->recovery_start
= recovery_start
;
4602 if (info_d
->recovery_start
== MaxSector
)
4603 this->array
.working_disks
++;
4605 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
4606 info_d
->data_offset
= __le32_to_cpu(map
->pba_of_lba0
);
4607 info_d
->component_size
= __le32_to_cpu(map
->blocks_per_member
);
4609 /* now that the disk list is up-to-date fixup recovery_start */
4610 update_recovery_start(dev
, this);
4614 /* if array has bad blocks, set suitable bit in array status */
4616 rest
->array
.state
|= (1<<MD_SB_BBM_ERRORS
);
4622 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
, int failed
)
4624 struct imsm_map
*map
= get_imsm_map(dev
, 0);
4627 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
4628 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
4630 switch (get_imsm_raid_level(map
)) {
4632 return IMSM_T_STATE_FAILED
;
4635 if (failed
< map
->num_members
)
4636 return IMSM_T_STATE_DEGRADED
;
4638 return IMSM_T_STATE_FAILED
;
4643 * check to see if any mirrors have failed, otherwise we
4644 * are degraded. Even numbered slots are mirrored on
4648 /* gcc -Os complains that this is unused */
4649 int insync
= insync
;
4651 for (i
= 0; i
< map
->num_members
; i
++) {
4652 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, -1);
4653 int idx
= ord_to_idx(ord
);
4654 struct imsm_disk
*disk
;
4656 /* reset the potential in-sync count on even-numbered
4657 * slots. num_copies is always 2 for imsm raid10
4662 disk
= get_imsm_disk(super
, idx
);
4663 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
4666 /* no in-sync disks left in this mirror the
4670 return IMSM_T_STATE_FAILED
;
4673 return IMSM_T_STATE_DEGRADED
;
4677 return IMSM_T_STATE_DEGRADED
;
4679 return IMSM_T_STATE_FAILED
;
4685 return map
->map_state
;
4688 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
)
4692 struct imsm_disk
*disk
;
4693 struct imsm_map
*map
= get_imsm_map(dev
, 0);
4694 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
);
4698 /* at the beginning of migration we set IMSM_ORD_REBUILD on
4699 * disks that are being rebuilt. New failures are recorded to
4700 * map[0]. So we look through all the disks we started with and
4701 * see if any failures are still present, or if any new ones
4704 * FIXME add support for online capacity expansion and
4705 * raid-level-migration
4707 for (i
= 0; i
< prev
->num_members
; i
++) {
4708 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
4709 ord
|= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
4710 idx
= ord_to_idx(ord
);
4712 disk
= get_imsm_disk(super
, idx
);
4713 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
4721 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
4724 struct intel_super
*super
= c
->sb
;
4725 struct imsm_super
*mpb
= super
->anchor
;
4727 if (atoi(inst
) >= mpb
->num_raid_devs
) {
4728 fprintf(stderr
, "%s: subarry index %d, out of range\n",
4729 __func__
, atoi(inst
));
4733 dprintf("imsm: open_new %s\n", inst
);
4734 a
->info
.container_member
= atoi(inst
);
4738 static int is_resyncing(struct imsm_dev
*dev
)
4740 struct imsm_map
*migr_map
;
4742 if (!dev
->vol
.migr_state
)
4745 if (migr_type(dev
) == MIGR_INIT
||
4746 migr_type(dev
) == MIGR_REPAIR
)
4749 if (migr_type(dev
) == MIGR_GEN_MIGR
)
4752 migr_map
= get_imsm_map(dev
, 1);
4754 if ((migr_map
->map_state
== IMSM_T_STATE_NORMAL
) &&
4755 (dev
->vol
.migr_type
!= MIGR_GEN_MIGR
))
4761 /* return true if we recorded new information */
4762 static int mark_failure(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
4766 struct imsm_map
*map
;
4768 /* new failures are always set in map[0] */
4769 map
= get_imsm_map(dev
, 0);
4771 slot
= get_imsm_disk_slot(map
, idx
);
4775 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
4776 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
4779 disk
->status
|= FAILED_DISK
;
4780 disk
->status
&= ~CONFIGURED_DISK
;
4781 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
4782 if (map
->failed_disk_num
== 0xff)
4783 map
->failed_disk_num
= slot
;
4787 static void mark_missing(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
4789 mark_failure(dev
, disk
, idx
);
4791 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
4794 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
4795 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
4798 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
4804 if (!super
->missing
)
4806 failed
= imsm_count_failed(super
, dev
);
4807 map_state
= imsm_check_degraded(super
, dev
, failed
);
4809 dprintf("imsm: mark missing\n");
4810 end_migration(dev
, map_state
);
4811 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
4812 mark_missing(dev
, &dl
->disk
, dl
->index
);
4813 super
->updates_pending
++;
4816 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
4818 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
4819 * states are handled in imsm_set_disk() with one exception, when a
4820 * resync is stopped due to a new failure this routine will set the
4821 * 'degraded' state for the array.
4823 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
4825 int inst
= a
->info
.container_member
;
4826 struct intel_super
*super
= a
->container
->sb
;
4827 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
4828 struct imsm_map
*map
= get_imsm_map(dev
, 0);
4829 int failed
= imsm_count_failed(super
, dev
);
4830 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
);
4831 __u32 blocks_per_unit
;
4833 if (dev
->vol
.migr_state
&&
4834 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
4835 /* array state change is blocked due to reshape action
4837 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
4838 * - finish the reshape (if last_checkpoint is big and action != reshape)
4839 * - update curr_migr_unit
4841 if (a
->curr_action
== reshape
) {
4842 /* still reshaping, maybe update curr_migr_unit */
4843 long long blocks_per_unit
= blocks_per_migr_unit(dev
);
4844 long long unit
= a
->last_checkpoint
;
4845 unit
/= blocks_per_unit
;
4846 if (unit
> __le32_to_cpu(dev
->vol
.curr_migr_unit
)) {
4847 dev
->vol
.curr_migr_unit
= __cpu_to_le32(unit
);
4848 super
->updates_pending
++;
4851 if (a
->last_checkpoint
== 0 && a
->prev_action
== reshape
) {
4852 /* for some reason we aborted the reshape.
4855 struct imsm_map
*map2
= get_imsm_map(dev
, 1);
4856 dev
->vol
.migr_state
= 0;
4857 dev
->vol
.migr_type
= 0;
4858 dev
->vol
.curr_migr_unit
= 0;
4859 memcpy(map
, map2
, sizeof_imsm_map(map2
));
4860 super
->updates_pending
++;
4862 if (a
->last_checkpoint
>= a
->info
.component_size
) {
4863 unsigned long long array_blocks
;
4865 /* it seems the reshape is all done */
4866 dev
->vol
.migr_state
= 0;
4867 dev
->vol
.migr_type
= 0;
4868 dev
->vol
.curr_migr_unit
= 0;
4870 used_disks
= imsm_num_data_members(dev
, -1);
4871 array_blocks
= map
->blocks_per_member
* used_disks
;
4872 /* round array size down to closest MB */
4873 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
)
4874 << SECT_PER_MB_SHIFT
;
4875 dev
->size_low
= __cpu_to_le32((__u32
) array_blocks
);
4876 dev
->size_high
= __cpu_to_le32((__u32
) (array_blocks
>> 32));
4877 a
->info
.custom_array_size
= array_blocks
;
4878 a
->check_reshape
= 1; /* encourage manager to update
4881 super
->updates_pending
++;
4887 /* before we activate this array handle any missing disks */
4888 if (consistent
== 2)
4889 handle_missing(super
, dev
);
4891 if (consistent
== 2 &&
4892 (!is_resync_complete(&a
->info
) ||
4893 map_state
!= IMSM_T_STATE_NORMAL
||
4894 dev
->vol
.migr_state
))
4897 if (is_resync_complete(&a
->info
)) {
4898 /* complete intialization / resync,
4899 * recovery and interrupted recovery is completed in
4902 if (is_resyncing(dev
)) {
4903 dprintf("imsm: mark resync done\n");
4904 end_migration(dev
, map_state
);
4905 super
->updates_pending
++;
4906 a
->last_checkpoint
= 0;
4908 } else if (!is_resyncing(dev
) && !failed
) {
4909 /* mark the start of the init process if nothing is failed */
4910 dprintf("imsm: mark resync start\n");
4911 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
4912 migrate(dev
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
4914 migrate(dev
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
4915 super
->updates_pending
++;
4918 /* check if we can update curr_migr_unit from resync_start, recovery_start */
4919 blocks_per_unit
= blocks_per_migr_unit(dev
);
4920 if (blocks_per_unit
) {
4924 units
= a
->last_checkpoint
/ blocks_per_unit
;
4927 /* check that we did not overflow 32-bits, and that
4928 * curr_migr_unit needs updating
4930 if (units32
== units
&&
4931 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
4932 dprintf("imsm: mark checkpoint (%u)\n", units32
);
4933 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
4934 super
->updates_pending
++;
4938 /* mark dirty / clean */
4939 if (dev
->vol
.dirty
!= !consistent
) {
4940 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
4945 super
->updates_pending
++;
4948 /* finalize online capacity expansion/reshape */
4949 if ((a
->curr_action
!= reshape
) &&
4950 (a
->prev_action
== reshape
)) {
4953 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
4954 imsm_set_disk(a
, mdi
->disk
.raid_disk
, mdi
->curr_state
);
4960 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
4962 int inst
= a
->info
.container_member
;
4963 struct intel_super
*super
= a
->container
->sb
;
4964 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
4965 struct imsm_map
*map
= get_imsm_map(dev
, 0);
4966 struct imsm_disk
*disk
;
4971 if (n
> map
->num_members
)
4972 fprintf(stderr
, "imsm: set_disk %d out of range 0..%d\n",
4973 n
, map
->num_members
- 1);
4978 dprintf("imsm: set_disk %d:%x\n", n
, state
);
4980 ord
= get_imsm_ord_tbl_ent(dev
, n
, -1);
4981 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
4983 /* check for new failures */
4984 if (state
& DS_FAULTY
) {
4985 if (mark_failure(dev
, disk
, ord_to_idx(ord
)))
4986 super
->updates_pending
++;
4989 /* check if in_sync */
4990 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
4991 struct imsm_map
*migr_map
= get_imsm_map(dev
, 1);
4993 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
4994 super
->updates_pending
++;
4997 failed
= imsm_count_failed(super
, dev
);
4998 map_state
= imsm_check_degraded(super
, dev
, failed
);
5000 /* check if recovery complete, newly degraded, or failed */
5001 if (map_state
== IMSM_T_STATE_NORMAL
&& is_rebuilding(dev
)) {
5002 end_migration(dev
, map_state
);
5003 map
= get_imsm_map(dev
, 0);
5004 map
->failed_disk_num
= ~0;
5005 super
->updates_pending
++;
5006 a
->last_checkpoint
= 0;
5007 } else if (map_state
== IMSM_T_STATE_DEGRADED
&&
5008 map
->map_state
!= map_state
&&
5009 !dev
->vol
.migr_state
) {
5010 dprintf("imsm: mark degraded\n");
5011 map
->map_state
= map_state
;
5012 super
->updates_pending
++;
5013 a
->last_checkpoint
= 0;
5014 } else if (map_state
== IMSM_T_STATE_FAILED
&&
5015 map
->map_state
!= map_state
) {
5016 dprintf("imsm: mark failed\n");
5017 end_migration(dev
, map_state
);
5018 super
->updates_pending
++;
5019 a
->last_checkpoint
= 0;
5020 } else if (is_gen_migration(dev
)) {
5021 dprintf("imsm: Detected General Migration in state: ");
5022 if (map_state
== IMSM_T_STATE_NORMAL
) {
5023 end_migration(dev
, map_state
);
5024 map
= get_imsm_map(dev
, 0);
5025 map
->failed_disk_num
= ~0;
5026 dprintf("normal\n");
5028 if (map_state
== IMSM_T_STATE_DEGRADED
) {
5029 printf("degraded\n");
5030 end_migration(dev
, map_state
);
5032 dprintf("failed\n");
5034 map
->map_state
= map_state
;
5036 super
->updates_pending
++;
5040 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
5043 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
5044 unsigned long long dsize
;
5045 unsigned long long sectors
;
5047 get_dev_size(fd
, NULL
, &dsize
);
5049 if (mpb_size
> 512) {
5050 /* -1 to account for anchor */
5051 sectors
= mpb_sectors(mpb
) - 1;
5053 /* write the extended mpb to the sectors preceeding the anchor */
5054 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0)
5057 if ((unsigned long long)write(fd
, buf
+ 512, 512 * sectors
)
5062 /* first block is stored on second to last sector of the disk */
5063 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0)
5066 if (write(fd
, buf
, 512) != 512)
5072 static void imsm_sync_metadata(struct supertype
*container
)
5074 struct intel_super
*super
= container
->sb
;
5076 dprintf("sync metadata: %d\n", super
->updates_pending
);
5077 if (!super
->updates_pending
)
5080 write_super_imsm(container
, 0);
5082 super
->updates_pending
= 0;
5085 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
5087 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
5088 int i
= get_imsm_disk_idx(dev
, idx
, -1);
5091 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5095 if (dl
&& is_failed(&dl
->disk
))
5099 dprintf("%s: found %x:%x\n", __func__
, dl
->major
, dl
->minor
);
5104 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
5105 struct active_array
*a
, int activate_new
,
5106 struct mdinfo
*additional_test_list
)
5108 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
5109 int idx
= get_imsm_disk_idx(dev
, slot
, -1);
5110 struct imsm_super
*mpb
= super
->anchor
;
5111 struct imsm_map
*map
;
5112 unsigned long long pos
;
5117 __u32 array_start
= 0;
5118 __u32 array_end
= 0;
5120 struct mdinfo
*test_list
;
5122 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5123 /* If in this array, skip */
5124 for (d
= a
->info
.devs
; d
; d
= d
->next
)
5125 if (d
->state_fd
>= 0 &&
5126 d
->disk
.major
== dl
->major
&&
5127 d
->disk
.minor
== dl
->minor
) {
5128 dprintf("%x:%x already in array\n",
5129 dl
->major
, dl
->minor
);
5134 test_list
= additional_test_list
;
5136 if (test_list
->disk
.major
== dl
->major
&&
5137 test_list
->disk
.minor
== dl
->minor
) {
5138 dprintf("%x:%x already in additional test list\n",
5139 dl
->major
, dl
->minor
);
5142 test_list
= test_list
->next
;
5147 /* skip in use or failed drives */
5148 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
5150 dprintf("%x:%x status (failed: %d index: %d)\n",
5151 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
5155 /* skip pure spares when we are looking for partially
5156 * assimilated drives
5158 if (dl
->index
== -1 && !activate_new
)
5161 /* Does this unused device have the requisite free space?
5162 * It needs to be able to cover all member volumes
5164 ex
= get_extents(super
, dl
);
5166 dprintf("cannot get extents\n");
5169 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5170 dev
= get_imsm_dev(super
, i
);
5171 map
= get_imsm_map(dev
, 0);
5173 /* check if this disk is already a member of
5176 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
5182 array_start
= __le32_to_cpu(map
->pba_of_lba0
);
5183 array_end
= array_start
+
5184 __le32_to_cpu(map
->blocks_per_member
) - 1;
5187 /* check that we can start at pba_of_lba0 with
5188 * blocks_per_member of space
5190 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
5194 pos
= ex
[j
].start
+ ex
[j
].size
;
5196 } while (ex
[j
-1].size
);
5203 if (i
< mpb
->num_raid_devs
) {
5204 dprintf("%x:%x does not have %u to %u available\n",
5205 dl
->major
, dl
->minor
, array_start
, array_end
);
5216 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
5218 struct imsm_dev
*dev2
;
5219 struct imsm_map
*map
;
5225 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
5227 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
);
5228 if (state
== IMSM_T_STATE_FAILED
) {
5229 map
= get_imsm_map(dev2
, 0);
5232 for (slot
= 0; slot
< map
->num_members
; slot
++) {
5234 * Check if failed disks are deleted from intel
5235 * disk list or are marked to be deleted
5237 idx
= get_imsm_disk_idx(dev2
, slot
, -1);
5238 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
5240 * Do not rebuild the array if failed disks
5241 * from failed sub-array are not removed from
5245 is_failed(&idisk
->disk
) &&
5246 (idisk
->action
!= DISK_REMOVE
))
5254 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
5255 struct metadata_update
**updates
)
5258 * Find a device with unused free space and use it to replace a
5259 * failed/vacant region in an array. We replace failed regions one a
5260 * array at a time. The result is that a new spare disk will be added
5261 * to the first failed array and after the monitor has finished
5262 * propagating failures the remainder will be consumed.
5264 * FIXME add a capability for mdmon to request spares from another
5268 struct intel_super
*super
= a
->container
->sb
;
5269 int inst
= a
->info
.container_member
;
5270 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
5271 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5272 int failed
= a
->info
.array
.raid_disks
;
5273 struct mdinfo
*rv
= NULL
;
5276 struct metadata_update
*mu
;
5278 struct imsm_update_activate_spare
*u
;
5283 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
5284 if ((d
->curr_state
& DS_FAULTY
) &&
5286 /* wait for Removal to happen */
5288 if (d
->state_fd
>= 0)
5292 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
5293 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
5295 if (dev
->vol
.migr_state
&&
5296 dev
->vol
.migr_type
== MIGR_GEN_MIGR
)
5297 /* No repair during migration */
5300 if (imsm_check_degraded(super
, dev
, failed
) != IMSM_T_STATE_DEGRADED
)
5304 * If there are any failed disks check state of the other volume.
5305 * Block rebuild if the another one is failed until failed disks
5306 * are removed from container.
5309 dprintf("found failed disks in %s, check if there another"
5310 "failed sub-array.\n",
5312 /* check if states of the other volumes allow for rebuild */
5313 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
5315 allowed
= imsm_rebuild_allowed(a
->container
,
5323 /* For each slot, if it is not working, find a spare */
5324 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
5325 for (d
= a
->info
.devs
; d
; d
= d
->next
)
5326 if (d
->disk
.raid_disk
== i
)
5328 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
5329 if (d
&& (d
->state_fd
>= 0))
5333 * OK, this device needs recovery. Try to re-add the
5334 * previous occupant of this slot, if this fails see if
5335 * we can continue the assimilation of a spare that was
5336 * partially assimilated, finally try to activate a new
5339 dl
= imsm_readd(super
, i
, a
);
5341 dl
= imsm_add_spare(super
, i
, a
, 0, NULL
);
5343 dl
= imsm_add_spare(super
, i
, a
, 1, NULL
);
5347 /* found a usable disk with enough space */
5348 di
= malloc(sizeof(*di
));
5351 memset(di
, 0, sizeof(*di
));
5353 /* dl->index will be -1 in the case we are activating a
5354 * pristine spare. imsm_process_update() will create a
5355 * new index in this case. Once a disk is found to be
5356 * failed in all member arrays it is kicked from the
5359 di
->disk
.number
= dl
->index
;
5361 /* (ab)use di->devs to store a pointer to the device
5364 di
->devs
= (struct mdinfo
*) dl
;
5366 di
->disk
.raid_disk
= i
;
5367 di
->disk
.major
= dl
->major
;
5368 di
->disk
.minor
= dl
->minor
;
5370 di
->recovery_start
= 0;
5371 di
->data_offset
= __le32_to_cpu(map
->pba_of_lba0
);
5372 di
->component_size
= a
->info
.component_size
;
5373 di
->container_member
= inst
;
5374 super
->random
= random32();
5378 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
5379 i
, di
->data_offset
);
5385 /* No spares found */
5387 /* Now 'rv' has a list of devices to return.
5388 * Create a metadata_update record to update the
5389 * disk_ord_tbl for the array
5391 mu
= malloc(sizeof(*mu
));
5393 mu
->buf
= malloc(sizeof(struct imsm_update_activate_spare
) * num_spares
);
5394 if (mu
->buf
== NULL
) {
5401 struct mdinfo
*n
= rv
->next
;
5410 mu
->space_list
= NULL
;
5411 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
5412 mu
->next
= *updates
;
5413 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
5415 for (di
= rv
; di
; di
= di
->next
) {
5416 u
->type
= update_activate_spare
;
5417 u
->dl
= (struct dl
*) di
->devs
;
5419 u
->slot
= di
->disk
.raid_disk
;
5430 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
5432 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
5433 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5434 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, 0);
5435 struct disk_info
*inf
= get_disk_info(u
);
5436 struct imsm_disk
*disk
;
5440 for (i
= 0; i
< map
->num_members
; i
++) {
5441 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, -1));
5442 for (j
= 0; j
< new_map
->num_members
; j
++)
5443 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
5451 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
5453 struct dl
*dl
= NULL
;
5454 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5455 if ((dl
->major
== major
) && (dl
->minor
== minor
))
5460 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
5462 struct dl
*prev
= NULL
;
5466 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5467 if ((dl
->major
== major
) && (dl
->minor
== minor
)) {
5470 prev
->next
= dl
->next
;
5472 super
->disks
= dl
->next
;
5474 __free_imsm_disk(dl
);
5475 dprintf("%s: removed %x:%x\n",
5476 __func__
, major
, minor
);
5484 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
5486 static int add_remove_disk_update(struct intel_super
*super
)
5488 int check_degraded
= 0;
5489 struct dl
*disk
= NULL
;
5490 /* add/remove some spares to/from the metadata/contrainer */
5491 while (super
->disk_mgmt_list
) {
5492 struct dl
*disk_cfg
;
5494 disk_cfg
= super
->disk_mgmt_list
;
5495 super
->disk_mgmt_list
= disk_cfg
->next
;
5496 disk_cfg
->next
= NULL
;
5498 if (disk_cfg
->action
== DISK_ADD
) {
5499 disk_cfg
->next
= super
->disks
;
5500 super
->disks
= disk_cfg
;
5502 dprintf("%s: added %x:%x\n",
5503 __func__
, disk_cfg
->major
,
5505 } else if (disk_cfg
->action
== DISK_REMOVE
) {
5506 dprintf("Disk remove action processed: %x.%x\n",
5507 disk_cfg
->major
, disk_cfg
->minor
);
5508 disk
= get_disk_super(super
,
5512 /* store action status */
5513 disk
->action
= DISK_REMOVE
;
5514 /* remove spare disks only */
5515 if (disk
->index
== -1) {
5516 remove_disk_super(super
,
5521 /* release allocate disk structure */
5522 __free_imsm_disk(disk_cfg
);
5525 return check_degraded
;
5528 static void imsm_process_update(struct supertype
*st
,
5529 struct metadata_update
*update
)
5532 * crack open the metadata_update envelope to find the update record
5533 * update can be one of:
5534 * update_reshape_container_disks - all the arrays in the container
5535 * are being reshaped to have more devices. We need to mark
5536 * the arrays for general migration and convert selected spares
5537 * into active devices.
5538 * update_activate_spare - a spare device has replaced a failed
5539 * device in an array, update the disk_ord_tbl. If this disk is
5540 * present in all member arrays then also clear the SPARE_DISK
5542 * update_create_array
5544 * update_rename_array
5545 * update_add_remove_disk
5547 struct intel_super
*super
= st
->sb
;
5548 struct imsm_super
*mpb
;
5549 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
5551 /* update requires a larger buf but the allocation failed */
5552 if (super
->next_len
&& !super
->next_buf
) {
5553 super
->next_len
= 0;
5557 if (super
->next_buf
) {
5558 memcpy(super
->next_buf
, super
->buf
, super
->len
);
5560 super
->len
= super
->next_len
;
5561 super
->buf
= super
->next_buf
;
5563 super
->next_len
= 0;
5564 super
->next_buf
= NULL
;
5567 mpb
= super
->anchor
;
5570 case update_reshape_container_disks
: {
5571 struct imsm_update_reshape
*u
= (void *)update
->buf
;
5572 struct dl
*new_disk
;
5573 struct intel_dev
*id
;
5575 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
5576 void **tofree
= NULL
;
5578 dprintf("imsm: imsm_process_update() for update_reshape\n");
5580 /* enable spares to use in array */
5581 for (i
= 0; i
< delta_disks
; i
++) {
5582 new_disk
= get_disk_super(super
,
5583 major(u
->new_disks
[i
]),
5584 minor(u
->new_disks
[i
]));
5585 if (new_disk
== NULL
|| new_disk
->index
< 0)
5586 goto update_reshape_exit
;
5588 new_disk
->index
= mpb
->num_disks
++;
5589 /* slot to fill in autolayout */
5590 new_disk
->raiddisk
= new_disk
->index
;
5591 new_disk
->disk
.status
|=
5593 new_disk
->disk
.status
&= ~SPARE_DISK
;
5596 dprintf("imsm: process_update(): update_reshape: volume set"\
5597 " mpb->num_raid_devs = %i\n", mpb
->num_raid_devs
);
5598 /* manage changes in volumes
5600 for (id
= super
->devlist
; id
; id
= id
->next
) {
5601 void **sp
= update
->space_list
;
5602 struct imsm_dev
*newdev
;
5603 struct imsm_map
*newmap
, *oldmap
;
5607 update
->space_list
= *sp
;
5609 /* Copy the dev, but not (all of) the map */
5610 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
5611 oldmap
= get_imsm_map(id
->dev
, 0);
5612 newmap
= get_imsm_map(newdev
, 0);
5613 /* Copy the current map */
5614 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
5615 newdev
->vol
.migr_state
= 1;
5616 newdev
->vol
.curr_migr_unit
= 0;
5617 newdev
->vol
.migr_type
= MIGR_GEN_MIGR
;
5618 newmap
->num_members
= u
->new_raid_disks
;
5619 for (i
= 0; i
< delta_disks
; i
++) {
5620 set_imsm_ord_tbl_ent(newmap
,
5621 u
->old_raid_disks
+ i
,
5622 u
->old_raid_disks
+ i
);
5624 /* New map is correct, now need to save old map */
5625 oldmap
= get_imsm_map(newdev
, 1);
5626 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
5628 sp
= (void **)id
->dev
;
5634 update
->space_list
= tofree
;
5635 super
->updates_pending
++;
5636 update_reshape_exit
:
5639 case update_activate_spare
: {
5640 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
5641 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
5642 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5643 struct imsm_map
*migr_map
;
5644 struct active_array
*a
;
5645 struct imsm_disk
*disk
;
5650 int victim
= get_imsm_disk_idx(dev
, u
->slot
, -1);
5653 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5658 fprintf(stderr
, "error: imsm_activate_spare passed "
5659 "an unknown disk (index: %d)\n",
5664 super
->updates_pending
++;
5666 /* count failures (excluding rebuilds and the victim)
5667 * to determine map[0] state
5670 for (i
= 0; i
< map
->num_members
; i
++) {
5673 disk
= get_imsm_disk(super
,
5674 get_imsm_disk_idx(dev
, i
, -1));
5675 if (!disk
|| is_failed(disk
))
5679 /* adding a pristine spare, assign a new index */
5680 if (dl
->index
< 0) {
5681 dl
->index
= super
->anchor
->num_disks
;
5682 super
->anchor
->num_disks
++;
5685 disk
->status
|= CONFIGURED_DISK
;
5686 disk
->status
&= ~SPARE_DISK
;
5689 to_state
= imsm_check_degraded(super
, dev
, failed
);
5690 map
->map_state
= IMSM_T_STATE_DEGRADED
;
5691 migrate(dev
, to_state
, MIGR_REBUILD
);
5692 migr_map
= get_imsm_map(dev
, 1);
5693 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
5694 set_imsm_ord_tbl_ent(migr_map
, u
->slot
, dl
->index
| IMSM_ORD_REBUILD
);
5696 /* update the family_num to mark a new container
5697 * generation, being careful to record the existing
5698 * family_num in orig_family_num to clean up after
5699 * earlier mdadm versions that neglected to set it.
5701 if (mpb
->orig_family_num
== 0)
5702 mpb
->orig_family_num
= mpb
->family_num
;
5703 mpb
->family_num
+= super
->random
;
5705 /* count arrays using the victim in the metadata */
5707 for (a
= st
->arrays
; a
; a
= a
->next
) {
5708 dev
= get_imsm_dev(super
, a
->info
.container_member
);
5709 map
= get_imsm_map(dev
, 0);
5711 if (get_imsm_disk_slot(map
, victim
) >= 0)
5715 /* delete the victim if it is no longer being
5721 /* We know that 'manager' isn't touching anything,
5722 * so it is safe to delete
5724 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
5725 if ((*dlp
)->index
== victim
)
5728 /* victim may be on the missing list */
5730 for (dlp
= &super
->missing
; *dlp
; dlp
= &(*dlp
)->next
)
5731 if ((*dlp
)->index
== victim
)
5733 imsm_delete(super
, dlp
, victim
);
5737 case update_create_array
: {
5738 /* someone wants to create a new array, we need to be aware of
5739 * a few races/collisions:
5740 * 1/ 'Create' called by two separate instances of mdadm
5741 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
5742 * devices that have since been assimilated via
5744 * In the event this update can not be carried out mdadm will
5745 * (FIX ME) notice that its update did not take hold.
5747 struct imsm_update_create_array
*u
= (void *) update
->buf
;
5748 struct intel_dev
*dv
;
5749 struct imsm_dev
*dev
;
5750 struct imsm_map
*map
, *new_map
;
5751 unsigned long long start
, end
;
5752 unsigned long long new_start
, new_end
;
5754 struct disk_info
*inf
;
5757 /* handle racing creates: first come first serve */
5758 if (u
->dev_idx
< mpb
->num_raid_devs
) {
5759 dprintf("%s: subarray %d already defined\n",
5760 __func__
, u
->dev_idx
);
5764 /* check update is next in sequence */
5765 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
5766 dprintf("%s: can not create array %d expected index %d\n",
5767 __func__
, u
->dev_idx
, mpb
->num_raid_devs
);
5771 new_map
= get_imsm_map(&u
->dev
, 0);
5772 new_start
= __le32_to_cpu(new_map
->pba_of_lba0
);
5773 new_end
= new_start
+ __le32_to_cpu(new_map
->blocks_per_member
);
5774 inf
= get_disk_info(u
);
5776 /* handle activate_spare versus create race:
5777 * check to make sure that overlapping arrays do not include
5780 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5781 dev
= get_imsm_dev(super
, i
);
5782 map
= get_imsm_map(dev
, 0);
5783 start
= __le32_to_cpu(map
->pba_of_lba0
);
5784 end
= start
+ __le32_to_cpu(map
->blocks_per_member
);
5785 if ((new_start
>= start
&& new_start
<= end
) ||
5786 (start
>= new_start
&& start
<= new_end
))
5791 if (disks_overlap(super
, i
, u
)) {
5792 dprintf("%s: arrays overlap\n", __func__
);
5797 /* check that prepare update was successful */
5798 if (!update
->space
) {
5799 dprintf("%s: prepare update failed\n", __func__
);
5803 /* check that all disks are still active before committing
5804 * changes. FIXME: could we instead handle this by creating a
5805 * degraded array? That's probably not what the user expects,
5806 * so better to drop this update on the floor.
5808 for (i
= 0; i
< new_map
->num_members
; i
++) {
5809 dl
= serial_to_dl(inf
[i
].serial
, super
);
5811 dprintf("%s: disk disappeared\n", __func__
);
5816 super
->updates_pending
++;
5818 /* convert spares to members and fixup ord_tbl */
5819 for (i
= 0; i
< new_map
->num_members
; i
++) {
5820 dl
= serial_to_dl(inf
[i
].serial
, super
);
5821 if (dl
->index
== -1) {
5822 dl
->index
= mpb
->num_disks
;
5824 dl
->disk
.status
|= CONFIGURED_DISK
;
5825 dl
->disk
.status
&= ~SPARE_DISK
;
5827 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
5832 update
->space
= NULL
;
5833 imsm_copy_dev(dev
, &u
->dev
);
5834 dv
->index
= u
->dev_idx
;
5835 dv
->next
= super
->devlist
;
5836 super
->devlist
= dv
;
5837 mpb
->num_raid_devs
++;
5839 imsm_update_version_info(super
);
5842 /* mdmon knows how to release update->space, but not
5843 * ((struct intel_dev *) update->space)->dev
5845 if (update
->space
) {
5851 case update_kill_array
: {
5852 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
5853 int victim
= u
->dev_idx
;
5854 struct active_array
*a
;
5855 struct intel_dev
**dp
;
5856 struct imsm_dev
*dev
;
5858 /* sanity check that we are not affecting the uuid of
5859 * active arrays, or deleting an active array
5861 * FIXME when immutable ids are available, but note that
5862 * we'll also need to fixup the invalidated/active
5863 * subarray indexes in mdstat
5865 for (a
= st
->arrays
; a
; a
= a
->next
)
5866 if (a
->info
.container_member
>= victim
)
5868 /* by definition if mdmon is running at least one array
5869 * is active in the container, so checking
5870 * mpb->num_raid_devs is just extra paranoia
5872 dev
= get_imsm_dev(super
, victim
);
5873 if (a
|| !dev
|| mpb
->num_raid_devs
== 1) {
5874 dprintf("failed to delete subarray-%d\n", victim
);
5878 for (dp
= &super
->devlist
; *dp
;)
5879 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
5882 if ((*dp
)->index
> (unsigned)victim
)
5886 mpb
->num_raid_devs
--;
5887 super
->updates_pending
++;
5890 case update_rename_array
: {
5891 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
5892 char name
[MAX_RAID_SERIAL_LEN
+1];
5893 int target
= u
->dev_idx
;
5894 struct active_array
*a
;
5895 struct imsm_dev
*dev
;
5897 /* sanity check that we are not affecting the uuid of
5900 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
5901 name
[MAX_RAID_SERIAL_LEN
] = '\0';
5902 for (a
= st
->arrays
; a
; a
= a
->next
)
5903 if (a
->info
.container_member
== target
)
5905 dev
= get_imsm_dev(super
, u
->dev_idx
);
5906 if (a
|| !dev
|| !check_name(super
, name
, 1)) {
5907 dprintf("failed to rename subarray-%d\n", target
);
5911 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
5912 super
->updates_pending
++;
5915 case update_add_remove_disk
: {
5916 /* we may be able to repair some arrays if disks are
5917 * being added, check teh status of add_remove_disk
5918 * if discs has been added.
5920 if (add_remove_disk_update(super
)) {
5921 struct active_array
*a
;
5923 super
->updates_pending
++;
5924 for (a
= st
->arrays
; a
; a
= a
->next
)
5925 a
->check_degraded
= 1;
5930 fprintf(stderr
, "error: unsuported process update type:"
5931 "(type: %d)\n", type
);
5935 static void imsm_prepare_update(struct supertype
*st
,
5936 struct metadata_update
*update
)
5939 * Allocate space to hold new disk entries, raid-device entries or a new
5940 * mpb if necessary. The manager synchronously waits for updates to
5941 * complete in the monitor, so new mpb buffers allocated here can be
5942 * integrated by the monitor thread without worrying about live pointers
5943 * in the manager thread.
5945 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
5946 struct intel_super
*super
= st
->sb
;
5947 struct imsm_super
*mpb
= super
->anchor
;
5952 case update_reshape_container_disks
: {
5953 /* Every raid device in the container is about to
5954 * gain some more devices, and we will enter a
5956 * So each 'imsm_map' will be bigger, and the imsm_vol
5957 * will now hold 2 of them.
5958 * Thus we need new 'struct imsm_dev' allocations sized
5959 * as sizeof_imsm_dev but with more devices in both maps.
5961 struct imsm_update_reshape
*u
= (void *)update
->buf
;
5962 struct intel_dev
*dl
;
5963 void **space_tail
= (void**)&update
->space_list
;
5965 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
5967 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
5968 int size
= sizeof_imsm_dev(dl
->dev
, 1);
5970 size
+= sizeof(__u32
) * 2 *
5971 (u
->new_raid_disks
- u
->old_raid_disks
);
5980 len
= disks_to_mpb_size(u
->new_raid_disks
);
5981 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
5984 case update_create_array
: {
5985 struct imsm_update_create_array
*u
= (void *) update
->buf
;
5986 struct intel_dev
*dv
;
5987 struct imsm_dev
*dev
= &u
->dev
;
5988 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5990 struct disk_info
*inf
;
5994 inf
= get_disk_info(u
);
5995 len
= sizeof_imsm_dev(dev
, 1);
5996 /* allocate a new super->devlist entry */
5997 dv
= malloc(sizeof(*dv
));
5999 dv
->dev
= malloc(len
);
6004 update
->space
= NULL
;
6008 /* count how many spares will be converted to members */
6009 for (i
= 0; i
< map
->num_members
; i
++) {
6010 dl
= serial_to_dl(inf
[i
].serial
, super
);
6012 /* hmm maybe it failed?, nothing we can do about
6017 if (count_memberships(dl
, super
) == 0)
6020 len
+= activate
* sizeof(struct imsm_disk
);
6027 /* check if we need a larger metadata buffer */
6028 if (super
->next_buf
)
6029 buf_len
= super
->next_len
;
6031 buf_len
= super
->len
;
6033 if (__le32_to_cpu(mpb
->mpb_size
) + len
> buf_len
) {
6034 /* ok we need a larger buf than what is currently allocated
6035 * if this allocation fails process_update will notice that
6036 * ->next_len is set and ->next_buf is NULL
6038 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + len
, 512);
6039 if (super
->next_buf
)
6040 free(super
->next_buf
);
6042 super
->next_len
= buf_len
;
6043 if (posix_memalign(&super
->next_buf
, 512, buf_len
) == 0)
6044 memset(super
->next_buf
, 0, buf_len
);
6046 super
->next_buf
= NULL
;
6050 /* must be called while manager is quiesced */
6051 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
6053 struct imsm_super
*mpb
= super
->anchor
;
6055 struct imsm_dev
*dev
;
6056 struct imsm_map
*map
;
6057 int i
, j
, num_members
;
6060 dprintf("%s: deleting device[%d] from imsm_super\n",
6063 /* shift all indexes down one */
6064 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
6065 if (iter
->index
> (int)index
)
6067 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
6068 if (iter
->index
> (int)index
)
6071 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6072 dev
= get_imsm_dev(super
, i
);
6073 map
= get_imsm_map(dev
, 0);
6074 num_members
= map
->num_members
;
6075 for (j
= 0; j
< num_members
; j
++) {
6076 /* update ord entries being careful not to propagate
6077 * ord-flags to the first map
6079 ord
= get_imsm_ord_tbl_ent(dev
, j
, -1);
6081 if (ord_to_idx(ord
) <= index
)
6084 map
= get_imsm_map(dev
, 0);
6085 set_imsm_ord_tbl_ent(map
, j
, ord_to_idx(ord
- 1));
6086 map
= get_imsm_map(dev
, 1);
6088 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
6093 super
->updates_pending
++;
6095 struct dl
*dl
= *dlp
;
6097 *dlp
= (*dlp
)->next
;
6098 __free_imsm_disk(dl
);
6101 #endif /* MDASSEMBLE */
6103 static char disk_by_path
[] = "/dev/disk/by-path/";
6105 static const char *imsm_get_disk_controller_domain(const char *path
)
6107 struct sys_dev
*list
, *hba
= NULL
;
6108 char disk_path
[PATH_MAX
];
6112 list
= find_driver_devices("pci", "ahci");
6113 for (hba
= list
; hba
; hba
= hba
->next
)
6114 if (devpath_to_vendor(hba
->path
) == 0x8086)
6120 strncpy(disk_path
, disk_by_path
, PATH_MAX
- 1);
6121 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
6122 if (stat(disk_path
, &st
) == 0) {
6123 dpath
= devt_to_devpath(st
.st_rdev
);
6125 ahci
= path_attached_to_hba(dpath
, hba
->path
);
6128 dprintf("path: %s(%s) hba: %s attached: %d\n",
6129 path
, dpath
, (hba
) ? hba
->path
: "NULL", ahci
);
6130 free_sys_dev(&list
);
6137 static int imsm_find_array_minor_by_subdev(int subdev
, int container
, int *minor
)
6139 char subdev_name
[20];
6140 struct mdstat_ent
*mdstat
;
6142 sprintf(subdev_name
, "%d", subdev
);
6143 mdstat
= mdstat_by_subdev(subdev_name
, container
);
6147 *minor
= mdstat
->devnum
;
6148 free_mdstat(mdstat
);
6152 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
6153 struct geo_params
*geo
,
6154 int *old_raid_disks
)
6157 struct mdinfo
*info
, *member
;
6158 int devices_that_can_grow
= 0;
6160 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): "
6161 "st->devnum = (%i)\n",
6164 if (geo
->size
!= -1 ||
6165 geo
->level
!= UnSet
||
6166 geo
->layout
!= UnSet
||
6167 geo
->chunksize
!= 0 ||
6168 geo
->raid_disks
== UnSet
) {
6169 dprintf("imsm: Container operation is allowed for "
6170 "raid disks number change only.\n");
6174 info
= container_content_imsm(st
, NULL
);
6175 for (member
= info
; member
; member
= member
->next
) {
6179 dprintf("imsm: checking device_num: %i\n",
6180 member
->container_member
);
6182 if (geo
->raid_disks
< member
->array
.raid_disks
) {
6183 /* we work on container for Online Capacity Expansion
6184 * only so raid_disks has to grow
6186 dprintf("imsm: for container operation raid disks "
6187 "increase is required\n");
6191 if ((info
->array
.level
!= 0) &&
6192 (info
->array
.level
!= 5)) {
6193 /* we cannot use this container with other raid level
6195 dprintf("imsm: for container operation wrong"\
6196 " raid level (%i) detected\n",
6200 /* check for platform support
6201 * for this raid level configuration
6203 struct intel_super
*super
= st
->sb
;
6204 if (!is_raid_level_supported(super
->orom
,
6205 member
->array
.level
,
6207 dprintf("platform does not support raid%d with"\
6211 geo
->raid_disks
> 1 ? "s" : "");
6216 if (*old_raid_disks
&&
6217 info
->array
.raid_disks
!= *old_raid_disks
)
6219 *old_raid_disks
= info
->array
.raid_disks
;
6221 /* All raid5 and raid0 volumes in container
6222 * have to be ready for Online Capacity Expansion
6223 * so they need to be assembled. We have already
6224 * checked that no recovery etc is happening.
6226 result
= imsm_find_array_minor_by_subdev(member
->container_member
,
6230 dprintf("imsm: cannot find array\n");
6233 devices_that_can_grow
++;
6236 if (!member
&& devices_that_can_grow
)
6240 dprintf("\tContainer operation allowed\n");
6242 dprintf("\tError: %i\n", ret_val
);
6247 /* Function: get_spares_for_grow
6248 * Description: Allocates memory and creates list of spare devices
6249 * avaliable in container. Checks if spare drive size is acceptable.
6250 * Parameters: Pointer to the supertype structure
6251 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
6254 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
6256 unsigned long long min_size
= min_acceptable_spare_size_imsm(st
);
6257 return container_choose_spares(st
, min_size
, NULL
, NULL
, NULL
, 0);
6260 /******************************************************************************
6261 * function: imsm_create_metadata_update_for_reshape
6262 * Function creates update for whole IMSM container.
6264 ******************************************************************************/
6265 static int imsm_create_metadata_update_for_reshape(
6266 struct supertype
*st
,
6267 struct geo_params
*geo
,
6269 struct imsm_update_reshape
**updatep
)
6271 struct intel_super
*super
= st
->sb
;
6272 struct imsm_super
*mpb
= super
->anchor
;
6273 int update_memory_size
= 0;
6274 struct imsm_update_reshape
*u
= NULL
;
6275 struct mdinfo
*spares
= NULL
;
6277 int delta_disks
= 0;
6279 dprintf("imsm_update_metadata_for_reshape(enter) raid_disks = %i\n",
6282 delta_disks
= geo
->raid_disks
- old_raid_disks
;
6284 /* size of all update data without anchor */
6285 update_memory_size
= sizeof(struct imsm_update_reshape
);
6287 /* now add space for spare disks that we need to add. */
6288 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
6290 u
= calloc(1, update_memory_size
);
6293 "cannot get memory for imsm_update_reshape update\n");
6296 u
->type
= update_reshape_container_disks
;
6297 u
->old_raid_disks
= old_raid_disks
;
6298 u
->new_raid_disks
= geo
->raid_disks
;
6300 /* now get spare disks list
6302 spares
= get_spares_for_grow(st
);
6305 || delta_disks
> spares
->array
.spare_disks
) {
6306 dprintf("imsm: ERROR: Cannot get spare devices.\n");
6310 /* we have got spares
6311 * update disk list in imsm_disk list table in anchor
6313 dprintf("imsm: %i spares are available.\n\n",
6314 spares
->array
.spare_disks
);
6316 for (i
= 0; i
< delta_disks
; i
++) {
6317 struct mdinfo
*dev
= spares
->devs
;
6320 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
6322 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
6323 dl
->index
= mpb
->num_disks
++;
6325 /* Now update the metadata so that container_content will find
6328 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6330 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
6331 struct imsm_map
*map
= get_imsm_map(dev
, 0);
6332 map
->num_members
= geo
->raid_disks
;
6333 for (d
= 0; d
< delta_disks
; d
++) {
6334 set_imsm_ord_tbl_ent(map
, old_raid_disks
+ d
,
6335 mpb
->num_disks
- delta_disks
+ d
);
6344 if (i
== delta_disks
) {
6346 return update_memory_size
;
6354 static int imsm_reshape_super(struct supertype
*st
, long long size
, int level
,
6355 int layout
, int chunksize
, int raid_disks
,
6356 char *backup
, char *dev
, int verbouse
)
6358 /* currently we only support increasing the number of devices
6359 * for a container. This increases the number of device for each
6360 * member array. They must all be RAID0 or RAID5.
6364 struct geo_params geo
;
6366 dprintf("imsm: reshape_super called.\n");
6368 memset(&geo
, sizeof(struct geo_params
), 0);
6373 geo
.layout
= layout
;
6374 geo
.chunksize
= chunksize
;
6375 geo
.raid_disks
= raid_disks
;
6377 dprintf("\tfor level : %i\n", geo
.level
);
6378 dprintf("\tfor raid_disks : %i\n", geo
.raid_disks
);
6380 if (experimental() == 0)
6383 /* verify reshape conditions
6384 * on container level we can only increase number of devices. */
6385 if (st
->container_dev
== st
->devnum
) {
6386 /* check for delta_disks > 0
6387 *and supported raid levels 0 and 5 only in container */
6388 int old_raid_disks
= 0;
6389 if (imsm_reshape_is_allowed_on_container(
6390 st
, &geo
, &old_raid_disks
)) {
6391 struct imsm_update_reshape
*u
= NULL
;
6394 len
= imsm_create_metadata_update_for_reshape(
6395 st
, &geo
, old_raid_disks
, &u
);
6399 append_metadata_update(st
, u
, len
);
6401 dprintf("imsm: Cannot prepare "\
6404 dprintf("imsm: Operation is not allowed "\
6405 "on this container\n");
6407 dprintf("imsm: not a container operation\n");
6409 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
6413 struct superswitch super_imsm
= {
6415 .examine_super
= examine_super_imsm
,
6416 .brief_examine_super
= brief_examine_super_imsm
,
6417 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
6418 .export_examine_super
= export_examine_super_imsm
,
6419 .detail_super
= detail_super_imsm
,
6420 .brief_detail_super
= brief_detail_super_imsm
,
6421 .write_init_super
= write_init_super_imsm
,
6422 .validate_geometry
= validate_geometry_imsm
,
6423 .add_to_super
= add_to_super_imsm
,
6424 .remove_from_super
= remove_from_super_imsm
,
6425 .detail_platform
= detail_platform_imsm
,
6426 .kill_subarray
= kill_subarray_imsm
,
6427 .update_subarray
= update_subarray_imsm
,
6428 .load_container
= load_container_imsm
,
6430 .match_home
= match_home_imsm
,
6431 .uuid_from_super
= uuid_from_super_imsm
,
6432 .getinfo_super
= getinfo_super_imsm
,
6433 .getinfo_super_disks
= getinfo_super_disks_imsm
,
6434 .update_super
= update_super_imsm
,
6436 .avail_size
= avail_size_imsm
,
6437 .min_acceptable_spare_size
= min_acceptable_spare_size_imsm
,
6439 .compare_super
= compare_super_imsm
,
6441 .load_super
= load_super_imsm
,
6442 .init_super
= init_super_imsm
,
6443 .store_super
= store_super_imsm
,
6444 .free_super
= free_super_imsm
,
6445 .match_metadata_desc
= match_metadata_desc_imsm
,
6446 .container_content
= container_content_imsm
,
6447 .default_geometry
= default_geometry_imsm
,
6448 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
6449 .reshape_super
= imsm_reshape_super
,
6456 .open_new
= imsm_open_new
,
6457 .set_array_state
= imsm_set_array_state
,
6458 .set_disk
= imsm_set_disk
,
6459 .sync_metadata
= imsm_sync_metadata
,
6460 .activate_spare
= imsm_activate_spare
,
6461 .process_update
= imsm_process_update
,
6462 .prepare_update
= imsm_prepare_update
,
6463 #endif /* MDASSEMBLE */