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 2210
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 #define RAID_DISK_RESERVED_BLOCKS_IMSM_HI 2209
199 #define GEN_MIGR_AREA_SIZE 2048 /* General Migration Copy Area size in blocks */
201 #define UNIT_SRC_NORMAL 0 /* Source data for curr_migr_unit must
202 * be recovered using srcMap */
203 #define UNIT_SRC_IN_CP_AREA 1 /* Source data for curr_migr_unit has
204 * already been migrated and must
205 * be recovered from checkpoint area */
207 __u32 rec_status
; /* Status used to determine how to restart
208 * migration in case it aborts
210 __u32 curr_migr_unit
; /* 0..numMigrUnits-1 */
211 __u32 family_num
; /* Family number of MPB
212 * containing the RaidDev
213 * that is migrating */
214 __u32 ascending_migr
; /* True if migrating in increasing
216 __u32 blocks_per_unit
; /* Num disk blocks per unit of operation */
217 __u32 dest_depth_per_unit
; /* Num member blocks each destMap
219 * advances per unit-of-operation */
220 __u32 ckpt_area_pba
; /* Pba of first block of ckpt copy area */
221 __u32 dest_1st_member_lba
; /* First member lba on first
222 * stripe of destination */
223 __u32 num_migr_units
; /* Total num migration units-of-op */
224 __u32 post_migr_vol_cap
; /* Size of volume after
225 * migration completes */
226 __u32 post_migr_vol_cap_hi
; /* Expansion space for LBA64 */
227 __u32 ckpt_read_disk_num
; /* Which member disk in destSubMap[0] the
228 * migration ckpt record was read from
229 * (for recovered migrations) */
230 } __attribute__ ((__packed__
));
232 static __u8
migr_type(struct imsm_dev
*dev
)
234 if (dev
->vol
.migr_type
== MIGR_VERIFY
&&
235 dev
->status
& DEV_VERIFY_AND_FIX
)
238 return dev
->vol
.migr_type
;
241 static void set_migr_type(struct imsm_dev
*dev
, __u8 migr_type
)
243 /* for compatibility with older oroms convert MIGR_REPAIR, into
244 * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
246 if (migr_type
== MIGR_REPAIR
) {
247 dev
->vol
.migr_type
= MIGR_VERIFY
;
248 dev
->status
|= DEV_VERIFY_AND_FIX
;
250 dev
->vol
.migr_type
= migr_type
;
251 dev
->status
&= ~DEV_VERIFY_AND_FIX
;
255 static unsigned int sector_count(__u32 bytes
)
257 return ((bytes
+ (512-1)) & (~(512-1))) / 512;
260 static unsigned int mpb_sectors(struct imsm_super
*mpb
)
262 return sector_count(__le32_to_cpu(mpb
->mpb_size
));
266 struct imsm_dev
*dev
;
267 struct intel_dev
*next
;
272 enum sys_dev_type type
;
275 struct intel_hba
*next
;
282 /* internal representation of IMSM metadata */
285 void *buf
; /* O_DIRECT buffer for reading/writing metadata */
286 struct imsm_super
*anchor
; /* immovable parameters */
289 void *migr_rec_buf
; /* buffer for I/O operations */
290 struct migr_record
*migr_rec
; /* migration record */
292 size_t len
; /* size of the 'buf' allocation */
293 void *next_buf
; /* for realloc'ing buf from the manager */
295 int updates_pending
; /* count of pending updates for mdmon */
296 int current_vol
; /* index of raid device undergoing creation */
297 __u32 create_offset
; /* common start for 'current_vol' */
298 __u32 random
; /* random data for seeding new family numbers */
299 struct intel_dev
*devlist
;
303 __u8 serial
[MAX_RAID_SERIAL_LEN
];
306 struct imsm_disk disk
;
309 struct extent
*e
; /* for determining freespace @ create */
310 int raiddisk
; /* slot to fill in autolayout */
313 struct dl
*disk_mgmt_list
; /* list of disks to add/remove while mdmon
315 struct dl
*missing
; /* disks removed while we weren't looking */
316 struct bbm_log
*bbm_log
;
317 struct intel_hba
*hba
; /* device path of the raid controller for this metadata */
318 const struct imsm_orom
*orom
; /* platform firmware support */
319 struct intel_super
*next
; /* (temp) list for disambiguating family_num */
323 struct imsm_disk disk
;
324 #define IMSM_UNKNOWN_OWNER (-1)
326 struct intel_disk
*next
;
330 unsigned long long start
, size
;
333 /* definitions of reshape process types */
334 enum imsm_reshape_type
{
339 /* definition of messages passed to imsm_process_update */
340 enum imsm_update_type
{
341 update_activate_spare
,
345 update_add_remove_disk
,
346 update_reshape_container_disks
,
347 update_reshape_migration
,
351 struct imsm_update_activate_spare
{
352 enum imsm_update_type type
;
356 struct imsm_update_activate_spare
*next
;
369 enum takeover_direction
{
373 struct imsm_update_takeover
{
374 enum imsm_update_type type
;
376 enum takeover_direction direction
;
379 struct imsm_update_reshape
{
380 enum imsm_update_type type
;
384 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
387 struct imsm_update_reshape_migration
{
388 enum imsm_update_type type
;
391 /* fields for array migration changes
398 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
402 __u8 serial
[MAX_RAID_SERIAL_LEN
];
405 struct imsm_update_create_array
{
406 enum imsm_update_type type
;
411 struct imsm_update_kill_array
{
412 enum imsm_update_type type
;
416 struct imsm_update_rename_array
{
417 enum imsm_update_type type
;
418 __u8 name
[MAX_RAID_SERIAL_LEN
];
422 struct imsm_update_add_remove_disk
{
423 enum imsm_update_type type
;
427 static const char *_sys_dev_type
[] = {
428 [SYS_DEV_UNKNOWN
] = "Unknown",
429 [SYS_DEV_SAS
] = "SAS",
430 [SYS_DEV_SATA
] = "SATA"
433 const char *get_sys_dev_type(enum sys_dev_type type
)
435 if (type
>= SYS_DEV_MAX
)
436 type
= SYS_DEV_UNKNOWN
;
438 return _sys_dev_type
[type
];
441 static struct intel_hba
* alloc_intel_hba(struct sys_dev
*device
)
443 struct intel_hba
*result
= malloc(sizeof(*result
));
445 result
->type
= device
->type
;
446 result
->path
= strdup(device
->path
);
448 if (result
->path
&& (result
->pci_id
= strrchr(result
->path
, '/')) != NULL
)
454 static struct intel_hba
* find_intel_hba(struct intel_hba
*hba
, struct sys_dev
*device
)
456 struct intel_hba
*result
=NULL
;
457 for (result
= hba
; result
; result
= result
->next
) {
458 if (result
->type
== device
->type
&& strcmp(result
->path
, device
->path
) == 0)
464 static int attach_hba_to_super(struct intel_super
*super
, struct sys_dev
*device
)
466 struct intel_hba
*hba
;
468 /* check if disk attached to Intel HBA */
469 hba
= find_intel_hba(super
->hba
, device
);
472 /* Check if HBA is already attached to super */
473 if (super
->hba
== NULL
) {
474 super
->hba
= alloc_intel_hba(device
);
479 /* Intel metadata allows for all disks attached to the same type HBA.
480 * Do not sypport odf HBA types mixing
482 if (device
->type
!= hba
->type
)
488 hba
->next
= alloc_intel_hba(device
);
492 static struct sys_dev
* find_disk_attached_hba(int fd
, const char *devname
)
494 struct sys_dev
*list
, *elem
, *prev
;
497 if ((list
= find_intel_devices()) == NULL
)
501 disk_path
= (char *) devname
;
503 disk_path
= diskfd_to_devpath(fd
);
510 for (prev
= NULL
, elem
= list
; elem
; prev
= elem
, elem
= elem
->next
) {
511 if (path_attached_to_hba(disk_path
, elem
->path
)) {
515 prev
->next
= elem
->next
;
517 if (disk_path
!= devname
)
523 if (disk_path
!= devname
)
531 static int find_intel_hba_capability(int fd
, struct intel_super
*super
,
534 static struct supertype
*match_metadata_desc_imsm(char *arg
)
536 struct supertype
*st
;
538 if (strcmp(arg
, "imsm") != 0 &&
539 strcmp(arg
, "default") != 0
543 st
= malloc(sizeof(*st
));
546 memset(st
, 0, sizeof(*st
));
547 st
->container_dev
= NoMdDev
;
548 st
->ss
= &super_imsm
;
549 st
->max_devs
= IMSM_MAX_DEVICES
;
550 st
->minor_version
= 0;
556 static __u8
*get_imsm_version(struct imsm_super
*mpb
)
558 return &mpb
->sig
[MPB_SIG_LEN
];
562 /* retrieve a disk directly from the anchor when the anchor is known to be
563 * up-to-date, currently only at load time
565 static struct imsm_disk
*__get_imsm_disk(struct imsm_super
*mpb
, __u8 index
)
567 if (index
>= mpb
->num_disks
)
569 return &mpb
->disk
[index
];
572 /* retrieve the disk description based on a index of the disk
575 static struct dl
*get_imsm_dl_disk(struct intel_super
*super
, __u8 index
)
579 for (d
= super
->disks
; d
; d
= d
->next
)
580 if (d
->index
== index
)
585 /* retrieve a disk from the parsed metadata */
586 static struct imsm_disk
*get_imsm_disk(struct intel_super
*super
, __u8 index
)
590 dl
= get_imsm_dl_disk(super
, index
);
597 /* generate a checksum directly from the anchor when the anchor is known to be
598 * up-to-date, currently only at load or write_super after coalescing
600 static __u32
__gen_imsm_checksum(struct imsm_super
*mpb
)
602 __u32 end
= mpb
->mpb_size
/ sizeof(end
);
603 __u32
*p
= (__u32
*) mpb
;
607 sum
+= __le32_to_cpu(*p
);
611 return sum
- __le32_to_cpu(mpb
->check_sum
);
614 static size_t sizeof_imsm_map(struct imsm_map
*map
)
616 return sizeof(struct imsm_map
) + sizeof(__u32
) * (map
->num_members
- 1);
619 struct imsm_map
*get_imsm_map(struct imsm_dev
*dev
, int second_map
)
621 /* A device can have 2 maps if it is in the middle of a migration.
623 * 0 - we return the first map
624 * 1 - we return the second map if it exists, else NULL
625 * -1 - we return the second map if it exists, else the first
627 struct imsm_map
*map
= &dev
->vol
.map
[0];
629 if (second_map
== 1 && !dev
->vol
.migr_state
)
631 else if (second_map
== 1 ||
632 (second_map
< 0 && dev
->vol
.migr_state
)) {
635 return ptr
+ sizeof_imsm_map(map
);
641 /* return the size of the device.
642 * migr_state increases the returned size if map[0] were to be duplicated
644 static size_t sizeof_imsm_dev(struct imsm_dev
*dev
, int migr_state
)
646 size_t size
= sizeof(*dev
) - sizeof(struct imsm_map
) +
647 sizeof_imsm_map(get_imsm_map(dev
, 0));
649 /* migrating means an additional map */
650 if (dev
->vol
.migr_state
)
651 size
+= sizeof_imsm_map(get_imsm_map(dev
, 1));
653 size
+= sizeof_imsm_map(get_imsm_map(dev
, 0));
659 /* retrieve disk serial number list from a metadata update */
660 static struct disk_info
*get_disk_info(struct imsm_update_create_array
*update
)
663 struct disk_info
*inf
;
665 inf
= u
+ sizeof(*update
) - sizeof(struct imsm_dev
) +
666 sizeof_imsm_dev(&update
->dev
, 0);
672 static struct imsm_dev
*__get_imsm_dev(struct imsm_super
*mpb
, __u8 index
)
678 if (index
>= mpb
->num_raid_devs
)
681 /* devices start after all disks */
682 offset
= ((void *) &mpb
->disk
[mpb
->num_disks
]) - _mpb
;
684 for (i
= 0; i
<= index
; i
++)
686 return _mpb
+ offset
;
688 offset
+= sizeof_imsm_dev(_mpb
+ offset
, 0);
693 static struct imsm_dev
*get_imsm_dev(struct intel_super
*super
, __u8 index
)
695 struct intel_dev
*dv
;
697 if (index
>= super
->anchor
->num_raid_devs
)
699 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
700 if (dv
->index
== index
)
708 * == 1 get second map
709 * == -1 than get map according to the current migr_state
711 static __u32
get_imsm_ord_tbl_ent(struct imsm_dev
*dev
,
715 struct imsm_map
*map
;
717 map
= get_imsm_map(dev
, second_map
);
719 /* top byte identifies disk under rebuild */
720 return __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
723 #define ord_to_idx(ord) (((ord) << 8) >> 8)
724 static __u32
get_imsm_disk_idx(struct imsm_dev
*dev
, int slot
, int second_map
)
726 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, second_map
);
728 return ord_to_idx(ord
);
731 static void set_imsm_ord_tbl_ent(struct imsm_map
*map
, int slot
, __u32 ord
)
733 map
->disk_ord_tbl
[slot
] = __cpu_to_le32(ord
);
736 static int get_imsm_disk_slot(struct imsm_map
*map
, unsigned idx
)
741 for (slot
= 0; slot
< map
->num_members
; slot
++) {
742 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
743 if (ord_to_idx(ord
) == idx
)
750 static int get_imsm_raid_level(struct imsm_map
*map
)
752 if (map
->raid_level
== 1) {
753 if (map
->num_members
== 2)
759 return map
->raid_level
;
762 static int cmp_extent(const void *av
, const void *bv
)
764 const struct extent
*a
= av
;
765 const struct extent
*b
= bv
;
766 if (a
->start
< b
->start
)
768 if (a
->start
> b
->start
)
773 static int count_memberships(struct dl
*dl
, struct intel_super
*super
)
778 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
779 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
780 struct imsm_map
*map
= get_imsm_map(dev
, 0);
782 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
789 static struct extent
*get_extents(struct intel_super
*super
, struct dl
*dl
)
791 /* find a list of used extents on the given physical device */
792 struct extent
*rv
, *e
;
794 int memberships
= count_memberships(dl
, super
);
795 __u32 reservation
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
797 rv
= malloc(sizeof(struct extent
) * (memberships
+ 1));
802 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
803 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
804 struct imsm_map
*map
= get_imsm_map(dev
, 0);
806 if (get_imsm_disk_slot(map
, dl
->index
) >= 0) {
807 e
->start
= __le32_to_cpu(map
->pba_of_lba0
);
808 e
->size
= __le32_to_cpu(map
->blocks_per_member
);
812 qsort(rv
, memberships
, sizeof(*rv
), cmp_extent
);
814 /* determine the start of the metadata
815 * when no raid devices are defined use the default
816 * ...otherwise allow the metadata to truncate the value
817 * as is the case with older versions of imsm
820 struct extent
*last
= &rv
[memberships
- 1];
823 remainder
= __le32_to_cpu(dl
->disk
.total_blocks
) -
824 (last
->start
+ last
->size
);
825 /* round down to 1k block to satisfy precision of the kernel
829 /* make sure remainder is still sane */
830 if (remainder
< (unsigned)ROUND_UP(super
->len
, 512) >> 9)
831 remainder
= ROUND_UP(super
->len
, 512) >> 9;
832 if (reservation
> remainder
)
833 reservation
= remainder
;
835 e
->start
= __le32_to_cpu(dl
->disk
.total_blocks
) - reservation
;
840 /* try to determine how much space is reserved for metadata from
841 * the last get_extents() entry, otherwise fallback to the
844 static __u32
imsm_reserved_sectors(struct intel_super
*super
, struct dl
*dl
)
850 /* for spares just return a minimal reservation which will grow
851 * once the spare is picked up by an array
854 return MPB_SECTOR_CNT
;
856 e
= get_extents(super
, dl
);
858 return MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
860 /* scroll to last entry */
861 for (i
= 0; e
[i
].size
; i
++)
864 rv
= __le32_to_cpu(dl
->disk
.total_blocks
) - e
[i
].start
;
871 static int is_spare(struct imsm_disk
*disk
)
873 return (disk
->status
& SPARE_DISK
) == SPARE_DISK
;
876 static int is_configured(struct imsm_disk
*disk
)
878 return (disk
->status
& CONFIGURED_DISK
) == CONFIGURED_DISK
;
881 static int is_failed(struct imsm_disk
*disk
)
883 return (disk
->status
& FAILED_DISK
) == FAILED_DISK
;
886 /* Return minimum size of a spare that can be used in this array*/
887 static unsigned long long min_acceptable_spare_size_imsm(struct supertype
*st
)
889 struct intel_super
*super
= st
->sb
;
893 unsigned long long rv
= 0;
897 /* find first active disk in array */
899 while (dl
&& (is_failed(&dl
->disk
) || dl
->index
== -1))
903 /* find last lba used by subarrays */
904 e
= get_extents(super
, dl
);
907 for (i
= 0; e
[i
].size
; i
++)
910 rv
= e
[i
-1].start
+ e
[i
-1].size
;
912 /* add the amount of space needed for metadata */
913 rv
= rv
+ MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
918 static __u64
blocks_per_migr_unit(struct imsm_dev
*dev
);
920 static void print_imsm_dev(struct imsm_dev
*dev
, char *uuid
, int disk_idx
)
924 struct imsm_map
*map
= get_imsm_map(dev
, 0);
925 struct imsm_map
*map2
= get_imsm_map(dev
, 1);
929 printf("[%.16s]:\n", dev
->volume
);
930 printf(" UUID : %s\n", uuid
);
931 printf(" RAID Level : %d", get_imsm_raid_level(map
));
933 printf(" <-- %d", get_imsm_raid_level(map2
));
935 printf(" Members : %d", map
->num_members
);
937 printf(" <-- %d", map2
->num_members
);
939 printf(" Slots : [");
940 for (i
= 0; i
< map
->num_members
; i
++) {
941 ord
= get_imsm_ord_tbl_ent(dev
, i
, 0);
942 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
947 for (i
= 0; i
< map2
->num_members
; i
++) {
948 ord
= get_imsm_ord_tbl_ent(dev
, i
, 1);
949 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
954 printf(" Failed disk : ");
955 if (map
->failed_disk_num
== 0xff)
958 printf("%i", map
->failed_disk_num
);
960 slot
= get_imsm_disk_slot(map
, disk_idx
);
962 ord
= get_imsm_ord_tbl_ent(dev
, slot
, -1);
963 printf(" This Slot : %d%s\n", slot
,
964 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
966 printf(" This Slot : ?\n");
967 sz
= __le32_to_cpu(dev
->size_high
);
969 sz
+= __le32_to_cpu(dev
->size_low
);
970 printf(" Array Size : %llu%s\n", (unsigned long long)sz
,
971 human_size(sz
* 512));
972 sz
= __le32_to_cpu(map
->blocks_per_member
);
973 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz
,
974 human_size(sz
* 512));
975 printf(" Sector Offset : %u\n",
976 __le32_to_cpu(map
->pba_of_lba0
));
977 printf(" Num Stripes : %u\n",
978 __le32_to_cpu(map
->num_data_stripes
));
979 printf(" Chunk Size : %u KiB",
980 __le16_to_cpu(map
->blocks_per_strip
) / 2);
982 printf(" <-- %u KiB",
983 __le16_to_cpu(map2
->blocks_per_strip
) / 2);
985 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
986 printf(" Migrate State : ");
987 if (dev
->vol
.migr_state
) {
988 if (migr_type(dev
) == MIGR_INIT
)
989 printf("initialize\n");
990 else if (migr_type(dev
) == MIGR_REBUILD
)
992 else if (migr_type(dev
) == MIGR_VERIFY
)
994 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
995 printf("general migration\n");
996 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
997 printf("state change\n");
998 else if (migr_type(dev
) == MIGR_REPAIR
)
1001 printf("<unknown:%d>\n", migr_type(dev
));
1004 printf(" Map State : %s", map_state_str
[map
->map_state
]);
1005 if (dev
->vol
.migr_state
) {
1006 struct imsm_map
*map
= get_imsm_map(dev
, 1);
1008 printf(" <-- %s", map_state_str
[map
->map_state
]);
1009 printf("\n Checkpoint : %u (%llu)",
1010 __le32_to_cpu(dev
->vol
.curr_migr_unit
),
1011 (unsigned long long)blocks_per_migr_unit(dev
));
1014 printf(" Dirty State : %s\n", dev
->vol
.dirty
? "dirty" : "clean");
1017 static void print_imsm_disk(struct imsm_super
*mpb
, int index
, __u32 reserved
)
1019 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, index
);
1020 char str
[MAX_RAID_SERIAL_LEN
+ 1];
1023 if (index
< 0 || !disk
)
1027 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
1028 printf(" Disk%02d Serial : %s\n", index
, str
);
1029 printf(" State :%s%s%s\n", is_spare(disk
) ? " spare" : "",
1030 is_configured(disk
) ? " active" : "",
1031 is_failed(disk
) ? " failed" : "");
1032 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
1033 sz
= __le32_to_cpu(disk
->total_blocks
) - reserved
;
1034 printf(" Usable Size : %llu%s\n", (unsigned long long)sz
,
1035 human_size(sz
* 512));
1038 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
);
1040 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
1042 struct intel_super
*super
= st
->sb
;
1043 struct imsm_super
*mpb
= super
->anchor
;
1044 char str
[MAX_SIGNATURE_LENGTH
];
1049 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
1052 snprintf(str
, MPB_SIG_LEN
, "%s", mpb
->sig
);
1053 printf(" Magic : %s\n", str
);
1054 snprintf(str
, strlen(MPB_VERSION_RAID0
), "%s", get_imsm_version(mpb
));
1055 printf(" Version : %s\n", get_imsm_version(mpb
));
1056 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
1057 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
1058 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
1059 getinfo_super_imsm(st
, &info
, NULL
);
1060 fname_from_uuid(st
, &info
, nbuf
, ':');
1061 printf(" UUID : %s\n", nbuf
+ 5);
1062 sum
= __le32_to_cpu(mpb
->check_sum
);
1063 printf(" Checksum : %08x %s\n", sum
,
1064 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
1065 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
));
1066 printf(" Disks : %d\n", mpb
->num_disks
);
1067 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
1068 print_imsm_disk(mpb
, super
->disks
->index
, reserved
);
1069 if (super
->bbm_log
) {
1070 struct bbm_log
*log
= super
->bbm_log
;
1073 printf("Bad Block Management Log:\n");
1074 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
1075 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
1076 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
1077 printf(" Spare Blocks : %d\n", __le32_to_cpu(log
->reserved_spare_block_count
));
1078 printf(" First Spare : %llx\n",
1079 (unsigned long long) __le64_to_cpu(log
->first_spare_lba
));
1081 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1083 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1085 super
->current_vol
= i
;
1086 getinfo_super_imsm(st
, &info
, NULL
);
1087 fname_from_uuid(st
, &info
, nbuf
, ':');
1088 print_imsm_dev(dev
, nbuf
+ 5, super
->disks
->index
);
1090 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1091 if (i
== super
->disks
->index
)
1093 print_imsm_disk(mpb
, i
, reserved
);
1095 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
1096 struct imsm_disk
*disk
;
1097 char str
[MAX_RAID_SERIAL_LEN
+ 1];
1105 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
1106 printf(" Disk Serial : %s\n", str
);
1107 printf(" State :%s%s%s\n", is_spare(disk
) ? " spare" : "",
1108 is_configured(disk
) ? " active" : "",
1109 is_failed(disk
) ? " failed" : "");
1110 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
1111 sz
= __le32_to_cpu(disk
->total_blocks
) - reserved
;
1112 printf(" Usable Size : %llu%s\n", (unsigned long long)sz
,
1113 human_size(sz
* 512));
1117 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
1119 /* We just write a generic IMSM ARRAY entry */
1122 struct intel_super
*super
= st
->sb
;
1124 if (!super
->anchor
->num_raid_devs
) {
1125 printf("ARRAY metadata=imsm\n");
1129 getinfo_super_imsm(st
, &info
, NULL
);
1130 fname_from_uuid(st
, &info
, nbuf
, ':');
1131 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
1134 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
1136 /* We just write a generic IMSM ARRAY entry */
1140 struct intel_super
*super
= st
->sb
;
1143 if (!super
->anchor
->num_raid_devs
)
1146 getinfo_super_imsm(st
, &info
, NULL
);
1147 fname_from_uuid(st
, &info
, nbuf
, ':');
1148 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1149 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1151 super
->current_vol
= i
;
1152 getinfo_super_imsm(st
, &info
, NULL
);
1153 fname_from_uuid(st
, &info
, nbuf1
, ':');
1154 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
1155 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
1159 static void export_examine_super_imsm(struct supertype
*st
)
1161 struct intel_super
*super
= st
->sb
;
1162 struct imsm_super
*mpb
= super
->anchor
;
1166 getinfo_super_imsm(st
, &info
, NULL
);
1167 fname_from_uuid(st
, &info
, nbuf
, ':');
1168 printf("MD_METADATA=imsm\n");
1169 printf("MD_LEVEL=container\n");
1170 printf("MD_UUID=%s\n", nbuf
+5);
1171 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
1174 static void detail_super_imsm(struct supertype
*st
, char *homehost
)
1179 getinfo_super_imsm(st
, &info
, NULL
);
1180 fname_from_uuid(st
, &info
, nbuf
, ':');
1181 printf("\n UUID : %s\n", nbuf
+ 5);
1184 static void brief_detail_super_imsm(struct supertype
*st
)
1188 getinfo_super_imsm(st
, &info
, NULL
);
1189 fname_from_uuid(st
, &info
, nbuf
, ':');
1190 printf(" UUID=%s", nbuf
+ 5);
1193 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
);
1194 static void fd2devname(int fd
, char *name
);
1196 static int ahci_enumerate_ports(const char *hba_path
, int port_count
, int host_base
, int verbose
)
1198 /* dump an unsorted list of devices attached to AHCI Intel storage
1199 * controller, as well as non-connected ports
1201 int hba_len
= strlen(hba_path
) + 1;
1206 unsigned long port_mask
= (1 << port_count
) - 1;
1208 if (port_count
> (int)sizeof(port_mask
) * 8) {
1210 fprintf(stderr
, Name
": port_count %d out of range\n", port_count
);
1214 /* scroll through /sys/dev/block looking for devices attached to
1217 dir
= opendir("/sys/dev/block");
1218 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
1229 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
1231 path
= devt_to_devpath(makedev(major
, minor
));
1234 if (!path_attached_to_hba(path
, hba_path
)) {
1240 /* retrieve the scsi device type */
1241 if (asprintf(&device
, "/sys/dev/block/%d:%d/device/xxxxxxx", major
, minor
) < 0) {
1243 fprintf(stderr
, Name
": failed to allocate 'device'\n");
1247 sprintf(device
, "/sys/dev/block/%d:%d/device/type", major
, minor
);
1248 if (load_sys(device
, buf
) != 0) {
1250 fprintf(stderr
, Name
": failed to read device type for %s\n",
1256 type
= strtoul(buf
, NULL
, 10);
1258 /* if it's not a disk print the vendor and model */
1259 if (!(type
== 0 || type
== 7 || type
== 14)) {
1262 sprintf(device
, "/sys/dev/block/%d:%d/device/vendor", major
, minor
);
1263 if (load_sys(device
, buf
) == 0) {
1264 strncpy(vendor
, buf
, sizeof(vendor
));
1265 vendor
[sizeof(vendor
) - 1] = '\0';
1266 c
= (char *) &vendor
[sizeof(vendor
) - 1];
1267 while (isspace(*c
) || *c
== '\0')
1271 sprintf(device
, "/sys/dev/block/%d:%d/device/model", major
, minor
);
1272 if (load_sys(device
, buf
) == 0) {
1273 strncpy(model
, buf
, sizeof(model
));
1274 model
[sizeof(model
) - 1] = '\0';
1275 c
= (char *) &model
[sizeof(model
) - 1];
1276 while (isspace(*c
) || *c
== '\0')
1280 if (vendor
[0] && model
[0])
1281 sprintf(buf
, "%.64s %.64s", vendor
, model
);
1283 switch (type
) { /* numbers from hald/linux/device.c */
1284 case 1: sprintf(buf
, "tape"); break;
1285 case 2: sprintf(buf
, "printer"); break;
1286 case 3: sprintf(buf
, "processor"); break;
1288 case 5: sprintf(buf
, "cdrom"); break;
1289 case 6: sprintf(buf
, "scanner"); break;
1290 case 8: sprintf(buf
, "media_changer"); break;
1291 case 9: sprintf(buf
, "comm"); break;
1292 case 12: sprintf(buf
, "raid"); break;
1293 default: sprintf(buf
, "unknown");
1299 /* chop device path to 'host%d' and calculate the port number */
1300 c
= strchr(&path
[hba_len
], '/');
1303 fprintf(stderr
, Name
": %s - invalid path name\n", path
+ hba_len
);
1308 if (sscanf(&path
[hba_len
], "host%d", &port
) == 1)
1312 *c
= '/'; /* repair the full string */
1313 fprintf(stderr
, Name
": failed to determine port number for %s\n",
1320 /* mark this port as used */
1321 port_mask
&= ~(1 << port
);
1323 /* print out the device information */
1325 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
1329 fd
= dev_open(ent
->d_name
, O_RDONLY
);
1331 printf(" Port%d : - disk info unavailable -\n", port
);
1333 fd2devname(fd
, buf
);
1334 printf(" Port%d : %s", port
, buf
);
1335 if (imsm_read_serial(fd
, NULL
, (__u8
*) buf
) == 0)
1336 printf(" (%s)\n", buf
);
1351 for (i
= 0; i
< port_count
; i
++)
1352 if (port_mask
& (1 << i
))
1353 printf(" Port%d : - no device attached -\n", i
);
1361 static void print_found_intel_controllers(struct sys_dev
*elem
)
1363 for (; elem
; elem
= elem
->next
) {
1364 fprintf(stderr
, Name
": found Intel(R) ");
1365 if (elem
->type
== SYS_DEV_SATA
)
1366 fprintf(stderr
, "SATA ");
1367 else if (elem
->type
== SYS_DEV_SAS
)
1368 fprintf(stderr
, "SAS ");
1369 fprintf(stderr
, "RAID controller");
1371 fprintf(stderr
, " at %s", elem
->pci_id
);
1372 fprintf(stderr
, ".\n");
1377 static int ahci_get_port_count(const char *hba_path
, int *port_count
)
1384 if ((dir
= opendir(hba_path
)) == NULL
)
1387 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
1390 if (sscanf(ent
->d_name
, "host%d", &host
) != 1)
1392 if (*port_count
== 0)
1394 else if (host
< host_base
)
1397 if (host
+ 1 > *port_count
+ host_base
)
1398 *port_count
= host
+ 1 - host_base
;
1404 static void print_imsm_capability(const struct imsm_orom
*orom
)
1406 printf(" Platform : Intel(R) Matrix Storage Manager\n");
1407 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
1408 orom
->hotfix_ver
, orom
->build
);
1409 printf(" RAID Levels :%s%s%s%s%s\n",
1410 imsm_orom_has_raid0(orom
) ? " raid0" : "",
1411 imsm_orom_has_raid1(orom
) ? " raid1" : "",
1412 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
1413 imsm_orom_has_raid10(orom
) ? " raid10" : "",
1414 imsm_orom_has_raid5(orom
) ? " raid5" : "");
1415 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1416 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
1417 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
1418 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
1419 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
1420 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
1421 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
1422 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
1423 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
1424 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
1425 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
1426 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
1427 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
1428 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
1429 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
1430 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
1431 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
1432 printf(" Max Disks : %d\n", orom
->tds
);
1433 printf(" Max Volumes : %d\n", orom
->vpa
);
1437 static int detail_platform_imsm(int verbose
, int enumerate_only
)
1439 /* There are two components to imsm platform support, the ahci SATA
1440 * controller and the option-rom. To find the SATA controller we
1441 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
1442 * controller with the Intel vendor id is present. This approach
1443 * allows mdadm to leverage the kernel's ahci detection logic, with the
1444 * caveat that if ahci.ko is not loaded mdadm will not be able to
1445 * detect platform raid capabilities. The option-rom resides in a
1446 * platform "Adapter ROM". We scan for its signature to retrieve the
1447 * platform capabilities. If raid support is disabled in the BIOS the
1448 * option-rom capability structure will not be available.
1450 const struct imsm_orom
*orom
;
1451 struct sys_dev
*list
, *hba
;
1456 if (enumerate_only
) {
1457 if (check_env("IMSM_NO_PLATFORM"))
1459 list
= find_intel_devices();
1462 for (hba
= list
; hba
; hba
= hba
->next
) {
1463 orom
= find_imsm_capability(hba
->type
);
1469 free_sys_dev(&list
);
1473 list
= find_intel_devices();
1476 fprintf(stderr
, Name
": no active Intel(R) RAID "
1477 "controller found.\n");
1478 free_sys_dev(&list
);
1481 print_found_intel_controllers(list
);
1483 for (hba
= list
; hba
; hba
= hba
->next
) {
1484 orom
= find_imsm_capability(hba
->type
);
1486 fprintf(stderr
, Name
": imsm capabilities not found for controller: %s (type %s)\n",
1487 hba
->path
, get_sys_dev_type(hba
->type
));
1489 print_imsm_capability(orom
);
1492 for (hba
= list
; hba
; hba
= hba
->next
) {
1493 printf(" I/O Controller : %s (%s)\n",
1494 hba
->path
, get_sys_dev_type(hba
->type
));
1496 if (hba
->type
== SYS_DEV_SATA
) {
1497 host_base
= ahci_get_port_count(hba
->path
, &port_count
);
1498 if (ahci_enumerate_ports(hba
->path
, port_count
, host_base
, verbose
)) {
1500 fprintf(stderr
, Name
": failed to enumerate "
1501 "ports on SATA controller at %s.", hba
->pci_id
);
1507 free_sys_dev(&list
);
1512 static int match_home_imsm(struct supertype
*st
, char *homehost
)
1514 /* the imsm metadata format does not specify any host
1515 * identification information. We return -1 since we can never
1516 * confirm nor deny whether a given array is "meant" for this
1517 * host. We rely on compare_super and the 'family_num' fields to
1518 * exclude member disks that do not belong, and we rely on
1519 * mdadm.conf to specify the arrays that should be assembled.
1520 * Auto-assembly may still pick up "foreign" arrays.
1526 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
1528 /* The uuid returned here is used for:
1529 * uuid to put into bitmap file (Create, Grow)
1530 * uuid for backup header when saving critical section (Grow)
1531 * comparing uuids when re-adding a device into an array
1532 * In these cases the uuid required is that of the data-array,
1533 * not the device-set.
1534 * uuid to recognise same set when adding a missing device back
1535 * to an array. This is a uuid for the device-set.
1537 * For each of these we can make do with a truncated
1538 * or hashed uuid rather than the original, as long as
1540 * In each case the uuid required is that of the data-array,
1541 * not the device-set.
1543 /* imsm does not track uuid's so we synthesis one using sha1 on
1544 * - The signature (Which is constant for all imsm array, but no matter)
1545 * - the orig_family_num of the container
1546 * - the index number of the volume
1547 * - the 'serial' number of the volume.
1548 * Hopefully these are all constant.
1550 struct intel_super
*super
= st
->sb
;
1553 struct sha1_ctx ctx
;
1554 struct imsm_dev
*dev
= NULL
;
1557 /* some mdadm versions failed to set ->orig_family_num, in which
1558 * case fall back to ->family_num. orig_family_num will be
1559 * fixed up with the first metadata update.
1561 family_num
= super
->anchor
->orig_family_num
;
1562 if (family_num
== 0)
1563 family_num
= super
->anchor
->family_num
;
1564 sha1_init_ctx(&ctx
);
1565 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
1566 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
1567 if (super
->current_vol
>= 0)
1568 dev
= get_imsm_dev(super
, super
->current_vol
);
1570 __u32 vol
= super
->current_vol
;
1571 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
1572 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
1574 sha1_finish_ctx(&ctx
, buf
);
1575 memcpy(uuid
, buf
, 4*4);
1580 get_imsm_numerical_version(struct imsm_super
*mpb
, int *m
, int *p
)
1582 __u8
*v
= get_imsm_version(mpb
);
1583 __u8
*end
= mpb
->sig
+ MAX_SIGNATURE_LENGTH
;
1584 char major
[] = { 0, 0, 0 };
1585 char minor
[] = { 0 ,0, 0 };
1586 char patch
[] = { 0, 0, 0 };
1587 char *ver_parse
[] = { major
, minor
, patch
};
1591 while (*v
!= '\0' && v
< end
) {
1592 if (*v
!= '.' && j
< 2)
1593 ver_parse
[i
][j
++] = *v
;
1601 *m
= strtol(minor
, NULL
, 0);
1602 *p
= strtol(patch
, NULL
, 0);
1606 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
1608 /* migr_strip_size when repairing or initializing parity */
1609 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1610 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1612 switch (get_imsm_raid_level(map
)) {
1617 return 128*1024 >> 9;
1621 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
1623 /* migr_strip_size when rebuilding a degraded disk, no idea why
1624 * this is different than migr_strip_size_resync(), but it's good
1627 struct imsm_map
*map
= get_imsm_map(dev
, 1);
1628 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1630 switch (get_imsm_raid_level(map
)) {
1633 if (map
->num_members
% map
->num_domains
== 0)
1634 return 128*1024 >> 9;
1638 return max((__u32
) 64*1024 >> 9, chunk
);
1640 return 128*1024 >> 9;
1644 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
1646 struct imsm_map
*lo
= get_imsm_map(dev
, 0);
1647 struct imsm_map
*hi
= get_imsm_map(dev
, 1);
1648 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
1649 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
1651 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
1654 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
1656 struct imsm_map
*lo
= get_imsm_map(dev
, 0);
1657 int level
= get_imsm_raid_level(lo
);
1659 if (level
== 1 || level
== 10) {
1660 struct imsm_map
*hi
= get_imsm_map(dev
, 1);
1662 return hi
->num_domains
;
1664 return num_stripes_per_unit_resync(dev
);
1667 static __u8
imsm_num_data_members(struct imsm_dev
*dev
, int second_map
)
1669 /* named 'imsm_' because raid0, raid1 and raid10
1670 * counter-intuitively have the same number of data disks
1672 struct imsm_map
*map
= get_imsm_map(dev
, second_map
);
1674 switch (get_imsm_raid_level(map
)) {
1678 return map
->num_members
;
1680 return map
->num_members
- 1;
1682 dprintf("%s: unsupported raid level\n", __func__
);
1687 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
1689 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1690 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1692 switch(get_imsm_raid_level(map
)) {
1695 return chunk
* map
->num_domains
;
1697 return chunk
* map
->num_members
;
1703 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
1705 struct imsm_map
*map
= get_imsm_map(dev
, 1);
1706 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1707 __u32 strip
= block
/ chunk
;
1709 switch (get_imsm_raid_level(map
)) {
1712 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
1713 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
1715 return vol_stripe
* chunk
+ block
% chunk
;
1717 __u32 stripe
= strip
/ (map
->num_members
- 1);
1719 return stripe
* chunk
+ block
% chunk
;
1726 static __u64
blocks_per_migr_unit(struct imsm_dev
*dev
)
1728 /* calculate the conversion factor between per member 'blocks'
1729 * (md/{resync,rebuild}_start) and imsm migration units, return
1730 * 0 for the 'not migrating' and 'unsupported migration' cases
1732 if (!dev
->vol
.migr_state
)
1735 switch (migr_type(dev
)) {
1740 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1741 __u32 stripes_per_unit
;
1742 __u32 blocks_per_unit
;
1751 /* yes, this is really the translation of migr_units to
1752 * per-member blocks in the 'resync' case
1754 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
1755 migr_chunk
= migr_strip_blocks_resync(dev
);
1756 disks
= imsm_num_data_members(dev
, 0);
1757 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
1758 stripe
= __le32_to_cpu(map
->blocks_per_strip
) * disks
;
1759 segment
= blocks_per_unit
/ stripe
;
1760 block_rel
= blocks_per_unit
- segment
* stripe
;
1761 parity_depth
= parity_segment_depth(dev
);
1762 block_map
= map_migr_block(dev
, block_rel
);
1763 return block_map
+ parity_depth
* segment
;
1765 case MIGR_REBUILD
: {
1766 __u32 stripes_per_unit
;
1769 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
1770 migr_chunk
= migr_strip_blocks_rebuild(dev
);
1771 return migr_chunk
* stripes_per_unit
;
1773 case MIGR_STATE_CHANGE
:
1779 static int imsm_level_to_layout(int level
)
1787 return ALGORITHM_LEFT_ASYMMETRIC
;
1794 /*******************************************************************************
1795 * Function: read_imsm_migr_rec
1796 * Description: Function reads imsm migration record from last sector of disk
1798 * fd : disk descriptor
1799 * super : metadata info
1803 ******************************************************************************/
1804 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
)
1807 unsigned long long dsize
;
1809 get_dev_size(fd
, NULL
, &dsize
);
1810 if (lseek64(fd
, dsize
- 512, SEEK_SET
) < 0) {
1812 Name
": Cannot seek to anchor block: %s\n",
1816 if (read(fd
, super
->migr_rec_buf
, 512) != 512) {
1818 Name
": Cannot read migr record block: %s\n",
1828 /*******************************************************************************
1829 * Function: load_imsm_migr_rec
1830 * Description: Function reads imsm migration record (it is stored at the last
1833 * super : imsm internal array info
1834 * info : general array info
1838 ******************************************************************************/
1839 static int load_imsm_migr_rec(struct intel_super
*super
, struct mdinfo
*info
)
1842 struct dl
*dl
= NULL
;
1848 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
1849 /* read only from one of the first two slots */
1850 if ((sd
->disk
.raid_disk
> 1) ||
1851 (sd
->disk
.raid_disk
< 0))
1853 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
1854 fd
= dev_open(nm
, O_RDONLY
);
1860 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
1861 /* read only from one of the first two slots */
1864 sprintf(nm
, "%d:%d", dl
->major
, dl
->minor
);
1865 fd
= dev_open(nm
, O_RDONLY
);
1872 retval
= read_imsm_migr_rec(fd
, super
);
1880 /*******************************************************************************
1881 * Function: write_imsm_migr_rec
1882 * Description: Function writes imsm migration record
1883 * (at the last sector of disk)
1885 * super : imsm internal array info
1889 ******************************************************************************/
1890 static int write_imsm_migr_rec(struct supertype
*st
)
1892 struct intel_super
*super
= st
->sb
;
1893 unsigned long long dsize
;
1899 for (sd
= super
->disks
; sd
; sd
= sd
->next
) {
1900 /* write to 2 first slots only */
1901 if ((sd
->index
< 0) || (sd
->index
> 1))
1903 sprintf(nm
, "%d:%d", sd
->major
, sd
->minor
);
1904 fd
= dev_open(nm
, O_RDWR
);
1907 get_dev_size(fd
, NULL
, &dsize
);
1908 if (lseek64(fd
, dsize
- 512, SEEK_SET
) < 0) {
1910 Name
": Cannot seek to anchor block: %s\n",
1914 if (write(fd
, super
->migr_rec_buf
, 512) != 512) {
1916 Name
": Cannot write migr record block: %s\n",
1931 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
1933 struct intel_super
*super
= st
->sb
;
1934 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
1935 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1936 struct imsm_map
*prev_map
= get_imsm_map(dev
, 1);
1937 struct imsm_map
*map_to_analyse
= map
;
1940 unsigned int component_size_alligment
;
1941 int map_disks
= info
->array
.raid_disks
;
1943 memset(info
, 0, sizeof(*info
));
1945 map_to_analyse
= prev_map
;
1947 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
1948 if (dl
->raiddisk
== info
->disk
.raid_disk
)
1950 info
->container_member
= super
->current_vol
;
1951 info
->array
.raid_disks
= map
->num_members
;
1952 info
->array
.level
= get_imsm_raid_level(map_to_analyse
);
1953 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
1954 info
->array
.md_minor
= -1;
1955 info
->array
.ctime
= 0;
1956 info
->array
.utime
= 0;
1957 info
->array
.chunk_size
=
1958 __le16_to_cpu(map_to_analyse
->blocks_per_strip
) << 9;
1959 info
->array
.state
= !dev
->vol
.dirty
;
1960 info
->custom_array_size
= __le32_to_cpu(dev
->size_high
);
1961 info
->custom_array_size
<<= 32;
1962 info
->custom_array_size
|= __le32_to_cpu(dev
->size_low
);
1963 if (prev_map
&& map
->map_state
== prev_map
->map_state
) {
1964 info
->reshape_active
= 1;
1965 info
->new_level
= get_imsm_raid_level(map
);
1966 info
->new_layout
= imsm_level_to_layout(info
->new_level
);
1967 info
->new_chunk
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
1968 info
->delta_disks
= map
->num_members
- prev_map
->num_members
;
1969 if (info
->delta_disks
) {
1970 /* this needs to be applied to every array
1973 info
->reshape_active
= 2;
1975 /* We shape information that we give to md might have to be
1976 * modify to cope with md's requirement for reshaping arrays.
1977 * For example, when reshaping a RAID0, md requires it to be
1978 * presented as a degraded RAID4.
1979 * Also if a RAID0 is migrating to a RAID5 we need to specify
1980 * the array as already being RAID5, but the 'before' layout
1981 * is a RAID4-like layout.
1983 switch (info
->array
.level
) {
1985 switch(info
->new_level
) {
1987 /* conversion is happening as RAID4 */
1988 info
->array
.level
= 4;
1989 info
->array
.raid_disks
+= 1;
1992 /* conversion is happening as RAID5 */
1993 info
->array
.level
= 5;
1994 info
->array
.layout
= ALGORITHM_PARITY_N
;
1995 info
->array
.raid_disks
+= 1;
1996 info
->delta_disks
-= 1;
1999 /* FIXME error message */
2000 info
->array
.level
= UnSet
;
2006 info
->new_level
= UnSet
;
2007 info
->new_layout
= UnSet
;
2008 info
->new_chunk
= info
->array
.chunk_size
;
2009 info
->delta_disks
= 0;
2011 info
->disk
.major
= 0;
2012 info
->disk
.minor
= 0;
2014 info
->disk
.major
= dl
->major
;
2015 info
->disk
.minor
= dl
->minor
;
2018 info
->data_offset
= __le32_to_cpu(map_to_analyse
->pba_of_lba0
);
2019 info
->component_size
=
2020 __le32_to_cpu(map_to_analyse
->blocks_per_member
);
2022 /* check component size aligment
2024 component_size_alligment
=
2025 info
->component_size
% (info
->array
.chunk_size
/512);
2027 if (component_size_alligment
&&
2028 (info
->array
.level
!= 1) && (info
->array
.level
!= UnSet
)) {
2029 dprintf("imsm: reported component size alligned from %llu ",
2030 info
->component_size
);
2031 info
->component_size
-= component_size_alligment
;
2032 dprintf("to %llu (%i).\n",
2033 info
->component_size
, component_size_alligment
);
2036 memset(info
->uuid
, 0, sizeof(info
->uuid
));
2037 info
->recovery_start
= MaxSector
;
2039 info
->reshape_progress
= 0;
2040 info
->resync_start
= MaxSector
;
2041 if (map_to_analyse
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
2043 info
->resync_start
= 0;
2045 if (dev
->vol
.migr_state
) {
2046 switch (migr_type(dev
)) {
2049 __u64 blocks_per_unit
= blocks_per_migr_unit(dev
);
2050 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
2052 info
->resync_start
= blocks_per_unit
* units
;
2055 case MIGR_GEN_MIGR
: {
2056 __u64 blocks_per_unit
= blocks_per_migr_unit(dev
);
2057 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
2058 unsigned long long array_blocks
;
2061 info
->reshape_progress
= blocks_per_unit
* units
;
2063 dprintf("IMSM: General Migration checkpoint : %llu "
2064 "(%llu) -> read reshape progress : %llu\n",
2065 units
, blocks_per_unit
, info
->reshape_progress
);
2067 used_disks
= imsm_num_data_members(dev
, 1);
2068 if (used_disks
> 0) {
2069 array_blocks
= map
->blocks_per_member
*
2071 /* round array size down to closest MB
2073 info
->custom_array_size
= (array_blocks
2074 >> SECT_PER_MB_SHIFT
)
2075 << SECT_PER_MB_SHIFT
;
2079 /* we could emulate the checkpointing of
2080 * 'sync_action=check' migrations, but for now
2081 * we just immediately complete them
2084 /* this is handled by container_content_imsm() */
2085 case MIGR_STATE_CHANGE
:
2086 /* FIXME handle other migrations */
2088 /* we are not dirty, so... */
2089 info
->resync_start
= MaxSector
;
2093 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
2094 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
2096 info
->array
.major_version
= -1;
2097 info
->array
.minor_version
= -2;
2098 devname
= devnum2devname(st
->container_dev
);
2099 *info
->text_version
= '\0';
2101 sprintf(info
->text_version
, "/%s/%d", devname
, info
->container_member
);
2103 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
2104 uuid_from_super_imsm(st
, info
->uuid
);
2108 for (i
=0; i
<map_disks
; i
++) {
2110 if (i
< info
->array
.raid_disks
) {
2111 struct imsm_disk
*dsk
;
2112 j
= get_imsm_disk_idx(dev
, i
, -1);
2113 dsk
= get_imsm_disk(super
, j
);
2114 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
2121 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
, int failed
);
2122 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
);
2124 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
2128 for (d
= super
->missing
; d
; d
= d
->next
)
2129 if (d
->index
== index
)
2134 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
2136 struct intel_super
*super
= st
->sb
;
2137 struct imsm_disk
*disk
;
2138 int map_disks
= info
->array
.raid_disks
;
2139 int max_enough
= -1;
2141 struct imsm_super
*mpb
;
2143 if (super
->current_vol
>= 0) {
2144 getinfo_super_imsm_volume(st
, info
, map
);
2147 memset(info
, 0, sizeof(*info
));
2149 /* Set raid_disks to zero so that Assemble will always pull in valid
2152 info
->array
.raid_disks
= 0;
2153 info
->array
.level
= LEVEL_CONTAINER
;
2154 info
->array
.layout
= 0;
2155 info
->array
.md_minor
= -1;
2156 info
->array
.ctime
= 0; /* N/A for imsm */
2157 info
->array
.utime
= 0;
2158 info
->array
.chunk_size
= 0;
2160 info
->disk
.major
= 0;
2161 info
->disk
.minor
= 0;
2162 info
->disk
.raid_disk
= -1;
2163 info
->reshape_active
= 0;
2164 info
->array
.major_version
= -1;
2165 info
->array
.minor_version
= -2;
2166 strcpy(info
->text_version
, "imsm");
2167 info
->safe_mode_delay
= 0;
2168 info
->disk
.number
= -1;
2169 info
->disk
.state
= 0;
2171 info
->recovery_start
= MaxSector
;
2173 /* do we have the all the insync disks that we expect? */
2174 mpb
= super
->anchor
;
2176 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
2177 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
2178 int failed
, enough
, j
, missing
= 0;
2179 struct imsm_map
*map
;
2182 failed
= imsm_count_failed(super
, dev
);
2183 state
= imsm_check_degraded(super
, dev
, failed
);
2184 map
= get_imsm_map(dev
, dev
->vol
.migr_state
);
2186 /* any newly missing disks?
2187 * (catches single-degraded vs double-degraded)
2189 for (j
= 0; j
< map
->num_members
; j
++) {
2190 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, -1);
2191 __u32 idx
= ord_to_idx(ord
);
2193 if (!(ord
& IMSM_ORD_REBUILD
) &&
2194 get_imsm_missing(super
, idx
)) {
2200 if (state
== IMSM_T_STATE_FAILED
)
2202 else if (state
== IMSM_T_STATE_DEGRADED
&&
2203 (state
!= map
->map_state
|| missing
))
2205 else /* we're normal, or already degraded */
2208 /* in the missing/failed disk case check to see
2209 * if at least one array is runnable
2211 max_enough
= max(max_enough
, enough
);
2213 dprintf("%s: enough: %d\n", __func__
, max_enough
);
2214 info
->container_enough
= max_enough
;
2217 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
2219 disk
= &super
->disks
->disk
;
2220 info
->data_offset
= __le32_to_cpu(disk
->total_blocks
) - reserved
;
2221 info
->component_size
= reserved
;
2222 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
2223 /* we don't change info->disk.raid_disk here because
2224 * this state will be finalized in mdmon after we have
2225 * found the 'most fresh' version of the metadata
2227 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
2228 info
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
2231 /* only call uuid_from_super_imsm when this disk is part of a populated container,
2232 * ->compare_super may have updated the 'num_raid_devs' field for spares
2234 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
2235 uuid_from_super_imsm(st
, info
->uuid
);
2237 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
2239 /* I don't know how to compute 'map' on imsm, so use safe default */
2242 for (i
= 0; i
< map_disks
; i
++)
2248 /* allocates memory and fills disk in mdinfo structure
2249 * for each disk in array */
2250 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
2252 struct mdinfo
*mddev
= NULL
;
2253 struct intel_super
*super
= st
->sb
;
2254 struct imsm_disk
*disk
;
2257 if (!super
|| !super
->disks
)
2260 mddev
= malloc(sizeof(*mddev
));
2262 fprintf(stderr
, Name
": Failed to allocate memory.\n");
2265 memset(mddev
, 0, sizeof(*mddev
));
2269 tmp
= malloc(sizeof(*tmp
));
2271 fprintf(stderr
, Name
": Failed to allocate memory.\n");
2276 memset(tmp
, 0, sizeof(*tmp
));
2278 tmp
->next
= mddev
->devs
;
2280 tmp
->disk
.number
= count
++;
2281 tmp
->disk
.major
= dl
->major
;
2282 tmp
->disk
.minor
= dl
->minor
;
2283 tmp
->disk
.state
= is_configured(disk
) ?
2284 (1 << MD_DISK_ACTIVE
) : 0;
2285 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
2286 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
2287 tmp
->disk
.raid_disk
= -1;
2293 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
2294 char *update
, char *devname
, int verbose
,
2295 int uuid_set
, char *homehost
)
2297 /* For 'assemble' and 'force' we need to return non-zero if any
2298 * change was made. For others, the return value is ignored.
2299 * Update options are:
2300 * force-one : This device looks a bit old but needs to be included,
2301 * update age info appropriately.
2302 * assemble: clear any 'faulty' flag to allow this device to
2304 * force-array: Array is degraded but being forced, mark it clean
2305 * if that will be needed to assemble it.
2307 * newdev: not used ????
2308 * grow: Array has gained a new device - this is currently for
2310 * resync: mark as dirty so a resync will happen.
2311 * name: update the name - preserving the homehost
2312 * uuid: Change the uuid of the array to match watch is given
2314 * Following are not relevant for this imsm:
2315 * sparc2.2 : update from old dodgey metadata
2316 * super-minor: change the preferred_minor number
2317 * summaries: update redundant counters.
2318 * homehost: update the recorded homehost
2319 * _reshape_progress: record new reshape_progress position.
2322 struct intel_super
*super
= st
->sb
;
2323 struct imsm_super
*mpb
;
2325 /* we can only update container info */
2326 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
2329 mpb
= super
->anchor
;
2331 if (strcmp(update
, "uuid") == 0 && uuid_set
&& !info
->update_private
)
2333 else if (strcmp(update
, "uuid") == 0 && uuid_set
&& info
->update_private
) {
2334 mpb
->orig_family_num
= *((__u32
*) info
->update_private
);
2336 } else if (strcmp(update
, "uuid") == 0) {
2337 __u32
*new_family
= malloc(sizeof(*new_family
));
2339 /* update orig_family_number with the incoming random
2340 * data, report the new effective uuid, and store the
2341 * new orig_family_num for future updates.
2344 memcpy(&mpb
->orig_family_num
, info
->uuid
, sizeof(__u32
));
2345 uuid_from_super_imsm(st
, info
->uuid
);
2346 *new_family
= mpb
->orig_family_num
;
2347 info
->update_private
= new_family
;
2350 } else if (strcmp(update
, "assemble") == 0)
2355 /* successful update? recompute checksum */
2357 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
2362 static size_t disks_to_mpb_size(int disks
)
2366 size
= sizeof(struct imsm_super
);
2367 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
2368 size
+= 2 * sizeof(struct imsm_dev
);
2369 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
2370 size
+= (4 - 2) * sizeof(struct imsm_map
);
2371 /* 4 possible disk_ord_tbl's */
2372 size
+= 4 * (disks
- 1) * sizeof(__u32
);
2377 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
)
2379 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
2382 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
2385 static void free_devlist(struct intel_super
*super
)
2387 struct intel_dev
*dv
;
2389 while (super
->devlist
) {
2390 dv
= super
->devlist
->next
;
2391 free(super
->devlist
->dev
);
2392 free(super
->devlist
);
2393 super
->devlist
= dv
;
2397 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
2399 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
2402 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
)
2406 * 0 same, or first was empty, and second was copied
2407 * 1 second had wrong number
2409 * 3 wrong other info
2411 struct intel_super
*first
= st
->sb
;
2412 struct intel_super
*sec
= tst
->sb
;
2419 /* in platform dependent environment test if the disks
2420 * use the same Intel hba
2422 if (!check_env("IMSM_NO_PLATFORM")) {
2423 if (!first
->hba
|| !sec
->hba
||
2424 (first
->hba
->type
!= sec
->hba
->type
)) {
2426 "HBAs of devices does not match %s != %s\n",
2427 first
->hba
? get_sys_dev_type(first
->hba
->type
) : NULL
,
2428 sec
->hba
? get_sys_dev_type(sec
->hba
->type
) : NULL
);
2433 /* if an anchor does not have num_raid_devs set then it is a free
2436 if (first
->anchor
->num_raid_devs
> 0 &&
2437 sec
->anchor
->num_raid_devs
> 0) {
2438 /* Determine if these disks might ever have been
2439 * related. Further disambiguation can only take place
2440 * in load_super_imsm_all
2442 __u32 first_family
= first
->anchor
->orig_family_num
;
2443 __u32 sec_family
= sec
->anchor
->orig_family_num
;
2445 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
2446 MAX_SIGNATURE_LENGTH
) != 0)
2449 if (first_family
== 0)
2450 first_family
= first
->anchor
->family_num
;
2451 if (sec_family
== 0)
2452 sec_family
= sec
->anchor
->family_num
;
2454 if (first_family
!= sec_family
)
2460 /* if 'first' is a spare promote it to a populated mpb with sec's
2463 if (first
->anchor
->num_raid_devs
== 0 &&
2464 sec
->anchor
->num_raid_devs
> 0) {
2466 struct intel_dev
*dv
;
2467 struct imsm_dev
*dev
;
2469 /* we need to copy raid device info from sec if an allocation
2470 * fails here we don't associate the spare
2472 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++) {
2473 dv
= malloc(sizeof(*dv
));
2476 dev
= malloc(sizeof_imsm_dev(get_imsm_dev(sec
, i
), 1));
2483 dv
->next
= first
->devlist
;
2484 first
->devlist
= dv
;
2486 if (i
< sec
->anchor
->num_raid_devs
) {
2487 /* allocation failure */
2488 free_devlist(first
);
2489 fprintf(stderr
, "imsm: failed to associate spare\n");
2492 first
->anchor
->num_raid_devs
= sec
->anchor
->num_raid_devs
;
2493 first
->anchor
->orig_family_num
= sec
->anchor
->orig_family_num
;
2494 first
->anchor
->family_num
= sec
->anchor
->family_num
;
2495 memcpy(first
->anchor
->sig
, sec
->anchor
->sig
, MAX_SIGNATURE_LENGTH
);
2496 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++)
2497 imsm_copy_dev(get_imsm_dev(first
, i
), get_imsm_dev(sec
, i
));
2503 static void fd2devname(int fd
, char *name
)
2507 char dname
[PATH_MAX
];
2512 if (fstat(fd
, &st
) != 0)
2514 sprintf(path
, "/sys/dev/block/%d:%d",
2515 major(st
.st_rdev
), minor(st
.st_rdev
));
2517 rv
= readlink(path
, dname
, sizeof(dname
));
2522 nm
= strrchr(dname
, '/');
2524 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
2527 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
2529 static int imsm_read_serial(int fd
, char *devname
,
2530 __u8 serial
[MAX_RAID_SERIAL_LEN
])
2532 unsigned char scsi_serial
[255];
2541 memset(scsi_serial
, 0, sizeof(scsi_serial
));
2543 rv
= scsi_get_serial(fd
, scsi_serial
, sizeof(scsi_serial
));
2545 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
2546 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
2547 fd2devname(fd
, (char *) serial
);
2554 Name
": Failed to retrieve serial for %s\n",
2559 rsp_len
= scsi_serial
[3];
2563 Name
": Failed to retrieve serial for %s\n",
2567 rsp_buf
= (char *) &scsi_serial
[4];
2569 /* trim all whitespace and non-printable characters and convert
2572 for (i
= 0, dest
= rsp_buf
; i
< rsp_len
; i
++) {
2575 /* ':' is reserved for use in placeholder serial
2576 * numbers for missing disks
2584 len
= dest
- rsp_buf
;
2587 /* truncate leading characters */
2588 if (len
> MAX_RAID_SERIAL_LEN
) {
2589 dest
+= len
- MAX_RAID_SERIAL_LEN
;
2590 len
= MAX_RAID_SERIAL_LEN
;
2593 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
2594 memcpy(serial
, dest
, len
);
2599 static int serialcmp(__u8
*s1
, __u8
*s2
)
2601 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
2604 static void serialcpy(__u8
*dest
, __u8
*src
)
2606 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
2610 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
2614 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
2615 if (serialcmp(dl
->serial
, serial
) == 0)
2622 static struct imsm_disk
*
2623 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
2627 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2628 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
2630 if (serialcmp(disk
->serial
, serial
) == 0) {
2641 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
2643 struct imsm_disk
*disk
;
2648 __u8 serial
[MAX_RAID_SERIAL_LEN
];
2650 rv
= imsm_read_serial(fd
, devname
, serial
);
2655 dl
= calloc(1, sizeof(*dl
));
2659 Name
": failed to allocate disk buffer for %s\n",
2665 dl
->major
= major(stb
.st_rdev
);
2666 dl
->minor
= minor(stb
.st_rdev
);
2667 dl
->next
= super
->disks
;
2668 dl
->fd
= keep_fd
? fd
: -1;
2669 assert(super
->disks
== NULL
);
2671 serialcpy(dl
->serial
, serial
);
2674 fd2devname(fd
, name
);
2676 dl
->devname
= strdup(devname
);
2678 dl
->devname
= strdup(name
);
2680 /* look up this disk's index in the current anchor */
2681 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
2684 /* only set index on disks that are a member of a
2685 * populated contianer, i.e. one with raid_devs
2687 if (is_failed(&dl
->disk
))
2689 else if (is_spare(&dl
->disk
))
2697 /* When migrating map0 contains the 'destination' state while map1
2698 * contains the current state. When not migrating map0 contains the
2699 * current state. This routine assumes that map[0].map_state is set to
2700 * the current array state before being called.
2702 * Migration is indicated by one of the following states
2703 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
2704 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
2705 * map1state=unitialized)
2706 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
2708 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
2709 * map1state=degraded)
2710 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
2713 static void migrate(struct imsm_dev
*dev
, struct intel_super
*super
,
2714 __u8 to_state
, int migr_type
)
2716 struct imsm_map
*dest
;
2717 struct imsm_map
*src
= get_imsm_map(dev
, 0);
2719 dev
->vol
.migr_state
= 1;
2720 set_migr_type(dev
, migr_type
);
2721 dev
->vol
.curr_migr_unit
= 0;
2722 dest
= get_imsm_map(dev
, 1);
2724 /* duplicate and then set the target end state in map[0] */
2725 memcpy(dest
, src
, sizeof_imsm_map(src
));
2726 if ((migr_type
== MIGR_REBUILD
) ||
2727 (migr_type
== MIGR_GEN_MIGR
)) {
2731 for (i
= 0; i
< src
->num_members
; i
++) {
2732 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
2733 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
2737 if (migr_type
== MIGR_GEN_MIGR
)
2738 /* Clear migration record */
2739 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
2741 src
->map_state
= to_state
;
2744 static void end_migration(struct imsm_dev
*dev
, __u8 map_state
)
2746 struct imsm_map
*map
= get_imsm_map(dev
, 0);
2747 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
);
2750 /* merge any IMSM_ORD_REBUILD bits that were not successfully
2751 * completed in the last migration.
2753 * FIXME add support for raid-level-migration
2755 for (i
= 0; i
< prev
->num_members
; i
++)
2756 for (j
= 0; j
< map
->num_members
; j
++)
2757 /* during online capacity expansion
2758 * disks position can be changed if takeover is used
2760 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
2761 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
2762 map
->disk_ord_tbl
[j
] |= prev
->disk_ord_tbl
[i
];
2766 dev
->vol
.migr_state
= 0;
2767 dev
->vol
.migr_type
= 0;
2768 dev
->vol
.curr_migr_unit
= 0;
2769 map
->map_state
= map_state
;
2773 static int parse_raid_devices(struct intel_super
*super
)
2776 struct imsm_dev
*dev_new
;
2777 size_t len
, len_migr
;
2779 size_t space_needed
= 0;
2780 struct imsm_super
*mpb
= super
->anchor
;
2782 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
2783 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
2784 struct intel_dev
*dv
;
2786 len
= sizeof_imsm_dev(dev_iter
, 0);
2787 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
2789 space_needed
+= len_migr
- len
;
2791 dv
= malloc(sizeof(*dv
));
2794 if (max_len
< len_migr
)
2796 if (max_len
> len_migr
)
2797 space_needed
+= max_len
- len_migr
;
2798 dev_new
= malloc(max_len
);
2803 imsm_copy_dev(dev_new
, dev_iter
);
2806 dv
->next
= super
->devlist
;
2807 super
->devlist
= dv
;
2810 /* ensure that super->buf is large enough when all raid devices
2813 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
2816 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
, 512);
2817 if (posix_memalign(&buf
, 512, len
) != 0)
2820 memcpy(buf
, super
->buf
, super
->len
);
2821 memset(buf
+ super
->len
, 0, len
- super
->len
);
2830 /* retrieve a pointer to the bbm log which starts after all raid devices */
2831 struct bbm_log
*__get_imsm_bbm_log(struct imsm_super
*mpb
)
2835 if (__le32_to_cpu(mpb
->bbm_log_size
)) {
2837 ptr
+= mpb
->mpb_size
- __le32_to_cpu(mpb
->bbm_log_size
);
2843 /*******************************************************************************
2844 * Function: check_mpb_migr_compatibility
2845 * Description: Function checks for unsupported migration features:
2846 * - migration optimization area (pba_of_lba0)
2847 * - descending reshape (ascending_migr)
2849 * super : imsm metadata information
2851 * 0 : migration is compatible
2852 * -1 : migration is not compatible
2853 ******************************************************************************/
2854 int check_mpb_migr_compatibility(struct intel_super
*super
)
2856 struct imsm_map
*map0
, *map1
;
2857 struct migr_record
*migr_rec
= super
->migr_rec
;
2860 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
2861 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
2864 dev_iter
->vol
.migr_state
== 1 &&
2865 dev_iter
->vol
.migr_type
== MIGR_GEN_MIGR
) {
2866 /* This device is migrating */
2867 map0
= get_imsm_map(dev_iter
, 0);
2868 map1
= get_imsm_map(dev_iter
, 1);
2869 if (map0
->pba_of_lba0
!= map1
->pba_of_lba0
)
2870 /* migration optimization area was used */
2872 if (migr_rec
->ascending_migr
== 0
2873 && migr_rec
->dest_depth_per_unit
> 0)
2874 /* descending reshape not supported yet */
2881 static void __free_imsm(struct intel_super
*super
, int free_disks
);
2883 /* load_imsm_mpb - read matrix metadata
2884 * allocates super->mpb to be freed by free_imsm
2886 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
2888 unsigned long long dsize
;
2889 unsigned long long sectors
;
2891 struct imsm_super
*anchor
;
2894 get_dev_size(fd
, NULL
, &dsize
);
2898 Name
": %s: device to small for imsm\n",
2903 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0) {
2906 Name
": Cannot seek to anchor block on %s: %s\n",
2907 devname
, strerror(errno
));
2911 if (posix_memalign((void**)&anchor
, 512, 512) != 0) {
2914 Name
": Failed to allocate imsm anchor buffer"
2915 " on %s\n", devname
);
2918 if (read(fd
, anchor
, 512) != 512) {
2921 Name
": Cannot read anchor block on %s: %s\n",
2922 devname
, strerror(errno
));
2927 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
2930 Name
": no IMSM anchor on %s\n", devname
);
2935 __free_imsm(super
, 0);
2936 /* reload capability and hba */
2938 /* capability and hba must be updated with new super allocation */
2939 find_intel_hba_capability(fd
, super
, devname
);
2940 super
->len
= ROUND_UP(anchor
->mpb_size
, 512);
2941 if (posix_memalign(&super
->buf
, 512, super
->len
) != 0) {
2944 Name
": unable to allocate %zu byte mpb buffer\n",
2949 memcpy(super
->buf
, anchor
, 512);
2951 sectors
= mpb_sectors(anchor
) - 1;
2954 if (posix_memalign(&super
->migr_rec_buf
, 512, 512) != 0) {
2955 fprintf(stderr
, Name
2956 ": %s could not allocate migr_rec buffer\n", __func__
);
2962 check_sum
= __gen_imsm_checksum(super
->anchor
);
2963 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
2966 Name
": IMSM checksum %x != %x on %s\n",
2968 __le32_to_cpu(super
->anchor
->check_sum
),
2976 /* read the extended mpb */
2977 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0) {
2980 Name
": Cannot seek to extended mpb on %s: %s\n",
2981 devname
, strerror(errno
));
2985 if ((unsigned)read(fd
, super
->buf
+ 512, super
->len
- 512) != super
->len
- 512) {
2988 Name
": Cannot read extended mpb on %s: %s\n",
2989 devname
, strerror(errno
));
2993 check_sum
= __gen_imsm_checksum(super
->anchor
);
2994 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
2997 Name
": IMSM checksum %x != %x on %s\n",
2998 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
3003 /* FIXME the BBM log is disk specific so we cannot use this global
3004 * buffer for all disks. Ok for now since we only look at the global
3005 * bbm_log_size parameter to gate assembly
3007 super
->bbm_log
= __get_imsm_bbm_log(super
->anchor
);
3012 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
);
3015 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3019 err
= load_imsm_mpb(fd
, super
, devname
);
3022 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
3025 err
= parse_raid_devices(super
);
3030 static void __free_imsm_disk(struct dl
*d
)
3042 static void free_imsm_disks(struct intel_super
*super
)
3046 while (super
->disks
) {
3048 super
->disks
= d
->next
;
3049 __free_imsm_disk(d
);
3051 while (super
->disk_mgmt_list
) {
3052 d
= super
->disk_mgmt_list
;
3053 super
->disk_mgmt_list
= d
->next
;
3054 __free_imsm_disk(d
);
3056 while (super
->missing
) {
3058 super
->missing
= d
->next
;
3059 __free_imsm_disk(d
);
3064 /* free all the pieces hanging off of a super pointer */
3065 static void __free_imsm(struct intel_super
*super
, int free_disks
)
3067 struct intel_hba
*elem
, *next
;
3073 /* unlink capability description */
3075 if (super
->migr_rec_buf
) {
3076 free(super
->migr_rec_buf
);
3077 super
->migr_rec_buf
= NULL
;
3080 free_imsm_disks(super
);
3081 free_devlist(super
);
3085 free((void *)elem
->path
);
3093 static void free_imsm(struct intel_super
*super
)
3095 __free_imsm(super
, 1);
3099 static void free_super_imsm(struct supertype
*st
)
3101 struct intel_super
*super
= st
->sb
;
3110 static struct intel_super
*alloc_super(void)
3112 struct intel_super
*super
= malloc(sizeof(*super
));
3115 memset(super
, 0, sizeof(*super
));
3116 super
->current_vol
= -1;
3117 super
->create_offset
= ~((__u32
) 0);
3123 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
3125 static int find_intel_hba_capability(int fd
, struct intel_super
*super
, char *devname
)
3127 struct sys_dev
*hba_name
;
3130 if ((fd
< 0) || check_env("IMSM_NO_PLATFORM")) {
3135 hba_name
= find_disk_attached_hba(fd
, NULL
);
3139 Name
": %s is not attached to Intel(R) RAID controller.\n",
3143 rv
= attach_hba_to_super(super
, hba_name
);
3146 struct intel_hba
*hba
= super
->hba
;
3148 fprintf(stderr
, Name
": %s is attached to Intel(R) %s RAID "
3149 "controller (%s),\n"
3150 " but the container is assigned to Intel(R) "
3151 "%s RAID controller (",
3154 hba_name
->pci_id
? : "Err!",
3155 get_sys_dev_type(hba_name
->type
));
3158 fprintf(stderr
, "%s", hba
->pci_id
? : "Err!");
3160 fprintf(stderr
, ", ");
3164 fprintf(stderr
, ").\n"
3165 " Mixing devices attached to different controllers "
3166 "is not allowed.\n");
3168 free_sys_dev(&hba_name
);
3171 super
->orom
= find_imsm_capability(hba_name
->type
);
3172 free_sys_dev(&hba_name
);
3179 /* find_missing - helper routine for load_super_imsm_all that identifies
3180 * disks that have disappeared from the system. This routine relies on
3181 * the mpb being uptodate, which it is at load time.
3183 static int find_missing(struct intel_super
*super
)
3186 struct imsm_super
*mpb
= super
->anchor
;
3188 struct imsm_disk
*disk
;
3190 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3191 disk
= __get_imsm_disk(mpb
, i
);
3192 dl
= serial_to_dl(disk
->serial
, super
);
3196 dl
= malloc(sizeof(*dl
));
3202 dl
->devname
= strdup("missing");
3204 serialcpy(dl
->serial
, disk
->serial
);
3207 dl
->next
= super
->missing
;
3208 super
->missing
= dl
;
3214 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
3216 struct intel_disk
*idisk
= disk_list
;
3219 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
3221 idisk
= idisk
->next
;
3227 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
3228 struct intel_super
*super
,
3229 struct intel_disk
**disk_list
)
3231 struct imsm_disk
*d
= &super
->disks
->disk
;
3232 struct imsm_super
*mpb
= super
->anchor
;
3235 for (i
= 0; i
< tbl_size
; i
++) {
3236 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
3237 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
3239 if (tbl_mpb
->family_num
== mpb
->family_num
) {
3240 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
3241 dprintf("%s: mpb from %d:%d matches %d:%d\n",
3242 __func__
, super
->disks
->major
,
3243 super
->disks
->minor
,
3244 table
[i
]->disks
->major
,
3245 table
[i
]->disks
->minor
);
3249 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
3250 is_configured(d
) == is_configured(tbl_d
)) &&
3251 tbl_mpb
->generation_num
< mpb
->generation_num
) {
3252 /* current version of the mpb is a
3253 * better candidate than the one in
3254 * super_table, but copy over "cross
3255 * generational" status
3257 struct intel_disk
*idisk
;
3259 dprintf("%s: mpb from %d:%d replaces %d:%d\n",
3260 __func__
, super
->disks
->major
,
3261 super
->disks
->minor
,
3262 table
[i
]->disks
->major
,
3263 table
[i
]->disks
->minor
);
3265 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
3266 if (idisk
&& is_failed(&idisk
->disk
))
3267 tbl_d
->status
|= FAILED_DISK
;
3270 struct intel_disk
*idisk
;
3271 struct imsm_disk
*disk
;
3273 /* tbl_mpb is more up to date, but copy
3274 * over cross generational status before
3277 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
3278 if (disk
&& is_failed(disk
))
3279 d
->status
|= FAILED_DISK
;
3281 idisk
= disk_list_get(d
->serial
, *disk_list
);
3284 if (disk
&& is_configured(disk
))
3285 idisk
->disk
.status
|= CONFIGURED_DISK
;
3288 dprintf("%s: mpb from %d:%d prefer %d:%d\n",
3289 __func__
, super
->disks
->major
,
3290 super
->disks
->minor
,
3291 table
[i
]->disks
->major
,
3292 table
[i
]->disks
->minor
);
3300 table
[tbl_size
++] = super
;
3304 /* update/extend the merged list of imsm_disk records */
3305 for (j
= 0; j
< mpb
->num_disks
; j
++) {
3306 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
3307 struct intel_disk
*idisk
;
3309 idisk
= disk_list_get(disk
->serial
, *disk_list
);
3311 idisk
->disk
.status
|= disk
->status
;
3312 if (is_configured(&idisk
->disk
) ||
3313 is_failed(&idisk
->disk
))
3314 idisk
->disk
.status
&= ~(SPARE_DISK
);
3316 idisk
= calloc(1, sizeof(*idisk
));
3319 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
3320 idisk
->disk
= *disk
;
3321 idisk
->next
= *disk_list
;
3325 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
3332 static struct intel_super
*
3333 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
3336 struct imsm_super
*mpb
= super
->anchor
;
3340 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3341 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3342 struct intel_disk
*idisk
;
3344 idisk
= disk_list_get(disk
->serial
, disk_list
);
3346 if (idisk
->owner
== owner
||
3347 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
3350 dprintf("%s: '%.16s' owner %d != %d\n",
3351 __func__
, disk
->serial
, idisk
->owner
,
3354 dprintf("%s: unknown disk %x [%d]: %.16s\n",
3355 __func__
, __le32_to_cpu(mpb
->family_num
), i
,
3361 if (ok_count
== mpb
->num_disks
)
3366 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
3368 struct intel_super
*s
;
3370 for (s
= super_list
; s
; s
= s
->next
) {
3371 if (family_num
!= s
->anchor
->family_num
)
3373 fprintf(stderr
, "Conflict, offlining family %#x on '%s'\n",
3374 __le32_to_cpu(family_num
), s
->disks
->devname
);
3378 static struct intel_super
*
3379 imsm_thunderdome(struct intel_super
**super_list
, int len
)
3381 struct intel_super
*super_table
[len
];
3382 struct intel_disk
*disk_list
= NULL
;
3383 struct intel_super
*champion
, *spare
;
3384 struct intel_super
*s
, **del
;
3389 memset(super_table
, 0, sizeof(super_table
));
3390 for (s
= *super_list
; s
; s
= s
->next
)
3391 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
3393 for (i
= 0; i
< tbl_size
; i
++) {
3394 struct imsm_disk
*d
;
3395 struct intel_disk
*idisk
;
3396 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
3399 d
= &s
->disks
->disk
;
3401 /* 'd' must appear in merged disk list for its
3402 * configuration to be valid
3404 idisk
= disk_list_get(d
->serial
, disk_list
);
3405 if (idisk
&& idisk
->owner
== i
)
3406 s
= validate_members(s
, disk_list
, i
);
3411 dprintf("%s: marking family: %#x from %d:%d offline\n",
3412 __func__
, mpb
->family_num
,
3413 super_table
[i
]->disks
->major
,
3414 super_table
[i
]->disks
->minor
);
3418 /* This is where the mdadm implementation differs from the Windows
3419 * driver which has no strict concept of a container. We can only
3420 * assemble one family from a container, so when returning a prodigal
3421 * array member to this system the code will not be able to disambiguate
3422 * the container contents that should be assembled ("foreign" versus
3423 * "local"). It requires user intervention to set the orig_family_num
3424 * to a new value to establish a new container. The Windows driver in
3425 * this situation fixes up the volume name in place and manages the
3426 * foreign array as an independent entity.
3431 for (i
= 0; i
< tbl_size
; i
++) {
3432 struct intel_super
*tbl_ent
= super_table
[i
];
3438 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
3443 if (s
&& !is_spare
) {
3444 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
3446 } else if (!s
&& !is_spare
)
3459 fprintf(stderr
, "Chose family %#x on '%s', "
3460 "assemble conflicts to new container with '--update=uuid'\n",
3461 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
3463 /* collect all dl's onto 'champion', and update them to
3464 * champion's version of the status
3466 for (s
= *super_list
; s
; s
= s
->next
) {
3467 struct imsm_super
*mpb
= champion
->anchor
;
3468 struct dl
*dl
= s
->disks
;
3473 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3474 struct imsm_disk
*disk
;
3476 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
3479 /* only set index on disks that are a member of
3480 * a populated contianer, i.e. one with
3483 if (is_failed(&dl
->disk
))
3485 else if (is_spare(&dl
->disk
))
3491 if (i
>= mpb
->num_disks
) {
3492 struct intel_disk
*idisk
;
3494 idisk
= disk_list_get(dl
->serial
, disk_list
);
3495 if (idisk
&& is_spare(&idisk
->disk
) &&
3496 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
3504 dl
->next
= champion
->disks
;
3505 champion
->disks
= dl
;
3509 /* delete 'champion' from super_list */
3510 for (del
= super_list
; *del
; ) {
3511 if (*del
== champion
) {
3512 *del
= (*del
)->next
;
3515 del
= &(*del
)->next
;
3517 champion
->next
= NULL
;
3521 struct intel_disk
*idisk
= disk_list
;
3523 disk_list
= disk_list
->next
;
3530 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
3534 struct intel_super
*super_list
= NULL
;
3535 struct intel_super
*super
= NULL
;
3536 int devnum
= fd2devnum(fd
);
3542 /* check if 'fd' an opened container */
3543 sra
= sysfs_read(fd
, 0, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
3547 if (sra
->array
.major_version
!= -1 ||
3548 sra
->array
.minor_version
!= -2 ||
3549 strcmp(sra
->text_version
, "imsm") != 0) {
3554 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
3555 struct intel_super
*s
= alloc_super();
3563 s
->next
= super_list
;
3567 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
3568 dfd
= dev_open(nm
, O_RDWR
);
3572 rv
= find_intel_hba_capability(dfd
, s
, devname
);
3573 /* no orom/efi or non-intel hba of the disk */
3577 err
= load_and_parse_mpb(dfd
, s
, NULL
, 1);
3579 /* retry the load if we might have raced against mdmon */
3580 if (err
== 3 && mdmon_running(devnum
))
3581 for (retry
= 0; retry
< 3; retry
++) {
3583 err
= load_and_parse_mpb(dfd
, s
, NULL
, 1);
3591 /* all mpbs enter, maybe one leaves */
3592 super
= imsm_thunderdome(&super_list
, i
);
3598 if (find_missing(super
) != 0) {
3604 /* load migration record */
3605 err
= load_imsm_migr_rec(super
, NULL
);
3611 /* Check migration compatibility */
3612 if (check_mpb_migr_compatibility(super
) != 0) {
3613 fprintf(stderr
, Name
": Unsupported migration detected");
3615 fprintf(stderr
, " on %s\n", devname
);
3617 fprintf(stderr
, " (IMSM).\n");
3626 while (super_list
) {
3627 struct intel_super
*s
= super_list
;
3629 super_list
= super_list
->next
;
3638 st
->container_dev
= devnum
;
3639 if (err
== 0 && st
->ss
== NULL
) {
3640 st
->ss
= &super_imsm
;
3641 st
->minor_version
= 0;
3642 st
->max_devs
= IMSM_MAX_DEVICES
;
3647 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
3649 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
);
3653 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
3655 struct intel_super
*super
;
3658 if (test_partition(fd
))
3659 /* IMSM not allowed on partitions */
3662 free_super_imsm(st
);
3664 super
= alloc_super();
3667 Name
": malloc of %zu failed.\n",
3671 /* Load hba and capabilities if they exist.
3672 * But do not preclude loading metadata in case capabilities or hba are
3673 * non-compliant and ignore_hw_compat is set.
3675 rv
= find_intel_hba_capability(fd
, super
, devname
);
3676 /* no orom/efi or non-intel hba of the disk */
3677 if ((rv
!= 0) && (st
->ignore_hw_compat
== 0)) {
3680 Name
": No OROM/EFI properties for %s\n", devname
);
3684 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
3689 Name
": Failed to load all information "
3690 "sections on %s\n", devname
);
3696 if (st
->ss
== NULL
) {
3697 st
->ss
= &super_imsm
;
3698 st
->minor_version
= 0;
3699 st
->max_devs
= IMSM_MAX_DEVICES
;
3702 /* load migration record */
3703 load_imsm_migr_rec(super
, NULL
);
3705 /* Check for unsupported migration features */
3706 if (check_mpb_migr_compatibility(super
) != 0) {
3707 fprintf(stderr
, Name
": Unsupported migration detected");
3709 fprintf(stderr
, " on %s\n", devname
);
3711 fprintf(stderr
, " (IMSM).\n");
3718 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
3720 if (info
->level
== 1)
3722 return info
->chunk_size
>> 9;
3725 static __u32
info_to_num_data_stripes(mdu_array_info_t
*info
, int num_domains
)
3729 num_stripes
= (info
->size
* 2) / info_to_blocks_per_strip(info
);
3730 num_stripes
/= num_domains
;
3735 static __u32
info_to_blocks_per_member(mdu_array_info_t
*info
)
3737 if (info
->level
== 1)
3738 return info
->size
* 2;
3740 return (info
->size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
3743 static void imsm_update_version_info(struct intel_super
*super
)
3745 /* update the version and attributes */
3746 struct imsm_super
*mpb
= super
->anchor
;
3748 struct imsm_dev
*dev
;
3749 struct imsm_map
*map
;
3752 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3753 dev
= get_imsm_dev(super
, i
);
3754 map
= get_imsm_map(dev
, 0);
3755 if (__le32_to_cpu(dev
->size_high
) > 0)
3756 mpb
->attributes
|= MPB_ATTRIB_2TB
;
3758 /* FIXME detect when an array spans a port multiplier */
3760 mpb
->attributes
|= MPB_ATTRIB_PM
;
3763 if (mpb
->num_raid_devs
> 1 ||
3764 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
3765 version
= MPB_VERSION_ATTRIBS
;
3766 switch (get_imsm_raid_level(map
)) {
3767 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
3768 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
3769 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
3770 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
3773 if (map
->num_members
>= 5)
3774 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
3775 else if (dev
->status
== DEV_CLONE_N_GO
)
3776 version
= MPB_VERSION_CNG
;
3777 else if (get_imsm_raid_level(map
) == 5)
3778 version
= MPB_VERSION_RAID5
;
3779 else if (map
->num_members
>= 3)
3780 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
3781 else if (get_imsm_raid_level(map
) == 1)
3782 version
= MPB_VERSION_RAID1
;
3784 version
= MPB_VERSION_RAID0
;
3786 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
3790 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
3792 struct imsm_super
*mpb
= super
->anchor
;
3793 char *reason
= NULL
;
3796 if (strlen(name
) > MAX_RAID_SERIAL_LEN
)
3797 reason
= "must be 16 characters or less";
3799 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3800 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3802 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
3803 reason
= "already exists";
3808 if (reason
&& !quiet
)
3809 fprintf(stderr
, Name
": imsm volume name %s\n", reason
);
3814 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
3815 unsigned long long size
, char *name
,
3816 char *homehost
, int *uuid
)
3818 /* We are creating a volume inside a pre-existing container.
3819 * so st->sb is already set.
3821 struct intel_super
*super
= st
->sb
;
3822 struct imsm_super
*mpb
= super
->anchor
;
3823 struct intel_dev
*dv
;
3824 struct imsm_dev
*dev
;
3825 struct imsm_vol
*vol
;
3826 struct imsm_map
*map
;
3827 int idx
= mpb
->num_raid_devs
;
3829 unsigned long long array_blocks
;
3830 size_t size_old
, size_new
;
3831 __u32 num_data_stripes
;
3833 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
3834 fprintf(stderr
, Name
": This imsm-container already has the "
3835 "maximum of %d volumes\n", super
->orom
->vpa
);
3839 /* ensure the mpb is large enough for the new data */
3840 size_old
= __le32_to_cpu(mpb
->mpb_size
);
3841 size_new
= disks_to_mpb_size(info
->nr_disks
);
3842 if (size_new
> size_old
) {
3844 size_t size_round
= ROUND_UP(size_new
, 512);
3846 if (posix_memalign(&mpb_new
, 512, size_round
) != 0) {
3847 fprintf(stderr
, Name
": could not allocate new mpb\n");
3850 if (posix_memalign(&super
->migr_rec_buf
, 512, 512) != 0) {
3851 fprintf(stderr
, Name
3852 ": %s could not allocate migr_rec buffer\n",
3858 memcpy(mpb_new
, mpb
, size_old
);
3861 super
->anchor
= mpb_new
;
3862 mpb
->mpb_size
= __cpu_to_le32(size_new
);
3863 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
3865 super
->current_vol
= idx
;
3866 /* when creating the first raid device in this container set num_disks
3867 * to zero, i.e. delete this spare and add raid member devices in
3868 * add_to_super_imsm_volume()
3870 if (super
->current_vol
== 0)
3873 if (!check_name(super
, name
, 0))
3875 dv
= malloc(sizeof(*dv
));
3877 fprintf(stderr
, Name
": failed to allocate device list entry\n");
3880 dev
= calloc(1, sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
3883 fprintf(stderr
, Name
": could not allocate raid device\n");
3887 strncpy((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
3888 if (info
->level
== 1)
3889 array_blocks
= info_to_blocks_per_member(info
);
3891 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
3892 info
->layout
, info
->chunk_size
,
3894 /* round array size down to closest MB */
3895 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
3897 dev
->size_low
= __cpu_to_le32((__u32
) array_blocks
);
3898 dev
->size_high
= __cpu_to_le32((__u32
) (array_blocks
>> 32));
3899 dev
->status
= (DEV_READ_COALESCING
| DEV_WRITE_COALESCING
);
3901 vol
->migr_state
= 0;
3902 set_migr_type(dev
, MIGR_INIT
);
3904 vol
->curr_migr_unit
= 0;
3905 map
= get_imsm_map(dev
, 0);
3906 map
->pba_of_lba0
= __cpu_to_le32(super
->create_offset
);
3907 map
->blocks_per_member
= __cpu_to_le32(info_to_blocks_per_member(info
));
3908 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
3909 map
->failed_disk_num
= ~0;
3910 map
->map_state
= info
->level
? IMSM_T_STATE_UNINITIALIZED
:
3911 IMSM_T_STATE_NORMAL
;
3914 if (info
->level
== 1 && info
->raid_disks
> 2) {
3917 fprintf(stderr
, Name
": imsm does not support more than 2 disks"
3918 "in a raid1 volume\n");
3922 map
->raid_level
= info
->level
;
3923 if (info
->level
== 10) {
3924 map
->raid_level
= 1;
3925 map
->num_domains
= info
->raid_disks
/ 2;
3926 } else if (info
->level
== 1)
3927 map
->num_domains
= info
->raid_disks
;
3929 map
->num_domains
= 1;
3931 num_data_stripes
= info_to_num_data_stripes(info
, map
->num_domains
);
3932 map
->num_data_stripes
= __cpu_to_le32(num_data_stripes
);
3934 map
->num_members
= info
->raid_disks
;
3935 for (i
= 0; i
< map
->num_members
; i
++) {
3936 /* initialized in add_to_super */
3937 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
3939 mpb
->num_raid_devs
++;
3942 dv
->index
= super
->current_vol
;
3943 dv
->next
= super
->devlist
;
3944 super
->devlist
= dv
;
3946 imsm_update_version_info(super
);
3951 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
3952 unsigned long long size
, char *name
,
3953 char *homehost
, int *uuid
)
3955 /* This is primarily called by Create when creating a new array.
3956 * We will then get add_to_super called for each component, and then
3957 * write_init_super called to write it out to each device.
3958 * For IMSM, Create can create on fresh devices or on a pre-existing
3960 * To create on a pre-existing array a different method will be called.
3961 * This one is just for fresh drives.
3963 struct intel_super
*super
;
3964 struct imsm_super
*mpb
;
3969 return init_super_imsm_volume(st
, info
, size
, name
, homehost
, uuid
);
3972 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
3976 super
= alloc_super();
3977 if (super
&& posix_memalign(&super
->buf
, 512, mpb_size
) != 0) {
3982 fprintf(stderr
, Name
3983 ": %s could not allocate superblock\n", __func__
);
3986 if (posix_memalign(&super
->migr_rec_buf
, 512, 512) != 0) {
3987 fprintf(stderr
, Name
3988 ": %s could not allocate migr_rec buffer\n", __func__
);
3993 memset(super
->buf
, 0, mpb_size
);
3995 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
3999 /* zeroing superblock */
4003 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
4005 version
= (char *) mpb
->sig
;
4006 strcpy(version
, MPB_SIGNATURE
);
4007 version
+= strlen(MPB_SIGNATURE
);
4008 strcpy(version
, MPB_VERSION_RAID0
);
4014 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
4015 int fd
, char *devname
)
4017 struct intel_super
*super
= st
->sb
;
4018 struct imsm_super
*mpb
= super
->anchor
;
4020 struct imsm_dev
*dev
;
4021 struct imsm_map
*map
;
4024 dev
= get_imsm_dev(super
, super
->current_vol
);
4025 map
= get_imsm_map(dev
, 0);
4027 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
4028 fprintf(stderr
, Name
": %s: Cannot add spare devices to IMSM volume\n",
4034 /* we're doing autolayout so grab the pre-marked (in
4035 * validate_geometry) raid_disk
4037 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4038 if (dl
->raiddisk
== dk
->raid_disk
)
4041 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4042 if (dl
->major
== dk
->major
&&
4043 dl
->minor
== dk
->minor
)
4048 fprintf(stderr
, Name
": %s is not a member of the same container\n", devname
);
4052 /* add a pristine spare to the metadata */
4053 if (dl
->index
< 0) {
4054 dl
->index
= super
->anchor
->num_disks
;
4055 super
->anchor
->num_disks
++;
4057 /* Check the device has not already been added */
4058 slot
= get_imsm_disk_slot(map
, dl
->index
);
4060 (get_imsm_ord_tbl_ent(dev
, slot
, -1) & IMSM_ORD_REBUILD
) == 0) {
4061 fprintf(stderr
, Name
": %s has been included in this array twice\n",
4065 set_imsm_ord_tbl_ent(map
, dk
->number
, dl
->index
);
4066 dl
->disk
.status
= CONFIGURED_DISK
;
4068 /* if we are creating the first raid device update the family number */
4069 if (super
->current_vol
== 0) {
4071 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
4072 struct imsm_disk
*_disk
= __get_imsm_disk(mpb
, dl
->index
);
4074 if (!_dev
|| !_disk
) {
4075 fprintf(stderr
, Name
": BUG mpb setup error\n");
4081 sum
+= __gen_imsm_checksum(mpb
);
4082 mpb
->family_num
= __cpu_to_le32(sum
);
4083 mpb
->orig_family_num
= mpb
->family_num
;
4090 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
4091 int fd
, char *devname
)
4093 struct intel_super
*super
= st
->sb
;
4095 unsigned long long size
;
4100 /* If we are on an RAID enabled platform check that the disk is
4101 * attached to the raid controller.
4102 * We do not need to test disks attachment for container based additions,
4103 * they shall be already tested when container was created/assembled.
4105 rv
= find_intel_hba_capability(fd
, super
, devname
);
4106 /* no orom/efi or non-intel hba of the disk */
4108 dprintf("capability: %p fd: %d ret: %d\n",
4109 super
->orom
, fd
, rv
);
4113 if (super
->current_vol
>= 0)
4114 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
4117 dd
= malloc(sizeof(*dd
));
4120 Name
": malloc failed %s:%d.\n", __func__
, __LINE__
);
4123 memset(dd
, 0, sizeof(*dd
));
4124 dd
->major
= major(stb
.st_rdev
);
4125 dd
->minor
= minor(stb
.st_rdev
);
4127 dd
->devname
= devname
? strdup(devname
) : NULL
;
4130 dd
->action
= DISK_ADD
;
4131 rv
= imsm_read_serial(fd
, devname
, dd
->serial
);
4134 Name
": failed to retrieve scsi serial, aborting\n");
4139 get_dev_size(fd
, NULL
, &size
);
4141 serialcpy(dd
->disk
.serial
, dd
->serial
);
4142 dd
->disk
.total_blocks
= __cpu_to_le32(size
);
4143 dd
->disk
.status
= SPARE_DISK
;
4144 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
4145 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
4147 dd
->disk
.scsi_id
= __cpu_to_le32(0);
4149 if (st
->update_tail
) {
4150 dd
->next
= super
->disk_mgmt_list
;
4151 super
->disk_mgmt_list
= dd
;
4153 dd
->next
= super
->disks
;
4155 super
->updates_pending
++;
4162 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
4164 struct intel_super
*super
= st
->sb
;
4167 /* remove from super works only in mdmon - for communication
4168 * manager - monitor. Check if communication memory buffer
4171 if (!st
->update_tail
) {
4173 Name
": %s shall be used in mdmon context only"
4174 "(line %d).\n", __func__
, __LINE__
);
4177 dd
= malloc(sizeof(*dd
));
4180 Name
": malloc failed %s:%d.\n", __func__
, __LINE__
);
4183 memset(dd
, 0, sizeof(*dd
));
4184 dd
->major
= dk
->major
;
4185 dd
->minor
= dk
->minor
;
4188 dd
->disk
.status
= SPARE_DISK
;
4189 dd
->action
= DISK_REMOVE
;
4191 dd
->next
= super
->disk_mgmt_list
;
4192 super
->disk_mgmt_list
= dd
;
4198 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
4202 struct imsm_super anchor
;
4203 } spare_record
__attribute__ ((aligned(512)));
4205 /* spare records have their own family number and do not have any defined raid
4208 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
4210 struct imsm_super
*mpb
= super
->anchor
;
4211 struct imsm_super
*spare
= &spare_record
.anchor
;
4215 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
)),
4216 spare
->generation_num
= __cpu_to_le32(1UL),
4217 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
4218 spare
->num_disks
= 1,
4219 spare
->num_raid_devs
= 0,
4220 spare
->cache_size
= mpb
->cache_size
,
4221 spare
->pwr_cycle_count
= __cpu_to_le32(1),
4223 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
4224 MPB_SIGNATURE MPB_VERSION_RAID0
);
4226 for (d
= super
->disks
; d
; d
= d
->next
) {
4230 spare
->disk
[0] = d
->disk
;
4231 sum
= __gen_imsm_checksum(spare
);
4232 spare
->family_num
= __cpu_to_le32(sum
);
4233 spare
->orig_family_num
= 0;
4234 sum
= __gen_imsm_checksum(spare
);
4235 spare
->check_sum
= __cpu_to_le32(sum
);
4237 if (store_imsm_mpb(d
->fd
, spare
)) {
4238 fprintf(stderr
, "%s: failed for device %d:%d %s\n",
4239 __func__
, d
->major
, d
->minor
, strerror(errno
));
4251 static int is_gen_migration(struct imsm_dev
*dev
);
4253 static int write_super_imsm(struct supertype
*st
, int doclose
)
4255 struct intel_super
*super
= st
->sb
;
4256 struct imsm_super
*mpb
= super
->anchor
;
4262 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
4264 int clear_migration_record
= 1;
4266 /* 'generation' is incremented everytime the metadata is written */
4267 generation
= __le32_to_cpu(mpb
->generation_num
);
4269 mpb
->generation_num
= __cpu_to_le32(generation
);
4271 /* fix up cases where previous mdadm releases failed to set
4274 if (mpb
->orig_family_num
== 0)
4275 mpb
->orig_family_num
= mpb
->family_num
;
4277 for (d
= super
->disks
; d
; d
= d
->next
) {
4281 mpb
->disk
[d
->index
] = d
->disk
;
4285 for (d
= super
->missing
; d
; d
= d
->next
) {
4286 mpb
->disk
[d
->index
] = d
->disk
;
4289 mpb
->num_disks
= num_disks
;
4290 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
4292 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4293 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
4294 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
4296 imsm_copy_dev(dev
, dev2
);
4297 mpb_size
+= sizeof_imsm_dev(dev
, 0);
4299 if (is_gen_migration(dev2
))
4300 clear_migration_record
= 0;
4302 mpb_size
+= __le32_to_cpu(mpb
->bbm_log_size
);
4303 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
4305 /* recalculate checksum */
4306 sum
= __gen_imsm_checksum(mpb
);
4307 mpb
->check_sum
= __cpu_to_le32(sum
);
4309 if (clear_migration_record
)
4310 memset(super
->migr_rec_buf
, 0, 512);
4312 /* write the mpb for disks that compose raid devices */
4313 for (d
= super
->disks
; d
; d
= d
->next
) {
4316 if (store_imsm_mpb(d
->fd
, mpb
))
4317 fprintf(stderr
, "%s: failed for device %d:%d %s\n",
4318 __func__
, d
->major
, d
->minor
, strerror(errno
));
4319 if (clear_migration_record
) {
4320 unsigned long long dsize
;
4322 get_dev_size(d
->fd
, NULL
, &dsize
);
4323 if (lseek64(d
->fd
, dsize
- 512, SEEK_SET
) >= 0) {
4324 write(d
->fd
, super
->migr_rec_buf
, 512);
4334 return write_super_imsm_spares(super
, doclose
);
4340 static int create_array(struct supertype
*st
, int dev_idx
)
4343 struct imsm_update_create_array
*u
;
4344 struct intel_super
*super
= st
->sb
;
4345 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
4346 struct imsm_map
*map
= get_imsm_map(dev
, 0);
4347 struct disk_info
*inf
;
4348 struct imsm_disk
*disk
;
4351 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
4352 sizeof(*inf
) * map
->num_members
;
4355 fprintf(stderr
, "%s: failed to allocate update buffer\n",
4360 u
->type
= update_create_array
;
4361 u
->dev_idx
= dev_idx
;
4362 imsm_copy_dev(&u
->dev
, dev
);
4363 inf
= get_disk_info(u
);
4364 for (i
= 0; i
< map
->num_members
; i
++) {
4365 int idx
= get_imsm_disk_idx(dev
, i
, -1);
4367 disk
= get_imsm_disk(super
, idx
);
4368 serialcpy(inf
[i
].serial
, disk
->serial
);
4370 append_metadata_update(st
, u
, len
);
4375 static int mgmt_disk(struct supertype
*st
)
4377 struct intel_super
*super
= st
->sb
;
4379 struct imsm_update_add_remove_disk
*u
;
4381 if (!super
->disk_mgmt_list
)
4387 fprintf(stderr
, "%s: failed to allocate update buffer\n",
4392 u
->type
= update_add_remove_disk
;
4393 append_metadata_update(st
, u
, len
);
4398 static int write_init_super_imsm(struct supertype
*st
)
4400 struct intel_super
*super
= st
->sb
;
4401 int current_vol
= super
->current_vol
;
4403 /* we are done with current_vol reset it to point st at the container */
4404 super
->current_vol
= -1;
4406 if (st
->update_tail
) {
4407 /* queue the recently created array / added disk
4408 * as a metadata update */
4411 /* determine if we are creating a volume or adding a disk */
4412 if (current_vol
< 0) {
4413 /* in the mgmt (add/remove) disk case we are running
4414 * in mdmon context, so don't close fd's
4416 return mgmt_disk(st
);
4418 rv
= create_array(st
, current_vol
);
4423 for (d
= super
->disks
; d
; d
= d
->next
)
4424 Kill(d
->devname
, NULL
, 0, 1, 1);
4425 return write_super_imsm(st
, 1);
4430 static int store_super_imsm(struct supertype
*st
, int fd
)
4432 struct intel_super
*super
= st
->sb
;
4433 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
4439 return store_imsm_mpb(fd
, mpb
);
4445 static int imsm_bbm_log_size(struct imsm_super
*mpb
)
4447 return __le32_to_cpu(mpb
->bbm_log_size
);
4451 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
4452 int layout
, int raiddisks
, int chunk
,
4453 unsigned long long size
, char *dev
,
4454 unsigned long long *freesize
,
4458 unsigned long long ldsize
;
4459 struct intel_super
*super
=NULL
;
4462 if (level
!= LEVEL_CONTAINER
)
4467 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
4470 fprintf(stderr
, Name
": imsm: Cannot open %s: %s\n",
4471 dev
, strerror(errno
));
4474 if (!get_dev_size(fd
, dev
, &ldsize
)) {
4479 /* capabilities retrieve could be possible
4480 * note that there is no fd for the disks in array.
4482 super
= alloc_super();
4485 Name
": malloc of %zu failed.\n",
4491 rv
= find_intel_hba_capability(fd
, super
, verbose
? dev
: NULL
);
4495 fd2devname(fd
, str
);
4496 dprintf("validate_geometry_imsm_container: fd: %d %s orom: %p rv: %d raiddisk: %d\n",
4497 fd
, str
, super
->orom
, rv
, raiddisks
);
4499 /* no orom/efi or non-intel hba of the disk */
4505 if (super
->orom
&& raiddisks
> super
->orom
->tds
) {
4507 fprintf(stderr
, Name
": %d exceeds maximum number of"
4508 " platform supported disks: %d\n",
4509 raiddisks
, super
->orom
->tds
);
4515 *freesize
= avail_size_imsm(st
, ldsize
>> 9);
4521 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
4523 const unsigned long long base_start
= e
[*idx
].start
;
4524 unsigned long long end
= base_start
+ e
[*idx
].size
;
4527 if (base_start
== end
)
4531 for (i
= *idx
; i
< num_extents
; i
++) {
4532 /* extend overlapping extents */
4533 if (e
[i
].start
>= base_start
&&
4534 e
[i
].start
<= end
) {
4537 if (e
[i
].start
+ e
[i
].size
> end
)
4538 end
= e
[i
].start
+ e
[i
].size
;
4539 } else if (e
[i
].start
> end
) {
4545 return end
- base_start
;
4548 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
4550 /* build a composite disk with all known extents and generate a new
4551 * 'maxsize' given the "all disks in an array must share a common start
4552 * offset" constraint
4554 struct extent
*e
= calloc(sum_extents
, sizeof(*e
));
4558 unsigned long long pos
;
4559 unsigned long long start
= 0;
4560 unsigned long long maxsize
;
4561 unsigned long reserve
;
4566 /* coalesce and sort all extents. also, check to see if we need to
4567 * reserve space between member arrays
4570 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
4573 for (i
= 0; i
< dl
->extent_cnt
; i
++)
4576 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
4581 while (i
< sum_extents
) {
4582 e
[j
].start
= e
[i
].start
;
4583 e
[j
].size
= find_size(e
, &i
, sum_extents
);
4585 if (e
[j
-1].size
== 0)
4594 unsigned long long esize
;
4596 esize
= e
[i
].start
- pos
;
4597 if (esize
>= maxsize
) {
4602 pos
= e
[i
].start
+ e
[i
].size
;
4604 } while (e
[i
-1].size
);
4610 /* FIXME assumes volume at offset 0 is the first volume in a
4613 if (start_extent
> 0)
4614 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
4618 if (maxsize
< reserve
)
4621 super
->create_offset
= ~((__u32
) 0);
4622 if (start
+ reserve
> super
->create_offset
)
4623 return 0; /* start overflows create_offset */
4624 super
->create_offset
= start
+ reserve
;
4626 return maxsize
- reserve
;
4629 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
4631 if (level
< 0 || level
== 6 || level
== 4)
4634 /* if we have an orom prevent invalid raid levels */
4637 case 0: return imsm_orom_has_raid0(orom
);
4640 return imsm_orom_has_raid1e(orom
);
4641 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
4642 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
4643 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
4646 return 1; /* not on an Intel RAID platform so anything goes */
4652 #define pr_vrb(fmt, arg...) (void) (verbose && fprintf(stderr, Name fmt, ##arg))
4654 * validate volume parameters with OROM/EFI capabilities
4657 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
4658 int raiddisks
, int *chunk
, int verbose
)
4663 /* validate container capabilities */
4664 if (super
->orom
&& raiddisks
> super
->orom
->tds
) {
4666 fprintf(stderr
, Name
": %d exceeds maximum number of"
4667 " platform supported disks: %d\n",
4668 raiddisks
, super
->orom
->tds
);
4672 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
4673 if (super
->orom
&& (!is_raid_level_supported(super
->orom
, level
,
4675 pr_vrb(": platform does not support raid%d with %d disk%s\n",
4676 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
4679 if (super
->orom
&& level
!= 1) {
4680 if (chunk
&& (*chunk
== 0 || *chunk
== UnSet
))
4681 *chunk
= imsm_orom_default_chunk(super
->orom
);
4682 else if (chunk
&& !imsm_orom_has_chunk(super
->orom
, *chunk
)) {
4683 pr_vrb(": platform does not support a chunk size of: "
4688 if (layout
!= imsm_level_to_layout(level
)) {
4690 pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n");
4691 else if (level
== 10)
4692 pr_vrb(": imsm raid 10 only supports the n2 layout\n");
4694 pr_vrb(": imsm unknown layout %#x for this raid level %d\n",
4701 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
4702 * FIX ME add ahci details
4704 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
4705 int layout
, int raiddisks
, int *chunk
,
4706 unsigned long long size
, char *dev
,
4707 unsigned long long *freesize
,
4711 struct intel_super
*super
= st
->sb
;
4712 struct imsm_super
*mpb
= super
->anchor
;
4714 unsigned long long pos
= 0;
4715 unsigned long long maxsize
;
4719 /* We must have the container info already read in. */
4723 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, verbose
)) {
4724 fprintf(stderr
, Name
": RAID gemetry validation failed. "
4725 "Cannot proceed with the action(s).\n");
4729 /* General test: make sure there is space for
4730 * 'raiddisks' device extents of size 'size' at a given
4733 unsigned long long minsize
= size
;
4734 unsigned long long start_offset
= MaxSector
;
4737 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
4738 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
4743 e
= get_extents(super
, dl
);
4746 unsigned long long esize
;
4747 esize
= e
[i
].start
- pos
;
4748 if (esize
>= minsize
)
4750 if (found
&& start_offset
== MaxSector
) {
4753 } else if (found
&& pos
!= start_offset
) {
4757 pos
= e
[i
].start
+ e
[i
].size
;
4759 } while (e
[i
-1].size
);
4764 if (dcnt
< raiddisks
) {
4766 fprintf(stderr
, Name
": imsm: Not enough "
4767 "devices with space for this array "
4775 /* This device must be a member of the set */
4776 if (stat(dev
, &stb
) < 0)
4778 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
4780 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
4781 if (dl
->major
== (int)major(stb
.st_rdev
) &&
4782 dl
->minor
== (int)minor(stb
.st_rdev
))
4787 fprintf(stderr
, Name
": %s is not in the "
4788 "same imsm set\n", dev
);
4790 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
4791 /* If a volume is present then the current creation attempt
4792 * cannot incorporate new spares because the orom may not
4793 * understand this configuration (all member disks must be
4794 * members of each array in the container).
4796 fprintf(stderr
, Name
": %s is a spare and a volume"
4797 " is already defined for this container\n", dev
);
4798 fprintf(stderr
, Name
": The option-rom requires all member"
4799 " disks to be a member of all volumes\n");
4803 /* retrieve the largest free space block */
4804 e
= get_extents(super
, dl
);
4809 unsigned long long esize
;
4811 esize
= e
[i
].start
- pos
;
4812 if (esize
>= maxsize
)
4814 pos
= e
[i
].start
+ e
[i
].size
;
4816 } while (e
[i
-1].size
);
4821 fprintf(stderr
, Name
": unable to determine free space for: %s\n",
4825 if (maxsize
< size
) {
4827 fprintf(stderr
, Name
": %s not enough space (%llu < %llu)\n",
4828 dev
, maxsize
, size
);
4832 /* count total number of extents for merge */
4834 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4836 i
+= dl
->extent_cnt
;
4838 maxsize
= merge_extents(super
, i
);
4839 if (maxsize
< size
|| maxsize
== 0) {
4841 fprintf(stderr
, Name
": not enough space after merge (%llu < %llu)\n",
4846 *freesize
= maxsize
;
4851 static int reserve_space(struct supertype
*st
, int raiddisks
,
4852 unsigned long long size
, int chunk
,
4853 unsigned long long *freesize
)
4855 struct intel_super
*super
= st
->sb
;
4856 struct imsm_super
*mpb
= super
->anchor
;
4861 unsigned long long maxsize
;
4862 unsigned long long minsize
;
4866 /* find the largest common start free region of the possible disks */
4870 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
4876 /* don't activate new spares if we are orom constrained
4877 * and there is already a volume active in the container
4879 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
4882 e
= get_extents(super
, dl
);
4885 for (i
= 1; e
[i
-1].size
; i
++)
4893 maxsize
= merge_extents(super
, extent_cnt
);
4897 minsize
= chunk
* 2;
4899 if (cnt
< raiddisks
||
4900 (super
->orom
&& used
&& used
!= raiddisks
) ||
4901 maxsize
< minsize
||
4903 fprintf(stderr
, Name
": not enough devices with space to create array.\n");
4904 return 0; /* No enough free spaces large enough */
4916 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4918 dl
->raiddisk
= cnt
++;
4925 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
4926 int raiddisks
, int *chunk
, unsigned long long size
,
4927 char *dev
, unsigned long long *freesize
,
4935 * if given unused devices create a container
4936 * if given given devices in a container create a member volume
4938 if (level
== LEVEL_CONTAINER
) {
4939 /* Must be a fresh device to add to a container */
4940 return validate_geometry_imsm_container(st
, level
, layout
,
4942 chunk
?*chunk
:0, size
,
4948 if (st
->sb
&& freesize
) {
4949 /* we are being asked to automatically layout a
4950 * new volume based on the current contents of
4951 * the container. If the the parameters can be
4952 * satisfied reserve_space will record the disks,
4953 * start offset, and size of the volume to be
4954 * created. add_to_super and getinfo_super
4955 * detect when autolayout is in progress.
4957 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
4961 return reserve_space(st
, raiddisks
, size
,
4962 chunk
?*chunk
:0, freesize
);
4967 /* creating in a given container */
4968 return validate_geometry_imsm_volume(st
, level
, layout
,
4969 raiddisks
, chunk
, size
,
4970 dev
, freesize
, verbose
);
4973 /* This device needs to be a device in an 'imsm' container */
4974 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
4978 Name
": Cannot create this array on device %s\n",
4983 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
4985 fprintf(stderr
, Name
": Cannot open %s: %s\n",
4986 dev
, strerror(errno
));
4989 /* Well, it is in use by someone, maybe an 'imsm' container. */
4990 cfd
= open_container(fd
);
4994 fprintf(stderr
, Name
": Cannot use %s: It is busy\n",
4998 sra
= sysfs_read(cfd
, 0, GET_VERSION
);
4999 if (sra
&& sra
->array
.major_version
== -1 &&
5000 strcmp(sra
->text_version
, "imsm") == 0)
5004 /* This is a member of a imsm container. Load the container
5005 * and try to create a volume
5007 struct intel_super
*super
;
5009 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
) == 0) {
5011 st
->container_dev
= fd2devnum(cfd
);
5013 return validate_geometry_imsm_volume(st
, level
, layout
,
5021 fprintf(stderr
, Name
": failed container membership check\n");
5027 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
5029 struct intel_super
*super
= st
->sb
;
5031 if (level
&& *level
== UnSet
)
5032 *level
= LEVEL_CONTAINER
;
5034 if (level
&& layout
&& *layout
== UnSet
)
5035 *layout
= imsm_level_to_layout(*level
);
5037 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0) &&
5038 super
&& super
->orom
)
5039 *chunk
= imsm_orom_default_chunk(super
->orom
);
5042 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
5044 static int kill_subarray_imsm(struct supertype
*st
)
5046 /* remove the subarray currently referenced by ->current_vol */
5048 struct intel_dev
**dp
;
5049 struct intel_super
*super
= st
->sb
;
5050 __u8 current_vol
= super
->current_vol
;
5051 struct imsm_super
*mpb
= super
->anchor
;
5053 if (super
->current_vol
< 0)
5055 super
->current_vol
= -1; /* invalidate subarray cursor */
5057 /* block deletions that would change the uuid of active subarrays
5059 * FIXME when immutable ids are available, but note that we'll
5060 * also need to fixup the invalidated/active subarray indexes in
5063 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5066 if (i
< current_vol
)
5068 sprintf(subarray
, "%u", i
);
5069 if (is_subarray_active(subarray
, st
->devname
)) {
5071 Name
": deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
5078 if (st
->update_tail
) {
5079 struct imsm_update_kill_array
*u
= malloc(sizeof(*u
));
5083 u
->type
= update_kill_array
;
5084 u
->dev_idx
= current_vol
;
5085 append_metadata_update(st
, u
, sizeof(*u
));
5090 for (dp
= &super
->devlist
; *dp
;)
5091 if ((*dp
)->index
== current_vol
) {
5094 handle_missing(super
, (*dp
)->dev
);
5095 if ((*dp
)->index
> current_vol
)
5100 /* no more raid devices, all active components are now spares,
5101 * but of course failed are still failed
5103 if (--mpb
->num_raid_devs
== 0) {
5106 for (d
= super
->disks
; d
; d
= d
->next
)
5107 if (d
->index
> -2) {
5109 d
->disk
.status
= SPARE_DISK
;
5113 super
->updates_pending
++;
5118 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
5119 char *update
, struct mddev_ident
*ident
)
5121 /* update the subarray currently referenced by ->current_vol */
5122 struct intel_super
*super
= st
->sb
;
5123 struct imsm_super
*mpb
= super
->anchor
;
5125 if (strcmp(update
, "name") == 0) {
5126 char *name
= ident
->name
;
5130 if (is_subarray_active(subarray
, st
->devname
)) {
5132 Name
": Unable to update name of active subarray\n");
5136 if (!check_name(super
, name
, 0))
5139 vol
= strtoul(subarray
, &ep
, 10);
5140 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
5143 if (st
->update_tail
) {
5144 struct imsm_update_rename_array
*u
= malloc(sizeof(*u
));
5148 u
->type
= update_rename_array
;
5150 snprintf((char *) u
->name
, MAX_RAID_SERIAL_LEN
, "%s", name
);
5151 append_metadata_update(st
, u
, sizeof(*u
));
5153 struct imsm_dev
*dev
;
5156 dev
= get_imsm_dev(super
, vol
);
5157 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
5158 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5159 dev
= get_imsm_dev(super
, i
);
5160 handle_missing(super
, dev
);
5162 super
->updates_pending
++;
5170 static int is_gen_migration(struct imsm_dev
*dev
)
5172 if (!dev
->vol
.migr_state
)
5175 if (migr_type(dev
) == MIGR_GEN_MIGR
)
5180 #endif /* MDASSEMBLE */
5182 static int is_rebuilding(struct imsm_dev
*dev
)
5184 struct imsm_map
*migr_map
;
5186 if (!dev
->vol
.migr_state
)
5189 if (migr_type(dev
) != MIGR_REBUILD
)
5192 migr_map
= get_imsm_map(dev
, 1);
5194 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
5200 static void update_recovery_start(struct imsm_dev
*dev
, struct mdinfo
*array
)
5202 struct mdinfo
*rebuild
= NULL
;
5206 if (!is_rebuilding(dev
))
5209 /* Find the rebuild target, but punt on the dual rebuild case */
5210 for (d
= array
->devs
; d
; d
= d
->next
)
5211 if (d
->recovery_start
== 0) {
5218 /* (?) none of the disks are marked with
5219 * IMSM_ORD_REBUILD, so assume they are missing and the
5220 * disk_ord_tbl was not correctly updated
5222 dprintf("%s: failed to locate out-of-sync disk\n", __func__
);
5226 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
5227 rebuild
->recovery_start
= units
* blocks_per_migr_unit(dev
);
5231 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
5233 /* Given a container loaded by load_super_imsm_all,
5234 * extract information about all the arrays into
5236 * If 'subarray' is given, just extract info about that array.
5238 * For each imsm_dev create an mdinfo, fill it in,
5239 * then look for matching devices in super->disks
5240 * and create appropriate device mdinfo.
5242 struct intel_super
*super
= st
->sb
;
5243 struct imsm_super
*mpb
= super
->anchor
;
5244 struct mdinfo
*rest
= NULL
;
5248 int spare_disks
= 0;
5250 /* check for bad blocks */
5251 if (imsm_bbm_log_size(super
->anchor
))
5254 /* count spare devices, not used in maps
5256 for (d
= super
->disks
; d
; d
= d
->next
)
5260 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5261 struct imsm_dev
*dev
;
5262 struct imsm_map
*map
;
5263 struct imsm_map
*map2
;
5264 struct mdinfo
*this;
5269 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
5272 dev
= get_imsm_dev(super
, i
);
5273 map
= get_imsm_map(dev
, 0);
5274 map2
= get_imsm_map(dev
, 1);
5276 /* do not publish arrays that are in the middle of an
5277 * unsupported migration
5279 if (dev
->vol
.migr_state
&&
5280 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
5281 fprintf(stderr
, Name
": cannot assemble volume '%.16s':"
5282 " unsupported migration in progress\n",
5286 /* do not publish arrays that are not support by controller's
5290 chunk
= __le16_to_cpu(map
->blocks_per_strip
) >> 1;
5292 if (!validate_geometry_imsm_orom(super
,
5293 get_imsm_raid_level(map
), /* RAID level */
5294 imsm_level_to_layout(get_imsm_raid_level(map
)),
5295 map
->num_members
, /* raid disks */
5298 fprintf(stderr
, Name
": RAID gemetry validation failed. "
5299 "Cannot proceed with the action(s).\n");
5302 #endif /* MDASSEMBLE */
5303 this = malloc(sizeof(*this));
5305 fprintf(stderr
, Name
": failed to allocate %zu bytes\n",
5309 memset(this, 0, sizeof(*this));
5312 super
->current_vol
= i
;
5313 getinfo_super_imsm_volume(st
, this, NULL
);
5314 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
5315 unsigned long long recovery_start
;
5316 struct mdinfo
*info_d
;
5323 idx
= get_imsm_disk_idx(dev
, slot
, 0);
5324 ord
= get_imsm_ord_tbl_ent(dev
, slot
, -1);
5325 for (d
= super
->disks
; d
; d
= d
->next
)
5326 if (d
->index
== idx
)
5329 recovery_start
= MaxSector
;
5332 if (d
&& is_failed(&d
->disk
))
5334 if (ord
& IMSM_ORD_REBUILD
)
5338 * if we skip some disks the array will be assmebled degraded;
5339 * reset resync start to avoid a dirty-degraded
5340 * situation when performing the intial sync
5342 * FIXME handle dirty degraded
5344 if ((skip
|| recovery_start
== 0) && !dev
->vol
.dirty
)
5345 this->resync_start
= MaxSector
;
5349 info_d
= calloc(1, sizeof(*info_d
));
5351 fprintf(stderr
, Name
": failed to allocate disk"
5352 " for volume %.16s\n", dev
->volume
);
5353 info_d
= this->devs
;
5355 struct mdinfo
*d
= info_d
->next
;
5364 info_d
->next
= this->devs
;
5365 this->devs
= info_d
;
5367 info_d
->disk
.number
= d
->index
;
5368 info_d
->disk
.major
= d
->major
;
5369 info_d
->disk
.minor
= d
->minor
;
5370 info_d
->disk
.raid_disk
= slot
;
5371 info_d
->recovery_start
= recovery_start
;
5373 if (slot
< map2
->num_members
)
5374 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
5376 this->array
.spare_disks
++;
5378 if (slot
< map
->num_members
)
5379 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
5381 this->array
.spare_disks
++;
5383 if (info_d
->recovery_start
== MaxSector
)
5384 this->array
.working_disks
++;
5386 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
5387 info_d
->data_offset
= __le32_to_cpu(map
->pba_of_lba0
);
5388 info_d
->component_size
= __le32_to_cpu(map
->blocks_per_member
);
5390 /* now that the disk list is up-to-date fixup recovery_start */
5391 update_recovery_start(dev
, this);
5392 this->array
.spare_disks
+= spare_disks
;
5396 /* if array has bad blocks, set suitable bit in array status */
5398 rest
->array
.state
|= (1<<MD_SB_BBM_ERRORS
);
5404 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
, int failed
)
5406 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5409 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
5410 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
5412 switch (get_imsm_raid_level(map
)) {
5414 return IMSM_T_STATE_FAILED
;
5417 if (failed
< map
->num_members
)
5418 return IMSM_T_STATE_DEGRADED
;
5420 return IMSM_T_STATE_FAILED
;
5425 * check to see if any mirrors have failed, otherwise we
5426 * are degraded. Even numbered slots are mirrored on
5430 /* gcc -Os complains that this is unused */
5431 int insync
= insync
;
5433 for (i
= 0; i
< map
->num_members
; i
++) {
5434 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, -1);
5435 int idx
= ord_to_idx(ord
);
5436 struct imsm_disk
*disk
;
5438 /* reset the potential in-sync count on even-numbered
5439 * slots. num_copies is always 2 for imsm raid10
5444 disk
= get_imsm_disk(super
, idx
);
5445 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
5448 /* no in-sync disks left in this mirror the
5452 return IMSM_T_STATE_FAILED
;
5455 return IMSM_T_STATE_DEGRADED
;
5459 return IMSM_T_STATE_DEGRADED
;
5461 return IMSM_T_STATE_FAILED
;
5467 return map
->map_state
;
5470 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
)
5474 struct imsm_disk
*disk
;
5475 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5476 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
);
5480 /* at the beginning of migration we set IMSM_ORD_REBUILD on
5481 * disks that are being rebuilt. New failures are recorded to
5482 * map[0]. So we look through all the disks we started with and
5483 * see if any failures are still present, or if any new ones
5486 * FIXME add support for online capacity expansion and
5487 * raid-level-migration
5489 for (i
= 0; i
< prev
->num_members
; i
++) {
5490 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
5491 ord
|= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
5492 idx
= ord_to_idx(ord
);
5494 disk
= get_imsm_disk(super
, idx
);
5495 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
5503 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
5506 struct intel_super
*super
= c
->sb
;
5507 struct imsm_super
*mpb
= super
->anchor
;
5509 if (atoi(inst
) >= mpb
->num_raid_devs
) {
5510 fprintf(stderr
, "%s: subarry index %d, out of range\n",
5511 __func__
, atoi(inst
));
5515 dprintf("imsm: open_new %s\n", inst
);
5516 a
->info
.container_member
= atoi(inst
);
5520 static int is_resyncing(struct imsm_dev
*dev
)
5522 struct imsm_map
*migr_map
;
5524 if (!dev
->vol
.migr_state
)
5527 if (migr_type(dev
) == MIGR_INIT
||
5528 migr_type(dev
) == MIGR_REPAIR
)
5531 if (migr_type(dev
) == MIGR_GEN_MIGR
)
5534 migr_map
= get_imsm_map(dev
, 1);
5536 if ((migr_map
->map_state
== IMSM_T_STATE_NORMAL
) &&
5537 (dev
->vol
.migr_type
!= MIGR_GEN_MIGR
))
5543 /* return true if we recorded new information */
5544 static int mark_failure(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
5548 struct imsm_map
*map
;
5550 /* new failures are always set in map[0] */
5551 map
= get_imsm_map(dev
, 0);
5553 slot
= get_imsm_disk_slot(map
, idx
);
5557 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
5558 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
5561 disk
->status
|= FAILED_DISK
;
5562 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
5563 if (map
->failed_disk_num
== 0xff)
5564 map
->failed_disk_num
= slot
;
5568 static void mark_missing(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
5570 mark_failure(dev
, disk
, idx
);
5572 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
5575 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
5576 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
5579 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
5585 if (!super
->missing
)
5587 failed
= imsm_count_failed(super
, dev
);
5588 map_state
= imsm_check_degraded(super
, dev
, failed
);
5590 dprintf("imsm: mark missing\n");
5591 end_migration(dev
, map_state
);
5592 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
5593 mark_missing(dev
, &dl
->disk
, dl
->index
);
5594 super
->updates_pending
++;
5597 static unsigned long long imsm_set_array_size(struct imsm_dev
*dev
)
5599 int used_disks
= imsm_num_data_members(dev
, 0);
5600 unsigned long long array_blocks
;
5601 struct imsm_map
*map
;
5603 if (used_disks
== 0) {
5604 /* when problems occures
5605 * return current array_blocks value
5607 array_blocks
= __le32_to_cpu(dev
->size_high
);
5608 array_blocks
= array_blocks
<< 32;
5609 array_blocks
+= __le32_to_cpu(dev
->size_low
);
5611 return array_blocks
;
5614 /* set array size in metadata
5616 map
= get_imsm_map(dev
, 0);
5617 array_blocks
= map
->blocks_per_member
* used_disks
;
5619 /* round array size down to closest MB
5621 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
5622 dev
->size_low
= __cpu_to_le32((__u32
)array_blocks
);
5623 dev
->size_high
= __cpu_to_le32((__u32
)(array_blocks
>> 32));
5625 return array_blocks
;
5628 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
5630 static void imsm_progress_container_reshape(struct intel_super
*super
)
5632 /* if no device has a migr_state, but some device has a
5633 * different number of members than the previous device, start
5634 * changing the number of devices in this device to match
5637 struct imsm_super
*mpb
= super
->anchor
;
5638 int prev_disks
= -1;
5642 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5643 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
5644 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5645 struct imsm_map
*map2
;
5646 int prev_num_members
;
5648 if (dev
->vol
.migr_state
)
5651 if (prev_disks
== -1)
5652 prev_disks
= map
->num_members
;
5653 if (prev_disks
== map
->num_members
)
5656 /* OK, this array needs to enter reshape mode.
5657 * i.e it needs a migr_state
5660 copy_map_size
= sizeof_imsm_map(map
);
5661 prev_num_members
= map
->num_members
;
5662 map
->num_members
= prev_disks
;
5663 dev
->vol
.migr_state
= 1;
5664 dev
->vol
.curr_migr_unit
= 0;
5665 dev
->vol
.migr_type
= MIGR_GEN_MIGR
;
5666 for (i
= prev_num_members
;
5667 i
< map
->num_members
; i
++)
5668 set_imsm_ord_tbl_ent(map
, i
, i
);
5669 map2
= get_imsm_map(dev
, 1);
5670 /* Copy the current map */
5671 memcpy(map2
, map
, copy_map_size
);
5672 map2
->num_members
= prev_num_members
;
5674 imsm_set_array_size(dev
);
5675 super
->updates_pending
++;
5679 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
5680 * states are handled in imsm_set_disk() with one exception, when a
5681 * resync is stopped due to a new failure this routine will set the
5682 * 'degraded' state for the array.
5684 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
5686 int inst
= a
->info
.container_member
;
5687 struct intel_super
*super
= a
->container
->sb
;
5688 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
5689 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5690 int failed
= imsm_count_failed(super
, dev
);
5691 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
);
5692 __u32 blocks_per_unit
;
5694 if (dev
->vol
.migr_state
&&
5695 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
5696 /* array state change is blocked due to reshape action
5698 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
5699 * - finish the reshape (if last_checkpoint is big and action != reshape)
5700 * - update curr_migr_unit
5702 if (a
->curr_action
== reshape
) {
5703 /* still reshaping, maybe update curr_migr_unit */
5704 goto mark_checkpoint
;
5706 if (a
->last_checkpoint
== 0 && a
->prev_action
== reshape
) {
5707 /* for some reason we aborted the reshape.
5710 struct imsm_map
*map2
= get_imsm_map(dev
, 1);
5711 dev
->vol
.migr_state
= 0;
5712 dev
->vol
.migr_type
= 0;
5713 dev
->vol
.curr_migr_unit
= 0;
5714 memcpy(map
, map2
, sizeof_imsm_map(map2
));
5715 super
->updates_pending
++;
5717 if (a
->last_checkpoint
>= a
->info
.component_size
) {
5718 unsigned long long array_blocks
;
5722 used_disks
= imsm_num_data_members(dev
, 0);
5723 if (used_disks
> 0) {
5725 map
->blocks_per_member
*
5727 /* round array size down to closest MB
5729 array_blocks
= (array_blocks
5730 >> SECT_PER_MB_SHIFT
)
5731 << SECT_PER_MB_SHIFT
;
5732 a
->info
.custom_array_size
= array_blocks
;
5733 /* encourage manager to update array
5737 a
->check_reshape
= 1;
5739 /* finalize online capacity expansion/reshape */
5740 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
5742 mdi
->disk
.raid_disk
,
5745 imsm_progress_container_reshape(super
);
5750 /* before we activate this array handle any missing disks */
5751 if (consistent
== 2)
5752 handle_missing(super
, dev
);
5754 if (consistent
== 2 &&
5755 (!is_resync_complete(&a
->info
) ||
5756 map_state
!= IMSM_T_STATE_NORMAL
||
5757 dev
->vol
.migr_state
))
5760 if (is_resync_complete(&a
->info
)) {
5761 /* complete intialization / resync,
5762 * recovery and interrupted recovery is completed in
5765 if (is_resyncing(dev
)) {
5766 dprintf("imsm: mark resync done\n");
5767 end_migration(dev
, map_state
);
5768 super
->updates_pending
++;
5769 a
->last_checkpoint
= 0;
5771 } else if (!is_resyncing(dev
) && !failed
) {
5772 /* mark the start of the init process if nothing is failed */
5773 dprintf("imsm: mark resync start\n");
5774 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
5775 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
5777 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
5778 super
->updates_pending
++;
5782 /* check if we can update curr_migr_unit from resync_start, recovery_start */
5783 blocks_per_unit
= blocks_per_migr_unit(dev
);
5784 if (blocks_per_unit
) {
5788 units
= a
->last_checkpoint
/ blocks_per_unit
;
5791 /* check that we did not overflow 32-bits, and that
5792 * curr_migr_unit needs updating
5794 if (units32
== units
&&
5796 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
5797 dprintf("imsm: mark checkpoint (%u)\n", units32
);
5798 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
5799 super
->updates_pending
++;
5803 /* mark dirty / clean */
5804 if (dev
->vol
.dirty
!= !consistent
) {
5805 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
5810 super
->updates_pending
++;
5816 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
5818 int inst
= a
->info
.container_member
;
5819 struct intel_super
*super
= a
->container
->sb
;
5820 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
5821 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5822 struct imsm_disk
*disk
;
5827 if (n
> map
->num_members
)
5828 fprintf(stderr
, "imsm: set_disk %d out of range 0..%d\n",
5829 n
, map
->num_members
- 1);
5834 dprintf("imsm: set_disk %d:%x\n", n
, state
);
5836 ord
= get_imsm_ord_tbl_ent(dev
, n
, -1);
5837 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
5839 /* check for new failures */
5840 if (state
& DS_FAULTY
) {
5841 if (mark_failure(dev
, disk
, ord_to_idx(ord
)))
5842 super
->updates_pending
++;
5845 /* check if in_sync */
5846 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
5847 struct imsm_map
*migr_map
= get_imsm_map(dev
, 1);
5849 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
5850 super
->updates_pending
++;
5853 failed
= imsm_count_failed(super
, dev
);
5854 map_state
= imsm_check_degraded(super
, dev
, failed
);
5856 /* check if recovery complete, newly degraded, or failed */
5857 if (map_state
== IMSM_T_STATE_NORMAL
&& is_rebuilding(dev
)) {
5858 end_migration(dev
, map_state
);
5859 map
= get_imsm_map(dev
, 0);
5860 map
->failed_disk_num
= ~0;
5861 super
->updates_pending
++;
5862 a
->last_checkpoint
= 0;
5863 } else if (map_state
== IMSM_T_STATE_DEGRADED
&&
5864 map
->map_state
!= map_state
&&
5865 !dev
->vol
.migr_state
) {
5866 dprintf("imsm: mark degraded\n");
5867 map
->map_state
= map_state
;
5868 super
->updates_pending
++;
5869 a
->last_checkpoint
= 0;
5870 } else if (map_state
== IMSM_T_STATE_FAILED
&&
5871 map
->map_state
!= map_state
) {
5872 dprintf("imsm: mark failed\n");
5873 end_migration(dev
, map_state
);
5874 super
->updates_pending
++;
5875 a
->last_checkpoint
= 0;
5876 } else if (is_gen_migration(dev
)) {
5877 dprintf("imsm: Detected General Migration in state: ");
5878 if (map_state
== IMSM_T_STATE_NORMAL
) {
5879 end_migration(dev
, map_state
);
5880 map
= get_imsm_map(dev
, 0);
5881 map
->failed_disk_num
= ~0;
5882 dprintf("normal\n");
5884 if (map_state
== IMSM_T_STATE_DEGRADED
) {
5885 printf("degraded\n");
5886 end_migration(dev
, map_state
);
5888 dprintf("failed\n");
5890 map
->map_state
= map_state
;
5892 super
->updates_pending
++;
5896 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
5899 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
5900 unsigned long long dsize
;
5901 unsigned long long sectors
;
5903 get_dev_size(fd
, NULL
, &dsize
);
5905 if (mpb_size
> 512) {
5906 /* -1 to account for anchor */
5907 sectors
= mpb_sectors(mpb
) - 1;
5909 /* write the extended mpb to the sectors preceeding the anchor */
5910 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0)
5913 if ((unsigned long long)write(fd
, buf
+ 512, 512 * sectors
)
5918 /* first block is stored on second to last sector of the disk */
5919 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0)
5922 if (write(fd
, buf
, 512) != 512)
5928 static void imsm_sync_metadata(struct supertype
*container
)
5930 struct intel_super
*super
= container
->sb
;
5932 dprintf("sync metadata: %d\n", super
->updates_pending
);
5933 if (!super
->updates_pending
)
5936 write_super_imsm(container
, 0);
5938 super
->updates_pending
= 0;
5941 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
5943 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
5944 int i
= get_imsm_disk_idx(dev
, idx
, -1);
5947 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5951 if (dl
&& is_failed(&dl
->disk
))
5955 dprintf("%s: found %x:%x\n", __func__
, dl
->major
, dl
->minor
);
5960 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
5961 struct active_array
*a
, int activate_new
,
5962 struct mdinfo
*additional_test_list
)
5964 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
5965 int idx
= get_imsm_disk_idx(dev
, slot
, -1);
5966 struct imsm_super
*mpb
= super
->anchor
;
5967 struct imsm_map
*map
;
5968 unsigned long long pos
;
5973 __u32 array_start
= 0;
5974 __u32 array_end
= 0;
5976 struct mdinfo
*test_list
;
5978 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5979 /* If in this array, skip */
5980 for (d
= a
->info
.devs
; d
; d
= d
->next
)
5981 if (d
->state_fd
>= 0 &&
5982 d
->disk
.major
== dl
->major
&&
5983 d
->disk
.minor
== dl
->minor
) {
5984 dprintf("%x:%x already in array\n",
5985 dl
->major
, dl
->minor
);
5990 test_list
= additional_test_list
;
5992 if (test_list
->disk
.major
== dl
->major
&&
5993 test_list
->disk
.minor
== dl
->minor
) {
5994 dprintf("%x:%x already in additional test list\n",
5995 dl
->major
, dl
->minor
);
5998 test_list
= test_list
->next
;
6003 /* skip in use or failed drives */
6004 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
6006 dprintf("%x:%x status (failed: %d index: %d)\n",
6007 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
6011 /* skip pure spares when we are looking for partially
6012 * assimilated drives
6014 if (dl
->index
== -1 && !activate_new
)
6017 /* Does this unused device have the requisite free space?
6018 * It needs to be able to cover all member volumes
6020 ex
= get_extents(super
, dl
);
6022 dprintf("cannot get extents\n");
6025 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6026 dev
= get_imsm_dev(super
, i
);
6027 map
= get_imsm_map(dev
, 0);
6029 /* check if this disk is already a member of
6032 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
6038 array_start
= __le32_to_cpu(map
->pba_of_lba0
);
6039 array_end
= array_start
+
6040 __le32_to_cpu(map
->blocks_per_member
) - 1;
6043 /* check that we can start at pba_of_lba0 with
6044 * blocks_per_member of space
6046 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
6050 pos
= ex
[j
].start
+ ex
[j
].size
;
6052 } while (ex
[j
-1].size
);
6059 if (i
< mpb
->num_raid_devs
) {
6060 dprintf("%x:%x does not have %u to %u available\n",
6061 dl
->major
, dl
->minor
, array_start
, array_end
);
6072 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
6074 struct imsm_dev
*dev2
;
6075 struct imsm_map
*map
;
6081 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
6083 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
);
6084 if (state
== IMSM_T_STATE_FAILED
) {
6085 map
= get_imsm_map(dev2
, 0);
6088 for (slot
= 0; slot
< map
->num_members
; slot
++) {
6090 * Check if failed disks are deleted from intel
6091 * disk list or are marked to be deleted
6093 idx
= get_imsm_disk_idx(dev2
, slot
, -1);
6094 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
6096 * Do not rebuild the array if failed disks
6097 * from failed sub-array are not removed from
6101 is_failed(&idisk
->disk
) &&
6102 (idisk
->action
!= DISK_REMOVE
))
6110 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
6111 struct metadata_update
**updates
)
6114 * Find a device with unused free space and use it to replace a
6115 * failed/vacant region in an array. We replace failed regions one a
6116 * array at a time. The result is that a new spare disk will be added
6117 * to the first failed array and after the monitor has finished
6118 * propagating failures the remainder will be consumed.
6120 * FIXME add a capability for mdmon to request spares from another
6124 struct intel_super
*super
= a
->container
->sb
;
6125 int inst
= a
->info
.container_member
;
6126 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
6127 struct imsm_map
*map
= get_imsm_map(dev
, 0);
6128 int failed
= a
->info
.array
.raid_disks
;
6129 struct mdinfo
*rv
= NULL
;
6132 struct metadata_update
*mu
;
6134 struct imsm_update_activate_spare
*u
;
6139 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
6140 if ((d
->curr_state
& DS_FAULTY
) &&
6142 /* wait for Removal to happen */
6144 if (d
->state_fd
>= 0)
6148 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
6149 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
6151 if (dev
->vol
.migr_state
&&
6152 dev
->vol
.migr_type
== MIGR_GEN_MIGR
)
6153 /* No repair during migration */
6156 if (a
->info
.array
.level
== 4)
6157 /* No repair for takeovered array
6158 * imsm doesn't support raid4
6162 if (imsm_check_degraded(super
, dev
, failed
) != IMSM_T_STATE_DEGRADED
)
6166 * If there are any failed disks check state of the other volume.
6167 * Block rebuild if the another one is failed until failed disks
6168 * are removed from container.
6171 dprintf("found failed disks in %s, check if there another"
6172 "failed sub-array.\n",
6174 /* check if states of the other volumes allow for rebuild */
6175 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
6177 allowed
= imsm_rebuild_allowed(a
->container
,
6185 /* For each slot, if it is not working, find a spare */
6186 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
6187 for (d
= a
->info
.devs
; d
; d
= d
->next
)
6188 if (d
->disk
.raid_disk
== i
)
6190 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
6191 if (d
&& (d
->state_fd
>= 0))
6195 * OK, this device needs recovery. Try to re-add the
6196 * previous occupant of this slot, if this fails see if
6197 * we can continue the assimilation of a spare that was
6198 * partially assimilated, finally try to activate a new
6201 dl
= imsm_readd(super
, i
, a
);
6203 dl
= imsm_add_spare(super
, i
, a
, 0, NULL
);
6205 dl
= imsm_add_spare(super
, i
, a
, 1, NULL
);
6209 /* found a usable disk with enough space */
6210 di
= malloc(sizeof(*di
));
6213 memset(di
, 0, sizeof(*di
));
6215 /* dl->index will be -1 in the case we are activating a
6216 * pristine spare. imsm_process_update() will create a
6217 * new index in this case. Once a disk is found to be
6218 * failed in all member arrays it is kicked from the
6221 di
->disk
.number
= dl
->index
;
6223 /* (ab)use di->devs to store a pointer to the device
6226 di
->devs
= (struct mdinfo
*) dl
;
6228 di
->disk
.raid_disk
= i
;
6229 di
->disk
.major
= dl
->major
;
6230 di
->disk
.minor
= dl
->minor
;
6232 di
->recovery_start
= 0;
6233 di
->data_offset
= __le32_to_cpu(map
->pba_of_lba0
);
6234 di
->component_size
= a
->info
.component_size
;
6235 di
->container_member
= inst
;
6236 super
->random
= random32();
6240 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
6241 i
, di
->data_offset
);
6247 /* No spares found */
6249 /* Now 'rv' has a list of devices to return.
6250 * Create a metadata_update record to update the
6251 * disk_ord_tbl for the array
6253 mu
= malloc(sizeof(*mu
));
6255 mu
->buf
= malloc(sizeof(struct imsm_update_activate_spare
) * num_spares
);
6256 if (mu
->buf
== NULL
) {
6263 struct mdinfo
*n
= rv
->next
;
6272 mu
->space_list
= NULL
;
6273 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
6274 mu
->next
= *updates
;
6275 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
6277 for (di
= rv
; di
; di
= di
->next
) {
6278 u
->type
= update_activate_spare
;
6279 u
->dl
= (struct dl
*) di
->devs
;
6281 u
->slot
= di
->disk
.raid_disk
;
6292 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
6294 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
6295 struct imsm_map
*map
= get_imsm_map(dev
, 0);
6296 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, 0);
6297 struct disk_info
*inf
= get_disk_info(u
);
6298 struct imsm_disk
*disk
;
6302 for (i
= 0; i
< map
->num_members
; i
++) {
6303 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, -1));
6304 for (j
= 0; j
< new_map
->num_members
; j
++)
6305 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
6313 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
6315 struct dl
*dl
= NULL
;
6316 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6317 if ((dl
->major
== major
) && (dl
->minor
== minor
))
6322 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
6324 struct dl
*prev
= NULL
;
6328 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6329 if ((dl
->major
== major
) && (dl
->minor
== minor
)) {
6332 prev
->next
= dl
->next
;
6334 super
->disks
= dl
->next
;
6336 __free_imsm_disk(dl
);
6337 dprintf("%s: removed %x:%x\n",
6338 __func__
, major
, minor
);
6346 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
6348 static int add_remove_disk_update(struct intel_super
*super
)
6350 int check_degraded
= 0;
6351 struct dl
*disk
= NULL
;
6352 /* add/remove some spares to/from the metadata/contrainer */
6353 while (super
->disk_mgmt_list
) {
6354 struct dl
*disk_cfg
;
6356 disk_cfg
= super
->disk_mgmt_list
;
6357 super
->disk_mgmt_list
= disk_cfg
->next
;
6358 disk_cfg
->next
= NULL
;
6360 if (disk_cfg
->action
== DISK_ADD
) {
6361 disk_cfg
->next
= super
->disks
;
6362 super
->disks
= disk_cfg
;
6364 dprintf("%s: added %x:%x\n",
6365 __func__
, disk_cfg
->major
,
6367 } else if (disk_cfg
->action
== DISK_REMOVE
) {
6368 dprintf("Disk remove action processed: %x.%x\n",
6369 disk_cfg
->major
, disk_cfg
->minor
);
6370 disk
= get_disk_super(super
,
6374 /* store action status */
6375 disk
->action
= DISK_REMOVE
;
6376 /* remove spare disks only */
6377 if (disk
->index
== -1) {
6378 remove_disk_super(super
,
6383 /* release allocate disk structure */
6384 __free_imsm_disk(disk_cfg
);
6387 return check_degraded
;
6391 static int apply_reshape_migration_update(struct imsm_update_reshape_migration
*u
,
6392 struct intel_super
*super
,
6395 struct intel_dev
*id
;
6396 void **tofree
= NULL
;
6399 dprintf("apply_reshape_migration_update()\n");
6400 if ((u
->subdev
< 0) ||
6402 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
6405 if ((space_list
== NULL
) || (*space_list
== NULL
)) {
6406 dprintf("imsm: Error: Memory is not allocated\n");
6410 for (id
= super
->devlist
; id
; id
= id
->next
) {
6411 if (id
->index
== (unsigned)u
->subdev
) {
6412 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
6413 struct imsm_map
*map
;
6414 struct imsm_dev
*new_dev
=
6415 (struct imsm_dev
*)*space_list
;
6416 struct imsm_map
*migr_map
= get_imsm_map(dev
, 1);
6418 struct dl
*new_disk
;
6420 if (new_dev
== NULL
)
6422 *space_list
= **space_list
;
6423 memcpy(new_dev
, dev
, sizeof_imsm_dev(dev
, 0));
6424 map
= get_imsm_map(new_dev
, 0);
6426 dprintf("imsm: Error: migration in progress");
6430 to_state
= map
->map_state
;
6431 if ((u
->new_level
== 5) && (map
->raid_level
== 0)) {
6433 /* this should not happen */
6434 if (u
->new_disks
[0] < 0) {
6435 map
->failed_disk_num
=
6436 map
->num_members
- 1;
6437 to_state
= IMSM_T_STATE_DEGRADED
;
6439 to_state
= IMSM_T_STATE_NORMAL
;
6441 migrate(new_dev
, super
, to_state
, MIGR_GEN_MIGR
);
6442 if (u
->new_level
> -1)
6443 map
->raid_level
= u
->new_level
;
6444 migr_map
= get_imsm_map(new_dev
, 1);
6445 if ((u
->new_level
== 5) &&
6446 (migr_map
->raid_level
== 0)) {
6447 int ord
= map
->num_members
- 1;
6448 migr_map
->num_members
--;
6449 if (u
->new_disks
[0] < 0)
6450 ord
|= IMSM_ORD_REBUILD
;
6451 set_imsm_ord_tbl_ent(map
,
6452 map
->num_members
- 1,
6456 tofree
= (void **)dev
;
6458 /* update chunk size
6460 if (u
->new_chunksize
> 0)
6461 map
->blocks_per_strip
=
6462 __cpu_to_le16(u
->new_chunksize
* 2);
6466 if ((u
->new_level
!= 5) ||
6467 (migr_map
->raid_level
!= 0) ||
6468 (migr_map
->raid_level
== map
->raid_level
))
6471 if (u
->new_disks
[0] >= 0) {
6474 new_disk
= get_disk_super(super
,
6475 major(u
->new_disks
[0]),
6476 minor(u
->new_disks
[0]));
6477 dprintf("imsm: new disk for reshape is: %i:%i "
6478 "(%p, index = %i)\n",
6479 major(u
->new_disks
[0]),
6480 minor(u
->new_disks
[0]),
6481 new_disk
, new_disk
->index
);
6482 if (new_disk
== NULL
)
6483 goto error_disk_add
;
6485 new_disk
->index
= map
->num_members
- 1;
6486 /* slot to fill in autolayout
6488 new_disk
->raiddisk
= new_disk
->index
;
6489 new_disk
->disk
.status
|= CONFIGURED_DISK
;
6490 new_disk
->disk
.status
&= ~SPARE_DISK
;
6492 goto error_disk_add
;
6495 *tofree
= *space_list
;
6496 /* calculate new size
6498 imsm_set_array_size(new_dev
);
6505 *space_list
= tofree
;
6509 dprintf("Error: imsm: Cannot find disk.\n");
6514 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
6515 struct intel_super
*super
,
6518 struct dl
*new_disk
;
6519 struct intel_dev
*id
;
6521 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
6522 int disk_count
= u
->old_raid_disks
;
6523 void **tofree
= NULL
;
6524 int devices_to_reshape
= 1;
6525 struct imsm_super
*mpb
= super
->anchor
;
6527 unsigned int dev_id
;
6529 dprintf("imsm: apply_reshape_container_disks_update()\n");
6531 /* enable spares to use in array */
6532 for (i
= 0; i
< delta_disks
; i
++) {
6533 new_disk
= get_disk_super(super
,
6534 major(u
->new_disks
[i
]),
6535 minor(u
->new_disks
[i
]));
6536 dprintf("imsm: new disk for reshape is: %i:%i "
6537 "(%p, index = %i)\n",
6538 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
6539 new_disk
, new_disk
->index
);
6540 if ((new_disk
== NULL
) ||
6541 ((new_disk
->index
>= 0) &&
6542 (new_disk
->index
< u
->old_raid_disks
)))
6543 goto update_reshape_exit
;
6544 new_disk
->index
= disk_count
++;
6545 /* slot to fill in autolayout
6547 new_disk
->raiddisk
= new_disk
->index
;
6548 new_disk
->disk
.status
|=
6550 new_disk
->disk
.status
&= ~SPARE_DISK
;
6553 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
6554 mpb
->num_raid_devs
);
6555 /* manage changes in volume
6557 for (dev_id
= 0; dev_id
< mpb
->num_raid_devs
; dev_id
++) {
6558 void **sp
= *space_list
;
6559 struct imsm_dev
*newdev
;
6560 struct imsm_map
*newmap
, *oldmap
;
6562 for (id
= super
->devlist
; id
; id
= id
->next
) {
6563 if (id
->index
== dev_id
)
6572 /* Copy the dev, but not (all of) the map */
6573 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
6574 oldmap
= get_imsm_map(id
->dev
, 0);
6575 newmap
= get_imsm_map(newdev
, 0);
6576 /* Copy the current map */
6577 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
6578 /* update one device only
6580 if (devices_to_reshape
) {
6581 dprintf("imsm: modifying subdev: %i\n",
6583 devices_to_reshape
--;
6584 newdev
->vol
.migr_state
= 1;
6585 newdev
->vol
.curr_migr_unit
= 0;
6586 newdev
->vol
.migr_type
= MIGR_GEN_MIGR
;
6587 newmap
->num_members
= u
->new_raid_disks
;
6588 for (i
= 0; i
< delta_disks
; i
++) {
6589 set_imsm_ord_tbl_ent(newmap
,
6590 u
->old_raid_disks
+ i
,
6591 u
->old_raid_disks
+ i
);
6593 /* New map is correct, now need to save old map
6595 newmap
= get_imsm_map(newdev
, 1);
6596 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
6598 imsm_set_array_size(newdev
);
6601 sp
= (void **)id
->dev
;
6606 /* Clear migration record */
6607 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
6610 *space_list
= tofree
;
6613 update_reshape_exit
:
6618 static int apply_takeover_update(struct imsm_update_takeover
*u
,
6619 struct intel_super
*super
,
6622 struct imsm_dev
*dev
= NULL
;
6623 struct intel_dev
*dv
;
6624 struct imsm_dev
*dev_new
;
6625 struct imsm_map
*map
;
6629 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
6630 if (dv
->index
== (unsigned int)u
->subarray
) {
6638 map
= get_imsm_map(dev
, 0);
6640 if (u
->direction
== R10_TO_R0
) {
6641 /* Number of failed disks must be half of initial disk number */
6642 if (imsm_count_failed(super
, dev
) != (map
->num_members
/ 2))
6645 /* iterate through devices to mark removed disks as spare */
6646 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
6647 if (dm
->disk
.status
& FAILED_DISK
) {
6648 int idx
= dm
->index
;
6649 /* update indexes on the disk list */
6650 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
6651 the index values will end up being correct.... NB */
6652 for (du
= super
->disks
; du
; du
= du
->next
)
6653 if (du
->index
> idx
)
6655 /* mark as spare disk */
6656 dm
->disk
.status
= SPARE_DISK
;
6661 map
->num_members
= map
->num_members
/ 2;
6662 map
->map_state
= IMSM_T_STATE_NORMAL
;
6663 map
->num_domains
= 1;
6664 map
->raid_level
= 0;
6665 map
->failed_disk_num
= -1;
6668 if (u
->direction
== R0_TO_R10
) {
6670 /* update slots in current disk list */
6671 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
6675 /* create new *missing* disks */
6676 for (i
= 0; i
< map
->num_members
; i
++) {
6677 space
= *space_list
;
6680 *space_list
= *space
;
6682 memcpy(du
, super
->disks
, sizeof(*du
));
6686 du
->index
= (i
* 2) + 1;
6687 sprintf((char *)du
->disk
.serial
,
6688 " MISSING_%d", du
->index
);
6689 sprintf((char *)du
->serial
,
6690 "MISSING_%d", du
->index
);
6691 du
->next
= super
->missing
;
6692 super
->missing
= du
;
6694 /* create new dev and map */
6695 space
= *space_list
;
6698 *space_list
= *space
;
6699 dev_new
= (void *)space
;
6700 memcpy(dev_new
, dev
, sizeof(*dev
));
6701 /* update new map */
6702 map
= get_imsm_map(dev_new
, 0);
6703 map
->num_members
= map
->num_members
* 2;
6704 map
->map_state
= IMSM_T_STATE_DEGRADED
;
6705 map
->num_domains
= 2;
6706 map
->raid_level
= 1;
6707 /* replace dev<->dev_new */
6710 /* update disk order table */
6711 for (du
= super
->disks
; du
; du
= du
->next
)
6713 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
6714 for (du
= super
->missing
; du
; du
= du
->next
)
6715 if (du
->index
>= 0) {
6716 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
6717 mark_missing(dev_new
, &du
->disk
, du
->index
);
6723 static void imsm_process_update(struct supertype
*st
,
6724 struct metadata_update
*update
)
6727 * crack open the metadata_update envelope to find the update record
6728 * update can be one of:
6729 * update_reshape_container_disks - all the arrays in the container
6730 * are being reshaped to have more devices. We need to mark
6731 * the arrays for general migration and convert selected spares
6732 * into active devices.
6733 * update_activate_spare - a spare device has replaced a failed
6734 * device in an array, update the disk_ord_tbl. If this disk is
6735 * present in all member arrays then also clear the SPARE_DISK
6737 * update_create_array
6739 * update_rename_array
6740 * update_add_remove_disk
6742 struct intel_super
*super
= st
->sb
;
6743 struct imsm_super
*mpb
;
6744 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
6746 /* update requires a larger buf but the allocation failed */
6747 if (super
->next_len
&& !super
->next_buf
) {
6748 super
->next_len
= 0;
6752 if (super
->next_buf
) {
6753 memcpy(super
->next_buf
, super
->buf
, super
->len
);
6755 super
->len
= super
->next_len
;
6756 super
->buf
= super
->next_buf
;
6758 super
->next_len
= 0;
6759 super
->next_buf
= NULL
;
6762 mpb
= super
->anchor
;
6765 case update_takeover
: {
6766 struct imsm_update_takeover
*u
= (void *)update
->buf
;
6767 if (apply_takeover_update(u
, super
, &update
->space_list
)) {
6768 imsm_update_version_info(super
);
6769 super
->updates_pending
++;
6774 case update_reshape_container_disks
: {
6775 struct imsm_update_reshape
*u
= (void *)update
->buf
;
6776 if (apply_reshape_container_disks_update(
6777 u
, super
, &update
->space_list
))
6778 super
->updates_pending
++;
6781 case update_reshape_migration
: {
6782 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
6783 if (apply_reshape_migration_update(
6784 u
, super
, &update
->space_list
))
6785 super
->updates_pending
++;
6788 case update_activate_spare
: {
6789 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
6790 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
6791 struct imsm_map
*map
= get_imsm_map(dev
, 0);
6792 struct imsm_map
*migr_map
;
6793 struct active_array
*a
;
6794 struct imsm_disk
*disk
;
6799 int victim
= get_imsm_disk_idx(dev
, u
->slot
, -1);
6802 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6807 fprintf(stderr
, "error: imsm_activate_spare passed "
6808 "an unknown disk (index: %d)\n",
6813 super
->updates_pending
++;
6814 /* count failures (excluding rebuilds and the victim)
6815 * to determine map[0] state
6818 for (i
= 0; i
< map
->num_members
; i
++) {
6821 disk
= get_imsm_disk(super
,
6822 get_imsm_disk_idx(dev
, i
, -1));
6823 if (!disk
|| is_failed(disk
))
6827 /* adding a pristine spare, assign a new index */
6828 if (dl
->index
< 0) {
6829 dl
->index
= super
->anchor
->num_disks
;
6830 super
->anchor
->num_disks
++;
6833 disk
->status
|= CONFIGURED_DISK
;
6834 disk
->status
&= ~SPARE_DISK
;
6837 to_state
= imsm_check_degraded(super
, dev
, failed
);
6838 map
->map_state
= IMSM_T_STATE_DEGRADED
;
6839 migrate(dev
, super
, to_state
, MIGR_REBUILD
);
6840 migr_map
= get_imsm_map(dev
, 1);
6841 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
6842 set_imsm_ord_tbl_ent(migr_map
, u
->slot
, dl
->index
| IMSM_ORD_REBUILD
);
6844 /* update the family_num to mark a new container
6845 * generation, being careful to record the existing
6846 * family_num in orig_family_num to clean up after
6847 * earlier mdadm versions that neglected to set it.
6849 if (mpb
->orig_family_num
== 0)
6850 mpb
->orig_family_num
= mpb
->family_num
;
6851 mpb
->family_num
+= super
->random
;
6853 /* count arrays using the victim in the metadata */
6855 for (a
= st
->arrays
; a
; a
= a
->next
) {
6856 dev
= get_imsm_dev(super
, a
->info
.container_member
);
6857 map
= get_imsm_map(dev
, 0);
6859 if (get_imsm_disk_slot(map
, victim
) >= 0)
6863 /* delete the victim if it is no longer being
6869 /* We know that 'manager' isn't touching anything,
6870 * so it is safe to delete
6872 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
6873 if ((*dlp
)->index
== victim
)
6876 /* victim may be on the missing list */
6878 for (dlp
= &super
->missing
; *dlp
; dlp
= &(*dlp
)->next
)
6879 if ((*dlp
)->index
== victim
)
6881 imsm_delete(super
, dlp
, victim
);
6885 case update_create_array
: {
6886 /* someone wants to create a new array, we need to be aware of
6887 * a few races/collisions:
6888 * 1/ 'Create' called by two separate instances of mdadm
6889 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
6890 * devices that have since been assimilated via
6892 * In the event this update can not be carried out mdadm will
6893 * (FIX ME) notice that its update did not take hold.
6895 struct imsm_update_create_array
*u
= (void *) update
->buf
;
6896 struct intel_dev
*dv
;
6897 struct imsm_dev
*dev
;
6898 struct imsm_map
*map
, *new_map
;
6899 unsigned long long start
, end
;
6900 unsigned long long new_start
, new_end
;
6902 struct disk_info
*inf
;
6905 /* handle racing creates: first come first serve */
6906 if (u
->dev_idx
< mpb
->num_raid_devs
) {
6907 dprintf("%s: subarray %d already defined\n",
6908 __func__
, u
->dev_idx
);
6912 /* check update is next in sequence */
6913 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
6914 dprintf("%s: can not create array %d expected index %d\n",
6915 __func__
, u
->dev_idx
, mpb
->num_raid_devs
);
6919 new_map
= get_imsm_map(&u
->dev
, 0);
6920 new_start
= __le32_to_cpu(new_map
->pba_of_lba0
);
6921 new_end
= new_start
+ __le32_to_cpu(new_map
->blocks_per_member
);
6922 inf
= get_disk_info(u
);
6924 /* handle activate_spare versus create race:
6925 * check to make sure that overlapping arrays do not include
6928 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6929 dev
= get_imsm_dev(super
, i
);
6930 map
= get_imsm_map(dev
, 0);
6931 start
= __le32_to_cpu(map
->pba_of_lba0
);
6932 end
= start
+ __le32_to_cpu(map
->blocks_per_member
);
6933 if ((new_start
>= start
&& new_start
<= end
) ||
6934 (start
>= new_start
&& start
<= new_end
))
6939 if (disks_overlap(super
, i
, u
)) {
6940 dprintf("%s: arrays overlap\n", __func__
);
6945 /* check that prepare update was successful */
6946 if (!update
->space
) {
6947 dprintf("%s: prepare update failed\n", __func__
);
6951 /* check that all disks are still active before committing
6952 * changes. FIXME: could we instead handle this by creating a
6953 * degraded array? That's probably not what the user expects,
6954 * so better to drop this update on the floor.
6956 for (i
= 0; i
< new_map
->num_members
; i
++) {
6957 dl
= serial_to_dl(inf
[i
].serial
, super
);
6959 dprintf("%s: disk disappeared\n", __func__
);
6964 super
->updates_pending
++;
6966 /* convert spares to members and fixup ord_tbl */
6967 for (i
= 0; i
< new_map
->num_members
; i
++) {
6968 dl
= serial_to_dl(inf
[i
].serial
, super
);
6969 if (dl
->index
== -1) {
6970 dl
->index
= mpb
->num_disks
;
6972 dl
->disk
.status
|= CONFIGURED_DISK
;
6973 dl
->disk
.status
&= ~SPARE_DISK
;
6975 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
6980 update
->space
= NULL
;
6981 imsm_copy_dev(dev
, &u
->dev
);
6982 dv
->index
= u
->dev_idx
;
6983 dv
->next
= super
->devlist
;
6984 super
->devlist
= dv
;
6985 mpb
->num_raid_devs
++;
6987 imsm_update_version_info(super
);
6990 /* mdmon knows how to release update->space, but not
6991 * ((struct intel_dev *) update->space)->dev
6993 if (update
->space
) {
6999 case update_kill_array
: {
7000 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
7001 int victim
= u
->dev_idx
;
7002 struct active_array
*a
;
7003 struct intel_dev
**dp
;
7004 struct imsm_dev
*dev
;
7006 /* sanity check that we are not affecting the uuid of
7007 * active arrays, or deleting an active array
7009 * FIXME when immutable ids are available, but note that
7010 * we'll also need to fixup the invalidated/active
7011 * subarray indexes in mdstat
7013 for (a
= st
->arrays
; a
; a
= a
->next
)
7014 if (a
->info
.container_member
>= victim
)
7016 /* by definition if mdmon is running at least one array
7017 * is active in the container, so checking
7018 * mpb->num_raid_devs is just extra paranoia
7020 dev
= get_imsm_dev(super
, victim
);
7021 if (a
|| !dev
|| mpb
->num_raid_devs
== 1) {
7022 dprintf("failed to delete subarray-%d\n", victim
);
7026 for (dp
= &super
->devlist
; *dp
;)
7027 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
7030 if ((*dp
)->index
> (unsigned)victim
)
7034 mpb
->num_raid_devs
--;
7035 super
->updates_pending
++;
7038 case update_rename_array
: {
7039 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
7040 char name
[MAX_RAID_SERIAL_LEN
+1];
7041 int target
= u
->dev_idx
;
7042 struct active_array
*a
;
7043 struct imsm_dev
*dev
;
7045 /* sanity check that we are not affecting the uuid of
7048 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
7049 name
[MAX_RAID_SERIAL_LEN
] = '\0';
7050 for (a
= st
->arrays
; a
; a
= a
->next
)
7051 if (a
->info
.container_member
== target
)
7053 dev
= get_imsm_dev(super
, u
->dev_idx
);
7054 if (a
|| !dev
|| !check_name(super
, name
, 1)) {
7055 dprintf("failed to rename subarray-%d\n", target
);
7059 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
7060 super
->updates_pending
++;
7063 case update_add_remove_disk
: {
7064 /* we may be able to repair some arrays if disks are
7065 * being added, check teh status of add_remove_disk
7066 * if discs has been added.
7068 if (add_remove_disk_update(super
)) {
7069 struct active_array
*a
;
7071 super
->updates_pending
++;
7072 for (a
= st
->arrays
; a
; a
= a
->next
)
7073 a
->check_degraded
= 1;
7078 fprintf(stderr
, "error: unsuported process update type:"
7079 "(type: %d)\n", type
);
7083 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
);
7085 static void imsm_prepare_update(struct supertype
*st
,
7086 struct metadata_update
*update
)
7089 * Allocate space to hold new disk entries, raid-device entries or a new
7090 * mpb if necessary. The manager synchronously waits for updates to
7091 * complete in the monitor, so new mpb buffers allocated here can be
7092 * integrated by the monitor thread without worrying about live pointers
7093 * in the manager thread.
7095 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
7096 struct intel_super
*super
= st
->sb
;
7097 struct imsm_super
*mpb
= super
->anchor
;
7102 case update_takeover
: {
7103 struct imsm_update_takeover
*u
= (void *)update
->buf
;
7104 if (u
->direction
== R0_TO_R10
) {
7105 void **tail
= (void **)&update
->space_list
;
7106 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subarray
);
7107 struct imsm_map
*map
= get_imsm_map(dev
, 0);
7108 int num_members
= map
->num_members
;
7112 /* allocate memory for added disks */
7113 for (i
= 0; i
< num_members
; i
++) {
7114 size
= sizeof(struct dl
);
7115 space
= malloc(size
);
7124 /* allocate memory for new device */
7125 size
= sizeof_imsm_dev(super
->devlist
->dev
, 0) +
7126 (num_members
* sizeof(__u32
));
7127 space
= malloc(size
);
7136 len
= disks_to_mpb_size(num_members
* 2);
7138 /* if allocation didn't success, free buffer */
7139 while (update
->space_list
) {
7140 void **sp
= update
->space_list
;
7141 update
->space_list
= *sp
;
7149 case update_reshape_container_disks
: {
7150 /* Every raid device in the container is about to
7151 * gain some more devices, and we will enter a
7153 * So each 'imsm_map' will be bigger, and the imsm_vol
7154 * will now hold 2 of them.
7155 * Thus we need new 'struct imsm_dev' allocations sized
7156 * as sizeof_imsm_dev but with more devices in both maps.
7158 struct imsm_update_reshape
*u
= (void *)update
->buf
;
7159 struct intel_dev
*dl
;
7160 void **space_tail
= (void**)&update
->space_list
;
7162 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
7164 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
7165 int size
= sizeof_imsm_dev(dl
->dev
, 1);
7167 if (u
->new_raid_disks
> u
->old_raid_disks
)
7168 size
+= sizeof(__u32
)*2*
7169 (u
->new_raid_disks
- u
->old_raid_disks
);
7178 len
= disks_to_mpb_size(u
->new_raid_disks
);
7179 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
7182 case update_reshape_migration
: {
7183 /* for migration level 0->5 we need to add disks
7184 * so the same as for container operation we will copy
7185 * device to the bigger location.
7186 * in memory prepared device and new disk area are prepared
7187 * for usage in process update
7189 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
7190 struct intel_dev
*id
;
7191 void **space_tail
= (void **)&update
->space_list
;
7194 int current_level
= -1;
7196 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
7198 /* add space for bigger array in update
7200 for (id
= super
->devlist
; id
; id
= id
->next
) {
7201 if (id
->index
== (unsigned)u
->subdev
) {
7202 size
= sizeof_imsm_dev(id
->dev
, 1);
7203 if (u
->new_raid_disks
> u
->old_raid_disks
)
7204 size
+= sizeof(__u32
)*2*
7205 (u
->new_raid_disks
- u
->old_raid_disks
);
7215 if (update
->space_list
== NULL
)
7218 /* add space for disk in update
7220 size
= sizeof(struct dl
);
7223 free(update
->space_list
);
7224 update
->space_list
= NULL
;
7231 /* add spare device to update
7233 for (id
= super
->devlist
; id
; id
= id
->next
)
7234 if (id
->index
== (unsigned)u
->subdev
) {
7235 struct imsm_dev
*dev
;
7236 struct imsm_map
*map
;
7238 dev
= get_imsm_dev(super
, u
->subdev
);
7239 map
= get_imsm_map(dev
, 0);
7240 current_level
= map
->raid_level
;
7243 if ((u
->new_level
== 5) && (u
->new_level
!= current_level
)) {
7244 struct mdinfo
*spares
;
7246 spares
= get_spares_for_grow(st
);
7254 makedev(dev
->disk
.major
,
7256 dl
= get_disk_super(super
,
7259 dl
->index
= u
->old_raid_disks
;
7265 len
= disks_to_mpb_size(u
->new_raid_disks
);
7266 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
7269 case update_create_array
: {
7270 struct imsm_update_create_array
*u
= (void *) update
->buf
;
7271 struct intel_dev
*dv
;
7272 struct imsm_dev
*dev
= &u
->dev
;
7273 struct imsm_map
*map
= get_imsm_map(dev
, 0);
7275 struct disk_info
*inf
;
7279 inf
= get_disk_info(u
);
7280 len
= sizeof_imsm_dev(dev
, 1);
7281 /* allocate a new super->devlist entry */
7282 dv
= malloc(sizeof(*dv
));
7284 dv
->dev
= malloc(len
);
7289 update
->space
= NULL
;
7293 /* count how many spares will be converted to members */
7294 for (i
= 0; i
< map
->num_members
; i
++) {
7295 dl
= serial_to_dl(inf
[i
].serial
, super
);
7297 /* hmm maybe it failed?, nothing we can do about
7302 if (count_memberships(dl
, super
) == 0)
7305 len
+= activate
* sizeof(struct imsm_disk
);
7312 /* check if we need a larger metadata buffer */
7313 if (super
->next_buf
)
7314 buf_len
= super
->next_len
;
7316 buf_len
= super
->len
;
7318 if (__le32_to_cpu(mpb
->mpb_size
) + len
> buf_len
) {
7319 /* ok we need a larger buf than what is currently allocated
7320 * if this allocation fails process_update will notice that
7321 * ->next_len is set and ->next_buf is NULL
7323 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + len
, 512);
7324 if (super
->next_buf
)
7325 free(super
->next_buf
);
7327 super
->next_len
= buf_len
;
7328 if (posix_memalign(&super
->next_buf
, 512, buf_len
) == 0)
7329 memset(super
->next_buf
, 0, buf_len
);
7331 super
->next_buf
= NULL
;
7335 /* must be called while manager is quiesced */
7336 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
7338 struct imsm_super
*mpb
= super
->anchor
;
7340 struct imsm_dev
*dev
;
7341 struct imsm_map
*map
;
7342 int i
, j
, num_members
;
7345 dprintf("%s: deleting device[%d] from imsm_super\n",
7348 /* shift all indexes down one */
7349 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
7350 if (iter
->index
> (int)index
)
7352 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
7353 if (iter
->index
> (int)index
)
7356 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7357 dev
= get_imsm_dev(super
, i
);
7358 map
= get_imsm_map(dev
, 0);
7359 num_members
= map
->num_members
;
7360 for (j
= 0; j
< num_members
; j
++) {
7361 /* update ord entries being careful not to propagate
7362 * ord-flags to the first map
7364 ord
= get_imsm_ord_tbl_ent(dev
, j
, -1);
7366 if (ord_to_idx(ord
) <= index
)
7369 map
= get_imsm_map(dev
, 0);
7370 set_imsm_ord_tbl_ent(map
, j
, ord_to_idx(ord
- 1));
7371 map
= get_imsm_map(dev
, 1);
7373 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
7378 super
->updates_pending
++;
7380 struct dl
*dl
= *dlp
;
7382 *dlp
= (*dlp
)->next
;
7383 __free_imsm_disk(dl
);
7387 /*******************************************************************************
7388 * Function: open_backup_targets
7389 * Description: Function opens file descriptors for all devices given in
7392 * info : general array info
7393 * raid_disks : number of disks
7394 * raid_fds : table of device's file descriptors
7398 ******************************************************************************/
7399 int open_backup_targets(struct mdinfo
*info
, int raid_disks
, int *raid_fds
)
7403 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
7406 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
)) {
7407 dprintf("disk is faulty!!\n");
7411 if ((sd
->disk
.raid_disk
>= raid_disks
) ||
7412 (sd
->disk
.raid_disk
< 0))
7415 dn
= map_dev(sd
->disk
.major
,
7417 raid_fds
[sd
->disk
.raid_disk
] = dev_open(dn
, O_RDWR
);
7418 if (raid_fds
[sd
->disk
.raid_disk
] < 0) {
7419 fprintf(stderr
, "cannot open component\n");
7426 /*******************************************************************************
7427 * Function: init_migr_record_imsm
7428 * Description: Function inits imsm migration record
7430 * super : imsm internal array info
7431 * dev : device under migration
7432 * info : general array info to find the smallest device
7435 ******************************************************************************/
7436 void init_migr_record_imsm(struct supertype
*st
, struct imsm_dev
*dev
,
7437 struct mdinfo
*info
)
7439 struct intel_super
*super
= st
->sb
;
7440 struct migr_record
*migr_rec
= super
->migr_rec
;
7442 unsigned long long dsize
, dev_sectors
;
7443 long long unsigned min_dev_sectors
= -1LLU;
7447 struct imsm_map
*map_dest
= get_imsm_map(dev
, 0);
7448 struct imsm_map
*map_src
= get_imsm_map(dev
, 1);
7449 unsigned long long num_migr_units
;
7451 unsigned long long array_blocks
=
7452 (((unsigned long long)__le32_to_cpu(dev
->size_high
)) << 32) +
7453 __le32_to_cpu(dev
->size_low
);
7455 memset(migr_rec
, 0, sizeof(struct migr_record
));
7456 migr_rec
->family_num
= __cpu_to_le32(super
->anchor
->family_num
);
7458 /* only ascending reshape supported now */
7459 migr_rec
->ascending_migr
= __cpu_to_le32(1);
7461 migr_rec
->dest_depth_per_unit
= GEN_MIGR_AREA_SIZE
/
7462 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
7463 migr_rec
->dest_depth_per_unit
*= map_dest
->blocks_per_strip
;
7464 new_data_disks
= imsm_num_data_members(dev
, 0);
7465 migr_rec
->blocks_per_unit
=
7466 __cpu_to_le32(migr_rec
->dest_depth_per_unit
* new_data_disks
);
7467 migr_rec
->dest_depth_per_unit
=
7468 __cpu_to_le32(migr_rec
->dest_depth_per_unit
);
7471 array_blocks
/ __le32_to_cpu(migr_rec
->blocks_per_unit
);
7473 if (array_blocks
% __le32_to_cpu(migr_rec
->blocks_per_unit
))
7475 migr_rec
->num_migr_units
= __cpu_to_le32(num_migr_units
);
7477 migr_rec
->post_migr_vol_cap
= dev
->size_low
;
7478 migr_rec
->post_migr_vol_cap_hi
= dev
->size_high
;
7481 /* Find the smallest dev */
7482 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
7483 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
7484 fd
= dev_open(nm
, O_RDONLY
);
7487 get_dev_size(fd
, NULL
, &dsize
);
7488 dev_sectors
= dsize
/ 512;
7489 if (dev_sectors
< min_dev_sectors
)
7490 min_dev_sectors
= dev_sectors
;
7493 migr_rec
->ckpt_area_pba
= __cpu_to_le32(min_dev_sectors
-
7494 RAID_DISK_RESERVED_BLOCKS_IMSM_HI
);
7496 write_imsm_migr_rec(st
);
7501 /*******************************************************************************
7502 * Function: save_backup_imsm
7503 * Description: Function saves critical data stripes to Migration Copy Area
7504 * and updates the current migration unit status.
7505 * Use restore_stripes() to form a destination stripe,
7506 * and to write it to the Copy Area.
7508 * st : supertype information
7509 * info : general array info
7510 * buf : input buffer
7511 * write_offset : address of data to backup
7512 * length : length of data to backup (blocks_per_unit)
7516 ******************************************************************************/
7517 int save_backup_imsm(struct supertype
*st
,
7518 struct imsm_dev
*dev
,
7519 struct mdinfo
*info
,
7525 struct intel_super
*super
= st
->sb
;
7526 unsigned long long *target_offsets
= NULL
;
7527 int *targets
= NULL
;
7529 struct imsm_map
*map_dest
= get_imsm_map(dev
, 0);
7530 int new_disks
= map_dest
->num_members
;
7532 targets
= malloc(new_disks
* sizeof(int));
7536 target_offsets
= malloc(new_disks
* sizeof(unsigned long long));
7537 if (!target_offsets
)
7540 for (i
= 0; i
< new_disks
; i
++) {
7542 target_offsets
[i
] = (unsigned long long)
7543 __le32_to_cpu(super
->migr_rec
->ckpt_area_pba
) * 512;
7546 if (open_backup_targets(info
, new_disks
, targets
))
7549 if (restore_stripes(targets
, /* list of dest devices */
7550 target_offsets
, /* migration record offsets */
7555 -1, /* source backup file descriptor */
7556 0, /* input buf offset
7557 * always 0 buf is already offset */
7561 fprintf(stderr
, Name
": Error restoring stripes\n");
7569 for (i
= 0; i
< new_disks
; i
++)
7570 if (targets
[i
] >= 0)
7574 free(target_offsets
);
7579 /*******************************************************************************
7580 * Function: save_checkpoint_imsm
7581 * Description: Function called for current unit status update
7582 * in the migration record. It writes it to disk.
7584 * super : imsm internal array info
7585 * info : general array info
7589 ******************************************************************************/
7590 int save_checkpoint_imsm(struct supertype
*st
, struct mdinfo
*info
, int state
)
7592 struct intel_super
*super
= st
->sb
;
7593 load_imsm_migr_rec(super
, info
);
7594 if (__le32_to_cpu(super
->migr_rec
->blocks_per_unit
) == 0) {
7595 dprintf("ERROR: blocks_per_unit = 0!!!\n");
7599 super
->migr_rec
->curr_migr_unit
=
7600 __cpu_to_le32(info
->reshape_progress
/
7601 __le32_to_cpu(super
->migr_rec
->blocks_per_unit
));
7602 super
->migr_rec
->rec_status
= __cpu_to_le32(state
);
7603 super
->migr_rec
->dest_1st_member_lba
=
7604 __cpu_to_le32((__le32_to_cpu(super
->migr_rec
->curr_migr_unit
))
7605 * __le32_to_cpu(super
->migr_rec
->dest_depth_per_unit
));
7606 if (write_imsm_migr_rec(st
) < 0) {
7607 dprintf("imsm: Cannot write migration record "
7608 "outside backup area\n");
7615 static char disk_by_path
[] = "/dev/disk/by-path/";
7617 static const char *imsm_get_disk_controller_domain(const char *path
)
7619 char disk_path
[PATH_MAX
];
7623 strncpy(disk_path
, disk_by_path
, PATH_MAX
- 1);
7624 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
7625 if (stat(disk_path
, &st
) == 0) {
7626 struct sys_dev
* hba
;
7629 path
= devt_to_devpath(st
.st_rdev
);
7632 hba
= find_disk_attached_hba(-1, path
);
7633 if (hba
&& hba
->type
== SYS_DEV_SAS
)
7635 else if (hba
&& hba
->type
== SYS_DEV_SATA
)
7639 dprintf("path: %s hba: %s attached: %s\n",
7640 path
, (hba
) ? hba
->path
: "NULL", drv
);
7648 static int imsm_find_array_minor_by_subdev(int subdev
, int container
, int *minor
)
7650 char subdev_name
[20];
7651 struct mdstat_ent
*mdstat
;
7653 sprintf(subdev_name
, "%d", subdev
);
7654 mdstat
= mdstat_by_subdev(subdev_name
, container
);
7658 *minor
= mdstat
->devnum
;
7659 free_mdstat(mdstat
);
7663 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
7664 struct geo_params
*geo
,
7665 int *old_raid_disks
)
7667 /* currently we only support increasing the number of devices
7668 * for a container. This increases the number of device for each
7669 * member array. They must all be RAID0 or RAID5.
7672 struct mdinfo
*info
, *member
;
7673 int devices_that_can_grow
= 0;
7675 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): "
7676 "st->devnum = (%i)\n",
7679 if (geo
->size
!= -1 ||
7680 geo
->level
!= UnSet
||
7681 geo
->layout
!= UnSet
||
7682 geo
->chunksize
!= 0 ||
7683 geo
->raid_disks
== UnSet
) {
7684 dprintf("imsm: Container operation is allowed for "
7685 "raid disks number change only.\n");
7689 info
= container_content_imsm(st
, NULL
);
7690 for (member
= info
; member
; member
= member
->next
) {
7694 dprintf("imsm: checking device_num: %i\n",
7695 member
->container_member
);
7697 if (geo
->raid_disks
<= member
->array
.raid_disks
) {
7698 /* we work on container for Online Capacity Expansion
7699 * only so raid_disks has to grow
7701 dprintf("imsm: for container operation raid disks "
7702 "increase is required\n");
7706 if ((info
->array
.level
!= 0) &&
7707 (info
->array
.level
!= 5)) {
7708 /* we cannot use this container with other raid level
7710 dprintf("imsm: for container operation wrong"
7711 " raid level (%i) detected\n",
7715 /* check for platform support
7716 * for this raid level configuration
7718 struct intel_super
*super
= st
->sb
;
7719 if (!is_raid_level_supported(super
->orom
,
7720 member
->array
.level
,
7722 dprintf("platform does not support raid%d with"
7726 geo
->raid_disks
> 1 ? "s" : "");
7729 /* check if component size is aligned to chunk size
7731 if (info
->component_size
%
7732 (info
->array
.chunk_size
/512)) {
7733 dprintf("Component size is not aligned to "
7739 if (*old_raid_disks
&&
7740 info
->array
.raid_disks
!= *old_raid_disks
)
7742 *old_raid_disks
= info
->array
.raid_disks
;
7744 /* All raid5 and raid0 volumes in container
7745 * have to be ready for Online Capacity Expansion
7746 * so they need to be assembled. We have already
7747 * checked that no recovery etc is happening.
7749 result
= imsm_find_array_minor_by_subdev(member
->container_member
,
7753 dprintf("imsm: cannot find array\n");
7756 devices_that_can_grow
++;
7759 if (!member
&& devices_that_can_grow
)
7763 dprintf("\tContainer operation allowed\n");
7765 dprintf("\tError: %i\n", ret_val
);
7770 /* Function: get_spares_for_grow
7771 * Description: Allocates memory and creates list of spare devices
7772 * avaliable in container. Checks if spare drive size is acceptable.
7773 * Parameters: Pointer to the supertype structure
7774 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
7777 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
7779 unsigned long long min_size
= min_acceptable_spare_size_imsm(st
);
7780 return container_choose_spares(st
, min_size
, NULL
, NULL
, NULL
, 0);
7783 /******************************************************************************
7784 * function: imsm_create_metadata_update_for_reshape
7785 * Function creates update for whole IMSM container.
7787 ******************************************************************************/
7788 static int imsm_create_metadata_update_for_reshape(
7789 struct supertype
*st
,
7790 struct geo_params
*geo
,
7792 struct imsm_update_reshape
**updatep
)
7794 struct intel_super
*super
= st
->sb
;
7795 struct imsm_super
*mpb
= super
->anchor
;
7796 int update_memory_size
= 0;
7797 struct imsm_update_reshape
*u
= NULL
;
7798 struct mdinfo
*spares
= NULL
;
7800 int delta_disks
= 0;
7803 dprintf("imsm_update_metadata_for_reshape(enter) raid_disks = %i\n",
7806 delta_disks
= geo
->raid_disks
- old_raid_disks
;
7808 /* size of all update data without anchor */
7809 update_memory_size
= sizeof(struct imsm_update_reshape
);
7811 /* now add space for spare disks that we need to add. */
7812 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
7814 u
= calloc(1, update_memory_size
);
7817 "cannot get memory for imsm_update_reshape update\n");
7820 u
->type
= update_reshape_container_disks
;
7821 u
->old_raid_disks
= old_raid_disks
;
7822 u
->new_raid_disks
= geo
->raid_disks
;
7824 /* now get spare disks list
7826 spares
= get_spares_for_grow(st
);
7829 || delta_disks
> spares
->array
.spare_disks
) {
7830 fprintf(stderr
, Name
": imsm: ERROR: Cannot get spare devices "
7831 "for %s.\n", geo
->dev_name
);
7835 /* we have got spares
7836 * update disk list in imsm_disk list table in anchor
7838 dprintf("imsm: %i spares are available.\n\n",
7839 spares
->array
.spare_disks
);
7842 for (i
= 0; i
< delta_disks
; i
++) {
7847 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
7849 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
7850 dl
->index
= mpb
->num_disks
;
7860 dprintf("imsm: reshape update preparation :");
7861 if (i
== delta_disks
) {
7864 return update_memory_size
;
7867 dprintf(" Error\n");
7872 /******************************************************************************
7873 * function: imsm_create_metadata_update_for_migration()
7874 * Creates update for IMSM array.
7876 ******************************************************************************/
7877 static int imsm_create_metadata_update_for_migration(
7878 struct supertype
*st
,
7879 struct geo_params
*geo
,
7880 struct imsm_update_reshape_migration
**updatep
)
7882 struct intel_super
*super
= st
->sb
;
7883 int update_memory_size
= 0;
7884 struct imsm_update_reshape_migration
*u
= NULL
;
7885 struct imsm_dev
*dev
;
7886 int previous_level
= -1;
7888 dprintf("imsm_create_metadata_update_for_migration(enter)"
7889 " New Level = %i\n", geo
->level
);
7891 /* size of all update data without anchor */
7892 update_memory_size
= sizeof(struct imsm_update_reshape_migration
);
7894 u
= calloc(1, update_memory_size
);
7896 dprintf("error: cannot get memory for "
7897 "imsm_create_metadata_update_for_migration\n");
7900 u
->type
= update_reshape_migration
;
7901 u
->subdev
= super
->current_vol
;
7902 u
->new_level
= geo
->level
;
7903 u
->new_layout
= geo
->layout
;
7904 u
->new_raid_disks
= u
->old_raid_disks
= geo
->raid_disks
;
7905 u
->new_disks
[0] = -1;
7906 u
->new_chunksize
= -1;
7908 dev
= get_imsm_dev(super
, u
->subdev
);
7910 struct imsm_map
*map
;
7912 map
= get_imsm_map(dev
, 0);
7914 int current_chunk_size
=
7915 __le16_to_cpu(map
->blocks_per_strip
) / 2;
7917 if (geo
->chunksize
!= current_chunk_size
) {
7918 u
->new_chunksize
= geo
->chunksize
/ 1024;
7920 "chunk size change from %i to %i\n",
7921 current_chunk_size
, u
->new_chunksize
);
7923 previous_level
= map
->raid_level
;
7926 if ((geo
->level
== 5) && (previous_level
== 0)) {
7927 struct mdinfo
*spares
= NULL
;
7929 u
->new_raid_disks
++;
7930 spares
= get_spares_for_grow(st
);
7931 if ((spares
== NULL
) || (spares
->array
.spare_disks
< 1)) {
7934 update_memory_size
= 0;
7935 dprintf("error: cannot get spare device "
7936 "for requested migration");
7941 dprintf("imsm: reshape update preparation : OK\n");
7944 return update_memory_size
;
7947 static void imsm_update_metadata_locally(struct supertype
*st
,
7950 struct metadata_update mu
;
7955 mu
.space_list
= NULL
;
7957 imsm_prepare_update(st
, &mu
);
7958 imsm_process_update(st
, &mu
);
7960 while (mu
.space_list
) {
7961 void **space
= mu
.space_list
;
7962 mu
.space_list
= *space
;
7967 /***************************************************************************
7968 * Function: imsm_analyze_change
7969 * Description: Function analyze change for single volume
7970 * and validate if transition is supported
7971 * Parameters: Geometry parameters, supertype structure
7972 * Returns: Operation type code on success, -1 if fail
7973 ****************************************************************************/
7974 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
7975 struct geo_params
*geo
)
7982 getinfo_super_imsm_volume(st
, &info
, NULL
);
7984 if ((geo
->level
!= info
.array
.level
) &&
7985 (geo
->level
>= 0) &&
7986 (geo
->level
!= UnSet
)) {
7987 switch (info
.array
.level
) {
7989 if (geo
->level
== 5) {
7990 change
= CH_MIGRATION
;
7993 if (geo
->level
== 10) {
7994 change
= CH_TAKEOVER
;
7999 if (geo
->level
== 0) {
8000 change
= CH_TAKEOVER
;
8005 if (geo
->level
== 0) {
8006 change
= CH_TAKEOVER
;
8013 Name
" Error. Level Migration from %d to %d "
8015 info
.array
.level
, geo
->level
);
8016 goto analyse_change_exit
;
8019 geo
->level
= info
.array
.level
;
8021 if ((geo
->layout
!= info
.array
.layout
)
8022 && ((geo
->layout
!= UnSet
) && (geo
->layout
!= -1))) {
8023 change
= CH_MIGRATION
;
8024 if ((info
.array
.layout
== 0)
8025 && (info
.array
.level
== 5)
8026 && (geo
->layout
== 5)) {
8027 /* reshape 5 -> 4 */
8028 } else if ((info
.array
.layout
== 5)
8029 && (info
.array
.level
== 5)
8030 && (geo
->layout
== 0)) {
8031 /* reshape 4 -> 5 */
8036 Name
" Error. Layout Migration from %d to %d "
8038 info
.array
.layout
, geo
->layout
);
8040 goto analyse_change_exit
;
8043 geo
->layout
= info
.array
.layout
;
8045 if ((geo
->chunksize
> 0) && (geo
->chunksize
!= UnSet
)
8046 && (geo
->chunksize
!= info
.array
.chunk_size
))
8047 change
= CH_MIGRATION
;
8049 geo
->chunksize
= info
.array
.chunk_size
;
8051 chunk
= geo
->chunksize
/ 1024;
8052 if (!validate_geometry_imsm(st
,
8062 struct intel_super
*super
= st
->sb
;
8063 struct imsm_super
*mpb
= super
->anchor
;
8065 if (mpb
->num_raid_devs
> 1) {
8067 Name
" Error. Cannot perform operation on %s"
8068 "- for this operation it MUST be single "
8069 "array in container\n",
8075 analyse_change_exit
:
8080 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
8082 struct intel_super
*super
= st
->sb
;
8083 struct imsm_update_takeover
*u
;
8085 u
= malloc(sizeof(struct imsm_update_takeover
));
8089 u
->type
= update_takeover
;
8090 u
->subarray
= super
->current_vol
;
8092 /* 10->0 transition */
8093 if (geo
->level
== 0)
8094 u
->direction
= R10_TO_R0
;
8096 /* 0->10 transition */
8097 if (geo
->level
== 10)
8098 u
->direction
= R0_TO_R10
;
8100 /* update metadata locally */
8101 imsm_update_metadata_locally(st
, u
,
8102 sizeof(struct imsm_update_takeover
));
8103 /* and possibly remotely */
8104 if (st
->update_tail
)
8105 append_metadata_update(st
, u
,
8106 sizeof(struct imsm_update_takeover
));
8113 static int warn_user_about_risk(void)
8118 "\nThis is an experimental feature. Data on the RAID volume(s) "
8119 "can be lost!!!\n\n"
8120 "To continue command execution please make sure that\n"
8121 "the grow process will not be interrupted. Use safe power\n"
8122 "supply to avoid unexpected system reboot. Make sure that\n"
8123 "reshaped container is not assembled automatically during\n"
8125 "If reshape is interrupted, assemble array manually\n"
8126 "using e.g. '-Ac' option and up to date mdadm.conf file.\n"
8127 "Assembly in scan mode is not possible in such case.\n"
8128 "Growing container with boot array is not possible.\n"
8129 "If boot array reshape is interrupted, whole file system\n"
8130 "can be lost.\n\n");
8131 rv
= ask("Do you want to continue? ");
8132 fprintf(stderr
, "\n");
8137 static int imsm_reshape_super(struct supertype
*st
, long long size
, int level
,
8138 int layout
, int chunksize
, int raid_disks
,
8139 int delta_disks
, char *backup
, char *dev
,
8143 struct geo_params geo
;
8145 dprintf("imsm: reshape_super called.\n");
8147 memset(&geo
, 0, sizeof(struct geo_params
));
8150 geo
.dev_id
= st
->devnum
;
8153 geo
.layout
= layout
;
8154 geo
.chunksize
= chunksize
;
8155 geo
.raid_disks
= raid_disks
;
8156 if (delta_disks
!= UnSet
)
8157 geo
.raid_disks
+= delta_disks
;
8159 dprintf("\tfor level : %i\n", geo
.level
);
8160 dprintf("\tfor raid_disks : %i\n", geo
.raid_disks
);
8162 if (experimental() == 0)
8165 if (st
->container_dev
== st
->devnum
) {
8166 /* On container level we can only increase number of devices. */
8167 dprintf("imsm: info: Container operation\n");
8168 int old_raid_disks
= 0;
8170 /* this warning will be removed when imsm checkpointing
8171 * will be implemented, and restoring from check-point
8172 * operation will be transparent for reboot process
8174 if (warn_user_about_risk() == 0)
8177 if (imsm_reshape_is_allowed_on_container(
8178 st
, &geo
, &old_raid_disks
)) {
8179 struct imsm_update_reshape
*u
= NULL
;
8182 len
= imsm_create_metadata_update_for_reshape(
8183 st
, &geo
, old_raid_disks
, &u
);
8186 dprintf("imsm: Cannot prepare update\n");
8187 goto exit_imsm_reshape_super
;
8191 /* update metadata locally */
8192 imsm_update_metadata_locally(st
, u
, len
);
8193 /* and possibly remotely */
8194 if (st
->update_tail
)
8195 append_metadata_update(st
, u
, len
);
8200 fprintf(stderr
, Name
": (imsm) Operation "
8201 "is not allowed on this container\n");
8204 /* On volume level we support following operations
8205 * - takeover: raid10 -> raid0; raid0 -> raid10
8206 * - chunk size migration
8207 * - migration: raid5 -> raid0; raid0 -> raid5
8209 struct intel_super
*super
= st
->sb
;
8210 struct intel_dev
*dev
= super
->devlist
;
8212 dprintf("imsm: info: Volume operation\n");
8213 /* find requested device */
8215 imsm_find_array_minor_by_subdev(dev
->index
, st
->container_dev
, &devnum
);
8216 if (devnum
== geo
.dev_id
)
8221 fprintf(stderr
, Name
" Cannot find %s (%i) subarray\n",
8222 geo
.dev_name
, geo
.dev_id
);
8223 goto exit_imsm_reshape_super
;
8225 super
->current_vol
= dev
->index
;
8226 change
= imsm_analyze_change(st
, &geo
);
8229 ret_val
= imsm_takeover(st
, &geo
);
8231 case CH_MIGRATION
: {
8232 struct imsm_update_reshape_migration
*u
= NULL
;
8234 imsm_create_metadata_update_for_migration(
8238 "Cannot prepare update\n");
8242 /* update metadata locally */
8243 imsm_update_metadata_locally(st
, u
, len
);
8244 /* and possibly remotely */
8245 if (st
->update_tail
)
8246 append_metadata_update(st
, u
, len
);
8256 exit_imsm_reshape_super
:
8257 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
8261 /*******************************************************************************
8262 * Function: wait_for_reshape_imsm
8263 * Description: Function writes new sync_max value and waits until
8264 * reshape process reach new position
8266 * sra : general array info
8267 * to_complete : new sync_max position
8268 * ndata : number of disks in new array's layout
8271 * 1 : there is no reshape in progress,
8273 ******************************************************************************/
8274 int wait_for_reshape_imsm(struct mdinfo
*sra
, unsigned long long to_complete
,
8277 int fd
= sysfs_get_fd(sra
, NULL
, "reshape_position");
8278 unsigned long long completed
;
8280 struct timeval timeout
;
8285 sysfs_fd_get_ll(fd
, &completed
);
8287 if (to_complete
== 0) {/* reshape till the end of array */
8288 sysfs_set_str(sra
, NULL
, "sync_max", "max");
8289 to_complete
= MaxSector
;
8291 if (completed
> to_complete
)
8293 if (sysfs_set_num(sra
, NULL
, "sync_max",
8294 to_complete
/ ndata
) != 0) {
8300 /* FIXME should not need a timeout at all */
8301 timeout
.tv_sec
= 30;
8302 timeout
.tv_usec
= 0;
8308 select(fd
+1, NULL
, NULL
, &rfds
, &timeout
);
8309 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
8313 if (sysfs_get_str(sra
, NULL
, "sync_action",
8315 strncmp(action
, "reshape", 7) != 0)
8317 } while (completed
< to_complete
);
8323 /*******************************************************************************
8324 * Function: check_degradation_change
8325 * Description: Check that array hasn't become failed.
8327 * info : for sysfs access
8328 * sources : source disks descriptors
8329 * degraded: previous degradation level
8332 ******************************************************************************/
8333 int check_degradation_change(struct mdinfo
*info
,
8337 unsigned long long new_degraded
;
8338 sysfs_get_ll(info
, NULL
, "degraded", &new_degraded
);
8339 if (new_degraded
!= (unsigned long long)degraded
) {
8340 /* check each device to ensure it is still working */
8343 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
8344 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
8346 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
8348 if (sysfs_get_str(info
,
8349 sd
, "state", sbuf
, 20) < 0 ||
8350 strstr(sbuf
, "faulty") ||
8351 strstr(sbuf
, "in_sync") == NULL
) {
8352 /* this device is dead */
8353 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
8354 if (sd
->disk
.raid_disk
>= 0 &&
8355 sources
[sd
->disk
.raid_disk
] >= 0) {
8357 sd
->disk
.raid_disk
]);
8358 sources
[sd
->disk
.raid_disk
] =
8367 return new_degraded
;
8370 /*******************************************************************************
8371 * Function: imsm_manage_reshape
8372 * Description: Function finds array under reshape and it manages reshape
8373 * process. It creates stripes backups (if required) and sets
8376 * afd : Backup handle (nattive) - not used
8377 * sra : general array info
8378 * reshape : reshape parameters - not used
8379 * st : supertype structure
8380 * blocks : size of critical section [blocks]
8381 * fds : table of source device descriptor
8382 * offsets : start of array (offest per devices)
8384 * destfd : table of destination device descriptor
8385 * destoffsets : table of destination offsets (per device)
8387 * 1 : success, reshape is done
8389 ******************************************************************************/
8390 static int imsm_manage_reshape(
8391 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
8392 struct supertype
*st
, unsigned long backup_blocks
,
8393 int *fds
, unsigned long long *offsets
,
8394 int dests
, int *destfd
, unsigned long long *destoffsets
)
8397 struct intel_super
*super
= st
->sb
;
8398 struct intel_dev
*dv
= NULL
;
8399 struct imsm_dev
*dev
= NULL
;
8400 struct imsm_map
*map_src
, *map_dest
;
8401 int migr_vol_qan
= 0;
8402 int ndata
, odata
; /* [bytes] */
8403 int chunk
; /* [bytes] */
8404 struct migr_record
*migr_rec
;
8406 unsigned int buf_size
; /* [bytes] */
8407 unsigned long long max_position
; /* array size [bytes] */
8408 unsigned long long next_step
; /* [blocks]/[bytes] */
8409 unsigned long long old_data_stripe_length
;
8410 unsigned long long new_data_stripe_length
;
8411 unsigned long long start_src
; /* [bytes] */
8412 unsigned long long start
; /* [bytes] */
8413 unsigned long long start_buf_shift
; /* [bytes] */
8416 if (!fds
|| !offsets
|| !destfd
|| !destoffsets
|| !sra
)
8419 /* Find volume during the reshape */
8420 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
8421 if (dv
->dev
->vol
.migr_type
== MIGR_GEN_MIGR
8422 && dv
->dev
->vol
.migr_state
== 1) {
8427 /* Only one volume can migrate at the same time */
8428 if (migr_vol_qan
!= 1) {
8429 fprintf(stderr
, Name
" : %s", migr_vol_qan
?
8430 "Number of migrating volumes greater than 1\n" :
8431 "There is no volume during migrationg\n");
8435 map_src
= get_imsm_map(dev
, 1);
8436 if (map_src
== NULL
)
8438 map_dest
= get_imsm_map(dev
, 0);
8440 ndata
= imsm_num_data_members(dev
, 0);
8441 odata
= imsm_num_data_members(dev
, 1);
8443 chunk
= map_src
->blocks_per_strip
* 512;
8444 old_data_stripe_length
= odata
* chunk
;
8446 migr_rec
= super
->migr_rec
;
8449 sra
->new_chunk
= __le16_to_cpu(map_dest
->blocks_per_strip
) * 512;
8450 sra
->new_level
= map_dest
->raid_level
;
8451 new_data_stripe_length
= sra
->new_chunk
* ndata
;
8453 /* initialize migration record for start condition */
8454 if (sra
->reshape_progress
== 0)
8455 init_migr_record_imsm(st
, dev
, sra
);
8458 buf_size
= __le32_to_cpu(migr_rec
->blocks_per_unit
) * 512;
8459 /* extend buffer size for parity disk */
8460 buf_size
+= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
8461 /* add space for stripe aligment */
8462 buf_size
+= old_data_stripe_length
;
8463 if (posix_memalign((void **)&buf
, 4096, buf_size
)) {
8464 dprintf("imsm: Cannot allocate checpoint buffer\n");
8469 __le32_to_cpu(migr_rec
->post_migr_vol_cap
) +
8470 ((unsigned long long)__le32_to_cpu(
8471 migr_rec
->post_migr_vol_cap_hi
) << 32);
8473 while (__le32_to_cpu(migr_rec
->curr_migr_unit
) <
8474 __le32_to_cpu(migr_rec
->num_migr_units
)) {
8475 /* current reshape position [blocks] */
8476 unsigned long long current_position
=
8477 __le32_to_cpu(migr_rec
->blocks_per_unit
)
8478 * __le32_to_cpu(migr_rec
->curr_migr_unit
);
8479 unsigned long long border
;
8481 /* Check that array hasn't become failed.
8483 degraded
= check_degradation_change(sra
, fds
, degraded
);
8485 dprintf("imsm: Abort reshape due to degradation"
8486 " level (%i)\n", degraded
);
8490 next_step
= __le32_to_cpu(migr_rec
->blocks_per_unit
);
8492 if ((current_position
+ next_step
) > max_position
)
8493 next_step
= max_position
- current_position
;
8495 start
= (map_src
->pba_of_lba0
+ dev
->reserved_blocks
+
8496 current_position
) * 512;
8498 /* allign reading start to old geometry */
8499 start_buf_shift
= start
% old_data_stripe_length
;
8500 start_src
= start
- start_buf_shift
;
8502 border
= (start_src
/ odata
) - (start
/ ndata
);
8504 if (border
<= __le32_to_cpu(migr_rec
->dest_depth_per_unit
)) {
8505 /* save critical stripes to buf
8506 * start - start address of current unit
8508 * start_src - start address of current unit
8509 * to backup alligned to source array
8512 unsigned long long next_step_filler
= 0;
8513 unsigned long long copy_length
= next_step
* 512;
8515 /* allign copy area length to stripe in old geometry */
8516 next_step_filler
= ((copy_length
+ start_buf_shift
)
8517 % old_data_stripe_length
);
8518 if (next_step_filler
)
8519 next_step_filler
= (old_data_stripe_length
8520 - next_step_filler
);
8521 dprintf("save_stripes() parameters: start = %llu,"
8522 "\tstart_src = %llu,\tnext_step*512 = %llu,"
8523 "\tstart_in_buf_shift = %llu,"
8524 "\tnext_step_filler = %llu\n",
8525 start
, start_src
, copy_length
,
8526 start_buf_shift
, next_step_filler
);
8528 if (save_stripes(fds
, offsets
, map_src
->num_members
,
8529 chunk
, sra
->array
.level
,
8530 sra
->array
.layout
, 0, NULL
, start_src
,
8532 next_step_filler
+ start_buf_shift
,
8534 dprintf("imsm: Cannot save stripes"
8538 /* Convert data to destination format and store it
8539 * in backup general migration area
8541 if (save_backup_imsm(st
, dev
, sra
,
8542 buf
+ start_buf_shift
,
8543 ndata
, copy_length
)) {
8544 dprintf("imsm: Cannot save stripes to "
8545 "target devices\n");
8548 if (save_checkpoint_imsm(st
, sra
,
8549 UNIT_SRC_IN_CP_AREA
)) {
8550 dprintf("imsm: Cannot write checkpoint to "
8551 "migration record (UNIT_SRC_IN_CP_AREA)\n");
8554 /* decrease backup_blocks */
8555 if (backup_blocks
> (unsigned long)next_step
)
8556 backup_blocks
-= next_step
;
8560 /* When data backed up, checkpoint stored,
8561 * kick the kernel to reshape unit of data
8563 next_step
= next_step
+ sra
->reshape_progress
;
8564 sysfs_set_num(sra
, NULL
, "suspend_lo", sra
->reshape_progress
);
8565 sysfs_set_num(sra
, NULL
, "suspend_hi", next_step
);
8567 /* wait until reshape finish */
8568 if (wait_for_reshape_imsm(sra
, next_step
, ndata
) < 0)
8569 dprintf("wait_for_reshape_imsm returned error,"
8570 " but we ignore it!\n");
8572 sra
->reshape_progress
= next_step
;
8574 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
)) {
8575 dprintf("imsm: Cannot write checkpoint to "
8576 "migration record (UNIT_SRC_NORMAL)\n");
8582 /* return '1' if done */
8590 #endif /* MDASSEMBLE */
8592 struct superswitch super_imsm
= {
8594 .examine_super
= examine_super_imsm
,
8595 .brief_examine_super
= brief_examine_super_imsm
,
8596 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
8597 .export_examine_super
= export_examine_super_imsm
,
8598 .detail_super
= detail_super_imsm
,
8599 .brief_detail_super
= brief_detail_super_imsm
,
8600 .write_init_super
= write_init_super_imsm
,
8601 .validate_geometry
= validate_geometry_imsm
,
8602 .add_to_super
= add_to_super_imsm
,
8603 .remove_from_super
= remove_from_super_imsm
,
8604 .detail_platform
= detail_platform_imsm
,
8605 .kill_subarray
= kill_subarray_imsm
,
8606 .update_subarray
= update_subarray_imsm
,
8607 .load_container
= load_container_imsm
,
8608 .default_geometry
= default_geometry_imsm
,
8609 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
8610 .reshape_super
= imsm_reshape_super
,
8611 .manage_reshape
= imsm_manage_reshape
,
8613 .match_home
= match_home_imsm
,
8614 .uuid_from_super
= uuid_from_super_imsm
,
8615 .getinfo_super
= getinfo_super_imsm
,
8616 .getinfo_super_disks
= getinfo_super_disks_imsm
,
8617 .update_super
= update_super_imsm
,
8619 .avail_size
= avail_size_imsm
,
8620 .min_acceptable_spare_size
= min_acceptable_spare_size_imsm
,
8622 .compare_super
= compare_super_imsm
,
8624 .load_super
= load_super_imsm
,
8625 .init_super
= init_super_imsm
,
8626 .store_super
= store_super_imsm
,
8627 .free_super
= free_super_imsm
,
8628 .match_metadata_desc
= match_metadata_desc_imsm
,
8629 .container_content
= container_content_imsm
,
8636 .open_new
= imsm_open_new
,
8637 .set_array_state
= imsm_set_array_state
,
8638 .set_disk
= imsm_set_disk
,
8639 .sync_metadata
= imsm_sync_metadata
,
8640 .activate_spare
= imsm_activate_spare
,
8641 .process_update
= imsm_process_update
,
8642 .prepare_update
= imsm_prepare_update
,
8643 #endif /* MDASSEMBLE */