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
; /* 0xE8 - 0xEB total blocks */
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 #define IMSM_DISK_FILLERS 4
106 __u32 filler
[IMSM_DISK_FILLERS
]; /* 0xF4 - 0x107 MPB_DISK_FILLERS for future expansion */
109 /* map selector for map managment
115 /* RAID map configuration infos. */
117 __u32 pba_of_lba0
; /* start address of partition */
118 __u32 blocks_per_member
;/* blocks per member */
119 __u32 num_data_stripes
; /* number of data stripes */
120 __u16 blocks_per_strip
;
121 __u8 map_state
; /* Normal, Uninitialized, Degraded, Failed */
122 #define IMSM_T_STATE_NORMAL 0
123 #define IMSM_T_STATE_UNINITIALIZED 1
124 #define IMSM_T_STATE_DEGRADED 2
125 #define IMSM_T_STATE_FAILED 3
127 #define IMSM_T_RAID0 0
128 #define IMSM_T_RAID1 1
129 #define IMSM_T_RAID5 5 /* since metadata version 1.2.02 ? */
130 __u8 num_members
; /* number of member disks */
131 __u8 num_domains
; /* number of parity domains */
132 __u8 failed_disk_num
; /* valid only when state is degraded */
134 __u32 filler
[7]; /* expansion area */
135 #define IMSM_ORD_REBUILD (1 << 24)
136 __u32 disk_ord_tbl
[1]; /* disk_ord_tbl[num_members],
137 * top byte contains some flags
139 } __attribute__ ((packed
));
142 __u32 curr_migr_unit
;
143 __u32 checkpoint_id
; /* id to access curr_migr_unit */
144 __u8 migr_state
; /* Normal or Migrating */
146 #define MIGR_REBUILD 1
147 #define MIGR_VERIFY 2 /* analagous to echo check > sync_action */
148 #define MIGR_GEN_MIGR 3
149 #define MIGR_STATE_CHANGE 4
150 #define MIGR_REPAIR 5
151 __u8 migr_type
; /* Initializing, Rebuilding, ... */
153 __u8 fs_state
; /* fast-sync state for CnG (0xff == disabled) */
154 __u16 verify_errors
; /* number of mismatches */
155 __u16 bad_blocks
; /* number of bad blocks during verify */
157 struct imsm_map map
[1];
158 /* here comes another one if migr_state */
159 } __attribute__ ((packed
));
162 __u8 volume
[MAX_RAID_SERIAL_LEN
];
165 #define DEV_BOOTABLE __cpu_to_le32(0x01)
166 #define DEV_BOOT_DEVICE __cpu_to_le32(0x02)
167 #define DEV_READ_COALESCING __cpu_to_le32(0x04)
168 #define DEV_WRITE_COALESCING __cpu_to_le32(0x08)
169 #define DEV_LAST_SHUTDOWN_DIRTY __cpu_to_le32(0x10)
170 #define DEV_HIDDEN_AT_BOOT __cpu_to_le32(0x20)
171 #define DEV_CURRENTLY_HIDDEN __cpu_to_le32(0x40)
172 #define DEV_VERIFY_AND_FIX __cpu_to_le32(0x80)
173 #define DEV_MAP_STATE_UNINIT __cpu_to_le32(0x100)
174 #define DEV_NO_AUTO_RECOVERY __cpu_to_le32(0x200)
175 #define DEV_CLONE_N_GO __cpu_to_le32(0x400)
176 #define DEV_CLONE_MAN_SYNC __cpu_to_le32(0x800)
177 #define DEV_CNG_MASTER_DISK_NUM __cpu_to_le32(0x1000)
178 __u32 status
; /* Persistent RaidDev status */
179 __u32 reserved_blocks
; /* Reserved blocks at beginning of volume */
183 __u8 cng_master_disk
;
187 #define IMSM_DEV_FILLERS 10
188 __u32 filler
[IMSM_DEV_FILLERS
];
190 } __attribute__ ((packed
));
193 __u8 sig
[MAX_SIGNATURE_LENGTH
]; /* 0x00 - 0x1F */
194 __u32 check_sum
; /* 0x20 - 0x23 MPB Checksum */
195 __u32 mpb_size
; /* 0x24 - 0x27 Size of MPB */
196 __u32 family_num
; /* 0x28 - 0x2B Checksum from first time this config was written */
197 __u32 generation_num
; /* 0x2C - 0x2F Incremented each time this array's MPB is written */
198 __u32 error_log_size
; /* 0x30 - 0x33 in bytes */
199 __u32 attributes
; /* 0x34 - 0x37 */
200 __u8 num_disks
; /* 0x38 Number of configured disks */
201 __u8 num_raid_devs
; /* 0x39 Number of configured volumes */
202 __u8 error_log_pos
; /* 0x3A */
203 __u8 fill
[1]; /* 0x3B */
204 __u32 cache_size
; /* 0x3c - 0x40 in mb */
205 __u32 orig_family_num
; /* 0x40 - 0x43 original family num */
206 __u32 pwr_cycle_count
; /* 0x44 - 0x47 simulated power cycle count for array */
207 __u32 bbm_log_size
; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */
208 #define IMSM_FILLERS 35
209 __u32 filler
[IMSM_FILLERS
]; /* 0x4C - 0xD7 RAID_MPB_FILLERS */
210 struct imsm_disk disk
[1]; /* 0xD8 diskTbl[numDisks] */
211 /* here comes imsm_dev[num_raid_devs] */
212 /* here comes BBM logs */
213 } __attribute__ ((packed
));
215 #define BBM_LOG_MAX_ENTRIES 254
217 struct bbm_log_entry
{
218 __u64 defective_block_start
;
219 #define UNREADABLE 0xFFFFFFFF
220 __u32 spare_block_offset
;
221 __u16 remapped_marked_count
;
223 } __attribute__ ((__packed__
));
226 __u32 signature
; /* 0xABADB10C */
228 __u32 reserved_spare_block_count
; /* 0 */
229 __u32 reserved
; /* 0xFFFF */
230 __u64 first_spare_lba
;
231 struct bbm_log_entry mapped_block_entries
[BBM_LOG_MAX_ENTRIES
];
232 } __attribute__ ((__packed__
));
236 static char *map_state_str
[] = { "normal", "uninitialized", "degraded", "failed" };
239 #define RAID_DISK_RESERVED_BLOCKS_IMSM_HI 2209
241 #define GEN_MIGR_AREA_SIZE 2048 /* General Migration Copy Area size in blocks */
243 #define MIGR_REC_BUF_SIZE 512 /* size of migr_record i/o buffer */
244 #define MIGR_REC_POSITION 512 /* migr_record position offset on disk,
245 * MIGR_REC_BUF_SIZE <= MIGR_REC_POSITION
249 #define UNIT_SRC_NORMAL 0 /* Source data for curr_migr_unit must
250 * be recovered using srcMap */
251 #define UNIT_SRC_IN_CP_AREA 1 /* Source data for curr_migr_unit has
252 * already been migrated and must
253 * be recovered from checkpoint area */
255 __u32 rec_status
; /* Status used to determine how to restart
256 * migration in case it aborts
258 __u32 curr_migr_unit
; /* 0..numMigrUnits-1 */
259 __u32 family_num
; /* Family number of MPB
260 * containing the RaidDev
261 * that is migrating */
262 __u32 ascending_migr
; /* True if migrating in increasing
264 __u32 blocks_per_unit
; /* Num disk blocks per unit of operation */
265 __u32 dest_depth_per_unit
; /* Num member blocks each destMap
267 * advances per unit-of-operation */
268 __u32 ckpt_area_pba
; /* Pba of first block of ckpt copy area */
269 __u32 dest_1st_member_lba
; /* First member lba on first
270 * stripe of destination */
271 __u32 num_migr_units
; /* Total num migration units-of-op */
272 __u32 post_migr_vol_cap
; /* Size of volume after
273 * migration completes */
274 __u32 post_migr_vol_cap_hi
; /* Expansion space for LBA64 */
275 __u32 ckpt_read_disk_num
; /* Which member disk in destSubMap[0] the
276 * migration ckpt record was read from
277 * (for recovered migrations) */
278 } __attribute__ ((__packed__
));
283 * 2: metadata does not match
291 struct md_list
*next
;
294 #define pr_vrb(fmt, arg...) (void) (verbose && fprintf(stderr, Name fmt, ##arg))
296 static __u8
migr_type(struct imsm_dev
*dev
)
298 if (dev
->vol
.migr_type
== MIGR_VERIFY
&&
299 dev
->status
& DEV_VERIFY_AND_FIX
)
302 return dev
->vol
.migr_type
;
305 static void set_migr_type(struct imsm_dev
*dev
, __u8 migr_type
)
307 /* for compatibility with older oroms convert MIGR_REPAIR, into
308 * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
310 if (migr_type
== MIGR_REPAIR
) {
311 dev
->vol
.migr_type
= MIGR_VERIFY
;
312 dev
->status
|= DEV_VERIFY_AND_FIX
;
314 dev
->vol
.migr_type
= migr_type
;
315 dev
->status
&= ~DEV_VERIFY_AND_FIX
;
319 static unsigned int sector_count(__u32 bytes
)
321 return ((bytes
+ (512-1)) & (~(512-1))) / 512;
324 static unsigned int mpb_sectors(struct imsm_super
*mpb
)
326 return sector_count(__le32_to_cpu(mpb
->mpb_size
));
330 struct imsm_dev
*dev
;
331 struct intel_dev
*next
;
336 enum sys_dev_type type
;
339 struct intel_hba
*next
;
346 /* internal representation of IMSM metadata */
349 void *buf
; /* O_DIRECT buffer for reading/writing metadata */
350 struct imsm_super
*anchor
; /* immovable parameters */
353 void *migr_rec_buf
; /* buffer for I/O operations */
354 struct migr_record
*migr_rec
; /* migration record */
356 int clean_migration_record_by_mdmon
; /* when reshape is switched to next
357 array, it indicates that mdmon is allowed to clean migration
359 size_t len
; /* size of the 'buf' allocation */
360 void *next_buf
; /* for realloc'ing buf from the manager */
362 int updates_pending
; /* count of pending updates for mdmon */
363 int current_vol
; /* index of raid device undergoing creation */
364 __u32 create_offset
; /* common start for 'current_vol' */
365 __u32 random
; /* random data for seeding new family numbers */
366 struct intel_dev
*devlist
;
370 __u8 serial
[MAX_RAID_SERIAL_LEN
];
373 struct imsm_disk disk
;
376 struct extent
*e
; /* for determining freespace @ create */
377 int raiddisk
; /* slot to fill in autolayout */
379 } *disks
, *current_disk
;
380 struct dl
*disk_mgmt_list
; /* list of disks to add/remove while mdmon
382 struct dl
*missing
; /* disks removed while we weren't looking */
383 struct bbm_log
*bbm_log
;
384 struct intel_hba
*hba
; /* device path of the raid controller for this metadata */
385 const struct imsm_orom
*orom
; /* platform firmware support */
386 struct intel_super
*next
; /* (temp) list for disambiguating family_num */
390 struct imsm_disk disk
;
391 #define IMSM_UNKNOWN_OWNER (-1)
393 struct intel_disk
*next
;
397 unsigned long long start
, size
;
400 /* definitions of reshape process types */
401 enum imsm_reshape_type
{
406 /* definition of messages passed to imsm_process_update */
407 enum imsm_update_type
{
408 update_activate_spare
,
412 update_add_remove_disk
,
413 update_reshape_container_disks
,
414 update_reshape_migration
,
416 update_general_migration_checkpoint
,
419 struct imsm_update_activate_spare
{
420 enum imsm_update_type type
;
424 struct imsm_update_activate_spare
*next
;
437 enum takeover_direction
{
441 struct imsm_update_takeover
{
442 enum imsm_update_type type
;
444 enum takeover_direction direction
;
447 struct imsm_update_reshape
{
448 enum imsm_update_type type
;
452 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
455 struct imsm_update_reshape_migration
{
456 enum imsm_update_type type
;
459 /* fields for array migration changes
466 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
469 struct imsm_update_general_migration_checkpoint
{
470 enum imsm_update_type type
;
471 __u32 curr_migr_unit
;
475 __u8 serial
[MAX_RAID_SERIAL_LEN
];
478 struct imsm_update_create_array
{
479 enum imsm_update_type type
;
484 struct imsm_update_kill_array
{
485 enum imsm_update_type type
;
489 struct imsm_update_rename_array
{
490 enum imsm_update_type type
;
491 __u8 name
[MAX_RAID_SERIAL_LEN
];
495 struct imsm_update_add_remove_disk
{
496 enum imsm_update_type type
;
500 static const char *_sys_dev_type
[] = {
501 [SYS_DEV_UNKNOWN
] = "Unknown",
502 [SYS_DEV_SAS
] = "SAS",
503 [SYS_DEV_SATA
] = "SATA"
506 const char *get_sys_dev_type(enum sys_dev_type type
)
508 if (type
>= SYS_DEV_MAX
)
509 type
= SYS_DEV_UNKNOWN
;
511 return _sys_dev_type
[type
];
514 static struct intel_hba
* alloc_intel_hba(struct sys_dev
*device
)
516 struct intel_hba
*result
= malloc(sizeof(*result
));
518 result
->type
= device
->type
;
519 result
->path
= strdup(device
->path
);
521 if (result
->path
&& (result
->pci_id
= strrchr(result
->path
, '/')) != NULL
)
527 static struct intel_hba
* find_intel_hba(struct intel_hba
*hba
, struct sys_dev
*device
)
529 struct intel_hba
*result
=NULL
;
530 for (result
= hba
; result
; result
= result
->next
) {
531 if (result
->type
== device
->type
&& strcmp(result
->path
, device
->path
) == 0)
537 static int attach_hba_to_super(struct intel_super
*super
, struct sys_dev
*device
)
539 struct intel_hba
*hba
;
541 /* check if disk attached to Intel HBA */
542 hba
= find_intel_hba(super
->hba
, device
);
545 /* Check if HBA is already attached to super */
546 if (super
->hba
== NULL
) {
547 super
->hba
= alloc_intel_hba(device
);
552 /* Intel metadata allows for all disks attached to the same type HBA.
553 * Do not sypport odf HBA types mixing
555 if (device
->type
!= hba
->type
)
561 hba
->next
= alloc_intel_hba(device
);
565 static struct sys_dev
* find_disk_attached_hba(int fd
, const char *devname
)
567 struct sys_dev
*list
, *elem
, *prev
;
570 if ((list
= find_intel_devices()) == NULL
)
574 disk_path
= (char *) devname
;
576 disk_path
= diskfd_to_devpath(fd
);
583 for (prev
= NULL
, elem
= list
; elem
; prev
= elem
, elem
= elem
->next
) {
584 if (path_attached_to_hba(disk_path
, elem
->path
)) {
588 prev
->next
= elem
->next
;
590 if (disk_path
!= devname
)
596 if (disk_path
!= devname
)
604 static int find_intel_hba_capability(int fd
, struct intel_super
*super
,
607 static struct supertype
*match_metadata_desc_imsm(char *arg
)
609 struct supertype
*st
;
611 if (strcmp(arg
, "imsm") != 0 &&
612 strcmp(arg
, "default") != 0
616 st
= malloc(sizeof(*st
));
619 memset(st
, 0, sizeof(*st
));
620 st
->container_dev
= NoMdDev
;
621 st
->ss
= &super_imsm
;
622 st
->max_devs
= IMSM_MAX_DEVICES
;
623 st
->minor_version
= 0;
629 static __u8
*get_imsm_version(struct imsm_super
*mpb
)
631 return &mpb
->sig
[MPB_SIG_LEN
];
635 /* retrieve a disk directly from the anchor when the anchor is known to be
636 * up-to-date, currently only at load time
638 static struct imsm_disk
*__get_imsm_disk(struct imsm_super
*mpb
, __u8 index
)
640 if (index
>= mpb
->num_disks
)
642 return &mpb
->disk
[index
];
645 /* retrieve the disk description based on a index of the disk
648 static struct dl
*get_imsm_dl_disk(struct intel_super
*super
, __u8 index
)
652 for (d
= super
->disks
; d
; d
= d
->next
)
653 if (d
->index
== index
)
658 /* retrieve a disk from the parsed metadata */
659 static struct imsm_disk
*get_imsm_disk(struct intel_super
*super
, __u8 index
)
663 dl
= get_imsm_dl_disk(super
, index
);
670 /* generate a checksum directly from the anchor when the anchor is known to be
671 * up-to-date, currently only at load or write_super after coalescing
673 static __u32
__gen_imsm_checksum(struct imsm_super
*mpb
)
675 __u32 end
= mpb
->mpb_size
/ sizeof(end
);
676 __u32
*p
= (__u32
*) mpb
;
680 sum
+= __le32_to_cpu(*p
);
684 return sum
- __le32_to_cpu(mpb
->check_sum
);
687 static size_t sizeof_imsm_map(struct imsm_map
*map
)
689 return sizeof(struct imsm_map
) + sizeof(__u32
) * (map
->num_members
- 1);
692 struct imsm_map
*get_imsm_map(struct imsm_dev
*dev
, int second_map
)
694 /* A device can have 2 maps if it is in the middle of a migration.
696 * MAP_0 - we return the first map
697 * MAP_1 - we return the second map if it exists, else NULL
698 * MAP_X - we return the second map if it exists, else the first
700 struct imsm_map
*map
= &dev
->vol
.map
[0];
701 struct imsm_map
*map2
= NULL
;
703 if (dev
->vol
.migr_state
)
704 map2
= (void *)map
+ sizeof_imsm_map(map
);
706 switch (second_map
) {
723 /* return the size of the device.
724 * migr_state increases the returned size if map[0] were to be duplicated
726 static size_t sizeof_imsm_dev(struct imsm_dev
*dev
, int migr_state
)
728 size_t size
= sizeof(*dev
) - sizeof(struct imsm_map
) +
729 sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
731 /* migrating means an additional map */
732 if (dev
->vol
.migr_state
)
733 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_1
));
735 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
741 /* retrieve disk serial number list from a metadata update */
742 static struct disk_info
*get_disk_info(struct imsm_update_create_array
*update
)
745 struct disk_info
*inf
;
747 inf
= u
+ sizeof(*update
) - sizeof(struct imsm_dev
) +
748 sizeof_imsm_dev(&update
->dev
, 0);
754 static struct imsm_dev
*__get_imsm_dev(struct imsm_super
*mpb
, __u8 index
)
760 if (index
>= mpb
->num_raid_devs
)
763 /* devices start after all disks */
764 offset
= ((void *) &mpb
->disk
[mpb
->num_disks
]) - _mpb
;
766 for (i
= 0; i
<= index
; i
++)
768 return _mpb
+ offset
;
770 offset
+= sizeof_imsm_dev(_mpb
+ offset
, 0);
775 static struct imsm_dev
*get_imsm_dev(struct intel_super
*super
, __u8 index
)
777 struct intel_dev
*dv
;
779 if (index
>= super
->anchor
->num_raid_devs
)
781 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
782 if (dv
->index
== index
)
789 * == MAP_0 get first map
790 * == MAP_1 get second map
791 * == MAP_X than get map according to the current migr_state
793 static __u32
get_imsm_ord_tbl_ent(struct imsm_dev
*dev
,
797 struct imsm_map
*map
;
799 map
= get_imsm_map(dev
, second_map
);
801 /* top byte identifies disk under rebuild */
802 return __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
805 #define ord_to_idx(ord) (((ord) << 8) >> 8)
806 static __u32
get_imsm_disk_idx(struct imsm_dev
*dev
, int slot
, int second_map
)
808 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, second_map
);
810 return ord_to_idx(ord
);
813 static void set_imsm_ord_tbl_ent(struct imsm_map
*map
, int slot
, __u32 ord
)
815 map
->disk_ord_tbl
[slot
] = __cpu_to_le32(ord
);
818 static int get_imsm_disk_slot(struct imsm_map
*map
, unsigned idx
)
823 for (slot
= 0; slot
< map
->num_members
; slot
++) {
824 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
825 if (ord_to_idx(ord
) == idx
)
832 static int get_imsm_raid_level(struct imsm_map
*map
)
834 if (map
->raid_level
== 1) {
835 if (map
->num_members
== 2)
841 return map
->raid_level
;
844 static int cmp_extent(const void *av
, const void *bv
)
846 const struct extent
*a
= av
;
847 const struct extent
*b
= bv
;
848 if (a
->start
< b
->start
)
850 if (a
->start
> b
->start
)
855 static int count_memberships(struct dl
*dl
, struct intel_super
*super
)
860 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
861 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
862 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
864 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
871 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
);
873 static struct extent
*get_extents(struct intel_super
*super
, struct dl
*dl
)
875 /* find a list of used extents on the given physical device */
876 struct extent
*rv
, *e
;
878 int memberships
= count_memberships(dl
, super
);
881 /* trim the reserved area for spares, so they can join any array
882 * regardless of whether the OROM has assigned sectors from the
883 * IMSM_RESERVED_SECTORS region
886 reservation
= imsm_min_reserved_sectors(super
);
888 reservation
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
890 rv
= malloc(sizeof(struct extent
) * (memberships
+ 1));
895 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
896 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
897 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
899 if (get_imsm_disk_slot(map
, dl
->index
) >= 0) {
900 e
->start
= __le32_to_cpu(map
->pba_of_lba0
);
901 e
->size
= __le32_to_cpu(map
->blocks_per_member
);
905 qsort(rv
, memberships
, sizeof(*rv
), cmp_extent
);
907 /* determine the start of the metadata
908 * when no raid devices are defined use the default
909 * ...otherwise allow the metadata to truncate the value
910 * as is the case with older versions of imsm
913 struct extent
*last
= &rv
[memberships
- 1];
916 remainder
= __le32_to_cpu(dl
->disk
.total_blocks
) -
917 (last
->start
+ last
->size
);
918 /* round down to 1k block to satisfy precision of the kernel
922 /* make sure remainder is still sane */
923 if (remainder
< (unsigned)ROUND_UP(super
->len
, 512) >> 9)
924 remainder
= ROUND_UP(super
->len
, 512) >> 9;
925 if (reservation
> remainder
)
926 reservation
= remainder
;
928 e
->start
= __le32_to_cpu(dl
->disk
.total_blocks
) - reservation
;
933 /* try to determine how much space is reserved for metadata from
934 * the last get_extents() entry, otherwise fallback to the
937 static __u32
imsm_reserved_sectors(struct intel_super
*super
, struct dl
*dl
)
943 /* for spares just return a minimal reservation which will grow
944 * once the spare is picked up by an array
947 return MPB_SECTOR_CNT
;
949 e
= get_extents(super
, dl
);
951 return MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
953 /* scroll to last entry */
954 for (i
= 0; e
[i
].size
; i
++)
957 rv
= __le32_to_cpu(dl
->disk
.total_blocks
) - e
[i
].start
;
964 static int is_spare(struct imsm_disk
*disk
)
966 return (disk
->status
& SPARE_DISK
) == SPARE_DISK
;
969 static int is_configured(struct imsm_disk
*disk
)
971 return (disk
->status
& CONFIGURED_DISK
) == CONFIGURED_DISK
;
974 static int is_failed(struct imsm_disk
*disk
)
976 return (disk
->status
& FAILED_DISK
) == FAILED_DISK
;
979 /* try to determine how much space is reserved for metadata from
980 * the last get_extents() entry on the smallest active disk,
981 * otherwise fallback to the default
983 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
)
987 __u32 min_active
, remainder
;
988 __u32 rv
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
989 struct dl
*dl
, *dl_min
= NULL
;
995 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
998 if (dl
->disk
.total_blocks
< min_active
|| min_active
== 0) {
1000 min_active
= dl
->disk
.total_blocks
;
1006 /* find last lba used by subarrays on the smallest active disk */
1007 e
= get_extents(super
, dl_min
);
1010 for (i
= 0; e
[i
].size
; i
++)
1013 remainder
= min_active
- e
[i
].start
;
1016 /* to give priority to recovery we should not require full
1017 IMSM_RESERVED_SECTORS from the spare */
1018 rv
= MPB_SECTOR_CNT
+ NUM_BLOCKS_DIRTY_STRIPE_REGION
;
1020 /* if real reservation is smaller use that value */
1021 return (remainder
< rv
) ? remainder
: rv
;
1024 /* Return minimum size of a spare that can be used in this array*/
1025 static unsigned long long min_acceptable_spare_size_imsm(struct supertype
*st
)
1027 struct intel_super
*super
= st
->sb
;
1031 unsigned long long rv
= 0;
1035 /* find first active disk in array */
1037 while (dl
&& (is_failed(&dl
->disk
) || dl
->index
== -1))
1041 /* find last lba used by subarrays */
1042 e
= get_extents(super
, dl
);
1045 for (i
= 0; e
[i
].size
; i
++)
1048 rv
= e
[i
-1].start
+ e
[i
-1].size
;
1051 /* add the amount of space needed for metadata */
1052 rv
= rv
+ imsm_min_reserved_sectors(super
);
1057 static int is_gen_migration(struct imsm_dev
*dev
);
1060 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
1061 struct imsm_dev
*dev
);
1063 static void print_imsm_dev(struct intel_super
*super
,
1064 struct imsm_dev
*dev
,
1070 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1071 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
1075 printf("[%.16s]:\n", dev
->volume
);
1076 printf(" UUID : %s\n", uuid
);
1077 printf(" RAID Level : %d", get_imsm_raid_level(map
));
1079 printf(" <-- %d", get_imsm_raid_level(map2
));
1081 printf(" Members : %d", map
->num_members
);
1083 printf(" <-- %d", map2
->num_members
);
1085 printf(" Slots : [");
1086 for (i
= 0; i
< map
->num_members
; i
++) {
1087 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
1088 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1093 for (i
= 0; i
< map2
->num_members
; i
++) {
1094 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_1
);
1095 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1100 printf(" Failed disk : ");
1101 if (map
->failed_disk_num
== 0xff)
1104 printf("%i", map
->failed_disk_num
);
1106 slot
= get_imsm_disk_slot(map
, disk_idx
);
1108 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
1109 printf(" This Slot : %d%s\n", slot
,
1110 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
1112 printf(" This Slot : ?\n");
1113 sz
= __le32_to_cpu(dev
->size_high
);
1115 sz
+= __le32_to_cpu(dev
->size_low
);
1116 printf(" Array Size : %llu%s\n", (unsigned long long)sz
,
1117 human_size(sz
* 512));
1118 sz
= __le32_to_cpu(map
->blocks_per_member
);
1119 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz
,
1120 human_size(sz
* 512));
1121 printf(" Sector Offset : %u\n",
1122 __le32_to_cpu(map
->pba_of_lba0
));
1123 printf(" Num Stripes : %u\n",
1124 __le32_to_cpu(map
->num_data_stripes
));
1125 printf(" Chunk Size : %u KiB",
1126 __le16_to_cpu(map
->blocks_per_strip
) / 2);
1128 printf(" <-- %u KiB",
1129 __le16_to_cpu(map2
->blocks_per_strip
) / 2);
1131 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
1132 printf(" Migrate State : ");
1133 if (dev
->vol
.migr_state
) {
1134 if (migr_type(dev
) == MIGR_INIT
)
1135 printf("initialize\n");
1136 else if (migr_type(dev
) == MIGR_REBUILD
)
1137 printf("rebuild\n");
1138 else if (migr_type(dev
) == MIGR_VERIFY
)
1140 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
1141 printf("general migration\n");
1142 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
1143 printf("state change\n");
1144 else if (migr_type(dev
) == MIGR_REPAIR
)
1147 printf("<unknown:%d>\n", migr_type(dev
));
1150 printf(" Map State : %s", map_state_str
[map
->map_state
]);
1151 if (dev
->vol
.migr_state
) {
1152 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
1154 printf(" <-- %s", map_state_str
[map
->map_state
]);
1155 printf("\n Checkpoint : %u ",
1156 __le32_to_cpu(dev
->vol
.curr_migr_unit
));
1157 if ((is_gen_migration(dev
)) && ((slot
> 1) || (slot
< 0)))
1160 printf("(%llu)", (unsigned long long)
1161 blocks_per_migr_unit(super
, dev
));
1164 printf(" Dirty State : %s\n", dev
->vol
.dirty
? "dirty" : "clean");
1167 static void print_imsm_disk(struct imsm_disk
*disk
, int index
, __u32 reserved
)
1169 char str
[MAX_RAID_SERIAL_LEN
+ 1];
1172 if (index
< -1 || !disk
)
1176 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
1178 printf(" Disk%02d Serial : %s\n", index
, str
);
1180 printf(" Disk Serial : %s\n", str
);
1181 printf(" State :%s%s%s\n", is_spare(disk
) ? " spare" : "",
1182 is_configured(disk
) ? " active" : "",
1183 is_failed(disk
) ? " failed" : "");
1184 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
1185 sz
= __le32_to_cpu(disk
->total_blocks
) - reserved
;
1186 printf(" Usable Size : %llu%s\n", (unsigned long long)sz
,
1187 human_size(sz
* 512));
1190 void examine_migr_rec_imsm(struct intel_super
*super
)
1192 struct migr_record
*migr_rec
= super
->migr_rec
;
1193 struct imsm_super
*mpb
= super
->anchor
;
1196 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1197 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1198 struct imsm_map
*map
;
1201 if (is_gen_migration(dev
) == 0)
1204 printf("\nMigration Record Information:");
1206 /* first map under migration */
1207 map
= get_imsm_map(dev
, MAP_0
);
1209 slot
= get_imsm_disk_slot(map
, super
->disks
->index
);
1210 if ((map
== NULL
) || (slot
> 1) || (slot
< 0)) {
1211 printf(" Empty\n ");
1212 printf("Examine one of first two disks in array\n");
1215 printf("\n Status : ");
1216 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
1219 printf("Contains Data\n");
1220 printf(" Current Unit : %u\n",
1221 __le32_to_cpu(migr_rec
->curr_migr_unit
));
1222 printf(" Family : %u\n",
1223 __le32_to_cpu(migr_rec
->family_num
));
1224 printf(" Ascending : %u\n",
1225 __le32_to_cpu(migr_rec
->ascending_migr
));
1226 printf(" Blocks Per Unit : %u\n",
1227 __le32_to_cpu(migr_rec
->blocks_per_unit
));
1228 printf(" Dest. Depth Per Unit : %u\n",
1229 __le32_to_cpu(migr_rec
->dest_depth_per_unit
));
1230 printf(" Checkpoint Area pba : %u\n",
1231 __le32_to_cpu(migr_rec
->ckpt_area_pba
));
1232 printf(" First member lba : %u\n",
1233 __le32_to_cpu(migr_rec
->dest_1st_member_lba
));
1234 printf(" Total Number of Units : %u\n",
1235 __le32_to_cpu(migr_rec
->num_migr_units
));
1236 printf(" Size of volume : %u\n",
1237 __le32_to_cpu(migr_rec
->post_migr_vol_cap
));
1238 printf(" Expansion space for LBA64 : %u\n",
1239 __le32_to_cpu(migr_rec
->post_migr_vol_cap_hi
));
1240 printf(" Record was read from : %u\n",
1241 __le32_to_cpu(migr_rec
->ckpt_read_disk_num
));
1246 #endif /* MDASSEMBLE */
1247 /*******************************************************************************
1248 * function: imsm_check_attributes
1249 * Description: Function checks if features represented by attributes flags
1250 * are supported by mdadm.
1252 * attributes - Attributes read from metadata
1254 * 0 - passed attributes contains unsupported features flags
1255 * 1 - all features are supported
1256 ******************************************************************************/
1257 static int imsm_check_attributes(__u32 attributes
)
1260 __u32 not_supported
= MPB_ATTRIB_SUPPORTED
^0xffffffff;
1262 not_supported
&= ~MPB_ATTRIB_IGNORED
;
1264 not_supported
&= attributes
;
1265 if (not_supported
) {
1266 fprintf(stderr
, Name
"(IMSM): Unsupported attributes : %x\n",
1267 (unsigned)__le32_to_cpu(not_supported
));
1268 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1269 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
1270 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1272 if (not_supported
& MPB_ATTRIB_2TB
) {
1273 dprintf("\t\tMPB_ATTRIB_2TB\n");
1274 not_supported
^= MPB_ATTRIB_2TB
;
1276 if (not_supported
& MPB_ATTRIB_RAID0
) {
1277 dprintf("\t\tMPB_ATTRIB_RAID0\n");
1278 not_supported
^= MPB_ATTRIB_RAID0
;
1280 if (not_supported
& MPB_ATTRIB_RAID1
) {
1281 dprintf("\t\tMPB_ATTRIB_RAID1\n");
1282 not_supported
^= MPB_ATTRIB_RAID1
;
1284 if (not_supported
& MPB_ATTRIB_RAID10
) {
1285 dprintf("\t\tMPB_ATTRIB_RAID10\n");
1286 not_supported
^= MPB_ATTRIB_RAID10
;
1288 if (not_supported
& MPB_ATTRIB_RAID1E
) {
1289 dprintf("\t\tMPB_ATTRIB_RAID1E\n");
1290 not_supported
^= MPB_ATTRIB_RAID1E
;
1292 if (not_supported
& MPB_ATTRIB_RAID5
) {
1293 dprintf("\t\tMPB_ATTRIB_RAID5\n");
1294 not_supported
^= MPB_ATTRIB_RAID5
;
1296 if (not_supported
& MPB_ATTRIB_RAIDCNG
) {
1297 dprintf("\t\tMPB_ATTRIB_RAIDCNG\n");
1298 not_supported
^= MPB_ATTRIB_RAIDCNG
;
1300 if (not_supported
& MPB_ATTRIB_BBM
) {
1301 dprintf("\t\tMPB_ATTRIB_BBM\n");
1302 not_supported
^= MPB_ATTRIB_BBM
;
1304 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1305 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n");
1306 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1308 if (not_supported
& MPB_ATTRIB_EXP_STRIPE_SIZE
) {
1309 dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n");
1310 not_supported
^= MPB_ATTRIB_EXP_STRIPE_SIZE
;
1312 if (not_supported
& MPB_ATTRIB_2TB_DISK
) {
1313 dprintf("\t\tMPB_ATTRIB_2TB_DISK\n");
1314 not_supported
^= MPB_ATTRIB_2TB_DISK
;
1316 if (not_supported
& MPB_ATTRIB_NEVER_USE2
) {
1317 dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n");
1318 not_supported
^= MPB_ATTRIB_NEVER_USE2
;
1320 if (not_supported
& MPB_ATTRIB_NEVER_USE
) {
1321 dprintf("\t\tMPB_ATTRIB_NEVER_USE\n");
1322 not_supported
^= MPB_ATTRIB_NEVER_USE
;
1326 dprintf(Name
"(IMSM): Unknown attributes : %x\n", not_supported
);
1335 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
);
1337 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
1339 struct intel_super
*super
= st
->sb
;
1340 struct imsm_super
*mpb
= super
->anchor
;
1341 char str
[MAX_SIGNATURE_LENGTH
];
1346 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
1349 snprintf(str
, MPB_SIG_LEN
, "%s", mpb
->sig
);
1350 printf(" Magic : %s\n", str
);
1351 snprintf(str
, strlen(MPB_VERSION_RAID0
), "%s", get_imsm_version(mpb
));
1352 printf(" Version : %s\n", get_imsm_version(mpb
));
1353 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
1354 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
1355 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
1356 printf(" Attributes : ");
1357 if (imsm_check_attributes(mpb
->attributes
))
1358 printf("All supported\n");
1360 printf("not supported\n");
1361 getinfo_super_imsm(st
, &info
, NULL
);
1362 fname_from_uuid(st
, &info
, nbuf
, ':');
1363 printf(" UUID : %s\n", nbuf
+ 5);
1364 sum
= __le32_to_cpu(mpb
->check_sum
);
1365 printf(" Checksum : %08x %s\n", sum
,
1366 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
1367 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
));
1368 printf(" Disks : %d\n", mpb
->num_disks
);
1369 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
1370 print_imsm_disk(__get_imsm_disk(mpb
, super
->disks
->index
), super
->disks
->index
, reserved
);
1371 if (super
->bbm_log
) {
1372 struct bbm_log
*log
= super
->bbm_log
;
1375 printf("Bad Block Management Log:\n");
1376 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
1377 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
1378 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
1379 printf(" Spare Blocks : %d\n", __le32_to_cpu(log
->reserved_spare_block_count
));
1380 printf(" First Spare : %llx\n",
1381 (unsigned long long) __le64_to_cpu(log
->first_spare_lba
));
1383 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1385 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1387 super
->current_vol
= i
;
1388 getinfo_super_imsm(st
, &info
, NULL
);
1389 fname_from_uuid(st
, &info
, nbuf
, ':');
1390 print_imsm_dev(super
, dev
, nbuf
+ 5, super
->disks
->index
);
1392 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1393 if (i
== super
->disks
->index
)
1395 print_imsm_disk(__get_imsm_disk(mpb
, i
), i
, reserved
);
1398 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
1399 if (dl
->index
== -1)
1400 print_imsm_disk(&dl
->disk
, -1, reserved
);
1402 examine_migr_rec_imsm(super
);
1405 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
1407 /* We just write a generic IMSM ARRAY entry */
1410 struct intel_super
*super
= st
->sb
;
1412 if (!super
->anchor
->num_raid_devs
) {
1413 printf("ARRAY metadata=imsm\n");
1417 getinfo_super_imsm(st
, &info
, NULL
);
1418 fname_from_uuid(st
, &info
, nbuf
, ':');
1419 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
1422 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
1424 /* We just write a generic IMSM ARRAY entry */
1428 struct intel_super
*super
= st
->sb
;
1431 if (!super
->anchor
->num_raid_devs
)
1434 getinfo_super_imsm(st
, &info
, NULL
);
1435 fname_from_uuid(st
, &info
, nbuf
, ':');
1436 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1437 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1439 super
->current_vol
= i
;
1440 getinfo_super_imsm(st
, &info
, NULL
);
1441 fname_from_uuid(st
, &info
, nbuf1
, ':');
1442 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
1443 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
1447 static void export_examine_super_imsm(struct supertype
*st
)
1449 struct intel_super
*super
= st
->sb
;
1450 struct imsm_super
*mpb
= super
->anchor
;
1454 getinfo_super_imsm(st
, &info
, NULL
);
1455 fname_from_uuid(st
, &info
, nbuf
, ':');
1456 printf("MD_METADATA=imsm\n");
1457 printf("MD_LEVEL=container\n");
1458 printf("MD_UUID=%s\n", nbuf
+5);
1459 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
1462 static void detail_super_imsm(struct supertype
*st
, char *homehost
)
1467 getinfo_super_imsm(st
, &info
, NULL
);
1468 fname_from_uuid(st
, &info
, nbuf
, ':');
1469 printf("\n UUID : %s\n", nbuf
+ 5);
1472 static void brief_detail_super_imsm(struct supertype
*st
)
1476 getinfo_super_imsm(st
, &info
, NULL
);
1477 fname_from_uuid(st
, &info
, nbuf
, ':');
1478 printf(" UUID=%s", nbuf
+ 5);
1481 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
);
1482 static void fd2devname(int fd
, char *name
);
1484 static int ahci_enumerate_ports(const char *hba_path
, int port_count
, int host_base
, int verbose
)
1486 /* dump an unsorted list of devices attached to AHCI Intel storage
1487 * controller, as well as non-connected ports
1489 int hba_len
= strlen(hba_path
) + 1;
1494 unsigned long port_mask
= (1 << port_count
) - 1;
1496 if (port_count
> (int)sizeof(port_mask
) * 8) {
1498 fprintf(stderr
, Name
": port_count %d out of range\n", port_count
);
1502 /* scroll through /sys/dev/block looking for devices attached to
1505 dir
= opendir("/sys/dev/block");
1506 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
1517 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
1519 path
= devt_to_devpath(makedev(major
, minor
));
1522 if (!path_attached_to_hba(path
, hba_path
)) {
1528 /* retrieve the scsi device type */
1529 if (asprintf(&device
, "/sys/dev/block/%d:%d/device/xxxxxxx", major
, minor
) < 0) {
1531 fprintf(stderr
, Name
": failed to allocate 'device'\n");
1535 sprintf(device
, "/sys/dev/block/%d:%d/device/type", major
, minor
);
1536 if (load_sys(device
, buf
) != 0) {
1538 fprintf(stderr
, Name
": failed to read device type for %s\n",
1544 type
= strtoul(buf
, NULL
, 10);
1546 /* if it's not a disk print the vendor and model */
1547 if (!(type
== 0 || type
== 7 || type
== 14)) {
1550 sprintf(device
, "/sys/dev/block/%d:%d/device/vendor", major
, minor
);
1551 if (load_sys(device
, buf
) == 0) {
1552 strncpy(vendor
, buf
, sizeof(vendor
));
1553 vendor
[sizeof(vendor
) - 1] = '\0';
1554 c
= (char *) &vendor
[sizeof(vendor
) - 1];
1555 while (isspace(*c
) || *c
== '\0')
1559 sprintf(device
, "/sys/dev/block/%d:%d/device/model", major
, minor
);
1560 if (load_sys(device
, buf
) == 0) {
1561 strncpy(model
, buf
, sizeof(model
));
1562 model
[sizeof(model
) - 1] = '\0';
1563 c
= (char *) &model
[sizeof(model
) - 1];
1564 while (isspace(*c
) || *c
== '\0')
1568 if (vendor
[0] && model
[0])
1569 sprintf(buf
, "%.64s %.64s", vendor
, model
);
1571 switch (type
) { /* numbers from hald/linux/device.c */
1572 case 1: sprintf(buf
, "tape"); break;
1573 case 2: sprintf(buf
, "printer"); break;
1574 case 3: sprintf(buf
, "processor"); break;
1576 case 5: sprintf(buf
, "cdrom"); break;
1577 case 6: sprintf(buf
, "scanner"); break;
1578 case 8: sprintf(buf
, "media_changer"); break;
1579 case 9: sprintf(buf
, "comm"); break;
1580 case 12: sprintf(buf
, "raid"); break;
1581 default: sprintf(buf
, "unknown");
1587 /* chop device path to 'host%d' and calculate the port number */
1588 c
= strchr(&path
[hba_len
], '/');
1591 fprintf(stderr
, Name
": %s - invalid path name\n", path
+ hba_len
);
1596 if (sscanf(&path
[hba_len
], "host%d", &port
) == 1)
1600 *c
= '/'; /* repair the full string */
1601 fprintf(stderr
, Name
": failed to determine port number for %s\n",
1608 /* mark this port as used */
1609 port_mask
&= ~(1 << port
);
1611 /* print out the device information */
1613 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
1617 fd
= dev_open(ent
->d_name
, O_RDONLY
);
1619 printf(" Port%d : - disk info unavailable -\n", port
);
1621 fd2devname(fd
, buf
);
1622 printf(" Port%d : %s", port
, buf
);
1623 if (imsm_read_serial(fd
, NULL
, (__u8
*) buf
) == 0)
1624 printf(" (%.*s)\n", MAX_RAID_SERIAL_LEN
, buf
);
1639 for (i
= 0; i
< port_count
; i
++)
1640 if (port_mask
& (1 << i
))
1641 printf(" Port%d : - no device attached -\n", i
);
1647 static void print_found_intel_controllers(struct sys_dev
*elem
)
1649 for (; elem
; elem
= elem
->next
) {
1650 fprintf(stderr
, Name
": found Intel(R) ");
1651 if (elem
->type
== SYS_DEV_SATA
)
1652 fprintf(stderr
, "SATA ");
1653 else if (elem
->type
== SYS_DEV_SAS
)
1654 fprintf(stderr
, "SAS ");
1655 fprintf(stderr
, "RAID controller");
1657 fprintf(stderr
, " at %s", elem
->pci_id
);
1658 fprintf(stderr
, ".\n");
1663 static int ahci_get_port_count(const char *hba_path
, int *port_count
)
1670 if ((dir
= opendir(hba_path
)) == NULL
)
1673 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
1676 if (sscanf(ent
->d_name
, "host%d", &host
) != 1)
1678 if (*port_count
== 0)
1680 else if (host
< host_base
)
1683 if (host
+ 1 > *port_count
+ host_base
)
1684 *port_count
= host
+ 1 - host_base
;
1690 static void print_imsm_capability(const struct imsm_orom
*orom
)
1692 printf(" Platform : Intel(R) Matrix Storage Manager\n");
1693 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
1694 orom
->hotfix_ver
, orom
->build
);
1695 printf(" RAID Levels :%s%s%s%s%s\n",
1696 imsm_orom_has_raid0(orom
) ? " raid0" : "",
1697 imsm_orom_has_raid1(orom
) ? " raid1" : "",
1698 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
1699 imsm_orom_has_raid10(orom
) ? " raid10" : "",
1700 imsm_orom_has_raid5(orom
) ? " raid5" : "");
1701 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1702 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
1703 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
1704 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
1705 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
1706 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
1707 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
1708 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
1709 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
1710 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
1711 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
1712 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
1713 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
1714 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
1715 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
1716 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
1717 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
1718 printf(" Max Disks : %d\n", orom
->tds
);
1719 printf(" Max Volumes : %d\n", orom
->vpa
);
1723 static int detail_platform_imsm(int verbose
, int enumerate_only
)
1725 /* There are two components to imsm platform support, the ahci SATA
1726 * controller and the option-rom. To find the SATA controller we
1727 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
1728 * controller with the Intel vendor id is present. This approach
1729 * allows mdadm to leverage the kernel's ahci detection logic, with the
1730 * caveat that if ahci.ko is not loaded mdadm will not be able to
1731 * detect platform raid capabilities. The option-rom resides in a
1732 * platform "Adapter ROM". We scan for its signature to retrieve the
1733 * platform capabilities. If raid support is disabled in the BIOS the
1734 * option-rom capability structure will not be available.
1736 const struct imsm_orom
*orom
;
1737 struct sys_dev
*list
, *hba
;
1742 if (enumerate_only
) {
1743 if (check_env("IMSM_NO_PLATFORM"))
1745 list
= find_intel_devices();
1748 for (hba
= list
; hba
; hba
= hba
->next
) {
1749 orom
= find_imsm_capability(hba
->type
);
1755 free_sys_dev(&list
);
1759 list
= find_intel_devices();
1762 fprintf(stderr
, Name
": no active Intel(R) RAID "
1763 "controller found.\n");
1764 free_sys_dev(&list
);
1767 print_found_intel_controllers(list
);
1769 for (hba
= list
; hba
; hba
= hba
->next
) {
1770 orom
= find_imsm_capability(hba
->type
);
1772 fprintf(stderr
, Name
": imsm capabilities not found for controller: %s (type %s)\n",
1773 hba
->path
, get_sys_dev_type(hba
->type
));
1775 print_imsm_capability(orom
);
1778 for (hba
= list
; hba
; hba
= hba
->next
) {
1779 printf(" I/O Controller : %s (%s)\n",
1780 hba
->path
, get_sys_dev_type(hba
->type
));
1782 if (hba
->type
== SYS_DEV_SATA
) {
1783 host_base
= ahci_get_port_count(hba
->path
, &port_count
);
1784 if (ahci_enumerate_ports(hba
->path
, port_count
, host_base
, verbose
)) {
1786 fprintf(stderr
, Name
": failed to enumerate "
1787 "ports on SATA controller at %s.", hba
->pci_id
);
1793 free_sys_dev(&list
);
1798 static int match_home_imsm(struct supertype
*st
, char *homehost
)
1800 /* the imsm metadata format does not specify any host
1801 * identification information. We return -1 since we can never
1802 * confirm nor deny whether a given array is "meant" for this
1803 * host. We rely on compare_super and the 'family_num' fields to
1804 * exclude member disks that do not belong, and we rely on
1805 * mdadm.conf to specify the arrays that should be assembled.
1806 * Auto-assembly may still pick up "foreign" arrays.
1812 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
1814 /* The uuid returned here is used for:
1815 * uuid to put into bitmap file (Create, Grow)
1816 * uuid for backup header when saving critical section (Grow)
1817 * comparing uuids when re-adding a device into an array
1818 * In these cases the uuid required is that of the data-array,
1819 * not the device-set.
1820 * uuid to recognise same set when adding a missing device back
1821 * to an array. This is a uuid for the device-set.
1823 * For each of these we can make do with a truncated
1824 * or hashed uuid rather than the original, as long as
1826 * In each case the uuid required is that of the data-array,
1827 * not the device-set.
1829 /* imsm does not track uuid's so we synthesis one using sha1 on
1830 * - The signature (Which is constant for all imsm array, but no matter)
1831 * - the orig_family_num of the container
1832 * - the index number of the volume
1833 * - the 'serial' number of the volume.
1834 * Hopefully these are all constant.
1836 struct intel_super
*super
= st
->sb
;
1839 struct sha1_ctx ctx
;
1840 struct imsm_dev
*dev
= NULL
;
1843 /* some mdadm versions failed to set ->orig_family_num, in which
1844 * case fall back to ->family_num. orig_family_num will be
1845 * fixed up with the first metadata update.
1847 family_num
= super
->anchor
->orig_family_num
;
1848 if (family_num
== 0)
1849 family_num
= super
->anchor
->family_num
;
1850 sha1_init_ctx(&ctx
);
1851 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
1852 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
1853 if (super
->current_vol
>= 0)
1854 dev
= get_imsm_dev(super
, super
->current_vol
);
1856 __u32 vol
= super
->current_vol
;
1857 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
1858 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
1860 sha1_finish_ctx(&ctx
, buf
);
1861 memcpy(uuid
, buf
, 4*4);
1866 get_imsm_numerical_version(struct imsm_super
*mpb
, int *m
, int *p
)
1868 __u8
*v
= get_imsm_version(mpb
);
1869 __u8
*end
= mpb
->sig
+ MAX_SIGNATURE_LENGTH
;
1870 char major
[] = { 0, 0, 0 };
1871 char minor
[] = { 0 ,0, 0 };
1872 char patch
[] = { 0, 0, 0 };
1873 char *ver_parse
[] = { major
, minor
, patch
};
1877 while (*v
!= '\0' && v
< end
) {
1878 if (*v
!= '.' && j
< 2)
1879 ver_parse
[i
][j
++] = *v
;
1887 *m
= strtol(minor
, NULL
, 0);
1888 *p
= strtol(patch
, NULL
, 0);
1892 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
1894 /* migr_strip_size when repairing or initializing parity */
1895 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1896 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1898 switch (get_imsm_raid_level(map
)) {
1903 return 128*1024 >> 9;
1907 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
1909 /* migr_strip_size when rebuilding a degraded disk, no idea why
1910 * this is different than migr_strip_size_resync(), but it's good
1913 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
1914 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1916 switch (get_imsm_raid_level(map
)) {
1919 if (map
->num_members
% map
->num_domains
== 0)
1920 return 128*1024 >> 9;
1924 return max((__u32
) 64*1024 >> 9, chunk
);
1926 return 128*1024 >> 9;
1930 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
1932 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
1933 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
1934 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
1935 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
1937 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
1940 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
1942 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
1943 int level
= get_imsm_raid_level(lo
);
1945 if (level
== 1 || level
== 10) {
1946 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
1948 return hi
->num_domains
;
1950 return num_stripes_per_unit_resync(dev
);
1953 static __u8
imsm_num_data_members(struct imsm_dev
*dev
, int second_map
)
1955 /* named 'imsm_' because raid0, raid1 and raid10
1956 * counter-intuitively have the same number of data disks
1958 struct imsm_map
*map
= get_imsm_map(dev
, second_map
);
1960 switch (get_imsm_raid_level(map
)) {
1964 return map
->num_members
;
1966 return map
->num_members
- 1;
1968 dprintf("%s: unsupported raid level\n", __func__
);
1973 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
1975 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1976 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1978 switch(get_imsm_raid_level(map
)) {
1981 return chunk
* map
->num_domains
;
1983 return chunk
* map
->num_members
;
1989 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
1991 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
1992 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1993 __u32 strip
= block
/ chunk
;
1995 switch (get_imsm_raid_level(map
)) {
1998 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
1999 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
2001 return vol_stripe
* chunk
+ block
% chunk
;
2003 __u32 stripe
= strip
/ (map
->num_members
- 1);
2005 return stripe
* chunk
+ block
% chunk
;
2012 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
2013 struct imsm_dev
*dev
)
2015 /* calculate the conversion factor between per member 'blocks'
2016 * (md/{resync,rebuild}_start) and imsm migration units, return
2017 * 0 for the 'not migrating' and 'unsupported migration' cases
2019 if (!dev
->vol
.migr_state
)
2022 switch (migr_type(dev
)) {
2023 case MIGR_GEN_MIGR
: {
2024 struct migr_record
*migr_rec
= super
->migr_rec
;
2025 return __le32_to_cpu(migr_rec
->blocks_per_unit
);
2030 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2031 __u32 stripes_per_unit
;
2032 __u32 blocks_per_unit
;
2041 /* yes, this is really the translation of migr_units to
2042 * per-member blocks in the 'resync' case
2044 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
2045 migr_chunk
= migr_strip_blocks_resync(dev
);
2046 disks
= imsm_num_data_members(dev
, MAP_0
);
2047 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
2048 stripe
= __le16_to_cpu(map
->blocks_per_strip
) * disks
;
2049 segment
= blocks_per_unit
/ stripe
;
2050 block_rel
= blocks_per_unit
- segment
* stripe
;
2051 parity_depth
= parity_segment_depth(dev
);
2052 block_map
= map_migr_block(dev
, block_rel
);
2053 return block_map
+ parity_depth
* segment
;
2055 case MIGR_REBUILD
: {
2056 __u32 stripes_per_unit
;
2059 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
2060 migr_chunk
= migr_strip_blocks_rebuild(dev
);
2061 return migr_chunk
* stripes_per_unit
;
2063 case MIGR_STATE_CHANGE
:
2069 static int imsm_level_to_layout(int level
)
2077 return ALGORITHM_LEFT_ASYMMETRIC
;
2084 /*******************************************************************************
2085 * Function: read_imsm_migr_rec
2086 * Description: Function reads imsm migration record from last sector of disk
2088 * fd : disk descriptor
2089 * super : metadata info
2093 ******************************************************************************/
2094 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
)
2097 unsigned long long dsize
;
2099 get_dev_size(fd
, NULL
, &dsize
);
2100 if (lseek64(fd
, dsize
- MIGR_REC_POSITION
, SEEK_SET
) < 0) {
2102 Name
": Cannot seek to anchor block: %s\n",
2106 if (read(fd
, super
->migr_rec_buf
, MIGR_REC_BUF_SIZE
) !=
2107 MIGR_REC_BUF_SIZE
) {
2109 Name
": Cannot read migr record block: %s\n",
2119 static struct imsm_dev
*imsm_get_device_during_migration(
2120 struct intel_super
*super
)
2123 struct intel_dev
*dv
;
2125 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
2126 if (is_gen_migration(dv
->dev
))
2132 /*******************************************************************************
2133 * Function: load_imsm_migr_rec
2134 * Description: Function reads imsm migration record (it is stored at the last
2137 * super : imsm internal array info
2138 * info : general array info
2142 * -2 : no migration in progress
2143 ******************************************************************************/
2144 static int load_imsm_migr_rec(struct intel_super
*super
, struct mdinfo
*info
)
2147 struct dl
*dl
= NULL
;
2151 struct imsm_dev
*dev
;
2152 struct imsm_map
*map
= NULL
;
2155 /* find map under migration */
2156 dev
= imsm_get_device_during_migration(super
);
2157 /* nothing to load,no migration in progress?
2161 map
= get_imsm_map(dev
, MAP_0
);
2164 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
2165 /* skip spare and failed disks
2167 if (sd
->disk
.raid_disk
< 0)
2169 /* read only from one of the first two slots */
2171 slot
= get_imsm_disk_slot(map
,
2172 sd
->disk
.raid_disk
);
2173 if ((map
== NULL
) || (slot
> 1) || (slot
< 0))
2176 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
2177 fd
= dev_open(nm
, O_RDONLY
);
2183 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
2184 /* skip spare and failed disks
2188 /* read only from one of the first two slots */
2190 slot
= get_imsm_disk_slot(map
, dl
->index
);
2191 if ((map
== NULL
) || (slot
> 1) || (slot
< 0))
2193 sprintf(nm
, "%d:%d", dl
->major
, dl
->minor
);
2194 fd
= dev_open(nm
, O_RDONLY
);
2201 retval
= read_imsm_migr_rec(fd
, super
);
2210 /*******************************************************************************
2211 * function: imsm_create_metadata_checkpoint_update
2212 * Description: It creates update for checkpoint change.
2214 * super : imsm internal array info
2215 * u : pointer to prepared update
2218 * If length is equal to 0, input pointer u contains no update
2219 ******************************************************************************/
2220 static int imsm_create_metadata_checkpoint_update(
2221 struct intel_super
*super
,
2222 struct imsm_update_general_migration_checkpoint
**u
)
2225 int update_memory_size
= 0;
2227 dprintf("imsm_create_metadata_checkpoint_update(enter)\n");
2233 /* size of all update data without anchor */
2234 update_memory_size
=
2235 sizeof(struct imsm_update_general_migration_checkpoint
);
2237 *u
= calloc(1, update_memory_size
);
2239 dprintf("error: cannot get memory for "
2240 "imsm_create_metadata_checkpoint_update update\n");
2243 (*u
)->type
= update_general_migration_checkpoint
;
2244 (*u
)->curr_migr_unit
= __le32_to_cpu(super
->migr_rec
->curr_migr_unit
);
2245 dprintf("imsm_create_metadata_checkpoint_update: prepared for %u\n",
2246 (*u
)->curr_migr_unit
);
2248 return update_memory_size
;
2252 static void imsm_update_metadata_locally(struct supertype
*st
,
2253 void *buf
, int len
);
2255 /*******************************************************************************
2256 * Function: write_imsm_migr_rec
2257 * Description: Function writes imsm migration record
2258 * (at the last sector of disk)
2260 * super : imsm internal array info
2264 ******************************************************************************/
2265 static int write_imsm_migr_rec(struct supertype
*st
)
2267 struct intel_super
*super
= st
->sb
;
2268 unsigned long long dsize
;
2274 struct imsm_update_general_migration_checkpoint
*u
;
2275 struct imsm_dev
*dev
;
2276 struct imsm_map
*map
= NULL
;
2278 /* find map under migration */
2279 dev
= imsm_get_device_during_migration(super
);
2280 /* if no migration, write buffer anyway to clear migr_record
2281 * on disk based on first available device
2284 dev
= get_imsm_dev(super
, super
->current_vol
< 0 ? 0 :
2285 super
->current_vol
);
2287 map
= get_imsm_map(dev
, MAP_0
);
2289 for (sd
= super
->disks
; sd
; sd
= sd
->next
) {
2292 /* skip failed and spare devices */
2295 /* write to 2 first slots only */
2297 slot
= get_imsm_disk_slot(map
, sd
->index
);
2298 if ((map
== NULL
) || (slot
> 1) || (slot
< 0))
2301 sprintf(nm
, "%d:%d", sd
->major
, sd
->minor
);
2302 fd
= dev_open(nm
, O_RDWR
);
2305 get_dev_size(fd
, NULL
, &dsize
);
2306 if (lseek64(fd
, dsize
- MIGR_REC_POSITION
, SEEK_SET
) < 0) {
2308 Name
": Cannot seek to anchor block: %s\n",
2312 if (write(fd
, super
->migr_rec_buf
, MIGR_REC_BUF_SIZE
) !=
2313 MIGR_REC_BUF_SIZE
) {
2315 Name
": Cannot write migr record block: %s\n",
2322 /* update checkpoint information in metadata */
2323 len
= imsm_create_metadata_checkpoint_update(super
, &u
);
2326 dprintf("imsm: Cannot prepare update\n");
2329 /* update metadata locally */
2330 imsm_update_metadata_locally(st
, u
, len
);
2331 /* and possibly remotely */
2332 if (st
->update_tail
) {
2333 append_metadata_update(st
, u
, len
);
2334 /* during reshape we do all work inside metadata handler
2335 * manage_reshape(), so metadata update has to be triggered
2338 flush_metadata_updates(st
);
2339 st
->update_tail
= &st
->updates
;
2349 #endif /* MDASSEMBLE */
2351 /* spare/missing disks activations are not allowe when
2352 * array/container performs reshape operation, because
2353 * all arrays in container works on the same disks set
2355 int imsm_reshape_blocks_arrays_changes(struct intel_super
*super
)
2358 struct intel_dev
*i_dev
;
2359 struct imsm_dev
*dev
;
2361 /* check whole container
2363 for (i_dev
= super
->devlist
; i_dev
; i_dev
= i_dev
->next
) {
2365 if (is_gen_migration(dev
)) {
2366 /* No repair during any migration in container
2375 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
2377 struct intel_super
*super
= st
->sb
;
2378 struct migr_record
*migr_rec
= super
->migr_rec
;
2379 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
2380 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2381 struct imsm_map
*prev_map
= get_imsm_map(dev
, MAP_1
);
2382 struct imsm_map
*map_to_analyse
= map
;
2385 unsigned int component_size_alligment
;
2386 int map_disks
= info
->array
.raid_disks
;
2388 memset(info
, 0, sizeof(*info
));
2390 map_to_analyse
= prev_map
;
2392 dl
= super
->current_disk
;
2394 info
->container_member
= super
->current_vol
;
2395 info
->array
.raid_disks
= map
->num_members
;
2396 info
->array
.level
= get_imsm_raid_level(map_to_analyse
);
2397 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
2398 info
->array
.md_minor
= -1;
2399 info
->array
.ctime
= 0;
2400 info
->array
.utime
= 0;
2401 info
->array
.chunk_size
=
2402 __le16_to_cpu(map_to_analyse
->blocks_per_strip
) << 9;
2403 info
->array
.state
= !dev
->vol
.dirty
;
2404 info
->custom_array_size
= __le32_to_cpu(dev
->size_high
);
2405 info
->custom_array_size
<<= 32;
2406 info
->custom_array_size
|= __le32_to_cpu(dev
->size_low
);
2407 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
2409 if (is_gen_migration(dev
)) {
2410 info
->reshape_active
= 1;
2411 info
->new_level
= get_imsm_raid_level(map
);
2412 info
->new_layout
= imsm_level_to_layout(info
->new_level
);
2413 info
->new_chunk
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
2414 info
->delta_disks
= map
->num_members
- prev_map
->num_members
;
2415 if (info
->delta_disks
) {
2416 /* this needs to be applied to every array
2419 info
->reshape_active
= CONTAINER_RESHAPE
;
2421 /* We shape information that we give to md might have to be
2422 * modify to cope with md's requirement for reshaping arrays.
2423 * For example, when reshaping a RAID0, md requires it to be
2424 * presented as a degraded RAID4.
2425 * Also if a RAID0 is migrating to a RAID5 we need to specify
2426 * the array as already being RAID5, but the 'before' layout
2427 * is a RAID4-like layout.
2429 switch (info
->array
.level
) {
2431 switch(info
->new_level
) {
2433 /* conversion is happening as RAID4 */
2434 info
->array
.level
= 4;
2435 info
->array
.raid_disks
+= 1;
2438 /* conversion is happening as RAID5 */
2439 info
->array
.level
= 5;
2440 info
->array
.layout
= ALGORITHM_PARITY_N
;
2441 info
->delta_disks
-= 1;
2444 /* FIXME error message */
2445 info
->array
.level
= UnSet
;
2451 info
->new_level
= UnSet
;
2452 info
->new_layout
= UnSet
;
2453 info
->new_chunk
= info
->array
.chunk_size
;
2454 info
->delta_disks
= 0;
2458 info
->disk
.major
= dl
->major
;
2459 info
->disk
.minor
= dl
->minor
;
2460 info
->disk
.number
= dl
->index
;
2461 info
->disk
.raid_disk
= get_imsm_disk_slot(map_to_analyse
,
2465 info
->data_offset
= __le32_to_cpu(map_to_analyse
->pba_of_lba0
);
2466 info
->component_size
=
2467 __le32_to_cpu(map_to_analyse
->blocks_per_member
);
2469 /* check component size aligment
2471 component_size_alligment
=
2472 info
->component_size
% (info
->array
.chunk_size
/512);
2474 if (component_size_alligment
&&
2475 (info
->array
.level
!= 1) && (info
->array
.level
!= UnSet
)) {
2476 dprintf("imsm: reported component size alligned from %llu ",
2477 info
->component_size
);
2478 info
->component_size
-= component_size_alligment
;
2479 dprintf("to %llu (%i).\n",
2480 info
->component_size
, component_size_alligment
);
2483 memset(info
->uuid
, 0, sizeof(info
->uuid
));
2484 info
->recovery_start
= MaxSector
;
2486 info
->reshape_progress
= 0;
2487 info
->resync_start
= MaxSector
;
2488 if ((map_to_analyse
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
2490 imsm_reshape_blocks_arrays_changes(super
) == 0) {
2491 info
->resync_start
= 0;
2493 if (dev
->vol
.migr_state
) {
2494 switch (migr_type(dev
)) {
2497 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
2499 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
2501 info
->resync_start
= blocks_per_unit
* units
;
2504 case MIGR_GEN_MIGR
: {
2505 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
2507 __u64 units
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
2508 unsigned long long array_blocks
;
2511 if (__le32_to_cpu(migr_rec
->ascending_migr
) &&
2513 (__le32_to_cpu(migr_rec
->num_migr_units
)-1)) &&
2514 (super
->migr_rec
->rec_status
==
2515 __cpu_to_le32(UNIT_SRC_IN_CP_AREA
)))
2518 info
->reshape_progress
= blocks_per_unit
* units
;
2520 dprintf("IMSM: General Migration checkpoint : %llu "
2521 "(%llu) -> read reshape progress : %llu\n",
2522 (unsigned long long)units
,
2523 (unsigned long long)blocks_per_unit
,
2524 info
->reshape_progress
);
2526 used_disks
= imsm_num_data_members(dev
, MAP_1
);
2527 if (used_disks
> 0) {
2528 array_blocks
= map
->blocks_per_member
*
2530 /* round array size down to closest MB
2532 info
->custom_array_size
= (array_blocks
2533 >> SECT_PER_MB_SHIFT
)
2534 << SECT_PER_MB_SHIFT
;
2538 /* we could emulate the checkpointing of
2539 * 'sync_action=check' migrations, but for now
2540 * we just immediately complete them
2543 /* this is handled by container_content_imsm() */
2544 case MIGR_STATE_CHANGE
:
2545 /* FIXME handle other migrations */
2547 /* we are not dirty, so... */
2548 info
->resync_start
= MaxSector
;
2552 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
2553 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
2555 info
->array
.major_version
= -1;
2556 info
->array
.minor_version
= -2;
2557 devname
= devnum2devname(st
->container_dev
);
2558 *info
->text_version
= '\0';
2560 sprintf(info
->text_version
, "/%s/%d", devname
, info
->container_member
);
2562 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
2563 uuid_from_super_imsm(st
, info
->uuid
);
2567 for (i
=0; i
<map_disks
; i
++) {
2569 if (i
< info
->array
.raid_disks
) {
2570 struct imsm_disk
*dsk
;
2571 j
= get_imsm_disk_idx(dev
, i
, MAP_X
);
2572 dsk
= get_imsm_disk(super
, j
);
2573 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
2580 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
2581 int failed
, int look_in_map
);
2583 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
2588 static void manage_second_map(struct intel_super
*super
, struct imsm_dev
*dev
)
2590 if (is_gen_migration(dev
)) {
2593 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
2595 failed
= imsm_count_failed(super
, dev
, MAP_1
);
2596 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_1
);
2597 if (map2
->map_state
!= map_state
) {
2598 map2
->map_state
= map_state
;
2599 super
->updates_pending
++;
2605 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
2609 for (d
= super
->missing
; d
; d
= d
->next
)
2610 if (d
->index
== index
)
2615 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
2617 struct intel_super
*super
= st
->sb
;
2618 struct imsm_disk
*disk
;
2619 int map_disks
= info
->array
.raid_disks
;
2620 int max_enough
= -1;
2622 struct imsm_super
*mpb
;
2624 if (super
->current_vol
>= 0) {
2625 getinfo_super_imsm_volume(st
, info
, map
);
2628 memset(info
, 0, sizeof(*info
));
2630 /* Set raid_disks to zero so that Assemble will always pull in valid
2633 info
->array
.raid_disks
= 0;
2634 info
->array
.level
= LEVEL_CONTAINER
;
2635 info
->array
.layout
= 0;
2636 info
->array
.md_minor
= -1;
2637 info
->array
.ctime
= 0; /* N/A for imsm */
2638 info
->array
.utime
= 0;
2639 info
->array
.chunk_size
= 0;
2641 info
->disk
.major
= 0;
2642 info
->disk
.minor
= 0;
2643 info
->disk
.raid_disk
= -1;
2644 info
->reshape_active
= 0;
2645 info
->array
.major_version
= -1;
2646 info
->array
.minor_version
= -2;
2647 strcpy(info
->text_version
, "imsm");
2648 info
->safe_mode_delay
= 0;
2649 info
->disk
.number
= -1;
2650 info
->disk
.state
= 0;
2652 info
->recovery_start
= MaxSector
;
2653 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
2655 /* do we have the all the insync disks that we expect? */
2656 mpb
= super
->anchor
;
2658 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
2659 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
2660 int failed
, enough
, j
, missing
= 0;
2661 struct imsm_map
*map
;
2664 failed
= imsm_count_failed(super
, dev
, MAP_0
);
2665 state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
2666 map
= get_imsm_map(dev
, MAP_0
);
2668 /* any newly missing disks?
2669 * (catches single-degraded vs double-degraded)
2671 for (j
= 0; j
< map
->num_members
; j
++) {
2672 __u32 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
2673 __u32 idx
= ord_to_idx(ord
);
2675 if (!(ord
& IMSM_ORD_REBUILD
) &&
2676 get_imsm_missing(super
, idx
)) {
2682 if (state
== IMSM_T_STATE_FAILED
)
2684 else if (state
== IMSM_T_STATE_DEGRADED
&&
2685 (state
!= map
->map_state
|| missing
))
2687 else /* we're normal, or already degraded */
2689 if (is_gen_migration(dev
) && missing
) {
2690 /* during general migration we need all disks
2691 * that process is running on.
2692 * No new missing disk is allowed.
2696 /* no more checks necessary
2700 /* in the missing/failed disk case check to see
2701 * if at least one array is runnable
2703 max_enough
= max(max_enough
, enough
);
2705 dprintf("%s: enough: %d\n", __func__
, max_enough
);
2706 info
->container_enough
= max_enough
;
2709 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
2711 disk
= &super
->disks
->disk
;
2712 info
->data_offset
= __le32_to_cpu(disk
->total_blocks
) - reserved
;
2713 info
->component_size
= reserved
;
2714 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
2715 /* we don't change info->disk.raid_disk here because
2716 * this state will be finalized in mdmon after we have
2717 * found the 'most fresh' version of the metadata
2719 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
2720 info
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
2723 /* only call uuid_from_super_imsm when this disk is part of a populated container,
2724 * ->compare_super may have updated the 'num_raid_devs' field for spares
2726 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
2727 uuid_from_super_imsm(st
, info
->uuid
);
2729 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
2731 /* I don't know how to compute 'map' on imsm, so use safe default */
2734 for (i
= 0; i
< map_disks
; i
++)
2740 /* allocates memory and fills disk in mdinfo structure
2741 * for each disk in array */
2742 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
2744 struct mdinfo
*mddev
= NULL
;
2745 struct intel_super
*super
= st
->sb
;
2746 struct imsm_disk
*disk
;
2749 if (!super
|| !super
->disks
)
2752 mddev
= malloc(sizeof(*mddev
));
2754 fprintf(stderr
, Name
": Failed to allocate memory.\n");
2757 memset(mddev
, 0, sizeof(*mddev
));
2761 tmp
= malloc(sizeof(*tmp
));
2763 fprintf(stderr
, Name
": Failed to allocate memory.\n");
2768 memset(tmp
, 0, sizeof(*tmp
));
2770 tmp
->next
= mddev
->devs
;
2772 tmp
->disk
.number
= count
++;
2773 tmp
->disk
.major
= dl
->major
;
2774 tmp
->disk
.minor
= dl
->minor
;
2775 tmp
->disk
.state
= is_configured(disk
) ?
2776 (1 << MD_DISK_ACTIVE
) : 0;
2777 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
2778 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
2779 tmp
->disk
.raid_disk
= -1;
2785 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
2786 char *update
, char *devname
, int verbose
,
2787 int uuid_set
, char *homehost
)
2789 /* For 'assemble' and 'force' we need to return non-zero if any
2790 * change was made. For others, the return value is ignored.
2791 * Update options are:
2792 * force-one : This device looks a bit old but needs to be included,
2793 * update age info appropriately.
2794 * assemble: clear any 'faulty' flag to allow this device to
2796 * force-array: Array is degraded but being forced, mark it clean
2797 * if that will be needed to assemble it.
2799 * newdev: not used ????
2800 * grow: Array has gained a new device - this is currently for
2802 * resync: mark as dirty so a resync will happen.
2803 * name: update the name - preserving the homehost
2804 * uuid: Change the uuid of the array to match watch is given
2806 * Following are not relevant for this imsm:
2807 * sparc2.2 : update from old dodgey metadata
2808 * super-minor: change the preferred_minor number
2809 * summaries: update redundant counters.
2810 * homehost: update the recorded homehost
2811 * _reshape_progress: record new reshape_progress position.
2814 struct intel_super
*super
= st
->sb
;
2815 struct imsm_super
*mpb
;
2817 /* we can only update container info */
2818 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
2821 mpb
= super
->anchor
;
2823 if (strcmp(update
, "uuid") == 0) {
2824 /* We take this to mean that the family_num should be updated.
2825 * However that is much smaller than the uuid so we cannot really
2826 * allow an explicit uuid to be given. And it is hard to reliably
2828 * So if !uuid_set we know the current uuid is random and just used
2829 * the first 'int' and copy it to the other 3 positions.
2830 * Otherwise we require the 4 'int's to be the same as would be the
2831 * case if we are using a random uuid. So an explicit uuid will be
2832 * accepted as long as all for ints are the same... which shouldn't hurt
2835 info
->uuid
[1] = info
->uuid
[2] = info
->uuid
[3] = info
->uuid
[0];
2838 if (info
->uuid
[0] != info
->uuid
[1] ||
2839 info
->uuid
[1] != info
->uuid
[2] ||
2840 info
->uuid
[2] != info
->uuid
[3])
2846 mpb
->orig_family_num
= info
->uuid
[0];
2847 } else if (strcmp(update
, "assemble") == 0)
2852 /* successful update? recompute checksum */
2854 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
2859 static size_t disks_to_mpb_size(int disks
)
2863 size
= sizeof(struct imsm_super
);
2864 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
2865 size
+= 2 * sizeof(struct imsm_dev
);
2866 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
2867 size
+= (4 - 2) * sizeof(struct imsm_map
);
2868 /* 4 possible disk_ord_tbl's */
2869 size
+= 4 * (disks
- 1) * sizeof(__u32
);
2874 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
)
2876 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
2879 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
2882 static void free_devlist(struct intel_super
*super
)
2884 struct intel_dev
*dv
;
2886 while (super
->devlist
) {
2887 dv
= super
->devlist
->next
;
2888 free(super
->devlist
->dev
);
2889 free(super
->devlist
);
2890 super
->devlist
= dv
;
2894 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
2896 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
2899 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
)
2903 * 0 same, or first was empty, and second was copied
2904 * 1 second had wrong number
2906 * 3 wrong other info
2908 struct intel_super
*first
= st
->sb
;
2909 struct intel_super
*sec
= tst
->sb
;
2916 /* in platform dependent environment test if the disks
2917 * use the same Intel hba
2919 if (!check_env("IMSM_NO_PLATFORM")) {
2920 if (!first
->hba
|| !sec
->hba
||
2921 (first
->hba
->type
!= sec
->hba
->type
)) {
2923 "HBAs of devices does not match %s != %s\n",
2924 first
->hba
? get_sys_dev_type(first
->hba
->type
) : NULL
,
2925 sec
->hba
? get_sys_dev_type(sec
->hba
->type
) : NULL
);
2930 /* if an anchor does not have num_raid_devs set then it is a free
2933 if (first
->anchor
->num_raid_devs
> 0 &&
2934 sec
->anchor
->num_raid_devs
> 0) {
2935 /* Determine if these disks might ever have been
2936 * related. Further disambiguation can only take place
2937 * in load_super_imsm_all
2939 __u32 first_family
= first
->anchor
->orig_family_num
;
2940 __u32 sec_family
= sec
->anchor
->orig_family_num
;
2942 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
2943 MAX_SIGNATURE_LENGTH
) != 0)
2946 if (first_family
== 0)
2947 first_family
= first
->anchor
->family_num
;
2948 if (sec_family
== 0)
2949 sec_family
= sec
->anchor
->family_num
;
2951 if (first_family
!= sec_family
)
2957 /* if 'first' is a spare promote it to a populated mpb with sec's
2960 if (first
->anchor
->num_raid_devs
== 0 &&
2961 sec
->anchor
->num_raid_devs
> 0) {
2963 struct intel_dev
*dv
;
2964 struct imsm_dev
*dev
;
2966 /* we need to copy raid device info from sec if an allocation
2967 * fails here we don't associate the spare
2969 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++) {
2970 dv
= malloc(sizeof(*dv
));
2973 dev
= malloc(sizeof_imsm_dev(get_imsm_dev(sec
, i
), 1));
2980 dv
->next
= first
->devlist
;
2981 first
->devlist
= dv
;
2983 if (i
< sec
->anchor
->num_raid_devs
) {
2984 /* allocation failure */
2985 free_devlist(first
);
2986 fprintf(stderr
, "imsm: failed to associate spare\n");
2989 first
->anchor
->num_raid_devs
= sec
->anchor
->num_raid_devs
;
2990 first
->anchor
->orig_family_num
= sec
->anchor
->orig_family_num
;
2991 first
->anchor
->family_num
= sec
->anchor
->family_num
;
2992 memcpy(first
->anchor
->sig
, sec
->anchor
->sig
, MAX_SIGNATURE_LENGTH
);
2993 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++)
2994 imsm_copy_dev(get_imsm_dev(first
, i
), get_imsm_dev(sec
, i
));
3000 static void fd2devname(int fd
, char *name
)
3004 char dname
[PATH_MAX
];
3009 if (fstat(fd
, &st
) != 0)
3011 sprintf(path
, "/sys/dev/block/%d:%d",
3012 major(st
.st_rdev
), minor(st
.st_rdev
));
3014 rv
= readlink(path
, dname
, sizeof(dname
)-1);
3019 nm
= strrchr(dname
, '/');
3022 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
3026 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
3028 static int imsm_read_serial(int fd
, char *devname
,
3029 __u8 serial
[MAX_RAID_SERIAL_LEN
])
3031 unsigned char scsi_serial
[255];
3040 memset(scsi_serial
, 0, sizeof(scsi_serial
));
3042 rv
= scsi_get_serial(fd
, scsi_serial
, sizeof(scsi_serial
));
3044 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
3045 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3046 fd2devname(fd
, (char *) serial
);
3053 Name
": Failed to retrieve serial for %s\n",
3058 rsp_len
= scsi_serial
[3];
3062 Name
": Failed to retrieve serial for %s\n",
3066 rsp_buf
= (char *) &scsi_serial
[4];
3068 /* trim all whitespace and non-printable characters and convert
3071 for (i
= 0, dest
= rsp_buf
; i
< rsp_len
; i
++) {
3074 /* ':' is reserved for use in placeholder serial
3075 * numbers for missing disks
3083 len
= dest
- rsp_buf
;
3086 /* truncate leading characters */
3087 if (len
> MAX_RAID_SERIAL_LEN
) {
3088 dest
+= len
- MAX_RAID_SERIAL_LEN
;
3089 len
= MAX_RAID_SERIAL_LEN
;
3092 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3093 memcpy(serial
, dest
, len
);
3098 static int serialcmp(__u8
*s1
, __u8
*s2
)
3100 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
3103 static void serialcpy(__u8
*dest
, __u8
*src
)
3105 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
3108 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
3112 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3113 if (serialcmp(dl
->serial
, serial
) == 0)
3119 static struct imsm_disk
*
3120 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
3124 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3125 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3127 if (serialcmp(disk
->serial
, serial
) == 0) {
3138 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3140 struct imsm_disk
*disk
;
3145 __u8 serial
[MAX_RAID_SERIAL_LEN
];
3147 rv
= imsm_read_serial(fd
, devname
, serial
);
3152 dl
= calloc(1, sizeof(*dl
));
3156 Name
": failed to allocate disk buffer for %s\n",
3162 dl
->major
= major(stb
.st_rdev
);
3163 dl
->minor
= minor(stb
.st_rdev
);
3164 dl
->next
= super
->disks
;
3165 dl
->fd
= keep_fd
? fd
: -1;
3166 assert(super
->disks
== NULL
);
3168 serialcpy(dl
->serial
, serial
);
3171 fd2devname(fd
, name
);
3173 dl
->devname
= strdup(devname
);
3175 dl
->devname
= strdup(name
);
3177 /* look up this disk's index in the current anchor */
3178 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
3181 /* only set index on disks that are a member of a
3182 * populated contianer, i.e. one with raid_devs
3184 if (is_failed(&dl
->disk
))
3186 else if (is_spare(&dl
->disk
))
3194 /* When migrating map0 contains the 'destination' state while map1
3195 * contains the current state. When not migrating map0 contains the
3196 * current state. This routine assumes that map[0].map_state is set to
3197 * the current array state before being called.
3199 * Migration is indicated by one of the following states
3200 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
3201 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
3202 * map1state=unitialized)
3203 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
3205 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
3206 * map1state=degraded)
3207 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
3210 static void migrate(struct imsm_dev
*dev
, struct intel_super
*super
,
3211 __u8 to_state
, int migr_type
)
3213 struct imsm_map
*dest
;
3214 struct imsm_map
*src
= get_imsm_map(dev
, MAP_0
);
3216 dev
->vol
.migr_state
= 1;
3217 set_migr_type(dev
, migr_type
);
3218 dev
->vol
.curr_migr_unit
= 0;
3219 dest
= get_imsm_map(dev
, MAP_1
);
3221 /* duplicate and then set the target end state in map[0] */
3222 memcpy(dest
, src
, sizeof_imsm_map(src
));
3223 if ((migr_type
== MIGR_REBUILD
) ||
3224 (migr_type
== MIGR_GEN_MIGR
)) {
3228 for (i
= 0; i
< src
->num_members
; i
++) {
3229 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
3230 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
3234 if (migr_type
== MIGR_GEN_MIGR
)
3235 /* Clear migration record */
3236 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
3238 src
->map_state
= to_state
;
3241 static void end_migration(struct imsm_dev
*dev
, struct intel_super
*super
,
3244 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3245 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
== 0 ?
3249 /* merge any IMSM_ORD_REBUILD bits that were not successfully
3250 * completed in the last migration.
3252 * FIXME add support for raid-level-migration
3254 if ((map_state
!= map
->map_state
) && (is_gen_migration(dev
) == 0) &&
3255 (prev
->map_state
!= IMSM_T_STATE_UNINITIALIZED
)) {
3256 /* when final map state is other than expected
3257 * merge maps (not for migration)
3261 for (i
= 0; i
< prev
->num_members
; i
++)
3262 for (j
= 0; j
< map
->num_members
; j
++)
3263 /* during online capacity expansion
3264 * disks position can be changed
3265 * if takeover is used
3267 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
3268 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
3269 map
->disk_ord_tbl
[j
] |=
3270 prev
->disk_ord_tbl
[i
];
3273 failed
= imsm_count_failed(super
, dev
, MAP_0
);
3274 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
3277 dev
->vol
.migr_state
= 0;
3278 set_migr_type(dev
, 0);
3279 dev
->vol
.curr_migr_unit
= 0;
3280 map
->map_state
= map_state
;
3284 static int parse_raid_devices(struct intel_super
*super
)
3287 struct imsm_dev
*dev_new
;
3288 size_t len
, len_migr
;
3290 size_t space_needed
= 0;
3291 struct imsm_super
*mpb
= super
->anchor
;
3293 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3294 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3295 struct intel_dev
*dv
;
3297 len
= sizeof_imsm_dev(dev_iter
, 0);
3298 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
3300 space_needed
+= len_migr
- len
;
3302 dv
= malloc(sizeof(*dv
));
3305 if (max_len
< len_migr
)
3307 if (max_len
> len_migr
)
3308 space_needed
+= max_len
- len_migr
;
3309 dev_new
= malloc(max_len
);
3314 imsm_copy_dev(dev_new
, dev_iter
);
3317 dv
->next
= super
->devlist
;
3318 super
->devlist
= dv
;
3321 /* ensure that super->buf is large enough when all raid devices
3324 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
3327 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
, 512);
3328 if (posix_memalign(&buf
, 512, len
) != 0)
3331 memcpy(buf
, super
->buf
, super
->len
);
3332 memset(buf
+ super
->len
, 0, len
- super
->len
);
3341 /* retrieve a pointer to the bbm log which starts after all raid devices */
3342 struct bbm_log
*__get_imsm_bbm_log(struct imsm_super
*mpb
)
3346 if (__le32_to_cpu(mpb
->bbm_log_size
)) {
3348 ptr
+= mpb
->mpb_size
- __le32_to_cpu(mpb
->bbm_log_size
);
3354 /*******************************************************************************
3355 * Function: check_mpb_migr_compatibility
3356 * Description: Function checks for unsupported migration features:
3357 * - migration optimization area (pba_of_lba0)
3358 * - descending reshape (ascending_migr)
3360 * super : imsm metadata information
3362 * 0 : migration is compatible
3363 * -1 : migration is not compatible
3364 ******************************************************************************/
3365 int check_mpb_migr_compatibility(struct intel_super
*super
)
3367 struct imsm_map
*map0
, *map1
;
3368 struct migr_record
*migr_rec
= super
->migr_rec
;
3371 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3372 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3375 dev_iter
->vol
.migr_state
== 1 &&
3376 dev_iter
->vol
.migr_type
== MIGR_GEN_MIGR
) {
3377 /* This device is migrating */
3378 map0
= get_imsm_map(dev_iter
, MAP_0
);
3379 map1
= get_imsm_map(dev_iter
, MAP_1
);
3380 if (map0
->pba_of_lba0
!= map1
->pba_of_lba0
)
3381 /* migration optimization area was used */
3383 if (migr_rec
->ascending_migr
== 0
3384 && migr_rec
->dest_depth_per_unit
> 0)
3385 /* descending reshape not supported yet */
3392 static void __free_imsm(struct intel_super
*super
, int free_disks
);
3394 /* load_imsm_mpb - read matrix metadata
3395 * allocates super->mpb to be freed by free_imsm
3397 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
3399 unsigned long long dsize
;
3400 unsigned long long sectors
;
3402 struct imsm_super
*anchor
;
3405 get_dev_size(fd
, NULL
, &dsize
);
3409 Name
": %s: device to small for imsm\n",
3414 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0) {
3416 fprintf(stderr
, Name
3417 ": Cannot seek to anchor block on %s: %s\n",
3418 devname
, strerror(errno
));
3422 if (posix_memalign((void**)&anchor
, 512, 512) != 0) {
3425 Name
": Failed to allocate imsm anchor buffer"
3426 " on %s\n", devname
);
3429 if (read(fd
, anchor
, 512) != 512) {
3432 Name
": Cannot read anchor block on %s: %s\n",
3433 devname
, strerror(errno
));
3438 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
3441 Name
": no IMSM anchor on %s\n", devname
);
3446 __free_imsm(super
, 0);
3447 /* reload capability and hba */
3449 /* capability and hba must be updated with new super allocation */
3450 find_intel_hba_capability(fd
, super
, devname
);
3451 super
->len
= ROUND_UP(anchor
->mpb_size
, 512);
3452 if (posix_memalign(&super
->buf
, 512, super
->len
) != 0) {
3455 Name
": unable to allocate %zu byte mpb buffer\n",
3460 memcpy(super
->buf
, anchor
, 512);
3462 sectors
= mpb_sectors(anchor
) - 1;
3465 if (posix_memalign(&super
->migr_rec_buf
, 512, MIGR_REC_BUF_SIZE
) != 0) {
3466 fprintf(stderr
, Name
3467 ": %s could not allocate migr_rec buffer\n", __func__
);
3471 super
->clean_migration_record_by_mdmon
= 0;
3474 check_sum
= __gen_imsm_checksum(super
->anchor
);
3475 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
3478 Name
": IMSM checksum %x != %x on %s\n",
3480 __le32_to_cpu(super
->anchor
->check_sum
),
3488 /* read the extended mpb */
3489 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0) {
3492 Name
": Cannot seek to extended mpb on %s: %s\n",
3493 devname
, strerror(errno
));
3497 if ((unsigned)read(fd
, super
->buf
+ 512, super
->len
- 512) != super
->len
- 512) {
3500 Name
": Cannot read extended mpb on %s: %s\n",
3501 devname
, strerror(errno
));
3505 check_sum
= __gen_imsm_checksum(super
->anchor
);
3506 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
3509 Name
": IMSM checksum %x != %x on %s\n",
3510 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
3515 /* FIXME the BBM log is disk specific so we cannot use this global
3516 * buffer for all disks. Ok for now since we only look at the global
3517 * bbm_log_size parameter to gate assembly
3519 super
->bbm_log
= __get_imsm_bbm_log(super
->anchor
);
3524 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
);
3527 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3531 err
= load_imsm_mpb(fd
, super
, devname
);
3534 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
3537 err
= parse_raid_devices(super
);
3542 static void __free_imsm_disk(struct dl
*d
)
3554 static void free_imsm_disks(struct intel_super
*super
)
3558 while (super
->disks
) {
3560 super
->disks
= d
->next
;
3561 __free_imsm_disk(d
);
3563 while (super
->disk_mgmt_list
) {
3564 d
= super
->disk_mgmt_list
;
3565 super
->disk_mgmt_list
= d
->next
;
3566 __free_imsm_disk(d
);
3568 while (super
->missing
) {
3570 super
->missing
= d
->next
;
3571 __free_imsm_disk(d
);
3576 /* free all the pieces hanging off of a super pointer */
3577 static void __free_imsm(struct intel_super
*super
, int free_disks
)
3579 struct intel_hba
*elem
, *next
;
3585 /* unlink capability description */
3587 if (super
->migr_rec_buf
) {
3588 free(super
->migr_rec_buf
);
3589 super
->migr_rec_buf
= NULL
;
3592 free_imsm_disks(super
);
3593 free_devlist(super
);
3597 free((void *)elem
->path
);
3605 static void free_imsm(struct intel_super
*super
)
3607 __free_imsm(super
, 1);
3611 static void free_super_imsm(struct supertype
*st
)
3613 struct intel_super
*super
= st
->sb
;
3622 static struct intel_super
*alloc_super(void)
3624 struct intel_super
*super
= malloc(sizeof(*super
));
3627 memset(super
, 0, sizeof(*super
));
3628 super
->current_vol
= -1;
3629 super
->create_offset
= ~((__u32
) 0);
3635 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
3637 static int find_intel_hba_capability(int fd
, struct intel_super
*super
, char *devname
)
3639 struct sys_dev
*hba_name
;
3642 if ((fd
< 0) || check_env("IMSM_NO_PLATFORM")) {
3647 hba_name
= find_disk_attached_hba(fd
, NULL
);
3651 Name
": %s is not attached to Intel(R) RAID controller.\n",
3655 rv
= attach_hba_to_super(super
, hba_name
);
3658 struct intel_hba
*hba
= super
->hba
;
3660 fprintf(stderr
, Name
": %s is attached to Intel(R) %s RAID "
3661 "controller (%s),\n"
3662 " but the container is assigned to Intel(R) "
3663 "%s RAID controller (",
3666 hba_name
->pci_id
? : "Err!",
3667 get_sys_dev_type(hba_name
->type
));
3670 fprintf(stderr
, "%s", hba
->pci_id
? : "Err!");
3672 fprintf(stderr
, ", ");
3676 fprintf(stderr
, ").\n"
3677 " Mixing devices attached to different controllers "
3678 "is not allowed.\n");
3680 free_sys_dev(&hba_name
);
3683 super
->orom
= find_imsm_capability(hba_name
->type
);
3684 free_sys_dev(&hba_name
);
3690 /* find_missing - helper routine for load_super_imsm_all that identifies
3691 * disks that have disappeared from the system. This routine relies on
3692 * the mpb being uptodate, which it is at load time.
3694 static int find_missing(struct intel_super
*super
)
3697 struct imsm_super
*mpb
= super
->anchor
;
3699 struct imsm_disk
*disk
;
3701 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3702 disk
= __get_imsm_disk(mpb
, i
);
3703 dl
= serial_to_dl(disk
->serial
, super
);
3707 dl
= malloc(sizeof(*dl
));
3713 dl
->devname
= strdup("missing");
3715 serialcpy(dl
->serial
, disk
->serial
);
3718 dl
->next
= super
->missing
;
3719 super
->missing
= dl
;
3726 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
3728 struct intel_disk
*idisk
= disk_list
;
3731 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
3733 idisk
= idisk
->next
;
3739 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
3740 struct intel_super
*super
,
3741 struct intel_disk
**disk_list
)
3743 struct imsm_disk
*d
= &super
->disks
->disk
;
3744 struct imsm_super
*mpb
= super
->anchor
;
3747 for (i
= 0; i
< tbl_size
; i
++) {
3748 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
3749 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
3751 if (tbl_mpb
->family_num
== mpb
->family_num
) {
3752 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
3753 dprintf("%s: mpb from %d:%d matches %d:%d\n",
3754 __func__
, super
->disks
->major
,
3755 super
->disks
->minor
,
3756 table
[i
]->disks
->major
,
3757 table
[i
]->disks
->minor
);
3761 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
3762 is_configured(d
) == is_configured(tbl_d
)) &&
3763 tbl_mpb
->generation_num
< mpb
->generation_num
) {
3764 /* current version of the mpb is a
3765 * better candidate than the one in
3766 * super_table, but copy over "cross
3767 * generational" status
3769 struct intel_disk
*idisk
;
3771 dprintf("%s: mpb from %d:%d replaces %d:%d\n",
3772 __func__
, super
->disks
->major
,
3773 super
->disks
->minor
,
3774 table
[i
]->disks
->major
,
3775 table
[i
]->disks
->minor
);
3777 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
3778 if (idisk
&& is_failed(&idisk
->disk
))
3779 tbl_d
->status
|= FAILED_DISK
;
3782 struct intel_disk
*idisk
;
3783 struct imsm_disk
*disk
;
3785 /* tbl_mpb is more up to date, but copy
3786 * over cross generational status before
3789 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
3790 if (disk
&& is_failed(disk
))
3791 d
->status
|= FAILED_DISK
;
3793 idisk
= disk_list_get(d
->serial
, *disk_list
);
3796 if (disk
&& is_configured(disk
))
3797 idisk
->disk
.status
|= CONFIGURED_DISK
;
3800 dprintf("%s: mpb from %d:%d prefer %d:%d\n",
3801 __func__
, super
->disks
->major
,
3802 super
->disks
->minor
,
3803 table
[i
]->disks
->major
,
3804 table
[i
]->disks
->minor
);
3812 table
[tbl_size
++] = super
;
3816 /* update/extend the merged list of imsm_disk records */
3817 for (j
= 0; j
< mpb
->num_disks
; j
++) {
3818 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
3819 struct intel_disk
*idisk
;
3821 idisk
= disk_list_get(disk
->serial
, *disk_list
);
3823 idisk
->disk
.status
|= disk
->status
;
3824 if (is_configured(&idisk
->disk
) ||
3825 is_failed(&idisk
->disk
))
3826 idisk
->disk
.status
&= ~(SPARE_DISK
);
3828 idisk
= calloc(1, sizeof(*idisk
));
3831 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
3832 idisk
->disk
= *disk
;
3833 idisk
->next
= *disk_list
;
3837 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
3844 static struct intel_super
*
3845 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
3848 struct imsm_super
*mpb
= super
->anchor
;
3852 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3853 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3854 struct intel_disk
*idisk
;
3856 idisk
= disk_list_get(disk
->serial
, disk_list
);
3858 if (idisk
->owner
== owner
||
3859 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
3862 dprintf("%s: '%.16s' owner %d != %d\n",
3863 __func__
, disk
->serial
, idisk
->owner
,
3866 dprintf("%s: unknown disk %x [%d]: %.16s\n",
3867 __func__
, __le32_to_cpu(mpb
->family_num
), i
,
3873 if (ok_count
== mpb
->num_disks
)
3878 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
3880 struct intel_super
*s
;
3882 for (s
= super_list
; s
; s
= s
->next
) {
3883 if (family_num
!= s
->anchor
->family_num
)
3885 fprintf(stderr
, "Conflict, offlining family %#x on '%s'\n",
3886 __le32_to_cpu(family_num
), s
->disks
->devname
);
3890 static struct intel_super
*
3891 imsm_thunderdome(struct intel_super
**super_list
, int len
)
3893 struct intel_super
*super_table
[len
];
3894 struct intel_disk
*disk_list
= NULL
;
3895 struct intel_super
*champion
, *spare
;
3896 struct intel_super
*s
, **del
;
3901 memset(super_table
, 0, sizeof(super_table
));
3902 for (s
= *super_list
; s
; s
= s
->next
)
3903 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
3905 for (i
= 0; i
< tbl_size
; i
++) {
3906 struct imsm_disk
*d
;
3907 struct intel_disk
*idisk
;
3908 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
3911 d
= &s
->disks
->disk
;
3913 /* 'd' must appear in merged disk list for its
3914 * configuration to be valid
3916 idisk
= disk_list_get(d
->serial
, disk_list
);
3917 if (idisk
&& idisk
->owner
== i
)
3918 s
= validate_members(s
, disk_list
, i
);
3923 dprintf("%s: marking family: %#x from %d:%d offline\n",
3924 __func__
, mpb
->family_num
,
3925 super_table
[i
]->disks
->major
,
3926 super_table
[i
]->disks
->minor
);
3930 /* This is where the mdadm implementation differs from the Windows
3931 * driver which has no strict concept of a container. We can only
3932 * assemble one family from a container, so when returning a prodigal
3933 * array member to this system the code will not be able to disambiguate
3934 * the container contents that should be assembled ("foreign" versus
3935 * "local"). It requires user intervention to set the orig_family_num
3936 * to a new value to establish a new container. The Windows driver in
3937 * this situation fixes up the volume name in place and manages the
3938 * foreign array as an independent entity.
3943 for (i
= 0; i
< tbl_size
; i
++) {
3944 struct intel_super
*tbl_ent
= super_table
[i
];
3950 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
3955 if (s
&& !is_spare
) {
3956 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
3958 } else if (!s
&& !is_spare
)
3971 fprintf(stderr
, "Chose family %#x on '%s', "
3972 "assemble conflicts to new container with '--update=uuid'\n",
3973 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
3975 /* collect all dl's onto 'champion', and update them to
3976 * champion's version of the status
3978 for (s
= *super_list
; s
; s
= s
->next
) {
3979 struct imsm_super
*mpb
= champion
->anchor
;
3980 struct dl
*dl
= s
->disks
;
3985 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3986 struct imsm_disk
*disk
;
3988 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
3991 /* only set index on disks that are a member of
3992 * a populated contianer, i.e. one with
3995 if (is_failed(&dl
->disk
))
3997 else if (is_spare(&dl
->disk
))
4003 if (i
>= mpb
->num_disks
) {
4004 struct intel_disk
*idisk
;
4006 idisk
= disk_list_get(dl
->serial
, disk_list
);
4007 if (idisk
&& is_spare(&idisk
->disk
) &&
4008 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
4016 dl
->next
= champion
->disks
;
4017 champion
->disks
= dl
;
4021 /* delete 'champion' from super_list */
4022 for (del
= super_list
; *del
; ) {
4023 if (*del
== champion
) {
4024 *del
= (*del
)->next
;
4027 del
= &(*del
)->next
;
4029 champion
->next
= NULL
;
4033 struct intel_disk
*idisk
= disk_list
;
4035 disk_list
= disk_list
->next
;
4044 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
);
4045 static int get_super_block(struct intel_super
**super_list
, int devnum
, char *devname
,
4046 int major
, int minor
, int keep_fd
);
4048 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4049 int *max
, int keep_fd
);
4052 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
4053 char *devname
, struct md_list
*devlist
,
4056 struct intel_super
*super_list
= NULL
;
4057 struct intel_super
*super
= NULL
;
4062 /* 'fd' is an opened container */
4063 err
= get_sra_super_block(fd
, &super_list
, devname
, &i
, keep_fd
);
4065 /* get super block from devlist devices */
4066 err
= get_devlist_super_block(devlist
, &super_list
, &i
, keep_fd
);
4069 /* all mpbs enter, maybe one leaves */
4070 super
= imsm_thunderdome(&super_list
, i
);
4076 if (find_missing(super
) != 0) {
4082 /* load migration record */
4083 err
= load_imsm_migr_rec(super
, NULL
);
4085 /* migration is in progress,
4086 * but migr_rec cannot be loaded,
4092 /* Check migration compatibility */
4093 if ((err
== 0) && (check_mpb_migr_compatibility(super
) != 0)) {
4094 fprintf(stderr
, Name
": Unsupported migration detected");
4096 fprintf(stderr
, " on %s\n", devname
);
4098 fprintf(stderr
, " (IMSM).\n");
4107 while (super_list
) {
4108 struct intel_super
*s
= super_list
;
4110 super_list
= super_list
->next
;
4120 st
->container_dev
= fd2devnum(fd
);
4122 st
->container_dev
= NoMdDev
;
4123 if (err
== 0 && st
->ss
== NULL
) {
4124 st
->ss
= &super_imsm
;
4125 st
->minor_version
= 0;
4126 st
->max_devs
= IMSM_MAX_DEVICES
;
4133 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4134 int *max
, int keep_fd
)
4136 struct md_list
*tmpdev
;
4140 for (i
= 0, tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
4141 if (tmpdev
->used
!= 1)
4143 if (tmpdev
->container
== 1) {
4145 int fd
= dev_open(tmpdev
->devname
, O_RDONLY
|O_EXCL
);
4147 fprintf(stderr
, Name
": cannot open device %s: %s\n",
4148 tmpdev
->devname
, strerror(errno
));
4152 err
= get_sra_super_block(fd
, super_list
,
4153 tmpdev
->devname
, &lmax
,
4162 int major
= major(tmpdev
->st_rdev
);
4163 int minor
= minor(tmpdev
->st_rdev
);
4164 err
= get_super_block(super_list
,
4181 static int get_super_block(struct intel_super
**super_list
, int devnum
, char *devname
,
4182 int major
, int minor
, int keep_fd
)
4184 struct intel_super
*s
= NULL
;
4197 sprintf(nm
, "%d:%d", major
, minor
);
4198 dfd
= dev_open(nm
, O_RDWR
);
4204 rv
= find_intel_hba_capability(dfd
, s
, devname
);
4205 /* no orom/efi or non-intel hba of the disk */
4211 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4213 /* retry the load if we might have raced against mdmon */
4214 if (err
== 3 && (devnum
!= -1) && mdmon_running(devnum
))
4215 for (retry
= 0; retry
< 3; retry
++) {
4217 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4223 s
->next
= *super_list
;
4231 if ((dfd
>= 0) && (!keep_fd
))
4238 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
)
4245 sra
= sysfs_read(fd
, 0, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
4249 if (sra
->array
.major_version
!= -1 ||
4250 sra
->array
.minor_version
!= -2 ||
4251 strcmp(sra
->text_version
, "imsm") != 0) {
4256 devnum
= fd2devnum(fd
);
4257 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
4258 if (get_super_block(super_list
, devnum
, devname
,
4259 sd
->disk
.major
, sd
->disk
.minor
, keep_fd
) != 0) {
4270 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
4272 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
, NULL
, 1);
4276 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
4278 struct intel_super
*super
;
4281 if (test_partition(fd
))
4282 /* IMSM not allowed on partitions */
4285 free_super_imsm(st
);
4287 super
= alloc_super();
4290 Name
": malloc of %zu failed.\n",
4294 /* Load hba and capabilities if they exist.
4295 * But do not preclude loading metadata in case capabilities or hba are
4296 * non-compliant and ignore_hw_compat is set.
4298 rv
= find_intel_hba_capability(fd
, super
, devname
);
4299 /* no orom/efi or non-intel hba of the disk */
4300 if ((rv
!= 0) && (st
->ignore_hw_compat
== 0)) {
4303 Name
": No OROM/EFI properties for %s\n", devname
);
4307 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
4312 Name
": Failed to load all information "
4313 "sections on %s\n", devname
);
4319 if (st
->ss
== NULL
) {
4320 st
->ss
= &super_imsm
;
4321 st
->minor_version
= 0;
4322 st
->max_devs
= IMSM_MAX_DEVICES
;
4325 /* load migration record */
4326 if (load_imsm_migr_rec(super
, NULL
) == 0) {
4327 /* Check for unsupported migration features */
4328 if (check_mpb_migr_compatibility(super
) != 0) {
4330 Name
": Unsupported migration detected");
4332 fprintf(stderr
, " on %s\n", devname
);
4334 fprintf(stderr
, " (IMSM).\n");
4342 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
4344 if (info
->level
== 1)
4346 return info
->chunk_size
>> 9;
4349 static __u32
info_to_num_data_stripes(mdu_array_info_t
*info
, int num_domains
)
4353 num_stripes
= (info
->size
* 2) / info_to_blocks_per_strip(info
);
4354 num_stripes
/= num_domains
;
4359 static __u32
info_to_blocks_per_member(mdu_array_info_t
*info
)
4361 if (info
->level
== 1)
4362 return info
->size
* 2;
4364 return (info
->size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
4367 static void imsm_update_version_info(struct intel_super
*super
)
4369 /* update the version and attributes */
4370 struct imsm_super
*mpb
= super
->anchor
;
4372 struct imsm_dev
*dev
;
4373 struct imsm_map
*map
;
4376 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4377 dev
= get_imsm_dev(super
, i
);
4378 map
= get_imsm_map(dev
, MAP_0
);
4379 if (__le32_to_cpu(dev
->size_high
) > 0)
4380 mpb
->attributes
|= MPB_ATTRIB_2TB
;
4382 /* FIXME detect when an array spans a port multiplier */
4384 mpb
->attributes
|= MPB_ATTRIB_PM
;
4387 if (mpb
->num_raid_devs
> 1 ||
4388 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
4389 version
= MPB_VERSION_ATTRIBS
;
4390 switch (get_imsm_raid_level(map
)) {
4391 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
4392 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
4393 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
4394 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
4397 if (map
->num_members
>= 5)
4398 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
4399 else if (dev
->status
== DEV_CLONE_N_GO
)
4400 version
= MPB_VERSION_CNG
;
4401 else if (get_imsm_raid_level(map
) == 5)
4402 version
= MPB_VERSION_RAID5
;
4403 else if (map
->num_members
>= 3)
4404 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
4405 else if (get_imsm_raid_level(map
) == 1)
4406 version
= MPB_VERSION_RAID1
;
4408 version
= MPB_VERSION_RAID0
;
4410 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
4414 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
4416 struct imsm_super
*mpb
= super
->anchor
;
4417 char *reason
= NULL
;
4420 if (strlen(name
) > MAX_RAID_SERIAL_LEN
)
4421 reason
= "must be 16 characters or less";
4423 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4424 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
4426 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
4427 reason
= "already exists";
4432 if (reason
&& !quiet
)
4433 fprintf(stderr
, Name
": imsm volume name %s\n", reason
);
4438 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
4439 unsigned long long size
, char *name
,
4440 char *homehost
, int *uuid
)
4442 /* We are creating a volume inside a pre-existing container.
4443 * so st->sb is already set.
4445 struct intel_super
*super
= st
->sb
;
4446 struct imsm_super
*mpb
= super
->anchor
;
4447 struct intel_dev
*dv
;
4448 struct imsm_dev
*dev
;
4449 struct imsm_vol
*vol
;
4450 struct imsm_map
*map
;
4451 int idx
= mpb
->num_raid_devs
;
4453 unsigned long long array_blocks
;
4454 size_t size_old
, size_new
;
4455 __u32 num_data_stripes
;
4457 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
4458 fprintf(stderr
, Name
": This imsm-container already has the "
4459 "maximum of %d volumes\n", super
->orom
->vpa
);
4463 /* ensure the mpb is large enough for the new data */
4464 size_old
= __le32_to_cpu(mpb
->mpb_size
);
4465 size_new
= disks_to_mpb_size(info
->nr_disks
);
4466 if (size_new
> size_old
) {
4468 size_t size_round
= ROUND_UP(size_new
, 512);
4470 if (posix_memalign(&mpb_new
, 512, size_round
) != 0) {
4471 fprintf(stderr
, Name
": could not allocate new mpb\n");
4474 if (posix_memalign(&super
->migr_rec_buf
, 512,
4475 MIGR_REC_BUF_SIZE
) != 0) {
4476 fprintf(stderr
, Name
4477 ": %s could not allocate migr_rec buffer\n",
4484 memcpy(mpb_new
, mpb
, size_old
);
4487 super
->anchor
= mpb_new
;
4488 mpb
->mpb_size
= __cpu_to_le32(size_new
);
4489 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
4491 super
->current_vol
= idx
;
4493 /* handle 'failed_disks' by either:
4494 * a) create dummy disk entries in the table if this the first
4495 * volume in the array. We add them here as this is the only
4496 * opportunity to add them. add_to_super_imsm_volume()
4497 * handles the non-failed disks and continues incrementing
4499 * b) validate that 'failed_disks' matches the current number
4500 * of missing disks if the container is populated
4502 if (super
->current_vol
== 0) {
4504 for (i
= 0; i
< info
->failed_disks
; i
++) {
4505 struct imsm_disk
*disk
;
4508 disk
= __get_imsm_disk(mpb
, i
);
4509 disk
->status
= CONFIGURED_DISK
| FAILED_DISK
;
4510 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
4511 snprintf((char *) disk
->serial
, MAX_RAID_SERIAL_LEN
,
4514 find_missing(super
);
4519 for (d
= super
->missing
; d
; d
= d
->next
)
4521 if (info
->failed_disks
> missing
) {
4522 fprintf(stderr
, Name
": unable to add 'missing' disk to container\n");
4527 if (!check_name(super
, name
, 0))
4529 dv
= malloc(sizeof(*dv
));
4531 fprintf(stderr
, Name
": failed to allocate device list entry\n");
4534 dev
= calloc(1, sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
4537 fprintf(stderr
, Name
": could not allocate raid device\n");
4541 strncpy((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
4542 if (info
->level
== 1)
4543 array_blocks
= info_to_blocks_per_member(info
);
4545 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
4546 info
->layout
, info
->chunk_size
,
4548 /* round array size down to closest MB */
4549 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
4551 dev
->size_low
= __cpu_to_le32((__u32
) array_blocks
);
4552 dev
->size_high
= __cpu_to_le32((__u32
) (array_blocks
>> 32));
4553 dev
->status
= (DEV_READ_COALESCING
| DEV_WRITE_COALESCING
);
4555 vol
->migr_state
= 0;
4556 set_migr_type(dev
, MIGR_INIT
);
4557 vol
->dirty
= !info
->state
;
4558 vol
->curr_migr_unit
= 0;
4559 map
= get_imsm_map(dev
, MAP_0
);
4560 map
->pba_of_lba0
= __cpu_to_le32(super
->create_offset
);
4561 map
->blocks_per_member
= __cpu_to_le32(info_to_blocks_per_member(info
));
4562 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
4563 map
->failed_disk_num
= ~0;
4564 if (info
->level
> 0)
4565 map
->map_state
= IMSM_T_STATE_UNINITIALIZED
;
4567 map
->map_state
= info
->failed_disks
? IMSM_T_STATE_FAILED
:
4568 IMSM_T_STATE_NORMAL
;
4571 if (info
->level
== 1 && info
->raid_disks
> 2) {
4574 fprintf(stderr
, Name
": imsm does not support more than 2 disks"
4575 "in a raid1 volume\n");
4579 map
->raid_level
= info
->level
;
4580 if (info
->level
== 10) {
4581 map
->raid_level
= 1;
4582 map
->num_domains
= info
->raid_disks
/ 2;
4583 } else if (info
->level
== 1)
4584 map
->num_domains
= info
->raid_disks
;
4586 map
->num_domains
= 1;
4588 num_data_stripes
= info_to_num_data_stripes(info
, map
->num_domains
);
4589 map
->num_data_stripes
= __cpu_to_le32(num_data_stripes
);
4591 map
->num_members
= info
->raid_disks
;
4592 for (i
= 0; i
< map
->num_members
; i
++) {
4593 /* initialized in add_to_super */
4594 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
4596 mpb
->num_raid_devs
++;
4599 dv
->index
= super
->current_vol
;
4600 dv
->next
= super
->devlist
;
4601 super
->devlist
= dv
;
4603 imsm_update_version_info(super
);
4608 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
4609 unsigned long long size
, char *name
,
4610 char *homehost
, int *uuid
)
4612 /* This is primarily called by Create when creating a new array.
4613 * We will then get add_to_super called for each component, and then
4614 * write_init_super called to write it out to each device.
4615 * For IMSM, Create can create on fresh devices or on a pre-existing
4617 * To create on a pre-existing array a different method will be called.
4618 * This one is just for fresh drives.
4620 struct intel_super
*super
;
4621 struct imsm_super
*mpb
;
4626 return init_super_imsm_volume(st
, info
, size
, name
, homehost
, uuid
);
4629 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
4633 super
= alloc_super();
4634 if (super
&& posix_memalign(&super
->buf
, 512, mpb_size
) != 0) {
4639 fprintf(stderr
, Name
4640 ": %s could not allocate superblock\n", __func__
);
4643 if (posix_memalign(&super
->migr_rec_buf
, 512, MIGR_REC_BUF_SIZE
) != 0) {
4644 fprintf(stderr
, Name
4645 ": %s could not allocate migr_rec buffer\n", __func__
);
4650 memset(super
->buf
, 0, mpb_size
);
4652 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
4656 /* zeroing superblock */
4660 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
4662 version
= (char *) mpb
->sig
;
4663 strcpy(version
, MPB_SIGNATURE
);
4664 version
+= strlen(MPB_SIGNATURE
);
4665 strcpy(version
, MPB_VERSION_RAID0
);
4671 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
4672 int fd
, char *devname
)
4674 struct intel_super
*super
= st
->sb
;
4675 struct imsm_super
*mpb
= super
->anchor
;
4676 struct imsm_disk
*_disk
;
4677 struct imsm_dev
*dev
;
4678 struct imsm_map
*map
;
4682 dev
= get_imsm_dev(super
, super
->current_vol
);
4683 map
= get_imsm_map(dev
, MAP_0
);
4685 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
4686 fprintf(stderr
, Name
": %s: Cannot add spare devices to IMSM volume\n",
4692 /* we're doing autolayout so grab the pre-marked (in
4693 * validate_geometry) raid_disk
4695 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4696 if (dl
->raiddisk
== dk
->raid_disk
)
4699 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4700 if (dl
->major
== dk
->major
&&
4701 dl
->minor
== dk
->minor
)
4706 fprintf(stderr
, Name
": %s is not a member of the same container\n", devname
);
4710 /* add a pristine spare to the metadata */
4711 if (dl
->index
< 0) {
4712 dl
->index
= super
->anchor
->num_disks
;
4713 super
->anchor
->num_disks
++;
4715 /* Check the device has not already been added */
4716 slot
= get_imsm_disk_slot(map
, dl
->index
);
4718 (get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
) & IMSM_ORD_REBUILD
) == 0) {
4719 fprintf(stderr
, Name
": %s has been included in this array twice\n",
4723 set_imsm_ord_tbl_ent(map
, dk
->raid_disk
, dl
->index
);
4724 dl
->disk
.status
= CONFIGURED_DISK
;
4726 /* update size of 'missing' disks to be at least as large as the
4727 * largest acitve member (we only have dummy missing disks when
4728 * creating the first volume)
4730 if (super
->current_vol
== 0) {
4731 for (df
= super
->missing
; df
; df
= df
->next
) {
4732 if (dl
->disk
.total_blocks
> df
->disk
.total_blocks
)
4733 df
->disk
.total_blocks
= dl
->disk
.total_blocks
;
4734 _disk
= __get_imsm_disk(mpb
, df
->index
);
4739 /* refresh unset/failed slots to point to valid 'missing' entries */
4740 for (df
= super
->missing
; df
; df
= df
->next
)
4741 for (slot
= 0; slot
< mpb
->num_disks
; slot
++) {
4742 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
4744 if ((ord
& IMSM_ORD_REBUILD
) == 0)
4746 set_imsm_ord_tbl_ent(map
, slot
, df
->index
| IMSM_ORD_REBUILD
);
4747 if (is_gen_migration(dev
)) {
4748 struct imsm_map
*map2
= get_imsm_map(dev
,
4750 int slot2
= get_imsm_disk_slot(map2
, df
->index
);
4751 if ((slot2
< map2
->num_members
) &&
4753 __u32 ord2
= get_imsm_ord_tbl_ent(dev
,
4756 if ((unsigned)df
->index
==
4758 set_imsm_ord_tbl_ent(map2
,
4764 dprintf("set slot:%d to missing disk:%d\n", slot
, df
->index
);
4768 /* if we are creating the first raid device update the family number */
4769 if (super
->current_vol
== 0) {
4771 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
4773 _disk
= __get_imsm_disk(mpb
, dl
->index
);
4774 if (!_dev
|| !_disk
) {
4775 fprintf(stderr
, Name
": BUG mpb setup error\n");
4781 sum
+= __gen_imsm_checksum(mpb
);
4782 mpb
->family_num
= __cpu_to_le32(sum
);
4783 mpb
->orig_family_num
= mpb
->family_num
;
4785 super
->current_disk
= dl
;
4790 * Function marks disk as spare and restores disk serial
4791 * in case it was previously marked as failed by takeover operation
4793 * -1 : critical error
4794 * 0 : disk is marked as spare but serial is not set
4797 int mark_spare(struct dl
*disk
)
4799 __u8 serial
[MAX_RAID_SERIAL_LEN
];
4806 if (!imsm_read_serial(disk
->fd
, NULL
, serial
)) {
4807 /* Restore disk serial number, because takeover marks disk
4808 * as failed and adds to serial ':0' before it becomes
4811 serialcpy(disk
->serial
, serial
);
4812 serialcpy(disk
->disk
.serial
, serial
);
4815 disk
->disk
.status
= SPARE_DISK
;
4821 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
4822 int fd
, char *devname
)
4824 struct intel_super
*super
= st
->sb
;
4826 unsigned long long size
;
4831 /* If we are on an RAID enabled platform check that the disk is
4832 * attached to the raid controller.
4833 * We do not need to test disks attachment for container based additions,
4834 * they shall be already tested when container was created/assembled.
4836 rv
= find_intel_hba_capability(fd
, super
, devname
);
4837 /* no orom/efi or non-intel hba of the disk */
4839 dprintf("capability: %p fd: %d ret: %d\n",
4840 super
->orom
, fd
, rv
);
4844 if (super
->current_vol
>= 0)
4845 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
4848 dd
= malloc(sizeof(*dd
));
4851 Name
": malloc failed %s:%d.\n", __func__
, __LINE__
);
4854 memset(dd
, 0, sizeof(*dd
));
4855 dd
->major
= major(stb
.st_rdev
);
4856 dd
->minor
= minor(stb
.st_rdev
);
4857 dd
->devname
= devname
? strdup(devname
) : NULL
;
4860 dd
->action
= DISK_ADD
;
4861 rv
= imsm_read_serial(fd
, devname
, dd
->serial
);
4864 Name
": failed to retrieve scsi serial, aborting\n");
4869 get_dev_size(fd
, NULL
, &size
);
4871 serialcpy(dd
->disk
.serial
, dd
->serial
);
4872 dd
->disk
.total_blocks
= __cpu_to_le32(size
);
4874 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
4875 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
4877 dd
->disk
.scsi_id
= __cpu_to_le32(0);
4879 if (st
->update_tail
) {
4880 dd
->next
= super
->disk_mgmt_list
;
4881 super
->disk_mgmt_list
= dd
;
4883 dd
->next
= super
->disks
;
4885 super
->updates_pending
++;
4892 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
4894 struct intel_super
*super
= st
->sb
;
4897 /* remove from super works only in mdmon - for communication
4898 * manager - monitor. Check if communication memory buffer
4901 if (!st
->update_tail
) {
4903 Name
": %s shall be used in mdmon context only"
4904 "(line %d).\n", __func__
, __LINE__
);
4907 dd
= malloc(sizeof(*dd
));
4910 Name
": malloc failed %s:%d.\n", __func__
, __LINE__
);
4913 memset(dd
, 0, sizeof(*dd
));
4914 dd
->major
= dk
->major
;
4915 dd
->minor
= dk
->minor
;
4918 dd
->action
= DISK_REMOVE
;
4920 dd
->next
= super
->disk_mgmt_list
;
4921 super
->disk_mgmt_list
= dd
;
4927 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
4931 struct imsm_super anchor
;
4932 } spare_record
__attribute__ ((aligned(512)));
4934 /* spare records have their own family number and do not have any defined raid
4937 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
4939 struct imsm_super
*mpb
= super
->anchor
;
4940 struct imsm_super
*spare
= &spare_record
.anchor
;
4944 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
)),
4945 spare
->generation_num
= __cpu_to_le32(1UL),
4946 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
4947 spare
->num_disks
= 1,
4948 spare
->num_raid_devs
= 0,
4949 spare
->cache_size
= mpb
->cache_size
,
4950 spare
->pwr_cycle_count
= __cpu_to_le32(1),
4952 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
4953 MPB_SIGNATURE MPB_VERSION_RAID0
);
4955 for (d
= super
->disks
; d
; d
= d
->next
) {
4959 spare
->disk
[0] = d
->disk
;
4960 sum
= __gen_imsm_checksum(spare
);
4961 spare
->family_num
= __cpu_to_le32(sum
);
4962 spare
->orig_family_num
= 0;
4963 sum
= __gen_imsm_checksum(spare
);
4964 spare
->check_sum
= __cpu_to_le32(sum
);
4966 if (store_imsm_mpb(d
->fd
, spare
)) {
4967 fprintf(stderr
, "%s: failed for device %d:%d %s\n",
4968 __func__
, d
->major
, d
->minor
, strerror(errno
));
4980 static int write_super_imsm(struct supertype
*st
, int doclose
)
4982 struct intel_super
*super
= st
->sb
;
4983 struct imsm_super
*mpb
= super
->anchor
;
4989 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
4991 int clear_migration_record
= 1;
4993 /* 'generation' is incremented everytime the metadata is written */
4994 generation
= __le32_to_cpu(mpb
->generation_num
);
4996 mpb
->generation_num
= __cpu_to_le32(generation
);
4998 /* fix up cases where previous mdadm releases failed to set
5001 if (mpb
->orig_family_num
== 0)
5002 mpb
->orig_family_num
= mpb
->family_num
;
5004 for (d
= super
->disks
; d
; d
= d
->next
) {
5008 mpb
->disk
[d
->index
] = d
->disk
;
5012 for (d
= super
->missing
; d
; d
= d
->next
) {
5013 mpb
->disk
[d
->index
] = d
->disk
;
5016 mpb
->num_disks
= num_disks
;
5017 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
5019 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5020 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
5021 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
5023 imsm_copy_dev(dev
, dev2
);
5024 mpb_size
+= sizeof_imsm_dev(dev
, 0);
5026 if (is_gen_migration(dev2
))
5027 clear_migration_record
= 0;
5029 mpb_size
+= __le32_to_cpu(mpb
->bbm_log_size
);
5030 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5032 /* recalculate checksum */
5033 sum
= __gen_imsm_checksum(mpb
);
5034 mpb
->check_sum
= __cpu_to_le32(sum
);
5036 if (super
->clean_migration_record_by_mdmon
) {
5037 clear_migration_record
= 1;
5038 super
->clean_migration_record_by_mdmon
= 0;
5040 if (clear_migration_record
)
5041 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SIZE
);
5043 /* write the mpb for disks that compose raid devices */
5044 for (d
= super
->disks
; d
; d
= d
->next
) {
5045 if (d
->index
< 0 || is_failed(&d
->disk
))
5048 if (clear_migration_record
) {
5049 unsigned long long dsize
;
5051 get_dev_size(d
->fd
, NULL
, &dsize
);
5052 if (lseek64(d
->fd
, dsize
- 512, SEEK_SET
) >= 0) {
5053 if (write(d
->fd
, super
->migr_rec_buf
,
5054 MIGR_REC_BUF_SIZE
) != MIGR_REC_BUF_SIZE
)
5055 perror("Write migr_rec failed");
5059 if (store_imsm_mpb(d
->fd
, mpb
))
5061 "%s: failed for device %d:%d (fd: %d)%s\n",
5062 __func__
, d
->major
, d
->minor
,
5063 d
->fd
, strerror(errno
));
5072 return write_super_imsm_spares(super
, doclose
);
5078 static int create_array(struct supertype
*st
, int dev_idx
)
5081 struct imsm_update_create_array
*u
;
5082 struct intel_super
*super
= st
->sb
;
5083 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
5084 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
5085 struct disk_info
*inf
;
5086 struct imsm_disk
*disk
;
5089 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
5090 sizeof(*inf
) * map
->num_members
;
5093 fprintf(stderr
, "%s: failed to allocate update buffer\n",
5098 u
->type
= update_create_array
;
5099 u
->dev_idx
= dev_idx
;
5100 imsm_copy_dev(&u
->dev
, dev
);
5101 inf
= get_disk_info(u
);
5102 for (i
= 0; i
< map
->num_members
; i
++) {
5103 int idx
= get_imsm_disk_idx(dev
, i
, MAP_X
);
5105 disk
= get_imsm_disk(super
, idx
);
5106 serialcpy(inf
[i
].serial
, disk
->serial
);
5108 append_metadata_update(st
, u
, len
);
5113 static int mgmt_disk(struct supertype
*st
)
5115 struct intel_super
*super
= st
->sb
;
5117 struct imsm_update_add_remove_disk
*u
;
5119 if (!super
->disk_mgmt_list
)
5125 fprintf(stderr
, "%s: failed to allocate update buffer\n",
5130 u
->type
= update_add_remove_disk
;
5131 append_metadata_update(st
, u
, len
);
5136 static int write_init_super_imsm(struct supertype
*st
)
5138 struct intel_super
*super
= st
->sb
;
5139 int current_vol
= super
->current_vol
;
5141 /* we are done with current_vol reset it to point st at the container */
5142 super
->current_vol
= -1;
5144 if (st
->update_tail
) {
5145 /* queue the recently created array / added disk
5146 * as a metadata update */
5149 /* determine if we are creating a volume or adding a disk */
5150 if (current_vol
< 0) {
5151 /* in the mgmt (add/remove) disk case we are running
5152 * in mdmon context, so don't close fd's
5154 return mgmt_disk(st
);
5156 rv
= create_array(st
, current_vol
);
5161 for (d
= super
->disks
; d
; d
= d
->next
)
5162 Kill(d
->devname
, NULL
, 0, 1, 1);
5163 return write_super_imsm(st
, 1);
5168 static int store_super_imsm(struct supertype
*st
, int fd
)
5170 struct intel_super
*super
= st
->sb
;
5171 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
5177 return store_imsm_mpb(fd
, mpb
);
5183 static int imsm_bbm_log_size(struct imsm_super
*mpb
)
5185 return __le32_to_cpu(mpb
->bbm_log_size
);
5189 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
5190 int layout
, int raiddisks
, int chunk
,
5191 unsigned long long size
, char *dev
,
5192 unsigned long long *freesize
,
5196 unsigned long long ldsize
;
5197 struct intel_super
*super
=NULL
;
5200 if (level
!= LEVEL_CONTAINER
)
5205 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
5208 fprintf(stderr
, Name
": imsm: Cannot open %s: %s\n",
5209 dev
, strerror(errno
));
5212 if (!get_dev_size(fd
, dev
, &ldsize
)) {
5217 /* capabilities retrieve could be possible
5218 * note that there is no fd for the disks in array.
5220 super
= alloc_super();
5223 Name
": malloc of %zu failed.\n",
5229 rv
= find_intel_hba_capability(fd
, super
, verbose
? dev
: NULL
);
5233 fd2devname(fd
, str
);
5234 dprintf("validate_geometry_imsm_container: fd: %d %s orom: %p rv: %d raiddisk: %d\n",
5235 fd
, str
, super
->orom
, rv
, raiddisks
);
5237 /* no orom/efi or non-intel hba of the disk */
5243 if (super
->orom
&& raiddisks
> super
->orom
->tds
) {
5245 fprintf(stderr
, Name
": %d exceeds maximum number of"
5246 " platform supported disks: %d\n",
5247 raiddisks
, super
->orom
->tds
);
5253 *freesize
= avail_size_imsm(st
, ldsize
>> 9);
5259 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
5261 const unsigned long long base_start
= e
[*idx
].start
;
5262 unsigned long long end
= base_start
+ e
[*idx
].size
;
5265 if (base_start
== end
)
5269 for (i
= *idx
; i
< num_extents
; i
++) {
5270 /* extend overlapping extents */
5271 if (e
[i
].start
>= base_start
&&
5272 e
[i
].start
<= end
) {
5275 if (e
[i
].start
+ e
[i
].size
> end
)
5276 end
= e
[i
].start
+ e
[i
].size
;
5277 } else if (e
[i
].start
> end
) {
5283 return end
- base_start
;
5286 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
5288 /* build a composite disk with all known extents and generate a new
5289 * 'maxsize' given the "all disks in an array must share a common start
5290 * offset" constraint
5292 struct extent
*e
= calloc(sum_extents
, sizeof(*e
));
5296 unsigned long long pos
;
5297 unsigned long long start
= 0;
5298 unsigned long long maxsize
;
5299 unsigned long reserve
;
5304 /* coalesce and sort all extents. also, check to see if we need to
5305 * reserve space between member arrays
5308 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5311 for (i
= 0; i
< dl
->extent_cnt
; i
++)
5314 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
5319 while (i
< sum_extents
) {
5320 e
[j
].start
= e
[i
].start
;
5321 e
[j
].size
= find_size(e
, &i
, sum_extents
);
5323 if (e
[j
-1].size
== 0)
5332 unsigned long long esize
;
5334 esize
= e
[i
].start
- pos
;
5335 if (esize
>= maxsize
) {
5340 pos
= e
[i
].start
+ e
[i
].size
;
5342 } while (e
[i
-1].size
);
5348 /* FIXME assumes volume at offset 0 is the first volume in a
5351 if (start_extent
> 0)
5352 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
5356 if (maxsize
< reserve
)
5359 super
->create_offset
= ~((__u32
) 0);
5360 if (start
+ reserve
> super
->create_offset
)
5361 return 0; /* start overflows create_offset */
5362 super
->create_offset
= start
+ reserve
;
5364 return maxsize
- reserve
;
5367 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
5369 if (level
< 0 || level
== 6 || level
== 4)
5372 /* if we have an orom prevent invalid raid levels */
5375 case 0: return imsm_orom_has_raid0(orom
);
5378 return imsm_orom_has_raid1e(orom
);
5379 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
5380 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
5381 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
5384 return 1; /* not on an Intel RAID platform so anything goes */
5391 active_arrays_by_format(char *name
, char* hba
, struct md_list
**devlist
,
5392 int dpa
, int verbose
)
5394 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
5395 struct mdstat_ent
*memb
= NULL
;
5398 struct md_list
*dv
= NULL
;
5401 for (memb
= mdstat
; memb
; memb
= memb
->next
) {
5402 if (memb
->metadata_version
&&
5403 (strncmp(memb
->metadata_version
, "external:", 9) == 0) &&
5404 (strcmp(&memb
->metadata_version
[9], name
) == 0) &&
5405 !is_subarray(memb
->metadata_version
+9) &&
5407 struct dev_member
*dev
= memb
->members
;
5409 while(dev
&& (fd
< 0)) {
5410 char *path
= malloc(strlen(dev
->name
) + strlen("/dev/") + 1);
5412 num
= sprintf(path
, "%s%s", "/dev/", dev
->name
);
5414 fd
= open(path
, O_RDONLY
, 0);
5415 if ((num
<= 0) || (fd
< 0)) {
5416 pr_vrb(": Cannot open %s: %s\n",
5417 dev
->name
, strerror(errno
));
5424 if ((fd
>= 0) && disk_attached_to_hba(fd
, hba
)) {
5425 struct mdstat_ent
*vol
;
5426 for (vol
= mdstat
; vol
; vol
= vol
->next
) {
5427 if ((vol
->active
> 0) &&
5428 vol
->metadata_version
&&
5429 is_container_member(vol
, memb
->dev
)) {
5434 if (*devlist
&& (found
< dpa
)) {
5435 dv
= calloc(1, sizeof(*dv
));
5437 fprintf(stderr
, Name
": calloc failed\n");
5439 dv
->devname
= malloc(strlen(memb
->dev
) + strlen("/dev/") + 1);
5440 if (dv
->devname
!= NULL
) {
5441 sprintf(dv
->devname
, "%s%s", "/dev/", memb
->dev
);
5444 dv
->next
= *devlist
;
5455 free_mdstat(mdstat
);
5460 static struct md_list
*
5461 get_loop_devices(void)
5464 struct md_list
*devlist
= NULL
;
5465 struct md_list
*dv
= NULL
;
5467 for(i
= 0; i
< 12; i
++) {
5468 dv
= calloc(1, sizeof(*dv
));
5470 fprintf(stderr
, Name
": calloc failed\n");
5473 dv
->devname
= malloc(40);
5474 if (dv
->devname
== NULL
) {
5475 fprintf(stderr
, Name
": malloc failed\n");
5479 sprintf(dv
->devname
, "/dev/loop%d", i
);
5487 static struct md_list
*
5488 get_devices(const char *hba_path
)
5490 struct md_list
*devlist
= NULL
;
5491 struct md_list
*dv
= NULL
;
5497 devlist
= get_loop_devices();
5500 /* scroll through /sys/dev/block looking for devices attached to
5503 dir
= opendir("/sys/dev/block");
5504 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
5509 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
5511 path
= devt_to_devpath(makedev(major
, minor
));
5514 if (!path_attached_to_hba(path
, hba_path
)) {
5521 fd
= dev_open(ent
->d_name
, O_RDONLY
);
5523 fd2devname(fd
, buf
);
5526 fprintf(stderr
, Name
": cannot open device: %s\n",
5532 dv
= calloc(1, sizeof(*dv
));
5534 fprintf(stderr
, Name
": malloc failed\n");
5538 dv
->devname
= strdup(buf
);
5539 if (dv
->devname
== NULL
) {
5540 fprintf(stderr
, Name
": malloc failed\n");
5551 devlist
= devlist
->next
;
5560 count_volumes_list(struct md_list
*devlist
, char *homehost
,
5561 int verbose
, int *found
)
5563 struct md_list
*tmpdev
;
5565 struct supertype
*st
= NULL
;
5567 /* first walk the list of devices to find a consistent set
5568 * that match the criterea, if that is possible.
5569 * We flag the ones we like with 'used'.
5572 st
= match_metadata_desc_imsm("imsm");
5574 pr_vrb(": cannot allocate memory for imsm supertype\n");
5578 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5579 char *devname
= tmpdev
->devname
;
5581 struct supertype
*tst
;
5583 if (tmpdev
->used
> 1)
5585 tst
= dup_super(st
);
5587 pr_vrb(": cannot allocate memory for imsm supertype\n");
5590 tmpdev
->container
= 0;
5591 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
5593 dprintf(": cannot open device %s: %s\n",
5594 devname
, strerror(errno
));
5596 } else if (fstat(dfd
, &stb
)< 0) {
5598 dprintf(": fstat failed for %s: %s\n",
5599 devname
, strerror(errno
));
5601 } else if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
5602 dprintf(": %s is not a block device.\n",
5605 } else if (must_be_container(dfd
)) {
5606 struct supertype
*cst
;
5607 cst
= super_by_fd(dfd
, NULL
);
5609 dprintf(": cannot recognize container type %s\n",
5612 } else if (tst
->ss
!= st
->ss
) {
5613 dprintf(": non-imsm container - ignore it: %s\n",
5616 } else if (!tst
->ss
->load_container
||
5617 tst
->ss
->load_container(tst
, dfd
, NULL
))
5620 tmpdev
->container
= 1;
5623 cst
->ss
->free_super(cst
);
5625 tmpdev
->st_rdev
= stb
.st_rdev
;
5626 if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
5627 dprintf(": no RAID superblock on %s\n",
5630 } else if (tst
->ss
->compare_super
== NULL
) {
5631 dprintf(": Cannot assemble %s metadata on %s\n",
5632 tst
->ss
->name
, devname
);
5638 if (tmpdev
->used
== 2 || tmpdev
->used
== 4) {
5639 /* Ignore unrecognised devices during auto-assembly */
5644 tst
->ss
->getinfo_super(tst
, &info
, NULL
);
5646 if (st
->minor_version
== -1)
5647 st
->minor_version
= tst
->minor_version
;
5649 if (memcmp(info
.uuid
, uuid_zero
,
5650 sizeof(int[4])) == 0) {
5651 /* this is a floating spare. It cannot define
5652 * an array unless there are no more arrays of
5653 * this type to be found. It can be included
5654 * in an array of this type though.
5660 if (st
->ss
!= tst
->ss
||
5661 st
->minor_version
!= tst
->minor_version
||
5662 st
->ss
->compare_super(st
, tst
) != 0) {
5663 /* Some mismatch. If exactly one array matches this host,
5664 * we can resolve on that one.
5665 * Or, if we are auto assembling, we just ignore the second
5668 dprintf(": superblock on %s doesn't match others - assembly aborted\n",
5674 dprintf("found: devname: %s\n", devname
);
5678 tst
->ss
->free_super(tst
);
5682 if ((err
= load_super_imsm_all(st
, -1, &st
->sb
, NULL
, devlist
, 0)) == 0) {
5683 struct mdinfo
*iter
, *head
= st
->ss
->container_content(st
, NULL
);
5684 for (iter
= head
; iter
; iter
= iter
->next
) {
5685 dprintf("content->text_version: %s vol\n",
5686 iter
->text_version
);
5687 if (iter
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
5688 /* do not assemble arrays with unsupported
5690 dprintf(": Cannot activate member %s.\n",
5691 iter
->text_version
);
5698 dprintf(" no valid super block on device list: err: %d %p\n",
5702 dprintf(" no more devices to examin\n");
5705 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5706 if ((tmpdev
->used
== 1) && (tmpdev
->found
)) {
5708 if (count
< tmpdev
->found
)
5711 count
-= tmpdev
->found
;
5714 if (tmpdev
->used
== 1)
5719 st
->ss
->free_super(st
);
5725 count_volumes(char *hba
, int dpa
, int verbose
)
5727 struct md_list
*devlist
= NULL
;
5731 devlist
= get_devices(hba
);
5732 /* if no intel devices return zero volumes */
5733 if (devlist
== NULL
)
5736 count
= active_arrays_by_format("imsm", hba
, &devlist
, dpa
, verbose
);
5737 dprintf(" path: %s active arrays: %d\n", hba
, count
);
5738 if (devlist
== NULL
)
5742 count
+= count_volumes_list(devlist
,
5746 dprintf("found %d count: %d\n", found
, count
);
5749 dprintf("path: %s total number of volumes: %d\n", hba
, count
);
5752 struct md_list
*dv
= devlist
;
5753 devlist
= devlist
->next
;
5760 static int imsm_default_chunk(const struct imsm_orom
*orom
)
5762 /* up to 512 if the plaform supports it, otherwise the platform max.
5763 * 128 if no platform detected
5765 int fs
= max(7, orom
? fls(orom
->sss
) : 0);
5767 return min(512, (1 << fs
));
5771 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
5772 int raiddisks
, int *chunk
, int verbose
)
5774 /* check/set platform and metadata limits/defaults */
5775 if (super
->orom
&& raiddisks
> super
->orom
->dpa
) {
5776 pr_vrb(": platform supports a maximum of %d disks per array\n",
5781 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
5782 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
5783 pr_vrb(": platform does not support raid%d with %d disk%s\n",
5784 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
5788 if (chunk
&& (*chunk
== 0 || *chunk
== UnSet
))
5789 *chunk
= imsm_default_chunk(super
->orom
);
5791 if (super
->orom
&& chunk
&& !imsm_orom_has_chunk(super
->orom
, *chunk
)) {
5792 pr_vrb(": platform does not support a chunk size of: "
5797 if (layout
!= imsm_level_to_layout(level
)) {
5799 pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n");
5800 else if (level
== 10)
5801 pr_vrb(": imsm raid 10 only supports the n2 layout\n");
5803 pr_vrb(": imsm unknown layout %#x for this raid level %d\n",
5810 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
5811 * FIX ME add ahci details
5813 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
5814 int layout
, int raiddisks
, int *chunk
,
5815 unsigned long long size
, char *dev
,
5816 unsigned long long *freesize
,
5820 struct intel_super
*super
= st
->sb
;
5821 struct imsm_super
*mpb
;
5823 unsigned long long pos
= 0;
5824 unsigned long long maxsize
;
5828 /* We must have the container info already read in. */
5832 mpb
= super
->anchor
;
5834 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, verbose
)) {
5835 fprintf(stderr
, Name
": RAID gemetry validation failed. "
5836 "Cannot proceed with the action(s).\n");
5840 /* General test: make sure there is space for
5841 * 'raiddisks' device extents of size 'size' at a given
5844 unsigned long long minsize
= size
;
5845 unsigned long long start_offset
= MaxSector
;
5848 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
5849 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5854 e
= get_extents(super
, dl
);
5857 unsigned long long esize
;
5858 esize
= e
[i
].start
- pos
;
5859 if (esize
>= minsize
)
5861 if (found
&& start_offset
== MaxSector
) {
5864 } else if (found
&& pos
!= start_offset
) {
5868 pos
= e
[i
].start
+ e
[i
].size
;
5870 } while (e
[i
-1].size
);
5875 if (dcnt
< raiddisks
) {
5877 fprintf(stderr
, Name
": imsm: Not enough "
5878 "devices with space for this array "
5886 /* This device must be a member of the set */
5887 if (stat(dev
, &stb
) < 0)
5889 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
5891 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5892 if (dl
->major
== (int)major(stb
.st_rdev
) &&
5893 dl
->minor
== (int)minor(stb
.st_rdev
))
5898 fprintf(stderr
, Name
": %s is not in the "
5899 "same imsm set\n", dev
);
5901 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
5902 /* If a volume is present then the current creation attempt
5903 * cannot incorporate new spares because the orom may not
5904 * understand this configuration (all member disks must be
5905 * members of each array in the container).
5907 fprintf(stderr
, Name
": %s is a spare and a volume"
5908 " is already defined for this container\n", dev
);
5909 fprintf(stderr
, Name
": The option-rom requires all member"
5910 " disks to be a member of all volumes\n");
5912 } else if (super
->orom
&& mpb
->num_raid_devs
> 0 &&
5913 mpb
->num_disks
!= raiddisks
) {
5914 fprintf(stderr
, Name
": The option-rom requires all member"
5915 " disks to be a member of all volumes\n");
5919 /* retrieve the largest free space block */
5920 e
= get_extents(super
, dl
);
5925 unsigned long long esize
;
5927 esize
= e
[i
].start
- pos
;
5928 if (esize
>= maxsize
)
5930 pos
= e
[i
].start
+ e
[i
].size
;
5932 } while (e
[i
-1].size
);
5937 fprintf(stderr
, Name
": unable to determine free space for: %s\n",
5941 if (maxsize
< size
) {
5943 fprintf(stderr
, Name
": %s not enough space (%llu < %llu)\n",
5944 dev
, maxsize
, size
);
5948 /* count total number of extents for merge */
5950 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5952 i
+= dl
->extent_cnt
;
5954 maxsize
= merge_extents(super
, i
);
5956 if (!check_env("IMSM_NO_PLATFORM") &&
5957 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
5958 fprintf(stderr
, Name
": attempting to create a second "
5959 "volume with size less then remaining space. "
5964 if (maxsize
< size
|| maxsize
== 0) {
5967 fprintf(stderr
, Name
": no free space"
5968 " left on device. Aborting...\n");
5970 fprintf(stderr
, Name
": not enough space"
5971 " to create volume of given size"
5972 " (%llu < %llu). Aborting...\n",
5978 *freesize
= maxsize
;
5981 int count
= count_volumes(super
->hba
->path
,
5982 super
->orom
->dpa
, verbose
);
5983 if (super
->orom
->vphba
<= count
) {
5984 pr_vrb(": platform does not support more then %d raid volumes.\n",
5985 super
->orom
->vphba
);
5992 static int reserve_space(struct supertype
*st
, int raiddisks
,
5993 unsigned long long size
, int chunk
,
5994 unsigned long long *freesize
)
5996 struct intel_super
*super
= st
->sb
;
5997 struct imsm_super
*mpb
= super
->anchor
;
6002 unsigned long long maxsize
;
6003 unsigned long long minsize
;
6007 /* find the largest common start free region of the possible disks */
6011 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6017 /* don't activate new spares if we are orom constrained
6018 * and there is already a volume active in the container
6020 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
6023 e
= get_extents(super
, dl
);
6026 for (i
= 1; e
[i
-1].size
; i
++)
6034 maxsize
= merge_extents(super
, extent_cnt
);
6038 minsize
= chunk
* 2;
6040 if (cnt
< raiddisks
||
6041 (super
->orom
&& used
&& used
!= raiddisks
) ||
6042 maxsize
< minsize
||
6044 fprintf(stderr
, Name
": not enough devices with space to create array.\n");
6045 return 0; /* No enough free spaces large enough */
6056 if (!check_env("IMSM_NO_PLATFORM") &&
6057 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
6058 fprintf(stderr
, Name
": attempting to create a second "
6059 "volume with size less then remaining space. "
6064 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6066 dl
->raiddisk
= cnt
++;
6073 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
6074 int raiddisks
, int *chunk
, unsigned long long size
,
6075 char *dev
, unsigned long long *freesize
,
6083 * if given unused devices create a container
6084 * if given given devices in a container create a member volume
6086 if (level
== LEVEL_CONTAINER
) {
6087 /* Must be a fresh device to add to a container */
6088 return validate_geometry_imsm_container(st
, level
, layout
,
6090 chunk
?*chunk
:0, size
,
6097 struct intel_super
*super
= st
->sb
;
6098 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
6102 /* we are being asked to automatically layout a
6103 * new volume based on the current contents of
6104 * the container. If the the parameters can be
6105 * satisfied reserve_space will record the disks,
6106 * start offset, and size of the volume to be
6107 * created. add_to_super and getinfo_super
6108 * detect when autolayout is in progress.
6110 /* assuming that freesize is always given when array is
6112 if (super
->orom
&& freesize
) {
6114 count
= count_volumes(super
->hba
->path
,
6115 super
->orom
->dpa
, verbose
);
6116 if (super
->orom
->vphba
<= count
) {
6117 pr_vrb(": platform does not support more"
6118 "then %d raid volumes.\n",
6119 super
->orom
->vphba
);
6124 return reserve_space(st
, raiddisks
, size
,
6125 chunk
?*chunk
:0, freesize
);
6130 /* creating in a given container */
6131 return validate_geometry_imsm_volume(st
, level
, layout
,
6132 raiddisks
, chunk
, size
,
6133 dev
, freesize
, verbose
);
6136 /* This device needs to be a device in an 'imsm' container */
6137 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
6141 Name
": Cannot create this array on device %s\n",
6146 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
6148 fprintf(stderr
, Name
": Cannot open %s: %s\n",
6149 dev
, strerror(errno
));
6152 /* Well, it is in use by someone, maybe an 'imsm' container. */
6153 cfd
= open_container(fd
);
6157 fprintf(stderr
, Name
": Cannot use %s: It is busy\n",
6161 sra
= sysfs_read(cfd
, 0, GET_VERSION
);
6162 if (sra
&& sra
->array
.major_version
== -1 &&
6163 strcmp(sra
->text_version
, "imsm") == 0)
6167 /* This is a member of a imsm container. Load the container
6168 * and try to create a volume
6170 struct intel_super
*super
;
6172 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
, NULL
, 1) == 0) {
6174 st
->container_dev
= fd2devnum(cfd
);
6176 return validate_geometry_imsm_volume(st
, level
, layout
,
6185 fprintf(stderr
, Name
": failed container membership check\n");
6191 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
6193 struct intel_super
*super
= st
->sb
;
6195 if (level
&& *level
== UnSet
)
6196 *level
= LEVEL_CONTAINER
;
6198 if (level
&& layout
&& *layout
== UnSet
)
6199 *layout
= imsm_level_to_layout(*level
);
6201 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0))
6202 *chunk
= imsm_default_chunk(super
->orom
);
6205 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
6207 static int kill_subarray_imsm(struct supertype
*st
)
6209 /* remove the subarray currently referenced by ->current_vol */
6211 struct intel_dev
**dp
;
6212 struct intel_super
*super
= st
->sb
;
6213 __u8 current_vol
= super
->current_vol
;
6214 struct imsm_super
*mpb
= super
->anchor
;
6216 if (super
->current_vol
< 0)
6218 super
->current_vol
= -1; /* invalidate subarray cursor */
6220 /* block deletions that would change the uuid of active subarrays
6222 * FIXME when immutable ids are available, but note that we'll
6223 * also need to fixup the invalidated/active subarray indexes in
6226 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6229 if (i
< current_vol
)
6231 sprintf(subarray
, "%u", i
);
6232 if (is_subarray_active(subarray
, st
->devname
)) {
6234 Name
": deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
6241 if (st
->update_tail
) {
6242 struct imsm_update_kill_array
*u
= malloc(sizeof(*u
));
6246 u
->type
= update_kill_array
;
6247 u
->dev_idx
= current_vol
;
6248 append_metadata_update(st
, u
, sizeof(*u
));
6253 for (dp
= &super
->devlist
; *dp
;)
6254 if ((*dp
)->index
== current_vol
) {
6257 handle_missing(super
, (*dp
)->dev
);
6258 if ((*dp
)->index
> current_vol
)
6263 /* no more raid devices, all active components are now spares,
6264 * but of course failed are still failed
6266 if (--mpb
->num_raid_devs
== 0) {
6269 for (d
= super
->disks
; d
; d
= d
->next
)
6274 super
->updates_pending
++;
6279 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
6280 char *update
, struct mddev_ident
*ident
)
6282 /* update the subarray currently referenced by ->current_vol */
6283 struct intel_super
*super
= st
->sb
;
6284 struct imsm_super
*mpb
= super
->anchor
;
6286 if (strcmp(update
, "name") == 0) {
6287 char *name
= ident
->name
;
6291 if (is_subarray_active(subarray
, st
->devname
)) {
6293 Name
": Unable to update name of active subarray\n");
6297 if (!check_name(super
, name
, 0))
6300 vol
= strtoul(subarray
, &ep
, 10);
6301 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
6304 if (st
->update_tail
) {
6305 struct imsm_update_rename_array
*u
= malloc(sizeof(*u
));
6309 u
->type
= update_rename_array
;
6311 snprintf((char *) u
->name
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6312 append_metadata_update(st
, u
, sizeof(*u
));
6314 struct imsm_dev
*dev
;
6317 dev
= get_imsm_dev(super
, vol
);
6318 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6319 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6320 dev
= get_imsm_dev(super
, i
);
6321 handle_missing(super
, dev
);
6323 super
->updates_pending
++;
6330 #endif /* MDASSEMBLE */
6332 static int is_gen_migration(struct imsm_dev
*dev
)
6337 if (!dev
->vol
.migr_state
)
6340 if (migr_type(dev
) == MIGR_GEN_MIGR
)
6346 static int is_rebuilding(struct imsm_dev
*dev
)
6348 struct imsm_map
*migr_map
;
6350 if (!dev
->vol
.migr_state
)
6353 if (migr_type(dev
) != MIGR_REBUILD
)
6356 migr_map
= get_imsm_map(dev
, MAP_1
);
6358 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
6365 static int is_initializing(struct imsm_dev
*dev
)
6367 struct imsm_map
*migr_map
;
6369 if (!dev
->vol
.migr_state
)
6372 if (migr_type(dev
) != MIGR_INIT
)
6375 migr_map
= get_imsm_map(dev
, MAP_1
);
6377 if (migr_map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
6384 static void update_recovery_start(struct intel_super
*super
,
6385 struct imsm_dev
*dev
,
6386 struct mdinfo
*array
)
6388 struct mdinfo
*rebuild
= NULL
;
6392 if (!is_rebuilding(dev
))
6395 /* Find the rebuild target, but punt on the dual rebuild case */
6396 for (d
= array
->devs
; d
; d
= d
->next
)
6397 if (d
->recovery_start
== 0) {
6404 /* (?) none of the disks are marked with
6405 * IMSM_ORD_REBUILD, so assume they are missing and the
6406 * disk_ord_tbl was not correctly updated
6408 dprintf("%s: failed to locate out-of-sync disk\n", __func__
);
6412 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
6413 rebuild
->recovery_start
= units
* blocks_per_migr_unit(super
, dev
);
6417 static int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
);
6420 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
6422 /* Given a container loaded by load_super_imsm_all,
6423 * extract information about all the arrays into
6425 * If 'subarray' is given, just extract info about that array.
6427 * For each imsm_dev create an mdinfo, fill it in,
6428 * then look for matching devices in super->disks
6429 * and create appropriate device mdinfo.
6431 struct intel_super
*super
= st
->sb
;
6432 struct imsm_super
*mpb
= super
->anchor
;
6433 struct mdinfo
*rest
= NULL
;
6437 int spare_disks
= 0;
6439 /* do not assemble arrays when not all attributes are supported */
6440 if (imsm_check_attributes(mpb
->attributes
) == 0) {
6442 fprintf(stderr
, Name
": Unsupported attributes in IMSM metadata."
6443 "Arrays activation is blocked.\n");
6446 /* check for bad blocks */
6447 if (imsm_bbm_log_size(super
->anchor
)) {
6448 fprintf(stderr
, Name
": BBM log found in IMSM metadata."
6449 "Arrays activation is blocked.\n");
6454 /* count spare devices, not used in maps
6456 for (d
= super
->disks
; d
; d
= d
->next
)
6460 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6461 struct imsm_dev
*dev
;
6462 struct imsm_map
*map
;
6463 struct imsm_map
*map2
;
6464 struct mdinfo
*this;
6472 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
6475 dev
= get_imsm_dev(super
, i
);
6476 map
= get_imsm_map(dev
, MAP_0
);
6477 map2
= get_imsm_map(dev
, MAP_1
);
6479 /* do not publish arrays that are in the middle of an
6480 * unsupported migration
6482 if (dev
->vol
.migr_state
&&
6483 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
6484 fprintf(stderr
, Name
": cannot assemble volume '%.16s':"
6485 " unsupported migration in progress\n",
6489 /* do not publish arrays that are not support by controller's
6493 this = malloc(sizeof(*this));
6495 fprintf(stderr
, Name
": failed to allocate %zu bytes\n",
6500 super
->current_vol
= i
;
6501 getinfo_super_imsm_volume(st
, this, NULL
);
6504 chunk
= __le16_to_cpu(map
->blocks_per_strip
) >> 1;
6505 /* mdadm does not support all metadata features- set the bit in all arrays state */
6506 if (!validate_geometry_imsm_orom(super
,
6507 get_imsm_raid_level(map
), /* RAID level */
6508 imsm_level_to_layout(get_imsm_raid_level(map
)),
6509 map
->num_members
, /* raid disks */
6512 fprintf(stderr
, Name
": IMSM RAID geometry validation"
6513 " failed. Array %s activation is blocked.\n",
6515 this->array
.state
|=
6516 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
6517 (1<<MD_SB_BLOCK_VOLUME
);
6521 /* if array has bad blocks, set suitable bit in all arrays state */
6523 this->array
.state
|=
6524 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
6525 (1<<MD_SB_BLOCK_VOLUME
);
6527 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
6528 unsigned long long recovery_start
;
6529 struct mdinfo
*info_d
;
6536 idx
= get_imsm_disk_idx(dev
, slot
, MAP_0
);
6537 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
6538 for (d
= super
->disks
; d
; d
= d
->next
)
6539 if (d
->index
== idx
)
6542 recovery_start
= MaxSector
;
6545 if (d
&& is_failed(&d
->disk
))
6547 if (ord
& IMSM_ORD_REBUILD
)
6551 * if we skip some disks the array will be assmebled degraded;
6552 * reset resync start to avoid a dirty-degraded
6553 * situation when performing the intial sync
6555 * FIXME handle dirty degraded
6557 if ((skip
|| recovery_start
== 0) && !dev
->vol
.dirty
)
6558 this->resync_start
= MaxSector
;
6562 info_d
= calloc(1, sizeof(*info_d
));
6564 fprintf(stderr
, Name
": failed to allocate disk"
6565 " for volume %.16s\n", dev
->volume
);
6566 info_d
= this->devs
;
6568 struct mdinfo
*d
= info_d
->next
;
6577 info_d
->next
= this->devs
;
6578 this->devs
= info_d
;
6580 info_d
->disk
.number
= d
->index
;
6581 info_d
->disk
.major
= d
->major
;
6582 info_d
->disk
.minor
= d
->minor
;
6583 info_d
->disk
.raid_disk
= slot
;
6584 info_d
->recovery_start
= recovery_start
;
6586 if (slot
< map2
->num_members
)
6587 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
6589 this->array
.spare_disks
++;
6591 if (slot
< map
->num_members
)
6592 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
6594 this->array
.spare_disks
++;
6596 if (info_d
->recovery_start
== MaxSector
)
6597 this->array
.working_disks
++;
6599 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
6600 info_d
->data_offset
= __le32_to_cpu(map
->pba_of_lba0
);
6601 info_d
->component_size
= __le32_to_cpu(map
->blocks_per_member
);
6603 /* now that the disk list is up-to-date fixup recovery_start */
6604 update_recovery_start(super
, dev
, this);
6605 this->array
.spare_disks
+= spare_disks
;
6608 /* check for reshape */
6609 if (this->reshape_active
== 1)
6610 recover_backup_imsm(st
, this);
6619 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
6620 int failed
, int look_in_map
)
6622 struct imsm_map
*map
;
6624 map
= get_imsm_map(dev
, look_in_map
);
6627 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
6628 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
6630 switch (get_imsm_raid_level(map
)) {
6632 return IMSM_T_STATE_FAILED
;
6635 if (failed
< map
->num_members
)
6636 return IMSM_T_STATE_DEGRADED
;
6638 return IMSM_T_STATE_FAILED
;
6643 * check to see if any mirrors have failed, otherwise we
6644 * are degraded. Even numbered slots are mirrored on
6648 /* gcc -Os complains that this is unused */
6649 int insync
= insync
;
6651 for (i
= 0; i
< map
->num_members
; i
++) {
6652 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_X
);
6653 int idx
= ord_to_idx(ord
);
6654 struct imsm_disk
*disk
;
6656 /* reset the potential in-sync count on even-numbered
6657 * slots. num_copies is always 2 for imsm raid10
6662 disk
= get_imsm_disk(super
, idx
);
6663 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
6666 /* no in-sync disks left in this mirror the
6670 return IMSM_T_STATE_FAILED
;
6673 return IMSM_T_STATE_DEGRADED
;
6677 return IMSM_T_STATE_DEGRADED
;
6679 return IMSM_T_STATE_FAILED
;
6685 return map
->map_state
;
6688 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
6693 struct imsm_disk
*disk
;
6694 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
6695 struct imsm_map
*prev
= get_imsm_map(dev
, MAP_1
);
6696 struct imsm_map
*map_for_loop
;
6701 /* at the beginning of migration we set IMSM_ORD_REBUILD on
6702 * disks that are being rebuilt. New failures are recorded to
6703 * map[0]. So we look through all the disks we started with and
6704 * see if any failures are still present, or if any new ones
6708 if (prev
&& (map
->num_members
< prev
->num_members
))
6709 map_for_loop
= prev
;
6711 for (i
= 0; i
< map_for_loop
->num_members
; i
++) {
6713 /* when MAP_X is passed both maps failures are counted
6716 ((look_in_map
== MAP_1
) || (look_in_map
== MAP_X
)) &&
6717 (i
< prev
->num_members
)) {
6718 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
6719 idx_1
= ord_to_idx(ord
);
6721 disk
= get_imsm_disk(super
, idx_1
);
6722 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
6725 if (((look_in_map
== MAP_0
) || (look_in_map
== MAP_X
)) &&
6726 (i
< map
->num_members
)) {
6727 ord
= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
6728 idx
= ord_to_idx(ord
);
6731 disk
= get_imsm_disk(super
, idx
);
6732 if (!disk
|| is_failed(disk
) ||
6733 ord
& IMSM_ORD_REBUILD
)
6743 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
6746 struct intel_super
*super
= c
->sb
;
6747 struct imsm_super
*mpb
= super
->anchor
;
6749 if (atoi(inst
) >= mpb
->num_raid_devs
) {
6750 fprintf(stderr
, "%s: subarry index %d, out of range\n",
6751 __func__
, atoi(inst
));
6755 dprintf("imsm: open_new %s\n", inst
);
6756 a
->info
.container_member
= atoi(inst
);
6760 static int is_resyncing(struct imsm_dev
*dev
)
6762 struct imsm_map
*migr_map
;
6764 if (!dev
->vol
.migr_state
)
6767 if (migr_type(dev
) == MIGR_INIT
||
6768 migr_type(dev
) == MIGR_REPAIR
)
6771 if (migr_type(dev
) == MIGR_GEN_MIGR
)
6774 migr_map
= get_imsm_map(dev
, MAP_1
);
6776 if ((migr_map
->map_state
== IMSM_T_STATE_NORMAL
) &&
6777 (dev
->vol
.migr_type
!= MIGR_GEN_MIGR
))
6783 /* return true if we recorded new information */
6784 static int mark_failure(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
6788 struct imsm_map
*map
;
6789 char buf
[MAX_RAID_SERIAL_LEN
+3];
6790 unsigned int len
, shift
= 0;
6792 /* new failures are always set in map[0] */
6793 map
= get_imsm_map(dev
, MAP_0
);
6795 slot
= get_imsm_disk_slot(map
, idx
);
6799 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
6800 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
6803 memcpy(buf
, disk
->serial
, MAX_RAID_SERIAL_LEN
);
6804 buf
[MAX_RAID_SERIAL_LEN
] = '\000';
6806 if ((len
= strlen(buf
)) >= MAX_RAID_SERIAL_LEN
)
6807 shift
= len
- MAX_RAID_SERIAL_LEN
+ 1;
6808 strncpy((char *)disk
->serial
, &buf
[shift
], MAX_RAID_SERIAL_LEN
);
6810 disk
->status
|= FAILED_DISK
;
6811 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
6812 /* mark failures in second map if second map exists and this disk
6814 * This is valid for migration, initialization and rebuild
6816 if (dev
->vol
.migr_state
) {
6817 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
6818 int slot2
= get_imsm_disk_slot(map2
, idx
);
6820 if ((slot2
< map2
->num_members
) &&
6822 set_imsm_ord_tbl_ent(map2
, slot2
,
6823 idx
| IMSM_ORD_REBUILD
);
6825 if (map
->failed_disk_num
== 0xff)
6826 map
->failed_disk_num
= slot
;
6830 static void mark_missing(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
6832 mark_failure(dev
, disk
, idx
);
6834 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
6837 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
6838 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
6841 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
6845 if (!super
->missing
)
6848 dprintf("imsm: mark missing\n");
6849 /* end process for initialization and rebuild only
6851 if (is_gen_migration(dev
) == 0) {
6855 failed
= imsm_count_failed(super
, dev
, MAP_0
);
6856 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
6858 end_migration(dev
, super
, map_state
);
6860 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
6861 mark_missing(dev
, &dl
->disk
, dl
->index
);
6862 super
->updates_pending
++;
6865 static unsigned long long imsm_set_array_size(struct imsm_dev
*dev
)
6867 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
6868 unsigned long long array_blocks
;
6869 struct imsm_map
*map
;
6871 if (used_disks
== 0) {
6872 /* when problems occures
6873 * return current array_blocks value
6875 array_blocks
= __le32_to_cpu(dev
->size_high
);
6876 array_blocks
= array_blocks
<< 32;
6877 array_blocks
+= __le32_to_cpu(dev
->size_low
);
6879 return array_blocks
;
6882 /* set array size in metadata
6884 map
= get_imsm_map(dev
, MAP_0
);
6885 array_blocks
= map
->blocks_per_member
* used_disks
;
6887 /* round array size down to closest MB
6889 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
6890 dev
->size_low
= __cpu_to_le32((__u32
)array_blocks
);
6891 dev
->size_high
= __cpu_to_le32((__u32
)(array_blocks
>> 32));
6893 return array_blocks
;
6896 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
6898 static void imsm_progress_container_reshape(struct intel_super
*super
)
6900 /* if no device has a migr_state, but some device has a
6901 * different number of members than the previous device, start
6902 * changing the number of devices in this device to match
6905 struct imsm_super
*mpb
= super
->anchor
;
6906 int prev_disks
= -1;
6910 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6911 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
6912 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
6913 struct imsm_map
*map2
;
6914 int prev_num_members
;
6916 if (dev
->vol
.migr_state
)
6919 if (prev_disks
== -1)
6920 prev_disks
= map
->num_members
;
6921 if (prev_disks
== map
->num_members
)
6924 /* OK, this array needs to enter reshape mode.
6925 * i.e it needs a migr_state
6928 copy_map_size
= sizeof_imsm_map(map
);
6929 prev_num_members
= map
->num_members
;
6930 map
->num_members
= prev_disks
;
6931 dev
->vol
.migr_state
= 1;
6932 dev
->vol
.curr_migr_unit
= 0;
6933 set_migr_type(dev
, MIGR_GEN_MIGR
);
6934 for (i
= prev_num_members
;
6935 i
< map
->num_members
; i
++)
6936 set_imsm_ord_tbl_ent(map
, i
, i
);
6937 map2
= get_imsm_map(dev
, MAP_1
);
6938 /* Copy the current map */
6939 memcpy(map2
, map
, copy_map_size
);
6940 map2
->num_members
= prev_num_members
;
6942 imsm_set_array_size(dev
);
6943 super
->clean_migration_record_by_mdmon
= 1;
6944 super
->updates_pending
++;
6948 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
6949 * states are handled in imsm_set_disk() with one exception, when a
6950 * resync is stopped due to a new failure this routine will set the
6951 * 'degraded' state for the array.
6953 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
6955 int inst
= a
->info
.container_member
;
6956 struct intel_super
*super
= a
->container
->sb
;
6957 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
6958 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
6959 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
6960 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
6961 __u32 blocks_per_unit
;
6963 if (dev
->vol
.migr_state
&&
6964 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
6965 /* array state change is blocked due to reshape action
6967 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
6968 * - finish the reshape (if last_checkpoint is big and action != reshape)
6969 * - update curr_migr_unit
6971 if (a
->curr_action
== reshape
) {
6972 /* still reshaping, maybe update curr_migr_unit */
6973 goto mark_checkpoint
;
6975 if (a
->last_checkpoint
== 0 && a
->prev_action
== reshape
) {
6976 /* for some reason we aborted the reshape.
6978 * disable automatic metadata rollback
6979 * user action is required to recover process
6982 struct imsm_map
*map2
=
6983 get_imsm_map(dev
, MAP_1
);
6984 dev
->vol
.migr_state
= 0;
6985 set_migr_type(dev
, 0);
6986 dev
->vol
.curr_migr_unit
= 0;
6988 sizeof_imsm_map(map2
));
6989 super
->updates_pending
++;
6992 if (a
->last_checkpoint
>= a
->info
.component_size
) {
6993 unsigned long long array_blocks
;
6997 used_disks
= imsm_num_data_members(dev
, MAP_0
);
6998 if (used_disks
> 0) {
7000 map
->blocks_per_member
*
7002 /* round array size down to closest MB
7004 array_blocks
= (array_blocks
7005 >> SECT_PER_MB_SHIFT
)
7006 << SECT_PER_MB_SHIFT
;
7007 a
->info
.custom_array_size
= array_blocks
;
7008 /* encourage manager to update array
7012 a
->check_reshape
= 1;
7014 /* finalize online capacity expansion/reshape */
7015 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
7017 mdi
->disk
.raid_disk
,
7020 imsm_progress_container_reshape(super
);
7025 /* before we activate this array handle any missing disks */
7026 if (consistent
== 2)
7027 handle_missing(super
, dev
);
7029 if (consistent
== 2 &&
7030 (!is_resync_complete(&a
->info
) ||
7031 map_state
!= IMSM_T_STATE_NORMAL
||
7032 dev
->vol
.migr_state
))
7035 if (is_resync_complete(&a
->info
)) {
7036 /* complete intialization / resync,
7037 * recovery and interrupted recovery is completed in
7040 if (is_resyncing(dev
)) {
7041 dprintf("imsm: mark resync done\n");
7042 end_migration(dev
, super
, map_state
);
7043 super
->updates_pending
++;
7044 a
->last_checkpoint
= 0;
7046 } else if ((!is_resyncing(dev
) && !failed
) &&
7047 (imsm_reshape_blocks_arrays_changes(super
) == 0)) {
7048 /* mark the start of the init process if nothing is failed */
7049 dprintf("imsm: mark resync start\n");
7050 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
7051 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
7053 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
7054 super
->updates_pending
++;
7058 /* skip checkpointing for general migration,
7059 * it is controlled in mdadm
7061 if (is_gen_migration(dev
))
7062 goto skip_mark_checkpoint
;
7064 /* check if we can update curr_migr_unit from resync_start, recovery_start */
7065 blocks_per_unit
= blocks_per_migr_unit(super
, dev
);
7066 if (blocks_per_unit
) {
7070 units
= a
->last_checkpoint
/ blocks_per_unit
;
7073 /* check that we did not overflow 32-bits, and that
7074 * curr_migr_unit needs updating
7076 if (units32
== units
&&
7078 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
7079 dprintf("imsm: mark checkpoint (%u)\n", units32
);
7080 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
7081 super
->updates_pending
++;
7085 skip_mark_checkpoint
:
7086 /* mark dirty / clean */
7087 if (dev
->vol
.dirty
!= !consistent
) {
7088 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
7093 super
->updates_pending
++;
7099 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
7101 int inst
= a
->info
.container_member
;
7102 struct intel_super
*super
= a
->container
->sb
;
7103 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7104 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7105 struct imsm_disk
*disk
;
7110 if (n
> map
->num_members
)
7111 fprintf(stderr
, "imsm: set_disk %d out of range 0..%d\n",
7112 n
, map
->num_members
- 1);
7117 dprintf("imsm: set_disk %d:%x\n", n
, state
);
7119 ord
= get_imsm_ord_tbl_ent(dev
, n
, MAP_0
);
7120 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
7122 /* check for new failures */
7123 if (state
& DS_FAULTY
) {
7124 if (mark_failure(dev
, disk
, ord_to_idx(ord
)))
7125 super
->updates_pending
++;
7128 /* check if in_sync */
7129 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
7130 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
7132 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
7133 super
->updates_pending
++;
7136 failed
= imsm_count_failed(super
, dev
, MAP_0
);
7137 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7139 /* check if recovery complete, newly degraded, or failed */
7140 dprintf("imsm: Detected transition to state ");
7141 switch (map_state
) {
7142 case IMSM_T_STATE_NORMAL
: /* transition to normal state */
7143 dprintf("normal: ");
7144 if (is_rebuilding(dev
)) {
7145 dprintf("while rebuilding");
7146 end_migration(dev
, super
, map_state
);
7147 map
= get_imsm_map(dev
, MAP_0
);
7148 map
->failed_disk_num
= ~0;
7149 super
->updates_pending
++;
7150 a
->last_checkpoint
= 0;
7153 if (is_gen_migration(dev
)) {
7154 dprintf("while general migration");
7155 if (a
->last_checkpoint
>= a
->info
.component_size
)
7156 end_migration(dev
, super
, map_state
);
7158 map
->map_state
= map_state
;
7159 map
= get_imsm_map(dev
, MAP_0
);
7160 map
->failed_disk_num
= ~0;
7161 super
->updates_pending
++;
7165 case IMSM_T_STATE_DEGRADED
: /* transition to degraded state */
7166 dprintf("degraded: ");
7167 if ((map
->map_state
!= map_state
) &&
7168 !dev
->vol
.migr_state
) {
7169 dprintf("mark degraded");
7170 map
->map_state
= map_state
;
7171 super
->updates_pending
++;
7172 a
->last_checkpoint
= 0;
7175 if (is_rebuilding(dev
)) {
7176 dprintf("while rebuilding.");
7177 if (map
->map_state
!= map_state
) {
7178 dprintf(" Map state change");
7179 end_migration(dev
, super
, map_state
);
7180 super
->updates_pending
++;
7184 if (is_gen_migration(dev
)) {
7185 dprintf("while general migration");
7186 if (a
->last_checkpoint
>= a
->info
.component_size
)
7187 end_migration(dev
, super
, map_state
);
7189 map
->map_state
= map_state
;
7190 manage_second_map(super
, dev
);
7192 super
->updates_pending
++;
7195 if (is_initializing(dev
)) {
7196 dprintf("while initialization.");
7197 map
->map_state
= map_state
;
7198 super
->updates_pending
++;
7202 case IMSM_T_STATE_FAILED
: /* transition to failed state */
7203 dprintf("failed: ");
7204 if (is_gen_migration(dev
)) {
7205 dprintf("while general migration");
7206 map
->map_state
= map_state
;
7207 super
->updates_pending
++;
7210 if (map
->map_state
!= map_state
) {
7211 dprintf("mark failed");
7212 end_migration(dev
, super
, map_state
);
7213 super
->updates_pending
++;
7214 a
->last_checkpoint
= 0;
7219 dprintf("state %i\n", map_state
);
7225 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
7228 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
7229 unsigned long long dsize
;
7230 unsigned long long sectors
;
7232 get_dev_size(fd
, NULL
, &dsize
);
7234 if (mpb_size
> 512) {
7235 /* -1 to account for anchor */
7236 sectors
= mpb_sectors(mpb
) - 1;
7238 /* write the extended mpb to the sectors preceeding the anchor */
7239 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0)
7242 if ((unsigned long long)write(fd
, buf
+ 512, 512 * sectors
)
7247 /* first block is stored on second to last sector of the disk */
7248 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0)
7251 if (write(fd
, buf
, 512) != 512)
7257 static void imsm_sync_metadata(struct supertype
*container
)
7259 struct intel_super
*super
= container
->sb
;
7261 dprintf("sync metadata: %d\n", super
->updates_pending
);
7262 if (!super
->updates_pending
)
7265 write_super_imsm(container
, 0);
7267 super
->updates_pending
= 0;
7270 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
7272 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
7273 int i
= get_imsm_disk_idx(dev
, idx
, MAP_X
);
7276 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7280 if (dl
&& is_failed(&dl
->disk
))
7284 dprintf("%s: found %x:%x\n", __func__
, dl
->major
, dl
->minor
);
7289 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
7290 struct active_array
*a
, int activate_new
,
7291 struct mdinfo
*additional_test_list
)
7293 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
7294 int idx
= get_imsm_disk_idx(dev
, slot
, MAP_X
);
7295 struct imsm_super
*mpb
= super
->anchor
;
7296 struct imsm_map
*map
;
7297 unsigned long long pos
;
7302 __u32 array_start
= 0;
7303 __u32 array_end
= 0;
7305 struct mdinfo
*test_list
;
7307 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7308 /* If in this array, skip */
7309 for (d
= a
->info
.devs
; d
; d
= d
->next
)
7310 if (d
->state_fd
>= 0 &&
7311 d
->disk
.major
== dl
->major
&&
7312 d
->disk
.minor
== dl
->minor
) {
7313 dprintf("%x:%x already in array\n",
7314 dl
->major
, dl
->minor
);
7319 test_list
= additional_test_list
;
7321 if (test_list
->disk
.major
== dl
->major
&&
7322 test_list
->disk
.minor
== dl
->minor
) {
7323 dprintf("%x:%x already in additional test list\n",
7324 dl
->major
, dl
->minor
);
7327 test_list
= test_list
->next
;
7332 /* skip in use or failed drives */
7333 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
7335 dprintf("%x:%x status (failed: %d index: %d)\n",
7336 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
7340 /* skip pure spares when we are looking for partially
7341 * assimilated drives
7343 if (dl
->index
== -1 && !activate_new
)
7346 /* Does this unused device have the requisite free space?
7347 * It needs to be able to cover all member volumes
7349 ex
= get_extents(super
, dl
);
7351 dprintf("cannot get extents\n");
7354 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7355 dev
= get_imsm_dev(super
, i
);
7356 map
= get_imsm_map(dev
, MAP_0
);
7358 /* check if this disk is already a member of
7361 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
7367 array_start
= __le32_to_cpu(map
->pba_of_lba0
);
7368 array_end
= array_start
+
7369 __le32_to_cpu(map
->blocks_per_member
) - 1;
7372 /* check that we can start at pba_of_lba0 with
7373 * blocks_per_member of space
7375 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
7379 pos
= ex
[j
].start
+ ex
[j
].size
;
7381 } while (ex
[j
-1].size
);
7388 if (i
< mpb
->num_raid_devs
) {
7389 dprintf("%x:%x does not have %u to %u available\n",
7390 dl
->major
, dl
->minor
, array_start
, array_end
);
7401 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
7403 struct imsm_dev
*dev2
;
7404 struct imsm_map
*map
;
7410 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
7412 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
, MAP_0
);
7413 if (state
== IMSM_T_STATE_FAILED
) {
7414 map
= get_imsm_map(dev2
, MAP_0
);
7417 for (slot
= 0; slot
< map
->num_members
; slot
++) {
7419 * Check if failed disks are deleted from intel
7420 * disk list or are marked to be deleted
7422 idx
= get_imsm_disk_idx(dev2
, slot
, MAP_X
);
7423 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
7425 * Do not rebuild the array if failed disks
7426 * from failed sub-array are not removed from
7430 is_failed(&idisk
->disk
) &&
7431 (idisk
->action
!= DISK_REMOVE
))
7439 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
7440 struct metadata_update
**updates
)
7443 * Find a device with unused free space and use it to replace a
7444 * failed/vacant region in an array. We replace failed regions one a
7445 * array at a time. The result is that a new spare disk will be added
7446 * to the first failed array and after the monitor has finished
7447 * propagating failures the remainder will be consumed.
7449 * FIXME add a capability for mdmon to request spares from another
7453 struct intel_super
*super
= a
->container
->sb
;
7454 int inst
= a
->info
.container_member
;
7455 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7456 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7457 int failed
= a
->info
.array
.raid_disks
;
7458 struct mdinfo
*rv
= NULL
;
7461 struct metadata_update
*mu
;
7463 struct imsm_update_activate_spare
*u
;
7468 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
7469 if ((d
->curr_state
& DS_FAULTY
) &&
7471 /* wait for Removal to happen */
7473 if (d
->state_fd
>= 0)
7477 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
7478 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
7480 if (imsm_reshape_blocks_arrays_changes(super
))
7483 /* Cannot activate another spare if rebuild is in progress already
7485 if (is_rebuilding(dev
)) {
7486 dprintf("imsm: No spare activation allowed. "
7487 "Rebuild in progress already.\n");
7491 if (a
->info
.array
.level
== 4)
7492 /* No repair for takeovered array
7493 * imsm doesn't support raid4
7497 if (imsm_check_degraded(super
, dev
, failed
, MAP_0
) !=
7498 IMSM_T_STATE_DEGRADED
)
7502 * If there are any failed disks check state of the other volume.
7503 * Block rebuild if the another one is failed until failed disks
7504 * are removed from container.
7507 dprintf("found failed disks in %.*s, check if there another"
7508 "failed sub-array.\n",
7509 MAX_RAID_SERIAL_LEN
, dev
->volume
);
7510 /* check if states of the other volumes allow for rebuild */
7511 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
7513 allowed
= imsm_rebuild_allowed(a
->container
,
7521 /* For each slot, if it is not working, find a spare */
7522 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
7523 for (d
= a
->info
.devs
; d
; d
= d
->next
)
7524 if (d
->disk
.raid_disk
== i
)
7526 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
7527 if (d
&& (d
->state_fd
>= 0))
7531 * OK, this device needs recovery. Try to re-add the
7532 * previous occupant of this slot, if this fails see if
7533 * we can continue the assimilation of a spare that was
7534 * partially assimilated, finally try to activate a new
7537 dl
= imsm_readd(super
, i
, a
);
7539 dl
= imsm_add_spare(super
, i
, a
, 0, rv
);
7541 dl
= imsm_add_spare(super
, i
, a
, 1, rv
);
7545 /* found a usable disk with enough space */
7546 di
= malloc(sizeof(*di
));
7549 memset(di
, 0, sizeof(*di
));
7551 /* dl->index will be -1 in the case we are activating a
7552 * pristine spare. imsm_process_update() will create a
7553 * new index in this case. Once a disk is found to be
7554 * failed in all member arrays it is kicked from the
7557 di
->disk
.number
= dl
->index
;
7559 /* (ab)use di->devs to store a pointer to the device
7562 di
->devs
= (struct mdinfo
*) dl
;
7564 di
->disk
.raid_disk
= i
;
7565 di
->disk
.major
= dl
->major
;
7566 di
->disk
.minor
= dl
->minor
;
7568 di
->recovery_start
= 0;
7569 di
->data_offset
= __le32_to_cpu(map
->pba_of_lba0
);
7570 di
->component_size
= a
->info
.component_size
;
7571 di
->container_member
= inst
;
7572 super
->random
= random32();
7576 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
7577 i
, di
->data_offset
);
7581 /* No spares found */
7583 /* Now 'rv' has a list of devices to return.
7584 * Create a metadata_update record to update the
7585 * disk_ord_tbl for the array
7587 mu
= malloc(sizeof(*mu
));
7589 mu
->buf
= malloc(sizeof(struct imsm_update_activate_spare
) * num_spares
);
7590 if (mu
->buf
== NULL
) {
7597 struct mdinfo
*n
= rv
->next
;
7606 mu
->space_list
= NULL
;
7607 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
7608 mu
->next
= *updates
;
7609 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
7611 for (di
= rv
; di
; di
= di
->next
) {
7612 u
->type
= update_activate_spare
;
7613 u
->dl
= (struct dl
*) di
->devs
;
7615 u
->slot
= di
->disk
.raid_disk
;
7626 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
7628 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
7629 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7630 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, MAP_0
);
7631 struct disk_info
*inf
= get_disk_info(u
);
7632 struct imsm_disk
*disk
;
7636 for (i
= 0; i
< map
->num_members
; i
++) {
7637 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, MAP_X
));
7638 for (j
= 0; j
< new_map
->num_members
; j
++)
7639 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
7647 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
7649 struct dl
*dl
= NULL
;
7650 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7651 if ((dl
->major
== major
) && (dl
->minor
== minor
))
7656 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
7658 struct dl
*prev
= NULL
;
7662 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7663 if ((dl
->major
== major
) && (dl
->minor
== minor
)) {
7666 prev
->next
= dl
->next
;
7668 super
->disks
= dl
->next
;
7670 __free_imsm_disk(dl
);
7671 dprintf("%s: removed %x:%x\n",
7672 __func__
, major
, minor
);
7680 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
7682 static int add_remove_disk_update(struct intel_super
*super
)
7684 int check_degraded
= 0;
7685 struct dl
*disk
= NULL
;
7686 /* add/remove some spares to/from the metadata/contrainer */
7687 while (super
->disk_mgmt_list
) {
7688 struct dl
*disk_cfg
;
7690 disk_cfg
= super
->disk_mgmt_list
;
7691 super
->disk_mgmt_list
= disk_cfg
->next
;
7692 disk_cfg
->next
= NULL
;
7694 if (disk_cfg
->action
== DISK_ADD
) {
7695 disk_cfg
->next
= super
->disks
;
7696 super
->disks
= disk_cfg
;
7698 dprintf("%s: added %x:%x\n",
7699 __func__
, disk_cfg
->major
,
7701 } else if (disk_cfg
->action
== DISK_REMOVE
) {
7702 dprintf("Disk remove action processed: %x.%x\n",
7703 disk_cfg
->major
, disk_cfg
->minor
);
7704 disk
= get_disk_super(super
,
7708 /* store action status */
7709 disk
->action
= DISK_REMOVE
;
7710 /* remove spare disks only */
7711 if (disk
->index
== -1) {
7712 remove_disk_super(super
,
7717 /* release allocate disk structure */
7718 __free_imsm_disk(disk_cfg
);
7721 return check_degraded
;
7725 static int apply_reshape_migration_update(struct imsm_update_reshape_migration
*u
,
7726 struct intel_super
*super
,
7729 struct intel_dev
*id
;
7730 void **tofree
= NULL
;
7733 dprintf("apply_reshape_migration_update()\n");
7734 if ((u
->subdev
< 0) ||
7736 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
7739 if ((space_list
== NULL
) || (*space_list
== NULL
)) {
7740 dprintf("imsm: Error: Memory is not allocated\n");
7744 for (id
= super
->devlist
; id
; id
= id
->next
) {
7745 if (id
->index
== (unsigned)u
->subdev
) {
7746 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
7747 struct imsm_map
*map
;
7748 struct imsm_dev
*new_dev
=
7749 (struct imsm_dev
*)*space_list
;
7750 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
7752 struct dl
*new_disk
;
7754 if (new_dev
== NULL
)
7756 *space_list
= **space_list
;
7757 memcpy(new_dev
, dev
, sizeof_imsm_dev(dev
, 0));
7758 map
= get_imsm_map(new_dev
, MAP_0
);
7760 dprintf("imsm: Error: migration in progress");
7764 to_state
= map
->map_state
;
7765 if ((u
->new_level
== 5) && (map
->raid_level
== 0)) {
7767 /* this should not happen */
7768 if (u
->new_disks
[0] < 0) {
7769 map
->failed_disk_num
=
7770 map
->num_members
- 1;
7771 to_state
= IMSM_T_STATE_DEGRADED
;
7773 to_state
= IMSM_T_STATE_NORMAL
;
7775 migrate(new_dev
, super
, to_state
, MIGR_GEN_MIGR
);
7776 if (u
->new_level
> -1)
7777 map
->raid_level
= u
->new_level
;
7778 migr_map
= get_imsm_map(new_dev
, MAP_1
);
7779 if ((u
->new_level
== 5) &&
7780 (migr_map
->raid_level
== 0)) {
7781 int ord
= map
->num_members
- 1;
7782 migr_map
->num_members
--;
7783 if (u
->new_disks
[0] < 0)
7784 ord
|= IMSM_ORD_REBUILD
;
7785 set_imsm_ord_tbl_ent(map
,
7786 map
->num_members
- 1,
7790 tofree
= (void **)dev
;
7792 /* update chunk size
7794 if (u
->new_chunksize
> 0)
7795 map
->blocks_per_strip
=
7796 __cpu_to_le16(u
->new_chunksize
* 2);
7800 if ((u
->new_level
!= 5) ||
7801 (migr_map
->raid_level
!= 0) ||
7802 (migr_map
->raid_level
== map
->raid_level
))
7805 if (u
->new_disks
[0] >= 0) {
7808 new_disk
= get_disk_super(super
,
7809 major(u
->new_disks
[0]),
7810 minor(u
->new_disks
[0]));
7811 dprintf("imsm: new disk for reshape is: %i:%i "
7812 "(%p, index = %i)\n",
7813 major(u
->new_disks
[0]),
7814 minor(u
->new_disks
[0]),
7815 new_disk
, new_disk
->index
);
7816 if (new_disk
== NULL
)
7817 goto error_disk_add
;
7819 new_disk
->index
= map
->num_members
- 1;
7820 /* slot to fill in autolayout
7822 new_disk
->raiddisk
= new_disk
->index
;
7823 new_disk
->disk
.status
|= CONFIGURED_DISK
;
7824 new_disk
->disk
.status
&= ~SPARE_DISK
;
7826 goto error_disk_add
;
7829 *tofree
= *space_list
;
7830 /* calculate new size
7832 imsm_set_array_size(new_dev
);
7839 *space_list
= tofree
;
7843 dprintf("Error: imsm: Cannot find disk.\n");
7847 static int apply_update_activate_spare(struct imsm_update_activate_spare
*u
,
7848 struct intel_super
*super
,
7849 struct active_array
*active_array
)
7851 struct imsm_super
*mpb
= super
->anchor
;
7852 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
7853 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7854 struct imsm_map
*migr_map
;
7855 struct active_array
*a
;
7856 struct imsm_disk
*disk
;
7863 int second_map_created
= 0;
7865 for (; u
; u
= u
->next
) {
7866 victim
= get_imsm_disk_idx(dev
, u
->slot
, MAP_X
);
7871 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7876 fprintf(stderr
, "error: imsm_activate_spare passed "
7877 "an unknown disk (index: %d)\n",
7882 /* count failures (excluding rebuilds and the victim)
7883 * to determine map[0] state
7886 for (i
= 0; i
< map
->num_members
; i
++) {
7889 disk
= get_imsm_disk(super
,
7890 get_imsm_disk_idx(dev
, i
, MAP_X
));
7891 if (!disk
|| is_failed(disk
))
7895 /* adding a pristine spare, assign a new index */
7896 if (dl
->index
< 0) {
7897 dl
->index
= super
->anchor
->num_disks
;
7898 super
->anchor
->num_disks
++;
7901 disk
->status
|= CONFIGURED_DISK
;
7902 disk
->status
&= ~SPARE_DISK
;
7905 to_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7906 if (!second_map_created
) {
7907 second_map_created
= 1;
7908 map
->map_state
= IMSM_T_STATE_DEGRADED
;
7909 migrate(dev
, super
, to_state
, MIGR_REBUILD
);
7911 map
->map_state
= to_state
;
7912 migr_map
= get_imsm_map(dev
, MAP_1
);
7913 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
7914 set_imsm_ord_tbl_ent(migr_map
, u
->slot
,
7915 dl
->index
| IMSM_ORD_REBUILD
);
7917 /* update the family_num to mark a new container
7918 * generation, being careful to record the existing
7919 * family_num in orig_family_num to clean up after
7920 * earlier mdadm versions that neglected to set it.
7922 if (mpb
->orig_family_num
== 0)
7923 mpb
->orig_family_num
= mpb
->family_num
;
7924 mpb
->family_num
+= super
->random
;
7926 /* count arrays using the victim in the metadata */
7928 for (a
= active_array
; a
; a
= a
->next
) {
7929 dev
= get_imsm_dev(super
, a
->info
.container_member
);
7930 map
= get_imsm_map(dev
, MAP_0
);
7932 if (get_imsm_disk_slot(map
, victim
) >= 0)
7936 /* delete the victim if it is no longer being
7942 /* We know that 'manager' isn't touching anything,
7943 * so it is safe to delete
7945 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
7946 if ((*dlp
)->index
== victim
)
7949 /* victim may be on the missing list */
7951 for (dlp
= &super
->missing
; *dlp
;
7952 dlp
= &(*dlp
)->next
)
7953 if ((*dlp
)->index
== victim
)
7955 imsm_delete(super
, dlp
, victim
);
7962 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
7963 struct intel_super
*super
,
7966 struct dl
*new_disk
;
7967 struct intel_dev
*id
;
7969 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
7970 int disk_count
= u
->old_raid_disks
;
7971 void **tofree
= NULL
;
7972 int devices_to_reshape
= 1;
7973 struct imsm_super
*mpb
= super
->anchor
;
7975 unsigned int dev_id
;
7977 dprintf("imsm: apply_reshape_container_disks_update()\n");
7979 /* enable spares to use in array */
7980 for (i
= 0; i
< delta_disks
; i
++) {
7981 new_disk
= get_disk_super(super
,
7982 major(u
->new_disks
[i
]),
7983 minor(u
->new_disks
[i
]));
7984 dprintf("imsm: new disk for reshape is: %i:%i "
7985 "(%p, index = %i)\n",
7986 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
7987 new_disk
, new_disk
->index
);
7988 if ((new_disk
== NULL
) ||
7989 ((new_disk
->index
>= 0) &&
7990 (new_disk
->index
< u
->old_raid_disks
)))
7991 goto update_reshape_exit
;
7992 new_disk
->index
= disk_count
++;
7993 /* slot to fill in autolayout
7995 new_disk
->raiddisk
= new_disk
->index
;
7996 new_disk
->disk
.status
|=
7998 new_disk
->disk
.status
&= ~SPARE_DISK
;
8001 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
8002 mpb
->num_raid_devs
);
8003 /* manage changes in volume
8005 for (dev_id
= 0; dev_id
< mpb
->num_raid_devs
; dev_id
++) {
8006 void **sp
= *space_list
;
8007 struct imsm_dev
*newdev
;
8008 struct imsm_map
*newmap
, *oldmap
;
8010 for (id
= super
->devlist
; id
; id
= id
->next
) {
8011 if (id
->index
== dev_id
)
8020 /* Copy the dev, but not (all of) the map */
8021 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
8022 oldmap
= get_imsm_map(id
->dev
, MAP_0
);
8023 newmap
= get_imsm_map(newdev
, MAP_0
);
8024 /* Copy the current map */
8025 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8026 /* update one device only
8028 if (devices_to_reshape
) {
8029 dprintf("imsm: modifying subdev: %i\n",
8031 devices_to_reshape
--;
8032 newdev
->vol
.migr_state
= 1;
8033 newdev
->vol
.curr_migr_unit
= 0;
8034 set_migr_type(newdev
, MIGR_GEN_MIGR
);
8035 newmap
->num_members
= u
->new_raid_disks
;
8036 for (i
= 0; i
< delta_disks
; i
++) {
8037 set_imsm_ord_tbl_ent(newmap
,
8038 u
->old_raid_disks
+ i
,
8039 u
->old_raid_disks
+ i
);
8041 /* New map is correct, now need to save old map
8043 newmap
= get_imsm_map(newdev
, MAP_1
);
8044 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8046 imsm_set_array_size(newdev
);
8049 sp
= (void **)id
->dev
;
8054 /* Clear migration record */
8055 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
8058 *space_list
= tofree
;
8061 update_reshape_exit
:
8066 static int apply_takeover_update(struct imsm_update_takeover
*u
,
8067 struct intel_super
*super
,
8070 struct imsm_dev
*dev
= NULL
;
8071 struct intel_dev
*dv
;
8072 struct imsm_dev
*dev_new
;
8073 struct imsm_map
*map
;
8077 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
8078 if (dv
->index
== (unsigned int)u
->subarray
) {
8086 map
= get_imsm_map(dev
, MAP_0
);
8088 if (u
->direction
== R10_TO_R0
) {
8089 /* Number of failed disks must be half of initial disk number */
8090 if (imsm_count_failed(super
, dev
, MAP_0
) !=
8091 (map
->num_members
/ 2))
8094 /* iterate through devices to mark removed disks as spare */
8095 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
8096 if (dm
->disk
.status
& FAILED_DISK
) {
8097 int idx
= dm
->index
;
8098 /* update indexes on the disk list */
8099 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
8100 the index values will end up being correct.... NB */
8101 for (du
= super
->disks
; du
; du
= du
->next
)
8102 if (du
->index
> idx
)
8104 /* mark as spare disk */
8109 map
->num_members
= map
->num_members
/ 2;
8110 map
->map_state
= IMSM_T_STATE_NORMAL
;
8111 map
->num_domains
= 1;
8112 map
->raid_level
= 0;
8113 map
->failed_disk_num
= -1;
8116 if (u
->direction
== R0_TO_R10
) {
8118 /* update slots in current disk list */
8119 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
8123 /* create new *missing* disks */
8124 for (i
= 0; i
< map
->num_members
; i
++) {
8125 space
= *space_list
;
8128 *space_list
= *space
;
8130 memcpy(du
, super
->disks
, sizeof(*du
));
8134 du
->index
= (i
* 2) + 1;
8135 sprintf((char *)du
->disk
.serial
,
8136 " MISSING_%d", du
->index
);
8137 sprintf((char *)du
->serial
,
8138 "MISSING_%d", du
->index
);
8139 du
->next
= super
->missing
;
8140 super
->missing
= du
;
8142 /* create new dev and map */
8143 space
= *space_list
;
8146 *space_list
= *space
;
8147 dev_new
= (void *)space
;
8148 memcpy(dev_new
, dev
, sizeof(*dev
));
8149 /* update new map */
8150 map
= get_imsm_map(dev_new
, MAP_0
);
8151 map
->num_members
= map
->num_members
* 2;
8152 map
->map_state
= IMSM_T_STATE_DEGRADED
;
8153 map
->num_domains
= 2;
8154 map
->raid_level
= 1;
8155 /* replace dev<->dev_new */
8158 /* update disk order table */
8159 for (du
= super
->disks
; du
; du
= du
->next
)
8161 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
8162 for (du
= super
->missing
; du
; du
= du
->next
)
8163 if (du
->index
>= 0) {
8164 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
8165 mark_missing(dv
->dev
, &du
->disk
, du
->index
);
8171 static void imsm_process_update(struct supertype
*st
,
8172 struct metadata_update
*update
)
8175 * crack open the metadata_update envelope to find the update record
8176 * update can be one of:
8177 * update_reshape_container_disks - all the arrays in the container
8178 * are being reshaped to have more devices. We need to mark
8179 * the arrays for general migration and convert selected spares
8180 * into active devices.
8181 * update_activate_spare - a spare device has replaced a failed
8182 * device in an array, update the disk_ord_tbl. If this disk is
8183 * present in all member arrays then also clear the SPARE_DISK
8185 * update_create_array
8187 * update_rename_array
8188 * update_add_remove_disk
8190 struct intel_super
*super
= st
->sb
;
8191 struct imsm_super
*mpb
;
8192 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
8194 /* update requires a larger buf but the allocation failed */
8195 if (super
->next_len
&& !super
->next_buf
) {
8196 super
->next_len
= 0;
8200 if (super
->next_buf
) {
8201 memcpy(super
->next_buf
, super
->buf
, super
->len
);
8203 super
->len
= super
->next_len
;
8204 super
->buf
= super
->next_buf
;
8206 super
->next_len
= 0;
8207 super
->next_buf
= NULL
;
8210 mpb
= super
->anchor
;
8213 case update_general_migration_checkpoint
: {
8214 struct intel_dev
*id
;
8215 struct imsm_update_general_migration_checkpoint
*u
=
8216 (void *)update
->buf
;
8218 dprintf("imsm: process_update() "
8219 "for update_general_migration_checkpoint called\n");
8221 /* find device under general migration */
8222 for (id
= super
->devlist
; id
; id
= id
->next
) {
8223 if (is_gen_migration(id
->dev
)) {
8224 id
->dev
->vol
.curr_migr_unit
=
8225 __cpu_to_le32(u
->curr_migr_unit
);
8226 super
->updates_pending
++;
8231 case update_takeover
: {
8232 struct imsm_update_takeover
*u
= (void *)update
->buf
;
8233 if (apply_takeover_update(u
, super
, &update
->space_list
)) {
8234 imsm_update_version_info(super
);
8235 super
->updates_pending
++;
8240 case update_reshape_container_disks
: {
8241 struct imsm_update_reshape
*u
= (void *)update
->buf
;
8242 if (apply_reshape_container_disks_update(
8243 u
, super
, &update
->space_list
))
8244 super
->updates_pending
++;
8247 case update_reshape_migration
: {
8248 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
8249 if (apply_reshape_migration_update(
8250 u
, super
, &update
->space_list
))
8251 super
->updates_pending
++;
8254 case update_activate_spare
: {
8255 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
8256 if (apply_update_activate_spare(u
, super
, st
->arrays
))
8257 super
->updates_pending
++;
8260 case update_create_array
: {
8261 /* someone wants to create a new array, we need to be aware of
8262 * a few races/collisions:
8263 * 1/ 'Create' called by two separate instances of mdadm
8264 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
8265 * devices that have since been assimilated via
8267 * In the event this update can not be carried out mdadm will
8268 * (FIX ME) notice that its update did not take hold.
8270 struct imsm_update_create_array
*u
= (void *) update
->buf
;
8271 struct intel_dev
*dv
;
8272 struct imsm_dev
*dev
;
8273 struct imsm_map
*map
, *new_map
;
8274 unsigned long long start
, end
;
8275 unsigned long long new_start
, new_end
;
8277 struct disk_info
*inf
;
8280 /* handle racing creates: first come first serve */
8281 if (u
->dev_idx
< mpb
->num_raid_devs
) {
8282 dprintf("%s: subarray %d already defined\n",
8283 __func__
, u
->dev_idx
);
8287 /* check update is next in sequence */
8288 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
8289 dprintf("%s: can not create array %d expected index %d\n",
8290 __func__
, u
->dev_idx
, mpb
->num_raid_devs
);
8294 new_map
= get_imsm_map(&u
->dev
, MAP_0
);
8295 new_start
= __le32_to_cpu(new_map
->pba_of_lba0
);
8296 new_end
= new_start
+ __le32_to_cpu(new_map
->blocks_per_member
);
8297 inf
= get_disk_info(u
);
8299 /* handle activate_spare versus create race:
8300 * check to make sure that overlapping arrays do not include
8303 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8304 dev
= get_imsm_dev(super
, i
);
8305 map
= get_imsm_map(dev
, MAP_0
);
8306 start
= __le32_to_cpu(map
->pba_of_lba0
);
8307 end
= start
+ __le32_to_cpu(map
->blocks_per_member
);
8308 if ((new_start
>= start
&& new_start
<= end
) ||
8309 (start
>= new_start
&& start
<= new_end
))
8314 if (disks_overlap(super
, i
, u
)) {
8315 dprintf("%s: arrays overlap\n", __func__
);
8320 /* check that prepare update was successful */
8321 if (!update
->space
) {
8322 dprintf("%s: prepare update failed\n", __func__
);
8326 /* check that all disks are still active before committing
8327 * changes. FIXME: could we instead handle this by creating a
8328 * degraded array? That's probably not what the user expects,
8329 * so better to drop this update on the floor.
8331 for (i
= 0; i
< new_map
->num_members
; i
++) {
8332 dl
= serial_to_dl(inf
[i
].serial
, super
);
8334 dprintf("%s: disk disappeared\n", __func__
);
8339 super
->updates_pending
++;
8341 /* convert spares to members and fixup ord_tbl */
8342 for (i
= 0; i
< new_map
->num_members
; i
++) {
8343 dl
= serial_to_dl(inf
[i
].serial
, super
);
8344 if (dl
->index
== -1) {
8345 dl
->index
= mpb
->num_disks
;
8347 dl
->disk
.status
|= CONFIGURED_DISK
;
8348 dl
->disk
.status
&= ~SPARE_DISK
;
8350 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
8355 update
->space
= NULL
;
8356 imsm_copy_dev(dev
, &u
->dev
);
8357 dv
->index
= u
->dev_idx
;
8358 dv
->next
= super
->devlist
;
8359 super
->devlist
= dv
;
8360 mpb
->num_raid_devs
++;
8362 imsm_update_version_info(super
);
8365 /* mdmon knows how to release update->space, but not
8366 * ((struct intel_dev *) update->space)->dev
8368 if (update
->space
) {
8374 case update_kill_array
: {
8375 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
8376 int victim
= u
->dev_idx
;
8377 struct active_array
*a
;
8378 struct intel_dev
**dp
;
8379 struct imsm_dev
*dev
;
8381 /* sanity check that we are not affecting the uuid of
8382 * active arrays, or deleting an active array
8384 * FIXME when immutable ids are available, but note that
8385 * we'll also need to fixup the invalidated/active
8386 * subarray indexes in mdstat
8388 for (a
= st
->arrays
; a
; a
= a
->next
)
8389 if (a
->info
.container_member
>= victim
)
8391 /* by definition if mdmon is running at least one array
8392 * is active in the container, so checking
8393 * mpb->num_raid_devs is just extra paranoia
8395 dev
= get_imsm_dev(super
, victim
);
8396 if (a
|| !dev
|| mpb
->num_raid_devs
== 1) {
8397 dprintf("failed to delete subarray-%d\n", victim
);
8401 for (dp
= &super
->devlist
; *dp
;)
8402 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
8405 if ((*dp
)->index
> (unsigned)victim
)
8409 mpb
->num_raid_devs
--;
8410 super
->updates_pending
++;
8413 case update_rename_array
: {
8414 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
8415 char name
[MAX_RAID_SERIAL_LEN
+1];
8416 int target
= u
->dev_idx
;
8417 struct active_array
*a
;
8418 struct imsm_dev
*dev
;
8420 /* sanity check that we are not affecting the uuid of
8423 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
8424 name
[MAX_RAID_SERIAL_LEN
] = '\0';
8425 for (a
= st
->arrays
; a
; a
= a
->next
)
8426 if (a
->info
.container_member
== target
)
8428 dev
= get_imsm_dev(super
, u
->dev_idx
);
8429 if (a
|| !dev
|| !check_name(super
, name
, 1)) {
8430 dprintf("failed to rename subarray-%d\n", target
);
8434 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
8435 super
->updates_pending
++;
8438 case update_add_remove_disk
: {
8439 /* we may be able to repair some arrays if disks are
8440 * being added, check teh status of add_remove_disk
8441 * if discs has been added.
8443 if (add_remove_disk_update(super
)) {
8444 struct active_array
*a
;
8446 super
->updates_pending
++;
8447 for (a
= st
->arrays
; a
; a
= a
->next
)
8448 a
->check_degraded
= 1;
8453 fprintf(stderr
, "error: unsuported process update type:"
8454 "(type: %d)\n", type
);
8458 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
);
8460 static void imsm_prepare_update(struct supertype
*st
,
8461 struct metadata_update
*update
)
8464 * Allocate space to hold new disk entries, raid-device entries or a new
8465 * mpb if necessary. The manager synchronously waits for updates to
8466 * complete in the monitor, so new mpb buffers allocated here can be
8467 * integrated by the monitor thread without worrying about live pointers
8468 * in the manager thread.
8470 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
8471 struct intel_super
*super
= st
->sb
;
8472 struct imsm_super
*mpb
= super
->anchor
;
8477 case update_general_migration_checkpoint
:
8478 dprintf("imsm: prepare_update() "
8479 "for update_general_migration_checkpoint called\n");
8481 case update_takeover
: {
8482 struct imsm_update_takeover
*u
= (void *)update
->buf
;
8483 if (u
->direction
== R0_TO_R10
) {
8484 void **tail
= (void **)&update
->space_list
;
8485 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subarray
);
8486 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8487 int num_members
= map
->num_members
;
8491 /* allocate memory for added disks */
8492 for (i
= 0; i
< num_members
; i
++) {
8493 size
= sizeof(struct dl
);
8494 space
= malloc(size
);
8503 /* allocate memory for new device */
8504 size
= sizeof_imsm_dev(super
->devlist
->dev
, 0) +
8505 (num_members
* sizeof(__u32
));
8506 space
= malloc(size
);
8515 len
= disks_to_mpb_size(num_members
* 2);
8517 /* if allocation didn't success, free buffer */
8518 while (update
->space_list
) {
8519 void **sp
= update
->space_list
;
8520 update
->space_list
= *sp
;
8528 case update_reshape_container_disks
: {
8529 /* Every raid device in the container is about to
8530 * gain some more devices, and we will enter a
8532 * So each 'imsm_map' will be bigger, and the imsm_vol
8533 * will now hold 2 of them.
8534 * Thus we need new 'struct imsm_dev' allocations sized
8535 * as sizeof_imsm_dev but with more devices in both maps.
8537 struct imsm_update_reshape
*u
= (void *)update
->buf
;
8538 struct intel_dev
*dl
;
8539 void **space_tail
= (void**)&update
->space_list
;
8541 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
8543 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
8544 int size
= sizeof_imsm_dev(dl
->dev
, 1);
8546 if (u
->new_raid_disks
> u
->old_raid_disks
)
8547 size
+= sizeof(__u32
)*2*
8548 (u
->new_raid_disks
- u
->old_raid_disks
);
8557 len
= disks_to_mpb_size(u
->new_raid_disks
);
8558 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
8561 case update_reshape_migration
: {
8562 /* for migration level 0->5 we need to add disks
8563 * so the same as for container operation we will copy
8564 * device to the bigger location.
8565 * in memory prepared device and new disk area are prepared
8566 * for usage in process update
8568 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
8569 struct intel_dev
*id
;
8570 void **space_tail
= (void **)&update
->space_list
;
8573 int current_level
= -1;
8575 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
8577 /* add space for bigger array in update
8579 for (id
= super
->devlist
; id
; id
= id
->next
) {
8580 if (id
->index
== (unsigned)u
->subdev
) {
8581 size
= sizeof_imsm_dev(id
->dev
, 1);
8582 if (u
->new_raid_disks
> u
->old_raid_disks
)
8583 size
+= sizeof(__u32
)*2*
8584 (u
->new_raid_disks
- u
->old_raid_disks
);
8594 if (update
->space_list
== NULL
)
8597 /* add space for disk in update
8599 size
= sizeof(struct dl
);
8602 free(update
->space_list
);
8603 update
->space_list
= NULL
;
8610 /* add spare device to update
8612 for (id
= super
->devlist
; id
; id
= id
->next
)
8613 if (id
->index
== (unsigned)u
->subdev
) {
8614 struct imsm_dev
*dev
;
8615 struct imsm_map
*map
;
8617 dev
= get_imsm_dev(super
, u
->subdev
);
8618 map
= get_imsm_map(dev
, MAP_0
);
8619 current_level
= map
->raid_level
;
8622 if ((u
->new_level
== 5) && (u
->new_level
!= current_level
)) {
8623 struct mdinfo
*spares
;
8625 spares
= get_spares_for_grow(st
);
8633 makedev(dev
->disk
.major
,
8635 dl
= get_disk_super(super
,
8638 dl
->index
= u
->old_raid_disks
;
8644 len
= disks_to_mpb_size(u
->new_raid_disks
);
8645 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
8648 case update_create_array
: {
8649 struct imsm_update_create_array
*u
= (void *) update
->buf
;
8650 struct intel_dev
*dv
;
8651 struct imsm_dev
*dev
= &u
->dev
;
8652 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8654 struct disk_info
*inf
;
8658 inf
= get_disk_info(u
);
8659 len
= sizeof_imsm_dev(dev
, 1);
8660 /* allocate a new super->devlist entry */
8661 dv
= malloc(sizeof(*dv
));
8663 dv
->dev
= malloc(len
);
8668 update
->space
= NULL
;
8672 /* count how many spares will be converted to members */
8673 for (i
= 0; i
< map
->num_members
; i
++) {
8674 dl
= serial_to_dl(inf
[i
].serial
, super
);
8676 /* hmm maybe it failed?, nothing we can do about
8681 if (count_memberships(dl
, super
) == 0)
8684 len
+= activate
* sizeof(struct imsm_disk
);
8691 /* check if we need a larger metadata buffer */
8692 if (super
->next_buf
)
8693 buf_len
= super
->next_len
;
8695 buf_len
= super
->len
;
8697 if (__le32_to_cpu(mpb
->mpb_size
) + len
> buf_len
) {
8698 /* ok we need a larger buf than what is currently allocated
8699 * if this allocation fails process_update will notice that
8700 * ->next_len is set and ->next_buf is NULL
8702 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + len
, 512);
8703 if (super
->next_buf
)
8704 free(super
->next_buf
);
8706 super
->next_len
= buf_len
;
8707 if (posix_memalign(&super
->next_buf
, 512, buf_len
) == 0)
8708 memset(super
->next_buf
, 0, buf_len
);
8710 super
->next_buf
= NULL
;
8714 /* must be called while manager is quiesced */
8715 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
8717 struct imsm_super
*mpb
= super
->anchor
;
8719 struct imsm_dev
*dev
;
8720 struct imsm_map
*map
;
8721 int i
, j
, num_members
;
8724 dprintf("%s: deleting device[%d] from imsm_super\n",
8727 /* shift all indexes down one */
8728 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
8729 if (iter
->index
> (int)index
)
8731 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
8732 if (iter
->index
> (int)index
)
8735 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8736 dev
= get_imsm_dev(super
, i
);
8737 map
= get_imsm_map(dev
, MAP_0
);
8738 num_members
= map
->num_members
;
8739 for (j
= 0; j
< num_members
; j
++) {
8740 /* update ord entries being careful not to propagate
8741 * ord-flags to the first map
8743 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_X
);
8745 if (ord_to_idx(ord
) <= index
)
8748 map
= get_imsm_map(dev
, MAP_0
);
8749 set_imsm_ord_tbl_ent(map
, j
, ord_to_idx(ord
- 1));
8750 map
= get_imsm_map(dev
, MAP_1
);
8752 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
8757 super
->updates_pending
++;
8759 struct dl
*dl
= *dlp
;
8761 *dlp
= (*dlp
)->next
;
8762 __free_imsm_disk(dl
);
8765 #endif /* MDASSEMBLE */
8767 static void close_targets(int *targets
, int new_disks
)
8774 for (i
= 0; i
< new_disks
; i
++) {
8775 if (targets
[i
] >= 0) {
8782 static int imsm_get_allowed_degradation(int level
, int raid_disks
,
8783 struct intel_super
*super
,
8784 struct imsm_dev
*dev
)
8790 struct imsm_map
*map
;
8793 ret_val
= raid_disks
/2;
8794 /* check map if all disks pairs not failed
8797 map
= get_imsm_map(dev
, MAP_0
);
8798 for (i
= 0; i
< ret_val
; i
++) {
8799 int degradation
= 0;
8800 if (get_imsm_disk(super
, i
) == NULL
)
8802 if (get_imsm_disk(super
, i
+ 1) == NULL
)
8804 if (degradation
== 2)
8807 map
= get_imsm_map(dev
, MAP_1
);
8808 /* if there is no second map
8809 * result can be returned
8813 /* check degradation in second map
8815 for (i
= 0; i
< ret_val
; i
++) {
8816 int degradation
= 0;
8817 if (get_imsm_disk(super
, i
) == NULL
)
8819 if (get_imsm_disk(super
, i
+ 1) == NULL
)
8821 if (degradation
== 2)
8836 /*******************************************************************************
8837 * Function: open_backup_targets
8838 * Description: Function opens file descriptors for all devices given in
8841 * info : general array info
8842 * raid_disks : number of disks
8843 * raid_fds : table of device's file descriptors
8844 * super : intel super for raid10 degradation check
8845 * dev : intel device for raid10 degradation check
8849 ******************************************************************************/
8850 int open_backup_targets(struct mdinfo
*info
, int raid_disks
, int *raid_fds
,
8851 struct intel_super
*super
, struct imsm_dev
*dev
)
8857 for (i
= 0; i
< raid_disks
; i
++)
8860 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
8863 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
)) {
8864 dprintf("disk is faulty!!\n");
8868 if ((sd
->disk
.raid_disk
>= raid_disks
) ||
8869 (sd
->disk
.raid_disk
< 0))
8872 dn
= map_dev(sd
->disk
.major
,
8874 raid_fds
[sd
->disk
.raid_disk
] = dev_open(dn
, O_RDWR
);
8875 if (raid_fds
[sd
->disk
.raid_disk
] < 0) {
8876 fprintf(stderr
, "cannot open component\n");
8881 /* check if maximum array degradation level is not exceeded
8883 if ((raid_disks
- opened
) >
8884 imsm_get_allowed_degradation(info
->new_level
,
8887 fprintf(stderr
, "Not enough disks can be opened.\n");
8888 close_targets(raid_fds
, raid_disks
);
8895 /*******************************************************************************
8896 * Function: init_migr_record_imsm
8897 * Description: Function inits imsm migration record
8899 * super : imsm internal array info
8900 * dev : device under migration
8901 * info : general array info to find the smallest device
8904 ******************************************************************************/
8905 void init_migr_record_imsm(struct supertype
*st
, struct imsm_dev
*dev
,
8906 struct mdinfo
*info
)
8908 struct intel_super
*super
= st
->sb
;
8909 struct migr_record
*migr_rec
= super
->migr_rec
;
8911 unsigned long long dsize
, dev_sectors
;
8912 long long unsigned min_dev_sectors
= -1LLU;
8916 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
8917 struct imsm_map
*map_src
= get_imsm_map(dev
, MAP_1
);
8918 unsigned long long num_migr_units
;
8919 unsigned long long array_blocks
;
8921 memset(migr_rec
, 0, sizeof(struct migr_record
));
8922 migr_rec
->family_num
= __cpu_to_le32(super
->anchor
->family_num
);
8924 /* only ascending reshape supported now */
8925 migr_rec
->ascending_migr
= __cpu_to_le32(1);
8927 migr_rec
->dest_depth_per_unit
= GEN_MIGR_AREA_SIZE
/
8928 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
8929 migr_rec
->dest_depth_per_unit
*=
8930 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
8931 new_data_disks
= imsm_num_data_members(dev
, MAP_0
);
8932 migr_rec
->blocks_per_unit
=
8933 __cpu_to_le32(migr_rec
->dest_depth_per_unit
* new_data_disks
);
8934 migr_rec
->dest_depth_per_unit
=
8935 __cpu_to_le32(migr_rec
->dest_depth_per_unit
);
8936 array_blocks
= info
->component_size
* new_data_disks
;
8938 array_blocks
/ __le32_to_cpu(migr_rec
->blocks_per_unit
);
8940 if (array_blocks
% __le32_to_cpu(migr_rec
->blocks_per_unit
))
8942 migr_rec
->num_migr_units
= __cpu_to_le32(num_migr_units
);
8944 migr_rec
->post_migr_vol_cap
= dev
->size_low
;
8945 migr_rec
->post_migr_vol_cap_hi
= dev
->size_high
;
8948 /* Find the smallest dev */
8949 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
8950 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
8951 fd
= dev_open(nm
, O_RDONLY
);
8954 get_dev_size(fd
, NULL
, &dsize
);
8955 dev_sectors
= dsize
/ 512;
8956 if (dev_sectors
< min_dev_sectors
)
8957 min_dev_sectors
= dev_sectors
;
8960 migr_rec
->ckpt_area_pba
= __cpu_to_le32(min_dev_sectors
-
8961 RAID_DISK_RESERVED_BLOCKS_IMSM_HI
);
8963 write_imsm_migr_rec(st
);
8968 /*******************************************************************************
8969 * Function: save_backup_imsm
8970 * Description: Function saves critical data stripes to Migration Copy Area
8971 * and updates the current migration unit status.
8972 * Use restore_stripes() to form a destination stripe,
8973 * and to write it to the Copy Area.
8975 * st : supertype information
8976 * dev : imsm device that backup is saved for
8977 * info : general array info
8978 * buf : input buffer
8979 * length : length of data to backup (blocks_per_unit)
8983 ******************************************************************************/
8984 int save_backup_imsm(struct supertype
*st
,
8985 struct imsm_dev
*dev
,
8986 struct mdinfo
*info
,
8991 struct intel_super
*super
= st
->sb
;
8992 unsigned long long *target_offsets
= NULL
;
8993 int *targets
= NULL
;
8995 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
8996 int new_disks
= map_dest
->num_members
;
8997 int dest_layout
= 0;
8999 unsigned long long start
;
9000 int data_disks
= imsm_num_data_members(dev
, MAP_0
);
9002 targets
= malloc(new_disks
* sizeof(int));
9006 for (i
= 0; i
< new_disks
; i
++)
9009 target_offsets
= malloc(new_disks
* sizeof(unsigned long long));
9010 if (!target_offsets
)
9013 start
= info
->reshape_progress
* 512;
9014 for (i
= 0; i
< new_disks
; i
++) {
9015 target_offsets
[i
] = (unsigned long long)
9016 __le32_to_cpu(super
->migr_rec
->ckpt_area_pba
) * 512;
9017 /* move back copy area adderss, it will be moved forward
9018 * in restore_stripes() using start input variable
9020 target_offsets
[i
] -= start
/data_disks
;
9023 if (open_backup_targets(info
, new_disks
, targets
,
9027 dest_layout
= imsm_level_to_layout(map_dest
->raid_level
);
9028 dest_chunk
= __le16_to_cpu(map_dest
->blocks_per_strip
) * 512;
9030 if (restore_stripes(targets
, /* list of dest devices */
9031 target_offsets
, /* migration record offsets */
9034 map_dest
->raid_level
,
9036 -1, /* source backup file descriptor */
9037 0, /* input buf offset
9038 * always 0 buf is already offseted */
9042 fprintf(stderr
, Name
": Error restoring stripes\n");
9050 close_targets(targets
, new_disks
);
9053 free(target_offsets
);
9058 /*******************************************************************************
9059 * Function: save_checkpoint_imsm
9060 * Description: Function called for current unit status update
9061 * in the migration record. It writes it to disk.
9063 * super : imsm internal array info
9064 * info : general array info
9068 * 2: failure, means no valid migration record
9069 * / no general migration in progress /
9070 ******************************************************************************/
9071 int save_checkpoint_imsm(struct supertype
*st
, struct mdinfo
*info
, int state
)
9073 struct intel_super
*super
= st
->sb
;
9074 unsigned long long blocks_per_unit
;
9075 unsigned long long curr_migr_unit
;
9077 if (load_imsm_migr_rec(super
, info
) != 0) {
9078 dprintf("imsm: ERROR: Cannot read migration record "
9079 "for checkpoint save.\n");
9083 blocks_per_unit
= __le32_to_cpu(super
->migr_rec
->blocks_per_unit
);
9084 if (blocks_per_unit
== 0) {
9085 dprintf("imsm: no migration in progress.\n");
9088 curr_migr_unit
= info
->reshape_progress
/ blocks_per_unit
;
9089 /* check if array is alligned to copy area
9090 * if it is not alligned, add one to current migration unit value
9091 * this can happend on array reshape finish only
9093 if (info
->reshape_progress
% blocks_per_unit
)
9096 super
->migr_rec
->curr_migr_unit
=
9097 __cpu_to_le32(curr_migr_unit
);
9098 super
->migr_rec
->rec_status
= __cpu_to_le32(state
);
9099 super
->migr_rec
->dest_1st_member_lba
=
9100 __cpu_to_le32(curr_migr_unit
*
9101 __le32_to_cpu(super
->migr_rec
->dest_depth_per_unit
));
9102 if (write_imsm_migr_rec(st
) < 0) {
9103 dprintf("imsm: Cannot write migration record "
9104 "outside backup area\n");
9111 /*******************************************************************************
9112 * Function: recover_backup_imsm
9113 * Description: Function recovers critical data from the Migration Copy Area
9114 * while assembling an array.
9116 * super : imsm internal array info
9117 * info : general array info
9119 * 0 : success (or there is no data to recover)
9121 ******************************************************************************/
9122 int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
)
9124 struct intel_super
*super
= st
->sb
;
9125 struct migr_record
*migr_rec
= super
->migr_rec
;
9126 struct imsm_map
*map_dest
= NULL
;
9127 struct intel_dev
*id
= NULL
;
9128 unsigned long long read_offset
;
9129 unsigned long long write_offset
;
9131 int *targets
= NULL
;
9132 int new_disks
, i
, err
;
9135 unsigned long curr_migr_unit
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
9136 unsigned long num_migr_units
= __le32_to_cpu(migr_rec
->num_migr_units
);
9138 int skipped_disks
= 0;
9140 err
= sysfs_get_str(info
, NULL
, "array_state", (char *)buffer
, 20);
9144 /* recover data only during assemblation */
9145 if (strncmp(buffer
, "inactive", 8) != 0)
9147 /* no data to recover */
9148 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
9150 if (curr_migr_unit
>= num_migr_units
)
9153 /* find device during reshape */
9154 for (id
= super
->devlist
; id
; id
= id
->next
)
9155 if (is_gen_migration(id
->dev
))
9160 map_dest
= get_imsm_map(id
->dev
, MAP_0
);
9161 new_disks
= map_dest
->num_members
;
9163 read_offset
= (unsigned long long)
9164 __le32_to_cpu(migr_rec
->ckpt_area_pba
) * 512;
9166 write_offset
= ((unsigned long long)
9167 __le32_to_cpu(migr_rec
->dest_1st_member_lba
) +
9168 __le32_to_cpu(map_dest
->pba_of_lba0
)) * 512;
9170 unit_len
= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
9171 if (posix_memalign((void **)&buf
, 512, unit_len
) != 0)
9173 targets
= malloc(new_disks
* sizeof(int));
9177 if (open_backup_targets(info
, new_disks
, targets
, super
, id
->dev
)) {
9179 Name
": Cannot open some devices belonging to array.\n");
9183 for (i
= 0; i
< new_disks
; i
++) {
9184 if (targets
[i
] < 0) {
9188 if (lseek64(targets
[i
], read_offset
, SEEK_SET
) < 0) {
9190 Name
": Cannot seek to block: %s\n",
9195 if ((unsigned)read(targets
[i
], buf
, unit_len
) != unit_len
) {
9197 Name
": Cannot read copy area block: %s\n",
9202 if (lseek64(targets
[i
], write_offset
, SEEK_SET
) < 0) {
9204 Name
": Cannot seek to block: %s\n",
9209 if ((unsigned)write(targets
[i
], buf
, unit_len
) != unit_len
) {
9211 Name
": Cannot restore block: %s\n",
9218 if (skipped_disks
> imsm_get_allowed_degradation(info
->new_level
,
9223 Name
": Cannot restore data from backup."
9224 " Too many failed disks\n");
9228 if (save_checkpoint_imsm(st
, info
, UNIT_SRC_NORMAL
)) {
9229 /* ignore error == 2, this can mean end of reshape here
9231 dprintf("imsm: Cannot write checkpoint to "
9232 "migration record (UNIT_SRC_NORMAL) during restart\n");
9238 for (i
= 0; i
< new_disks
; i
++)
9247 static char disk_by_path
[] = "/dev/disk/by-path/";
9249 static const char *imsm_get_disk_controller_domain(const char *path
)
9251 char disk_path
[PATH_MAX
];
9255 strncpy(disk_path
, disk_by_path
, PATH_MAX
- 1);
9256 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
9257 if (stat(disk_path
, &st
) == 0) {
9258 struct sys_dev
* hba
;
9261 path
= devt_to_devpath(st
.st_rdev
);
9264 hba
= find_disk_attached_hba(-1, path
);
9265 if (hba
&& hba
->type
== SYS_DEV_SAS
)
9267 else if (hba
&& hba
->type
== SYS_DEV_SATA
)
9271 dprintf("path: %s hba: %s attached: %s\n",
9272 path
, (hba
) ? hba
->path
: "NULL", drv
);
9280 static int imsm_find_array_minor_by_subdev(int subdev
, int container
, int *minor
)
9282 char subdev_name
[20];
9283 struct mdstat_ent
*mdstat
;
9285 sprintf(subdev_name
, "%d", subdev
);
9286 mdstat
= mdstat_by_subdev(subdev_name
, container
);
9290 *minor
= mdstat
->devnum
;
9291 free_mdstat(mdstat
);
9295 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
9296 struct geo_params
*geo
,
9297 int *old_raid_disks
)
9299 /* currently we only support increasing the number of devices
9300 * for a container. This increases the number of device for each
9301 * member array. They must all be RAID0 or RAID5.
9304 struct mdinfo
*info
, *member
;
9305 int devices_that_can_grow
= 0;
9307 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): "
9308 "st->devnum = (%i)\n",
9311 if (geo
->size
!= -1 ||
9312 geo
->level
!= UnSet
||
9313 geo
->layout
!= UnSet
||
9314 geo
->chunksize
!= 0 ||
9315 geo
->raid_disks
== UnSet
) {
9316 dprintf("imsm: Container operation is allowed for "
9317 "raid disks number change only.\n");
9321 info
= container_content_imsm(st
, NULL
);
9322 for (member
= info
; member
; member
= member
->next
) {
9326 dprintf("imsm: checking device_num: %i\n",
9327 member
->container_member
);
9329 if (geo
->raid_disks
<= member
->array
.raid_disks
) {
9330 /* we work on container for Online Capacity Expansion
9331 * only so raid_disks has to grow
9333 dprintf("imsm: for container operation raid disks "
9334 "increase is required\n");
9338 if ((info
->array
.level
!= 0) &&
9339 (info
->array
.level
!= 5)) {
9340 /* we cannot use this container with other raid level
9342 dprintf("imsm: for container operation wrong"
9343 " raid level (%i) detected\n",
9347 /* check for platform support
9348 * for this raid level configuration
9350 struct intel_super
*super
= st
->sb
;
9351 if (!is_raid_level_supported(super
->orom
,
9352 member
->array
.level
,
9354 dprintf("platform does not support raid%d with"
9358 geo
->raid_disks
> 1 ? "s" : "");
9361 /* check if component size is aligned to chunk size
9363 if (info
->component_size
%
9364 (info
->array
.chunk_size
/512)) {
9365 dprintf("Component size is not aligned to "
9371 if (*old_raid_disks
&&
9372 info
->array
.raid_disks
!= *old_raid_disks
)
9374 *old_raid_disks
= info
->array
.raid_disks
;
9376 /* All raid5 and raid0 volumes in container
9377 * have to be ready for Online Capacity Expansion
9378 * so they need to be assembled. We have already
9379 * checked that no recovery etc is happening.
9381 result
= imsm_find_array_minor_by_subdev(member
->container_member
,
9385 dprintf("imsm: cannot find array\n");
9388 devices_that_can_grow
++;
9391 if (!member
&& devices_that_can_grow
)
9395 dprintf("\tContainer operation allowed\n");
9397 dprintf("\tError: %i\n", ret_val
);
9402 /* Function: get_spares_for_grow
9403 * Description: Allocates memory and creates list of spare devices
9404 * avaliable in container. Checks if spare drive size is acceptable.
9405 * Parameters: Pointer to the supertype structure
9406 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
9409 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
9411 unsigned long long min_size
= min_acceptable_spare_size_imsm(st
);
9412 return container_choose_spares(st
, min_size
, NULL
, NULL
, NULL
, 0);
9415 /******************************************************************************
9416 * function: imsm_create_metadata_update_for_reshape
9417 * Function creates update for whole IMSM container.
9419 ******************************************************************************/
9420 static int imsm_create_metadata_update_for_reshape(
9421 struct supertype
*st
,
9422 struct geo_params
*geo
,
9424 struct imsm_update_reshape
**updatep
)
9426 struct intel_super
*super
= st
->sb
;
9427 struct imsm_super
*mpb
= super
->anchor
;
9428 int update_memory_size
= 0;
9429 struct imsm_update_reshape
*u
= NULL
;
9430 struct mdinfo
*spares
= NULL
;
9432 int delta_disks
= 0;
9435 dprintf("imsm_update_metadata_for_reshape(enter) raid_disks = %i\n",
9438 delta_disks
= geo
->raid_disks
- old_raid_disks
;
9440 /* size of all update data without anchor */
9441 update_memory_size
= sizeof(struct imsm_update_reshape
);
9443 /* now add space for spare disks that we need to add. */
9444 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
9446 u
= calloc(1, update_memory_size
);
9449 "cannot get memory for imsm_update_reshape update\n");
9452 u
->type
= update_reshape_container_disks
;
9453 u
->old_raid_disks
= old_raid_disks
;
9454 u
->new_raid_disks
= geo
->raid_disks
;
9456 /* now get spare disks list
9458 spares
= get_spares_for_grow(st
);
9461 || delta_disks
> spares
->array
.spare_disks
) {
9462 fprintf(stderr
, Name
": imsm: ERROR: Cannot get spare devices "
9463 "for %s.\n", geo
->dev_name
);
9468 /* we have got spares
9469 * update disk list in imsm_disk list table in anchor
9471 dprintf("imsm: %i spares are available.\n\n",
9472 spares
->array
.spare_disks
);
9475 for (i
= 0; i
< delta_disks
; i
++) {
9480 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
9482 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
9483 dl
->index
= mpb
->num_disks
;
9493 dprintf("imsm: reshape update preparation :");
9494 if (i
== delta_disks
) {
9497 return update_memory_size
;
9500 dprintf(" Error\n");
9505 /******************************************************************************
9506 * function: imsm_create_metadata_update_for_migration()
9507 * Creates update for IMSM array.
9509 ******************************************************************************/
9510 static int imsm_create_metadata_update_for_migration(
9511 struct supertype
*st
,
9512 struct geo_params
*geo
,
9513 struct imsm_update_reshape_migration
**updatep
)
9515 struct intel_super
*super
= st
->sb
;
9516 int update_memory_size
= 0;
9517 struct imsm_update_reshape_migration
*u
= NULL
;
9518 struct imsm_dev
*dev
;
9519 int previous_level
= -1;
9521 dprintf("imsm_create_metadata_update_for_migration(enter)"
9522 " New Level = %i\n", geo
->level
);
9524 /* size of all update data without anchor */
9525 update_memory_size
= sizeof(struct imsm_update_reshape_migration
);
9527 u
= calloc(1, update_memory_size
);
9529 dprintf("error: cannot get memory for "
9530 "imsm_create_metadata_update_for_migration\n");
9533 u
->type
= update_reshape_migration
;
9534 u
->subdev
= super
->current_vol
;
9535 u
->new_level
= geo
->level
;
9536 u
->new_layout
= geo
->layout
;
9537 u
->new_raid_disks
= u
->old_raid_disks
= geo
->raid_disks
;
9538 u
->new_disks
[0] = -1;
9539 u
->new_chunksize
= -1;
9541 dev
= get_imsm_dev(super
, u
->subdev
);
9543 struct imsm_map
*map
;
9545 map
= get_imsm_map(dev
, MAP_0
);
9547 int current_chunk_size
=
9548 __le16_to_cpu(map
->blocks_per_strip
) / 2;
9550 if (geo
->chunksize
!= current_chunk_size
) {
9551 u
->new_chunksize
= geo
->chunksize
/ 1024;
9553 "chunk size change from %i to %i\n",
9554 current_chunk_size
, u
->new_chunksize
);
9556 previous_level
= map
->raid_level
;
9559 if ((geo
->level
== 5) && (previous_level
== 0)) {
9560 struct mdinfo
*spares
= NULL
;
9562 u
->new_raid_disks
++;
9563 spares
= get_spares_for_grow(st
);
9564 if ((spares
== NULL
) || (spares
->array
.spare_disks
< 1)) {
9567 update_memory_size
= 0;
9568 dprintf("error: cannot get spare device "
9569 "for requested migration");
9574 dprintf("imsm: reshape update preparation : OK\n");
9577 return update_memory_size
;
9580 static void imsm_update_metadata_locally(struct supertype
*st
,
9583 struct metadata_update mu
;
9588 mu
.space_list
= NULL
;
9590 imsm_prepare_update(st
, &mu
);
9591 imsm_process_update(st
, &mu
);
9593 while (mu
.space_list
) {
9594 void **space
= mu
.space_list
;
9595 mu
.space_list
= *space
;
9600 /***************************************************************************
9601 * Function: imsm_analyze_change
9602 * Description: Function analyze change for single volume
9603 * and validate if transition is supported
9604 * Parameters: Geometry parameters, supertype structure
9605 * Returns: Operation type code on success, -1 if fail
9606 ****************************************************************************/
9607 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
9608 struct geo_params
*geo
)
9614 /* number of added/removed disks in operation result */
9615 int devNumChange
= 0;
9616 /* imsm compatible layout value for array geometry verification */
9617 int imsm_layout
= -1;
9619 getinfo_super_imsm_volume(st
, &info
, NULL
);
9620 if ((geo
->level
!= info
.array
.level
) &&
9621 (geo
->level
>= 0) &&
9622 (geo
->level
!= UnSet
)) {
9623 switch (info
.array
.level
) {
9625 if (geo
->level
== 5) {
9626 change
= CH_MIGRATION
;
9627 if (geo
->layout
!= ALGORITHM_LEFT_ASYMMETRIC
) {
9629 Name
" Error. Requested Layout "
9630 "not supported (left-asymmetric layout "
9631 "is supported only)!\n");
9633 goto analyse_change_exit
;
9635 imsm_layout
= geo
->layout
;
9637 devNumChange
= 1; /* parity disk added */
9638 } else if (geo
->level
== 10) {
9639 change
= CH_TAKEOVER
;
9641 devNumChange
= 2; /* two mirrors added */
9642 imsm_layout
= 0x102; /* imsm supported layout */
9647 if (geo
->level
== 0) {
9648 change
= CH_TAKEOVER
;
9650 devNumChange
= -(geo
->raid_disks
/2);
9651 imsm_layout
= 0; /* imsm raid0 layout */
9657 Name
" Error. Level Migration from %d to %d "
9659 info
.array
.level
, geo
->level
);
9660 goto analyse_change_exit
;
9663 geo
->level
= info
.array
.level
;
9665 if ((geo
->layout
!= info
.array
.layout
)
9666 && ((geo
->layout
!= UnSet
) && (geo
->layout
!= -1))) {
9667 change
= CH_MIGRATION
;
9668 if ((info
.array
.layout
== 0)
9669 && (info
.array
.level
== 5)
9670 && (geo
->layout
== 5)) {
9671 /* reshape 5 -> 4 */
9672 } else if ((info
.array
.layout
== 5)
9673 && (info
.array
.level
== 5)
9674 && (geo
->layout
== 0)) {
9675 /* reshape 4 -> 5 */
9680 Name
" Error. Layout Migration from %d to %d "
9682 info
.array
.layout
, geo
->layout
);
9684 goto analyse_change_exit
;
9687 geo
->layout
= info
.array
.layout
;
9688 if (imsm_layout
== -1)
9689 imsm_layout
= info
.array
.layout
;
9692 if ((geo
->chunksize
> 0) && (geo
->chunksize
!= UnSet
)
9693 && (geo
->chunksize
!= info
.array
.chunk_size
))
9694 change
= CH_MIGRATION
;
9696 geo
->chunksize
= info
.array
.chunk_size
;
9698 chunk
= geo
->chunksize
/ 1024;
9699 if (!validate_geometry_imsm(st
,
9702 geo
->raid_disks
+ devNumChange
,
9709 struct intel_super
*super
= st
->sb
;
9710 struct imsm_super
*mpb
= super
->anchor
;
9712 if (mpb
->num_raid_devs
> 1) {
9714 Name
" Error. Cannot perform operation on %s"
9715 "- for this operation it MUST be single "
9716 "array in container\n",
9722 analyse_change_exit
:
9727 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
9729 struct intel_super
*super
= st
->sb
;
9730 struct imsm_update_takeover
*u
;
9732 u
= malloc(sizeof(struct imsm_update_takeover
));
9736 u
->type
= update_takeover
;
9737 u
->subarray
= super
->current_vol
;
9739 /* 10->0 transition */
9740 if (geo
->level
== 0)
9741 u
->direction
= R10_TO_R0
;
9743 /* 0->10 transition */
9744 if (geo
->level
== 10)
9745 u
->direction
= R0_TO_R10
;
9747 /* update metadata locally */
9748 imsm_update_metadata_locally(st
, u
,
9749 sizeof(struct imsm_update_takeover
));
9750 /* and possibly remotely */
9751 if (st
->update_tail
)
9752 append_metadata_update(st
, u
,
9753 sizeof(struct imsm_update_takeover
));
9760 static int imsm_reshape_super(struct supertype
*st
, long long size
, int level
,
9761 int layout
, int chunksize
, int raid_disks
,
9762 int delta_disks
, char *backup
, char *dev
,
9766 struct geo_params geo
;
9768 dprintf("imsm: reshape_super called.\n");
9770 memset(&geo
, 0, sizeof(struct geo_params
));
9773 geo
.dev_id
= st
->devnum
;
9776 geo
.layout
= layout
;
9777 geo
.chunksize
= chunksize
;
9778 geo
.raid_disks
= raid_disks
;
9779 if (delta_disks
!= UnSet
)
9780 geo
.raid_disks
+= delta_disks
;
9782 dprintf("\tfor level : %i\n", geo
.level
);
9783 dprintf("\tfor raid_disks : %i\n", geo
.raid_disks
);
9785 if (experimental() == 0)
9788 if (st
->container_dev
== st
->devnum
) {
9789 /* On container level we can only increase number of devices. */
9790 dprintf("imsm: info: Container operation\n");
9791 int old_raid_disks
= 0;
9793 if (imsm_reshape_is_allowed_on_container(
9794 st
, &geo
, &old_raid_disks
)) {
9795 struct imsm_update_reshape
*u
= NULL
;
9798 len
= imsm_create_metadata_update_for_reshape(
9799 st
, &geo
, old_raid_disks
, &u
);
9802 dprintf("imsm: Cannot prepare update\n");
9803 goto exit_imsm_reshape_super
;
9807 /* update metadata locally */
9808 imsm_update_metadata_locally(st
, u
, len
);
9809 /* and possibly remotely */
9810 if (st
->update_tail
)
9811 append_metadata_update(st
, u
, len
);
9816 fprintf(stderr
, Name
": (imsm) Operation "
9817 "is not allowed on this container\n");
9820 /* On volume level we support following operations
9821 * - takeover: raid10 -> raid0; raid0 -> raid10
9822 * - chunk size migration
9823 * - migration: raid5 -> raid0; raid0 -> raid5
9825 struct intel_super
*super
= st
->sb
;
9826 struct intel_dev
*dev
= super
->devlist
;
9828 dprintf("imsm: info: Volume operation\n");
9829 /* find requested device */
9831 if (imsm_find_array_minor_by_subdev(
9832 dev
->index
, st
->container_dev
, &devnum
) == 0
9833 && devnum
== geo
.dev_id
)
9838 fprintf(stderr
, Name
" Cannot find %s (%i) subarray\n",
9839 geo
.dev_name
, geo
.dev_id
);
9840 goto exit_imsm_reshape_super
;
9842 super
->current_vol
= dev
->index
;
9843 change
= imsm_analyze_change(st
, &geo
);
9846 ret_val
= imsm_takeover(st
, &geo
);
9848 case CH_MIGRATION
: {
9849 struct imsm_update_reshape_migration
*u
= NULL
;
9851 imsm_create_metadata_update_for_migration(
9855 "Cannot prepare update\n");
9859 /* update metadata locally */
9860 imsm_update_metadata_locally(st
, u
, len
);
9861 /* and possibly remotely */
9862 if (st
->update_tail
)
9863 append_metadata_update(st
, u
, len
);
9873 exit_imsm_reshape_super
:
9874 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
9878 /*******************************************************************************
9879 * Function: wait_for_reshape_imsm
9880 * Description: Function writes new sync_max value and waits until
9881 * reshape process reach new position
9883 * sra : general array info
9884 * ndata : number of disks in new array's layout
9887 * 1 : there is no reshape in progress,
9889 ******************************************************************************/
9890 int wait_for_reshape_imsm(struct mdinfo
*sra
, int ndata
)
9892 int fd
= sysfs_get_fd(sra
, NULL
, "reshape_position");
9893 unsigned long long completed
;
9894 /* to_complete : new sync_max position */
9895 unsigned long long to_complete
= sra
->reshape_progress
;
9896 unsigned long long position_to_set
= to_complete
/ ndata
;
9899 dprintf("imsm: wait_for_reshape_imsm() "
9900 "cannot open reshape_position\n");
9904 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
9905 dprintf("imsm: wait_for_reshape_imsm() "
9906 "cannot read reshape_position (no reshape in progres)\n");
9911 if (completed
> to_complete
) {
9912 dprintf("imsm: wait_for_reshape_imsm() "
9913 "wrong next position to set %llu (%llu)\n",
9914 to_complete
, completed
);
9918 dprintf("Position set: %llu\n", position_to_set
);
9919 if (sysfs_set_num(sra
, NULL
, "sync_max",
9920 position_to_set
) != 0) {
9921 dprintf("imsm: wait_for_reshape_imsm() "
9922 "cannot set reshape position to %llu\n",
9933 select(fd
+1, &rfds
, NULL
, NULL
, NULL
);
9934 if (sysfs_get_str(sra
, NULL
, "sync_action",
9936 strncmp(action
, "reshape", 7) != 0)
9938 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
9939 dprintf("imsm: wait_for_reshape_imsm() "
9940 "cannot read reshape_position (in loop)\n");
9944 } while (completed
< to_complete
);
9950 /*******************************************************************************
9951 * Function: check_degradation_change
9952 * Description: Check that array hasn't become failed.
9954 * info : for sysfs access
9955 * sources : source disks descriptors
9956 * degraded: previous degradation level
9959 ******************************************************************************/
9960 int check_degradation_change(struct mdinfo
*info
,
9964 unsigned long long new_degraded
;
9965 sysfs_get_ll(info
, NULL
, "degraded", &new_degraded
);
9966 if (new_degraded
!= (unsigned long long)degraded
) {
9967 /* check each device to ensure it is still working */
9970 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
9971 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
9973 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
9975 if (sysfs_get_str(info
,
9976 sd
, "state", sbuf
, 20) < 0 ||
9977 strstr(sbuf
, "faulty") ||
9978 strstr(sbuf
, "in_sync") == NULL
) {
9979 /* this device is dead */
9980 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
9981 if (sd
->disk
.raid_disk
>= 0 &&
9982 sources
[sd
->disk
.raid_disk
] >= 0) {
9984 sd
->disk
.raid_disk
]);
9985 sources
[sd
->disk
.raid_disk
] =
9994 return new_degraded
;
9997 /*******************************************************************************
9998 * Function: imsm_manage_reshape
9999 * Description: Function finds array under reshape and it manages reshape
10000 * process. It creates stripes backups (if required) and sets
10003 * afd : Backup handle (nattive) - not used
10004 * sra : general array info
10005 * reshape : reshape parameters - not used
10006 * st : supertype structure
10007 * blocks : size of critical section [blocks]
10008 * fds : table of source device descriptor
10009 * offsets : start of array (offest per devices)
10011 * destfd : table of destination device descriptor
10012 * destoffsets : table of destination offsets (per device)
10014 * 1 : success, reshape is done
10016 ******************************************************************************/
10017 static int imsm_manage_reshape(
10018 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
10019 struct supertype
*st
, unsigned long backup_blocks
,
10020 int *fds
, unsigned long long *offsets
,
10021 int dests
, int *destfd
, unsigned long long *destoffsets
)
10024 struct intel_super
*super
= st
->sb
;
10025 struct intel_dev
*dv
= NULL
;
10026 struct imsm_dev
*dev
= NULL
;
10027 struct imsm_map
*map_src
;
10028 int migr_vol_qan
= 0;
10029 int ndata
, odata
; /* [bytes] */
10030 int chunk
; /* [bytes] */
10031 struct migr_record
*migr_rec
;
10033 unsigned int buf_size
; /* [bytes] */
10034 unsigned long long max_position
; /* array size [bytes] */
10035 unsigned long long next_step
; /* [blocks]/[bytes] */
10036 unsigned long long old_data_stripe_length
;
10037 unsigned long long start_src
; /* [bytes] */
10038 unsigned long long start
; /* [bytes] */
10039 unsigned long long start_buf_shift
; /* [bytes] */
10041 int source_layout
= 0;
10043 if (!fds
|| !offsets
|| !sra
)
10046 /* Find volume during the reshape */
10047 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
10048 if (dv
->dev
->vol
.migr_type
== MIGR_GEN_MIGR
10049 && dv
->dev
->vol
.migr_state
== 1) {
10054 /* Only one volume can migrate at the same time */
10055 if (migr_vol_qan
!= 1) {
10056 fprintf(stderr
, Name
" : %s", migr_vol_qan
?
10057 "Number of migrating volumes greater than 1\n" :
10058 "There is no volume during migrationg\n");
10062 map_src
= get_imsm_map(dev
, MAP_1
);
10063 if (map_src
== NULL
)
10066 ndata
= imsm_num_data_members(dev
, MAP_0
);
10067 odata
= imsm_num_data_members(dev
, MAP_1
);
10069 chunk
= __le16_to_cpu(map_src
->blocks_per_strip
) * 512;
10070 old_data_stripe_length
= odata
* chunk
;
10072 migr_rec
= super
->migr_rec
;
10074 /* initialize migration record for start condition */
10075 if (sra
->reshape_progress
== 0)
10076 init_migr_record_imsm(st
, dev
, sra
);
10078 if (__le32_to_cpu(migr_rec
->rec_status
) != UNIT_SRC_NORMAL
) {
10079 dprintf("imsm: cannot restart migration when data "
10080 "are present in copy area.\n");
10083 /* Save checkpoint to update migration record for current
10084 * reshape position (in md). It can be farther than current
10085 * reshape position in metadata.
10087 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
10088 /* ignore error == 2, this can mean end of reshape here
10090 dprintf("imsm: Cannot write checkpoint to "
10091 "migration record (UNIT_SRC_NORMAL, "
10092 "initial save)\n");
10097 /* size for data */
10098 buf_size
= __le32_to_cpu(migr_rec
->blocks_per_unit
) * 512;
10099 /* extend buffer size for parity disk */
10100 buf_size
+= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
10101 /* add space for stripe aligment */
10102 buf_size
+= old_data_stripe_length
;
10103 if (posix_memalign((void **)&buf
, 4096, buf_size
)) {
10104 dprintf("imsm: Cannot allocate checpoint buffer\n");
10108 max_position
= sra
->component_size
* ndata
;
10109 source_layout
= imsm_level_to_layout(map_src
->raid_level
);
10111 while (__le32_to_cpu(migr_rec
->curr_migr_unit
) <
10112 __le32_to_cpu(migr_rec
->num_migr_units
)) {
10113 /* current reshape position [blocks] */
10114 unsigned long long current_position
=
10115 __le32_to_cpu(migr_rec
->blocks_per_unit
)
10116 * __le32_to_cpu(migr_rec
->curr_migr_unit
);
10117 unsigned long long border
;
10119 /* Check that array hasn't become failed.
10121 degraded
= check_degradation_change(sra
, fds
, degraded
);
10122 if (degraded
> 1) {
10123 dprintf("imsm: Abort reshape due to degradation"
10124 " level (%i)\n", degraded
);
10128 next_step
= __le32_to_cpu(migr_rec
->blocks_per_unit
);
10130 if ((current_position
+ next_step
) > max_position
)
10131 next_step
= max_position
- current_position
;
10133 start
= current_position
* 512;
10135 /* allign reading start to old geometry */
10136 start_buf_shift
= start
% old_data_stripe_length
;
10137 start_src
= start
- start_buf_shift
;
10139 border
= (start_src
/ odata
) - (start
/ ndata
);
10141 if (border
<= __le32_to_cpu(migr_rec
->dest_depth_per_unit
)) {
10142 /* save critical stripes to buf
10143 * start - start address of current unit
10144 * to backup [bytes]
10145 * start_src - start address of current unit
10146 * to backup alligned to source array
10149 unsigned long long next_step_filler
= 0;
10150 unsigned long long copy_length
= next_step
* 512;
10152 /* allign copy area length to stripe in old geometry */
10153 next_step_filler
= ((copy_length
+ start_buf_shift
)
10154 % old_data_stripe_length
);
10155 if (next_step_filler
)
10156 next_step_filler
= (old_data_stripe_length
10157 - next_step_filler
);
10158 dprintf("save_stripes() parameters: start = %llu,"
10159 "\tstart_src = %llu,\tnext_step*512 = %llu,"
10160 "\tstart_in_buf_shift = %llu,"
10161 "\tnext_step_filler = %llu\n",
10162 start
, start_src
, copy_length
,
10163 start_buf_shift
, next_step_filler
);
10165 if (save_stripes(fds
, offsets
, map_src
->num_members
,
10166 chunk
, map_src
->raid_level
,
10167 source_layout
, 0, NULL
, start_src
,
10169 next_step_filler
+ start_buf_shift
,
10171 dprintf("imsm: Cannot save stripes"
10175 /* Convert data to destination format and store it
10176 * in backup general migration area
10178 if (save_backup_imsm(st
, dev
, sra
,
10179 buf
+ start_buf_shift
, copy_length
)) {
10180 dprintf("imsm: Cannot save stripes to "
10181 "target devices\n");
10184 if (save_checkpoint_imsm(st
, sra
,
10185 UNIT_SRC_IN_CP_AREA
)) {
10186 dprintf("imsm: Cannot write checkpoint to "
10187 "migration record (UNIT_SRC_IN_CP_AREA)\n");
10191 /* set next step to use whole border area */
10192 border
/= next_step
;
10194 next_step
*= border
;
10196 /* When data backed up, checkpoint stored,
10197 * kick the kernel to reshape unit of data
10199 next_step
= next_step
+ sra
->reshape_progress
;
10200 /* limit next step to array max position */
10201 if (next_step
> max_position
)
10202 next_step
= max_position
;
10203 sysfs_set_num(sra
, NULL
, "suspend_lo", sra
->reshape_progress
);
10204 sysfs_set_num(sra
, NULL
, "suspend_hi", next_step
);
10205 sra
->reshape_progress
= next_step
;
10207 /* wait until reshape finish */
10208 if (wait_for_reshape_imsm(sra
, ndata
) < 0) {
10209 dprintf("wait_for_reshape_imsm returned error!\n");
10213 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
10214 /* ignore error == 2, this can mean end of reshape here
10216 dprintf("imsm: Cannot write checkpoint to "
10217 "migration record (UNIT_SRC_NORMAL)\n");
10223 /* return '1' if done */
10227 abort_reshape(sra
);
10231 #endif /* MDASSEMBLE */
10233 struct superswitch super_imsm
= {
10235 .examine_super
= examine_super_imsm
,
10236 .brief_examine_super
= brief_examine_super_imsm
,
10237 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
10238 .export_examine_super
= export_examine_super_imsm
,
10239 .detail_super
= detail_super_imsm
,
10240 .brief_detail_super
= brief_detail_super_imsm
,
10241 .write_init_super
= write_init_super_imsm
,
10242 .validate_geometry
= validate_geometry_imsm
,
10243 .add_to_super
= add_to_super_imsm
,
10244 .remove_from_super
= remove_from_super_imsm
,
10245 .detail_platform
= detail_platform_imsm
,
10246 .kill_subarray
= kill_subarray_imsm
,
10247 .update_subarray
= update_subarray_imsm
,
10248 .load_container
= load_container_imsm
,
10249 .default_geometry
= default_geometry_imsm
,
10250 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
10251 .reshape_super
= imsm_reshape_super
,
10252 .manage_reshape
= imsm_manage_reshape
,
10253 .recover_backup
= recover_backup_imsm
,
10255 .match_home
= match_home_imsm
,
10256 .uuid_from_super
= uuid_from_super_imsm
,
10257 .getinfo_super
= getinfo_super_imsm
,
10258 .getinfo_super_disks
= getinfo_super_disks_imsm
,
10259 .update_super
= update_super_imsm
,
10261 .avail_size
= avail_size_imsm
,
10262 .min_acceptable_spare_size
= min_acceptable_spare_size_imsm
,
10264 .compare_super
= compare_super_imsm
,
10266 .load_super
= load_super_imsm
,
10267 .init_super
= init_super_imsm
,
10268 .store_super
= store_super_imsm
,
10269 .free_super
= free_super_imsm
,
10270 .match_metadata_desc
= match_metadata_desc_imsm
,
10271 .container_content
= container_content_imsm
,
10279 .open_new
= imsm_open_new
,
10280 .set_array_state
= imsm_set_array_state
,
10281 .set_disk
= imsm_set_disk
,
10282 .sync_metadata
= imsm_sync_metadata
,
10283 .activate_spare
= imsm_activate_spare
,
10284 .process_update
= imsm_process_update
,
10285 .prepare_update
= imsm_prepare_update
,
10286 #endif /* MDASSEMBLE */