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
{
301 /* definition of messages passed to imsm_process_update */
302 enum imsm_update_type
{
303 update_activate_spare
,
307 update_add_remove_disk
,
308 update_reshape_container_disks
,
312 struct imsm_update_activate_spare
{
313 enum imsm_update_type type
;
317 struct imsm_update_activate_spare
*next
;
330 enum takeover_direction
{
334 struct imsm_update_takeover
{
335 enum imsm_update_type type
;
337 enum takeover_direction direction
;
340 struct imsm_update_reshape
{
341 enum imsm_update_type type
;
344 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
348 __u8 serial
[MAX_RAID_SERIAL_LEN
];
351 struct imsm_update_create_array
{
352 enum imsm_update_type type
;
357 struct imsm_update_kill_array
{
358 enum imsm_update_type type
;
362 struct imsm_update_rename_array
{
363 enum imsm_update_type type
;
364 __u8 name
[MAX_RAID_SERIAL_LEN
];
368 struct imsm_update_add_remove_disk
{
369 enum imsm_update_type type
;
373 static const char *_sys_dev_type
[] = {
374 [SYS_DEV_UNKNOWN
] = "Unknown",
375 [SYS_DEV_SAS
] = "SAS",
376 [SYS_DEV_SATA
] = "SATA"
379 const char *get_sys_dev_type(enum sys_dev_type type
)
381 if (type
>= SYS_DEV_MAX
)
382 type
= SYS_DEV_UNKNOWN
;
384 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)
412 static int attach_hba_to_super(struct intel_super
*super
, struct sys_dev
*device
,
415 struct intel_hba
*hba
;
417 /* check if disk attached to Intel HBA */
418 hba
= find_intel_hba(super
->hba
, device
);
421 /* Check if HBA is already attached to super */
422 if (super
->hba
== NULL
) {
423 super
->hba
= alloc_intel_hba(device
);
428 /* Intel metadata allows for all disks attached to the same type HBA.
429 * Do not sypport odf HBA types mixing
431 if (device
->type
!= hba
->type
)
437 hba
->next
= alloc_intel_hba(device
);
441 static struct sys_dev
* find_disk_attached_hba(int fd
, const char *devname
)
443 struct sys_dev
*list
, *elem
, *prev
;
446 if ((list
= find_intel_devices()) == NULL
)
450 disk_path
= (char *) devname
;
452 disk_path
= diskfd_to_devpath(fd
);
459 for (prev
= NULL
, elem
= list
; elem
; prev
= elem
, elem
= elem
->next
) {
460 if (path_attached_to_hba(disk_path
, elem
->path
)) {
464 prev
->next
= elem
->next
;
466 if (disk_path
!= devname
)
472 if (disk_path
!= devname
)
480 static struct supertype
*match_metadata_desc_imsm(char *arg
)
482 struct supertype
*st
;
484 if (strcmp(arg
, "imsm") != 0 &&
485 strcmp(arg
, "default") != 0
489 st
= malloc(sizeof(*st
));
492 memset(st
, 0, sizeof(*st
));
493 st
->container_dev
= NoMdDev
;
494 st
->ss
= &super_imsm
;
495 st
->max_devs
= IMSM_MAX_DEVICES
;
496 st
->minor_version
= 0;
502 static __u8
*get_imsm_version(struct imsm_super
*mpb
)
504 return &mpb
->sig
[MPB_SIG_LEN
];
508 /* retrieve a disk directly from the anchor when the anchor is known to be
509 * up-to-date, currently only at load time
511 static struct imsm_disk
*__get_imsm_disk(struct imsm_super
*mpb
, __u8 index
)
513 if (index
>= mpb
->num_disks
)
515 return &mpb
->disk
[index
];
518 /* retrieve the disk description based on a index of the disk
521 static struct dl
*get_imsm_dl_disk(struct intel_super
*super
, __u8 index
)
525 for (d
= super
->disks
; d
; d
= d
->next
)
526 if (d
->index
== index
)
531 /* retrieve a disk from the parsed metadata */
532 static struct imsm_disk
*get_imsm_disk(struct intel_super
*super
, __u8 index
)
536 dl
= get_imsm_dl_disk(super
, index
);
543 /* generate a checksum directly from the anchor when the anchor is known to be
544 * up-to-date, currently only at load or write_super after coalescing
546 static __u32
__gen_imsm_checksum(struct imsm_super
*mpb
)
548 __u32 end
= mpb
->mpb_size
/ sizeof(end
);
549 __u32
*p
= (__u32
*) mpb
;
553 sum
+= __le32_to_cpu(*p
);
557 return sum
- __le32_to_cpu(mpb
->check_sum
);
560 static size_t sizeof_imsm_map(struct imsm_map
*map
)
562 return sizeof(struct imsm_map
) + sizeof(__u32
) * (map
->num_members
- 1);
565 struct imsm_map
*get_imsm_map(struct imsm_dev
*dev
, int second_map
)
567 struct imsm_map
*map
= &dev
->vol
.map
[0];
569 if (second_map
&& !dev
->vol
.migr_state
)
571 else if (second_map
) {
574 return ptr
+ sizeof_imsm_map(map
);
580 /* return the size of the device.
581 * migr_state increases the returned size if map[0] were to be duplicated
583 static size_t sizeof_imsm_dev(struct imsm_dev
*dev
, int migr_state
)
585 size_t size
= sizeof(*dev
) - sizeof(struct imsm_map
) +
586 sizeof_imsm_map(get_imsm_map(dev
, 0));
588 /* migrating means an additional map */
589 if (dev
->vol
.migr_state
)
590 size
+= sizeof_imsm_map(get_imsm_map(dev
, 1));
592 size
+= sizeof_imsm_map(get_imsm_map(dev
, 0));
598 /* retrieve disk serial number list from a metadata update */
599 static struct disk_info
*get_disk_info(struct imsm_update_create_array
*update
)
602 struct disk_info
*inf
;
604 inf
= u
+ sizeof(*update
) - sizeof(struct imsm_dev
) +
605 sizeof_imsm_dev(&update
->dev
, 0);
611 static struct imsm_dev
*__get_imsm_dev(struct imsm_super
*mpb
, __u8 index
)
617 if (index
>= mpb
->num_raid_devs
)
620 /* devices start after all disks */
621 offset
= ((void *) &mpb
->disk
[mpb
->num_disks
]) - _mpb
;
623 for (i
= 0; i
<= index
; i
++)
625 return _mpb
+ offset
;
627 offset
+= sizeof_imsm_dev(_mpb
+ offset
, 0);
632 static struct imsm_dev
*get_imsm_dev(struct intel_super
*super
, __u8 index
)
634 struct intel_dev
*dv
;
636 if (index
>= super
->anchor
->num_raid_devs
)
638 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
639 if (dv
->index
== index
)
647 * == 1 get second map
648 * == -1 than get map according to the current migr_state
650 static __u32
get_imsm_ord_tbl_ent(struct imsm_dev
*dev
,
654 struct imsm_map
*map
;
656 if (second_map
== -1) {
657 if (dev
->vol
.migr_state
)
658 map
= get_imsm_map(dev
, 1);
660 map
= get_imsm_map(dev
, 0);
662 map
= get_imsm_map(dev
, second_map
);
665 /* top byte identifies disk under rebuild */
666 return __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
669 #define ord_to_idx(ord) (((ord) << 8) >> 8)
670 static __u32
get_imsm_disk_idx(struct imsm_dev
*dev
, int slot
, int second_map
)
672 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, second_map
);
674 return ord_to_idx(ord
);
677 static void set_imsm_ord_tbl_ent(struct imsm_map
*map
, int slot
, __u32 ord
)
679 map
->disk_ord_tbl
[slot
] = __cpu_to_le32(ord
);
682 static int get_imsm_disk_slot(struct imsm_map
*map
, unsigned idx
)
687 for (slot
= 0; slot
< map
->num_members
; slot
++) {
688 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
689 if (ord_to_idx(ord
) == idx
)
696 static int get_imsm_raid_level(struct imsm_map
*map
)
698 if (map
->raid_level
== 1) {
699 if (map
->num_members
== 2)
705 return map
->raid_level
;
708 static int cmp_extent(const void *av
, const void *bv
)
710 const struct extent
*a
= av
;
711 const struct extent
*b
= bv
;
712 if (a
->start
< b
->start
)
714 if (a
->start
> b
->start
)
719 static int count_memberships(struct dl
*dl
, struct intel_super
*super
)
724 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
725 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
726 struct imsm_map
*map
= get_imsm_map(dev
, 0);
728 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
735 static struct extent
*get_extents(struct intel_super
*super
, struct dl
*dl
)
737 /* find a list of used extents on the given physical device */
738 struct extent
*rv
, *e
;
740 int memberships
= count_memberships(dl
, super
);
741 __u32 reservation
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
743 rv
= malloc(sizeof(struct extent
) * (memberships
+ 1));
748 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
749 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
750 struct imsm_map
*map
= get_imsm_map(dev
, 0);
752 if (get_imsm_disk_slot(map
, dl
->index
) >= 0) {
753 e
->start
= __le32_to_cpu(map
->pba_of_lba0
);
754 e
->size
= __le32_to_cpu(map
->blocks_per_member
);
758 qsort(rv
, memberships
, sizeof(*rv
), cmp_extent
);
760 /* determine the start of the metadata
761 * when no raid devices are defined use the default
762 * ...otherwise allow the metadata to truncate the value
763 * as is the case with older versions of imsm
766 struct extent
*last
= &rv
[memberships
- 1];
769 remainder
= __le32_to_cpu(dl
->disk
.total_blocks
) -
770 (last
->start
+ last
->size
);
771 /* round down to 1k block to satisfy precision of the kernel
775 /* make sure remainder is still sane */
776 if (remainder
< (unsigned)ROUND_UP(super
->len
, 512) >> 9)
777 remainder
= ROUND_UP(super
->len
, 512) >> 9;
778 if (reservation
> remainder
)
779 reservation
= remainder
;
781 e
->start
= __le32_to_cpu(dl
->disk
.total_blocks
) - reservation
;
786 /* try to determine how much space is reserved for metadata from
787 * the last get_extents() entry, otherwise fallback to the
790 static __u32
imsm_reserved_sectors(struct intel_super
*super
, struct dl
*dl
)
796 /* for spares just return a minimal reservation which will grow
797 * once the spare is picked up by an array
800 return MPB_SECTOR_CNT
;
802 e
= get_extents(super
, dl
);
804 return MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
806 /* scroll to last entry */
807 for (i
= 0; e
[i
].size
; i
++)
810 rv
= __le32_to_cpu(dl
->disk
.total_blocks
) - e
[i
].start
;
817 static int is_spare(struct imsm_disk
*disk
)
819 return (disk
->status
& SPARE_DISK
) == SPARE_DISK
;
822 static int is_configured(struct imsm_disk
*disk
)
824 return (disk
->status
& CONFIGURED_DISK
) == CONFIGURED_DISK
;
827 static int is_failed(struct imsm_disk
*disk
)
829 return (disk
->status
& FAILED_DISK
) == FAILED_DISK
;
832 /* Return minimum size of a spare that can be used in this array*/
833 static unsigned long long min_acceptable_spare_size_imsm(struct supertype
*st
)
835 struct intel_super
*super
= st
->sb
;
839 unsigned long long rv
= 0;
843 /* find first active disk in array */
845 while (dl
&& (is_failed(&dl
->disk
) || dl
->index
== -1))
849 /* find last lba used by subarrays */
850 e
= get_extents(super
, dl
);
853 for (i
= 0; e
[i
].size
; i
++)
856 rv
= e
[i
-1].start
+ e
[i
-1].size
;
858 /* add the amount of space needed for metadata */
859 rv
= rv
+ MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
864 static __u64
blocks_per_migr_unit(struct imsm_dev
*dev
);
866 static void print_imsm_dev(struct imsm_dev
*dev
, char *uuid
, int disk_idx
)
870 struct imsm_map
*map
= get_imsm_map(dev
, 0);
871 struct imsm_map
*map2
= get_imsm_map(dev
, 1);
875 printf("[%.16s]:\n", dev
->volume
);
876 printf(" UUID : %s\n", uuid
);
877 printf(" RAID Level : %d", get_imsm_raid_level(map
));
879 printf(" <-- %d", get_imsm_raid_level(map2
));
881 printf(" Members : %d", map
->num_members
);
883 printf(" <-- %d", map2
->num_members
);
885 printf(" Slots : [");
886 for (i
= 0; i
< map
->num_members
; i
++) {
887 ord
= get_imsm_ord_tbl_ent(dev
, i
, 0);
888 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
893 for (i
= 0; i
< map2
->num_members
; i
++) {
894 ord
= get_imsm_ord_tbl_ent(dev
, i
, 1);
895 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
900 slot
= get_imsm_disk_slot(map
, disk_idx
);
902 ord
= get_imsm_ord_tbl_ent(dev
, slot
, -1);
903 printf(" This Slot : %d%s\n", slot
,
904 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
906 printf(" This Slot : ?\n");
907 sz
= __le32_to_cpu(dev
->size_high
);
909 sz
+= __le32_to_cpu(dev
->size_low
);
910 printf(" Array Size : %llu%s\n", (unsigned long long)sz
,
911 human_size(sz
* 512));
912 sz
= __le32_to_cpu(map
->blocks_per_member
);
913 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz
,
914 human_size(sz
* 512));
915 printf(" Sector Offset : %u\n",
916 __le32_to_cpu(map
->pba_of_lba0
));
917 printf(" Num Stripes : %u\n",
918 __le32_to_cpu(map
->num_data_stripes
));
919 printf(" Chunk Size : %u KiB",
920 __le16_to_cpu(map
->blocks_per_strip
) / 2);
922 printf(" <-- %u KiB",
923 __le16_to_cpu(map2
->blocks_per_strip
) / 2);
925 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
926 printf(" Migrate State : ");
927 if (dev
->vol
.migr_state
) {
928 if (migr_type(dev
) == MIGR_INIT
)
929 printf("initialize\n");
930 else if (migr_type(dev
) == MIGR_REBUILD
)
932 else if (migr_type(dev
) == MIGR_VERIFY
)
934 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
935 printf("general migration\n");
936 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
937 printf("state change\n");
938 else if (migr_type(dev
) == MIGR_REPAIR
)
941 printf("<unknown:%d>\n", migr_type(dev
));
944 printf(" Map State : %s", map_state_str
[map
->map_state
]);
945 if (dev
->vol
.migr_state
) {
946 struct imsm_map
*map
= get_imsm_map(dev
, 1);
948 printf(" <-- %s", map_state_str
[map
->map_state
]);
949 printf("\n Checkpoint : %u (%llu)",
950 __le32_to_cpu(dev
->vol
.curr_migr_unit
),
951 (unsigned long long)blocks_per_migr_unit(dev
));
954 printf(" Dirty State : %s\n", dev
->vol
.dirty
? "dirty" : "clean");
957 static void print_imsm_disk(struct imsm_super
*mpb
, int index
, __u32 reserved
)
959 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, index
);
960 char str
[MAX_RAID_SERIAL_LEN
+ 1];
963 if (index
< 0 || !disk
)
967 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
968 printf(" Disk%02d Serial : %s\n", index
, str
);
969 printf(" State :%s%s%s\n", is_spare(disk
) ? " spare" : "",
970 is_configured(disk
) ? " active" : "",
971 is_failed(disk
) ? " failed" : "");
972 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
973 sz
= __le32_to_cpu(disk
->total_blocks
) - reserved
;
974 printf(" Usable Size : %llu%s\n", (unsigned long long)sz
,
975 human_size(sz
* 512));
978 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
);
980 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
982 struct intel_super
*super
= st
->sb
;
983 struct imsm_super
*mpb
= super
->anchor
;
984 char str
[MAX_SIGNATURE_LENGTH
];
989 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
992 snprintf(str
, MPB_SIG_LEN
, "%s", mpb
->sig
);
993 printf(" Magic : %s\n", str
);
994 snprintf(str
, strlen(MPB_VERSION_RAID0
), "%s", get_imsm_version(mpb
));
995 printf(" Version : %s\n", get_imsm_version(mpb
));
996 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
997 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
998 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
999 getinfo_super_imsm(st
, &info
, NULL
);
1000 fname_from_uuid(st
, &info
, nbuf
, ':');
1001 printf(" UUID : %s\n", nbuf
+ 5);
1002 sum
= __le32_to_cpu(mpb
->check_sum
);
1003 printf(" Checksum : %08x %s\n", sum
,
1004 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
1005 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
));
1006 printf(" Disks : %d\n", mpb
->num_disks
);
1007 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
1008 print_imsm_disk(mpb
, super
->disks
->index
, reserved
);
1009 if (super
->bbm_log
) {
1010 struct bbm_log
*log
= super
->bbm_log
;
1013 printf("Bad Block Management Log:\n");
1014 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
1015 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
1016 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
1017 printf(" Spare Blocks : %d\n", __le32_to_cpu(log
->reserved_spare_block_count
));
1018 printf(" First Spare : %llx\n",
1019 (unsigned long long) __le64_to_cpu(log
->first_spare_lba
));
1021 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1023 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1025 super
->current_vol
= i
;
1026 getinfo_super_imsm(st
, &info
, NULL
);
1027 fname_from_uuid(st
, &info
, nbuf
, ':');
1028 print_imsm_dev(dev
, nbuf
+ 5, super
->disks
->index
);
1030 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1031 if (i
== super
->disks
->index
)
1033 print_imsm_disk(mpb
, i
, reserved
);
1035 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
1036 struct imsm_disk
*disk
;
1037 char str
[MAX_RAID_SERIAL_LEN
+ 1];
1045 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
1046 printf(" Disk Serial : %s\n", str
);
1047 printf(" State :%s%s%s\n", is_spare(disk
) ? " spare" : "",
1048 is_configured(disk
) ? " active" : "",
1049 is_failed(disk
) ? " failed" : "");
1050 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
1051 sz
= __le32_to_cpu(disk
->total_blocks
) - reserved
;
1052 printf(" Usable Size : %llu%s\n", (unsigned long long)sz
,
1053 human_size(sz
* 512));
1057 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
1059 /* We just write a generic IMSM ARRAY entry */
1062 struct intel_super
*super
= st
->sb
;
1064 if (!super
->anchor
->num_raid_devs
) {
1065 printf("ARRAY metadata=imsm\n");
1069 getinfo_super_imsm(st
, &info
, NULL
);
1070 fname_from_uuid(st
, &info
, nbuf
, ':');
1071 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
1074 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
1076 /* We just write a generic IMSM ARRAY entry */
1080 struct intel_super
*super
= st
->sb
;
1083 if (!super
->anchor
->num_raid_devs
)
1086 getinfo_super_imsm(st
, &info
, NULL
);
1087 fname_from_uuid(st
, &info
, nbuf
, ':');
1088 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1089 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1091 super
->current_vol
= i
;
1092 getinfo_super_imsm(st
, &info
, NULL
);
1093 fname_from_uuid(st
, &info
, nbuf1
, ':');
1094 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
1095 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
1099 static void export_examine_super_imsm(struct supertype
*st
)
1101 struct intel_super
*super
= st
->sb
;
1102 struct imsm_super
*mpb
= super
->anchor
;
1106 getinfo_super_imsm(st
, &info
, NULL
);
1107 fname_from_uuid(st
, &info
, nbuf
, ':');
1108 printf("MD_METADATA=imsm\n");
1109 printf("MD_LEVEL=container\n");
1110 printf("MD_UUID=%s\n", nbuf
+5);
1111 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
1114 static void detail_super_imsm(struct supertype
*st
, char *homehost
)
1119 getinfo_super_imsm(st
, &info
, NULL
);
1120 fname_from_uuid(st
, &info
, nbuf
, ':');
1121 printf("\n UUID : %s\n", nbuf
+ 5);
1124 static void brief_detail_super_imsm(struct supertype
*st
)
1128 getinfo_super_imsm(st
, &info
, NULL
);
1129 fname_from_uuid(st
, &info
, nbuf
, ':');
1130 printf(" UUID=%s", nbuf
+ 5);
1133 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
);
1134 static void fd2devname(int fd
, char *name
);
1136 static int ahci_enumerate_ports(const char *hba_path
, int port_count
, int host_base
, int verbose
)
1138 /* dump an unsorted list of devices attached to AHCI Intel storage
1139 * controller, as well as non-connected ports
1141 int hba_len
= strlen(hba_path
) + 1;
1146 unsigned long port_mask
= (1 << port_count
) - 1;
1148 if (port_count
> (int)sizeof(port_mask
) * 8) {
1150 fprintf(stderr
, Name
": port_count %d out of range\n", port_count
);
1154 /* scroll through /sys/dev/block looking for devices attached to
1157 dir
= opendir("/sys/dev/block");
1158 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
1169 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
1171 path
= devt_to_devpath(makedev(major
, minor
));
1174 if (!path_attached_to_hba(path
, hba_path
)) {
1180 /* retrieve the scsi device type */
1181 if (asprintf(&device
, "/sys/dev/block/%d:%d/device/xxxxxxx", major
, minor
) < 0) {
1183 fprintf(stderr
, Name
": failed to allocate 'device'\n");
1187 sprintf(device
, "/sys/dev/block/%d:%d/device/type", major
, minor
);
1188 if (load_sys(device
, buf
) != 0) {
1190 fprintf(stderr
, Name
": failed to read device type for %s\n",
1196 type
= strtoul(buf
, NULL
, 10);
1198 /* if it's not a disk print the vendor and model */
1199 if (!(type
== 0 || type
== 7 || type
== 14)) {
1202 sprintf(device
, "/sys/dev/block/%d:%d/device/vendor", major
, minor
);
1203 if (load_sys(device
, buf
) == 0) {
1204 strncpy(vendor
, buf
, sizeof(vendor
));
1205 vendor
[sizeof(vendor
) - 1] = '\0';
1206 c
= (char *) &vendor
[sizeof(vendor
) - 1];
1207 while (isspace(*c
) || *c
== '\0')
1211 sprintf(device
, "/sys/dev/block/%d:%d/device/model", major
, minor
);
1212 if (load_sys(device
, buf
) == 0) {
1213 strncpy(model
, buf
, sizeof(model
));
1214 model
[sizeof(model
) - 1] = '\0';
1215 c
= (char *) &model
[sizeof(model
) - 1];
1216 while (isspace(*c
) || *c
== '\0')
1220 if (vendor
[0] && model
[0])
1221 sprintf(buf
, "%.64s %.64s", vendor
, model
);
1223 switch (type
) { /* numbers from hald/linux/device.c */
1224 case 1: sprintf(buf
, "tape"); break;
1225 case 2: sprintf(buf
, "printer"); break;
1226 case 3: sprintf(buf
, "processor"); break;
1228 case 5: sprintf(buf
, "cdrom"); break;
1229 case 6: sprintf(buf
, "scanner"); break;
1230 case 8: sprintf(buf
, "media_changer"); break;
1231 case 9: sprintf(buf
, "comm"); break;
1232 case 12: sprintf(buf
, "raid"); break;
1233 default: sprintf(buf
, "unknown");
1239 /* chop device path to 'host%d' and calculate the port number */
1240 c
= strchr(&path
[hba_len
], '/');
1243 fprintf(stderr
, Name
": %s - invalid path name\n", path
+ hba_len
);
1248 if (sscanf(&path
[hba_len
], "host%d", &port
) == 1)
1252 *c
= '/'; /* repair the full string */
1253 fprintf(stderr
, Name
": failed to determine port number for %s\n",
1260 /* mark this port as used */
1261 port_mask
&= ~(1 << port
);
1263 /* print out the device information */
1265 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
1269 fd
= dev_open(ent
->d_name
, O_RDONLY
);
1271 printf(" Port%d : - disk info unavailable -\n", port
);
1273 fd2devname(fd
, buf
);
1274 printf(" Port%d : %s", port
, buf
);
1275 if (imsm_read_serial(fd
, NULL
, (__u8
*) buf
) == 0)
1276 printf(" (%s)\n", buf
);
1291 for (i
= 0; i
< port_count
; i
++)
1292 if (port_mask
& (1 << i
))
1293 printf(" Port%d : - no device attached -\n", i
);
1301 static void print_found_intel_controllers(struct sys_dev
*elem
)
1303 for (; elem
; elem
= elem
->next
) {
1304 fprintf(stderr
, Name
": found Intel(R) ");
1305 if (elem
->type
== SYS_DEV_SATA
)
1306 fprintf(stderr
, "SATA ");
1307 else if (elem
->type
== SYS_DEV_SAS
)
1308 fprintf(stderr
, "SAS ");
1309 fprintf(stderr
, "RAID controller");
1311 fprintf(stderr
, " at %s", elem
->pci_id
);
1312 fprintf(stderr
, ".\n");
1317 static int ahci_get_port_count(const char *hba_path
, int *port_count
)
1324 if ((dir
= opendir(hba_path
)) == NULL
)
1327 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
1330 if (sscanf(ent
->d_name
, "host%d", &host
) != 1)
1332 if (*port_count
== 0)
1334 else if (host
< host_base
)
1337 if (host
+ 1 > *port_count
+ host_base
)
1338 *port_count
= host
+ 1 - host_base
;
1344 static int detail_platform_imsm(int verbose
, int enumerate_only
)
1346 /* There are two components to imsm platform support, the ahci SATA
1347 * controller and the option-rom. To find the SATA controller we
1348 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
1349 * controller with the Intel vendor id is present. This approach
1350 * allows mdadm to leverage the kernel's ahci detection logic, with the
1351 * caveat that if ahci.ko is not loaded mdadm will not be able to
1352 * detect platform raid capabilities. The option-rom resides in a
1353 * platform "Adapter ROM". We scan for its signature to retrieve the
1354 * platform capabilities. If raid support is disabled in the BIOS the
1355 * option-rom capability structure will not be available.
1357 const struct imsm_orom
*orom
;
1358 struct sys_dev
*list
, *hba
;
1363 if (enumerate_only
) {
1364 if (check_env("IMSM_NO_PLATFORM") || find_imsm_orom())
1369 list
= find_intel_devices();
1372 fprintf(stderr
, Name
": no active Intel(R) RAID "
1373 "controller found.\n");
1374 free_sys_dev(&list
);
1377 print_found_intel_controllers(list
);
1379 orom
= find_imsm_orom();
1381 free_sys_dev(&list
);
1383 fprintf(stderr
, Name
": imsm option-rom not found\n");
1387 printf(" Platform : Intel(R) Matrix Storage Manager\n");
1388 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
1389 orom
->hotfix_ver
, orom
->build
);
1390 printf(" RAID Levels :%s%s%s%s%s\n",
1391 imsm_orom_has_raid0(orom
) ? " raid0" : "",
1392 imsm_orom_has_raid1(orom
) ? " raid1" : "",
1393 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
1394 imsm_orom_has_raid10(orom
) ? " raid10" : "",
1395 imsm_orom_has_raid5(orom
) ? " raid5" : "");
1396 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1397 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
1398 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
1399 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
1400 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
1401 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
1402 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
1403 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
1404 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
1405 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
1406 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
1407 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
1408 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
1409 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
1410 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
1411 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
1412 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
1413 printf(" Max Disks : %d\n", orom
->tds
);
1414 printf(" Max Volumes : %d\n", orom
->vpa
);
1416 for (hba
= list
; hba
; hba
= hba
->next
) {
1417 printf(" I/O Controller : %s (%s)\n",
1418 hba
->path
, get_sys_dev_type(hba
->type
));
1420 if (hba
->type
== SYS_DEV_SATA
) {
1421 host_base
= ahci_get_port_count(hba
->path
, &port_count
);
1422 if (ahci_enumerate_ports(hba
->path
, port_count
, host_base
, verbose
)) {
1424 fprintf(stderr
, Name
": failed to enumerate "
1425 "ports on SATA controller at %s.", hba
->pci_id
);
1428 } else if (hba
->type
== SYS_DEV_SAS
) {
1430 fprintf(stderr
, Name
": failed to enumerate "
1431 "devices on SAS controller at %s.", hba
->pci_id
);
1436 free_sys_dev(&list
);
1441 static int match_home_imsm(struct supertype
*st
, char *homehost
)
1443 /* the imsm metadata format does not specify any host
1444 * identification information. We return -1 since we can never
1445 * confirm nor deny whether a given array is "meant" for this
1446 * host. We rely on compare_super and the 'family_num' fields to
1447 * exclude member disks that do not belong, and we rely on
1448 * mdadm.conf to specify the arrays that should be assembled.
1449 * Auto-assembly may still pick up "foreign" arrays.
1455 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
1457 /* The uuid returned here is used for:
1458 * uuid to put into bitmap file (Create, Grow)
1459 * uuid for backup header when saving critical section (Grow)
1460 * comparing uuids when re-adding a device into an array
1461 * In these cases the uuid required is that of the data-array,
1462 * not the device-set.
1463 * uuid to recognise same set when adding a missing device back
1464 * to an array. This is a uuid for the device-set.
1466 * For each of these we can make do with a truncated
1467 * or hashed uuid rather than the original, as long as
1469 * In each case the uuid required is that of the data-array,
1470 * not the device-set.
1472 /* imsm does not track uuid's so we synthesis one using sha1 on
1473 * - The signature (Which is constant for all imsm array, but no matter)
1474 * - the orig_family_num of the container
1475 * - the index number of the volume
1476 * - the 'serial' number of the volume.
1477 * Hopefully these are all constant.
1479 struct intel_super
*super
= st
->sb
;
1482 struct sha1_ctx ctx
;
1483 struct imsm_dev
*dev
= NULL
;
1486 /* some mdadm versions failed to set ->orig_family_num, in which
1487 * case fall back to ->family_num. orig_family_num will be
1488 * fixed up with the first metadata update.
1490 family_num
= super
->anchor
->orig_family_num
;
1491 if (family_num
== 0)
1492 family_num
= super
->anchor
->family_num
;
1493 sha1_init_ctx(&ctx
);
1494 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
1495 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
1496 if (super
->current_vol
>= 0)
1497 dev
= get_imsm_dev(super
, super
->current_vol
);
1499 __u32 vol
= super
->current_vol
;
1500 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
1501 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
1503 sha1_finish_ctx(&ctx
, buf
);
1504 memcpy(uuid
, buf
, 4*4);
1509 get_imsm_numerical_version(struct imsm_super
*mpb
, int *m
, int *p
)
1511 __u8
*v
= get_imsm_version(mpb
);
1512 __u8
*end
= mpb
->sig
+ MAX_SIGNATURE_LENGTH
;
1513 char major
[] = { 0, 0, 0 };
1514 char minor
[] = { 0 ,0, 0 };
1515 char patch
[] = { 0, 0, 0 };
1516 char *ver_parse
[] = { major
, minor
, patch
};
1520 while (*v
!= '\0' && v
< end
) {
1521 if (*v
!= '.' && j
< 2)
1522 ver_parse
[i
][j
++] = *v
;
1530 *m
= strtol(minor
, NULL
, 0);
1531 *p
= strtol(patch
, NULL
, 0);
1535 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
1537 /* migr_strip_size when repairing or initializing parity */
1538 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1539 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1541 switch (get_imsm_raid_level(map
)) {
1546 return 128*1024 >> 9;
1550 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
1552 /* migr_strip_size when rebuilding a degraded disk, no idea why
1553 * this is different than migr_strip_size_resync(), but it's good
1556 struct imsm_map
*map
= get_imsm_map(dev
, 1);
1557 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1559 switch (get_imsm_raid_level(map
)) {
1562 if (map
->num_members
% map
->num_domains
== 0)
1563 return 128*1024 >> 9;
1567 return max((__u32
) 64*1024 >> 9, chunk
);
1569 return 128*1024 >> 9;
1573 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
1575 struct imsm_map
*lo
= get_imsm_map(dev
, 0);
1576 struct imsm_map
*hi
= get_imsm_map(dev
, 1);
1577 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
1578 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
1580 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
1583 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
1585 struct imsm_map
*lo
= get_imsm_map(dev
, 0);
1586 int level
= get_imsm_raid_level(lo
);
1588 if (level
== 1 || level
== 10) {
1589 struct imsm_map
*hi
= get_imsm_map(dev
, 1);
1591 return hi
->num_domains
;
1593 return num_stripes_per_unit_resync(dev
);
1596 static __u8
imsm_num_data_members(struct imsm_dev
*dev
, int second_map
)
1598 /* named 'imsm_' because raid0, raid1 and raid10
1599 * counter-intuitively have the same number of data disks
1601 struct imsm_map
*map
= get_imsm_map(dev
, second_map
);
1603 switch (get_imsm_raid_level(map
)) {
1607 return map
->num_members
;
1609 return map
->num_members
- 1;
1611 dprintf("%s: unsupported raid level\n", __func__
);
1616 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
1618 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1619 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1621 switch(get_imsm_raid_level(map
)) {
1624 return chunk
* map
->num_domains
;
1626 return chunk
* map
->num_members
;
1632 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
1634 struct imsm_map
*map
= get_imsm_map(dev
, 1);
1635 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1636 __u32 strip
= block
/ chunk
;
1638 switch (get_imsm_raid_level(map
)) {
1641 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
1642 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
1644 return vol_stripe
* chunk
+ block
% chunk
;
1646 __u32 stripe
= strip
/ (map
->num_members
- 1);
1648 return stripe
* chunk
+ block
% chunk
;
1655 static __u64
blocks_per_migr_unit(struct imsm_dev
*dev
)
1657 /* calculate the conversion factor between per member 'blocks'
1658 * (md/{resync,rebuild}_start) and imsm migration units, return
1659 * 0 for the 'not migrating' and 'unsupported migration' cases
1661 if (!dev
->vol
.migr_state
)
1664 switch (migr_type(dev
)) {
1669 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1670 __u32 stripes_per_unit
;
1671 __u32 blocks_per_unit
;
1680 /* yes, this is really the translation of migr_units to
1681 * per-member blocks in the 'resync' case
1683 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
1684 migr_chunk
= migr_strip_blocks_resync(dev
);
1685 disks
= imsm_num_data_members(dev
, 0);
1686 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
1687 stripe
= __le32_to_cpu(map
->blocks_per_strip
) * disks
;
1688 segment
= blocks_per_unit
/ stripe
;
1689 block_rel
= blocks_per_unit
- segment
* stripe
;
1690 parity_depth
= parity_segment_depth(dev
);
1691 block_map
= map_migr_block(dev
, block_rel
);
1692 return block_map
+ parity_depth
* segment
;
1694 case MIGR_REBUILD
: {
1695 __u32 stripes_per_unit
;
1698 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
1699 migr_chunk
= migr_strip_blocks_rebuild(dev
);
1700 return migr_chunk
* stripes_per_unit
;
1702 case MIGR_STATE_CHANGE
:
1708 static int imsm_level_to_layout(int level
)
1716 return ALGORITHM_LEFT_ASYMMETRIC
;
1723 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
1725 struct intel_super
*super
= st
->sb
;
1726 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
1727 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1728 struct imsm_map
*prev_map
= get_imsm_map(dev
, 1);
1729 struct imsm_map
*map_to_analyse
= map
;
1732 int map_disks
= info
->array
.raid_disks
;
1735 map_to_analyse
= prev_map
;
1737 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
1738 if (dl
->raiddisk
== info
->disk
.raid_disk
)
1740 info
->container_member
= super
->current_vol
;
1741 info
->array
.raid_disks
= map_to_analyse
->num_members
;
1742 info
->array
.level
= get_imsm_raid_level(map_to_analyse
);
1743 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
1744 info
->array
.md_minor
= -1;
1745 info
->array
.ctime
= 0;
1746 info
->array
.utime
= 0;
1747 info
->array
.chunk_size
=
1748 __le16_to_cpu(map_to_analyse
->blocks_per_strip
) << 9;
1749 info
->array
.state
= !dev
->vol
.dirty
;
1750 info
->custom_array_size
= __le32_to_cpu(dev
->size_high
);
1751 info
->custom_array_size
<<= 32;
1752 info
->custom_array_size
|= __le32_to_cpu(dev
->size_low
);
1754 info
->new_level
= get_imsm_raid_level(map
);
1755 info
->new_layout
= imsm_level_to_layout(info
->new_level
);
1756 info
->new_chunk
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
1758 info
->new_level
= UnSet
;
1759 info
->new_layout
= UnSet
;
1760 info
->new_chunk
= info
->array
.chunk_size
;
1762 info
->disk
.major
= 0;
1763 info
->disk
.minor
= 0;
1765 info
->disk
.major
= dl
->major
;
1766 info
->disk
.minor
= dl
->minor
;
1769 info
->data_offset
= __le32_to_cpu(map_to_analyse
->pba_of_lba0
);
1770 info
->component_size
=
1771 __le32_to_cpu(map_to_analyse
->blocks_per_member
);
1772 memset(info
->uuid
, 0, sizeof(info
->uuid
));
1773 info
->recovery_start
= MaxSector
;
1774 info
->reshape_active
= (prev_map
!= NULL
);
1775 if (info
->reshape_active
)
1776 info
->delta_disks
= map
->num_members
- prev_map
->num_members
;
1778 info
->delta_disks
= 0;
1780 if (map_to_analyse
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
1782 info
->resync_start
= 0;
1783 } else if (dev
->vol
.migr_state
) {
1784 switch (migr_type(dev
)) {
1787 __u64 blocks_per_unit
= blocks_per_migr_unit(dev
);
1788 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
1790 info
->resync_start
= blocks_per_unit
* units
;
1794 /* we could emulate the checkpointing of
1795 * 'sync_action=check' migrations, but for now
1796 * we just immediately complete them
1799 /* this is handled by container_content_imsm() */
1801 case MIGR_STATE_CHANGE
:
1802 /* FIXME handle other migrations */
1804 /* we are not dirty, so... */
1805 info
->resync_start
= MaxSector
;
1808 info
->resync_start
= MaxSector
;
1810 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
1811 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
1813 info
->array
.major_version
= -1;
1814 info
->array
.minor_version
= -2;
1815 devname
= devnum2devname(st
->container_dev
);
1816 *info
->text_version
= '\0';
1818 sprintf(info
->text_version
, "/%s/%d", devname
, info
->container_member
);
1820 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
1821 uuid_from_super_imsm(st
, info
->uuid
);
1825 for (i
=0; i
<map_disks
; i
++) {
1827 if (i
< info
->array
.raid_disks
) {
1828 struct imsm_disk
*dsk
;
1829 j
= get_imsm_disk_idx(dev
, i
, -1);
1830 dsk
= get_imsm_disk(super
, j
);
1831 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
1838 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
, int failed
);
1839 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
);
1841 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
1845 for (d
= super
->missing
; d
; d
= d
->next
)
1846 if (d
->index
== index
)
1851 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
1853 struct intel_super
*super
= st
->sb
;
1854 struct imsm_disk
*disk
;
1855 int map_disks
= info
->array
.raid_disks
;
1856 int max_enough
= -1;
1858 struct imsm_super
*mpb
;
1860 if (super
->current_vol
>= 0) {
1861 getinfo_super_imsm_volume(st
, info
, map
);
1865 /* Set raid_disks to zero so that Assemble will always pull in valid
1868 info
->array
.raid_disks
= 0;
1869 info
->array
.level
= LEVEL_CONTAINER
;
1870 info
->array
.layout
= 0;
1871 info
->array
.md_minor
= -1;
1872 info
->array
.ctime
= 0; /* N/A for imsm */
1873 info
->array
.utime
= 0;
1874 info
->array
.chunk_size
= 0;
1876 info
->disk
.major
= 0;
1877 info
->disk
.minor
= 0;
1878 info
->disk
.raid_disk
= -1;
1879 info
->reshape_active
= 0;
1880 info
->array
.major_version
= -1;
1881 info
->array
.minor_version
= -2;
1882 strcpy(info
->text_version
, "imsm");
1883 info
->safe_mode_delay
= 0;
1884 info
->disk
.number
= -1;
1885 info
->disk
.state
= 0;
1887 info
->recovery_start
= MaxSector
;
1889 /* do we have the all the insync disks that we expect? */
1890 mpb
= super
->anchor
;
1892 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1893 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1894 int failed
, enough
, j
, missing
= 0;
1895 struct imsm_map
*map
;
1898 failed
= imsm_count_failed(super
, dev
);
1899 state
= imsm_check_degraded(super
, dev
, failed
);
1900 map
= get_imsm_map(dev
, dev
->vol
.migr_state
);
1902 /* any newly missing disks?
1903 * (catches single-degraded vs double-degraded)
1905 for (j
= 0; j
< map
->num_members
; j
++) {
1906 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, -1);
1907 __u32 idx
= ord_to_idx(ord
);
1909 if (!(ord
& IMSM_ORD_REBUILD
) &&
1910 get_imsm_missing(super
, idx
)) {
1916 if (state
== IMSM_T_STATE_FAILED
)
1918 else if (state
== IMSM_T_STATE_DEGRADED
&&
1919 (state
!= map
->map_state
|| missing
))
1921 else /* we're normal, or already degraded */
1924 /* in the missing/failed disk case check to see
1925 * if at least one array is runnable
1927 max_enough
= max(max_enough
, enough
);
1929 dprintf("%s: enough: %d\n", __func__
, max_enough
);
1930 info
->container_enough
= max_enough
;
1933 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
1935 disk
= &super
->disks
->disk
;
1936 info
->data_offset
= __le32_to_cpu(disk
->total_blocks
) - reserved
;
1937 info
->component_size
= reserved
;
1938 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
1939 /* we don't change info->disk.raid_disk here because
1940 * this state will be finalized in mdmon after we have
1941 * found the 'most fresh' version of the metadata
1943 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
1944 info
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
1947 /* only call uuid_from_super_imsm when this disk is part of a populated container,
1948 * ->compare_super may have updated the 'num_raid_devs' field for spares
1950 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
1951 uuid_from_super_imsm(st
, info
->uuid
);
1953 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
1955 /* I don't know how to compute 'map' on imsm, so use safe default */
1958 for (i
= 0; i
< map_disks
; i
++)
1964 /* allocates memory and fills disk in mdinfo structure
1965 * for each disk in array */
1966 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
1968 struct mdinfo
*mddev
= NULL
;
1969 struct intel_super
*super
= st
->sb
;
1970 struct imsm_disk
*disk
;
1973 if (!super
|| !super
->disks
)
1976 mddev
= malloc(sizeof(*mddev
));
1978 fprintf(stderr
, Name
": Failed to allocate memory.\n");
1981 memset(mddev
, 0, sizeof(*mddev
));
1985 tmp
= malloc(sizeof(*tmp
));
1987 fprintf(stderr
, Name
": Failed to allocate memory.\n");
1992 memset(tmp
, 0, sizeof(*tmp
));
1994 tmp
->next
= mddev
->devs
;
1996 tmp
->disk
.number
= count
++;
1997 tmp
->disk
.major
= dl
->major
;
1998 tmp
->disk
.minor
= dl
->minor
;
1999 tmp
->disk
.state
= is_configured(disk
) ?
2000 (1 << MD_DISK_ACTIVE
) : 0;
2001 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
2002 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
2003 tmp
->disk
.raid_disk
= -1;
2009 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
2010 char *update
, char *devname
, int verbose
,
2011 int uuid_set
, char *homehost
)
2013 /* For 'assemble' and 'force' we need to return non-zero if any
2014 * change was made. For others, the return value is ignored.
2015 * Update options are:
2016 * force-one : This device looks a bit old but needs to be included,
2017 * update age info appropriately.
2018 * assemble: clear any 'faulty' flag to allow this device to
2020 * force-array: Array is degraded but being forced, mark it clean
2021 * if that will be needed to assemble it.
2023 * newdev: not used ????
2024 * grow: Array has gained a new device - this is currently for
2026 * resync: mark as dirty so a resync will happen.
2027 * name: update the name - preserving the homehost
2028 * uuid: Change the uuid of the array to match watch is given
2030 * Following are not relevant for this imsm:
2031 * sparc2.2 : update from old dodgey metadata
2032 * super-minor: change the preferred_minor number
2033 * summaries: update redundant counters.
2034 * homehost: update the recorded homehost
2035 * _reshape_progress: record new reshape_progress position.
2038 struct intel_super
*super
= st
->sb
;
2039 struct imsm_super
*mpb
;
2041 /* we can only update container info */
2042 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
2045 mpb
= super
->anchor
;
2047 if (strcmp(update
, "uuid") == 0 && uuid_set
&& !info
->update_private
)
2049 else if (strcmp(update
, "uuid") == 0 && uuid_set
&& info
->update_private
) {
2050 mpb
->orig_family_num
= *((__u32
*) info
->update_private
);
2052 } else if (strcmp(update
, "uuid") == 0) {
2053 __u32
*new_family
= malloc(sizeof(*new_family
));
2055 /* update orig_family_number with the incoming random
2056 * data, report the new effective uuid, and store the
2057 * new orig_family_num for future updates.
2060 memcpy(&mpb
->orig_family_num
, info
->uuid
, sizeof(__u32
));
2061 uuid_from_super_imsm(st
, info
->uuid
);
2062 *new_family
= mpb
->orig_family_num
;
2063 info
->update_private
= new_family
;
2066 } else if (strcmp(update
, "assemble") == 0)
2071 /* successful update? recompute checksum */
2073 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
2078 static size_t disks_to_mpb_size(int disks
)
2082 size
= sizeof(struct imsm_super
);
2083 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
2084 size
+= 2 * sizeof(struct imsm_dev
);
2085 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
2086 size
+= (4 - 2) * sizeof(struct imsm_map
);
2087 /* 4 possible disk_ord_tbl's */
2088 size
+= 4 * (disks
- 1) * sizeof(__u32
);
2093 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
)
2095 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
2098 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
2101 static void free_devlist(struct intel_super
*super
)
2103 struct intel_dev
*dv
;
2105 while (super
->devlist
) {
2106 dv
= super
->devlist
->next
;
2107 free(super
->devlist
->dev
);
2108 free(super
->devlist
);
2109 super
->devlist
= dv
;
2113 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
2115 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
2118 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
)
2122 * 0 same, or first was empty, and second was copied
2123 * 1 second had wrong number
2125 * 3 wrong other info
2127 struct intel_super
*first
= st
->sb
;
2128 struct intel_super
*sec
= tst
->sb
;
2136 /* if an anchor does not have num_raid_devs set then it is a free
2139 if (first
->anchor
->num_raid_devs
> 0 &&
2140 sec
->anchor
->num_raid_devs
> 0) {
2141 /* Determine if these disks might ever have been
2142 * related. Further disambiguation can only take place
2143 * in load_super_imsm_all
2145 __u32 first_family
= first
->anchor
->orig_family_num
;
2146 __u32 sec_family
= sec
->anchor
->orig_family_num
;
2148 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
2149 MAX_SIGNATURE_LENGTH
) != 0)
2152 if (first_family
== 0)
2153 first_family
= first
->anchor
->family_num
;
2154 if (sec_family
== 0)
2155 sec_family
= sec
->anchor
->family_num
;
2157 if (first_family
!= sec_family
)
2163 /* if 'first' is a spare promote it to a populated mpb with sec's
2166 if (first
->anchor
->num_raid_devs
== 0 &&
2167 sec
->anchor
->num_raid_devs
> 0) {
2169 struct intel_dev
*dv
;
2170 struct imsm_dev
*dev
;
2172 /* we need to copy raid device info from sec if an allocation
2173 * fails here we don't associate the spare
2175 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++) {
2176 dv
= malloc(sizeof(*dv
));
2179 dev
= malloc(sizeof_imsm_dev(get_imsm_dev(sec
, i
), 1));
2186 dv
->next
= first
->devlist
;
2187 first
->devlist
= dv
;
2189 if (i
< sec
->anchor
->num_raid_devs
) {
2190 /* allocation failure */
2191 free_devlist(first
);
2192 fprintf(stderr
, "imsm: failed to associate spare\n");
2195 first
->anchor
->num_raid_devs
= sec
->anchor
->num_raid_devs
;
2196 first
->anchor
->orig_family_num
= sec
->anchor
->orig_family_num
;
2197 first
->anchor
->family_num
= sec
->anchor
->family_num
;
2198 memcpy(first
->anchor
->sig
, sec
->anchor
->sig
, MAX_SIGNATURE_LENGTH
);
2199 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++)
2200 imsm_copy_dev(get_imsm_dev(first
, i
), get_imsm_dev(sec
, i
));
2206 static void fd2devname(int fd
, char *name
)
2210 char dname
[PATH_MAX
];
2215 if (fstat(fd
, &st
) != 0)
2217 sprintf(path
, "/sys/dev/block/%d:%d",
2218 major(st
.st_rdev
), minor(st
.st_rdev
));
2220 rv
= readlink(path
, dname
, sizeof(dname
));
2225 nm
= strrchr(dname
, '/');
2227 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
2230 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
2232 static int imsm_read_serial(int fd
, char *devname
,
2233 __u8 serial
[MAX_RAID_SERIAL_LEN
])
2235 unsigned char scsi_serial
[255];
2244 memset(scsi_serial
, 0, sizeof(scsi_serial
));
2246 rv
= scsi_get_serial(fd
, scsi_serial
, sizeof(scsi_serial
));
2248 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
2249 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
2250 fd2devname(fd
, (char *) serial
);
2257 Name
": Failed to retrieve serial for %s\n",
2262 rsp_len
= scsi_serial
[3];
2266 Name
": Failed to retrieve serial for %s\n",
2270 rsp_buf
= (char *) &scsi_serial
[4];
2272 /* trim all whitespace and non-printable characters and convert
2275 for (i
= 0, dest
= rsp_buf
; i
< rsp_len
; i
++) {
2278 /* ':' is reserved for use in placeholder serial
2279 * numbers for missing disks
2287 len
= dest
- rsp_buf
;
2290 /* truncate leading characters */
2291 if (len
> MAX_RAID_SERIAL_LEN
) {
2292 dest
+= len
- MAX_RAID_SERIAL_LEN
;
2293 len
= MAX_RAID_SERIAL_LEN
;
2296 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
2297 memcpy(serial
, dest
, len
);
2302 static int serialcmp(__u8
*s1
, __u8
*s2
)
2304 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
2307 static void serialcpy(__u8
*dest
, __u8
*src
)
2309 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
2313 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
2317 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
2318 if (serialcmp(dl
->serial
, serial
) == 0)
2325 static struct imsm_disk
*
2326 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
2330 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2331 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
2333 if (serialcmp(disk
->serial
, serial
) == 0) {
2344 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
2346 struct imsm_disk
*disk
;
2351 __u8 serial
[MAX_RAID_SERIAL_LEN
];
2353 rv
= imsm_read_serial(fd
, devname
, serial
);
2358 dl
= calloc(1, sizeof(*dl
));
2362 Name
": failed to allocate disk buffer for %s\n",
2368 dl
->major
= major(stb
.st_rdev
);
2369 dl
->minor
= minor(stb
.st_rdev
);
2370 dl
->next
= super
->disks
;
2371 dl
->fd
= keep_fd
? fd
: -1;
2372 assert(super
->disks
== NULL
);
2374 serialcpy(dl
->serial
, serial
);
2377 fd2devname(fd
, name
);
2379 dl
->devname
= strdup(devname
);
2381 dl
->devname
= strdup(name
);
2383 /* look up this disk's index in the current anchor */
2384 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
2387 /* only set index on disks that are a member of a
2388 * populated contianer, i.e. one with raid_devs
2390 if (is_failed(&dl
->disk
))
2392 else if (is_spare(&dl
->disk
))
2400 /* When migrating map0 contains the 'destination' state while map1
2401 * contains the current state. When not migrating map0 contains the
2402 * current state. This routine assumes that map[0].map_state is set to
2403 * the current array state before being called.
2405 * Migration is indicated by one of the following states
2406 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
2407 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
2408 * map1state=unitialized)
2409 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
2411 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
2412 * map1state=degraded)
2414 static void migrate(struct imsm_dev
*dev
, __u8 to_state
, int migr_type
)
2416 struct imsm_map
*dest
;
2417 struct imsm_map
*src
= get_imsm_map(dev
, 0);
2419 dev
->vol
.migr_state
= 1;
2420 set_migr_type(dev
, migr_type
);
2421 dev
->vol
.curr_migr_unit
= 0;
2422 dest
= get_imsm_map(dev
, 1);
2424 /* duplicate and then set the target end state in map[0] */
2425 memcpy(dest
, src
, sizeof_imsm_map(src
));
2426 if ((migr_type
== MIGR_REBUILD
) ||
2427 (migr_type
== MIGR_GEN_MIGR
)) {
2431 for (i
= 0; i
< src
->num_members
; i
++) {
2432 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
2433 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
2437 src
->map_state
= to_state
;
2440 static void end_migration(struct imsm_dev
*dev
, __u8 map_state
)
2442 struct imsm_map
*map
= get_imsm_map(dev
, 0);
2443 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
);
2446 /* merge any IMSM_ORD_REBUILD bits that were not successfully
2447 * completed in the last migration.
2449 * FIXME add support for raid-level-migration
2451 for (i
= 0; i
< prev
->num_members
; i
++)
2452 for (j
= 0; j
< map
->num_members
; j
++)
2453 /* during online capacity expansion
2454 * disks position can be changed if takeover is used
2456 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
2457 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
2458 map
->disk_ord_tbl
[j
] |= prev
->disk_ord_tbl
[i
];
2462 dev
->vol
.migr_state
= 0;
2463 dev
->vol
.migr_type
= 0;
2464 dev
->vol
.curr_migr_unit
= 0;
2465 map
->map_state
= map_state
;
2469 static int parse_raid_devices(struct intel_super
*super
)
2472 struct imsm_dev
*dev_new
;
2473 size_t len
, len_migr
;
2475 size_t space_needed
= 0;
2476 struct imsm_super
*mpb
= super
->anchor
;
2478 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
2479 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
2480 struct intel_dev
*dv
;
2482 len
= sizeof_imsm_dev(dev_iter
, 0);
2483 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
2485 space_needed
+= len_migr
- len
;
2487 dv
= malloc(sizeof(*dv
));
2490 if (max_len
< len_migr
)
2492 if (max_len
> len_migr
)
2493 space_needed
+= max_len
- len_migr
;
2494 dev_new
= malloc(max_len
);
2499 imsm_copy_dev(dev_new
, dev_iter
);
2502 dv
->next
= super
->devlist
;
2503 super
->devlist
= dv
;
2506 /* ensure that super->buf is large enough when all raid devices
2509 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
2512 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
, 512);
2513 if (posix_memalign(&buf
, 512, len
) != 0)
2516 memcpy(buf
, super
->buf
, super
->len
);
2517 memset(buf
+ super
->len
, 0, len
- super
->len
);
2526 /* retrieve a pointer to the bbm log which starts after all raid devices */
2527 struct bbm_log
*__get_imsm_bbm_log(struct imsm_super
*mpb
)
2531 if (__le32_to_cpu(mpb
->bbm_log_size
)) {
2533 ptr
+= mpb
->mpb_size
- __le32_to_cpu(mpb
->bbm_log_size
);
2539 static void __free_imsm(struct intel_super
*super
, int free_disks
);
2541 /* load_imsm_mpb - read matrix metadata
2542 * allocates super->mpb to be freed by free_super
2544 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
2546 unsigned long long dsize
;
2547 unsigned long long sectors
;
2549 struct imsm_super
*anchor
;
2552 get_dev_size(fd
, NULL
, &dsize
);
2556 Name
": %s: device to small for imsm\n",
2561 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0) {
2564 Name
": Cannot seek to anchor block on %s: %s\n",
2565 devname
, strerror(errno
));
2569 if (posix_memalign((void**)&anchor
, 512, 512) != 0) {
2572 Name
": Failed to allocate imsm anchor buffer"
2573 " on %s\n", devname
);
2576 if (read(fd
, anchor
, 512) != 512) {
2579 Name
": Cannot read anchor block on %s: %s\n",
2580 devname
, strerror(errno
));
2585 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
2588 Name
": no IMSM anchor on %s\n", devname
);
2593 __free_imsm(super
, 0);
2594 super
->len
= ROUND_UP(anchor
->mpb_size
, 512);
2595 if (posix_memalign(&super
->buf
, 512, super
->len
) != 0) {
2598 Name
": unable to allocate %zu byte mpb buffer\n",
2603 memcpy(super
->buf
, anchor
, 512);
2605 sectors
= mpb_sectors(anchor
) - 1;
2608 check_sum
= __gen_imsm_checksum(super
->anchor
);
2609 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
2612 Name
": IMSM checksum %x != %x on %s\n",
2614 __le32_to_cpu(super
->anchor
->check_sum
),
2622 /* read the extended mpb */
2623 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0) {
2626 Name
": Cannot seek to extended mpb on %s: %s\n",
2627 devname
, strerror(errno
));
2631 if ((unsigned)read(fd
, super
->buf
+ 512, super
->len
- 512) != super
->len
- 512) {
2634 Name
": Cannot read extended mpb on %s: %s\n",
2635 devname
, strerror(errno
));
2639 check_sum
= __gen_imsm_checksum(super
->anchor
);
2640 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
2643 Name
": IMSM checksum %x != %x on %s\n",
2644 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
2649 /* FIXME the BBM log is disk specific so we cannot use this global
2650 * buffer for all disks. Ok for now since we only look at the global
2651 * bbm_log_size parameter to gate assembly
2653 super
->bbm_log
= __get_imsm_bbm_log(super
->anchor
);
2659 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
2663 err
= load_imsm_mpb(fd
, super
, devname
);
2666 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
2669 err
= parse_raid_devices(super
);
2674 static void __free_imsm_disk(struct dl
*d
)
2686 static void free_imsm_disks(struct intel_super
*super
)
2690 while (super
->disks
) {
2692 super
->disks
= d
->next
;
2693 __free_imsm_disk(d
);
2695 while (super
->disk_mgmt_list
) {
2696 d
= super
->disk_mgmt_list
;
2697 super
->disk_mgmt_list
= d
->next
;
2698 __free_imsm_disk(d
);
2700 while (super
->missing
) {
2702 super
->missing
= d
->next
;
2703 __free_imsm_disk(d
);
2708 /* free all the pieces hanging off of a super pointer */
2709 static void __free_imsm(struct intel_super
*super
, int free_disks
)
2711 struct intel_hba
*elem
, *next
;
2718 free_imsm_disks(super
);
2719 free_devlist(super
);
2723 free((void *)elem
->path
);
2731 static void free_imsm(struct intel_super
*super
)
2733 __free_imsm(super
, 1);
2737 static void free_super_imsm(struct supertype
*st
)
2739 struct intel_super
*super
= st
->sb
;
2748 static struct intel_super
*alloc_super(void)
2750 struct intel_super
*super
= malloc(sizeof(*super
));
2753 memset(super
, 0, sizeof(*super
));
2754 super
->current_vol
= -1;
2755 super
->create_offset
= ~((__u32
) 0);
2756 if (!check_env("IMSM_NO_PLATFORM"))
2757 super
->orom
= find_imsm_orom();
2764 /* find_missing - helper routine for load_super_imsm_all that identifies
2765 * disks that have disappeared from the system. This routine relies on
2766 * the mpb being uptodate, which it is at load time.
2768 static int find_missing(struct intel_super
*super
)
2771 struct imsm_super
*mpb
= super
->anchor
;
2773 struct imsm_disk
*disk
;
2775 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2776 disk
= __get_imsm_disk(mpb
, i
);
2777 dl
= serial_to_dl(disk
->serial
, super
);
2781 dl
= malloc(sizeof(*dl
));
2787 dl
->devname
= strdup("missing");
2789 serialcpy(dl
->serial
, disk
->serial
);
2792 dl
->next
= super
->missing
;
2793 super
->missing
= dl
;
2799 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
2801 struct intel_disk
*idisk
= disk_list
;
2804 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
2806 idisk
= idisk
->next
;
2812 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
2813 struct intel_super
*super
,
2814 struct intel_disk
**disk_list
)
2816 struct imsm_disk
*d
= &super
->disks
->disk
;
2817 struct imsm_super
*mpb
= super
->anchor
;
2820 for (i
= 0; i
< tbl_size
; i
++) {
2821 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
2822 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
2824 if (tbl_mpb
->family_num
== mpb
->family_num
) {
2825 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
2826 dprintf("%s: mpb from %d:%d matches %d:%d\n",
2827 __func__
, super
->disks
->major
,
2828 super
->disks
->minor
,
2829 table
[i
]->disks
->major
,
2830 table
[i
]->disks
->minor
);
2834 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
2835 is_configured(d
) == is_configured(tbl_d
)) &&
2836 tbl_mpb
->generation_num
< mpb
->generation_num
) {
2837 /* current version of the mpb is a
2838 * better candidate than the one in
2839 * super_table, but copy over "cross
2840 * generational" status
2842 struct intel_disk
*idisk
;
2844 dprintf("%s: mpb from %d:%d replaces %d:%d\n",
2845 __func__
, super
->disks
->major
,
2846 super
->disks
->minor
,
2847 table
[i
]->disks
->major
,
2848 table
[i
]->disks
->minor
);
2850 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
2851 if (idisk
&& is_failed(&idisk
->disk
))
2852 tbl_d
->status
|= FAILED_DISK
;
2855 struct intel_disk
*idisk
;
2856 struct imsm_disk
*disk
;
2858 /* tbl_mpb is more up to date, but copy
2859 * over cross generational status before
2862 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
2863 if (disk
&& is_failed(disk
))
2864 d
->status
|= FAILED_DISK
;
2866 idisk
= disk_list_get(d
->serial
, *disk_list
);
2869 if (disk
&& is_configured(disk
))
2870 idisk
->disk
.status
|= CONFIGURED_DISK
;
2873 dprintf("%s: mpb from %d:%d prefer %d:%d\n",
2874 __func__
, super
->disks
->major
,
2875 super
->disks
->minor
,
2876 table
[i
]->disks
->major
,
2877 table
[i
]->disks
->minor
);
2885 table
[tbl_size
++] = super
;
2889 /* update/extend the merged list of imsm_disk records */
2890 for (j
= 0; j
< mpb
->num_disks
; j
++) {
2891 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
2892 struct intel_disk
*idisk
;
2894 idisk
= disk_list_get(disk
->serial
, *disk_list
);
2896 idisk
->disk
.status
|= disk
->status
;
2897 if (is_configured(&idisk
->disk
) ||
2898 is_failed(&idisk
->disk
))
2899 idisk
->disk
.status
&= ~(SPARE_DISK
);
2901 idisk
= calloc(1, sizeof(*idisk
));
2904 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
2905 idisk
->disk
= *disk
;
2906 idisk
->next
= *disk_list
;
2910 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
2917 static struct intel_super
*
2918 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
2921 struct imsm_super
*mpb
= super
->anchor
;
2925 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2926 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
2927 struct intel_disk
*idisk
;
2929 idisk
= disk_list_get(disk
->serial
, disk_list
);
2931 if (idisk
->owner
== owner
||
2932 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
2935 dprintf("%s: '%.16s' owner %d != %d\n",
2936 __func__
, disk
->serial
, idisk
->owner
,
2939 dprintf("%s: unknown disk %x [%d]: %.16s\n",
2940 __func__
, __le32_to_cpu(mpb
->family_num
), i
,
2946 if (ok_count
== mpb
->num_disks
)
2951 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
2953 struct intel_super
*s
;
2955 for (s
= super_list
; s
; s
= s
->next
) {
2956 if (family_num
!= s
->anchor
->family_num
)
2958 fprintf(stderr
, "Conflict, offlining family %#x on '%s'\n",
2959 __le32_to_cpu(family_num
), s
->disks
->devname
);
2963 static struct intel_super
*
2964 imsm_thunderdome(struct intel_super
**super_list
, int len
)
2966 struct intel_super
*super_table
[len
];
2967 struct intel_disk
*disk_list
= NULL
;
2968 struct intel_super
*champion
, *spare
;
2969 struct intel_super
*s
, **del
;
2974 memset(super_table
, 0, sizeof(super_table
));
2975 for (s
= *super_list
; s
; s
= s
->next
)
2976 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
2978 for (i
= 0; i
< tbl_size
; i
++) {
2979 struct imsm_disk
*d
;
2980 struct intel_disk
*idisk
;
2981 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
2984 d
= &s
->disks
->disk
;
2986 /* 'd' must appear in merged disk list for its
2987 * configuration to be valid
2989 idisk
= disk_list_get(d
->serial
, disk_list
);
2990 if (idisk
&& idisk
->owner
== i
)
2991 s
= validate_members(s
, disk_list
, i
);
2996 dprintf("%s: marking family: %#x from %d:%d offline\n",
2997 __func__
, mpb
->family_num
,
2998 super_table
[i
]->disks
->major
,
2999 super_table
[i
]->disks
->minor
);
3003 /* This is where the mdadm implementation differs from the Windows
3004 * driver which has no strict concept of a container. We can only
3005 * assemble one family from a container, so when returning a prodigal
3006 * array member to this system the code will not be able to disambiguate
3007 * the container contents that should be assembled ("foreign" versus
3008 * "local"). It requires user intervention to set the orig_family_num
3009 * to a new value to establish a new container. The Windows driver in
3010 * this situation fixes up the volume name in place and manages the
3011 * foreign array as an independent entity.
3016 for (i
= 0; i
< tbl_size
; i
++) {
3017 struct intel_super
*tbl_ent
= super_table
[i
];
3023 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
3028 if (s
&& !is_spare
) {
3029 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
3031 } else if (!s
&& !is_spare
)
3044 fprintf(stderr
, "Chose family %#x on '%s', "
3045 "assemble conflicts to new container with '--update=uuid'\n",
3046 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
3048 /* collect all dl's onto 'champion', and update them to
3049 * champion's version of the status
3051 for (s
= *super_list
; s
; s
= s
->next
) {
3052 struct imsm_super
*mpb
= champion
->anchor
;
3053 struct dl
*dl
= s
->disks
;
3058 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3059 struct imsm_disk
*disk
;
3061 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
3064 /* only set index on disks that are a member of
3065 * a populated contianer, i.e. one with
3068 if (is_failed(&dl
->disk
))
3070 else if (is_spare(&dl
->disk
))
3076 if (i
>= mpb
->num_disks
) {
3077 struct intel_disk
*idisk
;
3079 idisk
= disk_list_get(dl
->serial
, disk_list
);
3080 if (idisk
&& is_spare(&idisk
->disk
) &&
3081 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
3089 dl
->next
= champion
->disks
;
3090 champion
->disks
= dl
;
3094 /* delete 'champion' from super_list */
3095 for (del
= super_list
; *del
; ) {
3096 if (*del
== champion
) {
3097 *del
= (*del
)->next
;
3100 del
= &(*del
)->next
;
3102 champion
->next
= NULL
;
3106 struct intel_disk
*idisk
= disk_list
;
3108 disk_list
= disk_list
->next
;
3115 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
3119 struct intel_super
*super_list
= NULL
;
3120 struct intel_super
*super
= NULL
;
3121 int devnum
= fd2devnum(fd
);
3127 /* check if 'fd' an opened container */
3128 sra
= sysfs_read(fd
, 0, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
3132 if (sra
->array
.major_version
!= -1 ||
3133 sra
->array
.minor_version
!= -2 ||
3134 strcmp(sra
->text_version
, "imsm") != 0) {
3139 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
3140 struct intel_super
*s
= alloc_super();
3147 s
->next
= super_list
;
3151 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
3152 dfd
= dev_open(nm
, O_RDWR
);
3156 err
= load_and_parse_mpb(dfd
, s
, NULL
, 1);
3158 /* retry the load if we might have raced against mdmon */
3159 if (err
== 3 && mdmon_running(devnum
))
3160 for (retry
= 0; retry
< 3; retry
++) {
3162 err
= load_and_parse_mpb(dfd
, s
, NULL
, 1);
3170 /* all mpbs enter, maybe one leaves */
3171 super
= imsm_thunderdome(&super_list
, i
);
3177 if (find_missing(super
) != 0) {
3185 while (super_list
) {
3186 struct intel_super
*s
= super_list
;
3188 super_list
= super_list
->next
;
3197 st
->container_dev
= devnum
;
3198 if (err
== 0 && st
->ss
== NULL
) {
3199 st
->ss
= &super_imsm
;
3200 st
->minor_version
= 0;
3201 st
->max_devs
= IMSM_MAX_DEVICES
;
3206 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
3208 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
);
3212 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
3214 struct intel_super
*super
;
3217 if (test_partition(fd
))
3218 /* IMSM not allowed on partitions */
3221 free_super_imsm(st
);
3223 super
= alloc_super();
3226 Name
": malloc of %zu failed.\n",
3231 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
3236 Name
": Failed to load all information "
3237 "sections on %s\n", devname
);
3243 if (st
->ss
== NULL
) {
3244 st
->ss
= &super_imsm
;
3245 st
->minor_version
= 0;
3246 st
->max_devs
= IMSM_MAX_DEVICES
;
3251 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
3253 if (info
->level
== 1)
3255 return info
->chunk_size
>> 9;
3258 static __u32
info_to_num_data_stripes(mdu_array_info_t
*info
, int num_domains
)
3262 num_stripes
= (info
->size
* 2) / info_to_blocks_per_strip(info
);
3263 num_stripes
/= num_domains
;
3268 static __u32
info_to_blocks_per_member(mdu_array_info_t
*info
)
3270 if (info
->level
== 1)
3271 return info
->size
* 2;
3273 return (info
->size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
3276 static void imsm_update_version_info(struct intel_super
*super
)
3278 /* update the version and attributes */
3279 struct imsm_super
*mpb
= super
->anchor
;
3281 struct imsm_dev
*dev
;
3282 struct imsm_map
*map
;
3285 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3286 dev
= get_imsm_dev(super
, i
);
3287 map
= get_imsm_map(dev
, 0);
3288 if (__le32_to_cpu(dev
->size_high
) > 0)
3289 mpb
->attributes
|= MPB_ATTRIB_2TB
;
3291 /* FIXME detect when an array spans a port multiplier */
3293 mpb
->attributes
|= MPB_ATTRIB_PM
;
3296 if (mpb
->num_raid_devs
> 1 ||
3297 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
3298 version
= MPB_VERSION_ATTRIBS
;
3299 switch (get_imsm_raid_level(map
)) {
3300 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
3301 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
3302 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
3303 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
3306 if (map
->num_members
>= 5)
3307 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
3308 else if (dev
->status
== DEV_CLONE_N_GO
)
3309 version
= MPB_VERSION_CNG
;
3310 else if (get_imsm_raid_level(map
) == 5)
3311 version
= MPB_VERSION_RAID5
;
3312 else if (map
->num_members
>= 3)
3313 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
3314 else if (get_imsm_raid_level(map
) == 1)
3315 version
= MPB_VERSION_RAID1
;
3317 version
= MPB_VERSION_RAID0
;
3319 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
3323 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
3325 struct imsm_super
*mpb
= super
->anchor
;
3326 char *reason
= NULL
;
3329 if (strlen(name
) > MAX_RAID_SERIAL_LEN
)
3330 reason
= "must be 16 characters or less";
3332 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3333 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3335 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
3336 reason
= "already exists";
3341 if (reason
&& !quiet
)
3342 fprintf(stderr
, Name
": imsm volume name %s\n", reason
);
3347 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
3348 unsigned long long size
, char *name
,
3349 char *homehost
, int *uuid
)
3351 /* We are creating a volume inside a pre-existing container.
3352 * so st->sb is already set.
3354 struct intel_super
*super
= st
->sb
;
3355 struct imsm_super
*mpb
= super
->anchor
;
3356 struct intel_dev
*dv
;
3357 struct imsm_dev
*dev
;
3358 struct imsm_vol
*vol
;
3359 struct imsm_map
*map
;
3360 int idx
= mpb
->num_raid_devs
;
3362 unsigned long long array_blocks
;
3363 size_t size_old
, size_new
;
3364 __u32 num_data_stripes
;
3366 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
3367 fprintf(stderr
, Name
": This imsm-container already has the "
3368 "maximum of %d volumes\n", super
->orom
->vpa
);
3372 /* ensure the mpb is large enough for the new data */
3373 size_old
= __le32_to_cpu(mpb
->mpb_size
);
3374 size_new
= disks_to_mpb_size(info
->nr_disks
);
3375 if (size_new
> size_old
) {
3377 size_t size_round
= ROUND_UP(size_new
, 512);
3379 if (posix_memalign(&mpb_new
, 512, size_round
) != 0) {
3380 fprintf(stderr
, Name
": could not allocate new mpb\n");
3383 memcpy(mpb_new
, mpb
, size_old
);
3386 super
->anchor
= mpb_new
;
3387 mpb
->mpb_size
= __cpu_to_le32(size_new
);
3388 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
3390 super
->current_vol
= idx
;
3391 /* when creating the first raid device in this container set num_disks
3392 * to zero, i.e. delete this spare and add raid member devices in
3393 * add_to_super_imsm_volume()
3395 if (super
->current_vol
== 0)
3398 if (!check_name(super
, name
, 0))
3400 dv
= malloc(sizeof(*dv
));
3402 fprintf(stderr
, Name
": failed to allocate device list entry\n");
3405 dev
= malloc(sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
3408 fprintf(stderr
, Name
": could not allocate raid device\n");
3411 strncpy((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
3412 if (info
->level
== 1)
3413 array_blocks
= info_to_blocks_per_member(info
);
3415 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
3416 info
->layout
, info
->chunk_size
,
3418 /* round array size down to closest MB */
3419 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
3421 dev
->size_low
= __cpu_to_le32((__u32
) array_blocks
);
3422 dev
->size_high
= __cpu_to_le32((__u32
) (array_blocks
>> 32));
3423 dev
->status
= __cpu_to_le32(0);
3424 dev
->reserved_blocks
= __cpu_to_le32(0);
3426 vol
->migr_state
= 0;
3427 set_migr_type(dev
, MIGR_INIT
);
3429 vol
->curr_migr_unit
= 0;
3430 map
= get_imsm_map(dev
, 0);
3431 map
->pba_of_lba0
= __cpu_to_le32(super
->create_offset
);
3432 map
->blocks_per_member
= __cpu_to_le32(info_to_blocks_per_member(info
));
3433 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
3434 map
->failed_disk_num
= ~0;
3435 map
->map_state
= info
->level
? IMSM_T_STATE_UNINITIALIZED
:
3436 IMSM_T_STATE_NORMAL
;
3439 if (info
->level
== 1 && info
->raid_disks
> 2) {
3442 fprintf(stderr
, Name
": imsm does not support more than 2 disks"
3443 "in a raid1 volume\n");
3447 map
->raid_level
= info
->level
;
3448 if (info
->level
== 10) {
3449 map
->raid_level
= 1;
3450 map
->num_domains
= info
->raid_disks
/ 2;
3451 } else if (info
->level
== 1)
3452 map
->num_domains
= info
->raid_disks
;
3454 map
->num_domains
= 1;
3456 num_data_stripes
= info_to_num_data_stripes(info
, map
->num_domains
);
3457 map
->num_data_stripes
= __cpu_to_le32(num_data_stripes
);
3459 map
->num_members
= info
->raid_disks
;
3460 for (i
= 0; i
< map
->num_members
; i
++) {
3461 /* initialized in add_to_super */
3462 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
3464 mpb
->num_raid_devs
++;
3467 dv
->index
= super
->current_vol
;
3468 dv
->next
= super
->devlist
;
3469 super
->devlist
= dv
;
3471 imsm_update_version_info(super
);
3476 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
3477 unsigned long long size
, char *name
,
3478 char *homehost
, int *uuid
)
3480 /* This is primarily called by Create when creating a new array.
3481 * We will then get add_to_super called for each component, and then
3482 * write_init_super called to write it out to each device.
3483 * For IMSM, Create can create on fresh devices or on a pre-existing
3485 * To create on a pre-existing array a different method will be called.
3486 * This one is just for fresh drives.
3488 struct intel_super
*super
;
3489 struct imsm_super
*mpb
;
3494 return init_super_imsm_volume(st
, info
, size
, name
, homehost
, uuid
);
3497 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
3501 super
= alloc_super();
3502 if (super
&& posix_memalign(&super
->buf
, 512, mpb_size
) != 0) {
3507 fprintf(stderr
, Name
3508 ": %s could not allocate superblock\n", __func__
);
3511 memset(super
->buf
, 0, mpb_size
);
3513 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
3517 /* zeroing superblock */
3521 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
3523 version
= (char *) mpb
->sig
;
3524 strcpy(version
, MPB_SIGNATURE
);
3525 version
+= strlen(MPB_SIGNATURE
);
3526 strcpy(version
, MPB_VERSION_RAID0
);
3532 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
3533 int fd
, char *devname
)
3535 struct intel_super
*super
= st
->sb
;
3536 struct imsm_super
*mpb
= super
->anchor
;
3538 struct imsm_dev
*dev
;
3539 struct imsm_map
*map
;
3542 dev
= get_imsm_dev(super
, super
->current_vol
);
3543 map
= get_imsm_map(dev
, 0);
3545 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
3546 fprintf(stderr
, Name
": %s: Cannot add spare devices to IMSM volume\n",
3552 /* we're doing autolayout so grab the pre-marked (in
3553 * validate_geometry) raid_disk
3555 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3556 if (dl
->raiddisk
== dk
->raid_disk
)
3559 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3560 if (dl
->major
== dk
->major
&&
3561 dl
->minor
== dk
->minor
)
3566 fprintf(stderr
, Name
": %s is not a member of the same container\n", devname
);
3570 /* add a pristine spare to the metadata */
3571 if (dl
->index
< 0) {
3572 dl
->index
= super
->anchor
->num_disks
;
3573 super
->anchor
->num_disks
++;
3575 /* Check the device has not already been added */
3576 slot
= get_imsm_disk_slot(map
, dl
->index
);
3578 (get_imsm_ord_tbl_ent(dev
, slot
, -1) & IMSM_ORD_REBUILD
) == 0) {
3579 fprintf(stderr
, Name
": %s has been included in this array twice\n",
3583 set_imsm_ord_tbl_ent(map
, dk
->number
, dl
->index
);
3584 dl
->disk
.status
= CONFIGURED_DISK
;
3586 /* if we are creating the first raid device update the family number */
3587 if (super
->current_vol
== 0) {
3589 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
3590 struct imsm_disk
*_disk
= __get_imsm_disk(mpb
, dl
->index
);
3592 if (!_dev
|| !_disk
) {
3593 fprintf(stderr
, Name
": BUG mpb setup error\n");
3599 sum
+= __gen_imsm_checksum(mpb
);
3600 mpb
->family_num
= __cpu_to_le32(sum
);
3601 mpb
->orig_family_num
= mpb
->family_num
;
3608 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
3609 int fd
, char *devname
)
3611 struct intel_super
*super
= st
->sb
;
3613 unsigned long long size
;
3618 /* If we are on an RAID enabled platform check that the disk is
3619 * attached to the raid controller.
3620 * We do not need to test disks attachment for container based additions,
3621 * they shall be already tested when container was created/assembled.
3623 if ((fd
!= -1) && !check_env("IMSM_NO_PLATFORM")) {
3624 struct sys_dev
*hba_name
;
3625 struct intel_hba
*hba
;
3627 hba_name
= find_disk_attached_hba(fd
, NULL
);
3630 Name
": %s is not attached to Intel(R) RAID controller.\n",
3631 devname
? : "disk");
3634 rv
= attach_hba_to_super(super
, hba_name
, devname
);
3637 fprintf(stderr
, Name
": %s is attached to Intel(R) %s RAID "
3638 "controller (%s),\n but the container is assigned to Intel(R) "
3639 "%s RAID controller (",
3641 get_sys_dev_type(hba_name
->type
),
3642 hba_name
->pci_id
? : "Err!",
3643 get_sys_dev_type(hba_name
->type
));
3647 fprintf(stderr
, "%s", hba
->pci_id
? : "Err!");
3649 fprintf(stderr
, ", ");
3653 fprintf(stderr
, ").\n"
3654 " Mixing devices attached to different controllers "
3655 "is not allowed.\n");
3656 free_sys_dev(&hba_name
);
3659 free_sys_dev(&hba_name
);
3662 if (super
->current_vol
>= 0)
3663 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
3666 dd
= malloc(sizeof(*dd
));
3669 Name
": malloc failed %s:%d.\n", __func__
, __LINE__
);
3672 memset(dd
, 0, sizeof(*dd
));
3673 dd
->major
= major(stb
.st_rdev
);
3674 dd
->minor
= minor(stb
.st_rdev
);
3676 dd
->devname
= devname
? strdup(devname
) : NULL
;
3679 dd
->action
= DISK_ADD
;
3680 rv
= imsm_read_serial(fd
, devname
, dd
->serial
);
3683 Name
": failed to retrieve scsi serial, aborting\n");
3688 get_dev_size(fd
, NULL
, &size
);
3690 serialcpy(dd
->disk
.serial
, dd
->serial
);
3691 dd
->disk
.total_blocks
= __cpu_to_le32(size
);
3692 dd
->disk
.status
= SPARE_DISK
;
3693 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
3694 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
3696 dd
->disk
.scsi_id
= __cpu_to_le32(0);
3698 if (st
->update_tail
) {
3699 dd
->next
= super
->disk_mgmt_list
;
3700 super
->disk_mgmt_list
= dd
;
3702 dd
->next
= super
->disks
;
3710 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
3712 struct intel_super
*super
= st
->sb
;
3715 /* remove from super works only in mdmon - for communication
3716 * manager - monitor. Check if communication memory buffer
3719 if (!st
->update_tail
) {
3721 Name
": %s shall be used in mdmon context only"
3722 "(line %d).\n", __func__
, __LINE__
);
3725 dd
= malloc(sizeof(*dd
));
3728 Name
": malloc failed %s:%d.\n", __func__
, __LINE__
);
3731 memset(dd
, 0, sizeof(*dd
));
3732 dd
->major
= dk
->major
;
3733 dd
->minor
= dk
->minor
;
3736 dd
->disk
.status
= SPARE_DISK
;
3737 dd
->action
= DISK_REMOVE
;
3739 dd
->next
= super
->disk_mgmt_list
;
3740 super
->disk_mgmt_list
= dd
;
3746 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
3750 struct imsm_super anchor
;
3751 } spare_record
__attribute__ ((aligned(512)));
3753 /* spare records have their own family number and do not have any defined raid
3756 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
3758 struct imsm_super
*mpb
= super
->anchor
;
3759 struct imsm_super
*spare
= &spare_record
.anchor
;
3763 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
)),
3764 spare
->generation_num
= __cpu_to_le32(1UL),
3765 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
3766 spare
->num_disks
= 1,
3767 spare
->num_raid_devs
= 0,
3768 spare
->cache_size
= mpb
->cache_size
,
3769 spare
->pwr_cycle_count
= __cpu_to_le32(1),
3771 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
3772 MPB_SIGNATURE MPB_VERSION_RAID0
);
3774 for (d
= super
->disks
; d
; d
= d
->next
) {
3778 spare
->disk
[0] = d
->disk
;
3779 sum
= __gen_imsm_checksum(spare
);
3780 spare
->family_num
= __cpu_to_le32(sum
);
3781 spare
->orig_family_num
= 0;
3782 sum
= __gen_imsm_checksum(spare
);
3783 spare
->check_sum
= __cpu_to_le32(sum
);
3785 if (store_imsm_mpb(d
->fd
, spare
)) {
3786 fprintf(stderr
, "%s: failed for device %d:%d %s\n",
3787 __func__
, d
->major
, d
->minor
, strerror(errno
));
3799 static int write_super_imsm(struct supertype
*st
, int doclose
)
3801 struct intel_super
*super
= st
->sb
;
3802 struct imsm_super
*mpb
= super
->anchor
;
3808 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
3811 /* 'generation' is incremented everytime the metadata is written */
3812 generation
= __le32_to_cpu(mpb
->generation_num
);
3814 mpb
->generation_num
= __cpu_to_le32(generation
);
3816 /* fix up cases where previous mdadm releases failed to set
3819 if (mpb
->orig_family_num
== 0)
3820 mpb
->orig_family_num
= mpb
->family_num
;
3822 for (d
= super
->disks
; d
; d
= d
->next
) {
3826 mpb
->disk
[d
->index
] = d
->disk
;
3830 for (d
= super
->missing
; d
; d
= d
->next
) {
3831 mpb
->disk
[d
->index
] = d
->disk
;
3834 mpb
->num_disks
= num_disks
;
3835 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
3837 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3838 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
3839 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
3841 imsm_copy_dev(dev
, dev2
);
3842 mpb_size
+= sizeof_imsm_dev(dev
, 0);
3845 mpb_size
+= __le32_to_cpu(mpb
->bbm_log_size
);
3846 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
3848 /* recalculate checksum */
3849 sum
= __gen_imsm_checksum(mpb
);
3850 mpb
->check_sum
= __cpu_to_le32(sum
);
3852 /* write the mpb for disks that compose raid devices */
3853 for (d
= super
->disks
; d
; d
= d
->next
) {
3856 if (store_imsm_mpb(d
->fd
, mpb
))
3857 fprintf(stderr
, "%s: failed for device %d:%d %s\n",
3858 __func__
, d
->major
, d
->minor
, strerror(errno
));
3866 return write_super_imsm_spares(super
, doclose
);
3872 static int create_array(struct supertype
*st
, int dev_idx
)
3875 struct imsm_update_create_array
*u
;
3876 struct intel_super
*super
= st
->sb
;
3877 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
3878 struct imsm_map
*map
= get_imsm_map(dev
, 0);
3879 struct disk_info
*inf
;
3880 struct imsm_disk
*disk
;
3883 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
3884 sizeof(*inf
) * map
->num_members
;
3887 fprintf(stderr
, "%s: failed to allocate update buffer\n",
3892 u
->type
= update_create_array
;
3893 u
->dev_idx
= dev_idx
;
3894 imsm_copy_dev(&u
->dev
, dev
);
3895 inf
= get_disk_info(u
);
3896 for (i
= 0; i
< map
->num_members
; i
++) {
3897 int idx
= get_imsm_disk_idx(dev
, i
, -1);
3899 disk
= get_imsm_disk(super
, idx
);
3900 serialcpy(inf
[i
].serial
, disk
->serial
);
3902 append_metadata_update(st
, u
, len
);
3907 static int mgmt_disk(struct supertype
*st
)
3909 struct intel_super
*super
= st
->sb
;
3911 struct imsm_update_add_remove_disk
*u
;
3913 if (!super
->disk_mgmt_list
)
3919 fprintf(stderr
, "%s: failed to allocate update buffer\n",
3924 u
->type
= update_add_remove_disk
;
3925 append_metadata_update(st
, u
, len
);
3930 static int write_init_super_imsm(struct supertype
*st
)
3932 struct intel_super
*super
= st
->sb
;
3933 int current_vol
= super
->current_vol
;
3935 /* we are done with current_vol reset it to point st at the container */
3936 super
->current_vol
= -1;
3938 if (st
->update_tail
) {
3939 /* queue the recently created array / added disk
3940 * as a metadata update */
3943 /* determine if we are creating a volume or adding a disk */
3944 if (current_vol
< 0) {
3945 /* in the mgmt (add/remove) disk case we are running
3946 * in mdmon context, so don't close fd's
3948 return mgmt_disk(st
);
3950 rv
= create_array(st
, current_vol
);
3955 for (d
= super
->disks
; d
; d
= d
->next
)
3956 Kill(d
->devname
, NULL
, 0, 1, 1);
3957 return write_super_imsm(st
, 1);
3962 static int store_super_imsm(struct supertype
*st
, int fd
)
3964 struct intel_super
*super
= st
->sb
;
3965 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
3971 return store_imsm_mpb(fd
, mpb
);
3977 static int imsm_bbm_log_size(struct imsm_super
*mpb
)
3979 return __le32_to_cpu(mpb
->bbm_log_size
);
3983 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
3984 int layout
, int raiddisks
, int chunk
,
3985 unsigned long long size
, char *dev
,
3986 unsigned long long *freesize
,
3990 unsigned long long ldsize
;
3991 const struct imsm_orom
*orom
;
3993 if (level
!= LEVEL_CONTAINER
)
3998 if (check_env("IMSM_NO_PLATFORM"))
4001 orom
= find_imsm_orom();
4002 if (orom
&& raiddisks
> orom
->tds
) {
4004 fprintf(stderr
, Name
": %d exceeds maximum number of"
4005 " platform supported disks: %d\n",
4006 raiddisks
, orom
->tds
);
4010 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
4013 fprintf(stderr
, Name
": imsm: Cannot open %s: %s\n",
4014 dev
, strerror(errno
));
4017 if (!get_dev_size(fd
, dev
, &ldsize
)) {
4023 *freesize
= avail_size_imsm(st
, ldsize
>> 9);
4028 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
4030 const unsigned long long base_start
= e
[*idx
].start
;
4031 unsigned long long end
= base_start
+ e
[*idx
].size
;
4034 if (base_start
== end
)
4038 for (i
= *idx
; i
< num_extents
; i
++) {
4039 /* extend overlapping extents */
4040 if (e
[i
].start
>= base_start
&&
4041 e
[i
].start
<= end
) {
4044 if (e
[i
].start
+ e
[i
].size
> end
)
4045 end
= e
[i
].start
+ e
[i
].size
;
4046 } else if (e
[i
].start
> end
) {
4052 return end
- base_start
;
4055 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
4057 /* build a composite disk with all known extents and generate a new
4058 * 'maxsize' given the "all disks in an array must share a common start
4059 * offset" constraint
4061 struct extent
*e
= calloc(sum_extents
, sizeof(*e
));
4065 unsigned long long pos
;
4066 unsigned long long start
= 0;
4067 unsigned long long maxsize
;
4068 unsigned long reserve
;
4073 /* coalesce and sort all extents. also, check to see if we need to
4074 * reserve space between member arrays
4077 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
4080 for (i
= 0; i
< dl
->extent_cnt
; i
++)
4083 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
4088 while (i
< sum_extents
) {
4089 e
[j
].start
= e
[i
].start
;
4090 e
[j
].size
= find_size(e
, &i
, sum_extents
);
4092 if (e
[j
-1].size
== 0)
4101 unsigned long long esize
;
4103 esize
= e
[i
].start
- pos
;
4104 if (esize
>= maxsize
) {
4109 pos
= e
[i
].start
+ e
[i
].size
;
4111 } while (e
[i
-1].size
);
4117 /* FIXME assumes volume at offset 0 is the first volume in a
4120 if (start_extent
> 0)
4121 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
4125 if (maxsize
< reserve
)
4128 super
->create_offset
= ~((__u32
) 0);
4129 if (start
+ reserve
> super
->create_offset
)
4130 return 0; /* start overflows create_offset */
4131 super
->create_offset
= start
+ reserve
;
4133 return maxsize
- reserve
;
4136 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
4138 if (level
< 0 || level
== 6 || level
== 4)
4141 /* if we have an orom prevent invalid raid levels */
4144 case 0: return imsm_orom_has_raid0(orom
);
4147 return imsm_orom_has_raid1e(orom
);
4148 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
4149 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
4150 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
4153 return 1; /* not on an Intel RAID platform so anything goes */
4158 #define pr_vrb(fmt, arg...) (void) (verbose && fprintf(stderr, Name fmt, ##arg))
4160 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
4161 int raiddisks
, int chunk
, int verbose
)
4163 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
4164 pr_vrb(": platform does not support raid%d with %d disk%s\n",
4165 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
4168 if (super
->orom
&& level
!= 1 &&
4169 !imsm_orom_has_chunk(super
->orom
, chunk
)) {
4170 pr_vrb(": platform does not support a chunk size of: %d\n", chunk
);
4173 if (layout
!= imsm_level_to_layout(level
)) {
4175 pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n");
4176 else if (level
== 10)
4177 pr_vrb(": imsm raid 10 only supports the n2 layout\n");
4179 pr_vrb(": imsm unknown layout %#x for this raid level %d\n",
4187 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
4188 * FIX ME add ahci details
4190 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
4191 int layout
, int raiddisks
, int chunk
,
4192 unsigned long long size
, char *dev
,
4193 unsigned long long *freesize
,
4197 struct intel_super
*super
= st
->sb
;
4198 struct imsm_super
*mpb
= super
->anchor
;
4200 unsigned long long pos
= 0;
4201 unsigned long long maxsize
;
4205 /* We must have the container info already read in. */
4209 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, verbose
))
4213 /* General test: make sure there is space for
4214 * 'raiddisks' device extents of size 'size' at a given
4217 unsigned long long minsize
= size
;
4218 unsigned long long start_offset
= MaxSector
;
4221 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
4222 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
4227 e
= get_extents(super
, dl
);
4230 unsigned long long esize
;
4231 esize
= e
[i
].start
- pos
;
4232 if (esize
>= minsize
)
4234 if (found
&& start_offset
== MaxSector
) {
4237 } else if (found
&& pos
!= start_offset
) {
4241 pos
= e
[i
].start
+ e
[i
].size
;
4243 } while (e
[i
-1].size
);
4248 if (dcnt
< raiddisks
) {
4250 fprintf(stderr
, Name
": imsm: Not enough "
4251 "devices with space for this array "
4259 /* This device must be a member of the set */
4260 if (stat(dev
, &stb
) < 0)
4262 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
4264 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
4265 if (dl
->major
== (int)major(stb
.st_rdev
) &&
4266 dl
->minor
== (int)minor(stb
.st_rdev
))
4271 fprintf(stderr
, Name
": %s is not in the "
4272 "same imsm set\n", dev
);
4274 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
4275 /* If a volume is present then the current creation attempt
4276 * cannot incorporate new spares because the orom may not
4277 * understand this configuration (all member disks must be
4278 * members of each array in the container).
4280 fprintf(stderr
, Name
": %s is a spare and a volume"
4281 " is already defined for this container\n", dev
);
4282 fprintf(stderr
, Name
": The option-rom requires all member"
4283 " disks to be a member of all volumes\n");
4287 /* retrieve the largest free space block */
4288 e
= get_extents(super
, dl
);
4293 unsigned long long esize
;
4295 esize
= e
[i
].start
- pos
;
4296 if (esize
>= maxsize
)
4298 pos
= e
[i
].start
+ e
[i
].size
;
4300 } while (e
[i
-1].size
);
4305 fprintf(stderr
, Name
": unable to determine free space for: %s\n",
4309 if (maxsize
< size
) {
4311 fprintf(stderr
, Name
": %s not enough space (%llu < %llu)\n",
4312 dev
, maxsize
, size
);
4316 /* count total number of extents for merge */
4318 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4320 i
+= dl
->extent_cnt
;
4322 maxsize
= merge_extents(super
, i
);
4323 if (maxsize
< size
|| maxsize
== 0) {
4325 fprintf(stderr
, Name
": not enough space after merge (%llu < %llu)\n",
4330 *freesize
= maxsize
;
4335 static int reserve_space(struct supertype
*st
, int raiddisks
,
4336 unsigned long long size
, int chunk
,
4337 unsigned long long *freesize
)
4339 struct intel_super
*super
= st
->sb
;
4340 struct imsm_super
*mpb
= super
->anchor
;
4345 unsigned long long maxsize
;
4346 unsigned long long minsize
;
4350 /* find the largest common start free region of the possible disks */
4354 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
4360 /* don't activate new spares if we are orom constrained
4361 * and there is already a volume active in the container
4363 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
4366 e
= get_extents(super
, dl
);
4369 for (i
= 1; e
[i
-1].size
; i
++)
4377 maxsize
= merge_extents(super
, extent_cnt
);
4382 if (cnt
< raiddisks
||
4383 (super
->orom
&& used
&& used
!= raiddisks
) ||
4384 maxsize
< minsize
||
4386 fprintf(stderr
, Name
": not enough devices with space to create array.\n");
4387 return 0; /* No enough free spaces large enough */
4399 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4401 dl
->raiddisk
= cnt
++;
4408 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
4409 int raiddisks
, int chunk
, unsigned long long size
,
4410 char *dev
, unsigned long long *freesize
,
4417 /* if given unused devices create a container
4418 * if given given devices in a container create a member volume
4420 if (level
== LEVEL_CONTAINER
) {
4421 /* Must be a fresh device to add to a container */
4422 return validate_geometry_imsm_container(st
, level
, layout
,
4423 raiddisks
, chunk
, size
,
4429 if (st
->sb
&& freesize
) {
4430 /* we are being asked to automatically layout a
4431 * new volume based on the current contents of
4432 * the container. If the the parameters can be
4433 * satisfied reserve_space will record the disks,
4434 * start offset, and size of the volume to be
4435 * created. add_to_super and getinfo_super
4436 * detect when autolayout is in progress.
4438 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
4442 return reserve_space(st
, raiddisks
, size
, chunk
, freesize
);
4447 /* creating in a given container */
4448 return validate_geometry_imsm_volume(st
, level
, layout
,
4449 raiddisks
, chunk
, size
,
4450 dev
, freesize
, verbose
);
4453 /* This device needs to be a device in an 'imsm' container */
4454 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
4458 Name
": Cannot create this array on device %s\n",
4463 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
4465 fprintf(stderr
, Name
": Cannot open %s: %s\n",
4466 dev
, strerror(errno
));
4469 /* Well, it is in use by someone, maybe an 'imsm' container. */
4470 cfd
= open_container(fd
);
4474 fprintf(stderr
, Name
": Cannot use %s: It is busy\n",
4478 sra
= sysfs_read(cfd
, 0, GET_VERSION
);
4479 if (sra
&& sra
->array
.major_version
== -1 &&
4480 strcmp(sra
->text_version
, "imsm") == 0)
4484 /* This is a member of a imsm container. Load the container
4485 * and try to create a volume
4487 struct intel_super
*super
;
4489 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
) == 0) {
4491 st
->container_dev
= fd2devnum(cfd
);
4493 return validate_geometry_imsm_volume(st
, level
, layout
,
4501 fprintf(stderr
, Name
": failed container membership check\n");
4507 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
4509 struct intel_super
*super
= st
->sb
;
4511 if (level
&& *level
== UnSet
)
4512 *level
= LEVEL_CONTAINER
;
4514 if (level
&& layout
&& *layout
== UnSet
)
4515 *layout
= imsm_level_to_layout(*level
);
4517 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0) &&
4518 super
&& super
->orom
)
4519 *chunk
= imsm_orom_default_chunk(super
->orom
);
4522 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
4524 static int kill_subarray_imsm(struct supertype
*st
)
4526 /* remove the subarray currently referenced by ->current_vol */
4528 struct intel_dev
**dp
;
4529 struct intel_super
*super
= st
->sb
;
4530 __u8 current_vol
= super
->current_vol
;
4531 struct imsm_super
*mpb
= super
->anchor
;
4533 if (super
->current_vol
< 0)
4535 super
->current_vol
= -1; /* invalidate subarray cursor */
4537 /* block deletions that would change the uuid of active subarrays
4539 * FIXME when immutable ids are available, but note that we'll
4540 * also need to fixup the invalidated/active subarray indexes in
4543 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4546 if (i
< current_vol
)
4548 sprintf(subarray
, "%u", i
);
4549 if (is_subarray_active(subarray
, st
->devname
)) {
4551 Name
": deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
4558 if (st
->update_tail
) {
4559 struct imsm_update_kill_array
*u
= malloc(sizeof(*u
));
4563 u
->type
= update_kill_array
;
4564 u
->dev_idx
= current_vol
;
4565 append_metadata_update(st
, u
, sizeof(*u
));
4570 for (dp
= &super
->devlist
; *dp
;)
4571 if ((*dp
)->index
== current_vol
) {
4574 handle_missing(super
, (*dp
)->dev
);
4575 if ((*dp
)->index
> current_vol
)
4580 /* no more raid devices, all active components are now spares,
4581 * but of course failed are still failed
4583 if (--mpb
->num_raid_devs
== 0) {
4586 for (d
= super
->disks
; d
; d
= d
->next
)
4587 if (d
->index
> -2) {
4589 d
->disk
.status
= SPARE_DISK
;
4593 super
->updates_pending
++;
4598 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
4599 char *update
, struct mddev_ident
*ident
)
4601 /* update the subarray currently referenced by ->current_vol */
4602 struct intel_super
*super
= st
->sb
;
4603 struct imsm_super
*mpb
= super
->anchor
;
4605 if (strcmp(update
, "name") == 0) {
4606 char *name
= ident
->name
;
4610 if (is_subarray_active(subarray
, st
->devname
)) {
4612 Name
": Unable to update name of active subarray\n");
4616 if (!check_name(super
, name
, 0))
4619 vol
= strtoul(subarray
, &ep
, 10);
4620 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
4623 if (st
->update_tail
) {
4624 struct imsm_update_rename_array
*u
= malloc(sizeof(*u
));
4628 u
->type
= update_rename_array
;
4630 snprintf((char *) u
->name
, MAX_RAID_SERIAL_LEN
, "%s", name
);
4631 append_metadata_update(st
, u
, sizeof(*u
));
4633 struct imsm_dev
*dev
;
4636 dev
= get_imsm_dev(super
, vol
);
4637 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
4638 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4639 dev
= get_imsm_dev(super
, i
);
4640 handle_missing(super
, dev
);
4642 super
->updates_pending
++;
4649 #endif /* MDASSEMBLE */
4651 static int is_gen_migration(struct imsm_dev
*dev
)
4653 if (!dev
->vol
.migr_state
)
4656 if (migr_type(dev
) == MIGR_GEN_MIGR
)
4662 static int is_rebuilding(struct imsm_dev
*dev
)
4664 struct imsm_map
*migr_map
;
4666 if (!dev
->vol
.migr_state
)
4669 if (migr_type(dev
) != MIGR_REBUILD
)
4672 migr_map
= get_imsm_map(dev
, 1);
4674 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
4680 static void update_recovery_start(struct imsm_dev
*dev
, struct mdinfo
*array
)
4682 struct mdinfo
*rebuild
= NULL
;
4686 if (!is_rebuilding(dev
))
4689 /* Find the rebuild target, but punt on the dual rebuild case */
4690 for (d
= array
->devs
; d
; d
= d
->next
)
4691 if (d
->recovery_start
== 0) {
4698 /* (?) none of the disks are marked with
4699 * IMSM_ORD_REBUILD, so assume they are missing and the
4700 * disk_ord_tbl was not correctly updated
4702 dprintf("%s: failed to locate out-of-sync disk\n", __func__
);
4706 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
4707 rebuild
->recovery_start
= units
* blocks_per_migr_unit(dev
);
4711 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
4713 /* Given a container loaded by load_super_imsm_all,
4714 * extract information about all the arrays into
4716 * If 'subarray' is given, just extract info about that array.
4718 * For each imsm_dev create an mdinfo, fill it in,
4719 * then look for matching devices in super->disks
4720 * and create appropriate device mdinfo.
4722 struct intel_super
*super
= st
->sb
;
4723 struct imsm_super
*mpb
= super
->anchor
;
4724 struct mdinfo
*rest
= NULL
;
4728 /* check for bad blocks */
4729 if (imsm_bbm_log_size(super
->anchor
))
4732 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4733 struct imsm_dev
*dev
;
4734 struct imsm_map
*map
;
4735 struct imsm_map
*map2
;
4736 struct mdinfo
*this;
4741 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
4744 dev
= get_imsm_dev(super
, i
);
4745 map
= get_imsm_map(dev
, 0);
4746 map2
= get_imsm_map(dev
, 1);
4748 /* do not publish arrays that are in the middle of an
4749 * unsupported migration
4751 if (dev
->vol
.migr_state
&&
4752 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
4753 fprintf(stderr
, Name
": cannot assemble volume '%.16s':"
4754 " unsupported migration in progress\n",
4759 this = malloc(sizeof(*this));
4761 fprintf(stderr
, Name
": failed to allocate %zu bytes\n",
4765 memset(this, 0, sizeof(*this));
4768 super
->current_vol
= i
;
4769 getinfo_super_imsm_volume(st
, this, NULL
);
4770 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
4771 unsigned long long recovery_start
;
4772 struct mdinfo
*info_d
;
4779 idx
= get_imsm_disk_idx(dev
, slot
, 0);
4780 ord
= get_imsm_ord_tbl_ent(dev
, slot
, 0);
4781 for (d
= super
->disks
; d
; d
= d
->next
)
4782 if (d
->index
== idx
)
4785 recovery_start
= MaxSector
;
4788 if (d
&& is_failed(&d
->disk
))
4790 if (ord
& IMSM_ORD_REBUILD
)
4794 * if we skip some disks the array will be assmebled degraded;
4795 * reset resync start to avoid a dirty-degraded
4796 * situation when performing the intial sync
4798 * FIXME handle dirty degraded
4800 if ((skip
|| recovery_start
== 0) && !dev
->vol
.dirty
)
4801 this->resync_start
= MaxSector
;
4805 info_d
= calloc(1, sizeof(*info_d
));
4807 fprintf(stderr
, Name
": failed to allocate disk"
4808 " for volume %.16s\n", dev
->volume
);
4809 info_d
= this->devs
;
4811 struct mdinfo
*d
= info_d
->next
;
4820 info_d
->next
= this->devs
;
4821 this->devs
= info_d
;
4823 info_d
->disk
.number
= d
->index
;
4824 info_d
->disk
.major
= d
->major
;
4825 info_d
->disk
.minor
= d
->minor
;
4826 info_d
->disk
.raid_disk
= slot
;
4827 info_d
->recovery_start
= recovery_start
;
4829 if (slot
< map2
->num_members
)
4830 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
4832 this->array
.spare_disks
++;
4834 if (slot
< map
->num_members
)
4835 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
4837 this->array
.spare_disks
++;
4839 if (info_d
->recovery_start
== MaxSector
)
4840 this->array
.working_disks
++;
4842 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
4843 info_d
->data_offset
= __le32_to_cpu(map
->pba_of_lba0
);
4844 info_d
->component_size
= __le32_to_cpu(map
->blocks_per_member
);
4846 /* now that the disk list is up-to-date fixup recovery_start */
4847 update_recovery_start(dev
, this);
4851 /* if array has bad blocks, set suitable bit in array status */
4853 rest
->array
.state
|= (1<<MD_SB_BBM_ERRORS
);
4859 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
, int failed
)
4861 struct imsm_map
*map
= get_imsm_map(dev
, 0);
4864 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
4865 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
4867 switch (get_imsm_raid_level(map
)) {
4869 return IMSM_T_STATE_FAILED
;
4872 if (failed
< map
->num_members
)
4873 return IMSM_T_STATE_DEGRADED
;
4875 return IMSM_T_STATE_FAILED
;
4880 * check to see if any mirrors have failed, otherwise we
4881 * are degraded. Even numbered slots are mirrored on
4885 /* gcc -Os complains that this is unused */
4886 int insync
= insync
;
4888 for (i
= 0; i
< map
->num_members
; i
++) {
4889 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, -1);
4890 int idx
= ord_to_idx(ord
);
4891 struct imsm_disk
*disk
;
4893 /* reset the potential in-sync count on even-numbered
4894 * slots. num_copies is always 2 for imsm raid10
4899 disk
= get_imsm_disk(super
, idx
);
4900 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
4903 /* no in-sync disks left in this mirror the
4907 return IMSM_T_STATE_FAILED
;
4910 return IMSM_T_STATE_DEGRADED
;
4914 return IMSM_T_STATE_DEGRADED
;
4916 return IMSM_T_STATE_FAILED
;
4922 return map
->map_state
;
4925 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
)
4929 struct imsm_disk
*disk
;
4930 struct imsm_map
*map
= get_imsm_map(dev
, 0);
4931 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
);
4935 /* at the beginning of migration we set IMSM_ORD_REBUILD on
4936 * disks that are being rebuilt. New failures are recorded to
4937 * map[0]. So we look through all the disks we started with and
4938 * see if any failures are still present, or if any new ones
4941 * FIXME add support for online capacity expansion and
4942 * raid-level-migration
4944 for (i
= 0; i
< prev
->num_members
; i
++) {
4945 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
4946 ord
|= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
4947 idx
= ord_to_idx(ord
);
4949 disk
= get_imsm_disk(super
, idx
);
4950 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
4958 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
4961 struct intel_super
*super
= c
->sb
;
4962 struct imsm_super
*mpb
= super
->anchor
;
4964 if (atoi(inst
) >= mpb
->num_raid_devs
) {
4965 fprintf(stderr
, "%s: subarry index %d, out of range\n",
4966 __func__
, atoi(inst
));
4970 dprintf("imsm: open_new %s\n", inst
);
4971 a
->info
.container_member
= atoi(inst
);
4975 static int is_resyncing(struct imsm_dev
*dev
)
4977 struct imsm_map
*migr_map
;
4979 if (!dev
->vol
.migr_state
)
4982 if (migr_type(dev
) == MIGR_INIT
||
4983 migr_type(dev
) == MIGR_REPAIR
)
4986 if (migr_type(dev
) == MIGR_GEN_MIGR
)
4989 migr_map
= get_imsm_map(dev
, 1);
4991 if ((migr_map
->map_state
== IMSM_T_STATE_NORMAL
) &&
4992 (dev
->vol
.migr_type
!= MIGR_GEN_MIGR
))
4998 /* return true if we recorded new information */
4999 static int mark_failure(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
5003 struct imsm_map
*map
;
5005 /* new failures are always set in map[0] */
5006 map
= get_imsm_map(dev
, 0);
5008 slot
= get_imsm_disk_slot(map
, idx
);
5012 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
5013 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
5016 disk
->status
|= FAILED_DISK
;
5017 disk
->status
&= ~CONFIGURED_DISK
;
5018 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
5019 if (map
->failed_disk_num
== 0xff)
5020 map
->failed_disk_num
= slot
;
5024 static void mark_missing(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
5026 mark_failure(dev
, disk
, idx
);
5028 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
5031 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
5032 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
5035 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
5041 if (!super
->missing
)
5043 failed
= imsm_count_failed(super
, dev
);
5044 map_state
= imsm_check_degraded(super
, dev
, failed
);
5046 dprintf("imsm: mark missing\n");
5047 end_migration(dev
, map_state
);
5048 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
5049 mark_missing(dev
, &dl
->disk
, dl
->index
);
5050 super
->updates_pending
++;
5053 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
5055 static void imsm_progress_container_reshape(struct intel_super
*super
)
5057 /* if no device has a migr_state, but some device has a
5058 * different number of members than the previous device, start
5059 * changing the number of devices in this device to match
5062 struct imsm_super
*mpb
= super
->anchor
;
5063 int prev_disks
= -1;
5066 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5067 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
5068 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5069 struct imsm_map
*map2
;
5070 int prev_num_members
;
5073 if (dev
->vol
.migr_state
)
5076 if (prev_disks
== -1)
5077 prev_disks
= map
->num_members
;
5078 if (prev_disks
== map
->num_members
)
5081 /* OK, this array needs to enter reshape mode.
5082 * i.e it needs a migr_state
5085 prev_num_members
= map
->num_members
;
5086 map
->num_members
= prev_disks
;
5087 dev
->vol
.migr_state
= 1;
5088 dev
->vol
.curr_migr_unit
= 0;
5089 dev
->vol
.migr_type
= MIGR_GEN_MIGR
;
5090 for (i
= prev_num_members
;
5091 i
< map
->num_members
; i
++)
5092 set_imsm_ord_tbl_ent(map
, i
, i
);
5093 map2
= get_imsm_map(dev
, 1);
5094 /* Copy the current map */
5095 memcpy(map2
, map
, sizeof_imsm_map(map
));
5096 map2
->num_members
= prev_num_members
;
5098 /* calculate new size
5100 used_disks
= imsm_num_data_members(dev
, 0);
5102 unsigned long long array_blocks
;
5105 map
->blocks_per_member
5107 /* round array size down to closest MB
5109 array_blocks
= (array_blocks
5110 >> SECT_PER_MB_SHIFT
)
5111 << SECT_PER_MB_SHIFT
;
5113 __cpu_to_le32((__u32
)array_blocks
);
5116 (__u32
)(array_blocks
>> 32));
5118 super
->updates_pending
++;
5122 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
5123 * states are handled in imsm_set_disk() with one exception, when a
5124 * resync is stopped due to a new failure this routine will set the
5125 * 'degraded' state for the array.
5127 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
5129 int inst
= a
->info
.container_member
;
5130 struct intel_super
*super
= a
->container
->sb
;
5131 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
5132 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5133 int failed
= imsm_count_failed(super
, dev
);
5134 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
);
5135 __u32 blocks_per_unit
;
5137 if (dev
->vol
.migr_state
&&
5138 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
5139 /* array state change is blocked due to reshape action
5141 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
5142 * - finish the reshape (if last_checkpoint is big and action != reshape)
5143 * - update curr_migr_unit
5145 if (a
->curr_action
== reshape
) {
5146 /* still reshaping, maybe update curr_migr_unit */
5147 long long blocks_per_unit
= blocks_per_migr_unit(dev
);
5148 long long unit
= a
->last_checkpoint
;
5149 if (blocks_per_unit
) {
5150 unit
/= blocks_per_unit
;
5152 __le32_to_cpu(dev
->vol
.curr_migr_unit
)) {
5153 dev
->vol
.curr_migr_unit
=
5154 __cpu_to_le32(unit
);
5155 super
->updates_pending
++;
5160 if (a
->last_checkpoint
== 0 && a
->prev_action
== reshape
) {
5161 /* for some reason we aborted the reshape.
5164 struct imsm_map
*map2
= get_imsm_map(dev
, 1);
5165 dev
->vol
.migr_state
= 0;
5166 dev
->vol
.migr_type
= 0;
5167 dev
->vol
.curr_migr_unit
= 0;
5168 memcpy(map
, map2
, sizeof_imsm_map(map2
));
5169 super
->updates_pending
++;
5171 if (a
->last_checkpoint
>= a
->info
.component_size
) {
5172 unsigned long long array_blocks
;
5174 /* it seems the reshape is all done */
5175 dev
->vol
.migr_state
= 0;
5176 dev
->vol
.migr_type
= 0;
5177 dev
->vol
.curr_migr_unit
= 0;
5179 used_disks
= imsm_num_data_members(dev
, -1);
5180 array_blocks
= map
->blocks_per_member
* used_disks
;
5181 /* round array size down to closest MB */
5182 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
)
5183 << SECT_PER_MB_SHIFT
;
5184 dev
->size_low
= __cpu_to_le32((__u32
) array_blocks
);
5185 dev
->size_high
= __cpu_to_le32((__u32
) (array_blocks
>> 32));
5186 a
->info
.custom_array_size
= array_blocks
;
5187 a
->check_reshape
= 1; /* encourage manager to update
5190 super
->updates_pending
++;
5191 imsm_progress_container_reshape(super
);
5196 /* before we activate this array handle any missing disks */
5197 if (consistent
== 2)
5198 handle_missing(super
, dev
);
5200 if (consistent
== 2 &&
5201 (!is_resync_complete(&a
->info
) ||
5202 map_state
!= IMSM_T_STATE_NORMAL
||
5203 dev
->vol
.migr_state
))
5206 if (is_resync_complete(&a
->info
)) {
5207 /* complete intialization / resync,
5208 * recovery and interrupted recovery is completed in
5211 if (is_resyncing(dev
)) {
5212 dprintf("imsm: mark resync done\n");
5213 end_migration(dev
, map_state
);
5214 super
->updates_pending
++;
5215 a
->last_checkpoint
= 0;
5217 } else if (!is_resyncing(dev
) && !failed
) {
5218 /* mark the start of the init process if nothing is failed */
5219 dprintf("imsm: mark resync start\n");
5220 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
5221 migrate(dev
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
5223 migrate(dev
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
5224 super
->updates_pending
++;
5227 /* check if we can update curr_migr_unit from resync_start, recovery_start */
5228 blocks_per_unit
= blocks_per_migr_unit(dev
);
5229 if (blocks_per_unit
) {
5233 units
= a
->last_checkpoint
/ blocks_per_unit
;
5236 /* check that we did not overflow 32-bits, and that
5237 * curr_migr_unit needs updating
5239 if (units32
== units
&&
5240 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
5241 dprintf("imsm: mark checkpoint (%u)\n", units32
);
5242 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
5243 super
->updates_pending
++;
5247 /* mark dirty / clean */
5248 if (dev
->vol
.dirty
!= !consistent
) {
5249 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
5254 super
->updates_pending
++;
5257 /* manage online capacity expansion/reshape */
5258 if ((a
->curr_action
!= reshape
) &&
5259 (a
->prev_action
== reshape
)) {
5262 /* finalize online capacity expansion/reshape */
5263 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
5264 imsm_set_disk(a
, mdi
->disk
.raid_disk
, mdi
->curr_state
);
5266 /* check next volume reshape */
5267 imsm_progress_container_reshape(super
);
5273 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
5275 int inst
= a
->info
.container_member
;
5276 struct intel_super
*super
= a
->container
->sb
;
5277 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
5278 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5279 struct imsm_disk
*disk
;
5284 if (n
> map
->num_members
)
5285 fprintf(stderr
, "imsm: set_disk %d out of range 0..%d\n",
5286 n
, map
->num_members
- 1);
5291 dprintf("imsm: set_disk %d:%x\n", n
, state
);
5293 ord
= get_imsm_ord_tbl_ent(dev
, n
, -1);
5294 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
5296 /* check for new failures */
5297 if (state
& DS_FAULTY
) {
5298 if (mark_failure(dev
, disk
, ord_to_idx(ord
)))
5299 super
->updates_pending
++;
5302 /* check if in_sync */
5303 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
5304 struct imsm_map
*migr_map
= get_imsm_map(dev
, 1);
5306 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
5307 super
->updates_pending
++;
5310 failed
= imsm_count_failed(super
, dev
);
5311 map_state
= imsm_check_degraded(super
, dev
, failed
);
5313 /* check if recovery complete, newly degraded, or failed */
5314 if (map_state
== IMSM_T_STATE_NORMAL
&& is_rebuilding(dev
)) {
5315 end_migration(dev
, map_state
);
5316 map
= get_imsm_map(dev
, 0);
5317 map
->failed_disk_num
= ~0;
5318 super
->updates_pending
++;
5319 a
->last_checkpoint
= 0;
5320 } else if (map_state
== IMSM_T_STATE_DEGRADED
&&
5321 map
->map_state
!= map_state
&&
5322 !dev
->vol
.migr_state
) {
5323 dprintf("imsm: mark degraded\n");
5324 map
->map_state
= map_state
;
5325 super
->updates_pending
++;
5326 a
->last_checkpoint
= 0;
5327 } else if (map_state
== IMSM_T_STATE_FAILED
&&
5328 map
->map_state
!= map_state
) {
5329 dprintf("imsm: mark failed\n");
5330 end_migration(dev
, map_state
);
5331 super
->updates_pending
++;
5332 a
->last_checkpoint
= 0;
5333 } else if (is_gen_migration(dev
)) {
5334 dprintf("imsm: Detected General Migration in state: ");
5335 if (map_state
== IMSM_T_STATE_NORMAL
) {
5336 end_migration(dev
, map_state
);
5337 map
= get_imsm_map(dev
, 0);
5338 map
->failed_disk_num
= ~0;
5339 dprintf("normal\n");
5341 if (map_state
== IMSM_T_STATE_DEGRADED
) {
5342 printf("degraded\n");
5343 end_migration(dev
, map_state
);
5345 dprintf("failed\n");
5347 map
->map_state
= map_state
;
5349 super
->updates_pending
++;
5353 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
5356 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
5357 unsigned long long dsize
;
5358 unsigned long long sectors
;
5360 get_dev_size(fd
, NULL
, &dsize
);
5362 if (mpb_size
> 512) {
5363 /* -1 to account for anchor */
5364 sectors
= mpb_sectors(mpb
) - 1;
5366 /* write the extended mpb to the sectors preceeding the anchor */
5367 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0)
5370 if ((unsigned long long)write(fd
, buf
+ 512, 512 * sectors
)
5375 /* first block is stored on second to last sector of the disk */
5376 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0)
5379 if (write(fd
, buf
, 512) != 512)
5385 static void imsm_sync_metadata(struct supertype
*container
)
5387 struct intel_super
*super
= container
->sb
;
5389 dprintf("sync metadata: %d\n", super
->updates_pending
);
5390 if (!super
->updates_pending
)
5393 write_super_imsm(container
, 0);
5395 super
->updates_pending
= 0;
5398 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
5400 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
5401 int i
= get_imsm_disk_idx(dev
, idx
, -1);
5404 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5408 if (dl
&& is_failed(&dl
->disk
))
5412 dprintf("%s: found %x:%x\n", __func__
, dl
->major
, dl
->minor
);
5417 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
5418 struct active_array
*a
, int activate_new
,
5419 struct mdinfo
*additional_test_list
)
5421 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
5422 int idx
= get_imsm_disk_idx(dev
, slot
, -1);
5423 struct imsm_super
*mpb
= super
->anchor
;
5424 struct imsm_map
*map
;
5425 unsigned long long pos
;
5430 __u32 array_start
= 0;
5431 __u32 array_end
= 0;
5433 struct mdinfo
*test_list
;
5435 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5436 /* If in this array, skip */
5437 for (d
= a
->info
.devs
; d
; d
= d
->next
)
5438 if (d
->state_fd
>= 0 &&
5439 d
->disk
.major
== dl
->major
&&
5440 d
->disk
.minor
== dl
->minor
) {
5441 dprintf("%x:%x already in array\n",
5442 dl
->major
, dl
->minor
);
5447 test_list
= additional_test_list
;
5449 if (test_list
->disk
.major
== dl
->major
&&
5450 test_list
->disk
.minor
== dl
->minor
) {
5451 dprintf("%x:%x already in additional test list\n",
5452 dl
->major
, dl
->minor
);
5455 test_list
= test_list
->next
;
5460 /* skip in use or failed drives */
5461 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
5463 dprintf("%x:%x status (failed: %d index: %d)\n",
5464 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
5468 /* skip pure spares when we are looking for partially
5469 * assimilated drives
5471 if (dl
->index
== -1 && !activate_new
)
5474 /* Does this unused device have the requisite free space?
5475 * It needs to be able to cover all member volumes
5477 ex
= get_extents(super
, dl
);
5479 dprintf("cannot get extents\n");
5482 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5483 dev
= get_imsm_dev(super
, i
);
5484 map
= get_imsm_map(dev
, 0);
5486 /* check if this disk is already a member of
5489 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
5495 array_start
= __le32_to_cpu(map
->pba_of_lba0
);
5496 array_end
= array_start
+
5497 __le32_to_cpu(map
->blocks_per_member
) - 1;
5500 /* check that we can start at pba_of_lba0 with
5501 * blocks_per_member of space
5503 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
5507 pos
= ex
[j
].start
+ ex
[j
].size
;
5509 } while (ex
[j
-1].size
);
5516 if (i
< mpb
->num_raid_devs
) {
5517 dprintf("%x:%x does not have %u to %u available\n",
5518 dl
->major
, dl
->minor
, array_start
, array_end
);
5529 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
5531 struct imsm_dev
*dev2
;
5532 struct imsm_map
*map
;
5538 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
5540 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
);
5541 if (state
== IMSM_T_STATE_FAILED
) {
5542 map
= get_imsm_map(dev2
, 0);
5545 for (slot
= 0; slot
< map
->num_members
; slot
++) {
5547 * Check if failed disks are deleted from intel
5548 * disk list or are marked to be deleted
5550 idx
= get_imsm_disk_idx(dev2
, slot
, -1);
5551 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
5553 * Do not rebuild the array if failed disks
5554 * from failed sub-array are not removed from
5558 is_failed(&idisk
->disk
) &&
5559 (idisk
->action
!= DISK_REMOVE
))
5567 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
5568 struct metadata_update
**updates
)
5571 * Find a device with unused free space and use it to replace a
5572 * failed/vacant region in an array. We replace failed regions one a
5573 * array at a time. The result is that a new spare disk will be added
5574 * to the first failed array and after the monitor has finished
5575 * propagating failures the remainder will be consumed.
5577 * FIXME add a capability for mdmon to request spares from another
5581 struct intel_super
*super
= a
->container
->sb
;
5582 int inst
= a
->info
.container_member
;
5583 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
5584 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5585 int failed
= a
->info
.array
.raid_disks
;
5586 struct mdinfo
*rv
= NULL
;
5589 struct metadata_update
*mu
;
5591 struct imsm_update_activate_spare
*u
;
5596 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
5597 if ((d
->curr_state
& DS_FAULTY
) &&
5599 /* wait for Removal to happen */
5601 if (d
->state_fd
>= 0)
5605 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
5606 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
5608 if (dev
->vol
.migr_state
&&
5609 dev
->vol
.migr_type
== MIGR_GEN_MIGR
)
5610 /* No repair during migration */
5613 if (a
->info
.array
.level
== 4)
5614 /* No repair for takeovered array
5615 * imsm doesn't support raid4
5619 if (imsm_check_degraded(super
, dev
, failed
) != IMSM_T_STATE_DEGRADED
)
5623 * If there are any failed disks check state of the other volume.
5624 * Block rebuild if the another one is failed until failed disks
5625 * are removed from container.
5628 dprintf("found failed disks in %s, check if there another"
5629 "failed sub-array.\n",
5631 /* check if states of the other volumes allow for rebuild */
5632 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
5634 allowed
= imsm_rebuild_allowed(a
->container
,
5642 /* For each slot, if it is not working, find a spare */
5643 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
5644 for (d
= a
->info
.devs
; d
; d
= d
->next
)
5645 if (d
->disk
.raid_disk
== i
)
5647 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
5648 if (d
&& (d
->state_fd
>= 0))
5652 * OK, this device needs recovery. Try to re-add the
5653 * previous occupant of this slot, if this fails see if
5654 * we can continue the assimilation of a spare that was
5655 * partially assimilated, finally try to activate a new
5658 dl
= imsm_readd(super
, i
, a
);
5660 dl
= imsm_add_spare(super
, i
, a
, 0, NULL
);
5662 dl
= imsm_add_spare(super
, i
, a
, 1, NULL
);
5666 /* found a usable disk with enough space */
5667 di
= malloc(sizeof(*di
));
5670 memset(di
, 0, sizeof(*di
));
5672 /* dl->index will be -1 in the case we are activating a
5673 * pristine spare. imsm_process_update() will create a
5674 * new index in this case. Once a disk is found to be
5675 * failed in all member arrays it is kicked from the
5678 di
->disk
.number
= dl
->index
;
5680 /* (ab)use di->devs to store a pointer to the device
5683 di
->devs
= (struct mdinfo
*) dl
;
5685 di
->disk
.raid_disk
= i
;
5686 di
->disk
.major
= dl
->major
;
5687 di
->disk
.minor
= dl
->minor
;
5689 di
->recovery_start
= 0;
5690 di
->data_offset
= __le32_to_cpu(map
->pba_of_lba0
);
5691 di
->component_size
= a
->info
.component_size
;
5692 di
->container_member
= inst
;
5693 super
->random
= random32();
5697 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
5698 i
, di
->data_offset
);
5704 /* No spares found */
5706 /* Now 'rv' has a list of devices to return.
5707 * Create a metadata_update record to update the
5708 * disk_ord_tbl for the array
5710 mu
= malloc(sizeof(*mu
));
5712 mu
->buf
= malloc(sizeof(struct imsm_update_activate_spare
) * num_spares
);
5713 if (mu
->buf
== NULL
) {
5720 struct mdinfo
*n
= rv
->next
;
5729 mu
->space_list
= NULL
;
5730 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
5731 mu
->next
= *updates
;
5732 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
5734 for (di
= rv
; di
; di
= di
->next
) {
5735 u
->type
= update_activate_spare
;
5736 u
->dl
= (struct dl
*) di
->devs
;
5738 u
->slot
= di
->disk
.raid_disk
;
5749 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
5751 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
5752 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5753 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, 0);
5754 struct disk_info
*inf
= get_disk_info(u
);
5755 struct imsm_disk
*disk
;
5759 for (i
= 0; i
< map
->num_members
; i
++) {
5760 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, -1));
5761 for (j
= 0; j
< new_map
->num_members
; j
++)
5762 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
5770 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
5772 struct dl
*dl
= NULL
;
5773 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5774 if ((dl
->major
== major
) && (dl
->minor
== minor
))
5779 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
5781 struct dl
*prev
= NULL
;
5785 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5786 if ((dl
->major
== major
) && (dl
->minor
== minor
)) {
5789 prev
->next
= dl
->next
;
5791 super
->disks
= dl
->next
;
5793 __free_imsm_disk(dl
);
5794 dprintf("%s: removed %x:%x\n",
5795 __func__
, major
, minor
);
5803 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
5805 static int add_remove_disk_update(struct intel_super
*super
)
5807 int check_degraded
= 0;
5808 struct dl
*disk
= NULL
;
5809 /* add/remove some spares to/from the metadata/contrainer */
5810 while (super
->disk_mgmt_list
) {
5811 struct dl
*disk_cfg
;
5813 disk_cfg
= super
->disk_mgmt_list
;
5814 super
->disk_mgmt_list
= disk_cfg
->next
;
5815 disk_cfg
->next
= NULL
;
5817 if (disk_cfg
->action
== DISK_ADD
) {
5818 disk_cfg
->next
= super
->disks
;
5819 super
->disks
= disk_cfg
;
5821 dprintf("%s: added %x:%x\n",
5822 __func__
, disk_cfg
->major
,
5824 } else if (disk_cfg
->action
== DISK_REMOVE
) {
5825 dprintf("Disk remove action processed: %x.%x\n",
5826 disk_cfg
->major
, disk_cfg
->minor
);
5827 disk
= get_disk_super(super
,
5831 /* store action status */
5832 disk
->action
= DISK_REMOVE
;
5833 /* remove spare disks only */
5834 if (disk
->index
== -1) {
5835 remove_disk_super(super
,
5840 /* release allocate disk structure */
5841 __free_imsm_disk(disk_cfg
);
5844 return check_degraded
;
5847 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
5848 struct intel_super
*super
,
5851 struct dl
*new_disk
;
5852 struct intel_dev
*id
;
5854 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
5855 int disk_count
= u
->old_raid_disks
;
5856 void **tofree
= NULL
;
5857 int devices_to_reshape
= 1;
5858 struct imsm_super
*mpb
= super
->anchor
;
5860 unsigned int dev_id
;
5862 dprintf("imsm: imsm_process_update() for update_reshape\n");
5864 /* enable spares to use in array */
5865 for (i
= 0; i
< delta_disks
; i
++) {
5866 new_disk
= get_disk_super(super
,
5867 major(u
->new_disks
[i
]),
5868 minor(u
->new_disks
[i
]));
5869 dprintf("imsm: imsm_process_update(): new disk "
5870 "for reshape is: %i:%i (%p, index = %i)\n",
5871 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
5872 new_disk
, new_disk
->index
);
5873 if ((new_disk
== NULL
) ||
5874 ((new_disk
->index
>= 0) &&
5875 (new_disk
->index
< u
->old_raid_disks
)))
5876 goto update_reshape_exit
;
5877 new_disk
->index
= disk_count
++;
5878 /* slot to fill in autolayout
5880 new_disk
->raiddisk
= new_disk
->index
;
5881 new_disk
->disk
.status
|=
5883 new_disk
->disk
.status
&= ~SPARE_DISK
;
5886 dprintf("imsm: process_update(): update_reshape: volume set"
5887 " mpb->num_raid_devs = %i\n", mpb
->num_raid_devs
);
5888 /* manage changes in volume
5890 for (dev_id
= 0; dev_id
< mpb
->num_raid_devs
; dev_id
++) {
5891 void **sp
= *space_list
;
5892 struct imsm_dev
*newdev
;
5893 struct imsm_map
*newmap
, *oldmap
;
5895 for (id
= super
->devlist
; id
; id
= id
->next
) {
5896 if (id
->index
== dev_id
)
5905 /* Copy the dev, but not (all of) the map */
5906 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
5907 oldmap
= get_imsm_map(id
->dev
, 0);
5908 newmap
= get_imsm_map(newdev
, 0);
5909 /* Copy the current map */
5910 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
5911 /* update one device only
5913 if (devices_to_reshape
) {
5916 dprintf("process_update(): modifying "
5917 "subdev: %i\n", id
->index
);
5918 devices_to_reshape
--;
5919 newdev
->vol
.migr_state
= 1;
5920 newdev
->vol
.curr_migr_unit
= 0;
5921 newdev
->vol
.migr_type
= MIGR_GEN_MIGR
;
5922 newmap
->num_members
= u
->new_raid_disks
;
5923 for (i
= 0; i
< delta_disks
; i
++) {
5924 set_imsm_ord_tbl_ent(newmap
,
5925 u
->old_raid_disks
+ i
,
5926 u
->old_raid_disks
+ i
);
5928 /* New map is correct, now need to save old map
5930 newmap
= get_imsm_map(newdev
, 1);
5931 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
5933 /* calculate new size
5935 used_disks
= imsm_num_data_members(newdev
, 0);
5937 unsigned long long array_blocks
;
5940 newmap
->blocks_per_member
* used_disks
;
5941 /* round array size down to closest MB
5943 array_blocks
= (array_blocks
5944 >> SECT_PER_MB_SHIFT
)
5945 << SECT_PER_MB_SHIFT
;
5947 __cpu_to_le32((__u32
)array_blocks
);
5949 __cpu_to_le32((__u32
)(array_blocks
>> 32));
5953 sp
= (void **)id
->dev
;
5959 *space_list
= tofree
;
5962 update_reshape_exit
:
5967 static int apply_takeover_update(struct imsm_update_takeover
*u
,
5968 struct intel_super
*super
,
5971 struct imsm_dev
*dev
= NULL
;
5972 struct intel_dev
*dv
;
5973 struct imsm_dev
*dev_new
;
5974 struct imsm_map
*map
;
5978 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
5979 if (dv
->index
== (unsigned int)u
->subarray
) {
5987 map
= get_imsm_map(dev
, 0);
5989 if (u
->direction
== R10_TO_R0
) {
5990 /* Number of failed disks must be half of initial disk number */
5991 if (imsm_count_failed(super
, dev
) != (map
->num_members
/ 2))
5994 /* iterate through devices to mark removed disks as spare */
5995 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
5996 if (dm
->disk
.status
& FAILED_DISK
) {
5997 int idx
= dm
->index
;
5998 /* update indexes on the disk list */
5999 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
6000 the index values will end up being correct.... NB */
6001 for (du
= super
->disks
; du
; du
= du
->next
)
6002 if (du
->index
> idx
)
6004 /* mark as spare disk */
6005 dm
->disk
.status
= SPARE_DISK
;
6010 map
->num_members
= map
->num_members
/ 2;
6011 map
->map_state
= IMSM_T_STATE_NORMAL
;
6012 map
->num_domains
= 1;
6013 map
->raid_level
= 0;
6014 map
->failed_disk_num
= -1;
6017 if (u
->direction
== R0_TO_R10
) {
6019 /* update slots in current disk list */
6020 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
6024 /* create new *missing* disks */
6025 for (i
= 0; i
< map
->num_members
; i
++) {
6026 space
= *space_list
;
6029 *space_list
= *space
;
6031 memcpy(du
, super
->disks
, sizeof(*du
));
6032 du
->disk
.status
= FAILED_DISK
;
6033 du
->disk
.scsi_id
= 0;
6037 du
->index
= (i
* 2) + 1;
6038 sprintf((char *)du
->disk
.serial
,
6039 " MISSING_%d", du
->index
);
6040 sprintf((char *)du
->serial
,
6041 "MISSING_%d", du
->index
);
6042 du
->next
= super
->missing
;
6043 super
->missing
= du
;
6045 /* create new dev and map */
6046 space
= *space_list
;
6049 *space_list
= *space
;
6050 dev_new
= (void *)space
;
6051 memcpy(dev_new
, dev
, sizeof(*dev
));
6052 /* update new map */
6053 map
= get_imsm_map(dev_new
, 0);
6054 map
->failed_disk_num
= map
->num_members
;
6055 map
->num_members
= map
->num_members
* 2;
6056 map
->map_state
= IMSM_T_STATE_NORMAL
;
6057 map
->num_domains
= 2;
6058 map
->raid_level
= 1;
6059 /* replace dev<->dev_new */
6062 /* update disk order table */
6063 for (du
= super
->disks
; du
; du
= du
->next
)
6065 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
6066 for (du
= super
->missing
; du
; du
= du
->next
)
6068 set_imsm_ord_tbl_ent(map
, du
->index
,
6069 du
->index
| IMSM_ORD_REBUILD
);
6074 static void imsm_process_update(struct supertype
*st
,
6075 struct metadata_update
*update
)
6078 * crack open the metadata_update envelope to find the update record
6079 * update can be one of:
6080 * update_reshape_container_disks - all the arrays in the container
6081 * are being reshaped to have more devices. We need to mark
6082 * the arrays for general migration and convert selected spares
6083 * into active devices.
6084 * update_activate_spare - a spare device has replaced a failed
6085 * device in an array, update the disk_ord_tbl. If this disk is
6086 * present in all member arrays then also clear the SPARE_DISK
6088 * update_create_array
6090 * update_rename_array
6091 * update_add_remove_disk
6093 struct intel_super
*super
= st
->sb
;
6094 struct imsm_super
*mpb
;
6095 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
6097 /* update requires a larger buf but the allocation failed */
6098 if (super
->next_len
&& !super
->next_buf
) {
6099 super
->next_len
= 0;
6103 if (super
->next_buf
) {
6104 memcpy(super
->next_buf
, super
->buf
, super
->len
);
6106 super
->len
= super
->next_len
;
6107 super
->buf
= super
->next_buf
;
6109 super
->next_len
= 0;
6110 super
->next_buf
= NULL
;
6113 mpb
= super
->anchor
;
6116 case update_takeover
: {
6117 struct imsm_update_takeover
*u
= (void *)update
->buf
;
6118 if (apply_takeover_update(u
, super
, &update
->space_list
))
6119 super
->updates_pending
++;
6123 case update_reshape_container_disks
: {
6124 struct imsm_update_reshape
*u
= (void *)update
->buf
;
6125 if (apply_reshape_container_disks_update(
6126 u
, super
, &update
->space_list
))
6127 super
->updates_pending
++;
6130 case update_activate_spare
: {
6131 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
6132 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
6133 struct imsm_map
*map
= get_imsm_map(dev
, 0);
6134 struct imsm_map
*migr_map
;
6135 struct active_array
*a
;
6136 struct imsm_disk
*disk
;
6141 int victim
= get_imsm_disk_idx(dev
, u
->slot
, -1);
6144 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6149 fprintf(stderr
, "error: imsm_activate_spare passed "
6150 "an unknown disk (index: %d)\n",
6155 super
->updates_pending
++;
6157 /* count failures (excluding rebuilds and the victim)
6158 * to determine map[0] state
6161 for (i
= 0; i
< map
->num_members
; i
++) {
6164 disk
= get_imsm_disk(super
,
6165 get_imsm_disk_idx(dev
, i
, -1));
6166 if (!disk
|| is_failed(disk
))
6170 /* adding a pristine spare, assign a new index */
6171 if (dl
->index
< 0) {
6172 dl
->index
= super
->anchor
->num_disks
;
6173 super
->anchor
->num_disks
++;
6176 disk
->status
|= CONFIGURED_DISK
;
6177 disk
->status
&= ~SPARE_DISK
;
6180 to_state
= imsm_check_degraded(super
, dev
, failed
);
6181 map
->map_state
= IMSM_T_STATE_DEGRADED
;
6182 migrate(dev
, to_state
, MIGR_REBUILD
);
6183 migr_map
= get_imsm_map(dev
, 1);
6184 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
6185 set_imsm_ord_tbl_ent(migr_map
, u
->slot
, dl
->index
| IMSM_ORD_REBUILD
);
6187 /* update the family_num to mark a new container
6188 * generation, being careful to record the existing
6189 * family_num in orig_family_num to clean up after
6190 * earlier mdadm versions that neglected to set it.
6192 if (mpb
->orig_family_num
== 0)
6193 mpb
->orig_family_num
= mpb
->family_num
;
6194 mpb
->family_num
+= super
->random
;
6196 /* count arrays using the victim in the metadata */
6198 for (a
= st
->arrays
; a
; a
= a
->next
) {
6199 dev
= get_imsm_dev(super
, a
->info
.container_member
);
6200 map
= get_imsm_map(dev
, 0);
6202 if (get_imsm_disk_slot(map
, victim
) >= 0)
6206 /* delete the victim if it is no longer being
6212 /* We know that 'manager' isn't touching anything,
6213 * so it is safe to delete
6215 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
6216 if ((*dlp
)->index
== victim
)
6219 /* victim may be on the missing list */
6221 for (dlp
= &super
->missing
; *dlp
; dlp
= &(*dlp
)->next
)
6222 if ((*dlp
)->index
== victim
)
6224 imsm_delete(super
, dlp
, victim
);
6228 case update_create_array
: {
6229 /* someone wants to create a new array, we need to be aware of
6230 * a few races/collisions:
6231 * 1/ 'Create' called by two separate instances of mdadm
6232 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
6233 * devices that have since been assimilated via
6235 * In the event this update can not be carried out mdadm will
6236 * (FIX ME) notice that its update did not take hold.
6238 struct imsm_update_create_array
*u
= (void *) update
->buf
;
6239 struct intel_dev
*dv
;
6240 struct imsm_dev
*dev
;
6241 struct imsm_map
*map
, *new_map
;
6242 unsigned long long start
, end
;
6243 unsigned long long new_start
, new_end
;
6245 struct disk_info
*inf
;
6248 /* handle racing creates: first come first serve */
6249 if (u
->dev_idx
< mpb
->num_raid_devs
) {
6250 dprintf("%s: subarray %d already defined\n",
6251 __func__
, u
->dev_idx
);
6255 /* check update is next in sequence */
6256 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
6257 dprintf("%s: can not create array %d expected index %d\n",
6258 __func__
, u
->dev_idx
, mpb
->num_raid_devs
);
6262 new_map
= get_imsm_map(&u
->dev
, 0);
6263 new_start
= __le32_to_cpu(new_map
->pba_of_lba0
);
6264 new_end
= new_start
+ __le32_to_cpu(new_map
->blocks_per_member
);
6265 inf
= get_disk_info(u
);
6267 /* handle activate_spare versus create race:
6268 * check to make sure that overlapping arrays do not include
6271 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6272 dev
= get_imsm_dev(super
, i
);
6273 map
= get_imsm_map(dev
, 0);
6274 start
= __le32_to_cpu(map
->pba_of_lba0
);
6275 end
= start
+ __le32_to_cpu(map
->blocks_per_member
);
6276 if ((new_start
>= start
&& new_start
<= end
) ||
6277 (start
>= new_start
&& start
<= new_end
))
6282 if (disks_overlap(super
, i
, u
)) {
6283 dprintf("%s: arrays overlap\n", __func__
);
6288 /* check that prepare update was successful */
6289 if (!update
->space
) {
6290 dprintf("%s: prepare update failed\n", __func__
);
6294 /* check that all disks are still active before committing
6295 * changes. FIXME: could we instead handle this by creating a
6296 * degraded array? That's probably not what the user expects,
6297 * so better to drop this update on the floor.
6299 for (i
= 0; i
< new_map
->num_members
; i
++) {
6300 dl
= serial_to_dl(inf
[i
].serial
, super
);
6302 dprintf("%s: disk disappeared\n", __func__
);
6307 super
->updates_pending
++;
6309 /* convert spares to members and fixup ord_tbl */
6310 for (i
= 0; i
< new_map
->num_members
; i
++) {
6311 dl
= serial_to_dl(inf
[i
].serial
, super
);
6312 if (dl
->index
== -1) {
6313 dl
->index
= mpb
->num_disks
;
6315 dl
->disk
.status
|= CONFIGURED_DISK
;
6316 dl
->disk
.status
&= ~SPARE_DISK
;
6318 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
6323 update
->space
= NULL
;
6324 imsm_copy_dev(dev
, &u
->dev
);
6325 dv
->index
= u
->dev_idx
;
6326 dv
->next
= super
->devlist
;
6327 super
->devlist
= dv
;
6328 mpb
->num_raid_devs
++;
6330 imsm_update_version_info(super
);
6333 /* mdmon knows how to release update->space, but not
6334 * ((struct intel_dev *) update->space)->dev
6336 if (update
->space
) {
6342 case update_kill_array
: {
6343 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
6344 int victim
= u
->dev_idx
;
6345 struct active_array
*a
;
6346 struct intel_dev
**dp
;
6347 struct imsm_dev
*dev
;
6349 /* sanity check that we are not affecting the uuid of
6350 * active arrays, or deleting an active array
6352 * FIXME when immutable ids are available, but note that
6353 * we'll also need to fixup the invalidated/active
6354 * subarray indexes in mdstat
6356 for (a
= st
->arrays
; a
; a
= a
->next
)
6357 if (a
->info
.container_member
>= victim
)
6359 /* by definition if mdmon is running at least one array
6360 * is active in the container, so checking
6361 * mpb->num_raid_devs is just extra paranoia
6363 dev
= get_imsm_dev(super
, victim
);
6364 if (a
|| !dev
|| mpb
->num_raid_devs
== 1) {
6365 dprintf("failed to delete subarray-%d\n", victim
);
6369 for (dp
= &super
->devlist
; *dp
;)
6370 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
6373 if ((*dp
)->index
> (unsigned)victim
)
6377 mpb
->num_raid_devs
--;
6378 super
->updates_pending
++;
6381 case update_rename_array
: {
6382 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
6383 char name
[MAX_RAID_SERIAL_LEN
+1];
6384 int target
= u
->dev_idx
;
6385 struct active_array
*a
;
6386 struct imsm_dev
*dev
;
6388 /* sanity check that we are not affecting the uuid of
6391 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
6392 name
[MAX_RAID_SERIAL_LEN
] = '\0';
6393 for (a
= st
->arrays
; a
; a
= a
->next
)
6394 if (a
->info
.container_member
== target
)
6396 dev
= get_imsm_dev(super
, u
->dev_idx
);
6397 if (a
|| !dev
|| !check_name(super
, name
, 1)) {
6398 dprintf("failed to rename subarray-%d\n", target
);
6402 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6403 super
->updates_pending
++;
6406 case update_add_remove_disk
: {
6407 /* we may be able to repair some arrays if disks are
6408 * being added, check teh status of add_remove_disk
6409 * if discs has been added.
6411 if (add_remove_disk_update(super
)) {
6412 struct active_array
*a
;
6414 super
->updates_pending
++;
6415 for (a
= st
->arrays
; a
; a
= a
->next
)
6416 a
->check_degraded
= 1;
6421 fprintf(stderr
, "error: unsuported process update type:"
6422 "(type: %d)\n", type
);
6426 static void imsm_prepare_update(struct supertype
*st
,
6427 struct metadata_update
*update
)
6430 * Allocate space to hold new disk entries, raid-device entries or a new
6431 * mpb if necessary. The manager synchronously waits for updates to
6432 * complete in the monitor, so new mpb buffers allocated here can be
6433 * integrated by the monitor thread without worrying about live pointers
6434 * in the manager thread.
6436 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
6437 struct intel_super
*super
= st
->sb
;
6438 struct imsm_super
*mpb
= super
->anchor
;
6443 case update_takeover
: {
6444 struct imsm_update_takeover
*u
= (void *)update
->buf
;
6445 if (u
->direction
== R0_TO_R10
) {
6446 void **tail
= (void **)&update
->space_list
;
6447 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subarray
);
6448 struct imsm_map
*map
= get_imsm_map(dev
, 0);
6449 int num_members
= map
->num_members
;
6453 /* allocate memory for added disks */
6454 for (i
= 0; i
< num_members
; i
++) {
6455 size
= sizeof(struct dl
);
6456 space
= malloc(size
);
6465 /* allocate memory for new device */
6466 size
= sizeof_imsm_dev(super
->devlist
->dev
, 0) +
6467 (num_members
* sizeof(__u32
));
6468 space
= malloc(size
);
6477 len
= disks_to_mpb_size(num_members
* 2);
6479 /* if allocation didn't success, free buffer */
6480 while (update
->space_list
) {
6481 void **sp
= update
->space_list
;
6482 update
->space_list
= *sp
;
6490 case update_reshape_container_disks
: {
6491 /* Every raid device in the container is about to
6492 * gain some more devices, and we will enter a
6494 * So each 'imsm_map' will be bigger, and the imsm_vol
6495 * will now hold 2 of them.
6496 * Thus we need new 'struct imsm_dev' allocations sized
6497 * as sizeof_imsm_dev but with more devices in both maps.
6499 struct imsm_update_reshape
*u
= (void *)update
->buf
;
6500 struct intel_dev
*dl
;
6501 void **space_tail
= (void**)&update
->space_list
;
6503 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
6505 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
6506 int size
= sizeof_imsm_dev(dl
->dev
, 1);
6508 if (u
->new_raid_disks
> u
->old_raid_disks
)
6509 size
+= sizeof(__u32
)*2*
6510 (u
->new_raid_disks
- u
->old_raid_disks
);
6519 len
= disks_to_mpb_size(u
->new_raid_disks
);
6520 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
6523 case update_create_array
: {
6524 struct imsm_update_create_array
*u
= (void *) update
->buf
;
6525 struct intel_dev
*dv
;
6526 struct imsm_dev
*dev
= &u
->dev
;
6527 struct imsm_map
*map
= get_imsm_map(dev
, 0);
6529 struct disk_info
*inf
;
6533 inf
= get_disk_info(u
);
6534 len
= sizeof_imsm_dev(dev
, 1);
6535 /* allocate a new super->devlist entry */
6536 dv
= malloc(sizeof(*dv
));
6538 dv
->dev
= malloc(len
);
6543 update
->space
= NULL
;
6547 /* count how many spares will be converted to members */
6548 for (i
= 0; i
< map
->num_members
; i
++) {
6549 dl
= serial_to_dl(inf
[i
].serial
, super
);
6551 /* hmm maybe it failed?, nothing we can do about
6556 if (count_memberships(dl
, super
) == 0)
6559 len
+= activate
* sizeof(struct imsm_disk
);
6566 /* check if we need a larger metadata buffer */
6567 if (super
->next_buf
)
6568 buf_len
= super
->next_len
;
6570 buf_len
= super
->len
;
6572 if (__le32_to_cpu(mpb
->mpb_size
) + len
> buf_len
) {
6573 /* ok we need a larger buf than what is currently allocated
6574 * if this allocation fails process_update will notice that
6575 * ->next_len is set and ->next_buf is NULL
6577 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + len
, 512);
6578 if (super
->next_buf
)
6579 free(super
->next_buf
);
6581 super
->next_len
= buf_len
;
6582 if (posix_memalign(&super
->next_buf
, 512, buf_len
) == 0)
6583 memset(super
->next_buf
, 0, buf_len
);
6585 super
->next_buf
= NULL
;
6589 /* must be called while manager is quiesced */
6590 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
6592 struct imsm_super
*mpb
= super
->anchor
;
6594 struct imsm_dev
*dev
;
6595 struct imsm_map
*map
;
6596 int i
, j
, num_members
;
6599 dprintf("%s: deleting device[%d] from imsm_super\n",
6602 /* shift all indexes down one */
6603 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
6604 if (iter
->index
> (int)index
)
6606 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
6607 if (iter
->index
> (int)index
)
6610 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6611 dev
= get_imsm_dev(super
, i
);
6612 map
= get_imsm_map(dev
, 0);
6613 num_members
= map
->num_members
;
6614 for (j
= 0; j
< num_members
; j
++) {
6615 /* update ord entries being careful not to propagate
6616 * ord-flags to the first map
6618 ord
= get_imsm_ord_tbl_ent(dev
, j
, -1);
6620 if (ord_to_idx(ord
) <= index
)
6623 map
= get_imsm_map(dev
, 0);
6624 set_imsm_ord_tbl_ent(map
, j
, ord_to_idx(ord
- 1));
6625 map
= get_imsm_map(dev
, 1);
6627 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
6632 super
->updates_pending
++;
6634 struct dl
*dl
= *dlp
;
6636 *dlp
= (*dlp
)->next
;
6637 __free_imsm_disk(dl
);
6640 #endif /* MDASSEMBLE */
6642 static char disk_by_path
[] = "/dev/disk/by-path/";
6644 static const char *imsm_get_disk_controller_domain(const char *path
)
6646 char disk_path
[PATH_MAX
];
6650 strncpy(disk_path
, disk_by_path
, PATH_MAX
- 1);
6651 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
6652 if (stat(disk_path
, &st
) == 0) {
6653 struct sys_dev
* hba
;
6656 path
= devt_to_devpath(st
.st_rdev
);
6659 hba
= find_disk_attached_hba(-1, path
);
6660 if (hba
&& hba
->type
== SYS_DEV_SAS
)
6662 else if (hba
&& hba
->type
== SYS_DEV_SATA
)
6666 dprintf("path: %s hba: %s attached: %s\n",
6667 path
, (hba
) ? hba
->path
: "NULL", drv
);
6675 static int imsm_find_array_minor_by_subdev(int subdev
, int container
, int *minor
)
6677 char subdev_name
[20];
6678 struct mdstat_ent
*mdstat
;
6680 sprintf(subdev_name
, "%d", subdev
);
6681 mdstat
= mdstat_by_subdev(subdev_name
, container
);
6685 *minor
= mdstat
->devnum
;
6686 free_mdstat(mdstat
);
6690 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
6691 struct geo_params
*geo
,
6692 int *old_raid_disks
)
6694 /* currently we only support increasing the number of devices
6695 * for a container. This increases the number of device for each
6696 * member array. They must all be RAID0 or RAID5.
6699 struct mdinfo
*info
, *member
;
6700 int devices_that_can_grow
= 0;
6702 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): "
6703 "st->devnum = (%i)\n",
6706 if (geo
->size
!= -1 ||
6707 geo
->level
!= UnSet
||
6708 geo
->layout
!= UnSet
||
6709 geo
->chunksize
!= 0 ||
6710 geo
->raid_disks
== UnSet
) {
6711 dprintf("imsm: Container operation is allowed for "
6712 "raid disks number change only.\n");
6716 info
= container_content_imsm(st
, NULL
);
6717 for (member
= info
; member
; member
= member
->next
) {
6721 dprintf("imsm: checking device_num: %i\n",
6722 member
->container_member
);
6724 if (geo
->raid_disks
<= member
->array
.raid_disks
) {
6725 /* we work on container for Online Capacity Expansion
6726 * only so raid_disks has to grow
6728 dprintf("imsm: for container operation raid disks "
6729 "increase is required\n");
6733 if ((info
->array
.level
!= 0) &&
6734 (info
->array
.level
!= 5)) {
6735 /* we cannot use this container with other raid level
6737 dprintf("imsm: for container operation wrong"
6738 " raid level (%i) detected\n",
6742 /* check for platform support
6743 * for this raid level configuration
6745 struct intel_super
*super
= st
->sb
;
6746 if (!is_raid_level_supported(super
->orom
,
6747 member
->array
.level
,
6749 dprintf("platform does not support raid%d with"
6753 geo
->raid_disks
> 1 ? "s" : "");
6758 if (*old_raid_disks
&&
6759 info
->array
.raid_disks
!= *old_raid_disks
)
6761 *old_raid_disks
= info
->array
.raid_disks
;
6763 /* All raid5 and raid0 volumes in container
6764 * have to be ready for Online Capacity Expansion
6765 * so they need to be assembled. We have already
6766 * checked that no recovery etc is happening.
6768 result
= imsm_find_array_minor_by_subdev(member
->container_member
,
6772 dprintf("imsm: cannot find array\n");
6775 devices_that_can_grow
++;
6778 if (!member
&& devices_that_can_grow
)
6782 dprintf("\tContainer operation allowed\n");
6784 dprintf("\tError: %i\n", ret_val
);
6789 /* Function: get_spares_for_grow
6790 * Description: Allocates memory and creates list of spare devices
6791 * avaliable in container. Checks if spare drive size is acceptable.
6792 * Parameters: Pointer to the supertype structure
6793 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
6796 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
6798 unsigned long long min_size
= min_acceptable_spare_size_imsm(st
);
6799 return container_choose_spares(st
, min_size
, NULL
, NULL
, NULL
, 0);
6802 /******************************************************************************
6803 * function: imsm_create_metadata_update_for_reshape
6804 * Function creates update for whole IMSM container.
6806 ******************************************************************************/
6807 static int imsm_create_metadata_update_for_reshape(
6808 struct supertype
*st
,
6809 struct geo_params
*geo
,
6811 struct imsm_update_reshape
**updatep
)
6813 struct intel_super
*super
= st
->sb
;
6814 struct imsm_super
*mpb
= super
->anchor
;
6815 int update_memory_size
= 0;
6816 struct imsm_update_reshape
*u
= NULL
;
6817 struct mdinfo
*spares
= NULL
;
6819 int delta_disks
= 0;
6822 dprintf("imsm_update_metadata_for_reshape(enter) raid_disks = %i\n",
6825 delta_disks
= geo
->raid_disks
- old_raid_disks
;
6827 /* size of all update data without anchor */
6828 update_memory_size
= sizeof(struct imsm_update_reshape
);
6830 /* now add space for spare disks that we need to add. */
6831 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
6833 u
= calloc(1, update_memory_size
);
6836 "cannot get memory for imsm_update_reshape update\n");
6839 u
->type
= update_reshape_container_disks
;
6840 u
->old_raid_disks
= old_raid_disks
;
6841 u
->new_raid_disks
= geo
->raid_disks
;
6843 /* now get spare disks list
6845 spares
= get_spares_for_grow(st
);
6848 || delta_disks
> spares
->array
.spare_disks
) {
6849 dprintf("imsm: ERROR: Cannot get spare devices.\n");
6853 /* we have got spares
6854 * update disk list in imsm_disk list table in anchor
6856 dprintf("imsm: %i spares are available.\n\n",
6857 spares
->array
.spare_disks
);
6860 for (i
= 0; i
< delta_disks
; i
++) {
6865 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
6867 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
6868 dl
->index
= mpb
->num_disks
;
6878 dprintf("imsm: reshape update preparation :");
6879 if (i
== delta_disks
) {
6882 return update_memory_size
;
6885 dprintf(" Error\n");
6890 static void imsm_update_metadata_locally(struct supertype
*st
,
6893 struct metadata_update mu
;
6898 mu
.space_list
= NULL
;
6900 imsm_prepare_update(st
, &mu
);
6901 imsm_process_update(st
, &mu
);
6903 while (mu
.space_list
) {
6904 void **space
= mu
.space_list
;
6905 mu
.space_list
= *space
;
6910 /***************************************************************************
6911 * Function: imsm_analyze_change
6912 * Description: Function analyze change for single volume
6913 * and validate if transition is supported
6914 * Parameters: Geometry parameters, supertype structure
6915 * Returns: Operation type code on success, -1 if fail
6916 ****************************************************************************/
6917 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
6918 struct geo_params
*geo
)
6924 getinfo_super_imsm_volume(st
, &info
, NULL
);
6926 if ((geo
->level
!= info
.array
.level
) &&
6927 (geo
->level
>= 0) &&
6928 (geo
->level
!= UnSet
)) {
6929 switch (info
.array
.level
) {
6931 if (geo
->level
== 5) {
6932 change
= CH_LEVEL_MIGRATION
;
6935 if (geo
->level
== 10) {
6936 change
= CH_TAKEOVER
;
6941 if (geo
->level
== 0) {
6942 change
= CH_TAKEOVER
;
6947 if (geo
->level
!= 0)
6948 change
= CH_LEVEL_MIGRATION
;
6951 if (geo
->level
== 0) {
6952 change
= CH_TAKEOVER
;
6959 Name
" Error. Level Migration from %d to %d "
6961 info
.array
.level
, geo
->level
);
6962 goto analyse_change_exit
;
6965 geo
->level
= info
.array
.level
;
6967 if ((geo
->layout
!= info
.array
.layout
)
6968 && ((geo
->layout
!= UnSet
) && (geo
->layout
!= -1))) {
6969 change
= CH_LEVEL_MIGRATION
;
6970 if ((info
.array
.layout
== 0)
6971 && (info
.array
.level
== 5)
6972 && (geo
->layout
== 5)) {
6973 /* reshape 5 -> 4 */
6974 } else if ((info
.array
.layout
== 5)
6975 && (info
.array
.level
== 5)
6976 && (geo
->layout
== 0)) {
6977 /* reshape 4 -> 5 */
6982 Name
" Error. Layout Migration from %d to %d "
6984 info
.array
.layout
, geo
->layout
);
6986 goto analyse_change_exit
;
6989 geo
->layout
= info
.array
.layout
;
6991 if ((geo
->chunksize
> 0) && (geo
->chunksize
!= UnSet
)
6992 && (geo
->chunksize
!= info
.array
.chunk_size
))
6993 change
= CH_CHUNK_MIGR
;
6995 geo
->chunksize
= info
.array
.chunk_size
;
6997 if (!validate_geometry_imsm(st
,
7001 (geo
->chunksize
/ 1024),
7007 struct intel_super
*super
= st
->sb
;
7008 struct imsm_super
*mpb
= super
->anchor
;
7010 if (mpb
->num_raid_devs
> 1) {
7012 Name
" Error. Cannot perform operation on %s"
7013 "- for this operation it MUST be single "
7014 "array in container\n",
7020 analyse_change_exit
:
7025 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
7027 struct intel_super
*super
= st
->sb
;
7028 struct imsm_update_takeover
*u
;
7030 u
= malloc(sizeof(struct imsm_update_takeover
));
7034 u
->type
= update_takeover
;
7035 u
->subarray
= super
->current_vol
;
7037 /* 10->0 transition */
7038 if (geo
->level
== 0)
7039 u
->direction
= R10_TO_R0
;
7041 /* 0->10 transition */
7042 if (geo
->level
== 10)
7043 u
->direction
= R0_TO_R10
;
7045 /* update metadata locally */
7046 imsm_update_metadata_locally(st
, u
,
7047 sizeof(struct imsm_update_takeover
));
7048 /* and possibly remotely */
7049 if (st
->update_tail
)
7050 append_metadata_update(st
, u
,
7051 sizeof(struct imsm_update_takeover
));
7058 static int imsm_reshape_super(struct supertype
*st
, long long size
, int level
,
7059 int layout
, int chunksize
, int raid_disks
,
7060 char *backup
, char *dev
, int verbose
)
7063 struct geo_params geo
;
7065 dprintf("imsm: reshape_super called.\n");
7067 memset(&geo
, sizeof(struct geo_params
), 0);
7070 geo
.dev_id
= st
->devnum
;
7073 geo
.layout
= layout
;
7074 geo
.chunksize
= chunksize
;
7075 geo
.raid_disks
= raid_disks
;
7077 dprintf("\tfor level : %i\n", geo
.level
);
7078 dprintf("\tfor raid_disks : %i\n", geo
.raid_disks
);
7080 if (experimental() == 0)
7083 if (st
->container_dev
== st
->devnum
) {
7084 /* On container level we can only increase number of devices. */
7085 dprintf("imsm: info: Container operation\n");
7086 int old_raid_disks
= 0;
7087 if (imsm_reshape_is_allowed_on_container(
7088 st
, &geo
, &old_raid_disks
)) {
7089 struct imsm_update_reshape
*u
= NULL
;
7092 len
= imsm_create_metadata_update_for_reshape(
7093 st
, &geo
, old_raid_disks
, &u
);
7096 dprintf("imsm: Cannot prepare update\n");
7097 goto exit_imsm_reshape_super
;
7101 /* update metadata locally */
7102 imsm_update_metadata_locally(st
, u
, len
);
7103 /* and possibly remotely */
7104 if (st
->update_tail
)
7105 append_metadata_update(st
, u
, len
);
7110 fprintf(stderr
, Name
"imsm: Operation is not allowed "
7111 "on this container\n");
7114 /* On volume level we support following operations
7115 * - takeover: raid10 -> raid0; raid0 -> raid10
7116 * - chunk size migration
7117 * - migration: raid5 -> raid0; raid0 -> raid5
7119 struct intel_super
*super
= st
->sb
;
7120 struct intel_dev
*dev
= super
->devlist
;
7122 dprintf("imsm: info: Volume operation\n");
7123 /* find requested device */
7125 imsm_find_array_minor_by_subdev(dev
->index
, st
->container_dev
, &devnum
);
7126 if (devnum
== geo
.dev_id
)
7131 fprintf(stderr
, Name
" Cannot find %s (%i) subarray\n",
7132 geo
.dev_name
, geo
.dev_id
);
7133 goto exit_imsm_reshape_super
;
7135 super
->current_vol
= dev
->index
;
7136 change
= imsm_analyze_change(st
, &geo
);
7139 ret_val
= imsm_takeover(st
, &geo
);
7144 case CH_LEVEL_MIGRATION
:
7152 exit_imsm_reshape_super
:
7153 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
7157 static int imsm_manage_reshape(
7158 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
7159 struct supertype
*st
, unsigned long stripes
,
7160 int *fds
, unsigned long long *offsets
,
7161 int dests
, int *destfd
, unsigned long long *destoffsets
)
7163 /* Just use child_monitor for now */
7164 return child_monitor(
7165 afd
, sra
, reshape
, st
, stripes
,
7166 fds
, offsets
, dests
, destfd
, destoffsets
);
7169 struct superswitch super_imsm
= {
7171 .examine_super
= examine_super_imsm
,
7172 .brief_examine_super
= brief_examine_super_imsm
,
7173 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
7174 .export_examine_super
= export_examine_super_imsm
,
7175 .detail_super
= detail_super_imsm
,
7176 .brief_detail_super
= brief_detail_super_imsm
,
7177 .write_init_super
= write_init_super_imsm
,
7178 .validate_geometry
= validate_geometry_imsm
,
7179 .add_to_super
= add_to_super_imsm
,
7180 .remove_from_super
= remove_from_super_imsm
,
7181 .detail_platform
= detail_platform_imsm
,
7182 .kill_subarray
= kill_subarray_imsm
,
7183 .update_subarray
= update_subarray_imsm
,
7184 .load_container
= load_container_imsm
,
7186 .match_home
= match_home_imsm
,
7187 .uuid_from_super
= uuid_from_super_imsm
,
7188 .getinfo_super
= getinfo_super_imsm
,
7189 .getinfo_super_disks
= getinfo_super_disks_imsm
,
7190 .update_super
= update_super_imsm
,
7192 .avail_size
= avail_size_imsm
,
7193 .min_acceptable_spare_size
= min_acceptable_spare_size_imsm
,
7195 .compare_super
= compare_super_imsm
,
7197 .load_super
= load_super_imsm
,
7198 .init_super
= init_super_imsm
,
7199 .store_super
= store_super_imsm
,
7200 .free_super
= free_super_imsm
,
7201 .match_metadata_desc
= match_metadata_desc_imsm
,
7202 .container_content
= container_content_imsm
,
7203 .default_geometry
= default_geometry_imsm
,
7204 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
7205 .reshape_super
= imsm_reshape_super
,
7206 .manage_reshape
= imsm_manage_reshape
,
7213 .open_new
= imsm_open_new
,
7214 .set_array_state
= imsm_set_array_state
,
7215 .set_disk
= imsm_set_disk
,
7216 .sync_metadata
= imsm_sync_metadata
,
7217 .activate_spare
= imsm_activate_spare
,
7218 .process_update
= imsm_process_update
,
7219 .prepare_update
= imsm_prepare_update
,
7220 #endif /* MDASSEMBLE */