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
)) {
2035 return map
->num_members
;
2039 return map
->num_members
/2;
2041 return map
->num_members
- 1;
2043 dprintf("%s: unsupported raid level\n", __func__
);
2048 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
2050 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2051 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2053 switch(get_imsm_raid_level(map
)) {
2056 return chunk
* map
->num_domains
;
2058 return chunk
* map
->num_members
;
2064 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
2066 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2067 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2068 __u32 strip
= block
/ chunk
;
2070 switch (get_imsm_raid_level(map
)) {
2073 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
2074 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
2076 return vol_stripe
* chunk
+ block
% chunk
;
2078 __u32 stripe
= strip
/ (map
->num_members
- 1);
2080 return stripe
* chunk
+ block
% chunk
;
2087 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
2088 struct imsm_dev
*dev
)
2090 /* calculate the conversion factor between per member 'blocks'
2091 * (md/{resync,rebuild}_start) and imsm migration units, return
2092 * 0 for the 'not migrating' and 'unsupported migration' cases
2094 if (!dev
->vol
.migr_state
)
2097 switch (migr_type(dev
)) {
2098 case MIGR_GEN_MIGR
: {
2099 struct migr_record
*migr_rec
= super
->migr_rec
;
2100 return __le32_to_cpu(migr_rec
->blocks_per_unit
);
2105 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2106 __u32 stripes_per_unit
;
2107 __u32 blocks_per_unit
;
2116 /* yes, this is really the translation of migr_units to
2117 * per-member blocks in the 'resync' case
2119 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
2120 migr_chunk
= migr_strip_blocks_resync(dev
);
2121 disks
= imsm_num_data_members(dev
, MAP_0
);
2122 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
2123 stripe
= __le16_to_cpu(map
->blocks_per_strip
) * disks
;
2124 segment
= blocks_per_unit
/ stripe
;
2125 block_rel
= blocks_per_unit
- segment
* stripe
;
2126 parity_depth
= parity_segment_depth(dev
);
2127 block_map
= map_migr_block(dev
, block_rel
);
2128 return block_map
+ parity_depth
* segment
;
2130 case MIGR_REBUILD
: {
2131 __u32 stripes_per_unit
;
2134 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
2135 migr_chunk
= migr_strip_blocks_rebuild(dev
);
2136 return migr_chunk
* stripes_per_unit
;
2138 case MIGR_STATE_CHANGE
:
2144 static int imsm_level_to_layout(int level
)
2152 return ALGORITHM_LEFT_ASYMMETRIC
;
2159 /*******************************************************************************
2160 * Function: read_imsm_migr_rec
2161 * Description: Function reads imsm migration record from last sector of disk
2163 * fd : disk descriptor
2164 * super : metadata info
2168 ******************************************************************************/
2169 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
)
2172 unsigned long long dsize
;
2174 get_dev_size(fd
, NULL
, &dsize
);
2175 if (lseek64(fd
, dsize
- MIGR_REC_POSITION
, SEEK_SET
) < 0) {
2177 Name
": Cannot seek to anchor block: %s\n",
2181 if (read(fd
, super
->migr_rec_buf
, MIGR_REC_BUF_SIZE
) !=
2182 MIGR_REC_BUF_SIZE
) {
2184 Name
": Cannot read migr record block: %s\n",
2194 static struct imsm_dev
*imsm_get_device_during_migration(
2195 struct intel_super
*super
)
2198 struct intel_dev
*dv
;
2200 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
2201 if (is_gen_migration(dv
->dev
))
2207 /*******************************************************************************
2208 * Function: load_imsm_migr_rec
2209 * Description: Function reads imsm migration record (it is stored at the last
2212 * super : imsm internal array info
2213 * info : general array info
2217 * -2 : no migration in progress
2218 ******************************************************************************/
2219 static int load_imsm_migr_rec(struct intel_super
*super
, struct mdinfo
*info
)
2222 struct dl
*dl
= NULL
;
2226 struct imsm_dev
*dev
;
2227 struct imsm_map
*map
= NULL
;
2230 /* find map under migration */
2231 dev
= imsm_get_device_during_migration(super
);
2232 /* nothing to load,no migration in progress?
2236 map
= get_imsm_map(dev
, MAP_0
);
2239 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
2240 /* skip spare and failed disks
2242 if (sd
->disk
.raid_disk
< 0)
2244 /* read only from one of the first two slots */
2246 slot
= get_imsm_disk_slot(map
,
2247 sd
->disk
.raid_disk
);
2248 if ((map
== NULL
) || (slot
> 1) || (slot
< 0))
2251 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
2252 fd
= dev_open(nm
, O_RDONLY
);
2258 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
2259 /* skip spare and failed disks
2263 /* read only from one of the first two slots */
2265 slot
= get_imsm_disk_slot(map
, dl
->index
);
2266 if ((map
== NULL
) || (slot
> 1) || (slot
< 0))
2268 sprintf(nm
, "%d:%d", dl
->major
, dl
->minor
);
2269 fd
= dev_open(nm
, O_RDONLY
);
2276 retval
= read_imsm_migr_rec(fd
, super
);
2285 /*******************************************************************************
2286 * function: imsm_create_metadata_checkpoint_update
2287 * Description: It creates update for checkpoint change.
2289 * super : imsm internal array info
2290 * u : pointer to prepared update
2293 * If length is equal to 0, input pointer u contains no update
2294 ******************************************************************************/
2295 static int imsm_create_metadata_checkpoint_update(
2296 struct intel_super
*super
,
2297 struct imsm_update_general_migration_checkpoint
**u
)
2300 int update_memory_size
= 0;
2302 dprintf("imsm_create_metadata_checkpoint_update(enter)\n");
2308 /* size of all update data without anchor */
2309 update_memory_size
=
2310 sizeof(struct imsm_update_general_migration_checkpoint
);
2312 *u
= calloc(1, update_memory_size
);
2314 dprintf("error: cannot get memory for "
2315 "imsm_create_metadata_checkpoint_update update\n");
2318 (*u
)->type
= update_general_migration_checkpoint
;
2319 (*u
)->curr_migr_unit
= __le32_to_cpu(super
->migr_rec
->curr_migr_unit
);
2320 dprintf("imsm_create_metadata_checkpoint_update: prepared for %u\n",
2321 (*u
)->curr_migr_unit
);
2323 return update_memory_size
;
2327 static void imsm_update_metadata_locally(struct supertype
*st
,
2328 void *buf
, int len
);
2330 /*******************************************************************************
2331 * Function: write_imsm_migr_rec
2332 * Description: Function writes imsm migration record
2333 * (at the last sector of disk)
2335 * super : imsm internal array info
2339 ******************************************************************************/
2340 static int write_imsm_migr_rec(struct supertype
*st
)
2342 struct intel_super
*super
= st
->sb
;
2343 unsigned long long dsize
;
2349 struct imsm_update_general_migration_checkpoint
*u
;
2350 struct imsm_dev
*dev
;
2351 struct imsm_map
*map
= NULL
;
2353 /* find map under migration */
2354 dev
= imsm_get_device_during_migration(super
);
2355 /* if no migration, write buffer anyway to clear migr_record
2356 * on disk based on first available device
2359 dev
= get_imsm_dev(super
, super
->current_vol
< 0 ? 0 :
2360 super
->current_vol
);
2362 map
= get_imsm_map(dev
, MAP_0
);
2364 for (sd
= super
->disks
; sd
; sd
= sd
->next
) {
2367 /* skip failed and spare devices */
2370 /* write to 2 first slots only */
2372 slot
= get_imsm_disk_slot(map
, sd
->index
);
2373 if ((map
== NULL
) || (slot
> 1) || (slot
< 0))
2376 sprintf(nm
, "%d:%d", sd
->major
, sd
->minor
);
2377 fd
= dev_open(nm
, O_RDWR
);
2380 get_dev_size(fd
, NULL
, &dsize
);
2381 if (lseek64(fd
, dsize
- MIGR_REC_POSITION
, SEEK_SET
) < 0) {
2383 Name
": Cannot seek to anchor block: %s\n",
2387 if (write(fd
, super
->migr_rec_buf
, MIGR_REC_BUF_SIZE
) !=
2388 MIGR_REC_BUF_SIZE
) {
2390 Name
": Cannot write migr record block: %s\n",
2397 /* update checkpoint information in metadata */
2398 len
= imsm_create_metadata_checkpoint_update(super
, &u
);
2401 dprintf("imsm: Cannot prepare update\n");
2404 /* update metadata locally */
2405 imsm_update_metadata_locally(st
, u
, len
);
2406 /* and possibly remotely */
2407 if (st
->update_tail
) {
2408 append_metadata_update(st
, u
, len
);
2409 /* during reshape we do all work inside metadata handler
2410 * manage_reshape(), so metadata update has to be triggered
2413 flush_metadata_updates(st
);
2414 st
->update_tail
= &st
->updates
;
2424 #endif /* MDASSEMBLE */
2426 /* spare/missing disks activations are not allowe when
2427 * array/container performs reshape operation, because
2428 * all arrays in container works on the same disks set
2430 int imsm_reshape_blocks_arrays_changes(struct intel_super
*super
)
2433 struct intel_dev
*i_dev
;
2434 struct imsm_dev
*dev
;
2436 /* check whole container
2438 for (i_dev
= super
->devlist
; i_dev
; i_dev
= i_dev
->next
) {
2440 if (is_gen_migration(dev
)) {
2441 /* No repair during any migration in container
2450 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
2452 struct intel_super
*super
= st
->sb
;
2453 struct migr_record
*migr_rec
= super
->migr_rec
;
2454 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
2455 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2456 struct imsm_map
*prev_map
= get_imsm_map(dev
, MAP_1
);
2457 struct imsm_map
*map_to_analyse
= map
;
2460 unsigned int component_size_alligment
;
2461 int map_disks
= info
->array
.raid_disks
;
2463 memset(info
, 0, sizeof(*info
));
2465 map_to_analyse
= prev_map
;
2467 dl
= super
->current_disk
;
2469 info
->container_member
= super
->current_vol
;
2470 info
->array
.raid_disks
= map
->num_members
;
2471 info
->array
.level
= get_imsm_raid_level(map_to_analyse
);
2472 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
2473 info
->array
.md_minor
= -1;
2474 info
->array
.ctime
= 0;
2475 info
->array
.utime
= 0;
2476 info
->array
.chunk_size
=
2477 __le16_to_cpu(map_to_analyse
->blocks_per_strip
) << 9;
2478 info
->array
.state
= !dev
->vol
.dirty
;
2479 info
->custom_array_size
= __le32_to_cpu(dev
->size_high
);
2480 info
->custom_array_size
<<= 32;
2481 info
->custom_array_size
|= __le32_to_cpu(dev
->size_low
);
2482 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
2484 if (is_gen_migration(dev
)) {
2485 info
->reshape_active
= 1;
2486 info
->new_level
= get_imsm_raid_level(map
);
2487 info
->new_layout
= imsm_level_to_layout(info
->new_level
);
2488 info
->new_chunk
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
2489 info
->delta_disks
= map
->num_members
- prev_map
->num_members
;
2490 if (info
->delta_disks
) {
2491 /* this needs to be applied to every array
2494 info
->reshape_active
= CONTAINER_RESHAPE
;
2496 /* We shape information that we give to md might have to be
2497 * modify to cope with md's requirement for reshaping arrays.
2498 * For example, when reshaping a RAID0, md requires it to be
2499 * presented as a degraded RAID4.
2500 * Also if a RAID0 is migrating to a RAID5 we need to specify
2501 * the array as already being RAID5, but the 'before' layout
2502 * is a RAID4-like layout.
2504 switch (info
->array
.level
) {
2506 switch(info
->new_level
) {
2508 /* conversion is happening as RAID4 */
2509 info
->array
.level
= 4;
2510 info
->array
.raid_disks
+= 1;
2513 /* conversion is happening as RAID5 */
2514 info
->array
.level
= 5;
2515 info
->array
.layout
= ALGORITHM_PARITY_N
;
2516 info
->delta_disks
-= 1;
2519 /* FIXME error message */
2520 info
->array
.level
= UnSet
;
2526 info
->new_level
= UnSet
;
2527 info
->new_layout
= UnSet
;
2528 info
->new_chunk
= info
->array
.chunk_size
;
2529 info
->delta_disks
= 0;
2533 info
->disk
.major
= dl
->major
;
2534 info
->disk
.minor
= dl
->minor
;
2535 info
->disk
.number
= dl
->index
;
2536 info
->disk
.raid_disk
= get_imsm_disk_slot(map_to_analyse
,
2540 info
->data_offset
= pba_of_lba0(map_to_analyse
);
2541 info
->component_size
= blocks_per_member(map_to_analyse
);
2543 /* check component size aligment
2545 component_size_alligment
=
2546 info
->component_size
% (info
->array
.chunk_size
/512);
2548 if (component_size_alligment
&&
2549 (info
->array
.level
!= 1) && (info
->array
.level
!= UnSet
)) {
2550 dprintf("imsm: reported component size alligned from %llu ",
2551 info
->component_size
);
2552 info
->component_size
-= component_size_alligment
;
2553 dprintf("to %llu (%i).\n",
2554 info
->component_size
, component_size_alligment
);
2557 memset(info
->uuid
, 0, sizeof(info
->uuid
));
2558 info
->recovery_start
= MaxSector
;
2560 info
->reshape_progress
= 0;
2561 info
->resync_start
= MaxSector
;
2562 if ((map_to_analyse
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
2564 imsm_reshape_blocks_arrays_changes(super
) == 0) {
2565 info
->resync_start
= 0;
2567 if (dev
->vol
.migr_state
) {
2568 switch (migr_type(dev
)) {
2571 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
2573 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
2575 info
->resync_start
= blocks_per_unit
* units
;
2578 case MIGR_GEN_MIGR
: {
2579 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
2581 __u64 units
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
2582 unsigned long long array_blocks
;
2585 if (__le32_to_cpu(migr_rec
->ascending_migr
) &&
2587 (__le32_to_cpu(migr_rec
->num_migr_units
)-1)) &&
2588 (super
->migr_rec
->rec_status
==
2589 __cpu_to_le32(UNIT_SRC_IN_CP_AREA
)))
2592 info
->reshape_progress
= blocks_per_unit
* units
;
2594 dprintf("IMSM: General Migration checkpoint : %llu "
2595 "(%llu) -> read reshape progress : %llu\n",
2596 (unsigned long long)units
,
2597 (unsigned long long)blocks_per_unit
,
2598 info
->reshape_progress
);
2600 used_disks
= imsm_num_data_members(dev
, MAP_1
);
2601 if (used_disks
> 0) {
2602 array_blocks
= blocks_per_member(map
) *
2604 /* round array size down to closest MB
2606 info
->custom_array_size
= (array_blocks
2607 >> SECT_PER_MB_SHIFT
)
2608 << SECT_PER_MB_SHIFT
;
2612 /* we could emulate the checkpointing of
2613 * 'sync_action=check' migrations, but for now
2614 * we just immediately complete them
2617 /* this is handled by container_content_imsm() */
2618 case MIGR_STATE_CHANGE
:
2619 /* FIXME handle other migrations */
2621 /* we are not dirty, so... */
2622 info
->resync_start
= MaxSector
;
2626 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
2627 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
2629 info
->array
.major_version
= -1;
2630 info
->array
.minor_version
= -2;
2631 devname
= devnum2devname(st
->container_dev
);
2632 *info
->text_version
= '\0';
2634 sprintf(info
->text_version
, "/%s/%d", devname
, info
->container_member
);
2636 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
2637 uuid_from_super_imsm(st
, info
->uuid
);
2641 for (i
=0; i
<map_disks
; i
++) {
2643 if (i
< info
->array
.raid_disks
) {
2644 struct imsm_disk
*dsk
;
2645 j
= get_imsm_disk_idx(dev
, i
, MAP_X
);
2646 dsk
= get_imsm_disk(super
, j
);
2647 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
2654 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
2655 int failed
, int look_in_map
);
2657 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
2662 static void manage_second_map(struct intel_super
*super
, struct imsm_dev
*dev
)
2664 if (is_gen_migration(dev
)) {
2667 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
2669 failed
= imsm_count_failed(super
, dev
, MAP_1
);
2670 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_1
);
2671 if (map2
->map_state
!= map_state
) {
2672 map2
->map_state
= map_state
;
2673 super
->updates_pending
++;
2679 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
2683 for (d
= super
->missing
; d
; d
= d
->next
)
2684 if (d
->index
== index
)
2689 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
2691 struct intel_super
*super
= st
->sb
;
2692 struct imsm_disk
*disk
;
2693 int map_disks
= info
->array
.raid_disks
;
2694 int max_enough
= -1;
2696 struct imsm_super
*mpb
;
2698 if (super
->current_vol
>= 0) {
2699 getinfo_super_imsm_volume(st
, info
, map
);
2702 memset(info
, 0, sizeof(*info
));
2704 /* Set raid_disks to zero so that Assemble will always pull in valid
2707 info
->array
.raid_disks
= 0;
2708 info
->array
.level
= LEVEL_CONTAINER
;
2709 info
->array
.layout
= 0;
2710 info
->array
.md_minor
= -1;
2711 info
->array
.ctime
= 0; /* N/A for imsm */
2712 info
->array
.utime
= 0;
2713 info
->array
.chunk_size
= 0;
2715 info
->disk
.major
= 0;
2716 info
->disk
.minor
= 0;
2717 info
->disk
.raid_disk
= -1;
2718 info
->reshape_active
= 0;
2719 info
->array
.major_version
= -1;
2720 info
->array
.minor_version
= -2;
2721 strcpy(info
->text_version
, "imsm");
2722 info
->safe_mode_delay
= 0;
2723 info
->disk
.number
= -1;
2724 info
->disk
.state
= 0;
2726 info
->recovery_start
= MaxSector
;
2727 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
2729 /* do we have the all the insync disks that we expect? */
2730 mpb
= super
->anchor
;
2732 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
2733 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
2734 int failed
, enough
, j
, missing
= 0;
2735 struct imsm_map
*map
;
2738 failed
= imsm_count_failed(super
, dev
, MAP_0
);
2739 state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
2740 map
= get_imsm_map(dev
, MAP_0
);
2742 /* any newly missing disks?
2743 * (catches single-degraded vs double-degraded)
2745 for (j
= 0; j
< map
->num_members
; j
++) {
2746 __u32 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
2747 __u32 idx
= ord_to_idx(ord
);
2749 if (!(ord
& IMSM_ORD_REBUILD
) &&
2750 get_imsm_missing(super
, idx
)) {
2756 if (state
== IMSM_T_STATE_FAILED
)
2758 else if (state
== IMSM_T_STATE_DEGRADED
&&
2759 (state
!= map
->map_state
|| missing
))
2761 else /* we're normal, or already degraded */
2763 if (is_gen_migration(dev
) && missing
) {
2764 /* during general migration we need all disks
2765 * that process is running on.
2766 * No new missing disk is allowed.
2770 /* no more checks necessary
2774 /* in the missing/failed disk case check to see
2775 * if at least one array is runnable
2777 max_enough
= max(max_enough
, enough
);
2779 dprintf("%s: enough: %d\n", __func__
, max_enough
);
2780 info
->container_enough
= max_enough
;
2783 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
2785 disk
= &super
->disks
->disk
;
2786 info
->data_offset
= total_blocks(&super
->disks
->disk
) - reserved
;
2787 info
->component_size
= reserved
;
2788 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
2789 /* we don't change info->disk.raid_disk here because
2790 * this state will be finalized in mdmon after we have
2791 * found the 'most fresh' version of the metadata
2793 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
2794 info
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
2797 /* only call uuid_from_super_imsm when this disk is part of a populated container,
2798 * ->compare_super may have updated the 'num_raid_devs' field for spares
2800 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
2801 uuid_from_super_imsm(st
, info
->uuid
);
2803 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
2805 /* I don't know how to compute 'map' on imsm, so use safe default */
2808 for (i
= 0; i
< map_disks
; i
++)
2814 /* allocates memory and fills disk in mdinfo structure
2815 * for each disk in array */
2816 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
2818 struct mdinfo
*mddev
= NULL
;
2819 struct intel_super
*super
= st
->sb
;
2820 struct imsm_disk
*disk
;
2823 if (!super
|| !super
->disks
)
2826 mddev
= malloc(sizeof(*mddev
));
2828 fprintf(stderr
, Name
": Failed to allocate memory.\n");
2831 memset(mddev
, 0, sizeof(*mddev
));
2835 tmp
= malloc(sizeof(*tmp
));
2837 fprintf(stderr
, Name
": Failed to allocate memory.\n");
2842 memset(tmp
, 0, sizeof(*tmp
));
2844 tmp
->next
= mddev
->devs
;
2846 tmp
->disk
.number
= count
++;
2847 tmp
->disk
.major
= dl
->major
;
2848 tmp
->disk
.minor
= dl
->minor
;
2849 tmp
->disk
.state
= is_configured(disk
) ?
2850 (1 << MD_DISK_ACTIVE
) : 0;
2851 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
2852 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
2853 tmp
->disk
.raid_disk
= -1;
2859 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
2860 char *update
, char *devname
, int verbose
,
2861 int uuid_set
, char *homehost
)
2863 /* For 'assemble' and 'force' we need to return non-zero if any
2864 * change was made. For others, the return value is ignored.
2865 * Update options are:
2866 * force-one : This device looks a bit old but needs to be included,
2867 * update age info appropriately.
2868 * assemble: clear any 'faulty' flag to allow this device to
2870 * force-array: Array is degraded but being forced, mark it clean
2871 * if that will be needed to assemble it.
2873 * newdev: not used ????
2874 * grow: Array has gained a new device - this is currently for
2876 * resync: mark as dirty so a resync will happen.
2877 * name: update the name - preserving the homehost
2878 * uuid: Change the uuid of the array to match watch is given
2880 * Following are not relevant for this imsm:
2881 * sparc2.2 : update from old dodgey metadata
2882 * super-minor: change the preferred_minor number
2883 * summaries: update redundant counters.
2884 * homehost: update the recorded homehost
2885 * _reshape_progress: record new reshape_progress position.
2888 struct intel_super
*super
= st
->sb
;
2889 struct imsm_super
*mpb
;
2891 /* we can only update container info */
2892 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
2895 mpb
= super
->anchor
;
2897 if (strcmp(update
, "uuid") == 0) {
2898 /* We take this to mean that the family_num should be updated.
2899 * However that is much smaller than the uuid so we cannot really
2900 * allow an explicit uuid to be given. And it is hard to reliably
2902 * So if !uuid_set we know the current uuid is random and just used
2903 * the first 'int' and copy it to the other 3 positions.
2904 * Otherwise we require the 4 'int's to be the same as would be the
2905 * case if we are using a random uuid. So an explicit uuid will be
2906 * accepted as long as all for ints are the same... which shouldn't hurt
2909 info
->uuid
[1] = info
->uuid
[2] = info
->uuid
[3] = info
->uuid
[0];
2912 if (info
->uuid
[0] != info
->uuid
[1] ||
2913 info
->uuid
[1] != info
->uuid
[2] ||
2914 info
->uuid
[2] != info
->uuid
[3])
2920 mpb
->orig_family_num
= info
->uuid
[0];
2921 } else if (strcmp(update
, "assemble") == 0)
2926 /* successful update? recompute checksum */
2928 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
2933 static size_t disks_to_mpb_size(int disks
)
2937 size
= sizeof(struct imsm_super
);
2938 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
2939 size
+= 2 * sizeof(struct imsm_dev
);
2940 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
2941 size
+= (4 - 2) * sizeof(struct imsm_map
);
2942 /* 4 possible disk_ord_tbl's */
2943 size
+= 4 * (disks
- 1) * sizeof(__u32
);
2948 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
)
2950 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
2953 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
2956 static void free_devlist(struct intel_super
*super
)
2958 struct intel_dev
*dv
;
2960 while (super
->devlist
) {
2961 dv
= super
->devlist
->next
;
2962 free(super
->devlist
->dev
);
2963 free(super
->devlist
);
2964 super
->devlist
= dv
;
2968 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
2970 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
2973 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
)
2977 * 0 same, or first was empty, and second was copied
2978 * 1 second had wrong number
2980 * 3 wrong other info
2982 struct intel_super
*first
= st
->sb
;
2983 struct intel_super
*sec
= tst
->sb
;
2990 /* in platform dependent environment test if the disks
2991 * use the same Intel hba
2993 if (!check_env("IMSM_NO_PLATFORM")) {
2994 if (!first
->hba
|| !sec
->hba
||
2995 (first
->hba
->type
!= sec
->hba
->type
)) {
2997 "HBAs of devices does not match %s != %s\n",
2998 first
->hba
? get_sys_dev_type(first
->hba
->type
) : NULL
,
2999 sec
->hba
? get_sys_dev_type(sec
->hba
->type
) : NULL
);
3004 /* if an anchor does not have num_raid_devs set then it is a free
3007 if (first
->anchor
->num_raid_devs
> 0 &&
3008 sec
->anchor
->num_raid_devs
> 0) {
3009 /* Determine if these disks might ever have been
3010 * related. Further disambiguation can only take place
3011 * in load_super_imsm_all
3013 __u32 first_family
= first
->anchor
->orig_family_num
;
3014 __u32 sec_family
= sec
->anchor
->orig_family_num
;
3016 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
3017 MAX_SIGNATURE_LENGTH
) != 0)
3020 if (first_family
== 0)
3021 first_family
= first
->anchor
->family_num
;
3022 if (sec_family
== 0)
3023 sec_family
= sec
->anchor
->family_num
;
3025 if (first_family
!= sec_family
)
3031 /* if 'first' is a spare promote it to a populated mpb with sec's
3034 if (first
->anchor
->num_raid_devs
== 0 &&
3035 sec
->anchor
->num_raid_devs
> 0) {
3037 struct intel_dev
*dv
;
3038 struct imsm_dev
*dev
;
3040 /* we need to copy raid device info from sec if an allocation
3041 * fails here we don't associate the spare
3043 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++) {
3044 dv
= malloc(sizeof(*dv
));
3047 dev
= malloc(sizeof_imsm_dev(get_imsm_dev(sec
, i
), 1));
3054 dv
->next
= first
->devlist
;
3055 first
->devlist
= dv
;
3057 if (i
< sec
->anchor
->num_raid_devs
) {
3058 /* allocation failure */
3059 free_devlist(first
);
3060 fprintf(stderr
, "imsm: failed to associate spare\n");
3063 first
->anchor
->num_raid_devs
= sec
->anchor
->num_raid_devs
;
3064 first
->anchor
->orig_family_num
= sec
->anchor
->orig_family_num
;
3065 first
->anchor
->family_num
= sec
->anchor
->family_num
;
3066 memcpy(first
->anchor
->sig
, sec
->anchor
->sig
, MAX_SIGNATURE_LENGTH
);
3067 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++)
3068 imsm_copy_dev(get_imsm_dev(first
, i
), get_imsm_dev(sec
, i
));
3074 static void fd2devname(int fd
, char *name
)
3078 char dname
[PATH_MAX
];
3083 if (fstat(fd
, &st
) != 0)
3085 sprintf(path
, "/sys/dev/block/%d:%d",
3086 major(st
.st_rdev
), minor(st
.st_rdev
));
3088 rv
= readlink(path
, dname
, sizeof(dname
)-1);
3093 nm
= strrchr(dname
, '/');
3096 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
3100 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
3102 static int imsm_read_serial(int fd
, char *devname
,
3103 __u8 serial
[MAX_RAID_SERIAL_LEN
])
3105 unsigned char scsi_serial
[255];
3114 memset(scsi_serial
, 0, sizeof(scsi_serial
));
3116 rv
= scsi_get_serial(fd
, scsi_serial
, sizeof(scsi_serial
));
3118 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
3119 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3120 fd2devname(fd
, (char *) serial
);
3127 Name
": Failed to retrieve serial for %s\n",
3132 rsp_len
= scsi_serial
[3];
3136 Name
": Failed to retrieve serial for %s\n",
3140 rsp_buf
= (char *) &scsi_serial
[4];
3142 /* trim all whitespace and non-printable characters and convert
3145 for (i
= 0, dest
= rsp_buf
; i
< rsp_len
; i
++) {
3148 /* ':' is reserved for use in placeholder serial
3149 * numbers for missing disks
3157 len
= dest
- rsp_buf
;
3160 /* truncate leading characters */
3161 if (len
> MAX_RAID_SERIAL_LEN
) {
3162 dest
+= len
- MAX_RAID_SERIAL_LEN
;
3163 len
= MAX_RAID_SERIAL_LEN
;
3166 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3167 memcpy(serial
, dest
, len
);
3172 static int serialcmp(__u8
*s1
, __u8
*s2
)
3174 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
3177 static void serialcpy(__u8
*dest
, __u8
*src
)
3179 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
3182 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
3186 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3187 if (serialcmp(dl
->serial
, serial
) == 0)
3193 static struct imsm_disk
*
3194 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
3198 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3199 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3201 if (serialcmp(disk
->serial
, serial
) == 0) {
3212 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3214 struct imsm_disk
*disk
;
3219 __u8 serial
[MAX_RAID_SERIAL_LEN
];
3221 rv
= imsm_read_serial(fd
, devname
, serial
);
3226 dl
= calloc(1, sizeof(*dl
));
3230 Name
": failed to allocate disk buffer for %s\n",
3236 dl
->major
= major(stb
.st_rdev
);
3237 dl
->minor
= minor(stb
.st_rdev
);
3238 dl
->next
= super
->disks
;
3239 dl
->fd
= keep_fd
? fd
: -1;
3240 assert(super
->disks
== NULL
);
3242 serialcpy(dl
->serial
, serial
);
3245 fd2devname(fd
, name
);
3247 dl
->devname
= strdup(devname
);
3249 dl
->devname
= strdup(name
);
3251 /* look up this disk's index in the current anchor */
3252 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
3255 /* only set index on disks that are a member of a
3256 * populated contianer, i.e. one with raid_devs
3258 if (is_failed(&dl
->disk
))
3260 else if (is_spare(&dl
->disk
))
3268 /* When migrating map0 contains the 'destination' state while map1
3269 * contains the current state. When not migrating map0 contains the
3270 * current state. This routine assumes that map[0].map_state is set to
3271 * the current array state before being called.
3273 * Migration is indicated by one of the following states
3274 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
3275 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
3276 * map1state=unitialized)
3277 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
3279 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
3280 * map1state=degraded)
3281 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
3284 static void migrate(struct imsm_dev
*dev
, struct intel_super
*super
,
3285 __u8 to_state
, int migr_type
)
3287 struct imsm_map
*dest
;
3288 struct imsm_map
*src
= get_imsm_map(dev
, MAP_0
);
3290 dev
->vol
.migr_state
= 1;
3291 set_migr_type(dev
, migr_type
);
3292 dev
->vol
.curr_migr_unit
= 0;
3293 dest
= get_imsm_map(dev
, MAP_1
);
3295 /* duplicate and then set the target end state in map[0] */
3296 memcpy(dest
, src
, sizeof_imsm_map(src
));
3297 if ((migr_type
== MIGR_REBUILD
) ||
3298 (migr_type
== MIGR_GEN_MIGR
)) {
3302 for (i
= 0; i
< src
->num_members
; i
++) {
3303 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
3304 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
3308 if (migr_type
== MIGR_GEN_MIGR
)
3309 /* Clear migration record */
3310 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
3312 src
->map_state
= to_state
;
3315 static void end_migration(struct imsm_dev
*dev
, struct intel_super
*super
,
3318 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3319 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
== 0 ?
3323 /* merge any IMSM_ORD_REBUILD bits that were not successfully
3324 * completed in the last migration.
3326 * FIXME add support for raid-level-migration
3328 if ((map_state
!= map
->map_state
) && (is_gen_migration(dev
) == 0) &&
3329 (prev
->map_state
!= IMSM_T_STATE_UNINITIALIZED
)) {
3330 /* when final map state is other than expected
3331 * merge maps (not for migration)
3335 for (i
= 0; i
< prev
->num_members
; i
++)
3336 for (j
= 0; j
< map
->num_members
; j
++)
3337 /* during online capacity expansion
3338 * disks position can be changed
3339 * if takeover is used
3341 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
3342 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
3343 map
->disk_ord_tbl
[j
] |=
3344 prev
->disk_ord_tbl
[i
];
3347 failed
= imsm_count_failed(super
, dev
, MAP_0
);
3348 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
3351 dev
->vol
.migr_state
= 0;
3352 set_migr_type(dev
, 0);
3353 dev
->vol
.curr_migr_unit
= 0;
3354 map
->map_state
= map_state
;
3358 static int parse_raid_devices(struct intel_super
*super
)
3361 struct imsm_dev
*dev_new
;
3362 size_t len
, len_migr
;
3364 size_t space_needed
= 0;
3365 struct imsm_super
*mpb
= super
->anchor
;
3367 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3368 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3369 struct intel_dev
*dv
;
3371 len
= sizeof_imsm_dev(dev_iter
, 0);
3372 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
3374 space_needed
+= len_migr
- len
;
3376 dv
= malloc(sizeof(*dv
));
3379 if (max_len
< len_migr
)
3381 if (max_len
> len_migr
)
3382 space_needed
+= max_len
- len_migr
;
3383 dev_new
= malloc(max_len
);
3388 imsm_copy_dev(dev_new
, dev_iter
);
3391 dv
->next
= super
->devlist
;
3392 super
->devlist
= dv
;
3395 /* ensure that super->buf is large enough when all raid devices
3398 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
3401 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
, 512);
3402 if (posix_memalign(&buf
, 512, len
) != 0)
3405 memcpy(buf
, super
->buf
, super
->len
);
3406 memset(buf
+ super
->len
, 0, len
- super
->len
);
3415 /* retrieve a pointer to the bbm log which starts after all raid devices */
3416 struct bbm_log
*__get_imsm_bbm_log(struct imsm_super
*mpb
)
3420 if (__le32_to_cpu(mpb
->bbm_log_size
)) {
3422 ptr
+= mpb
->mpb_size
- __le32_to_cpu(mpb
->bbm_log_size
);
3428 /*******************************************************************************
3429 * Function: check_mpb_migr_compatibility
3430 * Description: Function checks for unsupported migration features:
3431 * - migration optimization area (pba_of_lba0)
3432 * - descending reshape (ascending_migr)
3434 * super : imsm metadata information
3436 * 0 : migration is compatible
3437 * -1 : migration is not compatible
3438 ******************************************************************************/
3439 int check_mpb_migr_compatibility(struct intel_super
*super
)
3441 struct imsm_map
*map0
, *map1
;
3442 struct migr_record
*migr_rec
= super
->migr_rec
;
3445 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3446 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3449 dev_iter
->vol
.migr_state
== 1 &&
3450 dev_iter
->vol
.migr_type
== MIGR_GEN_MIGR
) {
3451 /* This device is migrating */
3452 map0
= get_imsm_map(dev_iter
, MAP_0
);
3453 map1
= get_imsm_map(dev_iter
, MAP_1
);
3454 if (pba_of_lba0(map0
) != pba_of_lba0(map1
))
3455 /* migration optimization area was used */
3457 if (migr_rec
->ascending_migr
== 0
3458 && migr_rec
->dest_depth_per_unit
> 0)
3459 /* descending reshape not supported yet */
3466 static void __free_imsm(struct intel_super
*super
, int free_disks
);
3468 /* load_imsm_mpb - read matrix metadata
3469 * allocates super->mpb to be freed by free_imsm
3471 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
3473 unsigned long long dsize
;
3474 unsigned long long sectors
;
3476 struct imsm_super
*anchor
;
3479 get_dev_size(fd
, NULL
, &dsize
);
3483 Name
": %s: device to small for imsm\n",
3488 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0) {
3490 fprintf(stderr
, Name
3491 ": Cannot seek to anchor block on %s: %s\n",
3492 devname
, strerror(errno
));
3496 if (posix_memalign((void**)&anchor
, 512, 512) != 0) {
3499 Name
": Failed to allocate imsm anchor buffer"
3500 " on %s\n", devname
);
3503 if (read(fd
, anchor
, 512) != 512) {
3506 Name
": Cannot read anchor block on %s: %s\n",
3507 devname
, strerror(errno
));
3512 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
3515 Name
": no IMSM anchor on %s\n", devname
);
3520 __free_imsm(super
, 0);
3521 /* reload capability and hba */
3523 /* capability and hba must be updated with new super allocation */
3524 find_intel_hba_capability(fd
, super
, devname
);
3525 super
->len
= ROUND_UP(anchor
->mpb_size
, 512);
3526 if (posix_memalign(&super
->buf
, 512, super
->len
) != 0) {
3529 Name
": unable to allocate %zu byte mpb buffer\n",
3534 memcpy(super
->buf
, anchor
, 512);
3536 sectors
= mpb_sectors(anchor
) - 1;
3539 if (posix_memalign(&super
->migr_rec_buf
, 512, MIGR_REC_BUF_SIZE
) != 0) {
3540 fprintf(stderr
, Name
3541 ": %s could not allocate migr_rec buffer\n", __func__
);
3545 super
->clean_migration_record_by_mdmon
= 0;
3548 check_sum
= __gen_imsm_checksum(super
->anchor
);
3549 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
3552 Name
": IMSM checksum %x != %x on %s\n",
3554 __le32_to_cpu(super
->anchor
->check_sum
),
3562 /* read the extended mpb */
3563 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0) {
3566 Name
": Cannot seek to extended mpb on %s: %s\n",
3567 devname
, strerror(errno
));
3571 if ((unsigned)read(fd
, super
->buf
+ 512, super
->len
- 512) != super
->len
- 512) {
3574 Name
": Cannot read extended mpb on %s: %s\n",
3575 devname
, strerror(errno
));
3579 check_sum
= __gen_imsm_checksum(super
->anchor
);
3580 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
3583 Name
": IMSM checksum %x != %x on %s\n",
3584 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
3589 /* FIXME the BBM log is disk specific so we cannot use this global
3590 * buffer for all disks. Ok for now since we only look at the global
3591 * bbm_log_size parameter to gate assembly
3593 super
->bbm_log
= __get_imsm_bbm_log(super
->anchor
);
3598 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
);
3600 /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
3601 static void clear_hi(struct intel_super
*super
)
3603 struct imsm_super
*mpb
= super
->anchor
;
3605 if (mpb
->attributes
& MPB_ATTRIB_2TB_DISK
)
3607 for (i
= 0; i
< mpb
->num_disks
; ++i
) {
3608 struct imsm_disk
*disk
= &mpb
->disk
[i
];
3609 disk
->total_blocks_hi
= 0;
3611 for (i
= 0; i
< mpb
->num_raid_devs
; ++i
) {
3612 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3615 for (n
= 0; n
< 2; ++n
) {
3616 struct imsm_map
*map
= get_imsm_map(dev
, n
);
3619 map
->pba_of_lba0_hi
= 0;
3620 map
->blocks_per_member_hi
= 0;
3621 map
->num_data_stripes_hi
= 0;
3627 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3631 err
= load_imsm_mpb(fd
, super
, devname
);
3634 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
3637 err
= parse_raid_devices(super
);
3642 static void __free_imsm_disk(struct dl
*d
)
3654 static void free_imsm_disks(struct intel_super
*super
)
3658 while (super
->disks
) {
3660 super
->disks
= d
->next
;
3661 __free_imsm_disk(d
);
3663 while (super
->disk_mgmt_list
) {
3664 d
= super
->disk_mgmt_list
;
3665 super
->disk_mgmt_list
= d
->next
;
3666 __free_imsm_disk(d
);
3668 while (super
->missing
) {
3670 super
->missing
= d
->next
;
3671 __free_imsm_disk(d
);
3676 /* free all the pieces hanging off of a super pointer */
3677 static void __free_imsm(struct intel_super
*super
, int free_disks
)
3679 struct intel_hba
*elem
, *next
;
3685 /* unlink capability description */
3687 if (super
->migr_rec_buf
) {
3688 free(super
->migr_rec_buf
);
3689 super
->migr_rec_buf
= NULL
;
3692 free_imsm_disks(super
);
3693 free_devlist(super
);
3697 free((void *)elem
->path
);
3705 static void free_imsm(struct intel_super
*super
)
3707 __free_imsm(super
, 1);
3711 static void free_super_imsm(struct supertype
*st
)
3713 struct intel_super
*super
= st
->sb
;
3722 static struct intel_super
*alloc_super(void)
3724 struct intel_super
*super
= malloc(sizeof(*super
));
3727 memset(super
, 0, sizeof(*super
));
3728 super
->current_vol
= -1;
3729 super
->create_offset
= ~((unsigned long long) 0);
3735 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
3737 static int find_intel_hba_capability(int fd
, struct intel_super
*super
, char *devname
)
3739 struct sys_dev
*hba_name
;
3742 if ((fd
< 0) || check_env("IMSM_NO_PLATFORM")) {
3747 hba_name
= find_disk_attached_hba(fd
, NULL
);
3751 Name
": %s is not attached to Intel(R) RAID controller.\n",
3755 rv
= attach_hba_to_super(super
, hba_name
);
3758 struct intel_hba
*hba
= super
->hba
;
3760 fprintf(stderr
, Name
": %s is attached to Intel(R) %s RAID "
3761 "controller (%s),\n"
3762 " but the container is assigned to Intel(R) "
3763 "%s RAID controller (",
3766 hba_name
->pci_id
? : "Err!",
3767 get_sys_dev_type(hba_name
->type
));
3770 fprintf(stderr
, "%s", hba
->pci_id
? : "Err!");
3772 fprintf(stderr
, ", ");
3776 fprintf(stderr
, ").\n"
3777 " Mixing devices attached to different controllers "
3778 "is not allowed.\n");
3780 free_sys_dev(&hba_name
);
3783 super
->orom
= find_imsm_capability(hba_name
->type
);
3784 free_sys_dev(&hba_name
);
3790 /* find_missing - helper routine for load_super_imsm_all that identifies
3791 * disks that have disappeared from the system. This routine relies on
3792 * the mpb being uptodate, which it is at load time.
3794 static int find_missing(struct intel_super
*super
)
3797 struct imsm_super
*mpb
= super
->anchor
;
3799 struct imsm_disk
*disk
;
3801 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3802 disk
= __get_imsm_disk(mpb
, i
);
3803 dl
= serial_to_dl(disk
->serial
, super
);
3807 dl
= malloc(sizeof(*dl
));
3813 dl
->devname
= strdup("missing");
3815 serialcpy(dl
->serial
, disk
->serial
);
3818 dl
->next
= super
->missing
;
3819 super
->missing
= dl
;
3826 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
3828 struct intel_disk
*idisk
= disk_list
;
3831 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
3833 idisk
= idisk
->next
;
3839 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
3840 struct intel_super
*super
,
3841 struct intel_disk
**disk_list
)
3843 struct imsm_disk
*d
= &super
->disks
->disk
;
3844 struct imsm_super
*mpb
= super
->anchor
;
3847 for (i
= 0; i
< tbl_size
; i
++) {
3848 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
3849 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
3851 if (tbl_mpb
->family_num
== mpb
->family_num
) {
3852 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
3853 dprintf("%s: mpb from %d:%d matches %d:%d\n",
3854 __func__
, super
->disks
->major
,
3855 super
->disks
->minor
,
3856 table
[i
]->disks
->major
,
3857 table
[i
]->disks
->minor
);
3861 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
3862 is_configured(d
) == is_configured(tbl_d
)) &&
3863 tbl_mpb
->generation_num
< mpb
->generation_num
) {
3864 /* current version of the mpb is a
3865 * better candidate than the one in
3866 * super_table, but copy over "cross
3867 * generational" status
3869 struct intel_disk
*idisk
;
3871 dprintf("%s: mpb from %d:%d replaces %d:%d\n",
3872 __func__
, super
->disks
->major
,
3873 super
->disks
->minor
,
3874 table
[i
]->disks
->major
,
3875 table
[i
]->disks
->minor
);
3877 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
3878 if (idisk
&& is_failed(&idisk
->disk
))
3879 tbl_d
->status
|= FAILED_DISK
;
3882 struct intel_disk
*idisk
;
3883 struct imsm_disk
*disk
;
3885 /* tbl_mpb is more up to date, but copy
3886 * over cross generational status before
3889 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
3890 if (disk
&& is_failed(disk
))
3891 d
->status
|= FAILED_DISK
;
3893 idisk
= disk_list_get(d
->serial
, *disk_list
);
3896 if (disk
&& is_configured(disk
))
3897 idisk
->disk
.status
|= CONFIGURED_DISK
;
3900 dprintf("%s: mpb from %d:%d prefer %d:%d\n",
3901 __func__
, super
->disks
->major
,
3902 super
->disks
->minor
,
3903 table
[i
]->disks
->major
,
3904 table
[i
]->disks
->minor
);
3912 table
[tbl_size
++] = super
;
3916 /* update/extend the merged list of imsm_disk records */
3917 for (j
= 0; j
< mpb
->num_disks
; j
++) {
3918 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
3919 struct intel_disk
*idisk
;
3921 idisk
= disk_list_get(disk
->serial
, *disk_list
);
3923 idisk
->disk
.status
|= disk
->status
;
3924 if (is_configured(&idisk
->disk
) ||
3925 is_failed(&idisk
->disk
))
3926 idisk
->disk
.status
&= ~(SPARE_DISK
);
3928 idisk
= calloc(1, sizeof(*idisk
));
3931 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
3932 idisk
->disk
= *disk
;
3933 idisk
->next
= *disk_list
;
3937 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
3944 static struct intel_super
*
3945 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
3948 struct imsm_super
*mpb
= super
->anchor
;
3952 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3953 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3954 struct intel_disk
*idisk
;
3956 idisk
= disk_list_get(disk
->serial
, disk_list
);
3958 if (idisk
->owner
== owner
||
3959 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
3962 dprintf("%s: '%.16s' owner %d != %d\n",
3963 __func__
, disk
->serial
, idisk
->owner
,
3966 dprintf("%s: unknown disk %x [%d]: %.16s\n",
3967 __func__
, __le32_to_cpu(mpb
->family_num
), i
,
3973 if (ok_count
== mpb
->num_disks
)
3978 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
3980 struct intel_super
*s
;
3982 for (s
= super_list
; s
; s
= s
->next
) {
3983 if (family_num
!= s
->anchor
->family_num
)
3985 fprintf(stderr
, "Conflict, offlining family %#x on '%s'\n",
3986 __le32_to_cpu(family_num
), s
->disks
->devname
);
3990 static struct intel_super
*
3991 imsm_thunderdome(struct intel_super
**super_list
, int len
)
3993 struct intel_super
*super_table
[len
];
3994 struct intel_disk
*disk_list
= NULL
;
3995 struct intel_super
*champion
, *spare
;
3996 struct intel_super
*s
, **del
;
4001 memset(super_table
, 0, sizeof(super_table
));
4002 for (s
= *super_list
; s
; s
= s
->next
)
4003 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
4005 for (i
= 0; i
< tbl_size
; i
++) {
4006 struct imsm_disk
*d
;
4007 struct intel_disk
*idisk
;
4008 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
4011 d
= &s
->disks
->disk
;
4013 /* 'd' must appear in merged disk list for its
4014 * configuration to be valid
4016 idisk
= disk_list_get(d
->serial
, disk_list
);
4017 if (idisk
&& idisk
->owner
== i
)
4018 s
= validate_members(s
, disk_list
, i
);
4023 dprintf("%s: marking family: %#x from %d:%d offline\n",
4024 __func__
, mpb
->family_num
,
4025 super_table
[i
]->disks
->major
,
4026 super_table
[i
]->disks
->minor
);
4030 /* This is where the mdadm implementation differs from the Windows
4031 * driver which has no strict concept of a container. We can only
4032 * assemble one family from a container, so when returning a prodigal
4033 * array member to this system the code will not be able to disambiguate
4034 * the container contents that should be assembled ("foreign" versus
4035 * "local"). It requires user intervention to set the orig_family_num
4036 * to a new value to establish a new container. The Windows driver in
4037 * this situation fixes up the volume name in place and manages the
4038 * foreign array as an independent entity.
4043 for (i
= 0; i
< tbl_size
; i
++) {
4044 struct intel_super
*tbl_ent
= super_table
[i
];
4050 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
4055 if (s
&& !is_spare
) {
4056 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
4058 } else if (!s
&& !is_spare
)
4071 fprintf(stderr
, "Chose family %#x on '%s', "
4072 "assemble conflicts to new container with '--update=uuid'\n",
4073 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
4075 /* collect all dl's onto 'champion', and update them to
4076 * champion's version of the status
4078 for (s
= *super_list
; s
; s
= s
->next
) {
4079 struct imsm_super
*mpb
= champion
->anchor
;
4080 struct dl
*dl
= s
->disks
;
4085 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4086 struct imsm_disk
*disk
;
4088 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
4091 /* only set index on disks that are a member of
4092 * a populated contianer, i.e. one with
4095 if (is_failed(&dl
->disk
))
4097 else if (is_spare(&dl
->disk
))
4103 if (i
>= mpb
->num_disks
) {
4104 struct intel_disk
*idisk
;
4106 idisk
= disk_list_get(dl
->serial
, disk_list
);
4107 if (idisk
&& is_spare(&idisk
->disk
) &&
4108 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
4116 dl
->next
= champion
->disks
;
4117 champion
->disks
= dl
;
4121 /* delete 'champion' from super_list */
4122 for (del
= super_list
; *del
; ) {
4123 if (*del
== champion
) {
4124 *del
= (*del
)->next
;
4127 del
= &(*del
)->next
;
4129 champion
->next
= NULL
;
4133 struct intel_disk
*idisk
= disk_list
;
4135 disk_list
= disk_list
->next
;
4144 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
);
4145 static int get_super_block(struct intel_super
**super_list
, int devnum
, char *devname
,
4146 int major
, int minor
, int keep_fd
);
4148 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4149 int *max
, int keep_fd
);
4152 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
4153 char *devname
, struct md_list
*devlist
,
4156 struct intel_super
*super_list
= NULL
;
4157 struct intel_super
*super
= NULL
;
4162 /* 'fd' is an opened container */
4163 err
= get_sra_super_block(fd
, &super_list
, devname
, &i
, keep_fd
);
4165 /* get super block from devlist devices */
4166 err
= get_devlist_super_block(devlist
, &super_list
, &i
, keep_fd
);
4169 /* all mpbs enter, maybe one leaves */
4170 super
= imsm_thunderdome(&super_list
, i
);
4176 if (find_missing(super
) != 0) {
4182 /* load migration record */
4183 err
= load_imsm_migr_rec(super
, NULL
);
4185 /* migration is in progress,
4186 * but migr_rec cannot be loaded,
4192 /* Check migration compatibility */
4193 if ((err
== 0) && (check_mpb_migr_compatibility(super
) != 0)) {
4194 fprintf(stderr
, Name
": Unsupported migration detected");
4196 fprintf(stderr
, " on %s\n", devname
);
4198 fprintf(stderr
, " (IMSM).\n");
4207 while (super_list
) {
4208 struct intel_super
*s
= super_list
;
4210 super_list
= super_list
->next
;
4220 st
->container_dev
= fd2devnum(fd
);
4222 st
->container_dev
= NoMdDev
;
4223 if (err
== 0 && st
->ss
== NULL
) {
4224 st
->ss
= &super_imsm
;
4225 st
->minor_version
= 0;
4226 st
->max_devs
= IMSM_MAX_DEVICES
;
4233 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4234 int *max
, int keep_fd
)
4236 struct md_list
*tmpdev
;
4240 for (i
= 0, tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
4241 if (tmpdev
->used
!= 1)
4243 if (tmpdev
->container
== 1) {
4245 int fd
= dev_open(tmpdev
->devname
, O_RDONLY
|O_EXCL
);
4247 fprintf(stderr
, Name
": cannot open device %s: %s\n",
4248 tmpdev
->devname
, strerror(errno
));
4252 err
= get_sra_super_block(fd
, super_list
,
4253 tmpdev
->devname
, &lmax
,
4262 int major
= major(tmpdev
->st_rdev
);
4263 int minor
= minor(tmpdev
->st_rdev
);
4264 err
= get_super_block(super_list
,
4281 static int get_super_block(struct intel_super
**super_list
, int devnum
, char *devname
,
4282 int major
, int minor
, int keep_fd
)
4284 struct intel_super
*s
= NULL
;
4297 sprintf(nm
, "%d:%d", major
, minor
);
4298 dfd
= dev_open(nm
, O_RDWR
);
4304 rv
= find_intel_hba_capability(dfd
, s
, devname
);
4305 /* no orom/efi or non-intel hba of the disk */
4311 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4313 /* retry the load if we might have raced against mdmon */
4314 if (err
== 3 && (devnum
!= -1) && mdmon_running(devnum
))
4315 for (retry
= 0; retry
< 3; retry
++) {
4317 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4323 s
->next
= *super_list
;
4331 if ((dfd
>= 0) && (!keep_fd
))
4338 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
)
4345 sra
= sysfs_read(fd
, 0, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
4349 if (sra
->array
.major_version
!= -1 ||
4350 sra
->array
.minor_version
!= -2 ||
4351 strcmp(sra
->text_version
, "imsm") != 0) {
4356 devnum
= fd2devnum(fd
);
4357 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
4358 if (get_super_block(super_list
, devnum
, devname
,
4359 sd
->disk
.major
, sd
->disk
.minor
, keep_fd
) != 0) {
4370 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
4372 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
, NULL
, 1);
4376 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
4378 struct intel_super
*super
;
4381 if (test_partition(fd
))
4382 /* IMSM not allowed on partitions */
4385 free_super_imsm(st
);
4387 super
= alloc_super();
4390 Name
": malloc of %zu failed.\n",
4394 /* Load hba and capabilities if they exist.
4395 * But do not preclude loading metadata in case capabilities or hba are
4396 * non-compliant and ignore_hw_compat is set.
4398 rv
= find_intel_hba_capability(fd
, super
, devname
);
4399 /* no orom/efi or non-intel hba of the disk */
4400 if ((rv
!= 0) && (st
->ignore_hw_compat
== 0)) {
4403 Name
": No OROM/EFI properties for %s\n", devname
);
4407 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
4412 Name
": Failed to load all information "
4413 "sections on %s\n", devname
);
4419 if (st
->ss
== NULL
) {
4420 st
->ss
= &super_imsm
;
4421 st
->minor_version
= 0;
4422 st
->max_devs
= IMSM_MAX_DEVICES
;
4425 /* load migration record */
4426 if (load_imsm_migr_rec(super
, NULL
) == 0) {
4427 /* Check for unsupported migration features */
4428 if (check_mpb_migr_compatibility(super
) != 0) {
4430 Name
": Unsupported migration detected");
4432 fprintf(stderr
, " on %s\n", devname
);
4434 fprintf(stderr
, " (IMSM).\n");
4442 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
4444 if (info
->level
== 1)
4446 return info
->chunk_size
>> 9;
4449 static unsigned long long info_to_blocks_per_member(mdu_array_info_t
*info
,
4450 unsigned long long size
)
4452 if (info
->level
== 1)
4455 return (size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
4458 static void imsm_update_version_info(struct intel_super
*super
)
4460 /* update the version and attributes */
4461 struct imsm_super
*mpb
= super
->anchor
;
4463 struct imsm_dev
*dev
;
4464 struct imsm_map
*map
;
4467 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4468 dev
= get_imsm_dev(super
, i
);
4469 map
= get_imsm_map(dev
, MAP_0
);
4470 if (__le32_to_cpu(dev
->size_high
) > 0)
4471 mpb
->attributes
|= MPB_ATTRIB_2TB
;
4473 /* FIXME detect when an array spans a port multiplier */
4475 mpb
->attributes
|= MPB_ATTRIB_PM
;
4478 if (mpb
->num_raid_devs
> 1 ||
4479 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
4480 version
= MPB_VERSION_ATTRIBS
;
4481 switch (get_imsm_raid_level(map
)) {
4482 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
4483 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
4484 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
4485 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
4488 if (map
->num_members
>= 5)
4489 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
4490 else if (dev
->status
== DEV_CLONE_N_GO
)
4491 version
= MPB_VERSION_CNG
;
4492 else if (get_imsm_raid_level(map
) == 5)
4493 version
= MPB_VERSION_RAID5
;
4494 else if (map
->num_members
>= 3)
4495 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
4496 else if (get_imsm_raid_level(map
) == 1)
4497 version
= MPB_VERSION_RAID1
;
4499 version
= MPB_VERSION_RAID0
;
4501 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
4505 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
4507 struct imsm_super
*mpb
= super
->anchor
;
4508 char *reason
= NULL
;
4511 if (strlen(name
) > MAX_RAID_SERIAL_LEN
)
4512 reason
= "must be 16 characters or less";
4514 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4515 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
4517 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
4518 reason
= "already exists";
4523 if (reason
&& !quiet
)
4524 fprintf(stderr
, Name
": imsm volume name %s\n", reason
);
4529 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
4530 unsigned long long size
, char *name
,
4531 char *homehost
, int *uuid
)
4533 /* We are creating a volume inside a pre-existing container.
4534 * so st->sb is already set.
4536 struct intel_super
*super
= st
->sb
;
4537 struct imsm_super
*mpb
= super
->anchor
;
4538 struct intel_dev
*dv
;
4539 struct imsm_dev
*dev
;
4540 struct imsm_vol
*vol
;
4541 struct imsm_map
*map
;
4542 int idx
= mpb
->num_raid_devs
;
4544 unsigned long long array_blocks
;
4545 size_t size_old
, size_new
;
4546 unsigned long long num_data_stripes
;
4548 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
4549 fprintf(stderr
, Name
": This imsm-container already has the "
4550 "maximum of %d volumes\n", super
->orom
->vpa
);
4554 /* ensure the mpb is large enough for the new data */
4555 size_old
= __le32_to_cpu(mpb
->mpb_size
);
4556 size_new
= disks_to_mpb_size(info
->nr_disks
);
4557 if (size_new
> size_old
) {
4559 size_t size_round
= ROUND_UP(size_new
, 512);
4561 if (posix_memalign(&mpb_new
, 512, size_round
) != 0) {
4562 fprintf(stderr
, Name
": could not allocate new mpb\n");
4565 if (posix_memalign(&super
->migr_rec_buf
, 512,
4566 MIGR_REC_BUF_SIZE
) != 0) {
4567 fprintf(stderr
, Name
4568 ": %s could not allocate migr_rec buffer\n",
4575 memcpy(mpb_new
, mpb
, size_old
);
4578 super
->anchor
= mpb_new
;
4579 mpb
->mpb_size
= __cpu_to_le32(size_new
);
4580 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
4582 super
->current_vol
= idx
;
4584 /* handle 'failed_disks' by either:
4585 * a) create dummy disk entries in the table if this the first
4586 * volume in the array. We add them here as this is the only
4587 * opportunity to add them. add_to_super_imsm_volume()
4588 * handles the non-failed disks and continues incrementing
4590 * b) validate that 'failed_disks' matches the current number
4591 * of missing disks if the container is populated
4593 if (super
->current_vol
== 0) {
4595 for (i
= 0; i
< info
->failed_disks
; i
++) {
4596 struct imsm_disk
*disk
;
4599 disk
= __get_imsm_disk(mpb
, i
);
4600 disk
->status
= CONFIGURED_DISK
| FAILED_DISK
;
4601 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
4602 snprintf((char *) disk
->serial
, MAX_RAID_SERIAL_LEN
,
4605 find_missing(super
);
4610 for (d
= super
->missing
; d
; d
= d
->next
)
4612 if (info
->failed_disks
> missing
) {
4613 fprintf(stderr
, Name
": unable to add 'missing' disk to container\n");
4618 if (!check_name(super
, name
, 0))
4620 dv
= malloc(sizeof(*dv
));
4622 fprintf(stderr
, Name
": failed to allocate device list entry\n");
4625 dev
= calloc(1, sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
4628 fprintf(stderr
, Name
": could not allocate raid device\n");
4632 strncpy((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
4633 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
4634 info
->layout
, info
->chunk_size
,
4636 /* round array size down to closest MB */
4637 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
4639 dev
->size_low
= __cpu_to_le32((__u32
) array_blocks
);
4640 dev
->size_high
= __cpu_to_le32((__u32
) (array_blocks
>> 32));
4641 dev
->status
= (DEV_READ_COALESCING
| DEV_WRITE_COALESCING
);
4643 vol
->migr_state
= 0;
4644 set_migr_type(dev
, MIGR_INIT
);
4645 vol
->dirty
= !info
->state
;
4646 vol
->curr_migr_unit
= 0;
4647 map
= get_imsm_map(dev
, MAP_0
);
4648 set_pba_of_lba0(map
, super
->create_offset
);
4649 set_blocks_per_member(map
, info_to_blocks_per_member(info
, size
));
4650 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
4651 map
->failed_disk_num
= ~0;
4652 if (info
->level
> 0)
4653 map
->map_state
= IMSM_T_STATE_UNINITIALIZED
;
4655 map
->map_state
= info
->failed_disks
? IMSM_T_STATE_FAILED
:
4656 IMSM_T_STATE_NORMAL
;
4659 if (info
->level
== 1 && info
->raid_disks
> 2) {
4662 fprintf(stderr
, Name
": imsm does not support more than 2 disks"
4663 "in a raid1 volume\n");
4667 map
->raid_level
= info
->level
;
4668 if (info
->level
== 10) {
4669 map
->raid_level
= 1;
4670 map
->num_domains
= info
->raid_disks
/ 2;
4671 } else if (info
->level
== 1)
4672 map
->num_domains
= info
->raid_disks
;
4674 map
->num_domains
= 1;
4676 /* info->size is only int so use the 'size' parameter instead */
4677 num_data_stripes
= (size
* 2) / info_to_blocks_per_strip(info
);
4678 num_data_stripes
/= map
->num_domains
;
4679 set_num_data_stripes(map
, num_data_stripes
);
4681 map
->num_members
= info
->raid_disks
;
4682 for (i
= 0; i
< map
->num_members
; i
++) {
4683 /* initialized in add_to_super */
4684 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
4686 mpb
->num_raid_devs
++;
4689 dv
->index
= super
->current_vol
;
4690 dv
->next
= super
->devlist
;
4691 super
->devlist
= dv
;
4693 imsm_update_version_info(super
);
4698 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
4699 unsigned long long size
, char *name
,
4700 char *homehost
, int *uuid
)
4702 /* This is primarily called by Create when creating a new array.
4703 * We will then get add_to_super called for each component, and then
4704 * write_init_super called to write it out to each device.
4705 * For IMSM, Create can create on fresh devices or on a pre-existing
4707 * To create on a pre-existing array a different method will be called.
4708 * This one is just for fresh drives.
4710 struct intel_super
*super
;
4711 struct imsm_super
*mpb
;
4716 return init_super_imsm_volume(st
, info
, size
, name
, homehost
, uuid
);
4719 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
4723 super
= alloc_super();
4724 if (super
&& posix_memalign(&super
->buf
, 512, mpb_size
) != 0) {
4729 fprintf(stderr
, Name
4730 ": %s could not allocate superblock\n", __func__
);
4733 if (posix_memalign(&super
->migr_rec_buf
, 512, MIGR_REC_BUF_SIZE
) != 0) {
4734 fprintf(stderr
, Name
4735 ": %s could not allocate migr_rec buffer\n", __func__
);
4740 memset(super
->buf
, 0, mpb_size
);
4742 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
4746 /* zeroing superblock */
4750 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
4752 version
= (char *) mpb
->sig
;
4753 strcpy(version
, MPB_SIGNATURE
);
4754 version
+= strlen(MPB_SIGNATURE
);
4755 strcpy(version
, MPB_VERSION_RAID0
);
4761 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
4762 int fd
, char *devname
)
4764 struct intel_super
*super
= st
->sb
;
4765 struct imsm_super
*mpb
= super
->anchor
;
4766 struct imsm_disk
*_disk
;
4767 struct imsm_dev
*dev
;
4768 struct imsm_map
*map
;
4772 dev
= get_imsm_dev(super
, super
->current_vol
);
4773 map
= get_imsm_map(dev
, MAP_0
);
4775 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
4776 fprintf(stderr
, Name
": %s: Cannot add spare devices to IMSM volume\n",
4782 /* we're doing autolayout so grab the pre-marked (in
4783 * validate_geometry) raid_disk
4785 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4786 if (dl
->raiddisk
== dk
->raid_disk
)
4789 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4790 if (dl
->major
== dk
->major
&&
4791 dl
->minor
== dk
->minor
)
4796 fprintf(stderr
, Name
": %s is not a member of the same container\n", devname
);
4800 /* add a pristine spare to the metadata */
4801 if (dl
->index
< 0) {
4802 dl
->index
= super
->anchor
->num_disks
;
4803 super
->anchor
->num_disks
++;
4805 /* Check the device has not already been added */
4806 slot
= get_imsm_disk_slot(map
, dl
->index
);
4808 (get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
) & IMSM_ORD_REBUILD
) == 0) {
4809 fprintf(stderr
, Name
": %s has been included in this array twice\n",
4813 set_imsm_ord_tbl_ent(map
, dk
->raid_disk
, dl
->index
);
4814 dl
->disk
.status
= CONFIGURED_DISK
;
4816 /* update size of 'missing' disks to be at least as large as the
4817 * largest acitve member (we only have dummy missing disks when
4818 * creating the first volume)
4820 if (super
->current_vol
== 0) {
4821 for (df
= super
->missing
; df
; df
= df
->next
) {
4822 if (total_blocks(&dl
->disk
) > total_blocks(&df
->disk
))
4823 set_total_blocks(&df
->disk
, total_blocks(&dl
->disk
));
4824 _disk
= __get_imsm_disk(mpb
, df
->index
);
4829 /* refresh unset/failed slots to point to valid 'missing' entries */
4830 for (df
= super
->missing
; df
; df
= df
->next
)
4831 for (slot
= 0; slot
< mpb
->num_disks
; slot
++) {
4832 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
4834 if ((ord
& IMSM_ORD_REBUILD
) == 0)
4836 set_imsm_ord_tbl_ent(map
, slot
, df
->index
| IMSM_ORD_REBUILD
);
4837 if (is_gen_migration(dev
)) {
4838 struct imsm_map
*map2
= get_imsm_map(dev
,
4840 int slot2
= get_imsm_disk_slot(map2
, df
->index
);
4841 if ((slot2
< map2
->num_members
) &&
4843 __u32 ord2
= get_imsm_ord_tbl_ent(dev
,
4846 if ((unsigned)df
->index
==
4848 set_imsm_ord_tbl_ent(map2
,
4854 dprintf("set slot:%d to missing disk:%d\n", slot
, df
->index
);
4858 /* if we are creating the first raid device update the family number */
4859 if (super
->current_vol
== 0) {
4861 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
4863 _disk
= __get_imsm_disk(mpb
, dl
->index
);
4864 if (!_dev
|| !_disk
) {
4865 fprintf(stderr
, Name
": BUG mpb setup error\n");
4871 sum
+= __gen_imsm_checksum(mpb
);
4872 mpb
->family_num
= __cpu_to_le32(sum
);
4873 mpb
->orig_family_num
= mpb
->family_num
;
4875 super
->current_disk
= dl
;
4880 * Function marks disk as spare and restores disk serial
4881 * in case it was previously marked as failed by takeover operation
4883 * -1 : critical error
4884 * 0 : disk is marked as spare but serial is not set
4887 int mark_spare(struct dl
*disk
)
4889 __u8 serial
[MAX_RAID_SERIAL_LEN
];
4896 if (!imsm_read_serial(disk
->fd
, NULL
, serial
)) {
4897 /* Restore disk serial number, because takeover marks disk
4898 * as failed and adds to serial ':0' before it becomes
4901 serialcpy(disk
->serial
, serial
);
4902 serialcpy(disk
->disk
.serial
, serial
);
4905 disk
->disk
.status
= SPARE_DISK
;
4911 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
4912 int fd
, char *devname
)
4914 struct intel_super
*super
= st
->sb
;
4916 unsigned long long size
;
4921 /* If we are on an RAID enabled platform check that the disk is
4922 * attached to the raid controller.
4923 * We do not need to test disks attachment for container based additions,
4924 * they shall be already tested when container was created/assembled.
4926 rv
= find_intel_hba_capability(fd
, super
, devname
);
4927 /* no orom/efi or non-intel hba of the disk */
4929 dprintf("capability: %p fd: %d ret: %d\n",
4930 super
->orom
, fd
, rv
);
4934 if (super
->current_vol
>= 0)
4935 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
4938 dd
= malloc(sizeof(*dd
));
4941 Name
": malloc failed %s:%d.\n", __func__
, __LINE__
);
4944 memset(dd
, 0, sizeof(*dd
));
4945 dd
->major
= major(stb
.st_rdev
);
4946 dd
->minor
= minor(stb
.st_rdev
);
4947 dd
->devname
= devname
? strdup(devname
) : NULL
;
4950 dd
->action
= DISK_ADD
;
4951 rv
= imsm_read_serial(fd
, devname
, dd
->serial
);
4954 Name
": failed to retrieve scsi serial, aborting\n");
4959 get_dev_size(fd
, NULL
, &size
);
4961 serialcpy(dd
->disk
.serial
, dd
->serial
);
4962 set_total_blocks(&dd
->disk
, size
);
4963 if (__le32_to_cpu(dd
->disk
.total_blocks_hi
) > 0) {
4964 struct imsm_super
*mpb
= super
->anchor
;
4965 mpb
->attributes
|= MPB_ATTRIB_2TB_DISK
;
4968 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
4969 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
4971 dd
->disk
.scsi_id
= __cpu_to_le32(0);
4973 if (st
->update_tail
) {
4974 dd
->next
= super
->disk_mgmt_list
;
4975 super
->disk_mgmt_list
= dd
;
4977 dd
->next
= super
->disks
;
4979 super
->updates_pending
++;
4986 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
4988 struct intel_super
*super
= st
->sb
;
4991 /* remove from super works only in mdmon - for communication
4992 * manager - monitor. Check if communication memory buffer
4995 if (!st
->update_tail
) {
4997 Name
": %s shall be used in mdmon context only"
4998 "(line %d).\n", __func__
, __LINE__
);
5001 dd
= malloc(sizeof(*dd
));
5004 Name
": malloc failed %s:%d.\n", __func__
, __LINE__
);
5007 memset(dd
, 0, sizeof(*dd
));
5008 dd
->major
= dk
->major
;
5009 dd
->minor
= dk
->minor
;
5012 dd
->action
= DISK_REMOVE
;
5014 dd
->next
= super
->disk_mgmt_list
;
5015 super
->disk_mgmt_list
= dd
;
5021 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
5025 struct imsm_super anchor
;
5026 } spare_record
__attribute__ ((aligned(512)));
5028 /* spare records have their own family number and do not have any defined raid
5031 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
5033 struct imsm_super
*mpb
= super
->anchor
;
5034 struct imsm_super
*spare
= &spare_record
.anchor
;
5038 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
)),
5039 spare
->generation_num
= __cpu_to_le32(1UL),
5040 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
5041 spare
->num_disks
= 1,
5042 spare
->num_raid_devs
= 0,
5043 spare
->cache_size
= mpb
->cache_size
,
5044 spare
->pwr_cycle_count
= __cpu_to_le32(1),
5046 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
5047 MPB_SIGNATURE MPB_VERSION_RAID0
);
5049 for (d
= super
->disks
; d
; d
= d
->next
) {
5053 spare
->disk
[0] = d
->disk
;
5054 if (__le32_to_cpu(d
->disk
.total_blocks_hi
) > 0)
5055 spare
->attributes
|= MPB_ATTRIB_2TB_DISK
;
5057 sum
= __gen_imsm_checksum(spare
);
5058 spare
->family_num
= __cpu_to_le32(sum
);
5059 spare
->orig_family_num
= 0;
5060 sum
= __gen_imsm_checksum(spare
);
5061 spare
->check_sum
= __cpu_to_le32(sum
);
5063 if (store_imsm_mpb(d
->fd
, spare
)) {
5064 fprintf(stderr
, "%s: failed for device %d:%d %s\n",
5065 __func__
, d
->major
, d
->minor
, strerror(errno
));
5077 static int write_super_imsm(struct supertype
*st
, int doclose
)
5079 struct intel_super
*super
= st
->sb
;
5080 struct imsm_super
*mpb
= super
->anchor
;
5086 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
5088 int clear_migration_record
= 1;
5090 /* 'generation' is incremented everytime the metadata is written */
5091 generation
= __le32_to_cpu(mpb
->generation_num
);
5093 mpb
->generation_num
= __cpu_to_le32(generation
);
5095 /* fix up cases where previous mdadm releases failed to set
5098 if (mpb
->orig_family_num
== 0)
5099 mpb
->orig_family_num
= mpb
->family_num
;
5101 for (d
= super
->disks
; d
; d
= d
->next
) {
5105 mpb
->disk
[d
->index
] = d
->disk
;
5109 for (d
= super
->missing
; d
; d
= d
->next
) {
5110 mpb
->disk
[d
->index
] = d
->disk
;
5113 mpb
->num_disks
= num_disks
;
5114 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
5116 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5117 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
5118 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
5120 imsm_copy_dev(dev
, dev2
);
5121 mpb_size
+= sizeof_imsm_dev(dev
, 0);
5123 if (is_gen_migration(dev2
))
5124 clear_migration_record
= 0;
5126 mpb_size
+= __le32_to_cpu(mpb
->bbm_log_size
);
5127 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5129 /* recalculate checksum */
5130 sum
= __gen_imsm_checksum(mpb
);
5131 mpb
->check_sum
= __cpu_to_le32(sum
);
5133 if (super
->clean_migration_record_by_mdmon
) {
5134 clear_migration_record
= 1;
5135 super
->clean_migration_record_by_mdmon
= 0;
5137 if (clear_migration_record
)
5138 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SIZE
);
5140 /* write the mpb for disks that compose raid devices */
5141 for (d
= super
->disks
; d
; d
= d
->next
) {
5142 if (d
->index
< 0 || is_failed(&d
->disk
))
5145 if (clear_migration_record
) {
5146 unsigned long long dsize
;
5148 get_dev_size(d
->fd
, NULL
, &dsize
);
5149 if (lseek64(d
->fd
, dsize
- 512, SEEK_SET
) >= 0) {
5150 if (write(d
->fd
, super
->migr_rec_buf
,
5151 MIGR_REC_BUF_SIZE
) != MIGR_REC_BUF_SIZE
)
5152 perror("Write migr_rec failed");
5156 if (store_imsm_mpb(d
->fd
, mpb
))
5158 "%s: failed for device %d:%d (fd: %d)%s\n",
5159 __func__
, d
->major
, d
->minor
,
5160 d
->fd
, strerror(errno
));
5169 return write_super_imsm_spares(super
, doclose
);
5175 static int create_array(struct supertype
*st
, int dev_idx
)
5178 struct imsm_update_create_array
*u
;
5179 struct intel_super
*super
= st
->sb
;
5180 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
5181 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
5182 struct disk_info
*inf
;
5183 struct imsm_disk
*disk
;
5186 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
5187 sizeof(*inf
) * map
->num_members
;
5190 fprintf(stderr
, "%s: failed to allocate update buffer\n",
5195 u
->type
= update_create_array
;
5196 u
->dev_idx
= dev_idx
;
5197 imsm_copy_dev(&u
->dev
, dev
);
5198 inf
= get_disk_info(u
);
5199 for (i
= 0; i
< map
->num_members
; i
++) {
5200 int idx
= get_imsm_disk_idx(dev
, i
, MAP_X
);
5202 disk
= get_imsm_disk(super
, idx
);
5203 serialcpy(inf
[i
].serial
, disk
->serial
);
5205 append_metadata_update(st
, u
, len
);
5210 static int mgmt_disk(struct supertype
*st
)
5212 struct intel_super
*super
= st
->sb
;
5214 struct imsm_update_add_remove_disk
*u
;
5216 if (!super
->disk_mgmt_list
)
5222 fprintf(stderr
, "%s: failed to allocate update buffer\n",
5227 u
->type
= update_add_remove_disk
;
5228 append_metadata_update(st
, u
, len
);
5233 static int write_init_super_imsm(struct supertype
*st
)
5235 struct intel_super
*super
= st
->sb
;
5236 int current_vol
= super
->current_vol
;
5238 /* we are done with current_vol reset it to point st at the container */
5239 super
->current_vol
= -1;
5241 if (st
->update_tail
) {
5242 /* queue the recently created array / added disk
5243 * as a metadata update */
5246 /* determine if we are creating a volume or adding a disk */
5247 if (current_vol
< 0) {
5248 /* in the mgmt (add/remove) disk case we are running
5249 * in mdmon context, so don't close fd's
5251 return mgmt_disk(st
);
5253 rv
= create_array(st
, current_vol
);
5258 for (d
= super
->disks
; d
; d
= d
->next
)
5259 Kill(d
->devname
, NULL
, 0, 1, 1);
5260 return write_super_imsm(st
, 1);
5265 static int store_super_imsm(struct supertype
*st
, int fd
)
5267 struct intel_super
*super
= st
->sb
;
5268 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
5274 return store_imsm_mpb(fd
, mpb
);
5280 static int imsm_bbm_log_size(struct imsm_super
*mpb
)
5282 return __le32_to_cpu(mpb
->bbm_log_size
);
5286 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
5287 int layout
, int raiddisks
, int chunk
,
5288 unsigned long long size
, char *dev
,
5289 unsigned long long *freesize
,
5293 unsigned long long ldsize
;
5294 struct intel_super
*super
=NULL
;
5297 if (level
!= LEVEL_CONTAINER
)
5302 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
5305 fprintf(stderr
, Name
": imsm: Cannot open %s: %s\n",
5306 dev
, strerror(errno
));
5309 if (!get_dev_size(fd
, dev
, &ldsize
)) {
5314 /* capabilities retrieve could be possible
5315 * note that there is no fd for the disks in array.
5317 super
= alloc_super();
5320 Name
": malloc of %zu failed.\n",
5326 rv
= find_intel_hba_capability(fd
, super
, verbose
? dev
: NULL
);
5330 fd2devname(fd
, str
);
5331 dprintf("validate_geometry_imsm_container: fd: %d %s orom: %p rv: %d raiddisk: %d\n",
5332 fd
, str
, super
->orom
, rv
, raiddisks
);
5334 /* no orom/efi or non-intel hba of the disk */
5341 if (raiddisks
> super
->orom
->tds
) {
5343 fprintf(stderr
, Name
": %d exceeds maximum number of"
5344 " platform supported disks: %d\n",
5345 raiddisks
, super
->orom
->tds
);
5349 if ((super
->orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) == 0 &&
5350 (ldsize
>> 9) >> 32 > 0) {
5352 fprintf(stderr
, Name
": %s exceeds maximum platform supported size\n", dev
);
5358 *freesize
= avail_size_imsm(st
, ldsize
>> 9);
5364 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
5366 const unsigned long long base_start
= e
[*idx
].start
;
5367 unsigned long long end
= base_start
+ e
[*idx
].size
;
5370 if (base_start
== end
)
5374 for (i
= *idx
; i
< num_extents
; i
++) {
5375 /* extend overlapping extents */
5376 if (e
[i
].start
>= base_start
&&
5377 e
[i
].start
<= end
) {
5380 if (e
[i
].start
+ e
[i
].size
> end
)
5381 end
= e
[i
].start
+ e
[i
].size
;
5382 } else if (e
[i
].start
> end
) {
5388 return end
- base_start
;
5391 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
5393 /* build a composite disk with all known extents and generate a new
5394 * 'maxsize' given the "all disks in an array must share a common start
5395 * offset" constraint
5397 struct extent
*e
= calloc(sum_extents
, sizeof(*e
));
5401 unsigned long long pos
;
5402 unsigned long long start
= 0;
5403 unsigned long long maxsize
;
5404 unsigned long reserve
;
5409 /* coalesce and sort all extents. also, check to see if we need to
5410 * reserve space between member arrays
5413 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5416 for (i
= 0; i
< dl
->extent_cnt
; i
++)
5419 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
5424 while (i
< sum_extents
) {
5425 e
[j
].start
= e
[i
].start
;
5426 e
[j
].size
= find_size(e
, &i
, sum_extents
);
5428 if (e
[j
-1].size
== 0)
5437 unsigned long long esize
;
5439 esize
= e
[i
].start
- pos
;
5440 if (esize
>= maxsize
) {
5445 pos
= e
[i
].start
+ e
[i
].size
;
5447 } while (e
[i
-1].size
);
5453 /* FIXME assumes volume at offset 0 is the first volume in a
5456 if (start_extent
> 0)
5457 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
5461 if (maxsize
< reserve
)
5464 super
->create_offset
= ~((unsigned long long) 0);
5465 if (start
+ reserve
> super
->create_offset
)
5466 return 0; /* start overflows create_offset */
5467 super
->create_offset
= start
+ reserve
;
5469 return maxsize
- reserve
;
5472 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
5474 if (level
< 0 || level
== 6 || level
== 4)
5477 /* if we have an orom prevent invalid raid levels */
5480 case 0: return imsm_orom_has_raid0(orom
);
5483 return imsm_orom_has_raid1e(orom
);
5484 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
5485 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
5486 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
5489 return 1; /* not on an Intel RAID platform so anything goes */
5496 active_arrays_by_format(char *name
, char* hba
, struct md_list
**devlist
,
5497 int dpa
, int verbose
)
5499 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
5500 struct mdstat_ent
*memb
= NULL
;
5503 struct md_list
*dv
= NULL
;
5506 for (memb
= mdstat
; memb
; memb
= memb
->next
) {
5507 if (memb
->metadata_version
&&
5508 (strncmp(memb
->metadata_version
, "external:", 9) == 0) &&
5509 (strcmp(&memb
->metadata_version
[9], name
) == 0) &&
5510 !is_subarray(memb
->metadata_version
+9) &&
5512 struct dev_member
*dev
= memb
->members
;
5514 while(dev
&& (fd
< 0)) {
5515 char *path
= malloc(strlen(dev
->name
) + strlen("/dev/") + 1);
5517 num
= sprintf(path
, "%s%s", "/dev/", dev
->name
);
5519 fd
= open(path
, O_RDONLY
, 0);
5520 if ((num
<= 0) || (fd
< 0)) {
5521 pr_vrb(": Cannot open %s: %s\n",
5522 dev
->name
, strerror(errno
));
5529 if ((fd
>= 0) && disk_attached_to_hba(fd
, hba
)) {
5530 struct mdstat_ent
*vol
;
5531 for (vol
= mdstat
; vol
; vol
= vol
->next
) {
5532 if ((vol
->active
> 0) &&
5533 vol
->metadata_version
&&
5534 is_container_member(vol
, memb
->dev
)) {
5539 if (*devlist
&& (found
< dpa
)) {
5540 dv
= calloc(1, sizeof(*dv
));
5542 fprintf(stderr
, Name
": calloc failed\n");
5544 dv
->devname
= malloc(strlen(memb
->dev
) + strlen("/dev/") + 1);
5545 if (dv
->devname
!= NULL
) {
5546 sprintf(dv
->devname
, "%s%s", "/dev/", memb
->dev
);
5549 dv
->next
= *devlist
;
5560 free_mdstat(mdstat
);
5565 static struct md_list
*
5566 get_loop_devices(void)
5569 struct md_list
*devlist
= NULL
;
5570 struct md_list
*dv
= NULL
;
5572 for(i
= 0; i
< 12; i
++) {
5573 dv
= calloc(1, sizeof(*dv
));
5575 fprintf(stderr
, Name
": calloc failed\n");
5578 dv
->devname
= malloc(40);
5579 if (dv
->devname
== NULL
) {
5580 fprintf(stderr
, Name
": malloc failed\n");
5584 sprintf(dv
->devname
, "/dev/loop%d", i
);
5592 static struct md_list
*
5593 get_devices(const char *hba_path
)
5595 struct md_list
*devlist
= NULL
;
5596 struct md_list
*dv
= NULL
;
5602 devlist
= get_loop_devices();
5605 /* scroll through /sys/dev/block looking for devices attached to
5608 dir
= opendir("/sys/dev/block");
5609 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
5614 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
5616 path
= devt_to_devpath(makedev(major
, minor
));
5619 if (!path_attached_to_hba(path
, hba_path
)) {
5626 fd
= dev_open(ent
->d_name
, O_RDONLY
);
5628 fd2devname(fd
, buf
);
5631 fprintf(stderr
, Name
": cannot open device: %s\n",
5637 dv
= calloc(1, sizeof(*dv
));
5639 fprintf(stderr
, Name
": malloc failed\n");
5643 dv
->devname
= strdup(buf
);
5644 if (dv
->devname
== NULL
) {
5645 fprintf(stderr
, Name
": malloc failed\n");
5656 devlist
= devlist
->next
;
5665 count_volumes_list(struct md_list
*devlist
, char *homehost
,
5666 int verbose
, int *found
)
5668 struct md_list
*tmpdev
;
5670 struct supertype
*st
= NULL
;
5672 /* first walk the list of devices to find a consistent set
5673 * that match the criterea, if that is possible.
5674 * We flag the ones we like with 'used'.
5677 st
= match_metadata_desc_imsm("imsm");
5679 pr_vrb(": cannot allocate memory for imsm supertype\n");
5683 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5684 char *devname
= tmpdev
->devname
;
5686 struct supertype
*tst
;
5688 if (tmpdev
->used
> 1)
5690 tst
= dup_super(st
);
5692 pr_vrb(": cannot allocate memory for imsm supertype\n");
5695 tmpdev
->container
= 0;
5696 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
5698 dprintf(": cannot open device %s: %s\n",
5699 devname
, strerror(errno
));
5701 } else if (fstat(dfd
, &stb
)< 0) {
5703 dprintf(": fstat failed for %s: %s\n",
5704 devname
, strerror(errno
));
5706 } else if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
5707 dprintf(": %s is not a block device.\n",
5710 } else if (must_be_container(dfd
)) {
5711 struct supertype
*cst
;
5712 cst
= super_by_fd(dfd
, NULL
);
5714 dprintf(": cannot recognize container type %s\n",
5717 } else if (tst
->ss
!= st
->ss
) {
5718 dprintf(": non-imsm container - ignore it: %s\n",
5721 } else if (!tst
->ss
->load_container
||
5722 tst
->ss
->load_container(tst
, dfd
, NULL
))
5725 tmpdev
->container
= 1;
5728 cst
->ss
->free_super(cst
);
5730 tmpdev
->st_rdev
= stb
.st_rdev
;
5731 if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
5732 dprintf(": no RAID superblock on %s\n",
5735 } else if (tst
->ss
->compare_super
== NULL
) {
5736 dprintf(": Cannot assemble %s metadata on %s\n",
5737 tst
->ss
->name
, devname
);
5743 if (tmpdev
->used
== 2 || tmpdev
->used
== 4) {
5744 /* Ignore unrecognised devices during auto-assembly */
5749 tst
->ss
->getinfo_super(tst
, &info
, NULL
);
5751 if (st
->minor_version
== -1)
5752 st
->minor_version
= tst
->minor_version
;
5754 if (memcmp(info
.uuid
, uuid_zero
,
5755 sizeof(int[4])) == 0) {
5756 /* this is a floating spare. It cannot define
5757 * an array unless there are no more arrays of
5758 * this type to be found. It can be included
5759 * in an array of this type though.
5765 if (st
->ss
!= tst
->ss
||
5766 st
->minor_version
!= tst
->minor_version
||
5767 st
->ss
->compare_super(st
, tst
) != 0) {
5768 /* Some mismatch. If exactly one array matches this host,
5769 * we can resolve on that one.
5770 * Or, if we are auto assembling, we just ignore the second
5773 dprintf(": superblock on %s doesn't match others - assembly aborted\n",
5779 dprintf("found: devname: %s\n", devname
);
5783 tst
->ss
->free_super(tst
);
5787 if ((err
= load_super_imsm_all(st
, -1, &st
->sb
, NULL
, devlist
, 0)) == 0) {
5788 struct mdinfo
*iter
, *head
= st
->ss
->container_content(st
, NULL
);
5789 for (iter
= head
; iter
; iter
= iter
->next
) {
5790 dprintf("content->text_version: %s vol\n",
5791 iter
->text_version
);
5792 if (iter
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
5793 /* do not assemble arrays with unsupported
5795 dprintf(": Cannot activate member %s.\n",
5796 iter
->text_version
);
5803 dprintf(" no valid super block on device list: err: %d %p\n",
5807 dprintf(" no more devices to examin\n");
5810 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5811 if ((tmpdev
->used
== 1) && (tmpdev
->found
)) {
5813 if (count
< tmpdev
->found
)
5816 count
-= tmpdev
->found
;
5819 if (tmpdev
->used
== 1)
5824 st
->ss
->free_super(st
);
5830 count_volumes(char *hba
, int dpa
, int verbose
)
5832 struct md_list
*devlist
= NULL
;
5836 devlist
= get_devices(hba
);
5837 /* if no intel devices return zero volumes */
5838 if (devlist
== NULL
)
5841 count
= active_arrays_by_format("imsm", hba
, &devlist
, dpa
, verbose
);
5842 dprintf(" path: %s active arrays: %d\n", hba
, count
);
5843 if (devlist
== NULL
)
5847 count
+= count_volumes_list(devlist
,
5851 dprintf("found %d count: %d\n", found
, count
);
5854 dprintf("path: %s total number of volumes: %d\n", hba
, count
);
5857 struct md_list
*dv
= devlist
;
5858 devlist
= devlist
->next
;
5865 static int imsm_default_chunk(const struct imsm_orom
*orom
)
5867 /* up to 512 if the plaform supports it, otherwise the platform max.
5868 * 128 if no platform detected
5870 int fs
= max(7, orom
? fls(orom
->sss
) : 0);
5872 return min(512, (1 << fs
));
5876 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
5877 int raiddisks
, int *chunk
, unsigned long long size
, int verbose
)
5879 /* check/set platform and metadata limits/defaults */
5880 if (super
->orom
&& raiddisks
> super
->orom
->dpa
) {
5881 pr_vrb(": platform supports a maximum of %d disks per array\n",
5886 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
5887 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
5888 pr_vrb(": platform does not support raid%d with %d disk%s\n",
5889 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
5893 if (chunk
&& (*chunk
== 0 || *chunk
== UnSet
))
5894 *chunk
= imsm_default_chunk(super
->orom
);
5896 if (super
->orom
&& chunk
&& !imsm_orom_has_chunk(super
->orom
, *chunk
)) {
5897 pr_vrb(": platform does not support a chunk size of: "
5902 if (layout
!= imsm_level_to_layout(level
)) {
5904 pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n");
5905 else if (level
== 10)
5906 pr_vrb(": imsm raid 10 only supports the n2 layout\n");
5908 pr_vrb(": imsm unknown layout %#x for this raid level %d\n",
5913 if (super
->orom
&& (super
->orom
->attr
& IMSM_OROM_ATTR_2TB
) == 0 && chunk
&&
5914 (calc_array_size(level
, raiddisks
, layout
, *chunk
, size
) >> 32) > 0) {
5915 pr_vrb(": platform does not support a volume size over 2TB\n");
5921 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
5922 * FIX ME add ahci details
5924 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
5925 int layout
, int raiddisks
, int *chunk
,
5926 unsigned long long size
, char *dev
,
5927 unsigned long long *freesize
,
5931 struct intel_super
*super
= st
->sb
;
5932 struct imsm_super
*mpb
;
5934 unsigned long long pos
= 0;
5935 unsigned long long maxsize
;
5939 /* We must have the container info already read in. */
5943 mpb
= super
->anchor
;
5945 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, size
, verbose
)) {
5946 fprintf(stderr
, Name
": RAID gemetry validation failed. "
5947 "Cannot proceed with the action(s).\n");
5951 /* General test: make sure there is space for
5952 * 'raiddisks' device extents of size 'size' at a given
5955 unsigned long long minsize
= size
;
5956 unsigned long long start_offset
= MaxSector
;
5959 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
5960 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5965 e
= get_extents(super
, dl
);
5968 unsigned long long esize
;
5969 esize
= e
[i
].start
- pos
;
5970 if (esize
>= minsize
)
5972 if (found
&& start_offset
== MaxSector
) {
5975 } else if (found
&& pos
!= start_offset
) {
5979 pos
= e
[i
].start
+ e
[i
].size
;
5981 } while (e
[i
-1].size
);
5986 if (dcnt
< raiddisks
) {
5988 fprintf(stderr
, Name
": imsm: Not enough "
5989 "devices with space for this array "
5997 /* This device must be a member of the set */
5998 if (stat(dev
, &stb
) < 0)
6000 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
6002 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6003 if (dl
->major
== (int)major(stb
.st_rdev
) &&
6004 dl
->minor
== (int)minor(stb
.st_rdev
))
6009 fprintf(stderr
, Name
": %s is not in the "
6010 "same imsm set\n", dev
);
6012 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
6013 /* If a volume is present then the current creation attempt
6014 * cannot incorporate new spares because the orom may not
6015 * understand this configuration (all member disks must be
6016 * members of each array in the container).
6018 fprintf(stderr
, Name
": %s is a spare and a volume"
6019 " is already defined for this container\n", dev
);
6020 fprintf(stderr
, Name
": The option-rom requires all member"
6021 " disks to be a member of all volumes\n");
6023 } else if (super
->orom
&& mpb
->num_raid_devs
> 0 &&
6024 mpb
->num_disks
!= raiddisks
) {
6025 fprintf(stderr
, Name
": The option-rom requires all member"
6026 " disks to be a member of all volumes\n");
6030 /* retrieve the largest free space block */
6031 e
= get_extents(super
, dl
);
6036 unsigned long long esize
;
6038 esize
= e
[i
].start
- pos
;
6039 if (esize
>= maxsize
)
6041 pos
= e
[i
].start
+ e
[i
].size
;
6043 } while (e
[i
-1].size
);
6048 fprintf(stderr
, Name
": unable to determine free space for: %s\n",
6052 if (maxsize
< size
) {
6054 fprintf(stderr
, Name
": %s not enough space (%llu < %llu)\n",
6055 dev
, maxsize
, size
);
6059 /* count total number of extents for merge */
6061 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6063 i
+= dl
->extent_cnt
;
6065 maxsize
= merge_extents(super
, i
);
6067 if (!check_env("IMSM_NO_PLATFORM") &&
6068 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
6069 fprintf(stderr
, Name
": attempting to create a second "
6070 "volume with size less then remaining space. "
6075 if (maxsize
< size
|| maxsize
== 0) {
6078 fprintf(stderr
, Name
": no free space"
6079 " left on device. Aborting...\n");
6081 fprintf(stderr
, Name
": not enough space"
6082 " to create volume of given size"
6083 " (%llu < %llu). Aborting...\n",
6089 *freesize
= maxsize
;
6092 int count
= count_volumes(super
->hba
->path
,
6093 super
->orom
->dpa
, verbose
);
6094 if (super
->orom
->vphba
<= count
) {
6095 pr_vrb(": platform does not support more than %d raid volumes.\n",
6096 super
->orom
->vphba
);
6103 static int reserve_space(struct supertype
*st
, int raiddisks
,
6104 unsigned long long size
, int chunk
,
6105 unsigned long long *freesize
)
6107 struct intel_super
*super
= st
->sb
;
6108 struct imsm_super
*mpb
= super
->anchor
;
6113 unsigned long long maxsize
;
6114 unsigned long long minsize
;
6118 /* find the largest common start free region of the possible disks */
6122 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6128 /* don't activate new spares if we are orom constrained
6129 * and there is already a volume active in the container
6131 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
6134 e
= get_extents(super
, dl
);
6137 for (i
= 1; e
[i
-1].size
; i
++)
6145 maxsize
= merge_extents(super
, extent_cnt
);
6149 minsize
= chunk
* 2;
6151 if (cnt
< raiddisks
||
6152 (super
->orom
&& used
&& used
!= raiddisks
) ||
6153 maxsize
< minsize
||
6155 fprintf(stderr
, Name
": not enough devices with space to create array.\n");
6156 return 0; /* No enough free spaces large enough */
6167 if (!check_env("IMSM_NO_PLATFORM") &&
6168 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
6169 fprintf(stderr
, Name
": attempting to create a second "
6170 "volume with size less then remaining space. "
6175 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6177 dl
->raiddisk
= cnt
++;
6184 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
6185 int raiddisks
, int *chunk
, unsigned long long size
,
6186 char *dev
, unsigned long long *freesize
,
6194 * if given unused devices create a container
6195 * if given given devices in a container create a member volume
6197 if (level
== LEVEL_CONTAINER
) {
6198 /* Must be a fresh device to add to a container */
6199 return validate_geometry_imsm_container(st
, level
, layout
,
6201 chunk
?*chunk
:0, size
,
6208 struct intel_super
*super
= st
->sb
;
6209 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
6210 raiddisks
, chunk
, size
,
6213 /* we are being asked to automatically layout a
6214 * new volume based on the current contents of
6215 * the container. If the the parameters can be
6216 * satisfied reserve_space will record the disks,
6217 * start offset, and size of the volume to be
6218 * created. add_to_super and getinfo_super
6219 * detect when autolayout is in progress.
6221 /* assuming that freesize is always given when array is
6223 if (super
->orom
&& freesize
) {
6225 count
= count_volumes(super
->hba
->path
,
6226 super
->orom
->dpa
, verbose
);
6227 if (super
->orom
->vphba
<= count
) {
6228 pr_vrb(": platform does not support more"
6229 " than %d raid volumes.\n",
6230 super
->orom
->vphba
);
6235 return reserve_space(st
, raiddisks
, size
,
6236 chunk
?*chunk
:0, freesize
);
6241 /* creating in a given container */
6242 return validate_geometry_imsm_volume(st
, level
, layout
,
6243 raiddisks
, chunk
, size
,
6244 dev
, freesize
, verbose
);
6247 /* This device needs to be a device in an 'imsm' container */
6248 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
6252 Name
": Cannot create this array on device %s\n",
6257 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
6259 fprintf(stderr
, Name
": Cannot open %s: %s\n",
6260 dev
, strerror(errno
));
6263 /* Well, it is in use by someone, maybe an 'imsm' container. */
6264 cfd
= open_container(fd
);
6268 fprintf(stderr
, Name
": Cannot use %s: It is busy\n",
6272 sra
= sysfs_read(cfd
, 0, GET_VERSION
);
6273 if (sra
&& sra
->array
.major_version
== -1 &&
6274 strcmp(sra
->text_version
, "imsm") == 0)
6278 /* This is a member of a imsm container. Load the container
6279 * and try to create a volume
6281 struct intel_super
*super
;
6283 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
, NULL
, 1) == 0) {
6285 st
->container_dev
= fd2devnum(cfd
);
6287 return validate_geometry_imsm_volume(st
, level
, layout
,
6296 fprintf(stderr
, Name
": failed container membership check\n");
6302 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
6304 struct intel_super
*super
= st
->sb
;
6306 if (level
&& *level
== UnSet
)
6307 *level
= LEVEL_CONTAINER
;
6309 if (level
&& layout
&& *layout
== UnSet
)
6310 *layout
= imsm_level_to_layout(*level
);
6312 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0))
6313 *chunk
= imsm_default_chunk(super
->orom
);
6316 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
6318 static int kill_subarray_imsm(struct supertype
*st
)
6320 /* remove the subarray currently referenced by ->current_vol */
6322 struct intel_dev
**dp
;
6323 struct intel_super
*super
= st
->sb
;
6324 __u8 current_vol
= super
->current_vol
;
6325 struct imsm_super
*mpb
= super
->anchor
;
6327 if (super
->current_vol
< 0)
6329 super
->current_vol
= -1; /* invalidate subarray cursor */
6331 /* block deletions that would change the uuid of active subarrays
6333 * FIXME when immutable ids are available, but note that we'll
6334 * also need to fixup the invalidated/active subarray indexes in
6337 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6340 if (i
< current_vol
)
6342 sprintf(subarray
, "%u", i
);
6343 if (is_subarray_active(subarray
, st
->devname
)) {
6345 Name
": deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
6352 if (st
->update_tail
) {
6353 struct imsm_update_kill_array
*u
= malloc(sizeof(*u
));
6357 u
->type
= update_kill_array
;
6358 u
->dev_idx
= current_vol
;
6359 append_metadata_update(st
, u
, sizeof(*u
));
6364 for (dp
= &super
->devlist
; *dp
;)
6365 if ((*dp
)->index
== current_vol
) {
6368 handle_missing(super
, (*dp
)->dev
);
6369 if ((*dp
)->index
> current_vol
)
6374 /* no more raid devices, all active components are now spares,
6375 * but of course failed are still failed
6377 if (--mpb
->num_raid_devs
== 0) {
6380 for (d
= super
->disks
; d
; d
= d
->next
)
6385 super
->updates_pending
++;
6390 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
6391 char *update
, struct mddev_ident
*ident
)
6393 /* update the subarray currently referenced by ->current_vol */
6394 struct intel_super
*super
= st
->sb
;
6395 struct imsm_super
*mpb
= super
->anchor
;
6397 if (strcmp(update
, "name") == 0) {
6398 char *name
= ident
->name
;
6402 if (is_subarray_active(subarray
, st
->devname
)) {
6404 Name
": Unable to update name of active subarray\n");
6408 if (!check_name(super
, name
, 0))
6411 vol
= strtoul(subarray
, &ep
, 10);
6412 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
6415 if (st
->update_tail
) {
6416 struct imsm_update_rename_array
*u
= malloc(sizeof(*u
));
6420 u
->type
= update_rename_array
;
6422 snprintf((char *) u
->name
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6423 append_metadata_update(st
, u
, sizeof(*u
));
6425 struct imsm_dev
*dev
;
6428 dev
= get_imsm_dev(super
, vol
);
6429 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6430 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6431 dev
= get_imsm_dev(super
, i
);
6432 handle_missing(super
, dev
);
6434 super
->updates_pending
++;
6441 #endif /* MDASSEMBLE */
6443 static int is_gen_migration(struct imsm_dev
*dev
)
6448 if (!dev
->vol
.migr_state
)
6451 if (migr_type(dev
) == MIGR_GEN_MIGR
)
6457 static int is_rebuilding(struct imsm_dev
*dev
)
6459 struct imsm_map
*migr_map
;
6461 if (!dev
->vol
.migr_state
)
6464 if (migr_type(dev
) != MIGR_REBUILD
)
6467 migr_map
= get_imsm_map(dev
, MAP_1
);
6469 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
6476 static int is_initializing(struct imsm_dev
*dev
)
6478 struct imsm_map
*migr_map
;
6480 if (!dev
->vol
.migr_state
)
6483 if (migr_type(dev
) != MIGR_INIT
)
6486 migr_map
= get_imsm_map(dev
, MAP_1
);
6488 if (migr_map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
6495 static void update_recovery_start(struct intel_super
*super
,
6496 struct imsm_dev
*dev
,
6497 struct mdinfo
*array
)
6499 struct mdinfo
*rebuild
= NULL
;
6503 if (!is_rebuilding(dev
))
6506 /* Find the rebuild target, but punt on the dual rebuild case */
6507 for (d
= array
->devs
; d
; d
= d
->next
)
6508 if (d
->recovery_start
== 0) {
6515 /* (?) none of the disks are marked with
6516 * IMSM_ORD_REBUILD, so assume they are missing and the
6517 * disk_ord_tbl was not correctly updated
6519 dprintf("%s: failed to locate out-of-sync disk\n", __func__
);
6523 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
6524 rebuild
->recovery_start
= units
* blocks_per_migr_unit(super
, dev
);
6528 static int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
);
6531 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
6533 /* Given a container loaded by load_super_imsm_all,
6534 * extract information about all the arrays into
6536 * If 'subarray' is given, just extract info about that array.
6538 * For each imsm_dev create an mdinfo, fill it in,
6539 * then look for matching devices in super->disks
6540 * and create appropriate device mdinfo.
6542 struct intel_super
*super
= st
->sb
;
6543 struct imsm_super
*mpb
= super
->anchor
;
6544 struct mdinfo
*rest
= NULL
;
6548 int spare_disks
= 0;
6550 /* do not assemble arrays when not all attributes are supported */
6551 if (imsm_check_attributes(mpb
->attributes
) == 0) {
6553 fprintf(stderr
, Name
": Unsupported attributes in IMSM metadata."
6554 "Arrays activation is blocked.\n");
6557 /* check for bad blocks */
6558 if (imsm_bbm_log_size(super
->anchor
)) {
6559 fprintf(stderr
, Name
": BBM log found in IMSM metadata."
6560 "Arrays activation is blocked.\n");
6565 /* count spare devices, not used in maps
6567 for (d
= super
->disks
; d
; d
= d
->next
)
6571 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6572 struct imsm_dev
*dev
;
6573 struct imsm_map
*map
;
6574 struct imsm_map
*map2
;
6575 struct mdinfo
*this;
6583 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
6586 dev
= get_imsm_dev(super
, i
);
6587 map
= get_imsm_map(dev
, MAP_0
);
6588 map2
= get_imsm_map(dev
, MAP_1
);
6590 /* do not publish arrays that are in the middle of an
6591 * unsupported migration
6593 if (dev
->vol
.migr_state
&&
6594 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
6595 fprintf(stderr
, Name
": cannot assemble volume '%.16s':"
6596 " unsupported migration in progress\n",
6600 /* do not publish arrays that are not support by controller's
6604 this = malloc(sizeof(*this));
6606 fprintf(stderr
, Name
": failed to allocate %zu bytes\n",
6611 super
->current_vol
= i
;
6612 getinfo_super_imsm_volume(st
, this, NULL
);
6615 chunk
= __le16_to_cpu(map
->blocks_per_strip
) >> 1;
6616 /* mdadm does not support all metadata features- set the bit in all arrays state */
6617 if (!validate_geometry_imsm_orom(super
,
6618 get_imsm_raid_level(map
), /* RAID level */
6619 imsm_level_to_layout(get_imsm_raid_level(map
)),
6620 map
->num_members
, /* raid disks */
6621 &chunk
, join_u32(dev
->size_low
, dev
->size_high
),
6623 fprintf(stderr
, Name
": IMSM RAID geometry validation"
6624 " failed. Array %s activation is blocked.\n",
6626 this->array
.state
|=
6627 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
6628 (1<<MD_SB_BLOCK_VOLUME
);
6632 /* if array has bad blocks, set suitable bit in all arrays state */
6634 this->array
.state
|=
6635 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
6636 (1<<MD_SB_BLOCK_VOLUME
);
6638 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
6639 unsigned long long recovery_start
;
6640 struct mdinfo
*info_d
;
6647 idx
= get_imsm_disk_idx(dev
, slot
, MAP_0
);
6648 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
6649 for (d
= super
->disks
; d
; d
= d
->next
)
6650 if (d
->index
== idx
)
6653 recovery_start
= MaxSector
;
6656 if (d
&& is_failed(&d
->disk
))
6658 if (ord
& IMSM_ORD_REBUILD
)
6662 * if we skip some disks the array will be assmebled degraded;
6663 * reset resync start to avoid a dirty-degraded
6664 * situation when performing the intial sync
6666 * FIXME handle dirty degraded
6668 if ((skip
|| recovery_start
== 0) && !dev
->vol
.dirty
)
6669 this->resync_start
= MaxSector
;
6673 info_d
= calloc(1, sizeof(*info_d
));
6675 fprintf(stderr
, Name
": failed to allocate disk"
6676 " for volume %.16s\n", dev
->volume
);
6677 info_d
= this->devs
;
6679 struct mdinfo
*d
= info_d
->next
;
6688 info_d
->next
= this->devs
;
6689 this->devs
= info_d
;
6691 info_d
->disk
.number
= d
->index
;
6692 info_d
->disk
.major
= d
->major
;
6693 info_d
->disk
.minor
= d
->minor
;
6694 info_d
->disk
.raid_disk
= slot
;
6695 info_d
->recovery_start
= recovery_start
;
6697 if (slot
< map2
->num_members
)
6698 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
6700 this->array
.spare_disks
++;
6702 if (slot
< map
->num_members
)
6703 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
6705 this->array
.spare_disks
++;
6707 if (info_d
->recovery_start
== MaxSector
)
6708 this->array
.working_disks
++;
6710 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
6711 info_d
->data_offset
= pba_of_lba0(map
);
6712 info_d
->component_size
= blocks_per_member(map
);
6714 /* now that the disk list is up-to-date fixup recovery_start */
6715 update_recovery_start(super
, dev
, this);
6716 this->array
.spare_disks
+= spare_disks
;
6719 /* check for reshape */
6720 if (this->reshape_active
== 1)
6721 recover_backup_imsm(st
, this);
6730 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
6731 int failed
, int look_in_map
)
6733 struct imsm_map
*map
;
6735 map
= get_imsm_map(dev
, look_in_map
);
6738 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
6739 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
6741 switch (get_imsm_raid_level(map
)) {
6743 return IMSM_T_STATE_FAILED
;
6746 if (failed
< map
->num_members
)
6747 return IMSM_T_STATE_DEGRADED
;
6749 return IMSM_T_STATE_FAILED
;
6754 * check to see if any mirrors have failed, otherwise we
6755 * are degraded. Even numbered slots are mirrored on
6759 /* gcc -Os complains that this is unused */
6760 int insync
= insync
;
6762 for (i
= 0; i
< map
->num_members
; i
++) {
6763 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_X
);
6764 int idx
= ord_to_idx(ord
);
6765 struct imsm_disk
*disk
;
6767 /* reset the potential in-sync count on even-numbered
6768 * slots. num_copies is always 2 for imsm raid10
6773 disk
= get_imsm_disk(super
, idx
);
6774 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
6777 /* no in-sync disks left in this mirror the
6781 return IMSM_T_STATE_FAILED
;
6784 return IMSM_T_STATE_DEGRADED
;
6788 return IMSM_T_STATE_DEGRADED
;
6790 return IMSM_T_STATE_FAILED
;
6796 return map
->map_state
;
6799 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
6804 struct imsm_disk
*disk
;
6805 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
6806 struct imsm_map
*prev
= get_imsm_map(dev
, MAP_1
);
6807 struct imsm_map
*map_for_loop
;
6812 /* at the beginning of migration we set IMSM_ORD_REBUILD on
6813 * disks that are being rebuilt. New failures are recorded to
6814 * map[0]. So we look through all the disks we started with and
6815 * see if any failures are still present, or if any new ones
6819 if (prev
&& (map
->num_members
< prev
->num_members
))
6820 map_for_loop
= prev
;
6822 for (i
= 0; i
< map_for_loop
->num_members
; i
++) {
6824 /* when MAP_X is passed both maps failures are counted
6827 ((look_in_map
== MAP_1
) || (look_in_map
== MAP_X
)) &&
6828 (i
< prev
->num_members
)) {
6829 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
6830 idx_1
= ord_to_idx(ord
);
6832 disk
= get_imsm_disk(super
, idx_1
);
6833 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
6836 if (((look_in_map
== MAP_0
) || (look_in_map
== MAP_X
)) &&
6837 (i
< map
->num_members
)) {
6838 ord
= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
6839 idx
= ord_to_idx(ord
);
6842 disk
= get_imsm_disk(super
, idx
);
6843 if (!disk
|| is_failed(disk
) ||
6844 ord
& IMSM_ORD_REBUILD
)
6854 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
6857 struct intel_super
*super
= c
->sb
;
6858 struct imsm_super
*mpb
= super
->anchor
;
6860 if (atoi(inst
) >= mpb
->num_raid_devs
) {
6861 fprintf(stderr
, "%s: subarry index %d, out of range\n",
6862 __func__
, atoi(inst
));
6866 dprintf("imsm: open_new %s\n", inst
);
6867 a
->info
.container_member
= atoi(inst
);
6871 static int is_resyncing(struct imsm_dev
*dev
)
6873 struct imsm_map
*migr_map
;
6875 if (!dev
->vol
.migr_state
)
6878 if (migr_type(dev
) == MIGR_INIT
||
6879 migr_type(dev
) == MIGR_REPAIR
)
6882 if (migr_type(dev
) == MIGR_GEN_MIGR
)
6885 migr_map
= get_imsm_map(dev
, MAP_1
);
6887 if ((migr_map
->map_state
== IMSM_T_STATE_NORMAL
) &&
6888 (dev
->vol
.migr_type
!= MIGR_GEN_MIGR
))
6894 /* return true if we recorded new information */
6895 static int mark_failure(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
6899 struct imsm_map
*map
;
6900 char buf
[MAX_RAID_SERIAL_LEN
+3];
6901 unsigned int len
, shift
= 0;
6903 /* new failures are always set in map[0] */
6904 map
= get_imsm_map(dev
, MAP_0
);
6906 slot
= get_imsm_disk_slot(map
, idx
);
6910 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
6911 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
6914 memcpy(buf
, disk
->serial
, MAX_RAID_SERIAL_LEN
);
6915 buf
[MAX_RAID_SERIAL_LEN
] = '\000';
6917 if ((len
= strlen(buf
)) >= MAX_RAID_SERIAL_LEN
)
6918 shift
= len
- MAX_RAID_SERIAL_LEN
+ 1;
6919 strncpy((char *)disk
->serial
, &buf
[shift
], MAX_RAID_SERIAL_LEN
);
6921 disk
->status
|= FAILED_DISK
;
6922 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
6923 /* mark failures in second map if second map exists and this disk
6925 * This is valid for migration, initialization and rebuild
6927 if (dev
->vol
.migr_state
) {
6928 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
6929 int slot2
= get_imsm_disk_slot(map2
, idx
);
6931 if ((slot2
< map2
->num_members
) &&
6933 set_imsm_ord_tbl_ent(map2
, slot2
,
6934 idx
| IMSM_ORD_REBUILD
);
6936 if (map
->failed_disk_num
== 0xff)
6937 map
->failed_disk_num
= slot
;
6941 static void mark_missing(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
6943 mark_failure(dev
, disk
, idx
);
6945 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
6948 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
6949 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
6952 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
6956 if (!super
->missing
)
6959 dprintf("imsm: mark missing\n");
6960 /* end process for initialization and rebuild only
6962 if (is_gen_migration(dev
) == 0) {
6966 failed
= imsm_count_failed(super
, dev
, MAP_0
);
6967 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
6969 end_migration(dev
, super
, map_state
);
6971 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
6972 mark_missing(dev
, &dl
->disk
, dl
->index
);
6973 super
->updates_pending
++;
6976 static unsigned long long imsm_set_array_size(struct imsm_dev
*dev
)
6978 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
6979 unsigned long long array_blocks
;
6980 struct imsm_map
*map
;
6982 if (used_disks
== 0) {
6983 /* when problems occures
6984 * return current array_blocks value
6986 array_blocks
= __le32_to_cpu(dev
->size_high
);
6987 array_blocks
= array_blocks
<< 32;
6988 array_blocks
+= __le32_to_cpu(dev
->size_low
);
6990 return array_blocks
;
6993 /* set array size in metadata
6995 map
= get_imsm_map(dev
, MAP_0
);
6996 array_blocks
= blocks_per_member(map
) * used_disks
;
6998 /* round array size down to closest MB
7000 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
7001 dev
->size_low
= __cpu_to_le32((__u32
)array_blocks
);
7002 dev
->size_high
= __cpu_to_le32((__u32
)(array_blocks
>> 32));
7004 return array_blocks
;
7007 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
7009 static void imsm_progress_container_reshape(struct intel_super
*super
)
7011 /* if no device has a migr_state, but some device has a
7012 * different number of members than the previous device, start
7013 * changing the number of devices in this device to match
7016 struct imsm_super
*mpb
= super
->anchor
;
7017 int prev_disks
= -1;
7021 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7022 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
7023 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7024 struct imsm_map
*map2
;
7025 int prev_num_members
;
7027 if (dev
->vol
.migr_state
)
7030 if (prev_disks
== -1)
7031 prev_disks
= map
->num_members
;
7032 if (prev_disks
== map
->num_members
)
7035 /* OK, this array needs to enter reshape mode.
7036 * i.e it needs a migr_state
7039 copy_map_size
= sizeof_imsm_map(map
);
7040 prev_num_members
= map
->num_members
;
7041 map
->num_members
= prev_disks
;
7042 dev
->vol
.migr_state
= 1;
7043 dev
->vol
.curr_migr_unit
= 0;
7044 set_migr_type(dev
, MIGR_GEN_MIGR
);
7045 for (i
= prev_num_members
;
7046 i
< map
->num_members
; i
++)
7047 set_imsm_ord_tbl_ent(map
, i
, i
);
7048 map2
= get_imsm_map(dev
, MAP_1
);
7049 /* Copy the current map */
7050 memcpy(map2
, map
, copy_map_size
);
7051 map2
->num_members
= prev_num_members
;
7053 imsm_set_array_size(dev
);
7054 super
->clean_migration_record_by_mdmon
= 1;
7055 super
->updates_pending
++;
7059 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
7060 * states are handled in imsm_set_disk() with one exception, when a
7061 * resync is stopped due to a new failure this routine will set the
7062 * 'degraded' state for the array.
7064 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
7066 int inst
= a
->info
.container_member
;
7067 struct intel_super
*super
= a
->container
->sb
;
7068 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7069 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7070 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
7071 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7072 __u32 blocks_per_unit
;
7074 if (dev
->vol
.migr_state
&&
7075 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
7076 /* array state change is blocked due to reshape action
7078 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
7079 * - finish the reshape (if last_checkpoint is big and action != reshape)
7080 * - update curr_migr_unit
7082 if (a
->curr_action
== reshape
) {
7083 /* still reshaping, maybe update curr_migr_unit */
7084 goto mark_checkpoint
;
7086 if (a
->last_checkpoint
== 0 && a
->prev_action
== reshape
) {
7087 /* for some reason we aborted the reshape.
7089 * disable automatic metadata rollback
7090 * user action is required to recover process
7093 struct imsm_map
*map2
=
7094 get_imsm_map(dev
, MAP_1
);
7095 dev
->vol
.migr_state
= 0;
7096 set_migr_type(dev
, 0);
7097 dev
->vol
.curr_migr_unit
= 0;
7099 sizeof_imsm_map(map2
));
7100 super
->updates_pending
++;
7103 if (a
->last_checkpoint
>= a
->info
.component_size
) {
7104 unsigned long long array_blocks
;
7108 used_disks
= imsm_num_data_members(dev
, MAP_0
);
7109 if (used_disks
> 0) {
7111 blocks_per_member(map
) *
7113 /* round array size down to closest MB
7115 array_blocks
= (array_blocks
7116 >> SECT_PER_MB_SHIFT
)
7117 << SECT_PER_MB_SHIFT
;
7118 a
->info
.custom_array_size
= array_blocks
;
7119 /* encourage manager to update array
7123 a
->check_reshape
= 1;
7125 /* finalize online capacity expansion/reshape */
7126 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
7128 mdi
->disk
.raid_disk
,
7131 imsm_progress_container_reshape(super
);
7136 /* before we activate this array handle any missing disks */
7137 if (consistent
== 2)
7138 handle_missing(super
, dev
);
7140 if (consistent
== 2 &&
7141 (!is_resync_complete(&a
->info
) ||
7142 map_state
!= IMSM_T_STATE_NORMAL
||
7143 dev
->vol
.migr_state
))
7146 if (is_resync_complete(&a
->info
)) {
7147 /* complete intialization / resync,
7148 * recovery and interrupted recovery is completed in
7151 if (is_resyncing(dev
)) {
7152 dprintf("imsm: mark resync done\n");
7153 end_migration(dev
, super
, map_state
);
7154 super
->updates_pending
++;
7155 a
->last_checkpoint
= 0;
7157 } else if ((!is_resyncing(dev
) && !failed
) &&
7158 (imsm_reshape_blocks_arrays_changes(super
) == 0)) {
7159 /* mark the start of the init process if nothing is failed */
7160 dprintf("imsm: mark resync start\n");
7161 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
7162 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
7164 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
7165 super
->updates_pending
++;
7169 /* skip checkpointing for general migration,
7170 * it is controlled in mdadm
7172 if (is_gen_migration(dev
))
7173 goto skip_mark_checkpoint
;
7175 /* check if we can update curr_migr_unit from resync_start, recovery_start */
7176 blocks_per_unit
= blocks_per_migr_unit(super
, dev
);
7177 if (blocks_per_unit
) {
7181 units
= a
->last_checkpoint
/ blocks_per_unit
;
7184 /* check that we did not overflow 32-bits, and that
7185 * curr_migr_unit needs updating
7187 if (units32
== units
&&
7189 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
7190 dprintf("imsm: mark checkpoint (%u)\n", units32
);
7191 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
7192 super
->updates_pending
++;
7196 skip_mark_checkpoint
:
7197 /* mark dirty / clean */
7198 if (dev
->vol
.dirty
!= !consistent
) {
7199 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
7204 super
->updates_pending
++;
7210 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
7212 int inst
= a
->info
.container_member
;
7213 struct intel_super
*super
= a
->container
->sb
;
7214 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7215 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7216 struct imsm_disk
*disk
;
7221 if (n
> map
->num_members
)
7222 fprintf(stderr
, "imsm: set_disk %d out of range 0..%d\n",
7223 n
, map
->num_members
- 1);
7228 dprintf("imsm: set_disk %d:%x\n", n
, state
);
7230 ord
= get_imsm_ord_tbl_ent(dev
, n
, MAP_0
);
7231 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
7233 /* check for new failures */
7234 if (state
& DS_FAULTY
) {
7235 if (mark_failure(dev
, disk
, ord_to_idx(ord
)))
7236 super
->updates_pending
++;
7239 /* check if in_sync */
7240 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
7241 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
7243 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
7244 super
->updates_pending
++;
7247 failed
= imsm_count_failed(super
, dev
, MAP_0
);
7248 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7250 /* check if recovery complete, newly degraded, or failed */
7251 dprintf("imsm: Detected transition to state ");
7252 switch (map_state
) {
7253 case IMSM_T_STATE_NORMAL
: /* transition to normal state */
7254 dprintf("normal: ");
7255 if (is_rebuilding(dev
)) {
7256 dprintf("while rebuilding");
7257 end_migration(dev
, super
, map_state
);
7258 map
= get_imsm_map(dev
, MAP_0
);
7259 map
->failed_disk_num
= ~0;
7260 super
->updates_pending
++;
7261 a
->last_checkpoint
= 0;
7264 if (is_gen_migration(dev
)) {
7265 dprintf("while general migration");
7266 if (a
->last_checkpoint
>= a
->info
.component_size
)
7267 end_migration(dev
, super
, map_state
);
7269 map
->map_state
= map_state
;
7270 map
= get_imsm_map(dev
, MAP_0
);
7271 map
->failed_disk_num
= ~0;
7272 super
->updates_pending
++;
7276 case IMSM_T_STATE_DEGRADED
: /* transition to degraded state */
7277 dprintf("degraded: ");
7278 if ((map
->map_state
!= map_state
) &&
7279 !dev
->vol
.migr_state
) {
7280 dprintf("mark degraded");
7281 map
->map_state
= map_state
;
7282 super
->updates_pending
++;
7283 a
->last_checkpoint
= 0;
7286 if (is_rebuilding(dev
)) {
7287 dprintf("while rebuilding.");
7288 if (map
->map_state
!= map_state
) {
7289 dprintf(" Map state change");
7290 end_migration(dev
, super
, map_state
);
7291 super
->updates_pending
++;
7295 if (is_gen_migration(dev
)) {
7296 dprintf("while general migration");
7297 if (a
->last_checkpoint
>= a
->info
.component_size
)
7298 end_migration(dev
, super
, map_state
);
7300 map
->map_state
= map_state
;
7301 manage_second_map(super
, dev
);
7303 super
->updates_pending
++;
7306 if (is_initializing(dev
)) {
7307 dprintf("while initialization.");
7308 map
->map_state
= map_state
;
7309 super
->updates_pending
++;
7313 case IMSM_T_STATE_FAILED
: /* transition to failed state */
7314 dprintf("failed: ");
7315 if (is_gen_migration(dev
)) {
7316 dprintf("while general migration");
7317 map
->map_state
= map_state
;
7318 super
->updates_pending
++;
7321 if (map
->map_state
!= map_state
) {
7322 dprintf("mark failed");
7323 end_migration(dev
, super
, map_state
);
7324 super
->updates_pending
++;
7325 a
->last_checkpoint
= 0;
7330 dprintf("state %i\n", map_state
);
7336 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
7339 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
7340 unsigned long long dsize
;
7341 unsigned long long sectors
;
7343 get_dev_size(fd
, NULL
, &dsize
);
7345 if (mpb_size
> 512) {
7346 /* -1 to account for anchor */
7347 sectors
= mpb_sectors(mpb
) - 1;
7349 /* write the extended mpb to the sectors preceeding the anchor */
7350 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0)
7353 if ((unsigned long long)write(fd
, buf
+ 512, 512 * sectors
)
7358 /* first block is stored on second to last sector of the disk */
7359 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0)
7362 if (write(fd
, buf
, 512) != 512)
7368 static void imsm_sync_metadata(struct supertype
*container
)
7370 struct intel_super
*super
= container
->sb
;
7372 dprintf("sync metadata: %d\n", super
->updates_pending
);
7373 if (!super
->updates_pending
)
7376 write_super_imsm(container
, 0);
7378 super
->updates_pending
= 0;
7381 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
7383 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
7384 int i
= get_imsm_disk_idx(dev
, idx
, MAP_X
);
7387 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7391 if (dl
&& is_failed(&dl
->disk
))
7395 dprintf("%s: found %x:%x\n", __func__
, dl
->major
, dl
->minor
);
7400 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
7401 struct active_array
*a
, int activate_new
,
7402 struct mdinfo
*additional_test_list
)
7404 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
7405 int idx
= get_imsm_disk_idx(dev
, slot
, MAP_X
);
7406 struct imsm_super
*mpb
= super
->anchor
;
7407 struct imsm_map
*map
;
7408 unsigned long long pos
;
7413 __u32 array_start
= 0;
7414 __u32 array_end
= 0;
7416 struct mdinfo
*test_list
;
7418 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7419 /* If in this array, skip */
7420 for (d
= a
->info
.devs
; d
; d
= d
->next
)
7421 if (d
->state_fd
>= 0 &&
7422 d
->disk
.major
== dl
->major
&&
7423 d
->disk
.minor
== dl
->minor
) {
7424 dprintf("%x:%x already in array\n",
7425 dl
->major
, dl
->minor
);
7430 test_list
= additional_test_list
;
7432 if (test_list
->disk
.major
== dl
->major
&&
7433 test_list
->disk
.minor
== dl
->minor
) {
7434 dprintf("%x:%x already in additional test list\n",
7435 dl
->major
, dl
->minor
);
7438 test_list
= test_list
->next
;
7443 /* skip in use or failed drives */
7444 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
7446 dprintf("%x:%x status (failed: %d index: %d)\n",
7447 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
7451 /* skip pure spares when we are looking for partially
7452 * assimilated drives
7454 if (dl
->index
== -1 && !activate_new
)
7457 /* Does this unused device have the requisite free space?
7458 * It needs to be able to cover all member volumes
7460 ex
= get_extents(super
, dl
);
7462 dprintf("cannot get extents\n");
7465 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7466 dev
= get_imsm_dev(super
, i
);
7467 map
= get_imsm_map(dev
, MAP_0
);
7469 /* check if this disk is already a member of
7472 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
7478 array_start
= pba_of_lba0(map
);
7479 array_end
= array_start
+
7480 blocks_per_member(map
) - 1;
7483 /* check that we can start at pba_of_lba0 with
7484 * blocks_per_member of space
7486 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
7490 pos
= ex
[j
].start
+ ex
[j
].size
;
7492 } while (ex
[j
-1].size
);
7499 if (i
< mpb
->num_raid_devs
) {
7500 dprintf("%x:%x does not have %u to %u available\n",
7501 dl
->major
, dl
->minor
, array_start
, array_end
);
7512 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
7514 struct imsm_dev
*dev2
;
7515 struct imsm_map
*map
;
7521 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
7523 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
, MAP_0
);
7524 if (state
== IMSM_T_STATE_FAILED
) {
7525 map
= get_imsm_map(dev2
, MAP_0
);
7528 for (slot
= 0; slot
< map
->num_members
; slot
++) {
7530 * Check if failed disks are deleted from intel
7531 * disk list or are marked to be deleted
7533 idx
= get_imsm_disk_idx(dev2
, slot
, MAP_X
);
7534 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
7536 * Do not rebuild the array if failed disks
7537 * from failed sub-array are not removed from
7541 is_failed(&idisk
->disk
) &&
7542 (idisk
->action
!= DISK_REMOVE
))
7550 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
7551 struct metadata_update
**updates
)
7554 * Find a device with unused free space and use it to replace a
7555 * failed/vacant region in an array. We replace failed regions one a
7556 * array at a time. The result is that a new spare disk will be added
7557 * to the first failed array and after the monitor has finished
7558 * propagating failures the remainder will be consumed.
7560 * FIXME add a capability for mdmon to request spares from another
7564 struct intel_super
*super
= a
->container
->sb
;
7565 int inst
= a
->info
.container_member
;
7566 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7567 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7568 int failed
= a
->info
.array
.raid_disks
;
7569 struct mdinfo
*rv
= NULL
;
7572 struct metadata_update
*mu
;
7574 struct imsm_update_activate_spare
*u
;
7579 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
7580 if ((d
->curr_state
& DS_FAULTY
) &&
7582 /* wait for Removal to happen */
7584 if (d
->state_fd
>= 0)
7588 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
7589 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
7591 if (imsm_reshape_blocks_arrays_changes(super
))
7594 /* Cannot activate another spare if rebuild is in progress already
7596 if (is_rebuilding(dev
)) {
7597 dprintf("imsm: No spare activation allowed. "
7598 "Rebuild in progress already.\n");
7602 if (a
->info
.array
.level
== 4)
7603 /* No repair for takeovered array
7604 * imsm doesn't support raid4
7608 if (imsm_check_degraded(super
, dev
, failed
, MAP_0
) !=
7609 IMSM_T_STATE_DEGRADED
)
7613 * If there are any failed disks check state of the other volume.
7614 * Block rebuild if the another one is failed until failed disks
7615 * are removed from container.
7618 dprintf("found failed disks in %.*s, check if there another"
7619 "failed sub-array.\n",
7620 MAX_RAID_SERIAL_LEN
, dev
->volume
);
7621 /* check if states of the other volumes allow for rebuild */
7622 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
7624 allowed
= imsm_rebuild_allowed(a
->container
,
7632 /* For each slot, if it is not working, find a spare */
7633 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
7634 for (d
= a
->info
.devs
; d
; d
= d
->next
)
7635 if (d
->disk
.raid_disk
== i
)
7637 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
7638 if (d
&& (d
->state_fd
>= 0))
7642 * OK, this device needs recovery. Try to re-add the
7643 * previous occupant of this slot, if this fails see if
7644 * we can continue the assimilation of a spare that was
7645 * partially assimilated, finally try to activate a new
7648 dl
= imsm_readd(super
, i
, a
);
7650 dl
= imsm_add_spare(super
, i
, a
, 0, rv
);
7652 dl
= imsm_add_spare(super
, i
, a
, 1, rv
);
7656 /* found a usable disk with enough space */
7657 di
= malloc(sizeof(*di
));
7660 memset(di
, 0, sizeof(*di
));
7662 /* dl->index will be -1 in the case we are activating a
7663 * pristine spare. imsm_process_update() will create a
7664 * new index in this case. Once a disk is found to be
7665 * failed in all member arrays it is kicked from the
7668 di
->disk
.number
= dl
->index
;
7670 /* (ab)use di->devs to store a pointer to the device
7673 di
->devs
= (struct mdinfo
*) dl
;
7675 di
->disk
.raid_disk
= i
;
7676 di
->disk
.major
= dl
->major
;
7677 di
->disk
.minor
= dl
->minor
;
7679 di
->recovery_start
= 0;
7680 di
->data_offset
= pba_of_lba0(map
);
7681 di
->component_size
= a
->info
.component_size
;
7682 di
->container_member
= inst
;
7683 super
->random
= random32();
7687 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
7688 i
, di
->data_offset
);
7692 /* No spares found */
7694 /* Now 'rv' has a list of devices to return.
7695 * Create a metadata_update record to update the
7696 * disk_ord_tbl for the array
7698 mu
= malloc(sizeof(*mu
));
7700 mu
->buf
= malloc(sizeof(struct imsm_update_activate_spare
) * num_spares
);
7701 if (mu
->buf
== NULL
) {
7708 struct mdinfo
*n
= rv
->next
;
7717 mu
->space_list
= NULL
;
7718 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
7719 mu
->next
= *updates
;
7720 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
7722 for (di
= rv
; di
; di
= di
->next
) {
7723 u
->type
= update_activate_spare
;
7724 u
->dl
= (struct dl
*) di
->devs
;
7726 u
->slot
= di
->disk
.raid_disk
;
7737 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
7739 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
7740 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7741 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, MAP_0
);
7742 struct disk_info
*inf
= get_disk_info(u
);
7743 struct imsm_disk
*disk
;
7747 for (i
= 0; i
< map
->num_members
; i
++) {
7748 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, MAP_X
));
7749 for (j
= 0; j
< new_map
->num_members
; j
++)
7750 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
7758 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
7760 struct dl
*dl
= NULL
;
7761 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7762 if ((dl
->major
== major
) && (dl
->minor
== minor
))
7767 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
7769 struct dl
*prev
= NULL
;
7773 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7774 if ((dl
->major
== major
) && (dl
->minor
== minor
)) {
7777 prev
->next
= dl
->next
;
7779 super
->disks
= dl
->next
;
7781 __free_imsm_disk(dl
);
7782 dprintf("%s: removed %x:%x\n",
7783 __func__
, major
, minor
);
7791 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
7793 static int add_remove_disk_update(struct intel_super
*super
)
7795 int check_degraded
= 0;
7796 struct dl
*disk
= NULL
;
7797 /* add/remove some spares to/from the metadata/contrainer */
7798 while (super
->disk_mgmt_list
) {
7799 struct dl
*disk_cfg
;
7801 disk_cfg
= super
->disk_mgmt_list
;
7802 super
->disk_mgmt_list
= disk_cfg
->next
;
7803 disk_cfg
->next
= NULL
;
7805 if (disk_cfg
->action
== DISK_ADD
) {
7806 disk_cfg
->next
= super
->disks
;
7807 super
->disks
= disk_cfg
;
7809 dprintf("%s: added %x:%x\n",
7810 __func__
, disk_cfg
->major
,
7812 } else if (disk_cfg
->action
== DISK_REMOVE
) {
7813 dprintf("Disk remove action processed: %x.%x\n",
7814 disk_cfg
->major
, disk_cfg
->minor
);
7815 disk
= get_disk_super(super
,
7819 /* store action status */
7820 disk
->action
= DISK_REMOVE
;
7821 /* remove spare disks only */
7822 if (disk
->index
== -1) {
7823 remove_disk_super(super
,
7828 /* release allocate disk structure */
7829 __free_imsm_disk(disk_cfg
);
7832 return check_degraded
;
7836 static int apply_reshape_migration_update(struct imsm_update_reshape_migration
*u
,
7837 struct intel_super
*super
,
7840 struct intel_dev
*id
;
7841 void **tofree
= NULL
;
7844 dprintf("apply_reshape_migration_update()\n");
7845 if ((u
->subdev
< 0) ||
7847 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
7850 if ((space_list
== NULL
) || (*space_list
== NULL
)) {
7851 dprintf("imsm: Error: Memory is not allocated\n");
7855 for (id
= super
->devlist
; id
; id
= id
->next
) {
7856 if (id
->index
== (unsigned)u
->subdev
) {
7857 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
7858 struct imsm_map
*map
;
7859 struct imsm_dev
*new_dev
=
7860 (struct imsm_dev
*)*space_list
;
7861 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
7863 struct dl
*new_disk
;
7865 if (new_dev
== NULL
)
7867 *space_list
= **space_list
;
7868 memcpy(new_dev
, dev
, sizeof_imsm_dev(dev
, 0));
7869 map
= get_imsm_map(new_dev
, MAP_0
);
7871 dprintf("imsm: Error: migration in progress");
7875 to_state
= map
->map_state
;
7876 if ((u
->new_level
== 5) && (map
->raid_level
== 0)) {
7878 /* this should not happen */
7879 if (u
->new_disks
[0] < 0) {
7880 map
->failed_disk_num
=
7881 map
->num_members
- 1;
7882 to_state
= IMSM_T_STATE_DEGRADED
;
7884 to_state
= IMSM_T_STATE_NORMAL
;
7886 migrate(new_dev
, super
, to_state
, MIGR_GEN_MIGR
);
7887 if (u
->new_level
> -1)
7888 map
->raid_level
= u
->new_level
;
7889 migr_map
= get_imsm_map(new_dev
, MAP_1
);
7890 if ((u
->new_level
== 5) &&
7891 (migr_map
->raid_level
== 0)) {
7892 int ord
= map
->num_members
- 1;
7893 migr_map
->num_members
--;
7894 if (u
->new_disks
[0] < 0)
7895 ord
|= IMSM_ORD_REBUILD
;
7896 set_imsm_ord_tbl_ent(map
,
7897 map
->num_members
- 1,
7901 tofree
= (void **)dev
;
7903 /* update chunk size
7905 if (u
->new_chunksize
> 0)
7906 map
->blocks_per_strip
=
7907 __cpu_to_le16(u
->new_chunksize
* 2);
7911 if ((u
->new_level
!= 5) ||
7912 (migr_map
->raid_level
!= 0) ||
7913 (migr_map
->raid_level
== map
->raid_level
))
7916 if (u
->new_disks
[0] >= 0) {
7919 new_disk
= get_disk_super(super
,
7920 major(u
->new_disks
[0]),
7921 minor(u
->new_disks
[0]));
7922 dprintf("imsm: new disk for reshape is: %i:%i "
7923 "(%p, index = %i)\n",
7924 major(u
->new_disks
[0]),
7925 minor(u
->new_disks
[0]),
7926 new_disk
, new_disk
->index
);
7927 if (new_disk
== NULL
)
7928 goto error_disk_add
;
7930 new_disk
->index
= map
->num_members
- 1;
7931 /* slot to fill in autolayout
7933 new_disk
->raiddisk
= new_disk
->index
;
7934 new_disk
->disk
.status
|= CONFIGURED_DISK
;
7935 new_disk
->disk
.status
&= ~SPARE_DISK
;
7937 goto error_disk_add
;
7940 *tofree
= *space_list
;
7941 /* calculate new size
7943 imsm_set_array_size(new_dev
);
7950 *space_list
= tofree
;
7954 dprintf("Error: imsm: Cannot find disk.\n");
7958 static int apply_update_activate_spare(struct imsm_update_activate_spare
*u
,
7959 struct intel_super
*super
,
7960 struct active_array
*active_array
)
7962 struct imsm_super
*mpb
= super
->anchor
;
7963 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
7964 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7965 struct imsm_map
*migr_map
;
7966 struct active_array
*a
;
7967 struct imsm_disk
*disk
;
7974 int second_map_created
= 0;
7976 for (; u
; u
= u
->next
) {
7977 victim
= get_imsm_disk_idx(dev
, u
->slot
, MAP_X
);
7982 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7987 fprintf(stderr
, "error: imsm_activate_spare passed "
7988 "an unknown disk (index: %d)\n",
7993 /* count failures (excluding rebuilds and the victim)
7994 * to determine map[0] state
7997 for (i
= 0; i
< map
->num_members
; i
++) {
8000 disk
= get_imsm_disk(super
,
8001 get_imsm_disk_idx(dev
, i
, MAP_X
));
8002 if (!disk
|| is_failed(disk
))
8006 /* adding a pristine spare, assign a new index */
8007 if (dl
->index
< 0) {
8008 dl
->index
= super
->anchor
->num_disks
;
8009 super
->anchor
->num_disks
++;
8012 disk
->status
|= CONFIGURED_DISK
;
8013 disk
->status
&= ~SPARE_DISK
;
8016 to_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8017 if (!second_map_created
) {
8018 second_map_created
= 1;
8019 map
->map_state
= IMSM_T_STATE_DEGRADED
;
8020 migrate(dev
, super
, to_state
, MIGR_REBUILD
);
8022 map
->map_state
= to_state
;
8023 migr_map
= get_imsm_map(dev
, MAP_1
);
8024 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
8025 set_imsm_ord_tbl_ent(migr_map
, u
->slot
,
8026 dl
->index
| IMSM_ORD_REBUILD
);
8028 /* update the family_num to mark a new container
8029 * generation, being careful to record the existing
8030 * family_num in orig_family_num to clean up after
8031 * earlier mdadm versions that neglected to set it.
8033 if (mpb
->orig_family_num
== 0)
8034 mpb
->orig_family_num
= mpb
->family_num
;
8035 mpb
->family_num
+= super
->random
;
8037 /* count arrays using the victim in the metadata */
8039 for (a
= active_array
; a
; a
= a
->next
) {
8040 dev
= get_imsm_dev(super
, a
->info
.container_member
);
8041 map
= get_imsm_map(dev
, MAP_0
);
8043 if (get_imsm_disk_slot(map
, victim
) >= 0)
8047 /* delete the victim if it is no longer being
8053 /* We know that 'manager' isn't touching anything,
8054 * so it is safe to delete
8056 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
8057 if ((*dlp
)->index
== victim
)
8060 /* victim may be on the missing list */
8062 for (dlp
= &super
->missing
; *dlp
;
8063 dlp
= &(*dlp
)->next
)
8064 if ((*dlp
)->index
== victim
)
8066 imsm_delete(super
, dlp
, victim
);
8073 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
8074 struct intel_super
*super
,
8077 struct dl
*new_disk
;
8078 struct intel_dev
*id
;
8080 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
8081 int disk_count
= u
->old_raid_disks
;
8082 void **tofree
= NULL
;
8083 int devices_to_reshape
= 1;
8084 struct imsm_super
*mpb
= super
->anchor
;
8086 unsigned int dev_id
;
8088 dprintf("imsm: apply_reshape_container_disks_update()\n");
8090 /* enable spares to use in array */
8091 for (i
= 0; i
< delta_disks
; i
++) {
8092 new_disk
= get_disk_super(super
,
8093 major(u
->new_disks
[i
]),
8094 minor(u
->new_disks
[i
]));
8095 dprintf("imsm: new disk for reshape is: %i:%i "
8096 "(%p, index = %i)\n",
8097 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
8098 new_disk
, new_disk
->index
);
8099 if ((new_disk
== NULL
) ||
8100 ((new_disk
->index
>= 0) &&
8101 (new_disk
->index
< u
->old_raid_disks
)))
8102 goto update_reshape_exit
;
8103 new_disk
->index
= disk_count
++;
8104 /* slot to fill in autolayout
8106 new_disk
->raiddisk
= new_disk
->index
;
8107 new_disk
->disk
.status
|=
8109 new_disk
->disk
.status
&= ~SPARE_DISK
;
8112 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
8113 mpb
->num_raid_devs
);
8114 /* manage changes in volume
8116 for (dev_id
= 0; dev_id
< mpb
->num_raid_devs
; dev_id
++) {
8117 void **sp
= *space_list
;
8118 struct imsm_dev
*newdev
;
8119 struct imsm_map
*newmap
, *oldmap
;
8121 for (id
= super
->devlist
; id
; id
= id
->next
) {
8122 if (id
->index
== dev_id
)
8131 /* Copy the dev, but not (all of) the map */
8132 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
8133 oldmap
= get_imsm_map(id
->dev
, MAP_0
);
8134 newmap
= get_imsm_map(newdev
, MAP_0
);
8135 /* Copy the current map */
8136 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8137 /* update one device only
8139 if (devices_to_reshape
) {
8140 dprintf("imsm: modifying subdev: %i\n",
8142 devices_to_reshape
--;
8143 newdev
->vol
.migr_state
= 1;
8144 newdev
->vol
.curr_migr_unit
= 0;
8145 set_migr_type(newdev
, MIGR_GEN_MIGR
);
8146 newmap
->num_members
= u
->new_raid_disks
;
8147 for (i
= 0; i
< delta_disks
; i
++) {
8148 set_imsm_ord_tbl_ent(newmap
,
8149 u
->old_raid_disks
+ i
,
8150 u
->old_raid_disks
+ i
);
8152 /* New map is correct, now need to save old map
8154 newmap
= get_imsm_map(newdev
, MAP_1
);
8155 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8157 imsm_set_array_size(newdev
);
8160 sp
= (void **)id
->dev
;
8165 /* Clear migration record */
8166 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
8169 *space_list
= tofree
;
8172 update_reshape_exit
:
8177 static int apply_takeover_update(struct imsm_update_takeover
*u
,
8178 struct intel_super
*super
,
8181 struct imsm_dev
*dev
= NULL
;
8182 struct intel_dev
*dv
;
8183 struct imsm_dev
*dev_new
;
8184 struct imsm_map
*map
;
8188 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
8189 if (dv
->index
== (unsigned int)u
->subarray
) {
8197 map
= get_imsm_map(dev
, MAP_0
);
8199 if (u
->direction
== R10_TO_R0
) {
8200 /* Number of failed disks must be half of initial disk number */
8201 if (imsm_count_failed(super
, dev
, MAP_0
) !=
8202 (map
->num_members
/ 2))
8205 /* iterate through devices to mark removed disks as spare */
8206 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
8207 if (dm
->disk
.status
& FAILED_DISK
) {
8208 int idx
= dm
->index
;
8209 /* update indexes on the disk list */
8210 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
8211 the index values will end up being correct.... NB */
8212 for (du
= super
->disks
; du
; du
= du
->next
)
8213 if (du
->index
> idx
)
8215 /* mark as spare disk */
8220 map
->num_members
= map
->num_members
/ 2;
8221 map
->map_state
= IMSM_T_STATE_NORMAL
;
8222 map
->num_domains
= 1;
8223 map
->raid_level
= 0;
8224 map
->failed_disk_num
= -1;
8227 if (u
->direction
== R0_TO_R10
) {
8229 /* update slots in current disk list */
8230 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
8234 /* create new *missing* disks */
8235 for (i
= 0; i
< map
->num_members
; i
++) {
8236 space
= *space_list
;
8239 *space_list
= *space
;
8241 memcpy(du
, super
->disks
, sizeof(*du
));
8245 du
->index
= (i
* 2) + 1;
8246 sprintf((char *)du
->disk
.serial
,
8247 " MISSING_%d", du
->index
);
8248 sprintf((char *)du
->serial
,
8249 "MISSING_%d", du
->index
);
8250 du
->next
= super
->missing
;
8251 super
->missing
= du
;
8253 /* create new dev and map */
8254 space
= *space_list
;
8257 *space_list
= *space
;
8258 dev_new
= (void *)space
;
8259 memcpy(dev_new
, dev
, sizeof(*dev
));
8260 /* update new map */
8261 map
= get_imsm_map(dev_new
, MAP_0
);
8262 map
->num_members
= map
->num_members
* 2;
8263 map
->map_state
= IMSM_T_STATE_DEGRADED
;
8264 map
->num_domains
= 2;
8265 map
->raid_level
= 1;
8266 /* replace dev<->dev_new */
8269 /* update disk order table */
8270 for (du
= super
->disks
; du
; du
= du
->next
)
8272 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
8273 for (du
= super
->missing
; du
; du
= du
->next
)
8274 if (du
->index
>= 0) {
8275 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
8276 mark_missing(dv
->dev
, &du
->disk
, du
->index
);
8282 static void imsm_process_update(struct supertype
*st
,
8283 struct metadata_update
*update
)
8286 * crack open the metadata_update envelope to find the update record
8287 * update can be one of:
8288 * update_reshape_container_disks - all the arrays in the container
8289 * are being reshaped to have more devices. We need to mark
8290 * the arrays for general migration and convert selected spares
8291 * into active devices.
8292 * update_activate_spare - a spare device has replaced a failed
8293 * device in an array, update the disk_ord_tbl. If this disk is
8294 * present in all member arrays then also clear the SPARE_DISK
8296 * update_create_array
8298 * update_rename_array
8299 * update_add_remove_disk
8301 struct intel_super
*super
= st
->sb
;
8302 struct imsm_super
*mpb
;
8303 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
8305 /* update requires a larger buf but the allocation failed */
8306 if (super
->next_len
&& !super
->next_buf
) {
8307 super
->next_len
= 0;
8311 if (super
->next_buf
) {
8312 memcpy(super
->next_buf
, super
->buf
, super
->len
);
8314 super
->len
= super
->next_len
;
8315 super
->buf
= super
->next_buf
;
8317 super
->next_len
= 0;
8318 super
->next_buf
= NULL
;
8321 mpb
= super
->anchor
;
8324 case update_general_migration_checkpoint
: {
8325 struct intel_dev
*id
;
8326 struct imsm_update_general_migration_checkpoint
*u
=
8327 (void *)update
->buf
;
8329 dprintf("imsm: process_update() "
8330 "for update_general_migration_checkpoint called\n");
8332 /* find device under general migration */
8333 for (id
= super
->devlist
; id
; id
= id
->next
) {
8334 if (is_gen_migration(id
->dev
)) {
8335 id
->dev
->vol
.curr_migr_unit
=
8336 __cpu_to_le32(u
->curr_migr_unit
);
8337 super
->updates_pending
++;
8342 case update_takeover
: {
8343 struct imsm_update_takeover
*u
= (void *)update
->buf
;
8344 if (apply_takeover_update(u
, super
, &update
->space_list
)) {
8345 imsm_update_version_info(super
);
8346 super
->updates_pending
++;
8351 case update_reshape_container_disks
: {
8352 struct imsm_update_reshape
*u
= (void *)update
->buf
;
8353 if (apply_reshape_container_disks_update(
8354 u
, super
, &update
->space_list
))
8355 super
->updates_pending
++;
8358 case update_reshape_migration
: {
8359 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
8360 if (apply_reshape_migration_update(
8361 u
, super
, &update
->space_list
))
8362 super
->updates_pending
++;
8365 case update_activate_spare
: {
8366 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
8367 if (apply_update_activate_spare(u
, super
, st
->arrays
))
8368 super
->updates_pending
++;
8371 case update_create_array
: {
8372 /* someone wants to create a new array, we need to be aware of
8373 * a few races/collisions:
8374 * 1/ 'Create' called by two separate instances of mdadm
8375 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
8376 * devices that have since been assimilated via
8378 * In the event this update can not be carried out mdadm will
8379 * (FIX ME) notice that its update did not take hold.
8381 struct imsm_update_create_array
*u
= (void *) update
->buf
;
8382 struct intel_dev
*dv
;
8383 struct imsm_dev
*dev
;
8384 struct imsm_map
*map
, *new_map
;
8385 unsigned long long start
, end
;
8386 unsigned long long new_start
, new_end
;
8388 struct disk_info
*inf
;
8391 /* handle racing creates: first come first serve */
8392 if (u
->dev_idx
< mpb
->num_raid_devs
) {
8393 dprintf("%s: subarray %d already defined\n",
8394 __func__
, u
->dev_idx
);
8398 /* check update is next in sequence */
8399 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
8400 dprintf("%s: can not create array %d expected index %d\n",
8401 __func__
, u
->dev_idx
, mpb
->num_raid_devs
);
8405 new_map
= get_imsm_map(&u
->dev
, MAP_0
);
8406 new_start
= pba_of_lba0(new_map
);
8407 new_end
= new_start
+ blocks_per_member(new_map
);
8408 inf
= get_disk_info(u
);
8410 /* handle activate_spare versus create race:
8411 * check to make sure that overlapping arrays do not include
8414 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8415 dev
= get_imsm_dev(super
, i
);
8416 map
= get_imsm_map(dev
, MAP_0
);
8417 start
= pba_of_lba0(map
);
8418 end
= start
+ blocks_per_member(map
);
8419 if ((new_start
>= start
&& new_start
<= end
) ||
8420 (start
>= new_start
&& start
<= new_end
))
8425 if (disks_overlap(super
, i
, u
)) {
8426 dprintf("%s: arrays overlap\n", __func__
);
8431 /* check that prepare update was successful */
8432 if (!update
->space
) {
8433 dprintf("%s: prepare update failed\n", __func__
);
8437 /* check that all disks are still active before committing
8438 * changes. FIXME: could we instead handle this by creating a
8439 * degraded array? That's probably not what the user expects,
8440 * so better to drop this update on the floor.
8442 for (i
= 0; i
< new_map
->num_members
; i
++) {
8443 dl
= serial_to_dl(inf
[i
].serial
, super
);
8445 dprintf("%s: disk disappeared\n", __func__
);
8450 super
->updates_pending
++;
8452 /* convert spares to members and fixup ord_tbl */
8453 for (i
= 0; i
< new_map
->num_members
; i
++) {
8454 dl
= serial_to_dl(inf
[i
].serial
, super
);
8455 if (dl
->index
== -1) {
8456 dl
->index
= mpb
->num_disks
;
8458 dl
->disk
.status
|= CONFIGURED_DISK
;
8459 dl
->disk
.status
&= ~SPARE_DISK
;
8461 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
8466 update
->space
= NULL
;
8467 imsm_copy_dev(dev
, &u
->dev
);
8468 dv
->index
= u
->dev_idx
;
8469 dv
->next
= super
->devlist
;
8470 super
->devlist
= dv
;
8471 mpb
->num_raid_devs
++;
8473 imsm_update_version_info(super
);
8476 /* mdmon knows how to release update->space, but not
8477 * ((struct intel_dev *) update->space)->dev
8479 if (update
->space
) {
8485 case update_kill_array
: {
8486 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
8487 int victim
= u
->dev_idx
;
8488 struct active_array
*a
;
8489 struct intel_dev
**dp
;
8490 struct imsm_dev
*dev
;
8492 /* sanity check that we are not affecting the uuid of
8493 * active arrays, or deleting an active array
8495 * FIXME when immutable ids are available, but note that
8496 * we'll also need to fixup the invalidated/active
8497 * subarray indexes in mdstat
8499 for (a
= st
->arrays
; a
; a
= a
->next
)
8500 if (a
->info
.container_member
>= victim
)
8502 /* by definition if mdmon is running at least one array
8503 * is active in the container, so checking
8504 * mpb->num_raid_devs is just extra paranoia
8506 dev
= get_imsm_dev(super
, victim
);
8507 if (a
|| !dev
|| mpb
->num_raid_devs
== 1) {
8508 dprintf("failed to delete subarray-%d\n", victim
);
8512 for (dp
= &super
->devlist
; *dp
;)
8513 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
8516 if ((*dp
)->index
> (unsigned)victim
)
8520 mpb
->num_raid_devs
--;
8521 super
->updates_pending
++;
8524 case update_rename_array
: {
8525 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
8526 char name
[MAX_RAID_SERIAL_LEN
+1];
8527 int target
= u
->dev_idx
;
8528 struct active_array
*a
;
8529 struct imsm_dev
*dev
;
8531 /* sanity check that we are not affecting the uuid of
8534 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
8535 name
[MAX_RAID_SERIAL_LEN
] = '\0';
8536 for (a
= st
->arrays
; a
; a
= a
->next
)
8537 if (a
->info
.container_member
== target
)
8539 dev
= get_imsm_dev(super
, u
->dev_idx
);
8540 if (a
|| !dev
|| !check_name(super
, name
, 1)) {
8541 dprintf("failed to rename subarray-%d\n", target
);
8545 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
8546 super
->updates_pending
++;
8549 case update_add_remove_disk
: {
8550 /* we may be able to repair some arrays if disks are
8551 * being added, check teh status of add_remove_disk
8552 * if discs has been added.
8554 if (add_remove_disk_update(super
)) {
8555 struct active_array
*a
;
8557 super
->updates_pending
++;
8558 for (a
= st
->arrays
; a
; a
= a
->next
)
8559 a
->check_degraded
= 1;
8564 fprintf(stderr
, "error: unsuported process update type:"
8565 "(type: %d)\n", type
);
8569 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
);
8571 static void imsm_prepare_update(struct supertype
*st
,
8572 struct metadata_update
*update
)
8575 * Allocate space to hold new disk entries, raid-device entries or a new
8576 * mpb if necessary. The manager synchronously waits for updates to
8577 * complete in the monitor, so new mpb buffers allocated here can be
8578 * integrated by the monitor thread without worrying about live pointers
8579 * in the manager thread.
8581 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
8582 struct intel_super
*super
= st
->sb
;
8583 struct imsm_super
*mpb
= super
->anchor
;
8588 case update_general_migration_checkpoint
:
8589 dprintf("imsm: prepare_update() "
8590 "for update_general_migration_checkpoint called\n");
8592 case update_takeover
: {
8593 struct imsm_update_takeover
*u
= (void *)update
->buf
;
8594 if (u
->direction
== R0_TO_R10
) {
8595 void **tail
= (void **)&update
->space_list
;
8596 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subarray
);
8597 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8598 int num_members
= map
->num_members
;
8602 /* allocate memory for added disks */
8603 for (i
= 0; i
< num_members
; i
++) {
8604 size
= sizeof(struct dl
);
8605 space
= malloc(size
);
8614 /* allocate memory for new device */
8615 size
= sizeof_imsm_dev(super
->devlist
->dev
, 0) +
8616 (num_members
* sizeof(__u32
));
8617 space
= malloc(size
);
8626 len
= disks_to_mpb_size(num_members
* 2);
8628 /* if allocation didn't success, free buffer */
8629 while (update
->space_list
) {
8630 void **sp
= update
->space_list
;
8631 update
->space_list
= *sp
;
8639 case update_reshape_container_disks
: {
8640 /* Every raid device in the container is about to
8641 * gain some more devices, and we will enter a
8643 * So each 'imsm_map' will be bigger, and the imsm_vol
8644 * will now hold 2 of them.
8645 * Thus we need new 'struct imsm_dev' allocations sized
8646 * as sizeof_imsm_dev but with more devices in both maps.
8648 struct imsm_update_reshape
*u
= (void *)update
->buf
;
8649 struct intel_dev
*dl
;
8650 void **space_tail
= (void**)&update
->space_list
;
8652 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
8654 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
8655 int size
= sizeof_imsm_dev(dl
->dev
, 1);
8657 if (u
->new_raid_disks
> u
->old_raid_disks
)
8658 size
+= sizeof(__u32
)*2*
8659 (u
->new_raid_disks
- u
->old_raid_disks
);
8668 len
= disks_to_mpb_size(u
->new_raid_disks
);
8669 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
8672 case update_reshape_migration
: {
8673 /* for migration level 0->5 we need to add disks
8674 * so the same as for container operation we will copy
8675 * device to the bigger location.
8676 * in memory prepared device and new disk area are prepared
8677 * for usage in process update
8679 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
8680 struct intel_dev
*id
;
8681 void **space_tail
= (void **)&update
->space_list
;
8684 int current_level
= -1;
8686 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
8688 /* add space for bigger array in update
8690 for (id
= super
->devlist
; id
; id
= id
->next
) {
8691 if (id
->index
== (unsigned)u
->subdev
) {
8692 size
= sizeof_imsm_dev(id
->dev
, 1);
8693 if (u
->new_raid_disks
> u
->old_raid_disks
)
8694 size
+= sizeof(__u32
)*2*
8695 (u
->new_raid_disks
- u
->old_raid_disks
);
8705 if (update
->space_list
== NULL
)
8708 /* add space for disk in update
8710 size
= sizeof(struct dl
);
8713 free(update
->space_list
);
8714 update
->space_list
= NULL
;
8721 /* add spare device to update
8723 for (id
= super
->devlist
; id
; id
= id
->next
)
8724 if (id
->index
== (unsigned)u
->subdev
) {
8725 struct imsm_dev
*dev
;
8726 struct imsm_map
*map
;
8728 dev
= get_imsm_dev(super
, u
->subdev
);
8729 map
= get_imsm_map(dev
, MAP_0
);
8730 current_level
= map
->raid_level
;
8733 if ((u
->new_level
== 5) && (u
->new_level
!= current_level
)) {
8734 struct mdinfo
*spares
;
8736 spares
= get_spares_for_grow(st
);
8744 makedev(dev
->disk
.major
,
8746 dl
= get_disk_super(super
,
8749 dl
->index
= u
->old_raid_disks
;
8755 len
= disks_to_mpb_size(u
->new_raid_disks
);
8756 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
8759 case update_create_array
: {
8760 struct imsm_update_create_array
*u
= (void *) update
->buf
;
8761 struct intel_dev
*dv
;
8762 struct imsm_dev
*dev
= &u
->dev
;
8763 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8765 struct disk_info
*inf
;
8769 inf
= get_disk_info(u
);
8770 len
= sizeof_imsm_dev(dev
, 1);
8771 /* allocate a new super->devlist entry */
8772 dv
= malloc(sizeof(*dv
));
8774 dv
->dev
= malloc(len
);
8779 update
->space
= NULL
;
8783 /* count how many spares will be converted to members */
8784 for (i
= 0; i
< map
->num_members
; i
++) {
8785 dl
= serial_to_dl(inf
[i
].serial
, super
);
8787 /* hmm maybe it failed?, nothing we can do about
8792 if (count_memberships(dl
, super
) == 0)
8795 len
+= activate
* sizeof(struct imsm_disk
);
8802 /* check if we need a larger metadata buffer */
8803 if (super
->next_buf
)
8804 buf_len
= super
->next_len
;
8806 buf_len
= super
->len
;
8808 if (__le32_to_cpu(mpb
->mpb_size
) + len
> buf_len
) {
8809 /* ok we need a larger buf than what is currently allocated
8810 * if this allocation fails process_update will notice that
8811 * ->next_len is set and ->next_buf is NULL
8813 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + len
, 512);
8814 if (super
->next_buf
)
8815 free(super
->next_buf
);
8817 super
->next_len
= buf_len
;
8818 if (posix_memalign(&super
->next_buf
, 512, buf_len
) == 0)
8819 memset(super
->next_buf
, 0, buf_len
);
8821 super
->next_buf
= NULL
;
8825 /* must be called while manager is quiesced */
8826 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
8828 struct imsm_super
*mpb
= super
->anchor
;
8830 struct imsm_dev
*dev
;
8831 struct imsm_map
*map
;
8832 int i
, j
, num_members
;
8835 dprintf("%s: deleting device[%d] from imsm_super\n",
8838 /* shift all indexes down one */
8839 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
8840 if (iter
->index
> (int)index
)
8842 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
8843 if (iter
->index
> (int)index
)
8846 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8847 dev
= get_imsm_dev(super
, i
);
8848 map
= get_imsm_map(dev
, MAP_0
);
8849 num_members
= map
->num_members
;
8850 for (j
= 0; j
< num_members
; j
++) {
8851 /* update ord entries being careful not to propagate
8852 * ord-flags to the first map
8854 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_X
);
8856 if (ord_to_idx(ord
) <= index
)
8859 map
= get_imsm_map(dev
, MAP_0
);
8860 set_imsm_ord_tbl_ent(map
, j
, ord_to_idx(ord
- 1));
8861 map
= get_imsm_map(dev
, MAP_1
);
8863 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
8868 super
->updates_pending
++;
8870 struct dl
*dl
= *dlp
;
8872 *dlp
= (*dlp
)->next
;
8873 __free_imsm_disk(dl
);
8876 #endif /* MDASSEMBLE */
8878 static void close_targets(int *targets
, int new_disks
)
8885 for (i
= 0; i
< new_disks
; i
++) {
8886 if (targets
[i
] >= 0) {
8893 static int imsm_get_allowed_degradation(int level
, int raid_disks
,
8894 struct intel_super
*super
,
8895 struct imsm_dev
*dev
)
8901 struct imsm_map
*map
;
8904 ret_val
= raid_disks
/2;
8905 /* check map if all disks pairs not failed
8908 map
= get_imsm_map(dev
, MAP_0
);
8909 for (i
= 0; i
< ret_val
; i
++) {
8910 int degradation
= 0;
8911 if (get_imsm_disk(super
, i
) == NULL
)
8913 if (get_imsm_disk(super
, i
+ 1) == NULL
)
8915 if (degradation
== 2)
8918 map
= get_imsm_map(dev
, MAP_1
);
8919 /* if there is no second map
8920 * result can be returned
8924 /* check degradation in second map
8926 for (i
= 0; i
< ret_val
; i
++) {
8927 int degradation
= 0;
8928 if (get_imsm_disk(super
, i
) == NULL
)
8930 if (get_imsm_disk(super
, i
+ 1) == NULL
)
8932 if (degradation
== 2)
8947 /*******************************************************************************
8948 * Function: open_backup_targets
8949 * Description: Function opens file descriptors for all devices given in
8952 * info : general array info
8953 * raid_disks : number of disks
8954 * raid_fds : table of device's file descriptors
8955 * super : intel super for raid10 degradation check
8956 * dev : intel device for raid10 degradation check
8960 ******************************************************************************/
8961 int open_backup_targets(struct mdinfo
*info
, int raid_disks
, int *raid_fds
,
8962 struct intel_super
*super
, struct imsm_dev
*dev
)
8968 for (i
= 0; i
< raid_disks
; i
++)
8971 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
8974 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
)) {
8975 dprintf("disk is faulty!!\n");
8979 if ((sd
->disk
.raid_disk
>= raid_disks
) ||
8980 (sd
->disk
.raid_disk
< 0))
8983 dn
= map_dev(sd
->disk
.major
,
8985 raid_fds
[sd
->disk
.raid_disk
] = dev_open(dn
, O_RDWR
);
8986 if (raid_fds
[sd
->disk
.raid_disk
] < 0) {
8987 fprintf(stderr
, "cannot open component\n");
8992 /* check if maximum array degradation level is not exceeded
8994 if ((raid_disks
- opened
) >
8995 imsm_get_allowed_degradation(info
->new_level
,
8998 fprintf(stderr
, "Not enough disks can be opened.\n");
8999 close_targets(raid_fds
, raid_disks
);
9006 /*******************************************************************************
9007 * Function: init_migr_record_imsm
9008 * Description: Function inits imsm migration record
9010 * super : imsm internal array info
9011 * dev : device under migration
9012 * info : general array info to find the smallest device
9015 ******************************************************************************/
9016 void init_migr_record_imsm(struct supertype
*st
, struct imsm_dev
*dev
,
9017 struct mdinfo
*info
)
9019 struct intel_super
*super
= st
->sb
;
9020 struct migr_record
*migr_rec
= super
->migr_rec
;
9022 unsigned long long dsize
, dev_sectors
;
9023 long long unsigned min_dev_sectors
= -1LLU;
9027 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
9028 struct imsm_map
*map_src
= get_imsm_map(dev
, MAP_1
);
9029 unsigned long long num_migr_units
;
9030 unsigned long long array_blocks
;
9032 memset(migr_rec
, 0, sizeof(struct migr_record
));
9033 migr_rec
->family_num
= __cpu_to_le32(super
->anchor
->family_num
);
9035 /* only ascending reshape supported now */
9036 migr_rec
->ascending_migr
= __cpu_to_le32(1);
9038 migr_rec
->dest_depth_per_unit
= GEN_MIGR_AREA_SIZE
/
9039 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
9040 migr_rec
->dest_depth_per_unit
*=
9041 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
9042 new_data_disks
= imsm_num_data_members(dev
, MAP_0
);
9043 migr_rec
->blocks_per_unit
=
9044 __cpu_to_le32(migr_rec
->dest_depth_per_unit
* new_data_disks
);
9045 migr_rec
->dest_depth_per_unit
=
9046 __cpu_to_le32(migr_rec
->dest_depth_per_unit
);
9047 array_blocks
= info
->component_size
* new_data_disks
;
9049 array_blocks
/ __le32_to_cpu(migr_rec
->blocks_per_unit
);
9051 if (array_blocks
% __le32_to_cpu(migr_rec
->blocks_per_unit
))
9053 migr_rec
->num_migr_units
= __cpu_to_le32(num_migr_units
);
9055 migr_rec
->post_migr_vol_cap
= dev
->size_low
;
9056 migr_rec
->post_migr_vol_cap_hi
= dev
->size_high
;
9059 /* Find the smallest dev */
9060 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
9061 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
9062 fd
= dev_open(nm
, O_RDONLY
);
9065 get_dev_size(fd
, NULL
, &dsize
);
9066 dev_sectors
= dsize
/ 512;
9067 if (dev_sectors
< min_dev_sectors
)
9068 min_dev_sectors
= dev_sectors
;
9071 migr_rec
->ckpt_area_pba
= __cpu_to_le32(min_dev_sectors
-
9072 RAID_DISK_RESERVED_BLOCKS_IMSM_HI
);
9074 write_imsm_migr_rec(st
);
9079 /*******************************************************************************
9080 * Function: save_backup_imsm
9081 * Description: Function saves critical data stripes to Migration Copy Area
9082 * and updates the current migration unit status.
9083 * Use restore_stripes() to form a destination stripe,
9084 * and to write it to the Copy Area.
9086 * st : supertype information
9087 * dev : imsm device that backup is saved for
9088 * info : general array info
9089 * buf : input buffer
9090 * length : length of data to backup (blocks_per_unit)
9094 ******************************************************************************/
9095 int save_backup_imsm(struct supertype
*st
,
9096 struct imsm_dev
*dev
,
9097 struct mdinfo
*info
,
9102 struct intel_super
*super
= st
->sb
;
9103 unsigned long long *target_offsets
= NULL
;
9104 int *targets
= NULL
;
9106 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
9107 int new_disks
= map_dest
->num_members
;
9108 int dest_layout
= 0;
9110 unsigned long long start
;
9111 int data_disks
= imsm_num_data_members(dev
, MAP_0
);
9113 targets
= malloc(new_disks
* sizeof(int));
9117 for (i
= 0; i
< new_disks
; i
++)
9120 target_offsets
= malloc(new_disks
* sizeof(unsigned long long));
9121 if (!target_offsets
)
9124 start
= info
->reshape_progress
* 512;
9125 for (i
= 0; i
< new_disks
; i
++) {
9126 target_offsets
[i
] = (unsigned long long)
9127 __le32_to_cpu(super
->migr_rec
->ckpt_area_pba
) * 512;
9128 /* move back copy area adderss, it will be moved forward
9129 * in restore_stripes() using start input variable
9131 target_offsets
[i
] -= start
/data_disks
;
9134 if (open_backup_targets(info
, new_disks
, targets
,
9138 dest_layout
= imsm_level_to_layout(map_dest
->raid_level
);
9139 dest_chunk
= __le16_to_cpu(map_dest
->blocks_per_strip
) * 512;
9141 if (restore_stripes(targets
, /* list of dest devices */
9142 target_offsets
, /* migration record offsets */
9145 map_dest
->raid_level
,
9147 -1, /* source backup file descriptor */
9148 0, /* input buf offset
9149 * always 0 buf is already offseted */
9153 fprintf(stderr
, Name
": Error restoring stripes\n");
9161 close_targets(targets
, new_disks
);
9164 free(target_offsets
);
9169 /*******************************************************************************
9170 * Function: save_checkpoint_imsm
9171 * Description: Function called for current unit status update
9172 * in the migration record. It writes it to disk.
9174 * super : imsm internal array info
9175 * info : general array info
9179 * 2: failure, means no valid migration record
9180 * / no general migration in progress /
9181 ******************************************************************************/
9182 int save_checkpoint_imsm(struct supertype
*st
, struct mdinfo
*info
, int state
)
9184 struct intel_super
*super
= st
->sb
;
9185 unsigned long long blocks_per_unit
;
9186 unsigned long long curr_migr_unit
;
9188 if (load_imsm_migr_rec(super
, info
) != 0) {
9189 dprintf("imsm: ERROR: Cannot read migration record "
9190 "for checkpoint save.\n");
9194 blocks_per_unit
= __le32_to_cpu(super
->migr_rec
->blocks_per_unit
);
9195 if (blocks_per_unit
== 0) {
9196 dprintf("imsm: no migration in progress.\n");
9199 curr_migr_unit
= info
->reshape_progress
/ blocks_per_unit
;
9200 /* check if array is alligned to copy area
9201 * if it is not alligned, add one to current migration unit value
9202 * this can happend on array reshape finish only
9204 if (info
->reshape_progress
% blocks_per_unit
)
9207 super
->migr_rec
->curr_migr_unit
=
9208 __cpu_to_le32(curr_migr_unit
);
9209 super
->migr_rec
->rec_status
= __cpu_to_le32(state
);
9210 super
->migr_rec
->dest_1st_member_lba
=
9211 __cpu_to_le32(curr_migr_unit
*
9212 __le32_to_cpu(super
->migr_rec
->dest_depth_per_unit
));
9213 if (write_imsm_migr_rec(st
) < 0) {
9214 dprintf("imsm: Cannot write migration record "
9215 "outside backup area\n");
9222 /*******************************************************************************
9223 * Function: recover_backup_imsm
9224 * Description: Function recovers critical data from the Migration Copy Area
9225 * while assembling an array.
9227 * super : imsm internal array info
9228 * info : general array info
9230 * 0 : success (or there is no data to recover)
9232 ******************************************************************************/
9233 int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
)
9235 struct intel_super
*super
= st
->sb
;
9236 struct migr_record
*migr_rec
= super
->migr_rec
;
9237 struct imsm_map
*map_dest
= NULL
;
9238 struct intel_dev
*id
= NULL
;
9239 unsigned long long read_offset
;
9240 unsigned long long write_offset
;
9242 int *targets
= NULL
;
9243 int new_disks
, i
, err
;
9246 unsigned long curr_migr_unit
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
9247 unsigned long num_migr_units
= __le32_to_cpu(migr_rec
->num_migr_units
);
9249 int skipped_disks
= 0;
9251 err
= sysfs_get_str(info
, NULL
, "array_state", (char *)buffer
, 20);
9255 /* recover data only during assemblation */
9256 if (strncmp(buffer
, "inactive", 8) != 0)
9258 /* no data to recover */
9259 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
9261 if (curr_migr_unit
>= num_migr_units
)
9264 /* find device during reshape */
9265 for (id
= super
->devlist
; id
; id
= id
->next
)
9266 if (is_gen_migration(id
->dev
))
9271 map_dest
= get_imsm_map(id
->dev
, MAP_0
);
9272 new_disks
= map_dest
->num_members
;
9274 read_offset
= (unsigned long long)
9275 __le32_to_cpu(migr_rec
->ckpt_area_pba
) * 512;
9277 write_offset
= ((unsigned long long)
9278 __le32_to_cpu(migr_rec
->dest_1st_member_lba
) +
9279 pba_of_lba0(map_dest
)) * 512;
9281 unit_len
= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
9282 if (posix_memalign((void **)&buf
, 512, unit_len
) != 0)
9284 targets
= malloc(new_disks
* sizeof(int));
9288 if (open_backup_targets(info
, new_disks
, targets
, super
, id
->dev
)) {
9290 Name
": Cannot open some devices belonging to array.\n");
9294 for (i
= 0; i
< new_disks
; i
++) {
9295 if (targets
[i
] < 0) {
9299 if (lseek64(targets
[i
], read_offset
, SEEK_SET
) < 0) {
9301 Name
": Cannot seek to block: %s\n",
9306 if ((unsigned)read(targets
[i
], buf
, unit_len
) != unit_len
) {
9308 Name
": Cannot read copy area block: %s\n",
9313 if (lseek64(targets
[i
], write_offset
, SEEK_SET
) < 0) {
9315 Name
": Cannot seek to block: %s\n",
9320 if ((unsigned)write(targets
[i
], buf
, unit_len
) != unit_len
) {
9322 Name
": Cannot restore block: %s\n",
9329 if (skipped_disks
> imsm_get_allowed_degradation(info
->new_level
,
9334 Name
": Cannot restore data from backup."
9335 " Too many failed disks\n");
9339 if (save_checkpoint_imsm(st
, info
, UNIT_SRC_NORMAL
)) {
9340 /* ignore error == 2, this can mean end of reshape here
9342 dprintf("imsm: Cannot write checkpoint to "
9343 "migration record (UNIT_SRC_NORMAL) during restart\n");
9349 for (i
= 0; i
< new_disks
; i
++)
9358 static char disk_by_path
[] = "/dev/disk/by-path/";
9360 static const char *imsm_get_disk_controller_domain(const char *path
)
9362 char disk_path
[PATH_MAX
];
9366 strncpy(disk_path
, disk_by_path
, PATH_MAX
- 1);
9367 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
9368 if (stat(disk_path
, &st
) == 0) {
9369 struct sys_dev
* hba
;
9372 path
= devt_to_devpath(st
.st_rdev
);
9375 hba
= find_disk_attached_hba(-1, path
);
9376 if (hba
&& hba
->type
== SYS_DEV_SAS
)
9378 else if (hba
&& hba
->type
== SYS_DEV_SATA
)
9382 dprintf("path: %s hba: %s attached: %s\n",
9383 path
, (hba
) ? hba
->path
: "NULL", drv
);
9391 static int imsm_find_array_minor_by_subdev(int subdev
, int container
, int *minor
)
9393 char subdev_name
[20];
9394 struct mdstat_ent
*mdstat
;
9396 sprintf(subdev_name
, "%d", subdev
);
9397 mdstat
= mdstat_by_subdev(subdev_name
, container
);
9401 *minor
= mdstat
->devnum
;
9402 free_mdstat(mdstat
);
9406 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
9407 struct geo_params
*geo
,
9408 int *old_raid_disks
)
9410 /* currently we only support increasing the number of devices
9411 * for a container. This increases the number of device for each
9412 * member array. They must all be RAID0 or RAID5.
9415 struct mdinfo
*info
, *member
;
9416 int devices_that_can_grow
= 0;
9418 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): "
9419 "st->devnum = (%i)\n",
9422 if (geo
->size
!= -1 ||
9423 geo
->level
!= UnSet
||
9424 geo
->layout
!= UnSet
||
9425 geo
->chunksize
!= 0 ||
9426 geo
->raid_disks
== UnSet
) {
9427 dprintf("imsm: Container operation is allowed for "
9428 "raid disks number change only.\n");
9432 info
= container_content_imsm(st
, NULL
);
9433 for (member
= info
; member
; member
= member
->next
) {
9437 dprintf("imsm: checking device_num: %i\n",
9438 member
->container_member
);
9440 if (geo
->raid_disks
<= member
->array
.raid_disks
) {
9441 /* we work on container for Online Capacity Expansion
9442 * only so raid_disks has to grow
9444 dprintf("imsm: for container operation raid disks "
9445 "increase is required\n");
9449 if ((info
->array
.level
!= 0) &&
9450 (info
->array
.level
!= 5)) {
9451 /* we cannot use this container with other raid level
9453 dprintf("imsm: for container operation wrong"
9454 " raid level (%i) detected\n",
9458 /* check for platform support
9459 * for this raid level configuration
9461 struct intel_super
*super
= st
->sb
;
9462 if (!is_raid_level_supported(super
->orom
,
9463 member
->array
.level
,
9465 dprintf("platform does not support raid%d with"
9469 geo
->raid_disks
> 1 ? "s" : "");
9472 /* check if component size is aligned to chunk size
9474 if (info
->component_size
%
9475 (info
->array
.chunk_size
/512)) {
9476 dprintf("Component size is not aligned to "
9482 if (*old_raid_disks
&&
9483 info
->array
.raid_disks
!= *old_raid_disks
)
9485 *old_raid_disks
= info
->array
.raid_disks
;
9487 /* All raid5 and raid0 volumes in container
9488 * have to be ready for Online Capacity Expansion
9489 * so they need to be assembled. We have already
9490 * checked that no recovery etc is happening.
9492 result
= imsm_find_array_minor_by_subdev(member
->container_member
,
9496 dprintf("imsm: cannot find array\n");
9499 devices_that_can_grow
++;
9502 if (!member
&& devices_that_can_grow
)
9506 dprintf("\tContainer operation allowed\n");
9508 dprintf("\tError: %i\n", ret_val
);
9513 /* Function: get_spares_for_grow
9514 * Description: Allocates memory and creates list of spare devices
9515 * avaliable in container. Checks if spare drive size is acceptable.
9516 * Parameters: Pointer to the supertype structure
9517 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
9520 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
9522 unsigned long long min_size
= min_acceptable_spare_size_imsm(st
);
9523 return container_choose_spares(st
, min_size
, NULL
, NULL
, NULL
, 0);
9526 /******************************************************************************
9527 * function: imsm_create_metadata_update_for_reshape
9528 * Function creates update for whole IMSM container.
9530 ******************************************************************************/
9531 static int imsm_create_metadata_update_for_reshape(
9532 struct supertype
*st
,
9533 struct geo_params
*geo
,
9535 struct imsm_update_reshape
**updatep
)
9537 struct intel_super
*super
= st
->sb
;
9538 struct imsm_super
*mpb
= super
->anchor
;
9539 int update_memory_size
= 0;
9540 struct imsm_update_reshape
*u
= NULL
;
9541 struct mdinfo
*spares
= NULL
;
9543 int delta_disks
= 0;
9546 dprintf("imsm_update_metadata_for_reshape(enter) raid_disks = %i\n",
9549 delta_disks
= geo
->raid_disks
- old_raid_disks
;
9551 /* size of all update data without anchor */
9552 update_memory_size
= sizeof(struct imsm_update_reshape
);
9554 /* now add space for spare disks that we need to add. */
9555 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
9557 u
= calloc(1, update_memory_size
);
9560 "cannot get memory for imsm_update_reshape update\n");
9563 u
->type
= update_reshape_container_disks
;
9564 u
->old_raid_disks
= old_raid_disks
;
9565 u
->new_raid_disks
= geo
->raid_disks
;
9567 /* now get spare disks list
9569 spares
= get_spares_for_grow(st
);
9572 || delta_disks
> spares
->array
.spare_disks
) {
9573 fprintf(stderr
, Name
": imsm: ERROR: Cannot get spare devices "
9574 "for %s.\n", geo
->dev_name
);
9579 /* we have got spares
9580 * update disk list in imsm_disk list table in anchor
9582 dprintf("imsm: %i spares are available.\n\n",
9583 spares
->array
.spare_disks
);
9586 for (i
= 0; i
< delta_disks
; i
++) {
9591 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
9593 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
9594 dl
->index
= mpb
->num_disks
;
9604 dprintf("imsm: reshape update preparation :");
9605 if (i
== delta_disks
) {
9608 return update_memory_size
;
9611 dprintf(" Error\n");
9616 /******************************************************************************
9617 * function: imsm_create_metadata_update_for_migration()
9618 * Creates update for IMSM array.
9620 ******************************************************************************/
9621 static int imsm_create_metadata_update_for_migration(
9622 struct supertype
*st
,
9623 struct geo_params
*geo
,
9624 struct imsm_update_reshape_migration
**updatep
)
9626 struct intel_super
*super
= st
->sb
;
9627 int update_memory_size
= 0;
9628 struct imsm_update_reshape_migration
*u
= NULL
;
9629 struct imsm_dev
*dev
;
9630 int previous_level
= -1;
9632 dprintf("imsm_create_metadata_update_for_migration(enter)"
9633 " New Level = %i\n", geo
->level
);
9635 /* size of all update data without anchor */
9636 update_memory_size
= sizeof(struct imsm_update_reshape_migration
);
9638 u
= calloc(1, update_memory_size
);
9640 dprintf("error: cannot get memory for "
9641 "imsm_create_metadata_update_for_migration\n");
9644 u
->type
= update_reshape_migration
;
9645 u
->subdev
= super
->current_vol
;
9646 u
->new_level
= geo
->level
;
9647 u
->new_layout
= geo
->layout
;
9648 u
->new_raid_disks
= u
->old_raid_disks
= geo
->raid_disks
;
9649 u
->new_disks
[0] = -1;
9650 u
->new_chunksize
= -1;
9652 dev
= get_imsm_dev(super
, u
->subdev
);
9654 struct imsm_map
*map
;
9656 map
= get_imsm_map(dev
, MAP_0
);
9658 int current_chunk_size
=
9659 __le16_to_cpu(map
->blocks_per_strip
) / 2;
9661 if (geo
->chunksize
!= current_chunk_size
) {
9662 u
->new_chunksize
= geo
->chunksize
/ 1024;
9664 "chunk size change from %i to %i\n",
9665 current_chunk_size
, u
->new_chunksize
);
9667 previous_level
= map
->raid_level
;
9670 if ((geo
->level
== 5) && (previous_level
== 0)) {
9671 struct mdinfo
*spares
= NULL
;
9673 u
->new_raid_disks
++;
9674 spares
= get_spares_for_grow(st
);
9675 if ((spares
== NULL
) || (spares
->array
.spare_disks
< 1)) {
9678 update_memory_size
= 0;
9679 dprintf("error: cannot get spare device "
9680 "for requested migration");
9685 dprintf("imsm: reshape update preparation : OK\n");
9688 return update_memory_size
;
9691 static void imsm_update_metadata_locally(struct supertype
*st
,
9694 struct metadata_update mu
;
9699 mu
.space_list
= NULL
;
9701 imsm_prepare_update(st
, &mu
);
9702 imsm_process_update(st
, &mu
);
9704 while (mu
.space_list
) {
9705 void **space
= mu
.space_list
;
9706 mu
.space_list
= *space
;
9711 /***************************************************************************
9712 * Function: imsm_analyze_change
9713 * Description: Function analyze change for single volume
9714 * and validate if transition is supported
9715 * Parameters: Geometry parameters, supertype structure
9716 * Returns: Operation type code on success, -1 if fail
9717 ****************************************************************************/
9718 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
9719 struct geo_params
*geo
)
9725 /* number of added/removed disks in operation result */
9726 int devNumChange
= 0;
9727 /* imsm compatible layout value for array geometry verification */
9728 int imsm_layout
= -1;
9730 getinfo_super_imsm_volume(st
, &info
, NULL
);
9731 if ((geo
->level
!= info
.array
.level
) &&
9732 (geo
->level
>= 0) &&
9733 (geo
->level
!= UnSet
)) {
9734 switch (info
.array
.level
) {
9736 if (geo
->level
== 5) {
9737 change
= CH_MIGRATION
;
9738 if (geo
->layout
!= ALGORITHM_LEFT_ASYMMETRIC
) {
9740 Name
" Error. Requested Layout "
9741 "not supported (left-asymmetric layout "
9742 "is supported only)!\n");
9744 goto analyse_change_exit
;
9746 imsm_layout
= geo
->layout
;
9748 devNumChange
= 1; /* parity disk added */
9749 } else if (geo
->level
== 10) {
9750 change
= CH_TAKEOVER
;
9752 devNumChange
= 2; /* two mirrors added */
9753 imsm_layout
= 0x102; /* imsm supported layout */
9758 if (geo
->level
== 0) {
9759 change
= CH_TAKEOVER
;
9761 devNumChange
= -(geo
->raid_disks
/2);
9762 imsm_layout
= 0; /* imsm raid0 layout */
9768 Name
" Error. Level Migration from %d to %d "
9770 info
.array
.level
, geo
->level
);
9771 goto analyse_change_exit
;
9774 geo
->level
= info
.array
.level
;
9776 if ((geo
->layout
!= info
.array
.layout
)
9777 && ((geo
->layout
!= UnSet
) && (geo
->layout
!= -1))) {
9778 change
= CH_MIGRATION
;
9779 if ((info
.array
.layout
== 0)
9780 && (info
.array
.level
== 5)
9781 && (geo
->layout
== 5)) {
9782 /* reshape 5 -> 4 */
9783 } else if ((info
.array
.layout
== 5)
9784 && (info
.array
.level
== 5)
9785 && (geo
->layout
== 0)) {
9786 /* reshape 4 -> 5 */
9791 Name
" Error. Layout Migration from %d to %d "
9793 info
.array
.layout
, geo
->layout
);
9795 goto analyse_change_exit
;
9798 geo
->layout
= info
.array
.layout
;
9799 if (imsm_layout
== -1)
9800 imsm_layout
= info
.array
.layout
;
9803 if ((geo
->chunksize
> 0) && (geo
->chunksize
!= UnSet
)
9804 && (geo
->chunksize
!= info
.array
.chunk_size
))
9805 change
= CH_MIGRATION
;
9807 geo
->chunksize
= info
.array
.chunk_size
;
9809 chunk
= geo
->chunksize
/ 1024;
9810 if (!validate_geometry_imsm(st
,
9813 geo
->raid_disks
+ devNumChange
,
9820 struct intel_super
*super
= st
->sb
;
9821 struct imsm_super
*mpb
= super
->anchor
;
9823 if (mpb
->num_raid_devs
> 1) {
9825 Name
" Error. Cannot perform operation on %s"
9826 "- for this operation it MUST be single "
9827 "array in container\n",
9833 analyse_change_exit
:
9838 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
9840 struct intel_super
*super
= st
->sb
;
9841 struct imsm_update_takeover
*u
;
9843 u
= malloc(sizeof(struct imsm_update_takeover
));
9847 u
->type
= update_takeover
;
9848 u
->subarray
= super
->current_vol
;
9850 /* 10->0 transition */
9851 if (geo
->level
== 0)
9852 u
->direction
= R10_TO_R0
;
9854 /* 0->10 transition */
9855 if (geo
->level
== 10)
9856 u
->direction
= R0_TO_R10
;
9858 /* update metadata locally */
9859 imsm_update_metadata_locally(st
, u
,
9860 sizeof(struct imsm_update_takeover
));
9861 /* and possibly remotely */
9862 if (st
->update_tail
)
9863 append_metadata_update(st
, u
,
9864 sizeof(struct imsm_update_takeover
));
9871 static int imsm_reshape_super(struct supertype
*st
, long long size
, int level
,
9872 int layout
, int chunksize
, int raid_disks
,
9873 int delta_disks
, char *backup
, char *dev
,
9877 struct geo_params geo
;
9879 dprintf("imsm: reshape_super called.\n");
9881 memset(&geo
, 0, sizeof(struct geo_params
));
9884 geo
.dev_id
= st
->devnum
;
9887 geo
.layout
= layout
;
9888 geo
.chunksize
= chunksize
;
9889 geo
.raid_disks
= raid_disks
;
9890 if (delta_disks
!= UnSet
)
9891 geo
.raid_disks
+= delta_disks
;
9893 dprintf("\tfor level : %i\n", geo
.level
);
9894 dprintf("\tfor raid_disks : %i\n", geo
.raid_disks
);
9896 if (experimental() == 0)
9899 if (st
->container_dev
== st
->devnum
) {
9900 /* On container level we can only increase number of devices. */
9901 dprintf("imsm: info: Container operation\n");
9902 int old_raid_disks
= 0;
9904 if (imsm_reshape_is_allowed_on_container(
9905 st
, &geo
, &old_raid_disks
)) {
9906 struct imsm_update_reshape
*u
= NULL
;
9909 len
= imsm_create_metadata_update_for_reshape(
9910 st
, &geo
, old_raid_disks
, &u
);
9913 dprintf("imsm: Cannot prepare update\n");
9914 goto exit_imsm_reshape_super
;
9918 /* update metadata locally */
9919 imsm_update_metadata_locally(st
, u
, len
);
9920 /* and possibly remotely */
9921 if (st
->update_tail
)
9922 append_metadata_update(st
, u
, len
);
9927 fprintf(stderr
, Name
": (imsm) Operation "
9928 "is not allowed on this container\n");
9931 /* On volume level we support following operations
9932 * - takeover: raid10 -> raid0; raid0 -> raid10
9933 * - chunk size migration
9934 * - migration: raid5 -> raid0; raid0 -> raid5
9936 struct intel_super
*super
= st
->sb
;
9937 struct intel_dev
*dev
= super
->devlist
;
9939 dprintf("imsm: info: Volume operation\n");
9940 /* find requested device */
9942 if (imsm_find_array_minor_by_subdev(
9943 dev
->index
, st
->container_dev
, &devnum
) == 0
9944 && devnum
== geo
.dev_id
)
9949 fprintf(stderr
, Name
" Cannot find %s (%i) subarray\n",
9950 geo
.dev_name
, geo
.dev_id
);
9951 goto exit_imsm_reshape_super
;
9953 super
->current_vol
= dev
->index
;
9954 change
= imsm_analyze_change(st
, &geo
);
9957 ret_val
= imsm_takeover(st
, &geo
);
9959 case CH_MIGRATION
: {
9960 struct imsm_update_reshape_migration
*u
= NULL
;
9962 imsm_create_metadata_update_for_migration(
9966 "Cannot prepare update\n");
9970 /* update metadata locally */
9971 imsm_update_metadata_locally(st
, u
, len
);
9972 /* and possibly remotely */
9973 if (st
->update_tail
)
9974 append_metadata_update(st
, u
, len
);
9984 exit_imsm_reshape_super
:
9985 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
9989 /*******************************************************************************
9990 * Function: wait_for_reshape_imsm
9991 * Description: Function writes new sync_max value and waits until
9992 * reshape process reach new position
9994 * sra : general array info
9995 * ndata : number of disks in new array's layout
9998 * 1 : there is no reshape in progress,
10000 ******************************************************************************/
10001 int wait_for_reshape_imsm(struct mdinfo
*sra
, int ndata
)
10003 int fd
= sysfs_get_fd(sra
, NULL
, "reshape_position");
10004 unsigned long long completed
;
10005 /* to_complete : new sync_max position */
10006 unsigned long long to_complete
= sra
->reshape_progress
;
10007 unsigned long long position_to_set
= to_complete
/ ndata
;
10010 dprintf("imsm: wait_for_reshape_imsm() "
10011 "cannot open reshape_position\n");
10015 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
10016 dprintf("imsm: wait_for_reshape_imsm() "
10017 "cannot read reshape_position (no reshape in progres)\n");
10022 if (completed
> to_complete
) {
10023 dprintf("imsm: wait_for_reshape_imsm() "
10024 "wrong next position to set %llu (%llu)\n",
10025 to_complete
, completed
);
10029 dprintf("Position set: %llu\n", position_to_set
);
10030 if (sysfs_set_num(sra
, NULL
, "sync_max",
10031 position_to_set
) != 0) {
10032 dprintf("imsm: wait_for_reshape_imsm() "
10033 "cannot set reshape position to %llu\n",
10044 select(fd
+1, &rfds
, NULL
, NULL
, NULL
);
10045 if (sysfs_get_str(sra
, NULL
, "sync_action",
10047 strncmp(action
, "reshape", 7) != 0)
10049 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
10050 dprintf("imsm: wait_for_reshape_imsm() "
10051 "cannot read reshape_position (in loop)\n");
10055 } while (completed
< to_complete
);
10061 /*******************************************************************************
10062 * Function: check_degradation_change
10063 * Description: Check that array hasn't become failed.
10065 * info : for sysfs access
10066 * sources : source disks descriptors
10067 * degraded: previous degradation level
10069 * degradation level
10070 ******************************************************************************/
10071 int check_degradation_change(struct mdinfo
*info
,
10075 unsigned long long new_degraded
;
10076 sysfs_get_ll(info
, NULL
, "degraded", &new_degraded
);
10077 if (new_degraded
!= (unsigned long long)degraded
) {
10078 /* check each device to ensure it is still working */
10081 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
10082 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
10084 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
10086 if (sysfs_get_str(info
,
10087 sd
, "state", sbuf
, 20) < 0 ||
10088 strstr(sbuf
, "faulty") ||
10089 strstr(sbuf
, "in_sync") == NULL
) {
10090 /* this device is dead */
10091 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
10092 if (sd
->disk
.raid_disk
>= 0 &&
10093 sources
[sd
->disk
.raid_disk
] >= 0) {
10095 sd
->disk
.raid_disk
]);
10096 sources
[sd
->disk
.raid_disk
] =
10105 return new_degraded
;
10108 /*******************************************************************************
10109 * Function: imsm_manage_reshape
10110 * Description: Function finds array under reshape and it manages reshape
10111 * process. It creates stripes backups (if required) and sets
10114 * afd : Backup handle (nattive) - not used
10115 * sra : general array info
10116 * reshape : reshape parameters - not used
10117 * st : supertype structure
10118 * blocks : size of critical section [blocks]
10119 * fds : table of source device descriptor
10120 * offsets : start of array (offest per devices)
10122 * destfd : table of destination device descriptor
10123 * destoffsets : table of destination offsets (per device)
10125 * 1 : success, reshape is done
10127 ******************************************************************************/
10128 static int imsm_manage_reshape(
10129 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
10130 struct supertype
*st
, unsigned long backup_blocks
,
10131 int *fds
, unsigned long long *offsets
,
10132 int dests
, int *destfd
, unsigned long long *destoffsets
)
10135 struct intel_super
*super
= st
->sb
;
10136 struct intel_dev
*dv
= NULL
;
10137 struct imsm_dev
*dev
= NULL
;
10138 struct imsm_map
*map_src
;
10139 int migr_vol_qan
= 0;
10140 int ndata
, odata
; /* [bytes] */
10141 int chunk
; /* [bytes] */
10142 struct migr_record
*migr_rec
;
10144 unsigned int buf_size
; /* [bytes] */
10145 unsigned long long max_position
; /* array size [bytes] */
10146 unsigned long long next_step
; /* [blocks]/[bytes] */
10147 unsigned long long old_data_stripe_length
;
10148 unsigned long long start_src
; /* [bytes] */
10149 unsigned long long start
; /* [bytes] */
10150 unsigned long long start_buf_shift
; /* [bytes] */
10152 int source_layout
= 0;
10154 if (!fds
|| !offsets
|| !sra
)
10157 /* Find volume during the reshape */
10158 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
10159 if (dv
->dev
->vol
.migr_type
== MIGR_GEN_MIGR
10160 && dv
->dev
->vol
.migr_state
== 1) {
10165 /* Only one volume can migrate at the same time */
10166 if (migr_vol_qan
!= 1) {
10167 fprintf(stderr
, Name
" : %s", migr_vol_qan
?
10168 "Number of migrating volumes greater than 1\n" :
10169 "There is no volume during migrationg\n");
10173 map_src
= get_imsm_map(dev
, MAP_1
);
10174 if (map_src
== NULL
)
10177 ndata
= imsm_num_data_members(dev
, MAP_0
);
10178 odata
= imsm_num_data_members(dev
, MAP_1
);
10180 chunk
= __le16_to_cpu(map_src
->blocks_per_strip
) * 512;
10181 old_data_stripe_length
= odata
* chunk
;
10183 migr_rec
= super
->migr_rec
;
10185 /* initialize migration record for start condition */
10186 if (sra
->reshape_progress
== 0)
10187 init_migr_record_imsm(st
, dev
, sra
);
10189 if (__le32_to_cpu(migr_rec
->rec_status
) != UNIT_SRC_NORMAL
) {
10190 dprintf("imsm: cannot restart migration when data "
10191 "are present in copy area.\n");
10194 /* Save checkpoint to update migration record for current
10195 * reshape position (in md). It can be farther than current
10196 * reshape position in metadata.
10198 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
10199 /* ignore error == 2, this can mean end of reshape here
10201 dprintf("imsm: Cannot write checkpoint to "
10202 "migration record (UNIT_SRC_NORMAL, "
10203 "initial save)\n");
10208 /* size for data */
10209 buf_size
= __le32_to_cpu(migr_rec
->blocks_per_unit
) * 512;
10210 /* extend buffer size for parity disk */
10211 buf_size
+= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
10212 /* add space for stripe aligment */
10213 buf_size
+= old_data_stripe_length
;
10214 if (posix_memalign((void **)&buf
, 4096, buf_size
)) {
10215 dprintf("imsm: Cannot allocate checpoint buffer\n");
10219 max_position
= sra
->component_size
* ndata
;
10220 source_layout
= imsm_level_to_layout(map_src
->raid_level
);
10222 while (__le32_to_cpu(migr_rec
->curr_migr_unit
) <
10223 __le32_to_cpu(migr_rec
->num_migr_units
)) {
10224 /* current reshape position [blocks] */
10225 unsigned long long current_position
=
10226 __le32_to_cpu(migr_rec
->blocks_per_unit
)
10227 * __le32_to_cpu(migr_rec
->curr_migr_unit
);
10228 unsigned long long border
;
10230 /* Check that array hasn't become failed.
10232 degraded
= check_degradation_change(sra
, fds
, degraded
);
10233 if (degraded
> 1) {
10234 dprintf("imsm: Abort reshape due to degradation"
10235 " level (%i)\n", degraded
);
10239 next_step
= __le32_to_cpu(migr_rec
->blocks_per_unit
);
10241 if ((current_position
+ next_step
) > max_position
)
10242 next_step
= max_position
- current_position
;
10244 start
= current_position
* 512;
10246 /* allign reading start to old geometry */
10247 start_buf_shift
= start
% old_data_stripe_length
;
10248 start_src
= start
- start_buf_shift
;
10250 border
= (start_src
/ odata
) - (start
/ ndata
);
10252 if (border
<= __le32_to_cpu(migr_rec
->dest_depth_per_unit
)) {
10253 /* save critical stripes to buf
10254 * start - start address of current unit
10255 * to backup [bytes]
10256 * start_src - start address of current unit
10257 * to backup alligned to source array
10260 unsigned long long next_step_filler
= 0;
10261 unsigned long long copy_length
= next_step
* 512;
10263 /* allign copy area length to stripe in old geometry */
10264 next_step_filler
= ((copy_length
+ start_buf_shift
)
10265 % old_data_stripe_length
);
10266 if (next_step_filler
)
10267 next_step_filler
= (old_data_stripe_length
10268 - next_step_filler
);
10269 dprintf("save_stripes() parameters: start = %llu,"
10270 "\tstart_src = %llu,\tnext_step*512 = %llu,"
10271 "\tstart_in_buf_shift = %llu,"
10272 "\tnext_step_filler = %llu\n",
10273 start
, start_src
, copy_length
,
10274 start_buf_shift
, next_step_filler
);
10276 if (save_stripes(fds
, offsets
, map_src
->num_members
,
10277 chunk
, map_src
->raid_level
,
10278 source_layout
, 0, NULL
, start_src
,
10280 next_step_filler
+ start_buf_shift
,
10282 dprintf("imsm: Cannot save stripes"
10286 /* Convert data to destination format and store it
10287 * in backup general migration area
10289 if (save_backup_imsm(st
, dev
, sra
,
10290 buf
+ start_buf_shift
, copy_length
)) {
10291 dprintf("imsm: Cannot save stripes to "
10292 "target devices\n");
10295 if (save_checkpoint_imsm(st
, sra
,
10296 UNIT_SRC_IN_CP_AREA
)) {
10297 dprintf("imsm: Cannot write checkpoint to "
10298 "migration record (UNIT_SRC_IN_CP_AREA)\n");
10302 /* set next step to use whole border area */
10303 border
/= next_step
;
10305 next_step
*= border
;
10307 /* When data backed up, checkpoint stored,
10308 * kick the kernel to reshape unit of data
10310 next_step
= next_step
+ sra
->reshape_progress
;
10311 /* limit next step to array max position */
10312 if (next_step
> max_position
)
10313 next_step
= max_position
;
10314 sysfs_set_num(sra
, NULL
, "suspend_lo", sra
->reshape_progress
);
10315 sysfs_set_num(sra
, NULL
, "suspend_hi", next_step
);
10316 sra
->reshape_progress
= next_step
;
10318 /* wait until reshape finish */
10319 if (wait_for_reshape_imsm(sra
, ndata
) < 0) {
10320 dprintf("wait_for_reshape_imsm returned error!\n");
10324 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
10325 /* ignore error == 2, this can mean end of reshape here
10327 dprintf("imsm: Cannot write checkpoint to "
10328 "migration record (UNIT_SRC_NORMAL)\n");
10334 /* return '1' if done */
10338 abort_reshape(sra
);
10342 #endif /* MDASSEMBLE */
10344 struct superswitch super_imsm
= {
10346 .examine_super
= examine_super_imsm
,
10347 .brief_examine_super
= brief_examine_super_imsm
,
10348 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
10349 .export_examine_super
= export_examine_super_imsm
,
10350 .detail_super
= detail_super_imsm
,
10351 .brief_detail_super
= brief_detail_super_imsm
,
10352 .write_init_super
= write_init_super_imsm
,
10353 .validate_geometry
= validate_geometry_imsm
,
10354 .add_to_super
= add_to_super_imsm
,
10355 .remove_from_super
= remove_from_super_imsm
,
10356 .detail_platform
= detail_platform_imsm
,
10357 .kill_subarray
= kill_subarray_imsm
,
10358 .update_subarray
= update_subarray_imsm
,
10359 .load_container
= load_container_imsm
,
10360 .default_geometry
= default_geometry_imsm
,
10361 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
10362 .reshape_super
= imsm_reshape_super
,
10363 .manage_reshape
= imsm_manage_reshape
,
10364 .recover_backup
= recover_backup_imsm
,
10366 .match_home
= match_home_imsm
,
10367 .uuid_from_super
= uuid_from_super_imsm
,
10368 .getinfo_super
= getinfo_super_imsm
,
10369 .getinfo_super_disks
= getinfo_super_disks_imsm
,
10370 .update_super
= update_super_imsm
,
10372 .avail_size
= avail_size_imsm
,
10373 .min_acceptable_spare_size
= min_acceptable_spare_size_imsm
,
10375 .compare_super
= compare_super_imsm
,
10377 .load_super
= load_super_imsm
,
10378 .init_super
= init_super_imsm
,
10379 .store_super
= store_super_imsm
,
10380 .free_super
= free_super_imsm
,
10381 .match_metadata_desc
= match_metadata_desc_imsm
,
10382 .container_content
= container_content_imsm
,
10390 .open_new
= imsm_open_new
,
10391 .set_array_state
= imsm_set_array_state
,
10392 .set_disk
= imsm_set_disk
,
10393 .sync_metadata
= imsm_sync_metadata
,
10394 .activate_spare
= imsm_activate_spare
,
10395 .process_update
= imsm_process_update
,
10396 .prepare_update
= imsm_prepare_update
,
10397 #endif /* MDASSEMBLE */