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 && pr_err(fmt, ##arg))
300 static __u8
migr_type(struct imsm_dev
*dev
)
302 if (dev
->vol
.migr_type
== MIGR_VERIFY
&&
303 dev
->status
& DEV_VERIFY_AND_FIX
)
306 return dev
->vol
.migr_type
;
309 static void set_migr_type(struct imsm_dev
*dev
, __u8 migr_type
)
311 /* for compatibility with older oroms convert MIGR_REPAIR, into
312 * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
314 if (migr_type
== MIGR_REPAIR
) {
315 dev
->vol
.migr_type
= MIGR_VERIFY
;
316 dev
->status
|= DEV_VERIFY_AND_FIX
;
318 dev
->vol
.migr_type
= migr_type
;
319 dev
->status
&= ~DEV_VERIFY_AND_FIX
;
323 static unsigned int sector_count(__u32 bytes
)
325 return ROUND_UP(bytes
, 512) / 512;
328 static unsigned int mpb_sectors(struct imsm_super
*mpb
)
330 return sector_count(__le32_to_cpu(mpb
->mpb_size
));
334 struct imsm_dev
*dev
;
335 struct intel_dev
*next
;
340 enum sys_dev_type type
;
343 struct intel_hba
*next
;
350 /* internal representation of IMSM metadata */
353 void *buf
; /* O_DIRECT buffer for reading/writing metadata */
354 struct imsm_super
*anchor
; /* immovable parameters */
357 void *migr_rec_buf
; /* buffer for I/O operations */
358 struct migr_record
*migr_rec
; /* migration record */
360 int clean_migration_record_by_mdmon
; /* when reshape is switched to next
361 array, it indicates that mdmon is allowed to clean migration
363 size_t len
; /* size of the 'buf' allocation */
364 void *next_buf
; /* for realloc'ing buf from the manager */
366 int updates_pending
; /* count of pending updates for mdmon */
367 int current_vol
; /* index of raid device undergoing creation */
368 unsigned long long create_offset
; /* common start for 'current_vol' */
369 __u32 random
; /* random data for seeding new family numbers */
370 struct intel_dev
*devlist
;
374 __u8 serial
[MAX_RAID_SERIAL_LEN
];
377 struct imsm_disk disk
;
380 struct extent
*e
; /* for determining freespace @ create */
381 int raiddisk
; /* slot to fill in autolayout */
383 } *disks
, *current_disk
;
384 struct dl
*disk_mgmt_list
; /* list of disks to add/remove while mdmon
386 struct dl
*missing
; /* disks removed while we weren't looking */
387 struct bbm_log
*bbm_log
;
388 struct intel_hba
*hba
; /* device path of the raid controller for this metadata */
389 const struct imsm_orom
*orom
; /* platform firmware support */
390 struct intel_super
*next
; /* (temp) list for disambiguating family_num */
394 struct imsm_disk disk
;
395 #define IMSM_UNKNOWN_OWNER (-1)
397 struct intel_disk
*next
;
401 unsigned long long start
, size
;
404 /* definitions of reshape process types */
405 enum imsm_reshape_type
{
411 /* definition of messages passed to imsm_process_update */
412 enum imsm_update_type
{
413 update_activate_spare
,
417 update_add_remove_disk
,
418 update_reshape_container_disks
,
419 update_reshape_migration
,
421 update_general_migration_checkpoint
,
425 struct imsm_update_activate_spare
{
426 enum imsm_update_type type
;
430 struct imsm_update_activate_spare
*next
;
436 unsigned long long size
;
443 enum takeover_direction
{
447 struct imsm_update_takeover
{
448 enum imsm_update_type type
;
450 enum takeover_direction direction
;
453 struct imsm_update_reshape
{
454 enum imsm_update_type type
;
458 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
461 struct imsm_update_reshape_migration
{
462 enum imsm_update_type type
;
465 /* fields for array migration changes
472 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
475 struct imsm_update_size_change
{
476 enum imsm_update_type type
;
481 struct imsm_update_general_migration_checkpoint
{
482 enum imsm_update_type type
;
483 __u32 curr_migr_unit
;
487 __u8 serial
[MAX_RAID_SERIAL_LEN
];
490 struct imsm_update_create_array
{
491 enum imsm_update_type type
;
496 struct imsm_update_kill_array
{
497 enum imsm_update_type type
;
501 struct imsm_update_rename_array
{
502 enum imsm_update_type type
;
503 __u8 name
[MAX_RAID_SERIAL_LEN
];
507 struct imsm_update_add_remove_disk
{
508 enum imsm_update_type type
;
512 static const char *_sys_dev_type
[] = {
513 [SYS_DEV_UNKNOWN
] = "Unknown",
514 [SYS_DEV_SAS
] = "SAS",
515 [SYS_DEV_SATA
] = "SATA"
518 const char *get_sys_dev_type(enum sys_dev_type type
)
520 if (type
>= SYS_DEV_MAX
)
521 type
= SYS_DEV_UNKNOWN
;
523 return _sys_dev_type
[type
];
526 static struct intel_hba
* alloc_intel_hba(struct sys_dev
*device
)
528 struct intel_hba
*result
= xmalloc(sizeof(*result
));
530 result
->type
= device
->type
;
531 result
->path
= xstrdup(device
->path
);
533 if (result
->path
&& (result
->pci_id
= strrchr(result
->path
, '/')) != NULL
)
539 static struct intel_hba
* find_intel_hba(struct intel_hba
*hba
, struct sys_dev
*device
)
541 struct intel_hba
*result
=NULL
;
542 for (result
= hba
; result
; result
= result
->next
) {
543 if (result
->type
== device
->type
&& strcmp(result
->path
, device
->path
) == 0)
549 static int attach_hba_to_super(struct intel_super
*super
, struct sys_dev
*device
)
551 struct intel_hba
*hba
;
553 /* check if disk attached to Intel HBA */
554 hba
= find_intel_hba(super
->hba
, device
);
557 /* Check if HBA is already attached to super */
558 if (super
->hba
== NULL
) {
559 super
->hba
= alloc_intel_hba(device
);
562 /* IMSM metadata disallows to attach disks to multiple
568 static struct sys_dev
* find_disk_attached_hba(int fd
, const char *devname
)
570 struct sys_dev
*list
, *elem
;
573 if ((list
= find_intel_devices()) == NULL
)
577 disk_path
= (char *) devname
;
579 disk_path
= diskfd_to_devpath(fd
);
584 for (elem
= list
; elem
; elem
= elem
->next
)
585 if (path_attached_to_hba(disk_path
, elem
->path
))
588 if (disk_path
!= devname
)
595 static int find_intel_hba_capability(int fd
, struct intel_super
*super
,
598 static struct supertype
*match_metadata_desc_imsm(char *arg
)
600 struct supertype
*st
;
602 if (strcmp(arg
, "imsm") != 0 &&
603 strcmp(arg
, "default") != 0
607 st
= xcalloc(1, sizeof(*st
));
608 st
->container_dev
= NoMdDev
;
609 st
->ss
= &super_imsm
;
610 st
->max_devs
= IMSM_MAX_DEVICES
;
611 st
->minor_version
= 0;
617 static __u8
*get_imsm_version(struct imsm_super
*mpb
)
619 return &mpb
->sig
[MPB_SIG_LEN
];
623 /* retrieve a disk directly from the anchor when the anchor is known to be
624 * up-to-date, currently only at load time
626 static struct imsm_disk
*__get_imsm_disk(struct imsm_super
*mpb
, __u8 index
)
628 if (index
>= mpb
->num_disks
)
630 return &mpb
->disk
[index
];
633 /* retrieve the disk description based on a index of the disk
636 static struct dl
*get_imsm_dl_disk(struct intel_super
*super
, __u8 index
)
640 for (d
= super
->disks
; d
; d
= d
->next
)
641 if (d
->index
== index
)
646 /* retrieve a disk from the parsed metadata */
647 static struct imsm_disk
*get_imsm_disk(struct intel_super
*super
, __u8 index
)
651 dl
= get_imsm_dl_disk(super
, index
);
658 /* generate a checksum directly from the anchor when the anchor is known to be
659 * up-to-date, currently only at load or write_super after coalescing
661 static __u32
__gen_imsm_checksum(struct imsm_super
*mpb
)
663 __u32 end
= mpb
->mpb_size
/ sizeof(end
);
664 __u32
*p
= (__u32
*) mpb
;
668 sum
+= __le32_to_cpu(*p
);
672 return sum
- __le32_to_cpu(mpb
->check_sum
);
675 static size_t sizeof_imsm_map(struct imsm_map
*map
)
677 return sizeof(struct imsm_map
) + sizeof(__u32
) * (map
->num_members
- 1);
680 struct imsm_map
*get_imsm_map(struct imsm_dev
*dev
, int second_map
)
682 /* A device can have 2 maps if it is in the middle of a migration.
684 * MAP_0 - we return the first map
685 * MAP_1 - we return the second map if it exists, else NULL
686 * MAP_X - we return the second map if it exists, else the first
688 struct imsm_map
*map
= &dev
->vol
.map
[0];
689 struct imsm_map
*map2
= NULL
;
691 if (dev
->vol
.migr_state
)
692 map2
= (void *)map
+ sizeof_imsm_map(map
);
694 switch (second_map
) {
711 /* return the size of the device.
712 * migr_state increases the returned size if map[0] were to be duplicated
714 static size_t sizeof_imsm_dev(struct imsm_dev
*dev
, int migr_state
)
716 size_t size
= sizeof(*dev
) - sizeof(struct imsm_map
) +
717 sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
719 /* migrating means an additional map */
720 if (dev
->vol
.migr_state
)
721 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_1
));
723 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
729 /* retrieve disk serial number list from a metadata update */
730 static struct disk_info
*get_disk_info(struct imsm_update_create_array
*update
)
733 struct disk_info
*inf
;
735 inf
= u
+ sizeof(*update
) - sizeof(struct imsm_dev
) +
736 sizeof_imsm_dev(&update
->dev
, 0);
742 static struct imsm_dev
*__get_imsm_dev(struct imsm_super
*mpb
, __u8 index
)
748 if (index
>= mpb
->num_raid_devs
)
751 /* devices start after all disks */
752 offset
= ((void *) &mpb
->disk
[mpb
->num_disks
]) - _mpb
;
754 for (i
= 0; i
<= index
; i
++)
756 return _mpb
+ offset
;
758 offset
+= sizeof_imsm_dev(_mpb
+ offset
, 0);
763 static struct imsm_dev
*get_imsm_dev(struct intel_super
*super
, __u8 index
)
765 struct intel_dev
*dv
;
767 if (index
>= super
->anchor
->num_raid_devs
)
769 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
770 if (dv
->index
== index
)
777 * == MAP_0 get first map
778 * == MAP_1 get second map
779 * == MAP_X than get map according to the current migr_state
781 static __u32
get_imsm_ord_tbl_ent(struct imsm_dev
*dev
,
785 struct imsm_map
*map
;
787 map
= get_imsm_map(dev
, second_map
);
789 /* top byte identifies disk under rebuild */
790 return __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
793 #define ord_to_idx(ord) (((ord) << 8) >> 8)
794 static __u32
get_imsm_disk_idx(struct imsm_dev
*dev
, int slot
, int second_map
)
796 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, second_map
);
798 return ord_to_idx(ord
);
801 static void set_imsm_ord_tbl_ent(struct imsm_map
*map
, int slot
, __u32 ord
)
803 map
->disk_ord_tbl
[slot
] = __cpu_to_le32(ord
);
806 static int get_imsm_disk_slot(struct imsm_map
*map
, unsigned idx
)
811 for (slot
= 0; slot
< map
->num_members
; slot
++) {
812 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
813 if (ord_to_idx(ord
) == idx
)
820 static int get_imsm_raid_level(struct imsm_map
*map
)
822 if (map
->raid_level
== 1) {
823 if (map
->num_members
== 2)
829 return map
->raid_level
;
832 static int cmp_extent(const void *av
, const void *bv
)
834 const struct extent
*a
= av
;
835 const struct extent
*b
= bv
;
836 if (a
->start
< b
->start
)
838 if (a
->start
> b
->start
)
843 static int count_memberships(struct dl
*dl
, struct intel_super
*super
)
848 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
849 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
850 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
852 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
859 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
);
861 static int split_ull(unsigned long long n
, __u32
*lo
, __u32
*hi
)
863 if (lo
== 0 || hi
== 0)
865 *lo
= __le32_to_cpu((unsigned)n
);
866 *hi
= __le32_to_cpu((unsigned)(n
>> 32));
870 static unsigned long long join_u32(__u32 lo
, __u32 hi
)
872 return (unsigned long long)__le32_to_cpu(lo
) |
873 (((unsigned long long)__le32_to_cpu(hi
)) << 32);
876 static unsigned long long total_blocks(struct imsm_disk
*disk
)
880 return join_u32(disk
->total_blocks_lo
, disk
->total_blocks_hi
);
883 static unsigned long long pba_of_lba0(struct imsm_map
*map
)
887 return join_u32(map
->pba_of_lba0_lo
, map
->pba_of_lba0_hi
);
890 static unsigned long long blocks_per_member(struct imsm_map
*map
)
894 return join_u32(map
->blocks_per_member_lo
, map
->blocks_per_member_hi
);
898 static unsigned long long num_data_stripes(struct imsm_map
*map
)
902 return join_u32(map
->num_data_stripes_lo
, map
->num_data_stripes_hi
);
905 static void set_total_blocks(struct imsm_disk
*disk
, unsigned long long n
)
907 split_ull(n
, &disk
->total_blocks_lo
, &disk
->total_blocks_hi
);
911 static void set_pba_of_lba0(struct imsm_map
*map
, unsigned long long n
)
913 split_ull(n
, &map
->pba_of_lba0_lo
, &map
->pba_of_lba0_hi
);
916 static void set_blocks_per_member(struct imsm_map
*map
, unsigned long long n
)
918 split_ull(n
, &map
->blocks_per_member_lo
, &map
->blocks_per_member_hi
);
921 static void set_num_data_stripes(struct imsm_map
*map
, unsigned long long n
)
923 split_ull(n
, &map
->num_data_stripes_lo
, &map
->num_data_stripes_hi
);
926 static struct extent
*get_extents(struct intel_super
*super
, struct dl
*dl
)
928 /* find a list of used extents on the given physical device */
929 struct extent
*rv
, *e
;
931 int memberships
= count_memberships(dl
, super
);
934 /* trim the reserved area for spares, so they can join any array
935 * regardless of whether the OROM has assigned sectors from the
936 * IMSM_RESERVED_SECTORS region
939 reservation
= imsm_min_reserved_sectors(super
);
941 reservation
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
943 rv
= xcalloc(sizeof(struct extent
), (memberships
+ 1));
946 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
947 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
948 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
950 if (get_imsm_disk_slot(map
, dl
->index
) >= 0) {
951 e
->start
= pba_of_lba0(map
);
952 e
->size
= blocks_per_member(map
);
956 qsort(rv
, memberships
, sizeof(*rv
), cmp_extent
);
958 /* determine the start of the metadata
959 * when no raid devices are defined use the default
960 * ...otherwise allow the metadata to truncate the value
961 * as is the case with older versions of imsm
964 struct extent
*last
= &rv
[memberships
- 1];
965 unsigned long long remainder
;
967 remainder
= total_blocks(&dl
->disk
) - (last
->start
+ last
->size
);
968 /* round down to 1k block to satisfy precision of the kernel
972 /* make sure remainder is still sane */
973 if (remainder
< (unsigned)ROUND_UP(super
->len
, 512) >> 9)
974 remainder
= ROUND_UP(super
->len
, 512) >> 9;
975 if (reservation
> remainder
)
976 reservation
= remainder
;
978 e
->start
= total_blocks(&dl
->disk
) - reservation
;
983 /* try to determine how much space is reserved for metadata from
984 * the last get_extents() entry, otherwise fallback to the
987 static __u32
imsm_reserved_sectors(struct intel_super
*super
, struct dl
*dl
)
993 /* for spares just return a minimal reservation which will grow
994 * once the spare is picked up by an array
997 return MPB_SECTOR_CNT
;
999 e
= get_extents(super
, dl
);
1001 return MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1003 /* scroll to last entry */
1004 for (i
= 0; e
[i
].size
; i
++)
1007 rv
= total_blocks(&dl
->disk
) - e
[i
].start
;
1014 static int is_spare(struct imsm_disk
*disk
)
1016 return (disk
->status
& SPARE_DISK
) == SPARE_DISK
;
1019 static int is_configured(struct imsm_disk
*disk
)
1021 return (disk
->status
& CONFIGURED_DISK
) == CONFIGURED_DISK
;
1024 static int is_failed(struct imsm_disk
*disk
)
1026 return (disk
->status
& FAILED_DISK
) == FAILED_DISK
;
1029 /* try to determine how much space is reserved for metadata from
1030 * the last get_extents() entry on the smallest active disk,
1031 * otherwise fallback to the default
1033 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
)
1037 unsigned long long min_active
;
1039 __u32 rv
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1040 struct dl
*dl
, *dl_min
= NULL
;
1046 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
1049 unsigned long long blocks
= total_blocks(&dl
->disk
);
1050 if (blocks
< min_active
|| min_active
== 0) {
1052 min_active
= blocks
;
1058 /* find last lba used by subarrays on the smallest active disk */
1059 e
= get_extents(super
, dl_min
);
1062 for (i
= 0; e
[i
].size
; i
++)
1065 remainder
= min_active
- e
[i
].start
;
1068 /* to give priority to recovery we should not require full
1069 IMSM_RESERVED_SECTORS from the spare */
1070 rv
= MPB_SECTOR_CNT
+ NUM_BLOCKS_DIRTY_STRIPE_REGION
;
1072 /* if real reservation is smaller use that value */
1073 return (remainder
< rv
) ? remainder
: rv
;
1076 /* Return minimum size of a spare that can be used in this array*/
1077 static unsigned long long min_acceptable_spare_size_imsm(struct supertype
*st
)
1079 struct intel_super
*super
= st
->sb
;
1083 unsigned long long rv
= 0;
1087 /* find first active disk in array */
1089 while (dl
&& (is_failed(&dl
->disk
) || dl
->index
== -1))
1093 /* find last lba used by subarrays */
1094 e
= get_extents(super
, dl
);
1097 for (i
= 0; e
[i
].size
; i
++)
1100 rv
= e
[i
-1].start
+ e
[i
-1].size
;
1103 /* add the amount of space needed for metadata */
1104 rv
= rv
+ imsm_min_reserved_sectors(super
);
1109 static int is_gen_migration(struct imsm_dev
*dev
);
1112 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
1113 struct imsm_dev
*dev
);
1115 static void print_imsm_dev(struct intel_super
*super
,
1116 struct imsm_dev
*dev
,
1122 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1123 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
1127 printf("[%.16s]:\n", dev
->volume
);
1128 printf(" UUID : %s\n", uuid
);
1129 printf(" RAID Level : %d", get_imsm_raid_level(map
));
1131 printf(" <-- %d", get_imsm_raid_level(map2
));
1133 printf(" Members : %d", map
->num_members
);
1135 printf(" <-- %d", map2
->num_members
);
1137 printf(" Slots : [");
1138 for (i
= 0; i
< map
->num_members
; i
++) {
1139 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
1140 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1145 for (i
= 0; i
< map2
->num_members
; i
++) {
1146 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_1
);
1147 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1152 printf(" Failed disk : ");
1153 if (map
->failed_disk_num
== 0xff)
1156 printf("%i", map
->failed_disk_num
);
1158 slot
= get_imsm_disk_slot(map
, disk_idx
);
1160 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
1161 printf(" This Slot : %d%s\n", slot
,
1162 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
1164 printf(" This Slot : ?\n");
1165 sz
= __le32_to_cpu(dev
->size_high
);
1167 sz
+= __le32_to_cpu(dev
->size_low
);
1168 printf(" Array Size : %llu%s\n", (unsigned long long)sz
,
1169 human_size(sz
* 512));
1170 sz
= blocks_per_member(map
);
1171 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz
,
1172 human_size(sz
* 512));
1173 printf(" Sector Offset : %llu\n",
1175 printf(" Num Stripes : %llu\n",
1176 num_data_stripes(map
));
1177 printf(" Chunk Size : %u KiB",
1178 __le16_to_cpu(map
->blocks_per_strip
) / 2);
1180 printf(" <-- %u KiB",
1181 __le16_to_cpu(map2
->blocks_per_strip
) / 2);
1183 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
1184 printf(" Migrate State : ");
1185 if (dev
->vol
.migr_state
) {
1186 if (migr_type(dev
) == MIGR_INIT
)
1187 printf("initialize\n");
1188 else if (migr_type(dev
) == MIGR_REBUILD
)
1189 printf("rebuild\n");
1190 else if (migr_type(dev
) == MIGR_VERIFY
)
1192 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
1193 printf("general migration\n");
1194 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
1195 printf("state change\n");
1196 else if (migr_type(dev
) == MIGR_REPAIR
)
1199 printf("<unknown:%d>\n", migr_type(dev
));
1202 printf(" Map State : %s", map_state_str
[map
->map_state
]);
1203 if (dev
->vol
.migr_state
) {
1204 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
1206 printf(" <-- %s", map_state_str
[map
->map_state
]);
1207 printf("\n Checkpoint : %u ",
1208 __le32_to_cpu(dev
->vol
.curr_migr_unit
));
1209 if ((is_gen_migration(dev
)) && ((slot
> 1) || (slot
< 0)))
1212 printf("(%llu)", (unsigned long long)
1213 blocks_per_migr_unit(super
, dev
));
1216 printf(" Dirty State : %s\n", dev
->vol
.dirty
? "dirty" : "clean");
1219 static void print_imsm_disk(struct imsm_disk
*disk
, int index
, __u32 reserved
)
1221 char str
[MAX_RAID_SERIAL_LEN
+ 1];
1224 if (index
< -1 || !disk
)
1228 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
1230 printf(" Disk%02d Serial : %s\n", index
, str
);
1232 printf(" Disk Serial : %s\n", str
);
1233 printf(" State :%s%s%s\n", is_spare(disk
) ? " spare" : "",
1234 is_configured(disk
) ? " active" : "",
1235 is_failed(disk
) ? " failed" : "");
1236 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
1237 sz
= total_blocks(disk
) - reserved
;
1238 printf(" Usable Size : %llu%s\n", (unsigned long long)sz
,
1239 human_size(sz
* 512));
1242 void examine_migr_rec_imsm(struct intel_super
*super
)
1244 struct migr_record
*migr_rec
= super
->migr_rec
;
1245 struct imsm_super
*mpb
= super
->anchor
;
1248 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1249 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1250 struct imsm_map
*map
;
1253 if (is_gen_migration(dev
) == 0)
1256 printf("\nMigration Record Information:");
1258 /* first map under migration */
1259 map
= get_imsm_map(dev
, MAP_0
);
1261 slot
= get_imsm_disk_slot(map
, super
->disks
->index
);
1262 if ((map
== NULL
) || (slot
> 1) || (slot
< 0)) {
1263 printf(" Empty\n ");
1264 printf("Examine one of first two disks in array\n");
1267 printf("\n Status : ");
1268 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
1271 printf("Contains Data\n");
1272 printf(" Current Unit : %u\n",
1273 __le32_to_cpu(migr_rec
->curr_migr_unit
));
1274 printf(" Family : %u\n",
1275 __le32_to_cpu(migr_rec
->family_num
));
1276 printf(" Ascending : %u\n",
1277 __le32_to_cpu(migr_rec
->ascending_migr
));
1278 printf(" Blocks Per Unit : %u\n",
1279 __le32_to_cpu(migr_rec
->blocks_per_unit
));
1280 printf(" Dest. Depth Per Unit : %u\n",
1281 __le32_to_cpu(migr_rec
->dest_depth_per_unit
));
1282 printf(" Checkpoint Area pba : %u\n",
1283 __le32_to_cpu(migr_rec
->ckpt_area_pba
));
1284 printf(" First member lba : %u\n",
1285 __le32_to_cpu(migr_rec
->dest_1st_member_lba
));
1286 printf(" Total Number of Units : %u\n",
1287 __le32_to_cpu(migr_rec
->num_migr_units
));
1288 printf(" Size of volume : %u\n",
1289 __le32_to_cpu(migr_rec
->post_migr_vol_cap
));
1290 printf(" Expansion space for LBA64 : %u\n",
1291 __le32_to_cpu(migr_rec
->post_migr_vol_cap_hi
));
1292 printf(" Record was read from : %u\n",
1293 __le32_to_cpu(migr_rec
->ckpt_read_disk_num
));
1298 #endif /* MDASSEMBLE */
1299 /*******************************************************************************
1300 * function: imsm_check_attributes
1301 * Description: Function checks if features represented by attributes flags
1302 * are supported by mdadm.
1304 * attributes - Attributes read from metadata
1306 * 0 - passed attributes contains unsupported features flags
1307 * 1 - all features are supported
1308 ******************************************************************************/
1309 static int imsm_check_attributes(__u32 attributes
)
1312 __u32 not_supported
= MPB_ATTRIB_SUPPORTED
^0xffffffff;
1314 not_supported
&= ~MPB_ATTRIB_IGNORED
;
1316 not_supported
&= attributes
;
1317 if (not_supported
) {
1318 pr_err("(IMSM): Unsupported attributes : %x\n",
1319 (unsigned)__le32_to_cpu(not_supported
));
1320 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1321 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
1322 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1324 if (not_supported
& MPB_ATTRIB_2TB
) {
1325 dprintf("\t\tMPB_ATTRIB_2TB\n");
1326 not_supported
^= MPB_ATTRIB_2TB
;
1328 if (not_supported
& MPB_ATTRIB_RAID0
) {
1329 dprintf("\t\tMPB_ATTRIB_RAID0\n");
1330 not_supported
^= MPB_ATTRIB_RAID0
;
1332 if (not_supported
& MPB_ATTRIB_RAID1
) {
1333 dprintf("\t\tMPB_ATTRIB_RAID1\n");
1334 not_supported
^= MPB_ATTRIB_RAID1
;
1336 if (not_supported
& MPB_ATTRIB_RAID10
) {
1337 dprintf("\t\tMPB_ATTRIB_RAID10\n");
1338 not_supported
^= MPB_ATTRIB_RAID10
;
1340 if (not_supported
& MPB_ATTRIB_RAID1E
) {
1341 dprintf("\t\tMPB_ATTRIB_RAID1E\n");
1342 not_supported
^= MPB_ATTRIB_RAID1E
;
1344 if (not_supported
& MPB_ATTRIB_RAID5
) {
1345 dprintf("\t\tMPB_ATTRIB_RAID5\n");
1346 not_supported
^= MPB_ATTRIB_RAID5
;
1348 if (not_supported
& MPB_ATTRIB_RAIDCNG
) {
1349 dprintf("\t\tMPB_ATTRIB_RAIDCNG\n");
1350 not_supported
^= MPB_ATTRIB_RAIDCNG
;
1352 if (not_supported
& MPB_ATTRIB_BBM
) {
1353 dprintf("\t\tMPB_ATTRIB_BBM\n");
1354 not_supported
^= MPB_ATTRIB_BBM
;
1356 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1357 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n");
1358 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1360 if (not_supported
& MPB_ATTRIB_EXP_STRIPE_SIZE
) {
1361 dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n");
1362 not_supported
^= MPB_ATTRIB_EXP_STRIPE_SIZE
;
1364 if (not_supported
& MPB_ATTRIB_2TB_DISK
) {
1365 dprintf("\t\tMPB_ATTRIB_2TB_DISK\n");
1366 not_supported
^= MPB_ATTRIB_2TB_DISK
;
1368 if (not_supported
& MPB_ATTRIB_NEVER_USE2
) {
1369 dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n");
1370 not_supported
^= MPB_ATTRIB_NEVER_USE2
;
1372 if (not_supported
& MPB_ATTRIB_NEVER_USE
) {
1373 dprintf("\t\tMPB_ATTRIB_NEVER_USE\n");
1374 not_supported
^= MPB_ATTRIB_NEVER_USE
;
1378 dprintf(Name
"(IMSM): Unknown attributes : %x\n", not_supported
);
1387 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
);
1389 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
1391 struct intel_super
*super
= st
->sb
;
1392 struct imsm_super
*mpb
= super
->anchor
;
1393 char str
[MAX_SIGNATURE_LENGTH
];
1398 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
1401 snprintf(str
, MPB_SIG_LEN
, "%s", mpb
->sig
);
1402 printf(" Magic : %s\n", str
);
1403 snprintf(str
, strlen(MPB_VERSION_RAID0
), "%s", get_imsm_version(mpb
));
1404 printf(" Version : %s\n", get_imsm_version(mpb
));
1405 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
1406 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
1407 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
1408 printf(" Attributes : ");
1409 if (imsm_check_attributes(mpb
->attributes
))
1410 printf("All supported\n");
1412 printf("not supported\n");
1413 getinfo_super_imsm(st
, &info
, NULL
);
1414 fname_from_uuid(st
, &info
, nbuf
, ':');
1415 printf(" UUID : %s\n", nbuf
+ 5);
1416 sum
= __le32_to_cpu(mpb
->check_sum
);
1417 printf(" Checksum : %08x %s\n", sum
,
1418 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
1419 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
));
1420 printf(" Disks : %d\n", mpb
->num_disks
);
1421 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
1422 print_imsm_disk(__get_imsm_disk(mpb
, super
->disks
->index
), super
->disks
->index
, reserved
);
1423 if (super
->bbm_log
) {
1424 struct bbm_log
*log
= super
->bbm_log
;
1427 printf("Bad Block Management Log:\n");
1428 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
1429 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
1430 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
1431 printf(" Spare Blocks : %d\n", __le32_to_cpu(log
->reserved_spare_block_count
));
1432 printf(" First Spare : %llx\n",
1433 (unsigned long long) __le64_to_cpu(log
->first_spare_lba
));
1435 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1437 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1439 super
->current_vol
= i
;
1440 getinfo_super_imsm(st
, &info
, NULL
);
1441 fname_from_uuid(st
, &info
, nbuf
, ':');
1442 print_imsm_dev(super
, dev
, nbuf
+ 5, super
->disks
->index
);
1444 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1445 if (i
== super
->disks
->index
)
1447 print_imsm_disk(__get_imsm_disk(mpb
, i
), i
, reserved
);
1450 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
1451 if (dl
->index
== -1)
1452 print_imsm_disk(&dl
->disk
, -1, reserved
);
1454 examine_migr_rec_imsm(super
);
1457 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
1459 /* We just write a generic IMSM ARRAY entry */
1462 struct intel_super
*super
= st
->sb
;
1464 if (!super
->anchor
->num_raid_devs
) {
1465 printf("ARRAY metadata=imsm\n");
1469 getinfo_super_imsm(st
, &info
, NULL
);
1470 fname_from_uuid(st
, &info
, nbuf
, ':');
1471 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
1474 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
1476 /* We just write a generic IMSM ARRAY entry */
1480 struct intel_super
*super
= st
->sb
;
1483 if (!super
->anchor
->num_raid_devs
)
1486 getinfo_super_imsm(st
, &info
, NULL
);
1487 fname_from_uuid(st
, &info
, nbuf
, ':');
1488 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1489 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1491 super
->current_vol
= i
;
1492 getinfo_super_imsm(st
, &info
, NULL
);
1493 fname_from_uuid(st
, &info
, nbuf1
, ':');
1494 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
1495 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
1499 static void export_examine_super_imsm(struct supertype
*st
)
1501 struct intel_super
*super
= st
->sb
;
1502 struct imsm_super
*mpb
= super
->anchor
;
1506 getinfo_super_imsm(st
, &info
, NULL
);
1507 fname_from_uuid(st
, &info
, nbuf
, ':');
1508 printf("MD_METADATA=imsm\n");
1509 printf("MD_LEVEL=container\n");
1510 printf("MD_UUID=%s\n", nbuf
+5);
1511 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
1514 static void detail_super_imsm(struct supertype
*st
, char *homehost
)
1519 getinfo_super_imsm(st
, &info
, NULL
);
1520 fname_from_uuid(st
, &info
, nbuf
, ':');
1521 printf("\n UUID : %s\n", nbuf
+ 5);
1524 static void brief_detail_super_imsm(struct supertype
*st
)
1528 getinfo_super_imsm(st
, &info
, NULL
);
1529 fname_from_uuid(st
, &info
, nbuf
, ':');
1530 printf(" UUID=%s", nbuf
+ 5);
1533 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
);
1534 static void fd2devname(int fd
, char *name
);
1536 static int ahci_enumerate_ports(const char *hba_path
, int port_count
, int host_base
, int verbose
)
1538 /* dump an unsorted list of devices attached to AHCI Intel storage
1539 * controller, as well as non-connected ports
1541 int hba_len
= strlen(hba_path
) + 1;
1546 unsigned long port_mask
= (1 << port_count
) - 1;
1548 if (port_count
> (int)sizeof(port_mask
) * 8) {
1550 pr_err("port_count %d out of range\n", port_count
);
1554 /* scroll through /sys/dev/block looking for devices attached to
1557 dir
= opendir("/sys/dev/block");
1558 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
1569 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
1571 path
= devt_to_devpath(makedev(major
, minor
));
1574 if (!path_attached_to_hba(path
, hba_path
)) {
1580 /* retrieve the scsi device type */
1581 if (asprintf(&device
, "/sys/dev/block/%d:%d/device/xxxxxxx", major
, minor
) < 0) {
1583 pr_err("failed to allocate 'device'\n");
1587 sprintf(device
, "/sys/dev/block/%d:%d/device/type", major
, minor
);
1588 if (load_sys(device
, buf
) != 0) {
1590 pr_err("failed to read device type for %s\n",
1596 type
= strtoul(buf
, NULL
, 10);
1598 /* if it's not a disk print the vendor and model */
1599 if (!(type
== 0 || type
== 7 || type
== 14)) {
1602 sprintf(device
, "/sys/dev/block/%d:%d/device/vendor", major
, minor
);
1603 if (load_sys(device
, buf
) == 0) {
1604 strncpy(vendor
, buf
, sizeof(vendor
));
1605 vendor
[sizeof(vendor
) - 1] = '\0';
1606 c
= (char *) &vendor
[sizeof(vendor
) - 1];
1607 while (isspace(*c
) || *c
== '\0')
1611 sprintf(device
, "/sys/dev/block/%d:%d/device/model", major
, minor
);
1612 if (load_sys(device
, buf
) == 0) {
1613 strncpy(model
, buf
, sizeof(model
));
1614 model
[sizeof(model
) - 1] = '\0';
1615 c
= (char *) &model
[sizeof(model
) - 1];
1616 while (isspace(*c
) || *c
== '\0')
1620 if (vendor
[0] && model
[0])
1621 sprintf(buf
, "%.64s %.64s", vendor
, model
);
1623 switch (type
) { /* numbers from hald/linux/device.c */
1624 case 1: sprintf(buf
, "tape"); break;
1625 case 2: sprintf(buf
, "printer"); break;
1626 case 3: sprintf(buf
, "processor"); break;
1628 case 5: sprintf(buf
, "cdrom"); break;
1629 case 6: sprintf(buf
, "scanner"); break;
1630 case 8: sprintf(buf
, "media_changer"); break;
1631 case 9: sprintf(buf
, "comm"); break;
1632 case 12: sprintf(buf
, "raid"); break;
1633 default: sprintf(buf
, "unknown");
1639 /* chop device path to 'host%d' and calculate the port number */
1640 c
= strchr(&path
[hba_len
], '/');
1643 pr_err("%s - invalid path name\n", path
+ hba_len
);
1648 if (sscanf(&path
[hba_len
], "host%d", &port
) == 1)
1652 *c
= '/'; /* repair the full string */
1653 pr_err("failed to determine port number for %s\n",
1660 /* mark this port as used */
1661 port_mask
&= ~(1 << port
);
1663 /* print out the device information */
1665 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
1669 fd
= dev_open(ent
->d_name
, O_RDONLY
);
1671 printf(" Port%d : - disk info unavailable -\n", port
);
1673 fd2devname(fd
, buf
);
1674 printf(" Port%d : %s", port
, buf
);
1675 if (imsm_read_serial(fd
, NULL
, (__u8
*) buf
) == 0)
1676 printf(" (%.*s)\n", MAX_RAID_SERIAL_LEN
, buf
);
1691 for (i
= 0; i
< port_count
; i
++)
1692 if (port_mask
& (1 << i
))
1693 printf(" Port%d : - no device attached -\n", i
);
1699 static void print_found_intel_controllers(struct sys_dev
*elem
)
1701 for (; elem
; elem
= elem
->next
) {
1702 pr_err("found Intel(R) ");
1703 if (elem
->type
== SYS_DEV_SATA
)
1704 fprintf(stderr
, "SATA ");
1705 else if (elem
->type
== SYS_DEV_SAS
)
1706 fprintf(stderr
, "SAS ");
1707 fprintf(stderr
, "RAID controller");
1709 fprintf(stderr
, " at %s", elem
->pci_id
);
1710 fprintf(stderr
, ".\n");
1715 static int ahci_get_port_count(const char *hba_path
, int *port_count
)
1722 if ((dir
= opendir(hba_path
)) == NULL
)
1725 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
1728 if (sscanf(ent
->d_name
, "host%d", &host
) != 1)
1730 if (*port_count
== 0)
1732 else if (host
< host_base
)
1735 if (host
+ 1 > *port_count
+ host_base
)
1736 *port_count
= host
+ 1 - host_base
;
1742 static void print_imsm_capability(const struct imsm_orom
*orom
)
1744 printf(" Platform : Intel(R) Matrix Storage Manager\n");
1745 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
1746 orom
->hotfix_ver
, orom
->build
);
1747 printf(" RAID Levels :%s%s%s%s%s\n",
1748 imsm_orom_has_raid0(orom
) ? " raid0" : "",
1749 imsm_orom_has_raid1(orom
) ? " raid1" : "",
1750 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
1751 imsm_orom_has_raid10(orom
) ? " raid10" : "",
1752 imsm_orom_has_raid5(orom
) ? " raid5" : "");
1753 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1754 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
1755 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
1756 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
1757 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
1758 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
1759 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
1760 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
1761 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
1762 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
1763 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
1764 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
1765 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
1766 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
1767 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
1768 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
1769 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
1770 printf(" 2TB volumes :%s supported\n",
1771 (orom
->attr
& IMSM_OROM_ATTR_2TB
)?"":" not");
1772 printf(" 2TB disks :%s supported\n",
1773 (orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
)?"":" not");
1774 printf(" Max Disks : %d\n", orom
->tds
);
1775 printf(" Max Volumes : %d per array, %d per controller\n",
1776 orom
->vpa
, orom
->vphba
);
1780 static void print_imsm_capability_export(const struct imsm_orom
*orom
)
1782 printf("MD_FIRMWARE_TYPE=imsm\n");
1783 printf("IMSM_VERSION=%d.%d.%d.%d\n",orom
->major_ver
, orom
->minor_ver
,
1784 orom
->hotfix_ver
, orom
->build
);
1785 printf("IMSM_SUPPORTED_RAID_LEVELS=%s%s%s%s%s\n",
1786 imsm_orom_has_raid0(orom
) ? "raid0 " : "",
1787 imsm_orom_has_raid1(orom
) ? "raid1 " : "",
1788 imsm_orom_has_raid1e(orom
) ? "raid1e " : "",
1789 imsm_orom_has_raid5(orom
) ? "raid10 " : "",
1790 imsm_orom_has_raid10(orom
) ? "raid5 " : "");
1791 printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1792 imsm_orom_has_chunk(orom
, 2) ? "2k " : "",
1793 imsm_orom_has_chunk(orom
, 4) ? "4k " : "",
1794 imsm_orom_has_chunk(orom
, 8) ? "8k " : "",
1795 imsm_orom_has_chunk(orom
, 16) ? "16k " : "",
1796 imsm_orom_has_chunk(orom
, 32) ? "32k " : "",
1797 imsm_orom_has_chunk(orom
, 64) ? "64k " : "",
1798 imsm_orom_has_chunk(orom
, 128) ? "128k " : "",
1799 imsm_orom_has_chunk(orom
, 256) ? "256k " : "",
1800 imsm_orom_has_chunk(orom
, 512) ? "512k " : "",
1801 imsm_orom_has_chunk(orom
, 1024*1) ? "1M " : "",
1802 imsm_orom_has_chunk(orom
, 1024*2) ? "2M " : "",
1803 imsm_orom_has_chunk(orom
, 1024*4) ? "4M " : "",
1804 imsm_orom_has_chunk(orom
, 1024*8) ? "8M " : "",
1805 imsm_orom_has_chunk(orom
, 1024*16) ? "16M " : "",
1806 imsm_orom_has_chunk(orom
, 1024*32) ? "32M " : "",
1807 imsm_orom_has_chunk(orom
, 1024*64) ? "64M " : "");
1808 printf("IMSM_2TB_VOLUMES=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB
) ? "yes" : "no");
1809 printf("IMSM_2TB_DISKS=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) ? "yes" : "no");
1810 printf("IMSM_MAX_DISKS=%d\n",orom
->tds
);
1811 printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom
->vpa
);
1812 printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom
->vphba
);
1815 static int detail_platform_imsm(int verbose
, int enumerate_only
, char *controller_path
)
1817 /* There are two components to imsm platform support, the ahci SATA
1818 * controller and the option-rom. To find the SATA controller we
1819 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
1820 * controller with the Intel vendor id is present. This approach
1821 * allows mdadm to leverage the kernel's ahci detection logic, with the
1822 * caveat that if ahci.ko is not loaded mdadm will not be able to
1823 * detect platform raid capabilities. The option-rom resides in a
1824 * platform "Adapter ROM". We scan for its signature to retrieve the
1825 * platform capabilities. If raid support is disabled in the BIOS the
1826 * option-rom capability structure will not be available.
1828 const struct imsm_orom
*orom
;
1829 struct sys_dev
*list
, *hba
;
1834 if (enumerate_only
) {
1835 if (check_env("IMSM_NO_PLATFORM"))
1837 list
= find_intel_devices();
1840 for (hba
= list
; hba
; hba
= hba
->next
) {
1841 orom
= find_imsm_capability(hba
->type
);
1852 list
= find_intel_devices();
1855 pr_err("no active Intel(R) RAID "
1856 "controller found.\n");
1858 } else if (verbose
> 0)
1859 print_found_intel_controllers(list
);
1861 for (hba
= list
; hba
; hba
= hba
->next
) {
1862 if (controller_path
&& (compare_paths(hba
->path
,controller_path
) != 0))
1864 orom
= find_imsm_capability(hba
->type
);
1866 pr_err("imsm capabilities not found for controller: %s (type %s)\n",
1867 hba
->path
, get_sys_dev_type(hba
->type
));
1870 print_imsm_capability(orom
);
1871 printf(" I/O Controller : %s (%s)\n",
1872 hba
->path
, get_sys_dev_type(hba
->type
));
1873 if (hba
->type
== SYS_DEV_SATA
) {
1874 host_base
= ahci_get_port_count(hba
->path
, &port_count
);
1875 if (ahci_enumerate_ports(hba
->path
, port_count
, host_base
, verbose
)) {
1877 pr_err("failed to enumerate "
1878 "ports on SATA controller at %s.\n", hba
->pci_id
);
1885 if (controller_path
&& result
== 1)
1886 pr_err("no active Intel(R) RAID "
1887 "controller found under %s\n",controller_path
);
1892 static int export_detail_platform_imsm(int verbose
, char *controller_path
)
1894 const struct imsm_orom
*orom
;
1895 struct sys_dev
*list
, *hba
;
1898 list
= find_intel_devices();
1901 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n");
1906 for (hba
= list
; hba
; hba
= hba
->next
) {
1907 if (controller_path
&& (compare_paths(hba
->path
,controller_path
) != 0))
1909 orom
= find_imsm_capability(hba
->type
);
1912 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n",hba
->path
);
1915 print_imsm_capability_export(orom
);
1925 static int match_home_imsm(struct supertype
*st
, char *homehost
)
1927 /* the imsm metadata format does not specify any host
1928 * identification information. We return -1 since we can never
1929 * confirm nor deny whether a given array is "meant" for this
1930 * host. We rely on compare_super and the 'family_num' fields to
1931 * exclude member disks that do not belong, and we rely on
1932 * mdadm.conf to specify the arrays that should be assembled.
1933 * Auto-assembly may still pick up "foreign" arrays.
1939 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
1941 /* The uuid returned here is used for:
1942 * uuid to put into bitmap file (Create, Grow)
1943 * uuid for backup header when saving critical section (Grow)
1944 * comparing uuids when re-adding a device into an array
1945 * In these cases the uuid required is that of the data-array,
1946 * not the device-set.
1947 * uuid to recognise same set when adding a missing device back
1948 * to an array. This is a uuid for the device-set.
1950 * For each of these we can make do with a truncated
1951 * or hashed uuid rather than the original, as long as
1953 * In each case the uuid required is that of the data-array,
1954 * not the device-set.
1956 /* imsm does not track uuid's so we synthesis one using sha1 on
1957 * - The signature (Which is constant for all imsm array, but no matter)
1958 * - the orig_family_num of the container
1959 * - the index number of the volume
1960 * - the 'serial' number of the volume.
1961 * Hopefully these are all constant.
1963 struct intel_super
*super
= st
->sb
;
1966 struct sha1_ctx ctx
;
1967 struct imsm_dev
*dev
= NULL
;
1970 /* some mdadm versions failed to set ->orig_family_num, in which
1971 * case fall back to ->family_num. orig_family_num will be
1972 * fixed up with the first metadata update.
1974 family_num
= super
->anchor
->orig_family_num
;
1975 if (family_num
== 0)
1976 family_num
= super
->anchor
->family_num
;
1977 sha1_init_ctx(&ctx
);
1978 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
1979 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
1980 if (super
->current_vol
>= 0)
1981 dev
= get_imsm_dev(super
, super
->current_vol
);
1983 __u32 vol
= super
->current_vol
;
1984 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
1985 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
1987 sha1_finish_ctx(&ctx
, buf
);
1988 memcpy(uuid
, buf
, 4*4);
1993 get_imsm_numerical_version(struct imsm_super
*mpb
, int *m
, int *p
)
1995 __u8
*v
= get_imsm_version(mpb
);
1996 __u8
*end
= mpb
->sig
+ MAX_SIGNATURE_LENGTH
;
1997 char major
[] = { 0, 0, 0 };
1998 char minor
[] = { 0 ,0, 0 };
1999 char patch
[] = { 0, 0, 0 };
2000 char *ver_parse
[] = { major
, minor
, patch
};
2004 while (*v
!= '\0' && v
< end
) {
2005 if (*v
!= '.' && j
< 2)
2006 ver_parse
[i
][j
++] = *v
;
2014 *m
= strtol(minor
, NULL
, 0);
2015 *p
= strtol(patch
, NULL
, 0);
2019 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
2021 /* migr_strip_size when repairing or initializing parity */
2022 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2023 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2025 switch (get_imsm_raid_level(map
)) {
2030 return 128*1024 >> 9;
2034 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
2036 /* migr_strip_size when rebuilding a degraded disk, no idea why
2037 * this is different than migr_strip_size_resync(), but it's good
2040 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2041 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2043 switch (get_imsm_raid_level(map
)) {
2046 if (map
->num_members
% map
->num_domains
== 0)
2047 return 128*1024 >> 9;
2051 return max((__u32
) 64*1024 >> 9, chunk
);
2053 return 128*1024 >> 9;
2057 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
2059 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2060 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2061 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
2062 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
2064 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
2067 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
2069 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2070 int level
= get_imsm_raid_level(lo
);
2072 if (level
== 1 || level
== 10) {
2073 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2075 return hi
->num_domains
;
2077 return num_stripes_per_unit_resync(dev
);
2080 static __u8
imsm_num_data_members(struct imsm_dev
*dev
, int second_map
)
2082 /* named 'imsm_' because raid0, raid1 and raid10
2083 * counter-intuitively have the same number of data disks
2085 struct imsm_map
*map
= get_imsm_map(dev
, second_map
);
2087 switch (get_imsm_raid_level(map
)) {
2089 return map
->num_members
;
2093 return map
->num_members
/2;
2095 return map
->num_members
- 1;
2097 dprintf("%s: unsupported raid level\n", __func__
);
2102 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
2104 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2105 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2107 switch(get_imsm_raid_level(map
)) {
2110 return chunk
* map
->num_domains
;
2112 return chunk
* map
->num_members
;
2118 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
2120 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2121 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2122 __u32 strip
= block
/ chunk
;
2124 switch (get_imsm_raid_level(map
)) {
2127 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
2128 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
2130 return vol_stripe
* chunk
+ block
% chunk
;
2132 __u32 stripe
= strip
/ (map
->num_members
- 1);
2134 return stripe
* chunk
+ block
% chunk
;
2141 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
2142 struct imsm_dev
*dev
)
2144 /* calculate the conversion factor between per member 'blocks'
2145 * (md/{resync,rebuild}_start) and imsm migration units, return
2146 * 0 for the 'not migrating' and 'unsupported migration' cases
2148 if (!dev
->vol
.migr_state
)
2151 switch (migr_type(dev
)) {
2152 case MIGR_GEN_MIGR
: {
2153 struct migr_record
*migr_rec
= super
->migr_rec
;
2154 return __le32_to_cpu(migr_rec
->blocks_per_unit
);
2159 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2160 __u32 stripes_per_unit
;
2161 __u32 blocks_per_unit
;
2170 /* yes, this is really the translation of migr_units to
2171 * per-member blocks in the 'resync' case
2173 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
2174 migr_chunk
= migr_strip_blocks_resync(dev
);
2175 disks
= imsm_num_data_members(dev
, MAP_0
);
2176 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
2177 stripe
= __le16_to_cpu(map
->blocks_per_strip
) * disks
;
2178 segment
= blocks_per_unit
/ stripe
;
2179 block_rel
= blocks_per_unit
- segment
* stripe
;
2180 parity_depth
= parity_segment_depth(dev
);
2181 block_map
= map_migr_block(dev
, block_rel
);
2182 return block_map
+ parity_depth
* segment
;
2184 case MIGR_REBUILD
: {
2185 __u32 stripes_per_unit
;
2188 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
2189 migr_chunk
= migr_strip_blocks_rebuild(dev
);
2190 return migr_chunk
* stripes_per_unit
;
2192 case MIGR_STATE_CHANGE
:
2198 static int imsm_level_to_layout(int level
)
2206 return ALGORITHM_LEFT_ASYMMETRIC
;
2213 /*******************************************************************************
2214 * Function: read_imsm_migr_rec
2215 * Description: Function reads imsm migration record from last sector of disk
2217 * fd : disk descriptor
2218 * super : metadata info
2222 ******************************************************************************/
2223 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
)
2226 unsigned long long dsize
;
2228 get_dev_size(fd
, NULL
, &dsize
);
2229 if (lseek64(fd
, dsize
- MIGR_REC_POSITION
, SEEK_SET
) < 0) {
2230 pr_err("Cannot seek to anchor block: %s\n",
2234 if (read(fd
, super
->migr_rec_buf
, MIGR_REC_BUF_SIZE
) !=
2235 MIGR_REC_BUF_SIZE
) {
2236 pr_err("Cannot read migr record block: %s\n",
2246 static struct imsm_dev
*imsm_get_device_during_migration(
2247 struct intel_super
*super
)
2250 struct intel_dev
*dv
;
2252 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
2253 if (is_gen_migration(dv
->dev
))
2259 /*******************************************************************************
2260 * Function: load_imsm_migr_rec
2261 * Description: Function reads imsm migration record (it is stored at the last
2264 * super : imsm internal array info
2265 * info : general array info
2269 * -2 : no migration in progress
2270 ******************************************************************************/
2271 static int load_imsm_migr_rec(struct intel_super
*super
, struct mdinfo
*info
)
2274 struct dl
*dl
= NULL
;
2278 struct imsm_dev
*dev
;
2279 struct imsm_map
*map
= NULL
;
2282 /* find map under migration */
2283 dev
= imsm_get_device_during_migration(super
);
2284 /* nothing to load,no migration in progress?
2288 map
= get_imsm_map(dev
, MAP_0
);
2291 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
2292 /* skip spare and failed disks
2294 if (sd
->disk
.raid_disk
< 0)
2296 /* read only from one of the first two slots */
2298 slot
= get_imsm_disk_slot(map
,
2299 sd
->disk
.raid_disk
);
2300 if ((map
== NULL
) || (slot
> 1) || (slot
< 0))
2303 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
2304 fd
= dev_open(nm
, O_RDONLY
);
2310 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
2311 /* skip spare and failed disks
2315 /* read only from one of the first two slots */
2317 slot
= get_imsm_disk_slot(map
, dl
->index
);
2318 if ((map
== NULL
) || (slot
> 1) || (slot
< 0))
2320 sprintf(nm
, "%d:%d", dl
->major
, dl
->minor
);
2321 fd
= dev_open(nm
, O_RDONLY
);
2328 retval
= read_imsm_migr_rec(fd
, super
);
2337 /*******************************************************************************
2338 * function: imsm_create_metadata_checkpoint_update
2339 * Description: It creates update for checkpoint change.
2341 * super : imsm internal array info
2342 * u : pointer to prepared update
2345 * If length is equal to 0, input pointer u contains no update
2346 ******************************************************************************/
2347 static int imsm_create_metadata_checkpoint_update(
2348 struct intel_super
*super
,
2349 struct imsm_update_general_migration_checkpoint
**u
)
2352 int update_memory_size
= 0;
2354 dprintf("imsm_create_metadata_checkpoint_update(enter)\n");
2360 /* size of all update data without anchor */
2361 update_memory_size
=
2362 sizeof(struct imsm_update_general_migration_checkpoint
);
2364 *u
= xcalloc(1, update_memory_size
);
2366 dprintf("error: cannot get memory for "
2367 "imsm_create_metadata_checkpoint_update update\n");
2370 (*u
)->type
= update_general_migration_checkpoint
;
2371 (*u
)->curr_migr_unit
= __le32_to_cpu(super
->migr_rec
->curr_migr_unit
);
2372 dprintf("imsm_create_metadata_checkpoint_update: prepared for %u\n",
2373 (*u
)->curr_migr_unit
);
2375 return update_memory_size
;
2379 static void imsm_update_metadata_locally(struct supertype
*st
,
2380 void *buf
, int len
);
2382 /*******************************************************************************
2383 * Function: write_imsm_migr_rec
2384 * Description: Function writes imsm migration record
2385 * (at the last sector of disk)
2387 * super : imsm internal array info
2391 ******************************************************************************/
2392 static int write_imsm_migr_rec(struct supertype
*st
)
2394 struct intel_super
*super
= st
->sb
;
2395 unsigned long long dsize
;
2401 struct imsm_update_general_migration_checkpoint
*u
;
2402 struct imsm_dev
*dev
;
2403 struct imsm_map
*map
= NULL
;
2405 /* find map under migration */
2406 dev
= imsm_get_device_during_migration(super
);
2407 /* if no migration, write buffer anyway to clear migr_record
2408 * on disk based on first available device
2411 dev
= get_imsm_dev(super
, super
->current_vol
< 0 ? 0 :
2412 super
->current_vol
);
2414 map
= get_imsm_map(dev
, MAP_0
);
2416 for (sd
= super
->disks
; sd
; sd
= sd
->next
) {
2419 /* skip failed and spare devices */
2422 /* write to 2 first slots only */
2424 slot
= get_imsm_disk_slot(map
, sd
->index
);
2425 if ((map
== NULL
) || (slot
> 1) || (slot
< 0))
2428 sprintf(nm
, "%d:%d", sd
->major
, sd
->minor
);
2429 fd
= dev_open(nm
, O_RDWR
);
2432 get_dev_size(fd
, NULL
, &dsize
);
2433 if (lseek64(fd
, dsize
- MIGR_REC_POSITION
, SEEK_SET
) < 0) {
2434 pr_err("Cannot seek to anchor block: %s\n",
2438 if (write(fd
, super
->migr_rec_buf
, MIGR_REC_BUF_SIZE
) !=
2439 MIGR_REC_BUF_SIZE
) {
2440 pr_err("Cannot write migr record block: %s\n",
2447 /* update checkpoint information in metadata */
2448 len
= imsm_create_metadata_checkpoint_update(super
, &u
);
2451 dprintf("imsm: Cannot prepare update\n");
2454 /* update metadata locally */
2455 imsm_update_metadata_locally(st
, u
, len
);
2456 /* and possibly remotely */
2457 if (st
->update_tail
) {
2458 append_metadata_update(st
, u
, len
);
2459 /* during reshape we do all work inside metadata handler
2460 * manage_reshape(), so metadata update has to be triggered
2463 flush_metadata_updates(st
);
2464 st
->update_tail
= &st
->updates
;
2474 #endif /* MDASSEMBLE */
2476 /* spare/missing disks activations are not allowe when
2477 * array/container performs reshape operation, because
2478 * all arrays in container works on the same disks set
2480 int imsm_reshape_blocks_arrays_changes(struct intel_super
*super
)
2483 struct intel_dev
*i_dev
;
2484 struct imsm_dev
*dev
;
2486 /* check whole container
2488 for (i_dev
= super
->devlist
; i_dev
; i_dev
= i_dev
->next
) {
2490 if (is_gen_migration(dev
)) {
2491 /* No repair during any migration in container
2499 static unsigned long long imsm_component_size_aligment_check(int level
,
2501 unsigned long long component_size
)
2503 unsigned int component_size_alligment
;
2505 /* check component size aligment
2507 component_size_alligment
= component_size
% (chunk_size
/512);
2509 dprintf("imsm_component_size_aligment_check(Level: %i, "
2510 "chunk_size = %i, component_size = %llu), "
2511 "component_size_alligment = %u\n",
2512 level
, chunk_size
, component_size
,
2513 component_size_alligment
);
2515 if (component_size_alligment
&& (level
!= 1) && (level
!= UnSet
)) {
2516 dprintf("imsm: reported component size alligned from %llu ",
2518 component_size
-= component_size_alligment
;
2519 dprintf("to %llu (%i).\n",
2520 component_size
, component_size_alligment
);
2523 return component_size
;
2526 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
2528 struct intel_super
*super
= st
->sb
;
2529 struct migr_record
*migr_rec
= super
->migr_rec
;
2530 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
2531 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2532 struct imsm_map
*prev_map
= get_imsm_map(dev
, MAP_1
);
2533 struct imsm_map
*map_to_analyse
= map
;
2536 int map_disks
= info
->array
.raid_disks
;
2538 memset(info
, 0, sizeof(*info
));
2540 map_to_analyse
= prev_map
;
2542 dl
= super
->current_disk
;
2544 info
->container_member
= super
->current_vol
;
2545 info
->array
.raid_disks
= map
->num_members
;
2546 info
->array
.level
= get_imsm_raid_level(map_to_analyse
);
2547 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
2548 info
->array
.md_minor
= -1;
2549 info
->array
.ctime
= 0;
2550 info
->array
.utime
= 0;
2551 info
->array
.chunk_size
=
2552 __le16_to_cpu(map_to_analyse
->blocks_per_strip
) << 9;
2553 info
->array
.state
= !dev
->vol
.dirty
;
2554 info
->custom_array_size
= __le32_to_cpu(dev
->size_high
);
2555 info
->custom_array_size
<<= 32;
2556 info
->custom_array_size
|= __le32_to_cpu(dev
->size_low
);
2557 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
2559 if (is_gen_migration(dev
)) {
2560 info
->reshape_active
= 1;
2561 info
->new_level
= get_imsm_raid_level(map
);
2562 info
->new_layout
= imsm_level_to_layout(info
->new_level
);
2563 info
->new_chunk
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
2564 info
->delta_disks
= map
->num_members
- prev_map
->num_members
;
2565 if (info
->delta_disks
) {
2566 /* this needs to be applied to every array
2569 info
->reshape_active
= CONTAINER_RESHAPE
;
2571 /* We shape information that we give to md might have to be
2572 * modify to cope with md's requirement for reshaping arrays.
2573 * For example, when reshaping a RAID0, md requires it to be
2574 * presented as a degraded RAID4.
2575 * Also if a RAID0 is migrating to a RAID5 we need to specify
2576 * the array as already being RAID5, but the 'before' layout
2577 * is a RAID4-like layout.
2579 switch (info
->array
.level
) {
2581 switch(info
->new_level
) {
2583 /* conversion is happening as RAID4 */
2584 info
->array
.level
= 4;
2585 info
->array
.raid_disks
+= 1;
2588 /* conversion is happening as RAID5 */
2589 info
->array
.level
= 5;
2590 info
->array
.layout
= ALGORITHM_PARITY_N
;
2591 info
->delta_disks
-= 1;
2594 /* FIXME error message */
2595 info
->array
.level
= UnSet
;
2601 info
->new_level
= UnSet
;
2602 info
->new_layout
= UnSet
;
2603 info
->new_chunk
= info
->array
.chunk_size
;
2604 info
->delta_disks
= 0;
2608 info
->disk
.major
= dl
->major
;
2609 info
->disk
.minor
= dl
->minor
;
2610 info
->disk
.number
= dl
->index
;
2611 info
->disk
.raid_disk
= get_imsm_disk_slot(map_to_analyse
,
2615 info
->data_offset
= pba_of_lba0(map_to_analyse
);
2616 info
->component_size
= blocks_per_member(map_to_analyse
);
2618 info
->component_size
= imsm_component_size_aligment_check(
2620 info
->array
.chunk_size
,
2621 info
->component_size
);
2623 memset(info
->uuid
, 0, sizeof(info
->uuid
));
2624 info
->recovery_start
= MaxSector
;
2626 info
->reshape_progress
= 0;
2627 info
->resync_start
= MaxSector
;
2628 if ((map_to_analyse
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
2630 imsm_reshape_blocks_arrays_changes(super
) == 0) {
2631 info
->resync_start
= 0;
2633 if (dev
->vol
.migr_state
) {
2634 switch (migr_type(dev
)) {
2637 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
2639 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
2641 info
->resync_start
= blocks_per_unit
* units
;
2644 case MIGR_GEN_MIGR
: {
2645 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
2647 __u64 units
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
2648 unsigned long long array_blocks
;
2651 if (__le32_to_cpu(migr_rec
->ascending_migr
) &&
2653 (__le32_to_cpu(migr_rec
->num_migr_units
)-1)) &&
2654 (super
->migr_rec
->rec_status
==
2655 __cpu_to_le32(UNIT_SRC_IN_CP_AREA
)))
2658 info
->reshape_progress
= blocks_per_unit
* units
;
2660 dprintf("IMSM: General Migration checkpoint : %llu "
2661 "(%llu) -> read reshape progress : %llu\n",
2662 (unsigned long long)units
,
2663 (unsigned long long)blocks_per_unit
,
2664 info
->reshape_progress
);
2666 used_disks
= imsm_num_data_members(dev
, MAP_1
);
2667 if (used_disks
> 0) {
2668 array_blocks
= blocks_per_member(map
) *
2670 /* round array size down to closest MB
2672 info
->custom_array_size
= (array_blocks
2673 >> SECT_PER_MB_SHIFT
)
2674 << SECT_PER_MB_SHIFT
;
2678 /* we could emulate the checkpointing of
2679 * 'sync_action=check' migrations, but for now
2680 * we just immediately complete them
2683 /* this is handled by container_content_imsm() */
2684 case MIGR_STATE_CHANGE
:
2685 /* FIXME handle other migrations */
2687 /* we are not dirty, so... */
2688 info
->resync_start
= MaxSector
;
2692 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
2693 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
2695 info
->array
.major_version
= -1;
2696 info
->array
.minor_version
= -2;
2697 devname
= devnum2devname(st
->container_dev
);
2698 *info
->text_version
= '\0';
2700 sprintf(info
->text_version
, "/%s/%d", devname
, info
->container_member
);
2702 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
2703 uuid_from_super_imsm(st
, info
->uuid
);
2707 for (i
=0; i
<map_disks
; i
++) {
2709 if (i
< info
->array
.raid_disks
) {
2710 struct imsm_disk
*dsk
;
2711 j
= get_imsm_disk_idx(dev
, i
, MAP_X
);
2712 dsk
= get_imsm_disk(super
, j
);
2713 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
2720 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
2721 int failed
, int look_in_map
);
2723 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
2728 static void manage_second_map(struct intel_super
*super
, struct imsm_dev
*dev
)
2730 if (is_gen_migration(dev
)) {
2733 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
2735 failed
= imsm_count_failed(super
, dev
, MAP_1
);
2736 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_1
);
2737 if (map2
->map_state
!= map_state
) {
2738 map2
->map_state
= map_state
;
2739 super
->updates_pending
++;
2745 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
2749 for (d
= super
->missing
; d
; d
= d
->next
)
2750 if (d
->index
== index
)
2755 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
2757 struct intel_super
*super
= st
->sb
;
2758 struct imsm_disk
*disk
;
2759 int map_disks
= info
->array
.raid_disks
;
2760 int max_enough
= -1;
2762 struct imsm_super
*mpb
;
2764 if (super
->current_vol
>= 0) {
2765 getinfo_super_imsm_volume(st
, info
, map
);
2768 memset(info
, 0, sizeof(*info
));
2770 /* Set raid_disks to zero so that Assemble will always pull in valid
2773 info
->array
.raid_disks
= 0;
2774 info
->array
.level
= LEVEL_CONTAINER
;
2775 info
->array
.layout
= 0;
2776 info
->array
.md_minor
= -1;
2777 info
->array
.ctime
= 0; /* N/A for imsm */
2778 info
->array
.utime
= 0;
2779 info
->array
.chunk_size
= 0;
2781 info
->disk
.major
= 0;
2782 info
->disk
.minor
= 0;
2783 info
->disk
.raid_disk
= -1;
2784 info
->reshape_active
= 0;
2785 info
->array
.major_version
= -1;
2786 info
->array
.minor_version
= -2;
2787 strcpy(info
->text_version
, "imsm");
2788 info
->safe_mode_delay
= 0;
2789 info
->disk
.number
= -1;
2790 info
->disk
.state
= 0;
2792 info
->recovery_start
= MaxSector
;
2793 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
2795 /* do we have the all the insync disks that we expect? */
2796 mpb
= super
->anchor
;
2798 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
2799 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
2800 int failed
, enough
, j
, missing
= 0;
2801 struct imsm_map
*map
;
2804 failed
= imsm_count_failed(super
, dev
, MAP_0
);
2805 state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
2806 map
= get_imsm_map(dev
, MAP_0
);
2808 /* any newly missing disks?
2809 * (catches single-degraded vs double-degraded)
2811 for (j
= 0; j
< map
->num_members
; j
++) {
2812 __u32 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
2813 __u32 idx
= ord_to_idx(ord
);
2815 if (!(ord
& IMSM_ORD_REBUILD
) &&
2816 get_imsm_missing(super
, idx
)) {
2822 if (state
== IMSM_T_STATE_FAILED
)
2824 else if (state
== IMSM_T_STATE_DEGRADED
&&
2825 (state
!= map
->map_state
|| missing
))
2827 else /* we're normal, or already degraded */
2829 if (is_gen_migration(dev
) && missing
) {
2830 /* during general migration we need all disks
2831 * that process is running on.
2832 * No new missing disk is allowed.
2836 /* no more checks necessary
2840 /* in the missing/failed disk case check to see
2841 * if at least one array is runnable
2843 max_enough
= max(max_enough
, enough
);
2845 dprintf("%s: enough: %d\n", __func__
, max_enough
);
2846 info
->container_enough
= max_enough
;
2849 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
2851 disk
= &super
->disks
->disk
;
2852 info
->data_offset
= total_blocks(&super
->disks
->disk
) - reserved
;
2853 info
->component_size
= reserved
;
2854 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
2855 /* we don't change info->disk.raid_disk here because
2856 * this state will be finalized in mdmon after we have
2857 * found the 'most fresh' version of the metadata
2859 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
2860 info
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
2863 /* only call uuid_from_super_imsm when this disk is part of a populated container,
2864 * ->compare_super may have updated the 'num_raid_devs' field for spares
2866 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
2867 uuid_from_super_imsm(st
, info
->uuid
);
2869 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
2871 /* I don't know how to compute 'map' on imsm, so use safe default */
2874 for (i
= 0; i
< map_disks
; i
++)
2880 /* allocates memory and fills disk in mdinfo structure
2881 * for each disk in array */
2882 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
2884 struct mdinfo
*mddev
= NULL
;
2885 struct intel_super
*super
= st
->sb
;
2886 struct imsm_disk
*disk
;
2889 if (!super
|| !super
->disks
)
2892 mddev
= xcalloc(1, sizeof(*mddev
));
2896 tmp
= xcalloc(1, sizeof(*tmp
));
2898 tmp
->next
= mddev
->devs
;
2900 tmp
->disk
.number
= count
++;
2901 tmp
->disk
.major
= dl
->major
;
2902 tmp
->disk
.minor
= dl
->minor
;
2903 tmp
->disk
.state
= is_configured(disk
) ?
2904 (1 << MD_DISK_ACTIVE
) : 0;
2905 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
2906 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
2907 tmp
->disk
.raid_disk
= -1;
2913 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
2914 char *update
, char *devname
, int verbose
,
2915 int uuid_set
, char *homehost
)
2917 /* For 'assemble' and 'force' we need to return non-zero if any
2918 * change was made. For others, the return value is ignored.
2919 * Update options are:
2920 * force-one : This device looks a bit old but needs to be included,
2921 * update age info appropriately.
2922 * assemble: clear any 'faulty' flag to allow this device to
2924 * force-array: Array is degraded but being forced, mark it clean
2925 * if that will be needed to assemble it.
2927 * newdev: not used ????
2928 * grow: Array has gained a new device - this is currently for
2930 * resync: mark as dirty so a resync will happen.
2931 * name: update the name - preserving the homehost
2932 * uuid: Change the uuid of the array to match watch is given
2934 * Following are not relevant for this imsm:
2935 * sparc2.2 : update from old dodgey metadata
2936 * super-minor: change the preferred_minor number
2937 * summaries: update redundant counters.
2938 * homehost: update the recorded homehost
2939 * _reshape_progress: record new reshape_progress position.
2942 struct intel_super
*super
= st
->sb
;
2943 struct imsm_super
*mpb
;
2945 /* we can only update container info */
2946 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
2949 mpb
= super
->anchor
;
2951 if (strcmp(update
, "uuid") == 0) {
2952 /* We take this to mean that the family_num should be updated.
2953 * However that is much smaller than the uuid so we cannot really
2954 * allow an explicit uuid to be given. And it is hard to reliably
2956 * So if !uuid_set we know the current uuid is random and just used
2957 * the first 'int' and copy it to the other 3 positions.
2958 * Otherwise we require the 4 'int's to be the same as would be the
2959 * case if we are using a random uuid. So an explicit uuid will be
2960 * accepted as long as all for ints are the same... which shouldn't hurt
2963 info
->uuid
[1] = info
->uuid
[2] = info
->uuid
[3] = info
->uuid
[0];
2966 if (info
->uuid
[0] != info
->uuid
[1] ||
2967 info
->uuid
[1] != info
->uuid
[2] ||
2968 info
->uuid
[2] != info
->uuid
[3])
2974 mpb
->orig_family_num
= info
->uuid
[0];
2975 } else if (strcmp(update
, "assemble") == 0)
2980 /* successful update? recompute checksum */
2982 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
2987 static size_t disks_to_mpb_size(int disks
)
2991 size
= sizeof(struct imsm_super
);
2992 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
2993 size
+= 2 * sizeof(struct imsm_dev
);
2994 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
2995 size
+= (4 - 2) * sizeof(struct imsm_map
);
2996 /* 4 possible disk_ord_tbl's */
2997 size
+= 4 * (disks
- 1) * sizeof(__u32
);
3002 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
,
3003 unsigned long long data_offset
)
3005 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
3008 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
3011 static void free_devlist(struct intel_super
*super
)
3013 struct intel_dev
*dv
;
3015 while (super
->devlist
) {
3016 dv
= super
->devlist
->next
;
3017 free(super
->devlist
->dev
);
3018 free(super
->devlist
);
3019 super
->devlist
= dv
;
3023 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
3025 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
3028 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
)
3032 * 0 same, or first was empty, and second was copied
3033 * 1 second had wrong number
3035 * 3 wrong other info
3037 struct intel_super
*first
= st
->sb
;
3038 struct intel_super
*sec
= tst
->sb
;
3045 /* in platform dependent environment test if the disks
3046 * use the same Intel hba
3047 * If not on Intel hba at all, allow anything.
3049 if (!check_env("IMSM_NO_PLATFORM")) {
3050 if (first
->hba
&& sec
->hba
&&
3051 strcmp(first
->hba
->path
, sec
->hba
->path
) != 0) {
3053 "HBAs of devices does not match %s != %s\n",
3054 first
->hba
? first
->hba
->path
: NULL
,
3055 sec
->hba
? sec
->hba
->path
: NULL
);
3060 /* if an anchor does not have num_raid_devs set then it is a free
3063 if (first
->anchor
->num_raid_devs
> 0 &&
3064 sec
->anchor
->num_raid_devs
> 0) {
3065 /* Determine if these disks might ever have been
3066 * related. Further disambiguation can only take place
3067 * in load_super_imsm_all
3069 __u32 first_family
= first
->anchor
->orig_family_num
;
3070 __u32 sec_family
= sec
->anchor
->orig_family_num
;
3072 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
3073 MAX_SIGNATURE_LENGTH
) != 0)
3076 if (first_family
== 0)
3077 first_family
= first
->anchor
->family_num
;
3078 if (sec_family
== 0)
3079 sec_family
= sec
->anchor
->family_num
;
3081 if (first_family
!= sec_family
)
3087 /* if 'first' is a spare promote it to a populated mpb with sec's
3090 if (first
->anchor
->num_raid_devs
== 0 &&
3091 sec
->anchor
->num_raid_devs
> 0) {
3093 struct intel_dev
*dv
;
3094 struct imsm_dev
*dev
;
3096 /* we need to copy raid device info from sec if an allocation
3097 * fails here we don't associate the spare
3099 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++) {
3100 dv
= xmalloc(sizeof(*dv
));
3101 dev
= xmalloc(sizeof_imsm_dev(get_imsm_dev(sec
, i
), 1));
3104 dv
->next
= first
->devlist
;
3105 first
->devlist
= dv
;
3107 if (i
< sec
->anchor
->num_raid_devs
) {
3108 /* allocation failure */
3109 free_devlist(first
);
3110 fprintf(stderr
, "imsm: failed to associate spare\n");
3113 first
->anchor
->num_raid_devs
= sec
->anchor
->num_raid_devs
;
3114 first
->anchor
->orig_family_num
= sec
->anchor
->orig_family_num
;
3115 first
->anchor
->family_num
= sec
->anchor
->family_num
;
3116 memcpy(first
->anchor
->sig
, sec
->anchor
->sig
, MAX_SIGNATURE_LENGTH
);
3117 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++)
3118 imsm_copy_dev(get_imsm_dev(first
, i
), get_imsm_dev(sec
, i
));
3124 static void fd2devname(int fd
, char *name
)
3128 char dname
[PATH_MAX
];
3133 if (fstat(fd
, &st
) != 0)
3135 sprintf(path
, "/sys/dev/block/%d:%d",
3136 major(st
.st_rdev
), minor(st
.st_rdev
));
3138 rv
= readlink(path
, dname
, sizeof(dname
)-1);
3143 nm
= strrchr(dname
, '/');
3146 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
3150 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
3152 static int imsm_read_serial(int fd
, char *devname
,
3153 __u8 serial
[MAX_RAID_SERIAL_LEN
])
3155 unsigned char scsi_serial
[255];
3164 memset(scsi_serial
, 0, sizeof(scsi_serial
));
3166 rv
= scsi_get_serial(fd
, scsi_serial
, sizeof(scsi_serial
));
3168 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
3169 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3170 fd2devname(fd
, (char *) serial
);
3176 pr_err("Failed to retrieve serial for %s\n",
3181 rsp_len
= scsi_serial
[3];
3184 pr_err("Failed to retrieve serial for %s\n",
3188 rsp_buf
= (char *) &scsi_serial
[4];
3190 /* trim all whitespace and non-printable characters and convert
3193 for (i
= 0, dest
= rsp_buf
; i
< rsp_len
; i
++) {
3196 /* ':' is reserved for use in placeholder serial
3197 * numbers for missing disks
3205 len
= dest
- rsp_buf
;
3208 /* truncate leading characters */
3209 if (len
> MAX_RAID_SERIAL_LEN
) {
3210 dest
+= len
- MAX_RAID_SERIAL_LEN
;
3211 len
= MAX_RAID_SERIAL_LEN
;
3214 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3215 memcpy(serial
, dest
, len
);
3220 static int serialcmp(__u8
*s1
, __u8
*s2
)
3222 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
3225 static void serialcpy(__u8
*dest
, __u8
*src
)
3227 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
3230 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
3234 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3235 if (serialcmp(dl
->serial
, serial
) == 0)
3241 static struct imsm_disk
*
3242 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
3246 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3247 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3249 if (serialcmp(disk
->serial
, serial
) == 0) {
3260 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3262 struct imsm_disk
*disk
;
3267 __u8 serial
[MAX_RAID_SERIAL_LEN
];
3269 rv
= imsm_read_serial(fd
, devname
, serial
);
3274 dl
= xcalloc(1, sizeof(*dl
));
3277 dl
->major
= major(stb
.st_rdev
);
3278 dl
->minor
= minor(stb
.st_rdev
);
3279 dl
->next
= super
->disks
;
3280 dl
->fd
= keep_fd
? fd
: -1;
3281 assert(super
->disks
== NULL
);
3283 serialcpy(dl
->serial
, serial
);
3286 fd2devname(fd
, name
);
3288 dl
->devname
= xstrdup(devname
);
3290 dl
->devname
= xstrdup(name
);
3292 /* look up this disk's index in the current anchor */
3293 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
3296 /* only set index on disks that are a member of a
3297 * populated contianer, i.e. one with raid_devs
3299 if (is_failed(&dl
->disk
))
3301 else if (is_spare(&dl
->disk
))
3309 /* When migrating map0 contains the 'destination' state while map1
3310 * contains the current state. When not migrating map0 contains the
3311 * current state. This routine assumes that map[0].map_state is set to
3312 * the current array state before being called.
3314 * Migration is indicated by one of the following states
3315 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
3316 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
3317 * map1state=unitialized)
3318 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
3320 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
3321 * map1state=degraded)
3322 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
3325 static void migrate(struct imsm_dev
*dev
, struct intel_super
*super
,
3326 __u8 to_state
, int migr_type
)
3328 struct imsm_map
*dest
;
3329 struct imsm_map
*src
= get_imsm_map(dev
, MAP_0
);
3331 dev
->vol
.migr_state
= 1;
3332 set_migr_type(dev
, migr_type
);
3333 dev
->vol
.curr_migr_unit
= 0;
3334 dest
= get_imsm_map(dev
, MAP_1
);
3336 /* duplicate and then set the target end state in map[0] */
3337 memcpy(dest
, src
, sizeof_imsm_map(src
));
3338 if ((migr_type
== MIGR_REBUILD
) ||
3339 (migr_type
== MIGR_GEN_MIGR
)) {
3343 for (i
= 0; i
< src
->num_members
; i
++) {
3344 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
3345 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
3349 if (migr_type
== MIGR_GEN_MIGR
)
3350 /* Clear migration record */
3351 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
3353 src
->map_state
= to_state
;
3356 static void end_migration(struct imsm_dev
*dev
, struct intel_super
*super
,
3359 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3360 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
== 0 ?
3364 /* merge any IMSM_ORD_REBUILD bits that were not successfully
3365 * completed in the last migration.
3367 * FIXME add support for raid-level-migration
3369 if ((map_state
!= map
->map_state
) && (is_gen_migration(dev
) == 0) &&
3370 (prev
->map_state
!= IMSM_T_STATE_UNINITIALIZED
)) {
3371 /* when final map state is other than expected
3372 * merge maps (not for migration)
3376 for (i
= 0; i
< prev
->num_members
; i
++)
3377 for (j
= 0; j
< map
->num_members
; j
++)
3378 /* during online capacity expansion
3379 * disks position can be changed
3380 * if takeover is used
3382 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
3383 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
3384 map
->disk_ord_tbl
[j
] |=
3385 prev
->disk_ord_tbl
[i
];
3388 failed
= imsm_count_failed(super
, dev
, MAP_0
);
3389 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
3392 dev
->vol
.migr_state
= 0;
3393 set_migr_type(dev
, 0);
3394 dev
->vol
.curr_migr_unit
= 0;
3395 map
->map_state
= map_state
;
3399 static int parse_raid_devices(struct intel_super
*super
)
3402 struct imsm_dev
*dev_new
;
3403 size_t len
, len_migr
;
3405 size_t space_needed
= 0;
3406 struct imsm_super
*mpb
= super
->anchor
;
3408 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3409 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3410 struct intel_dev
*dv
;
3412 len
= sizeof_imsm_dev(dev_iter
, 0);
3413 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
3415 space_needed
+= len_migr
- len
;
3417 dv
= xmalloc(sizeof(*dv
));
3418 if (max_len
< len_migr
)
3420 if (max_len
> len_migr
)
3421 space_needed
+= max_len
- len_migr
;
3422 dev_new
= xmalloc(max_len
);
3423 imsm_copy_dev(dev_new
, dev_iter
);
3426 dv
->next
= super
->devlist
;
3427 super
->devlist
= dv
;
3430 /* ensure that super->buf is large enough when all raid devices
3433 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
3436 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
, 512);
3437 if (posix_memalign(&buf
, 512, len
) != 0)
3440 memcpy(buf
, super
->buf
, super
->len
);
3441 memset(buf
+ super
->len
, 0, len
- super
->len
);
3450 /* retrieve a pointer to the bbm log which starts after all raid devices */
3451 struct bbm_log
*__get_imsm_bbm_log(struct imsm_super
*mpb
)
3455 if (__le32_to_cpu(mpb
->bbm_log_size
)) {
3457 ptr
+= mpb
->mpb_size
- __le32_to_cpu(mpb
->bbm_log_size
);
3463 /*******************************************************************************
3464 * Function: check_mpb_migr_compatibility
3465 * Description: Function checks for unsupported migration features:
3466 * - migration optimization area (pba_of_lba0)
3467 * - descending reshape (ascending_migr)
3469 * super : imsm metadata information
3471 * 0 : migration is compatible
3472 * -1 : migration is not compatible
3473 ******************************************************************************/
3474 int check_mpb_migr_compatibility(struct intel_super
*super
)
3476 struct imsm_map
*map0
, *map1
;
3477 struct migr_record
*migr_rec
= super
->migr_rec
;
3480 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3481 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3484 dev_iter
->vol
.migr_state
== 1 &&
3485 dev_iter
->vol
.migr_type
== MIGR_GEN_MIGR
) {
3486 /* This device is migrating */
3487 map0
= get_imsm_map(dev_iter
, MAP_0
);
3488 map1
= get_imsm_map(dev_iter
, MAP_1
);
3489 if (pba_of_lba0(map0
) != pba_of_lba0(map1
))
3490 /* migration optimization area was used */
3492 if (migr_rec
->ascending_migr
== 0
3493 && migr_rec
->dest_depth_per_unit
> 0)
3494 /* descending reshape not supported yet */
3501 static void __free_imsm(struct intel_super
*super
, int free_disks
);
3503 /* load_imsm_mpb - read matrix metadata
3504 * allocates super->mpb to be freed by free_imsm
3506 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
3508 unsigned long long dsize
;
3509 unsigned long long sectors
;
3511 struct imsm_super
*anchor
;
3514 get_dev_size(fd
, NULL
, &dsize
);
3517 pr_err("%s: device to small for imsm\n",
3522 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0) {
3524 pr_err("Cannot seek to anchor block on %s: %s\n",
3525 devname
, strerror(errno
));
3529 if (posix_memalign((void**)&anchor
, 512, 512) != 0) {
3531 pr_err("Failed to allocate imsm anchor buffer"
3532 " on %s\n", devname
);
3535 if (read(fd
, anchor
, 512) != 512) {
3537 pr_err("Cannot read anchor block on %s: %s\n",
3538 devname
, strerror(errno
));
3543 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
3545 pr_err("no IMSM anchor on %s\n", devname
);
3550 __free_imsm(super
, 0);
3551 /* reload capability and hba */
3553 /* capability and hba must be updated with new super allocation */
3554 find_intel_hba_capability(fd
, super
, devname
);
3555 super
->len
= ROUND_UP(anchor
->mpb_size
, 512);
3556 if (posix_memalign(&super
->buf
, 512, super
->len
) != 0) {
3558 pr_err("unable to allocate %zu byte mpb buffer\n",
3563 memcpy(super
->buf
, anchor
, 512);
3565 sectors
= mpb_sectors(anchor
) - 1;
3568 if (posix_memalign(&super
->migr_rec_buf
, 512, MIGR_REC_BUF_SIZE
) != 0) {
3569 pr_err("%s could not allocate migr_rec buffer\n", __func__
);
3573 super
->clean_migration_record_by_mdmon
= 0;
3576 check_sum
= __gen_imsm_checksum(super
->anchor
);
3577 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
3579 pr_err("IMSM checksum %x != %x on %s\n",
3581 __le32_to_cpu(super
->anchor
->check_sum
),
3589 /* read the extended mpb */
3590 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0) {
3592 pr_err("Cannot seek to extended mpb on %s: %s\n",
3593 devname
, strerror(errno
));
3597 if ((unsigned)read(fd
, super
->buf
+ 512, super
->len
- 512) != super
->len
- 512) {
3599 pr_err("Cannot read extended mpb on %s: %s\n",
3600 devname
, strerror(errno
));
3604 check_sum
= __gen_imsm_checksum(super
->anchor
);
3605 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
3607 pr_err("IMSM checksum %x != %x on %s\n",
3608 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
3613 /* FIXME the BBM log is disk specific so we cannot use this global
3614 * buffer for all disks. Ok for now since we only look at the global
3615 * bbm_log_size parameter to gate assembly
3617 super
->bbm_log
= __get_imsm_bbm_log(super
->anchor
);
3622 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
);
3624 /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
3625 static void clear_hi(struct intel_super
*super
)
3627 struct imsm_super
*mpb
= super
->anchor
;
3629 if (mpb
->attributes
& MPB_ATTRIB_2TB_DISK
)
3631 for (i
= 0; i
< mpb
->num_disks
; ++i
) {
3632 struct imsm_disk
*disk
= &mpb
->disk
[i
];
3633 disk
->total_blocks_hi
= 0;
3635 for (i
= 0; i
< mpb
->num_raid_devs
; ++i
) {
3636 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3639 for (n
= 0; n
< 2; ++n
) {
3640 struct imsm_map
*map
= get_imsm_map(dev
, n
);
3643 map
->pba_of_lba0_hi
= 0;
3644 map
->blocks_per_member_hi
= 0;
3645 map
->num_data_stripes_hi
= 0;
3651 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3655 err
= load_imsm_mpb(fd
, super
, devname
);
3658 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
3661 err
= parse_raid_devices(super
);
3666 static void __free_imsm_disk(struct dl
*d
)
3678 static void free_imsm_disks(struct intel_super
*super
)
3682 while (super
->disks
) {
3684 super
->disks
= d
->next
;
3685 __free_imsm_disk(d
);
3687 while (super
->disk_mgmt_list
) {
3688 d
= super
->disk_mgmt_list
;
3689 super
->disk_mgmt_list
= d
->next
;
3690 __free_imsm_disk(d
);
3692 while (super
->missing
) {
3694 super
->missing
= d
->next
;
3695 __free_imsm_disk(d
);
3700 /* free all the pieces hanging off of a super pointer */
3701 static void __free_imsm(struct intel_super
*super
, int free_disks
)
3703 struct intel_hba
*elem
, *next
;
3709 /* unlink capability description */
3711 if (super
->migr_rec_buf
) {
3712 free(super
->migr_rec_buf
);
3713 super
->migr_rec_buf
= NULL
;
3716 free_imsm_disks(super
);
3717 free_devlist(super
);
3721 free((void *)elem
->path
);
3729 static void free_imsm(struct intel_super
*super
)
3731 __free_imsm(super
, 1);
3735 static void free_super_imsm(struct supertype
*st
)
3737 struct intel_super
*super
= st
->sb
;
3746 static struct intel_super
*alloc_super(void)
3748 struct intel_super
*super
= xcalloc(1, sizeof(*super
));
3750 super
->current_vol
= -1;
3751 super
->create_offset
= ~((unsigned long long) 0);
3756 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
3758 static int find_intel_hba_capability(int fd
, struct intel_super
*super
, char *devname
)
3760 struct sys_dev
*hba_name
;
3763 if ((fd
< 0) || check_env("IMSM_NO_PLATFORM")) {
3768 hba_name
= find_disk_attached_hba(fd
, NULL
);
3771 pr_err("%s is not attached to Intel(R) RAID controller.\n",
3775 rv
= attach_hba_to_super(super
, hba_name
);
3778 struct intel_hba
*hba
= super
->hba
;
3780 pr_err("%s is attached to Intel(R) %s RAID "
3781 "controller (%s),\n"
3782 " but the container is assigned to Intel(R) "
3783 "%s RAID controller (",
3786 hba_name
->pci_id
? : "Err!",
3787 get_sys_dev_type(hba_name
->type
));
3790 fprintf(stderr
, "%s", hba
->pci_id
? : "Err!");
3792 fprintf(stderr
, ", ");
3796 fprintf(stderr
, ").\n"
3797 " Mixing devices attached to multiple controllers "
3798 "is not allowed.\n");
3802 super
->orom
= find_imsm_capability(hba_name
->type
);
3808 /* find_missing - helper routine for load_super_imsm_all that identifies
3809 * disks that have disappeared from the system. This routine relies on
3810 * the mpb being uptodate, which it is at load time.
3812 static int find_missing(struct intel_super
*super
)
3815 struct imsm_super
*mpb
= super
->anchor
;
3817 struct imsm_disk
*disk
;
3819 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3820 disk
= __get_imsm_disk(mpb
, i
);
3821 dl
= serial_to_dl(disk
->serial
, super
);
3825 dl
= xmalloc(sizeof(*dl
));
3829 dl
->devname
= xstrdup("missing");
3831 serialcpy(dl
->serial
, disk
->serial
);
3834 dl
->next
= super
->missing
;
3835 super
->missing
= dl
;
3842 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
3844 struct intel_disk
*idisk
= disk_list
;
3847 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
3849 idisk
= idisk
->next
;
3855 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
3856 struct intel_super
*super
,
3857 struct intel_disk
**disk_list
)
3859 struct imsm_disk
*d
= &super
->disks
->disk
;
3860 struct imsm_super
*mpb
= super
->anchor
;
3863 for (i
= 0; i
< tbl_size
; i
++) {
3864 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
3865 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
3867 if (tbl_mpb
->family_num
== mpb
->family_num
) {
3868 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
3869 dprintf("%s: mpb from %d:%d matches %d:%d\n",
3870 __func__
, super
->disks
->major
,
3871 super
->disks
->minor
,
3872 table
[i
]->disks
->major
,
3873 table
[i
]->disks
->minor
);
3877 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
3878 is_configured(d
) == is_configured(tbl_d
)) &&
3879 tbl_mpb
->generation_num
< mpb
->generation_num
) {
3880 /* current version of the mpb is a
3881 * better candidate than the one in
3882 * super_table, but copy over "cross
3883 * generational" status
3885 struct intel_disk
*idisk
;
3887 dprintf("%s: mpb from %d:%d replaces %d:%d\n",
3888 __func__
, super
->disks
->major
,
3889 super
->disks
->minor
,
3890 table
[i
]->disks
->major
,
3891 table
[i
]->disks
->minor
);
3893 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
3894 if (idisk
&& is_failed(&idisk
->disk
))
3895 tbl_d
->status
|= FAILED_DISK
;
3898 struct intel_disk
*idisk
;
3899 struct imsm_disk
*disk
;
3901 /* tbl_mpb is more up to date, but copy
3902 * over cross generational status before
3905 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
3906 if (disk
&& is_failed(disk
))
3907 d
->status
|= FAILED_DISK
;
3909 idisk
= disk_list_get(d
->serial
, *disk_list
);
3912 if (disk
&& is_configured(disk
))
3913 idisk
->disk
.status
|= CONFIGURED_DISK
;
3916 dprintf("%s: mpb from %d:%d prefer %d:%d\n",
3917 __func__
, super
->disks
->major
,
3918 super
->disks
->minor
,
3919 table
[i
]->disks
->major
,
3920 table
[i
]->disks
->minor
);
3928 table
[tbl_size
++] = super
;
3932 /* update/extend the merged list of imsm_disk records */
3933 for (j
= 0; j
< mpb
->num_disks
; j
++) {
3934 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
3935 struct intel_disk
*idisk
;
3937 idisk
= disk_list_get(disk
->serial
, *disk_list
);
3939 idisk
->disk
.status
|= disk
->status
;
3940 if (is_configured(&idisk
->disk
) ||
3941 is_failed(&idisk
->disk
))
3942 idisk
->disk
.status
&= ~(SPARE_DISK
);
3944 idisk
= xcalloc(1, sizeof(*idisk
));
3945 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
3946 idisk
->disk
= *disk
;
3947 idisk
->next
= *disk_list
;
3951 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
3958 static struct intel_super
*
3959 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
3962 struct imsm_super
*mpb
= super
->anchor
;
3966 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3967 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3968 struct intel_disk
*idisk
;
3970 idisk
= disk_list_get(disk
->serial
, disk_list
);
3972 if (idisk
->owner
== owner
||
3973 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
3976 dprintf("%s: '%.16s' owner %d != %d\n",
3977 __func__
, disk
->serial
, idisk
->owner
,
3980 dprintf("%s: unknown disk %x [%d]: %.16s\n",
3981 __func__
, __le32_to_cpu(mpb
->family_num
), i
,
3987 if (ok_count
== mpb
->num_disks
)
3992 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
3994 struct intel_super
*s
;
3996 for (s
= super_list
; s
; s
= s
->next
) {
3997 if (family_num
!= s
->anchor
->family_num
)
3999 fprintf(stderr
, "Conflict, offlining family %#x on '%s'\n",
4000 __le32_to_cpu(family_num
), s
->disks
->devname
);
4004 static struct intel_super
*
4005 imsm_thunderdome(struct intel_super
**super_list
, int len
)
4007 struct intel_super
*super_table
[len
];
4008 struct intel_disk
*disk_list
= NULL
;
4009 struct intel_super
*champion
, *spare
;
4010 struct intel_super
*s
, **del
;
4015 memset(super_table
, 0, sizeof(super_table
));
4016 for (s
= *super_list
; s
; s
= s
->next
)
4017 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
4019 for (i
= 0; i
< tbl_size
; i
++) {
4020 struct imsm_disk
*d
;
4021 struct intel_disk
*idisk
;
4022 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
4025 d
= &s
->disks
->disk
;
4027 /* 'd' must appear in merged disk list for its
4028 * configuration to be valid
4030 idisk
= disk_list_get(d
->serial
, disk_list
);
4031 if (idisk
&& idisk
->owner
== i
)
4032 s
= validate_members(s
, disk_list
, i
);
4037 dprintf("%s: marking family: %#x from %d:%d offline\n",
4038 __func__
, mpb
->family_num
,
4039 super_table
[i
]->disks
->major
,
4040 super_table
[i
]->disks
->minor
);
4044 /* This is where the mdadm implementation differs from the Windows
4045 * driver which has no strict concept of a container. We can only
4046 * assemble one family from a container, so when returning a prodigal
4047 * array member to this system the code will not be able to disambiguate
4048 * the container contents that should be assembled ("foreign" versus
4049 * "local"). It requires user intervention to set the orig_family_num
4050 * to a new value to establish a new container. The Windows driver in
4051 * this situation fixes up the volume name in place and manages the
4052 * foreign array as an independent entity.
4057 for (i
= 0; i
< tbl_size
; i
++) {
4058 struct intel_super
*tbl_ent
= super_table
[i
];
4064 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
4069 if (s
&& !is_spare
) {
4070 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
4072 } else if (!s
&& !is_spare
)
4085 fprintf(stderr
, "Chose family %#x on '%s', "
4086 "assemble conflicts to new container with '--update=uuid'\n",
4087 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
4089 /* collect all dl's onto 'champion', and update them to
4090 * champion's version of the status
4092 for (s
= *super_list
; s
; s
= s
->next
) {
4093 struct imsm_super
*mpb
= champion
->anchor
;
4094 struct dl
*dl
= s
->disks
;
4099 mpb
->attributes
|= s
->anchor
->attributes
& MPB_ATTRIB_2TB_DISK
;
4101 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4102 struct imsm_disk
*disk
;
4104 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
4107 /* only set index on disks that are a member of
4108 * a populated contianer, i.e. one with
4111 if (is_failed(&dl
->disk
))
4113 else if (is_spare(&dl
->disk
))
4119 if (i
>= mpb
->num_disks
) {
4120 struct intel_disk
*idisk
;
4122 idisk
= disk_list_get(dl
->serial
, disk_list
);
4123 if (idisk
&& is_spare(&idisk
->disk
) &&
4124 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
4132 dl
->next
= champion
->disks
;
4133 champion
->disks
= dl
;
4137 /* delete 'champion' from super_list */
4138 for (del
= super_list
; *del
; ) {
4139 if (*del
== champion
) {
4140 *del
= (*del
)->next
;
4143 del
= &(*del
)->next
;
4145 champion
->next
= NULL
;
4149 struct intel_disk
*idisk
= disk_list
;
4151 disk_list
= disk_list
->next
;
4160 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
);
4161 static int get_super_block(struct intel_super
**super_list
, int devnum
, char *devname
,
4162 int major
, int minor
, int keep_fd
);
4164 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4165 int *max
, int keep_fd
);
4168 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
4169 char *devname
, struct md_list
*devlist
,
4172 struct intel_super
*super_list
= NULL
;
4173 struct intel_super
*super
= NULL
;
4178 /* 'fd' is an opened container */
4179 err
= get_sra_super_block(fd
, &super_list
, devname
, &i
, keep_fd
);
4181 /* get super block from devlist devices */
4182 err
= get_devlist_super_block(devlist
, &super_list
, &i
, keep_fd
);
4185 /* all mpbs enter, maybe one leaves */
4186 super
= imsm_thunderdome(&super_list
, i
);
4192 if (find_missing(super
) != 0) {
4198 /* load migration record */
4199 err
= load_imsm_migr_rec(super
, NULL
);
4201 /* migration is in progress,
4202 * but migr_rec cannot be loaded,
4208 /* Check migration compatibility */
4209 if ((err
== 0) && (check_mpb_migr_compatibility(super
) != 0)) {
4210 pr_err("Unsupported migration detected");
4212 fprintf(stderr
, " on %s\n", devname
);
4214 fprintf(stderr
, " (IMSM).\n");
4223 while (super_list
) {
4224 struct intel_super
*s
= super_list
;
4226 super_list
= super_list
->next
;
4236 st
->container_dev
= fd2devnum(fd
);
4238 st
->container_dev
= NoMdDev
;
4239 if (err
== 0 && st
->ss
== NULL
) {
4240 st
->ss
= &super_imsm
;
4241 st
->minor_version
= 0;
4242 st
->max_devs
= IMSM_MAX_DEVICES
;
4249 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4250 int *max
, int keep_fd
)
4252 struct md_list
*tmpdev
;
4256 for (i
= 0, tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
4257 if (tmpdev
->used
!= 1)
4259 if (tmpdev
->container
== 1) {
4261 int fd
= dev_open(tmpdev
->devname
, O_RDONLY
|O_EXCL
);
4263 pr_err("cannot open device %s: %s\n",
4264 tmpdev
->devname
, strerror(errno
));
4268 err
= get_sra_super_block(fd
, super_list
,
4269 tmpdev
->devname
, &lmax
,
4278 int major
= major(tmpdev
->st_rdev
);
4279 int minor
= minor(tmpdev
->st_rdev
);
4280 err
= get_super_block(super_list
,
4297 static int get_super_block(struct intel_super
**super_list
, int devnum
, char *devname
,
4298 int major
, int minor
, int keep_fd
)
4300 struct intel_super
*s
= NULL
;
4312 sprintf(nm
, "%d:%d", major
, minor
);
4313 dfd
= dev_open(nm
, O_RDWR
);
4319 find_intel_hba_capability(dfd
, s
, devname
);
4320 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4322 /* retry the load if we might have raced against mdmon */
4323 if (err
== 3 && (devnum
!= -1) && mdmon_running(devnum
))
4324 for (retry
= 0; retry
< 3; retry
++) {
4326 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4332 s
->next
= *super_list
;
4340 if ((dfd
>= 0) && (!keep_fd
))
4347 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
)
4354 sra
= sysfs_read(fd
, 0, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
4358 if (sra
->array
.major_version
!= -1 ||
4359 sra
->array
.minor_version
!= -2 ||
4360 strcmp(sra
->text_version
, "imsm") != 0) {
4365 devnum
= fd2devnum(fd
);
4366 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
4367 if (get_super_block(super_list
, devnum
, devname
,
4368 sd
->disk
.major
, sd
->disk
.minor
, keep_fd
) != 0) {
4379 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
4381 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
, NULL
, 1);
4385 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
4387 struct intel_super
*super
;
4390 if (test_partition(fd
))
4391 /* IMSM not allowed on partitions */
4394 free_super_imsm(st
);
4396 super
= alloc_super();
4397 /* Load hba and capabilities if they exist.
4398 * But do not preclude loading metadata in case capabilities or hba are
4399 * non-compliant and ignore_hw_compat is set.
4401 rv
= find_intel_hba_capability(fd
, super
, devname
);
4402 /* no orom/efi or non-intel hba of the disk */
4403 if ((rv
!= 0) && (st
->ignore_hw_compat
== 0)) {
4405 pr_err("No OROM/EFI properties for %s\n", devname
);
4409 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
4413 pr_err("Failed to load all information "
4414 "sections on %s\n", devname
);
4420 if (st
->ss
== NULL
) {
4421 st
->ss
= &super_imsm
;
4422 st
->minor_version
= 0;
4423 st
->max_devs
= IMSM_MAX_DEVICES
;
4426 /* load migration record */
4427 if (load_imsm_migr_rec(super
, NULL
) == 0) {
4428 /* Check for unsupported migration features */
4429 if (check_mpb_migr_compatibility(super
) != 0) {
4430 pr_err("Unsupported migration detected");
4432 fprintf(stderr
, " on %s\n", devname
);
4434 fprintf(stderr
, " (IMSM).\n");
4442 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
4444 if (info
->level
== 1)
4446 return info
->chunk_size
>> 9;
4449 static unsigned long long info_to_blocks_per_member(mdu_array_info_t
*info
,
4450 unsigned long long size
)
4452 if (info
->level
== 1)
4455 return (size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
4458 static void imsm_update_version_info(struct intel_super
*super
)
4460 /* update the version and attributes */
4461 struct imsm_super
*mpb
= super
->anchor
;
4463 struct imsm_dev
*dev
;
4464 struct imsm_map
*map
;
4467 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4468 dev
= get_imsm_dev(super
, i
);
4469 map
= get_imsm_map(dev
, MAP_0
);
4470 if (__le32_to_cpu(dev
->size_high
) > 0)
4471 mpb
->attributes
|= MPB_ATTRIB_2TB
;
4473 /* FIXME detect when an array spans a port multiplier */
4475 mpb
->attributes
|= MPB_ATTRIB_PM
;
4478 if (mpb
->num_raid_devs
> 1 ||
4479 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
4480 version
= MPB_VERSION_ATTRIBS
;
4481 switch (get_imsm_raid_level(map
)) {
4482 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
4483 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
4484 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
4485 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
4488 if (map
->num_members
>= 5)
4489 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
4490 else if (dev
->status
== DEV_CLONE_N_GO
)
4491 version
= MPB_VERSION_CNG
;
4492 else if (get_imsm_raid_level(map
) == 5)
4493 version
= MPB_VERSION_RAID5
;
4494 else if (map
->num_members
>= 3)
4495 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
4496 else if (get_imsm_raid_level(map
) == 1)
4497 version
= MPB_VERSION_RAID1
;
4499 version
= MPB_VERSION_RAID0
;
4501 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
4505 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
4507 struct imsm_super
*mpb
= super
->anchor
;
4508 char *reason
= NULL
;
4511 if (strlen(name
) > MAX_RAID_SERIAL_LEN
)
4512 reason
= "must be 16 characters or less";
4514 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4515 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
4517 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
4518 reason
= "already exists";
4523 if (reason
&& !quiet
)
4524 pr_err("imsm volume name %s\n", reason
);
4529 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
4530 unsigned long long size
, char *name
,
4531 char *homehost
, int *uuid
,
4532 long long data_offset
)
4534 /* We are creating a volume inside a pre-existing container.
4535 * so st->sb is already set.
4537 struct intel_super
*super
= st
->sb
;
4538 struct imsm_super
*mpb
= super
->anchor
;
4539 struct intel_dev
*dv
;
4540 struct imsm_dev
*dev
;
4541 struct imsm_vol
*vol
;
4542 struct imsm_map
*map
;
4543 int idx
= mpb
->num_raid_devs
;
4545 unsigned long long array_blocks
;
4546 size_t size_old
, size_new
;
4547 unsigned long long num_data_stripes
;
4549 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
4550 pr_err("This imsm-container already has the "
4551 "maximum of %d volumes\n", super
->orom
->vpa
);
4555 /* ensure the mpb is large enough for the new data */
4556 size_old
= __le32_to_cpu(mpb
->mpb_size
);
4557 size_new
= disks_to_mpb_size(info
->nr_disks
);
4558 if (size_new
> size_old
) {
4560 size_t size_round
= ROUND_UP(size_new
, 512);
4562 if (posix_memalign(&mpb_new
, 512, size_round
) != 0) {
4563 pr_err("could not allocate new mpb\n");
4566 if (posix_memalign(&super
->migr_rec_buf
, 512,
4567 MIGR_REC_BUF_SIZE
) != 0) {
4568 pr_err("%s could not allocate migr_rec buffer\n",
4575 memcpy(mpb_new
, mpb
, size_old
);
4578 super
->anchor
= mpb_new
;
4579 mpb
->mpb_size
= __cpu_to_le32(size_new
);
4580 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
4582 super
->current_vol
= idx
;
4584 /* handle 'failed_disks' by either:
4585 * a) create dummy disk entries in the table if this the first
4586 * volume in the array. We add them here as this is the only
4587 * opportunity to add them. add_to_super_imsm_volume()
4588 * handles the non-failed disks and continues incrementing
4590 * b) validate that 'failed_disks' matches the current number
4591 * of missing disks if the container is populated
4593 if (super
->current_vol
== 0) {
4595 for (i
= 0; i
< info
->failed_disks
; i
++) {
4596 struct imsm_disk
*disk
;
4599 disk
= __get_imsm_disk(mpb
, i
);
4600 disk
->status
= CONFIGURED_DISK
| FAILED_DISK
;
4601 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
4602 snprintf((char *) disk
->serial
, MAX_RAID_SERIAL_LEN
,
4605 find_missing(super
);
4610 for (d
= super
->missing
; d
; d
= d
->next
)
4612 if (info
->failed_disks
> missing
) {
4613 pr_err("unable to add 'missing' disk to container\n");
4618 if (!check_name(super
, name
, 0))
4620 dv
= xmalloc(sizeof(*dv
));
4621 dev
= xcalloc(1, sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
4622 strncpy((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
4623 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
4624 info
->layout
, info
->chunk_size
,
4626 /* round array size down to closest MB */
4627 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
4629 dev
->size_low
= __cpu_to_le32((__u32
) array_blocks
);
4630 dev
->size_high
= __cpu_to_le32((__u32
) (array_blocks
>> 32));
4631 dev
->status
= (DEV_READ_COALESCING
| DEV_WRITE_COALESCING
);
4633 vol
->migr_state
= 0;
4634 set_migr_type(dev
, MIGR_INIT
);
4635 vol
->dirty
= !info
->state
;
4636 vol
->curr_migr_unit
= 0;
4637 map
= get_imsm_map(dev
, MAP_0
);
4638 set_pba_of_lba0(map
, super
->create_offset
);
4639 set_blocks_per_member(map
, info_to_blocks_per_member(info
, size
));
4640 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
4641 map
->failed_disk_num
= ~0;
4642 if (info
->level
> 0)
4643 map
->map_state
= (info
->state
? IMSM_T_STATE_NORMAL
4644 : IMSM_T_STATE_UNINITIALIZED
);
4646 map
->map_state
= info
->failed_disks
? IMSM_T_STATE_FAILED
:
4647 IMSM_T_STATE_NORMAL
;
4650 if (info
->level
== 1 && info
->raid_disks
> 2) {
4653 pr_err("imsm does not support more than 2 disks"
4654 "in a raid1 volume\n");
4658 map
->raid_level
= info
->level
;
4659 if (info
->level
== 10) {
4660 map
->raid_level
= 1;
4661 map
->num_domains
= info
->raid_disks
/ 2;
4662 } else if (info
->level
== 1)
4663 map
->num_domains
= info
->raid_disks
;
4665 map
->num_domains
= 1;
4667 /* info->size is only int so use the 'size' parameter instead */
4668 num_data_stripes
= (size
* 2) / info_to_blocks_per_strip(info
);
4669 num_data_stripes
/= map
->num_domains
;
4670 set_num_data_stripes(map
, num_data_stripes
);
4672 map
->num_members
= info
->raid_disks
;
4673 for (i
= 0; i
< map
->num_members
; i
++) {
4674 /* initialized in add_to_super */
4675 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
4677 mpb
->num_raid_devs
++;
4680 dv
->index
= super
->current_vol
;
4681 dv
->next
= super
->devlist
;
4682 super
->devlist
= dv
;
4684 imsm_update_version_info(super
);
4689 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
4690 unsigned long long size
, char *name
,
4691 char *homehost
, int *uuid
,
4692 unsigned long long data_offset
)
4694 /* This is primarily called by Create when creating a new array.
4695 * We will then get add_to_super called for each component, and then
4696 * write_init_super called to write it out to each device.
4697 * For IMSM, Create can create on fresh devices or on a pre-existing
4699 * To create on a pre-existing array a different method will be called.
4700 * This one is just for fresh drives.
4702 struct intel_super
*super
;
4703 struct imsm_super
*mpb
;
4707 if (data_offset
!= INVALID_SECTORS
) {
4708 fprintf(stderr
, Name
": data-offset not supported by imsm\n");
4713 return init_super_imsm_volume(st
, info
, size
, name
, homehost
, uuid
,
4717 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
4721 super
= alloc_super();
4722 if (super
&& posix_memalign(&super
->buf
, 512, mpb_size
) != 0) {
4727 pr_err("%s could not allocate superblock\n", __func__
);
4730 if (posix_memalign(&super
->migr_rec_buf
, 512, MIGR_REC_BUF_SIZE
) != 0) {
4731 pr_err("%s could not allocate migr_rec buffer\n", __func__
);
4736 memset(super
->buf
, 0, mpb_size
);
4738 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
4742 /* zeroing superblock */
4746 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
4748 version
= (char *) mpb
->sig
;
4749 strcpy(version
, MPB_SIGNATURE
);
4750 version
+= strlen(MPB_SIGNATURE
);
4751 strcpy(version
, MPB_VERSION_RAID0
);
4757 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
4758 int fd
, char *devname
)
4760 struct intel_super
*super
= st
->sb
;
4761 struct imsm_super
*mpb
= super
->anchor
;
4762 struct imsm_disk
*_disk
;
4763 struct imsm_dev
*dev
;
4764 struct imsm_map
*map
;
4768 dev
= get_imsm_dev(super
, super
->current_vol
);
4769 map
= get_imsm_map(dev
, MAP_0
);
4771 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
4772 pr_err("%s: Cannot add spare devices to IMSM volume\n",
4778 /* we're doing autolayout so grab the pre-marked (in
4779 * validate_geometry) raid_disk
4781 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4782 if (dl
->raiddisk
== dk
->raid_disk
)
4785 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4786 if (dl
->major
== dk
->major
&&
4787 dl
->minor
== dk
->minor
)
4792 pr_err("%s is not a member of the same container\n", devname
);
4796 /* add a pristine spare to the metadata */
4797 if (dl
->index
< 0) {
4798 dl
->index
= super
->anchor
->num_disks
;
4799 super
->anchor
->num_disks
++;
4801 /* Check the device has not already been added */
4802 slot
= get_imsm_disk_slot(map
, dl
->index
);
4804 (get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
) & IMSM_ORD_REBUILD
) == 0) {
4805 pr_err("%s has been included in this array twice\n",
4809 set_imsm_ord_tbl_ent(map
, dk
->raid_disk
, dl
->index
);
4810 dl
->disk
.status
= CONFIGURED_DISK
;
4812 /* update size of 'missing' disks to be at least as large as the
4813 * largest acitve member (we only have dummy missing disks when
4814 * creating the first volume)
4816 if (super
->current_vol
== 0) {
4817 for (df
= super
->missing
; df
; df
= df
->next
) {
4818 if (total_blocks(&dl
->disk
) > total_blocks(&df
->disk
))
4819 set_total_blocks(&df
->disk
, total_blocks(&dl
->disk
));
4820 _disk
= __get_imsm_disk(mpb
, df
->index
);
4825 /* refresh unset/failed slots to point to valid 'missing' entries */
4826 for (df
= super
->missing
; df
; df
= df
->next
)
4827 for (slot
= 0; slot
< mpb
->num_disks
; slot
++) {
4828 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
4830 if ((ord
& IMSM_ORD_REBUILD
) == 0)
4832 set_imsm_ord_tbl_ent(map
, slot
, df
->index
| IMSM_ORD_REBUILD
);
4833 if (is_gen_migration(dev
)) {
4834 struct imsm_map
*map2
= get_imsm_map(dev
,
4836 int slot2
= get_imsm_disk_slot(map2
, df
->index
);
4837 if ((slot2
< map2
->num_members
) &&
4839 __u32 ord2
= get_imsm_ord_tbl_ent(dev
,
4842 if ((unsigned)df
->index
==
4844 set_imsm_ord_tbl_ent(map2
,
4850 dprintf("set slot:%d to missing disk:%d\n", slot
, df
->index
);
4854 /* if we are creating the first raid device update the family number */
4855 if (super
->current_vol
== 0) {
4857 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
4859 _disk
= __get_imsm_disk(mpb
, dl
->index
);
4860 if (!_dev
|| !_disk
) {
4861 pr_err("BUG mpb setup error\n");
4867 sum
+= __gen_imsm_checksum(mpb
);
4868 mpb
->family_num
= __cpu_to_le32(sum
);
4869 mpb
->orig_family_num
= mpb
->family_num
;
4871 super
->current_disk
= dl
;
4876 * Function marks disk as spare and restores disk serial
4877 * in case it was previously marked as failed by takeover operation
4879 * -1 : critical error
4880 * 0 : disk is marked as spare but serial is not set
4883 int mark_spare(struct dl
*disk
)
4885 __u8 serial
[MAX_RAID_SERIAL_LEN
];
4892 if (!imsm_read_serial(disk
->fd
, NULL
, serial
)) {
4893 /* Restore disk serial number, because takeover marks disk
4894 * as failed and adds to serial ':0' before it becomes
4897 serialcpy(disk
->serial
, serial
);
4898 serialcpy(disk
->disk
.serial
, serial
);
4901 disk
->disk
.status
= SPARE_DISK
;
4907 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
4908 int fd
, char *devname
,
4909 unsigned long long data_offset
)
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
= xcalloc(sizeof(*dd
), 1);
4936 dd
->major
= major(stb
.st_rdev
);
4937 dd
->minor
= minor(stb
.st_rdev
);
4938 dd
->devname
= devname
? xstrdup(devname
) : NULL
;
4941 dd
->action
= DISK_ADD
;
4942 rv
= imsm_read_serial(fd
, devname
, dd
->serial
);
4944 pr_err("failed to retrieve scsi serial, aborting\n");
4949 get_dev_size(fd
, NULL
, &size
);
4951 serialcpy(dd
->disk
.serial
, dd
->serial
);
4952 set_total_blocks(&dd
->disk
, size
);
4953 if (__le32_to_cpu(dd
->disk
.total_blocks_hi
) > 0) {
4954 struct imsm_super
*mpb
= super
->anchor
;
4955 mpb
->attributes
|= MPB_ATTRIB_2TB_DISK
;
4958 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
4959 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
4961 dd
->disk
.scsi_id
= __cpu_to_le32(0);
4963 if (st
->update_tail
) {
4964 dd
->next
= super
->disk_mgmt_list
;
4965 super
->disk_mgmt_list
= dd
;
4967 dd
->next
= super
->disks
;
4969 super
->updates_pending
++;
4976 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
4978 struct intel_super
*super
= st
->sb
;
4981 /* remove from super works only in mdmon - for communication
4982 * manager - monitor. Check if communication memory buffer
4985 if (!st
->update_tail
) {
4986 pr_err("%s shall be used in mdmon context only"
4987 "(line %d).\n", __func__
, __LINE__
);
4990 dd
= xcalloc(1, sizeof(*dd
));
4991 dd
->major
= dk
->major
;
4992 dd
->minor
= dk
->minor
;
4995 dd
->action
= DISK_REMOVE
;
4997 dd
->next
= super
->disk_mgmt_list
;
4998 super
->disk_mgmt_list
= dd
;
5004 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
5008 struct imsm_super anchor
;
5009 } spare_record
__attribute__ ((aligned(512)));
5011 /* spare records have their own family number and do not have any defined raid
5014 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
5016 struct imsm_super
*mpb
= super
->anchor
;
5017 struct imsm_super
*spare
= &spare_record
.anchor
;
5021 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
)),
5022 spare
->generation_num
= __cpu_to_le32(1UL),
5023 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
5024 spare
->num_disks
= 1,
5025 spare
->num_raid_devs
= 0,
5026 spare
->cache_size
= mpb
->cache_size
,
5027 spare
->pwr_cycle_count
= __cpu_to_le32(1),
5029 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
5030 MPB_SIGNATURE MPB_VERSION_RAID0
);
5032 for (d
= super
->disks
; d
; d
= d
->next
) {
5036 spare
->disk
[0] = d
->disk
;
5037 if (__le32_to_cpu(d
->disk
.total_blocks_hi
) > 0)
5038 spare
->attributes
|= MPB_ATTRIB_2TB_DISK
;
5040 sum
= __gen_imsm_checksum(spare
);
5041 spare
->family_num
= __cpu_to_le32(sum
);
5042 spare
->orig_family_num
= 0;
5043 sum
= __gen_imsm_checksum(spare
);
5044 spare
->check_sum
= __cpu_to_le32(sum
);
5046 if (store_imsm_mpb(d
->fd
, spare
)) {
5047 fprintf(stderr
, "%s: failed for device %d:%d %s\n",
5048 __func__
, d
->major
, d
->minor
, strerror(errno
));
5060 static int write_super_imsm(struct supertype
*st
, int doclose
)
5062 struct intel_super
*super
= st
->sb
;
5063 struct imsm_super
*mpb
= super
->anchor
;
5069 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
5071 int clear_migration_record
= 1;
5073 /* 'generation' is incremented everytime the metadata is written */
5074 generation
= __le32_to_cpu(mpb
->generation_num
);
5076 mpb
->generation_num
= __cpu_to_le32(generation
);
5078 /* fix up cases where previous mdadm releases failed to set
5081 if (mpb
->orig_family_num
== 0)
5082 mpb
->orig_family_num
= mpb
->family_num
;
5084 for (d
= super
->disks
; d
; d
= d
->next
) {
5088 mpb
->disk
[d
->index
] = d
->disk
;
5092 for (d
= super
->missing
; d
; d
= d
->next
) {
5093 mpb
->disk
[d
->index
] = d
->disk
;
5096 mpb
->num_disks
= num_disks
;
5097 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
5099 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5100 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
5101 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
5103 imsm_copy_dev(dev
, dev2
);
5104 mpb_size
+= sizeof_imsm_dev(dev
, 0);
5106 if (is_gen_migration(dev2
))
5107 clear_migration_record
= 0;
5109 mpb_size
+= __le32_to_cpu(mpb
->bbm_log_size
);
5110 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5112 /* recalculate checksum */
5113 sum
= __gen_imsm_checksum(mpb
);
5114 mpb
->check_sum
= __cpu_to_le32(sum
);
5116 if (super
->clean_migration_record_by_mdmon
) {
5117 clear_migration_record
= 1;
5118 super
->clean_migration_record_by_mdmon
= 0;
5120 if (clear_migration_record
)
5121 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SIZE
);
5123 /* write the mpb for disks that compose raid devices */
5124 for (d
= super
->disks
; d
; d
= d
->next
) {
5125 if (d
->index
< 0 || is_failed(&d
->disk
))
5128 if (clear_migration_record
) {
5129 unsigned long long dsize
;
5131 get_dev_size(d
->fd
, NULL
, &dsize
);
5132 if (lseek64(d
->fd
, dsize
- 512, SEEK_SET
) >= 0) {
5133 if (write(d
->fd
, super
->migr_rec_buf
,
5134 MIGR_REC_BUF_SIZE
) != MIGR_REC_BUF_SIZE
)
5135 perror("Write migr_rec failed");
5139 if (store_imsm_mpb(d
->fd
, mpb
))
5141 "%s: failed for device %d:%d (fd: %d)%s\n",
5142 __func__
, d
->major
, d
->minor
,
5143 d
->fd
, strerror(errno
));
5152 return write_super_imsm_spares(super
, doclose
);
5158 static int create_array(struct supertype
*st
, int dev_idx
)
5161 struct imsm_update_create_array
*u
;
5162 struct intel_super
*super
= st
->sb
;
5163 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
5164 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
5165 struct disk_info
*inf
;
5166 struct imsm_disk
*disk
;
5169 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
5170 sizeof(*inf
) * map
->num_members
;
5172 u
->type
= update_create_array
;
5173 u
->dev_idx
= dev_idx
;
5174 imsm_copy_dev(&u
->dev
, dev
);
5175 inf
= get_disk_info(u
);
5176 for (i
= 0; i
< map
->num_members
; i
++) {
5177 int idx
= get_imsm_disk_idx(dev
, i
, MAP_X
);
5179 disk
= get_imsm_disk(super
, idx
);
5180 serialcpy(inf
[i
].serial
, disk
->serial
);
5182 append_metadata_update(st
, u
, len
);
5187 static int mgmt_disk(struct supertype
*st
)
5189 struct intel_super
*super
= st
->sb
;
5191 struct imsm_update_add_remove_disk
*u
;
5193 if (!super
->disk_mgmt_list
)
5198 u
->type
= update_add_remove_disk
;
5199 append_metadata_update(st
, u
, len
);
5204 static int write_init_super_imsm(struct supertype
*st
)
5206 struct intel_super
*super
= st
->sb
;
5207 int current_vol
= super
->current_vol
;
5209 /* we are done with current_vol reset it to point st at the container */
5210 super
->current_vol
= -1;
5212 if (st
->update_tail
) {
5213 /* queue the recently created array / added disk
5214 * as a metadata update */
5217 /* determine if we are creating a volume or adding a disk */
5218 if (current_vol
< 0) {
5219 /* in the mgmt (add/remove) disk case we are running
5220 * in mdmon context, so don't close fd's
5222 return mgmt_disk(st
);
5224 rv
= create_array(st
, current_vol
);
5229 for (d
= super
->disks
; d
; d
= d
->next
)
5230 Kill(d
->devname
, NULL
, 0, -1, 1);
5231 return write_super_imsm(st
, 1);
5236 static int store_super_imsm(struct supertype
*st
, int fd
)
5238 struct intel_super
*super
= st
->sb
;
5239 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
5245 return store_imsm_mpb(fd
, mpb
);
5251 static int imsm_bbm_log_size(struct imsm_super
*mpb
)
5253 return __le32_to_cpu(mpb
->bbm_log_size
);
5257 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
5258 int layout
, int raiddisks
, int chunk
,
5259 unsigned long long size
,
5260 unsigned long long data_offset
,
5262 unsigned long long *freesize
,
5266 unsigned long long ldsize
;
5267 struct intel_super
*super
=NULL
;
5270 if (level
!= LEVEL_CONTAINER
)
5275 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
5278 pr_err("imsm: Cannot open %s: %s\n",
5279 dev
, strerror(errno
));
5282 if (!get_dev_size(fd
, dev
, &ldsize
)) {
5287 /* capabilities retrieve could be possible
5288 * note that there is no fd for the disks in array.
5290 super
= alloc_super();
5291 rv
= find_intel_hba_capability(fd
, super
, verbose
> 0 ? dev
: NULL
);
5295 fd2devname(fd
, str
);
5296 dprintf("validate_geometry_imsm_container: fd: %d %s orom: %p rv: %d raiddisk: %d\n",
5297 fd
, str
, super
->orom
, rv
, raiddisks
);
5299 /* no orom/efi or non-intel hba of the disk */
5306 if (raiddisks
> super
->orom
->tds
) {
5308 pr_err("%d exceeds maximum number of"
5309 " platform supported disks: %d\n",
5310 raiddisks
, super
->orom
->tds
);
5314 if ((super
->orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) == 0 &&
5315 (ldsize
>> 9) >> 32 > 0) {
5317 pr_err("%s exceeds maximum platform supported size\n", dev
);
5323 *freesize
= avail_size_imsm(st
, ldsize
>> 9, data_offset
);
5329 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
5331 const unsigned long long base_start
= e
[*idx
].start
;
5332 unsigned long long end
= base_start
+ e
[*idx
].size
;
5335 if (base_start
== end
)
5339 for (i
= *idx
; i
< num_extents
; i
++) {
5340 /* extend overlapping extents */
5341 if (e
[i
].start
>= base_start
&&
5342 e
[i
].start
<= end
) {
5345 if (e
[i
].start
+ e
[i
].size
> end
)
5346 end
= e
[i
].start
+ e
[i
].size
;
5347 } else if (e
[i
].start
> end
) {
5353 return end
- base_start
;
5356 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
5358 /* build a composite disk with all known extents and generate a new
5359 * 'maxsize' given the "all disks in an array must share a common start
5360 * offset" constraint
5362 struct extent
*e
= xcalloc(sum_extents
, sizeof(*e
));
5366 unsigned long long pos
;
5367 unsigned long long start
= 0;
5368 unsigned long long maxsize
;
5369 unsigned long reserve
;
5371 /* coalesce and sort all extents. also, check to see if we need to
5372 * reserve space between member arrays
5375 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5378 for (i
= 0; i
< dl
->extent_cnt
; i
++)
5381 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
5386 while (i
< sum_extents
) {
5387 e
[j
].start
= e
[i
].start
;
5388 e
[j
].size
= find_size(e
, &i
, sum_extents
);
5390 if (e
[j
-1].size
== 0)
5399 unsigned long long esize
;
5401 esize
= e
[i
].start
- pos
;
5402 if (esize
>= maxsize
) {
5407 pos
= e
[i
].start
+ e
[i
].size
;
5409 } while (e
[i
-1].size
);
5415 /* FIXME assumes volume at offset 0 is the first volume in a
5418 if (start_extent
> 0)
5419 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
5423 if (maxsize
< reserve
)
5426 super
->create_offset
= ~((unsigned long long) 0);
5427 if (start
+ reserve
> super
->create_offset
)
5428 return 0; /* start overflows create_offset */
5429 super
->create_offset
= start
+ reserve
;
5431 return maxsize
- reserve
;
5434 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
5436 if (level
< 0 || level
== 6 || level
== 4)
5439 /* if we have an orom prevent invalid raid levels */
5442 case 0: return imsm_orom_has_raid0(orom
);
5445 return imsm_orom_has_raid1e(orom
);
5446 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
5447 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
5448 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
5451 return 1; /* not on an Intel RAID platform so anything goes */
5458 active_arrays_by_format(char *name
, char* hba
, struct md_list
**devlist
,
5459 int dpa
, int verbose
)
5461 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
5462 struct mdstat_ent
*memb
= NULL
;
5465 struct md_list
*dv
= NULL
;
5468 for (memb
= mdstat
; memb
; memb
= memb
->next
) {
5469 if (memb
->metadata_version
&&
5470 (strncmp(memb
->metadata_version
, "external:", 9) == 0) &&
5471 (strcmp(&memb
->metadata_version
[9], name
) == 0) &&
5472 !is_subarray(memb
->metadata_version
+9) &&
5474 struct dev_member
*dev
= memb
->members
;
5476 while(dev
&& (fd
< 0)) {
5477 char *path
= xmalloc(strlen(dev
->name
) + strlen("/dev/") + 1);
5478 num
= sprintf(path
, "%s%s", "/dev/", dev
->name
);
5480 fd
= open(path
, O_RDONLY
, 0);
5481 if ((num
<= 0) || (fd
< 0)) {
5482 pr_vrb(": Cannot open %s: %s\n",
5483 dev
->name
, strerror(errno
));
5489 if ((fd
>= 0) && disk_attached_to_hba(fd
, hba
)) {
5490 struct mdstat_ent
*vol
;
5491 for (vol
= mdstat
; vol
; vol
= vol
->next
) {
5492 if ((vol
->active
> 0) &&
5493 vol
->metadata_version
&&
5494 is_container_member(vol
, memb
->dev
)) {
5499 if (*devlist
&& (found
< dpa
)) {
5500 dv
= xcalloc(1, sizeof(*dv
));
5501 dv
->devname
= xmalloc(strlen(memb
->dev
) + strlen("/dev/") + 1);
5502 sprintf(dv
->devname
, "%s%s", "/dev/", memb
->dev
);
5505 dv
->next
= *devlist
;
5513 free_mdstat(mdstat
);
5518 static struct md_list
*
5519 get_loop_devices(void)
5522 struct md_list
*devlist
= NULL
;
5523 struct md_list
*dv
= NULL
;
5525 for(i
= 0; i
< 12; i
++) {
5526 dv
= xcalloc(1, sizeof(*dv
));
5527 dv
->devname
= xmalloc(40);
5528 sprintf(dv
->devname
, "/dev/loop%d", i
);
5536 static struct md_list
*
5537 get_devices(const char *hba_path
)
5539 struct md_list
*devlist
= NULL
;
5540 struct md_list
*dv
= NULL
;
5546 devlist
= get_loop_devices();
5549 /* scroll through /sys/dev/block looking for devices attached to
5552 dir
= opendir("/sys/dev/block");
5553 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
5558 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
5560 path
= devt_to_devpath(makedev(major
, minor
));
5563 if (!path_attached_to_hba(path
, hba_path
)) {
5570 fd
= dev_open(ent
->d_name
, O_RDONLY
);
5572 fd2devname(fd
, buf
);
5575 pr_err("cannot open device: %s\n",
5581 dv
= xcalloc(1, sizeof(*dv
));
5582 dv
->devname
= xstrdup(buf
);
5589 devlist
= devlist
->next
;
5599 count_volumes_list(struct md_list
*devlist
, char *homehost
,
5600 int verbose
, int *found
)
5602 struct md_list
*tmpdev
;
5604 struct supertype
*st
= NULL
;
5606 /* first walk the list of devices to find a consistent set
5607 * that match the criterea, if that is possible.
5608 * We flag the ones we like with 'used'.
5611 st
= match_metadata_desc_imsm("imsm");
5613 pr_vrb(": cannot allocate memory for imsm supertype\n");
5617 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5618 char *devname
= tmpdev
->devname
;
5620 struct supertype
*tst
;
5622 if (tmpdev
->used
> 1)
5624 tst
= dup_super(st
);
5626 pr_vrb(": cannot allocate memory for imsm supertype\n");
5629 tmpdev
->container
= 0;
5630 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
5632 dprintf(": cannot open device %s: %s\n",
5633 devname
, strerror(errno
));
5635 } else if (fstat(dfd
, &stb
)< 0) {
5637 dprintf(": fstat failed for %s: %s\n",
5638 devname
, strerror(errno
));
5640 } else if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
5641 dprintf(": %s is not a block device.\n",
5644 } else if (must_be_container(dfd
)) {
5645 struct supertype
*cst
;
5646 cst
= super_by_fd(dfd
, NULL
);
5648 dprintf(": cannot recognize container type %s\n",
5651 } else if (tst
->ss
!= st
->ss
) {
5652 dprintf(": non-imsm container - ignore it: %s\n",
5655 } else if (!tst
->ss
->load_container
||
5656 tst
->ss
->load_container(tst
, dfd
, NULL
))
5659 tmpdev
->container
= 1;
5662 cst
->ss
->free_super(cst
);
5664 tmpdev
->st_rdev
= stb
.st_rdev
;
5665 if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
5666 dprintf(": no RAID superblock on %s\n",
5669 } else if (tst
->ss
->compare_super
== NULL
) {
5670 dprintf(": Cannot assemble %s metadata on %s\n",
5671 tst
->ss
->name
, devname
);
5677 if (tmpdev
->used
== 2 || tmpdev
->used
== 4) {
5678 /* Ignore unrecognised devices during auto-assembly */
5683 tst
->ss
->getinfo_super(tst
, &info
, NULL
);
5685 if (st
->minor_version
== -1)
5686 st
->minor_version
= tst
->minor_version
;
5688 if (memcmp(info
.uuid
, uuid_zero
,
5689 sizeof(int[4])) == 0) {
5690 /* this is a floating spare. It cannot define
5691 * an array unless there are no more arrays of
5692 * this type to be found. It can be included
5693 * in an array of this type though.
5699 if (st
->ss
!= tst
->ss
||
5700 st
->minor_version
!= tst
->minor_version
||
5701 st
->ss
->compare_super(st
, tst
) != 0) {
5702 /* Some mismatch. If exactly one array matches this host,
5703 * we can resolve on that one.
5704 * Or, if we are auto assembling, we just ignore the second
5707 dprintf(": superblock on %s doesn't match others - assembly aborted\n",
5713 dprintf("found: devname: %s\n", devname
);
5717 tst
->ss
->free_super(tst
);
5721 if ((err
= load_super_imsm_all(st
, -1, &st
->sb
, NULL
, devlist
, 0)) == 0) {
5722 struct mdinfo
*iter
, *head
= st
->ss
->container_content(st
, NULL
);
5723 for (iter
= head
; iter
; iter
= iter
->next
) {
5724 dprintf("content->text_version: %s vol\n",
5725 iter
->text_version
);
5726 if (iter
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
5727 /* do not assemble arrays with unsupported
5729 dprintf(": Cannot activate member %s.\n",
5730 iter
->text_version
);
5737 dprintf(" no valid super block on device list: err: %d %p\n",
5741 dprintf(" no more devices to examin\n");
5744 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5745 if ((tmpdev
->used
== 1) && (tmpdev
->found
)) {
5747 if (count
< tmpdev
->found
)
5750 count
-= tmpdev
->found
;
5753 if (tmpdev
->used
== 1)
5758 st
->ss
->free_super(st
);
5764 count_volumes(char *hba
, int dpa
, int verbose
)
5766 struct md_list
*devlist
= NULL
;
5770 devlist
= get_devices(hba
);
5771 /* if no intel devices return zero volumes */
5772 if (devlist
== NULL
)
5775 count
= active_arrays_by_format("imsm", hba
, &devlist
, dpa
, verbose
);
5776 dprintf(" path: %s active arrays: %d\n", hba
, count
);
5777 if (devlist
== NULL
)
5781 count
+= count_volumes_list(devlist
,
5785 dprintf("found %d count: %d\n", found
, count
);
5788 dprintf("path: %s total number of volumes: %d\n", hba
, count
);
5791 struct md_list
*dv
= devlist
;
5792 devlist
= devlist
->next
;
5799 static int imsm_default_chunk(const struct imsm_orom
*orom
)
5801 /* up to 512 if the plaform supports it, otherwise the platform max.
5802 * 128 if no platform detected
5804 int fs
= max(7, orom
? fls(orom
->sss
) : 0);
5806 return min(512, (1 << fs
));
5810 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
5811 int raiddisks
, int *chunk
, unsigned long long size
, int verbose
)
5813 /* check/set platform and metadata limits/defaults */
5814 if (super
->orom
&& raiddisks
> super
->orom
->dpa
) {
5815 pr_vrb(": platform supports a maximum of %d disks per array\n",
5820 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
5821 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
5822 pr_vrb(": platform does not support raid%d with %d disk%s\n",
5823 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
5827 if (chunk
&& (*chunk
== 0 || *chunk
== UnSet
))
5828 *chunk
= imsm_default_chunk(super
->orom
);
5830 if (super
->orom
&& chunk
&& !imsm_orom_has_chunk(super
->orom
, *chunk
)) {
5831 pr_vrb(": platform does not support a chunk size of: "
5836 if (layout
!= imsm_level_to_layout(level
)) {
5838 pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n");
5839 else if (level
== 10)
5840 pr_vrb(": imsm raid 10 only supports the n2 layout\n");
5842 pr_vrb(": imsm unknown layout %#x for this raid level %d\n",
5847 if (super
->orom
&& (super
->orom
->attr
& IMSM_OROM_ATTR_2TB
) == 0 && chunk
&&
5848 (calc_array_size(level
, raiddisks
, layout
, *chunk
, size
) >> 32) > 0) {
5849 pr_vrb(": platform does not support a volume size over 2TB\n");
5855 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
5856 * FIX ME add ahci details
5858 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
5859 int layout
, int raiddisks
, int *chunk
,
5860 unsigned long long size
,
5861 unsigned long long data_offset
,
5863 unsigned long long *freesize
,
5867 struct intel_super
*super
= st
->sb
;
5868 struct imsm_super
*mpb
;
5870 unsigned long long pos
= 0;
5871 unsigned long long maxsize
;
5875 /* We must have the container info already read in. */
5879 mpb
= super
->anchor
;
5881 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, size
, verbose
)) {
5882 pr_err("RAID gemetry validation failed. "
5883 "Cannot proceed with the action(s).\n");
5887 /* General test: make sure there is space for
5888 * 'raiddisks' device extents of size 'size' at a given
5891 unsigned long long minsize
= size
;
5892 unsigned long long start_offset
= MaxSector
;
5895 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
5896 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5901 e
= get_extents(super
, dl
);
5904 unsigned long long esize
;
5905 esize
= e
[i
].start
- pos
;
5906 if (esize
>= minsize
)
5908 if (found
&& start_offset
== MaxSector
) {
5911 } else if (found
&& pos
!= start_offset
) {
5915 pos
= e
[i
].start
+ e
[i
].size
;
5917 } while (e
[i
-1].size
);
5922 if (dcnt
< raiddisks
) {
5924 pr_err("imsm: Not enough "
5925 "devices with space for this array "
5933 /* This device must be a member of the set */
5934 if (stat(dev
, &stb
) < 0)
5936 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
5938 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5939 if (dl
->major
== (int)major(stb
.st_rdev
) &&
5940 dl
->minor
== (int)minor(stb
.st_rdev
))
5945 pr_err("%s is not in the "
5946 "same imsm set\n", dev
);
5948 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
5949 /* If a volume is present then the current creation attempt
5950 * cannot incorporate new spares because the orom may not
5951 * understand this configuration (all member disks must be
5952 * members of each array in the container).
5954 pr_err("%s is a spare and a volume"
5955 " is already defined for this container\n", dev
);
5956 pr_err("The option-rom requires all member"
5957 " disks to be a member of all volumes\n");
5959 } else if (super
->orom
&& mpb
->num_raid_devs
> 0 &&
5960 mpb
->num_disks
!= raiddisks
) {
5961 pr_err("The option-rom requires all member"
5962 " disks to be a member of all volumes\n");
5966 /* retrieve the largest free space block */
5967 e
= get_extents(super
, dl
);
5972 unsigned long long esize
;
5974 esize
= e
[i
].start
- pos
;
5975 if (esize
>= maxsize
)
5977 pos
= e
[i
].start
+ e
[i
].size
;
5979 } while (e
[i
-1].size
);
5984 pr_err("unable to determine free space for: %s\n",
5988 if (maxsize
< size
) {
5990 pr_err("%s not enough space (%llu < %llu)\n",
5991 dev
, maxsize
, size
);
5995 /* count total number of extents for merge */
5997 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5999 i
+= dl
->extent_cnt
;
6001 maxsize
= merge_extents(super
, i
);
6003 if (!check_env("IMSM_NO_PLATFORM") &&
6004 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
6005 pr_err("attempting to create a second "
6006 "volume with size less then remaining space. "
6011 if (maxsize
< size
|| maxsize
== 0) {
6014 pr_err("no free space"
6015 " left on device. Aborting...\n");
6017 pr_err("not enough space"
6018 " to create volume of given size"
6019 " (%llu < %llu). Aborting...\n",
6025 *freesize
= maxsize
;
6028 int count
= count_volumes(super
->hba
->path
,
6029 super
->orom
->dpa
, verbose
);
6030 if (super
->orom
->vphba
<= count
) {
6031 pr_vrb(": platform does not support more than %d raid volumes.\n",
6032 super
->orom
->vphba
);
6039 static int imsm_get_free_size(struct supertype
*st
, int raiddisks
,
6040 unsigned long long size
, int chunk
,
6041 unsigned long long *freesize
)
6043 struct intel_super
*super
= st
->sb
;
6044 struct imsm_super
*mpb
= super
->anchor
;
6049 unsigned long long maxsize
;
6050 unsigned long long minsize
;
6054 /* find the largest common start free region of the possible disks */
6058 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6064 /* don't activate new spares if we are orom constrained
6065 * and there is already a volume active in the container
6067 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
6070 e
= get_extents(super
, dl
);
6073 for (i
= 1; e
[i
-1].size
; i
++)
6081 maxsize
= merge_extents(super
, extent_cnt
);
6085 minsize
= chunk
* 2;
6087 if (cnt
< raiddisks
||
6088 (super
->orom
&& used
&& used
!= raiddisks
) ||
6089 maxsize
< minsize
||
6091 pr_err("not enough devices with space to create array.\n");
6092 return 0; /* No enough free spaces large enough */
6103 if (!check_env("IMSM_NO_PLATFORM") &&
6104 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
6105 pr_err("attempting to create a second "
6106 "volume with size less then remaining space. "
6111 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6113 dl
->raiddisk
= cnt
++;
6117 dprintf("imsm: imsm_get_free_size() returns : %llu\n", size
);
6122 static int reserve_space(struct supertype
*st
, int raiddisks
,
6123 unsigned long long size
, int chunk
,
6124 unsigned long long *freesize
)
6126 struct intel_super
*super
= st
->sb
;
6131 rv
= imsm_get_free_size(st
, raiddisks
, size
, chunk
, freesize
);
6134 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6136 dl
->raiddisk
= cnt
++;
6143 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
6144 int raiddisks
, int *chunk
, unsigned long long size
,
6145 unsigned long long data_offset
,
6146 char *dev
, unsigned long long *freesize
,
6154 * if given unused devices create a container
6155 * if given given devices in a container create a member volume
6157 if (level
== LEVEL_CONTAINER
) {
6158 /* Must be a fresh device to add to a container */
6159 return validate_geometry_imsm_container(st
, level
, layout
,
6169 struct intel_super
*super
= st
->sb
;
6170 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
6171 raiddisks
, chunk
, size
,
6174 /* we are being asked to automatically layout a
6175 * new volume based on the current contents of
6176 * the container. If the the parameters can be
6177 * satisfied reserve_space will record the disks,
6178 * start offset, and size of the volume to be
6179 * created. add_to_super and getinfo_super
6180 * detect when autolayout is in progress.
6182 /* assuming that freesize is always given when array is
6184 if (super
->orom
&& freesize
) {
6186 count
= count_volumes(super
->hba
->path
,
6187 super
->orom
->dpa
, verbose
);
6188 if (super
->orom
->vphba
<= count
) {
6189 pr_vrb(": platform does not support more"
6190 " than %d raid volumes.\n",
6191 super
->orom
->vphba
);
6196 return reserve_space(st
, raiddisks
, size
,
6197 chunk
?*chunk
:0, freesize
);
6202 /* creating in a given container */
6203 return validate_geometry_imsm_volume(st
, level
, layout
,
6204 raiddisks
, chunk
, size
,
6206 dev
, freesize
, verbose
);
6209 /* This device needs to be a device in an 'imsm' container */
6210 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
6213 pr_err("Cannot create this array on device %s\n",
6218 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
6220 pr_err("Cannot open %s: %s\n",
6221 dev
, strerror(errno
));
6224 /* Well, it is in use by someone, maybe an 'imsm' container. */
6225 cfd
= open_container(fd
);
6229 pr_err("Cannot use %s: It is busy\n",
6233 sra
= sysfs_read(cfd
, 0, GET_VERSION
);
6234 if (sra
&& sra
->array
.major_version
== -1 &&
6235 strcmp(sra
->text_version
, "imsm") == 0)
6239 /* This is a member of a imsm container. Load the container
6240 * and try to create a volume
6242 struct intel_super
*super
;
6244 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
, NULL
, 1) == 0) {
6246 st
->container_dev
= fd2devnum(cfd
);
6248 return validate_geometry_imsm_volume(st
, level
, layout
,
6250 size
, data_offset
, dev
,
6257 pr_err("failed container membership check\n");
6263 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
6265 struct intel_super
*super
= st
->sb
;
6267 if (level
&& *level
== UnSet
)
6268 *level
= LEVEL_CONTAINER
;
6270 if (level
&& layout
&& *layout
== UnSet
)
6271 *layout
= imsm_level_to_layout(*level
);
6273 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0))
6274 *chunk
= imsm_default_chunk(super
->orom
);
6277 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
6279 static int kill_subarray_imsm(struct supertype
*st
)
6281 /* remove the subarray currently referenced by ->current_vol */
6283 struct intel_dev
**dp
;
6284 struct intel_super
*super
= st
->sb
;
6285 __u8 current_vol
= super
->current_vol
;
6286 struct imsm_super
*mpb
= super
->anchor
;
6288 if (super
->current_vol
< 0)
6290 super
->current_vol
= -1; /* invalidate subarray cursor */
6292 /* block deletions that would change the uuid of active subarrays
6294 * FIXME when immutable ids are available, but note that we'll
6295 * also need to fixup the invalidated/active subarray indexes in
6298 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6301 if (i
< current_vol
)
6303 sprintf(subarray
, "%u", i
);
6304 if (is_subarray_active(subarray
, st
->devname
)) {
6305 pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
6312 if (st
->update_tail
) {
6313 struct imsm_update_kill_array
*u
= xmalloc(sizeof(*u
));
6315 u
->type
= update_kill_array
;
6316 u
->dev_idx
= current_vol
;
6317 append_metadata_update(st
, u
, sizeof(*u
));
6322 for (dp
= &super
->devlist
; *dp
;)
6323 if ((*dp
)->index
== current_vol
) {
6326 handle_missing(super
, (*dp
)->dev
);
6327 if ((*dp
)->index
> current_vol
)
6332 /* no more raid devices, all active components are now spares,
6333 * but of course failed are still failed
6335 if (--mpb
->num_raid_devs
== 0) {
6338 for (d
= super
->disks
; d
; d
= d
->next
)
6343 super
->updates_pending
++;
6348 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
6349 char *update
, struct mddev_ident
*ident
)
6351 /* update the subarray currently referenced by ->current_vol */
6352 struct intel_super
*super
= st
->sb
;
6353 struct imsm_super
*mpb
= super
->anchor
;
6355 if (strcmp(update
, "name") == 0) {
6356 char *name
= ident
->name
;
6360 if (is_subarray_active(subarray
, st
->devname
)) {
6361 pr_err("Unable to update name of active subarray\n");
6365 if (!check_name(super
, name
, 0))
6368 vol
= strtoul(subarray
, &ep
, 10);
6369 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
6372 if (st
->update_tail
) {
6373 struct imsm_update_rename_array
*u
= xmalloc(sizeof(*u
));
6375 u
->type
= update_rename_array
;
6377 snprintf((char *) u
->name
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6378 append_metadata_update(st
, u
, sizeof(*u
));
6380 struct imsm_dev
*dev
;
6383 dev
= get_imsm_dev(super
, vol
);
6384 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6385 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6386 dev
= get_imsm_dev(super
, i
);
6387 handle_missing(super
, dev
);
6389 super
->updates_pending
++;
6396 #endif /* MDASSEMBLE */
6398 static int is_gen_migration(struct imsm_dev
*dev
)
6403 if (!dev
->vol
.migr_state
)
6406 if (migr_type(dev
) == MIGR_GEN_MIGR
)
6412 static int is_rebuilding(struct imsm_dev
*dev
)
6414 struct imsm_map
*migr_map
;
6416 if (!dev
->vol
.migr_state
)
6419 if (migr_type(dev
) != MIGR_REBUILD
)
6422 migr_map
= get_imsm_map(dev
, MAP_1
);
6424 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
6431 static int is_initializing(struct imsm_dev
*dev
)
6433 struct imsm_map
*migr_map
;
6435 if (!dev
->vol
.migr_state
)
6438 if (migr_type(dev
) != MIGR_INIT
)
6441 migr_map
= get_imsm_map(dev
, MAP_1
);
6443 if (migr_map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
6450 static void update_recovery_start(struct intel_super
*super
,
6451 struct imsm_dev
*dev
,
6452 struct mdinfo
*array
)
6454 struct mdinfo
*rebuild
= NULL
;
6458 if (!is_rebuilding(dev
))
6461 /* Find the rebuild target, but punt on the dual rebuild case */
6462 for (d
= array
->devs
; d
; d
= d
->next
)
6463 if (d
->recovery_start
== 0) {
6470 /* (?) none of the disks are marked with
6471 * IMSM_ORD_REBUILD, so assume they are missing and the
6472 * disk_ord_tbl was not correctly updated
6474 dprintf("%s: failed to locate out-of-sync disk\n", __func__
);
6478 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
6479 rebuild
->recovery_start
= units
* blocks_per_migr_unit(super
, dev
);
6483 static int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
);
6486 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
6488 /* Given a container loaded by load_super_imsm_all,
6489 * extract information about all the arrays into
6491 * If 'subarray' is given, just extract info about that array.
6493 * For each imsm_dev create an mdinfo, fill it in,
6494 * then look for matching devices in super->disks
6495 * and create appropriate device mdinfo.
6497 struct intel_super
*super
= st
->sb
;
6498 struct imsm_super
*mpb
= super
->anchor
;
6499 struct mdinfo
*rest
= NULL
;
6503 int spare_disks
= 0;
6505 /* do not assemble arrays when not all attributes are supported */
6506 if (imsm_check_attributes(mpb
->attributes
) == 0) {
6508 pr_err("Unsupported attributes in IMSM metadata."
6509 "Arrays activation is blocked.\n");
6512 /* check for bad blocks */
6513 if (imsm_bbm_log_size(super
->anchor
)) {
6514 pr_err("BBM log found in IMSM metadata."
6515 "Arrays activation is blocked.\n");
6520 /* count spare devices, not used in maps
6522 for (d
= super
->disks
; d
; d
= d
->next
)
6526 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6527 struct imsm_dev
*dev
;
6528 struct imsm_map
*map
;
6529 struct imsm_map
*map2
;
6530 struct mdinfo
*this;
6538 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
6541 dev
= get_imsm_dev(super
, i
);
6542 map
= get_imsm_map(dev
, MAP_0
);
6543 map2
= get_imsm_map(dev
, MAP_1
);
6545 /* do not publish arrays that are in the middle of an
6546 * unsupported migration
6548 if (dev
->vol
.migr_state
&&
6549 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
6550 pr_err("cannot assemble volume '%.16s':"
6551 " unsupported migration in progress\n",
6555 /* do not publish arrays that are not support by controller's
6559 this = xmalloc(sizeof(*this));
6561 super
->current_vol
= i
;
6562 getinfo_super_imsm_volume(st
, this, NULL
);
6565 chunk
= __le16_to_cpu(map
->blocks_per_strip
) >> 1;
6566 /* mdadm does not support all metadata features- set the bit in all arrays state */
6567 if (!validate_geometry_imsm_orom(super
,
6568 get_imsm_raid_level(map
), /* RAID level */
6569 imsm_level_to_layout(get_imsm_raid_level(map
)),
6570 map
->num_members
, /* raid disks */
6571 &chunk
, join_u32(dev
->size_low
, dev
->size_high
),
6573 pr_err("IMSM RAID geometry validation"
6574 " failed. Array %s activation is blocked.\n",
6576 this->array
.state
|=
6577 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
6578 (1<<MD_SB_BLOCK_VOLUME
);
6582 /* if array has bad blocks, set suitable bit in all arrays state */
6584 this->array
.state
|=
6585 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
6586 (1<<MD_SB_BLOCK_VOLUME
);
6588 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
6589 unsigned long long recovery_start
;
6590 struct mdinfo
*info_d
;
6597 idx
= get_imsm_disk_idx(dev
, slot
, MAP_0
);
6598 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
6599 for (d
= super
->disks
; d
; d
= d
->next
)
6600 if (d
->index
== idx
)
6603 recovery_start
= MaxSector
;
6606 if (d
&& is_failed(&d
->disk
))
6608 if (ord
& IMSM_ORD_REBUILD
)
6612 * if we skip some disks the array will be assmebled degraded;
6613 * reset resync start to avoid a dirty-degraded
6614 * situation when performing the intial sync
6616 * FIXME handle dirty degraded
6618 if ((skip
|| recovery_start
== 0) && !dev
->vol
.dirty
)
6619 this->resync_start
= MaxSector
;
6623 info_d
= xcalloc(1, sizeof(*info_d
));
6624 info_d
->next
= this->devs
;
6625 this->devs
= info_d
;
6627 info_d
->disk
.number
= d
->index
;
6628 info_d
->disk
.major
= d
->major
;
6629 info_d
->disk
.minor
= d
->minor
;
6630 info_d
->disk
.raid_disk
= slot
;
6631 info_d
->recovery_start
= recovery_start
;
6633 if (slot
< map2
->num_members
)
6634 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
6636 this->array
.spare_disks
++;
6638 if (slot
< map
->num_members
)
6639 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
6641 this->array
.spare_disks
++;
6643 if (info_d
->recovery_start
== MaxSector
)
6644 this->array
.working_disks
++;
6646 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
6647 info_d
->data_offset
= pba_of_lba0(map
);
6648 info_d
->component_size
= blocks_per_member(map
);
6650 /* now that the disk list is up-to-date fixup recovery_start */
6651 update_recovery_start(super
, dev
, this);
6652 this->array
.spare_disks
+= spare_disks
;
6655 /* check for reshape */
6656 if (this->reshape_active
== 1)
6657 recover_backup_imsm(st
, this);
6666 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
6667 int failed
, int look_in_map
)
6669 struct imsm_map
*map
;
6671 map
= get_imsm_map(dev
, look_in_map
);
6674 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
6675 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
6677 switch (get_imsm_raid_level(map
)) {
6679 return IMSM_T_STATE_FAILED
;
6682 if (failed
< map
->num_members
)
6683 return IMSM_T_STATE_DEGRADED
;
6685 return IMSM_T_STATE_FAILED
;
6690 * check to see if any mirrors have failed, otherwise we
6691 * are degraded. Even numbered slots are mirrored on
6695 /* gcc -Os complains that this is unused */
6696 int insync
= insync
;
6698 for (i
= 0; i
< map
->num_members
; i
++) {
6699 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_X
);
6700 int idx
= ord_to_idx(ord
);
6701 struct imsm_disk
*disk
;
6703 /* reset the potential in-sync count on even-numbered
6704 * slots. num_copies is always 2 for imsm raid10
6709 disk
= get_imsm_disk(super
, idx
);
6710 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
6713 /* no in-sync disks left in this mirror the
6717 return IMSM_T_STATE_FAILED
;
6720 return IMSM_T_STATE_DEGRADED
;
6724 return IMSM_T_STATE_DEGRADED
;
6726 return IMSM_T_STATE_FAILED
;
6732 return map
->map_state
;
6735 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
6740 struct imsm_disk
*disk
;
6741 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
6742 struct imsm_map
*prev
= get_imsm_map(dev
, MAP_1
);
6743 struct imsm_map
*map_for_loop
;
6748 /* at the beginning of migration we set IMSM_ORD_REBUILD on
6749 * disks that are being rebuilt. New failures are recorded to
6750 * map[0]. So we look through all the disks we started with and
6751 * see if any failures are still present, or if any new ones
6755 if (prev
&& (map
->num_members
< prev
->num_members
))
6756 map_for_loop
= prev
;
6758 for (i
= 0; i
< map_for_loop
->num_members
; i
++) {
6760 /* when MAP_X is passed both maps failures are counted
6763 ((look_in_map
== MAP_1
) || (look_in_map
== MAP_X
)) &&
6764 (i
< prev
->num_members
)) {
6765 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
6766 idx_1
= ord_to_idx(ord
);
6768 disk
= get_imsm_disk(super
, idx_1
);
6769 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
6772 if (((look_in_map
== MAP_0
) || (look_in_map
== MAP_X
)) &&
6773 (i
< map
->num_members
)) {
6774 ord
= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
6775 idx
= ord_to_idx(ord
);
6778 disk
= get_imsm_disk(super
, idx
);
6779 if (!disk
|| is_failed(disk
) ||
6780 ord
& IMSM_ORD_REBUILD
)
6790 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
6793 struct intel_super
*super
= c
->sb
;
6794 struct imsm_super
*mpb
= super
->anchor
;
6796 if (atoi(inst
) >= mpb
->num_raid_devs
) {
6797 fprintf(stderr
, "%s: subarry index %d, out of range\n",
6798 __func__
, atoi(inst
));
6802 dprintf("imsm: open_new %s\n", inst
);
6803 a
->info
.container_member
= atoi(inst
);
6807 static int is_resyncing(struct imsm_dev
*dev
)
6809 struct imsm_map
*migr_map
;
6811 if (!dev
->vol
.migr_state
)
6814 if (migr_type(dev
) == MIGR_INIT
||
6815 migr_type(dev
) == MIGR_REPAIR
)
6818 if (migr_type(dev
) == MIGR_GEN_MIGR
)
6821 migr_map
= get_imsm_map(dev
, MAP_1
);
6823 if ((migr_map
->map_state
== IMSM_T_STATE_NORMAL
) &&
6824 (dev
->vol
.migr_type
!= MIGR_GEN_MIGR
))
6830 /* return true if we recorded new information */
6831 static int mark_failure(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
6835 struct imsm_map
*map
;
6836 char buf
[MAX_RAID_SERIAL_LEN
+3];
6837 unsigned int len
, shift
= 0;
6839 /* new failures are always set in map[0] */
6840 map
= get_imsm_map(dev
, MAP_0
);
6842 slot
= get_imsm_disk_slot(map
, idx
);
6846 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
6847 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
6850 memcpy(buf
, disk
->serial
, MAX_RAID_SERIAL_LEN
);
6851 buf
[MAX_RAID_SERIAL_LEN
] = '\000';
6853 if ((len
= strlen(buf
)) >= MAX_RAID_SERIAL_LEN
)
6854 shift
= len
- MAX_RAID_SERIAL_LEN
+ 1;
6855 strncpy((char *)disk
->serial
, &buf
[shift
], MAX_RAID_SERIAL_LEN
);
6857 disk
->status
|= FAILED_DISK
;
6858 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
6859 /* mark failures in second map if second map exists and this disk
6861 * This is valid for migration, initialization and rebuild
6863 if (dev
->vol
.migr_state
) {
6864 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
6865 int slot2
= get_imsm_disk_slot(map2
, idx
);
6867 if ((slot2
< map2
->num_members
) &&
6869 set_imsm_ord_tbl_ent(map2
, slot2
,
6870 idx
| IMSM_ORD_REBUILD
);
6872 if (map
->failed_disk_num
== 0xff)
6873 map
->failed_disk_num
= slot
;
6877 static void mark_missing(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
6879 mark_failure(dev
, disk
, idx
);
6881 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
6884 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
6885 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
6888 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
6892 if (!super
->missing
)
6895 dprintf("imsm: mark missing\n");
6896 /* end process for initialization and rebuild only
6898 if (is_gen_migration(dev
) == 0) {
6902 failed
= imsm_count_failed(super
, dev
, MAP_0
);
6903 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
6905 end_migration(dev
, super
, map_state
);
6907 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
6908 mark_missing(dev
, &dl
->disk
, dl
->index
);
6909 super
->updates_pending
++;
6912 static unsigned long long imsm_set_array_size(struct imsm_dev
*dev
,
6915 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
6916 unsigned long long array_blocks
;
6917 struct imsm_map
*map
;
6919 if (used_disks
== 0) {
6920 /* when problems occures
6921 * return current array_blocks value
6923 array_blocks
= __le32_to_cpu(dev
->size_high
);
6924 array_blocks
= array_blocks
<< 32;
6925 array_blocks
+= __le32_to_cpu(dev
->size_low
);
6927 return array_blocks
;
6930 /* set array size in metadata
6932 if (new_size
<= 0) {
6933 /* OLCE size change is caused by added disks
6935 map
= get_imsm_map(dev
, MAP_0
);
6936 array_blocks
= blocks_per_member(map
) * used_disks
;
6938 /* Online Volume Size Change
6939 * Using available free space
6941 array_blocks
= new_size
;
6944 /* round array size down to closest MB
6946 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
6947 dev
->size_low
= __cpu_to_le32((__u32
)array_blocks
);
6948 dev
->size_high
= __cpu_to_le32((__u32
)(array_blocks
>> 32));
6950 return array_blocks
;
6953 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
6955 static void imsm_progress_container_reshape(struct intel_super
*super
)
6957 /* if no device has a migr_state, but some device has a
6958 * different number of members than the previous device, start
6959 * changing the number of devices in this device to match
6962 struct imsm_super
*mpb
= super
->anchor
;
6963 int prev_disks
= -1;
6967 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6968 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
6969 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
6970 struct imsm_map
*map2
;
6971 int prev_num_members
;
6973 if (dev
->vol
.migr_state
)
6976 if (prev_disks
== -1)
6977 prev_disks
= map
->num_members
;
6978 if (prev_disks
== map
->num_members
)
6981 /* OK, this array needs to enter reshape mode.
6982 * i.e it needs a migr_state
6985 copy_map_size
= sizeof_imsm_map(map
);
6986 prev_num_members
= map
->num_members
;
6987 map
->num_members
= prev_disks
;
6988 dev
->vol
.migr_state
= 1;
6989 dev
->vol
.curr_migr_unit
= 0;
6990 set_migr_type(dev
, MIGR_GEN_MIGR
);
6991 for (i
= prev_num_members
;
6992 i
< map
->num_members
; i
++)
6993 set_imsm_ord_tbl_ent(map
, i
, i
);
6994 map2
= get_imsm_map(dev
, MAP_1
);
6995 /* Copy the current map */
6996 memcpy(map2
, map
, copy_map_size
);
6997 map2
->num_members
= prev_num_members
;
6999 imsm_set_array_size(dev
, -1);
7000 super
->clean_migration_record_by_mdmon
= 1;
7001 super
->updates_pending
++;
7005 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
7006 * states are handled in imsm_set_disk() with one exception, when a
7007 * resync is stopped due to a new failure this routine will set the
7008 * 'degraded' state for the array.
7010 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
7012 int inst
= a
->info
.container_member
;
7013 struct intel_super
*super
= a
->container
->sb
;
7014 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7015 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7016 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
7017 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7018 __u32 blocks_per_unit
;
7020 if (dev
->vol
.migr_state
&&
7021 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
7022 /* array state change is blocked due to reshape action
7024 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
7025 * - finish the reshape (if last_checkpoint is big and action != reshape)
7026 * - update curr_migr_unit
7028 if (a
->curr_action
== reshape
) {
7029 /* still reshaping, maybe update curr_migr_unit */
7030 goto mark_checkpoint
;
7032 if (a
->last_checkpoint
== 0 && a
->prev_action
== reshape
) {
7033 /* for some reason we aborted the reshape.
7035 * disable automatic metadata rollback
7036 * user action is required to recover process
7039 struct imsm_map
*map2
=
7040 get_imsm_map(dev
, MAP_1
);
7041 dev
->vol
.migr_state
= 0;
7042 set_migr_type(dev
, 0);
7043 dev
->vol
.curr_migr_unit
= 0;
7045 sizeof_imsm_map(map2
));
7046 super
->updates_pending
++;
7049 if (a
->last_checkpoint
>= a
->info
.component_size
) {
7050 unsigned long long array_blocks
;
7054 used_disks
= imsm_num_data_members(dev
, MAP_0
);
7055 if (used_disks
> 0) {
7057 blocks_per_member(map
) *
7059 /* round array size down to closest MB
7061 array_blocks
= (array_blocks
7062 >> SECT_PER_MB_SHIFT
)
7063 << SECT_PER_MB_SHIFT
;
7064 a
->info
.custom_array_size
= array_blocks
;
7065 /* encourage manager to update array
7069 a
->check_reshape
= 1;
7071 /* finalize online capacity expansion/reshape */
7072 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
7074 mdi
->disk
.raid_disk
,
7077 imsm_progress_container_reshape(super
);
7082 /* before we activate this array handle any missing disks */
7083 if (consistent
== 2)
7084 handle_missing(super
, dev
);
7086 if (consistent
== 2 &&
7087 (!is_resync_complete(&a
->info
) ||
7088 map_state
!= IMSM_T_STATE_NORMAL
||
7089 dev
->vol
.migr_state
))
7092 if (is_resync_complete(&a
->info
)) {
7093 /* complete intialization / resync,
7094 * recovery and interrupted recovery is completed in
7097 if (is_resyncing(dev
)) {
7098 dprintf("imsm: mark resync done\n");
7099 end_migration(dev
, super
, map_state
);
7100 super
->updates_pending
++;
7101 a
->last_checkpoint
= 0;
7103 } else if ((!is_resyncing(dev
) && !failed
) &&
7104 (imsm_reshape_blocks_arrays_changes(super
) == 0)) {
7105 /* mark the start of the init process if nothing is failed */
7106 dprintf("imsm: mark resync start\n");
7107 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
7108 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
7110 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
7111 super
->updates_pending
++;
7115 /* skip checkpointing for general migration,
7116 * it is controlled in mdadm
7118 if (is_gen_migration(dev
))
7119 goto skip_mark_checkpoint
;
7121 /* check if we can update curr_migr_unit from resync_start, recovery_start */
7122 blocks_per_unit
= blocks_per_migr_unit(super
, dev
);
7123 if (blocks_per_unit
) {
7127 units
= a
->last_checkpoint
/ blocks_per_unit
;
7130 /* check that we did not overflow 32-bits, and that
7131 * curr_migr_unit needs updating
7133 if (units32
== units
&&
7135 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
7136 dprintf("imsm: mark checkpoint (%u)\n", units32
);
7137 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
7138 super
->updates_pending
++;
7142 skip_mark_checkpoint
:
7143 /* mark dirty / clean */
7144 if (dev
->vol
.dirty
!= !consistent
) {
7145 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
7150 super
->updates_pending
++;
7156 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
7158 int inst
= a
->info
.container_member
;
7159 struct intel_super
*super
= a
->container
->sb
;
7160 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7161 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7162 struct imsm_disk
*disk
;
7164 int recovery_not_finished
= 0;
7169 if (n
> map
->num_members
)
7170 fprintf(stderr
, "imsm: set_disk %d out of range 0..%d\n",
7171 n
, map
->num_members
- 1);
7176 dprintf("imsm: set_disk %d:%x\n", n
, state
);
7178 ord
= get_imsm_ord_tbl_ent(dev
, n
, MAP_0
);
7179 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
7181 /* check for new failures */
7182 if (state
& DS_FAULTY
) {
7183 if (mark_failure(dev
, disk
, ord_to_idx(ord
)))
7184 super
->updates_pending
++;
7187 /* check if in_sync */
7188 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
7189 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
7191 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
7192 super
->updates_pending
++;
7195 failed
= imsm_count_failed(super
, dev
, MAP_0
);
7196 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7198 /* check if recovery complete, newly degraded, or failed */
7199 dprintf("imsm: Detected transition to state ");
7200 switch (map_state
) {
7201 case IMSM_T_STATE_NORMAL
: /* transition to normal state */
7202 dprintf("normal: ");
7203 if (is_rebuilding(dev
)) {
7204 dprintf("while rebuilding");
7205 /* check if recovery is really finished */
7206 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
7207 if (mdi
->recovery_start
!= MaxSector
) {
7208 recovery_not_finished
= 1;
7211 if (recovery_not_finished
) {
7212 dprintf("\nimsm: Rebuild has not finished yet, "
7213 "state not changed");
7214 if (a
->last_checkpoint
< mdi
->recovery_start
) {
7215 a
->last_checkpoint
= mdi
->recovery_start
;
7216 super
->updates_pending
++;
7220 end_migration(dev
, super
, map_state
);
7221 map
= get_imsm_map(dev
, MAP_0
);
7222 map
->failed_disk_num
= ~0;
7223 super
->updates_pending
++;
7224 a
->last_checkpoint
= 0;
7227 if (is_gen_migration(dev
)) {
7228 dprintf("while general migration");
7229 if (a
->last_checkpoint
>= a
->info
.component_size
)
7230 end_migration(dev
, super
, map_state
);
7232 map
->map_state
= map_state
;
7233 map
= get_imsm_map(dev
, MAP_0
);
7234 map
->failed_disk_num
= ~0;
7235 super
->updates_pending
++;
7239 case IMSM_T_STATE_DEGRADED
: /* transition to degraded state */
7240 dprintf("degraded: ");
7241 if ((map
->map_state
!= map_state
) &&
7242 !dev
->vol
.migr_state
) {
7243 dprintf("mark degraded");
7244 map
->map_state
= map_state
;
7245 super
->updates_pending
++;
7246 a
->last_checkpoint
= 0;
7249 if (is_rebuilding(dev
)) {
7250 dprintf("while rebuilding.");
7251 if (map
->map_state
!= map_state
) {
7252 dprintf(" Map state change");
7253 end_migration(dev
, super
, map_state
);
7254 super
->updates_pending
++;
7258 if (is_gen_migration(dev
)) {
7259 dprintf("while general migration");
7260 if (a
->last_checkpoint
>= a
->info
.component_size
)
7261 end_migration(dev
, super
, map_state
);
7263 map
->map_state
= map_state
;
7264 manage_second_map(super
, dev
);
7266 super
->updates_pending
++;
7269 if (is_initializing(dev
)) {
7270 dprintf("while initialization.");
7271 map
->map_state
= map_state
;
7272 super
->updates_pending
++;
7276 case IMSM_T_STATE_FAILED
: /* transition to failed state */
7277 dprintf("failed: ");
7278 if (is_gen_migration(dev
)) {
7279 dprintf("while general migration");
7280 map
->map_state
= map_state
;
7281 super
->updates_pending
++;
7284 if (map
->map_state
!= map_state
) {
7285 dprintf("mark failed");
7286 end_migration(dev
, super
, map_state
);
7287 super
->updates_pending
++;
7288 a
->last_checkpoint
= 0;
7293 dprintf("state %i\n", map_state
);
7299 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
7302 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
7303 unsigned long long dsize
;
7304 unsigned long long sectors
;
7306 get_dev_size(fd
, NULL
, &dsize
);
7308 if (mpb_size
> 512) {
7309 /* -1 to account for anchor */
7310 sectors
= mpb_sectors(mpb
) - 1;
7312 /* write the extended mpb to the sectors preceeding the anchor */
7313 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0)
7316 if ((unsigned long long)write(fd
, buf
+ 512, 512 * sectors
)
7321 /* first block is stored on second to last sector of the disk */
7322 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0)
7325 if (write(fd
, buf
, 512) != 512)
7331 static void imsm_sync_metadata(struct supertype
*container
)
7333 struct intel_super
*super
= container
->sb
;
7335 dprintf("sync metadata: %d\n", super
->updates_pending
);
7336 if (!super
->updates_pending
)
7339 write_super_imsm(container
, 0);
7341 super
->updates_pending
= 0;
7344 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
7346 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
7347 int i
= get_imsm_disk_idx(dev
, idx
, MAP_X
);
7350 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7354 if (dl
&& is_failed(&dl
->disk
))
7358 dprintf("%s: found %x:%x\n", __func__
, dl
->major
, dl
->minor
);
7363 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
7364 struct active_array
*a
, int activate_new
,
7365 struct mdinfo
*additional_test_list
)
7367 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
7368 int idx
= get_imsm_disk_idx(dev
, slot
, MAP_X
);
7369 struct imsm_super
*mpb
= super
->anchor
;
7370 struct imsm_map
*map
;
7371 unsigned long long pos
;
7376 __u32 array_start
= 0;
7377 __u32 array_end
= 0;
7379 struct mdinfo
*test_list
;
7381 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7382 /* If in this array, skip */
7383 for (d
= a
->info
.devs
; d
; d
= d
->next
)
7384 if (d
->state_fd
>= 0 &&
7385 d
->disk
.major
== dl
->major
&&
7386 d
->disk
.minor
== dl
->minor
) {
7387 dprintf("%x:%x already in array\n",
7388 dl
->major
, dl
->minor
);
7393 test_list
= additional_test_list
;
7395 if (test_list
->disk
.major
== dl
->major
&&
7396 test_list
->disk
.minor
== dl
->minor
) {
7397 dprintf("%x:%x already in additional test list\n",
7398 dl
->major
, dl
->minor
);
7401 test_list
= test_list
->next
;
7406 /* skip in use or failed drives */
7407 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
7409 dprintf("%x:%x status (failed: %d index: %d)\n",
7410 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
7414 /* skip pure spares when we are looking for partially
7415 * assimilated drives
7417 if (dl
->index
== -1 && !activate_new
)
7420 /* Does this unused device have the requisite free space?
7421 * It needs to be able to cover all member volumes
7423 ex
= get_extents(super
, dl
);
7425 dprintf("cannot get extents\n");
7428 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7429 dev
= get_imsm_dev(super
, i
);
7430 map
= get_imsm_map(dev
, MAP_0
);
7432 /* check if this disk is already a member of
7435 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
7441 array_start
= pba_of_lba0(map
);
7442 array_end
= array_start
+
7443 blocks_per_member(map
) - 1;
7446 /* check that we can start at pba_of_lba0 with
7447 * blocks_per_member of space
7449 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
7453 pos
= ex
[j
].start
+ ex
[j
].size
;
7455 } while (ex
[j
-1].size
);
7462 if (i
< mpb
->num_raid_devs
) {
7463 dprintf("%x:%x does not have %u to %u available\n",
7464 dl
->major
, dl
->minor
, array_start
, array_end
);
7475 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
7477 struct imsm_dev
*dev2
;
7478 struct imsm_map
*map
;
7484 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
7486 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
, MAP_0
);
7487 if (state
== IMSM_T_STATE_FAILED
) {
7488 map
= get_imsm_map(dev2
, MAP_0
);
7491 for (slot
= 0; slot
< map
->num_members
; slot
++) {
7493 * Check if failed disks are deleted from intel
7494 * disk list or are marked to be deleted
7496 idx
= get_imsm_disk_idx(dev2
, slot
, MAP_X
);
7497 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
7499 * Do not rebuild the array if failed disks
7500 * from failed sub-array are not removed from
7504 is_failed(&idisk
->disk
) &&
7505 (idisk
->action
!= DISK_REMOVE
))
7513 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
7514 struct metadata_update
**updates
)
7517 * Find a device with unused free space and use it to replace a
7518 * failed/vacant region in an array. We replace failed regions one a
7519 * array at a time. The result is that a new spare disk will be added
7520 * to the first failed array and after the monitor has finished
7521 * propagating failures the remainder will be consumed.
7523 * FIXME add a capability for mdmon to request spares from another
7527 struct intel_super
*super
= a
->container
->sb
;
7528 int inst
= a
->info
.container_member
;
7529 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7530 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7531 int failed
= a
->info
.array
.raid_disks
;
7532 struct mdinfo
*rv
= NULL
;
7535 struct metadata_update
*mu
;
7537 struct imsm_update_activate_spare
*u
;
7542 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
7543 if ((d
->curr_state
& DS_FAULTY
) &&
7545 /* wait for Removal to happen */
7547 if (d
->state_fd
>= 0)
7551 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
7552 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
7554 if (imsm_reshape_blocks_arrays_changes(super
))
7557 /* Cannot activate another spare if rebuild is in progress already
7559 if (is_rebuilding(dev
)) {
7560 dprintf("imsm: No spare activation allowed. "
7561 "Rebuild in progress already.\n");
7565 if (a
->info
.array
.level
== 4)
7566 /* No repair for takeovered array
7567 * imsm doesn't support raid4
7571 if (imsm_check_degraded(super
, dev
, failed
, MAP_0
) !=
7572 IMSM_T_STATE_DEGRADED
)
7576 * If there are any failed disks check state of the other volume.
7577 * Block rebuild if the another one is failed until failed disks
7578 * are removed from container.
7581 dprintf("found failed disks in %.*s, check if there another"
7582 "failed sub-array.\n",
7583 MAX_RAID_SERIAL_LEN
, dev
->volume
);
7584 /* check if states of the other volumes allow for rebuild */
7585 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
7587 allowed
= imsm_rebuild_allowed(a
->container
,
7595 /* For each slot, if it is not working, find a spare */
7596 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
7597 for (d
= a
->info
.devs
; d
; d
= d
->next
)
7598 if (d
->disk
.raid_disk
== i
)
7600 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
7601 if (d
&& (d
->state_fd
>= 0))
7605 * OK, this device needs recovery. Try to re-add the
7606 * previous occupant of this slot, if this fails see if
7607 * we can continue the assimilation of a spare that was
7608 * partially assimilated, finally try to activate a new
7611 dl
= imsm_readd(super
, i
, a
);
7613 dl
= imsm_add_spare(super
, i
, a
, 0, rv
);
7615 dl
= imsm_add_spare(super
, i
, a
, 1, rv
);
7619 /* found a usable disk with enough space */
7620 di
= xcalloc(1, sizeof(*di
));
7622 /* dl->index will be -1 in the case we are activating a
7623 * pristine spare. imsm_process_update() will create a
7624 * new index in this case. Once a disk is found to be
7625 * failed in all member arrays it is kicked from the
7628 di
->disk
.number
= dl
->index
;
7630 /* (ab)use di->devs to store a pointer to the device
7633 di
->devs
= (struct mdinfo
*) dl
;
7635 di
->disk
.raid_disk
= i
;
7636 di
->disk
.major
= dl
->major
;
7637 di
->disk
.minor
= dl
->minor
;
7639 di
->recovery_start
= 0;
7640 di
->data_offset
= pba_of_lba0(map
);
7641 di
->component_size
= a
->info
.component_size
;
7642 di
->container_member
= inst
;
7643 super
->random
= random32();
7647 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
7648 i
, di
->data_offset
);
7652 /* No spares found */
7654 /* Now 'rv' has a list of devices to return.
7655 * Create a metadata_update record to update the
7656 * disk_ord_tbl for the array
7658 mu
= xmalloc(sizeof(*mu
));
7659 mu
->buf
= xcalloc(num_spares
,
7660 sizeof(struct imsm_update_activate_spare
));
7662 mu
->space_list
= NULL
;
7663 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
7664 mu
->next
= *updates
;
7665 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
7667 for (di
= rv
; di
; di
= di
->next
) {
7668 u
->type
= update_activate_spare
;
7669 u
->dl
= (struct dl
*) di
->devs
;
7671 u
->slot
= di
->disk
.raid_disk
;
7682 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
7684 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
7685 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7686 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, MAP_0
);
7687 struct disk_info
*inf
= get_disk_info(u
);
7688 struct imsm_disk
*disk
;
7692 for (i
= 0; i
< map
->num_members
; i
++) {
7693 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, MAP_X
));
7694 for (j
= 0; j
< new_map
->num_members
; j
++)
7695 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
7703 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
7705 struct dl
*dl
= NULL
;
7706 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7707 if ((dl
->major
== major
) && (dl
->minor
== minor
))
7712 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
7714 struct dl
*prev
= NULL
;
7718 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7719 if ((dl
->major
== major
) && (dl
->minor
== minor
)) {
7722 prev
->next
= dl
->next
;
7724 super
->disks
= dl
->next
;
7726 __free_imsm_disk(dl
);
7727 dprintf("%s: removed %x:%x\n",
7728 __func__
, major
, minor
);
7736 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
7738 static int add_remove_disk_update(struct intel_super
*super
)
7740 int check_degraded
= 0;
7741 struct dl
*disk
= NULL
;
7742 /* add/remove some spares to/from the metadata/contrainer */
7743 while (super
->disk_mgmt_list
) {
7744 struct dl
*disk_cfg
;
7746 disk_cfg
= super
->disk_mgmt_list
;
7747 super
->disk_mgmt_list
= disk_cfg
->next
;
7748 disk_cfg
->next
= NULL
;
7750 if (disk_cfg
->action
== DISK_ADD
) {
7751 disk_cfg
->next
= super
->disks
;
7752 super
->disks
= disk_cfg
;
7754 dprintf("%s: added %x:%x\n",
7755 __func__
, disk_cfg
->major
,
7757 } else if (disk_cfg
->action
== DISK_REMOVE
) {
7758 dprintf("Disk remove action processed: %x.%x\n",
7759 disk_cfg
->major
, disk_cfg
->minor
);
7760 disk
= get_disk_super(super
,
7764 /* store action status */
7765 disk
->action
= DISK_REMOVE
;
7766 /* remove spare disks only */
7767 if (disk
->index
== -1) {
7768 remove_disk_super(super
,
7773 /* release allocate disk structure */
7774 __free_imsm_disk(disk_cfg
);
7777 return check_degraded
;
7781 static int apply_reshape_migration_update(struct imsm_update_reshape_migration
*u
,
7782 struct intel_super
*super
,
7785 struct intel_dev
*id
;
7786 void **tofree
= NULL
;
7789 dprintf("apply_reshape_migration_update()\n");
7790 if ((u
->subdev
< 0) ||
7792 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
7795 if ((space_list
== NULL
) || (*space_list
== NULL
)) {
7796 dprintf("imsm: Error: Memory is not allocated\n");
7800 for (id
= super
->devlist
; id
; id
= id
->next
) {
7801 if (id
->index
== (unsigned)u
->subdev
) {
7802 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
7803 struct imsm_map
*map
;
7804 struct imsm_dev
*new_dev
=
7805 (struct imsm_dev
*)*space_list
;
7806 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
7808 struct dl
*new_disk
;
7810 if (new_dev
== NULL
)
7812 *space_list
= **space_list
;
7813 memcpy(new_dev
, dev
, sizeof_imsm_dev(dev
, 0));
7814 map
= get_imsm_map(new_dev
, MAP_0
);
7816 dprintf("imsm: Error: migration in progress");
7820 to_state
= map
->map_state
;
7821 if ((u
->new_level
== 5) && (map
->raid_level
== 0)) {
7823 /* this should not happen */
7824 if (u
->new_disks
[0] < 0) {
7825 map
->failed_disk_num
=
7826 map
->num_members
- 1;
7827 to_state
= IMSM_T_STATE_DEGRADED
;
7829 to_state
= IMSM_T_STATE_NORMAL
;
7831 migrate(new_dev
, super
, to_state
, MIGR_GEN_MIGR
);
7832 if (u
->new_level
> -1)
7833 map
->raid_level
= u
->new_level
;
7834 migr_map
= get_imsm_map(new_dev
, MAP_1
);
7835 if ((u
->new_level
== 5) &&
7836 (migr_map
->raid_level
== 0)) {
7837 int ord
= map
->num_members
- 1;
7838 migr_map
->num_members
--;
7839 if (u
->new_disks
[0] < 0)
7840 ord
|= IMSM_ORD_REBUILD
;
7841 set_imsm_ord_tbl_ent(map
,
7842 map
->num_members
- 1,
7846 tofree
= (void **)dev
;
7848 /* update chunk size
7850 if (u
->new_chunksize
> 0)
7851 map
->blocks_per_strip
=
7852 __cpu_to_le16(u
->new_chunksize
* 2);
7856 if ((u
->new_level
!= 5) ||
7857 (migr_map
->raid_level
!= 0) ||
7858 (migr_map
->raid_level
== map
->raid_level
))
7861 if (u
->new_disks
[0] >= 0) {
7864 new_disk
= get_disk_super(super
,
7865 major(u
->new_disks
[0]),
7866 minor(u
->new_disks
[0]));
7867 dprintf("imsm: new disk for reshape is: %i:%i "
7868 "(%p, index = %i)\n",
7869 major(u
->new_disks
[0]),
7870 minor(u
->new_disks
[0]),
7871 new_disk
, new_disk
->index
);
7872 if (new_disk
== NULL
)
7873 goto error_disk_add
;
7875 new_disk
->index
= map
->num_members
- 1;
7876 /* slot to fill in autolayout
7878 new_disk
->raiddisk
= new_disk
->index
;
7879 new_disk
->disk
.status
|= CONFIGURED_DISK
;
7880 new_disk
->disk
.status
&= ~SPARE_DISK
;
7882 goto error_disk_add
;
7885 *tofree
= *space_list
;
7886 /* calculate new size
7888 imsm_set_array_size(new_dev
, -1);
7895 *space_list
= tofree
;
7899 dprintf("Error: imsm: Cannot find disk.\n");
7903 static int apply_size_change_update(struct imsm_update_size_change
*u
,
7904 struct intel_super
*super
)
7906 struct intel_dev
*id
;
7909 dprintf("apply_size_change_update()\n");
7910 if ((u
->subdev
< 0) ||
7912 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
7916 for (id
= super
->devlist
; id
; id
= id
->next
) {
7917 if (id
->index
== (unsigned)u
->subdev
) {
7918 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
7919 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7920 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
7921 unsigned long long blocks_per_member
;
7923 /* calculate new size
7925 blocks_per_member
= u
->new_size
/ used_disks
;
7926 dprintf("imsm: apply_size_change_update(size: %llu, "
7927 "blocks per member: %llu)\n",
7928 u
->new_size
, blocks_per_member
);
7929 set_blocks_per_member(map
, blocks_per_member
);
7930 imsm_set_array_size(dev
, u
->new_size
);
7941 static int apply_update_activate_spare(struct imsm_update_activate_spare
*u
,
7942 struct intel_super
*super
,
7943 struct active_array
*active_array
)
7945 struct imsm_super
*mpb
= super
->anchor
;
7946 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
7947 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7948 struct imsm_map
*migr_map
;
7949 struct active_array
*a
;
7950 struct imsm_disk
*disk
;
7957 int second_map_created
= 0;
7959 for (; u
; u
= u
->next
) {
7960 victim
= get_imsm_disk_idx(dev
, u
->slot
, MAP_X
);
7965 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7970 fprintf(stderr
, "error: imsm_activate_spare passed "
7971 "an unknown disk (index: %d)\n",
7976 /* count failures (excluding rebuilds and the victim)
7977 * to determine map[0] state
7980 for (i
= 0; i
< map
->num_members
; i
++) {
7983 disk
= get_imsm_disk(super
,
7984 get_imsm_disk_idx(dev
, i
, MAP_X
));
7985 if (!disk
|| is_failed(disk
))
7989 /* adding a pristine spare, assign a new index */
7990 if (dl
->index
< 0) {
7991 dl
->index
= super
->anchor
->num_disks
;
7992 super
->anchor
->num_disks
++;
7995 disk
->status
|= CONFIGURED_DISK
;
7996 disk
->status
&= ~SPARE_DISK
;
7999 to_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8000 if (!second_map_created
) {
8001 second_map_created
= 1;
8002 map
->map_state
= IMSM_T_STATE_DEGRADED
;
8003 migrate(dev
, super
, to_state
, MIGR_REBUILD
);
8005 map
->map_state
= to_state
;
8006 migr_map
= get_imsm_map(dev
, MAP_1
);
8007 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
8008 set_imsm_ord_tbl_ent(migr_map
, u
->slot
,
8009 dl
->index
| IMSM_ORD_REBUILD
);
8011 /* update the family_num to mark a new container
8012 * generation, being careful to record the existing
8013 * family_num in orig_family_num to clean up after
8014 * earlier mdadm versions that neglected to set it.
8016 if (mpb
->orig_family_num
== 0)
8017 mpb
->orig_family_num
= mpb
->family_num
;
8018 mpb
->family_num
+= super
->random
;
8020 /* count arrays using the victim in the metadata */
8022 for (a
= active_array
; a
; a
= a
->next
) {
8023 dev
= get_imsm_dev(super
, a
->info
.container_member
);
8024 map
= get_imsm_map(dev
, MAP_0
);
8026 if (get_imsm_disk_slot(map
, victim
) >= 0)
8030 /* delete the victim if it is no longer being
8036 /* We know that 'manager' isn't touching anything,
8037 * so it is safe to delete
8039 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
8040 if ((*dlp
)->index
== victim
)
8043 /* victim may be on the missing list */
8045 for (dlp
= &super
->missing
; *dlp
;
8046 dlp
= &(*dlp
)->next
)
8047 if ((*dlp
)->index
== victim
)
8049 imsm_delete(super
, dlp
, victim
);
8056 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
8057 struct intel_super
*super
,
8060 struct dl
*new_disk
;
8061 struct intel_dev
*id
;
8063 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
8064 int disk_count
= u
->old_raid_disks
;
8065 void **tofree
= NULL
;
8066 int devices_to_reshape
= 1;
8067 struct imsm_super
*mpb
= super
->anchor
;
8069 unsigned int dev_id
;
8071 dprintf("imsm: apply_reshape_container_disks_update()\n");
8073 /* enable spares to use in array */
8074 for (i
= 0; i
< delta_disks
; i
++) {
8075 new_disk
= get_disk_super(super
,
8076 major(u
->new_disks
[i
]),
8077 minor(u
->new_disks
[i
]));
8078 dprintf("imsm: new disk for reshape is: %i:%i "
8079 "(%p, index = %i)\n",
8080 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
8081 new_disk
, new_disk
->index
);
8082 if ((new_disk
== NULL
) ||
8083 ((new_disk
->index
>= 0) &&
8084 (new_disk
->index
< u
->old_raid_disks
)))
8085 goto update_reshape_exit
;
8086 new_disk
->index
= disk_count
++;
8087 /* slot to fill in autolayout
8089 new_disk
->raiddisk
= new_disk
->index
;
8090 new_disk
->disk
.status
|=
8092 new_disk
->disk
.status
&= ~SPARE_DISK
;
8095 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
8096 mpb
->num_raid_devs
);
8097 /* manage changes in volume
8099 for (dev_id
= 0; dev_id
< mpb
->num_raid_devs
; dev_id
++) {
8100 void **sp
= *space_list
;
8101 struct imsm_dev
*newdev
;
8102 struct imsm_map
*newmap
, *oldmap
;
8104 for (id
= super
->devlist
; id
; id
= id
->next
) {
8105 if (id
->index
== dev_id
)
8114 /* Copy the dev, but not (all of) the map */
8115 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
8116 oldmap
= get_imsm_map(id
->dev
, MAP_0
);
8117 newmap
= get_imsm_map(newdev
, MAP_0
);
8118 /* Copy the current map */
8119 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8120 /* update one device only
8122 if (devices_to_reshape
) {
8123 dprintf("imsm: modifying subdev: %i\n",
8125 devices_to_reshape
--;
8126 newdev
->vol
.migr_state
= 1;
8127 newdev
->vol
.curr_migr_unit
= 0;
8128 set_migr_type(newdev
, MIGR_GEN_MIGR
);
8129 newmap
->num_members
= u
->new_raid_disks
;
8130 for (i
= 0; i
< delta_disks
; i
++) {
8131 set_imsm_ord_tbl_ent(newmap
,
8132 u
->old_raid_disks
+ i
,
8133 u
->old_raid_disks
+ i
);
8135 /* New map is correct, now need to save old map
8137 newmap
= get_imsm_map(newdev
, MAP_1
);
8138 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8140 imsm_set_array_size(newdev
, -1);
8143 sp
= (void **)id
->dev
;
8148 /* Clear migration record */
8149 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
8152 *space_list
= tofree
;
8155 update_reshape_exit
:
8160 static int apply_takeover_update(struct imsm_update_takeover
*u
,
8161 struct intel_super
*super
,
8164 struct imsm_dev
*dev
= NULL
;
8165 struct intel_dev
*dv
;
8166 struct imsm_dev
*dev_new
;
8167 struct imsm_map
*map
;
8171 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
8172 if (dv
->index
== (unsigned int)u
->subarray
) {
8180 map
= get_imsm_map(dev
, MAP_0
);
8182 if (u
->direction
== R10_TO_R0
) {
8183 /* Number of failed disks must be half of initial disk number */
8184 if (imsm_count_failed(super
, dev
, MAP_0
) !=
8185 (map
->num_members
/ 2))
8188 /* iterate through devices to mark removed disks as spare */
8189 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
8190 if (dm
->disk
.status
& FAILED_DISK
) {
8191 int idx
= dm
->index
;
8192 /* update indexes on the disk list */
8193 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
8194 the index values will end up being correct.... NB */
8195 for (du
= super
->disks
; du
; du
= du
->next
)
8196 if (du
->index
> idx
)
8198 /* mark as spare disk */
8203 map
->num_members
= map
->num_members
/ 2;
8204 map
->map_state
= IMSM_T_STATE_NORMAL
;
8205 map
->num_domains
= 1;
8206 map
->raid_level
= 0;
8207 map
->failed_disk_num
= -1;
8210 if (u
->direction
== R0_TO_R10
) {
8212 /* update slots in current disk list */
8213 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
8217 /* create new *missing* disks */
8218 for (i
= 0; i
< map
->num_members
; i
++) {
8219 space
= *space_list
;
8222 *space_list
= *space
;
8224 memcpy(du
, super
->disks
, sizeof(*du
));
8228 du
->index
= (i
* 2) + 1;
8229 sprintf((char *)du
->disk
.serial
,
8230 " MISSING_%d", du
->index
);
8231 sprintf((char *)du
->serial
,
8232 "MISSING_%d", du
->index
);
8233 du
->next
= super
->missing
;
8234 super
->missing
= du
;
8236 /* create new dev and map */
8237 space
= *space_list
;
8240 *space_list
= *space
;
8241 dev_new
= (void *)space
;
8242 memcpy(dev_new
, dev
, sizeof(*dev
));
8243 /* update new map */
8244 map
= get_imsm_map(dev_new
, MAP_0
);
8245 map
->num_members
= map
->num_members
* 2;
8246 map
->map_state
= IMSM_T_STATE_DEGRADED
;
8247 map
->num_domains
= 2;
8248 map
->raid_level
= 1;
8249 /* replace dev<->dev_new */
8252 /* update disk order table */
8253 for (du
= super
->disks
; du
; du
= du
->next
)
8255 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
8256 for (du
= super
->missing
; du
; du
= du
->next
)
8257 if (du
->index
>= 0) {
8258 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
8259 mark_missing(dv
->dev
, &du
->disk
, du
->index
);
8265 static void imsm_process_update(struct supertype
*st
,
8266 struct metadata_update
*update
)
8269 * crack open the metadata_update envelope to find the update record
8270 * update can be one of:
8271 * update_reshape_container_disks - all the arrays in the container
8272 * are being reshaped to have more devices. We need to mark
8273 * the arrays for general migration and convert selected spares
8274 * into active devices.
8275 * update_activate_spare - a spare device has replaced a failed
8276 * device in an array, update the disk_ord_tbl. If this disk is
8277 * present in all member arrays then also clear the SPARE_DISK
8279 * update_create_array
8281 * update_rename_array
8282 * update_add_remove_disk
8284 struct intel_super
*super
= st
->sb
;
8285 struct imsm_super
*mpb
;
8286 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
8288 /* update requires a larger buf but the allocation failed */
8289 if (super
->next_len
&& !super
->next_buf
) {
8290 super
->next_len
= 0;
8294 if (super
->next_buf
) {
8295 memcpy(super
->next_buf
, super
->buf
, super
->len
);
8297 super
->len
= super
->next_len
;
8298 super
->buf
= super
->next_buf
;
8300 super
->next_len
= 0;
8301 super
->next_buf
= NULL
;
8304 mpb
= super
->anchor
;
8307 case update_general_migration_checkpoint
: {
8308 struct intel_dev
*id
;
8309 struct imsm_update_general_migration_checkpoint
*u
=
8310 (void *)update
->buf
;
8312 dprintf("imsm: process_update() "
8313 "for update_general_migration_checkpoint called\n");
8315 /* find device under general migration */
8316 for (id
= super
->devlist
; id
; id
= id
->next
) {
8317 if (is_gen_migration(id
->dev
)) {
8318 id
->dev
->vol
.curr_migr_unit
=
8319 __cpu_to_le32(u
->curr_migr_unit
);
8320 super
->updates_pending
++;
8325 case update_takeover
: {
8326 struct imsm_update_takeover
*u
= (void *)update
->buf
;
8327 if (apply_takeover_update(u
, super
, &update
->space_list
)) {
8328 imsm_update_version_info(super
);
8329 super
->updates_pending
++;
8334 case update_reshape_container_disks
: {
8335 struct imsm_update_reshape
*u
= (void *)update
->buf
;
8336 if (apply_reshape_container_disks_update(
8337 u
, super
, &update
->space_list
))
8338 super
->updates_pending
++;
8341 case update_reshape_migration
: {
8342 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
8343 if (apply_reshape_migration_update(
8344 u
, super
, &update
->space_list
))
8345 super
->updates_pending
++;
8348 case update_size_change
: {
8349 struct imsm_update_size_change
*u
= (void *)update
->buf
;
8350 if (apply_size_change_update(u
, super
))
8351 super
->updates_pending
++;
8354 case update_activate_spare
: {
8355 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
8356 if (apply_update_activate_spare(u
, super
, st
->arrays
))
8357 super
->updates_pending
++;
8360 case update_create_array
: {
8361 /* someone wants to create a new array, we need to be aware of
8362 * a few races/collisions:
8363 * 1/ 'Create' called by two separate instances of mdadm
8364 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
8365 * devices that have since been assimilated via
8367 * In the event this update can not be carried out mdadm will
8368 * (FIX ME) notice that its update did not take hold.
8370 struct imsm_update_create_array
*u
= (void *) update
->buf
;
8371 struct intel_dev
*dv
;
8372 struct imsm_dev
*dev
;
8373 struct imsm_map
*map
, *new_map
;
8374 unsigned long long start
, end
;
8375 unsigned long long new_start
, new_end
;
8377 struct disk_info
*inf
;
8380 /* handle racing creates: first come first serve */
8381 if (u
->dev_idx
< mpb
->num_raid_devs
) {
8382 dprintf("%s: subarray %d already defined\n",
8383 __func__
, u
->dev_idx
);
8387 /* check update is next in sequence */
8388 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
8389 dprintf("%s: can not create array %d expected index %d\n",
8390 __func__
, u
->dev_idx
, mpb
->num_raid_devs
);
8394 new_map
= get_imsm_map(&u
->dev
, MAP_0
);
8395 new_start
= pba_of_lba0(new_map
);
8396 new_end
= new_start
+ blocks_per_member(new_map
);
8397 inf
= get_disk_info(u
);
8399 /* handle activate_spare versus create race:
8400 * check to make sure that overlapping arrays do not include
8403 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8404 dev
= get_imsm_dev(super
, i
);
8405 map
= get_imsm_map(dev
, MAP_0
);
8406 start
= pba_of_lba0(map
);
8407 end
= start
+ blocks_per_member(map
);
8408 if ((new_start
>= start
&& new_start
<= end
) ||
8409 (start
>= new_start
&& start
<= new_end
))
8414 if (disks_overlap(super
, i
, u
)) {
8415 dprintf("%s: arrays overlap\n", __func__
);
8420 /* check that prepare update was successful */
8421 if (!update
->space
) {
8422 dprintf("%s: prepare update failed\n", __func__
);
8426 /* check that all disks are still active before committing
8427 * changes. FIXME: could we instead handle this by creating a
8428 * degraded array? That's probably not what the user expects,
8429 * so better to drop this update on the floor.
8431 for (i
= 0; i
< new_map
->num_members
; i
++) {
8432 dl
= serial_to_dl(inf
[i
].serial
, super
);
8434 dprintf("%s: disk disappeared\n", __func__
);
8439 super
->updates_pending
++;
8441 /* convert spares to members and fixup ord_tbl */
8442 for (i
= 0; i
< new_map
->num_members
; i
++) {
8443 dl
= serial_to_dl(inf
[i
].serial
, super
);
8444 if (dl
->index
== -1) {
8445 dl
->index
= mpb
->num_disks
;
8447 dl
->disk
.status
|= CONFIGURED_DISK
;
8448 dl
->disk
.status
&= ~SPARE_DISK
;
8450 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
8455 update
->space
= NULL
;
8456 imsm_copy_dev(dev
, &u
->dev
);
8457 dv
->index
= u
->dev_idx
;
8458 dv
->next
= super
->devlist
;
8459 super
->devlist
= dv
;
8460 mpb
->num_raid_devs
++;
8462 imsm_update_version_info(super
);
8465 /* mdmon knows how to release update->space, but not
8466 * ((struct intel_dev *) update->space)->dev
8468 if (update
->space
) {
8474 case update_kill_array
: {
8475 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
8476 int victim
= u
->dev_idx
;
8477 struct active_array
*a
;
8478 struct intel_dev
**dp
;
8479 struct imsm_dev
*dev
;
8481 /* sanity check that we are not affecting the uuid of
8482 * active arrays, or deleting an active array
8484 * FIXME when immutable ids are available, but note that
8485 * we'll also need to fixup the invalidated/active
8486 * subarray indexes in mdstat
8488 for (a
= st
->arrays
; a
; a
= a
->next
)
8489 if (a
->info
.container_member
>= victim
)
8491 /* by definition if mdmon is running at least one array
8492 * is active in the container, so checking
8493 * mpb->num_raid_devs is just extra paranoia
8495 dev
= get_imsm_dev(super
, victim
);
8496 if (a
|| !dev
|| mpb
->num_raid_devs
== 1) {
8497 dprintf("failed to delete subarray-%d\n", victim
);
8501 for (dp
= &super
->devlist
; *dp
;)
8502 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
8505 if ((*dp
)->index
> (unsigned)victim
)
8509 mpb
->num_raid_devs
--;
8510 super
->updates_pending
++;
8513 case update_rename_array
: {
8514 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
8515 char name
[MAX_RAID_SERIAL_LEN
+1];
8516 int target
= u
->dev_idx
;
8517 struct active_array
*a
;
8518 struct imsm_dev
*dev
;
8520 /* sanity check that we are not affecting the uuid of
8523 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
8524 name
[MAX_RAID_SERIAL_LEN
] = '\0';
8525 for (a
= st
->arrays
; a
; a
= a
->next
)
8526 if (a
->info
.container_member
== target
)
8528 dev
= get_imsm_dev(super
, u
->dev_idx
);
8529 if (a
|| !dev
|| !check_name(super
, name
, 1)) {
8530 dprintf("failed to rename subarray-%d\n", target
);
8534 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
8535 super
->updates_pending
++;
8538 case update_add_remove_disk
: {
8539 /* we may be able to repair some arrays if disks are
8540 * being added, check teh status of add_remove_disk
8541 * if discs has been added.
8543 if (add_remove_disk_update(super
)) {
8544 struct active_array
*a
;
8546 super
->updates_pending
++;
8547 for (a
= st
->arrays
; a
; a
= a
->next
)
8548 a
->check_degraded
= 1;
8553 fprintf(stderr
, "error: unsuported process update type:"
8554 "(type: %d)\n", type
);
8558 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
);
8560 static void imsm_prepare_update(struct supertype
*st
,
8561 struct metadata_update
*update
)
8564 * Allocate space to hold new disk entries, raid-device entries or a new
8565 * mpb if necessary. The manager synchronously waits for updates to
8566 * complete in the monitor, so new mpb buffers allocated here can be
8567 * integrated by the monitor thread without worrying about live pointers
8568 * in the manager thread.
8570 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
8571 struct intel_super
*super
= st
->sb
;
8572 struct imsm_super
*mpb
= super
->anchor
;
8577 case update_general_migration_checkpoint
:
8578 dprintf("imsm: prepare_update() "
8579 "for update_general_migration_checkpoint called\n");
8581 case update_takeover
: {
8582 struct imsm_update_takeover
*u
= (void *)update
->buf
;
8583 if (u
->direction
== R0_TO_R10
) {
8584 void **tail
= (void **)&update
->space_list
;
8585 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subarray
);
8586 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8587 int num_members
= map
->num_members
;
8590 /* allocate memory for added disks */
8591 for (i
= 0; i
< num_members
; i
++) {
8592 size
= sizeof(struct dl
);
8593 space
= xmalloc(size
);
8598 /* allocate memory for new device */
8599 size
= sizeof_imsm_dev(super
->devlist
->dev
, 0) +
8600 (num_members
* sizeof(__u32
));
8601 space
= xmalloc(size
);
8605 len
= disks_to_mpb_size(num_members
* 2);
8610 case update_reshape_container_disks
: {
8611 /* Every raid device in the container is about to
8612 * gain some more devices, and we will enter a
8614 * So each 'imsm_map' will be bigger, and the imsm_vol
8615 * will now hold 2 of them.
8616 * Thus we need new 'struct imsm_dev' allocations sized
8617 * as sizeof_imsm_dev but with more devices in both maps.
8619 struct imsm_update_reshape
*u
= (void *)update
->buf
;
8620 struct intel_dev
*dl
;
8621 void **space_tail
= (void**)&update
->space_list
;
8623 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
8625 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
8626 int size
= sizeof_imsm_dev(dl
->dev
, 1);
8628 if (u
->new_raid_disks
> u
->old_raid_disks
)
8629 size
+= sizeof(__u32
)*2*
8630 (u
->new_raid_disks
- u
->old_raid_disks
);
8637 len
= disks_to_mpb_size(u
->new_raid_disks
);
8638 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
8641 case update_reshape_migration
: {
8642 /* for migration level 0->5 we need to add disks
8643 * so the same as for container operation we will copy
8644 * device to the bigger location.
8645 * in memory prepared device and new disk area are prepared
8646 * for usage in process update
8648 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
8649 struct intel_dev
*id
;
8650 void **space_tail
= (void **)&update
->space_list
;
8653 int current_level
= -1;
8655 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
8657 /* add space for bigger array in update
8659 for (id
= super
->devlist
; id
; id
= id
->next
) {
8660 if (id
->index
== (unsigned)u
->subdev
) {
8661 size
= sizeof_imsm_dev(id
->dev
, 1);
8662 if (u
->new_raid_disks
> u
->old_raid_disks
)
8663 size
+= sizeof(__u32
)*2*
8664 (u
->new_raid_disks
- u
->old_raid_disks
);
8672 if (update
->space_list
== NULL
)
8675 /* add space for disk in update
8677 size
= sizeof(struct dl
);
8683 /* add spare device to update
8685 for (id
= super
->devlist
; id
; id
= id
->next
)
8686 if (id
->index
== (unsigned)u
->subdev
) {
8687 struct imsm_dev
*dev
;
8688 struct imsm_map
*map
;
8690 dev
= get_imsm_dev(super
, u
->subdev
);
8691 map
= get_imsm_map(dev
, MAP_0
);
8692 current_level
= map
->raid_level
;
8695 if ((u
->new_level
== 5) && (u
->new_level
!= current_level
)) {
8696 struct mdinfo
*spares
;
8698 spares
= get_spares_for_grow(st
);
8706 makedev(dev
->disk
.major
,
8708 dl
= get_disk_super(super
,
8711 dl
->index
= u
->old_raid_disks
;
8717 len
= disks_to_mpb_size(u
->new_raid_disks
);
8718 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
8721 case update_size_change
: {
8724 case update_create_array
: {
8725 struct imsm_update_create_array
*u
= (void *) update
->buf
;
8726 struct intel_dev
*dv
;
8727 struct imsm_dev
*dev
= &u
->dev
;
8728 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8730 struct disk_info
*inf
;
8734 inf
= get_disk_info(u
);
8735 len
= sizeof_imsm_dev(dev
, 1);
8736 /* allocate a new super->devlist entry */
8737 dv
= xmalloc(sizeof(*dv
));
8738 dv
->dev
= xmalloc(len
);
8741 /* count how many spares will be converted to members */
8742 for (i
= 0; i
< map
->num_members
; i
++) {
8743 dl
= serial_to_dl(inf
[i
].serial
, super
);
8745 /* hmm maybe it failed?, nothing we can do about
8750 if (count_memberships(dl
, super
) == 0)
8753 len
+= activate
* sizeof(struct imsm_disk
);
8760 /* check if we need a larger metadata buffer */
8761 if (super
->next_buf
)
8762 buf_len
= super
->next_len
;
8764 buf_len
= super
->len
;
8766 if (__le32_to_cpu(mpb
->mpb_size
) + len
> buf_len
) {
8767 /* ok we need a larger buf than what is currently allocated
8768 * if this allocation fails process_update will notice that
8769 * ->next_len is set and ->next_buf is NULL
8771 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + len
, 512);
8772 if (super
->next_buf
)
8773 free(super
->next_buf
);
8775 super
->next_len
= buf_len
;
8776 if (posix_memalign(&super
->next_buf
, 512, buf_len
) == 0)
8777 memset(super
->next_buf
, 0, buf_len
);
8779 super
->next_buf
= NULL
;
8783 /* must be called while manager is quiesced */
8784 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
8786 struct imsm_super
*mpb
= super
->anchor
;
8788 struct imsm_dev
*dev
;
8789 struct imsm_map
*map
;
8790 int i
, j
, num_members
;
8793 dprintf("%s: deleting device[%d] from imsm_super\n",
8796 /* shift all indexes down one */
8797 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
8798 if (iter
->index
> (int)index
)
8800 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
8801 if (iter
->index
> (int)index
)
8804 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8805 dev
= get_imsm_dev(super
, i
);
8806 map
= get_imsm_map(dev
, MAP_0
);
8807 num_members
= map
->num_members
;
8808 for (j
= 0; j
< num_members
; j
++) {
8809 /* update ord entries being careful not to propagate
8810 * ord-flags to the first map
8812 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_X
);
8814 if (ord_to_idx(ord
) <= index
)
8817 map
= get_imsm_map(dev
, MAP_0
);
8818 set_imsm_ord_tbl_ent(map
, j
, ord_to_idx(ord
- 1));
8819 map
= get_imsm_map(dev
, MAP_1
);
8821 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
8826 super
->updates_pending
++;
8828 struct dl
*dl
= *dlp
;
8830 *dlp
= (*dlp
)->next
;
8831 __free_imsm_disk(dl
);
8834 #endif /* MDASSEMBLE */
8836 static void close_targets(int *targets
, int new_disks
)
8843 for (i
= 0; i
< new_disks
; i
++) {
8844 if (targets
[i
] >= 0) {
8851 static int imsm_get_allowed_degradation(int level
, int raid_disks
,
8852 struct intel_super
*super
,
8853 struct imsm_dev
*dev
)
8859 struct imsm_map
*map
;
8862 ret_val
= raid_disks
/2;
8863 /* check map if all disks pairs not failed
8866 map
= get_imsm_map(dev
, MAP_0
);
8867 for (i
= 0; i
< ret_val
; i
++) {
8868 int degradation
= 0;
8869 if (get_imsm_disk(super
, i
) == NULL
)
8871 if (get_imsm_disk(super
, i
+ 1) == NULL
)
8873 if (degradation
== 2)
8876 map
= get_imsm_map(dev
, MAP_1
);
8877 /* if there is no second map
8878 * result can be returned
8882 /* check degradation in second map
8884 for (i
= 0; i
< ret_val
; i
++) {
8885 int degradation
= 0;
8886 if (get_imsm_disk(super
, i
) == NULL
)
8888 if (get_imsm_disk(super
, i
+ 1) == NULL
)
8890 if (degradation
== 2)
8905 /*******************************************************************************
8906 * Function: open_backup_targets
8907 * Description: Function opens file descriptors for all devices given in
8910 * info : general array info
8911 * raid_disks : number of disks
8912 * raid_fds : table of device's file descriptors
8913 * super : intel super for raid10 degradation check
8914 * dev : intel device for raid10 degradation check
8918 ******************************************************************************/
8919 int open_backup_targets(struct mdinfo
*info
, int raid_disks
, int *raid_fds
,
8920 struct intel_super
*super
, struct imsm_dev
*dev
)
8926 for (i
= 0; i
< raid_disks
; i
++)
8929 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
8932 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
)) {
8933 dprintf("disk is faulty!!\n");
8937 if ((sd
->disk
.raid_disk
>= raid_disks
) ||
8938 (sd
->disk
.raid_disk
< 0))
8941 dn
= map_dev(sd
->disk
.major
,
8943 raid_fds
[sd
->disk
.raid_disk
] = dev_open(dn
, O_RDWR
);
8944 if (raid_fds
[sd
->disk
.raid_disk
] < 0) {
8945 fprintf(stderr
, "cannot open component\n");
8950 /* check if maximum array degradation level is not exceeded
8952 if ((raid_disks
- opened
) >
8953 imsm_get_allowed_degradation(info
->new_level
,
8956 fprintf(stderr
, "Not enough disks can be opened.\n");
8957 close_targets(raid_fds
, raid_disks
);
8964 /*******************************************************************************
8965 * Function: init_migr_record_imsm
8966 * Description: Function inits imsm migration record
8968 * super : imsm internal array info
8969 * dev : device under migration
8970 * info : general array info to find the smallest device
8973 ******************************************************************************/
8974 void init_migr_record_imsm(struct supertype
*st
, struct imsm_dev
*dev
,
8975 struct mdinfo
*info
)
8977 struct intel_super
*super
= st
->sb
;
8978 struct migr_record
*migr_rec
= super
->migr_rec
;
8980 unsigned long long dsize
, dev_sectors
;
8981 long long unsigned min_dev_sectors
= -1LLU;
8985 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
8986 struct imsm_map
*map_src
= get_imsm_map(dev
, MAP_1
);
8987 unsigned long long num_migr_units
;
8988 unsigned long long array_blocks
;
8990 memset(migr_rec
, 0, sizeof(struct migr_record
));
8991 migr_rec
->family_num
= __cpu_to_le32(super
->anchor
->family_num
);
8993 /* only ascending reshape supported now */
8994 migr_rec
->ascending_migr
= __cpu_to_le32(1);
8996 migr_rec
->dest_depth_per_unit
= GEN_MIGR_AREA_SIZE
/
8997 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
8998 migr_rec
->dest_depth_per_unit
*=
8999 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
9000 new_data_disks
= imsm_num_data_members(dev
, MAP_0
);
9001 migr_rec
->blocks_per_unit
=
9002 __cpu_to_le32(migr_rec
->dest_depth_per_unit
* new_data_disks
);
9003 migr_rec
->dest_depth_per_unit
=
9004 __cpu_to_le32(migr_rec
->dest_depth_per_unit
);
9005 array_blocks
= info
->component_size
* new_data_disks
;
9007 array_blocks
/ __le32_to_cpu(migr_rec
->blocks_per_unit
);
9009 if (array_blocks
% __le32_to_cpu(migr_rec
->blocks_per_unit
))
9011 migr_rec
->num_migr_units
= __cpu_to_le32(num_migr_units
);
9013 migr_rec
->post_migr_vol_cap
= dev
->size_low
;
9014 migr_rec
->post_migr_vol_cap_hi
= dev
->size_high
;
9017 /* Find the smallest dev */
9018 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
9019 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
9020 fd
= dev_open(nm
, O_RDONLY
);
9023 get_dev_size(fd
, NULL
, &dsize
);
9024 dev_sectors
= dsize
/ 512;
9025 if (dev_sectors
< min_dev_sectors
)
9026 min_dev_sectors
= dev_sectors
;
9029 migr_rec
->ckpt_area_pba
= __cpu_to_le32(min_dev_sectors
-
9030 RAID_DISK_RESERVED_BLOCKS_IMSM_HI
);
9032 write_imsm_migr_rec(st
);
9037 /*******************************************************************************
9038 * Function: save_backup_imsm
9039 * Description: Function saves critical data stripes to Migration Copy Area
9040 * and updates the current migration unit status.
9041 * Use restore_stripes() to form a destination stripe,
9042 * and to write it to the Copy Area.
9044 * st : supertype information
9045 * dev : imsm device that backup is saved for
9046 * info : general array info
9047 * buf : input buffer
9048 * length : length of data to backup (blocks_per_unit)
9052 ******************************************************************************/
9053 int save_backup_imsm(struct supertype
*st
,
9054 struct imsm_dev
*dev
,
9055 struct mdinfo
*info
,
9060 struct intel_super
*super
= st
->sb
;
9061 unsigned long long *target_offsets
= NULL
;
9062 int *targets
= NULL
;
9064 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
9065 int new_disks
= map_dest
->num_members
;
9066 int dest_layout
= 0;
9068 unsigned long long start
;
9069 int data_disks
= imsm_num_data_members(dev
, MAP_0
);
9071 targets
= xmalloc(new_disks
* sizeof(int));
9073 for (i
= 0; i
< new_disks
; i
++)
9076 target_offsets
= xcalloc(new_disks
, sizeof(unsigned long long));
9078 start
= info
->reshape_progress
* 512;
9079 for (i
= 0; i
< new_disks
; i
++) {
9080 target_offsets
[i
] = (unsigned long long)
9081 __le32_to_cpu(super
->migr_rec
->ckpt_area_pba
) * 512;
9082 /* move back copy area adderss, it will be moved forward
9083 * in restore_stripes() using start input variable
9085 target_offsets
[i
] -= start
/data_disks
;
9088 if (open_backup_targets(info
, new_disks
, targets
,
9092 dest_layout
= imsm_level_to_layout(map_dest
->raid_level
);
9093 dest_chunk
= __le16_to_cpu(map_dest
->blocks_per_strip
) * 512;
9095 if (restore_stripes(targets
, /* list of dest devices */
9096 target_offsets
, /* migration record offsets */
9099 map_dest
->raid_level
,
9101 -1, /* source backup file descriptor */
9102 0, /* input buf offset
9103 * always 0 buf is already offseted */
9107 pr_err("Error restoring stripes\n");
9115 close_targets(targets
, new_disks
);
9118 free(target_offsets
);
9123 /*******************************************************************************
9124 * Function: save_checkpoint_imsm
9125 * Description: Function called for current unit status update
9126 * in the migration record. It writes it to disk.
9128 * super : imsm internal array info
9129 * info : general array info
9133 * 2: failure, means no valid migration record
9134 * / no general migration in progress /
9135 ******************************************************************************/
9136 int save_checkpoint_imsm(struct supertype
*st
, struct mdinfo
*info
, int state
)
9138 struct intel_super
*super
= st
->sb
;
9139 unsigned long long blocks_per_unit
;
9140 unsigned long long curr_migr_unit
;
9142 if (load_imsm_migr_rec(super
, info
) != 0) {
9143 dprintf("imsm: ERROR: Cannot read migration record "
9144 "for checkpoint save.\n");
9148 blocks_per_unit
= __le32_to_cpu(super
->migr_rec
->blocks_per_unit
);
9149 if (blocks_per_unit
== 0) {
9150 dprintf("imsm: no migration in progress.\n");
9153 curr_migr_unit
= info
->reshape_progress
/ blocks_per_unit
;
9154 /* check if array is alligned to copy area
9155 * if it is not alligned, add one to current migration unit value
9156 * this can happend on array reshape finish only
9158 if (info
->reshape_progress
% blocks_per_unit
)
9161 super
->migr_rec
->curr_migr_unit
=
9162 __cpu_to_le32(curr_migr_unit
);
9163 super
->migr_rec
->rec_status
= __cpu_to_le32(state
);
9164 super
->migr_rec
->dest_1st_member_lba
=
9165 __cpu_to_le32(curr_migr_unit
*
9166 __le32_to_cpu(super
->migr_rec
->dest_depth_per_unit
));
9167 if (write_imsm_migr_rec(st
) < 0) {
9168 dprintf("imsm: Cannot write migration record "
9169 "outside backup area\n");
9176 /*******************************************************************************
9177 * Function: recover_backup_imsm
9178 * Description: Function recovers critical data from the Migration Copy Area
9179 * while assembling an array.
9181 * super : imsm internal array info
9182 * info : general array info
9184 * 0 : success (or there is no data to recover)
9186 ******************************************************************************/
9187 int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
)
9189 struct intel_super
*super
= st
->sb
;
9190 struct migr_record
*migr_rec
= super
->migr_rec
;
9191 struct imsm_map
*map_dest
= NULL
;
9192 struct intel_dev
*id
= NULL
;
9193 unsigned long long read_offset
;
9194 unsigned long long write_offset
;
9196 int *targets
= NULL
;
9197 int new_disks
, i
, err
;
9200 unsigned long curr_migr_unit
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
9201 unsigned long num_migr_units
= __le32_to_cpu(migr_rec
->num_migr_units
);
9203 int skipped_disks
= 0;
9205 err
= sysfs_get_str(info
, NULL
, "array_state", (char *)buffer
, 20);
9209 /* recover data only during assemblation */
9210 if (strncmp(buffer
, "inactive", 8) != 0)
9212 /* no data to recover */
9213 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
9215 if (curr_migr_unit
>= num_migr_units
)
9218 /* find device during reshape */
9219 for (id
= super
->devlist
; id
; id
= id
->next
)
9220 if (is_gen_migration(id
->dev
))
9225 map_dest
= get_imsm_map(id
->dev
, MAP_0
);
9226 new_disks
= map_dest
->num_members
;
9228 read_offset
= (unsigned long long)
9229 __le32_to_cpu(migr_rec
->ckpt_area_pba
) * 512;
9231 write_offset
= ((unsigned long long)
9232 __le32_to_cpu(migr_rec
->dest_1st_member_lba
) +
9233 pba_of_lba0(map_dest
)) * 512;
9235 unit_len
= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
9236 if (posix_memalign((void **)&buf
, 512, unit_len
) != 0)
9238 targets
= xcalloc(new_disks
, sizeof(int));
9240 if (open_backup_targets(info
, new_disks
, targets
, super
, id
->dev
)) {
9241 pr_err("Cannot open some devices belonging to array.\n");
9245 for (i
= 0; i
< new_disks
; i
++) {
9246 if (targets
[i
] < 0) {
9250 if (lseek64(targets
[i
], read_offset
, SEEK_SET
) < 0) {
9251 pr_err("Cannot seek to block: %s\n",
9256 if ((unsigned)read(targets
[i
], buf
, unit_len
) != unit_len
) {
9257 pr_err("Cannot read copy area block: %s\n",
9262 if (lseek64(targets
[i
], write_offset
, SEEK_SET
) < 0) {
9263 pr_err("Cannot seek to block: %s\n",
9268 if ((unsigned)write(targets
[i
], buf
, unit_len
) != unit_len
) {
9269 pr_err("Cannot restore block: %s\n",
9276 if (skipped_disks
> imsm_get_allowed_degradation(info
->new_level
,
9280 pr_err("Cannot restore data from backup."
9281 " Too many failed disks\n");
9285 if (save_checkpoint_imsm(st
, info
, UNIT_SRC_NORMAL
)) {
9286 /* ignore error == 2, this can mean end of reshape here
9288 dprintf("imsm: Cannot write checkpoint to "
9289 "migration record (UNIT_SRC_NORMAL) during restart\n");
9295 for (i
= 0; i
< new_disks
; i
++)
9304 static char disk_by_path
[] = "/dev/disk/by-path/";
9306 static const char *imsm_get_disk_controller_domain(const char *path
)
9308 char disk_path
[PATH_MAX
];
9312 strncpy(disk_path
, disk_by_path
, PATH_MAX
- 1);
9313 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
9314 if (stat(disk_path
, &st
) == 0) {
9315 struct sys_dev
* hba
;
9318 path
= devt_to_devpath(st
.st_rdev
);
9321 hba
= find_disk_attached_hba(-1, path
);
9322 if (hba
&& hba
->type
== SYS_DEV_SAS
)
9324 else if (hba
&& hba
->type
== SYS_DEV_SATA
)
9328 dprintf("path: %s hba: %s attached: %s\n",
9329 path
, (hba
) ? hba
->path
: "NULL", drv
);
9335 static int imsm_find_array_minor_by_subdev(int subdev
, int container
, int *minor
)
9337 char subdev_name
[20];
9338 struct mdstat_ent
*mdstat
;
9340 sprintf(subdev_name
, "%d", subdev
);
9341 mdstat
= mdstat_by_subdev(subdev_name
, container
);
9345 *minor
= mdstat
->devnum
;
9346 free_mdstat(mdstat
);
9350 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
9351 struct geo_params
*geo
,
9352 int *old_raid_disks
,
9355 /* currently we only support increasing the number of devices
9356 * for a container. This increases the number of device for each
9357 * member array. They must all be RAID0 or RAID5.
9360 struct mdinfo
*info
, *member
;
9361 int devices_that_can_grow
= 0;
9363 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): "
9364 "st->devnum = (%i)\n",
9367 if (geo
->size
> 0 ||
9368 geo
->level
!= UnSet
||
9369 geo
->layout
!= UnSet
||
9370 geo
->chunksize
!= 0 ||
9371 geo
->raid_disks
== UnSet
) {
9372 dprintf("imsm: Container operation is allowed for "
9373 "raid disks number change only.\n");
9377 if (direction
== ROLLBACK_METADATA_CHANGES
) {
9378 dprintf("imsm: Metadata changes rollback is not supported for "
9379 "container operation.\n");
9383 info
= container_content_imsm(st
, NULL
);
9384 for (member
= info
; member
; member
= member
->next
) {
9388 dprintf("imsm: checking device_num: %i\n",
9389 member
->container_member
);
9391 if (geo
->raid_disks
<= member
->array
.raid_disks
) {
9392 /* we work on container for Online Capacity Expansion
9393 * only so raid_disks has to grow
9395 dprintf("imsm: for container operation raid disks "
9396 "increase is required\n");
9400 if ((info
->array
.level
!= 0) &&
9401 (info
->array
.level
!= 5)) {
9402 /* we cannot use this container with other raid level
9404 dprintf("imsm: for container operation wrong"
9405 " raid level (%i) detected\n",
9409 /* check for platform support
9410 * for this raid level configuration
9412 struct intel_super
*super
= st
->sb
;
9413 if (!is_raid_level_supported(super
->orom
,
9414 member
->array
.level
,
9416 dprintf("platform does not support raid%d with"
9420 geo
->raid_disks
> 1 ? "s" : "");
9423 /* check if component size is aligned to chunk size
9425 if (info
->component_size
%
9426 (info
->array
.chunk_size
/512)) {
9427 dprintf("Component size is not aligned to "
9433 if (*old_raid_disks
&&
9434 info
->array
.raid_disks
!= *old_raid_disks
)
9436 *old_raid_disks
= info
->array
.raid_disks
;
9438 /* All raid5 and raid0 volumes in container
9439 * have to be ready for Online Capacity Expansion
9440 * so they need to be assembled. We have already
9441 * checked that no recovery etc is happening.
9443 result
= imsm_find_array_minor_by_subdev(member
->container_member
,
9447 dprintf("imsm: cannot find array\n");
9450 devices_that_can_grow
++;
9453 if (!member
&& devices_that_can_grow
)
9457 dprintf("\tContainer operation allowed\n");
9459 dprintf("\tError: %i\n", ret_val
);
9464 /* Function: get_spares_for_grow
9465 * Description: Allocates memory and creates list of spare devices
9466 * avaliable in container. Checks if spare drive size is acceptable.
9467 * Parameters: Pointer to the supertype structure
9468 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
9471 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
9473 unsigned long long min_size
= min_acceptable_spare_size_imsm(st
);
9474 return container_choose_spares(st
, min_size
, NULL
, NULL
, NULL
, 0);
9477 /******************************************************************************
9478 * function: imsm_create_metadata_update_for_reshape
9479 * Function creates update for whole IMSM container.
9481 ******************************************************************************/
9482 static int imsm_create_metadata_update_for_reshape(
9483 struct supertype
*st
,
9484 struct geo_params
*geo
,
9486 struct imsm_update_reshape
**updatep
)
9488 struct intel_super
*super
= st
->sb
;
9489 struct imsm_super
*mpb
= super
->anchor
;
9490 int update_memory_size
= 0;
9491 struct imsm_update_reshape
*u
= NULL
;
9492 struct mdinfo
*spares
= NULL
;
9494 int delta_disks
= 0;
9497 dprintf("imsm_update_metadata_for_reshape(enter) raid_disks = %i\n",
9500 delta_disks
= geo
->raid_disks
- old_raid_disks
;
9502 /* size of all update data without anchor */
9503 update_memory_size
= sizeof(struct imsm_update_reshape
);
9505 /* now add space for spare disks that we need to add. */
9506 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
9508 u
= xcalloc(1, update_memory_size
);
9509 u
->type
= update_reshape_container_disks
;
9510 u
->old_raid_disks
= old_raid_disks
;
9511 u
->new_raid_disks
= geo
->raid_disks
;
9513 /* now get spare disks list
9515 spares
= get_spares_for_grow(st
);
9518 || delta_disks
> spares
->array
.spare_disks
) {
9519 pr_err("imsm: ERROR: Cannot get spare devices "
9520 "for %s.\n", geo
->dev_name
);
9525 /* we have got spares
9526 * update disk list in imsm_disk list table in anchor
9528 dprintf("imsm: %i spares are available.\n\n",
9529 spares
->array
.spare_disks
);
9532 for (i
= 0; i
< delta_disks
; i
++) {
9537 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
9539 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
9540 dl
->index
= mpb
->num_disks
;
9550 dprintf("imsm: reshape update preparation :");
9551 if (i
== delta_disks
) {
9554 return update_memory_size
;
9557 dprintf(" Error\n");
9563 /******************************************************************************
9564 * function: imsm_create_metadata_update_for_size_change()
9565 * Creates update for IMSM array for array size change.
9567 ******************************************************************************/
9568 static int imsm_create_metadata_update_for_size_change(
9569 struct supertype
*st
,
9570 struct geo_params
*geo
,
9571 struct imsm_update_size_change
**updatep
)
9573 struct intel_super
*super
= st
->sb
;
9574 int update_memory_size
= 0;
9575 struct imsm_update_size_change
*u
= NULL
;
9577 dprintf("imsm_create_metadata_update_for_size_change(enter)"
9578 " New size = %llu\n", geo
->size
);
9580 /* size of all update data without anchor */
9581 update_memory_size
= sizeof(struct imsm_update_size_change
);
9583 u
= xcalloc(1, update_memory_size
);
9584 u
->type
= update_size_change
;
9585 u
->subdev
= super
->current_vol
;
9586 u
->new_size
= geo
->size
;
9588 dprintf("imsm: reshape update preparation : OK\n");
9591 return update_memory_size
;
9594 /******************************************************************************
9595 * function: imsm_create_metadata_update_for_migration()
9596 * Creates update for IMSM array.
9598 ******************************************************************************/
9599 static int imsm_create_metadata_update_for_migration(
9600 struct supertype
*st
,
9601 struct geo_params
*geo
,
9602 struct imsm_update_reshape_migration
**updatep
)
9604 struct intel_super
*super
= st
->sb
;
9605 int update_memory_size
= 0;
9606 struct imsm_update_reshape_migration
*u
= NULL
;
9607 struct imsm_dev
*dev
;
9608 int previous_level
= -1;
9610 dprintf("imsm_create_metadata_update_for_migration(enter)"
9611 " New Level = %i\n", geo
->level
);
9613 /* size of all update data without anchor */
9614 update_memory_size
= sizeof(struct imsm_update_reshape_migration
);
9616 u
= xcalloc(1, update_memory_size
);
9617 u
->type
= update_reshape_migration
;
9618 u
->subdev
= super
->current_vol
;
9619 u
->new_level
= geo
->level
;
9620 u
->new_layout
= geo
->layout
;
9621 u
->new_raid_disks
= u
->old_raid_disks
= geo
->raid_disks
;
9622 u
->new_disks
[0] = -1;
9623 u
->new_chunksize
= -1;
9625 dev
= get_imsm_dev(super
, u
->subdev
);
9627 struct imsm_map
*map
;
9629 map
= get_imsm_map(dev
, MAP_0
);
9631 int current_chunk_size
=
9632 __le16_to_cpu(map
->blocks_per_strip
) / 2;
9634 if (geo
->chunksize
!= current_chunk_size
) {
9635 u
->new_chunksize
= geo
->chunksize
/ 1024;
9637 "chunk size change from %i to %i\n",
9638 current_chunk_size
, u
->new_chunksize
);
9640 previous_level
= map
->raid_level
;
9643 if ((geo
->level
== 5) && (previous_level
== 0)) {
9644 struct mdinfo
*spares
= NULL
;
9646 u
->new_raid_disks
++;
9647 spares
= get_spares_for_grow(st
);
9648 if ((spares
== NULL
) || (spares
->array
.spare_disks
< 1)) {
9651 update_memory_size
= 0;
9652 dprintf("error: cannot get spare device "
9653 "for requested migration");
9658 dprintf("imsm: reshape update preparation : OK\n");
9661 return update_memory_size
;
9664 static void imsm_update_metadata_locally(struct supertype
*st
,
9667 struct metadata_update mu
;
9672 mu
.space_list
= NULL
;
9674 imsm_prepare_update(st
, &mu
);
9675 imsm_process_update(st
, &mu
);
9677 while (mu
.space_list
) {
9678 void **space
= mu
.space_list
;
9679 mu
.space_list
= *space
;
9684 /***************************************************************************
9685 * Function: imsm_analyze_change
9686 * Description: Function analyze change for single volume
9687 * and validate if transition is supported
9688 * Parameters: Geometry parameters, supertype structure,
9689 * metadata change direction (apply/rollback)
9690 * Returns: Operation type code on success, -1 if fail
9691 ****************************************************************************/
9692 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
9693 struct geo_params
*geo
,
9700 /* number of added/removed disks in operation result */
9701 int devNumChange
= 0;
9702 /* imsm compatible layout value for array geometry verification */
9703 int imsm_layout
= -1;
9705 struct imsm_dev
*dev
;
9706 struct intel_super
*super
;
9707 unsigned long long current_size
;
9708 unsigned long long free_size
;
9709 unsigned long long max_size
;
9712 getinfo_super_imsm_volume(st
, &info
, NULL
);
9713 if ((geo
->level
!= info
.array
.level
) &&
9714 (geo
->level
>= 0) &&
9715 (geo
->level
!= UnSet
)) {
9716 switch (info
.array
.level
) {
9718 if (geo
->level
== 5) {
9719 change
= CH_MIGRATION
;
9720 if (geo
->layout
!= ALGORITHM_LEFT_ASYMMETRIC
) {
9721 pr_err("Error. Requested Layout "
9722 "not supported (left-asymmetric layout "
9723 "is supported only)!\n");
9725 goto analyse_change_exit
;
9727 imsm_layout
= geo
->layout
;
9729 devNumChange
= 1; /* parity disk added */
9730 } else if (geo
->level
== 10) {
9731 change
= CH_TAKEOVER
;
9733 devNumChange
= 2; /* two mirrors added */
9734 imsm_layout
= 0x102; /* imsm supported layout */
9739 if (geo
->level
== 0) {
9740 change
= CH_TAKEOVER
;
9742 devNumChange
= -(geo
->raid_disks
/2);
9743 imsm_layout
= 0; /* imsm raid0 layout */
9748 pr_err("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 */
9770 pr_err("Error. Layout Migration from %d to %d "
9772 info
.array
.layout
, geo
->layout
);
9774 goto analyse_change_exit
;
9777 geo
->layout
= info
.array
.layout
;
9778 if (imsm_layout
== -1)
9779 imsm_layout
= info
.array
.layout
;
9782 if ((geo
->chunksize
> 0) && (geo
->chunksize
!= UnSet
)
9783 && (geo
->chunksize
!= info
.array
.chunk_size
))
9784 change
= CH_MIGRATION
;
9786 geo
->chunksize
= info
.array
.chunk_size
;
9788 chunk
= geo
->chunksize
/ 1024;
9791 dev
= get_imsm_dev(super
, super
->current_vol
);
9792 data_disks
= imsm_num_data_members(dev
, MAP_0
);
9793 /* compute current size per disk member
9795 current_size
= info
.custom_array_size
/ data_disks
;
9797 if ((geo
->size
> 0) && (geo
->size
!= MAX_SIZE
)) {
9798 /* align component size
9800 geo
->size
= imsm_component_size_aligment_check(
9801 get_imsm_raid_level(dev
->vol
.map
),
9804 if (geo
->size
== 0) {
9805 pr_err("Error. Size expansion is " \
9806 "supported only (current size is %llu, " \
9807 "requested size /rounded/ is 0).\n",
9809 goto analyse_change_exit
;
9813 if ((current_size
!= geo
->size
) && (geo
->size
> 0)) {
9815 pr_err("Error. Size change should be the only "
9816 "one at a time.\n");
9818 goto analyse_change_exit
;
9820 if ((super
->current_vol
+ 1) != super
->anchor
->num_raid_devs
) {
9821 pr_err("Error. The last volume in container "
9822 "can be expanded only (%i/%i).\n",
9823 super
->current_vol
, st
->devnum
);
9824 goto analyse_change_exit
;
9826 /* check the maximum available size
9828 rv
= imsm_get_free_size(st
, dev
->vol
.map
->num_members
,
9829 0, chunk
, &free_size
);
9831 /* Cannot find maximum available space
9835 max_size
= free_size
+ current_size
;
9836 /* align component size
9838 max_size
= imsm_component_size_aligment_check(
9839 get_imsm_raid_level(dev
->vol
.map
),
9843 if (geo
->size
== MAX_SIZE
) {
9844 /* requested size change to the maximum available size
9846 if (max_size
== 0) {
9847 pr_err("Error. Cannot find "
9848 "maximum available space.\n");
9850 goto analyse_change_exit
;
9852 geo
->size
= max_size
;
9855 if ((direction
== ROLLBACK_METADATA_CHANGES
)) {
9856 /* accept size for rollback only
9859 /* round size due to metadata compatibility
9861 geo
->size
= (geo
->size
>> SECT_PER_MB_SHIFT
)
9862 << SECT_PER_MB_SHIFT
;
9863 dprintf("Prepare update for size change to %llu\n",
9865 if (current_size
>= geo
->size
) {
9866 pr_err("Error. Size expansion is "
9867 "supported only (current size is %llu, "
9868 "requested size /rounded/ is %llu).\n",
9869 current_size
, geo
->size
);
9870 goto analyse_change_exit
;
9872 if (max_size
&& geo
->size
> max_size
) {
9873 pr_err("Error. Requested size is larger "
9874 "than maximum available size (maximum "
9875 "available size is %llu, "
9876 "requested size /rounded/ is %llu).\n",
9877 max_size
, geo
->size
);
9878 goto analyse_change_exit
;
9881 geo
->size
*= data_disks
;
9882 geo
->raid_disks
= dev
->vol
.map
->num_members
;
9883 change
= CH_ARRAY_SIZE
;
9885 if (!validate_geometry_imsm(st
,
9888 geo
->raid_disks
+ devNumChange
,
9890 geo
->size
, INVALID_SECTORS
,
9895 struct intel_super
*super
= st
->sb
;
9896 struct imsm_super
*mpb
= super
->anchor
;
9898 if (mpb
->num_raid_devs
> 1) {
9899 pr_err("Error. Cannot perform operation on %s"
9900 "- for this operation it MUST be single "
9901 "array in container\n",
9907 analyse_change_exit
:
9908 if ((direction
== ROLLBACK_METADATA_CHANGES
) &&
9909 ((change
== CH_MIGRATION
) || (change
== CH_TAKEOVER
))) {
9910 dprintf("imsm: Metadata changes rollback is not supported for "
9911 "migration and takeover operations.\n");
9917 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
9919 struct intel_super
*super
= st
->sb
;
9920 struct imsm_update_takeover
*u
;
9922 u
= xmalloc(sizeof(struct imsm_update_takeover
));
9924 u
->type
= update_takeover
;
9925 u
->subarray
= super
->current_vol
;
9927 /* 10->0 transition */
9928 if (geo
->level
== 0)
9929 u
->direction
= R10_TO_R0
;
9931 /* 0->10 transition */
9932 if (geo
->level
== 10)
9933 u
->direction
= R0_TO_R10
;
9935 /* update metadata locally */
9936 imsm_update_metadata_locally(st
, u
,
9937 sizeof(struct imsm_update_takeover
));
9938 /* and possibly remotely */
9939 if (st
->update_tail
)
9940 append_metadata_update(st
, u
,
9941 sizeof(struct imsm_update_takeover
));
9948 static int imsm_reshape_super(struct supertype
*st
, unsigned long long size
,
9950 int layout
, int chunksize
, int raid_disks
,
9951 int delta_disks
, char *backup
, char *dev
,
9952 int direction
, int verbose
)
9955 struct geo_params geo
;
9957 dprintf("imsm: reshape_super called.\n");
9959 memset(&geo
, 0, sizeof(struct geo_params
));
9962 geo
.dev_id
= st
->devnum
;
9965 geo
.layout
= layout
;
9966 geo
.chunksize
= chunksize
;
9967 geo
.raid_disks
= raid_disks
;
9968 if (delta_disks
!= UnSet
)
9969 geo
.raid_disks
+= delta_disks
;
9971 dprintf("\tfor level : %i\n", geo
.level
);
9972 dprintf("\tfor raid_disks : %i\n", geo
.raid_disks
);
9974 if (experimental() == 0)
9977 if (st
->container_dev
== st
->devnum
) {
9978 /* On container level we can only increase number of devices. */
9979 dprintf("imsm: info: Container operation\n");
9980 int old_raid_disks
= 0;
9982 if (imsm_reshape_is_allowed_on_container(
9983 st
, &geo
, &old_raid_disks
, direction
)) {
9984 struct imsm_update_reshape
*u
= NULL
;
9987 len
= imsm_create_metadata_update_for_reshape(
9988 st
, &geo
, old_raid_disks
, &u
);
9991 dprintf("imsm: Cannot prepare update\n");
9992 goto exit_imsm_reshape_super
;
9996 /* update metadata locally */
9997 imsm_update_metadata_locally(st
, u
, len
);
9998 /* and possibly remotely */
9999 if (st
->update_tail
)
10000 append_metadata_update(st
, u
, len
);
10005 pr_err("(imsm) Operation "
10006 "is not allowed on this container\n");
10009 /* On volume level we support following operations
10010 * - takeover: raid10 -> raid0; raid0 -> raid10
10011 * - chunk size migration
10012 * - migration: raid5 -> raid0; raid0 -> raid5
10014 struct intel_super
*super
= st
->sb
;
10015 struct intel_dev
*dev
= super
->devlist
;
10016 int change
, devnum
;
10017 dprintf("imsm: info: Volume operation\n");
10018 /* find requested device */
10020 if (imsm_find_array_minor_by_subdev(
10021 dev
->index
, st
->container_dev
, &devnum
) == 0
10022 && devnum
== geo
.dev_id
)
10027 pr_err("Cannot find %s (%i) subarray\n",
10028 geo
.dev_name
, geo
.dev_id
);
10029 goto exit_imsm_reshape_super
;
10031 super
->current_vol
= dev
->index
;
10032 change
= imsm_analyze_change(st
, &geo
, direction
);
10035 ret_val
= imsm_takeover(st
, &geo
);
10037 case CH_MIGRATION
: {
10038 struct imsm_update_reshape_migration
*u
= NULL
;
10040 imsm_create_metadata_update_for_migration(
10044 "Cannot prepare update\n");
10048 /* update metadata locally */
10049 imsm_update_metadata_locally(st
, u
, len
);
10050 /* and possibly remotely */
10051 if (st
->update_tail
)
10052 append_metadata_update(st
, u
, len
);
10057 case CH_ARRAY_SIZE
: {
10058 struct imsm_update_size_change
*u
= NULL
;
10060 imsm_create_metadata_update_for_size_change(
10064 "Cannot prepare update\n");
10068 /* update metadata locally */
10069 imsm_update_metadata_locally(st
, u
, len
);
10070 /* and possibly remotely */
10071 if (st
->update_tail
)
10072 append_metadata_update(st
, u
, len
);
10082 exit_imsm_reshape_super
:
10083 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
10087 /*******************************************************************************
10088 * Function: wait_for_reshape_imsm
10089 * Description: Function writes new sync_max value and waits until
10090 * reshape process reach new position
10092 * sra : general array info
10093 * ndata : number of disks in new array's layout
10096 * 1 : there is no reshape in progress,
10098 ******************************************************************************/
10099 int wait_for_reshape_imsm(struct mdinfo
*sra
, int ndata
)
10101 int fd
= sysfs_get_fd(sra
, NULL
, "reshape_position");
10102 unsigned long long completed
;
10103 /* to_complete : new sync_max position */
10104 unsigned long long to_complete
= sra
->reshape_progress
;
10105 unsigned long long position_to_set
= to_complete
/ ndata
;
10108 dprintf("imsm: wait_for_reshape_imsm() "
10109 "cannot open reshape_position\n");
10113 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
10114 dprintf("imsm: wait_for_reshape_imsm() "
10115 "cannot read reshape_position (no reshape in progres)\n");
10120 if (completed
> to_complete
) {
10121 dprintf("imsm: wait_for_reshape_imsm() "
10122 "wrong next position to set %llu (%llu)\n",
10123 to_complete
, completed
);
10127 dprintf("Position set: %llu\n", position_to_set
);
10128 if (sysfs_set_num(sra
, NULL
, "sync_max",
10129 position_to_set
) != 0) {
10130 dprintf("imsm: wait_for_reshape_imsm() "
10131 "cannot set reshape position to %llu\n",
10142 select(fd
+1, &rfds
, NULL
, NULL
, NULL
);
10143 if (sysfs_get_str(sra
, NULL
, "sync_action",
10145 strncmp(action
, "reshape", 7) != 0)
10147 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
10148 dprintf("imsm: wait_for_reshape_imsm() "
10149 "cannot read reshape_position (in loop)\n");
10153 } while (completed
< to_complete
);
10159 /*******************************************************************************
10160 * Function: check_degradation_change
10161 * Description: Check that array hasn't become failed.
10163 * info : for sysfs access
10164 * sources : source disks descriptors
10165 * degraded: previous degradation level
10167 * degradation level
10168 ******************************************************************************/
10169 int check_degradation_change(struct mdinfo
*info
,
10173 unsigned long long new_degraded
;
10176 rv
= sysfs_get_ll(info
, NULL
, "degraded", &new_degraded
);
10177 if ((rv
== -1) || (new_degraded
!= (unsigned long long)degraded
)) {
10178 /* check each device to ensure it is still working */
10181 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
10182 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
10184 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
10186 if (sysfs_get_str(info
,
10187 sd
, "state", sbuf
, 20) < 0 ||
10188 strstr(sbuf
, "faulty") ||
10189 strstr(sbuf
, "in_sync") == NULL
) {
10190 /* this device is dead */
10191 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
10192 if (sd
->disk
.raid_disk
>= 0 &&
10193 sources
[sd
->disk
.raid_disk
] >= 0) {
10195 sd
->disk
.raid_disk
]);
10196 sources
[sd
->disk
.raid_disk
] =
10205 return new_degraded
;
10208 /*******************************************************************************
10209 * Function: imsm_manage_reshape
10210 * Description: Function finds array under reshape and it manages reshape
10211 * process. It creates stripes backups (if required) and sets
10214 * afd : Backup handle (nattive) - not used
10215 * sra : general array info
10216 * reshape : reshape parameters - not used
10217 * st : supertype structure
10218 * blocks : size of critical section [blocks]
10219 * fds : table of source device descriptor
10220 * offsets : start of array (offest per devices)
10222 * destfd : table of destination device descriptor
10223 * destoffsets : table of destination offsets (per device)
10225 * 1 : success, reshape is done
10227 ******************************************************************************/
10228 static int imsm_manage_reshape(
10229 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
10230 struct supertype
*st
, unsigned long backup_blocks
,
10231 int *fds
, unsigned long long *offsets
,
10232 int dests
, int *destfd
, unsigned long long *destoffsets
)
10235 struct intel_super
*super
= st
->sb
;
10236 struct intel_dev
*dv
= NULL
;
10237 struct imsm_dev
*dev
= NULL
;
10238 struct imsm_map
*map_src
;
10239 int migr_vol_qan
= 0;
10240 int ndata
, odata
; /* [bytes] */
10241 int chunk
; /* [bytes] */
10242 struct migr_record
*migr_rec
;
10244 unsigned int buf_size
; /* [bytes] */
10245 unsigned long long max_position
; /* array size [bytes] */
10246 unsigned long long next_step
; /* [blocks]/[bytes] */
10247 unsigned long long old_data_stripe_length
;
10248 unsigned long long start_src
; /* [bytes] */
10249 unsigned long long start
; /* [bytes] */
10250 unsigned long long start_buf_shift
; /* [bytes] */
10252 int source_layout
= 0;
10254 if (!fds
|| !offsets
|| !sra
)
10257 /* Find volume during the reshape */
10258 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
10259 if (dv
->dev
->vol
.migr_type
== MIGR_GEN_MIGR
10260 && dv
->dev
->vol
.migr_state
== 1) {
10265 /* Only one volume can migrate at the same time */
10266 if (migr_vol_qan
!= 1) {
10267 pr_err(": %s", migr_vol_qan
?
10268 "Number of migrating volumes greater than 1\n" :
10269 "There is no volume during migrationg\n");
10273 map_src
= get_imsm_map(dev
, MAP_1
);
10274 if (map_src
== NULL
)
10277 ndata
= imsm_num_data_members(dev
, MAP_0
);
10278 odata
= imsm_num_data_members(dev
, MAP_1
);
10280 chunk
= __le16_to_cpu(map_src
->blocks_per_strip
) * 512;
10281 old_data_stripe_length
= odata
* chunk
;
10283 migr_rec
= super
->migr_rec
;
10285 /* initialize migration record for start condition */
10286 if (sra
->reshape_progress
== 0)
10287 init_migr_record_imsm(st
, dev
, sra
);
10289 if (__le32_to_cpu(migr_rec
->rec_status
) != UNIT_SRC_NORMAL
) {
10290 dprintf("imsm: cannot restart migration when data "
10291 "are present in copy area.\n");
10294 /* Save checkpoint to update migration record for current
10295 * reshape position (in md). It can be farther than current
10296 * reshape position in metadata.
10298 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
10299 /* ignore error == 2, this can mean end of reshape here
10301 dprintf("imsm: Cannot write checkpoint to "
10302 "migration record (UNIT_SRC_NORMAL, "
10303 "initial save)\n");
10308 /* size for data */
10309 buf_size
= __le32_to_cpu(migr_rec
->blocks_per_unit
) * 512;
10310 /* extend buffer size for parity disk */
10311 buf_size
+= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
10312 /* add space for stripe aligment */
10313 buf_size
+= old_data_stripe_length
;
10314 if (posix_memalign((void **)&buf
, 4096, buf_size
)) {
10315 dprintf("imsm: Cannot allocate checpoint buffer\n");
10319 max_position
= sra
->component_size
* ndata
;
10320 source_layout
= imsm_level_to_layout(map_src
->raid_level
);
10322 while (__le32_to_cpu(migr_rec
->curr_migr_unit
) <
10323 __le32_to_cpu(migr_rec
->num_migr_units
)) {
10324 /* current reshape position [blocks] */
10325 unsigned long long current_position
=
10326 __le32_to_cpu(migr_rec
->blocks_per_unit
)
10327 * __le32_to_cpu(migr_rec
->curr_migr_unit
);
10328 unsigned long long border
;
10330 /* Check that array hasn't become failed.
10332 degraded
= check_degradation_change(sra
, fds
, degraded
);
10333 if (degraded
> 1) {
10334 dprintf("imsm: Abort reshape due to degradation"
10335 " level (%i)\n", degraded
);
10339 next_step
= __le32_to_cpu(migr_rec
->blocks_per_unit
);
10341 if ((current_position
+ next_step
) > max_position
)
10342 next_step
= max_position
- current_position
;
10344 start
= current_position
* 512;
10346 /* allign reading start to old geometry */
10347 start_buf_shift
= start
% old_data_stripe_length
;
10348 start_src
= start
- start_buf_shift
;
10350 border
= (start_src
/ odata
) - (start
/ ndata
);
10352 if (border
<= __le32_to_cpu(migr_rec
->dest_depth_per_unit
)) {
10353 /* save critical stripes to buf
10354 * start - start address of current unit
10355 * to backup [bytes]
10356 * start_src - start address of current unit
10357 * to backup alligned to source array
10360 unsigned long long next_step_filler
= 0;
10361 unsigned long long copy_length
= next_step
* 512;
10363 /* allign copy area length to stripe in old geometry */
10364 next_step_filler
= ((copy_length
+ start_buf_shift
)
10365 % old_data_stripe_length
);
10366 if (next_step_filler
)
10367 next_step_filler
= (old_data_stripe_length
10368 - next_step_filler
);
10369 dprintf("save_stripes() parameters: start = %llu,"
10370 "\tstart_src = %llu,\tnext_step*512 = %llu,"
10371 "\tstart_in_buf_shift = %llu,"
10372 "\tnext_step_filler = %llu\n",
10373 start
, start_src
, copy_length
,
10374 start_buf_shift
, next_step_filler
);
10376 if (save_stripes(fds
, offsets
, map_src
->num_members
,
10377 chunk
, map_src
->raid_level
,
10378 source_layout
, 0, NULL
, start_src
,
10380 next_step_filler
+ start_buf_shift
,
10382 dprintf("imsm: Cannot save stripes"
10386 /* Convert data to destination format and store it
10387 * in backup general migration area
10389 if (save_backup_imsm(st
, dev
, sra
,
10390 buf
+ start_buf_shift
, copy_length
)) {
10391 dprintf("imsm: Cannot save stripes to "
10392 "target devices\n");
10395 if (save_checkpoint_imsm(st
, sra
,
10396 UNIT_SRC_IN_CP_AREA
)) {
10397 dprintf("imsm: Cannot write checkpoint to "
10398 "migration record (UNIT_SRC_IN_CP_AREA)\n");
10402 /* set next step to use whole border area */
10403 border
/= next_step
;
10405 next_step
*= border
;
10407 /* When data backed up, checkpoint stored,
10408 * kick the kernel to reshape unit of data
10410 next_step
= next_step
+ sra
->reshape_progress
;
10411 /* limit next step to array max position */
10412 if (next_step
> max_position
)
10413 next_step
= max_position
;
10414 sysfs_set_num(sra
, NULL
, "suspend_lo", sra
->reshape_progress
);
10415 sysfs_set_num(sra
, NULL
, "suspend_hi", next_step
);
10416 sra
->reshape_progress
= next_step
;
10418 /* wait until reshape finish */
10419 if (wait_for_reshape_imsm(sra
, ndata
) < 0) {
10420 dprintf("wait_for_reshape_imsm returned error!\n");
10424 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
10425 /* ignore error == 2, this can mean end of reshape here
10427 dprintf("imsm: Cannot write checkpoint to "
10428 "migration record (UNIT_SRC_NORMAL)\n");
10434 /* return '1' if done */
10438 abort_reshape(sra
);
10442 #endif /* MDASSEMBLE */
10444 struct superswitch super_imsm
= {
10446 .examine_super
= examine_super_imsm
,
10447 .brief_examine_super
= brief_examine_super_imsm
,
10448 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
10449 .export_examine_super
= export_examine_super_imsm
,
10450 .detail_super
= detail_super_imsm
,
10451 .brief_detail_super
= brief_detail_super_imsm
,
10452 .write_init_super
= write_init_super_imsm
,
10453 .validate_geometry
= validate_geometry_imsm
,
10454 .add_to_super
= add_to_super_imsm
,
10455 .remove_from_super
= remove_from_super_imsm
,
10456 .detail_platform
= detail_platform_imsm
,
10457 .export_detail_platform
= export_detail_platform_imsm
,
10458 .kill_subarray
= kill_subarray_imsm
,
10459 .update_subarray
= update_subarray_imsm
,
10460 .load_container
= load_container_imsm
,
10461 .default_geometry
= default_geometry_imsm
,
10462 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
10463 .reshape_super
= imsm_reshape_super
,
10464 .manage_reshape
= imsm_manage_reshape
,
10465 .recover_backup
= recover_backup_imsm
,
10467 .match_home
= match_home_imsm
,
10468 .uuid_from_super
= uuid_from_super_imsm
,
10469 .getinfo_super
= getinfo_super_imsm
,
10470 .getinfo_super_disks
= getinfo_super_disks_imsm
,
10471 .update_super
= update_super_imsm
,
10473 .avail_size
= avail_size_imsm
,
10474 .min_acceptable_spare_size
= min_acceptable_spare_size_imsm
,
10476 .compare_super
= compare_super_imsm
,
10478 .load_super
= load_super_imsm
,
10479 .init_super
= init_super_imsm
,
10480 .store_super
= store_super_imsm
,
10481 .free_super
= free_super_imsm
,
10482 .match_metadata_desc
= match_metadata_desc_imsm
,
10483 .container_content
= container_content_imsm
,
10491 .open_new
= imsm_open_new
,
10492 .set_array_state
= imsm_set_array_state
,
10493 .set_disk
= imsm_set_disk
,
10494 .sync_metadata
= imsm_sync_metadata
,
10495 .activate_spare
= imsm_activate_spare
,
10496 .process_update
= imsm_process_update
,
10497 .prepare_update
= imsm_prepare_update
,
10498 #endif /* MDASSEMBLE */