2 * mdadm - Intel(R) Matrix Storage Manager Support
4 * Copyright (C) 2002-2008 Intel Corporation
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20 #define HAVE_STDINT_H 1
24 #include "platform-intel.h"
30 /* MPB == Metadata Parameter Block */
31 #define MPB_SIGNATURE "Intel Raid ISM Cfg Sig. "
32 #define MPB_SIG_LEN (strlen(MPB_SIGNATURE))
33 #define MPB_VERSION_RAID0 "1.0.00"
34 #define MPB_VERSION_RAID1 "1.1.00"
35 #define MPB_VERSION_MANY_VOLUMES_PER_ARRAY "1.2.00"
36 #define MPB_VERSION_3OR4_DISK_ARRAY "1.2.01"
37 #define MPB_VERSION_RAID5 "1.2.02"
38 #define MPB_VERSION_5OR6_DISK_ARRAY "1.2.04"
39 #define MPB_VERSION_CNG "1.2.06"
40 #define MPB_VERSION_ATTRIBS "1.3.00"
41 #define MAX_SIGNATURE_LENGTH 32
42 #define MAX_RAID_SERIAL_LEN 16
45 #define MPB_ATTRIB_RAID0 __cpu_to_le32(0x00000001)
47 #define MPB_ATTRIB_RAID1 __cpu_to_le32(0x00000002)
49 #define MPB_ATTRIB_RAID10 __cpu_to_le32(0x00000004)
51 #define MPB_ATTRIB_RAID1E __cpu_to_le32(0x00000008)
53 #define MPB_ATTRIB_RAID5 __cpu_to_le32(0x00000010)
54 /* supports RAID CNG */
55 #define MPB_ATTRIB_RAIDCNG __cpu_to_le32(0x00000020)
56 /* supports expanded stripe sizes of 256K, 512K and 1MB */
57 #define MPB_ATTRIB_EXP_STRIPE_SIZE __cpu_to_le32(0x00000040)
59 /* The OROM Support RST Caching of Volumes */
60 #define MPB_ATTRIB_NVM __cpu_to_le32(0x02000000)
61 /* The OROM supports creating disks greater than 2TB */
62 #define MPB_ATTRIB_2TB_DISK __cpu_to_le32(0x04000000)
63 /* The OROM supports Bad Block Management */
64 #define MPB_ATTRIB_BBM __cpu_to_le32(0x08000000)
66 /* THe OROM Supports NVM Caching of Volumes */
67 #define MPB_ATTRIB_NEVER_USE2 __cpu_to_le32(0x10000000)
68 /* The OROM supports creating volumes greater than 2TB */
69 #define MPB_ATTRIB_2TB __cpu_to_le32(0x20000000)
70 /* originally for PMP, now it's wasted b/c. Never use this bit! */
71 #define MPB_ATTRIB_NEVER_USE __cpu_to_le32(0x40000000)
72 /* Verify MPB contents against checksum after reading MPB */
73 #define MPB_ATTRIB_CHECKSUM_VERIFY __cpu_to_le32(0x80000000)
75 /* Define all supported attributes that have to be accepted by mdadm
77 #define MPB_ATTRIB_SUPPORTED (MPB_ATTRIB_CHECKSUM_VERIFY | \
79 MPB_ATTRIB_2TB_DISK | \
84 MPB_ATTRIB_EXP_STRIPE_SIZE)
86 /* Define attributes that are unused but not harmful */
87 #define MPB_ATTRIB_IGNORED (MPB_ATTRIB_NEVER_USE)
89 #define MPB_SECTOR_CNT 2210
90 #define IMSM_RESERVED_SECTORS 4096
91 #define NUM_BLOCKS_DIRTY_STRIPE_REGION 2056
92 #define SECT_PER_MB_SHIFT 11
94 /* Disk configuration info. */
95 #define IMSM_MAX_DEVICES 255
97 __u8 serial
[MAX_RAID_SERIAL_LEN
];/* 0xD8 - 0xE7 ascii serial number */
98 __u32 total_blocks_lo
; /* 0xE8 - 0xEB total blocks lo */
99 __u32 scsi_id
; /* 0xEC - 0xEF scsi ID */
100 #define SPARE_DISK __cpu_to_le32(0x01) /* Spare */
101 #define CONFIGURED_DISK __cpu_to_le32(0x02) /* Member of some RaidDev */
102 #define FAILED_DISK __cpu_to_le32(0x04) /* Permanent failure */
103 __u32 status
; /* 0xF0 - 0xF3 */
104 __u32 owner_cfg_num
; /* which config 0,1,2... owns this disk */
105 __u32 total_blocks_hi
; /* 0xF4 - 0xF5 total blocks hi */
106 #define IMSM_DISK_FILLERS 3
107 __u32 filler
[IMSM_DISK_FILLERS
]; /* 0xF5 - 0x107 MPB_DISK_FILLERS for future expansion */
110 /* map selector for map managment
116 /* RAID map configuration infos. */
118 __u32 pba_of_lba0_lo
; /* start address of partition */
119 __u32 blocks_per_member_lo
;/* blocks per member */
120 __u32 num_data_stripes_lo
; /* number of data stripes */
121 __u16 blocks_per_strip
;
122 __u8 map_state
; /* Normal, Uninitialized, Degraded, Failed */
123 #define IMSM_T_STATE_NORMAL 0
124 #define IMSM_T_STATE_UNINITIALIZED 1
125 #define IMSM_T_STATE_DEGRADED 2
126 #define IMSM_T_STATE_FAILED 3
128 #define IMSM_T_RAID0 0
129 #define IMSM_T_RAID1 1
130 #define IMSM_T_RAID5 5 /* since metadata version 1.2.02 ? */
131 __u8 num_members
; /* number of member disks */
132 __u8 num_domains
; /* number of parity domains */
133 __u8 failed_disk_num
; /* valid only when state is degraded */
135 __u32 pba_of_lba0_hi
;
136 __u32 blocks_per_member_hi
;
137 __u32 num_data_stripes_hi
;
138 __u32 filler
[4]; /* expansion area */
139 #define IMSM_ORD_REBUILD (1 << 24)
140 __u32 disk_ord_tbl
[1]; /* disk_ord_tbl[num_members],
141 * top byte contains some flags
143 } __attribute__ ((packed
));
146 __u32 curr_migr_unit
;
147 __u32 checkpoint_id
; /* id to access curr_migr_unit */
148 __u8 migr_state
; /* Normal or Migrating */
150 #define MIGR_REBUILD 1
151 #define MIGR_VERIFY 2 /* analagous to echo check > sync_action */
152 #define MIGR_GEN_MIGR 3
153 #define MIGR_STATE_CHANGE 4
154 #define MIGR_REPAIR 5
155 __u8 migr_type
; /* Initializing, Rebuilding, ... */
157 __u8 fs_state
; /* fast-sync state for CnG (0xff == disabled) */
158 __u16 verify_errors
; /* number of mismatches */
159 __u16 bad_blocks
; /* number of bad blocks during verify */
161 struct imsm_map map
[1];
162 /* here comes another one if migr_state */
163 } __attribute__ ((packed
));
166 __u8 volume
[MAX_RAID_SERIAL_LEN
];
169 #define DEV_BOOTABLE __cpu_to_le32(0x01)
170 #define DEV_BOOT_DEVICE __cpu_to_le32(0x02)
171 #define DEV_READ_COALESCING __cpu_to_le32(0x04)
172 #define DEV_WRITE_COALESCING __cpu_to_le32(0x08)
173 #define DEV_LAST_SHUTDOWN_DIRTY __cpu_to_le32(0x10)
174 #define DEV_HIDDEN_AT_BOOT __cpu_to_le32(0x20)
175 #define DEV_CURRENTLY_HIDDEN __cpu_to_le32(0x40)
176 #define DEV_VERIFY_AND_FIX __cpu_to_le32(0x80)
177 #define DEV_MAP_STATE_UNINIT __cpu_to_le32(0x100)
178 #define DEV_NO_AUTO_RECOVERY __cpu_to_le32(0x200)
179 #define DEV_CLONE_N_GO __cpu_to_le32(0x400)
180 #define DEV_CLONE_MAN_SYNC __cpu_to_le32(0x800)
181 #define DEV_CNG_MASTER_DISK_NUM __cpu_to_le32(0x1000)
182 __u32 status
; /* Persistent RaidDev status */
183 __u32 reserved_blocks
; /* Reserved blocks at beginning of volume */
187 __u8 cng_master_disk
;
191 #define IMSM_DEV_FILLERS 10
192 __u32 filler
[IMSM_DEV_FILLERS
];
194 } __attribute__ ((packed
));
197 __u8 sig
[MAX_SIGNATURE_LENGTH
]; /* 0x00 - 0x1F */
198 __u32 check_sum
; /* 0x20 - 0x23 MPB Checksum */
199 __u32 mpb_size
; /* 0x24 - 0x27 Size of MPB */
200 __u32 family_num
; /* 0x28 - 0x2B Checksum from first time this config was written */
201 __u32 generation_num
; /* 0x2C - 0x2F Incremented each time this array's MPB is written */
202 __u32 error_log_size
; /* 0x30 - 0x33 in bytes */
203 __u32 attributes
; /* 0x34 - 0x37 */
204 __u8 num_disks
; /* 0x38 Number of configured disks */
205 __u8 num_raid_devs
; /* 0x39 Number of configured volumes */
206 __u8 error_log_pos
; /* 0x3A */
207 __u8 fill
[1]; /* 0x3B */
208 __u32 cache_size
; /* 0x3c - 0x40 in mb */
209 __u32 orig_family_num
; /* 0x40 - 0x43 original family num */
210 __u32 pwr_cycle_count
; /* 0x44 - 0x47 simulated power cycle count for array */
211 __u32 bbm_log_size
; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */
212 #define IMSM_FILLERS 35
213 __u32 filler
[IMSM_FILLERS
]; /* 0x4C - 0xD7 RAID_MPB_FILLERS */
214 struct imsm_disk disk
[1]; /* 0xD8 diskTbl[numDisks] */
215 /* here comes imsm_dev[num_raid_devs] */
216 /* here comes BBM logs */
217 } __attribute__ ((packed
));
219 #define BBM_LOG_MAX_ENTRIES 254
221 struct bbm_log_entry
{
222 __u64 defective_block_start
;
223 #define UNREADABLE 0xFFFFFFFF
224 __u32 spare_block_offset
;
225 __u16 remapped_marked_count
;
227 } __attribute__ ((__packed__
));
230 __u32 signature
; /* 0xABADB10C */
232 __u32 reserved_spare_block_count
; /* 0 */
233 __u32 reserved
; /* 0xFFFF */
234 __u64 first_spare_lba
;
235 struct bbm_log_entry mapped_block_entries
[BBM_LOG_MAX_ENTRIES
];
236 } __attribute__ ((__packed__
));
240 static char *map_state_str
[] = { "normal", "uninitialized", "degraded", "failed" };
243 #define RAID_DISK_RESERVED_BLOCKS_IMSM_HI 2209
245 #define GEN_MIGR_AREA_SIZE 2048 /* General Migration Copy Area size in blocks */
247 #define MIGR_REC_BUF_SIZE 512 /* size of migr_record i/o buffer */
248 #define MIGR_REC_POSITION 512 /* migr_record position offset on disk,
249 * MIGR_REC_BUF_SIZE <= MIGR_REC_POSITION
253 #define UNIT_SRC_NORMAL 0 /* Source data for curr_migr_unit must
254 * be recovered using srcMap */
255 #define UNIT_SRC_IN_CP_AREA 1 /* Source data for curr_migr_unit has
256 * already been migrated and must
257 * be recovered from checkpoint area */
259 __u32 rec_status
; /* Status used to determine how to restart
260 * migration in case it aborts
262 __u32 curr_migr_unit
; /* 0..numMigrUnits-1 */
263 __u32 family_num
; /* Family number of MPB
264 * containing the RaidDev
265 * that is migrating */
266 __u32 ascending_migr
; /* True if migrating in increasing
268 __u32 blocks_per_unit
; /* Num disk blocks per unit of operation */
269 __u32 dest_depth_per_unit
; /* Num member blocks each destMap
271 * advances per unit-of-operation */
272 __u32 ckpt_area_pba
; /* Pba of first block of ckpt copy area */
273 __u32 dest_1st_member_lba
; /* First member lba on first
274 * stripe of destination */
275 __u32 num_migr_units
; /* Total num migration units-of-op */
276 __u32 post_migr_vol_cap
; /* Size of volume after
277 * migration completes */
278 __u32 post_migr_vol_cap_hi
; /* Expansion space for LBA64 */
279 __u32 ckpt_read_disk_num
; /* Which member disk in destSubMap[0] the
280 * migration ckpt record was read from
281 * (for recovered migrations) */
282 } __attribute__ ((__packed__
));
287 * 2: metadata does not match
295 struct md_list
*next
;
298 #define pr_vrb(fmt, arg...) (void) (verbose && fprintf(stderr, Name fmt, ##arg))
300 static __u8
migr_type(struct imsm_dev
*dev
)
302 if (dev
->vol
.migr_type
== MIGR_VERIFY
&&
303 dev
->status
& DEV_VERIFY_AND_FIX
)
306 return dev
->vol
.migr_type
;
309 static void set_migr_type(struct imsm_dev
*dev
, __u8 migr_type
)
311 /* for compatibility with older oroms convert MIGR_REPAIR, into
312 * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
314 if (migr_type
== MIGR_REPAIR
) {
315 dev
->vol
.migr_type
= MIGR_VERIFY
;
316 dev
->status
|= DEV_VERIFY_AND_FIX
;
318 dev
->vol
.migr_type
= migr_type
;
319 dev
->status
&= ~DEV_VERIFY_AND_FIX
;
323 static unsigned int sector_count(__u32 bytes
)
325 return ROUND_UP(bytes
, 512) / 512;
328 static unsigned int mpb_sectors(struct imsm_super
*mpb
)
330 return sector_count(__le32_to_cpu(mpb
->mpb_size
));
334 struct imsm_dev
*dev
;
335 struct intel_dev
*next
;
340 enum sys_dev_type type
;
343 struct intel_hba
*next
;
350 /* internal representation of IMSM metadata */
353 void *buf
; /* O_DIRECT buffer for reading/writing metadata */
354 struct imsm_super
*anchor
; /* immovable parameters */
357 void *migr_rec_buf
; /* buffer for I/O operations */
358 struct migr_record
*migr_rec
; /* migration record */
360 int clean_migration_record_by_mdmon
; /* when reshape is switched to next
361 array, it indicates that mdmon is allowed to clean migration
363 size_t len
; /* size of the 'buf' allocation */
364 void *next_buf
; /* for realloc'ing buf from the manager */
366 int updates_pending
; /* count of pending updates for mdmon */
367 int current_vol
; /* index of raid device undergoing creation */
368 unsigned long long create_offset
; /* common start for 'current_vol' */
369 __u32 random
; /* random data for seeding new family numbers */
370 struct intel_dev
*devlist
;
374 __u8 serial
[MAX_RAID_SERIAL_LEN
];
377 struct imsm_disk disk
;
380 struct extent
*e
; /* for determining freespace @ create */
381 int raiddisk
; /* slot to fill in autolayout */
383 } *disks
, *current_disk
;
384 struct dl
*disk_mgmt_list
; /* list of disks to add/remove while mdmon
386 struct dl
*missing
; /* disks removed while we weren't looking */
387 struct bbm_log
*bbm_log
;
388 struct intel_hba
*hba
; /* device path of the raid controller for this metadata */
389 const struct imsm_orom
*orom
; /* platform firmware support */
390 struct intel_super
*next
; /* (temp) list for disambiguating family_num */
394 struct imsm_disk disk
;
395 #define IMSM_UNKNOWN_OWNER (-1)
397 struct intel_disk
*next
;
401 unsigned long long start
, size
;
404 /* definitions of reshape process types */
405 enum imsm_reshape_type
{
411 /* definition of messages passed to imsm_process_update */
412 enum imsm_update_type
{
413 update_activate_spare
,
417 update_add_remove_disk
,
418 update_reshape_container_disks
,
419 update_reshape_migration
,
421 update_general_migration_checkpoint
,
425 struct imsm_update_activate_spare
{
426 enum imsm_update_type type
;
430 struct imsm_update_activate_spare
*next
;
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
= malloc(sizeof(*result
));
530 result
->type
= device
->type
;
531 result
->path
= strdup(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
);
564 /* Intel metadata allows for all disks attached to the same type HBA.
565 * Do not sypport odf HBA types mixing
567 if (device
->type
!= hba
->type
)
573 hba
->next
= alloc_intel_hba(device
);
577 static struct sys_dev
* find_disk_attached_hba(int fd
, const char *devname
)
579 struct sys_dev
*list
, *elem
, *prev
;
582 if ((list
= find_intel_devices()) == NULL
)
586 disk_path
= (char *) devname
;
588 disk_path
= diskfd_to_devpath(fd
);
595 for (prev
= NULL
, elem
= list
; elem
; prev
= elem
, elem
= elem
->next
) {
596 if (path_attached_to_hba(disk_path
, elem
->path
)) {
600 prev
->next
= elem
->next
;
602 if (disk_path
!= devname
)
608 if (disk_path
!= devname
)
616 static int find_intel_hba_capability(int fd
, struct intel_super
*super
,
619 static struct supertype
*match_metadata_desc_imsm(char *arg
)
621 struct supertype
*st
;
623 if (strcmp(arg
, "imsm") != 0 &&
624 strcmp(arg
, "default") != 0
628 st
= malloc(sizeof(*st
));
631 memset(st
, 0, sizeof(*st
));
632 st
->container_dev
= NoMdDev
;
633 st
->ss
= &super_imsm
;
634 st
->max_devs
= IMSM_MAX_DEVICES
;
635 st
->minor_version
= 0;
641 static __u8
*get_imsm_version(struct imsm_super
*mpb
)
643 return &mpb
->sig
[MPB_SIG_LEN
];
647 /* retrieve a disk directly from the anchor when the anchor is known to be
648 * up-to-date, currently only at load time
650 static struct imsm_disk
*__get_imsm_disk(struct imsm_super
*mpb
, __u8 index
)
652 if (index
>= mpb
->num_disks
)
654 return &mpb
->disk
[index
];
657 /* retrieve the disk description based on a index of the disk
660 static struct dl
*get_imsm_dl_disk(struct intel_super
*super
, __u8 index
)
664 for (d
= super
->disks
; d
; d
= d
->next
)
665 if (d
->index
== index
)
670 /* retrieve a disk from the parsed metadata */
671 static struct imsm_disk
*get_imsm_disk(struct intel_super
*super
, __u8 index
)
675 dl
= get_imsm_dl_disk(super
, index
);
682 /* generate a checksum directly from the anchor when the anchor is known to be
683 * up-to-date, currently only at load or write_super after coalescing
685 static __u32
__gen_imsm_checksum(struct imsm_super
*mpb
)
687 __u32 end
= mpb
->mpb_size
/ sizeof(end
);
688 __u32
*p
= (__u32
*) mpb
;
692 sum
+= __le32_to_cpu(*p
);
696 return sum
- __le32_to_cpu(mpb
->check_sum
);
699 static size_t sizeof_imsm_map(struct imsm_map
*map
)
701 return sizeof(struct imsm_map
) + sizeof(__u32
) * (map
->num_members
- 1);
704 struct imsm_map
*get_imsm_map(struct imsm_dev
*dev
, int second_map
)
706 /* A device can have 2 maps if it is in the middle of a migration.
708 * MAP_0 - we return the first map
709 * MAP_1 - we return the second map if it exists, else NULL
710 * MAP_X - we return the second map if it exists, else the first
712 struct imsm_map
*map
= &dev
->vol
.map
[0];
713 struct imsm_map
*map2
= NULL
;
715 if (dev
->vol
.migr_state
)
716 map2
= (void *)map
+ sizeof_imsm_map(map
);
718 switch (second_map
) {
735 /* return the size of the device.
736 * migr_state increases the returned size if map[0] were to be duplicated
738 static size_t sizeof_imsm_dev(struct imsm_dev
*dev
, int migr_state
)
740 size_t size
= sizeof(*dev
) - sizeof(struct imsm_map
) +
741 sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
743 /* migrating means an additional map */
744 if (dev
->vol
.migr_state
)
745 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_1
));
747 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
753 /* retrieve disk serial number list from a metadata update */
754 static struct disk_info
*get_disk_info(struct imsm_update_create_array
*update
)
757 struct disk_info
*inf
;
759 inf
= u
+ sizeof(*update
) - sizeof(struct imsm_dev
) +
760 sizeof_imsm_dev(&update
->dev
, 0);
766 static struct imsm_dev
*__get_imsm_dev(struct imsm_super
*mpb
, __u8 index
)
772 if (index
>= mpb
->num_raid_devs
)
775 /* devices start after all disks */
776 offset
= ((void *) &mpb
->disk
[mpb
->num_disks
]) - _mpb
;
778 for (i
= 0; i
<= index
; i
++)
780 return _mpb
+ offset
;
782 offset
+= sizeof_imsm_dev(_mpb
+ offset
, 0);
787 static struct imsm_dev
*get_imsm_dev(struct intel_super
*super
, __u8 index
)
789 struct intel_dev
*dv
;
791 if (index
>= super
->anchor
->num_raid_devs
)
793 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
794 if (dv
->index
== index
)
801 * == MAP_0 get first map
802 * == MAP_1 get second map
803 * == MAP_X than get map according to the current migr_state
805 static __u32
get_imsm_ord_tbl_ent(struct imsm_dev
*dev
,
809 struct imsm_map
*map
;
811 map
= get_imsm_map(dev
, second_map
);
813 /* top byte identifies disk under rebuild */
814 return __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
817 #define ord_to_idx(ord) (((ord) << 8) >> 8)
818 static __u32
get_imsm_disk_idx(struct imsm_dev
*dev
, int slot
, int second_map
)
820 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, second_map
);
822 return ord_to_idx(ord
);
825 static void set_imsm_ord_tbl_ent(struct imsm_map
*map
, int slot
, __u32 ord
)
827 map
->disk_ord_tbl
[slot
] = __cpu_to_le32(ord
);
830 static int get_imsm_disk_slot(struct imsm_map
*map
, unsigned idx
)
835 for (slot
= 0; slot
< map
->num_members
; slot
++) {
836 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
837 if (ord_to_idx(ord
) == idx
)
844 static int get_imsm_raid_level(struct imsm_map
*map
)
846 if (map
->raid_level
== 1) {
847 if (map
->num_members
== 2)
853 return map
->raid_level
;
856 static int cmp_extent(const void *av
, const void *bv
)
858 const struct extent
*a
= av
;
859 const struct extent
*b
= bv
;
860 if (a
->start
< b
->start
)
862 if (a
->start
> b
->start
)
867 static int count_memberships(struct dl
*dl
, struct intel_super
*super
)
872 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
873 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
874 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
876 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
883 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
);
885 static int split_ull(unsigned long long n
, __u32
*lo
, __u32
*hi
)
887 if (lo
== 0 || hi
== 0)
889 *lo
= __le32_to_cpu((unsigned)n
);
890 *hi
= __le32_to_cpu((unsigned)(n
>> 32));
894 static unsigned long long join_u32(__u32 lo
, __u32 hi
)
896 return (unsigned long long)__le32_to_cpu(lo
) |
897 (((unsigned long long)__le32_to_cpu(hi
)) << 32);
900 static unsigned long long total_blocks(struct imsm_disk
*disk
)
904 return join_u32(disk
->total_blocks_lo
, disk
->total_blocks_hi
);
907 static unsigned long long pba_of_lba0(struct imsm_map
*map
)
911 return join_u32(map
->pba_of_lba0_lo
, map
->pba_of_lba0_hi
);
914 static unsigned long long blocks_per_member(struct imsm_map
*map
)
918 return join_u32(map
->blocks_per_member_lo
, map
->blocks_per_member_hi
);
922 static unsigned long long num_data_stripes(struct imsm_map
*map
)
926 return join_u32(map
->num_data_stripes_lo
, map
->num_data_stripes_hi
);
929 static void set_total_blocks(struct imsm_disk
*disk
, unsigned long long n
)
931 split_ull(n
, &disk
->total_blocks_lo
, &disk
->total_blocks_hi
);
935 static void set_pba_of_lba0(struct imsm_map
*map
, unsigned long long n
)
937 split_ull(n
, &map
->pba_of_lba0_lo
, &map
->pba_of_lba0_hi
);
940 static void set_blocks_per_member(struct imsm_map
*map
, unsigned long long n
)
942 split_ull(n
, &map
->blocks_per_member_lo
, &map
->blocks_per_member_hi
);
945 static void set_num_data_stripes(struct imsm_map
*map
, unsigned long long n
)
947 split_ull(n
, &map
->num_data_stripes_lo
, &map
->num_data_stripes_hi
);
950 static struct extent
*get_extents(struct intel_super
*super
, struct dl
*dl
)
952 /* find a list of used extents on the given physical device */
953 struct extent
*rv
, *e
;
955 int memberships
= count_memberships(dl
, super
);
958 /* trim the reserved area for spares, so they can join any array
959 * regardless of whether the OROM has assigned sectors from the
960 * IMSM_RESERVED_SECTORS region
963 reservation
= imsm_min_reserved_sectors(super
);
965 reservation
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
967 rv
= malloc(sizeof(struct extent
) * (memberships
+ 1));
972 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
973 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
974 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
976 if (get_imsm_disk_slot(map
, dl
->index
) >= 0) {
977 e
->start
= pba_of_lba0(map
);
978 e
->size
= blocks_per_member(map
);
982 qsort(rv
, memberships
, sizeof(*rv
), cmp_extent
);
984 /* determine the start of the metadata
985 * when no raid devices are defined use the default
986 * ...otherwise allow the metadata to truncate the value
987 * as is the case with older versions of imsm
990 struct extent
*last
= &rv
[memberships
- 1];
991 unsigned long long remainder
;
993 remainder
= total_blocks(&dl
->disk
) - (last
->start
+ last
->size
);
994 /* round down to 1k block to satisfy precision of the kernel
998 /* make sure remainder is still sane */
999 if (remainder
< (unsigned)ROUND_UP(super
->len
, 512) >> 9)
1000 remainder
= ROUND_UP(super
->len
, 512) >> 9;
1001 if (reservation
> remainder
)
1002 reservation
= remainder
;
1004 e
->start
= total_blocks(&dl
->disk
) - reservation
;
1009 /* try to determine how much space is reserved for metadata from
1010 * the last get_extents() entry, otherwise fallback to the
1013 static __u32
imsm_reserved_sectors(struct intel_super
*super
, struct dl
*dl
)
1019 /* for spares just return a minimal reservation which will grow
1020 * once the spare is picked up by an array
1022 if (dl
->index
== -1)
1023 return MPB_SECTOR_CNT
;
1025 e
= get_extents(super
, dl
);
1027 return MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1029 /* scroll to last entry */
1030 for (i
= 0; e
[i
].size
; i
++)
1033 rv
= total_blocks(&dl
->disk
) - e
[i
].start
;
1040 static int is_spare(struct imsm_disk
*disk
)
1042 return (disk
->status
& SPARE_DISK
) == SPARE_DISK
;
1045 static int is_configured(struct imsm_disk
*disk
)
1047 return (disk
->status
& CONFIGURED_DISK
) == CONFIGURED_DISK
;
1050 static int is_failed(struct imsm_disk
*disk
)
1052 return (disk
->status
& FAILED_DISK
) == FAILED_DISK
;
1055 /* try to determine how much space is reserved for metadata from
1056 * the last get_extents() entry on the smallest active disk,
1057 * otherwise fallback to the default
1059 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
)
1063 unsigned long long min_active
;
1065 __u32 rv
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1066 struct dl
*dl
, *dl_min
= NULL
;
1072 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
1075 unsigned long long blocks
= total_blocks(&dl
->disk
);
1076 if (blocks
< min_active
|| min_active
== 0) {
1078 min_active
= blocks
;
1084 /* find last lba used by subarrays on the smallest active disk */
1085 e
= get_extents(super
, dl_min
);
1088 for (i
= 0; e
[i
].size
; i
++)
1091 remainder
= min_active
- e
[i
].start
;
1094 /* to give priority to recovery we should not require full
1095 IMSM_RESERVED_SECTORS from the spare */
1096 rv
= MPB_SECTOR_CNT
+ NUM_BLOCKS_DIRTY_STRIPE_REGION
;
1098 /* if real reservation is smaller use that value */
1099 return (remainder
< rv
) ? remainder
: rv
;
1102 /* Return minimum size of a spare that can be used in this array*/
1103 static unsigned long long min_acceptable_spare_size_imsm(struct supertype
*st
)
1105 struct intel_super
*super
= st
->sb
;
1109 unsigned long long rv
= 0;
1113 /* find first active disk in array */
1115 while (dl
&& (is_failed(&dl
->disk
) || dl
->index
== -1))
1119 /* find last lba used by subarrays */
1120 e
= get_extents(super
, dl
);
1123 for (i
= 0; e
[i
].size
; i
++)
1126 rv
= e
[i
-1].start
+ e
[i
-1].size
;
1129 /* add the amount of space needed for metadata */
1130 rv
= rv
+ imsm_min_reserved_sectors(super
);
1135 static int is_gen_migration(struct imsm_dev
*dev
);
1138 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
1139 struct imsm_dev
*dev
);
1141 static void print_imsm_dev(struct intel_super
*super
,
1142 struct imsm_dev
*dev
,
1148 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1149 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
1153 printf("[%.16s]:\n", dev
->volume
);
1154 printf(" UUID : %s\n", uuid
);
1155 printf(" RAID Level : %d", get_imsm_raid_level(map
));
1157 printf(" <-- %d", get_imsm_raid_level(map2
));
1159 printf(" Members : %d", map
->num_members
);
1161 printf(" <-- %d", map2
->num_members
);
1163 printf(" Slots : [");
1164 for (i
= 0; i
< map
->num_members
; i
++) {
1165 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
1166 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1171 for (i
= 0; i
< map2
->num_members
; i
++) {
1172 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_1
);
1173 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1178 printf(" Failed disk : ");
1179 if (map
->failed_disk_num
== 0xff)
1182 printf("%i", map
->failed_disk_num
);
1184 slot
= get_imsm_disk_slot(map
, disk_idx
);
1186 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
1187 printf(" This Slot : %d%s\n", slot
,
1188 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
1190 printf(" This Slot : ?\n");
1191 sz
= __le32_to_cpu(dev
->size_high
);
1193 sz
+= __le32_to_cpu(dev
->size_low
);
1194 printf(" Array Size : %llu%s\n", (unsigned long long)sz
,
1195 human_size(sz
* 512));
1196 sz
= blocks_per_member(map
);
1197 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz
,
1198 human_size(sz
* 512));
1199 printf(" Sector Offset : %llu\n",
1201 printf(" Num Stripes : %llu\n",
1202 num_data_stripes(map
));
1203 printf(" Chunk Size : %u KiB",
1204 __le16_to_cpu(map
->blocks_per_strip
) / 2);
1206 printf(" <-- %u KiB",
1207 __le16_to_cpu(map2
->blocks_per_strip
) / 2);
1209 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
1210 printf(" Migrate State : ");
1211 if (dev
->vol
.migr_state
) {
1212 if (migr_type(dev
) == MIGR_INIT
)
1213 printf("initialize\n");
1214 else if (migr_type(dev
) == MIGR_REBUILD
)
1215 printf("rebuild\n");
1216 else if (migr_type(dev
) == MIGR_VERIFY
)
1218 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
1219 printf("general migration\n");
1220 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
1221 printf("state change\n");
1222 else if (migr_type(dev
) == MIGR_REPAIR
)
1225 printf("<unknown:%d>\n", migr_type(dev
));
1228 printf(" Map State : %s", map_state_str
[map
->map_state
]);
1229 if (dev
->vol
.migr_state
) {
1230 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
1232 printf(" <-- %s", map_state_str
[map
->map_state
]);
1233 printf("\n Checkpoint : %u ",
1234 __le32_to_cpu(dev
->vol
.curr_migr_unit
));
1235 if ((is_gen_migration(dev
)) && ((slot
> 1) || (slot
< 0)))
1238 printf("(%llu)", (unsigned long long)
1239 blocks_per_migr_unit(super
, dev
));
1242 printf(" Dirty State : %s\n", dev
->vol
.dirty
? "dirty" : "clean");
1245 static void print_imsm_disk(struct imsm_disk
*disk
, int index
, __u32 reserved
)
1247 char str
[MAX_RAID_SERIAL_LEN
+ 1];
1250 if (index
< -1 || !disk
)
1254 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
1256 printf(" Disk%02d Serial : %s\n", index
, str
);
1258 printf(" Disk Serial : %s\n", str
);
1259 printf(" State :%s%s%s\n", is_spare(disk
) ? " spare" : "",
1260 is_configured(disk
) ? " active" : "",
1261 is_failed(disk
) ? " failed" : "");
1262 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
1263 sz
= total_blocks(disk
) - reserved
;
1264 printf(" Usable Size : %llu%s\n", (unsigned long long)sz
,
1265 human_size(sz
* 512));
1268 void examine_migr_rec_imsm(struct intel_super
*super
)
1270 struct migr_record
*migr_rec
= super
->migr_rec
;
1271 struct imsm_super
*mpb
= super
->anchor
;
1274 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1275 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1276 struct imsm_map
*map
;
1279 if (is_gen_migration(dev
) == 0)
1282 printf("\nMigration Record Information:");
1284 /* first map under migration */
1285 map
= get_imsm_map(dev
, MAP_0
);
1287 slot
= get_imsm_disk_slot(map
, super
->disks
->index
);
1288 if ((map
== NULL
) || (slot
> 1) || (slot
< 0)) {
1289 printf(" Empty\n ");
1290 printf("Examine one of first two disks in array\n");
1293 printf("\n Status : ");
1294 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
1297 printf("Contains Data\n");
1298 printf(" Current Unit : %u\n",
1299 __le32_to_cpu(migr_rec
->curr_migr_unit
));
1300 printf(" Family : %u\n",
1301 __le32_to_cpu(migr_rec
->family_num
));
1302 printf(" Ascending : %u\n",
1303 __le32_to_cpu(migr_rec
->ascending_migr
));
1304 printf(" Blocks Per Unit : %u\n",
1305 __le32_to_cpu(migr_rec
->blocks_per_unit
));
1306 printf(" Dest. Depth Per Unit : %u\n",
1307 __le32_to_cpu(migr_rec
->dest_depth_per_unit
));
1308 printf(" Checkpoint Area pba : %u\n",
1309 __le32_to_cpu(migr_rec
->ckpt_area_pba
));
1310 printf(" First member lba : %u\n",
1311 __le32_to_cpu(migr_rec
->dest_1st_member_lba
));
1312 printf(" Total Number of Units : %u\n",
1313 __le32_to_cpu(migr_rec
->num_migr_units
));
1314 printf(" Size of volume : %u\n",
1315 __le32_to_cpu(migr_rec
->post_migr_vol_cap
));
1316 printf(" Expansion space for LBA64 : %u\n",
1317 __le32_to_cpu(migr_rec
->post_migr_vol_cap_hi
));
1318 printf(" Record was read from : %u\n",
1319 __le32_to_cpu(migr_rec
->ckpt_read_disk_num
));
1324 #endif /* MDASSEMBLE */
1325 /*******************************************************************************
1326 * function: imsm_check_attributes
1327 * Description: Function checks if features represented by attributes flags
1328 * are supported by mdadm.
1330 * attributes - Attributes read from metadata
1332 * 0 - passed attributes contains unsupported features flags
1333 * 1 - all features are supported
1334 ******************************************************************************/
1335 static int imsm_check_attributes(__u32 attributes
)
1338 __u32 not_supported
= MPB_ATTRIB_SUPPORTED
^0xffffffff;
1340 not_supported
&= ~MPB_ATTRIB_IGNORED
;
1342 not_supported
&= attributes
;
1343 if (not_supported
) {
1344 fprintf(stderr
, Name
"(IMSM): Unsupported attributes : %x\n",
1345 (unsigned)__le32_to_cpu(not_supported
));
1346 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1347 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
1348 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1350 if (not_supported
& MPB_ATTRIB_2TB
) {
1351 dprintf("\t\tMPB_ATTRIB_2TB\n");
1352 not_supported
^= MPB_ATTRIB_2TB
;
1354 if (not_supported
& MPB_ATTRIB_RAID0
) {
1355 dprintf("\t\tMPB_ATTRIB_RAID0\n");
1356 not_supported
^= MPB_ATTRIB_RAID0
;
1358 if (not_supported
& MPB_ATTRIB_RAID1
) {
1359 dprintf("\t\tMPB_ATTRIB_RAID1\n");
1360 not_supported
^= MPB_ATTRIB_RAID1
;
1362 if (not_supported
& MPB_ATTRIB_RAID10
) {
1363 dprintf("\t\tMPB_ATTRIB_RAID10\n");
1364 not_supported
^= MPB_ATTRIB_RAID10
;
1366 if (not_supported
& MPB_ATTRIB_RAID1E
) {
1367 dprintf("\t\tMPB_ATTRIB_RAID1E\n");
1368 not_supported
^= MPB_ATTRIB_RAID1E
;
1370 if (not_supported
& MPB_ATTRIB_RAID5
) {
1371 dprintf("\t\tMPB_ATTRIB_RAID5\n");
1372 not_supported
^= MPB_ATTRIB_RAID5
;
1374 if (not_supported
& MPB_ATTRIB_RAIDCNG
) {
1375 dprintf("\t\tMPB_ATTRIB_RAIDCNG\n");
1376 not_supported
^= MPB_ATTRIB_RAIDCNG
;
1378 if (not_supported
& MPB_ATTRIB_BBM
) {
1379 dprintf("\t\tMPB_ATTRIB_BBM\n");
1380 not_supported
^= MPB_ATTRIB_BBM
;
1382 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1383 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n");
1384 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1386 if (not_supported
& MPB_ATTRIB_EXP_STRIPE_SIZE
) {
1387 dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n");
1388 not_supported
^= MPB_ATTRIB_EXP_STRIPE_SIZE
;
1390 if (not_supported
& MPB_ATTRIB_2TB_DISK
) {
1391 dprintf("\t\tMPB_ATTRIB_2TB_DISK\n");
1392 not_supported
^= MPB_ATTRIB_2TB_DISK
;
1394 if (not_supported
& MPB_ATTRIB_NEVER_USE2
) {
1395 dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n");
1396 not_supported
^= MPB_ATTRIB_NEVER_USE2
;
1398 if (not_supported
& MPB_ATTRIB_NEVER_USE
) {
1399 dprintf("\t\tMPB_ATTRIB_NEVER_USE\n");
1400 not_supported
^= MPB_ATTRIB_NEVER_USE
;
1404 dprintf(Name
"(IMSM): Unknown attributes : %x\n", not_supported
);
1413 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
);
1415 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
1417 struct intel_super
*super
= st
->sb
;
1418 struct imsm_super
*mpb
= super
->anchor
;
1419 char str
[MAX_SIGNATURE_LENGTH
];
1424 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
1427 snprintf(str
, MPB_SIG_LEN
, "%s", mpb
->sig
);
1428 printf(" Magic : %s\n", str
);
1429 snprintf(str
, strlen(MPB_VERSION_RAID0
), "%s", get_imsm_version(mpb
));
1430 printf(" Version : %s\n", get_imsm_version(mpb
));
1431 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
1432 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
1433 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
1434 printf(" Attributes : ");
1435 if (imsm_check_attributes(mpb
->attributes
))
1436 printf("All supported\n");
1438 printf("not supported\n");
1439 getinfo_super_imsm(st
, &info
, NULL
);
1440 fname_from_uuid(st
, &info
, nbuf
, ':');
1441 printf(" UUID : %s\n", nbuf
+ 5);
1442 sum
= __le32_to_cpu(mpb
->check_sum
);
1443 printf(" Checksum : %08x %s\n", sum
,
1444 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
1445 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
));
1446 printf(" Disks : %d\n", mpb
->num_disks
);
1447 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
1448 print_imsm_disk(__get_imsm_disk(mpb
, super
->disks
->index
), super
->disks
->index
, reserved
);
1449 if (super
->bbm_log
) {
1450 struct bbm_log
*log
= super
->bbm_log
;
1453 printf("Bad Block Management Log:\n");
1454 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
1455 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
1456 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
1457 printf(" Spare Blocks : %d\n", __le32_to_cpu(log
->reserved_spare_block_count
));
1458 printf(" First Spare : %llx\n",
1459 (unsigned long long) __le64_to_cpu(log
->first_spare_lba
));
1461 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1463 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1465 super
->current_vol
= i
;
1466 getinfo_super_imsm(st
, &info
, NULL
);
1467 fname_from_uuid(st
, &info
, nbuf
, ':');
1468 print_imsm_dev(super
, dev
, nbuf
+ 5, super
->disks
->index
);
1470 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1471 if (i
== super
->disks
->index
)
1473 print_imsm_disk(__get_imsm_disk(mpb
, i
), i
, reserved
);
1476 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
1477 if (dl
->index
== -1)
1478 print_imsm_disk(&dl
->disk
, -1, reserved
);
1480 examine_migr_rec_imsm(super
);
1483 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
1485 /* We just write a generic IMSM ARRAY entry */
1488 struct intel_super
*super
= st
->sb
;
1490 if (!super
->anchor
->num_raid_devs
) {
1491 printf("ARRAY metadata=imsm\n");
1495 getinfo_super_imsm(st
, &info
, NULL
);
1496 fname_from_uuid(st
, &info
, nbuf
, ':');
1497 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
1500 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
1502 /* We just write a generic IMSM ARRAY entry */
1506 struct intel_super
*super
= st
->sb
;
1509 if (!super
->anchor
->num_raid_devs
)
1512 getinfo_super_imsm(st
, &info
, NULL
);
1513 fname_from_uuid(st
, &info
, nbuf
, ':');
1514 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1515 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1517 super
->current_vol
= i
;
1518 getinfo_super_imsm(st
, &info
, NULL
);
1519 fname_from_uuid(st
, &info
, nbuf1
, ':');
1520 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
1521 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
1525 static void export_examine_super_imsm(struct supertype
*st
)
1527 struct intel_super
*super
= st
->sb
;
1528 struct imsm_super
*mpb
= super
->anchor
;
1532 getinfo_super_imsm(st
, &info
, NULL
);
1533 fname_from_uuid(st
, &info
, nbuf
, ':');
1534 printf("MD_METADATA=imsm\n");
1535 printf("MD_LEVEL=container\n");
1536 printf("MD_UUID=%s\n", nbuf
+5);
1537 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
1540 static void detail_super_imsm(struct supertype
*st
, char *homehost
)
1545 getinfo_super_imsm(st
, &info
, NULL
);
1546 fname_from_uuid(st
, &info
, nbuf
, ':');
1547 printf("\n UUID : %s\n", nbuf
+ 5);
1550 static void brief_detail_super_imsm(struct supertype
*st
)
1554 getinfo_super_imsm(st
, &info
, NULL
);
1555 fname_from_uuid(st
, &info
, nbuf
, ':');
1556 printf(" UUID=%s", nbuf
+ 5);
1559 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
);
1560 static void fd2devname(int fd
, char *name
);
1562 static int ahci_enumerate_ports(const char *hba_path
, int port_count
, int host_base
, int verbose
)
1564 /* dump an unsorted list of devices attached to AHCI Intel storage
1565 * controller, as well as non-connected ports
1567 int hba_len
= strlen(hba_path
) + 1;
1572 unsigned long port_mask
= (1 << port_count
) - 1;
1574 if (port_count
> (int)sizeof(port_mask
) * 8) {
1576 fprintf(stderr
, Name
": port_count %d out of range\n", port_count
);
1580 /* scroll through /sys/dev/block looking for devices attached to
1583 dir
= opendir("/sys/dev/block");
1584 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
1595 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
1597 path
= devt_to_devpath(makedev(major
, minor
));
1600 if (!path_attached_to_hba(path
, hba_path
)) {
1606 /* retrieve the scsi device type */
1607 if (asprintf(&device
, "/sys/dev/block/%d:%d/device/xxxxxxx", major
, minor
) < 0) {
1609 fprintf(stderr
, Name
": failed to allocate 'device'\n");
1613 sprintf(device
, "/sys/dev/block/%d:%d/device/type", major
, minor
);
1614 if (load_sys(device
, buf
) != 0) {
1616 fprintf(stderr
, Name
": failed to read device type for %s\n",
1622 type
= strtoul(buf
, NULL
, 10);
1624 /* if it's not a disk print the vendor and model */
1625 if (!(type
== 0 || type
== 7 || type
== 14)) {
1628 sprintf(device
, "/sys/dev/block/%d:%d/device/vendor", major
, minor
);
1629 if (load_sys(device
, buf
) == 0) {
1630 strncpy(vendor
, buf
, sizeof(vendor
));
1631 vendor
[sizeof(vendor
) - 1] = '\0';
1632 c
= (char *) &vendor
[sizeof(vendor
) - 1];
1633 while (isspace(*c
) || *c
== '\0')
1637 sprintf(device
, "/sys/dev/block/%d:%d/device/model", major
, minor
);
1638 if (load_sys(device
, buf
) == 0) {
1639 strncpy(model
, buf
, sizeof(model
));
1640 model
[sizeof(model
) - 1] = '\0';
1641 c
= (char *) &model
[sizeof(model
) - 1];
1642 while (isspace(*c
) || *c
== '\0')
1646 if (vendor
[0] && model
[0])
1647 sprintf(buf
, "%.64s %.64s", vendor
, model
);
1649 switch (type
) { /* numbers from hald/linux/device.c */
1650 case 1: sprintf(buf
, "tape"); break;
1651 case 2: sprintf(buf
, "printer"); break;
1652 case 3: sprintf(buf
, "processor"); break;
1654 case 5: sprintf(buf
, "cdrom"); break;
1655 case 6: sprintf(buf
, "scanner"); break;
1656 case 8: sprintf(buf
, "media_changer"); break;
1657 case 9: sprintf(buf
, "comm"); break;
1658 case 12: sprintf(buf
, "raid"); break;
1659 default: sprintf(buf
, "unknown");
1665 /* chop device path to 'host%d' and calculate the port number */
1666 c
= strchr(&path
[hba_len
], '/');
1669 fprintf(stderr
, Name
": %s - invalid path name\n", path
+ hba_len
);
1674 if (sscanf(&path
[hba_len
], "host%d", &port
) == 1)
1678 *c
= '/'; /* repair the full string */
1679 fprintf(stderr
, Name
": failed to determine port number for %s\n",
1686 /* mark this port as used */
1687 port_mask
&= ~(1 << port
);
1689 /* print out the device information */
1691 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
1695 fd
= dev_open(ent
->d_name
, O_RDONLY
);
1697 printf(" Port%d : - disk info unavailable -\n", port
);
1699 fd2devname(fd
, buf
);
1700 printf(" Port%d : %s", port
, buf
);
1701 if (imsm_read_serial(fd
, NULL
, (__u8
*) buf
) == 0)
1702 printf(" (%.*s)\n", MAX_RAID_SERIAL_LEN
, buf
);
1717 for (i
= 0; i
< port_count
; i
++)
1718 if (port_mask
& (1 << i
))
1719 printf(" Port%d : - no device attached -\n", i
);
1725 static void print_found_intel_controllers(struct sys_dev
*elem
)
1727 for (; elem
; elem
= elem
->next
) {
1728 fprintf(stderr
, Name
": found Intel(R) ");
1729 if (elem
->type
== SYS_DEV_SATA
)
1730 fprintf(stderr
, "SATA ");
1731 else if (elem
->type
== SYS_DEV_SAS
)
1732 fprintf(stderr
, "SAS ");
1733 fprintf(stderr
, "RAID controller");
1735 fprintf(stderr
, " at %s", elem
->pci_id
);
1736 fprintf(stderr
, ".\n");
1741 static int ahci_get_port_count(const char *hba_path
, int *port_count
)
1748 if ((dir
= opendir(hba_path
)) == NULL
)
1751 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
1754 if (sscanf(ent
->d_name
, "host%d", &host
) != 1)
1756 if (*port_count
== 0)
1758 else if (host
< host_base
)
1761 if (host
+ 1 > *port_count
+ host_base
)
1762 *port_count
= host
+ 1 - host_base
;
1768 static void print_imsm_capability(const struct imsm_orom
*orom
)
1770 printf(" Platform : Intel(R) Matrix Storage Manager\n");
1771 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
1772 orom
->hotfix_ver
, orom
->build
);
1773 printf(" RAID Levels :%s%s%s%s%s\n",
1774 imsm_orom_has_raid0(orom
) ? " raid0" : "",
1775 imsm_orom_has_raid1(orom
) ? " raid1" : "",
1776 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
1777 imsm_orom_has_raid10(orom
) ? " raid10" : "",
1778 imsm_orom_has_raid5(orom
) ? " raid5" : "");
1779 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1780 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
1781 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
1782 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
1783 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
1784 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
1785 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
1786 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
1787 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
1788 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
1789 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
1790 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
1791 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
1792 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
1793 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
1794 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
1795 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
1796 printf(" 2TB volumes :%s supported\n",
1797 (orom
->attr
& IMSM_OROM_ATTR_2TB
)?"":" not");
1798 printf(" 2TB disks :%s supported\n",
1799 (orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
)?"":" not");
1800 printf(" Max Disks : %d\n", orom
->tds
);
1801 printf(" Max Volumes : %d per array, %d per controller\n",
1802 orom
->vpa
, orom
->vphba
);
1806 static int detail_platform_imsm(int verbose
, int enumerate_only
)
1808 /* There are two components to imsm platform support, the ahci SATA
1809 * controller and the option-rom. To find the SATA controller we
1810 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
1811 * controller with the Intel vendor id is present. This approach
1812 * allows mdadm to leverage the kernel's ahci detection logic, with the
1813 * caveat that if ahci.ko is not loaded mdadm will not be able to
1814 * detect platform raid capabilities. The option-rom resides in a
1815 * platform "Adapter ROM". We scan for its signature to retrieve the
1816 * platform capabilities. If raid support is disabled in the BIOS the
1817 * option-rom capability structure will not be available.
1819 const struct imsm_orom
*orom
;
1820 struct sys_dev
*list
, *hba
;
1825 if (enumerate_only
) {
1826 if (check_env("IMSM_NO_PLATFORM"))
1828 list
= find_intel_devices();
1831 for (hba
= list
; hba
; hba
= hba
->next
) {
1832 orom
= find_imsm_capability(hba
->type
);
1838 free_sys_dev(&list
);
1842 list
= find_intel_devices();
1845 fprintf(stderr
, Name
": no active Intel(R) RAID "
1846 "controller found.\n");
1847 free_sys_dev(&list
);
1850 print_found_intel_controllers(list
);
1852 for (hba
= list
; hba
; hba
= hba
->next
) {
1853 orom
= find_imsm_capability(hba
->type
);
1855 fprintf(stderr
, Name
": imsm capabilities not found for controller: %s (type %s)\n",
1856 hba
->path
, get_sys_dev_type(hba
->type
));
1858 print_imsm_capability(orom
);
1861 for (hba
= list
; hba
; hba
= hba
->next
) {
1862 printf(" I/O Controller : %s (%s)\n",
1863 hba
->path
, get_sys_dev_type(hba
->type
));
1865 if (hba
->type
== SYS_DEV_SATA
) {
1866 host_base
= ahci_get_port_count(hba
->path
, &port_count
);
1867 if (ahci_enumerate_ports(hba
->path
, port_count
, host_base
, verbose
)) {
1869 fprintf(stderr
, Name
": failed to enumerate "
1870 "ports on SATA controller at %s.", hba
->pci_id
);
1876 free_sys_dev(&list
);
1881 static int match_home_imsm(struct supertype
*st
, char *homehost
)
1883 /* the imsm metadata format does not specify any host
1884 * identification information. We return -1 since we can never
1885 * confirm nor deny whether a given array is "meant" for this
1886 * host. We rely on compare_super and the 'family_num' fields to
1887 * exclude member disks that do not belong, and we rely on
1888 * mdadm.conf to specify the arrays that should be assembled.
1889 * Auto-assembly may still pick up "foreign" arrays.
1895 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
1897 /* The uuid returned here is used for:
1898 * uuid to put into bitmap file (Create, Grow)
1899 * uuid for backup header when saving critical section (Grow)
1900 * comparing uuids when re-adding a device into an array
1901 * In these cases the uuid required is that of the data-array,
1902 * not the device-set.
1903 * uuid to recognise same set when adding a missing device back
1904 * to an array. This is a uuid for the device-set.
1906 * For each of these we can make do with a truncated
1907 * or hashed uuid rather than the original, as long as
1909 * In each case the uuid required is that of the data-array,
1910 * not the device-set.
1912 /* imsm does not track uuid's so we synthesis one using sha1 on
1913 * - The signature (Which is constant for all imsm array, but no matter)
1914 * - the orig_family_num of the container
1915 * - the index number of the volume
1916 * - the 'serial' number of the volume.
1917 * Hopefully these are all constant.
1919 struct intel_super
*super
= st
->sb
;
1922 struct sha1_ctx ctx
;
1923 struct imsm_dev
*dev
= NULL
;
1926 /* some mdadm versions failed to set ->orig_family_num, in which
1927 * case fall back to ->family_num. orig_family_num will be
1928 * fixed up with the first metadata update.
1930 family_num
= super
->anchor
->orig_family_num
;
1931 if (family_num
== 0)
1932 family_num
= super
->anchor
->family_num
;
1933 sha1_init_ctx(&ctx
);
1934 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
1935 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
1936 if (super
->current_vol
>= 0)
1937 dev
= get_imsm_dev(super
, super
->current_vol
);
1939 __u32 vol
= super
->current_vol
;
1940 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
1941 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
1943 sha1_finish_ctx(&ctx
, buf
);
1944 memcpy(uuid
, buf
, 4*4);
1949 get_imsm_numerical_version(struct imsm_super
*mpb
, int *m
, int *p
)
1951 __u8
*v
= get_imsm_version(mpb
);
1952 __u8
*end
= mpb
->sig
+ MAX_SIGNATURE_LENGTH
;
1953 char major
[] = { 0, 0, 0 };
1954 char minor
[] = { 0 ,0, 0 };
1955 char patch
[] = { 0, 0, 0 };
1956 char *ver_parse
[] = { major
, minor
, patch
};
1960 while (*v
!= '\0' && v
< end
) {
1961 if (*v
!= '.' && j
< 2)
1962 ver_parse
[i
][j
++] = *v
;
1970 *m
= strtol(minor
, NULL
, 0);
1971 *p
= strtol(patch
, NULL
, 0);
1975 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
1977 /* migr_strip_size when repairing or initializing parity */
1978 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1979 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1981 switch (get_imsm_raid_level(map
)) {
1986 return 128*1024 >> 9;
1990 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
1992 /* migr_strip_size when rebuilding a degraded disk, no idea why
1993 * this is different than migr_strip_size_resync(), but it's good
1996 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
1997 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1999 switch (get_imsm_raid_level(map
)) {
2002 if (map
->num_members
% map
->num_domains
== 0)
2003 return 128*1024 >> 9;
2007 return max((__u32
) 64*1024 >> 9, chunk
);
2009 return 128*1024 >> 9;
2013 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
2015 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2016 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2017 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
2018 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
2020 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
2023 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
2025 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2026 int level
= get_imsm_raid_level(lo
);
2028 if (level
== 1 || level
== 10) {
2029 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2031 return hi
->num_domains
;
2033 return num_stripes_per_unit_resync(dev
);
2036 static __u8
imsm_num_data_members(struct imsm_dev
*dev
, int second_map
)
2038 /* named 'imsm_' because raid0, raid1 and raid10
2039 * counter-intuitively have the same number of data disks
2041 struct imsm_map
*map
= get_imsm_map(dev
, second_map
);
2043 switch (get_imsm_raid_level(map
)) {
2045 return map
->num_members
;
2049 return map
->num_members
/2;
2051 return map
->num_members
- 1;
2053 dprintf("%s: unsupported raid level\n", __func__
);
2058 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
2060 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2061 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2063 switch(get_imsm_raid_level(map
)) {
2066 return chunk
* map
->num_domains
;
2068 return chunk
* map
->num_members
;
2074 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
2076 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2077 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2078 __u32 strip
= block
/ chunk
;
2080 switch (get_imsm_raid_level(map
)) {
2083 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
2084 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
2086 return vol_stripe
* chunk
+ block
% chunk
;
2088 __u32 stripe
= strip
/ (map
->num_members
- 1);
2090 return stripe
* chunk
+ block
% chunk
;
2097 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
2098 struct imsm_dev
*dev
)
2100 /* calculate the conversion factor between per member 'blocks'
2101 * (md/{resync,rebuild}_start) and imsm migration units, return
2102 * 0 for the 'not migrating' and 'unsupported migration' cases
2104 if (!dev
->vol
.migr_state
)
2107 switch (migr_type(dev
)) {
2108 case MIGR_GEN_MIGR
: {
2109 struct migr_record
*migr_rec
= super
->migr_rec
;
2110 return __le32_to_cpu(migr_rec
->blocks_per_unit
);
2115 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2116 __u32 stripes_per_unit
;
2117 __u32 blocks_per_unit
;
2126 /* yes, this is really the translation of migr_units to
2127 * per-member blocks in the 'resync' case
2129 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
2130 migr_chunk
= migr_strip_blocks_resync(dev
);
2131 disks
= imsm_num_data_members(dev
, MAP_0
);
2132 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
2133 stripe
= __le16_to_cpu(map
->blocks_per_strip
) * disks
;
2134 segment
= blocks_per_unit
/ stripe
;
2135 block_rel
= blocks_per_unit
- segment
* stripe
;
2136 parity_depth
= parity_segment_depth(dev
);
2137 block_map
= map_migr_block(dev
, block_rel
);
2138 return block_map
+ parity_depth
* segment
;
2140 case MIGR_REBUILD
: {
2141 __u32 stripes_per_unit
;
2144 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
2145 migr_chunk
= migr_strip_blocks_rebuild(dev
);
2146 return migr_chunk
* stripes_per_unit
;
2148 case MIGR_STATE_CHANGE
:
2154 static int imsm_level_to_layout(int level
)
2162 return ALGORITHM_LEFT_ASYMMETRIC
;
2169 /*******************************************************************************
2170 * Function: read_imsm_migr_rec
2171 * Description: Function reads imsm migration record from last sector of disk
2173 * fd : disk descriptor
2174 * super : metadata info
2178 ******************************************************************************/
2179 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
)
2182 unsigned long long dsize
;
2184 get_dev_size(fd
, NULL
, &dsize
);
2185 if (lseek64(fd
, dsize
- MIGR_REC_POSITION
, SEEK_SET
) < 0) {
2187 Name
": Cannot seek to anchor block: %s\n",
2191 if (read(fd
, super
->migr_rec_buf
, MIGR_REC_BUF_SIZE
) !=
2192 MIGR_REC_BUF_SIZE
) {
2194 Name
": Cannot read migr record block: %s\n",
2204 static struct imsm_dev
*imsm_get_device_during_migration(
2205 struct intel_super
*super
)
2208 struct intel_dev
*dv
;
2210 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
2211 if (is_gen_migration(dv
->dev
))
2217 /*******************************************************************************
2218 * Function: load_imsm_migr_rec
2219 * Description: Function reads imsm migration record (it is stored at the last
2222 * super : imsm internal array info
2223 * info : general array info
2227 * -2 : no migration in progress
2228 ******************************************************************************/
2229 static int load_imsm_migr_rec(struct intel_super
*super
, struct mdinfo
*info
)
2232 struct dl
*dl
= NULL
;
2236 struct imsm_dev
*dev
;
2237 struct imsm_map
*map
= NULL
;
2240 /* find map under migration */
2241 dev
= imsm_get_device_during_migration(super
);
2242 /* nothing to load,no migration in progress?
2246 map
= get_imsm_map(dev
, MAP_0
);
2249 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
2250 /* skip spare and failed disks
2252 if (sd
->disk
.raid_disk
< 0)
2254 /* read only from one of the first two slots */
2256 slot
= get_imsm_disk_slot(map
,
2257 sd
->disk
.raid_disk
);
2258 if ((map
== NULL
) || (slot
> 1) || (slot
< 0))
2261 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
2262 fd
= dev_open(nm
, O_RDONLY
);
2268 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
2269 /* skip spare and failed disks
2273 /* read only from one of the first two slots */
2275 slot
= get_imsm_disk_slot(map
, dl
->index
);
2276 if ((map
== NULL
) || (slot
> 1) || (slot
< 0))
2278 sprintf(nm
, "%d:%d", dl
->major
, dl
->minor
);
2279 fd
= dev_open(nm
, O_RDONLY
);
2286 retval
= read_imsm_migr_rec(fd
, super
);
2295 /*******************************************************************************
2296 * function: imsm_create_metadata_checkpoint_update
2297 * Description: It creates update for checkpoint change.
2299 * super : imsm internal array info
2300 * u : pointer to prepared update
2303 * If length is equal to 0, input pointer u contains no update
2304 ******************************************************************************/
2305 static int imsm_create_metadata_checkpoint_update(
2306 struct intel_super
*super
,
2307 struct imsm_update_general_migration_checkpoint
**u
)
2310 int update_memory_size
= 0;
2312 dprintf("imsm_create_metadata_checkpoint_update(enter)\n");
2318 /* size of all update data without anchor */
2319 update_memory_size
=
2320 sizeof(struct imsm_update_general_migration_checkpoint
);
2322 *u
= calloc(1, update_memory_size
);
2324 dprintf("error: cannot get memory for "
2325 "imsm_create_metadata_checkpoint_update update\n");
2328 (*u
)->type
= update_general_migration_checkpoint
;
2329 (*u
)->curr_migr_unit
= __le32_to_cpu(super
->migr_rec
->curr_migr_unit
);
2330 dprintf("imsm_create_metadata_checkpoint_update: prepared for %u\n",
2331 (*u
)->curr_migr_unit
);
2333 return update_memory_size
;
2337 static void imsm_update_metadata_locally(struct supertype
*st
,
2338 void *buf
, int len
);
2340 /*******************************************************************************
2341 * Function: write_imsm_migr_rec
2342 * Description: Function writes imsm migration record
2343 * (at the last sector of disk)
2345 * super : imsm internal array info
2349 ******************************************************************************/
2350 static int write_imsm_migr_rec(struct supertype
*st
)
2352 struct intel_super
*super
= st
->sb
;
2353 unsigned long long dsize
;
2359 struct imsm_update_general_migration_checkpoint
*u
;
2360 struct imsm_dev
*dev
;
2361 struct imsm_map
*map
= NULL
;
2363 /* find map under migration */
2364 dev
= imsm_get_device_during_migration(super
);
2365 /* if no migration, write buffer anyway to clear migr_record
2366 * on disk based on first available device
2369 dev
= get_imsm_dev(super
, super
->current_vol
< 0 ? 0 :
2370 super
->current_vol
);
2372 map
= get_imsm_map(dev
, MAP_0
);
2374 for (sd
= super
->disks
; sd
; sd
= sd
->next
) {
2377 /* skip failed and spare devices */
2380 /* write to 2 first slots only */
2382 slot
= get_imsm_disk_slot(map
, sd
->index
);
2383 if ((map
== NULL
) || (slot
> 1) || (slot
< 0))
2386 sprintf(nm
, "%d:%d", sd
->major
, sd
->minor
);
2387 fd
= dev_open(nm
, O_RDWR
);
2390 get_dev_size(fd
, NULL
, &dsize
);
2391 if (lseek64(fd
, dsize
- MIGR_REC_POSITION
, SEEK_SET
) < 0) {
2393 Name
": Cannot seek to anchor block: %s\n",
2397 if (write(fd
, super
->migr_rec_buf
, MIGR_REC_BUF_SIZE
) !=
2398 MIGR_REC_BUF_SIZE
) {
2400 Name
": Cannot write migr record block: %s\n",
2407 /* update checkpoint information in metadata */
2408 len
= imsm_create_metadata_checkpoint_update(super
, &u
);
2411 dprintf("imsm: Cannot prepare update\n");
2414 /* update metadata locally */
2415 imsm_update_metadata_locally(st
, u
, len
);
2416 /* and possibly remotely */
2417 if (st
->update_tail
) {
2418 append_metadata_update(st
, u
, len
);
2419 /* during reshape we do all work inside metadata handler
2420 * manage_reshape(), so metadata update has to be triggered
2423 flush_metadata_updates(st
);
2424 st
->update_tail
= &st
->updates
;
2434 #endif /* MDASSEMBLE */
2436 /* spare/missing disks activations are not allowe when
2437 * array/container performs reshape operation, because
2438 * all arrays in container works on the same disks set
2440 int imsm_reshape_blocks_arrays_changes(struct intel_super
*super
)
2443 struct intel_dev
*i_dev
;
2444 struct imsm_dev
*dev
;
2446 /* check whole container
2448 for (i_dev
= super
->devlist
; i_dev
; i_dev
= i_dev
->next
) {
2450 if (is_gen_migration(dev
)) {
2451 /* No repair during any migration in container
2459 static unsigned long long imsm_component_size_aligment_check(int level
,
2461 unsigned long long component_size
)
2463 unsigned int component_size_alligment
;
2465 /* check component size aligment
2467 component_size_alligment
= component_size
% (chunk_size
/512);
2469 dprintf("imsm_component_size_aligment_check(Level: %i, "
2470 "chunk_size = %i, component_size = %llu), "
2471 "component_size_alligment = %u\n",
2472 level
, chunk_size
, component_size
,
2473 component_size_alligment
);
2475 if (component_size_alligment
&& (level
!= 1) && (level
!= UnSet
)) {
2476 dprintf("imsm: reported component size alligned from %llu ",
2478 component_size
-= component_size_alligment
;
2479 dprintf("to %llu (%i).\n",
2480 component_size
, component_size_alligment
);
2483 return component_size
;
2486 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
2488 struct intel_super
*super
= st
->sb
;
2489 struct migr_record
*migr_rec
= super
->migr_rec
;
2490 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
2491 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2492 struct imsm_map
*prev_map
= get_imsm_map(dev
, MAP_1
);
2493 struct imsm_map
*map_to_analyse
= map
;
2496 int map_disks
= info
->array
.raid_disks
;
2498 memset(info
, 0, sizeof(*info
));
2500 map_to_analyse
= prev_map
;
2502 dl
= super
->current_disk
;
2504 info
->container_member
= super
->current_vol
;
2505 info
->array
.raid_disks
= map
->num_members
;
2506 info
->array
.level
= get_imsm_raid_level(map_to_analyse
);
2507 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
2508 info
->array
.md_minor
= -1;
2509 info
->array
.ctime
= 0;
2510 info
->array
.utime
= 0;
2511 info
->array
.chunk_size
=
2512 __le16_to_cpu(map_to_analyse
->blocks_per_strip
) << 9;
2513 info
->array
.state
= !dev
->vol
.dirty
;
2514 info
->custom_array_size
= __le32_to_cpu(dev
->size_high
);
2515 info
->custom_array_size
<<= 32;
2516 info
->custom_array_size
|= __le32_to_cpu(dev
->size_low
);
2517 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
2519 if (is_gen_migration(dev
)) {
2520 info
->reshape_active
= 1;
2521 info
->new_level
= get_imsm_raid_level(map
);
2522 info
->new_layout
= imsm_level_to_layout(info
->new_level
);
2523 info
->new_chunk
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
2524 info
->delta_disks
= map
->num_members
- prev_map
->num_members
;
2525 if (info
->delta_disks
) {
2526 /* this needs to be applied to every array
2529 info
->reshape_active
= CONTAINER_RESHAPE
;
2531 /* We shape information that we give to md might have to be
2532 * modify to cope with md's requirement for reshaping arrays.
2533 * For example, when reshaping a RAID0, md requires it to be
2534 * presented as a degraded RAID4.
2535 * Also if a RAID0 is migrating to a RAID5 we need to specify
2536 * the array as already being RAID5, but the 'before' layout
2537 * is a RAID4-like layout.
2539 switch (info
->array
.level
) {
2541 switch(info
->new_level
) {
2543 /* conversion is happening as RAID4 */
2544 info
->array
.level
= 4;
2545 info
->array
.raid_disks
+= 1;
2548 /* conversion is happening as RAID5 */
2549 info
->array
.level
= 5;
2550 info
->array
.layout
= ALGORITHM_PARITY_N
;
2551 info
->delta_disks
-= 1;
2554 /* FIXME error message */
2555 info
->array
.level
= UnSet
;
2561 info
->new_level
= UnSet
;
2562 info
->new_layout
= UnSet
;
2563 info
->new_chunk
= info
->array
.chunk_size
;
2564 info
->delta_disks
= 0;
2568 info
->disk
.major
= dl
->major
;
2569 info
->disk
.minor
= dl
->minor
;
2570 info
->disk
.number
= dl
->index
;
2571 info
->disk
.raid_disk
= get_imsm_disk_slot(map_to_analyse
,
2575 info
->data_offset
= pba_of_lba0(map_to_analyse
);
2576 info
->component_size
= blocks_per_member(map_to_analyse
);
2578 info
->component_size
= imsm_component_size_aligment_check(
2580 info
->array
.chunk_size
,
2581 info
->component_size
);
2583 memset(info
->uuid
, 0, sizeof(info
->uuid
));
2584 info
->recovery_start
= MaxSector
;
2586 info
->reshape_progress
= 0;
2587 info
->resync_start
= MaxSector
;
2588 if ((map_to_analyse
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
2590 imsm_reshape_blocks_arrays_changes(super
) == 0) {
2591 info
->resync_start
= 0;
2593 if (dev
->vol
.migr_state
) {
2594 switch (migr_type(dev
)) {
2597 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
2599 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
2601 info
->resync_start
= blocks_per_unit
* units
;
2604 case MIGR_GEN_MIGR
: {
2605 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
2607 __u64 units
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
2608 unsigned long long array_blocks
;
2611 if (__le32_to_cpu(migr_rec
->ascending_migr
) &&
2613 (__le32_to_cpu(migr_rec
->num_migr_units
)-1)) &&
2614 (super
->migr_rec
->rec_status
==
2615 __cpu_to_le32(UNIT_SRC_IN_CP_AREA
)))
2618 info
->reshape_progress
= blocks_per_unit
* units
;
2620 dprintf("IMSM: General Migration checkpoint : %llu "
2621 "(%llu) -> read reshape progress : %llu\n",
2622 (unsigned long long)units
,
2623 (unsigned long long)blocks_per_unit
,
2624 info
->reshape_progress
);
2626 used_disks
= imsm_num_data_members(dev
, MAP_1
);
2627 if (used_disks
> 0) {
2628 array_blocks
= blocks_per_member(map
) *
2630 /* round array size down to closest MB
2632 info
->custom_array_size
= (array_blocks
2633 >> SECT_PER_MB_SHIFT
)
2634 << SECT_PER_MB_SHIFT
;
2638 /* we could emulate the checkpointing of
2639 * 'sync_action=check' migrations, but for now
2640 * we just immediately complete them
2643 /* this is handled by container_content_imsm() */
2644 case MIGR_STATE_CHANGE
:
2645 /* FIXME handle other migrations */
2647 /* we are not dirty, so... */
2648 info
->resync_start
= MaxSector
;
2652 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
2653 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
2655 info
->array
.major_version
= -1;
2656 info
->array
.minor_version
= -2;
2657 devname
= devnum2devname(st
->container_dev
);
2658 *info
->text_version
= '\0';
2660 sprintf(info
->text_version
, "/%s/%d", devname
, info
->container_member
);
2662 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
2663 uuid_from_super_imsm(st
, info
->uuid
);
2667 for (i
=0; i
<map_disks
; i
++) {
2669 if (i
< info
->array
.raid_disks
) {
2670 struct imsm_disk
*dsk
;
2671 j
= get_imsm_disk_idx(dev
, i
, MAP_X
);
2672 dsk
= get_imsm_disk(super
, j
);
2673 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
2680 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
2681 int failed
, int look_in_map
);
2683 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
2688 static void manage_second_map(struct intel_super
*super
, struct imsm_dev
*dev
)
2690 if (is_gen_migration(dev
)) {
2693 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
2695 failed
= imsm_count_failed(super
, dev
, MAP_1
);
2696 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_1
);
2697 if (map2
->map_state
!= map_state
) {
2698 map2
->map_state
= map_state
;
2699 super
->updates_pending
++;
2705 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
2709 for (d
= super
->missing
; d
; d
= d
->next
)
2710 if (d
->index
== index
)
2715 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
2717 struct intel_super
*super
= st
->sb
;
2718 struct imsm_disk
*disk
;
2719 int map_disks
= info
->array
.raid_disks
;
2720 int max_enough
= -1;
2722 struct imsm_super
*mpb
;
2724 if (super
->current_vol
>= 0) {
2725 getinfo_super_imsm_volume(st
, info
, map
);
2728 memset(info
, 0, sizeof(*info
));
2730 /* Set raid_disks to zero so that Assemble will always pull in valid
2733 info
->array
.raid_disks
= 0;
2734 info
->array
.level
= LEVEL_CONTAINER
;
2735 info
->array
.layout
= 0;
2736 info
->array
.md_minor
= -1;
2737 info
->array
.ctime
= 0; /* N/A for imsm */
2738 info
->array
.utime
= 0;
2739 info
->array
.chunk_size
= 0;
2741 info
->disk
.major
= 0;
2742 info
->disk
.minor
= 0;
2743 info
->disk
.raid_disk
= -1;
2744 info
->reshape_active
= 0;
2745 info
->array
.major_version
= -1;
2746 info
->array
.minor_version
= -2;
2747 strcpy(info
->text_version
, "imsm");
2748 info
->safe_mode_delay
= 0;
2749 info
->disk
.number
= -1;
2750 info
->disk
.state
= 0;
2752 info
->recovery_start
= MaxSector
;
2753 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
2755 /* do we have the all the insync disks that we expect? */
2756 mpb
= super
->anchor
;
2758 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
2759 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
2760 int failed
, enough
, j
, missing
= 0;
2761 struct imsm_map
*map
;
2764 failed
= imsm_count_failed(super
, dev
, MAP_0
);
2765 state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
2766 map
= get_imsm_map(dev
, MAP_0
);
2768 /* any newly missing disks?
2769 * (catches single-degraded vs double-degraded)
2771 for (j
= 0; j
< map
->num_members
; j
++) {
2772 __u32 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
2773 __u32 idx
= ord_to_idx(ord
);
2775 if (!(ord
& IMSM_ORD_REBUILD
) &&
2776 get_imsm_missing(super
, idx
)) {
2782 if (state
== IMSM_T_STATE_FAILED
)
2784 else if (state
== IMSM_T_STATE_DEGRADED
&&
2785 (state
!= map
->map_state
|| missing
))
2787 else /* we're normal, or already degraded */
2789 if (is_gen_migration(dev
) && missing
) {
2790 /* during general migration we need all disks
2791 * that process is running on.
2792 * No new missing disk is allowed.
2796 /* no more checks necessary
2800 /* in the missing/failed disk case check to see
2801 * if at least one array is runnable
2803 max_enough
= max(max_enough
, enough
);
2805 dprintf("%s: enough: %d\n", __func__
, max_enough
);
2806 info
->container_enough
= max_enough
;
2809 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
2811 disk
= &super
->disks
->disk
;
2812 info
->data_offset
= total_blocks(&super
->disks
->disk
) - reserved
;
2813 info
->component_size
= reserved
;
2814 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
2815 /* we don't change info->disk.raid_disk here because
2816 * this state will be finalized in mdmon after we have
2817 * found the 'most fresh' version of the metadata
2819 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
2820 info
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
2823 /* only call uuid_from_super_imsm when this disk is part of a populated container,
2824 * ->compare_super may have updated the 'num_raid_devs' field for spares
2826 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
2827 uuid_from_super_imsm(st
, info
->uuid
);
2829 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
2831 /* I don't know how to compute 'map' on imsm, so use safe default */
2834 for (i
= 0; i
< map_disks
; i
++)
2840 /* allocates memory and fills disk in mdinfo structure
2841 * for each disk in array */
2842 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
2844 struct mdinfo
*mddev
= NULL
;
2845 struct intel_super
*super
= st
->sb
;
2846 struct imsm_disk
*disk
;
2849 if (!super
|| !super
->disks
)
2852 mddev
= malloc(sizeof(*mddev
));
2854 fprintf(stderr
, Name
": Failed to allocate memory.\n");
2857 memset(mddev
, 0, sizeof(*mddev
));
2861 tmp
= malloc(sizeof(*tmp
));
2863 fprintf(stderr
, Name
": Failed to allocate memory.\n");
2868 memset(tmp
, 0, sizeof(*tmp
));
2870 tmp
->next
= mddev
->devs
;
2872 tmp
->disk
.number
= count
++;
2873 tmp
->disk
.major
= dl
->major
;
2874 tmp
->disk
.minor
= dl
->minor
;
2875 tmp
->disk
.state
= is_configured(disk
) ?
2876 (1 << MD_DISK_ACTIVE
) : 0;
2877 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
2878 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
2879 tmp
->disk
.raid_disk
= -1;
2885 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
2886 char *update
, char *devname
, int verbose
,
2887 int uuid_set
, char *homehost
)
2889 /* For 'assemble' and 'force' we need to return non-zero if any
2890 * change was made. For others, the return value is ignored.
2891 * Update options are:
2892 * force-one : This device looks a bit old but needs to be included,
2893 * update age info appropriately.
2894 * assemble: clear any 'faulty' flag to allow this device to
2896 * force-array: Array is degraded but being forced, mark it clean
2897 * if that will be needed to assemble it.
2899 * newdev: not used ????
2900 * grow: Array has gained a new device - this is currently for
2902 * resync: mark as dirty so a resync will happen.
2903 * name: update the name - preserving the homehost
2904 * uuid: Change the uuid of the array to match watch is given
2906 * Following are not relevant for this imsm:
2907 * sparc2.2 : update from old dodgey metadata
2908 * super-minor: change the preferred_minor number
2909 * summaries: update redundant counters.
2910 * homehost: update the recorded homehost
2911 * _reshape_progress: record new reshape_progress position.
2914 struct intel_super
*super
= st
->sb
;
2915 struct imsm_super
*mpb
;
2917 /* we can only update container info */
2918 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
2921 mpb
= super
->anchor
;
2923 if (strcmp(update
, "uuid") == 0) {
2924 /* We take this to mean that the family_num should be updated.
2925 * However that is much smaller than the uuid so we cannot really
2926 * allow an explicit uuid to be given. And it is hard to reliably
2928 * So if !uuid_set we know the current uuid is random and just used
2929 * the first 'int' and copy it to the other 3 positions.
2930 * Otherwise we require the 4 'int's to be the same as would be the
2931 * case if we are using a random uuid. So an explicit uuid will be
2932 * accepted as long as all for ints are the same... which shouldn't hurt
2935 info
->uuid
[1] = info
->uuid
[2] = info
->uuid
[3] = info
->uuid
[0];
2938 if (info
->uuid
[0] != info
->uuid
[1] ||
2939 info
->uuid
[1] != info
->uuid
[2] ||
2940 info
->uuid
[2] != info
->uuid
[3])
2946 mpb
->orig_family_num
= info
->uuid
[0];
2947 } else if (strcmp(update
, "assemble") == 0)
2952 /* successful update? recompute checksum */
2954 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
2959 static size_t disks_to_mpb_size(int disks
)
2963 size
= sizeof(struct imsm_super
);
2964 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
2965 size
+= 2 * sizeof(struct imsm_dev
);
2966 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
2967 size
+= (4 - 2) * sizeof(struct imsm_map
);
2968 /* 4 possible disk_ord_tbl's */
2969 size
+= 4 * (disks
- 1) * sizeof(__u32
);
2974 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
)
2976 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
2979 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
2982 static void free_devlist(struct intel_super
*super
)
2984 struct intel_dev
*dv
;
2986 while (super
->devlist
) {
2987 dv
= super
->devlist
->next
;
2988 free(super
->devlist
->dev
);
2989 free(super
->devlist
);
2990 super
->devlist
= dv
;
2994 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
2996 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
2999 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
)
3003 * 0 same, or first was empty, and second was copied
3004 * 1 second had wrong number
3006 * 3 wrong other info
3008 struct intel_super
*first
= st
->sb
;
3009 struct intel_super
*sec
= tst
->sb
;
3016 /* in platform dependent environment test if the disks
3017 * use the same Intel hba
3019 if (!check_env("IMSM_NO_PLATFORM")) {
3020 if (!first
->hba
|| !sec
->hba
||
3021 (first
->hba
->type
!= sec
->hba
->type
)) {
3023 "HBAs of devices does not match %s != %s\n",
3024 first
->hba
? get_sys_dev_type(first
->hba
->type
) : NULL
,
3025 sec
->hba
? get_sys_dev_type(sec
->hba
->type
) : NULL
);
3030 /* if an anchor does not have num_raid_devs set then it is a free
3033 if (first
->anchor
->num_raid_devs
> 0 &&
3034 sec
->anchor
->num_raid_devs
> 0) {
3035 /* Determine if these disks might ever have been
3036 * related. Further disambiguation can only take place
3037 * in load_super_imsm_all
3039 __u32 first_family
= first
->anchor
->orig_family_num
;
3040 __u32 sec_family
= sec
->anchor
->orig_family_num
;
3042 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
3043 MAX_SIGNATURE_LENGTH
) != 0)
3046 if (first_family
== 0)
3047 first_family
= first
->anchor
->family_num
;
3048 if (sec_family
== 0)
3049 sec_family
= sec
->anchor
->family_num
;
3051 if (first_family
!= sec_family
)
3057 /* if 'first' is a spare promote it to a populated mpb with sec's
3060 if (first
->anchor
->num_raid_devs
== 0 &&
3061 sec
->anchor
->num_raid_devs
> 0) {
3063 struct intel_dev
*dv
;
3064 struct imsm_dev
*dev
;
3066 /* we need to copy raid device info from sec if an allocation
3067 * fails here we don't associate the spare
3069 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++) {
3070 dv
= malloc(sizeof(*dv
));
3073 dev
= malloc(sizeof_imsm_dev(get_imsm_dev(sec
, i
), 1));
3080 dv
->next
= first
->devlist
;
3081 first
->devlist
= dv
;
3083 if (i
< sec
->anchor
->num_raid_devs
) {
3084 /* allocation failure */
3085 free_devlist(first
);
3086 fprintf(stderr
, "imsm: failed to associate spare\n");
3089 first
->anchor
->num_raid_devs
= sec
->anchor
->num_raid_devs
;
3090 first
->anchor
->orig_family_num
= sec
->anchor
->orig_family_num
;
3091 first
->anchor
->family_num
= sec
->anchor
->family_num
;
3092 memcpy(first
->anchor
->sig
, sec
->anchor
->sig
, MAX_SIGNATURE_LENGTH
);
3093 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++)
3094 imsm_copy_dev(get_imsm_dev(first
, i
), get_imsm_dev(sec
, i
));
3100 static void fd2devname(int fd
, char *name
)
3104 char dname
[PATH_MAX
];
3109 if (fstat(fd
, &st
) != 0)
3111 sprintf(path
, "/sys/dev/block/%d:%d",
3112 major(st
.st_rdev
), minor(st
.st_rdev
));
3114 rv
= readlink(path
, dname
, sizeof(dname
)-1);
3119 nm
= strrchr(dname
, '/');
3122 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
3126 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
3128 static int imsm_read_serial(int fd
, char *devname
,
3129 __u8 serial
[MAX_RAID_SERIAL_LEN
])
3131 unsigned char scsi_serial
[255];
3140 memset(scsi_serial
, 0, sizeof(scsi_serial
));
3142 rv
= scsi_get_serial(fd
, scsi_serial
, sizeof(scsi_serial
));
3144 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
3145 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3146 fd2devname(fd
, (char *) serial
);
3153 Name
": Failed to retrieve serial for %s\n",
3158 rsp_len
= scsi_serial
[3];
3162 Name
": Failed to retrieve serial for %s\n",
3166 rsp_buf
= (char *) &scsi_serial
[4];
3168 /* trim all whitespace and non-printable characters and convert
3171 for (i
= 0, dest
= rsp_buf
; i
< rsp_len
; i
++) {
3174 /* ':' is reserved for use in placeholder serial
3175 * numbers for missing disks
3183 len
= dest
- rsp_buf
;
3186 /* truncate leading characters */
3187 if (len
> MAX_RAID_SERIAL_LEN
) {
3188 dest
+= len
- MAX_RAID_SERIAL_LEN
;
3189 len
= MAX_RAID_SERIAL_LEN
;
3192 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3193 memcpy(serial
, dest
, len
);
3198 static int serialcmp(__u8
*s1
, __u8
*s2
)
3200 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
3203 static void serialcpy(__u8
*dest
, __u8
*src
)
3205 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
3208 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
3212 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3213 if (serialcmp(dl
->serial
, serial
) == 0)
3219 static struct imsm_disk
*
3220 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
3224 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3225 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3227 if (serialcmp(disk
->serial
, serial
) == 0) {
3238 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3240 struct imsm_disk
*disk
;
3245 __u8 serial
[MAX_RAID_SERIAL_LEN
];
3247 rv
= imsm_read_serial(fd
, devname
, serial
);
3252 dl
= calloc(1, sizeof(*dl
));
3256 Name
": failed to allocate disk buffer for %s\n",
3262 dl
->major
= major(stb
.st_rdev
);
3263 dl
->minor
= minor(stb
.st_rdev
);
3264 dl
->next
= super
->disks
;
3265 dl
->fd
= keep_fd
? fd
: -1;
3266 assert(super
->disks
== NULL
);
3268 serialcpy(dl
->serial
, serial
);
3271 fd2devname(fd
, name
);
3273 dl
->devname
= strdup(devname
);
3275 dl
->devname
= strdup(name
);
3277 /* look up this disk's index in the current anchor */
3278 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
3281 /* only set index on disks that are a member of a
3282 * populated contianer, i.e. one with raid_devs
3284 if (is_failed(&dl
->disk
))
3286 else if (is_spare(&dl
->disk
))
3294 /* When migrating map0 contains the 'destination' state while map1
3295 * contains the current state. When not migrating map0 contains the
3296 * current state. This routine assumes that map[0].map_state is set to
3297 * the current array state before being called.
3299 * Migration is indicated by one of the following states
3300 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
3301 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
3302 * map1state=unitialized)
3303 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
3305 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
3306 * map1state=degraded)
3307 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
3310 static void migrate(struct imsm_dev
*dev
, struct intel_super
*super
,
3311 __u8 to_state
, int migr_type
)
3313 struct imsm_map
*dest
;
3314 struct imsm_map
*src
= get_imsm_map(dev
, MAP_0
);
3316 dev
->vol
.migr_state
= 1;
3317 set_migr_type(dev
, migr_type
);
3318 dev
->vol
.curr_migr_unit
= 0;
3319 dest
= get_imsm_map(dev
, MAP_1
);
3321 /* duplicate and then set the target end state in map[0] */
3322 memcpy(dest
, src
, sizeof_imsm_map(src
));
3323 if ((migr_type
== MIGR_REBUILD
) ||
3324 (migr_type
== MIGR_GEN_MIGR
)) {
3328 for (i
= 0; i
< src
->num_members
; i
++) {
3329 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
3330 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
3334 if (migr_type
== MIGR_GEN_MIGR
)
3335 /* Clear migration record */
3336 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
3338 src
->map_state
= to_state
;
3341 static void end_migration(struct imsm_dev
*dev
, struct intel_super
*super
,
3344 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3345 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
== 0 ?
3349 /* merge any IMSM_ORD_REBUILD bits that were not successfully
3350 * completed in the last migration.
3352 * FIXME add support for raid-level-migration
3354 if ((map_state
!= map
->map_state
) && (is_gen_migration(dev
) == 0) &&
3355 (prev
->map_state
!= IMSM_T_STATE_UNINITIALIZED
)) {
3356 /* when final map state is other than expected
3357 * merge maps (not for migration)
3361 for (i
= 0; i
< prev
->num_members
; i
++)
3362 for (j
= 0; j
< map
->num_members
; j
++)
3363 /* during online capacity expansion
3364 * disks position can be changed
3365 * if takeover is used
3367 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
3368 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
3369 map
->disk_ord_tbl
[j
] |=
3370 prev
->disk_ord_tbl
[i
];
3373 failed
= imsm_count_failed(super
, dev
, MAP_0
);
3374 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
3377 dev
->vol
.migr_state
= 0;
3378 set_migr_type(dev
, 0);
3379 dev
->vol
.curr_migr_unit
= 0;
3380 map
->map_state
= map_state
;
3384 static int parse_raid_devices(struct intel_super
*super
)
3387 struct imsm_dev
*dev_new
;
3388 size_t len
, len_migr
;
3390 size_t space_needed
= 0;
3391 struct imsm_super
*mpb
= super
->anchor
;
3393 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3394 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3395 struct intel_dev
*dv
;
3397 len
= sizeof_imsm_dev(dev_iter
, 0);
3398 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
3400 space_needed
+= len_migr
- len
;
3402 dv
= malloc(sizeof(*dv
));
3405 if (max_len
< len_migr
)
3407 if (max_len
> len_migr
)
3408 space_needed
+= max_len
- len_migr
;
3409 dev_new
= malloc(max_len
);
3414 imsm_copy_dev(dev_new
, dev_iter
);
3417 dv
->next
= super
->devlist
;
3418 super
->devlist
= dv
;
3421 /* ensure that super->buf is large enough when all raid devices
3424 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
3427 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
, 512);
3428 if (posix_memalign(&buf
, 512, len
) != 0)
3431 memcpy(buf
, super
->buf
, super
->len
);
3432 memset(buf
+ super
->len
, 0, len
- super
->len
);
3441 /* retrieve a pointer to the bbm log which starts after all raid devices */
3442 struct bbm_log
*__get_imsm_bbm_log(struct imsm_super
*mpb
)
3446 if (__le32_to_cpu(mpb
->bbm_log_size
)) {
3448 ptr
+= mpb
->mpb_size
- __le32_to_cpu(mpb
->bbm_log_size
);
3454 /*******************************************************************************
3455 * Function: check_mpb_migr_compatibility
3456 * Description: Function checks for unsupported migration features:
3457 * - migration optimization area (pba_of_lba0)
3458 * - descending reshape (ascending_migr)
3460 * super : imsm metadata information
3462 * 0 : migration is compatible
3463 * -1 : migration is not compatible
3464 ******************************************************************************/
3465 int check_mpb_migr_compatibility(struct intel_super
*super
)
3467 struct imsm_map
*map0
, *map1
;
3468 struct migr_record
*migr_rec
= super
->migr_rec
;
3471 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3472 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3475 dev_iter
->vol
.migr_state
== 1 &&
3476 dev_iter
->vol
.migr_type
== MIGR_GEN_MIGR
) {
3477 /* This device is migrating */
3478 map0
= get_imsm_map(dev_iter
, MAP_0
);
3479 map1
= get_imsm_map(dev_iter
, MAP_1
);
3480 if (pba_of_lba0(map0
) != pba_of_lba0(map1
))
3481 /* migration optimization area was used */
3483 if (migr_rec
->ascending_migr
== 0
3484 && migr_rec
->dest_depth_per_unit
> 0)
3485 /* descending reshape not supported yet */
3492 static void __free_imsm(struct intel_super
*super
, int free_disks
);
3494 /* load_imsm_mpb - read matrix metadata
3495 * allocates super->mpb to be freed by free_imsm
3497 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
3499 unsigned long long dsize
;
3500 unsigned long long sectors
;
3502 struct imsm_super
*anchor
;
3505 get_dev_size(fd
, NULL
, &dsize
);
3509 Name
": %s: device to small for imsm\n",
3514 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0) {
3516 fprintf(stderr
, Name
3517 ": Cannot seek to anchor block on %s: %s\n",
3518 devname
, strerror(errno
));
3522 if (posix_memalign((void**)&anchor
, 512, 512) != 0) {
3525 Name
": Failed to allocate imsm anchor buffer"
3526 " on %s\n", devname
);
3529 if (read(fd
, anchor
, 512) != 512) {
3532 Name
": Cannot read anchor block on %s: %s\n",
3533 devname
, strerror(errno
));
3538 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
3541 Name
": no IMSM anchor on %s\n", devname
);
3546 __free_imsm(super
, 0);
3547 /* reload capability and hba */
3549 /* capability and hba must be updated with new super allocation */
3550 find_intel_hba_capability(fd
, super
, devname
);
3551 super
->len
= ROUND_UP(anchor
->mpb_size
, 512);
3552 if (posix_memalign(&super
->buf
, 512, super
->len
) != 0) {
3555 Name
": unable to allocate %zu byte mpb buffer\n",
3560 memcpy(super
->buf
, anchor
, 512);
3562 sectors
= mpb_sectors(anchor
) - 1;
3565 if (posix_memalign(&super
->migr_rec_buf
, 512, MIGR_REC_BUF_SIZE
) != 0) {
3566 fprintf(stderr
, Name
3567 ": %s could not allocate migr_rec buffer\n", __func__
);
3571 super
->clean_migration_record_by_mdmon
= 0;
3574 check_sum
= __gen_imsm_checksum(super
->anchor
);
3575 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
3578 Name
": IMSM checksum %x != %x on %s\n",
3580 __le32_to_cpu(super
->anchor
->check_sum
),
3588 /* read the extended mpb */
3589 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0) {
3592 Name
": 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) {
3600 Name
": Cannot read extended mpb on %s: %s\n",
3601 devname
, strerror(errno
));
3605 check_sum
= __gen_imsm_checksum(super
->anchor
);
3606 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
3609 Name
": IMSM checksum %x != %x on %s\n",
3610 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
3615 /* FIXME the BBM log is disk specific so we cannot use this global
3616 * buffer for all disks. Ok for now since we only look at the global
3617 * bbm_log_size parameter to gate assembly
3619 super
->bbm_log
= __get_imsm_bbm_log(super
->anchor
);
3624 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
);
3626 /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
3627 static void clear_hi(struct intel_super
*super
)
3629 struct imsm_super
*mpb
= super
->anchor
;
3631 if (mpb
->attributes
& MPB_ATTRIB_2TB_DISK
)
3633 for (i
= 0; i
< mpb
->num_disks
; ++i
) {
3634 struct imsm_disk
*disk
= &mpb
->disk
[i
];
3635 disk
->total_blocks_hi
= 0;
3637 for (i
= 0; i
< mpb
->num_raid_devs
; ++i
) {
3638 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3641 for (n
= 0; n
< 2; ++n
) {
3642 struct imsm_map
*map
= get_imsm_map(dev
, n
);
3645 map
->pba_of_lba0_hi
= 0;
3646 map
->blocks_per_member_hi
= 0;
3647 map
->num_data_stripes_hi
= 0;
3653 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3657 err
= load_imsm_mpb(fd
, super
, devname
);
3660 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
3663 err
= parse_raid_devices(super
);
3668 static void __free_imsm_disk(struct dl
*d
)
3680 static void free_imsm_disks(struct intel_super
*super
)
3684 while (super
->disks
) {
3686 super
->disks
= d
->next
;
3687 __free_imsm_disk(d
);
3689 while (super
->disk_mgmt_list
) {
3690 d
= super
->disk_mgmt_list
;
3691 super
->disk_mgmt_list
= d
->next
;
3692 __free_imsm_disk(d
);
3694 while (super
->missing
) {
3696 super
->missing
= d
->next
;
3697 __free_imsm_disk(d
);
3702 /* free all the pieces hanging off of a super pointer */
3703 static void __free_imsm(struct intel_super
*super
, int free_disks
)
3705 struct intel_hba
*elem
, *next
;
3711 /* unlink capability description */
3713 if (super
->migr_rec_buf
) {
3714 free(super
->migr_rec_buf
);
3715 super
->migr_rec_buf
= NULL
;
3718 free_imsm_disks(super
);
3719 free_devlist(super
);
3723 free((void *)elem
->path
);
3731 static void free_imsm(struct intel_super
*super
)
3733 __free_imsm(super
, 1);
3737 static void free_super_imsm(struct supertype
*st
)
3739 struct intel_super
*super
= st
->sb
;
3748 static struct intel_super
*alloc_super(void)
3750 struct intel_super
*super
= malloc(sizeof(*super
));
3753 memset(super
, 0, sizeof(*super
));
3754 super
->current_vol
= -1;
3755 super
->create_offset
= ~((unsigned long long) 0);
3761 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
3763 static int find_intel_hba_capability(int fd
, struct intel_super
*super
, char *devname
)
3765 struct sys_dev
*hba_name
;
3768 if ((fd
< 0) || check_env("IMSM_NO_PLATFORM")) {
3773 hba_name
= find_disk_attached_hba(fd
, NULL
);
3777 Name
": %s is not attached to Intel(R) RAID controller.\n",
3781 rv
= attach_hba_to_super(super
, hba_name
);
3784 struct intel_hba
*hba
= super
->hba
;
3786 fprintf(stderr
, Name
": %s is attached to Intel(R) %s RAID "
3787 "controller (%s),\n"
3788 " but the container is assigned to Intel(R) "
3789 "%s RAID controller (",
3792 hba_name
->pci_id
? : "Err!",
3793 get_sys_dev_type(hba_name
->type
));
3796 fprintf(stderr
, "%s", hba
->pci_id
? : "Err!");
3798 fprintf(stderr
, ", ");
3802 fprintf(stderr
, ").\n"
3803 " Mixing devices attached to different controllers "
3804 "is not allowed.\n");
3806 free_sys_dev(&hba_name
);
3809 super
->orom
= find_imsm_capability(hba_name
->type
);
3810 free_sys_dev(&hba_name
);
3816 /* find_missing - helper routine for load_super_imsm_all that identifies
3817 * disks that have disappeared from the system. This routine relies on
3818 * the mpb being uptodate, which it is at load time.
3820 static int find_missing(struct intel_super
*super
)
3823 struct imsm_super
*mpb
= super
->anchor
;
3825 struct imsm_disk
*disk
;
3827 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3828 disk
= __get_imsm_disk(mpb
, i
);
3829 dl
= serial_to_dl(disk
->serial
, super
);
3833 dl
= malloc(sizeof(*dl
));
3839 dl
->devname
= strdup("missing");
3841 serialcpy(dl
->serial
, disk
->serial
);
3844 dl
->next
= super
->missing
;
3845 super
->missing
= dl
;
3852 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
3854 struct intel_disk
*idisk
= disk_list
;
3857 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
3859 idisk
= idisk
->next
;
3865 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
3866 struct intel_super
*super
,
3867 struct intel_disk
**disk_list
)
3869 struct imsm_disk
*d
= &super
->disks
->disk
;
3870 struct imsm_super
*mpb
= super
->anchor
;
3873 for (i
= 0; i
< tbl_size
; i
++) {
3874 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
3875 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
3877 if (tbl_mpb
->family_num
== mpb
->family_num
) {
3878 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
3879 dprintf("%s: mpb from %d:%d matches %d:%d\n",
3880 __func__
, super
->disks
->major
,
3881 super
->disks
->minor
,
3882 table
[i
]->disks
->major
,
3883 table
[i
]->disks
->minor
);
3887 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
3888 is_configured(d
) == is_configured(tbl_d
)) &&
3889 tbl_mpb
->generation_num
< mpb
->generation_num
) {
3890 /* current version of the mpb is a
3891 * better candidate than the one in
3892 * super_table, but copy over "cross
3893 * generational" status
3895 struct intel_disk
*idisk
;
3897 dprintf("%s: mpb from %d:%d replaces %d:%d\n",
3898 __func__
, super
->disks
->major
,
3899 super
->disks
->minor
,
3900 table
[i
]->disks
->major
,
3901 table
[i
]->disks
->minor
);
3903 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
3904 if (idisk
&& is_failed(&idisk
->disk
))
3905 tbl_d
->status
|= FAILED_DISK
;
3908 struct intel_disk
*idisk
;
3909 struct imsm_disk
*disk
;
3911 /* tbl_mpb is more up to date, but copy
3912 * over cross generational status before
3915 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
3916 if (disk
&& is_failed(disk
))
3917 d
->status
|= FAILED_DISK
;
3919 idisk
= disk_list_get(d
->serial
, *disk_list
);
3922 if (disk
&& is_configured(disk
))
3923 idisk
->disk
.status
|= CONFIGURED_DISK
;
3926 dprintf("%s: mpb from %d:%d prefer %d:%d\n",
3927 __func__
, super
->disks
->major
,
3928 super
->disks
->minor
,
3929 table
[i
]->disks
->major
,
3930 table
[i
]->disks
->minor
);
3938 table
[tbl_size
++] = super
;
3942 /* update/extend the merged list of imsm_disk records */
3943 for (j
= 0; j
< mpb
->num_disks
; j
++) {
3944 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
3945 struct intel_disk
*idisk
;
3947 idisk
= disk_list_get(disk
->serial
, *disk_list
);
3949 idisk
->disk
.status
|= disk
->status
;
3950 if (is_configured(&idisk
->disk
) ||
3951 is_failed(&idisk
->disk
))
3952 idisk
->disk
.status
&= ~(SPARE_DISK
);
3954 idisk
= calloc(1, sizeof(*idisk
));
3957 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
3958 idisk
->disk
= *disk
;
3959 idisk
->next
= *disk_list
;
3963 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
3970 static struct intel_super
*
3971 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
3974 struct imsm_super
*mpb
= super
->anchor
;
3978 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3979 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3980 struct intel_disk
*idisk
;
3982 idisk
= disk_list_get(disk
->serial
, disk_list
);
3984 if (idisk
->owner
== owner
||
3985 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
3988 dprintf("%s: '%.16s' owner %d != %d\n",
3989 __func__
, disk
->serial
, idisk
->owner
,
3992 dprintf("%s: unknown disk %x [%d]: %.16s\n",
3993 __func__
, __le32_to_cpu(mpb
->family_num
), i
,
3999 if (ok_count
== mpb
->num_disks
)
4004 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
4006 struct intel_super
*s
;
4008 for (s
= super_list
; s
; s
= s
->next
) {
4009 if (family_num
!= s
->anchor
->family_num
)
4011 fprintf(stderr
, "Conflict, offlining family %#x on '%s'\n",
4012 __le32_to_cpu(family_num
), s
->disks
->devname
);
4016 static struct intel_super
*
4017 imsm_thunderdome(struct intel_super
**super_list
, int len
)
4019 struct intel_super
*super_table
[len
];
4020 struct intel_disk
*disk_list
= NULL
;
4021 struct intel_super
*champion
, *spare
;
4022 struct intel_super
*s
, **del
;
4027 memset(super_table
, 0, sizeof(super_table
));
4028 for (s
= *super_list
; s
; s
= s
->next
)
4029 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
4031 for (i
= 0; i
< tbl_size
; i
++) {
4032 struct imsm_disk
*d
;
4033 struct intel_disk
*idisk
;
4034 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
4037 d
= &s
->disks
->disk
;
4039 /* 'd' must appear in merged disk list for its
4040 * configuration to be valid
4042 idisk
= disk_list_get(d
->serial
, disk_list
);
4043 if (idisk
&& idisk
->owner
== i
)
4044 s
= validate_members(s
, disk_list
, i
);
4049 dprintf("%s: marking family: %#x from %d:%d offline\n",
4050 __func__
, mpb
->family_num
,
4051 super_table
[i
]->disks
->major
,
4052 super_table
[i
]->disks
->minor
);
4056 /* This is where the mdadm implementation differs from the Windows
4057 * driver which has no strict concept of a container. We can only
4058 * assemble one family from a container, so when returning a prodigal
4059 * array member to this system the code will not be able to disambiguate
4060 * the container contents that should be assembled ("foreign" versus
4061 * "local"). It requires user intervention to set the orig_family_num
4062 * to a new value to establish a new container. The Windows driver in
4063 * this situation fixes up the volume name in place and manages the
4064 * foreign array as an independent entity.
4069 for (i
= 0; i
< tbl_size
; i
++) {
4070 struct intel_super
*tbl_ent
= super_table
[i
];
4076 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
4081 if (s
&& !is_spare
) {
4082 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
4084 } else if (!s
&& !is_spare
)
4097 fprintf(stderr
, "Chose family %#x on '%s', "
4098 "assemble conflicts to new container with '--update=uuid'\n",
4099 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
4101 /* collect all dl's onto 'champion', and update them to
4102 * champion's version of the status
4104 for (s
= *super_list
; s
; s
= s
->next
) {
4105 struct imsm_super
*mpb
= champion
->anchor
;
4106 struct dl
*dl
= s
->disks
;
4111 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4112 struct imsm_disk
*disk
;
4114 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
4117 /* only set index on disks that are a member of
4118 * a populated contianer, i.e. one with
4121 if (is_failed(&dl
->disk
))
4123 else if (is_spare(&dl
->disk
))
4129 if (i
>= mpb
->num_disks
) {
4130 struct intel_disk
*idisk
;
4132 idisk
= disk_list_get(dl
->serial
, disk_list
);
4133 if (idisk
&& is_spare(&idisk
->disk
) &&
4134 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
4142 dl
->next
= champion
->disks
;
4143 champion
->disks
= dl
;
4147 /* delete 'champion' from super_list */
4148 for (del
= super_list
; *del
; ) {
4149 if (*del
== champion
) {
4150 *del
= (*del
)->next
;
4153 del
= &(*del
)->next
;
4155 champion
->next
= NULL
;
4159 struct intel_disk
*idisk
= disk_list
;
4161 disk_list
= disk_list
->next
;
4170 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
);
4171 static int get_super_block(struct intel_super
**super_list
, int devnum
, char *devname
,
4172 int major
, int minor
, int keep_fd
);
4174 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4175 int *max
, int keep_fd
);
4178 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
4179 char *devname
, struct md_list
*devlist
,
4182 struct intel_super
*super_list
= NULL
;
4183 struct intel_super
*super
= NULL
;
4188 /* 'fd' is an opened container */
4189 err
= get_sra_super_block(fd
, &super_list
, devname
, &i
, keep_fd
);
4191 /* get super block from devlist devices */
4192 err
= get_devlist_super_block(devlist
, &super_list
, &i
, keep_fd
);
4195 /* all mpbs enter, maybe one leaves */
4196 super
= imsm_thunderdome(&super_list
, i
);
4202 if (find_missing(super
) != 0) {
4208 /* load migration record */
4209 err
= load_imsm_migr_rec(super
, NULL
);
4211 /* migration is in progress,
4212 * but migr_rec cannot be loaded,
4218 /* Check migration compatibility */
4219 if ((err
== 0) && (check_mpb_migr_compatibility(super
) != 0)) {
4220 fprintf(stderr
, Name
": Unsupported migration detected");
4222 fprintf(stderr
, " on %s\n", devname
);
4224 fprintf(stderr
, " (IMSM).\n");
4233 while (super_list
) {
4234 struct intel_super
*s
= super_list
;
4236 super_list
= super_list
->next
;
4246 st
->container_dev
= fd2devnum(fd
);
4248 st
->container_dev
= NoMdDev
;
4249 if (err
== 0 && st
->ss
== NULL
) {
4250 st
->ss
= &super_imsm
;
4251 st
->minor_version
= 0;
4252 st
->max_devs
= IMSM_MAX_DEVICES
;
4259 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4260 int *max
, int keep_fd
)
4262 struct md_list
*tmpdev
;
4266 for (i
= 0, tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
4267 if (tmpdev
->used
!= 1)
4269 if (tmpdev
->container
== 1) {
4271 int fd
= dev_open(tmpdev
->devname
, O_RDONLY
|O_EXCL
);
4273 fprintf(stderr
, Name
": cannot open device %s: %s\n",
4274 tmpdev
->devname
, strerror(errno
));
4278 err
= get_sra_super_block(fd
, super_list
,
4279 tmpdev
->devname
, &lmax
,
4288 int major
= major(tmpdev
->st_rdev
);
4289 int minor
= minor(tmpdev
->st_rdev
);
4290 err
= get_super_block(super_list
,
4307 static int get_super_block(struct intel_super
**super_list
, int devnum
, char *devname
,
4308 int major
, int minor
, int keep_fd
)
4310 struct intel_super
*s
= NULL
;
4323 sprintf(nm
, "%d:%d", major
, minor
);
4324 dfd
= dev_open(nm
, O_RDWR
);
4330 rv
= find_intel_hba_capability(dfd
, s
, devname
);
4331 /* no orom/efi or non-intel hba of the disk */
4337 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4339 /* retry the load if we might have raced against mdmon */
4340 if (err
== 3 && (devnum
!= -1) && mdmon_running(devnum
))
4341 for (retry
= 0; retry
< 3; retry
++) {
4343 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4349 s
->next
= *super_list
;
4357 if ((dfd
>= 0) && (!keep_fd
))
4364 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
)
4371 sra
= sysfs_read(fd
, 0, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
4375 if (sra
->array
.major_version
!= -1 ||
4376 sra
->array
.minor_version
!= -2 ||
4377 strcmp(sra
->text_version
, "imsm") != 0) {
4382 devnum
= fd2devnum(fd
);
4383 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
4384 if (get_super_block(super_list
, devnum
, devname
,
4385 sd
->disk
.major
, sd
->disk
.minor
, keep_fd
) != 0) {
4396 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
4398 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
, NULL
, 1);
4402 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
4404 struct intel_super
*super
;
4407 if (test_partition(fd
))
4408 /* IMSM not allowed on partitions */
4411 free_super_imsm(st
);
4413 super
= alloc_super();
4416 Name
": malloc of %zu failed.\n",
4420 /* Load hba and capabilities if they exist.
4421 * But do not preclude loading metadata in case capabilities or hba are
4422 * non-compliant and ignore_hw_compat is set.
4424 rv
= find_intel_hba_capability(fd
, super
, devname
);
4425 /* no orom/efi or non-intel hba of the disk */
4426 if ((rv
!= 0) && (st
->ignore_hw_compat
== 0)) {
4429 Name
": No OROM/EFI properties for %s\n", devname
);
4433 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
4438 Name
": Failed to load all information "
4439 "sections on %s\n", devname
);
4445 if (st
->ss
== NULL
) {
4446 st
->ss
= &super_imsm
;
4447 st
->minor_version
= 0;
4448 st
->max_devs
= IMSM_MAX_DEVICES
;
4451 /* load migration record */
4452 if (load_imsm_migr_rec(super
, NULL
) == 0) {
4453 /* Check for unsupported migration features */
4454 if (check_mpb_migr_compatibility(super
) != 0) {
4456 Name
": Unsupported migration detected");
4458 fprintf(stderr
, " on %s\n", devname
);
4460 fprintf(stderr
, " (IMSM).\n");
4468 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
4470 if (info
->level
== 1)
4472 return info
->chunk_size
>> 9;
4475 static unsigned long long info_to_blocks_per_member(mdu_array_info_t
*info
,
4476 unsigned long long size
)
4478 if (info
->level
== 1)
4481 return (size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
4484 static void imsm_update_version_info(struct intel_super
*super
)
4486 /* update the version and attributes */
4487 struct imsm_super
*mpb
= super
->anchor
;
4489 struct imsm_dev
*dev
;
4490 struct imsm_map
*map
;
4493 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4494 dev
= get_imsm_dev(super
, i
);
4495 map
= get_imsm_map(dev
, MAP_0
);
4496 if (__le32_to_cpu(dev
->size_high
) > 0)
4497 mpb
->attributes
|= MPB_ATTRIB_2TB
;
4499 /* FIXME detect when an array spans a port multiplier */
4501 mpb
->attributes
|= MPB_ATTRIB_PM
;
4504 if (mpb
->num_raid_devs
> 1 ||
4505 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
4506 version
= MPB_VERSION_ATTRIBS
;
4507 switch (get_imsm_raid_level(map
)) {
4508 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
4509 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
4510 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
4511 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
4514 if (map
->num_members
>= 5)
4515 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
4516 else if (dev
->status
== DEV_CLONE_N_GO
)
4517 version
= MPB_VERSION_CNG
;
4518 else if (get_imsm_raid_level(map
) == 5)
4519 version
= MPB_VERSION_RAID5
;
4520 else if (map
->num_members
>= 3)
4521 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
4522 else if (get_imsm_raid_level(map
) == 1)
4523 version
= MPB_VERSION_RAID1
;
4525 version
= MPB_VERSION_RAID0
;
4527 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
4531 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
4533 struct imsm_super
*mpb
= super
->anchor
;
4534 char *reason
= NULL
;
4537 if (strlen(name
) > MAX_RAID_SERIAL_LEN
)
4538 reason
= "must be 16 characters or less";
4540 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4541 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
4543 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
4544 reason
= "already exists";
4549 if (reason
&& !quiet
)
4550 fprintf(stderr
, Name
": imsm volume name %s\n", reason
);
4555 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
4556 unsigned long long size
, char *name
,
4557 char *homehost
, int *uuid
)
4559 /* We are creating a volume inside a pre-existing container.
4560 * so st->sb is already set.
4562 struct intel_super
*super
= st
->sb
;
4563 struct imsm_super
*mpb
= super
->anchor
;
4564 struct intel_dev
*dv
;
4565 struct imsm_dev
*dev
;
4566 struct imsm_vol
*vol
;
4567 struct imsm_map
*map
;
4568 int idx
= mpb
->num_raid_devs
;
4570 unsigned long long array_blocks
;
4571 size_t size_old
, size_new
;
4572 unsigned long long num_data_stripes
;
4574 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
4575 fprintf(stderr
, Name
": This imsm-container already has the "
4576 "maximum of %d volumes\n", super
->orom
->vpa
);
4580 /* ensure the mpb is large enough for the new data */
4581 size_old
= __le32_to_cpu(mpb
->mpb_size
);
4582 size_new
= disks_to_mpb_size(info
->nr_disks
);
4583 if (size_new
> size_old
) {
4585 size_t size_round
= ROUND_UP(size_new
, 512);
4587 if (posix_memalign(&mpb_new
, 512, size_round
) != 0) {
4588 fprintf(stderr
, Name
": could not allocate new mpb\n");
4591 if (posix_memalign(&super
->migr_rec_buf
, 512,
4592 MIGR_REC_BUF_SIZE
) != 0) {
4593 fprintf(stderr
, Name
4594 ": %s could not allocate migr_rec buffer\n",
4601 memcpy(mpb_new
, mpb
, size_old
);
4604 super
->anchor
= mpb_new
;
4605 mpb
->mpb_size
= __cpu_to_le32(size_new
);
4606 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
4608 super
->current_vol
= idx
;
4610 /* handle 'failed_disks' by either:
4611 * a) create dummy disk entries in the table if this the first
4612 * volume in the array. We add them here as this is the only
4613 * opportunity to add them. add_to_super_imsm_volume()
4614 * handles the non-failed disks and continues incrementing
4616 * b) validate that 'failed_disks' matches the current number
4617 * of missing disks if the container is populated
4619 if (super
->current_vol
== 0) {
4621 for (i
= 0; i
< info
->failed_disks
; i
++) {
4622 struct imsm_disk
*disk
;
4625 disk
= __get_imsm_disk(mpb
, i
);
4626 disk
->status
= CONFIGURED_DISK
| FAILED_DISK
;
4627 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
4628 snprintf((char *) disk
->serial
, MAX_RAID_SERIAL_LEN
,
4631 find_missing(super
);
4636 for (d
= super
->missing
; d
; d
= d
->next
)
4638 if (info
->failed_disks
> missing
) {
4639 fprintf(stderr
, Name
": unable to add 'missing' disk to container\n");
4644 if (!check_name(super
, name
, 0))
4646 dv
= malloc(sizeof(*dv
));
4648 fprintf(stderr
, Name
": failed to allocate device list entry\n");
4651 dev
= calloc(1, sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
4654 fprintf(stderr
, Name
": could not allocate raid device\n");
4658 strncpy((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
4659 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
4660 info
->layout
, info
->chunk_size
,
4662 /* round array size down to closest MB */
4663 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
4665 dev
->size_low
= __cpu_to_le32((__u32
) array_blocks
);
4666 dev
->size_high
= __cpu_to_le32((__u32
) (array_blocks
>> 32));
4667 dev
->status
= (DEV_READ_COALESCING
| DEV_WRITE_COALESCING
);
4669 vol
->migr_state
= 0;
4670 set_migr_type(dev
, MIGR_INIT
);
4671 vol
->dirty
= !info
->state
;
4672 vol
->curr_migr_unit
= 0;
4673 map
= get_imsm_map(dev
, MAP_0
);
4674 set_pba_of_lba0(map
, super
->create_offset
);
4675 set_blocks_per_member(map
, info_to_blocks_per_member(info
, size
));
4676 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
4677 map
->failed_disk_num
= ~0;
4678 if (info
->level
> 0)
4679 map
->map_state
= IMSM_T_STATE_UNINITIALIZED
;
4681 map
->map_state
= info
->failed_disks
? IMSM_T_STATE_FAILED
:
4682 IMSM_T_STATE_NORMAL
;
4685 if (info
->level
== 1 && info
->raid_disks
> 2) {
4688 fprintf(stderr
, Name
": imsm does not support more than 2 disks"
4689 "in a raid1 volume\n");
4693 map
->raid_level
= info
->level
;
4694 if (info
->level
== 10) {
4695 map
->raid_level
= 1;
4696 map
->num_domains
= info
->raid_disks
/ 2;
4697 } else if (info
->level
== 1)
4698 map
->num_domains
= info
->raid_disks
;
4700 map
->num_domains
= 1;
4702 /* info->size is only int so use the 'size' parameter instead */
4703 num_data_stripes
= (size
* 2) / info_to_blocks_per_strip(info
);
4704 num_data_stripes
/= map
->num_domains
;
4705 set_num_data_stripes(map
, num_data_stripes
);
4707 map
->num_members
= info
->raid_disks
;
4708 for (i
= 0; i
< map
->num_members
; i
++) {
4709 /* initialized in add_to_super */
4710 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
4712 mpb
->num_raid_devs
++;
4715 dv
->index
= super
->current_vol
;
4716 dv
->next
= super
->devlist
;
4717 super
->devlist
= dv
;
4719 imsm_update_version_info(super
);
4724 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
4725 unsigned long long size
, char *name
,
4726 char *homehost
, int *uuid
)
4728 /* This is primarily called by Create when creating a new array.
4729 * We will then get add_to_super called for each component, and then
4730 * write_init_super called to write it out to each device.
4731 * For IMSM, Create can create on fresh devices or on a pre-existing
4733 * To create on a pre-existing array a different method will be called.
4734 * This one is just for fresh drives.
4736 struct intel_super
*super
;
4737 struct imsm_super
*mpb
;
4742 return init_super_imsm_volume(st
, info
, size
, name
, homehost
, uuid
);
4745 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
4749 super
= alloc_super();
4750 if (super
&& posix_memalign(&super
->buf
, 512, mpb_size
) != 0) {
4755 fprintf(stderr
, Name
4756 ": %s could not allocate superblock\n", __func__
);
4759 if (posix_memalign(&super
->migr_rec_buf
, 512, MIGR_REC_BUF_SIZE
) != 0) {
4760 fprintf(stderr
, Name
4761 ": %s could not allocate migr_rec buffer\n", __func__
);
4766 memset(super
->buf
, 0, mpb_size
);
4768 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
4772 /* zeroing superblock */
4776 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
4778 version
= (char *) mpb
->sig
;
4779 strcpy(version
, MPB_SIGNATURE
);
4780 version
+= strlen(MPB_SIGNATURE
);
4781 strcpy(version
, MPB_VERSION_RAID0
);
4787 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
4788 int fd
, char *devname
)
4790 struct intel_super
*super
= st
->sb
;
4791 struct imsm_super
*mpb
= super
->anchor
;
4792 struct imsm_disk
*_disk
;
4793 struct imsm_dev
*dev
;
4794 struct imsm_map
*map
;
4798 dev
= get_imsm_dev(super
, super
->current_vol
);
4799 map
= get_imsm_map(dev
, MAP_0
);
4801 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
4802 fprintf(stderr
, Name
": %s: Cannot add spare devices to IMSM volume\n",
4808 /* we're doing autolayout so grab the pre-marked (in
4809 * validate_geometry) raid_disk
4811 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4812 if (dl
->raiddisk
== dk
->raid_disk
)
4815 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4816 if (dl
->major
== dk
->major
&&
4817 dl
->minor
== dk
->minor
)
4822 fprintf(stderr
, Name
": %s is not a member of the same container\n", devname
);
4826 /* add a pristine spare to the metadata */
4827 if (dl
->index
< 0) {
4828 dl
->index
= super
->anchor
->num_disks
;
4829 super
->anchor
->num_disks
++;
4831 /* Check the device has not already been added */
4832 slot
= get_imsm_disk_slot(map
, dl
->index
);
4834 (get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
) & IMSM_ORD_REBUILD
) == 0) {
4835 fprintf(stderr
, Name
": %s has been included in this array twice\n",
4839 set_imsm_ord_tbl_ent(map
, dk
->raid_disk
, dl
->index
);
4840 dl
->disk
.status
= CONFIGURED_DISK
;
4842 /* update size of 'missing' disks to be at least as large as the
4843 * largest acitve member (we only have dummy missing disks when
4844 * creating the first volume)
4846 if (super
->current_vol
== 0) {
4847 for (df
= super
->missing
; df
; df
= df
->next
) {
4848 if (total_blocks(&dl
->disk
) > total_blocks(&df
->disk
))
4849 set_total_blocks(&df
->disk
, total_blocks(&dl
->disk
));
4850 _disk
= __get_imsm_disk(mpb
, df
->index
);
4855 /* refresh unset/failed slots to point to valid 'missing' entries */
4856 for (df
= super
->missing
; df
; df
= df
->next
)
4857 for (slot
= 0; slot
< mpb
->num_disks
; slot
++) {
4858 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
4860 if ((ord
& IMSM_ORD_REBUILD
) == 0)
4862 set_imsm_ord_tbl_ent(map
, slot
, df
->index
| IMSM_ORD_REBUILD
);
4863 if (is_gen_migration(dev
)) {
4864 struct imsm_map
*map2
= get_imsm_map(dev
,
4866 int slot2
= get_imsm_disk_slot(map2
, df
->index
);
4867 if ((slot2
< map2
->num_members
) &&
4869 __u32 ord2
= get_imsm_ord_tbl_ent(dev
,
4872 if ((unsigned)df
->index
==
4874 set_imsm_ord_tbl_ent(map2
,
4880 dprintf("set slot:%d to missing disk:%d\n", slot
, df
->index
);
4884 /* if we are creating the first raid device update the family number */
4885 if (super
->current_vol
== 0) {
4887 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
4889 _disk
= __get_imsm_disk(mpb
, dl
->index
);
4890 if (!_dev
|| !_disk
) {
4891 fprintf(stderr
, Name
": BUG mpb setup error\n");
4897 sum
+= __gen_imsm_checksum(mpb
);
4898 mpb
->family_num
= __cpu_to_le32(sum
);
4899 mpb
->orig_family_num
= mpb
->family_num
;
4901 super
->current_disk
= dl
;
4906 * Function marks disk as spare and restores disk serial
4907 * in case it was previously marked as failed by takeover operation
4909 * -1 : critical error
4910 * 0 : disk is marked as spare but serial is not set
4913 int mark_spare(struct dl
*disk
)
4915 __u8 serial
[MAX_RAID_SERIAL_LEN
];
4922 if (!imsm_read_serial(disk
->fd
, NULL
, serial
)) {
4923 /* Restore disk serial number, because takeover marks disk
4924 * as failed and adds to serial ':0' before it becomes
4927 serialcpy(disk
->serial
, serial
);
4928 serialcpy(disk
->disk
.serial
, serial
);
4931 disk
->disk
.status
= SPARE_DISK
;
4937 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
4938 int fd
, char *devname
)
4940 struct intel_super
*super
= st
->sb
;
4942 unsigned long long size
;
4947 /* If we are on an RAID enabled platform check that the disk is
4948 * attached to the raid controller.
4949 * We do not need to test disks attachment for container based additions,
4950 * they shall be already tested when container was created/assembled.
4952 rv
= find_intel_hba_capability(fd
, super
, devname
);
4953 /* no orom/efi or non-intel hba of the disk */
4955 dprintf("capability: %p fd: %d ret: %d\n",
4956 super
->orom
, fd
, rv
);
4960 if (super
->current_vol
>= 0)
4961 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
4964 dd
= malloc(sizeof(*dd
));
4967 Name
": malloc failed %s:%d.\n", __func__
, __LINE__
);
4970 memset(dd
, 0, sizeof(*dd
));
4971 dd
->major
= major(stb
.st_rdev
);
4972 dd
->minor
= minor(stb
.st_rdev
);
4973 dd
->devname
= devname
? strdup(devname
) : NULL
;
4976 dd
->action
= DISK_ADD
;
4977 rv
= imsm_read_serial(fd
, devname
, dd
->serial
);
4980 Name
": failed to retrieve scsi serial, aborting\n");
4985 get_dev_size(fd
, NULL
, &size
);
4987 serialcpy(dd
->disk
.serial
, dd
->serial
);
4988 set_total_blocks(&dd
->disk
, size
);
4989 if (__le32_to_cpu(dd
->disk
.total_blocks_hi
) > 0) {
4990 struct imsm_super
*mpb
= super
->anchor
;
4991 mpb
->attributes
|= MPB_ATTRIB_2TB_DISK
;
4994 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
4995 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
4997 dd
->disk
.scsi_id
= __cpu_to_le32(0);
4999 if (st
->update_tail
) {
5000 dd
->next
= super
->disk_mgmt_list
;
5001 super
->disk_mgmt_list
= dd
;
5003 dd
->next
= super
->disks
;
5005 super
->updates_pending
++;
5012 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
5014 struct intel_super
*super
= st
->sb
;
5017 /* remove from super works only in mdmon - for communication
5018 * manager - monitor. Check if communication memory buffer
5021 if (!st
->update_tail
) {
5023 Name
": %s shall be used in mdmon context only"
5024 "(line %d).\n", __func__
, __LINE__
);
5027 dd
= malloc(sizeof(*dd
));
5030 Name
": malloc failed %s:%d.\n", __func__
, __LINE__
);
5033 memset(dd
, 0, sizeof(*dd
));
5034 dd
->major
= dk
->major
;
5035 dd
->minor
= dk
->minor
;
5038 dd
->action
= DISK_REMOVE
;
5040 dd
->next
= super
->disk_mgmt_list
;
5041 super
->disk_mgmt_list
= dd
;
5047 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
5051 struct imsm_super anchor
;
5052 } spare_record
__attribute__ ((aligned(512)));
5054 /* spare records have their own family number and do not have any defined raid
5057 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
5059 struct imsm_super
*mpb
= super
->anchor
;
5060 struct imsm_super
*spare
= &spare_record
.anchor
;
5064 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
)),
5065 spare
->generation_num
= __cpu_to_le32(1UL),
5066 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
5067 spare
->num_disks
= 1,
5068 spare
->num_raid_devs
= 0,
5069 spare
->cache_size
= mpb
->cache_size
,
5070 spare
->pwr_cycle_count
= __cpu_to_le32(1),
5072 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
5073 MPB_SIGNATURE MPB_VERSION_RAID0
);
5075 for (d
= super
->disks
; d
; d
= d
->next
) {
5079 spare
->disk
[0] = d
->disk
;
5080 if (__le32_to_cpu(d
->disk
.total_blocks_hi
) > 0)
5081 spare
->attributes
|= MPB_ATTRIB_2TB_DISK
;
5083 sum
= __gen_imsm_checksum(spare
);
5084 spare
->family_num
= __cpu_to_le32(sum
);
5085 spare
->orig_family_num
= 0;
5086 sum
= __gen_imsm_checksum(spare
);
5087 spare
->check_sum
= __cpu_to_le32(sum
);
5089 if (store_imsm_mpb(d
->fd
, spare
)) {
5090 fprintf(stderr
, "%s: failed for device %d:%d %s\n",
5091 __func__
, d
->major
, d
->minor
, strerror(errno
));
5103 static int write_super_imsm(struct supertype
*st
, int doclose
)
5105 struct intel_super
*super
= st
->sb
;
5106 struct imsm_super
*mpb
= super
->anchor
;
5112 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
5114 int clear_migration_record
= 1;
5116 /* 'generation' is incremented everytime the metadata is written */
5117 generation
= __le32_to_cpu(mpb
->generation_num
);
5119 mpb
->generation_num
= __cpu_to_le32(generation
);
5121 /* fix up cases where previous mdadm releases failed to set
5124 if (mpb
->orig_family_num
== 0)
5125 mpb
->orig_family_num
= mpb
->family_num
;
5127 for (d
= super
->disks
; d
; d
= d
->next
) {
5131 mpb
->disk
[d
->index
] = d
->disk
;
5135 for (d
= super
->missing
; d
; d
= d
->next
) {
5136 mpb
->disk
[d
->index
] = d
->disk
;
5139 mpb
->num_disks
= num_disks
;
5140 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
5142 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5143 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
5144 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
5146 imsm_copy_dev(dev
, dev2
);
5147 mpb_size
+= sizeof_imsm_dev(dev
, 0);
5149 if (is_gen_migration(dev2
))
5150 clear_migration_record
= 0;
5152 mpb_size
+= __le32_to_cpu(mpb
->bbm_log_size
);
5153 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5155 /* recalculate checksum */
5156 sum
= __gen_imsm_checksum(mpb
);
5157 mpb
->check_sum
= __cpu_to_le32(sum
);
5159 if (super
->clean_migration_record_by_mdmon
) {
5160 clear_migration_record
= 1;
5161 super
->clean_migration_record_by_mdmon
= 0;
5163 if (clear_migration_record
)
5164 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SIZE
);
5166 /* write the mpb for disks that compose raid devices */
5167 for (d
= super
->disks
; d
; d
= d
->next
) {
5168 if (d
->index
< 0 || is_failed(&d
->disk
))
5171 if (clear_migration_record
) {
5172 unsigned long long dsize
;
5174 get_dev_size(d
->fd
, NULL
, &dsize
);
5175 if (lseek64(d
->fd
, dsize
- 512, SEEK_SET
) >= 0) {
5176 if (write(d
->fd
, super
->migr_rec_buf
,
5177 MIGR_REC_BUF_SIZE
) != MIGR_REC_BUF_SIZE
)
5178 perror("Write migr_rec failed");
5182 if (store_imsm_mpb(d
->fd
, mpb
))
5184 "%s: failed for device %d:%d (fd: %d)%s\n",
5185 __func__
, d
->major
, d
->minor
,
5186 d
->fd
, strerror(errno
));
5195 return write_super_imsm_spares(super
, doclose
);
5201 static int create_array(struct supertype
*st
, int dev_idx
)
5204 struct imsm_update_create_array
*u
;
5205 struct intel_super
*super
= st
->sb
;
5206 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
5207 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
5208 struct disk_info
*inf
;
5209 struct imsm_disk
*disk
;
5212 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
5213 sizeof(*inf
) * map
->num_members
;
5216 fprintf(stderr
, "%s: failed to allocate update buffer\n",
5221 u
->type
= update_create_array
;
5222 u
->dev_idx
= dev_idx
;
5223 imsm_copy_dev(&u
->dev
, dev
);
5224 inf
= get_disk_info(u
);
5225 for (i
= 0; i
< map
->num_members
; i
++) {
5226 int idx
= get_imsm_disk_idx(dev
, i
, MAP_X
);
5228 disk
= get_imsm_disk(super
, idx
);
5229 serialcpy(inf
[i
].serial
, disk
->serial
);
5231 append_metadata_update(st
, u
, len
);
5236 static int mgmt_disk(struct supertype
*st
)
5238 struct intel_super
*super
= st
->sb
;
5240 struct imsm_update_add_remove_disk
*u
;
5242 if (!super
->disk_mgmt_list
)
5248 fprintf(stderr
, "%s: failed to allocate update buffer\n",
5253 u
->type
= update_add_remove_disk
;
5254 append_metadata_update(st
, u
, len
);
5259 static int write_init_super_imsm(struct supertype
*st
)
5261 struct intel_super
*super
= st
->sb
;
5262 int current_vol
= super
->current_vol
;
5264 /* we are done with current_vol reset it to point st at the container */
5265 super
->current_vol
= -1;
5267 if (st
->update_tail
) {
5268 /* queue the recently created array / added disk
5269 * as a metadata update */
5272 /* determine if we are creating a volume or adding a disk */
5273 if (current_vol
< 0) {
5274 /* in the mgmt (add/remove) disk case we are running
5275 * in mdmon context, so don't close fd's
5277 return mgmt_disk(st
);
5279 rv
= create_array(st
, current_vol
);
5284 for (d
= super
->disks
; d
; d
= d
->next
)
5285 Kill(d
->devname
, NULL
, 0, 1, 1);
5286 return write_super_imsm(st
, 1);
5291 static int store_super_imsm(struct supertype
*st
, int fd
)
5293 struct intel_super
*super
= st
->sb
;
5294 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
5300 return store_imsm_mpb(fd
, mpb
);
5306 static int imsm_bbm_log_size(struct imsm_super
*mpb
)
5308 return __le32_to_cpu(mpb
->bbm_log_size
);
5312 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
5313 int layout
, int raiddisks
, int chunk
,
5314 unsigned long long size
, char *dev
,
5315 unsigned long long *freesize
,
5319 unsigned long long ldsize
;
5320 struct intel_super
*super
=NULL
;
5323 if (level
!= LEVEL_CONTAINER
)
5328 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
5331 fprintf(stderr
, Name
": imsm: Cannot open %s: %s\n",
5332 dev
, strerror(errno
));
5335 if (!get_dev_size(fd
, dev
, &ldsize
)) {
5340 /* capabilities retrieve could be possible
5341 * note that there is no fd for the disks in array.
5343 super
= alloc_super();
5346 Name
": malloc of %zu failed.\n",
5352 rv
= find_intel_hba_capability(fd
, super
, verbose
? dev
: NULL
);
5356 fd2devname(fd
, str
);
5357 dprintf("validate_geometry_imsm_container: fd: %d %s orom: %p rv: %d raiddisk: %d\n",
5358 fd
, str
, super
->orom
, rv
, raiddisks
);
5360 /* no orom/efi or non-intel hba of the disk */
5367 if (raiddisks
> super
->orom
->tds
) {
5369 fprintf(stderr
, Name
": %d exceeds maximum number of"
5370 " platform supported disks: %d\n",
5371 raiddisks
, super
->orom
->tds
);
5375 if ((super
->orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) == 0 &&
5376 (ldsize
>> 9) >> 32 > 0) {
5378 fprintf(stderr
, Name
": %s exceeds maximum platform supported size\n", dev
);
5384 *freesize
= avail_size_imsm(st
, ldsize
>> 9);
5390 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
5392 const unsigned long long base_start
= e
[*idx
].start
;
5393 unsigned long long end
= base_start
+ e
[*idx
].size
;
5396 if (base_start
== end
)
5400 for (i
= *idx
; i
< num_extents
; i
++) {
5401 /* extend overlapping extents */
5402 if (e
[i
].start
>= base_start
&&
5403 e
[i
].start
<= end
) {
5406 if (e
[i
].start
+ e
[i
].size
> end
)
5407 end
= e
[i
].start
+ e
[i
].size
;
5408 } else if (e
[i
].start
> end
) {
5414 return end
- base_start
;
5417 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
5419 /* build a composite disk with all known extents and generate a new
5420 * 'maxsize' given the "all disks in an array must share a common start
5421 * offset" constraint
5423 struct extent
*e
= calloc(sum_extents
, sizeof(*e
));
5427 unsigned long long pos
;
5428 unsigned long long start
= 0;
5429 unsigned long long maxsize
;
5430 unsigned long reserve
;
5435 /* coalesce and sort all extents. also, check to see if we need to
5436 * reserve space between member arrays
5439 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5442 for (i
= 0; i
< dl
->extent_cnt
; i
++)
5445 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
5450 while (i
< sum_extents
) {
5451 e
[j
].start
= e
[i
].start
;
5452 e
[j
].size
= find_size(e
, &i
, sum_extents
);
5454 if (e
[j
-1].size
== 0)
5463 unsigned long long esize
;
5465 esize
= e
[i
].start
- pos
;
5466 if (esize
>= maxsize
) {
5471 pos
= e
[i
].start
+ e
[i
].size
;
5473 } while (e
[i
-1].size
);
5479 /* FIXME assumes volume at offset 0 is the first volume in a
5482 if (start_extent
> 0)
5483 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
5487 if (maxsize
< reserve
)
5490 super
->create_offset
= ~((unsigned long long) 0);
5491 if (start
+ reserve
> super
->create_offset
)
5492 return 0; /* start overflows create_offset */
5493 super
->create_offset
= start
+ reserve
;
5495 return maxsize
- reserve
;
5498 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
5500 if (level
< 0 || level
== 6 || level
== 4)
5503 /* if we have an orom prevent invalid raid levels */
5506 case 0: return imsm_orom_has_raid0(orom
);
5509 return imsm_orom_has_raid1e(orom
);
5510 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
5511 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
5512 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
5515 return 1; /* not on an Intel RAID platform so anything goes */
5522 active_arrays_by_format(char *name
, char* hba
, struct md_list
**devlist
,
5523 int dpa
, int verbose
)
5525 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
5526 struct mdstat_ent
*memb
= NULL
;
5529 struct md_list
*dv
= NULL
;
5532 for (memb
= mdstat
; memb
; memb
= memb
->next
) {
5533 if (memb
->metadata_version
&&
5534 (strncmp(memb
->metadata_version
, "external:", 9) == 0) &&
5535 (strcmp(&memb
->metadata_version
[9], name
) == 0) &&
5536 !is_subarray(memb
->metadata_version
+9) &&
5538 struct dev_member
*dev
= memb
->members
;
5540 while(dev
&& (fd
< 0)) {
5541 char *path
= malloc(strlen(dev
->name
) + strlen("/dev/") + 1);
5543 num
= sprintf(path
, "%s%s", "/dev/", dev
->name
);
5545 fd
= open(path
, O_RDONLY
, 0);
5546 if ((num
<= 0) || (fd
< 0)) {
5547 pr_vrb(": Cannot open %s: %s\n",
5548 dev
->name
, strerror(errno
));
5555 if ((fd
>= 0) && disk_attached_to_hba(fd
, hba
)) {
5556 struct mdstat_ent
*vol
;
5557 for (vol
= mdstat
; vol
; vol
= vol
->next
) {
5558 if ((vol
->active
> 0) &&
5559 vol
->metadata_version
&&
5560 is_container_member(vol
, memb
->dev
)) {
5565 if (*devlist
&& (found
< dpa
)) {
5566 dv
= calloc(1, sizeof(*dv
));
5568 fprintf(stderr
, Name
": calloc failed\n");
5570 dv
->devname
= malloc(strlen(memb
->dev
) + strlen("/dev/") + 1);
5571 if (dv
->devname
!= NULL
) {
5572 sprintf(dv
->devname
, "%s%s", "/dev/", memb
->dev
);
5575 dv
->next
= *devlist
;
5586 free_mdstat(mdstat
);
5591 static struct md_list
*
5592 get_loop_devices(void)
5595 struct md_list
*devlist
= NULL
;
5596 struct md_list
*dv
= NULL
;
5598 for(i
= 0; i
< 12; i
++) {
5599 dv
= calloc(1, sizeof(*dv
));
5601 fprintf(stderr
, Name
": calloc failed\n");
5604 dv
->devname
= malloc(40);
5605 if (dv
->devname
== NULL
) {
5606 fprintf(stderr
, Name
": malloc failed\n");
5610 sprintf(dv
->devname
, "/dev/loop%d", i
);
5618 static struct md_list
*
5619 get_devices(const char *hba_path
)
5621 struct md_list
*devlist
= NULL
;
5622 struct md_list
*dv
= NULL
;
5628 devlist
= get_loop_devices();
5631 /* scroll through /sys/dev/block looking for devices attached to
5634 dir
= opendir("/sys/dev/block");
5635 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
5640 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
5642 path
= devt_to_devpath(makedev(major
, minor
));
5645 if (!path_attached_to_hba(path
, hba_path
)) {
5652 fd
= dev_open(ent
->d_name
, O_RDONLY
);
5654 fd2devname(fd
, buf
);
5657 fprintf(stderr
, Name
": cannot open device: %s\n",
5663 dv
= calloc(1, sizeof(*dv
));
5665 fprintf(stderr
, Name
": malloc failed\n");
5669 dv
->devname
= strdup(buf
);
5670 if (dv
->devname
== NULL
) {
5671 fprintf(stderr
, Name
": malloc failed\n");
5682 devlist
= devlist
->next
;
5691 count_volumes_list(struct md_list
*devlist
, char *homehost
,
5692 int verbose
, int *found
)
5694 struct md_list
*tmpdev
;
5696 struct supertype
*st
= NULL
;
5698 /* first walk the list of devices to find a consistent set
5699 * that match the criterea, if that is possible.
5700 * We flag the ones we like with 'used'.
5703 st
= match_metadata_desc_imsm("imsm");
5705 pr_vrb(": cannot allocate memory for imsm supertype\n");
5709 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5710 char *devname
= tmpdev
->devname
;
5712 struct supertype
*tst
;
5714 if (tmpdev
->used
> 1)
5716 tst
= dup_super(st
);
5718 pr_vrb(": cannot allocate memory for imsm supertype\n");
5721 tmpdev
->container
= 0;
5722 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
5724 dprintf(": cannot open device %s: %s\n",
5725 devname
, strerror(errno
));
5727 } else if (fstat(dfd
, &stb
)< 0) {
5729 dprintf(": fstat failed for %s: %s\n",
5730 devname
, strerror(errno
));
5732 } else if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
5733 dprintf(": %s is not a block device.\n",
5736 } else if (must_be_container(dfd
)) {
5737 struct supertype
*cst
;
5738 cst
= super_by_fd(dfd
, NULL
);
5740 dprintf(": cannot recognize container type %s\n",
5743 } else if (tst
->ss
!= st
->ss
) {
5744 dprintf(": non-imsm container - ignore it: %s\n",
5747 } else if (!tst
->ss
->load_container
||
5748 tst
->ss
->load_container(tst
, dfd
, NULL
))
5751 tmpdev
->container
= 1;
5754 cst
->ss
->free_super(cst
);
5756 tmpdev
->st_rdev
= stb
.st_rdev
;
5757 if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
5758 dprintf(": no RAID superblock on %s\n",
5761 } else if (tst
->ss
->compare_super
== NULL
) {
5762 dprintf(": Cannot assemble %s metadata on %s\n",
5763 tst
->ss
->name
, devname
);
5769 if (tmpdev
->used
== 2 || tmpdev
->used
== 4) {
5770 /* Ignore unrecognised devices during auto-assembly */
5775 tst
->ss
->getinfo_super(tst
, &info
, NULL
);
5777 if (st
->minor_version
== -1)
5778 st
->minor_version
= tst
->minor_version
;
5780 if (memcmp(info
.uuid
, uuid_zero
,
5781 sizeof(int[4])) == 0) {
5782 /* this is a floating spare. It cannot define
5783 * an array unless there are no more arrays of
5784 * this type to be found. It can be included
5785 * in an array of this type though.
5791 if (st
->ss
!= tst
->ss
||
5792 st
->minor_version
!= tst
->minor_version
||
5793 st
->ss
->compare_super(st
, tst
) != 0) {
5794 /* Some mismatch. If exactly one array matches this host,
5795 * we can resolve on that one.
5796 * Or, if we are auto assembling, we just ignore the second
5799 dprintf(": superblock on %s doesn't match others - assembly aborted\n",
5805 dprintf("found: devname: %s\n", devname
);
5809 tst
->ss
->free_super(tst
);
5813 if ((err
= load_super_imsm_all(st
, -1, &st
->sb
, NULL
, devlist
, 0)) == 0) {
5814 struct mdinfo
*iter
, *head
= st
->ss
->container_content(st
, NULL
);
5815 for (iter
= head
; iter
; iter
= iter
->next
) {
5816 dprintf("content->text_version: %s vol\n",
5817 iter
->text_version
);
5818 if (iter
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
5819 /* do not assemble arrays with unsupported
5821 dprintf(": Cannot activate member %s.\n",
5822 iter
->text_version
);
5829 dprintf(" no valid super block on device list: err: %d %p\n",
5833 dprintf(" no more devices to examin\n");
5836 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5837 if ((tmpdev
->used
== 1) && (tmpdev
->found
)) {
5839 if (count
< tmpdev
->found
)
5842 count
-= tmpdev
->found
;
5845 if (tmpdev
->used
== 1)
5850 st
->ss
->free_super(st
);
5856 count_volumes(char *hba
, int dpa
, int verbose
)
5858 struct md_list
*devlist
= NULL
;
5862 devlist
= get_devices(hba
);
5863 /* if no intel devices return zero volumes */
5864 if (devlist
== NULL
)
5867 count
= active_arrays_by_format("imsm", hba
, &devlist
, dpa
, verbose
);
5868 dprintf(" path: %s active arrays: %d\n", hba
, count
);
5869 if (devlist
== NULL
)
5873 count
+= count_volumes_list(devlist
,
5877 dprintf("found %d count: %d\n", found
, count
);
5880 dprintf("path: %s total number of volumes: %d\n", hba
, count
);
5883 struct md_list
*dv
= devlist
;
5884 devlist
= devlist
->next
;
5891 static int imsm_default_chunk(const struct imsm_orom
*orom
)
5893 /* up to 512 if the plaform supports it, otherwise the platform max.
5894 * 128 if no platform detected
5896 int fs
= max(7, orom
? fls(orom
->sss
) : 0);
5898 return min(512, (1 << fs
));
5902 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
5903 int raiddisks
, int *chunk
, unsigned long long size
, int verbose
)
5905 /* check/set platform and metadata limits/defaults */
5906 if (super
->orom
&& raiddisks
> super
->orom
->dpa
) {
5907 pr_vrb(": platform supports a maximum of %d disks per array\n",
5912 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
5913 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
5914 pr_vrb(": platform does not support raid%d with %d disk%s\n",
5915 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
5919 if (chunk
&& (*chunk
== 0 || *chunk
== UnSet
))
5920 *chunk
= imsm_default_chunk(super
->orom
);
5922 if (super
->orom
&& chunk
&& !imsm_orom_has_chunk(super
->orom
, *chunk
)) {
5923 pr_vrb(": platform does not support a chunk size of: "
5928 if (layout
!= imsm_level_to_layout(level
)) {
5930 pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n");
5931 else if (level
== 10)
5932 pr_vrb(": imsm raid 10 only supports the n2 layout\n");
5934 pr_vrb(": imsm unknown layout %#x for this raid level %d\n",
5939 if (super
->orom
&& (super
->orom
->attr
& IMSM_OROM_ATTR_2TB
) == 0 && chunk
&&
5940 (calc_array_size(level
, raiddisks
, layout
, *chunk
, size
) >> 32) > 0) {
5941 pr_vrb(": platform does not support a volume size over 2TB\n");
5947 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
5948 * FIX ME add ahci details
5950 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
5951 int layout
, int raiddisks
, int *chunk
,
5952 unsigned long long size
, char *dev
,
5953 unsigned long long *freesize
,
5957 struct intel_super
*super
= st
->sb
;
5958 struct imsm_super
*mpb
;
5960 unsigned long long pos
= 0;
5961 unsigned long long maxsize
;
5965 /* We must have the container info already read in. */
5969 mpb
= super
->anchor
;
5971 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, size
, verbose
)) {
5972 fprintf(stderr
, Name
": RAID gemetry validation failed. "
5973 "Cannot proceed with the action(s).\n");
5977 /* General test: make sure there is space for
5978 * 'raiddisks' device extents of size 'size' at a given
5981 unsigned long long minsize
= size
;
5982 unsigned long long start_offset
= MaxSector
;
5985 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
5986 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5991 e
= get_extents(super
, dl
);
5994 unsigned long long esize
;
5995 esize
= e
[i
].start
- pos
;
5996 if (esize
>= minsize
)
5998 if (found
&& start_offset
== MaxSector
) {
6001 } else if (found
&& pos
!= start_offset
) {
6005 pos
= e
[i
].start
+ e
[i
].size
;
6007 } while (e
[i
-1].size
);
6012 if (dcnt
< raiddisks
) {
6014 fprintf(stderr
, Name
": imsm: Not enough "
6015 "devices with space for this array "
6023 /* This device must be a member of the set */
6024 if (stat(dev
, &stb
) < 0)
6026 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
6028 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6029 if (dl
->major
== (int)major(stb
.st_rdev
) &&
6030 dl
->minor
== (int)minor(stb
.st_rdev
))
6035 fprintf(stderr
, Name
": %s is not in the "
6036 "same imsm set\n", dev
);
6038 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
6039 /* If a volume is present then the current creation attempt
6040 * cannot incorporate new spares because the orom may not
6041 * understand this configuration (all member disks must be
6042 * members of each array in the container).
6044 fprintf(stderr
, Name
": %s is a spare and a volume"
6045 " is already defined for this container\n", dev
);
6046 fprintf(stderr
, Name
": The option-rom requires all member"
6047 " disks to be a member of all volumes\n");
6049 } else if (super
->orom
&& mpb
->num_raid_devs
> 0 &&
6050 mpb
->num_disks
!= raiddisks
) {
6051 fprintf(stderr
, Name
": The option-rom requires all member"
6052 " disks to be a member of all volumes\n");
6056 /* retrieve the largest free space block */
6057 e
= get_extents(super
, dl
);
6062 unsigned long long esize
;
6064 esize
= e
[i
].start
- pos
;
6065 if (esize
>= maxsize
)
6067 pos
= e
[i
].start
+ e
[i
].size
;
6069 } while (e
[i
-1].size
);
6074 fprintf(stderr
, Name
": unable to determine free space for: %s\n",
6078 if (maxsize
< size
) {
6080 fprintf(stderr
, Name
": %s not enough space (%llu < %llu)\n",
6081 dev
, maxsize
, size
);
6085 /* count total number of extents for merge */
6087 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6089 i
+= dl
->extent_cnt
;
6091 maxsize
= merge_extents(super
, i
);
6093 if (!check_env("IMSM_NO_PLATFORM") &&
6094 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
6095 fprintf(stderr
, Name
": attempting to create a second "
6096 "volume with size less then remaining space. "
6101 if (maxsize
< size
|| maxsize
== 0) {
6104 fprintf(stderr
, Name
": no free space"
6105 " left on device. Aborting...\n");
6107 fprintf(stderr
, Name
": not enough space"
6108 " to create volume of given size"
6109 " (%llu < %llu). Aborting...\n",
6115 *freesize
= maxsize
;
6118 int count
= count_volumes(super
->hba
->path
,
6119 super
->orom
->dpa
, verbose
);
6120 if (super
->orom
->vphba
<= count
) {
6121 pr_vrb(": platform does not support more than %d raid volumes.\n",
6122 super
->orom
->vphba
);
6129 static int imsm_get_free_size(struct supertype
*st
, int raiddisks
,
6130 unsigned long long size
, int chunk
,
6131 unsigned long long *freesize
)
6133 struct intel_super
*super
= st
->sb
;
6134 struct imsm_super
*mpb
= super
->anchor
;
6139 unsigned long long maxsize
;
6140 unsigned long long minsize
;
6144 /* find the largest common start free region of the possible disks */
6148 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6154 /* don't activate new spares if we are orom constrained
6155 * and there is already a volume active in the container
6157 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
6160 e
= get_extents(super
, dl
);
6163 for (i
= 1; e
[i
-1].size
; i
++)
6171 maxsize
= merge_extents(super
, extent_cnt
);
6175 minsize
= chunk
* 2;
6177 if (cnt
< raiddisks
||
6178 (super
->orom
&& used
&& used
!= raiddisks
) ||
6179 maxsize
< minsize
||
6181 fprintf(stderr
, Name
": not enough devices with space to create array.\n");
6182 return 0; /* No enough free spaces large enough */
6193 if (!check_env("IMSM_NO_PLATFORM") &&
6194 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
6195 fprintf(stderr
, Name
": attempting to create a second "
6196 "volume with size less then remaining space. "
6201 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6203 dl
->raiddisk
= cnt
++;
6207 dprintf("imsm: imsm_get_free_size() returns : %llu\n", size
);
6212 static int reserve_space(struct supertype
*st
, int raiddisks
,
6213 unsigned long long size
, int chunk
,
6214 unsigned long long *freesize
)
6216 struct intel_super
*super
= st
->sb
;
6221 rv
= imsm_get_free_size(st
, raiddisks
, size
, chunk
, freesize
);
6224 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6226 dl
->raiddisk
= cnt
++;
6233 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
6234 int raiddisks
, int *chunk
, unsigned long long size
,
6235 char *dev
, unsigned long long *freesize
,
6243 * if given unused devices create a container
6244 * if given given devices in a container create a member volume
6246 if (level
== LEVEL_CONTAINER
) {
6247 /* Must be a fresh device to add to a container */
6248 return validate_geometry_imsm_container(st
, level
, layout
,
6250 chunk
?*chunk
:0, size
,
6257 struct intel_super
*super
= st
->sb
;
6258 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
6259 raiddisks
, chunk
, size
,
6262 /* we are being asked to automatically layout a
6263 * new volume based on the current contents of
6264 * the container. If the the parameters can be
6265 * satisfied reserve_space will record the disks,
6266 * start offset, and size of the volume to be
6267 * created. add_to_super and getinfo_super
6268 * detect when autolayout is in progress.
6270 /* assuming that freesize is always given when array is
6272 if (super
->orom
&& freesize
) {
6274 count
= count_volumes(super
->hba
->path
,
6275 super
->orom
->dpa
, verbose
);
6276 if (super
->orom
->vphba
<= count
) {
6277 pr_vrb(": platform does not support more"
6278 " than %d raid volumes.\n",
6279 super
->orom
->vphba
);
6284 return reserve_space(st
, raiddisks
, size
,
6285 chunk
?*chunk
:0, freesize
);
6290 /* creating in a given container */
6291 return validate_geometry_imsm_volume(st
, level
, layout
,
6292 raiddisks
, chunk
, size
,
6293 dev
, freesize
, verbose
);
6296 /* This device needs to be a device in an 'imsm' container */
6297 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
6301 Name
": Cannot create this array on device %s\n",
6306 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
6308 fprintf(stderr
, Name
": Cannot open %s: %s\n",
6309 dev
, strerror(errno
));
6312 /* Well, it is in use by someone, maybe an 'imsm' container. */
6313 cfd
= open_container(fd
);
6317 fprintf(stderr
, Name
": Cannot use %s: It is busy\n",
6321 sra
= sysfs_read(cfd
, 0, GET_VERSION
);
6322 if (sra
&& sra
->array
.major_version
== -1 &&
6323 strcmp(sra
->text_version
, "imsm") == 0)
6327 /* This is a member of a imsm container. Load the container
6328 * and try to create a volume
6330 struct intel_super
*super
;
6332 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
, NULL
, 1) == 0) {
6334 st
->container_dev
= fd2devnum(cfd
);
6336 return validate_geometry_imsm_volume(st
, level
, layout
,
6345 fprintf(stderr
, Name
": failed container membership check\n");
6351 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
6353 struct intel_super
*super
= st
->sb
;
6355 if (level
&& *level
== UnSet
)
6356 *level
= LEVEL_CONTAINER
;
6358 if (level
&& layout
&& *layout
== UnSet
)
6359 *layout
= imsm_level_to_layout(*level
);
6361 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0))
6362 *chunk
= imsm_default_chunk(super
->orom
);
6365 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
6367 static int kill_subarray_imsm(struct supertype
*st
)
6369 /* remove the subarray currently referenced by ->current_vol */
6371 struct intel_dev
**dp
;
6372 struct intel_super
*super
= st
->sb
;
6373 __u8 current_vol
= super
->current_vol
;
6374 struct imsm_super
*mpb
= super
->anchor
;
6376 if (super
->current_vol
< 0)
6378 super
->current_vol
= -1; /* invalidate subarray cursor */
6380 /* block deletions that would change the uuid of active subarrays
6382 * FIXME when immutable ids are available, but note that we'll
6383 * also need to fixup the invalidated/active subarray indexes in
6386 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6389 if (i
< current_vol
)
6391 sprintf(subarray
, "%u", i
);
6392 if (is_subarray_active(subarray
, st
->devname
)) {
6394 Name
": deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
6401 if (st
->update_tail
) {
6402 struct imsm_update_kill_array
*u
= malloc(sizeof(*u
));
6406 u
->type
= update_kill_array
;
6407 u
->dev_idx
= current_vol
;
6408 append_metadata_update(st
, u
, sizeof(*u
));
6413 for (dp
= &super
->devlist
; *dp
;)
6414 if ((*dp
)->index
== current_vol
) {
6417 handle_missing(super
, (*dp
)->dev
);
6418 if ((*dp
)->index
> current_vol
)
6423 /* no more raid devices, all active components are now spares,
6424 * but of course failed are still failed
6426 if (--mpb
->num_raid_devs
== 0) {
6429 for (d
= super
->disks
; d
; d
= d
->next
)
6434 super
->updates_pending
++;
6439 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
6440 char *update
, struct mddev_ident
*ident
)
6442 /* update the subarray currently referenced by ->current_vol */
6443 struct intel_super
*super
= st
->sb
;
6444 struct imsm_super
*mpb
= super
->anchor
;
6446 if (strcmp(update
, "name") == 0) {
6447 char *name
= ident
->name
;
6451 if (is_subarray_active(subarray
, st
->devname
)) {
6453 Name
": Unable to update name of active subarray\n");
6457 if (!check_name(super
, name
, 0))
6460 vol
= strtoul(subarray
, &ep
, 10);
6461 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
6464 if (st
->update_tail
) {
6465 struct imsm_update_rename_array
*u
= malloc(sizeof(*u
));
6469 u
->type
= update_rename_array
;
6471 snprintf((char *) u
->name
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6472 append_metadata_update(st
, u
, sizeof(*u
));
6474 struct imsm_dev
*dev
;
6477 dev
= get_imsm_dev(super
, vol
);
6478 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6479 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6480 dev
= get_imsm_dev(super
, i
);
6481 handle_missing(super
, dev
);
6483 super
->updates_pending
++;
6490 #endif /* MDASSEMBLE */
6492 static int is_gen_migration(struct imsm_dev
*dev
)
6497 if (!dev
->vol
.migr_state
)
6500 if (migr_type(dev
) == MIGR_GEN_MIGR
)
6506 static int is_rebuilding(struct imsm_dev
*dev
)
6508 struct imsm_map
*migr_map
;
6510 if (!dev
->vol
.migr_state
)
6513 if (migr_type(dev
) != MIGR_REBUILD
)
6516 migr_map
= get_imsm_map(dev
, MAP_1
);
6518 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
6525 static int is_initializing(struct imsm_dev
*dev
)
6527 struct imsm_map
*migr_map
;
6529 if (!dev
->vol
.migr_state
)
6532 if (migr_type(dev
) != MIGR_INIT
)
6535 migr_map
= get_imsm_map(dev
, MAP_1
);
6537 if (migr_map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
6544 static void update_recovery_start(struct intel_super
*super
,
6545 struct imsm_dev
*dev
,
6546 struct mdinfo
*array
)
6548 struct mdinfo
*rebuild
= NULL
;
6552 if (!is_rebuilding(dev
))
6555 /* Find the rebuild target, but punt on the dual rebuild case */
6556 for (d
= array
->devs
; d
; d
= d
->next
)
6557 if (d
->recovery_start
== 0) {
6564 /* (?) none of the disks are marked with
6565 * IMSM_ORD_REBUILD, so assume they are missing and the
6566 * disk_ord_tbl was not correctly updated
6568 dprintf("%s: failed to locate out-of-sync disk\n", __func__
);
6572 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
6573 rebuild
->recovery_start
= units
* blocks_per_migr_unit(super
, dev
);
6577 static int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
);
6580 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
6582 /* Given a container loaded by load_super_imsm_all,
6583 * extract information about all the arrays into
6585 * If 'subarray' is given, just extract info about that array.
6587 * For each imsm_dev create an mdinfo, fill it in,
6588 * then look for matching devices in super->disks
6589 * and create appropriate device mdinfo.
6591 struct intel_super
*super
= st
->sb
;
6592 struct imsm_super
*mpb
= super
->anchor
;
6593 struct mdinfo
*rest
= NULL
;
6597 int spare_disks
= 0;
6599 /* do not assemble arrays when not all attributes are supported */
6600 if (imsm_check_attributes(mpb
->attributes
) == 0) {
6602 fprintf(stderr
, Name
": Unsupported attributes in IMSM metadata."
6603 "Arrays activation is blocked.\n");
6606 /* check for bad blocks */
6607 if (imsm_bbm_log_size(super
->anchor
)) {
6608 fprintf(stderr
, Name
": BBM log found in IMSM metadata."
6609 "Arrays activation is blocked.\n");
6614 /* count spare devices, not used in maps
6616 for (d
= super
->disks
; d
; d
= d
->next
)
6620 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6621 struct imsm_dev
*dev
;
6622 struct imsm_map
*map
;
6623 struct imsm_map
*map2
;
6624 struct mdinfo
*this;
6632 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
6635 dev
= get_imsm_dev(super
, i
);
6636 map
= get_imsm_map(dev
, MAP_0
);
6637 map2
= get_imsm_map(dev
, MAP_1
);
6639 /* do not publish arrays that are in the middle of an
6640 * unsupported migration
6642 if (dev
->vol
.migr_state
&&
6643 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
6644 fprintf(stderr
, Name
": cannot assemble volume '%.16s':"
6645 " unsupported migration in progress\n",
6649 /* do not publish arrays that are not support by controller's
6653 this = malloc(sizeof(*this));
6655 fprintf(stderr
, Name
": failed to allocate %zu bytes\n",
6660 super
->current_vol
= i
;
6661 getinfo_super_imsm_volume(st
, this, NULL
);
6664 chunk
= __le16_to_cpu(map
->blocks_per_strip
) >> 1;
6665 /* mdadm does not support all metadata features- set the bit in all arrays state */
6666 if (!validate_geometry_imsm_orom(super
,
6667 get_imsm_raid_level(map
), /* RAID level */
6668 imsm_level_to_layout(get_imsm_raid_level(map
)),
6669 map
->num_members
, /* raid disks */
6670 &chunk
, join_u32(dev
->size_low
, dev
->size_high
),
6672 fprintf(stderr
, Name
": IMSM RAID geometry validation"
6673 " failed. Array %s activation is blocked.\n",
6675 this->array
.state
|=
6676 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
6677 (1<<MD_SB_BLOCK_VOLUME
);
6681 /* if array has bad blocks, set suitable bit in all arrays state */
6683 this->array
.state
|=
6684 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
6685 (1<<MD_SB_BLOCK_VOLUME
);
6687 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
6688 unsigned long long recovery_start
;
6689 struct mdinfo
*info_d
;
6696 idx
= get_imsm_disk_idx(dev
, slot
, MAP_0
);
6697 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
6698 for (d
= super
->disks
; d
; d
= d
->next
)
6699 if (d
->index
== idx
)
6702 recovery_start
= MaxSector
;
6705 if (d
&& is_failed(&d
->disk
))
6707 if (ord
& IMSM_ORD_REBUILD
)
6711 * if we skip some disks the array will be assmebled degraded;
6712 * reset resync start to avoid a dirty-degraded
6713 * situation when performing the intial sync
6715 * FIXME handle dirty degraded
6717 if ((skip
|| recovery_start
== 0) && !dev
->vol
.dirty
)
6718 this->resync_start
= MaxSector
;
6722 info_d
= calloc(1, sizeof(*info_d
));
6724 fprintf(stderr
, Name
": failed to allocate disk"
6725 " for volume %.16s\n", dev
->volume
);
6726 info_d
= this->devs
;
6728 struct mdinfo
*d
= info_d
->next
;
6737 info_d
->next
= this->devs
;
6738 this->devs
= info_d
;
6740 info_d
->disk
.number
= d
->index
;
6741 info_d
->disk
.major
= d
->major
;
6742 info_d
->disk
.minor
= d
->minor
;
6743 info_d
->disk
.raid_disk
= slot
;
6744 info_d
->recovery_start
= recovery_start
;
6746 if (slot
< map2
->num_members
)
6747 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
6749 this->array
.spare_disks
++;
6751 if (slot
< map
->num_members
)
6752 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
6754 this->array
.spare_disks
++;
6756 if (info_d
->recovery_start
== MaxSector
)
6757 this->array
.working_disks
++;
6759 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
6760 info_d
->data_offset
= pba_of_lba0(map
);
6761 info_d
->component_size
= blocks_per_member(map
);
6763 /* now that the disk list is up-to-date fixup recovery_start */
6764 update_recovery_start(super
, dev
, this);
6765 this->array
.spare_disks
+= spare_disks
;
6768 /* check for reshape */
6769 if (this->reshape_active
== 1)
6770 recover_backup_imsm(st
, this);
6779 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
6780 int failed
, int look_in_map
)
6782 struct imsm_map
*map
;
6784 map
= get_imsm_map(dev
, look_in_map
);
6787 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
6788 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
6790 switch (get_imsm_raid_level(map
)) {
6792 return IMSM_T_STATE_FAILED
;
6795 if (failed
< map
->num_members
)
6796 return IMSM_T_STATE_DEGRADED
;
6798 return IMSM_T_STATE_FAILED
;
6803 * check to see if any mirrors have failed, otherwise we
6804 * are degraded. Even numbered slots are mirrored on
6808 /* gcc -Os complains that this is unused */
6809 int insync
= insync
;
6811 for (i
= 0; i
< map
->num_members
; i
++) {
6812 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_X
);
6813 int idx
= ord_to_idx(ord
);
6814 struct imsm_disk
*disk
;
6816 /* reset the potential in-sync count on even-numbered
6817 * slots. num_copies is always 2 for imsm raid10
6822 disk
= get_imsm_disk(super
, idx
);
6823 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
6826 /* no in-sync disks left in this mirror the
6830 return IMSM_T_STATE_FAILED
;
6833 return IMSM_T_STATE_DEGRADED
;
6837 return IMSM_T_STATE_DEGRADED
;
6839 return IMSM_T_STATE_FAILED
;
6845 return map
->map_state
;
6848 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
6853 struct imsm_disk
*disk
;
6854 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
6855 struct imsm_map
*prev
= get_imsm_map(dev
, MAP_1
);
6856 struct imsm_map
*map_for_loop
;
6861 /* at the beginning of migration we set IMSM_ORD_REBUILD on
6862 * disks that are being rebuilt. New failures are recorded to
6863 * map[0]. So we look through all the disks we started with and
6864 * see if any failures are still present, or if any new ones
6868 if (prev
&& (map
->num_members
< prev
->num_members
))
6869 map_for_loop
= prev
;
6871 for (i
= 0; i
< map_for_loop
->num_members
; i
++) {
6873 /* when MAP_X is passed both maps failures are counted
6876 ((look_in_map
== MAP_1
) || (look_in_map
== MAP_X
)) &&
6877 (i
< prev
->num_members
)) {
6878 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
6879 idx_1
= ord_to_idx(ord
);
6881 disk
= get_imsm_disk(super
, idx_1
);
6882 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
6885 if (((look_in_map
== MAP_0
) || (look_in_map
== MAP_X
)) &&
6886 (i
< map
->num_members
)) {
6887 ord
= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
6888 idx
= ord_to_idx(ord
);
6891 disk
= get_imsm_disk(super
, idx
);
6892 if (!disk
|| is_failed(disk
) ||
6893 ord
& IMSM_ORD_REBUILD
)
6903 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
6906 struct intel_super
*super
= c
->sb
;
6907 struct imsm_super
*mpb
= super
->anchor
;
6909 if (atoi(inst
) >= mpb
->num_raid_devs
) {
6910 fprintf(stderr
, "%s: subarry index %d, out of range\n",
6911 __func__
, atoi(inst
));
6915 dprintf("imsm: open_new %s\n", inst
);
6916 a
->info
.container_member
= atoi(inst
);
6920 static int is_resyncing(struct imsm_dev
*dev
)
6922 struct imsm_map
*migr_map
;
6924 if (!dev
->vol
.migr_state
)
6927 if (migr_type(dev
) == MIGR_INIT
||
6928 migr_type(dev
) == MIGR_REPAIR
)
6931 if (migr_type(dev
) == MIGR_GEN_MIGR
)
6934 migr_map
= get_imsm_map(dev
, MAP_1
);
6936 if ((migr_map
->map_state
== IMSM_T_STATE_NORMAL
) &&
6937 (dev
->vol
.migr_type
!= MIGR_GEN_MIGR
))
6943 /* return true if we recorded new information */
6944 static int mark_failure(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
6948 struct imsm_map
*map
;
6949 char buf
[MAX_RAID_SERIAL_LEN
+3];
6950 unsigned int len
, shift
= 0;
6952 /* new failures are always set in map[0] */
6953 map
= get_imsm_map(dev
, MAP_0
);
6955 slot
= get_imsm_disk_slot(map
, idx
);
6959 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
6960 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
6963 memcpy(buf
, disk
->serial
, MAX_RAID_SERIAL_LEN
);
6964 buf
[MAX_RAID_SERIAL_LEN
] = '\000';
6966 if ((len
= strlen(buf
)) >= MAX_RAID_SERIAL_LEN
)
6967 shift
= len
- MAX_RAID_SERIAL_LEN
+ 1;
6968 strncpy((char *)disk
->serial
, &buf
[shift
], MAX_RAID_SERIAL_LEN
);
6970 disk
->status
|= FAILED_DISK
;
6971 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
6972 /* mark failures in second map if second map exists and this disk
6974 * This is valid for migration, initialization and rebuild
6976 if (dev
->vol
.migr_state
) {
6977 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
6978 int slot2
= get_imsm_disk_slot(map2
, idx
);
6980 if ((slot2
< map2
->num_members
) &&
6982 set_imsm_ord_tbl_ent(map2
, slot2
,
6983 idx
| IMSM_ORD_REBUILD
);
6985 if (map
->failed_disk_num
== 0xff)
6986 map
->failed_disk_num
= slot
;
6990 static void mark_missing(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
6992 mark_failure(dev
, disk
, idx
);
6994 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
6997 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
6998 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
7001 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
7005 if (!super
->missing
)
7008 dprintf("imsm: mark missing\n");
7009 /* end process for initialization and rebuild only
7011 if (is_gen_migration(dev
) == 0) {
7015 failed
= imsm_count_failed(super
, dev
, MAP_0
);
7016 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7018 end_migration(dev
, super
, map_state
);
7020 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
7021 mark_missing(dev
, &dl
->disk
, dl
->index
);
7022 super
->updates_pending
++;
7025 static unsigned long long imsm_set_array_size(struct imsm_dev
*dev
,
7028 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
7029 unsigned long long array_blocks
;
7030 struct imsm_map
*map
;
7032 if (used_disks
== 0) {
7033 /* when problems occures
7034 * return current array_blocks value
7036 array_blocks
= __le32_to_cpu(dev
->size_high
);
7037 array_blocks
= array_blocks
<< 32;
7038 array_blocks
+= __le32_to_cpu(dev
->size_low
);
7040 return array_blocks
;
7043 /* set array size in metadata
7045 if (new_size
<= 0) {
7046 /* OLCE size change is caused by added disks
7048 map
= get_imsm_map(dev
, MAP_0
);
7049 array_blocks
= blocks_per_member(map
) * used_disks
;
7051 /* Online Volume Size Change
7052 * Using available free space
7054 array_blocks
= new_size
;
7057 /* round array size down to closest MB
7059 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
7060 dev
->size_low
= __cpu_to_le32((__u32
)array_blocks
);
7061 dev
->size_high
= __cpu_to_le32((__u32
)(array_blocks
>> 32));
7063 return array_blocks
;
7066 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
7068 static void imsm_progress_container_reshape(struct intel_super
*super
)
7070 /* if no device has a migr_state, but some device has a
7071 * different number of members than the previous device, start
7072 * changing the number of devices in this device to match
7075 struct imsm_super
*mpb
= super
->anchor
;
7076 int prev_disks
= -1;
7080 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7081 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
7082 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7083 struct imsm_map
*map2
;
7084 int prev_num_members
;
7086 if (dev
->vol
.migr_state
)
7089 if (prev_disks
== -1)
7090 prev_disks
= map
->num_members
;
7091 if (prev_disks
== map
->num_members
)
7094 /* OK, this array needs to enter reshape mode.
7095 * i.e it needs a migr_state
7098 copy_map_size
= sizeof_imsm_map(map
);
7099 prev_num_members
= map
->num_members
;
7100 map
->num_members
= prev_disks
;
7101 dev
->vol
.migr_state
= 1;
7102 dev
->vol
.curr_migr_unit
= 0;
7103 set_migr_type(dev
, MIGR_GEN_MIGR
);
7104 for (i
= prev_num_members
;
7105 i
< map
->num_members
; i
++)
7106 set_imsm_ord_tbl_ent(map
, i
, i
);
7107 map2
= get_imsm_map(dev
, MAP_1
);
7108 /* Copy the current map */
7109 memcpy(map2
, map
, copy_map_size
);
7110 map2
->num_members
= prev_num_members
;
7112 imsm_set_array_size(dev
, -1);
7113 super
->clean_migration_record_by_mdmon
= 1;
7114 super
->updates_pending
++;
7118 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
7119 * states are handled in imsm_set_disk() with one exception, when a
7120 * resync is stopped due to a new failure this routine will set the
7121 * 'degraded' state for the array.
7123 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
7125 int inst
= a
->info
.container_member
;
7126 struct intel_super
*super
= a
->container
->sb
;
7127 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7128 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7129 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
7130 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7131 __u32 blocks_per_unit
;
7133 if (dev
->vol
.migr_state
&&
7134 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
7135 /* array state change is blocked due to reshape action
7137 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
7138 * - finish the reshape (if last_checkpoint is big and action != reshape)
7139 * - update curr_migr_unit
7141 if (a
->curr_action
== reshape
) {
7142 /* still reshaping, maybe update curr_migr_unit */
7143 goto mark_checkpoint
;
7145 if (a
->last_checkpoint
== 0 && a
->prev_action
== reshape
) {
7146 /* for some reason we aborted the reshape.
7148 * disable automatic metadata rollback
7149 * user action is required to recover process
7152 struct imsm_map
*map2
=
7153 get_imsm_map(dev
, MAP_1
);
7154 dev
->vol
.migr_state
= 0;
7155 set_migr_type(dev
, 0);
7156 dev
->vol
.curr_migr_unit
= 0;
7158 sizeof_imsm_map(map2
));
7159 super
->updates_pending
++;
7162 if (a
->last_checkpoint
>= a
->info
.component_size
) {
7163 unsigned long long array_blocks
;
7167 used_disks
= imsm_num_data_members(dev
, MAP_0
);
7168 if (used_disks
> 0) {
7170 blocks_per_member(map
) *
7172 /* round array size down to closest MB
7174 array_blocks
= (array_blocks
7175 >> SECT_PER_MB_SHIFT
)
7176 << SECT_PER_MB_SHIFT
;
7177 a
->info
.custom_array_size
= array_blocks
;
7178 /* encourage manager to update array
7182 a
->check_reshape
= 1;
7184 /* finalize online capacity expansion/reshape */
7185 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
7187 mdi
->disk
.raid_disk
,
7190 imsm_progress_container_reshape(super
);
7195 /* before we activate this array handle any missing disks */
7196 if (consistent
== 2)
7197 handle_missing(super
, dev
);
7199 if (consistent
== 2 &&
7200 (!is_resync_complete(&a
->info
) ||
7201 map_state
!= IMSM_T_STATE_NORMAL
||
7202 dev
->vol
.migr_state
))
7205 if (is_resync_complete(&a
->info
)) {
7206 /* complete intialization / resync,
7207 * recovery and interrupted recovery is completed in
7210 if (is_resyncing(dev
)) {
7211 dprintf("imsm: mark resync done\n");
7212 end_migration(dev
, super
, map_state
);
7213 super
->updates_pending
++;
7214 a
->last_checkpoint
= 0;
7216 } else if ((!is_resyncing(dev
) && !failed
) &&
7217 (imsm_reshape_blocks_arrays_changes(super
) == 0)) {
7218 /* mark the start of the init process if nothing is failed */
7219 dprintf("imsm: mark resync start\n");
7220 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
7221 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
7223 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
7224 super
->updates_pending
++;
7228 /* skip checkpointing for general migration,
7229 * it is controlled in mdadm
7231 if (is_gen_migration(dev
))
7232 goto skip_mark_checkpoint
;
7234 /* check if we can update curr_migr_unit from resync_start, recovery_start */
7235 blocks_per_unit
= blocks_per_migr_unit(super
, dev
);
7236 if (blocks_per_unit
) {
7240 units
= a
->last_checkpoint
/ blocks_per_unit
;
7243 /* check that we did not overflow 32-bits, and that
7244 * curr_migr_unit needs updating
7246 if (units32
== units
&&
7248 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
7249 dprintf("imsm: mark checkpoint (%u)\n", units32
);
7250 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
7251 super
->updates_pending
++;
7255 skip_mark_checkpoint
:
7256 /* mark dirty / clean */
7257 if (dev
->vol
.dirty
!= !consistent
) {
7258 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
7263 super
->updates_pending
++;
7269 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
7271 int inst
= a
->info
.container_member
;
7272 struct intel_super
*super
= a
->container
->sb
;
7273 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7274 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7275 struct imsm_disk
*disk
;
7280 if (n
> map
->num_members
)
7281 fprintf(stderr
, "imsm: set_disk %d out of range 0..%d\n",
7282 n
, map
->num_members
- 1);
7287 dprintf("imsm: set_disk %d:%x\n", n
, state
);
7289 ord
= get_imsm_ord_tbl_ent(dev
, n
, MAP_0
);
7290 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
7292 /* check for new failures */
7293 if (state
& DS_FAULTY
) {
7294 if (mark_failure(dev
, disk
, ord_to_idx(ord
)))
7295 super
->updates_pending
++;
7298 /* check if in_sync */
7299 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
7300 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
7302 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
7303 super
->updates_pending
++;
7306 failed
= imsm_count_failed(super
, dev
, MAP_0
);
7307 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7309 /* check if recovery complete, newly degraded, or failed */
7310 dprintf("imsm: Detected transition to state ");
7311 switch (map_state
) {
7312 case IMSM_T_STATE_NORMAL
: /* transition to normal state */
7313 dprintf("normal: ");
7314 if (is_rebuilding(dev
)) {
7315 dprintf("while rebuilding");
7316 end_migration(dev
, super
, map_state
);
7317 map
= get_imsm_map(dev
, MAP_0
);
7318 map
->failed_disk_num
= ~0;
7319 super
->updates_pending
++;
7320 a
->last_checkpoint
= 0;
7323 if (is_gen_migration(dev
)) {
7324 dprintf("while general migration");
7325 if (a
->last_checkpoint
>= a
->info
.component_size
)
7326 end_migration(dev
, super
, map_state
);
7328 map
->map_state
= map_state
;
7329 map
= get_imsm_map(dev
, MAP_0
);
7330 map
->failed_disk_num
= ~0;
7331 super
->updates_pending
++;
7335 case IMSM_T_STATE_DEGRADED
: /* transition to degraded state */
7336 dprintf("degraded: ");
7337 if ((map
->map_state
!= map_state
) &&
7338 !dev
->vol
.migr_state
) {
7339 dprintf("mark degraded");
7340 map
->map_state
= map_state
;
7341 super
->updates_pending
++;
7342 a
->last_checkpoint
= 0;
7345 if (is_rebuilding(dev
)) {
7346 dprintf("while rebuilding.");
7347 if (map
->map_state
!= map_state
) {
7348 dprintf(" Map state change");
7349 end_migration(dev
, super
, map_state
);
7350 super
->updates_pending
++;
7354 if (is_gen_migration(dev
)) {
7355 dprintf("while general migration");
7356 if (a
->last_checkpoint
>= a
->info
.component_size
)
7357 end_migration(dev
, super
, map_state
);
7359 map
->map_state
= map_state
;
7360 manage_second_map(super
, dev
);
7362 super
->updates_pending
++;
7365 if (is_initializing(dev
)) {
7366 dprintf("while initialization.");
7367 map
->map_state
= map_state
;
7368 super
->updates_pending
++;
7372 case IMSM_T_STATE_FAILED
: /* transition to failed state */
7373 dprintf("failed: ");
7374 if (is_gen_migration(dev
)) {
7375 dprintf("while general migration");
7376 map
->map_state
= map_state
;
7377 super
->updates_pending
++;
7380 if (map
->map_state
!= map_state
) {
7381 dprintf("mark failed");
7382 end_migration(dev
, super
, map_state
);
7383 super
->updates_pending
++;
7384 a
->last_checkpoint
= 0;
7389 dprintf("state %i\n", map_state
);
7395 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
7398 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
7399 unsigned long long dsize
;
7400 unsigned long long sectors
;
7402 get_dev_size(fd
, NULL
, &dsize
);
7404 if (mpb_size
> 512) {
7405 /* -1 to account for anchor */
7406 sectors
= mpb_sectors(mpb
) - 1;
7408 /* write the extended mpb to the sectors preceeding the anchor */
7409 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0)
7412 if ((unsigned long long)write(fd
, buf
+ 512, 512 * sectors
)
7417 /* first block is stored on second to last sector of the disk */
7418 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0)
7421 if (write(fd
, buf
, 512) != 512)
7427 static void imsm_sync_metadata(struct supertype
*container
)
7429 struct intel_super
*super
= container
->sb
;
7431 dprintf("sync metadata: %d\n", super
->updates_pending
);
7432 if (!super
->updates_pending
)
7435 write_super_imsm(container
, 0);
7437 super
->updates_pending
= 0;
7440 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
7442 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
7443 int i
= get_imsm_disk_idx(dev
, idx
, MAP_X
);
7446 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7450 if (dl
&& is_failed(&dl
->disk
))
7454 dprintf("%s: found %x:%x\n", __func__
, dl
->major
, dl
->minor
);
7459 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
7460 struct active_array
*a
, int activate_new
,
7461 struct mdinfo
*additional_test_list
)
7463 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
7464 int idx
= get_imsm_disk_idx(dev
, slot
, MAP_X
);
7465 struct imsm_super
*mpb
= super
->anchor
;
7466 struct imsm_map
*map
;
7467 unsigned long long pos
;
7472 __u32 array_start
= 0;
7473 __u32 array_end
= 0;
7475 struct mdinfo
*test_list
;
7477 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7478 /* If in this array, skip */
7479 for (d
= a
->info
.devs
; d
; d
= d
->next
)
7480 if (d
->state_fd
>= 0 &&
7481 d
->disk
.major
== dl
->major
&&
7482 d
->disk
.minor
== dl
->minor
) {
7483 dprintf("%x:%x already in array\n",
7484 dl
->major
, dl
->minor
);
7489 test_list
= additional_test_list
;
7491 if (test_list
->disk
.major
== dl
->major
&&
7492 test_list
->disk
.minor
== dl
->minor
) {
7493 dprintf("%x:%x already in additional test list\n",
7494 dl
->major
, dl
->minor
);
7497 test_list
= test_list
->next
;
7502 /* skip in use or failed drives */
7503 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
7505 dprintf("%x:%x status (failed: %d index: %d)\n",
7506 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
7510 /* skip pure spares when we are looking for partially
7511 * assimilated drives
7513 if (dl
->index
== -1 && !activate_new
)
7516 /* Does this unused device have the requisite free space?
7517 * It needs to be able to cover all member volumes
7519 ex
= get_extents(super
, dl
);
7521 dprintf("cannot get extents\n");
7524 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7525 dev
= get_imsm_dev(super
, i
);
7526 map
= get_imsm_map(dev
, MAP_0
);
7528 /* check if this disk is already a member of
7531 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
7537 array_start
= pba_of_lba0(map
);
7538 array_end
= array_start
+
7539 blocks_per_member(map
) - 1;
7542 /* check that we can start at pba_of_lba0 with
7543 * blocks_per_member of space
7545 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
7549 pos
= ex
[j
].start
+ ex
[j
].size
;
7551 } while (ex
[j
-1].size
);
7558 if (i
< mpb
->num_raid_devs
) {
7559 dprintf("%x:%x does not have %u to %u available\n",
7560 dl
->major
, dl
->minor
, array_start
, array_end
);
7571 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
7573 struct imsm_dev
*dev2
;
7574 struct imsm_map
*map
;
7580 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
7582 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
, MAP_0
);
7583 if (state
== IMSM_T_STATE_FAILED
) {
7584 map
= get_imsm_map(dev2
, MAP_0
);
7587 for (slot
= 0; slot
< map
->num_members
; slot
++) {
7589 * Check if failed disks are deleted from intel
7590 * disk list or are marked to be deleted
7592 idx
= get_imsm_disk_idx(dev2
, slot
, MAP_X
);
7593 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
7595 * Do not rebuild the array if failed disks
7596 * from failed sub-array are not removed from
7600 is_failed(&idisk
->disk
) &&
7601 (idisk
->action
!= DISK_REMOVE
))
7609 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
7610 struct metadata_update
**updates
)
7613 * Find a device with unused free space and use it to replace a
7614 * failed/vacant region in an array. We replace failed regions one a
7615 * array at a time. The result is that a new spare disk will be added
7616 * to the first failed array and after the monitor has finished
7617 * propagating failures the remainder will be consumed.
7619 * FIXME add a capability for mdmon to request spares from another
7623 struct intel_super
*super
= a
->container
->sb
;
7624 int inst
= a
->info
.container_member
;
7625 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7626 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7627 int failed
= a
->info
.array
.raid_disks
;
7628 struct mdinfo
*rv
= NULL
;
7631 struct metadata_update
*mu
;
7633 struct imsm_update_activate_spare
*u
;
7638 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
7639 if ((d
->curr_state
& DS_FAULTY
) &&
7641 /* wait for Removal to happen */
7643 if (d
->state_fd
>= 0)
7647 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
7648 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
7650 if (imsm_reshape_blocks_arrays_changes(super
))
7653 /* Cannot activate another spare if rebuild is in progress already
7655 if (is_rebuilding(dev
)) {
7656 dprintf("imsm: No spare activation allowed. "
7657 "Rebuild in progress already.\n");
7661 if (a
->info
.array
.level
== 4)
7662 /* No repair for takeovered array
7663 * imsm doesn't support raid4
7667 if (imsm_check_degraded(super
, dev
, failed
, MAP_0
) !=
7668 IMSM_T_STATE_DEGRADED
)
7672 * If there are any failed disks check state of the other volume.
7673 * Block rebuild if the another one is failed until failed disks
7674 * are removed from container.
7677 dprintf("found failed disks in %.*s, check if there another"
7678 "failed sub-array.\n",
7679 MAX_RAID_SERIAL_LEN
, dev
->volume
);
7680 /* check if states of the other volumes allow for rebuild */
7681 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
7683 allowed
= imsm_rebuild_allowed(a
->container
,
7691 /* For each slot, if it is not working, find a spare */
7692 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
7693 for (d
= a
->info
.devs
; d
; d
= d
->next
)
7694 if (d
->disk
.raid_disk
== i
)
7696 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
7697 if (d
&& (d
->state_fd
>= 0))
7701 * OK, this device needs recovery. Try to re-add the
7702 * previous occupant of this slot, if this fails see if
7703 * we can continue the assimilation of a spare that was
7704 * partially assimilated, finally try to activate a new
7707 dl
= imsm_readd(super
, i
, a
);
7709 dl
= imsm_add_spare(super
, i
, a
, 0, rv
);
7711 dl
= imsm_add_spare(super
, i
, a
, 1, rv
);
7715 /* found a usable disk with enough space */
7716 di
= malloc(sizeof(*di
));
7719 memset(di
, 0, sizeof(*di
));
7721 /* dl->index will be -1 in the case we are activating a
7722 * pristine spare. imsm_process_update() will create a
7723 * new index in this case. Once a disk is found to be
7724 * failed in all member arrays it is kicked from the
7727 di
->disk
.number
= dl
->index
;
7729 /* (ab)use di->devs to store a pointer to the device
7732 di
->devs
= (struct mdinfo
*) dl
;
7734 di
->disk
.raid_disk
= i
;
7735 di
->disk
.major
= dl
->major
;
7736 di
->disk
.minor
= dl
->minor
;
7738 di
->recovery_start
= 0;
7739 di
->data_offset
= pba_of_lba0(map
);
7740 di
->component_size
= a
->info
.component_size
;
7741 di
->container_member
= inst
;
7742 super
->random
= random32();
7746 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
7747 i
, di
->data_offset
);
7751 /* No spares found */
7753 /* Now 'rv' has a list of devices to return.
7754 * Create a metadata_update record to update the
7755 * disk_ord_tbl for the array
7757 mu
= malloc(sizeof(*mu
));
7759 mu
->buf
= malloc(sizeof(struct imsm_update_activate_spare
) * num_spares
);
7760 if (mu
->buf
== NULL
) {
7767 struct mdinfo
*n
= rv
->next
;
7776 mu
->space_list
= NULL
;
7777 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
7778 mu
->next
= *updates
;
7779 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
7781 for (di
= rv
; di
; di
= di
->next
) {
7782 u
->type
= update_activate_spare
;
7783 u
->dl
= (struct dl
*) di
->devs
;
7785 u
->slot
= di
->disk
.raid_disk
;
7796 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
7798 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
7799 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7800 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, MAP_0
);
7801 struct disk_info
*inf
= get_disk_info(u
);
7802 struct imsm_disk
*disk
;
7806 for (i
= 0; i
< map
->num_members
; i
++) {
7807 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, MAP_X
));
7808 for (j
= 0; j
< new_map
->num_members
; j
++)
7809 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
7817 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
7819 struct dl
*dl
= NULL
;
7820 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7821 if ((dl
->major
== major
) && (dl
->minor
== minor
))
7826 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
7828 struct dl
*prev
= NULL
;
7832 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7833 if ((dl
->major
== major
) && (dl
->minor
== minor
)) {
7836 prev
->next
= dl
->next
;
7838 super
->disks
= dl
->next
;
7840 __free_imsm_disk(dl
);
7841 dprintf("%s: removed %x:%x\n",
7842 __func__
, major
, minor
);
7850 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
7852 static int add_remove_disk_update(struct intel_super
*super
)
7854 int check_degraded
= 0;
7855 struct dl
*disk
= NULL
;
7856 /* add/remove some spares to/from the metadata/contrainer */
7857 while (super
->disk_mgmt_list
) {
7858 struct dl
*disk_cfg
;
7860 disk_cfg
= super
->disk_mgmt_list
;
7861 super
->disk_mgmt_list
= disk_cfg
->next
;
7862 disk_cfg
->next
= NULL
;
7864 if (disk_cfg
->action
== DISK_ADD
) {
7865 disk_cfg
->next
= super
->disks
;
7866 super
->disks
= disk_cfg
;
7868 dprintf("%s: added %x:%x\n",
7869 __func__
, disk_cfg
->major
,
7871 } else if (disk_cfg
->action
== DISK_REMOVE
) {
7872 dprintf("Disk remove action processed: %x.%x\n",
7873 disk_cfg
->major
, disk_cfg
->minor
);
7874 disk
= get_disk_super(super
,
7878 /* store action status */
7879 disk
->action
= DISK_REMOVE
;
7880 /* remove spare disks only */
7881 if (disk
->index
== -1) {
7882 remove_disk_super(super
,
7887 /* release allocate disk structure */
7888 __free_imsm_disk(disk_cfg
);
7891 return check_degraded
;
7895 static int apply_reshape_migration_update(struct imsm_update_reshape_migration
*u
,
7896 struct intel_super
*super
,
7899 struct intel_dev
*id
;
7900 void **tofree
= NULL
;
7903 dprintf("apply_reshape_migration_update()\n");
7904 if ((u
->subdev
< 0) ||
7906 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
7909 if ((space_list
== NULL
) || (*space_list
== NULL
)) {
7910 dprintf("imsm: Error: Memory is not allocated\n");
7914 for (id
= super
->devlist
; id
; id
= id
->next
) {
7915 if (id
->index
== (unsigned)u
->subdev
) {
7916 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
7917 struct imsm_map
*map
;
7918 struct imsm_dev
*new_dev
=
7919 (struct imsm_dev
*)*space_list
;
7920 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
7922 struct dl
*new_disk
;
7924 if (new_dev
== NULL
)
7926 *space_list
= **space_list
;
7927 memcpy(new_dev
, dev
, sizeof_imsm_dev(dev
, 0));
7928 map
= get_imsm_map(new_dev
, MAP_0
);
7930 dprintf("imsm: Error: migration in progress");
7934 to_state
= map
->map_state
;
7935 if ((u
->new_level
== 5) && (map
->raid_level
== 0)) {
7937 /* this should not happen */
7938 if (u
->new_disks
[0] < 0) {
7939 map
->failed_disk_num
=
7940 map
->num_members
- 1;
7941 to_state
= IMSM_T_STATE_DEGRADED
;
7943 to_state
= IMSM_T_STATE_NORMAL
;
7945 migrate(new_dev
, super
, to_state
, MIGR_GEN_MIGR
);
7946 if (u
->new_level
> -1)
7947 map
->raid_level
= u
->new_level
;
7948 migr_map
= get_imsm_map(new_dev
, MAP_1
);
7949 if ((u
->new_level
== 5) &&
7950 (migr_map
->raid_level
== 0)) {
7951 int ord
= map
->num_members
- 1;
7952 migr_map
->num_members
--;
7953 if (u
->new_disks
[0] < 0)
7954 ord
|= IMSM_ORD_REBUILD
;
7955 set_imsm_ord_tbl_ent(map
,
7956 map
->num_members
- 1,
7960 tofree
= (void **)dev
;
7962 /* update chunk size
7964 if (u
->new_chunksize
> 0)
7965 map
->blocks_per_strip
=
7966 __cpu_to_le16(u
->new_chunksize
* 2);
7970 if ((u
->new_level
!= 5) ||
7971 (migr_map
->raid_level
!= 0) ||
7972 (migr_map
->raid_level
== map
->raid_level
))
7975 if (u
->new_disks
[0] >= 0) {
7978 new_disk
= get_disk_super(super
,
7979 major(u
->new_disks
[0]),
7980 minor(u
->new_disks
[0]));
7981 dprintf("imsm: new disk for reshape is: %i:%i "
7982 "(%p, index = %i)\n",
7983 major(u
->new_disks
[0]),
7984 minor(u
->new_disks
[0]),
7985 new_disk
, new_disk
->index
);
7986 if (new_disk
== NULL
)
7987 goto error_disk_add
;
7989 new_disk
->index
= map
->num_members
- 1;
7990 /* slot to fill in autolayout
7992 new_disk
->raiddisk
= new_disk
->index
;
7993 new_disk
->disk
.status
|= CONFIGURED_DISK
;
7994 new_disk
->disk
.status
&= ~SPARE_DISK
;
7996 goto error_disk_add
;
7999 *tofree
= *space_list
;
8000 /* calculate new size
8002 imsm_set_array_size(new_dev
, -1);
8009 *space_list
= tofree
;
8013 dprintf("Error: imsm: Cannot find disk.\n");
8017 static int apply_size_change_update(struct imsm_update_size_change
*u
,
8018 struct intel_super
*super
)
8020 struct intel_dev
*id
;
8023 dprintf("apply_size_change_update()\n");
8024 if ((u
->subdev
< 0) ||
8026 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
8030 for (id
= super
->devlist
; id
; id
= id
->next
) {
8031 if (id
->index
== (unsigned)u
->subdev
) {
8032 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
8033 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8034 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
8035 unsigned long long blocks_per_member
;
8037 /* calculate new size
8039 blocks_per_member
= u
->new_size
/ used_disks
;
8040 dprintf("imsm: apply_size_change_update(size: %llu, "
8041 "blocks per member: %llu)\n",
8042 u
->new_size
, blocks_per_member
);
8043 set_blocks_per_member(map
, blocks_per_member
);
8044 imsm_set_array_size(dev
, u
->new_size
);
8055 static int apply_update_activate_spare(struct imsm_update_activate_spare
*u
,
8056 struct intel_super
*super
,
8057 struct active_array
*active_array
)
8059 struct imsm_super
*mpb
= super
->anchor
;
8060 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
8061 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8062 struct imsm_map
*migr_map
;
8063 struct active_array
*a
;
8064 struct imsm_disk
*disk
;
8071 int second_map_created
= 0;
8073 for (; u
; u
= u
->next
) {
8074 victim
= get_imsm_disk_idx(dev
, u
->slot
, MAP_X
);
8079 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
8084 fprintf(stderr
, "error: imsm_activate_spare passed "
8085 "an unknown disk (index: %d)\n",
8090 /* count failures (excluding rebuilds and the victim)
8091 * to determine map[0] state
8094 for (i
= 0; i
< map
->num_members
; i
++) {
8097 disk
= get_imsm_disk(super
,
8098 get_imsm_disk_idx(dev
, i
, MAP_X
));
8099 if (!disk
|| is_failed(disk
))
8103 /* adding a pristine spare, assign a new index */
8104 if (dl
->index
< 0) {
8105 dl
->index
= super
->anchor
->num_disks
;
8106 super
->anchor
->num_disks
++;
8109 disk
->status
|= CONFIGURED_DISK
;
8110 disk
->status
&= ~SPARE_DISK
;
8113 to_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8114 if (!second_map_created
) {
8115 second_map_created
= 1;
8116 map
->map_state
= IMSM_T_STATE_DEGRADED
;
8117 migrate(dev
, super
, to_state
, MIGR_REBUILD
);
8119 map
->map_state
= to_state
;
8120 migr_map
= get_imsm_map(dev
, MAP_1
);
8121 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
8122 set_imsm_ord_tbl_ent(migr_map
, u
->slot
,
8123 dl
->index
| IMSM_ORD_REBUILD
);
8125 /* update the family_num to mark a new container
8126 * generation, being careful to record the existing
8127 * family_num in orig_family_num to clean up after
8128 * earlier mdadm versions that neglected to set it.
8130 if (mpb
->orig_family_num
== 0)
8131 mpb
->orig_family_num
= mpb
->family_num
;
8132 mpb
->family_num
+= super
->random
;
8134 /* count arrays using the victim in the metadata */
8136 for (a
= active_array
; a
; a
= a
->next
) {
8137 dev
= get_imsm_dev(super
, a
->info
.container_member
);
8138 map
= get_imsm_map(dev
, MAP_0
);
8140 if (get_imsm_disk_slot(map
, victim
) >= 0)
8144 /* delete the victim if it is no longer being
8150 /* We know that 'manager' isn't touching anything,
8151 * so it is safe to delete
8153 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
8154 if ((*dlp
)->index
== victim
)
8157 /* victim may be on the missing list */
8159 for (dlp
= &super
->missing
; *dlp
;
8160 dlp
= &(*dlp
)->next
)
8161 if ((*dlp
)->index
== victim
)
8163 imsm_delete(super
, dlp
, victim
);
8170 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
8171 struct intel_super
*super
,
8174 struct dl
*new_disk
;
8175 struct intel_dev
*id
;
8177 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
8178 int disk_count
= u
->old_raid_disks
;
8179 void **tofree
= NULL
;
8180 int devices_to_reshape
= 1;
8181 struct imsm_super
*mpb
= super
->anchor
;
8183 unsigned int dev_id
;
8185 dprintf("imsm: apply_reshape_container_disks_update()\n");
8187 /* enable spares to use in array */
8188 for (i
= 0; i
< delta_disks
; i
++) {
8189 new_disk
= get_disk_super(super
,
8190 major(u
->new_disks
[i
]),
8191 minor(u
->new_disks
[i
]));
8192 dprintf("imsm: new disk for reshape is: %i:%i "
8193 "(%p, index = %i)\n",
8194 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
8195 new_disk
, new_disk
->index
);
8196 if ((new_disk
== NULL
) ||
8197 ((new_disk
->index
>= 0) &&
8198 (new_disk
->index
< u
->old_raid_disks
)))
8199 goto update_reshape_exit
;
8200 new_disk
->index
= disk_count
++;
8201 /* slot to fill in autolayout
8203 new_disk
->raiddisk
= new_disk
->index
;
8204 new_disk
->disk
.status
|=
8206 new_disk
->disk
.status
&= ~SPARE_DISK
;
8209 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
8210 mpb
->num_raid_devs
);
8211 /* manage changes in volume
8213 for (dev_id
= 0; dev_id
< mpb
->num_raid_devs
; dev_id
++) {
8214 void **sp
= *space_list
;
8215 struct imsm_dev
*newdev
;
8216 struct imsm_map
*newmap
, *oldmap
;
8218 for (id
= super
->devlist
; id
; id
= id
->next
) {
8219 if (id
->index
== dev_id
)
8228 /* Copy the dev, but not (all of) the map */
8229 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
8230 oldmap
= get_imsm_map(id
->dev
, MAP_0
);
8231 newmap
= get_imsm_map(newdev
, MAP_0
);
8232 /* Copy the current map */
8233 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8234 /* update one device only
8236 if (devices_to_reshape
) {
8237 dprintf("imsm: modifying subdev: %i\n",
8239 devices_to_reshape
--;
8240 newdev
->vol
.migr_state
= 1;
8241 newdev
->vol
.curr_migr_unit
= 0;
8242 set_migr_type(newdev
, MIGR_GEN_MIGR
);
8243 newmap
->num_members
= u
->new_raid_disks
;
8244 for (i
= 0; i
< delta_disks
; i
++) {
8245 set_imsm_ord_tbl_ent(newmap
,
8246 u
->old_raid_disks
+ i
,
8247 u
->old_raid_disks
+ i
);
8249 /* New map is correct, now need to save old map
8251 newmap
= get_imsm_map(newdev
, MAP_1
);
8252 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8254 imsm_set_array_size(newdev
, -1);
8257 sp
= (void **)id
->dev
;
8262 /* Clear migration record */
8263 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
8266 *space_list
= tofree
;
8269 update_reshape_exit
:
8274 static int apply_takeover_update(struct imsm_update_takeover
*u
,
8275 struct intel_super
*super
,
8278 struct imsm_dev
*dev
= NULL
;
8279 struct intel_dev
*dv
;
8280 struct imsm_dev
*dev_new
;
8281 struct imsm_map
*map
;
8285 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
8286 if (dv
->index
== (unsigned int)u
->subarray
) {
8294 map
= get_imsm_map(dev
, MAP_0
);
8296 if (u
->direction
== R10_TO_R0
) {
8297 /* Number of failed disks must be half of initial disk number */
8298 if (imsm_count_failed(super
, dev
, MAP_0
) !=
8299 (map
->num_members
/ 2))
8302 /* iterate through devices to mark removed disks as spare */
8303 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
8304 if (dm
->disk
.status
& FAILED_DISK
) {
8305 int idx
= dm
->index
;
8306 /* update indexes on the disk list */
8307 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
8308 the index values will end up being correct.... NB */
8309 for (du
= super
->disks
; du
; du
= du
->next
)
8310 if (du
->index
> idx
)
8312 /* mark as spare disk */
8317 map
->num_members
= map
->num_members
/ 2;
8318 map
->map_state
= IMSM_T_STATE_NORMAL
;
8319 map
->num_domains
= 1;
8320 map
->raid_level
= 0;
8321 map
->failed_disk_num
= -1;
8324 if (u
->direction
== R0_TO_R10
) {
8326 /* update slots in current disk list */
8327 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
8331 /* create new *missing* disks */
8332 for (i
= 0; i
< map
->num_members
; i
++) {
8333 space
= *space_list
;
8336 *space_list
= *space
;
8338 memcpy(du
, super
->disks
, sizeof(*du
));
8342 du
->index
= (i
* 2) + 1;
8343 sprintf((char *)du
->disk
.serial
,
8344 " MISSING_%d", du
->index
);
8345 sprintf((char *)du
->serial
,
8346 "MISSING_%d", du
->index
);
8347 du
->next
= super
->missing
;
8348 super
->missing
= du
;
8350 /* create new dev and map */
8351 space
= *space_list
;
8354 *space_list
= *space
;
8355 dev_new
= (void *)space
;
8356 memcpy(dev_new
, dev
, sizeof(*dev
));
8357 /* update new map */
8358 map
= get_imsm_map(dev_new
, MAP_0
);
8359 map
->num_members
= map
->num_members
* 2;
8360 map
->map_state
= IMSM_T_STATE_DEGRADED
;
8361 map
->num_domains
= 2;
8362 map
->raid_level
= 1;
8363 /* replace dev<->dev_new */
8366 /* update disk order table */
8367 for (du
= super
->disks
; du
; du
= du
->next
)
8369 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
8370 for (du
= super
->missing
; du
; du
= du
->next
)
8371 if (du
->index
>= 0) {
8372 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
8373 mark_missing(dv
->dev
, &du
->disk
, du
->index
);
8379 static void imsm_process_update(struct supertype
*st
,
8380 struct metadata_update
*update
)
8383 * crack open the metadata_update envelope to find the update record
8384 * update can be one of:
8385 * update_reshape_container_disks - all the arrays in the container
8386 * are being reshaped to have more devices. We need to mark
8387 * the arrays for general migration and convert selected spares
8388 * into active devices.
8389 * update_activate_spare - a spare device has replaced a failed
8390 * device in an array, update the disk_ord_tbl. If this disk is
8391 * present in all member arrays then also clear the SPARE_DISK
8393 * update_create_array
8395 * update_rename_array
8396 * update_add_remove_disk
8398 struct intel_super
*super
= st
->sb
;
8399 struct imsm_super
*mpb
;
8400 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
8402 /* update requires a larger buf but the allocation failed */
8403 if (super
->next_len
&& !super
->next_buf
) {
8404 super
->next_len
= 0;
8408 if (super
->next_buf
) {
8409 memcpy(super
->next_buf
, super
->buf
, super
->len
);
8411 super
->len
= super
->next_len
;
8412 super
->buf
= super
->next_buf
;
8414 super
->next_len
= 0;
8415 super
->next_buf
= NULL
;
8418 mpb
= super
->anchor
;
8421 case update_general_migration_checkpoint
: {
8422 struct intel_dev
*id
;
8423 struct imsm_update_general_migration_checkpoint
*u
=
8424 (void *)update
->buf
;
8426 dprintf("imsm: process_update() "
8427 "for update_general_migration_checkpoint called\n");
8429 /* find device under general migration */
8430 for (id
= super
->devlist
; id
; id
= id
->next
) {
8431 if (is_gen_migration(id
->dev
)) {
8432 id
->dev
->vol
.curr_migr_unit
=
8433 __cpu_to_le32(u
->curr_migr_unit
);
8434 super
->updates_pending
++;
8439 case update_takeover
: {
8440 struct imsm_update_takeover
*u
= (void *)update
->buf
;
8441 if (apply_takeover_update(u
, super
, &update
->space_list
)) {
8442 imsm_update_version_info(super
);
8443 super
->updates_pending
++;
8448 case update_reshape_container_disks
: {
8449 struct imsm_update_reshape
*u
= (void *)update
->buf
;
8450 if (apply_reshape_container_disks_update(
8451 u
, super
, &update
->space_list
))
8452 super
->updates_pending
++;
8455 case update_reshape_migration
: {
8456 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
8457 if (apply_reshape_migration_update(
8458 u
, super
, &update
->space_list
))
8459 super
->updates_pending
++;
8462 case update_size_change
: {
8463 struct imsm_update_size_change
*u
= (void *)update
->buf
;
8464 if (apply_size_change_update(u
, super
))
8465 super
->updates_pending
++;
8468 case update_activate_spare
: {
8469 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
8470 if (apply_update_activate_spare(u
, super
, st
->arrays
))
8471 super
->updates_pending
++;
8474 case update_create_array
: {
8475 /* someone wants to create a new array, we need to be aware of
8476 * a few races/collisions:
8477 * 1/ 'Create' called by two separate instances of mdadm
8478 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
8479 * devices that have since been assimilated via
8481 * In the event this update can not be carried out mdadm will
8482 * (FIX ME) notice that its update did not take hold.
8484 struct imsm_update_create_array
*u
= (void *) update
->buf
;
8485 struct intel_dev
*dv
;
8486 struct imsm_dev
*dev
;
8487 struct imsm_map
*map
, *new_map
;
8488 unsigned long long start
, end
;
8489 unsigned long long new_start
, new_end
;
8491 struct disk_info
*inf
;
8494 /* handle racing creates: first come first serve */
8495 if (u
->dev_idx
< mpb
->num_raid_devs
) {
8496 dprintf("%s: subarray %d already defined\n",
8497 __func__
, u
->dev_idx
);
8501 /* check update is next in sequence */
8502 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
8503 dprintf("%s: can not create array %d expected index %d\n",
8504 __func__
, u
->dev_idx
, mpb
->num_raid_devs
);
8508 new_map
= get_imsm_map(&u
->dev
, MAP_0
);
8509 new_start
= pba_of_lba0(new_map
);
8510 new_end
= new_start
+ blocks_per_member(new_map
);
8511 inf
= get_disk_info(u
);
8513 /* handle activate_spare versus create race:
8514 * check to make sure that overlapping arrays do not include
8517 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8518 dev
= get_imsm_dev(super
, i
);
8519 map
= get_imsm_map(dev
, MAP_0
);
8520 start
= pba_of_lba0(map
);
8521 end
= start
+ blocks_per_member(map
);
8522 if ((new_start
>= start
&& new_start
<= end
) ||
8523 (start
>= new_start
&& start
<= new_end
))
8528 if (disks_overlap(super
, i
, u
)) {
8529 dprintf("%s: arrays overlap\n", __func__
);
8534 /* check that prepare update was successful */
8535 if (!update
->space
) {
8536 dprintf("%s: prepare update failed\n", __func__
);
8540 /* check that all disks are still active before committing
8541 * changes. FIXME: could we instead handle this by creating a
8542 * degraded array? That's probably not what the user expects,
8543 * so better to drop this update on the floor.
8545 for (i
= 0; i
< new_map
->num_members
; i
++) {
8546 dl
= serial_to_dl(inf
[i
].serial
, super
);
8548 dprintf("%s: disk disappeared\n", __func__
);
8553 super
->updates_pending
++;
8555 /* convert spares to members and fixup ord_tbl */
8556 for (i
= 0; i
< new_map
->num_members
; i
++) {
8557 dl
= serial_to_dl(inf
[i
].serial
, super
);
8558 if (dl
->index
== -1) {
8559 dl
->index
= mpb
->num_disks
;
8561 dl
->disk
.status
|= CONFIGURED_DISK
;
8562 dl
->disk
.status
&= ~SPARE_DISK
;
8564 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
8569 update
->space
= NULL
;
8570 imsm_copy_dev(dev
, &u
->dev
);
8571 dv
->index
= u
->dev_idx
;
8572 dv
->next
= super
->devlist
;
8573 super
->devlist
= dv
;
8574 mpb
->num_raid_devs
++;
8576 imsm_update_version_info(super
);
8579 /* mdmon knows how to release update->space, but not
8580 * ((struct intel_dev *) update->space)->dev
8582 if (update
->space
) {
8588 case update_kill_array
: {
8589 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
8590 int victim
= u
->dev_idx
;
8591 struct active_array
*a
;
8592 struct intel_dev
**dp
;
8593 struct imsm_dev
*dev
;
8595 /* sanity check that we are not affecting the uuid of
8596 * active arrays, or deleting an active array
8598 * FIXME when immutable ids are available, but note that
8599 * we'll also need to fixup the invalidated/active
8600 * subarray indexes in mdstat
8602 for (a
= st
->arrays
; a
; a
= a
->next
)
8603 if (a
->info
.container_member
>= victim
)
8605 /* by definition if mdmon is running at least one array
8606 * is active in the container, so checking
8607 * mpb->num_raid_devs is just extra paranoia
8609 dev
= get_imsm_dev(super
, victim
);
8610 if (a
|| !dev
|| mpb
->num_raid_devs
== 1) {
8611 dprintf("failed to delete subarray-%d\n", victim
);
8615 for (dp
= &super
->devlist
; *dp
;)
8616 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
8619 if ((*dp
)->index
> (unsigned)victim
)
8623 mpb
->num_raid_devs
--;
8624 super
->updates_pending
++;
8627 case update_rename_array
: {
8628 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
8629 char name
[MAX_RAID_SERIAL_LEN
+1];
8630 int target
= u
->dev_idx
;
8631 struct active_array
*a
;
8632 struct imsm_dev
*dev
;
8634 /* sanity check that we are not affecting the uuid of
8637 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
8638 name
[MAX_RAID_SERIAL_LEN
] = '\0';
8639 for (a
= st
->arrays
; a
; a
= a
->next
)
8640 if (a
->info
.container_member
== target
)
8642 dev
= get_imsm_dev(super
, u
->dev_idx
);
8643 if (a
|| !dev
|| !check_name(super
, name
, 1)) {
8644 dprintf("failed to rename subarray-%d\n", target
);
8648 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
8649 super
->updates_pending
++;
8652 case update_add_remove_disk
: {
8653 /* we may be able to repair some arrays if disks are
8654 * being added, check teh status of add_remove_disk
8655 * if discs has been added.
8657 if (add_remove_disk_update(super
)) {
8658 struct active_array
*a
;
8660 super
->updates_pending
++;
8661 for (a
= st
->arrays
; a
; a
= a
->next
)
8662 a
->check_degraded
= 1;
8667 fprintf(stderr
, "error: unsuported process update type:"
8668 "(type: %d)\n", type
);
8672 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
);
8674 static void imsm_prepare_update(struct supertype
*st
,
8675 struct metadata_update
*update
)
8678 * Allocate space to hold new disk entries, raid-device entries or a new
8679 * mpb if necessary. The manager synchronously waits for updates to
8680 * complete in the monitor, so new mpb buffers allocated here can be
8681 * integrated by the monitor thread without worrying about live pointers
8682 * in the manager thread.
8684 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
8685 struct intel_super
*super
= st
->sb
;
8686 struct imsm_super
*mpb
= super
->anchor
;
8691 case update_general_migration_checkpoint
:
8692 dprintf("imsm: prepare_update() "
8693 "for update_general_migration_checkpoint called\n");
8695 case update_takeover
: {
8696 struct imsm_update_takeover
*u
= (void *)update
->buf
;
8697 if (u
->direction
== R0_TO_R10
) {
8698 void **tail
= (void **)&update
->space_list
;
8699 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subarray
);
8700 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8701 int num_members
= map
->num_members
;
8705 /* allocate memory for added disks */
8706 for (i
= 0; i
< num_members
; i
++) {
8707 size
= sizeof(struct dl
);
8708 space
= malloc(size
);
8717 /* allocate memory for new device */
8718 size
= sizeof_imsm_dev(super
->devlist
->dev
, 0) +
8719 (num_members
* sizeof(__u32
));
8720 space
= malloc(size
);
8729 len
= disks_to_mpb_size(num_members
* 2);
8731 /* if allocation didn't success, free buffer */
8732 while (update
->space_list
) {
8733 void **sp
= update
->space_list
;
8734 update
->space_list
= *sp
;
8742 case update_reshape_container_disks
: {
8743 /* Every raid device in the container is about to
8744 * gain some more devices, and we will enter a
8746 * So each 'imsm_map' will be bigger, and the imsm_vol
8747 * will now hold 2 of them.
8748 * Thus we need new 'struct imsm_dev' allocations sized
8749 * as sizeof_imsm_dev but with more devices in both maps.
8751 struct imsm_update_reshape
*u
= (void *)update
->buf
;
8752 struct intel_dev
*dl
;
8753 void **space_tail
= (void**)&update
->space_list
;
8755 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
8757 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
8758 int size
= sizeof_imsm_dev(dl
->dev
, 1);
8760 if (u
->new_raid_disks
> u
->old_raid_disks
)
8761 size
+= sizeof(__u32
)*2*
8762 (u
->new_raid_disks
- u
->old_raid_disks
);
8771 len
= disks_to_mpb_size(u
->new_raid_disks
);
8772 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
8775 case update_reshape_migration
: {
8776 /* for migration level 0->5 we need to add disks
8777 * so the same as for container operation we will copy
8778 * device to the bigger location.
8779 * in memory prepared device and new disk area are prepared
8780 * for usage in process update
8782 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
8783 struct intel_dev
*id
;
8784 void **space_tail
= (void **)&update
->space_list
;
8787 int current_level
= -1;
8789 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
8791 /* add space for bigger array in update
8793 for (id
= super
->devlist
; id
; id
= id
->next
) {
8794 if (id
->index
== (unsigned)u
->subdev
) {
8795 size
= sizeof_imsm_dev(id
->dev
, 1);
8796 if (u
->new_raid_disks
> u
->old_raid_disks
)
8797 size
+= sizeof(__u32
)*2*
8798 (u
->new_raid_disks
- u
->old_raid_disks
);
8808 if (update
->space_list
== NULL
)
8811 /* add space for disk in update
8813 size
= sizeof(struct dl
);
8816 free(update
->space_list
);
8817 update
->space_list
= NULL
;
8824 /* add spare device to update
8826 for (id
= super
->devlist
; id
; id
= id
->next
)
8827 if (id
->index
== (unsigned)u
->subdev
) {
8828 struct imsm_dev
*dev
;
8829 struct imsm_map
*map
;
8831 dev
= get_imsm_dev(super
, u
->subdev
);
8832 map
= get_imsm_map(dev
, MAP_0
);
8833 current_level
= map
->raid_level
;
8836 if ((u
->new_level
== 5) && (u
->new_level
!= current_level
)) {
8837 struct mdinfo
*spares
;
8839 spares
= get_spares_for_grow(st
);
8847 makedev(dev
->disk
.major
,
8849 dl
= get_disk_super(super
,
8852 dl
->index
= u
->old_raid_disks
;
8858 len
= disks_to_mpb_size(u
->new_raid_disks
);
8859 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
8862 case update_size_change
: {
8865 case update_create_array
: {
8866 struct imsm_update_create_array
*u
= (void *) update
->buf
;
8867 struct intel_dev
*dv
;
8868 struct imsm_dev
*dev
= &u
->dev
;
8869 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8871 struct disk_info
*inf
;
8875 inf
= get_disk_info(u
);
8876 len
= sizeof_imsm_dev(dev
, 1);
8877 /* allocate a new super->devlist entry */
8878 dv
= malloc(sizeof(*dv
));
8880 dv
->dev
= malloc(len
);
8885 update
->space
= NULL
;
8889 /* count how many spares will be converted to members */
8890 for (i
= 0; i
< map
->num_members
; i
++) {
8891 dl
= serial_to_dl(inf
[i
].serial
, super
);
8893 /* hmm maybe it failed?, nothing we can do about
8898 if (count_memberships(dl
, super
) == 0)
8901 len
+= activate
* sizeof(struct imsm_disk
);
8908 /* check if we need a larger metadata buffer */
8909 if (super
->next_buf
)
8910 buf_len
= super
->next_len
;
8912 buf_len
= super
->len
;
8914 if (__le32_to_cpu(mpb
->mpb_size
) + len
> buf_len
) {
8915 /* ok we need a larger buf than what is currently allocated
8916 * if this allocation fails process_update will notice that
8917 * ->next_len is set and ->next_buf is NULL
8919 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + len
, 512);
8920 if (super
->next_buf
)
8921 free(super
->next_buf
);
8923 super
->next_len
= buf_len
;
8924 if (posix_memalign(&super
->next_buf
, 512, buf_len
) == 0)
8925 memset(super
->next_buf
, 0, buf_len
);
8927 super
->next_buf
= NULL
;
8931 /* must be called while manager is quiesced */
8932 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
8934 struct imsm_super
*mpb
= super
->anchor
;
8936 struct imsm_dev
*dev
;
8937 struct imsm_map
*map
;
8938 int i
, j
, num_members
;
8941 dprintf("%s: deleting device[%d] from imsm_super\n",
8944 /* shift all indexes down one */
8945 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
8946 if (iter
->index
> (int)index
)
8948 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
8949 if (iter
->index
> (int)index
)
8952 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8953 dev
= get_imsm_dev(super
, i
);
8954 map
= get_imsm_map(dev
, MAP_0
);
8955 num_members
= map
->num_members
;
8956 for (j
= 0; j
< num_members
; j
++) {
8957 /* update ord entries being careful not to propagate
8958 * ord-flags to the first map
8960 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_X
);
8962 if (ord_to_idx(ord
) <= index
)
8965 map
= get_imsm_map(dev
, MAP_0
);
8966 set_imsm_ord_tbl_ent(map
, j
, ord_to_idx(ord
- 1));
8967 map
= get_imsm_map(dev
, MAP_1
);
8969 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
8974 super
->updates_pending
++;
8976 struct dl
*dl
= *dlp
;
8978 *dlp
= (*dlp
)->next
;
8979 __free_imsm_disk(dl
);
8982 #endif /* MDASSEMBLE */
8984 static void close_targets(int *targets
, int new_disks
)
8991 for (i
= 0; i
< new_disks
; i
++) {
8992 if (targets
[i
] >= 0) {
8999 static int imsm_get_allowed_degradation(int level
, int raid_disks
,
9000 struct intel_super
*super
,
9001 struct imsm_dev
*dev
)
9007 struct imsm_map
*map
;
9010 ret_val
= raid_disks
/2;
9011 /* check map if all disks pairs not failed
9014 map
= get_imsm_map(dev
, MAP_0
);
9015 for (i
= 0; i
< ret_val
; i
++) {
9016 int degradation
= 0;
9017 if (get_imsm_disk(super
, i
) == NULL
)
9019 if (get_imsm_disk(super
, i
+ 1) == NULL
)
9021 if (degradation
== 2)
9024 map
= get_imsm_map(dev
, MAP_1
);
9025 /* if there is no second map
9026 * result can be returned
9030 /* check degradation in second map
9032 for (i
= 0; i
< ret_val
; i
++) {
9033 int degradation
= 0;
9034 if (get_imsm_disk(super
, i
) == NULL
)
9036 if (get_imsm_disk(super
, i
+ 1) == NULL
)
9038 if (degradation
== 2)
9053 /*******************************************************************************
9054 * Function: open_backup_targets
9055 * Description: Function opens file descriptors for all devices given in
9058 * info : general array info
9059 * raid_disks : number of disks
9060 * raid_fds : table of device's file descriptors
9061 * super : intel super for raid10 degradation check
9062 * dev : intel device for raid10 degradation check
9066 ******************************************************************************/
9067 int open_backup_targets(struct mdinfo
*info
, int raid_disks
, int *raid_fds
,
9068 struct intel_super
*super
, struct imsm_dev
*dev
)
9074 for (i
= 0; i
< raid_disks
; i
++)
9077 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
9080 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
)) {
9081 dprintf("disk is faulty!!\n");
9085 if ((sd
->disk
.raid_disk
>= raid_disks
) ||
9086 (sd
->disk
.raid_disk
< 0))
9089 dn
= map_dev(sd
->disk
.major
,
9091 raid_fds
[sd
->disk
.raid_disk
] = dev_open(dn
, O_RDWR
);
9092 if (raid_fds
[sd
->disk
.raid_disk
] < 0) {
9093 fprintf(stderr
, "cannot open component\n");
9098 /* check if maximum array degradation level is not exceeded
9100 if ((raid_disks
- opened
) >
9101 imsm_get_allowed_degradation(info
->new_level
,
9104 fprintf(stderr
, "Not enough disks can be opened.\n");
9105 close_targets(raid_fds
, raid_disks
);
9112 /*******************************************************************************
9113 * Function: init_migr_record_imsm
9114 * Description: Function inits imsm migration record
9116 * super : imsm internal array info
9117 * dev : device under migration
9118 * info : general array info to find the smallest device
9121 ******************************************************************************/
9122 void init_migr_record_imsm(struct supertype
*st
, struct imsm_dev
*dev
,
9123 struct mdinfo
*info
)
9125 struct intel_super
*super
= st
->sb
;
9126 struct migr_record
*migr_rec
= super
->migr_rec
;
9128 unsigned long long dsize
, dev_sectors
;
9129 long long unsigned min_dev_sectors
= -1LLU;
9133 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
9134 struct imsm_map
*map_src
= get_imsm_map(dev
, MAP_1
);
9135 unsigned long long num_migr_units
;
9136 unsigned long long array_blocks
;
9138 memset(migr_rec
, 0, sizeof(struct migr_record
));
9139 migr_rec
->family_num
= __cpu_to_le32(super
->anchor
->family_num
);
9141 /* only ascending reshape supported now */
9142 migr_rec
->ascending_migr
= __cpu_to_le32(1);
9144 migr_rec
->dest_depth_per_unit
= GEN_MIGR_AREA_SIZE
/
9145 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
9146 migr_rec
->dest_depth_per_unit
*=
9147 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
9148 new_data_disks
= imsm_num_data_members(dev
, MAP_0
);
9149 migr_rec
->blocks_per_unit
=
9150 __cpu_to_le32(migr_rec
->dest_depth_per_unit
* new_data_disks
);
9151 migr_rec
->dest_depth_per_unit
=
9152 __cpu_to_le32(migr_rec
->dest_depth_per_unit
);
9153 array_blocks
= info
->component_size
* new_data_disks
;
9155 array_blocks
/ __le32_to_cpu(migr_rec
->blocks_per_unit
);
9157 if (array_blocks
% __le32_to_cpu(migr_rec
->blocks_per_unit
))
9159 migr_rec
->num_migr_units
= __cpu_to_le32(num_migr_units
);
9161 migr_rec
->post_migr_vol_cap
= dev
->size_low
;
9162 migr_rec
->post_migr_vol_cap_hi
= dev
->size_high
;
9165 /* Find the smallest dev */
9166 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
9167 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
9168 fd
= dev_open(nm
, O_RDONLY
);
9171 get_dev_size(fd
, NULL
, &dsize
);
9172 dev_sectors
= dsize
/ 512;
9173 if (dev_sectors
< min_dev_sectors
)
9174 min_dev_sectors
= dev_sectors
;
9177 migr_rec
->ckpt_area_pba
= __cpu_to_le32(min_dev_sectors
-
9178 RAID_DISK_RESERVED_BLOCKS_IMSM_HI
);
9180 write_imsm_migr_rec(st
);
9185 /*******************************************************************************
9186 * Function: save_backup_imsm
9187 * Description: Function saves critical data stripes to Migration Copy Area
9188 * and updates the current migration unit status.
9189 * Use restore_stripes() to form a destination stripe,
9190 * and to write it to the Copy Area.
9192 * st : supertype information
9193 * dev : imsm device that backup is saved for
9194 * info : general array info
9195 * buf : input buffer
9196 * length : length of data to backup (blocks_per_unit)
9200 ******************************************************************************/
9201 int save_backup_imsm(struct supertype
*st
,
9202 struct imsm_dev
*dev
,
9203 struct mdinfo
*info
,
9208 struct intel_super
*super
= st
->sb
;
9209 unsigned long long *target_offsets
= NULL
;
9210 int *targets
= NULL
;
9212 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
9213 int new_disks
= map_dest
->num_members
;
9214 int dest_layout
= 0;
9216 unsigned long long start
;
9217 int data_disks
= imsm_num_data_members(dev
, MAP_0
);
9219 targets
= malloc(new_disks
* sizeof(int));
9223 for (i
= 0; i
< new_disks
; i
++)
9226 target_offsets
= malloc(new_disks
* sizeof(unsigned long long));
9227 if (!target_offsets
)
9230 start
= info
->reshape_progress
* 512;
9231 for (i
= 0; i
< new_disks
; i
++) {
9232 target_offsets
[i
] = (unsigned long long)
9233 __le32_to_cpu(super
->migr_rec
->ckpt_area_pba
) * 512;
9234 /* move back copy area adderss, it will be moved forward
9235 * in restore_stripes() using start input variable
9237 target_offsets
[i
] -= start
/data_disks
;
9240 if (open_backup_targets(info
, new_disks
, targets
,
9244 dest_layout
= imsm_level_to_layout(map_dest
->raid_level
);
9245 dest_chunk
= __le16_to_cpu(map_dest
->blocks_per_strip
) * 512;
9247 if (restore_stripes(targets
, /* list of dest devices */
9248 target_offsets
, /* migration record offsets */
9251 map_dest
->raid_level
,
9253 -1, /* source backup file descriptor */
9254 0, /* input buf offset
9255 * always 0 buf is already offseted */
9259 fprintf(stderr
, Name
": Error restoring stripes\n");
9267 close_targets(targets
, new_disks
);
9270 free(target_offsets
);
9275 /*******************************************************************************
9276 * Function: save_checkpoint_imsm
9277 * Description: Function called for current unit status update
9278 * in the migration record. It writes it to disk.
9280 * super : imsm internal array info
9281 * info : general array info
9285 * 2: failure, means no valid migration record
9286 * / no general migration in progress /
9287 ******************************************************************************/
9288 int save_checkpoint_imsm(struct supertype
*st
, struct mdinfo
*info
, int state
)
9290 struct intel_super
*super
= st
->sb
;
9291 unsigned long long blocks_per_unit
;
9292 unsigned long long curr_migr_unit
;
9294 if (load_imsm_migr_rec(super
, info
) != 0) {
9295 dprintf("imsm: ERROR: Cannot read migration record "
9296 "for checkpoint save.\n");
9300 blocks_per_unit
= __le32_to_cpu(super
->migr_rec
->blocks_per_unit
);
9301 if (blocks_per_unit
== 0) {
9302 dprintf("imsm: no migration in progress.\n");
9305 curr_migr_unit
= info
->reshape_progress
/ blocks_per_unit
;
9306 /* check if array is alligned to copy area
9307 * if it is not alligned, add one to current migration unit value
9308 * this can happend on array reshape finish only
9310 if (info
->reshape_progress
% blocks_per_unit
)
9313 super
->migr_rec
->curr_migr_unit
=
9314 __cpu_to_le32(curr_migr_unit
);
9315 super
->migr_rec
->rec_status
= __cpu_to_le32(state
);
9316 super
->migr_rec
->dest_1st_member_lba
=
9317 __cpu_to_le32(curr_migr_unit
*
9318 __le32_to_cpu(super
->migr_rec
->dest_depth_per_unit
));
9319 if (write_imsm_migr_rec(st
) < 0) {
9320 dprintf("imsm: Cannot write migration record "
9321 "outside backup area\n");
9328 /*******************************************************************************
9329 * Function: recover_backup_imsm
9330 * Description: Function recovers critical data from the Migration Copy Area
9331 * while assembling an array.
9333 * super : imsm internal array info
9334 * info : general array info
9336 * 0 : success (or there is no data to recover)
9338 ******************************************************************************/
9339 int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
)
9341 struct intel_super
*super
= st
->sb
;
9342 struct migr_record
*migr_rec
= super
->migr_rec
;
9343 struct imsm_map
*map_dest
= NULL
;
9344 struct intel_dev
*id
= NULL
;
9345 unsigned long long read_offset
;
9346 unsigned long long write_offset
;
9348 int *targets
= NULL
;
9349 int new_disks
, i
, err
;
9352 unsigned long curr_migr_unit
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
9353 unsigned long num_migr_units
= __le32_to_cpu(migr_rec
->num_migr_units
);
9355 int skipped_disks
= 0;
9357 err
= sysfs_get_str(info
, NULL
, "array_state", (char *)buffer
, 20);
9361 /* recover data only during assemblation */
9362 if (strncmp(buffer
, "inactive", 8) != 0)
9364 /* no data to recover */
9365 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
9367 if (curr_migr_unit
>= num_migr_units
)
9370 /* find device during reshape */
9371 for (id
= super
->devlist
; id
; id
= id
->next
)
9372 if (is_gen_migration(id
->dev
))
9377 map_dest
= get_imsm_map(id
->dev
, MAP_0
);
9378 new_disks
= map_dest
->num_members
;
9380 read_offset
= (unsigned long long)
9381 __le32_to_cpu(migr_rec
->ckpt_area_pba
) * 512;
9383 write_offset
= ((unsigned long long)
9384 __le32_to_cpu(migr_rec
->dest_1st_member_lba
) +
9385 pba_of_lba0(map_dest
)) * 512;
9387 unit_len
= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
9388 if (posix_memalign((void **)&buf
, 512, unit_len
) != 0)
9390 targets
= malloc(new_disks
* sizeof(int));
9394 if (open_backup_targets(info
, new_disks
, targets
, super
, id
->dev
)) {
9396 Name
": Cannot open some devices belonging to array.\n");
9400 for (i
= 0; i
< new_disks
; i
++) {
9401 if (targets
[i
] < 0) {
9405 if (lseek64(targets
[i
], read_offset
, SEEK_SET
) < 0) {
9407 Name
": Cannot seek to block: %s\n",
9412 if ((unsigned)read(targets
[i
], buf
, unit_len
) != unit_len
) {
9414 Name
": Cannot read copy area block: %s\n",
9419 if (lseek64(targets
[i
], write_offset
, SEEK_SET
) < 0) {
9421 Name
": Cannot seek to block: %s\n",
9426 if ((unsigned)write(targets
[i
], buf
, unit_len
) != unit_len
) {
9428 Name
": Cannot restore block: %s\n",
9435 if (skipped_disks
> imsm_get_allowed_degradation(info
->new_level
,
9440 Name
": Cannot restore data from backup."
9441 " Too many failed disks\n");
9445 if (save_checkpoint_imsm(st
, info
, UNIT_SRC_NORMAL
)) {
9446 /* ignore error == 2, this can mean end of reshape here
9448 dprintf("imsm: Cannot write checkpoint to "
9449 "migration record (UNIT_SRC_NORMAL) during restart\n");
9455 for (i
= 0; i
< new_disks
; i
++)
9464 static char disk_by_path
[] = "/dev/disk/by-path/";
9466 static const char *imsm_get_disk_controller_domain(const char *path
)
9468 char disk_path
[PATH_MAX
];
9472 strncpy(disk_path
, disk_by_path
, PATH_MAX
- 1);
9473 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
9474 if (stat(disk_path
, &st
) == 0) {
9475 struct sys_dev
* hba
;
9478 path
= devt_to_devpath(st
.st_rdev
);
9481 hba
= find_disk_attached_hba(-1, path
);
9482 if (hba
&& hba
->type
== SYS_DEV_SAS
)
9484 else if (hba
&& hba
->type
== SYS_DEV_SATA
)
9488 dprintf("path: %s hba: %s attached: %s\n",
9489 path
, (hba
) ? hba
->path
: "NULL", drv
);
9497 static int imsm_find_array_minor_by_subdev(int subdev
, int container
, int *minor
)
9499 char subdev_name
[20];
9500 struct mdstat_ent
*mdstat
;
9502 sprintf(subdev_name
, "%d", subdev
);
9503 mdstat
= mdstat_by_subdev(subdev_name
, container
);
9507 *minor
= mdstat
->devnum
;
9508 free_mdstat(mdstat
);
9512 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
9513 struct geo_params
*geo
,
9514 int *old_raid_disks
,
9517 /* currently we only support increasing the number of devices
9518 * for a container. This increases the number of device for each
9519 * member array. They must all be RAID0 or RAID5.
9522 struct mdinfo
*info
, *member
;
9523 int devices_that_can_grow
= 0;
9525 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): "
9526 "st->devnum = (%i)\n",
9529 if (geo
->size
!= -1 ||
9530 geo
->level
!= UnSet
||
9531 geo
->layout
!= UnSet
||
9532 geo
->chunksize
!= 0 ||
9533 geo
->raid_disks
== UnSet
) {
9534 dprintf("imsm: Container operation is allowed for "
9535 "raid disks number change only.\n");
9539 if (direction
== ROLLBACK_METADATA_CHANGES
) {
9540 dprintf("imsm: Metadata changes rollback is not supported for "
9541 "container operation.\n");
9545 info
= container_content_imsm(st
, NULL
);
9546 for (member
= info
; member
; member
= member
->next
) {
9550 dprintf("imsm: checking device_num: %i\n",
9551 member
->container_member
);
9553 if (geo
->raid_disks
<= member
->array
.raid_disks
) {
9554 /* we work on container for Online Capacity Expansion
9555 * only so raid_disks has to grow
9557 dprintf("imsm: for container operation raid disks "
9558 "increase is required\n");
9562 if ((info
->array
.level
!= 0) &&
9563 (info
->array
.level
!= 5)) {
9564 /* we cannot use this container with other raid level
9566 dprintf("imsm: for container operation wrong"
9567 " raid level (%i) detected\n",
9571 /* check for platform support
9572 * for this raid level configuration
9574 struct intel_super
*super
= st
->sb
;
9575 if (!is_raid_level_supported(super
->orom
,
9576 member
->array
.level
,
9578 dprintf("platform does not support raid%d with"
9582 geo
->raid_disks
> 1 ? "s" : "");
9585 /* check if component size is aligned to chunk size
9587 if (info
->component_size
%
9588 (info
->array
.chunk_size
/512)) {
9589 dprintf("Component size is not aligned to "
9595 if (*old_raid_disks
&&
9596 info
->array
.raid_disks
!= *old_raid_disks
)
9598 *old_raid_disks
= info
->array
.raid_disks
;
9600 /* All raid5 and raid0 volumes in container
9601 * have to be ready for Online Capacity Expansion
9602 * so they need to be assembled. We have already
9603 * checked that no recovery etc is happening.
9605 result
= imsm_find_array_minor_by_subdev(member
->container_member
,
9609 dprintf("imsm: cannot find array\n");
9612 devices_that_can_grow
++;
9615 if (!member
&& devices_that_can_grow
)
9619 dprintf("\tContainer operation allowed\n");
9621 dprintf("\tError: %i\n", ret_val
);
9626 /* Function: get_spares_for_grow
9627 * Description: Allocates memory and creates list of spare devices
9628 * avaliable in container. Checks if spare drive size is acceptable.
9629 * Parameters: Pointer to the supertype structure
9630 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
9633 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
9635 unsigned long long min_size
= min_acceptable_spare_size_imsm(st
);
9636 return container_choose_spares(st
, min_size
, NULL
, NULL
, NULL
, 0);
9639 /******************************************************************************
9640 * function: imsm_create_metadata_update_for_reshape
9641 * Function creates update for whole IMSM container.
9643 ******************************************************************************/
9644 static int imsm_create_metadata_update_for_reshape(
9645 struct supertype
*st
,
9646 struct geo_params
*geo
,
9648 struct imsm_update_reshape
**updatep
)
9650 struct intel_super
*super
= st
->sb
;
9651 struct imsm_super
*mpb
= super
->anchor
;
9652 int update_memory_size
= 0;
9653 struct imsm_update_reshape
*u
= NULL
;
9654 struct mdinfo
*spares
= NULL
;
9656 int delta_disks
= 0;
9659 dprintf("imsm_update_metadata_for_reshape(enter) raid_disks = %i\n",
9662 delta_disks
= geo
->raid_disks
- old_raid_disks
;
9664 /* size of all update data without anchor */
9665 update_memory_size
= sizeof(struct imsm_update_reshape
);
9667 /* now add space for spare disks that we need to add. */
9668 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
9670 u
= calloc(1, update_memory_size
);
9673 "cannot get memory for imsm_update_reshape update\n");
9676 u
->type
= update_reshape_container_disks
;
9677 u
->old_raid_disks
= old_raid_disks
;
9678 u
->new_raid_disks
= geo
->raid_disks
;
9680 /* now get spare disks list
9682 spares
= get_spares_for_grow(st
);
9685 || delta_disks
> spares
->array
.spare_disks
) {
9686 fprintf(stderr
, Name
": imsm: ERROR: Cannot get spare devices "
9687 "for %s.\n", geo
->dev_name
);
9692 /* we have got spares
9693 * update disk list in imsm_disk list table in anchor
9695 dprintf("imsm: %i spares are available.\n\n",
9696 spares
->array
.spare_disks
);
9699 for (i
= 0; i
< delta_disks
; i
++) {
9704 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
9706 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
9707 dl
->index
= mpb
->num_disks
;
9717 dprintf("imsm: reshape update preparation :");
9718 if (i
== delta_disks
) {
9721 return update_memory_size
;
9724 dprintf(" Error\n");
9730 /******************************************************************************
9731 * function: imsm_create_metadata_update_for_size_change()
9732 * Creates update for IMSM array for array size change.
9734 ******************************************************************************/
9735 static int imsm_create_metadata_update_for_size_change(
9736 struct supertype
*st
,
9737 struct geo_params
*geo
,
9738 struct imsm_update_size_change
**updatep
)
9740 struct intel_super
*super
= st
->sb
;
9741 int update_memory_size
= 0;
9742 struct imsm_update_size_change
*u
= NULL
;
9744 dprintf("imsm_create_metadata_update_for_size_change(enter)"
9745 " New size = %llu\n", geo
->size
);
9747 /* size of all update data without anchor */
9748 update_memory_size
= sizeof(struct imsm_update_size_change
);
9750 u
= calloc(1, update_memory_size
);
9752 dprintf("error: cannot get memory for "
9753 "imsm_create_metadata_update_for_size_change\n");
9756 u
->type
= update_size_change
;
9757 u
->subdev
= super
->current_vol
;
9758 u
->new_size
= geo
->size
;
9760 dprintf("imsm: reshape update preparation : OK\n");
9763 return update_memory_size
;
9766 /******************************************************************************
9767 * function: imsm_create_metadata_update_for_migration()
9768 * Creates update for IMSM array.
9770 ******************************************************************************/
9771 static int imsm_create_metadata_update_for_migration(
9772 struct supertype
*st
,
9773 struct geo_params
*geo
,
9774 struct imsm_update_reshape_migration
**updatep
)
9776 struct intel_super
*super
= st
->sb
;
9777 int update_memory_size
= 0;
9778 struct imsm_update_reshape_migration
*u
= NULL
;
9779 struct imsm_dev
*dev
;
9780 int previous_level
= -1;
9782 dprintf("imsm_create_metadata_update_for_migration(enter)"
9783 " New Level = %i\n", geo
->level
);
9785 /* size of all update data without anchor */
9786 update_memory_size
= sizeof(struct imsm_update_reshape_migration
);
9788 u
= calloc(1, update_memory_size
);
9790 dprintf("error: cannot get memory for "
9791 "imsm_create_metadata_update_for_migration\n");
9794 u
->type
= update_reshape_migration
;
9795 u
->subdev
= super
->current_vol
;
9796 u
->new_level
= geo
->level
;
9797 u
->new_layout
= geo
->layout
;
9798 u
->new_raid_disks
= u
->old_raid_disks
= geo
->raid_disks
;
9799 u
->new_disks
[0] = -1;
9800 u
->new_chunksize
= -1;
9802 dev
= get_imsm_dev(super
, u
->subdev
);
9804 struct imsm_map
*map
;
9806 map
= get_imsm_map(dev
, MAP_0
);
9808 int current_chunk_size
=
9809 __le16_to_cpu(map
->blocks_per_strip
) / 2;
9811 if (geo
->chunksize
!= current_chunk_size
) {
9812 u
->new_chunksize
= geo
->chunksize
/ 1024;
9814 "chunk size change from %i to %i\n",
9815 current_chunk_size
, u
->new_chunksize
);
9817 previous_level
= map
->raid_level
;
9820 if ((geo
->level
== 5) && (previous_level
== 0)) {
9821 struct mdinfo
*spares
= NULL
;
9823 u
->new_raid_disks
++;
9824 spares
= get_spares_for_grow(st
);
9825 if ((spares
== NULL
) || (spares
->array
.spare_disks
< 1)) {
9828 update_memory_size
= 0;
9829 dprintf("error: cannot get spare device "
9830 "for requested migration");
9835 dprintf("imsm: reshape update preparation : OK\n");
9838 return update_memory_size
;
9841 static void imsm_update_metadata_locally(struct supertype
*st
,
9844 struct metadata_update mu
;
9849 mu
.space_list
= NULL
;
9851 imsm_prepare_update(st
, &mu
);
9852 imsm_process_update(st
, &mu
);
9854 while (mu
.space_list
) {
9855 void **space
= mu
.space_list
;
9856 mu
.space_list
= *space
;
9861 /***************************************************************************
9862 * Function: imsm_analyze_change
9863 * Description: Function analyze change for single volume
9864 * and validate if transition is supported
9865 * Parameters: Geometry parameters, supertype structure,
9866 * metadata change direction (apply/rollback)
9867 * Returns: Operation type code on success, -1 if fail
9868 ****************************************************************************/
9869 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
9870 struct geo_params
*geo
,
9877 /* number of added/removed disks in operation result */
9878 int devNumChange
= 0;
9879 /* imsm compatible layout value for array geometry verification */
9880 int imsm_layout
= -1;
9882 struct imsm_dev
*dev
;
9883 struct intel_super
*super
;
9884 long long current_size
;
9886 getinfo_super_imsm_volume(st
, &info
, NULL
);
9887 if ((geo
->level
!= info
.array
.level
) &&
9888 (geo
->level
>= 0) &&
9889 (geo
->level
!= UnSet
)) {
9890 switch (info
.array
.level
) {
9892 if (geo
->level
== 5) {
9893 change
= CH_MIGRATION
;
9894 if (geo
->layout
!= ALGORITHM_LEFT_ASYMMETRIC
) {
9896 Name
" Error. Requested Layout "
9897 "not supported (left-asymmetric layout "
9898 "is supported only)!\n");
9900 goto analyse_change_exit
;
9902 imsm_layout
= geo
->layout
;
9904 devNumChange
= 1; /* parity disk added */
9905 } else if (geo
->level
== 10) {
9906 change
= CH_TAKEOVER
;
9908 devNumChange
= 2; /* two mirrors added */
9909 imsm_layout
= 0x102; /* imsm supported layout */
9914 if (geo
->level
== 0) {
9915 change
= CH_TAKEOVER
;
9917 devNumChange
= -(geo
->raid_disks
/2);
9918 imsm_layout
= 0; /* imsm raid0 layout */
9924 Name
" Error. Level Migration from %d to %d "
9926 info
.array
.level
, geo
->level
);
9927 goto analyse_change_exit
;
9930 geo
->level
= info
.array
.level
;
9932 if ((geo
->layout
!= info
.array
.layout
)
9933 && ((geo
->layout
!= UnSet
) && (geo
->layout
!= -1))) {
9934 change
= CH_MIGRATION
;
9935 if ((info
.array
.layout
== 0)
9936 && (info
.array
.level
== 5)
9937 && (geo
->layout
== 5)) {
9938 /* reshape 5 -> 4 */
9939 } else if ((info
.array
.layout
== 5)
9940 && (info
.array
.level
== 5)
9941 && (geo
->layout
== 0)) {
9942 /* reshape 4 -> 5 */
9947 Name
" Error. Layout Migration from %d to %d "
9949 info
.array
.layout
, geo
->layout
);
9951 goto analyse_change_exit
;
9954 geo
->layout
= info
.array
.layout
;
9955 if (imsm_layout
== -1)
9956 imsm_layout
= info
.array
.layout
;
9959 if ((geo
->chunksize
> 0) && (geo
->chunksize
!= UnSet
)
9960 && (geo
->chunksize
!= info
.array
.chunk_size
))
9961 change
= CH_MIGRATION
;
9963 geo
->chunksize
= info
.array
.chunk_size
;
9965 chunk
= geo
->chunksize
/ 1024;
9968 dev
= get_imsm_dev(super
, super
->current_vol
);
9969 data_disks
= imsm_num_data_members(dev
, MAP_0
);
9970 /* compute current size per disk member
9972 current_size
= info
.custom_array_size
/ data_disks
;
9974 if (geo
->size
> 0) {
9975 /* align component size
9977 geo
->size
= imsm_component_size_aligment_check(
9978 get_imsm_raid_level(dev
->vol
.map
),
9983 if ((current_size
!= geo
->size
) && (geo
->size
>= 0)) {
9986 Name
" Error. Size change should be the only "
9987 "one at a time.\n");
9989 goto analyse_change_exit
;
9991 if ((super
->current_vol
+ 1) != super
->anchor
->num_raid_devs
) {
9993 Name
" Error. The last volume in container "
9994 "can be expanded only (%i/%i).\n",
9995 super
->current_vol
, st
->devnum
);
9996 goto analyse_change_exit
;
9998 if (geo
->size
== 0) {
9999 /* requested size change to the maximum available size
10001 unsigned long long freesize
;
10004 rv
= imsm_get_free_size(st
, dev
->vol
.map
->num_members
,
10005 0, chunk
, &freesize
);
10007 fprintf(stderr
, Name
" Error. Cannot find "
10008 "maximum available space.\n");
10010 goto analyse_change_exit
;
10012 geo
->size
= freesize
+ current_size
;
10014 /* align component size
10016 geo
->size
= imsm_component_size_aligment_check(
10017 get_imsm_raid_level(dev
->vol
.map
),
10022 if ((direction
== ROLLBACK_METADATA_CHANGES
)) {
10023 /* accept size for rollback only
10026 /* round size due to metadata compatibility
10028 geo
->size
= (geo
->size
>> SECT_PER_MB_SHIFT
)
10029 << SECT_PER_MB_SHIFT
;
10030 dprintf("Prepare update for size change to %llu\n",
10032 if (current_size
>= geo
->size
) {
10034 Name
" Error. Size expanssion is "
10035 "supported only (current size is %llu, "
10036 "requested size /rounded/ is %llu).\n",
10037 current_size
, geo
->size
);
10038 goto analyse_change_exit
;
10041 geo
->size
*= data_disks
;
10042 geo
->raid_disks
= dev
->vol
.map
->num_members
;
10043 change
= CH_ARRAY_SIZE
;
10045 if (!validate_geometry_imsm(st
,
10048 geo
->raid_disks
+ devNumChange
,
10055 struct intel_super
*super
= st
->sb
;
10056 struct imsm_super
*mpb
= super
->anchor
;
10058 if (mpb
->num_raid_devs
> 1) {
10060 Name
" Error. Cannot perform operation on %s"
10061 "- for this operation it MUST be single "
10062 "array in container\n",
10068 analyse_change_exit
:
10069 if ((direction
== ROLLBACK_METADATA_CHANGES
) &&
10070 ((change
== CH_MIGRATION
) || (change
== CH_TAKEOVER
))) {
10071 dprintf("imsm: Metadata changes rollback is not supported for "
10072 "migration and takeover operations.\n");
10078 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
10080 struct intel_super
*super
= st
->sb
;
10081 struct imsm_update_takeover
*u
;
10083 u
= malloc(sizeof(struct imsm_update_takeover
));
10087 u
->type
= update_takeover
;
10088 u
->subarray
= super
->current_vol
;
10090 /* 10->0 transition */
10091 if (geo
->level
== 0)
10092 u
->direction
= R10_TO_R0
;
10094 /* 0->10 transition */
10095 if (geo
->level
== 10)
10096 u
->direction
= R0_TO_R10
;
10098 /* update metadata locally */
10099 imsm_update_metadata_locally(st
, u
,
10100 sizeof(struct imsm_update_takeover
));
10101 /* and possibly remotely */
10102 if (st
->update_tail
)
10103 append_metadata_update(st
, u
,
10104 sizeof(struct imsm_update_takeover
));
10111 static int imsm_reshape_super(struct supertype
*st
, long long size
, int level
,
10112 int layout
, int chunksize
, int raid_disks
,
10113 int delta_disks
, char *backup
, char *dev
,
10114 int direction
, int verbose
)
10117 struct geo_params geo
;
10119 dprintf("imsm: reshape_super called.\n");
10121 memset(&geo
, 0, sizeof(struct geo_params
));
10123 geo
.dev_name
= dev
;
10124 geo
.dev_id
= st
->devnum
;
10127 geo
.layout
= layout
;
10128 geo
.chunksize
= chunksize
;
10129 geo
.raid_disks
= raid_disks
;
10130 if (delta_disks
!= UnSet
)
10131 geo
.raid_disks
+= delta_disks
;
10133 dprintf("\tfor level : %i\n", geo
.level
);
10134 dprintf("\tfor raid_disks : %i\n", geo
.raid_disks
);
10136 if (experimental() == 0)
10139 if (st
->container_dev
== st
->devnum
) {
10140 /* On container level we can only increase number of devices. */
10141 dprintf("imsm: info: Container operation\n");
10142 int old_raid_disks
= 0;
10144 if (imsm_reshape_is_allowed_on_container(
10145 st
, &geo
, &old_raid_disks
, direction
)) {
10146 struct imsm_update_reshape
*u
= NULL
;
10149 len
= imsm_create_metadata_update_for_reshape(
10150 st
, &geo
, old_raid_disks
, &u
);
10153 dprintf("imsm: Cannot prepare update\n");
10154 goto exit_imsm_reshape_super
;
10158 /* update metadata locally */
10159 imsm_update_metadata_locally(st
, u
, len
);
10160 /* and possibly remotely */
10161 if (st
->update_tail
)
10162 append_metadata_update(st
, u
, len
);
10167 fprintf(stderr
, Name
": (imsm) Operation "
10168 "is not allowed on this container\n");
10171 /* On volume level we support following operations
10172 * - takeover: raid10 -> raid0; raid0 -> raid10
10173 * - chunk size migration
10174 * - migration: raid5 -> raid0; raid0 -> raid5
10176 struct intel_super
*super
= st
->sb
;
10177 struct intel_dev
*dev
= super
->devlist
;
10178 int change
, devnum
;
10179 dprintf("imsm: info: Volume operation\n");
10180 /* find requested device */
10182 if (imsm_find_array_minor_by_subdev(
10183 dev
->index
, st
->container_dev
, &devnum
) == 0
10184 && devnum
== geo
.dev_id
)
10189 fprintf(stderr
, Name
" Cannot find %s (%i) subarray\n",
10190 geo
.dev_name
, geo
.dev_id
);
10191 goto exit_imsm_reshape_super
;
10193 super
->current_vol
= dev
->index
;
10194 change
= imsm_analyze_change(st
, &geo
, direction
);
10197 ret_val
= imsm_takeover(st
, &geo
);
10199 case CH_MIGRATION
: {
10200 struct imsm_update_reshape_migration
*u
= NULL
;
10202 imsm_create_metadata_update_for_migration(
10206 "Cannot prepare update\n");
10210 /* update metadata locally */
10211 imsm_update_metadata_locally(st
, u
, len
);
10212 /* and possibly remotely */
10213 if (st
->update_tail
)
10214 append_metadata_update(st
, u
, len
);
10219 case CH_ARRAY_SIZE
: {
10220 struct imsm_update_size_change
*u
= NULL
;
10222 imsm_create_metadata_update_for_size_change(
10226 "Cannot prepare update\n");
10230 /* update metadata locally */
10231 imsm_update_metadata_locally(st
, u
, len
);
10232 /* and possibly remotely */
10233 if (st
->update_tail
)
10234 append_metadata_update(st
, u
, len
);
10244 exit_imsm_reshape_super
:
10245 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
10249 /*******************************************************************************
10250 * Function: wait_for_reshape_imsm
10251 * Description: Function writes new sync_max value and waits until
10252 * reshape process reach new position
10254 * sra : general array info
10255 * ndata : number of disks in new array's layout
10258 * 1 : there is no reshape in progress,
10260 ******************************************************************************/
10261 int wait_for_reshape_imsm(struct mdinfo
*sra
, int ndata
)
10263 int fd
= sysfs_get_fd(sra
, NULL
, "reshape_position");
10264 unsigned long long completed
;
10265 /* to_complete : new sync_max position */
10266 unsigned long long to_complete
= sra
->reshape_progress
;
10267 unsigned long long position_to_set
= to_complete
/ ndata
;
10270 dprintf("imsm: wait_for_reshape_imsm() "
10271 "cannot open reshape_position\n");
10275 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
10276 dprintf("imsm: wait_for_reshape_imsm() "
10277 "cannot read reshape_position (no reshape in progres)\n");
10282 if (completed
> to_complete
) {
10283 dprintf("imsm: wait_for_reshape_imsm() "
10284 "wrong next position to set %llu (%llu)\n",
10285 to_complete
, completed
);
10289 dprintf("Position set: %llu\n", position_to_set
);
10290 if (sysfs_set_num(sra
, NULL
, "sync_max",
10291 position_to_set
) != 0) {
10292 dprintf("imsm: wait_for_reshape_imsm() "
10293 "cannot set reshape position to %llu\n",
10304 select(fd
+1, &rfds
, NULL
, NULL
, NULL
);
10305 if (sysfs_get_str(sra
, NULL
, "sync_action",
10307 strncmp(action
, "reshape", 7) != 0)
10309 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
10310 dprintf("imsm: wait_for_reshape_imsm() "
10311 "cannot read reshape_position (in loop)\n");
10315 } while (completed
< to_complete
);
10321 /*******************************************************************************
10322 * Function: check_degradation_change
10323 * Description: Check that array hasn't become failed.
10325 * info : for sysfs access
10326 * sources : source disks descriptors
10327 * degraded: previous degradation level
10329 * degradation level
10330 ******************************************************************************/
10331 int check_degradation_change(struct mdinfo
*info
,
10335 unsigned long long new_degraded
;
10336 sysfs_get_ll(info
, NULL
, "degraded", &new_degraded
);
10337 if (new_degraded
!= (unsigned long long)degraded
) {
10338 /* check each device to ensure it is still working */
10341 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
10342 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
10344 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
10346 if (sysfs_get_str(info
,
10347 sd
, "state", sbuf
, 20) < 0 ||
10348 strstr(sbuf
, "faulty") ||
10349 strstr(sbuf
, "in_sync") == NULL
) {
10350 /* this device is dead */
10351 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
10352 if (sd
->disk
.raid_disk
>= 0 &&
10353 sources
[sd
->disk
.raid_disk
] >= 0) {
10355 sd
->disk
.raid_disk
]);
10356 sources
[sd
->disk
.raid_disk
] =
10365 return new_degraded
;
10368 /*******************************************************************************
10369 * Function: imsm_manage_reshape
10370 * Description: Function finds array under reshape and it manages reshape
10371 * process. It creates stripes backups (if required) and sets
10374 * afd : Backup handle (nattive) - not used
10375 * sra : general array info
10376 * reshape : reshape parameters - not used
10377 * st : supertype structure
10378 * blocks : size of critical section [blocks]
10379 * fds : table of source device descriptor
10380 * offsets : start of array (offest per devices)
10382 * destfd : table of destination device descriptor
10383 * destoffsets : table of destination offsets (per device)
10385 * 1 : success, reshape is done
10387 ******************************************************************************/
10388 static int imsm_manage_reshape(
10389 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
10390 struct supertype
*st
, unsigned long backup_blocks
,
10391 int *fds
, unsigned long long *offsets
,
10392 int dests
, int *destfd
, unsigned long long *destoffsets
)
10395 struct intel_super
*super
= st
->sb
;
10396 struct intel_dev
*dv
= NULL
;
10397 struct imsm_dev
*dev
= NULL
;
10398 struct imsm_map
*map_src
;
10399 int migr_vol_qan
= 0;
10400 int ndata
, odata
; /* [bytes] */
10401 int chunk
; /* [bytes] */
10402 struct migr_record
*migr_rec
;
10404 unsigned int buf_size
; /* [bytes] */
10405 unsigned long long max_position
; /* array size [bytes] */
10406 unsigned long long next_step
; /* [blocks]/[bytes] */
10407 unsigned long long old_data_stripe_length
;
10408 unsigned long long start_src
; /* [bytes] */
10409 unsigned long long start
; /* [bytes] */
10410 unsigned long long start_buf_shift
; /* [bytes] */
10412 int source_layout
= 0;
10414 if (!fds
|| !offsets
|| !sra
)
10417 /* Find volume during the reshape */
10418 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
10419 if (dv
->dev
->vol
.migr_type
== MIGR_GEN_MIGR
10420 && dv
->dev
->vol
.migr_state
== 1) {
10425 /* Only one volume can migrate at the same time */
10426 if (migr_vol_qan
!= 1) {
10427 fprintf(stderr
, Name
" : %s", migr_vol_qan
?
10428 "Number of migrating volumes greater than 1\n" :
10429 "There is no volume during migrationg\n");
10433 map_src
= get_imsm_map(dev
, MAP_1
);
10434 if (map_src
== NULL
)
10437 ndata
= imsm_num_data_members(dev
, MAP_0
);
10438 odata
= imsm_num_data_members(dev
, MAP_1
);
10440 chunk
= __le16_to_cpu(map_src
->blocks_per_strip
) * 512;
10441 old_data_stripe_length
= odata
* chunk
;
10443 migr_rec
= super
->migr_rec
;
10445 /* initialize migration record for start condition */
10446 if (sra
->reshape_progress
== 0)
10447 init_migr_record_imsm(st
, dev
, sra
);
10449 if (__le32_to_cpu(migr_rec
->rec_status
) != UNIT_SRC_NORMAL
) {
10450 dprintf("imsm: cannot restart migration when data "
10451 "are present in copy area.\n");
10454 /* Save checkpoint to update migration record for current
10455 * reshape position (in md). It can be farther than current
10456 * reshape position in metadata.
10458 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
10459 /* ignore error == 2, this can mean end of reshape here
10461 dprintf("imsm: Cannot write checkpoint to "
10462 "migration record (UNIT_SRC_NORMAL, "
10463 "initial save)\n");
10468 /* size for data */
10469 buf_size
= __le32_to_cpu(migr_rec
->blocks_per_unit
) * 512;
10470 /* extend buffer size for parity disk */
10471 buf_size
+= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
10472 /* add space for stripe aligment */
10473 buf_size
+= old_data_stripe_length
;
10474 if (posix_memalign((void **)&buf
, 4096, buf_size
)) {
10475 dprintf("imsm: Cannot allocate checpoint buffer\n");
10479 max_position
= sra
->component_size
* ndata
;
10480 source_layout
= imsm_level_to_layout(map_src
->raid_level
);
10482 while (__le32_to_cpu(migr_rec
->curr_migr_unit
) <
10483 __le32_to_cpu(migr_rec
->num_migr_units
)) {
10484 /* current reshape position [blocks] */
10485 unsigned long long current_position
=
10486 __le32_to_cpu(migr_rec
->blocks_per_unit
)
10487 * __le32_to_cpu(migr_rec
->curr_migr_unit
);
10488 unsigned long long border
;
10490 /* Check that array hasn't become failed.
10492 degraded
= check_degradation_change(sra
, fds
, degraded
);
10493 if (degraded
> 1) {
10494 dprintf("imsm: Abort reshape due to degradation"
10495 " level (%i)\n", degraded
);
10499 next_step
= __le32_to_cpu(migr_rec
->blocks_per_unit
);
10501 if ((current_position
+ next_step
) > max_position
)
10502 next_step
= max_position
- current_position
;
10504 start
= current_position
* 512;
10506 /* allign reading start to old geometry */
10507 start_buf_shift
= start
% old_data_stripe_length
;
10508 start_src
= start
- start_buf_shift
;
10510 border
= (start_src
/ odata
) - (start
/ ndata
);
10512 if (border
<= __le32_to_cpu(migr_rec
->dest_depth_per_unit
)) {
10513 /* save critical stripes to buf
10514 * start - start address of current unit
10515 * to backup [bytes]
10516 * start_src - start address of current unit
10517 * to backup alligned to source array
10520 unsigned long long next_step_filler
= 0;
10521 unsigned long long copy_length
= next_step
* 512;
10523 /* allign copy area length to stripe in old geometry */
10524 next_step_filler
= ((copy_length
+ start_buf_shift
)
10525 % old_data_stripe_length
);
10526 if (next_step_filler
)
10527 next_step_filler
= (old_data_stripe_length
10528 - next_step_filler
);
10529 dprintf("save_stripes() parameters: start = %llu,"
10530 "\tstart_src = %llu,\tnext_step*512 = %llu,"
10531 "\tstart_in_buf_shift = %llu,"
10532 "\tnext_step_filler = %llu\n",
10533 start
, start_src
, copy_length
,
10534 start_buf_shift
, next_step_filler
);
10536 if (save_stripes(fds
, offsets
, map_src
->num_members
,
10537 chunk
, map_src
->raid_level
,
10538 source_layout
, 0, NULL
, start_src
,
10540 next_step_filler
+ start_buf_shift
,
10542 dprintf("imsm: Cannot save stripes"
10546 /* Convert data to destination format and store it
10547 * in backup general migration area
10549 if (save_backup_imsm(st
, dev
, sra
,
10550 buf
+ start_buf_shift
, copy_length
)) {
10551 dprintf("imsm: Cannot save stripes to "
10552 "target devices\n");
10555 if (save_checkpoint_imsm(st
, sra
,
10556 UNIT_SRC_IN_CP_AREA
)) {
10557 dprintf("imsm: Cannot write checkpoint to "
10558 "migration record (UNIT_SRC_IN_CP_AREA)\n");
10562 /* set next step to use whole border area */
10563 border
/= next_step
;
10565 next_step
*= border
;
10567 /* When data backed up, checkpoint stored,
10568 * kick the kernel to reshape unit of data
10570 next_step
= next_step
+ sra
->reshape_progress
;
10571 /* limit next step to array max position */
10572 if (next_step
> max_position
)
10573 next_step
= max_position
;
10574 sysfs_set_num(sra
, NULL
, "suspend_lo", sra
->reshape_progress
);
10575 sysfs_set_num(sra
, NULL
, "suspend_hi", next_step
);
10576 sra
->reshape_progress
= next_step
;
10578 /* wait until reshape finish */
10579 if (wait_for_reshape_imsm(sra
, ndata
) < 0) {
10580 dprintf("wait_for_reshape_imsm returned error!\n");
10584 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
10585 /* ignore error == 2, this can mean end of reshape here
10587 dprintf("imsm: Cannot write checkpoint to "
10588 "migration record (UNIT_SRC_NORMAL)\n");
10594 /* return '1' if done */
10598 abort_reshape(sra
);
10602 #endif /* MDASSEMBLE */
10604 struct superswitch super_imsm
= {
10606 .examine_super
= examine_super_imsm
,
10607 .brief_examine_super
= brief_examine_super_imsm
,
10608 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
10609 .export_examine_super
= export_examine_super_imsm
,
10610 .detail_super
= detail_super_imsm
,
10611 .brief_detail_super
= brief_detail_super_imsm
,
10612 .write_init_super
= write_init_super_imsm
,
10613 .validate_geometry
= validate_geometry_imsm
,
10614 .add_to_super
= add_to_super_imsm
,
10615 .remove_from_super
= remove_from_super_imsm
,
10616 .detail_platform
= detail_platform_imsm
,
10617 .kill_subarray
= kill_subarray_imsm
,
10618 .update_subarray
= update_subarray_imsm
,
10619 .load_container
= load_container_imsm
,
10620 .default_geometry
= default_geometry_imsm
,
10621 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
10622 .reshape_super
= imsm_reshape_super
,
10623 .manage_reshape
= imsm_manage_reshape
,
10624 .recover_backup
= recover_backup_imsm
,
10626 .match_home
= match_home_imsm
,
10627 .uuid_from_super
= uuid_from_super_imsm
,
10628 .getinfo_super
= getinfo_super_imsm
,
10629 .getinfo_super_disks
= getinfo_super_disks_imsm
,
10630 .update_super
= update_super_imsm
,
10632 .avail_size
= avail_size_imsm
,
10633 .min_acceptable_spare_size
= min_acceptable_spare_size_imsm
,
10635 .compare_super
= compare_super_imsm
,
10637 .load_super
= load_super_imsm
,
10638 .init_super
= init_super_imsm
,
10639 .store_super
= store_super_imsm
,
10640 .free_super
= free_super_imsm
,
10641 .match_metadata_desc
= match_metadata_desc_imsm
,
10642 .container_content
= container_content_imsm
,
10650 .open_new
= imsm_open_new
,
10651 .set_array_state
= imsm_set_array_state
,
10652 .set_disk
= imsm_set_disk
,
10653 .sync_metadata
= imsm_sync_metadata
,
10654 .activate_spare
= imsm_activate_spare
,
10655 .process_update
= imsm_process_update
,
10656 .prepare_update
= imsm_prepare_update
,
10657 #endif /* MDASSEMBLE */