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
45 #define MPB_ATTRIB_RAID0 __cpu_to_le32(0x00000001)
47 #define MPB_ATTRIB_RAID1 __cpu_to_le32(0x00000002)
49 #define MPB_ATTRIB_RAID10 __cpu_to_le32(0x00000004)
51 #define MPB_ATTRIB_RAID1E __cpu_to_le32(0x00000008)
53 #define MPB_ATTRIB_RAID5 __cpu_to_le32(0x00000010)
54 /* supports RAID CNG */
55 #define MPB_ATTRIB_RAIDCNG __cpu_to_le32(0x00000020)
56 /* supports expanded stripe sizes of 256K, 512K and 1MB */
57 #define MPB_ATTRIB_EXP_STRIPE_SIZE __cpu_to_le32(0x00000040)
59 /* The OROM Support RST Caching of Volumes */
60 #define MPB_ATTRIB_NVM __cpu_to_le32(0x02000000)
61 /* The OROM supports creating disks greater than 2TB */
62 #define MPB_ATTRIB_2TB_DISK __cpu_to_le32(0x04000000)
63 /* The OROM supports Bad Block Management */
64 #define MPB_ATTRIB_BBM __cpu_to_le32(0x08000000)
66 /* THe OROM Supports NVM Caching of Volumes */
67 #define MPB_ATTRIB_NEVER_USE2 __cpu_to_le32(0x10000000)
68 /* The OROM supports creating volumes greater than 2TB */
69 #define MPB_ATTRIB_2TB __cpu_to_le32(0x20000000)
70 /* originally for PMP, now it's wasted b/c. Never use this bit! */
71 #define MPB_ATTRIB_NEVER_USE __cpu_to_le32(0x40000000)
72 /* Verify MPB contents against checksum after reading MPB */
73 #define MPB_ATTRIB_CHECKSUM_VERIFY __cpu_to_le32(0x80000000)
75 /* Define all supported attributes that have to be accepted by mdadm
77 #define MPB_ATTRIB_SUPPORTED (MPB_ATTRIB_CHECKSUM_VERIFY | \
79 MPB_ATTRIB_2TB_DISK | \
84 MPB_ATTRIB_EXP_STRIPE_SIZE)
86 /* Define attributes that are unused but not harmful */
87 #define MPB_ATTRIB_IGNORED (MPB_ATTRIB_NEVER_USE)
89 #define MPB_SECTOR_CNT 2210
90 #define IMSM_RESERVED_SECTORS 4096
91 #define NUM_BLOCKS_DIRTY_STRIPE_REGION 2056
92 #define SECT_PER_MB_SHIFT 11
94 /* Disk configuration info. */
95 #define IMSM_MAX_DEVICES 255
97 __u8 serial
[MAX_RAID_SERIAL_LEN
];/* 0xD8 - 0xE7 ascii serial number */
98 __u32 total_blocks_lo
; /* 0xE8 - 0xEB total blocks lo */
99 __u32 scsi_id
; /* 0xEC - 0xEF scsi ID */
100 #define SPARE_DISK __cpu_to_le32(0x01) /* Spare */
101 #define CONFIGURED_DISK __cpu_to_le32(0x02) /* Member of some RaidDev */
102 #define FAILED_DISK __cpu_to_le32(0x04) /* Permanent failure */
103 __u32 status
; /* 0xF0 - 0xF3 */
104 __u32 owner_cfg_num
; /* which config 0,1,2... owns this disk */
105 __u32 total_blocks_hi
; /* 0xF4 - 0xF5 total blocks hi */
106 #define IMSM_DISK_FILLERS 3
107 __u32 filler
[IMSM_DISK_FILLERS
]; /* 0xF5 - 0x107 MPB_DISK_FILLERS for future expansion */
110 /* map selector for map managment
116 /* RAID map configuration infos. */
118 __u32 pba_of_lba0_lo
; /* start address of partition */
119 __u32 blocks_per_member_lo
;/* blocks per member */
120 __u32 num_data_stripes_lo
; /* number of data stripes */
121 __u16 blocks_per_strip
;
122 __u8 map_state
; /* Normal, Uninitialized, Degraded, Failed */
123 #define IMSM_T_STATE_NORMAL 0
124 #define IMSM_T_STATE_UNINITIALIZED 1
125 #define IMSM_T_STATE_DEGRADED 2
126 #define IMSM_T_STATE_FAILED 3
128 #define IMSM_T_RAID0 0
129 #define IMSM_T_RAID1 1
130 #define IMSM_T_RAID5 5 /* since metadata version 1.2.02 ? */
131 __u8 num_members
; /* number of member disks */
132 __u8 num_domains
; /* number of parity domains */
133 __u8 failed_disk_num
; /* valid only when state is degraded */
135 __u32 pba_of_lba0_hi
;
136 __u32 blocks_per_member_hi
;
137 __u32 num_data_stripes_hi
;
138 __u32 filler
[4]; /* expansion area */
139 #define IMSM_ORD_REBUILD (1 << 24)
140 __u32 disk_ord_tbl
[1]; /* disk_ord_tbl[num_members],
141 * top byte contains some flags
143 } __attribute__ ((packed
));
146 __u32 curr_migr_unit
;
147 __u32 checkpoint_id
; /* id to access curr_migr_unit */
148 __u8 migr_state
; /* Normal or Migrating */
150 #define MIGR_REBUILD 1
151 #define MIGR_VERIFY 2 /* analagous to echo check > sync_action */
152 #define MIGR_GEN_MIGR 3
153 #define MIGR_STATE_CHANGE 4
154 #define MIGR_REPAIR 5
155 __u8 migr_type
; /* Initializing, Rebuilding, ... */
157 __u8 fs_state
; /* fast-sync state for CnG (0xff == disabled) */
158 __u16 verify_errors
; /* number of mismatches */
159 __u16 bad_blocks
; /* number of bad blocks during verify */
161 struct imsm_map map
[1];
162 /* here comes another one if migr_state */
163 } __attribute__ ((packed
));
166 __u8 volume
[MAX_RAID_SERIAL_LEN
];
169 #define DEV_BOOTABLE __cpu_to_le32(0x01)
170 #define DEV_BOOT_DEVICE __cpu_to_le32(0x02)
171 #define DEV_READ_COALESCING __cpu_to_le32(0x04)
172 #define DEV_WRITE_COALESCING __cpu_to_le32(0x08)
173 #define DEV_LAST_SHUTDOWN_DIRTY __cpu_to_le32(0x10)
174 #define DEV_HIDDEN_AT_BOOT __cpu_to_le32(0x20)
175 #define DEV_CURRENTLY_HIDDEN __cpu_to_le32(0x40)
176 #define DEV_VERIFY_AND_FIX __cpu_to_le32(0x80)
177 #define DEV_MAP_STATE_UNINIT __cpu_to_le32(0x100)
178 #define DEV_NO_AUTO_RECOVERY __cpu_to_le32(0x200)
179 #define DEV_CLONE_N_GO __cpu_to_le32(0x400)
180 #define DEV_CLONE_MAN_SYNC __cpu_to_le32(0x800)
181 #define DEV_CNG_MASTER_DISK_NUM __cpu_to_le32(0x1000)
182 __u32 status
; /* Persistent RaidDev status */
183 __u32 reserved_blocks
; /* Reserved blocks at beginning of volume */
187 __u8 cng_master_disk
;
191 #define IMSM_DEV_FILLERS 10
192 __u32 filler
[IMSM_DEV_FILLERS
];
194 } __attribute__ ((packed
));
197 __u8 sig
[MAX_SIGNATURE_LENGTH
]; /* 0x00 - 0x1F */
198 __u32 check_sum
; /* 0x20 - 0x23 MPB Checksum */
199 __u32 mpb_size
; /* 0x24 - 0x27 Size of MPB */
200 __u32 family_num
; /* 0x28 - 0x2B Checksum from first time this config was written */
201 __u32 generation_num
; /* 0x2C - 0x2F Incremented each time this array's MPB is written */
202 __u32 error_log_size
; /* 0x30 - 0x33 in bytes */
203 __u32 attributes
; /* 0x34 - 0x37 */
204 __u8 num_disks
; /* 0x38 Number of configured disks */
205 __u8 num_raid_devs
; /* 0x39 Number of configured volumes */
206 __u8 error_log_pos
; /* 0x3A */
207 __u8 fill
[1]; /* 0x3B */
208 __u32 cache_size
; /* 0x3c - 0x40 in mb */
209 __u32 orig_family_num
; /* 0x40 - 0x43 original family num */
210 __u32 pwr_cycle_count
; /* 0x44 - 0x47 simulated power cycle count for array */
211 __u32 bbm_log_size
; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */
212 #define IMSM_FILLERS 35
213 __u32 filler
[IMSM_FILLERS
]; /* 0x4C - 0xD7 RAID_MPB_FILLERS */
214 struct imsm_disk disk
[1]; /* 0xD8 diskTbl[numDisks] */
215 /* here comes imsm_dev[num_raid_devs] */
216 /* here comes BBM logs */
217 } __attribute__ ((packed
));
219 #define BBM_LOG_MAX_ENTRIES 254
221 struct bbm_log_entry
{
222 __u64 defective_block_start
;
223 #define UNREADABLE 0xFFFFFFFF
224 __u32 spare_block_offset
;
225 __u16 remapped_marked_count
;
227 } __attribute__ ((__packed__
));
230 __u32 signature
; /* 0xABADB10C */
232 __u32 reserved_spare_block_count
; /* 0 */
233 __u32 reserved
; /* 0xFFFF */
234 __u64 first_spare_lba
;
235 struct bbm_log_entry mapped_block_entries
[BBM_LOG_MAX_ENTRIES
];
236 } __attribute__ ((__packed__
));
240 static char *map_state_str
[] = { "normal", "uninitialized", "degraded", "failed" };
243 #define RAID_DISK_RESERVED_BLOCKS_IMSM_HI 2209
245 #define GEN_MIGR_AREA_SIZE 2048 /* General Migration Copy Area size in blocks */
247 #define MIGR_REC_BUF_SIZE 512 /* size of migr_record i/o buffer */
248 #define MIGR_REC_POSITION 512 /* migr_record position offset on disk,
249 * MIGR_REC_BUF_SIZE <= MIGR_REC_POSITION
253 #define UNIT_SRC_NORMAL 0 /* Source data for curr_migr_unit must
254 * be recovered using srcMap */
255 #define UNIT_SRC_IN_CP_AREA 1 /* Source data for curr_migr_unit has
256 * already been migrated and must
257 * be recovered from checkpoint area */
259 __u32 rec_status
; /* Status used to determine how to restart
260 * migration in case it aborts
262 __u32 curr_migr_unit
; /* 0..numMigrUnits-1 */
263 __u32 family_num
; /* Family number of MPB
264 * containing the RaidDev
265 * that is migrating */
266 __u32 ascending_migr
; /* True if migrating in increasing
268 __u32 blocks_per_unit
; /* Num disk blocks per unit of operation */
269 __u32 dest_depth_per_unit
; /* Num member blocks each destMap
271 * advances per unit-of-operation */
272 __u32 ckpt_area_pba
; /* Pba of first block of ckpt copy area */
273 __u32 dest_1st_member_lba
; /* First member lba on first
274 * stripe of destination */
275 __u32 num_migr_units
; /* Total num migration units-of-op */
276 __u32 post_migr_vol_cap
; /* Size of volume after
277 * migration completes */
278 __u32 post_migr_vol_cap_hi
; /* Expansion space for LBA64 */
279 __u32 ckpt_read_disk_num
; /* Which member disk in destSubMap[0] the
280 * migration ckpt record was read from
281 * (for recovered migrations) */
282 } __attribute__ ((__packed__
));
287 * 2: metadata does not match
295 struct md_list
*next
;
298 #define pr_vrb(fmt, arg...) (void) (verbose && fprintf(stderr, Name fmt, ##arg))
300 static __u8
migr_type(struct imsm_dev
*dev
)
302 if (dev
->vol
.migr_type
== MIGR_VERIFY
&&
303 dev
->status
& DEV_VERIFY_AND_FIX
)
306 return dev
->vol
.migr_type
;
309 static void set_migr_type(struct imsm_dev
*dev
, __u8 migr_type
)
311 /* for compatibility with older oroms convert MIGR_REPAIR, into
312 * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
314 if (migr_type
== MIGR_REPAIR
) {
315 dev
->vol
.migr_type
= MIGR_VERIFY
;
316 dev
->status
|= DEV_VERIFY_AND_FIX
;
318 dev
->vol
.migr_type
= migr_type
;
319 dev
->status
&= ~DEV_VERIFY_AND_FIX
;
323 static unsigned int sector_count(__u32 bytes
)
325 return ROUND_UP(bytes
, 512) / 512;
328 static unsigned int mpb_sectors(struct imsm_super
*mpb
)
330 return sector_count(__le32_to_cpu(mpb
->mpb_size
));
334 struct imsm_dev
*dev
;
335 struct intel_dev
*next
;
340 enum sys_dev_type type
;
343 struct intel_hba
*next
;
350 /* internal representation of IMSM metadata */
353 void *buf
; /* O_DIRECT buffer for reading/writing metadata */
354 struct imsm_super
*anchor
; /* immovable parameters */
357 void *migr_rec_buf
; /* buffer for I/O operations */
358 struct migr_record
*migr_rec
; /* migration record */
360 int clean_migration_record_by_mdmon
; /* when reshape is switched to next
361 array, it indicates that mdmon is allowed to clean migration
363 size_t len
; /* size of the 'buf' allocation */
364 void *next_buf
; /* for realloc'ing buf from the manager */
366 int updates_pending
; /* count of pending updates for mdmon */
367 int current_vol
; /* index of raid device undergoing creation */
368 unsigned long long create_offset
; /* common start for 'current_vol' */
369 __u32 random
; /* random data for seeding new family numbers */
370 struct intel_dev
*devlist
;
374 __u8 serial
[MAX_RAID_SERIAL_LEN
];
377 struct imsm_disk disk
;
380 struct extent
*e
; /* for determining freespace @ create */
381 int raiddisk
; /* slot to fill in autolayout */
383 } *disks
, *current_disk
;
384 struct dl
*disk_mgmt_list
; /* list of disks to add/remove while mdmon
386 struct dl
*missing
; /* disks removed while we weren't looking */
387 struct bbm_log
*bbm_log
;
388 struct intel_hba
*hba
; /* device path of the raid controller for this metadata */
389 const struct imsm_orom
*orom
; /* platform firmware support */
390 struct intel_super
*next
; /* (temp) list for disambiguating family_num */
394 struct imsm_disk disk
;
395 #define IMSM_UNKNOWN_OWNER (-1)
397 struct intel_disk
*next
;
401 unsigned long long start
, size
;
404 /* definitions of reshape process types */
405 enum imsm_reshape_type
{
411 /* definition of messages passed to imsm_process_update */
412 enum imsm_update_type
{
413 update_activate_spare
,
417 update_add_remove_disk
,
418 update_reshape_container_disks
,
419 update_reshape_migration
,
421 update_general_migration_checkpoint
,
424 struct imsm_update_activate_spare
{
425 enum imsm_update_type type
;
429 struct imsm_update_activate_spare
*next
;
442 enum takeover_direction
{
446 struct imsm_update_takeover
{
447 enum imsm_update_type type
;
449 enum takeover_direction direction
;
452 struct imsm_update_reshape
{
453 enum imsm_update_type type
;
457 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
460 struct imsm_update_reshape_migration
{
461 enum imsm_update_type type
;
464 /* fields for array migration changes
471 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
474 struct imsm_update_general_migration_checkpoint
{
475 enum imsm_update_type type
;
476 __u32 curr_migr_unit
;
480 __u8 serial
[MAX_RAID_SERIAL_LEN
];
483 struct imsm_update_create_array
{
484 enum imsm_update_type type
;
489 struct imsm_update_kill_array
{
490 enum imsm_update_type type
;
494 struct imsm_update_rename_array
{
495 enum imsm_update_type type
;
496 __u8 name
[MAX_RAID_SERIAL_LEN
];
500 struct imsm_update_add_remove_disk
{
501 enum imsm_update_type type
;
505 static const char *_sys_dev_type
[] = {
506 [SYS_DEV_UNKNOWN
] = "Unknown",
507 [SYS_DEV_SAS
] = "SAS",
508 [SYS_DEV_SATA
] = "SATA"
511 const char *get_sys_dev_type(enum sys_dev_type type
)
513 if (type
>= SYS_DEV_MAX
)
514 type
= SYS_DEV_UNKNOWN
;
516 return _sys_dev_type
[type
];
519 static struct intel_hba
* alloc_intel_hba(struct sys_dev
*device
)
521 struct intel_hba
*result
= malloc(sizeof(*result
));
523 result
->type
= device
->type
;
524 result
->path
= strdup(device
->path
);
526 if (result
->path
&& (result
->pci_id
= strrchr(result
->path
, '/')) != NULL
)
532 static struct intel_hba
* find_intel_hba(struct intel_hba
*hba
, struct sys_dev
*device
)
534 struct intel_hba
*result
=NULL
;
535 for (result
= hba
; result
; result
= result
->next
) {
536 if (result
->type
== device
->type
&& strcmp(result
->path
, device
->path
) == 0)
542 static int attach_hba_to_super(struct intel_super
*super
, struct sys_dev
*device
)
544 struct intel_hba
*hba
;
546 /* check if disk attached to Intel HBA */
547 hba
= find_intel_hba(super
->hba
, device
);
550 /* Check if HBA is already attached to super */
551 if (super
->hba
== NULL
) {
552 super
->hba
= alloc_intel_hba(device
);
557 /* Intel metadata allows for all disks attached to the same type HBA.
558 * Do not sypport odf HBA types mixing
560 if (device
->type
!= hba
->type
)
566 hba
->next
= alloc_intel_hba(device
);
570 static struct sys_dev
* find_disk_attached_hba(int fd
, const char *devname
)
572 struct sys_dev
*list
, *elem
, *prev
;
575 if ((list
= find_intel_devices()) == NULL
)
579 disk_path
= (char *) devname
;
581 disk_path
= diskfd_to_devpath(fd
);
588 for (prev
= NULL
, elem
= list
; elem
; prev
= elem
, elem
= elem
->next
) {
589 if (path_attached_to_hba(disk_path
, elem
->path
)) {
593 prev
->next
= elem
->next
;
595 if (disk_path
!= devname
)
601 if (disk_path
!= devname
)
609 static int find_intel_hba_capability(int fd
, struct intel_super
*super
,
612 static struct supertype
*match_metadata_desc_imsm(char *arg
)
614 struct supertype
*st
;
616 if (strcmp(arg
, "imsm") != 0 &&
617 strcmp(arg
, "default") != 0
621 st
= malloc(sizeof(*st
));
624 memset(st
, 0, sizeof(*st
));
625 st
->container_dev
= NoMdDev
;
626 st
->ss
= &super_imsm
;
627 st
->max_devs
= IMSM_MAX_DEVICES
;
628 st
->minor_version
= 0;
634 static __u8
*get_imsm_version(struct imsm_super
*mpb
)
636 return &mpb
->sig
[MPB_SIG_LEN
];
640 /* retrieve a disk directly from the anchor when the anchor is known to be
641 * up-to-date, currently only at load time
643 static struct imsm_disk
*__get_imsm_disk(struct imsm_super
*mpb
, __u8 index
)
645 if (index
>= mpb
->num_disks
)
647 return &mpb
->disk
[index
];
650 /* retrieve the disk description based on a index of the disk
653 static struct dl
*get_imsm_dl_disk(struct intel_super
*super
, __u8 index
)
657 for (d
= super
->disks
; d
; d
= d
->next
)
658 if (d
->index
== index
)
663 /* retrieve a disk from the parsed metadata */
664 static struct imsm_disk
*get_imsm_disk(struct intel_super
*super
, __u8 index
)
668 dl
= get_imsm_dl_disk(super
, index
);
675 /* generate a checksum directly from the anchor when the anchor is known to be
676 * up-to-date, currently only at load or write_super after coalescing
678 static __u32
__gen_imsm_checksum(struct imsm_super
*mpb
)
680 __u32 end
= mpb
->mpb_size
/ sizeof(end
);
681 __u32
*p
= (__u32
*) mpb
;
685 sum
+= __le32_to_cpu(*p
);
689 return sum
- __le32_to_cpu(mpb
->check_sum
);
692 static size_t sizeof_imsm_map(struct imsm_map
*map
)
694 return sizeof(struct imsm_map
) + sizeof(__u32
) * (map
->num_members
- 1);
697 struct imsm_map
*get_imsm_map(struct imsm_dev
*dev
, int second_map
)
699 /* A device can have 2 maps if it is in the middle of a migration.
701 * MAP_0 - we return the first map
702 * MAP_1 - we return the second map if it exists, else NULL
703 * MAP_X - we return the second map if it exists, else the first
705 struct imsm_map
*map
= &dev
->vol
.map
[0];
706 struct imsm_map
*map2
= NULL
;
708 if (dev
->vol
.migr_state
)
709 map2
= (void *)map
+ sizeof_imsm_map(map
);
711 switch (second_map
) {
728 /* return the size of the device.
729 * migr_state increases the returned size if map[0] were to be duplicated
731 static size_t sizeof_imsm_dev(struct imsm_dev
*dev
, int migr_state
)
733 size_t size
= sizeof(*dev
) - sizeof(struct imsm_map
) +
734 sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
736 /* migrating means an additional map */
737 if (dev
->vol
.migr_state
)
738 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_1
));
740 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
746 /* retrieve disk serial number list from a metadata update */
747 static struct disk_info
*get_disk_info(struct imsm_update_create_array
*update
)
750 struct disk_info
*inf
;
752 inf
= u
+ sizeof(*update
) - sizeof(struct imsm_dev
) +
753 sizeof_imsm_dev(&update
->dev
, 0);
759 static struct imsm_dev
*__get_imsm_dev(struct imsm_super
*mpb
, __u8 index
)
765 if (index
>= mpb
->num_raid_devs
)
768 /* devices start after all disks */
769 offset
= ((void *) &mpb
->disk
[mpb
->num_disks
]) - _mpb
;
771 for (i
= 0; i
<= index
; i
++)
773 return _mpb
+ offset
;
775 offset
+= sizeof_imsm_dev(_mpb
+ offset
, 0);
780 static struct imsm_dev
*get_imsm_dev(struct intel_super
*super
, __u8 index
)
782 struct intel_dev
*dv
;
784 if (index
>= super
->anchor
->num_raid_devs
)
786 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
787 if (dv
->index
== index
)
794 * == MAP_0 get first map
795 * == MAP_1 get second map
796 * == MAP_X than get map according to the current migr_state
798 static __u32
get_imsm_ord_tbl_ent(struct imsm_dev
*dev
,
802 struct imsm_map
*map
;
804 map
= get_imsm_map(dev
, second_map
);
806 /* top byte identifies disk under rebuild */
807 return __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
810 #define ord_to_idx(ord) (((ord) << 8) >> 8)
811 static __u32
get_imsm_disk_idx(struct imsm_dev
*dev
, int slot
, int second_map
)
813 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, second_map
);
815 return ord_to_idx(ord
);
818 static void set_imsm_ord_tbl_ent(struct imsm_map
*map
, int slot
, __u32 ord
)
820 map
->disk_ord_tbl
[slot
] = __cpu_to_le32(ord
);
823 static int get_imsm_disk_slot(struct imsm_map
*map
, unsigned idx
)
828 for (slot
= 0; slot
< map
->num_members
; slot
++) {
829 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
830 if (ord_to_idx(ord
) == idx
)
837 static int get_imsm_raid_level(struct imsm_map
*map
)
839 if (map
->raid_level
== 1) {
840 if (map
->num_members
== 2)
846 return map
->raid_level
;
849 static int cmp_extent(const void *av
, const void *bv
)
851 const struct extent
*a
= av
;
852 const struct extent
*b
= bv
;
853 if (a
->start
< b
->start
)
855 if (a
->start
> b
->start
)
860 static int count_memberships(struct dl
*dl
, struct intel_super
*super
)
865 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
866 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
867 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
869 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
876 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
);
878 static int split_ull(unsigned long long n
, __u32
*lo
, __u32
*hi
)
880 if (lo
== 0 || hi
== 0)
882 *lo
= __le32_to_cpu((unsigned)n
);
883 *hi
= __le32_to_cpu((unsigned)(n
>> 32));
887 static unsigned long long join_u32(__u32 lo
, __u32 hi
)
889 return (unsigned long long)__le32_to_cpu(lo
) |
890 (((unsigned long long)__le32_to_cpu(hi
)) << 32);
893 static unsigned long long total_blocks(struct imsm_disk
*disk
)
897 return join_u32(disk
->total_blocks_lo
, disk
->total_blocks_hi
);
900 static unsigned long long pba_of_lba0(struct imsm_map
*map
)
904 return join_u32(map
->pba_of_lba0_lo
, map
->pba_of_lba0_hi
);
907 static unsigned long long blocks_per_member(struct imsm_map
*map
)
911 return join_u32(map
->blocks_per_member_lo
, map
->blocks_per_member_hi
);
914 static unsigned long long num_data_stripes(struct imsm_map
*map
)
918 return join_u32(map
->num_data_stripes_lo
, map
->num_data_stripes_hi
);
921 static void set_total_blocks(struct imsm_disk
*disk
, unsigned long long n
)
923 split_ull(n
, &disk
->total_blocks_lo
, &disk
->total_blocks_hi
);
926 static void set_pba_of_lba0(struct imsm_map
*map
, unsigned long long n
)
928 split_ull(n
, &map
->pba_of_lba0_lo
, &map
->pba_of_lba0_hi
);
931 static void set_blocks_per_member(struct imsm_map
*map
, unsigned long long n
)
933 split_ull(n
, &map
->blocks_per_member_lo
, &map
->blocks_per_member_hi
);
936 static void set_num_data_stripes(struct imsm_map
*map
, unsigned long long n
)
938 split_ull(n
, &map
->num_data_stripes_lo
, &map
->num_data_stripes_hi
);
941 static struct extent
*get_extents(struct intel_super
*super
, struct dl
*dl
)
943 /* find a list of used extents on the given physical device */
944 struct extent
*rv
, *e
;
946 int memberships
= count_memberships(dl
, super
);
949 /* trim the reserved area for spares, so they can join any array
950 * regardless of whether the OROM has assigned sectors from the
951 * IMSM_RESERVED_SECTORS region
954 reservation
= imsm_min_reserved_sectors(super
);
956 reservation
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
958 rv
= malloc(sizeof(struct extent
) * (memberships
+ 1));
963 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
964 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
965 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
967 if (get_imsm_disk_slot(map
, dl
->index
) >= 0) {
968 e
->start
= pba_of_lba0(map
);
969 e
->size
= blocks_per_member(map
);
973 qsort(rv
, memberships
, sizeof(*rv
), cmp_extent
);
975 /* determine the start of the metadata
976 * when no raid devices are defined use the default
977 * ...otherwise allow the metadata to truncate the value
978 * as is the case with older versions of imsm
981 struct extent
*last
= &rv
[memberships
- 1];
982 unsigned long long remainder
;
984 remainder
= total_blocks(&dl
->disk
) - (last
->start
+ last
->size
);
985 /* round down to 1k block to satisfy precision of the kernel
989 /* make sure remainder is still sane */
990 if (remainder
< (unsigned)ROUND_UP(super
->len
, 512) >> 9)
991 remainder
= ROUND_UP(super
->len
, 512) >> 9;
992 if (reservation
> remainder
)
993 reservation
= remainder
;
995 e
->start
= total_blocks(&dl
->disk
) - reservation
;
1000 /* try to determine how much space is reserved for metadata from
1001 * the last get_extents() entry, otherwise fallback to the
1004 static __u32
imsm_reserved_sectors(struct intel_super
*super
, struct dl
*dl
)
1010 /* for spares just return a minimal reservation which will grow
1011 * once the spare is picked up by an array
1013 if (dl
->index
== -1)
1014 return MPB_SECTOR_CNT
;
1016 e
= get_extents(super
, dl
);
1018 return MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1020 /* scroll to last entry */
1021 for (i
= 0; e
[i
].size
; i
++)
1024 rv
= total_blocks(&dl
->disk
) - e
[i
].start
;
1031 static int is_spare(struct imsm_disk
*disk
)
1033 return (disk
->status
& SPARE_DISK
) == SPARE_DISK
;
1036 static int is_configured(struct imsm_disk
*disk
)
1038 return (disk
->status
& CONFIGURED_DISK
) == CONFIGURED_DISK
;
1041 static int is_failed(struct imsm_disk
*disk
)
1043 return (disk
->status
& FAILED_DISK
) == FAILED_DISK
;
1046 /* try to determine how much space is reserved for metadata from
1047 * the last get_extents() entry on the smallest active disk,
1048 * otherwise fallback to the default
1050 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
)
1054 unsigned long long min_active
;
1056 __u32 rv
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1057 struct dl
*dl
, *dl_min
= NULL
;
1063 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
1066 unsigned long long blocks
= total_blocks(&dl
->disk
);
1067 if (blocks
< min_active
|| min_active
== 0) {
1069 min_active
= blocks
;
1075 /* find last lba used by subarrays on the smallest active disk */
1076 e
= get_extents(super
, dl_min
);
1079 for (i
= 0; e
[i
].size
; i
++)
1082 remainder
= min_active
- e
[i
].start
;
1085 /* to give priority to recovery we should not require full
1086 IMSM_RESERVED_SECTORS from the spare */
1087 rv
= MPB_SECTOR_CNT
+ NUM_BLOCKS_DIRTY_STRIPE_REGION
;
1089 /* if real reservation is smaller use that value */
1090 return (remainder
< rv
) ? remainder
: rv
;
1093 /* Return minimum size of a spare that can be used in this array*/
1094 static unsigned long long min_acceptable_spare_size_imsm(struct supertype
*st
)
1096 struct intel_super
*super
= st
->sb
;
1100 unsigned long long rv
= 0;
1104 /* find first active disk in array */
1106 while (dl
&& (is_failed(&dl
->disk
) || dl
->index
== -1))
1110 /* find last lba used by subarrays */
1111 e
= get_extents(super
, dl
);
1114 for (i
= 0; e
[i
].size
; i
++)
1117 rv
= e
[i
-1].start
+ e
[i
-1].size
;
1120 /* add the amount of space needed for metadata */
1121 rv
= rv
+ imsm_min_reserved_sectors(super
);
1126 static int is_gen_migration(struct imsm_dev
*dev
);
1129 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
1130 struct imsm_dev
*dev
);
1132 static void print_imsm_dev(struct intel_super
*super
,
1133 struct imsm_dev
*dev
,
1139 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1140 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
1144 printf("[%.16s]:\n", dev
->volume
);
1145 printf(" UUID : %s\n", uuid
);
1146 printf(" RAID Level : %d", get_imsm_raid_level(map
));
1148 printf(" <-- %d", get_imsm_raid_level(map2
));
1150 printf(" Members : %d", map
->num_members
);
1152 printf(" <-- %d", map2
->num_members
);
1154 printf(" Slots : [");
1155 for (i
= 0; i
< map
->num_members
; i
++) {
1156 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
1157 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1162 for (i
= 0; i
< map2
->num_members
; i
++) {
1163 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_1
);
1164 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1169 printf(" Failed disk : ");
1170 if (map
->failed_disk_num
== 0xff)
1173 printf("%i", map
->failed_disk_num
);
1175 slot
= get_imsm_disk_slot(map
, disk_idx
);
1177 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
1178 printf(" This Slot : %d%s\n", slot
,
1179 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
1181 printf(" This Slot : ?\n");
1182 sz
= __le32_to_cpu(dev
->size_high
);
1184 sz
+= __le32_to_cpu(dev
->size_low
);
1185 printf(" Array Size : %llu%s\n", (unsigned long long)sz
,
1186 human_size(sz
* 512));
1187 sz
= blocks_per_member(map
);
1188 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz
,
1189 human_size(sz
* 512));
1190 printf(" Sector Offset : %llu\n",
1192 printf(" Num Stripes : %llu\n",
1193 num_data_stripes(map
));
1194 printf(" Chunk Size : %u KiB",
1195 __le16_to_cpu(map
->blocks_per_strip
) / 2);
1197 printf(" <-- %u KiB",
1198 __le16_to_cpu(map2
->blocks_per_strip
) / 2);
1200 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
1201 printf(" Migrate State : ");
1202 if (dev
->vol
.migr_state
) {
1203 if (migr_type(dev
) == MIGR_INIT
)
1204 printf("initialize\n");
1205 else if (migr_type(dev
) == MIGR_REBUILD
)
1206 printf("rebuild\n");
1207 else if (migr_type(dev
) == MIGR_VERIFY
)
1209 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
1210 printf("general migration\n");
1211 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
1212 printf("state change\n");
1213 else if (migr_type(dev
) == MIGR_REPAIR
)
1216 printf("<unknown:%d>\n", migr_type(dev
));
1219 printf(" Map State : %s", map_state_str
[map
->map_state
]);
1220 if (dev
->vol
.migr_state
) {
1221 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
1223 printf(" <-- %s", map_state_str
[map
->map_state
]);
1224 printf("\n Checkpoint : %u ",
1225 __le32_to_cpu(dev
->vol
.curr_migr_unit
));
1226 if ((is_gen_migration(dev
)) && ((slot
> 1) || (slot
< 0)))
1229 printf("(%llu)", (unsigned long long)
1230 blocks_per_migr_unit(super
, dev
));
1233 printf(" Dirty State : %s\n", dev
->vol
.dirty
? "dirty" : "clean");
1236 static void print_imsm_disk(struct imsm_disk
*disk
, int index
, __u32 reserved
)
1238 char str
[MAX_RAID_SERIAL_LEN
+ 1];
1241 if (index
< -1 || !disk
)
1245 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
1247 printf(" Disk%02d Serial : %s\n", index
, str
);
1249 printf(" Disk Serial : %s\n", str
);
1250 printf(" State :%s%s%s\n", is_spare(disk
) ? " spare" : "",
1251 is_configured(disk
) ? " active" : "",
1252 is_failed(disk
) ? " failed" : "");
1253 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
1254 sz
= total_blocks(disk
) - reserved
;
1255 printf(" Usable Size : %llu%s\n", (unsigned long long)sz
,
1256 human_size(sz
* 512));
1259 void examine_migr_rec_imsm(struct intel_super
*super
)
1261 struct migr_record
*migr_rec
= super
->migr_rec
;
1262 struct imsm_super
*mpb
= super
->anchor
;
1265 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1266 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1267 struct imsm_map
*map
;
1270 if (is_gen_migration(dev
) == 0)
1273 printf("\nMigration Record Information:");
1275 /* first map under migration */
1276 map
= get_imsm_map(dev
, MAP_0
);
1278 slot
= get_imsm_disk_slot(map
, super
->disks
->index
);
1279 if ((map
== NULL
) || (slot
> 1) || (slot
< 0)) {
1280 printf(" Empty\n ");
1281 printf("Examine one of first two disks in array\n");
1284 printf("\n Status : ");
1285 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
1288 printf("Contains Data\n");
1289 printf(" Current Unit : %u\n",
1290 __le32_to_cpu(migr_rec
->curr_migr_unit
));
1291 printf(" Family : %u\n",
1292 __le32_to_cpu(migr_rec
->family_num
));
1293 printf(" Ascending : %u\n",
1294 __le32_to_cpu(migr_rec
->ascending_migr
));
1295 printf(" Blocks Per Unit : %u\n",
1296 __le32_to_cpu(migr_rec
->blocks_per_unit
));
1297 printf(" Dest. Depth Per Unit : %u\n",
1298 __le32_to_cpu(migr_rec
->dest_depth_per_unit
));
1299 printf(" Checkpoint Area pba : %u\n",
1300 __le32_to_cpu(migr_rec
->ckpt_area_pba
));
1301 printf(" First member lba : %u\n",
1302 __le32_to_cpu(migr_rec
->dest_1st_member_lba
));
1303 printf(" Total Number of Units : %u\n",
1304 __le32_to_cpu(migr_rec
->num_migr_units
));
1305 printf(" Size of volume : %u\n",
1306 __le32_to_cpu(migr_rec
->post_migr_vol_cap
));
1307 printf(" Expansion space for LBA64 : %u\n",
1308 __le32_to_cpu(migr_rec
->post_migr_vol_cap_hi
));
1309 printf(" Record was read from : %u\n",
1310 __le32_to_cpu(migr_rec
->ckpt_read_disk_num
));
1315 #endif /* MDASSEMBLE */
1316 /*******************************************************************************
1317 * function: imsm_check_attributes
1318 * Description: Function checks if features represented by attributes flags
1319 * are supported by mdadm.
1321 * attributes - Attributes read from metadata
1323 * 0 - passed attributes contains unsupported features flags
1324 * 1 - all features are supported
1325 ******************************************************************************/
1326 static int imsm_check_attributes(__u32 attributes
)
1329 __u32 not_supported
= MPB_ATTRIB_SUPPORTED
^0xffffffff;
1331 not_supported
&= ~MPB_ATTRIB_IGNORED
;
1333 not_supported
&= attributes
;
1334 if (not_supported
) {
1335 fprintf(stderr
, Name
"(IMSM): Unsupported attributes : %x\n",
1336 (unsigned)__le32_to_cpu(not_supported
));
1337 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1338 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
1339 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1341 if (not_supported
& MPB_ATTRIB_2TB
) {
1342 dprintf("\t\tMPB_ATTRIB_2TB\n");
1343 not_supported
^= MPB_ATTRIB_2TB
;
1345 if (not_supported
& MPB_ATTRIB_RAID0
) {
1346 dprintf("\t\tMPB_ATTRIB_RAID0\n");
1347 not_supported
^= MPB_ATTRIB_RAID0
;
1349 if (not_supported
& MPB_ATTRIB_RAID1
) {
1350 dprintf("\t\tMPB_ATTRIB_RAID1\n");
1351 not_supported
^= MPB_ATTRIB_RAID1
;
1353 if (not_supported
& MPB_ATTRIB_RAID10
) {
1354 dprintf("\t\tMPB_ATTRIB_RAID10\n");
1355 not_supported
^= MPB_ATTRIB_RAID10
;
1357 if (not_supported
& MPB_ATTRIB_RAID1E
) {
1358 dprintf("\t\tMPB_ATTRIB_RAID1E\n");
1359 not_supported
^= MPB_ATTRIB_RAID1E
;
1361 if (not_supported
& MPB_ATTRIB_RAID5
) {
1362 dprintf("\t\tMPB_ATTRIB_RAID5\n");
1363 not_supported
^= MPB_ATTRIB_RAID5
;
1365 if (not_supported
& MPB_ATTRIB_RAIDCNG
) {
1366 dprintf("\t\tMPB_ATTRIB_RAIDCNG\n");
1367 not_supported
^= MPB_ATTRIB_RAIDCNG
;
1369 if (not_supported
& MPB_ATTRIB_BBM
) {
1370 dprintf("\t\tMPB_ATTRIB_BBM\n");
1371 not_supported
^= MPB_ATTRIB_BBM
;
1373 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1374 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n");
1375 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1377 if (not_supported
& MPB_ATTRIB_EXP_STRIPE_SIZE
) {
1378 dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n");
1379 not_supported
^= MPB_ATTRIB_EXP_STRIPE_SIZE
;
1381 if (not_supported
& MPB_ATTRIB_2TB_DISK
) {
1382 dprintf("\t\tMPB_ATTRIB_2TB_DISK\n");
1383 not_supported
^= MPB_ATTRIB_2TB_DISK
;
1385 if (not_supported
& MPB_ATTRIB_NEVER_USE2
) {
1386 dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n");
1387 not_supported
^= MPB_ATTRIB_NEVER_USE2
;
1389 if (not_supported
& MPB_ATTRIB_NEVER_USE
) {
1390 dprintf("\t\tMPB_ATTRIB_NEVER_USE\n");
1391 not_supported
^= MPB_ATTRIB_NEVER_USE
;
1395 dprintf(Name
"(IMSM): Unknown attributes : %x\n", not_supported
);
1404 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
);
1406 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
1408 struct intel_super
*super
= st
->sb
;
1409 struct imsm_super
*mpb
= super
->anchor
;
1410 char str
[MAX_SIGNATURE_LENGTH
];
1415 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
1418 snprintf(str
, MPB_SIG_LEN
, "%s", mpb
->sig
);
1419 printf(" Magic : %s\n", str
);
1420 snprintf(str
, strlen(MPB_VERSION_RAID0
), "%s", get_imsm_version(mpb
));
1421 printf(" Version : %s\n", get_imsm_version(mpb
));
1422 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
1423 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
1424 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
1425 printf(" Attributes : ");
1426 if (imsm_check_attributes(mpb
->attributes
))
1427 printf("All supported\n");
1429 printf("not supported\n");
1430 getinfo_super_imsm(st
, &info
, NULL
);
1431 fname_from_uuid(st
, &info
, nbuf
, ':');
1432 printf(" UUID : %s\n", nbuf
+ 5);
1433 sum
= __le32_to_cpu(mpb
->check_sum
);
1434 printf(" Checksum : %08x %s\n", sum
,
1435 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
1436 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
));
1437 printf(" Disks : %d\n", mpb
->num_disks
);
1438 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
1439 print_imsm_disk(__get_imsm_disk(mpb
, super
->disks
->index
), super
->disks
->index
, reserved
);
1440 if (super
->bbm_log
) {
1441 struct bbm_log
*log
= super
->bbm_log
;
1444 printf("Bad Block Management Log:\n");
1445 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
1446 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
1447 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
1448 printf(" Spare Blocks : %d\n", __le32_to_cpu(log
->reserved_spare_block_count
));
1449 printf(" First Spare : %llx\n",
1450 (unsigned long long) __le64_to_cpu(log
->first_spare_lba
));
1452 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1454 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1456 super
->current_vol
= i
;
1457 getinfo_super_imsm(st
, &info
, NULL
);
1458 fname_from_uuid(st
, &info
, nbuf
, ':');
1459 print_imsm_dev(super
, dev
, nbuf
+ 5, super
->disks
->index
);
1461 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1462 if (i
== super
->disks
->index
)
1464 print_imsm_disk(__get_imsm_disk(mpb
, i
), i
, reserved
);
1467 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
1468 if (dl
->index
== -1)
1469 print_imsm_disk(&dl
->disk
, -1, reserved
);
1471 examine_migr_rec_imsm(super
);
1474 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
1476 /* We just write a generic IMSM ARRAY entry */
1479 struct intel_super
*super
= st
->sb
;
1481 if (!super
->anchor
->num_raid_devs
) {
1482 printf("ARRAY metadata=imsm\n");
1486 getinfo_super_imsm(st
, &info
, NULL
);
1487 fname_from_uuid(st
, &info
, nbuf
, ':');
1488 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
1491 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
1493 /* We just write a generic IMSM ARRAY entry */
1497 struct intel_super
*super
= st
->sb
;
1500 if (!super
->anchor
->num_raid_devs
)
1503 getinfo_super_imsm(st
, &info
, NULL
);
1504 fname_from_uuid(st
, &info
, nbuf
, ':');
1505 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1506 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1508 super
->current_vol
= i
;
1509 getinfo_super_imsm(st
, &info
, NULL
);
1510 fname_from_uuid(st
, &info
, nbuf1
, ':');
1511 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
1512 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
1516 static void export_examine_super_imsm(struct supertype
*st
)
1518 struct intel_super
*super
= st
->sb
;
1519 struct imsm_super
*mpb
= super
->anchor
;
1523 getinfo_super_imsm(st
, &info
, NULL
);
1524 fname_from_uuid(st
, &info
, nbuf
, ':');
1525 printf("MD_METADATA=imsm\n");
1526 printf("MD_LEVEL=container\n");
1527 printf("MD_UUID=%s\n", nbuf
+5);
1528 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
1531 static void detail_super_imsm(struct supertype
*st
, char *homehost
)
1536 getinfo_super_imsm(st
, &info
, NULL
);
1537 fname_from_uuid(st
, &info
, nbuf
, ':');
1538 printf("\n UUID : %s\n", nbuf
+ 5);
1541 static void brief_detail_super_imsm(struct supertype
*st
)
1545 getinfo_super_imsm(st
, &info
, NULL
);
1546 fname_from_uuid(st
, &info
, nbuf
, ':');
1547 printf(" UUID=%s", nbuf
+ 5);
1550 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
);
1551 static void fd2devname(int fd
, char *name
);
1553 static int ahci_enumerate_ports(const char *hba_path
, int port_count
, int host_base
, int verbose
)
1555 /* dump an unsorted list of devices attached to AHCI Intel storage
1556 * controller, as well as non-connected ports
1558 int hba_len
= strlen(hba_path
) + 1;
1563 unsigned long port_mask
= (1 << port_count
) - 1;
1565 if (port_count
> (int)sizeof(port_mask
) * 8) {
1567 fprintf(stderr
, Name
": port_count %d out of range\n", port_count
);
1571 /* scroll through /sys/dev/block looking for devices attached to
1574 dir
= opendir("/sys/dev/block");
1575 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
1586 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
1588 path
= devt_to_devpath(makedev(major
, minor
));
1591 if (!path_attached_to_hba(path
, hba_path
)) {
1597 /* retrieve the scsi device type */
1598 if (asprintf(&device
, "/sys/dev/block/%d:%d/device/xxxxxxx", major
, minor
) < 0) {
1600 fprintf(stderr
, Name
": failed to allocate 'device'\n");
1604 sprintf(device
, "/sys/dev/block/%d:%d/device/type", major
, minor
);
1605 if (load_sys(device
, buf
) != 0) {
1607 fprintf(stderr
, Name
": failed to read device type for %s\n",
1613 type
= strtoul(buf
, NULL
, 10);
1615 /* if it's not a disk print the vendor and model */
1616 if (!(type
== 0 || type
== 7 || type
== 14)) {
1619 sprintf(device
, "/sys/dev/block/%d:%d/device/vendor", major
, minor
);
1620 if (load_sys(device
, buf
) == 0) {
1621 strncpy(vendor
, buf
, sizeof(vendor
));
1622 vendor
[sizeof(vendor
) - 1] = '\0';
1623 c
= (char *) &vendor
[sizeof(vendor
) - 1];
1624 while (isspace(*c
) || *c
== '\0')
1628 sprintf(device
, "/sys/dev/block/%d:%d/device/model", major
, minor
);
1629 if (load_sys(device
, buf
) == 0) {
1630 strncpy(model
, buf
, sizeof(model
));
1631 model
[sizeof(model
) - 1] = '\0';
1632 c
= (char *) &model
[sizeof(model
) - 1];
1633 while (isspace(*c
) || *c
== '\0')
1637 if (vendor
[0] && model
[0])
1638 sprintf(buf
, "%.64s %.64s", vendor
, model
);
1640 switch (type
) { /* numbers from hald/linux/device.c */
1641 case 1: sprintf(buf
, "tape"); break;
1642 case 2: sprintf(buf
, "printer"); break;
1643 case 3: sprintf(buf
, "processor"); break;
1645 case 5: sprintf(buf
, "cdrom"); break;
1646 case 6: sprintf(buf
, "scanner"); break;
1647 case 8: sprintf(buf
, "media_changer"); break;
1648 case 9: sprintf(buf
, "comm"); break;
1649 case 12: sprintf(buf
, "raid"); break;
1650 default: sprintf(buf
, "unknown");
1656 /* chop device path to 'host%d' and calculate the port number */
1657 c
= strchr(&path
[hba_len
], '/');
1660 fprintf(stderr
, Name
": %s - invalid path name\n", path
+ hba_len
);
1665 if (sscanf(&path
[hba_len
], "host%d", &port
) == 1)
1669 *c
= '/'; /* repair the full string */
1670 fprintf(stderr
, Name
": failed to determine port number for %s\n",
1677 /* mark this port as used */
1678 port_mask
&= ~(1 << port
);
1680 /* print out the device information */
1682 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
1686 fd
= dev_open(ent
->d_name
, O_RDONLY
);
1688 printf(" Port%d : - disk info unavailable -\n", port
);
1690 fd2devname(fd
, buf
);
1691 printf(" Port%d : %s", port
, buf
);
1692 if (imsm_read_serial(fd
, NULL
, (__u8
*) buf
) == 0)
1693 printf(" (%.*s)\n", MAX_RAID_SERIAL_LEN
, buf
);
1708 for (i
= 0; i
< port_count
; i
++)
1709 if (port_mask
& (1 << i
))
1710 printf(" Port%d : - no device attached -\n", i
);
1716 static void print_found_intel_controllers(struct sys_dev
*elem
)
1718 for (; elem
; elem
= elem
->next
) {
1719 fprintf(stderr
, Name
": found Intel(R) ");
1720 if (elem
->type
== SYS_DEV_SATA
)
1721 fprintf(stderr
, "SATA ");
1722 else if (elem
->type
== SYS_DEV_SAS
)
1723 fprintf(stderr
, "SAS ");
1724 fprintf(stderr
, "RAID controller");
1726 fprintf(stderr
, " at %s", elem
->pci_id
);
1727 fprintf(stderr
, ".\n");
1732 static int ahci_get_port_count(const char *hba_path
, int *port_count
)
1739 if ((dir
= opendir(hba_path
)) == NULL
)
1742 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
1745 if (sscanf(ent
->d_name
, "host%d", &host
) != 1)
1747 if (*port_count
== 0)
1749 else if (host
< host_base
)
1752 if (host
+ 1 > *port_count
+ host_base
)
1753 *port_count
= host
+ 1 - host_base
;
1759 static void print_imsm_capability(const struct imsm_orom
*orom
)
1761 printf(" Platform : Intel(R) Matrix Storage Manager\n");
1762 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
1763 orom
->hotfix_ver
, orom
->build
);
1764 printf(" RAID Levels :%s%s%s%s%s\n",
1765 imsm_orom_has_raid0(orom
) ? " raid0" : "",
1766 imsm_orom_has_raid1(orom
) ? " raid1" : "",
1767 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
1768 imsm_orom_has_raid10(orom
) ? " raid10" : "",
1769 imsm_orom_has_raid5(orom
) ? " raid5" : "");
1770 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1771 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
1772 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
1773 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
1774 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
1775 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
1776 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
1777 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
1778 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
1779 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
1780 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
1781 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
1782 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
1783 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
1784 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
1785 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
1786 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
1787 printf(" 2TB volumes :%s supported\n",
1788 (orom
->attr
& IMSM_OROM_ATTR_2TB
)?"":" not");
1789 printf(" 2TB disks :%s supported\n",
1790 (orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
)?"":" not");
1791 printf(" Max Disks : %d\n", orom
->tds
);
1792 printf(" Max Volumes : %d per array, %d per controller\n",
1793 orom
->vpa
, orom
->vphba
);
1797 static int detail_platform_imsm(int verbose
, int enumerate_only
)
1799 /* There are two components to imsm platform support, the ahci SATA
1800 * controller and the option-rom. To find the SATA controller we
1801 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
1802 * controller with the Intel vendor id is present. This approach
1803 * allows mdadm to leverage the kernel's ahci detection logic, with the
1804 * caveat that if ahci.ko is not loaded mdadm will not be able to
1805 * detect platform raid capabilities. The option-rom resides in a
1806 * platform "Adapter ROM". We scan for its signature to retrieve the
1807 * platform capabilities. If raid support is disabled in the BIOS the
1808 * option-rom capability structure will not be available.
1810 const struct imsm_orom
*orom
;
1811 struct sys_dev
*list
, *hba
;
1816 if (enumerate_only
) {
1817 if (check_env("IMSM_NO_PLATFORM"))
1819 list
= find_intel_devices();
1822 for (hba
= list
; hba
; hba
= hba
->next
) {
1823 orom
= find_imsm_capability(hba
->type
);
1829 free_sys_dev(&list
);
1833 list
= find_intel_devices();
1836 fprintf(stderr
, Name
": no active Intel(R) RAID "
1837 "controller found.\n");
1838 free_sys_dev(&list
);
1841 print_found_intel_controllers(list
);
1843 for (hba
= list
; hba
; hba
= hba
->next
) {
1844 orom
= find_imsm_capability(hba
->type
);
1846 fprintf(stderr
, Name
": imsm capabilities not found for controller: %s (type %s)\n",
1847 hba
->path
, get_sys_dev_type(hba
->type
));
1849 print_imsm_capability(orom
);
1852 for (hba
= list
; hba
; hba
= hba
->next
) {
1853 printf(" I/O Controller : %s (%s)\n",
1854 hba
->path
, get_sys_dev_type(hba
->type
));
1856 if (hba
->type
== SYS_DEV_SATA
) {
1857 host_base
= ahci_get_port_count(hba
->path
, &port_count
);
1858 if (ahci_enumerate_ports(hba
->path
, port_count
, host_base
, verbose
)) {
1860 fprintf(stderr
, Name
": failed to enumerate "
1861 "ports on SATA controller at %s.", hba
->pci_id
);
1867 free_sys_dev(&list
);
1872 static int match_home_imsm(struct supertype
*st
, char *homehost
)
1874 /* the imsm metadata format does not specify any host
1875 * identification information. We return -1 since we can never
1876 * confirm nor deny whether a given array is "meant" for this
1877 * host. We rely on compare_super and the 'family_num' fields to
1878 * exclude member disks that do not belong, and we rely on
1879 * mdadm.conf to specify the arrays that should be assembled.
1880 * Auto-assembly may still pick up "foreign" arrays.
1886 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
1888 /* The uuid returned here is used for:
1889 * uuid to put into bitmap file (Create, Grow)
1890 * uuid for backup header when saving critical section (Grow)
1891 * comparing uuids when re-adding a device into an array
1892 * In these cases the uuid required is that of the data-array,
1893 * not the device-set.
1894 * uuid to recognise same set when adding a missing device back
1895 * to an array. This is a uuid for the device-set.
1897 * For each of these we can make do with a truncated
1898 * or hashed uuid rather than the original, as long as
1900 * In each case the uuid required is that of the data-array,
1901 * not the device-set.
1903 /* imsm does not track uuid's so we synthesis one using sha1 on
1904 * - The signature (Which is constant for all imsm array, but no matter)
1905 * - the orig_family_num of the container
1906 * - the index number of the volume
1907 * - the 'serial' number of the volume.
1908 * Hopefully these are all constant.
1910 struct intel_super
*super
= st
->sb
;
1913 struct sha1_ctx ctx
;
1914 struct imsm_dev
*dev
= NULL
;
1917 /* some mdadm versions failed to set ->orig_family_num, in which
1918 * case fall back to ->family_num. orig_family_num will be
1919 * fixed up with the first metadata update.
1921 family_num
= super
->anchor
->orig_family_num
;
1922 if (family_num
== 0)
1923 family_num
= super
->anchor
->family_num
;
1924 sha1_init_ctx(&ctx
);
1925 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
1926 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
1927 if (super
->current_vol
>= 0)
1928 dev
= get_imsm_dev(super
, super
->current_vol
);
1930 __u32 vol
= super
->current_vol
;
1931 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
1932 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
1934 sha1_finish_ctx(&ctx
, buf
);
1935 memcpy(uuid
, buf
, 4*4);
1940 get_imsm_numerical_version(struct imsm_super
*mpb
, int *m
, int *p
)
1942 __u8
*v
= get_imsm_version(mpb
);
1943 __u8
*end
= mpb
->sig
+ MAX_SIGNATURE_LENGTH
;
1944 char major
[] = { 0, 0, 0 };
1945 char minor
[] = { 0 ,0, 0 };
1946 char patch
[] = { 0, 0, 0 };
1947 char *ver_parse
[] = { major
, minor
, patch
};
1951 while (*v
!= '\0' && v
< end
) {
1952 if (*v
!= '.' && j
< 2)
1953 ver_parse
[i
][j
++] = *v
;
1961 *m
= strtol(minor
, NULL
, 0);
1962 *p
= strtol(patch
, NULL
, 0);
1966 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
1968 /* migr_strip_size when repairing or initializing parity */
1969 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1970 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1972 switch (get_imsm_raid_level(map
)) {
1977 return 128*1024 >> 9;
1981 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
1983 /* migr_strip_size when rebuilding a degraded disk, no idea why
1984 * this is different than migr_strip_size_resync(), but it's good
1987 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
1988 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1990 switch (get_imsm_raid_level(map
)) {
1993 if (map
->num_members
% map
->num_domains
== 0)
1994 return 128*1024 >> 9;
1998 return max((__u32
) 64*1024 >> 9, chunk
);
2000 return 128*1024 >> 9;
2004 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
2006 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2007 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2008 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
2009 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
2011 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
2014 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
2016 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2017 int level
= get_imsm_raid_level(lo
);
2019 if (level
== 1 || level
== 10) {
2020 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2022 return hi
->num_domains
;
2024 return num_stripes_per_unit_resync(dev
);
2027 static __u8
imsm_num_data_members(struct imsm_dev
*dev
, int second_map
)
2029 /* named 'imsm_' because raid0, raid1 and raid10
2030 * counter-intuitively have the same number of data disks
2032 struct imsm_map
*map
= get_imsm_map(dev
, second_map
);
2034 switch (get_imsm_raid_level(map
)) {
2036 return map
->num_members
;
2040 return map
->num_members
/2;
2042 return map
->num_members
- 1;
2044 dprintf("%s: unsupported raid level\n", __func__
);
2049 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
2051 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2052 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2054 switch(get_imsm_raid_level(map
)) {
2057 return chunk
* map
->num_domains
;
2059 return chunk
* map
->num_members
;
2065 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
2067 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2068 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2069 __u32 strip
= block
/ chunk
;
2071 switch (get_imsm_raid_level(map
)) {
2074 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
2075 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
2077 return vol_stripe
* chunk
+ block
% chunk
;
2079 __u32 stripe
= strip
/ (map
->num_members
- 1);
2081 return stripe
* chunk
+ block
% chunk
;
2088 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
2089 struct imsm_dev
*dev
)
2091 /* calculate the conversion factor between per member 'blocks'
2092 * (md/{resync,rebuild}_start) and imsm migration units, return
2093 * 0 for the 'not migrating' and 'unsupported migration' cases
2095 if (!dev
->vol
.migr_state
)
2098 switch (migr_type(dev
)) {
2099 case MIGR_GEN_MIGR
: {
2100 struct migr_record
*migr_rec
= super
->migr_rec
;
2101 return __le32_to_cpu(migr_rec
->blocks_per_unit
);
2106 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2107 __u32 stripes_per_unit
;
2108 __u32 blocks_per_unit
;
2117 /* yes, this is really the translation of migr_units to
2118 * per-member blocks in the 'resync' case
2120 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
2121 migr_chunk
= migr_strip_blocks_resync(dev
);
2122 disks
= imsm_num_data_members(dev
, MAP_0
);
2123 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
2124 stripe
= __le16_to_cpu(map
->blocks_per_strip
) * disks
;
2125 segment
= blocks_per_unit
/ stripe
;
2126 block_rel
= blocks_per_unit
- segment
* stripe
;
2127 parity_depth
= parity_segment_depth(dev
);
2128 block_map
= map_migr_block(dev
, block_rel
);
2129 return block_map
+ parity_depth
* segment
;
2131 case MIGR_REBUILD
: {
2132 __u32 stripes_per_unit
;
2135 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
2136 migr_chunk
= migr_strip_blocks_rebuild(dev
);
2137 return migr_chunk
* stripes_per_unit
;
2139 case MIGR_STATE_CHANGE
:
2145 static int imsm_level_to_layout(int level
)
2153 return ALGORITHM_LEFT_ASYMMETRIC
;
2160 /*******************************************************************************
2161 * Function: read_imsm_migr_rec
2162 * Description: Function reads imsm migration record from last sector of disk
2164 * fd : disk descriptor
2165 * super : metadata info
2169 ******************************************************************************/
2170 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
)
2173 unsigned long long dsize
;
2175 get_dev_size(fd
, NULL
, &dsize
);
2176 if (lseek64(fd
, dsize
- MIGR_REC_POSITION
, SEEK_SET
) < 0) {
2178 Name
": Cannot seek to anchor block: %s\n",
2182 if (read(fd
, super
->migr_rec_buf
, MIGR_REC_BUF_SIZE
) !=
2183 MIGR_REC_BUF_SIZE
) {
2185 Name
": Cannot read migr record block: %s\n",
2195 static struct imsm_dev
*imsm_get_device_during_migration(
2196 struct intel_super
*super
)
2199 struct intel_dev
*dv
;
2201 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
2202 if (is_gen_migration(dv
->dev
))
2208 /*******************************************************************************
2209 * Function: load_imsm_migr_rec
2210 * Description: Function reads imsm migration record (it is stored at the last
2213 * super : imsm internal array info
2214 * info : general array info
2218 * -2 : no migration in progress
2219 ******************************************************************************/
2220 static int load_imsm_migr_rec(struct intel_super
*super
, struct mdinfo
*info
)
2223 struct dl
*dl
= NULL
;
2227 struct imsm_dev
*dev
;
2228 struct imsm_map
*map
= NULL
;
2231 /* find map under migration */
2232 dev
= imsm_get_device_during_migration(super
);
2233 /* nothing to load,no migration in progress?
2237 map
= get_imsm_map(dev
, MAP_0
);
2240 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
2241 /* skip spare and failed disks
2243 if (sd
->disk
.raid_disk
< 0)
2245 /* read only from one of the first two slots */
2247 slot
= get_imsm_disk_slot(map
,
2248 sd
->disk
.raid_disk
);
2249 if ((map
== NULL
) || (slot
> 1) || (slot
< 0))
2252 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
2253 fd
= dev_open(nm
, O_RDONLY
);
2259 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
2260 /* skip spare and failed disks
2264 /* read only from one of the first two slots */
2266 slot
= get_imsm_disk_slot(map
, dl
->index
);
2267 if ((map
== NULL
) || (slot
> 1) || (slot
< 0))
2269 sprintf(nm
, "%d:%d", dl
->major
, dl
->minor
);
2270 fd
= dev_open(nm
, O_RDONLY
);
2277 retval
= read_imsm_migr_rec(fd
, super
);
2286 /*******************************************************************************
2287 * function: imsm_create_metadata_checkpoint_update
2288 * Description: It creates update for checkpoint change.
2290 * super : imsm internal array info
2291 * u : pointer to prepared update
2294 * If length is equal to 0, input pointer u contains no update
2295 ******************************************************************************/
2296 static int imsm_create_metadata_checkpoint_update(
2297 struct intel_super
*super
,
2298 struct imsm_update_general_migration_checkpoint
**u
)
2301 int update_memory_size
= 0;
2303 dprintf("imsm_create_metadata_checkpoint_update(enter)\n");
2309 /* size of all update data without anchor */
2310 update_memory_size
=
2311 sizeof(struct imsm_update_general_migration_checkpoint
);
2313 *u
= calloc(1, update_memory_size
);
2315 dprintf("error: cannot get memory for "
2316 "imsm_create_metadata_checkpoint_update update\n");
2319 (*u
)->type
= update_general_migration_checkpoint
;
2320 (*u
)->curr_migr_unit
= __le32_to_cpu(super
->migr_rec
->curr_migr_unit
);
2321 dprintf("imsm_create_metadata_checkpoint_update: prepared for %u\n",
2322 (*u
)->curr_migr_unit
);
2324 return update_memory_size
;
2328 static void imsm_update_metadata_locally(struct supertype
*st
,
2329 void *buf
, int len
);
2331 /*******************************************************************************
2332 * Function: write_imsm_migr_rec
2333 * Description: Function writes imsm migration record
2334 * (at the last sector of disk)
2336 * super : imsm internal array info
2340 ******************************************************************************/
2341 static int write_imsm_migr_rec(struct supertype
*st
)
2343 struct intel_super
*super
= st
->sb
;
2344 unsigned long long dsize
;
2350 struct imsm_update_general_migration_checkpoint
*u
;
2351 struct imsm_dev
*dev
;
2352 struct imsm_map
*map
= NULL
;
2354 /* find map under migration */
2355 dev
= imsm_get_device_during_migration(super
);
2356 /* if no migration, write buffer anyway to clear migr_record
2357 * on disk based on first available device
2360 dev
= get_imsm_dev(super
, super
->current_vol
< 0 ? 0 :
2361 super
->current_vol
);
2363 map
= get_imsm_map(dev
, MAP_0
);
2365 for (sd
= super
->disks
; sd
; sd
= sd
->next
) {
2368 /* skip failed and spare devices */
2371 /* write to 2 first slots only */
2373 slot
= get_imsm_disk_slot(map
, sd
->index
);
2374 if ((map
== NULL
) || (slot
> 1) || (slot
< 0))
2377 sprintf(nm
, "%d:%d", sd
->major
, sd
->minor
);
2378 fd
= dev_open(nm
, O_RDWR
);
2381 get_dev_size(fd
, NULL
, &dsize
);
2382 if (lseek64(fd
, dsize
- MIGR_REC_POSITION
, SEEK_SET
) < 0) {
2384 Name
": Cannot seek to anchor block: %s\n",
2388 if (write(fd
, super
->migr_rec_buf
, MIGR_REC_BUF_SIZE
) !=
2389 MIGR_REC_BUF_SIZE
) {
2391 Name
": Cannot write migr record block: %s\n",
2398 /* update checkpoint information in metadata */
2399 len
= imsm_create_metadata_checkpoint_update(super
, &u
);
2402 dprintf("imsm: Cannot prepare update\n");
2405 /* update metadata locally */
2406 imsm_update_metadata_locally(st
, u
, len
);
2407 /* and possibly remotely */
2408 if (st
->update_tail
) {
2409 append_metadata_update(st
, u
, len
);
2410 /* during reshape we do all work inside metadata handler
2411 * manage_reshape(), so metadata update has to be triggered
2414 flush_metadata_updates(st
);
2415 st
->update_tail
= &st
->updates
;
2425 #endif /* MDASSEMBLE */
2427 /* spare/missing disks activations are not allowe when
2428 * array/container performs reshape operation, because
2429 * all arrays in container works on the same disks set
2431 int imsm_reshape_blocks_arrays_changes(struct intel_super
*super
)
2434 struct intel_dev
*i_dev
;
2435 struct imsm_dev
*dev
;
2437 /* check whole container
2439 for (i_dev
= super
->devlist
; i_dev
; i_dev
= i_dev
->next
) {
2441 if (is_gen_migration(dev
)) {
2442 /* No repair during any migration in container
2451 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
2453 struct intel_super
*super
= st
->sb
;
2454 struct migr_record
*migr_rec
= super
->migr_rec
;
2455 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
2456 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2457 struct imsm_map
*prev_map
= get_imsm_map(dev
, MAP_1
);
2458 struct imsm_map
*map_to_analyse
= map
;
2461 unsigned int component_size_alligment
;
2462 int map_disks
= info
->array
.raid_disks
;
2464 memset(info
, 0, sizeof(*info
));
2466 map_to_analyse
= prev_map
;
2468 dl
= super
->current_disk
;
2470 info
->container_member
= super
->current_vol
;
2471 info
->array
.raid_disks
= map
->num_members
;
2472 info
->array
.level
= get_imsm_raid_level(map_to_analyse
);
2473 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
2474 info
->array
.md_minor
= -1;
2475 info
->array
.ctime
= 0;
2476 info
->array
.utime
= 0;
2477 info
->array
.chunk_size
=
2478 __le16_to_cpu(map_to_analyse
->blocks_per_strip
) << 9;
2479 info
->array
.state
= !dev
->vol
.dirty
;
2480 info
->custom_array_size
= __le32_to_cpu(dev
->size_high
);
2481 info
->custom_array_size
<<= 32;
2482 info
->custom_array_size
|= __le32_to_cpu(dev
->size_low
);
2483 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
2485 if (is_gen_migration(dev
)) {
2486 info
->reshape_active
= 1;
2487 info
->new_level
= get_imsm_raid_level(map
);
2488 info
->new_layout
= imsm_level_to_layout(info
->new_level
);
2489 info
->new_chunk
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
2490 info
->delta_disks
= map
->num_members
- prev_map
->num_members
;
2491 if (info
->delta_disks
) {
2492 /* this needs to be applied to every array
2495 info
->reshape_active
= CONTAINER_RESHAPE
;
2497 /* We shape information that we give to md might have to be
2498 * modify to cope with md's requirement for reshaping arrays.
2499 * For example, when reshaping a RAID0, md requires it to be
2500 * presented as a degraded RAID4.
2501 * Also if a RAID0 is migrating to a RAID5 we need to specify
2502 * the array as already being RAID5, but the 'before' layout
2503 * is a RAID4-like layout.
2505 switch (info
->array
.level
) {
2507 switch(info
->new_level
) {
2509 /* conversion is happening as RAID4 */
2510 info
->array
.level
= 4;
2511 info
->array
.raid_disks
+= 1;
2514 /* conversion is happening as RAID5 */
2515 info
->array
.level
= 5;
2516 info
->array
.layout
= ALGORITHM_PARITY_N
;
2517 info
->delta_disks
-= 1;
2520 /* FIXME error message */
2521 info
->array
.level
= UnSet
;
2527 info
->new_level
= UnSet
;
2528 info
->new_layout
= UnSet
;
2529 info
->new_chunk
= info
->array
.chunk_size
;
2530 info
->delta_disks
= 0;
2534 info
->disk
.major
= dl
->major
;
2535 info
->disk
.minor
= dl
->minor
;
2536 info
->disk
.number
= dl
->index
;
2537 info
->disk
.raid_disk
= get_imsm_disk_slot(map_to_analyse
,
2541 info
->data_offset
= pba_of_lba0(map_to_analyse
);
2542 info
->component_size
= blocks_per_member(map_to_analyse
);
2544 /* check component size aligment
2546 component_size_alligment
=
2547 info
->component_size
% (info
->array
.chunk_size
/512);
2549 if (component_size_alligment
&&
2550 (info
->array
.level
!= 1) && (info
->array
.level
!= UnSet
)) {
2551 dprintf("imsm: reported component size alligned from %llu ",
2552 info
->component_size
);
2553 info
->component_size
-= component_size_alligment
;
2554 dprintf("to %llu (%i).\n",
2555 info
->component_size
, component_size_alligment
);
2558 memset(info
->uuid
, 0, sizeof(info
->uuid
));
2559 info
->recovery_start
= MaxSector
;
2561 info
->reshape_progress
= 0;
2562 info
->resync_start
= MaxSector
;
2563 if ((map_to_analyse
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
2565 imsm_reshape_blocks_arrays_changes(super
) == 0) {
2566 info
->resync_start
= 0;
2568 if (dev
->vol
.migr_state
) {
2569 switch (migr_type(dev
)) {
2572 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
2574 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
2576 info
->resync_start
= blocks_per_unit
* units
;
2579 case MIGR_GEN_MIGR
: {
2580 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
2582 __u64 units
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
2583 unsigned long long array_blocks
;
2586 if (__le32_to_cpu(migr_rec
->ascending_migr
) &&
2588 (__le32_to_cpu(migr_rec
->num_migr_units
)-1)) &&
2589 (super
->migr_rec
->rec_status
==
2590 __cpu_to_le32(UNIT_SRC_IN_CP_AREA
)))
2593 info
->reshape_progress
= blocks_per_unit
* units
;
2595 dprintf("IMSM: General Migration checkpoint : %llu "
2596 "(%llu) -> read reshape progress : %llu\n",
2597 (unsigned long long)units
,
2598 (unsigned long long)blocks_per_unit
,
2599 info
->reshape_progress
);
2601 used_disks
= imsm_num_data_members(dev
, MAP_1
);
2602 if (used_disks
> 0) {
2603 array_blocks
= blocks_per_member(map
) *
2605 /* round array size down to closest MB
2607 info
->custom_array_size
= (array_blocks
2608 >> SECT_PER_MB_SHIFT
)
2609 << SECT_PER_MB_SHIFT
;
2613 /* we could emulate the checkpointing of
2614 * 'sync_action=check' migrations, but for now
2615 * we just immediately complete them
2618 /* this is handled by container_content_imsm() */
2619 case MIGR_STATE_CHANGE
:
2620 /* FIXME handle other migrations */
2622 /* we are not dirty, so... */
2623 info
->resync_start
= MaxSector
;
2627 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
2628 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
2630 info
->array
.major_version
= -1;
2631 info
->array
.minor_version
= -2;
2632 devname
= devnum2devname(st
->container_dev
);
2633 *info
->text_version
= '\0';
2635 sprintf(info
->text_version
, "/%s/%d", devname
, info
->container_member
);
2637 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
2638 uuid_from_super_imsm(st
, info
->uuid
);
2642 for (i
=0; i
<map_disks
; i
++) {
2644 if (i
< info
->array
.raid_disks
) {
2645 struct imsm_disk
*dsk
;
2646 j
= get_imsm_disk_idx(dev
, i
, MAP_X
);
2647 dsk
= get_imsm_disk(super
, j
);
2648 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
2655 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
2656 int failed
, int look_in_map
);
2658 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
2663 static void manage_second_map(struct intel_super
*super
, struct imsm_dev
*dev
)
2665 if (is_gen_migration(dev
)) {
2668 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
2670 failed
= imsm_count_failed(super
, dev
, MAP_1
);
2671 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_1
);
2672 if (map2
->map_state
!= map_state
) {
2673 map2
->map_state
= map_state
;
2674 super
->updates_pending
++;
2680 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
2684 for (d
= super
->missing
; d
; d
= d
->next
)
2685 if (d
->index
== index
)
2690 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
2692 struct intel_super
*super
= st
->sb
;
2693 struct imsm_disk
*disk
;
2694 int map_disks
= info
->array
.raid_disks
;
2695 int max_enough
= -1;
2697 struct imsm_super
*mpb
;
2699 if (super
->current_vol
>= 0) {
2700 getinfo_super_imsm_volume(st
, info
, map
);
2703 memset(info
, 0, sizeof(*info
));
2705 /* Set raid_disks to zero so that Assemble will always pull in valid
2708 info
->array
.raid_disks
= 0;
2709 info
->array
.level
= LEVEL_CONTAINER
;
2710 info
->array
.layout
= 0;
2711 info
->array
.md_minor
= -1;
2712 info
->array
.ctime
= 0; /* N/A for imsm */
2713 info
->array
.utime
= 0;
2714 info
->array
.chunk_size
= 0;
2716 info
->disk
.major
= 0;
2717 info
->disk
.minor
= 0;
2718 info
->disk
.raid_disk
= -1;
2719 info
->reshape_active
= 0;
2720 info
->array
.major_version
= -1;
2721 info
->array
.minor_version
= -2;
2722 strcpy(info
->text_version
, "imsm");
2723 info
->safe_mode_delay
= 0;
2724 info
->disk
.number
= -1;
2725 info
->disk
.state
= 0;
2727 info
->recovery_start
= MaxSector
;
2728 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
2730 /* do we have the all the insync disks that we expect? */
2731 mpb
= super
->anchor
;
2733 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
2734 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
2735 int failed
, enough
, j
, missing
= 0;
2736 struct imsm_map
*map
;
2739 failed
= imsm_count_failed(super
, dev
, MAP_0
);
2740 state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
2741 map
= get_imsm_map(dev
, MAP_0
);
2743 /* any newly missing disks?
2744 * (catches single-degraded vs double-degraded)
2746 for (j
= 0; j
< map
->num_members
; j
++) {
2747 __u32 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
2748 __u32 idx
= ord_to_idx(ord
);
2750 if (!(ord
& IMSM_ORD_REBUILD
) &&
2751 get_imsm_missing(super
, idx
)) {
2757 if (state
== IMSM_T_STATE_FAILED
)
2759 else if (state
== IMSM_T_STATE_DEGRADED
&&
2760 (state
!= map
->map_state
|| missing
))
2762 else /* we're normal, or already degraded */
2764 if (is_gen_migration(dev
) && missing
) {
2765 /* during general migration we need all disks
2766 * that process is running on.
2767 * No new missing disk is allowed.
2771 /* no more checks necessary
2775 /* in the missing/failed disk case check to see
2776 * if at least one array is runnable
2778 max_enough
= max(max_enough
, enough
);
2780 dprintf("%s: enough: %d\n", __func__
, max_enough
);
2781 info
->container_enough
= max_enough
;
2784 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
2786 disk
= &super
->disks
->disk
;
2787 info
->data_offset
= total_blocks(&super
->disks
->disk
) - reserved
;
2788 info
->component_size
= reserved
;
2789 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
2790 /* we don't change info->disk.raid_disk here because
2791 * this state will be finalized in mdmon after we have
2792 * found the 'most fresh' version of the metadata
2794 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
2795 info
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
2798 /* only call uuid_from_super_imsm when this disk is part of a populated container,
2799 * ->compare_super may have updated the 'num_raid_devs' field for spares
2801 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
2802 uuid_from_super_imsm(st
, info
->uuid
);
2804 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
2806 /* I don't know how to compute 'map' on imsm, so use safe default */
2809 for (i
= 0; i
< map_disks
; i
++)
2815 /* allocates memory and fills disk in mdinfo structure
2816 * for each disk in array */
2817 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
2819 struct mdinfo
*mddev
= NULL
;
2820 struct intel_super
*super
= st
->sb
;
2821 struct imsm_disk
*disk
;
2824 if (!super
|| !super
->disks
)
2827 mddev
= malloc(sizeof(*mddev
));
2829 fprintf(stderr
, Name
": Failed to allocate memory.\n");
2832 memset(mddev
, 0, sizeof(*mddev
));
2836 tmp
= malloc(sizeof(*tmp
));
2838 fprintf(stderr
, Name
": Failed to allocate memory.\n");
2843 memset(tmp
, 0, sizeof(*tmp
));
2845 tmp
->next
= mddev
->devs
;
2847 tmp
->disk
.number
= count
++;
2848 tmp
->disk
.major
= dl
->major
;
2849 tmp
->disk
.minor
= dl
->minor
;
2850 tmp
->disk
.state
= is_configured(disk
) ?
2851 (1 << MD_DISK_ACTIVE
) : 0;
2852 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
2853 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
2854 tmp
->disk
.raid_disk
= -1;
2860 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
2861 char *update
, char *devname
, int verbose
,
2862 int uuid_set
, char *homehost
)
2864 /* For 'assemble' and 'force' we need to return non-zero if any
2865 * change was made. For others, the return value is ignored.
2866 * Update options are:
2867 * force-one : This device looks a bit old but needs to be included,
2868 * update age info appropriately.
2869 * assemble: clear any 'faulty' flag to allow this device to
2871 * force-array: Array is degraded but being forced, mark it clean
2872 * if that will be needed to assemble it.
2874 * newdev: not used ????
2875 * grow: Array has gained a new device - this is currently for
2877 * resync: mark as dirty so a resync will happen.
2878 * name: update the name - preserving the homehost
2879 * uuid: Change the uuid of the array to match watch is given
2881 * Following are not relevant for this imsm:
2882 * sparc2.2 : update from old dodgey metadata
2883 * super-minor: change the preferred_minor number
2884 * summaries: update redundant counters.
2885 * homehost: update the recorded homehost
2886 * _reshape_progress: record new reshape_progress position.
2889 struct intel_super
*super
= st
->sb
;
2890 struct imsm_super
*mpb
;
2892 /* we can only update container info */
2893 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
2896 mpb
= super
->anchor
;
2898 if (strcmp(update
, "uuid") == 0) {
2899 /* We take this to mean that the family_num should be updated.
2900 * However that is much smaller than the uuid so we cannot really
2901 * allow an explicit uuid to be given. And it is hard to reliably
2903 * So if !uuid_set we know the current uuid is random and just used
2904 * the first 'int' and copy it to the other 3 positions.
2905 * Otherwise we require the 4 'int's to be the same as would be the
2906 * case if we are using a random uuid. So an explicit uuid will be
2907 * accepted as long as all for ints are the same... which shouldn't hurt
2910 info
->uuid
[1] = info
->uuid
[2] = info
->uuid
[3] = info
->uuid
[0];
2913 if (info
->uuid
[0] != info
->uuid
[1] ||
2914 info
->uuid
[1] != info
->uuid
[2] ||
2915 info
->uuid
[2] != info
->uuid
[3])
2921 mpb
->orig_family_num
= info
->uuid
[0];
2922 } else if (strcmp(update
, "assemble") == 0)
2927 /* successful update? recompute checksum */
2929 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
2934 static size_t disks_to_mpb_size(int disks
)
2938 size
= sizeof(struct imsm_super
);
2939 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
2940 size
+= 2 * sizeof(struct imsm_dev
);
2941 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
2942 size
+= (4 - 2) * sizeof(struct imsm_map
);
2943 /* 4 possible disk_ord_tbl's */
2944 size
+= 4 * (disks
- 1) * sizeof(__u32
);
2949 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
)
2951 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
2954 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
2957 static void free_devlist(struct intel_super
*super
)
2959 struct intel_dev
*dv
;
2961 while (super
->devlist
) {
2962 dv
= super
->devlist
->next
;
2963 free(super
->devlist
->dev
);
2964 free(super
->devlist
);
2965 super
->devlist
= dv
;
2969 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
2971 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
2974 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
)
2978 * 0 same, or first was empty, and second was copied
2979 * 1 second had wrong number
2981 * 3 wrong other info
2983 struct intel_super
*first
= st
->sb
;
2984 struct intel_super
*sec
= tst
->sb
;
2991 /* in platform dependent environment test if the disks
2992 * use the same Intel hba
2994 if (!check_env("IMSM_NO_PLATFORM")) {
2995 if (!first
->hba
|| !sec
->hba
||
2996 (first
->hba
->type
!= sec
->hba
->type
)) {
2998 "HBAs of devices does not match %s != %s\n",
2999 first
->hba
? get_sys_dev_type(first
->hba
->type
) : NULL
,
3000 sec
->hba
? get_sys_dev_type(sec
->hba
->type
) : NULL
);
3005 /* if an anchor does not have num_raid_devs set then it is a free
3008 if (first
->anchor
->num_raid_devs
> 0 &&
3009 sec
->anchor
->num_raid_devs
> 0) {
3010 /* Determine if these disks might ever have been
3011 * related. Further disambiguation can only take place
3012 * in load_super_imsm_all
3014 __u32 first_family
= first
->anchor
->orig_family_num
;
3015 __u32 sec_family
= sec
->anchor
->orig_family_num
;
3017 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
3018 MAX_SIGNATURE_LENGTH
) != 0)
3021 if (first_family
== 0)
3022 first_family
= first
->anchor
->family_num
;
3023 if (sec_family
== 0)
3024 sec_family
= sec
->anchor
->family_num
;
3026 if (first_family
!= sec_family
)
3032 /* if 'first' is a spare promote it to a populated mpb with sec's
3035 if (first
->anchor
->num_raid_devs
== 0 &&
3036 sec
->anchor
->num_raid_devs
> 0) {
3038 struct intel_dev
*dv
;
3039 struct imsm_dev
*dev
;
3041 /* we need to copy raid device info from sec if an allocation
3042 * fails here we don't associate the spare
3044 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++) {
3045 dv
= malloc(sizeof(*dv
));
3048 dev
= malloc(sizeof_imsm_dev(get_imsm_dev(sec
, i
), 1));
3055 dv
->next
= first
->devlist
;
3056 first
->devlist
= dv
;
3058 if (i
< sec
->anchor
->num_raid_devs
) {
3059 /* allocation failure */
3060 free_devlist(first
);
3061 fprintf(stderr
, "imsm: failed to associate spare\n");
3064 first
->anchor
->num_raid_devs
= sec
->anchor
->num_raid_devs
;
3065 first
->anchor
->orig_family_num
= sec
->anchor
->orig_family_num
;
3066 first
->anchor
->family_num
= sec
->anchor
->family_num
;
3067 memcpy(first
->anchor
->sig
, sec
->anchor
->sig
, MAX_SIGNATURE_LENGTH
);
3068 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++)
3069 imsm_copy_dev(get_imsm_dev(first
, i
), get_imsm_dev(sec
, i
));
3075 static void fd2devname(int fd
, char *name
)
3079 char dname
[PATH_MAX
];
3084 if (fstat(fd
, &st
) != 0)
3086 sprintf(path
, "/sys/dev/block/%d:%d",
3087 major(st
.st_rdev
), minor(st
.st_rdev
));
3089 rv
= readlink(path
, dname
, sizeof(dname
)-1);
3094 nm
= strrchr(dname
, '/');
3097 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
3101 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
3103 static int imsm_read_serial(int fd
, char *devname
,
3104 __u8 serial
[MAX_RAID_SERIAL_LEN
])
3106 unsigned char scsi_serial
[255];
3115 memset(scsi_serial
, 0, sizeof(scsi_serial
));
3117 rv
= scsi_get_serial(fd
, scsi_serial
, sizeof(scsi_serial
));
3119 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
3120 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3121 fd2devname(fd
, (char *) serial
);
3128 Name
": Failed to retrieve serial for %s\n",
3133 rsp_len
= scsi_serial
[3];
3137 Name
": Failed to retrieve serial for %s\n",
3141 rsp_buf
= (char *) &scsi_serial
[4];
3143 /* trim all whitespace and non-printable characters and convert
3146 for (i
= 0, dest
= rsp_buf
; i
< rsp_len
; i
++) {
3149 /* ':' is reserved for use in placeholder serial
3150 * numbers for missing disks
3158 len
= dest
- rsp_buf
;
3161 /* truncate leading characters */
3162 if (len
> MAX_RAID_SERIAL_LEN
) {
3163 dest
+= len
- MAX_RAID_SERIAL_LEN
;
3164 len
= MAX_RAID_SERIAL_LEN
;
3167 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3168 memcpy(serial
, dest
, len
);
3173 static int serialcmp(__u8
*s1
, __u8
*s2
)
3175 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
3178 static void serialcpy(__u8
*dest
, __u8
*src
)
3180 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
3183 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
3187 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3188 if (serialcmp(dl
->serial
, serial
) == 0)
3194 static struct imsm_disk
*
3195 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
3199 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3200 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3202 if (serialcmp(disk
->serial
, serial
) == 0) {
3213 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3215 struct imsm_disk
*disk
;
3220 __u8 serial
[MAX_RAID_SERIAL_LEN
];
3222 rv
= imsm_read_serial(fd
, devname
, serial
);
3227 dl
= calloc(1, sizeof(*dl
));
3231 Name
": failed to allocate disk buffer for %s\n",
3237 dl
->major
= major(stb
.st_rdev
);
3238 dl
->minor
= minor(stb
.st_rdev
);
3239 dl
->next
= super
->disks
;
3240 dl
->fd
= keep_fd
? fd
: -1;
3241 assert(super
->disks
== NULL
);
3243 serialcpy(dl
->serial
, serial
);
3246 fd2devname(fd
, name
);
3248 dl
->devname
= strdup(devname
);
3250 dl
->devname
= strdup(name
);
3252 /* look up this disk's index in the current anchor */
3253 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
3256 /* only set index on disks that are a member of a
3257 * populated contianer, i.e. one with raid_devs
3259 if (is_failed(&dl
->disk
))
3261 else if (is_spare(&dl
->disk
))
3269 /* When migrating map0 contains the 'destination' state while map1
3270 * contains the current state. When not migrating map0 contains the
3271 * current state. This routine assumes that map[0].map_state is set to
3272 * the current array state before being called.
3274 * Migration is indicated by one of the following states
3275 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
3276 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
3277 * map1state=unitialized)
3278 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
3280 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
3281 * map1state=degraded)
3282 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
3285 static void migrate(struct imsm_dev
*dev
, struct intel_super
*super
,
3286 __u8 to_state
, int migr_type
)
3288 struct imsm_map
*dest
;
3289 struct imsm_map
*src
= get_imsm_map(dev
, MAP_0
);
3291 dev
->vol
.migr_state
= 1;
3292 set_migr_type(dev
, migr_type
);
3293 dev
->vol
.curr_migr_unit
= 0;
3294 dest
= get_imsm_map(dev
, MAP_1
);
3296 /* duplicate and then set the target end state in map[0] */
3297 memcpy(dest
, src
, sizeof_imsm_map(src
));
3298 if ((migr_type
== MIGR_REBUILD
) ||
3299 (migr_type
== MIGR_GEN_MIGR
)) {
3303 for (i
= 0; i
< src
->num_members
; i
++) {
3304 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
3305 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
3309 if (migr_type
== MIGR_GEN_MIGR
)
3310 /* Clear migration record */
3311 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
3313 src
->map_state
= to_state
;
3316 static void end_migration(struct imsm_dev
*dev
, struct intel_super
*super
,
3319 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3320 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
== 0 ?
3324 /* merge any IMSM_ORD_REBUILD bits that were not successfully
3325 * completed in the last migration.
3327 * FIXME add support for raid-level-migration
3329 if ((map_state
!= map
->map_state
) && (is_gen_migration(dev
) == 0) &&
3330 (prev
->map_state
!= IMSM_T_STATE_UNINITIALIZED
)) {
3331 /* when final map state is other than expected
3332 * merge maps (not for migration)
3336 for (i
= 0; i
< prev
->num_members
; i
++)
3337 for (j
= 0; j
< map
->num_members
; j
++)
3338 /* during online capacity expansion
3339 * disks position can be changed
3340 * if takeover is used
3342 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
3343 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
3344 map
->disk_ord_tbl
[j
] |=
3345 prev
->disk_ord_tbl
[i
];
3348 failed
= imsm_count_failed(super
, dev
, MAP_0
);
3349 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
3352 dev
->vol
.migr_state
= 0;
3353 set_migr_type(dev
, 0);
3354 dev
->vol
.curr_migr_unit
= 0;
3355 map
->map_state
= map_state
;
3359 static int parse_raid_devices(struct intel_super
*super
)
3362 struct imsm_dev
*dev_new
;
3363 size_t len
, len_migr
;
3365 size_t space_needed
= 0;
3366 struct imsm_super
*mpb
= super
->anchor
;
3368 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3369 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3370 struct intel_dev
*dv
;
3372 len
= sizeof_imsm_dev(dev_iter
, 0);
3373 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
3375 space_needed
+= len_migr
- len
;
3377 dv
= malloc(sizeof(*dv
));
3380 if (max_len
< len_migr
)
3382 if (max_len
> len_migr
)
3383 space_needed
+= max_len
- len_migr
;
3384 dev_new
= malloc(max_len
);
3389 imsm_copy_dev(dev_new
, dev_iter
);
3392 dv
->next
= super
->devlist
;
3393 super
->devlist
= dv
;
3396 /* ensure that super->buf is large enough when all raid devices
3399 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
3402 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
, 512);
3403 if (posix_memalign(&buf
, 512, len
) != 0)
3406 memcpy(buf
, super
->buf
, super
->len
);
3407 memset(buf
+ super
->len
, 0, len
- super
->len
);
3416 /* retrieve a pointer to the bbm log which starts after all raid devices */
3417 struct bbm_log
*__get_imsm_bbm_log(struct imsm_super
*mpb
)
3421 if (__le32_to_cpu(mpb
->bbm_log_size
)) {
3423 ptr
+= mpb
->mpb_size
- __le32_to_cpu(mpb
->bbm_log_size
);
3429 /*******************************************************************************
3430 * Function: check_mpb_migr_compatibility
3431 * Description: Function checks for unsupported migration features:
3432 * - migration optimization area (pba_of_lba0)
3433 * - descending reshape (ascending_migr)
3435 * super : imsm metadata information
3437 * 0 : migration is compatible
3438 * -1 : migration is not compatible
3439 ******************************************************************************/
3440 int check_mpb_migr_compatibility(struct intel_super
*super
)
3442 struct imsm_map
*map0
, *map1
;
3443 struct migr_record
*migr_rec
= super
->migr_rec
;
3446 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3447 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3450 dev_iter
->vol
.migr_state
== 1 &&
3451 dev_iter
->vol
.migr_type
== MIGR_GEN_MIGR
) {
3452 /* This device is migrating */
3453 map0
= get_imsm_map(dev_iter
, MAP_0
);
3454 map1
= get_imsm_map(dev_iter
, MAP_1
);
3455 if (pba_of_lba0(map0
) != pba_of_lba0(map1
))
3456 /* migration optimization area was used */
3458 if (migr_rec
->ascending_migr
== 0
3459 && migr_rec
->dest_depth_per_unit
> 0)
3460 /* descending reshape not supported yet */
3467 static void __free_imsm(struct intel_super
*super
, int free_disks
);
3469 /* load_imsm_mpb - read matrix metadata
3470 * allocates super->mpb to be freed by free_imsm
3472 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
3474 unsigned long long dsize
;
3475 unsigned long long sectors
;
3477 struct imsm_super
*anchor
;
3480 get_dev_size(fd
, NULL
, &dsize
);
3484 Name
": %s: device to small for imsm\n",
3489 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0) {
3491 fprintf(stderr
, Name
3492 ": Cannot seek to anchor block on %s: %s\n",
3493 devname
, strerror(errno
));
3497 if (posix_memalign((void**)&anchor
, 512, 512) != 0) {
3500 Name
": Failed to allocate imsm anchor buffer"
3501 " on %s\n", devname
);
3504 if (read(fd
, anchor
, 512) != 512) {
3507 Name
": Cannot read anchor block on %s: %s\n",
3508 devname
, strerror(errno
));
3513 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
3516 Name
": no IMSM anchor on %s\n", devname
);
3521 __free_imsm(super
, 0);
3522 /* reload capability and hba */
3524 /* capability and hba must be updated with new super allocation */
3525 find_intel_hba_capability(fd
, super
, devname
);
3526 super
->len
= ROUND_UP(anchor
->mpb_size
, 512);
3527 if (posix_memalign(&super
->buf
, 512, super
->len
) != 0) {
3530 Name
": unable to allocate %zu byte mpb buffer\n",
3535 memcpy(super
->buf
, anchor
, 512);
3537 sectors
= mpb_sectors(anchor
) - 1;
3540 if (posix_memalign(&super
->migr_rec_buf
, 512, MIGR_REC_BUF_SIZE
) != 0) {
3541 fprintf(stderr
, Name
3542 ": %s could not allocate migr_rec buffer\n", __func__
);
3546 super
->clean_migration_record_by_mdmon
= 0;
3549 check_sum
= __gen_imsm_checksum(super
->anchor
);
3550 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
3553 Name
": IMSM checksum %x != %x on %s\n",
3555 __le32_to_cpu(super
->anchor
->check_sum
),
3563 /* read the extended mpb */
3564 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0) {
3567 Name
": Cannot seek to extended mpb on %s: %s\n",
3568 devname
, strerror(errno
));
3572 if ((unsigned)read(fd
, super
->buf
+ 512, super
->len
- 512) != super
->len
- 512) {
3575 Name
": Cannot read extended mpb on %s: %s\n",
3576 devname
, strerror(errno
));
3580 check_sum
= __gen_imsm_checksum(super
->anchor
);
3581 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
3584 Name
": IMSM checksum %x != %x on %s\n",
3585 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
3590 /* FIXME the BBM log is disk specific so we cannot use this global
3591 * buffer for all disks. Ok for now since we only look at the global
3592 * bbm_log_size parameter to gate assembly
3594 super
->bbm_log
= __get_imsm_bbm_log(super
->anchor
);
3599 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
);
3601 /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
3602 static void clear_hi(struct intel_super
*super
)
3604 struct imsm_super
*mpb
= super
->anchor
;
3606 if (mpb
->attributes
& MPB_ATTRIB_2TB_DISK
)
3608 for (i
= 0; i
< mpb
->num_disks
; ++i
) {
3609 struct imsm_disk
*disk
= &mpb
->disk
[i
];
3610 disk
->total_blocks_hi
= 0;
3612 for (i
= 0; i
< mpb
->num_raid_devs
; ++i
) {
3613 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3616 for (n
= 0; n
< 2; ++n
) {
3617 struct imsm_map
*map
= get_imsm_map(dev
, n
);
3620 map
->pba_of_lba0_hi
= 0;
3621 map
->blocks_per_member_hi
= 0;
3622 map
->num_data_stripes_hi
= 0;
3628 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3632 err
= load_imsm_mpb(fd
, super
, devname
);
3635 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
3638 err
= parse_raid_devices(super
);
3643 static void __free_imsm_disk(struct dl
*d
)
3655 static void free_imsm_disks(struct intel_super
*super
)
3659 while (super
->disks
) {
3661 super
->disks
= d
->next
;
3662 __free_imsm_disk(d
);
3664 while (super
->disk_mgmt_list
) {
3665 d
= super
->disk_mgmt_list
;
3666 super
->disk_mgmt_list
= d
->next
;
3667 __free_imsm_disk(d
);
3669 while (super
->missing
) {
3671 super
->missing
= d
->next
;
3672 __free_imsm_disk(d
);
3677 /* free all the pieces hanging off of a super pointer */
3678 static void __free_imsm(struct intel_super
*super
, int free_disks
)
3680 struct intel_hba
*elem
, *next
;
3686 /* unlink capability description */
3688 if (super
->migr_rec_buf
) {
3689 free(super
->migr_rec_buf
);
3690 super
->migr_rec_buf
= NULL
;
3693 free_imsm_disks(super
);
3694 free_devlist(super
);
3698 free((void *)elem
->path
);
3706 static void free_imsm(struct intel_super
*super
)
3708 __free_imsm(super
, 1);
3712 static void free_super_imsm(struct supertype
*st
)
3714 struct intel_super
*super
= st
->sb
;
3723 static struct intel_super
*alloc_super(void)
3725 struct intel_super
*super
= malloc(sizeof(*super
));
3728 memset(super
, 0, sizeof(*super
));
3729 super
->current_vol
= -1;
3730 super
->create_offset
= ~((unsigned long long) 0);
3736 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
3738 static int find_intel_hba_capability(int fd
, struct intel_super
*super
, char *devname
)
3740 struct sys_dev
*hba_name
;
3743 if ((fd
< 0) || check_env("IMSM_NO_PLATFORM")) {
3748 hba_name
= find_disk_attached_hba(fd
, NULL
);
3752 Name
": %s is not attached to Intel(R) RAID controller.\n",
3756 rv
= attach_hba_to_super(super
, hba_name
);
3759 struct intel_hba
*hba
= super
->hba
;
3761 fprintf(stderr
, Name
": %s is attached to Intel(R) %s RAID "
3762 "controller (%s),\n"
3763 " but the container is assigned to Intel(R) "
3764 "%s RAID controller (",
3767 hba_name
->pci_id
? : "Err!",
3768 get_sys_dev_type(hba_name
->type
));
3771 fprintf(stderr
, "%s", hba
->pci_id
? : "Err!");
3773 fprintf(stderr
, ", ");
3777 fprintf(stderr
, ").\n"
3778 " Mixing devices attached to different controllers "
3779 "is not allowed.\n");
3781 free_sys_dev(&hba_name
);
3784 super
->orom
= find_imsm_capability(hba_name
->type
);
3785 free_sys_dev(&hba_name
);
3791 /* find_missing - helper routine for load_super_imsm_all that identifies
3792 * disks that have disappeared from the system. This routine relies on
3793 * the mpb being uptodate, which it is at load time.
3795 static int find_missing(struct intel_super
*super
)
3798 struct imsm_super
*mpb
= super
->anchor
;
3800 struct imsm_disk
*disk
;
3802 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3803 disk
= __get_imsm_disk(mpb
, i
);
3804 dl
= serial_to_dl(disk
->serial
, super
);
3808 dl
= malloc(sizeof(*dl
));
3814 dl
->devname
= strdup("missing");
3816 serialcpy(dl
->serial
, disk
->serial
);
3819 dl
->next
= super
->missing
;
3820 super
->missing
= dl
;
3827 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
3829 struct intel_disk
*idisk
= disk_list
;
3832 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
3834 idisk
= idisk
->next
;
3840 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
3841 struct intel_super
*super
,
3842 struct intel_disk
**disk_list
)
3844 struct imsm_disk
*d
= &super
->disks
->disk
;
3845 struct imsm_super
*mpb
= super
->anchor
;
3848 for (i
= 0; i
< tbl_size
; i
++) {
3849 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
3850 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
3852 if (tbl_mpb
->family_num
== mpb
->family_num
) {
3853 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
3854 dprintf("%s: mpb from %d:%d matches %d:%d\n",
3855 __func__
, super
->disks
->major
,
3856 super
->disks
->minor
,
3857 table
[i
]->disks
->major
,
3858 table
[i
]->disks
->minor
);
3862 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
3863 is_configured(d
) == is_configured(tbl_d
)) &&
3864 tbl_mpb
->generation_num
< mpb
->generation_num
) {
3865 /* current version of the mpb is a
3866 * better candidate than the one in
3867 * super_table, but copy over "cross
3868 * generational" status
3870 struct intel_disk
*idisk
;
3872 dprintf("%s: mpb from %d:%d replaces %d:%d\n",
3873 __func__
, super
->disks
->major
,
3874 super
->disks
->minor
,
3875 table
[i
]->disks
->major
,
3876 table
[i
]->disks
->minor
);
3878 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
3879 if (idisk
&& is_failed(&idisk
->disk
))
3880 tbl_d
->status
|= FAILED_DISK
;
3883 struct intel_disk
*idisk
;
3884 struct imsm_disk
*disk
;
3886 /* tbl_mpb is more up to date, but copy
3887 * over cross generational status before
3890 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
3891 if (disk
&& is_failed(disk
))
3892 d
->status
|= FAILED_DISK
;
3894 idisk
= disk_list_get(d
->serial
, *disk_list
);
3897 if (disk
&& is_configured(disk
))
3898 idisk
->disk
.status
|= CONFIGURED_DISK
;
3901 dprintf("%s: mpb from %d:%d prefer %d:%d\n",
3902 __func__
, super
->disks
->major
,
3903 super
->disks
->minor
,
3904 table
[i
]->disks
->major
,
3905 table
[i
]->disks
->minor
);
3913 table
[tbl_size
++] = super
;
3917 /* update/extend the merged list of imsm_disk records */
3918 for (j
= 0; j
< mpb
->num_disks
; j
++) {
3919 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
3920 struct intel_disk
*idisk
;
3922 idisk
= disk_list_get(disk
->serial
, *disk_list
);
3924 idisk
->disk
.status
|= disk
->status
;
3925 if (is_configured(&idisk
->disk
) ||
3926 is_failed(&idisk
->disk
))
3927 idisk
->disk
.status
&= ~(SPARE_DISK
);
3929 idisk
= calloc(1, sizeof(*idisk
));
3932 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
3933 idisk
->disk
= *disk
;
3934 idisk
->next
= *disk_list
;
3938 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
3945 static struct intel_super
*
3946 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
3949 struct imsm_super
*mpb
= super
->anchor
;
3953 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3954 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3955 struct intel_disk
*idisk
;
3957 idisk
= disk_list_get(disk
->serial
, disk_list
);
3959 if (idisk
->owner
== owner
||
3960 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
3963 dprintf("%s: '%.16s' owner %d != %d\n",
3964 __func__
, disk
->serial
, idisk
->owner
,
3967 dprintf("%s: unknown disk %x [%d]: %.16s\n",
3968 __func__
, __le32_to_cpu(mpb
->family_num
), i
,
3974 if (ok_count
== mpb
->num_disks
)
3979 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
3981 struct intel_super
*s
;
3983 for (s
= super_list
; s
; s
= s
->next
) {
3984 if (family_num
!= s
->anchor
->family_num
)
3986 fprintf(stderr
, "Conflict, offlining family %#x on '%s'\n",
3987 __le32_to_cpu(family_num
), s
->disks
->devname
);
3991 static struct intel_super
*
3992 imsm_thunderdome(struct intel_super
**super_list
, int len
)
3994 struct intel_super
*super_table
[len
];
3995 struct intel_disk
*disk_list
= NULL
;
3996 struct intel_super
*champion
, *spare
;
3997 struct intel_super
*s
, **del
;
4002 memset(super_table
, 0, sizeof(super_table
));
4003 for (s
= *super_list
; s
; s
= s
->next
)
4004 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
4006 for (i
= 0; i
< tbl_size
; i
++) {
4007 struct imsm_disk
*d
;
4008 struct intel_disk
*idisk
;
4009 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
4012 d
= &s
->disks
->disk
;
4014 /* 'd' must appear in merged disk list for its
4015 * configuration to be valid
4017 idisk
= disk_list_get(d
->serial
, disk_list
);
4018 if (idisk
&& idisk
->owner
== i
)
4019 s
= validate_members(s
, disk_list
, i
);
4024 dprintf("%s: marking family: %#x from %d:%d offline\n",
4025 __func__
, mpb
->family_num
,
4026 super_table
[i
]->disks
->major
,
4027 super_table
[i
]->disks
->minor
);
4031 /* This is where the mdadm implementation differs from the Windows
4032 * driver which has no strict concept of a container. We can only
4033 * assemble one family from a container, so when returning a prodigal
4034 * array member to this system the code will not be able to disambiguate
4035 * the container contents that should be assembled ("foreign" versus
4036 * "local"). It requires user intervention to set the orig_family_num
4037 * to a new value to establish a new container. The Windows driver in
4038 * this situation fixes up the volume name in place and manages the
4039 * foreign array as an independent entity.
4044 for (i
= 0; i
< tbl_size
; i
++) {
4045 struct intel_super
*tbl_ent
= super_table
[i
];
4051 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
4056 if (s
&& !is_spare
) {
4057 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
4059 } else if (!s
&& !is_spare
)
4072 fprintf(stderr
, "Chose family %#x on '%s', "
4073 "assemble conflicts to new container with '--update=uuid'\n",
4074 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
4076 /* collect all dl's onto 'champion', and update them to
4077 * champion's version of the status
4079 for (s
= *super_list
; s
; s
= s
->next
) {
4080 struct imsm_super
*mpb
= champion
->anchor
;
4081 struct dl
*dl
= s
->disks
;
4086 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4087 struct imsm_disk
*disk
;
4089 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
4092 /* only set index on disks that are a member of
4093 * a populated contianer, i.e. one with
4096 if (is_failed(&dl
->disk
))
4098 else if (is_spare(&dl
->disk
))
4104 if (i
>= mpb
->num_disks
) {
4105 struct intel_disk
*idisk
;
4107 idisk
= disk_list_get(dl
->serial
, disk_list
);
4108 if (idisk
&& is_spare(&idisk
->disk
) &&
4109 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
4117 dl
->next
= champion
->disks
;
4118 champion
->disks
= dl
;
4122 /* delete 'champion' from super_list */
4123 for (del
= super_list
; *del
; ) {
4124 if (*del
== champion
) {
4125 *del
= (*del
)->next
;
4128 del
= &(*del
)->next
;
4130 champion
->next
= NULL
;
4134 struct intel_disk
*idisk
= disk_list
;
4136 disk_list
= disk_list
->next
;
4145 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
);
4146 static int get_super_block(struct intel_super
**super_list
, int devnum
, char *devname
,
4147 int major
, int minor
, int keep_fd
);
4149 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4150 int *max
, int keep_fd
);
4153 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
4154 char *devname
, struct md_list
*devlist
,
4157 struct intel_super
*super_list
= NULL
;
4158 struct intel_super
*super
= NULL
;
4163 /* 'fd' is an opened container */
4164 err
= get_sra_super_block(fd
, &super_list
, devname
, &i
, keep_fd
);
4166 /* get super block from devlist devices */
4167 err
= get_devlist_super_block(devlist
, &super_list
, &i
, keep_fd
);
4170 /* all mpbs enter, maybe one leaves */
4171 super
= imsm_thunderdome(&super_list
, i
);
4177 if (find_missing(super
) != 0) {
4183 /* load migration record */
4184 err
= load_imsm_migr_rec(super
, NULL
);
4186 /* migration is in progress,
4187 * but migr_rec cannot be loaded,
4193 /* Check migration compatibility */
4194 if ((err
== 0) && (check_mpb_migr_compatibility(super
) != 0)) {
4195 fprintf(stderr
, Name
": Unsupported migration detected");
4197 fprintf(stderr
, " on %s\n", devname
);
4199 fprintf(stderr
, " (IMSM).\n");
4208 while (super_list
) {
4209 struct intel_super
*s
= super_list
;
4211 super_list
= super_list
->next
;
4221 st
->container_dev
= fd2devnum(fd
);
4223 st
->container_dev
= NoMdDev
;
4224 if (err
== 0 && st
->ss
== NULL
) {
4225 st
->ss
= &super_imsm
;
4226 st
->minor_version
= 0;
4227 st
->max_devs
= IMSM_MAX_DEVICES
;
4234 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4235 int *max
, int keep_fd
)
4237 struct md_list
*tmpdev
;
4241 for (i
= 0, tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
4242 if (tmpdev
->used
!= 1)
4244 if (tmpdev
->container
== 1) {
4246 int fd
= dev_open(tmpdev
->devname
, O_RDONLY
|O_EXCL
);
4248 fprintf(stderr
, Name
": cannot open device %s: %s\n",
4249 tmpdev
->devname
, strerror(errno
));
4253 err
= get_sra_super_block(fd
, super_list
,
4254 tmpdev
->devname
, &lmax
,
4263 int major
= major(tmpdev
->st_rdev
);
4264 int minor
= minor(tmpdev
->st_rdev
);
4265 err
= get_super_block(super_list
,
4282 static int get_super_block(struct intel_super
**super_list
, int devnum
, char *devname
,
4283 int major
, int minor
, int keep_fd
)
4285 struct intel_super
*s
= NULL
;
4298 sprintf(nm
, "%d:%d", major
, minor
);
4299 dfd
= dev_open(nm
, O_RDWR
);
4305 rv
= find_intel_hba_capability(dfd
, s
, devname
);
4306 /* no orom/efi or non-intel hba of the disk */
4312 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4314 /* retry the load if we might have raced against mdmon */
4315 if (err
== 3 && (devnum
!= -1) && mdmon_running(devnum
))
4316 for (retry
= 0; retry
< 3; retry
++) {
4318 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4324 s
->next
= *super_list
;
4332 if ((dfd
>= 0) && (!keep_fd
))
4339 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
)
4346 sra
= sysfs_read(fd
, 0, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
4350 if (sra
->array
.major_version
!= -1 ||
4351 sra
->array
.minor_version
!= -2 ||
4352 strcmp(sra
->text_version
, "imsm") != 0) {
4357 devnum
= fd2devnum(fd
);
4358 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
4359 if (get_super_block(super_list
, devnum
, devname
,
4360 sd
->disk
.major
, sd
->disk
.minor
, keep_fd
) != 0) {
4371 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
4373 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
, NULL
, 1);
4377 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
4379 struct intel_super
*super
;
4382 if (test_partition(fd
))
4383 /* IMSM not allowed on partitions */
4386 free_super_imsm(st
);
4388 super
= alloc_super();
4391 Name
": malloc of %zu failed.\n",
4395 /* Load hba and capabilities if they exist.
4396 * But do not preclude loading metadata in case capabilities or hba are
4397 * non-compliant and ignore_hw_compat is set.
4399 rv
= find_intel_hba_capability(fd
, super
, devname
);
4400 /* no orom/efi or non-intel hba of the disk */
4401 if ((rv
!= 0) && (st
->ignore_hw_compat
== 0)) {
4404 Name
": No OROM/EFI properties for %s\n", devname
);
4408 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
4413 Name
": Failed to load all information "
4414 "sections on %s\n", devname
);
4420 if (st
->ss
== NULL
) {
4421 st
->ss
= &super_imsm
;
4422 st
->minor_version
= 0;
4423 st
->max_devs
= IMSM_MAX_DEVICES
;
4426 /* load migration record */
4427 if (load_imsm_migr_rec(super
, NULL
) == 0) {
4428 /* Check for unsupported migration features */
4429 if (check_mpb_migr_compatibility(super
) != 0) {
4431 Name
": Unsupported migration detected");
4433 fprintf(stderr
, " on %s\n", devname
);
4435 fprintf(stderr
, " (IMSM).\n");
4443 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
4445 if (info
->level
== 1)
4447 return info
->chunk_size
>> 9;
4450 static unsigned long long info_to_blocks_per_member(mdu_array_info_t
*info
,
4451 unsigned long long size
)
4453 if (info
->level
== 1)
4456 return (size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
4459 static void imsm_update_version_info(struct intel_super
*super
)
4461 /* update the version and attributes */
4462 struct imsm_super
*mpb
= super
->anchor
;
4464 struct imsm_dev
*dev
;
4465 struct imsm_map
*map
;
4468 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4469 dev
= get_imsm_dev(super
, i
);
4470 map
= get_imsm_map(dev
, MAP_0
);
4471 if (__le32_to_cpu(dev
->size_high
) > 0)
4472 mpb
->attributes
|= MPB_ATTRIB_2TB
;
4474 /* FIXME detect when an array spans a port multiplier */
4476 mpb
->attributes
|= MPB_ATTRIB_PM
;
4479 if (mpb
->num_raid_devs
> 1 ||
4480 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
4481 version
= MPB_VERSION_ATTRIBS
;
4482 switch (get_imsm_raid_level(map
)) {
4483 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
4484 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
4485 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
4486 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
4489 if (map
->num_members
>= 5)
4490 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
4491 else if (dev
->status
== DEV_CLONE_N_GO
)
4492 version
= MPB_VERSION_CNG
;
4493 else if (get_imsm_raid_level(map
) == 5)
4494 version
= MPB_VERSION_RAID5
;
4495 else if (map
->num_members
>= 3)
4496 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
4497 else if (get_imsm_raid_level(map
) == 1)
4498 version
= MPB_VERSION_RAID1
;
4500 version
= MPB_VERSION_RAID0
;
4502 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
4506 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
4508 struct imsm_super
*mpb
= super
->anchor
;
4509 char *reason
= NULL
;
4512 if (strlen(name
) > MAX_RAID_SERIAL_LEN
)
4513 reason
= "must be 16 characters or less";
4515 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4516 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
4518 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
4519 reason
= "already exists";
4524 if (reason
&& !quiet
)
4525 fprintf(stderr
, Name
": imsm volume name %s\n", reason
);
4530 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
4531 unsigned long long size
, char *name
,
4532 char *homehost
, int *uuid
)
4534 /* We are creating a volume inside a pre-existing container.
4535 * so st->sb is already set.
4537 struct intel_super
*super
= st
->sb
;
4538 struct imsm_super
*mpb
= super
->anchor
;
4539 struct intel_dev
*dv
;
4540 struct imsm_dev
*dev
;
4541 struct imsm_vol
*vol
;
4542 struct imsm_map
*map
;
4543 int idx
= mpb
->num_raid_devs
;
4545 unsigned long long array_blocks
;
4546 size_t size_old
, size_new
;
4547 unsigned long long num_data_stripes
;
4549 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
4550 fprintf(stderr
, Name
": This imsm-container already has the "
4551 "maximum of %d volumes\n", super
->orom
->vpa
);
4555 /* ensure the mpb is large enough for the new data */
4556 size_old
= __le32_to_cpu(mpb
->mpb_size
);
4557 size_new
= disks_to_mpb_size(info
->nr_disks
);
4558 if (size_new
> size_old
) {
4560 size_t size_round
= ROUND_UP(size_new
, 512);
4562 if (posix_memalign(&mpb_new
, 512, size_round
) != 0) {
4563 fprintf(stderr
, Name
": could not allocate new mpb\n");
4566 if (posix_memalign(&super
->migr_rec_buf
, 512,
4567 MIGR_REC_BUF_SIZE
) != 0) {
4568 fprintf(stderr
, Name
4569 ": %s could not allocate migr_rec buffer\n",
4576 memcpy(mpb_new
, mpb
, size_old
);
4579 super
->anchor
= mpb_new
;
4580 mpb
->mpb_size
= __cpu_to_le32(size_new
);
4581 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
4583 super
->current_vol
= idx
;
4585 /* handle 'failed_disks' by either:
4586 * a) create dummy disk entries in the table if this the first
4587 * volume in the array. We add them here as this is the only
4588 * opportunity to add them. add_to_super_imsm_volume()
4589 * handles the non-failed disks and continues incrementing
4591 * b) validate that 'failed_disks' matches the current number
4592 * of missing disks if the container is populated
4594 if (super
->current_vol
== 0) {
4596 for (i
= 0; i
< info
->failed_disks
; i
++) {
4597 struct imsm_disk
*disk
;
4600 disk
= __get_imsm_disk(mpb
, i
);
4601 disk
->status
= CONFIGURED_DISK
| FAILED_DISK
;
4602 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
4603 snprintf((char *) disk
->serial
, MAX_RAID_SERIAL_LEN
,
4606 find_missing(super
);
4611 for (d
= super
->missing
; d
; d
= d
->next
)
4613 if (info
->failed_disks
> missing
) {
4614 fprintf(stderr
, Name
": unable to add 'missing' disk to container\n");
4619 if (!check_name(super
, name
, 0))
4621 dv
= malloc(sizeof(*dv
));
4623 fprintf(stderr
, Name
": failed to allocate device list entry\n");
4626 dev
= calloc(1, sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
4629 fprintf(stderr
, Name
": could not allocate raid device\n");
4633 strncpy((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
4634 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
4635 info
->layout
, info
->chunk_size
,
4637 /* round array size down to closest MB */
4638 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
4640 dev
->size_low
= __cpu_to_le32((__u32
) array_blocks
);
4641 dev
->size_high
= __cpu_to_le32((__u32
) (array_blocks
>> 32));
4642 dev
->status
= (DEV_READ_COALESCING
| DEV_WRITE_COALESCING
);
4644 vol
->migr_state
= 0;
4645 set_migr_type(dev
, MIGR_INIT
);
4646 vol
->dirty
= !info
->state
;
4647 vol
->curr_migr_unit
= 0;
4648 map
= get_imsm_map(dev
, MAP_0
);
4649 set_pba_of_lba0(map
, super
->create_offset
);
4650 set_blocks_per_member(map
, info_to_blocks_per_member(info
, size
));
4651 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
4652 map
->failed_disk_num
= ~0;
4653 if (info
->level
> 0)
4654 map
->map_state
= IMSM_T_STATE_UNINITIALIZED
;
4656 map
->map_state
= info
->failed_disks
? IMSM_T_STATE_FAILED
:
4657 IMSM_T_STATE_NORMAL
;
4660 if (info
->level
== 1 && info
->raid_disks
> 2) {
4663 fprintf(stderr
, Name
": imsm does not support more than 2 disks"
4664 "in a raid1 volume\n");
4668 map
->raid_level
= info
->level
;
4669 if (info
->level
== 10) {
4670 map
->raid_level
= 1;
4671 map
->num_domains
= info
->raid_disks
/ 2;
4672 } else if (info
->level
== 1)
4673 map
->num_domains
= info
->raid_disks
;
4675 map
->num_domains
= 1;
4677 /* info->size is only int so use the 'size' parameter instead */
4678 num_data_stripes
= (size
* 2) / info_to_blocks_per_strip(info
);
4679 num_data_stripes
/= map
->num_domains
;
4680 set_num_data_stripes(map
, num_data_stripes
);
4682 map
->num_members
= info
->raid_disks
;
4683 for (i
= 0; i
< map
->num_members
; i
++) {
4684 /* initialized in add_to_super */
4685 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
4687 mpb
->num_raid_devs
++;
4690 dv
->index
= super
->current_vol
;
4691 dv
->next
= super
->devlist
;
4692 super
->devlist
= dv
;
4694 imsm_update_version_info(super
);
4699 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
4700 unsigned long long size
, char *name
,
4701 char *homehost
, int *uuid
)
4703 /* This is primarily called by Create when creating a new array.
4704 * We will then get add_to_super called for each component, and then
4705 * write_init_super called to write it out to each device.
4706 * For IMSM, Create can create on fresh devices or on a pre-existing
4708 * To create on a pre-existing array a different method will be called.
4709 * This one is just for fresh drives.
4711 struct intel_super
*super
;
4712 struct imsm_super
*mpb
;
4717 return init_super_imsm_volume(st
, info
, size
, name
, homehost
, uuid
);
4720 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
4724 super
= alloc_super();
4725 if (super
&& posix_memalign(&super
->buf
, 512, mpb_size
) != 0) {
4730 fprintf(stderr
, Name
4731 ": %s could not allocate superblock\n", __func__
);
4734 if (posix_memalign(&super
->migr_rec_buf
, 512, MIGR_REC_BUF_SIZE
) != 0) {
4735 fprintf(stderr
, Name
4736 ": %s could not allocate migr_rec buffer\n", __func__
);
4741 memset(super
->buf
, 0, mpb_size
);
4743 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
4747 /* zeroing superblock */
4751 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
4753 version
= (char *) mpb
->sig
;
4754 strcpy(version
, MPB_SIGNATURE
);
4755 version
+= strlen(MPB_SIGNATURE
);
4756 strcpy(version
, MPB_VERSION_RAID0
);
4762 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
4763 int fd
, char *devname
)
4765 struct intel_super
*super
= st
->sb
;
4766 struct imsm_super
*mpb
= super
->anchor
;
4767 struct imsm_disk
*_disk
;
4768 struct imsm_dev
*dev
;
4769 struct imsm_map
*map
;
4773 dev
= get_imsm_dev(super
, super
->current_vol
);
4774 map
= get_imsm_map(dev
, MAP_0
);
4776 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
4777 fprintf(stderr
, Name
": %s: Cannot add spare devices to IMSM volume\n",
4783 /* we're doing autolayout so grab the pre-marked (in
4784 * validate_geometry) raid_disk
4786 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4787 if (dl
->raiddisk
== dk
->raid_disk
)
4790 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4791 if (dl
->major
== dk
->major
&&
4792 dl
->minor
== dk
->minor
)
4797 fprintf(stderr
, Name
": %s is not a member of the same container\n", devname
);
4801 /* add a pristine spare to the metadata */
4802 if (dl
->index
< 0) {
4803 dl
->index
= super
->anchor
->num_disks
;
4804 super
->anchor
->num_disks
++;
4806 /* Check the device has not already been added */
4807 slot
= get_imsm_disk_slot(map
, dl
->index
);
4809 (get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
) & IMSM_ORD_REBUILD
) == 0) {
4810 fprintf(stderr
, Name
": %s has been included in this array twice\n",
4814 set_imsm_ord_tbl_ent(map
, dk
->raid_disk
, dl
->index
);
4815 dl
->disk
.status
= CONFIGURED_DISK
;
4817 /* update size of 'missing' disks to be at least as large as the
4818 * largest acitve member (we only have dummy missing disks when
4819 * creating the first volume)
4821 if (super
->current_vol
== 0) {
4822 for (df
= super
->missing
; df
; df
= df
->next
) {
4823 if (total_blocks(&dl
->disk
) > total_blocks(&df
->disk
))
4824 set_total_blocks(&df
->disk
, total_blocks(&dl
->disk
));
4825 _disk
= __get_imsm_disk(mpb
, df
->index
);
4830 /* refresh unset/failed slots to point to valid 'missing' entries */
4831 for (df
= super
->missing
; df
; df
= df
->next
)
4832 for (slot
= 0; slot
< mpb
->num_disks
; slot
++) {
4833 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
4835 if ((ord
& IMSM_ORD_REBUILD
) == 0)
4837 set_imsm_ord_tbl_ent(map
, slot
, df
->index
| IMSM_ORD_REBUILD
);
4838 if (is_gen_migration(dev
)) {
4839 struct imsm_map
*map2
= get_imsm_map(dev
,
4841 int slot2
= get_imsm_disk_slot(map2
, df
->index
);
4842 if ((slot2
< map2
->num_members
) &&
4844 __u32 ord2
= get_imsm_ord_tbl_ent(dev
,
4847 if ((unsigned)df
->index
==
4849 set_imsm_ord_tbl_ent(map2
,
4855 dprintf("set slot:%d to missing disk:%d\n", slot
, df
->index
);
4859 /* if we are creating the first raid device update the family number */
4860 if (super
->current_vol
== 0) {
4862 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
4864 _disk
= __get_imsm_disk(mpb
, dl
->index
);
4865 if (!_dev
|| !_disk
) {
4866 fprintf(stderr
, Name
": BUG mpb setup error\n");
4872 sum
+= __gen_imsm_checksum(mpb
);
4873 mpb
->family_num
= __cpu_to_le32(sum
);
4874 mpb
->orig_family_num
= mpb
->family_num
;
4876 super
->current_disk
= dl
;
4881 * Function marks disk as spare and restores disk serial
4882 * in case it was previously marked as failed by takeover operation
4884 * -1 : critical error
4885 * 0 : disk is marked as spare but serial is not set
4888 int mark_spare(struct dl
*disk
)
4890 __u8 serial
[MAX_RAID_SERIAL_LEN
];
4897 if (!imsm_read_serial(disk
->fd
, NULL
, serial
)) {
4898 /* Restore disk serial number, because takeover marks disk
4899 * as failed and adds to serial ':0' before it becomes
4902 serialcpy(disk
->serial
, serial
);
4903 serialcpy(disk
->disk
.serial
, serial
);
4906 disk
->disk
.status
= SPARE_DISK
;
4912 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
4913 int fd
, char *devname
)
4915 struct intel_super
*super
= st
->sb
;
4917 unsigned long long size
;
4922 /* If we are on an RAID enabled platform check that the disk is
4923 * attached to the raid controller.
4924 * We do not need to test disks attachment for container based additions,
4925 * they shall be already tested when container was created/assembled.
4927 rv
= find_intel_hba_capability(fd
, super
, devname
);
4928 /* no orom/efi or non-intel hba of the disk */
4930 dprintf("capability: %p fd: %d ret: %d\n",
4931 super
->orom
, fd
, rv
);
4935 if (super
->current_vol
>= 0)
4936 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
4939 dd
= malloc(sizeof(*dd
));
4942 Name
": malloc failed %s:%d.\n", __func__
, __LINE__
);
4945 memset(dd
, 0, sizeof(*dd
));
4946 dd
->major
= major(stb
.st_rdev
);
4947 dd
->minor
= minor(stb
.st_rdev
);
4948 dd
->devname
= devname
? strdup(devname
) : NULL
;
4951 dd
->action
= DISK_ADD
;
4952 rv
= imsm_read_serial(fd
, devname
, dd
->serial
);
4955 Name
": failed to retrieve scsi serial, aborting\n");
4960 get_dev_size(fd
, NULL
, &size
);
4962 serialcpy(dd
->disk
.serial
, dd
->serial
);
4963 set_total_blocks(&dd
->disk
, size
);
4964 if (__le32_to_cpu(dd
->disk
.total_blocks_hi
) > 0) {
4965 struct imsm_super
*mpb
= super
->anchor
;
4966 mpb
->attributes
|= MPB_ATTRIB_2TB_DISK
;
4969 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
4970 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
4972 dd
->disk
.scsi_id
= __cpu_to_le32(0);
4974 if (st
->update_tail
) {
4975 dd
->next
= super
->disk_mgmt_list
;
4976 super
->disk_mgmt_list
= dd
;
4978 dd
->next
= super
->disks
;
4980 super
->updates_pending
++;
4987 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
4989 struct intel_super
*super
= st
->sb
;
4992 /* remove from super works only in mdmon - for communication
4993 * manager - monitor. Check if communication memory buffer
4996 if (!st
->update_tail
) {
4998 Name
": %s shall be used in mdmon context only"
4999 "(line %d).\n", __func__
, __LINE__
);
5002 dd
= malloc(sizeof(*dd
));
5005 Name
": malloc failed %s:%d.\n", __func__
, __LINE__
);
5008 memset(dd
, 0, sizeof(*dd
));
5009 dd
->major
= dk
->major
;
5010 dd
->minor
= dk
->minor
;
5013 dd
->action
= DISK_REMOVE
;
5015 dd
->next
= super
->disk_mgmt_list
;
5016 super
->disk_mgmt_list
= dd
;
5022 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
5026 struct imsm_super anchor
;
5027 } spare_record
__attribute__ ((aligned(512)));
5029 /* spare records have their own family number and do not have any defined raid
5032 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
5034 struct imsm_super
*mpb
= super
->anchor
;
5035 struct imsm_super
*spare
= &spare_record
.anchor
;
5039 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
)),
5040 spare
->generation_num
= __cpu_to_le32(1UL),
5041 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
5042 spare
->num_disks
= 1,
5043 spare
->num_raid_devs
= 0,
5044 spare
->cache_size
= mpb
->cache_size
,
5045 spare
->pwr_cycle_count
= __cpu_to_le32(1),
5047 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
5048 MPB_SIGNATURE MPB_VERSION_RAID0
);
5050 for (d
= super
->disks
; d
; d
= d
->next
) {
5054 spare
->disk
[0] = d
->disk
;
5055 if (__le32_to_cpu(d
->disk
.total_blocks_hi
) > 0)
5056 spare
->attributes
|= MPB_ATTRIB_2TB_DISK
;
5058 sum
= __gen_imsm_checksum(spare
);
5059 spare
->family_num
= __cpu_to_le32(sum
);
5060 spare
->orig_family_num
= 0;
5061 sum
= __gen_imsm_checksum(spare
);
5062 spare
->check_sum
= __cpu_to_le32(sum
);
5064 if (store_imsm_mpb(d
->fd
, spare
)) {
5065 fprintf(stderr
, "%s: failed for device %d:%d %s\n",
5066 __func__
, d
->major
, d
->minor
, strerror(errno
));
5078 static int write_super_imsm(struct supertype
*st
, int doclose
)
5080 struct intel_super
*super
= st
->sb
;
5081 struct imsm_super
*mpb
= super
->anchor
;
5087 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
5089 int clear_migration_record
= 1;
5091 /* 'generation' is incremented everytime the metadata is written */
5092 generation
= __le32_to_cpu(mpb
->generation_num
);
5094 mpb
->generation_num
= __cpu_to_le32(generation
);
5096 /* fix up cases where previous mdadm releases failed to set
5099 if (mpb
->orig_family_num
== 0)
5100 mpb
->orig_family_num
= mpb
->family_num
;
5102 for (d
= super
->disks
; d
; d
= d
->next
) {
5106 mpb
->disk
[d
->index
] = d
->disk
;
5110 for (d
= super
->missing
; d
; d
= d
->next
) {
5111 mpb
->disk
[d
->index
] = d
->disk
;
5114 mpb
->num_disks
= num_disks
;
5115 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
5117 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5118 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
5119 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
5121 imsm_copy_dev(dev
, dev2
);
5122 mpb_size
+= sizeof_imsm_dev(dev
, 0);
5124 if (is_gen_migration(dev2
))
5125 clear_migration_record
= 0;
5127 mpb_size
+= __le32_to_cpu(mpb
->bbm_log_size
);
5128 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5130 /* recalculate checksum */
5131 sum
= __gen_imsm_checksum(mpb
);
5132 mpb
->check_sum
= __cpu_to_le32(sum
);
5134 if (super
->clean_migration_record_by_mdmon
) {
5135 clear_migration_record
= 1;
5136 super
->clean_migration_record_by_mdmon
= 0;
5138 if (clear_migration_record
)
5139 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SIZE
);
5141 /* write the mpb for disks that compose raid devices */
5142 for (d
= super
->disks
; d
; d
= d
->next
) {
5143 if (d
->index
< 0 || is_failed(&d
->disk
))
5146 if (clear_migration_record
) {
5147 unsigned long long dsize
;
5149 get_dev_size(d
->fd
, NULL
, &dsize
);
5150 if (lseek64(d
->fd
, dsize
- 512, SEEK_SET
) >= 0) {
5151 if (write(d
->fd
, super
->migr_rec_buf
,
5152 MIGR_REC_BUF_SIZE
) != MIGR_REC_BUF_SIZE
)
5153 perror("Write migr_rec failed");
5157 if (store_imsm_mpb(d
->fd
, mpb
))
5159 "%s: failed for device %d:%d (fd: %d)%s\n",
5160 __func__
, d
->major
, d
->minor
,
5161 d
->fd
, strerror(errno
));
5170 return write_super_imsm_spares(super
, doclose
);
5176 static int create_array(struct supertype
*st
, int dev_idx
)
5179 struct imsm_update_create_array
*u
;
5180 struct intel_super
*super
= st
->sb
;
5181 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
5182 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
5183 struct disk_info
*inf
;
5184 struct imsm_disk
*disk
;
5187 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
5188 sizeof(*inf
) * map
->num_members
;
5191 fprintf(stderr
, "%s: failed to allocate update buffer\n",
5196 u
->type
= update_create_array
;
5197 u
->dev_idx
= dev_idx
;
5198 imsm_copy_dev(&u
->dev
, dev
);
5199 inf
= get_disk_info(u
);
5200 for (i
= 0; i
< map
->num_members
; i
++) {
5201 int idx
= get_imsm_disk_idx(dev
, i
, MAP_X
);
5203 disk
= get_imsm_disk(super
, idx
);
5204 serialcpy(inf
[i
].serial
, disk
->serial
);
5206 append_metadata_update(st
, u
, len
);
5211 static int mgmt_disk(struct supertype
*st
)
5213 struct intel_super
*super
= st
->sb
;
5215 struct imsm_update_add_remove_disk
*u
;
5217 if (!super
->disk_mgmt_list
)
5223 fprintf(stderr
, "%s: failed to allocate update buffer\n",
5228 u
->type
= update_add_remove_disk
;
5229 append_metadata_update(st
, u
, len
);
5234 static int write_init_super_imsm(struct supertype
*st
)
5236 struct intel_super
*super
= st
->sb
;
5237 int current_vol
= super
->current_vol
;
5239 /* we are done with current_vol reset it to point st at the container */
5240 super
->current_vol
= -1;
5242 if (st
->update_tail
) {
5243 /* queue the recently created array / added disk
5244 * as a metadata update */
5247 /* determine if we are creating a volume or adding a disk */
5248 if (current_vol
< 0) {
5249 /* in the mgmt (add/remove) disk case we are running
5250 * in mdmon context, so don't close fd's
5252 return mgmt_disk(st
);
5254 rv
= create_array(st
, current_vol
);
5259 for (d
= super
->disks
; d
; d
= d
->next
)
5260 Kill(d
->devname
, NULL
, 0, 1, 1);
5261 return write_super_imsm(st
, 1);
5266 static int store_super_imsm(struct supertype
*st
, int fd
)
5268 struct intel_super
*super
= st
->sb
;
5269 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
5275 return store_imsm_mpb(fd
, mpb
);
5281 static int imsm_bbm_log_size(struct imsm_super
*mpb
)
5283 return __le32_to_cpu(mpb
->bbm_log_size
);
5287 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
5288 int layout
, int raiddisks
, int chunk
,
5289 unsigned long long size
, char *dev
,
5290 unsigned long long *freesize
,
5294 unsigned long long ldsize
;
5295 struct intel_super
*super
=NULL
;
5298 if (level
!= LEVEL_CONTAINER
)
5303 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
5306 fprintf(stderr
, Name
": imsm: Cannot open %s: %s\n",
5307 dev
, strerror(errno
));
5310 if (!get_dev_size(fd
, dev
, &ldsize
)) {
5315 /* capabilities retrieve could be possible
5316 * note that there is no fd for the disks in array.
5318 super
= alloc_super();
5321 Name
": malloc of %zu failed.\n",
5327 rv
= find_intel_hba_capability(fd
, super
, verbose
? dev
: NULL
);
5331 fd2devname(fd
, str
);
5332 dprintf("validate_geometry_imsm_container: fd: %d %s orom: %p rv: %d raiddisk: %d\n",
5333 fd
, str
, super
->orom
, rv
, raiddisks
);
5335 /* no orom/efi or non-intel hba of the disk */
5342 if (raiddisks
> super
->orom
->tds
) {
5344 fprintf(stderr
, Name
": %d exceeds maximum number of"
5345 " platform supported disks: %d\n",
5346 raiddisks
, super
->orom
->tds
);
5350 if ((super
->orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) == 0 &&
5351 (ldsize
>> 9) >> 32 > 0) {
5353 fprintf(stderr
, Name
": %s exceeds maximum platform supported size\n", dev
);
5359 *freesize
= avail_size_imsm(st
, ldsize
>> 9);
5365 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
5367 const unsigned long long base_start
= e
[*idx
].start
;
5368 unsigned long long end
= base_start
+ e
[*idx
].size
;
5371 if (base_start
== end
)
5375 for (i
= *idx
; i
< num_extents
; i
++) {
5376 /* extend overlapping extents */
5377 if (e
[i
].start
>= base_start
&&
5378 e
[i
].start
<= end
) {
5381 if (e
[i
].start
+ e
[i
].size
> end
)
5382 end
= e
[i
].start
+ e
[i
].size
;
5383 } else if (e
[i
].start
> end
) {
5389 return end
- base_start
;
5392 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
5394 /* build a composite disk with all known extents and generate a new
5395 * 'maxsize' given the "all disks in an array must share a common start
5396 * offset" constraint
5398 struct extent
*e
= calloc(sum_extents
, sizeof(*e
));
5402 unsigned long long pos
;
5403 unsigned long long start
= 0;
5404 unsigned long long maxsize
;
5405 unsigned long reserve
;
5410 /* coalesce and sort all extents. also, check to see if we need to
5411 * reserve space between member arrays
5414 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5417 for (i
= 0; i
< dl
->extent_cnt
; i
++)
5420 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
5425 while (i
< sum_extents
) {
5426 e
[j
].start
= e
[i
].start
;
5427 e
[j
].size
= find_size(e
, &i
, sum_extents
);
5429 if (e
[j
-1].size
== 0)
5438 unsigned long long esize
;
5440 esize
= e
[i
].start
- pos
;
5441 if (esize
>= maxsize
) {
5446 pos
= e
[i
].start
+ e
[i
].size
;
5448 } while (e
[i
-1].size
);
5454 /* FIXME assumes volume at offset 0 is the first volume in a
5457 if (start_extent
> 0)
5458 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
5462 if (maxsize
< reserve
)
5465 super
->create_offset
= ~((unsigned long long) 0);
5466 if (start
+ reserve
> super
->create_offset
)
5467 return 0; /* start overflows create_offset */
5468 super
->create_offset
= start
+ reserve
;
5470 return maxsize
- reserve
;
5473 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
5475 if (level
< 0 || level
== 6 || level
== 4)
5478 /* if we have an orom prevent invalid raid levels */
5481 case 0: return imsm_orom_has_raid0(orom
);
5484 return imsm_orom_has_raid1e(orom
);
5485 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
5486 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
5487 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
5490 return 1; /* not on an Intel RAID platform so anything goes */
5497 active_arrays_by_format(char *name
, char* hba
, struct md_list
**devlist
,
5498 int dpa
, int verbose
)
5500 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
5501 struct mdstat_ent
*memb
= NULL
;
5504 struct md_list
*dv
= NULL
;
5507 for (memb
= mdstat
; memb
; memb
= memb
->next
) {
5508 if (memb
->metadata_version
&&
5509 (strncmp(memb
->metadata_version
, "external:", 9) == 0) &&
5510 (strcmp(&memb
->metadata_version
[9], name
) == 0) &&
5511 !is_subarray(memb
->metadata_version
+9) &&
5513 struct dev_member
*dev
= memb
->members
;
5515 while(dev
&& (fd
< 0)) {
5516 char *path
= malloc(strlen(dev
->name
) + strlen("/dev/") + 1);
5518 num
= sprintf(path
, "%s%s", "/dev/", dev
->name
);
5520 fd
= open(path
, O_RDONLY
, 0);
5521 if ((num
<= 0) || (fd
< 0)) {
5522 pr_vrb(": Cannot open %s: %s\n",
5523 dev
->name
, strerror(errno
));
5530 if ((fd
>= 0) && disk_attached_to_hba(fd
, hba
)) {
5531 struct mdstat_ent
*vol
;
5532 for (vol
= mdstat
; vol
; vol
= vol
->next
) {
5533 if ((vol
->active
> 0) &&
5534 vol
->metadata_version
&&
5535 is_container_member(vol
, memb
->dev
)) {
5540 if (*devlist
&& (found
< dpa
)) {
5541 dv
= calloc(1, sizeof(*dv
));
5543 fprintf(stderr
, Name
": calloc failed\n");
5545 dv
->devname
= malloc(strlen(memb
->dev
) + strlen("/dev/") + 1);
5546 if (dv
->devname
!= NULL
) {
5547 sprintf(dv
->devname
, "%s%s", "/dev/", memb
->dev
);
5550 dv
->next
= *devlist
;
5561 free_mdstat(mdstat
);
5566 static struct md_list
*
5567 get_loop_devices(void)
5570 struct md_list
*devlist
= NULL
;
5571 struct md_list
*dv
= NULL
;
5573 for(i
= 0; i
< 12; i
++) {
5574 dv
= calloc(1, sizeof(*dv
));
5576 fprintf(stderr
, Name
": calloc failed\n");
5579 dv
->devname
= malloc(40);
5580 if (dv
->devname
== NULL
) {
5581 fprintf(stderr
, Name
": malloc failed\n");
5585 sprintf(dv
->devname
, "/dev/loop%d", i
);
5593 static struct md_list
*
5594 get_devices(const char *hba_path
)
5596 struct md_list
*devlist
= NULL
;
5597 struct md_list
*dv
= NULL
;
5603 devlist
= get_loop_devices();
5606 /* scroll through /sys/dev/block looking for devices attached to
5609 dir
= opendir("/sys/dev/block");
5610 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
5615 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
5617 path
= devt_to_devpath(makedev(major
, minor
));
5620 if (!path_attached_to_hba(path
, hba_path
)) {
5627 fd
= dev_open(ent
->d_name
, O_RDONLY
);
5629 fd2devname(fd
, buf
);
5632 fprintf(stderr
, Name
": cannot open device: %s\n",
5638 dv
= calloc(1, sizeof(*dv
));
5640 fprintf(stderr
, Name
": malloc failed\n");
5644 dv
->devname
= strdup(buf
);
5645 if (dv
->devname
== NULL
) {
5646 fprintf(stderr
, Name
": malloc failed\n");
5657 devlist
= devlist
->next
;
5666 count_volumes_list(struct md_list
*devlist
, char *homehost
,
5667 int verbose
, int *found
)
5669 struct md_list
*tmpdev
;
5671 struct supertype
*st
= NULL
;
5673 /* first walk the list of devices to find a consistent set
5674 * that match the criterea, if that is possible.
5675 * We flag the ones we like with 'used'.
5678 st
= match_metadata_desc_imsm("imsm");
5680 pr_vrb(": cannot allocate memory for imsm supertype\n");
5684 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5685 char *devname
= tmpdev
->devname
;
5687 struct supertype
*tst
;
5689 if (tmpdev
->used
> 1)
5691 tst
= dup_super(st
);
5693 pr_vrb(": cannot allocate memory for imsm supertype\n");
5696 tmpdev
->container
= 0;
5697 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
5699 dprintf(": cannot open device %s: %s\n",
5700 devname
, strerror(errno
));
5702 } else if (fstat(dfd
, &stb
)< 0) {
5704 dprintf(": fstat failed for %s: %s\n",
5705 devname
, strerror(errno
));
5707 } else if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
5708 dprintf(": %s is not a block device.\n",
5711 } else if (must_be_container(dfd
)) {
5712 struct supertype
*cst
;
5713 cst
= super_by_fd(dfd
, NULL
);
5715 dprintf(": cannot recognize container type %s\n",
5718 } else if (tst
->ss
!= st
->ss
) {
5719 dprintf(": non-imsm container - ignore it: %s\n",
5722 } else if (!tst
->ss
->load_container
||
5723 tst
->ss
->load_container(tst
, dfd
, NULL
))
5726 tmpdev
->container
= 1;
5729 cst
->ss
->free_super(cst
);
5731 tmpdev
->st_rdev
= stb
.st_rdev
;
5732 if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
5733 dprintf(": no RAID superblock on %s\n",
5736 } else if (tst
->ss
->compare_super
== NULL
) {
5737 dprintf(": Cannot assemble %s metadata on %s\n",
5738 tst
->ss
->name
, devname
);
5744 if (tmpdev
->used
== 2 || tmpdev
->used
== 4) {
5745 /* Ignore unrecognised devices during auto-assembly */
5750 tst
->ss
->getinfo_super(tst
, &info
, NULL
);
5752 if (st
->minor_version
== -1)
5753 st
->minor_version
= tst
->minor_version
;
5755 if (memcmp(info
.uuid
, uuid_zero
,
5756 sizeof(int[4])) == 0) {
5757 /* this is a floating spare. It cannot define
5758 * an array unless there are no more arrays of
5759 * this type to be found. It can be included
5760 * in an array of this type though.
5766 if (st
->ss
!= tst
->ss
||
5767 st
->minor_version
!= tst
->minor_version
||
5768 st
->ss
->compare_super(st
, tst
) != 0) {
5769 /* Some mismatch. If exactly one array matches this host,
5770 * we can resolve on that one.
5771 * Or, if we are auto assembling, we just ignore the second
5774 dprintf(": superblock on %s doesn't match others - assembly aborted\n",
5780 dprintf("found: devname: %s\n", devname
);
5784 tst
->ss
->free_super(tst
);
5788 if ((err
= load_super_imsm_all(st
, -1, &st
->sb
, NULL
, devlist
, 0)) == 0) {
5789 struct mdinfo
*iter
, *head
= st
->ss
->container_content(st
, NULL
);
5790 for (iter
= head
; iter
; iter
= iter
->next
) {
5791 dprintf("content->text_version: %s vol\n",
5792 iter
->text_version
);
5793 if (iter
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
5794 /* do not assemble arrays with unsupported
5796 dprintf(": Cannot activate member %s.\n",
5797 iter
->text_version
);
5804 dprintf(" no valid super block on device list: err: %d %p\n",
5808 dprintf(" no more devices to examin\n");
5811 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5812 if ((tmpdev
->used
== 1) && (tmpdev
->found
)) {
5814 if (count
< tmpdev
->found
)
5817 count
-= tmpdev
->found
;
5820 if (tmpdev
->used
== 1)
5825 st
->ss
->free_super(st
);
5831 count_volumes(char *hba
, int dpa
, int verbose
)
5833 struct md_list
*devlist
= NULL
;
5837 devlist
= get_devices(hba
);
5838 /* if no intel devices return zero volumes */
5839 if (devlist
== NULL
)
5842 count
= active_arrays_by_format("imsm", hba
, &devlist
, dpa
, verbose
);
5843 dprintf(" path: %s active arrays: %d\n", hba
, count
);
5844 if (devlist
== NULL
)
5848 count
+= count_volumes_list(devlist
,
5852 dprintf("found %d count: %d\n", found
, count
);
5855 dprintf("path: %s total number of volumes: %d\n", hba
, count
);
5858 struct md_list
*dv
= devlist
;
5859 devlist
= devlist
->next
;
5866 static int imsm_default_chunk(const struct imsm_orom
*orom
)
5868 /* up to 512 if the plaform supports it, otherwise the platform max.
5869 * 128 if no platform detected
5871 int fs
= max(7, orom
? fls(orom
->sss
) : 0);
5873 return min(512, (1 << fs
));
5877 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
5878 int raiddisks
, int *chunk
, unsigned long long size
, int verbose
)
5880 /* check/set platform and metadata limits/defaults */
5881 if (super
->orom
&& raiddisks
> super
->orom
->dpa
) {
5882 pr_vrb(": platform supports a maximum of %d disks per array\n",
5887 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
5888 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
5889 pr_vrb(": platform does not support raid%d with %d disk%s\n",
5890 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
5894 if (chunk
&& (*chunk
== 0 || *chunk
== UnSet
))
5895 *chunk
= imsm_default_chunk(super
->orom
);
5897 if (super
->orom
&& chunk
&& !imsm_orom_has_chunk(super
->orom
, *chunk
)) {
5898 pr_vrb(": platform does not support a chunk size of: "
5903 if (layout
!= imsm_level_to_layout(level
)) {
5905 pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n");
5906 else if (level
== 10)
5907 pr_vrb(": imsm raid 10 only supports the n2 layout\n");
5909 pr_vrb(": imsm unknown layout %#x for this raid level %d\n",
5914 if (super
->orom
&& (super
->orom
->attr
& IMSM_OROM_ATTR_2TB
) == 0 && chunk
&&
5915 (calc_array_size(level
, raiddisks
, layout
, *chunk
, size
) >> 32) > 0) {
5916 pr_vrb(": platform does not support a volume size over 2TB\n");
5922 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
5923 * FIX ME add ahci details
5925 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
5926 int layout
, int raiddisks
, int *chunk
,
5927 unsigned long long size
, char *dev
,
5928 unsigned long long *freesize
,
5932 struct intel_super
*super
= st
->sb
;
5933 struct imsm_super
*mpb
;
5935 unsigned long long pos
= 0;
5936 unsigned long long maxsize
;
5940 /* We must have the container info already read in. */
5944 mpb
= super
->anchor
;
5946 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, size
, verbose
)) {
5947 fprintf(stderr
, Name
": RAID gemetry validation failed. "
5948 "Cannot proceed with the action(s).\n");
5952 /* General test: make sure there is space for
5953 * 'raiddisks' device extents of size 'size' at a given
5956 unsigned long long minsize
= size
;
5957 unsigned long long start_offset
= MaxSector
;
5960 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
5961 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5966 e
= get_extents(super
, dl
);
5969 unsigned long long esize
;
5970 esize
= e
[i
].start
- pos
;
5971 if (esize
>= minsize
)
5973 if (found
&& start_offset
== MaxSector
) {
5976 } else if (found
&& pos
!= start_offset
) {
5980 pos
= e
[i
].start
+ e
[i
].size
;
5982 } while (e
[i
-1].size
);
5987 if (dcnt
< raiddisks
) {
5989 fprintf(stderr
, Name
": imsm: Not enough "
5990 "devices with space for this array "
5998 /* This device must be a member of the set */
5999 if (stat(dev
, &stb
) < 0)
6001 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
6003 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6004 if (dl
->major
== (int)major(stb
.st_rdev
) &&
6005 dl
->minor
== (int)minor(stb
.st_rdev
))
6010 fprintf(stderr
, Name
": %s is not in the "
6011 "same imsm set\n", dev
);
6013 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
6014 /* If a volume is present then the current creation attempt
6015 * cannot incorporate new spares because the orom may not
6016 * understand this configuration (all member disks must be
6017 * members of each array in the container).
6019 fprintf(stderr
, Name
": %s is a spare and a volume"
6020 " is already defined for this container\n", dev
);
6021 fprintf(stderr
, Name
": The option-rom requires all member"
6022 " disks to be a member of all volumes\n");
6024 } else if (super
->orom
&& mpb
->num_raid_devs
> 0 &&
6025 mpb
->num_disks
!= raiddisks
) {
6026 fprintf(stderr
, Name
": The option-rom requires all member"
6027 " disks to be a member of all volumes\n");
6031 /* retrieve the largest free space block */
6032 e
= get_extents(super
, dl
);
6037 unsigned long long esize
;
6039 esize
= e
[i
].start
- pos
;
6040 if (esize
>= maxsize
)
6042 pos
= e
[i
].start
+ e
[i
].size
;
6044 } while (e
[i
-1].size
);
6049 fprintf(stderr
, Name
": unable to determine free space for: %s\n",
6053 if (maxsize
< size
) {
6055 fprintf(stderr
, Name
": %s not enough space (%llu < %llu)\n",
6056 dev
, maxsize
, size
);
6060 /* count total number of extents for merge */
6062 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6064 i
+= dl
->extent_cnt
;
6066 maxsize
= merge_extents(super
, i
);
6068 if (!check_env("IMSM_NO_PLATFORM") &&
6069 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
6070 fprintf(stderr
, Name
": attempting to create a second "
6071 "volume with size less then remaining space. "
6076 if (maxsize
< size
|| maxsize
== 0) {
6079 fprintf(stderr
, Name
": no free space"
6080 " left on device. Aborting...\n");
6082 fprintf(stderr
, Name
": not enough space"
6083 " to create volume of given size"
6084 " (%llu < %llu). Aborting...\n",
6090 *freesize
= maxsize
;
6093 int count
= count_volumes(super
->hba
->path
,
6094 super
->orom
->dpa
, verbose
);
6095 if (super
->orom
->vphba
<= count
) {
6096 pr_vrb(": platform does not support more than %d raid volumes.\n",
6097 super
->orom
->vphba
);
6104 static int reserve_space(struct supertype
*st
, int raiddisks
,
6105 unsigned long long size
, int chunk
,
6106 unsigned long long *freesize
)
6108 struct intel_super
*super
= st
->sb
;
6109 struct imsm_super
*mpb
= super
->anchor
;
6114 unsigned long long maxsize
;
6115 unsigned long long minsize
;
6119 /* find the largest common start free region of the possible disks */
6123 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6129 /* don't activate new spares if we are orom constrained
6130 * and there is already a volume active in the container
6132 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
6135 e
= get_extents(super
, dl
);
6138 for (i
= 1; e
[i
-1].size
; i
++)
6146 maxsize
= merge_extents(super
, extent_cnt
);
6150 minsize
= chunk
* 2;
6152 if (cnt
< raiddisks
||
6153 (super
->orom
&& used
&& used
!= raiddisks
) ||
6154 maxsize
< minsize
||
6156 fprintf(stderr
, Name
": not enough devices with space to create array.\n");
6157 return 0; /* No enough free spaces large enough */
6168 if (!check_env("IMSM_NO_PLATFORM") &&
6169 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
6170 fprintf(stderr
, Name
": attempting to create a second "
6171 "volume with size less then remaining space. "
6176 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6178 dl
->raiddisk
= cnt
++;
6185 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
6186 int raiddisks
, int *chunk
, unsigned long long size
,
6187 char *dev
, unsigned long long *freesize
,
6195 * if given unused devices create a container
6196 * if given given devices in a container create a member volume
6198 if (level
== LEVEL_CONTAINER
) {
6199 /* Must be a fresh device to add to a container */
6200 return validate_geometry_imsm_container(st
, level
, layout
,
6202 chunk
?*chunk
:0, size
,
6209 struct intel_super
*super
= st
->sb
;
6210 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
6211 raiddisks
, chunk
, size
,
6214 /* we are being asked to automatically layout a
6215 * new volume based on the current contents of
6216 * the container. If the the parameters can be
6217 * satisfied reserve_space will record the disks,
6218 * start offset, and size of the volume to be
6219 * created. add_to_super and getinfo_super
6220 * detect when autolayout is in progress.
6222 /* assuming that freesize is always given when array is
6224 if (super
->orom
&& freesize
) {
6226 count
= count_volumes(super
->hba
->path
,
6227 super
->orom
->dpa
, verbose
);
6228 if (super
->orom
->vphba
<= count
) {
6229 pr_vrb(": platform does not support more"
6230 " than %d raid volumes.\n",
6231 super
->orom
->vphba
);
6236 return reserve_space(st
, raiddisks
, size
,
6237 chunk
?*chunk
:0, freesize
);
6242 /* creating in a given container */
6243 return validate_geometry_imsm_volume(st
, level
, layout
,
6244 raiddisks
, chunk
, size
,
6245 dev
, freesize
, verbose
);
6248 /* This device needs to be a device in an 'imsm' container */
6249 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
6253 Name
": Cannot create this array on device %s\n",
6258 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
6260 fprintf(stderr
, Name
": Cannot open %s: %s\n",
6261 dev
, strerror(errno
));
6264 /* Well, it is in use by someone, maybe an 'imsm' container. */
6265 cfd
= open_container(fd
);
6269 fprintf(stderr
, Name
": Cannot use %s: It is busy\n",
6273 sra
= sysfs_read(cfd
, 0, GET_VERSION
);
6274 if (sra
&& sra
->array
.major_version
== -1 &&
6275 strcmp(sra
->text_version
, "imsm") == 0)
6279 /* This is a member of a imsm container. Load the container
6280 * and try to create a volume
6282 struct intel_super
*super
;
6284 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
, NULL
, 1) == 0) {
6286 st
->container_dev
= fd2devnum(cfd
);
6288 return validate_geometry_imsm_volume(st
, level
, layout
,
6297 fprintf(stderr
, Name
": failed container membership check\n");
6303 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
6305 struct intel_super
*super
= st
->sb
;
6307 if (level
&& *level
== UnSet
)
6308 *level
= LEVEL_CONTAINER
;
6310 if (level
&& layout
&& *layout
== UnSet
)
6311 *layout
= imsm_level_to_layout(*level
);
6313 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0))
6314 *chunk
= imsm_default_chunk(super
->orom
);
6317 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
6319 static int kill_subarray_imsm(struct supertype
*st
)
6321 /* remove the subarray currently referenced by ->current_vol */
6323 struct intel_dev
**dp
;
6324 struct intel_super
*super
= st
->sb
;
6325 __u8 current_vol
= super
->current_vol
;
6326 struct imsm_super
*mpb
= super
->anchor
;
6328 if (super
->current_vol
< 0)
6330 super
->current_vol
= -1; /* invalidate subarray cursor */
6332 /* block deletions that would change the uuid of active subarrays
6334 * FIXME when immutable ids are available, but note that we'll
6335 * also need to fixup the invalidated/active subarray indexes in
6338 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6341 if (i
< current_vol
)
6343 sprintf(subarray
, "%u", i
);
6344 if (is_subarray_active(subarray
, st
->devname
)) {
6346 Name
": deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
6353 if (st
->update_tail
) {
6354 struct imsm_update_kill_array
*u
= malloc(sizeof(*u
));
6358 u
->type
= update_kill_array
;
6359 u
->dev_idx
= current_vol
;
6360 append_metadata_update(st
, u
, sizeof(*u
));
6365 for (dp
= &super
->devlist
; *dp
;)
6366 if ((*dp
)->index
== current_vol
) {
6369 handle_missing(super
, (*dp
)->dev
);
6370 if ((*dp
)->index
> current_vol
)
6375 /* no more raid devices, all active components are now spares,
6376 * but of course failed are still failed
6378 if (--mpb
->num_raid_devs
== 0) {
6381 for (d
= super
->disks
; d
; d
= d
->next
)
6386 super
->updates_pending
++;
6391 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
6392 char *update
, struct mddev_ident
*ident
)
6394 /* update the subarray currently referenced by ->current_vol */
6395 struct intel_super
*super
= st
->sb
;
6396 struct imsm_super
*mpb
= super
->anchor
;
6398 if (strcmp(update
, "name") == 0) {
6399 char *name
= ident
->name
;
6403 if (is_subarray_active(subarray
, st
->devname
)) {
6405 Name
": Unable to update name of active subarray\n");
6409 if (!check_name(super
, name
, 0))
6412 vol
= strtoul(subarray
, &ep
, 10);
6413 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
6416 if (st
->update_tail
) {
6417 struct imsm_update_rename_array
*u
= malloc(sizeof(*u
));
6421 u
->type
= update_rename_array
;
6423 snprintf((char *) u
->name
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6424 append_metadata_update(st
, u
, sizeof(*u
));
6426 struct imsm_dev
*dev
;
6429 dev
= get_imsm_dev(super
, vol
);
6430 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6431 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6432 dev
= get_imsm_dev(super
, i
);
6433 handle_missing(super
, dev
);
6435 super
->updates_pending
++;
6442 #endif /* MDASSEMBLE */
6444 static int is_gen_migration(struct imsm_dev
*dev
)
6449 if (!dev
->vol
.migr_state
)
6452 if (migr_type(dev
) == MIGR_GEN_MIGR
)
6458 static int is_rebuilding(struct imsm_dev
*dev
)
6460 struct imsm_map
*migr_map
;
6462 if (!dev
->vol
.migr_state
)
6465 if (migr_type(dev
) != MIGR_REBUILD
)
6468 migr_map
= get_imsm_map(dev
, MAP_1
);
6470 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
6477 static int is_initializing(struct imsm_dev
*dev
)
6479 struct imsm_map
*migr_map
;
6481 if (!dev
->vol
.migr_state
)
6484 if (migr_type(dev
) != MIGR_INIT
)
6487 migr_map
= get_imsm_map(dev
, MAP_1
);
6489 if (migr_map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
6496 static void update_recovery_start(struct intel_super
*super
,
6497 struct imsm_dev
*dev
,
6498 struct mdinfo
*array
)
6500 struct mdinfo
*rebuild
= NULL
;
6504 if (!is_rebuilding(dev
))
6507 /* Find the rebuild target, but punt on the dual rebuild case */
6508 for (d
= array
->devs
; d
; d
= d
->next
)
6509 if (d
->recovery_start
== 0) {
6516 /* (?) none of the disks are marked with
6517 * IMSM_ORD_REBUILD, so assume they are missing and the
6518 * disk_ord_tbl was not correctly updated
6520 dprintf("%s: failed to locate out-of-sync disk\n", __func__
);
6524 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
6525 rebuild
->recovery_start
= units
* blocks_per_migr_unit(super
, dev
);
6529 static int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
);
6532 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
6534 /* Given a container loaded by load_super_imsm_all,
6535 * extract information about all the arrays into
6537 * If 'subarray' is given, just extract info about that array.
6539 * For each imsm_dev create an mdinfo, fill it in,
6540 * then look for matching devices in super->disks
6541 * and create appropriate device mdinfo.
6543 struct intel_super
*super
= st
->sb
;
6544 struct imsm_super
*mpb
= super
->anchor
;
6545 struct mdinfo
*rest
= NULL
;
6549 int spare_disks
= 0;
6551 /* do not assemble arrays when not all attributes are supported */
6552 if (imsm_check_attributes(mpb
->attributes
) == 0) {
6554 fprintf(stderr
, Name
": Unsupported attributes in IMSM metadata."
6555 "Arrays activation is blocked.\n");
6558 /* check for bad blocks */
6559 if (imsm_bbm_log_size(super
->anchor
)) {
6560 fprintf(stderr
, Name
": BBM log found in IMSM metadata."
6561 "Arrays activation is blocked.\n");
6566 /* count spare devices, not used in maps
6568 for (d
= super
->disks
; d
; d
= d
->next
)
6572 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6573 struct imsm_dev
*dev
;
6574 struct imsm_map
*map
;
6575 struct imsm_map
*map2
;
6576 struct mdinfo
*this;
6584 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
6587 dev
= get_imsm_dev(super
, i
);
6588 map
= get_imsm_map(dev
, MAP_0
);
6589 map2
= get_imsm_map(dev
, MAP_1
);
6591 /* do not publish arrays that are in the middle of an
6592 * unsupported migration
6594 if (dev
->vol
.migr_state
&&
6595 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
6596 fprintf(stderr
, Name
": cannot assemble volume '%.16s':"
6597 " unsupported migration in progress\n",
6601 /* do not publish arrays that are not support by controller's
6605 this = malloc(sizeof(*this));
6607 fprintf(stderr
, Name
": failed to allocate %zu bytes\n",
6612 super
->current_vol
= i
;
6613 getinfo_super_imsm_volume(st
, this, NULL
);
6616 chunk
= __le16_to_cpu(map
->blocks_per_strip
) >> 1;
6617 /* mdadm does not support all metadata features- set the bit in all arrays state */
6618 if (!validate_geometry_imsm_orom(super
,
6619 get_imsm_raid_level(map
), /* RAID level */
6620 imsm_level_to_layout(get_imsm_raid_level(map
)),
6621 map
->num_members
, /* raid disks */
6622 &chunk
, join_u32(dev
->size_low
, dev
->size_high
),
6624 fprintf(stderr
, Name
": IMSM RAID geometry validation"
6625 " failed. Array %s activation is blocked.\n",
6627 this->array
.state
|=
6628 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
6629 (1<<MD_SB_BLOCK_VOLUME
);
6633 /* if array has bad blocks, set suitable bit in all arrays state */
6635 this->array
.state
|=
6636 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
6637 (1<<MD_SB_BLOCK_VOLUME
);
6639 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
6640 unsigned long long recovery_start
;
6641 struct mdinfo
*info_d
;
6648 idx
= get_imsm_disk_idx(dev
, slot
, MAP_0
);
6649 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
6650 for (d
= super
->disks
; d
; d
= d
->next
)
6651 if (d
->index
== idx
)
6654 recovery_start
= MaxSector
;
6657 if (d
&& is_failed(&d
->disk
))
6659 if (ord
& IMSM_ORD_REBUILD
)
6663 * if we skip some disks the array will be assmebled degraded;
6664 * reset resync start to avoid a dirty-degraded
6665 * situation when performing the intial sync
6667 * FIXME handle dirty degraded
6669 if ((skip
|| recovery_start
== 0) && !dev
->vol
.dirty
)
6670 this->resync_start
= MaxSector
;
6674 info_d
= calloc(1, sizeof(*info_d
));
6676 fprintf(stderr
, Name
": failed to allocate disk"
6677 " for volume %.16s\n", dev
->volume
);
6678 info_d
= this->devs
;
6680 struct mdinfo
*d
= info_d
->next
;
6689 info_d
->next
= this->devs
;
6690 this->devs
= info_d
;
6692 info_d
->disk
.number
= d
->index
;
6693 info_d
->disk
.major
= d
->major
;
6694 info_d
->disk
.minor
= d
->minor
;
6695 info_d
->disk
.raid_disk
= slot
;
6696 info_d
->recovery_start
= recovery_start
;
6698 if (slot
< map2
->num_members
)
6699 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
6701 this->array
.spare_disks
++;
6703 if (slot
< map
->num_members
)
6704 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
6706 this->array
.spare_disks
++;
6708 if (info_d
->recovery_start
== MaxSector
)
6709 this->array
.working_disks
++;
6711 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
6712 info_d
->data_offset
= pba_of_lba0(map
);
6713 info_d
->component_size
= blocks_per_member(map
);
6715 /* now that the disk list is up-to-date fixup recovery_start */
6716 update_recovery_start(super
, dev
, this);
6717 this->array
.spare_disks
+= spare_disks
;
6720 /* check for reshape */
6721 if (this->reshape_active
== 1)
6722 recover_backup_imsm(st
, this);
6731 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
6732 int failed
, int look_in_map
)
6734 struct imsm_map
*map
;
6736 map
= get_imsm_map(dev
, look_in_map
);
6739 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
6740 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
6742 switch (get_imsm_raid_level(map
)) {
6744 return IMSM_T_STATE_FAILED
;
6747 if (failed
< map
->num_members
)
6748 return IMSM_T_STATE_DEGRADED
;
6750 return IMSM_T_STATE_FAILED
;
6755 * check to see if any mirrors have failed, otherwise we
6756 * are degraded. Even numbered slots are mirrored on
6760 /* gcc -Os complains that this is unused */
6761 int insync
= insync
;
6763 for (i
= 0; i
< map
->num_members
; i
++) {
6764 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_X
);
6765 int idx
= ord_to_idx(ord
);
6766 struct imsm_disk
*disk
;
6768 /* reset the potential in-sync count on even-numbered
6769 * slots. num_copies is always 2 for imsm raid10
6774 disk
= get_imsm_disk(super
, idx
);
6775 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
6778 /* no in-sync disks left in this mirror the
6782 return IMSM_T_STATE_FAILED
;
6785 return IMSM_T_STATE_DEGRADED
;
6789 return IMSM_T_STATE_DEGRADED
;
6791 return IMSM_T_STATE_FAILED
;
6797 return map
->map_state
;
6800 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
6805 struct imsm_disk
*disk
;
6806 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
6807 struct imsm_map
*prev
= get_imsm_map(dev
, MAP_1
);
6808 struct imsm_map
*map_for_loop
;
6813 /* at the beginning of migration we set IMSM_ORD_REBUILD on
6814 * disks that are being rebuilt. New failures are recorded to
6815 * map[0]. So we look through all the disks we started with and
6816 * see if any failures are still present, or if any new ones
6820 if (prev
&& (map
->num_members
< prev
->num_members
))
6821 map_for_loop
= prev
;
6823 for (i
= 0; i
< map_for_loop
->num_members
; i
++) {
6825 /* when MAP_X is passed both maps failures are counted
6828 ((look_in_map
== MAP_1
) || (look_in_map
== MAP_X
)) &&
6829 (i
< prev
->num_members
)) {
6830 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
6831 idx_1
= ord_to_idx(ord
);
6833 disk
= get_imsm_disk(super
, idx_1
);
6834 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
6837 if (((look_in_map
== MAP_0
) || (look_in_map
== MAP_X
)) &&
6838 (i
< map
->num_members
)) {
6839 ord
= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
6840 idx
= ord_to_idx(ord
);
6843 disk
= get_imsm_disk(super
, idx
);
6844 if (!disk
|| is_failed(disk
) ||
6845 ord
& IMSM_ORD_REBUILD
)
6855 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
6858 struct intel_super
*super
= c
->sb
;
6859 struct imsm_super
*mpb
= super
->anchor
;
6861 if (atoi(inst
) >= mpb
->num_raid_devs
) {
6862 fprintf(stderr
, "%s: subarry index %d, out of range\n",
6863 __func__
, atoi(inst
));
6867 dprintf("imsm: open_new %s\n", inst
);
6868 a
->info
.container_member
= atoi(inst
);
6872 static int is_resyncing(struct imsm_dev
*dev
)
6874 struct imsm_map
*migr_map
;
6876 if (!dev
->vol
.migr_state
)
6879 if (migr_type(dev
) == MIGR_INIT
||
6880 migr_type(dev
) == MIGR_REPAIR
)
6883 if (migr_type(dev
) == MIGR_GEN_MIGR
)
6886 migr_map
= get_imsm_map(dev
, MAP_1
);
6888 if ((migr_map
->map_state
== IMSM_T_STATE_NORMAL
) &&
6889 (dev
->vol
.migr_type
!= MIGR_GEN_MIGR
))
6895 /* return true if we recorded new information */
6896 static int mark_failure(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
6900 struct imsm_map
*map
;
6901 char buf
[MAX_RAID_SERIAL_LEN
+3];
6902 unsigned int len
, shift
= 0;
6904 /* new failures are always set in map[0] */
6905 map
= get_imsm_map(dev
, MAP_0
);
6907 slot
= get_imsm_disk_slot(map
, idx
);
6911 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
6912 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
6915 memcpy(buf
, disk
->serial
, MAX_RAID_SERIAL_LEN
);
6916 buf
[MAX_RAID_SERIAL_LEN
] = '\000';
6918 if ((len
= strlen(buf
)) >= MAX_RAID_SERIAL_LEN
)
6919 shift
= len
- MAX_RAID_SERIAL_LEN
+ 1;
6920 strncpy((char *)disk
->serial
, &buf
[shift
], MAX_RAID_SERIAL_LEN
);
6922 disk
->status
|= FAILED_DISK
;
6923 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
6924 /* mark failures in second map if second map exists and this disk
6926 * This is valid for migration, initialization and rebuild
6928 if (dev
->vol
.migr_state
) {
6929 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
6930 int slot2
= get_imsm_disk_slot(map2
, idx
);
6932 if ((slot2
< map2
->num_members
) &&
6934 set_imsm_ord_tbl_ent(map2
, slot2
,
6935 idx
| IMSM_ORD_REBUILD
);
6937 if (map
->failed_disk_num
== 0xff)
6938 map
->failed_disk_num
= slot
;
6942 static void mark_missing(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
6944 mark_failure(dev
, disk
, idx
);
6946 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
6949 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
6950 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
6953 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
6957 if (!super
->missing
)
6960 dprintf("imsm: mark missing\n");
6961 /* end process for initialization and rebuild only
6963 if (is_gen_migration(dev
) == 0) {
6967 failed
= imsm_count_failed(super
, dev
, MAP_0
);
6968 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
6970 end_migration(dev
, super
, map_state
);
6972 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
6973 mark_missing(dev
, &dl
->disk
, dl
->index
);
6974 super
->updates_pending
++;
6977 static unsigned long long imsm_set_array_size(struct imsm_dev
*dev
)
6979 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
6980 unsigned long long array_blocks
;
6981 struct imsm_map
*map
;
6983 if (used_disks
== 0) {
6984 /* when problems occures
6985 * return current array_blocks value
6987 array_blocks
= __le32_to_cpu(dev
->size_high
);
6988 array_blocks
= array_blocks
<< 32;
6989 array_blocks
+= __le32_to_cpu(dev
->size_low
);
6991 return array_blocks
;
6994 /* set array size in metadata
6996 map
= get_imsm_map(dev
, MAP_0
);
6997 array_blocks
= blocks_per_member(map
) * used_disks
;
6999 /* round array size down to closest MB
7001 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
7002 dev
->size_low
= __cpu_to_le32((__u32
)array_blocks
);
7003 dev
->size_high
= __cpu_to_le32((__u32
)(array_blocks
>> 32));
7005 return array_blocks
;
7008 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
7010 static void imsm_progress_container_reshape(struct intel_super
*super
)
7012 /* if no device has a migr_state, but some device has a
7013 * different number of members than the previous device, start
7014 * changing the number of devices in this device to match
7017 struct imsm_super
*mpb
= super
->anchor
;
7018 int prev_disks
= -1;
7022 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7023 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
7024 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7025 struct imsm_map
*map2
;
7026 int prev_num_members
;
7028 if (dev
->vol
.migr_state
)
7031 if (prev_disks
== -1)
7032 prev_disks
= map
->num_members
;
7033 if (prev_disks
== map
->num_members
)
7036 /* OK, this array needs to enter reshape mode.
7037 * i.e it needs a migr_state
7040 copy_map_size
= sizeof_imsm_map(map
);
7041 prev_num_members
= map
->num_members
;
7042 map
->num_members
= prev_disks
;
7043 dev
->vol
.migr_state
= 1;
7044 dev
->vol
.curr_migr_unit
= 0;
7045 set_migr_type(dev
, MIGR_GEN_MIGR
);
7046 for (i
= prev_num_members
;
7047 i
< map
->num_members
; i
++)
7048 set_imsm_ord_tbl_ent(map
, i
, i
);
7049 map2
= get_imsm_map(dev
, MAP_1
);
7050 /* Copy the current map */
7051 memcpy(map2
, map
, copy_map_size
);
7052 map2
->num_members
= prev_num_members
;
7054 imsm_set_array_size(dev
);
7055 super
->clean_migration_record_by_mdmon
= 1;
7056 super
->updates_pending
++;
7060 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
7061 * states are handled in imsm_set_disk() with one exception, when a
7062 * resync is stopped due to a new failure this routine will set the
7063 * 'degraded' state for the array.
7065 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
7067 int inst
= a
->info
.container_member
;
7068 struct intel_super
*super
= a
->container
->sb
;
7069 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7070 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7071 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
7072 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7073 __u32 blocks_per_unit
;
7075 if (dev
->vol
.migr_state
&&
7076 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
7077 /* array state change is blocked due to reshape action
7079 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
7080 * - finish the reshape (if last_checkpoint is big and action != reshape)
7081 * - update curr_migr_unit
7083 if (a
->curr_action
== reshape
) {
7084 /* still reshaping, maybe update curr_migr_unit */
7085 goto mark_checkpoint
;
7087 if (a
->last_checkpoint
== 0 && a
->prev_action
== reshape
) {
7088 /* for some reason we aborted the reshape.
7090 * disable automatic metadata rollback
7091 * user action is required to recover process
7094 struct imsm_map
*map2
=
7095 get_imsm_map(dev
, MAP_1
);
7096 dev
->vol
.migr_state
= 0;
7097 set_migr_type(dev
, 0);
7098 dev
->vol
.curr_migr_unit
= 0;
7100 sizeof_imsm_map(map2
));
7101 super
->updates_pending
++;
7104 if (a
->last_checkpoint
>= a
->info
.component_size
) {
7105 unsigned long long array_blocks
;
7109 used_disks
= imsm_num_data_members(dev
, MAP_0
);
7110 if (used_disks
> 0) {
7112 blocks_per_member(map
) *
7114 /* round array size down to closest MB
7116 array_blocks
= (array_blocks
7117 >> SECT_PER_MB_SHIFT
)
7118 << SECT_PER_MB_SHIFT
;
7119 a
->info
.custom_array_size
= array_blocks
;
7120 /* encourage manager to update array
7124 a
->check_reshape
= 1;
7126 /* finalize online capacity expansion/reshape */
7127 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
7129 mdi
->disk
.raid_disk
,
7132 imsm_progress_container_reshape(super
);
7137 /* before we activate this array handle any missing disks */
7138 if (consistent
== 2)
7139 handle_missing(super
, dev
);
7141 if (consistent
== 2 &&
7142 (!is_resync_complete(&a
->info
) ||
7143 map_state
!= IMSM_T_STATE_NORMAL
||
7144 dev
->vol
.migr_state
))
7147 if (is_resync_complete(&a
->info
)) {
7148 /* complete intialization / resync,
7149 * recovery and interrupted recovery is completed in
7152 if (is_resyncing(dev
)) {
7153 dprintf("imsm: mark resync done\n");
7154 end_migration(dev
, super
, map_state
);
7155 super
->updates_pending
++;
7156 a
->last_checkpoint
= 0;
7158 } else if ((!is_resyncing(dev
) && !failed
) &&
7159 (imsm_reshape_blocks_arrays_changes(super
) == 0)) {
7160 /* mark the start of the init process if nothing is failed */
7161 dprintf("imsm: mark resync start\n");
7162 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
7163 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
7165 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
7166 super
->updates_pending
++;
7170 /* skip checkpointing for general migration,
7171 * it is controlled in mdadm
7173 if (is_gen_migration(dev
))
7174 goto skip_mark_checkpoint
;
7176 /* check if we can update curr_migr_unit from resync_start, recovery_start */
7177 blocks_per_unit
= blocks_per_migr_unit(super
, dev
);
7178 if (blocks_per_unit
) {
7182 units
= a
->last_checkpoint
/ blocks_per_unit
;
7185 /* check that we did not overflow 32-bits, and that
7186 * curr_migr_unit needs updating
7188 if (units32
== units
&&
7190 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
7191 dprintf("imsm: mark checkpoint (%u)\n", units32
);
7192 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
7193 super
->updates_pending
++;
7197 skip_mark_checkpoint
:
7198 /* mark dirty / clean */
7199 if (dev
->vol
.dirty
!= !consistent
) {
7200 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
7205 super
->updates_pending
++;
7211 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
7213 int inst
= a
->info
.container_member
;
7214 struct intel_super
*super
= a
->container
->sb
;
7215 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7216 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7217 struct imsm_disk
*disk
;
7222 if (n
> map
->num_members
)
7223 fprintf(stderr
, "imsm: set_disk %d out of range 0..%d\n",
7224 n
, map
->num_members
- 1);
7229 dprintf("imsm: set_disk %d:%x\n", n
, state
);
7231 ord
= get_imsm_ord_tbl_ent(dev
, n
, MAP_0
);
7232 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
7234 /* check for new failures */
7235 if (state
& DS_FAULTY
) {
7236 if (mark_failure(dev
, disk
, ord_to_idx(ord
)))
7237 super
->updates_pending
++;
7240 /* check if in_sync */
7241 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
7242 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
7244 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
7245 super
->updates_pending
++;
7248 failed
= imsm_count_failed(super
, dev
, MAP_0
);
7249 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7251 /* check if recovery complete, newly degraded, or failed */
7252 dprintf("imsm: Detected transition to state ");
7253 switch (map_state
) {
7254 case IMSM_T_STATE_NORMAL
: /* transition to normal state */
7255 dprintf("normal: ");
7256 if (is_rebuilding(dev
)) {
7257 dprintf("while rebuilding");
7258 end_migration(dev
, super
, map_state
);
7259 map
= get_imsm_map(dev
, MAP_0
);
7260 map
->failed_disk_num
= ~0;
7261 super
->updates_pending
++;
7262 a
->last_checkpoint
= 0;
7265 if (is_gen_migration(dev
)) {
7266 dprintf("while general migration");
7267 if (a
->last_checkpoint
>= a
->info
.component_size
)
7268 end_migration(dev
, super
, map_state
);
7270 map
->map_state
= map_state
;
7271 map
= get_imsm_map(dev
, MAP_0
);
7272 map
->failed_disk_num
= ~0;
7273 super
->updates_pending
++;
7277 case IMSM_T_STATE_DEGRADED
: /* transition to degraded state */
7278 dprintf("degraded: ");
7279 if ((map
->map_state
!= map_state
) &&
7280 !dev
->vol
.migr_state
) {
7281 dprintf("mark degraded");
7282 map
->map_state
= map_state
;
7283 super
->updates_pending
++;
7284 a
->last_checkpoint
= 0;
7287 if (is_rebuilding(dev
)) {
7288 dprintf("while rebuilding.");
7289 if (map
->map_state
!= map_state
) {
7290 dprintf(" Map state change");
7291 end_migration(dev
, super
, map_state
);
7292 super
->updates_pending
++;
7296 if (is_gen_migration(dev
)) {
7297 dprintf("while general migration");
7298 if (a
->last_checkpoint
>= a
->info
.component_size
)
7299 end_migration(dev
, super
, map_state
);
7301 map
->map_state
= map_state
;
7302 manage_second_map(super
, dev
);
7304 super
->updates_pending
++;
7307 if (is_initializing(dev
)) {
7308 dprintf("while initialization.");
7309 map
->map_state
= map_state
;
7310 super
->updates_pending
++;
7314 case IMSM_T_STATE_FAILED
: /* transition to failed state */
7315 dprintf("failed: ");
7316 if (is_gen_migration(dev
)) {
7317 dprintf("while general migration");
7318 map
->map_state
= map_state
;
7319 super
->updates_pending
++;
7322 if (map
->map_state
!= map_state
) {
7323 dprintf("mark failed");
7324 end_migration(dev
, super
, map_state
);
7325 super
->updates_pending
++;
7326 a
->last_checkpoint
= 0;
7331 dprintf("state %i\n", map_state
);
7337 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
7340 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
7341 unsigned long long dsize
;
7342 unsigned long long sectors
;
7344 get_dev_size(fd
, NULL
, &dsize
);
7346 if (mpb_size
> 512) {
7347 /* -1 to account for anchor */
7348 sectors
= mpb_sectors(mpb
) - 1;
7350 /* write the extended mpb to the sectors preceeding the anchor */
7351 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0)
7354 if ((unsigned long long)write(fd
, buf
+ 512, 512 * sectors
)
7359 /* first block is stored on second to last sector of the disk */
7360 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0)
7363 if (write(fd
, buf
, 512) != 512)
7369 static void imsm_sync_metadata(struct supertype
*container
)
7371 struct intel_super
*super
= container
->sb
;
7373 dprintf("sync metadata: %d\n", super
->updates_pending
);
7374 if (!super
->updates_pending
)
7377 write_super_imsm(container
, 0);
7379 super
->updates_pending
= 0;
7382 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
7384 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
7385 int i
= get_imsm_disk_idx(dev
, idx
, MAP_X
);
7388 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7392 if (dl
&& is_failed(&dl
->disk
))
7396 dprintf("%s: found %x:%x\n", __func__
, dl
->major
, dl
->minor
);
7401 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
7402 struct active_array
*a
, int activate_new
,
7403 struct mdinfo
*additional_test_list
)
7405 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
7406 int idx
= get_imsm_disk_idx(dev
, slot
, MAP_X
);
7407 struct imsm_super
*mpb
= super
->anchor
;
7408 struct imsm_map
*map
;
7409 unsigned long long pos
;
7414 __u32 array_start
= 0;
7415 __u32 array_end
= 0;
7417 struct mdinfo
*test_list
;
7419 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7420 /* If in this array, skip */
7421 for (d
= a
->info
.devs
; d
; d
= d
->next
)
7422 if (d
->state_fd
>= 0 &&
7423 d
->disk
.major
== dl
->major
&&
7424 d
->disk
.minor
== dl
->minor
) {
7425 dprintf("%x:%x already in array\n",
7426 dl
->major
, dl
->minor
);
7431 test_list
= additional_test_list
;
7433 if (test_list
->disk
.major
== dl
->major
&&
7434 test_list
->disk
.minor
== dl
->minor
) {
7435 dprintf("%x:%x already in additional test list\n",
7436 dl
->major
, dl
->minor
);
7439 test_list
= test_list
->next
;
7444 /* skip in use or failed drives */
7445 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
7447 dprintf("%x:%x status (failed: %d index: %d)\n",
7448 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
7452 /* skip pure spares when we are looking for partially
7453 * assimilated drives
7455 if (dl
->index
== -1 && !activate_new
)
7458 /* Does this unused device have the requisite free space?
7459 * It needs to be able to cover all member volumes
7461 ex
= get_extents(super
, dl
);
7463 dprintf("cannot get extents\n");
7466 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7467 dev
= get_imsm_dev(super
, i
);
7468 map
= get_imsm_map(dev
, MAP_0
);
7470 /* check if this disk is already a member of
7473 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
7479 array_start
= pba_of_lba0(map
);
7480 array_end
= array_start
+
7481 blocks_per_member(map
) - 1;
7484 /* check that we can start at pba_of_lba0 with
7485 * blocks_per_member of space
7487 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
7491 pos
= ex
[j
].start
+ ex
[j
].size
;
7493 } while (ex
[j
-1].size
);
7500 if (i
< mpb
->num_raid_devs
) {
7501 dprintf("%x:%x does not have %u to %u available\n",
7502 dl
->major
, dl
->minor
, array_start
, array_end
);
7513 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
7515 struct imsm_dev
*dev2
;
7516 struct imsm_map
*map
;
7522 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
7524 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
, MAP_0
);
7525 if (state
== IMSM_T_STATE_FAILED
) {
7526 map
= get_imsm_map(dev2
, MAP_0
);
7529 for (slot
= 0; slot
< map
->num_members
; slot
++) {
7531 * Check if failed disks are deleted from intel
7532 * disk list or are marked to be deleted
7534 idx
= get_imsm_disk_idx(dev2
, slot
, MAP_X
);
7535 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
7537 * Do not rebuild the array if failed disks
7538 * from failed sub-array are not removed from
7542 is_failed(&idisk
->disk
) &&
7543 (idisk
->action
!= DISK_REMOVE
))
7551 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
7552 struct metadata_update
**updates
)
7555 * Find a device with unused free space and use it to replace a
7556 * failed/vacant region in an array. We replace failed regions one a
7557 * array at a time. The result is that a new spare disk will be added
7558 * to the first failed array and after the monitor has finished
7559 * propagating failures the remainder will be consumed.
7561 * FIXME add a capability for mdmon to request spares from another
7565 struct intel_super
*super
= a
->container
->sb
;
7566 int inst
= a
->info
.container_member
;
7567 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7568 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7569 int failed
= a
->info
.array
.raid_disks
;
7570 struct mdinfo
*rv
= NULL
;
7573 struct metadata_update
*mu
;
7575 struct imsm_update_activate_spare
*u
;
7580 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
7581 if ((d
->curr_state
& DS_FAULTY
) &&
7583 /* wait for Removal to happen */
7585 if (d
->state_fd
>= 0)
7589 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
7590 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
7592 if (imsm_reshape_blocks_arrays_changes(super
))
7595 /* Cannot activate another spare if rebuild is in progress already
7597 if (is_rebuilding(dev
)) {
7598 dprintf("imsm: No spare activation allowed. "
7599 "Rebuild in progress already.\n");
7603 if (a
->info
.array
.level
== 4)
7604 /* No repair for takeovered array
7605 * imsm doesn't support raid4
7609 if (imsm_check_degraded(super
, dev
, failed
, MAP_0
) !=
7610 IMSM_T_STATE_DEGRADED
)
7614 * If there are any failed disks check state of the other volume.
7615 * Block rebuild if the another one is failed until failed disks
7616 * are removed from container.
7619 dprintf("found failed disks in %.*s, check if there another"
7620 "failed sub-array.\n",
7621 MAX_RAID_SERIAL_LEN
, dev
->volume
);
7622 /* check if states of the other volumes allow for rebuild */
7623 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
7625 allowed
= imsm_rebuild_allowed(a
->container
,
7633 /* For each slot, if it is not working, find a spare */
7634 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
7635 for (d
= a
->info
.devs
; d
; d
= d
->next
)
7636 if (d
->disk
.raid_disk
== i
)
7638 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
7639 if (d
&& (d
->state_fd
>= 0))
7643 * OK, this device needs recovery. Try to re-add the
7644 * previous occupant of this slot, if this fails see if
7645 * we can continue the assimilation of a spare that was
7646 * partially assimilated, finally try to activate a new
7649 dl
= imsm_readd(super
, i
, a
);
7651 dl
= imsm_add_spare(super
, i
, a
, 0, rv
);
7653 dl
= imsm_add_spare(super
, i
, a
, 1, rv
);
7657 /* found a usable disk with enough space */
7658 di
= malloc(sizeof(*di
));
7661 memset(di
, 0, sizeof(*di
));
7663 /* dl->index will be -1 in the case we are activating a
7664 * pristine spare. imsm_process_update() will create a
7665 * new index in this case. Once a disk is found to be
7666 * failed in all member arrays it is kicked from the
7669 di
->disk
.number
= dl
->index
;
7671 /* (ab)use di->devs to store a pointer to the device
7674 di
->devs
= (struct mdinfo
*) dl
;
7676 di
->disk
.raid_disk
= i
;
7677 di
->disk
.major
= dl
->major
;
7678 di
->disk
.minor
= dl
->minor
;
7680 di
->recovery_start
= 0;
7681 di
->data_offset
= pba_of_lba0(map
);
7682 di
->component_size
= a
->info
.component_size
;
7683 di
->container_member
= inst
;
7684 super
->random
= random32();
7688 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
7689 i
, di
->data_offset
);
7693 /* No spares found */
7695 /* Now 'rv' has a list of devices to return.
7696 * Create a metadata_update record to update the
7697 * disk_ord_tbl for the array
7699 mu
= malloc(sizeof(*mu
));
7701 mu
->buf
= malloc(sizeof(struct imsm_update_activate_spare
) * num_spares
);
7702 if (mu
->buf
== NULL
) {
7709 struct mdinfo
*n
= rv
->next
;
7718 mu
->space_list
= NULL
;
7719 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
7720 mu
->next
= *updates
;
7721 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
7723 for (di
= rv
; di
; di
= di
->next
) {
7724 u
->type
= update_activate_spare
;
7725 u
->dl
= (struct dl
*) di
->devs
;
7727 u
->slot
= di
->disk
.raid_disk
;
7738 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
7740 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
7741 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7742 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, MAP_0
);
7743 struct disk_info
*inf
= get_disk_info(u
);
7744 struct imsm_disk
*disk
;
7748 for (i
= 0; i
< map
->num_members
; i
++) {
7749 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, MAP_X
));
7750 for (j
= 0; j
< new_map
->num_members
; j
++)
7751 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
7759 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
7761 struct dl
*dl
= NULL
;
7762 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7763 if ((dl
->major
== major
) && (dl
->minor
== minor
))
7768 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
7770 struct dl
*prev
= NULL
;
7774 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7775 if ((dl
->major
== major
) && (dl
->minor
== minor
)) {
7778 prev
->next
= dl
->next
;
7780 super
->disks
= dl
->next
;
7782 __free_imsm_disk(dl
);
7783 dprintf("%s: removed %x:%x\n",
7784 __func__
, major
, minor
);
7792 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
7794 static int add_remove_disk_update(struct intel_super
*super
)
7796 int check_degraded
= 0;
7797 struct dl
*disk
= NULL
;
7798 /* add/remove some spares to/from the metadata/contrainer */
7799 while (super
->disk_mgmt_list
) {
7800 struct dl
*disk_cfg
;
7802 disk_cfg
= super
->disk_mgmt_list
;
7803 super
->disk_mgmt_list
= disk_cfg
->next
;
7804 disk_cfg
->next
= NULL
;
7806 if (disk_cfg
->action
== DISK_ADD
) {
7807 disk_cfg
->next
= super
->disks
;
7808 super
->disks
= disk_cfg
;
7810 dprintf("%s: added %x:%x\n",
7811 __func__
, disk_cfg
->major
,
7813 } else if (disk_cfg
->action
== DISK_REMOVE
) {
7814 dprintf("Disk remove action processed: %x.%x\n",
7815 disk_cfg
->major
, disk_cfg
->minor
);
7816 disk
= get_disk_super(super
,
7820 /* store action status */
7821 disk
->action
= DISK_REMOVE
;
7822 /* remove spare disks only */
7823 if (disk
->index
== -1) {
7824 remove_disk_super(super
,
7829 /* release allocate disk structure */
7830 __free_imsm_disk(disk_cfg
);
7833 return check_degraded
;
7837 static int apply_reshape_migration_update(struct imsm_update_reshape_migration
*u
,
7838 struct intel_super
*super
,
7841 struct intel_dev
*id
;
7842 void **tofree
= NULL
;
7845 dprintf("apply_reshape_migration_update()\n");
7846 if ((u
->subdev
< 0) ||
7848 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
7851 if ((space_list
== NULL
) || (*space_list
== NULL
)) {
7852 dprintf("imsm: Error: Memory is not allocated\n");
7856 for (id
= super
->devlist
; id
; id
= id
->next
) {
7857 if (id
->index
== (unsigned)u
->subdev
) {
7858 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
7859 struct imsm_map
*map
;
7860 struct imsm_dev
*new_dev
=
7861 (struct imsm_dev
*)*space_list
;
7862 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
7864 struct dl
*new_disk
;
7866 if (new_dev
== NULL
)
7868 *space_list
= **space_list
;
7869 memcpy(new_dev
, dev
, sizeof_imsm_dev(dev
, 0));
7870 map
= get_imsm_map(new_dev
, MAP_0
);
7872 dprintf("imsm: Error: migration in progress");
7876 to_state
= map
->map_state
;
7877 if ((u
->new_level
== 5) && (map
->raid_level
== 0)) {
7879 /* this should not happen */
7880 if (u
->new_disks
[0] < 0) {
7881 map
->failed_disk_num
=
7882 map
->num_members
- 1;
7883 to_state
= IMSM_T_STATE_DEGRADED
;
7885 to_state
= IMSM_T_STATE_NORMAL
;
7887 migrate(new_dev
, super
, to_state
, MIGR_GEN_MIGR
);
7888 if (u
->new_level
> -1)
7889 map
->raid_level
= u
->new_level
;
7890 migr_map
= get_imsm_map(new_dev
, MAP_1
);
7891 if ((u
->new_level
== 5) &&
7892 (migr_map
->raid_level
== 0)) {
7893 int ord
= map
->num_members
- 1;
7894 migr_map
->num_members
--;
7895 if (u
->new_disks
[0] < 0)
7896 ord
|= IMSM_ORD_REBUILD
;
7897 set_imsm_ord_tbl_ent(map
,
7898 map
->num_members
- 1,
7902 tofree
= (void **)dev
;
7904 /* update chunk size
7906 if (u
->new_chunksize
> 0)
7907 map
->blocks_per_strip
=
7908 __cpu_to_le16(u
->new_chunksize
* 2);
7912 if ((u
->new_level
!= 5) ||
7913 (migr_map
->raid_level
!= 0) ||
7914 (migr_map
->raid_level
== map
->raid_level
))
7917 if (u
->new_disks
[0] >= 0) {
7920 new_disk
= get_disk_super(super
,
7921 major(u
->new_disks
[0]),
7922 minor(u
->new_disks
[0]));
7923 dprintf("imsm: new disk for reshape is: %i:%i "
7924 "(%p, index = %i)\n",
7925 major(u
->new_disks
[0]),
7926 minor(u
->new_disks
[0]),
7927 new_disk
, new_disk
->index
);
7928 if (new_disk
== NULL
)
7929 goto error_disk_add
;
7931 new_disk
->index
= map
->num_members
- 1;
7932 /* slot to fill in autolayout
7934 new_disk
->raiddisk
= new_disk
->index
;
7935 new_disk
->disk
.status
|= CONFIGURED_DISK
;
7936 new_disk
->disk
.status
&= ~SPARE_DISK
;
7938 goto error_disk_add
;
7941 *tofree
= *space_list
;
7942 /* calculate new size
7944 imsm_set_array_size(new_dev
);
7951 *space_list
= tofree
;
7955 dprintf("Error: imsm: Cannot find disk.\n");
7959 static int apply_update_activate_spare(struct imsm_update_activate_spare
*u
,
7960 struct intel_super
*super
,
7961 struct active_array
*active_array
)
7963 struct imsm_super
*mpb
= super
->anchor
;
7964 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
7965 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7966 struct imsm_map
*migr_map
;
7967 struct active_array
*a
;
7968 struct imsm_disk
*disk
;
7975 int second_map_created
= 0;
7977 for (; u
; u
= u
->next
) {
7978 victim
= get_imsm_disk_idx(dev
, u
->slot
, MAP_X
);
7983 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7988 fprintf(stderr
, "error: imsm_activate_spare passed "
7989 "an unknown disk (index: %d)\n",
7994 /* count failures (excluding rebuilds and the victim)
7995 * to determine map[0] state
7998 for (i
= 0; i
< map
->num_members
; i
++) {
8001 disk
= get_imsm_disk(super
,
8002 get_imsm_disk_idx(dev
, i
, MAP_X
));
8003 if (!disk
|| is_failed(disk
))
8007 /* adding a pristine spare, assign a new index */
8008 if (dl
->index
< 0) {
8009 dl
->index
= super
->anchor
->num_disks
;
8010 super
->anchor
->num_disks
++;
8013 disk
->status
|= CONFIGURED_DISK
;
8014 disk
->status
&= ~SPARE_DISK
;
8017 to_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8018 if (!second_map_created
) {
8019 second_map_created
= 1;
8020 map
->map_state
= IMSM_T_STATE_DEGRADED
;
8021 migrate(dev
, super
, to_state
, MIGR_REBUILD
);
8023 map
->map_state
= to_state
;
8024 migr_map
= get_imsm_map(dev
, MAP_1
);
8025 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
8026 set_imsm_ord_tbl_ent(migr_map
, u
->slot
,
8027 dl
->index
| IMSM_ORD_REBUILD
);
8029 /* update the family_num to mark a new container
8030 * generation, being careful to record the existing
8031 * family_num in orig_family_num to clean up after
8032 * earlier mdadm versions that neglected to set it.
8034 if (mpb
->orig_family_num
== 0)
8035 mpb
->orig_family_num
= mpb
->family_num
;
8036 mpb
->family_num
+= super
->random
;
8038 /* count arrays using the victim in the metadata */
8040 for (a
= active_array
; a
; a
= a
->next
) {
8041 dev
= get_imsm_dev(super
, a
->info
.container_member
);
8042 map
= get_imsm_map(dev
, MAP_0
);
8044 if (get_imsm_disk_slot(map
, victim
) >= 0)
8048 /* delete the victim if it is no longer being
8054 /* We know that 'manager' isn't touching anything,
8055 * so it is safe to delete
8057 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
8058 if ((*dlp
)->index
== victim
)
8061 /* victim may be on the missing list */
8063 for (dlp
= &super
->missing
; *dlp
;
8064 dlp
= &(*dlp
)->next
)
8065 if ((*dlp
)->index
== victim
)
8067 imsm_delete(super
, dlp
, victim
);
8074 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
8075 struct intel_super
*super
,
8078 struct dl
*new_disk
;
8079 struct intel_dev
*id
;
8081 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
8082 int disk_count
= u
->old_raid_disks
;
8083 void **tofree
= NULL
;
8084 int devices_to_reshape
= 1;
8085 struct imsm_super
*mpb
= super
->anchor
;
8087 unsigned int dev_id
;
8089 dprintf("imsm: apply_reshape_container_disks_update()\n");
8091 /* enable spares to use in array */
8092 for (i
= 0; i
< delta_disks
; i
++) {
8093 new_disk
= get_disk_super(super
,
8094 major(u
->new_disks
[i
]),
8095 minor(u
->new_disks
[i
]));
8096 dprintf("imsm: new disk for reshape is: %i:%i "
8097 "(%p, index = %i)\n",
8098 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
8099 new_disk
, new_disk
->index
);
8100 if ((new_disk
== NULL
) ||
8101 ((new_disk
->index
>= 0) &&
8102 (new_disk
->index
< u
->old_raid_disks
)))
8103 goto update_reshape_exit
;
8104 new_disk
->index
= disk_count
++;
8105 /* slot to fill in autolayout
8107 new_disk
->raiddisk
= new_disk
->index
;
8108 new_disk
->disk
.status
|=
8110 new_disk
->disk
.status
&= ~SPARE_DISK
;
8113 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
8114 mpb
->num_raid_devs
);
8115 /* manage changes in volume
8117 for (dev_id
= 0; dev_id
< mpb
->num_raid_devs
; dev_id
++) {
8118 void **sp
= *space_list
;
8119 struct imsm_dev
*newdev
;
8120 struct imsm_map
*newmap
, *oldmap
;
8122 for (id
= super
->devlist
; id
; id
= id
->next
) {
8123 if (id
->index
== dev_id
)
8132 /* Copy the dev, but not (all of) the map */
8133 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
8134 oldmap
= get_imsm_map(id
->dev
, MAP_0
);
8135 newmap
= get_imsm_map(newdev
, MAP_0
);
8136 /* Copy the current map */
8137 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8138 /* update one device only
8140 if (devices_to_reshape
) {
8141 dprintf("imsm: modifying subdev: %i\n",
8143 devices_to_reshape
--;
8144 newdev
->vol
.migr_state
= 1;
8145 newdev
->vol
.curr_migr_unit
= 0;
8146 set_migr_type(newdev
, MIGR_GEN_MIGR
);
8147 newmap
->num_members
= u
->new_raid_disks
;
8148 for (i
= 0; i
< delta_disks
; i
++) {
8149 set_imsm_ord_tbl_ent(newmap
,
8150 u
->old_raid_disks
+ i
,
8151 u
->old_raid_disks
+ i
);
8153 /* New map is correct, now need to save old map
8155 newmap
= get_imsm_map(newdev
, MAP_1
);
8156 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8158 imsm_set_array_size(newdev
);
8161 sp
= (void **)id
->dev
;
8166 /* Clear migration record */
8167 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
8170 *space_list
= tofree
;
8173 update_reshape_exit
:
8178 static int apply_takeover_update(struct imsm_update_takeover
*u
,
8179 struct intel_super
*super
,
8182 struct imsm_dev
*dev
= NULL
;
8183 struct intel_dev
*dv
;
8184 struct imsm_dev
*dev_new
;
8185 struct imsm_map
*map
;
8189 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
8190 if (dv
->index
== (unsigned int)u
->subarray
) {
8198 map
= get_imsm_map(dev
, MAP_0
);
8200 if (u
->direction
== R10_TO_R0
) {
8201 /* Number of failed disks must be half of initial disk number */
8202 if (imsm_count_failed(super
, dev
, MAP_0
) !=
8203 (map
->num_members
/ 2))
8206 /* iterate through devices to mark removed disks as spare */
8207 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
8208 if (dm
->disk
.status
& FAILED_DISK
) {
8209 int idx
= dm
->index
;
8210 /* update indexes on the disk list */
8211 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
8212 the index values will end up being correct.... NB */
8213 for (du
= super
->disks
; du
; du
= du
->next
)
8214 if (du
->index
> idx
)
8216 /* mark as spare disk */
8221 map
->num_members
= map
->num_members
/ 2;
8222 map
->map_state
= IMSM_T_STATE_NORMAL
;
8223 map
->num_domains
= 1;
8224 map
->raid_level
= 0;
8225 map
->failed_disk_num
= -1;
8228 if (u
->direction
== R0_TO_R10
) {
8230 /* update slots in current disk list */
8231 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
8235 /* create new *missing* disks */
8236 for (i
= 0; i
< map
->num_members
; i
++) {
8237 space
= *space_list
;
8240 *space_list
= *space
;
8242 memcpy(du
, super
->disks
, sizeof(*du
));
8246 du
->index
= (i
* 2) + 1;
8247 sprintf((char *)du
->disk
.serial
,
8248 " MISSING_%d", du
->index
);
8249 sprintf((char *)du
->serial
,
8250 "MISSING_%d", du
->index
);
8251 du
->next
= super
->missing
;
8252 super
->missing
= du
;
8254 /* create new dev and map */
8255 space
= *space_list
;
8258 *space_list
= *space
;
8259 dev_new
= (void *)space
;
8260 memcpy(dev_new
, dev
, sizeof(*dev
));
8261 /* update new map */
8262 map
= get_imsm_map(dev_new
, MAP_0
);
8263 map
->num_members
= map
->num_members
* 2;
8264 map
->map_state
= IMSM_T_STATE_DEGRADED
;
8265 map
->num_domains
= 2;
8266 map
->raid_level
= 1;
8267 /* replace dev<->dev_new */
8270 /* update disk order table */
8271 for (du
= super
->disks
; du
; du
= du
->next
)
8273 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
8274 for (du
= super
->missing
; du
; du
= du
->next
)
8275 if (du
->index
>= 0) {
8276 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
8277 mark_missing(dv
->dev
, &du
->disk
, du
->index
);
8283 static void imsm_process_update(struct supertype
*st
,
8284 struct metadata_update
*update
)
8287 * crack open the metadata_update envelope to find the update record
8288 * update can be one of:
8289 * update_reshape_container_disks - all the arrays in the container
8290 * are being reshaped to have more devices. We need to mark
8291 * the arrays for general migration and convert selected spares
8292 * into active devices.
8293 * update_activate_spare - a spare device has replaced a failed
8294 * device in an array, update the disk_ord_tbl. If this disk is
8295 * present in all member arrays then also clear the SPARE_DISK
8297 * update_create_array
8299 * update_rename_array
8300 * update_add_remove_disk
8302 struct intel_super
*super
= st
->sb
;
8303 struct imsm_super
*mpb
;
8304 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
8306 /* update requires a larger buf but the allocation failed */
8307 if (super
->next_len
&& !super
->next_buf
) {
8308 super
->next_len
= 0;
8312 if (super
->next_buf
) {
8313 memcpy(super
->next_buf
, super
->buf
, super
->len
);
8315 super
->len
= super
->next_len
;
8316 super
->buf
= super
->next_buf
;
8318 super
->next_len
= 0;
8319 super
->next_buf
= NULL
;
8322 mpb
= super
->anchor
;
8325 case update_general_migration_checkpoint
: {
8326 struct intel_dev
*id
;
8327 struct imsm_update_general_migration_checkpoint
*u
=
8328 (void *)update
->buf
;
8330 dprintf("imsm: process_update() "
8331 "for update_general_migration_checkpoint called\n");
8333 /* find device under general migration */
8334 for (id
= super
->devlist
; id
; id
= id
->next
) {
8335 if (is_gen_migration(id
->dev
)) {
8336 id
->dev
->vol
.curr_migr_unit
=
8337 __cpu_to_le32(u
->curr_migr_unit
);
8338 super
->updates_pending
++;
8343 case update_takeover
: {
8344 struct imsm_update_takeover
*u
= (void *)update
->buf
;
8345 if (apply_takeover_update(u
, super
, &update
->space_list
)) {
8346 imsm_update_version_info(super
);
8347 super
->updates_pending
++;
8352 case update_reshape_container_disks
: {
8353 struct imsm_update_reshape
*u
= (void *)update
->buf
;
8354 if (apply_reshape_container_disks_update(
8355 u
, super
, &update
->space_list
))
8356 super
->updates_pending
++;
8359 case update_reshape_migration
: {
8360 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
8361 if (apply_reshape_migration_update(
8362 u
, super
, &update
->space_list
))
8363 super
->updates_pending
++;
8366 case update_activate_spare
: {
8367 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
8368 if (apply_update_activate_spare(u
, super
, st
->arrays
))
8369 super
->updates_pending
++;
8372 case update_create_array
: {
8373 /* someone wants to create a new array, we need to be aware of
8374 * a few races/collisions:
8375 * 1/ 'Create' called by two separate instances of mdadm
8376 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
8377 * devices that have since been assimilated via
8379 * In the event this update can not be carried out mdadm will
8380 * (FIX ME) notice that its update did not take hold.
8382 struct imsm_update_create_array
*u
= (void *) update
->buf
;
8383 struct intel_dev
*dv
;
8384 struct imsm_dev
*dev
;
8385 struct imsm_map
*map
, *new_map
;
8386 unsigned long long start
, end
;
8387 unsigned long long new_start
, new_end
;
8389 struct disk_info
*inf
;
8392 /* handle racing creates: first come first serve */
8393 if (u
->dev_idx
< mpb
->num_raid_devs
) {
8394 dprintf("%s: subarray %d already defined\n",
8395 __func__
, u
->dev_idx
);
8399 /* check update is next in sequence */
8400 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
8401 dprintf("%s: can not create array %d expected index %d\n",
8402 __func__
, u
->dev_idx
, mpb
->num_raid_devs
);
8406 new_map
= get_imsm_map(&u
->dev
, MAP_0
);
8407 new_start
= pba_of_lba0(new_map
);
8408 new_end
= new_start
+ blocks_per_member(new_map
);
8409 inf
= get_disk_info(u
);
8411 /* handle activate_spare versus create race:
8412 * check to make sure that overlapping arrays do not include
8415 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8416 dev
= get_imsm_dev(super
, i
);
8417 map
= get_imsm_map(dev
, MAP_0
);
8418 start
= pba_of_lba0(map
);
8419 end
= start
+ blocks_per_member(map
);
8420 if ((new_start
>= start
&& new_start
<= end
) ||
8421 (start
>= new_start
&& start
<= new_end
))
8426 if (disks_overlap(super
, i
, u
)) {
8427 dprintf("%s: arrays overlap\n", __func__
);
8432 /* check that prepare update was successful */
8433 if (!update
->space
) {
8434 dprintf("%s: prepare update failed\n", __func__
);
8438 /* check that all disks are still active before committing
8439 * changes. FIXME: could we instead handle this by creating a
8440 * degraded array? That's probably not what the user expects,
8441 * so better to drop this update on the floor.
8443 for (i
= 0; i
< new_map
->num_members
; i
++) {
8444 dl
= serial_to_dl(inf
[i
].serial
, super
);
8446 dprintf("%s: disk disappeared\n", __func__
);
8451 super
->updates_pending
++;
8453 /* convert spares to members and fixup ord_tbl */
8454 for (i
= 0; i
< new_map
->num_members
; i
++) {
8455 dl
= serial_to_dl(inf
[i
].serial
, super
);
8456 if (dl
->index
== -1) {
8457 dl
->index
= mpb
->num_disks
;
8459 dl
->disk
.status
|= CONFIGURED_DISK
;
8460 dl
->disk
.status
&= ~SPARE_DISK
;
8462 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
8467 update
->space
= NULL
;
8468 imsm_copy_dev(dev
, &u
->dev
);
8469 dv
->index
= u
->dev_idx
;
8470 dv
->next
= super
->devlist
;
8471 super
->devlist
= dv
;
8472 mpb
->num_raid_devs
++;
8474 imsm_update_version_info(super
);
8477 /* mdmon knows how to release update->space, but not
8478 * ((struct intel_dev *) update->space)->dev
8480 if (update
->space
) {
8486 case update_kill_array
: {
8487 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
8488 int victim
= u
->dev_idx
;
8489 struct active_array
*a
;
8490 struct intel_dev
**dp
;
8491 struct imsm_dev
*dev
;
8493 /* sanity check that we are not affecting the uuid of
8494 * active arrays, or deleting an active array
8496 * FIXME when immutable ids are available, but note that
8497 * we'll also need to fixup the invalidated/active
8498 * subarray indexes in mdstat
8500 for (a
= st
->arrays
; a
; a
= a
->next
)
8501 if (a
->info
.container_member
>= victim
)
8503 /* by definition if mdmon is running at least one array
8504 * is active in the container, so checking
8505 * mpb->num_raid_devs is just extra paranoia
8507 dev
= get_imsm_dev(super
, victim
);
8508 if (a
|| !dev
|| mpb
->num_raid_devs
== 1) {
8509 dprintf("failed to delete subarray-%d\n", victim
);
8513 for (dp
= &super
->devlist
; *dp
;)
8514 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
8517 if ((*dp
)->index
> (unsigned)victim
)
8521 mpb
->num_raid_devs
--;
8522 super
->updates_pending
++;
8525 case update_rename_array
: {
8526 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
8527 char name
[MAX_RAID_SERIAL_LEN
+1];
8528 int target
= u
->dev_idx
;
8529 struct active_array
*a
;
8530 struct imsm_dev
*dev
;
8532 /* sanity check that we are not affecting the uuid of
8535 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
8536 name
[MAX_RAID_SERIAL_LEN
] = '\0';
8537 for (a
= st
->arrays
; a
; a
= a
->next
)
8538 if (a
->info
.container_member
== target
)
8540 dev
= get_imsm_dev(super
, u
->dev_idx
);
8541 if (a
|| !dev
|| !check_name(super
, name
, 1)) {
8542 dprintf("failed to rename subarray-%d\n", target
);
8546 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
8547 super
->updates_pending
++;
8550 case update_add_remove_disk
: {
8551 /* we may be able to repair some arrays if disks are
8552 * being added, check teh status of add_remove_disk
8553 * if discs has been added.
8555 if (add_remove_disk_update(super
)) {
8556 struct active_array
*a
;
8558 super
->updates_pending
++;
8559 for (a
= st
->arrays
; a
; a
= a
->next
)
8560 a
->check_degraded
= 1;
8565 fprintf(stderr
, "error: unsuported process update type:"
8566 "(type: %d)\n", type
);
8570 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
);
8572 static void imsm_prepare_update(struct supertype
*st
,
8573 struct metadata_update
*update
)
8576 * Allocate space to hold new disk entries, raid-device entries or a new
8577 * mpb if necessary. The manager synchronously waits for updates to
8578 * complete in the monitor, so new mpb buffers allocated here can be
8579 * integrated by the monitor thread without worrying about live pointers
8580 * in the manager thread.
8582 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
8583 struct intel_super
*super
= st
->sb
;
8584 struct imsm_super
*mpb
= super
->anchor
;
8589 case update_general_migration_checkpoint
:
8590 dprintf("imsm: prepare_update() "
8591 "for update_general_migration_checkpoint called\n");
8593 case update_takeover
: {
8594 struct imsm_update_takeover
*u
= (void *)update
->buf
;
8595 if (u
->direction
== R0_TO_R10
) {
8596 void **tail
= (void **)&update
->space_list
;
8597 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subarray
);
8598 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8599 int num_members
= map
->num_members
;
8603 /* allocate memory for added disks */
8604 for (i
= 0; i
< num_members
; i
++) {
8605 size
= sizeof(struct dl
);
8606 space
= malloc(size
);
8615 /* allocate memory for new device */
8616 size
= sizeof_imsm_dev(super
->devlist
->dev
, 0) +
8617 (num_members
* sizeof(__u32
));
8618 space
= malloc(size
);
8627 len
= disks_to_mpb_size(num_members
* 2);
8629 /* if allocation didn't success, free buffer */
8630 while (update
->space_list
) {
8631 void **sp
= update
->space_list
;
8632 update
->space_list
= *sp
;
8640 case update_reshape_container_disks
: {
8641 /* Every raid device in the container is about to
8642 * gain some more devices, and we will enter a
8644 * So each 'imsm_map' will be bigger, and the imsm_vol
8645 * will now hold 2 of them.
8646 * Thus we need new 'struct imsm_dev' allocations sized
8647 * as sizeof_imsm_dev but with more devices in both maps.
8649 struct imsm_update_reshape
*u
= (void *)update
->buf
;
8650 struct intel_dev
*dl
;
8651 void **space_tail
= (void**)&update
->space_list
;
8653 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
8655 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
8656 int size
= sizeof_imsm_dev(dl
->dev
, 1);
8658 if (u
->new_raid_disks
> u
->old_raid_disks
)
8659 size
+= sizeof(__u32
)*2*
8660 (u
->new_raid_disks
- u
->old_raid_disks
);
8669 len
= disks_to_mpb_size(u
->new_raid_disks
);
8670 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
8673 case update_reshape_migration
: {
8674 /* for migration level 0->5 we need to add disks
8675 * so the same as for container operation we will copy
8676 * device to the bigger location.
8677 * in memory prepared device and new disk area are prepared
8678 * for usage in process update
8680 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
8681 struct intel_dev
*id
;
8682 void **space_tail
= (void **)&update
->space_list
;
8685 int current_level
= -1;
8687 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
8689 /* add space for bigger array in update
8691 for (id
= super
->devlist
; id
; id
= id
->next
) {
8692 if (id
->index
== (unsigned)u
->subdev
) {
8693 size
= sizeof_imsm_dev(id
->dev
, 1);
8694 if (u
->new_raid_disks
> u
->old_raid_disks
)
8695 size
+= sizeof(__u32
)*2*
8696 (u
->new_raid_disks
- u
->old_raid_disks
);
8706 if (update
->space_list
== NULL
)
8709 /* add space for disk in update
8711 size
= sizeof(struct dl
);
8714 free(update
->space_list
);
8715 update
->space_list
= NULL
;
8722 /* add spare device to update
8724 for (id
= super
->devlist
; id
; id
= id
->next
)
8725 if (id
->index
== (unsigned)u
->subdev
) {
8726 struct imsm_dev
*dev
;
8727 struct imsm_map
*map
;
8729 dev
= get_imsm_dev(super
, u
->subdev
);
8730 map
= get_imsm_map(dev
, MAP_0
);
8731 current_level
= map
->raid_level
;
8734 if ((u
->new_level
== 5) && (u
->new_level
!= current_level
)) {
8735 struct mdinfo
*spares
;
8737 spares
= get_spares_for_grow(st
);
8745 makedev(dev
->disk
.major
,
8747 dl
= get_disk_super(super
,
8750 dl
->index
= u
->old_raid_disks
;
8756 len
= disks_to_mpb_size(u
->new_raid_disks
);
8757 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
8760 case update_create_array
: {
8761 struct imsm_update_create_array
*u
= (void *) update
->buf
;
8762 struct intel_dev
*dv
;
8763 struct imsm_dev
*dev
= &u
->dev
;
8764 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8766 struct disk_info
*inf
;
8770 inf
= get_disk_info(u
);
8771 len
= sizeof_imsm_dev(dev
, 1);
8772 /* allocate a new super->devlist entry */
8773 dv
= malloc(sizeof(*dv
));
8775 dv
->dev
= malloc(len
);
8780 update
->space
= NULL
;
8784 /* count how many spares will be converted to members */
8785 for (i
= 0; i
< map
->num_members
; i
++) {
8786 dl
= serial_to_dl(inf
[i
].serial
, super
);
8788 /* hmm maybe it failed?, nothing we can do about
8793 if (count_memberships(dl
, super
) == 0)
8796 len
+= activate
* sizeof(struct imsm_disk
);
8803 /* check if we need a larger metadata buffer */
8804 if (super
->next_buf
)
8805 buf_len
= super
->next_len
;
8807 buf_len
= super
->len
;
8809 if (__le32_to_cpu(mpb
->mpb_size
) + len
> buf_len
) {
8810 /* ok we need a larger buf than what is currently allocated
8811 * if this allocation fails process_update will notice that
8812 * ->next_len is set and ->next_buf is NULL
8814 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + len
, 512);
8815 if (super
->next_buf
)
8816 free(super
->next_buf
);
8818 super
->next_len
= buf_len
;
8819 if (posix_memalign(&super
->next_buf
, 512, buf_len
) == 0)
8820 memset(super
->next_buf
, 0, buf_len
);
8822 super
->next_buf
= NULL
;
8826 /* must be called while manager is quiesced */
8827 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
8829 struct imsm_super
*mpb
= super
->anchor
;
8831 struct imsm_dev
*dev
;
8832 struct imsm_map
*map
;
8833 int i
, j
, num_members
;
8836 dprintf("%s: deleting device[%d] from imsm_super\n",
8839 /* shift all indexes down one */
8840 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
8841 if (iter
->index
> (int)index
)
8843 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
8844 if (iter
->index
> (int)index
)
8847 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8848 dev
= get_imsm_dev(super
, i
);
8849 map
= get_imsm_map(dev
, MAP_0
);
8850 num_members
= map
->num_members
;
8851 for (j
= 0; j
< num_members
; j
++) {
8852 /* update ord entries being careful not to propagate
8853 * ord-flags to the first map
8855 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_X
);
8857 if (ord_to_idx(ord
) <= index
)
8860 map
= get_imsm_map(dev
, MAP_0
);
8861 set_imsm_ord_tbl_ent(map
, j
, ord_to_idx(ord
- 1));
8862 map
= get_imsm_map(dev
, MAP_1
);
8864 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
8869 super
->updates_pending
++;
8871 struct dl
*dl
= *dlp
;
8873 *dlp
= (*dlp
)->next
;
8874 __free_imsm_disk(dl
);
8877 #endif /* MDASSEMBLE */
8879 static void close_targets(int *targets
, int new_disks
)
8886 for (i
= 0; i
< new_disks
; i
++) {
8887 if (targets
[i
] >= 0) {
8894 static int imsm_get_allowed_degradation(int level
, int raid_disks
,
8895 struct intel_super
*super
,
8896 struct imsm_dev
*dev
)
8902 struct imsm_map
*map
;
8905 ret_val
= raid_disks
/2;
8906 /* check map if all disks pairs not failed
8909 map
= get_imsm_map(dev
, MAP_0
);
8910 for (i
= 0; i
< ret_val
; i
++) {
8911 int degradation
= 0;
8912 if (get_imsm_disk(super
, i
) == NULL
)
8914 if (get_imsm_disk(super
, i
+ 1) == NULL
)
8916 if (degradation
== 2)
8919 map
= get_imsm_map(dev
, MAP_1
);
8920 /* if there is no second map
8921 * result can be returned
8925 /* check degradation in second map
8927 for (i
= 0; i
< ret_val
; i
++) {
8928 int degradation
= 0;
8929 if (get_imsm_disk(super
, i
) == NULL
)
8931 if (get_imsm_disk(super
, i
+ 1) == NULL
)
8933 if (degradation
== 2)
8948 /*******************************************************************************
8949 * Function: open_backup_targets
8950 * Description: Function opens file descriptors for all devices given in
8953 * info : general array info
8954 * raid_disks : number of disks
8955 * raid_fds : table of device's file descriptors
8956 * super : intel super for raid10 degradation check
8957 * dev : intel device for raid10 degradation check
8961 ******************************************************************************/
8962 int open_backup_targets(struct mdinfo
*info
, int raid_disks
, int *raid_fds
,
8963 struct intel_super
*super
, struct imsm_dev
*dev
)
8969 for (i
= 0; i
< raid_disks
; i
++)
8972 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
8975 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
)) {
8976 dprintf("disk is faulty!!\n");
8980 if ((sd
->disk
.raid_disk
>= raid_disks
) ||
8981 (sd
->disk
.raid_disk
< 0))
8984 dn
= map_dev(sd
->disk
.major
,
8986 raid_fds
[sd
->disk
.raid_disk
] = dev_open(dn
, O_RDWR
);
8987 if (raid_fds
[sd
->disk
.raid_disk
] < 0) {
8988 fprintf(stderr
, "cannot open component\n");
8993 /* check if maximum array degradation level is not exceeded
8995 if ((raid_disks
- opened
) >
8996 imsm_get_allowed_degradation(info
->new_level
,
8999 fprintf(stderr
, "Not enough disks can be opened.\n");
9000 close_targets(raid_fds
, raid_disks
);
9007 /*******************************************************************************
9008 * Function: init_migr_record_imsm
9009 * Description: Function inits imsm migration record
9011 * super : imsm internal array info
9012 * dev : device under migration
9013 * info : general array info to find the smallest device
9016 ******************************************************************************/
9017 void init_migr_record_imsm(struct supertype
*st
, struct imsm_dev
*dev
,
9018 struct mdinfo
*info
)
9020 struct intel_super
*super
= st
->sb
;
9021 struct migr_record
*migr_rec
= super
->migr_rec
;
9023 unsigned long long dsize
, dev_sectors
;
9024 long long unsigned min_dev_sectors
= -1LLU;
9028 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
9029 struct imsm_map
*map_src
= get_imsm_map(dev
, MAP_1
);
9030 unsigned long long num_migr_units
;
9031 unsigned long long array_blocks
;
9033 memset(migr_rec
, 0, sizeof(struct migr_record
));
9034 migr_rec
->family_num
= __cpu_to_le32(super
->anchor
->family_num
);
9036 /* only ascending reshape supported now */
9037 migr_rec
->ascending_migr
= __cpu_to_le32(1);
9039 migr_rec
->dest_depth_per_unit
= GEN_MIGR_AREA_SIZE
/
9040 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
9041 migr_rec
->dest_depth_per_unit
*=
9042 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
9043 new_data_disks
= imsm_num_data_members(dev
, MAP_0
);
9044 migr_rec
->blocks_per_unit
=
9045 __cpu_to_le32(migr_rec
->dest_depth_per_unit
* new_data_disks
);
9046 migr_rec
->dest_depth_per_unit
=
9047 __cpu_to_le32(migr_rec
->dest_depth_per_unit
);
9048 array_blocks
= info
->component_size
* new_data_disks
;
9050 array_blocks
/ __le32_to_cpu(migr_rec
->blocks_per_unit
);
9052 if (array_blocks
% __le32_to_cpu(migr_rec
->blocks_per_unit
))
9054 migr_rec
->num_migr_units
= __cpu_to_le32(num_migr_units
);
9056 migr_rec
->post_migr_vol_cap
= dev
->size_low
;
9057 migr_rec
->post_migr_vol_cap_hi
= dev
->size_high
;
9060 /* Find the smallest dev */
9061 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
9062 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
9063 fd
= dev_open(nm
, O_RDONLY
);
9066 get_dev_size(fd
, NULL
, &dsize
);
9067 dev_sectors
= dsize
/ 512;
9068 if (dev_sectors
< min_dev_sectors
)
9069 min_dev_sectors
= dev_sectors
;
9072 migr_rec
->ckpt_area_pba
= __cpu_to_le32(min_dev_sectors
-
9073 RAID_DISK_RESERVED_BLOCKS_IMSM_HI
);
9075 write_imsm_migr_rec(st
);
9080 /*******************************************************************************
9081 * Function: save_backup_imsm
9082 * Description: Function saves critical data stripes to Migration Copy Area
9083 * and updates the current migration unit status.
9084 * Use restore_stripes() to form a destination stripe,
9085 * and to write it to the Copy Area.
9087 * st : supertype information
9088 * dev : imsm device that backup is saved for
9089 * info : general array info
9090 * buf : input buffer
9091 * length : length of data to backup (blocks_per_unit)
9095 ******************************************************************************/
9096 int save_backup_imsm(struct supertype
*st
,
9097 struct imsm_dev
*dev
,
9098 struct mdinfo
*info
,
9103 struct intel_super
*super
= st
->sb
;
9104 unsigned long long *target_offsets
= NULL
;
9105 int *targets
= NULL
;
9107 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
9108 int new_disks
= map_dest
->num_members
;
9109 int dest_layout
= 0;
9111 unsigned long long start
;
9112 int data_disks
= imsm_num_data_members(dev
, MAP_0
);
9114 targets
= malloc(new_disks
* sizeof(int));
9118 for (i
= 0; i
< new_disks
; i
++)
9121 target_offsets
= malloc(new_disks
* sizeof(unsigned long long));
9122 if (!target_offsets
)
9125 start
= info
->reshape_progress
* 512;
9126 for (i
= 0; i
< new_disks
; i
++) {
9127 target_offsets
[i
] = (unsigned long long)
9128 __le32_to_cpu(super
->migr_rec
->ckpt_area_pba
) * 512;
9129 /* move back copy area adderss, it will be moved forward
9130 * in restore_stripes() using start input variable
9132 target_offsets
[i
] -= start
/data_disks
;
9135 if (open_backup_targets(info
, new_disks
, targets
,
9139 dest_layout
= imsm_level_to_layout(map_dest
->raid_level
);
9140 dest_chunk
= __le16_to_cpu(map_dest
->blocks_per_strip
) * 512;
9142 if (restore_stripes(targets
, /* list of dest devices */
9143 target_offsets
, /* migration record offsets */
9146 map_dest
->raid_level
,
9148 -1, /* source backup file descriptor */
9149 0, /* input buf offset
9150 * always 0 buf is already offseted */
9154 fprintf(stderr
, Name
": Error restoring stripes\n");
9162 close_targets(targets
, new_disks
);
9165 free(target_offsets
);
9170 /*******************************************************************************
9171 * Function: save_checkpoint_imsm
9172 * Description: Function called for current unit status update
9173 * in the migration record. It writes it to disk.
9175 * super : imsm internal array info
9176 * info : general array info
9180 * 2: failure, means no valid migration record
9181 * / no general migration in progress /
9182 ******************************************************************************/
9183 int save_checkpoint_imsm(struct supertype
*st
, struct mdinfo
*info
, int state
)
9185 struct intel_super
*super
= st
->sb
;
9186 unsigned long long blocks_per_unit
;
9187 unsigned long long curr_migr_unit
;
9189 if (load_imsm_migr_rec(super
, info
) != 0) {
9190 dprintf("imsm: ERROR: Cannot read migration record "
9191 "for checkpoint save.\n");
9195 blocks_per_unit
= __le32_to_cpu(super
->migr_rec
->blocks_per_unit
);
9196 if (blocks_per_unit
== 0) {
9197 dprintf("imsm: no migration in progress.\n");
9200 curr_migr_unit
= info
->reshape_progress
/ blocks_per_unit
;
9201 /* check if array is alligned to copy area
9202 * if it is not alligned, add one to current migration unit value
9203 * this can happend on array reshape finish only
9205 if (info
->reshape_progress
% blocks_per_unit
)
9208 super
->migr_rec
->curr_migr_unit
=
9209 __cpu_to_le32(curr_migr_unit
);
9210 super
->migr_rec
->rec_status
= __cpu_to_le32(state
);
9211 super
->migr_rec
->dest_1st_member_lba
=
9212 __cpu_to_le32(curr_migr_unit
*
9213 __le32_to_cpu(super
->migr_rec
->dest_depth_per_unit
));
9214 if (write_imsm_migr_rec(st
) < 0) {
9215 dprintf("imsm: Cannot write migration record "
9216 "outside backup area\n");
9223 /*******************************************************************************
9224 * Function: recover_backup_imsm
9225 * Description: Function recovers critical data from the Migration Copy Area
9226 * while assembling an array.
9228 * super : imsm internal array info
9229 * info : general array info
9231 * 0 : success (or there is no data to recover)
9233 ******************************************************************************/
9234 int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
)
9236 struct intel_super
*super
= st
->sb
;
9237 struct migr_record
*migr_rec
= super
->migr_rec
;
9238 struct imsm_map
*map_dest
= NULL
;
9239 struct intel_dev
*id
= NULL
;
9240 unsigned long long read_offset
;
9241 unsigned long long write_offset
;
9243 int *targets
= NULL
;
9244 int new_disks
, i
, err
;
9247 unsigned long curr_migr_unit
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
9248 unsigned long num_migr_units
= __le32_to_cpu(migr_rec
->num_migr_units
);
9250 int skipped_disks
= 0;
9252 err
= sysfs_get_str(info
, NULL
, "array_state", (char *)buffer
, 20);
9256 /* recover data only during assemblation */
9257 if (strncmp(buffer
, "inactive", 8) != 0)
9259 /* no data to recover */
9260 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
9262 if (curr_migr_unit
>= num_migr_units
)
9265 /* find device during reshape */
9266 for (id
= super
->devlist
; id
; id
= id
->next
)
9267 if (is_gen_migration(id
->dev
))
9272 map_dest
= get_imsm_map(id
->dev
, MAP_0
);
9273 new_disks
= map_dest
->num_members
;
9275 read_offset
= (unsigned long long)
9276 __le32_to_cpu(migr_rec
->ckpt_area_pba
) * 512;
9278 write_offset
= ((unsigned long long)
9279 __le32_to_cpu(migr_rec
->dest_1st_member_lba
) +
9280 pba_of_lba0(map_dest
)) * 512;
9282 unit_len
= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
9283 if (posix_memalign((void **)&buf
, 512, unit_len
) != 0)
9285 targets
= malloc(new_disks
* sizeof(int));
9289 if (open_backup_targets(info
, new_disks
, targets
, super
, id
->dev
)) {
9291 Name
": Cannot open some devices belonging to array.\n");
9295 for (i
= 0; i
< new_disks
; i
++) {
9296 if (targets
[i
] < 0) {
9300 if (lseek64(targets
[i
], read_offset
, SEEK_SET
) < 0) {
9302 Name
": Cannot seek to block: %s\n",
9307 if ((unsigned)read(targets
[i
], buf
, unit_len
) != unit_len
) {
9309 Name
": Cannot read copy area block: %s\n",
9314 if (lseek64(targets
[i
], write_offset
, SEEK_SET
) < 0) {
9316 Name
": Cannot seek to block: %s\n",
9321 if ((unsigned)write(targets
[i
], buf
, unit_len
) != unit_len
) {
9323 Name
": Cannot restore block: %s\n",
9330 if (skipped_disks
> imsm_get_allowed_degradation(info
->new_level
,
9335 Name
": Cannot restore data from backup."
9336 " Too many failed disks\n");
9340 if (save_checkpoint_imsm(st
, info
, UNIT_SRC_NORMAL
)) {
9341 /* ignore error == 2, this can mean end of reshape here
9343 dprintf("imsm: Cannot write checkpoint to "
9344 "migration record (UNIT_SRC_NORMAL) during restart\n");
9350 for (i
= 0; i
< new_disks
; i
++)
9359 static char disk_by_path
[] = "/dev/disk/by-path/";
9361 static const char *imsm_get_disk_controller_domain(const char *path
)
9363 char disk_path
[PATH_MAX
];
9367 strncpy(disk_path
, disk_by_path
, PATH_MAX
- 1);
9368 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
9369 if (stat(disk_path
, &st
) == 0) {
9370 struct sys_dev
* hba
;
9373 path
= devt_to_devpath(st
.st_rdev
);
9376 hba
= find_disk_attached_hba(-1, path
);
9377 if (hba
&& hba
->type
== SYS_DEV_SAS
)
9379 else if (hba
&& hba
->type
== SYS_DEV_SATA
)
9383 dprintf("path: %s hba: %s attached: %s\n",
9384 path
, (hba
) ? hba
->path
: "NULL", drv
);
9392 static int imsm_find_array_minor_by_subdev(int subdev
, int container
, int *minor
)
9394 char subdev_name
[20];
9395 struct mdstat_ent
*mdstat
;
9397 sprintf(subdev_name
, "%d", subdev
);
9398 mdstat
= mdstat_by_subdev(subdev_name
, container
);
9402 *minor
= mdstat
->devnum
;
9403 free_mdstat(mdstat
);
9407 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
9408 struct geo_params
*geo
,
9409 int *old_raid_disks
)
9411 /* currently we only support increasing the number of devices
9412 * for a container. This increases the number of device for each
9413 * member array. They must all be RAID0 or RAID5.
9416 struct mdinfo
*info
, *member
;
9417 int devices_that_can_grow
= 0;
9419 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): "
9420 "st->devnum = (%i)\n",
9423 if (geo
->size
!= -1 ||
9424 geo
->level
!= UnSet
||
9425 geo
->layout
!= UnSet
||
9426 geo
->chunksize
!= 0 ||
9427 geo
->raid_disks
== UnSet
) {
9428 dprintf("imsm: Container operation is allowed for "
9429 "raid disks number change only.\n");
9433 info
= container_content_imsm(st
, NULL
);
9434 for (member
= info
; member
; member
= member
->next
) {
9438 dprintf("imsm: checking device_num: %i\n",
9439 member
->container_member
);
9441 if (geo
->raid_disks
<= member
->array
.raid_disks
) {
9442 /* we work on container for Online Capacity Expansion
9443 * only so raid_disks has to grow
9445 dprintf("imsm: for container operation raid disks "
9446 "increase is required\n");
9450 if ((info
->array
.level
!= 0) &&
9451 (info
->array
.level
!= 5)) {
9452 /* we cannot use this container with other raid level
9454 dprintf("imsm: for container operation wrong"
9455 " raid level (%i) detected\n",
9459 /* check for platform support
9460 * for this raid level configuration
9462 struct intel_super
*super
= st
->sb
;
9463 if (!is_raid_level_supported(super
->orom
,
9464 member
->array
.level
,
9466 dprintf("platform does not support raid%d with"
9470 geo
->raid_disks
> 1 ? "s" : "");
9473 /* check if component size is aligned to chunk size
9475 if (info
->component_size
%
9476 (info
->array
.chunk_size
/512)) {
9477 dprintf("Component size is not aligned to "
9483 if (*old_raid_disks
&&
9484 info
->array
.raid_disks
!= *old_raid_disks
)
9486 *old_raid_disks
= info
->array
.raid_disks
;
9488 /* All raid5 and raid0 volumes in container
9489 * have to be ready for Online Capacity Expansion
9490 * so they need to be assembled. We have already
9491 * checked that no recovery etc is happening.
9493 result
= imsm_find_array_minor_by_subdev(member
->container_member
,
9497 dprintf("imsm: cannot find array\n");
9500 devices_that_can_grow
++;
9503 if (!member
&& devices_that_can_grow
)
9507 dprintf("\tContainer operation allowed\n");
9509 dprintf("\tError: %i\n", ret_val
);
9514 /* Function: get_spares_for_grow
9515 * Description: Allocates memory and creates list of spare devices
9516 * avaliable in container. Checks if spare drive size is acceptable.
9517 * Parameters: Pointer to the supertype structure
9518 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
9521 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
9523 unsigned long long min_size
= min_acceptable_spare_size_imsm(st
);
9524 return container_choose_spares(st
, min_size
, NULL
, NULL
, NULL
, 0);
9527 /******************************************************************************
9528 * function: imsm_create_metadata_update_for_reshape
9529 * Function creates update for whole IMSM container.
9531 ******************************************************************************/
9532 static int imsm_create_metadata_update_for_reshape(
9533 struct supertype
*st
,
9534 struct geo_params
*geo
,
9536 struct imsm_update_reshape
**updatep
)
9538 struct intel_super
*super
= st
->sb
;
9539 struct imsm_super
*mpb
= super
->anchor
;
9540 int update_memory_size
= 0;
9541 struct imsm_update_reshape
*u
= NULL
;
9542 struct mdinfo
*spares
= NULL
;
9544 int delta_disks
= 0;
9547 dprintf("imsm_update_metadata_for_reshape(enter) raid_disks = %i\n",
9550 delta_disks
= geo
->raid_disks
- old_raid_disks
;
9552 /* size of all update data without anchor */
9553 update_memory_size
= sizeof(struct imsm_update_reshape
);
9555 /* now add space for spare disks that we need to add. */
9556 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
9558 u
= calloc(1, update_memory_size
);
9561 "cannot get memory for imsm_update_reshape update\n");
9564 u
->type
= update_reshape_container_disks
;
9565 u
->old_raid_disks
= old_raid_disks
;
9566 u
->new_raid_disks
= geo
->raid_disks
;
9568 /* now get spare disks list
9570 spares
= get_spares_for_grow(st
);
9573 || delta_disks
> spares
->array
.spare_disks
) {
9574 fprintf(stderr
, Name
": imsm: ERROR: Cannot get spare devices "
9575 "for %s.\n", geo
->dev_name
);
9580 /* we have got spares
9581 * update disk list in imsm_disk list table in anchor
9583 dprintf("imsm: %i spares are available.\n\n",
9584 spares
->array
.spare_disks
);
9587 for (i
= 0; i
< delta_disks
; i
++) {
9592 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
9594 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
9595 dl
->index
= mpb
->num_disks
;
9605 dprintf("imsm: reshape update preparation :");
9606 if (i
== delta_disks
) {
9609 return update_memory_size
;
9612 dprintf(" Error\n");
9617 /******************************************************************************
9618 * function: imsm_create_metadata_update_for_migration()
9619 * Creates update for IMSM array.
9621 ******************************************************************************/
9622 static int imsm_create_metadata_update_for_migration(
9623 struct supertype
*st
,
9624 struct geo_params
*geo
,
9625 struct imsm_update_reshape_migration
**updatep
)
9627 struct intel_super
*super
= st
->sb
;
9628 int update_memory_size
= 0;
9629 struct imsm_update_reshape_migration
*u
= NULL
;
9630 struct imsm_dev
*dev
;
9631 int previous_level
= -1;
9633 dprintf("imsm_create_metadata_update_for_migration(enter)"
9634 " New Level = %i\n", geo
->level
);
9636 /* size of all update data without anchor */
9637 update_memory_size
= sizeof(struct imsm_update_reshape_migration
);
9639 u
= calloc(1, update_memory_size
);
9641 dprintf("error: cannot get memory for "
9642 "imsm_create_metadata_update_for_migration\n");
9645 u
->type
= update_reshape_migration
;
9646 u
->subdev
= super
->current_vol
;
9647 u
->new_level
= geo
->level
;
9648 u
->new_layout
= geo
->layout
;
9649 u
->new_raid_disks
= u
->old_raid_disks
= geo
->raid_disks
;
9650 u
->new_disks
[0] = -1;
9651 u
->new_chunksize
= -1;
9653 dev
= get_imsm_dev(super
, u
->subdev
);
9655 struct imsm_map
*map
;
9657 map
= get_imsm_map(dev
, MAP_0
);
9659 int current_chunk_size
=
9660 __le16_to_cpu(map
->blocks_per_strip
) / 2;
9662 if (geo
->chunksize
!= current_chunk_size
) {
9663 u
->new_chunksize
= geo
->chunksize
/ 1024;
9665 "chunk size change from %i to %i\n",
9666 current_chunk_size
, u
->new_chunksize
);
9668 previous_level
= map
->raid_level
;
9671 if ((geo
->level
== 5) && (previous_level
== 0)) {
9672 struct mdinfo
*spares
= NULL
;
9674 u
->new_raid_disks
++;
9675 spares
= get_spares_for_grow(st
);
9676 if ((spares
== NULL
) || (spares
->array
.spare_disks
< 1)) {
9679 update_memory_size
= 0;
9680 dprintf("error: cannot get spare device "
9681 "for requested migration");
9686 dprintf("imsm: reshape update preparation : OK\n");
9689 return update_memory_size
;
9692 static void imsm_update_metadata_locally(struct supertype
*st
,
9695 struct metadata_update mu
;
9700 mu
.space_list
= NULL
;
9702 imsm_prepare_update(st
, &mu
);
9703 imsm_process_update(st
, &mu
);
9705 while (mu
.space_list
) {
9706 void **space
= mu
.space_list
;
9707 mu
.space_list
= *space
;
9712 /***************************************************************************
9713 * Function: imsm_analyze_change
9714 * Description: Function analyze change for single volume
9715 * and validate if transition is supported
9716 * Parameters: Geometry parameters, supertype structure
9717 * Returns: Operation type code on success, -1 if fail
9718 ****************************************************************************/
9719 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
9720 struct geo_params
*geo
)
9726 /* number of added/removed disks in operation result */
9727 int devNumChange
= 0;
9728 /* imsm compatible layout value for array geometry verification */
9729 int imsm_layout
= -1;
9731 struct imsm_dev
*dev
;
9732 struct intel_super
*super
;
9733 long long current_size
;
9735 getinfo_super_imsm_volume(st
, &info
, NULL
);
9736 if ((geo
->level
!= info
.array
.level
) &&
9737 (geo
->level
>= 0) &&
9738 (geo
->level
!= UnSet
)) {
9739 switch (info
.array
.level
) {
9741 if (geo
->level
== 5) {
9742 change
= CH_MIGRATION
;
9743 if (geo
->layout
!= ALGORITHM_LEFT_ASYMMETRIC
) {
9745 Name
" Error. Requested Layout "
9746 "not supported (left-asymmetric layout "
9747 "is supported only)!\n");
9749 goto analyse_change_exit
;
9751 imsm_layout
= geo
->layout
;
9753 devNumChange
= 1; /* parity disk added */
9754 } else if (geo
->level
== 10) {
9755 change
= CH_TAKEOVER
;
9757 devNumChange
= 2; /* two mirrors added */
9758 imsm_layout
= 0x102; /* imsm supported layout */
9763 if (geo
->level
== 0) {
9764 change
= CH_TAKEOVER
;
9766 devNumChange
= -(geo
->raid_disks
/2);
9767 imsm_layout
= 0; /* imsm raid0 layout */
9773 Name
" Error. Level Migration from %d to %d "
9775 info
.array
.level
, geo
->level
);
9776 goto analyse_change_exit
;
9779 geo
->level
= info
.array
.level
;
9781 if ((geo
->layout
!= info
.array
.layout
)
9782 && ((geo
->layout
!= UnSet
) && (geo
->layout
!= -1))) {
9783 change
= CH_MIGRATION
;
9784 if ((info
.array
.layout
== 0)
9785 && (info
.array
.level
== 5)
9786 && (geo
->layout
== 5)) {
9787 /* reshape 5 -> 4 */
9788 } else if ((info
.array
.layout
== 5)
9789 && (info
.array
.level
== 5)
9790 && (geo
->layout
== 0)) {
9791 /* reshape 4 -> 5 */
9796 Name
" Error. Layout Migration from %d to %d "
9798 info
.array
.layout
, geo
->layout
);
9800 goto analyse_change_exit
;
9803 geo
->layout
= info
.array
.layout
;
9804 if (imsm_layout
== -1)
9805 imsm_layout
= info
.array
.layout
;
9808 if ((geo
->chunksize
> 0) && (geo
->chunksize
!= UnSet
)
9809 && (geo
->chunksize
!= info
.array
.chunk_size
))
9810 change
= CH_MIGRATION
;
9812 geo
->chunksize
= info
.array
.chunk_size
;
9814 chunk
= geo
->chunksize
/ 1024;
9817 dev
= get_imsm_dev(super
, super
->current_vol
);
9818 data_disks
= imsm_num_data_members(dev
, MAP_0
);
9819 /* compute current size in K per disk member
9821 current_size
= info
.custom_array_size
/ 2 / data_disks
;
9823 if ((current_size
!= geo
->size
) && (geo
->size
> 0)) {
9826 Name
" Error. Size change should be the only "
9827 "one at a time.\n");
9829 goto analyse_change_exit
;
9831 if ((super
->current_vol
+ 1) != super
->anchor
->num_raid_devs
) {
9833 Name
" Error. The last volume in container "
9834 "can be expanded only (%i/%i).\n",
9835 super
->current_vol
, st
->devnum
);
9836 goto analyse_change_exit
;
9839 /* round size due to metadata compatibility
9841 geo
->size
= (geo
->size
>> SECT_PER_MB_SHIFT
)
9842 << SECT_PER_MB_SHIFT
;
9843 dprintf("Prepare update for size change to %llu\n", geo
->size
);
9844 if (current_size
>= geo
->size
) {
9846 Name
" Error. Size expanssion is supported only"
9847 " (current size is %llu, requested size "
9848 "/rounded/ is %llu).\n",
9849 current_size
, geo
->size
);
9850 goto analyse_change_exit
;
9852 geo
->size
*= data_disks
;
9853 geo
->raid_disks
= dev
->vol
.map
->num_members
;
9854 change
= CH_ARRAY_SIZE
;
9856 if (!validate_geometry_imsm(st
,
9859 geo
->raid_disks
+ devNumChange
,
9866 struct intel_super
*super
= st
->sb
;
9867 struct imsm_super
*mpb
= super
->anchor
;
9869 if (mpb
->num_raid_devs
> 1) {
9871 Name
" Error. Cannot perform operation on %s"
9872 "- for this operation it MUST be single "
9873 "array in container\n",
9879 analyse_change_exit
:
9884 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
9886 struct intel_super
*super
= st
->sb
;
9887 struct imsm_update_takeover
*u
;
9889 u
= malloc(sizeof(struct imsm_update_takeover
));
9893 u
->type
= update_takeover
;
9894 u
->subarray
= super
->current_vol
;
9896 /* 10->0 transition */
9897 if (geo
->level
== 0)
9898 u
->direction
= R10_TO_R0
;
9900 /* 0->10 transition */
9901 if (geo
->level
== 10)
9902 u
->direction
= R0_TO_R10
;
9904 /* update metadata locally */
9905 imsm_update_metadata_locally(st
, u
,
9906 sizeof(struct imsm_update_takeover
));
9907 /* and possibly remotely */
9908 if (st
->update_tail
)
9909 append_metadata_update(st
, u
,
9910 sizeof(struct imsm_update_takeover
));
9917 static int imsm_reshape_super(struct supertype
*st
, long long size
, int level
,
9918 int layout
, int chunksize
, int raid_disks
,
9919 int delta_disks
, char *backup
, char *dev
,
9923 struct geo_params geo
;
9925 dprintf("imsm: reshape_super called.\n");
9927 memset(&geo
, 0, sizeof(struct geo_params
));
9930 geo
.dev_id
= st
->devnum
;
9933 geo
.layout
= layout
;
9934 geo
.chunksize
= chunksize
;
9935 geo
.raid_disks
= raid_disks
;
9936 if (delta_disks
!= UnSet
)
9937 geo
.raid_disks
+= delta_disks
;
9939 dprintf("\tfor level : %i\n", geo
.level
);
9940 dprintf("\tfor raid_disks : %i\n", geo
.raid_disks
);
9942 if (experimental() == 0)
9945 if (st
->container_dev
== st
->devnum
) {
9946 /* On container level we can only increase number of devices. */
9947 dprintf("imsm: info: Container operation\n");
9948 int old_raid_disks
= 0;
9950 if (imsm_reshape_is_allowed_on_container(
9951 st
, &geo
, &old_raid_disks
)) {
9952 struct imsm_update_reshape
*u
= NULL
;
9955 len
= imsm_create_metadata_update_for_reshape(
9956 st
, &geo
, old_raid_disks
, &u
);
9959 dprintf("imsm: Cannot prepare update\n");
9960 goto exit_imsm_reshape_super
;
9964 /* update metadata locally */
9965 imsm_update_metadata_locally(st
, u
, len
);
9966 /* and possibly remotely */
9967 if (st
->update_tail
)
9968 append_metadata_update(st
, u
, len
);
9973 fprintf(stderr
, Name
": (imsm) Operation "
9974 "is not allowed on this container\n");
9977 /* On volume level we support following operations
9978 * - takeover: raid10 -> raid0; raid0 -> raid10
9979 * - chunk size migration
9980 * - migration: raid5 -> raid0; raid0 -> raid5
9982 struct intel_super
*super
= st
->sb
;
9983 struct intel_dev
*dev
= super
->devlist
;
9985 dprintf("imsm: info: Volume operation\n");
9986 /* find requested device */
9988 if (imsm_find_array_minor_by_subdev(
9989 dev
->index
, st
->container_dev
, &devnum
) == 0
9990 && devnum
== geo
.dev_id
)
9995 fprintf(stderr
, Name
" Cannot find %s (%i) subarray\n",
9996 geo
.dev_name
, geo
.dev_id
);
9997 goto exit_imsm_reshape_super
;
9999 super
->current_vol
= dev
->index
;
10000 change
= imsm_analyze_change(st
, &geo
);
10003 ret_val
= imsm_takeover(st
, &geo
);
10005 case CH_MIGRATION
: {
10006 struct imsm_update_reshape_migration
*u
= NULL
;
10008 imsm_create_metadata_update_for_migration(
10012 "Cannot prepare update\n");
10016 /* update metadata locally */
10017 imsm_update_metadata_locally(st
, u
, len
);
10018 /* and possibly remotely */
10019 if (st
->update_tail
)
10020 append_metadata_update(st
, u
, len
);
10025 case CH_ARRAY_SIZE
: {
10026 /* ToDo: Prepare metadata update here
10035 exit_imsm_reshape_super
:
10036 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
10040 /*******************************************************************************
10041 * Function: wait_for_reshape_imsm
10042 * Description: Function writes new sync_max value and waits until
10043 * reshape process reach new position
10045 * sra : general array info
10046 * ndata : number of disks in new array's layout
10049 * 1 : there is no reshape in progress,
10051 ******************************************************************************/
10052 int wait_for_reshape_imsm(struct mdinfo
*sra
, int ndata
)
10054 int fd
= sysfs_get_fd(sra
, NULL
, "reshape_position");
10055 unsigned long long completed
;
10056 /* to_complete : new sync_max position */
10057 unsigned long long to_complete
= sra
->reshape_progress
;
10058 unsigned long long position_to_set
= to_complete
/ ndata
;
10061 dprintf("imsm: wait_for_reshape_imsm() "
10062 "cannot open reshape_position\n");
10066 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
10067 dprintf("imsm: wait_for_reshape_imsm() "
10068 "cannot read reshape_position (no reshape in progres)\n");
10073 if (completed
> to_complete
) {
10074 dprintf("imsm: wait_for_reshape_imsm() "
10075 "wrong next position to set %llu (%llu)\n",
10076 to_complete
, completed
);
10080 dprintf("Position set: %llu\n", position_to_set
);
10081 if (sysfs_set_num(sra
, NULL
, "sync_max",
10082 position_to_set
) != 0) {
10083 dprintf("imsm: wait_for_reshape_imsm() "
10084 "cannot set reshape position to %llu\n",
10095 select(fd
+1, &rfds
, NULL
, NULL
, NULL
);
10096 if (sysfs_get_str(sra
, NULL
, "sync_action",
10098 strncmp(action
, "reshape", 7) != 0)
10100 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
10101 dprintf("imsm: wait_for_reshape_imsm() "
10102 "cannot read reshape_position (in loop)\n");
10106 } while (completed
< to_complete
);
10112 /*******************************************************************************
10113 * Function: check_degradation_change
10114 * Description: Check that array hasn't become failed.
10116 * info : for sysfs access
10117 * sources : source disks descriptors
10118 * degraded: previous degradation level
10120 * degradation level
10121 ******************************************************************************/
10122 int check_degradation_change(struct mdinfo
*info
,
10126 unsigned long long new_degraded
;
10127 sysfs_get_ll(info
, NULL
, "degraded", &new_degraded
);
10128 if (new_degraded
!= (unsigned long long)degraded
) {
10129 /* check each device to ensure it is still working */
10132 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
10133 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
10135 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
10137 if (sysfs_get_str(info
,
10138 sd
, "state", sbuf
, 20) < 0 ||
10139 strstr(sbuf
, "faulty") ||
10140 strstr(sbuf
, "in_sync") == NULL
) {
10141 /* this device is dead */
10142 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
10143 if (sd
->disk
.raid_disk
>= 0 &&
10144 sources
[sd
->disk
.raid_disk
] >= 0) {
10146 sd
->disk
.raid_disk
]);
10147 sources
[sd
->disk
.raid_disk
] =
10156 return new_degraded
;
10159 /*******************************************************************************
10160 * Function: imsm_manage_reshape
10161 * Description: Function finds array under reshape and it manages reshape
10162 * process. It creates stripes backups (if required) and sets
10165 * afd : Backup handle (nattive) - not used
10166 * sra : general array info
10167 * reshape : reshape parameters - not used
10168 * st : supertype structure
10169 * blocks : size of critical section [blocks]
10170 * fds : table of source device descriptor
10171 * offsets : start of array (offest per devices)
10173 * destfd : table of destination device descriptor
10174 * destoffsets : table of destination offsets (per device)
10176 * 1 : success, reshape is done
10178 ******************************************************************************/
10179 static int imsm_manage_reshape(
10180 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
10181 struct supertype
*st
, unsigned long backup_blocks
,
10182 int *fds
, unsigned long long *offsets
,
10183 int dests
, int *destfd
, unsigned long long *destoffsets
)
10186 struct intel_super
*super
= st
->sb
;
10187 struct intel_dev
*dv
= NULL
;
10188 struct imsm_dev
*dev
= NULL
;
10189 struct imsm_map
*map_src
;
10190 int migr_vol_qan
= 0;
10191 int ndata
, odata
; /* [bytes] */
10192 int chunk
; /* [bytes] */
10193 struct migr_record
*migr_rec
;
10195 unsigned int buf_size
; /* [bytes] */
10196 unsigned long long max_position
; /* array size [bytes] */
10197 unsigned long long next_step
; /* [blocks]/[bytes] */
10198 unsigned long long old_data_stripe_length
;
10199 unsigned long long start_src
; /* [bytes] */
10200 unsigned long long start
; /* [bytes] */
10201 unsigned long long start_buf_shift
; /* [bytes] */
10203 int source_layout
= 0;
10205 if (!fds
|| !offsets
|| !sra
)
10208 /* Find volume during the reshape */
10209 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
10210 if (dv
->dev
->vol
.migr_type
== MIGR_GEN_MIGR
10211 && dv
->dev
->vol
.migr_state
== 1) {
10216 /* Only one volume can migrate at the same time */
10217 if (migr_vol_qan
!= 1) {
10218 fprintf(stderr
, Name
" : %s", migr_vol_qan
?
10219 "Number of migrating volumes greater than 1\n" :
10220 "There is no volume during migrationg\n");
10224 map_src
= get_imsm_map(dev
, MAP_1
);
10225 if (map_src
== NULL
)
10228 ndata
= imsm_num_data_members(dev
, MAP_0
);
10229 odata
= imsm_num_data_members(dev
, MAP_1
);
10231 chunk
= __le16_to_cpu(map_src
->blocks_per_strip
) * 512;
10232 old_data_stripe_length
= odata
* chunk
;
10234 migr_rec
= super
->migr_rec
;
10236 /* initialize migration record for start condition */
10237 if (sra
->reshape_progress
== 0)
10238 init_migr_record_imsm(st
, dev
, sra
);
10240 if (__le32_to_cpu(migr_rec
->rec_status
) != UNIT_SRC_NORMAL
) {
10241 dprintf("imsm: cannot restart migration when data "
10242 "are present in copy area.\n");
10245 /* Save checkpoint to update migration record for current
10246 * reshape position (in md). It can be farther than current
10247 * reshape position in metadata.
10249 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
10250 /* ignore error == 2, this can mean end of reshape here
10252 dprintf("imsm: Cannot write checkpoint to "
10253 "migration record (UNIT_SRC_NORMAL, "
10254 "initial save)\n");
10259 /* size for data */
10260 buf_size
= __le32_to_cpu(migr_rec
->blocks_per_unit
) * 512;
10261 /* extend buffer size for parity disk */
10262 buf_size
+= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
10263 /* add space for stripe aligment */
10264 buf_size
+= old_data_stripe_length
;
10265 if (posix_memalign((void **)&buf
, 4096, buf_size
)) {
10266 dprintf("imsm: Cannot allocate checpoint buffer\n");
10270 max_position
= sra
->component_size
* ndata
;
10271 source_layout
= imsm_level_to_layout(map_src
->raid_level
);
10273 while (__le32_to_cpu(migr_rec
->curr_migr_unit
) <
10274 __le32_to_cpu(migr_rec
->num_migr_units
)) {
10275 /* current reshape position [blocks] */
10276 unsigned long long current_position
=
10277 __le32_to_cpu(migr_rec
->blocks_per_unit
)
10278 * __le32_to_cpu(migr_rec
->curr_migr_unit
);
10279 unsigned long long border
;
10281 /* Check that array hasn't become failed.
10283 degraded
= check_degradation_change(sra
, fds
, degraded
);
10284 if (degraded
> 1) {
10285 dprintf("imsm: Abort reshape due to degradation"
10286 " level (%i)\n", degraded
);
10290 next_step
= __le32_to_cpu(migr_rec
->blocks_per_unit
);
10292 if ((current_position
+ next_step
) > max_position
)
10293 next_step
= max_position
- current_position
;
10295 start
= current_position
* 512;
10297 /* allign reading start to old geometry */
10298 start_buf_shift
= start
% old_data_stripe_length
;
10299 start_src
= start
- start_buf_shift
;
10301 border
= (start_src
/ odata
) - (start
/ ndata
);
10303 if (border
<= __le32_to_cpu(migr_rec
->dest_depth_per_unit
)) {
10304 /* save critical stripes to buf
10305 * start - start address of current unit
10306 * to backup [bytes]
10307 * start_src - start address of current unit
10308 * to backup alligned to source array
10311 unsigned long long next_step_filler
= 0;
10312 unsigned long long copy_length
= next_step
* 512;
10314 /* allign copy area length to stripe in old geometry */
10315 next_step_filler
= ((copy_length
+ start_buf_shift
)
10316 % old_data_stripe_length
);
10317 if (next_step_filler
)
10318 next_step_filler
= (old_data_stripe_length
10319 - next_step_filler
);
10320 dprintf("save_stripes() parameters: start = %llu,"
10321 "\tstart_src = %llu,\tnext_step*512 = %llu,"
10322 "\tstart_in_buf_shift = %llu,"
10323 "\tnext_step_filler = %llu\n",
10324 start
, start_src
, copy_length
,
10325 start_buf_shift
, next_step_filler
);
10327 if (save_stripes(fds
, offsets
, map_src
->num_members
,
10328 chunk
, map_src
->raid_level
,
10329 source_layout
, 0, NULL
, start_src
,
10331 next_step_filler
+ start_buf_shift
,
10333 dprintf("imsm: Cannot save stripes"
10337 /* Convert data to destination format and store it
10338 * in backup general migration area
10340 if (save_backup_imsm(st
, dev
, sra
,
10341 buf
+ start_buf_shift
, copy_length
)) {
10342 dprintf("imsm: Cannot save stripes to "
10343 "target devices\n");
10346 if (save_checkpoint_imsm(st
, sra
,
10347 UNIT_SRC_IN_CP_AREA
)) {
10348 dprintf("imsm: Cannot write checkpoint to "
10349 "migration record (UNIT_SRC_IN_CP_AREA)\n");
10353 /* set next step to use whole border area */
10354 border
/= next_step
;
10356 next_step
*= border
;
10358 /* When data backed up, checkpoint stored,
10359 * kick the kernel to reshape unit of data
10361 next_step
= next_step
+ sra
->reshape_progress
;
10362 /* limit next step to array max position */
10363 if (next_step
> max_position
)
10364 next_step
= max_position
;
10365 sysfs_set_num(sra
, NULL
, "suspend_lo", sra
->reshape_progress
);
10366 sysfs_set_num(sra
, NULL
, "suspend_hi", next_step
);
10367 sra
->reshape_progress
= next_step
;
10369 /* wait until reshape finish */
10370 if (wait_for_reshape_imsm(sra
, ndata
) < 0) {
10371 dprintf("wait_for_reshape_imsm returned error!\n");
10375 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
10376 /* ignore error == 2, this can mean end of reshape here
10378 dprintf("imsm: Cannot write checkpoint to "
10379 "migration record (UNIT_SRC_NORMAL)\n");
10385 /* return '1' if done */
10389 abort_reshape(sra
);
10393 #endif /* MDASSEMBLE */
10395 struct superswitch super_imsm
= {
10397 .examine_super
= examine_super_imsm
,
10398 .brief_examine_super
= brief_examine_super_imsm
,
10399 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
10400 .export_examine_super
= export_examine_super_imsm
,
10401 .detail_super
= detail_super_imsm
,
10402 .brief_detail_super
= brief_detail_super_imsm
,
10403 .write_init_super
= write_init_super_imsm
,
10404 .validate_geometry
= validate_geometry_imsm
,
10405 .add_to_super
= add_to_super_imsm
,
10406 .remove_from_super
= remove_from_super_imsm
,
10407 .detail_platform
= detail_platform_imsm
,
10408 .kill_subarray
= kill_subarray_imsm
,
10409 .update_subarray
= update_subarray_imsm
,
10410 .load_container
= load_container_imsm
,
10411 .default_geometry
= default_geometry_imsm
,
10412 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
10413 .reshape_super
= imsm_reshape_super
,
10414 .manage_reshape
= imsm_manage_reshape
,
10415 .recover_backup
= recover_backup_imsm
,
10417 .match_home
= match_home_imsm
,
10418 .uuid_from_super
= uuid_from_super_imsm
,
10419 .getinfo_super
= getinfo_super_imsm
,
10420 .getinfo_super_disks
= getinfo_super_disks_imsm
,
10421 .update_super
= update_super_imsm
,
10423 .avail_size
= avail_size_imsm
,
10424 .min_acceptable_spare_size
= min_acceptable_spare_size_imsm
,
10426 .compare_super
= compare_super_imsm
,
10428 .load_super
= load_super_imsm
,
10429 .init_super
= init_super_imsm
,
10430 .store_super
= store_super_imsm
,
10431 .free_super
= free_super_imsm
,
10432 .match_metadata_desc
= match_metadata_desc_imsm
,
10433 .container_content
= container_content_imsm
,
10441 .open_new
= imsm_open_new
,
10442 .set_array_state
= imsm_set_array_state
,
10443 .set_disk
= imsm_set_disk
,
10444 .sync_metadata
= imsm_sync_metadata
,
10445 .activate_spare
= imsm_activate_spare
,
10446 .process_update
= imsm_process_update
,
10447 .prepare_update
= imsm_prepare_update
,
10448 #endif /* MDASSEMBLE */