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 */
2687 /* in the missing/failed disk case check to see
2688 * if at least one array is runnable
2690 max_enough
= max(max_enough
, enough
);
2692 dprintf("%s: enough: %d\n", __func__
, max_enough
);
2693 info
->container_enough
= max_enough
;
2696 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
2698 disk
= &super
->disks
->disk
;
2699 info
->data_offset
= __le32_to_cpu(disk
->total_blocks
) - reserved
;
2700 info
->component_size
= reserved
;
2701 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
2702 /* we don't change info->disk.raid_disk here because
2703 * this state will be finalized in mdmon after we have
2704 * found the 'most fresh' version of the metadata
2706 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
2707 info
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
2710 /* only call uuid_from_super_imsm when this disk is part of a populated container,
2711 * ->compare_super may have updated the 'num_raid_devs' field for spares
2713 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
2714 uuid_from_super_imsm(st
, info
->uuid
);
2716 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
2718 /* I don't know how to compute 'map' on imsm, so use safe default */
2721 for (i
= 0; i
< map_disks
; i
++)
2727 /* allocates memory and fills disk in mdinfo structure
2728 * for each disk in array */
2729 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
2731 struct mdinfo
*mddev
= NULL
;
2732 struct intel_super
*super
= st
->sb
;
2733 struct imsm_disk
*disk
;
2736 if (!super
|| !super
->disks
)
2739 mddev
= malloc(sizeof(*mddev
));
2741 fprintf(stderr
, Name
": Failed to allocate memory.\n");
2744 memset(mddev
, 0, sizeof(*mddev
));
2748 tmp
= malloc(sizeof(*tmp
));
2750 fprintf(stderr
, Name
": Failed to allocate memory.\n");
2755 memset(tmp
, 0, sizeof(*tmp
));
2757 tmp
->next
= mddev
->devs
;
2759 tmp
->disk
.number
= count
++;
2760 tmp
->disk
.major
= dl
->major
;
2761 tmp
->disk
.minor
= dl
->minor
;
2762 tmp
->disk
.state
= is_configured(disk
) ?
2763 (1 << MD_DISK_ACTIVE
) : 0;
2764 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
2765 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
2766 tmp
->disk
.raid_disk
= -1;
2772 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
2773 char *update
, char *devname
, int verbose
,
2774 int uuid_set
, char *homehost
)
2776 /* For 'assemble' and 'force' we need to return non-zero if any
2777 * change was made. For others, the return value is ignored.
2778 * Update options are:
2779 * force-one : This device looks a bit old but needs to be included,
2780 * update age info appropriately.
2781 * assemble: clear any 'faulty' flag to allow this device to
2783 * force-array: Array is degraded but being forced, mark it clean
2784 * if that will be needed to assemble it.
2786 * newdev: not used ????
2787 * grow: Array has gained a new device - this is currently for
2789 * resync: mark as dirty so a resync will happen.
2790 * name: update the name - preserving the homehost
2791 * uuid: Change the uuid of the array to match watch is given
2793 * Following are not relevant for this imsm:
2794 * sparc2.2 : update from old dodgey metadata
2795 * super-minor: change the preferred_minor number
2796 * summaries: update redundant counters.
2797 * homehost: update the recorded homehost
2798 * _reshape_progress: record new reshape_progress position.
2801 struct intel_super
*super
= st
->sb
;
2802 struct imsm_super
*mpb
;
2804 /* we can only update container info */
2805 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
2808 mpb
= super
->anchor
;
2810 if (strcmp(update
, "uuid") == 0) {
2811 /* We take this to mean that the family_num should be updated.
2812 * However that is much smaller than the uuid so we cannot really
2813 * allow an explicit uuid to be given. And it is hard to reliably
2815 * So if !uuid_set we know the current uuid is random and just used
2816 * the first 'int' and copy it to the other 3 positions.
2817 * Otherwise we require the 4 'int's to be the same as would be the
2818 * case if we are using a random uuid. So an explicit uuid will be
2819 * accepted as long as all for ints are the same... which shouldn't hurt
2822 info
->uuid
[1] = info
->uuid
[2] = info
->uuid
[3] = info
->uuid
[0];
2825 if (info
->uuid
[0] != info
->uuid
[1] ||
2826 info
->uuid
[1] != info
->uuid
[2] ||
2827 info
->uuid
[2] != info
->uuid
[3])
2833 mpb
->orig_family_num
= info
->uuid
[0];
2834 } else if (strcmp(update
, "assemble") == 0)
2839 /* successful update? recompute checksum */
2841 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
2846 static size_t disks_to_mpb_size(int disks
)
2850 size
= sizeof(struct imsm_super
);
2851 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
2852 size
+= 2 * sizeof(struct imsm_dev
);
2853 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
2854 size
+= (4 - 2) * sizeof(struct imsm_map
);
2855 /* 4 possible disk_ord_tbl's */
2856 size
+= 4 * (disks
- 1) * sizeof(__u32
);
2861 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
)
2863 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
2866 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
2869 static void free_devlist(struct intel_super
*super
)
2871 struct intel_dev
*dv
;
2873 while (super
->devlist
) {
2874 dv
= super
->devlist
->next
;
2875 free(super
->devlist
->dev
);
2876 free(super
->devlist
);
2877 super
->devlist
= dv
;
2881 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
2883 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
2886 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
)
2890 * 0 same, or first was empty, and second was copied
2891 * 1 second had wrong number
2893 * 3 wrong other info
2895 struct intel_super
*first
= st
->sb
;
2896 struct intel_super
*sec
= tst
->sb
;
2903 /* in platform dependent environment test if the disks
2904 * use the same Intel hba
2906 if (!check_env("IMSM_NO_PLATFORM")) {
2907 if (!first
->hba
|| !sec
->hba
||
2908 (first
->hba
->type
!= sec
->hba
->type
)) {
2910 "HBAs of devices does not match %s != %s\n",
2911 first
->hba
? get_sys_dev_type(first
->hba
->type
) : NULL
,
2912 sec
->hba
? get_sys_dev_type(sec
->hba
->type
) : NULL
);
2917 /* if an anchor does not have num_raid_devs set then it is a free
2920 if (first
->anchor
->num_raid_devs
> 0 &&
2921 sec
->anchor
->num_raid_devs
> 0) {
2922 /* Determine if these disks might ever have been
2923 * related. Further disambiguation can only take place
2924 * in load_super_imsm_all
2926 __u32 first_family
= first
->anchor
->orig_family_num
;
2927 __u32 sec_family
= sec
->anchor
->orig_family_num
;
2929 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
2930 MAX_SIGNATURE_LENGTH
) != 0)
2933 if (first_family
== 0)
2934 first_family
= first
->anchor
->family_num
;
2935 if (sec_family
== 0)
2936 sec_family
= sec
->anchor
->family_num
;
2938 if (first_family
!= sec_family
)
2944 /* if 'first' is a spare promote it to a populated mpb with sec's
2947 if (first
->anchor
->num_raid_devs
== 0 &&
2948 sec
->anchor
->num_raid_devs
> 0) {
2950 struct intel_dev
*dv
;
2951 struct imsm_dev
*dev
;
2953 /* we need to copy raid device info from sec if an allocation
2954 * fails here we don't associate the spare
2956 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++) {
2957 dv
= malloc(sizeof(*dv
));
2960 dev
= malloc(sizeof_imsm_dev(get_imsm_dev(sec
, i
), 1));
2967 dv
->next
= first
->devlist
;
2968 first
->devlist
= dv
;
2970 if (i
< sec
->anchor
->num_raid_devs
) {
2971 /* allocation failure */
2972 free_devlist(first
);
2973 fprintf(stderr
, "imsm: failed to associate spare\n");
2976 first
->anchor
->num_raid_devs
= sec
->anchor
->num_raid_devs
;
2977 first
->anchor
->orig_family_num
= sec
->anchor
->orig_family_num
;
2978 first
->anchor
->family_num
= sec
->anchor
->family_num
;
2979 memcpy(first
->anchor
->sig
, sec
->anchor
->sig
, MAX_SIGNATURE_LENGTH
);
2980 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++)
2981 imsm_copy_dev(get_imsm_dev(first
, i
), get_imsm_dev(sec
, i
));
2987 static void fd2devname(int fd
, char *name
)
2991 char dname
[PATH_MAX
];
2996 if (fstat(fd
, &st
) != 0)
2998 sprintf(path
, "/sys/dev/block/%d:%d",
2999 major(st
.st_rdev
), minor(st
.st_rdev
));
3001 rv
= readlink(path
, dname
, sizeof(dname
)-1);
3006 nm
= strrchr(dname
, '/');
3009 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
3013 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
3015 static int imsm_read_serial(int fd
, char *devname
,
3016 __u8 serial
[MAX_RAID_SERIAL_LEN
])
3018 unsigned char scsi_serial
[255];
3027 memset(scsi_serial
, 0, sizeof(scsi_serial
));
3029 rv
= scsi_get_serial(fd
, scsi_serial
, sizeof(scsi_serial
));
3031 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
3032 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3033 fd2devname(fd
, (char *) serial
);
3040 Name
": Failed to retrieve serial for %s\n",
3045 rsp_len
= scsi_serial
[3];
3049 Name
": Failed to retrieve serial for %s\n",
3053 rsp_buf
= (char *) &scsi_serial
[4];
3055 /* trim all whitespace and non-printable characters and convert
3058 for (i
= 0, dest
= rsp_buf
; i
< rsp_len
; i
++) {
3061 /* ':' is reserved for use in placeholder serial
3062 * numbers for missing disks
3070 len
= dest
- rsp_buf
;
3073 /* truncate leading characters */
3074 if (len
> MAX_RAID_SERIAL_LEN
) {
3075 dest
+= len
- MAX_RAID_SERIAL_LEN
;
3076 len
= MAX_RAID_SERIAL_LEN
;
3079 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3080 memcpy(serial
, dest
, len
);
3085 static int serialcmp(__u8
*s1
, __u8
*s2
)
3087 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
3090 static void serialcpy(__u8
*dest
, __u8
*src
)
3092 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
3095 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
3099 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3100 if (serialcmp(dl
->serial
, serial
) == 0)
3106 static struct imsm_disk
*
3107 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
3111 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3112 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3114 if (serialcmp(disk
->serial
, serial
) == 0) {
3125 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3127 struct imsm_disk
*disk
;
3132 __u8 serial
[MAX_RAID_SERIAL_LEN
];
3134 rv
= imsm_read_serial(fd
, devname
, serial
);
3139 dl
= calloc(1, sizeof(*dl
));
3143 Name
": failed to allocate disk buffer for %s\n",
3149 dl
->major
= major(stb
.st_rdev
);
3150 dl
->minor
= minor(stb
.st_rdev
);
3151 dl
->next
= super
->disks
;
3152 dl
->fd
= keep_fd
? fd
: -1;
3153 assert(super
->disks
== NULL
);
3155 serialcpy(dl
->serial
, serial
);
3158 fd2devname(fd
, name
);
3160 dl
->devname
= strdup(devname
);
3162 dl
->devname
= strdup(name
);
3164 /* look up this disk's index in the current anchor */
3165 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
3168 /* only set index on disks that are a member of a
3169 * populated contianer, i.e. one with raid_devs
3171 if (is_failed(&dl
->disk
))
3173 else if (is_spare(&dl
->disk
))
3181 /* When migrating map0 contains the 'destination' state while map1
3182 * contains the current state. When not migrating map0 contains the
3183 * current state. This routine assumes that map[0].map_state is set to
3184 * the current array state before being called.
3186 * Migration is indicated by one of the following states
3187 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
3188 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
3189 * map1state=unitialized)
3190 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
3192 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
3193 * map1state=degraded)
3194 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
3197 static void migrate(struct imsm_dev
*dev
, struct intel_super
*super
,
3198 __u8 to_state
, int migr_type
)
3200 struct imsm_map
*dest
;
3201 struct imsm_map
*src
= get_imsm_map(dev
, MAP_0
);
3203 dev
->vol
.migr_state
= 1;
3204 set_migr_type(dev
, migr_type
);
3205 dev
->vol
.curr_migr_unit
= 0;
3206 dest
= get_imsm_map(dev
, MAP_1
);
3208 /* duplicate and then set the target end state in map[0] */
3209 memcpy(dest
, src
, sizeof_imsm_map(src
));
3210 if ((migr_type
== MIGR_REBUILD
) ||
3211 (migr_type
== MIGR_GEN_MIGR
)) {
3215 for (i
= 0; i
< src
->num_members
; i
++) {
3216 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
3217 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
3221 if (migr_type
== MIGR_GEN_MIGR
)
3222 /* Clear migration record */
3223 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
3225 src
->map_state
= to_state
;
3228 static void end_migration(struct imsm_dev
*dev
, struct intel_super
*super
,
3231 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3232 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
== 0 ?
3236 /* merge any IMSM_ORD_REBUILD bits that were not successfully
3237 * completed in the last migration.
3239 * FIXME add support for raid-level-migration
3241 if ((map_state
!= map
->map_state
) && (is_gen_migration(dev
) == 0) &&
3242 (prev
->map_state
!= IMSM_T_STATE_UNINITIALIZED
)) {
3243 /* when final map state is other than expected
3244 * merge maps (not for migration)
3248 for (i
= 0; i
< prev
->num_members
; i
++)
3249 for (j
= 0; j
< map
->num_members
; j
++)
3250 /* during online capacity expansion
3251 * disks position can be changed
3252 * if takeover is used
3254 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
3255 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
3256 map
->disk_ord_tbl
[j
] |=
3257 prev
->disk_ord_tbl
[i
];
3260 failed
= imsm_count_failed(super
, dev
, MAP_0
);
3261 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
3264 dev
->vol
.migr_state
= 0;
3265 set_migr_type(dev
, 0);
3266 dev
->vol
.curr_migr_unit
= 0;
3267 map
->map_state
= map_state
;
3271 static int parse_raid_devices(struct intel_super
*super
)
3274 struct imsm_dev
*dev_new
;
3275 size_t len
, len_migr
;
3277 size_t space_needed
= 0;
3278 struct imsm_super
*mpb
= super
->anchor
;
3280 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3281 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3282 struct intel_dev
*dv
;
3284 len
= sizeof_imsm_dev(dev_iter
, 0);
3285 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
3287 space_needed
+= len_migr
- len
;
3289 dv
= malloc(sizeof(*dv
));
3292 if (max_len
< len_migr
)
3294 if (max_len
> len_migr
)
3295 space_needed
+= max_len
- len_migr
;
3296 dev_new
= malloc(max_len
);
3301 imsm_copy_dev(dev_new
, dev_iter
);
3304 dv
->next
= super
->devlist
;
3305 super
->devlist
= dv
;
3308 /* ensure that super->buf is large enough when all raid devices
3311 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
3314 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
, 512);
3315 if (posix_memalign(&buf
, 512, len
) != 0)
3318 memcpy(buf
, super
->buf
, super
->len
);
3319 memset(buf
+ super
->len
, 0, len
- super
->len
);
3328 /* retrieve a pointer to the bbm log which starts after all raid devices */
3329 struct bbm_log
*__get_imsm_bbm_log(struct imsm_super
*mpb
)
3333 if (__le32_to_cpu(mpb
->bbm_log_size
)) {
3335 ptr
+= mpb
->mpb_size
- __le32_to_cpu(mpb
->bbm_log_size
);
3341 /*******************************************************************************
3342 * Function: check_mpb_migr_compatibility
3343 * Description: Function checks for unsupported migration features:
3344 * - migration optimization area (pba_of_lba0)
3345 * - descending reshape (ascending_migr)
3347 * super : imsm metadata information
3349 * 0 : migration is compatible
3350 * -1 : migration is not compatible
3351 ******************************************************************************/
3352 int check_mpb_migr_compatibility(struct intel_super
*super
)
3354 struct imsm_map
*map0
, *map1
;
3355 struct migr_record
*migr_rec
= super
->migr_rec
;
3358 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3359 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3362 dev_iter
->vol
.migr_state
== 1 &&
3363 dev_iter
->vol
.migr_type
== MIGR_GEN_MIGR
) {
3364 /* This device is migrating */
3365 map0
= get_imsm_map(dev_iter
, MAP_0
);
3366 map1
= get_imsm_map(dev_iter
, MAP_1
);
3367 if (map0
->pba_of_lba0
!= map1
->pba_of_lba0
)
3368 /* migration optimization area was used */
3370 if (migr_rec
->ascending_migr
== 0
3371 && migr_rec
->dest_depth_per_unit
> 0)
3372 /* descending reshape not supported yet */
3379 static void __free_imsm(struct intel_super
*super
, int free_disks
);
3381 /* load_imsm_mpb - read matrix metadata
3382 * allocates super->mpb to be freed by free_imsm
3384 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
3386 unsigned long long dsize
;
3387 unsigned long long sectors
;
3389 struct imsm_super
*anchor
;
3392 get_dev_size(fd
, NULL
, &dsize
);
3396 Name
": %s: device to small for imsm\n",
3401 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0) {
3403 fprintf(stderr
, Name
3404 ": Cannot seek to anchor block on %s: %s\n",
3405 devname
, strerror(errno
));
3409 if (posix_memalign((void**)&anchor
, 512, 512) != 0) {
3412 Name
": Failed to allocate imsm anchor buffer"
3413 " on %s\n", devname
);
3416 if (read(fd
, anchor
, 512) != 512) {
3419 Name
": Cannot read anchor block on %s: %s\n",
3420 devname
, strerror(errno
));
3425 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
3428 Name
": no IMSM anchor on %s\n", devname
);
3433 __free_imsm(super
, 0);
3434 /* reload capability and hba */
3436 /* capability and hba must be updated with new super allocation */
3437 find_intel_hba_capability(fd
, super
, devname
);
3438 super
->len
= ROUND_UP(anchor
->mpb_size
, 512);
3439 if (posix_memalign(&super
->buf
, 512, super
->len
) != 0) {
3442 Name
": unable to allocate %zu byte mpb buffer\n",
3447 memcpy(super
->buf
, anchor
, 512);
3449 sectors
= mpb_sectors(anchor
) - 1;
3452 if (posix_memalign(&super
->migr_rec_buf
, 512, MIGR_REC_BUF_SIZE
) != 0) {
3453 fprintf(stderr
, Name
3454 ": %s could not allocate migr_rec buffer\n", __func__
);
3460 check_sum
= __gen_imsm_checksum(super
->anchor
);
3461 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
3464 Name
": IMSM checksum %x != %x on %s\n",
3466 __le32_to_cpu(super
->anchor
->check_sum
),
3474 /* read the extended mpb */
3475 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0) {
3478 Name
": Cannot seek to extended mpb on %s: %s\n",
3479 devname
, strerror(errno
));
3483 if ((unsigned)read(fd
, super
->buf
+ 512, super
->len
- 512) != super
->len
- 512) {
3486 Name
": Cannot read extended mpb on %s: %s\n",
3487 devname
, strerror(errno
));
3491 check_sum
= __gen_imsm_checksum(super
->anchor
);
3492 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
3495 Name
": IMSM checksum %x != %x on %s\n",
3496 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
3501 /* FIXME the BBM log is disk specific so we cannot use this global
3502 * buffer for all disks. Ok for now since we only look at the global
3503 * bbm_log_size parameter to gate assembly
3505 super
->bbm_log
= __get_imsm_bbm_log(super
->anchor
);
3510 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
);
3513 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3517 err
= load_imsm_mpb(fd
, super
, devname
);
3520 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
3523 err
= parse_raid_devices(super
);
3528 static void __free_imsm_disk(struct dl
*d
)
3540 static void free_imsm_disks(struct intel_super
*super
)
3544 while (super
->disks
) {
3546 super
->disks
= d
->next
;
3547 __free_imsm_disk(d
);
3549 while (super
->disk_mgmt_list
) {
3550 d
= super
->disk_mgmt_list
;
3551 super
->disk_mgmt_list
= d
->next
;
3552 __free_imsm_disk(d
);
3554 while (super
->missing
) {
3556 super
->missing
= d
->next
;
3557 __free_imsm_disk(d
);
3562 /* free all the pieces hanging off of a super pointer */
3563 static void __free_imsm(struct intel_super
*super
, int free_disks
)
3565 struct intel_hba
*elem
, *next
;
3571 /* unlink capability description */
3573 if (super
->migr_rec_buf
) {
3574 free(super
->migr_rec_buf
);
3575 super
->migr_rec_buf
= NULL
;
3578 free_imsm_disks(super
);
3579 free_devlist(super
);
3583 free((void *)elem
->path
);
3591 static void free_imsm(struct intel_super
*super
)
3593 __free_imsm(super
, 1);
3597 static void free_super_imsm(struct supertype
*st
)
3599 struct intel_super
*super
= st
->sb
;
3608 static struct intel_super
*alloc_super(void)
3610 struct intel_super
*super
= malloc(sizeof(*super
));
3613 memset(super
, 0, sizeof(*super
));
3614 super
->current_vol
= -1;
3615 super
->create_offset
= ~((__u32
) 0);
3621 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
3623 static int find_intel_hba_capability(int fd
, struct intel_super
*super
, char *devname
)
3625 struct sys_dev
*hba_name
;
3628 if ((fd
< 0) || check_env("IMSM_NO_PLATFORM")) {
3633 hba_name
= find_disk_attached_hba(fd
, NULL
);
3637 Name
": %s is not attached to Intel(R) RAID controller.\n",
3641 rv
= attach_hba_to_super(super
, hba_name
);
3644 struct intel_hba
*hba
= super
->hba
;
3646 fprintf(stderr
, Name
": %s is attached to Intel(R) %s RAID "
3647 "controller (%s),\n"
3648 " but the container is assigned to Intel(R) "
3649 "%s RAID controller (",
3652 hba_name
->pci_id
? : "Err!",
3653 get_sys_dev_type(hba_name
->type
));
3656 fprintf(stderr
, "%s", hba
->pci_id
? : "Err!");
3658 fprintf(stderr
, ", ");
3662 fprintf(stderr
, ").\n"
3663 " Mixing devices attached to different controllers "
3664 "is not allowed.\n");
3666 free_sys_dev(&hba_name
);
3669 super
->orom
= find_imsm_capability(hba_name
->type
);
3670 free_sys_dev(&hba_name
);
3676 /* find_missing - helper routine for load_super_imsm_all that identifies
3677 * disks that have disappeared from the system. This routine relies on
3678 * the mpb being uptodate, which it is at load time.
3680 static int find_missing(struct intel_super
*super
)
3683 struct imsm_super
*mpb
= super
->anchor
;
3685 struct imsm_disk
*disk
;
3687 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3688 disk
= __get_imsm_disk(mpb
, i
);
3689 dl
= serial_to_dl(disk
->serial
, super
);
3693 dl
= malloc(sizeof(*dl
));
3699 dl
->devname
= strdup("missing");
3701 serialcpy(dl
->serial
, disk
->serial
);
3704 dl
->next
= super
->missing
;
3705 super
->missing
= dl
;
3712 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
3714 struct intel_disk
*idisk
= disk_list
;
3717 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
3719 idisk
= idisk
->next
;
3725 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
3726 struct intel_super
*super
,
3727 struct intel_disk
**disk_list
)
3729 struct imsm_disk
*d
= &super
->disks
->disk
;
3730 struct imsm_super
*mpb
= super
->anchor
;
3733 for (i
= 0; i
< tbl_size
; i
++) {
3734 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
3735 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
3737 if (tbl_mpb
->family_num
== mpb
->family_num
) {
3738 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
3739 dprintf("%s: mpb from %d:%d matches %d:%d\n",
3740 __func__
, super
->disks
->major
,
3741 super
->disks
->minor
,
3742 table
[i
]->disks
->major
,
3743 table
[i
]->disks
->minor
);
3747 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
3748 is_configured(d
) == is_configured(tbl_d
)) &&
3749 tbl_mpb
->generation_num
< mpb
->generation_num
) {
3750 /* current version of the mpb is a
3751 * better candidate than the one in
3752 * super_table, but copy over "cross
3753 * generational" status
3755 struct intel_disk
*idisk
;
3757 dprintf("%s: mpb from %d:%d replaces %d:%d\n",
3758 __func__
, super
->disks
->major
,
3759 super
->disks
->minor
,
3760 table
[i
]->disks
->major
,
3761 table
[i
]->disks
->minor
);
3763 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
3764 if (idisk
&& is_failed(&idisk
->disk
))
3765 tbl_d
->status
|= FAILED_DISK
;
3768 struct intel_disk
*idisk
;
3769 struct imsm_disk
*disk
;
3771 /* tbl_mpb is more up to date, but copy
3772 * over cross generational status before
3775 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
3776 if (disk
&& is_failed(disk
))
3777 d
->status
|= FAILED_DISK
;
3779 idisk
= disk_list_get(d
->serial
, *disk_list
);
3782 if (disk
&& is_configured(disk
))
3783 idisk
->disk
.status
|= CONFIGURED_DISK
;
3786 dprintf("%s: mpb from %d:%d prefer %d:%d\n",
3787 __func__
, super
->disks
->major
,
3788 super
->disks
->minor
,
3789 table
[i
]->disks
->major
,
3790 table
[i
]->disks
->minor
);
3798 table
[tbl_size
++] = super
;
3802 /* update/extend the merged list of imsm_disk records */
3803 for (j
= 0; j
< mpb
->num_disks
; j
++) {
3804 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
3805 struct intel_disk
*idisk
;
3807 idisk
= disk_list_get(disk
->serial
, *disk_list
);
3809 idisk
->disk
.status
|= disk
->status
;
3810 if (is_configured(&idisk
->disk
) ||
3811 is_failed(&idisk
->disk
))
3812 idisk
->disk
.status
&= ~(SPARE_DISK
);
3814 idisk
= calloc(1, sizeof(*idisk
));
3817 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
3818 idisk
->disk
= *disk
;
3819 idisk
->next
= *disk_list
;
3823 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
3830 static struct intel_super
*
3831 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
3834 struct imsm_super
*mpb
= super
->anchor
;
3838 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3839 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3840 struct intel_disk
*idisk
;
3842 idisk
= disk_list_get(disk
->serial
, disk_list
);
3844 if (idisk
->owner
== owner
||
3845 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
3848 dprintf("%s: '%.16s' owner %d != %d\n",
3849 __func__
, disk
->serial
, idisk
->owner
,
3852 dprintf("%s: unknown disk %x [%d]: %.16s\n",
3853 __func__
, __le32_to_cpu(mpb
->family_num
), i
,
3859 if (ok_count
== mpb
->num_disks
)
3864 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
3866 struct intel_super
*s
;
3868 for (s
= super_list
; s
; s
= s
->next
) {
3869 if (family_num
!= s
->anchor
->family_num
)
3871 fprintf(stderr
, "Conflict, offlining family %#x on '%s'\n",
3872 __le32_to_cpu(family_num
), s
->disks
->devname
);
3876 static struct intel_super
*
3877 imsm_thunderdome(struct intel_super
**super_list
, int len
)
3879 struct intel_super
*super_table
[len
];
3880 struct intel_disk
*disk_list
= NULL
;
3881 struct intel_super
*champion
, *spare
;
3882 struct intel_super
*s
, **del
;
3887 memset(super_table
, 0, sizeof(super_table
));
3888 for (s
= *super_list
; s
; s
= s
->next
)
3889 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
3891 for (i
= 0; i
< tbl_size
; i
++) {
3892 struct imsm_disk
*d
;
3893 struct intel_disk
*idisk
;
3894 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
3897 d
= &s
->disks
->disk
;
3899 /* 'd' must appear in merged disk list for its
3900 * configuration to be valid
3902 idisk
= disk_list_get(d
->serial
, disk_list
);
3903 if (idisk
&& idisk
->owner
== i
)
3904 s
= validate_members(s
, disk_list
, i
);
3909 dprintf("%s: marking family: %#x from %d:%d offline\n",
3910 __func__
, mpb
->family_num
,
3911 super_table
[i
]->disks
->major
,
3912 super_table
[i
]->disks
->minor
);
3916 /* This is where the mdadm implementation differs from the Windows
3917 * driver which has no strict concept of a container. We can only
3918 * assemble one family from a container, so when returning a prodigal
3919 * array member to this system the code will not be able to disambiguate
3920 * the container contents that should be assembled ("foreign" versus
3921 * "local"). It requires user intervention to set the orig_family_num
3922 * to a new value to establish a new container. The Windows driver in
3923 * this situation fixes up the volume name in place and manages the
3924 * foreign array as an independent entity.
3929 for (i
= 0; i
< tbl_size
; i
++) {
3930 struct intel_super
*tbl_ent
= super_table
[i
];
3936 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
3941 if (s
&& !is_spare
) {
3942 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
3944 } else if (!s
&& !is_spare
)
3957 fprintf(stderr
, "Chose family %#x on '%s', "
3958 "assemble conflicts to new container with '--update=uuid'\n",
3959 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
3961 /* collect all dl's onto 'champion', and update them to
3962 * champion's version of the status
3964 for (s
= *super_list
; s
; s
= s
->next
) {
3965 struct imsm_super
*mpb
= champion
->anchor
;
3966 struct dl
*dl
= s
->disks
;
3971 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3972 struct imsm_disk
*disk
;
3974 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
3977 /* only set index on disks that are a member of
3978 * a populated contianer, i.e. one with
3981 if (is_failed(&dl
->disk
))
3983 else if (is_spare(&dl
->disk
))
3989 if (i
>= mpb
->num_disks
) {
3990 struct intel_disk
*idisk
;
3992 idisk
= disk_list_get(dl
->serial
, disk_list
);
3993 if (idisk
&& is_spare(&idisk
->disk
) &&
3994 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
4002 dl
->next
= champion
->disks
;
4003 champion
->disks
= dl
;
4007 /* delete 'champion' from super_list */
4008 for (del
= super_list
; *del
; ) {
4009 if (*del
== champion
) {
4010 *del
= (*del
)->next
;
4013 del
= &(*del
)->next
;
4015 champion
->next
= NULL
;
4019 struct intel_disk
*idisk
= disk_list
;
4021 disk_list
= disk_list
->next
;
4030 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
);
4031 static int get_super_block(struct intel_super
**super_list
, int devnum
, char *devname
,
4032 int major
, int minor
, int keep_fd
);
4034 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4035 int *max
, int keep_fd
);
4038 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
4039 char *devname
, struct md_list
*devlist
,
4042 struct intel_super
*super_list
= NULL
;
4043 struct intel_super
*super
= NULL
;
4048 /* 'fd' is an opened container */
4049 err
= get_sra_super_block(fd
, &super_list
, devname
, &i
, keep_fd
);
4051 /* get super block from devlist devices */
4052 err
= get_devlist_super_block(devlist
, &super_list
, &i
, keep_fd
);
4055 /* all mpbs enter, maybe one leaves */
4056 super
= imsm_thunderdome(&super_list
, i
);
4062 if (find_missing(super
) != 0) {
4068 /* load migration record */
4069 err
= load_imsm_migr_rec(super
, NULL
);
4071 /* migration is in progress,
4072 * but migr_rec cannot be loaded,
4078 /* Check migration compatibility */
4079 if ((err
== 0) && (check_mpb_migr_compatibility(super
) != 0)) {
4080 fprintf(stderr
, Name
": Unsupported migration detected");
4082 fprintf(stderr
, " on %s\n", devname
);
4084 fprintf(stderr
, " (IMSM).\n");
4093 while (super_list
) {
4094 struct intel_super
*s
= super_list
;
4096 super_list
= super_list
->next
;
4106 st
->container_dev
= fd2devnum(fd
);
4108 st
->container_dev
= NoMdDev
;
4109 if (err
== 0 && st
->ss
== NULL
) {
4110 st
->ss
= &super_imsm
;
4111 st
->minor_version
= 0;
4112 st
->max_devs
= IMSM_MAX_DEVICES
;
4119 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4120 int *max
, int keep_fd
)
4122 struct md_list
*tmpdev
;
4127 for (i
= 0, tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
4128 if (tmpdev
->used
!= 1)
4130 if (tmpdev
->container
== 1) {
4131 int fd
= dev_open(tmpdev
->devname
, O_RDONLY
|O_EXCL
);
4133 fprintf(stderr
, Name
": cannot open device %s: %s\n",
4134 tmpdev
->devname
, strerror(errno
));
4138 err
= get_sra_super_block(fd
, super_list
,
4139 tmpdev
->devname
, &lmax
,
4148 int major
= major(tmpdev
->st_rdev
);
4149 int minor
= minor(tmpdev
->st_rdev
);
4150 err
= get_super_block(super_list
,
4167 static int get_super_block(struct intel_super
**super_list
, int devnum
, char *devname
,
4168 int major
, int minor
, int keep_fd
)
4170 struct intel_super
*s
= NULL
;
4183 sprintf(nm
, "%d:%d", major
, minor
);
4184 dfd
= dev_open(nm
, O_RDWR
);
4190 rv
= find_intel_hba_capability(dfd
, s
, devname
);
4191 /* no orom/efi or non-intel hba of the disk */
4197 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4199 /* retry the load if we might have raced against mdmon */
4200 if (err
== 3 && (devnum
!= -1) && mdmon_running(devnum
))
4201 for (retry
= 0; retry
< 3; retry
++) {
4203 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4209 s
->next
= *super_list
;
4217 if ((dfd
>= 0) && (!keep_fd
))
4224 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
)
4231 sra
= sysfs_read(fd
, 0, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
4235 if (sra
->array
.major_version
!= -1 ||
4236 sra
->array
.minor_version
!= -2 ||
4237 strcmp(sra
->text_version
, "imsm") != 0) {
4242 devnum
= fd2devnum(fd
);
4243 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
4244 if (get_super_block(super_list
, devnum
, devname
,
4245 sd
->disk
.major
, sd
->disk
.minor
, keep_fd
) != 0) {
4256 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
4258 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
, NULL
, 1);
4262 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
4264 struct intel_super
*super
;
4267 if (test_partition(fd
))
4268 /* IMSM not allowed on partitions */
4271 free_super_imsm(st
);
4273 super
= alloc_super();
4276 Name
": malloc of %zu failed.\n",
4280 /* Load hba and capabilities if they exist.
4281 * But do not preclude loading metadata in case capabilities or hba are
4282 * non-compliant and ignore_hw_compat is set.
4284 rv
= find_intel_hba_capability(fd
, super
, devname
);
4285 /* no orom/efi or non-intel hba of the disk */
4286 if ((rv
!= 0) && (st
->ignore_hw_compat
== 0)) {
4289 Name
": No OROM/EFI properties for %s\n", devname
);
4293 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
4298 Name
": Failed to load all information "
4299 "sections on %s\n", devname
);
4305 if (st
->ss
== NULL
) {
4306 st
->ss
= &super_imsm
;
4307 st
->minor_version
= 0;
4308 st
->max_devs
= IMSM_MAX_DEVICES
;
4311 /* load migration record */
4312 if (load_imsm_migr_rec(super
, NULL
) == 0) {
4313 /* Check for unsupported migration features */
4314 if (check_mpb_migr_compatibility(super
) != 0) {
4316 Name
": Unsupported migration detected");
4318 fprintf(stderr
, " on %s\n", devname
);
4320 fprintf(stderr
, " (IMSM).\n");
4328 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
4330 if (info
->level
== 1)
4332 return info
->chunk_size
>> 9;
4335 static __u32
info_to_num_data_stripes(mdu_array_info_t
*info
, int num_domains
)
4339 num_stripes
= (info
->size
* 2) / info_to_blocks_per_strip(info
);
4340 num_stripes
/= num_domains
;
4345 static __u32
info_to_blocks_per_member(mdu_array_info_t
*info
)
4347 if (info
->level
== 1)
4348 return info
->size
* 2;
4350 return (info
->size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
4353 static void imsm_update_version_info(struct intel_super
*super
)
4355 /* update the version and attributes */
4356 struct imsm_super
*mpb
= super
->anchor
;
4358 struct imsm_dev
*dev
;
4359 struct imsm_map
*map
;
4362 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4363 dev
= get_imsm_dev(super
, i
);
4364 map
= get_imsm_map(dev
, MAP_0
);
4365 if (__le32_to_cpu(dev
->size_high
) > 0)
4366 mpb
->attributes
|= MPB_ATTRIB_2TB
;
4368 /* FIXME detect when an array spans a port multiplier */
4370 mpb
->attributes
|= MPB_ATTRIB_PM
;
4373 if (mpb
->num_raid_devs
> 1 ||
4374 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
4375 version
= MPB_VERSION_ATTRIBS
;
4376 switch (get_imsm_raid_level(map
)) {
4377 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
4378 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
4379 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
4380 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
4383 if (map
->num_members
>= 5)
4384 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
4385 else if (dev
->status
== DEV_CLONE_N_GO
)
4386 version
= MPB_VERSION_CNG
;
4387 else if (get_imsm_raid_level(map
) == 5)
4388 version
= MPB_VERSION_RAID5
;
4389 else if (map
->num_members
>= 3)
4390 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
4391 else if (get_imsm_raid_level(map
) == 1)
4392 version
= MPB_VERSION_RAID1
;
4394 version
= MPB_VERSION_RAID0
;
4396 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
4400 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
4402 struct imsm_super
*mpb
= super
->anchor
;
4403 char *reason
= NULL
;
4406 if (strlen(name
) > MAX_RAID_SERIAL_LEN
)
4407 reason
= "must be 16 characters or less";
4409 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4410 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
4412 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
4413 reason
= "already exists";
4418 if (reason
&& !quiet
)
4419 fprintf(stderr
, Name
": imsm volume name %s\n", reason
);
4424 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
4425 unsigned long long size
, char *name
,
4426 char *homehost
, int *uuid
)
4428 /* We are creating a volume inside a pre-existing container.
4429 * so st->sb is already set.
4431 struct intel_super
*super
= st
->sb
;
4432 struct imsm_super
*mpb
= super
->anchor
;
4433 struct intel_dev
*dv
;
4434 struct imsm_dev
*dev
;
4435 struct imsm_vol
*vol
;
4436 struct imsm_map
*map
;
4437 int idx
= mpb
->num_raid_devs
;
4439 unsigned long long array_blocks
;
4440 size_t size_old
, size_new
;
4441 __u32 num_data_stripes
;
4443 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
4444 fprintf(stderr
, Name
": This imsm-container already has the "
4445 "maximum of %d volumes\n", super
->orom
->vpa
);
4449 /* ensure the mpb is large enough for the new data */
4450 size_old
= __le32_to_cpu(mpb
->mpb_size
);
4451 size_new
= disks_to_mpb_size(info
->nr_disks
);
4452 if (size_new
> size_old
) {
4454 size_t size_round
= ROUND_UP(size_new
, 512);
4456 if (posix_memalign(&mpb_new
, 512, size_round
) != 0) {
4457 fprintf(stderr
, Name
": could not allocate new mpb\n");
4460 if (posix_memalign(&super
->migr_rec_buf
, 512,
4461 MIGR_REC_BUF_SIZE
) != 0) {
4462 fprintf(stderr
, Name
4463 ": %s could not allocate migr_rec buffer\n",
4470 memcpy(mpb_new
, mpb
, size_old
);
4473 super
->anchor
= mpb_new
;
4474 mpb
->mpb_size
= __cpu_to_le32(size_new
);
4475 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
4477 super
->current_vol
= idx
;
4479 /* handle 'failed_disks' by either:
4480 * a) create dummy disk entries in the table if this the first
4481 * volume in the array. We add them here as this is the only
4482 * opportunity to add them. add_to_super_imsm_volume()
4483 * handles the non-failed disks and continues incrementing
4485 * b) validate that 'failed_disks' matches the current number
4486 * of missing disks if the container is populated
4488 if (super
->current_vol
== 0) {
4490 for (i
= 0; i
< info
->failed_disks
; i
++) {
4491 struct imsm_disk
*disk
;
4494 disk
= __get_imsm_disk(mpb
, i
);
4495 disk
->status
= CONFIGURED_DISK
| FAILED_DISK
;
4496 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
4497 snprintf((char *) disk
->serial
, MAX_RAID_SERIAL_LEN
,
4500 find_missing(super
);
4505 for (d
= super
->missing
; d
; d
= d
->next
)
4507 if (info
->failed_disks
> missing
) {
4508 fprintf(stderr
, Name
": unable to add 'missing' disk to container\n");
4513 if (!check_name(super
, name
, 0))
4515 dv
= malloc(sizeof(*dv
));
4517 fprintf(stderr
, Name
": failed to allocate device list entry\n");
4520 dev
= calloc(1, sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
4523 fprintf(stderr
, Name
": could not allocate raid device\n");
4527 strncpy((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
4528 if (info
->level
== 1)
4529 array_blocks
= info_to_blocks_per_member(info
);
4531 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
4532 info
->layout
, info
->chunk_size
,
4534 /* round array size down to closest MB */
4535 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
4537 dev
->size_low
= __cpu_to_le32((__u32
) array_blocks
);
4538 dev
->size_high
= __cpu_to_le32((__u32
) (array_blocks
>> 32));
4539 dev
->status
= (DEV_READ_COALESCING
| DEV_WRITE_COALESCING
);
4541 vol
->migr_state
= 0;
4542 set_migr_type(dev
, MIGR_INIT
);
4543 vol
->dirty
= !info
->state
;
4544 vol
->curr_migr_unit
= 0;
4545 map
= get_imsm_map(dev
, MAP_0
);
4546 map
->pba_of_lba0
= __cpu_to_le32(super
->create_offset
);
4547 map
->blocks_per_member
= __cpu_to_le32(info_to_blocks_per_member(info
));
4548 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
4549 map
->failed_disk_num
= ~0;
4550 if (info
->level
> 0)
4551 map
->map_state
= IMSM_T_STATE_UNINITIALIZED
;
4553 map
->map_state
= info
->failed_disks
? IMSM_T_STATE_FAILED
:
4554 IMSM_T_STATE_NORMAL
;
4557 if (info
->level
== 1 && info
->raid_disks
> 2) {
4560 fprintf(stderr
, Name
": imsm does not support more than 2 disks"
4561 "in a raid1 volume\n");
4565 map
->raid_level
= info
->level
;
4566 if (info
->level
== 10) {
4567 map
->raid_level
= 1;
4568 map
->num_domains
= info
->raid_disks
/ 2;
4569 } else if (info
->level
== 1)
4570 map
->num_domains
= info
->raid_disks
;
4572 map
->num_domains
= 1;
4574 num_data_stripes
= info_to_num_data_stripes(info
, map
->num_domains
);
4575 map
->num_data_stripes
= __cpu_to_le32(num_data_stripes
);
4577 map
->num_members
= info
->raid_disks
;
4578 for (i
= 0; i
< map
->num_members
; i
++) {
4579 /* initialized in add_to_super */
4580 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
4582 mpb
->num_raid_devs
++;
4585 dv
->index
= super
->current_vol
;
4586 dv
->next
= super
->devlist
;
4587 super
->devlist
= dv
;
4589 imsm_update_version_info(super
);
4594 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
4595 unsigned long long size
, char *name
,
4596 char *homehost
, int *uuid
)
4598 /* This is primarily called by Create when creating a new array.
4599 * We will then get add_to_super called for each component, and then
4600 * write_init_super called to write it out to each device.
4601 * For IMSM, Create can create on fresh devices or on a pre-existing
4603 * To create on a pre-existing array a different method will be called.
4604 * This one is just for fresh drives.
4606 struct intel_super
*super
;
4607 struct imsm_super
*mpb
;
4612 return init_super_imsm_volume(st
, info
, size
, name
, homehost
, uuid
);
4615 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
4619 super
= alloc_super();
4620 if (super
&& posix_memalign(&super
->buf
, 512, mpb_size
) != 0) {
4625 fprintf(stderr
, Name
4626 ": %s could not allocate superblock\n", __func__
);
4629 if (posix_memalign(&super
->migr_rec_buf
, 512, MIGR_REC_BUF_SIZE
) != 0) {
4630 fprintf(stderr
, Name
4631 ": %s could not allocate migr_rec buffer\n", __func__
);
4636 memset(super
->buf
, 0, mpb_size
);
4638 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
4642 /* zeroing superblock */
4646 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
4648 version
= (char *) mpb
->sig
;
4649 strcpy(version
, MPB_SIGNATURE
);
4650 version
+= strlen(MPB_SIGNATURE
);
4651 strcpy(version
, MPB_VERSION_RAID0
);
4657 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
4658 int fd
, char *devname
)
4660 struct intel_super
*super
= st
->sb
;
4661 struct imsm_super
*mpb
= super
->anchor
;
4662 struct imsm_disk
*_disk
;
4663 struct imsm_dev
*dev
;
4664 struct imsm_map
*map
;
4668 dev
= get_imsm_dev(super
, super
->current_vol
);
4669 map
= get_imsm_map(dev
, MAP_0
);
4671 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
4672 fprintf(stderr
, Name
": %s: Cannot add spare devices to IMSM volume\n",
4678 /* we're doing autolayout so grab the pre-marked (in
4679 * validate_geometry) raid_disk
4681 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4682 if (dl
->raiddisk
== dk
->raid_disk
)
4685 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4686 if (dl
->major
== dk
->major
&&
4687 dl
->minor
== dk
->minor
)
4692 fprintf(stderr
, Name
": %s is not a member of the same container\n", devname
);
4696 /* add a pristine spare to the metadata */
4697 if (dl
->index
< 0) {
4698 dl
->index
= super
->anchor
->num_disks
;
4699 super
->anchor
->num_disks
++;
4701 /* Check the device has not already been added */
4702 slot
= get_imsm_disk_slot(map
, dl
->index
);
4704 (get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
) & IMSM_ORD_REBUILD
) == 0) {
4705 fprintf(stderr
, Name
": %s has been included in this array twice\n",
4709 set_imsm_ord_tbl_ent(map
, dk
->raid_disk
, dl
->index
);
4710 dl
->disk
.status
= CONFIGURED_DISK
;
4712 /* update size of 'missing' disks to be at least as large as the
4713 * largest acitve member (we only have dummy missing disks when
4714 * creating the first volume)
4716 if (super
->current_vol
== 0) {
4717 for (df
= super
->missing
; df
; df
= df
->next
) {
4718 if (dl
->disk
.total_blocks
> df
->disk
.total_blocks
)
4719 df
->disk
.total_blocks
= dl
->disk
.total_blocks
;
4720 _disk
= __get_imsm_disk(mpb
, df
->index
);
4725 /* refresh unset/failed slots to point to valid 'missing' entries */
4726 for (df
= super
->missing
; df
; df
= df
->next
)
4727 for (slot
= 0; slot
< mpb
->num_disks
; slot
++) {
4728 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
4730 if ((ord
& IMSM_ORD_REBUILD
) == 0)
4732 set_imsm_ord_tbl_ent(map
, slot
, df
->index
| IMSM_ORD_REBUILD
);
4733 if (is_gen_migration(dev
)) {
4734 struct imsm_map
*map2
= get_imsm_map(dev
,
4736 int slot2
= get_imsm_disk_slot(map2
, df
->index
);
4737 if ((slot2
< map2
->num_members
) &&
4739 __u32 ord2
= get_imsm_ord_tbl_ent(dev
,
4742 if ((unsigned)df
->index
==
4744 set_imsm_ord_tbl_ent(map2
,
4750 dprintf("set slot:%d to missing disk:%d\n", slot
, df
->index
);
4754 /* if we are creating the first raid device update the family number */
4755 if (super
->current_vol
== 0) {
4757 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
4759 _disk
= __get_imsm_disk(mpb
, dl
->index
);
4760 if (!_dev
|| !_disk
) {
4761 fprintf(stderr
, Name
": BUG mpb setup error\n");
4767 sum
+= __gen_imsm_checksum(mpb
);
4768 mpb
->family_num
= __cpu_to_le32(sum
);
4769 mpb
->orig_family_num
= mpb
->family_num
;
4771 super
->current_disk
= dl
;
4776 * Function marks disk as spare and restores disk serial
4777 * in case it was previously marked as failed by takeover operation
4779 * -1 : critical error
4780 * 0 : disk is marked as spare but serial is not set
4783 int mark_spare(struct dl
*disk
)
4785 __u8 serial
[MAX_RAID_SERIAL_LEN
];
4792 if (!imsm_read_serial(disk
->fd
, NULL
, serial
)) {
4793 /* Restore disk serial number, because takeover marks disk
4794 * as failed and adds to serial ':0' before it becomes
4797 serialcpy(disk
->serial
, serial
);
4798 serialcpy(disk
->disk
.serial
, serial
);
4801 disk
->disk
.status
= SPARE_DISK
;
4807 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
4808 int fd
, char *devname
)
4810 struct intel_super
*super
= st
->sb
;
4812 unsigned long long size
;
4817 /* If we are on an RAID enabled platform check that the disk is
4818 * attached to the raid controller.
4819 * We do not need to test disks attachment for container based additions,
4820 * they shall be already tested when container was created/assembled.
4822 rv
= find_intel_hba_capability(fd
, super
, devname
);
4823 /* no orom/efi or non-intel hba of the disk */
4825 dprintf("capability: %p fd: %d ret: %d\n",
4826 super
->orom
, fd
, rv
);
4830 if (super
->current_vol
>= 0)
4831 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
4834 dd
= malloc(sizeof(*dd
));
4837 Name
": malloc failed %s:%d.\n", __func__
, __LINE__
);
4840 memset(dd
, 0, sizeof(*dd
));
4841 dd
->major
= major(stb
.st_rdev
);
4842 dd
->minor
= minor(stb
.st_rdev
);
4843 dd
->devname
= devname
? strdup(devname
) : NULL
;
4846 dd
->action
= DISK_ADD
;
4847 rv
= imsm_read_serial(fd
, devname
, dd
->serial
);
4850 Name
": failed to retrieve scsi serial, aborting\n");
4855 get_dev_size(fd
, NULL
, &size
);
4857 serialcpy(dd
->disk
.serial
, dd
->serial
);
4858 dd
->disk
.total_blocks
= __cpu_to_le32(size
);
4860 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
4861 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
4863 dd
->disk
.scsi_id
= __cpu_to_le32(0);
4865 if (st
->update_tail
) {
4866 dd
->next
= super
->disk_mgmt_list
;
4867 super
->disk_mgmt_list
= dd
;
4869 dd
->next
= super
->disks
;
4871 super
->updates_pending
++;
4878 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
4880 struct intel_super
*super
= st
->sb
;
4883 /* remove from super works only in mdmon - for communication
4884 * manager - monitor. Check if communication memory buffer
4887 if (!st
->update_tail
) {
4889 Name
": %s shall be used in mdmon context only"
4890 "(line %d).\n", __func__
, __LINE__
);
4893 dd
= malloc(sizeof(*dd
));
4896 Name
": malloc failed %s:%d.\n", __func__
, __LINE__
);
4899 memset(dd
, 0, sizeof(*dd
));
4900 dd
->major
= dk
->major
;
4901 dd
->minor
= dk
->minor
;
4904 dd
->action
= DISK_REMOVE
;
4906 dd
->next
= super
->disk_mgmt_list
;
4907 super
->disk_mgmt_list
= dd
;
4913 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
4917 struct imsm_super anchor
;
4918 } spare_record
__attribute__ ((aligned(512)));
4920 /* spare records have their own family number and do not have any defined raid
4923 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
4925 struct imsm_super
*mpb
= super
->anchor
;
4926 struct imsm_super
*spare
= &spare_record
.anchor
;
4930 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
)),
4931 spare
->generation_num
= __cpu_to_le32(1UL),
4932 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
4933 spare
->num_disks
= 1,
4934 spare
->num_raid_devs
= 0,
4935 spare
->cache_size
= mpb
->cache_size
,
4936 spare
->pwr_cycle_count
= __cpu_to_le32(1),
4938 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
4939 MPB_SIGNATURE MPB_VERSION_RAID0
);
4941 for (d
= super
->disks
; d
; d
= d
->next
) {
4945 spare
->disk
[0] = d
->disk
;
4946 sum
= __gen_imsm_checksum(spare
);
4947 spare
->family_num
= __cpu_to_le32(sum
);
4948 spare
->orig_family_num
= 0;
4949 sum
= __gen_imsm_checksum(spare
);
4950 spare
->check_sum
= __cpu_to_le32(sum
);
4952 if (store_imsm_mpb(d
->fd
, spare
)) {
4953 fprintf(stderr
, "%s: failed for device %d:%d %s\n",
4954 __func__
, d
->major
, d
->minor
, strerror(errno
));
4966 static int write_super_imsm(struct supertype
*st
, int doclose
)
4968 struct intel_super
*super
= st
->sb
;
4969 struct imsm_super
*mpb
= super
->anchor
;
4975 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
4977 int clear_migration_record
= 1;
4979 /* 'generation' is incremented everytime the metadata is written */
4980 generation
= __le32_to_cpu(mpb
->generation_num
);
4982 mpb
->generation_num
= __cpu_to_le32(generation
);
4984 /* fix up cases where previous mdadm releases failed to set
4987 if (mpb
->orig_family_num
== 0)
4988 mpb
->orig_family_num
= mpb
->family_num
;
4990 for (d
= super
->disks
; d
; d
= d
->next
) {
4994 mpb
->disk
[d
->index
] = d
->disk
;
4998 for (d
= super
->missing
; d
; d
= d
->next
) {
4999 mpb
->disk
[d
->index
] = d
->disk
;
5002 mpb
->num_disks
= num_disks
;
5003 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
5005 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5006 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
5007 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
5009 imsm_copy_dev(dev
, dev2
);
5010 mpb_size
+= sizeof_imsm_dev(dev
, 0);
5012 if (is_gen_migration(dev2
))
5013 clear_migration_record
= 0;
5015 mpb_size
+= __le32_to_cpu(mpb
->bbm_log_size
);
5016 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5018 /* recalculate checksum */
5019 sum
= __gen_imsm_checksum(mpb
);
5020 mpb
->check_sum
= __cpu_to_le32(sum
);
5022 if (clear_migration_record
)
5023 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SIZE
);
5025 /* write the mpb for disks that compose raid devices */
5026 for (d
= super
->disks
; d
; d
= d
->next
) {
5027 if (d
->index
< 0 || is_failed(&d
->disk
))
5029 if (store_imsm_mpb(d
->fd
, mpb
))
5030 fprintf(stderr
, "%s: failed for device %d:%d %s\n",
5031 __func__
, d
->major
, d
->minor
, strerror(errno
));
5032 if (clear_migration_record
) {
5033 unsigned long long dsize
;
5035 get_dev_size(d
->fd
, NULL
, &dsize
);
5036 if (lseek64(d
->fd
, dsize
- 512, SEEK_SET
) >= 0) {
5037 if (write(d
->fd
, super
->migr_rec_buf
,
5038 MIGR_REC_BUF_SIZE
) != MIGR_REC_BUF_SIZE
)
5039 perror("Write migr_rec failed");
5049 return write_super_imsm_spares(super
, doclose
);
5055 static int create_array(struct supertype
*st
, int dev_idx
)
5058 struct imsm_update_create_array
*u
;
5059 struct intel_super
*super
= st
->sb
;
5060 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
5061 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
5062 struct disk_info
*inf
;
5063 struct imsm_disk
*disk
;
5066 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
5067 sizeof(*inf
) * map
->num_members
;
5070 fprintf(stderr
, "%s: failed to allocate update buffer\n",
5075 u
->type
= update_create_array
;
5076 u
->dev_idx
= dev_idx
;
5077 imsm_copy_dev(&u
->dev
, dev
);
5078 inf
= get_disk_info(u
);
5079 for (i
= 0; i
< map
->num_members
; i
++) {
5080 int idx
= get_imsm_disk_idx(dev
, i
, MAP_X
);
5082 disk
= get_imsm_disk(super
, idx
);
5083 serialcpy(inf
[i
].serial
, disk
->serial
);
5085 append_metadata_update(st
, u
, len
);
5090 static int mgmt_disk(struct supertype
*st
)
5092 struct intel_super
*super
= st
->sb
;
5094 struct imsm_update_add_remove_disk
*u
;
5096 if (!super
->disk_mgmt_list
)
5102 fprintf(stderr
, "%s: failed to allocate update buffer\n",
5107 u
->type
= update_add_remove_disk
;
5108 append_metadata_update(st
, u
, len
);
5113 static int write_init_super_imsm(struct supertype
*st
)
5115 struct intel_super
*super
= st
->sb
;
5116 int current_vol
= super
->current_vol
;
5118 /* we are done with current_vol reset it to point st at the container */
5119 super
->current_vol
= -1;
5121 if (st
->update_tail
) {
5122 /* queue the recently created array / added disk
5123 * as a metadata update */
5126 /* determine if we are creating a volume or adding a disk */
5127 if (current_vol
< 0) {
5128 /* in the mgmt (add/remove) disk case we are running
5129 * in mdmon context, so don't close fd's
5131 return mgmt_disk(st
);
5133 rv
= create_array(st
, current_vol
);
5138 for (d
= super
->disks
; d
; d
= d
->next
)
5139 Kill(d
->devname
, NULL
, 0, 1, 1);
5140 return write_super_imsm(st
, 1);
5145 static int store_super_imsm(struct supertype
*st
, int fd
)
5147 struct intel_super
*super
= st
->sb
;
5148 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
5154 return store_imsm_mpb(fd
, mpb
);
5160 static int imsm_bbm_log_size(struct imsm_super
*mpb
)
5162 return __le32_to_cpu(mpb
->bbm_log_size
);
5166 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
5167 int layout
, int raiddisks
, int chunk
,
5168 unsigned long long size
, char *dev
,
5169 unsigned long long *freesize
,
5173 unsigned long long ldsize
;
5174 struct intel_super
*super
=NULL
;
5177 if (level
!= LEVEL_CONTAINER
)
5182 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
5185 fprintf(stderr
, Name
": imsm: Cannot open %s: %s\n",
5186 dev
, strerror(errno
));
5189 if (!get_dev_size(fd
, dev
, &ldsize
)) {
5194 /* capabilities retrieve could be possible
5195 * note that there is no fd for the disks in array.
5197 super
= alloc_super();
5200 Name
": malloc of %zu failed.\n",
5206 rv
= find_intel_hba_capability(fd
, super
, verbose
? dev
: NULL
);
5210 fd2devname(fd
, str
);
5211 dprintf("validate_geometry_imsm_container: fd: %d %s orom: %p rv: %d raiddisk: %d\n",
5212 fd
, str
, super
->orom
, rv
, raiddisks
);
5214 /* no orom/efi or non-intel hba of the disk */
5220 if (super
->orom
&& raiddisks
> super
->orom
->tds
) {
5222 fprintf(stderr
, Name
": %d exceeds maximum number of"
5223 " platform supported disks: %d\n",
5224 raiddisks
, super
->orom
->tds
);
5230 *freesize
= avail_size_imsm(st
, ldsize
>> 9);
5236 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
5238 const unsigned long long base_start
= e
[*idx
].start
;
5239 unsigned long long end
= base_start
+ e
[*idx
].size
;
5242 if (base_start
== end
)
5246 for (i
= *idx
; i
< num_extents
; i
++) {
5247 /* extend overlapping extents */
5248 if (e
[i
].start
>= base_start
&&
5249 e
[i
].start
<= end
) {
5252 if (e
[i
].start
+ e
[i
].size
> end
)
5253 end
= e
[i
].start
+ e
[i
].size
;
5254 } else if (e
[i
].start
> end
) {
5260 return end
- base_start
;
5263 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
5265 /* build a composite disk with all known extents and generate a new
5266 * 'maxsize' given the "all disks in an array must share a common start
5267 * offset" constraint
5269 struct extent
*e
= calloc(sum_extents
, sizeof(*e
));
5273 unsigned long long pos
;
5274 unsigned long long start
= 0;
5275 unsigned long long maxsize
;
5276 unsigned long reserve
;
5281 /* coalesce and sort all extents. also, check to see if we need to
5282 * reserve space between member arrays
5285 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5288 for (i
= 0; i
< dl
->extent_cnt
; i
++)
5291 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
5296 while (i
< sum_extents
) {
5297 e
[j
].start
= e
[i
].start
;
5298 e
[j
].size
= find_size(e
, &i
, sum_extents
);
5300 if (e
[j
-1].size
== 0)
5309 unsigned long long esize
;
5311 esize
= e
[i
].start
- pos
;
5312 if (esize
>= maxsize
) {
5317 pos
= e
[i
].start
+ e
[i
].size
;
5319 } while (e
[i
-1].size
);
5325 /* FIXME assumes volume at offset 0 is the first volume in a
5328 if (start_extent
> 0)
5329 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
5333 if (maxsize
< reserve
)
5336 super
->create_offset
= ~((__u32
) 0);
5337 if (start
+ reserve
> super
->create_offset
)
5338 return 0; /* start overflows create_offset */
5339 super
->create_offset
= start
+ reserve
;
5341 return maxsize
- reserve
;
5344 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
5346 if (level
< 0 || level
== 6 || level
== 4)
5349 /* if we have an orom prevent invalid raid levels */
5352 case 0: return imsm_orom_has_raid0(orom
);
5355 return imsm_orom_has_raid1e(orom
);
5356 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
5357 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
5358 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
5361 return 1; /* not on an Intel RAID platform so anything goes */
5366 static int imsm_default_chunk(const struct imsm_orom
*orom
)
5368 /* up to 512 if the plaform supports it, otherwise the platform max.
5369 * 128 if no platform detected
5371 int fs
= max(7, orom
? fls(orom
->sss
) : 0);
5373 return min(512, (1 << fs
));
5377 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
5378 int raiddisks
, int *chunk
, int verbose
)
5380 /* check/set platform and metadata limits/defaults */
5381 if (super
->orom
&& raiddisks
> super
->orom
->dpa
) {
5382 pr_vrb(": platform supports a maximum of %d disks per array\n",
5387 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
5388 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
5389 pr_vrb(": platform does not support raid%d with %d disk%s\n",
5390 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
5394 if (chunk
&& (*chunk
== 0 || *chunk
== UnSet
))
5395 *chunk
= imsm_default_chunk(super
->orom
);
5397 if (super
->orom
&& chunk
&& !imsm_orom_has_chunk(super
->orom
, *chunk
)) {
5398 pr_vrb(": platform does not support a chunk size of: "
5403 if (layout
!= imsm_level_to_layout(level
)) {
5405 pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n");
5406 else if (level
== 10)
5407 pr_vrb(": imsm raid 10 only supports the n2 layout\n");
5409 pr_vrb(": imsm unknown layout %#x for this raid level %d\n",
5416 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
5417 * FIX ME add ahci details
5419 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
5420 int layout
, int raiddisks
, int *chunk
,
5421 unsigned long long size
, char *dev
,
5422 unsigned long long *freesize
,
5426 struct intel_super
*super
= st
->sb
;
5427 struct imsm_super
*mpb
;
5429 unsigned long long pos
= 0;
5430 unsigned long long maxsize
;
5434 /* We must have the container info already read in. */
5438 mpb
= super
->anchor
;
5440 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, verbose
)) {
5441 fprintf(stderr
, Name
": RAID gemetry validation failed. "
5442 "Cannot proceed with the action(s).\n");
5446 /* General test: make sure there is space for
5447 * 'raiddisks' device extents of size 'size' at a given
5450 unsigned long long minsize
= size
;
5451 unsigned long long start_offset
= MaxSector
;
5454 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
5455 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5460 e
= get_extents(super
, dl
);
5463 unsigned long long esize
;
5464 esize
= e
[i
].start
- pos
;
5465 if (esize
>= minsize
)
5467 if (found
&& start_offset
== MaxSector
) {
5470 } else if (found
&& pos
!= start_offset
) {
5474 pos
= e
[i
].start
+ e
[i
].size
;
5476 } while (e
[i
-1].size
);
5481 if (dcnt
< raiddisks
) {
5483 fprintf(stderr
, Name
": imsm: Not enough "
5484 "devices with space for this array "
5492 /* This device must be a member of the set */
5493 if (stat(dev
, &stb
) < 0)
5495 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
5497 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5498 if (dl
->major
== (int)major(stb
.st_rdev
) &&
5499 dl
->minor
== (int)minor(stb
.st_rdev
))
5504 fprintf(stderr
, Name
": %s is not in the "
5505 "same imsm set\n", dev
);
5507 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
5508 /* If a volume is present then the current creation attempt
5509 * cannot incorporate new spares because the orom may not
5510 * understand this configuration (all member disks must be
5511 * members of each array in the container).
5513 fprintf(stderr
, Name
": %s is a spare and a volume"
5514 " is already defined for this container\n", dev
);
5515 fprintf(stderr
, Name
": The option-rom requires all member"
5516 " disks to be a member of all volumes\n");
5518 } else if (super
->orom
&& mpb
->num_raid_devs
> 0 &&
5519 mpb
->num_disks
!= raiddisks
) {
5520 fprintf(stderr
, Name
": The option-rom requires all member"
5521 " disks to be a member of all volumes\n");
5525 /* retrieve the largest free space block */
5526 e
= get_extents(super
, dl
);
5531 unsigned long long esize
;
5533 esize
= e
[i
].start
- pos
;
5534 if (esize
>= maxsize
)
5536 pos
= e
[i
].start
+ e
[i
].size
;
5538 } while (e
[i
-1].size
);
5543 fprintf(stderr
, Name
": unable to determine free space for: %s\n",
5547 if (maxsize
< size
) {
5549 fprintf(stderr
, Name
": %s not enough space (%llu < %llu)\n",
5550 dev
, maxsize
, size
);
5554 /* count total number of extents for merge */
5556 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5558 i
+= dl
->extent_cnt
;
5560 maxsize
= merge_extents(super
, i
);
5562 if (!check_env("IMSM_NO_PLATFORM") &&
5563 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
5564 fprintf(stderr
, Name
": attempting to create a second "
5565 "volume with size less then remaining space. "
5570 if (maxsize
< size
|| maxsize
== 0) {
5573 fprintf(stderr
, Name
": no free space"
5574 " left on device. Aborting...\n");
5576 fprintf(stderr
, Name
": not enough space"
5577 " to create volume of given size"
5578 " (%llu < %llu). Aborting...\n",
5584 *freesize
= maxsize
;
5589 static int reserve_space(struct supertype
*st
, int raiddisks
,
5590 unsigned long long size
, int chunk
,
5591 unsigned long long *freesize
)
5593 struct intel_super
*super
= st
->sb
;
5594 struct imsm_super
*mpb
= super
->anchor
;
5599 unsigned long long maxsize
;
5600 unsigned long long minsize
;
5604 /* find the largest common start free region of the possible disks */
5608 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5614 /* don't activate new spares if we are orom constrained
5615 * and there is already a volume active in the container
5617 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
5620 e
= get_extents(super
, dl
);
5623 for (i
= 1; e
[i
-1].size
; i
++)
5631 maxsize
= merge_extents(super
, extent_cnt
);
5635 minsize
= chunk
* 2;
5637 if (cnt
< raiddisks
||
5638 (super
->orom
&& used
&& used
!= raiddisks
) ||
5639 maxsize
< minsize
||
5641 fprintf(stderr
, Name
": not enough devices with space to create array.\n");
5642 return 0; /* No enough free spaces large enough */
5654 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5656 dl
->raiddisk
= cnt
++;
5663 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
5664 int raiddisks
, int *chunk
, unsigned long long size
,
5665 char *dev
, unsigned long long *freesize
,
5673 * if given unused devices create a container
5674 * if given given devices in a container create a member volume
5676 if (level
== LEVEL_CONTAINER
) {
5677 /* Must be a fresh device to add to a container */
5678 return validate_geometry_imsm_container(st
, level
, layout
,
5680 chunk
?*chunk
:0, size
,
5687 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
5691 /* we are being asked to automatically layout a
5692 * new volume based on the current contents of
5693 * the container. If the the parameters can be
5694 * satisfied reserve_space will record the disks,
5695 * start offset, and size of the volume to be
5696 * created. add_to_super and getinfo_super
5697 * detect when autolayout is in progress.
5700 return reserve_space(st
, raiddisks
, size
,
5701 chunk
?*chunk
:0, freesize
);
5706 /* creating in a given container */
5707 return validate_geometry_imsm_volume(st
, level
, layout
,
5708 raiddisks
, chunk
, size
,
5709 dev
, freesize
, verbose
);
5712 /* This device needs to be a device in an 'imsm' container */
5713 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
5717 Name
": Cannot create this array on device %s\n",
5722 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
5724 fprintf(stderr
, Name
": Cannot open %s: %s\n",
5725 dev
, strerror(errno
));
5728 /* Well, it is in use by someone, maybe an 'imsm' container. */
5729 cfd
= open_container(fd
);
5733 fprintf(stderr
, Name
": Cannot use %s: It is busy\n",
5737 sra
= sysfs_read(cfd
, 0, GET_VERSION
);
5738 if (sra
&& sra
->array
.major_version
== -1 &&
5739 strcmp(sra
->text_version
, "imsm") == 0)
5743 /* This is a member of a imsm container. Load the container
5744 * and try to create a volume
5746 struct intel_super
*super
;
5748 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
, NULL
, 1) == 0) {
5750 st
->container_dev
= fd2devnum(cfd
);
5752 return validate_geometry_imsm_volume(st
, level
, layout
,
5761 fprintf(stderr
, Name
": failed container membership check\n");
5767 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
5769 struct intel_super
*super
= st
->sb
;
5771 if (level
&& *level
== UnSet
)
5772 *level
= LEVEL_CONTAINER
;
5774 if (level
&& layout
&& *layout
== UnSet
)
5775 *layout
= imsm_level_to_layout(*level
);
5777 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0))
5778 *chunk
= imsm_default_chunk(super
->orom
);
5781 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
5783 static int kill_subarray_imsm(struct supertype
*st
)
5785 /* remove the subarray currently referenced by ->current_vol */
5787 struct intel_dev
**dp
;
5788 struct intel_super
*super
= st
->sb
;
5789 __u8 current_vol
= super
->current_vol
;
5790 struct imsm_super
*mpb
= super
->anchor
;
5792 if (super
->current_vol
< 0)
5794 super
->current_vol
= -1; /* invalidate subarray cursor */
5796 /* block deletions that would change the uuid of active subarrays
5798 * FIXME when immutable ids are available, but note that we'll
5799 * also need to fixup the invalidated/active subarray indexes in
5802 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5805 if (i
< current_vol
)
5807 sprintf(subarray
, "%u", i
);
5808 if (is_subarray_active(subarray
, st
->devname
)) {
5810 Name
": deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
5817 if (st
->update_tail
) {
5818 struct imsm_update_kill_array
*u
= malloc(sizeof(*u
));
5822 u
->type
= update_kill_array
;
5823 u
->dev_idx
= current_vol
;
5824 append_metadata_update(st
, u
, sizeof(*u
));
5829 for (dp
= &super
->devlist
; *dp
;)
5830 if ((*dp
)->index
== current_vol
) {
5833 handle_missing(super
, (*dp
)->dev
);
5834 if ((*dp
)->index
> current_vol
)
5839 /* no more raid devices, all active components are now spares,
5840 * but of course failed are still failed
5842 if (--mpb
->num_raid_devs
== 0) {
5845 for (d
= super
->disks
; d
; d
= d
->next
)
5850 super
->updates_pending
++;
5855 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
5856 char *update
, struct mddev_ident
*ident
)
5858 /* update the subarray currently referenced by ->current_vol */
5859 struct intel_super
*super
= st
->sb
;
5860 struct imsm_super
*mpb
= super
->anchor
;
5862 if (strcmp(update
, "name") == 0) {
5863 char *name
= ident
->name
;
5867 if (is_subarray_active(subarray
, st
->devname
)) {
5869 Name
": Unable to update name of active subarray\n");
5873 if (!check_name(super
, name
, 0))
5876 vol
= strtoul(subarray
, &ep
, 10);
5877 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
5880 if (st
->update_tail
) {
5881 struct imsm_update_rename_array
*u
= malloc(sizeof(*u
));
5885 u
->type
= update_rename_array
;
5887 snprintf((char *) u
->name
, MAX_RAID_SERIAL_LEN
, "%s", name
);
5888 append_metadata_update(st
, u
, sizeof(*u
));
5890 struct imsm_dev
*dev
;
5893 dev
= get_imsm_dev(super
, vol
);
5894 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
5895 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5896 dev
= get_imsm_dev(super
, i
);
5897 handle_missing(super
, dev
);
5899 super
->updates_pending
++;
5906 #endif /* MDASSEMBLE */
5908 static int is_gen_migration(struct imsm_dev
*dev
)
5913 if (!dev
->vol
.migr_state
)
5916 if (migr_type(dev
) == MIGR_GEN_MIGR
)
5922 static int is_rebuilding(struct imsm_dev
*dev
)
5924 struct imsm_map
*migr_map
;
5926 if (!dev
->vol
.migr_state
)
5929 if (migr_type(dev
) != MIGR_REBUILD
)
5932 migr_map
= get_imsm_map(dev
, MAP_1
);
5934 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
5941 static int is_initializing(struct imsm_dev
*dev
)
5943 struct imsm_map
*migr_map
;
5945 if (!dev
->vol
.migr_state
)
5948 if (migr_type(dev
) != MIGR_INIT
)
5951 migr_map
= get_imsm_map(dev
, MAP_1
);
5953 if (migr_map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
5960 static void update_recovery_start(struct intel_super
*super
,
5961 struct imsm_dev
*dev
,
5962 struct mdinfo
*array
)
5964 struct mdinfo
*rebuild
= NULL
;
5968 if (!is_rebuilding(dev
))
5971 /* Find the rebuild target, but punt on the dual rebuild case */
5972 for (d
= array
->devs
; d
; d
= d
->next
)
5973 if (d
->recovery_start
== 0) {
5980 /* (?) none of the disks are marked with
5981 * IMSM_ORD_REBUILD, so assume they are missing and the
5982 * disk_ord_tbl was not correctly updated
5984 dprintf("%s: failed to locate out-of-sync disk\n", __func__
);
5988 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
5989 rebuild
->recovery_start
= units
* blocks_per_migr_unit(super
, dev
);
5993 static int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
);
5996 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
5998 /* Given a container loaded by load_super_imsm_all,
5999 * extract information about all the arrays into
6001 * If 'subarray' is given, just extract info about that array.
6003 * For each imsm_dev create an mdinfo, fill it in,
6004 * then look for matching devices in super->disks
6005 * and create appropriate device mdinfo.
6007 struct intel_super
*super
= st
->sb
;
6008 struct imsm_super
*mpb
= super
->anchor
;
6009 struct mdinfo
*rest
= NULL
;
6013 int spare_disks
= 0;
6015 /* do not assemble arrays when not all attributes are supported */
6016 if (imsm_check_attributes(mpb
->attributes
) == 0) {
6018 fprintf(stderr
, Name
": Unsupported attributes in IMSM metadata."
6019 "Arrays activation is blocked.\n");
6022 /* check for bad blocks */
6023 if (imsm_bbm_log_size(super
->anchor
)) {
6024 fprintf(stderr
, Name
": BBM log found in IMSM metadata."
6025 "Arrays activation is blocked.\n");
6030 /* count spare devices, not used in maps
6032 for (d
= super
->disks
; d
; d
= d
->next
)
6036 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6037 struct imsm_dev
*dev
;
6038 struct imsm_map
*map
;
6039 struct imsm_map
*map2
;
6040 struct mdinfo
*this;
6048 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
6051 dev
= get_imsm_dev(super
, i
);
6052 map
= get_imsm_map(dev
, MAP_0
);
6053 map2
= get_imsm_map(dev
, MAP_1
);
6055 /* do not publish arrays that are in the middle of an
6056 * unsupported migration
6058 if (dev
->vol
.migr_state
&&
6059 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
6060 fprintf(stderr
, Name
": cannot assemble volume '%.16s':"
6061 " unsupported migration in progress\n",
6065 /* do not publish arrays that are not support by controller's
6069 this = malloc(sizeof(*this));
6071 fprintf(stderr
, Name
": failed to allocate %zu bytes\n",
6076 super
->current_vol
= i
;
6077 getinfo_super_imsm_volume(st
, this, NULL
);
6080 chunk
= __le16_to_cpu(map
->blocks_per_strip
) >> 1;
6081 /* mdadm does not support all metadata features- set the bit in all arrays state */
6082 if (!validate_geometry_imsm_orom(super
,
6083 get_imsm_raid_level(map
), /* RAID level */
6084 imsm_level_to_layout(get_imsm_raid_level(map
)),
6085 map
->num_members
, /* raid disks */
6088 fprintf(stderr
, Name
": IMSM RAID geometry validation"
6089 " failed. Array %s activation is blocked.\n",
6091 this->array
.state
|=
6092 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
6093 (1<<MD_SB_BLOCK_VOLUME
);
6097 /* if array has bad blocks, set suitable bit in all arrays state */
6099 this->array
.state
|=
6100 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
6101 (1<<MD_SB_BLOCK_VOLUME
);
6103 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
6104 unsigned long long recovery_start
;
6105 struct mdinfo
*info_d
;
6112 idx
= get_imsm_disk_idx(dev
, slot
, MAP_0
);
6113 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
6114 for (d
= super
->disks
; d
; d
= d
->next
)
6115 if (d
->index
== idx
)
6118 recovery_start
= MaxSector
;
6121 if (d
&& is_failed(&d
->disk
))
6123 if (ord
& IMSM_ORD_REBUILD
)
6127 * if we skip some disks the array will be assmebled degraded;
6128 * reset resync start to avoid a dirty-degraded
6129 * situation when performing the intial sync
6131 * FIXME handle dirty degraded
6133 if ((skip
|| recovery_start
== 0) && !dev
->vol
.dirty
)
6134 this->resync_start
= MaxSector
;
6138 info_d
= calloc(1, sizeof(*info_d
));
6140 fprintf(stderr
, Name
": failed to allocate disk"
6141 " for volume %.16s\n", dev
->volume
);
6142 info_d
= this->devs
;
6144 struct mdinfo
*d
= info_d
->next
;
6153 info_d
->next
= this->devs
;
6154 this->devs
= info_d
;
6156 info_d
->disk
.number
= d
->index
;
6157 info_d
->disk
.major
= d
->major
;
6158 info_d
->disk
.minor
= d
->minor
;
6159 info_d
->disk
.raid_disk
= slot
;
6160 info_d
->recovery_start
= recovery_start
;
6162 if (slot
< map2
->num_members
)
6163 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
6165 this->array
.spare_disks
++;
6167 if (slot
< map
->num_members
)
6168 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
6170 this->array
.spare_disks
++;
6172 if (info_d
->recovery_start
== MaxSector
)
6173 this->array
.working_disks
++;
6175 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
6176 info_d
->data_offset
= __le32_to_cpu(map
->pba_of_lba0
);
6177 info_d
->component_size
= __le32_to_cpu(map
->blocks_per_member
);
6179 /* now that the disk list is up-to-date fixup recovery_start */
6180 update_recovery_start(super
, dev
, this);
6181 this->array
.spare_disks
+= spare_disks
;
6184 /* check for reshape */
6185 if (this->reshape_active
== 1)
6186 recover_backup_imsm(st
, this);
6195 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
6196 int failed
, int look_in_map
)
6198 struct imsm_map
*map
;
6200 map
= get_imsm_map(dev
, look_in_map
);
6203 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
6204 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
6206 switch (get_imsm_raid_level(map
)) {
6208 return IMSM_T_STATE_FAILED
;
6211 if (failed
< map
->num_members
)
6212 return IMSM_T_STATE_DEGRADED
;
6214 return IMSM_T_STATE_FAILED
;
6219 * check to see if any mirrors have failed, otherwise we
6220 * are degraded. Even numbered slots are mirrored on
6224 /* gcc -Os complains that this is unused */
6225 int insync
= insync
;
6227 for (i
= 0; i
< map
->num_members
; i
++) {
6228 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_X
);
6229 int idx
= ord_to_idx(ord
);
6230 struct imsm_disk
*disk
;
6232 /* reset the potential in-sync count on even-numbered
6233 * slots. num_copies is always 2 for imsm raid10
6238 disk
= get_imsm_disk(super
, idx
);
6239 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
6242 /* no in-sync disks left in this mirror the
6246 return IMSM_T_STATE_FAILED
;
6249 return IMSM_T_STATE_DEGRADED
;
6253 return IMSM_T_STATE_DEGRADED
;
6255 return IMSM_T_STATE_FAILED
;
6261 return map
->map_state
;
6264 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
6269 struct imsm_disk
*disk
;
6270 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
6271 struct imsm_map
*prev
= get_imsm_map(dev
, MAP_1
);
6272 struct imsm_map
*map_for_loop
;
6277 /* at the beginning of migration we set IMSM_ORD_REBUILD on
6278 * disks that are being rebuilt. New failures are recorded to
6279 * map[0]. So we look through all the disks we started with and
6280 * see if any failures are still present, or if any new ones
6284 if (prev
&& (map
->num_members
< prev
->num_members
))
6285 map_for_loop
= prev
;
6287 for (i
= 0; i
< map_for_loop
->num_members
; i
++) {
6289 /* when MAP_X is passed both maps failures are counted
6292 ((look_in_map
== MAP_1
) || (look_in_map
== MAP_X
)) &&
6293 (i
< prev
->num_members
)) {
6294 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
6295 idx_1
= ord_to_idx(ord
);
6297 disk
= get_imsm_disk(super
, idx_1
);
6298 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
6301 if (((look_in_map
== MAP_0
) || (look_in_map
== MAP_X
)) &&
6302 (i
< map
->num_members
)) {
6303 ord
= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
6304 idx
= ord_to_idx(ord
);
6307 disk
= get_imsm_disk(super
, idx
);
6308 if (!disk
|| is_failed(disk
) ||
6309 ord
& IMSM_ORD_REBUILD
)
6319 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
6322 struct intel_super
*super
= c
->sb
;
6323 struct imsm_super
*mpb
= super
->anchor
;
6325 if (atoi(inst
) >= mpb
->num_raid_devs
) {
6326 fprintf(stderr
, "%s: subarry index %d, out of range\n",
6327 __func__
, atoi(inst
));
6331 dprintf("imsm: open_new %s\n", inst
);
6332 a
->info
.container_member
= atoi(inst
);
6336 static int is_resyncing(struct imsm_dev
*dev
)
6338 struct imsm_map
*migr_map
;
6340 if (!dev
->vol
.migr_state
)
6343 if (migr_type(dev
) == MIGR_INIT
||
6344 migr_type(dev
) == MIGR_REPAIR
)
6347 if (migr_type(dev
) == MIGR_GEN_MIGR
)
6350 migr_map
= get_imsm_map(dev
, MAP_1
);
6352 if ((migr_map
->map_state
== IMSM_T_STATE_NORMAL
) &&
6353 (dev
->vol
.migr_type
!= MIGR_GEN_MIGR
))
6359 /* return true if we recorded new information */
6360 static int mark_failure(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
6364 struct imsm_map
*map
;
6365 char buf
[MAX_RAID_SERIAL_LEN
+3];
6366 unsigned int len
, shift
= 0;
6368 /* new failures are always set in map[0] */
6369 map
= get_imsm_map(dev
, MAP_0
);
6371 slot
= get_imsm_disk_slot(map
, idx
);
6375 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
6376 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
6379 memcpy(buf
, disk
->serial
, MAX_RAID_SERIAL_LEN
);
6380 buf
[MAX_RAID_SERIAL_LEN
] = '\000';
6382 if ((len
= strlen(buf
)) >= MAX_RAID_SERIAL_LEN
)
6383 shift
= len
- MAX_RAID_SERIAL_LEN
+ 1;
6384 strncpy((char *)disk
->serial
, &buf
[shift
], MAX_RAID_SERIAL_LEN
);
6386 disk
->status
|= FAILED_DISK
;
6387 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
6388 /* mark failures in second map if second map exists and this disk
6390 * This is valid for migration, initialization and rebuild
6392 if (dev
->vol
.migr_state
) {
6393 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
6394 int slot2
= get_imsm_disk_slot(map2
, idx
);
6396 if ((slot2
< map2
->num_members
) &&
6398 set_imsm_ord_tbl_ent(map2
, slot2
,
6399 idx
| IMSM_ORD_REBUILD
);
6401 if (map
->failed_disk_num
== 0xff)
6402 map
->failed_disk_num
= slot
;
6406 static void mark_missing(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
6408 mark_failure(dev
, disk
, idx
);
6410 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
6413 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
6414 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
6417 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
6421 if (!super
->missing
)
6424 dprintf("imsm: mark missing\n");
6425 /* end process for initialization and rebuild only
6427 if (is_gen_migration(dev
) == 0) {
6431 failed
= imsm_count_failed(super
, dev
, MAP_0
);
6432 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
6434 end_migration(dev
, super
, map_state
);
6436 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
6437 mark_missing(dev
, &dl
->disk
, dl
->index
);
6438 super
->updates_pending
++;
6441 static unsigned long long imsm_set_array_size(struct imsm_dev
*dev
)
6443 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
6444 unsigned long long array_blocks
;
6445 struct imsm_map
*map
;
6447 if (used_disks
== 0) {
6448 /* when problems occures
6449 * return current array_blocks value
6451 array_blocks
= __le32_to_cpu(dev
->size_high
);
6452 array_blocks
= array_blocks
<< 32;
6453 array_blocks
+= __le32_to_cpu(dev
->size_low
);
6455 return array_blocks
;
6458 /* set array size in metadata
6460 map
= get_imsm_map(dev
, MAP_0
);
6461 array_blocks
= map
->blocks_per_member
* used_disks
;
6463 /* round array size down to closest MB
6465 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
6466 dev
->size_low
= __cpu_to_le32((__u32
)array_blocks
);
6467 dev
->size_high
= __cpu_to_le32((__u32
)(array_blocks
>> 32));
6469 return array_blocks
;
6472 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
6474 static void imsm_progress_container_reshape(struct intel_super
*super
)
6476 /* if no device has a migr_state, but some device has a
6477 * different number of members than the previous device, start
6478 * changing the number of devices in this device to match
6481 struct imsm_super
*mpb
= super
->anchor
;
6482 int prev_disks
= -1;
6486 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6487 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
6488 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
6489 struct imsm_map
*map2
;
6490 int prev_num_members
;
6492 if (dev
->vol
.migr_state
)
6495 if (prev_disks
== -1)
6496 prev_disks
= map
->num_members
;
6497 if (prev_disks
== map
->num_members
)
6500 /* OK, this array needs to enter reshape mode.
6501 * i.e it needs a migr_state
6504 copy_map_size
= sizeof_imsm_map(map
);
6505 prev_num_members
= map
->num_members
;
6506 map
->num_members
= prev_disks
;
6507 dev
->vol
.migr_state
= 1;
6508 dev
->vol
.curr_migr_unit
= 0;
6509 set_migr_type(dev
, MIGR_GEN_MIGR
);
6510 for (i
= prev_num_members
;
6511 i
< map
->num_members
; i
++)
6512 set_imsm_ord_tbl_ent(map
, i
, i
);
6513 map2
= get_imsm_map(dev
, MAP_1
);
6514 /* Copy the current map */
6515 memcpy(map2
, map
, copy_map_size
);
6516 map2
->num_members
= prev_num_members
;
6518 imsm_set_array_size(dev
);
6519 super
->updates_pending
++;
6523 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
6524 * states are handled in imsm_set_disk() with one exception, when a
6525 * resync is stopped due to a new failure this routine will set the
6526 * 'degraded' state for the array.
6528 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
6530 int inst
= a
->info
.container_member
;
6531 struct intel_super
*super
= a
->container
->sb
;
6532 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
6533 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
6534 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
6535 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
6536 __u32 blocks_per_unit
;
6538 if (dev
->vol
.migr_state
&&
6539 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
6540 /* array state change is blocked due to reshape action
6542 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
6543 * - finish the reshape (if last_checkpoint is big and action != reshape)
6544 * - update curr_migr_unit
6546 if (a
->curr_action
== reshape
) {
6547 /* still reshaping, maybe update curr_migr_unit */
6548 goto mark_checkpoint
;
6550 if (a
->last_checkpoint
== 0 && a
->prev_action
== reshape
) {
6551 /* for some reason we aborted the reshape.
6553 * disable automatic metadata rollback
6554 * user action is required to recover process
6557 struct imsm_map
*map2
=
6558 get_imsm_map(dev
, MAP_1
);
6559 dev
->vol
.migr_state
= 0;
6560 set_migr_type(dev
, 0);
6561 dev
->vol
.curr_migr_unit
= 0;
6563 sizeof_imsm_map(map2
));
6564 super
->updates_pending
++;
6567 if (a
->last_checkpoint
>= a
->info
.component_size
) {
6568 unsigned long long array_blocks
;
6572 used_disks
= imsm_num_data_members(dev
, MAP_0
);
6573 if (used_disks
> 0) {
6575 map
->blocks_per_member
*
6577 /* round array size down to closest MB
6579 array_blocks
= (array_blocks
6580 >> SECT_PER_MB_SHIFT
)
6581 << SECT_PER_MB_SHIFT
;
6582 a
->info
.custom_array_size
= array_blocks
;
6583 /* encourage manager to update array
6587 a
->check_reshape
= 1;
6589 /* finalize online capacity expansion/reshape */
6590 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
6592 mdi
->disk
.raid_disk
,
6595 imsm_progress_container_reshape(super
);
6600 /* before we activate this array handle any missing disks */
6601 if (consistent
== 2)
6602 handle_missing(super
, dev
);
6604 if (consistent
== 2 &&
6605 (!is_resync_complete(&a
->info
) ||
6606 map_state
!= IMSM_T_STATE_NORMAL
||
6607 dev
->vol
.migr_state
))
6610 if (is_resync_complete(&a
->info
)) {
6611 /* complete intialization / resync,
6612 * recovery and interrupted recovery is completed in
6615 if (is_resyncing(dev
)) {
6616 dprintf("imsm: mark resync done\n");
6617 end_migration(dev
, super
, map_state
);
6618 super
->updates_pending
++;
6619 a
->last_checkpoint
= 0;
6621 } else if ((!is_resyncing(dev
) && !failed
) &&
6622 (imsm_reshape_blocks_arrays_changes(super
) == 0)) {
6623 /* mark the start of the init process if nothing is failed */
6624 dprintf("imsm: mark resync start\n");
6625 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
6626 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
6628 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
6629 super
->updates_pending
++;
6633 /* skip checkpointing for general migration,
6634 * it is controlled in mdadm
6636 if (is_gen_migration(dev
))
6637 goto skip_mark_checkpoint
;
6639 /* check if we can update curr_migr_unit from resync_start, recovery_start */
6640 blocks_per_unit
= blocks_per_migr_unit(super
, dev
);
6641 if (blocks_per_unit
) {
6645 units
= a
->last_checkpoint
/ blocks_per_unit
;
6648 /* check that we did not overflow 32-bits, and that
6649 * curr_migr_unit needs updating
6651 if (units32
== units
&&
6653 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
6654 dprintf("imsm: mark checkpoint (%u)\n", units32
);
6655 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
6656 super
->updates_pending
++;
6660 skip_mark_checkpoint
:
6661 /* mark dirty / clean */
6662 if (dev
->vol
.dirty
!= !consistent
) {
6663 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
6668 super
->updates_pending
++;
6674 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
6676 int inst
= a
->info
.container_member
;
6677 struct intel_super
*super
= a
->container
->sb
;
6678 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
6679 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
6680 struct imsm_disk
*disk
;
6685 if (n
> map
->num_members
)
6686 fprintf(stderr
, "imsm: set_disk %d out of range 0..%d\n",
6687 n
, map
->num_members
- 1);
6692 dprintf("imsm: set_disk %d:%x\n", n
, state
);
6694 ord
= get_imsm_ord_tbl_ent(dev
, n
, MAP_0
);
6695 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
6697 /* check for new failures */
6698 if (state
& DS_FAULTY
) {
6699 if (mark_failure(dev
, disk
, ord_to_idx(ord
)))
6700 super
->updates_pending
++;
6703 /* check if in_sync */
6704 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
6705 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
6707 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
6708 super
->updates_pending
++;
6711 failed
= imsm_count_failed(super
, dev
, MAP_0
);
6712 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
6714 /* check if recovery complete, newly degraded, or failed */
6715 dprintf("imsm: Detected transition to state ");
6716 switch (map_state
) {
6717 case IMSM_T_STATE_NORMAL
: /* transition to normal state */
6718 dprintf("normal: ");
6719 if (is_rebuilding(dev
)) {
6720 dprintf("while rebuilding");
6721 end_migration(dev
, super
, map_state
);
6722 map
= get_imsm_map(dev
, MAP_0
);
6723 map
->failed_disk_num
= ~0;
6724 super
->updates_pending
++;
6725 a
->last_checkpoint
= 0;
6728 if (is_gen_migration(dev
)) {
6729 dprintf("while general migration");
6730 if (a
->last_checkpoint
>= a
->info
.component_size
)
6731 end_migration(dev
, super
, map_state
);
6733 map
->map_state
= map_state
;
6734 map
= get_imsm_map(dev
, MAP_0
);
6735 map
->failed_disk_num
= ~0;
6736 super
->updates_pending
++;
6740 case IMSM_T_STATE_DEGRADED
: /* transition to degraded state */
6741 dprintf("degraded: ");
6742 if ((map
->map_state
!= map_state
) &&
6743 !dev
->vol
.migr_state
) {
6744 dprintf("mark degraded");
6745 map
->map_state
= map_state
;
6746 super
->updates_pending
++;
6747 a
->last_checkpoint
= 0;
6750 if (is_rebuilding(dev
)) {
6751 dprintf("while rebuilding.");
6752 if (map
->map_state
!= map_state
) {
6753 dprintf(" Map state change");
6754 end_migration(dev
, super
, map_state
);
6755 super
->updates_pending
++;
6759 if (is_gen_migration(dev
)) {
6760 dprintf("while general migration");
6761 if (a
->last_checkpoint
>= a
->info
.component_size
)
6762 end_migration(dev
, super
, map_state
);
6764 map
->map_state
= map_state
;
6765 manage_second_map(super
, dev
);
6767 super
->updates_pending
++;
6770 if (is_initializing(dev
)) {
6771 dprintf("while initialization.");
6772 map
->map_state
= map_state
;
6773 super
->updates_pending
++;
6777 case IMSM_T_STATE_FAILED
: /* transition to failed state */
6778 dprintf("failed: ");
6779 if (is_gen_migration(dev
)) {
6780 dprintf("while general migration");
6781 map
->map_state
= map_state
;
6782 super
->updates_pending
++;
6785 if (map
->map_state
!= map_state
) {
6786 dprintf("mark failed");
6787 end_migration(dev
, super
, map_state
);
6788 super
->updates_pending
++;
6789 a
->last_checkpoint
= 0;
6794 dprintf("state %i\n", map_state
);
6800 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
6803 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
6804 unsigned long long dsize
;
6805 unsigned long long sectors
;
6807 get_dev_size(fd
, NULL
, &dsize
);
6809 if (mpb_size
> 512) {
6810 /* -1 to account for anchor */
6811 sectors
= mpb_sectors(mpb
) - 1;
6813 /* write the extended mpb to the sectors preceeding the anchor */
6814 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0)
6817 if ((unsigned long long)write(fd
, buf
+ 512, 512 * sectors
)
6822 /* first block is stored on second to last sector of the disk */
6823 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0)
6826 if (write(fd
, buf
, 512) != 512)
6832 static void imsm_sync_metadata(struct supertype
*container
)
6834 struct intel_super
*super
= container
->sb
;
6836 dprintf("sync metadata: %d\n", super
->updates_pending
);
6837 if (!super
->updates_pending
)
6840 write_super_imsm(container
, 0);
6842 super
->updates_pending
= 0;
6845 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
6847 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
6848 int i
= get_imsm_disk_idx(dev
, idx
, MAP_X
);
6851 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6855 if (dl
&& is_failed(&dl
->disk
))
6859 dprintf("%s: found %x:%x\n", __func__
, dl
->major
, dl
->minor
);
6864 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
6865 struct active_array
*a
, int activate_new
,
6866 struct mdinfo
*additional_test_list
)
6868 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
6869 int idx
= get_imsm_disk_idx(dev
, slot
, MAP_X
);
6870 struct imsm_super
*mpb
= super
->anchor
;
6871 struct imsm_map
*map
;
6872 unsigned long long pos
;
6877 __u32 array_start
= 0;
6878 __u32 array_end
= 0;
6880 struct mdinfo
*test_list
;
6882 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6883 /* If in this array, skip */
6884 for (d
= a
->info
.devs
; d
; d
= d
->next
)
6885 if (d
->state_fd
>= 0 &&
6886 d
->disk
.major
== dl
->major
&&
6887 d
->disk
.minor
== dl
->minor
) {
6888 dprintf("%x:%x already in array\n",
6889 dl
->major
, dl
->minor
);
6894 test_list
= additional_test_list
;
6896 if (test_list
->disk
.major
== dl
->major
&&
6897 test_list
->disk
.minor
== dl
->minor
) {
6898 dprintf("%x:%x already in additional test list\n",
6899 dl
->major
, dl
->minor
);
6902 test_list
= test_list
->next
;
6907 /* skip in use or failed drives */
6908 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
6910 dprintf("%x:%x status (failed: %d index: %d)\n",
6911 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
6915 /* skip pure spares when we are looking for partially
6916 * assimilated drives
6918 if (dl
->index
== -1 && !activate_new
)
6921 /* Does this unused device have the requisite free space?
6922 * It needs to be able to cover all member volumes
6924 ex
= get_extents(super
, dl
);
6926 dprintf("cannot get extents\n");
6929 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6930 dev
= get_imsm_dev(super
, i
);
6931 map
= get_imsm_map(dev
, MAP_0
);
6933 /* check if this disk is already a member of
6936 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
6942 array_start
= __le32_to_cpu(map
->pba_of_lba0
);
6943 array_end
= array_start
+
6944 __le32_to_cpu(map
->blocks_per_member
) - 1;
6947 /* check that we can start at pba_of_lba0 with
6948 * blocks_per_member of space
6950 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
6954 pos
= ex
[j
].start
+ ex
[j
].size
;
6956 } while (ex
[j
-1].size
);
6963 if (i
< mpb
->num_raid_devs
) {
6964 dprintf("%x:%x does not have %u to %u available\n",
6965 dl
->major
, dl
->minor
, array_start
, array_end
);
6976 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
6978 struct imsm_dev
*dev2
;
6979 struct imsm_map
*map
;
6985 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
6987 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
, MAP_0
);
6988 if (state
== IMSM_T_STATE_FAILED
) {
6989 map
= get_imsm_map(dev2
, MAP_0
);
6992 for (slot
= 0; slot
< map
->num_members
; slot
++) {
6994 * Check if failed disks are deleted from intel
6995 * disk list or are marked to be deleted
6997 idx
= get_imsm_disk_idx(dev2
, slot
, MAP_X
);
6998 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
7000 * Do not rebuild the array if failed disks
7001 * from failed sub-array are not removed from
7005 is_failed(&idisk
->disk
) &&
7006 (idisk
->action
!= DISK_REMOVE
))
7014 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
7015 struct metadata_update
**updates
)
7018 * Find a device with unused free space and use it to replace a
7019 * failed/vacant region in an array. We replace failed regions one a
7020 * array at a time. The result is that a new spare disk will be added
7021 * to the first failed array and after the monitor has finished
7022 * propagating failures the remainder will be consumed.
7024 * FIXME add a capability for mdmon to request spares from another
7028 struct intel_super
*super
= a
->container
->sb
;
7029 int inst
= a
->info
.container_member
;
7030 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7031 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7032 int failed
= a
->info
.array
.raid_disks
;
7033 struct mdinfo
*rv
= NULL
;
7036 struct metadata_update
*mu
;
7038 struct imsm_update_activate_spare
*u
;
7043 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
7044 if ((d
->curr_state
& DS_FAULTY
) &&
7046 /* wait for Removal to happen */
7048 if (d
->state_fd
>= 0)
7052 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
7053 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
7055 if (imsm_reshape_blocks_arrays_changes(super
))
7058 /* Cannot activate another spare if rebuild is in progress already
7060 if (is_rebuilding(dev
)) {
7061 dprintf("imsm: No spare activation allowed. "
7062 "Rebuild in progress already.\n");
7066 if (a
->info
.array
.level
== 4)
7067 /* No repair for takeovered array
7068 * imsm doesn't support raid4
7072 if (imsm_check_degraded(super
, dev
, failed
, MAP_0
) !=
7073 IMSM_T_STATE_DEGRADED
)
7077 * If there are any failed disks check state of the other volume.
7078 * Block rebuild if the another one is failed until failed disks
7079 * are removed from container.
7082 dprintf("found failed disks in %.*s, check if there another"
7083 "failed sub-array.\n",
7084 MAX_RAID_SERIAL_LEN
, dev
->volume
);
7085 /* check if states of the other volumes allow for rebuild */
7086 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
7088 allowed
= imsm_rebuild_allowed(a
->container
,
7096 /* For each slot, if it is not working, find a spare */
7097 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
7098 for (d
= a
->info
.devs
; d
; d
= d
->next
)
7099 if (d
->disk
.raid_disk
== i
)
7101 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
7102 if (d
&& (d
->state_fd
>= 0))
7106 * OK, this device needs recovery. Try to re-add the
7107 * previous occupant of this slot, if this fails see if
7108 * we can continue the assimilation of a spare that was
7109 * partially assimilated, finally try to activate a new
7112 dl
= imsm_readd(super
, i
, a
);
7114 dl
= imsm_add_spare(super
, i
, a
, 0, rv
);
7116 dl
= imsm_add_spare(super
, i
, a
, 1, rv
);
7120 /* found a usable disk with enough space */
7121 di
= malloc(sizeof(*di
));
7124 memset(di
, 0, sizeof(*di
));
7126 /* dl->index will be -1 in the case we are activating a
7127 * pristine spare. imsm_process_update() will create a
7128 * new index in this case. Once a disk is found to be
7129 * failed in all member arrays it is kicked from the
7132 di
->disk
.number
= dl
->index
;
7134 /* (ab)use di->devs to store a pointer to the device
7137 di
->devs
= (struct mdinfo
*) dl
;
7139 di
->disk
.raid_disk
= i
;
7140 di
->disk
.major
= dl
->major
;
7141 di
->disk
.minor
= dl
->minor
;
7143 di
->recovery_start
= 0;
7144 di
->data_offset
= __le32_to_cpu(map
->pba_of_lba0
);
7145 di
->component_size
= a
->info
.component_size
;
7146 di
->container_member
= inst
;
7147 super
->random
= random32();
7151 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
7152 i
, di
->data_offset
);
7156 /* No spares found */
7158 /* Now 'rv' has a list of devices to return.
7159 * Create a metadata_update record to update the
7160 * disk_ord_tbl for the array
7162 mu
= malloc(sizeof(*mu
));
7164 mu
->buf
= malloc(sizeof(struct imsm_update_activate_spare
) * num_spares
);
7165 if (mu
->buf
== NULL
) {
7172 struct mdinfo
*n
= rv
->next
;
7181 mu
->space_list
= NULL
;
7182 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
7183 mu
->next
= *updates
;
7184 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
7186 for (di
= rv
; di
; di
= di
->next
) {
7187 u
->type
= update_activate_spare
;
7188 u
->dl
= (struct dl
*) di
->devs
;
7190 u
->slot
= di
->disk
.raid_disk
;
7201 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
7203 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
7204 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7205 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, MAP_0
);
7206 struct disk_info
*inf
= get_disk_info(u
);
7207 struct imsm_disk
*disk
;
7211 for (i
= 0; i
< map
->num_members
; i
++) {
7212 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, MAP_X
));
7213 for (j
= 0; j
< new_map
->num_members
; j
++)
7214 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
7222 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
7224 struct dl
*dl
= NULL
;
7225 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7226 if ((dl
->major
== major
) && (dl
->minor
== minor
))
7231 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
7233 struct dl
*prev
= NULL
;
7237 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7238 if ((dl
->major
== major
) && (dl
->minor
== minor
)) {
7241 prev
->next
= dl
->next
;
7243 super
->disks
= dl
->next
;
7245 __free_imsm_disk(dl
);
7246 dprintf("%s: removed %x:%x\n",
7247 __func__
, major
, minor
);
7255 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
7257 static int add_remove_disk_update(struct intel_super
*super
)
7259 int check_degraded
= 0;
7260 struct dl
*disk
= NULL
;
7261 /* add/remove some spares to/from the metadata/contrainer */
7262 while (super
->disk_mgmt_list
) {
7263 struct dl
*disk_cfg
;
7265 disk_cfg
= super
->disk_mgmt_list
;
7266 super
->disk_mgmt_list
= disk_cfg
->next
;
7267 disk_cfg
->next
= NULL
;
7269 if (disk_cfg
->action
== DISK_ADD
) {
7270 disk_cfg
->next
= super
->disks
;
7271 super
->disks
= disk_cfg
;
7273 dprintf("%s: added %x:%x\n",
7274 __func__
, disk_cfg
->major
,
7276 } else if (disk_cfg
->action
== DISK_REMOVE
) {
7277 dprintf("Disk remove action processed: %x.%x\n",
7278 disk_cfg
->major
, disk_cfg
->minor
);
7279 disk
= get_disk_super(super
,
7283 /* store action status */
7284 disk
->action
= DISK_REMOVE
;
7285 /* remove spare disks only */
7286 if (disk
->index
== -1) {
7287 remove_disk_super(super
,
7292 /* release allocate disk structure */
7293 __free_imsm_disk(disk_cfg
);
7296 return check_degraded
;
7300 static int apply_reshape_migration_update(struct imsm_update_reshape_migration
*u
,
7301 struct intel_super
*super
,
7304 struct intel_dev
*id
;
7305 void **tofree
= NULL
;
7308 dprintf("apply_reshape_migration_update()\n");
7309 if ((u
->subdev
< 0) ||
7311 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
7314 if ((space_list
== NULL
) || (*space_list
== NULL
)) {
7315 dprintf("imsm: Error: Memory is not allocated\n");
7319 for (id
= super
->devlist
; id
; id
= id
->next
) {
7320 if (id
->index
== (unsigned)u
->subdev
) {
7321 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
7322 struct imsm_map
*map
;
7323 struct imsm_dev
*new_dev
=
7324 (struct imsm_dev
*)*space_list
;
7325 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
7327 struct dl
*new_disk
;
7329 if (new_dev
== NULL
)
7331 *space_list
= **space_list
;
7332 memcpy(new_dev
, dev
, sizeof_imsm_dev(dev
, 0));
7333 map
= get_imsm_map(new_dev
, MAP_0
);
7335 dprintf("imsm: Error: migration in progress");
7339 to_state
= map
->map_state
;
7340 if ((u
->new_level
== 5) && (map
->raid_level
== 0)) {
7342 /* this should not happen */
7343 if (u
->new_disks
[0] < 0) {
7344 map
->failed_disk_num
=
7345 map
->num_members
- 1;
7346 to_state
= IMSM_T_STATE_DEGRADED
;
7348 to_state
= IMSM_T_STATE_NORMAL
;
7350 migrate(new_dev
, super
, to_state
, MIGR_GEN_MIGR
);
7351 if (u
->new_level
> -1)
7352 map
->raid_level
= u
->new_level
;
7353 migr_map
= get_imsm_map(new_dev
, MAP_1
);
7354 if ((u
->new_level
== 5) &&
7355 (migr_map
->raid_level
== 0)) {
7356 int ord
= map
->num_members
- 1;
7357 migr_map
->num_members
--;
7358 if (u
->new_disks
[0] < 0)
7359 ord
|= IMSM_ORD_REBUILD
;
7360 set_imsm_ord_tbl_ent(map
,
7361 map
->num_members
- 1,
7365 tofree
= (void **)dev
;
7367 /* update chunk size
7369 if (u
->new_chunksize
> 0)
7370 map
->blocks_per_strip
=
7371 __cpu_to_le16(u
->new_chunksize
* 2);
7375 if ((u
->new_level
!= 5) ||
7376 (migr_map
->raid_level
!= 0) ||
7377 (migr_map
->raid_level
== map
->raid_level
))
7380 if (u
->new_disks
[0] >= 0) {
7383 new_disk
= get_disk_super(super
,
7384 major(u
->new_disks
[0]),
7385 minor(u
->new_disks
[0]));
7386 dprintf("imsm: new disk for reshape is: %i:%i "
7387 "(%p, index = %i)\n",
7388 major(u
->new_disks
[0]),
7389 minor(u
->new_disks
[0]),
7390 new_disk
, new_disk
->index
);
7391 if (new_disk
== NULL
)
7392 goto error_disk_add
;
7394 new_disk
->index
= map
->num_members
- 1;
7395 /* slot to fill in autolayout
7397 new_disk
->raiddisk
= new_disk
->index
;
7398 new_disk
->disk
.status
|= CONFIGURED_DISK
;
7399 new_disk
->disk
.status
&= ~SPARE_DISK
;
7401 goto error_disk_add
;
7404 *tofree
= *space_list
;
7405 /* calculate new size
7407 imsm_set_array_size(new_dev
);
7414 *space_list
= tofree
;
7418 dprintf("Error: imsm: Cannot find disk.\n");
7422 static int apply_update_activate_spare(struct imsm_update_activate_spare
*u
,
7423 struct intel_super
*super
,
7424 struct active_array
*active_array
)
7426 struct imsm_super
*mpb
= super
->anchor
;
7427 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
7428 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7429 struct imsm_map
*migr_map
;
7430 struct active_array
*a
;
7431 struct imsm_disk
*disk
;
7438 int second_map_created
= 0;
7440 for (; u
; u
= u
->next
) {
7441 victim
= get_imsm_disk_idx(dev
, u
->slot
, MAP_X
);
7446 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7451 fprintf(stderr
, "error: imsm_activate_spare passed "
7452 "an unknown disk (index: %d)\n",
7457 /* count failures (excluding rebuilds and the victim)
7458 * to determine map[0] state
7461 for (i
= 0; i
< map
->num_members
; i
++) {
7464 disk
= get_imsm_disk(super
,
7465 get_imsm_disk_idx(dev
, i
, MAP_X
));
7466 if (!disk
|| is_failed(disk
))
7470 /* adding a pristine spare, assign a new index */
7471 if (dl
->index
< 0) {
7472 dl
->index
= super
->anchor
->num_disks
;
7473 super
->anchor
->num_disks
++;
7476 disk
->status
|= CONFIGURED_DISK
;
7477 disk
->status
&= ~SPARE_DISK
;
7480 to_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7481 if (!second_map_created
) {
7482 second_map_created
= 1;
7483 map
->map_state
= IMSM_T_STATE_DEGRADED
;
7484 migrate(dev
, super
, to_state
, MIGR_REBUILD
);
7486 map
->map_state
= to_state
;
7487 migr_map
= get_imsm_map(dev
, MAP_1
);
7488 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
7489 set_imsm_ord_tbl_ent(migr_map
, u
->slot
,
7490 dl
->index
| IMSM_ORD_REBUILD
);
7492 /* update the family_num to mark a new container
7493 * generation, being careful to record the existing
7494 * family_num in orig_family_num to clean up after
7495 * earlier mdadm versions that neglected to set it.
7497 if (mpb
->orig_family_num
== 0)
7498 mpb
->orig_family_num
= mpb
->family_num
;
7499 mpb
->family_num
+= super
->random
;
7501 /* count arrays using the victim in the metadata */
7503 for (a
= active_array
; a
; a
= a
->next
) {
7504 dev
= get_imsm_dev(super
, a
->info
.container_member
);
7505 map
= get_imsm_map(dev
, MAP_0
);
7507 if (get_imsm_disk_slot(map
, victim
) >= 0)
7511 /* delete the victim if it is no longer being
7517 /* We know that 'manager' isn't touching anything,
7518 * so it is safe to delete
7520 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
7521 if ((*dlp
)->index
== victim
)
7524 /* victim may be on the missing list */
7526 for (dlp
= &super
->missing
; *dlp
;
7527 dlp
= &(*dlp
)->next
)
7528 if ((*dlp
)->index
== victim
)
7530 imsm_delete(super
, dlp
, victim
);
7537 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
7538 struct intel_super
*super
,
7541 struct dl
*new_disk
;
7542 struct intel_dev
*id
;
7544 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
7545 int disk_count
= u
->old_raid_disks
;
7546 void **tofree
= NULL
;
7547 int devices_to_reshape
= 1;
7548 struct imsm_super
*mpb
= super
->anchor
;
7550 unsigned int dev_id
;
7552 dprintf("imsm: apply_reshape_container_disks_update()\n");
7554 /* enable spares to use in array */
7555 for (i
= 0; i
< delta_disks
; i
++) {
7556 new_disk
= get_disk_super(super
,
7557 major(u
->new_disks
[i
]),
7558 minor(u
->new_disks
[i
]));
7559 dprintf("imsm: new disk for reshape is: %i:%i "
7560 "(%p, index = %i)\n",
7561 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
7562 new_disk
, new_disk
->index
);
7563 if ((new_disk
== NULL
) ||
7564 ((new_disk
->index
>= 0) &&
7565 (new_disk
->index
< u
->old_raid_disks
)))
7566 goto update_reshape_exit
;
7567 new_disk
->index
= disk_count
++;
7568 /* slot to fill in autolayout
7570 new_disk
->raiddisk
= new_disk
->index
;
7571 new_disk
->disk
.status
|=
7573 new_disk
->disk
.status
&= ~SPARE_DISK
;
7576 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
7577 mpb
->num_raid_devs
);
7578 /* manage changes in volume
7580 for (dev_id
= 0; dev_id
< mpb
->num_raid_devs
; dev_id
++) {
7581 void **sp
= *space_list
;
7582 struct imsm_dev
*newdev
;
7583 struct imsm_map
*newmap
, *oldmap
;
7585 for (id
= super
->devlist
; id
; id
= id
->next
) {
7586 if (id
->index
== dev_id
)
7595 /* Copy the dev, but not (all of) the map */
7596 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
7597 oldmap
= get_imsm_map(id
->dev
, MAP_0
);
7598 newmap
= get_imsm_map(newdev
, MAP_0
);
7599 /* Copy the current map */
7600 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
7601 /* update one device only
7603 if (devices_to_reshape
) {
7604 dprintf("imsm: modifying subdev: %i\n",
7606 devices_to_reshape
--;
7607 newdev
->vol
.migr_state
= 1;
7608 newdev
->vol
.curr_migr_unit
= 0;
7609 set_migr_type(newdev
, MIGR_GEN_MIGR
);
7610 newmap
->num_members
= u
->new_raid_disks
;
7611 for (i
= 0; i
< delta_disks
; i
++) {
7612 set_imsm_ord_tbl_ent(newmap
,
7613 u
->old_raid_disks
+ i
,
7614 u
->old_raid_disks
+ i
);
7616 /* New map is correct, now need to save old map
7618 newmap
= get_imsm_map(newdev
, MAP_1
);
7619 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
7621 imsm_set_array_size(newdev
);
7624 sp
= (void **)id
->dev
;
7629 /* Clear migration record */
7630 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
7633 *space_list
= tofree
;
7636 update_reshape_exit
:
7641 static int apply_takeover_update(struct imsm_update_takeover
*u
,
7642 struct intel_super
*super
,
7645 struct imsm_dev
*dev
= NULL
;
7646 struct intel_dev
*dv
;
7647 struct imsm_dev
*dev_new
;
7648 struct imsm_map
*map
;
7652 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
7653 if (dv
->index
== (unsigned int)u
->subarray
) {
7661 map
= get_imsm_map(dev
, MAP_0
);
7663 if (u
->direction
== R10_TO_R0
) {
7664 /* Number of failed disks must be half of initial disk number */
7665 if (imsm_count_failed(super
, dev
, MAP_0
) !=
7666 (map
->num_members
/ 2))
7669 /* iterate through devices to mark removed disks as spare */
7670 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
7671 if (dm
->disk
.status
& FAILED_DISK
) {
7672 int idx
= dm
->index
;
7673 /* update indexes on the disk list */
7674 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
7675 the index values will end up being correct.... NB */
7676 for (du
= super
->disks
; du
; du
= du
->next
)
7677 if (du
->index
> idx
)
7679 /* mark as spare disk */
7684 map
->num_members
= map
->num_members
/ 2;
7685 map
->map_state
= IMSM_T_STATE_NORMAL
;
7686 map
->num_domains
= 1;
7687 map
->raid_level
= 0;
7688 map
->failed_disk_num
= -1;
7691 if (u
->direction
== R0_TO_R10
) {
7693 /* update slots in current disk list */
7694 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
7698 /* create new *missing* disks */
7699 for (i
= 0; i
< map
->num_members
; i
++) {
7700 space
= *space_list
;
7703 *space_list
= *space
;
7705 memcpy(du
, super
->disks
, sizeof(*du
));
7709 du
->index
= (i
* 2) + 1;
7710 sprintf((char *)du
->disk
.serial
,
7711 " MISSING_%d", du
->index
);
7712 sprintf((char *)du
->serial
,
7713 "MISSING_%d", du
->index
);
7714 du
->next
= super
->missing
;
7715 super
->missing
= du
;
7717 /* create new dev and map */
7718 space
= *space_list
;
7721 *space_list
= *space
;
7722 dev_new
= (void *)space
;
7723 memcpy(dev_new
, dev
, sizeof(*dev
));
7724 /* update new map */
7725 map
= get_imsm_map(dev_new
, MAP_0
);
7726 map
->num_members
= map
->num_members
* 2;
7727 map
->map_state
= IMSM_T_STATE_DEGRADED
;
7728 map
->num_domains
= 2;
7729 map
->raid_level
= 1;
7730 /* replace dev<->dev_new */
7733 /* update disk order table */
7734 for (du
= super
->disks
; du
; du
= du
->next
)
7736 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
7737 for (du
= super
->missing
; du
; du
= du
->next
)
7738 if (du
->index
>= 0) {
7739 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
7740 mark_missing(dv
->dev
, &du
->disk
, du
->index
);
7746 static void imsm_process_update(struct supertype
*st
,
7747 struct metadata_update
*update
)
7750 * crack open the metadata_update envelope to find the update record
7751 * update can be one of:
7752 * update_reshape_container_disks - all the arrays in the container
7753 * are being reshaped to have more devices. We need to mark
7754 * the arrays for general migration and convert selected spares
7755 * into active devices.
7756 * update_activate_spare - a spare device has replaced a failed
7757 * device in an array, update the disk_ord_tbl. If this disk is
7758 * present in all member arrays then also clear the SPARE_DISK
7760 * update_create_array
7762 * update_rename_array
7763 * update_add_remove_disk
7765 struct intel_super
*super
= st
->sb
;
7766 struct imsm_super
*mpb
;
7767 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
7769 /* update requires a larger buf but the allocation failed */
7770 if (super
->next_len
&& !super
->next_buf
) {
7771 super
->next_len
= 0;
7775 if (super
->next_buf
) {
7776 memcpy(super
->next_buf
, super
->buf
, super
->len
);
7778 super
->len
= super
->next_len
;
7779 super
->buf
= super
->next_buf
;
7781 super
->next_len
= 0;
7782 super
->next_buf
= NULL
;
7785 mpb
= super
->anchor
;
7788 case update_general_migration_checkpoint
: {
7789 struct intel_dev
*id
;
7790 struct imsm_update_general_migration_checkpoint
*u
=
7791 (void *)update
->buf
;
7793 dprintf("imsm: process_update() "
7794 "for update_general_migration_checkpoint called\n");
7796 /* find device under general migration */
7797 for (id
= super
->devlist
; id
; id
= id
->next
) {
7798 if (is_gen_migration(id
->dev
)) {
7799 id
->dev
->vol
.curr_migr_unit
=
7800 __cpu_to_le32(u
->curr_migr_unit
);
7801 super
->updates_pending
++;
7806 case update_takeover
: {
7807 struct imsm_update_takeover
*u
= (void *)update
->buf
;
7808 if (apply_takeover_update(u
, super
, &update
->space_list
)) {
7809 imsm_update_version_info(super
);
7810 super
->updates_pending
++;
7815 case update_reshape_container_disks
: {
7816 struct imsm_update_reshape
*u
= (void *)update
->buf
;
7817 if (apply_reshape_container_disks_update(
7818 u
, super
, &update
->space_list
))
7819 super
->updates_pending
++;
7822 case update_reshape_migration
: {
7823 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
7824 if (apply_reshape_migration_update(
7825 u
, super
, &update
->space_list
))
7826 super
->updates_pending
++;
7829 case update_activate_spare
: {
7830 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
7831 if (apply_update_activate_spare(u
, super
, st
->arrays
))
7832 super
->updates_pending
++;
7835 case update_create_array
: {
7836 /* someone wants to create a new array, we need to be aware of
7837 * a few races/collisions:
7838 * 1/ 'Create' called by two separate instances of mdadm
7839 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
7840 * devices that have since been assimilated via
7842 * In the event this update can not be carried out mdadm will
7843 * (FIX ME) notice that its update did not take hold.
7845 struct imsm_update_create_array
*u
= (void *) update
->buf
;
7846 struct intel_dev
*dv
;
7847 struct imsm_dev
*dev
;
7848 struct imsm_map
*map
, *new_map
;
7849 unsigned long long start
, end
;
7850 unsigned long long new_start
, new_end
;
7852 struct disk_info
*inf
;
7855 /* handle racing creates: first come first serve */
7856 if (u
->dev_idx
< mpb
->num_raid_devs
) {
7857 dprintf("%s: subarray %d already defined\n",
7858 __func__
, u
->dev_idx
);
7862 /* check update is next in sequence */
7863 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
7864 dprintf("%s: can not create array %d expected index %d\n",
7865 __func__
, u
->dev_idx
, mpb
->num_raid_devs
);
7869 new_map
= get_imsm_map(&u
->dev
, MAP_0
);
7870 new_start
= __le32_to_cpu(new_map
->pba_of_lba0
);
7871 new_end
= new_start
+ __le32_to_cpu(new_map
->blocks_per_member
);
7872 inf
= get_disk_info(u
);
7874 /* handle activate_spare versus create race:
7875 * check to make sure that overlapping arrays do not include
7878 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7879 dev
= get_imsm_dev(super
, i
);
7880 map
= get_imsm_map(dev
, MAP_0
);
7881 start
= __le32_to_cpu(map
->pba_of_lba0
);
7882 end
= start
+ __le32_to_cpu(map
->blocks_per_member
);
7883 if ((new_start
>= start
&& new_start
<= end
) ||
7884 (start
>= new_start
&& start
<= new_end
))
7889 if (disks_overlap(super
, i
, u
)) {
7890 dprintf("%s: arrays overlap\n", __func__
);
7895 /* check that prepare update was successful */
7896 if (!update
->space
) {
7897 dprintf("%s: prepare update failed\n", __func__
);
7901 /* check that all disks are still active before committing
7902 * changes. FIXME: could we instead handle this by creating a
7903 * degraded array? That's probably not what the user expects,
7904 * so better to drop this update on the floor.
7906 for (i
= 0; i
< new_map
->num_members
; i
++) {
7907 dl
= serial_to_dl(inf
[i
].serial
, super
);
7909 dprintf("%s: disk disappeared\n", __func__
);
7914 super
->updates_pending
++;
7916 /* convert spares to members and fixup ord_tbl */
7917 for (i
= 0; i
< new_map
->num_members
; i
++) {
7918 dl
= serial_to_dl(inf
[i
].serial
, super
);
7919 if (dl
->index
== -1) {
7920 dl
->index
= mpb
->num_disks
;
7922 dl
->disk
.status
|= CONFIGURED_DISK
;
7923 dl
->disk
.status
&= ~SPARE_DISK
;
7925 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
7930 update
->space
= NULL
;
7931 imsm_copy_dev(dev
, &u
->dev
);
7932 dv
->index
= u
->dev_idx
;
7933 dv
->next
= super
->devlist
;
7934 super
->devlist
= dv
;
7935 mpb
->num_raid_devs
++;
7937 imsm_update_version_info(super
);
7940 /* mdmon knows how to release update->space, but not
7941 * ((struct intel_dev *) update->space)->dev
7943 if (update
->space
) {
7949 case update_kill_array
: {
7950 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
7951 int victim
= u
->dev_idx
;
7952 struct active_array
*a
;
7953 struct intel_dev
**dp
;
7954 struct imsm_dev
*dev
;
7956 /* sanity check that we are not affecting the uuid of
7957 * active arrays, or deleting an active array
7959 * FIXME when immutable ids are available, but note that
7960 * we'll also need to fixup the invalidated/active
7961 * subarray indexes in mdstat
7963 for (a
= st
->arrays
; a
; a
= a
->next
)
7964 if (a
->info
.container_member
>= victim
)
7966 /* by definition if mdmon is running at least one array
7967 * is active in the container, so checking
7968 * mpb->num_raid_devs is just extra paranoia
7970 dev
= get_imsm_dev(super
, victim
);
7971 if (a
|| !dev
|| mpb
->num_raid_devs
== 1) {
7972 dprintf("failed to delete subarray-%d\n", victim
);
7976 for (dp
= &super
->devlist
; *dp
;)
7977 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
7980 if ((*dp
)->index
> (unsigned)victim
)
7984 mpb
->num_raid_devs
--;
7985 super
->updates_pending
++;
7988 case update_rename_array
: {
7989 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
7990 char name
[MAX_RAID_SERIAL_LEN
+1];
7991 int target
= u
->dev_idx
;
7992 struct active_array
*a
;
7993 struct imsm_dev
*dev
;
7995 /* sanity check that we are not affecting the uuid of
7998 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
7999 name
[MAX_RAID_SERIAL_LEN
] = '\0';
8000 for (a
= st
->arrays
; a
; a
= a
->next
)
8001 if (a
->info
.container_member
== target
)
8003 dev
= get_imsm_dev(super
, u
->dev_idx
);
8004 if (a
|| !dev
|| !check_name(super
, name
, 1)) {
8005 dprintf("failed to rename subarray-%d\n", target
);
8009 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
8010 super
->updates_pending
++;
8013 case update_add_remove_disk
: {
8014 /* we may be able to repair some arrays if disks are
8015 * being added, check teh status of add_remove_disk
8016 * if discs has been added.
8018 if (add_remove_disk_update(super
)) {
8019 struct active_array
*a
;
8021 super
->updates_pending
++;
8022 for (a
= st
->arrays
; a
; a
= a
->next
)
8023 a
->check_degraded
= 1;
8028 fprintf(stderr
, "error: unsuported process update type:"
8029 "(type: %d)\n", type
);
8033 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
);
8035 static void imsm_prepare_update(struct supertype
*st
,
8036 struct metadata_update
*update
)
8039 * Allocate space to hold new disk entries, raid-device entries or a new
8040 * mpb if necessary. The manager synchronously waits for updates to
8041 * complete in the monitor, so new mpb buffers allocated here can be
8042 * integrated by the monitor thread without worrying about live pointers
8043 * in the manager thread.
8045 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
8046 struct intel_super
*super
= st
->sb
;
8047 struct imsm_super
*mpb
= super
->anchor
;
8052 case update_general_migration_checkpoint
:
8053 dprintf("imsm: prepare_update() "
8054 "for update_general_migration_checkpoint called\n");
8056 case update_takeover
: {
8057 struct imsm_update_takeover
*u
= (void *)update
->buf
;
8058 if (u
->direction
== R0_TO_R10
) {
8059 void **tail
= (void **)&update
->space_list
;
8060 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subarray
);
8061 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8062 int num_members
= map
->num_members
;
8066 /* allocate memory for added disks */
8067 for (i
= 0; i
< num_members
; i
++) {
8068 size
= sizeof(struct dl
);
8069 space
= malloc(size
);
8078 /* allocate memory for new device */
8079 size
= sizeof_imsm_dev(super
->devlist
->dev
, 0) +
8080 (num_members
* sizeof(__u32
));
8081 space
= malloc(size
);
8090 len
= disks_to_mpb_size(num_members
* 2);
8092 /* if allocation didn't success, free buffer */
8093 while (update
->space_list
) {
8094 void **sp
= update
->space_list
;
8095 update
->space_list
= *sp
;
8103 case update_reshape_container_disks
: {
8104 /* Every raid device in the container is about to
8105 * gain some more devices, and we will enter a
8107 * So each 'imsm_map' will be bigger, and the imsm_vol
8108 * will now hold 2 of them.
8109 * Thus we need new 'struct imsm_dev' allocations sized
8110 * as sizeof_imsm_dev but with more devices in both maps.
8112 struct imsm_update_reshape
*u
= (void *)update
->buf
;
8113 struct intel_dev
*dl
;
8114 void **space_tail
= (void**)&update
->space_list
;
8116 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
8118 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
8119 int size
= sizeof_imsm_dev(dl
->dev
, 1);
8121 if (u
->new_raid_disks
> u
->old_raid_disks
)
8122 size
+= sizeof(__u32
)*2*
8123 (u
->new_raid_disks
- u
->old_raid_disks
);
8132 len
= disks_to_mpb_size(u
->new_raid_disks
);
8133 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
8136 case update_reshape_migration
: {
8137 /* for migration level 0->5 we need to add disks
8138 * so the same as for container operation we will copy
8139 * device to the bigger location.
8140 * in memory prepared device and new disk area are prepared
8141 * for usage in process update
8143 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
8144 struct intel_dev
*id
;
8145 void **space_tail
= (void **)&update
->space_list
;
8148 int current_level
= -1;
8150 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
8152 /* add space for bigger array in update
8154 for (id
= super
->devlist
; id
; id
= id
->next
) {
8155 if (id
->index
== (unsigned)u
->subdev
) {
8156 size
= sizeof_imsm_dev(id
->dev
, 1);
8157 if (u
->new_raid_disks
> u
->old_raid_disks
)
8158 size
+= sizeof(__u32
)*2*
8159 (u
->new_raid_disks
- u
->old_raid_disks
);
8169 if (update
->space_list
== NULL
)
8172 /* add space for disk in update
8174 size
= sizeof(struct dl
);
8177 free(update
->space_list
);
8178 update
->space_list
= NULL
;
8185 /* add spare device to update
8187 for (id
= super
->devlist
; id
; id
= id
->next
)
8188 if (id
->index
== (unsigned)u
->subdev
) {
8189 struct imsm_dev
*dev
;
8190 struct imsm_map
*map
;
8192 dev
= get_imsm_dev(super
, u
->subdev
);
8193 map
= get_imsm_map(dev
, MAP_0
);
8194 current_level
= map
->raid_level
;
8197 if ((u
->new_level
== 5) && (u
->new_level
!= current_level
)) {
8198 struct mdinfo
*spares
;
8200 spares
= get_spares_for_grow(st
);
8208 makedev(dev
->disk
.major
,
8210 dl
= get_disk_super(super
,
8213 dl
->index
= u
->old_raid_disks
;
8219 len
= disks_to_mpb_size(u
->new_raid_disks
);
8220 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
8223 case update_create_array
: {
8224 struct imsm_update_create_array
*u
= (void *) update
->buf
;
8225 struct intel_dev
*dv
;
8226 struct imsm_dev
*dev
= &u
->dev
;
8227 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8229 struct disk_info
*inf
;
8233 inf
= get_disk_info(u
);
8234 len
= sizeof_imsm_dev(dev
, 1);
8235 /* allocate a new super->devlist entry */
8236 dv
= malloc(sizeof(*dv
));
8238 dv
->dev
= malloc(len
);
8243 update
->space
= NULL
;
8247 /* count how many spares will be converted to members */
8248 for (i
= 0; i
< map
->num_members
; i
++) {
8249 dl
= serial_to_dl(inf
[i
].serial
, super
);
8251 /* hmm maybe it failed?, nothing we can do about
8256 if (count_memberships(dl
, super
) == 0)
8259 len
+= activate
* sizeof(struct imsm_disk
);
8266 /* check if we need a larger metadata buffer */
8267 if (super
->next_buf
)
8268 buf_len
= super
->next_len
;
8270 buf_len
= super
->len
;
8272 if (__le32_to_cpu(mpb
->mpb_size
) + len
> buf_len
) {
8273 /* ok we need a larger buf than what is currently allocated
8274 * if this allocation fails process_update will notice that
8275 * ->next_len is set and ->next_buf is NULL
8277 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + len
, 512);
8278 if (super
->next_buf
)
8279 free(super
->next_buf
);
8281 super
->next_len
= buf_len
;
8282 if (posix_memalign(&super
->next_buf
, 512, buf_len
) == 0)
8283 memset(super
->next_buf
, 0, buf_len
);
8285 super
->next_buf
= NULL
;
8289 /* must be called while manager is quiesced */
8290 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
8292 struct imsm_super
*mpb
= super
->anchor
;
8294 struct imsm_dev
*dev
;
8295 struct imsm_map
*map
;
8296 int i
, j
, num_members
;
8299 dprintf("%s: deleting device[%d] from imsm_super\n",
8302 /* shift all indexes down one */
8303 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
8304 if (iter
->index
> (int)index
)
8306 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
8307 if (iter
->index
> (int)index
)
8310 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8311 dev
= get_imsm_dev(super
, i
);
8312 map
= get_imsm_map(dev
, MAP_0
);
8313 num_members
= map
->num_members
;
8314 for (j
= 0; j
< num_members
; j
++) {
8315 /* update ord entries being careful not to propagate
8316 * ord-flags to the first map
8318 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_X
);
8320 if (ord_to_idx(ord
) <= index
)
8323 map
= get_imsm_map(dev
, MAP_0
);
8324 set_imsm_ord_tbl_ent(map
, j
, ord_to_idx(ord
- 1));
8325 map
= get_imsm_map(dev
, MAP_1
);
8327 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
8332 super
->updates_pending
++;
8334 struct dl
*dl
= *dlp
;
8336 *dlp
= (*dlp
)->next
;
8337 __free_imsm_disk(dl
);
8340 #endif /* MDASSEMBLE */
8342 static void close_targets(int *targets
, int new_disks
)
8349 for (i
= 0; i
< new_disks
; i
++) {
8350 if (targets
[i
] >= 0) {
8357 static int imsm_get_allowed_degradation(int level
, int raid_disks
,
8358 struct intel_super
*super
,
8359 struct imsm_dev
*dev
)
8364 struct imsm_map
*map
;
8367 ret_val
= raid_disks
/2;
8368 /* check map if all disks pairs not failed
8371 map
= get_imsm_map(dev
, MAP_0
);
8372 for (i
= 0; i
< ret_val
; i
++) {
8373 int degradation
= 0;
8374 if (get_imsm_disk(super
, i
) == NULL
)
8376 if (get_imsm_disk(super
, i
+ 1) == NULL
)
8378 if (degradation
== 2)
8381 map
= get_imsm_map(dev
, MAP_1
);
8382 /* if there is no second map
8383 * result can be returned
8387 /* check degradation in second map
8389 for (i
= 0; i
< ret_val
; i
++) {
8390 int degradation
= 0;
8391 if (get_imsm_disk(super
, i
) == NULL
)
8393 if (get_imsm_disk(super
, i
+ 1) == NULL
)
8395 if (degradation
== 2)
8410 /*******************************************************************************
8411 * Function: open_backup_targets
8412 * Description: Function opens file descriptors for all devices given in
8415 * info : general array info
8416 * raid_disks : number of disks
8417 * raid_fds : table of device's file descriptors
8418 * super : intel super for raid10 degradation check
8419 * dev : intel device for raid10 degradation check
8423 ******************************************************************************/
8424 int open_backup_targets(struct mdinfo
*info
, int raid_disks
, int *raid_fds
,
8425 struct intel_super
*super
, struct imsm_dev
*dev
)
8431 for (i
= 0; i
< raid_disks
; i
++)
8434 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
8437 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
)) {
8438 dprintf("disk is faulty!!\n");
8442 if ((sd
->disk
.raid_disk
>= raid_disks
) ||
8443 (sd
->disk
.raid_disk
< 0))
8446 dn
= map_dev(sd
->disk
.major
,
8448 raid_fds
[sd
->disk
.raid_disk
] = dev_open(dn
, O_RDWR
);
8449 if (raid_fds
[sd
->disk
.raid_disk
] < 0) {
8450 fprintf(stderr
, "cannot open component\n");
8455 /* check if maximum array degradation level is not exceeded
8457 if ((raid_disks
- opened
) >
8458 imsm_get_allowed_degradation(info
->new_level
,
8461 fprintf(stderr
, "Not enough disks can be opened.\n");
8462 close_targets(raid_fds
, raid_disks
);
8469 /*******************************************************************************
8470 * Function: init_migr_record_imsm
8471 * Description: Function inits imsm migration record
8473 * super : imsm internal array info
8474 * dev : device under migration
8475 * info : general array info to find the smallest device
8478 ******************************************************************************/
8479 void init_migr_record_imsm(struct supertype
*st
, struct imsm_dev
*dev
,
8480 struct mdinfo
*info
)
8482 struct intel_super
*super
= st
->sb
;
8483 struct migr_record
*migr_rec
= super
->migr_rec
;
8485 unsigned long long dsize
, dev_sectors
;
8486 long long unsigned min_dev_sectors
= -1LLU;
8490 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
8491 struct imsm_map
*map_src
= get_imsm_map(dev
, MAP_1
);
8492 unsigned long long num_migr_units
;
8493 unsigned long long array_blocks
;
8495 memset(migr_rec
, 0, sizeof(struct migr_record
));
8496 migr_rec
->family_num
= __cpu_to_le32(super
->anchor
->family_num
);
8498 /* only ascending reshape supported now */
8499 migr_rec
->ascending_migr
= __cpu_to_le32(1);
8501 migr_rec
->dest_depth_per_unit
= GEN_MIGR_AREA_SIZE
/
8502 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
8503 migr_rec
->dest_depth_per_unit
*= map_dest
->blocks_per_strip
;
8504 new_data_disks
= imsm_num_data_members(dev
, MAP_0
);
8505 migr_rec
->blocks_per_unit
=
8506 __cpu_to_le32(migr_rec
->dest_depth_per_unit
* new_data_disks
);
8507 migr_rec
->dest_depth_per_unit
=
8508 __cpu_to_le32(migr_rec
->dest_depth_per_unit
);
8509 array_blocks
= info
->component_size
* new_data_disks
;
8511 array_blocks
/ __le32_to_cpu(migr_rec
->blocks_per_unit
);
8513 if (array_blocks
% __le32_to_cpu(migr_rec
->blocks_per_unit
))
8515 migr_rec
->num_migr_units
= __cpu_to_le32(num_migr_units
);
8517 migr_rec
->post_migr_vol_cap
= dev
->size_low
;
8518 migr_rec
->post_migr_vol_cap_hi
= dev
->size_high
;
8521 /* Find the smallest dev */
8522 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
8523 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
8524 fd
= dev_open(nm
, O_RDONLY
);
8527 get_dev_size(fd
, NULL
, &dsize
);
8528 dev_sectors
= dsize
/ 512;
8529 if (dev_sectors
< min_dev_sectors
)
8530 min_dev_sectors
= dev_sectors
;
8533 migr_rec
->ckpt_area_pba
= __cpu_to_le32(min_dev_sectors
-
8534 RAID_DISK_RESERVED_BLOCKS_IMSM_HI
);
8536 write_imsm_migr_rec(st
);
8541 /*******************************************************************************
8542 * Function: save_backup_imsm
8543 * Description: Function saves critical data stripes to Migration Copy Area
8544 * and updates the current migration unit status.
8545 * Use restore_stripes() to form a destination stripe,
8546 * and to write it to the Copy Area.
8548 * st : supertype information
8549 * dev : imsm device that backup is saved for
8550 * info : general array info
8551 * buf : input buffer
8552 * length : length of data to backup (blocks_per_unit)
8556 ******************************************************************************/
8557 int save_backup_imsm(struct supertype
*st
,
8558 struct imsm_dev
*dev
,
8559 struct mdinfo
*info
,
8564 struct intel_super
*super
= st
->sb
;
8565 unsigned long long *target_offsets
= NULL
;
8566 int *targets
= NULL
;
8568 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
8569 int new_disks
= map_dest
->num_members
;
8570 int dest_layout
= 0;
8572 unsigned long long start
;
8573 int data_disks
= imsm_num_data_members(dev
, MAP_0
);
8575 targets
= malloc(new_disks
* sizeof(int));
8579 for (i
= 0; i
< new_disks
; i
++)
8582 target_offsets
= malloc(new_disks
* sizeof(unsigned long long));
8583 if (!target_offsets
)
8586 start
= info
->reshape_progress
* 512;
8587 for (i
= 0; i
< new_disks
; i
++) {
8588 target_offsets
[i
] = (unsigned long long)
8589 __le32_to_cpu(super
->migr_rec
->ckpt_area_pba
) * 512;
8590 /* move back copy area adderss, it will be moved forward
8591 * in restore_stripes() using start input variable
8593 target_offsets
[i
] -= start
/data_disks
;
8596 if (open_backup_targets(info
, new_disks
, targets
,
8600 dest_layout
= imsm_level_to_layout(map_dest
->raid_level
);
8601 dest_chunk
= __le16_to_cpu(map_dest
->blocks_per_strip
) * 512;
8603 if (restore_stripes(targets
, /* list of dest devices */
8604 target_offsets
, /* migration record offsets */
8607 map_dest
->raid_level
,
8609 -1, /* source backup file descriptor */
8610 0, /* input buf offset
8611 * always 0 buf is already offseted */
8615 fprintf(stderr
, Name
": Error restoring stripes\n");
8623 close_targets(targets
, new_disks
);
8626 free(target_offsets
);
8631 /*******************************************************************************
8632 * Function: save_checkpoint_imsm
8633 * Description: Function called for current unit status update
8634 * in the migration record. It writes it to disk.
8636 * super : imsm internal array info
8637 * info : general array info
8641 * 2: failure, means no valid migration record
8642 * / no general migration in progress /
8643 ******************************************************************************/
8644 int save_checkpoint_imsm(struct supertype
*st
, struct mdinfo
*info
, int state
)
8646 struct intel_super
*super
= st
->sb
;
8647 unsigned long long blocks_per_unit
;
8648 unsigned long long curr_migr_unit
;
8650 if (load_imsm_migr_rec(super
, info
) != 0) {
8651 dprintf("imsm: ERROR: Cannot read migration record "
8652 "for checkpoint save.\n");
8656 blocks_per_unit
= __le32_to_cpu(super
->migr_rec
->blocks_per_unit
);
8657 if (blocks_per_unit
== 0) {
8658 dprintf("imsm: no migration in progress.\n");
8661 curr_migr_unit
= info
->reshape_progress
/ blocks_per_unit
;
8662 /* check if array is alligned to copy area
8663 * if it is not alligned, add one to current migration unit value
8664 * this can happend on array reshape finish only
8666 if (info
->reshape_progress
% blocks_per_unit
)
8669 super
->migr_rec
->curr_migr_unit
=
8670 __cpu_to_le32(curr_migr_unit
);
8671 super
->migr_rec
->rec_status
= __cpu_to_le32(state
);
8672 super
->migr_rec
->dest_1st_member_lba
=
8673 __cpu_to_le32(curr_migr_unit
*
8674 __le32_to_cpu(super
->migr_rec
->dest_depth_per_unit
));
8675 if (write_imsm_migr_rec(st
) < 0) {
8676 dprintf("imsm: Cannot write migration record "
8677 "outside backup area\n");
8684 /*******************************************************************************
8685 * Function: recover_backup_imsm
8686 * Description: Function recovers critical data from the Migration Copy Area
8687 * while assembling an array.
8689 * super : imsm internal array info
8690 * info : general array info
8692 * 0 : success (or there is no data to recover)
8694 ******************************************************************************/
8695 int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
)
8697 struct intel_super
*super
= st
->sb
;
8698 struct migr_record
*migr_rec
= super
->migr_rec
;
8699 struct imsm_map
*map_dest
= NULL
;
8700 struct intel_dev
*id
= NULL
;
8701 unsigned long long read_offset
;
8702 unsigned long long write_offset
;
8704 int *targets
= NULL
;
8705 int new_disks
, i
, err
;
8708 unsigned long curr_migr_unit
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
8709 unsigned long num_migr_units
= __le32_to_cpu(migr_rec
->num_migr_units
);
8711 int skipped_disks
= 0;
8713 err
= sysfs_get_str(info
, NULL
, "array_state", (char *)buffer
, 20);
8717 /* recover data only during assemblation */
8718 if (strncmp(buffer
, "inactive", 8) != 0)
8720 /* no data to recover */
8721 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
8723 if (curr_migr_unit
>= num_migr_units
)
8726 /* find device during reshape */
8727 for (id
= super
->devlist
; id
; id
= id
->next
)
8728 if (is_gen_migration(id
->dev
))
8733 map_dest
= get_imsm_map(id
->dev
, MAP_0
);
8734 new_disks
= map_dest
->num_members
;
8736 read_offset
= (unsigned long long)
8737 __le32_to_cpu(migr_rec
->ckpt_area_pba
) * 512;
8739 write_offset
= ((unsigned long long)
8740 __le32_to_cpu(migr_rec
->dest_1st_member_lba
) +
8741 __le32_to_cpu(map_dest
->pba_of_lba0
)) * 512;
8743 unit_len
= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
8744 if (posix_memalign((void **)&buf
, 512, unit_len
) != 0)
8746 targets
= malloc(new_disks
* sizeof(int));
8750 if (open_backup_targets(info
, new_disks
, targets
, super
, id
->dev
)) {
8752 Name
": Cannot open some devices belonging to array.\n");
8756 for (i
= 0; i
< new_disks
; i
++) {
8757 if (targets
[i
] < 0) {
8761 if (lseek64(targets
[i
], read_offset
, SEEK_SET
) < 0) {
8763 Name
": Cannot seek to block: %s\n",
8768 if ((unsigned)read(targets
[i
], buf
, unit_len
) != unit_len
) {
8770 Name
": Cannot read copy area block: %s\n",
8775 if (lseek64(targets
[i
], write_offset
, SEEK_SET
) < 0) {
8777 Name
": Cannot seek to block: %s\n",
8782 if ((unsigned)write(targets
[i
], buf
, unit_len
) != unit_len
) {
8784 Name
": Cannot restore block: %s\n",
8791 if (skipped_disks
> imsm_get_allowed_degradation(info
->new_level
,
8796 Name
": Cannot restore data from backup."
8797 " Too many failed disks\n");
8801 if (save_checkpoint_imsm(st
, info
, UNIT_SRC_NORMAL
)) {
8802 /* ignore error == 2, this can mean end of reshape here
8804 dprintf("imsm: Cannot write checkpoint to "
8805 "migration record (UNIT_SRC_NORMAL) during restart\n");
8811 for (i
= 0; i
< new_disks
; i
++)
8820 static char disk_by_path
[] = "/dev/disk/by-path/";
8822 static const char *imsm_get_disk_controller_domain(const char *path
)
8824 char disk_path
[PATH_MAX
];
8828 strncpy(disk_path
, disk_by_path
, PATH_MAX
- 1);
8829 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
8830 if (stat(disk_path
, &st
) == 0) {
8831 struct sys_dev
* hba
;
8834 path
= devt_to_devpath(st
.st_rdev
);
8837 hba
= find_disk_attached_hba(-1, path
);
8838 if (hba
&& hba
->type
== SYS_DEV_SAS
)
8840 else if (hba
&& hba
->type
== SYS_DEV_SATA
)
8844 dprintf("path: %s hba: %s attached: %s\n",
8845 path
, (hba
) ? hba
->path
: "NULL", drv
);
8853 static int imsm_find_array_minor_by_subdev(int subdev
, int container
, int *minor
)
8855 char subdev_name
[20];
8856 struct mdstat_ent
*mdstat
;
8858 sprintf(subdev_name
, "%d", subdev
);
8859 mdstat
= mdstat_by_subdev(subdev_name
, container
);
8863 *minor
= mdstat
->devnum
;
8864 free_mdstat(mdstat
);
8868 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
8869 struct geo_params
*geo
,
8870 int *old_raid_disks
)
8872 /* currently we only support increasing the number of devices
8873 * for a container. This increases the number of device for each
8874 * member array. They must all be RAID0 or RAID5.
8877 struct mdinfo
*info
, *member
;
8878 int devices_that_can_grow
= 0;
8880 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): "
8881 "st->devnum = (%i)\n",
8884 if (geo
->size
!= -1 ||
8885 geo
->level
!= UnSet
||
8886 geo
->layout
!= UnSet
||
8887 geo
->chunksize
!= 0 ||
8888 geo
->raid_disks
== UnSet
) {
8889 dprintf("imsm: Container operation is allowed for "
8890 "raid disks number change only.\n");
8894 info
= container_content_imsm(st
, NULL
);
8895 for (member
= info
; member
; member
= member
->next
) {
8899 dprintf("imsm: checking device_num: %i\n",
8900 member
->container_member
);
8902 if (geo
->raid_disks
<= member
->array
.raid_disks
) {
8903 /* we work on container for Online Capacity Expansion
8904 * only so raid_disks has to grow
8906 dprintf("imsm: for container operation raid disks "
8907 "increase is required\n");
8911 if ((info
->array
.level
!= 0) &&
8912 (info
->array
.level
!= 5)) {
8913 /* we cannot use this container with other raid level
8915 dprintf("imsm: for container operation wrong"
8916 " raid level (%i) detected\n",
8920 /* check for platform support
8921 * for this raid level configuration
8923 struct intel_super
*super
= st
->sb
;
8924 if (!is_raid_level_supported(super
->orom
,
8925 member
->array
.level
,
8927 dprintf("platform does not support raid%d with"
8931 geo
->raid_disks
> 1 ? "s" : "");
8934 /* check if component size is aligned to chunk size
8936 if (info
->component_size
%
8937 (info
->array
.chunk_size
/512)) {
8938 dprintf("Component size is not aligned to "
8944 if (*old_raid_disks
&&
8945 info
->array
.raid_disks
!= *old_raid_disks
)
8947 *old_raid_disks
= info
->array
.raid_disks
;
8949 /* All raid5 and raid0 volumes in container
8950 * have to be ready for Online Capacity Expansion
8951 * so they need to be assembled. We have already
8952 * checked that no recovery etc is happening.
8954 result
= imsm_find_array_minor_by_subdev(member
->container_member
,
8958 dprintf("imsm: cannot find array\n");
8961 devices_that_can_grow
++;
8964 if (!member
&& devices_that_can_grow
)
8968 dprintf("\tContainer operation allowed\n");
8970 dprintf("\tError: %i\n", ret_val
);
8975 /* Function: get_spares_for_grow
8976 * Description: Allocates memory and creates list of spare devices
8977 * avaliable in container. Checks if spare drive size is acceptable.
8978 * Parameters: Pointer to the supertype structure
8979 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
8982 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
8984 unsigned long long min_size
= min_acceptable_spare_size_imsm(st
);
8985 return container_choose_spares(st
, min_size
, NULL
, NULL
, NULL
, 0);
8988 /******************************************************************************
8989 * function: imsm_create_metadata_update_for_reshape
8990 * Function creates update for whole IMSM container.
8992 ******************************************************************************/
8993 static int imsm_create_metadata_update_for_reshape(
8994 struct supertype
*st
,
8995 struct geo_params
*geo
,
8997 struct imsm_update_reshape
**updatep
)
8999 struct intel_super
*super
= st
->sb
;
9000 struct imsm_super
*mpb
= super
->anchor
;
9001 int update_memory_size
= 0;
9002 struct imsm_update_reshape
*u
= NULL
;
9003 struct mdinfo
*spares
= NULL
;
9005 int delta_disks
= 0;
9008 dprintf("imsm_update_metadata_for_reshape(enter) raid_disks = %i\n",
9011 delta_disks
= geo
->raid_disks
- old_raid_disks
;
9013 /* size of all update data without anchor */
9014 update_memory_size
= sizeof(struct imsm_update_reshape
);
9016 /* now add space for spare disks that we need to add. */
9017 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
9019 u
= calloc(1, update_memory_size
);
9022 "cannot get memory for imsm_update_reshape update\n");
9025 u
->type
= update_reshape_container_disks
;
9026 u
->old_raid_disks
= old_raid_disks
;
9027 u
->new_raid_disks
= geo
->raid_disks
;
9029 /* now get spare disks list
9031 spares
= get_spares_for_grow(st
);
9034 || delta_disks
> spares
->array
.spare_disks
) {
9035 fprintf(stderr
, Name
": imsm: ERROR: Cannot get spare devices "
9036 "for %s.\n", geo
->dev_name
);
9041 /* we have got spares
9042 * update disk list in imsm_disk list table in anchor
9044 dprintf("imsm: %i spares are available.\n\n",
9045 spares
->array
.spare_disks
);
9048 for (i
= 0; i
< delta_disks
; i
++) {
9053 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
9055 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
9056 dl
->index
= mpb
->num_disks
;
9066 dprintf("imsm: reshape update preparation :");
9067 if (i
== delta_disks
) {
9070 return update_memory_size
;
9073 dprintf(" Error\n");
9078 /******************************************************************************
9079 * function: imsm_create_metadata_update_for_migration()
9080 * Creates update for IMSM array.
9082 ******************************************************************************/
9083 static int imsm_create_metadata_update_for_migration(
9084 struct supertype
*st
,
9085 struct geo_params
*geo
,
9086 struct imsm_update_reshape_migration
**updatep
)
9088 struct intel_super
*super
= st
->sb
;
9089 int update_memory_size
= 0;
9090 struct imsm_update_reshape_migration
*u
= NULL
;
9091 struct imsm_dev
*dev
;
9092 int previous_level
= -1;
9094 dprintf("imsm_create_metadata_update_for_migration(enter)"
9095 " New Level = %i\n", geo
->level
);
9097 /* size of all update data without anchor */
9098 update_memory_size
= sizeof(struct imsm_update_reshape_migration
);
9100 u
= calloc(1, update_memory_size
);
9102 dprintf("error: cannot get memory for "
9103 "imsm_create_metadata_update_for_migration\n");
9106 u
->type
= update_reshape_migration
;
9107 u
->subdev
= super
->current_vol
;
9108 u
->new_level
= geo
->level
;
9109 u
->new_layout
= geo
->layout
;
9110 u
->new_raid_disks
= u
->old_raid_disks
= geo
->raid_disks
;
9111 u
->new_disks
[0] = -1;
9112 u
->new_chunksize
= -1;
9114 dev
= get_imsm_dev(super
, u
->subdev
);
9116 struct imsm_map
*map
;
9118 map
= get_imsm_map(dev
, MAP_0
);
9120 int current_chunk_size
=
9121 __le16_to_cpu(map
->blocks_per_strip
) / 2;
9123 if (geo
->chunksize
!= current_chunk_size
) {
9124 u
->new_chunksize
= geo
->chunksize
/ 1024;
9126 "chunk size change from %i to %i\n",
9127 current_chunk_size
, u
->new_chunksize
);
9129 previous_level
= map
->raid_level
;
9132 if ((geo
->level
== 5) && (previous_level
== 0)) {
9133 struct mdinfo
*spares
= NULL
;
9135 u
->new_raid_disks
++;
9136 spares
= get_spares_for_grow(st
);
9137 if ((spares
== NULL
) || (spares
->array
.spare_disks
< 1)) {
9140 update_memory_size
= 0;
9141 dprintf("error: cannot get spare device "
9142 "for requested migration");
9147 dprintf("imsm: reshape update preparation : OK\n");
9150 return update_memory_size
;
9153 static void imsm_update_metadata_locally(struct supertype
*st
,
9156 struct metadata_update mu
;
9161 mu
.space_list
= NULL
;
9163 imsm_prepare_update(st
, &mu
);
9164 imsm_process_update(st
, &mu
);
9166 while (mu
.space_list
) {
9167 void **space
= mu
.space_list
;
9168 mu
.space_list
= *space
;
9173 /***************************************************************************
9174 * Function: imsm_analyze_change
9175 * Description: Function analyze change for single volume
9176 * and validate if transition is supported
9177 * Parameters: Geometry parameters, supertype structure
9178 * Returns: Operation type code on success, -1 if fail
9179 ****************************************************************************/
9180 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
9181 struct geo_params
*geo
)
9187 /* number of added/removed disks in operation result */
9188 int devNumChange
= 0;
9189 /* imsm compatible layout value for array geometry verification */
9190 int imsm_layout
= -1;
9192 getinfo_super_imsm_volume(st
, &info
, NULL
);
9193 if ((geo
->level
!= info
.array
.level
) &&
9194 (geo
->level
>= 0) &&
9195 (geo
->level
!= UnSet
)) {
9196 switch (info
.array
.level
) {
9198 if (geo
->level
== 5) {
9199 change
= CH_MIGRATION
;
9200 if (geo
->layout
!= ALGORITHM_LEFT_ASYMMETRIC
) {
9202 Name
" Error. Requested Layout "
9203 "not supported (left-asymmetric layout "
9204 "is supported only)!\n");
9206 goto analyse_change_exit
;
9208 imsm_layout
= geo
->layout
;
9210 devNumChange
= 1; /* parity disk added */
9211 } else if (geo
->level
== 10) {
9212 change
= CH_TAKEOVER
;
9214 devNumChange
= 2; /* two mirrors added */
9215 imsm_layout
= 0x102; /* imsm supported layout */
9220 if (geo
->level
== 0) {
9221 change
= CH_TAKEOVER
;
9223 devNumChange
= -(geo
->raid_disks
/2);
9224 imsm_layout
= 0; /* imsm raid0 layout */
9230 Name
" Error. Level Migration from %d to %d "
9232 info
.array
.level
, geo
->level
);
9233 goto analyse_change_exit
;
9236 geo
->level
= info
.array
.level
;
9238 if ((geo
->layout
!= info
.array
.layout
)
9239 && ((geo
->layout
!= UnSet
) && (geo
->layout
!= -1))) {
9240 change
= CH_MIGRATION
;
9241 if ((info
.array
.layout
== 0)
9242 && (info
.array
.level
== 5)
9243 && (geo
->layout
== 5)) {
9244 /* reshape 5 -> 4 */
9245 } else if ((info
.array
.layout
== 5)
9246 && (info
.array
.level
== 5)
9247 && (geo
->layout
== 0)) {
9248 /* reshape 4 -> 5 */
9253 Name
" Error. Layout Migration from %d to %d "
9255 info
.array
.layout
, geo
->layout
);
9257 goto analyse_change_exit
;
9260 geo
->layout
= info
.array
.layout
;
9261 if (imsm_layout
== -1)
9262 imsm_layout
= info
.array
.layout
;
9265 if ((geo
->chunksize
> 0) && (geo
->chunksize
!= UnSet
)
9266 && (geo
->chunksize
!= info
.array
.chunk_size
))
9267 change
= CH_MIGRATION
;
9269 geo
->chunksize
= info
.array
.chunk_size
;
9271 chunk
= geo
->chunksize
/ 1024;
9272 if (!validate_geometry_imsm(st
,
9275 geo
->raid_disks
+ devNumChange
,
9282 struct intel_super
*super
= st
->sb
;
9283 struct imsm_super
*mpb
= super
->anchor
;
9285 if (mpb
->num_raid_devs
> 1) {
9287 Name
" Error. Cannot perform operation on %s"
9288 "- for this operation it MUST be single "
9289 "array in container\n",
9295 analyse_change_exit
:
9300 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
9302 struct intel_super
*super
= st
->sb
;
9303 struct imsm_update_takeover
*u
;
9305 u
= malloc(sizeof(struct imsm_update_takeover
));
9309 u
->type
= update_takeover
;
9310 u
->subarray
= super
->current_vol
;
9312 /* 10->0 transition */
9313 if (geo
->level
== 0)
9314 u
->direction
= R10_TO_R0
;
9316 /* 0->10 transition */
9317 if (geo
->level
== 10)
9318 u
->direction
= R0_TO_R10
;
9320 /* update metadata locally */
9321 imsm_update_metadata_locally(st
, u
,
9322 sizeof(struct imsm_update_takeover
));
9323 /* and possibly remotely */
9324 if (st
->update_tail
)
9325 append_metadata_update(st
, u
,
9326 sizeof(struct imsm_update_takeover
));
9333 static int imsm_reshape_super(struct supertype
*st
, long long size
, int level
,
9334 int layout
, int chunksize
, int raid_disks
,
9335 int delta_disks
, char *backup
, char *dev
,
9339 struct geo_params geo
;
9341 dprintf("imsm: reshape_super called.\n");
9343 memset(&geo
, 0, sizeof(struct geo_params
));
9346 geo
.dev_id
= st
->devnum
;
9349 geo
.layout
= layout
;
9350 geo
.chunksize
= chunksize
;
9351 geo
.raid_disks
= raid_disks
;
9352 if (delta_disks
!= UnSet
)
9353 geo
.raid_disks
+= delta_disks
;
9355 dprintf("\tfor level : %i\n", geo
.level
);
9356 dprintf("\tfor raid_disks : %i\n", geo
.raid_disks
);
9358 if (experimental() == 0)
9361 if (st
->container_dev
== st
->devnum
) {
9362 /* On container level we can only increase number of devices. */
9363 dprintf("imsm: info: Container operation\n");
9364 int old_raid_disks
= 0;
9366 if (imsm_reshape_is_allowed_on_container(
9367 st
, &geo
, &old_raid_disks
)) {
9368 struct imsm_update_reshape
*u
= NULL
;
9371 len
= imsm_create_metadata_update_for_reshape(
9372 st
, &geo
, old_raid_disks
, &u
);
9375 dprintf("imsm: Cannot prepare update\n");
9376 goto exit_imsm_reshape_super
;
9380 /* update metadata locally */
9381 imsm_update_metadata_locally(st
, u
, len
);
9382 /* and possibly remotely */
9383 if (st
->update_tail
)
9384 append_metadata_update(st
, u
, len
);
9389 fprintf(stderr
, Name
": (imsm) Operation "
9390 "is not allowed on this container\n");
9393 /* On volume level we support following operations
9394 * - takeover: raid10 -> raid0; raid0 -> raid10
9395 * - chunk size migration
9396 * - migration: raid5 -> raid0; raid0 -> raid5
9398 struct intel_super
*super
= st
->sb
;
9399 struct intel_dev
*dev
= super
->devlist
;
9401 dprintf("imsm: info: Volume operation\n");
9402 /* find requested device */
9404 if (imsm_find_array_minor_by_subdev(
9405 dev
->index
, st
->container_dev
, &devnum
) == 0
9406 && devnum
== geo
.dev_id
)
9411 fprintf(stderr
, Name
" Cannot find %s (%i) subarray\n",
9412 geo
.dev_name
, geo
.dev_id
);
9413 goto exit_imsm_reshape_super
;
9415 super
->current_vol
= dev
->index
;
9416 change
= imsm_analyze_change(st
, &geo
);
9419 ret_val
= imsm_takeover(st
, &geo
);
9421 case CH_MIGRATION
: {
9422 struct imsm_update_reshape_migration
*u
= NULL
;
9424 imsm_create_metadata_update_for_migration(
9428 "Cannot prepare update\n");
9432 /* update metadata locally */
9433 imsm_update_metadata_locally(st
, u
, len
);
9434 /* and possibly remotely */
9435 if (st
->update_tail
)
9436 append_metadata_update(st
, u
, len
);
9446 exit_imsm_reshape_super
:
9447 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
9451 /*******************************************************************************
9452 * Function: wait_for_reshape_imsm
9453 * Description: Function writes new sync_max value and waits until
9454 * reshape process reach new position
9456 * sra : general array info
9457 * ndata : number of disks in new array's layout
9460 * 1 : there is no reshape in progress,
9462 ******************************************************************************/
9463 int wait_for_reshape_imsm(struct mdinfo
*sra
, int ndata
)
9465 int fd
= sysfs_get_fd(sra
, NULL
, "reshape_position");
9466 unsigned long long completed
;
9467 /* to_complete : new sync_max position */
9468 unsigned long long to_complete
= sra
->reshape_progress
;
9469 unsigned long long position_to_set
= to_complete
/ ndata
;
9472 dprintf("imsm: wait_for_reshape_imsm() "
9473 "cannot open reshape_position\n");
9477 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
9478 dprintf("imsm: wait_for_reshape_imsm() "
9479 "cannot read reshape_position (no reshape in progres)\n");
9484 if (completed
> to_complete
) {
9485 dprintf("imsm: wait_for_reshape_imsm() "
9486 "wrong next position to set %llu (%llu)\n",
9487 to_complete
, completed
);
9491 dprintf("Position set: %llu\n", position_to_set
);
9492 if (sysfs_set_num(sra
, NULL
, "sync_max",
9493 position_to_set
) != 0) {
9494 dprintf("imsm: wait_for_reshape_imsm() "
9495 "cannot set reshape position to %llu\n",
9506 select(fd
+1, &rfds
, NULL
, NULL
, NULL
);
9507 if (sysfs_get_str(sra
, NULL
, "sync_action",
9509 strncmp(action
, "reshape", 7) != 0)
9511 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
9512 dprintf("imsm: wait_for_reshape_imsm() "
9513 "cannot read reshape_position (in loop)\n");
9517 } while (completed
< to_complete
);
9523 /*******************************************************************************
9524 * Function: check_degradation_change
9525 * Description: Check that array hasn't become failed.
9527 * info : for sysfs access
9528 * sources : source disks descriptors
9529 * degraded: previous degradation level
9532 ******************************************************************************/
9533 int check_degradation_change(struct mdinfo
*info
,
9537 unsigned long long new_degraded
;
9538 sysfs_get_ll(info
, NULL
, "degraded", &new_degraded
);
9539 if (new_degraded
!= (unsigned long long)degraded
) {
9540 /* check each device to ensure it is still working */
9543 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
9544 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
9546 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
9548 if (sysfs_get_str(info
,
9549 sd
, "state", sbuf
, 20) < 0 ||
9550 strstr(sbuf
, "faulty") ||
9551 strstr(sbuf
, "in_sync") == NULL
) {
9552 /* this device is dead */
9553 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
9554 if (sd
->disk
.raid_disk
>= 0 &&
9555 sources
[sd
->disk
.raid_disk
] >= 0) {
9557 sd
->disk
.raid_disk
]);
9558 sources
[sd
->disk
.raid_disk
] =
9567 return new_degraded
;
9570 /*******************************************************************************
9571 * Function: imsm_manage_reshape
9572 * Description: Function finds array under reshape and it manages reshape
9573 * process. It creates stripes backups (if required) and sets
9576 * afd : Backup handle (nattive) - not used
9577 * sra : general array info
9578 * reshape : reshape parameters - not used
9579 * st : supertype structure
9580 * blocks : size of critical section [blocks]
9581 * fds : table of source device descriptor
9582 * offsets : start of array (offest per devices)
9584 * destfd : table of destination device descriptor
9585 * destoffsets : table of destination offsets (per device)
9587 * 1 : success, reshape is done
9589 ******************************************************************************/
9590 static int imsm_manage_reshape(
9591 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
9592 struct supertype
*st
, unsigned long backup_blocks
,
9593 int *fds
, unsigned long long *offsets
,
9594 int dests
, int *destfd
, unsigned long long *destoffsets
)
9597 struct intel_super
*super
= st
->sb
;
9598 struct intel_dev
*dv
= NULL
;
9599 struct imsm_dev
*dev
= NULL
;
9600 struct imsm_map
*map_src
;
9601 int migr_vol_qan
= 0;
9602 int ndata
, odata
; /* [bytes] */
9603 int chunk
; /* [bytes] */
9604 struct migr_record
*migr_rec
;
9606 unsigned int buf_size
; /* [bytes] */
9607 unsigned long long max_position
; /* array size [bytes] */
9608 unsigned long long next_step
; /* [blocks]/[bytes] */
9609 unsigned long long old_data_stripe_length
;
9610 unsigned long long start_src
; /* [bytes] */
9611 unsigned long long start
; /* [bytes] */
9612 unsigned long long start_buf_shift
; /* [bytes] */
9614 int source_layout
= 0;
9616 if (!fds
|| !offsets
|| !sra
)
9619 /* Find volume during the reshape */
9620 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
9621 if (dv
->dev
->vol
.migr_type
== MIGR_GEN_MIGR
9622 && dv
->dev
->vol
.migr_state
== 1) {
9627 /* Only one volume can migrate at the same time */
9628 if (migr_vol_qan
!= 1) {
9629 fprintf(stderr
, Name
" : %s", migr_vol_qan
?
9630 "Number of migrating volumes greater than 1\n" :
9631 "There is no volume during migrationg\n");
9635 map_src
= get_imsm_map(dev
, MAP_1
);
9636 if (map_src
== NULL
)
9639 ndata
= imsm_num_data_members(dev
, MAP_0
);
9640 odata
= imsm_num_data_members(dev
, MAP_1
);
9642 chunk
= __le16_to_cpu(map_src
->blocks_per_strip
) * 512;
9643 old_data_stripe_length
= odata
* chunk
;
9645 migr_rec
= super
->migr_rec
;
9647 /* initialize migration record for start condition */
9648 if (sra
->reshape_progress
== 0)
9649 init_migr_record_imsm(st
, dev
, sra
);
9651 if (__le32_to_cpu(migr_rec
->rec_status
) != UNIT_SRC_NORMAL
) {
9652 dprintf("imsm: cannot restart migration when data "
9653 "are present in copy area.\n");
9659 buf_size
= __le32_to_cpu(migr_rec
->blocks_per_unit
) * 512;
9660 /* extend buffer size for parity disk */
9661 buf_size
+= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
9662 /* add space for stripe aligment */
9663 buf_size
+= old_data_stripe_length
;
9664 if (posix_memalign((void **)&buf
, 4096, buf_size
)) {
9665 dprintf("imsm: Cannot allocate checpoint buffer\n");
9669 max_position
= sra
->component_size
* ndata
;
9670 source_layout
= imsm_level_to_layout(map_src
->raid_level
);
9672 while (__le32_to_cpu(migr_rec
->curr_migr_unit
) <
9673 __le32_to_cpu(migr_rec
->num_migr_units
)) {
9674 /* current reshape position [blocks] */
9675 unsigned long long current_position
=
9676 __le32_to_cpu(migr_rec
->blocks_per_unit
)
9677 * __le32_to_cpu(migr_rec
->curr_migr_unit
);
9678 unsigned long long border
;
9680 /* Check that array hasn't become failed.
9682 degraded
= check_degradation_change(sra
, fds
, degraded
);
9684 dprintf("imsm: Abort reshape due to degradation"
9685 " level (%i)\n", degraded
);
9689 next_step
= __le32_to_cpu(migr_rec
->blocks_per_unit
);
9691 if ((current_position
+ next_step
) > max_position
)
9692 next_step
= max_position
- current_position
;
9694 start
= current_position
* 512;
9696 /* allign reading start to old geometry */
9697 start_buf_shift
= start
% old_data_stripe_length
;
9698 start_src
= start
- start_buf_shift
;
9700 border
= (start_src
/ odata
) - (start
/ ndata
);
9702 if (border
<= __le32_to_cpu(migr_rec
->dest_depth_per_unit
)) {
9703 /* save critical stripes to buf
9704 * start - start address of current unit
9706 * start_src - start address of current unit
9707 * to backup alligned to source array
9710 unsigned long long next_step_filler
= 0;
9711 unsigned long long copy_length
= next_step
* 512;
9713 /* allign copy area length to stripe in old geometry */
9714 next_step_filler
= ((copy_length
+ start_buf_shift
)
9715 % old_data_stripe_length
);
9716 if (next_step_filler
)
9717 next_step_filler
= (old_data_stripe_length
9718 - next_step_filler
);
9719 dprintf("save_stripes() parameters: start = %llu,"
9720 "\tstart_src = %llu,\tnext_step*512 = %llu,"
9721 "\tstart_in_buf_shift = %llu,"
9722 "\tnext_step_filler = %llu\n",
9723 start
, start_src
, copy_length
,
9724 start_buf_shift
, next_step_filler
);
9726 if (save_stripes(fds
, offsets
, map_src
->num_members
,
9727 chunk
, map_src
->raid_level
,
9728 source_layout
, 0, NULL
, start_src
,
9730 next_step_filler
+ start_buf_shift
,
9732 dprintf("imsm: Cannot save stripes"
9736 /* Convert data to destination format and store it
9737 * in backup general migration area
9739 if (save_backup_imsm(st
, dev
, sra
,
9740 buf
+ start_buf_shift
, copy_length
)) {
9741 dprintf("imsm: Cannot save stripes to "
9742 "target devices\n");
9745 if (save_checkpoint_imsm(st
, sra
,
9746 UNIT_SRC_IN_CP_AREA
)) {
9747 dprintf("imsm: Cannot write checkpoint to "
9748 "migration record (UNIT_SRC_IN_CP_AREA)\n");
9752 /* set next step to use whole border area */
9753 border
/= next_step
;
9755 next_step
*= border
;
9757 /* When data backed up, checkpoint stored,
9758 * kick the kernel to reshape unit of data
9760 next_step
= next_step
+ sra
->reshape_progress
;
9761 /* limit next step to array max position */
9762 if (next_step
> max_position
)
9763 next_step
= max_position
;
9764 sysfs_set_num(sra
, NULL
, "suspend_lo", sra
->reshape_progress
);
9765 sysfs_set_num(sra
, NULL
, "suspend_hi", next_step
);
9766 sra
->reshape_progress
= next_step
;
9768 /* wait until reshape finish */
9769 if (wait_for_reshape_imsm(sra
, ndata
) < 0) {
9770 dprintf("wait_for_reshape_imsm returned error!\n");
9774 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
9775 /* ignore error == 2, this can mean end of reshape here
9777 dprintf("imsm: Cannot write checkpoint to "
9778 "migration record (UNIT_SRC_NORMAL)\n");
9784 /* return '1' if done */
9792 #endif /* MDASSEMBLE */
9794 struct superswitch super_imsm
= {
9796 .examine_super
= examine_super_imsm
,
9797 .brief_examine_super
= brief_examine_super_imsm
,
9798 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
9799 .export_examine_super
= export_examine_super_imsm
,
9800 .detail_super
= detail_super_imsm
,
9801 .brief_detail_super
= brief_detail_super_imsm
,
9802 .write_init_super
= write_init_super_imsm
,
9803 .validate_geometry
= validate_geometry_imsm
,
9804 .add_to_super
= add_to_super_imsm
,
9805 .remove_from_super
= remove_from_super_imsm
,
9806 .detail_platform
= detail_platform_imsm
,
9807 .kill_subarray
= kill_subarray_imsm
,
9808 .update_subarray
= update_subarray_imsm
,
9809 .load_container
= load_container_imsm
,
9810 .default_geometry
= default_geometry_imsm
,
9811 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
9812 .reshape_super
= imsm_reshape_super
,
9813 .manage_reshape
= imsm_manage_reshape
,
9814 .recover_backup
= recover_backup_imsm
,
9816 .match_home
= match_home_imsm
,
9817 .uuid_from_super
= uuid_from_super_imsm
,
9818 .getinfo_super
= getinfo_super_imsm
,
9819 .getinfo_super_disks
= getinfo_super_disks_imsm
,
9820 .update_super
= update_super_imsm
,
9822 .avail_size
= avail_size_imsm
,
9823 .min_acceptable_spare_size
= min_acceptable_spare_size_imsm
,
9825 .compare_super
= compare_super_imsm
,
9827 .load_super
= load_super_imsm
,
9828 .init_super
= init_super_imsm
,
9829 .store_super
= store_super_imsm
,
9830 .free_super
= free_super_imsm
,
9831 .match_metadata_desc
= match_metadata_desc_imsm
,
9832 .container_content
= container_content_imsm
,
9840 .open_new
= imsm_open_new
,
9841 .set_array_state
= imsm_set_array_state
,
9842 .set_disk
= imsm_set_disk
,
9843 .sync_metadata
= imsm_sync_metadata
,
9844 .activate_spare
= imsm_activate_spare
,
9845 .process_update
= imsm_process_update
,
9846 .prepare_update
= imsm_prepare_update
,
9847 #endif /* MDASSEMBLE */