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
;
237 enum sys_dev_type type
;
240 struct intel_hba
*next
;
247 /* internal representation of IMSM metadata */
250 void *buf
; /* O_DIRECT buffer for reading/writing metadata */
251 struct imsm_super
*anchor
; /* immovable parameters */
253 size_t len
; /* size of the 'buf' allocation */
254 void *next_buf
; /* for realloc'ing buf from the manager */
256 int updates_pending
; /* count of pending updates for mdmon */
257 int current_vol
; /* index of raid device undergoing creation */
258 __u32 create_offset
; /* common start for 'current_vol' */
259 __u32 random
; /* random data for seeding new family numbers */
260 struct intel_dev
*devlist
;
264 __u8 serial
[MAX_RAID_SERIAL_LEN
];
267 struct imsm_disk disk
;
270 struct extent
*e
; /* for determining freespace @ create */
271 int raiddisk
; /* slot to fill in autolayout */
274 struct dl
*disk_mgmt_list
; /* list of disks to add/remove while mdmon
276 struct dl
*missing
; /* disks removed while we weren't looking */
277 struct bbm_log
*bbm_log
;
278 struct intel_hba
*hba
; /* device path of the raid controller for this metadata */
279 const struct imsm_orom
*orom
; /* platform firmware support */
280 struct intel_super
*next
; /* (temp) list for disambiguating family_num */
284 struct imsm_disk disk
;
285 #define IMSM_UNKNOWN_OWNER (-1)
287 struct intel_disk
*next
;
291 unsigned long long start
, size
;
294 /* definitions of reshape process types */
295 enum imsm_reshape_type
{
300 /* definition of messages passed to imsm_process_update */
301 enum imsm_update_type
{
302 update_activate_spare
,
306 update_add_remove_disk
,
307 update_reshape_container_disks
,
311 struct imsm_update_activate_spare
{
312 enum imsm_update_type type
;
316 struct imsm_update_activate_spare
*next
;
329 enum takeover_direction
{
333 struct imsm_update_takeover
{
334 enum imsm_update_type type
;
336 enum takeover_direction direction
;
339 struct imsm_update_reshape
{
340 enum imsm_update_type type
;
343 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
347 __u8 serial
[MAX_RAID_SERIAL_LEN
];
350 struct imsm_update_create_array
{
351 enum imsm_update_type type
;
356 struct imsm_update_kill_array
{
357 enum imsm_update_type type
;
361 struct imsm_update_rename_array
{
362 enum imsm_update_type type
;
363 __u8 name
[MAX_RAID_SERIAL_LEN
];
367 struct imsm_update_add_remove_disk
{
368 enum imsm_update_type type
;
372 static const char *_sys_dev_type
[] = {
373 [SYS_DEV_UNKNOWN
] = "Unknown",
374 [SYS_DEV_SAS
] = "SAS",
375 [SYS_DEV_SATA
] = "SATA"
378 const char *get_sys_dev_type(enum sys_dev_type type
)
380 if (type
>= SYS_DEV_MAX
)
381 type
= SYS_DEV_UNKNOWN
;
383 return _sys_dev_type
[type
];
387 static struct intel_hba
* alloc_intel_hba(struct sys_dev
*device
)
389 struct intel_hba
*result
= malloc(sizeof(*result
));
391 result
->type
= device
->type
;
392 result
->path
= strdup(device
->path
);
394 if (result
->path
&& (result
->pci_id
= strrchr(result
->path
, '/')) != NULL
)
400 static struct intel_hba
* find_intel_hba(struct intel_hba
*hba
, struct sys_dev
*device
)
402 struct intel_hba
*result
=NULL
;
403 for (result
= hba
; result
; result
= result
->next
) {
404 if (result
->type
== device
->type
&& strcmp(result
->path
, device
->path
) == 0)
411 static int attach_hba_to_super(struct intel_super
*super
, struct sys_dev
*device
)
413 struct intel_hba
*hba
;
415 /* check if disk attached to Intel HBA */
416 hba
= find_intel_hba(super
->hba
, device
);
419 /* Check if HBA is already attached to super */
420 if (super
->hba
== NULL
) {
421 super
->hba
= alloc_intel_hba(device
);
426 /* Intel metadata allows for all disks attached to the same type HBA.
427 * Do not sypport odf HBA types mixing
429 if (device
->type
!= hba
->type
)
435 hba
->next
= alloc_intel_hba(device
);
439 static struct sys_dev
* find_disk_attached_hba(int fd
, const char *devname
)
441 struct sys_dev
*list
, *elem
, *prev
;
444 if ((list
= find_intel_devices()) == NULL
)
448 disk_path
= (char *) devname
;
450 disk_path
= diskfd_to_devpath(fd
);
457 for (prev
= NULL
, elem
= list
; elem
; prev
= elem
, elem
= elem
->next
) {
458 if (path_attached_to_hba(disk_path
, elem
->path
)) {
462 prev
->next
= elem
->next
;
464 if (disk_path
!= devname
)
470 if (disk_path
!= devname
)
476 #endif /* MDASSEMBLE */
479 static int find_intel_hba_capability(int fd
, struct intel_super
*super
,
482 static struct supertype
*match_metadata_desc_imsm(char *arg
)
484 struct supertype
*st
;
486 if (strcmp(arg
, "imsm") != 0 &&
487 strcmp(arg
, "default") != 0
491 st
= malloc(sizeof(*st
));
494 memset(st
, 0, sizeof(*st
));
495 st
->container_dev
= NoMdDev
;
496 st
->ss
= &super_imsm
;
497 st
->max_devs
= IMSM_MAX_DEVICES
;
498 st
->minor_version
= 0;
504 static __u8
*get_imsm_version(struct imsm_super
*mpb
)
506 return &mpb
->sig
[MPB_SIG_LEN
];
510 /* retrieve a disk directly from the anchor when the anchor is known to be
511 * up-to-date, currently only at load time
513 static struct imsm_disk
*__get_imsm_disk(struct imsm_super
*mpb
, __u8 index
)
515 if (index
>= mpb
->num_disks
)
517 return &mpb
->disk
[index
];
520 /* retrieve the disk description based on a index of the disk
523 static struct dl
*get_imsm_dl_disk(struct intel_super
*super
, __u8 index
)
527 for (d
= super
->disks
; d
; d
= d
->next
)
528 if (d
->index
== index
)
533 /* retrieve a disk from the parsed metadata */
534 static struct imsm_disk
*get_imsm_disk(struct intel_super
*super
, __u8 index
)
538 dl
= get_imsm_dl_disk(super
, index
);
545 /* generate a checksum directly from the anchor when the anchor is known to be
546 * up-to-date, currently only at load or write_super after coalescing
548 static __u32
__gen_imsm_checksum(struct imsm_super
*mpb
)
550 __u32 end
= mpb
->mpb_size
/ sizeof(end
);
551 __u32
*p
= (__u32
*) mpb
;
555 sum
+= __le32_to_cpu(*p
);
559 return sum
- __le32_to_cpu(mpb
->check_sum
);
562 static size_t sizeof_imsm_map(struct imsm_map
*map
)
564 return sizeof(struct imsm_map
) + sizeof(__u32
) * (map
->num_members
- 1);
567 struct imsm_map
*get_imsm_map(struct imsm_dev
*dev
, int second_map
)
569 /* A device can have 2 maps if it is in the middle of a migration.
571 * 0 - we return the first map
572 * 1 - we return the second map if it exists, else NULL
573 * -1 - we return the second map if it exists, else the first
575 struct imsm_map
*map
= &dev
->vol
.map
[0];
577 if (second_map
== 1 && !dev
->vol
.migr_state
)
579 else if (second_map
== 1 ||
580 (second_map
< 0 && dev
->vol
.migr_state
)) {
583 return ptr
+ sizeof_imsm_map(map
);
589 /* return the size of the device.
590 * migr_state increases the returned size if map[0] were to be duplicated
592 static size_t sizeof_imsm_dev(struct imsm_dev
*dev
, int migr_state
)
594 size_t size
= sizeof(*dev
) - sizeof(struct imsm_map
) +
595 sizeof_imsm_map(get_imsm_map(dev
, 0));
597 /* migrating means an additional map */
598 if (dev
->vol
.migr_state
)
599 size
+= sizeof_imsm_map(get_imsm_map(dev
, 1));
601 size
+= sizeof_imsm_map(get_imsm_map(dev
, 0));
607 /* retrieve disk serial number list from a metadata update */
608 static struct disk_info
*get_disk_info(struct imsm_update_create_array
*update
)
611 struct disk_info
*inf
;
613 inf
= u
+ sizeof(*update
) - sizeof(struct imsm_dev
) +
614 sizeof_imsm_dev(&update
->dev
, 0);
620 static struct imsm_dev
*__get_imsm_dev(struct imsm_super
*mpb
, __u8 index
)
626 if (index
>= mpb
->num_raid_devs
)
629 /* devices start after all disks */
630 offset
= ((void *) &mpb
->disk
[mpb
->num_disks
]) - _mpb
;
632 for (i
= 0; i
<= index
; i
++)
634 return _mpb
+ offset
;
636 offset
+= sizeof_imsm_dev(_mpb
+ offset
, 0);
641 static struct imsm_dev
*get_imsm_dev(struct intel_super
*super
, __u8 index
)
643 struct intel_dev
*dv
;
645 if (index
>= super
->anchor
->num_raid_devs
)
647 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
648 if (dv
->index
== index
)
656 * == 1 get second map
657 * == -1 than get map according to the current migr_state
659 static __u32
get_imsm_ord_tbl_ent(struct imsm_dev
*dev
,
663 struct imsm_map
*map
;
665 map
= get_imsm_map(dev
, second_map
);
667 /* top byte identifies disk under rebuild */
668 return __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
671 #define ord_to_idx(ord) (((ord) << 8) >> 8)
672 static __u32
get_imsm_disk_idx(struct imsm_dev
*dev
, int slot
, int second_map
)
674 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, second_map
);
676 return ord_to_idx(ord
);
679 static void set_imsm_ord_tbl_ent(struct imsm_map
*map
, int slot
, __u32 ord
)
681 map
->disk_ord_tbl
[slot
] = __cpu_to_le32(ord
);
684 static int get_imsm_disk_slot(struct imsm_map
*map
, unsigned idx
)
689 for (slot
= 0; slot
< map
->num_members
; slot
++) {
690 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
691 if (ord_to_idx(ord
) == idx
)
698 static int get_imsm_raid_level(struct imsm_map
*map
)
700 if (map
->raid_level
== 1) {
701 if (map
->num_members
== 2)
707 return map
->raid_level
;
710 static int cmp_extent(const void *av
, const void *bv
)
712 const struct extent
*a
= av
;
713 const struct extent
*b
= bv
;
714 if (a
->start
< b
->start
)
716 if (a
->start
> b
->start
)
721 static int count_memberships(struct dl
*dl
, struct intel_super
*super
)
726 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
727 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
728 struct imsm_map
*map
= get_imsm_map(dev
, 0);
730 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
737 static struct extent
*get_extents(struct intel_super
*super
, struct dl
*dl
)
739 /* find a list of used extents on the given physical device */
740 struct extent
*rv
, *e
;
742 int memberships
= count_memberships(dl
, super
);
743 __u32 reservation
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
745 rv
= malloc(sizeof(struct extent
) * (memberships
+ 1));
750 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
751 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
752 struct imsm_map
*map
= get_imsm_map(dev
, 0);
754 if (get_imsm_disk_slot(map
, dl
->index
) >= 0) {
755 e
->start
= __le32_to_cpu(map
->pba_of_lba0
);
756 e
->size
= __le32_to_cpu(map
->blocks_per_member
);
760 qsort(rv
, memberships
, sizeof(*rv
), cmp_extent
);
762 /* determine the start of the metadata
763 * when no raid devices are defined use the default
764 * ...otherwise allow the metadata to truncate the value
765 * as is the case with older versions of imsm
768 struct extent
*last
= &rv
[memberships
- 1];
771 remainder
= __le32_to_cpu(dl
->disk
.total_blocks
) -
772 (last
->start
+ last
->size
);
773 /* round down to 1k block to satisfy precision of the kernel
777 /* make sure remainder is still sane */
778 if (remainder
< (unsigned)ROUND_UP(super
->len
, 512) >> 9)
779 remainder
= ROUND_UP(super
->len
, 512) >> 9;
780 if (reservation
> remainder
)
781 reservation
= remainder
;
783 e
->start
= __le32_to_cpu(dl
->disk
.total_blocks
) - reservation
;
788 /* try to determine how much space is reserved for metadata from
789 * the last get_extents() entry, otherwise fallback to the
792 static __u32
imsm_reserved_sectors(struct intel_super
*super
, struct dl
*dl
)
798 /* for spares just return a minimal reservation which will grow
799 * once the spare is picked up by an array
802 return MPB_SECTOR_CNT
;
804 e
= get_extents(super
, dl
);
806 return MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
808 /* scroll to last entry */
809 for (i
= 0; e
[i
].size
; i
++)
812 rv
= __le32_to_cpu(dl
->disk
.total_blocks
) - e
[i
].start
;
819 static int is_spare(struct imsm_disk
*disk
)
821 return (disk
->status
& SPARE_DISK
) == SPARE_DISK
;
824 static int is_configured(struct imsm_disk
*disk
)
826 return (disk
->status
& CONFIGURED_DISK
) == CONFIGURED_DISK
;
829 static int is_failed(struct imsm_disk
*disk
)
831 return (disk
->status
& FAILED_DISK
) == FAILED_DISK
;
834 /* Return minimum size of a spare that can be used in this array*/
835 static unsigned long long min_acceptable_spare_size_imsm(struct supertype
*st
)
837 struct intel_super
*super
= st
->sb
;
841 unsigned long long rv
= 0;
845 /* find first active disk in array */
847 while (dl
&& (is_failed(&dl
->disk
) || dl
->index
== -1))
851 /* find last lba used by subarrays */
852 e
= get_extents(super
, dl
);
855 for (i
= 0; e
[i
].size
; i
++)
858 rv
= e
[i
-1].start
+ e
[i
-1].size
;
860 /* add the amount of space needed for metadata */
861 rv
= rv
+ MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
866 static __u64
blocks_per_migr_unit(struct imsm_dev
*dev
);
868 static void print_imsm_dev(struct imsm_dev
*dev
, char *uuid
, int disk_idx
)
872 struct imsm_map
*map
= get_imsm_map(dev
, 0);
873 struct imsm_map
*map2
= get_imsm_map(dev
, 1);
877 printf("[%.16s]:\n", dev
->volume
);
878 printf(" UUID : %s\n", uuid
);
879 printf(" RAID Level : %d", get_imsm_raid_level(map
));
881 printf(" <-- %d", get_imsm_raid_level(map2
));
883 printf(" Members : %d", map
->num_members
);
885 printf(" <-- %d", map2
->num_members
);
887 printf(" Slots : [");
888 for (i
= 0; i
< map
->num_members
; i
++) {
889 ord
= get_imsm_ord_tbl_ent(dev
, i
, 0);
890 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
895 for (i
= 0; i
< map2
->num_members
; i
++) {
896 ord
= get_imsm_ord_tbl_ent(dev
, i
, 1);
897 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
902 printf(" Failed disk : ");
903 if (map
->failed_disk_num
== 0xff)
906 printf("%i", map
->failed_disk_num
);
908 slot
= get_imsm_disk_slot(map
, disk_idx
);
910 ord
= get_imsm_ord_tbl_ent(dev
, slot
, -1);
911 printf(" This Slot : %d%s\n", slot
,
912 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
914 printf(" This Slot : ?\n");
915 sz
= __le32_to_cpu(dev
->size_high
);
917 sz
+= __le32_to_cpu(dev
->size_low
);
918 printf(" Array Size : %llu%s\n", (unsigned long long)sz
,
919 human_size(sz
* 512));
920 sz
= __le32_to_cpu(map
->blocks_per_member
);
921 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz
,
922 human_size(sz
* 512));
923 printf(" Sector Offset : %u\n",
924 __le32_to_cpu(map
->pba_of_lba0
));
925 printf(" Num Stripes : %u\n",
926 __le32_to_cpu(map
->num_data_stripes
));
927 printf(" Chunk Size : %u KiB",
928 __le16_to_cpu(map
->blocks_per_strip
) / 2);
930 printf(" <-- %u KiB",
931 __le16_to_cpu(map2
->blocks_per_strip
) / 2);
933 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
934 printf(" Migrate State : ");
935 if (dev
->vol
.migr_state
) {
936 if (migr_type(dev
) == MIGR_INIT
)
937 printf("initialize\n");
938 else if (migr_type(dev
) == MIGR_REBUILD
)
940 else if (migr_type(dev
) == MIGR_VERIFY
)
942 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
943 printf("general migration\n");
944 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
945 printf("state change\n");
946 else if (migr_type(dev
) == MIGR_REPAIR
)
949 printf("<unknown:%d>\n", migr_type(dev
));
952 printf(" Map State : %s", map_state_str
[map
->map_state
]);
953 if (dev
->vol
.migr_state
) {
954 struct imsm_map
*map
= get_imsm_map(dev
, 1);
956 printf(" <-- %s", map_state_str
[map
->map_state
]);
957 printf("\n Checkpoint : %u (%llu)",
958 __le32_to_cpu(dev
->vol
.curr_migr_unit
),
959 (unsigned long long)blocks_per_migr_unit(dev
));
962 printf(" Dirty State : %s\n", dev
->vol
.dirty
? "dirty" : "clean");
965 static void print_imsm_disk(struct imsm_super
*mpb
, int index
, __u32 reserved
)
967 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, index
);
968 char str
[MAX_RAID_SERIAL_LEN
+ 1];
971 if (index
< 0 || !disk
)
975 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
976 printf(" Disk%02d Serial : %s\n", index
, str
);
977 printf(" State :%s%s%s\n", is_spare(disk
) ? " spare" : "",
978 is_configured(disk
) ? " active" : "",
979 is_failed(disk
) ? " failed" : "");
980 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
981 sz
= __le32_to_cpu(disk
->total_blocks
) - reserved
;
982 printf(" Usable Size : %llu%s\n", (unsigned long long)sz
,
983 human_size(sz
* 512));
986 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
);
988 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
990 struct intel_super
*super
= st
->sb
;
991 struct imsm_super
*mpb
= super
->anchor
;
992 char str
[MAX_SIGNATURE_LENGTH
];
997 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
1000 snprintf(str
, MPB_SIG_LEN
, "%s", mpb
->sig
);
1001 printf(" Magic : %s\n", str
);
1002 snprintf(str
, strlen(MPB_VERSION_RAID0
), "%s", get_imsm_version(mpb
));
1003 printf(" Version : %s\n", get_imsm_version(mpb
));
1004 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
1005 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
1006 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
1007 getinfo_super_imsm(st
, &info
, NULL
);
1008 fname_from_uuid(st
, &info
, nbuf
, ':');
1009 printf(" UUID : %s\n", nbuf
+ 5);
1010 sum
= __le32_to_cpu(mpb
->check_sum
);
1011 printf(" Checksum : %08x %s\n", sum
,
1012 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
1013 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
));
1014 printf(" Disks : %d\n", mpb
->num_disks
);
1015 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
1016 print_imsm_disk(mpb
, super
->disks
->index
, reserved
);
1017 if (super
->bbm_log
) {
1018 struct bbm_log
*log
= super
->bbm_log
;
1021 printf("Bad Block Management Log:\n");
1022 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
1023 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
1024 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
1025 printf(" Spare Blocks : %d\n", __le32_to_cpu(log
->reserved_spare_block_count
));
1026 printf(" First Spare : %llx\n",
1027 (unsigned long long) __le64_to_cpu(log
->first_spare_lba
));
1029 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1031 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1033 super
->current_vol
= i
;
1034 getinfo_super_imsm(st
, &info
, NULL
);
1035 fname_from_uuid(st
, &info
, nbuf
, ':');
1036 print_imsm_dev(dev
, nbuf
+ 5, super
->disks
->index
);
1038 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1039 if (i
== super
->disks
->index
)
1041 print_imsm_disk(mpb
, i
, reserved
);
1043 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
1044 struct imsm_disk
*disk
;
1045 char str
[MAX_RAID_SERIAL_LEN
+ 1];
1053 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
1054 printf(" Disk Serial : %s\n", str
);
1055 printf(" State :%s%s%s\n", is_spare(disk
) ? " spare" : "",
1056 is_configured(disk
) ? " active" : "",
1057 is_failed(disk
) ? " failed" : "");
1058 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
1059 sz
= __le32_to_cpu(disk
->total_blocks
) - reserved
;
1060 printf(" Usable Size : %llu%s\n", (unsigned long long)sz
,
1061 human_size(sz
* 512));
1065 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
1067 /* We just write a generic IMSM ARRAY entry */
1070 struct intel_super
*super
= st
->sb
;
1072 if (!super
->anchor
->num_raid_devs
) {
1073 printf("ARRAY metadata=imsm\n");
1077 getinfo_super_imsm(st
, &info
, NULL
);
1078 fname_from_uuid(st
, &info
, nbuf
, ':');
1079 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
1082 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
1084 /* We just write a generic IMSM ARRAY entry */
1088 struct intel_super
*super
= st
->sb
;
1091 if (!super
->anchor
->num_raid_devs
)
1094 getinfo_super_imsm(st
, &info
, NULL
);
1095 fname_from_uuid(st
, &info
, nbuf
, ':');
1096 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1097 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1099 super
->current_vol
= i
;
1100 getinfo_super_imsm(st
, &info
, NULL
);
1101 fname_from_uuid(st
, &info
, nbuf1
, ':');
1102 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
1103 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
1107 static void export_examine_super_imsm(struct supertype
*st
)
1109 struct intel_super
*super
= st
->sb
;
1110 struct imsm_super
*mpb
= super
->anchor
;
1114 getinfo_super_imsm(st
, &info
, NULL
);
1115 fname_from_uuid(st
, &info
, nbuf
, ':');
1116 printf("MD_METADATA=imsm\n");
1117 printf("MD_LEVEL=container\n");
1118 printf("MD_UUID=%s\n", nbuf
+5);
1119 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
1122 static void detail_super_imsm(struct supertype
*st
, char *homehost
)
1127 getinfo_super_imsm(st
, &info
, NULL
);
1128 fname_from_uuid(st
, &info
, nbuf
, ':');
1129 printf("\n UUID : %s\n", nbuf
+ 5);
1132 static void brief_detail_super_imsm(struct supertype
*st
)
1136 getinfo_super_imsm(st
, &info
, NULL
);
1137 fname_from_uuid(st
, &info
, nbuf
, ':');
1138 printf(" UUID=%s", nbuf
+ 5);
1141 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
);
1142 static void fd2devname(int fd
, char *name
);
1144 static int ahci_enumerate_ports(const char *hba_path
, int port_count
, int host_base
, int verbose
)
1146 /* dump an unsorted list of devices attached to AHCI Intel storage
1147 * controller, as well as non-connected ports
1149 int hba_len
= strlen(hba_path
) + 1;
1154 unsigned long port_mask
= (1 << port_count
) - 1;
1156 if (port_count
> (int)sizeof(port_mask
) * 8) {
1158 fprintf(stderr
, Name
": port_count %d out of range\n", port_count
);
1162 /* scroll through /sys/dev/block looking for devices attached to
1165 dir
= opendir("/sys/dev/block");
1166 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
1177 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
1179 path
= devt_to_devpath(makedev(major
, minor
));
1182 if (!path_attached_to_hba(path
, hba_path
)) {
1188 /* retrieve the scsi device type */
1189 if (asprintf(&device
, "/sys/dev/block/%d:%d/device/xxxxxxx", major
, minor
) < 0) {
1191 fprintf(stderr
, Name
": failed to allocate 'device'\n");
1195 sprintf(device
, "/sys/dev/block/%d:%d/device/type", major
, minor
);
1196 if (load_sys(device
, buf
) != 0) {
1198 fprintf(stderr
, Name
": failed to read device type for %s\n",
1204 type
= strtoul(buf
, NULL
, 10);
1206 /* if it's not a disk print the vendor and model */
1207 if (!(type
== 0 || type
== 7 || type
== 14)) {
1210 sprintf(device
, "/sys/dev/block/%d:%d/device/vendor", major
, minor
);
1211 if (load_sys(device
, buf
) == 0) {
1212 strncpy(vendor
, buf
, sizeof(vendor
));
1213 vendor
[sizeof(vendor
) - 1] = '\0';
1214 c
= (char *) &vendor
[sizeof(vendor
) - 1];
1215 while (isspace(*c
) || *c
== '\0')
1219 sprintf(device
, "/sys/dev/block/%d:%d/device/model", major
, minor
);
1220 if (load_sys(device
, buf
) == 0) {
1221 strncpy(model
, buf
, sizeof(model
));
1222 model
[sizeof(model
) - 1] = '\0';
1223 c
= (char *) &model
[sizeof(model
) - 1];
1224 while (isspace(*c
) || *c
== '\0')
1228 if (vendor
[0] && model
[0])
1229 sprintf(buf
, "%.64s %.64s", vendor
, model
);
1231 switch (type
) { /* numbers from hald/linux/device.c */
1232 case 1: sprintf(buf
, "tape"); break;
1233 case 2: sprintf(buf
, "printer"); break;
1234 case 3: sprintf(buf
, "processor"); break;
1236 case 5: sprintf(buf
, "cdrom"); break;
1237 case 6: sprintf(buf
, "scanner"); break;
1238 case 8: sprintf(buf
, "media_changer"); break;
1239 case 9: sprintf(buf
, "comm"); break;
1240 case 12: sprintf(buf
, "raid"); break;
1241 default: sprintf(buf
, "unknown");
1247 /* chop device path to 'host%d' and calculate the port number */
1248 c
= strchr(&path
[hba_len
], '/');
1251 fprintf(stderr
, Name
": %s - invalid path name\n", path
+ hba_len
);
1256 if (sscanf(&path
[hba_len
], "host%d", &port
) == 1)
1260 *c
= '/'; /* repair the full string */
1261 fprintf(stderr
, Name
": failed to determine port number for %s\n",
1268 /* mark this port as used */
1269 port_mask
&= ~(1 << port
);
1271 /* print out the device information */
1273 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
1277 fd
= dev_open(ent
->d_name
, O_RDONLY
);
1279 printf(" Port%d : - disk info unavailable -\n", port
);
1281 fd2devname(fd
, buf
);
1282 printf(" Port%d : %s", port
, buf
);
1283 if (imsm_read_serial(fd
, NULL
, (__u8
*) buf
) == 0)
1284 printf(" (%s)\n", buf
);
1299 for (i
= 0; i
< port_count
; i
++)
1300 if (port_mask
& (1 << i
))
1301 printf(" Port%d : - no device attached -\n", i
);
1309 static void print_found_intel_controllers(struct sys_dev
*elem
)
1311 for (; elem
; elem
= elem
->next
) {
1312 fprintf(stderr
, Name
": found Intel(R) ");
1313 if (elem
->type
== SYS_DEV_SATA
)
1314 fprintf(stderr
, "SATA ");
1315 else if (elem
->type
== SYS_DEV_SAS
)
1316 fprintf(stderr
, "SAS ");
1317 fprintf(stderr
, "RAID controller");
1319 fprintf(stderr
, " at %s", elem
->pci_id
);
1320 fprintf(stderr
, ".\n");
1325 static int ahci_get_port_count(const char *hba_path
, int *port_count
)
1332 if ((dir
= opendir(hba_path
)) == NULL
)
1335 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
1338 if (sscanf(ent
->d_name
, "host%d", &host
) != 1)
1340 if (*port_count
== 0)
1342 else if (host
< host_base
)
1345 if (host
+ 1 > *port_count
+ host_base
)
1346 *port_count
= host
+ 1 - host_base
;
1352 static void print_imsm_capability(const struct imsm_orom
*orom
)
1354 printf(" Platform : Intel(R) Matrix Storage Manager\n");
1355 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
1356 orom
->hotfix_ver
, orom
->build
);
1357 printf(" RAID Levels :%s%s%s%s%s\n",
1358 imsm_orom_has_raid0(orom
) ? " raid0" : "",
1359 imsm_orom_has_raid1(orom
) ? " raid1" : "",
1360 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
1361 imsm_orom_has_raid10(orom
) ? " raid10" : "",
1362 imsm_orom_has_raid5(orom
) ? " raid5" : "");
1363 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1364 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
1365 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
1366 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
1367 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
1368 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
1369 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
1370 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
1371 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
1372 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
1373 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
1374 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
1375 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
1376 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
1377 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
1378 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
1379 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
1380 printf(" Max Disks : %d\n", orom
->tds
);
1381 printf(" Max Volumes : %d\n", orom
->vpa
);
1385 static int detail_platform_imsm(int verbose
, int enumerate_only
)
1387 /* There are two components to imsm platform support, the ahci SATA
1388 * controller and the option-rom. To find the SATA controller we
1389 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
1390 * controller with the Intel vendor id is present. This approach
1391 * allows mdadm to leverage the kernel's ahci detection logic, with the
1392 * caveat that if ahci.ko is not loaded mdadm will not be able to
1393 * detect platform raid capabilities. The option-rom resides in a
1394 * platform "Adapter ROM". We scan for its signature to retrieve the
1395 * platform capabilities. If raid support is disabled in the BIOS the
1396 * option-rom capability structure will not be available.
1398 const struct imsm_orom
*orom
;
1399 struct sys_dev
*list
, *hba
;
1404 if (enumerate_only
) {
1405 if (check_env("IMSM_NO_PLATFORM"))
1407 list
= find_intel_devices();
1410 for (hba
= list
; hba
; hba
= hba
->next
) {
1411 orom
= find_imsm_capability(hba
->type
);
1417 free_sys_dev(&list
);
1421 list
= find_intel_devices();
1424 fprintf(stderr
, Name
": no active Intel(R) RAID "
1425 "controller found.\n");
1426 free_sys_dev(&list
);
1429 print_found_intel_controllers(list
);
1431 for (hba
= list
; hba
; hba
= hba
->next
) {
1432 orom
= find_imsm_capability(hba
->type
);
1434 fprintf(stderr
, Name
": imsm capabilities not found for controller: %s (type %s)\n",
1435 hba
->path
, get_sys_dev_type(hba
->type
));
1437 print_imsm_capability(orom
);
1440 for (hba
= list
; hba
; hba
= hba
->next
) {
1441 printf(" I/O Controller : %s (%s)\n",
1442 hba
->path
, get_sys_dev_type(hba
->type
));
1444 if (hba
->type
== SYS_DEV_SATA
) {
1445 host_base
= ahci_get_port_count(hba
->path
, &port_count
);
1446 if (ahci_enumerate_ports(hba
->path
, port_count
, host_base
, verbose
)) {
1448 fprintf(stderr
, Name
": failed to enumerate "
1449 "ports on SATA controller at %s.", hba
->pci_id
);
1455 free_sys_dev(&list
);
1460 static int match_home_imsm(struct supertype
*st
, char *homehost
)
1462 /* the imsm metadata format does not specify any host
1463 * identification information. We return -1 since we can never
1464 * confirm nor deny whether a given array is "meant" for this
1465 * host. We rely on compare_super and the 'family_num' fields to
1466 * exclude member disks that do not belong, and we rely on
1467 * mdadm.conf to specify the arrays that should be assembled.
1468 * Auto-assembly may still pick up "foreign" arrays.
1474 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
1476 /* The uuid returned here is used for:
1477 * uuid to put into bitmap file (Create, Grow)
1478 * uuid for backup header when saving critical section (Grow)
1479 * comparing uuids when re-adding a device into an array
1480 * In these cases the uuid required is that of the data-array,
1481 * not the device-set.
1482 * uuid to recognise same set when adding a missing device back
1483 * to an array. This is a uuid for the device-set.
1485 * For each of these we can make do with a truncated
1486 * or hashed uuid rather than the original, as long as
1488 * In each case the uuid required is that of the data-array,
1489 * not the device-set.
1491 /* imsm does not track uuid's so we synthesis one using sha1 on
1492 * - The signature (Which is constant for all imsm array, but no matter)
1493 * - the orig_family_num of the container
1494 * - the index number of the volume
1495 * - the 'serial' number of the volume.
1496 * Hopefully these are all constant.
1498 struct intel_super
*super
= st
->sb
;
1501 struct sha1_ctx ctx
;
1502 struct imsm_dev
*dev
= NULL
;
1505 /* some mdadm versions failed to set ->orig_family_num, in which
1506 * case fall back to ->family_num. orig_family_num will be
1507 * fixed up with the first metadata update.
1509 family_num
= super
->anchor
->orig_family_num
;
1510 if (family_num
== 0)
1511 family_num
= super
->anchor
->family_num
;
1512 sha1_init_ctx(&ctx
);
1513 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
1514 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
1515 if (super
->current_vol
>= 0)
1516 dev
= get_imsm_dev(super
, super
->current_vol
);
1518 __u32 vol
= super
->current_vol
;
1519 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
1520 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
1522 sha1_finish_ctx(&ctx
, buf
);
1523 memcpy(uuid
, buf
, 4*4);
1528 get_imsm_numerical_version(struct imsm_super
*mpb
, int *m
, int *p
)
1530 __u8
*v
= get_imsm_version(mpb
);
1531 __u8
*end
= mpb
->sig
+ MAX_SIGNATURE_LENGTH
;
1532 char major
[] = { 0, 0, 0 };
1533 char minor
[] = { 0 ,0, 0 };
1534 char patch
[] = { 0, 0, 0 };
1535 char *ver_parse
[] = { major
, minor
, patch
};
1539 while (*v
!= '\0' && v
< end
) {
1540 if (*v
!= '.' && j
< 2)
1541 ver_parse
[i
][j
++] = *v
;
1549 *m
= strtol(minor
, NULL
, 0);
1550 *p
= strtol(patch
, NULL
, 0);
1554 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
1556 /* migr_strip_size when repairing or initializing parity */
1557 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1558 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1560 switch (get_imsm_raid_level(map
)) {
1565 return 128*1024 >> 9;
1569 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
1571 /* migr_strip_size when rebuilding a degraded disk, no idea why
1572 * this is different than migr_strip_size_resync(), but it's good
1575 struct imsm_map
*map
= get_imsm_map(dev
, 1);
1576 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1578 switch (get_imsm_raid_level(map
)) {
1581 if (map
->num_members
% map
->num_domains
== 0)
1582 return 128*1024 >> 9;
1586 return max((__u32
) 64*1024 >> 9, chunk
);
1588 return 128*1024 >> 9;
1592 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
1594 struct imsm_map
*lo
= get_imsm_map(dev
, 0);
1595 struct imsm_map
*hi
= get_imsm_map(dev
, 1);
1596 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
1597 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
1599 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
1602 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
1604 struct imsm_map
*lo
= get_imsm_map(dev
, 0);
1605 int level
= get_imsm_raid_level(lo
);
1607 if (level
== 1 || level
== 10) {
1608 struct imsm_map
*hi
= get_imsm_map(dev
, 1);
1610 return hi
->num_domains
;
1612 return num_stripes_per_unit_resync(dev
);
1615 static __u8
imsm_num_data_members(struct imsm_dev
*dev
, int second_map
)
1617 /* named 'imsm_' because raid0, raid1 and raid10
1618 * counter-intuitively have the same number of data disks
1620 struct imsm_map
*map
= get_imsm_map(dev
, second_map
);
1622 switch (get_imsm_raid_level(map
)) {
1626 return map
->num_members
;
1628 return map
->num_members
- 1;
1630 dprintf("%s: unsupported raid level\n", __func__
);
1635 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
1637 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1638 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1640 switch(get_imsm_raid_level(map
)) {
1643 return chunk
* map
->num_domains
;
1645 return chunk
* map
->num_members
;
1651 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
1653 struct imsm_map
*map
= get_imsm_map(dev
, 1);
1654 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1655 __u32 strip
= block
/ chunk
;
1657 switch (get_imsm_raid_level(map
)) {
1660 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
1661 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
1663 return vol_stripe
* chunk
+ block
% chunk
;
1665 __u32 stripe
= strip
/ (map
->num_members
- 1);
1667 return stripe
* chunk
+ block
% chunk
;
1674 static __u64
blocks_per_migr_unit(struct imsm_dev
*dev
)
1676 /* calculate the conversion factor between per member 'blocks'
1677 * (md/{resync,rebuild}_start) and imsm migration units, return
1678 * 0 for the 'not migrating' and 'unsupported migration' cases
1680 if (!dev
->vol
.migr_state
)
1683 switch (migr_type(dev
)) {
1688 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1689 __u32 stripes_per_unit
;
1690 __u32 blocks_per_unit
;
1699 /* yes, this is really the translation of migr_units to
1700 * per-member blocks in the 'resync' case
1702 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
1703 migr_chunk
= migr_strip_blocks_resync(dev
);
1704 disks
= imsm_num_data_members(dev
, 0);
1705 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
1706 stripe
= __le32_to_cpu(map
->blocks_per_strip
) * disks
;
1707 segment
= blocks_per_unit
/ stripe
;
1708 block_rel
= blocks_per_unit
- segment
* stripe
;
1709 parity_depth
= parity_segment_depth(dev
);
1710 block_map
= map_migr_block(dev
, block_rel
);
1711 return block_map
+ parity_depth
* segment
;
1713 case MIGR_REBUILD
: {
1714 __u32 stripes_per_unit
;
1717 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
1718 migr_chunk
= migr_strip_blocks_rebuild(dev
);
1719 return migr_chunk
* stripes_per_unit
;
1721 case MIGR_STATE_CHANGE
:
1727 static int imsm_level_to_layout(int level
)
1735 return ALGORITHM_LEFT_ASYMMETRIC
;
1742 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
1744 struct intel_super
*super
= st
->sb
;
1745 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
1746 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1747 struct imsm_map
*prev_map
= get_imsm_map(dev
, 1);
1748 struct imsm_map
*map_to_analyse
= map
;
1751 int map_disks
= info
->array
.raid_disks
;
1754 map_to_analyse
= prev_map
;
1756 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
1757 if (dl
->raiddisk
== info
->disk
.raid_disk
)
1759 info
->container_member
= super
->current_vol
;
1760 info
->array
.raid_disks
= map_to_analyse
->num_members
;
1761 info
->array
.level
= get_imsm_raid_level(map_to_analyse
);
1762 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
1763 info
->array
.md_minor
= -1;
1764 info
->array
.ctime
= 0;
1765 info
->array
.utime
= 0;
1766 info
->array
.chunk_size
=
1767 __le16_to_cpu(map_to_analyse
->blocks_per_strip
) << 9;
1768 info
->array
.state
= !dev
->vol
.dirty
;
1769 info
->custom_array_size
= __le32_to_cpu(dev
->size_high
);
1770 info
->custom_array_size
<<= 32;
1771 info
->custom_array_size
|= __le32_to_cpu(dev
->size_low
);
1772 if (prev_map
&& map
->map_state
== prev_map
->map_state
) {
1773 info
->reshape_active
= 1;
1774 info
->new_level
= get_imsm_raid_level(map
);
1775 info
->new_layout
= imsm_level_to_layout(info
->new_level
);
1776 info
->new_chunk
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
1777 info
->delta_disks
= map
->num_members
- prev_map
->num_members
;
1778 if (info
->delta_disks
) {
1779 /* this needs to be applied to every array
1782 info
->reshape_active
= 2;
1784 /* We shape information that we give to md might have to be
1785 * modify to cope with md's requirement for reshaping arrays.
1786 * For example, when reshaping a RAID0, md requires it to be
1787 * presented as a degraded RAID4.
1788 * Also if a RAID0 is migrating to a RAID5 we need to specify
1789 * the array as already being RAID5, but the 'before' layout
1790 * is a RAID4-like layout.
1792 switch (info
->array
.level
) {
1794 switch(info
->new_level
) {
1796 /* conversion is happening as RAID4 */
1797 info
->array
.level
= 4;
1798 info
->array
.raid_disks
+= 1;
1801 /* conversion is happening as RAID5 */
1802 info
->array
.level
= 5;
1803 info
->array
.layout
= ALGORITHM_PARITY_N
;
1804 info
->array
.raid_disks
+= 1;
1805 info
->delta_disks
-= 1;
1808 /* FIXME error message */
1809 info
->array
.level
= UnSet
;
1815 info
->new_level
= UnSet
;
1816 info
->new_layout
= UnSet
;
1817 info
->new_chunk
= info
->array
.chunk_size
;
1818 info
->delta_disks
= 0;
1820 info
->disk
.major
= 0;
1821 info
->disk
.minor
= 0;
1823 info
->disk
.major
= dl
->major
;
1824 info
->disk
.minor
= dl
->minor
;
1827 info
->data_offset
= __le32_to_cpu(map_to_analyse
->pba_of_lba0
);
1828 info
->component_size
=
1829 __le32_to_cpu(map_to_analyse
->blocks_per_member
);
1830 memset(info
->uuid
, 0, sizeof(info
->uuid
));
1831 info
->recovery_start
= MaxSector
;
1833 info
->reshape_progress
= 0;
1834 info
->resync_start
= MaxSector
;
1835 if (map_to_analyse
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
1837 info
->resync_start
= 0;
1839 if (dev
->vol
.migr_state
) {
1840 switch (migr_type(dev
)) {
1843 __u64 blocks_per_unit
= blocks_per_migr_unit(dev
);
1844 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
1846 info
->resync_start
= blocks_per_unit
* units
;
1849 case MIGR_GEN_MIGR
: {
1850 __u64 blocks_per_unit
= blocks_per_migr_unit(dev
);
1851 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
1852 unsigned long long array_blocks
;
1855 info
->reshape_progress
= blocks_per_unit
* units
;
1857 /* checkpoint is written per disks unit
1858 * recalculate it to reshape position
1860 used_disks
= imsm_num_data_members(dev
, 0);
1861 info
->reshape_progress
*= used_disks
;
1862 dprintf("IMSM: General Migration checkpoint : %llu "
1863 "(%llu) -> read reshape progress : %llu\n",
1864 units
, blocks_per_unit
, info
->reshape_progress
);
1866 used_disks
= imsm_num_data_members(dev
, 1);
1867 if (used_disks
> 0) {
1868 array_blocks
= map
->blocks_per_member
*
1870 /* round array size down to closest MB
1872 info
->custom_array_size
= (array_blocks
1873 >> SECT_PER_MB_SHIFT
)
1874 << SECT_PER_MB_SHIFT
;
1878 /* we could emulate the checkpointing of
1879 * 'sync_action=check' migrations, but for now
1880 * we just immediately complete them
1883 /* this is handled by container_content_imsm() */
1884 case MIGR_STATE_CHANGE
:
1885 /* FIXME handle other migrations */
1887 /* we are not dirty, so... */
1888 info
->resync_start
= MaxSector
;
1892 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
1893 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
1895 info
->array
.major_version
= -1;
1896 info
->array
.minor_version
= -2;
1897 devname
= devnum2devname(st
->container_dev
);
1898 *info
->text_version
= '\0';
1900 sprintf(info
->text_version
, "/%s/%d", devname
, info
->container_member
);
1902 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
1903 uuid_from_super_imsm(st
, info
->uuid
);
1907 for (i
=0; i
<map_disks
; i
++) {
1909 if (i
< info
->array
.raid_disks
) {
1910 struct imsm_disk
*dsk
;
1911 j
= get_imsm_disk_idx(dev
, i
, -1);
1912 dsk
= get_imsm_disk(super
, j
);
1913 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
1920 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
, int failed
);
1921 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
);
1923 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
1927 for (d
= super
->missing
; d
; d
= d
->next
)
1928 if (d
->index
== index
)
1933 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
1935 struct intel_super
*super
= st
->sb
;
1936 struct imsm_disk
*disk
;
1937 int map_disks
= info
->array
.raid_disks
;
1938 int max_enough
= -1;
1940 struct imsm_super
*mpb
;
1942 if (super
->current_vol
>= 0) {
1943 getinfo_super_imsm_volume(st
, info
, map
);
1947 /* Set raid_disks to zero so that Assemble will always pull in valid
1950 info
->array
.raid_disks
= 0;
1951 info
->array
.level
= LEVEL_CONTAINER
;
1952 info
->array
.layout
= 0;
1953 info
->array
.md_minor
= -1;
1954 info
->array
.ctime
= 0; /* N/A for imsm */
1955 info
->array
.utime
= 0;
1956 info
->array
.chunk_size
= 0;
1958 info
->disk
.major
= 0;
1959 info
->disk
.minor
= 0;
1960 info
->disk
.raid_disk
= -1;
1961 info
->reshape_active
= 0;
1962 info
->array
.major_version
= -1;
1963 info
->array
.minor_version
= -2;
1964 strcpy(info
->text_version
, "imsm");
1965 info
->safe_mode_delay
= 0;
1966 info
->disk
.number
= -1;
1967 info
->disk
.state
= 0;
1969 info
->recovery_start
= MaxSector
;
1971 /* do we have the all the insync disks that we expect? */
1972 mpb
= super
->anchor
;
1974 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1975 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1976 int failed
, enough
, j
, missing
= 0;
1977 struct imsm_map
*map
;
1980 failed
= imsm_count_failed(super
, dev
);
1981 state
= imsm_check_degraded(super
, dev
, failed
);
1982 map
= get_imsm_map(dev
, dev
->vol
.migr_state
);
1984 /* any newly missing disks?
1985 * (catches single-degraded vs double-degraded)
1987 for (j
= 0; j
< map
->num_members
; j
++) {
1988 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, -1);
1989 __u32 idx
= ord_to_idx(ord
);
1991 if (!(ord
& IMSM_ORD_REBUILD
) &&
1992 get_imsm_missing(super
, idx
)) {
1998 if (state
== IMSM_T_STATE_FAILED
)
2000 else if (state
== IMSM_T_STATE_DEGRADED
&&
2001 (state
!= map
->map_state
|| missing
))
2003 else /* we're normal, or already degraded */
2006 /* in the missing/failed disk case check to see
2007 * if at least one array is runnable
2009 max_enough
= max(max_enough
, enough
);
2011 dprintf("%s: enough: %d\n", __func__
, max_enough
);
2012 info
->container_enough
= max_enough
;
2015 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
2017 disk
= &super
->disks
->disk
;
2018 info
->data_offset
= __le32_to_cpu(disk
->total_blocks
) - reserved
;
2019 info
->component_size
= reserved
;
2020 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
2021 /* we don't change info->disk.raid_disk here because
2022 * this state will be finalized in mdmon after we have
2023 * found the 'most fresh' version of the metadata
2025 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
2026 info
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
2029 /* only call uuid_from_super_imsm when this disk is part of a populated container,
2030 * ->compare_super may have updated the 'num_raid_devs' field for spares
2032 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
2033 uuid_from_super_imsm(st
, info
->uuid
);
2035 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
2037 /* I don't know how to compute 'map' on imsm, so use safe default */
2040 for (i
= 0; i
< map_disks
; i
++)
2046 /* allocates memory and fills disk in mdinfo structure
2047 * for each disk in array */
2048 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
2050 struct mdinfo
*mddev
= NULL
;
2051 struct intel_super
*super
= st
->sb
;
2052 struct imsm_disk
*disk
;
2055 if (!super
|| !super
->disks
)
2058 mddev
= malloc(sizeof(*mddev
));
2060 fprintf(stderr
, Name
": Failed to allocate memory.\n");
2063 memset(mddev
, 0, sizeof(*mddev
));
2067 tmp
= malloc(sizeof(*tmp
));
2069 fprintf(stderr
, Name
": Failed to allocate memory.\n");
2074 memset(tmp
, 0, sizeof(*tmp
));
2076 tmp
->next
= mddev
->devs
;
2078 tmp
->disk
.number
= count
++;
2079 tmp
->disk
.major
= dl
->major
;
2080 tmp
->disk
.minor
= dl
->minor
;
2081 tmp
->disk
.state
= is_configured(disk
) ?
2082 (1 << MD_DISK_ACTIVE
) : 0;
2083 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
2084 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
2085 tmp
->disk
.raid_disk
= -1;
2091 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
2092 char *update
, char *devname
, int verbose
,
2093 int uuid_set
, char *homehost
)
2095 /* For 'assemble' and 'force' we need to return non-zero if any
2096 * change was made. For others, the return value is ignored.
2097 * Update options are:
2098 * force-one : This device looks a bit old but needs to be included,
2099 * update age info appropriately.
2100 * assemble: clear any 'faulty' flag to allow this device to
2102 * force-array: Array is degraded but being forced, mark it clean
2103 * if that will be needed to assemble it.
2105 * newdev: not used ????
2106 * grow: Array has gained a new device - this is currently for
2108 * resync: mark as dirty so a resync will happen.
2109 * name: update the name - preserving the homehost
2110 * uuid: Change the uuid of the array to match watch is given
2112 * Following are not relevant for this imsm:
2113 * sparc2.2 : update from old dodgey metadata
2114 * super-minor: change the preferred_minor number
2115 * summaries: update redundant counters.
2116 * homehost: update the recorded homehost
2117 * _reshape_progress: record new reshape_progress position.
2120 struct intel_super
*super
= st
->sb
;
2121 struct imsm_super
*mpb
;
2123 /* we can only update container info */
2124 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
2127 mpb
= super
->anchor
;
2129 if (strcmp(update
, "uuid") == 0 && uuid_set
&& !info
->update_private
)
2131 else if (strcmp(update
, "uuid") == 0 && uuid_set
&& info
->update_private
) {
2132 mpb
->orig_family_num
= *((__u32
*) info
->update_private
);
2134 } else if (strcmp(update
, "uuid") == 0) {
2135 __u32
*new_family
= malloc(sizeof(*new_family
));
2137 /* update orig_family_number with the incoming random
2138 * data, report the new effective uuid, and store the
2139 * new orig_family_num for future updates.
2142 memcpy(&mpb
->orig_family_num
, info
->uuid
, sizeof(__u32
));
2143 uuid_from_super_imsm(st
, info
->uuid
);
2144 *new_family
= mpb
->orig_family_num
;
2145 info
->update_private
= new_family
;
2148 } else if (strcmp(update
, "assemble") == 0)
2153 /* successful update? recompute checksum */
2155 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
2160 static size_t disks_to_mpb_size(int disks
)
2164 size
= sizeof(struct imsm_super
);
2165 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
2166 size
+= 2 * sizeof(struct imsm_dev
);
2167 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
2168 size
+= (4 - 2) * sizeof(struct imsm_map
);
2169 /* 4 possible disk_ord_tbl's */
2170 size
+= 4 * (disks
- 1) * sizeof(__u32
);
2175 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
)
2177 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
2180 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
2183 static void free_devlist(struct intel_super
*super
)
2185 struct intel_dev
*dv
;
2187 while (super
->devlist
) {
2188 dv
= super
->devlist
->next
;
2189 free(super
->devlist
->dev
);
2190 free(super
->devlist
);
2191 super
->devlist
= dv
;
2195 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
2197 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
2200 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
)
2204 * 0 same, or first was empty, and second was copied
2205 * 1 second had wrong number
2207 * 3 wrong other info
2209 struct intel_super
*first
= st
->sb
;
2210 struct intel_super
*sec
= tst
->sb
;
2217 /* in platform dependent environment test if the disks
2218 * use the same Intel hba
2220 if (!check_env("IMSM_NO_PLATFORM")) {
2221 if (first
->hba
->type
!= sec
->hba
->type
) {
2223 "HBAs of devices does not match %s != %s\n",
2224 get_sys_dev_type(first
->hba
->type
),
2225 get_sys_dev_type(sec
->hba
->type
));
2230 /* if an anchor does not have num_raid_devs set then it is a free
2233 if (first
->anchor
->num_raid_devs
> 0 &&
2234 sec
->anchor
->num_raid_devs
> 0) {
2235 /* Determine if these disks might ever have been
2236 * related. Further disambiguation can only take place
2237 * in load_super_imsm_all
2239 __u32 first_family
= first
->anchor
->orig_family_num
;
2240 __u32 sec_family
= sec
->anchor
->orig_family_num
;
2242 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
2243 MAX_SIGNATURE_LENGTH
) != 0)
2246 if (first_family
== 0)
2247 first_family
= first
->anchor
->family_num
;
2248 if (sec_family
== 0)
2249 sec_family
= sec
->anchor
->family_num
;
2251 if (first_family
!= sec_family
)
2257 /* if 'first' is a spare promote it to a populated mpb with sec's
2260 if (first
->anchor
->num_raid_devs
== 0 &&
2261 sec
->anchor
->num_raid_devs
> 0) {
2263 struct intel_dev
*dv
;
2264 struct imsm_dev
*dev
;
2266 /* we need to copy raid device info from sec if an allocation
2267 * fails here we don't associate the spare
2269 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++) {
2270 dv
= malloc(sizeof(*dv
));
2273 dev
= malloc(sizeof_imsm_dev(get_imsm_dev(sec
, i
), 1));
2280 dv
->next
= first
->devlist
;
2281 first
->devlist
= dv
;
2283 if (i
< sec
->anchor
->num_raid_devs
) {
2284 /* allocation failure */
2285 free_devlist(first
);
2286 fprintf(stderr
, "imsm: failed to associate spare\n");
2289 first
->anchor
->num_raid_devs
= sec
->anchor
->num_raid_devs
;
2290 first
->anchor
->orig_family_num
= sec
->anchor
->orig_family_num
;
2291 first
->anchor
->family_num
= sec
->anchor
->family_num
;
2292 memcpy(first
->anchor
->sig
, sec
->anchor
->sig
, MAX_SIGNATURE_LENGTH
);
2293 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++)
2294 imsm_copy_dev(get_imsm_dev(first
, i
), get_imsm_dev(sec
, i
));
2300 static void fd2devname(int fd
, char *name
)
2304 char dname
[PATH_MAX
];
2309 if (fstat(fd
, &st
) != 0)
2311 sprintf(path
, "/sys/dev/block/%d:%d",
2312 major(st
.st_rdev
), minor(st
.st_rdev
));
2314 rv
= readlink(path
, dname
, sizeof(dname
));
2319 nm
= strrchr(dname
, '/');
2321 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
2324 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
2326 static int imsm_read_serial(int fd
, char *devname
,
2327 __u8 serial
[MAX_RAID_SERIAL_LEN
])
2329 unsigned char scsi_serial
[255];
2338 memset(scsi_serial
, 0, sizeof(scsi_serial
));
2340 rv
= scsi_get_serial(fd
, scsi_serial
, sizeof(scsi_serial
));
2342 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
2343 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
2344 fd2devname(fd
, (char *) serial
);
2351 Name
": Failed to retrieve serial for %s\n",
2356 rsp_len
= scsi_serial
[3];
2360 Name
": Failed to retrieve serial for %s\n",
2364 rsp_buf
= (char *) &scsi_serial
[4];
2366 /* trim all whitespace and non-printable characters and convert
2369 for (i
= 0, dest
= rsp_buf
; i
< rsp_len
; i
++) {
2372 /* ':' is reserved for use in placeholder serial
2373 * numbers for missing disks
2381 len
= dest
- rsp_buf
;
2384 /* truncate leading characters */
2385 if (len
> MAX_RAID_SERIAL_LEN
) {
2386 dest
+= len
- MAX_RAID_SERIAL_LEN
;
2387 len
= MAX_RAID_SERIAL_LEN
;
2390 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
2391 memcpy(serial
, dest
, len
);
2396 static int serialcmp(__u8
*s1
, __u8
*s2
)
2398 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
2401 static void serialcpy(__u8
*dest
, __u8
*src
)
2403 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
2407 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
2411 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
2412 if (serialcmp(dl
->serial
, serial
) == 0)
2419 static struct imsm_disk
*
2420 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
2424 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2425 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
2427 if (serialcmp(disk
->serial
, serial
) == 0) {
2438 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
2440 struct imsm_disk
*disk
;
2445 __u8 serial
[MAX_RAID_SERIAL_LEN
];
2447 rv
= imsm_read_serial(fd
, devname
, serial
);
2452 dl
= calloc(1, sizeof(*dl
));
2456 Name
": failed to allocate disk buffer for %s\n",
2462 dl
->major
= major(stb
.st_rdev
);
2463 dl
->minor
= minor(stb
.st_rdev
);
2464 dl
->next
= super
->disks
;
2465 dl
->fd
= keep_fd
? fd
: -1;
2466 assert(super
->disks
== NULL
);
2468 serialcpy(dl
->serial
, serial
);
2471 fd2devname(fd
, name
);
2473 dl
->devname
= strdup(devname
);
2475 dl
->devname
= strdup(name
);
2477 /* look up this disk's index in the current anchor */
2478 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
2481 /* only set index on disks that are a member of a
2482 * populated contianer, i.e. one with raid_devs
2484 if (is_failed(&dl
->disk
))
2486 else if (is_spare(&dl
->disk
))
2494 /* When migrating map0 contains the 'destination' state while map1
2495 * contains the current state. When not migrating map0 contains the
2496 * current state. This routine assumes that map[0].map_state is set to
2497 * the current array state before being called.
2499 * Migration is indicated by one of the following states
2500 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
2501 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
2502 * map1state=unitialized)
2503 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
2505 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
2506 * map1state=degraded)
2508 static void migrate(struct imsm_dev
*dev
, __u8 to_state
, int migr_type
)
2510 struct imsm_map
*dest
;
2511 struct imsm_map
*src
= get_imsm_map(dev
, 0);
2513 dev
->vol
.migr_state
= 1;
2514 set_migr_type(dev
, migr_type
);
2515 dev
->vol
.curr_migr_unit
= 0;
2516 dest
= get_imsm_map(dev
, 1);
2518 /* duplicate and then set the target end state in map[0] */
2519 memcpy(dest
, src
, sizeof_imsm_map(src
));
2520 if ((migr_type
== MIGR_REBUILD
) ||
2521 (migr_type
== MIGR_GEN_MIGR
)) {
2525 for (i
= 0; i
< src
->num_members
; i
++) {
2526 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
2527 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
2531 src
->map_state
= to_state
;
2534 static void end_migration(struct imsm_dev
*dev
, __u8 map_state
)
2536 struct imsm_map
*map
= get_imsm_map(dev
, 0);
2537 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
);
2540 /* merge any IMSM_ORD_REBUILD bits that were not successfully
2541 * completed in the last migration.
2543 * FIXME add support for raid-level-migration
2545 for (i
= 0; i
< prev
->num_members
; i
++)
2546 for (j
= 0; j
< map
->num_members
; j
++)
2547 /* during online capacity expansion
2548 * disks position can be changed if takeover is used
2550 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
2551 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
2552 map
->disk_ord_tbl
[j
] |= prev
->disk_ord_tbl
[i
];
2556 dev
->vol
.migr_state
= 0;
2557 dev
->vol
.migr_type
= 0;
2558 dev
->vol
.curr_migr_unit
= 0;
2559 map
->map_state
= map_state
;
2563 static int parse_raid_devices(struct intel_super
*super
)
2566 struct imsm_dev
*dev_new
;
2567 size_t len
, len_migr
;
2569 size_t space_needed
= 0;
2570 struct imsm_super
*mpb
= super
->anchor
;
2572 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
2573 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
2574 struct intel_dev
*dv
;
2576 len
= sizeof_imsm_dev(dev_iter
, 0);
2577 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
2579 space_needed
+= len_migr
- len
;
2581 dv
= malloc(sizeof(*dv
));
2584 if (max_len
< len_migr
)
2586 if (max_len
> len_migr
)
2587 space_needed
+= max_len
- len_migr
;
2588 dev_new
= malloc(max_len
);
2593 imsm_copy_dev(dev_new
, dev_iter
);
2596 dv
->next
= super
->devlist
;
2597 super
->devlist
= dv
;
2600 /* ensure that super->buf is large enough when all raid devices
2603 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
2606 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
, 512);
2607 if (posix_memalign(&buf
, 512, len
) != 0)
2610 memcpy(buf
, super
->buf
, super
->len
);
2611 memset(buf
+ super
->len
, 0, len
- super
->len
);
2620 /* retrieve a pointer to the bbm log which starts after all raid devices */
2621 struct bbm_log
*__get_imsm_bbm_log(struct imsm_super
*mpb
)
2625 if (__le32_to_cpu(mpb
->bbm_log_size
)) {
2627 ptr
+= mpb
->mpb_size
- __le32_to_cpu(mpb
->bbm_log_size
);
2633 static void __free_imsm(struct intel_super
*super
, int free_disks
);
2635 /* load_imsm_mpb - read matrix metadata
2636 * allocates super->mpb to be freed by free_imsm
2638 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
2640 unsigned long long dsize
;
2641 unsigned long long sectors
;
2643 struct imsm_super
*anchor
;
2646 get_dev_size(fd
, NULL
, &dsize
);
2650 Name
": %s: device to small for imsm\n",
2655 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0) {
2658 Name
": Cannot seek to anchor block on %s: %s\n",
2659 devname
, strerror(errno
));
2663 if (posix_memalign((void**)&anchor
, 512, 512) != 0) {
2666 Name
": Failed to allocate imsm anchor buffer"
2667 " on %s\n", devname
);
2670 if (read(fd
, anchor
, 512) != 512) {
2673 Name
": Cannot read anchor block on %s: %s\n",
2674 devname
, strerror(errno
));
2679 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
2682 Name
": no IMSM anchor on %s\n", devname
);
2687 __free_imsm(super
, 0);
2688 /* reload capability and hba */
2690 /* capability and hba must be updated with new super allocation */
2691 find_intel_hba_capability(fd
, super
, devname
);
2692 super
->len
= ROUND_UP(anchor
->mpb_size
, 512);
2693 if (posix_memalign(&super
->buf
, 512, super
->len
) != 0) {
2696 Name
": unable to allocate %zu byte mpb buffer\n",
2701 memcpy(super
->buf
, anchor
, 512);
2703 sectors
= mpb_sectors(anchor
) - 1;
2706 check_sum
= __gen_imsm_checksum(super
->anchor
);
2707 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
2710 Name
": IMSM checksum %x != %x on %s\n",
2712 __le32_to_cpu(super
->anchor
->check_sum
),
2720 /* read the extended mpb */
2721 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0) {
2724 Name
": Cannot seek to extended mpb on %s: %s\n",
2725 devname
, strerror(errno
));
2729 if ((unsigned)read(fd
, super
->buf
+ 512, super
->len
- 512) != super
->len
- 512) {
2732 Name
": Cannot read extended mpb on %s: %s\n",
2733 devname
, strerror(errno
));
2737 check_sum
= __gen_imsm_checksum(super
->anchor
);
2738 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
2741 Name
": IMSM checksum %x != %x on %s\n",
2742 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
2747 /* FIXME the BBM log is disk specific so we cannot use this global
2748 * buffer for all disks. Ok for now since we only look at the global
2749 * bbm_log_size parameter to gate assembly
2751 super
->bbm_log
= __get_imsm_bbm_log(super
->anchor
);
2757 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
2761 err
= load_imsm_mpb(fd
, super
, devname
);
2764 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
2767 err
= parse_raid_devices(super
);
2772 static void __free_imsm_disk(struct dl
*d
)
2784 static void free_imsm_disks(struct intel_super
*super
)
2788 while (super
->disks
) {
2790 super
->disks
= d
->next
;
2791 __free_imsm_disk(d
);
2793 while (super
->disk_mgmt_list
) {
2794 d
= super
->disk_mgmt_list
;
2795 super
->disk_mgmt_list
= d
->next
;
2796 __free_imsm_disk(d
);
2798 while (super
->missing
) {
2800 super
->missing
= d
->next
;
2801 __free_imsm_disk(d
);
2806 /* free all the pieces hanging off of a super pointer */
2807 static void __free_imsm(struct intel_super
*super
, int free_disks
)
2809 struct intel_hba
*elem
, *next
;
2815 /* unlink capability description */
2818 free_imsm_disks(super
);
2819 free_devlist(super
);
2823 free((void *)elem
->path
);
2831 static void free_imsm(struct intel_super
*super
)
2833 __free_imsm(super
, 1);
2837 static void free_super_imsm(struct supertype
*st
)
2839 struct intel_super
*super
= st
->sb
;
2848 static struct intel_super
*alloc_super(void)
2850 struct intel_super
*super
= malloc(sizeof(*super
));
2853 memset(super
, 0, sizeof(*super
));
2854 super
->current_vol
= -1;
2855 super
->create_offset
= ~((__u32
) 0);
2861 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
2863 static int find_intel_hba_capability(int fd
, struct intel_super
*super
, char *devname
)
2865 struct sys_dev
*hba_name
;
2868 if ((fd
< 0) || check_env("IMSM_NO_PLATFORM")) {
2873 hba_name
= find_disk_attached_hba(fd
, NULL
);
2877 Name
": %s is not attached to Intel(R) RAID controller.\n",
2881 rv
= attach_hba_to_super(super
, hba_name
);
2884 struct intel_hba
*hba
= super
->hba
;
2886 fprintf(stderr
, Name
": %s is attached to Intel(R) %s RAID "
2887 "controller (%s),\n"
2888 " but the container is assigned to Intel(R) "
2889 "%s RAID controller (",
2892 hba_name
->pci_id
? : "Err!",
2893 get_sys_dev_type(hba_name
->type
));
2896 fprintf(stderr
, "%s", hba
->pci_id
? : "Err!");
2898 fprintf(stderr
, ", ");
2902 fprintf(stderr
, ").\n"
2903 " Mixing devices attached to different controllers "
2904 "is not allowed.\n");
2906 free_sys_dev(&hba_name
);
2909 super
->orom
= find_imsm_capability(hba_name
->type
);
2910 free_sys_dev(&hba_name
);
2917 /* find_missing - helper routine for load_super_imsm_all that identifies
2918 * disks that have disappeared from the system. This routine relies on
2919 * the mpb being uptodate, which it is at load time.
2921 static int find_missing(struct intel_super
*super
)
2924 struct imsm_super
*mpb
= super
->anchor
;
2926 struct imsm_disk
*disk
;
2928 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2929 disk
= __get_imsm_disk(mpb
, i
);
2930 dl
= serial_to_dl(disk
->serial
, super
);
2934 dl
= malloc(sizeof(*dl
));
2940 dl
->devname
= strdup("missing");
2942 serialcpy(dl
->serial
, disk
->serial
);
2945 dl
->next
= super
->missing
;
2946 super
->missing
= dl
;
2952 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
2954 struct intel_disk
*idisk
= disk_list
;
2957 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
2959 idisk
= idisk
->next
;
2965 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
2966 struct intel_super
*super
,
2967 struct intel_disk
**disk_list
)
2969 struct imsm_disk
*d
= &super
->disks
->disk
;
2970 struct imsm_super
*mpb
= super
->anchor
;
2973 for (i
= 0; i
< tbl_size
; i
++) {
2974 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
2975 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
2977 if (tbl_mpb
->family_num
== mpb
->family_num
) {
2978 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
2979 dprintf("%s: mpb from %d:%d matches %d:%d\n",
2980 __func__
, super
->disks
->major
,
2981 super
->disks
->minor
,
2982 table
[i
]->disks
->major
,
2983 table
[i
]->disks
->minor
);
2987 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
2988 is_configured(d
) == is_configured(tbl_d
)) &&
2989 tbl_mpb
->generation_num
< mpb
->generation_num
) {
2990 /* current version of the mpb is a
2991 * better candidate than the one in
2992 * super_table, but copy over "cross
2993 * generational" status
2995 struct intel_disk
*idisk
;
2997 dprintf("%s: mpb from %d:%d replaces %d:%d\n",
2998 __func__
, super
->disks
->major
,
2999 super
->disks
->minor
,
3000 table
[i
]->disks
->major
,
3001 table
[i
]->disks
->minor
);
3003 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
3004 if (idisk
&& is_failed(&idisk
->disk
))
3005 tbl_d
->status
|= FAILED_DISK
;
3008 struct intel_disk
*idisk
;
3009 struct imsm_disk
*disk
;
3011 /* tbl_mpb is more up to date, but copy
3012 * over cross generational status before
3015 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
3016 if (disk
&& is_failed(disk
))
3017 d
->status
|= FAILED_DISK
;
3019 idisk
= disk_list_get(d
->serial
, *disk_list
);
3022 if (disk
&& is_configured(disk
))
3023 idisk
->disk
.status
|= CONFIGURED_DISK
;
3026 dprintf("%s: mpb from %d:%d prefer %d:%d\n",
3027 __func__
, super
->disks
->major
,
3028 super
->disks
->minor
,
3029 table
[i
]->disks
->major
,
3030 table
[i
]->disks
->minor
);
3038 table
[tbl_size
++] = super
;
3042 /* update/extend the merged list of imsm_disk records */
3043 for (j
= 0; j
< mpb
->num_disks
; j
++) {
3044 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
3045 struct intel_disk
*idisk
;
3047 idisk
= disk_list_get(disk
->serial
, *disk_list
);
3049 idisk
->disk
.status
|= disk
->status
;
3050 if (is_configured(&idisk
->disk
) ||
3051 is_failed(&idisk
->disk
))
3052 idisk
->disk
.status
&= ~(SPARE_DISK
);
3054 idisk
= calloc(1, sizeof(*idisk
));
3057 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
3058 idisk
->disk
= *disk
;
3059 idisk
->next
= *disk_list
;
3063 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
3070 static struct intel_super
*
3071 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
3074 struct imsm_super
*mpb
= super
->anchor
;
3078 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3079 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3080 struct intel_disk
*idisk
;
3082 idisk
= disk_list_get(disk
->serial
, disk_list
);
3084 if (idisk
->owner
== owner
||
3085 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
3088 dprintf("%s: '%.16s' owner %d != %d\n",
3089 __func__
, disk
->serial
, idisk
->owner
,
3092 dprintf("%s: unknown disk %x [%d]: %.16s\n",
3093 __func__
, __le32_to_cpu(mpb
->family_num
), i
,
3099 if (ok_count
== mpb
->num_disks
)
3104 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
3106 struct intel_super
*s
;
3108 for (s
= super_list
; s
; s
= s
->next
) {
3109 if (family_num
!= s
->anchor
->family_num
)
3111 fprintf(stderr
, "Conflict, offlining family %#x on '%s'\n",
3112 __le32_to_cpu(family_num
), s
->disks
->devname
);
3116 static struct intel_super
*
3117 imsm_thunderdome(struct intel_super
**super_list
, int len
)
3119 struct intel_super
*super_table
[len
];
3120 struct intel_disk
*disk_list
= NULL
;
3121 struct intel_super
*champion
, *spare
;
3122 struct intel_super
*s
, **del
;
3127 memset(super_table
, 0, sizeof(super_table
));
3128 for (s
= *super_list
; s
; s
= s
->next
)
3129 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
3131 for (i
= 0; i
< tbl_size
; i
++) {
3132 struct imsm_disk
*d
;
3133 struct intel_disk
*idisk
;
3134 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
3137 d
= &s
->disks
->disk
;
3139 /* 'd' must appear in merged disk list for its
3140 * configuration to be valid
3142 idisk
= disk_list_get(d
->serial
, disk_list
);
3143 if (idisk
&& idisk
->owner
== i
)
3144 s
= validate_members(s
, disk_list
, i
);
3149 dprintf("%s: marking family: %#x from %d:%d offline\n",
3150 __func__
, mpb
->family_num
,
3151 super_table
[i
]->disks
->major
,
3152 super_table
[i
]->disks
->minor
);
3156 /* This is where the mdadm implementation differs from the Windows
3157 * driver which has no strict concept of a container. We can only
3158 * assemble one family from a container, so when returning a prodigal
3159 * array member to this system the code will not be able to disambiguate
3160 * the container contents that should be assembled ("foreign" versus
3161 * "local"). It requires user intervention to set the orig_family_num
3162 * to a new value to establish a new container. The Windows driver in
3163 * this situation fixes up the volume name in place and manages the
3164 * foreign array as an independent entity.
3169 for (i
= 0; i
< tbl_size
; i
++) {
3170 struct intel_super
*tbl_ent
= super_table
[i
];
3176 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
3181 if (s
&& !is_spare
) {
3182 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
3184 } else if (!s
&& !is_spare
)
3197 fprintf(stderr
, "Chose family %#x on '%s', "
3198 "assemble conflicts to new container with '--update=uuid'\n",
3199 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
3201 /* collect all dl's onto 'champion', and update them to
3202 * champion's version of the status
3204 for (s
= *super_list
; s
; s
= s
->next
) {
3205 struct imsm_super
*mpb
= champion
->anchor
;
3206 struct dl
*dl
= s
->disks
;
3211 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3212 struct imsm_disk
*disk
;
3214 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
3217 /* only set index on disks that are a member of
3218 * a populated contianer, i.e. one with
3221 if (is_failed(&dl
->disk
))
3223 else if (is_spare(&dl
->disk
))
3229 if (i
>= mpb
->num_disks
) {
3230 struct intel_disk
*idisk
;
3232 idisk
= disk_list_get(dl
->serial
, disk_list
);
3233 if (idisk
&& is_spare(&idisk
->disk
) &&
3234 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
3242 dl
->next
= champion
->disks
;
3243 champion
->disks
= dl
;
3247 /* delete 'champion' from super_list */
3248 for (del
= super_list
; *del
; ) {
3249 if (*del
== champion
) {
3250 *del
= (*del
)->next
;
3253 del
= &(*del
)->next
;
3255 champion
->next
= NULL
;
3259 struct intel_disk
*idisk
= disk_list
;
3261 disk_list
= disk_list
->next
;
3268 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
3272 struct intel_super
*super_list
= NULL
;
3273 struct intel_super
*super
= NULL
;
3274 int devnum
= fd2devnum(fd
);
3280 /* check if 'fd' an opened container */
3281 sra
= sysfs_read(fd
, 0, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
3285 if (sra
->array
.major_version
!= -1 ||
3286 sra
->array
.minor_version
!= -2 ||
3287 strcmp(sra
->text_version
, "imsm") != 0) {
3292 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
3293 struct intel_super
*s
= alloc_super();
3301 s
->next
= super_list
;
3305 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
3306 dfd
= dev_open(nm
, O_RDWR
);
3310 rv
= find_intel_hba_capability(dfd
, s
, devname
);
3311 /* no orom/efi or non-intel hba of the disk */
3315 err
= load_and_parse_mpb(dfd
, s
, NULL
, 1);
3317 /* retry the load if we might have raced against mdmon */
3318 if (err
== 3 && mdmon_running(devnum
))
3319 for (retry
= 0; retry
< 3; retry
++) {
3321 err
= load_and_parse_mpb(dfd
, s
, NULL
, 1);
3329 /* all mpbs enter, maybe one leaves */
3330 super
= imsm_thunderdome(&super_list
, i
);
3336 if (find_missing(super
) != 0) {
3344 while (super_list
) {
3345 struct intel_super
*s
= super_list
;
3347 super_list
= super_list
->next
;
3356 st
->container_dev
= devnum
;
3357 if (err
== 0 && st
->ss
== NULL
) {
3358 st
->ss
= &super_imsm
;
3359 st
->minor_version
= 0;
3360 st
->max_devs
= IMSM_MAX_DEVICES
;
3365 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
3367 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
);
3371 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
3373 struct intel_super
*super
;
3376 if (test_partition(fd
))
3377 /* IMSM not allowed on partitions */
3380 free_super_imsm(st
);
3382 super
= alloc_super();
3385 Name
": malloc of %zu failed.\n",
3389 rv
= find_intel_hba_capability(fd
, super
, devname
);
3390 /* no orom/efi or non-intel hba of the disk */
3394 Name
": No OROM/EFI properties for %s\n", devname
);
3399 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
3404 Name
": Failed to load all information "
3405 "sections on %s\n", devname
);
3411 if (st
->ss
== NULL
) {
3412 st
->ss
= &super_imsm
;
3413 st
->minor_version
= 0;
3414 st
->max_devs
= IMSM_MAX_DEVICES
;
3419 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
3421 if (info
->level
== 1)
3423 return info
->chunk_size
>> 9;
3426 static __u32
info_to_num_data_stripes(mdu_array_info_t
*info
, int num_domains
)
3430 num_stripes
= (info
->size
* 2) / info_to_blocks_per_strip(info
);
3431 num_stripes
/= num_domains
;
3436 static __u32
info_to_blocks_per_member(mdu_array_info_t
*info
)
3438 if (info
->level
== 1)
3439 return info
->size
* 2;
3441 return (info
->size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
3444 static void imsm_update_version_info(struct intel_super
*super
)
3446 /* update the version and attributes */
3447 struct imsm_super
*mpb
= super
->anchor
;
3449 struct imsm_dev
*dev
;
3450 struct imsm_map
*map
;
3453 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3454 dev
= get_imsm_dev(super
, i
);
3455 map
= get_imsm_map(dev
, 0);
3456 if (__le32_to_cpu(dev
->size_high
) > 0)
3457 mpb
->attributes
|= MPB_ATTRIB_2TB
;
3459 /* FIXME detect when an array spans a port multiplier */
3461 mpb
->attributes
|= MPB_ATTRIB_PM
;
3464 if (mpb
->num_raid_devs
> 1 ||
3465 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
3466 version
= MPB_VERSION_ATTRIBS
;
3467 switch (get_imsm_raid_level(map
)) {
3468 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
3469 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
3470 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
3471 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
3474 if (map
->num_members
>= 5)
3475 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
3476 else if (dev
->status
== DEV_CLONE_N_GO
)
3477 version
= MPB_VERSION_CNG
;
3478 else if (get_imsm_raid_level(map
) == 5)
3479 version
= MPB_VERSION_RAID5
;
3480 else if (map
->num_members
>= 3)
3481 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
3482 else if (get_imsm_raid_level(map
) == 1)
3483 version
= MPB_VERSION_RAID1
;
3485 version
= MPB_VERSION_RAID0
;
3487 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
3491 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
3493 struct imsm_super
*mpb
= super
->anchor
;
3494 char *reason
= NULL
;
3497 if (strlen(name
) > MAX_RAID_SERIAL_LEN
)
3498 reason
= "must be 16 characters or less";
3500 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3501 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3503 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
3504 reason
= "already exists";
3509 if (reason
&& !quiet
)
3510 fprintf(stderr
, Name
": imsm volume name %s\n", reason
);
3515 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
3516 unsigned long long size
, char *name
,
3517 char *homehost
, int *uuid
)
3519 /* We are creating a volume inside a pre-existing container.
3520 * so st->sb is already set.
3522 struct intel_super
*super
= st
->sb
;
3523 struct imsm_super
*mpb
= super
->anchor
;
3524 struct intel_dev
*dv
;
3525 struct imsm_dev
*dev
;
3526 struct imsm_vol
*vol
;
3527 struct imsm_map
*map
;
3528 int idx
= mpb
->num_raid_devs
;
3530 unsigned long long array_blocks
;
3531 size_t size_old
, size_new
;
3532 __u32 num_data_stripes
;
3534 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
3535 fprintf(stderr
, Name
": This imsm-container already has the "
3536 "maximum of %d volumes\n", super
->orom
->vpa
);
3540 /* ensure the mpb is large enough for the new data */
3541 size_old
= __le32_to_cpu(mpb
->mpb_size
);
3542 size_new
= disks_to_mpb_size(info
->nr_disks
);
3543 if (size_new
> size_old
) {
3545 size_t size_round
= ROUND_UP(size_new
, 512);
3547 if (posix_memalign(&mpb_new
, 512, size_round
) != 0) {
3548 fprintf(stderr
, Name
": could not allocate new mpb\n");
3551 memcpy(mpb_new
, mpb
, size_old
);
3554 super
->anchor
= mpb_new
;
3555 mpb
->mpb_size
= __cpu_to_le32(size_new
);
3556 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
3558 super
->current_vol
= idx
;
3559 /* when creating the first raid device in this container set num_disks
3560 * to zero, i.e. delete this spare and add raid member devices in
3561 * add_to_super_imsm_volume()
3563 if (super
->current_vol
== 0)
3566 if (!check_name(super
, name
, 0))
3568 dv
= malloc(sizeof(*dv
));
3570 fprintf(stderr
, Name
": failed to allocate device list entry\n");
3573 dev
= calloc(1, sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
3576 fprintf(stderr
, Name
": could not allocate raid device\n");
3580 strncpy((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
3581 if (info
->level
== 1)
3582 array_blocks
= info_to_blocks_per_member(info
);
3584 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
3585 info
->layout
, info
->chunk_size
,
3587 /* round array size down to closest MB */
3588 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
3590 dev
->size_low
= __cpu_to_le32((__u32
) array_blocks
);
3591 dev
->size_high
= __cpu_to_le32((__u32
) (array_blocks
>> 32));
3592 dev
->status
= (DEV_READ_COALESCING
| DEV_WRITE_COALESCING
);
3594 vol
->migr_state
= 0;
3595 set_migr_type(dev
, MIGR_INIT
);
3597 vol
->curr_migr_unit
= 0;
3598 map
= get_imsm_map(dev
, 0);
3599 map
->pba_of_lba0
= __cpu_to_le32(super
->create_offset
);
3600 map
->blocks_per_member
= __cpu_to_le32(info_to_blocks_per_member(info
));
3601 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
3602 map
->failed_disk_num
= ~0;
3603 map
->map_state
= info
->level
? IMSM_T_STATE_UNINITIALIZED
:
3604 IMSM_T_STATE_NORMAL
;
3607 if (info
->level
== 1 && info
->raid_disks
> 2) {
3610 fprintf(stderr
, Name
": imsm does not support more than 2 disks"
3611 "in a raid1 volume\n");
3615 map
->raid_level
= info
->level
;
3616 if (info
->level
== 10) {
3617 map
->raid_level
= 1;
3618 map
->num_domains
= info
->raid_disks
/ 2;
3619 } else if (info
->level
== 1)
3620 map
->num_domains
= info
->raid_disks
;
3622 map
->num_domains
= 1;
3624 num_data_stripes
= info_to_num_data_stripes(info
, map
->num_domains
);
3625 map
->num_data_stripes
= __cpu_to_le32(num_data_stripes
);
3627 map
->num_members
= info
->raid_disks
;
3628 for (i
= 0; i
< map
->num_members
; i
++) {
3629 /* initialized in add_to_super */
3630 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
3632 mpb
->num_raid_devs
++;
3635 dv
->index
= super
->current_vol
;
3636 dv
->next
= super
->devlist
;
3637 super
->devlist
= dv
;
3639 imsm_update_version_info(super
);
3644 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
3645 unsigned long long size
, char *name
,
3646 char *homehost
, int *uuid
)
3648 /* This is primarily called by Create when creating a new array.
3649 * We will then get add_to_super called for each component, and then
3650 * write_init_super called to write it out to each device.
3651 * For IMSM, Create can create on fresh devices or on a pre-existing
3653 * To create on a pre-existing array a different method will be called.
3654 * This one is just for fresh drives.
3656 struct intel_super
*super
;
3657 struct imsm_super
*mpb
;
3662 return init_super_imsm_volume(st
, info
, size
, name
, homehost
, uuid
);
3665 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
3669 super
= alloc_super();
3670 if (super
&& posix_memalign(&super
->buf
, 512, mpb_size
) != 0) {
3675 fprintf(stderr
, Name
3676 ": %s could not allocate superblock\n", __func__
);
3679 memset(super
->buf
, 0, mpb_size
);
3681 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
3685 /* zeroing superblock */
3689 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
3691 version
= (char *) mpb
->sig
;
3692 strcpy(version
, MPB_SIGNATURE
);
3693 version
+= strlen(MPB_SIGNATURE
);
3694 strcpy(version
, MPB_VERSION_RAID0
);
3700 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
3701 int fd
, char *devname
)
3703 struct intel_super
*super
= st
->sb
;
3704 struct imsm_super
*mpb
= super
->anchor
;
3706 struct imsm_dev
*dev
;
3707 struct imsm_map
*map
;
3710 dev
= get_imsm_dev(super
, super
->current_vol
);
3711 map
= get_imsm_map(dev
, 0);
3713 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
3714 fprintf(stderr
, Name
": %s: Cannot add spare devices to IMSM volume\n",
3720 /* we're doing autolayout so grab the pre-marked (in
3721 * validate_geometry) raid_disk
3723 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3724 if (dl
->raiddisk
== dk
->raid_disk
)
3727 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3728 if (dl
->major
== dk
->major
&&
3729 dl
->minor
== dk
->minor
)
3734 fprintf(stderr
, Name
": %s is not a member of the same container\n", devname
);
3738 /* add a pristine spare to the metadata */
3739 if (dl
->index
< 0) {
3740 dl
->index
= super
->anchor
->num_disks
;
3741 super
->anchor
->num_disks
++;
3743 /* Check the device has not already been added */
3744 slot
= get_imsm_disk_slot(map
, dl
->index
);
3746 (get_imsm_ord_tbl_ent(dev
, slot
, -1) & IMSM_ORD_REBUILD
) == 0) {
3747 fprintf(stderr
, Name
": %s has been included in this array twice\n",
3751 set_imsm_ord_tbl_ent(map
, dk
->number
, dl
->index
);
3752 dl
->disk
.status
= CONFIGURED_DISK
;
3754 /* if we are creating the first raid device update the family number */
3755 if (super
->current_vol
== 0) {
3757 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
3758 struct imsm_disk
*_disk
= __get_imsm_disk(mpb
, dl
->index
);
3760 if (!_dev
|| !_disk
) {
3761 fprintf(stderr
, Name
": BUG mpb setup error\n");
3767 sum
+= __gen_imsm_checksum(mpb
);
3768 mpb
->family_num
= __cpu_to_le32(sum
);
3769 mpb
->orig_family_num
= mpb
->family_num
;
3776 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
3777 int fd
, char *devname
)
3779 struct intel_super
*super
= st
->sb
;
3781 unsigned long long size
;
3786 /* If we are on an RAID enabled platform check that the disk is
3787 * attached to the raid controller.
3788 * We do not need to test disks attachment for container based additions,
3789 * they shall be already tested when container was created/assembled.
3791 rv
= find_intel_hba_capability(fd
, super
, devname
);
3792 /* no orom/efi or non-intel hba of the disk */
3794 dprintf("capability: %p fd: %d ret: %d\n",
3795 super
->orom
, fd
, rv
);
3799 if (super
->current_vol
>= 0)
3800 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
3803 dd
= malloc(sizeof(*dd
));
3806 Name
": malloc failed %s:%d.\n", __func__
, __LINE__
);
3809 memset(dd
, 0, sizeof(*dd
));
3810 dd
->major
= major(stb
.st_rdev
);
3811 dd
->minor
= minor(stb
.st_rdev
);
3813 dd
->devname
= devname
? strdup(devname
) : NULL
;
3816 dd
->action
= DISK_ADD
;
3817 rv
= imsm_read_serial(fd
, devname
, dd
->serial
);
3820 Name
": failed to retrieve scsi serial, aborting\n");
3825 get_dev_size(fd
, NULL
, &size
);
3827 serialcpy(dd
->disk
.serial
, dd
->serial
);
3828 dd
->disk
.total_blocks
= __cpu_to_le32(size
);
3829 dd
->disk
.status
= SPARE_DISK
;
3830 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
3831 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
3833 dd
->disk
.scsi_id
= __cpu_to_le32(0);
3835 if (st
->update_tail
) {
3836 dd
->next
= super
->disk_mgmt_list
;
3837 super
->disk_mgmt_list
= dd
;
3839 dd
->next
= super
->disks
;
3847 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
3849 struct intel_super
*super
= st
->sb
;
3852 /* remove from super works only in mdmon - for communication
3853 * manager - monitor. Check if communication memory buffer
3856 if (!st
->update_tail
) {
3858 Name
": %s shall be used in mdmon context only"
3859 "(line %d).\n", __func__
, __LINE__
);
3862 dd
= malloc(sizeof(*dd
));
3865 Name
": malloc failed %s:%d.\n", __func__
, __LINE__
);
3868 memset(dd
, 0, sizeof(*dd
));
3869 dd
->major
= dk
->major
;
3870 dd
->minor
= dk
->minor
;
3873 dd
->disk
.status
= SPARE_DISK
;
3874 dd
->action
= DISK_REMOVE
;
3876 dd
->next
= super
->disk_mgmt_list
;
3877 super
->disk_mgmt_list
= dd
;
3883 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
3887 struct imsm_super anchor
;
3888 } spare_record
__attribute__ ((aligned(512)));
3890 /* spare records have their own family number and do not have any defined raid
3893 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
3895 struct imsm_super
*mpb
= super
->anchor
;
3896 struct imsm_super
*spare
= &spare_record
.anchor
;
3900 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
)),
3901 spare
->generation_num
= __cpu_to_le32(1UL),
3902 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
3903 spare
->num_disks
= 1,
3904 spare
->num_raid_devs
= 0,
3905 spare
->cache_size
= mpb
->cache_size
,
3906 spare
->pwr_cycle_count
= __cpu_to_le32(1),
3908 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
3909 MPB_SIGNATURE MPB_VERSION_RAID0
);
3911 for (d
= super
->disks
; d
; d
= d
->next
) {
3915 spare
->disk
[0] = d
->disk
;
3916 sum
= __gen_imsm_checksum(spare
);
3917 spare
->family_num
= __cpu_to_le32(sum
);
3918 spare
->orig_family_num
= 0;
3919 sum
= __gen_imsm_checksum(spare
);
3920 spare
->check_sum
= __cpu_to_le32(sum
);
3922 if (store_imsm_mpb(d
->fd
, spare
)) {
3923 fprintf(stderr
, "%s: failed for device %d:%d %s\n",
3924 __func__
, d
->major
, d
->minor
, strerror(errno
));
3936 static int write_super_imsm(struct supertype
*st
, int doclose
)
3938 struct intel_super
*super
= st
->sb
;
3939 struct imsm_super
*mpb
= super
->anchor
;
3945 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
3948 /* 'generation' is incremented everytime the metadata is written */
3949 generation
= __le32_to_cpu(mpb
->generation_num
);
3951 mpb
->generation_num
= __cpu_to_le32(generation
);
3953 /* fix up cases where previous mdadm releases failed to set
3956 if (mpb
->orig_family_num
== 0)
3957 mpb
->orig_family_num
= mpb
->family_num
;
3959 for (d
= super
->disks
; d
; d
= d
->next
) {
3963 mpb
->disk
[d
->index
] = d
->disk
;
3967 for (d
= super
->missing
; d
; d
= d
->next
) {
3968 mpb
->disk
[d
->index
] = d
->disk
;
3971 mpb
->num_disks
= num_disks
;
3972 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
3974 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3975 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
3976 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
3978 imsm_copy_dev(dev
, dev2
);
3979 mpb_size
+= sizeof_imsm_dev(dev
, 0);
3982 mpb_size
+= __le32_to_cpu(mpb
->bbm_log_size
);
3983 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
3985 /* recalculate checksum */
3986 sum
= __gen_imsm_checksum(mpb
);
3987 mpb
->check_sum
= __cpu_to_le32(sum
);
3989 /* write the mpb for disks that compose raid devices */
3990 for (d
= super
->disks
; d
; d
= d
->next
) {
3993 if (store_imsm_mpb(d
->fd
, mpb
))
3994 fprintf(stderr
, "%s: failed for device %d:%d %s\n",
3995 __func__
, d
->major
, d
->minor
, strerror(errno
));
4003 return write_super_imsm_spares(super
, doclose
);
4009 static int create_array(struct supertype
*st
, int dev_idx
)
4012 struct imsm_update_create_array
*u
;
4013 struct intel_super
*super
= st
->sb
;
4014 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
4015 struct imsm_map
*map
= get_imsm_map(dev
, 0);
4016 struct disk_info
*inf
;
4017 struct imsm_disk
*disk
;
4020 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
4021 sizeof(*inf
) * map
->num_members
;
4024 fprintf(stderr
, "%s: failed to allocate update buffer\n",
4029 u
->type
= update_create_array
;
4030 u
->dev_idx
= dev_idx
;
4031 imsm_copy_dev(&u
->dev
, dev
);
4032 inf
= get_disk_info(u
);
4033 for (i
= 0; i
< map
->num_members
; i
++) {
4034 int idx
= get_imsm_disk_idx(dev
, i
, -1);
4036 disk
= get_imsm_disk(super
, idx
);
4037 serialcpy(inf
[i
].serial
, disk
->serial
);
4039 append_metadata_update(st
, u
, len
);
4044 static int mgmt_disk(struct supertype
*st
)
4046 struct intel_super
*super
= st
->sb
;
4048 struct imsm_update_add_remove_disk
*u
;
4050 if (!super
->disk_mgmt_list
)
4056 fprintf(stderr
, "%s: failed to allocate update buffer\n",
4061 u
->type
= update_add_remove_disk
;
4062 append_metadata_update(st
, u
, len
);
4067 static int write_init_super_imsm(struct supertype
*st
)
4069 struct intel_super
*super
= st
->sb
;
4070 int current_vol
= super
->current_vol
;
4072 /* we are done with current_vol reset it to point st at the container */
4073 super
->current_vol
= -1;
4075 if (st
->update_tail
) {
4076 /* queue the recently created array / added disk
4077 * as a metadata update */
4080 /* determine if we are creating a volume or adding a disk */
4081 if (current_vol
< 0) {
4082 /* in the mgmt (add/remove) disk case we are running
4083 * in mdmon context, so don't close fd's
4085 return mgmt_disk(st
);
4087 rv
= create_array(st
, current_vol
);
4092 for (d
= super
->disks
; d
; d
= d
->next
)
4093 Kill(d
->devname
, NULL
, 0, 1, 1);
4094 return write_super_imsm(st
, 1);
4099 static int store_super_imsm(struct supertype
*st
, int fd
)
4101 struct intel_super
*super
= st
->sb
;
4102 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
4108 return store_imsm_mpb(fd
, mpb
);
4114 static int imsm_bbm_log_size(struct imsm_super
*mpb
)
4116 return __le32_to_cpu(mpb
->bbm_log_size
);
4120 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
4121 int layout
, int raiddisks
, int chunk
,
4122 unsigned long long size
, char *dev
,
4123 unsigned long long *freesize
,
4127 unsigned long long ldsize
;
4128 struct intel_super
*super
=NULL
;
4131 if (level
!= LEVEL_CONTAINER
)
4136 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
4139 fprintf(stderr
, Name
": imsm: Cannot open %s: %s\n",
4140 dev
, strerror(errno
));
4143 if (!get_dev_size(fd
, dev
, &ldsize
)) {
4148 /* capabilities retrieve could be possible
4149 * note that there is no fd for the disks in array.
4151 super
= alloc_super();
4154 Name
": malloc of %zu failed.\n",
4160 rv
= find_intel_hba_capability(fd
, super
, verbose
? dev
: NULL
);
4164 fd2devname(fd
, str
);
4165 dprintf("validate_geometry_imsm_container: fd: %d %s orom: %p rv: %d raiddisk: %d\n",
4166 fd
, str
, super
->orom
, rv
, raiddisks
);
4168 /* no orom/efi or non-intel hba of the disk */
4174 if (super
->orom
&& raiddisks
> super
->orom
->tds
) {
4176 fprintf(stderr
, Name
": %d exceeds maximum number of"
4177 " platform supported disks: %d\n",
4178 raiddisks
, super
->orom
->tds
);
4184 *freesize
= avail_size_imsm(st
, ldsize
>> 9);
4190 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
4192 const unsigned long long base_start
= e
[*idx
].start
;
4193 unsigned long long end
= base_start
+ e
[*idx
].size
;
4196 if (base_start
== end
)
4200 for (i
= *idx
; i
< num_extents
; i
++) {
4201 /* extend overlapping extents */
4202 if (e
[i
].start
>= base_start
&&
4203 e
[i
].start
<= end
) {
4206 if (e
[i
].start
+ e
[i
].size
> end
)
4207 end
= e
[i
].start
+ e
[i
].size
;
4208 } else if (e
[i
].start
> end
) {
4214 return end
- base_start
;
4217 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
4219 /* build a composite disk with all known extents and generate a new
4220 * 'maxsize' given the "all disks in an array must share a common start
4221 * offset" constraint
4223 struct extent
*e
= calloc(sum_extents
, sizeof(*e
));
4227 unsigned long long pos
;
4228 unsigned long long start
= 0;
4229 unsigned long long maxsize
;
4230 unsigned long reserve
;
4235 /* coalesce and sort all extents. also, check to see if we need to
4236 * reserve space between member arrays
4239 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
4242 for (i
= 0; i
< dl
->extent_cnt
; i
++)
4245 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
4250 while (i
< sum_extents
) {
4251 e
[j
].start
= e
[i
].start
;
4252 e
[j
].size
= find_size(e
, &i
, sum_extents
);
4254 if (e
[j
-1].size
== 0)
4263 unsigned long long esize
;
4265 esize
= e
[i
].start
- pos
;
4266 if (esize
>= maxsize
) {
4271 pos
= e
[i
].start
+ e
[i
].size
;
4273 } while (e
[i
-1].size
);
4279 /* FIXME assumes volume at offset 0 is the first volume in a
4282 if (start_extent
> 0)
4283 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
4287 if (maxsize
< reserve
)
4290 super
->create_offset
= ~((__u32
) 0);
4291 if (start
+ reserve
> super
->create_offset
)
4292 return 0; /* start overflows create_offset */
4293 super
->create_offset
= start
+ reserve
;
4295 return maxsize
- reserve
;
4298 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
4300 if (level
< 0 || level
== 6 || level
== 4)
4303 /* if we have an orom prevent invalid raid levels */
4306 case 0: return imsm_orom_has_raid0(orom
);
4309 return imsm_orom_has_raid1e(orom
);
4310 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
4311 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
4312 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
4315 return 1; /* not on an Intel RAID platform so anything goes */
4321 #define pr_vrb(fmt, arg...) (void) (verbose && fprintf(stderr, Name fmt, ##arg))
4323 * validate volume parameters with OROM/EFI capabilities
4326 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
4327 int raiddisks
, int *chunk
, int verbose
)
4332 /* validate container capabilities */
4333 if (super
->orom
&& raiddisks
> super
->orom
->tds
) {
4335 fprintf(stderr
, Name
": %d exceeds maximum number of"
4336 " platform supported disks: %d\n",
4337 raiddisks
, super
->orom
->tds
);
4341 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
4342 if (super
->orom
&& (!is_raid_level_supported(super
->orom
, level
,
4344 pr_vrb(": platform does not support raid%d with %d disk%s\n",
4345 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
4348 if (super
->orom
&& level
!= 1) {
4349 if (chunk
&& (*chunk
== 0 || *chunk
== UnSet
))
4350 *chunk
= imsm_orom_default_chunk(super
->orom
);
4351 else if (chunk
&& !imsm_orom_has_chunk(super
->orom
, *chunk
)) {
4352 pr_vrb(": platform does not support a chunk size of: "
4357 if (layout
!= imsm_level_to_layout(level
)) {
4359 pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n");
4360 else if (level
== 10)
4361 pr_vrb(": imsm raid 10 only supports the n2 layout\n");
4363 pr_vrb(": imsm unknown layout %#x for this raid level %d\n",
4370 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
4371 * FIX ME add ahci details
4373 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
4374 int layout
, int raiddisks
, int *chunk
,
4375 unsigned long long size
, char *dev
,
4376 unsigned long long *freesize
,
4380 struct intel_super
*super
= st
->sb
;
4381 struct imsm_super
*mpb
= super
->anchor
;
4383 unsigned long long pos
= 0;
4384 unsigned long long maxsize
;
4388 /* We must have the container info already read in. */
4392 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, verbose
)) {
4393 fprintf(stderr
, Name
": RAID gemetry validation failed. "
4394 "Cannot proceed with the action(s).\n");
4398 /* General test: make sure there is space for
4399 * 'raiddisks' device extents of size 'size' at a given
4402 unsigned long long minsize
= size
;
4403 unsigned long long start_offset
= MaxSector
;
4406 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
4407 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
4412 e
= get_extents(super
, dl
);
4415 unsigned long long esize
;
4416 esize
= e
[i
].start
- pos
;
4417 if (esize
>= minsize
)
4419 if (found
&& start_offset
== MaxSector
) {
4422 } else if (found
&& pos
!= start_offset
) {
4426 pos
= e
[i
].start
+ e
[i
].size
;
4428 } while (e
[i
-1].size
);
4433 if (dcnt
< raiddisks
) {
4435 fprintf(stderr
, Name
": imsm: Not enough "
4436 "devices with space for this array "
4444 /* This device must be a member of the set */
4445 if (stat(dev
, &stb
) < 0)
4447 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
4449 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
4450 if (dl
->major
== (int)major(stb
.st_rdev
) &&
4451 dl
->minor
== (int)minor(stb
.st_rdev
))
4456 fprintf(stderr
, Name
": %s is not in the "
4457 "same imsm set\n", dev
);
4459 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
4460 /* If a volume is present then the current creation attempt
4461 * cannot incorporate new spares because the orom may not
4462 * understand this configuration (all member disks must be
4463 * members of each array in the container).
4465 fprintf(stderr
, Name
": %s is a spare and a volume"
4466 " is already defined for this container\n", dev
);
4467 fprintf(stderr
, Name
": The option-rom requires all member"
4468 " disks to be a member of all volumes\n");
4472 /* retrieve the largest free space block */
4473 e
= get_extents(super
, dl
);
4478 unsigned long long esize
;
4480 esize
= e
[i
].start
- pos
;
4481 if (esize
>= maxsize
)
4483 pos
= e
[i
].start
+ e
[i
].size
;
4485 } while (e
[i
-1].size
);
4490 fprintf(stderr
, Name
": unable to determine free space for: %s\n",
4494 if (maxsize
< size
) {
4496 fprintf(stderr
, Name
": %s not enough space (%llu < %llu)\n",
4497 dev
, maxsize
, size
);
4501 /* count total number of extents for merge */
4503 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4505 i
+= dl
->extent_cnt
;
4507 maxsize
= merge_extents(super
, i
);
4508 if (maxsize
< size
|| maxsize
== 0) {
4510 fprintf(stderr
, Name
": not enough space after merge (%llu < %llu)\n",
4515 *freesize
= maxsize
;
4520 static int reserve_space(struct supertype
*st
, int raiddisks
,
4521 unsigned long long size
, int chunk
,
4522 unsigned long long *freesize
)
4524 struct intel_super
*super
= st
->sb
;
4525 struct imsm_super
*mpb
= super
->anchor
;
4530 unsigned long long maxsize
;
4531 unsigned long long minsize
;
4535 /* find the largest common start free region of the possible disks */
4539 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
4545 /* don't activate new spares if we are orom constrained
4546 * and there is already a volume active in the container
4548 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
4551 e
= get_extents(super
, dl
);
4554 for (i
= 1; e
[i
-1].size
; i
++)
4562 maxsize
= merge_extents(super
, extent_cnt
);
4566 minsize
= chunk
* 2;
4568 if (cnt
< raiddisks
||
4569 (super
->orom
&& used
&& used
!= raiddisks
) ||
4570 maxsize
< minsize
||
4572 fprintf(stderr
, Name
": not enough devices with space to create array.\n");
4573 return 0; /* No enough free spaces large enough */
4585 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4587 dl
->raiddisk
= cnt
++;
4594 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
4595 int raiddisks
, int *chunk
, unsigned long long size
,
4596 char *dev
, unsigned long long *freesize
,
4604 * if given unused devices create a container
4605 * if given given devices in a container create a member volume
4607 if (level
== LEVEL_CONTAINER
) {
4608 /* Must be a fresh device to add to a container */
4609 return validate_geometry_imsm_container(st
, level
, layout
,
4611 chunk
?*chunk
:0, size
,
4617 if (st
->sb
&& freesize
) {
4618 /* we are being asked to automatically layout a
4619 * new volume based on the current contents of
4620 * the container. If the the parameters can be
4621 * satisfied reserve_space will record the disks,
4622 * start offset, and size of the volume to be
4623 * created. add_to_super and getinfo_super
4624 * detect when autolayout is in progress.
4626 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
4630 return reserve_space(st
, raiddisks
, size
,
4631 chunk
?*chunk
:0, freesize
);
4636 /* creating in a given container */
4637 return validate_geometry_imsm_volume(st
, level
, layout
,
4638 raiddisks
, chunk
, size
,
4639 dev
, freesize
, verbose
);
4642 /* This device needs to be a device in an 'imsm' container */
4643 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
4647 Name
": Cannot create this array on device %s\n",
4652 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
4654 fprintf(stderr
, Name
": Cannot open %s: %s\n",
4655 dev
, strerror(errno
));
4658 /* Well, it is in use by someone, maybe an 'imsm' container. */
4659 cfd
= open_container(fd
);
4663 fprintf(stderr
, Name
": Cannot use %s: It is busy\n",
4667 sra
= sysfs_read(cfd
, 0, GET_VERSION
);
4668 if (sra
&& sra
->array
.major_version
== -1 &&
4669 strcmp(sra
->text_version
, "imsm") == 0)
4673 /* This is a member of a imsm container. Load the container
4674 * and try to create a volume
4676 struct intel_super
*super
;
4678 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
) == 0) {
4680 st
->container_dev
= fd2devnum(cfd
);
4682 return validate_geometry_imsm_volume(st
, level
, layout
,
4690 fprintf(stderr
, Name
": failed container membership check\n");
4696 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
4698 struct intel_super
*super
= st
->sb
;
4700 if (level
&& *level
== UnSet
)
4701 *level
= LEVEL_CONTAINER
;
4703 if (level
&& layout
&& *layout
== UnSet
)
4704 *layout
= imsm_level_to_layout(*level
);
4706 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0) &&
4707 super
&& super
->orom
)
4708 *chunk
= imsm_orom_default_chunk(super
->orom
);
4711 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
4713 static int kill_subarray_imsm(struct supertype
*st
)
4715 /* remove the subarray currently referenced by ->current_vol */
4717 struct intel_dev
**dp
;
4718 struct intel_super
*super
= st
->sb
;
4719 __u8 current_vol
= super
->current_vol
;
4720 struct imsm_super
*mpb
= super
->anchor
;
4722 if (super
->current_vol
< 0)
4724 super
->current_vol
= -1; /* invalidate subarray cursor */
4726 /* block deletions that would change the uuid of active subarrays
4728 * FIXME when immutable ids are available, but note that we'll
4729 * also need to fixup the invalidated/active subarray indexes in
4732 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4735 if (i
< current_vol
)
4737 sprintf(subarray
, "%u", i
);
4738 if (is_subarray_active(subarray
, st
->devname
)) {
4740 Name
": deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
4747 if (st
->update_tail
) {
4748 struct imsm_update_kill_array
*u
= malloc(sizeof(*u
));
4752 u
->type
= update_kill_array
;
4753 u
->dev_idx
= current_vol
;
4754 append_metadata_update(st
, u
, sizeof(*u
));
4759 for (dp
= &super
->devlist
; *dp
;)
4760 if ((*dp
)->index
== current_vol
) {
4763 handle_missing(super
, (*dp
)->dev
);
4764 if ((*dp
)->index
> current_vol
)
4769 /* no more raid devices, all active components are now spares,
4770 * but of course failed are still failed
4772 if (--mpb
->num_raid_devs
== 0) {
4775 for (d
= super
->disks
; d
; d
= d
->next
)
4776 if (d
->index
> -2) {
4778 d
->disk
.status
= SPARE_DISK
;
4782 super
->updates_pending
++;
4787 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
4788 char *update
, struct mddev_ident
*ident
)
4790 /* update the subarray currently referenced by ->current_vol */
4791 struct intel_super
*super
= st
->sb
;
4792 struct imsm_super
*mpb
= super
->anchor
;
4794 if (strcmp(update
, "name") == 0) {
4795 char *name
= ident
->name
;
4799 if (is_subarray_active(subarray
, st
->devname
)) {
4801 Name
": Unable to update name of active subarray\n");
4805 if (!check_name(super
, name
, 0))
4808 vol
= strtoul(subarray
, &ep
, 10);
4809 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
4812 if (st
->update_tail
) {
4813 struct imsm_update_rename_array
*u
= malloc(sizeof(*u
));
4817 u
->type
= update_rename_array
;
4819 snprintf((char *) u
->name
, MAX_RAID_SERIAL_LEN
, "%s", name
);
4820 append_metadata_update(st
, u
, sizeof(*u
));
4822 struct imsm_dev
*dev
;
4825 dev
= get_imsm_dev(super
, vol
);
4826 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
4827 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4828 dev
= get_imsm_dev(super
, i
);
4829 handle_missing(super
, dev
);
4831 super
->updates_pending
++;
4839 static int is_gen_migration(struct imsm_dev
*dev
)
4841 if (!dev
->vol
.migr_state
)
4844 if (migr_type(dev
) == MIGR_GEN_MIGR
)
4849 #endif /* MDASSEMBLE */
4851 static int is_rebuilding(struct imsm_dev
*dev
)
4853 struct imsm_map
*migr_map
;
4855 if (!dev
->vol
.migr_state
)
4858 if (migr_type(dev
) != MIGR_REBUILD
)
4861 migr_map
= get_imsm_map(dev
, 1);
4863 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
4869 static void update_recovery_start(struct imsm_dev
*dev
, struct mdinfo
*array
)
4871 struct mdinfo
*rebuild
= NULL
;
4875 if (!is_rebuilding(dev
))
4878 /* Find the rebuild target, but punt on the dual rebuild case */
4879 for (d
= array
->devs
; d
; d
= d
->next
)
4880 if (d
->recovery_start
== 0) {
4887 /* (?) none of the disks are marked with
4888 * IMSM_ORD_REBUILD, so assume they are missing and the
4889 * disk_ord_tbl was not correctly updated
4891 dprintf("%s: failed to locate out-of-sync disk\n", __func__
);
4895 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
4896 rebuild
->recovery_start
= units
* blocks_per_migr_unit(dev
);
4900 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
4902 /* Given a container loaded by load_super_imsm_all,
4903 * extract information about all the arrays into
4905 * If 'subarray' is given, just extract info about that array.
4907 * For each imsm_dev create an mdinfo, fill it in,
4908 * then look for matching devices in super->disks
4909 * and create appropriate device mdinfo.
4911 struct intel_super
*super
= st
->sb
;
4912 struct imsm_super
*mpb
= super
->anchor
;
4913 struct mdinfo
*rest
= NULL
;
4917 int spare_disks
= 0;
4919 /* check for bad blocks */
4920 if (imsm_bbm_log_size(super
->anchor
))
4923 /* count spare devices, not used in maps
4925 for (d
= super
->disks
; d
; d
= d
->next
)
4929 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4930 struct imsm_dev
*dev
;
4931 struct imsm_map
*map
;
4932 struct imsm_map
*map2
;
4933 struct mdinfo
*this;
4938 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
4941 dev
= get_imsm_dev(super
, i
);
4942 map
= get_imsm_map(dev
, 0);
4943 map2
= get_imsm_map(dev
, 1);
4945 /* do not publish arrays that are in the middle of an
4946 * unsupported migration
4948 if (dev
->vol
.migr_state
&&
4949 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
4950 fprintf(stderr
, Name
": cannot assemble volume '%.16s':"
4951 " unsupported migration in progress\n",
4955 /* do not publish arrays that are not support by controller's
4959 chunk
= __le16_to_cpu(map
->blocks_per_strip
) >> 1;
4960 if (!validate_geometry_imsm_orom(super
,
4961 get_imsm_raid_level(map
), /* RAID level */
4962 imsm_level_to_layout(get_imsm_raid_level(map
)),
4963 map
->num_members
, /* raid disks */
4966 fprintf(stderr
, Name
": RAID gemetry validation failed. "
4967 "Cannot proceed with the action(s).\n");
4970 this = malloc(sizeof(*this));
4972 fprintf(stderr
, Name
": failed to allocate %zu bytes\n",
4976 memset(this, 0, sizeof(*this));
4979 super
->current_vol
= i
;
4980 getinfo_super_imsm_volume(st
, this, NULL
);
4981 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
4982 unsigned long long recovery_start
;
4983 struct mdinfo
*info_d
;
4990 idx
= get_imsm_disk_idx(dev
, slot
, 0);
4991 ord
= get_imsm_ord_tbl_ent(dev
, slot
, -1);
4992 for (d
= super
->disks
; d
; d
= d
->next
)
4993 if (d
->index
== idx
)
4996 recovery_start
= MaxSector
;
4999 if (d
&& is_failed(&d
->disk
))
5001 if (ord
& IMSM_ORD_REBUILD
)
5005 * if we skip some disks the array will be assmebled degraded;
5006 * reset resync start to avoid a dirty-degraded
5007 * situation when performing the intial sync
5009 * FIXME handle dirty degraded
5011 if ((skip
|| recovery_start
== 0) && !dev
->vol
.dirty
)
5012 this->resync_start
= MaxSector
;
5016 info_d
= calloc(1, sizeof(*info_d
));
5018 fprintf(stderr
, Name
": failed to allocate disk"
5019 " for volume %.16s\n", dev
->volume
);
5020 info_d
= this->devs
;
5022 struct mdinfo
*d
= info_d
->next
;
5031 info_d
->next
= this->devs
;
5032 this->devs
= info_d
;
5034 info_d
->disk
.number
= d
->index
;
5035 info_d
->disk
.major
= d
->major
;
5036 info_d
->disk
.minor
= d
->minor
;
5037 info_d
->disk
.raid_disk
= slot
;
5038 info_d
->recovery_start
= recovery_start
;
5040 if (slot
< map2
->num_members
)
5041 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
5043 this->array
.spare_disks
++;
5045 if (slot
< map
->num_members
)
5046 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
5048 this->array
.spare_disks
++;
5050 if (info_d
->recovery_start
== MaxSector
)
5051 this->array
.working_disks
++;
5053 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
5054 info_d
->data_offset
= __le32_to_cpu(map
->pba_of_lba0
);
5055 info_d
->component_size
= __le32_to_cpu(map
->blocks_per_member
);
5057 /* now that the disk list is up-to-date fixup recovery_start */
5058 update_recovery_start(dev
, this);
5059 this->array
.spare_disks
+= spare_disks
;
5063 /* if array has bad blocks, set suitable bit in array status */
5065 rest
->array
.state
|= (1<<MD_SB_BBM_ERRORS
);
5071 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
, int failed
)
5073 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5076 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
5077 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
5079 switch (get_imsm_raid_level(map
)) {
5081 return IMSM_T_STATE_FAILED
;
5084 if (failed
< map
->num_members
)
5085 return IMSM_T_STATE_DEGRADED
;
5087 return IMSM_T_STATE_FAILED
;
5092 * check to see if any mirrors have failed, otherwise we
5093 * are degraded. Even numbered slots are mirrored on
5097 /* gcc -Os complains that this is unused */
5098 int insync
= insync
;
5100 for (i
= 0; i
< map
->num_members
; i
++) {
5101 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, -1);
5102 int idx
= ord_to_idx(ord
);
5103 struct imsm_disk
*disk
;
5105 /* reset the potential in-sync count on even-numbered
5106 * slots. num_copies is always 2 for imsm raid10
5111 disk
= get_imsm_disk(super
, idx
);
5112 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
5115 /* no in-sync disks left in this mirror the
5119 return IMSM_T_STATE_FAILED
;
5122 return IMSM_T_STATE_DEGRADED
;
5126 return IMSM_T_STATE_DEGRADED
;
5128 return IMSM_T_STATE_FAILED
;
5134 return map
->map_state
;
5137 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
)
5141 struct imsm_disk
*disk
;
5142 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5143 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
);
5147 /* at the beginning of migration we set IMSM_ORD_REBUILD on
5148 * disks that are being rebuilt. New failures are recorded to
5149 * map[0]. So we look through all the disks we started with and
5150 * see if any failures are still present, or if any new ones
5153 * FIXME add support for online capacity expansion and
5154 * raid-level-migration
5156 for (i
= 0; i
< prev
->num_members
; i
++) {
5157 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
5158 ord
|= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
5159 idx
= ord_to_idx(ord
);
5161 disk
= get_imsm_disk(super
, idx
);
5162 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
5170 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
5173 struct intel_super
*super
= c
->sb
;
5174 struct imsm_super
*mpb
= super
->anchor
;
5176 if (atoi(inst
) >= mpb
->num_raid_devs
) {
5177 fprintf(stderr
, "%s: subarry index %d, out of range\n",
5178 __func__
, atoi(inst
));
5182 dprintf("imsm: open_new %s\n", inst
);
5183 a
->info
.container_member
= atoi(inst
);
5187 static int is_resyncing(struct imsm_dev
*dev
)
5189 struct imsm_map
*migr_map
;
5191 if (!dev
->vol
.migr_state
)
5194 if (migr_type(dev
) == MIGR_INIT
||
5195 migr_type(dev
) == MIGR_REPAIR
)
5198 if (migr_type(dev
) == MIGR_GEN_MIGR
)
5201 migr_map
= get_imsm_map(dev
, 1);
5203 if ((migr_map
->map_state
== IMSM_T_STATE_NORMAL
) &&
5204 (dev
->vol
.migr_type
!= MIGR_GEN_MIGR
))
5210 /* return true if we recorded new information */
5211 static int mark_failure(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
5215 struct imsm_map
*map
;
5216 char buf
[MAX_RAID_SERIAL_LEN
+3];
5217 unsigned int len
, shift
= 0;
5219 /* new failures are always set in map[0] */
5220 map
= get_imsm_map(dev
, 0);
5222 slot
= get_imsm_disk_slot(map
, idx
);
5226 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
5227 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
5230 sprintf(buf
, "%s:0", disk
->serial
);
5231 if ((len
= strlen(buf
)) >= MAX_RAID_SERIAL_LEN
)
5232 shift
= len
- MAX_RAID_SERIAL_LEN
+ 1;
5233 strncpy((char *)disk
->serial
, &buf
[shift
], MAX_RAID_SERIAL_LEN
);
5235 disk
->status
|= FAILED_DISK
;
5236 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
5237 if (map
->failed_disk_num
== 0xff)
5238 map
->failed_disk_num
= slot
;
5242 static void mark_missing(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
5244 mark_failure(dev
, disk
, idx
);
5246 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
5249 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
5250 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
5253 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
5259 if (!super
->missing
)
5261 failed
= imsm_count_failed(super
, dev
);
5262 map_state
= imsm_check_degraded(super
, dev
, failed
);
5264 dprintf("imsm: mark missing\n");
5265 end_migration(dev
, map_state
);
5266 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
5267 mark_missing(dev
, &dl
->disk
, dl
->index
);
5268 super
->updates_pending
++;
5271 static unsigned long long imsm_set_array_size(struct imsm_dev
*dev
)
5273 int used_disks
= imsm_num_data_members(dev
, 0);
5274 unsigned long long array_blocks
;
5275 struct imsm_map
*map
;
5277 if (used_disks
== 0) {
5278 /* when problems occures
5279 * return current array_blocks value
5281 array_blocks
= __le32_to_cpu(dev
->size_high
);
5282 array_blocks
= array_blocks
<< 32;
5283 array_blocks
+= __le32_to_cpu(dev
->size_low
);
5285 return array_blocks
;
5288 /* set array size in metadata
5290 map
= get_imsm_map(dev
, 0);
5291 array_blocks
= map
->blocks_per_member
* used_disks
;
5293 /* round array size down to closest MB
5295 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
5296 dev
->size_low
= __cpu_to_le32((__u32
)array_blocks
);
5297 dev
->size_high
= __cpu_to_le32((__u32
)(array_blocks
>> 32));
5299 return array_blocks
;
5302 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
5304 static void imsm_progress_container_reshape(struct intel_super
*super
)
5306 /* if no device has a migr_state, but some device has a
5307 * different number of members than the previous device, start
5308 * changing the number of devices in this device to match
5311 struct imsm_super
*mpb
= super
->anchor
;
5312 int prev_disks
= -1;
5316 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5317 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
5318 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5319 struct imsm_map
*map2
;
5320 int prev_num_members
;
5322 if (dev
->vol
.migr_state
)
5325 if (prev_disks
== -1)
5326 prev_disks
= map
->num_members
;
5327 if (prev_disks
== map
->num_members
)
5330 /* OK, this array needs to enter reshape mode.
5331 * i.e it needs a migr_state
5334 copy_map_size
= sizeof_imsm_map(map
);
5335 prev_num_members
= map
->num_members
;
5336 map
->num_members
= prev_disks
;
5337 dev
->vol
.migr_state
= 1;
5338 dev
->vol
.curr_migr_unit
= 0;
5339 dev
->vol
.migr_type
= MIGR_GEN_MIGR
;
5340 for (i
= prev_num_members
;
5341 i
< map
->num_members
; i
++)
5342 set_imsm_ord_tbl_ent(map
, i
, i
);
5343 map2
= get_imsm_map(dev
, 1);
5344 /* Copy the current map */
5345 memcpy(map2
, map
, copy_map_size
);
5346 map2
->num_members
= prev_num_members
;
5348 imsm_set_array_size(dev
);
5349 super
->updates_pending
++;
5353 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
5354 * states are handled in imsm_set_disk() with one exception, when a
5355 * resync is stopped due to a new failure this routine will set the
5356 * 'degraded' state for the array.
5358 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
5360 int inst
= a
->info
.container_member
;
5361 struct intel_super
*super
= a
->container
->sb
;
5362 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
5363 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5364 int failed
= imsm_count_failed(super
, dev
);
5365 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
);
5366 __u32 blocks_per_unit
;
5368 if (dev
->vol
.migr_state
&&
5369 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
5370 /* array state change is blocked due to reshape action
5372 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
5373 * - finish the reshape (if last_checkpoint is big and action != reshape)
5374 * - update curr_migr_unit
5376 if (a
->curr_action
== reshape
) {
5377 /* still reshaping, maybe update curr_migr_unit */
5378 goto mark_checkpoint
;
5380 if (a
->last_checkpoint
== 0 && a
->prev_action
== reshape
) {
5381 /* for some reason we aborted the reshape.
5384 struct imsm_map
*map2
= get_imsm_map(dev
, 1);
5385 dev
->vol
.migr_state
= 0;
5386 dev
->vol
.migr_type
= 0;
5387 dev
->vol
.curr_migr_unit
= 0;
5388 memcpy(map
, map2
, sizeof_imsm_map(map2
));
5389 super
->updates_pending
++;
5391 if (a
->last_checkpoint
>= a
->info
.component_size
) {
5392 unsigned long long array_blocks
;
5396 used_disks
= imsm_num_data_members(dev
, 0);
5397 if (used_disks
> 0) {
5399 map
->blocks_per_member
*
5401 /* round array size down to closest MB
5403 array_blocks
= (array_blocks
5404 >> SECT_PER_MB_SHIFT
)
5405 << SECT_PER_MB_SHIFT
;
5406 a
->info
.custom_array_size
= array_blocks
;
5407 /* encourage manager to update array
5411 a
->check_reshape
= 1;
5413 /* finalize online capacity expansion/reshape */
5414 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
5416 mdi
->disk
.raid_disk
,
5419 imsm_progress_container_reshape(super
);
5424 /* before we activate this array handle any missing disks */
5425 if (consistent
== 2)
5426 handle_missing(super
, dev
);
5428 if (consistent
== 2 &&
5429 (!is_resync_complete(&a
->info
) ||
5430 map_state
!= IMSM_T_STATE_NORMAL
||
5431 dev
->vol
.migr_state
))
5434 if (is_resync_complete(&a
->info
)) {
5435 /* complete intialization / resync,
5436 * recovery and interrupted recovery is completed in
5439 if (is_resyncing(dev
)) {
5440 dprintf("imsm: mark resync done\n");
5441 end_migration(dev
, map_state
);
5442 super
->updates_pending
++;
5443 a
->last_checkpoint
= 0;
5445 } else if (!is_resyncing(dev
) && !failed
) {
5446 /* mark the start of the init process if nothing is failed */
5447 dprintf("imsm: mark resync start\n");
5448 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
5449 migrate(dev
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
5451 migrate(dev
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
5452 super
->updates_pending
++;
5456 /* check if we can update curr_migr_unit from resync_start, recovery_start */
5457 blocks_per_unit
= blocks_per_migr_unit(dev
);
5458 if (blocks_per_unit
) {
5462 units
= a
->last_checkpoint
/ blocks_per_unit
;
5465 /* check that we did not overflow 32-bits, and that
5466 * curr_migr_unit needs updating
5468 if (units32
== units
&&
5469 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
5470 dprintf("imsm: mark checkpoint (%u)\n", units32
);
5471 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
5472 super
->updates_pending
++;
5476 /* mark dirty / clean */
5477 if (dev
->vol
.dirty
!= !consistent
) {
5478 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
5483 super
->updates_pending
++;
5489 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
5491 int inst
= a
->info
.container_member
;
5492 struct intel_super
*super
= a
->container
->sb
;
5493 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
5494 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5495 struct imsm_disk
*disk
;
5500 if (n
> map
->num_members
)
5501 fprintf(stderr
, "imsm: set_disk %d out of range 0..%d\n",
5502 n
, map
->num_members
- 1);
5507 dprintf("imsm: set_disk %d:%x\n", n
, state
);
5509 ord
= get_imsm_ord_tbl_ent(dev
, n
, -1);
5510 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
5512 /* check for new failures */
5513 if (state
& DS_FAULTY
) {
5514 if (mark_failure(dev
, disk
, ord_to_idx(ord
)))
5515 super
->updates_pending
++;
5518 /* check if in_sync */
5519 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
5520 struct imsm_map
*migr_map
= get_imsm_map(dev
, 1);
5522 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
5523 super
->updates_pending
++;
5526 failed
= imsm_count_failed(super
, dev
);
5527 map_state
= imsm_check_degraded(super
, dev
, failed
);
5529 /* check if recovery complete, newly degraded, or failed */
5530 if (map_state
== IMSM_T_STATE_NORMAL
&& is_rebuilding(dev
)) {
5531 end_migration(dev
, map_state
);
5532 map
= get_imsm_map(dev
, 0);
5533 map
->failed_disk_num
= ~0;
5534 super
->updates_pending
++;
5535 a
->last_checkpoint
= 0;
5536 } else if (map_state
== IMSM_T_STATE_DEGRADED
&&
5537 map
->map_state
!= map_state
&&
5538 !dev
->vol
.migr_state
) {
5539 dprintf("imsm: mark degraded\n");
5540 map
->map_state
= map_state
;
5541 super
->updates_pending
++;
5542 a
->last_checkpoint
= 0;
5543 } else if (map_state
== IMSM_T_STATE_FAILED
&&
5544 map
->map_state
!= map_state
) {
5545 dprintf("imsm: mark failed\n");
5546 end_migration(dev
, map_state
);
5547 super
->updates_pending
++;
5548 a
->last_checkpoint
= 0;
5549 } else if (is_gen_migration(dev
)) {
5550 dprintf("imsm: Detected General Migration in state: ");
5551 if (map_state
== IMSM_T_STATE_NORMAL
) {
5552 end_migration(dev
, map_state
);
5553 map
= get_imsm_map(dev
, 0);
5554 map
->failed_disk_num
= ~0;
5555 dprintf("normal\n");
5557 if (map_state
== IMSM_T_STATE_DEGRADED
) {
5558 printf("degraded\n");
5559 end_migration(dev
, map_state
);
5561 dprintf("failed\n");
5563 map
->map_state
= map_state
;
5565 super
->updates_pending
++;
5569 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
5572 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
5573 unsigned long long dsize
;
5574 unsigned long long sectors
;
5576 get_dev_size(fd
, NULL
, &dsize
);
5578 if (mpb_size
> 512) {
5579 /* -1 to account for anchor */
5580 sectors
= mpb_sectors(mpb
) - 1;
5582 /* write the extended mpb to the sectors preceeding the anchor */
5583 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0)
5586 if ((unsigned long long)write(fd
, buf
+ 512, 512 * sectors
)
5591 /* first block is stored on second to last sector of the disk */
5592 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0)
5595 if (write(fd
, buf
, 512) != 512)
5601 static void imsm_sync_metadata(struct supertype
*container
)
5603 struct intel_super
*super
= container
->sb
;
5605 dprintf("sync metadata: %d\n", super
->updates_pending
);
5606 if (!super
->updates_pending
)
5609 write_super_imsm(container
, 0);
5611 super
->updates_pending
= 0;
5614 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
5616 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
5617 int i
= get_imsm_disk_idx(dev
, idx
, -1);
5620 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5624 if (dl
&& is_failed(&dl
->disk
))
5628 dprintf("%s: found %x:%x\n", __func__
, dl
->major
, dl
->minor
);
5633 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
5634 struct active_array
*a
, int activate_new
,
5635 struct mdinfo
*additional_test_list
)
5637 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
5638 int idx
= get_imsm_disk_idx(dev
, slot
, -1);
5639 struct imsm_super
*mpb
= super
->anchor
;
5640 struct imsm_map
*map
;
5641 unsigned long long pos
;
5646 __u32 array_start
= 0;
5647 __u32 array_end
= 0;
5649 struct mdinfo
*test_list
;
5651 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5652 /* If in this array, skip */
5653 for (d
= a
->info
.devs
; d
; d
= d
->next
)
5654 if (d
->state_fd
>= 0 &&
5655 d
->disk
.major
== dl
->major
&&
5656 d
->disk
.minor
== dl
->minor
) {
5657 dprintf("%x:%x already in array\n",
5658 dl
->major
, dl
->minor
);
5663 test_list
= additional_test_list
;
5665 if (test_list
->disk
.major
== dl
->major
&&
5666 test_list
->disk
.minor
== dl
->minor
) {
5667 dprintf("%x:%x already in additional test list\n",
5668 dl
->major
, dl
->minor
);
5671 test_list
= test_list
->next
;
5676 /* skip in use or failed drives */
5677 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
5679 dprintf("%x:%x status (failed: %d index: %d)\n",
5680 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
5684 /* skip pure spares when we are looking for partially
5685 * assimilated drives
5687 if (dl
->index
== -1 && !activate_new
)
5690 /* Does this unused device have the requisite free space?
5691 * It needs to be able to cover all member volumes
5693 ex
= get_extents(super
, dl
);
5695 dprintf("cannot get extents\n");
5698 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5699 dev
= get_imsm_dev(super
, i
);
5700 map
= get_imsm_map(dev
, 0);
5702 /* check if this disk is already a member of
5705 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
5711 array_start
= __le32_to_cpu(map
->pba_of_lba0
);
5712 array_end
= array_start
+
5713 __le32_to_cpu(map
->blocks_per_member
) - 1;
5716 /* check that we can start at pba_of_lba0 with
5717 * blocks_per_member of space
5719 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
5723 pos
= ex
[j
].start
+ ex
[j
].size
;
5725 } while (ex
[j
-1].size
);
5732 if (i
< mpb
->num_raid_devs
) {
5733 dprintf("%x:%x does not have %u to %u available\n",
5734 dl
->major
, dl
->minor
, array_start
, array_end
);
5745 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
5747 struct imsm_dev
*dev2
;
5748 struct imsm_map
*map
;
5754 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
5756 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
);
5757 if (state
== IMSM_T_STATE_FAILED
) {
5758 map
= get_imsm_map(dev2
, 0);
5761 for (slot
= 0; slot
< map
->num_members
; slot
++) {
5763 * Check if failed disks are deleted from intel
5764 * disk list or are marked to be deleted
5766 idx
= get_imsm_disk_idx(dev2
, slot
, -1);
5767 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
5769 * Do not rebuild the array if failed disks
5770 * from failed sub-array are not removed from
5774 is_failed(&idisk
->disk
) &&
5775 (idisk
->action
!= DISK_REMOVE
))
5783 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
5784 struct metadata_update
**updates
)
5787 * Find a device with unused free space and use it to replace a
5788 * failed/vacant region in an array. We replace failed regions one a
5789 * array at a time. The result is that a new spare disk will be added
5790 * to the first failed array and after the monitor has finished
5791 * propagating failures the remainder will be consumed.
5793 * FIXME add a capability for mdmon to request spares from another
5797 struct intel_super
*super
= a
->container
->sb
;
5798 int inst
= a
->info
.container_member
;
5799 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
5800 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5801 int failed
= a
->info
.array
.raid_disks
;
5802 struct mdinfo
*rv
= NULL
;
5805 struct metadata_update
*mu
;
5807 struct imsm_update_activate_spare
*u
;
5812 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
5813 if ((d
->curr_state
& DS_FAULTY
) &&
5815 /* wait for Removal to happen */
5817 if (d
->state_fd
>= 0)
5821 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
5822 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
5824 if (dev
->vol
.migr_state
&&
5825 dev
->vol
.migr_type
== MIGR_GEN_MIGR
)
5826 /* No repair during migration */
5829 if (a
->info
.array
.level
== 4)
5830 /* No repair for takeovered array
5831 * imsm doesn't support raid4
5835 if (imsm_check_degraded(super
, dev
, failed
) != IMSM_T_STATE_DEGRADED
)
5839 * If there are any failed disks check state of the other volume.
5840 * Block rebuild if the another one is failed until failed disks
5841 * are removed from container.
5844 dprintf("found failed disks in %s, check if there another"
5845 "failed sub-array.\n",
5847 /* check if states of the other volumes allow for rebuild */
5848 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
5850 allowed
= imsm_rebuild_allowed(a
->container
,
5858 /* For each slot, if it is not working, find a spare */
5859 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
5860 for (d
= a
->info
.devs
; d
; d
= d
->next
)
5861 if (d
->disk
.raid_disk
== i
)
5863 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
5864 if (d
&& (d
->state_fd
>= 0))
5868 * OK, this device needs recovery. Try to re-add the
5869 * previous occupant of this slot, if this fails see if
5870 * we can continue the assimilation of a spare that was
5871 * partially assimilated, finally try to activate a new
5874 dl
= imsm_readd(super
, i
, a
);
5876 dl
= imsm_add_spare(super
, i
, a
, 0, NULL
);
5878 dl
= imsm_add_spare(super
, i
, a
, 1, NULL
);
5882 /* found a usable disk with enough space */
5883 di
= malloc(sizeof(*di
));
5886 memset(di
, 0, sizeof(*di
));
5888 /* dl->index will be -1 in the case we are activating a
5889 * pristine spare. imsm_process_update() will create a
5890 * new index in this case. Once a disk is found to be
5891 * failed in all member arrays it is kicked from the
5894 di
->disk
.number
= dl
->index
;
5896 /* (ab)use di->devs to store a pointer to the device
5899 di
->devs
= (struct mdinfo
*) dl
;
5901 di
->disk
.raid_disk
= i
;
5902 di
->disk
.major
= dl
->major
;
5903 di
->disk
.minor
= dl
->minor
;
5905 di
->recovery_start
= 0;
5906 di
->data_offset
= __le32_to_cpu(map
->pba_of_lba0
);
5907 di
->component_size
= a
->info
.component_size
;
5908 di
->container_member
= inst
;
5909 super
->random
= random32();
5913 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
5914 i
, di
->data_offset
);
5920 /* No spares found */
5922 /* Now 'rv' has a list of devices to return.
5923 * Create a metadata_update record to update the
5924 * disk_ord_tbl for the array
5926 mu
= malloc(sizeof(*mu
));
5928 mu
->buf
= malloc(sizeof(struct imsm_update_activate_spare
) * num_spares
);
5929 if (mu
->buf
== NULL
) {
5936 struct mdinfo
*n
= rv
->next
;
5945 mu
->space_list
= NULL
;
5946 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
5947 mu
->next
= *updates
;
5948 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
5950 for (di
= rv
; di
; di
= di
->next
) {
5951 u
->type
= update_activate_spare
;
5952 u
->dl
= (struct dl
*) di
->devs
;
5954 u
->slot
= di
->disk
.raid_disk
;
5965 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
5967 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
5968 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5969 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, 0);
5970 struct disk_info
*inf
= get_disk_info(u
);
5971 struct imsm_disk
*disk
;
5975 for (i
= 0; i
< map
->num_members
; i
++) {
5976 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, -1));
5977 for (j
= 0; j
< new_map
->num_members
; j
++)
5978 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
5986 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
5988 struct dl
*dl
= NULL
;
5989 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5990 if ((dl
->major
== major
) && (dl
->minor
== minor
))
5995 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
5997 struct dl
*prev
= NULL
;
6001 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6002 if ((dl
->major
== major
) && (dl
->minor
== minor
)) {
6005 prev
->next
= dl
->next
;
6007 super
->disks
= dl
->next
;
6009 __free_imsm_disk(dl
);
6010 dprintf("%s: removed %x:%x\n",
6011 __func__
, major
, minor
);
6019 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
6021 static int add_remove_disk_update(struct intel_super
*super
)
6023 int check_degraded
= 0;
6024 struct dl
*disk
= NULL
;
6025 /* add/remove some spares to/from the metadata/contrainer */
6026 while (super
->disk_mgmt_list
) {
6027 struct dl
*disk_cfg
;
6029 disk_cfg
= super
->disk_mgmt_list
;
6030 super
->disk_mgmt_list
= disk_cfg
->next
;
6031 disk_cfg
->next
= NULL
;
6033 if (disk_cfg
->action
== DISK_ADD
) {
6034 disk_cfg
->next
= super
->disks
;
6035 super
->disks
= disk_cfg
;
6037 dprintf("%s: added %x:%x\n",
6038 __func__
, disk_cfg
->major
,
6040 } else if (disk_cfg
->action
== DISK_REMOVE
) {
6041 dprintf("Disk remove action processed: %x.%x\n",
6042 disk_cfg
->major
, disk_cfg
->minor
);
6043 disk
= get_disk_super(super
,
6047 /* store action status */
6048 disk
->action
= DISK_REMOVE
;
6049 /* remove spare disks only */
6050 if (disk
->index
== -1) {
6051 remove_disk_super(super
,
6056 /* release allocate disk structure */
6057 __free_imsm_disk(disk_cfg
);
6060 return check_degraded
;
6063 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
6064 struct intel_super
*super
,
6067 struct dl
*new_disk
;
6068 struct intel_dev
*id
;
6070 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
6071 int disk_count
= u
->old_raid_disks
;
6072 void **tofree
= NULL
;
6073 int devices_to_reshape
= 1;
6074 struct imsm_super
*mpb
= super
->anchor
;
6076 unsigned int dev_id
;
6078 dprintf("imsm: apply_reshape_container_disks_update()\n");
6080 /* enable spares to use in array */
6081 for (i
= 0; i
< delta_disks
; i
++) {
6082 new_disk
= get_disk_super(super
,
6083 major(u
->new_disks
[i
]),
6084 minor(u
->new_disks
[i
]));
6085 dprintf("imsm: new disk for reshape is: %i:%i "
6086 "(%p, index = %i)\n",
6087 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
6088 new_disk
, new_disk
->index
);
6089 if ((new_disk
== NULL
) ||
6090 ((new_disk
->index
>= 0) &&
6091 (new_disk
->index
< u
->old_raid_disks
)))
6092 goto update_reshape_exit
;
6093 new_disk
->index
= disk_count
++;
6094 /* slot to fill in autolayout
6096 new_disk
->raiddisk
= new_disk
->index
;
6097 new_disk
->disk
.status
|=
6099 new_disk
->disk
.status
&= ~SPARE_DISK
;
6102 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
6103 mpb
->num_raid_devs
);
6104 /* manage changes in volume
6106 for (dev_id
= 0; dev_id
< mpb
->num_raid_devs
; dev_id
++) {
6107 void **sp
= *space_list
;
6108 struct imsm_dev
*newdev
;
6109 struct imsm_map
*newmap
, *oldmap
;
6111 for (id
= super
->devlist
; id
; id
= id
->next
) {
6112 if (id
->index
== dev_id
)
6121 /* Copy the dev, but not (all of) the map */
6122 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
6123 oldmap
= get_imsm_map(id
->dev
, 0);
6124 newmap
= get_imsm_map(newdev
, 0);
6125 /* Copy the current map */
6126 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
6127 /* update one device only
6129 if (devices_to_reshape
) {
6130 dprintf("imsm: modifying subdev: %i\n",
6132 devices_to_reshape
--;
6133 newdev
->vol
.migr_state
= 1;
6134 newdev
->vol
.curr_migr_unit
= 0;
6135 newdev
->vol
.migr_type
= MIGR_GEN_MIGR
;
6136 newmap
->num_members
= u
->new_raid_disks
;
6137 for (i
= 0; i
< delta_disks
; i
++) {
6138 set_imsm_ord_tbl_ent(newmap
,
6139 u
->old_raid_disks
+ i
,
6140 u
->old_raid_disks
+ i
);
6142 /* New map is correct, now need to save old map
6144 newmap
= get_imsm_map(newdev
, 1);
6145 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
6147 imsm_set_array_size(newdev
);
6150 sp
= (void **)id
->dev
;
6156 *space_list
= tofree
;
6159 update_reshape_exit
:
6164 static int apply_takeover_update(struct imsm_update_takeover
*u
,
6165 struct intel_super
*super
,
6168 struct imsm_dev
*dev
= NULL
;
6169 struct intel_dev
*dv
;
6170 struct imsm_dev
*dev_new
;
6171 struct imsm_map
*map
;
6175 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
6176 if (dv
->index
== (unsigned int)u
->subarray
) {
6184 map
= get_imsm_map(dev
, 0);
6186 if (u
->direction
== R10_TO_R0
) {
6187 /* Number of failed disks must be half of initial disk number */
6188 if (imsm_count_failed(super
, dev
) != (map
->num_members
/ 2))
6191 /* iterate through devices to mark removed disks as spare */
6192 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
6193 if (dm
->disk
.status
& FAILED_DISK
) {
6194 int idx
= dm
->index
;
6195 /* update indexes on the disk list */
6196 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
6197 the index values will end up being correct.... NB */
6198 for (du
= super
->disks
; du
; du
= du
->next
)
6199 if (du
->index
> idx
)
6201 /* mark as spare disk */
6202 dm
->disk
.status
= SPARE_DISK
;
6207 map
->num_members
= map
->num_members
/ 2;
6208 map
->map_state
= IMSM_T_STATE_NORMAL
;
6209 map
->num_domains
= 1;
6210 map
->raid_level
= 0;
6211 map
->failed_disk_num
= -1;
6214 if (u
->direction
== R0_TO_R10
) {
6216 /* update slots in current disk list */
6217 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
6221 /* create new *missing* disks */
6222 for (i
= 0; i
< map
->num_members
; i
++) {
6223 space
= *space_list
;
6226 *space_list
= *space
;
6228 memcpy(du
, super
->disks
, sizeof(*du
));
6232 du
->index
= (i
* 2) + 1;
6233 sprintf((char *)du
->disk
.serial
,
6234 " MISSING_%d", du
->index
);
6235 sprintf((char *)du
->serial
,
6236 "MISSING_%d", du
->index
);
6237 du
->next
= super
->missing
;
6238 super
->missing
= du
;
6240 /* create new dev and map */
6241 space
= *space_list
;
6244 *space_list
= *space
;
6245 dev_new
= (void *)space
;
6246 memcpy(dev_new
, dev
, sizeof(*dev
));
6247 /* update new map */
6248 map
= get_imsm_map(dev_new
, 0);
6249 map
->num_members
= map
->num_members
* 2;
6250 map
->map_state
= IMSM_T_STATE_DEGRADED
;
6251 map
->num_domains
= 2;
6252 map
->raid_level
= 1;
6253 /* replace dev<->dev_new */
6256 /* update disk order table */
6257 for (du
= super
->disks
; du
; du
= du
->next
)
6259 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
6260 for (du
= super
->missing
; du
; du
= du
->next
)
6261 if (du
->index
>= 0) {
6262 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
6263 mark_missing(dev_new
, &du
->disk
, du
->index
);
6269 static void imsm_process_update(struct supertype
*st
,
6270 struct metadata_update
*update
)
6273 * crack open the metadata_update envelope to find the update record
6274 * update can be one of:
6275 * update_reshape_container_disks - all the arrays in the container
6276 * are being reshaped to have more devices. We need to mark
6277 * the arrays for general migration and convert selected spares
6278 * into active devices.
6279 * update_activate_spare - a spare device has replaced a failed
6280 * device in an array, update the disk_ord_tbl. If this disk is
6281 * present in all member arrays then also clear the SPARE_DISK
6283 * update_create_array
6285 * update_rename_array
6286 * update_add_remove_disk
6288 struct intel_super
*super
= st
->sb
;
6289 struct imsm_super
*mpb
;
6290 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
6292 /* update requires a larger buf but the allocation failed */
6293 if (super
->next_len
&& !super
->next_buf
) {
6294 super
->next_len
= 0;
6298 if (super
->next_buf
) {
6299 memcpy(super
->next_buf
, super
->buf
, super
->len
);
6301 super
->len
= super
->next_len
;
6302 super
->buf
= super
->next_buf
;
6304 super
->next_len
= 0;
6305 super
->next_buf
= NULL
;
6308 mpb
= super
->anchor
;
6311 case update_takeover
: {
6312 struct imsm_update_takeover
*u
= (void *)update
->buf
;
6313 if (apply_takeover_update(u
, super
, &update
->space_list
)) {
6314 imsm_update_version_info(super
);
6315 super
->updates_pending
++;
6320 case update_reshape_container_disks
: {
6321 struct imsm_update_reshape
*u
= (void *)update
->buf
;
6322 if (apply_reshape_container_disks_update(
6323 u
, super
, &update
->space_list
))
6324 super
->updates_pending
++;
6327 case update_activate_spare
: {
6328 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
6329 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
6330 struct imsm_map
*map
= get_imsm_map(dev
, 0);
6331 struct imsm_map
*migr_map
;
6332 struct active_array
*a
;
6333 struct imsm_disk
*disk
;
6338 int victim
= get_imsm_disk_idx(dev
, u
->slot
, -1);
6341 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6346 fprintf(stderr
, "error: imsm_activate_spare passed "
6347 "an unknown disk (index: %d)\n",
6352 super
->updates_pending
++;
6354 /* count failures (excluding rebuilds and the victim)
6355 * to determine map[0] state
6358 for (i
= 0; i
< map
->num_members
; i
++) {
6361 disk
= get_imsm_disk(super
,
6362 get_imsm_disk_idx(dev
, i
, -1));
6363 if (!disk
|| is_failed(disk
))
6367 /* adding a pristine spare, assign a new index */
6368 if (dl
->index
< 0) {
6369 dl
->index
= super
->anchor
->num_disks
;
6370 super
->anchor
->num_disks
++;
6373 disk
->status
|= CONFIGURED_DISK
;
6374 disk
->status
&= ~SPARE_DISK
;
6377 to_state
= imsm_check_degraded(super
, dev
, failed
);
6378 map
->map_state
= IMSM_T_STATE_DEGRADED
;
6379 migrate(dev
, to_state
, MIGR_REBUILD
);
6380 migr_map
= get_imsm_map(dev
, 1);
6381 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
6382 set_imsm_ord_tbl_ent(migr_map
, u
->slot
, dl
->index
| IMSM_ORD_REBUILD
);
6384 /* update the family_num to mark a new container
6385 * generation, being careful to record the existing
6386 * family_num in orig_family_num to clean up after
6387 * earlier mdadm versions that neglected to set it.
6389 if (mpb
->orig_family_num
== 0)
6390 mpb
->orig_family_num
= mpb
->family_num
;
6391 mpb
->family_num
+= super
->random
;
6393 /* count arrays using the victim in the metadata */
6395 for (a
= st
->arrays
; a
; a
= a
->next
) {
6396 dev
= get_imsm_dev(super
, a
->info
.container_member
);
6397 map
= get_imsm_map(dev
, 0);
6399 if (get_imsm_disk_slot(map
, victim
) >= 0)
6403 /* delete the victim if it is no longer being
6409 /* We know that 'manager' isn't touching anything,
6410 * so it is safe to delete
6412 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
6413 if ((*dlp
)->index
== victim
)
6416 /* victim may be on the missing list */
6418 for (dlp
= &super
->missing
; *dlp
; dlp
= &(*dlp
)->next
)
6419 if ((*dlp
)->index
== victim
)
6421 imsm_delete(super
, dlp
, victim
);
6425 case update_create_array
: {
6426 /* someone wants to create a new array, we need to be aware of
6427 * a few races/collisions:
6428 * 1/ 'Create' called by two separate instances of mdadm
6429 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
6430 * devices that have since been assimilated via
6432 * In the event this update can not be carried out mdadm will
6433 * (FIX ME) notice that its update did not take hold.
6435 struct imsm_update_create_array
*u
= (void *) update
->buf
;
6436 struct intel_dev
*dv
;
6437 struct imsm_dev
*dev
;
6438 struct imsm_map
*map
, *new_map
;
6439 unsigned long long start
, end
;
6440 unsigned long long new_start
, new_end
;
6442 struct disk_info
*inf
;
6445 /* handle racing creates: first come first serve */
6446 if (u
->dev_idx
< mpb
->num_raid_devs
) {
6447 dprintf("%s: subarray %d already defined\n",
6448 __func__
, u
->dev_idx
);
6452 /* check update is next in sequence */
6453 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
6454 dprintf("%s: can not create array %d expected index %d\n",
6455 __func__
, u
->dev_idx
, mpb
->num_raid_devs
);
6459 new_map
= get_imsm_map(&u
->dev
, 0);
6460 new_start
= __le32_to_cpu(new_map
->pba_of_lba0
);
6461 new_end
= new_start
+ __le32_to_cpu(new_map
->blocks_per_member
);
6462 inf
= get_disk_info(u
);
6464 /* handle activate_spare versus create race:
6465 * check to make sure that overlapping arrays do not include
6468 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6469 dev
= get_imsm_dev(super
, i
);
6470 map
= get_imsm_map(dev
, 0);
6471 start
= __le32_to_cpu(map
->pba_of_lba0
);
6472 end
= start
+ __le32_to_cpu(map
->blocks_per_member
);
6473 if ((new_start
>= start
&& new_start
<= end
) ||
6474 (start
>= new_start
&& start
<= new_end
))
6479 if (disks_overlap(super
, i
, u
)) {
6480 dprintf("%s: arrays overlap\n", __func__
);
6485 /* check that prepare update was successful */
6486 if (!update
->space
) {
6487 dprintf("%s: prepare update failed\n", __func__
);
6491 /* check that all disks are still active before committing
6492 * changes. FIXME: could we instead handle this by creating a
6493 * degraded array? That's probably not what the user expects,
6494 * so better to drop this update on the floor.
6496 for (i
= 0; i
< new_map
->num_members
; i
++) {
6497 dl
= serial_to_dl(inf
[i
].serial
, super
);
6499 dprintf("%s: disk disappeared\n", __func__
);
6504 super
->updates_pending
++;
6506 /* convert spares to members and fixup ord_tbl */
6507 for (i
= 0; i
< new_map
->num_members
; i
++) {
6508 dl
= serial_to_dl(inf
[i
].serial
, super
);
6509 if (dl
->index
== -1) {
6510 dl
->index
= mpb
->num_disks
;
6512 dl
->disk
.status
|= CONFIGURED_DISK
;
6513 dl
->disk
.status
&= ~SPARE_DISK
;
6515 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
6520 update
->space
= NULL
;
6521 imsm_copy_dev(dev
, &u
->dev
);
6522 dv
->index
= u
->dev_idx
;
6523 dv
->next
= super
->devlist
;
6524 super
->devlist
= dv
;
6525 mpb
->num_raid_devs
++;
6527 imsm_update_version_info(super
);
6530 /* mdmon knows how to release update->space, but not
6531 * ((struct intel_dev *) update->space)->dev
6533 if (update
->space
) {
6539 case update_kill_array
: {
6540 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
6541 int victim
= u
->dev_idx
;
6542 struct active_array
*a
;
6543 struct intel_dev
**dp
;
6544 struct imsm_dev
*dev
;
6546 /* sanity check that we are not affecting the uuid of
6547 * active arrays, or deleting an active array
6549 * FIXME when immutable ids are available, but note that
6550 * we'll also need to fixup the invalidated/active
6551 * subarray indexes in mdstat
6553 for (a
= st
->arrays
; a
; a
= a
->next
)
6554 if (a
->info
.container_member
>= victim
)
6556 /* by definition if mdmon is running at least one array
6557 * is active in the container, so checking
6558 * mpb->num_raid_devs is just extra paranoia
6560 dev
= get_imsm_dev(super
, victim
);
6561 if (a
|| !dev
|| mpb
->num_raid_devs
== 1) {
6562 dprintf("failed to delete subarray-%d\n", victim
);
6566 for (dp
= &super
->devlist
; *dp
;)
6567 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
6570 if ((*dp
)->index
> (unsigned)victim
)
6574 mpb
->num_raid_devs
--;
6575 super
->updates_pending
++;
6578 case update_rename_array
: {
6579 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
6580 char name
[MAX_RAID_SERIAL_LEN
+1];
6581 int target
= u
->dev_idx
;
6582 struct active_array
*a
;
6583 struct imsm_dev
*dev
;
6585 /* sanity check that we are not affecting the uuid of
6588 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
6589 name
[MAX_RAID_SERIAL_LEN
] = '\0';
6590 for (a
= st
->arrays
; a
; a
= a
->next
)
6591 if (a
->info
.container_member
== target
)
6593 dev
= get_imsm_dev(super
, u
->dev_idx
);
6594 if (a
|| !dev
|| !check_name(super
, name
, 1)) {
6595 dprintf("failed to rename subarray-%d\n", target
);
6599 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6600 super
->updates_pending
++;
6603 case update_add_remove_disk
: {
6604 /* we may be able to repair some arrays if disks are
6605 * being added, check teh status of add_remove_disk
6606 * if discs has been added.
6608 if (add_remove_disk_update(super
)) {
6609 struct active_array
*a
;
6611 super
->updates_pending
++;
6612 for (a
= st
->arrays
; a
; a
= a
->next
)
6613 a
->check_degraded
= 1;
6618 fprintf(stderr
, "error: unsuported process update type:"
6619 "(type: %d)\n", type
);
6623 static void imsm_prepare_update(struct supertype
*st
,
6624 struct metadata_update
*update
)
6627 * Allocate space to hold new disk entries, raid-device entries or a new
6628 * mpb if necessary. The manager synchronously waits for updates to
6629 * complete in the monitor, so new mpb buffers allocated here can be
6630 * integrated by the monitor thread without worrying about live pointers
6631 * in the manager thread.
6633 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
6634 struct intel_super
*super
= st
->sb
;
6635 struct imsm_super
*mpb
= super
->anchor
;
6640 case update_takeover
: {
6641 struct imsm_update_takeover
*u
= (void *)update
->buf
;
6642 if (u
->direction
== R0_TO_R10
) {
6643 void **tail
= (void **)&update
->space_list
;
6644 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subarray
);
6645 struct imsm_map
*map
= get_imsm_map(dev
, 0);
6646 int num_members
= map
->num_members
;
6650 /* allocate memory for added disks */
6651 for (i
= 0; i
< num_members
; i
++) {
6652 size
= sizeof(struct dl
);
6653 space
= malloc(size
);
6662 /* allocate memory for new device */
6663 size
= sizeof_imsm_dev(super
->devlist
->dev
, 0) +
6664 (num_members
* sizeof(__u32
));
6665 space
= malloc(size
);
6674 len
= disks_to_mpb_size(num_members
* 2);
6676 /* if allocation didn't success, free buffer */
6677 while (update
->space_list
) {
6678 void **sp
= update
->space_list
;
6679 update
->space_list
= *sp
;
6687 case update_reshape_container_disks
: {
6688 /* Every raid device in the container is about to
6689 * gain some more devices, and we will enter a
6691 * So each 'imsm_map' will be bigger, and the imsm_vol
6692 * will now hold 2 of them.
6693 * Thus we need new 'struct imsm_dev' allocations sized
6694 * as sizeof_imsm_dev but with more devices in both maps.
6696 struct imsm_update_reshape
*u
= (void *)update
->buf
;
6697 struct intel_dev
*dl
;
6698 void **space_tail
= (void**)&update
->space_list
;
6700 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
6702 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
6703 int size
= sizeof_imsm_dev(dl
->dev
, 1);
6705 if (u
->new_raid_disks
> u
->old_raid_disks
)
6706 size
+= sizeof(__u32
)*2*
6707 (u
->new_raid_disks
- u
->old_raid_disks
);
6716 len
= disks_to_mpb_size(u
->new_raid_disks
);
6717 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
6720 case update_create_array
: {
6721 struct imsm_update_create_array
*u
= (void *) update
->buf
;
6722 struct intel_dev
*dv
;
6723 struct imsm_dev
*dev
= &u
->dev
;
6724 struct imsm_map
*map
= get_imsm_map(dev
, 0);
6726 struct disk_info
*inf
;
6730 inf
= get_disk_info(u
);
6731 len
= sizeof_imsm_dev(dev
, 1);
6732 /* allocate a new super->devlist entry */
6733 dv
= malloc(sizeof(*dv
));
6735 dv
->dev
= malloc(len
);
6740 update
->space
= NULL
;
6744 /* count how many spares will be converted to members */
6745 for (i
= 0; i
< map
->num_members
; i
++) {
6746 dl
= serial_to_dl(inf
[i
].serial
, super
);
6748 /* hmm maybe it failed?, nothing we can do about
6753 if (count_memberships(dl
, super
) == 0)
6756 len
+= activate
* sizeof(struct imsm_disk
);
6763 /* check if we need a larger metadata buffer */
6764 if (super
->next_buf
)
6765 buf_len
= super
->next_len
;
6767 buf_len
= super
->len
;
6769 if (__le32_to_cpu(mpb
->mpb_size
) + len
> buf_len
) {
6770 /* ok we need a larger buf than what is currently allocated
6771 * if this allocation fails process_update will notice that
6772 * ->next_len is set and ->next_buf is NULL
6774 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + len
, 512);
6775 if (super
->next_buf
)
6776 free(super
->next_buf
);
6778 super
->next_len
= buf_len
;
6779 if (posix_memalign(&super
->next_buf
, 512, buf_len
) == 0)
6780 memset(super
->next_buf
, 0, buf_len
);
6782 super
->next_buf
= NULL
;
6786 /* must be called while manager is quiesced */
6787 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
6789 struct imsm_super
*mpb
= super
->anchor
;
6791 struct imsm_dev
*dev
;
6792 struct imsm_map
*map
;
6793 int i
, j
, num_members
;
6796 dprintf("%s: deleting device[%d] from imsm_super\n",
6799 /* shift all indexes down one */
6800 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
6801 if (iter
->index
> (int)index
)
6803 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
6804 if (iter
->index
> (int)index
)
6807 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6808 dev
= get_imsm_dev(super
, i
);
6809 map
= get_imsm_map(dev
, 0);
6810 num_members
= map
->num_members
;
6811 for (j
= 0; j
< num_members
; j
++) {
6812 /* update ord entries being careful not to propagate
6813 * ord-flags to the first map
6815 ord
= get_imsm_ord_tbl_ent(dev
, j
, -1);
6817 if (ord_to_idx(ord
) <= index
)
6820 map
= get_imsm_map(dev
, 0);
6821 set_imsm_ord_tbl_ent(map
, j
, ord_to_idx(ord
- 1));
6822 map
= get_imsm_map(dev
, 1);
6824 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
6829 super
->updates_pending
++;
6831 struct dl
*dl
= *dlp
;
6833 *dlp
= (*dlp
)->next
;
6834 __free_imsm_disk(dl
);
6838 static char disk_by_path
[] = "/dev/disk/by-path/";
6840 static const char *imsm_get_disk_controller_domain(const char *path
)
6842 char disk_path
[PATH_MAX
];
6846 strncpy(disk_path
, disk_by_path
, PATH_MAX
- 1);
6847 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
6848 if (stat(disk_path
, &st
) == 0) {
6849 struct sys_dev
* hba
;
6852 path
= devt_to_devpath(st
.st_rdev
);
6855 hba
= find_disk_attached_hba(-1, path
);
6856 if (hba
&& hba
->type
== SYS_DEV_SAS
)
6858 else if (hba
&& hba
->type
== SYS_DEV_SATA
)
6862 dprintf("path: %s hba: %s attached: %s\n",
6863 path
, (hba
) ? hba
->path
: "NULL", drv
);
6871 static int imsm_find_array_minor_by_subdev(int subdev
, int container
, int *minor
)
6873 char subdev_name
[20];
6874 struct mdstat_ent
*mdstat
;
6876 sprintf(subdev_name
, "%d", subdev
);
6877 mdstat
= mdstat_by_subdev(subdev_name
, container
);
6881 *minor
= mdstat
->devnum
;
6882 free_mdstat(mdstat
);
6886 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
6887 struct geo_params
*geo
,
6888 int *old_raid_disks
)
6890 /* currently we only support increasing the number of devices
6891 * for a container. This increases the number of device for each
6892 * member array. They must all be RAID0 or RAID5.
6895 struct mdinfo
*info
, *member
;
6896 int devices_that_can_grow
= 0;
6898 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): "
6899 "st->devnum = (%i)\n",
6902 if (geo
->size
!= -1 ||
6903 geo
->level
!= UnSet
||
6904 geo
->layout
!= UnSet
||
6905 geo
->chunksize
!= 0 ||
6906 geo
->raid_disks
== UnSet
) {
6907 dprintf("imsm: Container operation is allowed for "
6908 "raid disks number change only.\n");
6912 info
= container_content_imsm(st
, NULL
);
6913 for (member
= info
; member
; member
= member
->next
) {
6917 dprintf("imsm: checking device_num: %i\n",
6918 member
->container_member
);
6920 if (geo
->raid_disks
<= member
->array
.raid_disks
) {
6921 /* we work on container for Online Capacity Expansion
6922 * only so raid_disks has to grow
6924 dprintf("imsm: for container operation raid disks "
6925 "increase is required\n");
6929 if ((info
->array
.level
!= 0) &&
6930 (info
->array
.level
!= 5)) {
6931 /* we cannot use this container with other raid level
6933 dprintf("imsm: for container operation wrong"
6934 " raid level (%i) detected\n",
6938 /* check for platform support
6939 * for this raid level configuration
6941 struct intel_super
*super
= st
->sb
;
6942 if (!is_raid_level_supported(super
->orom
,
6943 member
->array
.level
,
6945 dprintf("platform does not support raid%d with"
6949 geo
->raid_disks
> 1 ? "s" : "");
6954 if (*old_raid_disks
&&
6955 info
->array
.raid_disks
!= *old_raid_disks
)
6957 *old_raid_disks
= info
->array
.raid_disks
;
6959 /* All raid5 and raid0 volumes in container
6960 * have to be ready for Online Capacity Expansion
6961 * so they need to be assembled. We have already
6962 * checked that no recovery etc is happening.
6964 result
= imsm_find_array_minor_by_subdev(member
->container_member
,
6968 dprintf("imsm: cannot find array\n");
6971 devices_that_can_grow
++;
6974 if (!member
&& devices_that_can_grow
)
6978 dprintf("\tContainer operation allowed\n");
6980 dprintf("\tError: %i\n", ret_val
);
6985 /* Function: get_spares_for_grow
6986 * Description: Allocates memory and creates list of spare devices
6987 * avaliable in container. Checks if spare drive size is acceptable.
6988 * Parameters: Pointer to the supertype structure
6989 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
6992 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
6994 unsigned long long min_size
= min_acceptable_spare_size_imsm(st
);
6995 return container_choose_spares(st
, min_size
, NULL
, NULL
, NULL
, 0);
6998 /******************************************************************************
6999 * function: imsm_create_metadata_update_for_reshape
7000 * Function creates update for whole IMSM container.
7002 ******************************************************************************/
7003 static int imsm_create_metadata_update_for_reshape(
7004 struct supertype
*st
,
7005 struct geo_params
*geo
,
7007 struct imsm_update_reshape
**updatep
)
7009 struct intel_super
*super
= st
->sb
;
7010 struct imsm_super
*mpb
= super
->anchor
;
7011 int update_memory_size
= 0;
7012 struct imsm_update_reshape
*u
= NULL
;
7013 struct mdinfo
*spares
= NULL
;
7015 int delta_disks
= 0;
7018 dprintf("imsm_update_metadata_for_reshape(enter) raid_disks = %i\n",
7021 delta_disks
= geo
->raid_disks
- old_raid_disks
;
7023 /* size of all update data without anchor */
7024 update_memory_size
= sizeof(struct imsm_update_reshape
);
7026 /* now add space for spare disks that we need to add. */
7027 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
7029 u
= calloc(1, update_memory_size
);
7032 "cannot get memory for imsm_update_reshape update\n");
7035 u
->type
= update_reshape_container_disks
;
7036 u
->old_raid_disks
= old_raid_disks
;
7037 u
->new_raid_disks
= geo
->raid_disks
;
7039 /* now get spare disks list
7041 spares
= get_spares_for_grow(st
);
7044 || delta_disks
> spares
->array
.spare_disks
) {
7045 fprintf(stderr
, Name
": imsm: ERROR: Cannot get spare devices "
7046 "for %s.\n", geo
->dev_name
);
7050 /* we have got spares
7051 * update disk list in imsm_disk list table in anchor
7053 dprintf("imsm: %i spares are available.\n\n",
7054 spares
->array
.spare_disks
);
7057 for (i
= 0; i
< delta_disks
; i
++) {
7062 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
7064 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
7065 dl
->index
= mpb
->num_disks
;
7075 dprintf("imsm: reshape update preparation :");
7076 if (i
== delta_disks
) {
7079 return update_memory_size
;
7082 dprintf(" Error\n");
7087 static void imsm_update_metadata_locally(struct supertype
*st
,
7090 struct metadata_update mu
;
7095 mu
.space_list
= NULL
;
7097 imsm_prepare_update(st
, &mu
);
7098 imsm_process_update(st
, &mu
);
7100 while (mu
.space_list
) {
7101 void **space
= mu
.space_list
;
7102 mu
.space_list
= *space
;
7107 /***************************************************************************
7108 * Function: imsm_analyze_change
7109 * Description: Function analyze change for single volume
7110 * and validate if transition is supported
7111 * Parameters: Geometry parameters, supertype structure
7112 * Returns: Operation type code on success, -1 if fail
7113 ****************************************************************************/
7114 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
7115 struct geo_params
*geo
)
7122 getinfo_super_imsm_volume(st
, &info
, NULL
);
7124 if ((geo
->level
!= info
.array
.level
) &&
7125 (geo
->level
>= 0) &&
7126 (geo
->level
!= UnSet
)) {
7127 switch (info
.array
.level
) {
7129 if (geo
->level
== 5) {
7130 change
= CH_MIGRATION
;
7133 if (geo
->level
== 10) {
7134 change
= CH_TAKEOVER
;
7139 if (geo
->level
== 0) {
7140 change
= CH_TAKEOVER
;
7145 if (geo
->level
== 0)
7146 change
= CH_MIGRATION
;
7149 if (geo
->level
== 0) {
7150 change
= CH_TAKEOVER
;
7157 Name
" Error. Level Migration from %d to %d "
7159 info
.array
.level
, geo
->level
);
7160 goto analyse_change_exit
;
7163 geo
->level
= info
.array
.level
;
7165 if ((geo
->layout
!= info
.array
.layout
)
7166 && ((geo
->layout
!= UnSet
) && (geo
->layout
!= -1))) {
7167 change
= CH_MIGRATION
;
7168 if ((info
.array
.layout
== 0)
7169 && (info
.array
.level
== 5)
7170 && (geo
->layout
== 5)) {
7171 /* reshape 5 -> 4 */
7172 } else if ((info
.array
.layout
== 5)
7173 && (info
.array
.level
== 5)
7174 && (geo
->layout
== 0)) {
7175 /* reshape 4 -> 5 */
7180 Name
" Error. Layout Migration from %d to %d "
7182 info
.array
.layout
, geo
->layout
);
7184 goto analyse_change_exit
;
7187 geo
->layout
= info
.array
.layout
;
7189 if ((geo
->chunksize
> 0) && (geo
->chunksize
!= UnSet
)
7190 && (geo
->chunksize
!= info
.array
.chunk_size
))
7191 change
= CH_MIGRATION
;
7193 geo
->chunksize
= info
.array
.chunk_size
;
7195 chunk
= geo
->chunksize
/ 1024;
7196 if (!validate_geometry_imsm(st
,
7206 struct intel_super
*super
= st
->sb
;
7207 struct imsm_super
*mpb
= super
->anchor
;
7209 if (mpb
->num_raid_devs
> 1) {
7211 Name
" Error. Cannot perform operation on %s"
7212 "- for this operation it MUST be single "
7213 "array in container\n",
7219 analyse_change_exit
:
7224 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
7226 struct intel_super
*super
= st
->sb
;
7227 struct imsm_update_takeover
*u
;
7229 u
= malloc(sizeof(struct imsm_update_takeover
));
7233 u
->type
= update_takeover
;
7234 u
->subarray
= super
->current_vol
;
7236 /* 10->0 transition */
7237 if (geo
->level
== 0)
7238 u
->direction
= R10_TO_R0
;
7240 /* 0->10 transition */
7241 if (geo
->level
== 10)
7242 u
->direction
= R0_TO_R10
;
7244 /* update metadata locally */
7245 imsm_update_metadata_locally(st
, u
,
7246 sizeof(struct imsm_update_takeover
));
7247 /* and possibly remotely */
7248 if (st
->update_tail
)
7249 append_metadata_update(st
, u
,
7250 sizeof(struct imsm_update_takeover
));
7257 static int imsm_reshape_super(struct supertype
*st
, long long size
, int level
,
7258 int layout
, int chunksize
, int raid_disks
,
7259 int delta_disks
, char *backup
, char *dev
,
7263 struct geo_params geo
;
7265 dprintf("imsm: reshape_super called.\n");
7267 memset(&geo
, 0, sizeof(struct geo_params
));
7270 geo
.dev_id
= st
->devnum
;
7273 geo
.layout
= layout
;
7274 geo
.chunksize
= chunksize
;
7275 geo
.raid_disks
= raid_disks
;
7276 if (delta_disks
!= UnSet
)
7277 geo
.raid_disks
+= delta_disks
;
7279 dprintf("\tfor level : %i\n", geo
.level
);
7280 dprintf("\tfor raid_disks : %i\n", geo
.raid_disks
);
7282 if (experimental() == 0)
7285 if (st
->container_dev
== st
->devnum
) {
7286 /* On container level we can only increase number of devices. */
7287 dprintf("imsm: info: Container operation\n");
7288 int old_raid_disks
= 0;
7289 if (imsm_reshape_is_allowed_on_container(
7290 st
, &geo
, &old_raid_disks
)) {
7291 struct imsm_update_reshape
*u
= NULL
;
7294 len
= imsm_create_metadata_update_for_reshape(
7295 st
, &geo
, old_raid_disks
, &u
);
7298 dprintf("imsm: Cannot prepare update\n");
7299 goto exit_imsm_reshape_super
;
7303 /* update metadata locally */
7304 imsm_update_metadata_locally(st
, u
, len
);
7305 /* and possibly remotely */
7306 if (st
->update_tail
)
7307 append_metadata_update(st
, u
, len
);
7312 fprintf(stderr
, Name
": (imsm) Operation "
7313 "is not allowed on this container\n");
7316 /* On volume level we support following operations
7317 * - takeover: raid10 -> raid0; raid0 -> raid10
7318 * - chunk size migration
7319 * - migration: raid5 -> raid0; raid0 -> raid5
7321 struct intel_super
*super
= st
->sb
;
7322 struct intel_dev
*dev
= super
->devlist
;
7324 dprintf("imsm: info: Volume operation\n");
7325 /* find requested device */
7327 imsm_find_array_minor_by_subdev(dev
->index
, st
->container_dev
, &devnum
);
7328 if (devnum
== geo
.dev_id
)
7333 fprintf(stderr
, Name
" Cannot find %s (%i) subarray\n",
7334 geo
.dev_name
, geo
.dev_id
);
7335 goto exit_imsm_reshape_super
;
7337 super
->current_vol
= dev
->index
;
7338 change
= imsm_analyze_change(st
, &geo
);
7341 ret_val
= imsm_takeover(st
, &geo
);
7351 exit_imsm_reshape_super
:
7352 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
7356 static int imsm_manage_reshape(
7357 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
7358 struct supertype
*st
, unsigned long stripes
,
7359 int *fds
, unsigned long long *offsets
,
7360 int dests
, int *destfd
, unsigned long long *destoffsets
)
7362 /* Just use child_monitor for now */
7363 return child_monitor(
7364 afd
, sra
, reshape
, st
, stripes
,
7365 fds
, offsets
, dests
, destfd
, destoffsets
);
7367 #endif /* MDASSEMBLE */
7369 struct superswitch super_imsm
= {
7371 .examine_super
= examine_super_imsm
,
7372 .brief_examine_super
= brief_examine_super_imsm
,
7373 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
7374 .export_examine_super
= export_examine_super_imsm
,
7375 .detail_super
= detail_super_imsm
,
7376 .brief_detail_super
= brief_detail_super_imsm
,
7377 .write_init_super
= write_init_super_imsm
,
7378 .validate_geometry
= validate_geometry_imsm
,
7379 .add_to_super
= add_to_super_imsm
,
7380 .remove_from_super
= remove_from_super_imsm
,
7381 .detail_platform
= detail_platform_imsm
,
7382 .kill_subarray
= kill_subarray_imsm
,
7383 .update_subarray
= update_subarray_imsm
,
7384 .load_container
= load_container_imsm
,
7385 .default_geometry
= default_geometry_imsm
,
7386 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
7387 .reshape_super
= imsm_reshape_super
,
7388 .manage_reshape
= imsm_manage_reshape
,
7390 .match_home
= match_home_imsm
,
7391 .uuid_from_super
= uuid_from_super_imsm
,
7392 .getinfo_super
= getinfo_super_imsm
,
7393 .getinfo_super_disks
= getinfo_super_disks_imsm
,
7394 .update_super
= update_super_imsm
,
7396 .avail_size
= avail_size_imsm
,
7397 .min_acceptable_spare_size
= min_acceptable_spare_size_imsm
,
7399 .compare_super
= compare_super_imsm
,
7401 .load_super
= load_super_imsm
,
7402 .init_super
= init_super_imsm
,
7403 .store_super
= store_super_imsm
,
7404 .free_super
= free_super_imsm
,
7405 .match_metadata_desc
= match_metadata_desc_imsm
,
7406 .container_content
= container_content_imsm
,
7413 .open_new
= imsm_open_new
,
7414 .set_array_state
= imsm_set_array_state
,
7415 .set_disk
= imsm_set_disk
,
7416 .sync_metadata
= imsm_sync_metadata
,
7417 .activate_spare
= imsm_activate_spare
,
7418 .process_update
= imsm_process_update
,
7419 .prepare_update
= imsm_prepare_update
,
7420 #endif /* MDASSEMBLE */