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
{
410 /* definition of messages passed to imsm_process_update */
411 enum imsm_update_type
{
412 update_activate_spare
,
416 update_add_remove_disk
,
417 update_reshape_container_disks
,
418 update_reshape_migration
,
420 update_general_migration_checkpoint
,
423 struct imsm_update_activate_spare
{
424 enum imsm_update_type type
;
428 struct imsm_update_activate_spare
*next
;
441 enum takeover_direction
{
445 struct imsm_update_takeover
{
446 enum imsm_update_type type
;
448 enum takeover_direction direction
;
451 struct imsm_update_reshape
{
452 enum imsm_update_type type
;
456 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
459 struct imsm_update_reshape_migration
{
460 enum imsm_update_type type
;
463 /* fields for array migration changes
470 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
473 struct imsm_update_general_migration_checkpoint
{
474 enum imsm_update_type type
;
475 __u32 curr_migr_unit
;
479 __u8 serial
[MAX_RAID_SERIAL_LEN
];
482 struct imsm_update_create_array
{
483 enum imsm_update_type type
;
488 struct imsm_update_kill_array
{
489 enum imsm_update_type type
;
493 struct imsm_update_rename_array
{
494 enum imsm_update_type type
;
495 __u8 name
[MAX_RAID_SERIAL_LEN
];
499 struct imsm_update_add_remove_disk
{
500 enum imsm_update_type type
;
504 static const char *_sys_dev_type
[] = {
505 [SYS_DEV_UNKNOWN
] = "Unknown",
506 [SYS_DEV_SAS
] = "SAS",
507 [SYS_DEV_SATA
] = "SATA"
510 const char *get_sys_dev_type(enum sys_dev_type type
)
512 if (type
>= SYS_DEV_MAX
)
513 type
= SYS_DEV_UNKNOWN
;
515 return _sys_dev_type
[type
];
518 static struct intel_hba
* alloc_intel_hba(struct sys_dev
*device
)
520 struct intel_hba
*result
= malloc(sizeof(*result
));
522 result
->type
= device
->type
;
523 result
->path
= strdup(device
->path
);
525 if (result
->path
&& (result
->pci_id
= strrchr(result
->path
, '/')) != NULL
)
531 static struct intel_hba
* find_intel_hba(struct intel_hba
*hba
, struct sys_dev
*device
)
533 struct intel_hba
*result
=NULL
;
534 for (result
= hba
; result
; result
= result
->next
) {
535 if (result
->type
== device
->type
&& strcmp(result
->path
, device
->path
) == 0)
541 static int attach_hba_to_super(struct intel_super
*super
, struct sys_dev
*device
)
543 struct intel_hba
*hba
;
545 /* check if disk attached to Intel HBA */
546 hba
= find_intel_hba(super
->hba
, device
);
549 /* Check if HBA is already attached to super */
550 if (super
->hba
== NULL
) {
551 super
->hba
= alloc_intel_hba(device
);
556 /* Intel metadata allows for all disks attached to the same type HBA.
557 * Do not sypport odf HBA types mixing
559 if (device
->type
!= hba
->type
)
565 hba
->next
= alloc_intel_hba(device
);
569 static struct sys_dev
* find_disk_attached_hba(int fd
, const char *devname
)
571 struct sys_dev
*list
, *elem
, *prev
;
574 if ((list
= find_intel_devices()) == NULL
)
578 disk_path
= (char *) devname
;
580 disk_path
= diskfd_to_devpath(fd
);
587 for (prev
= NULL
, elem
= list
; elem
; prev
= elem
, elem
= elem
->next
) {
588 if (path_attached_to_hba(disk_path
, elem
->path
)) {
592 prev
->next
= elem
->next
;
594 if (disk_path
!= devname
)
600 if (disk_path
!= devname
)
608 static int find_intel_hba_capability(int fd
, struct intel_super
*super
,
611 static struct supertype
*match_metadata_desc_imsm(char *arg
)
613 struct supertype
*st
;
615 if (strcmp(arg
, "imsm") != 0 &&
616 strcmp(arg
, "default") != 0
620 st
= malloc(sizeof(*st
));
623 memset(st
, 0, sizeof(*st
));
624 st
->container_dev
= NoMdDev
;
625 st
->ss
= &super_imsm
;
626 st
->max_devs
= IMSM_MAX_DEVICES
;
627 st
->minor_version
= 0;
633 static __u8
*get_imsm_version(struct imsm_super
*mpb
)
635 return &mpb
->sig
[MPB_SIG_LEN
];
639 /* retrieve a disk directly from the anchor when the anchor is known to be
640 * up-to-date, currently only at load time
642 static struct imsm_disk
*__get_imsm_disk(struct imsm_super
*mpb
, __u8 index
)
644 if (index
>= mpb
->num_disks
)
646 return &mpb
->disk
[index
];
649 /* retrieve the disk description based on a index of the disk
652 static struct dl
*get_imsm_dl_disk(struct intel_super
*super
, __u8 index
)
656 for (d
= super
->disks
; d
; d
= d
->next
)
657 if (d
->index
== index
)
662 /* retrieve a disk from the parsed metadata */
663 static struct imsm_disk
*get_imsm_disk(struct intel_super
*super
, __u8 index
)
667 dl
= get_imsm_dl_disk(super
, index
);
674 /* generate a checksum directly from the anchor when the anchor is known to be
675 * up-to-date, currently only at load or write_super after coalescing
677 static __u32
__gen_imsm_checksum(struct imsm_super
*mpb
)
679 __u32 end
= mpb
->mpb_size
/ sizeof(end
);
680 __u32
*p
= (__u32
*) mpb
;
684 sum
+= __le32_to_cpu(*p
);
688 return sum
- __le32_to_cpu(mpb
->check_sum
);
691 static size_t sizeof_imsm_map(struct imsm_map
*map
)
693 return sizeof(struct imsm_map
) + sizeof(__u32
) * (map
->num_members
- 1);
696 struct imsm_map
*get_imsm_map(struct imsm_dev
*dev
, int second_map
)
698 /* A device can have 2 maps if it is in the middle of a migration.
700 * MAP_0 - we return the first map
701 * MAP_1 - we return the second map if it exists, else NULL
702 * MAP_X - we return the second map if it exists, else the first
704 struct imsm_map
*map
= &dev
->vol
.map
[0];
705 struct imsm_map
*map2
= NULL
;
707 if (dev
->vol
.migr_state
)
708 map2
= (void *)map
+ sizeof_imsm_map(map
);
710 switch (second_map
) {
727 /* return the size of the device.
728 * migr_state increases the returned size if map[0] were to be duplicated
730 static size_t sizeof_imsm_dev(struct imsm_dev
*dev
, int migr_state
)
732 size_t size
= sizeof(*dev
) - sizeof(struct imsm_map
) +
733 sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
735 /* migrating means an additional map */
736 if (dev
->vol
.migr_state
)
737 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_1
));
739 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
745 /* retrieve disk serial number list from a metadata update */
746 static struct disk_info
*get_disk_info(struct imsm_update_create_array
*update
)
749 struct disk_info
*inf
;
751 inf
= u
+ sizeof(*update
) - sizeof(struct imsm_dev
) +
752 sizeof_imsm_dev(&update
->dev
, 0);
758 static struct imsm_dev
*__get_imsm_dev(struct imsm_super
*mpb
, __u8 index
)
764 if (index
>= mpb
->num_raid_devs
)
767 /* devices start after all disks */
768 offset
= ((void *) &mpb
->disk
[mpb
->num_disks
]) - _mpb
;
770 for (i
= 0; i
<= index
; i
++)
772 return _mpb
+ offset
;
774 offset
+= sizeof_imsm_dev(_mpb
+ offset
, 0);
779 static struct imsm_dev
*get_imsm_dev(struct intel_super
*super
, __u8 index
)
781 struct intel_dev
*dv
;
783 if (index
>= super
->anchor
->num_raid_devs
)
785 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
786 if (dv
->index
== index
)
793 * == MAP_0 get first map
794 * == MAP_1 get second map
795 * == MAP_X than get map according to the current migr_state
797 static __u32
get_imsm_ord_tbl_ent(struct imsm_dev
*dev
,
801 struct imsm_map
*map
;
803 map
= get_imsm_map(dev
, second_map
);
805 /* top byte identifies disk under rebuild */
806 return __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
809 #define ord_to_idx(ord) (((ord) << 8) >> 8)
810 static __u32
get_imsm_disk_idx(struct imsm_dev
*dev
, int slot
, int second_map
)
812 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, second_map
);
814 return ord_to_idx(ord
);
817 static void set_imsm_ord_tbl_ent(struct imsm_map
*map
, int slot
, __u32 ord
)
819 map
->disk_ord_tbl
[slot
] = __cpu_to_le32(ord
);
822 static int get_imsm_disk_slot(struct imsm_map
*map
, unsigned idx
)
827 for (slot
= 0; slot
< map
->num_members
; slot
++) {
828 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
829 if (ord_to_idx(ord
) == idx
)
836 static int get_imsm_raid_level(struct imsm_map
*map
)
838 if (map
->raid_level
== 1) {
839 if (map
->num_members
== 2)
845 return map
->raid_level
;
848 static int cmp_extent(const void *av
, const void *bv
)
850 const struct extent
*a
= av
;
851 const struct extent
*b
= bv
;
852 if (a
->start
< b
->start
)
854 if (a
->start
> b
->start
)
859 static int count_memberships(struct dl
*dl
, struct intel_super
*super
)
864 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
865 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
866 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
868 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
875 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
);
877 static int split_ull(unsigned long long n
, __u32
*lo
, __u32
*hi
)
879 if (lo
== 0 || hi
== 0)
881 *lo
= __le32_to_cpu((unsigned)n
);
882 *hi
= __le32_to_cpu((unsigned)(n
>> 32));
886 static unsigned long long join_u32(__u32 lo
, __u32 hi
)
888 return (unsigned long long)__le32_to_cpu(lo
) |
889 (((unsigned long long)__le32_to_cpu(hi
)) << 32);
892 static unsigned long long total_blocks(struct imsm_disk
*disk
)
896 return join_u32(disk
->total_blocks_lo
, disk
->total_blocks_hi
);
899 static unsigned long long pba_of_lba0(struct imsm_map
*map
)
903 return join_u32(map
->pba_of_lba0_lo
, map
->pba_of_lba0_hi
);
906 static unsigned long long blocks_per_member(struct imsm_map
*map
)
910 return join_u32(map
->blocks_per_member_lo
, map
->blocks_per_member_hi
);
913 static unsigned long long num_data_stripes(struct imsm_map
*map
)
917 return join_u32(map
->num_data_stripes_lo
, map
->num_data_stripes_hi
);
920 static void set_total_blocks(struct imsm_disk
*disk
, unsigned long long n
)
922 split_ull(n
, &disk
->total_blocks_lo
, &disk
->total_blocks_hi
);
925 static void set_pba_of_lba0(struct imsm_map
*map
, unsigned long long n
)
927 split_ull(n
, &map
->pba_of_lba0_lo
, &map
->pba_of_lba0_hi
);
930 static void set_blocks_per_member(struct imsm_map
*map
, unsigned long long n
)
932 split_ull(n
, &map
->blocks_per_member_lo
, &map
->blocks_per_member_hi
);
935 static void set_num_data_stripes(struct imsm_map
*map
, unsigned long long n
)
937 split_ull(n
, &map
->num_data_stripes_lo
, &map
->num_data_stripes_hi
);
940 static struct extent
*get_extents(struct intel_super
*super
, struct dl
*dl
)
942 /* find a list of used extents on the given physical device */
943 struct extent
*rv
, *e
;
945 int memberships
= count_memberships(dl
, super
);
948 /* trim the reserved area for spares, so they can join any array
949 * regardless of whether the OROM has assigned sectors from the
950 * IMSM_RESERVED_SECTORS region
953 reservation
= imsm_min_reserved_sectors(super
);
955 reservation
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
957 rv
= malloc(sizeof(struct extent
) * (memberships
+ 1));
962 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
963 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
964 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
966 if (get_imsm_disk_slot(map
, dl
->index
) >= 0) {
967 e
->start
= pba_of_lba0(map
);
968 e
->size
= blocks_per_member(map
);
972 qsort(rv
, memberships
, sizeof(*rv
), cmp_extent
);
974 /* determine the start of the metadata
975 * when no raid devices are defined use the default
976 * ...otherwise allow the metadata to truncate the value
977 * as is the case with older versions of imsm
980 struct extent
*last
= &rv
[memberships
- 1];
981 unsigned long long remainder
;
983 remainder
= total_blocks(&dl
->disk
) - (last
->start
+ last
->size
);
984 /* round down to 1k block to satisfy precision of the kernel
988 /* make sure remainder is still sane */
989 if (remainder
< (unsigned)ROUND_UP(super
->len
, 512) >> 9)
990 remainder
= ROUND_UP(super
->len
, 512) >> 9;
991 if (reservation
> remainder
)
992 reservation
= remainder
;
994 e
->start
= total_blocks(&dl
->disk
) - reservation
;
999 /* try to determine how much space is reserved for metadata from
1000 * the last get_extents() entry, otherwise fallback to the
1003 static __u32
imsm_reserved_sectors(struct intel_super
*super
, struct dl
*dl
)
1009 /* for spares just return a minimal reservation which will grow
1010 * once the spare is picked up by an array
1012 if (dl
->index
== -1)
1013 return MPB_SECTOR_CNT
;
1015 e
= get_extents(super
, dl
);
1017 return MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1019 /* scroll to last entry */
1020 for (i
= 0; e
[i
].size
; i
++)
1023 rv
= total_blocks(&dl
->disk
) - e
[i
].start
;
1030 static int is_spare(struct imsm_disk
*disk
)
1032 return (disk
->status
& SPARE_DISK
) == SPARE_DISK
;
1035 static int is_configured(struct imsm_disk
*disk
)
1037 return (disk
->status
& CONFIGURED_DISK
) == CONFIGURED_DISK
;
1040 static int is_failed(struct imsm_disk
*disk
)
1042 return (disk
->status
& FAILED_DISK
) == FAILED_DISK
;
1045 /* try to determine how much space is reserved for metadata from
1046 * the last get_extents() entry on the smallest active disk,
1047 * otherwise fallback to the default
1049 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
)
1053 unsigned long long min_active
;
1055 __u32 rv
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1056 struct dl
*dl
, *dl_min
= NULL
;
1062 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
1065 unsigned long long blocks
= total_blocks(&dl
->disk
);
1066 if (blocks
< min_active
|| min_active
== 0) {
1068 min_active
= blocks
;
1074 /* find last lba used by subarrays on the smallest active disk */
1075 e
= get_extents(super
, dl_min
);
1078 for (i
= 0; e
[i
].size
; i
++)
1081 remainder
= min_active
- e
[i
].start
;
1084 /* to give priority to recovery we should not require full
1085 IMSM_RESERVED_SECTORS from the spare */
1086 rv
= MPB_SECTOR_CNT
+ NUM_BLOCKS_DIRTY_STRIPE_REGION
;
1088 /* if real reservation is smaller use that value */
1089 return (remainder
< rv
) ? remainder
: rv
;
1092 /* Return minimum size of a spare that can be used in this array*/
1093 static unsigned long long min_acceptable_spare_size_imsm(struct supertype
*st
)
1095 struct intel_super
*super
= st
->sb
;
1099 unsigned long long rv
= 0;
1103 /* find first active disk in array */
1105 while (dl
&& (is_failed(&dl
->disk
) || dl
->index
== -1))
1109 /* find last lba used by subarrays */
1110 e
= get_extents(super
, dl
);
1113 for (i
= 0; e
[i
].size
; i
++)
1116 rv
= e
[i
-1].start
+ e
[i
-1].size
;
1119 /* add the amount of space needed for metadata */
1120 rv
= rv
+ imsm_min_reserved_sectors(super
);
1125 static int is_gen_migration(struct imsm_dev
*dev
);
1128 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
1129 struct imsm_dev
*dev
);
1131 static void print_imsm_dev(struct intel_super
*super
,
1132 struct imsm_dev
*dev
,
1138 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1139 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
1143 printf("[%.16s]:\n", dev
->volume
);
1144 printf(" UUID : %s\n", uuid
);
1145 printf(" RAID Level : %d", get_imsm_raid_level(map
));
1147 printf(" <-- %d", get_imsm_raid_level(map2
));
1149 printf(" Members : %d", map
->num_members
);
1151 printf(" <-- %d", map2
->num_members
);
1153 printf(" Slots : [");
1154 for (i
= 0; i
< map
->num_members
; i
++) {
1155 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
1156 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1161 for (i
= 0; i
< map2
->num_members
; i
++) {
1162 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_1
);
1163 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1168 printf(" Failed disk : ");
1169 if (map
->failed_disk_num
== 0xff)
1172 printf("%i", map
->failed_disk_num
);
1174 slot
= get_imsm_disk_slot(map
, disk_idx
);
1176 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
1177 printf(" This Slot : %d%s\n", slot
,
1178 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
1180 printf(" This Slot : ?\n");
1181 sz
= __le32_to_cpu(dev
->size_high
);
1183 sz
+= __le32_to_cpu(dev
->size_low
);
1184 printf(" Array Size : %llu%s\n", (unsigned long long)sz
,
1185 human_size(sz
* 512));
1186 sz
= blocks_per_member(map
);
1187 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz
,
1188 human_size(sz
* 512));
1189 printf(" Sector Offset : %llu\n",
1191 printf(" Num Stripes : %llu\n",
1192 num_data_stripes(map
));
1193 printf(" Chunk Size : %u KiB",
1194 __le16_to_cpu(map
->blocks_per_strip
) / 2);
1196 printf(" <-- %u KiB",
1197 __le16_to_cpu(map2
->blocks_per_strip
) / 2);
1199 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
1200 printf(" Migrate State : ");
1201 if (dev
->vol
.migr_state
) {
1202 if (migr_type(dev
) == MIGR_INIT
)
1203 printf("initialize\n");
1204 else if (migr_type(dev
) == MIGR_REBUILD
)
1205 printf("rebuild\n");
1206 else if (migr_type(dev
) == MIGR_VERIFY
)
1208 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
1209 printf("general migration\n");
1210 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
1211 printf("state change\n");
1212 else if (migr_type(dev
) == MIGR_REPAIR
)
1215 printf("<unknown:%d>\n", migr_type(dev
));
1218 printf(" Map State : %s", map_state_str
[map
->map_state
]);
1219 if (dev
->vol
.migr_state
) {
1220 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
1222 printf(" <-- %s", map_state_str
[map
->map_state
]);
1223 printf("\n Checkpoint : %u ",
1224 __le32_to_cpu(dev
->vol
.curr_migr_unit
));
1225 if ((is_gen_migration(dev
)) && ((slot
> 1) || (slot
< 0)))
1228 printf("(%llu)", (unsigned long long)
1229 blocks_per_migr_unit(super
, dev
));
1232 printf(" Dirty State : %s\n", dev
->vol
.dirty
? "dirty" : "clean");
1235 static void print_imsm_disk(struct imsm_disk
*disk
, int index
, __u32 reserved
)
1237 char str
[MAX_RAID_SERIAL_LEN
+ 1];
1240 if (index
< -1 || !disk
)
1244 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
1246 printf(" Disk%02d Serial : %s\n", index
, str
);
1248 printf(" Disk Serial : %s\n", str
);
1249 printf(" State :%s%s%s\n", is_spare(disk
) ? " spare" : "",
1250 is_configured(disk
) ? " active" : "",
1251 is_failed(disk
) ? " failed" : "");
1252 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
1253 sz
= total_blocks(disk
) - reserved
;
1254 printf(" Usable Size : %llu%s\n", (unsigned long long)sz
,
1255 human_size(sz
* 512));
1258 void examine_migr_rec_imsm(struct intel_super
*super
)
1260 struct migr_record
*migr_rec
= super
->migr_rec
;
1261 struct imsm_super
*mpb
= super
->anchor
;
1264 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1265 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1266 struct imsm_map
*map
;
1269 if (is_gen_migration(dev
) == 0)
1272 printf("\nMigration Record Information:");
1274 /* first map under migration */
1275 map
= get_imsm_map(dev
, MAP_0
);
1277 slot
= get_imsm_disk_slot(map
, super
->disks
->index
);
1278 if ((map
== NULL
) || (slot
> 1) || (slot
< 0)) {
1279 printf(" Empty\n ");
1280 printf("Examine one of first two disks in array\n");
1283 printf("\n Status : ");
1284 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
1287 printf("Contains Data\n");
1288 printf(" Current Unit : %u\n",
1289 __le32_to_cpu(migr_rec
->curr_migr_unit
));
1290 printf(" Family : %u\n",
1291 __le32_to_cpu(migr_rec
->family_num
));
1292 printf(" Ascending : %u\n",
1293 __le32_to_cpu(migr_rec
->ascending_migr
));
1294 printf(" Blocks Per Unit : %u\n",
1295 __le32_to_cpu(migr_rec
->blocks_per_unit
));
1296 printf(" Dest. Depth Per Unit : %u\n",
1297 __le32_to_cpu(migr_rec
->dest_depth_per_unit
));
1298 printf(" Checkpoint Area pba : %u\n",
1299 __le32_to_cpu(migr_rec
->ckpt_area_pba
));
1300 printf(" First member lba : %u\n",
1301 __le32_to_cpu(migr_rec
->dest_1st_member_lba
));
1302 printf(" Total Number of Units : %u\n",
1303 __le32_to_cpu(migr_rec
->num_migr_units
));
1304 printf(" Size of volume : %u\n",
1305 __le32_to_cpu(migr_rec
->post_migr_vol_cap
));
1306 printf(" Expansion space for LBA64 : %u\n",
1307 __le32_to_cpu(migr_rec
->post_migr_vol_cap_hi
));
1308 printf(" Record was read from : %u\n",
1309 __le32_to_cpu(migr_rec
->ckpt_read_disk_num
));
1314 #endif /* MDASSEMBLE */
1315 /*******************************************************************************
1316 * function: imsm_check_attributes
1317 * Description: Function checks if features represented by attributes flags
1318 * are supported by mdadm.
1320 * attributes - Attributes read from metadata
1322 * 0 - passed attributes contains unsupported features flags
1323 * 1 - all features are supported
1324 ******************************************************************************/
1325 static int imsm_check_attributes(__u32 attributes
)
1328 __u32 not_supported
= MPB_ATTRIB_SUPPORTED
^0xffffffff;
1330 not_supported
&= ~MPB_ATTRIB_IGNORED
;
1332 not_supported
&= attributes
;
1333 if (not_supported
) {
1334 fprintf(stderr
, Name
"(IMSM): Unsupported attributes : %x\n",
1335 (unsigned)__le32_to_cpu(not_supported
));
1336 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1337 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
1338 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1340 if (not_supported
& MPB_ATTRIB_2TB
) {
1341 dprintf("\t\tMPB_ATTRIB_2TB\n");
1342 not_supported
^= MPB_ATTRIB_2TB
;
1344 if (not_supported
& MPB_ATTRIB_RAID0
) {
1345 dprintf("\t\tMPB_ATTRIB_RAID0\n");
1346 not_supported
^= MPB_ATTRIB_RAID0
;
1348 if (not_supported
& MPB_ATTRIB_RAID1
) {
1349 dprintf("\t\tMPB_ATTRIB_RAID1\n");
1350 not_supported
^= MPB_ATTRIB_RAID1
;
1352 if (not_supported
& MPB_ATTRIB_RAID10
) {
1353 dprintf("\t\tMPB_ATTRIB_RAID10\n");
1354 not_supported
^= MPB_ATTRIB_RAID10
;
1356 if (not_supported
& MPB_ATTRIB_RAID1E
) {
1357 dprintf("\t\tMPB_ATTRIB_RAID1E\n");
1358 not_supported
^= MPB_ATTRIB_RAID1E
;
1360 if (not_supported
& MPB_ATTRIB_RAID5
) {
1361 dprintf("\t\tMPB_ATTRIB_RAID5\n");
1362 not_supported
^= MPB_ATTRIB_RAID5
;
1364 if (not_supported
& MPB_ATTRIB_RAIDCNG
) {
1365 dprintf("\t\tMPB_ATTRIB_RAIDCNG\n");
1366 not_supported
^= MPB_ATTRIB_RAIDCNG
;
1368 if (not_supported
& MPB_ATTRIB_BBM
) {
1369 dprintf("\t\tMPB_ATTRIB_BBM\n");
1370 not_supported
^= MPB_ATTRIB_BBM
;
1372 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1373 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n");
1374 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1376 if (not_supported
& MPB_ATTRIB_EXP_STRIPE_SIZE
) {
1377 dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n");
1378 not_supported
^= MPB_ATTRIB_EXP_STRIPE_SIZE
;
1380 if (not_supported
& MPB_ATTRIB_2TB_DISK
) {
1381 dprintf("\t\tMPB_ATTRIB_2TB_DISK\n");
1382 not_supported
^= MPB_ATTRIB_2TB_DISK
;
1384 if (not_supported
& MPB_ATTRIB_NEVER_USE2
) {
1385 dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n");
1386 not_supported
^= MPB_ATTRIB_NEVER_USE2
;
1388 if (not_supported
& MPB_ATTRIB_NEVER_USE
) {
1389 dprintf("\t\tMPB_ATTRIB_NEVER_USE\n");
1390 not_supported
^= MPB_ATTRIB_NEVER_USE
;
1394 dprintf(Name
"(IMSM): Unknown attributes : %x\n", not_supported
);
1403 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
);
1405 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
1407 struct intel_super
*super
= st
->sb
;
1408 struct imsm_super
*mpb
= super
->anchor
;
1409 char str
[MAX_SIGNATURE_LENGTH
];
1414 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
1417 snprintf(str
, MPB_SIG_LEN
, "%s", mpb
->sig
);
1418 printf(" Magic : %s\n", str
);
1419 snprintf(str
, strlen(MPB_VERSION_RAID0
), "%s", get_imsm_version(mpb
));
1420 printf(" Version : %s\n", get_imsm_version(mpb
));
1421 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
1422 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
1423 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
1424 printf(" Attributes : ");
1425 if (imsm_check_attributes(mpb
->attributes
))
1426 printf("All supported\n");
1428 printf("not supported\n");
1429 getinfo_super_imsm(st
, &info
, NULL
);
1430 fname_from_uuid(st
, &info
, nbuf
, ':');
1431 printf(" UUID : %s\n", nbuf
+ 5);
1432 sum
= __le32_to_cpu(mpb
->check_sum
);
1433 printf(" Checksum : %08x %s\n", sum
,
1434 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
1435 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
));
1436 printf(" Disks : %d\n", mpb
->num_disks
);
1437 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
1438 print_imsm_disk(__get_imsm_disk(mpb
, super
->disks
->index
), super
->disks
->index
, reserved
);
1439 if (super
->bbm_log
) {
1440 struct bbm_log
*log
= super
->bbm_log
;
1443 printf("Bad Block Management Log:\n");
1444 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
1445 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
1446 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
1447 printf(" Spare Blocks : %d\n", __le32_to_cpu(log
->reserved_spare_block_count
));
1448 printf(" First Spare : %llx\n",
1449 (unsigned long long) __le64_to_cpu(log
->first_spare_lba
));
1451 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1453 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1455 super
->current_vol
= i
;
1456 getinfo_super_imsm(st
, &info
, NULL
);
1457 fname_from_uuid(st
, &info
, nbuf
, ':');
1458 print_imsm_dev(super
, dev
, nbuf
+ 5, super
->disks
->index
);
1460 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1461 if (i
== super
->disks
->index
)
1463 print_imsm_disk(__get_imsm_disk(mpb
, i
), i
, reserved
);
1466 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
1467 if (dl
->index
== -1)
1468 print_imsm_disk(&dl
->disk
, -1, reserved
);
1470 examine_migr_rec_imsm(super
);
1473 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
1475 /* We just write a generic IMSM ARRAY entry */
1478 struct intel_super
*super
= st
->sb
;
1480 if (!super
->anchor
->num_raid_devs
) {
1481 printf("ARRAY metadata=imsm\n");
1485 getinfo_super_imsm(st
, &info
, NULL
);
1486 fname_from_uuid(st
, &info
, nbuf
, ':');
1487 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
1490 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
1492 /* We just write a generic IMSM ARRAY entry */
1496 struct intel_super
*super
= st
->sb
;
1499 if (!super
->anchor
->num_raid_devs
)
1502 getinfo_super_imsm(st
, &info
, NULL
);
1503 fname_from_uuid(st
, &info
, nbuf
, ':');
1504 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1505 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1507 super
->current_vol
= i
;
1508 getinfo_super_imsm(st
, &info
, NULL
);
1509 fname_from_uuid(st
, &info
, nbuf1
, ':');
1510 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
1511 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
1515 static void export_examine_super_imsm(struct supertype
*st
)
1517 struct intel_super
*super
= st
->sb
;
1518 struct imsm_super
*mpb
= super
->anchor
;
1522 getinfo_super_imsm(st
, &info
, NULL
);
1523 fname_from_uuid(st
, &info
, nbuf
, ':');
1524 printf("MD_METADATA=imsm\n");
1525 printf("MD_LEVEL=container\n");
1526 printf("MD_UUID=%s\n", nbuf
+5);
1527 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
1530 static void detail_super_imsm(struct supertype
*st
, char *homehost
)
1535 getinfo_super_imsm(st
, &info
, NULL
);
1536 fname_from_uuid(st
, &info
, nbuf
, ':');
1537 printf("\n UUID : %s\n", nbuf
+ 5);
1540 static void brief_detail_super_imsm(struct supertype
*st
)
1544 getinfo_super_imsm(st
, &info
, NULL
);
1545 fname_from_uuid(st
, &info
, nbuf
, ':');
1546 printf(" UUID=%s", nbuf
+ 5);
1549 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
);
1550 static void fd2devname(int fd
, char *name
);
1552 static int ahci_enumerate_ports(const char *hba_path
, int port_count
, int host_base
, int verbose
)
1554 /* dump an unsorted list of devices attached to AHCI Intel storage
1555 * controller, as well as non-connected ports
1557 int hba_len
= strlen(hba_path
) + 1;
1562 unsigned long port_mask
= (1 << port_count
) - 1;
1564 if (port_count
> (int)sizeof(port_mask
) * 8) {
1566 fprintf(stderr
, Name
": port_count %d out of range\n", port_count
);
1570 /* scroll through /sys/dev/block looking for devices attached to
1573 dir
= opendir("/sys/dev/block");
1574 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
1585 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
1587 path
= devt_to_devpath(makedev(major
, minor
));
1590 if (!path_attached_to_hba(path
, hba_path
)) {
1596 /* retrieve the scsi device type */
1597 if (asprintf(&device
, "/sys/dev/block/%d:%d/device/xxxxxxx", major
, minor
) < 0) {
1599 fprintf(stderr
, Name
": failed to allocate 'device'\n");
1603 sprintf(device
, "/sys/dev/block/%d:%d/device/type", major
, minor
);
1604 if (load_sys(device
, buf
) != 0) {
1606 fprintf(stderr
, Name
": failed to read device type for %s\n",
1612 type
= strtoul(buf
, NULL
, 10);
1614 /* if it's not a disk print the vendor and model */
1615 if (!(type
== 0 || type
== 7 || type
== 14)) {
1618 sprintf(device
, "/sys/dev/block/%d:%d/device/vendor", major
, minor
);
1619 if (load_sys(device
, buf
) == 0) {
1620 strncpy(vendor
, buf
, sizeof(vendor
));
1621 vendor
[sizeof(vendor
) - 1] = '\0';
1622 c
= (char *) &vendor
[sizeof(vendor
) - 1];
1623 while (isspace(*c
) || *c
== '\0')
1627 sprintf(device
, "/sys/dev/block/%d:%d/device/model", major
, minor
);
1628 if (load_sys(device
, buf
) == 0) {
1629 strncpy(model
, buf
, sizeof(model
));
1630 model
[sizeof(model
) - 1] = '\0';
1631 c
= (char *) &model
[sizeof(model
) - 1];
1632 while (isspace(*c
) || *c
== '\0')
1636 if (vendor
[0] && model
[0])
1637 sprintf(buf
, "%.64s %.64s", vendor
, model
);
1639 switch (type
) { /* numbers from hald/linux/device.c */
1640 case 1: sprintf(buf
, "tape"); break;
1641 case 2: sprintf(buf
, "printer"); break;
1642 case 3: sprintf(buf
, "processor"); break;
1644 case 5: sprintf(buf
, "cdrom"); break;
1645 case 6: sprintf(buf
, "scanner"); break;
1646 case 8: sprintf(buf
, "media_changer"); break;
1647 case 9: sprintf(buf
, "comm"); break;
1648 case 12: sprintf(buf
, "raid"); break;
1649 default: sprintf(buf
, "unknown");
1655 /* chop device path to 'host%d' and calculate the port number */
1656 c
= strchr(&path
[hba_len
], '/');
1659 fprintf(stderr
, Name
": %s - invalid path name\n", path
+ hba_len
);
1664 if (sscanf(&path
[hba_len
], "host%d", &port
) == 1)
1668 *c
= '/'; /* repair the full string */
1669 fprintf(stderr
, Name
": failed to determine port number for %s\n",
1676 /* mark this port as used */
1677 port_mask
&= ~(1 << port
);
1679 /* print out the device information */
1681 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
1685 fd
= dev_open(ent
->d_name
, O_RDONLY
);
1687 printf(" Port%d : - disk info unavailable -\n", port
);
1689 fd2devname(fd
, buf
);
1690 printf(" Port%d : %s", port
, buf
);
1691 if (imsm_read_serial(fd
, NULL
, (__u8
*) buf
) == 0)
1692 printf(" (%.*s)\n", MAX_RAID_SERIAL_LEN
, buf
);
1707 for (i
= 0; i
< port_count
; i
++)
1708 if (port_mask
& (1 << i
))
1709 printf(" Port%d : - no device attached -\n", i
);
1715 static void print_found_intel_controllers(struct sys_dev
*elem
)
1717 for (; elem
; elem
= elem
->next
) {
1718 fprintf(stderr
, Name
": found Intel(R) ");
1719 if (elem
->type
== SYS_DEV_SATA
)
1720 fprintf(stderr
, "SATA ");
1721 else if (elem
->type
== SYS_DEV_SAS
)
1722 fprintf(stderr
, "SAS ");
1723 fprintf(stderr
, "RAID controller");
1725 fprintf(stderr
, " at %s", elem
->pci_id
);
1726 fprintf(stderr
, ".\n");
1731 static int ahci_get_port_count(const char *hba_path
, int *port_count
)
1738 if ((dir
= opendir(hba_path
)) == NULL
)
1741 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
1744 if (sscanf(ent
->d_name
, "host%d", &host
) != 1)
1746 if (*port_count
== 0)
1748 else if (host
< host_base
)
1751 if (host
+ 1 > *port_count
+ host_base
)
1752 *port_count
= host
+ 1 - host_base
;
1758 static void print_imsm_capability(const struct imsm_orom
*orom
)
1760 printf(" Platform : Intel(R) Matrix Storage Manager\n");
1761 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
1762 orom
->hotfix_ver
, orom
->build
);
1763 printf(" RAID Levels :%s%s%s%s%s\n",
1764 imsm_orom_has_raid0(orom
) ? " raid0" : "",
1765 imsm_orom_has_raid1(orom
) ? " raid1" : "",
1766 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
1767 imsm_orom_has_raid10(orom
) ? " raid10" : "",
1768 imsm_orom_has_raid5(orom
) ? " raid5" : "");
1769 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1770 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
1771 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
1772 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
1773 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
1774 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
1775 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
1776 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
1777 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
1778 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
1779 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
1780 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
1781 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
1782 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
1783 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
1784 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
1785 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
1786 printf(" 2TB volumes :%s supported\n",
1787 (orom
->attr
& IMSM_OROM_ATTR_2TB
)?"":" not");
1788 printf(" 2TB disks :%s supported\n",
1789 (orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
)?"":" not");
1790 printf(" Max Disks : %d\n", orom
->tds
);
1791 printf(" Max Volumes : %d per array, %d per controller\n",
1792 orom
->vpa
, orom
->vphba
);
1796 static int detail_platform_imsm(int verbose
, int enumerate_only
)
1798 /* There are two components to imsm platform support, the ahci SATA
1799 * controller and the option-rom. To find the SATA controller we
1800 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
1801 * controller with the Intel vendor id is present. This approach
1802 * allows mdadm to leverage the kernel's ahci detection logic, with the
1803 * caveat that if ahci.ko is not loaded mdadm will not be able to
1804 * detect platform raid capabilities. The option-rom resides in a
1805 * platform "Adapter ROM". We scan for its signature to retrieve the
1806 * platform capabilities. If raid support is disabled in the BIOS the
1807 * option-rom capability structure will not be available.
1809 const struct imsm_orom
*orom
;
1810 struct sys_dev
*list
, *hba
;
1815 if (enumerate_only
) {
1816 if (check_env("IMSM_NO_PLATFORM"))
1818 list
= find_intel_devices();
1821 for (hba
= list
; hba
; hba
= hba
->next
) {
1822 orom
= find_imsm_capability(hba
->type
);
1828 free_sys_dev(&list
);
1832 list
= find_intel_devices();
1835 fprintf(stderr
, Name
": no active Intel(R) RAID "
1836 "controller found.\n");
1837 free_sys_dev(&list
);
1840 print_found_intel_controllers(list
);
1842 for (hba
= list
; hba
; hba
= hba
->next
) {
1843 orom
= find_imsm_capability(hba
->type
);
1845 fprintf(stderr
, Name
": imsm capabilities not found for controller: %s (type %s)\n",
1846 hba
->path
, get_sys_dev_type(hba
->type
));
1848 print_imsm_capability(orom
);
1851 for (hba
= list
; hba
; hba
= hba
->next
) {
1852 printf(" I/O Controller : %s (%s)\n",
1853 hba
->path
, get_sys_dev_type(hba
->type
));
1855 if (hba
->type
== SYS_DEV_SATA
) {
1856 host_base
= ahci_get_port_count(hba
->path
, &port_count
);
1857 if (ahci_enumerate_ports(hba
->path
, port_count
, host_base
, verbose
)) {
1859 fprintf(stderr
, Name
": failed to enumerate "
1860 "ports on SATA controller at %s.", hba
->pci_id
);
1866 free_sys_dev(&list
);
1871 static int match_home_imsm(struct supertype
*st
, char *homehost
)
1873 /* the imsm metadata format does not specify any host
1874 * identification information. We return -1 since we can never
1875 * confirm nor deny whether a given array is "meant" for this
1876 * host. We rely on compare_super and the 'family_num' fields to
1877 * exclude member disks that do not belong, and we rely on
1878 * mdadm.conf to specify the arrays that should be assembled.
1879 * Auto-assembly may still pick up "foreign" arrays.
1885 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
1887 /* The uuid returned here is used for:
1888 * uuid to put into bitmap file (Create, Grow)
1889 * uuid for backup header when saving critical section (Grow)
1890 * comparing uuids when re-adding a device into an array
1891 * In these cases the uuid required is that of the data-array,
1892 * not the device-set.
1893 * uuid to recognise same set when adding a missing device back
1894 * to an array. This is a uuid for the device-set.
1896 * For each of these we can make do with a truncated
1897 * or hashed uuid rather than the original, as long as
1899 * In each case the uuid required is that of the data-array,
1900 * not the device-set.
1902 /* imsm does not track uuid's so we synthesis one using sha1 on
1903 * - The signature (Which is constant for all imsm array, but no matter)
1904 * - the orig_family_num of the container
1905 * - the index number of the volume
1906 * - the 'serial' number of the volume.
1907 * Hopefully these are all constant.
1909 struct intel_super
*super
= st
->sb
;
1912 struct sha1_ctx ctx
;
1913 struct imsm_dev
*dev
= NULL
;
1916 /* some mdadm versions failed to set ->orig_family_num, in which
1917 * case fall back to ->family_num. orig_family_num will be
1918 * fixed up with the first metadata update.
1920 family_num
= super
->anchor
->orig_family_num
;
1921 if (family_num
== 0)
1922 family_num
= super
->anchor
->family_num
;
1923 sha1_init_ctx(&ctx
);
1924 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
1925 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
1926 if (super
->current_vol
>= 0)
1927 dev
= get_imsm_dev(super
, super
->current_vol
);
1929 __u32 vol
= super
->current_vol
;
1930 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
1931 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
1933 sha1_finish_ctx(&ctx
, buf
);
1934 memcpy(uuid
, buf
, 4*4);
1939 get_imsm_numerical_version(struct imsm_super
*mpb
, int *m
, int *p
)
1941 __u8
*v
= get_imsm_version(mpb
);
1942 __u8
*end
= mpb
->sig
+ MAX_SIGNATURE_LENGTH
;
1943 char major
[] = { 0, 0, 0 };
1944 char minor
[] = { 0 ,0, 0 };
1945 char patch
[] = { 0, 0, 0 };
1946 char *ver_parse
[] = { major
, minor
, patch
};
1950 while (*v
!= '\0' && v
< end
) {
1951 if (*v
!= '.' && j
< 2)
1952 ver_parse
[i
][j
++] = *v
;
1960 *m
= strtol(minor
, NULL
, 0);
1961 *p
= strtol(patch
, NULL
, 0);
1965 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
1967 /* migr_strip_size when repairing or initializing parity */
1968 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1969 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1971 switch (get_imsm_raid_level(map
)) {
1976 return 128*1024 >> 9;
1980 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
1982 /* migr_strip_size when rebuilding a degraded disk, no idea why
1983 * this is different than migr_strip_size_resync(), but it's good
1986 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
1987 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1989 switch (get_imsm_raid_level(map
)) {
1992 if (map
->num_members
% map
->num_domains
== 0)
1993 return 128*1024 >> 9;
1997 return max((__u32
) 64*1024 >> 9, chunk
);
1999 return 128*1024 >> 9;
2003 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
2005 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2006 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2007 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
2008 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
2010 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
2013 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
2015 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2016 int level
= get_imsm_raid_level(lo
);
2018 if (level
== 1 || level
== 10) {
2019 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2021 return hi
->num_domains
;
2023 return num_stripes_per_unit_resync(dev
);
2026 static __u8
imsm_num_data_members(struct imsm_dev
*dev
, int second_map
)
2028 /* named 'imsm_' because raid0, raid1 and raid10
2029 * counter-intuitively have the same number of data disks
2031 struct imsm_map
*map
= get_imsm_map(dev
, second_map
);
2033 switch (get_imsm_raid_level(map
)) {
2037 return map
->num_members
;
2039 return map
->num_members
- 1;
2041 dprintf("%s: unsupported raid level\n", __func__
);
2046 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
2048 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2049 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2051 switch(get_imsm_raid_level(map
)) {
2054 return chunk
* map
->num_domains
;
2056 return chunk
* map
->num_members
;
2062 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
2064 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2065 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2066 __u32 strip
= block
/ chunk
;
2068 switch (get_imsm_raid_level(map
)) {
2071 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
2072 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
2074 return vol_stripe
* chunk
+ block
% chunk
;
2076 __u32 stripe
= strip
/ (map
->num_members
- 1);
2078 return stripe
* chunk
+ block
% chunk
;
2085 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
2086 struct imsm_dev
*dev
)
2088 /* calculate the conversion factor between per member 'blocks'
2089 * (md/{resync,rebuild}_start) and imsm migration units, return
2090 * 0 for the 'not migrating' and 'unsupported migration' cases
2092 if (!dev
->vol
.migr_state
)
2095 switch (migr_type(dev
)) {
2096 case MIGR_GEN_MIGR
: {
2097 struct migr_record
*migr_rec
= super
->migr_rec
;
2098 return __le32_to_cpu(migr_rec
->blocks_per_unit
);
2103 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2104 __u32 stripes_per_unit
;
2105 __u32 blocks_per_unit
;
2114 /* yes, this is really the translation of migr_units to
2115 * per-member blocks in the 'resync' case
2117 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
2118 migr_chunk
= migr_strip_blocks_resync(dev
);
2119 disks
= imsm_num_data_members(dev
, MAP_0
);
2120 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
2121 stripe
= __le16_to_cpu(map
->blocks_per_strip
) * disks
;
2122 segment
= blocks_per_unit
/ stripe
;
2123 block_rel
= blocks_per_unit
- segment
* stripe
;
2124 parity_depth
= parity_segment_depth(dev
);
2125 block_map
= map_migr_block(dev
, block_rel
);
2126 return block_map
+ parity_depth
* segment
;
2128 case MIGR_REBUILD
: {
2129 __u32 stripes_per_unit
;
2132 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
2133 migr_chunk
= migr_strip_blocks_rebuild(dev
);
2134 return migr_chunk
* stripes_per_unit
;
2136 case MIGR_STATE_CHANGE
:
2142 static int imsm_level_to_layout(int level
)
2150 return ALGORITHM_LEFT_ASYMMETRIC
;
2157 /*******************************************************************************
2158 * Function: read_imsm_migr_rec
2159 * Description: Function reads imsm migration record from last sector of disk
2161 * fd : disk descriptor
2162 * super : metadata info
2166 ******************************************************************************/
2167 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
)
2170 unsigned long long dsize
;
2172 get_dev_size(fd
, NULL
, &dsize
);
2173 if (lseek64(fd
, dsize
- MIGR_REC_POSITION
, SEEK_SET
) < 0) {
2175 Name
": Cannot seek to anchor block: %s\n",
2179 if (read(fd
, super
->migr_rec_buf
, MIGR_REC_BUF_SIZE
) !=
2180 MIGR_REC_BUF_SIZE
) {
2182 Name
": Cannot read migr record block: %s\n",
2192 static struct imsm_dev
*imsm_get_device_during_migration(
2193 struct intel_super
*super
)
2196 struct intel_dev
*dv
;
2198 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
2199 if (is_gen_migration(dv
->dev
))
2205 /*******************************************************************************
2206 * Function: load_imsm_migr_rec
2207 * Description: Function reads imsm migration record (it is stored at the last
2210 * super : imsm internal array info
2211 * info : general array info
2215 * -2 : no migration in progress
2216 ******************************************************************************/
2217 static int load_imsm_migr_rec(struct intel_super
*super
, struct mdinfo
*info
)
2220 struct dl
*dl
= NULL
;
2224 struct imsm_dev
*dev
;
2225 struct imsm_map
*map
= NULL
;
2228 /* find map under migration */
2229 dev
= imsm_get_device_during_migration(super
);
2230 /* nothing to load,no migration in progress?
2234 map
= get_imsm_map(dev
, MAP_0
);
2237 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
2238 /* skip spare and failed disks
2240 if (sd
->disk
.raid_disk
< 0)
2242 /* read only from one of the first two slots */
2244 slot
= get_imsm_disk_slot(map
,
2245 sd
->disk
.raid_disk
);
2246 if ((map
== NULL
) || (slot
> 1) || (slot
< 0))
2249 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
2250 fd
= dev_open(nm
, O_RDONLY
);
2256 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
2257 /* skip spare and failed disks
2261 /* read only from one of the first two slots */
2263 slot
= get_imsm_disk_slot(map
, dl
->index
);
2264 if ((map
== NULL
) || (slot
> 1) || (slot
< 0))
2266 sprintf(nm
, "%d:%d", dl
->major
, dl
->minor
);
2267 fd
= dev_open(nm
, O_RDONLY
);
2274 retval
= read_imsm_migr_rec(fd
, super
);
2283 /*******************************************************************************
2284 * function: imsm_create_metadata_checkpoint_update
2285 * Description: It creates update for checkpoint change.
2287 * super : imsm internal array info
2288 * u : pointer to prepared update
2291 * If length is equal to 0, input pointer u contains no update
2292 ******************************************************************************/
2293 static int imsm_create_metadata_checkpoint_update(
2294 struct intel_super
*super
,
2295 struct imsm_update_general_migration_checkpoint
**u
)
2298 int update_memory_size
= 0;
2300 dprintf("imsm_create_metadata_checkpoint_update(enter)\n");
2306 /* size of all update data without anchor */
2307 update_memory_size
=
2308 sizeof(struct imsm_update_general_migration_checkpoint
);
2310 *u
= calloc(1, update_memory_size
);
2312 dprintf("error: cannot get memory for "
2313 "imsm_create_metadata_checkpoint_update update\n");
2316 (*u
)->type
= update_general_migration_checkpoint
;
2317 (*u
)->curr_migr_unit
= __le32_to_cpu(super
->migr_rec
->curr_migr_unit
);
2318 dprintf("imsm_create_metadata_checkpoint_update: prepared for %u\n",
2319 (*u
)->curr_migr_unit
);
2321 return update_memory_size
;
2325 static void imsm_update_metadata_locally(struct supertype
*st
,
2326 void *buf
, int len
);
2328 /*******************************************************************************
2329 * Function: write_imsm_migr_rec
2330 * Description: Function writes imsm migration record
2331 * (at the last sector of disk)
2333 * super : imsm internal array info
2337 ******************************************************************************/
2338 static int write_imsm_migr_rec(struct supertype
*st
)
2340 struct intel_super
*super
= st
->sb
;
2341 unsigned long long dsize
;
2347 struct imsm_update_general_migration_checkpoint
*u
;
2348 struct imsm_dev
*dev
;
2349 struct imsm_map
*map
= NULL
;
2351 /* find map under migration */
2352 dev
= imsm_get_device_during_migration(super
);
2353 /* if no migration, write buffer anyway to clear migr_record
2354 * on disk based on first available device
2357 dev
= get_imsm_dev(super
, super
->current_vol
< 0 ? 0 :
2358 super
->current_vol
);
2360 map
= get_imsm_map(dev
, MAP_0
);
2362 for (sd
= super
->disks
; sd
; sd
= sd
->next
) {
2365 /* skip failed and spare devices */
2368 /* write to 2 first slots only */
2370 slot
= get_imsm_disk_slot(map
, sd
->index
);
2371 if ((map
== NULL
) || (slot
> 1) || (slot
< 0))
2374 sprintf(nm
, "%d:%d", sd
->major
, sd
->minor
);
2375 fd
= dev_open(nm
, O_RDWR
);
2378 get_dev_size(fd
, NULL
, &dsize
);
2379 if (lseek64(fd
, dsize
- MIGR_REC_POSITION
, SEEK_SET
) < 0) {
2381 Name
": Cannot seek to anchor block: %s\n",
2385 if (write(fd
, super
->migr_rec_buf
, MIGR_REC_BUF_SIZE
) !=
2386 MIGR_REC_BUF_SIZE
) {
2388 Name
": Cannot write migr record block: %s\n",
2395 /* update checkpoint information in metadata */
2396 len
= imsm_create_metadata_checkpoint_update(super
, &u
);
2399 dprintf("imsm: Cannot prepare update\n");
2402 /* update metadata locally */
2403 imsm_update_metadata_locally(st
, u
, len
);
2404 /* and possibly remotely */
2405 if (st
->update_tail
) {
2406 append_metadata_update(st
, u
, len
);
2407 /* during reshape we do all work inside metadata handler
2408 * manage_reshape(), so metadata update has to be triggered
2411 flush_metadata_updates(st
);
2412 st
->update_tail
= &st
->updates
;
2422 #endif /* MDASSEMBLE */
2424 /* spare/missing disks activations are not allowe when
2425 * array/container performs reshape operation, because
2426 * all arrays in container works on the same disks set
2428 int imsm_reshape_blocks_arrays_changes(struct intel_super
*super
)
2431 struct intel_dev
*i_dev
;
2432 struct imsm_dev
*dev
;
2434 /* check whole container
2436 for (i_dev
= super
->devlist
; i_dev
; i_dev
= i_dev
->next
) {
2438 if (is_gen_migration(dev
)) {
2439 /* No repair during any migration in container
2448 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
2450 struct intel_super
*super
= st
->sb
;
2451 struct migr_record
*migr_rec
= super
->migr_rec
;
2452 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
2453 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2454 struct imsm_map
*prev_map
= get_imsm_map(dev
, MAP_1
);
2455 struct imsm_map
*map_to_analyse
= map
;
2458 unsigned int component_size_alligment
;
2459 int map_disks
= info
->array
.raid_disks
;
2461 memset(info
, 0, sizeof(*info
));
2463 map_to_analyse
= prev_map
;
2465 dl
= super
->current_disk
;
2467 info
->container_member
= super
->current_vol
;
2468 info
->array
.raid_disks
= map
->num_members
;
2469 info
->array
.level
= get_imsm_raid_level(map_to_analyse
);
2470 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
2471 info
->array
.md_minor
= -1;
2472 info
->array
.ctime
= 0;
2473 info
->array
.utime
= 0;
2474 info
->array
.chunk_size
=
2475 __le16_to_cpu(map_to_analyse
->blocks_per_strip
) << 9;
2476 info
->array
.state
= !dev
->vol
.dirty
;
2477 info
->custom_array_size
= __le32_to_cpu(dev
->size_high
);
2478 info
->custom_array_size
<<= 32;
2479 info
->custom_array_size
|= __le32_to_cpu(dev
->size_low
);
2480 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
2482 if (is_gen_migration(dev
)) {
2483 info
->reshape_active
= 1;
2484 info
->new_level
= get_imsm_raid_level(map
);
2485 info
->new_layout
= imsm_level_to_layout(info
->new_level
);
2486 info
->new_chunk
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
2487 info
->delta_disks
= map
->num_members
- prev_map
->num_members
;
2488 if (info
->delta_disks
) {
2489 /* this needs to be applied to every array
2492 info
->reshape_active
= CONTAINER_RESHAPE
;
2494 /* We shape information that we give to md might have to be
2495 * modify to cope with md's requirement for reshaping arrays.
2496 * For example, when reshaping a RAID0, md requires it to be
2497 * presented as a degraded RAID4.
2498 * Also if a RAID0 is migrating to a RAID5 we need to specify
2499 * the array as already being RAID5, but the 'before' layout
2500 * is a RAID4-like layout.
2502 switch (info
->array
.level
) {
2504 switch(info
->new_level
) {
2506 /* conversion is happening as RAID4 */
2507 info
->array
.level
= 4;
2508 info
->array
.raid_disks
+= 1;
2511 /* conversion is happening as RAID5 */
2512 info
->array
.level
= 5;
2513 info
->array
.layout
= ALGORITHM_PARITY_N
;
2514 info
->delta_disks
-= 1;
2517 /* FIXME error message */
2518 info
->array
.level
= UnSet
;
2524 info
->new_level
= UnSet
;
2525 info
->new_layout
= UnSet
;
2526 info
->new_chunk
= info
->array
.chunk_size
;
2527 info
->delta_disks
= 0;
2531 info
->disk
.major
= dl
->major
;
2532 info
->disk
.minor
= dl
->minor
;
2533 info
->disk
.number
= dl
->index
;
2534 info
->disk
.raid_disk
= get_imsm_disk_slot(map_to_analyse
,
2538 info
->data_offset
= pba_of_lba0(map_to_analyse
);
2539 info
->component_size
= blocks_per_member(map_to_analyse
);
2541 /* check component size aligment
2543 component_size_alligment
=
2544 info
->component_size
% (info
->array
.chunk_size
/512);
2546 if (component_size_alligment
&&
2547 (info
->array
.level
!= 1) && (info
->array
.level
!= UnSet
)) {
2548 dprintf("imsm: reported component size alligned from %llu ",
2549 info
->component_size
);
2550 info
->component_size
-= component_size_alligment
;
2551 dprintf("to %llu (%i).\n",
2552 info
->component_size
, component_size_alligment
);
2555 memset(info
->uuid
, 0, sizeof(info
->uuid
));
2556 info
->recovery_start
= MaxSector
;
2558 info
->reshape_progress
= 0;
2559 info
->resync_start
= MaxSector
;
2560 if ((map_to_analyse
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
2562 imsm_reshape_blocks_arrays_changes(super
) == 0) {
2563 info
->resync_start
= 0;
2565 if (dev
->vol
.migr_state
) {
2566 switch (migr_type(dev
)) {
2569 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
2571 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
2573 info
->resync_start
= blocks_per_unit
* units
;
2576 case MIGR_GEN_MIGR
: {
2577 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
2579 __u64 units
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
2580 unsigned long long array_blocks
;
2583 if (__le32_to_cpu(migr_rec
->ascending_migr
) &&
2585 (__le32_to_cpu(migr_rec
->num_migr_units
)-1)) &&
2586 (super
->migr_rec
->rec_status
==
2587 __cpu_to_le32(UNIT_SRC_IN_CP_AREA
)))
2590 info
->reshape_progress
= blocks_per_unit
* units
;
2592 dprintf("IMSM: General Migration checkpoint : %llu "
2593 "(%llu) -> read reshape progress : %llu\n",
2594 (unsigned long long)units
,
2595 (unsigned long long)blocks_per_unit
,
2596 info
->reshape_progress
);
2598 used_disks
= imsm_num_data_members(dev
, MAP_1
);
2599 if (used_disks
> 0) {
2600 array_blocks
= blocks_per_member(map
) *
2602 /* round array size down to closest MB
2604 info
->custom_array_size
= (array_blocks
2605 >> SECT_PER_MB_SHIFT
)
2606 << SECT_PER_MB_SHIFT
;
2610 /* we could emulate the checkpointing of
2611 * 'sync_action=check' migrations, but for now
2612 * we just immediately complete them
2615 /* this is handled by container_content_imsm() */
2616 case MIGR_STATE_CHANGE
:
2617 /* FIXME handle other migrations */
2619 /* we are not dirty, so... */
2620 info
->resync_start
= MaxSector
;
2624 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
2625 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
2627 info
->array
.major_version
= -1;
2628 info
->array
.minor_version
= -2;
2629 devname
= devnum2devname(st
->container_dev
);
2630 *info
->text_version
= '\0';
2632 sprintf(info
->text_version
, "/%s/%d", devname
, info
->container_member
);
2634 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
2635 uuid_from_super_imsm(st
, info
->uuid
);
2639 for (i
=0; i
<map_disks
; i
++) {
2641 if (i
< info
->array
.raid_disks
) {
2642 struct imsm_disk
*dsk
;
2643 j
= get_imsm_disk_idx(dev
, i
, MAP_X
);
2644 dsk
= get_imsm_disk(super
, j
);
2645 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
2652 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
2653 int failed
, int look_in_map
);
2655 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
2660 static void manage_second_map(struct intel_super
*super
, struct imsm_dev
*dev
)
2662 if (is_gen_migration(dev
)) {
2665 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
2667 failed
= imsm_count_failed(super
, dev
, MAP_1
);
2668 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_1
);
2669 if (map2
->map_state
!= map_state
) {
2670 map2
->map_state
= map_state
;
2671 super
->updates_pending
++;
2677 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
2681 for (d
= super
->missing
; d
; d
= d
->next
)
2682 if (d
->index
== index
)
2687 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
2689 struct intel_super
*super
= st
->sb
;
2690 struct imsm_disk
*disk
;
2691 int map_disks
= info
->array
.raid_disks
;
2692 int max_enough
= -1;
2694 struct imsm_super
*mpb
;
2696 if (super
->current_vol
>= 0) {
2697 getinfo_super_imsm_volume(st
, info
, map
);
2700 memset(info
, 0, sizeof(*info
));
2702 /* Set raid_disks to zero so that Assemble will always pull in valid
2705 info
->array
.raid_disks
= 0;
2706 info
->array
.level
= LEVEL_CONTAINER
;
2707 info
->array
.layout
= 0;
2708 info
->array
.md_minor
= -1;
2709 info
->array
.ctime
= 0; /* N/A for imsm */
2710 info
->array
.utime
= 0;
2711 info
->array
.chunk_size
= 0;
2713 info
->disk
.major
= 0;
2714 info
->disk
.minor
= 0;
2715 info
->disk
.raid_disk
= -1;
2716 info
->reshape_active
= 0;
2717 info
->array
.major_version
= -1;
2718 info
->array
.minor_version
= -2;
2719 strcpy(info
->text_version
, "imsm");
2720 info
->safe_mode_delay
= 0;
2721 info
->disk
.number
= -1;
2722 info
->disk
.state
= 0;
2724 info
->recovery_start
= MaxSector
;
2725 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
2727 /* do we have the all the insync disks that we expect? */
2728 mpb
= super
->anchor
;
2730 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
2731 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
2732 int failed
, enough
, j
, missing
= 0;
2733 struct imsm_map
*map
;
2736 failed
= imsm_count_failed(super
, dev
, MAP_0
);
2737 state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
2738 map
= get_imsm_map(dev
, MAP_0
);
2740 /* any newly missing disks?
2741 * (catches single-degraded vs double-degraded)
2743 for (j
= 0; j
< map
->num_members
; j
++) {
2744 __u32 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
2745 __u32 idx
= ord_to_idx(ord
);
2747 if (!(ord
& IMSM_ORD_REBUILD
) &&
2748 get_imsm_missing(super
, idx
)) {
2754 if (state
== IMSM_T_STATE_FAILED
)
2756 else if (state
== IMSM_T_STATE_DEGRADED
&&
2757 (state
!= map
->map_state
|| missing
))
2759 else /* we're normal, or already degraded */
2761 if (is_gen_migration(dev
) && missing
) {
2762 /* during general migration we need all disks
2763 * that process is running on.
2764 * No new missing disk is allowed.
2768 /* no more checks necessary
2772 /* in the missing/failed disk case check to see
2773 * if at least one array is runnable
2775 max_enough
= max(max_enough
, enough
);
2777 dprintf("%s: enough: %d\n", __func__
, max_enough
);
2778 info
->container_enough
= max_enough
;
2781 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
2783 disk
= &super
->disks
->disk
;
2784 info
->data_offset
= total_blocks(&super
->disks
->disk
) - reserved
;
2785 info
->component_size
= reserved
;
2786 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
2787 /* we don't change info->disk.raid_disk here because
2788 * this state will be finalized in mdmon after we have
2789 * found the 'most fresh' version of the metadata
2791 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
2792 info
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
2795 /* only call uuid_from_super_imsm when this disk is part of a populated container,
2796 * ->compare_super may have updated the 'num_raid_devs' field for spares
2798 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
2799 uuid_from_super_imsm(st
, info
->uuid
);
2801 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
2803 /* I don't know how to compute 'map' on imsm, so use safe default */
2806 for (i
= 0; i
< map_disks
; i
++)
2812 /* allocates memory and fills disk in mdinfo structure
2813 * for each disk in array */
2814 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
2816 struct mdinfo
*mddev
= NULL
;
2817 struct intel_super
*super
= st
->sb
;
2818 struct imsm_disk
*disk
;
2821 if (!super
|| !super
->disks
)
2824 mddev
= malloc(sizeof(*mddev
));
2826 fprintf(stderr
, Name
": Failed to allocate memory.\n");
2829 memset(mddev
, 0, sizeof(*mddev
));
2833 tmp
= malloc(sizeof(*tmp
));
2835 fprintf(stderr
, Name
": Failed to allocate memory.\n");
2840 memset(tmp
, 0, sizeof(*tmp
));
2842 tmp
->next
= mddev
->devs
;
2844 tmp
->disk
.number
= count
++;
2845 tmp
->disk
.major
= dl
->major
;
2846 tmp
->disk
.minor
= dl
->minor
;
2847 tmp
->disk
.state
= is_configured(disk
) ?
2848 (1 << MD_DISK_ACTIVE
) : 0;
2849 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
2850 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
2851 tmp
->disk
.raid_disk
= -1;
2857 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
2858 char *update
, char *devname
, int verbose
,
2859 int uuid_set
, char *homehost
)
2861 /* For 'assemble' and 'force' we need to return non-zero if any
2862 * change was made. For others, the return value is ignored.
2863 * Update options are:
2864 * force-one : This device looks a bit old but needs to be included,
2865 * update age info appropriately.
2866 * assemble: clear any 'faulty' flag to allow this device to
2868 * force-array: Array is degraded but being forced, mark it clean
2869 * if that will be needed to assemble it.
2871 * newdev: not used ????
2872 * grow: Array has gained a new device - this is currently for
2874 * resync: mark as dirty so a resync will happen.
2875 * name: update the name - preserving the homehost
2876 * uuid: Change the uuid of the array to match watch is given
2878 * Following are not relevant for this imsm:
2879 * sparc2.2 : update from old dodgey metadata
2880 * super-minor: change the preferred_minor number
2881 * summaries: update redundant counters.
2882 * homehost: update the recorded homehost
2883 * _reshape_progress: record new reshape_progress position.
2886 struct intel_super
*super
= st
->sb
;
2887 struct imsm_super
*mpb
;
2889 /* we can only update container info */
2890 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
2893 mpb
= super
->anchor
;
2895 if (strcmp(update
, "uuid") == 0) {
2896 /* We take this to mean that the family_num should be updated.
2897 * However that is much smaller than the uuid so we cannot really
2898 * allow an explicit uuid to be given. And it is hard to reliably
2900 * So if !uuid_set we know the current uuid is random and just used
2901 * the first 'int' and copy it to the other 3 positions.
2902 * Otherwise we require the 4 'int's to be the same as would be the
2903 * case if we are using a random uuid. So an explicit uuid will be
2904 * accepted as long as all for ints are the same... which shouldn't hurt
2907 info
->uuid
[1] = info
->uuid
[2] = info
->uuid
[3] = info
->uuid
[0];
2910 if (info
->uuid
[0] != info
->uuid
[1] ||
2911 info
->uuid
[1] != info
->uuid
[2] ||
2912 info
->uuid
[2] != info
->uuid
[3])
2918 mpb
->orig_family_num
= info
->uuid
[0];
2919 } else if (strcmp(update
, "assemble") == 0)
2924 /* successful update? recompute checksum */
2926 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
2931 static size_t disks_to_mpb_size(int disks
)
2935 size
= sizeof(struct imsm_super
);
2936 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
2937 size
+= 2 * sizeof(struct imsm_dev
);
2938 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
2939 size
+= (4 - 2) * sizeof(struct imsm_map
);
2940 /* 4 possible disk_ord_tbl's */
2941 size
+= 4 * (disks
- 1) * sizeof(__u32
);
2946 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
)
2948 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
2951 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
2954 static void free_devlist(struct intel_super
*super
)
2956 struct intel_dev
*dv
;
2958 while (super
->devlist
) {
2959 dv
= super
->devlist
->next
;
2960 free(super
->devlist
->dev
);
2961 free(super
->devlist
);
2962 super
->devlist
= dv
;
2966 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
2968 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
2971 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
)
2975 * 0 same, or first was empty, and second was copied
2976 * 1 second had wrong number
2978 * 3 wrong other info
2980 struct intel_super
*first
= st
->sb
;
2981 struct intel_super
*sec
= tst
->sb
;
2988 /* in platform dependent environment test if the disks
2989 * use the same Intel hba
2991 if (!check_env("IMSM_NO_PLATFORM")) {
2992 if (!first
->hba
|| !sec
->hba
||
2993 (first
->hba
->type
!= sec
->hba
->type
)) {
2995 "HBAs of devices does not match %s != %s\n",
2996 first
->hba
? get_sys_dev_type(first
->hba
->type
) : NULL
,
2997 sec
->hba
? get_sys_dev_type(sec
->hba
->type
) : NULL
);
3002 /* if an anchor does not have num_raid_devs set then it is a free
3005 if (first
->anchor
->num_raid_devs
> 0 &&
3006 sec
->anchor
->num_raid_devs
> 0) {
3007 /* Determine if these disks might ever have been
3008 * related. Further disambiguation can only take place
3009 * in load_super_imsm_all
3011 __u32 first_family
= first
->anchor
->orig_family_num
;
3012 __u32 sec_family
= sec
->anchor
->orig_family_num
;
3014 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
3015 MAX_SIGNATURE_LENGTH
) != 0)
3018 if (first_family
== 0)
3019 first_family
= first
->anchor
->family_num
;
3020 if (sec_family
== 0)
3021 sec_family
= sec
->anchor
->family_num
;
3023 if (first_family
!= sec_family
)
3029 /* if 'first' is a spare promote it to a populated mpb with sec's
3032 if (first
->anchor
->num_raid_devs
== 0 &&
3033 sec
->anchor
->num_raid_devs
> 0) {
3035 struct intel_dev
*dv
;
3036 struct imsm_dev
*dev
;
3038 /* we need to copy raid device info from sec if an allocation
3039 * fails here we don't associate the spare
3041 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++) {
3042 dv
= malloc(sizeof(*dv
));
3045 dev
= malloc(sizeof_imsm_dev(get_imsm_dev(sec
, i
), 1));
3052 dv
->next
= first
->devlist
;
3053 first
->devlist
= dv
;
3055 if (i
< sec
->anchor
->num_raid_devs
) {
3056 /* allocation failure */
3057 free_devlist(first
);
3058 fprintf(stderr
, "imsm: failed to associate spare\n");
3061 first
->anchor
->num_raid_devs
= sec
->anchor
->num_raid_devs
;
3062 first
->anchor
->orig_family_num
= sec
->anchor
->orig_family_num
;
3063 first
->anchor
->family_num
= sec
->anchor
->family_num
;
3064 memcpy(first
->anchor
->sig
, sec
->anchor
->sig
, MAX_SIGNATURE_LENGTH
);
3065 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++)
3066 imsm_copy_dev(get_imsm_dev(first
, i
), get_imsm_dev(sec
, i
));
3072 static void fd2devname(int fd
, char *name
)
3076 char dname
[PATH_MAX
];
3081 if (fstat(fd
, &st
) != 0)
3083 sprintf(path
, "/sys/dev/block/%d:%d",
3084 major(st
.st_rdev
), minor(st
.st_rdev
));
3086 rv
= readlink(path
, dname
, sizeof(dname
)-1);
3091 nm
= strrchr(dname
, '/');
3094 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
3098 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
3100 static int imsm_read_serial(int fd
, char *devname
,
3101 __u8 serial
[MAX_RAID_SERIAL_LEN
])
3103 unsigned char scsi_serial
[255];
3112 memset(scsi_serial
, 0, sizeof(scsi_serial
));
3114 rv
= scsi_get_serial(fd
, scsi_serial
, sizeof(scsi_serial
));
3116 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
3117 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3118 fd2devname(fd
, (char *) serial
);
3125 Name
": Failed to retrieve serial for %s\n",
3130 rsp_len
= scsi_serial
[3];
3134 Name
": Failed to retrieve serial for %s\n",
3138 rsp_buf
= (char *) &scsi_serial
[4];
3140 /* trim all whitespace and non-printable characters and convert
3143 for (i
= 0, dest
= rsp_buf
; i
< rsp_len
; i
++) {
3146 /* ':' is reserved for use in placeholder serial
3147 * numbers for missing disks
3155 len
= dest
- rsp_buf
;
3158 /* truncate leading characters */
3159 if (len
> MAX_RAID_SERIAL_LEN
) {
3160 dest
+= len
- MAX_RAID_SERIAL_LEN
;
3161 len
= MAX_RAID_SERIAL_LEN
;
3164 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3165 memcpy(serial
, dest
, len
);
3170 static int serialcmp(__u8
*s1
, __u8
*s2
)
3172 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
3175 static void serialcpy(__u8
*dest
, __u8
*src
)
3177 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
3180 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
3184 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3185 if (serialcmp(dl
->serial
, serial
) == 0)
3191 static struct imsm_disk
*
3192 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
3196 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3197 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3199 if (serialcmp(disk
->serial
, serial
) == 0) {
3210 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3212 struct imsm_disk
*disk
;
3217 __u8 serial
[MAX_RAID_SERIAL_LEN
];
3219 rv
= imsm_read_serial(fd
, devname
, serial
);
3224 dl
= calloc(1, sizeof(*dl
));
3228 Name
": failed to allocate disk buffer for %s\n",
3234 dl
->major
= major(stb
.st_rdev
);
3235 dl
->minor
= minor(stb
.st_rdev
);
3236 dl
->next
= super
->disks
;
3237 dl
->fd
= keep_fd
? fd
: -1;
3238 assert(super
->disks
== NULL
);
3240 serialcpy(dl
->serial
, serial
);
3243 fd2devname(fd
, name
);
3245 dl
->devname
= strdup(devname
);
3247 dl
->devname
= strdup(name
);
3249 /* look up this disk's index in the current anchor */
3250 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
3253 /* only set index on disks that are a member of a
3254 * populated contianer, i.e. one with raid_devs
3256 if (is_failed(&dl
->disk
))
3258 else if (is_spare(&dl
->disk
))
3266 /* When migrating map0 contains the 'destination' state while map1
3267 * contains the current state. When not migrating map0 contains the
3268 * current state. This routine assumes that map[0].map_state is set to
3269 * the current array state before being called.
3271 * Migration is indicated by one of the following states
3272 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
3273 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
3274 * map1state=unitialized)
3275 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
3277 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
3278 * map1state=degraded)
3279 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
3282 static void migrate(struct imsm_dev
*dev
, struct intel_super
*super
,
3283 __u8 to_state
, int migr_type
)
3285 struct imsm_map
*dest
;
3286 struct imsm_map
*src
= get_imsm_map(dev
, MAP_0
);
3288 dev
->vol
.migr_state
= 1;
3289 set_migr_type(dev
, migr_type
);
3290 dev
->vol
.curr_migr_unit
= 0;
3291 dest
= get_imsm_map(dev
, MAP_1
);
3293 /* duplicate and then set the target end state in map[0] */
3294 memcpy(dest
, src
, sizeof_imsm_map(src
));
3295 if ((migr_type
== MIGR_REBUILD
) ||
3296 (migr_type
== MIGR_GEN_MIGR
)) {
3300 for (i
= 0; i
< src
->num_members
; i
++) {
3301 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
3302 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
3306 if (migr_type
== MIGR_GEN_MIGR
)
3307 /* Clear migration record */
3308 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
3310 src
->map_state
= to_state
;
3313 static void end_migration(struct imsm_dev
*dev
, struct intel_super
*super
,
3316 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3317 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
== 0 ?
3321 /* merge any IMSM_ORD_REBUILD bits that were not successfully
3322 * completed in the last migration.
3324 * FIXME add support for raid-level-migration
3326 if ((map_state
!= map
->map_state
) && (is_gen_migration(dev
) == 0) &&
3327 (prev
->map_state
!= IMSM_T_STATE_UNINITIALIZED
)) {
3328 /* when final map state is other than expected
3329 * merge maps (not for migration)
3333 for (i
= 0; i
< prev
->num_members
; i
++)
3334 for (j
= 0; j
< map
->num_members
; j
++)
3335 /* during online capacity expansion
3336 * disks position can be changed
3337 * if takeover is used
3339 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
3340 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
3341 map
->disk_ord_tbl
[j
] |=
3342 prev
->disk_ord_tbl
[i
];
3345 failed
= imsm_count_failed(super
, dev
, MAP_0
);
3346 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
3349 dev
->vol
.migr_state
= 0;
3350 set_migr_type(dev
, 0);
3351 dev
->vol
.curr_migr_unit
= 0;
3352 map
->map_state
= map_state
;
3356 static int parse_raid_devices(struct intel_super
*super
)
3359 struct imsm_dev
*dev_new
;
3360 size_t len
, len_migr
;
3362 size_t space_needed
= 0;
3363 struct imsm_super
*mpb
= super
->anchor
;
3365 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3366 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3367 struct intel_dev
*dv
;
3369 len
= sizeof_imsm_dev(dev_iter
, 0);
3370 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
3372 space_needed
+= len_migr
- len
;
3374 dv
= malloc(sizeof(*dv
));
3377 if (max_len
< len_migr
)
3379 if (max_len
> len_migr
)
3380 space_needed
+= max_len
- len_migr
;
3381 dev_new
= malloc(max_len
);
3386 imsm_copy_dev(dev_new
, dev_iter
);
3389 dv
->next
= super
->devlist
;
3390 super
->devlist
= dv
;
3393 /* ensure that super->buf is large enough when all raid devices
3396 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
3399 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
, 512);
3400 if (posix_memalign(&buf
, 512, len
) != 0)
3403 memcpy(buf
, super
->buf
, super
->len
);
3404 memset(buf
+ super
->len
, 0, len
- super
->len
);
3413 /* retrieve a pointer to the bbm log which starts after all raid devices */
3414 struct bbm_log
*__get_imsm_bbm_log(struct imsm_super
*mpb
)
3418 if (__le32_to_cpu(mpb
->bbm_log_size
)) {
3420 ptr
+= mpb
->mpb_size
- __le32_to_cpu(mpb
->bbm_log_size
);
3426 /*******************************************************************************
3427 * Function: check_mpb_migr_compatibility
3428 * Description: Function checks for unsupported migration features:
3429 * - migration optimization area (pba_of_lba0)
3430 * - descending reshape (ascending_migr)
3432 * super : imsm metadata information
3434 * 0 : migration is compatible
3435 * -1 : migration is not compatible
3436 ******************************************************************************/
3437 int check_mpb_migr_compatibility(struct intel_super
*super
)
3439 struct imsm_map
*map0
, *map1
;
3440 struct migr_record
*migr_rec
= super
->migr_rec
;
3443 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3444 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3447 dev_iter
->vol
.migr_state
== 1 &&
3448 dev_iter
->vol
.migr_type
== MIGR_GEN_MIGR
) {
3449 /* This device is migrating */
3450 map0
= get_imsm_map(dev_iter
, MAP_0
);
3451 map1
= get_imsm_map(dev_iter
, MAP_1
);
3452 if (pba_of_lba0(map0
) != pba_of_lba0(map1
))
3453 /* migration optimization area was used */
3455 if (migr_rec
->ascending_migr
== 0
3456 && migr_rec
->dest_depth_per_unit
> 0)
3457 /* descending reshape not supported yet */
3464 static void __free_imsm(struct intel_super
*super
, int free_disks
);
3466 /* load_imsm_mpb - read matrix metadata
3467 * allocates super->mpb to be freed by free_imsm
3469 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
3471 unsigned long long dsize
;
3472 unsigned long long sectors
;
3474 struct imsm_super
*anchor
;
3477 get_dev_size(fd
, NULL
, &dsize
);
3481 Name
": %s: device to small for imsm\n",
3486 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0) {
3488 fprintf(stderr
, Name
3489 ": Cannot seek to anchor block on %s: %s\n",
3490 devname
, strerror(errno
));
3494 if (posix_memalign((void**)&anchor
, 512, 512) != 0) {
3497 Name
": Failed to allocate imsm anchor buffer"
3498 " on %s\n", devname
);
3501 if (read(fd
, anchor
, 512) != 512) {
3504 Name
": Cannot read anchor block on %s: %s\n",
3505 devname
, strerror(errno
));
3510 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
3513 Name
": no IMSM anchor on %s\n", devname
);
3518 __free_imsm(super
, 0);
3519 /* reload capability and hba */
3521 /* capability and hba must be updated with new super allocation */
3522 find_intel_hba_capability(fd
, super
, devname
);
3523 super
->len
= ROUND_UP(anchor
->mpb_size
, 512);
3524 if (posix_memalign(&super
->buf
, 512, super
->len
) != 0) {
3527 Name
": unable to allocate %zu byte mpb buffer\n",
3532 memcpy(super
->buf
, anchor
, 512);
3534 sectors
= mpb_sectors(anchor
) - 1;
3537 if (posix_memalign(&super
->migr_rec_buf
, 512, MIGR_REC_BUF_SIZE
) != 0) {
3538 fprintf(stderr
, Name
3539 ": %s could not allocate migr_rec buffer\n", __func__
);
3543 super
->clean_migration_record_by_mdmon
= 0;
3546 check_sum
= __gen_imsm_checksum(super
->anchor
);
3547 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
3550 Name
": IMSM checksum %x != %x on %s\n",
3552 __le32_to_cpu(super
->anchor
->check_sum
),
3560 /* read the extended mpb */
3561 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0) {
3564 Name
": Cannot seek to extended mpb on %s: %s\n",
3565 devname
, strerror(errno
));
3569 if ((unsigned)read(fd
, super
->buf
+ 512, super
->len
- 512) != super
->len
- 512) {
3572 Name
": Cannot read extended mpb on %s: %s\n",
3573 devname
, strerror(errno
));
3577 check_sum
= __gen_imsm_checksum(super
->anchor
);
3578 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
3581 Name
": IMSM checksum %x != %x on %s\n",
3582 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
3587 /* FIXME the BBM log is disk specific so we cannot use this global
3588 * buffer for all disks. Ok for now since we only look at the global
3589 * bbm_log_size parameter to gate assembly
3591 super
->bbm_log
= __get_imsm_bbm_log(super
->anchor
);
3596 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
);
3598 /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
3599 static void clear_hi(struct intel_super
*super
)
3601 struct imsm_super
*mpb
= super
->anchor
;
3603 if (mpb
->attributes
& MPB_ATTRIB_2TB_DISK
)
3605 for (i
= 0; i
< mpb
->num_disks
; ++i
) {
3606 struct imsm_disk
*disk
= &mpb
->disk
[i
];
3607 disk
->total_blocks_hi
= 0;
3609 for (i
= 0; i
< mpb
->num_raid_devs
; ++i
) {
3610 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3613 for (n
= 0; n
< 2; ++n
) {
3614 struct imsm_map
*map
= get_imsm_map(dev
, n
);
3617 map
->pba_of_lba0_hi
= 0;
3618 map
->blocks_per_member_hi
= 0;
3619 map
->num_data_stripes_hi
= 0;
3625 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3629 err
= load_imsm_mpb(fd
, super
, devname
);
3632 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
3635 err
= parse_raid_devices(super
);
3640 static void __free_imsm_disk(struct dl
*d
)
3652 static void free_imsm_disks(struct intel_super
*super
)
3656 while (super
->disks
) {
3658 super
->disks
= d
->next
;
3659 __free_imsm_disk(d
);
3661 while (super
->disk_mgmt_list
) {
3662 d
= super
->disk_mgmt_list
;
3663 super
->disk_mgmt_list
= d
->next
;
3664 __free_imsm_disk(d
);
3666 while (super
->missing
) {
3668 super
->missing
= d
->next
;
3669 __free_imsm_disk(d
);
3674 /* free all the pieces hanging off of a super pointer */
3675 static void __free_imsm(struct intel_super
*super
, int free_disks
)
3677 struct intel_hba
*elem
, *next
;
3683 /* unlink capability description */
3685 if (super
->migr_rec_buf
) {
3686 free(super
->migr_rec_buf
);
3687 super
->migr_rec_buf
= NULL
;
3690 free_imsm_disks(super
);
3691 free_devlist(super
);
3695 free((void *)elem
->path
);
3703 static void free_imsm(struct intel_super
*super
)
3705 __free_imsm(super
, 1);
3709 static void free_super_imsm(struct supertype
*st
)
3711 struct intel_super
*super
= st
->sb
;
3720 static struct intel_super
*alloc_super(void)
3722 struct intel_super
*super
= malloc(sizeof(*super
));
3725 memset(super
, 0, sizeof(*super
));
3726 super
->current_vol
= -1;
3727 super
->create_offset
= ~((unsigned long long) 0);
3733 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
3735 static int find_intel_hba_capability(int fd
, struct intel_super
*super
, char *devname
)
3737 struct sys_dev
*hba_name
;
3740 if ((fd
< 0) || check_env("IMSM_NO_PLATFORM")) {
3745 hba_name
= find_disk_attached_hba(fd
, NULL
);
3749 Name
": %s is not attached to Intel(R) RAID controller.\n",
3753 rv
= attach_hba_to_super(super
, hba_name
);
3756 struct intel_hba
*hba
= super
->hba
;
3758 fprintf(stderr
, Name
": %s is attached to Intel(R) %s RAID "
3759 "controller (%s),\n"
3760 " but the container is assigned to Intel(R) "
3761 "%s RAID controller (",
3764 hba_name
->pci_id
? : "Err!",
3765 get_sys_dev_type(hba_name
->type
));
3768 fprintf(stderr
, "%s", hba
->pci_id
? : "Err!");
3770 fprintf(stderr
, ", ");
3774 fprintf(stderr
, ").\n"
3775 " Mixing devices attached to different controllers "
3776 "is not allowed.\n");
3778 free_sys_dev(&hba_name
);
3781 super
->orom
= find_imsm_capability(hba_name
->type
);
3782 free_sys_dev(&hba_name
);
3788 /* find_missing - helper routine for load_super_imsm_all that identifies
3789 * disks that have disappeared from the system. This routine relies on
3790 * the mpb being uptodate, which it is at load time.
3792 static int find_missing(struct intel_super
*super
)
3795 struct imsm_super
*mpb
= super
->anchor
;
3797 struct imsm_disk
*disk
;
3799 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3800 disk
= __get_imsm_disk(mpb
, i
);
3801 dl
= serial_to_dl(disk
->serial
, super
);
3805 dl
= malloc(sizeof(*dl
));
3811 dl
->devname
= strdup("missing");
3813 serialcpy(dl
->serial
, disk
->serial
);
3816 dl
->next
= super
->missing
;
3817 super
->missing
= dl
;
3824 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
3826 struct intel_disk
*idisk
= disk_list
;
3829 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
3831 idisk
= idisk
->next
;
3837 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
3838 struct intel_super
*super
,
3839 struct intel_disk
**disk_list
)
3841 struct imsm_disk
*d
= &super
->disks
->disk
;
3842 struct imsm_super
*mpb
= super
->anchor
;
3845 for (i
= 0; i
< tbl_size
; i
++) {
3846 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
3847 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
3849 if (tbl_mpb
->family_num
== mpb
->family_num
) {
3850 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
3851 dprintf("%s: mpb from %d:%d matches %d:%d\n",
3852 __func__
, super
->disks
->major
,
3853 super
->disks
->minor
,
3854 table
[i
]->disks
->major
,
3855 table
[i
]->disks
->minor
);
3859 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
3860 is_configured(d
) == is_configured(tbl_d
)) &&
3861 tbl_mpb
->generation_num
< mpb
->generation_num
) {
3862 /* current version of the mpb is a
3863 * better candidate than the one in
3864 * super_table, but copy over "cross
3865 * generational" status
3867 struct intel_disk
*idisk
;
3869 dprintf("%s: mpb from %d:%d replaces %d:%d\n",
3870 __func__
, super
->disks
->major
,
3871 super
->disks
->minor
,
3872 table
[i
]->disks
->major
,
3873 table
[i
]->disks
->minor
);
3875 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
3876 if (idisk
&& is_failed(&idisk
->disk
))
3877 tbl_d
->status
|= FAILED_DISK
;
3880 struct intel_disk
*idisk
;
3881 struct imsm_disk
*disk
;
3883 /* tbl_mpb is more up to date, but copy
3884 * over cross generational status before
3887 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
3888 if (disk
&& is_failed(disk
))
3889 d
->status
|= FAILED_DISK
;
3891 idisk
= disk_list_get(d
->serial
, *disk_list
);
3894 if (disk
&& is_configured(disk
))
3895 idisk
->disk
.status
|= CONFIGURED_DISK
;
3898 dprintf("%s: mpb from %d:%d prefer %d:%d\n",
3899 __func__
, super
->disks
->major
,
3900 super
->disks
->minor
,
3901 table
[i
]->disks
->major
,
3902 table
[i
]->disks
->minor
);
3910 table
[tbl_size
++] = super
;
3914 /* update/extend the merged list of imsm_disk records */
3915 for (j
= 0; j
< mpb
->num_disks
; j
++) {
3916 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
3917 struct intel_disk
*idisk
;
3919 idisk
= disk_list_get(disk
->serial
, *disk_list
);
3921 idisk
->disk
.status
|= disk
->status
;
3922 if (is_configured(&idisk
->disk
) ||
3923 is_failed(&idisk
->disk
))
3924 idisk
->disk
.status
&= ~(SPARE_DISK
);
3926 idisk
= calloc(1, sizeof(*idisk
));
3929 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
3930 idisk
->disk
= *disk
;
3931 idisk
->next
= *disk_list
;
3935 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
3942 static struct intel_super
*
3943 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
3946 struct imsm_super
*mpb
= super
->anchor
;
3950 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3951 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3952 struct intel_disk
*idisk
;
3954 idisk
= disk_list_get(disk
->serial
, disk_list
);
3956 if (idisk
->owner
== owner
||
3957 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
3960 dprintf("%s: '%.16s' owner %d != %d\n",
3961 __func__
, disk
->serial
, idisk
->owner
,
3964 dprintf("%s: unknown disk %x [%d]: %.16s\n",
3965 __func__
, __le32_to_cpu(mpb
->family_num
), i
,
3971 if (ok_count
== mpb
->num_disks
)
3976 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
3978 struct intel_super
*s
;
3980 for (s
= super_list
; s
; s
= s
->next
) {
3981 if (family_num
!= s
->anchor
->family_num
)
3983 fprintf(stderr
, "Conflict, offlining family %#x on '%s'\n",
3984 __le32_to_cpu(family_num
), s
->disks
->devname
);
3988 static struct intel_super
*
3989 imsm_thunderdome(struct intel_super
**super_list
, int len
)
3991 struct intel_super
*super_table
[len
];
3992 struct intel_disk
*disk_list
= NULL
;
3993 struct intel_super
*champion
, *spare
;
3994 struct intel_super
*s
, **del
;
3999 memset(super_table
, 0, sizeof(super_table
));
4000 for (s
= *super_list
; s
; s
= s
->next
)
4001 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
4003 for (i
= 0; i
< tbl_size
; i
++) {
4004 struct imsm_disk
*d
;
4005 struct intel_disk
*idisk
;
4006 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
4009 d
= &s
->disks
->disk
;
4011 /* 'd' must appear in merged disk list for its
4012 * configuration to be valid
4014 idisk
= disk_list_get(d
->serial
, disk_list
);
4015 if (idisk
&& idisk
->owner
== i
)
4016 s
= validate_members(s
, disk_list
, i
);
4021 dprintf("%s: marking family: %#x from %d:%d offline\n",
4022 __func__
, mpb
->family_num
,
4023 super_table
[i
]->disks
->major
,
4024 super_table
[i
]->disks
->minor
);
4028 /* This is where the mdadm implementation differs from the Windows
4029 * driver which has no strict concept of a container. We can only
4030 * assemble one family from a container, so when returning a prodigal
4031 * array member to this system the code will not be able to disambiguate
4032 * the container contents that should be assembled ("foreign" versus
4033 * "local"). It requires user intervention to set the orig_family_num
4034 * to a new value to establish a new container. The Windows driver in
4035 * this situation fixes up the volume name in place and manages the
4036 * foreign array as an independent entity.
4041 for (i
= 0; i
< tbl_size
; i
++) {
4042 struct intel_super
*tbl_ent
= super_table
[i
];
4048 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
4053 if (s
&& !is_spare
) {
4054 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
4056 } else if (!s
&& !is_spare
)
4069 fprintf(stderr
, "Chose family %#x on '%s', "
4070 "assemble conflicts to new container with '--update=uuid'\n",
4071 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
4073 /* collect all dl's onto 'champion', and update them to
4074 * champion's version of the status
4076 for (s
= *super_list
; s
; s
= s
->next
) {
4077 struct imsm_super
*mpb
= champion
->anchor
;
4078 struct dl
*dl
= s
->disks
;
4083 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4084 struct imsm_disk
*disk
;
4086 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
4089 /* only set index on disks that are a member of
4090 * a populated contianer, i.e. one with
4093 if (is_failed(&dl
->disk
))
4095 else if (is_spare(&dl
->disk
))
4101 if (i
>= mpb
->num_disks
) {
4102 struct intel_disk
*idisk
;
4104 idisk
= disk_list_get(dl
->serial
, disk_list
);
4105 if (idisk
&& is_spare(&idisk
->disk
) &&
4106 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
4114 dl
->next
= champion
->disks
;
4115 champion
->disks
= dl
;
4119 /* delete 'champion' from super_list */
4120 for (del
= super_list
; *del
; ) {
4121 if (*del
== champion
) {
4122 *del
= (*del
)->next
;
4125 del
= &(*del
)->next
;
4127 champion
->next
= NULL
;
4131 struct intel_disk
*idisk
= disk_list
;
4133 disk_list
= disk_list
->next
;
4142 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
);
4143 static int get_super_block(struct intel_super
**super_list
, int devnum
, char *devname
,
4144 int major
, int minor
, int keep_fd
);
4146 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4147 int *max
, int keep_fd
);
4150 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
4151 char *devname
, struct md_list
*devlist
,
4154 struct intel_super
*super_list
= NULL
;
4155 struct intel_super
*super
= NULL
;
4160 /* 'fd' is an opened container */
4161 err
= get_sra_super_block(fd
, &super_list
, devname
, &i
, keep_fd
);
4163 /* get super block from devlist devices */
4164 err
= get_devlist_super_block(devlist
, &super_list
, &i
, keep_fd
);
4167 /* all mpbs enter, maybe one leaves */
4168 super
= imsm_thunderdome(&super_list
, i
);
4174 if (find_missing(super
) != 0) {
4180 /* load migration record */
4181 err
= load_imsm_migr_rec(super
, NULL
);
4183 /* migration is in progress,
4184 * but migr_rec cannot be loaded,
4190 /* Check migration compatibility */
4191 if ((err
== 0) && (check_mpb_migr_compatibility(super
) != 0)) {
4192 fprintf(stderr
, Name
": Unsupported migration detected");
4194 fprintf(stderr
, " on %s\n", devname
);
4196 fprintf(stderr
, " (IMSM).\n");
4205 while (super_list
) {
4206 struct intel_super
*s
= super_list
;
4208 super_list
= super_list
->next
;
4218 st
->container_dev
= fd2devnum(fd
);
4220 st
->container_dev
= NoMdDev
;
4221 if (err
== 0 && st
->ss
== NULL
) {
4222 st
->ss
= &super_imsm
;
4223 st
->minor_version
= 0;
4224 st
->max_devs
= IMSM_MAX_DEVICES
;
4231 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4232 int *max
, int keep_fd
)
4234 struct md_list
*tmpdev
;
4238 for (i
= 0, tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
4239 if (tmpdev
->used
!= 1)
4241 if (tmpdev
->container
== 1) {
4243 int fd
= dev_open(tmpdev
->devname
, O_RDONLY
|O_EXCL
);
4245 fprintf(stderr
, Name
": cannot open device %s: %s\n",
4246 tmpdev
->devname
, strerror(errno
));
4250 err
= get_sra_super_block(fd
, super_list
,
4251 tmpdev
->devname
, &lmax
,
4260 int major
= major(tmpdev
->st_rdev
);
4261 int minor
= minor(tmpdev
->st_rdev
);
4262 err
= get_super_block(super_list
,
4279 static int get_super_block(struct intel_super
**super_list
, int devnum
, char *devname
,
4280 int major
, int minor
, int keep_fd
)
4282 struct intel_super
*s
= NULL
;
4295 sprintf(nm
, "%d:%d", major
, minor
);
4296 dfd
= dev_open(nm
, O_RDWR
);
4302 rv
= find_intel_hba_capability(dfd
, s
, devname
);
4303 /* no orom/efi or non-intel hba of the disk */
4309 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4311 /* retry the load if we might have raced against mdmon */
4312 if (err
== 3 && (devnum
!= -1) && mdmon_running(devnum
))
4313 for (retry
= 0; retry
< 3; retry
++) {
4315 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4321 s
->next
= *super_list
;
4329 if ((dfd
>= 0) && (!keep_fd
))
4336 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
)
4343 sra
= sysfs_read(fd
, 0, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
4347 if (sra
->array
.major_version
!= -1 ||
4348 sra
->array
.minor_version
!= -2 ||
4349 strcmp(sra
->text_version
, "imsm") != 0) {
4354 devnum
= fd2devnum(fd
);
4355 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
4356 if (get_super_block(super_list
, devnum
, devname
,
4357 sd
->disk
.major
, sd
->disk
.minor
, keep_fd
) != 0) {
4368 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
4370 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
, NULL
, 1);
4374 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
4376 struct intel_super
*super
;
4379 if (test_partition(fd
))
4380 /* IMSM not allowed on partitions */
4383 free_super_imsm(st
);
4385 super
= alloc_super();
4388 Name
": malloc of %zu failed.\n",
4392 /* Load hba and capabilities if they exist.
4393 * But do not preclude loading metadata in case capabilities or hba are
4394 * non-compliant and ignore_hw_compat is set.
4396 rv
= find_intel_hba_capability(fd
, super
, devname
);
4397 /* no orom/efi or non-intel hba of the disk */
4398 if ((rv
!= 0) && (st
->ignore_hw_compat
== 0)) {
4401 Name
": No OROM/EFI properties for %s\n", devname
);
4405 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
4410 Name
": Failed to load all information "
4411 "sections on %s\n", devname
);
4417 if (st
->ss
== NULL
) {
4418 st
->ss
= &super_imsm
;
4419 st
->minor_version
= 0;
4420 st
->max_devs
= IMSM_MAX_DEVICES
;
4423 /* load migration record */
4424 if (load_imsm_migr_rec(super
, NULL
) == 0) {
4425 /* Check for unsupported migration features */
4426 if (check_mpb_migr_compatibility(super
) != 0) {
4428 Name
": Unsupported migration detected");
4430 fprintf(stderr
, " on %s\n", devname
);
4432 fprintf(stderr
, " (IMSM).\n");
4440 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
4442 if (info
->level
== 1)
4444 return info
->chunk_size
>> 9;
4447 static unsigned long long info_to_blocks_per_member(mdu_array_info_t
*info
,
4448 unsigned long long size
)
4450 if (info
->level
== 1)
4453 return (size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
4456 static void imsm_update_version_info(struct intel_super
*super
)
4458 /* update the version and attributes */
4459 struct imsm_super
*mpb
= super
->anchor
;
4461 struct imsm_dev
*dev
;
4462 struct imsm_map
*map
;
4465 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4466 dev
= get_imsm_dev(super
, i
);
4467 map
= get_imsm_map(dev
, MAP_0
);
4468 if (__le32_to_cpu(dev
->size_high
) > 0)
4469 mpb
->attributes
|= MPB_ATTRIB_2TB
;
4471 /* FIXME detect when an array spans a port multiplier */
4473 mpb
->attributes
|= MPB_ATTRIB_PM
;
4476 if (mpb
->num_raid_devs
> 1 ||
4477 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
4478 version
= MPB_VERSION_ATTRIBS
;
4479 switch (get_imsm_raid_level(map
)) {
4480 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
4481 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
4482 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
4483 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
4486 if (map
->num_members
>= 5)
4487 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
4488 else if (dev
->status
== DEV_CLONE_N_GO
)
4489 version
= MPB_VERSION_CNG
;
4490 else if (get_imsm_raid_level(map
) == 5)
4491 version
= MPB_VERSION_RAID5
;
4492 else if (map
->num_members
>= 3)
4493 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
4494 else if (get_imsm_raid_level(map
) == 1)
4495 version
= MPB_VERSION_RAID1
;
4497 version
= MPB_VERSION_RAID0
;
4499 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
4503 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
4505 struct imsm_super
*mpb
= super
->anchor
;
4506 char *reason
= NULL
;
4509 if (strlen(name
) > MAX_RAID_SERIAL_LEN
)
4510 reason
= "must be 16 characters or less";
4512 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4513 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
4515 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
4516 reason
= "already exists";
4521 if (reason
&& !quiet
)
4522 fprintf(stderr
, Name
": imsm volume name %s\n", reason
);
4527 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
4528 unsigned long long size
, char *name
,
4529 char *homehost
, int *uuid
)
4531 /* We are creating a volume inside a pre-existing container.
4532 * so st->sb is already set.
4534 struct intel_super
*super
= st
->sb
;
4535 struct imsm_super
*mpb
= super
->anchor
;
4536 struct intel_dev
*dv
;
4537 struct imsm_dev
*dev
;
4538 struct imsm_vol
*vol
;
4539 struct imsm_map
*map
;
4540 int idx
= mpb
->num_raid_devs
;
4542 unsigned long long array_blocks
;
4543 size_t size_old
, size_new
;
4544 unsigned long long num_data_stripes
;
4546 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
4547 fprintf(stderr
, Name
": This imsm-container already has the "
4548 "maximum of %d volumes\n", super
->orom
->vpa
);
4552 /* ensure the mpb is large enough for the new data */
4553 size_old
= __le32_to_cpu(mpb
->mpb_size
);
4554 size_new
= disks_to_mpb_size(info
->nr_disks
);
4555 if (size_new
> size_old
) {
4557 size_t size_round
= ROUND_UP(size_new
, 512);
4559 if (posix_memalign(&mpb_new
, 512, size_round
) != 0) {
4560 fprintf(stderr
, Name
": could not allocate new mpb\n");
4563 if (posix_memalign(&super
->migr_rec_buf
, 512,
4564 MIGR_REC_BUF_SIZE
) != 0) {
4565 fprintf(stderr
, Name
4566 ": %s could not allocate migr_rec buffer\n",
4573 memcpy(mpb_new
, mpb
, size_old
);
4576 super
->anchor
= mpb_new
;
4577 mpb
->mpb_size
= __cpu_to_le32(size_new
);
4578 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
4580 super
->current_vol
= idx
;
4582 /* handle 'failed_disks' by either:
4583 * a) create dummy disk entries in the table if this the first
4584 * volume in the array. We add them here as this is the only
4585 * opportunity to add them. add_to_super_imsm_volume()
4586 * handles the non-failed disks and continues incrementing
4588 * b) validate that 'failed_disks' matches the current number
4589 * of missing disks if the container is populated
4591 if (super
->current_vol
== 0) {
4593 for (i
= 0; i
< info
->failed_disks
; i
++) {
4594 struct imsm_disk
*disk
;
4597 disk
= __get_imsm_disk(mpb
, i
);
4598 disk
->status
= CONFIGURED_DISK
| FAILED_DISK
;
4599 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
4600 snprintf((char *) disk
->serial
, MAX_RAID_SERIAL_LEN
,
4603 find_missing(super
);
4608 for (d
= super
->missing
; d
; d
= d
->next
)
4610 if (info
->failed_disks
> missing
) {
4611 fprintf(stderr
, Name
": unable to add 'missing' disk to container\n");
4616 if (!check_name(super
, name
, 0))
4618 dv
= malloc(sizeof(*dv
));
4620 fprintf(stderr
, Name
": failed to allocate device list entry\n");
4623 dev
= calloc(1, sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
4626 fprintf(stderr
, Name
": could not allocate raid device\n");
4630 strncpy((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
4631 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
4632 info
->layout
, info
->chunk_size
,
4634 /* round array size down to closest MB */
4635 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
4637 dev
->size_low
= __cpu_to_le32((__u32
) array_blocks
);
4638 dev
->size_high
= __cpu_to_le32((__u32
) (array_blocks
>> 32));
4639 dev
->status
= (DEV_READ_COALESCING
| DEV_WRITE_COALESCING
);
4641 vol
->migr_state
= 0;
4642 set_migr_type(dev
, MIGR_INIT
);
4643 vol
->dirty
= !info
->state
;
4644 vol
->curr_migr_unit
= 0;
4645 map
= get_imsm_map(dev
, MAP_0
);
4646 set_pba_of_lba0(map
, super
->create_offset
);
4647 set_blocks_per_member(map
, info_to_blocks_per_member(info
, size
));
4648 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
4649 map
->failed_disk_num
= ~0;
4650 if (info
->level
> 0)
4651 map
->map_state
= IMSM_T_STATE_UNINITIALIZED
;
4653 map
->map_state
= info
->failed_disks
? IMSM_T_STATE_FAILED
:
4654 IMSM_T_STATE_NORMAL
;
4657 if (info
->level
== 1 && info
->raid_disks
> 2) {
4660 fprintf(stderr
, Name
": imsm does not support more than 2 disks"
4661 "in a raid1 volume\n");
4665 map
->raid_level
= info
->level
;
4666 if (info
->level
== 10) {
4667 map
->raid_level
= 1;
4668 map
->num_domains
= info
->raid_disks
/ 2;
4669 } else if (info
->level
== 1)
4670 map
->num_domains
= info
->raid_disks
;
4672 map
->num_domains
= 1;
4674 /* info->size is only int so use the 'size' parameter instead */
4675 num_data_stripes
= (size
* 2) / info_to_blocks_per_strip(info
);
4676 num_data_stripes
/= map
->num_domains
;
4677 set_num_data_stripes(map
, num_data_stripes
);
4679 map
->num_members
= info
->raid_disks
;
4680 for (i
= 0; i
< map
->num_members
; i
++) {
4681 /* initialized in add_to_super */
4682 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
4684 mpb
->num_raid_devs
++;
4687 dv
->index
= super
->current_vol
;
4688 dv
->next
= super
->devlist
;
4689 super
->devlist
= dv
;
4691 imsm_update_version_info(super
);
4696 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
4697 unsigned long long size
, char *name
,
4698 char *homehost
, int *uuid
)
4700 /* This is primarily called by Create when creating a new array.
4701 * We will then get add_to_super called for each component, and then
4702 * write_init_super called to write it out to each device.
4703 * For IMSM, Create can create on fresh devices or on a pre-existing
4705 * To create on a pre-existing array a different method will be called.
4706 * This one is just for fresh drives.
4708 struct intel_super
*super
;
4709 struct imsm_super
*mpb
;
4714 return init_super_imsm_volume(st
, info
, size
, name
, homehost
, uuid
);
4717 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
4721 super
= alloc_super();
4722 if (super
&& posix_memalign(&super
->buf
, 512, mpb_size
) != 0) {
4727 fprintf(stderr
, Name
4728 ": %s could not allocate superblock\n", __func__
);
4731 if (posix_memalign(&super
->migr_rec_buf
, 512, MIGR_REC_BUF_SIZE
) != 0) {
4732 fprintf(stderr
, Name
4733 ": %s could not allocate migr_rec buffer\n", __func__
);
4738 memset(super
->buf
, 0, mpb_size
);
4740 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
4744 /* zeroing superblock */
4748 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
4750 version
= (char *) mpb
->sig
;
4751 strcpy(version
, MPB_SIGNATURE
);
4752 version
+= strlen(MPB_SIGNATURE
);
4753 strcpy(version
, MPB_VERSION_RAID0
);
4759 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
4760 int fd
, char *devname
)
4762 struct intel_super
*super
= st
->sb
;
4763 struct imsm_super
*mpb
= super
->anchor
;
4764 struct imsm_disk
*_disk
;
4765 struct imsm_dev
*dev
;
4766 struct imsm_map
*map
;
4770 dev
= get_imsm_dev(super
, super
->current_vol
);
4771 map
= get_imsm_map(dev
, MAP_0
);
4773 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
4774 fprintf(stderr
, Name
": %s: Cannot add spare devices to IMSM volume\n",
4780 /* we're doing autolayout so grab the pre-marked (in
4781 * validate_geometry) raid_disk
4783 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4784 if (dl
->raiddisk
== dk
->raid_disk
)
4787 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4788 if (dl
->major
== dk
->major
&&
4789 dl
->minor
== dk
->minor
)
4794 fprintf(stderr
, Name
": %s is not a member of the same container\n", devname
);
4798 /* add a pristine spare to the metadata */
4799 if (dl
->index
< 0) {
4800 dl
->index
= super
->anchor
->num_disks
;
4801 super
->anchor
->num_disks
++;
4803 /* Check the device has not already been added */
4804 slot
= get_imsm_disk_slot(map
, dl
->index
);
4806 (get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
) & IMSM_ORD_REBUILD
) == 0) {
4807 fprintf(stderr
, Name
": %s has been included in this array twice\n",
4811 set_imsm_ord_tbl_ent(map
, dk
->raid_disk
, dl
->index
);
4812 dl
->disk
.status
= CONFIGURED_DISK
;
4814 /* update size of 'missing' disks to be at least as large as the
4815 * largest acitve member (we only have dummy missing disks when
4816 * creating the first volume)
4818 if (super
->current_vol
== 0) {
4819 for (df
= super
->missing
; df
; df
= df
->next
) {
4820 if (total_blocks(&dl
->disk
) > total_blocks(&df
->disk
))
4821 set_total_blocks(&df
->disk
, total_blocks(&dl
->disk
));
4822 _disk
= __get_imsm_disk(mpb
, df
->index
);
4827 /* refresh unset/failed slots to point to valid 'missing' entries */
4828 for (df
= super
->missing
; df
; df
= df
->next
)
4829 for (slot
= 0; slot
< mpb
->num_disks
; slot
++) {
4830 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
4832 if ((ord
& IMSM_ORD_REBUILD
) == 0)
4834 set_imsm_ord_tbl_ent(map
, slot
, df
->index
| IMSM_ORD_REBUILD
);
4835 if (is_gen_migration(dev
)) {
4836 struct imsm_map
*map2
= get_imsm_map(dev
,
4838 int slot2
= get_imsm_disk_slot(map2
, df
->index
);
4839 if ((slot2
< map2
->num_members
) &&
4841 __u32 ord2
= get_imsm_ord_tbl_ent(dev
,
4844 if ((unsigned)df
->index
==
4846 set_imsm_ord_tbl_ent(map2
,
4852 dprintf("set slot:%d to missing disk:%d\n", slot
, df
->index
);
4856 /* if we are creating the first raid device update the family number */
4857 if (super
->current_vol
== 0) {
4859 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
4861 _disk
= __get_imsm_disk(mpb
, dl
->index
);
4862 if (!_dev
|| !_disk
) {
4863 fprintf(stderr
, Name
": BUG mpb setup error\n");
4869 sum
+= __gen_imsm_checksum(mpb
);
4870 mpb
->family_num
= __cpu_to_le32(sum
);
4871 mpb
->orig_family_num
= mpb
->family_num
;
4873 super
->current_disk
= dl
;
4878 * Function marks disk as spare and restores disk serial
4879 * in case it was previously marked as failed by takeover operation
4881 * -1 : critical error
4882 * 0 : disk is marked as spare but serial is not set
4885 int mark_spare(struct dl
*disk
)
4887 __u8 serial
[MAX_RAID_SERIAL_LEN
];
4894 if (!imsm_read_serial(disk
->fd
, NULL
, serial
)) {
4895 /* Restore disk serial number, because takeover marks disk
4896 * as failed and adds to serial ':0' before it becomes
4899 serialcpy(disk
->serial
, serial
);
4900 serialcpy(disk
->disk
.serial
, serial
);
4903 disk
->disk
.status
= SPARE_DISK
;
4909 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
4910 int fd
, char *devname
)
4912 struct intel_super
*super
= st
->sb
;
4914 unsigned long long size
;
4919 /* If we are on an RAID enabled platform check that the disk is
4920 * attached to the raid controller.
4921 * We do not need to test disks attachment for container based additions,
4922 * they shall be already tested when container was created/assembled.
4924 rv
= find_intel_hba_capability(fd
, super
, devname
);
4925 /* no orom/efi or non-intel hba of the disk */
4927 dprintf("capability: %p fd: %d ret: %d\n",
4928 super
->orom
, fd
, rv
);
4932 if (super
->current_vol
>= 0)
4933 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
4936 dd
= malloc(sizeof(*dd
));
4939 Name
": malloc failed %s:%d.\n", __func__
, __LINE__
);
4942 memset(dd
, 0, sizeof(*dd
));
4943 dd
->major
= major(stb
.st_rdev
);
4944 dd
->minor
= minor(stb
.st_rdev
);
4945 dd
->devname
= devname
? strdup(devname
) : NULL
;
4948 dd
->action
= DISK_ADD
;
4949 rv
= imsm_read_serial(fd
, devname
, dd
->serial
);
4952 Name
": failed to retrieve scsi serial, aborting\n");
4957 get_dev_size(fd
, NULL
, &size
);
4959 serialcpy(dd
->disk
.serial
, dd
->serial
);
4960 set_total_blocks(&dd
->disk
, size
);
4961 if (__le32_to_cpu(dd
->disk
.total_blocks_hi
) > 0) {
4962 struct imsm_super
*mpb
= super
->anchor
;
4963 mpb
->attributes
|= MPB_ATTRIB_2TB_DISK
;
4966 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
4967 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
4969 dd
->disk
.scsi_id
= __cpu_to_le32(0);
4971 if (st
->update_tail
) {
4972 dd
->next
= super
->disk_mgmt_list
;
4973 super
->disk_mgmt_list
= dd
;
4975 dd
->next
= super
->disks
;
4977 super
->updates_pending
++;
4984 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
4986 struct intel_super
*super
= st
->sb
;
4989 /* remove from super works only in mdmon - for communication
4990 * manager - monitor. Check if communication memory buffer
4993 if (!st
->update_tail
) {
4995 Name
": %s shall be used in mdmon context only"
4996 "(line %d).\n", __func__
, __LINE__
);
4999 dd
= malloc(sizeof(*dd
));
5002 Name
": malloc failed %s:%d.\n", __func__
, __LINE__
);
5005 memset(dd
, 0, sizeof(*dd
));
5006 dd
->major
= dk
->major
;
5007 dd
->minor
= dk
->minor
;
5010 dd
->action
= DISK_REMOVE
;
5012 dd
->next
= super
->disk_mgmt_list
;
5013 super
->disk_mgmt_list
= dd
;
5019 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
5023 struct imsm_super anchor
;
5024 } spare_record
__attribute__ ((aligned(512)));
5026 /* spare records have their own family number and do not have any defined raid
5029 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
5031 struct imsm_super
*mpb
= super
->anchor
;
5032 struct imsm_super
*spare
= &spare_record
.anchor
;
5036 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
)),
5037 spare
->generation_num
= __cpu_to_le32(1UL),
5038 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
5039 spare
->num_disks
= 1,
5040 spare
->num_raid_devs
= 0,
5041 spare
->cache_size
= mpb
->cache_size
,
5042 spare
->pwr_cycle_count
= __cpu_to_le32(1),
5044 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
5045 MPB_SIGNATURE MPB_VERSION_RAID0
);
5047 for (d
= super
->disks
; d
; d
= d
->next
) {
5051 spare
->disk
[0] = d
->disk
;
5052 if (__le32_to_cpu(d
->disk
.total_blocks_hi
) > 0)
5053 spare
->attributes
|= MPB_ATTRIB_2TB_DISK
;
5055 sum
= __gen_imsm_checksum(spare
);
5056 spare
->family_num
= __cpu_to_le32(sum
);
5057 spare
->orig_family_num
= 0;
5058 sum
= __gen_imsm_checksum(spare
);
5059 spare
->check_sum
= __cpu_to_le32(sum
);
5061 if (store_imsm_mpb(d
->fd
, spare
)) {
5062 fprintf(stderr
, "%s: failed for device %d:%d %s\n",
5063 __func__
, d
->major
, d
->minor
, strerror(errno
));
5075 static int write_super_imsm(struct supertype
*st
, int doclose
)
5077 struct intel_super
*super
= st
->sb
;
5078 struct imsm_super
*mpb
= super
->anchor
;
5084 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
5086 int clear_migration_record
= 1;
5088 /* 'generation' is incremented everytime the metadata is written */
5089 generation
= __le32_to_cpu(mpb
->generation_num
);
5091 mpb
->generation_num
= __cpu_to_le32(generation
);
5093 /* fix up cases where previous mdadm releases failed to set
5096 if (mpb
->orig_family_num
== 0)
5097 mpb
->orig_family_num
= mpb
->family_num
;
5099 for (d
= super
->disks
; d
; d
= d
->next
) {
5103 mpb
->disk
[d
->index
] = d
->disk
;
5107 for (d
= super
->missing
; d
; d
= d
->next
) {
5108 mpb
->disk
[d
->index
] = d
->disk
;
5111 mpb
->num_disks
= num_disks
;
5112 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
5114 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5115 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
5116 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
5118 imsm_copy_dev(dev
, dev2
);
5119 mpb_size
+= sizeof_imsm_dev(dev
, 0);
5121 if (is_gen_migration(dev2
))
5122 clear_migration_record
= 0;
5124 mpb_size
+= __le32_to_cpu(mpb
->bbm_log_size
);
5125 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5127 /* recalculate checksum */
5128 sum
= __gen_imsm_checksum(mpb
);
5129 mpb
->check_sum
= __cpu_to_le32(sum
);
5131 if (super
->clean_migration_record_by_mdmon
) {
5132 clear_migration_record
= 1;
5133 super
->clean_migration_record_by_mdmon
= 0;
5135 if (clear_migration_record
)
5136 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SIZE
);
5138 /* write the mpb for disks that compose raid devices */
5139 for (d
= super
->disks
; d
; d
= d
->next
) {
5140 if (d
->index
< 0 || is_failed(&d
->disk
))
5143 if (clear_migration_record
) {
5144 unsigned long long dsize
;
5146 get_dev_size(d
->fd
, NULL
, &dsize
);
5147 if (lseek64(d
->fd
, dsize
- 512, SEEK_SET
) >= 0) {
5148 if (write(d
->fd
, super
->migr_rec_buf
,
5149 MIGR_REC_BUF_SIZE
) != MIGR_REC_BUF_SIZE
)
5150 perror("Write migr_rec failed");
5154 if (store_imsm_mpb(d
->fd
, mpb
))
5156 "%s: failed for device %d:%d (fd: %d)%s\n",
5157 __func__
, d
->major
, d
->minor
,
5158 d
->fd
, strerror(errno
));
5167 return write_super_imsm_spares(super
, doclose
);
5173 static int create_array(struct supertype
*st
, int dev_idx
)
5176 struct imsm_update_create_array
*u
;
5177 struct intel_super
*super
= st
->sb
;
5178 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
5179 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
5180 struct disk_info
*inf
;
5181 struct imsm_disk
*disk
;
5184 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
5185 sizeof(*inf
) * map
->num_members
;
5188 fprintf(stderr
, "%s: failed to allocate update buffer\n",
5193 u
->type
= update_create_array
;
5194 u
->dev_idx
= dev_idx
;
5195 imsm_copy_dev(&u
->dev
, dev
);
5196 inf
= get_disk_info(u
);
5197 for (i
= 0; i
< map
->num_members
; i
++) {
5198 int idx
= get_imsm_disk_idx(dev
, i
, MAP_X
);
5200 disk
= get_imsm_disk(super
, idx
);
5201 serialcpy(inf
[i
].serial
, disk
->serial
);
5203 append_metadata_update(st
, u
, len
);
5208 static int mgmt_disk(struct supertype
*st
)
5210 struct intel_super
*super
= st
->sb
;
5212 struct imsm_update_add_remove_disk
*u
;
5214 if (!super
->disk_mgmt_list
)
5220 fprintf(stderr
, "%s: failed to allocate update buffer\n",
5225 u
->type
= update_add_remove_disk
;
5226 append_metadata_update(st
, u
, len
);
5231 static int write_init_super_imsm(struct supertype
*st
)
5233 struct intel_super
*super
= st
->sb
;
5234 int current_vol
= super
->current_vol
;
5236 /* we are done with current_vol reset it to point st at the container */
5237 super
->current_vol
= -1;
5239 if (st
->update_tail
) {
5240 /* queue the recently created array / added disk
5241 * as a metadata update */
5244 /* determine if we are creating a volume or adding a disk */
5245 if (current_vol
< 0) {
5246 /* in the mgmt (add/remove) disk case we are running
5247 * in mdmon context, so don't close fd's
5249 return mgmt_disk(st
);
5251 rv
= create_array(st
, current_vol
);
5256 for (d
= super
->disks
; d
; d
= d
->next
)
5257 Kill(d
->devname
, NULL
, 0, 1, 1);
5258 return write_super_imsm(st
, 1);
5263 static int store_super_imsm(struct supertype
*st
, int fd
)
5265 struct intel_super
*super
= st
->sb
;
5266 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
5272 return store_imsm_mpb(fd
, mpb
);
5278 static int imsm_bbm_log_size(struct imsm_super
*mpb
)
5280 return __le32_to_cpu(mpb
->bbm_log_size
);
5284 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
5285 int layout
, int raiddisks
, int chunk
,
5286 unsigned long long size
, char *dev
,
5287 unsigned long long *freesize
,
5291 unsigned long long ldsize
;
5292 struct intel_super
*super
=NULL
;
5295 if (level
!= LEVEL_CONTAINER
)
5300 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
5303 fprintf(stderr
, Name
": imsm: Cannot open %s: %s\n",
5304 dev
, strerror(errno
));
5307 if (!get_dev_size(fd
, dev
, &ldsize
)) {
5312 /* capabilities retrieve could be possible
5313 * note that there is no fd for the disks in array.
5315 super
= alloc_super();
5318 Name
": malloc of %zu failed.\n",
5324 rv
= find_intel_hba_capability(fd
, super
, verbose
? dev
: NULL
);
5328 fd2devname(fd
, str
);
5329 dprintf("validate_geometry_imsm_container: fd: %d %s orom: %p rv: %d raiddisk: %d\n",
5330 fd
, str
, super
->orom
, rv
, raiddisks
);
5332 /* no orom/efi or non-intel hba of the disk */
5339 if (raiddisks
> super
->orom
->tds
) {
5341 fprintf(stderr
, Name
": %d exceeds maximum number of"
5342 " platform supported disks: %d\n",
5343 raiddisks
, super
->orom
->tds
);
5347 if ((super
->orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) == 0 &&
5348 (ldsize
>> 9) >> 32 > 0) {
5350 fprintf(stderr
, Name
": %s exceeds maximum platform supported size\n", dev
);
5356 *freesize
= avail_size_imsm(st
, ldsize
>> 9);
5362 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
5364 const unsigned long long base_start
= e
[*idx
].start
;
5365 unsigned long long end
= base_start
+ e
[*idx
].size
;
5368 if (base_start
== end
)
5372 for (i
= *idx
; i
< num_extents
; i
++) {
5373 /* extend overlapping extents */
5374 if (e
[i
].start
>= base_start
&&
5375 e
[i
].start
<= end
) {
5378 if (e
[i
].start
+ e
[i
].size
> end
)
5379 end
= e
[i
].start
+ e
[i
].size
;
5380 } else if (e
[i
].start
> end
) {
5386 return end
- base_start
;
5389 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
5391 /* build a composite disk with all known extents and generate a new
5392 * 'maxsize' given the "all disks in an array must share a common start
5393 * offset" constraint
5395 struct extent
*e
= calloc(sum_extents
, sizeof(*e
));
5399 unsigned long long pos
;
5400 unsigned long long start
= 0;
5401 unsigned long long maxsize
;
5402 unsigned long reserve
;
5407 /* coalesce and sort all extents. also, check to see if we need to
5408 * reserve space between member arrays
5411 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5414 for (i
= 0; i
< dl
->extent_cnt
; i
++)
5417 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
5422 while (i
< sum_extents
) {
5423 e
[j
].start
= e
[i
].start
;
5424 e
[j
].size
= find_size(e
, &i
, sum_extents
);
5426 if (e
[j
-1].size
== 0)
5435 unsigned long long esize
;
5437 esize
= e
[i
].start
- pos
;
5438 if (esize
>= maxsize
) {
5443 pos
= e
[i
].start
+ e
[i
].size
;
5445 } while (e
[i
-1].size
);
5451 /* FIXME assumes volume at offset 0 is the first volume in a
5454 if (start_extent
> 0)
5455 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
5459 if (maxsize
< reserve
)
5462 super
->create_offset
= ~((unsigned long long) 0);
5463 if (start
+ reserve
> super
->create_offset
)
5464 return 0; /* start overflows create_offset */
5465 super
->create_offset
= start
+ reserve
;
5467 return maxsize
- reserve
;
5470 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
5472 if (level
< 0 || level
== 6 || level
== 4)
5475 /* if we have an orom prevent invalid raid levels */
5478 case 0: return imsm_orom_has_raid0(orom
);
5481 return imsm_orom_has_raid1e(orom
);
5482 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
5483 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
5484 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
5487 return 1; /* not on an Intel RAID platform so anything goes */
5494 active_arrays_by_format(char *name
, char* hba
, struct md_list
**devlist
,
5495 int dpa
, int verbose
)
5497 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
5498 struct mdstat_ent
*memb
= NULL
;
5501 struct md_list
*dv
= NULL
;
5504 for (memb
= mdstat
; memb
; memb
= memb
->next
) {
5505 if (memb
->metadata_version
&&
5506 (strncmp(memb
->metadata_version
, "external:", 9) == 0) &&
5507 (strcmp(&memb
->metadata_version
[9], name
) == 0) &&
5508 !is_subarray(memb
->metadata_version
+9) &&
5510 struct dev_member
*dev
= memb
->members
;
5512 while(dev
&& (fd
< 0)) {
5513 char *path
= malloc(strlen(dev
->name
) + strlen("/dev/") + 1);
5515 num
= sprintf(path
, "%s%s", "/dev/", dev
->name
);
5517 fd
= open(path
, O_RDONLY
, 0);
5518 if ((num
<= 0) || (fd
< 0)) {
5519 pr_vrb(": Cannot open %s: %s\n",
5520 dev
->name
, strerror(errno
));
5527 if ((fd
>= 0) && disk_attached_to_hba(fd
, hba
)) {
5528 struct mdstat_ent
*vol
;
5529 for (vol
= mdstat
; vol
; vol
= vol
->next
) {
5530 if ((vol
->active
> 0) &&
5531 vol
->metadata_version
&&
5532 is_container_member(vol
, memb
->dev
)) {
5537 if (*devlist
&& (found
< dpa
)) {
5538 dv
= calloc(1, sizeof(*dv
));
5540 fprintf(stderr
, Name
": calloc failed\n");
5542 dv
->devname
= malloc(strlen(memb
->dev
) + strlen("/dev/") + 1);
5543 if (dv
->devname
!= NULL
) {
5544 sprintf(dv
->devname
, "%s%s", "/dev/", memb
->dev
);
5547 dv
->next
= *devlist
;
5558 free_mdstat(mdstat
);
5563 static struct md_list
*
5564 get_loop_devices(void)
5567 struct md_list
*devlist
= NULL
;
5568 struct md_list
*dv
= NULL
;
5570 for(i
= 0; i
< 12; i
++) {
5571 dv
= calloc(1, sizeof(*dv
));
5573 fprintf(stderr
, Name
": calloc failed\n");
5576 dv
->devname
= malloc(40);
5577 if (dv
->devname
== NULL
) {
5578 fprintf(stderr
, Name
": malloc failed\n");
5582 sprintf(dv
->devname
, "/dev/loop%d", i
);
5590 static struct md_list
*
5591 get_devices(const char *hba_path
)
5593 struct md_list
*devlist
= NULL
;
5594 struct md_list
*dv
= NULL
;
5600 devlist
= get_loop_devices();
5603 /* scroll through /sys/dev/block looking for devices attached to
5606 dir
= opendir("/sys/dev/block");
5607 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
5612 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
5614 path
= devt_to_devpath(makedev(major
, minor
));
5617 if (!path_attached_to_hba(path
, hba_path
)) {
5624 fd
= dev_open(ent
->d_name
, O_RDONLY
);
5626 fd2devname(fd
, buf
);
5629 fprintf(stderr
, Name
": cannot open device: %s\n",
5635 dv
= calloc(1, sizeof(*dv
));
5637 fprintf(stderr
, Name
": malloc failed\n");
5641 dv
->devname
= strdup(buf
);
5642 if (dv
->devname
== NULL
) {
5643 fprintf(stderr
, Name
": malloc failed\n");
5654 devlist
= devlist
->next
;
5663 count_volumes_list(struct md_list
*devlist
, char *homehost
,
5664 int verbose
, int *found
)
5666 struct md_list
*tmpdev
;
5668 struct supertype
*st
= NULL
;
5670 /* first walk the list of devices to find a consistent set
5671 * that match the criterea, if that is possible.
5672 * We flag the ones we like with 'used'.
5675 st
= match_metadata_desc_imsm("imsm");
5677 pr_vrb(": cannot allocate memory for imsm supertype\n");
5681 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5682 char *devname
= tmpdev
->devname
;
5684 struct supertype
*tst
;
5686 if (tmpdev
->used
> 1)
5688 tst
= dup_super(st
);
5690 pr_vrb(": cannot allocate memory for imsm supertype\n");
5693 tmpdev
->container
= 0;
5694 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
5696 dprintf(": cannot open device %s: %s\n",
5697 devname
, strerror(errno
));
5699 } else if (fstat(dfd
, &stb
)< 0) {
5701 dprintf(": fstat failed for %s: %s\n",
5702 devname
, strerror(errno
));
5704 } else if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
5705 dprintf(": %s is not a block device.\n",
5708 } else if (must_be_container(dfd
)) {
5709 struct supertype
*cst
;
5710 cst
= super_by_fd(dfd
, NULL
);
5712 dprintf(": cannot recognize container type %s\n",
5715 } else if (tst
->ss
!= st
->ss
) {
5716 dprintf(": non-imsm container - ignore it: %s\n",
5719 } else if (!tst
->ss
->load_container
||
5720 tst
->ss
->load_container(tst
, dfd
, NULL
))
5723 tmpdev
->container
= 1;
5726 cst
->ss
->free_super(cst
);
5728 tmpdev
->st_rdev
= stb
.st_rdev
;
5729 if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
5730 dprintf(": no RAID superblock on %s\n",
5733 } else if (tst
->ss
->compare_super
== NULL
) {
5734 dprintf(": Cannot assemble %s metadata on %s\n",
5735 tst
->ss
->name
, devname
);
5741 if (tmpdev
->used
== 2 || tmpdev
->used
== 4) {
5742 /* Ignore unrecognised devices during auto-assembly */
5747 tst
->ss
->getinfo_super(tst
, &info
, NULL
);
5749 if (st
->minor_version
== -1)
5750 st
->minor_version
= tst
->minor_version
;
5752 if (memcmp(info
.uuid
, uuid_zero
,
5753 sizeof(int[4])) == 0) {
5754 /* this is a floating spare. It cannot define
5755 * an array unless there are no more arrays of
5756 * this type to be found. It can be included
5757 * in an array of this type though.
5763 if (st
->ss
!= tst
->ss
||
5764 st
->minor_version
!= tst
->minor_version
||
5765 st
->ss
->compare_super(st
, tst
) != 0) {
5766 /* Some mismatch. If exactly one array matches this host,
5767 * we can resolve on that one.
5768 * Or, if we are auto assembling, we just ignore the second
5771 dprintf(": superblock on %s doesn't match others - assembly aborted\n",
5777 dprintf("found: devname: %s\n", devname
);
5781 tst
->ss
->free_super(tst
);
5785 if ((err
= load_super_imsm_all(st
, -1, &st
->sb
, NULL
, devlist
, 0)) == 0) {
5786 struct mdinfo
*iter
, *head
= st
->ss
->container_content(st
, NULL
);
5787 for (iter
= head
; iter
; iter
= iter
->next
) {
5788 dprintf("content->text_version: %s vol\n",
5789 iter
->text_version
);
5790 if (iter
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
5791 /* do not assemble arrays with unsupported
5793 dprintf(": Cannot activate member %s.\n",
5794 iter
->text_version
);
5801 dprintf(" no valid super block on device list: err: %d %p\n",
5805 dprintf(" no more devices to examin\n");
5808 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5809 if ((tmpdev
->used
== 1) && (tmpdev
->found
)) {
5811 if (count
< tmpdev
->found
)
5814 count
-= tmpdev
->found
;
5817 if (tmpdev
->used
== 1)
5822 st
->ss
->free_super(st
);
5828 count_volumes(char *hba
, int dpa
, int verbose
)
5830 struct md_list
*devlist
= NULL
;
5834 devlist
= get_devices(hba
);
5835 /* if no intel devices return zero volumes */
5836 if (devlist
== NULL
)
5839 count
= active_arrays_by_format("imsm", hba
, &devlist
, dpa
, verbose
);
5840 dprintf(" path: %s active arrays: %d\n", hba
, count
);
5841 if (devlist
== NULL
)
5845 count
+= count_volumes_list(devlist
,
5849 dprintf("found %d count: %d\n", found
, count
);
5852 dprintf("path: %s total number of volumes: %d\n", hba
, count
);
5855 struct md_list
*dv
= devlist
;
5856 devlist
= devlist
->next
;
5863 static int imsm_default_chunk(const struct imsm_orom
*orom
)
5865 /* up to 512 if the plaform supports it, otherwise the platform max.
5866 * 128 if no platform detected
5868 int fs
= max(7, orom
? fls(orom
->sss
) : 0);
5870 return min(512, (1 << fs
));
5874 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
5875 int raiddisks
, int *chunk
, unsigned long long size
, int verbose
)
5877 /* check/set platform and metadata limits/defaults */
5878 if (super
->orom
&& raiddisks
> super
->orom
->dpa
) {
5879 pr_vrb(": platform supports a maximum of %d disks per array\n",
5884 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
5885 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
5886 pr_vrb(": platform does not support raid%d with %d disk%s\n",
5887 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
5891 if (chunk
&& (*chunk
== 0 || *chunk
== UnSet
))
5892 *chunk
= imsm_default_chunk(super
->orom
);
5894 if (super
->orom
&& chunk
&& !imsm_orom_has_chunk(super
->orom
, *chunk
)) {
5895 pr_vrb(": platform does not support a chunk size of: "
5900 if (layout
!= imsm_level_to_layout(level
)) {
5902 pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n");
5903 else if (level
== 10)
5904 pr_vrb(": imsm raid 10 only supports the n2 layout\n");
5906 pr_vrb(": imsm unknown layout %#x for this raid level %d\n",
5911 if (super
->orom
&& (super
->orom
->attr
& IMSM_OROM_ATTR_2TB
) == 0 && chunk
&&
5912 (calc_array_size(level
, raiddisks
, layout
, *chunk
, size
) >> 32) > 0) {
5913 pr_vrb(": platform does not support a volume size over 2TB\n");
5919 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
5920 * FIX ME add ahci details
5922 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
5923 int layout
, int raiddisks
, int *chunk
,
5924 unsigned long long size
, char *dev
,
5925 unsigned long long *freesize
,
5929 struct intel_super
*super
= st
->sb
;
5930 struct imsm_super
*mpb
;
5932 unsigned long long pos
= 0;
5933 unsigned long long maxsize
;
5937 /* We must have the container info already read in. */
5941 mpb
= super
->anchor
;
5943 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, size
, verbose
)) {
5944 fprintf(stderr
, Name
": RAID gemetry validation failed. "
5945 "Cannot proceed with the action(s).\n");
5949 /* General test: make sure there is space for
5950 * 'raiddisks' device extents of size 'size' at a given
5953 unsigned long long minsize
= size
;
5954 unsigned long long start_offset
= MaxSector
;
5957 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
5958 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5963 e
= get_extents(super
, dl
);
5966 unsigned long long esize
;
5967 esize
= e
[i
].start
- pos
;
5968 if (esize
>= minsize
)
5970 if (found
&& start_offset
== MaxSector
) {
5973 } else if (found
&& pos
!= start_offset
) {
5977 pos
= e
[i
].start
+ e
[i
].size
;
5979 } while (e
[i
-1].size
);
5984 if (dcnt
< raiddisks
) {
5986 fprintf(stderr
, Name
": imsm: Not enough "
5987 "devices with space for this array "
5995 /* This device must be a member of the set */
5996 if (stat(dev
, &stb
) < 0)
5998 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
6000 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6001 if (dl
->major
== (int)major(stb
.st_rdev
) &&
6002 dl
->minor
== (int)minor(stb
.st_rdev
))
6007 fprintf(stderr
, Name
": %s is not in the "
6008 "same imsm set\n", dev
);
6010 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
6011 /* If a volume is present then the current creation attempt
6012 * cannot incorporate new spares because the orom may not
6013 * understand this configuration (all member disks must be
6014 * members of each array in the container).
6016 fprintf(stderr
, Name
": %s is a spare and a volume"
6017 " is already defined for this container\n", dev
);
6018 fprintf(stderr
, Name
": The option-rom requires all member"
6019 " disks to be a member of all volumes\n");
6021 } else if (super
->orom
&& mpb
->num_raid_devs
> 0 &&
6022 mpb
->num_disks
!= raiddisks
) {
6023 fprintf(stderr
, Name
": The option-rom requires all member"
6024 " disks to be a member of all volumes\n");
6028 /* retrieve the largest free space block */
6029 e
= get_extents(super
, dl
);
6034 unsigned long long esize
;
6036 esize
= e
[i
].start
- pos
;
6037 if (esize
>= maxsize
)
6039 pos
= e
[i
].start
+ e
[i
].size
;
6041 } while (e
[i
-1].size
);
6046 fprintf(stderr
, Name
": unable to determine free space for: %s\n",
6050 if (maxsize
< size
) {
6052 fprintf(stderr
, Name
": %s not enough space (%llu < %llu)\n",
6053 dev
, maxsize
, size
);
6057 /* count total number of extents for merge */
6059 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6061 i
+= dl
->extent_cnt
;
6063 maxsize
= merge_extents(super
, i
);
6065 if (!check_env("IMSM_NO_PLATFORM") &&
6066 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
6067 fprintf(stderr
, Name
": attempting to create a second "
6068 "volume with size less then remaining space. "
6073 if (maxsize
< size
|| maxsize
== 0) {
6076 fprintf(stderr
, Name
": no free space"
6077 " left on device. Aborting...\n");
6079 fprintf(stderr
, Name
": not enough space"
6080 " to create volume of given size"
6081 " (%llu < %llu). Aborting...\n",
6087 *freesize
= maxsize
;
6090 int count
= count_volumes(super
->hba
->path
,
6091 super
->orom
->dpa
, verbose
);
6092 if (super
->orom
->vphba
<= count
) {
6093 pr_vrb(": platform does not support more than %d raid volumes.\n",
6094 super
->orom
->vphba
);
6101 static int reserve_space(struct supertype
*st
, int raiddisks
,
6102 unsigned long long size
, int chunk
,
6103 unsigned long long *freesize
)
6105 struct intel_super
*super
= st
->sb
;
6106 struct imsm_super
*mpb
= super
->anchor
;
6111 unsigned long long maxsize
;
6112 unsigned long long minsize
;
6116 /* find the largest common start free region of the possible disks */
6120 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6126 /* don't activate new spares if we are orom constrained
6127 * and there is already a volume active in the container
6129 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
6132 e
= get_extents(super
, dl
);
6135 for (i
= 1; e
[i
-1].size
; i
++)
6143 maxsize
= merge_extents(super
, extent_cnt
);
6147 minsize
= chunk
* 2;
6149 if (cnt
< raiddisks
||
6150 (super
->orom
&& used
&& used
!= raiddisks
) ||
6151 maxsize
< minsize
||
6153 fprintf(stderr
, Name
": not enough devices with space to create array.\n");
6154 return 0; /* No enough free spaces large enough */
6165 if (!check_env("IMSM_NO_PLATFORM") &&
6166 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
6167 fprintf(stderr
, Name
": attempting to create a second "
6168 "volume with size less then remaining space. "
6173 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6175 dl
->raiddisk
= cnt
++;
6182 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
6183 int raiddisks
, int *chunk
, unsigned long long size
,
6184 char *dev
, unsigned long long *freesize
,
6192 * if given unused devices create a container
6193 * if given given devices in a container create a member volume
6195 if (level
== LEVEL_CONTAINER
) {
6196 /* Must be a fresh device to add to a container */
6197 return validate_geometry_imsm_container(st
, level
, layout
,
6199 chunk
?*chunk
:0, size
,
6206 struct intel_super
*super
= st
->sb
;
6207 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
6208 raiddisks
, chunk
, size
,
6211 /* we are being asked to automatically layout a
6212 * new volume based on the current contents of
6213 * the container. If the the parameters can be
6214 * satisfied reserve_space will record the disks,
6215 * start offset, and size of the volume to be
6216 * created. add_to_super and getinfo_super
6217 * detect when autolayout is in progress.
6219 /* assuming that freesize is always given when array is
6221 if (super
->orom
&& freesize
) {
6223 count
= count_volumes(super
->hba
->path
,
6224 super
->orom
->dpa
, verbose
);
6225 if (super
->orom
->vphba
<= count
) {
6226 pr_vrb(": platform does not support more"
6227 " than %d raid volumes.\n",
6228 super
->orom
->vphba
);
6233 return reserve_space(st
, raiddisks
, size
,
6234 chunk
?*chunk
:0, freesize
);
6239 /* creating in a given container */
6240 return validate_geometry_imsm_volume(st
, level
, layout
,
6241 raiddisks
, chunk
, size
,
6242 dev
, freesize
, verbose
);
6245 /* This device needs to be a device in an 'imsm' container */
6246 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
6250 Name
": Cannot create this array on device %s\n",
6255 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
6257 fprintf(stderr
, Name
": Cannot open %s: %s\n",
6258 dev
, strerror(errno
));
6261 /* Well, it is in use by someone, maybe an 'imsm' container. */
6262 cfd
= open_container(fd
);
6266 fprintf(stderr
, Name
": Cannot use %s: It is busy\n",
6270 sra
= sysfs_read(cfd
, 0, GET_VERSION
);
6271 if (sra
&& sra
->array
.major_version
== -1 &&
6272 strcmp(sra
->text_version
, "imsm") == 0)
6276 /* This is a member of a imsm container. Load the container
6277 * and try to create a volume
6279 struct intel_super
*super
;
6281 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
, NULL
, 1) == 0) {
6283 st
->container_dev
= fd2devnum(cfd
);
6285 return validate_geometry_imsm_volume(st
, level
, layout
,
6294 fprintf(stderr
, Name
": failed container membership check\n");
6300 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
6302 struct intel_super
*super
= st
->sb
;
6304 if (level
&& *level
== UnSet
)
6305 *level
= LEVEL_CONTAINER
;
6307 if (level
&& layout
&& *layout
== UnSet
)
6308 *layout
= imsm_level_to_layout(*level
);
6310 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0))
6311 *chunk
= imsm_default_chunk(super
->orom
);
6314 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
6316 static int kill_subarray_imsm(struct supertype
*st
)
6318 /* remove the subarray currently referenced by ->current_vol */
6320 struct intel_dev
**dp
;
6321 struct intel_super
*super
= st
->sb
;
6322 __u8 current_vol
= super
->current_vol
;
6323 struct imsm_super
*mpb
= super
->anchor
;
6325 if (super
->current_vol
< 0)
6327 super
->current_vol
= -1; /* invalidate subarray cursor */
6329 /* block deletions that would change the uuid of active subarrays
6331 * FIXME when immutable ids are available, but note that we'll
6332 * also need to fixup the invalidated/active subarray indexes in
6335 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6338 if (i
< current_vol
)
6340 sprintf(subarray
, "%u", i
);
6341 if (is_subarray_active(subarray
, st
->devname
)) {
6343 Name
": deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
6350 if (st
->update_tail
) {
6351 struct imsm_update_kill_array
*u
= malloc(sizeof(*u
));
6355 u
->type
= update_kill_array
;
6356 u
->dev_idx
= current_vol
;
6357 append_metadata_update(st
, u
, sizeof(*u
));
6362 for (dp
= &super
->devlist
; *dp
;)
6363 if ((*dp
)->index
== current_vol
) {
6366 handle_missing(super
, (*dp
)->dev
);
6367 if ((*dp
)->index
> current_vol
)
6372 /* no more raid devices, all active components are now spares,
6373 * but of course failed are still failed
6375 if (--mpb
->num_raid_devs
== 0) {
6378 for (d
= super
->disks
; d
; d
= d
->next
)
6383 super
->updates_pending
++;
6388 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
6389 char *update
, struct mddev_ident
*ident
)
6391 /* update the subarray currently referenced by ->current_vol */
6392 struct intel_super
*super
= st
->sb
;
6393 struct imsm_super
*mpb
= super
->anchor
;
6395 if (strcmp(update
, "name") == 0) {
6396 char *name
= ident
->name
;
6400 if (is_subarray_active(subarray
, st
->devname
)) {
6402 Name
": Unable to update name of active subarray\n");
6406 if (!check_name(super
, name
, 0))
6409 vol
= strtoul(subarray
, &ep
, 10);
6410 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
6413 if (st
->update_tail
) {
6414 struct imsm_update_rename_array
*u
= malloc(sizeof(*u
));
6418 u
->type
= update_rename_array
;
6420 snprintf((char *) u
->name
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6421 append_metadata_update(st
, u
, sizeof(*u
));
6423 struct imsm_dev
*dev
;
6426 dev
= get_imsm_dev(super
, vol
);
6427 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6428 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6429 dev
= get_imsm_dev(super
, i
);
6430 handle_missing(super
, dev
);
6432 super
->updates_pending
++;
6439 #endif /* MDASSEMBLE */
6441 static int is_gen_migration(struct imsm_dev
*dev
)
6446 if (!dev
->vol
.migr_state
)
6449 if (migr_type(dev
) == MIGR_GEN_MIGR
)
6455 static int is_rebuilding(struct imsm_dev
*dev
)
6457 struct imsm_map
*migr_map
;
6459 if (!dev
->vol
.migr_state
)
6462 if (migr_type(dev
) != MIGR_REBUILD
)
6465 migr_map
= get_imsm_map(dev
, MAP_1
);
6467 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
6474 static int is_initializing(struct imsm_dev
*dev
)
6476 struct imsm_map
*migr_map
;
6478 if (!dev
->vol
.migr_state
)
6481 if (migr_type(dev
) != MIGR_INIT
)
6484 migr_map
= get_imsm_map(dev
, MAP_1
);
6486 if (migr_map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
6493 static void update_recovery_start(struct intel_super
*super
,
6494 struct imsm_dev
*dev
,
6495 struct mdinfo
*array
)
6497 struct mdinfo
*rebuild
= NULL
;
6501 if (!is_rebuilding(dev
))
6504 /* Find the rebuild target, but punt on the dual rebuild case */
6505 for (d
= array
->devs
; d
; d
= d
->next
)
6506 if (d
->recovery_start
== 0) {
6513 /* (?) none of the disks are marked with
6514 * IMSM_ORD_REBUILD, so assume they are missing and the
6515 * disk_ord_tbl was not correctly updated
6517 dprintf("%s: failed to locate out-of-sync disk\n", __func__
);
6521 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
6522 rebuild
->recovery_start
= units
* blocks_per_migr_unit(super
, dev
);
6526 static int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
);
6529 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
6531 /* Given a container loaded by load_super_imsm_all,
6532 * extract information about all the arrays into
6534 * If 'subarray' is given, just extract info about that array.
6536 * For each imsm_dev create an mdinfo, fill it in,
6537 * then look for matching devices in super->disks
6538 * and create appropriate device mdinfo.
6540 struct intel_super
*super
= st
->sb
;
6541 struct imsm_super
*mpb
= super
->anchor
;
6542 struct mdinfo
*rest
= NULL
;
6546 int spare_disks
= 0;
6548 /* do not assemble arrays when not all attributes are supported */
6549 if (imsm_check_attributes(mpb
->attributes
) == 0) {
6551 fprintf(stderr
, Name
": Unsupported attributes in IMSM metadata."
6552 "Arrays activation is blocked.\n");
6555 /* check for bad blocks */
6556 if (imsm_bbm_log_size(super
->anchor
)) {
6557 fprintf(stderr
, Name
": BBM log found in IMSM metadata."
6558 "Arrays activation is blocked.\n");
6563 /* count spare devices, not used in maps
6565 for (d
= super
->disks
; d
; d
= d
->next
)
6569 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6570 struct imsm_dev
*dev
;
6571 struct imsm_map
*map
;
6572 struct imsm_map
*map2
;
6573 struct mdinfo
*this;
6581 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
6584 dev
= get_imsm_dev(super
, i
);
6585 map
= get_imsm_map(dev
, MAP_0
);
6586 map2
= get_imsm_map(dev
, MAP_1
);
6588 /* do not publish arrays that are in the middle of an
6589 * unsupported migration
6591 if (dev
->vol
.migr_state
&&
6592 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
6593 fprintf(stderr
, Name
": cannot assemble volume '%.16s':"
6594 " unsupported migration in progress\n",
6598 /* do not publish arrays that are not support by controller's
6602 this = malloc(sizeof(*this));
6604 fprintf(stderr
, Name
": failed to allocate %zu bytes\n",
6609 super
->current_vol
= i
;
6610 getinfo_super_imsm_volume(st
, this, NULL
);
6613 chunk
= __le16_to_cpu(map
->blocks_per_strip
) >> 1;
6614 /* mdadm does not support all metadata features- set the bit in all arrays state */
6615 if (!validate_geometry_imsm_orom(super
,
6616 get_imsm_raid_level(map
), /* RAID level */
6617 imsm_level_to_layout(get_imsm_raid_level(map
)),
6618 map
->num_members
, /* raid disks */
6619 &chunk
, join_u32(dev
->size_low
, dev
->size_high
),
6621 fprintf(stderr
, Name
": IMSM RAID geometry validation"
6622 " failed. Array %s activation is blocked.\n",
6624 this->array
.state
|=
6625 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
6626 (1<<MD_SB_BLOCK_VOLUME
);
6630 /* if array has bad blocks, set suitable bit in all arrays state */
6632 this->array
.state
|=
6633 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
6634 (1<<MD_SB_BLOCK_VOLUME
);
6636 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
6637 unsigned long long recovery_start
;
6638 struct mdinfo
*info_d
;
6645 idx
= get_imsm_disk_idx(dev
, slot
, MAP_0
);
6646 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
6647 for (d
= super
->disks
; d
; d
= d
->next
)
6648 if (d
->index
== idx
)
6651 recovery_start
= MaxSector
;
6654 if (d
&& is_failed(&d
->disk
))
6656 if (ord
& IMSM_ORD_REBUILD
)
6660 * if we skip some disks the array will be assmebled degraded;
6661 * reset resync start to avoid a dirty-degraded
6662 * situation when performing the intial sync
6664 * FIXME handle dirty degraded
6666 if ((skip
|| recovery_start
== 0) && !dev
->vol
.dirty
)
6667 this->resync_start
= MaxSector
;
6671 info_d
= calloc(1, sizeof(*info_d
));
6673 fprintf(stderr
, Name
": failed to allocate disk"
6674 " for volume %.16s\n", dev
->volume
);
6675 info_d
= this->devs
;
6677 struct mdinfo
*d
= info_d
->next
;
6686 info_d
->next
= this->devs
;
6687 this->devs
= info_d
;
6689 info_d
->disk
.number
= d
->index
;
6690 info_d
->disk
.major
= d
->major
;
6691 info_d
->disk
.minor
= d
->minor
;
6692 info_d
->disk
.raid_disk
= slot
;
6693 info_d
->recovery_start
= recovery_start
;
6695 if (slot
< map2
->num_members
)
6696 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
6698 this->array
.spare_disks
++;
6700 if (slot
< map
->num_members
)
6701 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
6703 this->array
.spare_disks
++;
6705 if (info_d
->recovery_start
== MaxSector
)
6706 this->array
.working_disks
++;
6708 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
6709 info_d
->data_offset
= pba_of_lba0(map
);
6710 info_d
->component_size
= blocks_per_member(map
);
6712 /* now that the disk list is up-to-date fixup recovery_start */
6713 update_recovery_start(super
, dev
, this);
6714 this->array
.spare_disks
+= spare_disks
;
6717 /* check for reshape */
6718 if (this->reshape_active
== 1)
6719 recover_backup_imsm(st
, this);
6728 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
6729 int failed
, int look_in_map
)
6731 struct imsm_map
*map
;
6733 map
= get_imsm_map(dev
, look_in_map
);
6736 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
6737 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
6739 switch (get_imsm_raid_level(map
)) {
6741 return IMSM_T_STATE_FAILED
;
6744 if (failed
< map
->num_members
)
6745 return IMSM_T_STATE_DEGRADED
;
6747 return IMSM_T_STATE_FAILED
;
6752 * check to see if any mirrors have failed, otherwise we
6753 * are degraded. Even numbered slots are mirrored on
6757 /* gcc -Os complains that this is unused */
6758 int insync
= insync
;
6760 for (i
= 0; i
< map
->num_members
; i
++) {
6761 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_X
);
6762 int idx
= ord_to_idx(ord
);
6763 struct imsm_disk
*disk
;
6765 /* reset the potential in-sync count on even-numbered
6766 * slots. num_copies is always 2 for imsm raid10
6771 disk
= get_imsm_disk(super
, idx
);
6772 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
6775 /* no in-sync disks left in this mirror the
6779 return IMSM_T_STATE_FAILED
;
6782 return IMSM_T_STATE_DEGRADED
;
6786 return IMSM_T_STATE_DEGRADED
;
6788 return IMSM_T_STATE_FAILED
;
6794 return map
->map_state
;
6797 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
6802 struct imsm_disk
*disk
;
6803 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
6804 struct imsm_map
*prev
= get_imsm_map(dev
, MAP_1
);
6805 struct imsm_map
*map_for_loop
;
6810 /* at the beginning of migration we set IMSM_ORD_REBUILD on
6811 * disks that are being rebuilt. New failures are recorded to
6812 * map[0]. So we look through all the disks we started with and
6813 * see if any failures are still present, or if any new ones
6817 if (prev
&& (map
->num_members
< prev
->num_members
))
6818 map_for_loop
= prev
;
6820 for (i
= 0; i
< map_for_loop
->num_members
; i
++) {
6822 /* when MAP_X is passed both maps failures are counted
6825 ((look_in_map
== MAP_1
) || (look_in_map
== MAP_X
)) &&
6826 (i
< prev
->num_members
)) {
6827 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
6828 idx_1
= ord_to_idx(ord
);
6830 disk
= get_imsm_disk(super
, idx_1
);
6831 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
6834 if (((look_in_map
== MAP_0
) || (look_in_map
== MAP_X
)) &&
6835 (i
< map
->num_members
)) {
6836 ord
= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
6837 idx
= ord_to_idx(ord
);
6840 disk
= get_imsm_disk(super
, idx
);
6841 if (!disk
|| is_failed(disk
) ||
6842 ord
& IMSM_ORD_REBUILD
)
6852 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
6855 struct intel_super
*super
= c
->sb
;
6856 struct imsm_super
*mpb
= super
->anchor
;
6858 if (atoi(inst
) >= mpb
->num_raid_devs
) {
6859 fprintf(stderr
, "%s: subarry index %d, out of range\n",
6860 __func__
, atoi(inst
));
6864 dprintf("imsm: open_new %s\n", inst
);
6865 a
->info
.container_member
= atoi(inst
);
6869 static int is_resyncing(struct imsm_dev
*dev
)
6871 struct imsm_map
*migr_map
;
6873 if (!dev
->vol
.migr_state
)
6876 if (migr_type(dev
) == MIGR_INIT
||
6877 migr_type(dev
) == MIGR_REPAIR
)
6880 if (migr_type(dev
) == MIGR_GEN_MIGR
)
6883 migr_map
= get_imsm_map(dev
, MAP_1
);
6885 if ((migr_map
->map_state
== IMSM_T_STATE_NORMAL
) &&
6886 (dev
->vol
.migr_type
!= MIGR_GEN_MIGR
))
6892 /* return true if we recorded new information */
6893 static int mark_failure(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
6897 struct imsm_map
*map
;
6898 char buf
[MAX_RAID_SERIAL_LEN
+3];
6899 unsigned int len
, shift
= 0;
6901 /* new failures are always set in map[0] */
6902 map
= get_imsm_map(dev
, MAP_0
);
6904 slot
= get_imsm_disk_slot(map
, idx
);
6908 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
6909 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
6912 memcpy(buf
, disk
->serial
, MAX_RAID_SERIAL_LEN
);
6913 buf
[MAX_RAID_SERIAL_LEN
] = '\000';
6915 if ((len
= strlen(buf
)) >= MAX_RAID_SERIAL_LEN
)
6916 shift
= len
- MAX_RAID_SERIAL_LEN
+ 1;
6917 strncpy((char *)disk
->serial
, &buf
[shift
], MAX_RAID_SERIAL_LEN
);
6919 disk
->status
|= FAILED_DISK
;
6920 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
6921 /* mark failures in second map if second map exists and this disk
6923 * This is valid for migration, initialization and rebuild
6925 if (dev
->vol
.migr_state
) {
6926 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
6927 int slot2
= get_imsm_disk_slot(map2
, idx
);
6929 if ((slot2
< map2
->num_members
) &&
6931 set_imsm_ord_tbl_ent(map2
, slot2
,
6932 idx
| IMSM_ORD_REBUILD
);
6934 if (map
->failed_disk_num
== 0xff)
6935 map
->failed_disk_num
= slot
;
6939 static void mark_missing(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
6941 mark_failure(dev
, disk
, idx
);
6943 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
6946 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
6947 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
6950 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
6954 if (!super
->missing
)
6957 dprintf("imsm: mark missing\n");
6958 /* end process for initialization and rebuild only
6960 if (is_gen_migration(dev
) == 0) {
6964 failed
= imsm_count_failed(super
, dev
, MAP_0
);
6965 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
6967 end_migration(dev
, super
, map_state
);
6969 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
6970 mark_missing(dev
, &dl
->disk
, dl
->index
);
6971 super
->updates_pending
++;
6974 static unsigned long long imsm_set_array_size(struct imsm_dev
*dev
)
6976 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
6977 unsigned long long array_blocks
;
6978 struct imsm_map
*map
;
6980 if (used_disks
== 0) {
6981 /* when problems occures
6982 * return current array_blocks value
6984 array_blocks
= __le32_to_cpu(dev
->size_high
);
6985 array_blocks
= array_blocks
<< 32;
6986 array_blocks
+= __le32_to_cpu(dev
->size_low
);
6988 return array_blocks
;
6991 /* set array size in metadata
6993 map
= get_imsm_map(dev
, MAP_0
);
6994 array_blocks
= blocks_per_member(map
) * used_disks
;
6996 /* round array size down to closest MB
6998 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
6999 dev
->size_low
= __cpu_to_le32((__u32
)array_blocks
);
7000 dev
->size_high
= __cpu_to_le32((__u32
)(array_blocks
>> 32));
7002 return array_blocks
;
7005 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
7007 static void imsm_progress_container_reshape(struct intel_super
*super
)
7009 /* if no device has a migr_state, but some device has a
7010 * different number of members than the previous device, start
7011 * changing the number of devices in this device to match
7014 struct imsm_super
*mpb
= super
->anchor
;
7015 int prev_disks
= -1;
7019 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7020 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
7021 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7022 struct imsm_map
*map2
;
7023 int prev_num_members
;
7025 if (dev
->vol
.migr_state
)
7028 if (prev_disks
== -1)
7029 prev_disks
= map
->num_members
;
7030 if (prev_disks
== map
->num_members
)
7033 /* OK, this array needs to enter reshape mode.
7034 * i.e it needs a migr_state
7037 copy_map_size
= sizeof_imsm_map(map
);
7038 prev_num_members
= map
->num_members
;
7039 map
->num_members
= prev_disks
;
7040 dev
->vol
.migr_state
= 1;
7041 dev
->vol
.curr_migr_unit
= 0;
7042 set_migr_type(dev
, MIGR_GEN_MIGR
);
7043 for (i
= prev_num_members
;
7044 i
< map
->num_members
; i
++)
7045 set_imsm_ord_tbl_ent(map
, i
, i
);
7046 map2
= get_imsm_map(dev
, MAP_1
);
7047 /* Copy the current map */
7048 memcpy(map2
, map
, copy_map_size
);
7049 map2
->num_members
= prev_num_members
;
7051 imsm_set_array_size(dev
);
7052 super
->clean_migration_record_by_mdmon
= 1;
7053 super
->updates_pending
++;
7057 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
7058 * states are handled in imsm_set_disk() with one exception, when a
7059 * resync is stopped due to a new failure this routine will set the
7060 * 'degraded' state for the array.
7062 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
7064 int inst
= a
->info
.container_member
;
7065 struct intel_super
*super
= a
->container
->sb
;
7066 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7067 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7068 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
7069 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7070 __u32 blocks_per_unit
;
7072 if (dev
->vol
.migr_state
&&
7073 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
7074 /* array state change is blocked due to reshape action
7076 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
7077 * - finish the reshape (if last_checkpoint is big and action != reshape)
7078 * - update curr_migr_unit
7080 if (a
->curr_action
== reshape
) {
7081 /* still reshaping, maybe update curr_migr_unit */
7082 goto mark_checkpoint
;
7084 if (a
->last_checkpoint
== 0 && a
->prev_action
== reshape
) {
7085 /* for some reason we aborted the reshape.
7087 * disable automatic metadata rollback
7088 * user action is required to recover process
7091 struct imsm_map
*map2
=
7092 get_imsm_map(dev
, MAP_1
);
7093 dev
->vol
.migr_state
= 0;
7094 set_migr_type(dev
, 0);
7095 dev
->vol
.curr_migr_unit
= 0;
7097 sizeof_imsm_map(map2
));
7098 super
->updates_pending
++;
7101 if (a
->last_checkpoint
>= a
->info
.component_size
) {
7102 unsigned long long array_blocks
;
7106 used_disks
= imsm_num_data_members(dev
, MAP_0
);
7107 if (used_disks
> 0) {
7109 blocks_per_member(map
) *
7111 /* round array size down to closest MB
7113 array_blocks
= (array_blocks
7114 >> SECT_PER_MB_SHIFT
)
7115 << SECT_PER_MB_SHIFT
;
7116 a
->info
.custom_array_size
= array_blocks
;
7117 /* encourage manager to update array
7121 a
->check_reshape
= 1;
7123 /* finalize online capacity expansion/reshape */
7124 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
7126 mdi
->disk
.raid_disk
,
7129 imsm_progress_container_reshape(super
);
7134 /* before we activate this array handle any missing disks */
7135 if (consistent
== 2)
7136 handle_missing(super
, dev
);
7138 if (consistent
== 2 &&
7139 (!is_resync_complete(&a
->info
) ||
7140 map_state
!= IMSM_T_STATE_NORMAL
||
7141 dev
->vol
.migr_state
))
7144 if (is_resync_complete(&a
->info
)) {
7145 /* complete intialization / resync,
7146 * recovery and interrupted recovery is completed in
7149 if (is_resyncing(dev
)) {
7150 dprintf("imsm: mark resync done\n");
7151 end_migration(dev
, super
, map_state
);
7152 super
->updates_pending
++;
7153 a
->last_checkpoint
= 0;
7155 } else if ((!is_resyncing(dev
) && !failed
) &&
7156 (imsm_reshape_blocks_arrays_changes(super
) == 0)) {
7157 /* mark the start of the init process if nothing is failed */
7158 dprintf("imsm: mark resync start\n");
7159 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
7160 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
7162 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
7163 super
->updates_pending
++;
7167 /* skip checkpointing for general migration,
7168 * it is controlled in mdadm
7170 if (is_gen_migration(dev
))
7171 goto skip_mark_checkpoint
;
7173 /* check if we can update curr_migr_unit from resync_start, recovery_start */
7174 blocks_per_unit
= blocks_per_migr_unit(super
, dev
);
7175 if (blocks_per_unit
) {
7179 units
= a
->last_checkpoint
/ blocks_per_unit
;
7182 /* check that we did not overflow 32-bits, and that
7183 * curr_migr_unit needs updating
7185 if (units32
== units
&&
7187 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
7188 dprintf("imsm: mark checkpoint (%u)\n", units32
);
7189 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
7190 super
->updates_pending
++;
7194 skip_mark_checkpoint
:
7195 /* mark dirty / clean */
7196 if (dev
->vol
.dirty
!= !consistent
) {
7197 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
7202 super
->updates_pending
++;
7208 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
7210 int inst
= a
->info
.container_member
;
7211 struct intel_super
*super
= a
->container
->sb
;
7212 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7213 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7214 struct imsm_disk
*disk
;
7219 if (n
> map
->num_members
)
7220 fprintf(stderr
, "imsm: set_disk %d out of range 0..%d\n",
7221 n
, map
->num_members
- 1);
7226 dprintf("imsm: set_disk %d:%x\n", n
, state
);
7228 ord
= get_imsm_ord_tbl_ent(dev
, n
, MAP_0
);
7229 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
7231 /* check for new failures */
7232 if (state
& DS_FAULTY
) {
7233 if (mark_failure(dev
, disk
, ord_to_idx(ord
)))
7234 super
->updates_pending
++;
7237 /* check if in_sync */
7238 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
7239 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
7241 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
7242 super
->updates_pending
++;
7245 failed
= imsm_count_failed(super
, dev
, MAP_0
);
7246 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7248 /* check if recovery complete, newly degraded, or failed */
7249 dprintf("imsm: Detected transition to state ");
7250 switch (map_state
) {
7251 case IMSM_T_STATE_NORMAL
: /* transition to normal state */
7252 dprintf("normal: ");
7253 if (is_rebuilding(dev
)) {
7254 dprintf("while rebuilding");
7255 end_migration(dev
, super
, map_state
);
7256 map
= get_imsm_map(dev
, MAP_0
);
7257 map
->failed_disk_num
= ~0;
7258 super
->updates_pending
++;
7259 a
->last_checkpoint
= 0;
7262 if (is_gen_migration(dev
)) {
7263 dprintf("while general migration");
7264 if (a
->last_checkpoint
>= a
->info
.component_size
)
7265 end_migration(dev
, super
, map_state
);
7267 map
->map_state
= map_state
;
7268 map
= get_imsm_map(dev
, MAP_0
);
7269 map
->failed_disk_num
= ~0;
7270 super
->updates_pending
++;
7274 case IMSM_T_STATE_DEGRADED
: /* transition to degraded state */
7275 dprintf("degraded: ");
7276 if ((map
->map_state
!= map_state
) &&
7277 !dev
->vol
.migr_state
) {
7278 dprintf("mark degraded");
7279 map
->map_state
= map_state
;
7280 super
->updates_pending
++;
7281 a
->last_checkpoint
= 0;
7284 if (is_rebuilding(dev
)) {
7285 dprintf("while rebuilding.");
7286 if (map
->map_state
!= map_state
) {
7287 dprintf(" Map state change");
7288 end_migration(dev
, super
, map_state
);
7289 super
->updates_pending
++;
7293 if (is_gen_migration(dev
)) {
7294 dprintf("while general migration");
7295 if (a
->last_checkpoint
>= a
->info
.component_size
)
7296 end_migration(dev
, super
, map_state
);
7298 map
->map_state
= map_state
;
7299 manage_second_map(super
, dev
);
7301 super
->updates_pending
++;
7304 if (is_initializing(dev
)) {
7305 dprintf("while initialization.");
7306 map
->map_state
= map_state
;
7307 super
->updates_pending
++;
7311 case IMSM_T_STATE_FAILED
: /* transition to failed state */
7312 dprintf("failed: ");
7313 if (is_gen_migration(dev
)) {
7314 dprintf("while general migration");
7315 map
->map_state
= map_state
;
7316 super
->updates_pending
++;
7319 if (map
->map_state
!= map_state
) {
7320 dprintf("mark failed");
7321 end_migration(dev
, super
, map_state
);
7322 super
->updates_pending
++;
7323 a
->last_checkpoint
= 0;
7328 dprintf("state %i\n", map_state
);
7334 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
7337 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
7338 unsigned long long dsize
;
7339 unsigned long long sectors
;
7341 get_dev_size(fd
, NULL
, &dsize
);
7343 if (mpb_size
> 512) {
7344 /* -1 to account for anchor */
7345 sectors
= mpb_sectors(mpb
) - 1;
7347 /* write the extended mpb to the sectors preceeding the anchor */
7348 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0)
7351 if ((unsigned long long)write(fd
, buf
+ 512, 512 * sectors
)
7356 /* first block is stored on second to last sector of the disk */
7357 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0)
7360 if (write(fd
, buf
, 512) != 512)
7366 static void imsm_sync_metadata(struct supertype
*container
)
7368 struct intel_super
*super
= container
->sb
;
7370 dprintf("sync metadata: %d\n", super
->updates_pending
);
7371 if (!super
->updates_pending
)
7374 write_super_imsm(container
, 0);
7376 super
->updates_pending
= 0;
7379 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
7381 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
7382 int i
= get_imsm_disk_idx(dev
, idx
, MAP_X
);
7385 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7389 if (dl
&& is_failed(&dl
->disk
))
7393 dprintf("%s: found %x:%x\n", __func__
, dl
->major
, dl
->minor
);
7398 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
7399 struct active_array
*a
, int activate_new
,
7400 struct mdinfo
*additional_test_list
)
7402 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
7403 int idx
= get_imsm_disk_idx(dev
, slot
, MAP_X
);
7404 struct imsm_super
*mpb
= super
->anchor
;
7405 struct imsm_map
*map
;
7406 unsigned long long pos
;
7411 __u32 array_start
= 0;
7412 __u32 array_end
= 0;
7414 struct mdinfo
*test_list
;
7416 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7417 /* If in this array, skip */
7418 for (d
= a
->info
.devs
; d
; d
= d
->next
)
7419 if (d
->state_fd
>= 0 &&
7420 d
->disk
.major
== dl
->major
&&
7421 d
->disk
.minor
== dl
->minor
) {
7422 dprintf("%x:%x already in array\n",
7423 dl
->major
, dl
->minor
);
7428 test_list
= additional_test_list
;
7430 if (test_list
->disk
.major
== dl
->major
&&
7431 test_list
->disk
.minor
== dl
->minor
) {
7432 dprintf("%x:%x already in additional test list\n",
7433 dl
->major
, dl
->minor
);
7436 test_list
= test_list
->next
;
7441 /* skip in use or failed drives */
7442 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
7444 dprintf("%x:%x status (failed: %d index: %d)\n",
7445 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
7449 /* skip pure spares when we are looking for partially
7450 * assimilated drives
7452 if (dl
->index
== -1 && !activate_new
)
7455 /* Does this unused device have the requisite free space?
7456 * It needs to be able to cover all member volumes
7458 ex
= get_extents(super
, dl
);
7460 dprintf("cannot get extents\n");
7463 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7464 dev
= get_imsm_dev(super
, i
);
7465 map
= get_imsm_map(dev
, MAP_0
);
7467 /* check if this disk is already a member of
7470 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
7476 array_start
= pba_of_lba0(map
);
7477 array_end
= array_start
+
7478 blocks_per_member(map
) - 1;
7481 /* check that we can start at pba_of_lba0 with
7482 * blocks_per_member of space
7484 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
7488 pos
= ex
[j
].start
+ ex
[j
].size
;
7490 } while (ex
[j
-1].size
);
7497 if (i
< mpb
->num_raid_devs
) {
7498 dprintf("%x:%x does not have %u to %u available\n",
7499 dl
->major
, dl
->minor
, array_start
, array_end
);
7510 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
7512 struct imsm_dev
*dev2
;
7513 struct imsm_map
*map
;
7519 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
7521 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
, MAP_0
);
7522 if (state
== IMSM_T_STATE_FAILED
) {
7523 map
= get_imsm_map(dev2
, MAP_0
);
7526 for (slot
= 0; slot
< map
->num_members
; slot
++) {
7528 * Check if failed disks are deleted from intel
7529 * disk list or are marked to be deleted
7531 idx
= get_imsm_disk_idx(dev2
, slot
, MAP_X
);
7532 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
7534 * Do not rebuild the array if failed disks
7535 * from failed sub-array are not removed from
7539 is_failed(&idisk
->disk
) &&
7540 (idisk
->action
!= DISK_REMOVE
))
7548 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
7549 struct metadata_update
**updates
)
7552 * Find a device with unused free space and use it to replace a
7553 * failed/vacant region in an array. We replace failed regions one a
7554 * array at a time. The result is that a new spare disk will be added
7555 * to the first failed array and after the monitor has finished
7556 * propagating failures the remainder will be consumed.
7558 * FIXME add a capability for mdmon to request spares from another
7562 struct intel_super
*super
= a
->container
->sb
;
7563 int inst
= a
->info
.container_member
;
7564 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7565 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7566 int failed
= a
->info
.array
.raid_disks
;
7567 struct mdinfo
*rv
= NULL
;
7570 struct metadata_update
*mu
;
7572 struct imsm_update_activate_spare
*u
;
7577 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
7578 if ((d
->curr_state
& DS_FAULTY
) &&
7580 /* wait for Removal to happen */
7582 if (d
->state_fd
>= 0)
7586 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
7587 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
7589 if (imsm_reshape_blocks_arrays_changes(super
))
7592 /* Cannot activate another spare if rebuild is in progress already
7594 if (is_rebuilding(dev
)) {
7595 dprintf("imsm: No spare activation allowed. "
7596 "Rebuild in progress already.\n");
7600 if (a
->info
.array
.level
== 4)
7601 /* No repair for takeovered array
7602 * imsm doesn't support raid4
7606 if (imsm_check_degraded(super
, dev
, failed
, MAP_0
) !=
7607 IMSM_T_STATE_DEGRADED
)
7611 * If there are any failed disks check state of the other volume.
7612 * Block rebuild if the another one is failed until failed disks
7613 * are removed from container.
7616 dprintf("found failed disks in %.*s, check if there another"
7617 "failed sub-array.\n",
7618 MAX_RAID_SERIAL_LEN
, dev
->volume
);
7619 /* check if states of the other volumes allow for rebuild */
7620 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
7622 allowed
= imsm_rebuild_allowed(a
->container
,
7630 /* For each slot, if it is not working, find a spare */
7631 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
7632 for (d
= a
->info
.devs
; d
; d
= d
->next
)
7633 if (d
->disk
.raid_disk
== i
)
7635 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
7636 if (d
&& (d
->state_fd
>= 0))
7640 * OK, this device needs recovery. Try to re-add the
7641 * previous occupant of this slot, if this fails see if
7642 * we can continue the assimilation of a spare that was
7643 * partially assimilated, finally try to activate a new
7646 dl
= imsm_readd(super
, i
, a
);
7648 dl
= imsm_add_spare(super
, i
, a
, 0, rv
);
7650 dl
= imsm_add_spare(super
, i
, a
, 1, rv
);
7654 /* found a usable disk with enough space */
7655 di
= malloc(sizeof(*di
));
7658 memset(di
, 0, sizeof(*di
));
7660 /* dl->index will be -1 in the case we are activating a
7661 * pristine spare. imsm_process_update() will create a
7662 * new index in this case. Once a disk is found to be
7663 * failed in all member arrays it is kicked from the
7666 di
->disk
.number
= dl
->index
;
7668 /* (ab)use di->devs to store a pointer to the device
7671 di
->devs
= (struct mdinfo
*) dl
;
7673 di
->disk
.raid_disk
= i
;
7674 di
->disk
.major
= dl
->major
;
7675 di
->disk
.minor
= dl
->minor
;
7677 di
->recovery_start
= 0;
7678 di
->data_offset
= pba_of_lba0(map
);
7679 di
->component_size
= a
->info
.component_size
;
7680 di
->container_member
= inst
;
7681 super
->random
= random32();
7685 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
7686 i
, di
->data_offset
);
7690 /* No spares found */
7692 /* Now 'rv' has a list of devices to return.
7693 * Create a metadata_update record to update the
7694 * disk_ord_tbl for the array
7696 mu
= malloc(sizeof(*mu
));
7698 mu
->buf
= malloc(sizeof(struct imsm_update_activate_spare
) * num_spares
);
7699 if (mu
->buf
== NULL
) {
7706 struct mdinfo
*n
= rv
->next
;
7715 mu
->space_list
= NULL
;
7716 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
7717 mu
->next
= *updates
;
7718 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
7720 for (di
= rv
; di
; di
= di
->next
) {
7721 u
->type
= update_activate_spare
;
7722 u
->dl
= (struct dl
*) di
->devs
;
7724 u
->slot
= di
->disk
.raid_disk
;
7735 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
7737 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
7738 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7739 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, MAP_0
);
7740 struct disk_info
*inf
= get_disk_info(u
);
7741 struct imsm_disk
*disk
;
7745 for (i
= 0; i
< map
->num_members
; i
++) {
7746 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, MAP_X
));
7747 for (j
= 0; j
< new_map
->num_members
; j
++)
7748 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
7756 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
7758 struct dl
*dl
= NULL
;
7759 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7760 if ((dl
->major
== major
) && (dl
->minor
== minor
))
7765 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
7767 struct dl
*prev
= NULL
;
7771 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7772 if ((dl
->major
== major
) && (dl
->minor
== minor
)) {
7775 prev
->next
= dl
->next
;
7777 super
->disks
= dl
->next
;
7779 __free_imsm_disk(dl
);
7780 dprintf("%s: removed %x:%x\n",
7781 __func__
, major
, minor
);
7789 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
7791 static int add_remove_disk_update(struct intel_super
*super
)
7793 int check_degraded
= 0;
7794 struct dl
*disk
= NULL
;
7795 /* add/remove some spares to/from the metadata/contrainer */
7796 while (super
->disk_mgmt_list
) {
7797 struct dl
*disk_cfg
;
7799 disk_cfg
= super
->disk_mgmt_list
;
7800 super
->disk_mgmt_list
= disk_cfg
->next
;
7801 disk_cfg
->next
= NULL
;
7803 if (disk_cfg
->action
== DISK_ADD
) {
7804 disk_cfg
->next
= super
->disks
;
7805 super
->disks
= disk_cfg
;
7807 dprintf("%s: added %x:%x\n",
7808 __func__
, disk_cfg
->major
,
7810 } else if (disk_cfg
->action
== DISK_REMOVE
) {
7811 dprintf("Disk remove action processed: %x.%x\n",
7812 disk_cfg
->major
, disk_cfg
->minor
);
7813 disk
= get_disk_super(super
,
7817 /* store action status */
7818 disk
->action
= DISK_REMOVE
;
7819 /* remove spare disks only */
7820 if (disk
->index
== -1) {
7821 remove_disk_super(super
,
7826 /* release allocate disk structure */
7827 __free_imsm_disk(disk_cfg
);
7830 return check_degraded
;
7834 static int apply_reshape_migration_update(struct imsm_update_reshape_migration
*u
,
7835 struct intel_super
*super
,
7838 struct intel_dev
*id
;
7839 void **tofree
= NULL
;
7842 dprintf("apply_reshape_migration_update()\n");
7843 if ((u
->subdev
< 0) ||
7845 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
7848 if ((space_list
== NULL
) || (*space_list
== NULL
)) {
7849 dprintf("imsm: Error: Memory is not allocated\n");
7853 for (id
= super
->devlist
; id
; id
= id
->next
) {
7854 if (id
->index
== (unsigned)u
->subdev
) {
7855 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
7856 struct imsm_map
*map
;
7857 struct imsm_dev
*new_dev
=
7858 (struct imsm_dev
*)*space_list
;
7859 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
7861 struct dl
*new_disk
;
7863 if (new_dev
== NULL
)
7865 *space_list
= **space_list
;
7866 memcpy(new_dev
, dev
, sizeof_imsm_dev(dev
, 0));
7867 map
= get_imsm_map(new_dev
, MAP_0
);
7869 dprintf("imsm: Error: migration in progress");
7873 to_state
= map
->map_state
;
7874 if ((u
->new_level
== 5) && (map
->raid_level
== 0)) {
7876 /* this should not happen */
7877 if (u
->new_disks
[0] < 0) {
7878 map
->failed_disk_num
=
7879 map
->num_members
- 1;
7880 to_state
= IMSM_T_STATE_DEGRADED
;
7882 to_state
= IMSM_T_STATE_NORMAL
;
7884 migrate(new_dev
, super
, to_state
, MIGR_GEN_MIGR
);
7885 if (u
->new_level
> -1)
7886 map
->raid_level
= u
->new_level
;
7887 migr_map
= get_imsm_map(new_dev
, MAP_1
);
7888 if ((u
->new_level
== 5) &&
7889 (migr_map
->raid_level
== 0)) {
7890 int ord
= map
->num_members
- 1;
7891 migr_map
->num_members
--;
7892 if (u
->new_disks
[0] < 0)
7893 ord
|= IMSM_ORD_REBUILD
;
7894 set_imsm_ord_tbl_ent(map
,
7895 map
->num_members
- 1,
7899 tofree
= (void **)dev
;
7901 /* update chunk size
7903 if (u
->new_chunksize
> 0)
7904 map
->blocks_per_strip
=
7905 __cpu_to_le16(u
->new_chunksize
* 2);
7909 if ((u
->new_level
!= 5) ||
7910 (migr_map
->raid_level
!= 0) ||
7911 (migr_map
->raid_level
== map
->raid_level
))
7914 if (u
->new_disks
[0] >= 0) {
7917 new_disk
= get_disk_super(super
,
7918 major(u
->new_disks
[0]),
7919 minor(u
->new_disks
[0]));
7920 dprintf("imsm: new disk for reshape is: %i:%i "
7921 "(%p, index = %i)\n",
7922 major(u
->new_disks
[0]),
7923 minor(u
->new_disks
[0]),
7924 new_disk
, new_disk
->index
);
7925 if (new_disk
== NULL
)
7926 goto error_disk_add
;
7928 new_disk
->index
= map
->num_members
- 1;
7929 /* slot to fill in autolayout
7931 new_disk
->raiddisk
= new_disk
->index
;
7932 new_disk
->disk
.status
|= CONFIGURED_DISK
;
7933 new_disk
->disk
.status
&= ~SPARE_DISK
;
7935 goto error_disk_add
;
7938 *tofree
= *space_list
;
7939 /* calculate new size
7941 imsm_set_array_size(new_dev
);
7948 *space_list
= tofree
;
7952 dprintf("Error: imsm: Cannot find disk.\n");
7956 static int apply_update_activate_spare(struct imsm_update_activate_spare
*u
,
7957 struct intel_super
*super
,
7958 struct active_array
*active_array
)
7960 struct imsm_super
*mpb
= super
->anchor
;
7961 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
7962 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7963 struct imsm_map
*migr_map
;
7964 struct active_array
*a
;
7965 struct imsm_disk
*disk
;
7972 int second_map_created
= 0;
7974 for (; u
; u
= u
->next
) {
7975 victim
= get_imsm_disk_idx(dev
, u
->slot
, MAP_X
);
7980 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7985 fprintf(stderr
, "error: imsm_activate_spare passed "
7986 "an unknown disk (index: %d)\n",
7991 /* count failures (excluding rebuilds and the victim)
7992 * to determine map[0] state
7995 for (i
= 0; i
< map
->num_members
; i
++) {
7998 disk
= get_imsm_disk(super
,
7999 get_imsm_disk_idx(dev
, i
, MAP_X
));
8000 if (!disk
|| is_failed(disk
))
8004 /* adding a pristine spare, assign a new index */
8005 if (dl
->index
< 0) {
8006 dl
->index
= super
->anchor
->num_disks
;
8007 super
->anchor
->num_disks
++;
8010 disk
->status
|= CONFIGURED_DISK
;
8011 disk
->status
&= ~SPARE_DISK
;
8014 to_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8015 if (!second_map_created
) {
8016 second_map_created
= 1;
8017 map
->map_state
= IMSM_T_STATE_DEGRADED
;
8018 migrate(dev
, super
, to_state
, MIGR_REBUILD
);
8020 map
->map_state
= to_state
;
8021 migr_map
= get_imsm_map(dev
, MAP_1
);
8022 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
8023 set_imsm_ord_tbl_ent(migr_map
, u
->slot
,
8024 dl
->index
| IMSM_ORD_REBUILD
);
8026 /* update the family_num to mark a new container
8027 * generation, being careful to record the existing
8028 * family_num in orig_family_num to clean up after
8029 * earlier mdadm versions that neglected to set it.
8031 if (mpb
->orig_family_num
== 0)
8032 mpb
->orig_family_num
= mpb
->family_num
;
8033 mpb
->family_num
+= super
->random
;
8035 /* count arrays using the victim in the metadata */
8037 for (a
= active_array
; a
; a
= a
->next
) {
8038 dev
= get_imsm_dev(super
, a
->info
.container_member
);
8039 map
= get_imsm_map(dev
, MAP_0
);
8041 if (get_imsm_disk_slot(map
, victim
) >= 0)
8045 /* delete the victim if it is no longer being
8051 /* We know that 'manager' isn't touching anything,
8052 * so it is safe to delete
8054 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
8055 if ((*dlp
)->index
== victim
)
8058 /* victim may be on the missing list */
8060 for (dlp
= &super
->missing
; *dlp
;
8061 dlp
= &(*dlp
)->next
)
8062 if ((*dlp
)->index
== victim
)
8064 imsm_delete(super
, dlp
, victim
);
8071 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
8072 struct intel_super
*super
,
8075 struct dl
*new_disk
;
8076 struct intel_dev
*id
;
8078 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
8079 int disk_count
= u
->old_raid_disks
;
8080 void **tofree
= NULL
;
8081 int devices_to_reshape
= 1;
8082 struct imsm_super
*mpb
= super
->anchor
;
8084 unsigned int dev_id
;
8086 dprintf("imsm: apply_reshape_container_disks_update()\n");
8088 /* enable spares to use in array */
8089 for (i
= 0; i
< delta_disks
; i
++) {
8090 new_disk
= get_disk_super(super
,
8091 major(u
->new_disks
[i
]),
8092 minor(u
->new_disks
[i
]));
8093 dprintf("imsm: new disk for reshape is: %i:%i "
8094 "(%p, index = %i)\n",
8095 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
8096 new_disk
, new_disk
->index
);
8097 if ((new_disk
== NULL
) ||
8098 ((new_disk
->index
>= 0) &&
8099 (new_disk
->index
< u
->old_raid_disks
)))
8100 goto update_reshape_exit
;
8101 new_disk
->index
= disk_count
++;
8102 /* slot to fill in autolayout
8104 new_disk
->raiddisk
= new_disk
->index
;
8105 new_disk
->disk
.status
|=
8107 new_disk
->disk
.status
&= ~SPARE_DISK
;
8110 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
8111 mpb
->num_raid_devs
);
8112 /* manage changes in volume
8114 for (dev_id
= 0; dev_id
< mpb
->num_raid_devs
; dev_id
++) {
8115 void **sp
= *space_list
;
8116 struct imsm_dev
*newdev
;
8117 struct imsm_map
*newmap
, *oldmap
;
8119 for (id
= super
->devlist
; id
; id
= id
->next
) {
8120 if (id
->index
== dev_id
)
8129 /* Copy the dev, but not (all of) the map */
8130 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
8131 oldmap
= get_imsm_map(id
->dev
, MAP_0
);
8132 newmap
= get_imsm_map(newdev
, MAP_0
);
8133 /* Copy the current map */
8134 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8135 /* update one device only
8137 if (devices_to_reshape
) {
8138 dprintf("imsm: modifying subdev: %i\n",
8140 devices_to_reshape
--;
8141 newdev
->vol
.migr_state
= 1;
8142 newdev
->vol
.curr_migr_unit
= 0;
8143 set_migr_type(newdev
, MIGR_GEN_MIGR
);
8144 newmap
->num_members
= u
->new_raid_disks
;
8145 for (i
= 0; i
< delta_disks
; i
++) {
8146 set_imsm_ord_tbl_ent(newmap
,
8147 u
->old_raid_disks
+ i
,
8148 u
->old_raid_disks
+ i
);
8150 /* New map is correct, now need to save old map
8152 newmap
= get_imsm_map(newdev
, MAP_1
);
8153 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8155 imsm_set_array_size(newdev
);
8158 sp
= (void **)id
->dev
;
8163 /* Clear migration record */
8164 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
8167 *space_list
= tofree
;
8170 update_reshape_exit
:
8175 static int apply_takeover_update(struct imsm_update_takeover
*u
,
8176 struct intel_super
*super
,
8179 struct imsm_dev
*dev
= NULL
;
8180 struct intel_dev
*dv
;
8181 struct imsm_dev
*dev_new
;
8182 struct imsm_map
*map
;
8186 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
8187 if (dv
->index
== (unsigned int)u
->subarray
) {
8195 map
= get_imsm_map(dev
, MAP_0
);
8197 if (u
->direction
== R10_TO_R0
) {
8198 /* Number of failed disks must be half of initial disk number */
8199 if (imsm_count_failed(super
, dev
, MAP_0
) !=
8200 (map
->num_members
/ 2))
8203 /* iterate through devices to mark removed disks as spare */
8204 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
8205 if (dm
->disk
.status
& FAILED_DISK
) {
8206 int idx
= dm
->index
;
8207 /* update indexes on the disk list */
8208 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
8209 the index values will end up being correct.... NB */
8210 for (du
= super
->disks
; du
; du
= du
->next
)
8211 if (du
->index
> idx
)
8213 /* mark as spare disk */
8218 map
->num_members
= map
->num_members
/ 2;
8219 map
->map_state
= IMSM_T_STATE_NORMAL
;
8220 map
->num_domains
= 1;
8221 map
->raid_level
= 0;
8222 map
->failed_disk_num
= -1;
8225 if (u
->direction
== R0_TO_R10
) {
8227 /* update slots in current disk list */
8228 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
8232 /* create new *missing* disks */
8233 for (i
= 0; i
< map
->num_members
; i
++) {
8234 space
= *space_list
;
8237 *space_list
= *space
;
8239 memcpy(du
, super
->disks
, sizeof(*du
));
8243 du
->index
= (i
* 2) + 1;
8244 sprintf((char *)du
->disk
.serial
,
8245 " MISSING_%d", du
->index
);
8246 sprintf((char *)du
->serial
,
8247 "MISSING_%d", du
->index
);
8248 du
->next
= super
->missing
;
8249 super
->missing
= du
;
8251 /* create new dev and map */
8252 space
= *space_list
;
8255 *space_list
= *space
;
8256 dev_new
= (void *)space
;
8257 memcpy(dev_new
, dev
, sizeof(*dev
));
8258 /* update new map */
8259 map
= get_imsm_map(dev_new
, MAP_0
);
8260 map
->num_members
= map
->num_members
* 2;
8261 map
->map_state
= IMSM_T_STATE_DEGRADED
;
8262 map
->num_domains
= 2;
8263 map
->raid_level
= 1;
8264 /* replace dev<->dev_new */
8267 /* update disk order table */
8268 for (du
= super
->disks
; du
; du
= du
->next
)
8270 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
8271 for (du
= super
->missing
; du
; du
= du
->next
)
8272 if (du
->index
>= 0) {
8273 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
8274 mark_missing(dv
->dev
, &du
->disk
, du
->index
);
8280 static void imsm_process_update(struct supertype
*st
,
8281 struct metadata_update
*update
)
8284 * crack open the metadata_update envelope to find the update record
8285 * update can be one of:
8286 * update_reshape_container_disks - all the arrays in the container
8287 * are being reshaped to have more devices. We need to mark
8288 * the arrays for general migration and convert selected spares
8289 * into active devices.
8290 * update_activate_spare - a spare device has replaced a failed
8291 * device in an array, update the disk_ord_tbl. If this disk is
8292 * present in all member arrays then also clear the SPARE_DISK
8294 * update_create_array
8296 * update_rename_array
8297 * update_add_remove_disk
8299 struct intel_super
*super
= st
->sb
;
8300 struct imsm_super
*mpb
;
8301 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
8303 /* update requires a larger buf but the allocation failed */
8304 if (super
->next_len
&& !super
->next_buf
) {
8305 super
->next_len
= 0;
8309 if (super
->next_buf
) {
8310 memcpy(super
->next_buf
, super
->buf
, super
->len
);
8312 super
->len
= super
->next_len
;
8313 super
->buf
= super
->next_buf
;
8315 super
->next_len
= 0;
8316 super
->next_buf
= NULL
;
8319 mpb
= super
->anchor
;
8322 case update_general_migration_checkpoint
: {
8323 struct intel_dev
*id
;
8324 struct imsm_update_general_migration_checkpoint
*u
=
8325 (void *)update
->buf
;
8327 dprintf("imsm: process_update() "
8328 "for update_general_migration_checkpoint called\n");
8330 /* find device under general migration */
8331 for (id
= super
->devlist
; id
; id
= id
->next
) {
8332 if (is_gen_migration(id
->dev
)) {
8333 id
->dev
->vol
.curr_migr_unit
=
8334 __cpu_to_le32(u
->curr_migr_unit
);
8335 super
->updates_pending
++;
8340 case update_takeover
: {
8341 struct imsm_update_takeover
*u
= (void *)update
->buf
;
8342 if (apply_takeover_update(u
, super
, &update
->space_list
)) {
8343 imsm_update_version_info(super
);
8344 super
->updates_pending
++;
8349 case update_reshape_container_disks
: {
8350 struct imsm_update_reshape
*u
= (void *)update
->buf
;
8351 if (apply_reshape_container_disks_update(
8352 u
, super
, &update
->space_list
))
8353 super
->updates_pending
++;
8356 case update_reshape_migration
: {
8357 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
8358 if (apply_reshape_migration_update(
8359 u
, super
, &update
->space_list
))
8360 super
->updates_pending
++;
8363 case update_activate_spare
: {
8364 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
8365 if (apply_update_activate_spare(u
, super
, st
->arrays
))
8366 super
->updates_pending
++;
8369 case update_create_array
: {
8370 /* someone wants to create a new array, we need to be aware of
8371 * a few races/collisions:
8372 * 1/ 'Create' called by two separate instances of mdadm
8373 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
8374 * devices that have since been assimilated via
8376 * In the event this update can not be carried out mdadm will
8377 * (FIX ME) notice that its update did not take hold.
8379 struct imsm_update_create_array
*u
= (void *) update
->buf
;
8380 struct intel_dev
*dv
;
8381 struct imsm_dev
*dev
;
8382 struct imsm_map
*map
, *new_map
;
8383 unsigned long long start
, end
;
8384 unsigned long long new_start
, new_end
;
8386 struct disk_info
*inf
;
8389 /* handle racing creates: first come first serve */
8390 if (u
->dev_idx
< mpb
->num_raid_devs
) {
8391 dprintf("%s: subarray %d already defined\n",
8392 __func__
, u
->dev_idx
);
8396 /* check update is next in sequence */
8397 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
8398 dprintf("%s: can not create array %d expected index %d\n",
8399 __func__
, u
->dev_idx
, mpb
->num_raid_devs
);
8403 new_map
= get_imsm_map(&u
->dev
, MAP_0
);
8404 new_start
= pba_of_lba0(new_map
);
8405 new_end
= new_start
+ blocks_per_member(new_map
);
8406 inf
= get_disk_info(u
);
8408 /* handle activate_spare versus create race:
8409 * check to make sure that overlapping arrays do not include
8412 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8413 dev
= get_imsm_dev(super
, i
);
8414 map
= get_imsm_map(dev
, MAP_0
);
8415 start
= pba_of_lba0(map
);
8416 end
= start
+ blocks_per_member(map
);
8417 if ((new_start
>= start
&& new_start
<= end
) ||
8418 (start
>= new_start
&& start
<= new_end
))
8423 if (disks_overlap(super
, i
, u
)) {
8424 dprintf("%s: arrays overlap\n", __func__
);
8429 /* check that prepare update was successful */
8430 if (!update
->space
) {
8431 dprintf("%s: prepare update failed\n", __func__
);
8435 /* check that all disks are still active before committing
8436 * changes. FIXME: could we instead handle this by creating a
8437 * degraded array? That's probably not what the user expects,
8438 * so better to drop this update on the floor.
8440 for (i
= 0; i
< new_map
->num_members
; i
++) {
8441 dl
= serial_to_dl(inf
[i
].serial
, super
);
8443 dprintf("%s: disk disappeared\n", __func__
);
8448 super
->updates_pending
++;
8450 /* convert spares to members and fixup ord_tbl */
8451 for (i
= 0; i
< new_map
->num_members
; i
++) {
8452 dl
= serial_to_dl(inf
[i
].serial
, super
);
8453 if (dl
->index
== -1) {
8454 dl
->index
= mpb
->num_disks
;
8456 dl
->disk
.status
|= CONFIGURED_DISK
;
8457 dl
->disk
.status
&= ~SPARE_DISK
;
8459 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
8464 update
->space
= NULL
;
8465 imsm_copy_dev(dev
, &u
->dev
);
8466 dv
->index
= u
->dev_idx
;
8467 dv
->next
= super
->devlist
;
8468 super
->devlist
= dv
;
8469 mpb
->num_raid_devs
++;
8471 imsm_update_version_info(super
);
8474 /* mdmon knows how to release update->space, but not
8475 * ((struct intel_dev *) update->space)->dev
8477 if (update
->space
) {
8483 case update_kill_array
: {
8484 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
8485 int victim
= u
->dev_idx
;
8486 struct active_array
*a
;
8487 struct intel_dev
**dp
;
8488 struct imsm_dev
*dev
;
8490 /* sanity check that we are not affecting the uuid of
8491 * active arrays, or deleting an active array
8493 * FIXME when immutable ids are available, but note that
8494 * we'll also need to fixup the invalidated/active
8495 * subarray indexes in mdstat
8497 for (a
= st
->arrays
; a
; a
= a
->next
)
8498 if (a
->info
.container_member
>= victim
)
8500 /* by definition if mdmon is running at least one array
8501 * is active in the container, so checking
8502 * mpb->num_raid_devs is just extra paranoia
8504 dev
= get_imsm_dev(super
, victim
);
8505 if (a
|| !dev
|| mpb
->num_raid_devs
== 1) {
8506 dprintf("failed to delete subarray-%d\n", victim
);
8510 for (dp
= &super
->devlist
; *dp
;)
8511 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
8514 if ((*dp
)->index
> (unsigned)victim
)
8518 mpb
->num_raid_devs
--;
8519 super
->updates_pending
++;
8522 case update_rename_array
: {
8523 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
8524 char name
[MAX_RAID_SERIAL_LEN
+1];
8525 int target
= u
->dev_idx
;
8526 struct active_array
*a
;
8527 struct imsm_dev
*dev
;
8529 /* sanity check that we are not affecting the uuid of
8532 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
8533 name
[MAX_RAID_SERIAL_LEN
] = '\0';
8534 for (a
= st
->arrays
; a
; a
= a
->next
)
8535 if (a
->info
.container_member
== target
)
8537 dev
= get_imsm_dev(super
, u
->dev_idx
);
8538 if (a
|| !dev
|| !check_name(super
, name
, 1)) {
8539 dprintf("failed to rename subarray-%d\n", target
);
8543 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
8544 super
->updates_pending
++;
8547 case update_add_remove_disk
: {
8548 /* we may be able to repair some arrays if disks are
8549 * being added, check teh status of add_remove_disk
8550 * if discs has been added.
8552 if (add_remove_disk_update(super
)) {
8553 struct active_array
*a
;
8555 super
->updates_pending
++;
8556 for (a
= st
->arrays
; a
; a
= a
->next
)
8557 a
->check_degraded
= 1;
8562 fprintf(stderr
, "error: unsuported process update type:"
8563 "(type: %d)\n", type
);
8567 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
);
8569 static void imsm_prepare_update(struct supertype
*st
,
8570 struct metadata_update
*update
)
8573 * Allocate space to hold new disk entries, raid-device entries or a new
8574 * mpb if necessary. The manager synchronously waits for updates to
8575 * complete in the monitor, so new mpb buffers allocated here can be
8576 * integrated by the monitor thread without worrying about live pointers
8577 * in the manager thread.
8579 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
8580 struct intel_super
*super
= st
->sb
;
8581 struct imsm_super
*mpb
= super
->anchor
;
8586 case update_general_migration_checkpoint
:
8587 dprintf("imsm: prepare_update() "
8588 "for update_general_migration_checkpoint called\n");
8590 case update_takeover
: {
8591 struct imsm_update_takeover
*u
= (void *)update
->buf
;
8592 if (u
->direction
== R0_TO_R10
) {
8593 void **tail
= (void **)&update
->space_list
;
8594 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subarray
);
8595 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8596 int num_members
= map
->num_members
;
8600 /* allocate memory for added disks */
8601 for (i
= 0; i
< num_members
; i
++) {
8602 size
= sizeof(struct dl
);
8603 space
= malloc(size
);
8612 /* allocate memory for new device */
8613 size
= sizeof_imsm_dev(super
->devlist
->dev
, 0) +
8614 (num_members
* sizeof(__u32
));
8615 space
= malloc(size
);
8624 len
= disks_to_mpb_size(num_members
* 2);
8626 /* if allocation didn't success, free buffer */
8627 while (update
->space_list
) {
8628 void **sp
= update
->space_list
;
8629 update
->space_list
= *sp
;
8637 case update_reshape_container_disks
: {
8638 /* Every raid device in the container is about to
8639 * gain some more devices, and we will enter a
8641 * So each 'imsm_map' will be bigger, and the imsm_vol
8642 * will now hold 2 of them.
8643 * Thus we need new 'struct imsm_dev' allocations sized
8644 * as sizeof_imsm_dev but with more devices in both maps.
8646 struct imsm_update_reshape
*u
= (void *)update
->buf
;
8647 struct intel_dev
*dl
;
8648 void **space_tail
= (void**)&update
->space_list
;
8650 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
8652 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
8653 int size
= sizeof_imsm_dev(dl
->dev
, 1);
8655 if (u
->new_raid_disks
> u
->old_raid_disks
)
8656 size
+= sizeof(__u32
)*2*
8657 (u
->new_raid_disks
- u
->old_raid_disks
);
8666 len
= disks_to_mpb_size(u
->new_raid_disks
);
8667 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
8670 case update_reshape_migration
: {
8671 /* for migration level 0->5 we need to add disks
8672 * so the same as for container operation we will copy
8673 * device to the bigger location.
8674 * in memory prepared device and new disk area are prepared
8675 * for usage in process update
8677 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
8678 struct intel_dev
*id
;
8679 void **space_tail
= (void **)&update
->space_list
;
8682 int current_level
= -1;
8684 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
8686 /* add space for bigger array in update
8688 for (id
= super
->devlist
; id
; id
= id
->next
) {
8689 if (id
->index
== (unsigned)u
->subdev
) {
8690 size
= sizeof_imsm_dev(id
->dev
, 1);
8691 if (u
->new_raid_disks
> u
->old_raid_disks
)
8692 size
+= sizeof(__u32
)*2*
8693 (u
->new_raid_disks
- u
->old_raid_disks
);
8703 if (update
->space_list
== NULL
)
8706 /* add space for disk in update
8708 size
= sizeof(struct dl
);
8711 free(update
->space_list
);
8712 update
->space_list
= NULL
;
8719 /* add spare device to update
8721 for (id
= super
->devlist
; id
; id
= id
->next
)
8722 if (id
->index
== (unsigned)u
->subdev
) {
8723 struct imsm_dev
*dev
;
8724 struct imsm_map
*map
;
8726 dev
= get_imsm_dev(super
, u
->subdev
);
8727 map
= get_imsm_map(dev
, MAP_0
);
8728 current_level
= map
->raid_level
;
8731 if ((u
->new_level
== 5) && (u
->new_level
!= current_level
)) {
8732 struct mdinfo
*spares
;
8734 spares
= get_spares_for_grow(st
);
8742 makedev(dev
->disk
.major
,
8744 dl
= get_disk_super(super
,
8747 dl
->index
= u
->old_raid_disks
;
8753 len
= disks_to_mpb_size(u
->new_raid_disks
);
8754 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
8757 case update_create_array
: {
8758 struct imsm_update_create_array
*u
= (void *) update
->buf
;
8759 struct intel_dev
*dv
;
8760 struct imsm_dev
*dev
= &u
->dev
;
8761 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8763 struct disk_info
*inf
;
8767 inf
= get_disk_info(u
);
8768 len
= sizeof_imsm_dev(dev
, 1);
8769 /* allocate a new super->devlist entry */
8770 dv
= malloc(sizeof(*dv
));
8772 dv
->dev
= malloc(len
);
8777 update
->space
= NULL
;
8781 /* count how many spares will be converted to members */
8782 for (i
= 0; i
< map
->num_members
; i
++) {
8783 dl
= serial_to_dl(inf
[i
].serial
, super
);
8785 /* hmm maybe it failed?, nothing we can do about
8790 if (count_memberships(dl
, super
) == 0)
8793 len
+= activate
* sizeof(struct imsm_disk
);
8800 /* check if we need a larger metadata buffer */
8801 if (super
->next_buf
)
8802 buf_len
= super
->next_len
;
8804 buf_len
= super
->len
;
8806 if (__le32_to_cpu(mpb
->mpb_size
) + len
> buf_len
) {
8807 /* ok we need a larger buf than what is currently allocated
8808 * if this allocation fails process_update will notice that
8809 * ->next_len is set and ->next_buf is NULL
8811 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + len
, 512);
8812 if (super
->next_buf
)
8813 free(super
->next_buf
);
8815 super
->next_len
= buf_len
;
8816 if (posix_memalign(&super
->next_buf
, 512, buf_len
) == 0)
8817 memset(super
->next_buf
, 0, buf_len
);
8819 super
->next_buf
= NULL
;
8823 /* must be called while manager is quiesced */
8824 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
8826 struct imsm_super
*mpb
= super
->anchor
;
8828 struct imsm_dev
*dev
;
8829 struct imsm_map
*map
;
8830 int i
, j
, num_members
;
8833 dprintf("%s: deleting device[%d] from imsm_super\n",
8836 /* shift all indexes down one */
8837 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
8838 if (iter
->index
> (int)index
)
8840 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
8841 if (iter
->index
> (int)index
)
8844 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8845 dev
= get_imsm_dev(super
, i
);
8846 map
= get_imsm_map(dev
, MAP_0
);
8847 num_members
= map
->num_members
;
8848 for (j
= 0; j
< num_members
; j
++) {
8849 /* update ord entries being careful not to propagate
8850 * ord-flags to the first map
8852 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_X
);
8854 if (ord_to_idx(ord
) <= index
)
8857 map
= get_imsm_map(dev
, MAP_0
);
8858 set_imsm_ord_tbl_ent(map
, j
, ord_to_idx(ord
- 1));
8859 map
= get_imsm_map(dev
, MAP_1
);
8861 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
8866 super
->updates_pending
++;
8868 struct dl
*dl
= *dlp
;
8870 *dlp
= (*dlp
)->next
;
8871 __free_imsm_disk(dl
);
8874 #endif /* MDASSEMBLE */
8876 static void close_targets(int *targets
, int new_disks
)
8883 for (i
= 0; i
< new_disks
; i
++) {
8884 if (targets
[i
] >= 0) {
8891 static int imsm_get_allowed_degradation(int level
, int raid_disks
,
8892 struct intel_super
*super
,
8893 struct imsm_dev
*dev
)
8899 struct imsm_map
*map
;
8902 ret_val
= raid_disks
/2;
8903 /* check map if all disks pairs not failed
8906 map
= get_imsm_map(dev
, MAP_0
);
8907 for (i
= 0; i
< ret_val
; i
++) {
8908 int degradation
= 0;
8909 if (get_imsm_disk(super
, i
) == NULL
)
8911 if (get_imsm_disk(super
, i
+ 1) == NULL
)
8913 if (degradation
== 2)
8916 map
= get_imsm_map(dev
, MAP_1
);
8917 /* if there is no second map
8918 * result can be returned
8922 /* check degradation in second map
8924 for (i
= 0; i
< ret_val
; i
++) {
8925 int degradation
= 0;
8926 if (get_imsm_disk(super
, i
) == NULL
)
8928 if (get_imsm_disk(super
, i
+ 1) == NULL
)
8930 if (degradation
== 2)
8945 /*******************************************************************************
8946 * Function: open_backup_targets
8947 * Description: Function opens file descriptors for all devices given in
8950 * info : general array info
8951 * raid_disks : number of disks
8952 * raid_fds : table of device's file descriptors
8953 * super : intel super for raid10 degradation check
8954 * dev : intel device for raid10 degradation check
8958 ******************************************************************************/
8959 int open_backup_targets(struct mdinfo
*info
, int raid_disks
, int *raid_fds
,
8960 struct intel_super
*super
, struct imsm_dev
*dev
)
8966 for (i
= 0; i
< raid_disks
; i
++)
8969 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
8972 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
)) {
8973 dprintf("disk is faulty!!\n");
8977 if ((sd
->disk
.raid_disk
>= raid_disks
) ||
8978 (sd
->disk
.raid_disk
< 0))
8981 dn
= map_dev(sd
->disk
.major
,
8983 raid_fds
[sd
->disk
.raid_disk
] = dev_open(dn
, O_RDWR
);
8984 if (raid_fds
[sd
->disk
.raid_disk
] < 0) {
8985 fprintf(stderr
, "cannot open component\n");
8990 /* check if maximum array degradation level is not exceeded
8992 if ((raid_disks
- opened
) >
8993 imsm_get_allowed_degradation(info
->new_level
,
8996 fprintf(stderr
, "Not enough disks can be opened.\n");
8997 close_targets(raid_fds
, raid_disks
);
9004 /*******************************************************************************
9005 * Function: init_migr_record_imsm
9006 * Description: Function inits imsm migration record
9008 * super : imsm internal array info
9009 * dev : device under migration
9010 * info : general array info to find the smallest device
9013 ******************************************************************************/
9014 void init_migr_record_imsm(struct supertype
*st
, struct imsm_dev
*dev
,
9015 struct mdinfo
*info
)
9017 struct intel_super
*super
= st
->sb
;
9018 struct migr_record
*migr_rec
= super
->migr_rec
;
9020 unsigned long long dsize
, dev_sectors
;
9021 long long unsigned min_dev_sectors
= -1LLU;
9025 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
9026 struct imsm_map
*map_src
= get_imsm_map(dev
, MAP_1
);
9027 unsigned long long num_migr_units
;
9028 unsigned long long array_blocks
;
9030 memset(migr_rec
, 0, sizeof(struct migr_record
));
9031 migr_rec
->family_num
= __cpu_to_le32(super
->anchor
->family_num
);
9033 /* only ascending reshape supported now */
9034 migr_rec
->ascending_migr
= __cpu_to_le32(1);
9036 migr_rec
->dest_depth_per_unit
= GEN_MIGR_AREA_SIZE
/
9037 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
9038 migr_rec
->dest_depth_per_unit
*=
9039 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
9040 new_data_disks
= imsm_num_data_members(dev
, MAP_0
);
9041 migr_rec
->blocks_per_unit
=
9042 __cpu_to_le32(migr_rec
->dest_depth_per_unit
* new_data_disks
);
9043 migr_rec
->dest_depth_per_unit
=
9044 __cpu_to_le32(migr_rec
->dest_depth_per_unit
);
9045 array_blocks
= info
->component_size
* new_data_disks
;
9047 array_blocks
/ __le32_to_cpu(migr_rec
->blocks_per_unit
);
9049 if (array_blocks
% __le32_to_cpu(migr_rec
->blocks_per_unit
))
9051 migr_rec
->num_migr_units
= __cpu_to_le32(num_migr_units
);
9053 migr_rec
->post_migr_vol_cap
= dev
->size_low
;
9054 migr_rec
->post_migr_vol_cap_hi
= dev
->size_high
;
9057 /* Find the smallest dev */
9058 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
9059 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
9060 fd
= dev_open(nm
, O_RDONLY
);
9063 get_dev_size(fd
, NULL
, &dsize
);
9064 dev_sectors
= dsize
/ 512;
9065 if (dev_sectors
< min_dev_sectors
)
9066 min_dev_sectors
= dev_sectors
;
9069 migr_rec
->ckpt_area_pba
= __cpu_to_le32(min_dev_sectors
-
9070 RAID_DISK_RESERVED_BLOCKS_IMSM_HI
);
9072 write_imsm_migr_rec(st
);
9077 /*******************************************************************************
9078 * Function: save_backup_imsm
9079 * Description: Function saves critical data stripes to Migration Copy Area
9080 * and updates the current migration unit status.
9081 * Use restore_stripes() to form a destination stripe,
9082 * and to write it to the Copy Area.
9084 * st : supertype information
9085 * dev : imsm device that backup is saved for
9086 * info : general array info
9087 * buf : input buffer
9088 * length : length of data to backup (blocks_per_unit)
9092 ******************************************************************************/
9093 int save_backup_imsm(struct supertype
*st
,
9094 struct imsm_dev
*dev
,
9095 struct mdinfo
*info
,
9100 struct intel_super
*super
= st
->sb
;
9101 unsigned long long *target_offsets
= NULL
;
9102 int *targets
= NULL
;
9104 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
9105 int new_disks
= map_dest
->num_members
;
9106 int dest_layout
= 0;
9108 unsigned long long start
;
9109 int data_disks
= imsm_num_data_members(dev
, MAP_0
);
9111 targets
= malloc(new_disks
* sizeof(int));
9115 for (i
= 0; i
< new_disks
; i
++)
9118 target_offsets
= malloc(new_disks
* sizeof(unsigned long long));
9119 if (!target_offsets
)
9122 start
= info
->reshape_progress
* 512;
9123 for (i
= 0; i
< new_disks
; i
++) {
9124 target_offsets
[i
] = (unsigned long long)
9125 __le32_to_cpu(super
->migr_rec
->ckpt_area_pba
) * 512;
9126 /* move back copy area adderss, it will be moved forward
9127 * in restore_stripes() using start input variable
9129 target_offsets
[i
] -= start
/data_disks
;
9132 if (open_backup_targets(info
, new_disks
, targets
,
9136 dest_layout
= imsm_level_to_layout(map_dest
->raid_level
);
9137 dest_chunk
= __le16_to_cpu(map_dest
->blocks_per_strip
) * 512;
9139 if (restore_stripes(targets
, /* list of dest devices */
9140 target_offsets
, /* migration record offsets */
9143 map_dest
->raid_level
,
9145 -1, /* source backup file descriptor */
9146 0, /* input buf offset
9147 * always 0 buf is already offseted */
9151 fprintf(stderr
, Name
": Error restoring stripes\n");
9159 close_targets(targets
, new_disks
);
9162 free(target_offsets
);
9167 /*******************************************************************************
9168 * Function: save_checkpoint_imsm
9169 * Description: Function called for current unit status update
9170 * in the migration record. It writes it to disk.
9172 * super : imsm internal array info
9173 * info : general array info
9177 * 2: failure, means no valid migration record
9178 * / no general migration in progress /
9179 ******************************************************************************/
9180 int save_checkpoint_imsm(struct supertype
*st
, struct mdinfo
*info
, int state
)
9182 struct intel_super
*super
= st
->sb
;
9183 unsigned long long blocks_per_unit
;
9184 unsigned long long curr_migr_unit
;
9186 if (load_imsm_migr_rec(super
, info
) != 0) {
9187 dprintf("imsm: ERROR: Cannot read migration record "
9188 "for checkpoint save.\n");
9192 blocks_per_unit
= __le32_to_cpu(super
->migr_rec
->blocks_per_unit
);
9193 if (blocks_per_unit
== 0) {
9194 dprintf("imsm: no migration in progress.\n");
9197 curr_migr_unit
= info
->reshape_progress
/ blocks_per_unit
;
9198 /* check if array is alligned to copy area
9199 * if it is not alligned, add one to current migration unit value
9200 * this can happend on array reshape finish only
9202 if (info
->reshape_progress
% blocks_per_unit
)
9205 super
->migr_rec
->curr_migr_unit
=
9206 __cpu_to_le32(curr_migr_unit
);
9207 super
->migr_rec
->rec_status
= __cpu_to_le32(state
);
9208 super
->migr_rec
->dest_1st_member_lba
=
9209 __cpu_to_le32(curr_migr_unit
*
9210 __le32_to_cpu(super
->migr_rec
->dest_depth_per_unit
));
9211 if (write_imsm_migr_rec(st
) < 0) {
9212 dprintf("imsm: Cannot write migration record "
9213 "outside backup area\n");
9220 /*******************************************************************************
9221 * Function: recover_backup_imsm
9222 * Description: Function recovers critical data from the Migration Copy Area
9223 * while assembling an array.
9225 * super : imsm internal array info
9226 * info : general array info
9228 * 0 : success (or there is no data to recover)
9230 ******************************************************************************/
9231 int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
)
9233 struct intel_super
*super
= st
->sb
;
9234 struct migr_record
*migr_rec
= super
->migr_rec
;
9235 struct imsm_map
*map_dest
= NULL
;
9236 struct intel_dev
*id
= NULL
;
9237 unsigned long long read_offset
;
9238 unsigned long long write_offset
;
9240 int *targets
= NULL
;
9241 int new_disks
, i
, err
;
9244 unsigned long curr_migr_unit
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
9245 unsigned long num_migr_units
= __le32_to_cpu(migr_rec
->num_migr_units
);
9247 int skipped_disks
= 0;
9249 err
= sysfs_get_str(info
, NULL
, "array_state", (char *)buffer
, 20);
9253 /* recover data only during assemblation */
9254 if (strncmp(buffer
, "inactive", 8) != 0)
9256 /* no data to recover */
9257 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
9259 if (curr_migr_unit
>= num_migr_units
)
9262 /* find device during reshape */
9263 for (id
= super
->devlist
; id
; id
= id
->next
)
9264 if (is_gen_migration(id
->dev
))
9269 map_dest
= get_imsm_map(id
->dev
, MAP_0
);
9270 new_disks
= map_dest
->num_members
;
9272 read_offset
= (unsigned long long)
9273 __le32_to_cpu(migr_rec
->ckpt_area_pba
) * 512;
9275 write_offset
= ((unsigned long long)
9276 __le32_to_cpu(migr_rec
->dest_1st_member_lba
) +
9277 pba_of_lba0(map_dest
)) * 512;
9279 unit_len
= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
9280 if (posix_memalign((void **)&buf
, 512, unit_len
) != 0)
9282 targets
= malloc(new_disks
* sizeof(int));
9286 if (open_backup_targets(info
, new_disks
, targets
, super
, id
->dev
)) {
9288 Name
": Cannot open some devices belonging to array.\n");
9292 for (i
= 0; i
< new_disks
; i
++) {
9293 if (targets
[i
] < 0) {
9297 if (lseek64(targets
[i
], read_offset
, SEEK_SET
) < 0) {
9299 Name
": Cannot seek to block: %s\n",
9304 if ((unsigned)read(targets
[i
], buf
, unit_len
) != unit_len
) {
9306 Name
": Cannot read copy area block: %s\n",
9311 if (lseek64(targets
[i
], write_offset
, SEEK_SET
) < 0) {
9313 Name
": Cannot seek to block: %s\n",
9318 if ((unsigned)write(targets
[i
], buf
, unit_len
) != unit_len
) {
9320 Name
": Cannot restore block: %s\n",
9327 if (skipped_disks
> imsm_get_allowed_degradation(info
->new_level
,
9332 Name
": Cannot restore data from backup."
9333 " Too many failed disks\n");
9337 if (save_checkpoint_imsm(st
, info
, UNIT_SRC_NORMAL
)) {
9338 /* ignore error == 2, this can mean end of reshape here
9340 dprintf("imsm: Cannot write checkpoint to "
9341 "migration record (UNIT_SRC_NORMAL) during restart\n");
9347 for (i
= 0; i
< new_disks
; i
++)
9356 static char disk_by_path
[] = "/dev/disk/by-path/";
9358 static const char *imsm_get_disk_controller_domain(const char *path
)
9360 char disk_path
[PATH_MAX
];
9364 strncpy(disk_path
, disk_by_path
, PATH_MAX
- 1);
9365 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
9366 if (stat(disk_path
, &st
) == 0) {
9367 struct sys_dev
* hba
;
9370 path
= devt_to_devpath(st
.st_rdev
);
9373 hba
= find_disk_attached_hba(-1, path
);
9374 if (hba
&& hba
->type
== SYS_DEV_SAS
)
9376 else if (hba
&& hba
->type
== SYS_DEV_SATA
)
9380 dprintf("path: %s hba: %s attached: %s\n",
9381 path
, (hba
) ? hba
->path
: "NULL", drv
);
9389 static int imsm_find_array_minor_by_subdev(int subdev
, int container
, int *minor
)
9391 char subdev_name
[20];
9392 struct mdstat_ent
*mdstat
;
9394 sprintf(subdev_name
, "%d", subdev
);
9395 mdstat
= mdstat_by_subdev(subdev_name
, container
);
9399 *minor
= mdstat
->devnum
;
9400 free_mdstat(mdstat
);
9404 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
9405 struct geo_params
*geo
,
9406 int *old_raid_disks
)
9408 /* currently we only support increasing the number of devices
9409 * for a container. This increases the number of device for each
9410 * member array. They must all be RAID0 or RAID5.
9413 struct mdinfo
*info
, *member
;
9414 int devices_that_can_grow
= 0;
9416 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): "
9417 "st->devnum = (%i)\n",
9420 if (geo
->size
!= -1 ||
9421 geo
->level
!= UnSet
||
9422 geo
->layout
!= UnSet
||
9423 geo
->chunksize
!= 0 ||
9424 geo
->raid_disks
== UnSet
) {
9425 dprintf("imsm: Container operation is allowed for "
9426 "raid disks number change only.\n");
9430 info
= container_content_imsm(st
, NULL
);
9431 for (member
= info
; member
; member
= member
->next
) {
9435 dprintf("imsm: checking device_num: %i\n",
9436 member
->container_member
);
9438 if (geo
->raid_disks
<= member
->array
.raid_disks
) {
9439 /* we work on container for Online Capacity Expansion
9440 * only so raid_disks has to grow
9442 dprintf("imsm: for container operation raid disks "
9443 "increase is required\n");
9447 if ((info
->array
.level
!= 0) &&
9448 (info
->array
.level
!= 5)) {
9449 /* we cannot use this container with other raid level
9451 dprintf("imsm: for container operation wrong"
9452 " raid level (%i) detected\n",
9456 /* check for platform support
9457 * for this raid level configuration
9459 struct intel_super
*super
= st
->sb
;
9460 if (!is_raid_level_supported(super
->orom
,
9461 member
->array
.level
,
9463 dprintf("platform does not support raid%d with"
9467 geo
->raid_disks
> 1 ? "s" : "");
9470 /* check if component size is aligned to chunk size
9472 if (info
->component_size
%
9473 (info
->array
.chunk_size
/512)) {
9474 dprintf("Component size is not aligned to "
9480 if (*old_raid_disks
&&
9481 info
->array
.raid_disks
!= *old_raid_disks
)
9483 *old_raid_disks
= info
->array
.raid_disks
;
9485 /* All raid5 and raid0 volumes in container
9486 * have to be ready for Online Capacity Expansion
9487 * so they need to be assembled. We have already
9488 * checked that no recovery etc is happening.
9490 result
= imsm_find_array_minor_by_subdev(member
->container_member
,
9494 dprintf("imsm: cannot find array\n");
9497 devices_that_can_grow
++;
9500 if (!member
&& devices_that_can_grow
)
9504 dprintf("\tContainer operation allowed\n");
9506 dprintf("\tError: %i\n", ret_val
);
9511 /* Function: get_spares_for_grow
9512 * Description: Allocates memory and creates list of spare devices
9513 * avaliable in container. Checks if spare drive size is acceptable.
9514 * Parameters: Pointer to the supertype structure
9515 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
9518 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
9520 unsigned long long min_size
= min_acceptable_spare_size_imsm(st
);
9521 return container_choose_spares(st
, min_size
, NULL
, NULL
, NULL
, 0);
9524 /******************************************************************************
9525 * function: imsm_create_metadata_update_for_reshape
9526 * Function creates update for whole IMSM container.
9528 ******************************************************************************/
9529 static int imsm_create_metadata_update_for_reshape(
9530 struct supertype
*st
,
9531 struct geo_params
*geo
,
9533 struct imsm_update_reshape
**updatep
)
9535 struct intel_super
*super
= st
->sb
;
9536 struct imsm_super
*mpb
= super
->anchor
;
9537 int update_memory_size
= 0;
9538 struct imsm_update_reshape
*u
= NULL
;
9539 struct mdinfo
*spares
= NULL
;
9541 int delta_disks
= 0;
9544 dprintf("imsm_update_metadata_for_reshape(enter) raid_disks = %i\n",
9547 delta_disks
= geo
->raid_disks
- old_raid_disks
;
9549 /* size of all update data without anchor */
9550 update_memory_size
= sizeof(struct imsm_update_reshape
);
9552 /* now add space for spare disks that we need to add. */
9553 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
9555 u
= calloc(1, update_memory_size
);
9558 "cannot get memory for imsm_update_reshape update\n");
9561 u
->type
= update_reshape_container_disks
;
9562 u
->old_raid_disks
= old_raid_disks
;
9563 u
->new_raid_disks
= geo
->raid_disks
;
9565 /* now get spare disks list
9567 spares
= get_spares_for_grow(st
);
9570 || delta_disks
> spares
->array
.spare_disks
) {
9571 fprintf(stderr
, Name
": imsm: ERROR: Cannot get spare devices "
9572 "for %s.\n", geo
->dev_name
);
9577 /* we have got spares
9578 * update disk list in imsm_disk list table in anchor
9580 dprintf("imsm: %i spares are available.\n\n",
9581 spares
->array
.spare_disks
);
9584 for (i
= 0; i
< delta_disks
; i
++) {
9589 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
9591 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
9592 dl
->index
= mpb
->num_disks
;
9602 dprintf("imsm: reshape update preparation :");
9603 if (i
== delta_disks
) {
9606 return update_memory_size
;
9609 dprintf(" Error\n");
9614 /******************************************************************************
9615 * function: imsm_create_metadata_update_for_migration()
9616 * Creates update for IMSM array.
9618 ******************************************************************************/
9619 static int imsm_create_metadata_update_for_migration(
9620 struct supertype
*st
,
9621 struct geo_params
*geo
,
9622 struct imsm_update_reshape_migration
**updatep
)
9624 struct intel_super
*super
= st
->sb
;
9625 int update_memory_size
= 0;
9626 struct imsm_update_reshape_migration
*u
= NULL
;
9627 struct imsm_dev
*dev
;
9628 int previous_level
= -1;
9630 dprintf("imsm_create_metadata_update_for_migration(enter)"
9631 " New Level = %i\n", geo
->level
);
9633 /* size of all update data without anchor */
9634 update_memory_size
= sizeof(struct imsm_update_reshape_migration
);
9636 u
= calloc(1, update_memory_size
);
9638 dprintf("error: cannot get memory for "
9639 "imsm_create_metadata_update_for_migration\n");
9642 u
->type
= update_reshape_migration
;
9643 u
->subdev
= super
->current_vol
;
9644 u
->new_level
= geo
->level
;
9645 u
->new_layout
= geo
->layout
;
9646 u
->new_raid_disks
= u
->old_raid_disks
= geo
->raid_disks
;
9647 u
->new_disks
[0] = -1;
9648 u
->new_chunksize
= -1;
9650 dev
= get_imsm_dev(super
, u
->subdev
);
9652 struct imsm_map
*map
;
9654 map
= get_imsm_map(dev
, MAP_0
);
9656 int current_chunk_size
=
9657 __le16_to_cpu(map
->blocks_per_strip
) / 2;
9659 if (geo
->chunksize
!= current_chunk_size
) {
9660 u
->new_chunksize
= geo
->chunksize
/ 1024;
9662 "chunk size change from %i to %i\n",
9663 current_chunk_size
, u
->new_chunksize
);
9665 previous_level
= map
->raid_level
;
9668 if ((geo
->level
== 5) && (previous_level
== 0)) {
9669 struct mdinfo
*spares
= NULL
;
9671 u
->new_raid_disks
++;
9672 spares
= get_spares_for_grow(st
);
9673 if ((spares
== NULL
) || (spares
->array
.spare_disks
< 1)) {
9676 update_memory_size
= 0;
9677 dprintf("error: cannot get spare device "
9678 "for requested migration");
9683 dprintf("imsm: reshape update preparation : OK\n");
9686 return update_memory_size
;
9689 static void imsm_update_metadata_locally(struct supertype
*st
,
9692 struct metadata_update mu
;
9697 mu
.space_list
= NULL
;
9699 imsm_prepare_update(st
, &mu
);
9700 imsm_process_update(st
, &mu
);
9702 while (mu
.space_list
) {
9703 void **space
= mu
.space_list
;
9704 mu
.space_list
= *space
;
9709 /***************************************************************************
9710 * Function: imsm_analyze_change
9711 * Description: Function analyze change for single volume
9712 * and validate if transition is supported
9713 * Parameters: Geometry parameters, supertype structure
9714 * Returns: Operation type code on success, -1 if fail
9715 ****************************************************************************/
9716 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
9717 struct geo_params
*geo
)
9723 /* number of added/removed disks in operation result */
9724 int devNumChange
= 0;
9725 /* imsm compatible layout value for array geometry verification */
9726 int imsm_layout
= -1;
9728 getinfo_super_imsm_volume(st
, &info
, NULL
);
9729 if ((geo
->level
!= info
.array
.level
) &&
9730 (geo
->level
>= 0) &&
9731 (geo
->level
!= UnSet
)) {
9732 switch (info
.array
.level
) {
9734 if (geo
->level
== 5) {
9735 change
= CH_MIGRATION
;
9736 if (geo
->layout
!= ALGORITHM_LEFT_ASYMMETRIC
) {
9738 Name
" Error. Requested Layout "
9739 "not supported (left-asymmetric layout "
9740 "is supported only)!\n");
9742 goto analyse_change_exit
;
9744 imsm_layout
= geo
->layout
;
9746 devNumChange
= 1; /* parity disk added */
9747 } else if (geo
->level
== 10) {
9748 change
= CH_TAKEOVER
;
9750 devNumChange
= 2; /* two mirrors added */
9751 imsm_layout
= 0x102; /* imsm supported layout */
9756 if (geo
->level
== 0) {
9757 change
= CH_TAKEOVER
;
9759 devNumChange
= -(geo
->raid_disks
/2);
9760 imsm_layout
= 0; /* imsm raid0 layout */
9766 Name
" Error. Level Migration from %d to %d "
9768 info
.array
.level
, geo
->level
);
9769 goto analyse_change_exit
;
9772 geo
->level
= info
.array
.level
;
9774 if ((geo
->layout
!= info
.array
.layout
)
9775 && ((geo
->layout
!= UnSet
) && (geo
->layout
!= -1))) {
9776 change
= CH_MIGRATION
;
9777 if ((info
.array
.layout
== 0)
9778 && (info
.array
.level
== 5)
9779 && (geo
->layout
== 5)) {
9780 /* reshape 5 -> 4 */
9781 } else if ((info
.array
.layout
== 5)
9782 && (info
.array
.level
== 5)
9783 && (geo
->layout
== 0)) {
9784 /* reshape 4 -> 5 */
9789 Name
" Error. Layout Migration from %d to %d "
9791 info
.array
.layout
, geo
->layout
);
9793 goto analyse_change_exit
;
9796 geo
->layout
= info
.array
.layout
;
9797 if (imsm_layout
== -1)
9798 imsm_layout
= info
.array
.layout
;
9801 if ((geo
->chunksize
> 0) && (geo
->chunksize
!= UnSet
)
9802 && (geo
->chunksize
!= info
.array
.chunk_size
))
9803 change
= CH_MIGRATION
;
9805 geo
->chunksize
= info
.array
.chunk_size
;
9807 chunk
= geo
->chunksize
/ 1024;
9808 if (!validate_geometry_imsm(st
,
9811 geo
->raid_disks
+ devNumChange
,
9818 struct intel_super
*super
= st
->sb
;
9819 struct imsm_super
*mpb
= super
->anchor
;
9821 if (mpb
->num_raid_devs
> 1) {
9823 Name
" Error. Cannot perform operation on %s"
9824 "- for this operation it MUST be single "
9825 "array in container\n",
9831 analyse_change_exit
:
9836 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
9838 struct intel_super
*super
= st
->sb
;
9839 struct imsm_update_takeover
*u
;
9841 u
= malloc(sizeof(struct imsm_update_takeover
));
9845 u
->type
= update_takeover
;
9846 u
->subarray
= super
->current_vol
;
9848 /* 10->0 transition */
9849 if (geo
->level
== 0)
9850 u
->direction
= R10_TO_R0
;
9852 /* 0->10 transition */
9853 if (geo
->level
== 10)
9854 u
->direction
= R0_TO_R10
;
9856 /* update metadata locally */
9857 imsm_update_metadata_locally(st
, u
,
9858 sizeof(struct imsm_update_takeover
));
9859 /* and possibly remotely */
9860 if (st
->update_tail
)
9861 append_metadata_update(st
, u
,
9862 sizeof(struct imsm_update_takeover
));
9869 static int imsm_reshape_super(struct supertype
*st
, long long size
, int level
,
9870 int layout
, int chunksize
, int raid_disks
,
9871 int delta_disks
, char *backup
, char *dev
,
9875 struct geo_params geo
;
9877 dprintf("imsm: reshape_super called.\n");
9879 memset(&geo
, 0, sizeof(struct geo_params
));
9882 geo
.dev_id
= st
->devnum
;
9885 geo
.layout
= layout
;
9886 geo
.chunksize
= chunksize
;
9887 geo
.raid_disks
= raid_disks
;
9888 if (delta_disks
!= UnSet
)
9889 geo
.raid_disks
+= delta_disks
;
9891 dprintf("\tfor level : %i\n", geo
.level
);
9892 dprintf("\tfor raid_disks : %i\n", geo
.raid_disks
);
9894 if (experimental() == 0)
9897 if (st
->container_dev
== st
->devnum
) {
9898 /* On container level we can only increase number of devices. */
9899 dprintf("imsm: info: Container operation\n");
9900 int old_raid_disks
= 0;
9902 if (imsm_reshape_is_allowed_on_container(
9903 st
, &geo
, &old_raid_disks
)) {
9904 struct imsm_update_reshape
*u
= NULL
;
9907 len
= imsm_create_metadata_update_for_reshape(
9908 st
, &geo
, old_raid_disks
, &u
);
9911 dprintf("imsm: Cannot prepare update\n");
9912 goto exit_imsm_reshape_super
;
9916 /* update metadata locally */
9917 imsm_update_metadata_locally(st
, u
, len
);
9918 /* and possibly remotely */
9919 if (st
->update_tail
)
9920 append_metadata_update(st
, u
, len
);
9925 fprintf(stderr
, Name
": (imsm) Operation "
9926 "is not allowed on this container\n");
9929 /* On volume level we support following operations
9930 * - takeover: raid10 -> raid0; raid0 -> raid10
9931 * - chunk size migration
9932 * - migration: raid5 -> raid0; raid0 -> raid5
9934 struct intel_super
*super
= st
->sb
;
9935 struct intel_dev
*dev
= super
->devlist
;
9937 dprintf("imsm: info: Volume operation\n");
9938 /* find requested device */
9940 if (imsm_find_array_minor_by_subdev(
9941 dev
->index
, st
->container_dev
, &devnum
) == 0
9942 && devnum
== geo
.dev_id
)
9947 fprintf(stderr
, Name
" Cannot find %s (%i) subarray\n",
9948 geo
.dev_name
, geo
.dev_id
);
9949 goto exit_imsm_reshape_super
;
9951 super
->current_vol
= dev
->index
;
9952 change
= imsm_analyze_change(st
, &geo
);
9955 ret_val
= imsm_takeover(st
, &geo
);
9957 case CH_MIGRATION
: {
9958 struct imsm_update_reshape_migration
*u
= NULL
;
9960 imsm_create_metadata_update_for_migration(
9964 "Cannot prepare update\n");
9968 /* update metadata locally */
9969 imsm_update_metadata_locally(st
, u
, len
);
9970 /* and possibly remotely */
9971 if (st
->update_tail
)
9972 append_metadata_update(st
, u
, len
);
9982 exit_imsm_reshape_super
:
9983 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
9987 /*******************************************************************************
9988 * Function: wait_for_reshape_imsm
9989 * Description: Function writes new sync_max value and waits until
9990 * reshape process reach new position
9992 * sra : general array info
9993 * ndata : number of disks in new array's layout
9996 * 1 : there is no reshape in progress,
9998 ******************************************************************************/
9999 int wait_for_reshape_imsm(struct mdinfo
*sra
, int ndata
)
10001 int fd
= sysfs_get_fd(sra
, NULL
, "reshape_position");
10002 unsigned long long completed
;
10003 /* to_complete : new sync_max position */
10004 unsigned long long to_complete
= sra
->reshape_progress
;
10005 unsigned long long position_to_set
= to_complete
/ ndata
;
10008 dprintf("imsm: wait_for_reshape_imsm() "
10009 "cannot open reshape_position\n");
10013 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
10014 dprintf("imsm: wait_for_reshape_imsm() "
10015 "cannot read reshape_position (no reshape in progres)\n");
10020 if (completed
> to_complete
) {
10021 dprintf("imsm: wait_for_reshape_imsm() "
10022 "wrong next position to set %llu (%llu)\n",
10023 to_complete
, completed
);
10027 dprintf("Position set: %llu\n", position_to_set
);
10028 if (sysfs_set_num(sra
, NULL
, "sync_max",
10029 position_to_set
) != 0) {
10030 dprintf("imsm: wait_for_reshape_imsm() "
10031 "cannot set reshape position to %llu\n",
10042 select(fd
+1, &rfds
, NULL
, NULL
, NULL
);
10043 if (sysfs_get_str(sra
, NULL
, "sync_action",
10045 strncmp(action
, "reshape", 7) != 0)
10047 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
10048 dprintf("imsm: wait_for_reshape_imsm() "
10049 "cannot read reshape_position (in loop)\n");
10053 } while (completed
< to_complete
);
10059 /*******************************************************************************
10060 * Function: check_degradation_change
10061 * Description: Check that array hasn't become failed.
10063 * info : for sysfs access
10064 * sources : source disks descriptors
10065 * degraded: previous degradation level
10067 * degradation level
10068 ******************************************************************************/
10069 int check_degradation_change(struct mdinfo
*info
,
10073 unsigned long long new_degraded
;
10074 sysfs_get_ll(info
, NULL
, "degraded", &new_degraded
);
10075 if (new_degraded
!= (unsigned long long)degraded
) {
10076 /* check each device to ensure it is still working */
10079 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
10080 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
10082 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
10084 if (sysfs_get_str(info
,
10085 sd
, "state", sbuf
, 20) < 0 ||
10086 strstr(sbuf
, "faulty") ||
10087 strstr(sbuf
, "in_sync") == NULL
) {
10088 /* this device is dead */
10089 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
10090 if (sd
->disk
.raid_disk
>= 0 &&
10091 sources
[sd
->disk
.raid_disk
] >= 0) {
10093 sd
->disk
.raid_disk
]);
10094 sources
[sd
->disk
.raid_disk
] =
10103 return new_degraded
;
10106 /*******************************************************************************
10107 * Function: imsm_manage_reshape
10108 * Description: Function finds array under reshape and it manages reshape
10109 * process. It creates stripes backups (if required) and sets
10112 * afd : Backup handle (nattive) - not used
10113 * sra : general array info
10114 * reshape : reshape parameters - not used
10115 * st : supertype structure
10116 * blocks : size of critical section [blocks]
10117 * fds : table of source device descriptor
10118 * offsets : start of array (offest per devices)
10120 * destfd : table of destination device descriptor
10121 * destoffsets : table of destination offsets (per device)
10123 * 1 : success, reshape is done
10125 ******************************************************************************/
10126 static int imsm_manage_reshape(
10127 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
10128 struct supertype
*st
, unsigned long backup_blocks
,
10129 int *fds
, unsigned long long *offsets
,
10130 int dests
, int *destfd
, unsigned long long *destoffsets
)
10133 struct intel_super
*super
= st
->sb
;
10134 struct intel_dev
*dv
= NULL
;
10135 struct imsm_dev
*dev
= NULL
;
10136 struct imsm_map
*map_src
;
10137 int migr_vol_qan
= 0;
10138 int ndata
, odata
; /* [bytes] */
10139 int chunk
; /* [bytes] */
10140 struct migr_record
*migr_rec
;
10142 unsigned int buf_size
; /* [bytes] */
10143 unsigned long long max_position
; /* array size [bytes] */
10144 unsigned long long next_step
; /* [blocks]/[bytes] */
10145 unsigned long long old_data_stripe_length
;
10146 unsigned long long start_src
; /* [bytes] */
10147 unsigned long long start
; /* [bytes] */
10148 unsigned long long start_buf_shift
; /* [bytes] */
10150 int source_layout
= 0;
10152 if (!fds
|| !offsets
|| !sra
)
10155 /* Find volume during the reshape */
10156 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
10157 if (dv
->dev
->vol
.migr_type
== MIGR_GEN_MIGR
10158 && dv
->dev
->vol
.migr_state
== 1) {
10163 /* Only one volume can migrate at the same time */
10164 if (migr_vol_qan
!= 1) {
10165 fprintf(stderr
, Name
" : %s", migr_vol_qan
?
10166 "Number of migrating volumes greater than 1\n" :
10167 "There is no volume during migrationg\n");
10171 map_src
= get_imsm_map(dev
, MAP_1
);
10172 if (map_src
== NULL
)
10175 ndata
= imsm_num_data_members(dev
, MAP_0
);
10176 odata
= imsm_num_data_members(dev
, MAP_1
);
10178 chunk
= __le16_to_cpu(map_src
->blocks_per_strip
) * 512;
10179 old_data_stripe_length
= odata
* chunk
;
10181 migr_rec
= super
->migr_rec
;
10183 /* initialize migration record for start condition */
10184 if (sra
->reshape_progress
== 0)
10185 init_migr_record_imsm(st
, dev
, sra
);
10187 if (__le32_to_cpu(migr_rec
->rec_status
) != UNIT_SRC_NORMAL
) {
10188 dprintf("imsm: cannot restart migration when data "
10189 "are present in copy area.\n");
10192 /* Save checkpoint to update migration record for current
10193 * reshape position (in md). It can be farther than current
10194 * reshape position in metadata.
10196 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
10197 /* ignore error == 2, this can mean end of reshape here
10199 dprintf("imsm: Cannot write checkpoint to "
10200 "migration record (UNIT_SRC_NORMAL, "
10201 "initial save)\n");
10206 /* size for data */
10207 buf_size
= __le32_to_cpu(migr_rec
->blocks_per_unit
) * 512;
10208 /* extend buffer size for parity disk */
10209 buf_size
+= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
10210 /* add space for stripe aligment */
10211 buf_size
+= old_data_stripe_length
;
10212 if (posix_memalign((void **)&buf
, 4096, buf_size
)) {
10213 dprintf("imsm: Cannot allocate checpoint buffer\n");
10217 max_position
= sra
->component_size
* ndata
;
10218 source_layout
= imsm_level_to_layout(map_src
->raid_level
);
10220 while (__le32_to_cpu(migr_rec
->curr_migr_unit
) <
10221 __le32_to_cpu(migr_rec
->num_migr_units
)) {
10222 /* current reshape position [blocks] */
10223 unsigned long long current_position
=
10224 __le32_to_cpu(migr_rec
->blocks_per_unit
)
10225 * __le32_to_cpu(migr_rec
->curr_migr_unit
);
10226 unsigned long long border
;
10228 /* Check that array hasn't become failed.
10230 degraded
= check_degradation_change(sra
, fds
, degraded
);
10231 if (degraded
> 1) {
10232 dprintf("imsm: Abort reshape due to degradation"
10233 " level (%i)\n", degraded
);
10237 next_step
= __le32_to_cpu(migr_rec
->blocks_per_unit
);
10239 if ((current_position
+ next_step
) > max_position
)
10240 next_step
= max_position
- current_position
;
10242 start
= current_position
* 512;
10244 /* allign reading start to old geometry */
10245 start_buf_shift
= start
% old_data_stripe_length
;
10246 start_src
= start
- start_buf_shift
;
10248 border
= (start_src
/ odata
) - (start
/ ndata
);
10250 if (border
<= __le32_to_cpu(migr_rec
->dest_depth_per_unit
)) {
10251 /* save critical stripes to buf
10252 * start - start address of current unit
10253 * to backup [bytes]
10254 * start_src - start address of current unit
10255 * to backup alligned to source array
10258 unsigned long long next_step_filler
= 0;
10259 unsigned long long copy_length
= next_step
* 512;
10261 /* allign copy area length to stripe in old geometry */
10262 next_step_filler
= ((copy_length
+ start_buf_shift
)
10263 % old_data_stripe_length
);
10264 if (next_step_filler
)
10265 next_step_filler
= (old_data_stripe_length
10266 - next_step_filler
);
10267 dprintf("save_stripes() parameters: start = %llu,"
10268 "\tstart_src = %llu,\tnext_step*512 = %llu,"
10269 "\tstart_in_buf_shift = %llu,"
10270 "\tnext_step_filler = %llu\n",
10271 start
, start_src
, copy_length
,
10272 start_buf_shift
, next_step_filler
);
10274 if (save_stripes(fds
, offsets
, map_src
->num_members
,
10275 chunk
, map_src
->raid_level
,
10276 source_layout
, 0, NULL
, start_src
,
10278 next_step_filler
+ start_buf_shift
,
10280 dprintf("imsm: Cannot save stripes"
10284 /* Convert data to destination format and store it
10285 * in backup general migration area
10287 if (save_backup_imsm(st
, dev
, sra
,
10288 buf
+ start_buf_shift
, copy_length
)) {
10289 dprintf("imsm: Cannot save stripes to "
10290 "target devices\n");
10293 if (save_checkpoint_imsm(st
, sra
,
10294 UNIT_SRC_IN_CP_AREA
)) {
10295 dprintf("imsm: Cannot write checkpoint to "
10296 "migration record (UNIT_SRC_IN_CP_AREA)\n");
10300 /* set next step to use whole border area */
10301 border
/= next_step
;
10303 next_step
*= border
;
10305 /* When data backed up, checkpoint stored,
10306 * kick the kernel to reshape unit of data
10308 next_step
= next_step
+ sra
->reshape_progress
;
10309 /* limit next step to array max position */
10310 if (next_step
> max_position
)
10311 next_step
= max_position
;
10312 sysfs_set_num(sra
, NULL
, "suspend_lo", sra
->reshape_progress
);
10313 sysfs_set_num(sra
, NULL
, "suspend_hi", next_step
);
10314 sra
->reshape_progress
= next_step
;
10316 /* wait until reshape finish */
10317 if (wait_for_reshape_imsm(sra
, ndata
) < 0) {
10318 dprintf("wait_for_reshape_imsm returned error!\n");
10322 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
10323 /* ignore error == 2, this can mean end of reshape here
10325 dprintf("imsm: Cannot write checkpoint to "
10326 "migration record (UNIT_SRC_NORMAL)\n");
10332 /* return '1' if done */
10336 abort_reshape(sra
);
10340 #endif /* MDASSEMBLE */
10342 struct superswitch super_imsm
= {
10344 .examine_super
= examine_super_imsm
,
10345 .brief_examine_super
= brief_examine_super_imsm
,
10346 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
10347 .export_examine_super
= export_examine_super_imsm
,
10348 .detail_super
= detail_super_imsm
,
10349 .brief_detail_super
= brief_detail_super_imsm
,
10350 .write_init_super
= write_init_super_imsm
,
10351 .validate_geometry
= validate_geometry_imsm
,
10352 .add_to_super
= add_to_super_imsm
,
10353 .remove_from_super
= remove_from_super_imsm
,
10354 .detail_platform
= detail_platform_imsm
,
10355 .kill_subarray
= kill_subarray_imsm
,
10356 .update_subarray
= update_subarray_imsm
,
10357 .load_container
= load_container_imsm
,
10358 .default_geometry
= default_geometry_imsm
,
10359 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
10360 .reshape_super
= imsm_reshape_super
,
10361 .manage_reshape
= imsm_manage_reshape
,
10362 .recover_backup
= recover_backup_imsm
,
10364 .match_home
= match_home_imsm
,
10365 .uuid_from_super
= uuid_from_super_imsm
,
10366 .getinfo_super
= getinfo_super_imsm
,
10367 .getinfo_super_disks
= getinfo_super_disks_imsm
,
10368 .update_super
= update_super_imsm
,
10370 .avail_size
= avail_size_imsm
,
10371 .min_acceptable_spare_size
= min_acceptable_spare_size_imsm
,
10373 .compare_super
= compare_super_imsm
,
10375 .load_super
= load_super_imsm
,
10376 .init_super
= init_super_imsm
,
10377 .store_super
= store_super_imsm
,
10378 .free_super
= free_super_imsm
,
10379 .match_metadata_desc
= match_metadata_desc_imsm
,
10380 .container_content
= container_content_imsm
,
10388 .open_new
= imsm_open_new
,
10389 .set_array_state
= imsm_set_array_state
,
10390 .set_disk
= imsm_set_disk
,
10391 .sync_metadata
= imsm_sync_metadata
,
10392 .activate_spare
= imsm_activate_spare
,
10393 .process_update
= imsm_process_update
,
10394 .prepare_update
= imsm_prepare_update
,
10395 #endif /* MDASSEMBLE */