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(" Max Disks : %d\n", orom
->tds
);
1787 printf(" Max Volumes : %d\n", orom
->vpa
);
1788 printf(" 2TB volumes :%s supported\n",
1789 (orom
->attr
& IMSM_OROM_ATTR_2TB
)?"":" not");
1790 printf(" 2TB disks :%s supported\n",
1791 (orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
)?"":" not");
1795 static int detail_platform_imsm(int verbose
, int enumerate_only
)
1797 /* There are two components to imsm platform support, the ahci SATA
1798 * controller and the option-rom. To find the SATA controller we
1799 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
1800 * controller with the Intel vendor id is present. This approach
1801 * allows mdadm to leverage the kernel's ahci detection logic, with the
1802 * caveat that if ahci.ko is not loaded mdadm will not be able to
1803 * detect platform raid capabilities. The option-rom resides in a
1804 * platform "Adapter ROM". We scan for its signature to retrieve the
1805 * platform capabilities. If raid support is disabled in the BIOS the
1806 * option-rom capability structure will not be available.
1808 const struct imsm_orom
*orom
;
1809 struct sys_dev
*list
, *hba
;
1814 if (enumerate_only
) {
1815 if (check_env("IMSM_NO_PLATFORM"))
1817 list
= find_intel_devices();
1820 for (hba
= list
; hba
; hba
= hba
->next
) {
1821 orom
= find_imsm_capability(hba
->type
);
1827 free_sys_dev(&list
);
1831 list
= find_intel_devices();
1834 fprintf(stderr
, Name
": no active Intel(R) RAID "
1835 "controller found.\n");
1836 free_sys_dev(&list
);
1839 print_found_intel_controllers(list
);
1841 for (hba
= list
; hba
; hba
= hba
->next
) {
1842 orom
= find_imsm_capability(hba
->type
);
1844 fprintf(stderr
, Name
": imsm capabilities not found for controller: %s (type %s)\n",
1845 hba
->path
, get_sys_dev_type(hba
->type
));
1847 print_imsm_capability(orom
);
1850 for (hba
= list
; hba
; hba
= hba
->next
) {
1851 printf(" I/O Controller : %s (%s)\n",
1852 hba
->path
, get_sys_dev_type(hba
->type
));
1854 if (hba
->type
== SYS_DEV_SATA
) {
1855 host_base
= ahci_get_port_count(hba
->path
, &port_count
);
1856 if (ahci_enumerate_ports(hba
->path
, port_count
, host_base
, verbose
)) {
1858 fprintf(stderr
, Name
": failed to enumerate "
1859 "ports on SATA controller at %s.", hba
->pci_id
);
1865 free_sys_dev(&list
);
1870 static int match_home_imsm(struct supertype
*st
, char *homehost
)
1872 /* the imsm metadata format does not specify any host
1873 * identification information. We return -1 since we can never
1874 * confirm nor deny whether a given array is "meant" for this
1875 * host. We rely on compare_super and the 'family_num' fields to
1876 * exclude member disks that do not belong, and we rely on
1877 * mdadm.conf to specify the arrays that should be assembled.
1878 * Auto-assembly may still pick up "foreign" arrays.
1884 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
1886 /* The uuid returned here is used for:
1887 * uuid to put into bitmap file (Create, Grow)
1888 * uuid for backup header when saving critical section (Grow)
1889 * comparing uuids when re-adding a device into an array
1890 * In these cases the uuid required is that of the data-array,
1891 * not the device-set.
1892 * uuid to recognise same set when adding a missing device back
1893 * to an array. This is a uuid for the device-set.
1895 * For each of these we can make do with a truncated
1896 * or hashed uuid rather than the original, as long as
1898 * In each case the uuid required is that of the data-array,
1899 * not the device-set.
1901 /* imsm does not track uuid's so we synthesis one using sha1 on
1902 * - The signature (Which is constant for all imsm array, but no matter)
1903 * - the orig_family_num of the container
1904 * - the index number of the volume
1905 * - the 'serial' number of the volume.
1906 * Hopefully these are all constant.
1908 struct intel_super
*super
= st
->sb
;
1911 struct sha1_ctx ctx
;
1912 struct imsm_dev
*dev
= NULL
;
1915 /* some mdadm versions failed to set ->orig_family_num, in which
1916 * case fall back to ->family_num. orig_family_num will be
1917 * fixed up with the first metadata update.
1919 family_num
= super
->anchor
->orig_family_num
;
1920 if (family_num
== 0)
1921 family_num
= super
->anchor
->family_num
;
1922 sha1_init_ctx(&ctx
);
1923 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
1924 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
1925 if (super
->current_vol
>= 0)
1926 dev
= get_imsm_dev(super
, super
->current_vol
);
1928 __u32 vol
= super
->current_vol
;
1929 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
1930 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
1932 sha1_finish_ctx(&ctx
, buf
);
1933 memcpy(uuid
, buf
, 4*4);
1938 get_imsm_numerical_version(struct imsm_super
*mpb
, int *m
, int *p
)
1940 __u8
*v
= get_imsm_version(mpb
);
1941 __u8
*end
= mpb
->sig
+ MAX_SIGNATURE_LENGTH
;
1942 char major
[] = { 0, 0, 0 };
1943 char minor
[] = { 0 ,0, 0 };
1944 char patch
[] = { 0, 0, 0 };
1945 char *ver_parse
[] = { major
, minor
, patch
};
1949 while (*v
!= '\0' && v
< end
) {
1950 if (*v
!= '.' && j
< 2)
1951 ver_parse
[i
][j
++] = *v
;
1959 *m
= strtol(minor
, NULL
, 0);
1960 *p
= strtol(patch
, NULL
, 0);
1964 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
1966 /* migr_strip_size when repairing or initializing parity */
1967 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1968 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1970 switch (get_imsm_raid_level(map
)) {
1975 return 128*1024 >> 9;
1979 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
1981 /* migr_strip_size when rebuilding a degraded disk, no idea why
1982 * this is different than migr_strip_size_resync(), but it's good
1985 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
1986 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1988 switch (get_imsm_raid_level(map
)) {
1991 if (map
->num_members
% map
->num_domains
== 0)
1992 return 128*1024 >> 9;
1996 return max((__u32
) 64*1024 >> 9, chunk
);
1998 return 128*1024 >> 9;
2002 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
2004 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2005 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2006 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
2007 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
2009 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
2012 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
2014 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2015 int level
= get_imsm_raid_level(lo
);
2017 if (level
== 1 || level
== 10) {
2018 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2020 return hi
->num_domains
;
2022 return num_stripes_per_unit_resync(dev
);
2025 static __u8
imsm_num_data_members(struct imsm_dev
*dev
, int second_map
)
2027 /* named 'imsm_' because raid0, raid1 and raid10
2028 * counter-intuitively have the same number of data disks
2030 struct imsm_map
*map
= get_imsm_map(dev
, second_map
);
2032 switch (get_imsm_raid_level(map
)) {
2036 return map
->num_members
;
2038 return map
->num_members
- 1;
2040 dprintf("%s: unsupported raid level\n", __func__
);
2045 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
2047 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2048 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2050 switch(get_imsm_raid_level(map
)) {
2053 return chunk
* map
->num_domains
;
2055 return chunk
* map
->num_members
;
2061 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
2063 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2064 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2065 __u32 strip
= block
/ chunk
;
2067 switch (get_imsm_raid_level(map
)) {
2070 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
2071 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
2073 return vol_stripe
* chunk
+ block
% chunk
;
2075 __u32 stripe
= strip
/ (map
->num_members
- 1);
2077 return stripe
* chunk
+ block
% chunk
;
2084 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
2085 struct imsm_dev
*dev
)
2087 /* calculate the conversion factor between per member 'blocks'
2088 * (md/{resync,rebuild}_start) and imsm migration units, return
2089 * 0 for the 'not migrating' and 'unsupported migration' cases
2091 if (!dev
->vol
.migr_state
)
2094 switch (migr_type(dev
)) {
2095 case MIGR_GEN_MIGR
: {
2096 struct migr_record
*migr_rec
= super
->migr_rec
;
2097 return __le32_to_cpu(migr_rec
->blocks_per_unit
);
2102 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2103 __u32 stripes_per_unit
;
2104 __u32 blocks_per_unit
;
2113 /* yes, this is really the translation of migr_units to
2114 * per-member blocks in the 'resync' case
2116 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
2117 migr_chunk
= migr_strip_blocks_resync(dev
);
2118 disks
= imsm_num_data_members(dev
, MAP_0
);
2119 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
2120 stripe
= __le16_to_cpu(map
->blocks_per_strip
) * disks
;
2121 segment
= blocks_per_unit
/ stripe
;
2122 block_rel
= blocks_per_unit
- segment
* stripe
;
2123 parity_depth
= parity_segment_depth(dev
);
2124 block_map
= map_migr_block(dev
, block_rel
);
2125 return block_map
+ parity_depth
* segment
;
2127 case MIGR_REBUILD
: {
2128 __u32 stripes_per_unit
;
2131 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
2132 migr_chunk
= migr_strip_blocks_rebuild(dev
);
2133 return migr_chunk
* stripes_per_unit
;
2135 case MIGR_STATE_CHANGE
:
2141 static int imsm_level_to_layout(int level
)
2149 return ALGORITHM_LEFT_ASYMMETRIC
;
2156 /*******************************************************************************
2157 * Function: read_imsm_migr_rec
2158 * Description: Function reads imsm migration record from last sector of disk
2160 * fd : disk descriptor
2161 * super : metadata info
2165 ******************************************************************************/
2166 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
)
2169 unsigned long long dsize
;
2171 get_dev_size(fd
, NULL
, &dsize
);
2172 if (lseek64(fd
, dsize
- MIGR_REC_POSITION
, SEEK_SET
) < 0) {
2174 Name
": Cannot seek to anchor block: %s\n",
2178 if (read(fd
, super
->migr_rec_buf
, MIGR_REC_BUF_SIZE
) !=
2179 MIGR_REC_BUF_SIZE
) {
2181 Name
": Cannot read migr record block: %s\n",
2191 static struct imsm_dev
*imsm_get_device_during_migration(
2192 struct intel_super
*super
)
2195 struct intel_dev
*dv
;
2197 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
2198 if (is_gen_migration(dv
->dev
))
2204 /*******************************************************************************
2205 * Function: load_imsm_migr_rec
2206 * Description: Function reads imsm migration record (it is stored at the last
2209 * super : imsm internal array info
2210 * info : general array info
2214 * -2 : no migration in progress
2215 ******************************************************************************/
2216 static int load_imsm_migr_rec(struct intel_super
*super
, struct mdinfo
*info
)
2219 struct dl
*dl
= NULL
;
2223 struct imsm_dev
*dev
;
2224 struct imsm_map
*map
= NULL
;
2227 /* find map under migration */
2228 dev
= imsm_get_device_during_migration(super
);
2229 /* nothing to load,no migration in progress?
2233 map
= get_imsm_map(dev
, MAP_0
);
2236 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
2237 /* skip spare and failed disks
2239 if (sd
->disk
.raid_disk
< 0)
2241 /* read only from one of the first two slots */
2243 slot
= get_imsm_disk_slot(map
,
2244 sd
->disk
.raid_disk
);
2245 if ((map
== NULL
) || (slot
> 1) || (slot
< 0))
2248 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
2249 fd
= dev_open(nm
, O_RDONLY
);
2255 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
2256 /* skip spare and failed disks
2260 /* read only from one of the first two slots */
2262 slot
= get_imsm_disk_slot(map
, dl
->index
);
2263 if ((map
== NULL
) || (slot
> 1) || (slot
< 0))
2265 sprintf(nm
, "%d:%d", dl
->major
, dl
->minor
);
2266 fd
= dev_open(nm
, O_RDONLY
);
2273 retval
= read_imsm_migr_rec(fd
, super
);
2282 /*******************************************************************************
2283 * function: imsm_create_metadata_checkpoint_update
2284 * Description: It creates update for checkpoint change.
2286 * super : imsm internal array info
2287 * u : pointer to prepared update
2290 * If length is equal to 0, input pointer u contains no update
2291 ******************************************************************************/
2292 static int imsm_create_metadata_checkpoint_update(
2293 struct intel_super
*super
,
2294 struct imsm_update_general_migration_checkpoint
**u
)
2297 int update_memory_size
= 0;
2299 dprintf("imsm_create_metadata_checkpoint_update(enter)\n");
2305 /* size of all update data without anchor */
2306 update_memory_size
=
2307 sizeof(struct imsm_update_general_migration_checkpoint
);
2309 *u
= calloc(1, update_memory_size
);
2311 dprintf("error: cannot get memory for "
2312 "imsm_create_metadata_checkpoint_update update\n");
2315 (*u
)->type
= update_general_migration_checkpoint
;
2316 (*u
)->curr_migr_unit
= __le32_to_cpu(super
->migr_rec
->curr_migr_unit
);
2317 dprintf("imsm_create_metadata_checkpoint_update: prepared for %u\n",
2318 (*u
)->curr_migr_unit
);
2320 return update_memory_size
;
2324 static void imsm_update_metadata_locally(struct supertype
*st
,
2325 void *buf
, int len
);
2327 /*******************************************************************************
2328 * Function: write_imsm_migr_rec
2329 * Description: Function writes imsm migration record
2330 * (at the last sector of disk)
2332 * super : imsm internal array info
2336 ******************************************************************************/
2337 static int write_imsm_migr_rec(struct supertype
*st
)
2339 struct intel_super
*super
= st
->sb
;
2340 unsigned long long dsize
;
2346 struct imsm_update_general_migration_checkpoint
*u
;
2347 struct imsm_dev
*dev
;
2348 struct imsm_map
*map
= NULL
;
2350 /* find map under migration */
2351 dev
= imsm_get_device_during_migration(super
);
2352 /* if no migration, write buffer anyway to clear migr_record
2353 * on disk based on first available device
2356 dev
= get_imsm_dev(super
, super
->current_vol
< 0 ? 0 :
2357 super
->current_vol
);
2359 map
= get_imsm_map(dev
, MAP_0
);
2361 for (sd
= super
->disks
; sd
; sd
= sd
->next
) {
2364 /* skip failed and spare devices */
2367 /* write to 2 first slots only */
2369 slot
= get_imsm_disk_slot(map
, sd
->index
);
2370 if ((map
== NULL
) || (slot
> 1) || (slot
< 0))
2373 sprintf(nm
, "%d:%d", sd
->major
, sd
->minor
);
2374 fd
= dev_open(nm
, O_RDWR
);
2377 get_dev_size(fd
, NULL
, &dsize
);
2378 if (lseek64(fd
, dsize
- MIGR_REC_POSITION
, SEEK_SET
) < 0) {
2380 Name
": Cannot seek to anchor block: %s\n",
2384 if (write(fd
, super
->migr_rec_buf
, MIGR_REC_BUF_SIZE
) !=
2385 MIGR_REC_BUF_SIZE
) {
2387 Name
": Cannot write migr record block: %s\n",
2394 /* update checkpoint information in metadata */
2395 len
= imsm_create_metadata_checkpoint_update(super
, &u
);
2398 dprintf("imsm: Cannot prepare update\n");
2401 /* update metadata locally */
2402 imsm_update_metadata_locally(st
, u
, len
);
2403 /* and possibly remotely */
2404 if (st
->update_tail
) {
2405 append_metadata_update(st
, u
, len
);
2406 /* during reshape we do all work inside metadata handler
2407 * manage_reshape(), so metadata update has to be triggered
2410 flush_metadata_updates(st
);
2411 st
->update_tail
= &st
->updates
;
2421 #endif /* MDASSEMBLE */
2423 /* spare/missing disks activations are not allowe when
2424 * array/container performs reshape operation, because
2425 * all arrays in container works on the same disks set
2427 int imsm_reshape_blocks_arrays_changes(struct intel_super
*super
)
2430 struct intel_dev
*i_dev
;
2431 struct imsm_dev
*dev
;
2433 /* check whole container
2435 for (i_dev
= super
->devlist
; i_dev
; i_dev
= i_dev
->next
) {
2437 if (is_gen_migration(dev
)) {
2438 /* No repair during any migration in container
2447 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
2449 struct intel_super
*super
= st
->sb
;
2450 struct migr_record
*migr_rec
= super
->migr_rec
;
2451 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
2452 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2453 struct imsm_map
*prev_map
= get_imsm_map(dev
, MAP_1
);
2454 struct imsm_map
*map_to_analyse
= map
;
2457 unsigned int component_size_alligment
;
2458 int map_disks
= info
->array
.raid_disks
;
2460 memset(info
, 0, sizeof(*info
));
2462 map_to_analyse
= prev_map
;
2464 dl
= super
->current_disk
;
2466 info
->container_member
= super
->current_vol
;
2467 info
->array
.raid_disks
= map
->num_members
;
2468 info
->array
.level
= get_imsm_raid_level(map_to_analyse
);
2469 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
2470 info
->array
.md_minor
= -1;
2471 info
->array
.ctime
= 0;
2472 info
->array
.utime
= 0;
2473 info
->array
.chunk_size
=
2474 __le16_to_cpu(map_to_analyse
->blocks_per_strip
) << 9;
2475 info
->array
.state
= !dev
->vol
.dirty
;
2476 info
->custom_array_size
= __le32_to_cpu(dev
->size_high
);
2477 info
->custom_array_size
<<= 32;
2478 info
->custom_array_size
|= __le32_to_cpu(dev
->size_low
);
2479 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
2481 if (is_gen_migration(dev
)) {
2482 info
->reshape_active
= 1;
2483 info
->new_level
= get_imsm_raid_level(map
);
2484 info
->new_layout
= imsm_level_to_layout(info
->new_level
);
2485 info
->new_chunk
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
2486 info
->delta_disks
= map
->num_members
- prev_map
->num_members
;
2487 if (info
->delta_disks
) {
2488 /* this needs to be applied to every array
2491 info
->reshape_active
= CONTAINER_RESHAPE
;
2493 /* We shape information that we give to md might have to be
2494 * modify to cope with md's requirement for reshaping arrays.
2495 * For example, when reshaping a RAID0, md requires it to be
2496 * presented as a degraded RAID4.
2497 * Also if a RAID0 is migrating to a RAID5 we need to specify
2498 * the array as already being RAID5, but the 'before' layout
2499 * is a RAID4-like layout.
2501 switch (info
->array
.level
) {
2503 switch(info
->new_level
) {
2505 /* conversion is happening as RAID4 */
2506 info
->array
.level
= 4;
2507 info
->array
.raid_disks
+= 1;
2510 /* conversion is happening as RAID5 */
2511 info
->array
.level
= 5;
2512 info
->array
.layout
= ALGORITHM_PARITY_N
;
2513 info
->delta_disks
-= 1;
2516 /* FIXME error message */
2517 info
->array
.level
= UnSet
;
2523 info
->new_level
= UnSet
;
2524 info
->new_layout
= UnSet
;
2525 info
->new_chunk
= info
->array
.chunk_size
;
2526 info
->delta_disks
= 0;
2530 info
->disk
.major
= dl
->major
;
2531 info
->disk
.minor
= dl
->minor
;
2532 info
->disk
.number
= dl
->index
;
2533 info
->disk
.raid_disk
= get_imsm_disk_slot(map_to_analyse
,
2537 info
->data_offset
= pba_of_lba0(map_to_analyse
);
2538 info
->component_size
= blocks_per_member(map_to_analyse
);
2540 /* check component size aligment
2542 component_size_alligment
=
2543 info
->component_size
% (info
->array
.chunk_size
/512);
2545 if (component_size_alligment
&&
2546 (info
->array
.level
!= 1) && (info
->array
.level
!= UnSet
)) {
2547 dprintf("imsm: reported component size alligned from %llu ",
2548 info
->component_size
);
2549 info
->component_size
-= component_size_alligment
;
2550 dprintf("to %llu (%i).\n",
2551 info
->component_size
, component_size_alligment
);
2554 memset(info
->uuid
, 0, sizeof(info
->uuid
));
2555 info
->recovery_start
= MaxSector
;
2557 info
->reshape_progress
= 0;
2558 info
->resync_start
= MaxSector
;
2559 if ((map_to_analyse
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
2561 imsm_reshape_blocks_arrays_changes(super
) == 0) {
2562 info
->resync_start
= 0;
2564 if (dev
->vol
.migr_state
) {
2565 switch (migr_type(dev
)) {
2568 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
2570 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
2572 info
->resync_start
= blocks_per_unit
* units
;
2575 case MIGR_GEN_MIGR
: {
2576 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
2578 __u64 units
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
2579 unsigned long long array_blocks
;
2582 if (__le32_to_cpu(migr_rec
->ascending_migr
) &&
2584 (__le32_to_cpu(migr_rec
->num_migr_units
)-1)) &&
2585 (super
->migr_rec
->rec_status
==
2586 __cpu_to_le32(UNIT_SRC_IN_CP_AREA
)))
2589 info
->reshape_progress
= blocks_per_unit
* units
;
2591 dprintf("IMSM: General Migration checkpoint : %llu "
2592 "(%llu) -> read reshape progress : %llu\n",
2593 (unsigned long long)units
,
2594 (unsigned long long)blocks_per_unit
,
2595 info
->reshape_progress
);
2597 used_disks
= imsm_num_data_members(dev
, MAP_1
);
2598 if (used_disks
> 0) {
2599 array_blocks
= blocks_per_member(map
) *
2601 /* round array size down to closest MB
2603 info
->custom_array_size
= (array_blocks
2604 >> SECT_PER_MB_SHIFT
)
2605 << SECT_PER_MB_SHIFT
;
2609 /* we could emulate the checkpointing of
2610 * 'sync_action=check' migrations, but for now
2611 * we just immediately complete them
2614 /* this is handled by container_content_imsm() */
2615 case MIGR_STATE_CHANGE
:
2616 /* FIXME handle other migrations */
2618 /* we are not dirty, so... */
2619 info
->resync_start
= MaxSector
;
2623 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
2624 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
2626 info
->array
.major_version
= -1;
2627 info
->array
.minor_version
= -2;
2628 devname
= devnum2devname(st
->container_dev
);
2629 *info
->text_version
= '\0';
2631 sprintf(info
->text_version
, "/%s/%d", devname
, info
->container_member
);
2633 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
2634 uuid_from_super_imsm(st
, info
->uuid
);
2638 for (i
=0; i
<map_disks
; i
++) {
2640 if (i
< info
->array
.raid_disks
) {
2641 struct imsm_disk
*dsk
;
2642 j
= get_imsm_disk_idx(dev
, i
, MAP_X
);
2643 dsk
= get_imsm_disk(super
, j
);
2644 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
2651 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
2652 int failed
, int look_in_map
);
2654 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
2659 static void manage_second_map(struct intel_super
*super
, struct imsm_dev
*dev
)
2661 if (is_gen_migration(dev
)) {
2664 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
2666 failed
= imsm_count_failed(super
, dev
, MAP_1
);
2667 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_1
);
2668 if (map2
->map_state
!= map_state
) {
2669 map2
->map_state
= map_state
;
2670 super
->updates_pending
++;
2676 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
2680 for (d
= super
->missing
; d
; d
= d
->next
)
2681 if (d
->index
== index
)
2686 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
2688 struct intel_super
*super
= st
->sb
;
2689 struct imsm_disk
*disk
;
2690 int map_disks
= info
->array
.raid_disks
;
2691 int max_enough
= -1;
2693 struct imsm_super
*mpb
;
2695 if (super
->current_vol
>= 0) {
2696 getinfo_super_imsm_volume(st
, info
, map
);
2699 memset(info
, 0, sizeof(*info
));
2701 /* Set raid_disks to zero so that Assemble will always pull in valid
2704 info
->array
.raid_disks
= 0;
2705 info
->array
.level
= LEVEL_CONTAINER
;
2706 info
->array
.layout
= 0;
2707 info
->array
.md_minor
= -1;
2708 info
->array
.ctime
= 0; /* N/A for imsm */
2709 info
->array
.utime
= 0;
2710 info
->array
.chunk_size
= 0;
2712 info
->disk
.major
= 0;
2713 info
->disk
.minor
= 0;
2714 info
->disk
.raid_disk
= -1;
2715 info
->reshape_active
= 0;
2716 info
->array
.major_version
= -1;
2717 info
->array
.minor_version
= -2;
2718 strcpy(info
->text_version
, "imsm");
2719 info
->safe_mode_delay
= 0;
2720 info
->disk
.number
= -1;
2721 info
->disk
.state
= 0;
2723 info
->recovery_start
= MaxSector
;
2724 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
2726 /* do we have the all the insync disks that we expect? */
2727 mpb
= super
->anchor
;
2729 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
2730 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
2731 int failed
, enough
, j
, missing
= 0;
2732 struct imsm_map
*map
;
2735 failed
= imsm_count_failed(super
, dev
, MAP_0
);
2736 state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
2737 map
= get_imsm_map(dev
, MAP_0
);
2739 /* any newly missing disks?
2740 * (catches single-degraded vs double-degraded)
2742 for (j
= 0; j
< map
->num_members
; j
++) {
2743 __u32 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
2744 __u32 idx
= ord_to_idx(ord
);
2746 if (!(ord
& IMSM_ORD_REBUILD
) &&
2747 get_imsm_missing(super
, idx
)) {
2753 if (state
== IMSM_T_STATE_FAILED
)
2755 else if (state
== IMSM_T_STATE_DEGRADED
&&
2756 (state
!= map
->map_state
|| missing
))
2758 else /* we're normal, or already degraded */
2760 if (is_gen_migration(dev
) && missing
) {
2761 /* during general migration we need all disks
2762 * that process is running on.
2763 * No new missing disk is allowed.
2767 /* no more checks necessary
2771 /* in the missing/failed disk case check to see
2772 * if at least one array is runnable
2774 max_enough
= max(max_enough
, enough
);
2776 dprintf("%s: enough: %d\n", __func__
, max_enough
);
2777 info
->container_enough
= max_enough
;
2780 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
2782 disk
= &super
->disks
->disk
;
2783 info
->data_offset
= total_blocks(&super
->disks
->disk
) - reserved
;
2784 info
->component_size
= reserved
;
2785 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
2786 /* we don't change info->disk.raid_disk here because
2787 * this state will be finalized in mdmon after we have
2788 * found the 'most fresh' version of the metadata
2790 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
2791 info
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
2794 /* only call uuid_from_super_imsm when this disk is part of a populated container,
2795 * ->compare_super may have updated the 'num_raid_devs' field for spares
2797 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
2798 uuid_from_super_imsm(st
, info
->uuid
);
2800 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
2802 /* I don't know how to compute 'map' on imsm, so use safe default */
2805 for (i
= 0; i
< map_disks
; i
++)
2811 /* allocates memory and fills disk in mdinfo structure
2812 * for each disk in array */
2813 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
2815 struct mdinfo
*mddev
= NULL
;
2816 struct intel_super
*super
= st
->sb
;
2817 struct imsm_disk
*disk
;
2820 if (!super
|| !super
->disks
)
2823 mddev
= malloc(sizeof(*mddev
));
2825 fprintf(stderr
, Name
": Failed to allocate memory.\n");
2828 memset(mddev
, 0, sizeof(*mddev
));
2832 tmp
= malloc(sizeof(*tmp
));
2834 fprintf(stderr
, Name
": Failed to allocate memory.\n");
2839 memset(tmp
, 0, sizeof(*tmp
));
2841 tmp
->next
= mddev
->devs
;
2843 tmp
->disk
.number
= count
++;
2844 tmp
->disk
.major
= dl
->major
;
2845 tmp
->disk
.minor
= dl
->minor
;
2846 tmp
->disk
.state
= is_configured(disk
) ?
2847 (1 << MD_DISK_ACTIVE
) : 0;
2848 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
2849 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
2850 tmp
->disk
.raid_disk
= -1;
2856 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
2857 char *update
, char *devname
, int verbose
,
2858 int uuid_set
, char *homehost
)
2860 /* For 'assemble' and 'force' we need to return non-zero if any
2861 * change was made. For others, the return value is ignored.
2862 * Update options are:
2863 * force-one : This device looks a bit old but needs to be included,
2864 * update age info appropriately.
2865 * assemble: clear any 'faulty' flag to allow this device to
2867 * force-array: Array is degraded but being forced, mark it clean
2868 * if that will be needed to assemble it.
2870 * newdev: not used ????
2871 * grow: Array has gained a new device - this is currently for
2873 * resync: mark as dirty so a resync will happen.
2874 * name: update the name - preserving the homehost
2875 * uuid: Change the uuid of the array to match watch is given
2877 * Following are not relevant for this imsm:
2878 * sparc2.2 : update from old dodgey metadata
2879 * super-minor: change the preferred_minor number
2880 * summaries: update redundant counters.
2881 * homehost: update the recorded homehost
2882 * _reshape_progress: record new reshape_progress position.
2885 struct intel_super
*super
= st
->sb
;
2886 struct imsm_super
*mpb
;
2888 /* we can only update container info */
2889 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
2892 mpb
= super
->anchor
;
2894 if (strcmp(update
, "uuid") == 0) {
2895 /* We take this to mean that the family_num should be updated.
2896 * However that is much smaller than the uuid so we cannot really
2897 * allow an explicit uuid to be given. And it is hard to reliably
2899 * So if !uuid_set we know the current uuid is random and just used
2900 * the first 'int' and copy it to the other 3 positions.
2901 * Otherwise we require the 4 'int's to be the same as would be the
2902 * case if we are using a random uuid. So an explicit uuid will be
2903 * accepted as long as all for ints are the same... which shouldn't hurt
2906 info
->uuid
[1] = info
->uuid
[2] = info
->uuid
[3] = info
->uuid
[0];
2909 if (info
->uuid
[0] != info
->uuid
[1] ||
2910 info
->uuid
[1] != info
->uuid
[2] ||
2911 info
->uuid
[2] != info
->uuid
[3])
2917 mpb
->orig_family_num
= info
->uuid
[0];
2918 } else if (strcmp(update
, "assemble") == 0)
2923 /* successful update? recompute checksum */
2925 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
2930 static size_t disks_to_mpb_size(int disks
)
2934 size
= sizeof(struct imsm_super
);
2935 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
2936 size
+= 2 * sizeof(struct imsm_dev
);
2937 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
2938 size
+= (4 - 2) * sizeof(struct imsm_map
);
2939 /* 4 possible disk_ord_tbl's */
2940 size
+= 4 * (disks
- 1) * sizeof(__u32
);
2945 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
)
2947 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
2950 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
2953 static void free_devlist(struct intel_super
*super
)
2955 struct intel_dev
*dv
;
2957 while (super
->devlist
) {
2958 dv
= super
->devlist
->next
;
2959 free(super
->devlist
->dev
);
2960 free(super
->devlist
);
2961 super
->devlist
= dv
;
2965 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
2967 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
2970 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
)
2974 * 0 same, or first was empty, and second was copied
2975 * 1 second had wrong number
2977 * 3 wrong other info
2979 struct intel_super
*first
= st
->sb
;
2980 struct intel_super
*sec
= tst
->sb
;
2987 /* in platform dependent environment test if the disks
2988 * use the same Intel hba
2990 if (!check_env("IMSM_NO_PLATFORM")) {
2991 if (!first
->hba
|| !sec
->hba
||
2992 (first
->hba
->type
!= sec
->hba
->type
)) {
2994 "HBAs of devices does not match %s != %s\n",
2995 first
->hba
? get_sys_dev_type(first
->hba
->type
) : NULL
,
2996 sec
->hba
? get_sys_dev_type(sec
->hba
->type
) : NULL
);
3001 /* if an anchor does not have num_raid_devs set then it is a free
3004 if (first
->anchor
->num_raid_devs
> 0 &&
3005 sec
->anchor
->num_raid_devs
> 0) {
3006 /* Determine if these disks might ever have been
3007 * related. Further disambiguation can only take place
3008 * in load_super_imsm_all
3010 __u32 first_family
= first
->anchor
->orig_family_num
;
3011 __u32 sec_family
= sec
->anchor
->orig_family_num
;
3013 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
3014 MAX_SIGNATURE_LENGTH
) != 0)
3017 if (first_family
== 0)
3018 first_family
= first
->anchor
->family_num
;
3019 if (sec_family
== 0)
3020 sec_family
= sec
->anchor
->family_num
;
3022 if (first_family
!= sec_family
)
3028 /* if 'first' is a spare promote it to a populated mpb with sec's
3031 if (first
->anchor
->num_raid_devs
== 0 &&
3032 sec
->anchor
->num_raid_devs
> 0) {
3034 struct intel_dev
*dv
;
3035 struct imsm_dev
*dev
;
3037 /* we need to copy raid device info from sec if an allocation
3038 * fails here we don't associate the spare
3040 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++) {
3041 dv
= malloc(sizeof(*dv
));
3044 dev
= malloc(sizeof_imsm_dev(get_imsm_dev(sec
, i
), 1));
3051 dv
->next
= first
->devlist
;
3052 first
->devlist
= dv
;
3054 if (i
< sec
->anchor
->num_raid_devs
) {
3055 /* allocation failure */
3056 free_devlist(first
);
3057 fprintf(stderr
, "imsm: failed to associate spare\n");
3060 first
->anchor
->num_raid_devs
= sec
->anchor
->num_raid_devs
;
3061 first
->anchor
->orig_family_num
= sec
->anchor
->orig_family_num
;
3062 first
->anchor
->family_num
= sec
->anchor
->family_num
;
3063 memcpy(first
->anchor
->sig
, sec
->anchor
->sig
, MAX_SIGNATURE_LENGTH
);
3064 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++)
3065 imsm_copy_dev(get_imsm_dev(first
, i
), get_imsm_dev(sec
, i
));
3071 static void fd2devname(int fd
, char *name
)
3075 char dname
[PATH_MAX
];
3080 if (fstat(fd
, &st
) != 0)
3082 sprintf(path
, "/sys/dev/block/%d:%d",
3083 major(st
.st_rdev
), minor(st
.st_rdev
));
3085 rv
= readlink(path
, dname
, sizeof(dname
)-1);
3090 nm
= strrchr(dname
, '/');
3093 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
3097 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
3099 static int imsm_read_serial(int fd
, char *devname
,
3100 __u8 serial
[MAX_RAID_SERIAL_LEN
])
3102 unsigned char scsi_serial
[255];
3111 memset(scsi_serial
, 0, sizeof(scsi_serial
));
3113 rv
= scsi_get_serial(fd
, scsi_serial
, sizeof(scsi_serial
));
3115 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
3116 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3117 fd2devname(fd
, (char *) serial
);
3124 Name
": Failed to retrieve serial for %s\n",
3129 rsp_len
= scsi_serial
[3];
3133 Name
": Failed to retrieve serial for %s\n",
3137 rsp_buf
= (char *) &scsi_serial
[4];
3139 /* trim all whitespace and non-printable characters and convert
3142 for (i
= 0, dest
= rsp_buf
; i
< rsp_len
; i
++) {
3145 /* ':' is reserved for use in placeholder serial
3146 * numbers for missing disks
3154 len
= dest
- rsp_buf
;
3157 /* truncate leading characters */
3158 if (len
> MAX_RAID_SERIAL_LEN
) {
3159 dest
+= len
- MAX_RAID_SERIAL_LEN
;
3160 len
= MAX_RAID_SERIAL_LEN
;
3163 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3164 memcpy(serial
, dest
, len
);
3169 static int serialcmp(__u8
*s1
, __u8
*s2
)
3171 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
3174 static void serialcpy(__u8
*dest
, __u8
*src
)
3176 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
3179 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
3183 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3184 if (serialcmp(dl
->serial
, serial
) == 0)
3190 static struct imsm_disk
*
3191 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
3195 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3196 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3198 if (serialcmp(disk
->serial
, serial
) == 0) {
3209 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3211 struct imsm_disk
*disk
;
3216 __u8 serial
[MAX_RAID_SERIAL_LEN
];
3218 rv
= imsm_read_serial(fd
, devname
, serial
);
3223 dl
= calloc(1, sizeof(*dl
));
3227 Name
": failed to allocate disk buffer for %s\n",
3233 dl
->major
= major(stb
.st_rdev
);
3234 dl
->minor
= minor(stb
.st_rdev
);
3235 dl
->next
= super
->disks
;
3236 dl
->fd
= keep_fd
? fd
: -1;
3237 assert(super
->disks
== NULL
);
3239 serialcpy(dl
->serial
, serial
);
3242 fd2devname(fd
, name
);
3244 dl
->devname
= strdup(devname
);
3246 dl
->devname
= strdup(name
);
3248 /* look up this disk's index in the current anchor */
3249 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
3252 /* only set index on disks that are a member of a
3253 * populated contianer, i.e. one with raid_devs
3255 if (is_failed(&dl
->disk
))
3257 else if (is_spare(&dl
->disk
))
3265 /* When migrating map0 contains the 'destination' state while map1
3266 * contains the current state. When not migrating map0 contains the
3267 * current state. This routine assumes that map[0].map_state is set to
3268 * the current array state before being called.
3270 * Migration is indicated by one of the following states
3271 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
3272 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
3273 * map1state=unitialized)
3274 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
3276 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
3277 * map1state=degraded)
3278 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
3281 static void migrate(struct imsm_dev
*dev
, struct intel_super
*super
,
3282 __u8 to_state
, int migr_type
)
3284 struct imsm_map
*dest
;
3285 struct imsm_map
*src
= get_imsm_map(dev
, MAP_0
);
3287 dev
->vol
.migr_state
= 1;
3288 set_migr_type(dev
, migr_type
);
3289 dev
->vol
.curr_migr_unit
= 0;
3290 dest
= get_imsm_map(dev
, MAP_1
);
3292 /* duplicate and then set the target end state in map[0] */
3293 memcpy(dest
, src
, sizeof_imsm_map(src
));
3294 if ((migr_type
== MIGR_REBUILD
) ||
3295 (migr_type
== MIGR_GEN_MIGR
)) {
3299 for (i
= 0; i
< src
->num_members
; i
++) {
3300 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
3301 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
3305 if (migr_type
== MIGR_GEN_MIGR
)
3306 /* Clear migration record */
3307 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
3309 src
->map_state
= to_state
;
3312 static void end_migration(struct imsm_dev
*dev
, struct intel_super
*super
,
3315 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3316 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
== 0 ?
3320 /* merge any IMSM_ORD_REBUILD bits that were not successfully
3321 * completed in the last migration.
3323 * FIXME add support for raid-level-migration
3325 if ((map_state
!= map
->map_state
) && (is_gen_migration(dev
) == 0) &&
3326 (prev
->map_state
!= IMSM_T_STATE_UNINITIALIZED
)) {
3327 /* when final map state is other than expected
3328 * merge maps (not for migration)
3332 for (i
= 0; i
< prev
->num_members
; i
++)
3333 for (j
= 0; j
< map
->num_members
; j
++)
3334 /* during online capacity expansion
3335 * disks position can be changed
3336 * if takeover is used
3338 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
3339 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
3340 map
->disk_ord_tbl
[j
] |=
3341 prev
->disk_ord_tbl
[i
];
3344 failed
= imsm_count_failed(super
, dev
, MAP_0
);
3345 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
3348 dev
->vol
.migr_state
= 0;
3349 set_migr_type(dev
, 0);
3350 dev
->vol
.curr_migr_unit
= 0;
3351 map
->map_state
= map_state
;
3355 static int parse_raid_devices(struct intel_super
*super
)
3358 struct imsm_dev
*dev_new
;
3359 size_t len
, len_migr
;
3361 size_t space_needed
= 0;
3362 struct imsm_super
*mpb
= super
->anchor
;
3364 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3365 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3366 struct intel_dev
*dv
;
3368 len
= sizeof_imsm_dev(dev_iter
, 0);
3369 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
3371 space_needed
+= len_migr
- len
;
3373 dv
= malloc(sizeof(*dv
));
3376 if (max_len
< len_migr
)
3378 if (max_len
> len_migr
)
3379 space_needed
+= max_len
- len_migr
;
3380 dev_new
= malloc(max_len
);
3385 imsm_copy_dev(dev_new
, dev_iter
);
3388 dv
->next
= super
->devlist
;
3389 super
->devlist
= dv
;
3392 /* ensure that super->buf is large enough when all raid devices
3395 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
3398 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
, 512);
3399 if (posix_memalign(&buf
, 512, len
) != 0)
3402 memcpy(buf
, super
->buf
, super
->len
);
3403 memset(buf
+ super
->len
, 0, len
- super
->len
);
3412 /* retrieve a pointer to the bbm log which starts after all raid devices */
3413 struct bbm_log
*__get_imsm_bbm_log(struct imsm_super
*mpb
)
3417 if (__le32_to_cpu(mpb
->bbm_log_size
)) {
3419 ptr
+= mpb
->mpb_size
- __le32_to_cpu(mpb
->bbm_log_size
);
3425 /*******************************************************************************
3426 * Function: check_mpb_migr_compatibility
3427 * Description: Function checks for unsupported migration features:
3428 * - migration optimization area (pba_of_lba0)
3429 * - descending reshape (ascending_migr)
3431 * super : imsm metadata information
3433 * 0 : migration is compatible
3434 * -1 : migration is not compatible
3435 ******************************************************************************/
3436 int check_mpb_migr_compatibility(struct intel_super
*super
)
3438 struct imsm_map
*map0
, *map1
;
3439 struct migr_record
*migr_rec
= super
->migr_rec
;
3442 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3443 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3446 dev_iter
->vol
.migr_state
== 1 &&
3447 dev_iter
->vol
.migr_type
== MIGR_GEN_MIGR
) {
3448 /* This device is migrating */
3449 map0
= get_imsm_map(dev_iter
, MAP_0
);
3450 map1
= get_imsm_map(dev_iter
, MAP_1
);
3451 if (pba_of_lba0(map0
) != pba_of_lba0(map1
))
3452 /* migration optimization area was used */
3454 if (migr_rec
->ascending_migr
== 0
3455 && migr_rec
->dest_depth_per_unit
> 0)
3456 /* descending reshape not supported yet */
3463 static void __free_imsm(struct intel_super
*super
, int free_disks
);
3465 /* load_imsm_mpb - read matrix metadata
3466 * allocates super->mpb to be freed by free_imsm
3468 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
3470 unsigned long long dsize
;
3471 unsigned long long sectors
;
3473 struct imsm_super
*anchor
;
3476 get_dev_size(fd
, NULL
, &dsize
);
3480 Name
": %s: device to small for imsm\n",
3485 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0) {
3487 fprintf(stderr
, Name
3488 ": Cannot seek to anchor block on %s: %s\n",
3489 devname
, strerror(errno
));
3493 if (posix_memalign((void**)&anchor
, 512, 512) != 0) {
3496 Name
": Failed to allocate imsm anchor buffer"
3497 " on %s\n", devname
);
3500 if (read(fd
, anchor
, 512) != 512) {
3503 Name
": Cannot read anchor block on %s: %s\n",
3504 devname
, strerror(errno
));
3509 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
3512 Name
": no IMSM anchor on %s\n", devname
);
3517 __free_imsm(super
, 0);
3518 /* reload capability and hba */
3520 /* capability and hba must be updated with new super allocation */
3521 find_intel_hba_capability(fd
, super
, devname
);
3522 super
->len
= ROUND_UP(anchor
->mpb_size
, 512);
3523 if (posix_memalign(&super
->buf
, 512, super
->len
) != 0) {
3526 Name
": unable to allocate %zu byte mpb buffer\n",
3531 memcpy(super
->buf
, anchor
, 512);
3533 sectors
= mpb_sectors(anchor
) - 1;
3536 if (posix_memalign(&super
->migr_rec_buf
, 512, MIGR_REC_BUF_SIZE
) != 0) {
3537 fprintf(stderr
, Name
3538 ": %s could not allocate migr_rec buffer\n", __func__
);
3542 super
->clean_migration_record_by_mdmon
= 0;
3545 check_sum
= __gen_imsm_checksum(super
->anchor
);
3546 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
3549 Name
": IMSM checksum %x != %x on %s\n",
3551 __le32_to_cpu(super
->anchor
->check_sum
),
3559 /* read the extended mpb */
3560 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0) {
3563 Name
": Cannot seek to extended mpb on %s: %s\n",
3564 devname
, strerror(errno
));
3568 if ((unsigned)read(fd
, super
->buf
+ 512, super
->len
- 512) != super
->len
- 512) {
3571 Name
": Cannot read extended mpb on %s: %s\n",
3572 devname
, strerror(errno
));
3576 check_sum
= __gen_imsm_checksum(super
->anchor
);
3577 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
3580 Name
": IMSM checksum %x != %x on %s\n",
3581 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
3586 /* FIXME the BBM log is disk specific so we cannot use this global
3587 * buffer for all disks. Ok for now since we only look at the global
3588 * bbm_log_size parameter to gate assembly
3590 super
->bbm_log
= __get_imsm_bbm_log(super
->anchor
);
3595 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
);
3597 /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
3598 static void clear_hi(struct intel_super
*super
)
3600 struct imsm_super
*mpb
= super
->anchor
;
3602 if (mpb
->attributes
& MPB_ATTRIB_2TB_DISK
)
3604 for (i
= 0; i
< mpb
->num_disks
; ++i
) {
3605 struct imsm_disk
*disk
= &mpb
->disk
[i
];
3606 disk
->total_blocks_hi
= 0;
3608 for (i
= 0; i
< mpb
->num_raid_devs
; ++i
) {
3609 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3612 for (n
= 0; n
< 2; ++n
) {
3613 struct imsm_map
*map
= get_imsm_map(dev
, n
);
3616 map
->pba_of_lba0_hi
= 0;
3617 map
->blocks_per_member_hi
= 0;
3618 map
->num_data_stripes_hi
= 0;
3624 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3628 err
= load_imsm_mpb(fd
, super
, devname
);
3631 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
3634 err
= parse_raid_devices(super
);
3639 static void __free_imsm_disk(struct dl
*d
)
3651 static void free_imsm_disks(struct intel_super
*super
)
3655 while (super
->disks
) {
3657 super
->disks
= d
->next
;
3658 __free_imsm_disk(d
);
3660 while (super
->disk_mgmt_list
) {
3661 d
= super
->disk_mgmt_list
;
3662 super
->disk_mgmt_list
= d
->next
;
3663 __free_imsm_disk(d
);
3665 while (super
->missing
) {
3667 super
->missing
= d
->next
;
3668 __free_imsm_disk(d
);
3673 /* free all the pieces hanging off of a super pointer */
3674 static void __free_imsm(struct intel_super
*super
, int free_disks
)
3676 struct intel_hba
*elem
, *next
;
3682 /* unlink capability description */
3684 if (super
->migr_rec_buf
) {
3685 free(super
->migr_rec_buf
);
3686 super
->migr_rec_buf
= NULL
;
3689 free_imsm_disks(super
);
3690 free_devlist(super
);
3694 free((void *)elem
->path
);
3702 static void free_imsm(struct intel_super
*super
)
3704 __free_imsm(super
, 1);
3708 static void free_super_imsm(struct supertype
*st
)
3710 struct intel_super
*super
= st
->sb
;
3719 static struct intel_super
*alloc_super(void)
3721 struct intel_super
*super
= malloc(sizeof(*super
));
3724 memset(super
, 0, sizeof(*super
));
3725 super
->current_vol
= -1;
3726 super
->create_offset
= ~((unsigned long long) 0);
3732 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
3734 static int find_intel_hba_capability(int fd
, struct intel_super
*super
, char *devname
)
3736 struct sys_dev
*hba_name
;
3739 if ((fd
< 0) || check_env("IMSM_NO_PLATFORM")) {
3744 hba_name
= find_disk_attached_hba(fd
, NULL
);
3748 Name
": %s is not attached to Intel(R) RAID controller.\n",
3752 rv
= attach_hba_to_super(super
, hba_name
);
3755 struct intel_hba
*hba
= super
->hba
;
3757 fprintf(stderr
, Name
": %s is attached to Intel(R) %s RAID "
3758 "controller (%s),\n"
3759 " but the container is assigned to Intel(R) "
3760 "%s RAID controller (",
3763 hba_name
->pci_id
? : "Err!",
3764 get_sys_dev_type(hba_name
->type
));
3767 fprintf(stderr
, "%s", hba
->pci_id
? : "Err!");
3769 fprintf(stderr
, ", ");
3773 fprintf(stderr
, ").\n"
3774 " Mixing devices attached to different controllers "
3775 "is not allowed.\n");
3777 free_sys_dev(&hba_name
);
3780 super
->orom
= find_imsm_capability(hba_name
->type
);
3781 free_sys_dev(&hba_name
);
3787 /* find_missing - helper routine for load_super_imsm_all that identifies
3788 * disks that have disappeared from the system. This routine relies on
3789 * the mpb being uptodate, which it is at load time.
3791 static int find_missing(struct intel_super
*super
)
3794 struct imsm_super
*mpb
= super
->anchor
;
3796 struct imsm_disk
*disk
;
3798 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3799 disk
= __get_imsm_disk(mpb
, i
);
3800 dl
= serial_to_dl(disk
->serial
, super
);
3804 dl
= malloc(sizeof(*dl
));
3810 dl
->devname
= strdup("missing");
3812 serialcpy(dl
->serial
, disk
->serial
);
3815 dl
->next
= super
->missing
;
3816 super
->missing
= dl
;
3823 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
3825 struct intel_disk
*idisk
= disk_list
;
3828 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
3830 idisk
= idisk
->next
;
3836 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
3837 struct intel_super
*super
,
3838 struct intel_disk
**disk_list
)
3840 struct imsm_disk
*d
= &super
->disks
->disk
;
3841 struct imsm_super
*mpb
= super
->anchor
;
3844 for (i
= 0; i
< tbl_size
; i
++) {
3845 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
3846 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
3848 if (tbl_mpb
->family_num
== mpb
->family_num
) {
3849 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
3850 dprintf("%s: mpb from %d:%d matches %d:%d\n",
3851 __func__
, super
->disks
->major
,
3852 super
->disks
->minor
,
3853 table
[i
]->disks
->major
,
3854 table
[i
]->disks
->minor
);
3858 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
3859 is_configured(d
) == is_configured(tbl_d
)) &&
3860 tbl_mpb
->generation_num
< mpb
->generation_num
) {
3861 /* current version of the mpb is a
3862 * better candidate than the one in
3863 * super_table, but copy over "cross
3864 * generational" status
3866 struct intel_disk
*idisk
;
3868 dprintf("%s: mpb from %d:%d replaces %d:%d\n",
3869 __func__
, super
->disks
->major
,
3870 super
->disks
->minor
,
3871 table
[i
]->disks
->major
,
3872 table
[i
]->disks
->minor
);
3874 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
3875 if (idisk
&& is_failed(&idisk
->disk
))
3876 tbl_d
->status
|= FAILED_DISK
;
3879 struct intel_disk
*idisk
;
3880 struct imsm_disk
*disk
;
3882 /* tbl_mpb is more up to date, but copy
3883 * over cross generational status before
3886 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
3887 if (disk
&& is_failed(disk
))
3888 d
->status
|= FAILED_DISK
;
3890 idisk
= disk_list_get(d
->serial
, *disk_list
);
3893 if (disk
&& is_configured(disk
))
3894 idisk
->disk
.status
|= CONFIGURED_DISK
;
3897 dprintf("%s: mpb from %d:%d prefer %d:%d\n",
3898 __func__
, super
->disks
->major
,
3899 super
->disks
->minor
,
3900 table
[i
]->disks
->major
,
3901 table
[i
]->disks
->minor
);
3909 table
[tbl_size
++] = super
;
3913 /* update/extend the merged list of imsm_disk records */
3914 for (j
= 0; j
< mpb
->num_disks
; j
++) {
3915 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
3916 struct intel_disk
*idisk
;
3918 idisk
= disk_list_get(disk
->serial
, *disk_list
);
3920 idisk
->disk
.status
|= disk
->status
;
3921 if (is_configured(&idisk
->disk
) ||
3922 is_failed(&idisk
->disk
))
3923 idisk
->disk
.status
&= ~(SPARE_DISK
);
3925 idisk
= calloc(1, sizeof(*idisk
));
3928 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
3929 idisk
->disk
= *disk
;
3930 idisk
->next
= *disk_list
;
3934 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
3941 static struct intel_super
*
3942 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
3945 struct imsm_super
*mpb
= super
->anchor
;
3949 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3950 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3951 struct intel_disk
*idisk
;
3953 idisk
= disk_list_get(disk
->serial
, disk_list
);
3955 if (idisk
->owner
== owner
||
3956 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
3959 dprintf("%s: '%.16s' owner %d != %d\n",
3960 __func__
, disk
->serial
, idisk
->owner
,
3963 dprintf("%s: unknown disk %x [%d]: %.16s\n",
3964 __func__
, __le32_to_cpu(mpb
->family_num
), i
,
3970 if (ok_count
== mpb
->num_disks
)
3975 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
3977 struct intel_super
*s
;
3979 for (s
= super_list
; s
; s
= s
->next
) {
3980 if (family_num
!= s
->anchor
->family_num
)
3982 fprintf(stderr
, "Conflict, offlining family %#x on '%s'\n",
3983 __le32_to_cpu(family_num
), s
->disks
->devname
);
3987 static struct intel_super
*
3988 imsm_thunderdome(struct intel_super
**super_list
, int len
)
3990 struct intel_super
*super_table
[len
];
3991 struct intel_disk
*disk_list
= NULL
;
3992 struct intel_super
*champion
, *spare
;
3993 struct intel_super
*s
, **del
;
3998 memset(super_table
, 0, sizeof(super_table
));
3999 for (s
= *super_list
; s
; s
= s
->next
)
4000 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
4002 for (i
= 0; i
< tbl_size
; i
++) {
4003 struct imsm_disk
*d
;
4004 struct intel_disk
*idisk
;
4005 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
4008 d
= &s
->disks
->disk
;
4010 /* 'd' must appear in merged disk list for its
4011 * configuration to be valid
4013 idisk
= disk_list_get(d
->serial
, disk_list
);
4014 if (idisk
&& idisk
->owner
== i
)
4015 s
= validate_members(s
, disk_list
, i
);
4020 dprintf("%s: marking family: %#x from %d:%d offline\n",
4021 __func__
, mpb
->family_num
,
4022 super_table
[i
]->disks
->major
,
4023 super_table
[i
]->disks
->minor
);
4027 /* This is where the mdadm implementation differs from the Windows
4028 * driver which has no strict concept of a container. We can only
4029 * assemble one family from a container, so when returning a prodigal
4030 * array member to this system the code will not be able to disambiguate
4031 * the container contents that should be assembled ("foreign" versus
4032 * "local"). It requires user intervention to set the orig_family_num
4033 * to a new value to establish a new container. The Windows driver in
4034 * this situation fixes up the volume name in place and manages the
4035 * foreign array as an independent entity.
4040 for (i
= 0; i
< tbl_size
; i
++) {
4041 struct intel_super
*tbl_ent
= super_table
[i
];
4047 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
4052 if (s
&& !is_spare
) {
4053 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
4055 } else if (!s
&& !is_spare
)
4068 fprintf(stderr
, "Chose family %#x on '%s', "
4069 "assemble conflicts to new container with '--update=uuid'\n",
4070 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
4072 /* collect all dl's onto 'champion', and update them to
4073 * champion's version of the status
4075 for (s
= *super_list
; s
; s
= s
->next
) {
4076 struct imsm_super
*mpb
= champion
->anchor
;
4077 struct dl
*dl
= s
->disks
;
4082 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4083 struct imsm_disk
*disk
;
4085 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
4088 /* only set index on disks that are a member of
4089 * a populated contianer, i.e. one with
4092 if (is_failed(&dl
->disk
))
4094 else if (is_spare(&dl
->disk
))
4100 if (i
>= mpb
->num_disks
) {
4101 struct intel_disk
*idisk
;
4103 idisk
= disk_list_get(dl
->serial
, disk_list
);
4104 if (idisk
&& is_spare(&idisk
->disk
) &&
4105 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
4113 dl
->next
= champion
->disks
;
4114 champion
->disks
= dl
;
4118 /* delete 'champion' from super_list */
4119 for (del
= super_list
; *del
; ) {
4120 if (*del
== champion
) {
4121 *del
= (*del
)->next
;
4124 del
= &(*del
)->next
;
4126 champion
->next
= NULL
;
4130 struct intel_disk
*idisk
= disk_list
;
4132 disk_list
= disk_list
->next
;
4141 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
);
4142 static int get_super_block(struct intel_super
**super_list
, int devnum
, char *devname
,
4143 int major
, int minor
, int keep_fd
);
4145 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4146 int *max
, int keep_fd
);
4149 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
4150 char *devname
, struct md_list
*devlist
,
4153 struct intel_super
*super_list
= NULL
;
4154 struct intel_super
*super
= NULL
;
4159 /* 'fd' is an opened container */
4160 err
= get_sra_super_block(fd
, &super_list
, devname
, &i
, keep_fd
);
4162 /* get super block from devlist devices */
4163 err
= get_devlist_super_block(devlist
, &super_list
, &i
, keep_fd
);
4166 /* all mpbs enter, maybe one leaves */
4167 super
= imsm_thunderdome(&super_list
, i
);
4173 if (find_missing(super
) != 0) {
4179 /* load migration record */
4180 err
= load_imsm_migr_rec(super
, NULL
);
4182 /* migration is in progress,
4183 * but migr_rec cannot be loaded,
4189 /* Check migration compatibility */
4190 if ((err
== 0) && (check_mpb_migr_compatibility(super
) != 0)) {
4191 fprintf(stderr
, Name
": Unsupported migration detected");
4193 fprintf(stderr
, " on %s\n", devname
);
4195 fprintf(stderr
, " (IMSM).\n");
4204 while (super_list
) {
4205 struct intel_super
*s
= super_list
;
4207 super_list
= super_list
->next
;
4217 st
->container_dev
= fd2devnum(fd
);
4219 st
->container_dev
= NoMdDev
;
4220 if (err
== 0 && st
->ss
== NULL
) {
4221 st
->ss
= &super_imsm
;
4222 st
->minor_version
= 0;
4223 st
->max_devs
= IMSM_MAX_DEVICES
;
4230 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4231 int *max
, int keep_fd
)
4233 struct md_list
*tmpdev
;
4237 for (i
= 0, tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
4238 if (tmpdev
->used
!= 1)
4240 if (tmpdev
->container
== 1) {
4242 int fd
= dev_open(tmpdev
->devname
, O_RDONLY
|O_EXCL
);
4244 fprintf(stderr
, Name
": cannot open device %s: %s\n",
4245 tmpdev
->devname
, strerror(errno
));
4249 err
= get_sra_super_block(fd
, super_list
,
4250 tmpdev
->devname
, &lmax
,
4259 int major
= major(tmpdev
->st_rdev
);
4260 int minor
= minor(tmpdev
->st_rdev
);
4261 err
= get_super_block(super_list
,
4278 static int get_super_block(struct intel_super
**super_list
, int devnum
, char *devname
,
4279 int major
, int minor
, int keep_fd
)
4281 struct intel_super
*s
= NULL
;
4294 sprintf(nm
, "%d:%d", major
, minor
);
4295 dfd
= dev_open(nm
, O_RDWR
);
4301 rv
= find_intel_hba_capability(dfd
, s
, devname
);
4302 /* no orom/efi or non-intel hba of the disk */
4308 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4310 /* retry the load if we might have raced against mdmon */
4311 if (err
== 3 && (devnum
!= -1) && mdmon_running(devnum
))
4312 for (retry
= 0; retry
< 3; retry
++) {
4314 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4320 s
->next
= *super_list
;
4328 if ((dfd
>= 0) && (!keep_fd
))
4335 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
)
4342 sra
= sysfs_read(fd
, 0, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
4346 if (sra
->array
.major_version
!= -1 ||
4347 sra
->array
.minor_version
!= -2 ||
4348 strcmp(sra
->text_version
, "imsm") != 0) {
4353 devnum
= fd2devnum(fd
);
4354 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
4355 if (get_super_block(super_list
, devnum
, devname
,
4356 sd
->disk
.major
, sd
->disk
.minor
, keep_fd
) != 0) {
4367 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
4369 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
, NULL
, 1);
4373 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
4375 struct intel_super
*super
;
4378 if (test_partition(fd
))
4379 /* IMSM not allowed on partitions */
4382 free_super_imsm(st
);
4384 super
= alloc_super();
4387 Name
": malloc of %zu failed.\n",
4391 /* Load hba and capabilities if they exist.
4392 * But do not preclude loading metadata in case capabilities or hba are
4393 * non-compliant and ignore_hw_compat is set.
4395 rv
= find_intel_hba_capability(fd
, super
, devname
);
4396 /* no orom/efi or non-intel hba of the disk */
4397 if ((rv
!= 0) && (st
->ignore_hw_compat
== 0)) {
4400 Name
": No OROM/EFI properties for %s\n", devname
);
4404 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
4409 Name
": Failed to load all information "
4410 "sections on %s\n", devname
);
4416 if (st
->ss
== NULL
) {
4417 st
->ss
= &super_imsm
;
4418 st
->minor_version
= 0;
4419 st
->max_devs
= IMSM_MAX_DEVICES
;
4422 /* load migration record */
4423 if (load_imsm_migr_rec(super
, NULL
) == 0) {
4424 /* Check for unsupported migration features */
4425 if (check_mpb_migr_compatibility(super
) != 0) {
4427 Name
": Unsupported migration detected");
4429 fprintf(stderr
, " on %s\n", devname
);
4431 fprintf(stderr
, " (IMSM).\n");
4439 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
4441 if (info
->level
== 1)
4443 return info
->chunk_size
>> 9;
4446 static unsigned long long info_to_blocks_per_member(mdu_array_info_t
*info
,
4447 unsigned long long size
)
4449 if (info
->level
== 1)
4452 return (size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
4455 static void imsm_update_version_info(struct intel_super
*super
)
4457 /* update the version and attributes */
4458 struct imsm_super
*mpb
= super
->anchor
;
4460 struct imsm_dev
*dev
;
4461 struct imsm_map
*map
;
4464 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4465 dev
= get_imsm_dev(super
, i
);
4466 map
= get_imsm_map(dev
, MAP_0
);
4467 if (__le32_to_cpu(dev
->size_high
) > 0)
4468 mpb
->attributes
|= MPB_ATTRIB_2TB
;
4470 /* FIXME detect when an array spans a port multiplier */
4472 mpb
->attributes
|= MPB_ATTRIB_PM
;
4475 if (mpb
->num_raid_devs
> 1 ||
4476 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
4477 version
= MPB_VERSION_ATTRIBS
;
4478 switch (get_imsm_raid_level(map
)) {
4479 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
4480 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
4481 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
4482 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
4485 if (map
->num_members
>= 5)
4486 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
4487 else if (dev
->status
== DEV_CLONE_N_GO
)
4488 version
= MPB_VERSION_CNG
;
4489 else if (get_imsm_raid_level(map
) == 5)
4490 version
= MPB_VERSION_RAID5
;
4491 else if (map
->num_members
>= 3)
4492 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
4493 else if (get_imsm_raid_level(map
) == 1)
4494 version
= MPB_VERSION_RAID1
;
4496 version
= MPB_VERSION_RAID0
;
4498 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
4502 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
4504 struct imsm_super
*mpb
= super
->anchor
;
4505 char *reason
= NULL
;
4508 if (strlen(name
) > MAX_RAID_SERIAL_LEN
)
4509 reason
= "must be 16 characters or less";
4511 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4512 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
4514 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
4515 reason
= "already exists";
4520 if (reason
&& !quiet
)
4521 fprintf(stderr
, Name
": imsm volume name %s\n", reason
);
4526 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
4527 unsigned long long size
, char *name
,
4528 char *homehost
, int *uuid
)
4530 /* We are creating a volume inside a pre-existing container.
4531 * so st->sb is already set.
4533 struct intel_super
*super
= st
->sb
;
4534 struct imsm_super
*mpb
= super
->anchor
;
4535 struct intel_dev
*dv
;
4536 struct imsm_dev
*dev
;
4537 struct imsm_vol
*vol
;
4538 struct imsm_map
*map
;
4539 int idx
= mpb
->num_raid_devs
;
4541 unsigned long long array_blocks
;
4542 size_t size_old
, size_new
;
4543 unsigned long long num_data_stripes
;
4545 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
4546 fprintf(stderr
, Name
": This imsm-container already has the "
4547 "maximum of %d volumes\n", super
->orom
->vpa
);
4551 /* ensure the mpb is large enough for the new data */
4552 size_old
= __le32_to_cpu(mpb
->mpb_size
);
4553 size_new
= disks_to_mpb_size(info
->nr_disks
);
4554 if (size_new
> size_old
) {
4556 size_t size_round
= ROUND_UP(size_new
, 512);
4558 if (posix_memalign(&mpb_new
, 512, size_round
) != 0) {
4559 fprintf(stderr
, Name
": could not allocate new mpb\n");
4562 if (posix_memalign(&super
->migr_rec_buf
, 512,
4563 MIGR_REC_BUF_SIZE
) != 0) {
4564 fprintf(stderr
, Name
4565 ": %s could not allocate migr_rec buffer\n",
4572 memcpy(mpb_new
, mpb
, size_old
);
4575 super
->anchor
= mpb_new
;
4576 mpb
->mpb_size
= __cpu_to_le32(size_new
);
4577 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
4579 super
->current_vol
= idx
;
4581 /* handle 'failed_disks' by either:
4582 * a) create dummy disk entries in the table if this the first
4583 * volume in the array. We add them here as this is the only
4584 * opportunity to add them. add_to_super_imsm_volume()
4585 * handles the non-failed disks and continues incrementing
4587 * b) validate that 'failed_disks' matches the current number
4588 * of missing disks if the container is populated
4590 if (super
->current_vol
== 0) {
4592 for (i
= 0; i
< info
->failed_disks
; i
++) {
4593 struct imsm_disk
*disk
;
4596 disk
= __get_imsm_disk(mpb
, i
);
4597 disk
->status
= CONFIGURED_DISK
| FAILED_DISK
;
4598 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
4599 snprintf((char *) disk
->serial
, MAX_RAID_SERIAL_LEN
,
4602 find_missing(super
);
4607 for (d
= super
->missing
; d
; d
= d
->next
)
4609 if (info
->failed_disks
> missing
) {
4610 fprintf(stderr
, Name
": unable to add 'missing' disk to container\n");
4615 if (!check_name(super
, name
, 0))
4617 dv
= malloc(sizeof(*dv
));
4619 fprintf(stderr
, Name
": failed to allocate device list entry\n");
4622 dev
= calloc(1, sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
4625 fprintf(stderr
, Name
": could not allocate raid device\n");
4629 strncpy((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
4630 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
4631 info
->layout
, info
->chunk_size
,
4633 /* round array size down to closest MB */
4634 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
4636 dev
->size_low
= __cpu_to_le32((__u32
) array_blocks
);
4637 dev
->size_high
= __cpu_to_le32((__u32
) (array_blocks
>> 32));
4638 dev
->status
= (DEV_READ_COALESCING
| DEV_WRITE_COALESCING
);
4640 vol
->migr_state
= 0;
4641 set_migr_type(dev
, MIGR_INIT
);
4642 vol
->dirty
= !info
->state
;
4643 vol
->curr_migr_unit
= 0;
4644 map
= get_imsm_map(dev
, MAP_0
);
4645 set_pba_of_lba0(map
, super
->create_offset
);
4646 set_blocks_per_member(map
, info_to_blocks_per_member(info
, size
));
4647 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
4648 map
->failed_disk_num
= ~0;
4649 if (info
->level
> 0)
4650 map
->map_state
= IMSM_T_STATE_UNINITIALIZED
;
4652 map
->map_state
= info
->failed_disks
? IMSM_T_STATE_FAILED
:
4653 IMSM_T_STATE_NORMAL
;
4656 if (info
->level
== 1 && info
->raid_disks
> 2) {
4659 fprintf(stderr
, Name
": imsm does not support more than 2 disks"
4660 "in a raid1 volume\n");
4664 map
->raid_level
= info
->level
;
4665 if (info
->level
== 10) {
4666 map
->raid_level
= 1;
4667 map
->num_domains
= info
->raid_disks
/ 2;
4668 } else if (info
->level
== 1)
4669 map
->num_domains
= info
->raid_disks
;
4671 map
->num_domains
= 1;
4673 /* info->size is only int so use the 'size' parameter instead */
4674 num_data_stripes
= (size
* 2) / info_to_blocks_per_strip(info
);
4675 num_data_stripes
/= map
->num_domains
;
4676 set_num_data_stripes(map
, num_data_stripes
);
4678 map
->num_members
= info
->raid_disks
;
4679 for (i
= 0; i
< map
->num_members
; i
++) {
4680 /* initialized in add_to_super */
4681 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
4683 mpb
->num_raid_devs
++;
4686 dv
->index
= super
->current_vol
;
4687 dv
->next
= super
->devlist
;
4688 super
->devlist
= dv
;
4690 imsm_update_version_info(super
);
4695 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
4696 unsigned long long size
, char *name
,
4697 char *homehost
, int *uuid
)
4699 /* This is primarily called by Create when creating a new array.
4700 * We will then get add_to_super called for each component, and then
4701 * write_init_super called to write it out to each device.
4702 * For IMSM, Create can create on fresh devices or on a pre-existing
4704 * To create on a pre-existing array a different method will be called.
4705 * This one is just for fresh drives.
4707 struct intel_super
*super
;
4708 struct imsm_super
*mpb
;
4713 return init_super_imsm_volume(st
, info
, size
, name
, homehost
, uuid
);
4716 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
4720 super
= alloc_super();
4721 if (super
&& posix_memalign(&super
->buf
, 512, mpb_size
) != 0) {
4726 fprintf(stderr
, Name
4727 ": %s could not allocate superblock\n", __func__
);
4730 if (posix_memalign(&super
->migr_rec_buf
, 512, MIGR_REC_BUF_SIZE
) != 0) {
4731 fprintf(stderr
, Name
4732 ": %s could not allocate migr_rec buffer\n", __func__
);
4737 memset(super
->buf
, 0, mpb_size
);
4739 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
4743 /* zeroing superblock */
4747 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
4749 version
= (char *) mpb
->sig
;
4750 strcpy(version
, MPB_SIGNATURE
);
4751 version
+= strlen(MPB_SIGNATURE
);
4752 strcpy(version
, MPB_VERSION_RAID0
);
4758 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
4759 int fd
, char *devname
)
4761 struct intel_super
*super
= st
->sb
;
4762 struct imsm_super
*mpb
= super
->anchor
;
4763 struct imsm_disk
*_disk
;
4764 struct imsm_dev
*dev
;
4765 struct imsm_map
*map
;
4769 dev
= get_imsm_dev(super
, super
->current_vol
);
4770 map
= get_imsm_map(dev
, MAP_0
);
4772 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
4773 fprintf(stderr
, Name
": %s: Cannot add spare devices to IMSM volume\n",
4779 /* we're doing autolayout so grab the pre-marked (in
4780 * validate_geometry) raid_disk
4782 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4783 if (dl
->raiddisk
== dk
->raid_disk
)
4786 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4787 if (dl
->major
== dk
->major
&&
4788 dl
->minor
== dk
->minor
)
4793 fprintf(stderr
, Name
": %s is not a member of the same container\n", devname
);
4797 /* add a pristine spare to the metadata */
4798 if (dl
->index
< 0) {
4799 dl
->index
= super
->anchor
->num_disks
;
4800 super
->anchor
->num_disks
++;
4802 /* Check the device has not already been added */
4803 slot
= get_imsm_disk_slot(map
, dl
->index
);
4805 (get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
) & IMSM_ORD_REBUILD
) == 0) {
4806 fprintf(stderr
, Name
": %s has been included in this array twice\n",
4810 set_imsm_ord_tbl_ent(map
, dk
->raid_disk
, dl
->index
);
4811 dl
->disk
.status
= CONFIGURED_DISK
;
4813 /* update size of 'missing' disks to be at least as large as the
4814 * largest acitve member (we only have dummy missing disks when
4815 * creating the first volume)
4817 if (super
->current_vol
== 0) {
4818 for (df
= super
->missing
; df
; df
= df
->next
) {
4819 if (total_blocks(&dl
->disk
) > total_blocks(&df
->disk
))
4820 set_total_blocks(&df
->disk
, total_blocks(&dl
->disk
));
4821 _disk
= __get_imsm_disk(mpb
, df
->index
);
4826 /* refresh unset/failed slots to point to valid 'missing' entries */
4827 for (df
= super
->missing
; df
; df
= df
->next
)
4828 for (slot
= 0; slot
< mpb
->num_disks
; slot
++) {
4829 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
4831 if ((ord
& IMSM_ORD_REBUILD
) == 0)
4833 set_imsm_ord_tbl_ent(map
, slot
, df
->index
| IMSM_ORD_REBUILD
);
4834 if (is_gen_migration(dev
)) {
4835 struct imsm_map
*map2
= get_imsm_map(dev
,
4837 int slot2
= get_imsm_disk_slot(map2
, df
->index
);
4838 if ((slot2
< map2
->num_members
) &&
4840 __u32 ord2
= get_imsm_ord_tbl_ent(dev
,
4843 if ((unsigned)df
->index
==
4845 set_imsm_ord_tbl_ent(map2
,
4851 dprintf("set slot:%d to missing disk:%d\n", slot
, df
->index
);
4855 /* if we are creating the first raid device update the family number */
4856 if (super
->current_vol
== 0) {
4858 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
4860 _disk
= __get_imsm_disk(mpb
, dl
->index
);
4861 if (!_dev
|| !_disk
) {
4862 fprintf(stderr
, Name
": BUG mpb setup error\n");
4868 sum
+= __gen_imsm_checksum(mpb
);
4869 mpb
->family_num
= __cpu_to_le32(sum
);
4870 mpb
->orig_family_num
= mpb
->family_num
;
4872 super
->current_disk
= dl
;
4877 * Function marks disk as spare and restores disk serial
4878 * in case it was previously marked as failed by takeover operation
4880 * -1 : critical error
4881 * 0 : disk is marked as spare but serial is not set
4884 int mark_spare(struct dl
*disk
)
4886 __u8 serial
[MAX_RAID_SERIAL_LEN
];
4893 if (!imsm_read_serial(disk
->fd
, NULL
, serial
)) {
4894 /* Restore disk serial number, because takeover marks disk
4895 * as failed and adds to serial ':0' before it becomes
4898 serialcpy(disk
->serial
, serial
);
4899 serialcpy(disk
->disk
.serial
, serial
);
4902 disk
->disk
.status
= SPARE_DISK
;
4908 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
4909 int fd
, char *devname
)
4911 struct intel_super
*super
= st
->sb
;
4913 unsigned long long size
;
4918 /* If we are on an RAID enabled platform check that the disk is
4919 * attached to the raid controller.
4920 * We do not need to test disks attachment for container based additions,
4921 * they shall be already tested when container was created/assembled.
4923 rv
= find_intel_hba_capability(fd
, super
, devname
);
4924 /* no orom/efi or non-intel hba of the disk */
4926 dprintf("capability: %p fd: %d ret: %d\n",
4927 super
->orom
, fd
, rv
);
4931 if (super
->current_vol
>= 0)
4932 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
4935 dd
= malloc(sizeof(*dd
));
4938 Name
": malloc failed %s:%d.\n", __func__
, __LINE__
);
4941 memset(dd
, 0, sizeof(*dd
));
4942 dd
->major
= major(stb
.st_rdev
);
4943 dd
->minor
= minor(stb
.st_rdev
);
4944 dd
->devname
= devname
? strdup(devname
) : NULL
;
4947 dd
->action
= DISK_ADD
;
4948 rv
= imsm_read_serial(fd
, devname
, dd
->serial
);
4951 Name
": failed to retrieve scsi serial, aborting\n");
4956 get_dev_size(fd
, NULL
, &size
);
4958 serialcpy(dd
->disk
.serial
, dd
->serial
);
4959 set_total_blocks(&dd
->disk
, size
);
4960 if (__le32_to_cpu(dd
->disk
.total_blocks_hi
) > 0) {
4961 struct imsm_super
*mpb
= super
->anchor
;
4962 mpb
->attributes
|= MPB_ATTRIB_2TB_DISK
;
4965 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
4966 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
4968 dd
->disk
.scsi_id
= __cpu_to_le32(0);
4970 if (st
->update_tail
) {
4971 dd
->next
= super
->disk_mgmt_list
;
4972 super
->disk_mgmt_list
= dd
;
4974 dd
->next
= super
->disks
;
4976 super
->updates_pending
++;
4983 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
4985 struct intel_super
*super
= st
->sb
;
4988 /* remove from super works only in mdmon - for communication
4989 * manager - monitor. Check if communication memory buffer
4992 if (!st
->update_tail
) {
4994 Name
": %s shall be used in mdmon context only"
4995 "(line %d).\n", __func__
, __LINE__
);
4998 dd
= malloc(sizeof(*dd
));
5001 Name
": malloc failed %s:%d.\n", __func__
, __LINE__
);
5004 memset(dd
, 0, sizeof(*dd
));
5005 dd
->major
= dk
->major
;
5006 dd
->minor
= dk
->minor
;
5009 dd
->action
= DISK_REMOVE
;
5011 dd
->next
= super
->disk_mgmt_list
;
5012 super
->disk_mgmt_list
= dd
;
5018 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
5022 struct imsm_super anchor
;
5023 } spare_record
__attribute__ ((aligned(512)));
5025 /* spare records have their own family number and do not have any defined raid
5028 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
5030 struct imsm_super
*mpb
= super
->anchor
;
5031 struct imsm_super
*spare
= &spare_record
.anchor
;
5035 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
)),
5036 spare
->generation_num
= __cpu_to_le32(1UL),
5037 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
5038 spare
->num_disks
= 1,
5039 spare
->num_raid_devs
= 0,
5040 spare
->cache_size
= mpb
->cache_size
,
5041 spare
->pwr_cycle_count
= __cpu_to_le32(1),
5043 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
5044 MPB_SIGNATURE MPB_VERSION_RAID0
);
5046 for (d
= super
->disks
; d
; d
= d
->next
) {
5050 spare
->disk
[0] = d
->disk
;
5051 sum
= __gen_imsm_checksum(spare
);
5052 spare
->family_num
= __cpu_to_le32(sum
);
5053 spare
->orig_family_num
= 0;
5054 sum
= __gen_imsm_checksum(spare
);
5055 spare
->check_sum
= __cpu_to_le32(sum
);
5057 if (store_imsm_mpb(d
->fd
, spare
)) {
5058 fprintf(stderr
, "%s: failed for device %d:%d %s\n",
5059 __func__
, d
->major
, d
->minor
, strerror(errno
));
5071 static int write_super_imsm(struct supertype
*st
, int doclose
)
5073 struct intel_super
*super
= st
->sb
;
5074 struct imsm_super
*mpb
= super
->anchor
;
5080 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
5082 int clear_migration_record
= 1;
5084 /* 'generation' is incremented everytime the metadata is written */
5085 generation
= __le32_to_cpu(mpb
->generation_num
);
5087 mpb
->generation_num
= __cpu_to_le32(generation
);
5089 /* fix up cases where previous mdadm releases failed to set
5092 if (mpb
->orig_family_num
== 0)
5093 mpb
->orig_family_num
= mpb
->family_num
;
5095 for (d
= super
->disks
; d
; d
= d
->next
) {
5099 mpb
->disk
[d
->index
] = d
->disk
;
5103 for (d
= super
->missing
; d
; d
= d
->next
) {
5104 mpb
->disk
[d
->index
] = d
->disk
;
5107 mpb
->num_disks
= num_disks
;
5108 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
5110 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5111 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
5112 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
5114 imsm_copy_dev(dev
, dev2
);
5115 mpb_size
+= sizeof_imsm_dev(dev
, 0);
5117 if (is_gen_migration(dev2
))
5118 clear_migration_record
= 0;
5120 mpb_size
+= __le32_to_cpu(mpb
->bbm_log_size
);
5121 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5123 /* recalculate checksum */
5124 sum
= __gen_imsm_checksum(mpb
);
5125 mpb
->check_sum
= __cpu_to_le32(sum
);
5127 if (super
->clean_migration_record_by_mdmon
) {
5128 clear_migration_record
= 1;
5129 super
->clean_migration_record_by_mdmon
= 0;
5131 if (clear_migration_record
)
5132 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SIZE
);
5134 /* write the mpb for disks that compose raid devices */
5135 for (d
= super
->disks
; d
; d
= d
->next
) {
5136 if (d
->index
< 0 || is_failed(&d
->disk
))
5139 if (clear_migration_record
) {
5140 unsigned long long dsize
;
5142 get_dev_size(d
->fd
, NULL
, &dsize
);
5143 if (lseek64(d
->fd
, dsize
- 512, SEEK_SET
) >= 0) {
5144 if (write(d
->fd
, super
->migr_rec_buf
,
5145 MIGR_REC_BUF_SIZE
) != MIGR_REC_BUF_SIZE
)
5146 perror("Write migr_rec failed");
5150 if (store_imsm_mpb(d
->fd
, mpb
))
5152 "%s: failed for device %d:%d (fd: %d)%s\n",
5153 __func__
, d
->major
, d
->minor
,
5154 d
->fd
, strerror(errno
));
5163 return write_super_imsm_spares(super
, doclose
);
5169 static int create_array(struct supertype
*st
, int dev_idx
)
5172 struct imsm_update_create_array
*u
;
5173 struct intel_super
*super
= st
->sb
;
5174 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
5175 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
5176 struct disk_info
*inf
;
5177 struct imsm_disk
*disk
;
5180 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
5181 sizeof(*inf
) * map
->num_members
;
5184 fprintf(stderr
, "%s: failed to allocate update buffer\n",
5189 u
->type
= update_create_array
;
5190 u
->dev_idx
= dev_idx
;
5191 imsm_copy_dev(&u
->dev
, dev
);
5192 inf
= get_disk_info(u
);
5193 for (i
= 0; i
< map
->num_members
; i
++) {
5194 int idx
= get_imsm_disk_idx(dev
, i
, MAP_X
);
5196 disk
= get_imsm_disk(super
, idx
);
5197 serialcpy(inf
[i
].serial
, disk
->serial
);
5199 append_metadata_update(st
, u
, len
);
5204 static int mgmt_disk(struct supertype
*st
)
5206 struct intel_super
*super
= st
->sb
;
5208 struct imsm_update_add_remove_disk
*u
;
5210 if (!super
->disk_mgmt_list
)
5216 fprintf(stderr
, "%s: failed to allocate update buffer\n",
5221 u
->type
= update_add_remove_disk
;
5222 append_metadata_update(st
, u
, len
);
5227 static int write_init_super_imsm(struct supertype
*st
)
5229 struct intel_super
*super
= st
->sb
;
5230 int current_vol
= super
->current_vol
;
5232 /* we are done with current_vol reset it to point st at the container */
5233 super
->current_vol
= -1;
5235 if (st
->update_tail
) {
5236 /* queue the recently created array / added disk
5237 * as a metadata update */
5240 /* determine if we are creating a volume or adding a disk */
5241 if (current_vol
< 0) {
5242 /* in the mgmt (add/remove) disk case we are running
5243 * in mdmon context, so don't close fd's
5245 return mgmt_disk(st
);
5247 rv
= create_array(st
, current_vol
);
5252 for (d
= super
->disks
; d
; d
= d
->next
)
5253 Kill(d
->devname
, NULL
, 0, 1, 1);
5254 return write_super_imsm(st
, 1);
5259 static int store_super_imsm(struct supertype
*st
, int fd
)
5261 struct intel_super
*super
= st
->sb
;
5262 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
5268 return store_imsm_mpb(fd
, mpb
);
5274 static int imsm_bbm_log_size(struct imsm_super
*mpb
)
5276 return __le32_to_cpu(mpb
->bbm_log_size
);
5280 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
5281 int layout
, int raiddisks
, int chunk
,
5282 unsigned long long size
, char *dev
,
5283 unsigned long long *freesize
,
5287 unsigned long long ldsize
;
5288 struct intel_super
*super
=NULL
;
5291 if (level
!= LEVEL_CONTAINER
)
5296 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
5299 fprintf(stderr
, Name
": imsm: Cannot open %s: %s\n",
5300 dev
, strerror(errno
));
5303 if (!get_dev_size(fd
, dev
, &ldsize
)) {
5308 /* capabilities retrieve could be possible
5309 * note that there is no fd for the disks in array.
5311 super
= alloc_super();
5314 Name
": malloc of %zu failed.\n",
5320 rv
= find_intel_hba_capability(fd
, super
, verbose
? dev
: NULL
);
5324 fd2devname(fd
, str
);
5325 dprintf("validate_geometry_imsm_container: fd: %d %s orom: %p rv: %d raiddisk: %d\n",
5326 fd
, str
, super
->orom
, rv
, raiddisks
);
5328 /* no orom/efi or non-intel hba of the disk */
5334 if (super
->orom
&& raiddisks
> super
->orom
->tds
) {
5336 fprintf(stderr
, Name
": %d exceeds maximum number of"
5337 " platform supported disks: %d\n",
5338 raiddisks
, super
->orom
->tds
);
5344 *freesize
= avail_size_imsm(st
, ldsize
>> 9);
5350 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
5352 const unsigned long long base_start
= e
[*idx
].start
;
5353 unsigned long long end
= base_start
+ e
[*idx
].size
;
5356 if (base_start
== end
)
5360 for (i
= *idx
; i
< num_extents
; i
++) {
5361 /* extend overlapping extents */
5362 if (e
[i
].start
>= base_start
&&
5363 e
[i
].start
<= end
) {
5366 if (e
[i
].start
+ e
[i
].size
> end
)
5367 end
= e
[i
].start
+ e
[i
].size
;
5368 } else if (e
[i
].start
> end
) {
5374 return end
- base_start
;
5377 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
5379 /* build a composite disk with all known extents and generate a new
5380 * 'maxsize' given the "all disks in an array must share a common start
5381 * offset" constraint
5383 struct extent
*e
= calloc(sum_extents
, sizeof(*e
));
5387 unsigned long long pos
;
5388 unsigned long long start
= 0;
5389 unsigned long long maxsize
;
5390 unsigned long reserve
;
5395 /* coalesce and sort all extents. also, check to see if we need to
5396 * reserve space between member arrays
5399 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5402 for (i
= 0; i
< dl
->extent_cnt
; i
++)
5405 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
5410 while (i
< sum_extents
) {
5411 e
[j
].start
= e
[i
].start
;
5412 e
[j
].size
= find_size(e
, &i
, sum_extents
);
5414 if (e
[j
-1].size
== 0)
5423 unsigned long long esize
;
5425 esize
= e
[i
].start
- pos
;
5426 if (esize
>= maxsize
) {
5431 pos
= e
[i
].start
+ e
[i
].size
;
5433 } while (e
[i
-1].size
);
5439 /* FIXME assumes volume at offset 0 is the first volume in a
5442 if (start_extent
> 0)
5443 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
5447 if (maxsize
< reserve
)
5450 super
->create_offset
= ~((unsigned long long) 0);
5451 if (start
+ reserve
> super
->create_offset
)
5452 return 0; /* start overflows create_offset */
5453 super
->create_offset
= start
+ reserve
;
5455 return maxsize
- reserve
;
5458 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
5460 if (level
< 0 || level
== 6 || level
== 4)
5463 /* if we have an orom prevent invalid raid levels */
5466 case 0: return imsm_orom_has_raid0(orom
);
5469 return imsm_orom_has_raid1e(orom
);
5470 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
5471 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
5472 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
5475 return 1; /* not on an Intel RAID platform so anything goes */
5482 active_arrays_by_format(char *name
, char* hba
, struct md_list
**devlist
,
5483 int dpa
, int verbose
)
5485 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
5486 struct mdstat_ent
*memb
= NULL
;
5489 struct md_list
*dv
= NULL
;
5492 for (memb
= mdstat
; memb
; memb
= memb
->next
) {
5493 if (memb
->metadata_version
&&
5494 (strncmp(memb
->metadata_version
, "external:", 9) == 0) &&
5495 (strcmp(&memb
->metadata_version
[9], name
) == 0) &&
5496 !is_subarray(memb
->metadata_version
+9) &&
5498 struct dev_member
*dev
= memb
->members
;
5500 while(dev
&& (fd
< 0)) {
5501 char *path
= malloc(strlen(dev
->name
) + strlen("/dev/") + 1);
5503 num
= sprintf(path
, "%s%s", "/dev/", dev
->name
);
5505 fd
= open(path
, O_RDONLY
, 0);
5506 if ((num
<= 0) || (fd
< 0)) {
5507 pr_vrb(": Cannot open %s: %s\n",
5508 dev
->name
, strerror(errno
));
5515 if ((fd
>= 0) && disk_attached_to_hba(fd
, hba
)) {
5516 struct mdstat_ent
*vol
;
5517 for (vol
= mdstat
; vol
; vol
= vol
->next
) {
5518 if ((vol
->active
> 0) &&
5519 vol
->metadata_version
&&
5520 is_container_member(vol
, memb
->dev
)) {
5525 if (*devlist
&& (found
< dpa
)) {
5526 dv
= calloc(1, sizeof(*dv
));
5528 fprintf(stderr
, Name
": calloc failed\n");
5530 dv
->devname
= malloc(strlen(memb
->dev
) + strlen("/dev/") + 1);
5531 if (dv
->devname
!= NULL
) {
5532 sprintf(dv
->devname
, "%s%s", "/dev/", memb
->dev
);
5535 dv
->next
= *devlist
;
5546 free_mdstat(mdstat
);
5551 static struct md_list
*
5552 get_loop_devices(void)
5555 struct md_list
*devlist
= NULL
;
5556 struct md_list
*dv
= NULL
;
5558 for(i
= 0; i
< 12; i
++) {
5559 dv
= calloc(1, sizeof(*dv
));
5561 fprintf(stderr
, Name
": calloc failed\n");
5564 dv
->devname
= malloc(40);
5565 if (dv
->devname
== NULL
) {
5566 fprintf(stderr
, Name
": malloc failed\n");
5570 sprintf(dv
->devname
, "/dev/loop%d", i
);
5578 static struct md_list
*
5579 get_devices(const char *hba_path
)
5581 struct md_list
*devlist
= NULL
;
5582 struct md_list
*dv
= NULL
;
5588 devlist
= get_loop_devices();
5591 /* scroll through /sys/dev/block looking for devices attached to
5594 dir
= opendir("/sys/dev/block");
5595 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
5600 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
5602 path
= devt_to_devpath(makedev(major
, minor
));
5605 if (!path_attached_to_hba(path
, hba_path
)) {
5612 fd
= dev_open(ent
->d_name
, O_RDONLY
);
5614 fd2devname(fd
, buf
);
5617 fprintf(stderr
, Name
": cannot open device: %s\n",
5623 dv
= calloc(1, sizeof(*dv
));
5625 fprintf(stderr
, Name
": malloc failed\n");
5629 dv
->devname
= strdup(buf
);
5630 if (dv
->devname
== NULL
) {
5631 fprintf(stderr
, Name
": malloc failed\n");
5642 devlist
= devlist
->next
;
5651 count_volumes_list(struct md_list
*devlist
, char *homehost
,
5652 int verbose
, int *found
)
5654 struct md_list
*tmpdev
;
5656 struct supertype
*st
= NULL
;
5658 /* first walk the list of devices to find a consistent set
5659 * that match the criterea, if that is possible.
5660 * We flag the ones we like with 'used'.
5663 st
= match_metadata_desc_imsm("imsm");
5665 pr_vrb(": cannot allocate memory for imsm supertype\n");
5669 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5670 char *devname
= tmpdev
->devname
;
5672 struct supertype
*tst
;
5674 if (tmpdev
->used
> 1)
5676 tst
= dup_super(st
);
5678 pr_vrb(": cannot allocate memory for imsm supertype\n");
5681 tmpdev
->container
= 0;
5682 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
5684 dprintf(": cannot open device %s: %s\n",
5685 devname
, strerror(errno
));
5687 } else if (fstat(dfd
, &stb
)< 0) {
5689 dprintf(": fstat failed for %s: %s\n",
5690 devname
, strerror(errno
));
5692 } else if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
5693 dprintf(": %s is not a block device.\n",
5696 } else if (must_be_container(dfd
)) {
5697 struct supertype
*cst
;
5698 cst
= super_by_fd(dfd
, NULL
);
5700 dprintf(": cannot recognize container type %s\n",
5703 } else if (tst
->ss
!= st
->ss
) {
5704 dprintf(": non-imsm container - ignore it: %s\n",
5707 } else if (!tst
->ss
->load_container
||
5708 tst
->ss
->load_container(tst
, dfd
, NULL
))
5711 tmpdev
->container
= 1;
5714 cst
->ss
->free_super(cst
);
5716 tmpdev
->st_rdev
= stb
.st_rdev
;
5717 if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
5718 dprintf(": no RAID superblock on %s\n",
5721 } else if (tst
->ss
->compare_super
== NULL
) {
5722 dprintf(": Cannot assemble %s metadata on %s\n",
5723 tst
->ss
->name
, devname
);
5729 if (tmpdev
->used
== 2 || tmpdev
->used
== 4) {
5730 /* Ignore unrecognised devices during auto-assembly */
5735 tst
->ss
->getinfo_super(tst
, &info
, NULL
);
5737 if (st
->minor_version
== -1)
5738 st
->minor_version
= tst
->minor_version
;
5740 if (memcmp(info
.uuid
, uuid_zero
,
5741 sizeof(int[4])) == 0) {
5742 /* this is a floating spare. It cannot define
5743 * an array unless there are no more arrays of
5744 * this type to be found. It can be included
5745 * in an array of this type though.
5751 if (st
->ss
!= tst
->ss
||
5752 st
->minor_version
!= tst
->minor_version
||
5753 st
->ss
->compare_super(st
, tst
) != 0) {
5754 /* Some mismatch. If exactly one array matches this host,
5755 * we can resolve on that one.
5756 * Or, if we are auto assembling, we just ignore the second
5759 dprintf(": superblock on %s doesn't match others - assembly aborted\n",
5765 dprintf("found: devname: %s\n", devname
);
5769 tst
->ss
->free_super(tst
);
5773 if ((err
= load_super_imsm_all(st
, -1, &st
->sb
, NULL
, devlist
, 0)) == 0) {
5774 struct mdinfo
*iter
, *head
= st
->ss
->container_content(st
, NULL
);
5775 for (iter
= head
; iter
; iter
= iter
->next
) {
5776 dprintf("content->text_version: %s vol\n",
5777 iter
->text_version
);
5778 if (iter
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
5779 /* do not assemble arrays with unsupported
5781 dprintf(": Cannot activate member %s.\n",
5782 iter
->text_version
);
5789 dprintf(" no valid super block on device list: err: %d %p\n",
5793 dprintf(" no more devices to examin\n");
5796 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5797 if ((tmpdev
->used
== 1) && (tmpdev
->found
)) {
5799 if (count
< tmpdev
->found
)
5802 count
-= tmpdev
->found
;
5805 if (tmpdev
->used
== 1)
5810 st
->ss
->free_super(st
);
5816 count_volumes(char *hba
, int dpa
, int verbose
)
5818 struct md_list
*devlist
= NULL
;
5822 devlist
= get_devices(hba
);
5823 /* if no intel devices return zero volumes */
5824 if (devlist
== NULL
)
5827 count
= active_arrays_by_format("imsm", hba
, &devlist
, dpa
, verbose
);
5828 dprintf(" path: %s active arrays: %d\n", hba
, count
);
5829 if (devlist
== NULL
)
5833 count
+= count_volumes_list(devlist
,
5837 dprintf("found %d count: %d\n", found
, count
);
5840 dprintf("path: %s total number of volumes: %d\n", hba
, count
);
5843 struct md_list
*dv
= devlist
;
5844 devlist
= devlist
->next
;
5851 static int imsm_default_chunk(const struct imsm_orom
*orom
)
5853 /* up to 512 if the plaform supports it, otherwise the platform max.
5854 * 128 if no platform detected
5856 int fs
= max(7, orom
? fls(orom
->sss
) : 0);
5858 return min(512, (1 << fs
));
5862 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
5863 int raiddisks
, int *chunk
, int verbose
)
5865 /* check/set platform and metadata limits/defaults */
5866 if (super
->orom
&& raiddisks
> super
->orom
->dpa
) {
5867 pr_vrb(": platform supports a maximum of %d disks per array\n",
5872 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
5873 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
5874 pr_vrb(": platform does not support raid%d with %d disk%s\n",
5875 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
5879 if (chunk
&& (*chunk
== 0 || *chunk
== UnSet
))
5880 *chunk
= imsm_default_chunk(super
->orom
);
5882 if (super
->orom
&& chunk
&& !imsm_orom_has_chunk(super
->orom
, *chunk
)) {
5883 pr_vrb(": platform does not support a chunk size of: "
5888 if (layout
!= imsm_level_to_layout(level
)) {
5890 pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n");
5891 else if (level
== 10)
5892 pr_vrb(": imsm raid 10 only supports the n2 layout\n");
5894 pr_vrb(": imsm unknown layout %#x for this raid level %d\n",
5901 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
5902 * FIX ME add ahci details
5904 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
5905 int layout
, int raiddisks
, int *chunk
,
5906 unsigned long long size
, char *dev
,
5907 unsigned long long *freesize
,
5911 struct intel_super
*super
= st
->sb
;
5912 struct imsm_super
*mpb
;
5914 unsigned long long pos
= 0;
5915 unsigned long long maxsize
;
5919 /* We must have the container info already read in. */
5923 mpb
= super
->anchor
;
5925 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, verbose
)) {
5926 fprintf(stderr
, Name
": RAID gemetry validation failed. "
5927 "Cannot proceed with the action(s).\n");
5931 /* General test: make sure there is space for
5932 * 'raiddisks' device extents of size 'size' at a given
5935 unsigned long long minsize
= size
;
5936 unsigned long long start_offset
= MaxSector
;
5939 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
5940 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5945 e
= get_extents(super
, dl
);
5948 unsigned long long esize
;
5949 esize
= e
[i
].start
- pos
;
5950 if (esize
>= minsize
)
5952 if (found
&& start_offset
== MaxSector
) {
5955 } else if (found
&& pos
!= start_offset
) {
5959 pos
= e
[i
].start
+ e
[i
].size
;
5961 } while (e
[i
-1].size
);
5966 if (dcnt
< raiddisks
) {
5968 fprintf(stderr
, Name
": imsm: Not enough "
5969 "devices with space for this array "
5977 /* This device must be a member of the set */
5978 if (stat(dev
, &stb
) < 0)
5980 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
5982 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5983 if (dl
->major
== (int)major(stb
.st_rdev
) &&
5984 dl
->minor
== (int)minor(stb
.st_rdev
))
5989 fprintf(stderr
, Name
": %s is not in the "
5990 "same imsm set\n", dev
);
5992 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
5993 /* If a volume is present then the current creation attempt
5994 * cannot incorporate new spares because the orom may not
5995 * understand this configuration (all member disks must be
5996 * members of each array in the container).
5998 fprintf(stderr
, Name
": %s is a spare and a volume"
5999 " is already defined for this container\n", dev
);
6000 fprintf(stderr
, Name
": The option-rom requires all member"
6001 " disks to be a member of all volumes\n");
6003 } else if (super
->orom
&& mpb
->num_raid_devs
> 0 &&
6004 mpb
->num_disks
!= raiddisks
) {
6005 fprintf(stderr
, Name
": The option-rom requires all member"
6006 " disks to be a member of all volumes\n");
6010 /* retrieve the largest free space block */
6011 e
= get_extents(super
, dl
);
6016 unsigned long long esize
;
6018 esize
= e
[i
].start
- pos
;
6019 if (esize
>= maxsize
)
6021 pos
= e
[i
].start
+ e
[i
].size
;
6023 } while (e
[i
-1].size
);
6028 fprintf(stderr
, Name
": unable to determine free space for: %s\n",
6032 if (maxsize
< size
) {
6034 fprintf(stderr
, Name
": %s not enough space (%llu < %llu)\n",
6035 dev
, maxsize
, size
);
6039 /* count total number of extents for merge */
6041 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6043 i
+= dl
->extent_cnt
;
6045 maxsize
= merge_extents(super
, i
);
6047 if (!check_env("IMSM_NO_PLATFORM") &&
6048 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
6049 fprintf(stderr
, Name
": attempting to create a second "
6050 "volume with size less then remaining space. "
6055 if (maxsize
< size
|| maxsize
== 0) {
6058 fprintf(stderr
, Name
": no free space"
6059 " left on device. Aborting...\n");
6061 fprintf(stderr
, Name
": not enough space"
6062 " to create volume of given size"
6063 " (%llu < %llu). Aborting...\n",
6069 *freesize
= maxsize
;
6072 int count
= count_volumes(super
->hba
->path
,
6073 super
->orom
->dpa
, verbose
);
6074 if (super
->orom
->vphba
<= count
) {
6075 pr_vrb(": platform does not support more then %d raid volumes.\n",
6076 super
->orom
->vphba
);
6083 static int reserve_space(struct supertype
*st
, int raiddisks
,
6084 unsigned long long size
, int chunk
,
6085 unsigned long long *freesize
)
6087 struct intel_super
*super
= st
->sb
;
6088 struct imsm_super
*mpb
= super
->anchor
;
6093 unsigned long long maxsize
;
6094 unsigned long long minsize
;
6098 /* find the largest common start free region of the possible disks */
6102 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6108 /* don't activate new spares if we are orom constrained
6109 * and there is already a volume active in the container
6111 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
6114 e
= get_extents(super
, dl
);
6117 for (i
= 1; e
[i
-1].size
; i
++)
6125 maxsize
= merge_extents(super
, extent_cnt
);
6129 minsize
= chunk
* 2;
6131 if (cnt
< raiddisks
||
6132 (super
->orom
&& used
&& used
!= raiddisks
) ||
6133 maxsize
< minsize
||
6135 fprintf(stderr
, Name
": not enough devices with space to create array.\n");
6136 return 0; /* No enough free spaces large enough */
6147 if (!check_env("IMSM_NO_PLATFORM") &&
6148 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
6149 fprintf(stderr
, Name
": attempting to create a second "
6150 "volume with size less then remaining space. "
6155 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6157 dl
->raiddisk
= cnt
++;
6164 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
6165 int raiddisks
, int *chunk
, unsigned long long size
,
6166 char *dev
, unsigned long long *freesize
,
6174 * if given unused devices create a container
6175 * if given given devices in a container create a member volume
6177 if (level
== LEVEL_CONTAINER
) {
6178 /* Must be a fresh device to add to a container */
6179 return validate_geometry_imsm_container(st
, level
, layout
,
6181 chunk
?*chunk
:0, size
,
6188 struct intel_super
*super
= st
->sb
;
6189 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
6193 /* we are being asked to automatically layout a
6194 * new volume based on the current contents of
6195 * the container. If the the parameters can be
6196 * satisfied reserve_space will record the disks,
6197 * start offset, and size of the volume to be
6198 * created. add_to_super and getinfo_super
6199 * detect when autolayout is in progress.
6201 /* assuming that freesize is always given when array is
6203 if (super
->orom
&& freesize
) {
6205 count
= count_volumes(super
->hba
->path
,
6206 super
->orom
->dpa
, verbose
);
6207 if (super
->orom
->vphba
<= count
) {
6208 pr_vrb(": platform does not support more"
6209 "then %d raid volumes.\n",
6210 super
->orom
->vphba
);
6215 return reserve_space(st
, raiddisks
, size
,
6216 chunk
?*chunk
:0, freesize
);
6221 /* creating in a given container */
6222 return validate_geometry_imsm_volume(st
, level
, layout
,
6223 raiddisks
, chunk
, size
,
6224 dev
, freesize
, verbose
);
6227 /* This device needs to be a device in an 'imsm' container */
6228 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
6232 Name
": Cannot create this array on device %s\n",
6237 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
6239 fprintf(stderr
, Name
": Cannot open %s: %s\n",
6240 dev
, strerror(errno
));
6243 /* Well, it is in use by someone, maybe an 'imsm' container. */
6244 cfd
= open_container(fd
);
6248 fprintf(stderr
, Name
": Cannot use %s: It is busy\n",
6252 sra
= sysfs_read(cfd
, 0, GET_VERSION
);
6253 if (sra
&& sra
->array
.major_version
== -1 &&
6254 strcmp(sra
->text_version
, "imsm") == 0)
6258 /* This is a member of a imsm container. Load the container
6259 * and try to create a volume
6261 struct intel_super
*super
;
6263 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
, NULL
, 1) == 0) {
6265 st
->container_dev
= fd2devnum(cfd
);
6267 return validate_geometry_imsm_volume(st
, level
, layout
,
6276 fprintf(stderr
, Name
": failed container membership check\n");
6282 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
6284 struct intel_super
*super
= st
->sb
;
6286 if (level
&& *level
== UnSet
)
6287 *level
= LEVEL_CONTAINER
;
6289 if (level
&& layout
&& *layout
== UnSet
)
6290 *layout
= imsm_level_to_layout(*level
);
6292 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0))
6293 *chunk
= imsm_default_chunk(super
->orom
);
6296 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
6298 static int kill_subarray_imsm(struct supertype
*st
)
6300 /* remove the subarray currently referenced by ->current_vol */
6302 struct intel_dev
**dp
;
6303 struct intel_super
*super
= st
->sb
;
6304 __u8 current_vol
= super
->current_vol
;
6305 struct imsm_super
*mpb
= super
->anchor
;
6307 if (super
->current_vol
< 0)
6309 super
->current_vol
= -1; /* invalidate subarray cursor */
6311 /* block deletions that would change the uuid of active subarrays
6313 * FIXME when immutable ids are available, but note that we'll
6314 * also need to fixup the invalidated/active subarray indexes in
6317 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6320 if (i
< current_vol
)
6322 sprintf(subarray
, "%u", i
);
6323 if (is_subarray_active(subarray
, st
->devname
)) {
6325 Name
": deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
6332 if (st
->update_tail
) {
6333 struct imsm_update_kill_array
*u
= malloc(sizeof(*u
));
6337 u
->type
= update_kill_array
;
6338 u
->dev_idx
= current_vol
;
6339 append_metadata_update(st
, u
, sizeof(*u
));
6344 for (dp
= &super
->devlist
; *dp
;)
6345 if ((*dp
)->index
== current_vol
) {
6348 handle_missing(super
, (*dp
)->dev
);
6349 if ((*dp
)->index
> current_vol
)
6354 /* no more raid devices, all active components are now spares,
6355 * but of course failed are still failed
6357 if (--mpb
->num_raid_devs
== 0) {
6360 for (d
= super
->disks
; d
; d
= d
->next
)
6365 super
->updates_pending
++;
6370 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
6371 char *update
, struct mddev_ident
*ident
)
6373 /* update the subarray currently referenced by ->current_vol */
6374 struct intel_super
*super
= st
->sb
;
6375 struct imsm_super
*mpb
= super
->anchor
;
6377 if (strcmp(update
, "name") == 0) {
6378 char *name
= ident
->name
;
6382 if (is_subarray_active(subarray
, st
->devname
)) {
6384 Name
": Unable to update name of active subarray\n");
6388 if (!check_name(super
, name
, 0))
6391 vol
= strtoul(subarray
, &ep
, 10);
6392 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
6395 if (st
->update_tail
) {
6396 struct imsm_update_rename_array
*u
= malloc(sizeof(*u
));
6400 u
->type
= update_rename_array
;
6402 snprintf((char *) u
->name
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6403 append_metadata_update(st
, u
, sizeof(*u
));
6405 struct imsm_dev
*dev
;
6408 dev
= get_imsm_dev(super
, vol
);
6409 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6410 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6411 dev
= get_imsm_dev(super
, i
);
6412 handle_missing(super
, dev
);
6414 super
->updates_pending
++;
6421 #endif /* MDASSEMBLE */
6423 static int is_gen_migration(struct imsm_dev
*dev
)
6428 if (!dev
->vol
.migr_state
)
6431 if (migr_type(dev
) == MIGR_GEN_MIGR
)
6437 static int is_rebuilding(struct imsm_dev
*dev
)
6439 struct imsm_map
*migr_map
;
6441 if (!dev
->vol
.migr_state
)
6444 if (migr_type(dev
) != MIGR_REBUILD
)
6447 migr_map
= get_imsm_map(dev
, MAP_1
);
6449 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
6456 static int is_initializing(struct imsm_dev
*dev
)
6458 struct imsm_map
*migr_map
;
6460 if (!dev
->vol
.migr_state
)
6463 if (migr_type(dev
) != MIGR_INIT
)
6466 migr_map
= get_imsm_map(dev
, MAP_1
);
6468 if (migr_map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
6475 static void update_recovery_start(struct intel_super
*super
,
6476 struct imsm_dev
*dev
,
6477 struct mdinfo
*array
)
6479 struct mdinfo
*rebuild
= NULL
;
6483 if (!is_rebuilding(dev
))
6486 /* Find the rebuild target, but punt on the dual rebuild case */
6487 for (d
= array
->devs
; d
; d
= d
->next
)
6488 if (d
->recovery_start
== 0) {
6495 /* (?) none of the disks are marked with
6496 * IMSM_ORD_REBUILD, so assume they are missing and the
6497 * disk_ord_tbl was not correctly updated
6499 dprintf("%s: failed to locate out-of-sync disk\n", __func__
);
6503 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
6504 rebuild
->recovery_start
= units
* blocks_per_migr_unit(super
, dev
);
6508 static int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
);
6511 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
6513 /* Given a container loaded by load_super_imsm_all,
6514 * extract information about all the arrays into
6516 * If 'subarray' is given, just extract info about that array.
6518 * For each imsm_dev create an mdinfo, fill it in,
6519 * then look for matching devices in super->disks
6520 * and create appropriate device mdinfo.
6522 struct intel_super
*super
= st
->sb
;
6523 struct imsm_super
*mpb
= super
->anchor
;
6524 struct mdinfo
*rest
= NULL
;
6528 int spare_disks
= 0;
6530 /* do not assemble arrays when not all attributes are supported */
6531 if (imsm_check_attributes(mpb
->attributes
) == 0) {
6533 fprintf(stderr
, Name
": Unsupported attributes in IMSM metadata."
6534 "Arrays activation is blocked.\n");
6537 /* check for bad blocks */
6538 if (imsm_bbm_log_size(super
->anchor
)) {
6539 fprintf(stderr
, Name
": BBM log found in IMSM metadata."
6540 "Arrays activation is blocked.\n");
6545 /* count spare devices, not used in maps
6547 for (d
= super
->disks
; d
; d
= d
->next
)
6551 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6552 struct imsm_dev
*dev
;
6553 struct imsm_map
*map
;
6554 struct imsm_map
*map2
;
6555 struct mdinfo
*this;
6563 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
6566 dev
= get_imsm_dev(super
, i
);
6567 map
= get_imsm_map(dev
, MAP_0
);
6568 map2
= get_imsm_map(dev
, MAP_1
);
6570 /* do not publish arrays that are in the middle of an
6571 * unsupported migration
6573 if (dev
->vol
.migr_state
&&
6574 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
6575 fprintf(stderr
, Name
": cannot assemble volume '%.16s':"
6576 " unsupported migration in progress\n",
6580 /* do not publish arrays that are not support by controller's
6584 this = malloc(sizeof(*this));
6586 fprintf(stderr
, Name
": failed to allocate %zu bytes\n",
6591 super
->current_vol
= i
;
6592 getinfo_super_imsm_volume(st
, this, NULL
);
6595 chunk
= __le16_to_cpu(map
->blocks_per_strip
) >> 1;
6596 /* mdadm does not support all metadata features- set the bit in all arrays state */
6597 if (!validate_geometry_imsm_orom(super
,
6598 get_imsm_raid_level(map
), /* RAID level */
6599 imsm_level_to_layout(get_imsm_raid_level(map
)),
6600 map
->num_members
, /* raid disks */
6603 fprintf(stderr
, Name
": IMSM RAID geometry validation"
6604 " failed. Array %s activation is blocked.\n",
6606 this->array
.state
|=
6607 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
6608 (1<<MD_SB_BLOCK_VOLUME
);
6612 /* if array has bad blocks, set suitable bit in all arrays state */
6614 this->array
.state
|=
6615 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
6616 (1<<MD_SB_BLOCK_VOLUME
);
6618 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
6619 unsigned long long recovery_start
;
6620 struct mdinfo
*info_d
;
6627 idx
= get_imsm_disk_idx(dev
, slot
, MAP_0
);
6628 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
6629 for (d
= super
->disks
; d
; d
= d
->next
)
6630 if (d
->index
== idx
)
6633 recovery_start
= MaxSector
;
6636 if (d
&& is_failed(&d
->disk
))
6638 if (ord
& IMSM_ORD_REBUILD
)
6642 * if we skip some disks the array will be assmebled degraded;
6643 * reset resync start to avoid a dirty-degraded
6644 * situation when performing the intial sync
6646 * FIXME handle dirty degraded
6648 if ((skip
|| recovery_start
== 0) && !dev
->vol
.dirty
)
6649 this->resync_start
= MaxSector
;
6653 info_d
= calloc(1, sizeof(*info_d
));
6655 fprintf(stderr
, Name
": failed to allocate disk"
6656 " for volume %.16s\n", dev
->volume
);
6657 info_d
= this->devs
;
6659 struct mdinfo
*d
= info_d
->next
;
6668 info_d
->next
= this->devs
;
6669 this->devs
= info_d
;
6671 info_d
->disk
.number
= d
->index
;
6672 info_d
->disk
.major
= d
->major
;
6673 info_d
->disk
.minor
= d
->minor
;
6674 info_d
->disk
.raid_disk
= slot
;
6675 info_d
->recovery_start
= recovery_start
;
6677 if (slot
< map2
->num_members
)
6678 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
6680 this->array
.spare_disks
++;
6682 if (slot
< map
->num_members
)
6683 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
6685 this->array
.spare_disks
++;
6687 if (info_d
->recovery_start
== MaxSector
)
6688 this->array
.working_disks
++;
6690 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
6691 info_d
->data_offset
= pba_of_lba0(map
);
6692 info_d
->component_size
= blocks_per_member(map
);
6694 /* now that the disk list is up-to-date fixup recovery_start */
6695 update_recovery_start(super
, dev
, this);
6696 this->array
.spare_disks
+= spare_disks
;
6699 /* check for reshape */
6700 if (this->reshape_active
== 1)
6701 recover_backup_imsm(st
, this);
6710 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
6711 int failed
, int look_in_map
)
6713 struct imsm_map
*map
;
6715 map
= get_imsm_map(dev
, look_in_map
);
6718 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
6719 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
6721 switch (get_imsm_raid_level(map
)) {
6723 return IMSM_T_STATE_FAILED
;
6726 if (failed
< map
->num_members
)
6727 return IMSM_T_STATE_DEGRADED
;
6729 return IMSM_T_STATE_FAILED
;
6734 * check to see if any mirrors have failed, otherwise we
6735 * are degraded. Even numbered slots are mirrored on
6739 /* gcc -Os complains that this is unused */
6740 int insync
= insync
;
6742 for (i
= 0; i
< map
->num_members
; i
++) {
6743 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_X
);
6744 int idx
= ord_to_idx(ord
);
6745 struct imsm_disk
*disk
;
6747 /* reset the potential in-sync count on even-numbered
6748 * slots. num_copies is always 2 for imsm raid10
6753 disk
= get_imsm_disk(super
, idx
);
6754 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
6757 /* no in-sync disks left in this mirror the
6761 return IMSM_T_STATE_FAILED
;
6764 return IMSM_T_STATE_DEGRADED
;
6768 return IMSM_T_STATE_DEGRADED
;
6770 return IMSM_T_STATE_FAILED
;
6776 return map
->map_state
;
6779 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
6784 struct imsm_disk
*disk
;
6785 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
6786 struct imsm_map
*prev
= get_imsm_map(dev
, MAP_1
);
6787 struct imsm_map
*map_for_loop
;
6792 /* at the beginning of migration we set IMSM_ORD_REBUILD on
6793 * disks that are being rebuilt. New failures are recorded to
6794 * map[0]. So we look through all the disks we started with and
6795 * see if any failures are still present, or if any new ones
6799 if (prev
&& (map
->num_members
< prev
->num_members
))
6800 map_for_loop
= prev
;
6802 for (i
= 0; i
< map_for_loop
->num_members
; i
++) {
6804 /* when MAP_X is passed both maps failures are counted
6807 ((look_in_map
== MAP_1
) || (look_in_map
== MAP_X
)) &&
6808 (i
< prev
->num_members
)) {
6809 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
6810 idx_1
= ord_to_idx(ord
);
6812 disk
= get_imsm_disk(super
, idx_1
);
6813 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
6816 if (((look_in_map
== MAP_0
) || (look_in_map
== MAP_X
)) &&
6817 (i
< map
->num_members
)) {
6818 ord
= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
6819 idx
= ord_to_idx(ord
);
6822 disk
= get_imsm_disk(super
, idx
);
6823 if (!disk
|| is_failed(disk
) ||
6824 ord
& IMSM_ORD_REBUILD
)
6834 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
6837 struct intel_super
*super
= c
->sb
;
6838 struct imsm_super
*mpb
= super
->anchor
;
6840 if (atoi(inst
) >= mpb
->num_raid_devs
) {
6841 fprintf(stderr
, "%s: subarry index %d, out of range\n",
6842 __func__
, atoi(inst
));
6846 dprintf("imsm: open_new %s\n", inst
);
6847 a
->info
.container_member
= atoi(inst
);
6851 static int is_resyncing(struct imsm_dev
*dev
)
6853 struct imsm_map
*migr_map
;
6855 if (!dev
->vol
.migr_state
)
6858 if (migr_type(dev
) == MIGR_INIT
||
6859 migr_type(dev
) == MIGR_REPAIR
)
6862 if (migr_type(dev
) == MIGR_GEN_MIGR
)
6865 migr_map
= get_imsm_map(dev
, MAP_1
);
6867 if ((migr_map
->map_state
== IMSM_T_STATE_NORMAL
) &&
6868 (dev
->vol
.migr_type
!= MIGR_GEN_MIGR
))
6874 /* return true if we recorded new information */
6875 static int mark_failure(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
6879 struct imsm_map
*map
;
6880 char buf
[MAX_RAID_SERIAL_LEN
+3];
6881 unsigned int len
, shift
= 0;
6883 /* new failures are always set in map[0] */
6884 map
= get_imsm_map(dev
, MAP_0
);
6886 slot
= get_imsm_disk_slot(map
, idx
);
6890 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
6891 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
6894 memcpy(buf
, disk
->serial
, MAX_RAID_SERIAL_LEN
);
6895 buf
[MAX_RAID_SERIAL_LEN
] = '\000';
6897 if ((len
= strlen(buf
)) >= MAX_RAID_SERIAL_LEN
)
6898 shift
= len
- MAX_RAID_SERIAL_LEN
+ 1;
6899 strncpy((char *)disk
->serial
, &buf
[shift
], MAX_RAID_SERIAL_LEN
);
6901 disk
->status
|= FAILED_DISK
;
6902 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
6903 /* mark failures in second map if second map exists and this disk
6905 * This is valid for migration, initialization and rebuild
6907 if (dev
->vol
.migr_state
) {
6908 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
6909 int slot2
= get_imsm_disk_slot(map2
, idx
);
6911 if ((slot2
< map2
->num_members
) &&
6913 set_imsm_ord_tbl_ent(map2
, slot2
,
6914 idx
| IMSM_ORD_REBUILD
);
6916 if (map
->failed_disk_num
== 0xff)
6917 map
->failed_disk_num
= slot
;
6921 static void mark_missing(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
6923 mark_failure(dev
, disk
, idx
);
6925 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
6928 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
6929 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
6932 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
6936 if (!super
->missing
)
6939 dprintf("imsm: mark missing\n");
6940 /* end process for initialization and rebuild only
6942 if (is_gen_migration(dev
) == 0) {
6946 failed
= imsm_count_failed(super
, dev
, MAP_0
);
6947 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
6949 end_migration(dev
, super
, map_state
);
6951 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
6952 mark_missing(dev
, &dl
->disk
, dl
->index
);
6953 super
->updates_pending
++;
6956 static unsigned long long imsm_set_array_size(struct imsm_dev
*dev
)
6958 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
6959 unsigned long long array_blocks
;
6960 struct imsm_map
*map
;
6962 if (used_disks
== 0) {
6963 /* when problems occures
6964 * return current array_blocks value
6966 array_blocks
= __le32_to_cpu(dev
->size_high
);
6967 array_blocks
= array_blocks
<< 32;
6968 array_blocks
+= __le32_to_cpu(dev
->size_low
);
6970 return array_blocks
;
6973 /* set array size in metadata
6975 map
= get_imsm_map(dev
, MAP_0
);
6976 array_blocks
= blocks_per_member(map
) * used_disks
;
6978 /* round array size down to closest MB
6980 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
6981 dev
->size_low
= __cpu_to_le32((__u32
)array_blocks
);
6982 dev
->size_high
= __cpu_to_le32((__u32
)(array_blocks
>> 32));
6984 return array_blocks
;
6987 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
6989 static void imsm_progress_container_reshape(struct intel_super
*super
)
6991 /* if no device has a migr_state, but some device has a
6992 * different number of members than the previous device, start
6993 * changing the number of devices in this device to match
6996 struct imsm_super
*mpb
= super
->anchor
;
6997 int prev_disks
= -1;
7001 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7002 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
7003 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7004 struct imsm_map
*map2
;
7005 int prev_num_members
;
7007 if (dev
->vol
.migr_state
)
7010 if (prev_disks
== -1)
7011 prev_disks
= map
->num_members
;
7012 if (prev_disks
== map
->num_members
)
7015 /* OK, this array needs to enter reshape mode.
7016 * i.e it needs a migr_state
7019 copy_map_size
= sizeof_imsm_map(map
);
7020 prev_num_members
= map
->num_members
;
7021 map
->num_members
= prev_disks
;
7022 dev
->vol
.migr_state
= 1;
7023 dev
->vol
.curr_migr_unit
= 0;
7024 set_migr_type(dev
, MIGR_GEN_MIGR
);
7025 for (i
= prev_num_members
;
7026 i
< map
->num_members
; i
++)
7027 set_imsm_ord_tbl_ent(map
, i
, i
);
7028 map2
= get_imsm_map(dev
, MAP_1
);
7029 /* Copy the current map */
7030 memcpy(map2
, map
, copy_map_size
);
7031 map2
->num_members
= prev_num_members
;
7033 imsm_set_array_size(dev
);
7034 super
->clean_migration_record_by_mdmon
= 1;
7035 super
->updates_pending
++;
7039 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
7040 * states are handled in imsm_set_disk() with one exception, when a
7041 * resync is stopped due to a new failure this routine will set the
7042 * 'degraded' state for the array.
7044 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
7046 int inst
= a
->info
.container_member
;
7047 struct intel_super
*super
= a
->container
->sb
;
7048 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7049 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7050 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
7051 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7052 __u32 blocks_per_unit
;
7054 if (dev
->vol
.migr_state
&&
7055 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
7056 /* array state change is blocked due to reshape action
7058 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
7059 * - finish the reshape (if last_checkpoint is big and action != reshape)
7060 * - update curr_migr_unit
7062 if (a
->curr_action
== reshape
) {
7063 /* still reshaping, maybe update curr_migr_unit */
7064 goto mark_checkpoint
;
7066 if (a
->last_checkpoint
== 0 && a
->prev_action
== reshape
) {
7067 /* for some reason we aborted the reshape.
7069 * disable automatic metadata rollback
7070 * user action is required to recover process
7073 struct imsm_map
*map2
=
7074 get_imsm_map(dev
, MAP_1
);
7075 dev
->vol
.migr_state
= 0;
7076 set_migr_type(dev
, 0);
7077 dev
->vol
.curr_migr_unit
= 0;
7079 sizeof_imsm_map(map2
));
7080 super
->updates_pending
++;
7083 if (a
->last_checkpoint
>= a
->info
.component_size
) {
7084 unsigned long long array_blocks
;
7088 used_disks
= imsm_num_data_members(dev
, MAP_0
);
7089 if (used_disks
> 0) {
7091 blocks_per_member(map
) *
7093 /* round array size down to closest MB
7095 array_blocks
= (array_blocks
7096 >> SECT_PER_MB_SHIFT
)
7097 << SECT_PER_MB_SHIFT
;
7098 a
->info
.custom_array_size
= array_blocks
;
7099 /* encourage manager to update array
7103 a
->check_reshape
= 1;
7105 /* finalize online capacity expansion/reshape */
7106 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
7108 mdi
->disk
.raid_disk
,
7111 imsm_progress_container_reshape(super
);
7116 /* before we activate this array handle any missing disks */
7117 if (consistent
== 2)
7118 handle_missing(super
, dev
);
7120 if (consistent
== 2 &&
7121 (!is_resync_complete(&a
->info
) ||
7122 map_state
!= IMSM_T_STATE_NORMAL
||
7123 dev
->vol
.migr_state
))
7126 if (is_resync_complete(&a
->info
)) {
7127 /* complete intialization / resync,
7128 * recovery and interrupted recovery is completed in
7131 if (is_resyncing(dev
)) {
7132 dprintf("imsm: mark resync done\n");
7133 end_migration(dev
, super
, map_state
);
7134 super
->updates_pending
++;
7135 a
->last_checkpoint
= 0;
7137 } else if ((!is_resyncing(dev
) && !failed
) &&
7138 (imsm_reshape_blocks_arrays_changes(super
) == 0)) {
7139 /* mark the start of the init process if nothing is failed */
7140 dprintf("imsm: mark resync start\n");
7141 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
7142 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
7144 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
7145 super
->updates_pending
++;
7149 /* skip checkpointing for general migration,
7150 * it is controlled in mdadm
7152 if (is_gen_migration(dev
))
7153 goto skip_mark_checkpoint
;
7155 /* check if we can update curr_migr_unit from resync_start, recovery_start */
7156 blocks_per_unit
= blocks_per_migr_unit(super
, dev
);
7157 if (blocks_per_unit
) {
7161 units
= a
->last_checkpoint
/ blocks_per_unit
;
7164 /* check that we did not overflow 32-bits, and that
7165 * curr_migr_unit needs updating
7167 if (units32
== units
&&
7169 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
7170 dprintf("imsm: mark checkpoint (%u)\n", units32
);
7171 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
7172 super
->updates_pending
++;
7176 skip_mark_checkpoint
:
7177 /* mark dirty / clean */
7178 if (dev
->vol
.dirty
!= !consistent
) {
7179 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
7184 super
->updates_pending
++;
7190 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
7192 int inst
= a
->info
.container_member
;
7193 struct intel_super
*super
= a
->container
->sb
;
7194 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7195 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7196 struct imsm_disk
*disk
;
7201 if (n
> map
->num_members
)
7202 fprintf(stderr
, "imsm: set_disk %d out of range 0..%d\n",
7203 n
, map
->num_members
- 1);
7208 dprintf("imsm: set_disk %d:%x\n", n
, state
);
7210 ord
= get_imsm_ord_tbl_ent(dev
, n
, MAP_0
);
7211 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
7213 /* check for new failures */
7214 if (state
& DS_FAULTY
) {
7215 if (mark_failure(dev
, disk
, ord_to_idx(ord
)))
7216 super
->updates_pending
++;
7219 /* check if in_sync */
7220 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
7221 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
7223 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
7224 super
->updates_pending
++;
7227 failed
= imsm_count_failed(super
, dev
, MAP_0
);
7228 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7230 /* check if recovery complete, newly degraded, or failed */
7231 dprintf("imsm: Detected transition to state ");
7232 switch (map_state
) {
7233 case IMSM_T_STATE_NORMAL
: /* transition to normal state */
7234 dprintf("normal: ");
7235 if (is_rebuilding(dev
)) {
7236 dprintf("while rebuilding");
7237 end_migration(dev
, super
, map_state
);
7238 map
= get_imsm_map(dev
, MAP_0
);
7239 map
->failed_disk_num
= ~0;
7240 super
->updates_pending
++;
7241 a
->last_checkpoint
= 0;
7244 if (is_gen_migration(dev
)) {
7245 dprintf("while general migration");
7246 if (a
->last_checkpoint
>= a
->info
.component_size
)
7247 end_migration(dev
, super
, map_state
);
7249 map
->map_state
= map_state
;
7250 map
= get_imsm_map(dev
, MAP_0
);
7251 map
->failed_disk_num
= ~0;
7252 super
->updates_pending
++;
7256 case IMSM_T_STATE_DEGRADED
: /* transition to degraded state */
7257 dprintf("degraded: ");
7258 if ((map
->map_state
!= map_state
) &&
7259 !dev
->vol
.migr_state
) {
7260 dprintf("mark degraded");
7261 map
->map_state
= map_state
;
7262 super
->updates_pending
++;
7263 a
->last_checkpoint
= 0;
7266 if (is_rebuilding(dev
)) {
7267 dprintf("while rebuilding.");
7268 if (map
->map_state
!= map_state
) {
7269 dprintf(" Map state change");
7270 end_migration(dev
, super
, map_state
);
7271 super
->updates_pending
++;
7275 if (is_gen_migration(dev
)) {
7276 dprintf("while general migration");
7277 if (a
->last_checkpoint
>= a
->info
.component_size
)
7278 end_migration(dev
, super
, map_state
);
7280 map
->map_state
= map_state
;
7281 manage_second_map(super
, dev
);
7283 super
->updates_pending
++;
7286 if (is_initializing(dev
)) {
7287 dprintf("while initialization.");
7288 map
->map_state
= map_state
;
7289 super
->updates_pending
++;
7293 case IMSM_T_STATE_FAILED
: /* transition to failed state */
7294 dprintf("failed: ");
7295 if (is_gen_migration(dev
)) {
7296 dprintf("while general migration");
7297 map
->map_state
= map_state
;
7298 super
->updates_pending
++;
7301 if (map
->map_state
!= map_state
) {
7302 dprintf("mark failed");
7303 end_migration(dev
, super
, map_state
);
7304 super
->updates_pending
++;
7305 a
->last_checkpoint
= 0;
7310 dprintf("state %i\n", map_state
);
7316 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
7319 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
7320 unsigned long long dsize
;
7321 unsigned long long sectors
;
7323 get_dev_size(fd
, NULL
, &dsize
);
7325 if (mpb_size
> 512) {
7326 /* -1 to account for anchor */
7327 sectors
= mpb_sectors(mpb
) - 1;
7329 /* write the extended mpb to the sectors preceeding the anchor */
7330 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0)
7333 if ((unsigned long long)write(fd
, buf
+ 512, 512 * sectors
)
7338 /* first block is stored on second to last sector of the disk */
7339 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0)
7342 if (write(fd
, buf
, 512) != 512)
7348 static void imsm_sync_metadata(struct supertype
*container
)
7350 struct intel_super
*super
= container
->sb
;
7352 dprintf("sync metadata: %d\n", super
->updates_pending
);
7353 if (!super
->updates_pending
)
7356 write_super_imsm(container
, 0);
7358 super
->updates_pending
= 0;
7361 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
7363 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
7364 int i
= get_imsm_disk_idx(dev
, idx
, MAP_X
);
7367 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7371 if (dl
&& is_failed(&dl
->disk
))
7375 dprintf("%s: found %x:%x\n", __func__
, dl
->major
, dl
->minor
);
7380 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
7381 struct active_array
*a
, int activate_new
,
7382 struct mdinfo
*additional_test_list
)
7384 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
7385 int idx
= get_imsm_disk_idx(dev
, slot
, MAP_X
);
7386 struct imsm_super
*mpb
= super
->anchor
;
7387 struct imsm_map
*map
;
7388 unsigned long long pos
;
7393 __u32 array_start
= 0;
7394 __u32 array_end
= 0;
7396 struct mdinfo
*test_list
;
7398 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7399 /* If in this array, skip */
7400 for (d
= a
->info
.devs
; d
; d
= d
->next
)
7401 if (d
->state_fd
>= 0 &&
7402 d
->disk
.major
== dl
->major
&&
7403 d
->disk
.minor
== dl
->minor
) {
7404 dprintf("%x:%x already in array\n",
7405 dl
->major
, dl
->minor
);
7410 test_list
= additional_test_list
;
7412 if (test_list
->disk
.major
== dl
->major
&&
7413 test_list
->disk
.minor
== dl
->minor
) {
7414 dprintf("%x:%x already in additional test list\n",
7415 dl
->major
, dl
->minor
);
7418 test_list
= test_list
->next
;
7423 /* skip in use or failed drives */
7424 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
7426 dprintf("%x:%x status (failed: %d index: %d)\n",
7427 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
7431 /* skip pure spares when we are looking for partially
7432 * assimilated drives
7434 if (dl
->index
== -1 && !activate_new
)
7437 /* Does this unused device have the requisite free space?
7438 * It needs to be able to cover all member volumes
7440 ex
= get_extents(super
, dl
);
7442 dprintf("cannot get extents\n");
7445 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7446 dev
= get_imsm_dev(super
, i
);
7447 map
= get_imsm_map(dev
, MAP_0
);
7449 /* check if this disk is already a member of
7452 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
7458 array_start
= pba_of_lba0(map
);
7459 array_end
= array_start
+
7460 blocks_per_member(map
) - 1;
7463 /* check that we can start at pba_of_lba0 with
7464 * blocks_per_member of space
7466 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
7470 pos
= ex
[j
].start
+ ex
[j
].size
;
7472 } while (ex
[j
-1].size
);
7479 if (i
< mpb
->num_raid_devs
) {
7480 dprintf("%x:%x does not have %u to %u available\n",
7481 dl
->major
, dl
->minor
, array_start
, array_end
);
7492 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
7494 struct imsm_dev
*dev2
;
7495 struct imsm_map
*map
;
7501 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
7503 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
, MAP_0
);
7504 if (state
== IMSM_T_STATE_FAILED
) {
7505 map
= get_imsm_map(dev2
, MAP_0
);
7508 for (slot
= 0; slot
< map
->num_members
; slot
++) {
7510 * Check if failed disks are deleted from intel
7511 * disk list or are marked to be deleted
7513 idx
= get_imsm_disk_idx(dev2
, slot
, MAP_X
);
7514 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
7516 * Do not rebuild the array if failed disks
7517 * from failed sub-array are not removed from
7521 is_failed(&idisk
->disk
) &&
7522 (idisk
->action
!= DISK_REMOVE
))
7530 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
7531 struct metadata_update
**updates
)
7534 * Find a device with unused free space and use it to replace a
7535 * failed/vacant region in an array. We replace failed regions one a
7536 * array at a time. The result is that a new spare disk will be added
7537 * to the first failed array and after the monitor has finished
7538 * propagating failures the remainder will be consumed.
7540 * FIXME add a capability for mdmon to request spares from another
7544 struct intel_super
*super
= a
->container
->sb
;
7545 int inst
= a
->info
.container_member
;
7546 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7547 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7548 int failed
= a
->info
.array
.raid_disks
;
7549 struct mdinfo
*rv
= NULL
;
7552 struct metadata_update
*mu
;
7554 struct imsm_update_activate_spare
*u
;
7559 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
7560 if ((d
->curr_state
& DS_FAULTY
) &&
7562 /* wait for Removal to happen */
7564 if (d
->state_fd
>= 0)
7568 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
7569 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
7571 if (imsm_reshape_blocks_arrays_changes(super
))
7574 /* Cannot activate another spare if rebuild is in progress already
7576 if (is_rebuilding(dev
)) {
7577 dprintf("imsm: No spare activation allowed. "
7578 "Rebuild in progress already.\n");
7582 if (a
->info
.array
.level
== 4)
7583 /* No repair for takeovered array
7584 * imsm doesn't support raid4
7588 if (imsm_check_degraded(super
, dev
, failed
, MAP_0
) !=
7589 IMSM_T_STATE_DEGRADED
)
7593 * If there are any failed disks check state of the other volume.
7594 * Block rebuild if the another one is failed until failed disks
7595 * are removed from container.
7598 dprintf("found failed disks in %.*s, check if there another"
7599 "failed sub-array.\n",
7600 MAX_RAID_SERIAL_LEN
, dev
->volume
);
7601 /* check if states of the other volumes allow for rebuild */
7602 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
7604 allowed
= imsm_rebuild_allowed(a
->container
,
7612 /* For each slot, if it is not working, find a spare */
7613 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
7614 for (d
= a
->info
.devs
; d
; d
= d
->next
)
7615 if (d
->disk
.raid_disk
== i
)
7617 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
7618 if (d
&& (d
->state_fd
>= 0))
7622 * OK, this device needs recovery. Try to re-add the
7623 * previous occupant of this slot, if this fails see if
7624 * we can continue the assimilation of a spare that was
7625 * partially assimilated, finally try to activate a new
7628 dl
= imsm_readd(super
, i
, a
);
7630 dl
= imsm_add_spare(super
, i
, a
, 0, rv
);
7632 dl
= imsm_add_spare(super
, i
, a
, 1, rv
);
7636 /* found a usable disk with enough space */
7637 di
= malloc(sizeof(*di
));
7640 memset(di
, 0, sizeof(*di
));
7642 /* dl->index will be -1 in the case we are activating a
7643 * pristine spare. imsm_process_update() will create a
7644 * new index in this case. Once a disk is found to be
7645 * failed in all member arrays it is kicked from the
7648 di
->disk
.number
= dl
->index
;
7650 /* (ab)use di->devs to store a pointer to the device
7653 di
->devs
= (struct mdinfo
*) dl
;
7655 di
->disk
.raid_disk
= i
;
7656 di
->disk
.major
= dl
->major
;
7657 di
->disk
.minor
= dl
->minor
;
7659 di
->recovery_start
= 0;
7660 di
->data_offset
= pba_of_lba0(map
);
7661 di
->component_size
= a
->info
.component_size
;
7662 di
->container_member
= inst
;
7663 super
->random
= random32();
7667 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
7668 i
, di
->data_offset
);
7672 /* No spares found */
7674 /* Now 'rv' has a list of devices to return.
7675 * Create a metadata_update record to update the
7676 * disk_ord_tbl for the array
7678 mu
= malloc(sizeof(*mu
));
7680 mu
->buf
= malloc(sizeof(struct imsm_update_activate_spare
) * num_spares
);
7681 if (mu
->buf
== NULL
) {
7688 struct mdinfo
*n
= rv
->next
;
7697 mu
->space_list
= NULL
;
7698 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
7699 mu
->next
= *updates
;
7700 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
7702 for (di
= rv
; di
; di
= di
->next
) {
7703 u
->type
= update_activate_spare
;
7704 u
->dl
= (struct dl
*) di
->devs
;
7706 u
->slot
= di
->disk
.raid_disk
;
7717 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
7719 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
7720 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7721 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, MAP_0
);
7722 struct disk_info
*inf
= get_disk_info(u
);
7723 struct imsm_disk
*disk
;
7727 for (i
= 0; i
< map
->num_members
; i
++) {
7728 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, MAP_X
));
7729 for (j
= 0; j
< new_map
->num_members
; j
++)
7730 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
7738 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
7740 struct dl
*dl
= NULL
;
7741 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7742 if ((dl
->major
== major
) && (dl
->minor
== minor
))
7747 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
7749 struct dl
*prev
= NULL
;
7753 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7754 if ((dl
->major
== major
) && (dl
->minor
== minor
)) {
7757 prev
->next
= dl
->next
;
7759 super
->disks
= dl
->next
;
7761 __free_imsm_disk(dl
);
7762 dprintf("%s: removed %x:%x\n",
7763 __func__
, major
, minor
);
7771 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
7773 static int add_remove_disk_update(struct intel_super
*super
)
7775 int check_degraded
= 0;
7776 struct dl
*disk
= NULL
;
7777 /* add/remove some spares to/from the metadata/contrainer */
7778 while (super
->disk_mgmt_list
) {
7779 struct dl
*disk_cfg
;
7781 disk_cfg
= super
->disk_mgmt_list
;
7782 super
->disk_mgmt_list
= disk_cfg
->next
;
7783 disk_cfg
->next
= NULL
;
7785 if (disk_cfg
->action
== DISK_ADD
) {
7786 disk_cfg
->next
= super
->disks
;
7787 super
->disks
= disk_cfg
;
7789 dprintf("%s: added %x:%x\n",
7790 __func__
, disk_cfg
->major
,
7792 } else if (disk_cfg
->action
== DISK_REMOVE
) {
7793 dprintf("Disk remove action processed: %x.%x\n",
7794 disk_cfg
->major
, disk_cfg
->minor
);
7795 disk
= get_disk_super(super
,
7799 /* store action status */
7800 disk
->action
= DISK_REMOVE
;
7801 /* remove spare disks only */
7802 if (disk
->index
== -1) {
7803 remove_disk_super(super
,
7808 /* release allocate disk structure */
7809 __free_imsm_disk(disk_cfg
);
7812 return check_degraded
;
7816 static int apply_reshape_migration_update(struct imsm_update_reshape_migration
*u
,
7817 struct intel_super
*super
,
7820 struct intel_dev
*id
;
7821 void **tofree
= NULL
;
7824 dprintf("apply_reshape_migration_update()\n");
7825 if ((u
->subdev
< 0) ||
7827 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
7830 if ((space_list
== NULL
) || (*space_list
== NULL
)) {
7831 dprintf("imsm: Error: Memory is not allocated\n");
7835 for (id
= super
->devlist
; id
; id
= id
->next
) {
7836 if (id
->index
== (unsigned)u
->subdev
) {
7837 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
7838 struct imsm_map
*map
;
7839 struct imsm_dev
*new_dev
=
7840 (struct imsm_dev
*)*space_list
;
7841 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
7843 struct dl
*new_disk
;
7845 if (new_dev
== NULL
)
7847 *space_list
= **space_list
;
7848 memcpy(new_dev
, dev
, sizeof_imsm_dev(dev
, 0));
7849 map
= get_imsm_map(new_dev
, MAP_0
);
7851 dprintf("imsm: Error: migration in progress");
7855 to_state
= map
->map_state
;
7856 if ((u
->new_level
== 5) && (map
->raid_level
== 0)) {
7858 /* this should not happen */
7859 if (u
->new_disks
[0] < 0) {
7860 map
->failed_disk_num
=
7861 map
->num_members
- 1;
7862 to_state
= IMSM_T_STATE_DEGRADED
;
7864 to_state
= IMSM_T_STATE_NORMAL
;
7866 migrate(new_dev
, super
, to_state
, MIGR_GEN_MIGR
);
7867 if (u
->new_level
> -1)
7868 map
->raid_level
= u
->new_level
;
7869 migr_map
= get_imsm_map(new_dev
, MAP_1
);
7870 if ((u
->new_level
== 5) &&
7871 (migr_map
->raid_level
== 0)) {
7872 int ord
= map
->num_members
- 1;
7873 migr_map
->num_members
--;
7874 if (u
->new_disks
[0] < 0)
7875 ord
|= IMSM_ORD_REBUILD
;
7876 set_imsm_ord_tbl_ent(map
,
7877 map
->num_members
- 1,
7881 tofree
= (void **)dev
;
7883 /* update chunk size
7885 if (u
->new_chunksize
> 0)
7886 map
->blocks_per_strip
=
7887 __cpu_to_le16(u
->new_chunksize
* 2);
7891 if ((u
->new_level
!= 5) ||
7892 (migr_map
->raid_level
!= 0) ||
7893 (migr_map
->raid_level
== map
->raid_level
))
7896 if (u
->new_disks
[0] >= 0) {
7899 new_disk
= get_disk_super(super
,
7900 major(u
->new_disks
[0]),
7901 minor(u
->new_disks
[0]));
7902 dprintf("imsm: new disk for reshape is: %i:%i "
7903 "(%p, index = %i)\n",
7904 major(u
->new_disks
[0]),
7905 minor(u
->new_disks
[0]),
7906 new_disk
, new_disk
->index
);
7907 if (new_disk
== NULL
)
7908 goto error_disk_add
;
7910 new_disk
->index
= map
->num_members
- 1;
7911 /* slot to fill in autolayout
7913 new_disk
->raiddisk
= new_disk
->index
;
7914 new_disk
->disk
.status
|= CONFIGURED_DISK
;
7915 new_disk
->disk
.status
&= ~SPARE_DISK
;
7917 goto error_disk_add
;
7920 *tofree
= *space_list
;
7921 /* calculate new size
7923 imsm_set_array_size(new_dev
);
7930 *space_list
= tofree
;
7934 dprintf("Error: imsm: Cannot find disk.\n");
7938 static int apply_update_activate_spare(struct imsm_update_activate_spare
*u
,
7939 struct intel_super
*super
,
7940 struct active_array
*active_array
)
7942 struct imsm_super
*mpb
= super
->anchor
;
7943 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
7944 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7945 struct imsm_map
*migr_map
;
7946 struct active_array
*a
;
7947 struct imsm_disk
*disk
;
7954 int second_map_created
= 0;
7956 for (; u
; u
= u
->next
) {
7957 victim
= get_imsm_disk_idx(dev
, u
->slot
, MAP_X
);
7962 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7967 fprintf(stderr
, "error: imsm_activate_spare passed "
7968 "an unknown disk (index: %d)\n",
7973 /* count failures (excluding rebuilds and the victim)
7974 * to determine map[0] state
7977 for (i
= 0; i
< map
->num_members
; i
++) {
7980 disk
= get_imsm_disk(super
,
7981 get_imsm_disk_idx(dev
, i
, MAP_X
));
7982 if (!disk
|| is_failed(disk
))
7986 /* adding a pristine spare, assign a new index */
7987 if (dl
->index
< 0) {
7988 dl
->index
= super
->anchor
->num_disks
;
7989 super
->anchor
->num_disks
++;
7992 disk
->status
|= CONFIGURED_DISK
;
7993 disk
->status
&= ~SPARE_DISK
;
7996 to_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7997 if (!second_map_created
) {
7998 second_map_created
= 1;
7999 map
->map_state
= IMSM_T_STATE_DEGRADED
;
8000 migrate(dev
, super
, to_state
, MIGR_REBUILD
);
8002 map
->map_state
= to_state
;
8003 migr_map
= get_imsm_map(dev
, MAP_1
);
8004 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
8005 set_imsm_ord_tbl_ent(migr_map
, u
->slot
,
8006 dl
->index
| IMSM_ORD_REBUILD
);
8008 /* update the family_num to mark a new container
8009 * generation, being careful to record the existing
8010 * family_num in orig_family_num to clean up after
8011 * earlier mdadm versions that neglected to set it.
8013 if (mpb
->orig_family_num
== 0)
8014 mpb
->orig_family_num
= mpb
->family_num
;
8015 mpb
->family_num
+= super
->random
;
8017 /* count arrays using the victim in the metadata */
8019 for (a
= active_array
; a
; a
= a
->next
) {
8020 dev
= get_imsm_dev(super
, a
->info
.container_member
);
8021 map
= get_imsm_map(dev
, MAP_0
);
8023 if (get_imsm_disk_slot(map
, victim
) >= 0)
8027 /* delete the victim if it is no longer being
8033 /* We know that 'manager' isn't touching anything,
8034 * so it is safe to delete
8036 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
8037 if ((*dlp
)->index
== victim
)
8040 /* victim may be on the missing list */
8042 for (dlp
= &super
->missing
; *dlp
;
8043 dlp
= &(*dlp
)->next
)
8044 if ((*dlp
)->index
== victim
)
8046 imsm_delete(super
, dlp
, victim
);
8053 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
8054 struct intel_super
*super
,
8057 struct dl
*new_disk
;
8058 struct intel_dev
*id
;
8060 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
8061 int disk_count
= u
->old_raid_disks
;
8062 void **tofree
= NULL
;
8063 int devices_to_reshape
= 1;
8064 struct imsm_super
*mpb
= super
->anchor
;
8066 unsigned int dev_id
;
8068 dprintf("imsm: apply_reshape_container_disks_update()\n");
8070 /* enable spares to use in array */
8071 for (i
= 0; i
< delta_disks
; i
++) {
8072 new_disk
= get_disk_super(super
,
8073 major(u
->new_disks
[i
]),
8074 minor(u
->new_disks
[i
]));
8075 dprintf("imsm: new disk for reshape is: %i:%i "
8076 "(%p, index = %i)\n",
8077 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
8078 new_disk
, new_disk
->index
);
8079 if ((new_disk
== NULL
) ||
8080 ((new_disk
->index
>= 0) &&
8081 (new_disk
->index
< u
->old_raid_disks
)))
8082 goto update_reshape_exit
;
8083 new_disk
->index
= disk_count
++;
8084 /* slot to fill in autolayout
8086 new_disk
->raiddisk
= new_disk
->index
;
8087 new_disk
->disk
.status
|=
8089 new_disk
->disk
.status
&= ~SPARE_DISK
;
8092 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
8093 mpb
->num_raid_devs
);
8094 /* manage changes in volume
8096 for (dev_id
= 0; dev_id
< mpb
->num_raid_devs
; dev_id
++) {
8097 void **sp
= *space_list
;
8098 struct imsm_dev
*newdev
;
8099 struct imsm_map
*newmap
, *oldmap
;
8101 for (id
= super
->devlist
; id
; id
= id
->next
) {
8102 if (id
->index
== dev_id
)
8111 /* Copy the dev, but not (all of) the map */
8112 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
8113 oldmap
= get_imsm_map(id
->dev
, MAP_0
);
8114 newmap
= get_imsm_map(newdev
, MAP_0
);
8115 /* Copy the current map */
8116 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8117 /* update one device only
8119 if (devices_to_reshape
) {
8120 dprintf("imsm: modifying subdev: %i\n",
8122 devices_to_reshape
--;
8123 newdev
->vol
.migr_state
= 1;
8124 newdev
->vol
.curr_migr_unit
= 0;
8125 set_migr_type(newdev
, MIGR_GEN_MIGR
);
8126 newmap
->num_members
= u
->new_raid_disks
;
8127 for (i
= 0; i
< delta_disks
; i
++) {
8128 set_imsm_ord_tbl_ent(newmap
,
8129 u
->old_raid_disks
+ i
,
8130 u
->old_raid_disks
+ i
);
8132 /* New map is correct, now need to save old map
8134 newmap
= get_imsm_map(newdev
, MAP_1
);
8135 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8137 imsm_set_array_size(newdev
);
8140 sp
= (void **)id
->dev
;
8145 /* Clear migration record */
8146 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
8149 *space_list
= tofree
;
8152 update_reshape_exit
:
8157 static int apply_takeover_update(struct imsm_update_takeover
*u
,
8158 struct intel_super
*super
,
8161 struct imsm_dev
*dev
= NULL
;
8162 struct intel_dev
*dv
;
8163 struct imsm_dev
*dev_new
;
8164 struct imsm_map
*map
;
8168 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
8169 if (dv
->index
== (unsigned int)u
->subarray
) {
8177 map
= get_imsm_map(dev
, MAP_0
);
8179 if (u
->direction
== R10_TO_R0
) {
8180 /* Number of failed disks must be half of initial disk number */
8181 if (imsm_count_failed(super
, dev
, MAP_0
) !=
8182 (map
->num_members
/ 2))
8185 /* iterate through devices to mark removed disks as spare */
8186 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
8187 if (dm
->disk
.status
& FAILED_DISK
) {
8188 int idx
= dm
->index
;
8189 /* update indexes on the disk list */
8190 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
8191 the index values will end up being correct.... NB */
8192 for (du
= super
->disks
; du
; du
= du
->next
)
8193 if (du
->index
> idx
)
8195 /* mark as spare disk */
8200 map
->num_members
= map
->num_members
/ 2;
8201 map
->map_state
= IMSM_T_STATE_NORMAL
;
8202 map
->num_domains
= 1;
8203 map
->raid_level
= 0;
8204 map
->failed_disk_num
= -1;
8207 if (u
->direction
== R0_TO_R10
) {
8209 /* update slots in current disk list */
8210 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
8214 /* create new *missing* disks */
8215 for (i
= 0; i
< map
->num_members
; i
++) {
8216 space
= *space_list
;
8219 *space_list
= *space
;
8221 memcpy(du
, super
->disks
, sizeof(*du
));
8225 du
->index
= (i
* 2) + 1;
8226 sprintf((char *)du
->disk
.serial
,
8227 " MISSING_%d", du
->index
);
8228 sprintf((char *)du
->serial
,
8229 "MISSING_%d", du
->index
);
8230 du
->next
= super
->missing
;
8231 super
->missing
= du
;
8233 /* create new dev and map */
8234 space
= *space_list
;
8237 *space_list
= *space
;
8238 dev_new
= (void *)space
;
8239 memcpy(dev_new
, dev
, sizeof(*dev
));
8240 /* update new map */
8241 map
= get_imsm_map(dev_new
, MAP_0
);
8242 map
->num_members
= map
->num_members
* 2;
8243 map
->map_state
= IMSM_T_STATE_DEGRADED
;
8244 map
->num_domains
= 2;
8245 map
->raid_level
= 1;
8246 /* replace dev<->dev_new */
8249 /* update disk order table */
8250 for (du
= super
->disks
; du
; du
= du
->next
)
8252 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
8253 for (du
= super
->missing
; du
; du
= du
->next
)
8254 if (du
->index
>= 0) {
8255 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
8256 mark_missing(dv
->dev
, &du
->disk
, du
->index
);
8262 static void imsm_process_update(struct supertype
*st
,
8263 struct metadata_update
*update
)
8266 * crack open the metadata_update envelope to find the update record
8267 * update can be one of:
8268 * update_reshape_container_disks - all the arrays in the container
8269 * are being reshaped to have more devices. We need to mark
8270 * the arrays for general migration and convert selected spares
8271 * into active devices.
8272 * update_activate_spare - a spare device has replaced a failed
8273 * device in an array, update the disk_ord_tbl. If this disk is
8274 * present in all member arrays then also clear the SPARE_DISK
8276 * update_create_array
8278 * update_rename_array
8279 * update_add_remove_disk
8281 struct intel_super
*super
= st
->sb
;
8282 struct imsm_super
*mpb
;
8283 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
8285 /* update requires a larger buf but the allocation failed */
8286 if (super
->next_len
&& !super
->next_buf
) {
8287 super
->next_len
= 0;
8291 if (super
->next_buf
) {
8292 memcpy(super
->next_buf
, super
->buf
, super
->len
);
8294 super
->len
= super
->next_len
;
8295 super
->buf
= super
->next_buf
;
8297 super
->next_len
= 0;
8298 super
->next_buf
= NULL
;
8301 mpb
= super
->anchor
;
8304 case update_general_migration_checkpoint
: {
8305 struct intel_dev
*id
;
8306 struct imsm_update_general_migration_checkpoint
*u
=
8307 (void *)update
->buf
;
8309 dprintf("imsm: process_update() "
8310 "for update_general_migration_checkpoint called\n");
8312 /* find device under general migration */
8313 for (id
= super
->devlist
; id
; id
= id
->next
) {
8314 if (is_gen_migration(id
->dev
)) {
8315 id
->dev
->vol
.curr_migr_unit
=
8316 __cpu_to_le32(u
->curr_migr_unit
);
8317 super
->updates_pending
++;
8322 case update_takeover
: {
8323 struct imsm_update_takeover
*u
= (void *)update
->buf
;
8324 if (apply_takeover_update(u
, super
, &update
->space_list
)) {
8325 imsm_update_version_info(super
);
8326 super
->updates_pending
++;
8331 case update_reshape_container_disks
: {
8332 struct imsm_update_reshape
*u
= (void *)update
->buf
;
8333 if (apply_reshape_container_disks_update(
8334 u
, super
, &update
->space_list
))
8335 super
->updates_pending
++;
8338 case update_reshape_migration
: {
8339 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
8340 if (apply_reshape_migration_update(
8341 u
, super
, &update
->space_list
))
8342 super
->updates_pending
++;
8345 case update_activate_spare
: {
8346 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
8347 if (apply_update_activate_spare(u
, super
, st
->arrays
))
8348 super
->updates_pending
++;
8351 case update_create_array
: {
8352 /* someone wants to create a new array, we need to be aware of
8353 * a few races/collisions:
8354 * 1/ 'Create' called by two separate instances of mdadm
8355 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
8356 * devices that have since been assimilated via
8358 * In the event this update can not be carried out mdadm will
8359 * (FIX ME) notice that its update did not take hold.
8361 struct imsm_update_create_array
*u
= (void *) update
->buf
;
8362 struct intel_dev
*dv
;
8363 struct imsm_dev
*dev
;
8364 struct imsm_map
*map
, *new_map
;
8365 unsigned long long start
, end
;
8366 unsigned long long new_start
, new_end
;
8368 struct disk_info
*inf
;
8371 /* handle racing creates: first come first serve */
8372 if (u
->dev_idx
< mpb
->num_raid_devs
) {
8373 dprintf("%s: subarray %d already defined\n",
8374 __func__
, u
->dev_idx
);
8378 /* check update is next in sequence */
8379 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
8380 dprintf("%s: can not create array %d expected index %d\n",
8381 __func__
, u
->dev_idx
, mpb
->num_raid_devs
);
8385 new_map
= get_imsm_map(&u
->dev
, MAP_0
);
8386 new_start
= pba_of_lba0(new_map
);
8387 new_end
= new_start
+ blocks_per_member(new_map
);
8388 inf
= get_disk_info(u
);
8390 /* handle activate_spare versus create race:
8391 * check to make sure that overlapping arrays do not include
8394 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8395 dev
= get_imsm_dev(super
, i
);
8396 map
= get_imsm_map(dev
, MAP_0
);
8397 start
= pba_of_lba0(map
);
8398 end
= start
+ blocks_per_member(map
);
8399 if ((new_start
>= start
&& new_start
<= end
) ||
8400 (start
>= new_start
&& start
<= new_end
))
8405 if (disks_overlap(super
, i
, u
)) {
8406 dprintf("%s: arrays overlap\n", __func__
);
8411 /* check that prepare update was successful */
8412 if (!update
->space
) {
8413 dprintf("%s: prepare update failed\n", __func__
);
8417 /* check that all disks are still active before committing
8418 * changes. FIXME: could we instead handle this by creating a
8419 * degraded array? That's probably not what the user expects,
8420 * so better to drop this update on the floor.
8422 for (i
= 0; i
< new_map
->num_members
; i
++) {
8423 dl
= serial_to_dl(inf
[i
].serial
, super
);
8425 dprintf("%s: disk disappeared\n", __func__
);
8430 super
->updates_pending
++;
8432 /* convert spares to members and fixup ord_tbl */
8433 for (i
= 0; i
< new_map
->num_members
; i
++) {
8434 dl
= serial_to_dl(inf
[i
].serial
, super
);
8435 if (dl
->index
== -1) {
8436 dl
->index
= mpb
->num_disks
;
8438 dl
->disk
.status
|= CONFIGURED_DISK
;
8439 dl
->disk
.status
&= ~SPARE_DISK
;
8441 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
8446 update
->space
= NULL
;
8447 imsm_copy_dev(dev
, &u
->dev
);
8448 dv
->index
= u
->dev_idx
;
8449 dv
->next
= super
->devlist
;
8450 super
->devlist
= dv
;
8451 mpb
->num_raid_devs
++;
8453 imsm_update_version_info(super
);
8456 /* mdmon knows how to release update->space, but not
8457 * ((struct intel_dev *) update->space)->dev
8459 if (update
->space
) {
8465 case update_kill_array
: {
8466 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
8467 int victim
= u
->dev_idx
;
8468 struct active_array
*a
;
8469 struct intel_dev
**dp
;
8470 struct imsm_dev
*dev
;
8472 /* sanity check that we are not affecting the uuid of
8473 * active arrays, or deleting an active array
8475 * FIXME when immutable ids are available, but note that
8476 * we'll also need to fixup the invalidated/active
8477 * subarray indexes in mdstat
8479 for (a
= st
->arrays
; a
; a
= a
->next
)
8480 if (a
->info
.container_member
>= victim
)
8482 /* by definition if mdmon is running at least one array
8483 * is active in the container, so checking
8484 * mpb->num_raid_devs is just extra paranoia
8486 dev
= get_imsm_dev(super
, victim
);
8487 if (a
|| !dev
|| mpb
->num_raid_devs
== 1) {
8488 dprintf("failed to delete subarray-%d\n", victim
);
8492 for (dp
= &super
->devlist
; *dp
;)
8493 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
8496 if ((*dp
)->index
> (unsigned)victim
)
8500 mpb
->num_raid_devs
--;
8501 super
->updates_pending
++;
8504 case update_rename_array
: {
8505 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
8506 char name
[MAX_RAID_SERIAL_LEN
+1];
8507 int target
= u
->dev_idx
;
8508 struct active_array
*a
;
8509 struct imsm_dev
*dev
;
8511 /* sanity check that we are not affecting the uuid of
8514 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
8515 name
[MAX_RAID_SERIAL_LEN
] = '\0';
8516 for (a
= st
->arrays
; a
; a
= a
->next
)
8517 if (a
->info
.container_member
== target
)
8519 dev
= get_imsm_dev(super
, u
->dev_idx
);
8520 if (a
|| !dev
|| !check_name(super
, name
, 1)) {
8521 dprintf("failed to rename subarray-%d\n", target
);
8525 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
8526 super
->updates_pending
++;
8529 case update_add_remove_disk
: {
8530 /* we may be able to repair some arrays if disks are
8531 * being added, check teh status of add_remove_disk
8532 * if discs has been added.
8534 if (add_remove_disk_update(super
)) {
8535 struct active_array
*a
;
8537 super
->updates_pending
++;
8538 for (a
= st
->arrays
; a
; a
= a
->next
)
8539 a
->check_degraded
= 1;
8544 fprintf(stderr
, "error: unsuported process update type:"
8545 "(type: %d)\n", type
);
8549 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
);
8551 static void imsm_prepare_update(struct supertype
*st
,
8552 struct metadata_update
*update
)
8555 * Allocate space to hold new disk entries, raid-device entries or a new
8556 * mpb if necessary. The manager synchronously waits for updates to
8557 * complete in the monitor, so new mpb buffers allocated here can be
8558 * integrated by the monitor thread without worrying about live pointers
8559 * in the manager thread.
8561 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
8562 struct intel_super
*super
= st
->sb
;
8563 struct imsm_super
*mpb
= super
->anchor
;
8568 case update_general_migration_checkpoint
:
8569 dprintf("imsm: prepare_update() "
8570 "for update_general_migration_checkpoint called\n");
8572 case update_takeover
: {
8573 struct imsm_update_takeover
*u
= (void *)update
->buf
;
8574 if (u
->direction
== R0_TO_R10
) {
8575 void **tail
= (void **)&update
->space_list
;
8576 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subarray
);
8577 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8578 int num_members
= map
->num_members
;
8582 /* allocate memory for added disks */
8583 for (i
= 0; i
< num_members
; i
++) {
8584 size
= sizeof(struct dl
);
8585 space
= malloc(size
);
8594 /* allocate memory for new device */
8595 size
= sizeof_imsm_dev(super
->devlist
->dev
, 0) +
8596 (num_members
* sizeof(__u32
));
8597 space
= malloc(size
);
8606 len
= disks_to_mpb_size(num_members
* 2);
8608 /* if allocation didn't success, free buffer */
8609 while (update
->space_list
) {
8610 void **sp
= update
->space_list
;
8611 update
->space_list
= *sp
;
8619 case update_reshape_container_disks
: {
8620 /* Every raid device in the container is about to
8621 * gain some more devices, and we will enter a
8623 * So each 'imsm_map' will be bigger, and the imsm_vol
8624 * will now hold 2 of them.
8625 * Thus we need new 'struct imsm_dev' allocations sized
8626 * as sizeof_imsm_dev but with more devices in both maps.
8628 struct imsm_update_reshape
*u
= (void *)update
->buf
;
8629 struct intel_dev
*dl
;
8630 void **space_tail
= (void**)&update
->space_list
;
8632 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
8634 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
8635 int size
= sizeof_imsm_dev(dl
->dev
, 1);
8637 if (u
->new_raid_disks
> u
->old_raid_disks
)
8638 size
+= sizeof(__u32
)*2*
8639 (u
->new_raid_disks
- u
->old_raid_disks
);
8648 len
= disks_to_mpb_size(u
->new_raid_disks
);
8649 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
8652 case update_reshape_migration
: {
8653 /* for migration level 0->5 we need to add disks
8654 * so the same as for container operation we will copy
8655 * device to the bigger location.
8656 * in memory prepared device and new disk area are prepared
8657 * for usage in process update
8659 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
8660 struct intel_dev
*id
;
8661 void **space_tail
= (void **)&update
->space_list
;
8664 int current_level
= -1;
8666 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
8668 /* add space for bigger array in update
8670 for (id
= super
->devlist
; id
; id
= id
->next
) {
8671 if (id
->index
== (unsigned)u
->subdev
) {
8672 size
= sizeof_imsm_dev(id
->dev
, 1);
8673 if (u
->new_raid_disks
> u
->old_raid_disks
)
8674 size
+= sizeof(__u32
)*2*
8675 (u
->new_raid_disks
- u
->old_raid_disks
);
8685 if (update
->space_list
== NULL
)
8688 /* add space for disk in update
8690 size
= sizeof(struct dl
);
8693 free(update
->space_list
);
8694 update
->space_list
= NULL
;
8701 /* add spare device to update
8703 for (id
= super
->devlist
; id
; id
= id
->next
)
8704 if (id
->index
== (unsigned)u
->subdev
) {
8705 struct imsm_dev
*dev
;
8706 struct imsm_map
*map
;
8708 dev
= get_imsm_dev(super
, u
->subdev
);
8709 map
= get_imsm_map(dev
, MAP_0
);
8710 current_level
= map
->raid_level
;
8713 if ((u
->new_level
== 5) && (u
->new_level
!= current_level
)) {
8714 struct mdinfo
*spares
;
8716 spares
= get_spares_for_grow(st
);
8724 makedev(dev
->disk
.major
,
8726 dl
= get_disk_super(super
,
8729 dl
->index
= u
->old_raid_disks
;
8735 len
= disks_to_mpb_size(u
->new_raid_disks
);
8736 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
8739 case update_create_array
: {
8740 struct imsm_update_create_array
*u
= (void *) update
->buf
;
8741 struct intel_dev
*dv
;
8742 struct imsm_dev
*dev
= &u
->dev
;
8743 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8745 struct disk_info
*inf
;
8749 inf
= get_disk_info(u
);
8750 len
= sizeof_imsm_dev(dev
, 1);
8751 /* allocate a new super->devlist entry */
8752 dv
= malloc(sizeof(*dv
));
8754 dv
->dev
= malloc(len
);
8759 update
->space
= NULL
;
8763 /* count how many spares will be converted to members */
8764 for (i
= 0; i
< map
->num_members
; i
++) {
8765 dl
= serial_to_dl(inf
[i
].serial
, super
);
8767 /* hmm maybe it failed?, nothing we can do about
8772 if (count_memberships(dl
, super
) == 0)
8775 len
+= activate
* sizeof(struct imsm_disk
);
8782 /* check if we need a larger metadata buffer */
8783 if (super
->next_buf
)
8784 buf_len
= super
->next_len
;
8786 buf_len
= super
->len
;
8788 if (__le32_to_cpu(mpb
->mpb_size
) + len
> buf_len
) {
8789 /* ok we need a larger buf than what is currently allocated
8790 * if this allocation fails process_update will notice that
8791 * ->next_len is set and ->next_buf is NULL
8793 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + len
, 512);
8794 if (super
->next_buf
)
8795 free(super
->next_buf
);
8797 super
->next_len
= buf_len
;
8798 if (posix_memalign(&super
->next_buf
, 512, buf_len
) == 0)
8799 memset(super
->next_buf
, 0, buf_len
);
8801 super
->next_buf
= NULL
;
8805 /* must be called while manager is quiesced */
8806 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
8808 struct imsm_super
*mpb
= super
->anchor
;
8810 struct imsm_dev
*dev
;
8811 struct imsm_map
*map
;
8812 int i
, j
, num_members
;
8815 dprintf("%s: deleting device[%d] from imsm_super\n",
8818 /* shift all indexes down one */
8819 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
8820 if (iter
->index
> (int)index
)
8822 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
8823 if (iter
->index
> (int)index
)
8826 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8827 dev
= get_imsm_dev(super
, i
);
8828 map
= get_imsm_map(dev
, MAP_0
);
8829 num_members
= map
->num_members
;
8830 for (j
= 0; j
< num_members
; j
++) {
8831 /* update ord entries being careful not to propagate
8832 * ord-flags to the first map
8834 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_X
);
8836 if (ord_to_idx(ord
) <= index
)
8839 map
= get_imsm_map(dev
, MAP_0
);
8840 set_imsm_ord_tbl_ent(map
, j
, ord_to_idx(ord
- 1));
8841 map
= get_imsm_map(dev
, MAP_1
);
8843 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
8848 super
->updates_pending
++;
8850 struct dl
*dl
= *dlp
;
8852 *dlp
= (*dlp
)->next
;
8853 __free_imsm_disk(dl
);
8856 #endif /* MDASSEMBLE */
8858 static void close_targets(int *targets
, int new_disks
)
8865 for (i
= 0; i
< new_disks
; i
++) {
8866 if (targets
[i
] >= 0) {
8873 static int imsm_get_allowed_degradation(int level
, int raid_disks
,
8874 struct intel_super
*super
,
8875 struct imsm_dev
*dev
)
8881 struct imsm_map
*map
;
8884 ret_val
= raid_disks
/2;
8885 /* check map if all disks pairs not failed
8888 map
= get_imsm_map(dev
, MAP_0
);
8889 for (i
= 0; i
< ret_val
; i
++) {
8890 int degradation
= 0;
8891 if (get_imsm_disk(super
, i
) == NULL
)
8893 if (get_imsm_disk(super
, i
+ 1) == NULL
)
8895 if (degradation
== 2)
8898 map
= get_imsm_map(dev
, MAP_1
);
8899 /* if there is no second map
8900 * result can be returned
8904 /* check degradation in second map
8906 for (i
= 0; i
< ret_val
; i
++) {
8907 int degradation
= 0;
8908 if (get_imsm_disk(super
, i
) == NULL
)
8910 if (get_imsm_disk(super
, i
+ 1) == NULL
)
8912 if (degradation
== 2)
8927 /*******************************************************************************
8928 * Function: open_backup_targets
8929 * Description: Function opens file descriptors for all devices given in
8932 * info : general array info
8933 * raid_disks : number of disks
8934 * raid_fds : table of device's file descriptors
8935 * super : intel super for raid10 degradation check
8936 * dev : intel device for raid10 degradation check
8940 ******************************************************************************/
8941 int open_backup_targets(struct mdinfo
*info
, int raid_disks
, int *raid_fds
,
8942 struct intel_super
*super
, struct imsm_dev
*dev
)
8948 for (i
= 0; i
< raid_disks
; i
++)
8951 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
8954 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
)) {
8955 dprintf("disk is faulty!!\n");
8959 if ((sd
->disk
.raid_disk
>= raid_disks
) ||
8960 (sd
->disk
.raid_disk
< 0))
8963 dn
= map_dev(sd
->disk
.major
,
8965 raid_fds
[sd
->disk
.raid_disk
] = dev_open(dn
, O_RDWR
);
8966 if (raid_fds
[sd
->disk
.raid_disk
] < 0) {
8967 fprintf(stderr
, "cannot open component\n");
8972 /* check if maximum array degradation level is not exceeded
8974 if ((raid_disks
- opened
) >
8975 imsm_get_allowed_degradation(info
->new_level
,
8978 fprintf(stderr
, "Not enough disks can be opened.\n");
8979 close_targets(raid_fds
, raid_disks
);
8986 /*******************************************************************************
8987 * Function: init_migr_record_imsm
8988 * Description: Function inits imsm migration record
8990 * super : imsm internal array info
8991 * dev : device under migration
8992 * info : general array info to find the smallest device
8995 ******************************************************************************/
8996 void init_migr_record_imsm(struct supertype
*st
, struct imsm_dev
*dev
,
8997 struct mdinfo
*info
)
8999 struct intel_super
*super
= st
->sb
;
9000 struct migr_record
*migr_rec
= super
->migr_rec
;
9002 unsigned long long dsize
, dev_sectors
;
9003 long long unsigned min_dev_sectors
= -1LLU;
9007 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
9008 struct imsm_map
*map_src
= get_imsm_map(dev
, MAP_1
);
9009 unsigned long long num_migr_units
;
9010 unsigned long long array_blocks
;
9012 memset(migr_rec
, 0, sizeof(struct migr_record
));
9013 migr_rec
->family_num
= __cpu_to_le32(super
->anchor
->family_num
);
9015 /* only ascending reshape supported now */
9016 migr_rec
->ascending_migr
= __cpu_to_le32(1);
9018 migr_rec
->dest_depth_per_unit
= GEN_MIGR_AREA_SIZE
/
9019 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
9020 migr_rec
->dest_depth_per_unit
*=
9021 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
9022 new_data_disks
= imsm_num_data_members(dev
, MAP_0
);
9023 migr_rec
->blocks_per_unit
=
9024 __cpu_to_le32(migr_rec
->dest_depth_per_unit
* new_data_disks
);
9025 migr_rec
->dest_depth_per_unit
=
9026 __cpu_to_le32(migr_rec
->dest_depth_per_unit
);
9027 array_blocks
= info
->component_size
* new_data_disks
;
9029 array_blocks
/ __le32_to_cpu(migr_rec
->blocks_per_unit
);
9031 if (array_blocks
% __le32_to_cpu(migr_rec
->blocks_per_unit
))
9033 migr_rec
->num_migr_units
= __cpu_to_le32(num_migr_units
);
9035 migr_rec
->post_migr_vol_cap
= dev
->size_low
;
9036 migr_rec
->post_migr_vol_cap_hi
= dev
->size_high
;
9039 /* Find the smallest dev */
9040 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
9041 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
9042 fd
= dev_open(nm
, O_RDONLY
);
9045 get_dev_size(fd
, NULL
, &dsize
);
9046 dev_sectors
= dsize
/ 512;
9047 if (dev_sectors
< min_dev_sectors
)
9048 min_dev_sectors
= dev_sectors
;
9051 migr_rec
->ckpt_area_pba
= __cpu_to_le32(min_dev_sectors
-
9052 RAID_DISK_RESERVED_BLOCKS_IMSM_HI
);
9054 write_imsm_migr_rec(st
);
9059 /*******************************************************************************
9060 * Function: save_backup_imsm
9061 * Description: Function saves critical data stripes to Migration Copy Area
9062 * and updates the current migration unit status.
9063 * Use restore_stripes() to form a destination stripe,
9064 * and to write it to the Copy Area.
9066 * st : supertype information
9067 * dev : imsm device that backup is saved for
9068 * info : general array info
9069 * buf : input buffer
9070 * length : length of data to backup (blocks_per_unit)
9074 ******************************************************************************/
9075 int save_backup_imsm(struct supertype
*st
,
9076 struct imsm_dev
*dev
,
9077 struct mdinfo
*info
,
9082 struct intel_super
*super
= st
->sb
;
9083 unsigned long long *target_offsets
= NULL
;
9084 int *targets
= NULL
;
9086 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
9087 int new_disks
= map_dest
->num_members
;
9088 int dest_layout
= 0;
9090 unsigned long long start
;
9091 int data_disks
= imsm_num_data_members(dev
, MAP_0
);
9093 targets
= malloc(new_disks
* sizeof(int));
9097 for (i
= 0; i
< new_disks
; i
++)
9100 target_offsets
= malloc(new_disks
* sizeof(unsigned long long));
9101 if (!target_offsets
)
9104 start
= info
->reshape_progress
* 512;
9105 for (i
= 0; i
< new_disks
; i
++) {
9106 target_offsets
[i
] = (unsigned long long)
9107 __le32_to_cpu(super
->migr_rec
->ckpt_area_pba
) * 512;
9108 /* move back copy area adderss, it will be moved forward
9109 * in restore_stripes() using start input variable
9111 target_offsets
[i
] -= start
/data_disks
;
9114 if (open_backup_targets(info
, new_disks
, targets
,
9118 dest_layout
= imsm_level_to_layout(map_dest
->raid_level
);
9119 dest_chunk
= __le16_to_cpu(map_dest
->blocks_per_strip
) * 512;
9121 if (restore_stripes(targets
, /* list of dest devices */
9122 target_offsets
, /* migration record offsets */
9125 map_dest
->raid_level
,
9127 -1, /* source backup file descriptor */
9128 0, /* input buf offset
9129 * always 0 buf is already offseted */
9133 fprintf(stderr
, Name
": Error restoring stripes\n");
9141 close_targets(targets
, new_disks
);
9144 free(target_offsets
);
9149 /*******************************************************************************
9150 * Function: save_checkpoint_imsm
9151 * Description: Function called for current unit status update
9152 * in the migration record. It writes it to disk.
9154 * super : imsm internal array info
9155 * info : general array info
9159 * 2: failure, means no valid migration record
9160 * / no general migration in progress /
9161 ******************************************************************************/
9162 int save_checkpoint_imsm(struct supertype
*st
, struct mdinfo
*info
, int state
)
9164 struct intel_super
*super
= st
->sb
;
9165 unsigned long long blocks_per_unit
;
9166 unsigned long long curr_migr_unit
;
9168 if (load_imsm_migr_rec(super
, info
) != 0) {
9169 dprintf("imsm: ERROR: Cannot read migration record "
9170 "for checkpoint save.\n");
9174 blocks_per_unit
= __le32_to_cpu(super
->migr_rec
->blocks_per_unit
);
9175 if (blocks_per_unit
== 0) {
9176 dprintf("imsm: no migration in progress.\n");
9179 curr_migr_unit
= info
->reshape_progress
/ blocks_per_unit
;
9180 /* check if array is alligned to copy area
9181 * if it is not alligned, add one to current migration unit value
9182 * this can happend on array reshape finish only
9184 if (info
->reshape_progress
% blocks_per_unit
)
9187 super
->migr_rec
->curr_migr_unit
=
9188 __cpu_to_le32(curr_migr_unit
);
9189 super
->migr_rec
->rec_status
= __cpu_to_le32(state
);
9190 super
->migr_rec
->dest_1st_member_lba
=
9191 __cpu_to_le32(curr_migr_unit
*
9192 __le32_to_cpu(super
->migr_rec
->dest_depth_per_unit
));
9193 if (write_imsm_migr_rec(st
) < 0) {
9194 dprintf("imsm: Cannot write migration record "
9195 "outside backup area\n");
9202 /*******************************************************************************
9203 * Function: recover_backup_imsm
9204 * Description: Function recovers critical data from the Migration Copy Area
9205 * while assembling an array.
9207 * super : imsm internal array info
9208 * info : general array info
9210 * 0 : success (or there is no data to recover)
9212 ******************************************************************************/
9213 int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
)
9215 struct intel_super
*super
= st
->sb
;
9216 struct migr_record
*migr_rec
= super
->migr_rec
;
9217 struct imsm_map
*map_dest
= NULL
;
9218 struct intel_dev
*id
= NULL
;
9219 unsigned long long read_offset
;
9220 unsigned long long write_offset
;
9222 int *targets
= NULL
;
9223 int new_disks
, i
, err
;
9226 unsigned long curr_migr_unit
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
9227 unsigned long num_migr_units
= __le32_to_cpu(migr_rec
->num_migr_units
);
9229 int skipped_disks
= 0;
9231 err
= sysfs_get_str(info
, NULL
, "array_state", (char *)buffer
, 20);
9235 /* recover data only during assemblation */
9236 if (strncmp(buffer
, "inactive", 8) != 0)
9238 /* no data to recover */
9239 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
9241 if (curr_migr_unit
>= num_migr_units
)
9244 /* find device during reshape */
9245 for (id
= super
->devlist
; id
; id
= id
->next
)
9246 if (is_gen_migration(id
->dev
))
9251 map_dest
= get_imsm_map(id
->dev
, MAP_0
);
9252 new_disks
= map_dest
->num_members
;
9254 read_offset
= (unsigned long long)
9255 __le32_to_cpu(migr_rec
->ckpt_area_pba
) * 512;
9257 write_offset
= ((unsigned long long)
9258 __le32_to_cpu(migr_rec
->dest_1st_member_lba
) +
9259 pba_of_lba0(map_dest
)) * 512;
9261 unit_len
= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
9262 if (posix_memalign((void **)&buf
, 512, unit_len
) != 0)
9264 targets
= malloc(new_disks
* sizeof(int));
9268 if (open_backup_targets(info
, new_disks
, targets
, super
, id
->dev
)) {
9270 Name
": Cannot open some devices belonging to array.\n");
9274 for (i
= 0; i
< new_disks
; i
++) {
9275 if (targets
[i
] < 0) {
9279 if (lseek64(targets
[i
], read_offset
, SEEK_SET
) < 0) {
9281 Name
": Cannot seek to block: %s\n",
9286 if ((unsigned)read(targets
[i
], buf
, unit_len
) != unit_len
) {
9288 Name
": Cannot read copy area block: %s\n",
9293 if (lseek64(targets
[i
], write_offset
, SEEK_SET
) < 0) {
9295 Name
": Cannot seek to block: %s\n",
9300 if ((unsigned)write(targets
[i
], buf
, unit_len
) != unit_len
) {
9302 Name
": Cannot restore block: %s\n",
9309 if (skipped_disks
> imsm_get_allowed_degradation(info
->new_level
,
9314 Name
": Cannot restore data from backup."
9315 " Too many failed disks\n");
9319 if (save_checkpoint_imsm(st
, info
, UNIT_SRC_NORMAL
)) {
9320 /* ignore error == 2, this can mean end of reshape here
9322 dprintf("imsm: Cannot write checkpoint to "
9323 "migration record (UNIT_SRC_NORMAL) during restart\n");
9329 for (i
= 0; i
< new_disks
; i
++)
9338 static char disk_by_path
[] = "/dev/disk/by-path/";
9340 static const char *imsm_get_disk_controller_domain(const char *path
)
9342 char disk_path
[PATH_MAX
];
9346 strncpy(disk_path
, disk_by_path
, PATH_MAX
- 1);
9347 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
9348 if (stat(disk_path
, &st
) == 0) {
9349 struct sys_dev
* hba
;
9352 path
= devt_to_devpath(st
.st_rdev
);
9355 hba
= find_disk_attached_hba(-1, path
);
9356 if (hba
&& hba
->type
== SYS_DEV_SAS
)
9358 else if (hba
&& hba
->type
== SYS_DEV_SATA
)
9362 dprintf("path: %s hba: %s attached: %s\n",
9363 path
, (hba
) ? hba
->path
: "NULL", drv
);
9371 static int imsm_find_array_minor_by_subdev(int subdev
, int container
, int *minor
)
9373 char subdev_name
[20];
9374 struct mdstat_ent
*mdstat
;
9376 sprintf(subdev_name
, "%d", subdev
);
9377 mdstat
= mdstat_by_subdev(subdev_name
, container
);
9381 *minor
= mdstat
->devnum
;
9382 free_mdstat(mdstat
);
9386 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
9387 struct geo_params
*geo
,
9388 int *old_raid_disks
)
9390 /* currently we only support increasing the number of devices
9391 * for a container. This increases the number of device for each
9392 * member array. They must all be RAID0 or RAID5.
9395 struct mdinfo
*info
, *member
;
9396 int devices_that_can_grow
= 0;
9398 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): "
9399 "st->devnum = (%i)\n",
9402 if (geo
->size
!= -1 ||
9403 geo
->level
!= UnSet
||
9404 geo
->layout
!= UnSet
||
9405 geo
->chunksize
!= 0 ||
9406 geo
->raid_disks
== UnSet
) {
9407 dprintf("imsm: Container operation is allowed for "
9408 "raid disks number change only.\n");
9412 info
= container_content_imsm(st
, NULL
);
9413 for (member
= info
; member
; member
= member
->next
) {
9417 dprintf("imsm: checking device_num: %i\n",
9418 member
->container_member
);
9420 if (geo
->raid_disks
<= member
->array
.raid_disks
) {
9421 /* we work on container for Online Capacity Expansion
9422 * only so raid_disks has to grow
9424 dprintf("imsm: for container operation raid disks "
9425 "increase is required\n");
9429 if ((info
->array
.level
!= 0) &&
9430 (info
->array
.level
!= 5)) {
9431 /* we cannot use this container with other raid level
9433 dprintf("imsm: for container operation wrong"
9434 " raid level (%i) detected\n",
9438 /* check for platform support
9439 * for this raid level configuration
9441 struct intel_super
*super
= st
->sb
;
9442 if (!is_raid_level_supported(super
->orom
,
9443 member
->array
.level
,
9445 dprintf("platform does not support raid%d with"
9449 geo
->raid_disks
> 1 ? "s" : "");
9452 /* check if component size is aligned to chunk size
9454 if (info
->component_size
%
9455 (info
->array
.chunk_size
/512)) {
9456 dprintf("Component size is not aligned to "
9462 if (*old_raid_disks
&&
9463 info
->array
.raid_disks
!= *old_raid_disks
)
9465 *old_raid_disks
= info
->array
.raid_disks
;
9467 /* All raid5 and raid0 volumes in container
9468 * have to be ready for Online Capacity Expansion
9469 * so they need to be assembled. We have already
9470 * checked that no recovery etc is happening.
9472 result
= imsm_find_array_minor_by_subdev(member
->container_member
,
9476 dprintf("imsm: cannot find array\n");
9479 devices_that_can_grow
++;
9482 if (!member
&& devices_that_can_grow
)
9486 dprintf("\tContainer operation allowed\n");
9488 dprintf("\tError: %i\n", ret_val
);
9493 /* Function: get_spares_for_grow
9494 * Description: Allocates memory and creates list of spare devices
9495 * avaliable in container. Checks if spare drive size is acceptable.
9496 * Parameters: Pointer to the supertype structure
9497 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
9500 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
9502 unsigned long long min_size
= min_acceptable_spare_size_imsm(st
);
9503 return container_choose_spares(st
, min_size
, NULL
, NULL
, NULL
, 0);
9506 /******************************************************************************
9507 * function: imsm_create_metadata_update_for_reshape
9508 * Function creates update for whole IMSM container.
9510 ******************************************************************************/
9511 static int imsm_create_metadata_update_for_reshape(
9512 struct supertype
*st
,
9513 struct geo_params
*geo
,
9515 struct imsm_update_reshape
**updatep
)
9517 struct intel_super
*super
= st
->sb
;
9518 struct imsm_super
*mpb
= super
->anchor
;
9519 int update_memory_size
= 0;
9520 struct imsm_update_reshape
*u
= NULL
;
9521 struct mdinfo
*spares
= NULL
;
9523 int delta_disks
= 0;
9526 dprintf("imsm_update_metadata_for_reshape(enter) raid_disks = %i\n",
9529 delta_disks
= geo
->raid_disks
- old_raid_disks
;
9531 /* size of all update data without anchor */
9532 update_memory_size
= sizeof(struct imsm_update_reshape
);
9534 /* now add space for spare disks that we need to add. */
9535 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
9537 u
= calloc(1, update_memory_size
);
9540 "cannot get memory for imsm_update_reshape update\n");
9543 u
->type
= update_reshape_container_disks
;
9544 u
->old_raid_disks
= old_raid_disks
;
9545 u
->new_raid_disks
= geo
->raid_disks
;
9547 /* now get spare disks list
9549 spares
= get_spares_for_grow(st
);
9552 || delta_disks
> spares
->array
.spare_disks
) {
9553 fprintf(stderr
, Name
": imsm: ERROR: Cannot get spare devices "
9554 "for %s.\n", geo
->dev_name
);
9559 /* we have got spares
9560 * update disk list in imsm_disk list table in anchor
9562 dprintf("imsm: %i spares are available.\n\n",
9563 spares
->array
.spare_disks
);
9566 for (i
= 0; i
< delta_disks
; i
++) {
9571 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
9573 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
9574 dl
->index
= mpb
->num_disks
;
9584 dprintf("imsm: reshape update preparation :");
9585 if (i
== delta_disks
) {
9588 return update_memory_size
;
9591 dprintf(" Error\n");
9596 /******************************************************************************
9597 * function: imsm_create_metadata_update_for_migration()
9598 * Creates update for IMSM array.
9600 ******************************************************************************/
9601 static int imsm_create_metadata_update_for_migration(
9602 struct supertype
*st
,
9603 struct geo_params
*geo
,
9604 struct imsm_update_reshape_migration
**updatep
)
9606 struct intel_super
*super
= st
->sb
;
9607 int update_memory_size
= 0;
9608 struct imsm_update_reshape_migration
*u
= NULL
;
9609 struct imsm_dev
*dev
;
9610 int previous_level
= -1;
9612 dprintf("imsm_create_metadata_update_for_migration(enter)"
9613 " New Level = %i\n", geo
->level
);
9615 /* size of all update data without anchor */
9616 update_memory_size
= sizeof(struct imsm_update_reshape_migration
);
9618 u
= calloc(1, update_memory_size
);
9620 dprintf("error: cannot get memory for "
9621 "imsm_create_metadata_update_for_migration\n");
9624 u
->type
= update_reshape_migration
;
9625 u
->subdev
= super
->current_vol
;
9626 u
->new_level
= geo
->level
;
9627 u
->new_layout
= geo
->layout
;
9628 u
->new_raid_disks
= u
->old_raid_disks
= geo
->raid_disks
;
9629 u
->new_disks
[0] = -1;
9630 u
->new_chunksize
= -1;
9632 dev
= get_imsm_dev(super
, u
->subdev
);
9634 struct imsm_map
*map
;
9636 map
= get_imsm_map(dev
, MAP_0
);
9638 int current_chunk_size
=
9639 __le16_to_cpu(map
->blocks_per_strip
) / 2;
9641 if (geo
->chunksize
!= current_chunk_size
) {
9642 u
->new_chunksize
= geo
->chunksize
/ 1024;
9644 "chunk size change from %i to %i\n",
9645 current_chunk_size
, u
->new_chunksize
);
9647 previous_level
= map
->raid_level
;
9650 if ((geo
->level
== 5) && (previous_level
== 0)) {
9651 struct mdinfo
*spares
= NULL
;
9653 u
->new_raid_disks
++;
9654 spares
= get_spares_for_grow(st
);
9655 if ((spares
== NULL
) || (spares
->array
.spare_disks
< 1)) {
9658 update_memory_size
= 0;
9659 dprintf("error: cannot get spare device "
9660 "for requested migration");
9665 dprintf("imsm: reshape update preparation : OK\n");
9668 return update_memory_size
;
9671 static void imsm_update_metadata_locally(struct supertype
*st
,
9674 struct metadata_update mu
;
9679 mu
.space_list
= NULL
;
9681 imsm_prepare_update(st
, &mu
);
9682 imsm_process_update(st
, &mu
);
9684 while (mu
.space_list
) {
9685 void **space
= mu
.space_list
;
9686 mu
.space_list
= *space
;
9691 /***************************************************************************
9692 * Function: imsm_analyze_change
9693 * Description: Function analyze change for single volume
9694 * and validate if transition is supported
9695 * Parameters: Geometry parameters, supertype structure
9696 * Returns: Operation type code on success, -1 if fail
9697 ****************************************************************************/
9698 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
9699 struct geo_params
*geo
)
9705 /* number of added/removed disks in operation result */
9706 int devNumChange
= 0;
9707 /* imsm compatible layout value for array geometry verification */
9708 int imsm_layout
= -1;
9710 getinfo_super_imsm_volume(st
, &info
, NULL
);
9711 if ((geo
->level
!= info
.array
.level
) &&
9712 (geo
->level
>= 0) &&
9713 (geo
->level
!= UnSet
)) {
9714 switch (info
.array
.level
) {
9716 if (geo
->level
== 5) {
9717 change
= CH_MIGRATION
;
9718 if (geo
->layout
!= ALGORITHM_LEFT_ASYMMETRIC
) {
9720 Name
" Error. Requested Layout "
9721 "not supported (left-asymmetric layout "
9722 "is supported only)!\n");
9724 goto analyse_change_exit
;
9726 imsm_layout
= geo
->layout
;
9728 devNumChange
= 1; /* parity disk added */
9729 } else if (geo
->level
== 10) {
9730 change
= CH_TAKEOVER
;
9732 devNumChange
= 2; /* two mirrors added */
9733 imsm_layout
= 0x102; /* imsm supported layout */
9738 if (geo
->level
== 0) {
9739 change
= CH_TAKEOVER
;
9741 devNumChange
= -(geo
->raid_disks
/2);
9742 imsm_layout
= 0; /* imsm raid0 layout */
9748 Name
" Error. Level Migration from %d to %d "
9750 info
.array
.level
, geo
->level
);
9751 goto analyse_change_exit
;
9754 geo
->level
= info
.array
.level
;
9756 if ((geo
->layout
!= info
.array
.layout
)
9757 && ((geo
->layout
!= UnSet
) && (geo
->layout
!= -1))) {
9758 change
= CH_MIGRATION
;
9759 if ((info
.array
.layout
== 0)
9760 && (info
.array
.level
== 5)
9761 && (geo
->layout
== 5)) {
9762 /* reshape 5 -> 4 */
9763 } else if ((info
.array
.layout
== 5)
9764 && (info
.array
.level
== 5)
9765 && (geo
->layout
== 0)) {
9766 /* reshape 4 -> 5 */
9771 Name
" Error. Layout Migration from %d to %d "
9773 info
.array
.layout
, geo
->layout
);
9775 goto analyse_change_exit
;
9778 geo
->layout
= info
.array
.layout
;
9779 if (imsm_layout
== -1)
9780 imsm_layout
= info
.array
.layout
;
9783 if ((geo
->chunksize
> 0) && (geo
->chunksize
!= UnSet
)
9784 && (geo
->chunksize
!= info
.array
.chunk_size
))
9785 change
= CH_MIGRATION
;
9787 geo
->chunksize
= info
.array
.chunk_size
;
9789 chunk
= geo
->chunksize
/ 1024;
9790 if (!validate_geometry_imsm(st
,
9793 geo
->raid_disks
+ devNumChange
,
9800 struct intel_super
*super
= st
->sb
;
9801 struct imsm_super
*mpb
= super
->anchor
;
9803 if (mpb
->num_raid_devs
> 1) {
9805 Name
" Error. Cannot perform operation on %s"
9806 "- for this operation it MUST be single "
9807 "array in container\n",
9813 analyse_change_exit
:
9818 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
9820 struct intel_super
*super
= st
->sb
;
9821 struct imsm_update_takeover
*u
;
9823 u
= malloc(sizeof(struct imsm_update_takeover
));
9827 u
->type
= update_takeover
;
9828 u
->subarray
= super
->current_vol
;
9830 /* 10->0 transition */
9831 if (geo
->level
== 0)
9832 u
->direction
= R10_TO_R0
;
9834 /* 0->10 transition */
9835 if (geo
->level
== 10)
9836 u
->direction
= R0_TO_R10
;
9838 /* update metadata locally */
9839 imsm_update_metadata_locally(st
, u
,
9840 sizeof(struct imsm_update_takeover
));
9841 /* and possibly remotely */
9842 if (st
->update_tail
)
9843 append_metadata_update(st
, u
,
9844 sizeof(struct imsm_update_takeover
));
9851 static int imsm_reshape_super(struct supertype
*st
, long long size
, int level
,
9852 int layout
, int chunksize
, int raid_disks
,
9853 int delta_disks
, char *backup
, char *dev
,
9857 struct geo_params geo
;
9859 dprintf("imsm: reshape_super called.\n");
9861 memset(&geo
, 0, sizeof(struct geo_params
));
9864 geo
.dev_id
= st
->devnum
;
9867 geo
.layout
= layout
;
9868 geo
.chunksize
= chunksize
;
9869 geo
.raid_disks
= raid_disks
;
9870 if (delta_disks
!= UnSet
)
9871 geo
.raid_disks
+= delta_disks
;
9873 dprintf("\tfor level : %i\n", geo
.level
);
9874 dprintf("\tfor raid_disks : %i\n", geo
.raid_disks
);
9876 if (experimental() == 0)
9879 if (st
->container_dev
== st
->devnum
) {
9880 /* On container level we can only increase number of devices. */
9881 dprintf("imsm: info: Container operation\n");
9882 int old_raid_disks
= 0;
9884 if (imsm_reshape_is_allowed_on_container(
9885 st
, &geo
, &old_raid_disks
)) {
9886 struct imsm_update_reshape
*u
= NULL
;
9889 len
= imsm_create_metadata_update_for_reshape(
9890 st
, &geo
, old_raid_disks
, &u
);
9893 dprintf("imsm: Cannot prepare update\n");
9894 goto exit_imsm_reshape_super
;
9898 /* update metadata locally */
9899 imsm_update_metadata_locally(st
, u
, len
);
9900 /* and possibly remotely */
9901 if (st
->update_tail
)
9902 append_metadata_update(st
, u
, len
);
9907 fprintf(stderr
, Name
": (imsm) Operation "
9908 "is not allowed on this container\n");
9911 /* On volume level we support following operations
9912 * - takeover: raid10 -> raid0; raid0 -> raid10
9913 * - chunk size migration
9914 * - migration: raid5 -> raid0; raid0 -> raid5
9916 struct intel_super
*super
= st
->sb
;
9917 struct intel_dev
*dev
= super
->devlist
;
9919 dprintf("imsm: info: Volume operation\n");
9920 /* find requested device */
9922 if (imsm_find_array_minor_by_subdev(
9923 dev
->index
, st
->container_dev
, &devnum
) == 0
9924 && devnum
== geo
.dev_id
)
9929 fprintf(stderr
, Name
" Cannot find %s (%i) subarray\n",
9930 geo
.dev_name
, geo
.dev_id
);
9931 goto exit_imsm_reshape_super
;
9933 super
->current_vol
= dev
->index
;
9934 change
= imsm_analyze_change(st
, &geo
);
9937 ret_val
= imsm_takeover(st
, &geo
);
9939 case CH_MIGRATION
: {
9940 struct imsm_update_reshape_migration
*u
= NULL
;
9942 imsm_create_metadata_update_for_migration(
9946 "Cannot prepare update\n");
9950 /* update metadata locally */
9951 imsm_update_metadata_locally(st
, u
, len
);
9952 /* and possibly remotely */
9953 if (st
->update_tail
)
9954 append_metadata_update(st
, u
, len
);
9964 exit_imsm_reshape_super
:
9965 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
9969 /*******************************************************************************
9970 * Function: wait_for_reshape_imsm
9971 * Description: Function writes new sync_max value and waits until
9972 * reshape process reach new position
9974 * sra : general array info
9975 * ndata : number of disks in new array's layout
9978 * 1 : there is no reshape in progress,
9980 ******************************************************************************/
9981 int wait_for_reshape_imsm(struct mdinfo
*sra
, int ndata
)
9983 int fd
= sysfs_get_fd(sra
, NULL
, "reshape_position");
9984 unsigned long long completed
;
9985 /* to_complete : new sync_max position */
9986 unsigned long long to_complete
= sra
->reshape_progress
;
9987 unsigned long long position_to_set
= to_complete
/ ndata
;
9990 dprintf("imsm: wait_for_reshape_imsm() "
9991 "cannot open reshape_position\n");
9995 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
9996 dprintf("imsm: wait_for_reshape_imsm() "
9997 "cannot read reshape_position (no reshape in progres)\n");
10002 if (completed
> to_complete
) {
10003 dprintf("imsm: wait_for_reshape_imsm() "
10004 "wrong next position to set %llu (%llu)\n",
10005 to_complete
, completed
);
10009 dprintf("Position set: %llu\n", position_to_set
);
10010 if (sysfs_set_num(sra
, NULL
, "sync_max",
10011 position_to_set
) != 0) {
10012 dprintf("imsm: wait_for_reshape_imsm() "
10013 "cannot set reshape position to %llu\n",
10024 select(fd
+1, &rfds
, NULL
, NULL
, NULL
);
10025 if (sysfs_get_str(sra
, NULL
, "sync_action",
10027 strncmp(action
, "reshape", 7) != 0)
10029 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
10030 dprintf("imsm: wait_for_reshape_imsm() "
10031 "cannot read reshape_position (in loop)\n");
10035 } while (completed
< to_complete
);
10041 /*******************************************************************************
10042 * Function: check_degradation_change
10043 * Description: Check that array hasn't become failed.
10045 * info : for sysfs access
10046 * sources : source disks descriptors
10047 * degraded: previous degradation level
10049 * degradation level
10050 ******************************************************************************/
10051 int check_degradation_change(struct mdinfo
*info
,
10055 unsigned long long new_degraded
;
10056 sysfs_get_ll(info
, NULL
, "degraded", &new_degraded
);
10057 if (new_degraded
!= (unsigned long long)degraded
) {
10058 /* check each device to ensure it is still working */
10061 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
10062 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
10064 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
10066 if (sysfs_get_str(info
,
10067 sd
, "state", sbuf
, 20) < 0 ||
10068 strstr(sbuf
, "faulty") ||
10069 strstr(sbuf
, "in_sync") == NULL
) {
10070 /* this device is dead */
10071 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
10072 if (sd
->disk
.raid_disk
>= 0 &&
10073 sources
[sd
->disk
.raid_disk
] >= 0) {
10075 sd
->disk
.raid_disk
]);
10076 sources
[sd
->disk
.raid_disk
] =
10085 return new_degraded
;
10088 /*******************************************************************************
10089 * Function: imsm_manage_reshape
10090 * Description: Function finds array under reshape and it manages reshape
10091 * process. It creates stripes backups (if required) and sets
10094 * afd : Backup handle (nattive) - not used
10095 * sra : general array info
10096 * reshape : reshape parameters - not used
10097 * st : supertype structure
10098 * blocks : size of critical section [blocks]
10099 * fds : table of source device descriptor
10100 * offsets : start of array (offest per devices)
10102 * destfd : table of destination device descriptor
10103 * destoffsets : table of destination offsets (per device)
10105 * 1 : success, reshape is done
10107 ******************************************************************************/
10108 static int imsm_manage_reshape(
10109 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
10110 struct supertype
*st
, unsigned long backup_blocks
,
10111 int *fds
, unsigned long long *offsets
,
10112 int dests
, int *destfd
, unsigned long long *destoffsets
)
10115 struct intel_super
*super
= st
->sb
;
10116 struct intel_dev
*dv
= NULL
;
10117 struct imsm_dev
*dev
= NULL
;
10118 struct imsm_map
*map_src
;
10119 int migr_vol_qan
= 0;
10120 int ndata
, odata
; /* [bytes] */
10121 int chunk
; /* [bytes] */
10122 struct migr_record
*migr_rec
;
10124 unsigned int buf_size
; /* [bytes] */
10125 unsigned long long max_position
; /* array size [bytes] */
10126 unsigned long long next_step
; /* [blocks]/[bytes] */
10127 unsigned long long old_data_stripe_length
;
10128 unsigned long long start_src
; /* [bytes] */
10129 unsigned long long start
; /* [bytes] */
10130 unsigned long long start_buf_shift
; /* [bytes] */
10132 int source_layout
= 0;
10134 if (!fds
|| !offsets
|| !sra
)
10137 /* Find volume during the reshape */
10138 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
10139 if (dv
->dev
->vol
.migr_type
== MIGR_GEN_MIGR
10140 && dv
->dev
->vol
.migr_state
== 1) {
10145 /* Only one volume can migrate at the same time */
10146 if (migr_vol_qan
!= 1) {
10147 fprintf(stderr
, Name
" : %s", migr_vol_qan
?
10148 "Number of migrating volumes greater than 1\n" :
10149 "There is no volume during migrationg\n");
10153 map_src
= get_imsm_map(dev
, MAP_1
);
10154 if (map_src
== NULL
)
10157 ndata
= imsm_num_data_members(dev
, MAP_0
);
10158 odata
= imsm_num_data_members(dev
, MAP_1
);
10160 chunk
= __le16_to_cpu(map_src
->blocks_per_strip
) * 512;
10161 old_data_stripe_length
= odata
* chunk
;
10163 migr_rec
= super
->migr_rec
;
10165 /* initialize migration record for start condition */
10166 if (sra
->reshape_progress
== 0)
10167 init_migr_record_imsm(st
, dev
, sra
);
10169 if (__le32_to_cpu(migr_rec
->rec_status
) != UNIT_SRC_NORMAL
) {
10170 dprintf("imsm: cannot restart migration when data "
10171 "are present in copy area.\n");
10174 /* Save checkpoint to update migration record for current
10175 * reshape position (in md). It can be farther than current
10176 * reshape position in metadata.
10178 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
10179 /* ignore error == 2, this can mean end of reshape here
10181 dprintf("imsm: Cannot write checkpoint to "
10182 "migration record (UNIT_SRC_NORMAL, "
10183 "initial save)\n");
10188 /* size for data */
10189 buf_size
= __le32_to_cpu(migr_rec
->blocks_per_unit
) * 512;
10190 /* extend buffer size for parity disk */
10191 buf_size
+= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
10192 /* add space for stripe aligment */
10193 buf_size
+= old_data_stripe_length
;
10194 if (posix_memalign((void **)&buf
, 4096, buf_size
)) {
10195 dprintf("imsm: Cannot allocate checpoint buffer\n");
10199 max_position
= sra
->component_size
* ndata
;
10200 source_layout
= imsm_level_to_layout(map_src
->raid_level
);
10202 while (__le32_to_cpu(migr_rec
->curr_migr_unit
) <
10203 __le32_to_cpu(migr_rec
->num_migr_units
)) {
10204 /* current reshape position [blocks] */
10205 unsigned long long current_position
=
10206 __le32_to_cpu(migr_rec
->blocks_per_unit
)
10207 * __le32_to_cpu(migr_rec
->curr_migr_unit
);
10208 unsigned long long border
;
10210 /* Check that array hasn't become failed.
10212 degraded
= check_degradation_change(sra
, fds
, degraded
);
10213 if (degraded
> 1) {
10214 dprintf("imsm: Abort reshape due to degradation"
10215 " level (%i)\n", degraded
);
10219 next_step
= __le32_to_cpu(migr_rec
->blocks_per_unit
);
10221 if ((current_position
+ next_step
) > max_position
)
10222 next_step
= max_position
- current_position
;
10224 start
= current_position
* 512;
10226 /* allign reading start to old geometry */
10227 start_buf_shift
= start
% old_data_stripe_length
;
10228 start_src
= start
- start_buf_shift
;
10230 border
= (start_src
/ odata
) - (start
/ ndata
);
10232 if (border
<= __le32_to_cpu(migr_rec
->dest_depth_per_unit
)) {
10233 /* save critical stripes to buf
10234 * start - start address of current unit
10235 * to backup [bytes]
10236 * start_src - start address of current unit
10237 * to backup alligned to source array
10240 unsigned long long next_step_filler
= 0;
10241 unsigned long long copy_length
= next_step
* 512;
10243 /* allign copy area length to stripe in old geometry */
10244 next_step_filler
= ((copy_length
+ start_buf_shift
)
10245 % old_data_stripe_length
);
10246 if (next_step_filler
)
10247 next_step_filler
= (old_data_stripe_length
10248 - next_step_filler
);
10249 dprintf("save_stripes() parameters: start = %llu,"
10250 "\tstart_src = %llu,\tnext_step*512 = %llu,"
10251 "\tstart_in_buf_shift = %llu,"
10252 "\tnext_step_filler = %llu\n",
10253 start
, start_src
, copy_length
,
10254 start_buf_shift
, next_step_filler
);
10256 if (save_stripes(fds
, offsets
, map_src
->num_members
,
10257 chunk
, map_src
->raid_level
,
10258 source_layout
, 0, NULL
, start_src
,
10260 next_step_filler
+ start_buf_shift
,
10262 dprintf("imsm: Cannot save stripes"
10266 /* Convert data to destination format and store it
10267 * in backup general migration area
10269 if (save_backup_imsm(st
, dev
, sra
,
10270 buf
+ start_buf_shift
, copy_length
)) {
10271 dprintf("imsm: Cannot save stripes to "
10272 "target devices\n");
10275 if (save_checkpoint_imsm(st
, sra
,
10276 UNIT_SRC_IN_CP_AREA
)) {
10277 dprintf("imsm: Cannot write checkpoint to "
10278 "migration record (UNIT_SRC_IN_CP_AREA)\n");
10282 /* set next step to use whole border area */
10283 border
/= next_step
;
10285 next_step
*= border
;
10287 /* When data backed up, checkpoint stored,
10288 * kick the kernel to reshape unit of data
10290 next_step
= next_step
+ sra
->reshape_progress
;
10291 /* limit next step to array max position */
10292 if (next_step
> max_position
)
10293 next_step
= max_position
;
10294 sysfs_set_num(sra
, NULL
, "suspend_lo", sra
->reshape_progress
);
10295 sysfs_set_num(sra
, NULL
, "suspend_hi", next_step
);
10296 sra
->reshape_progress
= next_step
;
10298 /* wait until reshape finish */
10299 if (wait_for_reshape_imsm(sra
, ndata
) < 0) {
10300 dprintf("wait_for_reshape_imsm returned error!\n");
10304 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
10305 /* ignore error == 2, this can mean end of reshape here
10307 dprintf("imsm: Cannot write checkpoint to "
10308 "migration record (UNIT_SRC_NORMAL)\n");
10314 /* return '1' if done */
10318 abort_reshape(sra
);
10322 #endif /* MDASSEMBLE */
10324 struct superswitch super_imsm
= {
10326 .examine_super
= examine_super_imsm
,
10327 .brief_examine_super
= brief_examine_super_imsm
,
10328 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
10329 .export_examine_super
= export_examine_super_imsm
,
10330 .detail_super
= detail_super_imsm
,
10331 .brief_detail_super
= brief_detail_super_imsm
,
10332 .write_init_super
= write_init_super_imsm
,
10333 .validate_geometry
= validate_geometry_imsm
,
10334 .add_to_super
= add_to_super_imsm
,
10335 .remove_from_super
= remove_from_super_imsm
,
10336 .detail_platform
= detail_platform_imsm
,
10337 .kill_subarray
= kill_subarray_imsm
,
10338 .update_subarray
= update_subarray_imsm
,
10339 .load_container
= load_container_imsm
,
10340 .default_geometry
= default_geometry_imsm
,
10341 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
10342 .reshape_super
= imsm_reshape_super
,
10343 .manage_reshape
= imsm_manage_reshape
,
10344 .recover_backup
= recover_backup_imsm
,
10346 .match_home
= match_home_imsm
,
10347 .uuid_from_super
= uuid_from_super_imsm
,
10348 .getinfo_super
= getinfo_super_imsm
,
10349 .getinfo_super_disks
= getinfo_super_disks_imsm
,
10350 .update_super
= update_super_imsm
,
10352 .avail_size
= avail_size_imsm
,
10353 .min_acceptable_spare_size
= min_acceptable_spare_size_imsm
,
10355 .compare_super
= compare_super_imsm
,
10357 .load_super
= load_super_imsm
,
10358 .init_super
= init_super_imsm
,
10359 .store_super
= store_super_imsm
,
10360 .free_super
= free_super_imsm
,
10361 .match_metadata_desc
= match_metadata_desc_imsm
,
10362 .container_content
= container_content_imsm
,
10370 .open_new
= imsm_open_new
,
10371 .set_array_state
= imsm_set_array_state
,
10372 .set_disk
= imsm_set_disk
,
10373 .sync_metadata
= imsm_sync_metadata
,
10374 .activate_spare
= imsm_activate_spare
,
10375 .process_update
= imsm_process_update
,
10376 .prepare_update
= imsm_prepare_update
,
10377 #endif /* MDASSEMBLE */