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 size_t len
; /* size of the 'buf' allocation */
357 void *next_buf
; /* for realloc'ing buf from the manager */
359 int updates_pending
; /* count of pending updates for mdmon */
360 int current_vol
; /* index of raid device undergoing creation */
361 __u32 create_offset
; /* common start for 'current_vol' */
362 __u32 random
; /* random data for seeding new family numbers */
363 struct intel_dev
*devlist
;
367 __u8 serial
[MAX_RAID_SERIAL_LEN
];
370 struct imsm_disk disk
;
373 struct extent
*e
; /* for determining freespace @ create */
374 int raiddisk
; /* slot to fill in autolayout */
376 } *disks
, *current_disk
;
377 struct dl
*disk_mgmt_list
; /* list of disks to add/remove while mdmon
379 struct dl
*missing
; /* disks removed while we weren't looking */
380 struct bbm_log
*bbm_log
;
381 struct intel_hba
*hba
; /* device path of the raid controller for this metadata */
382 const struct imsm_orom
*orom
; /* platform firmware support */
383 struct intel_super
*next
; /* (temp) list for disambiguating family_num */
387 struct imsm_disk disk
;
388 #define IMSM_UNKNOWN_OWNER (-1)
390 struct intel_disk
*next
;
394 unsigned long long start
, size
;
397 /* definitions of reshape process types */
398 enum imsm_reshape_type
{
403 /* definition of messages passed to imsm_process_update */
404 enum imsm_update_type
{
405 update_activate_spare
,
409 update_add_remove_disk
,
410 update_reshape_container_disks
,
411 update_reshape_migration
,
413 update_general_migration_checkpoint
,
416 struct imsm_update_activate_spare
{
417 enum imsm_update_type type
;
421 struct imsm_update_activate_spare
*next
;
434 enum takeover_direction
{
438 struct imsm_update_takeover
{
439 enum imsm_update_type type
;
441 enum takeover_direction direction
;
444 struct imsm_update_reshape
{
445 enum imsm_update_type type
;
449 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
452 struct imsm_update_reshape_migration
{
453 enum imsm_update_type type
;
456 /* fields for array migration changes
463 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
466 struct imsm_update_general_migration_checkpoint
{
467 enum imsm_update_type type
;
468 __u32 curr_migr_unit
;
472 __u8 serial
[MAX_RAID_SERIAL_LEN
];
475 struct imsm_update_create_array
{
476 enum imsm_update_type type
;
481 struct imsm_update_kill_array
{
482 enum imsm_update_type type
;
486 struct imsm_update_rename_array
{
487 enum imsm_update_type type
;
488 __u8 name
[MAX_RAID_SERIAL_LEN
];
492 struct imsm_update_add_remove_disk
{
493 enum imsm_update_type type
;
497 static const char *_sys_dev_type
[] = {
498 [SYS_DEV_UNKNOWN
] = "Unknown",
499 [SYS_DEV_SAS
] = "SAS",
500 [SYS_DEV_SATA
] = "SATA"
503 const char *get_sys_dev_type(enum sys_dev_type type
)
505 if (type
>= SYS_DEV_MAX
)
506 type
= SYS_DEV_UNKNOWN
;
508 return _sys_dev_type
[type
];
511 static struct intel_hba
* alloc_intel_hba(struct sys_dev
*device
)
513 struct intel_hba
*result
= malloc(sizeof(*result
));
515 result
->type
= device
->type
;
516 result
->path
= strdup(device
->path
);
518 if (result
->path
&& (result
->pci_id
= strrchr(result
->path
, '/')) != NULL
)
524 static struct intel_hba
* find_intel_hba(struct intel_hba
*hba
, struct sys_dev
*device
)
526 struct intel_hba
*result
=NULL
;
527 for (result
= hba
; result
; result
= result
->next
) {
528 if (result
->type
== device
->type
&& strcmp(result
->path
, device
->path
) == 0)
534 static int attach_hba_to_super(struct intel_super
*super
, struct sys_dev
*device
)
536 struct intel_hba
*hba
;
538 /* check if disk attached to Intel HBA */
539 hba
= find_intel_hba(super
->hba
, device
);
542 /* Check if HBA is already attached to super */
543 if (super
->hba
== NULL
) {
544 super
->hba
= alloc_intel_hba(device
);
549 /* Intel metadata allows for all disks attached to the same type HBA.
550 * Do not sypport odf HBA types mixing
552 if (device
->type
!= hba
->type
)
558 hba
->next
= alloc_intel_hba(device
);
562 static struct sys_dev
* find_disk_attached_hba(int fd
, const char *devname
)
564 struct sys_dev
*list
, *elem
, *prev
;
567 if ((list
= find_intel_devices()) == NULL
)
571 disk_path
= (char *) devname
;
573 disk_path
= diskfd_to_devpath(fd
);
580 for (prev
= NULL
, elem
= list
; elem
; prev
= elem
, elem
= elem
->next
) {
581 if (path_attached_to_hba(disk_path
, elem
->path
)) {
585 prev
->next
= elem
->next
;
587 if (disk_path
!= devname
)
593 if (disk_path
!= devname
)
601 static int find_intel_hba_capability(int fd
, struct intel_super
*super
,
604 static struct supertype
*match_metadata_desc_imsm(char *arg
)
606 struct supertype
*st
;
608 if (strcmp(arg
, "imsm") != 0 &&
609 strcmp(arg
, "default") != 0
613 st
= malloc(sizeof(*st
));
616 memset(st
, 0, sizeof(*st
));
617 st
->container_dev
= NoMdDev
;
618 st
->ss
= &super_imsm
;
619 st
->max_devs
= IMSM_MAX_DEVICES
;
620 st
->minor_version
= 0;
626 static __u8
*get_imsm_version(struct imsm_super
*mpb
)
628 return &mpb
->sig
[MPB_SIG_LEN
];
632 /* retrieve a disk directly from the anchor when the anchor is known to be
633 * up-to-date, currently only at load time
635 static struct imsm_disk
*__get_imsm_disk(struct imsm_super
*mpb
, __u8 index
)
637 if (index
>= mpb
->num_disks
)
639 return &mpb
->disk
[index
];
642 /* retrieve the disk description based on a index of the disk
645 static struct dl
*get_imsm_dl_disk(struct intel_super
*super
, __u8 index
)
649 for (d
= super
->disks
; d
; d
= d
->next
)
650 if (d
->index
== index
)
655 /* retrieve a disk from the parsed metadata */
656 static struct imsm_disk
*get_imsm_disk(struct intel_super
*super
, __u8 index
)
660 dl
= get_imsm_dl_disk(super
, index
);
667 /* generate a checksum directly from the anchor when the anchor is known to be
668 * up-to-date, currently only at load or write_super after coalescing
670 static __u32
__gen_imsm_checksum(struct imsm_super
*mpb
)
672 __u32 end
= mpb
->mpb_size
/ sizeof(end
);
673 __u32
*p
= (__u32
*) mpb
;
677 sum
+= __le32_to_cpu(*p
);
681 return sum
- __le32_to_cpu(mpb
->check_sum
);
684 static size_t sizeof_imsm_map(struct imsm_map
*map
)
686 return sizeof(struct imsm_map
) + sizeof(__u32
) * (map
->num_members
- 1);
689 struct imsm_map
*get_imsm_map(struct imsm_dev
*dev
, int second_map
)
691 /* A device can have 2 maps if it is in the middle of a migration.
693 * MAP_0 - we return the first map
694 * MAP_1 - we return the second map if it exists, else NULL
695 * MAP_X - we return the second map if it exists, else the first
697 struct imsm_map
*map
= &dev
->vol
.map
[0];
698 struct imsm_map
*map2
= NULL
;
700 if (dev
->vol
.migr_state
)
701 map2
= (void *)map
+ sizeof_imsm_map(map
);
703 switch (second_map
) {
720 /* return the size of the device.
721 * migr_state increases the returned size if map[0] were to be duplicated
723 static size_t sizeof_imsm_dev(struct imsm_dev
*dev
, int migr_state
)
725 size_t size
= sizeof(*dev
) - sizeof(struct imsm_map
) +
726 sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
728 /* migrating means an additional map */
729 if (dev
->vol
.migr_state
)
730 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_1
));
732 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
738 /* retrieve disk serial number list from a metadata update */
739 static struct disk_info
*get_disk_info(struct imsm_update_create_array
*update
)
742 struct disk_info
*inf
;
744 inf
= u
+ sizeof(*update
) - sizeof(struct imsm_dev
) +
745 sizeof_imsm_dev(&update
->dev
, 0);
751 static struct imsm_dev
*__get_imsm_dev(struct imsm_super
*mpb
, __u8 index
)
757 if (index
>= mpb
->num_raid_devs
)
760 /* devices start after all disks */
761 offset
= ((void *) &mpb
->disk
[mpb
->num_disks
]) - _mpb
;
763 for (i
= 0; i
<= index
; i
++)
765 return _mpb
+ offset
;
767 offset
+= sizeof_imsm_dev(_mpb
+ offset
, 0);
772 static struct imsm_dev
*get_imsm_dev(struct intel_super
*super
, __u8 index
)
774 struct intel_dev
*dv
;
776 if (index
>= super
->anchor
->num_raid_devs
)
778 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
779 if (dv
->index
== index
)
786 * == MAP_0 get first map
787 * == MAP_1 get second map
788 * == MAP_X than get map according to the current migr_state
790 static __u32
get_imsm_ord_tbl_ent(struct imsm_dev
*dev
,
794 struct imsm_map
*map
;
796 map
= get_imsm_map(dev
, second_map
);
798 /* top byte identifies disk under rebuild */
799 return __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
802 #define ord_to_idx(ord) (((ord) << 8) >> 8)
803 static __u32
get_imsm_disk_idx(struct imsm_dev
*dev
, int slot
, int second_map
)
805 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, second_map
);
807 return ord_to_idx(ord
);
810 static void set_imsm_ord_tbl_ent(struct imsm_map
*map
, int slot
, __u32 ord
)
812 map
->disk_ord_tbl
[slot
] = __cpu_to_le32(ord
);
815 static int get_imsm_disk_slot(struct imsm_map
*map
, unsigned idx
)
820 for (slot
= 0; slot
< map
->num_members
; slot
++) {
821 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
822 if (ord_to_idx(ord
) == idx
)
829 static int get_imsm_raid_level(struct imsm_map
*map
)
831 if (map
->raid_level
== 1) {
832 if (map
->num_members
== 2)
838 return map
->raid_level
;
841 static int cmp_extent(const void *av
, const void *bv
)
843 const struct extent
*a
= av
;
844 const struct extent
*b
= bv
;
845 if (a
->start
< b
->start
)
847 if (a
->start
> b
->start
)
852 static int count_memberships(struct dl
*dl
, struct intel_super
*super
)
857 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
858 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
859 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
861 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
868 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
);
870 static struct extent
*get_extents(struct intel_super
*super
, struct dl
*dl
)
872 /* find a list of used extents on the given physical device */
873 struct extent
*rv
, *e
;
875 int memberships
= count_memberships(dl
, super
);
878 /* trim the reserved area for spares, so they can join any array
879 * regardless of whether the OROM has assigned sectors from the
880 * IMSM_RESERVED_SECTORS region
883 reservation
= imsm_min_reserved_sectors(super
);
885 reservation
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
887 rv
= malloc(sizeof(struct extent
) * (memberships
+ 1));
892 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
893 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
894 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
896 if (get_imsm_disk_slot(map
, dl
->index
) >= 0) {
897 e
->start
= __le32_to_cpu(map
->pba_of_lba0
);
898 e
->size
= __le32_to_cpu(map
->blocks_per_member
);
902 qsort(rv
, memberships
, sizeof(*rv
), cmp_extent
);
904 /* determine the start of the metadata
905 * when no raid devices are defined use the default
906 * ...otherwise allow the metadata to truncate the value
907 * as is the case with older versions of imsm
910 struct extent
*last
= &rv
[memberships
- 1];
913 remainder
= __le32_to_cpu(dl
->disk
.total_blocks
) -
914 (last
->start
+ last
->size
);
915 /* round down to 1k block to satisfy precision of the kernel
919 /* make sure remainder is still sane */
920 if (remainder
< (unsigned)ROUND_UP(super
->len
, 512) >> 9)
921 remainder
= ROUND_UP(super
->len
, 512) >> 9;
922 if (reservation
> remainder
)
923 reservation
= remainder
;
925 e
->start
= __le32_to_cpu(dl
->disk
.total_blocks
) - reservation
;
930 /* try to determine how much space is reserved for metadata from
931 * the last get_extents() entry, otherwise fallback to the
934 static __u32
imsm_reserved_sectors(struct intel_super
*super
, struct dl
*dl
)
940 /* for spares just return a minimal reservation which will grow
941 * once the spare is picked up by an array
944 return MPB_SECTOR_CNT
;
946 e
= get_extents(super
, dl
);
948 return MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
950 /* scroll to last entry */
951 for (i
= 0; e
[i
].size
; i
++)
954 rv
= __le32_to_cpu(dl
->disk
.total_blocks
) - e
[i
].start
;
961 static int is_spare(struct imsm_disk
*disk
)
963 return (disk
->status
& SPARE_DISK
) == SPARE_DISK
;
966 static int is_configured(struct imsm_disk
*disk
)
968 return (disk
->status
& CONFIGURED_DISK
) == CONFIGURED_DISK
;
971 static int is_failed(struct imsm_disk
*disk
)
973 return (disk
->status
& FAILED_DISK
) == FAILED_DISK
;
976 /* try to determine how much space is reserved for metadata from
977 * the last get_extents() entry on the smallest active disk,
978 * otherwise fallback to the default
980 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
)
984 __u32 min_active
, remainder
;
985 __u32 rv
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
986 struct dl
*dl
, *dl_min
= NULL
;
992 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
995 if (dl
->disk
.total_blocks
< min_active
|| min_active
== 0) {
997 min_active
= dl
->disk
.total_blocks
;
1003 /* find last lba used by subarrays on the smallest active disk */
1004 e
= get_extents(super
, dl_min
);
1007 for (i
= 0; e
[i
].size
; i
++)
1010 remainder
= min_active
- e
[i
].start
;
1013 /* to give priority to recovery we should not require full
1014 IMSM_RESERVED_SECTORS from the spare */
1015 rv
= MPB_SECTOR_CNT
+ NUM_BLOCKS_DIRTY_STRIPE_REGION
;
1017 /* if real reservation is smaller use that value */
1018 return (remainder
< rv
) ? remainder
: rv
;
1021 /* Return minimum size of a spare that can be used in this array*/
1022 static unsigned long long min_acceptable_spare_size_imsm(struct supertype
*st
)
1024 struct intel_super
*super
= st
->sb
;
1028 unsigned long long rv
= 0;
1032 /* find first active disk in array */
1034 while (dl
&& (is_failed(&dl
->disk
) || dl
->index
== -1))
1038 /* find last lba used by subarrays */
1039 e
= get_extents(super
, dl
);
1042 for (i
= 0; e
[i
].size
; i
++)
1045 rv
= e
[i
-1].start
+ e
[i
-1].size
;
1048 /* add the amount of space needed for metadata */
1049 rv
= rv
+ imsm_min_reserved_sectors(super
);
1054 static int is_gen_migration(struct imsm_dev
*dev
);
1057 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
1058 struct imsm_dev
*dev
);
1060 static void print_imsm_dev(struct intel_super
*super
,
1061 struct imsm_dev
*dev
,
1067 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1068 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
1072 printf("[%.16s]:\n", dev
->volume
);
1073 printf(" UUID : %s\n", uuid
);
1074 printf(" RAID Level : %d", get_imsm_raid_level(map
));
1076 printf(" <-- %d", get_imsm_raid_level(map2
));
1078 printf(" Members : %d", map
->num_members
);
1080 printf(" <-- %d", map2
->num_members
);
1082 printf(" Slots : [");
1083 for (i
= 0; i
< map
->num_members
; i
++) {
1084 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
1085 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1090 for (i
= 0; i
< map2
->num_members
; i
++) {
1091 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_1
);
1092 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1097 printf(" Failed disk : ");
1098 if (map
->failed_disk_num
== 0xff)
1101 printf("%i", map
->failed_disk_num
);
1103 slot
= get_imsm_disk_slot(map
, disk_idx
);
1105 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
1106 printf(" This Slot : %d%s\n", slot
,
1107 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
1109 printf(" This Slot : ?\n");
1110 sz
= __le32_to_cpu(dev
->size_high
);
1112 sz
+= __le32_to_cpu(dev
->size_low
);
1113 printf(" Array Size : %llu%s\n", (unsigned long long)sz
,
1114 human_size(sz
* 512));
1115 sz
= __le32_to_cpu(map
->blocks_per_member
);
1116 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz
,
1117 human_size(sz
* 512));
1118 printf(" Sector Offset : %u\n",
1119 __le32_to_cpu(map
->pba_of_lba0
));
1120 printf(" Num Stripes : %u\n",
1121 __le32_to_cpu(map
->num_data_stripes
));
1122 printf(" Chunk Size : %u KiB",
1123 __le16_to_cpu(map
->blocks_per_strip
) / 2);
1125 printf(" <-- %u KiB",
1126 __le16_to_cpu(map2
->blocks_per_strip
) / 2);
1128 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
1129 printf(" Migrate State : ");
1130 if (dev
->vol
.migr_state
) {
1131 if (migr_type(dev
) == MIGR_INIT
)
1132 printf("initialize\n");
1133 else if (migr_type(dev
) == MIGR_REBUILD
)
1134 printf("rebuild\n");
1135 else if (migr_type(dev
) == MIGR_VERIFY
)
1137 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
1138 printf("general migration\n");
1139 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
1140 printf("state change\n");
1141 else if (migr_type(dev
) == MIGR_REPAIR
)
1144 printf("<unknown:%d>\n", migr_type(dev
));
1147 printf(" Map State : %s", map_state_str
[map
->map_state
]);
1148 if (dev
->vol
.migr_state
) {
1149 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
1151 printf(" <-- %s", map_state_str
[map
->map_state
]);
1152 printf("\n Checkpoint : %u ",
1153 __le32_to_cpu(dev
->vol
.curr_migr_unit
));
1154 if ((is_gen_migration(dev
)) && ((slot
> 1) || (slot
< 0)))
1157 printf("(%llu)", (unsigned long long)
1158 blocks_per_migr_unit(super
, dev
));
1161 printf(" Dirty State : %s\n", dev
->vol
.dirty
? "dirty" : "clean");
1164 static void print_imsm_disk(struct imsm_disk
*disk
, int index
, __u32 reserved
)
1166 char str
[MAX_RAID_SERIAL_LEN
+ 1];
1169 if (index
< -1 || !disk
)
1173 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
1175 printf(" Disk%02d Serial : %s\n", index
, str
);
1177 printf(" Disk Serial : %s\n", str
);
1178 printf(" State :%s%s%s\n", is_spare(disk
) ? " spare" : "",
1179 is_configured(disk
) ? " active" : "",
1180 is_failed(disk
) ? " failed" : "");
1181 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
1182 sz
= __le32_to_cpu(disk
->total_blocks
) - reserved
;
1183 printf(" Usable Size : %llu%s\n", (unsigned long long)sz
,
1184 human_size(sz
* 512));
1187 void examine_migr_rec_imsm(struct intel_super
*super
)
1189 struct migr_record
*migr_rec
= super
->migr_rec
;
1190 struct imsm_super
*mpb
= super
->anchor
;
1193 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1194 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1195 struct imsm_map
*map
;
1198 if (is_gen_migration(dev
) == 0)
1201 printf("\nMigration Record Information:");
1203 /* first map under migration */
1204 map
= get_imsm_map(dev
, MAP_0
);
1206 slot
= get_imsm_disk_slot(map
, super
->disks
->index
);
1207 if ((map
== NULL
) || (slot
> 1) || (slot
< 0)) {
1208 printf(" Empty\n ");
1209 printf("Examine one of first two disks in array\n");
1212 printf("\n Status : ");
1213 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
1216 printf("Contains Data\n");
1217 printf(" Current Unit : %u\n",
1218 __le32_to_cpu(migr_rec
->curr_migr_unit
));
1219 printf(" Family : %u\n",
1220 __le32_to_cpu(migr_rec
->family_num
));
1221 printf(" Ascending : %u\n",
1222 __le32_to_cpu(migr_rec
->ascending_migr
));
1223 printf(" Blocks Per Unit : %u\n",
1224 __le32_to_cpu(migr_rec
->blocks_per_unit
));
1225 printf(" Dest. Depth Per Unit : %u\n",
1226 __le32_to_cpu(migr_rec
->dest_depth_per_unit
));
1227 printf(" Checkpoint Area pba : %u\n",
1228 __le32_to_cpu(migr_rec
->ckpt_area_pba
));
1229 printf(" First member lba : %u\n",
1230 __le32_to_cpu(migr_rec
->dest_1st_member_lba
));
1231 printf(" Total Number of Units : %u\n",
1232 __le32_to_cpu(migr_rec
->num_migr_units
));
1233 printf(" Size of volume : %u\n",
1234 __le32_to_cpu(migr_rec
->post_migr_vol_cap
));
1235 printf(" Expansion space for LBA64 : %u\n",
1236 __le32_to_cpu(migr_rec
->post_migr_vol_cap_hi
));
1237 printf(" Record was read from : %u\n",
1238 __le32_to_cpu(migr_rec
->ckpt_read_disk_num
));
1243 #endif /* MDASSEMBLE */
1244 /*******************************************************************************
1245 * function: imsm_check_attributes
1246 * Description: Function checks if features represented by attributes flags
1247 * are supported by mdadm.
1249 * attributes - Attributes read from metadata
1251 * 0 - passed attributes contains unsupported features flags
1252 * 1 - all features are supported
1253 ******************************************************************************/
1254 static int imsm_check_attributes(__u32 attributes
)
1257 __u32 not_supported
= MPB_ATTRIB_SUPPORTED
^0xffffffff;
1259 not_supported
&= ~MPB_ATTRIB_IGNORED
;
1261 not_supported
&= attributes
;
1262 if (not_supported
) {
1263 fprintf(stderr
, Name
"(IMSM): Unsupported attributes : %x\n",
1264 (unsigned)__le32_to_cpu(not_supported
));
1265 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1266 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
1267 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1269 if (not_supported
& MPB_ATTRIB_2TB
) {
1270 dprintf("\t\tMPB_ATTRIB_2TB\n");
1271 not_supported
^= MPB_ATTRIB_2TB
;
1273 if (not_supported
& MPB_ATTRIB_RAID0
) {
1274 dprintf("\t\tMPB_ATTRIB_RAID0\n");
1275 not_supported
^= MPB_ATTRIB_RAID0
;
1277 if (not_supported
& MPB_ATTRIB_RAID1
) {
1278 dprintf("\t\tMPB_ATTRIB_RAID1\n");
1279 not_supported
^= MPB_ATTRIB_RAID1
;
1281 if (not_supported
& MPB_ATTRIB_RAID10
) {
1282 dprintf("\t\tMPB_ATTRIB_RAID10\n");
1283 not_supported
^= MPB_ATTRIB_RAID10
;
1285 if (not_supported
& MPB_ATTRIB_RAID1E
) {
1286 dprintf("\t\tMPB_ATTRIB_RAID1E\n");
1287 not_supported
^= MPB_ATTRIB_RAID1E
;
1289 if (not_supported
& MPB_ATTRIB_RAID5
) {
1290 dprintf("\t\tMPB_ATTRIB_RAID5\n");
1291 not_supported
^= MPB_ATTRIB_RAID5
;
1293 if (not_supported
& MPB_ATTRIB_RAIDCNG
) {
1294 dprintf("\t\tMPB_ATTRIB_RAIDCNG\n");
1295 not_supported
^= MPB_ATTRIB_RAIDCNG
;
1297 if (not_supported
& MPB_ATTRIB_BBM
) {
1298 dprintf("\t\tMPB_ATTRIB_BBM\n");
1299 not_supported
^= MPB_ATTRIB_BBM
;
1301 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1302 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n");
1303 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1305 if (not_supported
& MPB_ATTRIB_EXP_STRIPE_SIZE
) {
1306 dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n");
1307 not_supported
^= MPB_ATTRIB_EXP_STRIPE_SIZE
;
1309 if (not_supported
& MPB_ATTRIB_2TB_DISK
) {
1310 dprintf("\t\tMPB_ATTRIB_2TB_DISK\n");
1311 not_supported
^= MPB_ATTRIB_2TB_DISK
;
1313 if (not_supported
& MPB_ATTRIB_NEVER_USE2
) {
1314 dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n");
1315 not_supported
^= MPB_ATTRIB_NEVER_USE2
;
1317 if (not_supported
& MPB_ATTRIB_NEVER_USE
) {
1318 dprintf("\t\tMPB_ATTRIB_NEVER_USE\n");
1319 not_supported
^= MPB_ATTRIB_NEVER_USE
;
1323 dprintf(Name
"(IMSM): Unknown attributes : %x\n", not_supported
);
1332 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
);
1334 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
1336 struct intel_super
*super
= st
->sb
;
1337 struct imsm_super
*mpb
= super
->anchor
;
1338 char str
[MAX_SIGNATURE_LENGTH
];
1343 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
1346 snprintf(str
, MPB_SIG_LEN
, "%s", mpb
->sig
);
1347 printf(" Magic : %s\n", str
);
1348 snprintf(str
, strlen(MPB_VERSION_RAID0
), "%s", get_imsm_version(mpb
));
1349 printf(" Version : %s\n", get_imsm_version(mpb
));
1350 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
1351 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
1352 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
1353 printf(" Attributes : ");
1354 if (imsm_check_attributes(mpb
->attributes
))
1355 printf("All supported\n");
1357 printf("not supported\n");
1358 getinfo_super_imsm(st
, &info
, NULL
);
1359 fname_from_uuid(st
, &info
, nbuf
, ':');
1360 printf(" UUID : %s\n", nbuf
+ 5);
1361 sum
= __le32_to_cpu(mpb
->check_sum
);
1362 printf(" Checksum : %08x %s\n", sum
,
1363 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
1364 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
));
1365 printf(" Disks : %d\n", mpb
->num_disks
);
1366 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
1367 print_imsm_disk(__get_imsm_disk(mpb
, super
->disks
->index
), super
->disks
->index
, reserved
);
1368 if (super
->bbm_log
) {
1369 struct bbm_log
*log
= super
->bbm_log
;
1372 printf("Bad Block Management Log:\n");
1373 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
1374 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
1375 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
1376 printf(" Spare Blocks : %d\n", __le32_to_cpu(log
->reserved_spare_block_count
));
1377 printf(" First Spare : %llx\n",
1378 (unsigned long long) __le64_to_cpu(log
->first_spare_lba
));
1380 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1382 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1384 super
->current_vol
= i
;
1385 getinfo_super_imsm(st
, &info
, NULL
);
1386 fname_from_uuid(st
, &info
, nbuf
, ':');
1387 print_imsm_dev(super
, dev
, nbuf
+ 5, super
->disks
->index
);
1389 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1390 if (i
== super
->disks
->index
)
1392 print_imsm_disk(__get_imsm_disk(mpb
, i
), i
, reserved
);
1395 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
1396 if (dl
->index
== -1)
1397 print_imsm_disk(&dl
->disk
, -1, reserved
);
1399 examine_migr_rec_imsm(super
);
1402 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
1404 /* We just write a generic IMSM ARRAY entry */
1407 struct intel_super
*super
= st
->sb
;
1409 if (!super
->anchor
->num_raid_devs
) {
1410 printf("ARRAY metadata=imsm\n");
1414 getinfo_super_imsm(st
, &info
, NULL
);
1415 fname_from_uuid(st
, &info
, nbuf
, ':');
1416 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
1419 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
1421 /* We just write a generic IMSM ARRAY entry */
1425 struct intel_super
*super
= st
->sb
;
1428 if (!super
->anchor
->num_raid_devs
)
1431 getinfo_super_imsm(st
, &info
, NULL
);
1432 fname_from_uuid(st
, &info
, nbuf
, ':');
1433 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1434 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1436 super
->current_vol
= i
;
1437 getinfo_super_imsm(st
, &info
, NULL
);
1438 fname_from_uuid(st
, &info
, nbuf1
, ':');
1439 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
1440 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
1444 static void export_examine_super_imsm(struct supertype
*st
)
1446 struct intel_super
*super
= st
->sb
;
1447 struct imsm_super
*mpb
= super
->anchor
;
1451 getinfo_super_imsm(st
, &info
, NULL
);
1452 fname_from_uuid(st
, &info
, nbuf
, ':');
1453 printf("MD_METADATA=imsm\n");
1454 printf("MD_LEVEL=container\n");
1455 printf("MD_UUID=%s\n", nbuf
+5);
1456 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
1459 static void detail_super_imsm(struct supertype
*st
, char *homehost
)
1464 getinfo_super_imsm(st
, &info
, NULL
);
1465 fname_from_uuid(st
, &info
, nbuf
, ':');
1466 printf("\n UUID : %s\n", nbuf
+ 5);
1469 static void brief_detail_super_imsm(struct supertype
*st
)
1473 getinfo_super_imsm(st
, &info
, NULL
);
1474 fname_from_uuid(st
, &info
, nbuf
, ':');
1475 printf(" UUID=%s", nbuf
+ 5);
1478 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
);
1479 static void fd2devname(int fd
, char *name
);
1481 static int ahci_enumerate_ports(const char *hba_path
, int port_count
, int host_base
, int verbose
)
1483 /* dump an unsorted list of devices attached to AHCI Intel storage
1484 * controller, as well as non-connected ports
1486 int hba_len
= strlen(hba_path
) + 1;
1491 unsigned long port_mask
= (1 << port_count
) - 1;
1493 if (port_count
> (int)sizeof(port_mask
) * 8) {
1495 fprintf(stderr
, Name
": port_count %d out of range\n", port_count
);
1499 /* scroll through /sys/dev/block looking for devices attached to
1502 dir
= opendir("/sys/dev/block");
1503 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
1514 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
1516 path
= devt_to_devpath(makedev(major
, minor
));
1519 if (!path_attached_to_hba(path
, hba_path
)) {
1525 /* retrieve the scsi device type */
1526 if (asprintf(&device
, "/sys/dev/block/%d:%d/device/xxxxxxx", major
, minor
) < 0) {
1528 fprintf(stderr
, Name
": failed to allocate 'device'\n");
1532 sprintf(device
, "/sys/dev/block/%d:%d/device/type", major
, minor
);
1533 if (load_sys(device
, buf
) != 0) {
1535 fprintf(stderr
, Name
": failed to read device type for %s\n",
1541 type
= strtoul(buf
, NULL
, 10);
1543 /* if it's not a disk print the vendor and model */
1544 if (!(type
== 0 || type
== 7 || type
== 14)) {
1547 sprintf(device
, "/sys/dev/block/%d:%d/device/vendor", major
, minor
);
1548 if (load_sys(device
, buf
) == 0) {
1549 strncpy(vendor
, buf
, sizeof(vendor
));
1550 vendor
[sizeof(vendor
) - 1] = '\0';
1551 c
= (char *) &vendor
[sizeof(vendor
) - 1];
1552 while (isspace(*c
) || *c
== '\0')
1556 sprintf(device
, "/sys/dev/block/%d:%d/device/model", major
, minor
);
1557 if (load_sys(device
, buf
) == 0) {
1558 strncpy(model
, buf
, sizeof(model
));
1559 model
[sizeof(model
) - 1] = '\0';
1560 c
= (char *) &model
[sizeof(model
) - 1];
1561 while (isspace(*c
) || *c
== '\0')
1565 if (vendor
[0] && model
[0])
1566 sprintf(buf
, "%.64s %.64s", vendor
, model
);
1568 switch (type
) { /* numbers from hald/linux/device.c */
1569 case 1: sprintf(buf
, "tape"); break;
1570 case 2: sprintf(buf
, "printer"); break;
1571 case 3: sprintf(buf
, "processor"); break;
1573 case 5: sprintf(buf
, "cdrom"); break;
1574 case 6: sprintf(buf
, "scanner"); break;
1575 case 8: sprintf(buf
, "media_changer"); break;
1576 case 9: sprintf(buf
, "comm"); break;
1577 case 12: sprintf(buf
, "raid"); break;
1578 default: sprintf(buf
, "unknown");
1584 /* chop device path to 'host%d' and calculate the port number */
1585 c
= strchr(&path
[hba_len
], '/');
1588 fprintf(stderr
, Name
": %s - invalid path name\n", path
+ hba_len
);
1593 if (sscanf(&path
[hba_len
], "host%d", &port
) == 1)
1597 *c
= '/'; /* repair the full string */
1598 fprintf(stderr
, Name
": failed to determine port number for %s\n",
1605 /* mark this port as used */
1606 port_mask
&= ~(1 << port
);
1608 /* print out the device information */
1610 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
1614 fd
= dev_open(ent
->d_name
, O_RDONLY
);
1616 printf(" Port%d : - disk info unavailable -\n", port
);
1618 fd2devname(fd
, buf
);
1619 printf(" Port%d : %s", port
, buf
);
1620 if (imsm_read_serial(fd
, NULL
, (__u8
*) buf
) == 0)
1621 printf(" (%.*s)\n", MAX_RAID_SERIAL_LEN
, buf
);
1636 for (i
= 0; i
< port_count
; i
++)
1637 if (port_mask
& (1 << i
))
1638 printf(" Port%d : - no device attached -\n", i
);
1644 static void print_found_intel_controllers(struct sys_dev
*elem
)
1646 for (; elem
; elem
= elem
->next
) {
1647 fprintf(stderr
, Name
": found Intel(R) ");
1648 if (elem
->type
== SYS_DEV_SATA
)
1649 fprintf(stderr
, "SATA ");
1650 else if (elem
->type
== SYS_DEV_SAS
)
1651 fprintf(stderr
, "SAS ");
1652 fprintf(stderr
, "RAID controller");
1654 fprintf(stderr
, " at %s", elem
->pci_id
);
1655 fprintf(stderr
, ".\n");
1660 static int ahci_get_port_count(const char *hba_path
, int *port_count
)
1667 if ((dir
= opendir(hba_path
)) == NULL
)
1670 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
1673 if (sscanf(ent
->d_name
, "host%d", &host
) != 1)
1675 if (*port_count
== 0)
1677 else if (host
< host_base
)
1680 if (host
+ 1 > *port_count
+ host_base
)
1681 *port_count
= host
+ 1 - host_base
;
1687 static void print_imsm_capability(const struct imsm_orom
*orom
)
1689 printf(" Platform : Intel(R) Matrix Storage Manager\n");
1690 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
1691 orom
->hotfix_ver
, orom
->build
);
1692 printf(" RAID Levels :%s%s%s%s%s\n",
1693 imsm_orom_has_raid0(orom
) ? " raid0" : "",
1694 imsm_orom_has_raid1(orom
) ? " raid1" : "",
1695 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
1696 imsm_orom_has_raid10(orom
) ? " raid10" : "",
1697 imsm_orom_has_raid5(orom
) ? " raid5" : "");
1698 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1699 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
1700 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
1701 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
1702 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
1703 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
1704 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
1705 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
1706 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
1707 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
1708 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
1709 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
1710 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
1711 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
1712 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
1713 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
1714 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
1715 printf(" Max Disks : %d\n", orom
->tds
);
1716 printf(" Max Volumes : %d\n", orom
->vpa
);
1720 static int detail_platform_imsm(int verbose
, int enumerate_only
)
1722 /* There are two components to imsm platform support, the ahci SATA
1723 * controller and the option-rom. To find the SATA controller we
1724 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
1725 * controller with the Intel vendor id is present. This approach
1726 * allows mdadm to leverage the kernel's ahci detection logic, with the
1727 * caveat that if ahci.ko is not loaded mdadm will not be able to
1728 * detect platform raid capabilities. The option-rom resides in a
1729 * platform "Adapter ROM". We scan for its signature to retrieve the
1730 * platform capabilities. If raid support is disabled in the BIOS the
1731 * option-rom capability structure will not be available.
1733 const struct imsm_orom
*orom
;
1734 struct sys_dev
*list
, *hba
;
1739 if (enumerate_only
) {
1740 if (check_env("IMSM_NO_PLATFORM"))
1742 list
= find_intel_devices();
1745 for (hba
= list
; hba
; hba
= hba
->next
) {
1746 orom
= find_imsm_capability(hba
->type
);
1752 free_sys_dev(&list
);
1756 list
= find_intel_devices();
1759 fprintf(stderr
, Name
": no active Intel(R) RAID "
1760 "controller found.\n");
1761 free_sys_dev(&list
);
1764 print_found_intel_controllers(list
);
1766 for (hba
= list
; hba
; hba
= hba
->next
) {
1767 orom
= find_imsm_capability(hba
->type
);
1769 fprintf(stderr
, Name
": imsm capabilities not found for controller: %s (type %s)\n",
1770 hba
->path
, get_sys_dev_type(hba
->type
));
1772 print_imsm_capability(orom
);
1775 for (hba
= list
; hba
; hba
= hba
->next
) {
1776 printf(" I/O Controller : %s (%s)\n",
1777 hba
->path
, get_sys_dev_type(hba
->type
));
1779 if (hba
->type
== SYS_DEV_SATA
) {
1780 host_base
= ahci_get_port_count(hba
->path
, &port_count
);
1781 if (ahci_enumerate_ports(hba
->path
, port_count
, host_base
, verbose
)) {
1783 fprintf(stderr
, Name
": failed to enumerate "
1784 "ports on SATA controller at %s.", hba
->pci_id
);
1790 free_sys_dev(&list
);
1795 static int match_home_imsm(struct supertype
*st
, char *homehost
)
1797 /* the imsm metadata format does not specify any host
1798 * identification information. We return -1 since we can never
1799 * confirm nor deny whether a given array is "meant" for this
1800 * host. We rely on compare_super and the 'family_num' fields to
1801 * exclude member disks that do not belong, and we rely on
1802 * mdadm.conf to specify the arrays that should be assembled.
1803 * Auto-assembly may still pick up "foreign" arrays.
1809 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
1811 /* The uuid returned here is used for:
1812 * uuid to put into bitmap file (Create, Grow)
1813 * uuid for backup header when saving critical section (Grow)
1814 * comparing uuids when re-adding a device into an array
1815 * In these cases the uuid required is that of the data-array,
1816 * not the device-set.
1817 * uuid to recognise same set when adding a missing device back
1818 * to an array. This is a uuid for the device-set.
1820 * For each of these we can make do with a truncated
1821 * or hashed uuid rather than the original, as long as
1823 * In each case the uuid required is that of the data-array,
1824 * not the device-set.
1826 /* imsm does not track uuid's so we synthesis one using sha1 on
1827 * - The signature (Which is constant for all imsm array, but no matter)
1828 * - the orig_family_num of the container
1829 * - the index number of the volume
1830 * - the 'serial' number of the volume.
1831 * Hopefully these are all constant.
1833 struct intel_super
*super
= st
->sb
;
1836 struct sha1_ctx ctx
;
1837 struct imsm_dev
*dev
= NULL
;
1840 /* some mdadm versions failed to set ->orig_family_num, in which
1841 * case fall back to ->family_num. orig_family_num will be
1842 * fixed up with the first metadata update.
1844 family_num
= super
->anchor
->orig_family_num
;
1845 if (family_num
== 0)
1846 family_num
= super
->anchor
->family_num
;
1847 sha1_init_ctx(&ctx
);
1848 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
1849 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
1850 if (super
->current_vol
>= 0)
1851 dev
= get_imsm_dev(super
, super
->current_vol
);
1853 __u32 vol
= super
->current_vol
;
1854 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
1855 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
1857 sha1_finish_ctx(&ctx
, buf
);
1858 memcpy(uuid
, buf
, 4*4);
1863 get_imsm_numerical_version(struct imsm_super
*mpb
, int *m
, int *p
)
1865 __u8
*v
= get_imsm_version(mpb
);
1866 __u8
*end
= mpb
->sig
+ MAX_SIGNATURE_LENGTH
;
1867 char major
[] = { 0, 0, 0 };
1868 char minor
[] = { 0 ,0, 0 };
1869 char patch
[] = { 0, 0, 0 };
1870 char *ver_parse
[] = { major
, minor
, patch
};
1874 while (*v
!= '\0' && v
< end
) {
1875 if (*v
!= '.' && j
< 2)
1876 ver_parse
[i
][j
++] = *v
;
1884 *m
= strtol(minor
, NULL
, 0);
1885 *p
= strtol(patch
, NULL
, 0);
1889 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
1891 /* migr_strip_size when repairing or initializing parity */
1892 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1893 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1895 switch (get_imsm_raid_level(map
)) {
1900 return 128*1024 >> 9;
1904 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
1906 /* migr_strip_size when rebuilding a degraded disk, no idea why
1907 * this is different than migr_strip_size_resync(), but it's good
1910 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
1911 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1913 switch (get_imsm_raid_level(map
)) {
1916 if (map
->num_members
% map
->num_domains
== 0)
1917 return 128*1024 >> 9;
1921 return max((__u32
) 64*1024 >> 9, chunk
);
1923 return 128*1024 >> 9;
1927 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
1929 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
1930 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
1931 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
1932 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
1934 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
1937 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
1939 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
1940 int level
= get_imsm_raid_level(lo
);
1942 if (level
== 1 || level
== 10) {
1943 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
1945 return hi
->num_domains
;
1947 return num_stripes_per_unit_resync(dev
);
1950 static __u8
imsm_num_data_members(struct imsm_dev
*dev
, int second_map
)
1952 /* named 'imsm_' because raid0, raid1 and raid10
1953 * counter-intuitively have the same number of data disks
1955 struct imsm_map
*map
= get_imsm_map(dev
, second_map
);
1957 switch (get_imsm_raid_level(map
)) {
1961 return map
->num_members
;
1963 return map
->num_members
- 1;
1965 dprintf("%s: unsupported raid level\n", __func__
);
1970 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
1972 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1973 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1975 switch(get_imsm_raid_level(map
)) {
1978 return chunk
* map
->num_domains
;
1980 return chunk
* map
->num_members
;
1986 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
1988 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
1989 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1990 __u32 strip
= block
/ chunk
;
1992 switch (get_imsm_raid_level(map
)) {
1995 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
1996 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
1998 return vol_stripe
* chunk
+ block
% chunk
;
2000 __u32 stripe
= strip
/ (map
->num_members
- 1);
2002 return stripe
* chunk
+ block
% chunk
;
2009 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
2010 struct imsm_dev
*dev
)
2012 /* calculate the conversion factor between per member 'blocks'
2013 * (md/{resync,rebuild}_start) and imsm migration units, return
2014 * 0 for the 'not migrating' and 'unsupported migration' cases
2016 if (!dev
->vol
.migr_state
)
2019 switch (migr_type(dev
)) {
2020 case MIGR_GEN_MIGR
: {
2021 struct migr_record
*migr_rec
= super
->migr_rec
;
2022 return __le32_to_cpu(migr_rec
->blocks_per_unit
);
2027 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2028 __u32 stripes_per_unit
;
2029 __u32 blocks_per_unit
;
2038 /* yes, this is really the translation of migr_units to
2039 * per-member blocks in the 'resync' case
2041 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
2042 migr_chunk
= migr_strip_blocks_resync(dev
);
2043 disks
= imsm_num_data_members(dev
, MAP_0
);
2044 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
2045 stripe
= __le16_to_cpu(map
->blocks_per_strip
) * disks
;
2046 segment
= blocks_per_unit
/ stripe
;
2047 block_rel
= blocks_per_unit
- segment
* stripe
;
2048 parity_depth
= parity_segment_depth(dev
);
2049 block_map
= map_migr_block(dev
, block_rel
);
2050 return block_map
+ parity_depth
* segment
;
2052 case MIGR_REBUILD
: {
2053 __u32 stripes_per_unit
;
2056 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
2057 migr_chunk
= migr_strip_blocks_rebuild(dev
);
2058 return migr_chunk
* stripes_per_unit
;
2060 case MIGR_STATE_CHANGE
:
2066 static int imsm_level_to_layout(int level
)
2074 return ALGORITHM_LEFT_ASYMMETRIC
;
2081 /*******************************************************************************
2082 * Function: read_imsm_migr_rec
2083 * Description: Function reads imsm migration record from last sector of disk
2085 * fd : disk descriptor
2086 * super : metadata info
2090 ******************************************************************************/
2091 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
)
2094 unsigned long long dsize
;
2096 get_dev_size(fd
, NULL
, &dsize
);
2097 if (lseek64(fd
, dsize
- MIGR_REC_POSITION
, SEEK_SET
) < 0) {
2099 Name
": Cannot seek to anchor block: %s\n",
2103 if (read(fd
, super
->migr_rec_buf
, MIGR_REC_BUF_SIZE
) !=
2104 MIGR_REC_BUF_SIZE
) {
2106 Name
": Cannot read migr record block: %s\n",
2116 static struct imsm_dev
*imsm_get_device_during_migration(
2117 struct intel_super
*super
)
2120 struct intel_dev
*dv
;
2122 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
2123 if (is_gen_migration(dv
->dev
))
2129 /*******************************************************************************
2130 * Function: load_imsm_migr_rec
2131 * Description: Function reads imsm migration record (it is stored at the last
2134 * super : imsm internal array info
2135 * info : general array info
2139 * -2 : no migration in progress
2140 ******************************************************************************/
2141 static int load_imsm_migr_rec(struct intel_super
*super
, struct mdinfo
*info
)
2144 struct dl
*dl
= NULL
;
2148 struct imsm_dev
*dev
;
2149 struct imsm_map
*map
= NULL
;
2152 /* find map under migration */
2153 dev
= imsm_get_device_during_migration(super
);
2154 /* nothing to load,no migration in progress?
2158 map
= get_imsm_map(dev
, MAP_0
);
2161 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
2162 /* skip spare and failed disks
2164 if (sd
->disk
.raid_disk
< 0)
2166 /* read only from one of the first two slots */
2168 slot
= get_imsm_disk_slot(map
,
2169 sd
->disk
.raid_disk
);
2170 if ((map
== NULL
) || (slot
> 1) || (slot
< 0))
2173 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
2174 fd
= dev_open(nm
, O_RDONLY
);
2180 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
2181 /* skip spare and failed disks
2185 /* read only from one of the first two slots */
2187 slot
= get_imsm_disk_slot(map
, dl
->index
);
2188 if ((map
== NULL
) || (slot
> 1) || (slot
< 0))
2190 sprintf(nm
, "%d:%d", dl
->major
, dl
->minor
);
2191 fd
= dev_open(nm
, O_RDONLY
);
2198 retval
= read_imsm_migr_rec(fd
, super
);
2207 /*******************************************************************************
2208 * function: imsm_create_metadata_checkpoint_update
2209 * Description: It creates update for checkpoint change.
2211 * super : imsm internal array info
2212 * u : pointer to prepared update
2215 * If length is equal to 0, input pointer u contains no update
2216 ******************************************************************************/
2217 static int imsm_create_metadata_checkpoint_update(
2218 struct intel_super
*super
,
2219 struct imsm_update_general_migration_checkpoint
**u
)
2222 int update_memory_size
= 0;
2224 dprintf("imsm_create_metadata_checkpoint_update(enter)\n");
2230 /* size of all update data without anchor */
2231 update_memory_size
=
2232 sizeof(struct imsm_update_general_migration_checkpoint
);
2234 *u
= calloc(1, update_memory_size
);
2236 dprintf("error: cannot get memory for "
2237 "imsm_create_metadata_checkpoint_update update\n");
2240 (*u
)->type
= update_general_migration_checkpoint
;
2241 (*u
)->curr_migr_unit
= __le32_to_cpu(super
->migr_rec
->curr_migr_unit
);
2242 dprintf("imsm_create_metadata_checkpoint_update: prepared for %u\n",
2243 (*u
)->curr_migr_unit
);
2245 return update_memory_size
;
2249 static void imsm_update_metadata_locally(struct supertype
*st
,
2250 void *buf
, int len
);
2252 /*******************************************************************************
2253 * Function: write_imsm_migr_rec
2254 * Description: Function writes imsm migration record
2255 * (at the last sector of disk)
2257 * super : imsm internal array info
2261 ******************************************************************************/
2262 static int write_imsm_migr_rec(struct supertype
*st
)
2264 struct intel_super
*super
= st
->sb
;
2265 unsigned long long dsize
;
2271 struct imsm_update_general_migration_checkpoint
*u
;
2272 struct imsm_dev
*dev
;
2273 struct imsm_map
*map
= NULL
;
2275 /* find map under migration */
2276 dev
= imsm_get_device_during_migration(super
);
2277 /* if no migration, write buffer anyway to clear migr_record
2278 * on disk based on first available device
2281 dev
= get_imsm_dev(super
, super
->current_vol
< 0 ? 0 :
2282 super
->current_vol
);
2284 map
= get_imsm_map(dev
, MAP_0
);
2286 for (sd
= super
->disks
; sd
; sd
= sd
->next
) {
2289 /* skip failed and spare devices */
2292 /* write to 2 first slots only */
2294 slot
= get_imsm_disk_slot(map
, sd
->index
);
2295 if ((map
== NULL
) || (slot
> 1) || (slot
< 0))
2298 sprintf(nm
, "%d:%d", sd
->major
, sd
->minor
);
2299 fd
= dev_open(nm
, O_RDWR
);
2302 get_dev_size(fd
, NULL
, &dsize
);
2303 if (lseek64(fd
, dsize
- MIGR_REC_POSITION
, SEEK_SET
) < 0) {
2305 Name
": Cannot seek to anchor block: %s\n",
2309 if (write(fd
, super
->migr_rec_buf
, MIGR_REC_BUF_SIZE
) !=
2310 MIGR_REC_BUF_SIZE
) {
2312 Name
": Cannot write migr record block: %s\n",
2319 /* update checkpoint information in metadata */
2320 len
= imsm_create_metadata_checkpoint_update(super
, &u
);
2323 dprintf("imsm: Cannot prepare update\n");
2326 /* update metadata locally */
2327 imsm_update_metadata_locally(st
, u
, len
);
2328 /* and possibly remotely */
2329 if (st
->update_tail
) {
2330 append_metadata_update(st
, u
, len
);
2331 /* during reshape we do all work inside metadata handler
2332 * manage_reshape(), so metadata update has to be triggered
2335 flush_metadata_updates(st
);
2336 st
->update_tail
= &st
->updates
;
2346 #endif /* MDASSEMBLE */
2348 /* spare/missing disks activations are not allowe when
2349 * array/container performs reshape operation, because
2350 * all arrays in container works on the same disks set
2352 int imsm_reshape_blocks_arrays_changes(struct intel_super
*super
)
2355 struct intel_dev
*i_dev
;
2356 struct imsm_dev
*dev
;
2358 /* check whole container
2360 for (i_dev
= super
->devlist
; i_dev
; i_dev
= i_dev
->next
) {
2362 if (is_gen_migration(dev
)) {
2363 /* No repair during any migration in container
2372 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
2374 struct intel_super
*super
= st
->sb
;
2375 struct migr_record
*migr_rec
= super
->migr_rec
;
2376 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
2377 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2378 struct imsm_map
*prev_map
= get_imsm_map(dev
, MAP_1
);
2379 struct imsm_map
*map_to_analyse
= map
;
2382 unsigned int component_size_alligment
;
2383 int map_disks
= info
->array
.raid_disks
;
2385 memset(info
, 0, sizeof(*info
));
2387 map_to_analyse
= prev_map
;
2389 dl
= super
->current_disk
;
2391 info
->container_member
= super
->current_vol
;
2392 info
->array
.raid_disks
= map
->num_members
;
2393 info
->array
.level
= get_imsm_raid_level(map_to_analyse
);
2394 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
2395 info
->array
.md_minor
= -1;
2396 info
->array
.ctime
= 0;
2397 info
->array
.utime
= 0;
2398 info
->array
.chunk_size
=
2399 __le16_to_cpu(map_to_analyse
->blocks_per_strip
) << 9;
2400 info
->array
.state
= !dev
->vol
.dirty
;
2401 info
->custom_array_size
= __le32_to_cpu(dev
->size_high
);
2402 info
->custom_array_size
<<= 32;
2403 info
->custom_array_size
|= __le32_to_cpu(dev
->size_low
);
2404 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
2406 if (is_gen_migration(dev
)) {
2407 info
->reshape_active
= 1;
2408 info
->new_level
= get_imsm_raid_level(map
);
2409 info
->new_layout
= imsm_level_to_layout(info
->new_level
);
2410 info
->new_chunk
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
2411 info
->delta_disks
= map
->num_members
- prev_map
->num_members
;
2412 if (info
->delta_disks
) {
2413 /* this needs to be applied to every array
2416 info
->reshape_active
= CONTAINER_RESHAPE
;
2418 /* We shape information that we give to md might have to be
2419 * modify to cope with md's requirement for reshaping arrays.
2420 * For example, when reshaping a RAID0, md requires it to be
2421 * presented as a degraded RAID4.
2422 * Also if a RAID0 is migrating to a RAID5 we need to specify
2423 * the array as already being RAID5, but the 'before' layout
2424 * is a RAID4-like layout.
2426 switch (info
->array
.level
) {
2428 switch(info
->new_level
) {
2430 /* conversion is happening as RAID4 */
2431 info
->array
.level
= 4;
2432 info
->array
.raid_disks
+= 1;
2435 /* conversion is happening as RAID5 */
2436 info
->array
.level
= 5;
2437 info
->array
.layout
= ALGORITHM_PARITY_N
;
2438 info
->delta_disks
-= 1;
2441 /* FIXME error message */
2442 info
->array
.level
= UnSet
;
2448 info
->new_level
= UnSet
;
2449 info
->new_layout
= UnSet
;
2450 info
->new_chunk
= info
->array
.chunk_size
;
2451 info
->delta_disks
= 0;
2455 info
->disk
.major
= dl
->major
;
2456 info
->disk
.minor
= dl
->minor
;
2457 info
->disk
.number
= dl
->index
;
2458 info
->disk
.raid_disk
= get_imsm_disk_slot(map_to_analyse
,
2462 info
->data_offset
= __le32_to_cpu(map_to_analyse
->pba_of_lba0
);
2463 info
->component_size
=
2464 __le32_to_cpu(map_to_analyse
->blocks_per_member
);
2466 /* check component size aligment
2468 component_size_alligment
=
2469 info
->component_size
% (info
->array
.chunk_size
/512);
2471 if (component_size_alligment
&&
2472 (info
->array
.level
!= 1) && (info
->array
.level
!= UnSet
)) {
2473 dprintf("imsm: reported component size alligned from %llu ",
2474 info
->component_size
);
2475 info
->component_size
-= component_size_alligment
;
2476 dprintf("to %llu (%i).\n",
2477 info
->component_size
, component_size_alligment
);
2480 memset(info
->uuid
, 0, sizeof(info
->uuid
));
2481 info
->recovery_start
= MaxSector
;
2483 info
->reshape_progress
= 0;
2484 info
->resync_start
= MaxSector
;
2485 if ((map_to_analyse
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
2487 imsm_reshape_blocks_arrays_changes(super
) == 0) {
2488 info
->resync_start
= 0;
2490 if (dev
->vol
.migr_state
) {
2491 switch (migr_type(dev
)) {
2494 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
2496 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
2498 info
->resync_start
= blocks_per_unit
* units
;
2501 case MIGR_GEN_MIGR
: {
2502 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
2504 __u64 units
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
2505 unsigned long long array_blocks
;
2508 if (__le32_to_cpu(migr_rec
->ascending_migr
) &&
2510 (__le32_to_cpu(migr_rec
->num_migr_units
)-1)) &&
2511 (super
->migr_rec
->rec_status
==
2512 __cpu_to_le32(UNIT_SRC_IN_CP_AREA
)))
2515 info
->reshape_progress
= blocks_per_unit
* units
;
2517 dprintf("IMSM: General Migration checkpoint : %llu "
2518 "(%llu) -> read reshape progress : %llu\n",
2519 (unsigned long long)units
,
2520 (unsigned long long)blocks_per_unit
,
2521 info
->reshape_progress
);
2523 used_disks
= imsm_num_data_members(dev
, MAP_1
);
2524 if (used_disks
> 0) {
2525 array_blocks
= map
->blocks_per_member
*
2527 /* round array size down to closest MB
2529 info
->custom_array_size
= (array_blocks
2530 >> SECT_PER_MB_SHIFT
)
2531 << SECT_PER_MB_SHIFT
;
2535 /* we could emulate the checkpointing of
2536 * 'sync_action=check' migrations, but for now
2537 * we just immediately complete them
2540 /* this is handled by container_content_imsm() */
2541 case MIGR_STATE_CHANGE
:
2542 /* FIXME handle other migrations */
2544 /* we are not dirty, so... */
2545 info
->resync_start
= MaxSector
;
2549 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
2550 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
2552 info
->array
.major_version
= -1;
2553 info
->array
.minor_version
= -2;
2554 devname
= devnum2devname(st
->container_dev
);
2555 *info
->text_version
= '\0';
2557 sprintf(info
->text_version
, "/%s/%d", devname
, info
->container_member
);
2559 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
2560 uuid_from_super_imsm(st
, info
->uuid
);
2564 for (i
=0; i
<map_disks
; i
++) {
2566 if (i
< info
->array
.raid_disks
) {
2567 struct imsm_disk
*dsk
;
2568 j
= get_imsm_disk_idx(dev
, i
, MAP_X
);
2569 dsk
= get_imsm_disk(super
, j
);
2570 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
2577 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
2578 int failed
, int look_in_map
);
2580 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
2585 static void manage_second_map(struct intel_super
*super
, struct imsm_dev
*dev
)
2587 if (is_gen_migration(dev
)) {
2590 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
2592 failed
= imsm_count_failed(super
, dev
, MAP_1
);
2593 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_1
);
2594 if (map2
->map_state
!= map_state
) {
2595 map2
->map_state
= map_state
;
2596 super
->updates_pending
++;
2602 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
2606 for (d
= super
->missing
; d
; d
= d
->next
)
2607 if (d
->index
== index
)
2612 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
2614 struct intel_super
*super
= st
->sb
;
2615 struct imsm_disk
*disk
;
2616 int map_disks
= info
->array
.raid_disks
;
2617 int max_enough
= -1;
2619 struct imsm_super
*mpb
;
2621 if (super
->current_vol
>= 0) {
2622 getinfo_super_imsm_volume(st
, info
, map
);
2625 memset(info
, 0, sizeof(*info
));
2627 /* Set raid_disks to zero so that Assemble will always pull in valid
2630 info
->array
.raid_disks
= 0;
2631 info
->array
.level
= LEVEL_CONTAINER
;
2632 info
->array
.layout
= 0;
2633 info
->array
.md_minor
= -1;
2634 info
->array
.ctime
= 0; /* N/A for imsm */
2635 info
->array
.utime
= 0;
2636 info
->array
.chunk_size
= 0;
2638 info
->disk
.major
= 0;
2639 info
->disk
.minor
= 0;
2640 info
->disk
.raid_disk
= -1;
2641 info
->reshape_active
= 0;
2642 info
->array
.major_version
= -1;
2643 info
->array
.minor_version
= -2;
2644 strcpy(info
->text_version
, "imsm");
2645 info
->safe_mode_delay
= 0;
2646 info
->disk
.number
= -1;
2647 info
->disk
.state
= 0;
2649 info
->recovery_start
= MaxSector
;
2650 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
2652 /* do we have the all the insync disks that we expect? */
2653 mpb
= super
->anchor
;
2655 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
2656 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
2657 int failed
, enough
, j
, missing
= 0;
2658 struct imsm_map
*map
;
2661 failed
= imsm_count_failed(super
, dev
, MAP_0
);
2662 state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
2663 map
= get_imsm_map(dev
, MAP_0
);
2665 /* any newly missing disks?
2666 * (catches single-degraded vs double-degraded)
2668 for (j
= 0; j
< map
->num_members
; j
++) {
2669 __u32 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
2670 __u32 idx
= ord_to_idx(ord
);
2672 if (!(ord
& IMSM_ORD_REBUILD
) &&
2673 get_imsm_missing(super
, idx
)) {
2679 if (state
== IMSM_T_STATE_FAILED
)
2681 else if (state
== IMSM_T_STATE_DEGRADED
&&
2682 (state
!= map
->map_state
|| missing
))
2684 else /* we're normal, or already degraded */
2686 if (is_gen_migration(dev
) && missing
) {
2687 /* during general migration we need all disks
2688 * that process is running on.
2689 * No new missing disk is allowed.
2693 /* no more checks necessary
2697 /* in the missing/failed disk case check to see
2698 * if at least one array is runnable
2700 max_enough
= max(max_enough
, enough
);
2702 dprintf("%s: enough: %d\n", __func__
, max_enough
);
2703 info
->container_enough
= max_enough
;
2706 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
2708 disk
= &super
->disks
->disk
;
2709 info
->data_offset
= __le32_to_cpu(disk
->total_blocks
) - reserved
;
2710 info
->component_size
= reserved
;
2711 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
2712 /* we don't change info->disk.raid_disk here because
2713 * this state will be finalized in mdmon after we have
2714 * found the 'most fresh' version of the metadata
2716 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
2717 info
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
2720 /* only call uuid_from_super_imsm when this disk is part of a populated container,
2721 * ->compare_super may have updated the 'num_raid_devs' field for spares
2723 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
2724 uuid_from_super_imsm(st
, info
->uuid
);
2726 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
2728 /* I don't know how to compute 'map' on imsm, so use safe default */
2731 for (i
= 0; i
< map_disks
; i
++)
2737 /* allocates memory and fills disk in mdinfo structure
2738 * for each disk in array */
2739 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
2741 struct mdinfo
*mddev
= NULL
;
2742 struct intel_super
*super
= st
->sb
;
2743 struct imsm_disk
*disk
;
2746 if (!super
|| !super
->disks
)
2749 mddev
= malloc(sizeof(*mddev
));
2751 fprintf(stderr
, Name
": Failed to allocate memory.\n");
2754 memset(mddev
, 0, sizeof(*mddev
));
2758 tmp
= malloc(sizeof(*tmp
));
2760 fprintf(stderr
, Name
": Failed to allocate memory.\n");
2765 memset(tmp
, 0, sizeof(*tmp
));
2767 tmp
->next
= mddev
->devs
;
2769 tmp
->disk
.number
= count
++;
2770 tmp
->disk
.major
= dl
->major
;
2771 tmp
->disk
.minor
= dl
->minor
;
2772 tmp
->disk
.state
= is_configured(disk
) ?
2773 (1 << MD_DISK_ACTIVE
) : 0;
2774 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
2775 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
2776 tmp
->disk
.raid_disk
= -1;
2782 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
2783 char *update
, char *devname
, int verbose
,
2784 int uuid_set
, char *homehost
)
2786 /* For 'assemble' and 'force' we need to return non-zero if any
2787 * change was made. For others, the return value is ignored.
2788 * Update options are:
2789 * force-one : This device looks a bit old but needs to be included,
2790 * update age info appropriately.
2791 * assemble: clear any 'faulty' flag to allow this device to
2793 * force-array: Array is degraded but being forced, mark it clean
2794 * if that will be needed to assemble it.
2796 * newdev: not used ????
2797 * grow: Array has gained a new device - this is currently for
2799 * resync: mark as dirty so a resync will happen.
2800 * name: update the name - preserving the homehost
2801 * uuid: Change the uuid of the array to match watch is given
2803 * Following are not relevant for this imsm:
2804 * sparc2.2 : update from old dodgey metadata
2805 * super-minor: change the preferred_minor number
2806 * summaries: update redundant counters.
2807 * homehost: update the recorded homehost
2808 * _reshape_progress: record new reshape_progress position.
2811 struct intel_super
*super
= st
->sb
;
2812 struct imsm_super
*mpb
;
2814 /* we can only update container info */
2815 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
2818 mpb
= super
->anchor
;
2820 if (strcmp(update
, "uuid") == 0) {
2821 /* We take this to mean that the family_num should be updated.
2822 * However that is much smaller than the uuid so we cannot really
2823 * allow an explicit uuid to be given. And it is hard to reliably
2825 * So if !uuid_set we know the current uuid is random and just used
2826 * the first 'int' and copy it to the other 3 positions.
2827 * Otherwise we require the 4 'int's to be the same as would be the
2828 * case if we are using a random uuid. So an explicit uuid will be
2829 * accepted as long as all for ints are the same... which shouldn't hurt
2832 info
->uuid
[1] = info
->uuid
[2] = info
->uuid
[3] = info
->uuid
[0];
2835 if (info
->uuid
[0] != info
->uuid
[1] ||
2836 info
->uuid
[1] != info
->uuid
[2] ||
2837 info
->uuid
[2] != info
->uuid
[3])
2843 mpb
->orig_family_num
= info
->uuid
[0];
2844 } else if (strcmp(update
, "assemble") == 0)
2849 /* successful update? recompute checksum */
2851 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
2856 static size_t disks_to_mpb_size(int disks
)
2860 size
= sizeof(struct imsm_super
);
2861 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
2862 size
+= 2 * sizeof(struct imsm_dev
);
2863 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
2864 size
+= (4 - 2) * sizeof(struct imsm_map
);
2865 /* 4 possible disk_ord_tbl's */
2866 size
+= 4 * (disks
- 1) * sizeof(__u32
);
2871 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
)
2873 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
2876 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
2879 static void free_devlist(struct intel_super
*super
)
2881 struct intel_dev
*dv
;
2883 while (super
->devlist
) {
2884 dv
= super
->devlist
->next
;
2885 free(super
->devlist
->dev
);
2886 free(super
->devlist
);
2887 super
->devlist
= dv
;
2891 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
2893 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
2896 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
)
2900 * 0 same, or first was empty, and second was copied
2901 * 1 second had wrong number
2903 * 3 wrong other info
2905 struct intel_super
*first
= st
->sb
;
2906 struct intel_super
*sec
= tst
->sb
;
2913 /* in platform dependent environment test if the disks
2914 * use the same Intel hba
2916 if (!check_env("IMSM_NO_PLATFORM")) {
2917 if (!first
->hba
|| !sec
->hba
||
2918 (first
->hba
->type
!= sec
->hba
->type
)) {
2920 "HBAs of devices does not match %s != %s\n",
2921 first
->hba
? get_sys_dev_type(first
->hba
->type
) : NULL
,
2922 sec
->hba
? get_sys_dev_type(sec
->hba
->type
) : NULL
);
2927 /* if an anchor does not have num_raid_devs set then it is a free
2930 if (first
->anchor
->num_raid_devs
> 0 &&
2931 sec
->anchor
->num_raid_devs
> 0) {
2932 /* Determine if these disks might ever have been
2933 * related. Further disambiguation can only take place
2934 * in load_super_imsm_all
2936 __u32 first_family
= first
->anchor
->orig_family_num
;
2937 __u32 sec_family
= sec
->anchor
->orig_family_num
;
2939 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
2940 MAX_SIGNATURE_LENGTH
) != 0)
2943 if (first_family
== 0)
2944 first_family
= first
->anchor
->family_num
;
2945 if (sec_family
== 0)
2946 sec_family
= sec
->anchor
->family_num
;
2948 if (first_family
!= sec_family
)
2954 /* if 'first' is a spare promote it to a populated mpb with sec's
2957 if (first
->anchor
->num_raid_devs
== 0 &&
2958 sec
->anchor
->num_raid_devs
> 0) {
2960 struct intel_dev
*dv
;
2961 struct imsm_dev
*dev
;
2963 /* we need to copy raid device info from sec if an allocation
2964 * fails here we don't associate the spare
2966 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++) {
2967 dv
= malloc(sizeof(*dv
));
2970 dev
= malloc(sizeof_imsm_dev(get_imsm_dev(sec
, i
), 1));
2977 dv
->next
= first
->devlist
;
2978 first
->devlist
= dv
;
2980 if (i
< sec
->anchor
->num_raid_devs
) {
2981 /* allocation failure */
2982 free_devlist(first
);
2983 fprintf(stderr
, "imsm: failed to associate spare\n");
2986 first
->anchor
->num_raid_devs
= sec
->anchor
->num_raid_devs
;
2987 first
->anchor
->orig_family_num
= sec
->anchor
->orig_family_num
;
2988 first
->anchor
->family_num
= sec
->anchor
->family_num
;
2989 memcpy(first
->anchor
->sig
, sec
->anchor
->sig
, MAX_SIGNATURE_LENGTH
);
2990 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++)
2991 imsm_copy_dev(get_imsm_dev(first
, i
), get_imsm_dev(sec
, i
));
2997 static void fd2devname(int fd
, char *name
)
3001 char dname
[PATH_MAX
];
3006 if (fstat(fd
, &st
) != 0)
3008 sprintf(path
, "/sys/dev/block/%d:%d",
3009 major(st
.st_rdev
), minor(st
.st_rdev
));
3011 rv
= readlink(path
, dname
, sizeof(dname
)-1);
3016 nm
= strrchr(dname
, '/');
3019 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
3023 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
3025 static int imsm_read_serial(int fd
, char *devname
,
3026 __u8 serial
[MAX_RAID_SERIAL_LEN
])
3028 unsigned char scsi_serial
[255];
3037 memset(scsi_serial
, 0, sizeof(scsi_serial
));
3039 rv
= scsi_get_serial(fd
, scsi_serial
, sizeof(scsi_serial
));
3041 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
3042 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3043 fd2devname(fd
, (char *) serial
);
3050 Name
": Failed to retrieve serial for %s\n",
3055 rsp_len
= scsi_serial
[3];
3059 Name
": Failed to retrieve serial for %s\n",
3063 rsp_buf
= (char *) &scsi_serial
[4];
3065 /* trim all whitespace and non-printable characters and convert
3068 for (i
= 0, dest
= rsp_buf
; i
< rsp_len
; i
++) {
3071 /* ':' is reserved for use in placeholder serial
3072 * numbers for missing disks
3080 len
= dest
- rsp_buf
;
3083 /* truncate leading characters */
3084 if (len
> MAX_RAID_SERIAL_LEN
) {
3085 dest
+= len
- MAX_RAID_SERIAL_LEN
;
3086 len
= MAX_RAID_SERIAL_LEN
;
3089 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3090 memcpy(serial
, dest
, len
);
3095 static int serialcmp(__u8
*s1
, __u8
*s2
)
3097 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
3100 static void serialcpy(__u8
*dest
, __u8
*src
)
3102 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
3105 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
3109 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3110 if (serialcmp(dl
->serial
, serial
) == 0)
3116 static struct imsm_disk
*
3117 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
3121 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3122 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3124 if (serialcmp(disk
->serial
, serial
) == 0) {
3135 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3137 struct imsm_disk
*disk
;
3142 __u8 serial
[MAX_RAID_SERIAL_LEN
];
3144 rv
= imsm_read_serial(fd
, devname
, serial
);
3149 dl
= calloc(1, sizeof(*dl
));
3153 Name
": failed to allocate disk buffer for %s\n",
3159 dl
->major
= major(stb
.st_rdev
);
3160 dl
->minor
= minor(stb
.st_rdev
);
3161 dl
->next
= super
->disks
;
3162 dl
->fd
= keep_fd
? fd
: -1;
3163 assert(super
->disks
== NULL
);
3165 serialcpy(dl
->serial
, serial
);
3168 fd2devname(fd
, name
);
3170 dl
->devname
= strdup(devname
);
3172 dl
->devname
= strdup(name
);
3174 /* look up this disk's index in the current anchor */
3175 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
3178 /* only set index on disks that are a member of a
3179 * populated contianer, i.e. one with raid_devs
3181 if (is_failed(&dl
->disk
))
3183 else if (is_spare(&dl
->disk
))
3191 /* When migrating map0 contains the 'destination' state while map1
3192 * contains the current state. When not migrating map0 contains the
3193 * current state. This routine assumes that map[0].map_state is set to
3194 * the current array state before being called.
3196 * Migration is indicated by one of the following states
3197 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
3198 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
3199 * map1state=unitialized)
3200 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
3202 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
3203 * map1state=degraded)
3204 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
3207 static void migrate(struct imsm_dev
*dev
, struct intel_super
*super
,
3208 __u8 to_state
, int migr_type
)
3210 struct imsm_map
*dest
;
3211 struct imsm_map
*src
= get_imsm_map(dev
, MAP_0
);
3213 dev
->vol
.migr_state
= 1;
3214 set_migr_type(dev
, migr_type
);
3215 dev
->vol
.curr_migr_unit
= 0;
3216 dest
= get_imsm_map(dev
, MAP_1
);
3218 /* duplicate and then set the target end state in map[0] */
3219 memcpy(dest
, src
, sizeof_imsm_map(src
));
3220 if ((migr_type
== MIGR_REBUILD
) ||
3221 (migr_type
== MIGR_GEN_MIGR
)) {
3225 for (i
= 0; i
< src
->num_members
; i
++) {
3226 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
3227 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
3231 if (migr_type
== MIGR_GEN_MIGR
)
3232 /* Clear migration record */
3233 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
3235 src
->map_state
= to_state
;
3238 static void end_migration(struct imsm_dev
*dev
, struct intel_super
*super
,
3241 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3242 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
== 0 ?
3246 /* merge any IMSM_ORD_REBUILD bits that were not successfully
3247 * completed in the last migration.
3249 * FIXME add support for raid-level-migration
3251 if ((map_state
!= map
->map_state
) && (is_gen_migration(dev
) == 0) &&
3252 (prev
->map_state
!= IMSM_T_STATE_UNINITIALIZED
)) {
3253 /* when final map state is other than expected
3254 * merge maps (not for migration)
3258 for (i
= 0; i
< prev
->num_members
; i
++)
3259 for (j
= 0; j
< map
->num_members
; j
++)
3260 /* during online capacity expansion
3261 * disks position can be changed
3262 * if takeover is used
3264 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
3265 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
3266 map
->disk_ord_tbl
[j
] |=
3267 prev
->disk_ord_tbl
[i
];
3270 failed
= imsm_count_failed(super
, dev
, MAP_0
);
3271 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
3274 dev
->vol
.migr_state
= 0;
3275 set_migr_type(dev
, 0);
3276 dev
->vol
.curr_migr_unit
= 0;
3277 map
->map_state
= map_state
;
3281 static int parse_raid_devices(struct intel_super
*super
)
3284 struct imsm_dev
*dev_new
;
3285 size_t len
, len_migr
;
3287 size_t space_needed
= 0;
3288 struct imsm_super
*mpb
= super
->anchor
;
3290 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3291 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3292 struct intel_dev
*dv
;
3294 len
= sizeof_imsm_dev(dev_iter
, 0);
3295 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
3297 space_needed
+= len_migr
- len
;
3299 dv
= malloc(sizeof(*dv
));
3302 if (max_len
< len_migr
)
3304 if (max_len
> len_migr
)
3305 space_needed
+= max_len
- len_migr
;
3306 dev_new
= malloc(max_len
);
3311 imsm_copy_dev(dev_new
, dev_iter
);
3314 dv
->next
= super
->devlist
;
3315 super
->devlist
= dv
;
3318 /* ensure that super->buf is large enough when all raid devices
3321 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
3324 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
, 512);
3325 if (posix_memalign(&buf
, 512, len
) != 0)
3328 memcpy(buf
, super
->buf
, super
->len
);
3329 memset(buf
+ super
->len
, 0, len
- super
->len
);
3338 /* retrieve a pointer to the bbm log which starts after all raid devices */
3339 struct bbm_log
*__get_imsm_bbm_log(struct imsm_super
*mpb
)
3343 if (__le32_to_cpu(mpb
->bbm_log_size
)) {
3345 ptr
+= mpb
->mpb_size
- __le32_to_cpu(mpb
->bbm_log_size
);
3351 /*******************************************************************************
3352 * Function: check_mpb_migr_compatibility
3353 * Description: Function checks for unsupported migration features:
3354 * - migration optimization area (pba_of_lba0)
3355 * - descending reshape (ascending_migr)
3357 * super : imsm metadata information
3359 * 0 : migration is compatible
3360 * -1 : migration is not compatible
3361 ******************************************************************************/
3362 int check_mpb_migr_compatibility(struct intel_super
*super
)
3364 struct imsm_map
*map0
, *map1
;
3365 struct migr_record
*migr_rec
= super
->migr_rec
;
3368 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3369 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3372 dev_iter
->vol
.migr_state
== 1 &&
3373 dev_iter
->vol
.migr_type
== MIGR_GEN_MIGR
) {
3374 /* This device is migrating */
3375 map0
= get_imsm_map(dev_iter
, MAP_0
);
3376 map1
= get_imsm_map(dev_iter
, MAP_1
);
3377 if (map0
->pba_of_lba0
!= map1
->pba_of_lba0
)
3378 /* migration optimization area was used */
3380 if (migr_rec
->ascending_migr
== 0
3381 && migr_rec
->dest_depth_per_unit
> 0)
3382 /* descending reshape not supported yet */
3389 static void __free_imsm(struct intel_super
*super
, int free_disks
);
3391 /* load_imsm_mpb - read matrix metadata
3392 * allocates super->mpb to be freed by free_imsm
3394 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
3396 unsigned long long dsize
;
3397 unsigned long long sectors
;
3399 struct imsm_super
*anchor
;
3402 get_dev_size(fd
, NULL
, &dsize
);
3406 Name
": %s: device to small for imsm\n",
3411 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0) {
3413 fprintf(stderr
, Name
3414 ": Cannot seek to anchor block on %s: %s\n",
3415 devname
, strerror(errno
));
3419 if (posix_memalign((void**)&anchor
, 512, 512) != 0) {
3422 Name
": Failed to allocate imsm anchor buffer"
3423 " on %s\n", devname
);
3426 if (read(fd
, anchor
, 512) != 512) {
3429 Name
": Cannot read anchor block on %s: %s\n",
3430 devname
, strerror(errno
));
3435 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
3438 Name
": no IMSM anchor on %s\n", devname
);
3443 __free_imsm(super
, 0);
3444 /* reload capability and hba */
3446 /* capability and hba must be updated with new super allocation */
3447 find_intel_hba_capability(fd
, super
, devname
);
3448 super
->len
= ROUND_UP(anchor
->mpb_size
, 512);
3449 if (posix_memalign(&super
->buf
, 512, super
->len
) != 0) {
3452 Name
": unable to allocate %zu byte mpb buffer\n",
3457 memcpy(super
->buf
, anchor
, 512);
3459 sectors
= mpb_sectors(anchor
) - 1;
3462 if (posix_memalign(&super
->migr_rec_buf
, 512, MIGR_REC_BUF_SIZE
) != 0) {
3463 fprintf(stderr
, Name
3464 ": %s could not allocate migr_rec buffer\n", __func__
);
3470 check_sum
= __gen_imsm_checksum(super
->anchor
);
3471 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
3474 Name
": IMSM checksum %x != %x on %s\n",
3476 __le32_to_cpu(super
->anchor
->check_sum
),
3484 /* read the extended mpb */
3485 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0) {
3488 Name
": Cannot seek to extended mpb on %s: %s\n",
3489 devname
, strerror(errno
));
3493 if ((unsigned)read(fd
, super
->buf
+ 512, super
->len
- 512) != super
->len
- 512) {
3496 Name
": Cannot read extended mpb on %s: %s\n",
3497 devname
, strerror(errno
));
3501 check_sum
= __gen_imsm_checksum(super
->anchor
);
3502 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
3505 Name
": IMSM checksum %x != %x on %s\n",
3506 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
3511 /* FIXME the BBM log is disk specific so we cannot use this global
3512 * buffer for all disks. Ok for now since we only look at the global
3513 * bbm_log_size parameter to gate assembly
3515 super
->bbm_log
= __get_imsm_bbm_log(super
->anchor
);
3520 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
);
3523 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3527 err
= load_imsm_mpb(fd
, super
, devname
);
3530 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
3533 err
= parse_raid_devices(super
);
3538 static void __free_imsm_disk(struct dl
*d
)
3550 static void free_imsm_disks(struct intel_super
*super
)
3554 while (super
->disks
) {
3556 super
->disks
= d
->next
;
3557 __free_imsm_disk(d
);
3559 while (super
->disk_mgmt_list
) {
3560 d
= super
->disk_mgmt_list
;
3561 super
->disk_mgmt_list
= d
->next
;
3562 __free_imsm_disk(d
);
3564 while (super
->missing
) {
3566 super
->missing
= d
->next
;
3567 __free_imsm_disk(d
);
3572 /* free all the pieces hanging off of a super pointer */
3573 static void __free_imsm(struct intel_super
*super
, int free_disks
)
3575 struct intel_hba
*elem
, *next
;
3581 /* unlink capability description */
3583 if (super
->migr_rec_buf
) {
3584 free(super
->migr_rec_buf
);
3585 super
->migr_rec_buf
= NULL
;
3588 free_imsm_disks(super
);
3589 free_devlist(super
);
3593 free((void *)elem
->path
);
3601 static void free_imsm(struct intel_super
*super
)
3603 __free_imsm(super
, 1);
3607 static void free_super_imsm(struct supertype
*st
)
3609 struct intel_super
*super
= st
->sb
;
3618 static struct intel_super
*alloc_super(void)
3620 struct intel_super
*super
= malloc(sizeof(*super
));
3623 memset(super
, 0, sizeof(*super
));
3624 super
->current_vol
= -1;
3625 super
->create_offset
= ~((__u32
) 0);
3631 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
3633 static int find_intel_hba_capability(int fd
, struct intel_super
*super
, char *devname
)
3635 struct sys_dev
*hba_name
;
3638 if ((fd
< 0) || check_env("IMSM_NO_PLATFORM")) {
3643 hba_name
= find_disk_attached_hba(fd
, NULL
);
3647 Name
": %s is not attached to Intel(R) RAID controller.\n",
3651 rv
= attach_hba_to_super(super
, hba_name
);
3654 struct intel_hba
*hba
= super
->hba
;
3656 fprintf(stderr
, Name
": %s is attached to Intel(R) %s RAID "
3657 "controller (%s),\n"
3658 " but the container is assigned to Intel(R) "
3659 "%s RAID controller (",
3662 hba_name
->pci_id
? : "Err!",
3663 get_sys_dev_type(hba_name
->type
));
3666 fprintf(stderr
, "%s", hba
->pci_id
? : "Err!");
3668 fprintf(stderr
, ", ");
3672 fprintf(stderr
, ").\n"
3673 " Mixing devices attached to different controllers "
3674 "is not allowed.\n");
3676 free_sys_dev(&hba_name
);
3679 super
->orom
= find_imsm_capability(hba_name
->type
);
3680 free_sys_dev(&hba_name
);
3686 /* find_missing - helper routine for load_super_imsm_all that identifies
3687 * disks that have disappeared from the system. This routine relies on
3688 * the mpb being uptodate, which it is at load time.
3690 static int find_missing(struct intel_super
*super
)
3693 struct imsm_super
*mpb
= super
->anchor
;
3695 struct imsm_disk
*disk
;
3697 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3698 disk
= __get_imsm_disk(mpb
, i
);
3699 dl
= serial_to_dl(disk
->serial
, super
);
3703 dl
= malloc(sizeof(*dl
));
3709 dl
->devname
= strdup("missing");
3711 serialcpy(dl
->serial
, disk
->serial
);
3714 dl
->next
= super
->missing
;
3715 super
->missing
= dl
;
3722 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
3724 struct intel_disk
*idisk
= disk_list
;
3727 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
3729 idisk
= idisk
->next
;
3735 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
3736 struct intel_super
*super
,
3737 struct intel_disk
**disk_list
)
3739 struct imsm_disk
*d
= &super
->disks
->disk
;
3740 struct imsm_super
*mpb
= super
->anchor
;
3743 for (i
= 0; i
< tbl_size
; i
++) {
3744 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
3745 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
3747 if (tbl_mpb
->family_num
== mpb
->family_num
) {
3748 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
3749 dprintf("%s: mpb from %d:%d matches %d:%d\n",
3750 __func__
, super
->disks
->major
,
3751 super
->disks
->minor
,
3752 table
[i
]->disks
->major
,
3753 table
[i
]->disks
->minor
);
3757 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
3758 is_configured(d
) == is_configured(tbl_d
)) &&
3759 tbl_mpb
->generation_num
< mpb
->generation_num
) {
3760 /* current version of the mpb is a
3761 * better candidate than the one in
3762 * super_table, but copy over "cross
3763 * generational" status
3765 struct intel_disk
*idisk
;
3767 dprintf("%s: mpb from %d:%d replaces %d:%d\n",
3768 __func__
, super
->disks
->major
,
3769 super
->disks
->minor
,
3770 table
[i
]->disks
->major
,
3771 table
[i
]->disks
->minor
);
3773 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
3774 if (idisk
&& is_failed(&idisk
->disk
))
3775 tbl_d
->status
|= FAILED_DISK
;
3778 struct intel_disk
*idisk
;
3779 struct imsm_disk
*disk
;
3781 /* tbl_mpb is more up to date, but copy
3782 * over cross generational status before
3785 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
3786 if (disk
&& is_failed(disk
))
3787 d
->status
|= FAILED_DISK
;
3789 idisk
= disk_list_get(d
->serial
, *disk_list
);
3792 if (disk
&& is_configured(disk
))
3793 idisk
->disk
.status
|= CONFIGURED_DISK
;
3796 dprintf("%s: mpb from %d:%d prefer %d:%d\n",
3797 __func__
, super
->disks
->major
,
3798 super
->disks
->minor
,
3799 table
[i
]->disks
->major
,
3800 table
[i
]->disks
->minor
);
3808 table
[tbl_size
++] = super
;
3812 /* update/extend the merged list of imsm_disk records */
3813 for (j
= 0; j
< mpb
->num_disks
; j
++) {
3814 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
3815 struct intel_disk
*idisk
;
3817 idisk
= disk_list_get(disk
->serial
, *disk_list
);
3819 idisk
->disk
.status
|= disk
->status
;
3820 if (is_configured(&idisk
->disk
) ||
3821 is_failed(&idisk
->disk
))
3822 idisk
->disk
.status
&= ~(SPARE_DISK
);
3824 idisk
= calloc(1, sizeof(*idisk
));
3827 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
3828 idisk
->disk
= *disk
;
3829 idisk
->next
= *disk_list
;
3833 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
3840 static struct intel_super
*
3841 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
3844 struct imsm_super
*mpb
= super
->anchor
;
3848 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3849 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3850 struct intel_disk
*idisk
;
3852 idisk
= disk_list_get(disk
->serial
, disk_list
);
3854 if (idisk
->owner
== owner
||
3855 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
3858 dprintf("%s: '%.16s' owner %d != %d\n",
3859 __func__
, disk
->serial
, idisk
->owner
,
3862 dprintf("%s: unknown disk %x [%d]: %.16s\n",
3863 __func__
, __le32_to_cpu(mpb
->family_num
), i
,
3869 if (ok_count
== mpb
->num_disks
)
3874 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
3876 struct intel_super
*s
;
3878 for (s
= super_list
; s
; s
= s
->next
) {
3879 if (family_num
!= s
->anchor
->family_num
)
3881 fprintf(stderr
, "Conflict, offlining family %#x on '%s'\n",
3882 __le32_to_cpu(family_num
), s
->disks
->devname
);
3886 static struct intel_super
*
3887 imsm_thunderdome(struct intel_super
**super_list
, int len
)
3889 struct intel_super
*super_table
[len
];
3890 struct intel_disk
*disk_list
= NULL
;
3891 struct intel_super
*champion
, *spare
;
3892 struct intel_super
*s
, **del
;
3897 memset(super_table
, 0, sizeof(super_table
));
3898 for (s
= *super_list
; s
; s
= s
->next
)
3899 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
3901 for (i
= 0; i
< tbl_size
; i
++) {
3902 struct imsm_disk
*d
;
3903 struct intel_disk
*idisk
;
3904 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
3907 d
= &s
->disks
->disk
;
3909 /* 'd' must appear in merged disk list for its
3910 * configuration to be valid
3912 idisk
= disk_list_get(d
->serial
, disk_list
);
3913 if (idisk
&& idisk
->owner
== i
)
3914 s
= validate_members(s
, disk_list
, i
);
3919 dprintf("%s: marking family: %#x from %d:%d offline\n",
3920 __func__
, mpb
->family_num
,
3921 super_table
[i
]->disks
->major
,
3922 super_table
[i
]->disks
->minor
);
3926 /* This is where the mdadm implementation differs from the Windows
3927 * driver which has no strict concept of a container. We can only
3928 * assemble one family from a container, so when returning a prodigal
3929 * array member to this system the code will not be able to disambiguate
3930 * the container contents that should be assembled ("foreign" versus
3931 * "local"). It requires user intervention to set the orig_family_num
3932 * to a new value to establish a new container. The Windows driver in
3933 * this situation fixes up the volume name in place and manages the
3934 * foreign array as an independent entity.
3939 for (i
= 0; i
< tbl_size
; i
++) {
3940 struct intel_super
*tbl_ent
= super_table
[i
];
3946 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
3951 if (s
&& !is_spare
) {
3952 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
3954 } else if (!s
&& !is_spare
)
3967 fprintf(stderr
, "Chose family %#x on '%s', "
3968 "assemble conflicts to new container with '--update=uuid'\n",
3969 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
3971 /* collect all dl's onto 'champion', and update them to
3972 * champion's version of the status
3974 for (s
= *super_list
; s
; s
= s
->next
) {
3975 struct imsm_super
*mpb
= champion
->anchor
;
3976 struct dl
*dl
= s
->disks
;
3981 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3982 struct imsm_disk
*disk
;
3984 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
3987 /* only set index on disks that are a member of
3988 * a populated contianer, i.e. one with
3991 if (is_failed(&dl
->disk
))
3993 else if (is_spare(&dl
->disk
))
3999 if (i
>= mpb
->num_disks
) {
4000 struct intel_disk
*idisk
;
4002 idisk
= disk_list_get(dl
->serial
, disk_list
);
4003 if (idisk
&& is_spare(&idisk
->disk
) &&
4004 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
4012 dl
->next
= champion
->disks
;
4013 champion
->disks
= dl
;
4017 /* delete 'champion' from super_list */
4018 for (del
= super_list
; *del
; ) {
4019 if (*del
== champion
) {
4020 *del
= (*del
)->next
;
4023 del
= &(*del
)->next
;
4025 champion
->next
= NULL
;
4029 struct intel_disk
*idisk
= disk_list
;
4031 disk_list
= disk_list
->next
;
4040 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
);
4041 static int get_super_block(struct intel_super
**super_list
, int devnum
, char *devname
,
4042 int major
, int minor
, int keep_fd
);
4044 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4045 int *max
, int keep_fd
);
4048 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
4049 char *devname
, struct md_list
*devlist
,
4052 struct intel_super
*super_list
= NULL
;
4053 struct intel_super
*super
= NULL
;
4058 /* 'fd' is an opened container */
4059 err
= get_sra_super_block(fd
, &super_list
, devname
, &i
, keep_fd
);
4061 /* get super block from devlist devices */
4062 err
= get_devlist_super_block(devlist
, &super_list
, &i
, keep_fd
);
4065 /* all mpbs enter, maybe one leaves */
4066 super
= imsm_thunderdome(&super_list
, i
);
4072 if (find_missing(super
) != 0) {
4078 /* load migration record */
4079 err
= load_imsm_migr_rec(super
, NULL
);
4081 /* migration is in progress,
4082 * but migr_rec cannot be loaded,
4088 /* Check migration compatibility */
4089 if ((err
== 0) && (check_mpb_migr_compatibility(super
) != 0)) {
4090 fprintf(stderr
, Name
": Unsupported migration detected");
4092 fprintf(stderr
, " on %s\n", devname
);
4094 fprintf(stderr
, " (IMSM).\n");
4103 while (super_list
) {
4104 struct intel_super
*s
= super_list
;
4106 super_list
= super_list
->next
;
4116 st
->container_dev
= fd2devnum(fd
);
4118 st
->container_dev
= NoMdDev
;
4119 if (err
== 0 && st
->ss
== NULL
) {
4120 st
->ss
= &super_imsm
;
4121 st
->minor_version
= 0;
4122 st
->max_devs
= IMSM_MAX_DEVICES
;
4129 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4130 int *max
, int keep_fd
)
4132 struct md_list
*tmpdev
;
4136 for (i
= 0, tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
4137 if (tmpdev
->used
!= 1)
4139 if (tmpdev
->container
== 1) {
4141 int fd
= dev_open(tmpdev
->devname
, O_RDONLY
|O_EXCL
);
4143 fprintf(stderr
, Name
": cannot open device %s: %s\n",
4144 tmpdev
->devname
, strerror(errno
));
4148 err
= get_sra_super_block(fd
, super_list
,
4149 tmpdev
->devname
, &lmax
,
4158 int major
= major(tmpdev
->st_rdev
);
4159 int minor
= minor(tmpdev
->st_rdev
);
4160 err
= get_super_block(super_list
,
4177 static int get_super_block(struct intel_super
**super_list
, int devnum
, char *devname
,
4178 int major
, int minor
, int keep_fd
)
4180 struct intel_super
*s
= NULL
;
4193 sprintf(nm
, "%d:%d", major
, minor
);
4194 dfd
= dev_open(nm
, O_RDWR
);
4200 rv
= find_intel_hba_capability(dfd
, s
, devname
);
4201 /* no orom/efi or non-intel hba of the disk */
4207 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4209 /* retry the load if we might have raced against mdmon */
4210 if (err
== 3 && (devnum
!= -1) && mdmon_running(devnum
))
4211 for (retry
= 0; retry
< 3; retry
++) {
4213 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4219 s
->next
= *super_list
;
4227 if ((dfd
>= 0) && (!keep_fd
))
4234 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
)
4241 sra
= sysfs_read(fd
, 0, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
4245 if (sra
->array
.major_version
!= -1 ||
4246 sra
->array
.minor_version
!= -2 ||
4247 strcmp(sra
->text_version
, "imsm") != 0) {
4252 devnum
= fd2devnum(fd
);
4253 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
4254 if (get_super_block(super_list
, devnum
, devname
,
4255 sd
->disk
.major
, sd
->disk
.minor
, keep_fd
) != 0) {
4266 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
4268 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
, NULL
, 1);
4272 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
4274 struct intel_super
*super
;
4277 if (test_partition(fd
))
4278 /* IMSM not allowed on partitions */
4281 free_super_imsm(st
);
4283 super
= alloc_super();
4286 Name
": malloc of %zu failed.\n",
4290 /* Load hba and capabilities if they exist.
4291 * But do not preclude loading metadata in case capabilities or hba are
4292 * non-compliant and ignore_hw_compat is set.
4294 rv
= find_intel_hba_capability(fd
, super
, devname
);
4295 /* no orom/efi or non-intel hba of the disk */
4296 if ((rv
!= 0) && (st
->ignore_hw_compat
== 0)) {
4299 Name
": No OROM/EFI properties for %s\n", devname
);
4303 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
4308 Name
": Failed to load all information "
4309 "sections on %s\n", devname
);
4315 if (st
->ss
== NULL
) {
4316 st
->ss
= &super_imsm
;
4317 st
->minor_version
= 0;
4318 st
->max_devs
= IMSM_MAX_DEVICES
;
4321 /* load migration record */
4322 if (load_imsm_migr_rec(super
, NULL
) == 0) {
4323 /* Check for unsupported migration features */
4324 if (check_mpb_migr_compatibility(super
) != 0) {
4326 Name
": Unsupported migration detected");
4328 fprintf(stderr
, " on %s\n", devname
);
4330 fprintf(stderr
, " (IMSM).\n");
4338 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
4340 if (info
->level
== 1)
4342 return info
->chunk_size
>> 9;
4345 static __u32
info_to_num_data_stripes(mdu_array_info_t
*info
, int num_domains
)
4349 num_stripes
= (info
->size
* 2) / info_to_blocks_per_strip(info
);
4350 num_stripes
/= num_domains
;
4355 static __u32
info_to_blocks_per_member(mdu_array_info_t
*info
)
4357 if (info
->level
== 1)
4358 return info
->size
* 2;
4360 return (info
->size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
4363 static void imsm_update_version_info(struct intel_super
*super
)
4365 /* update the version and attributes */
4366 struct imsm_super
*mpb
= super
->anchor
;
4368 struct imsm_dev
*dev
;
4369 struct imsm_map
*map
;
4372 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4373 dev
= get_imsm_dev(super
, i
);
4374 map
= get_imsm_map(dev
, MAP_0
);
4375 if (__le32_to_cpu(dev
->size_high
) > 0)
4376 mpb
->attributes
|= MPB_ATTRIB_2TB
;
4378 /* FIXME detect when an array spans a port multiplier */
4380 mpb
->attributes
|= MPB_ATTRIB_PM
;
4383 if (mpb
->num_raid_devs
> 1 ||
4384 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
4385 version
= MPB_VERSION_ATTRIBS
;
4386 switch (get_imsm_raid_level(map
)) {
4387 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
4388 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
4389 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
4390 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
4393 if (map
->num_members
>= 5)
4394 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
4395 else if (dev
->status
== DEV_CLONE_N_GO
)
4396 version
= MPB_VERSION_CNG
;
4397 else if (get_imsm_raid_level(map
) == 5)
4398 version
= MPB_VERSION_RAID5
;
4399 else if (map
->num_members
>= 3)
4400 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
4401 else if (get_imsm_raid_level(map
) == 1)
4402 version
= MPB_VERSION_RAID1
;
4404 version
= MPB_VERSION_RAID0
;
4406 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
4410 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
4412 struct imsm_super
*mpb
= super
->anchor
;
4413 char *reason
= NULL
;
4416 if (strlen(name
) > MAX_RAID_SERIAL_LEN
)
4417 reason
= "must be 16 characters or less";
4419 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4420 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
4422 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
4423 reason
= "already exists";
4428 if (reason
&& !quiet
)
4429 fprintf(stderr
, Name
": imsm volume name %s\n", reason
);
4434 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
4435 unsigned long long size
, char *name
,
4436 char *homehost
, int *uuid
)
4438 /* We are creating a volume inside a pre-existing container.
4439 * so st->sb is already set.
4441 struct intel_super
*super
= st
->sb
;
4442 struct imsm_super
*mpb
= super
->anchor
;
4443 struct intel_dev
*dv
;
4444 struct imsm_dev
*dev
;
4445 struct imsm_vol
*vol
;
4446 struct imsm_map
*map
;
4447 int idx
= mpb
->num_raid_devs
;
4449 unsigned long long array_blocks
;
4450 size_t size_old
, size_new
;
4451 __u32 num_data_stripes
;
4453 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
4454 fprintf(stderr
, Name
": This imsm-container already has the "
4455 "maximum of %d volumes\n", super
->orom
->vpa
);
4459 /* ensure the mpb is large enough for the new data */
4460 size_old
= __le32_to_cpu(mpb
->mpb_size
);
4461 size_new
= disks_to_mpb_size(info
->nr_disks
);
4462 if (size_new
> size_old
) {
4464 size_t size_round
= ROUND_UP(size_new
, 512);
4466 if (posix_memalign(&mpb_new
, 512, size_round
) != 0) {
4467 fprintf(stderr
, Name
": could not allocate new mpb\n");
4470 if (posix_memalign(&super
->migr_rec_buf
, 512,
4471 MIGR_REC_BUF_SIZE
) != 0) {
4472 fprintf(stderr
, Name
4473 ": %s could not allocate migr_rec buffer\n",
4480 memcpy(mpb_new
, mpb
, size_old
);
4483 super
->anchor
= mpb_new
;
4484 mpb
->mpb_size
= __cpu_to_le32(size_new
);
4485 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
4487 super
->current_vol
= idx
;
4489 /* handle 'failed_disks' by either:
4490 * a) create dummy disk entries in the table if this the first
4491 * volume in the array. We add them here as this is the only
4492 * opportunity to add them. add_to_super_imsm_volume()
4493 * handles the non-failed disks and continues incrementing
4495 * b) validate that 'failed_disks' matches the current number
4496 * of missing disks if the container is populated
4498 if (super
->current_vol
== 0) {
4500 for (i
= 0; i
< info
->failed_disks
; i
++) {
4501 struct imsm_disk
*disk
;
4504 disk
= __get_imsm_disk(mpb
, i
);
4505 disk
->status
= CONFIGURED_DISK
| FAILED_DISK
;
4506 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
4507 snprintf((char *) disk
->serial
, MAX_RAID_SERIAL_LEN
,
4510 find_missing(super
);
4515 for (d
= super
->missing
; d
; d
= d
->next
)
4517 if (info
->failed_disks
> missing
) {
4518 fprintf(stderr
, Name
": unable to add 'missing' disk to container\n");
4523 if (!check_name(super
, name
, 0))
4525 dv
= malloc(sizeof(*dv
));
4527 fprintf(stderr
, Name
": failed to allocate device list entry\n");
4530 dev
= calloc(1, sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
4533 fprintf(stderr
, Name
": could not allocate raid device\n");
4537 strncpy((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
4538 if (info
->level
== 1)
4539 array_blocks
= info_to_blocks_per_member(info
);
4541 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
4542 info
->layout
, info
->chunk_size
,
4544 /* round array size down to closest MB */
4545 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
4547 dev
->size_low
= __cpu_to_le32((__u32
) array_blocks
);
4548 dev
->size_high
= __cpu_to_le32((__u32
) (array_blocks
>> 32));
4549 dev
->status
= (DEV_READ_COALESCING
| DEV_WRITE_COALESCING
);
4551 vol
->migr_state
= 0;
4552 set_migr_type(dev
, MIGR_INIT
);
4553 vol
->dirty
= !info
->state
;
4554 vol
->curr_migr_unit
= 0;
4555 map
= get_imsm_map(dev
, MAP_0
);
4556 map
->pba_of_lba0
= __cpu_to_le32(super
->create_offset
);
4557 map
->blocks_per_member
= __cpu_to_le32(info_to_blocks_per_member(info
));
4558 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
4559 map
->failed_disk_num
= ~0;
4560 if (info
->level
> 0)
4561 map
->map_state
= IMSM_T_STATE_UNINITIALIZED
;
4563 map
->map_state
= info
->failed_disks
? IMSM_T_STATE_FAILED
:
4564 IMSM_T_STATE_NORMAL
;
4567 if (info
->level
== 1 && info
->raid_disks
> 2) {
4570 fprintf(stderr
, Name
": imsm does not support more than 2 disks"
4571 "in a raid1 volume\n");
4575 map
->raid_level
= info
->level
;
4576 if (info
->level
== 10) {
4577 map
->raid_level
= 1;
4578 map
->num_domains
= info
->raid_disks
/ 2;
4579 } else if (info
->level
== 1)
4580 map
->num_domains
= info
->raid_disks
;
4582 map
->num_domains
= 1;
4584 num_data_stripes
= info_to_num_data_stripes(info
, map
->num_domains
);
4585 map
->num_data_stripes
= __cpu_to_le32(num_data_stripes
);
4587 map
->num_members
= info
->raid_disks
;
4588 for (i
= 0; i
< map
->num_members
; i
++) {
4589 /* initialized in add_to_super */
4590 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
4592 mpb
->num_raid_devs
++;
4595 dv
->index
= super
->current_vol
;
4596 dv
->next
= super
->devlist
;
4597 super
->devlist
= dv
;
4599 imsm_update_version_info(super
);
4604 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
4605 unsigned long long size
, char *name
,
4606 char *homehost
, int *uuid
)
4608 /* This is primarily called by Create when creating a new array.
4609 * We will then get add_to_super called for each component, and then
4610 * write_init_super called to write it out to each device.
4611 * For IMSM, Create can create on fresh devices or on a pre-existing
4613 * To create on a pre-existing array a different method will be called.
4614 * This one is just for fresh drives.
4616 struct intel_super
*super
;
4617 struct imsm_super
*mpb
;
4622 return init_super_imsm_volume(st
, info
, size
, name
, homehost
, uuid
);
4625 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
4629 super
= alloc_super();
4630 if (super
&& posix_memalign(&super
->buf
, 512, mpb_size
) != 0) {
4635 fprintf(stderr
, Name
4636 ": %s could not allocate superblock\n", __func__
);
4639 if (posix_memalign(&super
->migr_rec_buf
, 512, MIGR_REC_BUF_SIZE
) != 0) {
4640 fprintf(stderr
, Name
4641 ": %s could not allocate migr_rec buffer\n", __func__
);
4646 memset(super
->buf
, 0, mpb_size
);
4648 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
4652 /* zeroing superblock */
4656 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
4658 version
= (char *) mpb
->sig
;
4659 strcpy(version
, MPB_SIGNATURE
);
4660 version
+= strlen(MPB_SIGNATURE
);
4661 strcpy(version
, MPB_VERSION_RAID0
);
4667 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
4668 int fd
, char *devname
)
4670 struct intel_super
*super
= st
->sb
;
4671 struct imsm_super
*mpb
= super
->anchor
;
4672 struct imsm_disk
*_disk
;
4673 struct imsm_dev
*dev
;
4674 struct imsm_map
*map
;
4678 dev
= get_imsm_dev(super
, super
->current_vol
);
4679 map
= get_imsm_map(dev
, MAP_0
);
4681 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
4682 fprintf(stderr
, Name
": %s: Cannot add spare devices to IMSM volume\n",
4688 /* we're doing autolayout so grab the pre-marked (in
4689 * validate_geometry) raid_disk
4691 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4692 if (dl
->raiddisk
== dk
->raid_disk
)
4695 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4696 if (dl
->major
== dk
->major
&&
4697 dl
->minor
== dk
->minor
)
4702 fprintf(stderr
, Name
": %s is not a member of the same container\n", devname
);
4706 /* add a pristine spare to the metadata */
4707 if (dl
->index
< 0) {
4708 dl
->index
= super
->anchor
->num_disks
;
4709 super
->anchor
->num_disks
++;
4711 /* Check the device has not already been added */
4712 slot
= get_imsm_disk_slot(map
, dl
->index
);
4714 (get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
) & IMSM_ORD_REBUILD
) == 0) {
4715 fprintf(stderr
, Name
": %s has been included in this array twice\n",
4719 set_imsm_ord_tbl_ent(map
, dk
->raid_disk
, dl
->index
);
4720 dl
->disk
.status
= CONFIGURED_DISK
;
4722 /* update size of 'missing' disks to be at least as large as the
4723 * largest acitve member (we only have dummy missing disks when
4724 * creating the first volume)
4726 if (super
->current_vol
== 0) {
4727 for (df
= super
->missing
; df
; df
= df
->next
) {
4728 if (dl
->disk
.total_blocks
> df
->disk
.total_blocks
)
4729 df
->disk
.total_blocks
= dl
->disk
.total_blocks
;
4730 _disk
= __get_imsm_disk(mpb
, df
->index
);
4735 /* refresh unset/failed slots to point to valid 'missing' entries */
4736 for (df
= super
->missing
; df
; df
= df
->next
)
4737 for (slot
= 0; slot
< mpb
->num_disks
; slot
++) {
4738 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
4740 if ((ord
& IMSM_ORD_REBUILD
) == 0)
4742 set_imsm_ord_tbl_ent(map
, slot
, df
->index
| IMSM_ORD_REBUILD
);
4743 if (is_gen_migration(dev
)) {
4744 struct imsm_map
*map2
= get_imsm_map(dev
,
4746 int slot2
= get_imsm_disk_slot(map2
, df
->index
);
4747 if ((slot2
< map2
->num_members
) &&
4749 __u32 ord2
= get_imsm_ord_tbl_ent(dev
,
4752 if ((unsigned)df
->index
==
4754 set_imsm_ord_tbl_ent(map2
,
4760 dprintf("set slot:%d to missing disk:%d\n", slot
, df
->index
);
4764 /* if we are creating the first raid device update the family number */
4765 if (super
->current_vol
== 0) {
4767 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
4769 _disk
= __get_imsm_disk(mpb
, dl
->index
);
4770 if (!_dev
|| !_disk
) {
4771 fprintf(stderr
, Name
": BUG mpb setup error\n");
4777 sum
+= __gen_imsm_checksum(mpb
);
4778 mpb
->family_num
= __cpu_to_le32(sum
);
4779 mpb
->orig_family_num
= mpb
->family_num
;
4781 super
->current_disk
= dl
;
4786 * Function marks disk as spare and restores disk serial
4787 * in case it was previously marked as failed by takeover operation
4789 * -1 : critical error
4790 * 0 : disk is marked as spare but serial is not set
4793 int mark_spare(struct dl
*disk
)
4795 __u8 serial
[MAX_RAID_SERIAL_LEN
];
4802 if (!imsm_read_serial(disk
->fd
, NULL
, serial
)) {
4803 /* Restore disk serial number, because takeover marks disk
4804 * as failed and adds to serial ':0' before it becomes
4807 serialcpy(disk
->serial
, serial
);
4808 serialcpy(disk
->disk
.serial
, serial
);
4811 disk
->disk
.status
= SPARE_DISK
;
4817 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
4818 int fd
, char *devname
)
4820 struct intel_super
*super
= st
->sb
;
4822 unsigned long long size
;
4827 /* If we are on an RAID enabled platform check that the disk is
4828 * attached to the raid controller.
4829 * We do not need to test disks attachment for container based additions,
4830 * they shall be already tested when container was created/assembled.
4832 rv
= find_intel_hba_capability(fd
, super
, devname
);
4833 /* no orom/efi or non-intel hba of the disk */
4835 dprintf("capability: %p fd: %d ret: %d\n",
4836 super
->orom
, fd
, rv
);
4840 if (super
->current_vol
>= 0)
4841 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
4844 dd
= malloc(sizeof(*dd
));
4847 Name
": malloc failed %s:%d.\n", __func__
, __LINE__
);
4850 memset(dd
, 0, sizeof(*dd
));
4851 dd
->major
= major(stb
.st_rdev
);
4852 dd
->minor
= minor(stb
.st_rdev
);
4853 dd
->devname
= devname
? strdup(devname
) : NULL
;
4856 dd
->action
= DISK_ADD
;
4857 rv
= imsm_read_serial(fd
, devname
, dd
->serial
);
4860 Name
": failed to retrieve scsi serial, aborting\n");
4865 get_dev_size(fd
, NULL
, &size
);
4867 serialcpy(dd
->disk
.serial
, dd
->serial
);
4868 dd
->disk
.total_blocks
= __cpu_to_le32(size
);
4870 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
4871 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
4873 dd
->disk
.scsi_id
= __cpu_to_le32(0);
4875 if (st
->update_tail
) {
4876 dd
->next
= super
->disk_mgmt_list
;
4877 super
->disk_mgmt_list
= dd
;
4879 dd
->next
= super
->disks
;
4881 super
->updates_pending
++;
4888 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
4890 struct intel_super
*super
= st
->sb
;
4893 /* remove from super works only in mdmon - for communication
4894 * manager - monitor. Check if communication memory buffer
4897 if (!st
->update_tail
) {
4899 Name
": %s shall be used in mdmon context only"
4900 "(line %d).\n", __func__
, __LINE__
);
4903 dd
= malloc(sizeof(*dd
));
4906 Name
": malloc failed %s:%d.\n", __func__
, __LINE__
);
4909 memset(dd
, 0, sizeof(*dd
));
4910 dd
->major
= dk
->major
;
4911 dd
->minor
= dk
->minor
;
4914 dd
->action
= DISK_REMOVE
;
4916 dd
->next
= super
->disk_mgmt_list
;
4917 super
->disk_mgmt_list
= dd
;
4923 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
4927 struct imsm_super anchor
;
4928 } spare_record
__attribute__ ((aligned(512)));
4930 /* spare records have their own family number and do not have any defined raid
4933 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
4935 struct imsm_super
*mpb
= super
->anchor
;
4936 struct imsm_super
*spare
= &spare_record
.anchor
;
4940 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
)),
4941 spare
->generation_num
= __cpu_to_le32(1UL),
4942 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
4943 spare
->num_disks
= 1,
4944 spare
->num_raid_devs
= 0,
4945 spare
->cache_size
= mpb
->cache_size
,
4946 spare
->pwr_cycle_count
= __cpu_to_le32(1),
4948 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
4949 MPB_SIGNATURE MPB_VERSION_RAID0
);
4951 for (d
= super
->disks
; d
; d
= d
->next
) {
4955 spare
->disk
[0] = d
->disk
;
4956 sum
= __gen_imsm_checksum(spare
);
4957 spare
->family_num
= __cpu_to_le32(sum
);
4958 spare
->orig_family_num
= 0;
4959 sum
= __gen_imsm_checksum(spare
);
4960 spare
->check_sum
= __cpu_to_le32(sum
);
4962 if (store_imsm_mpb(d
->fd
, spare
)) {
4963 fprintf(stderr
, "%s: failed for device %d:%d %s\n",
4964 __func__
, d
->major
, d
->minor
, strerror(errno
));
4976 static int write_super_imsm(struct supertype
*st
, int doclose
)
4978 struct intel_super
*super
= st
->sb
;
4979 struct imsm_super
*mpb
= super
->anchor
;
4985 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
4987 int clear_migration_record
= 1;
4989 /* 'generation' is incremented everytime the metadata is written */
4990 generation
= __le32_to_cpu(mpb
->generation_num
);
4992 mpb
->generation_num
= __cpu_to_le32(generation
);
4994 /* fix up cases where previous mdadm releases failed to set
4997 if (mpb
->orig_family_num
== 0)
4998 mpb
->orig_family_num
= mpb
->family_num
;
5000 for (d
= super
->disks
; d
; d
= d
->next
) {
5004 mpb
->disk
[d
->index
] = d
->disk
;
5008 for (d
= super
->missing
; d
; d
= d
->next
) {
5009 mpb
->disk
[d
->index
] = d
->disk
;
5012 mpb
->num_disks
= num_disks
;
5013 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
5015 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5016 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
5017 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
5019 imsm_copy_dev(dev
, dev2
);
5020 mpb_size
+= sizeof_imsm_dev(dev
, 0);
5022 if (is_gen_migration(dev2
))
5023 clear_migration_record
= 0;
5025 mpb_size
+= __le32_to_cpu(mpb
->bbm_log_size
);
5026 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5028 /* recalculate checksum */
5029 sum
= __gen_imsm_checksum(mpb
);
5030 mpb
->check_sum
= __cpu_to_le32(sum
);
5032 if (clear_migration_record
)
5033 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SIZE
);
5035 /* write the mpb for disks that compose raid devices */
5036 for (d
= super
->disks
; d
; d
= d
->next
) {
5037 if (d
->index
< 0 || is_failed(&d
->disk
))
5039 if (store_imsm_mpb(d
->fd
, mpb
))
5040 fprintf(stderr
, "%s: failed for device %d:%d (fd: %d)%s\n",
5041 __func__
, d
->major
, d
->minor
, d
->fd
, strerror(errno
));
5043 if (clear_migration_record
) {
5044 unsigned long long dsize
;
5046 get_dev_size(d
->fd
, NULL
, &dsize
);
5047 if (lseek64(d
->fd
, dsize
- 512, SEEK_SET
) >= 0) {
5048 if (write(d
->fd
, super
->migr_rec_buf
,
5049 MIGR_REC_BUF_SIZE
) != MIGR_REC_BUF_SIZE
)
5050 perror("Write migr_rec failed");
5060 return write_super_imsm_spares(super
, doclose
);
5066 static int create_array(struct supertype
*st
, int dev_idx
)
5069 struct imsm_update_create_array
*u
;
5070 struct intel_super
*super
= st
->sb
;
5071 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
5072 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
5073 struct disk_info
*inf
;
5074 struct imsm_disk
*disk
;
5077 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
5078 sizeof(*inf
) * map
->num_members
;
5081 fprintf(stderr
, "%s: failed to allocate update buffer\n",
5086 u
->type
= update_create_array
;
5087 u
->dev_idx
= dev_idx
;
5088 imsm_copy_dev(&u
->dev
, dev
);
5089 inf
= get_disk_info(u
);
5090 for (i
= 0; i
< map
->num_members
; i
++) {
5091 int idx
= get_imsm_disk_idx(dev
, i
, MAP_X
);
5093 disk
= get_imsm_disk(super
, idx
);
5094 serialcpy(inf
[i
].serial
, disk
->serial
);
5096 append_metadata_update(st
, u
, len
);
5101 static int mgmt_disk(struct supertype
*st
)
5103 struct intel_super
*super
= st
->sb
;
5105 struct imsm_update_add_remove_disk
*u
;
5107 if (!super
->disk_mgmt_list
)
5113 fprintf(stderr
, "%s: failed to allocate update buffer\n",
5118 u
->type
= update_add_remove_disk
;
5119 append_metadata_update(st
, u
, len
);
5124 static int write_init_super_imsm(struct supertype
*st
)
5126 struct intel_super
*super
= st
->sb
;
5127 int current_vol
= super
->current_vol
;
5129 /* we are done with current_vol reset it to point st at the container */
5130 super
->current_vol
= -1;
5132 if (st
->update_tail
) {
5133 /* queue the recently created array / added disk
5134 * as a metadata update */
5137 /* determine if we are creating a volume or adding a disk */
5138 if (current_vol
< 0) {
5139 /* in the mgmt (add/remove) disk case we are running
5140 * in mdmon context, so don't close fd's
5142 return mgmt_disk(st
);
5144 rv
= create_array(st
, current_vol
);
5149 for (d
= super
->disks
; d
; d
= d
->next
)
5150 Kill(d
->devname
, NULL
, 0, 1, 1);
5151 return write_super_imsm(st
, 1);
5156 static int store_super_imsm(struct supertype
*st
, int fd
)
5158 struct intel_super
*super
= st
->sb
;
5159 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
5165 return store_imsm_mpb(fd
, mpb
);
5171 static int imsm_bbm_log_size(struct imsm_super
*mpb
)
5173 return __le32_to_cpu(mpb
->bbm_log_size
);
5177 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
5178 int layout
, int raiddisks
, int chunk
,
5179 unsigned long long size
, char *dev
,
5180 unsigned long long *freesize
,
5184 unsigned long long ldsize
;
5185 struct intel_super
*super
=NULL
;
5188 if (level
!= LEVEL_CONTAINER
)
5193 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
5196 fprintf(stderr
, Name
": imsm: Cannot open %s: %s\n",
5197 dev
, strerror(errno
));
5200 if (!get_dev_size(fd
, dev
, &ldsize
)) {
5205 /* capabilities retrieve could be possible
5206 * note that there is no fd for the disks in array.
5208 super
= alloc_super();
5211 Name
": malloc of %zu failed.\n",
5217 rv
= find_intel_hba_capability(fd
, super
, verbose
? dev
: NULL
);
5221 fd2devname(fd
, str
);
5222 dprintf("validate_geometry_imsm_container: fd: %d %s orom: %p rv: %d raiddisk: %d\n",
5223 fd
, str
, super
->orom
, rv
, raiddisks
);
5225 /* no orom/efi or non-intel hba of the disk */
5231 if (super
->orom
&& raiddisks
> super
->orom
->tds
) {
5233 fprintf(stderr
, Name
": %d exceeds maximum number of"
5234 " platform supported disks: %d\n",
5235 raiddisks
, super
->orom
->tds
);
5241 *freesize
= avail_size_imsm(st
, ldsize
>> 9);
5247 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
5249 const unsigned long long base_start
= e
[*idx
].start
;
5250 unsigned long long end
= base_start
+ e
[*idx
].size
;
5253 if (base_start
== end
)
5257 for (i
= *idx
; i
< num_extents
; i
++) {
5258 /* extend overlapping extents */
5259 if (e
[i
].start
>= base_start
&&
5260 e
[i
].start
<= end
) {
5263 if (e
[i
].start
+ e
[i
].size
> end
)
5264 end
= e
[i
].start
+ e
[i
].size
;
5265 } else if (e
[i
].start
> end
) {
5271 return end
- base_start
;
5274 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
5276 /* build a composite disk with all known extents and generate a new
5277 * 'maxsize' given the "all disks in an array must share a common start
5278 * offset" constraint
5280 struct extent
*e
= calloc(sum_extents
, sizeof(*e
));
5284 unsigned long long pos
;
5285 unsigned long long start
= 0;
5286 unsigned long long maxsize
;
5287 unsigned long reserve
;
5292 /* coalesce and sort all extents. also, check to see if we need to
5293 * reserve space between member arrays
5296 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5299 for (i
= 0; i
< dl
->extent_cnt
; i
++)
5302 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
5307 while (i
< sum_extents
) {
5308 e
[j
].start
= e
[i
].start
;
5309 e
[j
].size
= find_size(e
, &i
, sum_extents
);
5311 if (e
[j
-1].size
== 0)
5320 unsigned long long esize
;
5322 esize
= e
[i
].start
- pos
;
5323 if (esize
>= maxsize
) {
5328 pos
= e
[i
].start
+ e
[i
].size
;
5330 } while (e
[i
-1].size
);
5336 /* FIXME assumes volume at offset 0 is the first volume in a
5339 if (start_extent
> 0)
5340 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
5344 if (maxsize
< reserve
)
5347 super
->create_offset
= ~((__u32
) 0);
5348 if (start
+ reserve
> super
->create_offset
)
5349 return 0; /* start overflows create_offset */
5350 super
->create_offset
= start
+ reserve
;
5352 return maxsize
- reserve
;
5355 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
5357 if (level
< 0 || level
== 6 || level
== 4)
5360 /* if we have an orom prevent invalid raid levels */
5363 case 0: return imsm_orom_has_raid0(orom
);
5366 return imsm_orom_has_raid1e(orom
);
5367 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
5368 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
5369 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
5372 return 1; /* not on an Intel RAID platform so anything goes */
5379 active_arrays_by_format(char *name
, char* hba
, struct md_list
**devlist
,
5380 int dpa
, int verbose
)
5382 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
5383 struct mdstat_ent
*memb
= NULL
;
5386 struct md_list
*dv
= NULL
;
5389 for (memb
= mdstat
; memb
; memb
= memb
->next
) {
5390 if (memb
->metadata_version
&&
5391 (strncmp(memb
->metadata_version
, "external:", 9) == 0) &&
5392 (strcmp(&memb
->metadata_version
[9], name
) == 0) &&
5393 !is_subarray(memb
->metadata_version
+9) &&
5395 struct dev_member
*dev
= memb
->members
;
5397 while(dev
&& (fd
< 0)) {
5398 char *path
= malloc(strlen(dev
->name
) + strlen("/dev/") + 1);
5400 num
= sprintf(path
, "%s%s", "/dev/", dev
->name
);
5402 fd
= open(path
, O_RDONLY
, 0);
5403 if ((num
<= 0) || (fd
< 0)) {
5404 pr_vrb(": Cannot open %s: %s\n",
5405 dev
->name
, strerror(errno
));
5412 if ((fd
>= 0) && disk_attached_to_hba(fd
, hba
)) {
5413 struct mdstat_ent
*vol
;
5414 for (vol
= mdstat
; vol
; vol
= vol
->next
) {
5415 if ((vol
->active
> 0) &&
5416 vol
->metadata_version
&&
5417 is_container_member(vol
, memb
->dev
)) {
5422 if (*devlist
&& (found
< dpa
)) {
5423 dv
= calloc(1, sizeof(*dv
));
5425 fprintf(stderr
, Name
": calloc failed\n");
5427 dv
->devname
= malloc(strlen(memb
->dev
) + strlen("/dev/") + 1);
5428 if (dv
->devname
!= NULL
) {
5429 sprintf(dv
->devname
, "%s%s", "/dev/", memb
->dev
);
5432 dv
->next
= *devlist
;
5443 free_mdstat(mdstat
);
5448 static struct md_list
*
5449 get_loop_devices(void)
5452 struct md_list
*devlist
= NULL
;
5453 struct md_list
*dv
= NULL
;
5455 for(i
= 0; i
< 12; i
++) {
5456 dv
= calloc(1, sizeof(*dv
));
5458 fprintf(stderr
, Name
": calloc failed\n");
5461 dv
->devname
= malloc(40);
5462 if (dv
->devname
== NULL
) {
5463 fprintf(stderr
, Name
": malloc failed\n");
5467 sprintf(dv
->devname
, "/dev/loop%d", i
);
5475 static struct md_list
*
5476 get_devices(const char *hba_path
)
5478 struct md_list
*devlist
= NULL
;
5479 struct md_list
*dv
= NULL
;
5485 devlist
= get_loop_devices();
5488 /* scroll through /sys/dev/block looking for devices attached to
5491 dir
= opendir("/sys/dev/block");
5492 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
5497 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
5499 path
= devt_to_devpath(makedev(major
, minor
));
5502 if (!path_attached_to_hba(path
, hba_path
)) {
5509 fd
= dev_open(ent
->d_name
, O_RDONLY
);
5511 fd2devname(fd
, buf
);
5514 fprintf(stderr
, Name
": cannot open device: %s\n",
5520 dv
= calloc(1, sizeof(*dv
));
5522 fprintf(stderr
, Name
": malloc failed\n");
5526 dv
->devname
= strdup(buf
);
5527 if (dv
->devname
== NULL
) {
5528 fprintf(stderr
, Name
": malloc failed\n");
5539 devlist
= devlist
->next
;
5548 count_volumes_list(struct md_list
*devlist
, char *homehost
,
5549 int verbose
, int *found
)
5551 struct md_list
*tmpdev
;
5553 struct supertype
*st
= NULL
;
5555 /* first walk the list of devices to find a consistent set
5556 * that match the criterea, if that is possible.
5557 * We flag the ones we like with 'used'.
5560 st
= match_metadata_desc_imsm("imsm");
5562 pr_vrb(": cannot allocate memory for imsm supertype\n");
5566 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5567 char *devname
= tmpdev
->devname
;
5569 struct supertype
*tst
;
5571 if (tmpdev
->used
> 1)
5573 tst
= dup_super(st
);
5575 pr_vrb(": cannot allocate memory for imsm supertype\n");
5578 tmpdev
->container
= 0;
5579 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
5581 dprintf(": cannot open device %s: %s\n",
5582 devname
, strerror(errno
));
5584 } else if (fstat(dfd
, &stb
)< 0) {
5586 dprintf(": fstat failed for %s: %s\n",
5587 devname
, strerror(errno
));
5589 } else if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
5590 dprintf(": %s is not a block device.\n",
5593 } else if (must_be_container(dfd
)) {
5594 struct supertype
*cst
;
5595 cst
= super_by_fd(dfd
, NULL
);
5597 dprintf(": cannot recognize container type %s\n",
5600 } else if (tst
->ss
!= st
->ss
) {
5601 dprintf(": non-imsm container - ignore it: %s\n",
5604 } else if (!tst
->ss
->load_container
||
5605 tst
->ss
->load_container(tst
, dfd
, NULL
))
5608 tmpdev
->container
= 1;
5611 cst
->ss
->free_super(cst
);
5613 tmpdev
->st_rdev
= stb
.st_rdev
;
5614 if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
5615 dprintf(": no RAID superblock on %s\n",
5618 } else if (tst
->ss
->compare_super
== NULL
) {
5619 dprintf(": Cannot assemble %s metadata on %s\n",
5620 tst
->ss
->name
, devname
);
5626 if (tmpdev
->used
== 2 || tmpdev
->used
== 4) {
5627 /* Ignore unrecognised devices during auto-assembly */
5632 tst
->ss
->getinfo_super(tst
, &info
, NULL
);
5634 if (st
->minor_version
== -1)
5635 st
->minor_version
= tst
->minor_version
;
5637 if (memcmp(info
.uuid
, uuid_zero
,
5638 sizeof(int[4])) == 0) {
5639 /* this is a floating spare. It cannot define
5640 * an array unless there are no more arrays of
5641 * this type to be found. It can be included
5642 * in an array of this type though.
5648 if (st
->ss
!= tst
->ss
||
5649 st
->minor_version
!= tst
->minor_version
||
5650 st
->ss
->compare_super(st
, tst
) != 0) {
5651 /* Some mismatch. If exactly one array matches this host,
5652 * we can resolve on that one.
5653 * Or, if we are auto assembling, we just ignore the second
5656 dprintf(": superblock on %s doesn't match others - assembly aborted\n",
5662 dprintf("found: devname: %s\n", devname
);
5666 tst
->ss
->free_super(tst
);
5670 if ((err
= load_super_imsm_all(st
, -1, &st
->sb
, NULL
, devlist
, 0)) == 0) {
5671 struct mdinfo
*iter
, *head
= st
->ss
->container_content(st
, NULL
);
5672 for (iter
= head
; iter
; iter
= iter
->next
) {
5673 dprintf("content->text_version: %s vol\n",
5674 iter
->text_version
);
5675 if (iter
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
5676 /* do not assemble arrays with unsupported
5678 dprintf(": Cannot activate member %s.\n",
5679 iter
->text_version
);
5686 dprintf(" no valid super block on device list: err: %d %p\n",
5690 dprintf(" no more devices to examin\n");
5693 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5694 if ((tmpdev
->used
== 1) && (tmpdev
->found
)) {
5696 if (count
< tmpdev
->found
)
5699 count
-= tmpdev
->found
;
5702 if (tmpdev
->used
== 1)
5707 st
->ss
->free_super(st
);
5713 count_volumes(char *hba
, int dpa
, int verbose
)
5715 struct md_list
*devlist
= NULL
;
5719 devlist
= get_devices(hba
);
5720 /* if no intel devices return zero volumes */
5721 if (devlist
== NULL
)
5724 count
= active_arrays_by_format("imsm", hba
, &devlist
, dpa
, verbose
);
5725 dprintf(" path: %s active arrays: %d\n", hba
, count
);
5726 if (devlist
== NULL
)
5730 count
+= count_volumes_list(devlist
,
5734 dprintf("found %d count: %d\n", found
, count
);
5737 dprintf("path: %s total number of volumes: %d\n", hba
, count
);
5740 struct md_list
*dv
= devlist
;
5741 devlist
= devlist
->next
;
5748 static int imsm_default_chunk(const struct imsm_orom
*orom
)
5750 /* up to 512 if the plaform supports it, otherwise the platform max.
5751 * 128 if no platform detected
5753 int fs
= max(7, orom
? fls(orom
->sss
) : 0);
5755 return min(512, (1 << fs
));
5759 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
5760 int raiddisks
, int *chunk
, int verbose
)
5762 /* check/set platform and metadata limits/defaults */
5763 if (super
->orom
&& raiddisks
> super
->orom
->dpa
) {
5764 pr_vrb(": platform supports a maximum of %d disks per array\n",
5769 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
5770 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
5771 pr_vrb(": platform does not support raid%d with %d disk%s\n",
5772 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
5776 if (chunk
&& (*chunk
== 0 || *chunk
== UnSet
))
5777 *chunk
= imsm_default_chunk(super
->orom
);
5779 if (super
->orom
&& chunk
&& !imsm_orom_has_chunk(super
->orom
, *chunk
)) {
5780 pr_vrb(": platform does not support a chunk size of: "
5785 if (layout
!= imsm_level_to_layout(level
)) {
5787 pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n");
5788 else if (level
== 10)
5789 pr_vrb(": imsm raid 10 only supports the n2 layout\n");
5791 pr_vrb(": imsm unknown layout %#x for this raid level %d\n",
5798 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
5799 * FIX ME add ahci details
5801 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
5802 int layout
, int raiddisks
, int *chunk
,
5803 unsigned long long size
, char *dev
,
5804 unsigned long long *freesize
,
5808 struct intel_super
*super
= st
->sb
;
5809 struct imsm_super
*mpb
;
5811 unsigned long long pos
= 0;
5812 unsigned long long maxsize
;
5816 /* We must have the container info already read in. */
5820 mpb
= super
->anchor
;
5822 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, verbose
)) {
5823 fprintf(stderr
, Name
": RAID gemetry validation failed. "
5824 "Cannot proceed with the action(s).\n");
5828 /* General test: make sure there is space for
5829 * 'raiddisks' device extents of size 'size' at a given
5832 unsigned long long minsize
= size
;
5833 unsigned long long start_offset
= MaxSector
;
5836 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
5837 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5842 e
= get_extents(super
, dl
);
5845 unsigned long long esize
;
5846 esize
= e
[i
].start
- pos
;
5847 if (esize
>= minsize
)
5849 if (found
&& start_offset
== MaxSector
) {
5852 } else if (found
&& pos
!= start_offset
) {
5856 pos
= e
[i
].start
+ e
[i
].size
;
5858 } while (e
[i
-1].size
);
5863 if (dcnt
< raiddisks
) {
5865 fprintf(stderr
, Name
": imsm: Not enough "
5866 "devices with space for this array "
5874 /* This device must be a member of the set */
5875 if (stat(dev
, &stb
) < 0)
5877 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
5879 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5880 if (dl
->major
== (int)major(stb
.st_rdev
) &&
5881 dl
->minor
== (int)minor(stb
.st_rdev
))
5886 fprintf(stderr
, Name
": %s is not in the "
5887 "same imsm set\n", dev
);
5889 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
5890 /* If a volume is present then the current creation attempt
5891 * cannot incorporate new spares because the orom may not
5892 * understand this configuration (all member disks must be
5893 * members of each array in the container).
5895 fprintf(stderr
, Name
": %s is a spare and a volume"
5896 " is already defined for this container\n", dev
);
5897 fprintf(stderr
, Name
": The option-rom requires all member"
5898 " disks to be a member of all volumes\n");
5900 } else if (super
->orom
&& mpb
->num_raid_devs
> 0 &&
5901 mpb
->num_disks
!= raiddisks
) {
5902 fprintf(stderr
, Name
": The option-rom requires all member"
5903 " disks to be a member of all volumes\n");
5907 /* retrieve the largest free space block */
5908 e
= get_extents(super
, dl
);
5913 unsigned long long esize
;
5915 esize
= e
[i
].start
- pos
;
5916 if (esize
>= maxsize
)
5918 pos
= e
[i
].start
+ e
[i
].size
;
5920 } while (e
[i
-1].size
);
5925 fprintf(stderr
, Name
": unable to determine free space for: %s\n",
5929 if (maxsize
< size
) {
5931 fprintf(stderr
, Name
": %s not enough space (%llu < %llu)\n",
5932 dev
, maxsize
, size
);
5936 /* count total number of extents for merge */
5938 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5940 i
+= dl
->extent_cnt
;
5942 maxsize
= merge_extents(super
, i
);
5944 if (!check_env("IMSM_NO_PLATFORM") &&
5945 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
5946 fprintf(stderr
, Name
": attempting to create a second "
5947 "volume with size less then remaining space. "
5952 if (maxsize
< size
|| maxsize
== 0) {
5955 fprintf(stderr
, Name
": no free space"
5956 " left on device. Aborting...\n");
5958 fprintf(stderr
, Name
": not enough space"
5959 " to create volume of given size"
5960 " (%llu < %llu). Aborting...\n",
5966 *freesize
= maxsize
;
5969 int count
= count_volumes(super
->hba
->path
,
5970 super
->orom
->dpa
, verbose
);
5971 if (super
->orom
->vphba
<= count
) {
5972 pr_vrb(": platform does not support more then %d raid volumes.\n",
5973 super
->orom
->vphba
);
5980 static int reserve_space(struct supertype
*st
, int raiddisks
,
5981 unsigned long long size
, int chunk
,
5982 unsigned long long *freesize
)
5984 struct intel_super
*super
= st
->sb
;
5985 struct imsm_super
*mpb
= super
->anchor
;
5990 unsigned long long maxsize
;
5991 unsigned long long minsize
;
5995 /* find the largest common start free region of the possible disks */
5999 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6005 /* don't activate new spares if we are orom constrained
6006 * and there is already a volume active in the container
6008 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
6011 e
= get_extents(super
, dl
);
6014 for (i
= 1; e
[i
-1].size
; i
++)
6022 maxsize
= merge_extents(super
, extent_cnt
);
6026 minsize
= chunk
* 2;
6028 if (cnt
< raiddisks
||
6029 (super
->orom
&& used
&& used
!= raiddisks
) ||
6030 maxsize
< minsize
||
6032 fprintf(stderr
, Name
": not enough devices with space to create array.\n");
6033 return 0; /* No enough free spaces large enough */
6044 if (!check_env("IMSM_NO_PLATFORM") &&
6045 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
6046 fprintf(stderr
, Name
": attempting to create a second "
6047 "volume with size less then remaining space. "
6052 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6054 dl
->raiddisk
= cnt
++;
6061 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
6062 int raiddisks
, int *chunk
, unsigned long long size
,
6063 char *dev
, unsigned long long *freesize
,
6071 * if given unused devices create a container
6072 * if given given devices in a container create a member volume
6074 if (level
== LEVEL_CONTAINER
) {
6075 /* Must be a fresh device to add to a container */
6076 return validate_geometry_imsm_container(st
, level
, layout
,
6078 chunk
?*chunk
:0, size
,
6085 struct intel_super
*super
= st
->sb
;
6086 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
6090 /* we are being asked to automatically layout a
6091 * new volume based on the current contents of
6092 * the container. If the the parameters can be
6093 * satisfied reserve_space will record the disks,
6094 * start offset, and size of the volume to be
6095 * created. add_to_super and getinfo_super
6096 * detect when autolayout is in progress.
6098 /* assuming that freesize is always given when array is
6100 if (super
->orom
&& freesize
) {
6102 count
= count_volumes(super
->hba
->path
,
6103 super
->orom
->dpa
, verbose
);
6104 if (super
->orom
->vphba
<= count
) {
6105 pr_vrb(": platform does not support more"
6106 "then %d raid volumes.\n",
6107 super
->orom
->vphba
);
6112 return reserve_space(st
, raiddisks
, size
,
6113 chunk
?*chunk
:0, freesize
);
6118 /* creating in a given container */
6119 return validate_geometry_imsm_volume(st
, level
, layout
,
6120 raiddisks
, chunk
, size
,
6121 dev
, freesize
, verbose
);
6124 /* This device needs to be a device in an 'imsm' container */
6125 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
6129 Name
": Cannot create this array on device %s\n",
6134 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
6136 fprintf(stderr
, Name
": Cannot open %s: %s\n",
6137 dev
, strerror(errno
));
6140 /* Well, it is in use by someone, maybe an 'imsm' container. */
6141 cfd
= open_container(fd
);
6145 fprintf(stderr
, Name
": Cannot use %s: It is busy\n",
6149 sra
= sysfs_read(cfd
, 0, GET_VERSION
);
6150 if (sra
&& sra
->array
.major_version
== -1 &&
6151 strcmp(sra
->text_version
, "imsm") == 0)
6155 /* This is a member of a imsm container. Load the container
6156 * and try to create a volume
6158 struct intel_super
*super
;
6160 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
, NULL
, 1) == 0) {
6162 st
->container_dev
= fd2devnum(cfd
);
6164 return validate_geometry_imsm_volume(st
, level
, layout
,
6173 fprintf(stderr
, Name
": failed container membership check\n");
6179 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
6181 struct intel_super
*super
= st
->sb
;
6183 if (level
&& *level
== UnSet
)
6184 *level
= LEVEL_CONTAINER
;
6186 if (level
&& layout
&& *layout
== UnSet
)
6187 *layout
= imsm_level_to_layout(*level
);
6189 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0))
6190 *chunk
= imsm_default_chunk(super
->orom
);
6193 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
6195 static int kill_subarray_imsm(struct supertype
*st
)
6197 /* remove the subarray currently referenced by ->current_vol */
6199 struct intel_dev
**dp
;
6200 struct intel_super
*super
= st
->sb
;
6201 __u8 current_vol
= super
->current_vol
;
6202 struct imsm_super
*mpb
= super
->anchor
;
6204 if (super
->current_vol
< 0)
6206 super
->current_vol
= -1; /* invalidate subarray cursor */
6208 /* block deletions that would change the uuid of active subarrays
6210 * FIXME when immutable ids are available, but note that we'll
6211 * also need to fixup the invalidated/active subarray indexes in
6214 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6217 if (i
< current_vol
)
6219 sprintf(subarray
, "%u", i
);
6220 if (is_subarray_active(subarray
, st
->devname
)) {
6222 Name
": deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
6229 if (st
->update_tail
) {
6230 struct imsm_update_kill_array
*u
= malloc(sizeof(*u
));
6234 u
->type
= update_kill_array
;
6235 u
->dev_idx
= current_vol
;
6236 append_metadata_update(st
, u
, sizeof(*u
));
6241 for (dp
= &super
->devlist
; *dp
;)
6242 if ((*dp
)->index
== current_vol
) {
6245 handle_missing(super
, (*dp
)->dev
);
6246 if ((*dp
)->index
> current_vol
)
6251 /* no more raid devices, all active components are now spares,
6252 * but of course failed are still failed
6254 if (--mpb
->num_raid_devs
== 0) {
6257 for (d
= super
->disks
; d
; d
= d
->next
)
6262 super
->updates_pending
++;
6267 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
6268 char *update
, struct mddev_ident
*ident
)
6270 /* update the subarray currently referenced by ->current_vol */
6271 struct intel_super
*super
= st
->sb
;
6272 struct imsm_super
*mpb
= super
->anchor
;
6274 if (strcmp(update
, "name") == 0) {
6275 char *name
= ident
->name
;
6279 if (is_subarray_active(subarray
, st
->devname
)) {
6281 Name
": Unable to update name of active subarray\n");
6285 if (!check_name(super
, name
, 0))
6288 vol
= strtoul(subarray
, &ep
, 10);
6289 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
6292 if (st
->update_tail
) {
6293 struct imsm_update_rename_array
*u
= malloc(sizeof(*u
));
6297 u
->type
= update_rename_array
;
6299 snprintf((char *) u
->name
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6300 append_metadata_update(st
, u
, sizeof(*u
));
6302 struct imsm_dev
*dev
;
6305 dev
= get_imsm_dev(super
, vol
);
6306 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6307 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6308 dev
= get_imsm_dev(super
, i
);
6309 handle_missing(super
, dev
);
6311 super
->updates_pending
++;
6318 #endif /* MDASSEMBLE */
6320 static int is_gen_migration(struct imsm_dev
*dev
)
6325 if (!dev
->vol
.migr_state
)
6328 if (migr_type(dev
) == MIGR_GEN_MIGR
)
6334 static int is_rebuilding(struct imsm_dev
*dev
)
6336 struct imsm_map
*migr_map
;
6338 if (!dev
->vol
.migr_state
)
6341 if (migr_type(dev
) != MIGR_REBUILD
)
6344 migr_map
= get_imsm_map(dev
, MAP_1
);
6346 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
6353 static int is_initializing(struct imsm_dev
*dev
)
6355 struct imsm_map
*migr_map
;
6357 if (!dev
->vol
.migr_state
)
6360 if (migr_type(dev
) != MIGR_INIT
)
6363 migr_map
= get_imsm_map(dev
, MAP_1
);
6365 if (migr_map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
6372 static void update_recovery_start(struct intel_super
*super
,
6373 struct imsm_dev
*dev
,
6374 struct mdinfo
*array
)
6376 struct mdinfo
*rebuild
= NULL
;
6380 if (!is_rebuilding(dev
))
6383 /* Find the rebuild target, but punt on the dual rebuild case */
6384 for (d
= array
->devs
; d
; d
= d
->next
)
6385 if (d
->recovery_start
== 0) {
6392 /* (?) none of the disks are marked with
6393 * IMSM_ORD_REBUILD, so assume they are missing and the
6394 * disk_ord_tbl was not correctly updated
6396 dprintf("%s: failed to locate out-of-sync disk\n", __func__
);
6400 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
6401 rebuild
->recovery_start
= units
* blocks_per_migr_unit(super
, dev
);
6405 static int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
);
6408 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
6410 /* Given a container loaded by load_super_imsm_all,
6411 * extract information about all the arrays into
6413 * If 'subarray' is given, just extract info about that array.
6415 * For each imsm_dev create an mdinfo, fill it in,
6416 * then look for matching devices in super->disks
6417 * and create appropriate device mdinfo.
6419 struct intel_super
*super
= st
->sb
;
6420 struct imsm_super
*mpb
= super
->anchor
;
6421 struct mdinfo
*rest
= NULL
;
6425 int spare_disks
= 0;
6427 /* do not assemble arrays when not all attributes are supported */
6428 if (imsm_check_attributes(mpb
->attributes
) == 0) {
6430 fprintf(stderr
, Name
": Unsupported attributes in IMSM metadata."
6431 "Arrays activation is blocked.\n");
6434 /* check for bad blocks */
6435 if (imsm_bbm_log_size(super
->anchor
)) {
6436 fprintf(stderr
, Name
": BBM log found in IMSM metadata."
6437 "Arrays activation is blocked.\n");
6442 /* count spare devices, not used in maps
6444 for (d
= super
->disks
; d
; d
= d
->next
)
6448 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6449 struct imsm_dev
*dev
;
6450 struct imsm_map
*map
;
6451 struct imsm_map
*map2
;
6452 struct mdinfo
*this;
6460 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
6463 dev
= get_imsm_dev(super
, i
);
6464 map
= get_imsm_map(dev
, MAP_0
);
6465 map2
= get_imsm_map(dev
, MAP_1
);
6467 /* do not publish arrays that are in the middle of an
6468 * unsupported migration
6470 if (dev
->vol
.migr_state
&&
6471 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
6472 fprintf(stderr
, Name
": cannot assemble volume '%.16s':"
6473 " unsupported migration in progress\n",
6477 /* do not publish arrays that are not support by controller's
6481 this = malloc(sizeof(*this));
6483 fprintf(stderr
, Name
": failed to allocate %zu bytes\n",
6488 super
->current_vol
= i
;
6489 getinfo_super_imsm_volume(st
, this, NULL
);
6492 chunk
= __le16_to_cpu(map
->blocks_per_strip
) >> 1;
6493 /* mdadm does not support all metadata features- set the bit in all arrays state */
6494 if (!validate_geometry_imsm_orom(super
,
6495 get_imsm_raid_level(map
), /* RAID level */
6496 imsm_level_to_layout(get_imsm_raid_level(map
)),
6497 map
->num_members
, /* raid disks */
6500 fprintf(stderr
, Name
": IMSM RAID geometry validation"
6501 " failed. Array %s activation is blocked.\n",
6503 this->array
.state
|=
6504 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
6505 (1<<MD_SB_BLOCK_VOLUME
);
6509 /* if array has bad blocks, set suitable bit in all arrays state */
6511 this->array
.state
|=
6512 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
6513 (1<<MD_SB_BLOCK_VOLUME
);
6515 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
6516 unsigned long long recovery_start
;
6517 struct mdinfo
*info_d
;
6524 idx
= get_imsm_disk_idx(dev
, slot
, MAP_0
);
6525 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
6526 for (d
= super
->disks
; d
; d
= d
->next
)
6527 if (d
->index
== idx
)
6530 recovery_start
= MaxSector
;
6533 if (d
&& is_failed(&d
->disk
))
6535 if (ord
& IMSM_ORD_REBUILD
)
6539 * if we skip some disks the array will be assmebled degraded;
6540 * reset resync start to avoid a dirty-degraded
6541 * situation when performing the intial sync
6543 * FIXME handle dirty degraded
6545 if ((skip
|| recovery_start
== 0) && !dev
->vol
.dirty
)
6546 this->resync_start
= MaxSector
;
6550 info_d
= calloc(1, sizeof(*info_d
));
6552 fprintf(stderr
, Name
": failed to allocate disk"
6553 " for volume %.16s\n", dev
->volume
);
6554 info_d
= this->devs
;
6556 struct mdinfo
*d
= info_d
->next
;
6565 info_d
->next
= this->devs
;
6566 this->devs
= info_d
;
6568 info_d
->disk
.number
= d
->index
;
6569 info_d
->disk
.major
= d
->major
;
6570 info_d
->disk
.minor
= d
->minor
;
6571 info_d
->disk
.raid_disk
= slot
;
6572 info_d
->recovery_start
= recovery_start
;
6574 if (slot
< map2
->num_members
)
6575 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
6577 this->array
.spare_disks
++;
6579 if (slot
< map
->num_members
)
6580 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
6582 this->array
.spare_disks
++;
6584 if (info_d
->recovery_start
== MaxSector
)
6585 this->array
.working_disks
++;
6587 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
6588 info_d
->data_offset
= __le32_to_cpu(map
->pba_of_lba0
);
6589 info_d
->component_size
= __le32_to_cpu(map
->blocks_per_member
);
6591 /* now that the disk list is up-to-date fixup recovery_start */
6592 update_recovery_start(super
, dev
, this);
6593 this->array
.spare_disks
+= spare_disks
;
6596 /* check for reshape */
6597 if (this->reshape_active
== 1)
6598 recover_backup_imsm(st
, this);
6607 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
6608 int failed
, int look_in_map
)
6610 struct imsm_map
*map
;
6612 map
= get_imsm_map(dev
, look_in_map
);
6615 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
6616 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
6618 switch (get_imsm_raid_level(map
)) {
6620 return IMSM_T_STATE_FAILED
;
6623 if (failed
< map
->num_members
)
6624 return IMSM_T_STATE_DEGRADED
;
6626 return IMSM_T_STATE_FAILED
;
6631 * check to see if any mirrors have failed, otherwise we
6632 * are degraded. Even numbered slots are mirrored on
6636 /* gcc -Os complains that this is unused */
6637 int insync
= insync
;
6639 for (i
= 0; i
< map
->num_members
; i
++) {
6640 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_X
);
6641 int idx
= ord_to_idx(ord
);
6642 struct imsm_disk
*disk
;
6644 /* reset the potential in-sync count on even-numbered
6645 * slots. num_copies is always 2 for imsm raid10
6650 disk
= get_imsm_disk(super
, idx
);
6651 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
6654 /* no in-sync disks left in this mirror the
6658 return IMSM_T_STATE_FAILED
;
6661 return IMSM_T_STATE_DEGRADED
;
6665 return IMSM_T_STATE_DEGRADED
;
6667 return IMSM_T_STATE_FAILED
;
6673 return map
->map_state
;
6676 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
6681 struct imsm_disk
*disk
;
6682 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
6683 struct imsm_map
*prev
= get_imsm_map(dev
, MAP_1
);
6684 struct imsm_map
*map_for_loop
;
6689 /* at the beginning of migration we set IMSM_ORD_REBUILD on
6690 * disks that are being rebuilt. New failures are recorded to
6691 * map[0]. So we look through all the disks we started with and
6692 * see if any failures are still present, or if any new ones
6696 if (prev
&& (map
->num_members
< prev
->num_members
))
6697 map_for_loop
= prev
;
6699 for (i
= 0; i
< map_for_loop
->num_members
; i
++) {
6701 /* when MAP_X is passed both maps failures are counted
6704 ((look_in_map
== MAP_1
) || (look_in_map
== MAP_X
)) &&
6705 (i
< prev
->num_members
)) {
6706 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
6707 idx_1
= ord_to_idx(ord
);
6709 disk
= get_imsm_disk(super
, idx_1
);
6710 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
6713 if (((look_in_map
== MAP_0
) || (look_in_map
== MAP_X
)) &&
6714 (i
< map
->num_members
)) {
6715 ord
= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
6716 idx
= ord_to_idx(ord
);
6719 disk
= get_imsm_disk(super
, idx
);
6720 if (!disk
|| is_failed(disk
) ||
6721 ord
& IMSM_ORD_REBUILD
)
6731 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
6734 struct intel_super
*super
= c
->sb
;
6735 struct imsm_super
*mpb
= super
->anchor
;
6737 if (atoi(inst
) >= mpb
->num_raid_devs
) {
6738 fprintf(stderr
, "%s: subarry index %d, out of range\n",
6739 __func__
, atoi(inst
));
6743 dprintf("imsm: open_new %s\n", inst
);
6744 a
->info
.container_member
= atoi(inst
);
6748 static int is_resyncing(struct imsm_dev
*dev
)
6750 struct imsm_map
*migr_map
;
6752 if (!dev
->vol
.migr_state
)
6755 if (migr_type(dev
) == MIGR_INIT
||
6756 migr_type(dev
) == MIGR_REPAIR
)
6759 if (migr_type(dev
) == MIGR_GEN_MIGR
)
6762 migr_map
= get_imsm_map(dev
, MAP_1
);
6764 if ((migr_map
->map_state
== IMSM_T_STATE_NORMAL
) &&
6765 (dev
->vol
.migr_type
!= MIGR_GEN_MIGR
))
6771 /* return true if we recorded new information */
6772 static int mark_failure(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
6776 struct imsm_map
*map
;
6777 char buf
[MAX_RAID_SERIAL_LEN
+3];
6778 unsigned int len
, shift
= 0;
6780 /* new failures are always set in map[0] */
6781 map
= get_imsm_map(dev
, MAP_0
);
6783 slot
= get_imsm_disk_slot(map
, idx
);
6787 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
6788 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
6791 memcpy(buf
, disk
->serial
, MAX_RAID_SERIAL_LEN
);
6792 buf
[MAX_RAID_SERIAL_LEN
] = '\000';
6794 if ((len
= strlen(buf
)) >= MAX_RAID_SERIAL_LEN
)
6795 shift
= len
- MAX_RAID_SERIAL_LEN
+ 1;
6796 strncpy((char *)disk
->serial
, &buf
[shift
], MAX_RAID_SERIAL_LEN
);
6798 disk
->status
|= FAILED_DISK
;
6799 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
6800 /* mark failures in second map if second map exists and this disk
6802 * This is valid for migration, initialization and rebuild
6804 if (dev
->vol
.migr_state
) {
6805 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
6806 int slot2
= get_imsm_disk_slot(map2
, idx
);
6808 if ((slot2
< map2
->num_members
) &&
6810 set_imsm_ord_tbl_ent(map2
, slot2
,
6811 idx
| IMSM_ORD_REBUILD
);
6813 if (map
->failed_disk_num
== 0xff)
6814 map
->failed_disk_num
= slot
;
6818 static void mark_missing(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
6820 mark_failure(dev
, disk
, idx
);
6822 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
6825 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
6826 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
6829 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
6833 if (!super
->missing
)
6836 dprintf("imsm: mark missing\n");
6837 /* end process for initialization and rebuild only
6839 if (is_gen_migration(dev
) == 0) {
6843 failed
= imsm_count_failed(super
, dev
, MAP_0
);
6844 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
6846 end_migration(dev
, super
, map_state
);
6848 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
6849 mark_missing(dev
, &dl
->disk
, dl
->index
);
6850 super
->updates_pending
++;
6853 static unsigned long long imsm_set_array_size(struct imsm_dev
*dev
)
6855 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
6856 unsigned long long array_blocks
;
6857 struct imsm_map
*map
;
6859 if (used_disks
== 0) {
6860 /* when problems occures
6861 * return current array_blocks value
6863 array_blocks
= __le32_to_cpu(dev
->size_high
);
6864 array_blocks
= array_blocks
<< 32;
6865 array_blocks
+= __le32_to_cpu(dev
->size_low
);
6867 return array_blocks
;
6870 /* set array size in metadata
6872 map
= get_imsm_map(dev
, MAP_0
);
6873 array_blocks
= map
->blocks_per_member
* used_disks
;
6875 /* round array size down to closest MB
6877 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
6878 dev
->size_low
= __cpu_to_le32((__u32
)array_blocks
);
6879 dev
->size_high
= __cpu_to_le32((__u32
)(array_blocks
>> 32));
6881 return array_blocks
;
6884 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
6886 static void imsm_progress_container_reshape(struct intel_super
*super
)
6888 /* if no device has a migr_state, but some device has a
6889 * different number of members than the previous device, start
6890 * changing the number of devices in this device to match
6893 struct imsm_super
*mpb
= super
->anchor
;
6894 int prev_disks
= -1;
6898 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6899 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
6900 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
6901 struct imsm_map
*map2
;
6902 int prev_num_members
;
6904 if (dev
->vol
.migr_state
)
6907 if (prev_disks
== -1)
6908 prev_disks
= map
->num_members
;
6909 if (prev_disks
== map
->num_members
)
6912 /* OK, this array needs to enter reshape mode.
6913 * i.e it needs a migr_state
6916 copy_map_size
= sizeof_imsm_map(map
);
6917 prev_num_members
= map
->num_members
;
6918 map
->num_members
= prev_disks
;
6919 dev
->vol
.migr_state
= 1;
6920 dev
->vol
.curr_migr_unit
= 0;
6921 set_migr_type(dev
, MIGR_GEN_MIGR
);
6922 for (i
= prev_num_members
;
6923 i
< map
->num_members
; i
++)
6924 set_imsm_ord_tbl_ent(map
, i
, i
);
6925 map2
= get_imsm_map(dev
, MAP_1
);
6926 /* Copy the current map */
6927 memcpy(map2
, map
, copy_map_size
);
6928 map2
->num_members
= prev_num_members
;
6930 imsm_set_array_size(dev
);
6931 super
->updates_pending
++;
6935 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
6936 * states are handled in imsm_set_disk() with one exception, when a
6937 * resync is stopped due to a new failure this routine will set the
6938 * 'degraded' state for the array.
6940 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
6942 int inst
= a
->info
.container_member
;
6943 struct intel_super
*super
= a
->container
->sb
;
6944 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
6945 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
6946 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
6947 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
6948 __u32 blocks_per_unit
;
6950 if (dev
->vol
.migr_state
&&
6951 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
6952 /* array state change is blocked due to reshape action
6954 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
6955 * - finish the reshape (if last_checkpoint is big and action != reshape)
6956 * - update curr_migr_unit
6958 if (a
->curr_action
== reshape
) {
6959 /* still reshaping, maybe update curr_migr_unit */
6960 goto mark_checkpoint
;
6962 if (a
->last_checkpoint
== 0 && a
->prev_action
== reshape
) {
6963 /* for some reason we aborted the reshape.
6965 * disable automatic metadata rollback
6966 * user action is required to recover process
6969 struct imsm_map
*map2
=
6970 get_imsm_map(dev
, MAP_1
);
6971 dev
->vol
.migr_state
= 0;
6972 set_migr_type(dev
, 0);
6973 dev
->vol
.curr_migr_unit
= 0;
6975 sizeof_imsm_map(map2
));
6976 super
->updates_pending
++;
6979 if (a
->last_checkpoint
>= a
->info
.component_size
) {
6980 unsigned long long array_blocks
;
6984 used_disks
= imsm_num_data_members(dev
, MAP_0
);
6985 if (used_disks
> 0) {
6987 map
->blocks_per_member
*
6989 /* round array size down to closest MB
6991 array_blocks
= (array_blocks
6992 >> SECT_PER_MB_SHIFT
)
6993 << SECT_PER_MB_SHIFT
;
6994 a
->info
.custom_array_size
= array_blocks
;
6995 /* encourage manager to update array
6999 a
->check_reshape
= 1;
7001 /* finalize online capacity expansion/reshape */
7002 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
7004 mdi
->disk
.raid_disk
,
7007 imsm_progress_container_reshape(super
);
7012 /* before we activate this array handle any missing disks */
7013 if (consistent
== 2)
7014 handle_missing(super
, dev
);
7016 if (consistent
== 2 &&
7017 (!is_resync_complete(&a
->info
) ||
7018 map_state
!= IMSM_T_STATE_NORMAL
||
7019 dev
->vol
.migr_state
))
7022 if (is_resync_complete(&a
->info
)) {
7023 /* complete intialization / resync,
7024 * recovery and interrupted recovery is completed in
7027 if (is_resyncing(dev
)) {
7028 dprintf("imsm: mark resync done\n");
7029 end_migration(dev
, super
, map_state
);
7030 super
->updates_pending
++;
7031 a
->last_checkpoint
= 0;
7033 } else if ((!is_resyncing(dev
) && !failed
) &&
7034 (imsm_reshape_blocks_arrays_changes(super
) == 0)) {
7035 /* mark the start of the init process if nothing is failed */
7036 dprintf("imsm: mark resync start\n");
7037 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
7038 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
7040 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
7041 super
->updates_pending
++;
7045 /* skip checkpointing for general migration,
7046 * it is controlled in mdadm
7048 if (is_gen_migration(dev
))
7049 goto skip_mark_checkpoint
;
7051 /* check if we can update curr_migr_unit from resync_start, recovery_start */
7052 blocks_per_unit
= blocks_per_migr_unit(super
, dev
);
7053 if (blocks_per_unit
) {
7057 units
= a
->last_checkpoint
/ blocks_per_unit
;
7060 /* check that we did not overflow 32-bits, and that
7061 * curr_migr_unit needs updating
7063 if (units32
== units
&&
7065 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
7066 dprintf("imsm: mark checkpoint (%u)\n", units32
);
7067 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
7068 super
->updates_pending
++;
7072 skip_mark_checkpoint
:
7073 /* mark dirty / clean */
7074 if (dev
->vol
.dirty
!= !consistent
) {
7075 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
7080 super
->updates_pending
++;
7086 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
7088 int inst
= a
->info
.container_member
;
7089 struct intel_super
*super
= a
->container
->sb
;
7090 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7091 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7092 struct imsm_disk
*disk
;
7097 if (n
> map
->num_members
)
7098 fprintf(stderr
, "imsm: set_disk %d out of range 0..%d\n",
7099 n
, map
->num_members
- 1);
7104 dprintf("imsm: set_disk %d:%x\n", n
, state
);
7106 ord
= get_imsm_ord_tbl_ent(dev
, n
, MAP_0
);
7107 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
7109 /* check for new failures */
7110 if (state
& DS_FAULTY
) {
7111 if (mark_failure(dev
, disk
, ord_to_idx(ord
)))
7112 super
->updates_pending
++;
7115 /* check if in_sync */
7116 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
7117 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
7119 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
7120 super
->updates_pending
++;
7123 failed
= imsm_count_failed(super
, dev
, MAP_0
);
7124 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7126 /* check if recovery complete, newly degraded, or failed */
7127 dprintf("imsm: Detected transition to state ");
7128 switch (map_state
) {
7129 case IMSM_T_STATE_NORMAL
: /* transition to normal state */
7130 dprintf("normal: ");
7131 if (is_rebuilding(dev
)) {
7132 dprintf("while rebuilding");
7133 end_migration(dev
, super
, map_state
);
7134 map
= get_imsm_map(dev
, MAP_0
);
7135 map
->failed_disk_num
= ~0;
7136 super
->updates_pending
++;
7137 a
->last_checkpoint
= 0;
7140 if (is_gen_migration(dev
)) {
7141 dprintf("while general migration");
7142 if (a
->last_checkpoint
>= a
->info
.component_size
)
7143 end_migration(dev
, super
, map_state
);
7145 map
->map_state
= map_state
;
7146 map
= get_imsm_map(dev
, MAP_0
);
7147 map
->failed_disk_num
= ~0;
7148 super
->updates_pending
++;
7152 case IMSM_T_STATE_DEGRADED
: /* transition to degraded state */
7153 dprintf("degraded: ");
7154 if ((map
->map_state
!= map_state
) &&
7155 !dev
->vol
.migr_state
) {
7156 dprintf("mark degraded");
7157 map
->map_state
= map_state
;
7158 super
->updates_pending
++;
7159 a
->last_checkpoint
= 0;
7162 if (is_rebuilding(dev
)) {
7163 dprintf("while rebuilding.");
7164 if (map
->map_state
!= map_state
) {
7165 dprintf(" Map state change");
7166 end_migration(dev
, super
, map_state
);
7167 super
->updates_pending
++;
7171 if (is_gen_migration(dev
)) {
7172 dprintf("while general migration");
7173 if (a
->last_checkpoint
>= a
->info
.component_size
)
7174 end_migration(dev
, super
, map_state
);
7176 map
->map_state
= map_state
;
7177 manage_second_map(super
, dev
);
7179 super
->updates_pending
++;
7182 if (is_initializing(dev
)) {
7183 dprintf("while initialization.");
7184 map
->map_state
= map_state
;
7185 super
->updates_pending
++;
7189 case IMSM_T_STATE_FAILED
: /* transition to failed state */
7190 dprintf("failed: ");
7191 if (is_gen_migration(dev
)) {
7192 dprintf("while general migration");
7193 map
->map_state
= map_state
;
7194 super
->updates_pending
++;
7197 if (map
->map_state
!= map_state
) {
7198 dprintf("mark failed");
7199 end_migration(dev
, super
, map_state
);
7200 super
->updates_pending
++;
7201 a
->last_checkpoint
= 0;
7206 dprintf("state %i\n", map_state
);
7212 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
7215 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
7216 unsigned long long dsize
;
7217 unsigned long long sectors
;
7219 get_dev_size(fd
, NULL
, &dsize
);
7221 if (mpb_size
> 512) {
7222 /* -1 to account for anchor */
7223 sectors
= mpb_sectors(mpb
) - 1;
7225 /* write the extended mpb to the sectors preceeding the anchor */
7226 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0)
7229 if ((unsigned long long)write(fd
, buf
+ 512, 512 * sectors
)
7234 /* first block is stored on second to last sector of the disk */
7235 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0)
7238 if (write(fd
, buf
, 512) != 512)
7244 static void imsm_sync_metadata(struct supertype
*container
)
7246 struct intel_super
*super
= container
->sb
;
7248 dprintf("sync metadata: %d\n", super
->updates_pending
);
7249 if (!super
->updates_pending
)
7252 write_super_imsm(container
, 0);
7254 super
->updates_pending
= 0;
7257 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
7259 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
7260 int i
= get_imsm_disk_idx(dev
, idx
, MAP_X
);
7263 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7267 if (dl
&& is_failed(&dl
->disk
))
7271 dprintf("%s: found %x:%x\n", __func__
, dl
->major
, dl
->minor
);
7276 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
7277 struct active_array
*a
, int activate_new
,
7278 struct mdinfo
*additional_test_list
)
7280 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
7281 int idx
= get_imsm_disk_idx(dev
, slot
, MAP_X
);
7282 struct imsm_super
*mpb
= super
->anchor
;
7283 struct imsm_map
*map
;
7284 unsigned long long pos
;
7289 __u32 array_start
= 0;
7290 __u32 array_end
= 0;
7292 struct mdinfo
*test_list
;
7294 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7295 /* If in this array, skip */
7296 for (d
= a
->info
.devs
; d
; d
= d
->next
)
7297 if (d
->state_fd
>= 0 &&
7298 d
->disk
.major
== dl
->major
&&
7299 d
->disk
.minor
== dl
->minor
) {
7300 dprintf("%x:%x already in array\n",
7301 dl
->major
, dl
->minor
);
7306 test_list
= additional_test_list
;
7308 if (test_list
->disk
.major
== dl
->major
&&
7309 test_list
->disk
.minor
== dl
->minor
) {
7310 dprintf("%x:%x already in additional test list\n",
7311 dl
->major
, dl
->minor
);
7314 test_list
= test_list
->next
;
7319 /* skip in use or failed drives */
7320 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
7322 dprintf("%x:%x status (failed: %d index: %d)\n",
7323 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
7327 /* skip pure spares when we are looking for partially
7328 * assimilated drives
7330 if (dl
->index
== -1 && !activate_new
)
7333 /* Does this unused device have the requisite free space?
7334 * It needs to be able to cover all member volumes
7336 ex
= get_extents(super
, dl
);
7338 dprintf("cannot get extents\n");
7341 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7342 dev
= get_imsm_dev(super
, i
);
7343 map
= get_imsm_map(dev
, MAP_0
);
7345 /* check if this disk is already a member of
7348 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
7354 array_start
= __le32_to_cpu(map
->pba_of_lba0
);
7355 array_end
= array_start
+
7356 __le32_to_cpu(map
->blocks_per_member
) - 1;
7359 /* check that we can start at pba_of_lba0 with
7360 * blocks_per_member of space
7362 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
7366 pos
= ex
[j
].start
+ ex
[j
].size
;
7368 } while (ex
[j
-1].size
);
7375 if (i
< mpb
->num_raid_devs
) {
7376 dprintf("%x:%x does not have %u to %u available\n",
7377 dl
->major
, dl
->minor
, array_start
, array_end
);
7388 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
7390 struct imsm_dev
*dev2
;
7391 struct imsm_map
*map
;
7397 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
7399 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
, MAP_0
);
7400 if (state
== IMSM_T_STATE_FAILED
) {
7401 map
= get_imsm_map(dev2
, MAP_0
);
7404 for (slot
= 0; slot
< map
->num_members
; slot
++) {
7406 * Check if failed disks are deleted from intel
7407 * disk list or are marked to be deleted
7409 idx
= get_imsm_disk_idx(dev2
, slot
, MAP_X
);
7410 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
7412 * Do not rebuild the array if failed disks
7413 * from failed sub-array are not removed from
7417 is_failed(&idisk
->disk
) &&
7418 (idisk
->action
!= DISK_REMOVE
))
7426 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
7427 struct metadata_update
**updates
)
7430 * Find a device with unused free space and use it to replace a
7431 * failed/vacant region in an array. We replace failed regions one a
7432 * array at a time. The result is that a new spare disk will be added
7433 * to the first failed array and after the monitor has finished
7434 * propagating failures the remainder will be consumed.
7436 * FIXME add a capability for mdmon to request spares from another
7440 struct intel_super
*super
= a
->container
->sb
;
7441 int inst
= a
->info
.container_member
;
7442 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7443 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7444 int failed
= a
->info
.array
.raid_disks
;
7445 struct mdinfo
*rv
= NULL
;
7448 struct metadata_update
*mu
;
7450 struct imsm_update_activate_spare
*u
;
7455 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
7456 if ((d
->curr_state
& DS_FAULTY
) &&
7458 /* wait for Removal to happen */
7460 if (d
->state_fd
>= 0)
7464 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
7465 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
7467 if (imsm_reshape_blocks_arrays_changes(super
))
7470 /* Cannot activate another spare if rebuild is in progress already
7472 if (is_rebuilding(dev
)) {
7473 dprintf("imsm: No spare activation allowed. "
7474 "Rebuild in progress already.\n");
7478 if (a
->info
.array
.level
== 4)
7479 /* No repair for takeovered array
7480 * imsm doesn't support raid4
7484 if (imsm_check_degraded(super
, dev
, failed
, MAP_0
) !=
7485 IMSM_T_STATE_DEGRADED
)
7489 * If there are any failed disks check state of the other volume.
7490 * Block rebuild if the another one is failed until failed disks
7491 * are removed from container.
7494 dprintf("found failed disks in %.*s, check if there another"
7495 "failed sub-array.\n",
7496 MAX_RAID_SERIAL_LEN
, dev
->volume
);
7497 /* check if states of the other volumes allow for rebuild */
7498 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
7500 allowed
= imsm_rebuild_allowed(a
->container
,
7508 /* For each slot, if it is not working, find a spare */
7509 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
7510 for (d
= a
->info
.devs
; d
; d
= d
->next
)
7511 if (d
->disk
.raid_disk
== i
)
7513 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
7514 if (d
&& (d
->state_fd
>= 0))
7518 * OK, this device needs recovery. Try to re-add the
7519 * previous occupant of this slot, if this fails see if
7520 * we can continue the assimilation of a spare that was
7521 * partially assimilated, finally try to activate a new
7524 dl
= imsm_readd(super
, i
, a
);
7526 dl
= imsm_add_spare(super
, i
, a
, 0, rv
);
7528 dl
= imsm_add_spare(super
, i
, a
, 1, rv
);
7532 /* found a usable disk with enough space */
7533 di
= malloc(sizeof(*di
));
7536 memset(di
, 0, sizeof(*di
));
7538 /* dl->index will be -1 in the case we are activating a
7539 * pristine spare. imsm_process_update() will create a
7540 * new index in this case. Once a disk is found to be
7541 * failed in all member arrays it is kicked from the
7544 di
->disk
.number
= dl
->index
;
7546 /* (ab)use di->devs to store a pointer to the device
7549 di
->devs
= (struct mdinfo
*) dl
;
7551 di
->disk
.raid_disk
= i
;
7552 di
->disk
.major
= dl
->major
;
7553 di
->disk
.minor
= dl
->minor
;
7555 di
->recovery_start
= 0;
7556 di
->data_offset
= __le32_to_cpu(map
->pba_of_lba0
);
7557 di
->component_size
= a
->info
.component_size
;
7558 di
->container_member
= inst
;
7559 super
->random
= random32();
7563 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
7564 i
, di
->data_offset
);
7568 /* No spares found */
7570 /* Now 'rv' has a list of devices to return.
7571 * Create a metadata_update record to update the
7572 * disk_ord_tbl for the array
7574 mu
= malloc(sizeof(*mu
));
7576 mu
->buf
= malloc(sizeof(struct imsm_update_activate_spare
) * num_spares
);
7577 if (mu
->buf
== NULL
) {
7584 struct mdinfo
*n
= rv
->next
;
7593 mu
->space_list
= NULL
;
7594 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
7595 mu
->next
= *updates
;
7596 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
7598 for (di
= rv
; di
; di
= di
->next
) {
7599 u
->type
= update_activate_spare
;
7600 u
->dl
= (struct dl
*) di
->devs
;
7602 u
->slot
= di
->disk
.raid_disk
;
7613 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
7615 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
7616 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7617 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, MAP_0
);
7618 struct disk_info
*inf
= get_disk_info(u
);
7619 struct imsm_disk
*disk
;
7623 for (i
= 0; i
< map
->num_members
; i
++) {
7624 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, MAP_X
));
7625 for (j
= 0; j
< new_map
->num_members
; j
++)
7626 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
7634 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
7636 struct dl
*dl
= NULL
;
7637 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7638 if ((dl
->major
== major
) && (dl
->minor
== minor
))
7643 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
7645 struct dl
*prev
= NULL
;
7649 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7650 if ((dl
->major
== major
) && (dl
->minor
== minor
)) {
7653 prev
->next
= dl
->next
;
7655 super
->disks
= dl
->next
;
7657 __free_imsm_disk(dl
);
7658 dprintf("%s: removed %x:%x\n",
7659 __func__
, major
, minor
);
7667 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
7669 static int add_remove_disk_update(struct intel_super
*super
)
7671 int check_degraded
= 0;
7672 struct dl
*disk
= NULL
;
7673 /* add/remove some spares to/from the metadata/contrainer */
7674 while (super
->disk_mgmt_list
) {
7675 struct dl
*disk_cfg
;
7677 disk_cfg
= super
->disk_mgmt_list
;
7678 super
->disk_mgmt_list
= disk_cfg
->next
;
7679 disk_cfg
->next
= NULL
;
7681 if (disk_cfg
->action
== DISK_ADD
) {
7682 disk_cfg
->next
= super
->disks
;
7683 super
->disks
= disk_cfg
;
7685 dprintf("%s: added %x:%x\n",
7686 __func__
, disk_cfg
->major
,
7688 } else if (disk_cfg
->action
== DISK_REMOVE
) {
7689 dprintf("Disk remove action processed: %x.%x\n",
7690 disk_cfg
->major
, disk_cfg
->minor
);
7691 disk
= get_disk_super(super
,
7695 /* store action status */
7696 disk
->action
= DISK_REMOVE
;
7697 /* remove spare disks only */
7698 if (disk
->index
== -1) {
7699 remove_disk_super(super
,
7704 /* release allocate disk structure */
7705 __free_imsm_disk(disk_cfg
);
7708 return check_degraded
;
7712 static int apply_reshape_migration_update(struct imsm_update_reshape_migration
*u
,
7713 struct intel_super
*super
,
7716 struct intel_dev
*id
;
7717 void **tofree
= NULL
;
7720 dprintf("apply_reshape_migration_update()\n");
7721 if ((u
->subdev
< 0) ||
7723 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
7726 if ((space_list
== NULL
) || (*space_list
== NULL
)) {
7727 dprintf("imsm: Error: Memory is not allocated\n");
7731 for (id
= super
->devlist
; id
; id
= id
->next
) {
7732 if (id
->index
== (unsigned)u
->subdev
) {
7733 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
7734 struct imsm_map
*map
;
7735 struct imsm_dev
*new_dev
=
7736 (struct imsm_dev
*)*space_list
;
7737 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
7739 struct dl
*new_disk
;
7741 if (new_dev
== NULL
)
7743 *space_list
= **space_list
;
7744 memcpy(new_dev
, dev
, sizeof_imsm_dev(dev
, 0));
7745 map
= get_imsm_map(new_dev
, MAP_0
);
7747 dprintf("imsm: Error: migration in progress");
7751 to_state
= map
->map_state
;
7752 if ((u
->new_level
== 5) && (map
->raid_level
== 0)) {
7754 /* this should not happen */
7755 if (u
->new_disks
[0] < 0) {
7756 map
->failed_disk_num
=
7757 map
->num_members
- 1;
7758 to_state
= IMSM_T_STATE_DEGRADED
;
7760 to_state
= IMSM_T_STATE_NORMAL
;
7762 migrate(new_dev
, super
, to_state
, MIGR_GEN_MIGR
);
7763 if (u
->new_level
> -1)
7764 map
->raid_level
= u
->new_level
;
7765 migr_map
= get_imsm_map(new_dev
, MAP_1
);
7766 if ((u
->new_level
== 5) &&
7767 (migr_map
->raid_level
== 0)) {
7768 int ord
= map
->num_members
- 1;
7769 migr_map
->num_members
--;
7770 if (u
->new_disks
[0] < 0)
7771 ord
|= IMSM_ORD_REBUILD
;
7772 set_imsm_ord_tbl_ent(map
,
7773 map
->num_members
- 1,
7777 tofree
= (void **)dev
;
7779 /* update chunk size
7781 if (u
->new_chunksize
> 0)
7782 map
->blocks_per_strip
=
7783 __cpu_to_le16(u
->new_chunksize
* 2);
7787 if ((u
->new_level
!= 5) ||
7788 (migr_map
->raid_level
!= 0) ||
7789 (migr_map
->raid_level
== map
->raid_level
))
7792 if (u
->new_disks
[0] >= 0) {
7795 new_disk
= get_disk_super(super
,
7796 major(u
->new_disks
[0]),
7797 minor(u
->new_disks
[0]));
7798 dprintf("imsm: new disk for reshape is: %i:%i "
7799 "(%p, index = %i)\n",
7800 major(u
->new_disks
[0]),
7801 minor(u
->new_disks
[0]),
7802 new_disk
, new_disk
->index
);
7803 if (new_disk
== NULL
)
7804 goto error_disk_add
;
7806 new_disk
->index
= map
->num_members
- 1;
7807 /* slot to fill in autolayout
7809 new_disk
->raiddisk
= new_disk
->index
;
7810 new_disk
->disk
.status
|= CONFIGURED_DISK
;
7811 new_disk
->disk
.status
&= ~SPARE_DISK
;
7813 goto error_disk_add
;
7816 *tofree
= *space_list
;
7817 /* calculate new size
7819 imsm_set_array_size(new_dev
);
7826 *space_list
= tofree
;
7830 dprintf("Error: imsm: Cannot find disk.\n");
7834 static int apply_update_activate_spare(struct imsm_update_activate_spare
*u
,
7835 struct intel_super
*super
,
7836 struct active_array
*active_array
)
7838 struct imsm_super
*mpb
= super
->anchor
;
7839 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
7840 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7841 struct imsm_map
*migr_map
;
7842 struct active_array
*a
;
7843 struct imsm_disk
*disk
;
7850 int second_map_created
= 0;
7852 for (; u
; u
= u
->next
) {
7853 victim
= get_imsm_disk_idx(dev
, u
->slot
, MAP_X
);
7858 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7863 fprintf(stderr
, "error: imsm_activate_spare passed "
7864 "an unknown disk (index: %d)\n",
7869 /* count failures (excluding rebuilds and the victim)
7870 * to determine map[0] state
7873 for (i
= 0; i
< map
->num_members
; i
++) {
7876 disk
= get_imsm_disk(super
,
7877 get_imsm_disk_idx(dev
, i
, MAP_X
));
7878 if (!disk
|| is_failed(disk
))
7882 /* adding a pristine spare, assign a new index */
7883 if (dl
->index
< 0) {
7884 dl
->index
= super
->anchor
->num_disks
;
7885 super
->anchor
->num_disks
++;
7888 disk
->status
|= CONFIGURED_DISK
;
7889 disk
->status
&= ~SPARE_DISK
;
7892 to_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7893 if (!second_map_created
) {
7894 second_map_created
= 1;
7895 map
->map_state
= IMSM_T_STATE_DEGRADED
;
7896 migrate(dev
, super
, to_state
, MIGR_REBUILD
);
7898 map
->map_state
= to_state
;
7899 migr_map
= get_imsm_map(dev
, MAP_1
);
7900 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
7901 set_imsm_ord_tbl_ent(migr_map
, u
->slot
,
7902 dl
->index
| IMSM_ORD_REBUILD
);
7904 /* update the family_num to mark a new container
7905 * generation, being careful to record the existing
7906 * family_num in orig_family_num to clean up after
7907 * earlier mdadm versions that neglected to set it.
7909 if (mpb
->orig_family_num
== 0)
7910 mpb
->orig_family_num
= mpb
->family_num
;
7911 mpb
->family_num
+= super
->random
;
7913 /* count arrays using the victim in the metadata */
7915 for (a
= active_array
; a
; a
= a
->next
) {
7916 dev
= get_imsm_dev(super
, a
->info
.container_member
);
7917 map
= get_imsm_map(dev
, MAP_0
);
7919 if (get_imsm_disk_slot(map
, victim
) >= 0)
7923 /* delete the victim if it is no longer being
7929 /* We know that 'manager' isn't touching anything,
7930 * so it is safe to delete
7932 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
7933 if ((*dlp
)->index
== victim
)
7936 /* victim may be on the missing list */
7938 for (dlp
= &super
->missing
; *dlp
;
7939 dlp
= &(*dlp
)->next
)
7940 if ((*dlp
)->index
== victim
)
7942 imsm_delete(super
, dlp
, victim
);
7949 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
7950 struct intel_super
*super
,
7953 struct dl
*new_disk
;
7954 struct intel_dev
*id
;
7956 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
7957 int disk_count
= u
->old_raid_disks
;
7958 void **tofree
= NULL
;
7959 int devices_to_reshape
= 1;
7960 struct imsm_super
*mpb
= super
->anchor
;
7962 unsigned int dev_id
;
7964 dprintf("imsm: apply_reshape_container_disks_update()\n");
7966 /* enable spares to use in array */
7967 for (i
= 0; i
< delta_disks
; i
++) {
7968 new_disk
= get_disk_super(super
,
7969 major(u
->new_disks
[i
]),
7970 minor(u
->new_disks
[i
]));
7971 dprintf("imsm: new disk for reshape is: %i:%i "
7972 "(%p, index = %i)\n",
7973 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
7974 new_disk
, new_disk
->index
);
7975 if ((new_disk
== NULL
) ||
7976 ((new_disk
->index
>= 0) &&
7977 (new_disk
->index
< u
->old_raid_disks
)))
7978 goto update_reshape_exit
;
7979 new_disk
->index
= disk_count
++;
7980 /* slot to fill in autolayout
7982 new_disk
->raiddisk
= new_disk
->index
;
7983 new_disk
->disk
.status
|=
7985 new_disk
->disk
.status
&= ~SPARE_DISK
;
7988 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
7989 mpb
->num_raid_devs
);
7990 /* manage changes in volume
7992 for (dev_id
= 0; dev_id
< mpb
->num_raid_devs
; dev_id
++) {
7993 void **sp
= *space_list
;
7994 struct imsm_dev
*newdev
;
7995 struct imsm_map
*newmap
, *oldmap
;
7997 for (id
= super
->devlist
; id
; id
= id
->next
) {
7998 if (id
->index
== dev_id
)
8007 /* Copy the dev, but not (all of) the map */
8008 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
8009 oldmap
= get_imsm_map(id
->dev
, MAP_0
);
8010 newmap
= get_imsm_map(newdev
, MAP_0
);
8011 /* Copy the current map */
8012 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8013 /* update one device only
8015 if (devices_to_reshape
) {
8016 dprintf("imsm: modifying subdev: %i\n",
8018 devices_to_reshape
--;
8019 newdev
->vol
.migr_state
= 1;
8020 newdev
->vol
.curr_migr_unit
= 0;
8021 set_migr_type(newdev
, MIGR_GEN_MIGR
);
8022 newmap
->num_members
= u
->new_raid_disks
;
8023 for (i
= 0; i
< delta_disks
; i
++) {
8024 set_imsm_ord_tbl_ent(newmap
,
8025 u
->old_raid_disks
+ i
,
8026 u
->old_raid_disks
+ i
);
8028 /* New map is correct, now need to save old map
8030 newmap
= get_imsm_map(newdev
, MAP_1
);
8031 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8033 imsm_set_array_size(newdev
);
8036 sp
= (void **)id
->dev
;
8041 /* Clear migration record */
8042 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
8045 *space_list
= tofree
;
8048 update_reshape_exit
:
8053 static int apply_takeover_update(struct imsm_update_takeover
*u
,
8054 struct intel_super
*super
,
8057 struct imsm_dev
*dev
= NULL
;
8058 struct intel_dev
*dv
;
8059 struct imsm_dev
*dev_new
;
8060 struct imsm_map
*map
;
8064 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
8065 if (dv
->index
== (unsigned int)u
->subarray
) {
8073 map
= get_imsm_map(dev
, MAP_0
);
8075 if (u
->direction
== R10_TO_R0
) {
8076 /* Number of failed disks must be half of initial disk number */
8077 if (imsm_count_failed(super
, dev
, MAP_0
) !=
8078 (map
->num_members
/ 2))
8081 /* iterate through devices to mark removed disks as spare */
8082 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
8083 if (dm
->disk
.status
& FAILED_DISK
) {
8084 int idx
= dm
->index
;
8085 /* update indexes on the disk list */
8086 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
8087 the index values will end up being correct.... NB */
8088 for (du
= super
->disks
; du
; du
= du
->next
)
8089 if (du
->index
> idx
)
8091 /* mark as spare disk */
8096 map
->num_members
= map
->num_members
/ 2;
8097 map
->map_state
= IMSM_T_STATE_NORMAL
;
8098 map
->num_domains
= 1;
8099 map
->raid_level
= 0;
8100 map
->failed_disk_num
= -1;
8103 if (u
->direction
== R0_TO_R10
) {
8105 /* update slots in current disk list */
8106 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
8110 /* create new *missing* disks */
8111 for (i
= 0; i
< map
->num_members
; i
++) {
8112 space
= *space_list
;
8115 *space_list
= *space
;
8117 memcpy(du
, super
->disks
, sizeof(*du
));
8121 du
->index
= (i
* 2) + 1;
8122 sprintf((char *)du
->disk
.serial
,
8123 " MISSING_%d", du
->index
);
8124 sprintf((char *)du
->serial
,
8125 "MISSING_%d", du
->index
);
8126 du
->next
= super
->missing
;
8127 super
->missing
= du
;
8129 /* create new dev and map */
8130 space
= *space_list
;
8133 *space_list
= *space
;
8134 dev_new
= (void *)space
;
8135 memcpy(dev_new
, dev
, sizeof(*dev
));
8136 /* update new map */
8137 map
= get_imsm_map(dev_new
, MAP_0
);
8138 map
->num_members
= map
->num_members
* 2;
8139 map
->map_state
= IMSM_T_STATE_DEGRADED
;
8140 map
->num_domains
= 2;
8141 map
->raid_level
= 1;
8142 /* replace dev<->dev_new */
8145 /* update disk order table */
8146 for (du
= super
->disks
; du
; du
= du
->next
)
8148 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
8149 for (du
= super
->missing
; du
; du
= du
->next
)
8150 if (du
->index
>= 0) {
8151 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
8152 mark_missing(dv
->dev
, &du
->disk
, du
->index
);
8158 static void imsm_process_update(struct supertype
*st
,
8159 struct metadata_update
*update
)
8162 * crack open the metadata_update envelope to find the update record
8163 * update can be one of:
8164 * update_reshape_container_disks - all the arrays in the container
8165 * are being reshaped to have more devices. We need to mark
8166 * the arrays for general migration and convert selected spares
8167 * into active devices.
8168 * update_activate_spare - a spare device has replaced a failed
8169 * device in an array, update the disk_ord_tbl. If this disk is
8170 * present in all member arrays then also clear the SPARE_DISK
8172 * update_create_array
8174 * update_rename_array
8175 * update_add_remove_disk
8177 struct intel_super
*super
= st
->sb
;
8178 struct imsm_super
*mpb
;
8179 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
8181 /* update requires a larger buf but the allocation failed */
8182 if (super
->next_len
&& !super
->next_buf
) {
8183 super
->next_len
= 0;
8187 if (super
->next_buf
) {
8188 memcpy(super
->next_buf
, super
->buf
, super
->len
);
8190 super
->len
= super
->next_len
;
8191 super
->buf
= super
->next_buf
;
8193 super
->next_len
= 0;
8194 super
->next_buf
= NULL
;
8197 mpb
= super
->anchor
;
8200 case update_general_migration_checkpoint
: {
8201 struct intel_dev
*id
;
8202 struct imsm_update_general_migration_checkpoint
*u
=
8203 (void *)update
->buf
;
8205 dprintf("imsm: process_update() "
8206 "for update_general_migration_checkpoint called\n");
8208 /* find device under general migration */
8209 for (id
= super
->devlist
; id
; id
= id
->next
) {
8210 if (is_gen_migration(id
->dev
)) {
8211 id
->dev
->vol
.curr_migr_unit
=
8212 __cpu_to_le32(u
->curr_migr_unit
);
8213 super
->updates_pending
++;
8218 case update_takeover
: {
8219 struct imsm_update_takeover
*u
= (void *)update
->buf
;
8220 if (apply_takeover_update(u
, super
, &update
->space_list
)) {
8221 imsm_update_version_info(super
);
8222 super
->updates_pending
++;
8227 case update_reshape_container_disks
: {
8228 struct imsm_update_reshape
*u
= (void *)update
->buf
;
8229 if (apply_reshape_container_disks_update(
8230 u
, super
, &update
->space_list
))
8231 super
->updates_pending
++;
8234 case update_reshape_migration
: {
8235 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
8236 if (apply_reshape_migration_update(
8237 u
, super
, &update
->space_list
))
8238 super
->updates_pending
++;
8241 case update_activate_spare
: {
8242 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
8243 if (apply_update_activate_spare(u
, super
, st
->arrays
))
8244 super
->updates_pending
++;
8247 case update_create_array
: {
8248 /* someone wants to create a new array, we need to be aware of
8249 * a few races/collisions:
8250 * 1/ 'Create' called by two separate instances of mdadm
8251 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
8252 * devices that have since been assimilated via
8254 * In the event this update can not be carried out mdadm will
8255 * (FIX ME) notice that its update did not take hold.
8257 struct imsm_update_create_array
*u
= (void *) update
->buf
;
8258 struct intel_dev
*dv
;
8259 struct imsm_dev
*dev
;
8260 struct imsm_map
*map
, *new_map
;
8261 unsigned long long start
, end
;
8262 unsigned long long new_start
, new_end
;
8264 struct disk_info
*inf
;
8267 /* handle racing creates: first come first serve */
8268 if (u
->dev_idx
< mpb
->num_raid_devs
) {
8269 dprintf("%s: subarray %d already defined\n",
8270 __func__
, u
->dev_idx
);
8274 /* check update is next in sequence */
8275 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
8276 dprintf("%s: can not create array %d expected index %d\n",
8277 __func__
, u
->dev_idx
, mpb
->num_raid_devs
);
8281 new_map
= get_imsm_map(&u
->dev
, MAP_0
);
8282 new_start
= __le32_to_cpu(new_map
->pba_of_lba0
);
8283 new_end
= new_start
+ __le32_to_cpu(new_map
->blocks_per_member
);
8284 inf
= get_disk_info(u
);
8286 /* handle activate_spare versus create race:
8287 * check to make sure that overlapping arrays do not include
8290 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8291 dev
= get_imsm_dev(super
, i
);
8292 map
= get_imsm_map(dev
, MAP_0
);
8293 start
= __le32_to_cpu(map
->pba_of_lba0
);
8294 end
= start
+ __le32_to_cpu(map
->blocks_per_member
);
8295 if ((new_start
>= start
&& new_start
<= end
) ||
8296 (start
>= new_start
&& start
<= new_end
))
8301 if (disks_overlap(super
, i
, u
)) {
8302 dprintf("%s: arrays overlap\n", __func__
);
8307 /* check that prepare update was successful */
8308 if (!update
->space
) {
8309 dprintf("%s: prepare update failed\n", __func__
);
8313 /* check that all disks are still active before committing
8314 * changes. FIXME: could we instead handle this by creating a
8315 * degraded array? That's probably not what the user expects,
8316 * so better to drop this update on the floor.
8318 for (i
= 0; i
< new_map
->num_members
; i
++) {
8319 dl
= serial_to_dl(inf
[i
].serial
, super
);
8321 dprintf("%s: disk disappeared\n", __func__
);
8326 super
->updates_pending
++;
8328 /* convert spares to members and fixup ord_tbl */
8329 for (i
= 0; i
< new_map
->num_members
; i
++) {
8330 dl
= serial_to_dl(inf
[i
].serial
, super
);
8331 if (dl
->index
== -1) {
8332 dl
->index
= mpb
->num_disks
;
8334 dl
->disk
.status
|= CONFIGURED_DISK
;
8335 dl
->disk
.status
&= ~SPARE_DISK
;
8337 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
8342 update
->space
= NULL
;
8343 imsm_copy_dev(dev
, &u
->dev
);
8344 dv
->index
= u
->dev_idx
;
8345 dv
->next
= super
->devlist
;
8346 super
->devlist
= dv
;
8347 mpb
->num_raid_devs
++;
8349 imsm_update_version_info(super
);
8352 /* mdmon knows how to release update->space, but not
8353 * ((struct intel_dev *) update->space)->dev
8355 if (update
->space
) {
8361 case update_kill_array
: {
8362 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
8363 int victim
= u
->dev_idx
;
8364 struct active_array
*a
;
8365 struct intel_dev
**dp
;
8366 struct imsm_dev
*dev
;
8368 /* sanity check that we are not affecting the uuid of
8369 * active arrays, or deleting an active array
8371 * FIXME when immutable ids are available, but note that
8372 * we'll also need to fixup the invalidated/active
8373 * subarray indexes in mdstat
8375 for (a
= st
->arrays
; a
; a
= a
->next
)
8376 if (a
->info
.container_member
>= victim
)
8378 /* by definition if mdmon is running at least one array
8379 * is active in the container, so checking
8380 * mpb->num_raid_devs is just extra paranoia
8382 dev
= get_imsm_dev(super
, victim
);
8383 if (a
|| !dev
|| mpb
->num_raid_devs
== 1) {
8384 dprintf("failed to delete subarray-%d\n", victim
);
8388 for (dp
= &super
->devlist
; *dp
;)
8389 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
8392 if ((*dp
)->index
> (unsigned)victim
)
8396 mpb
->num_raid_devs
--;
8397 super
->updates_pending
++;
8400 case update_rename_array
: {
8401 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
8402 char name
[MAX_RAID_SERIAL_LEN
+1];
8403 int target
= u
->dev_idx
;
8404 struct active_array
*a
;
8405 struct imsm_dev
*dev
;
8407 /* sanity check that we are not affecting the uuid of
8410 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
8411 name
[MAX_RAID_SERIAL_LEN
] = '\0';
8412 for (a
= st
->arrays
; a
; a
= a
->next
)
8413 if (a
->info
.container_member
== target
)
8415 dev
= get_imsm_dev(super
, u
->dev_idx
);
8416 if (a
|| !dev
|| !check_name(super
, name
, 1)) {
8417 dprintf("failed to rename subarray-%d\n", target
);
8421 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
8422 super
->updates_pending
++;
8425 case update_add_remove_disk
: {
8426 /* we may be able to repair some arrays if disks are
8427 * being added, check teh status of add_remove_disk
8428 * if discs has been added.
8430 if (add_remove_disk_update(super
)) {
8431 struct active_array
*a
;
8433 super
->updates_pending
++;
8434 for (a
= st
->arrays
; a
; a
= a
->next
)
8435 a
->check_degraded
= 1;
8440 fprintf(stderr
, "error: unsuported process update type:"
8441 "(type: %d)\n", type
);
8445 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
);
8447 static void imsm_prepare_update(struct supertype
*st
,
8448 struct metadata_update
*update
)
8451 * Allocate space to hold new disk entries, raid-device entries or a new
8452 * mpb if necessary. The manager synchronously waits for updates to
8453 * complete in the monitor, so new mpb buffers allocated here can be
8454 * integrated by the monitor thread without worrying about live pointers
8455 * in the manager thread.
8457 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
8458 struct intel_super
*super
= st
->sb
;
8459 struct imsm_super
*mpb
= super
->anchor
;
8464 case update_general_migration_checkpoint
:
8465 dprintf("imsm: prepare_update() "
8466 "for update_general_migration_checkpoint called\n");
8468 case update_takeover
: {
8469 struct imsm_update_takeover
*u
= (void *)update
->buf
;
8470 if (u
->direction
== R0_TO_R10
) {
8471 void **tail
= (void **)&update
->space_list
;
8472 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subarray
);
8473 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8474 int num_members
= map
->num_members
;
8478 /* allocate memory for added disks */
8479 for (i
= 0; i
< num_members
; i
++) {
8480 size
= sizeof(struct dl
);
8481 space
= malloc(size
);
8490 /* allocate memory for new device */
8491 size
= sizeof_imsm_dev(super
->devlist
->dev
, 0) +
8492 (num_members
* sizeof(__u32
));
8493 space
= malloc(size
);
8502 len
= disks_to_mpb_size(num_members
* 2);
8504 /* if allocation didn't success, free buffer */
8505 while (update
->space_list
) {
8506 void **sp
= update
->space_list
;
8507 update
->space_list
= *sp
;
8515 case update_reshape_container_disks
: {
8516 /* Every raid device in the container is about to
8517 * gain some more devices, and we will enter a
8519 * So each 'imsm_map' will be bigger, and the imsm_vol
8520 * will now hold 2 of them.
8521 * Thus we need new 'struct imsm_dev' allocations sized
8522 * as sizeof_imsm_dev but with more devices in both maps.
8524 struct imsm_update_reshape
*u
= (void *)update
->buf
;
8525 struct intel_dev
*dl
;
8526 void **space_tail
= (void**)&update
->space_list
;
8528 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
8530 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
8531 int size
= sizeof_imsm_dev(dl
->dev
, 1);
8533 if (u
->new_raid_disks
> u
->old_raid_disks
)
8534 size
+= sizeof(__u32
)*2*
8535 (u
->new_raid_disks
- u
->old_raid_disks
);
8544 len
= disks_to_mpb_size(u
->new_raid_disks
);
8545 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
8548 case update_reshape_migration
: {
8549 /* for migration level 0->5 we need to add disks
8550 * so the same as for container operation we will copy
8551 * device to the bigger location.
8552 * in memory prepared device and new disk area are prepared
8553 * for usage in process update
8555 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
8556 struct intel_dev
*id
;
8557 void **space_tail
= (void **)&update
->space_list
;
8560 int current_level
= -1;
8562 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
8564 /* add space for bigger array in update
8566 for (id
= super
->devlist
; id
; id
= id
->next
) {
8567 if (id
->index
== (unsigned)u
->subdev
) {
8568 size
= sizeof_imsm_dev(id
->dev
, 1);
8569 if (u
->new_raid_disks
> u
->old_raid_disks
)
8570 size
+= sizeof(__u32
)*2*
8571 (u
->new_raid_disks
- u
->old_raid_disks
);
8581 if (update
->space_list
== NULL
)
8584 /* add space for disk in update
8586 size
= sizeof(struct dl
);
8589 free(update
->space_list
);
8590 update
->space_list
= NULL
;
8597 /* add spare device to update
8599 for (id
= super
->devlist
; id
; id
= id
->next
)
8600 if (id
->index
== (unsigned)u
->subdev
) {
8601 struct imsm_dev
*dev
;
8602 struct imsm_map
*map
;
8604 dev
= get_imsm_dev(super
, u
->subdev
);
8605 map
= get_imsm_map(dev
, MAP_0
);
8606 current_level
= map
->raid_level
;
8609 if ((u
->new_level
== 5) && (u
->new_level
!= current_level
)) {
8610 struct mdinfo
*spares
;
8612 spares
= get_spares_for_grow(st
);
8620 makedev(dev
->disk
.major
,
8622 dl
= get_disk_super(super
,
8625 dl
->index
= u
->old_raid_disks
;
8631 len
= disks_to_mpb_size(u
->new_raid_disks
);
8632 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
8635 case update_create_array
: {
8636 struct imsm_update_create_array
*u
= (void *) update
->buf
;
8637 struct intel_dev
*dv
;
8638 struct imsm_dev
*dev
= &u
->dev
;
8639 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8641 struct disk_info
*inf
;
8645 inf
= get_disk_info(u
);
8646 len
= sizeof_imsm_dev(dev
, 1);
8647 /* allocate a new super->devlist entry */
8648 dv
= malloc(sizeof(*dv
));
8650 dv
->dev
= malloc(len
);
8655 update
->space
= NULL
;
8659 /* count how many spares will be converted to members */
8660 for (i
= 0; i
< map
->num_members
; i
++) {
8661 dl
= serial_to_dl(inf
[i
].serial
, super
);
8663 /* hmm maybe it failed?, nothing we can do about
8668 if (count_memberships(dl
, super
) == 0)
8671 len
+= activate
* sizeof(struct imsm_disk
);
8678 /* check if we need a larger metadata buffer */
8679 if (super
->next_buf
)
8680 buf_len
= super
->next_len
;
8682 buf_len
= super
->len
;
8684 if (__le32_to_cpu(mpb
->mpb_size
) + len
> buf_len
) {
8685 /* ok we need a larger buf than what is currently allocated
8686 * if this allocation fails process_update will notice that
8687 * ->next_len is set and ->next_buf is NULL
8689 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + len
, 512);
8690 if (super
->next_buf
)
8691 free(super
->next_buf
);
8693 super
->next_len
= buf_len
;
8694 if (posix_memalign(&super
->next_buf
, 512, buf_len
) == 0)
8695 memset(super
->next_buf
, 0, buf_len
);
8697 super
->next_buf
= NULL
;
8701 /* must be called while manager is quiesced */
8702 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
8704 struct imsm_super
*mpb
= super
->anchor
;
8706 struct imsm_dev
*dev
;
8707 struct imsm_map
*map
;
8708 int i
, j
, num_members
;
8711 dprintf("%s: deleting device[%d] from imsm_super\n",
8714 /* shift all indexes down one */
8715 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
8716 if (iter
->index
> (int)index
)
8718 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
8719 if (iter
->index
> (int)index
)
8722 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8723 dev
= get_imsm_dev(super
, i
);
8724 map
= get_imsm_map(dev
, MAP_0
);
8725 num_members
= map
->num_members
;
8726 for (j
= 0; j
< num_members
; j
++) {
8727 /* update ord entries being careful not to propagate
8728 * ord-flags to the first map
8730 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_X
);
8732 if (ord_to_idx(ord
) <= index
)
8735 map
= get_imsm_map(dev
, MAP_0
);
8736 set_imsm_ord_tbl_ent(map
, j
, ord_to_idx(ord
- 1));
8737 map
= get_imsm_map(dev
, MAP_1
);
8739 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
8744 super
->updates_pending
++;
8746 struct dl
*dl
= *dlp
;
8748 *dlp
= (*dlp
)->next
;
8749 __free_imsm_disk(dl
);
8752 #endif /* MDASSEMBLE */
8754 static void close_targets(int *targets
, int new_disks
)
8761 for (i
= 0; i
< new_disks
; i
++) {
8762 if (targets
[i
] >= 0) {
8769 static int imsm_get_allowed_degradation(int level
, int raid_disks
,
8770 struct intel_super
*super
,
8771 struct imsm_dev
*dev
)
8777 struct imsm_map
*map
;
8780 ret_val
= raid_disks
/2;
8781 /* check map if all disks pairs not failed
8784 map
= get_imsm_map(dev
, MAP_0
);
8785 for (i
= 0; i
< ret_val
; i
++) {
8786 int degradation
= 0;
8787 if (get_imsm_disk(super
, i
) == NULL
)
8789 if (get_imsm_disk(super
, i
+ 1) == NULL
)
8791 if (degradation
== 2)
8794 map
= get_imsm_map(dev
, MAP_1
);
8795 /* if there is no second map
8796 * result can be returned
8800 /* check degradation in second map
8802 for (i
= 0; i
< ret_val
; i
++) {
8803 int degradation
= 0;
8804 if (get_imsm_disk(super
, i
) == NULL
)
8806 if (get_imsm_disk(super
, i
+ 1) == NULL
)
8808 if (degradation
== 2)
8823 /*******************************************************************************
8824 * Function: open_backup_targets
8825 * Description: Function opens file descriptors for all devices given in
8828 * info : general array info
8829 * raid_disks : number of disks
8830 * raid_fds : table of device's file descriptors
8831 * super : intel super for raid10 degradation check
8832 * dev : intel device for raid10 degradation check
8836 ******************************************************************************/
8837 int open_backup_targets(struct mdinfo
*info
, int raid_disks
, int *raid_fds
,
8838 struct intel_super
*super
, struct imsm_dev
*dev
)
8844 for (i
= 0; i
< raid_disks
; i
++)
8847 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
8850 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
)) {
8851 dprintf("disk is faulty!!\n");
8855 if ((sd
->disk
.raid_disk
>= raid_disks
) ||
8856 (sd
->disk
.raid_disk
< 0))
8859 dn
= map_dev(sd
->disk
.major
,
8861 raid_fds
[sd
->disk
.raid_disk
] = dev_open(dn
, O_RDWR
);
8862 if (raid_fds
[sd
->disk
.raid_disk
] < 0) {
8863 fprintf(stderr
, "cannot open component\n");
8868 /* check if maximum array degradation level is not exceeded
8870 if ((raid_disks
- opened
) >
8871 imsm_get_allowed_degradation(info
->new_level
,
8874 fprintf(stderr
, "Not enough disks can be opened.\n");
8875 close_targets(raid_fds
, raid_disks
);
8882 /*******************************************************************************
8883 * Function: init_migr_record_imsm
8884 * Description: Function inits imsm migration record
8886 * super : imsm internal array info
8887 * dev : device under migration
8888 * info : general array info to find the smallest device
8891 ******************************************************************************/
8892 void init_migr_record_imsm(struct supertype
*st
, struct imsm_dev
*dev
,
8893 struct mdinfo
*info
)
8895 struct intel_super
*super
= st
->sb
;
8896 struct migr_record
*migr_rec
= super
->migr_rec
;
8898 unsigned long long dsize
, dev_sectors
;
8899 long long unsigned min_dev_sectors
= -1LLU;
8903 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
8904 struct imsm_map
*map_src
= get_imsm_map(dev
, MAP_1
);
8905 unsigned long long num_migr_units
;
8906 unsigned long long array_blocks
;
8908 memset(migr_rec
, 0, sizeof(struct migr_record
));
8909 migr_rec
->family_num
= __cpu_to_le32(super
->anchor
->family_num
);
8911 /* only ascending reshape supported now */
8912 migr_rec
->ascending_migr
= __cpu_to_le32(1);
8914 migr_rec
->dest_depth_per_unit
= GEN_MIGR_AREA_SIZE
/
8915 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
8916 migr_rec
->dest_depth_per_unit
*= map_dest
->blocks_per_strip
;
8917 new_data_disks
= imsm_num_data_members(dev
, MAP_0
);
8918 migr_rec
->blocks_per_unit
=
8919 __cpu_to_le32(migr_rec
->dest_depth_per_unit
* new_data_disks
);
8920 migr_rec
->dest_depth_per_unit
=
8921 __cpu_to_le32(migr_rec
->dest_depth_per_unit
);
8922 array_blocks
= info
->component_size
* new_data_disks
;
8924 array_blocks
/ __le32_to_cpu(migr_rec
->blocks_per_unit
);
8926 if (array_blocks
% __le32_to_cpu(migr_rec
->blocks_per_unit
))
8928 migr_rec
->num_migr_units
= __cpu_to_le32(num_migr_units
);
8930 migr_rec
->post_migr_vol_cap
= dev
->size_low
;
8931 migr_rec
->post_migr_vol_cap_hi
= dev
->size_high
;
8934 /* Find the smallest dev */
8935 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
8936 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
8937 fd
= dev_open(nm
, O_RDONLY
);
8940 get_dev_size(fd
, NULL
, &dsize
);
8941 dev_sectors
= dsize
/ 512;
8942 if (dev_sectors
< min_dev_sectors
)
8943 min_dev_sectors
= dev_sectors
;
8946 migr_rec
->ckpt_area_pba
= __cpu_to_le32(min_dev_sectors
-
8947 RAID_DISK_RESERVED_BLOCKS_IMSM_HI
);
8949 write_imsm_migr_rec(st
);
8954 /*******************************************************************************
8955 * Function: save_backup_imsm
8956 * Description: Function saves critical data stripes to Migration Copy Area
8957 * and updates the current migration unit status.
8958 * Use restore_stripes() to form a destination stripe,
8959 * and to write it to the Copy Area.
8961 * st : supertype information
8962 * dev : imsm device that backup is saved for
8963 * info : general array info
8964 * buf : input buffer
8965 * length : length of data to backup (blocks_per_unit)
8969 ******************************************************************************/
8970 int save_backup_imsm(struct supertype
*st
,
8971 struct imsm_dev
*dev
,
8972 struct mdinfo
*info
,
8977 struct intel_super
*super
= st
->sb
;
8978 unsigned long long *target_offsets
= NULL
;
8979 int *targets
= NULL
;
8981 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
8982 int new_disks
= map_dest
->num_members
;
8983 int dest_layout
= 0;
8985 unsigned long long start
;
8986 int data_disks
= imsm_num_data_members(dev
, MAP_0
);
8988 targets
= malloc(new_disks
* sizeof(int));
8992 for (i
= 0; i
< new_disks
; i
++)
8995 target_offsets
= malloc(new_disks
* sizeof(unsigned long long));
8996 if (!target_offsets
)
8999 start
= info
->reshape_progress
* 512;
9000 for (i
= 0; i
< new_disks
; i
++) {
9001 target_offsets
[i
] = (unsigned long long)
9002 __le32_to_cpu(super
->migr_rec
->ckpt_area_pba
) * 512;
9003 /* move back copy area adderss, it will be moved forward
9004 * in restore_stripes() using start input variable
9006 target_offsets
[i
] -= start
/data_disks
;
9009 if (open_backup_targets(info
, new_disks
, targets
,
9013 dest_layout
= imsm_level_to_layout(map_dest
->raid_level
);
9014 dest_chunk
= __le16_to_cpu(map_dest
->blocks_per_strip
) * 512;
9016 if (restore_stripes(targets
, /* list of dest devices */
9017 target_offsets
, /* migration record offsets */
9020 map_dest
->raid_level
,
9022 -1, /* source backup file descriptor */
9023 0, /* input buf offset
9024 * always 0 buf is already offseted */
9028 fprintf(stderr
, Name
": Error restoring stripes\n");
9036 close_targets(targets
, new_disks
);
9039 free(target_offsets
);
9044 /*******************************************************************************
9045 * Function: save_checkpoint_imsm
9046 * Description: Function called for current unit status update
9047 * in the migration record. It writes it to disk.
9049 * super : imsm internal array info
9050 * info : general array info
9054 * 2: failure, means no valid migration record
9055 * / no general migration in progress /
9056 ******************************************************************************/
9057 int save_checkpoint_imsm(struct supertype
*st
, struct mdinfo
*info
, int state
)
9059 struct intel_super
*super
= st
->sb
;
9060 unsigned long long blocks_per_unit
;
9061 unsigned long long curr_migr_unit
;
9063 if (load_imsm_migr_rec(super
, info
) != 0) {
9064 dprintf("imsm: ERROR: Cannot read migration record "
9065 "for checkpoint save.\n");
9069 blocks_per_unit
= __le32_to_cpu(super
->migr_rec
->blocks_per_unit
);
9070 if (blocks_per_unit
== 0) {
9071 dprintf("imsm: no migration in progress.\n");
9074 curr_migr_unit
= info
->reshape_progress
/ blocks_per_unit
;
9075 /* check if array is alligned to copy area
9076 * if it is not alligned, add one to current migration unit value
9077 * this can happend on array reshape finish only
9079 if (info
->reshape_progress
% blocks_per_unit
)
9082 super
->migr_rec
->curr_migr_unit
=
9083 __cpu_to_le32(curr_migr_unit
);
9084 super
->migr_rec
->rec_status
= __cpu_to_le32(state
);
9085 super
->migr_rec
->dest_1st_member_lba
=
9086 __cpu_to_le32(curr_migr_unit
*
9087 __le32_to_cpu(super
->migr_rec
->dest_depth_per_unit
));
9088 if (write_imsm_migr_rec(st
) < 0) {
9089 dprintf("imsm: Cannot write migration record "
9090 "outside backup area\n");
9097 /*******************************************************************************
9098 * Function: recover_backup_imsm
9099 * Description: Function recovers critical data from the Migration Copy Area
9100 * while assembling an array.
9102 * super : imsm internal array info
9103 * info : general array info
9105 * 0 : success (or there is no data to recover)
9107 ******************************************************************************/
9108 int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
)
9110 struct intel_super
*super
= st
->sb
;
9111 struct migr_record
*migr_rec
= super
->migr_rec
;
9112 struct imsm_map
*map_dest
= NULL
;
9113 struct intel_dev
*id
= NULL
;
9114 unsigned long long read_offset
;
9115 unsigned long long write_offset
;
9117 int *targets
= NULL
;
9118 int new_disks
, i
, err
;
9121 unsigned long curr_migr_unit
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
9122 unsigned long num_migr_units
= __le32_to_cpu(migr_rec
->num_migr_units
);
9124 int skipped_disks
= 0;
9126 err
= sysfs_get_str(info
, NULL
, "array_state", (char *)buffer
, 20);
9130 /* recover data only during assemblation */
9131 if (strncmp(buffer
, "inactive", 8) != 0)
9133 /* no data to recover */
9134 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
9136 if (curr_migr_unit
>= num_migr_units
)
9139 /* find device during reshape */
9140 for (id
= super
->devlist
; id
; id
= id
->next
)
9141 if (is_gen_migration(id
->dev
))
9146 map_dest
= get_imsm_map(id
->dev
, MAP_0
);
9147 new_disks
= map_dest
->num_members
;
9149 read_offset
= (unsigned long long)
9150 __le32_to_cpu(migr_rec
->ckpt_area_pba
) * 512;
9152 write_offset
= ((unsigned long long)
9153 __le32_to_cpu(migr_rec
->dest_1st_member_lba
) +
9154 __le32_to_cpu(map_dest
->pba_of_lba0
)) * 512;
9156 unit_len
= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
9157 if (posix_memalign((void **)&buf
, 512, unit_len
) != 0)
9159 targets
= malloc(new_disks
* sizeof(int));
9163 if (open_backup_targets(info
, new_disks
, targets
, super
, id
->dev
)) {
9165 Name
": Cannot open some devices belonging to array.\n");
9169 for (i
= 0; i
< new_disks
; i
++) {
9170 if (targets
[i
] < 0) {
9174 if (lseek64(targets
[i
], read_offset
, SEEK_SET
) < 0) {
9176 Name
": Cannot seek to block: %s\n",
9181 if ((unsigned)read(targets
[i
], buf
, unit_len
) != unit_len
) {
9183 Name
": Cannot read copy area block: %s\n",
9188 if (lseek64(targets
[i
], write_offset
, SEEK_SET
) < 0) {
9190 Name
": Cannot seek to block: %s\n",
9195 if ((unsigned)write(targets
[i
], buf
, unit_len
) != unit_len
) {
9197 Name
": Cannot restore block: %s\n",
9204 if (skipped_disks
> imsm_get_allowed_degradation(info
->new_level
,
9209 Name
": Cannot restore data from backup."
9210 " Too many failed disks\n");
9214 if (save_checkpoint_imsm(st
, info
, UNIT_SRC_NORMAL
)) {
9215 /* ignore error == 2, this can mean end of reshape here
9217 dprintf("imsm: Cannot write checkpoint to "
9218 "migration record (UNIT_SRC_NORMAL) during restart\n");
9224 for (i
= 0; i
< new_disks
; i
++)
9233 static char disk_by_path
[] = "/dev/disk/by-path/";
9235 static const char *imsm_get_disk_controller_domain(const char *path
)
9237 char disk_path
[PATH_MAX
];
9241 strncpy(disk_path
, disk_by_path
, PATH_MAX
- 1);
9242 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
9243 if (stat(disk_path
, &st
) == 0) {
9244 struct sys_dev
* hba
;
9247 path
= devt_to_devpath(st
.st_rdev
);
9250 hba
= find_disk_attached_hba(-1, path
);
9251 if (hba
&& hba
->type
== SYS_DEV_SAS
)
9253 else if (hba
&& hba
->type
== SYS_DEV_SATA
)
9257 dprintf("path: %s hba: %s attached: %s\n",
9258 path
, (hba
) ? hba
->path
: "NULL", drv
);
9266 static int imsm_find_array_minor_by_subdev(int subdev
, int container
, int *minor
)
9268 char subdev_name
[20];
9269 struct mdstat_ent
*mdstat
;
9271 sprintf(subdev_name
, "%d", subdev
);
9272 mdstat
= mdstat_by_subdev(subdev_name
, container
);
9276 *minor
= mdstat
->devnum
;
9277 free_mdstat(mdstat
);
9281 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
9282 struct geo_params
*geo
,
9283 int *old_raid_disks
)
9285 /* currently we only support increasing the number of devices
9286 * for a container. This increases the number of device for each
9287 * member array. They must all be RAID0 or RAID5.
9290 struct mdinfo
*info
, *member
;
9291 int devices_that_can_grow
= 0;
9293 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): "
9294 "st->devnum = (%i)\n",
9297 if (geo
->size
!= -1 ||
9298 geo
->level
!= UnSet
||
9299 geo
->layout
!= UnSet
||
9300 geo
->chunksize
!= 0 ||
9301 geo
->raid_disks
== UnSet
) {
9302 dprintf("imsm: Container operation is allowed for "
9303 "raid disks number change only.\n");
9307 info
= container_content_imsm(st
, NULL
);
9308 for (member
= info
; member
; member
= member
->next
) {
9312 dprintf("imsm: checking device_num: %i\n",
9313 member
->container_member
);
9315 if (geo
->raid_disks
<= member
->array
.raid_disks
) {
9316 /* we work on container for Online Capacity Expansion
9317 * only so raid_disks has to grow
9319 dprintf("imsm: for container operation raid disks "
9320 "increase is required\n");
9324 if ((info
->array
.level
!= 0) &&
9325 (info
->array
.level
!= 5)) {
9326 /* we cannot use this container with other raid level
9328 dprintf("imsm: for container operation wrong"
9329 " raid level (%i) detected\n",
9333 /* check for platform support
9334 * for this raid level configuration
9336 struct intel_super
*super
= st
->sb
;
9337 if (!is_raid_level_supported(super
->orom
,
9338 member
->array
.level
,
9340 dprintf("platform does not support raid%d with"
9344 geo
->raid_disks
> 1 ? "s" : "");
9347 /* check if component size is aligned to chunk size
9349 if (info
->component_size
%
9350 (info
->array
.chunk_size
/512)) {
9351 dprintf("Component size is not aligned to "
9357 if (*old_raid_disks
&&
9358 info
->array
.raid_disks
!= *old_raid_disks
)
9360 *old_raid_disks
= info
->array
.raid_disks
;
9362 /* All raid5 and raid0 volumes in container
9363 * have to be ready for Online Capacity Expansion
9364 * so they need to be assembled. We have already
9365 * checked that no recovery etc is happening.
9367 result
= imsm_find_array_minor_by_subdev(member
->container_member
,
9371 dprintf("imsm: cannot find array\n");
9374 devices_that_can_grow
++;
9377 if (!member
&& devices_that_can_grow
)
9381 dprintf("\tContainer operation allowed\n");
9383 dprintf("\tError: %i\n", ret_val
);
9388 /* Function: get_spares_for_grow
9389 * Description: Allocates memory and creates list of spare devices
9390 * avaliable in container. Checks if spare drive size is acceptable.
9391 * Parameters: Pointer to the supertype structure
9392 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
9395 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
9397 unsigned long long min_size
= min_acceptable_spare_size_imsm(st
);
9398 return container_choose_spares(st
, min_size
, NULL
, NULL
, NULL
, 0);
9401 /******************************************************************************
9402 * function: imsm_create_metadata_update_for_reshape
9403 * Function creates update for whole IMSM container.
9405 ******************************************************************************/
9406 static int imsm_create_metadata_update_for_reshape(
9407 struct supertype
*st
,
9408 struct geo_params
*geo
,
9410 struct imsm_update_reshape
**updatep
)
9412 struct intel_super
*super
= st
->sb
;
9413 struct imsm_super
*mpb
= super
->anchor
;
9414 int update_memory_size
= 0;
9415 struct imsm_update_reshape
*u
= NULL
;
9416 struct mdinfo
*spares
= NULL
;
9418 int delta_disks
= 0;
9421 dprintf("imsm_update_metadata_for_reshape(enter) raid_disks = %i\n",
9424 delta_disks
= geo
->raid_disks
- old_raid_disks
;
9426 /* size of all update data without anchor */
9427 update_memory_size
= sizeof(struct imsm_update_reshape
);
9429 /* now add space for spare disks that we need to add. */
9430 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
9432 u
= calloc(1, update_memory_size
);
9435 "cannot get memory for imsm_update_reshape update\n");
9438 u
->type
= update_reshape_container_disks
;
9439 u
->old_raid_disks
= old_raid_disks
;
9440 u
->new_raid_disks
= geo
->raid_disks
;
9442 /* now get spare disks list
9444 spares
= get_spares_for_grow(st
);
9447 || delta_disks
> spares
->array
.spare_disks
) {
9448 fprintf(stderr
, Name
": imsm: ERROR: Cannot get spare devices "
9449 "for %s.\n", geo
->dev_name
);
9454 /* we have got spares
9455 * update disk list in imsm_disk list table in anchor
9457 dprintf("imsm: %i spares are available.\n\n",
9458 spares
->array
.spare_disks
);
9461 for (i
= 0; i
< delta_disks
; i
++) {
9466 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
9468 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
9469 dl
->index
= mpb
->num_disks
;
9479 dprintf("imsm: reshape update preparation :");
9480 if (i
== delta_disks
) {
9483 return update_memory_size
;
9486 dprintf(" Error\n");
9491 /******************************************************************************
9492 * function: imsm_create_metadata_update_for_migration()
9493 * Creates update for IMSM array.
9495 ******************************************************************************/
9496 static int imsm_create_metadata_update_for_migration(
9497 struct supertype
*st
,
9498 struct geo_params
*geo
,
9499 struct imsm_update_reshape_migration
**updatep
)
9501 struct intel_super
*super
= st
->sb
;
9502 int update_memory_size
= 0;
9503 struct imsm_update_reshape_migration
*u
= NULL
;
9504 struct imsm_dev
*dev
;
9505 int previous_level
= -1;
9507 dprintf("imsm_create_metadata_update_for_migration(enter)"
9508 " New Level = %i\n", geo
->level
);
9510 /* size of all update data without anchor */
9511 update_memory_size
= sizeof(struct imsm_update_reshape_migration
);
9513 u
= calloc(1, update_memory_size
);
9515 dprintf("error: cannot get memory for "
9516 "imsm_create_metadata_update_for_migration\n");
9519 u
->type
= update_reshape_migration
;
9520 u
->subdev
= super
->current_vol
;
9521 u
->new_level
= geo
->level
;
9522 u
->new_layout
= geo
->layout
;
9523 u
->new_raid_disks
= u
->old_raid_disks
= geo
->raid_disks
;
9524 u
->new_disks
[0] = -1;
9525 u
->new_chunksize
= -1;
9527 dev
= get_imsm_dev(super
, u
->subdev
);
9529 struct imsm_map
*map
;
9531 map
= get_imsm_map(dev
, MAP_0
);
9533 int current_chunk_size
=
9534 __le16_to_cpu(map
->blocks_per_strip
) / 2;
9536 if (geo
->chunksize
!= current_chunk_size
) {
9537 u
->new_chunksize
= geo
->chunksize
/ 1024;
9539 "chunk size change from %i to %i\n",
9540 current_chunk_size
, u
->new_chunksize
);
9542 previous_level
= map
->raid_level
;
9545 if ((geo
->level
== 5) && (previous_level
== 0)) {
9546 struct mdinfo
*spares
= NULL
;
9548 u
->new_raid_disks
++;
9549 spares
= get_spares_for_grow(st
);
9550 if ((spares
== NULL
) || (spares
->array
.spare_disks
< 1)) {
9553 update_memory_size
= 0;
9554 dprintf("error: cannot get spare device "
9555 "for requested migration");
9560 dprintf("imsm: reshape update preparation : OK\n");
9563 return update_memory_size
;
9566 static void imsm_update_metadata_locally(struct supertype
*st
,
9569 struct metadata_update mu
;
9574 mu
.space_list
= NULL
;
9576 imsm_prepare_update(st
, &mu
);
9577 imsm_process_update(st
, &mu
);
9579 while (mu
.space_list
) {
9580 void **space
= mu
.space_list
;
9581 mu
.space_list
= *space
;
9586 /***************************************************************************
9587 * Function: imsm_analyze_change
9588 * Description: Function analyze change for single volume
9589 * and validate if transition is supported
9590 * Parameters: Geometry parameters, supertype structure
9591 * Returns: Operation type code on success, -1 if fail
9592 ****************************************************************************/
9593 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
9594 struct geo_params
*geo
)
9600 /* number of added/removed disks in operation result */
9601 int devNumChange
= 0;
9602 /* imsm compatible layout value for array geometry verification */
9603 int imsm_layout
= -1;
9605 getinfo_super_imsm_volume(st
, &info
, NULL
);
9606 if ((geo
->level
!= info
.array
.level
) &&
9607 (geo
->level
>= 0) &&
9608 (geo
->level
!= UnSet
)) {
9609 switch (info
.array
.level
) {
9611 if (geo
->level
== 5) {
9612 change
= CH_MIGRATION
;
9613 if (geo
->layout
!= ALGORITHM_LEFT_ASYMMETRIC
) {
9615 Name
" Error. Requested Layout "
9616 "not supported (left-asymmetric layout "
9617 "is supported only)!\n");
9619 goto analyse_change_exit
;
9621 imsm_layout
= geo
->layout
;
9623 devNumChange
= 1; /* parity disk added */
9624 } else if (geo
->level
== 10) {
9625 change
= CH_TAKEOVER
;
9627 devNumChange
= 2; /* two mirrors added */
9628 imsm_layout
= 0x102; /* imsm supported layout */
9633 if (geo
->level
== 0) {
9634 change
= CH_TAKEOVER
;
9636 devNumChange
= -(geo
->raid_disks
/2);
9637 imsm_layout
= 0; /* imsm raid0 layout */
9643 Name
" Error. Level Migration from %d to %d "
9645 info
.array
.level
, geo
->level
);
9646 goto analyse_change_exit
;
9649 geo
->level
= info
.array
.level
;
9651 if ((geo
->layout
!= info
.array
.layout
)
9652 && ((geo
->layout
!= UnSet
) && (geo
->layout
!= -1))) {
9653 change
= CH_MIGRATION
;
9654 if ((info
.array
.layout
== 0)
9655 && (info
.array
.level
== 5)
9656 && (geo
->layout
== 5)) {
9657 /* reshape 5 -> 4 */
9658 } else if ((info
.array
.layout
== 5)
9659 && (info
.array
.level
== 5)
9660 && (geo
->layout
== 0)) {
9661 /* reshape 4 -> 5 */
9666 Name
" Error. Layout Migration from %d to %d "
9668 info
.array
.layout
, geo
->layout
);
9670 goto analyse_change_exit
;
9673 geo
->layout
= info
.array
.layout
;
9674 if (imsm_layout
== -1)
9675 imsm_layout
= info
.array
.layout
;
9678 if ((geo
->chunksize
> 0) && (geo
->chunksize
!= UnSet
)
9679 && (geo
->chunksize
!= info
.array
.chunk_size
))
9680 change
= CH_MIGRATION
;
9682 geo
->chunksize
= info
.array
.chunk_size
;
9684 chunk
= geo
->chunksize
/ 1024;
9685 if (!validate_geometry_imsm(st
,
9688 geo
->raid_disks
+ devNumChange
,
9695 struct intel_super
*super
= st
->sb
;
9696 struct imsm_super
*mpb
= super
->anchor
;
9698 if (mpb
->num_raid_devs
> 1) {
9700 Name
" Error. Cannot perform operation on %s"
9701 "- for this operation it MUST be single "
9702 "array in container\n",
9708 analyse_change_exit
:
9713 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
9715 struct intel_super
*super
= st
->sb
;
9716 struct imsm_update_takeover
*u
;
9718 u
= malloc(sizeof(struct imsm_update_takeover
));
9722 u
->type
= update_takeover
;
9723 u
->subarray
= super
->current_vol
;
9725 /* 10->0 transition */
9726 if (geo
->level
== 0)
9727 u
->direction
= R10_TO_R0
;
9729 /* 0->10 transition */
9730 if (geo
->level
== 10)
9731 u
->direction
= R0_TO_R10
;
9733 /* update metadata locally */
9734 imsm_update_metadata_locally(st
, u
,
9735 sizeof(struct imsm_update_takeover
));
9736 /* and possibly remotely */
9737 if (st
->update_tail
)
9738 append_metadata_update(st
, u
,
9739 sizeof(struct imsm_update_takeover
));
9746 static int imsm_reshape_super(struct supertype
*st
, long long size
, int level
,
9747 int layout
, int chunksize
, int raid_disks
,
9748 int delta_disks
, char *backup
, char *dev
,
9752 struct geo_params geo
;
9754 dprintf("imsm: reshape_super called.\n");
9756 memset(&geo
, 0, sizeof(struct geo_params
));
9759 geo
.dev_id
= st
->devnum
;
9762 geo
.layout
= layout
;
9763 geo
.chunksize
= chunksize
;
9764 geo
.raid_disks
= raid_disks
;
9765 if (delta_disks
!= UnSet
)
9766 geo
.raid_disks
+= delta_disks
;
9768 dprintf("\tfor level : %i\n", geo
.level
);
9769 dprintf("\tfor raid_disks : %i\n", geo
.raid_disks
);
9771 if (experimental() == 0)
9774 if (st
->container_dev
== st
->devnum
) {
9775 /* On container level we can only increase number of devices. */
9776 dprintf("imsm: info: Container operation\n");
9777 int old_raid_disks
= 0;
9779 if (imsm_reshape_is_allowed_on_container(
9780 st
, &geo
, &old_raid_disks
)) {
9781 struct imsm_update_reshape
*u
= NULL
;
9784 len
= imsm_create_metadata_update_for_reshape(
9785 st
, &geo
, old_raid_disks
, &u
);
9788 dprintf("imsm: Cannot prepare update\n");
9789 goto exit_imsm_reshape_super
;
9793 /* update metadata locally */
9794 imsm_update_metadata_locally(st
, u
, len
);
9795 /* and possibly remotely */
9796 if (st
->update_tail
)
9797 append_metadata_update(st
, u
, len
);
9802 fprintf(stderr
, Name
": (imsm) Operation "
9803 "is not allowed on this container\n");
9806 /* On volume level we support following operations
9807 * - takeover: raid10 -> raid0; raid0 -> raid10
9808 * - chunk size migration
9809 * - migration: raid5 -> raid0; raid0 -> raid5
9811 struct intel_super
*super
= st
->sb
;
9812 struct intel_dev
*dev
= super
->devlist
;
9814 dprintf("imsm: info: Volume operation\n");
9815 /* find requested device */
9817 if (imsm_find_array_minor_by_subdev(
9818 dev
->index
, st
->container_dev
, &devnum
) == 0
9819 && devnum
== geo
.dev_id
)
9824 fprintf(stderr
, Name
" Cannot find %s (%i) subarray\n",
9825 geo
.dev_name
, geo
.dev_id
);
9826 goto exit_imsm_reshape_super
;
9828 super
->current_vol
= dev
->index
;
9829 change
= imsm_analyze_change(st
, &geo
);
9832 ret_val
= imsm_takeover(st
, &geo
);
9834 case CH_MIGRATION
: {
9835 struct imsm_update_reshape_migration
*u
= NULL
;
9837 imsm_create_metadata_update_for_migration(
9841 "Cannot prepare update\n");
9845 /* update metadata locally */
9846 imsm_update_metadata_locally(st
, u
, len
);
9847 /* and possibly remotely */
9848 if (st
->update_tail
)
9849 append_metadata_update(st
, u
, len
);
9859 exit_imsm_reshape_super
:
9860 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
9864 /*******************************************************************************
9865 * Function: wait_for_reshape_imsm
9866 * Description: Function writes new sync_max value and waits until
9867 * reshape process reach new position
9869 * sra : general array info
9870 * ndata : number of disks in new array's layout
9873 * 1 : there is no reshape in progress,
9875 ******************************************************************************/
9876 int wait_for_reshape_imsm(struct mdinfo
*sra
, int ndata
)
9878 int fd
= sysfs_get_fd(sra
, NULL
, "reshape_position");
9879 unsigned long long completed
;
9880 /* to_complete : new sync_max position */
9881 unsigned long long to_complete
= sra
->reshape_progress
;
9882 unsigned long long position_to_set
= to_complete
/ ndata
;
9885 dprintf("imsm: wait_for_reshape_imsm() "
9886 "cannot open reshape_position\n");
9890 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
9891 dprintf("imsm: wait_for_reshape_imsm() "
9892 "cannot read reshape_position (no reshape in progres)\n");
9897 if (completed
> to_complete
) {
9898 dprintf("imsm: wait_for_reshape_imsm() "
9899 "wrong next position to set %llu (%llu)\n",
9900 to_complete
, completed
);
9904 dprintf("Position set: %llu\n", position_to_set
);
9905 if (sysfs_set_num(sra
, NULL
, "sync_max",
9906 position_to_set
) != 0) {
9907 dprintf("imsm: wait_for_reshape_imsm() "
9908 "cannot set reshape position to %llu\n",
9919 select(fd
+1, &rfds
, NULL
, NULL
, NULL
);
9920 if (sysfs_get_str(sra
, NULL
, "sync_action",
9922 strncmp(action
, "reshape", 7) != 0)
9924 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
9925 dprintf("imsm: wait_for_reshape_imsm() "
9926 "cannot read reshape_position (in loop)\n");
9930 } while (completed
< to_complete
);
9936 /*******************************************************************************
9937 * Function: check_degradation_change
9938 * Description: Check that array hasn't become failed.
9940 * info : for sysfs access
9941 * sources : source disks descriptors
9942 * degraded: previous degradation level
9945 ******************************************************************************/
9946 int check_degradation_change(struct mdinfo
*info
,
9950 unsigned long long new_degraded
;
9951 sysfs_get_ll(info
, NULL
, "degraded", &new_degraded
);
9952 if (new_degraded
!= (unsigned long long)degraded
) {
9953 /* check each device to ensure it is still working */
9956 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
9957 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
9959 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
9961 if (sysfs_get_str(info
,
9962 sd
, "state", sbuf
, 20) < 0 ||
9963 strstr(sbuf
, "faulty") ||
9964 strstr(sbuf
, "in_sync") == NULL
) {
9965 /* this device is dead */
9966 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
9967 if (sd
->disk
.raid_disk
>= 0 &&
9968 sources
[sd
->disk
.raid_disk
] >= 0) {
9970 sd
->disk
.raid_disk
]);
9971 sources
[sd
->disk
.raid_disk
] =
9980 return new_degraded
;
9983 /*******************************************************************************
9984 * Function: imsm_manage_reshape
9985 * Description: Function finds array under reshape and it manages reshape
9986 * process. It creates stripes backups (if required) and sets
9989 * afd : Backup handle (nattive) - not used
9990 * sra : general array info
9991 * reshape : reshape parameters - not used
9992 * st : supertype structure
9993 * blocks : size of critical section [blocks]
9994 * fds : table of source device descriptor
9995 * offsets : start of array (offest per devices)
9997 * destfd : table of destination device descriptor
9998 * destoffsets : table of destination offsets (per device)
10000 * 1 : success, reshape is done
10002 ******************************************************************************/
10003 static int imsm_manage_reshape(
10004 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
10005 struct supertype
*st
, unsigned long backup_blocks
,
10006 int *fds
, unsigned long long *offsets
,
10007 int dests
, int *destfd
, unsigned long long *destoffsets
)
10010 struct intel_super
*super
= st
->sb
;
10011 struct intel_dev
*dv
= NULL
;
10012 struct imsm_dev
*dev
= NULL
;
10013 struct imsm_map
*map_src
;
10014 int migr_vol_qan
= 0;
10015 int ndata
, odata
; /* [bytes] */
10016 int chunk
; /* [bytes] */
10017 struct migr_record
*migr_rec
;
10019 unsigned int buf_size
; /* [bytes] */
10020 unsigned long long max_position
; /* array size [bytes] */
10021 unsigned long long next_step
; /* [blocks]/[bytes] */
10022 unsigned long long old_data_stripe_length
;
10023 unsigned long long start_src
; /* [bytes] */
10024 unsigned long long start
; /* [bytes] */
10025 unsigned long long start_buf_shift
; /* [bytes] */
10027 int source_layout
= 0;
10029 if (!fds
|| !offsets
|| !sra
)
10032 /* Find volume during the reshape */
10033 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
10034 if (dv
->dev
->vol
.migr_type
== MIGR_GEN_MIGR
10035 && dv
->dev
->vol
.migr_state
== 1) {
10040 /* Only one volume can migrate at the same time */
10041 if (migr_vol_qan
!= 1) {
10042 fprintf(stderr
, Name
" : %s", migr_vol_qan
?
10043 "Number of migrating volumes greater than 1\n" :
10044 "There is no volume during migrationg\n");
10048 map_src
= get_imsm_map(dev
, MAP_1
);
10049 if (map_src
== NULL
)
10052 ndata
= imsm_num_data_members(dev
, MAP_0
);
10053 odata
= imsm_num_data_members(dev
, MAP_1
);
10055 chunk
= __le16_to_cpu(map_src
->blocks_per_strip
) * 512;
10056 old_data_stripe_length
= odata
* chunk
;
10058 migr_rec
= super
->migr_rec
;
10060 /* initialize migration record for start condition */
10061 if (sra
->reshape_progress
== 0)
10062 init_migr_record_imsm(st
, dev
, sra
);
10064 if (__le32_to_cpu(migr_rec
->rec_status
) != UNIT_SRC_NORMAL
) {
10065 dprintf("imsm: cannot restart migration when data "
10066 "are present in copy area.\n");
10071 /* size for data */
10072 buf_size
= __le32_to_cpu(migr_rec
->blocks_per_unit
) * 512;
10073 /* extend buffer size for parity disk */
10074 buf_size
+= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
10075 /* add space for stripe aligment */
10076 buf_size
+= old_data_stripe_length
;
10077 if (posix_memalign((void **)&buf
, 4096, buf_size
)) {
10078 dprintf("imsm: Cannot allocate checpoint buffer\n");
10082 max_position
= sra
->component_size
* ndata
;
10083 source_layout
= imsm_level_to_layout(map_src
->raid_level
);
10085 while (__le32_to_cpu(migr_rec
->curr_migr_unit
) <
10086 __le32_to_cpu(migr_rec
->num_migr_units
)) {
10087 /* current reshape position [blocks] */
10088 unsigned long long current_position
=
10089 __le32_to_cpu(migr_rec
->blocks_per_unit
)
10090 * __le32_to_cpu(migr_rec
->curr_migr_unit
);
10091 unsigned long long border
;
10093 /* Check that array hasn't become failed.
10095 degraded
= check_degradation_change(sra
, fds
, degraded
);
10096 if (degraded
> 1) {
10097 dprintf("imsm: Abort reshape due to degradation"
10098 " level (%i)\n", degraded
);
10102 next_step
= __le32_to_cpu(migr_rec
->blocks_per_unit
);
10104 if ((current_position
+ next_step
) > max_position
)
10105 next_step
= max_position
- current_position
;
10107 start
= current_position
* 512;
10109 /* allign reading start to old geometry */
10110 start_buf_shift
= start
% old_data_stripe_length
;
10111 start_src
= start
- start_buf_shift
;
10113 border
= (start_src
/ odata
) - (start
/ ndata
);
10115 if (border
<= __le32_to_cpu(migr_rec
->dest_depth_per_unit
)) {
10116 /* save critical stripes to buf
10117 * start - start address of current unit
10118 * to backup [bytes]
10119 * start_src - start address of current unit
10120 * to backup alligned to source array
10123 unsigned long long next_step_filler
= 0;
10124 unsigned long long copy_length
= next_step
* 512;
10126 /* allign copy area length to stripe in old geometry */
10127 next_step_filler
= ((copy_length
+ start_buf_shift
)
10128 % old_data_stripe_length
);
10129 if (next_step_filler
)
10130 next_step_filler
= (old_data_stripe_length
10131 - next_step_filler
);
10132 dprintf("save_stripes() parameters: start = %llu,"
10133 "\tstart_src = %llu,\tnext_step*512 = %llu,"
10134 "\tstart_in_buf_shift = %llu,"
10135 "\tnext_step_filler = %llu\n",
10136 start
, start_src
, copy_length
,
10137 start_buf_shift
, next_step_filler
);
10139 if (save_stripes(fds
, offsets
, map_src
->num_members
,
10140 chunk
, map_src
->raid_level
,
10141 source_layout
, 0, NULL
, start_src
,
10143 next_step_filler
+ start_buf_shift
,
10145 dprintf("imsm: Cannot save stripes"
10149 /* Convert data to destination format and store it
10150 * in backup general migration area
10152 if (save_backup_imsm(st
, dev
, sra
,
10153 buf
+ start_buf_shift
, copy_length
)) {
10154 dprintf("imsm: Cannot save stripes to "
10155 "target devices\n");
10158 if (save_checkpoint_imsm(st
, sra
,
10159 UNIT_SRC_IN_CP_AREA
)) {
10160 dprintf("imsm: Cannot write checkpoint to "
10161 "migration record (UNIT_SRC_IN_CP_AREA)\n");
10165 /* set next step to use whole border area */
10166 border
/= next_step
;
10168 next_step
*= border
;
10170 /* When data backed up, checkpoint stored,
10171 * kick the kernel to reshape unit of data
10173 next_step
= next_step
+ sra
->reshape_progress
;
10174 /* limit next step to array max position */
10175 if (next_step
> max_position
)
10176 next_step
= max_position
;
10177 sysfs_set_num(sra
, NULL
, "suspend_lo", sra
->reshape_progress
);
10178 sysfs_set_num(sra
, NULL
, "suspend_hi", next_step
);
10179 sra
->reshape_progress
= next_step
;
10181 /* wait until reshape finish */
10182 if (wait_for_reshape_imsm(sra
, ndata
) < 0) {
10183 dprintf("wait_for_reshape_imsm returned error!\n");
10187 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
10188 /* ignore error == 2, this can mean end of reshape here
10190 dprintf("imsm: Cannot write checkpoint to "
10191 "migration record (UNIT_SRC_NORMAL)\n");
10197 /* return '1' if done */
10201 abort_reshape(sra
);
10205 #endif /* MDASSEMBLE */
10207 struct superswitch super_imsm
= {
10209 .examine_super
= examine_super_imsm
,
10210 .brief_examine_super
= brief_examine_super_imsm
,
10211 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
10212 .export_examine_super
= export_examine_super_imsm
,
10213 .detail_super
= detail_super_imsm
,
10214 .brief_detail_super
= brief_detail_super_imsm
,
10215 .write_init_super
= write_init_super_imsm
,
10216 .validate_geometry
= validate_geometry_imsm
,
10217 .add_to_super
= add_to_super_imsm
,
10218 .remove_from_super
= remove_from_super_imsm
,
10219 .detail_platform
= detail_platform_imsm
,
10220 .kill_subarray
= kill_subarray_imsm
,
10221 .update_subarray
= update_subarray_imsm
,
10222 .load_container
= load_container_imsm
,
10223 .default_geometry
= default_geometry_imsm
,
10224 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
10225 .reshape_super
= imsm_reshape_super
,
10226 .manage_reshape
= imsm_manage_reshape
,
10227 .recover_backup
= recover_backup_imsm
,
10229 .match_home
= match_home_imsm
,
10230 .uuid_from_super
= uuid_from_super_imsm
,
10231 .getinfo_super
= getinfo_super_imsm
,
10232 .getinfo_super_disks
= getinfo_super_disks_imsm
,
10233 .update_super
= update_super_imsm
,
10235 .avail_size
= avail_size_imsm
,
10236 .min_acceptable_spare_size
= min_acceptable_spare_size_imsm
,
10238 .compare_super
= compare_super_imsm
,
10240 .load_super
= load_super_imsm
,
10241 .init_super
= init_super_imsm
,
10242 .store_super
= store_super_imsm
,
10243 .free_super
= free_super_imsm
,
10244 .match_metadata_desc
= match_metadata_desc_imsm
,
10245 .container_content
= container_content_imsm
,
10253 .open_new
= imsm_open_new
,
10254 .set_array_state
= imsm_set_array_state
,
10255 .set_disk
= imsm_set_disk
,
10256 .sync_metadata
= imsm_sync_metadata
,
10257 .activate_spare
= imsm_activate_spare
,
10258 .process_update
= imsm_process_update
,
10259 .prepare_update
= imsm_prepare_update
,
10260 #endif /* MDASSEMBLE */